node-opcua-crypto 5.0.0 → 5.1.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.
@@ -596,12 +596,12 @@ import assert2 from "assert";
596
596
  import constants from "constants";
597
597
  import {
598
598
  createHash,
599
+ createPublicKey,
599
600
  createSign,
600
601
  createVerify,
601
602
  privateDecrypt as privateDecrypt1,
602
603
  publicEncrypt as publicEncrypt1
603
604
  } from "crypto";
604
- import jsrsasign from "jsrsasign";
605
605
 
606
606
  // source/buffer_utils.ts
607
607
  var createFastUninitializedBuffer = Buffer.allocUnsafe ? Buffer.allocUnsafe : (size) => {
@@ -791,8 +791,8 @@ function coerceCertificatePem(certificate) {
791
791
  }
792
792
  function extractPublicKeyFromCertificateSync(certificate) {
793
793
  certificate = coerceCertificatePem(certificate);
794
- const key = jsrsasign.KEYUTIL.getKey(certificate);
795
- const publicKeyAsPem = jsrsasign.KEYUTIL.getPEM(key);
794
+ const publicKeyObject = createPublicKey(certificate);
795
+ const publicKeyAsPem = publicKeyObject.export({ format: "pem", type: "spki" }).toString();
796
796
  assert2(typeof publicKeyAsPem === "string");
797
797
  return publicKeyAsPem;
798
798
  }
@@ -1214,13 +1214,154 @@ function combine_der(certificates) {
1214
1214
  return Buffer.concat(certificates);
1215
1215
  }
1216
1216
 
1217
+ // source/explore_certificate_revocation_list.ts
1218
+ function readNameForCrl(buffer, block) {
1219
+ return readDirectoryName(buffer, block);
1220
+ }
1221
+ function _readTbsCertList(buffer, blockInfo) {
1222
+ const blocks = readStruct(buffer, blockInfo);
1223
+ const hasOptionalVersion = blocks[0].tag === 2 /* INTEGER */;
1224
+ if (hasOptionalVersion) {
1225
+ const _version = readIntegerValue(buffer, blocks[0]);
1226
+ const signature = readAlgorithmIdentifier(buffer, blocks[1]);
1227
+ const issuer = readNameForCrl(buffer, blocks[2]);
1228
+ const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(getBlock(buffer, blocks[2])));
1229
+ const thisUpdate = readTime(buffer, blocks[3]);
1230
+ const nextUpdate = readTime(buffer, blocks[4]);
1231
+ const revokedCertificates = [];
1232
+ if (blocks[5] && blocks[5].tag < 128) {
1233
+ const list = readStruct(buffer, blocks[5]);
1234
+ for (const r of list) {
1235
+ const rr = readStruct(buffer, r);
1236
+ const userCertificate = formatBuffer2DigitHexWithColum(readLongIntegerValue(buffer, rr[0]));
1237
+ const revocationDate = readTime(buffer, rr[1]);
1238
+ revokedCertificates.push({
1239
+ revocationDate,
1240
+ userCertificate
1241
+ });
1242
+ }
1243
+ }
1244
+ const _ext0 = findBlockAtIndex(blocks, 0);
1245
+ return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
1246
+ } else {
1247
+ const signature = readAlgorithmIdentifier(buffer, blocks[0]);
1248
+ const issuer = readNameForCrl(buffer, blocks[1]);
1249
+ const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(getBlock(buffer, blocks[1])));
1250
+ const thisUpdate = readTime(buffer, blocks[2]);
1251
+ const nextUpdate = readTime(buffer, blocks[3]);
1252
+ const revokedCertificates = [];
1253
+ if (blocks[4] && blocks[4].tag < 128) {
1254
+ const list = readStruct(buffer, blocks[4]);
1255
+ for (const r of list) {
1256
+ const rr = readStruct(buffer, r);
1257
+ const userCertificate = formatBuffer2DigitHexWithColum(readLongIntegerValue(buffer, rr[0]));
1258
+ const revocationDate = readTime(buffer, rr[1]);
1259
+ revokedCertificates.push({
1260
+ revocationDate,
1261
+ userCertificate
1262
+ });
1263
+ }
1264
+ }
1265
+ return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
1266
+ }
1267
+ }
1268
+ function exploreCertificateRevocationList(crl) {
1269
+ const blockInfo = readTag(crl, 0);
1270
+ const blocks = readStruct(crl, blockInfo);
1271
+ const tbsCertList = _readTbsCertList(crl, blocks[0]);
1272
+ const signatureAlgorithm = readAlgorithmIdentifier(crl, blocks[1]);
1273
+ const signatureValue = readSignatureValueBin(crl, blocks[2]);
1274
+ return { tbsCertList, signatureAlgorithm, signatureValue };
1275
+ }
1276
+
1277
+ // source/verify_certificate_signature.ts
1278
+ import { createVerify as createVerify2 } from "crypto";
1279
+ function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
1280
+ const block_info = readTag(certificateOrCrl, 0);
1281
+ const blocks = readStruct(certificateOrCrl, block_info);
1282
+ const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
1283
+ const signatureAlgorithm = readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
1284
+ const signatureValue = readSignatureValueBin(certificateOrCrl, blocks[2]);
1285
+ const p = split_der(parentCertificate)[0];
1286
+ const certPem = toPem(p, "CERTIFICATE");
1287
+ const verify = createVerify2(signatureAlgorithm.identifier);
1288
+ verify.update(bufferToBeSigned);
1289
+ verify.end();
1290
+ return verify.verify(certPem, signatureValue);
1291
+ }
1292
+ function verifyCertificateSignature(certificate, parentCertificate) {
1293
+ return verifyCertificateOrClrSignature(certificate, parentCertificate);
1294
+ }
1295
+ function verifyCertificateRevocationListSignature(certificateRevocationList, parentCertificate) {
1296
+ return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
1297
+ }
1298
+ async function verifyCertificateChain(certificateChain) {
1299
+ for (let index = 1; index < certificateChain.length; index++) {
1300
+ const cert = certificateChain[index - 1];
1301
+ const certParent = certificateChain[index];
1302
+ const certParentInfo = exploreCertificate(certParent);
1303
+ const keyUsage = certParentInfo.tbsCertificate.extensions?.keyUsage;
1304
+ if (!keyUsage || !keyUsage.keyCertSign) {
1305
+ return {
1306
+ status: "BadCertificateIssuerUseNotAllowed",
1307
+ reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing"
1308
+ };
1309
+ }
1310
+ const parentSignChild = verifyCertificateSignature(cert, certParent);
1311
+ if (!parentSignChild) {
1312
+ return {
1313
+ status: "BadCertificateInvalid",
1314
+ reason: "One of the certificate in the chain is not signing the previous certificate"
1315
+ };
1316
+ }
1317
+ const certInfo = exploreCertificate(cert);
1318
+ if (!certInfo.tbsCertificate.extensions) {
1319
+ return {
1320
+ status: "BadCertificateInvalid",
1321
+ reason: "Cannot find X409 Extension 3 in certificate"
1322
+ };
1323
+ }
1324
+ if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
1325
+ return {
1326
+ status: "BadCertificateInvalid",
1327
+ reason: "Cannot find X409 Extension 3 in certificate (parent)"
1328
+ };
1329
+ }
1330
+ if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
1331
+ return {
1332
+ status: "BadCertificateInvalid",
1333
+ reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
1334
+ };
1335
+ }
1336
+ }
1337
+ return {
1338
+ status: "Good",
1339
+ reason: `certificate chain is valid(length = ${certificateChain.length})`
1340
+ };
1341
+ }
1342
+
1343
+ // source/crl_utils.ts
1344
+ function isCrlIssuedByCertificate(crl, certificate) {
1345
+ const crlInfo = exploreCertificateRevocationList(crl);
1346
+ const certInfo = exploreCertificate(certificate);
1347
+ return crlInfo.tbsCertList.issuerFingerprint === certInfo.tbsCertificate.subjectFingerPrint;
1348
+ }
1349
+ function verifyCrlIssuedByCertificate(crl, certificate) {
1350
+ if (!isCrlIssuedByCertificate(crl, certificate)) {
1351
+ return false;
1352
+ }
1353
+ return verifyCertificateRevocationListSignature(crl, certificate);
1354
+ }
1355
+
1217
1356
  // source/crypto_utils2.ts
1218
1357
  import assert5 from "assert";
1219
- import jsrsasign2 from "jsrsasign";
1358
+ import { createPrivateKey, createPublicKey as createPublicKey2 } from "crypto";
1220
1359
  function rsaLengthPrivateKey(key) {
1221
1360
  const keyPem = typeof key.hidden === "string" ? key.hidden : key.hidden.export({ type: "pkcs1", format: "pem" }).toString();
1222
- const a = jsrsasign2.KEYUTIL.getKey(keyPem);
1223
- return a.n.toString(16).length / 2;
1361
+ const keyObject = createPrivateKey(keyPem);
1362
+ const modulusLength = keyObject.asymmetricKeyDetails?.modulusLength;
1363
+ assert5(modulusLength, "Cannot determine modulus length from private key");
1364
+ return modulusLength / 8;
1224
1365
  }
1225
1366
  function toPem2(raw_key, pem) {
1226
1367
  if (raw_key.hidden) {
@@ -1260,14 +1401,18 @@ function coerceRsaPublicKeyPem(publicKey) {
1260
1401
  function rsaLengthPublicKey(key) {
1261
1402
  key = coercePublicKeyPem(key);
1262
1403
  assert5(typeof key === "string");
1263
- const a = jsrsasign2.KEYUTIL.getKey(key);
1264
- return a.n.toString(16).length / 2;
1404
+ const keyObject = createPublicKey2(key);
1405
+ const modulusLength = keyObject.asymmetricKeyDetails?.modulusLength;
1406
+ assert5(modulusLength, "Cannot determine modulus length from public key");
1407
+ return modulusLength / 8;
1265
1408
  }
1266
1409
  function rsaLengthRsaPublicKey(key) {
1267
1410
  key = coerceRsaPublicKeyPem(key);
1268
1411
  assert5(typeof key === "string");
1269
- const a = jsrsasign2.KEYUTIL.getKey(key);
1270
- return a.n.toString(16).length / 2;
1412
+ const keyObject = createPublicKey2(key);
1413
+ const modulusLength = keyObject.asymmetricKeyDetails?.modulusLength;
1414
+ assert5(modulusLength, "Cannot determine modulus length from public key");
1415
+ return modulusLength / 8;
1271
1416
  }
1272
1417
 
1273
1418
  // source/derived_keys.ts
@@ -1439,66 +1584,6 @@ function exploreAsn1(buffer) {
1439
1584
  dump(0, 0);
1440
1585
  }
1441
1586
 
1442
- // source/explore_certificate_revocation_list.ts
1443
- function readNameForCrl(buffer, block) {
1444
- return readDirectoryName(buffer, block);
1445
- }
1446
- function _readTbsCertList(buffer, blockInfo) {
1447
- const blocks = readStruct(buffer, blockInfo);
1448
- const hasOptionalVersion = blocks[0].tag === 2 /* INTEGER */;
1449
- if (hasOptionalVersion) {
1450
- const _version = readIntegerValue(buffer, blocks[0]);
1451
- const signature = readAlgorithmIdentifier(buffer, blocks[1]);
1452
- const issuer = readNameForCrl(buffer, blocks[2]);
1453
- const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(getBlock(buffer, blocks[2])));
1454
- const thisUpdate = readTime(buffer, blocks[3]);
1455
- const nextUpdate = readTime(buffer, blocks[4]);
1456
- const revokedCertificates = [];
1457
- if (blocks[5] && blocks[5].tag < 128) {
1458
- const list = readStruct(buffer, blocks[5]);
1459
- for (const r of list) {
1460
- const rr = readStruct(buffer, r);
1461
- const userCertificate = formatBuffer2DigitHexWithColum(readLongIntegerValue(buffer, rr[0]));
1462
- const revocationDate = readTime(buffer, rr[1]);
1463
- revokedCertificates.push({
1464
- revocationDate,
1465
- userCertificate
1466
- });
1467
- }
1468
- }
1469
- const _ext0 = findBlockAtIndex(blocks, 0);
1470
- return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
1471
- } else {
1472
- const signature = readAlgorithmIdentifier(buffer, blocks[0]);
1473
- const issuer = readNameForCrl(buffer, blocks[1]);
1474
- const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(getBlock(buffer, blocks[1])));
1475
- const thisUpdate = readTime(buffer, blocks[2]);
1476
- const nextUpdate = readTime(buffer, blocks[3]);
1477
- const revokedCertificates = [];
1478
- if (blocks[4] && blocks[4].tag < 128) {
1479
- const list = readStruct(buffer, blocks[4]);
1480
- for (const r of list) {
1481
- const rr = readStruct(buffer, r);
1482
- const userCertificate = formatBuffer2DigitHexWithColum(readLongIntegerValue(buffer, rr[0]));
1483
- const revocationDate = readTime(buffer, rr[1]);
1484
- revokedCertificates.push({
1485
- revocationDate,
1486
- userCertificate
1487
- });
1488
- }
1489
- }
1490
- return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates };
1491
- }
1492
- }
1493
- function exploreCertificateRevocationList(crl) {
1494
- const blockInfo = readTag(crl, 0);
1495
- const blocks = readStruct(crl, blockInfo);
1496
- const tbsCertList = _readTbsCertList(crl, blocks[0]);
1497
- const signatureAlgorithm = readAlgorithmIdentifier(crl, blocks[1]);
1498
- const signatureValue = readSignatureValueBin(crl, blocks[2]);
1499
- return { tbsCertList, signatureAlgorithm, signatureValue };
1500
- }
1501
-
1502
1587
  // source/explore_certificate_signing_request.ts
1503
1588
  function _readExtensionRequest(buffer) {
1504
1589
  const block = readTag(buffer, 0);
@@ -1746,72 +1831,6 @@ var Subject = class _Subject {
1746
1831
  }
1747
1832
  };
1748
1833
 
1749
- // source/verify_certificate_signature.ts
1750
- import { createVerify as createVerify2 } from "crypto";
1751
- function verifyCertificateOrClrSignature(certificateOrCrl, parentCertificate) {
1752
- const block_info = readTag(certificateOrCrl, 0);
1753
- const blocks = readStruct(certificateOrCrl, block_info);
1754
- const bufferToBeSigned = certificateOrCrl.subarray(block_info.position, blocks[1].position - 2);
1755
- const signatureAlgorithm = readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
1756
- const signatureValue = readSignatureValueBin(certificateOrCrl, blocks[2]);
1757
- const p = split_der(parentCertificate)[0];
1758
- const certPem = toPem(p, "CERTIFICATE");
1759
- const verify = createVerify2(signatureAlgorithm.identifier);
1760
- verify.update(bufferToBeSigned);
1761
- verify.end();
1762
- return verify.verify(certPem, signatureValue);
1763
- }
1764
- function verifyCertificateSignature(certificate, parentCertificate) {
1765
- return verifyCertificateOrClrSignature(certificate, parentCertificate);
1766
- }
1767
- function verifyCertificateRevocationListSignature(certificateRevocationList, parentCertificate) {
1768
- return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
1769
- }
1770
- async function verifyCertificateChain(certificateChain) {
1771
- for (let index = 1; index < certificateChain.length; index++) {
1772
- const cert = certificateChain[index - 1];
1773
- const certParent = certificateChain[index];
1774
- const certParentInfo = exploreCertificate(certParent);
1775
- const keyUsage = certParentInfo.tbsCertificate.extensions?.keyUsage;
1776
- if (!keyUsage || !keyUsage.keyCertSign) {
1777
- return {
1778
- status: "BadCertificateIssuerUseNotAllowed",
1779
- reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing"
1780
- };
1781
- }
1782
- const parentSignChild = verifyCertificateSignature(cert, certParent);
1783
- if (!parentSignChild) {
1784
- return {
1785
- status: "BadCertificateInvalid",
1786
- reason: "One of the certificate in the chain is not signing the previous certificate"
1787
- };
1788
- }
1789
- const certInfo = exploreCertificate(cert);
1790
- if (!certInfo.tbsCertificate.extensions) {
1791
- return {
1792
- status: "BadCertificateInvalid",
1793
- reason: "Cannot find X409 Extension 3 in certificate"
1794
- };
1795
- }
1796
- if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
1797
- return {
1798
- status: "BadCertificateInvalid",
1799
- reason: "Cannot find X409 Extension 3 in certificate (parent)"
1800
- };
1801
- }
1802
- if (certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !== certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier) {
1803
- return {
1804
- status: "BadCertificateInvalid",
1805
- reason: "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate"
1806
- };
1807
- }
1808
- }
1809
- return {
1810
- status: "Good",
1811
- reason: `certificate chain is valid(length = ${certificateChain.length})`
1812
- };
1813
- }
1814
-
1815
1834
  // source/x509/_crypto.ts
1816
1835
  import nativeCrypto from "crypto";
1817
1836
  import { Crypto as PeculiarWebCrypto } from "@peculiar/webcrypto";
@@ -6367,6 +6386,14 @@ export {
6367
6386
  exploreCertificate,
6368
6387
  split_der,
6369
6388
  combine_der,
6389
+ readNameForCrl,
6390
+ exploreCertificateRevocationList,
6391
+ verifyCertificateOrClrSignature,
6392
+ verifyCertificateSignature,
6393
+ verifyCertificateRevocationListSignature,
6394
+ verifyCertificateChain,
6395
+ isCrlIssuedByCertificate,
6396
+ verifyCrlIssuedByCertificate,
6370
6397
  rsaLengthPrivateKey,
6371
6398
  toPem2,
6372
6399
  coercePrivateKeyPem,
@@ -6387,8 +6414,6 @@ export {
6387
6414
  makeMessageChunkSignatureWithDerivedKeys,
6388
6415
  verifyChunkSignatureWithDerivedKeys,
6389
6416
  exploreAsn1,
6390
- readNameForCrl,
6391
- exploreCertificateRevocationList,
6392
6417
  readCertificationRequestInfo,
6393
6418
  exploreCertificateSigningRequest,
6394
6419
  explorePrivateKey,
@@ -6397,10 +6422,6 @@ export {
6397
6422
  publicKeyAndPrivateKeyMatches,
6398
6423
  certificateMatchesPrivateKey,
6399
6424
  Subject,
6400
- verifyCertificateOrClrSignature,
6401
- verifyCertificateSignature,
6402
- verifyCertificateRevocationListSignature,
6403
- verifyCertificateChain,
6404
6425
  generateKeyPair,
6405
6426
  generatePrivateKey,
6406
6427
  privateKeyToPEM,
@@ -6480,4 +6501,4 @@ asn1js/build/index.es.js:
6480
6501
  *
6481
6502
  *)
6482
6503
  */
6483
- //# sourceMappingURL=chunk-BIS3W2GR.mjs.map
6504
+ //# sourceMappingURL=chunk-ULG5CYBT.mjs.map