Exemple #1
0
        /// <summary>
        /// Get the public key from a key pair blob
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="keyPair"/> is null
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the key could not be extracted
        /// </exception>
        /// <param name="keyPair">key blob to get the public key from</param>
        /// <returns>public key blob</returns>
        public static byte[] ExtractPublicKeyFromKeyPair(byte[] keyPair)
        {
            if (keyPair == null)
            {
                throw new ArgumentNullException("keyPair");
            }

            // extract the public key portion of the blob
            IntPtr publicKeyBuffer = IntPtr.Zero;

            try
            {
                int publicKeyBlobSize = 0;

                if (!StrongNameNative.StrongNameGetPublicKey(
                        null, keyPair, keyPair.Length, out publicKeyBuffer, out publicKeyBlobSize))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                // copy the key out of unmanaged memory, and return it
                byte[] publicKeyBlob = new byte[publicKeyBlobSize];
                Marshal.Copy(publicKeyBuffer, publicKeyBlob, 0, publicKeyBlobSize);
                return(publicKeyBlob);
            }
            finally
            {
                if (publicKeyBuffer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(publicKeyBuffer);
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///     Get the public key from a key container
        /// </summary>
        /// <exception cref="ArgumentNullException">
        ///     If <paramref name="keyContainer"/> is null
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     If <paramref name="keyContainer"/> is empty
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///     If the key could not be extracted
        /// </exception>
        /// <param name="keyContainer">key container to get the public key from</param>
        /// <returns>public key blob</returns>
        public static byte[] ExtractPublicKeyFromKeyContainer(string keyContainerName)
        {
            if (string.IsNullOrEmpty(keyContainerName))
            {
                throw new ArgumentNullOrEmptyException("keyContainerName");
            }

            // extract the public key portion of the blob
            IntPtr publicKeyBuffer = IntPtr.Zero;

            try
            {
                int publicKeyBlobSize = 0;

                if (!StrongNameNative.StrongNameGetPublicKey(
                        keyContainerName, null, 0, out publicKeyBuffer, out publicKeyBlobSize))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                // copy the key out of unmanaged memory, and return it
                byte[] publicKeyBlob = new byte[publicKeyBlobSize];
                Marshal.Copy(publicKeyBuffer, publicKeyBlob, 0, publicKeyBlobSize);
                return(publicKeyBlob);
            }
            finally
            {
                if (publicKeyBuffer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(publicKeyBuffer);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Generate a key pair blob
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="keySize"/> is not positive
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the key could not be generated
        /// </exception>
        /// <param name="keySize">size, in bits, of the key to generate</param>
        /// <returns>generated key pair blob</returns>
        public static byte[] GenerateKeyPair(int keySize)
        {
            if (keySize <= 0)
            {
                throw new ArgumentOutOfRangeException("keySize");
            }

            // variables that hold the unmanaged key
            IntPtr keyBlob       = IntPtr.Zero;
            long   generatedSize = 0;

            try
            {
                // create the key
                if (!StrongNameNative.StrongNameKeyGenEx(
                        null, StrongNameKeyGenFlags.None, (int)keySize,
                        out keyBlob, out generatedSize))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                // make sure the key size makes sense
                if (generatedSize <= 0 || generatedSize > int.MaxValue)
                {
                    throw new InvalidOperationException();
                }

                // get the key into managed memory
                byte[] key = new byte[generatedSize];
                Marshal.Copy(keyBlob, key, 0, (int)generatedSize);
                return(key);
            }
            finally
            {
                // release the unmanaged memory the key resides in
                if (keyBlob != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(keyBlob);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Create a public key token from a signed assembly
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="filePath"/> is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <paramref name="filePath"/> is empty
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the token could not be generated
        /// </exception>
        /// <param name="filePath">assembly to generate the token of</param>
        /// <returns>public key token of <paramref name="filePath"/></returns>
        public static byte[] CreateTokenFromAssembly(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullOrEmptyException("filePath");
            }

            // get the key and token
            IntPtr blobBuffer  = IntPtr.Zero;
            IntPtr tokenBuffer = IntPtr.Zero;

            try
            {
                int blobSize  = 0;
                int tokenSize = 0;

                // extract the public key token
                if (!StrongNameNative.StrongNameTokenFromAssemblyEx(
                        filePath, out tokenBuffer, out tokenSize, out blobBuffer, out blobSize))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                // copy the token out of unmanaged memory, and return it
                byte[] token = new byte[tokenSize];
                Marshal.Copy(tokenBuffer, token, 0, tokenSize);
                return(token);
            }
            finally
            {
                if (blobBuffer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(blobBuffer);
                }
                if (tokenBuffer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(tokenBuffer);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Create a public key token from a public key
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="publicKey"/> is null
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the token could not be generated
        /// </exception>
        /// <param name="publicKey">public key to generate the token for</param>
        /// <returns>public key token of <paramref name="publicKey"/></returns>
        public static byte[] CreateTokenFromPublicKey(byte[] publicKey)
        {
            if (publicKey == null || publicKey.Length == 0)
            {
                throw new ArgumentNullOrEmptyException("publicKey");
            }

            IntPtr tokenPointer = IntPtr.Zero;
            int    tokenSize    = 0;

            try
            {
                // generate the token
                if (!StrongNameNative.StrongNameTokenFromPublicKey(
                        publicKey, publicKey.Length, out tokenPointer, out tokenSize))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                // make sure the key size makes sense
                if (tokenSize <= 0 || tokenSize > int.MaxValue)
                {
                    throw new InvalidOperationException();
                }

                // get the key into managed memory
                byte[] token = new byte[tokenSize];
                Marshal.Copy(tokenPointer, token, 0, tokenSize);
                return(token);
            }
            finally
            {
                // release the token memory
                if (tokenPointer != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(tokenPointer);
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Generates a strong name signature for the specified assembly, according to the specified flags.
        /// </summary>
        /// <param name="filePath">The path to the file that contains the manifest of the assembly for which the
        /// strong name signature will be generated.</param>
        /// <param name="keyPair">A pointer to the public/private key pair.</param>
        /// <param name="flags">Flags</param>
        public static byte[] GenerateSignatureFromKeyContainer(string filePath, string keyContainerName, StrongNameGenerationFlags flags)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullOrEmptyException("filePath");
            }

            if (string.IsNullOrEmpty(keyContainerName))
            {
                throw new ArgumentNullOrEmptyException("keyContainerName");
            }

            IntPtr signatureBlobPtr = IntPtr.Zero;

            try
            {
                int signatureBlobSize = 0;

                if (!StrongNameNative.StrongNameSignatureGenerationEx(
                        filePath, keyContainerName, null, 0, out signatureBlobPtr, out signatureBlobSize, flags))
                {
                    Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo());
                }

                byte[] signatureBlob = new byte[signatureBlobSize];
                Marshal.Copy(signatureBlobPtr, signatureBlob, 0, signatureBlobSize);
                return(signatureBlob);
            }
            finally
            {
                if (signatureBlobPtr != IntPtr.Zero)
                {
                    StrongNameNative.StrongNameFreeBuffer(signatureBlobPtr);
                }
            }
        }