// 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); } }
// 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); } }
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); } }
// TODO // we would get better performance if the runtime hashed the // assembly - as we wouldn't have to load it from disk a // second time. The runtime already have implementations of // SHA1 (and even MD5 if required someday). static public bool VerifySignature(byte[] publicKey, int algorithm, byte[] hash, byte[] signature) { try { RSA rsa = CryptoConvert.FromCapiPublicKeyBlob(publicKey); return(Verify(rsa, (AssemblyHashAlgorithm)algorithm, hash, signature)); } catch { // no exception allowed return(false); } }
public static int Verify(string assemblyName, bool forceVerification = true) { // 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) { MessageBox.Show($"Unable to load assembly: {assemblyName}"); return(2); } var publicKey = StrongNameManager.GetMappedPublicKey(an.GetPublicKeyToken()); if ((publicKey == null) || (publicKey.Length < 12)) { // no mapping publicKey = an.GetPublicKey(); if ((publicKey == null) || (publicKey.Length < 12)) { 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)) { var rsa = CryptoConvert.FromCapiPublicKeyBlob(publicKey, 12); var sn = new StrongName(rsa); if (sn.Verify(assemblyName)) { return(0); } else { MessageBox.Show($"Assembly {assemblyName} is delay-signed but not strongnamed."); return(1); } } else { MessageBox.Show($"Assembly {assemblyName} is strongnamed (verification skipped)."); return(0); } }
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 void FromCapiPublicKeyBlob_Invalid() { RSA rsa = CryptoConvert.FromCapiPublicKeyBlob(strongName, 0); }
public void FromCapiPublicKeyBlob_InvalidOffset() { RSA rsa = CryptoConvert.FromCapiPublicKeyBlob(new byte [0], 0); }
public void FromCapiPublicKeyBlob_Null() { RSA rsa = CryptoConvert.FromCapiPublicKeyBlob(null); }
public void FromCapiPublicKeyBlob() { RSA rsa = CryptoConvert.FromCapiPublicKeyBlob(strongNamePublicKey, 12); Assert.AreEqual(strongNamePublicKeyString, rsa.ToXmlString(false), "PublicKey"); }