Example #1
0
        static bool LoadConfig(bool quiet)
        {
            System.Reflection.MethodInfo config = typeof(System.Environment).GetMethod("GetMachineConfigPath",
                                                                                       System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

            if (config != null)
            {
                string path = (string)config.Invoke(null, null);

                bool exist = File.Exists(path);
                if (!quiet && !exist)
                {
                    Console.WriteLine("Couldn't find machine.config");
                }

                StrongNameManager.LoadConfig(path);
                return(exist);
            }
            else if (!quiet)
            {
                Console.WriteLine("Couldn't resolve machine.config location (corlib issue)");
            }

            // default CSP
            return(false);
        }
Example #2
0
        // modified copy from sn
        private static VerificationResult VerifyStrongName(AssemblyName an, string assemblyFile)
        {
            byte [] publicKey = StrongNameManager.GetMappedPublicKey(an.GetPublicKeyToken());
            if ((publicKey == null) || (publicKey.Length < 12))
            {
                // no mapping
                publicKey = an.GetPublicKey();
                if ((publicKey == null) || (publicKey.Length < 12))
                {
                    return(VerificationResult.WeakNamed);
                }
            }

            // Note: MustVerify is based on the original token (by design). Public key
            // remapping won't affect if the assembly is verified or not.
            if (StrongNameManager.MustVerify(new System.Reflection.AssemblyName(an.FullName)))
            {
                RSA        rsa = CryptoConvert.FromCapiPublicKeyBlob(publicKey, 12);
                StrongName sn  = new StrongName(rsa);
                if (sn.Verify(assemblyFile))
                {
                    return(VerificationResult.StrongNamed);
                }
                else
                {
                    return(VerificationResult.DelaySigned);
                }
            }
            else
            {
                return(VerificationResult.Skipped);
            }
        }
Example #3
0
        static int Verify(string assemblyName, bool forceVerification, bool quiet)
        {
            // this doesn't load the assembly (well it unloads it ;)
            // http://weblogs.asp.net/nunitaddin/posts/9991.aspx
            AssemblyName an = null;

            try {
                an = AssemblyName.GetAssemblyName(assemblyName);
            }
            catch {
            }
            if (an == null)
            {
                Console.WriteLine("Unable to load assembly: {0}", assemblyName);
                return(2);
            }

            byte[] publicKey = StrongNameManager.GetMappedPublicKey(an.GetPublicKeyToken());
            if ((publicKey == null) || (publicKey.Length < 12))
            {
                // no mapping
                publicKey = an.GetPublicKey();
                if ((publicKey == null) || (publicKey.Length < 12))
                {
                    Console.WriteLine("{0} is not a strongly named assembly.", assemblyName);
                    return(2);
                }
            }

            // Note: MustVerify is based on the original token (by design). Public key
            // remapping won't affect if the assembly is verified or not.
            if (forceVerification || StrongNameManager.MustVerify(an))
            {
                RSA        rsa = CryptoConvert.FromCapiPublicKeyBlob(publicKey, 12);
                StrongName sn  = new StrongName(rsa);
                if (sn.Verify(assemblyName))
                {
                    if (!quiet)
                    {
                        Console.WriteLine("Assembly {0} is strongnamed.", assemblyName);
                    }
                    return(0);
                }
                else
                {
                    Console.WriteLine("Assembly {0} is delay-signed but not strongnamed", assemblyName);
                    return(1);
                }
            }
            else
            {
                Console.WriteLine("Assembly {0} is strongnamed (verification skipped).", assemblyName);
                return(0);
            }
        }
Example #4
0
        // We don't want a dependency on StrongNameManager in Mono.Security.dll
        static public bool IsAssemblyStrongnamed(string assemblyName)
        {
            if (!initialized)
            {
                lock (lockObject) {
                    if (!initialized)
                    {
                        string config = Environment.GetMachineConfigPath();
                        StrongNameManager.LoadConfig(config);
                        initialized = true;
                    }
                }
            }

            try {
                // this doesn't load the assembly (well it unloads it ;)
                // http://weblogs.asp.net/nunitaddin/posts/9991.aspx
                AssemblyName an = AssemblyName.GetAssemblyName(assemblyName);
                if (an == null)
                {
                    return(false);
                }

                byte[] publicKey = StrongNameManager.GetMappedPublicKey(an.GetPublicKeyToken());
                if ((publicKey == null) || (publicKey.Length < 12))
                {
                    // no mapping
                    publicKey = an.GetPublicKey();
                    if ((publicKey == null) || (publicKey.Length < 12))
                    {
                        return(false);
                    }
                }

                // Note: MustVerify is based on the original token (by design). Public key
                // remapping won't affect if the assembly is verified or not.
                if (!StrongNameManager.MustVerify(an))
                {
                    return(true);
                }

                RSA        rsa    = CryptoConvert.FromCapiPublicKeyBlob(publicKey, 12);
                StrongName sn     = new StrongName(rsa);
                bool       result = sn.Verify(assemblyName);
                return(result);
            }
            catch {
                // no exception allowed
                return(false);
            }
        }
Example #5
0
        static bool ReSign(string assemblyName, RSA key, bool quiet)
        {
            // this doesn't load the assembly (well it unloads it ;)
            // http://weblogs.asp.net/nunitaddin/posts/9991.aspx
            AssemblyName an = null;

            try {
                an = AssemblyName.GetAssemblyName(assemblyName);
            }
            catch {
            }
            if (an == null)
            {
                Console.WriteLine("Unable to load assembly: {0}", assemblyName);
                return(false);
            }

            StrongName sign = new StrongName(key);

            byte[] token = an.GetPublicKeyToken();

            // first, try to compare using a mapped public key (e.g. ECMA)
            bool same = Compare(sign.PublicKey, StrongNameManager.GetMappedPublicKey(token));

            if (!same)
            {
                // second, try to compare using the assembly public key
                same = Compare(sign.PublicKey, an.GetPublicKey());
                if (!same)
                {
                    // third (and last) chance, try to compare public key token
                    same = Compare(sign.PublicKeyToken, token);
                }
            }

            if (same)
            {
                bool signed = sign.Sign(assemblyName);
                if (!quiet || !signed)
                {
                    Console.WriteLine(signed ? "Assembly {0} signed." : "Couldn't sign the assembly {0}.",
                                      assemblyName);
                }
                return(signed);
            }

            Console.WriteLine("Couldn't sign the assembly {0} with this key pair.", assemblyName);
            return(false);
        }