Exemple #1
0
 public void Sign(string fileName)
 {
     if (!Mscoree.StrongNameSignatureGeneration(fileName, _keyContainer, IntPtr.Zero, 0, 0, 0))
     {
         throw new ClrPlusException(String.Format("Unable Strong name assembly '{0}'.", fileName));
     }
 }
Exemple #2
0
 public void Dispose()
 {
     if (_keyContainer != null)
     {
         Mscoree.StrongNameKeyDelete(_keyContainer);
         _keyContainer = null;
     }
 }
Exemple #3
0
        public StrongNameCertificateWrapper(X509Certificate2 certificate)
        {
            _keyContainer = Guid.NewGuid().ToString();
            var privateKey = (certificate.PrivateKey as RSACryptoServiceProvider).ExportCspBlob(true);

            if (!Mscoree.StrongNameKeyInstall(_keyContainer, privateKey, privateKey.Length))
            {
                throw new ClrPlusException("Unable to create KeyContainer");
            }
        }
Exemple #4
0
        /// <summary>
        /// Generate a key.  Sizes have to be multiples of 1024
        /// </summary>
        /// <param name="keysize">size, in bits, of the key to generate</param>
        /// <returns>true if the operation succeeded, false otherwise</returns>
        public static byte[] GenerateKey(uint keysize)
        {
            var  keyBlob       = IntPtr.Zero;
            long generatedSize = 0;

            var createdKey = Mscoree.StrongNameKeyGenEx(
                null,
                Mscoree.StrongNameKeyGenFlags.None,
                (int)keysize,
                out keyBlob,
                out generatedSize);

            if (!createdKey || keyBlob == IntPtr.Zero)
            {
                var error = Marshal.GetExceptionForHR(Mscoree.StrongNameErrorInfo());
                throw error;
            }

            try
            {
                if (generatedSize <= 0 || generatedSize > int.MaxValue)
                {
                    throw new Exception("Error while generating key");
                }

                var key = new byte[generatedSize];
                Marshal.Copy(keyBlob, key, 0, (int)generatedSize);

                return(key);
            }
            finally
            {
                if (keyBlob != IntPtr.Zero)
                {
                    Mscoree.StrongNameFreeBuffer(keyBlob);
                }
            }
        }
Exemple #5
0
        /// <summary>
        ///     This puts the strong name into the actual file on disk. The file MUST be delay signed by this point.
        /// </summary>
        /// <param name="filename"> </param>
        /// <param name="certificate"> </param>
        public static void ApplyStrongName(string filename, CertificateReference certificate)
        {
            filename = filename.GetFullPath();
            filename.TryHardToMakeFileWriteable();

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("Can't find file", filename);
            }

            // strong name the binary using the private key from the certificate.
            var wszKeyContainer = Guid.NewGuid().ToString();
            var privateKey      = (certificate.Certificate.PrivateKey as RSACryptoServiceProvider).ExportCspBlob(true);

            if (!Mscoree.StrongNameKeyInstall(wszKeyContainer, privateKey, privateKey.Length))
            {
                throw new ClrPlusException("Unable to create KeyContainer");
            }
            if (!Mscoree.StrongNameSignatureGeneration(filename, wszKeyContainer, IntPtr.Zero, 0, 0, 0))
            {
                throw new ClrPlusException("Unable Strong name assembly '{0}'.".format(filename));
            }
            Mscoree.StrongNameKeyDelete(wszKeyContainer);
        }