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.
Files changed (45) hide show
  1. package/LICENSE +23 -23
  2. package/README.md +119 -114
  3. package/dist/{chunk-WLW5XUML.mjs → chunk-2RCYFHGG.mjs} +755 -724
  4. package/dist/chunk-2RCYFHGG.mjs.map +1 -0
  5. package/dist/chunk-C7PROBPE.mjs +14 -0
  6. package/dist/chunk-C7PROBPE.mjs.map +1 -0
  7. package/dist/{chunk-UEV3YRUV.mjs → chunk-UH5AT3JE.mjs} +9 -15
  8. package/dist/chunk-UH5AT3JE.mjs.map +1 -0
  9. package/dist/index.d.mts +3 -2
  10. package/dist/index.d.ts +3 -2
  11. package/dist/index.js +553 -552
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +9 -52
  14. package/dist/index_web-C5Oeu9mq.d.mts +503 -0
  15. package/dist/index_web-D1qc4UN2.d.ts +503 -0
  16. package/dist/source/index.d.mts +11 -516
  17. package/dist/source/index.d.ts +11 -516
  18. package/dist/source/index.js +537 -529
  19. package/dist/source/index.js.map +1 -1
  20. package/dist/source/index.mjs +9 -50
  21. package/dist/source/index_web.d.mts +1 -1
  22. package/dist/source/index_web.d.ts +1 -1
  23. package/dist/source/index_web.js +525 -522
  24. package/dist/source/index_web.js.map +1 -1
  25. package/dist/source/index_web.mjs +5 -49
  26. package/dist/source_nodejs/index.d.mts +1 -2
  27. package/dist/source_nodejs/index.d.ts +1 -2
  28. package/dist/source_nodejs/index.js +33 -41
  29. package/dist/source_nodejs/index.js.map +1 -1
  30. package/dist/source_nodejs/index.mjs +3 -5
  31. package/package.json +9 -4
  32. package/dist/chunk-5NV4OKIV.mjs +0 -1
  33. package/dist/chunk-5NV4OKIV.mjs.map +0 -1
  34. package/dist/chunk-UEV3YRUV.mjs.map +0 -1
  35. package/dist/chunk-WLW5XUML.mjs.map +0 -1
  36. package/dist/index_web.d.mts +0 -4
  37. package/dist/index_web.d.ts +0 -4
  38. package/dist/index_web.js +0 -6365
  39. package/dist/index_web.js.map +0 -1
  40. package/dist/index_web.mjs +0 -192
  41. package/dist/index_web.mjs.map +0 -1
  42. package/index.mjs +0 -1
  43. package/index_web.ts +0 -1
  44. package/web.d.ts +0 -1
  45. package/web.mjs +0 -1
@@ -35,28 +35,8 @@ __export(index_web_exports, {
35
35
  RSA_PKCS1_OAEP_PADDING: () => RSA_PKCS1_OAEP_PADDING,
36
36
  RSA_PKCS1_PADDING: () => RSA_PKCS1_PADDING,
37
37
  Subject: () => Subject,
38
- TagType: () => TagType,
39
38
  _coercePrivateKey: () => _coercePrivateKey,
40
- _findBlockAtIndex: () => _findBlockAtIndex,
41
- _getBlock: () => _getBlock,
42
- _readAlgorithmIdentifier: () => _readAlgorithmIdentifier,
43
- _readBitString: () => _readBitString,
44
- _readBooleanValue: () => _readBooleanValue,
45
- _readDirectoryName: () => _readDirectoryName,
46
- _readECCAlgorithmIdentifier: () => _readECCAlgorithmIdentifier,
47
39
  _readExtension: () => _readExtension,
48
- _readIntegerAsByteString: () => _readIntegerAsByteString,
49
- _readIntegerValue: () => _readIntegerValue,
50
- _readListOfInteger: () => _readListOfInteger,
51
- _readLongIntegerValue: () => _readLongIntegerValue,
52
- _readObjectIdentifier: () => _readObjectIdentifier,
53
- _readOctetString: () => _readOctetString,
54
- _readSignatureValue: () => _readSignatureValue,
55
- _readSignatureValueBin: () => _readSignatureValueBin,
56
- _readStruct: () => _readStruct,
57
- _readTime: () => _readTime,
58
- _readValue: () => _readValue,
59
- _readVersionValue: () => _readVersionValue,
60
40
  certificateMatchesPrivateKey: () => certificateMatchesPrivateKey,
61
41
  coerceCertificate: () => coerceCertificate,
62
42
  coerceCertificatePem: () => coerceCertificatePem,
@@ -65,7 +45,6 @@ __export(index_web_exports, {
65
45
  coercePublicKeyPem: () => coercePublicKeyPem,
66
46
  coerceRsaPublicKeyPem: () => coerceRsaPublicKeyPem,
67
47
  combine_der: () => combine_der,
68
- compactDirectoryName: () => compactDirectoryName,
69
48
  computeDerivedKeys: () => computeDerivedKeys,
70
49
  computePaddingFooter: () => computePaddingFooter,
71
50
  convertPEMtoDER: () => convertPEMtoDER,
@@ -75,6 +54,7 @@ __export(index_web_exports, {
75
54
  decryptBufferWithDerivedKeys: () => decryptBufferWithDerivedKeys,
76
55
  derToPrivateKey: () => derToPrivateKey,
77
56
  encryptBufferWithDerivedKeys: () => encryptBufferWithDerivedKeys,
57
+ exploreAsn1: () => exploreAsn1,
78
58
  exploreCertificate: () => exploreCertificate,
79
59
  exploreCertificateInfo: () => exploreCertificateInfo,
80
60
  exploreCertificateRevocationList: () => exploreCertificateRevocationList,
@@ -82,7 +62,6 @@ __export(index_web_exports, {
82
62
  explorePrivateKey: () => explorePrivateKey,
83
63
  extractPublicKeyFromCertificate: () => extractPublicKeyFromCertificate,
84
64
  extractPublicKeyFromCertificateSync: () => extractPublicKeyFromCertificateSync,
85
- formatBuffer2DigitHexWithColum: () => formatBuffer2DigitHexWithColum,
86
65
  generateKeyPair: () => generateKeyPair,
87
66
  generatePrivateKey: () => generatePrivateKey,
88
67
  hexDump: () => hexDump,
@@ -91,9 +70,9 @@ __export(index_web_exports, {
91
70
  makeMessageChunkSignature: () => makeMessageChunkSignature,
92
71
  makeMessageChunkSignatureWithDerivedKeys: () => makeMessageChunkSignatureWithDerivedKeys,
93
72
  makePrivateKeyFromPem: () => makePrivateKeyFromPem,
73
+ makePrivateKeyThumbPrint: () => makePrivateKeyThumbPrint,
94
74
  makePseudoRandomBuffer: () => makePseudoRandomBuffer,
95
75
  makeSHA1Thumbprint: () => makeSHA1Thumbprint,
96
- parseBitString: () => parseBitString,
97
76
  pemToPrivateKey: () => pemToPrivateKey,
98
77
  privateDecrypt: () => privateDecrypt,
99
78
  privateDecrypt_long: () => privateDecrypt_long,
@@ -105,7 +84,6 @@ __export(index_web_exports, {
105
84
  publicKeyAndPrivateKeyMatches: () => publicKeyAndPrivateKeyMatches,
106
85
  readCertificationRequestInfo: () => readCertificationRequestInfo,
107
86
  readNameForCrl: () => readNameForCrl,
108
- readTag: () => readTag,
109
87
  readTbsCertificate: () => readTbsCertificate,
110
88
  reduceLength: () => reduceLength,
111
89
  removePadding: () => removePadding,
@@ -126,42 +104,6 @@ __export(index_web_exports, {
126
104
  });
127
105
  module.exports = __toCommonJS(index_web_exports);
128
106
 
129
- // source/common.ts
130
- var import_crypto = __toESM(require("crypto"));
131
- var KeyObjectOrig = import_crypto.default.KeyObject;
132
- var { createPrivateKey: createPrivateKeyFromNodeJSCrypto } = import_crypto.default;
133
- function isKeyObject(mayBeKeyObject) {
134
- if (KeyObjectOrig) {
135
- return mayBeKeyObject instanceof KeyObjectOrig;
136
- }
137
- return typeof mayBeKeyObject === "object" && typeof mayBeKeyObject.type === "string";
138
- }
139
- var CertificatePurpose = /* @__PURE__ */ ((CertificatePurpose2) => {
140
- CertificatePurpose2[CertificatePurpose2["NotSpecified"] = 0] = "NotSpecified";
141
- CertificatePurpose2[CertificatePurpose2["ForCertificateAuthority"] = 1] = "ForCertificateAuthority";
142
- CertificatePurpose2[CertificatePurpose2["ForApplication"] = 2] = "ForApplication";
143
- CertificatePurpose2[CertificatePurpose2["ForUserAuthentication"] = 3] = "ForUserAuthentication";
144
- return CertificatePurpose2;
145
- })(CertificatePurpose || {});
146
-
147
- // source/derived_keys.ts
148
- var import_assert5 = __toESM(require("assert"));
149
- var import_crypto3 = require("crypto");
150
-
151
- // source/buffer_utils.ts
152
- var createFastUninitializedBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe : (size) => {
153
- return new Buffer(size);
154
- };
155
-
156
- // source/crypto_utils.ts
157
- var import_constants = __toESM(require("constants"));
158
- var import_assert3 = __toESM(require("assert"));
159
- var import_crypto2 = require("crypto");
160
- var import_hexy = __toESM(require("hexy"));
161
-
162
- // source/crypto_explore_certificate.ts
163
- var import_assert2 = __toESM(require("assert"));
164
-
165
107
  // source/asn1.ts
166
108
  var import_assert = __toESM(require("assert"));
167
109
 
@@ -229,6 +171,7 @@ var oid_map = {
229
171
  "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" },
230
172
  "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" },
231
173
  "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)" },
174
+ "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" },
232
175
  "1.3.6.1.5.5.7.3.17": { d: "1.3.6.1.5.5.7.3.17", c: "Internet Key Exchange (IKE)" },
233
176
  "1.3.6.1.5.5.7.3.1": { d: "serverAuth", c: "PKIX key purpose" },
234
177
  "1.3.6.1.5.5.7.3.2": { d: "clientAuth", c: "PKIX key purpose" },
@@ -488,12 +431,15 @@ var TagType = /* @__PURE__ */ ((TagType3) => {
488
431
  TagType3[TagType3["BMPString"] = 30] = "BMPString";
489
432
  TagType3[TagType3["SEQUENCE"] = 48] = "SEQUENCE";
490
433
  TagType3[TagType3["SET"] = 49] = "SET";
491
- TagType3[TagType3["A3"] = 163] = "A3";
434
+ TagType3[TagType3["CONTEXT_SPECIFIC0"] = 160] = "CONTEXT_SPECIFIC0";
435
+ TagType3[TagType3["CONTEXT_SPECIFIC1"] = 161] = "CONTEXT_SPECIFIC1";
436
+ TagType3[TagType3["CONTEXT_SPECIFIC2"] = 162] = "CONTEXT_SPECIFIC2";
437
+ TagType3[TagType3["CONTEXT_SPECIFIC3"] = 163] = "CONTEXT_SPECIFIC3";
438
+ TagType3[TagType3["A4"] = 164] = "A4";
492
439
  return TagType3;
493
440
  })(TagType || {});
494
441
  function readTag(buf, pos) {
495
- (0, import_assert.default)(buf instanceof Buffer);
496
- (0, import_assert.default)(Number.isFinite(pos) && pos >= 0);
442
+ const start = pos;
497
443
  if (buf.length <= pos) {
498
444
  throw new Error("Invalid position : buf.length=" + buf.length + " pos =" + pos);
499
445
  }
@@ -509,9 +455,9 @@ function readTag(buf, pos) {
509
455
  pos += 1;
510
456
  }
511
457
  }
512
- return { tag, position: pos, length };
458
+ return { start, tag, position: pos, length };
513
459
  }
514
- function _readStruct(buf, blockInfo) {
460
+ function readStruct(buf, blockInfo) {
515
461
  const length = blockInfo.length;
516
462
  let cursor = blockInfo.position;
517
463
  const end = blockInfo.position + length;
@@ -573,9 +519,9 @@ function _readIntegerAsByteString(buffer, block) {
573
519
  }
574
520
  function _readListOfInteger(buffer) {
575
521
  const block = readTag(buffer, 0);
576
- const inner_blocks = _readStruct(buffer, block);
577
- return inner_blocks.map((bblock) => {
578
- return _readIntegerAsByteString(buffer, bblock);
522
+ const inner_blocks = readStruct(buffer, block);
523
+ return inner_blocks.map((innerBlock) => {
524
+ return _readIntegerAsByteString(buffer, innerBlock);
579
525
  });
580
526
  }
581
527
  function parseOID(buffer, start, end) {
@@ -607,24 +553,24 @@ function _readObjectIdentifier(buffer, block) {
607
553
  name: oid_map[oid] ? oid_map[oid].d : oid
608
554
  };
609
555
  }
610
- function _readAlgorithmIdentifier(buffer, block) {
611
- const inner_blocks = _readStruct(buffer, block);
556
+ function readAlgorithmIdentifier(buffer, block) {
557
+ const inner_blocks = readStruct(buffer, block);
612
558
  return {
613
559
  identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name
614
560
  };
615
561
  }
616
562
  function _readECCAlgorithmIdentifier(buffer, block) {
617
- const inner_blocks = _readStruct(buffer, block);
563
+ const inner_blocks = readStruct(buffer, block);
618
564
  return {
619
565
  identifier: _readObjectIdentifier(buffer, inner_blocks[1]).name
620
566
  // difference with RSA as algorithm is second element of nested block
621
567
  };
622
568
  }
623
- function _readSignatureValueBin(buffer, block) {
569
+ function readSignatureValueBin(buffer, block) {
624
570
  return _readBitString(buffer, block).data;
625
571
  }
626
- function _readSignatureValue(buffer, block) {
627
- return _readSignatureValueBin(buffer, block).toString("hex");
572
+ function readSignatureValue(buffer, block) {
573
+ return readSignatureValueBin(buffer, block).toString("hex");
628
574
  }
629
575
  function _readLongIntegerValue(buffer, block) {
630
576
  (0, import_assert.default)(block.tag === 2 /* INTEGER */, "expecting a INTEGER tag");
@@ -705,18 +651,15 @@ function _readValue(buffer, block) {
705
651
  throw new Error("Invalid tag 0x" + block.tag.toString(16));
706
652
  }
707
653
  }
708
- function compactDirectoryName(d) {
709
- return JSON.stringify(d);
710
- }
711
654
  function _readDirectoryName(buffer, block) {
712
- const set_blocks = _readStruct(buffer, block);
655
+ const set_blocks = readStruct(buffer, block);
713
656
  const names = {};
714
657
  for (const set_block of set_blocks) {
715
658
  (0, import_assert.default)(set_block.tag === 49);
716
- const blocks = _readStruct(buffer, set_block);
659
+ const blocks = readStruct(buffer, set_block);
717
660
  (0, import_assert.default)(blocks.length === 1);
718
661
  (0, import_assert.default)(blocks[0].tag === 48);
719
- const sequenceBlock = _readStruct(buffer, blocks[0]);
662
+ const sequenceBlock = readStruct(buffer, blocks[0]);
720
663
  (0, import_assert.default)(sequenceBlock.length === 2);
721
664
  const type = _readObjectIdentifier(buffer, sequenceBlock[0]);
722
665
  names[type.name] = _readValue(buffer, sequenceBlock[1]);
@@ -734,11 +677,23 @@ function _readTime(buffer, block) {
734
677
  return _readValue(buffer, block);
735
678
  }
736
679
 
680
+ // source/crypto_utils.ts
681
+ var import_constants = __toESM(require("constants"));
682
+ var import_assert3 = __toESM(require("assert"));
683
+ var import_crypto = require("crypto");
684
+ var import_hexy = __toESM(require("hexy"));
685
+
686
+ // source/buffer_utils.ts
687
+ var createFastUninitializedBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe : (size) => {
688
+ return new Buffer(size);
689
+ };
690
+
737
691
  // source/crypto_explore_certificate.ts
692
+ var import_assert2 = __toESM(require("assert"));
738
693
  var doDebug = false;
739
694
  function _readAttributeTypeAndValue(buffer, block) {
740
- let inner_blocks = _readStruct(buffer, block);
741
- inner_blocks = _readStruct(buffer, inner_blocks[0]);
695
+ let inner_blocks = readStruct(buffer, block);
696
+ inner_blocks = readStruct(buffer, inner_blocks[0]);
742
697
  const data = {
743
698
  identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name,
744
699
  value: _readValue(buffer, inner_blocks[1])
@@ -750,7 +705,7 @@ function _readAttributeTypeAndValue(buffer, block) {
750
705
  return result;
751
706
  }
752
707
  function _readRelativeDistinguishedName(buffer, block) {
753
- const inner_blocks = _readStruct(buffer, block);
708
+ const inner_blocks = readStruct(buffer, block);
754
709
  const data = inner_blocks.map((block2) => _readAttributeTypeAndValue(buffer, block2));
755
710
  const result = {};
756
711
  for (const e of data) {
@@ -762,7 +717,7 @@ function _readName(buffer, block) {
762
717
  return _readRelativeDistinguishedName(buffer, block);
763
718
  }
764
719
  function _readValidity(buffer, block) {
765
- const inner_blocks = _readStruct(buffer, block);
720
+ const inner_blocks = readStruct(buffer, block);
766
721
  return {
767
722
  notBefore: _readTime(buffer, inner_blocks[0]),
768
723
  notAfter: _readTime(buffer, inner_blocks[1])
@@ -770,27 +725,27 @@ function _readValidity(buffer, block) {
770
725
  }
771
726
  function _readAuthorityKeyIdentifier(buffer) {
772
727
  const block_info = readTag(buffer, 0);
773
- const blocks = _readStruct(buffer, block_info);
728
+ const blocks = readStruct(buffer, block_info);
774
729
  const keyIdentifier_block = _findBlockAtIndex(blocks, 0);
775
730
  const authorityCertIssuer_block = _findBlockAtIndex(blocks, 1);
776
731
  const authorityCertSerialNumber_block = _findBlockAtIndex(blocks, 2);
777
732
  function _readAuthorityCertIssuer(block) {
778
- const inner_blocks = _readStruct(buffer, block);
733
+ const inner_blocks = readStruct(buffer, block);
779
734
  const directoryName_block = _findBlockAtIndex(inner_blocks, 4);
780
735
  if (directoryName_block) {
781
- const a = _readStruct(buffer, directoryName_block);
736
+ const a = readStruct(buffer, directoryName_block);
782
737
  return _readDirectoryName(buffer, a[0]);
783
738
  } else {
784
739
  throw new Error("Invalid _readAuthorityCertIssuer");
785
740
  }
786
741
  }
787
742
  function _readAuthorityCertIssuerFingerPrint(block) {
788
- const inner_blocks = _readStruct(buffer, block);
743
+ const inner_blocks = readStruct(buffer, block);
789
744
  const directoryName_block = _findBlockAtIndex(inner_blocks, 4);
790
745
  if (!directoryName_block) {
791
746
  return "";
792
747
  }
793
- const a = _readStruct(buffer, directoryName_block);
748
+ const a = readStruct(buffer, directoryName_block);
794
749
  if (a.length < 1) {
795
750
  return "";
796
751
  }
@@ -809,7 +764,7 @@ function _readAuthorityKeyIdentifier(buffer) {
809
764
  }
810
765
  function readBasicConstraint2_5_29_19(buffer, block) {
811
766
  const block_info = readTag(buffer, 0);
812
- const inner_blocks = _readStruct(buffer, block_info).slice(0, 2);
767
+ const inner_blocks = readStruct(buffer, block_info).slice(0, 2);
813
768
  let cA = false;
814
769
  let pathLengthConstraint = 0;
815
770
  let breakControl = 0;
@@ -838,9 +793,10 @@ function _readGeneralNames(buffer, block) {
838
793
  5: { name: "ediPartyName", type: "EDIPartyName" },
839
794
  6: { name: "uniformResourceIdentifier", type: "IA5String" },
840
795
  7: { name: "iPAddress", type: "OCTET_STRING" },
841
- 8: { name: "registeredID", type: "OBJECT_IDENTIFIER" }
796
+ 8: { name: "registeredID", type: "OBJECT_IDENTIFIER" },
797
+ 32: { name: "otherName", type: "AnotherName" }
842
798
  };
843
- const blocks = _readStruct(buffer, block);
799
+ const blocks = readStruct(buffer, block);
844
800
  function _readFromType(buffer2, block2, type) {
845
801
  switch (type) {
846
802
  case "IA5String":
@@ -852,13 +808,28 @@ function _readGeneralNames(buffer, block) {
852
808
  const n = {};
853
809
  for (const block2 of blocks) {
854
810
  (0, import_assert2.default)((block2.tag & 128) === 128);
855
- const t = block2.tag & 127;
856
- const type = _data[t];
811
+ const t2 = block2.tag & 127;
812
+ const type = _data[t2];
857
813
  if (!type) {
858
- throw new Error(" INVALID TYPE => " + t + "0x" + t.toString(16));
814
+ console.log("_readGeneralNames: INVALID TYPE => " + t2 + " 0x" + t2.toString(16));
815
+ continue;
816
+ }
817
+ if (t2 == 32) {
818
+ n[type.name] = n[type.name] || [];
819
+ const blocks2 = readStruct(buffer, block2);
820
+ const name = _readObjectIdentifier(buffer, blocks2[0]).name;
821
+ const buf = _getBlock(buffer, blocks2[1]);
822
+ const b = readTag(buf, 0);
823
+ const nn = _readValue(buf, b);
824
+ const data = {
825
+ identifier: name,
826
+ value: nn
827
+ };
828
+ n[type.name].push(data.value);
829
+ } else {
830
+ n[type.name] = n[type.name] || [];
831
+ n[type.name].push(_readFromType(buffer, block2, type.type));
859
832
  }
860
- n[type.name] = n[type.name] || [];
861
- n[type.name].push(_readFromType(buffer, block2, type.type));
862
833
  }
863
834
  return n;
864
835
  }
@@ -898,7 +869,7 @@ function readKeyUsage(oid, buffer) {
898
869
  function readExtKeyUsage(oid, buffer) {
899
870
  (0, import_assert2.default)(oid === "2.5.29.37");
900
871
  const block_info = readTag(buffer, 0);
901
- const inner_blocks = _readStruct(buffer, block_info);
872
+ const inner_blocks = readStruct(buffer, block_info);
902
873
  const extKeyUsage = {
903
874
  serverAuth: false,
904
875
  clientAuth: false,
@@ -918,13 +889,13 @@ function readExtKeyUsage(oid, buffer) {
918
889
  }
919
890
  function _readSubjectPublicKey(buffer) {
920
891
  const block_info = readTag(buffer, 0);
921
- const blocks = _readStruct(buffer, block_info);
892
+ const blocks = readStruct(buffer, block_info);
922
893
  return {
923
894
  modulus: buffer.subarray(blocks[0].position + 1, blocks[0].position + blocks[0].length)
924
895
  };
925
896
  }
926
897
  function _readExtension(buffer, block) {
927
- const inner_blocks = _readStruct(buffer, block);
898
+ const inner_blocks = readStruct(buffer, block);
928
899
  if (inner_blocks.length === 3) {
929
900
  (0, import_assert2.default)(inner_blocks[1].tag === 1 /* BOOLEAN */);
930
901
  inner_blocks[1] = inner_blocks[2];
@@ -964,8 +935,8 @@ function _readExtension(buffer, block) {
964
935
  }
965
936
  function _readExtensions(buffer, block) {
966
937
  (0, import_assert2.default)(block.tag === 163);
967
- let inner_blocks = _readStruct(buffer, block);
968
- inner_blocks = _readStruct(buffer, inner_blocks[0]);
938
+ let inner_blocks = readStruct(buffer, block);
939
+ inner_blocks = readStruct(buffer, inner_blocks[0]);
969
940
  const extensions = inner_blocks.map((block2) => _readExtension(buffer, block2));
970
941
  const result = {};
971
942
  for (const e of extensions) {
@@ -974,8 +945,8 @@ function _readExtensions(buffer, block) {
974
945
  return result;
975
946
  }
976
947
  function _readSubjectPublicKeyInfo(buffer, block) {
977
- const inner_blocks = _readStruct(buffer, block);
978
- const algorithm = _readAlgorithmIdentifier(buffer, inner_blocks[0]);
948
+ const inner_blocks = readStruct(buffer, block);
949
+ const algorithm = readAlgorithmIdentifier(buffer, inner_blocks[0]);
979
950
  const subjectPublicKey = _readBitString(buffer, inner_blocks[1]);
980
951
  const data = subjectPublicKey.data;
981
952
  const values = _readListOfInteger(data);
@@ -988,7 +959,7 @@ function _readSubjectPublicKeyInfo(buffer, block) {
988
959
  };
989
960
  }
990
961
  function _readSubjectECCPublicKeyInfo(buffer, block) {
991
- const inner_blocks = _readStruct(buffer, block);
962
+ const inner_blocks = readStruct(buffer, block);
992
963
  const algorithm = _readECCAlgorithmIdentifier(buffer, inner_blocks[0]);
993
964
  const subjectPublicKey = _readBitString(buffer, inner_blocks[1]);
994
965
  const data = subjectPublicKey.data;
@@ -1001,13 +972,13 @@ function _readSubjectECCPublicKeyInfo(buffer, block) {
1001
972
  };
1002
973
  }
1003
974
  function readTbsCertificate(buffer, block) {
1004
- const blocks = _readStruct(buffer, block);
975
+ const blocks = readStruct(buffer, block);
1005
976
  let version, serialNumber, signature, issuer, validity, subject, subjectFingerPrint, extensions;
1006
977
  let subjectPublicKeyInfo;
1007
978
  if (blocks.length === 6) {
1008
979
  version = 1;
1009
980
  serialNumber = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, blocks[0]));
1010
- signature = _readAlgorithmIdentifier(buffer, blocks[1]);
981
+ signature = readAlgorithmIdentifier(buffer, blocks[1]);
1011
982
  issuer = _readName(buffer, blocks[2]);
1012
983
  validity = _readValidity(buffer, blocks[3]);
1013
984
  subject = _readName(buffer, blocks[4]);
@@ -1021,13 +992,13 @@ function readTbsCertificate(buffer, block) {
1021
992
  }
1022
993
  version = _readVersionValue(buffer, version_block) + 1;
1023
994
  serialNumber = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, blocks[1]));
1024
- signature = _readAlgorithmIdentifier(buffer, blocks[2]);
995
+ signature = readAlgorithmIdentifier(buffer, blocks[2]);
1025
996
  issuer = _readName(buffer, blocks[3]);
1026
997
  validity = _readValidity(buffer, blocks[4]);
1027
998
  subject = _readName(buffer, blocks[5]);
1028
999
  subjectFingerPrint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[5])));
1029
- const inner_block = _readStruct(buffer, blocks[6]);
1030
- const what_type = _readAlgorithmIdentifier(buffer, inner_block[0]).identifier;
1000
+ const inner_block = readStruct(buffer, blocks[6]);
1001
+ const what_type = readAlgorithmIdentifier(buffer, inner_block[0]).identifier;
1031
1002
  switch (what_type) {
1032
1003
  case "rsaEncryption": {
1033
1004
  subjectPublicKeyInfo = _readSubjectPublicKeyInfo(buffer, blocks[6]);
@@ -1063,11 +1034,11 @@ function exploreCertificate(certificate) {
1063
1034
  (0, import_assert2.default)(certificate instanceof Buffer);
1064
1035
  if (!certificate._exploreCertificate_cache) {
1065
1036
  const block_info = readTag(certificate, 0);
1066
- const blocks = _readStruct(certificate, block_info);
1037
+ const blocks = readStruct(certificate, block_info);
1067
1038
  certificate._exploreCertificate_cache = {
1068
1039
  tbsCertificate: readTbsCertificate(certificate, blocks[0]),
1069
- signatureAlgorithm: _readAlgorithmIdentifier(certificate, blocks[1]),
1070
- signatureValue: _readSignatureValue(certificate, blocks[2])
1040
+ signatureAlgorithm: readAlgorithmIdentifier(certificate, blocks[1]),
1041
+ signatureValue: readSignatureValue(certificate, blocks[2])
1071
1042
  };
1072
1043
  }
1073
1044
  return certificate._exploreCertificate_cache;
@@ -1158,19 +1129,19 @@ function hexDump(buffer, width) {
1158
1129
  }
1159
1130
  }
1160
1131
  function makeMessageChunkSignature(chunk, options) {
1161
- const signer = (0, import_crypto2.createSign)(options.algorithm);
1132
+ const signer = (0, import_crypto.createSign)(options.algorithm);
1162
1133
  signer.update(chunk);
1163
1134
  const signature = signer.sign(options.privateKey.hidden);
1164
1135
  (0, import_assert3.default)(!options.signatureLength || signature.length === options.signatureLength);
1165
1136
  return signature;
1166
1137
  }
1167
1138
  function verifyMessageChunkSignature(blockToVerify, signature, options) {
1168
- const verify = (0, import_crypto2.createVerify)(options.algorithm);
1139
+ const verify = (0, import_crypto.createVerify)(options.algorithm);
1169
1140
  verify.update(blockToVerify);
1170
1141
  return verify.verify(options.publicKey, signature);
1171
1142
  }
1172
1143
  function makeSHA1Thumbprint(buffer) {
1173
- return (0, import_crypto2.createHash)("sha1").update(buffer).digest();
1144
+ return (0, import_crypto.createHash)("sha1").update(buffer).digest();
1174
1145
  }
1175
1146
  var RSA_PKCS1_OAEP_PADDING = import_constants.default.RSA_PKCS1_OAEP_PADDING;
1176
1147
  var RSA_PKCS1_PADDING = import_constants.default.RSA_PKCS1_PADDING;
@@ -1185,7 +1156,7 @@ function publicEncrypt_native(buffer, publicKey, algorithm) {
1185
1156
  if (algorithm === void 0) {
1186
1157
  algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
1187
1158
  }
1188
- return (0, import_crypto2.publicEncrypt)(
1159
+ return (0, import_crypto.publicEncrypt)(
1189
1160
  {
1190
1161
  key: publicKey,
1191
1162
  padding: algorithm
@@ -1198,7 +1169,7 @@ function privateDecrypt_native(buffer, privateKey, algorithm) {
1198
1169
  algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
1199
1170
  }
1200
1171
  try {
1201
- return (0, import_crypto2.privateDecrypt)(
1172
+ return (0, import_crypto.privateDecrypt)(
1202
1173
  {
1203
1174
  key: privateKey.hidden,
1204
1175
  padding: algorithm
@@ -1280,13 +1251,192 @@ function extractPublicKeyFromCertificate(certificate, callback) {
1280
1251
  });
1281
1252
  }
1282
1253
 
1283
- // source/explore_certificate.ts
1254
+ // source/explore_private_key.ts
1255
+ function f(buffer, b) {
1256
+ return buffer.subarray(b.position + 1, b.position + b.length);
1257
+ }
1258
+ var doDebug2 = !!process.env.DEBUG;
1259
+ function explorePrivateKey(privateKey2) {
1260
+ const privateKey1 = privateKey2.hidden;
1261
+ const privateKey = typeof privateKey1 === "string" ? convertPEMtoDER(privateKey1) : privateKey1.export({ format: "der", type: "pkcs1" });
1262
+ const block_info = readTag(privateKey, 0);
1263
+ const blocks = readStruct(privateKey, block_info);
1264
+ if (blocks.length === 9) {
1265
+ const version2 = f(privateKey, blocks[0]);
1266
+ const modulus2 = f(privateKey, blocks[1]);
1267
+ const publicExponent2 = f(privateKey, blocks[2]);
1268
+ const privateExponent2 = f(privateKey, blocks[3]);
1269
+ const prime12 = f(privateKey, blocks[4]);
1270
+ const prime22 = f(privateKey, blocks[5]);
1271
+ const exponent12 = f(privateKey, blocks[6]);
1272
+ const exponent22 = f(privateKey, blocks[7]);
1273
+ return {
1274
+ version: version2,
1275
+ modulus: modulus2,
1276
+ publicExponent: publicExponent2,
1277
+ privateExponent: privateExponent2,
1278
+ prime1: prime12,
1279
+ prime2: prime22,
1280
+ exponent1: exponent12,
1281
+ exponent2: exponent22
1282
+ };
1283
+ }
1284
+ if (doDebug2) {
1285
+ console.log("-------------------- private key:");
1286
+ console.log(block_info);
1287
+ console.log(
1288
+ blocks.map((b2) => ({
1289
+ tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
1290
+ l: b2.length,
1291
+ p: b2.position,
1292
+ buff: privateKey.subarray(b2.position, b2.position + b2.length).toString("hex")
1293
+ }))
1294
+ );
1295
+ }
1296
+ const b = blocks[2];
1297
+ const bb = privateKey.subarray(b.position, b.position + b.length);
1298
+ const block_info1 = readTag(bb, 0);
1299
+ const blocks1 = readStruct(bb, block_info1);
1300
+ if (doDebug2) {
1301
+ console.log(
1302
+ blocks1.map((b2) => ({
1303
+ tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
1304
+ l: b2.length,
1305
+ p: b2.position,
1306
+ buff: bb.subarray(b2.position, b2.position + b2.length).toString("hex")
1307
+ }))
1308
+ );
1309
+ }
1310
+ const version = f(bb, blocks1[0]);
1311
+ const modulus = f(bb, blocks1[1]);
1312
+ const publicExponent = f(bb, blocks1[2]);
1313
+ const privateExponent = f(bb, blocks1[3]);
1314
+ const prime1 = f(bb, blocks1[4]);
1315
+ const prime2 = f(bb, blocks1[5]);
1316
+ const exponent1 = f(bb, blocks1[6]);
1317
+ const exponent2 = f(bb, blocks1[7]);
1318
+ return {
1319
+ version,
1320
+ modulus,
1321
+ publicExponent,
1322
+ privateExponent,
1323
+ prime1,
1324
+ prime2,
1325
+ exponent1,
1326
+ exponent2
1327
+ };
1328
+ }
1329
+
1330
+ // source/public_private_match.ts
1331
+ function publicKeyAndPrivateKeyMatches(certificate, privateKey) {
1332
+ const i = exploreCertificate(certificate);
1333
+ const j = explorePrivateKey(privateKey);
1334
+ const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
1335
+ const modulus2 = j.modulus;
1336
+ if (modulus1.length != modulus2.length) {
1337
+ return false;
1338
+ }
1339
+ return modulus1.toString("hex") === modulus2.toString("hex");
1340
+ }
1341
+ function certificateMatchesPrivateKeyPEM(certificate, privateKey, blockSize) {
1342
+ const initialBuffer = Buffer.from("Lorem Ipsum");
1343
+ const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize);
1344
+ const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
1345
+ const finalString = decryptedBuffer.toString("utf-8");
1346
+ return initialBuffer.toString("utf-8") === finalString;
1347
+ }
1348
+ function certificateMatchesPrivateKey(certificate, privateKey) {
1349
+ const e = explorePrivateKey(privateKey);
1350
+ const blockSize = e.modulus.length;
1351
+ const certificatePEM = toPem(certificate, "CERTIFICATE");
1352
+ return certificateMatchesPrivateKeyPEM(certificatePEM, privateKey, blockSize);
1353
+ }
1354
+
1355
+ // source/common.ts
1356
+ var import_crypto2 = __toESM(require("crypto"));
1357
+ var KeyObjectOrig = import_crypto2.default.KeyObject;
1358
+ var { createPrivateKey: createPrivateKeyFromNodeJSCrypto } = import_crypto2.default;
1359
+ function isKeyObject(mayBeKeyObject) {
1360
+ if (KeyObjectOrig) {
1361
+ return mayBeKeyObject instanceof KeyObjectOrig;
1362
+ }
1363
+ return typeof mayBeKeyObject === "object" && typeof mayBeKeyObject.type === "string";
1364
+ }
1365
+ var CertificatePurpose = /* @__PURE__ */ ((CertificatePurpose2) => {
1366
+ CertificatePurpose2[CertificatePurpose2["NotSpecified"] = 0] = "NotSpecified";
1367
+ CertificatePurpose2[CertificatePurpose2["ForCertificateAuthority"] = 1] = "ForCertificateAuthority";
1368
+ CertificatePurpose2[CertificatePurpose2["ForApplication"] = 2] = "ForApplication";
1369
+ CertificatePurpose2[CertificatePurpose2["ForUserAuthentication"] = 3] = "ForUserAuthentication";
1370
+ return CertificatePurpose2;
1371
+ })(CertificatePurpose || {});
1372
+
1373
+ // source/crypto_utils2.ts
1284
1374
  var import_assert4 = __toESM(require("assert"));
1375
+ var import_jsrsasign2 = __toESM(require("jsrsasign"));
1376
+ function rsaLengthPrivateKey(key) {
1377
+ const keyPem = typeof key.hidden === "string" ? key.hidden : key.hidden.export({ type: "pkcs1", format: "pem" }).toString();
1378
+ const a = import_jsrsasign2.default.KEYUTIL.getKey(keyPem);
1379
+ return a.n.toString(16).length / 2;
1380
+ }
1381
+ function toPem2(raw_key, pem) {
1382
+ if (raw_key.hidden) {
1383
+ return toPem2(raw_key.hidden, pem);
1384
+ }
1385
+ (0, import_assert4.default)(raw_key, "expecting a key");
1386
+ (0, import_assert4.default)(typeof pem === "string");
1387
+ if (isKeyObject(raw_key)) {
1388
+ const _raw_key = raw_key;
1389
+ if (pem === "RSA PRIVATE KEY") {
1390
+ return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs1" }).toString());
1391
+ } else if (pem === "PRIVATE KEY") {
1392
+ return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs8" }).toString());
1393
+ } else {
1394
+ throw new Error("Unsupported case!");
1395
+ }
1396
+ }
1397
+ return toPem(raw_key, pem);
1398
+ }
1399
+ function coercePrivateKeyPem(privateKey) {
1400
+ return toPem2(privateKey, "PRIVATE KEY");
1401
+ }
1402
+ function coercePublicKeyPem(publicKey) {
1403
+ if (isKeyObject(publicKey)) {
1404
+ return publicKey.export({ format: "pem", type: "spki" }).toString();
1405
+ }
1406
+ (0, import_assert4.default)(typeof publicKey === "string");
1407
+ return publicKey;
1408
+ }
1409
+ function coerceRsaPublicKeyPem(publicKey) {
1410
+ if (isKeyObject(publicKey)) {
1411
+ return publicKey.export({ format: "pem", type: "spki" }).toString();
1412
+ }
1413
+ (0, import_assert4.default)(typeof publicKey === "string");
1414
+ return publicKey;
1415
+ }
1416
+ function rsaLengthPublicKey(key) {
1417
+ key = coercePublicKeyPem(key);
1418
+ (0, import_assert4.default)(typeof key === "string");
1419
+ const a = import_jsrsasign2.default.KEYUTIL.getKey(key);
1420
+ return a.n.toString(16).length / 2;
1421
+ }
1422
+ function rsaLengthRsaPublicKey(key) {
1423
+ key = coerceRsaPublicKeyPem(key);
1424
+ (0, import_assert4.default)(typeof key === "string");
1425
+ const a = import_jsrsasign2.default.KEYUTIL.getKey(key);
1426
+ return a.n.toString(16).length / 2;
1427
+ }
1428
+
1429
+ // source/derived_keys.ts
1430
+ var import_assert6 = __toESM(require("assert"));
1431
+ var import_crypto3 = require("crypto");
1432
+
1433
+ // source/explore_certificate.ts
1434
+ var import_assert5 = __toESM(require("assert"));
1285
1435
  function coerceCertificate(certificate) {
1286
1436
  if (typeof certificate === "string") {
1287
1437
  certificate = convertPEMtoDER(certificate);
1288
1438
  }
1289
- (0, import_assert4.default)(certificate instanceof Buffer);
1439
+ (0, import_assert5.default)(certificate instanceof Buffer);
1290
1440
  return certificate;
1291
1441
  }
1292
1442
  function exploreCertificateInfo(certificate) {
@@ -1313,8 +1463,8 @@ function plus(buf1, buf2) {
1313
1463
  return Buffer.concat([buf1, buf2]);
1314
1464
  }
1315
1465
  function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
1316
- (0, import_assert5.default)(seed instanceof Buffer);
1317
- (0, import_assert5.default)(sha1or256 === "SHA1" || sha1or256 === "SHA256");
1466
+ (0, import_assert6.default)(seed instanceof Buffer);
1467
+ (0, import_assert6.default)(sha1or256 === "SHA1" || sha1or256 === "SHA256");
1318
1468
  const a = [];
1319
1469
  a[0] = seed;
1320
1470
  let index = 1;
@@ -1327,12 +1477,12 @@ function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
1327
1477
  return p_hash.subarray(0, minLength);
1328
1478
  }
1329
1479
  function computeDerivedKeys(secret, seed, options) {
1330
- (0, import_assert5.default)(Number.isFinite(options.signatureLength));
1331
- (0, import_assert5.default)(Number.isFinite(options.encryptingKeyLength));
1332
- (0, import_assert5.default)(Number.isFinite(options.encryptingBlockSize));
1333
- (0, import_assert5.default)(typeof options.algorithm === "string");
1480
+ (0, import_assert6.default)(Number.isFinite(options.signatureLength));
1481
+ (0, import_assert6.default)(Number.isFinite(options.encryptingKeyLength));
1482
+ (0, import_assert6.default)(Number.isFinite(options.encryptingBlockSize));
1483
+ (0, import_assert6.default)(typeof options.algorithm === "string");
1334
1484
  options.sha1or256 = options.sha1or256 || "SHA1";
1335
- (0, import_assert5.default)(typeof options.sha1or256 === "string");
1485
+ (0, import_assert6.default)(typeof options.sha1or256 === "string");
1336
1486
  const offset1 = options.signingKeyLength;
1337
1487
  const offset2 = offset1 + options.encryptingKeyLength;
1338
1488
  const minLength = offset2 + options.encryptingBlockSize;
@@ -1357,7 +1507,7 @@ function removePadding(buffer) {
1357
1507
  return reduceLength(buffer, nbPaddingBytes);
1358
1508
  }
1359
1509
  function verifyChunkSignature(chunk, options) {
1360
- (0, import_assert5.default)(chunk instanceof Buffer);
1510
+ (0, import_assert6.default)(chunk instanceof Buffer);
1361
1511
  let signatureLength = options.signatureLength || 0;
1362
1512
  if (signatureLength === 0) {
1363
1513
  const cert = exploreCertificateInfo(options.publicKey);
@@ -1368,16 +1518,16 @@ function verifyChunkSignature(chunk, options) {
1368
1518
  return verifyMessageChunkSignature(block_to_verify, signature, options);
1369
1519
  }
1370
1520
  function computePaddingFooter(buffer, derivedKeys) {
1371
- (0, import_assert5.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "encryptingBlockSize"));
1521
+ (0, import_assert6.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "encryptingBlockSize"));
1372
1522
  const paddingSize = derivedKeys.encryptingBlockSize - (buffer.length + 1) % derivedKeys.encryptingBlockSize;
1373
1523
  const padding = createFastUninitializedBuffer(paddingSize + 1);
1374
1524
  padding.fill(paddingSize);
1375
1525
  return padding;
1376
1526
  }
1377
1527
  function derivedKeys_algorithm(derivedKeys) {
1378
- (0, import_assert5.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "algorithm"));
1528
+ (0, import_assert6.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "algorithm"));
1379
1529
  const algorithm = derivedKeys.algorithm || "aes-128-cbc";
1380
- (0, import_assert5.default)(algorithm === "aes-128-cbc" || algorithm === "aes-256-cbc");
1530
+ (0, import_assert6.default)(algorithm === "aes-128-cbc" || algorithm === "aes-256-cbc");
1381
1531
  return algorithm;
1382
1532
  }
1383
1533
  function encryptBufferWithDerivedKeys(buffer, derivedKeys) {
@@ -1403,12 +1553,12 @@ function decryptBufferWithDerivedKeys(buffer, derivedKeys) {
1403
1553
  return Buffer.concat(decrypted_chunks);
1404
1554
  }
1405
1555
  function makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys) {
1406
- (0, import_assert5.default)(message instanceof Buffer);
1407
- (0, import_assert5.default)(derivedKeys.signingKey instanceof Buffer);
1408
- (0, import_assert5.default)(typeof derivedKeys.sha1or256 === "string");
1409
- (0, import_assert5.default)(derivedKeys.sha1or256 === "SHA1" || derivedKeys.sha1or256 === "SHA256");
1556
+ (0, import_assert6.default)(message instanceof Buffer);
1557
+ (0, import_assert6.default)(derivedKeys.signingKey instanceof Buffer);
1558
+ (0, import_assert6.default)(typeof derivedKeys.sha1or256 === "string");
1559
+ (0, import_assert6.default)(derivedKeys.sha1or256 === "SHA1" || derivedKeys.sha1or256 === "SHA256");
1410
1560
  const signature = (0, import_crypto3.createHmac)(derivedKeys.sha1or256, derivedKeys.signingKey).update(message).digest();
1411
- (0, import_assert5.default)(signature.length === derivedKeys.signatureLength);
1561
+ (0, import_assert6.default)(signature.length === derivedKeys.signatureLength);
1412
1562
  return signature;
1413
1563
  }
1414
1564
  function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
@@ -1418,126 +1568,31 @@ function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
1418
1568
  return computedSignature.toString("hex") === expectedSignature.toString("hex");
1419
1569
  }
1420
1570
 
1421
- // source/crypto_utils2.ts
1422
- var import_assert6 = __toESM(require("assert"));
1423
- var import_jsrsasign2 = __toESM(require("jsrsasign"));
1424
- function rsaLengthPrivateKey(key) {
1425
- const keyPem = typeof key.hidden === "string" ? key.hidden : key.hidden.export({ type: "pkcs1", format: "pem" }).toString();
1426
- const a = import_jsrsasign2.default.KEYUTIL.getKey(keyPem);
1427
- return a.n.toString(16).length / 2;
1428
- }
1429
- function toPem2(raw_key, pem) {
1430
- if (raw_key.hidden) {
1431
- return toPem2(raw_key.hidden, pem);
1432
- }
1433
- (0, import_assert6.default)(raw_key, "expecting a key");
1434
- (0, import_assert6.default)(typeof pem === "string");
1435
- if (isKeyObject(raw_key)) {
1436
- const _raw_key = raw_key;
1437
- if (pem === "RSA PRIVATE KEY") {
1438
- return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs1" }).toString());
1439
- } else if (pem === "PRIVATE KEY") {
1440
- return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs8" }).toString());
1441
- } else {
1442
- throw new Error("Unsupported case!");
1443
- }
1444
- }
1445
- return toPem(raw_key, pem);
1446
- }
1447
- function coercePrivateKeyPem(privateKey) {
1448
- return toPem2(privateKey, "PRIVATE KEY");
1449
- }
1450
- function coercePublicKeyPem(publicKey) {
1451
- if (isKeyObject(publicKey)) {
1452
- return publicKey.export({ format: "pem", type: "spki" }).toString();
1453
- }
1454
- (0, import_assert6.default)(typeof publicKey === "string");
1455
- return publicKey;
1456
- }
1457
- function coerceRsaPublicKeyPem(publicKey) {
1458
- if (isKeyObject(publicKey)) {
1459
- return publicKey.export({ format: "pem", type: "spki" }).toString();
1460
- }
1461
- (0, import_assert6.default)(typeof publicKey === "string");
1462
- return publicKey;
1463
- }
1464
- function rsaLengthPublicKey(key) {
1465
- key = coercePublicKeyPem(key);
1466
- (0, import_assert6.default)(typeof key === "string");
1467
- const a = import_jsrsasign2.default.KEYUTIL.getKey(key);
1468
- return a.n.toString(16).length / 2;
1469
- }
1470
- function rsaLengthRsaPublicKey(key) {
1471
- key = coerceRsaPublicKeyPem(key);
1472
- (0, import_assert6.default)(typeof key === "string");
1473
- const a = import_jsrsasign2.default.KEYUTIL.getKey(key);
1474
- return a.n.toString(16).length / 2;
1475
- }
1476
-
1477
- // source/verify_certificate_signature.ts
1478
- var import_crypto4 = require("crypto");
1479
- function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
1480
- const block_info = readTag(certificateOrCrl, 0);
1481
- const blocks = _readStruct(certificateOrCrl, block_info);
1482
- const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
1483
- const signatureAlgorithm = _readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
1484
- const signatureValue = _readSignatureValueBin(certificateOrCrl, blocks[2]);
1485
- const p = split_der(parentCertificate)[0];
1486
- const certPem = toPem(p, "CERTIFICATE");
1487
- const verify = (0, import_crypto4.createVerify)(signatureAlgorithm.identifier);
1488
- verify.update(bufferToBeSigned);
1489
- verify.end();
1490
- return verify.verify(certPem, signatureValue);
1491
- }
1492
- function verifyCertificateSignature(certificate, parentCertificate) {
1493
- return verifyCertificateOrClrSignature(certificate, parentCertificate);
1494
- }
1495
- function verifyCertificateRevocationListSignature(certificateRevocationList, parentCertificate) {
1496
- return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
1497
- }
1498
- async function verifyCertificateChain(certificateChain) {
1499
- for (let index = 1; index < certificateChain.length; index++) {
1500
- const cert = certificateChain[index - 1];
1501
- const certParent = certificateChain[index];
1502
- const certParentInfo = exploreCertificate(certParent);
1503
- const keyUsage = certParentInfo.tbsCertificate.extensions.keyUsage;
1504
- if (!keyUsage.keyCertSign) {
1505
- return {
1506
- status: "BadCertificateIssuerUseNotAllowed",
1507
- reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing"
1508
- };
1509
- }
1510
- const parentSignChild = verifyCertificateSignature(cert, certParent);
1511
- if (!parentSignChild) {
1512
- return {
1513
- status: "BadCertificateInvalid",
1514
- reason: "One of the certificate in the chain is not signing the previous certificate"
1515
- };
1516
- }
1517
- const certInfo = exploreCertificate(cert);
1518
- if (!certInfo.tbsCertificate.extensions) {
1519
- return {
1520
- status: "BadCertificateInvalid",
1521
- reason: "Cannot find X409 Extension 3 in certificate"
1522
- };
1523
- }
1524
- if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
1525
- return {
1526
- status: "BadCertificateInvalid",
1527
- reason: "Cannot find X409 Extension 3 in certificate (parent)"
1528
- };
1529
- }
1530
- if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
1531
- return {
1532
- status: "BadCertificateInvalid",
1533
- reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
1534
- };
1571
+ // source/explore_asn1.ts
1572
+ function t(tag) {
1573
+ return TagType[tag];
1574
+ }
1575
+ function bi(blockInfo, depth) {
1576
+ const indent = " ".repeat(depth);
1577
+ const hl = blockInfo.position - blockInfo.start;
1578
+ 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)}`;
1579
+ }
1580
+ function exploreAsn1(buffer) {
1581
+ console.log(hexDump(buffer));
1582
+ function dump(offset, depth) {
1583
+ const blockInfo = readTag(buffer, offset);
1584
+ dumpBlock(blockInfo, depth);
1585
+ function dumpBlock(blockInfo2, depth2) {
1586
+ console.log(bi(blockInfo2, depth2));
1587
+ if (blockInfo2.tag === 48 /* SEQUENCE */ || blockInfo2.tag === 49 /* SET */ || blockInfo2.tag >= 160 /* CONTEXT_SPECIFIC0 */) {
1588
+ const blocks = readStruct(buffer, blockInfo2);
1589
+ for (const block of blocks) {
1590
+ dumpBlock(block, depth2 + 1);
1591
+ }
1592
+ }
1535
1593
  }
1536
1594
  }
1537
- return {
1538
- status: "Good",
1539
- reason: `certificate chain is valid(length = ${certificateChain.length})`
1540
- };
1595
+ dump(0, 0);
1541
1596
  }
1542
1597
 
1543
1598
  // source/explore_certificate_revocation_list.ts
@@ -1545,20 +1600,20 @@ function readNameForCrl(buffer, block) {
1545
1600
  return _readDirectoryName(buffer, block);
1546
1601
  }
1547
1602
  function _readTbsCertList(buffer, blockInfo) {
1548
- const blocks = _readStruct(buffer, blockInfo);
1603
+ const blocks = readStruct(buffer, blockInfo);
1549
1604
  const hasOptionalVersion = blocks[0].tag === 2 /* INTEGER */;
1550
1605
  if (hasOptionalVersion) {
1551
1606
  const version = _readIntegerValue(buffer, blocks[0]);
1552
- const signature = _readAlgorithmIdentifier(buffer, blocks[1]);
1607
+ const signature = readAlgorithmIdentifier(buffer, blocks[1]);
1553
1608
  const issuer = readNameForCrl(buffer, blocks[2]);
1554
1609
  const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[2])));
1555
1610
  const thisUpdate = _readTime(buffer, blocks[3]);
1556
1611
  const nextUpdate = _readTime(buffer, blocks[4]);
1557
1612
  const revokedCertificates = [];
1558
1613
  if (blocks[5] && blocks[5].tag < 128) {
1559
- const list = _readStruct(buffer, blocks[5]);
1614
+ const list = readStruct(buffer, blocks[5]);
1560
1615
  for (const r of list) {
1561
- const rr = _readStruct(buffer, r);
1616
+ const rr = readStruct(buffer, r);
1562
1617
  const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
1563
1618
  const revocationDate = _readTime(buffer, rr[1]);
1564
1619
  revokedCertificates.push({
@@ -1570,16 +1625,16 @@ function _readTbsCertList(buffer, blockInfo) {
1570
1625
  const ext0 = _findBlockAtIndex(blocks, 0);
1571
1626
  return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
1572
1627
  } else {
1573
- const signature = _readAlgorithmIdentifier(buffer, blocks[0]);
1628
+ const signature = readAlgorithmIdentifier(buffer, blocks[0]);
1574
1629
  const issuer = readNameForCrl(buffer, blocks[1]);
1575
1630
  const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[1])));
1576
1631
  const thisUpdate = _readTime(buffer, blocks[2]);
1577
1632
  const nextUpdate = _readTime(buffer, blocks[3]);
1578
1633
  const revokedCertificates = [];
1579
1634
  if (blocks[4] && blocks[4].tag < 128) {
1580
- const list = _readStruct(buffer, blocks[4]);
1635
+ const list = readStruct(buffer, blocks[4]);
1581
1636
  for (const r of list) {
1582
- const rr = _readStruct(buffer, r);
1637
+ const rr = readStruct(buffer, r);
1583
1638
  const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
1584
1639
  const revocationDate = _readTime(buffer, rr[1]);
1585
1640
  revokedCertificates.push({
@@ -1593,224 +1648,59 @@ function _readTbsCertList(buffer, blockInfo) {
1593
1648
  }
1594
1649
  function exploreCertificateRevocationList(crl) {
1595
1650
  const blockInfo = readTag(crl, 0);
1596
- const blocks = _readStruct(crl, blockInfo);
1651
+ const blocks = readStruct(crl, blockInfo);
1597
1652
  const tbsCertList = _readTbsCertList(crl, blocks[0]);
1598
- const signatureAlgorithm = _readAlgorithmIdentifier(crl, blocks[1]);
1599
- const signatureValue = _readSignatureValueBin(crl, blocks[2]);
1653
+ const signatureAlgorithm = readAlgorithmIdentifier(crl, blocks[1]);
1654
+ const signatureValue = readSignatureValueBin(crl, blocks[2]);
1600
1655
  return { tbsCertList, signatureAlgorithm, signatureValue };
1601
1656
  }
1602
1657
 
1603
1658
  // source/explore_certificate_signing_request.ts
1604
1659
  function _readExtensionRequest(buffer) {
1605
1660
  const block = readTag(buffer, 0);
1606
- const inner_blocks = _readStruct(buffer, block);
1661
+ const inner_blocks = readStruct(buffer, block);
1607
1662
  const extensions = inner_blocks.map((block1) => _readExtension(buffer, block1));
1608
1663
  const result = {};
1609
1664
  for (const e of extensions) {
1610
1665
  result[e.identifier.name] = e.value;
1611
1666
  }
1612
1667
  const { basicConstraints, keyUsage, subjectAltName } = result;
1613
- return { basicConstraints, keyUsage, subjectAltName };
1614
- }
1615
- function readCertificationRequestInfo(buffer, block) {
1616
- const blocks = _readStruct(buffer, block);
1617
- if (blocks.length === 4) {
1618
- const extensionRequestBlock = _findBlockAtIndex(blocks, 0);
1619
- if (!extensionRequestBlock) {
1620
- throw new Error("cannot find extensionRequest block");
1621
- }
1622
- const blocks1 = _readStruct(buffer, extensionRequestBlock);
1623
- const blocks2 = _readStruct(buffer, blocks1[0]);
1624
- const identifier = _readObjectIdentifier(buffer, blocks2[0]);
1625
- if (identifier.name !== "extensionRequest") {
1626
- throw new Error(" Cannot find extension Request in ASN1 block");
1627
- }
1628
- const buf = _getBlock(buffer, blocks2[1]);
1629
- const extensionRequest = _readExtensionRequest(buf);
1630
- return { extensionRequest };
1631
- }
1632
- throw new Error("Invalid CSR or ");
1633
- }
1634
- function exploreCertificateSigningRequest(crl) {
1635
- const blockInfo = readTag(crl, 0);
1636
- const blocks = _readStruct(crl, blockInfo);
1637
- const csrInfo = readCertificationRequestInfo(crl, blocks[0]);
1638
- return csrInfo;
1639
- }
1640
-
1641
- // source/explore_private_key.ts
1642
- function f(buffer, b) {
1643
- return buffer.subarray(b.position + 1, b.position + b.length);
1644
- }
1645
- var doDebug2 = !!process.env.DEBUG;
1646
- function explorePrivateKey(privateKey2) {
1647
- const privateKey1 = privateKey2.hidden;
1648
- const privateKey = typeof privateKey1 === "string" ? convertPEMtoDER(privateKey1) : privateKey1.export({ format: "der", type: "pkcs1" });
1649
- const block_info = readTag(privateKey, 0);
1650
- const blocks = _readStruct(privateKey, block_info);
1651
- if (blocks.length === 9) {
1652
- const version2 = f(privateKey, blocks[0]);
1653
- const modulus2 = f(privateKey, blocks[1]);
1654
- const publicExponent2 = f(privateKey, blocks[2]);
1655
- const privateExponent2 = f(privateKey, blocks[3]);
1656
- const prime12 = f(privateKey, blocks[4]);
1657
- const prime22 = f(privateKey, blocks[5]);
1658
- const exponent12 = f(privateKey, blocks[6]);
1659
- const exponent22 = f(privateKey, blocks[7]);
1660
- return {
1661
- version: version2,
1662
- modulus: modulus2,
1663
- publicExponent: publicExponent2,
1664
- privateExponent: privateExponent2,
1665
- prime1: prime12,
1666
- prime2: prime22,
1667
- exponent1: exponent12,
1668
- exponent2: exponent22
1669
- };
1670
- }
1671
- if (doDebug2) {
1672
- console.log("-------------------- private key:");
1673
- console.log(block_info);
1674
- console.log(
1675
- blocks.map((b2) => ({
1676
- tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
1677
- l: b2.length,
1678
- p: b2.position,
1679
- buff: privateKey.subarray(b2.position, b2.position + b2.length).toString("hex")
1680
- }))
1681
- );
1682
- }
1683
- const b = blocks[2];
1684
- const bb = privateKey.subarray(b.position, b.position + b.length);
1685
- const block_info1 = readTag(bb, 0);
1686
- const blocks1 = _readStruct(bb, block_info1);
1687
- if (doDebug2) {
1688
- console.log(
1689
- blocks1.map((b2) => ({
1690
- tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
1691
- l: b2.length,
1692
- p: b2.position,
1693
- buff: bb.subarray(b2.position, b2.position + b2.length).toString("hex")
1694
- }))
1695
- );
1696
- }
1697
- const version = f(bb, blocks1[0]);
1698
- const modulus = f(bb, blocks1[1]);
1699
- const publicExponent = f(bb, blocks1[2]);
1700
- const privateExponent = f(bb, blocks1[3]);
1701
- const prime1 = f(bb, blocks1[4]);
1702
- const prime2 = f(bb, blocks1[5]);
1703
- const exponent1 = f(bb, blocks1[6]);
1704
- const exponent2 = f(bb, blocks1[7]);
1705
- return {
1706
- version,
1707
- modulus,
1708
- publicExponent,
1709
- privateExponent,
1710
- prime1,
1711
- prime2,
1712
- exponent1,
1713
- exponent2
1714
- };
1715
- }
1716
-
1717
- // source/public_private_match.ts
1718
- function publicKeyAndPrivateKeyMatches(certificate, privateKey) {
1719
- const i = exploreCertificate(certificate);
1720
- const j = explorePrivateKey(privateKey);
1721
- const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
1722
- const modulus2 = j.modulus;
1723
- if (modulus1.length != modulus2.length) {
1724
- return false;
1725
- }
1726
- return modulus1.toString("hex") === modulus2.toString("hex");
1727
- }
1728
- function certificateMatchesPrivateKeyPEM(certificate, privateKey, blockSize) {
1729
- const initialBuffer = Buffer.from("Lorem Ipsum");
1730
- const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize);
1731
- const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
1732
- const finalString = decryptedBuffer.toString("utf-8");
1733
- return initialBuffer.toString("utf-8") === finalString;
1734
- }
1735
- function certificateMatchesPrivateKey(certificate, privateKey) {
1736
- const e = explorePrivateKey(privateKey);
1737
- const blockSize = e.modulus.length;
1738
- const certificatePEM = toPem(certificate, "CERTIFICATE");
1739
- return certificateMatchesPrivateKeyPEM(certificatePEM, privateKey, blockSize);
1740
- }
1741
-
1742
- // source/x509/_crypto.ts
1743
- var x509 = __toESM(require("@peculiar/x509"));
1744
- var import_webcrypto = require("@peculiar/webcrypto");
1745
- var import_crypto5 = __toESM(require("crypto"));
1746
- var x5092 = __toESM(require("@peculiar/x509"));
1747
- var doDebug3 = false;
1748
- var _crypto;
1749
- var ignoreCrypto = process.env.IGNORE_SUBTLE_FROM_CRYPTO;
1750
- if (typeof window === "undefined") {
1751
- _crypto = import_crypto5.default;
1752
- if (!_crypto?.subtle || ignoreCrypto) {
1753
- _crypto = new import_webcrypto.Crypto();
1754
- doDebug3 && console.warn("using @peculiar/webcrypto");
1755
- } else {
1756
- doDebug3 && console.warn("using nodejs crypto (native)");
1757
- }
1758
- x509.cryptoProvider.set(_crypto);
1759
- } else {
1760
- doDebug3 && console.warn("using browser crypto (native)");
1761
- _crypto = crypto;
1762
- x509.cryptoProvider.set(crypto);
1763
- }
1764
- function getCrypto() {
1765
- return _crypto || crypto || require("crypto");
1766
- }
1767
-
1768
- // source/x509/create_key_pair.ts
1769
- async function generateKeyPair(modulusLength = 2048) {
1770
- const crypto3 = getCrypto();
1771
- const alg = {
1772
- name: "RSASSA-PKCS1-v1_5",
1773
- hash: { name: "SHA-256" },
1774
- publicExponent: new Uint8Array([1, 0, 1]),
1775
- modulusLength
1776
- };
1777
- const keys = await crypto3.subtle.generateKey(alg, true, ["sign", "verify"]);
1778
- return keys;
1668
+ return { basicConstraints, keyUsage, subjectAltName };
1779
1669
  }
1780
- async function generatePrivateKey(modulusLength = 2048) {
1781
- return (await generateKeyPair(modulusLength)).privateKey;
1670
+ function readCertificationRequestInfo(buffer, block) {
1671
+ const blocks = readStruct(buffer, block);
1672
+ if (blocks.length === 4) {
1673
+ const extensionRequestBlock = _findBlockAtIndex(blocks, 0);
1674
+ if (!extensionRequestBlock) {
1675
+ throw new Error("cannot find extensionRequest block");
1676
+ }
1677
+ const blocks1 = readStruct(buffer, extensionRequestBlock);
1678
+ const blocks2 = readStruct(buffer, blocks1[0]);
1679
+ const identifier = _readObjectIdentifier(buffer, blocks2[0]);
1680
+ if (identifier.name !== "extensionRequest") {
1681
+ throw new Error(" Cannot find extension Request in ASN1 block");
1682
+ }
1683
+ const buf = _getBlock(buffer, blocks2[1]);
1684
+ const extensionRequest = _readExtensionRequest(buf);
1685
+ return { extensionRequest };
1686
+ }
1687
+ throw new Error("Invalid CSR or ");
1782
1688
  }
1783
- async function privateKeyToPEM(privateKey) {
1784
- const crypto3 = getCrypto();
1785
- const privDer = await crypto3.subtle.exportKey("pkcs8", privateKey);
1786
- const privPem = x5092.PemConverter.encode(privDer, "PRIVATE KEY");
1787
- return { privPem, privDer };
1689
+ function exploreCertificateSigningRequest(crl) {
1690
+ const blockInfo = readTag(crl, 0);
1691
+ const blocks = readStruct(crl, blockInfo);
1692
+ const csrInfo = readCertificationRequestInfo(crl, blocks[0]);
1693
+ return csrInfo;
1788
1694
  }
1789
- async function derToPrivateKey(privDer) {
1790
- const crypto3 = getCrypto();
1791
- return await crypto3.subtle.importKey(
1792
- "pkcs8",
1793
- privDer,
1794
- {
1795
- name: "RSASSA-PKCS1-v1_5",
1796
- hash: { name: "SHA-256" }
1797
- },
1798
- true,
1799
- [
1800
- "sign"
1801
- // "encrypt",
1802
- // "decrypt",
1803
- // "verify",
1804
- // "wrapKey",
1805
- // "unwrapKey",
1806
- // "deriveKey",
1807
- // "deriveBits"
1808
- ]
1809
- );
1695
+
1696
+ // source/make_private_key_from_pem.ts
1697
+ function makePrivateKeyFromPem(privateKeyInPem) {
1698
+ return { hidden: privateKeyInPem };
1810
1699
  }
1811
- async function pemToPrivateKey(pem) {
1812
- const privDer = x5092.PemConverter.decode(pem);
1813
- return derToPrivateKey(privDer[0]);
1700
+
1701
+ // source/make_private_key_thumbprint.ts
1702
+ function makePrivateKeyThumbPrint(privateKey) {
1703
+ return Buffer.alloc(0);
1814
1704
  }
1815
1705
 
1816
1706
  // source/subject.ts
@@ -1895,11 +1785,180 @@ var Subject = class _Subject {
1895
1785
  return this.toStringInternal("/");
1896
1786
  }
1897
1787
  toString() {
1898
- const t = this.toStringForOPCUA();
1899
- return t ? "/" + t : t;
1788
+ const t2 = this.toStringForOPCUA();
1789
+ return t2 ? "/" + t2 : t2;
1900
1790
  }
1901
1791
  };
1902
1792
 
1793
+ // source/verify_certificate_signature.ts
1794
+ var import_crypto4 = require("crypto");
1795
+ function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
1796
+ const block_info = readTag(certificateOrCrl, 0);
1797
+ const blocks = readStruct(certificateOrCrl, block_info);
1798
+ const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
1799
+ const signatureAlgorithm = readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
1800
+ const signatureValue = readSignatureValueBin(certificateOrCrl, blocks[2]);
1801
+ const p = split_der(parentCertificate)[0];
1802
+ const certPem = toPem(p, "CERTIFICATE");
1803
+ const verify = (0, import_crypto4.createVerify)(signatureAlgorithm.identifier);
1804
+ verify.update(bufferToBeSigned);
1805
+ verify.end();
1806
+ return verify.verify(certPem, signatureValue);
1807
+ }
1808
+ function verifyCertificateSignature(certificate, parentCertificate) {
1809
+ return verifyCertificateOrClrSignature(certificate, parentCertificate);
1810
+ }
1811
+ function verifyCertificateRevocationListSignature(certificateRevocationList, parentCertificate) {
1812
+ return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
1813
+ }
1814
+ async function verifyCertificateChain(certificateChain) {
1815
+ for (let index = 1; index < certificateChain.length; index++) {
1816
+ const cert = certificateChain[index - 1];
1817
+ const certParent = certificateChain[index];
1818
+ const certParentInfo = exploreCertificate(certParent);
1819
+ const keyUsage = certParentInfo.tbsCertificate.extensions.keyUsage;
1820
+ if (!keyUsage.keyCertSign) {
1821
+ return {
1822
+ status: "BadCertificateIssuerUseNotAllowed",
1823
+ reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing"
1824
+ };
1825
+ }
1826
+ const parentSignChild = verifyCertificateSignature(cert, certParent);
1827
+ if (!parentSignChild) {
1828
+ return {
1829
+ status: "BadCertificateInvalid",
1830
+ reason: "One of the certificate in the chain is not signing the previous certificate"
1831
+ };
1832
+ }
1833
+ const certInfo = exploreCertificate(cert);
1834
+ if (!certInfo.tbsCertificate.extensions) {
1835
+ return {
1836
+ status: "BadCertificateInvalid",
1837
+ reason: "Cannot find X409 Extension 3 in certificate"
1838
+ };
1839
+ }
1840
+ if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
1841
+ return {
1842
+ status: "BadCertificateInvalid",
1843
+ reason: "Cannot find X409 Extension 3 in certificate (parent)"
1844
+ };
1845
+ }
1846
+ if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
1847
+ return {
1848
+ status: "BadCertificateInvalid",
1849
+ reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
1850
+ };
1851
+ }
1852
+ }
1853
+ return {
1854
+ status: "Good",
1855
+ reason: `certificate chain is valid(length = ${certificateChain.length})`
1856
+ };
1857
+ }
1858
+
1859
+ // source/x509/_crypto.ts
1860
+ var x509 = __toESM(require("@peculiar/x509"));
1861
+ var import_webcrypto = require("@peculiar/webcrypto");
1862
+ var import_crypto5 = __toESM(require("crypto"));
1863
+ var x5092 = __toESM(require("@peculiar/x509"));
1864
+ var doDebug3 = false;
1865
+ var _crypto;
1866
+ var ignoreCrypto = process.env.IGNORE_SUBTLE_FROM_CRYPTO;
1867
+ if (typeof window === "undefined") {
1868
+ _crypto = import_crypto5.default;
1869
+ if (!_crypto?.subtle || ignoreCrypto) {
1870
+ _crypto = new import_webcrypto.Crypto();
1871
+ doDebug3 && console.warn("using @peculiar/webcrypto");
1872
+ } else {
1873
+ doDebug3 && console.warn("using nodejs crypto (native)");
1874
+ }
1875
+ x509.cryptoProvider.set(_crypto);
1876
+ } else {
1877
+ doDebug3 && console.warn("using browser crypto (native)");
1878
+ _crypto = crypto;
1879
+ x509.cryptoProvider.set(crypto);
1880
+ }
1881
+ function getCrypto() {
1882
+ return _crypto || crypto || require("crypto");
1883
+ }
1884
+
1885
+ // source/x509/create_key_pair.ts
1886
+ async function generateKeyPair(modulusLength = 2048) {
1887
+ const crypto3 = getCrypto();
1888
+ const alg = {
1889
+ name: "RSASSA-PKCS1-v1_5",
1890
+ hash: { name: "SHA-256" },
1891
+ publicExponent: new Uint8Array([1, 0, 1]),
1892
+ modulusLength
1893
+ };
1894
+ const keys = await crypto3.subtle.generateKey(alg, true, ["sign", "verify"]);
1895
+ return keys;
1896
+ }
1897
+ async function generatePrivateKey(modulusLength = 2048) {
1898
+ return (await generateKeyPair(modulusLength)).privateKey;
1899
+ }
1900
+ async function privateKeyToPEM(privateKey) {
1901
+ const crypto3 = getCrypto();
1902
+ const privDer = await crypto3.subtle.exportKey("pkcs8", privateKey);
1903
+ const privPem = x5092.PemConverter.encode(privDer, "PRIVATE KEY");
1904
+ return { privPem, privDer };
1905
+ }
1906
+ async function derToPrivateKey(privDer) {
1907
+ const crypto3 = getCrypto();
1908
+ return await crypto3.subtle.importKey(
1909
+ "pkcs8",
1910
+ privDer,
1911
+ {
1912
+ name: "RSASSA-PKCS1-v1_5",
1913
+ hash: { name: "SHA-256" }
1914
+ },
1915
+ true,
1916
+ [
1917
+ "sign"
1918
+ // "encrypt",
1919
+ // "decrypt",
1920
+ // "verify",
1921
+ // "wrapKey",
1922
+ // "unwrapKey",
1923
+ // "deriveKey",
1924
+ // "deriveBits"
1925
+ ]
1926
+ );
1927
+ }
1928
+ async function pemToPrivateKey(pem) {
1929
+ const privDer = x5092.PemConverter.decode(pem);
1930
+ return derToPrivateKey(privDer[0]);
1931
+ }
1932
+
1933
+ // source/x509/coerce_private_key.ts
1934
+ var crypto2 = getCrypto();
1935
+ var doDebug4 = false;
1936
+ function coercePEMorDerToPrivateKey(privateKeyInDerOrPem) {
1937
+ if (typeof privateKeyInDerOrPem === "string") {
1938
+ const hidden = createPrivateKeyFromNodeJSCrypto(privateKeyInDerOrPem);
1939
+ return { hidden };
1940
+ }
1941
+ throw new Error("not implemented");
1942
+ }
1943
+ async function _coercePrivateKey(privateKey) {
1944
+ const KeyObject4 = crypto2.KeyObject;
1945
+ if (privateKey instanceof Buffer) {
1946
+ const privateKey1 = await derToPrivateKey(privateKey);
1947
+ return KeyObject4.from(privateKey1);
1948
+ } else if (typeof privateKey === "string") {
1949
+ try {
1950
+ const privateKey1 = await pemToPrivateKey(privateKey);
1951
+ return KeyObject4.from(privateKey1);
1952
+ } catch (err) {
1953
+ doDebug4 && console.log(privateKey);
1954
+ throw err;
1955
+ }
1956
+ } else if (privateKey instanceof KeyObject4) {
1957
+ return privateKey;
1958
+ }
1959
+ throw new Error("Invalid privateKey");
1960
+ }
1961
+
1903
1962
  // source/x509/_get_attributes.ts
1904
1963
  var keyUsageApplication = x5092.KeyUsageFlags.keyEncipherment | x5092.KeyUsageFlags.nonRepudiation | x5092.KeyUsageFlags.dataEncipherment | x5092.KeyUsageFlags.keyCertSign | x5092.KeyUsageFlags.digitalSignature;
1905
1964
  var keyUsageCA = x5092.KeyUsageFlags.keyCertSign | x5092.KeyUsageFlags.cRLSign;
@@ -6164,40 +6223,6 @@ async function createSelfSignedCertificate({
6164
6223
  );
6165
6224
  return { cert: cert.toString("pem"), der: cert };
6166
6225
  }
6167
-
6168
- // source/x509/coerce_private_key.ts
6169
- var crypto2 = getCrypto();
6170
- var doDebug4 = false;
6171
- function coercePEMorDerToPrivateKey(privateKeyInDerOrPem) {
6172
- if (typeof privateKeyInDerOrPem === "string") {
6173
- const hidden = createPrivateKeyFromNodeJSCrypto(privateKeyInDerOrPem);
6174
- return { hidden };
6175
- }
6176
- throw new Error("not implemented");
6177
- }
6178
- async function _coercePrivateKey(privateKey) {
6179
- const KeyObject4 = crypto2.KeyObject;
6180
- if (privateKey instanceof Buffer) {
6181
- const privateKey1 = await derToPrivateKey(privateKey);
6182
- return KeyObject4.from(privateKey1);
6183
- } else if (typeof privateKey === "string") {
6184
- try {
6185
- const privateKey1 = await pemToPrivateKey(privateKey);
6186
- return KeyObject4.from(privateKey1);
6187
- } catch (err) {
6188
- doDebug4 && console.log(privateKey);
6189
- throw err;
6190
- }
6191
- } else if (privateKey instanceof KeyObject4) {
6192
- return privateKey;
6193
- }
6194
- throw new Error("Invalid privateKey");
6195
- }
6196
-
6197
- // source/make_private_key_from_pem.ts
6198
- function makePrivateKeyFromPem(privateKeyInPem) {
6199
- return { hidden: privateKeyInPem };
6200
- }
6201
6226
  // Annotate the CommonJS export names for ESM import in node:
6202
6227
  0 && (module.exports = {
6203
6228
  CertificatePurpose,
@@ -6205,28 +6230,8 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6205
6230
  RSA_PKCS1_OAEP_PADDING,
6206
6231
  RSA_PKCS1_PADDING,
6207
6232
  Subject,
6208
- TagType,
6209
6233
  _coercePrivateKey,
6210
- _findBlockAtIndex,
6211
- _getBlock,
6212
- _readAlgorithmIdentifier,
6213
- _readBitString,
6214
- _readBooleanValue,
6215
- _readDirectoryName,
6216
- _readECCAlgorithmIdentifier,
6217
6234
  _readExtension,
6218
- _readIntegerAsByteString,
6219
- _readIntegerValue,
6220
- _readListOfInteger,
6221
- _readLongIntegerValue,
6222
- _readObjectIdentifier,
6223
- _readOctetString,
6224
- _readSignatureValue,
6225
- _readSignatureValueBin,
6226
- _readStruct,
6227
- _readTime,
6228
- _readValue,
6229
- _readVersionValue,
6230
6235
  certificateMatchesPrivateKey,
6231
6236
  coerceCertificate,
6232
6237
  coerceCertificatePem,
@@ -6235,7 +6240,6 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6235
6240
  coercePublicKeyPem,
6236
6241
  coerceRsaPublicKeyPem,
6237
6242
  combine_der,
6238
- compactDirectoryName,
6239
6243
  computeDerivedKeys,
6240
6244
  computePaddingFooter,
6241
6245
  convertPEMtoDER,
@@ -6245,6 +6249,7 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6245
6249
  decryptBufferWithDerivedKeys,
6246
6250
  derToPrivateKey,
6247
6251
  encryptBufferWithDerivedKeys,
6252
+ exploreAsn1,
6248
6253
  exploreCertificate,
6249
6254
  exploreCertificateInfo,
6250
6255
  exploreCertificateRevocationList,
@@ -6252,7 +6257,6 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6252
6257
  explorePrivateKey,
6253
6258
  extractPublicKeyFromCertificate,
6254
6259
  extractPublicKeyFromCertificateSync,
6255
- formatBuffer2DigitHexWithColum,
6256
6260
  generateKeyPair,
6257
6261
  generatePrivateKey,
6258
6262
  hexDump,
@@ -6261,9 +6265,9 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6261
6265
  makeMessageChunkSignature,
6262
6266
  makeMessageChunkSignatureWithDerivedKeys,
6263
6267
  makePrivateKeyFromPem,
6268
+ makePrivateKeyThumbPrint,
6264
6269
  makePseudoRandomBuffer,
6265
6270
  makeSHA1Thumbprint,
6266
- parseBitString,
6267
6271
  pemToPrivateKey,
6268
6272
  privateDecrypt,
6269
6273
  privateDecrypt_long,
@@ -6275,7 +6279,6 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6275
6279
  publicKeyAndPrivateKeyMatches,
6276
6280
  readCertificationRequestInfo,
6277
6281
  readNameForCrl,
6278
- readTag,
6279
6282
  readTbsCertificate,
6280
6283
  reduceLength,
6281
6284
  removePadding,