2 // X509Certificates.cs: Handles X.509 certificates.
5 // Sebastien Pouliot <sebastien@xamarin.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
9 // Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.Serialization;
33 using System.Security.Cryptography;
34 using SSCX = System.Security.Cryptography.X509Certificates;
35 using System.Security.Permissions;
37 using Mono.Security.Cryptography;
39 namespace Mono.Security.X509 {
42 // a. Internet X.509 Public Key Infrastructure Certificate and CRL Profile
43 // http://www.ietf.org/rfc/rfc3280.txt
44 // b. ITU ASN.1 standards (free download)
45 // http://www.itu.int/ITU-T/studygroups/com17/languages/
48 internal class X509Certificate : ISerializable {
50 public class X509Certificate : ISerializable {
55 private byte[] m_encodedcert;
56 private DateTime m_from;
57 private DateTime m_until;
59 private string m_issuername;
60 private string m_keyalgo;
61 private byte[] m_keyalgoparams;
63 private string m_subject;
64 private byte[] m_publickey;
65 private byte[] signature;
66 private string m_signaturealgo;
67 private byte[] m_signaturealgoparams;
68 private byte[] certhash;
72 // from http://www.ietf.org/rfc/rfc2459.txt
74 //Certificate ::= SEQUENCE {
75 // tbsCertificate TBSCertificate,
76 // signatureAlgorithm AlgorithmIdentifier,
77 // signature BIT STRING }
79 //TBSCertificate ::= SEQUENCE {
80 // version [0] Version DEFAULT v1,
81 // serialNumber CertificateSerialNumber,
82 // signature AlgorithmIdentifier,
86 // subjectPublicKeyInfo SubjectPublicKeyInfo,
87 // issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
88 // -- If present, version shall be v2 or v3
89 // subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
90 // -- If present, version shall be v2 or v3
91 // extensions [3] Extensions OPTIONAL
92 // -- If present, version shall be v3 -- }
94 private byte[] serialnumber;
96 private byte[] issuerUniqueID;
97 private byte[] subjectUniqueID;
98 private X509ExtensionCollection extensions;
100 private static string encoding_error = Locale.GetText ("Input data cannot be coded as a valid certificate.");
103 // that's were the real job is!
104 private void Parse (byte[] data)
107 decoder = new ASN1 (data);
109 if (decoder.Tag != 0x30)
110 throw new CryptographicException (encoding_error);
111 // Certificate / TBSCertificate
112 if (decoder [0].Tag != 0x30)
113 throw new CryptographicException (encoding_error);
115 ASN1 tbsCertificate = decoder [0];
118 // Certificate / TBSCertificate / Version
119 ASN1 v = decoder [0][tbs];
120 version = 1; // DEFAULT v1
121 if ((v.Tag == 0xA0) && (v.Count > 0)) {
122 // version (optional) is present only in v2+ certs
123 version += v [0].Value [0]; // zero based
127 // Certificate / TBSCertificate / CertificateSerialNumber
128 ASN1 sn = decoder [0][tbs++];
130 throw new CryptographicException (encoding_error);
131 serialnumber = sn.Value;
132 Array.Reverse (serialnumber, 0, serialnumber.Length);
134 // Certificate / TBSCertificate / AlgorithmIdentifier
136 // ASN1 signatureAlgo = tbsCertificate.Element (tbs++, 0x30);
138 issuer = tbsCertificate.Element (tbs++, 0x30);
139 m_issuername = X501.ToString (issuer);
141 ASN1 validity = tbsCertificate.Element (tbs++, 0x30);
142 ASN1 notBefore = validity [0];
143 m_from = ASN1Convert.ToDateTime (notBefore);
144 ASN1 notAfter = validity [1];
145 m_until = ASN1Convert.ToDateTime (notAfter);
147 subject = tbsCertificate.Element (tbs++, 0x30);
148 m_subject = X501.ToString (subject);
150 ASN1 subjectPublicKeyInfo = tbsCertificate.Element (tbs++, 0x30);
152 ASN1 algorithm = subjectPublicKeyInfo.Element (0, 0x30);
153 ASN1 algo = algorithm.Element (0, 0x06);
154 m_keyalgo = ASN1Convert.ToOid (algo);
155 // parameters ANY DEFINED BY algorithm OPTIONAL
156 // so we dont ask for a specific (Element) type and return DER
157 ASN1 parameters = algorithm [1];
158 m_keyalgoparams = ((algorithm.Count > 1) ? parameters.GetBytes () : null);
160 ASN1 subjectPublicKey = subjectPublicKeyInfo.Element (1, 0x03);
161 // we must drop th first byte (which is the number of unused bits
163 int n = subjectPublicKey.Length - 1;
164 m_publickey = new byte [n];
165 Buffer.BlockCopy (subjectPublicKey.Value, 1, m_publickey, 0, n);
167 // signature processing
168 byte[] bitstring = decoder [2].Value;
169 // first byte contains unused bits in first byte
170 signature = new byte [bitstring.Length - 1];
171 Buffer.BlockCopy (bitstring, 1, signature, 0, signature.Length);
173 algorithm = decoder [1];
174 algo = algorithm.Element (0, 0x06);
175 m_signaturealgo = ASN1Convert.ToOid (algo);
176 parameters = algorithm [1];
177 if (parameters != null)
178 m_signaturealgoparams = parameters.GetBytes ();
180 m_signaturealgoparams = null;
182 // Certificate / TBSCertificate / issuerUniqueID
183 ASN1 issuerUID = tbsCertificate.Element (tbs, 0x81);
184 if (issuerUID != null) {
186 issuerUniqueID = issuerUID.Value;
189 // Certificate / TBSCertificate / subjectUniqueID
190 ASN1 subjectUID = tbsCertificate.Element (tbs, 0x82);
191 if (subjectUID != null) {
193 subjectUniqueID = subjectUID.Value;
196 // Certificate / TBSCertificate / Extensions
197 ASN1 extns = tbsCertificate.Element (tbs, 0xA3);
198 if ((extns != null) && (extns.Count == 1))
199 extensions = new X509ExtensionCollection (extns [0]);
201 extensions = new X509ExtensionCollection (null);
203 // keep a copy of the original data
204 m_encodedcert = (byte[]) data.Clone ();
206 catch (Exception ex) {
207 throw new CryptographicException (encoding_error, ex);
213 public X509Certificate (byte[] data)
216 // does it looks like PEM ?
217 if ((data.Length > 0) && (data [0] != 0x30)) {
219 data = PEM ("CERTIFICATE", data);
221 catch (Exception ex) {
222 throw new CryptographicException (encoding_error, ex);
229 private byte[] GetUnsignedBigInteger (byte[] integer)
231 if (integer [0] == 0x00) {
232 // this first byte is added so we're sure it's an unsigned integer
233 // however we can't feed it into RSAParameters or DSAParameters
234 int length = integer.Length - 1;
235 byte[] uinteger = new byte [length];
236 Buffer.BlockCopy (integer, 1, uinteger, 0, length);
247 if (m_keyalgoparams == null)
248 throw new CryptographicException ("Missing key algorithm parameters.");
251 DSAParameters dsaParams = new DSAParameters ();
252 // for DSA m_publickey contains 1 ASN.1 integer - Y
253 ASN1 pubkey = new ASN1 (m_publickey);
254 if ((pubkey == null) || (pubkey.Tag != 0x02))
256 dsaParams.Y = GetUnsignedBigInteger (pubkey.Value);
258 ASN1 param = new ASN1 (m_keyalgoparams);
259 if ((param == null) || (param.Tag != 0x30) || (param.Count < 3))
261 if ((param [0].Tag != 0x02) || (param [1].Tag != 0x02) || (param [2].Tag != 0x02))
263 dsaParams.P = GetUnsignedBigInteger (param [0].Value);
264 dsaParams.Q = GetUnsignedBigInteger (param [1].Value);
265 dsaParams.G = GetUnsignedBigInteger (param [2].Value);
267 // BUG: MS BCL 1.0 can't import a key which
268 // isn't the same size as the one present in
270 _dsa = (DSA) new DSACryptoServiceProvider (dsaParams.Y.Length << 3);
271 _dsa.ImportParameters (dsaParams);
283 public X509ExtensionCollection Extensions {
284 get { return extensions; }
289 if (certhash == null) {
290 if ((decoder == null) || (decoder.Count < 1))
292 string algo = PKCS1.HashNameFromOid (m_signaturealgo, false);
295 byte[] toBeSigned = decoder [0].GetBytes ();
296 using (var hash = PKCS1.CreateFromName (algo))
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 ();
317 set { m_keyalgoparams = value; }
320 public virtual byte[] PublicKey {
322 if (m_publickey == null)
324 return (byte[]) m_publickey.Clone ();
328 public virtual RSA RSA {
331 RSAParameters rsaParams = new RSAParameters ();
332 // for RSA m_publickey contains 2 ASN.1 integers
333 // the modulus and the public exponent
334 ASN1 pubkey = new ASN1 (m_publickey);
335 ASN1 modulus = pubkey [0];
336 if ((modulus == null) || (modulus.Tag != 0x02))
338 ASN1 exponent = pubkey [1];
339 if (exponent.Tag != 0x02)
342 rsaParams.Modulus = GetUnsignedBigInteger (modulus.Value);
343 rsaParams.Exponent = exponent.Value;
345 // BUG: MS BCL 1.0 can't import a key which
346 // isn't the same size as the one present in
348 int keySize = (rsaParams.Modulus.Length << 3);
349 _rsa = (RSA) new RSACryptoServiceProvider (keySize);
350 _rsa.ImportParameters (rsaParams);
362 public virtual byte[] RawData {
364 if (m_encodedcert == null)
366 return (byte[]) m_encodedcert.Clone ();
370 public virtual byte[] SerialNumber {
372 if (serialnumber == null)
374 return (byte[]) serialnumber.Clone ();
378 public virtual byte[] Signature {
380 if (signature == null)
383 switch (m_signaturealgo) {
384 case "1.2.840.113549.1.1.2": // MD2 with RSA encryption
385 case "1.2.840.113549.1.1.3": // MD4 with RSA encryption
386 case "1.2.840.113549.1.1.4": // MD5 with RSA encryption
387 case "1.2.840.113549.1.1.5": // SHA-1 with RSA Encryption
388 case "1.3.14.3.2.29": // SHA1 with RSA signature
389 case "1.2.840.113549.1.1.11": // SHA-256 with RSA Encryption
390 case "1.2.840.113549.1.1.12": // SHA-384 with RSA Encryption
391 case "1.2.840.113549.1.1.13": // SHA-512 with RSA Encryption
392 case "1.3.36.3.3.1.2": // RIPEMD160 with RSA Encryption
393 return (byte[]) signature.Clone ();
395 case "1.2.840.10040.4.3": // SHA-1 with DSA
396 ASN1 sign = new ASN1 (signature);
397 if ((sign == null) || (sign.Count != 2))
399 byte[] part1 = sign [0].Value;
400 byte[] part2 = sign [1].Value;
401 byte[] sig = new byte [40];
402 // parts may be less than 20 bytes (i.e. first bytes were 0x00)
403 // parts may be more than 20 bytes (i.e. first byte > 0x80, negative)
404 int s1 = System.Math.Max (0, part1.Length - 20);
405 int e1 = System.Math.Max (0, 20 - part1.Length);
406 Buffer.BlockCopy (part1, s1, sig, e1, part1.Length - s1);
407 int s2 = System.Math.Max (0, part2.Length - 20);
408 int e2 = System.Math.Max (20, 40 - part2.Length);
409 Buffer.BlockCopy (part2, s2, sig, e2, part2.Length - s2);
413 throw new CryptographicException ("Unsupported hash algorithm: " + m_signaturealgo);
418 public virtual string SignatureAlgorithm {
419 get { return m_signaturealgo; }
422 public virtual byte[] SignatureAlgorithmParameters {
424 if (m_signaturealgoparams == null)
425 return m_signaturealgoparams;
426 return (byte[]) m_signaturealgoparams.Clone ();
430 public virtual string SubjectName {
431 get { return m_subject; }
434 public virtual DateTime ValidFrom {
435 get { return m_from; }
438 public virtual DateTime ValidUntil {
439 get { return m_until; }
443 get { return version; }
446 public bool IsCurrent {
447 get { return WasCurrent (DateTime.UtcNow); }
450 public bool WasCurrent (DateTime instant)
452 return ((instant > ValidFrom) && (instant <= ValidUntil));
455 // uncommon v2 "extension"
456 public byte[] IssuerUniqueIdentifier {
458 if (issuerUniqueID == null)
460 return (byte[]) issuerUniqueID.Clone ();
464 // uncommon v2 "extension"
465 public byte[] SubjectUniqueIdentifier {
467 if (subjectUniqueID == null)
469 return (byte[]) subjectUniqueID.Clone ();
473 internal bool VerifySignature (DSA dsa)
475 // signatureOID is check by both this.Hash and this.Signature
476 DSASignatureDeformatter v = new DSASignatureDeformatter (dsa);
477 // only SHA-1 is supported
478 v.SetHashAlgorithm ("SHA1");
479 return v.VerifySignature (this.Hash, this.Signature);
482 internal bool VerifySignature (RSA rsa)
485 if (m_signaturealgo == "1.2.840.10040.4.3")
487 RSAPKCS1SignatureDeformatter v = new RSAPKCS1SignatureDeformatter (rsa);
488 v.SetHashAlgorithm (PKCS1.HashNameFromOid (m_signaturealgo));
489 return v.VerifySignature (this.Hash, this.Signature);
492 public bool VerifySignature (AsymmetricAlgorithm aa)
495 throw new ArgumentNullException ("aa");
498 return VerifySignature (aa as RSA);
500 return VerifySignature (aa as DSA);
502 throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString ());
505 public bool CheckSignature (byte[] hash, string hashAlgorithm, byte[] signature)
507 RSACryptoServiceProvider r = (RSACryptoServiceProvider) RSA;
508 return r.VerifyHash (hash, hashAlgorithm, signature);
511 public bool IsSelfSigned {
513 if (m_issuername != m_subject)
518 return VerifySignature (RSA);
519 else if (DSA != null)
520 return VerifySignature (DSA);
522 return false; // e.g. a certificate with only DSA parameters
524 catch (CryptographicException) {
530 public ASN1 GetIssuerName ()
535 public ASN1 GetSubjectName ()
540 protected X509Certificate (SerializationInfo info, StreamingContext context)
542 Parse ((byte[]) info.GetValue ("raw", typeof (byte[])));
545 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
546 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
548 info.AddValue ("raw", m_encodedcert);
549 // note: we NEVER serialize the private key
552 static byte[] PEM (string type, byte[] data)
554 string pem = Encoding.ASCII.GetString (data);
555 string header = String.Format ("-----BEGIN {0}-----", type);
556 string footer = String.Format ("-----END {0}-----", type);
557 int start = pem.IndexOf (header) + header.Length;
558 int end = pem.IndexOf (footer, start);
559 string base64 = pem.Substring (start, (end - start));
560 return Convert.FromBase64String (base64);