|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const async = require('async') |
| 4 | +const forge = require('node-forge') |
| 5 | +const util = require('./util') |
| 6 | + |
| 7 | +/** |
| 8 | + * Cryptographic Message Syntax (aka PKCS #7) |
| 9 | + * |
| 10 | + * CMS describes an encapsulation syntax for data protection. It |
| 11 | + * is used to digitally sign, digest, authenticate, or encrypt |
| 12 | + * arbitrary message content. |
| 13 | + * |
| 14 | + * See RFC 5652 for all the details. |
| 15 | + */ |
| 16 | +class CMS { |
| 17 | + /** |
| 18 | + * Creates a new instance with a keychain |
| 19 | + * |
| 20 | + * @param {Keychain} keychain - the available keys |
| 21 | + */ |
| 22 | + constructor (keychain) { |
| 23 | + if (!keychain) { |
| 24 | + throw new Error('keychain is required') |
| 25 | + } |
| 26 | + |
| 27 | + this.keychain = keychain |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Creates some protected data. |
| 32 | + * |
| 33 | + * The output Buffer contains the PKCS #7 message in DER. |
| 34 | + * |
| 35 | + * @param {string} name - The local key name. |
| 36 | + * @param {Buffer} plain - The data to encrypt. |
| 37 | + * @param {function(Error, Buffer)} callback |
| 38 | + * @returns {undefined} |
| 39 | + */ |
| 40 | + encrypt (name, plain, callback) { |
| 41 | + const self = this |
| 42 | + const done = (err, result) => async.setImmediate(() => callback(err, result)) |
| 43 | + |
| 44 | + if (!Buffer.isBuffer(plain)) { |
| 45 | + return done(new Error('Plain data must be a Buffer')) |
| 46 | + } |
| 47 | + |
| 48 | + async.series([ |
| 49 | + (cb) => self.keychain.findKeyByName(name, cb), |
| 50 | + (cb) => self.keychain._getPrivateKey(name, cb) |
| 51 | + ], (err, results) => { |
| 52 | + if (err) return done(err) |
| 53 | + |
| 54 | + let key = results[0] |
| 55 | + let pem = results[1] |
| 56 | + try { |
| 57 | + const privateKey = forge.pki.decryptRsaPrivateKey(pem, self.keychain._()) |
| 58 | + util.certificateForKey(key, privateKey, (err, certificate) => { |
| 59 | + if (err) return callback(err) |
| 60 | + |
| 61 | + // create a p7 enveloped message |
| 62 | + const p7 = forge.pkcs7.createEnvelopedData() |
| 63 | + p7.addRecipient(certificate) |
| 64 | + p7.content = forge.util.createBuffer(plain) |
| 65 | + p7.encrypt() |
| 66 | + |
| 67 | + // convert message to DER |
| 68 | + const der = forge.asn1.toDer(p7.toAsn1()).getBytes() |
| 69 | + done(null, Buffer.from(der, 'binary')) |
| 70 | + }) |
| 71 | + } catch (err) { |
| 72 | + done(err) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Reads some protected data. |
| 79 | + * |
| 80 | + * The keychain must contain one of the keys used to encrypt the data. If none of the keys |
| 81 | + * exists, an Error is returned with the property 'missingKeys'. It is array of key ids. |
| 82 | + * |
| 83 | + * @param {Buffer} cmsData - The CMS encrypted data to decrypt. |
| 84 | + * @param {function(Error, Buffer)} callback |
| 85 | + * @returns {undefined} |
| 86 | + */ |
| 87 | + decrypt (cmsData, callback) { |
| 88 | + const done = (err, result) => async.setImmediate(() => callback(err, result)) |
| 89 | + |
| 90 | + if (!Buffer.isBuffer(cmsData)) { |
| 91 | + return done(new Error('CMS data is required')) |
| 92 | + } |
| 93 | + |
| 94 | + const self = this |
| 95 | + let cms |
| 96 | + try { |
| 97 | + const buf = forge.util.createBuffer(cmsData.toString('binary')) |
| 98 | + const obj = forge.asn1.fromDer(buf) |
| 99 | + cms = forge.pkcs7.messageFromAsn1(obj) |
| 100 | + } catch (err) { |
| 101 | + return done(new Error('Invalid CMS: ' + err.message)) |
| 102 | + } |
| 103 | + |
| 104 | + // Find a recipient whose key we hold. We only deal with recipient certs |
| 105 | + // issued by ipfs (O=ipfs). |
| 106 | + const recipients = cms.recipients |
| 107 | + .filter(r => r.issuer.find(a => a.shortName === 'O' && a.value === 'ipfs')) |
| 108 | + .filter(r => r.issuer.find(a => a.shortName === 'CN')) |
| 109 | + .map(r => { |
| 110 | + return { |
| 111 | + recipient: r, |
| 112 | + keyId: r.issuer.find(a => a.shortName === 'CN').value |
| 113 | + } |
| 114 | + }) |
| 115 | + async.detect( |
| 116 | + recipients, |
| 117 | + (r, cb) => self.keychain.findKeyById(r.keyId, (err, info) => cb(null, !err && info)), |
| 118 | + (err, r) => { |
| 119 | + if (err) return done(err) |
| 120 | + if (!r) { |
| 121 | + const missingKeys = recipients.map(r => r.keyId) |
| 122 | + err = new Error('Decryption needs one of the key(s): ' + missingKeys.join(', ')) |
| 123 | + err.missingKeys = missingKeys |
| 124 | + return done(err) |
| 125 | + } |
| 126 | + |
| 127 | + async.waterfall([ |
| 128 | + (cb) => self.keychain.findKeyById(r.keyId, cb), |
| 129 | + (key, cb) => self.keychain._getPrivateKey(key.name, cb) |
| 130 | + ], (err, pem) => { |
| 131 | + if (err) return done(err) |
| 132 | + |
| 133 | + const privateKey = forge.pki.decryptRsaPrivateKey(pem, self.keychain._()) |
| 134 | + cms.decrypt(r.recipient, privateKey) |
| 135 | + done(null, Buffer.from(cms.content.getBytes(), 'binary')) |
| 136 | + }) |
| 137 | + } |
| 138 | + ) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = CMS |
0 commit comments