node-opcua-pki 6.15.0 → 6.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -31,9 +31,17 @@ function adjustDate(params) {
31
31
  assert(params instanceof Object);
32
32
  params.startDate = params.startDate || /* @__PURE__ */ new Date();
33
33
  assert(params.startDate instanceof Date);
34
- params.validity = params.validity || 365;
35
- params.endDate = new Date(params.startDate.getTime());
36
- params.endDate.setDate(params.startDate.getDate() + params.validity);
34
+ if (params.validityMs !== void 0) {
35
+ if (params.validityMs <= 0) {
36
+ throw new RangeError(`validityMs must be > 0 (got ${params.validityMs})`);
37
+ }
38
+ params.endDate = new Date(params.startDate.getTime() + params.validityMs);
39
+ params.validity = Math.ceil(params.validityMs / 864e5);
40
+ } else {
41
+ params.validity = params.validity || 365;
42
+ params.endDate = new Date(params.startDate.getTime());
43
+ params.endDate.setDate(params.startDate.getDate() + params.validity);
44
+ }
37
45
  assert(params.endDate instanceof Date);
38
46
  assert(params.startDate instanceof Date);
39
47
  }
@@ -1354,14 +1362,15 @@ var CertificateAuthority = class {
1354
1362
  * @returns the signed certificate as a DER-encoded buffer
1355
1363
  */
1356
1364
  async signCertificateRequestFromDER(csrDer, options) {
1357
- const validity = options?.validity ?? 365;
1358
1365
  const tmpDir = await fs7.promises.mkdtemp(path5.join(os3.tmpdir(), "pki-sign-"));
1359
1366
  try {
1360
1367
  const csrFile = path5.join(tmpDir, "request.csr");
1361
1368
  const certFile = path5.join(tmpDir, "certificate.pem");
1362
1369
  const csrPem = toPem(csrDer, "CERTIFICATE REQUEST");
1363
1370
  await fs7.promises.writeFile(csrFile, csrPem, "utf-8");
1364
- const signingParams = { validity };
1371
+ const signingParams = {};
1372
+ if (options?.validityMs !== void 0) signingParams.validityMs = options.validityMs;
1373
+ else signingParams.validity = options?.validity ?? 365;
1365
1374
  if (options?.startDate) signingParams.startDate = options.startDate;
1366
1375
  if (options?.dns) signingParams.dns = options.dns;
1367
1376
  if (options?.ip) signingParams.ip = options.ip;
@@ -1377,6 +1386,35 @@ var CertificateAuthority = class {
1377
1386
  });
1378
1387
  }
1379
1388
  }
1389
+ /**
1390
+ * Advertise the validity limits this CA can honor.
1391
+ *
1392
+ * Consumers (notably the GDS server in [`cert_auth.ts`](https://github.com/sterfive/node-opcua-gds))
1393
+ * clamp a requested validity against these bounds before calling
1394
+ * {@link signCertificateRequestFromDER}, so a misconfigured
1395
+ * `defaultCertValidity` cannot ask the CA for something it cannot
1396
+ * produce.
1397
+ *
1398
+ * Defaults match the OpenSSL-backed implementation:
1399
+ * - `minValidityMs = 60_000` (1 minute) — practical floor; the
1400
+ * X.509 spec floor is 1 second but very short certs are rarely
1401
+ * useful and pathological for any real deployment.
1402
+ * - `maxValidityMs = 10 * 365 * 86_400_000` (≈ 10 years) — long
1403
+ * enough for root CAs.
1404
+ * - `validityGranularityMs = 1_000` (1 second) — RFC 5280 §4.1.2.5
1405
+ * floor on `notBefore` / `notAfter`.
1406
+ * - `nativeUnit = "second"` — what `x509Date()` actually encodes.
1407
+ *
1408
+ * @see US-208 — the consumer-side capability story.
1409
+ */
1410
+ getCapabilities() {
1411
+ return {
1412
+ minValidityMs: 6e4,
1413
+ maxValidityMs: 10 * 365 * 864e5,
1414
+ validityGranularityMs: 1e3,
1415
+ nativeUnit: "second"
1416
+ };
1417
+ }
1380
1418
  /**
1381
1419
  * Generate a new RSA key pair, create an internal CSR, sign it
1382
1420
  * with this CA, and return both the certificate and private key
@@ -1394,7 +1432,6 @@ var CertificateAuthority = class {
1394
1432
  */
1395
1433
  async generateKeyPairAndSignDER(options) {
1396
1434
  const keySize = options.keySize ?? 2048;
1397
- const validity = options.validity ?? 365;
1398
1435
  const startDate = options.startDate ?? /* @__PURE__ */ new Date();
1399
1436
  const tmpDir = await fs7.promises.mkdtemp(path5.join(os3.tmpdir(), "pki-keygen-"));
1400
1437
  try {
@@ -1414,13 +1451,15 @@ var CertificateAuthority = class {
1414
1451
  purpose: CertificatePurpose.ForApplication
1415
1452
  });
1416
1453
  const certFile = path5.join(tmpDir, "certificate.pem");
1417
- await this.signCertificateRequest(certFile, csrFile, {
1454
+ const signingParams = {
1418
1455
  applicationUri: options.applicationUri,
1419
1456
  dns: options.dns,
1420
1457
  ip: options.ip,
1421
- startDate,
1422
- validity
1423
- });
1458
+ startDate
1459
+ };
1460
+ if (options.validityMs !== void 0) signingParams.validityMs = options.validityMs;
1461
+ else signingParams.validity = options.validity ?? 365;
1462
+ await this.signCertificateRequest(certFile, csrFile, signingParams);
1424
1463
  const certPem = readCertificatePEM(certFile);
1425
1464
  const certificateDer = convertPEMtoDER(certPem);
1426
1465
  const privateKey = readPrivateKey(privateKeyFile);
@@ -1445,7 +1484,6 @@ var CertificateAuthority = class {
1445
1484
  */
1446
1485
  async generateKeyPairAndSignPFX(options) {
1447
1486
  const keySize = options.keySize ?? 2048;
1448
- const validity = options.validity ?? 365;
1449
1487
  const startDate = options.startDate ?? /* @__PURE__ */ new Date();
1450
1488
  const passphrase = options.passphrase ?? "";
1451
1489
  const tmpDir = await fs7.promises.mkdtemp(path5.join(os3.tmpdir(), "pki-keygen-pfx-"));
@@ -1466,13 +1504,15 @@ var CertificateAuthority = class {
1466
1504
  purpose: CertificatePurpose.ForApplication
1467
1505
  });
1468
1506
  const certFile = path5.join(tmpDir, "certificate.pem");
1469
- await this.signCertificateRequest(certFile, csrFile, {
1507
+ const signingParams = {
1470
1508
  applicationUri: options.applicationUri,
1471
1509
  dns: options.dns,
1472
1510
  ip: options.ip,
1473
- startDate,
1474
- validity
1475
- });
1511
+ startDate
1512
+ };
1513
+ if (options.validityMs !== void 0) signingParams.validityMs = options.validityMs;
1514
+ else signingParams.validity = options.validity ?? 365;
1515
+ await this.signCertificateRequest(certFile, csrFile, signingParams);
1476
1516
  const pfxFile = path5.join(tmpDir, "bundle.pfx");
1477
1517
  await createPFX({
1478
1518
  certificateFile: certFile,