Verify() static private method

static private Verify ( RSA rsa, AssemblyHashAlgorithm algorithm, byte hash, byte signature ) : bool
rsa RSA
algorithm AssemblyHashAlgorithm
hash byte
signature byte
return bool
        // We don't want a dependency on StrongNameManager in Mono.Security.dll
        static public bool IsAssemblyStrongnamed(string assemblyName)
        {
            if (!initialized)
            {
                lock (lockObject)
                {
                    if (!initialized)
                    {
#if NET_2_1
                        // Moonlight cannot depend on machine.config
#else
                        string config = Environment.GetMachineConfigPath();
                        StrongNameManager.LoadConfig(config);
#endif
                        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);
            }
        }
        public static bool IsAssemblyStrongnamed(string assemblyName)
        {
            if (!StrongName.initialized)
            {
                object obj = StrongName.lockObject;
                lock (obj)
                {
                    if (!StrongName.initialized)
                    {
                        StrongName.initialized = true;
                    }
                }
            }
            bool result;

            try
            {
                AssemblyName assemblyName2 = AssemblyName.GetAssemblyName(assemblyName);
                if (assemblyName2 == null)
                {
                    result = false;
                }
                else
                {
                    byte[] mappedPublicKey = StrongNameManager.GetMappedPublicKey(assemblyName2.GetPublicKeyToken());
                    if (mappedPublicKey == null || mappedPublicKey.Length < 12)
                    {
                        mappedPublicKey = assemblyName2.GetPublicKey();
                        if (mappedPublicKey == null || mappedPublicKey.Length < 12)
                        {
                            return(false);
                        }
                    }
                    if (!StrongNameManager.MustVerify(assemblyName2))
                    {
                        result = true;
                    }
                    else
                    {
                        RSA        rsa        = CryptoConvert.FromCapiPublicKeyBlob(mappedPublicKey, 12);
                        StrongName strongName = new StrongName(rsa);
                        bool       flag       = strongName.Verify(assemblyName);
                        result = flag;
                    }
                }
            }
            catch
            {
                result = false;
            }
            return(result);
        }
        public static bool VerifySignature(byte[] publicKey, int algorithm, byte[] hash, byte[] signature)
        {
            bool result;

            try
            {
                RSA rsa = CryptoConvert.FromCapiPublicKeyBlob(publicKey);
                result = StrongName.Verify(rsa, (AssemblyHashAlgorithm)algorithm, hash, signature);
            }
            catch
            {
                result = false;
            }
            return(result);
        }
        public bool Verify(Stream stream)
        {
            StrongName.StrongNameSignature strongNameSignature = this.StrongHash(stream, StrongName.StrongNameOptions.Signature);
            if (strongNameSignature.Hash == null)
            {
                return(false);
            }
            bool result;

            try
            {
                AssemblyHashAlgorithm algorithm = AssemblyHashAlgorithm.SHA1;
                if (this.tokenAlgorithm == "MD5")
                {
                    algorithm = AssemblyHashAlgorithm.MD5;
                }
                result = StrongName.Verify(this.rsa, algorithm, strongNameSignature.Hash, strongNameSignature.Signature);
            }
            catch (CryptographicException)
            {
                result = false;
            }
            return(result);
        }
Example #5
0
		private static Assembly LoadAndVerifyAssemblyInternal(byte[] assemblyData)
		{
			Assembly assembly = Assembly.Load(assemblyData);
			byte[] publicKey = assembly.GetName().GetPublicKey();
			if (publicKey == null || publicKey.Length == 0)
			{
				return null;
			}
			RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
			rSACryptoServiceProvider.ImportCspBlob(publicKey);
			StrongName strongName = new StrongName(rSACryptoServiceProvider);
			Assembly result;
			using (MemoryStream memoryStream = new MemoryStream(assemblyData))
			{
				if (strongName.Verify(memoryStream))
				{
					Security._verifiedAssemblies.Add(assembly);
					result = assembly;
				}
				else
				{
					result = null;
				}
			}
			return result;
		}
Example #6
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 (an)) {
				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;
			}
		}
		// 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 #8
0
File: sn.cs Project: nobled/mono
		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;
			}
		}