certificate.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // from https://github.com/Rantanen/node-dtls/blob/25a7dc861bda38cfeac93a723500eea4f0ac2e86/Certificate.js
  2. // thanks to @Rantanen
  3. 'use strict'
  4. var asn = require('asn1.js')
  5. var Time = asn.define('Time', function () {
  6. this.choice({
  7. utcTime: this.utctime(),
  8. generalTime: this.gentime()
  9. })
  10. })
  11. var AttributeTypeValue = asn.define('AttributeTypeValue', function () {
  12. this.seq().obj(
  13. this.key('type').objid(),
  14. this.key('value').any()
  15. )
  16. })
  17. var AlgorithmIdentifier = asn.define('AlgorithmIdentifier', function () {
  18. this.seq().obj(
  19. this.key('algorithm').objid(),
  20. this.key('parameters').optional(),
  21. this.key('curve').objid().optional()
  22. )
  23. })
  24. var SubjectPublicKeyInfo = asn.define('SubjectPublicKeyInfo', function () {
  25. this.seq().obj(
  26. this.key('algorithm').use(AlgorithmIdentifier),
  27. this.key('subjectPublicKey').bitstr()
  28. )
  29. })
  30. var RelativeDistinguishedName = asn.define('RelativeDistinguishedName', function () {
  31. this.setof(AttributeTypeValue)
  32. })
  33. var RDNSequence = asn.define('RDNSequence', function () {
  34. this.seqof(RelativeDistinguishedName)
  35. })
  36. var Name = asn.define('Name', function () {
  37. this.choice({
  38. rdnSequence: this.use(RDNSequence)
  39. })
  40. })
  41. var Validity = asn.define('Validity', function () {
  42. this.seq().obj(
  43. this.key('notBefore').use(Time),
  44. this.key('notAfter').use(Time)
  45. )
  46. })
  47. var Extension = asn.define('Extension', function () {
  48. this.seq().obj(
  49. this.key('extnID').objid(),
  50. this.key('critical').bool().def(false),
  51. this.key('extnValue').octstr()
  52. )
  53. })
  54. var TBSCertificate = asn.define('TBSCertificate', function () {
  55. this.seq().obj(
  56. this.key('version').explicit(0).int().optional(),
  57. this.key('serialNumber').int(),
  58. this.key('signature').use(AlgorithmIdentifier),
  59. this.key('issuer').use(Name),
  60. this.key('validity').use(Validity),
  61. this.key('subject').use(Name),
  62. this.key('subjectPublicKeyInfo').use(SubjectPublicKeyInfo),
  63. this.key('issuerUniqueID').implicit(1).bitstr().optional(),
  64. this.key('subjectUniqueID').implicit(2).bitstr().optional(),
  65. this.key('extensions').explicit(3).seqof(Extension).optional()
  66. )
  67. })
  68. var X509Certificate = asn.define('X509Certificate', function () {
  69. this.seq().obj(
  70. this.key('tbsCertificate').use(TBSCertificate),
  71. this.key('signatureAlgorithm').use(AlgorithmIdentifier),
  72. this.key('signatureValue').bitstr()
  73. )
  74. })
  75. module.exports = X509Certificate