asn1.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // from https://github.com/indutny/self-signed/blob/gh-pages/lib/asn1.js
  2. // Fedor, you are amazing.
  3. 'use strict';
  4. var asn1 = require('asn1.js');
  5. exports.certificate = require('./certificate');
  6. var RSAPrivateKey = asn1.define('RSAPrivateKey', function () {
  7. this.seq().obj(
  8. this.key('version')['int'](),
  9. this.key('modulus')['int'](),
  10. this.key('publicExponent')['int'](),
  11. this.key('privateExponent')['int'](),
  12. this.key('prime1')['int'](),
  13. this.key('prime2')['int'](),
  14. this.key('exponent1')['int'](),
  15. this.key('exponent2')['int'](),
  16. this.key('coefficient')['int']()
  17. );
  18. });
  19. exports.RSAPrivateKey = RSAPrivateKey;
  20. var RSAPublicKey = asn1.define('RSAPublicKey', function () {
  21. this.seq().obj(
  22. this.key('modulus')['int'](),
  23. this.key('publicExponent')['int']()
  24. );
  25. });
  26. exports.RSAPublicKey = RSAPublicKey;
  27. var AlgorithmIdentifier = asn1.define('AlgorithmIdentifier', function () {
  28. this.seq().obj(
  29. this.key('algorithm').objid(),
  30. this.key('none').null_().optional(),
  31. this.key('curve').objid().optional(),
  32. this.key('params').seq().obj(
  33. this.key('p')['int'](),
  34. this.key('q')['int'](),
  35. this.key('g')['int']()
  36. ).optional()
  37. );
  38. });
  39. var PublicKey = asn1.define('SubjectPublicKeyInfo', function () {
  40. this.seq().obj(
  41. this.key('algorithm').use(AlgorithmIdentifier),
  42. this.key('subjectPublicKey').bitstr()
  43. );
  44. });
  45. exports.PublicKey = PublicKey;
  46. var PrivateKeyInfo = asn1.define('PrivateKeyInfo', function () {
  47. this.seq().obj(
  48. this.key('version')['int'](),
  49. this.key('algorithm').use(AlgorithmIdentifier),
  50. this.key('subjectPrivateKey').octstr()
  51. );
  52. });
  53. exports.PrivateKey = PrivateKeyInfo;
  54. var EncryptedPrivateKeyInfo = asn1.define('EncryptedPrivateKeyInfo', function () {
  55. this.seq().obj(
  56. this.key('algorithm').seq().obj(
  57. this.key('id').objid(),
  58. this.key('decrypt').seq().obj(
  59. this.key('kde').seq().obj(
  60. this.key('id').objid(),
  61. this.key('kdeparams').seq().obj(
  62. this.key('salt').octstr(),
  63. this.key('iters')['int']()
  64. )
  65. ),
  66. this.key('cipher').seq().obj(
  67. this.key('algo').objid(),
  68. this.key('iv').octstr()
  69. )
  70. )
  71. ),
  72. this.key('subjectPrivateKey').octstr()
  73. );
  74. });
  75. exports.EncryptedPrivateKey = EncryptedPrivateKeyInfo;
  76. var DSAPrivateKey = asn1.define('DSAPrivateKey', function () {
  77. this.seq().obj(
  78. this.key('version')['int'](),
  79. this.key('p')['int'](),
  80. this.key('q')['int'](),
  81. this.key('g')['int'](),
  82. this.key('pub_key')['int'](),
  83. this.key('priv_key')['int']()
  84. );
  85. });
  86. exports.DSAPrivateKey = DSAPrivateKey;
  87. exports.DSAparam = asn1.define('DSAparam', function () {
  88. this['int']();
  89. });
  90. var ECParameters = asn1.define('ECParameters', function () {
  91. this.choice({
  92. namedCurve: this.objid()
  93. });
  94. });
  95. var ECPrivateKey = asn1.define('ECPrivateKey', function () {
  96. this.seq().obj(
  97. this.key('version')['int'](),
  98. this.key('privateKey').octstr(),
  99. this.key('parameters').optional().explicit(0).use(ECParameters),
  100. this.key('publicKey').optional().explicit(1).bitstr()
  101. );
  102. });
  103. exports.ECPrivateKey = ECPrivateKey;
  104. exports.signature = asn1.define('signature', function () {
  105. this.seq().obj(
  106. this.key('r')['int'](),
  107. this.key('s')['int']()
  108. );
  109. });