node-opcua-crypto 4.10.0 → 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 (44) hide show
  1. package/README.md +9 -4
  2. package/dist/{chunk-46EEAYVO.mjs → chunk-2RCYFHGG.mjs} +730 -719
  3. package/dist/chunk-2RCYFHGG.mjs.map +1 -0
  4. package/dist/chunk-C7PROBPE.mjs +14 -0
  5. package/dist/chunk-C7PROBPE.mjs.map +1 -0
  6. package/dist/{chunk-GNEWUC7X.mjs → chunk-UH5AT3JE.mjs} +9 -15
  7. package/dist/chunk-UH5AT3JE.mjs.map +1 -0
  8. package/dist/index.d.mts +3 -3
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.js +517 -565
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +7 -53
  13. package/dist/index_web-C5Oeu9mq.d.mts +503 -0
  14. package/dist/index_web-D1qc4UN2.d.ts +503 -0
  15. package/dist/source/index.d.mts +13 -3
  16. package/dist/source/index.d.ts +13 -3
  17. package/dist/source/index.js +481 -522
  18. package/dist/source/index.js.map +1 -1
  19. package/dist/source/index.mjs +7 -51
  20. package/dist/source/index_web.d.mts +4 -528
  21. package/dist/source/index_web.d.ts +4 -528
  22. package/dist/source/index_web.js +498 -515
  23. package/dist/source/index_web.js.map +1 -1
  24. package/dist/source/index_web.mjs +5 -49
  25. package/dist/source_nodejs/index.d.mts +1 -2
  26. package/dist/source_nodejs/index.d.ts +1 -2
  27. package/dist/source_nodejs/index.js +31 -38
  28. package/dist/source_nodejs/index.js.map +1 -1
  29. package/dist/source_nodejs/index.mjs +3 -5
  30. package/package.json +9 -4
  31. package/dist/chunk-46EEAYVO.mjs.map +0 -1
  32. package/dist/chunk-GNEWUC7X.mjs.map +0 -1
  33. package/dist/chunk-VI4S2NM5.mjs +0 -38
  34. package/dist/chunk-VI4S2NM5.mjs.map +0 -1
  35. package/dist/index_web.d.mts +0 -5
  36. package/dist/index_web.d.ts +0 -5
  37. package/dist/index_web.js +0 -6414
  38. package/dist/index_web.js.map +0 -1
  39. package/dist/index_web.mjs +0 -195
  40. package/dist/index_web.mjs.map +0 -1
  41. package/index.mjs +0 -1
  42. package/index_web.ts +0 -1
  43. package/web.d.ts +0 -1
  44. 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
 
@@ -515,7 +457,7 @@ function readTag(buf, pos) {
515
457
  }
516
458
  return { start, tag, position: pos, length };
517
459
  }
518
- function _readStruct(buf, blockInfo) {
460
+ function readStruct(buf, blockInfo) {
519
461
  const length = blockInfo.length;
520
462
  let cursor = blockInfo.position;
521
463
  const end = blockInfo.position + length;
@@ -577,7 +519,7 @@ function _readIntegerAsByteString(buffer, block) {
577
519
  }
578
520
  function _readListOfInteger(buffer) {
579
521
  const block = readTag(buffer, 0);
580
- const inner_blocks = _readStruct(buffer, block);
522
+ const inner_blocks = readStruct(buffer, block);
581
523
  return inner_blocks.map((innerBlock) => {
582
524
  return _readIntegerAsByteString(buffer, innerBlock);
583
525
  });
@@ -611,24 +553,24 @@ function _readObjectIdentifier(buffer, block) {
611
553
  name: oid_map[oid] ? oid_map[oid].d : oid
612
554
  };
613
555
  }
614
- function _readAlgorithmIdentifier(buffer, block) {
615
- const inner_blocks = _readStruct(buffer, block);
556
+ function readAlgorithmIdentifier(buffer, block) {
557
+ const inner_blocks = readStruct(buffer, block);
616
558
  return {
617
559
  identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name
618
560
  };
619
561
  }
620
562
  function _readECCAlgorithmIdentifier(buffer, block) {
621
- const inner_blocks = _readStruct(buffer, block);
563
+ const inner_blocks = readStruct(buffer, block);
622
564
  return {
623
565
  identifier: _readObjectIdentifier(buffer, inner_blocks[1]).name
624
566
  // difference with RSA as algorithm is second element of nested block
625
567
  };
626
568
  }
627
- function _readSignatureValueBin(buffer, block) {
569
+ function readSignatureValueBin(buffer, block) {
628
570
  return _readBitString(buffer, block).data;
629
571
  }
630
- function _readSignatureValue(buffer, block) {
631
- return _readSignatureValueBin(buffer, block).toString("hex");
572
+ function readSignatureValue(buffer, block) {
573
+ return readSignatureValueBin(buffer, block).toString("hex");
632
574
  }
633
575
  function _readLongIntegerValue(buffer, block) {
634
576
  (0, import_assert.default)(block.tag === 2 /* INTEGER */, "expecting a INTEGER tag");
@@ -709,18 +651,15 @@ function _readValue(buffer, block) {
709
651
  throw new Error("Invalid tag 0x" + block.tag.toString(16));
710
652
  }
711
653
  }
712
- function compactDirectoryName(d) {
713
- return JSON.stringify(d);
714
- }
715
654
  function _readDirectoryName(buffer, block) {
716
- const set_blocks = _readStruct(buffer, block);
655
+ const set_blocks = readStruct(buffer, block);
717
656
  const names = {};
718
657
  for (const set_block of set_blocks) {
719
658
  (0, import_assert.default)(set_block.tag === 49);
720
- const blocks = _readStruct(buffer, set_block);
659
+ const blocks = readStruct(buffer, set_block);
721
660
  (0, import_assert.default)(blocks.length === 1);
722
661
  (0, import_assert.default)(blocks[0].tag === 48);
723
- const sequenceBlock = _readStruct(buffer, blocks[0]);
662
+ const sequenceBlock = readStruct(buffer, blocks[0]);
724
663
  (0, import_assert.default)(sequenceBlock.length === 2);
725
664
  const type = _readObjectIdentifier(buffer, sequenceBlock[0]);
726
665
  names[type.name] = _readValue(buffer, sequenceBlock[1]);
@@ -738,11 +677,23 @@ function _readTime(buffer, block) {
738
677
  return _readValue(buffer, block);
739
678
  }
740
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
+
741
691
  // source/crypto_explore_certificate.ts
692
+ var import_assert2 = __toESM(require("assert"));
742
693
  var doDebug = false;
743
694
  function _readAttributeTypeAndValue(buffer, block) {
744
- let inner_blocks = _readStruct(buffer, block);
745
- inner_blocks = _readStruct(buffer, inner_blocks[0]);
695
+ let inner_blocks = readStruct(buffer, block);
696
+ inner_blocks = readStruct(buffer, inner_blocks[0]);
746
697
  const data = {
747
698
  identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name,
748
699
  value: _readValue(buffer, inner_blocks[1])
@@ -754,7 +705,7 @@ function _readAttributeTypeAndValue(buffer, block) {
754
705
  return result;
755
706
  }
756
707
  function _readRelativeDistinguishedName(buffer, block) {
757
- const inner_blocks = _readStruct(buffer, block);
708
+ const inner_blocks = readStruct(buffer, block);
758
709
  const data = inner_blocks.map((block2) => _readAttributeTypeAndValue(buffer, block2));
759
710
  const result = {};
760
711
  for (const e of data) {
@@ -766,7 +717,7 @@ function _readName(buffer, block) {
766
717
  return _readRelativeDistinguishedName(buffer, block);
767
718
  }
768
719
  function _readValidity(buffer, block) {
769
- const inner_blocks = _readStruct(buffer, block);
720
+ const inner_blocks = readStruct(buffer, block);
770
721
  return {
771
722
  notBefore: _readTime(buffer, inner_blocks[0]),
772
723
  notAfter: _readTime(buffer, inner_blocks[1])
@@ -774,27 +725,27 @@ function _readValidity(buffer, block) {
774
725
  }
775
726
  function _readAuthorityKeyIdentifier(buffer) {
776
727
  const block_info = readTag(buffer, 0);
777
- const blocks = _readStruct(buffer, block_info);
728
+ const blocks = readStruct(buffer, block_info);
778
729
  const keyIdentifier_block = _findBlockAtIndex(blocks, 0);
779
730
  const authorityCertIssuer_block = _findBlockAtIndex(blocks, 1);
780
731
  const authorityCertSerialNumber_block = _findBlockAtIndex(blocks, 2);
781
732
  function _readAuthorityCertIssuer(block) {
782
- const inner_blocks = _readStruct(buffer, block);
733
+ const inner_blocks = readStruct(buffer, block);
783
734
  const directoryName_block = _findBlockAtIndex(inner_blocks, 4);
784
735
  if (directoryName_block) {
785
- const a = _readStruct(buffer, directoryName_block);
736
+ const a = readStruct(buffer, directoryName_block);
786
737
  return _readDirectoryName(buffer, a[0]);
787
738
  } else {
788
739
  throw new Error("Invalid _readAuthorityCertIssuer");
789
740
  }
790
741
  }
791
742
  function _readAuthorityCertIssuerFingerPrint(block) {
792
- const inner_blocks = _readStruct(buffer, block);
743
+ const inner_blocks = readStruct(buffer, block);
793
744
  const directoryName_block = _findBlockAtIndex(inner_blocks, 4);
794
745
  if (!directoryName_block) {
795
746
  return "";
796
747
  }
797
- const a = _readStruct(buffer, directoryName_block);
748
+ const a = readStruct(buffer, directoryName_block);
798
749
  if (a.length < 1) {
799
750
  return "";
800
751
  }
@@ -813,7 +764,7 @@ function _readAuthorityKeyIdentifier(buffer) {
813
764
  }
814
765
  function readBasicConstraint2_5_29_19(buffer, block) {
815
766
  const block_info = readTag(buffer, 0);
816
- const inner_blocks = _readStruct(buffer, block_info).slice(0, 2);
767
+ const inner_blocks = readStruct(buffer, block_info).slice(0, 2);
817
768
  let cA = false;
818
769
  let pathLengthConstraint = 0;
819
770
  let breakControl = 0;
@@ -845,7 +796,7 @@ function _readGeneralNames(buffer, block) {
845
796
  8: { name: "registeredID", type: "OBJECT_IDENTIFIER" },
846
797
  32: { name: "otherName", type: "AnotherName" }
847
798
  };
848
- const blocks = _readStruct(buffer, block);
799
+ const blocks = readStruct(buffer, block);
849
800
  function _readFromType(buffer2, block2, type) {
850
801
  switch (type) {
851
802
  case "IA5String":
@@ -857,15 +808,15 @@ function _readGeneralNames(buffer, block) {
857
808
  const n = {};
858
809
  for (const block2 of blocks) {
859
810
  (0, import_assert2.default)((block2.tag & 128) === 128);
860
- const t = block2.tag & 127;
861
- const type = _data[t];
811
+ const t2 = block2.tag & 127;
812
+ const type = _data[t2];
862
813
  if (!type) {
863
- console.log("_readGeneralNames: INVALID TYPE => " + t + " 0x" + t.toString(16));
814
+ console.log("_readGeneralNames: INVALID TYPE => " + t2 + " 0x" + t2.toString(16));
864
815
  continue;
865
816
  }
866
- if (t == 32) {
817
+ if (t2 == 32) {
867
818
  n[type.name] = n[type.name] || [];
868
- const blocks2 = _readStruct(buffer, block2);
819
+ const blocks2 = readStruct(buffer, block2);
869
820
  const name = _readObjectIdentifier(buffer, blocks2[0]).name;
870
821
  const buf = _getBlock(buffer, blocks2[1]);
871
822
  const b = readTag(buf, 0);
@@ -918,7 +869,7 @@ function readKeyUsage(oid, buffer) {
918
869
  function readExtKeyUsage(oid, buffer) {
919
870
  (0, import_assert2.default)(oid === "2.5.29.37");
920
871
  const block_info = readTag(buffer, 0);
921
- const inner_blocks = _readStruct(buffer, block_info);
872
+ const inner_blocks = readStruct(buffer, block_info);
922
873
  const extKeyUsage = {
923
874
  serverAuth: false,
924
875
  clientAuth: false,
@@ -938,13 +889,13 @@ function readExtKeyUsage(oid, buffer) {
938
889
  }
939
890
  function _readSubjectPublicKey(buffer) {
940
891
  const block_info = readTag(buffer, 0);
941
- const blocks = _readStruct(buffer, block_info);
892
+ const blocks = readStruct(buffer, block_info);
942
893
  return {
943
894
  modulus: buffer.subarray(blocks[0].position + 1, blocks[0].position + blocks[0].length)
944
895
  };
945
896
  }
946
897
  function _readExtension(buffer, block) {
947
- const inner_blocks = _readStruct(buffer, block);
898
+ const inner_blocks = readStruct(buffer, block);
948
899
  if (inner_blocks.length === 3) {
949
900
  (0, import_assert2.default)(inner_blocks[1].tag === 1 /* BOOLEAN */);
950
901
  inner_blocks[1] = inner_blocks[2];
@@ -984,8 +935,8 @@ function _readExtension(buffer, block) {
984
935
  }
985
936
  function _readExtensions(buffer, block) {
986
937
  (0, import_assert2.default)(block.tag === 163);
987
- let inner_blocks = _readStruct(buffer, block);
988
- inner_blocks = _readStruct(buffer, inner_blocks[0]);
938
+ let inner_blocks = readStruct(buffer, block);
939
+ inner_blocks = readStruct(buffer, inner_blocks[0]);
989
940
  const extensions = inner_blocks.map((block2) => _readExtension(buffer, block2));
990
941
  const result = {};
991
942
  for (const e of extensions) {
@@ -994,8 +945,8 @@ function _readExtensions(buffer, block) {
994
945
  return result;
995
946
  }
996
947
  function _readSubjectPublicKeyInfo(buffer, block) {
997
- const inner_blocks = _readStruct(buffer, block);
998
- const algorithm = _readAlgorithmIdentifier(buffer, inner_blocks[0]);
948
+ const inner_blocks = readStruct(buffer, block);
949
+ const algorithm = readAlgorithmIdentifier(buffer, inner_blocks[0]);
999
950
  const subjectPublicKey = _readBitString(buffer, inner_blocks[1]);
1000
951
  const data = subjectPublicKey.data;
1001
952
  const values = _readListOfInteger(data);
@@ -1008,7 +959,7 @@ function _readSubjectPublicKeyInfo(buffer, block) {
1008
959
  };
1009
960
  }
1010
961
  function _readSubjectECCPublicKeyInfo(buffer, block) {
1011
- const inner_blocks = _readStruct(buffer, block);
962
+ const inner_blocks = readStruct(buffer, block);
1012
963
  const algorithm = _readECCAlgorithmIdentifier(buffer, inner_blocks[0]);
1013
964
  const subjectPublicKey = _readBitString(buffer, inner_blocks[1]);
1014
965
  const data = subjectPublicKey.data;
@@ -1021,13 +972,13 @@ function _readSubjectECCPublicKeyInfo(buffer, block) {
1021
972
  };
1022
973
  }
1023
974
  function readTbsCertificate(buffer, block) {
1024
- const blocks = _readStruct(buffer, block);
975
+ const blocks = readStruct(buffer, block);
1025
976
  let version, serialNumber, signature, issuer, validity, subject, subjectFingerPrint, extensions;
1026
977
  let subjectPublicKeyInfo;
1027
978
  if (blocks.length === 6) {
1028
979
  version = 1;
1029
980
  serialNumber = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, blocks[0]));
1030
- signature = _readAlgorithmIdentifier(buffer, blocks[1]);
981
+ signature = readAlgorithmIdentifier(buffer, blocks[1]);
1031
982
  issuer = _readName(buffer, blocks[2]);
1032
983
  validity = _readValidity(buffer, blocks[3]);
1033
984
  subject = _readName(buffer, blocks[4]);
@@ -1041,13 +992,13 @@ function readTbsCertificate(buffer, block) {
1041
992
  }
1042
993
  version = _readVersionValue(buffer, version_block) + 1;
1043
994
  serialNumber = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, blocks[1]));
1044
- signature = _readAlgorithmIdentifier(buffer, blocks[2]);
995
+ signature = readAlgorithmIdentifier(buffer, blocks[2]);
1045
996
  issuer = _readName(buffer, blocks[3]);
1046
997
  validity = _readValidity(buffer, blocks[4]);
1047
998
  subject = _readName(buffer, blocks[5]);
1048
999
  subjectFingerPrint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[5])));
1049
- const inner_block = _readStruct(buffer, blocks[6]);
1050
- 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;
1051
1002
  switch (what_type) {
1052
1003
  case "rsaEncryption": {
1053
1004
  subjectPublicKeyInfo = _readSubjectPublicKeyInfo(buffer, blocks[6]);
@@ -1083,11 +1034,11 @@ function exploreCertificate(certificate) {
1083
1034
  (0, import_assert2.default)(certificate instanceof Buffer);
1084
1035
  if (!certificate._exploreCertificate_cache) {
1085
1036
  const block_info = readTag(certificate, 0);
1086
- const blocks = _readStruct(certificate, block_info);
1037
+ const blocks = readStruct(certificate, block_info);
1087
1038
  certificate._exploreCertificate_cache = {
1088
1039
  tbsCertificate: readTbsCertificate(certificate, blocks[0]),
1089
- signatureAlgorithm: _readAlgorithmIdentifier(certificate, blocks[1]),
1090
- signatureValue: _readSignatureValue(certificate, blocks[2])
1040
+ signatureAlgorithm: readAlgorithmIdentifier(certificate, blocks[1]),
1041
+ signatureValue: readSignatureValue(certificate, blocks[2])
1091
1042
  };
1092
1043
  }
1093
1044
  return certificate._exploreCertificate_cache;
@@ -1178,19 +1129,19 @@ function hexDump(buffer, width) {
1178
1129
  }
1179
1130
  }
1180
1131
  function makeMessageChunkSignature(chunk, options) {
1181
- const signer = (0, import_crypto2.createSign)(options.algorithm);
1132
+ const signer = (0, import_crypto.createSign)(options.algorithm);
1182
1133
  signer.update(chunk);
1183
1134
  const signature = signer.sign(options.privateKey.hidden);
1184
1135
  (0, import_assert3.default)(!options.signatureLength || signature.length === options.signatureLength);
1185
1136
  return signature;
1186
1137
  }
1187
1138
  function verifyMessageChunkSignature(blockToVerify, signature, options) {
1188
- const verify = (0, import_crypto2.createVerify)(options.algorithm);
1139
+ const verify = (0, import_crypto.createVerify)(options.algorithm);
1189
1140
  verify.update(blockToVerify);
1190
1141
  return verify.verify(options.publicKey, signature);
1191
1142
  }
1192
1143
  function makeSHA1Thumbprint(buffer) {
1193
- return (0, import_crypto2.createHash)("sha1").update(buffer).digest();
1144
+ return (0, import_crypto.createHash)("sha1").update(buffer).digest();
1194
1145
  }
1195
1146
  var RSA_PKCS1_OAEP_PADDING = import_constants.default.RSA_PKCS1_OAEP_PADDING;
1196
1147
  var RSA_PKCS1_PADDING = import_constants.default.RSA_PKCS1_PADDING;
@@ -1205,7 +1156,7 @@ function publicEncrypt_native(buffer, publicKey, algorithm) {
1205
1156
  if (algorithm === void 0) {
1206
1157
  algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
1207
1158
  }
1208
- return (0, import_crypto2.publicEncrypt)(
1159
+ return (0, import_crypto.publicEncrypt)(
1209
1160
  {
1210
1161
  key: publicKey,
1211
1162
  padding: algorithm
@@ -1218,7 +1169,7 @@ function privateDecrypt_native(buffer, privateKey, algorithm) {
1218
1169
  algorithm = 4 /* RSA_PKCS1_OAEP_PADDING */;
1219
1170
  }
1220
1171
  try {
1221
- return (0, import_crypto2.privateDecrypt)(
1172
+ return (0, import_crypto.privateDecrypt)(
1222
1173
  {
1223
1174
  key: privateKey.hidden,
1224
1175
  padding: algorithm
@@ -1300,13 +1251,192 @@ function extractPublicKeyFromCertificate(certificate, callback) {
1300
1251
  });
1301
1252
  }
1302
1253
 
1303
- // 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
1304
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"));
1305
1435
  function coerceCertificate(certificate) {
1306
1436
  if (typeof certificate === "string") {
1307
1437
  certificate = convertPEMtoDER(certificate);
1308
1438
  }
1309
- (0, import_assert4.default)(certificate instanceof Buffer);
1439
+ (0, import_assert5.default)(certificate instanceof Buffer);
1310
1440
  return certificate;
1311
1441
  }
1312
1442
  function exploreCertificateInfo(certificate) {
@@ -1333,8 +1463,8 @@ function plus(buf1, buf2) {
1333
1463
  return Buffer.concat([buf1, buf2]);
1334
1464
  }
1335
1465
  function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
1336
- (0, import_assert5.default)(seed instanceof Buffer);
1337
- (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");
1338
1468
  const a = [];
1339
1469
  a[0] = seed;
1340
1470
  let index = 1;
@@ -1347,12 +1477,12 @@ function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
1347
1477
  return p_hash.subarray(0, minLength);
1348
1478
  }
1349
1479
  function computeDerivedKeys(secret, seed, options) {
1350
- (0, import_assert5.default)(Number.isFinite(options.signatureLength));
1351
- (0, import_assert5.default)(Number.isFinite(options.encryptingKeyLength));
1352
- (0, import_assert5.default)(Number.isFinite(options.encryptingBlockSize));
1353
- (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");
1354
1484
  options.sha1or256 = options.sha1or256 || "SHA1";
1355
- (0, import_assert5.default)(typeof options.sha1or256 === "string");
1485
+ (0, import_assert6.default)(typeof options.sha1or256 === "string");
1356
1486
  const offset1 = options.signingKeyLength;
1357
1487
  const offset2 = offset1 + options.encryptingKeyLength;
1358
1488
  const minLength = offset2 + options.encryptingBlockSize;
@@ -1377,7 +1507,7 @@ function removePadding(buffer) {
1377
1507
  return reduceLength(buffer, nbPaddingBytes);
1378
1508
  }
1379
1509
  function verifyChunkSignature(chunk, options) {
1380
- (0, import_assert5.default)(chunk instanceof Buffer);
1510
+ (0, import_assert6.default)(chunk instanceof Buffer);
1381
1511
  let signatureLength = options.signatureLength || 0;
1382
1512
  if (signatureLength === 0) {
1383
1513
  const cert = exploreCertificateInfo(options.publicKey);
@@ -1388,16 +1518,16 @@ function verifyChunkSignature(chunk, options) {
1388
1518
  return verifyMessageChunkSignature(block_to_verify, signature, options);
1389
1519
  }
1390
1520
  function computePaddingFooter(buffer, derivedKeys) {
1391
- (0, import_assert5.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "encryptingBlockSize"));
1521
+ (0, import_assert6.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "encryptingBlockSize"));
1392
1522
  const paddingSize = derivedKeys.encryptingBlockSize - (buffer.length + 1) % derivedKeys.encryptingBlockSize;
1393
1523
  const padding = createFastUninitializedBuffer(paddingSize + 1);
1394
1524
  padding.fill(paddingSize);
1395
1525
  return padding;
1396
1526
  }
1397
1527
  function derivedKeys_algorithm(derivedKeys) {
1398
- (0, import_assert5.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "algorithm"));
1528
+ (0, import_assert6.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "algorithm"));
1399
1529
  const algorithm = derivedKeys.algorithm || "aes-128-cbc";
1400
- (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");
1401
1531
  return algorithm;
1402
1532
  }
1403
1533
  function encryptBufferWithDerivedKeys(buffer, derivedKeys) {
@@ -1423,12 +1553,12 @@ function decryptBufferWithDerivedKeys(buffer, derivedKeys) {
1423
1553
  return Buffer.concat(decrypted_chunks);
1424
1554
  }
1425
1555
  function makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys) {
1426
- (0, import_assert5.default)(message instanceof Buffer);
1427
- (0, import_assert5.default)(derivedKeys.signingKey instanceof Buffer);
1428
- (0, import_assert5.default)(typeof derivedKeys.sha1or256 === "string");
1429
- (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");
1430
1560
  const signature = (0, import_crypto3.createHmac)(derivedKeys.sha1or256, derivedKeys.signingKey).update(message).digest();
1431
- (0, import_assert5.default)(signature.length === derivedKeys.signatureLength);
1561
+ (0, import_assert6.default)(signature.length === derivedKeys.signatureLength);
1432
1562
  return signature;
1433
1563
  }
1434
1564
  function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
@@ -1438,126 +1568,31 @@ function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
1438
1568
  return computedSignature.toString("hex") === expectedSignature.toString("hex");
1439
1569
  }
1440
1570
 
1441
- // source/crypto_utils2.ts
1442
- var import_assert6 = __toESM(require("assert"));
1443
- var import_jsrsasign2 = __toESM(require("jsrsasign"));
1444
- function rsaLengthPrivateKey(key) {
1445
- const keyPem = typeof key.hidden === "string" ? key.hidden : key.hidden.export({ type: "pkcs1", format: "pem" }).toString();
1446
- const a = import_jsrsasign2.default.KEYUTIL.getKey(keyPem);
1447
- return a.n.toString(16).length / 2;
1448
- }
1449
- function toPem2(raw_key, pem) {
1450
- if (raw_key.hidden) {
1451
- return toPem2(raw_key.hidden, pem);
1452
- }
1453
- (0, import_assert6.default)(raw_key, "expecting a key");
1454
- (0, import_assert6.default)(typeof pem === "string");
1455
- if (isKeyObject(raw_key)) {
1456
- const _raw_key = raw_key;
1457
- if (pem === "RSA PRIVATE KEY") {
1458
- return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs1" }).toString());
1459
- } else if (pem === "PRIVATE KEY") {
1460
- return removeTrailingLF(_raw_key.export({ format: "pem", type: "pkcs8" }).toString());
1461
- } else {
1462
- throw new Error("Unsupported case!");
1463
- }
1464
- }
1465
- return toPem(raw_key, pem);
1466
- }
1467
- function coercePrivateKeyPem(privateKey) {
1468
- return toPem2(privateKey, "PRIVATE KEY");
1469
- }
1470
- function coercePublicKeyPem(publicKey) {
1471
- if (isKeyObject(publicKey)) {
1472
- return publicKey.export({ format: "pem", type: "spki" }).toString();
1473
- }
1474
- (0, import_assert6.default)(typeof publicKey === "string");
1475
- return publicKey;
1476
- }
1477
- function coerceRsaPublicKeyPem(publicKey) {
1478
- if (isKeyObject(publicKey)) {
1479
- return publicKey.export({ format: "pem", type: "spki" }).toString();
1480
- }
1481
- (0, import_assert6.default)(typeof publicKey === "string");
1482
- return publicKey;
1483
- }
1484
- function rsaLengthPublicKey(key) {
1485
- key = coercePublicKeyPem(key);
1486
- (0, import_assert6.default)(typeof key === "string");
1487
- const a = import_jsrsasign2.default.KEYUTIL.getKey(key);
1488
- return a.n.toString(16).length / 2;
1489
- }
1490
- function rsaLengthRsaPublicKey(key) {
1491
- key = coerceRsaPublicKeyPem(key);
1492
- (0, import_assert6.default)(typeof key === "string");
1493
- const a = import_jsrsasign2.default.KEYUTIL.getKey(key);
1494
- return a.n.toString(16).length / 2;
1495
- }
1496
-
1497
- // source/verify_certificate_signature.ts
1498
- var import_crypto4 = require("crypto");
1499
- function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
1500
- const block_info = readTag(certificateOrCrl, 0);
1501
- const blocks = _readStruct(certificateOrCrl, block_info);
1502
- const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
1503
- const signatureAlgorithm = _readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
1504
- const signatureValue = _readSignatureValueBin(certificateOrCrl, blocks[2]);
1505
- const p = split_der(parentCertificate)[0];
1506
- const certPem = toPem(p, "CERTIFICATE");
1507
- const verify = (0, import_crypto4.createVerify)(signatureAlgorithm.identifier);
1508
- verify.update(bufferToBeSigned);
1509
- verify.end();
1510
- return verify.verify(certPem, signatureValue);
1511
- }
1512
- function verifyCertificateSignature(certificate, parentCertificate) {
1513
- return verifyCertificateOrClrSignature(certificate, parentCertificate);
1514
- }
1515
- function verifyCertificateRevocationListSignature(certificateRevocationList, parentCertificate) {
1516
- return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
1517
- }
1518
- async function verifyCertificateChain(certificateChain) {
1519
- for (let index = 1; index < certificateChain.length; index++) {
1520
- const cert = certificateChain[index - 1];
1521
- const certParent = certificateChain[index];
1522
- const certParentInfo = exploreCertificate(certParent);
1523
- const keyUsage = certParentInfo.tbsCertificate.extensions.keyUsage;
1524
- if (!keyUsage.keyCertSign) {
1525
- return {
1526
- status: "BadCertificateIssuerUseNotAllowed",
1527
- reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing"
1528
- };
1529
- }
1530
- const parentSignChild = verifyCertificateSignature(cert, certParent);
1531
- if (!parentSignChild) {
1532
- return {
1533
- status: "BadCertificateInvalid",
1534
- reason: "One of the certificate in the chain is not signing the previous certificate"
1535
- };
1536
- }
1537
- const certInfo = exploreCertificate(cert);
1538
- if (!certInfo.tbsCertificate.extensions) {
1539
- return {
1540
- status: "BadCertificateInvalid",
1541
- reason: "Cannot find X409 Extension 3 in certificate"
1542
- };
1543
- }
1544
- if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
1545
- return {
1546
- status: "BadCertificateInvalid",
1547
- reason: "Cannot find X409 Extension 3 in certificate (parent)"
1548
- };
1549
- }
1550
- if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
1551
- return {
1552
- status: "BadCertificateInvalid",
1553
- reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
1554
- };
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
+ }
1555
1593
  }
1556
1594
  }
1557
- return {
1558
- status: "Good",
1559
- reason: `certificate chain is valid(length = ${certificateChain.length})`
1560
- };
1595
+ dump(0, 0);
1561
1596
  }
1562
1597
 
1563
1598
  // source/explore_certificate_revocation_list.ts
@@ -1565,20 +1600,20 @@ function readNameForCrl(buffer, block) {
1565
1600
  return _readDirectoryName(buffer, block);
1566
1601
  }
1567
1602
  function _readTbsCertList(buffer, blockInfo) {
1568
- const blocks = _readStruct(buffer, blockInfo);
1603
+ const blocks = readStruct(buffer, blockInfo);
1569
1604
  const hasOptionalVersion = blocks[0].tag === 2 /* INTEGER */;
1570
1605
  if (hasOptionalVersion) {
1571
1606
  const version = _readIntegerValue(buffer, blocks[0]);
1572
- const signature = _readAlgorithmIdentifier(buffer, blocks[1]);
1607
+ const signature = readAlgorithmIdentifier(buffer, blocks[1]);
1573
1608
  const issuer = readNameForCrl(buffer, blocks[2]);
1574
1609
  const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[2])));
1575
1610
  const thisUpdate = _readTime(buffer, blocks[3]);
1576
1611
  const nextUpdate = _readTime(buffer, blocks[4]);
1577
1612
  const revokedCertificates = [];
1578
1613
  if (blocks[5] && blocks[5].tag < 128) {
1579
- const list = _readStruct(buffer, blocks[5]);
1614
+ const list = readStruct(buffer, blocks[5]);
1580
1615
  for (const r of list) {
1581
- const rr = _readStruct(buffer, r);
1616
+ const rr = readStruct(buffer, r);
1582
1617
  const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
1583
1618
  const revocationDate = _readTime(buffer, rr[1]);
1584
1619
  revokedCertificates.push({
@@ -1590,16 +1625,16 @@ function _readTbsCertList(buffer, blockInfo) {
1590
1625
  const ext0 = _findBlockAtIndex(blocks, 0);
1591
1626
  return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
1592
1627
  } else {
1593
- const signature = _readAlgorithmIdentifier(buffer, blocks[0]);
1628
+ const signature = readAlgorithmIdentifier(buffer, blocks[0]);
1594
1629
  const issuer = readNameForCrl(buffer, blocks[1]);
1595
1630
  const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[1])));
1596
1631
  const thisUpdate = _readTime(buffer, blocks[2]);
1597
1632
  const nextUpdate = _readTime(buffer, blocks[3]);
1598
1633
  const revokedCertificates = [];
1599
1634
  if (blocks[4] && blocks[4].tag < 128) {
1600
- const list = _readStruct(buffer, blocks[4]);
1635
+ const list = readStruct(buffer, blocks[4]);
1601
1636
  for (const r of list) {
1602
- const rr = _readStruct(buffer, r);
1637
+ const rr = readStruct(buffer, r);
1603
1638
  const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
1604
1639
  const revocationDate = _readTime(buffer, rr[1]);
1605
1640
  revokedCertificates.push({
@@ -1613,224 +1648,59 @@ function _readTbsCertList(buffer, blockInfo) {
1613
1648
  }
1614
1649
  function exploreCertificateRevocationList(crl) {
1615
1650
  const blockInfo = readTag(crl, 0);
1616
- const blocks = _readStruct(crl, blockInfo);
1651
+ const blocks = readStruct(crl, blockInfo);
1617
1652
  const tbsCertList = _readTbsCertList(crl, blocks[0]);
1618
- const signatureAlgorithm = _readAlgorithmIdentifier(crl, blocks[1]);
1619
- const signatureValue = _readSignatureValueBin(crl, blocks[2]);
1653
+ const signatureAlgorithm = readAlgorithmIdentifier(crl, blocks[1]);
1654
+ const signatureValue = readSignatureValueBin(crl, blocks[2]);
1620
1655
  return { tbsCertList, signatureAlgorithm, signatureValue };
1621
1656
  }
1622
1657
 
1623
1658
  // source/explore_certificate_signing_request.ts
1624
1659
  function _readExtensionRequest(buffer) {
1625
1660
  const block = readTag(buffer, 0);
1626
- const inner_blocks = _readStruct(buffer, block);
1661
+ const inner_blocks = readStruct(buffer, block);
1627
1662
  const extensions = inner_blocks.map((block1) => _readExtension(buffer, block1));
1628
1663
  const result = {};
1629
1664
  for (const e of extensions) {
1630
1665
  result[e.identifier.name] = e.value;
1631
1666
  }
1632
1667
  const { basicConstraints, keyUsage, subjectAltName } = result;
1633
- return { basicConstraints, keyUsage, subjectAltName };
1634
- }
1635
- function readCertificationRequestInfo(buffer, block) {
1636
- const blocks = _readStruct(buffer, block);
1637
- if (blocks.length === 4) {
1638
- const extensionRequestBlock = _findBlockAtIndex(blocks, 0);
1639
- if (!extensionRequestBlock) {
1640
- throw new Error("cannot find extensionRequest block");
1641
- }
1642
- const blocks1 = _readStruct(buffer, extensionRequestBlock);
1643
- const blocks2 = _readStruct(buffer, blocks1[0]);
1644
- const identifier = _readObjectIdentifier(buffer, blocks2[0]);
1645
- if (identifier.name !== "extensionRequest") {
1646
- throw new Error(" Cannot find extension Request in ASN1 block");
1647
- }
1648
- const buf = _getBlock(buffer, blocks2[1]);
1649
- const extensionRequest = _readExtensionRequest(buf);
1650
- return { extensionRequest };
1651
- }
1652
- throw new Error("Invalid CSR or ");
1653
- }
1654
- function exploreCertificateSigningRequest(crl) {
1655
- const blockInfo = readTag(crl, 0);
1656
- const blocks = _readStruct(crl, blockInfo);
1657
- const csrInfo = readCertificationRequestInfo(crl, blocks[0]);
1658
- return csrInfo;
1659
- }
1660
-
1661
- // source/explore_private_key.ts
1662
- function f(buffer, b) {
1663
- return buffer.subarray(b.position + 1, b.position + b.length);
1664
- }
1665
- var doDebug2 = !!process.env.DEBUG;
1666
- function explorePrivateKey(privateKey2) {
1667
- const privateKey1 = privateKey2.hidden;
1668
- const privateKey = typeof privateKey1 === "string" ? convertPEMtoDER(privateKey1) : privateKey1.export({ format: "der", type: "pkcs1" });
1669
- const block_info = readTag(privateKey, 0);
1670
- const blocks = _readStruct(privateKey, block_info);
1671
- if (blocks.length === 9) {
1672
- const version2 = f(privateKey, blocks[0]);
1673
- const modulus2 = f(privateKey, blocks[1]);
1674
- const publicExponent2 = f(privateKey, blocks[2]);
1675
- const privateExponent2 = f(privateKey, blocks[3]);
1676
- const prime12 = f(privateKey, blocks[4]);
1677
- const prime22 = f(privateKey, blocks[5]);
1678
- const exponent12 = f(privateKey, blocks[6]);
1679
- const exponent22 = f(privateKey, blocks[7]);
1680
- return {
1681
- version: version2,
1682
- modulus: modulus2,
1683
- publicExponent: publicExponent2,
1684
- privateExponent: privateExponent2,
1685
- prime1: prime12,
1686
- prime2: prime22,
1687
- exponent1: exponent12,
1688
- exponent2: exponent22
1689
- };
1690
- }
1691
- if (doDebug2) {
1692
- console.log("-------------------- private key:");
1693
- console.log(block_info);
1694
- console.log(
1695
- blocks.map((b2) => ({
1696
- tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
1697
- l: b2.length,
1698
- p: b2.position,
1699
- buff: privateKey.subarray(b2.position, b2.position + b2.length).toString("hex")
1700
- }))
1701
- );
1702
- }
1703
- const b = blocks[2];
1704
- const bb = privateKey.subarray(b.position, b.position + b.length);
1705
- const block_info1 = readTag(bb, 0);
1706
- const blocks1 = _readStruct(bb, block_info1);
1707
- if (doDebug2) {
1708
- console.log(
1709
- blocks1.map((b2) => ({
1710
- tag: TagType[b2.tag] + " 0x" + b2.tag.toString(16),
1711
- l: b2.length,
1712
- p: b2.position,
1713
- buff: bb.subarray(b2.position, b2.position + b2.length).toString("hex")
1714
- }))
1715
- );
1716
- }
1717
- const version = f(bb, blocks1[0]);
1718
- const modulus = f(bb, blocks1[1]);
1719
- const publicExponent = f(bb, blocks1[2]);
1720
- const privateExponent = f(bb, blocks1[3]);
1721
- const prime1 = f(bb, blocks1[4]);
1722
- const prime2 = f(bb, blocks1[5]);
1723
- const exponent1 = f(bb, blocks1[6]);
1724
- const exponent2 = f(bb, blocks1[7]);
1725
- return {
1726
- version,
1727
- modulus,
1728
- publicExponent,
1729
- privateExponent,
1730
- prime1,
1731
- prime2,
1732
- exponent1,
1733
- exponent2
1734
- };
1735
- }
1736
-
1737
- // source/public_private_match.ts
1738
- function publicKeyAndPrivateKeyMatches(certificate, privateKey) {
1739
- const i = exploreCertificate(certificate);
1740
- const j = explorePrivateKey(privateKey);
1741
- const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
1742
- const modulus2 = j.modulus;
1743
- if (modulus1.length != modulus2.length) {
1744
- return false;
1745
- }
1746
- return modulus1.toString("hex") === modulus2.toString("hex");
1747
- }
1748
- function certificateMatchesPrivateKeyPEM(certificate, privateKey, blockSize) {
1749
- const initialBuffer = Buffer.from("Lorem Ipsum");
1750
- const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize);
1751
- const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
1752
- const finalString = decryptedBuffer.toString("utf-8");
1753
- return initialBuffer.toString("utf-8") === finalString;
1754
- }
1755
- function certificateMatchesPrivateKey(certificate, privateKey) {
1756
- const e = explorePrivateKey(privateKey);
1757
- const blockSize = e.modulus.length;
1758
- const certificatePEM = toPem(certificate, "CERTIFICATE");
1759
- return certificateMatchesPrivateKeyPEM(certificatePEM, privateKey, blockSize);
1760
- }
1761
-
1762
- // source/x509/_crypto.ts
1763
- var x509 = __toESM(require("@peculiar/x509"));
1764
- var import_webcrypto = require("@peculiar/webcrypto");
1765
- var import_crypto5 = __toESM(require("crypto"));
1766
- var x5092 = __toESM(require("@peculiar/x509"));
1767
- var doDebug3 = false;
1768
- var _crypto;
1769
- var ignoreCrypto = process.env.IGNORE_SUBTLE_FROM_CRYPTO;
1770
- if (typeof window === "undefined") {
1771
- _crypto = import_crypto5.default;
1772
- if (!_crypto?.subtle || ignoreCrypto) {
1773
- _crypto = new import_webcrypto.Crypto();
1774
- doDebug3 && console.warn("using @peculiar/webcrypto");
1775
- } else {
1776
- doDebug3 && console.warn("using nodejs crypto (native)");
1777
- }
1778
- x509.cryptoProvider.set(_crypto);
1779
- } else {
1780
- doDebug3 && console.warn("using browser crypto (native)");
1781
- _crypto = crypto;
1782
- x509.cryptoProvider.set(crypto);
1783
- }
1784
- function getCrypto() {
1785
- return _crypto || crypto || require("crypto");
1786
- }
1787
-
1788
- // source/x509/create_key_pair.ts
1789
- async function generateKeyPair(modulusLength = 2048) {
1790
- const crypto3 = getCrypto();
1791
- const alg = {
1792
- name: "RSASSA-PKCS1-v1_5",
1793
- hash: { name: "SHA-256" },
1794
- publicExponent: new Uint8Array([1, 0, 1]),
1795
- modulusLength
1796
- };
1797
- const keys = await crypto3.subtle.generateKey(alg, true, ["sign", "verify"]);
1798
- return keys;
1668
+ return { basicConstraints, keyUsage, subjectAltName };
1799
1669
  }
1800
- async function generatePrivateKey(modulusLength = 2048) {
1801
- 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 ");
1802
1688
  }
1803
- async function privateKeyToPEM(privateKey) {
1804
- const crypto3 = getCrypto();
1805
- const privDer = await crypto3.subtle.exportKey("pkcs8", privateKey);
1806
- const privPem = x5092.PemConverter.encode(privDer, "PRIVATE KEY");
1807
- 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;
1808
1694
  }
1809
- async function derToPrivateKey(privDer) {
1810
- const crypto3 = getCrypto();
1811
- return await crypto3.subtle.importKey(
1812
- "pkcs8",
1813
- privDer,
1814
- {
1815
- name: "RSASSA-PKCS1-v1_5",
1816
- hash: { name: "SHA-256" }
1817
- },
1818
- true,
1819
- [
1820
- "sign"
1821
- // "encrypt",
1822
- // "decrypt",
1823
- // "verify",
1824
- // "wrapKey",
1825
- // "unwrapKey",
1826
- // "deriveKey",
1827
- // "deriveBits"
1828
- ]
1829
- );
1695
+
1696
+ // source/make_private_key_from_pem.ts
1697
+ function makePrivateKeyFromPem(privateKeyInPem) {
1698
+ return { hidden: privateKeyInPem };
1830
1699
  }
1831
- async function pemToPrivateKey(pem) {
1832
- const privDer = x5092.PemConverter.decode(pem);
1833
- return derToPrivateKey(privDer[0]);
1700
+
1701
+ // source/make_private_key_thumbprint.ts
1702
+ function makePrivateKeyThumbPrint(privateKey) {
1703
+ return Buffer.alloc(0);
1834
1704
  }
1835
1705
 
1836
1706
  // source/subject.ts
@@ -1915,11 +1785,180 @@ var Subject = class _Subject {
1915
1785
  return this.toStringInternal("/");
1916
1786
  }
1917
1787
  toString() {
1918
- const t = this.toStringForOPCUA();
1919
- return t ? "/" + t : t;
1788
+ const t2 = this.toStringForOPCUA();
1789
+ return t2 ? "/" + t2 : t2;
1920
1790
  }
1921
1791
  };
1922
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
+
1923
1962
  // source/x509/_get_attributes.ts
1924
1963
  var keyUsageApplication = x5092.KeyUsageFlags.keyEncipherment | x5092.KeyUsageFlags.nonRepudiation | x5092.KeyUsageFlags.dataEncipherment | x5092.KeyUsageFlags.keyCertSign | x5092.KeyUsageFlags.digitalSignature;
1925
1964
  var keyUsageCA = x5092.KeyUsageFlags.keyCertSign | x5092.KeyUsageFlags.cRLSign;
@@ -6184,40 +6223,6 @@ async function createSelfSignedCertificate({
6184
6223
  );
6185
6224
  return { cert: cert.toString("pem"), der: cert };
6186
6225
  }
6187
-
6188
- // source/x509/coerce_private_key.ts
6189
- var crypto2 = getCrypto();
6190
- var doDebug4 = false;
6191
- function coercePEMorDerToPrivateKey(privateKeyInDerOrPem) {
6192
- if (typeof privateKeyInDerOrPem === "string") {
6193
- const hidden = createPrivateKeyFromNodeJSCrypto(privateKeyInDerOrPem);
6194
- return { hidden };
6195
- }
6196
- throw new Error("not implemented");
6197
- }
6198
- async function _coercePrivateKey(privateKey) {
6199
- const KeyObject4 = crypto2.KeyObject;
6200
- if (privateKey instanceof Buffer) {
6201
- const privateKey1 = await derToPrivateKey(privateKey);
6202
- return KeyObject4.from(privateKey1);
6203
- } else if (typeof privateKey === "string") {
6204
- try {
6205
- const privateKey1 = await pemToPrivateKey(privateKey);
6206
- return KeyObject4.from(privateKey1);
6207
- } catch (err) {
6208
- doDebug4 && console.log(privateKey);
6209
- throw err;
6210
- }
6211
- } else if (privateKey instanceof KeyObject4) {
6212
- return privateKey;
6213
- }
6214
- throw new Error("Invalid privateKey");
6215
- }
6216
-
6217
- // source/make_private_key_from_pem.ts
6218
- function makePrivateKeyFromPem(privateKeyInPem) {
6219
- return { hidden: privateKeyInPem };
6220
- }
6221
6226
  // Annotate the CommonJS export names for ESM import in node:
6222
6227
  0 && (module.exports = {
6223
6228
  CertificatePurpose,
@@ -6225,28 +6230,8 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6225
6230
  RSA_PKCS1_OAEP_PADDING,
6226
6231
  RSA_PKCS1_PADDING,
6227
6232
  Subject,
6228
- TagType,
6229
6233
  _coercePrivateKey,
6230
- _findBlockAtIndex,
6231
- _getBlock,
6232
- _readAlgorithmIdentifier,
6233
- _readBitString,
6234
- _readBooleanValue,
6235
- _readDirectoryName,
6236
- _readECCAlgorithmIdentifier,
6237
6234
  _readExtension,
6238
- _readIntegerAsByteString,
6239
- _readIntegerValue,
6240
- _readListOfInteger,
6241
- _readLongIntegerValue,
6242
- _readObjectIdentifier,
6243
- _readOctetString,
6244
- _readSignatureValue,
6245
- _readSignatureValueBin,
6246
- _readStruct,
6247
- _readTime,
6248
- _readValue,
6249
- _readVersionValue,
6250
6235
  certificateMatchesPrivateKey,
6251
6236
  coerceCertificate,
6252
6237
  coerceCertificatePem,
@@ -6255,7 +6240,6 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6255
6240
  coercePublicKeyPem,
6256
6241
  coerceRsaPublicKeyPem,
6257
6242
  combine_der,
6258
- compactDirectoryName,
6259
6243
  computeDerivedKeys,
6260
6244
  computePaddingFooter,
6261
6245
  convertPEMtoDER,
@@ -6265,6 +6249,7 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6265
6249
  decryptBufferWithDerivedKeys,
6266
6250
  derToPrivateKey,
6267
6251
  encryptBufferWithDerivedKeys,
6252
+ exploreAsn1,
6268
6253
  exploreCertificate,
6269
6254
  exploreCertificateInfo,
6270
6255
  exploreCertificateRevocationList,
@@ -6272,7 +6257,6 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6272
6257
  explorePrivateKey,
6273
6258
  extractPublicKeyFromCertificate,
6274
6259
  extractPublicKeyFromCertificateSync,
6275
- formatBuffer2DigitHexWithColum,
6276
6260
  generateKeyPair,
6277
6261
  generatePrivateKey,
6278
6262
  hexDump,
@@ -6281,9 +6265,9 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6281
6265
  makeMessageChunkSignature,
6282
6266
  makeMessageChunkSignatureWithDerivedKeys,
6283
6267
  makePrivateKeyFromPem,
6268
+ makePrivateKeyThumbPrint,
6284
6269
  makePseudoRandomBuffer,
6285
6270
  makeSHA1Thumbprint,
6286
- parseBitString,
6287
6271
  pemToPrivateKey,
6288
6272
  privateDecrypt,
6289
6273
  privateDecrypt_long,
@@ -6295,7 +6279,6 @@ function makePrivateKeyFromPem(privateKeyInPem) {
6295
6279
  publicKeyAndPrivateKeyMatches,
6296
6280
  readCertificationRequestInfo,
6297
6281
  readNameForCrl,
6298
- readTag,
6299
6282
  readTbsCertificate,
6300
6283
  reduceLength,
6301
6284
  removePadding,