2 // AuthenticodeDeformatter.cs: Authenticode signature validator
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Runtime
.InteropServices
;
33 using System
.Security
;
34 using System
.Security
.Cryptography
;
36 using Mono
.Security
.Cryptography
;
37 using Mono
.Security
.X509
;
39 namespace Mono
.Security
.Authenticode
{
42 // a. http://www.cs.auckland.ac.nz/~pgut001/pubs/authenticode.txt
49 class AuthenticodeDeformatter
: AuthenticodeBase
{
51 private string filename
;
53 private X509CertificateCollection coll
;
54 private ASN1 signedHash
;
55 private DateTime timestamp
;
56 private X509Certificate signingCertificate
;
58 private bool trustedRoot
;
59 private bool trustedTimestampRoot
;
62 private X509Chain signerChain
;
63 private X509Chain timestampChain
;
65 public AuthenticodeDeformatter () : base ()
68 signerChain
= new X509Chain ();
69 timestampChain
= new X509Chain ();
72 public AuthenticodeDeformatter (string fileName
) : this ()
77 public string FileName
{
78 get { return filename; }
82 CheckSignature (value);
84 catch (SecurityException
) {
95 if (signedHash
== null)
97 return (byte[]) signedHash
.Value
.Clone ();
109 public bool IsTrusted ()
116 if (signingCertificate
== null) {
121 if ((signerChain
.Root
== null) || !trustedRoot
) {
126 if (timestamp
!= DateTime
.MinValue
) {
127 if ((timestampChain
.Root
== null) || !trustedTimestampRoot
) {
132 // check that file was timestamped when certificates were valid
133 if (!signingCertificate
.WasCurrent (Timestamp
)) {
138 else if (!signingCertificate
.IsCurrent
) {
139 // signature only valid if the certificate is valid
149 public byte[] Signature
{
153 return (byte[]) entry
.Clone ();
157 public DateTime Timestamp
{
158 get { return timestamp; }
161 public X509CertificateCollection Certificates
{
165 public X509Certificate SigningCertificate
{
166 get { return signingCertificate; }
169 private bool CheckSignature (string fileName
)
173 entry
= GetSecurityEntry ();
175 // no signature is present
181 PKCS7
.ContentInfo ci
= new PKCS7
.ContentInfo (entry
);
182 if (ci
.ContentType
!= PKCS7
.Oid
.signedData
) {
187 PKCS7
.SignedData sd
= new PKCS7
.SignedData (ci
.Content
);
188 if (sd
.ContentInfo
.ContentType
!= spcIndirectDataContext
) {
193 coll
= sd
.Certificates
;
195 ASN1 spc
= sd
.ContentInfo
.Content
;
196 signedHash
= spc
[0][1][1];
198 HashAlgorithm ha
= null;
199 switch (signedHash
.Length
) {
201 ha
= HashAlgorithm
.Create ("MD5");
205 ha
= HashAlgorithm
.Create ("SHA1");
215 if (!signedHash
.CompareValue (hash
)) {
219 // messageDigest is a hash of spcIndirectDataContext (which includes the file hash)
220 byte[] spcIDC
= spc
[0].Value
;
221 ha
.Initialize (); // re-using hash instance
222 byte[] messageDigest
= ha
.ComputeHash (spcIDC
);
224 bool sign
= VerifySignature (sd
, messageDigest
, ha
);
225 return (sign
&& (reason
== 0));
228 private bool CompareIssuerSerial (string issuer
, byte[] serial
, X509Certificate x509
)
230 if (issuer
!= x509
.IssuerName
)
232 if (serial
.Length
!= x509
.SerialNumber
.Length
)
234 // MS shows the serial number inversed (so is Mono.Security.X509.X509Certificate)
235 int n
= serial
.Length
;
236 for (int i
=0; i
< serial
.Length
; i
++) {
237 if (serial
[i
] != x509
.SerialNumber
[--n
])
244 //private bool VerifySignature (ASN1 cs, byte[] calculatedMessageDigest, string hashName)
245 private bool VerifySignature (PKCS7
.SignedData sd
, byte[] calculatedMessageDigest
, HashAlgorithm ha
)
247 string contentType
= null;
248 ASN1 messageDigest
= null;
249 // string spcStatementType = null;
250 // string spcSpOpusInfo = null;
252 for (int i
=0; i
< sd
.SignerInfo
.AuthenticatedAttributes
.Count
; i
++) {
253 ASN1 attr
= (ASN1
) sd
.SignerInfo
.AuthenticatedAttributes
[i
];
254 string oid
= ASN1Convert
.ToOid (attr
[0]);
256 case "1.2.840.113549.1.9.3":
258 contentType
= ASN1Convert
.ToOid (attr
[1][0]);
260 case "1.2.840.113549.1.9.4":
262 messageDigest
= attr
[1][0];
264 case "1.3.6.1.4.1.311.2.1.11":
265 // spcStatementType (Microsoft code signing)
267 // - individualCodeSigning (1 3 6 1 4 1 311 2 1 21)
268 // - commercialCodeSigning (1 3 6 1 4 1 311 2 1 22)
269 // spcStatementType = ASN1Convert.ToOid (attr[1][0][0]);
271 case "1.3.6.1.4.1.311.2.1.12":
272 // spcSpOpusInfo (Microsoft code signing)
274 spcSpOpusInfo = System.Text.Encoding.UTF8.GetString (attr[1][0][0][0].Value);
276 catch (NullReferenceException) {
277 spcSpOpusInfo = null;
284 if (contentType
!= spcIndirectDataContext
)
287 // verify message digest
288 if (messageDigest
== null)
290 if (!messageDigest
.CompareValue (calculatedMessageDigest
))
294 string hashOID
= CryptoConfig
.MapNameToOID (ha
.ToString ());
296 // change to SET OF (not [0]) as per PKCS #7 1.5
297 ASN1 aa
= new ASN1 (0x31);
298 foreach (ASN1 a
in sd
.SignerInfo
.AuthenticatedAttributes
)
301 byte[] p7hash
= ha
.ComputeHash (aa
.GetBytes ());
303 byte[] signature
= sd
.SignerInfo
.Signature
;
304 // we need to find the specified certificate
305 string issuer
= sd
.SignerInfo
.IssuerName
;
306 byte[] serial
= sd
.SignerInfo
.SerialNumber
;
307 foreach (X509Certificate x509
in coll
) {
308 if (CompareIssuerSerial (issuer
, serial
, x509
)) {
309 // don't verify is key size don't match
310 if (x509
.PublicKey
.Length
> (signature
.Length
>> 3)) {
311 // return the signing certificate even if the signature isn't correct
312 // (required behaviour for 2.0 support)
313 signingCertificate
= x509
;
314 RSACryptoServiceProvider rsa
= (RSACryptoServiceProvider
) x509
.RSA
;
315 if (rsa
.VerifyHash (p7hash
, hashOID
, signature
)) {
316 signerChain
.LoadCertificates (coll
);
317 trustedRoot
= signerChain
.Build (x509
);
324 // timestamp signature is optional
325 if (sd
.SignerInfo
.UnauthenticatedAttributes
.Count
== 0) {
326 trustedTimestampRoot
= true;
328 for (int i
= 0; i
< sd
.SignerInfo
.UnauthenticatedAttributes
.Count
; i
++) {
329 ASN1 attr
= (ASN1
) sd
.SignerInfo
.UnauthenticatedAttributes
[i
];
330 string oid
= ASN1Convert
.ToOid (attr
[0]);
332 case PKCS7
.Oid
.countersignature
:
335 // countersignature (1 2 840 113549 1 9 6)
337 PKCS7
.SignerInfo cs
= new PKCS7
.SignerInfo (attr
[1]);
338 trustedTimestampRoot
= VerifyCounterSignature (cs
, signature
);
341 // we don't support other unauthenticated attributes
347 return (trustedRoot
&& trustedTimestampRoot
);
350 private bool VerifyCounterSignature (PKCS7
.SignerInfo cs
, byte[] signature
)
359 string contentType
= null;
360 ASN1 messageDigest
= null;
361 for (int i
=0; i
< cs
.AuthenticatedAttributes
.Count
; i
++) {
364 ASN1 attr
= (ASN1
) cs
.AuthenticatedAttributes
[i
];
365 string oid
= ASN1Convert
.ToOid (attr
[0]);
367 case "1.2.840.113549.1.9.3":
369 contentType
= ASN1Convert
.ToOid (attr
[1][0]);
371 case "1.2.840.113549.1.9.4":
373 messageDigest
= attr
[1][0];
375 case "1.2.840.113549.1.9.5":
378 // signingTime (1 2 840 113549 1 9 5)
380 // UTCTime '030124013651Z'
383 timestamp
= ASN1Convert
.ToDateTime (attr
[1][0]);
390 if (contentType
!= PKCS7
.Oid
.data
)
393 // verify message digest
394 if (messageDigest
== null)
396 // TODO: must be read from the ASN.1 structure
397 string hashName
= null;
398 switch (messageDigest
.Length
) {
406 HashAlgorithm ha
= HashAlgorithm
.Create (hashName
);
407 if (!messageDigest
.CompareValue (ha
.ComputeHash (signature
)))
411 byte[] counterSignature
= cs
.Signature
;
413 // change to SET OF (not [0]) as per PKCS #7 1.5
414 ASN1 aa
= new ASN1 (0x31);
415 foreach (ASN1 a
in cs
.AuthenticatedAttributes
)
417 byte[] p7hash
= ha
.ComputeHash (aa
.GetBytes ());
419 // we need to try all certificates
420 string issuer
= cs
.IssuerName
;
421 byte[] serial
= cs
.SerialNumber
;
422 foreach (X509Certificate x509
in coll
) {
423 if (CompareIssuerSerial (issuer
, serial
, x509
)) {
424 if (x509
.PublicKey
.Length
> counterSignature
.Length
) {
425 RSACryptoServiceProvider rsa
= (RSACryptoServiceProvider
) x509
.RSA
;
426 // we need to HACK around bad (PKCS#1 1.5) signatures made by Verisign Timestamp Service
427 // and this means copying stuff into our own RSAManaged to get the required flexibility
428 RSAManaged rsam
= new RSAManaged ();
429 rsam
.ImportParameters (rsa
.ExportParameters (false));
430 if (PKCS1
.Verify_v15 (rsam
, ha
, p7hash
, counterSignature
, true)) {
431 timestampChain
.LoadCertificates (coll
);
432 return (timestampChain
.Build (x509
));
437 // no certificate can verify this signature!
441 private void Reset ()
447 signingCertificate
= null;
450 trustedTimestampRoot
= false;
451 signerChain
.Reset ();
452 timestampChain
.Reset ();
453 timestamp
= DateTime
.MinValue
;