-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathes6.js
More file actions
447 lines (397 loc) · 15.2 KB
/
Copy pathes6.js
File metadata and controls
447 lines (397 loc) · 15.2 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* eslint-disable no-undef,no-unreachable */
import * as asn1js from "asn1js";
import { arrayBufferToString, stringToArrayBuffer, toBase64, fromBase64, isEqualBuffer } from "pvutils";
import { getCrypto, getAlgorithmParameters, setEngine } from "../../src/common.js";
import { formatPEM } from "../../examples/examples_common.js";
import Certificate from "../../src/Certificate.js";
import EnvelopedData from "../../src/EnvelopedData.js";
import ContentInfo from "../../src/ContentInfo.js";
import AttributeTypeAndValue from "../../src/AttributeTypeAndValue.js";
import BasicConstraints from "../../src/BasicConstraints.js";
import Extension from "../../src/Extension.js";
import OriginatorInfo from "../../src/OriginatorInfo.js";
import CertificateSet from "../../src/CertificateSet.js";
//<nodewebcryptoossl>
//*********************************************************************************
let certificateBuffer = new ArrayBuffer(0); // ArrayBuffer with loaded or created CERT
let privateKeyBuffer = new ArrayBuffer(0);
let cmsEnvelopedBuffer = new ArrayBuffer(0);
let valueBuffer = new ArrayBuffer(0);
let trustedCertificates = []; // Array of root certificates from "CA Bundle"
let hashAlg = "SHA-1";
let signAlg = "RSASSA-PKCS1-v1_5";
let oaepHashAlg = "SHA-1";
const encAlg = {
name: "AES-CBC",
length: 128
};
//*********************************************************************************
//region Create CERT
//*********************************************************************************
function createCertificateInternal() {
//region Initial variables
let sequence = Promise.resolve();
const certificate = new Certificate();
let publicKey;
let privateKey;
trustedCertificates = [];
//endregion
//region Get a "crypto" extension
const crypto = getCrypto();
if (typeof crypto === "undefined")
return Promise.reject("No WebCrypto extension found");
//endregion
//region Put a static values
certificate.version = 2;
certificate.serialNumber = new asn1js.Integer({ value: 1 });
certificate.issuer.relativeDistinguishedNames.push(new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.6", // Country name
value: new asn1js.PrintableString({ value: "RU" })
})
]
}));
certificate.issuer.relativeDistinguishedNames.push(new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.3", // Common name
value: new asn1js.BmpString({ value: "Test" })
})
]
}));
certificate.subject.relativeDistinguishedNames.push(new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.6", // Country name
value: new asn1js.PrintableString({ value: "RU" })
})
]
}));
certificate.subject.relativeDistinguishedNames.push(new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.3", // Common name
value: new asn1js.BmpString({ value: "Test" })
})
]
}));
certificate.notBefore.value = new Date(2016, 1, 1);
certificate.notAfter.value = new Date(2019, 1, 1);
certificate.extensions = []; // Extensions are not a part of certificate by default, it's an optional array
//region "BasicConstraints" extension
const basicConstr = new BasicConstraints({
cA: true,
pathLenConstraint: 3
});
certificate.extensions.push(new Extension({
extnID: "2.5.29.19",
critical: true,
extnValue: basicConstr.toSchema().toBER(false),
parsedValue: basicConstr // Parsed value for well-known extensions
}));
//endregion
//region "KeyUsage" extension
const bitArray = new ArrayBuffer(1);
const bitView = new Uint8Array(bitArray);
bitView[0] |= 0x02; // Key usage "cRLSign" flag
bitView[0] |= 0x04; // Key usage "keyCertSign" flag
const keyUsage = new asn1js.BitString({ valueHex: bitArray });
certificate.extensions.push(new Extension({
extnID: "2.5.29.15",
critical: false,
extnValue: keyUsage.toBER(false),
parsedValue: keyUsage // Parsed value for well-known extensions
}));
//endregion
//endregion
//region Create a new key pair
sequence = sequence.then(() => {
//region Get default algorithm parameters for key generation
const algorithm = getAlgorithmParameters(signAlg, "generatekey");
if ("hash" in algorithm.algorithm)
algorithm.algorithm.hash.name = hashAlg;
//endregion
return crypto.generateKey(algorithm.algorithm, true, algorithm.usages);
});
//endregion
//region Store new key in an interim variables
sequence = sequence.then(keyPair => {
publicKey = keyPair.publicKey;
privateKey = keyPair.privateKey;
}, error => Promise.reject(`Error during key generation: ${error}`));
//endregion
//region Exporting public key into "subjectPublicKeyInfo" value of certificate
sequence = sequence.then(() =>
certificate.subjectPublicKeyInfo.importKey(publicKey)
);
//endregion
//region Signing final certificate
sequence = sequence.then(() =>
certificate.sign(privateKey, hashAlg),
error => Promise.reject(`Error during exporting public key: ${error}`));
//endregion
//region Encode and store certificate
sequence = sequence.then(() => {
trustedCertificates.push(certificate);
certificateBuffer = certificate.toSchema(true).toBER(false);
}, error => Promise.reject(`Error during signing: ${error}`));
//endregion
//region Exporting private key
sequence = sequence.then(() =>
crypto.exportKey("pkcs8", privateKey)
);
//endregion
//region Store exported key on Web page
sequence = sequence.then(result => {
privateKeyBuffer = result;
}, error => Promise.reject(`Error during exporting of private key: ${error}`));
//endregion
return sequence;
}
//*********************************************************************************
function createCertificate() {
return createCertificateInternal().then(() => {
const certificateString = String.fromCharCode.apply(null, new Uint8Array(certificateBuffer));
let resultString = "-----BEGIN CERTIFICATE-----\r\n";
resultString = `${resultString}${formatPEM(toBase64(certificateString))}`;
resultString = `${resultString}\r\n-----END CERTIFICATE-----\r\n`;
// noinspection InnerHTMLJS
document.getElementById("new_signed_data").innerHTML = resultString;
alert("Certificate created successfully!");
const privateKeyString = String.fromCharCode.apply(null, new Uint8Array(privateKeyBuffer));
resultString = "-----BEGIN PRIVATE KEY-----\r\n";
resultString = `${resultString}${formatPEM(toBase64(privateKeyString))}`;
resultString = `${resultString}\r\n-----END PRIVATE KEY-----\r\n`;
// noinspection InnerHTMLJS
document.getElementById("pkcs8_key").innerHTML = resultString;
alert("Private key exported successfully!");
}, error => {
if (error instanceof Object)
alert(error.message);
else
alert(error);
});
}
//*********************************************************************************
//endregion
//*********************************************************************************
//region Encrypt input data
//*********************************************************************************
function envelopedEncryptInternal() {
//region Decode input certificate
const asn1 = asn1js.fromBER(certificateBuffer);
const certSimpl = new Certificate({ schema: asn1.result });
//endregion
const cmsEnveloped = new EnvelopedData({
originatorInfo: new OriginatorInfo({
certs: new CertificateSet({
certificates: [certSimpl]
})
})
});
cmsEnveloped.addRecipientByCertificate(certSimpl, { oaepHashAlgorithm: oaepHashAlg });
return cmsEnveloped.encrypt(encAlg, valueBuffer).
then(
() => {
const cmsContentSimpl = new ContentInfo();
cmsContentSimpl.contentType = "1.2.840.113549.1.7.3";
cmsContentSimpl.content = cmsEnveloped.toSchema();
cmsEnvelopedBuffer = cmsContentSimpl.toSchema().toBER(false);
},
error => Promise.reject(`ERROR DURING ENCRYPTION PROCESS: ${error}`)
);
}
//*********************************************************************************
function envelopedEncrypt() {
return Promise.resolve().then(() => {
// noinspection InnerHTMLJS
const encodedCertificate = document.getElementById("new_signed_data").value;
const clearEncodedCertificate = encodedCertificate.replace(/(-----(BEGIN|END)( NEW)? CERTIFICATE-----|\n)/g, "");
certificateBuffer = stringToArrayBuffer(fromBase64(clearEncodedCertificate));
valueBuffer = stringToArrayBuffer(document.getElementById("content").value);
}).then(() => envelopedEncryptInternal()).then(() => {
let resultString = "-----BEGIN CMS-----\r\n";
resultString = `${resultString}${formatPEM(toBase64(arrayBufferToString(cmsEnvelopedBuffer)))}`;
resultString = `${resultString}\r\n-----END CMS-----\r\n`;
// noinspection InnerHTMLJS
document.getElementById("encrypted_content").innerHTML = resultString;
alert("Encryption process finished successfully");
});
}
//*********************************************************************************
//endregion
//*********************************************************************************
//region Decrypt input data
//*********************************************************************************
function envelopedDecryptInternal() {
//region Decode input certificate
let asn1 = asn1js.fromBER(certificateBuffer);
const certSimpl = new Certificate({ schema: asn1.result });
//endregion
//region Decode CMS Enveloped content
asn1 = asn1js.fromBER(cmsEnvelopedBuffer);
const cmsContentSimpl = new ContentInfo({ schema: asn1.result });
const cmsEnvelopedSimp = new EnvelopedData({ schema: cmsContentSimpl.content });
//endregion
return cmsEnvelopedSimp.decrypt(0,
{
recipientCertificate: certSimpl,
recipientPrivateKey: privateKeyBuffer
}).then(
result => result,
error => Promise.reject(`ERROR DURING DECRYPTION PROCESS: ${error}`)
);
}
//*********************************************************************************
function envelopedDecrypt() {
return Promise.resolve().then(() => {
// noinspection InnerHTMLJS
const encodedCertificate = document.getElementById("new_signed_data").value;
const clearEncodedCertificate = encodedCertificate.replace(/(-----(BEGIN|END)( NEW)? CERTIFICATE-----|\n)/g, "");
certificateBuffer = stringToArrayBuffer(window.atob(clearEncodedCertificate));
// noinspection InnerHTMLJS
const encodedPrivateKey = document.getElementById("pkcs8_key").value;
const clearPrivateKey = encodedPrivateKey.replace(/(-----(BEGIN|END)( NEW)? PRIVATE KEY-----|\n)/g, "");
privateKeyBuffer = stringToArrayBuffer(window.atob(clearPrivateKey));
// noinspection InnerHTMLJS
const encodedCMSEnveloped = document.getElementById("encrypted_content").value;
const clearEncodedCMSEnveloped = encodedCMSEnveloped.replace(/(-----(BEGIN|END)( NEW)? CMS-----|\n)/g, "");
cmsEnvelopedBuffer = stringToArrayBuffer(window.atob(clearEncodedCMSEnveloped));
}).then(() => envelopedDecryptInternal()).then(result => {
// noinspection InnerHTMLJS
document.getElementById("decrypted_content").innerHTML = arrayBufferToString(result);
});
}
//*********************************************************************************
//endregion
//*********************************************************************************
function handleHashAlgOnChange() {
const hashOption = document.getElementById("hash_alg").value;
switch (hashOption) {
case "alg_SHA1":
hashAlg = "sha-1";
break;
case "alg_SHA256":
hashAlg = "sha-256";
break;
case "alg_SHA384":
hashAlg = "sha-384";
break;
case "alg_SHA512":
hashAlg = "sha-512";
break;
default:
}
}
//*********************************************************************************
function handleSignAlgOnChange() {
const signOption = document.getElementById("sign_alg").value;
switch (signOption) {
case "alg_RSA15":
signAlg = "RSASSA-PKCS1-V1_5";
break;
case "alg_RSA2":
signAlg = "RSA-PSS";
break;
case "alg_ECDSA":
signAlg = "ECDSA";
break;
default:
}
}
//*********************************************************************************
function handleEncAlgOnChange() {
const encryptionAlgorithmSelect = document.getElementById("content_enc_alg").value;
switch (encryptionAlgorithmSelect) {
case "alg_CBC":
encAlg.name = "AES-CBC";
break;
case "alg_GCM":
encAlg.name = "AES-GCM";
break;
default:
}
}
//*********************************************************************************
function handleEncLenOnChange() {
const encryptionAlgorithmLengthSelect = document.getElementById("content_enc_alg_len").value;
switch (encryptionAlgorithmLengthSelect) {
case "len_128":
encAlg.length = 128;
break;
case "len_192":
encAlg.length = 192;
break;
case "len_256":
encAlg.length = 256;
break;
default:
}
}
//*********************************************************************************
function handleOAEPHashAlgOnChange() {
const hashOption = document.getElementById("oaep_hash_alg").value;
switch (hashOption) {
case "alg_SHA1":
oaepHashAlg = "sha-1";
break;
case "alg_SHA256":
oaepHashAlg = "sha-256";
break;
case "alg_SHA384":
oaepHashAlg = "sha-384";
break;
case "alg_SHA512":
oaepHashAlg = "sha-512";
break;
default:
}
}
//*********************************************************************************
context("Hack for Rollup.js", () => {
return;
// noinspection UnreachableCodeJS
createCertificate();
envelopedEncrypt();
envelopedDecrypt();
handleHashAlgOnChange();
handleSignAlgOnChange();
handleEncAlgOnChange();
handleEncLenOnChange();
handleOAEPHashAlgOnChange();
setEngine();
});
//*********************************************************************************
context("How To Encrypt CMS via Certificate", () => {
//region Initial variables
const hashAlgs = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
const oaepHashAlgs = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
const signAlgs = ["RSASSA-PKCS1-V1_5", "ECDSA", "RSA-PSS"];
const encAlgs = ["AES-CBC", "AES-GCM"];
const encLens = [128, 192, 256];
valueBuffer = (new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09])).buffer;
//endregion
encAlgs.forEach(_encAlg => {
encLens.forEach(_encLen => {
signAlgs.forEach(_signAlg => {
hashAlgs.forEach(_hashAlg => {
oaepHashAlgs.forEach(_oaepHashAlg => {
const testName = `${_encAlg} with ${_encLen}, ${_hashAlg} + ${_signAlg}, OAEP hash: ${_oaepHashAlg}`;
it(testName, () => {
hashAlg = _hashAlg;
signAlg = _signAlg;
oaepHashAlg = _oaepHashAlg;
encAlg.name = _encAlg;
encAlg.length = _encLen;
return createCertificateInternal().then(() => envelopedEncryptInternal()).then(() => envelopedDecryptInternal()).then(result => {
assert.equal(isEqualBuffer(result, valueBuffer), true, "Decrypted value must be equal with initially encrypted value");
});
});
});
});
});
});
});
});
//*********************************************************************************