コード例 #1
0
        ///////////////////////////////////////////////////////////////////////

        private static bool MatchCertificatePublicKey(
            X509Certificate certificate,
            byte[] publicKey
            )
        {
            //
            // NOTE: Make sure the certificate public key matches what we
            //       expect it to be for our own software updates.
            //
            if (certificate != null)
            {
                byte[] certificatePublicKey = certificate.GetPublicKey();

                if ((certificatePublicKey != null) &&
                    (certificatePublicKey.Length > 0))
                {
                    if ((publicKey != null) && (publicKey.Length > 0))
                    {
                        return(GenericOps <byte> .Equals(
                                   certificatePublicKey, publicKey));
                    }
                }
            }

            return(false);
        }
コード例 #2
0
        ///////////////////////////////////////////////////////////////////////

        public static bool IsDefaultPublicKeyToken(
            byte[] publicKeyToken
            )
        {
            if (publicKeyToken == null)
            {
                return(true);
            }

            byte[] defaultPublicKeyToken = PublicKeyToken.Default;

            if (defaultPublicKeyToken == null)
            {
                return(false);
            }

            if (GenericOps <byte> .Equals(publicKeyToken, defaultPublicKeyToken))
            {
                return(true);
            }

            return(false);
        }
コード例 #3
0
        ///////////////////////////////////////////////////////////////////////

        public bool VerifyFile(
            Configuration configuration,
            string fileName,
            bool strongName
            )
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    Trace(configuration, String.Format(
                              "File \"{0}\" does not exist.", fileName),
                          TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                string error = null;

                if (strongName)
                {
#if NATIVE && WINDOWS
                    if (VersionOps.IsWindowsOperatingSystem() &&
                        !StrongNameEx.IsStrongNameSigned(
                            configuration, fileName, true, ref error))
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" is not signed.",
                                  fileName), TraceCategory);

                        Trace(configuration, String.Format(
                                  "Assembly signature error: {0}", error),
                              TraceCategory);

                        return(false);
                    }
#endif

                    ///////////////////////////////////////////////////////////

                    AssemblyName assemblyName =
                        AssemblyName.GetAssemblyName(fileName);

                    if (assemblyName == null)
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" has no name.", fileName),
                              TraceCategory);

                        return(false);
                    }

                    byte[] filePublicKeyToken = assemblyName.GetPublicKeyToken();

                    if (!GenericOps <byte> .Equals(
                            filePublicKeyToken, publicKeyToken))
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" has incorrect " +
                                  "public key token \"{1}\".", fileName,
                                  FormatOps.ToHexString(filePublicKeyToken)),
                              TraceCategory);

                        return(false);
                    }
                }

                ///////////////////////////////////////////////////////////////

                byte[] hash = null;

                if (FileOps.Hash(
                        configuration, "md5", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, md5Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" MD5 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                if (FileOps.Hash(
                        configuration, "sha1", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, sha1Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" SHA1 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                if (FileOps.Hash(
                        configuration, "sha512", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, sha512Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" SHA512 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                Trace(configuration, e, TraceCategory);
            }

            return(false);
        }
コード例 #4
0
        ///////////////////////////////////////////////////////////////////////

        public static bool IsStrongNameSigned(
            Assembly assembly,
            ref byte[] publicKeyToken,
            ref string error
            )
        {
            if (assembly == null)
            {
                error = "assembly is invalid";
                return(false);
            }

            AssemblyName assemblyName = assembly.GetName();

            if (assemblyName == null)
            {
                error = "assembly has invalid name";
                return(false);
            }

            byte[] publicKey = assemblyName.GetPublicKey();

            if (publicKey == null)
            {
                error = "assembly has invalid public key";
                return(false);
            }

            Evidence evidence = assembly.Evidence;

            if (evidence == null)
            {
                error = "assembly has invalid evidence";
                return(false);
            }

            IEnumerator enumerator = evidence.GetHostEnumerator();

            if (enumerator == null)
            {
                error = "assembly has invalid evidence enumerator";
                return(false);
            }

            while (enumerator.MoveNext())
            {
                StrongName strongName = enumerator.Current as StrongName;

                if (strongName == null)
                {
                    continue;
                }

                StrongNamePublicKeyBlob strongNamePublicKey =
                    strongName.PublicKey;

                if (strongNamePublicKey == null)
                {
                    error = "assembly strong name has invalid public key";
                    return(false);
                }

                if (GenericOps <byte> .Equals(ParseOps.HexString(
                                                  strongNamePublicKey.ToString()), publicKey))
                {
                    publicKeyToken = assemblyName.GetPublicKeyToken();

                    if (publicKeyToken == null)
                    {
                        error = "assembly has invalid public key token";
                        return(false);
                    }

                    return(true);
                }
            }

            error = "assembly is not signed";
            return(false);
        }