2 // AuthenticodeSignature.cs: Authenticode signature validator and generator
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
12 using System.Security.Cryptography;
15 using Mono.Security.X509;
17 namespace Mono.Security.Authenticode {
20 // a. http://www.cs.auckland.ac.nz/~pgut001/pubs/authenticode.txt
27 class AuthenticodeDeformatter : AuthenticodeBase {
29 private string filename;
31 private X509CertificateCollection coll;
32 private ASN1 signedHash;
33 private DateTime timestamp;
34 private X509Certificate signingCertificate;
37 private X509Chain signerChain;
38 private X509Chain timestampChain;
40 public AuthenticodeDeformatter () : base ()
43 signerChain = new X509Chain ();
44 timestampChain = new X509Chain ();
47 public AuthenticodeDeformatter (string fileName) : this ()
49 CheckSignature (fileName);
52 public string FileName {
53 get { return filename; }
54 set { CheckSignature (value); }
59 if (signedHash == null)
61 return signedHash.Value;
73 public bool IsTrusted ()
75 if (rawData == null) {
80 if (signingCertificate == null) {
85 if (signerChain.Root == null) {
90 if (timestamp != DateTime.MinValue) {
91 if (timestampChain.Root == null) {
96 // check that file was timestamped when certificates were valid
97 if (!signingCertificate.WasCurrent (Timestamp)) {
102 else if (!signingCertificate.IsCurrent) {
103 // signature only valid if the certificate is valid
112 public byte[] Signature {
113 get { return rawData; }
116 public DateTime Timestamp {
117 get { return timestamp; }
120 public X509CertificateCollection Certificates {
124 private bool CheckSignature (string fileName)
128 // by default we try with MD5
129 string hashName = "MD5";
130 // compare the signature's hash with the hash of the file
131 hash = HashFile (filename, hashName);
133 // is a signature present ?
137 PKCS7.ContentInfo ci = new PKCS7.ContentInfo (rawData);
138 if (ci.ContentType != PKCS7.signedData)
141 PKCS7.SignedData sd = new PKCS7.SignedData (ci.Content);
142 if (sd.ContentInfo.ContentType != spcIndirectDataContext)
145 coll = sd.Certificates;
147 ASN1 spc = sd.ContentInfo.Content;
148 signedHash = spc [0][1][1];
149 if (signedHash.Length == 20) {
150 // seems to be SHA-1, restart hashing
152 hash = HashFile (filename, hashName);
155 if (!signedHash.CompareValue (hash))
158 // messageDigest is a hash of spcIndirectDataContext (which includes the file hash)
159 byte[] spcIDC = spc [0].Value;
160 HashAlgorithm ha = HashAlgorithm.Create (hashName);
161 byte[] messageDigest = ha.ComputeHash (spcIDC);
163 return VerifySignature (sd, messageDigest, hashName);
166 private bool CompareIssuerSerial (string issuer, byte[] serial, X509Certificate x509)
168 if (issuer != x509.IssuerName)
170 if (serial.Length != x509.SerialNumber.Length)
172 // MS shows the serial number inversed (so is Mono.Security.X509.X509Certificate)
173 int n = serial.Length;
174 for (int i=0; i < serial.Length; i++) {
175 if (serial [i] != x509.SerialNumber [--n])
182 //private bool VerifySignature (ASN1 cs, byte[] calculatedMessageDigest, string hashName)
183 private bool VerifySignature (PKCS7.SignedData sd, byte[] calculatedMessageDigest, string hashName)
185 string contentType = null;
186 ASN1 messageDigest = null;
187 string spcStatementType = null;
188 string spcSpOpusInfo = null;
190 for (int i=0; i < sd.SignerInfo.AuthenticatedAttributes.Count; i++) {
191 ASN1 attr = (ASN1) sd.SignerInfo.AuthenticatedAttributes [i];
192 string oid = ASN1Convert.ToOID (attr[0]);
194 case "1.2.840.113549.1.9.3":
196 contentType = ASN1Convert.ToOID (attr[1][0]);
198 case "1.2.840.113549.1.9.4":
200 messageDigest = attr[1][0];
202 case "1.3.6.1.4.1.311.2.1.11":
203 // spcStatementType (Microsoft code signing)
205 // - individualCodeSigning (1 3 6 1 4 1 311 2 1 21)
206 // - commercialCodeSigning (1 3 6 1 4 1 311 2 1 22)
207 spcStatementType = ASN1Convert.ToOID (attr[1][0][0]);
209 case "1.3.6.1.4.1.311.2.1.12":
210 // spcSpOpusInfo (Microsoft code signing)
212 spcSpOpusInfo = System.Text.Encoding.UTF8.GetString (attr[1][0][1][0].Value);
215 spcSpOpusInfo = null;
222 if (contentType != spcIndirectDataContext)
225 // verify message digest
226 if (messageDigest == null)
228 if (!messageDigest.CompareValue (calculatedMessageDigest))
232 string hashOID = CryptoConfig.MapNameToOID (hashName);
234 // change to SET OF (not [0]) as per PKCS #7 1.5
235 ASN1 aa = new ASN1 (0x31);
236 foreach (ASN1 a in sd.SignerInfo.AuthenticatedAttributes)
238 HashAlgorithm ha = HashAlgorithm.Create (hashName);
239 byte[] p7hash = ha.ComputeHash (aa.GetBytes ());
241 byte[] signature = sd.SignerInfo.Signature;
242 // we need to find the specified certificate
243 string issuer = sd.SignerInfo.IssuerName;
244 byte[] serial = sd.SignerInfo.SerialNumber;
245 foreach (X509Certificate x509 in coll) {
246 if (CompareIssuerSerial (issuer, serial, x509)) {
247 // don't verify is key size don't match
248 if (x509.PublicKey.Length > (signature.Length >> 3)) {
249 RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) x509.RSA;
250 if (rsa.VerifyHash (p7hash, hashOID, signature)) {
251 signerChain.LoadCertificates (coll);
252 if (signerChain.GetChain (x509) != null)
253 signingCertificate = x509;
261 for (int i=0; i < sd.SignerInfo.UnauthenticatedAttributes.Count; i++) {
262 ASN1 attr = (ASN1) sd.SignerInfo.UnauthenticatedAttributes [i];
263 string oid = ASN1Convert.ToOID (attr [0]);
265 case PKCS7.countersignature:
268 // countersignature (1 2 840 113549 1 9 6)
270 PKCS7.SignerInfo cs = new PKCS7.SignerInfo (attr [1]);
271 return VerifyCounterSignature (cs, signature, hashName);
273 // we don't support other unauthenticated attributes
281 //private bool VerifyCounterSignature (ASN1 cs, byte[] signature, string hashName)
282 private bool VerifyCounterSignature (PKCS7.SignerInfo cs, byte[] signature, string hashName)
291 string contentType = null;
292 ASN1 messageDigest = null;
293 for (int i=0; i < cs.AuthenticatedAttributes.Count; i++) {
296 ASN1 attr = (ASN1) cs.AuthenticatedAttributes [i];
297 string oid = ASN1Convert.ToOID (attr[0]);
299 case "1.2.840.113549.1.9.3":
301 contentType = ASN1Convert.ToOID (attr[1][0]);
303 case "1.2.840.113549.1.9.4":
305 messageDigest = attr[1][0];
307 case "1.2.840.113549.1.9.5":
310 // signingTime (1 2 840 113549 1 9 5)
312 // UTCTime '030124013651Z'
315 timestamp = ASN1Convert.ToDateTime (attr[1][0]);
322 if (contentType != PKCS7.data)
325 // verify message digest
326 if (messageDigest == null)
328 // TODO: must be read from the ASN.1 structure
329 switch (messageDigest.Length) {
337 HashAlgorithm ha = HashAlgorithm.Create (hashName);
338 if (!messageDigest.CompareValue (ha.ComputeHash (signature)))
342 byte[] counterSignature = cs.Signature;
343 string hashOID = CryptoConfig.MapNameToOID (hashName);
345 // change to SET OF (not [0]) as per PKCS #7 1.5
346 ASN1 aa = new ASN1 (0x31);
347 foreach (ASN1 a in cs.AuthenticatedAttributes)
349 byte[] p7hash = ha.ComputeHash (aa.GetBytes ());
351 // we need to try all certificates
352 string issuer = cs.IssuerName;
353 byte[] serial = cs.SerialNumber;
354 foreach (X509Certificate x509 in coll) {
355 if (CompareIssuerSerial (issuer, serial, x509)) {
356 // don't verify is key size don't match
357 if (x509.PublicKey.Length > (counterSignature.Length >> 3)) {
358 RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) x509.RSA;
359 if (rsa.VerifyHash (p7hash, hashOID, counterSignature)) {
360 timestampChain.LoadCertificates (coll);
361 return (timestampChain.GetChain (x509) != null);
366 // no certificate can verify this signature!