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