node-opcua-crypto 4.9.4 → 4.11.0
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.
- package/LICENSE +23 -23
- package/README.md +119 -114
- package/dist/{chunk-WLW5XUML.mjs → chunk-2RCYFHGG.mjs} +755 -724
- package/dist/chunk-2RCYFHGG.mjs.map +1 -0
- package/dist/chunk-C7PROBPE.mjs +14 -0
- package/dist/chunk-C7PROBPE.mjs.map +1 -0
- package/dist/{chunk-UEV3YRUV.mjs → chunk-UH5AT3JE.mjs} +9 -15
- package/dist/chunk-UH5AT3JE.mjs.map +1 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +553 -552
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -52
- package/dist/index_web-C5Oeu9mq.d.mts +503 -0
- package/dist/index_web-D1qc4UN2.d.ts +503 -0
- package/dist/source/index.d.mts +11 -516
- package/dist/source/index.d.ts +11 -516
- package/dist/source/index.js +537 -529
- package/dist/source/index.js.map +1 -1
- package/dist/source/index.mjs +9 -50
- package/dist/source/index_web.d.mts +1 -1
- package/dist/source/index_web.d.ts +1 -1
- package/dist/source/index_web.js +525 -522
- package/dist/source/index_web.js.map +1 -1
- package/dist/source/index_web.mjs +5 -49
- package/dist/source_nodejs/index.d.mts +1 -2
- package/dist/source_nodejs/index.d.ts +1 -2
- package/dist/source_nodejs/index.js +33 -41
- package/dist/source_nodejs/index.js.map +1 -1
- package/dist/source_nodejs/index.mjs +3 -5
- package/package.json +9 -4
- package/dist/chunk-5NV4OKIV.mjs +0 -1
- package/dist/chunk-5NV4OKIV.mjs.map +0 -1
- package/dist/chunk-UEV3YRUV.mjs.map +0 -1
- package/dist/chunk-WLW5XUML.mjs.map +0 -1
- package/dist/index_web.d.mts +0 -4
- package/dist/index_web.d.ts +0 -4
- package/dist/index_web.js +0 -6365
- package/dist/index_web.js.map +0 -1
- package/dist/index_web.mjs +0 -192
- package/dist/index_web.mjs.map +0 -1
- package/index.mjs +0 -1
- package/index_web.ts +0 -1
- package/web.d.ts +0 -1
- package/web.mjs +0 -1
|
@@ -17,24 +17,6 @@ var getFilename = () => fileURLToPath(import.meta.url);
|
|
|
17
17
|
var getDirname = () => path.dirname(getFilename());
|
|
18
18
|
var __dirname = /* @__PURE__ */ getDirname();
|
|
19
19
|
|
|
20
|
-
// source/common.ts
|
|
21
|
-
import __crypto from "crypto";
|
|
22
|
-
var KeyObjectOrig = __crypto.KeyObject;
|
|
23
|
-
var { createPrivateKey: createPrivateKeyFromNodeJSCrypto } = __crypto;
|
|
24
|
-
function isKeyObject(mayBeKeyObject) {
|
|
25
|
-
if (KeyObjectOrig) {
|
|
26
|
-
return mayBeKeyObject instanceof KeyObjectOrig;
|
|
27
|
-
}
|
|
28
|
-
return typeof mayBeKeyObject === "object" && typeof mayBeKeyObject.type === "string";
|
|
29
|
-
}
|
|
30
|
-
var CertificatePurpose = /* @__PURE__ */ ((CertificatePurpose2) => {
|
|
31
|
-
CertificatePurpose2[CertificatePurpose2["NotSpecified"] = 0] = "NotSpecified";
|
|
32
|
-
CertificatePurpose2[CertificatePurpose2["ForCertificateAuthority"] = 1] = "ForCertificateAuthority";
|
|
33
|
-
CertificatePurpose2[CertificatePurpose2["ForApplication"] = 2] = "ForApplication";
|
|
34
|
-
CertificatePurpose2[CertificatePurpose2["ForUserAuthentication"] = 3] = "ForUserAuthentication";
|
|
35
|
-
return CertificatePurpose2;
|
|
36
|
-
})(CertificatePurpose || {});
|
|
37
|
-
|
|
38
20
|
// source/asn1.ts
|
|
39
21
|
import assert from "assert";
|
|
40
22
|
|
|
@@ -102,6 +84,7 @@ var oid_map = {
|
|
|
102
84
|
"1.3.6.1.4.1.311.2.1.22": { d: "1.3.6.1.4.1.311.2.1.22", c: "SPC_COMMERCIAL_SP_KEY_PURPOSE_OBJID" },
|
|
103
85
|
"1.3.6.1.4.1.311.10.3.1": { d: "1.3.6.1.4.1.311.10.3.1", c: "Signer of CTLs -- szOID_KP_CTL_USAGE_SIGNING" },
|
|
104
86
|
"1.3.6.1.4.1.311.10.3.4": { d: "1.3.6.1.4.1.311.10.3.4", c: "szOID_EFS_RECOVERY (Encryption File System)" },
|
|
87
|
+
"1.3.6.1.4.1.311.20.2.3": { d: "1.3.6.1.4.1.311.20.2.3", c: "id-on-personalData" },
|
|
105
88
|
"1.3.6.1.5.5.7.3.17": { d: "1.3.6.1.5.5.7.3.17", c: "Internet Key Exchange (IKE)" },
|
|
106
89
|
"1.3.6.1.5.5.7.3.1": { d: "serverAuth", c: "PKIX key purpose" },
|
|
107
90
|
"1.3.6.1.5.5.7.3.2": { d: "clientAuth", c: "PKIX key purpose" },
|
|
@@ -361,12 +344,15 @@ var TagType = /* @__PURE__ */ ((TagType3) => {
|
|
|
361
344
|
TagType3[TagType3["BMPString"] = 30] = "BMPString";
|
|
362
345
|
TagType3[TagType3["SEQUENCE"] = 48] = "SEQUENCE";
|
|
363
346
|
TagType3[TagType3["SET"] = 49] = "SET";
|
|
364
|
-
TagType3[TagType3["
|
|
347
|
+
TagType3[TagType3["CONTEXT_SPECIFIC0"] = 160] = "CONTEXT_SPECIFIC0";
|
|
348
|
+
TagType3[TagType3["CONTEXT_SPECIFIC1"] = 161] = "CONTEXT_SPECIFIC1";
|
|
349
|
+
TagType3[TagType3["CONTEXT_SPECIFIC2"] = 162] = "CONTEXT_SPECIFIC2";
|
|
350
|
+
TagType3[TagType3["CONTEXT_SPECIFIC3"] = 163] = "CONTEXT_SPECIFIC3";
|
|
351
|
+
TagType3[TagType3["A4"] = 164] = "A4";
|
|
365
352
|
return TagType3;
|
|
366
353
|
})(TagType || {});
|
|
367
354
|
function readTag(buf, pos) {
|
|
368
|
-
|
|
369
|
-
assert(Number.isFinite(pos) && pos >= 0);
|
|
355
|
+
const start = pos;
|
|
370
356
|
if (buf.length <= pos) {
|
|
371
357
|
throw new Error("Invalid position : buf.length=" + buf.length + " pos =" + pos);
|
|
372
358
|
}
|
|
@@ -382,9 +368,9 @@ function readTag(buf, pos) {
|
|
|
382
368
|
pos += 1;
|
|
383
369
|
}
|
|
384
370
|
}
|
|
385
|
-
return { tag, position: pos, length };
|
|
371
|
+
return { start, tag, position: pos, length };
|
|
386
372
|
}
|
|
387
|
-
function
|
|
373
|
+
function readStruct(buf, blockInfo) {
|
|
388
374
|
const length = blockInfo.length;
|
|
389
375
|
let cursor = blockInfo.position;
|
|
390
376
|
const end = blockInfo.position + length;
|
|
@@ -446,9 +432,9 @@ function _readIntegerAsByteString(buffer, block) {
|
|
|
446
432
|
}
|
|
447
433
|
function _readListOfInteger(buffer) {
|
|
448
434
|
const block = readTag(buffer, 0);
|
|
449
|
-
const inner_blocks =
|
|
450
|
-
return inner_blocks.map((
|
|
451
|
-
return _readIntegerAsByteString(buffer,
|
|
435
|
+
const inner_blocks = readStruct(buffer, block);
|
|
436
|
+
return inner_blocks.map((innerBlock) => {
|
|
437
|
+
return _readIntegerAsByteString(buffer, innerBlock);
|
|
452
438
|
});
|
|
453
439
|
}
|
|
454
440
|
function parseOID(buffer, start, end) {
|
|
@@ -480,24 +466,24 @@ function _readObjectIdentifier(buffer, block) {
|
|
|
480
466
|
name: oid_map[oid] ? oid_map[oid].d : oid
|
|
481
467
|
};
|
|
482
468
|
}
|
|
483
|
-
function
|
|
484
|
-
const inner_blocks =
|
|
469
|
+
function readAlgorithmIdentifier(buffer, block) {
|
|
470
|
+
const inner_blocks = readStruct(buffer, block);
|
|
485
471
|
return {
|
|
486
472
|
identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name
|
|
487
473
|
};
|
|
488
474
|
}
|
|
489
475
|
function _readECCAlgorithmIdentifier(buffer, block) {
|
|
490
|
-
const inner_blocks =
|
|
476
|
+
const inner_blocks = readStruct(buffer, block);
|
|
491
477
|
return {
|
|
492
478
|
identifier: _readObjectIdentifier(buffer, inner_blocks[1]).name
|
|
493
479
|
// difference with RSA as algorithm is second element of nested block
|
|
494
480
|
};
|
|
495
481
|
}
|
|
496
|
-
function
|
|
482
|
+
function readSignatureValueBin(buffer, block) {
|
|
497
483
|
return _readBitString(buffer, block).data;
|
|
498
484
|
}
|
|
499
|
-
function
|
|
500
|
-
return
|
|
485
|
+
function readSignatureValue(buffer, block) {
|
|
486
|
+
return readSignatureValueBin(buffer, block).toString("hex");
|
|
501
487
|
}
|
|
502
488
|
function _readLongIntegerValue(buffer, block) {
|
|
503
489
|
assert(block.tag === 2 /* INTEGER */, "expecting a INTEGER tag");
|
|
@@ -578,18 +564,15 @@ function _readValue(buffer, block) {
|
|
|
578
564
|
throw new Error("Invalid tag 0x" + block.tag.toString(16));
|
|
579
565
|
}
|
|
580
566
|
}
|
|
581
|
-
function compactDirectoryName(d) {
|
|
582
|
-
return JSON.stringify(d);
|
|
583
|
-
}
|
|
584
567
|
function _readDirectoryName(buffer, block) {
|
|
585
|
-
const set_blocks =
|
|
568
|
+
const set_blocks = readStruct(buffer, block);
|
|
586
569
|
const names = {};
|
|
587
570
|
for (const set_block of set_blocks) {
|
|
588
571
|
assert(set_block.tag === 49);
|
|
589
|
-
const blocks =
|
|
572
|
+
const blocks = readStruct(buffer, set_block);
|
|
590
573
|
assert(blocks.length === 1);
|
|
591
574
|
assert(blocks[0].tag === 48);
|
|
592
|
-
const sequenceBlock =
|
|
575
|
+
const sequenceBlock = readStruct(buffer, blocks[0]);
|
|
593
576
|
assert(sequenceBlock.length === 2);
|
|
594
577
|
const type = _readObjectIdentifier(buffer, sequenceBlock[0]);
|
|
595
578
|
names[type.name] = _readValue(buffer, sequenceBlock[1]);
|
|
@@ -607,12 +590,9 @@ function _readTime(buffer, block) {
|
|
|
607
590
|
return _readValue(buffer, block);
|
|
608
591
|
}
|
|
609
592
|
|
|
610
|
-
// source/crypto_explore_certificate.ts
|
|
611
|
-
import assert3 from "assert";
|
|
612
|
-
|
|
613
593
|
// source/crypto_utils.ts
|
|
614
594
|
import constants from "constants";
|
|
615
|
-
import
|
|
595
|
+
import assert3 from "assert";
|
|
616
596
|
import {
|
|
617
597
|
createHash,
|
|
618
598
|
createSign,
|
|
@@ -627,194 +607,12 @@ var createFastUninitializedBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe : (s
|
|
|
627
607
|
return new Buffer(size);
|
|
628
608
|
};
|
|
629
609
|
|
|
630
|
-
// source/crypto_utils.ts
|
|
631
|
-
import jsrsasign from "jsrsasign";
|
|
632
|
-
var { hexy } = pkg_hexy;
|
|
633
|
-
var PEM_REGEX = /^(-----BEGIN (.*)-----\r?\n([/+=a-zA-Z0-9\r\n]*)\r?\n-----END \2-----\r?\n?)/gm;
|
|
634
|
-
var PEM_TYPE_REGEX = /^(-----BEGIN (.*)-----)/m;
|
|
635
|
-
function identifyPemType(rawKey) {
|
|
636
|
-
if (rawKey instanceof Buffer) {
|
|
637
|
-
rawKey = rawKey.toString("utf8");
|
|
638
|
-
}
|
|
639
|
-
const match = PEM_TYPE_REGEX.exec(rawKey);
|
|
640
|
-
return !match ? void 0 : match[2];
|
|
641
|
-
}
|
|
642
|
-
function removeTrailingLF(str) {
|
|
643
|
-
const tmp = str.replace(/(\r|\n)+$/m, "").replace(/\r\n/gm, "\n");
|
|
644
|
-
return tmp;
|
|
645
|
-
}
|
|
646
|
-
function toPem(raw_key, pem) {
|
|
647
|
-
assert2(raw_key, "expecting a key");
|
|
648
|
-
assert2(typeof pem === "string");
|
|
649
|
-
let pemType = identifyPemType(raw_key);
|
|
650
|
-
if (pemType) {
|
|
651
|
-
return raw_key instanceof Buffer ? removeTrailingLF(raw_key.toString("utf8")) : removeTrailingLF(raw_key);
|
|
652
|
-
} else {
|
|
653
|
-
pemType = pem;
|
|
654
|
-
assert2(["CERTIFICATE REQUEST", "CERTIFICATE", "RSA PRIVATE KEY", "PUBLIC KEY", "X509 CRL"].indexOf(pemType) >= 0);
|
|
655
|
-
let b = raw_key.toString("base64");
|
|
656
|
-
let str = "-----BEGIN " + pemType + "-----\n";
|
|
657
|
-
while (b.length) {
|
|
658
|
-
str += b.substring(0, 64) + "\n";
|
|
659
|
-
b = b.substring(64);
|
|
660
|
-
}
|
|
661
|
-
str += "-----END " + pemType + "-----";
|
|
662
|
-
return str;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
function convertPEMtoDER(raw_key) {
|
|
666
|
-
let match;
|
|
667
|
-
let pemType;
|
|
668
|
-
let base64str;
|
|
669
|
-
const parts = [];
|
|
670
|
-
PEM_REGEX.lastIndex = 0;
|
|
671
|
-
while ((match = PEM_REGEX.exec(raw_key)) !== null) {
|
|
672
|
-
pemType = match[2];
|
|
673
|
-
base64str = match[3];
|
|
674
|
-
base64str = base64str.replace(/\r?\n/g, "");
|
|
675
|
-
parts.push(Buffer.from(base64str, "base64"));
|
|
676
|
-
}
|
|
677
|
-
return combine_der(parts);
|
|
678
|
-
}
|
|
679
|
-
function hexDump(buffer, width) {
|
|
680
|
-
if (!buffer) {
|
|
681
|
-
return "<>";
|
|
682
|
-
}
|
|
683
|
-
width = width || 32;
|
|
684
|
-
if (buffer.length > 1024) {
|
|
685
|
-
return hexy(buffer.subarray(0, 1024), { width, format: "twos" }) + "\n .... ( " + buffer.length + ")";
|
|
686
|
-
} else {
|
|
687
|
-
return hexy(buffer, { width, format: "twos" });
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
function makeMessageChunkSignature(chunk, options) {
|
|
691
|
-
const signer = createSign(options.algorithm);
|
|
692
|
-
signer.update(chunk);
|
|
693
|
-
const signature = signer.sign(options.privateKey.hidden);
|
|
694
|
-
assert2(!options.signatureLength || signature.length === options.signatureLength);
|
|
695
|
-
return signature;
|
|
696
|
-
}
|
|
697
|
-
function verifyMessageChunkSignature(blockToVerify, signature, options) {
|
|
698
|
-
const verify = createVerify(options.algorithm);
|
|
699
|
-
verify.update(blockToVerify);
|
|
700
|
-
return verify.verify(options.publicKey, signature);
|
|
701
|
-
}
|
|
702
|
-
function makeSHA1Thumbprint(buffer) {
|
|
703
|
-
return createHash("sha1").update(buffer).digest();
|
|
704
|
-
}
|
|
705
|
-
var RSA_PKCS1_OAEP_PADDING = constants.RSA_PKCS1_OAEP_PADDING;
|
|
706
|
-
var RSA_PKCS1_PADDING = constants.RSA_PKCS1_PADDING;
|
|
707
|
-
var PaddingAlgorithm = /* @__PURE__ */ ((PaddingAlgorithm2) => {
|
|
708
|
-
PaddingAlgorithm2[PaddingAlgorithm2["RSA_PKCS1_OAEP_PADDING"] = 4] = "RSA_PKCS1_OAEP_PADDING";
|
|
709
|
-
PaddingAlgorithm2[PaddingAlgorithm2["RSA_PKCS1_PADDING"] = 1] = "RSA_PKCS1_PADDING";
|
|
710
|
-
return PaddingAlgorithm2;
|
|
711
|
-
})(PaddingAlgorithm || {});
|
|
712
|
-
assert2(4 /* RSA_PKCS1_OAEP_PADDING */ === constants.RSA_PKCS1_OAEP_PADDING);
|
|
713
|
-
assert2(1 /* RSA_PKCS1_PADDING */ === constants.RSA_PKCS1_PADDING);
|
|
714
|
-
function publicEncrypt_native(buffer, publicKey, algorithm) {
|
|
715
|
-
if (algorithm === void 0) {
|
|
716
|
-
algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
|
|
717
|
-
}
|
|
718
|
-
return publicEncrypt1(
|
|
719
|
-
{
|
|
720
|
-
key: publicKey,
|
|
721
|
-
padding: algorithm
|
|
722
|
-
},
|
|
723
|
-
buffer
|
|
724
|
-
);
|
|
725
|
-
}
|
|
726
|
-
function privateDecrypt_native(buffer, privateKey, algorithm) {
|
|
727
|
-
if (algorithm === void 0) {
|
|
728
|
-
algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
|
|
729
|
-
}
|
|
730
|
-
try {
|
|
731
|
-
return privateDecrypt1(
|
|
732
|
-
{
|
|
733
|
-
key: privateKey.hidden,
|
|
734
|
-
padding: algorithm
|
|
735
|
-
},
|
|
736
|
-
buffer
|
|
737
|
-
);
|
|
738
|
-
} catch (err) {
|
|
739
|
-
return Buffer.alloc(1);
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
var publicEncrypt = publicEncrypt_native;
|
|
743
|
-
var privateDecrypt = privateDecrypt_native;
|
|
744
|
-
function publicEncrypt_long(buffer, publicKey, blockSize, padding, paddingAlgorithm) {
|
|
745
|
-
if (paddingAlgorithm === void 0) {
|
|
746
|
-
paddingAlgorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
|
|
747
|
-
}
|
|
748
|
-
if (paddingAlgorithm === RSA_PKCS1_PADDING) {
|
|
749
|
-
padding = padding || 11;
|
|
750
|
-
if (padding !== 11) throw new Error("padding should be 11");
|
|
751
|
-
} else if (paddingAlgorithm === RSA_PKCS1_OAEP_PADDING) {
|
|
752
|
-
padding = padding || 42;
|
|
753
|
-
if (padding !== 42) throw new Error("padding should be 42");
|
|
754
|
-
} else {
|
|
755
|
-
throw new Error("Invalid padding algorithm " + paddingAlgorithm);
|
|
756
|
-
}
|
|
757
|
-
const chunk_size = blockSize - padding;
|
|
758
|
-
const nbBlocks = Math.ceil(buffer.length / chunk_size);
|
|
759
|
-
const outputBuffer = createFastUninitializedBuffer(nbBlocks * blockSize);
|
|
760
|
-
for (let i = 0; i < nbBlocks; i++) {
|
|
761
|
-
const currentBlock = buffer.subarray(chunk_size * i, chunk_size * (i + 1));
|
|
762
|
-
const encrypted_chunk = publicEncrypt(currentBlock, publicKey, paddingAlgorithm);
|
|
763
|
-
if (encrypted_chunk.length !== blockSize) {
|
|
764
|
-
throw new Error(`publicEncrypt_long unexpected chunk length ${encrypted_chunk.length} expecting ${blockSize}`);
|
|
765
|
-
}
|
|
766
|
-
encrypted_chunk.copy(outputBuffer, i * blockSize);
|
|
767
|
-
}
|
|
768
|
-
return outputBuffer;
|
|
769
|
-
}
|
|
770
|
-
function privateDecrypt_long(buffer, privateKey, blockSize, paddingAlgorithm) {
|
|
771
|
-
paddingAlgorithm = paddingAlgorithm || RSA_PKCS1_OAEP_PADDING;
|
|
772
|
-
if (paddingAlgorithm !== RSA_PKCS1_PADDING && paddingAlgorithm !== RSA_PKCS1_OAEP_PADDING) {
|
|
773
|
-
throw new Error("Invalid padding algorithm " + paddingAlgorithm);
|
|
774
|
-
}
|
|
775
|
-
const nbBlocks = Math.ceil(buffer.length / blockSize);
|
|
776
|
-
const outputBuffer = createFastUninitializedBuffer(nbBlocks * blockSize);
|
|
777
|
-
let total_length = 0;
|
|
778
|
-
for (let i = 0; i < nbBlocks; i++) {
|
|
779
|
-
const currentBlock = buffer.subarray(blockSize * i, Math.min(blockSize * (i + 1), buffer.length));
|
|
780
|
-
const decrypted_buf = privateDecrypt(currentBlock, privateKey, paddingAlgorithm);
|
|
781
|
-
decrypted_buf.copy(outputBuffer, total_length);
|
|
782
|
-
total_length += decrypted_buf.length;
|
|
783
|
-
}
|
|
784
|
-
return outputBuffer.subarray(0, total_length);
|
|
785
|
-
}
|
|
786
|
-
function coerceCertificatePem(certificate) {
|
|
787
|
-
if (certificate instanceof Buffer) {
|
|
788
|
-
certificate = toPem(certificate, "CERTIFICATE");
|
|
789
|
-
}
|
|
790
|
-
assert2(typeof certificate === "string");
|
|
791
|
-
return certificate;
|
|
792
|
-
}
|
|
793
|
-
function extractPublicKeyFromCertificateSync(certificate) {
|
|
794
|
-
certificate = coerceCertificatePem(certificate);
|
|
795
|
-
const key = jsrsasign.KEYUTIL.getKey(certificate);
|
|
796
|
-
const publicKeyAsPem = jsrsasign.KEYUTIL.getPEM(key);
|
|
797
|
-
assert2(typeof publicKeyAsPem === "string");
|
|
798
|
-
return publicKeyAsPem;
|
|
799
|
-
}
|
|
800
|
-
function extractPublicKeyFromCertificate(certificate, callback) {
|
|
801
|
-
let err1 = null;
|
|
802
|
-
let keyPem;
|
|
803
|
-
try {
|
|
804
|
-
keyPem = extractPublicKeyFromCertificateSync(certificate);
|
|
805
|
-
} catch (err) {
|
|
806
|
-
err1 = err;
|
|
807
|
-
}
|
|
808
|
-
setImmediate(() => {
|
|
809
|
-
callback(err1, keyPem);
|
|
810
|
-
});
|
|
811
|
-
}
|
|
812
|
-
|
|
813
610
|
// source/crypto_explore_certificate.ts
|
|
611
|
+
import assert2 from "assert";
|
|
814
612
|
var doDebug = false;
|
|
815
613
|
function _readAttributeTypeAndValue(buffer, block) {
|
|
816
|
-
let inner_blocks =
|
|
817
|
-
inner_blocks =
|
|
614
|
+
let inner_blocks = readStruct(buffer, block);
|
|
615
|
+
inner_blocks = readStruct(buffer, inner_blocks[0]);
|
|
818
616
|
const data = {
|
|
819
617
|
identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name,
|
|
820
618
|
value: _readValue(buffer, inner_blocks[1])
|
|
@@ -826,7 +624,7 @@ function _readAttributeTypeAndValue(buffer, block) {
|
|
|
826
624
|
return result;
|
|
827
625
|
}
|
|
828
626
|
function _readRelativeDistinguishedName(buffer, block) {
|
|
829
|
-
const inner_blocks =
|
|
627
|
+
const inner_blocks = readStruct(buffer, block);
|
|
830
628
|
const data = inner_blocks.map((block2) => _readAttributeTypeAndValue(buffer, block2));
|
|
831
629
|
const result = {};
|
|
832
630
|
for (const e of data) {
|
|
@@ -838,7 +636,7 @@ function _readName(buffer, block) {
|
|
|
838
636
|
return _readRelativeDistinguishedName(buffer, block);
|
|
839
637
|
}
|
|
840
638
|
function _readValidity(buffer, block) {
|
|
841
|
-
const inner_blocks =
|
|
639
|
+
const inner_blocks = readStruct(buffer, block);
|
|
842
640
|
return {
|
|
843
641
|
notBefore: _readTime(buffer, inner_blocks[0]),
|
|
844
642
|
notAfter: _readTime(buffer, inner_blocks[1])
|
|
@@ -846,27 +644,27 @@ function _readValidity(buffer, block) {
|
|
|
846
644
|
}
|
|
847
645
|
function _readAuthorityKeyIdentifier(buffer) {
|
|
848
646
|
const block_info = readTag(buffer, 0);
|
|
849
|
-
const blocks =
|
|
647
|
+
const blocks = readStruct(buffer, block_info);
|
|
850
648
|
const keyIdentifier_block = _findBlockAtIndex(blocks, 0);
|
|
851
649
|
const authorityCertIssuer_block = _findBlockAtIndex(blocks, 1);
|
|
852
650
|
const authorityCertSerialNumber_block = _findBlockAtIndex(blocks, 2);
|
|
853
651
|
function _readAuthorityCertIssuer(block) {
|
|
854
|
-
const inner_blocks =
|
|
652
|
+
const inner_blocks = readStruct(buffer, block);
|
|
855
653
|
const directoryName_block = _findBlockAtIndex(inner_blocks, 4);
|
|
856
654
|
if (directoryName_block) {
|
|
857
|
-
const a =
|
|
655
|
+
const a = readStruct(buffer, directoryName_block);
|
|
858
656
|
return _readDirectoryName(buffer, a[0]);
|
|
859
657
|
} else {
|
|
860
658
|
throw new Error("Invalid _readAuthorityCertIssuer");
|
|
861
659
|
}
|
|
862
660
|
}
|
|
863
661
|
function _readAuthorityCertIssuerFingerPrint(block) {
|
|
864
|
-
const inner_blocks =
|
|
662
|
+
const inner_blocks = readStruct(buffer, block);
|
|
865
663
|
const directoryName_block = _findBlockAtIndex(inner_blocks, 4);
|
|
866
664
|
if (!directoryName_block) {
|
|
867
665
|
return "";
|
|
868
666
|
}
|
|
869
|
-
const a =
|
|
667
|
+
const a = readStruct(buffer, directoryName_block);
|
|
870
668
|
if (a.length < 1) {
|
|
871
669
|
return "";
|
|
872
670
|
}
|
|
@@ -885,7 +683,7 @@ function _readAuthorityKeyIdentifier(buffer) {
|
|
|
885
683
|
}
|
|
886
684
|
function readBasicConstraint2_5_29_19(buffer, block) {
|
|
887
685
|
const block_info = readTag(buffer, 0);
|
|
888
|
-
const inner_blocks =
|
|
686
|
+
const inner_blocks = readStruct(buffer, block_info).slice(0, 2);
|
|
889
687
|
let cA = false;
|
|
890
688
|
let pathLengthConstraint = 0;
|
|
891
689
|
let breakControl = 0;
|
|
@@ -914,9 +712,10 @@ function _readGeneralNames(buffer, block) {
|
|
|
914
712
|
5: { name: "ediPartyName", type: "EDIPartyName" },
|
|
915
713
|
6: { name: "uniformResourceIdentifier", type: "IA5String" },
|
|
916
714
|
7: { name: "iPAddress", type: "OCTET_STRING" },
|
|
917
|
-
8: { name: "registeredID", type: "OBJECT_IDENTIFIER" }
|
|
715
|
+
8: { name: "registeredID", type: "OBJECT_IDENTIFIER" },
|
|
716
|
+
32: { name: "otherName", type: "AnotherName" }
|
|
918
717
|
};
|
|
919
|
-
const blocks =
|
|
718
|
+
const blocks = readStruct(buffer, block);
|
|
920
719
|
function _readFromType(buffer2, block2, type) {
|
|
921
720
|
switch (type) {
|
|
922
721
|
case "IA5String":
|
|
@@ -927,14 +726,29 @@ function _readGeneralNames(buffer, block) {
|
|
|
927
726
|
}
|
|
928
727
|
const n = {};
|
|
929
728
|
for (const block2 of blocks) {
|
|
930
|
-
|
|
931
|
-
const
|
|
932
|
-
const type = _data[
|
|
729
|
+
assert2((block2.tag & 128) === 128);
|
|
730
|
+
const t2 = block2.tag & 127;
|
|
731
|
+
const type = _data[t2];
|
|
933
732
|
if (!type) {
|
|
934
|
-
|
|
733
|
+
console.log("_readGeneralNames: INVALID TYPE => " + t2 + " 0x" + t2.toString(16));
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
if (t2 == 32) {
|
|
737
|
+
n[type.name] = n[type.name] || [];
|
|
738
|
+
const blocks2 = readStruct(buffer, block2);
|
|
739
|
+
const name = _readObjectIdentifier(buffer, blocks2[0]).name;
|
|
740
|
+
const buf = _getBlock(buffer, blocks2[1]);
|
|
741
|
+
const b = readTag(buf, 0);
|
|
742
|
+
const nn = _readValue(buf, b);
|
|
743
|
+
const data = {
|
|
744
|
+
identifier: name,
|
|
745
|
+
value: nn
|
|
746
|
+
};
|
|
747
|
+
n[type.name].push(data.value);
|
|
748
|
+
} else {
|
|
749
|
+
n[type.name] = n[type.name] || [];
|
|
750
|
+
n[type.name].push(_readFromType(buffer, block2, type.type));
|
|
935
751
|
}
|
|
936
|
-
n[type.name] = n[type.name] || [];
|
|
937
|
-
n[type.name].push(_readFromType(buffer, block2, type.type));
|
|
938
752
|
}
|
|
939
753
|
return n;
|
|
940
754
|
}
|
|
@@ -972,9 +786,9 @@ function readKeyUsage(oid, buffer) {
|
|
|
972
786
|
};
|
|
973
787
|
}
|
|
974
788
|
function readExtKeyUsage(oid, buffer) {
|
|
975
|
-
|
|
789
|
+
assert2(oid === "2.5.29.37");
|
|
976
790
|
const block_info = readTag(buffer, 0);
|
|
977
|
-
const inner_blocks =
|
|
791
|
+
const inner_blocks = readStruct(buffer, block_info);
|
|
978
792
|
const extKeyUsage = {
|
|
979
793
|
serverAuth: false,
|
|
980
794
|
clientAuth: false,
|
|
@@ -994,15 +808,15 @@ function readExtKeyUsage(oid, buffer) {
|
|
|
994
808
|
}
|
|
995
809
|
function _readSubjectPublicKey(buffer) {
|
|
996
810
|
const block_info = readTag(buffer, 0);
|
|
997
|
-
const blocks =
|
|
811
|
+
const blocks = readStruct(buffer, block_info);
|
|
998
812
|
return {
|
|
999
813
|
modulus: buffer.subarray(blocks[0].position + 1, blocks[0].position + blocks[0].length)
|
|
1000
814
|
};
|
|
1001
815
|
}
|
|
1002
816
|
function _readExtension(buffer, block) {
|
|
1003
|
-
const inner_blocks =
|
|
817
|
+
const inner_blocks = readStruct(buffer, block);
|
|
1004
818
|
if (inner_blocks.length === 3) {
|
|
1005
|
-
|
|
819
|
+
assert2(inner_blocks[1].tag === 1 /* BOOLEAN */);
|
|
1006
820
|
inner_blocks[1] = inner_blocks[2];
|
|
1007
821
|
}
|
|
1008
822
|
const identifier = _readObjectIdentifier(buffer, inner_blocks[0]);
|
|
@@ -1039,9 +853,9 @@ function _readExtension(buffer, block) {
|
|
|
1039
853
|
};
|
|
1040
854
|
}
|
|
1041
855
|
function _readExtensions(buffer, block) {
|
|
1042
|
-
|
|
1043
|
-
let inner_blocks =
|
|
1044
|
-
inner_blocks =
|
|
856
|
+
assert2(block.tag === 163);
|
|
857
|
+
let inner_blocks = readStruct(buffer, block);
|
|
858
|
+
inner_blocks = readStruct(buffer, inner_blocks[0]);
|
|
1045
859
|
const extensions = inner_blocks.map((block2) => _readExtension(buffer, block2));
|
|
1046
860
|
const result = {};
|
|
1047
861
|
for (const e of extensions) {
|
|
@@ -1050,8 +864,8 @@ function _readExtensions(buffer, block) {
|
|
|
1050
864
|
return result;
|
|
1051
865
|
}
|
|
1052
866
|
function _readSubjectPublicKeyInfo(buffer, block) {
|
|
1053
|
-
const inner_blocks =
|
|
1054
|
-
const algorithm =
|
|
867
|
+
const inner_blocks = readStruct(buffer, block);
|
|
868
|
+
const algorithm = readAlgorithmIdentifier(buffer, inner_blocks[0]);
|
|
1055
869
|
const subjectPublicKey = _readBitString(buffer, inner_blocks[1]);
|
|
1056
870
|
const data = subjectPublicKey.data;
|
|
1057
871
|
const values = _readListOfInteger(data);
|
|
@@ -1064,7 +878,7 @@ function _readSubjectPublicKeyInfo(buffer, block) {
|
|
|
1064
878
|
};
|
|
1065
879
|
}
|
|
1066
880
|
function _readSubjectECCPublicKeyInfo(buffer, block) {
|
|
1067
|
-
const inner_blocks =
|
|
881
|
+
const inner_blocks = readStruct(buffer, block);
|
|
1068
882
|
const algorithm = _readECCAlgorithmIdentifier(buffer, inner_blocks[0]);
|
|
1069
883
|
const subjectPublicKey = _readBitString(buffer, inner_blocks[1]);
|
|
1070
884
|
const data = subjectPublicKey.data;
|
|
@@ -1077,13 +891,13 @@ function _readSubjectECCPublicKeyInfo(buffer, block) {
|
|
|
1077
891
|
};
|
|
1078
892
|
}
|
|
1079
893
|
function readTbsCertificate(buffer, block) {
|
|
1080
|
-
const blocks =
|
|
894
|
+
const blocks = readStruct(buffer, block);
|
|
1081
895
|
let version, serialNumber, signature, issuer, validity, subject, subjectFingerPrint, extensions;
|
|
1082
896
|
let subjectPublicKeyInfo;
|
|
1083
897
|
if (blocks.length === 6) {
|
|
1084
898
|
version = 1;
|
|
1085
899
|
serialNumber = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, blocks[0]));
|
|
1086
|
-
signature =
|
|
900
|
+
signature = readAlgorithmIdentifier(buffer, blocks[1]);
|
|
1087
901
|
issuer = _readName(buffer, blocks[2]);
|
|
1088
902
|
validity = _readValidity(buffer, blocks[3]);
|
|
1089
903
|
subject = _readName(buffer, blocks[4]);
|
|
@@ -1097,13 +911,13 @@ function readTbsCertificate(buffer, block) {
|
|
|
1097
911
|
}
|
|
1098
912
|
version = _readVersionValue(buffer, version_block) + 1;
|
|
1099
913
|
serialNumber = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, blocks[1]));
|
|
1100
|
-
signature =
|
|
914
|
+
signature = readAlgorithmIdentifier(buffer, blocks[2]);
|
|
1101
915
|
issuer = _readName(buffer, blocks[3]);
|
|
1102
916
|
validity = _readValidity(buffer, blocks[4]);
|
|
1103
917
|
subject = _readName(buffer, blocks[5]);
|
|
1104
918
|
subjectFingerPrint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[5])));
|
|
1105
|
-
const inner_block =
|
|
1106
|
-
const what_type =
|
|
919
|
+
const inner_block = readStruct(buffer, blocks[6]);
|
|
920
|
+
const what_type = readAlgorithmIdentifier(buffer, inner_block[0]).identifier;
|
|
1107
921
|
switch (what_type) {
|
|
1108
922
|
case "rsaEncryption": {
|
|
1109
923
|
subjectPublicKeyInfo = _readSubjectPublicKeyInfo(buffer, blocks[6]);
|
|
@@ -1136,14 +950,14 @@ function readTbsCertificate(buffer, block) {
|
|
|
1136
950
|
};
|
|
1137
951
|
}
|
|
1138
952
|
function exploreCertificate(certificate) {
|
|
1139
|
-
|
|
953
|
+
assert2(certificate instanceof Buffer);
|
|
1140
954
|
if (!certificate._exploreCertificate_cache) {
|
|
1141
955
|
const block_info = readTag(certificate, 0);
|
|
1142
|
-
const blocks =
|
|
956
|
+
const blocks = readStruct(certificate, block_info);
|
|
1143
957
|
certificate._exploreCertificate_cache = {
|
|
1144
958
|
tbsCertificate: readTbsCertificate(certificate, blocks[0]),
|
|
1145
|
-
signatureAlgorithm:
|
|
1146
|
-
signatureValue:
|
|
959
|
+
signatureAlgorithm: readAlgorithmIdentifier(certificate, blocks[1]),
|
|
960
|
+
signatureValue: readSignatureValue(certificate, blocks[2])
|
|
1147
961
|
};
|
|
1148
962
|
}
|
|
1149
963
|
return certificate._exploreCertificate_cache;
|
|
@@ -1165,51 +979,411 @@ function combine_der(certificates) {
|
|
|
1165
979
|
let sum = 0;
|
|
1166
980
|
b.forEach((block) => {
|
|
1167
981
|
const block_info = readTag(block, 0);
|
|
1168
|
-
|
|
982
|
+
assert2(block_info.position + block_info.length === block.length);
|
|
1169
983
|
sum += block.length;
|
|
1170
984
|
});
|
|
1171
|
-
|
|
985
|
+
assert2(sum === cert.length);
|
|
1172
986
|
}
|
|
1173
987
|
return Buffer.concat(certificates);
|
|
1174
988
|
}
|
|
1175
989
|
|
|
1176
|
-
// source/
|
|
1177
|
-
import
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
990
|
+
// source/crypto_utils.ts
|
|
991
|
+
import jsrsasign from "jsrsasign";
|
|
992
|
+
var { hexy } = pkg_hexy;
|
|
993
|
+
var PEM_REGEX = /^(-----BEGIN (.*)-----\r?\n([/+=a-zA-Z0-9\r\n]*)\r?\n-----END \2-----\r?\n?)/gm;
|
|
994
|
+
var PEM_TYPE_REGEX = /^(-----BEGIN (.*)-----)/m;
|
|
995
|
+
function identifyPemType(rawKey) {
|
|
996
|
+
if (rawKey instanceof Buffer) {
|
|
997
|
+
rawKey = rawKey.toString("utf8");
|
|
1181
998
|
}
|
|
1182
|
-
|
|
1183
|
-
return
|
|
999
|
+
const match = PEM_TYPE_REGEX.exec(rawKey);
|
|
1000
|
+
return !match ? void 0 : match[2];
|
|
1184
1001
|
}
|
|
1185
|
-
function
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
const data = {
|
|
1189
|
-
publicKeyLength: certInfo.tbsCertificate.subjectPublicKeyInfo.keyLength,
|
|
1190
|
-
notBefore: certInfo.tbsCertificate.validity.notBefore,
|
|
1191
|
-
notAfter: certInfo.tbsCertificate.validity.notAfter,
|
|
1192
|
-
publicKey: certInfo.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey,
|
|
1193
|
-
subject: certInfo.tbsCertificate.subject
|
|
1194
|
-
};
|
|
1195
|
-
if (!(data.publicKeyLength === 512 || data.publicKeyLength === 384 || data.publicKeyLength === 256 || data.publicKeyLength === 128)) {
|
|
1196
|
-
throw new Error("Invalid public key length (expecting 128,256,384 or 512)" + data.publicKeyLength);
|
|
1197
|
-
}
|
|
1198
|
-
return data;
|
|
1002
|
+
function removeTrailingLF(str) {
|
|
1003
|
+
const tmp = str.replace(/(\r|\n)+$/m, "").replace(/\r\n/gm, "\n");
|
|
1004
|
+
return tmp;
|
|
1199
1005
|
}
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1006
|
+
function toPem(raw_key, pem) {
|
|
1007
|
+
assert3(raw_key, "expecting a key");
|
|
1008
|
+
assert3(typeof pem === "string");
|
|
1009
|
+
let pemType = identifyPemType(raw_key);
|
|
1010
|
+
if (pemType) {
|
|
1011
|
+
return raw_key instanceof Buffer ? removeTrailingLF(raw_key.toString("utf8")) : removeTrailingLF(raw_key);
|
|
1012
|
+
} else {
|
|
1013
|
+
pemType = pem;
|
|
1014
|
+
assert3(["CERTIFICATE REQUEST", "CERTIFICATE", "RSA PRIVATE KEY", "PUBLIC KEY", "X509 CRL"].indexOf(pemType) >= 0);
|
|
1015
|
+
let b = raw_key.toString("base64");
|
|
1016
|
+
let str = "-----BEGIN " + pemType + "-----\n";
|
|
1017
|
+
while (b.length) {
|
|
1018
|
+
str += b.substring(0, 64) + "\n";
|
|
1019
|
+
b = b.substring(64);
|
|
1020
|
+
}
|
|
1021
|
+
str += "-----END " + pemType + "-----";
|
|
1022
|
+
return str;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
function convertPEMtoDER(raw_key) {
|
|
1026
|
+
let match;
|
|
1027
|
+
let pemType;
|
|
1028
|
+
let base64str;
|
|
1029
|
+
const parts = [];
|
|
1030
|
+
PEM_REGEX.lastIndex = 0;
|
|
1031
|
+
while ((match = PEM_REGEX.exec(raw_key)) !== null) {
|
|
1032
|
+
pemType = match[2];
|
|
1033
|
+
base64str = match[3];
|
|
1034
|
+
base64str = base64str.replace(/\r?\n/g, "");
|
|
1035
|
+
parts.push(Buffer.from(base64str, "base64"));
|
|
1036
|
+
}
|
|
1037
|
+
return combine_der(parts);
|
|
1038
|
+
}
|
|
1039
|
+
function hexDump(buffer, width) {
|
|
1040
|
+
if (!buffer) {
|
|
1041
|
+
return "<>";
|
|
1042
|
+
}
|
|
1043
|
+
width = width || 32;
|
|
1044
|
+
if (buffer.length > 1024) {
|
|
1045
|
+
return hexy(buffer.subarray(0, 1024), { width, format: "twos" }) + "\n .... ( " + buffer.length + ")";
|
|
1046
|
+
} else {
|
|
1047
|
+
return hexy(buffer, { width, format: "twos" });
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
function makeMessageChunkSignature(chunk, options) {
|
|
1051
|
+
const signer = createSign(options.algorithm);
|
|
1052
|
+
signer.update(chunk);
|
|
1053
|
+
const signature = signer.sign(options.privateKey.hidden);
|
|
1054
|
+
assert3(!options.signatureLength || signature.length === options.signatureLength);
|
|
1055
|
+
return signature;
|
|
1056
|
+
}
|
|
1057
|
+
function verifyMessageChunkSignature(blockToVerify, signature, options) {
|
|
1058
|
+
const verify = createVerify(options.algorithm);
|
|
1059
|
+
verify.update(blockToVerify);
|
|
1060
|
+
return verify.verify(options.publicKey, signature);
|
|
1061
|
+
}
|
|
1062
|
+
function makeSHA1Thumbprint(buffer) {
|
|
1063
|
+
return createHash("sha1").update(buffer).digest();
|
|
1064
|
+
}
|
|
1065
|
+
var RSA_PKCS1_OAEP_PADDING = constants.RSA_PKCS1_OAEP_PADDING;
|
|
1066
|
+
var RSA_PKCS1_PADDING = constants.RSA_PKCS1_PADDING;
|
|
1067
|
+
var PaddingAlgorithm = /* @__PURE__ */ ((PaddingAlgorithm2) => {
|
|
1068
|
+
PaddingAlgorithm2[PaddingAlgorithm2["RSA_PKCS1_OAEP_PADDING"] = 4] = "RSA_PKCS1_OAEP_PADDING";
|
|
1069
|
+
PaddingAlgorithm2[PaddingAlgorithm2["RSA_PKCS1_PADDING"] = 1] = "RSA_PKCS1_PADDING";
|
|
1070
|
+
return PaddingAlgorithm2;
|
|
1071
|
+
})(PaddingAlgorithm || {});
|
|
1072
|
+
assert3(4 /* RSA_PKCS1_OAEP_PADDING */ === constants.RSA_PKCS1_OAEP_PADDING);
|
|
1073
|
+
assert3(1 /* RSA_PKCS1_PADDING */ === constants.RSA_PKCS1_PADDING);
|
|
1074
|
+
function publicEncrypt_native(buffer, publicKey, algorithm) {
|
|
1075
|
+
if (algorithm === void 0) {
|
|
1076
|
+
algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
|
|
1077
|
+
}
|
|
1078
|
+
return publicEncrypt1(
|
|
1079
|
+
{
|
|
1080
|
+
key: publicKey,
|
|
1081
|
+
padding: algorithm
|
|
1082
|
+
},
|
|
1083
|
+
buffer
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
function privateDecrypt_native(buffer, privateKey, algorithm) {
|
|
1087
|
+
if (algorithm === void 0) {
|
|
1088
|
+
algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
|
|
1089
|
+
}
|
|
1090
|
+
try {
|
|
1091
|
+
return privateDecrypt1(
|
|
1092
|
+
{
|
|
1093
|
+
key: privateKey.hidden,
|
|
1094
|
+
padding: algorithm
|
|
1095
|
+
},
|
|
1096
|
+
buffer
|
|
1097
|
+
);
|
|
1098
|
+
} catch (err) {
|
|
1099
|
+
return Buffer.alloc(1);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
var publicEncrypt = publicEncrypt_native;
|
|
1103
|
+
var privateDecrypt = privateDecrypt_native;
|
|
1104
|
+
function publicEncrypt_long(buffer, publicKey, blockSize, padding, paddingAlgorithm) {
|
|
1105
|
+
if (paddingAlgorithm === void 0) {
|
|
1106
|
+
paddingAlgorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
|
|
1107
|
+
}
|
|
1108
|
+
if (paddingAlgorithm === RSA_PKCS1_PADDING) {
|
|
1109
|
+
padding = padding || 11;
|
|
1110
|
+
if (padding !== 11) throw new Error("padding should be 11");
|
|
1111
|
+
} else if (paddingAlgorithm === RSA_PKCS1_OAEP_PADDING) {
|
|
1112
|
+
padding = padding || 42;
|
|
1113
|
+
if (padding !== 42) throw new Error("padding should be 42");
|
|
1114
|
+
} else {
|
|
1115
|
+
throw new Error("Invalid padding algorithm " + paddingAlgorithm);
|
|
1116
|
+
}
|
|
1117
|
+
const chunk_size = blockSize - padding;
|
|
1118
|
+
const nbBlocks = Math.ceil(buffer.length / chunk_size);
|
|
1119
|
+
const outputBuffer = createFastUninitializedBuffer(nbBlocks * blockSize);
|
|
1120
|
+
for (let i = 0; i < nbBlocks; i++) {
|
|
1121
|
+
const currentBlock = buffer.subarray(chunk_size * i, chunk_size * (i + 1));
|
|
1122
|
+
const encrypted_chunk = publicEncrypt(currentBlock, publicKey, paddingAlgorithm);
|
|
1123
|
+
if (encrypted_chunk.length !== blockSize) {
|
|
1124
|
+
throw new Error(`publicEncrypt_long unexpected chunk length ${encrypted_chunk.length} expecting ${blockSize}`);
|
|
1125
|
+
}
|
|
1126
|
+
encrypted_chunk.copy(outputBuffer, i * blockSize);
|
|
1127
|
+
}
|
|
1128
|
+
return outputBuffer;
|
|
1129
|
+
}
|
|
1130
|
+
function privateDecrypt_long(buffer, privateKey, blockSize, paddingAlgorithm) {
|
|
1131
|
+
paddingAlgorithm = paddingAlgorithm || RSA_PKCS1_OAEP_PADDING;
|
|
1132
|
+
if (paddingAlgorithm !== RSA_PKCS1_PADDING && paddingAlgorithm !== RSA_PKCS1_OAEP_PADDING) {
|
|
1133
|
+
throw new Error("Invalid padding algorithm " + paddingAlgorithm);
|
|
1134
|
+
}
|
|
1135
|
+
const nbBlocks = Math.ceil(buffer.length / blockSize);
|
|
1136
|
+
const outputBuffer = createFastUninitializedBuffer(nbBlocks * blockSize);
|
|
1137
|
+
let total_length = 0;
|
|
1138
|
+
for (let i = 0; i < nbBlocks; i++) {
|
|
1139
|
+
const currentBlock = buffer.subarray(blockSize * i, Math.min(blockSize * (i + 1), buffer.length));
|
|
1140
|
+
const decrypted_buf = privateDecrypt(currentBlock, privateKey, paddingAlgorithm);
|
|
1141
|
+
decrypted_buf.copy(outputBuffer, total_length);
|
|
1142
|
+
total_length += decrypted_buf.length;
|
|
1143
|
+
}
|
|
1144
|
+
return outputBuffer.subarray(0, total_length);
|
|
1145
|
+
}
|
|
1146
|
+
function coerceCertificatePem(certificate) {
|
|
1147
|
+
if (certificate instanceof Buffer) {
|
|
1148
|
+
certificate = toPem(certificate, "CERTIFICATE");
|
|
1149
|
+
}
|
|
1150
|
+
assert3(typeof certificate === "string");
|
|
1151
|
+
return certificate;
|
|
1152
|
+
}
|
|
1153
|
+
function extractPublicKeyFromCertificateSync(certificate) {
|
|
1154
|
+
certificate = coerceCertificatePem(certificate);
|
|
1155
|
+
const key = jsrsasign.KEYUTIL.getKey(certificate);
|
|
1156
|
+
const publicKeyAsPem = jsrsasign.KEYUTIL.getPEM(key);
|
|
1157
|
+
assert3(typeof publicKeyAsPem === "string");
|
|
1158
|
+
return publicKeyAsPem;
|
|
1159
|
+
}
|
|
1160
|
+
function extractPublicKeyFromCertificate(certificate, callback) {
|
|
1161
|
+
let err1 = null;
|
|
1162
|
+
let keyPem;
|
|
1163
|
+
try {
|
|
1164
|
+
keyPem = extractPublicKeyFromCertificateSync(certificate);
|
|
1165
|
+
} catch (err) {
|
|
1166
|
+
err1 = err;
|
|
1167
|
+
}
|
|
1168
|
+
setImmediate(() => {
|
|
1169
|
+
callback(err1, keyPem);
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// source/explore_private_key.ts
|
|
1174
|
+
function f(buffer, b) {
|
|
1175
|
+
return buffer.subarray(b.position + 1, b.position + b.length);
|
|
1176
|
+
}
|
|
1177
|
+
var doDebug2 = !!process.env.DEBUG;
|
|
1178
|
+
function explorePrivateKey(privateKey2) {
|
|
1179
|
+
const privateKey1 = privateKey2.hidden;
|
|
1180
|
+
const privateKey = typeof privateKey1 === "string" ? convertPEMtoDER(privateKey1) : privateKey1.export({ format: "der", type: "pkcs1" });
|
|
1181
|
+
const block_info = readTag(privateKey, 0);
|
|
1182
|
+
const blocks = readStruct(privateKey, block_info);
|
|
1183
|
+
if (blocks.length === 9) {
|
|
1184
|
+
const version2 = f(privateKey, blocks[0]);
|
|
1185
|
+
const modulus2 = f(privateKey, blocks[1]);
|
|
1186
|
+
const publicExponent2 = f(privateKey, blocks[2]);
|
|
1187
|
+
const privateExponent2 = f(privateKey, blocks[3]);
|
|
1188
|
+
const prime12 = f(privateKey, blocks[4]);
|
|
1189
|
+
const prime22 = f(privateKey, blocks[5]);
|
|
1190
|
+
const exponent12 = f(privateKey, blocks[6]);
|
|
1191
|
+
const exponent22 = f(privateKey, blocks[7]);
|
|
1192
|
+
return {
|
|
1193
|
+
version: version2,
|
|
1194
|
+
modulus: modulus2,
|
|
1195
|
+
publicExponent: publicExponent2,
|
|
1196
|
+
privateExponent: privateExponent2,
|
|
1197
|
+
prime1: prime12,
|
|
1198
|
+
prime2: prime22,
|
|
1199
|
+
exponent1: exponent12,
|
|
1200
|
+
exponent2: exponent22
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
if (doDebug2) {
|
|
1204
|
+
console.log("-------------------- private key:");
|
|
1205
|
+
console.log(block_info);
|
|
1206
|
+
console.log(
|
|
1207
|
+
blocks.map((b2) => ({
|
|
1208
|
+
tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
|
|
1209
|
+
l: b2.length,
|
|
1210
|
+
p: b2.position,
|
|
1211
|
+
buff: privateKey.subarray(b2.position, b2.position + b2.length).toString("hex")
|
|
1212
|
+
}))
|
|
1213
|
+
);
|
|
1214
|
+
}
|
|
1215
|
+
const b = blocks[2];
|
|
1216
|
+
const bb = privateKey.subarray(b.position, b.position + b.length);
|
|
1217
|
+
const block_info1 = readTag(bb, 0);
|
|
1218
|
+
const blocks1 = readStruct(bb, block_info1);
|
|
1219
|
+
if (doDebug2) {
|
|
1220
|
+
console.log(
|
|
1221
|
+
blocks1.map((b2) => ({
|
|
1222
|
+
tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
|
|
1223
|
+
l: b2.length,
|
|
1224
|
+
p: b2.position,
|
|
1225
|
+
buff: bb.subarray(b2.position, b2.position + b2.length).toString("hex")
|
|
1226
|
+
}))
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
const version = f(bb, blocks1[0]);
|
|
1230
|
+
const modulus = f(bb, blocks1[1]);
|
|
1231
|
+
const publicExponent = f(bb, blocks1[2]);
|
|
1232
|
+
const privateExponent = f(bb, blocks1[3]);
|
|
1233
|
+
const prime1 = f(bb, blocks1[4]);
|
|
1234
|
+
const prime2 = f(bb, blocks1[5]);
|
|
1235
|
+
const exponent1 = f(bb, blocks1[6]);
|
|
1236
|
+
const exponent2 = f(bb, blocks1[7]);
|
|
1237
|
+
return {
|
|
1238
|
+
version,
|
|
1239
|
+
modulus,
|
|
1240
|
+
publicExponent,
|
|
1241
|
+
privateExponent,
|
|
1242
|
+
prime1,
|
|
1243
|
+
prime2,
|
|
1244
|
+
exponent1,
|
|
1245
|
+
exponent2
|
|
1246
|
+
};
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
// source/public_private_match.ts
|
|
1250
|
+
function publicKeyAndPrivateKeyMatches(certificate, privateKey) {
|
|
1251
|
+
const i = exploreCertificate(certificate);
|
|
1252
|
+
const j = explorePrivateKey(privateKey);
|
|
1253
|
+
const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
|
|
1254
|
+
const modulus2 = j.modulus;
|
|
1255
|
+
if (modulus1.length != modulus2.length) {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
return modulus1.toString("hex") === modulus2.toString("hex");
|
|
1259
|
+
}
|
|
1260
|
+
function certificateMatchesPrivateKeyPEM(certificate, privateKey, blockSize) {
|
|
1261
|
+
const initialBuffer = Buffer.from("Lorem Ipsum");
|
|
1262
|
+
const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize);
|
|
1263
|
+
const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
|
|
1264
|
+
const finalString = decryptedBuffer.toString("utf-8");
|
|
1265
|
+
return initialBuffer.toString("utf-8") === finalString;
|
|
1266
|
+
}
|
|
1267
|
+
function certificateMatchesPrivateKey(certificate, privateKey) {
|
|
1268
|
+
const e = explorePrivateKey(privateKey);
|
|
1269
|
+
const blockSize = e.modulus.length;
|
|
1270
|
+
const certificatePEM = toPem(certificate, "CERTIFICATE");
|
|
1271
|
+
return certificateMatchesPrivateKeyPEM(certificatePEM, privateKey, blockSize);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
// source/common.ts
|
|
1275
|
+
import __crypto from "crypto";
|
|
1276
|
+
var KeyObjectOrig = __crypto.KeyObject;
|
|
1277
|
+
var { createPrivateKey: createPrivateKeyFromNodeJSCrypto } = __crypto;
|
|
1278
|
+
function isKeyObject(mayBeKeyObject) {
|
|
1279
|
+
if (KeyObjectOrig) {
|
|
1280
|
+
return mayBeKeyObject instanceof KeyObjectOrig;
|
|
1281
|
+
}
|
|
1282
|
+
return typeof mayBeKeyObject === "object" && typeof mayBeKeyObject.type === "string";
|
|
1283
|
+
}
|
|
1284
|
+
var CertificatePurpose = /* @__PURE__ */ ((CertificatePurpose2) => {
|
|
1285
|
+
CertificatePurpose2[CertificatePurpose2["NotSpecified"] = 0] = "NotSpecified";
|
|
1286
|
+
CertificatePurpose2[CertificatePurpose2["ForCertificateAuthority"] = 1] = "ForCertificateAuthority";
|
|
1287
|
+
CertificatePurpose2[CertificatePurpose2["ForApplication"] = 2] = "ForApplication";
|
|
1288
|
+
CertificatePurpose2[CertificatePurpose2["ForUserAuthentication"] = 3] = "ForUserAuthentication";
|
|
1289
|
+
return CertificatePurpose2;
|
|
1290
|
+
})(CertificatePurpose || {});
|
|
1291
|
+
|
|
1292
|
+
// source/crypto_utils2.ts
|
|
1293
|
+
import assert4 from "assert";
|
|
1294
|
+
import jsrsasign2 from "jsrsasign";
|
|
1295
|
+
function rsaLengthPrivateKey(key) {
|
|
1296
|
+
const keyPem = typeof key.hidden === "string" ? key.hidden : key.hidden.export({ type: "pkcs1", format: "pem" }).toString();
|
|
1297
|
+
const a = jsrsasign2.KEYUTIL.getKey(keyPem);
|
|
1298
|
+
return a.n.toString(16).length / 2;
|
|
1299
|
+
}
|
|
1300
|
+
function toPem2(raw_key, pem) {
|
|
1301
|
+
if (raw_key.hidden) {
|
|
1302
|
+
return toPem2(raw_key.hidden, pem);
|
|
1303
|
+
}
|
|
1304
|
+
assert4(raw_key, "expecting a key");
|
|
1305
|
+
assert4(typeof pem === "string");
|
|
1306
|
+
if (isKeyObject(raw_key)) {
|
|
1307
|
+
const _raw_key = raw_key;
|
|
1308
|
+
if (pem === "RSA PRIVATE KEY") {
|
|
1309
|
+
return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs1" }).toString());
|
|
1310
|
+
} else if (pem === "PRIVATE KEY") {
|
|
1311
|
+
return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs8" }).toString());
|
|
1312
|
+
} else {
|
|
1313
|
+
throw new Error("Unsupported case!");
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
return toPem(raw_key, pem);
|
|
1317
|
+
}
|
|
1318
|
+
function coercePrivateKeyPem(privateKey) {
|
|
1319
|
+
return toPem2(privateKey, "PRIVATE KEY");
|
|
1320
|
+
}
|
|
1321
|
+
function coercePublicKeyPem(publicKey) {
|
|
1322
|
+
if (isKeyObject(publicKey)) {
|
|
1323
|
+
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
1324
|
+
}
|
|
1325
|
+
assert4(typeof publicKey === "string");
|
|
1326
|
+
return publicKey;
|
|
1327
|
+
}
|
|
1328
|
+
function coerceRsaPublicKeyPem(publicKey) {
|
|
1329
|
+
if (isKeyObject(publicKey)) {
|
|
1330
|
+
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
1331
|
+
}
|
|
1332
|
+
assert4(typeof publicKey === "string");
|
|
1333
|
+
return publicKey;
|
|
1334
|
+
}
|
|
1335
|
+
function rsaLengthPublicKey(key) {
|
|
1336
|
+
key = coercePublicKeyPem(key);
|
|
1337
|
+
assert4(typeof key === "string");
|
|
1338
|
+
const a = jsrsasign2.KEYUTIL.getKey(key);
|
|
1339
|
+
return a.n.toString(16).length / 2;
|
|
1340
|
+
}
|
|
1341
|
+
function rsaLengthRsaPublicKey(key) {
|
|
1342
|
+
key = coerceRsaPublicKeyPem(key);
|
|
1343
|
+
assert4(typeof key === "string");
|
|
1344
|
+
const a = jsrsasign2.KEYUTIL.getKey(key);
|
|
1345
|
+
return a.n.toString(16).length / 2;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// source/derived_keys.ts
|
|
1349
|
+
import assert6 from "assert";
|
|
1350
|
+
import { createCipheriv, createDecipheriv, createHmac } from "crypto";
|
|
1351
|
+
|
|
1352
|
+
// source/explore_certificate.ts
|
|
1353
|
+
import assert5 from "assert";
|
|
1354
|
+
function coerceCertificate(certificate) {
|
|
1355
|
+
if (typeof certificate === "string") {
|
|
1356
|
+
certificate = convertPEMtoDER(certificate);
|
|
1357
|
+
}
|
|
1358
|
+
assert5(certificate instanceof Buffer);
|
|
1359
|
+
return certificate;
|
|
1360
|
+
}
|
|
1361
|
+
function exploreCertificateInfo(certificate) {
|
|
1362
|
+
certificate = coerceCertificate(certificate);
|
|
1363
|
+
const certInfo = exploreCertificate(certificate);
|
|
1364
|
+
const data = {
|
|
1365
|
+
publicKeyLength: certInfo.tbsCertificate.subjectPublicKeyInfo.keyLength,
|
|
1366
|
+
notBefore: certInfo.tbsCertificate.validity.notBefore,
|
|
1367
|
+
notAfter: certInfo.tbsCertificate.validity.notAfter,
|
|
1368
|
+
publicKey: certInfo.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey,
|
|
1369
|
+
subject: certInfo.tbsCertificate.subject
|
|
1370
|
+
};
|
|
1371
|
+
if (!(data.publicKeyLength === 512 || data.publicKeyLength === 384 || data.publicKeyLength === 256 || data.publicKeyLength === 128)) {
|
|
1372
|
+
throw new Error("Invalid public key length (expecting 128,256,384 or 512)" + data.publicKeyLength);
|
|
1373
|
+
}
|
|
1374
|
+
return data;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
// source/derived_keys.ts
|
|
1378
|
+
function HMAC_HASH(sha1or256, secret, message) {
|
|
1379
|
+
return createHmac(sha1or256, secret).update(message).digest();
|
|
1206
1380
|
}
|
|
1207
1381
|
function plus(buf1, buf2) {
|
|
1208
1382
|
return Buffer.concat([buf1, buf2]);
|
|
1209
1383
|
}
|
|
1210
1384
|
function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
|
|
1211
|
-
|
|
1212
|
-
|
|
1385
|
+
assert6(seed instanceof Buffer);
|
|
1386
|
+
assert6(sha1or256 === "SHA1" || sha1or256 === "SHA256");
|
|
1213
1387
|
const a = [];
|
|
1214
1388
|
a[0] = seed;
|
|
1215
1389
|
let index = 1;
|
|
@@ -1222,12 +1396,12 @@ function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
|
|
|
1222
1396
|
return p_hash.subarray(0, minLength);
|
|
1223
1397
|
}
|
|
1224
1398
|
function computeDerivedKeys(secret, seed, options) {
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1399
|
+
assert6(Number.isFinite(options.signatureLength));
|
|
1400
|
+
assert6(Number.isFinite(options.encryptingKeyLength));
|
|
1401
|
+
assert6(Number.isFinite(options.encryptingBlockSize));
|
|
1402
|
+
assert6(typeof options.algorithm === "string");
|
|
1229
1403
|
options.sha1or256 = options.sha1or256 || "SHA1";
|
|
1230
|
-
|
|
1404
|
+
assert6(typeof options.sha1or256 === "string");
|
|
1231
1405
|
const offset1 = options.signingKeyLength;
|
|
1232
1406
|
const offset2 = offset1 + options.encryptingKeyLength;
|
|
1233
1407
|
const minLength = offset2 + options.encryptingBlockSize;
|
|
@@ -1252,7 +1426,7 @@ function removePadding(buffer) {
|
|
|
1252
1426
|
return reduceLength(buffer, nbPaddingBytes);
|
|
1253
1427
|
}
|
|
1254
1428
|
function verifyChunkSignature(chunk, options) {
|
|
1255
|
-
|
|
1429
|
+
assert6(chunk instanceof Buffer);
|
|
1256
1430
|
let signatureLength = options.signatureLength || 0;
|
|
1257
1431
|
if (signatureLength === 0) {
|
|
1258
1432
|
const cert = exploreCertificateInfo(options.publicKey);
|
|
@@ -1263,16 +1437,16 @@ function verifyChunkSignature(chunk, options) {
|
|
|
1263
1437
|
return verifyMessageChunkSignature(block_to_verify, signature, options);
|
|
1264
1438
|
}
|
|
1265
1439
|
function computePaddingFooter(buffer, derivedKeys) {
|
|
1266
|
-
|
|
1440
|
+
assert6(Object.prototype.hasOwnProperty.call(derivedKeys, "encryptingBlockSize"));
|
|
1267
1441
|
const paddingSize = derivedKeys.encryptingBlockSize - (buffer.length + 1) % derivedKeys.encryptingBlockSize;
|
|
1268
1442
|
const padding = createFastUninitializedBuffer(paddingSize + 1);
|
|
1269
1443
|
padding.fill(paddingSize);
|
|
1270
1444
|
return padding;
|
|
1271
1445
|
}
|
|
1272
1446
|
function derivedKeys_algorithm(derivedKeys) {
|
|
1273
|
-
|
|
1447
|
+
assert6(Object.prototype.hasOwnProperty.call(derivedKeys, "algorithm"));
|
|
1274
1448
|
const algorithm = derivedKeys.algorithm || "aes-128-cbc";
|
|
1275
|
-
|
|
1449
|
+
assert6(algorithm === "aes-128-cbc" || algorithm === "aes-256-cbc");
|
|
1276
1450
|
return algorithm;
|
|
1277
1451
|
}
|
|
1278
1452
|
function encryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
@@ -1286,153 +1460,58 @@ function encryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
|
1286
1460
|
encrypted_chunks.push(cipher.final());
|
|
1287
1461
|
return Buffer.concat(encrypted_chunks);
|
|
1288
1462
|
}
|
|
1289
|
-
function decryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
1290
|
-
const algorithm = derivedKeys_algorithm(derivedKeys);
|
|
1291
|
-
const key = derivedKeys.encryptingKey;
|
|
1292
|
-
const initVector = derivedKeys.initializationVector;
|
|
1293
|
-
const cipher = createDecipheriv(algorithm, key, initVector);
|
|
1294
|
-
cipher.setAutoPadding(false);
|
|
1295
|
-
const decrypted_chunks = [];
|
|
1296
|
-
decrypted_chunks.push(cipher.update(buffer));
|
|
1297
|
-
decrypted_chunks.push(cipher.final());
|
|
1298
|
-
return Buffer.concat(decrypted_chunks);
|
|
1299
|
-
}
|
|
1300
|
-
function makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys) {
|
|
1301
|
-
assert5(message instanceof Buffer);
|
|
1302
|
-
assert5(derivedKeys.signingKey instanceof Buffer);
|
|
1303
|
-
assert5(typeof derivedKeys.sha1or256 === "string");
|
|
1304
|
-
assert5(derivedKeys.sha1or256 === "SHA1" || derivedKeys.sha1or256 === "SHA256");
|
|
1305
|
-
const signature = createHmac(derivedKeys.sha1or256, derivedKeys.signingKey).update(message).digest();
|
|
1306
|
-
assert5(signature.length === derivedKeys.signatureLength);
|
|
1307
|
-
return signature;
|
|
1308
|
-
}
|
|
1309
|
-
function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
|
|
1310
|
-
const message = chunk.subarray(0, chunk.length - derivedKeys.signatureLength);
|
|
1311
|
-
const expectedSignature = chunk.subarray(chunk.length - derivedKeys.signatureLength);
|
|
1312
|
-
const computedSignature = makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys);
|
|
1313
|
-
return computedSignature.toString("hex") === expectedSignature.toString("hex");
|
|
1314
|
-
}
|
|
1315
|
-
|
|
1316
|
-
// source/crypto_utils2.ts
|
|
1317
|
-
import assert6 from "assert";
|
|
1318
|
-
import jsrsasign2 from "jsrsasign";
|
|
1319
|
-
function rsaLengthPrivateKey(key) {
|
|
1320
|
-
const keyPem = typeof key.hidden === "string" ? key.hidden : key.hidden.export({ type: "pkcs1", format: "pem" }).toString();
|
|
1321
|
-
const a = jsrsasign2.KEYUTIL.getKey(keyPem);
|
|
1322
|
-
return a.n.toString(16).length / 2;
|
|
1323
|
-
}
|
|
1324
|
-
function toPem2(raw_key, pem) {
|
|
1325
|
-
if (raw_key.hidden) {
|
|
1326
|
-
return toPem2(raw_key.hidden, pem);
|
|
1327
|
-
}
|
|
1328
|
-
assert6(raw_key, "expecting a key");
|
|
1329
|
-
assert6(typeof pem === "string");
|
|
1330
|
-
if (isKeyObject(raw_key)) {
|
|
1331
|
-
const _raw_key = raw_key;
|
|
1332
|
-
if (pem === "RSA PRIVATE KEY") {
|
|
1333
|
-
return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs1" }).toString());
|
|
1334
|
-
} else if (pem === "PRIVATE KEY") {
|
|
1335
|
-
return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs8" }).toString());
|
|
1336
|
-
} else {
|
|
1337
|
-
throw new Error("Unsupported case!");
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
return toPem(raw_key, pem);
|
|
1341
|
-
}
|
|
1342
|
-
function coercePrivateKeyPem(privateKey) {
|
|
1343
|
-
return toPem2(privateKey, "PRIVATE KEY");
|
|
1344
|
-
}
|
|
1345
|
-
function coercePublicKeyPem(publicKey) {
|
|
1346
|
-
if (isKeyObject(publicKey)) {
|
|
1347
|
-
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
1348
|
-
}
|
|
1349
|
-
assert6(typeof publicKey === "string");
|
|
1350
|
-
return publicKey;
|
|
1351
|
-
}
|
|
1352
|
-
function coerceRsaPublicKeyPem(publicKey) {
|
|
1353
|
-
if (isKeyObject(publicKey)) {
|
|
1354
|
-
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
1355
|
-
}
|
|
1356
|
-
assert6(typeof publicKey === "string");
|
|
1357
|
-
return publicKey;
|
|
1358
|
-
}
|
|
1359
|
-
function rsaLengthPublicKey(key) {
|
|
1360
|
-
key = coercePublicKeyPem(key);
|
|
1361
|
-
assert6(typeof key === "string");
|
|
1362
|
-
const a = jsrsasign2.KEYUTIL.getKey(key);
|
|
1363
|
-
return a.n.toString(16).length / 2;
|
|
1364
|
-
}
|
|
1365
|
-
function rsaLengthRsaPublicKey(key) {
|
|
1366
|
-
key = coerceRsaPublicKeyPem(key);
|
|
1367
|
-
assert6(typeof key === "string");
|
|
1368
|
-
const a = jsrsasign2.KEYUTIL.getKey(key);
|
|
1369
|
-
return a.n.toString(16).length / 2;
|
|
1370
|
-
}
|
|
1371
|
-
|
|
1372
|
-
// source/verify_certificate_signature.ts
|
|
1373
|
-
import { createVerify as createVerify2 } from "crypto";
|
|
1374
|
-
function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
|
|
1375
|
-
const block_info = readTag(certificateOrCrl, 0);
|
|
1376
|
-
const blocks = _readStruct(certificateOrCrl, block_info);
|
|
1377
|
-
const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
|
|
1378
|
-
const signatureAlgorithm = _readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
|
|
1379
|
-
const signatureValue = _readSignatureValueBin(certificateOrCrl, blocks[2]);
|
|
1380
|
-
const p = split_der(parentCertificate)[0];
|
|
1381
|
-
const certPem = toPem(p, "CERTIFICATE");
|
|
1382
|
-
const verify = createVerify2(signatureAlgorithm.identifier);
|
|
1383
|
-
verify.update(bufferToBeSigned);
|
|
1384
|
-
verify.end();
|
|
1385
|
-
return verify.verify(certPem, signatureValue);
|
|
1463
|
+
function decryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
1464
|
+
const algorithm = derivedKeys_algorithm(derivedKeys);
|
|
1465
|
+
const key = derivedKeys.encryptingKey;
|
|
1466
|
+
const initVector = derivedKeys.initializationVector;
|
|
1467
|
+
const cipher = createDecipheriv(algorithm, key, initVector);
|
|
1468
|
+
cipher.setAutoPadding(false);
|
|
1469
|
+
const decrypted_chunks = [];
|
|
1470
|
+
decrypted_chunks.push(cipher.update(buffer));
|
|
1471
|
+
decrypted_chunks.push(cipher.final());
|
|
1472
|
+
return Buffer.concat(decrypted_chunks);
|
|
1386
1473
|
}
|
|
1387
|
-
function
|
|
1388
|
-
|
|
1474
|
+
function makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys) {
|
|
1475
|
+
assert6(message instanceof Buffer);
|
|
1476
|
+
assert6(derivedKeys.signingKey instanceof Buffer);
|
|
1477
|
+
assert6(typeof derivedKeys.sha1or256 === "string");
|
|
1478
|
+
assert6(derivedKeys.sha1or256 === "SHA1" || derivedKeys.sha1or256 === "SHA256");
|
|
1479
|
+
const signature = createHmac(derivedKeys.sha1or256, derivedKeys.signingKey).update(message).digest();
|
|
1480
|
+
assert6(signature.length === derivedKeys.signatureLength);
|
|
1481
|
+
return signature;
|
|
1389
1482
|
}
|
|
1390
|
-
function
|
|
1391
|
-
|
|
1483
|
+
function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
|
|
1484
|
+
const message = chunk.subarray(0, chunk.length - derivedKeys.signatureLength);
|
|
1485
|
+
const expectedSignature = chunk.subarray(chunk.length - derivedKeys.signatureLength);
|
|
1486
|
+
const computedSignature = makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys);
|
|
1487
|
+
return computedSignature.toString("hex") === expectedSignature.toString("hex");
|
|
1392
1488
|
}
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
reason: "Cannot find X409 Extension 3 in certificate"
|
|
1417
|
-
};
|
|
1418
|
-
}
|
|
1419
|
-
if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
|
|
1420
|
-
return {
|
|
1421
|
-
status: "BadCertificateInvalid",
|
|
1422
|
-
reason: "Cannot find X409 Extension 3 in certificate (parent)"
|
|
1423
|
-
};
|
|
1424
|
-
}
|
|
1425
|
-
if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
|
|
1426
|
-
return {
|
|
1427
|
-
status: "BadCertificateInvalid",
|
|
1428
|
-
reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
|
|
1429
|
-
};
|
|
1489
|
+
|
|
1490
|
+
// source/explore_asn1.ts
|
|
1491
|
+
function t(tag) {
|
|
1492
|
+
return TagType[tag];
|
|
1493
|
+
}
|
|
1494
|
+
function bi(blockInfo, depth) {
|
|
1495
|
+
const indent = " ".repeat(depth);
|
|
1496
|
+
const hl = blockInfo.position - blockInfo.start;
|
|
1497
|
+
return `${blockInfo.start.toString().padStart(5, " ")}:d=${depth} hl=${hl.toString().padEnd(3, " ")} l=${blockInfo.length.toString().padStart(6, " ")} ${blockInfo.tag.toString(16).padEnd(2, " ")} ${indent} ${t(blockInfo.tag)}`;
|
|
1498
|
+
}
|
|
1499
|
+
function exploreAsn1(buffer) {
|
|
1500
|
+
console.log(hexDump(buffer));
|
|
1501
|
+
function dump(offset, depth) {
|
|
1502
|
+
const blockInfo = readTag(buffer, offset);
|
|
1503
|
+
dumpBlock(blockInfo, depth);
|
|
1504
|
+
function dumpBlock(blockInfo2, depth2) {
|
|
1505
|
+
console.log(bi(blockInfo2, depth2));
|
|
1506
|
+
if (blockInfo2.tag === 48 /* SEQUENCE */ || blockInfo2.tag === 49 /* SET */ || blockInfo2.tag >= 160 /* CONTEXT_SPECIFIC0 */) {
|
|
1507
|
+
const blocks = readStruct(buffer, blockInfo2);
|
|
1508
|
+
for (const block of blocks) {
|
|
1509
|
+
dumpBlock(block, depth2 + 1);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1430
1512
|
}
|
|
1431
1513
|
}
|
|
1432
|
-
|
|
1433
|
-
status: "Good",
|
|
1434
|
-
reason: `certificate chain is valid(length = ${certificateChain.length})`
|
|
1435
|
-
};
|
|
1514
|
+
dump(0, 0);
|
|
1436
1515
|
}
|
|
1437
1516
|
|
|
1438
1517
|
// source/explore_certificate_revocation_list.ts
|
|
@@ -1440,20 +1519,20 @@ function readNameForCrl(buffer, block) {
|
|
|
1440
1519
|
return _readDirectoryName(buffer, block);
|
|
1441
1520
|
}
|
|
1442
1521
|
function _readTbsCertList(buffer, blockInfo) {
|
|
1443
|
-
const blocks =
|
|
1522
|
+
const blocks = readStruct(buffer, blockInfo);
|
|
1444
1523
|
const hasOptionalVersion = blocks[0].tag === 2 /* INTEGER */;
|
|
1445
1524
|
if (hasOptionalVersion) {
|
|
1446
1525
|
const version = _readIntegerValue(buffer, blocks[0]);
|
|
1447
|
-
const signature =
|
|
1526
|
+
const signature = readAlgorithmIdentifier(buffer, blocks[1]);
|
|
1448
1527
|
const issuer = readNameForCrl(buffer, blocks[2]);
|
|
1449
1528
|
const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[2])));
|
|
1450
1529
|
const thisUpdate = _readTime(buffer, blocks[3]);
|
|
1451
1530
|
const nextUpdate = _readTime(buffer, blocks[4]);
|
|
1452
1531
|
const revokedCertificates = [];
|
|
1453
1532
|
if (blocks[5] && blocks[5].tag < 128) {
|
|
1454
|
-
const list =
|
|
1533
|
+
const list = readStruct(buffer, blocks[5]);
|
|
1455
1534
|
for (const r of list) {
|
|
1456
|
-
const rr =
|
|
1535
|
+
const rr = readStruct(buffer, r);
|
|
1457
1536
|
const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
|
|
1458
1537
|
const revocationDate = _readTime(buffer, rr[1]);
|
|
1459
1538
|
revokedCertificates.push({
|
|
@@ -1465,16 +1544,16 @@ function _readTbsCertList(buffer, blockInfo) {
|
|
|
1465
1544
|
const ext0 = _findBlockAtIndex(blocks, 0);
|
|
1466
1545
|
return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
|
|
1467
1546
|
} else {
|
|
1468
|
-
const signature =
|
|
1547
|
+
const signature = readAlgorithmIdentifier(buffer, blocks[0]);
|
|
1469
1548
|
const issuer = readNameForCrl(buffer, blocks[1]);
|
|
1470
1549
|
const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[1])));
|
|
1471
1550
|
const thisUpdate = _readTime(buffer, blocks[2]);
|
|
1472
1551
|
const nextUpdate = _readTime(buffer, blocks[3]);
|
|
1473
1552
|
const revokedCertificates = [];
|
|
1474
1553
|
if (blocks[4] && blocks[4].tag < 128) {
|
|
1475
|
-
const list =
|
|
1554
|
+
const list = readStruct(buffer, blocks[4]);
|
|
1476
1555
|
for (const r of list) {
|
|
1477
|
-
const rr =
|
|
1556
|
+
const rr = readStruct(buffer, r);
|
|
1478
1557
|
const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
|
|
1479
1558
|
const revocationDate = _readTime(buffer, rr[1]);
|
|
1480
1559
|
revokedCertificates.push({
|
|
@@ -1488,17 +1567,17 @@ function _readTbsCertList(buffer, blockInfo) {
|
|
|
1488
1567
|
}
|
|
1489
1568
|
function exploreCertificateRevocationList(crl) {
|
|
1490
1569
|
const blockInfo = readTag(crl, 0);
|
|
1491
|
-
const blocks =
|
|
1570
|
+
const blocks = readStruct(crl, blockInfo);
|
|
1492
1571
|
const tbsCertList = _readTbsCertList(crl, blocks[0]);
|
|
1493
|
-
const signatureAlgorithm =
|
|
1494
|
-
const signatureValue =
|
|
1572
|
+
const signatureAlgorithm = readAlgorithmIdentifier(crl, blocks[1]);
|
|
1573
|
+
const signatureValue = readSignatureValueBin(crl, blocks[2]);
|
|
1495
1574
|
return { tbsCertList, signatureAlgorithm, signatureValue };
|
|
1496
1575
|
}
|
|
1497
1576
|
|
|
1498
1577
|
// source/explore_certificate_signing_request.ts
|
|
1499
1578
|
function _readExtensionRequest(buffer) {
|
|
1500
1579
|
const block = readTag(buffer, 0);
|
|
1501
|
-
const inner_blocks =
|
|
1580
|
+
const inner_blocks = readStruct(buffer, block);
|
|
1502
1581
|
const extensions = inner_blocks.map((block1) => _readExtension(buffer, block1));
|
|
1503
1582
|
const result = {};
|
|
1504
1583
|
for (const e of extensions) {
|
|
@@ -1508,204 +1587,39 @@ function _readExtensionRequest(buffer) {
|
|
|
1508
1587
|
return { basicConstraints, keyUsage, subjectAltName };
|
|
1509
1588
|
}
|
|
1510
1589
|
function readCertificationRequestInfo(buffer, block) {
|
|
1511
|
-
const blocks =
|
|
1590
|
+
const blocks = readStruct(buffer, block);
|
|
1512
1591
|
if (blocks.length === 4) {
|
|
1513
1592
|
const extensionRequestBlock = _findBlockAtIndex(blocks, 0);
|
|
1514
1593
|
if (!extensionRequestBlock) {
|
|
1515
1594
|
throw new Error("cannot find extensionRequest block");
|
|
1516
1595
|
}
|
|
1517
|
-
const blocks1 =
|
|
1518
|
-
const blocks2 =
|
|
1596
|
+
const blocks1 = readStruct(buffer, extensionRequestBlock);
|
|
1597
|
+
const blocks2 = readStruct(buffer, blocks1[0]);
|
|
1519
1598
|
const identifier = _readObjectIdentifier(buffer, blocks2[0]);
|
|
1520
1599
|
if (identifier.name !== "extensionRequest") {
|
|
1521
1600
|
throw new Error(" Cannot find extension Request in ASN1 block");
|
|
1522
1601
|
}
|
|
1523
1602
|
const buf = _getBlock(buffer, blocks2[1]);
|
|
1524
|
-
const extensionRequest = _readExtensionRequest(buf);
|
|
1525
|
-
return { extensionRequest };
|
|
1526
|
-
}
|
|
1527
|
-
throw new Error("Invalid CSR or ");
|
|
1528
|
-
}
|
|
1529
|
-
function exploreCertificateSigningRequest(crl) {
|
|
1530
|
-
const blockInfo = readTag(crl, 0);
|
|
1531
|
-
const blocks = _readStruct(crl, blockInfo);
|
|
1532
|
-
const csrInfo = readCertificationRequestInfo(crl, blocks[0]);
|
|
1533
|
-
return csrInfo;
|
|
1534
|
-
}
|
|
1535
|
-
|
|
1536
|
-
// source/explore_private_key.ts
|
|
1537
|
-
function f(buffer, b) {
|
|
1538
|
-
return buffer.subarray(b.position + 1, b.position + b.length);
|
|
1539
|
-
}
|
|
1540
|
-
var doDebug2 = !!process.env.DEBUG;
|
|
1541
|
-
function explorePrivateKey(privateKey2) {
|
|
1542
|
-
const privateKey1 = privateKey2.hidden;
|
|
1543
|
-
const privateKey = typeof privateKey1 === "string" ? convertPEMtoDER(privateKey1) : privateKey1.export({ format: "der", type: "pkcs1" });
|
|
1544
|
-
const block_info = readTag(privateKey, 0);
|
|
1545
|
-
const blocks = _readStruct(privateKey, block_info);
|
|
1546
|
-
if (blocks.length === 9) {
|
|
1547
|
-
const version2 = f(privateKey, blocks[0]);
|
|
1548
|
-
const modulus2 = f(privateKey, blocks[1]);
|
|
1549
|
-
const publicExponent2 = f(privateKey, blocks[2]);
|
|
1550
|
-
const privateExponent2 = f(privateKey, blocks[3]);
|
|
1551
|
-
const prime12 = f(privateKey, blocks[4]);
|
|
1552
|
-
const prime22 = f(privateKey, blocks[5]);
|
|
1553
|
-
const exponent12 = f(privateKey, blocks[6]);
|
|
1554
|
-
const exponent22 = f(privateKey, blocks[7]);
|
|
1555
|
-
return {
|
|
1556
|
-
version: version2,
|
|
1557
|
-
modulus: modulus2,
|
|
1558
|
-
publicExponent: publicExponent2,
|
|
1559
|
-
privateExponent: privateExponent2,
|
|
1560
|
-
prime1: prime12,
|
|
1561
|
-
prime2: prime22,
|
|
1562
|
-
exponent1: exponent12,
|
|
1563
|
-
exponent2: exponent22
|
|
1564
|
-
};
|
|
1565
|
-
}
|
|
1566
|
-
if (doDebug2) {
|
|
1567
|
-
console.log("-------------------- private key:");
|
|
1568
|
-
console.log(block_info);
|
|
1569
|
-
console.log(
|
|
1570
|
-
blocks.map((b2) => ({
|
|
1571
|
-
tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
|
|
1572
|
-
l: b2.length,
|
|
1573
|
-
p: b2.position,
|
|
1574
|
-
buff: privateKey.subarray(b2.position, b2.position + b2.length).toString("hex")
|
|
1575
|
-
}))
|
|
1576
|
-
);
|
|
1577
|
-
}
|
|
1578
|
-
const b = blocks[2];
|
|
1579
|
-
const bb = privateKey.subarray(b.position, b.position + b.length);
|
|
1580
|
-
const block_info1 = readTag(bb, 0);
|
|
1581
|
-
const blocks1 = _readStruct(bb, block_info1);
|
|
1582
|
-
if (doDebug2) {
|
|
1583
|
-
console.log(
|
|
1584
|
-
blocks1.map((b2) => ({
|
|
1585
|
-
tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
|
|
1586
|
-
l: b2.length,
|
|
1587
|
-
p: b2.position,
|
|
1588
|
-
buff: bb.subarray(b2.position, b2.position + b2.length).toString("hex")
|
|
1589
|
-
}))
|
|
1590
|
-
);
|
|
1591
|
-
}
|
|
1592
|
-
const version = f(bb, blocks1[0]);
|
|
1593
|
-
const modulus = f(bb, blocks1[1]);
|
|
1594
|
-
const publicExponent = f(bb, blocks1[2]);
|
|
1595
|
-
const privateExponent = f(bb, blocks1[3]);
|
|
1596
|
-
const prime1 = f(bb, blocks1[4]);
|
|
1597
|
-
const prime2 = f(bb, blocks1[5]);
|
|
1598
|
-
const exponent1 = f(bb, blocks1[6]);
|
|
1599
|
-
const exponent2 = f(bb, blocks1[7]);
|
|
1600
|
-
return {
|
|
1601
|
-
version,
|
|
1602
|
-
modulus,
|
|
1603
|
-
publicExponent,
|
|
1604
|
-
privateExponent,
|
|
1605
|
-
prime1,
|
|
1606
|
-
prime2,
|
|
1607
|
-
exponent1,
|
|
1608
|
-
exponent2
|
|
1609
|
-
};
|
|
1610
|
-
}
|
|
1611
|
-
|
|
1612
|
-
// source/public_private_match.ts
|
|
1613
|
-
function publicKeyAndPrivateKeyMatches(certificate, privateKey) {
|
|
1614
|
-
const i = exploreCertificate(certificate);
|
|
1615
|
-
const j = explorePrivateKey(privateKey);
|
|
1616
|
-
const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
|
|
1617
|
-
const modulus2 = j.modulus;
|
|
1618
|
-
if (modulus1.length != modulus2.length) {
|
|
1619
|
-
return false;
|
|
1620
|
-
}
|
|
1621
|
-
return modulus1.toString("hex") === modulus2.toString("hex");
|
|
1622
|
-
}
|
|
1623
|
-
function certificateMatchesPrivateKeyPEM(certificate, privateKey, blockSize) {
|
|
1624
|
-
const initialBuffer = Buffer.from("Lorem Ipsum");
|
|
1625
|
-
const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize);
|
|
1626
|
-
const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
|
|
1627
|
-
const finalString = decryptedBuffer.toString("utf-8");
|
|
1628
|
-
return initialBuffer.toString("utf-8") === finalString;
|
|
1629
|
-
}
|
|
1630
|
-
function certificateMatchesPrivateKey(certificate, privateKey) {
|
|
1631
|
-
const e = explorePrivateKey(privateKey);
|
|
1632
|
-
const blockSize = e.modulus.length;
|
|
1633
|
-
const certificatePEM = toPem(certificate, "CERTIFICATE");
|
|
1634
|
-
return certificateMatchesPrivateKeyPEM(certificatePEM, privateKey, blockSize);
|
|
1635
|
-
}
|
|
1636
|
-
|
|
1637
|
-
// source/x509/_crypto.ts
|
|
1638
|
-
import * as x509 from "@peculiar/x509";
|
|
1639
|
-
import { Crypto as PeculiarWebCrypto } from "@peculiar/webcrypto";
|
|
1640
|
-
import nativeCrypto from "crypto";
|
|
1641
|
-
import * as x5092 from "@peculiar/x509";
|
|
1642
|
-
var doDebug3 = false;
|
|
1643
|
-
var _crypto;
|
|
1644
|
-
var ignoreCrypto = process.env.IGNORE_SUBTLE_FROM_CRYPTO;
|
|
1645
|
-
if (typeof window === "undefined") {
|
|
1646
|
-
_crypto = nativeCrypto;
|
|
1647
|
-
if (!_crypto?.subtle || ignoreCrypto) {
|
|
1648
|
-
_crypto = new PeculiarWebCrypto();
|
|
1649
|
-
doDebug3 && console.warn("using @peculiar/webcrypto");
|
|
1650
|
-
} else {
|
|
1651
|
-
doDebug3 && console.warn("using nodejs crypto (native)");
|
|
1652
|
-
}
|
|
1653
|
-
x509.cryptoProvider.set(_crypto);
|
|
1654
|
-
} else {
|
|
1655
|
-
doDebug3 && console.warn("using browser crypto (native)");
|
|
1656
|
-
_crypto = crypto;
|
|
1657
|
-
x509.cryptoProvider.set(crypto);
|
|
1658
|
-
}
|
|
1659
|
-
function getCrypto() {
|
|
1660
|
-
return _crypto || crypto || __require("crypto");
|
|
1661
|
-
}
|
|
1662
|
-
|
|
1663
|
-
// source/x509/create_key_pair.ts
|
|
1664
|
-
async function generateKeyPair(modulusLength = 2048) {
|
|
1665
|
-
const crypto3 = getCrypto();
|
|
1666
|
-
const alg = {
|
|
1667
|
-
name: "RSASSA-PKCS1-v1_5",
|
|
1668
|
-
hash: { name: "SHA-256" },
|
|
1669
|
-
publicExponent: new Uint8Array([1, 0, 1]),
|
|
1670
|
-
modulusLength
|
|
1671
|
-
};
|
|
1672
|
-
const keys = await crypto3.subtle.generateKey(alg, true, ["sign", "verify"]);
|
|
1673
|
-
return keys;
|
|
1674
|
-
}
|
|
1675
|
-
async function generatePrivateKey(modulusLength = 2048) {
|
|
1676
|
-
return (await generateKeyPair(modulusLength)).privateKey;
|
|
1677
|
-
}
|
|
1678
|
-
async function privateKeyToPEM(privateKey) {
|
|
1679
|
-
const crypto3 = getCrypto();
|
|
1680
|
-
const privDer = await crypto3.subtle.exportKey("pkcs8", privateKey);
|
|
1681
|
-
const privPem = x5092.PemConverter.encode(privDer, "PRIVATE KEY");
|
|
1682
|
-
return { privPem, privDer };
|
|
1683
|
-
}
|
|
1684
|
-
async function derToPrivateKey(privDer) {
|
|
1685
|
-
const crypto3 = getCrypto();
|
|
1686
|
-
return await crypto3.subtle.importKey(
|
|
1687
|
-
"pkcs8",
|
|
1688
|
-
privDer,
|
|
1689
|
-
{
|
|
1690
|
-
name: "RSASSA-PKCS1-v1_5",
|
|
1691
|
-
hash: { name: "SHA-256" }
|
|
1692
|
-
},
|
|
1693
|
-
true,
|
|
1694
|
-
[
|
|
1695
|
-
"sign"
|
|
1696
|
-
// "encrypt",
|
|
1697
|
-
// "decrypt",
|
|
1698
|
-
// "verify",
|
|
1699
|
-
// "wrapKey",
|
|
1700
|
-
// "unwrapKey",
|
|
1701
|
-
// "deriveKey",
|
|
1702
|
-
// "deriveBits"
|
|
1703
|
-
]
|
|
1704
|
-
);
|
|
1603
|
+
const extensionRequest = _readExtensionRequest(buf);
|
|
1604
|
+
return { extensionRequest };
|
|
1605
|
+
}
|
|
1606
|
+
throw new Error("Invalid CSR or ");
|
|
1705
1607
|
}
|
|
1706
|
-
|
|
1707
|
-
const
|
|
1708
|
-
|
|
1608
|
+
function exploreCertificateSigningRequest(crl) {
|
|
1609
|
+
const blockInfo = readTag(crl, 0);
|
|
1610
|
+
const blocks = readStruct(crl, blockInfo);
|
|
1611
|
+
const csrInfo = readCertificationRequestInfo(crl, blocks[0]);
|
|
1612
|
+
return csrInfo;
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
// source/make_private_key_from_pem.ts
|
|
1616
|
+
function makePrivateKeyFromPem(privateKeyInPem) {
|
|
1617
|
+
return { hidden: privateKeyInPem };
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
// source/make_private_key_thumbprint.ts
|
|
1621
|
+
function makePrivateKeyThumbPrint(privateKey) {
|
|
1622
|
+
return Buffer.alloc(0);
|
|
1709
1623
|
}
|
|
1710
1624
|
|
|
1711
1625
|
// source/subject.ts
|
|
@@ -1790,11 +1704,180 @@ var Subject = class _Subject {
|
|
|
1790
1704
|
return this.toStringInternal("/");
|
|
1791
1705
|
}
|
|
1792
1706
|
toString() {
|
|
1793
|
-
const
|
|
1794
|
-
return
|
|
1707
|
+
const t2 = this.toStringForOPCUA();
|
|
1708
|
+
return t2 ? "/" + t2 : t2;
|
|
1795
1709
|
}
|
|
1796
1710
|
};
|
|
1797
1711
|
|
|
1712
|
+
// source/verify_certificate_signature.ts
|
|
1713
|
+
import { createVerify as createVerify2 } from "crypto";
|
|
1714
|
+
function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
|
|
1715
|
+
const block_info = readTag(certificateOrCrl, 0);
|
|
1716
|
+
const blocks = readStruct(certificateOrCrl, block_info);
|
|
1717
|
+
const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
|
|
1718
|
+
const signatureAlgorithm = readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
|
|
1719
|
+
const signatureValue = readSignatureValueBin(certificateOrCrl, blocks[2]);
|
|
1720
|
+
const p = split_der(parentCertificate)[0];
|
|
1721
|
+
const certPem = toPem(p, "CERTIFICATE");
|
|
1722
|
+
const verify = createVerify2(signatureAlgorithm.identifier);
|
|
1723
|
+
verify.update(bufferToBeSigned);
|
|
1724
|
+
verify.end();
|
|
1725
|
+
return verify.verify(certPem, signatureValue);
|
|
1726
|
+
}
|
|
1727
|
+
function verifyCertificateSignature(certificate, parentCertificate) {
|
|
1728
|
+
return verifyCertificateOrClrSignature(certificate, parentCertificate);
|
|
1729
|
+
}
|
|
1730
|
+
function verifyCertificateRevocationListSignature(certificateRevocationList, parentCertificate) {
|
|
1731
|
+
return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
|
|
1732
|
+
}
|
|
1733
|
+
async function verifyCertificateChain(certificateChain) {
|
|
1734
|
+
for (let index = 1; index < certificateChain.length; index++) {
|
|
1735
|
+
const cert = certificateChain[index - 1];
|
|
1736
|
+
const certParent = certificateChain[index];
|
|
1737
|
+
const certParentInfo = exploreCertificate(certParent);
|
|
1738
|
+
const keyUsage = certParentInfo.tbsCertificate.extensions.keyUsage;
|
|
1739
|
+
if (!keyUsage.keyCertSign) {
|
|
1740
|
+
return {
|
|
1741
|
+
status: "BadCertificateIssuerUseNotAllowed",
|
|
1742
|
+
reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing"
|
|
1743
|
+
};
|
|
1744
|
+
}
|
|
1745
|
+
const parentSignChild = verifyCertificateSignature(cert, certParent);
|
|
1746
|
+
if (!parentSignChild) {
|
|
1747
|
+
return {
|
|
1748
|
+
status: "BadCertificateInvalid",
|
|
1749
|
+
reason: "One of the certificate in the chain is not signing the previous certificate"
|
|
1750
|
+
};
|
|
1751
|
+
}
|
|
1752
|
+
const certInfo = exploreCertificate(cert);
|
|
1753
|
+
if (!certInfo.tbsCertificate.extensions) {
|
|
1754
|
+
return {
|
|
1755
|
+
status: "BadCertificateInvalid",
|
|
1756
|
+
reason: "Cannot find X409 Extension 3 in certificate"
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
|
|
1760
|
+
return {
|
|
1761
|
+
status: "BadCertificateInvalid",
|
|
1762
|
+
reason: "Cannot find X409 Extension 3 in certificate (parent)"
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1765
|
+
if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
|
|
1766
|
+
return {
|
|
1767
|
+
status: "BadCertificateInvalid",
|
|
1768
|
+
reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
|
|
1769
|
+
};
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
return {
|
|
1773
|
+
status: "Good",
|
|
1774
|
+
reason: `certificate chain is valid(length = ${certificateChain.length})`
|
|
1775
|
+
};
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
// source/x509/_crypto.ts
|
|
1779
|
+
import * as x509 from "@peculiar/x509";
|
|
1780
|
+
import { Crypto as PeculiarWebCrypto } from "@peculiar/webcrypto";
|
|
1781
|
+
import nativeCrypto from "crypto";
|
|
1782
|
+
import * as x5092 from "@peculiar/x509";
|
|
1783
|
+
var doDebug3 = false;
|
|
1784
|
+
var _crypto;
|
|
1785
|
+
var ignoreCrypto = process.env.IGNORE_SUBTLE_FROM_CRYPTO;
|
|
1786
|
+
if (typeof window === "undefined") {
|
|
1787
|
+
_crypto = nativeCrypto;
|
|
1788
|
+
if (!_crypto?.subtle || ignoreCrypto) {
|
|
1789
|
+
_crypto = new PeculiarWebCrypto();
|
|
1790
|
+
doDebug3 && console.warn("using @peculiar/webcrypto");
|
|
1791
|
+
} else {
|
|
1792
|
+
doDebug3 && console.warn("using nodejs crypto (native)");
|
|
1793
|
+
}
|
|
1794
|
+
x509.cryptoProvider.set(_crypto);
|
|
1795
|
+
} else {
|
|
1796
|
+
doDebug3 && console.warn("using browser crypto (native)");
|
|
1797
|
+
_crypto = crypto;
|
|
1798
|
+
x509.cryptoProvider.set(crypto);
|
|
1799
|
+
}
|
|
1800
|
+
function getCrypto() {
|
|
1801
|
+
return _crypto || crypto || __require("crypto");
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
// source/x509/create_key_pair.ts
|
|
1805
|
+
async function generateKeyPair(modulusLength = 2048) {
|
|
1806
|
+
const crypto3 = getCrypto();
|
|
1807
|
+
const alg = {
|
|
1808
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
1809
|
+
hash: { name: "SHA-256" },
|
|
1810
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
1811
|
+
modulusLength
|
|
1812
|
+
};
|
|
1813
|
+
const keys = await crypto3.subtle.generateKey(alg, true, ["sign", "verify"]);
|
|
1814
|
+
return keys;
|
|
1815
|
+
}
|
|
1816
|
+
async function generatePrivateKey(modulusLength = 2048) {
|
|
1817
|
+
return (await generateKeyPair(modulusLength)).privateKey;
|
|
1818
|
+
}
|
|
1819
|
+
async function privateKeyToPEM(privateKey) {
|
|
1820
|
+
const crypto3 = getCrypto();
|
|
1821
|
+
const privDer = await crypto3.subtle.exportKey("pkcs8", privateKey);
|
|
1822
|
+
const privPem = x5092.PemConverter.encode(privDer, "PRIVATE KEY");
|
|
1823
|
+
return { privPem, privDer };
|
|
1824
|
+
}
|
|
1825
|
+
async function derToPrivateKey(privDer) {
|
|
1826
|
+
const crypto3 = getCrypto();
|
|
1827
|
+
return await crypto3.subtle.importKey(
|
|
1828
|
+
"pkcs8",
|
|
1829
|
+
privDer,
|
|
1830
|
+
{
|
|
1831
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
1832
|
+
hash: { name: "SHA-256" }
|
|
1833
|
+
},
|
|
1834
|
+
true,
|
|
1835
|
+
[
|
|
1836
|
+
"sign"
|
|
1837
|
+
// "encrypt",
|
|
1838
|
+
// "decrypt",
|
|
1839
|
+
// "verify",
|
|
1840
|
+
// "wrapKey",
|
|
1841
|
+
// "unwrapKey",
|
|
1842
|
+
// "deriveKey",
|
|
1843
|
+
// "deriveBits"
|
|
1844
|
+
]
|
|
1845
|
+
);
|
|
1846
|
+
}
|
|
1847
|
+
async function pemToPrivateKey(pem) {
|
|
1848
|
+
const privDer = x5092.PemConverter.decode(pem);
|
|
1849
|
+
return derToPrivateKey(privDer[0]);
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
// source/x509/coerce_private_key.ts
|
|
1853
|
+
var crypto2 = getCrypto();
|
|
1854
|
+
var doDebug4 = false;
|
|
1855
|
+
function coercePEMorDerToPrivateKey(privateKeyInDerOrPem) {
|
|
1856
|
+
if (typeof privateKeyInDerOrPem === "string") {
|
|
1857
|
+
const hidden = createPrivateKeyFromNodeJSCrypto(privateKeyInDerOrPem);
|
|
1858
|
+
return { hidden };
|
|
1859
|
+
}
|
|
1860
|
+
throw new Error("not implemented");
|
|
1861
|
+
}
|
|
1862
|
+
async function _coercePrivateKey(privateKey) {
|
|
1863
|
+
const KeyObject4 = crypto2.KeyObject;
|
|
1864
|
+
if (privateKey instanceof Buffer) {
|
|
1865
|
+
const privateKey1 = await derToPrivateKey(privateKey);
|
|
1866
|
+
return KeyObject4.from(privateKey1);
|
|
1867
|
+
} else if (typeof privateKey === "string") {
|
|
1868
|
+
try {
|
|
1869
|
+
const privateKey1 = await pemToPrivateKey(privateKey);
|
|
1870
|
+
return KeyObject4.from(privateKey1);
|
|
1871
|
+
} catch (err) {
|
|
1872
|
+
doDebug4 && console.log(privateKey);
|
|
1873
|
+
throw err;
|
|
1874
|
+
}
|
|
1875
|
+
} else if (privateKey instanceof KeyObject4) {
|
|
1876
|
+
return privateKey;
|
|
1877
|
+
}
|
|
1878
|
+
throw new Error("Invalid privateKey");
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1798
1881
|
// source/x509/_get_attributes.ts
|
|
1799
1882
|
var keyUsageApplication = x5092.KeyUsageFlags.keyEncipherment | x5092.KeyUsageFlags.nonRepudiation | x5092.KeyUsageFlags.dataEncipherment | x5092.KeyUsageFlags.keyCertSign | x5092.KeyUsageFlags.digitalSignature;
|
|
1800
1883
|
var keyUsageCA = x5092.KeyUsageFlags.keyCertSign | x5092.KeyUsageFlags.cRLSign;
|
|
@@ -6060,69 +6143,12 @@ async function createSelfSignedCertificate({
|
|
|
6060
6143
|
return { cert: cert.toString("pem"), der: cert };
|
|
6061
6144
|
}
|
|
6062
6145
|
|
|
6063
|
-
// source/x509/coerce_private_key.ts
|
|
6064
|
-
var crypto2 = getCrypto();
|
|
6065
|
-
var doDebug4 = false;
|
|
6066
|
-
function coercePEMorDerToPrivateKey(privateKeyInDerOrPem) {
|
|
6067
|
-
if (typeof privateKeyInDerOrPem === "string") {
|
|
6068
|
-
const hidden = createPrivateKeyFromNodeJSCrypto(privateKeyInDerOrPem);
|
|
6069
|
-
return { hidden };
|
|
6070
|
-
}
|
|
6071
|
-
throw new Error("not implemented");
|
|
6072
|
-
}
|
|
6073
|
-
async function _coercePrivateKey(privateKey) {
|
|
6074
|
-
const KeyObject4 = crypto2.KeyObject;
|
|
6075
|
-
if (privateKey instanceof Buffer) {
|
|
6076
|
-
const privateKey1 = await derToPrivateKey(privateKey);
|
|
6077
|
-
return KeyObject4.from(privateKey1);
|
|
6078
|
-
} else if (typeof privateKey === "string") {
|
|
6079
|
-
try {
|
|
6080
|
-
const privateKey1 = await pemToPrivateKey(privateKey);
|
|
6081
|
-
return KeyObject4.from(privateKey1);
|
|
6082
|
-
} catch (err) {
|
|
6083
|
-
doDebug4 && console.log(privateKey);
|
|
6084
|
-
throw err;
|
|
6085
|
-
}
|
|
6086
|
-
} else if (privateKey instanceof KeyObject4) {
|
|
6087
|
-
return privateKey;
|
|
6088
|
-
}
|
|
6089
|
-
throw new Error("Invalid privateKey");
|
|
6090
|
-
}
|
|
6091
|
-
|
|
6092
|
-
// source/make_private_key_from_pem.ts
|
|
6093
|
-
function makePrivateKeyFromPem(privateKeyInPem) {
|
|
6094
|
-
return { hidden: privateKeyInPem };
|
|
6095
|
-
}
|
|
6096
|
-
|
|
6097
6146
|
export {
|
|
6098
6147
|
__dirname,
|
|
6099
|
-
createPrivateKeyFromNodeJSCrypto,
|
|
6100
|
-
isKeyObject,
|
|
6101
|
-
CertificatePurpose,
|
|
6102
|
-
TagType,
|
|
6103
6148
|
readTag,
|
|
6104
|
-
|
|
6105
|
-
|
|
6106
|
-
|
|
6107
|
-
formatBuffer2DigitHexWithColum,
|
|
6108
|
-
_readOctetString,
|
|
6109
|
-
_getBlock,
|
|
6110
|
-
_readIntegerAsByteString,
|
|
6111
|
-
_readListOfInteger,
|
|
6112
|
-
_readObjectIdentifier,
|
|
6113
|
-
_readAlgorithmIdentifier,
|
|
6114
|
-
_readECCAlgorithmIdentifier,
|
|
6115
|
-
_readSignatureValueBin,
|
|
6116
|
-
_readSignatureValue,
|
|
6117
|
-
_readLongIntegerValue,
|
|
6118
|
-
_readIntegerValue,
|
|
6119
|
-
_readBooleanValue,
|
|
6120
|
-
_readVersionValue,
|
|
6121
|
-
_readValue,
|
|
6122
|
-
compactDirectoryName,
|
|
6123
|
-
_readDirectoryName,
|
|
6124
|
-
_findBlockAtIndex,
|
|
6125
|
-
_readTime,
|
|
6149
|
+
readStruct,
|
|
6150
|
+
readAlgorithmIdentifier,
|
|
6151
|
+
readSignatureValueBin,
|
|
6126
6152
|
_readExtension,
|
|
6127
6153
|
readTbsCertificate,
|
|
6128
6154
|
exploreCertificate,
|
|
@@ -6148,6 +6174,19 @@ export {
|
|
|
6148
6174
|
coerceCertificatePem,
|
|
6149
6175
|
extractPublicKeyFromCertificateSync,
|
|
6150
6176
|
extractPublicKeyFromCertificate,
|
|
6177
|
+
explorePrivateKey,
|
|
6178
|
+
publicKeyAndPrivateKeyMatches,
|
|
6179
|
+
certificateMatchesPrivateKey,
|
|
6180
|
+
createPrivateKeyFromNodeJSCrypto,
|
|
6181
|
+
isKeyObject,
|
|
6182
|
+
CertificatePurpose,
|
|
6183
|
+
rsaLengthPrivateKey,
|
|
6184
|
+
toPem2,
|
|
6185
|
+
coercePrivateKeyPem,
|
|
6186
|
+
coercePublicKeyPem,
|
|
6187
|
+
coerceRsaPublicKeyPem,
|
|
6188
|
+
rsaLengthPublicKey,
|
|
6189
|
+
rsaLengthRsaPublicKey,
|
|
6151
6190
|
coerceCertificate,
|
|
6152
6191
|
exploreCertificateInfo,
|
|
6153
6192
|
makePseudoRandomBuffer,
|
|
@@ -6160,35 +6199,27 @@ export {
|
|
|
6160
6199
|
decryptBufferWithDerivedKeys,
|
|
6161
6200
|
makeMessageChunkSignatureWithDerivedKeys,
|
|
6162
6201
|
verifyChunkSignatureWithDerivedKeys,
|
|
6163
|
-
|
|
6164
|
-
toPem2,
|
|
6165
|
-
coercePrivateKeyPem,
|
|
6166
|
-
coercePublicKeyPem,
|
|
6167
|
-
coerceRsaPublicKeyPem,
|
|
6168
|
-
rsaLengthPublicKey,
|
|
6169
|
-
rsaLengthRsaPublicKey,
|
|
6170
|
-
verifyCertificateOrClrSignature,
|
|
6171
|
-
verifyCertificateSignature,
|
|
6172
|
-
verifyCertificateRevocationListSignature,
|
|
6173
|
-
verifyCertificateChain,
|
|
6202
|
+
exploreAsn1,
|
|
6174
6203
|
readNameForCrl,
|
|
6175
6204
|
exploreCertificateRevocationList,
|
|
6176
6205
|
readCertificationRequestInfo,
|
|
6177
6206
|
exploreCertificateSigningRequest,
|
|
6178
|
-
|
|
6179
|
-
|
|
6180
|
-
|
|
6207
|
+
makePrivateKeyFromPem,
|
|
6208
|
+
makePrivateKeyThumbPrint,
|
|
6209
|
+
Subject,
|
|
6210
|
+
verifyCertificateOrClrSignature,
|
|
6211
|
+
verifyCertificateSignature,
|
|
6212
|
+
verifyCertificateRevocationListSignature,
|
|
6213
|
+
verifyCertificateChain,
|
|
6181
6214
|
generateKeyPair,
|
|
6182
6215
|
generatePrivateKey,
|
|
6183
6216
|
privateKeyToPEM,
|
|
6184
6217
|
derToPrivateKey,
|
|
6185
6218
|
pemToPrivateKey,
|
|
6186
|
-
Subject,
|
|
6187
|
-
createCertificateSigningRequest,
|
|
6188
|
-
createSelfSignedCertificate,
|
|
6189
6219
|
coercePEMorDerToPrivateKey,
|
|
6190
6220
|
_coercePrivateKey,
|
|
6191
|
-
|
|
6221
|
+
createCertificateSigningRequest,
|
|
6222
|
+
createSelfSignedCertificate
|
|
6192
6223
|
};
|
|
6193
6224
|
/*! Bundled license information:
|
|
6194
6225
|
|
|
@@ -6258,4 +6289,4 @@ asn1js/build/index.es.js:
|
|
|
6258
6289
|
*
|
|
6259
6290
|
*)
|
|
6260
6291
|
*/
|
|
6261
|
-
//# sourceMappingURL=chunk-
|
|
6292
|
+
//# sourceMappingURL=chunk-2RCYFHGG.mjs.map
|