UI for Zipcoin Blue

index.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var asn1 = require('./asn1')
  2. var aesid = require('./aesid.json')
  3. var fixProc = require('./fixProc')
  4. var ciphers = require('browserify-aes')
  5. var compat = require('pbkdf2')
  6. module.exports = parseKeys
  7. function parseKeys (buffer) {
  8. var password
  9. if (typeof buffer === 'object' && !Buffer.isBuffer(buffer)) {
  10. password = buffer.passphrase
  11. buffer = buffer.key
  12. }
  13. if (typeof buffer === 'string') {
  14. buffer = new Buffer(buffer)
  15. }
  16. var stripped = fixProc(buffer, password)
  17. var type = stripped.tag
  18. var data = stripped.data
  19. var subtype, ndata
  20. switch (type) {
  21. case 'CERTIFICATE':
  22. ndata = asn1.certificate.decode(data, 'der').tbsCertificate.subjectPublicKeyInfo
  23. // falls through
  24. case 'PUBLIC KEY':
  25. if (!ndata) {
  26. ndata = asn1.PublicKey.decode(data, 'der')
  27. }
  28. subtype = ndata.algorithm.algorithm.join('.')
  29. switch (subtype) {
  30. case '1.2.840.113549.1.1.1':
  31. return asn1.RSAPublicKey.decode(ndata.subjectPublicKey.data, 'der')
  32. case '1.2.840.10045.2.1':
  33. ndata.subjectPrivateKey = ndata.subjectPublicKey
  34. return {
  35. type: 'ec',
  36. data: ndata
  37. }
  38. case '1.2.840.10040.4.1':
  39. ndata.algorithm.params.pub_key = asn1.DSAparam.decode(ndata.subjectPublicKey.data, 'der')
  40. return {
  41. type: 'dsa',
  42. data: ndata.algorithm.params
  43. }
  44. default: throw new Error('unknown key id ' + subtype)
  45. }
  46. throw new Error('unknown key type ' + type)
  47. case 'ENCRYPTED PRIVATE KEY':
  48. data = asn1.EncryptedPrivateKey.decode(data, 'der')
  49. data = decrypt(data, password)
  50. // falls through
  51. case 'PRIVATE KEY':
  52. ndata = asn1.PrivateKey.decode(data, 'der')
  53. subtype = ndata.algorithm.algorithm.join('.')
  54. switch (subtype) {
  55. case '1.2.840.113549.1.1.1':
  56. return asn1.RSAPrivateKey.decode(ndata.subjectPrivateKey, 'der')
  57. case '1.2.840.10045.2.1':
  58. return {
  59. curve: ndata.algorithm.curve,
  60. privateKey: asn1.ECPrivateKey.decode(ndata.subjectPrivateKey, 'der').privateKey
  61. }
  62. case '1.2.840.10040.4.1':
  63. ndata.algorithm.params.priv_key = asn1.DSAparam.decode(ndata.subjectPrivateKey, 'der')
  64. return {
  65. type: 'dsa',
  66. params: ndata.algorithm.params
  67. }
  68. default: throw new Error('unknown key id ' + subtype)
  69. }
  70. throw new Error('unknown key type ' + type)
  71. case 'RSA PUBLIC KEY':
  72. return asn1.RSAPublicKey.decode(data, 'der')
  73. case 'RSA PRIVATE KEY':
  74. return asn1.RSAPrivateKey.decode(data, 'der')
  75. case 'DSA PRIVATE KEY':
  76. return {
  77. type: 'dsa',
  78. params: asn1.DSAPrivateKey.decode(data, 'der')
  79. }
  80. case 'EC PRIVATE KEY':
  81. data = asn1.ECPrivateKey.decode(data, 'der')
  82. return {
  83. curve: data.parameters.value,
  84. privateKey: data.privateKey
  85. }
  86. default: throw new Error('unknown key type ' + type)
  87. }
  88. }
  89. parseKeys.signature = asn1.signature
  90. function decrypt (data, password) {
  91. var salt = data.algorithm.decrypt.kde.kdeparams.salt
  92. var iters = parseInt(data.algorithm.decrypt.kde.kdeparams.iters.toString(), 10)
  93. var algo = aesid[data.algorithm.decrypt.cipher.algo.join('.')]
  94. var iv = data.algorithm.decrypt.cipher.iv
  95. var cipherText = data.subjectPrivateKey
  96. var keylen = parseInt(algo.split('-')[1], 10) / 8
  97. var key = compat.pbkdf2Sync(password, salt, iters, keylen)
  98. var cipher = ciphers.createDecipheriv(algo, key, iv)
  99. var out = []
  100. out.push(cipher.update(cipherText))
  101. out.push(cipher.final())
  102. return Buffer.concat(out)
  103. }