@blamejs/pki 0.8.31 → 0.8.48

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 (87) hide show
  1. package/CHANGELOG.md +393 -0
  2. package/README.md +66 -15
  3. package/bin/pki.js +716 -30
  4. package/examples/issue-a-certificate.js +90 -0
  5. package/examples/post-quantum.js +66 -0
  6. package/examples/read-a-certificate.js +56 -0
  7. package/examples/revoke-and-check.js +86 -0
  8. package/examples/sign-and-verify-cms.js +63 -0
  9. package/index.js +18 -5
  10. package/lib/acme.js +223 -74
  11. package/lib/alt-sig.js +441 -0
  12. package/lib/asn1-der.js +48 -25
  13. package/lib/attrcert-sign.js +27 -19
  14. package/lib/cbor-det.js +24 -14
  15. package/lib/cmc-build.js +9 -2
  16. package/lib/cmc-verify.js +14 -3
  17. package/lib/cmp-build.js +17 -5
  18. package/lib/cmp-session.js +24 -11
  19. package/lib/cmp-verify.js +28 -19
  20. package/lib/cms-compress.js +3 -1
  21. package/lib/cms-decrypt.js +102 -7
  22. package/lib/cms-encrypt.js +59 -7
  23. package/lib/cms-sign.js +31 -9
  24. package/lib/cms-verify.js +22 -18
  25. package/lib/composite-kem.js +30 -18
  26. package/lib/composite-sig.js +29 -14
  27. package/lib/constants.js +26 -0
  28. package/lib/crl-sign.js +111 -19
  29. package/lib/crmf-sign.js +167 -15
  30. package/lib/csr-sign.js +117 -6
  31. package/lib/ct.js +939 -48
  32. package/lib/est.js +29 -16
  33. package/lib/framework-error.js +12 -0
  34. package/lib/guard-async.js +1 -1
  35. package/lib/guard-bytes.js +103 -20
  36. package/lib/guard-crypto.js +205 -3
  37. package/lib/guard-encoding.js +6 -1
  38. package/lib/guard-identifier.js +55 -8
  39. package/lib/guard-intrinsic.js +43 -0
  40. package/lib/guard-json.js +122 -9
  41. package/lib/guard-limits.js +12 -1
  42. package/lib/guard-list.js +63 -8
  43. package/lib/guard-name.js +167 -20
  44. package/lib/guard-range.js +43 -5
  45. package/lib/guard-text.js +41 -0
  46. package/lib/guard-verdict.js +39 -8
  47. package/lib/hpke.js +688 -42
  48. package/lib/http-transport.js +156 -4
  49. package/lib/identity-match.js +4 -4
  50. package/lib/inspect.js +33 -6
  51. package/lib/jose.js +93 -25
  52. package/lib/key.js +18 -13
  53. package/lib/lint.js +165 -1
  54. package/lib/merkle.js +233 -27
  55. package/lib/ocsp.js +27 -14
  56. package/lib/oid.js +14 -4
  57. package/lib/path-validate.js +143 -61
  58. package/lib/pkcs12-build.js +7 -2
  59. package/lib/pki-build.js +31 -7
  60. package/lib/possession.js +575 -0
  61. package/lib/related-cert.js +708 -0
  62. package/lib/revocation-fetch.js +6 -6
  63. package/lib/scep.js +15 -5
  64. package/lib/schema-c509.js +5 -2
  65. package/lib/schema-cmc.js +1 -1
  66. package/lib/schema-cmp.js +8 -8
  67. package/lib/schema-cms.js +2 -15
  68. package/lib/schema-csr.js +69 -0
  69. package/lib/schema-engine.js +10 -3
  70. package/lib/schema-pkix.js +228 -2
  71. package/lib/schema-tsp.js +7 -6
  72. package/lib/sign-scheme.js +22 -9
  73. package/lib/sigstore.js +684 -168
  74. package/lib/sleep.js +8 -1
  75. package/lib/smime.js +22 -1
  76. package/lib/tlog.js +1235 -0
  77. package/lib/tls-cert-compress.js +25 -8
  78. package/lib/tsp-sign.js +120 -35
  79. package/lib/tuf.js +1395 -0
  80. package/lib/validator-tpm.js +4 -1
  81. package/lib/verify-seam.js +7 -1
  82. package/lib/webauthn-mds.js +41 -4
  83. package/lib/webauthn.js +43 -25
  84. package/lib/webcrypto.js +48 -46
  85. package/lib/x509-sign.js +225 -19
  86. package/package.json +2 -1
  87. package/sbom.cdx.json +6 -6
package/lib/schema-tsp.js CHANGED
@@ -42,6 +42,7 @@ var oid = require("./oid");
42
42
  var smime = require("./schema-smime");
43
43
  var cms = require("./schema-cms");
44
44
  var frameworkError = require("./framework-error");
45
+ var guard = require("./guard-all");
45
46
 
46
47
  var TspError = frameworkError.TspError;
47
48
  var PemError = frameworkError.PemError;
@@ -177,11 +178,11 @@ var PKI_STATUS_INFO = schema.seq([
177
178
  _assertMinimalNamedBits(bs.unusedBits, bs.bytes);
178
179
  failInfo = { unusedBits: bs.unusedBits, bytes: bs.bytes, bits: _namedBits(bs.bytes) };
179
180
  }
180
- return {
181
+ return guard.verdict.of({
181
182
  status: Number(status),
182
183
  statusString: m.fields.statusString.present ? m.fields.statusString.value.items.map(function (it) { return it.value; }) : null,
183
184
  failInfo: failInfo,
184
- };
185
+ });
185
186
  },
186
187
  });
187
188
 
@@ -213,14 +214,14 @@ var TIME_STAMP_RESP = schema.seq([
213
214
  if (granted && !present) throw NS.E("tsp/missing-token", "a granted TimeStampResp must carry a timeStampToken (RFC 3161 sec. 2.4.2)");
214
215
  if (!granted && present) throw NS.E("tsp/unexpected-token", "a non-granted TimeStampResp must not carry a timeStampToken (RFC 3161 sec. 2.4.2)");
215
216
  if (status.status !== 2 && status.failInfo) throw NS.E("tsp/unexpected-failinfo", "failInfo is present only when the status is rejection(2) (RFC 3161 sec. 2.4.2)");
216
- return {
217
+ return guard.verdict.of({
217
218
  status: status.status,
218
219
  statusString: status.statusString,
219
220
  failInfo: status.failInfo,
220
221
  /** @internal The token is a second parse of bytes carried inside this one, so it takes the
221
222
  * caps this door was given rather than the built-in ceilings. */
222
223
  timeStampToken: present ? parseToken(m.fields.timeStampToken.value.bytes, ctx ? ctx.caps : undefined) : null,
223
- };
224
+ });
224
225
  },
225
226
  });
226
227
 
@@ -552,7 +553,7 @@ function signerCertificateDer(token) {
552
553
  * one key. Two that name two different certificates leave which of them signed the token
553
554
  * unsettled, so this answers with nothing and the caller says so, rather than taking one of
554
555
  * them. */
555
- if (found !== null) return Buffer.compare(found, named) === 0 ? found : null;
556
+ if (found !== null) return guard.bytes.compare(found, named) === 0 ? found : null;
556
557
  found = named;
557
558
  }
558
559
  return found;
@@ -576,7 +577,7 @@ function _essNamedCertificate(token, attr) {
576
577
  var element = certs[j];
577
578
  if (!element || element.tagClass !== "universal" || !Buffer.isBuffer(element.bytes)) continue;
578
579
  var actual = digest().update(element.bytes).digest();
579
- if (actual.length === want.length && Buffer.compare(actual, want) === 0) return element.bytes;
580
+ if (actual.length === want.length && guard.bytes.compare(actual, want) === 0) return element.bytes;
580
581
  }
581
582
  return null;
582
583
  }
@@ -5,7 +5,6 @@
5
5
 
6
6
  var asn1 = require("./asn1-der");
7
7
  var oid = require("./oid");
8
- var nodeCrypto = require("crypto");
9
8
  var pkcs8 = require("./schema-pkcs8");
10
9
  var pkix = require("./schema-pkix");
11
10
  var frameworkError = require("./framework-error");
@@ -14,6 +13,13 @@ var subtle = webcrypto.webcrypto.subtle;
14
13
  var validator = require("./validator-all");
15
14
  var guard = require("./guard-all");
16
15
  var intrinsic = require("./guard-intrinsic");
16
+ /** @internal The signing and key-import operations, captured at load. The objects were held and their
17
+ * METHODS fetched per call, which leaves them replaceable: a `sign` returning bytes of its own choosing
18
+ * decides the signature that goes out on a certificate, a CRL or a request, and an `importKey` or an
19
+ * `adoptKey` returning a different private key makes the captured signer sign with that key instead. */
20
+ var _subtleSign = intrinsic.uncurry(subtle.sign);
21
+ var _subtleImportKey = intrinsic.uncurry(subtle.importKey);
22
+ var _adoptKey = webcrypto.adoptKey;
17
23
  var compositeSig = require("./composite-sig");
18
24
  var b = asn1.build;
19
25
  function O(name) { return oid.byName(name); }
@@ -28,6 +34,10 @@ var _isBuffer = intrinsic.isBuffer;
28
34
  var _bufferFrom = intrinsic.bufferFrom;
29
35
  var _isU8 = intrinsic.types.isUint8Array;
30
36
  var _resolve = intrinsic.uncurry(intrinsic.promiseResolve);
37
+ /** @internal The promise constructor, captured at load, for the reason `guard-intrinsic` gives: the
38
+ * resolve builds its promise from the receiver, and the bytes a signature is made of travel through
39
+ * these promises. */
40
+ var _Promise = intrinsic.Promise;
31
41
 
32
42
  var HASH = intrinsic.assign(intrinsic.create(null), { sha256: "SHA-256", sha384: "SHA-384", sha512: "SHA-512" });
33
43
  var NODE_DIGEST = intrinsic.assign(intrinsic.create(null), { sha256: "sha256", sha384: "sha384", sha512: "sha512", shake128: "shake128", shake256: "shake256" });
@@ -211,7 +221,7 @@ function _importKey(key, imp, E) {
211
221
  throw E("bad-input", "the signing key is a WebCrypto CryptoKey of type " + _stringify(key.type) + "; a signing key must be a private key (type \"private\")");
212
222
  }
213
223
  _assertKeyMatchesScheme(key, imp, E);
214
- return webcrypto.adoptKey(key, imp, ["sign"], E, "bad-input");
224
+ return _adoptKey(key, imp, ["sign"], E, "bad-input");
215
225
  }
216
226
  var der, owned = false;
217
227
  if (_isBuffer(key)) der = guard.bytes.view(key, E, "bad-input", "the signer private key");
@@ -224,7 +234,7 @@ function _importKey(key, imp, E) {
224
234
  throw E("bad-input", "a signer key must be a WebCrypto CryptoKey, a node:crypto KeyObject, a PKCS#8 DER Buffer, " +
225
235
  "a PKCS#8 PEM string, or a signer { algorithm, publicKey, sign }");
226
236
  }
227
- var imported = subtle.importKey("pkcs8", der, imp, false, ["sign"]);
237
+ var imported = _subtleImportKey(subtle, "pkcs8", der, imp, false, ["sign"]);
228
238
  if (!owned) return imported;
229
239
  return imported.then(function (k) {
230
240
  guard.secret.zeroize(der, E, "bad-input", "the signer private-key copy");
@@ -315,11 +325,14 @@ function _signExternally(signer, scheme, signedBytes, E) {
315
325
  * the rewritten artifact. `Buffer.from` is not enough: a small copy comes out of Node's shared
316
326
  * pool, whose backing ArrayBuffer the callback could reach the original through. */
317
327
  var handed = new intrinsic.Uint8Array(signedBytes.length);
318
- handed.set(signedBytes);
328
+ /** @internal The copy goes through the captured `set`. Read off the instance it dispatches through
329
+ * `Uint8Array.prototype`, and a replacement there writes different bytes into the allocation the
330
+ * callback is handed, so the signer signs a message this never assembled. */
331
+ intrinsic.typedArraySet(handed, signedBytes);
319
332
  var produced;
320
333
  try { produced = intrinsic.apply(signer.sign, signer.receiver, [handed]); }
321
334
  catch (e) { throw E("bad-input", "the signer sign() callback failed", e); }
322
- return _resolve(Promise, produced).then(function (raw) {
335
+ return _resolve(_Promise, produced).then(function (raw) {
323
336
  var sig = guard.bytes.snapshotSource(raw, E, "bad-input", "the signature the signer returned");
324
337
  return _assertSignerSignature(sig, scheme, E);
325
338
  }, function (e) {
@@ -369,7 +382,7 @@ function _nodeSignOptions(key, scheme) {
369
382
  var name = scheme.sign.name;
370
383
  if (name === "ECDSA") return { key: key, dsaEncoding: "ieee-p1363" };
371
384
  if (name === "RSA-PSS") {
372
- return { key: key, padding: nodeCrypto.constants.RSA_PKCS1_PSS_PADDING, saltLength: scheme.sign.saltLength };
385
+ return { key: key, padding: guard.crypto.padding.RSA_PKCS1_PSS, saltLength: scheme.sign.saltLength };
373
386
  }
374
387
  return key;
375
388
  }
@@ -393,7 +406,7 @@ function _signWithKeyObject(key, scheme, signedBytes, E) {
393
406
  }
394
407
  _assertKeyObjectMatchesScheme(key, scheme, E);
395
408
  try {
396
- return _bufferFrom(nodeCrypto.sign(_nodeDigest(scheme), signedBytes, _nodeSignOptions(key, scheme)));
409
+ return _bufferFrom(guard.crypto.sign(_nodeDigest(scheme), signedBytes, _nodeSignOptions(key, scheme)));
397
410
  } catch (e) {
398
411
  throw E("bad-input", "the signer KeyObject could not produce a " + scheme.imp.name + " signature", e);
399
412
  }
@@ -402,7 +415,7 @@ function _signWithKeyObject(key, scheme, signedBytes, E) {
402
415
  function signOverTbs(scheme, key, signedBytes, E) {
403
416
  if (intrinsic.types.isKeyObject(key)) {
404
417
  var koSig = _signWithKeyObject(key, scheme, signedBytes, E);
405
- return _resolve(Promise, scheme.ecdsaDer ? validator.sig.rawToEcdsaDer(koSig, scheme.coordLen) : koSig);
418
+ return _resolve(_Promise, scheme.ecdsaDer ? validator.sig.rawToEcdsaDer(koSig, scheme.coordLen) : koSig);
406
419
  }
407
420
  var signer = _snapshotSigner(key);
408
421
  if (signer) {
@@ -414,7 +427,7 @@ function signOverTbs(scheme, key, signedBytes, E) {
414
427
  return compositeSig.compositeSign(scheme.composite, _normCompositeKeys(key, scheme.composite, E), signedBytes).then(function (sig) { return _bufferFrom(sig); });
415
428
  }
416
429
  return _importKey(key, scheme.imp, E).then(function (priv) {
417
- return subtle.sign(scheme.sign, priv, signedBytes).then(function (sigRaw) {
430
+ return _subtleSign(subtle, scheme.sign, priv, signedBytes).then(function (sigRaw) {
418
431
  var sig = _bufferFrom(sigRaw);
419
432
  if (scheme.ecdsaDer) sig = validator.sig.rawToEcdsaDer(sig, scheme.coordLen);
420
433
  return sig;