|
| 1 | +using AuthenticodeLint.Interop; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Security.Cryptography; |
| 7 | +using System.Security.Cryptography.X509Certificates; |
| 8 | + |
| 9 | +namespace AuthenticodeLint.Rules |
| 10 | +{ |
| 11 | + public class NoUnknownCertificatesRule : IAuthenticodeRule |
| 12 | + { |
| 13 | + public int RuleId { get; } = 10010; |
| 14 | + |
| 15 | + public string RuleName { get; } = "No Unknown Certificates"; |
| 16 | + |
| 17 | + public string ShortDescription { get; } = "Checks for unknown embedded certificates."; |
| 18 | + |
| 19 | + public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file) |
| 20 | + { |
| 21 | + var result = RuleResult.Pass; |
| 22 | + var signatures = graph.VisitAll(); |
| 23 | + foreach(var signature in signatures) |
| 24 | + { |
| 25 | + var allEmbeddedCertificates = signature.AdditionalCertificates.Cast<X509Certificate2>().ToList(); |
| 26 | + var certificatesRequiringEliminiation = new HashSet<X509Certificate2>(allEmbeddedCertificates, new CertificateThumbprintComparer()); |
| 27 | + foreach(var certificate in allEmbeddedCertificates) |
| 28 | + { |
| 29 | + if (!certificatesRequiringEliminiation.Contains(certificate)) |
| 30 | + { |
| 31 | + //This certificate was already eliminated because it was part of a previous chain. |
| 32 | + continue; |
| 33 | + } |
| 34 | + using (var chain = new X509Chain()) |
| 35 | + { |
| 36 | + chain.ChainPolicy.ExtraStore.AddRange(signature.AdditionalCertificates); |
| 37 | + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; |
| 38 | + chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain; |
| 39 | + //All we care is that we can even find an authority. |
| 40 | + chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags & ~X509VerificationFlags.AllowUnknownCertificateAuthority; |
| 41 | + if(chain.Build(certificate)) |
| 42 | + { |
| 43 | + certificatesRequiringEliminiation.ExceptWith(chain.ChainElements.Cast<X509ChainElement>().Select(c => c.Certificate)); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + if (certificatesRequiringEliminiation.Count > 0) |
| 48 | + { |
| 49 | + foreach(var certificate in certificatesRequiringEliminiation) |
| 50 | + { |
| 51 | + verboseWriter.LogSignatureMessage(signature.SignerInfo, $"Signature contained untrusted certificate \"{certificate.Subject}\" ({certificate.Thumbprint})."); |
| 52 | + } |
| 53 | + result = RuleResult.Fail; |
| 54 | + } |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + private class CertificateThumbprintComparer : IEqualityComparer<X509Certificate2> |
| 62 | + { |
| 63 | + public bool Equals(X509Certificate2 x, X509Certificate2 y) |
| 64 | + { |
| 65 | + return x.Thumbprint == y.Thumbprint; |
| 66 | + } |
| 67 | + |
| 68 | + public int GetHashCode(X509Certificate2 obj) |
| 69 | + { |
| 70 | + return obj.Thumbprint.GetHashCode(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments