This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_cms.js
189 lines (163 loc) · 6.17 KB
/
_cms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
// This is a proof of concept. The code below is ugly, inefficient and has no tests.
const {pemToDer} = require('./_asn1_utils');
const asn1js = require('asn1js');
const bufferToArrayBuffer = require('buffer-to-arraybuffer');
const fs = require('fs');
const pkijs = require('pkijs');
const ENCRYPTION_ALGO = {name: 'AES-CBC', length: 128};
const OID_CMS_ENVELOPED_DATA = '1.2.840.113549.1.7.3';
const OID_CMS_SIGNED_DATA = "1.2.840.113549.1.7.2";
/**
* @param {Buffer} plaintext
* @param {string} certPath
* @returns {Promise<Buffer>} The ciphertext
*/
async function encrypt(plaintext, certPath) {
const cmsEnveloped = new pkijs.EnvelopedData();
const certificate = parseCertificate(pemToDer(fs.readFileSync(certPath)));
cmsEnveloped.addRecipientByCertificate(certificate, {oaepHashAlgorithm: 'SHA-256'});
await cmsEnveloped.encrypt(ENCRYPTION_ALGO, bufferToArrayBuffer(plaintext));
const cmsContentInfo = new pkijs.ContentInfo({
contentType: OID_CMS_ENVELOPED_DATA,
content: cmsEnveloped.toSchema(),
});
return Buffer.from(cmsContentInfo.toSchema().toBER(false));
}
/**
* @param {Buffer} ciphertext
* @param {string} keyPath
* @returns {Promise<Buffer>} The plaintext
*/
async function decrypt(ciphertext, keyPath) {
const asn1 = asn1js.fromBER(bufferToArrayBuffer(ciphertext));
const cmsContentInfo = new pkijs.ContentInfo({schema: asn1.result});
const cmsEnvelopedSimp = new pkijs.EnvelopedData({schema: cmsContentInfo.content});
const privateKeyBuffer = bufferToArrayBuffer(pemToDer(fs.readFileSync(keyPath)));
const plaintext = await cmsEnvelopedSimp.decrypt(
0,
{recipientPrivateKey: privateKeyBuffer},
);
return Buffer.from(plaintext);
}
/**
* @param {Buffer} plaintext
* @param {string|Buffer} keyPem PEM-encoded private key (or the path to it)
* @param {Buffer} certPem
* @param {string} hashAlgorithm
* @param {boolean} embedCert Whether to embed the DER-encoded form of `certPem` in the signature.
* @returns {Promise<Buffer>} The signature (detached)
*/
async function sign(plaintext, keyPem, certPem, hashAlgorithm, embedCert = false) {
hashAlgorithm = hashAlgorithm === 'sha256' ? 'SHA-256' : hashAlgorithm;
if (hashAlgorithm !== 'SHA-256') {
throw new Error(`Unsupported hash ${hashAlgorithm}`);
}
const certificate = parseCertificate(pemToDer(certPem));
const crypto = pkijs.getCrypto();
// Add signed attributes
const digest = await crypto.digest({name: hashAlgorithm}, new Uint8Array(plaintext));
const signerInfo = new pkijs.SignerInfo({
version: 1,
sid: new pkijs.IssuerAndSerialNumber({
issuer: certificate.issuer,
serialNumber: certificate.serialNumber,
}),
signedAttrs: new pkijs.SignedAndUnsignedAttributes({
type: 0,
attributes: [
new pkijs.Attribute({
type: "1.2.840.113549.1.9.3", // Content type
values: [
new asn1js.ObjectIdentifier({value: "1.2.840.113549.1.7.1"})
]
}),
new pkijs.Attribute({
type: "1.2.840.113549.1.9.4", // Message digest
values: [
new asn1js.OctetString({valueHex: digest})
]
})
],
}),
});
const signedDataParams = {
version: 1,
encapContentInfo: new pkijs.EncapsulatedContentInfo({
eContentType: "1.2.840.113549.1.7.1", // "data" content type
}),
signerInfos: [signerInfo],
};
if (embedCert) {
signedDataParams.certificates = [certificate];
}
const cmsSigned = new pkijs.SignedData(signedDataParams);
const keyAlgorithmParams = await pkijs.getAlgorithmParameters('RSA-PSS', 'importkey');
keyAlgorithmParams.algorithm.hash.name = hashAlgorithm;
keyPem = (typeof keyPem === 'string') ? fs.readFileSync(keyPem) : keyPem;
const privateKey = await crypto.importKey(
"pkcs8",
bufferToArrayBuffer(pemToDer(keyPem)),
keyAlgorithmParams.algorithm,
true,
["sign"],
);
await cmsSigned.sign(privateKey, 0, hashAlgorithm, bufferToArrayBuffer(plaintext));
const cmsContentSimp = new pkijs.ContentInfo({
contentType: OID_CMS_SIGNED_DATA,
content: cmsSigned.toSchema(true)
});
const signatureWrapped = cmsContentSimp.toSchema().toBER(false);
return Buffer.from(signatureWrapped);
}
/**
* @param {Buffer} plaintext
* @param {Buffer} signature
* @param {Buffer|null} certDer
* @returns {Promise<void|Buffer>} The certificate embedded in the signature if `certDer` is absent
* @throws Error If the signature doesn't match
*/
async function verifySignature(plaintext, signature, certDer = null) {
const asn1 = asn1js.fromBER(bufferToArrayBuffer(signature));
if (asn1.offset === (-1)) {
throw new Error('Invalid signature serialisation');
}
const cmsContentSimpl = new pkijs.ContentInfo({schema: asn1.result});
const signedDataParams = {schema: cmsContentSimpl.content};
let certificate;
if (certDer) {
certificate = parseCertificate(certDer);
signedDataParams.certificates = [certificate];
}
const cmsSignedSimpl = new pkijs.SignedData(signedDataParams);
const verificationParameters = {
signer: 0,
data: bufferToArrayBuffer(plaintext),
extendedMode: true,
};
const {signatureVerified, code} = await cmsSignedSimpl.verify(verificationParameters);
if (!signatureVerified) {
throw new Error(`Invalid signature (code: ${code})`);
}
if (!certDer) {
certificate = cmsSignedSimpl.certificates[0];
return Buffer.from(certificate.toSchema().toBER(false));
}
}
/**
* @param {Buffer} certBufferDer
* @returns {Certificate}
*/
function parseCertificate(certBufferDer) {
const asn1 = asn1js.fromBER(bufferToArrayBuffer(certBufferDer));
if (asn1.offset === (-1)) {
throw new Error('Invalid cert');
}
return new pkijs.Certificate({schema: asn1.result});
}
module.exports = {
encrypt,
decrypt,
sign,
verifySignature,
};