Beispiel #1
0
        /// <summary>
        /// Gets a value indicating whether the assembly manifest at the supplied
        /// path contains a strong name signature.
        /// </summary>
        /// <param name="assemblyPath">The path to the portable executable (.exe or
        /// .dll) file for the assembly to be verified.</param>
        /// <returns>True if the verification was successful; otherwise, false.</returns>
        /// <remarks>VerifyStrongName is a utility function to check the validity
        /// of an assembly, taking into account registry settings.</remarks>
        public static bool VerifyStrongName(string assemblyPath)
        {
            ICLRStrongName strongName = ClrMetaHost.CurrentRuntime.GetInterface <ICLRStrongName>(
                new Guid(0xB79B0ACD, 0xF5CD, 0x409b, 0xB5, 0xA5, 0xA1, 0x62, 0x44, 0x61, 0x0B, 0x92));

            return(strongName.StrongNameSignatureVerificationEx(assemblyPath, 1) != 0);
        }
Beispiel #2
0
        /*  leave this out for now. We'll make the command line compilers have this behavior, but
         * not the compiler library. This differs from previous versions because specifying a keyfile
         * and container through either the command line or attributes gave this "install the key" behavior.
         *
         *
         * //EDMAURER alink had weird, MSDN spec'd behavior. from MSDN "In case both /keyfile and /keycontainer are
         * //specified (either by command-line option or by custom attribute) in the same compilation, the compiler
         * //first tries the key container. If that succeeds, then the assembly is signed with the information in the
         * //key container. If the compiler does not find the key container, it tries the file specified with /keyfile.
         * //If this succeeds, the assembly is signed with the information in the key file, and the key information is
         * //installed in the key container (similar to sn -i) so that on the next compilation, the key container will
         * //be valid.
         *
         * private static ImmutableArray<byte> GetPublicKeyAndPossiblyInstall(string keyFilename, string keyContainer)
         * {
         *  if (keyContainer != null)
         *  {
         *      ImmutableArray<byte> result = GetPublicKey(keyContainer);
         *
         *      if (result.IsNotNull)
         *          return result;
         *  }
         *
         *  if (keyFilename != null)
         *  {
         *      byte[] keyFileContents = System.IO.File.ReadAllBytes(keyFilename);
         *
         *      if (keyContainer != null)
         *          InstallKey(keyFileContents, keyFilename);
         *
         *      return GetPublicKey(keyFileContents);
         *  }
         *
         *  return default(ImmutableArray<byte>);
         * }
         */

        /// <exception cref="IOException"/>
        private static void Sign(string filePath, string keyName)
        {
            try
            {
                ICLRStrongName strongName = GetStrongNameInterface();

                int unused;
                strongName.StrongNameSignatureGeneration(filePath, keyName, IntPtr.Zero, 0, null, out unused);
            }
            catch (COMException ex)
            {
                throw new IOException(ex.Message, ex);
            }
        }
Beispiel #3
0
        // internal for testing
        internal static ImmutableArray <byte> GetPublicKey(string keyContainer)
        {
            ICLRStrongName strongName = GetStrongNameInterface();

            IntPtr keyBlob;
            int    keyBlobByteCount;

            strongName.StrongNameGetPublicKey(keyContainer, default(IntPtr), 0, out keyBlob, out keyBlobByteCount);

            byte[] pubKey = new byte[keyBlobByteCount];
            Marshal.Copy(keyBlob, pubKey, 0, keyBlobByteCount);
            strongName.StrongNameFreeBuffer(keyBlob);

            return(pubKey.AsImmutableOrNull());
        }
Beispiel #4
0
        /// <exception cref="IOException"/>
        private static void Sign(string filePath, ImmutableArray <byte> keyPair)
        {
            try
            {
                ICLRStrongName strongName = GetStrongNameInterface();

                using (var pinned = PinnedImmutableArray.Create(keyPair))
                {
                    int unused;
                    strongName.StrongNameSignatureGeneration(filePath, null, pinned.Pointer, keyPair.Length, null, out unused);
                }
            }
            catch (COMException ex)
            {
                throw new IOException(ex.Message, ex);
            }
        }
Beispiel #5
0
        // internal for testing
        /// <exception cref="IOException"/>
        internal static ImmutableArray <byte> GetPublicKey(byte[] keyFileContents)
        {
            try
            {
                var lastSeen = lastSeenKeyPair;
                if (lastSeen != null && ByteSequenceComparer.ValueEquals(lastSeen.Item1, keyFileContents))
                {
                    return(lastSeen.Item2);
                }

                ICLRStrongName strongName = GetStrongNameInterface();

                IntPtr keyBlob;
                int    keyBlobByteCount;

                //EDMAURER use marshal to be safe?
                unsafe
                {
                    fixed(byte *p = keyFileContents)
                    {
                        try
                        {
                            strongName.StrongNameGetPublicKey(null, (IntPtr)p, keyFileContents.Length, out keyBlob, out keyBlobByteCount);
                        }
                        catch (ArgumentException ex)
                        {
                            throw new IOException(ex.Message);
                        }
                    }
                }

                byte[] pubKey = new byte[keyBlobByteCount];
                Marshal.Copy(keyBlob, pubKey, 0, keyBlobByteCount);
                strongName.StrongNameFreeBuffer(keyBlob);

                var result = pubKey.AsImmutableOrNull();
                lastSeenKeyPair = Tuple.Create(keyFileContents, result);

                return(result);
            }
            catch (COMException ex)
            {
                throw new IOException(ex.Message);
            }
        }
        /// <exception cref="IOException"/>
        private static unsafe void Sign(string filePath, ImmutableArray <byte> keyPair)
        {
            try
            {
                ICLRStrongName strongName = GetStrongNameInterface();

                fixed(byte *pinned = keyPair.ToArray())
                {
                    int unused;

                    strongName.StrongNameSignatureGeneration(filePath, null, (IntPtr)pinned, keyPair.Length, null, out unused);
                }
            }
            catch (COMException ex)
            {
                throw new IOException(ex.Message, ex);
            }
        }
Beispiel #7
0
        private unsafe static void InstallKey(byte[] keyBlob, string keyName)
        {
            try
            {
                ICLRStrongName strongName = DesktopStrongNameProvider.GetStrongNameInterface();

                //EDMAURER use marshal to be safe?
                fixed(byte *p = keyBlob)
                {
                    strongName.StrongNameKeyInstall(keyName, (IntPtr)p, keyBlob.Length);
                }
            }
            catch (COMException ex)
            {
                if (unchecked ((uint)ex.ErrorCode) != 0x8009000F)
                {
                    throw;
                }
            }
        }