X509CertificateBuilder.cs 7.5 KB

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