2 // X509Certificates.cs: Handles X.509 certificates.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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.
31 using System.Runtime.Serialization;
32 using System.Security.Cryptography;
33 using SSCX = System.Security.Cryptography.X509Certificates;
34 using System.Security.Permissions;
37 namespace Mono.Security.X509 {
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/
46 internal class X509Certificate : ISerializable {
48 public class X509Certificate : ISerializable {
50 public class X509Certificate {
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;
70 // from http://www.ietf.org/rfc/rfc2459.txt
72 //Certificate ::= SEQUENCE {
73 // tbsCertificate TBSCertificate,
74 // signatureAlgorithm AlgorithmIdentifier,
75 // signature BIT STRING }
77 //TBSCertificate ::= SEQUENCE {
78 // version [0] Version DEFAULT v1,
79 // serialNumber CertificateSerialNumber,
80 // signature AlgorithmIdentifier,
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 -- }
92 private byte[] serialnumber;
94 // private byte[] issuerUniqueID;
95 // private byte[] subjectUniqueID;
96 private X509ExtensionCollection extensions;
98 // that's were the real job is!
99 private void Parse (byte[] data)
101 string e = "Input data cannot be coded as a valid certificate.";
103 decoder = new ASN1 (data);
105 if (decoder.Tag != 0x30)
106 throw new CryptographicException (e);
107 // Certificate / TBSCertificate
108 if (decoder [0].Tag != 0x30)
109 throw new CryptographicException (e);
111 ASN1 tbsCertificate = decoder [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
123 // Certificate / TBSCertificate / CertificateSerialNumber
124 ASN1 sn = decoder [0][tbs++];
126 throw new CryptographicException (e);
127 serialnumber = sn.Value;
128 Array.Reverse (serialnumber, 0, serialnumber.Length);
130 // Certificate / TBSCertificate / AlgorithmIdentifier
132 // ASN1 signatureAlgo = tbsCertificate.Element (tbs++, 0x30);
134 ASN1 issuer = tbsCertificate.Element (tbs++, 0x30);
135 m_issuername = X501.ToString (issuer);
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);
143 ASN1 subject = tbsCertificate.Element (tbs++, 0x30);
144 m_subject = X501.ToString (subject);
146 ASN1 subjectPublicKeyInfo = tbsCertificate.Element (tbs++, 0x30);
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);
156 ASN1 subjectPublicKey = subjectPublicKeyInfo.Element (1, 0x03);
157 // we must drop th first byte (which is the number of unused bits
159 int n = subjectPublicKey.Length - 1;
160 m_publickey = new byte [n];
161 Buffer.BlockCopy (subjectPublicKey.Value, 1, m_publickey, 0, n);
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);
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 ();
176 m_signaturealgoparams = null;
178 // Certificate / TBSCertificate / issuerUniqueID
179 ASN1 issuerUID = tbsCertificate.Element (tbs, 0xA1);
180 if (issuerUID != null) {
182 // issuerUniqueID = issuerUID.Value;
185 // Certificate / TBSCertificate / subjectUniqueID
186 ASN1 subjectUID = tbsCertificate.Element (tbs, 0xA2);
187 if (subjectUID != null) {
189 // subjectUniqueID = subjectUID.Value;
192 // Certificate / TBSCertificate / Extensions
193 ASN1 extns = tbsCertificate.Element (tbs, 0xA3);
194 if ((extns != null) && (extns.Count == 1))
195 extensions = new X509ExtensionCollection (extns [0]);
197 extensions = new X509ExtensionCollection (null);
199 // keep a copy of the original data
200 m_encodedcert = (byte[]) data.Clone ();
202 catch (Exception ex) {
203 throw new CryptographicException (e, ex);
209 public X509Certificate (byte[] data)
215 private byte[] GetUnsignedBigInteger (byte[] integer)
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);
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))
239 dsaParams.Y = GetUnsignedBigInteger (pubkey.Value);
241 ASN1 param = new ASN1 (m_keyalgoparams);
242 if ((param == null) || (param.Tag != 0x30) || (param.Count < 3))
244 if ((param [0].Tag != 0x02) || (param [1].Tag != 0x02) || (param [2].Tag != 0x02))
246 dsaParams.P = GetUnsignedBigInteger (param [0].Value);
247 dsaParams.Q = GetUnsignedBigInteger (param [1].Value);
248 dsaParams.G = GetUnsignedBigInteger (param [2].Value);
250 // BUG: MS BCL 1.0 can't import a key which
251 // isn't the same size as the one present in
253 _dsa = (DSA) new DSACryptoServiceProvider (dsaParams.Y.Length << 3);
254 _dsa.ImportParameters (dsaParams);
266 public X509ExtensionCollection Extensions {
267 get { return extensions; }
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 ?
278 hash = HashAlgorithm.Create ("MD2");
280 hash = Mono.Security.Cryptography.MD2.Create ();
283 case "1.2.840.113549.1.1.4": // MD5 with RSA encryption
284 hash = MD5.Create ();
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 ();
294 if ((decoder == null) || (decoder.Count < 1))
296 byte[] toBeSigned = decoder [0].GetBytes ();
297 certhash = hash.ComputeHash (toBeSigned, 0, toBeSigned.Length);
299 return (byte[]) certhash.Clone ();
303 public virtual string IssuerName {
304 get { return m_issuername; }
307 public virtual string KeyAlgorithm {
308 get { return m_keyalgo; }
311 public virtual byte[] KeyAlgorithmParameters {
313 if (m_keyalgoparams == null)
315 return (byte[]) m_keyalgoparams.Clone ();
319 public virtual byte[] PublicKey {
321 if (m_publickey == null)
323 return (byte[]) m_publickey.Clone ();
327 public virtual RSA RSA {
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))
337 ASN1 exponent = pubkey [1];
338 if (exponent.Tag != 0x02)
341 rsaParams.Modulus = GetUnsignedBigInteger (modulus.Value);
342 rsaParams.Exponent = exponent.Value;
344 // BUG: MS BCL 1.0 can't import a key which
345 // isn't the same size as the one present in
347 int keySize = (rsaParams.Modulus.Length << 3);
348 _rsa = (RSA) new RSACryptoServiceProvider (keySize);
349 _rsa.ImportParameters (rsaParams);
361 public virtual byte[] RawData {
363 if (m_encodedcert == null)
365 return (byte[]) m_encodedcert.Clone ();
369 public virtual byte[] SerialNumber {
371 if (serialnumber == null)
373 return (byte[]) serialnumber.Clone ();
377 public virtual byte[] Signature {
379 if (signature == null)
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 ();
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))
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);
402 throw new CryptographicException ("Unsupported hash algorithm: " + m_signaturealgo);
407 public virtual string SignatureAlgorithm {
408 get { return m_signaturealgo; }
411 public virtual byte[] SignatureAlgorithmParameters {
413 if (m_signaturealgoparams == null)
414 return m_signaturealgoparams;
415 return (byte[]) m_signaturealgoparams.Clone ();
419 public virtual string SubjectName {
420 get { return m_subject; }
423 public virtual DateTime ValidFrom {
424 get { return m_from; }
427 public virtual DateTime ValidUntil {
428 get { return m_until; }
432 get { return version; }
435 public bool IsCurrent {
436 get { return WasCurrent (DateTime.Now); }
439 public bool WasCurrent (DateTime instant)
441 return ((instant > ValidFrom) && (instant <= ValidUntil));
444 internal bool VerifySignature (DSA dsa)
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);
453 internal bool VerifySignature (RSA rsa)
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");
462 // MD5 with RSA encryption
463 case "1.2.840.113549.1.1.4":
464 v.SetHashAlgorithm ("MD5");
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");
472 throw new CryptographicException ("Unsupported hash algorithm: " + m_signaturealgo);
474 return v.VerifySignature (this.Hash, this.Signature);
477 public bool VerifySignature (AsymmetricAlgorithm aa)
480 throw new ArgumentNullException ("aa");
483 return VerifySignature (aa as RSA);
485 return VerifySignature (aa as DSA);
487 throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString ());
490 public bool CheckSignature (byte[] hash, string hashAlgorithm, byte[] signature)
492 RSACryptoServiceProvider r = (RSACryptoServiceProvider) RSA;
493 return r.VerifyHash (hash, hashAlgorithm, signature);
496 public bool IsSelfSigned {
498 if (m_issuername == m_subject)
499 return VerifySignature (RSA);
505 #if INSIDE_CORLIB || NET_2_0
506 protected X509Certificate (SerializationInfo info, StreamingContext context)
508 Parse ((byte[]) info.GetValue ("raw", typeof (byte[])));
511 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
512 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
514 info.AddValue ("raw", m_encodedcert);
515 // note: we NEVER serialize the private key