In .:
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509Certificate.cs
1 //
2 // X509Certificates.cs: Handles X.509 certificates.
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Runtime.Serialization;
32 using System.Security.Cryptography;
33 using SSCX = System.Security.Cryptography.X509Certificates;
34 using System.Security.Permissions;
35 using System.Text;
36
37 namespace Mono.Security.X509 {
38
39         // References:
40         // a.   Internet X.509 Public Key Infrastructure Certificate and CRL Profile
41         //      http://www.ietf.org/rfc/rfc3280.txt
42         // b.   ITU ASN.1 standards (free download)
43         //      http://www.itu.int/ITU-T/studygroups/com17/languages/
44
45 #if INSIDE_CORLIB
46         internal class X509Certificate : ISerializable {
47 #elif NET_2_0
48         public class X509Certificate : ISerializable {
49 #else
50         public class X509Certificate {
51 #endif
52
53                 private ASN1 decoder;
54
55                 private byte[] m_encodedcert;
56                 private DateTime m_from;
57                 private DateTime m_until;
58                 private string m_issuername;
59                 private string m_keyalgo;
60                 private byte[] m_keyalgoparams;
61                 private string m_subject;
62                 private byte[] m_publickey;
63                 private byte[] signature;
64                 private string m_signaturealgo;
65                 private byte[] m_signaturealgoparams;
66                 private byte[] certhash;
67                 private RSA _rsa;
68                 private DSA _dsa;
69                 
70                 // from http://www.ietf.org/rfc/rfc2459.txt
71                 //
72                 //Certificate  ::=  SEQUENCE  {
73                 //     tbsCertificate       TBSCertificate,
74                 //     signatureAlgorithm   AlgorithmIdentifier,
75                 //     signature            BIT STRING  }
76                 //
77                 //TBSCertificate  ::=  SEQUENCE  {
78                 //     version         [0]  Version DEFAULT v1,
79                 //     serialNumber         CertificateSerialNumber,
80                 //     signature            AlgorithmIdentifier,
81                 //     issuer               Name,
82                 //     validity             Validity,
83                 //     subject              Name,
84                 //     subjectPublicKeyInfo SubjectPublicKeyInfo,
85                 //     issuerUniqueID  [1]  IMPLICIT UniqueIdentifier OPTIONAL,
86                 //                          -- If present, version shall be v2 or v3
87                 //     subjectUniqueID [2]  IMPLICIT UniqueIdentifier OPTIONAL,
88                 //                          -- If present, version shall be v2 or v3
89                 //     extensions      [3]  Extensions OPTIONAL
90                 //                          -- If present, version shall be v3 --  }
91                 private int version;
92                 private byte[] serialnumber;
93
94 //              private byte[] issuerUniqueID;
95 //              private byte[] subjectUniqueID;
96                 private X509ExtensionCollection extensions;
97
98                 // that's were the real job is!
99                 private void Parse (byte[] data) 
100                 {
101                         string e = "Input data cannot be coded as a valid certificate.";
102                         try {
103                                 decoder = new ASN1 (data);
104                                 // Certificate 
105                                 if (decoder.Tag != 0x30)
106                                         throw new CryptographicException (e);
107                                 // Certificate / TBSCertificate
108                                 if (decoder [0].Tag != 0x30)
109                                         throw new CryptographicException (e);
110
111                                 ASN1 tbsCertificate = decoder [0];
112
113                                 int tbs = 0;
114                                 // Certificate / TBSCertificate / Version
115                                 ASN1 v = decoder [0][tbs];
116                                 version = 1;                    // DEFAULT v1
117                                 if ((v.Tag == 0xA0) && (v.Count > 0)) {
118                                         // version (optional) is present only in v2+ certs
119                                         version += v [0].Value [0];     // zero based
120                                         tbs++;
121                                 }
122
123                                 // Certificate / TBSCertificate / CertificateSerialNumber
124                                 ASN1 sn = decoder [0][tbs++];
125                                 if (sn.Tag != 0x02) 
126                                         throw new CryptographicException (e);
127                                 serialnumber = sn.Value;
128                                 Array.Reverse (serialnumber, 0, serialnumber.Length);
129                 
130                                 // Certificate / TBSCertificate / AlgorithmIdentifier
131                                 tbs++;
132                                 // ASN1 signatureAlgo = tbsCertificate.Element (tbs++, 0x30); 
133                 
134                                 ASN1 issuer = tbsCertificate.Element (tbs++, 0x30); 
135                                 m_issuername = X501.ToString (issuer);
136                 
137                                 ASN1 validity = tbsCertificate.Element (tbs++, 0x30);
138                                 ASN1 notBefore = validity [0];
139                                 m_from = ASN1Convert.ToDateTime (notBefore);
140                                 ASN1 notAfter = validity [1];
141                                 m_until = ASN1Convert.ToDateTime (notAfter);
142                 
143                                 ASN1 subject = tbsCertificate.Element (tbs++, 0x30);
144                                 m_subject = X501.ToString (subject);
145                 
146                                 ASN1 subjectPublicKeyInfo = tbsCertificate.Element (tbs++, 0x30);
147                 
148                                 ASN1 algorithm = subjectPublicKeyInfo.Element (0, 0x30);
149                                 ASN1 algo = algorithm.Element (0, 0x06);
150                                 m_keyalgo = ASN1Convert.ToOid (algo);
151                                 // parameters ANY DEFINED BY algorithm OPTIONAL
152                                 // so we dont ask for a specific (Element) type and return DER
153                                 ASN1 parameters = algorithm [1];
154                                 m_keyalgoparams = ((algorithm.Count > 1) ? parameters.GetBytes () : null);
155                 
156                                 ASN1 subjectPublicKey = subjectPublicKeyInfo.Element (1, 0x03); 
157                                 // we must drop th first byte (which is the number of unused bits
158                                 // in the BITSTRING)
159                                 int n = subjectPublicKey.Length - 1;
160                                 m_publickey = new byte [n];
161                                 Buffer.BlockCopy (subjectPublicKey.Value, 1, m_publickey, 0, n);
162
163                                 // signature processing
164                                 byte[] bitstring = decoder [2].Value;
165                                 // first byte contains unused bits in first byte
166                                 signature = new byte [bitstring.Length - 1];
167                                 Buffer.BlockCopy (bitstring, 1, signature, 0, signature.Length);
168
169                                 algorithm = decoder [1];
170                                 algo = algorithm.Element (0, 0x06);
171                                 m_signaturealgo = ASN1Convert.ToOid (algo);
172                                 parameters = algorithm [1];
173                                 if (parameters != null)
174                                         m_signaturealgoparams = parameters.GetBytes ();
175                                 else
176                                         m_signaturealgoparams = null;
177
178                                 // Certificate / TBSCertificate / issuerUniqueID
179                                 ASN1 issuerUID = tbsCertificate.Element (tbs, 0xA1);
180                                 if (issuerUID != null) {
181                                         tbs++;
182 //                                      issuerUniqueID = issuerUID.Value;
183                                 }
184
185                                 // Certificate / TBSCertificate / subjectUniqueID
186                                 ASN1 subjectUID = tbsCertificate.Element (tbs, 0xA2);
187                                 if (subjectUID != null) {
188                                         tbs++;
189 //                                      subjectUniqueID = subjectUID.Value;
190                                 }
191
192                                 // Certificate / TBSCertificate / Extensions
193                                 ASN1 extns = tbsCertificate.Element (tbs, 0xA3);
194                                 if ((extns != null) && (extns.Count == 1))
195                                         extensions = new X509ExtensionCollection (extns [0]);
196                                 else
197                                         extensions = new X509ExtensionCollection (null);
198
199                                 // keep a copy of the original data
200                                 m_encodedcert = (byte[]) data.Clone ();
201                         }
202                         catch (Exception ex) {
203                                 throw new CryptographicException (e, ex);
204                         }
205                 }
206
207                 // constructors
208
209                 public X509Certificate (byte[] data) 
210                 {
211                         if (data != null)
212                                 Parse (data);
213                 }
214
215                 private byte[] GetUnsignedBigInteger (byte[] integer) 
216                 {
217                         if (integer [0] == 0x00) {
218                                 // this first byte is added so we're sure it's an unsigned integer
219                                 // however we can't feed it into RSAParameters or DSAParameters
220                                 int length = integer.Length - 1;
221                                 byte[] uinteger = new byte [length];
222                                 Buffer.BlockCopy (integer, 1, uinteger, 0, length);
223                                 return uinteger;
224                         }
225                         else
226                                 return integer;
227                 }
228
229                 // public methods
230
231                 public DSA DSA {
232                         get {
233                                 if (_dsa == null) {
234                                         DSAParameters dsaParams = new DSAParameters ();
235                                         // for DSA m_publickey contains 1 ASN.1 integer - Y
236                                         ASN1 pubkey = new ASN1 (m_publickey);
237                                         if ((pubkey == null) || (pubkey.Tag != 0x02))
238                                                 return null;
239                                         dsaParams.Y = GetUnsignedBigInteger (pubkey.Value);
240
241                                         ASN1 param = new ASN1 (m_keyalgoparams);
242                                         if ((param == null) || (param.Tag != 0x30) || (param.Count < 3))
243                                                 return null;
244                                         if ((param [0].Tag != 0x02) || (param [1].Tag != 0x02) || (param [2].Tag != 0x02))
245                                                 return null;
246                                         dsaParams.P = GetUnsignedBigInteger (param [0].Value);
247                                         dsaParams.Q = GetUnsignedBigInteger (param [1].Value);
248                                         dsaParams.G = GetUnsignedBigInteger (param [2].Value);
249
250                                         // BUG: MS BCL 1.0 can't import a key which 
251                                         // isn't the same size as the one present in
252                                         // the container.
253                                         _dsa = (DSA) new DSACryptoServiceProvider (dsaParams.Y.Length << 3);
254                                         _dsa.ImportParameters (dsaParams);
255                                 }
256                                 return _dsa; 
257                         }
258 #if NET_2_0
259                         set {
260                                 _dsa = value;
261                                 _rsa = null;
262                         }
263 #endif
264                 }
265
266                 public X509ExtensionCollection Extensions {
267                         get { return extensions; }
268                 }
269
270                 public byte[] Hash {
271                         get {
272                                 if (certhash == null) {
273                                         HashAlgorithm hash = null;
274                                         switch (m_signaturealgo) {
275                                                 case "1.2.840.113549.1.1.2":    // MD2 with RSA encryption 
276                                                         // maybe someone installed MD2 ?
277 #if INSIDE_CORLIB
278                                                         hash = HashAlgorithm.Create ("MD2");
279 #else
280                                                         hash = Mono.Security.Cryptography.MD2.Create ();
281 #endif
282                                                         break;
283                                                 case "1.2.840.113549.1.1.4":    // MD5 with RSA encryption 
284                                                         hash = MD5.Create ();
285                                                         break;
286                                                 case "1.2.840.113549.1.1.5":    // SHA-1 with RSA Encryption 
287                                                 case "1.3.14.3.2.29":           // SHA1 with RSA signature 
288                                                 case "1.2.840.10040.4.3":       // SHA1-1 with DSA
289                                                         hash = SHA1.Create ();
290                                                         break;
291                                                 default:
292                                                         return null;
293                                         }
294                                         if ((decoder == null) || (decoder.Count < 1))
295                                                 return null;
296                                         byte[] toBeSigned = decoder [0].GetBytes ();
297                                         certhash = hash.ComputeHash (toBeSigned, 0, toBeSigned.Length);
298                                 }
299                                 return (byte[]) certhash.Clone ();
300                         }
301                 }
302
303                 public virtual string IssuerName {
304                         get { return m_issuername; }
305                 }
306
307                 public virtual string KeyAlgorithm {
308                         get { return m_keyalgo; }
309                 }
310
311                 public virtual byte[] KeyAlgorithmParameters {
312                         get {
313                                 if (m_keyalgoparams == null)
314                                         return null;
315                                 return (byte[]) m_keyalgoparams.Clone (); 
316                         }
317                 }
318
319                 public virtual byte[] PublicKey {
320                         get { 
321                                 if (m_publickey == null)
322                                         return null;
323                                 return (byte[]) m_publickey.Clone ();
324                         }
325                 }
326
327                 public virtual RSA RSA {
328                         get {
329                                 if (_rsa == null) {
330                                         RSAParameters rsaParams = new RSAParameters ();
331                                         // for RSA m_publickey contains 2 ASN.1 integers
332                                         // the modulus and the public exponent
333                                         ASN1 pubkey = new ASN1 (m_publickey);
334                                         ASN1 modulus = pubkey [0];
335                                         if ((modulus == null) || (modulus.Tag != 0x02))
336                                                 return null;
337                                         ASN1 exponent = pubkey [1];
338                                         if (exponent.Tag != 0x02)
339                                                 return null;
340
341                                         rsaParams.Modulus = GetUnsignedBigInteger (modulus.Value);
342                                         rsaParams.Exponent = exponent.Value;
343
344                                         // BUG: MS BCL 1.0 can't import a key which 
345                                         // isn't the same size as the one present in
346                                         // the container.
347                                         int keySize = (rsaParams.Modulus.Length << 3);
348                                         _rsa = (RSA) new RSACryptoServiceProvider (keySize);
349                                         _rsa.ImportParameters (rsaParams);
350                                 }
351                                 return _rsa; 
352                         }
353 #if NET_2_0
354                         set {
355                                 _dsa = null;
356                                 _rsa = value;
357                         }
358 #endif
359                 }
360                 
361                 public virtual byte[] RawData {
362                         get {
363                                 if (m_encodedcert == null)
364                                         return null;
365                                 return (byte[]) m_encodedcert.Clone ();
366                         }
367                 }
368
369                 public virtual byte[] SerialNumber {
370                         get { 
371                                 if (serialnumber == null)
372                                         return null;
373                                 return (byte[]) serialnumber.Clone (); 
374                         }
375                 }
376
377                 public virtual byte[] Signature {
378                         get { 
379                                 if (signature == null)
380                                         return null;
381
382                                 switch (m_signaturealgo) {
383                                         case "1.2.840.113549.1.1.2":    // MD2 with RSA encryption 
384                                         case "1.2.840.113549.1.1.4":    // MD5 with RSA encryption 
385                                         case "1.2.840.113549.1.1.5":    // SHA-1 with RSA Encryption 
386                                         case "1.3.14.3.2.29":           // SHA1 with RSA signature
387                                                 return (byte[]) signature.Clone ();
388
389                                         case "1.2.840.10040.4.3":       // SHA-1 with DSA
390                                                 ASN1 sign = new ASN1 (signature);
391                                                 if ((sign == null) || (sign.Count != 2))
392                                                         return null;
393                                                 // parts may be less than 20 bytes (i.e. first bytes were 0x00)
394                                                 byte[] part1 = sign [0].Value;
395                                                 byte[] part2 = sign [1].Value;
396                                                 byte[] sig = new byte [40];
397                                                 Buffer.BlockCopy (part1, 0, sig, (20 - part1.Length), part1.Length);
398                                                 Buffer.BlockCopy (part2, 0, sig, (40 - part2.Length), part2.Length);
399                                                 return sig;
400
401                                         default:
402                                                 throw new CryptographicException ("Unsupported hash algorithm: " + m_signaturealgo);
403                                 }
404                         }
405                 }
406
407                 public virtual string SignatureAlgorithm {
408                         get { return m_signaturealgo; }
409                 }
410
411                 public virtual byte[] SignatureAlgorithmParameters {
412                         get { 
413                                 if (m_signaturealgoparams == null)
414                                         return m_signaturealgoparams;
415                                 return (byte[]) m_signaturealgoparams.Clone ();
416                         }
417                 }
418
419                 public virtual string SubjectName {
420                         get { return m_subject; }
421                 }
422
423                 public virtual DateTime ValidFrom {
424                         get { return m_from; }
425                 }
426
427                 public virtual DateTime ValidUntil {
428                         get { return m_until; }
429                 }
430
431                 public int Version {
432                         get { return version; }
433                 }
434
435                 public bool IsCurrent {
436                         get { return WasCurrent (DateTime.Now); }
437                 }
438
439                 public bool WasCurrent (DateTime instant) 
440                 {
441                         return ((instant > ValidFrom) && (instant <= ValidUntil));
442                 }
443
444                 internal bool VerifySignature (DSA dsa) 
445                 {
446                         // signatureOID is check by both this.Hash and this.Signature
447                         DSASignatureDeformatter v = new DSASignatureDeformatter (dsa);
448                         // only SHA-1 is supported
449                         v.SetHashAlgorithm ("SHA1");
450                         return v.VerifySignature (this.Hash, this.Signature);
451                 }
452
453                 internal bool VerifySignature (RSA rsa) 
454                 {
455                         RSAPKCS1SignatureDeformatter v = new RSAPKCS1SignatureDeformatter (rsa);
456                         switch (m_signaturealgo) {
457                                 // MD2 with RSA encryption 
458                                 case "1.2.840.113549.1.1.2":
459                                         // maybe someone installed MD2 ?
460                                         v.SetHashAlgorithm ("MD2");
461                                         break;
462                                 // MD5 with RSA encryption 
463                                 case "1.2.840.113549.1.1.4":
464                                         v.SetHashAlgorithm ("MD5");
465                                         break;
466                                 // SHA-1 with RSA Encryption 
467                                 case "1.2.840.113549.1.1.5":
468                                 case "1.3.14.3.2.29":
469                                         v.SetHashAlgorithm ("SHA1");
470                                         break;
471                                 default:
472                                         throw new CryptographicException ("Unsupported hash algorithm: " + m_signaturealgo);
473                         }
474                         return v.VerifySignature (this.Hash, this.Signature);
475                 }
476
477                 public bool VerifySignature (AsymmetricAlgorithm aa) 
478                 {
479                         if (aa == null)
480                                 throw new ArgumentNullException ("aa");
481
482                         if (aa is RSA)
483                                 return VerifySignature (aa as RSA);
484                         else if (aa is DSA)
485                                 return VerifySignature (aa as DSA);
486                         else 
487                                 throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString ());
488                 }
489
490                 public bool CheckSignature (byte[] hash, string hashAlgorithm, byte[] signature) 
491                 {
492                         RSACryptoServiceProvider r = (RSACryptoServiceProvider) RSA;
493                         return r.VerifyHash (hash, hashAlgorithm, signature);
494                 }
495
496                 public bool IsSelfSigned {
497                         get { 
498                                 if (m_issuername == m_subject)
499                                         return VerifySignature (RSA); 
500                                 else
501                                         return false;
502                         }
503                 }
504
505 #if INSIDE_CORLIB || NET_2_0
506                 protected X509Certificate (SerializationInfo info, StreamingContext context)
507                 {
508                         Parse ((byte[]) info.GetValue ("raw", typeof (byte[])));
509                 }
510
511                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
512                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
513                 {
514                         info.AddValue ("raw", m_encodedcert);
515                         // note: we NEVER serialize the private key
516                 }
517 #endif
518         }
519 }