@blamejs/pki 0.5.38 → 0.6.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/lib/cmp-build.js CHANGED
@@ -60,6 +60,8 @@ var _bigInt = intrinsic.BigInt;
60
60
  // go through a Date a caller could have replaced after this module was read.
61
61
  var _Date = intrinsic.Date;
62
62
  var csr = require("./schema-csr");
63
+ require("./path-validate"); // side-effect: path-validate injects its signature engine into csr-verify at load
64
+ var csrVerify = require("./csr-verify"); // verify an embedded p10cr request's proof-of-possession before sending it
63
65
  var crl = require("./schema-crl");
64
66
  var x509 = require("./schema-x509");
65
67
  var signScheme = require("./sign-scheme");
@@ -681,10 +683,17 @@ function _encodeBody(bodySpec, key, opts) {
681
683
  var inner;
682
684
  if (arm === "p10cr") {
683
685
  var csrDer = _b.reqDerSequence(bodySpec.p10cr, "p10cr (a CertificationRequest DER)");
684
- try { csr.parse(csrDer); } // validate the embedded PKCS#10 via the real parser (fail-closed on a non-CSR)
686
+ var parsedCsr;
687
+ try { parsedCsr = csr.parse(csrDer); } // validate structure via the real parser AND capture the parse for the PoP check
685
688
  catch (e) { if (e instanceof CmpError) throw e; throw _err("cmp/bad-input", "p10cr is not a valid PKCS#10 CertificationRequest", e); }
686
- inner = b.raw(csrDer);
687
- } else if (arm === "certConf") {
689
+ // Verify the embedded request's proof-of-possession before it is protected and sent: a p10cr carries
690
+ // a PKCS#10 CertificationRequest (RFC 9810 sec. 5.3.3, whose structure is RFC 2986), and its
691
+ // self-signature under its own subject public key is that request's proof-of-possession. A CSR whose
692
+ // self-signature does not verify is one the CA will reject, so refuse it rather than send it -- through
693
+ // the shared inbound-PoP seam pki.csr.verify uses.
694
+ return _p10crBody(csrDer, parsedCsr, tag, state);
695
+ }
696
+ if (arm === "certConf") {
688
697
  inner = _encodeCertConfirmContent(bodySpec.certConf, state);
689
698
  } else if (arm === "pollReq") {
690
699
  inner = _encodePollReqContent(bodySpec.pollReq);
@@ -713,6 +722,16 @@ function _encodeBody(bodySpec, key, opts) {
713
722
  return Promise.resolve({ bodyTLV: b.explicit(tag, inner), usesCmp2021: state.usesCmp2021 });
714
723
  }
715
724
 
725
+ // `await`, not a `.then` chain: the PoP verdict is read through the native promise job, not the
726
+ // replaceable Promise.prototype.then a co-resident could swap to force `ok` true and slip an
727
+ // unverified request past. The caller has already validated csrDer's structure synchronously.
728
+ async function _p10crBody(csrDer, parsedCsr, tag, state) {
729
+ if (!(await csrVerify.verifyCsrSignature(parsedCsr))) {
730
+ throw _err("cmp/bad-popo", "the p10cr CertificationRequest failed its proof-of-possession: its PKCS#10 signature does not verify under its own subject public key, so a CA would reject it");
731
+ }
732
+ return { bodyTLV: b.explicit(tag, b.raw(csrDer)), usesCmp2021: state.usesCmp2021 };
733
+ }
734
+
716
735
  // ---- protection ----
717
736
 
718
737
 
@@ -803,7 +822,7 @@ function build(message, opts) {
803
822
  ], _build);
804
823
  }
805
824
 
806
- function _build(message, opts) {
825
+ async function _build(message, opts) {
807
826
  opts = opts || {};
808
827
  if (!message || typeof message !== "object" || Buffer.isBuffer(message)) throw _err("cmp/bad-input", "the PKIMessage spec must be an object { header, body }");
809
828
  guard.identifier.assertKnownKeys(message, KNOWN_MESSAGE_KEYS, _err, "cmp/bad-input", function (k) {
@@ -824,30 +843,31 @@ function _build(message, opts) {
824
843
  var prot = _resolveProtection(opts);
825
844
  var senderKey = opts.key;
826
845
 
827
- return Promise.resolve(_encodeBody(message.body, senderKey, opts)).then(function (bodyResult) {
828
- var bodyTLV = bodyResult.bodyTLV;
829
- var pvno = message.header.pvno != null ? message.header.pvno : 2;
830
- if (typeof pvno !== "number" || !Number.isInteger(pvno)) throw _err("cmp/bad-input", "header.pvno must be an integer");
831
- if (bodyResult.usesCmp2021 && pvno < 3) pvno = 3; // a certConf hashAlg forces cmp2021(3)
832
-
833
- var headerTLV = _encodeHeader(message.header, prot.protectionAlgDer, pvno); // built ONCE
834
- var protectedPartDer = b.sequence([headerTLV, bodyTLV]); // virtual ProtectedPart ::= SEQUENCE { header, body }
835
-
836
- return prot.computeBits(protectedPartDer).then(function (protectionBits) {
837
- var msgChildren = [headerTLV, bodyTLV, b.explicit(0, protectionBits)]; // reuse the SAME header/body TLVs
838
- var extraCertsDers = _collectExtraCerts(opts, prot.certDer);
839
- if (extraCertsDers.length) msgChildren.push(b.explicit(1, b.sequence(extraCertsDers)));
840
- var der = b.sequence(msgChildren);
841
-
842
- cmp.parse(der); // round-trip re-validation: the emitted message MUST parse (protection<=>protectionAlg, pvno, freeText, etc.)
843
- if (opts.pem != null) {
844
- if (opts.pem === true) return cmp.pemEncode(der, "CMP");
845
- if (typeof opts.pem !== "string" || !opts.pem) throw _err("cmp/bad-input", "opts.pem must be true or a non-empty PEM label string");
846
- return cmp.pemEncode(der, opts.pem);
847
- }
848
- return der;
849
- });
850
- });
846
+ // `await`, not a `.then` chain: an embedded p10cr/crm proof-of-possession is enforced by _encodeBody
847
+ // REJECTING, so the verdict must reach here through the native promise job, not a Promise.prototype.then
848
+ // a co-resident could swap to fire this callback with a forged body and slip an unverified request past.
849
+ var bodyResult = await _encodeBody(message.body, senderKey, opts);
850
+ var bodyTLV = bodyResult.bodyTLV;
851
+ var pvno = message.header.pvno != null ? message.header.pvno : 2;
852
+ if (typeof pvno !== "number" || !Number.isInteger(pvno)) throw _err("cmp/bad-input", "header.pvno must be an integer");
853
+ if (bodyResult.usesCmp2021 && pvno < 3) pvno = 3; // a certConf hashAlg forces cmp2021(3)
854
+
855
+ var headerTLV = _encodeHeader(message.header, prot.protectionAlgDer, pvno); // built ONCE
856
+ var protectedPartDer = b.sequence([headerTLV, bodyTLV]); // virtual ProtectedPart ::= SEQUENCE { header, body }
857
+
858
+ var protectionBits = await prot.computeBits(protectedPartDer);
859
+ var msgChildren = [headerTLV, bodyTLV, b.explicit(0, protectionBits)]; // reuse the SAME header/body TLVs
860
+ var extraCertsDers = _collectExtraCerts(opts, prot.certDer);
861
+ if (extraCertsDers.length) msgChildren.push(b.explicit(1, b.sequence(extraCertsDers)));
862
+ var der = b.sequence(msgChildren);
863
+
864
+ cmp.parse(der); // round-trip re-validation: the emitted message MUST parse (protection<=>protectionAlg, pvno, freeText, etc.)
865
+ if (opts.pem != null) {
866
+ if (opts.pem === true) return cmp.pemEncode(der, "CMP");
867
+ if (typeof opts.pem !== "string" || !opts.pem) throw _err("cmp/bad-input", "opts.pem must be true or a non-empty PEM label string");
868
+ return cmp.pemEncode(der, opts.pem);
869
+ }
870
+ return der;
851
871
  }
852
872
 
853
873
  function _collectExtraCerts(opts, protCertDer) {
@@ -889,8 +909,10 @@ function _collectExtraCerts(opts, protCertDer) {
889
909
  * one of `{ key, cert }` (a signature under the sender key over the message, using any registry algorithm: RSA
890
910
  * / ECDSA / EdDSA / ML-DSA / SLH-DSA / composite, resolved from the certificate) or `{ mac }` (a PBMAC1
891
911
  * shared-secret MAC). The protection is computed over the exact DER of the virtual
892
- * `ProtectedPart ::= SEQUENCE { header, body }` and self-verified before the message is returned. The
893
- * emitted PKIMessage round-trips byte-identically through `pki.schema.cmp.parse`.
912
+ * `ProtectedPart ::= SEQUENCE { header, body }` and self-verified before the message is returned. An
913
+ * embedded `p10cr` request is checked against its own proof-of-possession first: a PKCS#10 whose
914
+ * self-signature does not verify under its subject public key is refused (`cmp/bad-popo`) rather than
915
+ * protected and sent. The emitted PKIMessage round-trips byte-identically through `pki.schema.cmp.parse`.
894
916
  *
895
917
  * @opts
896
918
  * - `key` (Buffer|CryptoKey) + `cert` (Buffer): signature protection under the sender key; `cert` is
@@ -1116,7 +1138,7 @@ function wellKnownUrl(base, opts) {
1116
1138
  * @primitive pki.cmp.transfer
1117
1139
  * @signature pki.cmp.transfer(url, message, opts?) -> Promise<{ response, responseBytes, status, contentType, tls }>
1118
1140
  * @since 0.3.19
1119
- * @status experimental
1141
+ * @status stable
1120
1142
  * @spec RFC 9811, RFC 9810
1121
1143
  * @related pki.cmp.build, pki.cmp.wellKnownUrl, pki.schema.cmp.parse, pki.transport.https
1122
1144
  *
@@ -1159,7 +1181,7 @@ function wellKnownUrl(base, opts) {
1159
1181
  * @primitive pki.cmp.wellKnownUrl
1160
1182
  * @signature pki.cmp.wellKnownUrl(base, opts?) -> string
1161
1183
  * @since 0.3.19
1162
- * @status experimental
1184
+ * @status stable
1163
1185
  * @spec RFC 9811, RFC 8615
1164
1186
  * @related pki.cmp.transfer
1165
1187
  *
@@ -390,7 +390,7 @@ function _certConfHash(certDer) {
390
390
  * @primitive pki.cmp.session
391
391
  * @signature pki.cmp.session(opts) -> session
392
392
  * @since 0.3.27
393
- * @status experimental
393
+ * @status stable
394
394
  * @spec RFC 9810, RFC 9811, RFC 9483
395
395
  * @related pki.cmp.build, pki.cmp.verify, pki.cmp.transfer
396
396
  *
package/lib/ct.js CHANGED
@@ -154,7 +154,7 @@ function _parseSct(r, sctLen) {
154
154
  * @primitive pki.ct.parseSctList
155
155
  * @signature pki.ct.parseSctList(extValue) -> { scts, unknownScts, all }
156
156
  * @since 0.1.20
157
- * @status experimental
157
+ * @status stable
158
158
  * @spec RFC 6962, RFC 5246, RFC 8446
159
159
  * @related pki.ct.reconstructSignedData, pki.ct.encodeSctList, pki.schema.x509.parse
160
160
  *
@@ -252,7 +252,7 @@ function _u24Bytes(n) {
252
252
  * @primitive pki.ct.reconstructSignedData
253
253
  * @signature pki.ct.reconstructSignedData(entry, sct) -> Buffer
254
254
  * @since 0.1.20
255
- * @status experimental
255
+ * @status stable
256
256
  * @spec RFC 6962
257
257
  * @related pki.ct.parseSctList
258
258
  *
@@ -378,7 +378,7 @@ function _spkiAlg(spki) {
378
378
  * @primitive pki.ct.verifySct
379
379
  * @signature pki.ct.verifySct(entry, sct, logPublicKey) -> Promise<boolean>
380
380
  * @since 0.2.12
381
- * @status experimental
381
+ * @status stable
382
382
  * @spec RFC 6962
383
383
  * @defends sct-signature-forgery (CWE-347)
384
384
  * @related pki.ct.parseSctList, pki.ct.reconstructSignedData
@@ -498,7 +498,7 @@ function _encodeSctBody(sct) {
498
498
  * @primitive pki.ct.encodeSctList
499
499
  * @signature pki.ct.encodeSctList(scts) -> Buffer
500
500
  * @since 0.2.24
501
- * @status experimental
501
+ * @status stable
502
502
  * @spec RFC 6962, RFC 5246
503
503
  * @related pki.ct.parseSctList, pki.ct.signSct
504
504
  *
@@ -573,7 +573,7 @@ function _logKeyMaterial(logKey) {
573
573
  * @primitive pki.ct.signSct
574
574
  * @signature pki.ct.signSct(entry, logKey, opts?) -> Promise<sct>
575
575
  * @since 0.2.24
576
- * @status experimental
576
+ * @status stable
577
577
  * @spec RFC 6962
578
578
  * @related pki.ct.verifySct, pki.ct.reconstructSignedData, pki.ct.encodeSctList
579
579
  *
@@ -723,7 +723,7 @@ function _logsAgree(a, b) {
723
723
  * @primitive pki.ct.parseLogList
724
724
  * @signature pki.ct.parseLogList(json, opts?) -> { logs, byLogId, version, timestamp }
725
725
  * @since 0.2.28
726
- * @status experimental
726
+ * @status stable
727
727
  * @spec RFC 6962
728
728
  * @related pki.ct.verifySctWithLogList, pki.ct.verifySct
729
729
  *
@@ -815,7 +815,7 @@ function _resolveNotAfter(entry, opts) {
815
815
  * @primitive pki.ct.verifySctWithLogList
816
816
  * @signature pki.ct.verifySctWithLogList(entry, sct, logList, opts?) -> Promise<boolean>
817
817
  * @since 0.2.28
818
- * @status experimental
818
+ * @status stable
819
819
  * @spec RFC 6962
820
820
  * @related pki.ct.parseLogList, pki.ct.verifySct
821
821
  *
@@ -878,7 +878,7 @@ async function verifySctWithLogList(entry, sct, logList, opts) {
878
878
  * @primitive pki.ct.verifyLogListSignature
879
879
  * @signature pki.ct.verifyLogListSignature(json, signature, publicKey) -> Promise<boolean>
880
880
  * @since 0.2.29
881
- * @status experimental
881
+ * @status stable
882
882
  * @spec RFC 6962, RFC 8017
883
883
  * @related pki.ct.parseLogList, pki.ct.verifySct
884
884
  *
@@ -1019,7 +1019,7 @@ function _fetchBody(transport, url, req, label) {
1019
1019
  * @primitive pki.ct.fetchLogList
1020
1020
  * @signature pki.ct.fetchLogList(opts) -> Promise<{ logs, byLogId, version, timestamp, raw, status, contentType, tls }>
1021
1021
  * @since 0.3.21
1022
- * @status experimental
1022
+ * @status stable
1023
1023
  * @spec RFC 6962
1024
1024
  * @related pki.ct.parseLogList, pki.ct.verifyLogListSignature, pki.ct.verifySctWithLogList
1025
1025
  *
package/lib/est.js CHANGED
@@ -162,7 +162,7 @@ function _knownOpts(opts, known, verb) {
162
162
  * @primitive pki.est.transferDecode
163
163
  * @signature pki.est.transferDecode(body) -> Buffer
164
164
  * @since 0.1.24
165
- * @status experimental
165
+ * @status stable
166
166
  * @spec RFC 8951, RFC 4648
167
167
  * @related pki.est.transferEncode
168
168
  *
@@ -200,7 +200,7 @@ function transferDecode(body) {
200
200
  * @primitive pki.est.transferEncode
201
201
  * @signature pki.est.transferEncode(der) -> string
202
202
  * @since 0.1.24
203
- * @status experimental
203
+ * @status stable
204
204
  * @spec RFC 8951, RFC 4648
205
205
  * @related pki.est.transferDecode
206
206
  *
@@ -389,7 +389,7 @@ function splitMultipartMixed(body, contentType) {
389
389
  * @primitive pki.est.parseCertsOnly
390
390
  * @signature pki.est.parseCertsOnly(der) -> { certificates, crls }
391
391
  * @since 0.1.24
392
- * @status experimental
392
+ * @status stable
393
393
  * @spec RFC 7030, RFC 5272, RFC 5652
394
394
  * @related pki.est.findIssuedCert, pki.schema.cms.parse
395
395
  *
@@ -695,7 +695,7 @@ var NOT_IMPLEMENTED_OPS = { fullcmc: 1 };
695
695
  * @primitive pki.est.classifyResponse
696
696
  * @signature pki.est.classifyResponse(status, headers, body, opts?) -> verdict
697
697
  * @since 0.1.24
698
- * @status experimental
698
+ * @status stable
699
699
  * @spec RFC 7030, RFC 8951
700
700
  * @related pki.est.paths, pki.est.parseCertsOnly
701
701
  *
@@ -796,7 +796,7 @@ function classifyResponse(status, headers, body, opts) {
796
796
  * @primitive pki.est.paths
797
797
  * @signature pki.est.paths(baseUrl, opts?) -> { cacerts, simpleenroll, ... }
798
798
  * @since 0.1.24
799
- * @status experimental
799
+ * @status stable
800
800
  * @spec RFC 7030
801
801
  * @related pki.est.classifyResponse
802
802
  *
@@ -1310,7 +1310,7 @@ function _certsResult(op, res, opts, csrSpki) {
1310
1310
  * @primitive pki.est.fullcmc
1311
1311
  * @signature pki.est.fullcmc(baseUrl, request, opts?) -> Promise<verdict | { retry, retryAfterSeconds }>
1312
1312
  * @since 0.4.16
1313
- * @status experimental
1313
+ * @status stable
1314
1314
  * @spec RFC 7030, RFC 8951, RFC 5273, RFC 5272
1315
1315
  * @related pki.cmc.build, pki.cmc.verify, pki.est.simpleenroll
1316
1316
  *
@@ -2024,7 +2024,7 @@ function _enroll(op, baseUrl, csrInput, opts) {
2024
2024
  * @primitive pki.est.cacerts
2025
2025
  * @signature pki.est.cacerts(baseUrl, opts?) -> Promise<{ certificates, crls } | { retry, retryAfterSeconds }>
2026
2026
  * @since 0.3.16
2027
- * @status experimental
2027
+ * @status stable
2028
2028
  * @spec RFC 7030, RFC 8951
2029
2029
  * @related pki.est.simpleenroll, pki.transport.https
2030
2030
  *
@@ -2068,7 +2068,7 @@ function cacerts(baseUrl, opts) {
2068
2068
  * @primitive pki.est.simpleenroll
2069
2069
  * @signature pki.est.simpleenroll(baseUrl, csr, opts?) -> Promise<{ certificate, chain, certificates } | { retry, retryAfterSeconds }>
2070
2070
  * @since 0.3.16
2071
- * @status experimental
2071
+ * @status stable
2072
2072
  * @spec RFC 7030, RFC 5272
2073
2073
  * @related pki.csr.sign, pki.est.simplereenroll, pki.est.cacerts
2074
2074
  *
@@ -2106,7 +2106,7 @@ function simpleenroll(baseUrl, csrInput, opts) {
2106
2106
  * @primitive pki.est.simplereenroll
2107
2107
  * @signature pki.est.simplereenroll(baseUrl, csr, opts?) -> Promise<{ certificate, chain, certificates } | { retry, retryAfterSeconds }>
2108
2108
  * @since 0.3.16
2109
- * @status experimental
2109
+ * @status stable
2110
2110
  * @spec RFC 7030
2111
2111
  * @related pki.est.simpleenroll, pki.est.reenrollGuard
2112
2112
  *
@@ -2285,7 +2285,7 @@ async function _serverkeygenResult(res, opts, derived) {
2285
2285
  * @primitive pki.est.serverkeygen
2286
2286
  * @signature pki.est.serverkeygen(baseUrl, csr, opts?) -> Promise<{ certificates, privateKey } | { certificates, encryptedKey } | { retry, retryAfterSeconds, retryAfterDate }>
2287
2287
  * @since 0.3.28
2288
- * @status experimental
2288
+ * @status stable
2289
2289
  * @spec RFC 7030, RFC 8951
2290
2290
  * @related pki.est.simpleenroll, pki.est.parseServerKeygenResponse
2291
2291
  *
@@ -2364,7 +2364,7 @@ function _csrattrsResult(res, opts) {
2364
2364
  * @primitive pki.est.csrattrs
2365
2365
  * @signature pki.est.csrattrs(baseUrl, opts?) -> Promise<{ available: true, attrs, plan } | { available: false, attrs: null }>
2366
2366
  * @since 0.3.28
2367
- * @status experimental
2367
+ * @status stable
2368
2368
  * @spec RFC 7030, RFC 8951, RFC 9908
2369
2369
  * @related pki.est.simpleenroll, pki.est.buildEnrollAttributes
2370
2370
  *
package/lib/hpke.js CHANGED
@@ -455,7 +455,7 @@ var suites = {
455
455
  * @primitive pki.hpke.setupS
456
456
  * @signature pki.hpke.setupS(suiteIds, recipientPublicKey, opts?) -> { enc, context }
457
457
  * @since 0.2.2
458
- * @status experimental
458
+ * @status stable
459
459
  * @spec RFC 9180
460
460
  * @related pki.hpke.setupR, pki.hpke.seal
461
461
  *
@@ -495,7 +495,7 @@ function _setupS(ids, pkR, opts) {
495
495
  * @primitive pki.hpke.setupR
496
496
  * @signature pki.hpke.setupR(suiteIds, enc, recipientPrivateKey, opts?) -> context
497
497
  * @since 0.2.2
498
- * @status experimental
498
+ * @status stable
499
499
  * @spec RFC 9180
500
500
  * @related pki.hpke.setupS, pki.hpke.open
501
501
  *
@@ -538,7 +538,7 @@ function _setupR(ids, enc, skR, opts) {
538
538
  * @primitive pki.hpke.seal
539
539
  * @signature pki.hpke.seal(suiteIds, recipientPublicKey, opts, aad, pt) -> { enc, ct }
540
540
  * @since 0.2.2
541
- * @status experimental
541
+ * @status stable
542
542
  * @spec RFC 9180
543
543
  * @related pki.hpke.open, pki.hpke.setupS
544
544
  *
@@ -574,7 +574,7 @@ function seal(ids, pkR, opts, aad, pt) {
574
574
  * @primitive pki.hpke.open
575
575
  * @signature pki.hpke.open(suiteIds, enc, recipientPrivateKey, opts, aad, ct) -> pt
576
576
  * @since 0.2.2
577
- * @status experimental
577
+ * @status stable
578
578
  * @spec RFC 9180
579
579
  * @related pki.hpke.seal, pki.hpke.setupR
580
580
  *
@@ -245,7 +245,7 @@ function _classifyError(e, C) {
245
245
  * @primitive pki.transport.https
246
246
  * @signature pki.transport.https(defaults?) -> transport
247
247
  * @since 0.3.16
248
- * @status experimental
248
+ * @status stable
249
249
  * @spec RFC 7030, RFC 8996
250
250
  * @defends tls-downgrade (CWE-757), server-impersonation (CWE-297), response-flooding (CWE-770), ssrf (CWE-918)
251
251
  * @related pki.est.cacerts, pki.est.simpleenroll
package/lib/lint.js CHANGED
@@ -714,7 +714,7 @@ function _applyThreshold(report, severity) {
714
714
  * @primitive pki.lint.certificate
715
715
  * @signature pki.lint.certificate(input, opts?) -> LintReport
716
716
  * @since 0.2.10
717
- * @status experimental
717
+ * @status stable
718
718
  * @spec RFC 5280, CA/Browser Forum TLS BR
719
719
  * @related pki.schema.x509.parse, pki.inspect.certificate, pki.path.validate
720
720
  *
@@ -772,7 +772,7 @@ function certificate(input, opts) {
772
772
  * @primitive pki.lint.rules
773
773
  * @signature pki.lint.rules(profile?) -> [{id, severity, source, citation}]
774
774
  * @since 0.2.10
775
- * @status experimental
775
+ * @status stable
776
776
  * @spec RFC 5280, CA/Browser Forum TLS BR
777
777
  *
778
778
  * Enumerate the rule registry (all rules, or one profile's). Each entry exposes its
@@ -790,7 +790,7 @@ function rules(profile) {
790
790
  * @primitive pki.lint.profiles
791
791
  * @signature pki.lint.profiles() -> [string]
792
792
  * @since 0.2.10
793
- * @status experimental
793
+ * @status stable
794
794
  * @spec RFC 5280, CA/Browser Forum TLS BR
795
795
  *
796
796
  * List the known lint-profile names.
@@ -0,0 +1,28 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) blamejs contributors
3
+ "use strict";
4
+ //
5
+ // @internal -- the single signature seam for an inbound signed OCSPRequest (RFC 6960 sec. 4.1.1),
6
+ // composed by pki.ocsp.verifyRequest. It routes the request-signature check through the same
7
+ // certification-path signature engine (its EdDSA low-order-point and algorithm-confusion gates
8
+ // included) that pki.ocsp.verify uses for a response and pki.crl.verify / pki.cms.verify use for
9
+ // theirs, so no second, weaker verifier of an untrusted request can drift. The producing side runs
10
+ // its own check over what it just signed (pki.ocsp.buildRequest's signer path); this is the
11
+ // different question a responder asks about a request someone else produced. The slot, the wiring
12
+ // assertion and the octet-alignment refusal are the shared seam (lib/verify-seam.js). It is not
13
+ // wired into index.js and is reached only by require.
14
+
15
+ var frameworkError = require("./framework-error");
16
+ var seam = require("./verify-seam");
17
+
18
+ var _seam = seam.makeSeam("ocsp-request-verify", frameworkError.OcspError, "ocsp/bad-input");
19
+
20
+ // Verify a signed OCSPRequest's signature over its exact tbsRequest bytes under the SPKI of the
21
+ // requestor certificate the request carries (RFC 6960 sec. 4.1.1). Unlike a CSR there is no
22
+ // self-signature: the verifying key is the requestor's, identified to the responder by
23
+ // requestorName and proven by the certificate embedded in (or supplied alongside) the request.
24
+ function verifyRequestSignature(signatureAlgorithm, signature, signerSpkiBytes, tbsRequestBytes) {
25
+ return _seam.verify(signatureAlgorithm, signature, signerSpkiBytes, tbsRequestBytes);
26
+ }
27
+
28
+ module.exports = { setEngine: _seam.setEngine, verifyRequestSignature: verifyRequestSignature };
package/lib/ocsp.js CHANGED
@@ -32,6 +32,7 @@ var ocspSchema = require("./schema-ocsp");
32
32
  var pathValidate = require("./path-validate");
33
33
  var signScheme = require("./sign-scheme");
34
34
  var ocspVerify = require("./ocsp-verify");
35
+ var ocspRequestVerify = require("./ocsp-request-verify"); // path-validate (required above) injects its signature engine into this seam at load
35
36
  var webcrypto = require("./webcrypto");
36
37
  var subtle = webcrypto.webcrypto.subtle;
37
38
  var guard = require("./guard-all");
@@ -579,9 +580,164 @@ function _responseNonce(parsed) {
579
580
  return null;
580
581
  }
581
582
 
583
+ var _VERIFY_REQUEST_OPTS = { certs: 1 };
584
+ // Parsed from BYTES so the signature, the algorithm that verifies it and the tbsRequest it covers
585
+ // are bound to one byte string, the same discipline pki.ocsp.verify follows for a response. A
586
+ // BufferSource is SNAPSHOT first: parseRequest surfaces tbsRequest / signature / embedded certs as
587
+ // views into it, and the signature check awaits, so without a copy a caller could mutate the buffer
588
+ // mid-verification and be judged on bytes other than the parser read. A PEM string decodes to a
589
+ // fresh buffer already; parseRequest owns that decode and a non-bytes input is its typed refusal.
590
+ function _requestFromBytes(request) {
591
+ if (guard.bytes.isByteSource(request)) request = guard.bytes.snapshotSource(request, OcspError, "ocsp/bad-input", "the OCSP request");
592
+ return ocspSchema.parseRequest(request);
593
+ }
594
+ function _reqVerdict(parsed, extra) {
595
+ return {
596
+ signed: extra.signed, signatureValid: extra.signatureValid,
597
+ signerCert: extra.signerCert != null ? extra.signerCert : null,
598
+ signerCerts: extra.signerCerts != null ? extra.signerCerts : [],
599
+ certs: extra.certs != null ? extra.certs : [],
600
+ signerSubject: extra.signerSubject != null ? extra.signerSubject : null,
601
+ requestorName: parsed.requestorName, requestList: parsed.requestList,
602
+ requestExtensions: parsed.requestExtensions, version: parsed.version,
603
+ reason: extra.reason != null ? extra.reason : null,
604
+ };
605
+ }
606
+ // Collect EVERY candidate certificate whose key verified the request signature. RFC 6960 sec. 4.1.1
607
+ // certs is an unordered SEQUENCE OF Certificate, and several may share the signing key (an expired
608
+ // certificate carried beside its renewal), so all matches are surfaced rather than one
609
+ // position-dependent pick: the responder builds a trusted path to one of them to select a usable
610
+ // one, and no valid match is hidden behind an unusable one that happened to appear first. `strict` separates the
611
+ // responder's own opts.certs (a malformed entry is its config error, a typed throw) from the
612
+ // request's untrusted embedded certs (a malformed one is skipped, never fatal).
613
+ async function _findRequestSigners(candidates, strict, parsed, sig, sigBits) {
614
+ var parsedList = [];
615
+ for (var i = 0; i < candidates.length; i++) {
616
+ try { parsedList.push({ der: candidates[i], cert: x509.parse(candidates[i]) }); }
617
+ catch (e) { if (strict) throw _err("ocsp/bad-signer-cert", "an opts.certs entry does not parse as an X.509 certificate", e); }
618
+ }
619
+ // The certs surfaced for path building are the PARSEABLE ones: a malformed embedded entry (skipped
620
+ // above rather than fatal) is excluded so it is never handed to pki.path.build as a candidate.
621
+ var parseable = parsedList.map(function (p) { return p.der; });
622
+ // signerCerts is every candidate whose key VERIFIES the request signature, identified by the
623
+ // verification itself rather than by comparing encoded key bytes. A request carries one signature
624
+ // under one key, but that key can appear across several certificates in the unordered certs field
625
+ // (an expired certificate beside its renewal), and the one key can be encoded differently between
626
+ // them -- rsaEncryption with NULL versus absent parameters, an EC point compressed versus
627
+ // uncompressed -- so a byte comparison would drop a genuine match, while a coincidental byte match
628
+ // under a different algorithm would be a false one. Verifying each candidate answers both exactly,
629
+ // through the algorithm-confusion gate (RFC 9814 sec. 4) in the shared engine. The work is bounded:
630
+ // the parser already caps the certificate count at OCSP_MAX_CERTS, and the engine returns false
631
+ // (never throws) for a candidate whose key does not match the signature, so a non-signer is skipped
632
+ // rather than aborting the verdict.
633
+ var matches = [];
634
+ for (var j = 0; j < parsedList.length; j++) {
635
+ if (await ocspRequestVerify.verifyRequestSignature(sig.signatureAlgorithm, sigBits, parsedList[j].cert.subjectPublicKeyInfo.bytes, parsed.tbsRequestBytes)) {
636
+ matches.push({ der: parsedList[j].der, subject: parsedList[j].cert.subject });
637
+ }
638
+ }
639
+ return { matches: matches, parseable: parseable };
640
+ }
641
+ // The verdict from `_findRequestSigners`: signerCerts is every match (so the caller's trust decision
642
+ // does not depend on ordering), signerCert / signerSubject the first for the common single-signer
643
+ // case. `certs` is every PARSEABLE certificate the request carried (or opts.certs supplied), so the
644
+ // responder has the intermediates it needs to chain a signer to an anchor -- those do not sign the
645
+ // request, so they never appear in signerCerts. No match is signatureValid:false with the reason.
646
+ function _signerVerdict(parsed, found, failReason) {
647
+ if (!found.matches.length) return _reqVerdict(parsed, { signed: true, signatureValid: false, certs: found.parseable, reason: failReason });
648
+ return _reqVerdict(parsed, {
649
+ signed: true, signatureValid: true, signerCert: found.matches[0].der,
650
+ signerCerts: found.matches.map(function (m) { return m.der; }), certs: found.parseable,
651
+ signerSubject: { dn: found.matches[0].subject.dn, rdns: found.matches[0].subject.rdns }, reason: null,
652
+ });
653
+ }
654
+
655
+ /**
656
+ * @primitive pki.ocsp.verifyRequest
657
+ * @signature pki.ocsp.verifyRequest(request, opts?) -> Promise<{ signed, signatureValid, signerCert, signerCerts, certs, signerSubject, requestorName, requestList, requestExtensions, version, reason }>
658
+ * @since 0.6.0
659
+ * @status stable
660
+ * @spec RFC 6960
661
+ * @related pki.ocsp.buildRequest, pki.ocsp.verify, pki.schema.ocsp.parseRequest, pki.csr.verify
662
+ *
663
+ * The responder-side counterpart to `pki.ocsp.buildRequest`: verify a client's signed OCSP request
664
+ * (RFC 6960 sec. 4.1.1). Signing a request is OPTIONAL, so an unsigned request is not an error; it is
665
+ * reported with `signed: false` and authenticates no requestor. For a signed request the requestor's
666
+ * signature over the exact `tbsRequest` is checked under the public key of the requestor certificate
667
+ * the request carries, through the same certification-path signature engine `pki.ocsp.verify` uses
668
+ * for a response (with its EdDSA low-order-point and algorithm-confusion gates). `request` is DER
669
+ * `Buffer` / `Uint8Array` or a PEM `OCSP REQUEST` string, parsed from bytes so the signature, its
670
+ * algorithm and the covered `tbsRequest` are one byte string and cannot be split.
671
+ *
672
+ * `signatureValid` means only that a certificate's key made this signature over this request. It
673
+ * says nothing about whether that certificate is trusted: `signerCerts` is every certificate the
674
+ * request carries whose key verified the signature (RFC 6960 sec. 4.1.1 certs is unordered, and a
675
+ * key may appear under an expired certificate beside its renewal), so the responder builds a trusted
676
+ * path to one of them rather than depend on ordering. `signerCert` and the decoded `signerSubject`
677
+ * are the first of those, the common single-signer case. `certs` is every parseable certificate the
678
+ * request carried (or opts.certs supplied), including intermediates that do not sign and so are absent
679
+ * from `signerCerts`: the responder passes it as the `opts.candidates` pool to `pki.path.build`, which
680
+ * discovers and validates the ordered path from a `signerCerts` entry (leaf) up to a trust anchor.
681
+ * The responder then confirms that certificate is authorized to sign the request -- its `keyUsage`,
682
+ * where present, must assert `digitalSignature` (RFC 5280 sec. 4.2.1.3, since the certificate signed
683
+ * the request) -- and compares its subject to the `requestorName` it expects. The decoded
684
+ * `requestList`, `requestExtensions` and `version` are
685
+ * returned so the CertIDs being asked about need no re-parse.
686
+ *
687
+ * @opts
688
+ * - `certs` An array of certificates (DER `Buffer` / `Uint8Array` or PEM strings) supplying the
689
+ * requestor certificate when the request omits its own `certs` (RFC 6960 lets them
690
+ * travel out of band); every entry whose key verifies is surfaced in `signerCerts`,
691
+ * and is ignored when the request embeds its own certificates.
692
+ *
693
+ * @example
694
+ * var pair = await pki.key.generate("Ed25519");
695
+ * var key = await pki.key.export(pair.privateKey), spki = await pki.key.export(pair.publicKey);
696
+ * var caDer = await pki.x509.sign({ subject: "OCSP CA", subjectPublicKey: spki,
697
+ * notBefore: new Date("2026-01-01T00:00:00Z"), notAfter: new Date("2027-01-01T00:00:00Z") }, { key: key });
698
+ * var request = await pki.ocsp.buildRequest({ cert: caDer, issuer: caDer },
699
+ * { signer: { cert: caDer, key: key }, requestorName: pki.schema.x509.parse(caDer).subject.bytes });
700
+ * var v = await pki.ocsp.verifyRequest(request);
701
+ * // v.signed && v.signatureValid -> for a signerCerts entry, pki.path.build(entry, { candidates:
702
+ * // v.certs, trustAnchors, time }) discovers and validates the path (several signerCerts may share
703
+ * // the key, e.g. an expired one beside its renewal); confirm that entry's keyUsage asserts
704
+ * // digitalSignature, then bind its subject to the requestor you expect before honoring v.requestList.
705
+ */
706
+ async function verifyRequest(request, opts) {
707
+ opts = guard.identifier.optionsObject(opts, _err, "ocsp/bad-input", "pki.ocsp.verifyRequest options");
708
+ guard.identifier.assertKnownKeys(opts, _VERIFY_REQUEST_OPTS, _err, "ocsp/bad-input",
709
+ "pki.ocsp.verifyRequest has an unknown option (out-of-band requestor certificates are `certs`): ");
710
+ var parsed = _requestFromBytes(request);
711
+ var sig = parsed.optionalSignature;
712
+ if (sig == null) {
713
+ return _reqVerdict(parsed, { signed: false, signatureValid: false,
714
+ reason: "the OCSP request is unsigned (RFC 6960 sec. 4.1.1 makes optionalSignature OPTIONAL); no requestor is authenticated" });
715
+ }
716
+ // sig.signature is the raw signature the parser already refused unless octet-aligned; the seam
717
+ // re-checks alignment, so it is handed the BIT STRING shape the seam verifies.
718
+ var sigBits = { unusedBits: 0, bytes: sig.signature };
719
+ // Prefer the certificates the request carries; fall back to opts.certs only when it carries none.
720
+ if (sig.certs.length) {
721
+ return _signerVerdict(parsed, await _findRequestSigners(sig.certs, false, parsed, sig, sigBits),
722
+ "the OCSP request signature does not verify over tbsRequest under any certificate the request carries (RFC 6960 sec. 4.1.1)");
723
+ }
724
+ var suppliedCerts = [];
725
+ if (opts.certs != null) {
726
+ var list = pkiBuild.reqDenseArray(opts.certs, "opts.certs", _err, "ocsp/bad-input");
727
+ for (var i = 0; i < list.length; i++) suppliedCerts.push(_normCertDer(list[i], "an opts.certs entry"));
728
+ }
729
+ if (!suppliedCerts.length) {
730
+ return _reqVerdict(parsed, { signed: true, signatureValid: false,
731
+ reason: "the signed OCSP request carries no certificate and opts.certs was not supplied, so its signature cannot be verified" });
732
+ }
733
+ return _signerVerdict(parsed, await _findRequestSigners(suppliedCerts, true, parsed, sig, sigBits),
734
+ "the OCSP request signature does not verify over tbsRequest under any opts.certs entry");
735
+ }
736
+
582
737
  module.exports = {
583
738
  buildRequest: buildRequest,
584
739
  sign: sign,
585
740
  buildErrorResponse: buildErrorResponse,
586
741
  verify: verify,
742
+ verifyRequest: verifyRequest,
587
743
  };
@@ -44,6 +44,7 @@ var crlVerify = require("./crl-verify");
44
44
  var csrVerify = require("./csr-verify");
45
45
  var crmfVerify = require("./crmf-verify");
46
46
  var attrcertVerify = require("./attrcert-verify");
47
+ var ocspRequestVerify = require("./ocsp-request-verify");
47
48
  var cmpVerify = require("./cmp-verify");
48
49
  var cmsVerify = require("./cms-verify");
49
50
  var cmpSession = require("./cmp-session");
@@ -2369,6 +2370,10 @@ crmfVerify.setEngine(_verifyWithSpki);
2369
2370
  // same way.
2370
2371
  attrcertVerify.setEngine(_verifyWithSpki);
2371
2372
 
2373
+ // And for a signed OCSPRequest (RFC 6960 sec. 4.1.1), so pki.ocsp.verifyRequest checks a requestor's
2374
+ // signature through the same one engine that verifies a response, never a second, weaker request path.
2375
+ ocspRequestVerify.setEngine(_verifyWithSpki);
2376
+
2372
2377
  // ---- the OCSP revocation checker (RFC 6960) -------------------------------
2373
2378
 
2374
2379
  // The OCSP response-verification core (responder authorization + CertID binding + currency +
@@ -3346,7 +3351,7 @@ async function _fetchAiaIssuers(current, aia) {
3346
3351
  * @primitive pki.path.build
3347
3352
  * @signature pki.path.build(leaf, opts) -> Promise<{ valid, path, trustAnchor, result, candidatesConsidered, aiaFetches }>
3348
3353
  * @since 0.3.7
3349
- * @status experimental
3354
+ * @status stable
3350
3355
  * @spec RFC 4158, RFC 5280
3351
3356
  * @related pki.path.validate, pki.schema.x509.parse, pki.trust.parseCertdata
3352
3357
  *
@@ -2014,7 +2014,7 @@ function _reconstructDer(r, sigNode) {
2014
2014
  * @primitive pki.schema.c509.parse
2015
2015
  * @signature pki.schema.c509.parse(bytes) -> { certificateType, serialNumber, serialNumberHex, ... }
2016
2016
  * @since 0.2.30
2017
- * @status experimental
2017
+ * @status stable
2018
2018
  * @spec draft-ietf-cose-cbor-encoded-cert, RFC 8949, RFC 9090, RFC 5280
2019
2019
  *
2020
2020
  * Decode a C509 certificate (draft-ietf-cose-cbor-encoded-cert) from its deterministic-CBOR bytes.
@@ -2274,7 +2274,7 @@ function _encodeC509Array(r) {
2274
2274
  * @primitive pki.schema.c509.encode
2275
2275
  * @signature pki.schema.c509.encode(input[, opts]) -> Buffer
2276
2276
  * @since 0.3.4
2277
- * @status experimental
2277
+ * @status stable
2278
2278
  * @spec draft-ietf-cose-cbor-encoded-cert, RFC 8949, RFC 9090
2279
2279
  * @related pki.schema.c509.parse
2280
2280
  *