X509CertificateBuilder.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // X509CertificateBuilder.cs: Handles building of X.509 certificates.
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. //
  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:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  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.
  29. //
  30. using System;
  31. using System.Security.Cryptography;
  32. namespace MediaBrowser.Server.Mono.Security {
  33. // From RFC3280
  34. /*
  35. * Certificate ::= SEQUENCE {
  36. * tbsCertificate TBSCertificate,
  37. * signatureAlgorithm AlgorithmIdentifier,
  38. * signature BIT STRING
  39. * }
  40. * TBSCertificate ::= SEQUENCE {
  41. * version [0] Version DEFAULT v1,
  42. * serialNumber CertificateSerialNumber,
  43. * signature AlgorithmIdentifier,
  44. * issuer Name,
  45. * validity Validity,
  46. * subject Name,
  47. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  48. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  49. * -- If present, version MUST be v2 or v3
  50. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  51. * -- If present, version MUST be v2 or v3
  52. * extensions [3] Extensions OPTIONAL
  53. * -- If present, version MUST be v3 --
  54. * }
  55. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  56. * CertificateSerialNumber ::= INTEGER
  57. * Validity ::= SEQUENCE {
  58. * notBefore Time,
  59. * notAfter Time
  60. * }
  61. * Time ::= CHOICE {
  62. * utcTime UTCTime,
  63. * generalTime GeneralizedTime
  64. * }
  65. */
  66. public class X509CertificateBuilder : X509Builder {
  67. private byte version;
  68. private byte[] sn;
  69. private string issuer;
  70. private DateTime notBefore;
  71. private DateTime notAfter;
  72. private string subject;
  73. private AsymmetricAlgorithm aa;
  74. private byte[] issuerUniqueID;
  75. private byte[] subjectUniqueID;
  76. private X509ExtensionCollection extensions;
  77. public X509CertificateBuilder () : this (3) {}
  78. public X509CertificateBuilder (byte version)
  79. {
  80. if (version > 3)
  81. throw new ArgumentException ("Invalid certificate version");
  82. this.version = version;
  83. extensions = new X509ExtensionCollection ();
  84. }
  85. public byte Version {
  86. get { return version; }
  87. set { version = value; }
  88. }
  89. public byte[] SerialNumber {
  90. get { return sn; }
  91. set { sn = value; }
  92. }
  93. public string IssuerName {
  94. get { return issuer; }
  95. set { issuer = value; }
  96. }
  97. public DateTime NotBefore {
  98. get { return notBefore; }
  99. set { notBefore = value; }
  100. }
  101. public DateTime NotAfter {
  102. get { return notAfter; }
  103. set { notAfter = value; }
  104. }
  105. public string SubjectName {
  106. get { return subject; }
  107. set { subject = value; }
  108. }
  109. public AsymmetricAlgorithm SubjectPublicKey {
  110. get { return aa; }
  111. set { aa = value; }
  112. }
  113. public byte[] IssuerUniqueId {
  114. get { return issuerUniqueID; }
  115. set { issuerUniqueID = value; }
  116. }
  117. public byte[] SubjectUniqueId {
  118. get { return subjectUniqueID; }
  119. set { subjectUniqueID = value; }
  120. }
  121. public X509ExtensionCollection Extensions {
  122. get { return extensions; }
  123. }
  124. /* SubjectPublicKeyInfo ::= SEQUENCE {
  125. * algorithm AlgorithmIdentifier,
  126. * subjectPublicKey BIT STRING }
  127. */
  128. private ASN1 SubjectPublicKeyInfo ()
  129. {
  130. ASN1 keyInfo = new ASN1 (0x30);
  131. if (aa is RSA) {
  132. keyInfo.Add (PKCS7.AlgorithmIdentifier ("1.2.840.113549.1.1.1"));
  133. RSAParameters p = (aa as RSA).ExportParameters (false);
  134. /* RSAPublicKey ::= SEQUENCE {
  135. * modulus INTEGER, -- n
  136. * publicExponent INTEGER } -- e
  137. */
  138. ASN1 key = new ASN1 (0x30);
  139. key.Add (ASN1Convert.FromUnsignedBigInteger (p.Modulus));
  140. key.Add (ASN1Convert.FromUnsignedBigInteger (p.Exponent));
  141. keyInfo.Add (new ASN1 (UniqueIdentifier (key.GetBytes ())));
  142. }
  143. else if (aa is DSA) {
  144. DSAParameters p = (aa as DSA).ExportParameters (false);
  145. /* Dss-Parms ::= SEQUENCE {
  146. * p INTEGER,
  147. * q INTEGER,
  148. * g INTEGER }
  149. */
  150. ASN1 param = new ASN1 (0x30);
  151. param.Add (ASN1Convert.FromUnsignedBigInteger (p.P));
  152. param.Add (ASN1Convert.FromUnsignedBigInteger (p.Q));
  153. param.Add (ASN1Convert.FromUnsignedBigInteger (p.G));
  154. keyInfo.Add (PKCS7.AlgorithmIdentifier ("1.2.840.10040.4.1", param));
  155. ASN1 key = keyInfo.Add (new ASN1 (0x03));
  156. // DSAPublicKey ::= INTEGER -- public key, y
  157. key.Add (ASN1Convert.FromUnsignedBigInteger (p.Y));
  158. }
  159. else
  160. throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString ());
  161. return keyInfo;
  162. }
  163. private byte[] UniqueIdentifier (byte[] id)
  164. {
  165. // UniqueIdentifier ::= BIT STRING
  166. ASN1 uid = new ASN1 (0x03);
  167. // first byte in a BITSTRING is the number of unused bits in the first byte
  168. byte[] v = new byte [id.Length + 1];
  169. Buffer.BlockCopy (id, 0, v, 1, id.Length);
  170. uid.Value = v;
  171. return uid.GetBytes ();
  172. }
  173. protected override ASN1 ToBeSigned (string oid)
  174. {
  175. // TBSCertificate
  176. ASN1 tbsCert = new ASN1 (0x30);
  177. if (version > 1) {
  178. // TBSCertificate / [0] Version DEFAULT v1,
  179. byte[] ver = { (byte)(version - 1) };
  180. ASN1 v = tbsCert.Add (new ASN1 (0xA0));
  181. v.Add (new ASN1 (0x02, ver));
  182. }
  183. // TBSCertificate / CertificateSerialNumber,
  184. tbsCert.Add (new ASN1 (0x02, sn));
  185. // TBSCertificate / AlgorithmIdentifier,
  186. tbsCert.Add (PKCS7.AlgorithmIdentifier (oid));
  187. // TBSCertificate / Name
  188. tbsCert.Add (X501.FromString (issuer));
  189. // TBSCertificate / Validity
  190. ASN1 validity = tbsCert.Add (new ASN1 (0x30));
  191. // TBSCertificate / Validity / Time
  192. validity.Add (ASN1Convert.FromDateTime (notBefore));
  193. // TBSCertificate / Validity / Time
  194. validity.Add (ASN1Convert.FromDateTime (notAfter));
  195. // TBSCertificate / Name
  196. tbsCert.Add (X501.FromString (subject));
  197. // TBSCertificate / SubjectPublicKeyInfo
  198. tbsCert.Add (SubjectPublicKeyInfo ());
  199. if (version > 1) {
  200. // TBSCertificate / [1] IMPLICIT UniqueIdentifier OPTIONAL
  201. if (issuerUniqueID != null)
  202. tbsCert.Add (new ASN1 (0xA1, UniqueIdentifier (issuerUniqueID)));
  203. // TBSCertificate / [2] IMPLICIT UniqueIdentifier OPTIONAL
  204. if (subjectUniqueID != null)
  205. tbsCert.Add (new ASN1 (0xA1, UniqueIdentifier (subjectUniqueID)));
  206. // TBSCertificate / [3] Extensions OPTIONAL
  207. if ((version > 2) && (extensions.Count > 0))
  208. tbsCert.Add (new ASN1 (0xA3, extensions.GetBytes ()));
  209. }
  210. return tbsCert;
  211. }
  212. }
  213. }