x509.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2014-2015 Sylvain Peyrefitte
  3. *
  4. * This file is part of node-rdpjs.
  5. *
  6. * node-rdpjs is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. // https://tools.ietf.org/html/rfc5280
  20. var asn1 = require('../asn1');
  21. /**
  22. * @see https://tools.ietf.org/html/rfc5280 page 20
  23. * @returns {asn1.univ.Choice}
  24. */
  25. function DirectoryString() {
  26. return new asn1.univ.Choice({
  27. teletexString : new asn1.univ.T61String(),
  28. printableString : new asn1.univ.PrintableString(),
  29. universalString : new asn1.univ.UniversalString(),
  30. utf8String : new asn1.univ.UTF8String(),
  31. bmpString : new asn1.univ.BMPString(),
  32. ia5String : new asn1.univ.IA5String()
  33. });
  34. }
  35. /**
  36. * https://tools.ietf.org/html/rfc5280 page 20
  37. * @returns {asn1.univ.Choice}
  38. */
  39. function AttributeValue() {
  40. return DirectoryString();
  41. }
  42. /**
  43. * @see https://tools.ietf.org/html/rfc5280 page 20
  44. * @returns {asn1.univ.ObjectIdentifier}
  45. */
  46. function AttributeType() {
  47. return new asn1.univ.ObjectIdentifier();
  48. }
  49. /**
  50. * @see https://tools.ietf.org/html/rfc5280 page 20
  51. * @returns {asn1.univ.Sequence}
  52. */
  53. function AttributeTypeAndValue() {
  54. return new asn1.univ.Sequence({
  55. type : AttributeType(),
  56. value : AttributeValue()
  57. });
  58. }
  59. /**
  60. * https://tools.ietf.org/html/rfc5280 page 116
  61. * @returns {asn1.univ.SetOf}
  62. */
  63. function RelativeDistinguishedName() {
  64. return new asn1.univ.SetOf(AttributeTypeAndValue);
  65. }
  66. /**
  67. * https://tools.ietf.org/html/rfc5280 page 116
  68. * @returns {asn1.univ.SequenceOf}
  69. */
  70. function RDNSequence() {
  71. return new asn1.univ.SequenceOf(RelativeDistinguishedName);
  72. }
  73. /**
  74. * @see https://tools.ietf.org/html/rfc5280 page 116
  75. * @returns {asn1.univ.Choice}
  76. */
  77. function Name() {
  78. return new asn1.univ.Choice({
  79. rdnSequence : RDNSequence()
  80. });
  81. }
  82. /**
  83. * @see https://tools.ietf.org/html/rfc5280 page 18
  84. * @returns {asn1.univ.Sequence}
  85. */
  86. function AlgorithmIdentifier() {
  87. return new asn1.univ.Sequence({
  88. algorithm : new asn1.univ.ObjectIdentifier(),
  89. parameters : new asn1.univ.Null()
  90. });
  91. }
  92. /**
  93. * @see https://tools.ietf.org/html/rfc5280 page 117
  94. * @returns {asn1.univ.Sequence}
  95. */
  96. function Extension() {
  97. return new asn1.univ.Sequence({
  98. extnID : new asn1.univ.ObjectIdentifier(),
  99. critical : new asn1.univ.Boolean(),
  100. extnValue : new asn1.univ.OctetString()
  101. });
  102. }
  103. /**
  104. * @see https://tools.ietf.org/html/rfc5280 page 117
  105. * @returns {asn1.univ.SequenceOf}
  106. */
  107. function Extensions() {
  108. return new asn1.univ.SequenceOf(Extension);
  109. }
  110. /**
  111. * @see https://tools.ietf.org/html/rfc5280 page 117
  112. * @returns {asn1.univ.Choice}
  113. */
  114. function Time() {
  115. return new asn1.univ.Choice({
  116. utcTime : new asn1.univ.UTCTime(),
  117. generalTime : new asn1.univ.GeneralizedTime()
  118. });
  119. }
  120. /**
  121. * @see https://tools.ietf.org/html/rfc5280 page 117
  122. * @returns {asn1.univ.Sequence}
  123. */
  124. function Validity() {
  125. return new asn1.univ.Sequence({
  126. notBefore : Time(),
  127. notAfter : Time()
  128. });
  129. }
  130. /**
  131. * @see https://tools.ietf.org/html/rfc5280 page 117
  132. * @returns {asn1.univ.Integer}
  133. */
  134. function CertificateSerialNumber() {
  135. return new asn1.univ.Integer();
  136. }
  137. /**
  138. * @see https://tools.ietf.org/html/rfc5280 page 117
  139. * @returns {asn1.univ.Sequence}
  140. */
  141. function SubjectPublicKeyInfo() {
  142. return new asn1.univ.Sequence({
  143. algorithm : AlgorithmIdentifier(),
  144. subjectPublicKey : new asn1.univ.BitString()
  145. });
  146. }
  147. /**
  148. * @see https://tools.ietf.org/html/rfc5280 page 117
  149. * @returns {asn1.univ.BitString}
  150. */
  151. function UniqueIdentifier() {
  152. return new asn1.univ.BitString();
  153. }
  154. /**
  155. * @see https://tools.ietf.org/html/rfc5280 page 117
  156. * @returns {asn1.univ.Sequence}
  157. */
  158. function TbsCertificate() {
  159. return new asn1.univ.Sequence({
  160. version : CertificateSerialNumber().explicitTag(new asn1.spec.Asn1Tag(asn1.spec.TagClass.Context, asn1.spec.TagFormat.Constructed, 0)),
  161. serialNumber : new asn1.univ.Integer(),
  162. signature : AlgorithmIdentifier(),
  163. issuer : Name(),
  164. validity : Validity(),
  165. subject : Name(),
  166. subjectPublicKeyInfo : SubjectPublicKeyInfo(),
  167. issuerUniqueID : UniqueIdentifier().implicitTag(asn1.spec.TagClass.Context, asn1.spec.TagFormat.Primitive, 1).optional(),
  168. subjectUniqueID : UniqueIdentifier().implicitTag(asn1.spec.TagClass.Context, asn1.spec.TagFormat.Primitive, 2).optional(),
  169. extensions : Extensions().implicitTag(asn1.spec.TagClass.Context, asn1.spec.TagFormat.Primitive, 3).optional()
  170. });
  171. }
  172. /**
  173. * @see https://tools.ietf.org/html/rfc5280 page 117
  174. * @returns {asn1.univ.Sequence}
  175. */
  176. function X509Certificate() {
  177. return new asn1.univ.Sequence({
  178. tbsCertificate : TbsCertificate(),
  179. signatureAlgorithm : AlgorithmIdentifier(),
  180. signatureValue : new asn1.univ.BitString()
  181. });
  182. }
  183. function RSAPublicKey() {
  184. return new asn1.univ.Sequence({
  185. modulus : new asn1.univ.Integer(),
  186. publicExponent : new asn1.univ.Integer()
  187. });
  188. }
  189. /**
  190. * Module Export
  191. */
  192. module.exports = {
  193. X509Certificate : X509Certificate,
  194. RSAPublicKey : RSAPublicKey
  195. };