@ne1410s/crypto 0.2.43 → 0.2.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,8 @@
18
18
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
19
  PERFORMANCE OF THIS SOFTWARE.
20
20
  ***************************************************************************** */
21
+ /* global Reflect, Promise */
22
+
21
23
 
22
24
  function __awaiter(thisArg, _arguments, P, generator) {
23
25
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -394,6 +396,7 @@
394
396
  /*!
395
397
  Copyright (c) Peculiar Ventures, LLC
396
398
  */
399
+
397
400
  function utilFromBase(inputBuffer, inputBase) {
398
401
  let result = 0;
399
402
  if (inputBuffer.length === 1) {
@@ -565,6 +568,7 @@
565
568
  *
566
569
  */
567
570
 
571
+
568
572
  function assertBigInt() {
569
573
  if (typeof BigInt === "undefined") {
570
574
  throw new Error("BigInt is not defined. Your environment doesn't implement BigInt.");
@@ -3282,333 +3286,333 @@
3282
3286
  })();
3283
3287
  TIME.NAME = "TIME";
3284
3288
 
3285
- /**
3286
- * Converts text to an array buffer.
3287
- * @param text The text.
3288
- * @returns An array buffer.
3289
- */
3290
- function textToBuffer(text) {
3291
- return Uint8Array.from(text.split(/(?=[\s\S])/u).map(function (c) { return c.charCodeAt(0); })).buffer;
3292
- }
3293
- /**
3294
- * Converts an array buffer to the corresponding text.
3295
- * @param buffer An array buffer.
3296
- * @returns The corresponding text.
3297
- */
3298
- function bufferToText(buffer) {
3299
- return String.fromCharCode.apply(null, new Uint8Array(buffer));
3300
- }
3301
- /**
3302
- * Encodes text as base 64.
3303
- * @param text The text.
3304
- * @returns The encoded base 64.
3305
- */
3306
- function textToBase64(text) {
3307
- return Buffer.from(text, 'binary').toString('base64');
3308
- }
3309
- /**
3310
- * Decodes base 64 to text.
3311
- * @param base64 The base 64.
3312
- * @returns The decoded text.
3313
- */
3314
- function base64ToText(base64) {
3315
- return Buffer.from(base64, 'base64').toString('binary');
3316
- }
3317
- /**
3318
- * Encodes an array buffer as base 64.
3319
- * @param buffer An array buffer.
3320
- * @returns The encoded base 64.
3321
- */
3322
- function bufferToBase64(buffer) {
3323
- return textToBase64(bufferToText(buffer));
3324
- }
3325
- /**
3326
- * Encodes an array buffer as base 64 url.
3327
- * @param buffer An array buffer.
3328
- * @returns The encoded base 64 url.
3329
- */
3330
- function bufferToBase64Url(buffer) {
3331
- return textToBase64Url(bufferToText(buffer));
3332
- }
3333
- /**
3334
- * Encodes text as base 64 url.
3335
- * @param text The text.
3336
- * @returns The encoded base 64 url.
3337
- */
3338
- function textToBase64Url(text) {
3339
- return textToBase64(text).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
3289
+ /**
3290
+ * Converts text to an array buffer.
3291
+ * @param text The text.
3292
+ * @returns An array buffer.
3293
+ */
3294
+ function textToBuffer(text) {
3295
+ return Uint8Array.from(text.split(/(?=[\s\S])/u).map(function (c) { return c.charCodeAt(0); })).buffer;
3296
+ }
3297
+ /**
3298
+ * Converts an array buffer to the corresponding text.
3299
+ * @param buffer An array buffer.
3300
+ * @returns The corresponding text.
3301
+ */
3302
+ function bufferToText(buffer) {
3303
+ return String.fromCharCode.apply(null, new Uint8Array(buffer));
3304
+ }
3305
+ /**
3306
+ * Encodes text as base 64.
3307
+ * @param text The text.
3308
+ * @returns The encoded base 64.
3309
+ */
3310
+ function textToBase64(text) {
3311
+ return Buffer.from(text, 'binary').toString('base64');
3312
+ }
3313
+ /**
3314
+ * Decodes base 64 to text.
3315
+ * @param base64 The base 64.
3316
+ * @returns The decoded text.
3317
+ */
3318
+ function base64ToText(base64) {
3319
+ return Buffer.from(base64, 'base64').toString('binary');
3320
+ }
3321
+ /**
3322
+ * Encodes an array buffer as base 64.
3323
+ * @param buffer An array buffer.
3324
+ * @returns The encoded base 64.
3325
+ */
3326
+ function bufferToBase64(buffer) {
3327
+ return textToBase64(bufferToText(buffer));
3328
+ }
3329
+ /**
3330
+ * Encodes an array buffer as base 64 url.
3331
+ * @param buffer An array buffer.
3332
+ * @returns The encoded base 64 url.
3333
+ */
3334
+ function bufferToBase64Url(buffer) {
3335
+ return textToBase64Url(bufferToText(buffer));
3336
+ }
3337
+ /**
3338
+ * Encodes text as base 64 url.
3339
+ * @param text The text.
3340
+ * @returns The encoded base 64 url.
3341
+ */
3342
+ function textToBase64Url(text) {
3343
+ return textToBase64(text).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
3340
3344
  }
3341
3345
 
3342
- var crypto = require('node:crypto');
3343
- var pkijs = require('pkijs');
3344
- pkijs.setEngine('OpenSSL', crypto, new pkijs.CryptoEngine({ name: '', crypto: crypto, subtle: crypto.subtle }));
3345
- var DEF_ALGO = {
3346
- name: 'RSASSA-PKCS1-v1_5',
3347
- modulusLength: 2048,
3348
- publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
3349
- hash: { name: 'SHA-256' },
3350
- };
3351
- function randomString() {
3352
- return __awaiter(this, void 0, void 0, function () {
3353
- var bytes;
3354
- return __generator(this, function (_a) {
3355
- bytes = new Uint8Array(24);
3356
- crypto.getRandomValues(bytes);
3357
- return [2 /*return*/, bufferToBase64Url(bytes.buffer)];
3358
- });
3359
- });
3360
- }
3361
- function gen() {
3362
- return __awaiter(this, void 0, void 0, function () {
3363
- var keys;
3364
- var _a;
3365
- return __generator(this, function (_b) {
3366
- switch (_b.label) {
3367
- case 0: return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
3368
- case 1:
3369
- keys = _b.sent();
3370
- _a = {};
3371
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
3372
- case 2:
3373
- _a.publicJwk = _b.sent();
3374
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
3375
- case 3: return [2 /*return*/, (_a.privateJwk = _b.sent(),
3376
- _a)];
3377
- }
3378
- });
3379
- });
3380
- }
3381
- function sign(text, privateJwk) {
3382
- return __awaiter(this, void 0, void 0, function () {
3383
- var cKey, buffer, signed;
3384
- return __generator(this, function (_a) {
3385
- switch (_a.label) {
3386
- case 0: return [4 /*yield*/, crypto.subtle.importKey('jwk', privateJwk, DEF_ALGO, true, ['sign'])];
3387
- case 1:
3388
- cKey = _a.sent(), buffer = textToBuffer(text);
3389
- return [4 /*yield*/, crypto.subtle.sign(DEF_ALGO.name, cKey, buffer)];
3390
- case 2:
3391
- signed = _a.sent();
3392
- return [2 /*return*/, bufferToBase64Url(signed)];
3393
- }
3394
- });
3395
- });
3396
- }
3397
- function digest(text) {
3398
- return __awaiter(this, void 0, void 0, function () {
3399
- var buffer, digest;
3400
- return __generator(this, function (_a) {
3401
- switch (_a.label) {
3402
- case 0:
3403
- buffer = textToBuffer(text);
3404
- return [4 /*yield*/, crypto.subtle.digest('SHA-256', buffer)];
3405
- case 1:
3406
- digest = _a.sent();
3407
- return [2 /*return*/, bufferToBase64Url(digest)];
3408
- }
3409
- });
3410
- });
3411
- }
3412
- //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS10ComplexExample/es6.js
3413
- function csr(params) {
3414
- return __awaiter(this, void 0, void 0, function () {
3415
- var pkcs10, keys, publicKey, toDigest, pubkeyhash_sha1, privateKey, pkcs10_schema, pkcs10_encoded, exportedPkcs8;
3416
- var _a;
3417
- return __generator(this, function (_b) {
3418
- switch (_b.label) {
3419
- case 0:
3420
- pkcs10 = new pkijs.CertificationRequest();
3421
- pkcs10.version = 0;
3422
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3423
- type: '2.5.4.3',
3424
- value: new PrintableString({ value: params.domains[0] }),
3425
- }));
3426
- if (params.country) {
3427
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3428
- type: '2.5.4.6',
3429
- value: new PrintableString({ value: params.country }),
3430
- }));
3431
- }
3432
- if (params.town) {
3433
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3434
- type: '2.5.4.7',
3435
- value: new PrintableString({ value: params.town }),
3436
- }));
3437
- }
3438
- if (params.county) {
3439
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3440
- type: '2.5.4.8',
3441
- value: new PrintableString({ value: params.county }),
3442
- }));
3443
- }
3444
- if (params.company) {
3445
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3446
- type: '2.5.4.10',
3447
- value: new PrintableString({ value: params.company }),
3448
- }));
3449
- }
3450
- if (params.department) {
3451
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3452
- type: '2.5.4.11',
3453
- value: new PrintableString({ value: params.department }),
3454
- }));
3455
- }
3456
- return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
3457
- case 1:
3458
- keys = _b.sent();
3459
- publicKey = keys.publicKey;
3460
- return [4 /*yield*/, pkcs10.subjectPublicKeyInfo.importKey(publicKey)];
3461
- case 2:
3462
- _b.sent();
3463
- toDigest = pkcs10.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex;
3464
- return [4 /*yield*/, crypto.subtle.digest('SHA-1', toDigest)];
3465
- case 3:
3466
- pubkeyhash_sha1 = _b.sent();
3467
- //pubkeyhash_sha256 = await crypto.subtle.digest('SHA-256', toDigest);
3468
- pkcs10.attributes = [];
3469
- pkcs10.attributes.push(new pkijs.Attribute({
3470
- type: '1.2.840.113549.1.9.14',
3471
- values: [
3472
- new pkijs.Extensions({
3473
- extensions: [
3474
- new pkijs.Extension({
3475
- extnID: '2.5.29.14',
3476
- critical: false,
3477
- extnValue: new OctetString({ valueHex: pubkeyhash_sha1 }).toBER(false),
3478
- }),
3479
- new pkijs.Extension({
3480
- extnID: '2.5.29.17',
3481
- critical: false,
3482
- extnValue: new pkijs.GeneralNames({
3483
- names: params.domains.map(function (dom) {
3484
- return new pkijs.GeneralName({
3485
- type: 2,
3486
- value: dom,
3487
- });
3488
- }),
3489
- })
3490
- .toSchema()
3491
- .toBER(false),
3492
- }),
3493
- ],
3494
- }).toSchema(),
3495
- ],
3496
- }));
3497
- privateKey = keys.privateKey;
3498
- return [4 /*yield*/, pkcs10.sign(privateKey, 'SHA-256')];
3499
- case 4:
3500
- _b.sent(), pkcs10_schema = pkcs10.toSchema(), pkcs10_encoded = pkcs10_schema.toBER(false);
3501
- return [4 /*yield*/, crypto.subtle.exportKey('pkcs8', keys.privateKey)];
3502
- case 5:
3503
- exportedPkcs8 = _b.sent();
3504
- _a = {
3505
- pem: bufferToPem(pkcs10_encoded, 'CERTIFICATE REQUEST'),
3506
- der: bufferToBase64Url(pkcs10_encoded),
3507
- pkcs8_pem: bufferToPem(exportedPkcs8, 'PRIVATE KEY'),
3508
- pkcs8_b64: bufferToBase64(exportedPkcs8)
3509
- };
3510
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
3511
- case 6:
3512
- _a.privateJwk = _b.sent();
3513
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
3514
- case 7: return [2 /*return*/, (_a.publicJwk = _b.sent(),
3515
- _a)];
3516
- }
3517
- });
3518
- });
3519
- }
3520
- //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS12SimpleExample/es6.js
3521
- function pfx(friendlyName, cert_b64, key_b64, password, hash) {
3522
- if (hash === void 0) { hash = 'SHA-256'; }
3523
- return __awaiter(this, void 0, void 0, function () {
3524
- var asn1_cer, asn1_key, objc_cer, objc_key, pkcs12;
3525
- return __generator(this, function (_a) {
3526
- switch (_a.label) {
3527
- case 0:
3528
- asn1_cer = fromBER(textToBuffer(base64ToText(cert_b64))), asn1_key = fromBER(textToBuffer(base64ToText(key_b64)));
3529
- objc_cer = new pkijs.Certificate({ schema: asn1_cer.result }), objc_key = new pkijs.PrivateKeyInfo({ schema: asn1_key.result });
3530
- pkcs12 = new pkijs.PFX({
3531
- parsedValue: {
3532
- integrityMode: 0,
3533
- authenticatedSafe: new pkijs.AuthenticatedSafe({
3534
- parsedValue: {
3535
- safeContents: [
3536
- {
3537
- privacyMode: 0,
3538
- value: new pkijs.SafeContents({
3539
- safeBags: [
3540
- new pkijs.SafeBag({
3541
- bagId: '1.2.840.113549.1.12.10.1.1',
3542
- bagValue: objc_key,
3543
- }),
3544
- new pkijs.SafeBag({
3545
- bagId: '1.2.840.113549.1.12.10.1.3',
3546
- bagValue: new pkijs.CertBag({
3547
- parsedValue: objc_cer,
3548
- }),
3549
- bagAttributes: [
3550
- new pkijs.Attribute({
3551
- type: '1.2.840.113549.1.9.20',
3552
- values: [new BmpString({ value: friendlyName })],
3553
- }),
3554
- ],
3555
- }),
3556
- ],
3557
- }),
3558
- },
3559
- ],
3560
- },
3561
- }),
3562
- },
3563
- });
3564
- return [4 /*yield*/, pkcs12.parsedValue.authenticatedSafe.makeInternalValues({
3565
- safeContents: [
3566
- {
3567
- /* Empty as 'No Privacy' for SafeContents */
3568
- },
3569
- ],
3570
- })];
3571
- case 1:
3572
- _a.sent();
3573
- return [4 /*yield*/, pkcs12.makeInternalValues({
3574
- password: textToBuffer(password),
3575
- iterations: 1000,
3576
- pbkdf2HashAlgorithm: hash,
3577
- hmacHashAlgorithm: hash,
3578
- })];
3579
- case 2:
3580
- _a.sent();
3581
- return [2 /*return*/, pkcs12.toSchema().toBER(false)];
3582
- }
3583
- });
3584
- });
3585
- }
3586
- function pemToBase64Parts(pem) {
3587
- return pem
3588
- .split(/-----[\w\s]+-----/)
3589
- .map(function (p) { return p.replace(/\s/g, ''); })
3590
- .filter(function (p) { return p; });
3591
- }
3592
- function base64ToPem(base64, title) {
3593
- var string_length = base64.length;
3594
- var result_string = '';
3595
- for (var i = 0, count = 0; i < string_length; i++, count++) {
3596
- if (count > 63) {
3597
- result_string += '\r\n';
3598
- count = 0;
3599
- }
3600
- result_string += base64[i];
3601
- }
3602
- title =
3603
- (title || '')
3604
- .replace(/[^\w\s]+/, '')
3605
- .trim()
3606
- .toUpperCase() || 'CERTIFICATE';
3607
- return "-----BEGIN ".concat(title, "-----\r\n").concat(result_string, "\r\n-----END ").concat(title, "-----\r\n");
3608
- }
3609
- function bufferToPem(pkcs10_buf, title) {
3610
- var base64 = bufferToBase64(pkcs10_buf);
3611
- return base64ToPem(base64, title);
3346
+ var crypto = require('node:crypto');
3347
+ var pkijs = require('pkijs');
3348
+ pkijs.setEngine('OpenSSL', crypto, new pkijs.CryptoEngine({ name: '', crypto: crypto, subtle: crypto.subtle }));
3349
+ var DEF_ALGO = {
3350
+ name: 'RSASSA-PKCS1-v1_5',
3351
+ modulusLength: 2048,
3352
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
3353
+ hash: { name: 'SHA-256' },
3354
+ };
3355
+ function randomString() {
3356
+ return __awaiter(this, void 0, void 0, function () {
3357
+ var bytes;
3358
+ return __generator(this, function (_a) {
3359
+ bytes = new Uint8Array(24);
3360
+ crypto.getRandomValues(bytes);
3361
+ return [2 /*return*/, bufferToBase64Url(bytes.buffer)];
3362
+ });
3363
+ });
3364
+ }
3365
+ function gen() {
3366
+ return __awaiter(this, void 0, void 0, function () {
3367
+ var keys;
3368
+ var _a;
3369
+ return __generator(this, function (_b) {
3370
+ switch (_b.label) {
3371
+ case 0: return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
3372
+ case 1:
3373
+ keys = _b.sent();
3374
+ _a = {};
3375
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
3376
+ case 2:
3377
+ _a.publicJwk = _b.sent();
3378
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
3379
+ case 3: return [2 /*return*/, (_a.privateJwk = _b.sent(),
3380
+ _a)];
3381
+ }
3382
+ });
3383
+ });
3384
+ }
3385
+ function sign(text, privateJwk) {
3386
+ return __awaiter(this, void 0, void 0, function () {
3387
+ var cKey, buffer, signed;
3388
+ return __generator(this, function (_a) {
3389
+ switch (_a.label) {
3390
+ case 0: return [4 /*yield*/, crypto.subtle.importKey('jwk', privateJwk, DEF_ALGO, true, ['sign'])];
3391
+ case 1:
3392
+ cKey = _a.sent(), buffer = textToBuffer(text);
3393
+ return [4 /*yield*/, crypto.subtle.sign(DEF_ALGO.name, cKey, buffer)];
3394
+ case 2:
3395
+ signed = _a.sent();
3396
+ return [2 /*return*/, bufferToBase64Url(signed)];
3397
+ }
3398
+ });
3399
+ });
3400
+ }
3401
+ function digest(text) {
3402
+ return __awaiter(this, void 0, void 0, function () {
3403
+ var buffer, digest;
3404
+ return __generator(this, function (_a) {
3405
+ switch (_a.label) {
3406
+ case 0:
3407
+ buffer = textToBuffer(text);
3408
+ return [4 /*yield*/, crypto.subtle.digest('SHA-256', buffer)];
3409
+ case 1:
3410
+ digest = _a.sent();
3411
+ return [2 /*return*/, bufferToBase64Url(digest)];
3412
+ }
3413
+ });
3414
+ });
3415
+ }
3416
+ //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS10ComplexExample/es6.js
3417
+ function csr(params) {
3418
+ return __awaiter(this, void 0, void 0, function () {
3419
+ var pkcs10, keys, publicKey, toDigest, pubkeyhash_sha1, privateKey, pkcs10_schema, pkcs10_encoded, exportedPkcs8;
3420
+ var _a;
3421
+ return __generator(this, function (_b) {
3422
+ switch (_b.label) {
3423
+ case 0:
3424
+ pkcs10 = new pkijs.CertificationRequest();
3425
+ pkcs10.version = 0;
3426
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3427
+ type: '2.5.4.3',
3428
+ value: new PrintableString({ value: params.domains[0] }),
3429
+ }));
3430
+ if (params.country) {
3431
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3432
+ type: '2.5.4.6',
3433
+ value: new PrintableString({ value: params.country }),
3434
+ }));
3435
+ }
3436
+ if (params.town) {
3437
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3438
+ type: '2.5.4.7',
3439
+ value: new PrintableString({ value: params.town }),
3440
+ }));
3441
+ }
3442
+ if (params.county) {
3443
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3444
+ type: '2.5.4.8',
3445
+ value: new PrintableString({ value: params.county }),
3446
+ }));
3447
+ }
3448
+ if (params.company) {
3449
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3450
+ type: '2.5.4.10',
3451
+ value: new PrintableString({ value: params.company }),
3452
+ }));
3453
+ }
3454
+ if (params.department) {
3455
+ pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3456
+ type: '2.5.4.11',
3457
+ value: new PrintableString({ value: params.department }),
3458
+ }));
3459
+ }
3460
+ return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
3461
+ case 1:
3462
+ keys = _b.sent();
3463
+ publicKey = keys.publicKey;
3464
+ return [4 /*yield*/, pkcs10.subjectPublicKeyInfo.importKey(publicKey)];
3465
+ case 2:
3466
+ _b.sent();
3467
+ toDigest = pkcs10.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex;
3468
+ return [4 /*yield*/, crypto.subtle.digest('SHA-1', toDigest)];
3469
+ case 3:
3470
+ pubkeyhash_sha1 = _b.sent();
3471
+ //pubkeyhash_sha256 = await crypto.subtle.digest('SHA-256', toDigest);
3472
+ pkcs10.attributes = [];
3473
+ pkcs10.attributes.push(new pkijs.Attribute({
3474
+ type: '1.2.840.113549.1.9.14',
3475
+ values: [
3476
+ new pkijs.Extensions({
3477
+ extensions: [
3478
+ new pkijs.Extension({
3479
+ extnID: '2.5.29.14',
3480
+ critical: false,
3481
+ extnValue: new OctetString({ valueHex: pubkeyhash_sha1 }).toBER(false),
3482
+ }),
3483
+ new pkijs.Extension({
3484
+ extnID: '2.5.29.17',
3485
+ critical: false,
3486
+ extnValue: new pkijs.GeneralNames({
3487
+ names: params.domains.map(function (dom) {
3488
+ return new pkijs.GeneralName({
3489
+ type: 2,
3490
+ value: dom,
3491
+ });
3492
+ }),
3493
+ })
3494
+ .toSchema()
3495
+ .toBER(false),
3496
+ }),
3497
+ ],
3498
+ }).toSchema(),
3499
+ ],
3500
+ }));
3501
+ privateKey = keys.privateKey;
3502
+ return [4 /*yield*/, pkcs10.sign(privateKey, 'SHA-256')];
3503
+ case 4:
3504
+ _b.sent(), pkcs10_schema = pkcs10.toSchema(), pkcs10_encoded = pkcs10_schema.toBER(false);
3505
+ return [4 /*yield*/, crypto.subtle.exportKey('pkcs8', keys.privateKey)];
3506
+ case 5:
3507
+ exportedPkcs8 = _b.sent();
3508
+ _a = {
3509
+ pem: bufferToPem(pkcs10_encoded, 'CERTIFICATE REQUEST'),
3510
+ der: bufferToBase64Url(pkcs10_encoded),
3511
+ pkcs8_pem: bufferToPem(exportedPkcs8, 'PRIVATE KEY'),
3512
+ pkcs8_b64: bufferToBase64(exportedPkcs8)
3513
+ };
3514
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
3515
+ case 6:
3516
+ _a.privateJwk = _b.sent();
3517
+ return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
3518
+ case 7: return [2 /*return*/, (_a.publicJwk = _b.sent(),
3519
+ _a)];
3520
+ }
3521
+ });
3522
+ });
3523
+ }
3524
+ //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS12SimpleExample/es6.js
3525
+ function pfx(friendlyName, cert_b64, key_b64, password, hash) {
3526
+ if (hash === void 0) { hash = 'SHA-256'; }
3527
+ return __awaiter(this, void 0, void 0, function () {
3528
+ var asn1_cer, asn1_key, objc_cer, objc_key, pkcs12;
3529
+ return __generator(this, function (_a) {
3530
+ switch (_a.label) {
3531
+ case 0:
3532
+ asn1_cer = fromBER(textToBuffer(base64ToText(cert_b64))), asn1_key = fromBER(textToBuffer(base64ToText(key_b64)));
3533
+ objc_cer = new pkijs.Certificate({ schema: asn1_cer.result }), objc_key = new pkijs.PrivateKeyInfo({ schema: asn1_key.result });
3534
+ pkcs12 = new pkijs.PFX({
3535
+ parsedValue: {
3536
+ integrityMode: 0,
3537
+ authenticatedSafe: new pkijs.AuthenticatedSafe({
3538
+ parsedValue: {
3539
+ safeContents: [
3540
+ {
3541
+ privacyMode: 0,
3542
+ value: new pkijs.SafeContents({
3543
+ safeBags: [
3544
+ new pkijs.SafeBag({
3545
+ bagId: '1.2.840.113549.1.12.10.1.1',
3546
+ bagValue: objc_key,
3547
+ }),
3548
+ new pkijs.SafeBag({
3549
+ bagId: '1.2.840.113549.1.12.10.1.3',
3550
+ bagValue: new pkijs.CertBag({
3551
+ parsedValue: objc_cer,
3552
+ }),
3553
+ bagAttributes: [
3554
+ new pkijs.Attribute({
3555
+ type: '1.2.840.113549.1.9.20',
3556
+ values: [new BmpString({ value: friendlyName })],
3557
+ }),
3558
+ ],
3559
+ }),
3560
+ ],
3561
+ }),
3562
+ },
3563
+ ],
3564
+ },
3565
+ }),
3566
+ },
3567
+ });
3568
+ return [4 /*yield*/, pkcs12.parsedValue.authenticatedSafe.makeInternalValues({
3569
+ safeContents: [
3570
+ {
3571
+ /* Empty as 'No Privacy' for SafeContents */
3572
+ },
3573
+ ],
3574
+ })];
3575
+ case 1:
3576
+ _a.sent();
3577
+ return [4 /*yield*/, pkcs12.makeInternalValues({
3578
+ password: textToBuffer(password),
3579
+ iterations: 1000,
3580
+ pbkdf2HashAlgorithm: hash,
3581
+ hmacHashAlgorithm: hash,
3582
+ })];
3583
+ case 2:
3584
+ _a.sent();
3585
+ return [2 /*return*/, pkcs12.toSchema().toBER(false)];
3586
+ }
3587
+ });
3588
+ });
3589
+ }
3590
+ function pemToBase64Parts(pem) {
3591
+ return pem
3592
+ .split(/-----[\w\s]+-----/)
3593
+ .map(function (p) { return p.replace(/\s/g, ''); })
3594
+ .filter(function (p) { return p; });
3595
+ }
3596
+ function base64ToPem(base64, title) {
3597
+ var string_length = base64.length;
3598
+ var result_string = '';
3599
+ for (var i = 0, count = 0; i < string_length; i++, count++) {
3600
+ if (count > 63) {
3601
+ result_string += '\r\n';
3602
+ count = 0;
3603
+ }
3604
+ result_string += base64[i];
3605
+ }
3606
+ title =
3607
+ (title || '')
3608
+ .replace(/[^\w\s]+/, '')
3609
+ .trim()
3610
+ .toUpperCase() || 'CERTIFICATE';
3611
+ return "-----BEGIN ".concat(title, "-----\r\n").concat(result_string, "\r\n-----END ").concat(title, "-----\r\n");
3612
+ }
3613
+ function bufferToPem(pkcs10_buf, title) {
3614
+ var base64 = bufferToBase64(pkcs10_buf);
3615
+ return base64ToPem(base64, title);
3612
3616
  }
3613
3617
 
3614
3618
  exports.base64ToPem = base64ToPem;