Authenticode.cs (4914B)
1 using System.Runtime.InteropServices; 2 using System.Runtime.InteropServices.ComTypes; 3 using System.Security.Cryptography.X509Certificates; 4 5 namespace DriverUpdater; 6 7 // Checks a downloaded installer's Authenticode signature before the app runs it. WinVerifyTrust 8 // checks that the signature matches the file and chains to a trusted root, with revocation checks. 9 // The name then comes from the certificate of the signer WinVerifyTrust just checked. 10 static class Authenticode 11 { 12 public static bool IsSignedBy(string path, string company) => 13 VerifiedSignerName(path)?.Equals(company, StringComparison.OrdinalIgnoreCase) == true; 14 15 // Returns the signer's name if the signature is valid, or null if it isn't. 16 static string? VerifiedSignerName(string path) 17 { 18 var fileInfo = new WinTrustFileInfo 19 { 20 StructSize = Marshal.SizeOf<WinTrustFileInfo>(), 21 FilePath = path, 22 }; 23 IntPtr fileInfoPointer = Marshal.AllocHGlobal(fileInfo.StructSize); 24 try 25 { 26 Marshal.StructureToPtr(fileInfo, fileInfoPointer, fDeleteOld: false); 27 var data = new WinTrustData 28 { 29 StructSize = Marshal.SizeOf<WinTrustData>(), 30 UIChoice = WtdUiNone, 31 RevocationChecks = WtdRevokeWholeChain, 32 UnionChoice = WtdChoiceFile, 33 FileInfo = fileInfoPointer, 34 StateAction = WtdStateActionVerify, 35 }; 36 var action = GenericVerifyV2; 37 int result = WinVerifyTrust(IntPtr.Zero, ref action, ref data); 38 string? signerName = result == 0 ? SignerName(data.StateData) : null; 39 40 // Free the state WinVerifyTrust kept from the verify call. 41 data.StateAction = WtdStateActionClose; 42 WinVerifyTrust(IntPtr.Zero, ref action, ref data); 43 44 return signerName; 45 } 46 finally 47 { 48 Marshal.DestroyStructure<WinTrustFileInfo>(fileInfoPointer); 49 Marshal.FreeHGlobal(fileInfoPointer); 50 } 51 } 52 53 // Walks from WinVerifyTrust's state to the signer, then to the first certificate in the signer's 54 // chain, which is the signer's own. 55 static string SignerName(IntPtr stateData) 56 { 57 IntPtr providerData = WTHelperProvDataFromStateData(stateData); 58 IntPtr signerPointer = WTHelperGetProvSignerFromChain(providerData, 0, false, 0); 59 var signer = Marshal.PtrToStructure<CryptProviderSigner>(signerPointer); 60 var signerCertificate = Marshal.PtrToStructure<CryptProviderCert>(signer.CertChain); 61 62 using var certificate = new X509Certificate2(signerCertificate.CertContext); 63 return certificate.GetNameInfo(X509NameType.SimpleName, forIssuer: false); 64 } 65 66 // WINTRUST_ACTION_GENERIC_VERIFY_V2: the standard Authenticode policy. 67 static readonly Guid GenericVerifyV2 = new("00AAC56B-CD44-11d0-8CC2-00C04FC295EE"); 68 69 const int WtdUiNone = 2; 70 const int WtdRevokeWholeChain = 1; 71 const int WtdChoiceFile = 1; 72 const int WtdStateActionVerify = 1; 73 const int WtdStateActionClose = 2; 74 75 [DllImport("wintrust.dll", ExactSpelling = true)] 76 static extern int WinVerifyTrust(IntPtr window, ref Guid action, ref WinTrustData data); 77 78 [DllImport("wintrust.dll", ExactSpelling = true)] 79 static extern IntPtr WTHelperProvDataFromStateData(IntPtr stateData); 80 81 [DllImport("wintrust.dll", ExactSpelling = true)] 82 static extern IntPtr WTHelperGetProvSignerFromChain( 83 IntPtr providerData, int signerIndex, bool counterSigner, int counterSignerIndex); 84 85 // WINTRUST_FILE_INFO 86 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 87 struct WinTrustFileInfo 88 { 89 public int StructSize; 90 public string FilePath; 91 public IntPtr FileHandle; 92 public IntPtr KnownSubject; 93 } 94 95 // WINTRUST_DATA 96 [StructLayout(LayoutKind.Sequential)] 97 struct WinTrustData 98 { 99 public int StructSize; 100 public IntPtr PolicyCallbackData; 101 public IntPtr SipClientData; 102 public int UIChoice; 103 public int RevocationChecks; 104 public int UnionChoice; 105 public IntPtr FileInfo; 106 public int StateAction; 107 public IntPtr StateData; 108 public IntPtr UrlReference; 109 public int ProvFlags; 110 public int UIContext; 111 public IntPtr SignatureSettings; 112 } 113 114 // The leading fields of CRYPT_PROVIDER_SGNR. The rest aren't needed. 115 [StructLayout(LayoutKind.Sequential)] 116 struct CryptProviderSigner 117 { 118 public int StructSize; 119 public FILETIME VerifyAsOf; 120 public int CertChainCount; 121 public IntPtr CertChain; 122 } 123 124 // The leading fields of CRYPT_PROVIDER_CERT. 125 [StructLayout(LayoutKind.Sequential)] 126 struct CryptProviderCert 127 { 128 public int StructSize; 129 public IntPtr CertContext; 130 } 131 }