@browsercore/tls 0.4.18 → 0.4.19
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/README.md +218 -6
- package/dist/certificates/ca-constraints.d.ts +41 -0
- package/dist/certificates/ca-constraints.d.ts.map +1 -0
- package/dist/certificates/ca-constraints.js +107 -0
- package/dist/certificates/ca-constraints.js.map +1 -0
- package/dist/certificates/cert-extensions.d.ts +47 -10
- package/dist/certificates/cert-extensions.d.ts.map +1 -1
- package/dist/certificates/cert-extensions.js +111 -10
- package/dist/certificates/cert-extensions.js.map +1 -1
- package/dist/certificates/certificate-path.d.ts +282 -0
- package/dist/certificates/certificate-path.d.ts.map +1 -0
- package/dist/certificates/certificate-path.js +499 -0
- package/dist/certificates/certificate-path.js.map +1 -0
- package/dist/certificates/certificates.d.ts +66 -47
- package/dist/certificates/certificates.d.ts.map +1 -1
- package/dist/certificates/certificates.js +163 -82
- package/dist/certificates/certificates.js.map +1 -1
- package/dist/certificates/der.d.ts +0 -8
- package/dist/certificates/der.d.ts.map +1 -1
- package/dist/certificates/der.js +0 -42
- package/dist/certificates/der.js.map +1 -1
- package/dist/certificates/extension-policy.d.ts +195 -0
- package/dist/certificates/extension-policy.d.ts.map +1 -0
- package/dist/certificates/extension-policy.js +346 -0
- package/dist/certificates/extension-policy.js.map +1 -0
- package/dist/certificates/signature-algorithm.d.ts +54 -0
- package/dist/certificates/signature-algorithm.d.ts.map +1 -0
- package/dist/certificates/signature-algorithm.js +359 -0
- package/dist/certificates/signature-algorithm.js.map +1 -0
- package/dist/connection/handshake-driver.d.ts +9 -6
- package/dist/connection/handshake-driver.d.ts.map +1 -1
- package/dist/connection/handshake-driver.js.map +1 -1
- package/dist/connection/handshake-messages.d.ts +9 -2
- package/dist/connection/handshake-messages.d.ts.map +1 -1
- package/dist/connection/handshake-messages.js +57 -23
- package/dist/connection/handshake-messages.js.map +1 -1
- package/dist/errors.d.ts +54 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +22 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/tls.d.ts +4 -3
- package/dist/tls.d.ts.map +1 -1
- package/dist/tls.js +4 -3
- package/dist/tls.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -1
- package/dist/types.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure RFC 5280 §6.1 certification-path building and path checks (no I/O).
|
|
3
|
+
*
|
|
4
|
+
* `buildCertificatePath` runs a depth-first search with backtracking from the
|
|
5
|
+
* leaf. At every step the candidates for the current certificate's issuer are
|
|
6
|
+
* tried in order: first the certificate ITSELF as a pinned anchor (identity
|
|
7
|
+
* match, see below), then the trust anchors as issuers, then the remaining
|
|
8
|
+
* server-sent certificates in wire order. A candidate matches when its
|
|
9
|
+
* subject DN equals the current certificate's issuer DN byte-for-byte AND its
|
|
10
|
+
* public key verifies the current certificate's signature; each such
|
|
11
|
+
* verification performed during building IS the chain verification (RFC 5280
|
|
12
|
+
* §6.1.3 (a)), so no pair is ever verified twice.
|
|
13
|
+
*
|
|
14
|
+
* PINNED ANCHORS — matched by identity, never verified: a certificate that is
|
|
15
|
+
* itself an anchor (self-signed, subject DN bytes AND subjectPublicKeyInfo
|
|
16
|
+
* bytes equal to the anchor's) ends the path immediately WITHOUT any
|
|
17
|
+
* signature verification, because there is no issuer edge left to verify —
|
|
18
|
+
* trust rests on the anchor identity, not on the certificate's own signature
|
|
19
|
+
* (old pinned leaves/roots are commonly self-signed with SHA-1 or another
|
|
20
|
+
* rejected algorithm). A NON-self-signed certificate whose ISSUER is an
|
|
21
|
+
* anchor is a real edge and stays signature-verified. A self-signed
|
|
22
|
+
* certificate that merely SHARES the anchor's subject DN but not its SPKI is
|
|
23
|
+
* a name collision: it is not the anchor and has no (real) issuer, so it
|
|
24
|
+
* fails with no-issuer.
|
|
25
|
+
*
|
|
26
|
+
* BACKTRACKING: reaching an anchor completes a candidate path; the path
|
|
27
|
+
* checks below then decide whether it is valid. The FIRST FULLY VALID path
|
|
28
|
+
* (anchored and passing every check) is the result; a candidate whose
|
|
29
|
+
* completed path fails a check is abandoned and the NEXT candidate is tried,
|
|
30
|
+
* so the outcome never depends on the order the server happened to choose
|
|
31
|
+
* (e.g. a cross-signed intermediate sent in a constrained and an unconstrained
|
|
32
|
+
* variant). Termination is unconditional: a certificate is never used twice
|
|
33
|
+
* on one path, and at most {@link MAX_PATH_INTERMEDIATES} intermediates are
|
|
34
|
+
* accepted, so the search visits at most (candidates × path length) states.
|
|
35
|
+
*
|
|
36
|
+
* FAILURE REPORTING: only when every candidate is exhausted is the path
|
|
37
|
+
* reported unanchored, with the failure observed at the DEEPEST search depth —
|
|
38
|
+
* the attempt that got closest to acceptance, where a completed-but-invalid
|
|
39
|
+
* path (anchor edge already added) outranks a dead end at the same prefix —
|
|
40
|
+
* ties resolved by the FIRST attempt recorded at that depth. The message
|
|
41
|
+
* always names the offending certificate.
|
|
42
|
+
*
|
|
43
|
+
* The signature-algorithm policy is deliberately NOT re-checked here — it
|
|
44
|
+
* lives in `validateCertificateChain`'s sweep, which runs in every mode.
|
|
45
|
+
*
|
|
46
|
+
* DN comparison detail: candidate filtering compares the DER byte spans of the
|
|
47
|
+
* candidate's subject and the current certificate's issuer. The signature check
|
|
48
|
+
* is what actually binds issuer to subject — DN equality only selects the
|
|
49
|
+
* candidate set (BoringSSL matches names byte-exactly for the same reason). A
|
|
50
|
+
* false-negative filter (differing encodings of the same DN) yields no-issuer,
|
|
51
|
+
* never a false-accept: nothing is trusted without a verifying signature (or
|
|
52
|
+
* an identity match with a pinned anchor).
|
|
53
|
+
*/
|
|
54
|
+
import { TlsCertificateError } from "../errors.js";
|
|
55
|
+
import { constantTimeEqual } from "../utils.js";
|
|
56
|
+
import { isCriticalExtensionHandled, serverAuthEkuAcceptable, CERTIFICATE_POLICIES_OID, NAME_CONSTRAINTS_OID, } from "./extension-policy.js";
|
|
57
|
+
/**
|
|
58
|
+
* Maximum intermediates between the leaf and the anchor. Chrome caps path
|
|
59
|
+
* length the same way; without a limit a degenerate chain (or a hostile
|
|
60
|
+
* server) could make the search walk hundreds of useless entries.
|
|
61
|
+
*/
|
|
62
|
+
export const MAX_PATH_INTERMEDIATES = 8;
|
|
63
|
+
/**
|
|
64
|
+
* Verify `subject`'s signature with the candidate issuer's SPKI. Returns false
|
|
65
|
+
* for a `rejected` signature algorithm — there is no TLS scheme to verify
|
|
66
|
+
* with; that policy failure belongs to validateCertificateChain's sweep, and
|
|
67
|
+
* treating it as "cannot verify" keeps this builder total (never throwing).
|
|
68
|
+
*/
|
|
69
|
+
function signatureVerifies(subject, issuerSpki, provider) {
|
|
70
|
+
const algorithm = subject.signatureAlgorithm;
|
|
71
|
+
if (algorithm.kind !== "supported") {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return provider.verifySignature(algorithm.scheme, issuerSpki, subject.signatureValue, subject.tbsBytes);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The pinned-anchor identity match: `cert` is itself a trust anchor when it is
|
|
78
|
+
* self-signed AND some anchor has byte-equal subject DN AND byte-equal SPKI.
|
|
79
|
+
* Only identity pins — a same-DN different-key anchor is a collision, not a
|
|
80
|
+
* match.
|
|
81
|
+
*/
|
|
82
|
+
function findPinnedAnchor(cert, trustAnchors) {
|
|
83
|
+
if (!cert.selfSigned) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
return trustAnchors.find((anchor) => constantTimeEqual(anchor.subjectDn, cert.subjectDn) &&
|
|
87
|
+
constantTimeEqual(anchor.subjectPublicKeyInfo, cert.subjectPublicKeyInfo));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Run the path checks on a completed candidate path (RFC 5280 §6.1.2–§6.1.4,
|
|
91
|
+
* in verifyChain's documented order) WITHOUT throwing; the first failure is
|
|
92
|
+
* returned so the search can record it and backtrack. `anchor` is the trust
|
|
93
|
+
* anchor that completed this path — exempt from all the checks (trusted a
|
|
94
|
+
* priori), including when a pinned anchor sits at the top of the path. The
|
|
95
|
+
* extension-policy checks run AFTER the EKU check to mirror Chrome's
|
|
96
|
+
* per-certificate processing order (EKU in BasicCertificateProcessing, the
|
|
97
|
+
* critical-extension sweep in PrepareForNextCertificate / WrapUp — both after
|
|
98
|
+
* VerifyExtendedKeyUsage).
|
|
99
|
+
*/
|
|
100
|
+
function validateBuiltPath(path, anchor, now) {
|
|
101
|
+
return (pathValidityWindowError(path, now) ??
|
|
102
|
+
pathCaConstraintsError(path) ??
|
|
103
|
+
pathLengthConstraintsError(path, anchor) ??
|
|
104
|
+
pathServerAuthEkuError(path, anchor) ??
|
|
105
|
+
pathCriticalExtensionsError(path, anchor));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Build the certification path from the leaf to a trust anchor.
|
|
109
|
+
*
|
|
110
|
+
* Depth-first search with backtracking: candidates are tried anchors-first,
|
|
111
|
+
* then server certificates in wire order; the first FULLY VALID path (reaching
|
|
112
|
+
* an anchor and passing every check) wins; exhausted candidates yield an
|
|
113
|
+
* unanchored result carrying the deepest failure (see the module comment).
|
|
114
|
+
* Unanchored results never throw — the caller decides how to surface them.
|
|
115
|
+
*/
|
|
116
|
+
export function buildCertificatePath(chain, trustAnchors, provider, now) {
|
|
117
|
+
/**
|
|
118
|
+
* Extend `path` (whose LAST element is `current` — the invariant the
|
|
119
|
+
* failure records rely on) toward an anchor using `candidates`, never
|
|
120
|
+
* reusing a certificate on one path. Returns the first fully valid
|
|
121
|
+
* completion, or the deepest failure observed in this subtree.
|
|
122
|
+
*/
|
|
123
|
+
const extend = (current, path, candidates) => {
|
|
124
|
+
// Each extend() invocation tracks its OWN deepest failure; the parent
|
|
125
|
+
// merges the child's result, so a strictly deeper failure wins and —
|
|
126
|
+
// on equal depth — the attempt that came first in search order.
|
|
127
|
+
let deepestHere;
|
|
128
|
+
const note = (failure) => {
|
|
129
|
+
deepestHere = deepestHere === undefined || failure.depth > deepestHere.depth ? failure : deepestHere;
|
|
130
|
+
return deepestHere;
|
|
131
|
+
};
|
|
132
|
+
// 0. Pinned identity: current IS an anchor — no edge to verify, its
|
|
133
|
+
// own signature algorithm is irrelevant. The completed path must
|
|
134
|
+
// still pass the checks; on failure this route is abandoned like
|
|
135
|
+
// any other (backtracking) and the search continues.
|
|
136
|
+
const pinned = findPinnedAnchor(current, trustAnchors);
|
|
137
|
+
if (pinned !== undefined) {
|
|
138
|
+
const invalid = validateBuiltPath(path, pinned, now);
|
|
139
|
+
if (invalid === undefined) {
|
|
140
|
+
return { outcome: "anchored", anchored: { kind: "anchored", path, anchor: pinned } };
|
|
141
|
+
}
|
|
142
|
+
note({
|
|
143
|
+
reason: invalid.reason,
|
|
144
|
+
message: invalid.message,
|
|
145
|
+
depth: path.length + 1,
|
|
146
|
+
certificate: current,
|
|
147
|
+
certificateIndex: path.length - 1,
|
|
148
|
+
candidateCount: 0,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
// 1. The trust anchors as ISSUER of current: a real edge, signature
|
|
152
|
+
// verified. Every DN-matching anchor is tried until one yields a
|
|
153
|
+
// fully valid path.
|
|
154
|
+
let anchorDnMatches = 0;
|
|
155
|
+
let anchorSigFailed = 0;
|
|
156
|
+
for (const anchor of trustAnchors) {
|
|
157
|
+
if (!constantTimeEqual(anchor.subjectDn, current.issuerDn)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
anchorDnMatches++;
|
|
161
|
+
if (!signatureVerifies(current, anchor.subjectPublicKeyInfo, provider)) {
|
|
162
|
+
anchorSigFailed++;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const invalid = validateBuiltPath(path, anchor, now);
|
|
166
|
+
if (invalid === undefined) {
|
|
167
|
+
return { outcome: "anchored", anchored: { kind: "anchored", path, anchor } };
|
|
168
|
+
}
|
|
169
|
+
note({
|
|
170
|
+
reason: invalid.reason,
|
|
171
|
+
message: invalid.message,
|
|
172
|
+
depth: path.length + 1,
|
|
173
|
+
certificate: current,
|
|
174
|
+
certificateIndex: path.length - 1,
|
|
175
|
+
candidateCount: 0,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// 2. Server-sent certificates as issuer, in wire order, each used at
|
|
179
|
+
// most once on a path (a cycle cannot re-consume its own entries).
|
|
180
|
+
let intermediateDnMatches = 0;
|
|
181
|
+
let intermediateSigFailed = 0;
|
|
182
|
+
let candidateIndex = 0;
|
|
183
|
+
for (const candidate of candidates) {
|
|
184
|
+
if (constantTimeEqual(candidate.subjectDn, current.issuerDn)) {
|
|
185
|
+
if (!signatureVerifies(current, candidate.subjectPublicKeyInfo, provider)) {
|
|
186
|
+
intermediateDnMatches++;
|
|
187
|
+
intermediateSigFailed++;
|
|
188
|
+
}
|
|
189
|
+
else if (path.length > MAX_PATH_INTERMEDIATES) {
|
|
190
|
+
// The verified issuer would become intermediate number
|
|
191
|
+
// path.length — exceeding the cap abandons this route
|
|
192
|
+
// (recorded, then the next candidate is tried).
|
|
193
|
+
note({
|
|
194
|
+
reason: "path-too-long",
|
|
195
|
+
message: `certificate path exceeds the maximum of ${MAX_PATH_INTERMEDIATES} intermediates: ` +
|
|
196
|
+
`certificate at index ${path.length} (issuer DN "${candidate.issuer}") would add one more`,
|
|
197
|
+
depth: path.length + 1,
|
|
198
|
+
certificate: candidate,
|
|
199
|
+
certificateIndex: path.length,
|
|
200
|
+
candidateCount: 0,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
const remaining = [...candidates.slice(0, candidateIndex), ...candidates.slice(candidateIndex + 1)];
|
|
205
|
+
const child = extend(candidate, [...path, candidate], remaining);
|
|
206
|
+
if (child.outcome === "anchored") {
|
|
207
|
+
return child;
|
|
208
|
+
}
|
|
209
|
+
// The route through this candidate dead-ended — its
|
|
210
|
+
// deepest failure is merged below; try the next candidate.
|
|
211
|
+
note(child.deepest);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
candidateIndex++;
|
|
215
|
+
}
|
|
216
|
+
// 3. Dead end at this level: record the step failure so it can be
|
|
217
|
+
// reported if no deeper attempt exists. For a SELF-SIGNED current,
|
|
218
|
+
// a DN match on a different key is a name collision, not a failed
|
|
219
|
+
// verification of a real edge — the only legitimate issuer of a
|
|
220
|
+
// self-signed certificate is itself, and it is not the anchor —
|
|
221
|
+
// so that shape reports no-issuer, not bad-signature.
|
|
222
|
+
const dnMatched = anchorDnMatches + intermediateDnMatches > 0;
|
|
223
|
+
const reason = dnMatched && !current.selfSigned ? "bad-signature" : "no-issuer";
|
|
224
|
+
const candidateCount = anchorSigFailed + intermediateSigFailed;
|
|
225
|
+
const message = reason === "bad-signature"
|
|
226
|
+
? `signature verification failed: certificate at index ${path.length - 1} (issuer DN "${current.issuer}") ` +
|
|
227
|
+
`matched ${candidateCount} candidate issuer(s) by name but none of their public keys verified the signature`
|
|
228
|
+
: `no issuer found for certificate at index ${path.length - 1} (issuer DN "${current.issuer}"): ` +
|
|
229
|
+
`no trust anchor and no remaining server-sent certificate has this subject and verified the signature`;
|
|
230
|
+
// The returned value is this subtree's deepest failure — the note
|
|
231
|
+
// above guarantees it is at least the step failure just recorded.
|
|
232
|
+
const deepest = note({
|
|
233
|
+
reason,
|
|
234
|
+
message,
|
|
235
|
+
depth: path.length,
|
|
236
|
+
certificate: current,
|
|
237
|
+
certificateIndex: path.length - 1,
|
|
238
|
+
candidateCount,
|
|
239
|
+
});
|
|
240
|
+
return { outcome: "exhausted", deepest };
|
|
241
|
+
};
|
|
242
|
+
const result = extend(chain.leaf, [chain.leaf], chain.intermediates);
|
|
243
|
+
if (result.outcome === "anchored") {
|
|
244
|
+
return result.anchored;
|
|
245
|
+
}
|
|
246
|
+
// Every candidate exhausted: report the deepest failure of the search
|
|
247
|
+
// (see the module comment).
|
|
248
|
+
const failure = result.deepest;
|
|
249
|
+
return {
|
|
250
|
+
kind: "unanchored",
|
|
251
|
+
reason: failure.reason,
|
|
252
|
+
message: failure.message,
|
|
253
|
+
certificate: failure.certificate,
|
|
254
|
+
certificateIndex: failure.certificateIndex,
|
|
255
|
+
candidateCount: failure.candidateCount,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Validity window for EVERY certificate on the built path (RFC 5280 §6.1.2
|
|
260
|
+
* (a)/(b) + §6.1.3) — including a pinned leaf: the path's certificates come
|
|
261
|
+
* from the wire, and the wire target's validity is checked unconditionally
|
|
262
|
+
* (BoringSSL's verify_certificate_chain does the same for a directly trusted
|
|
263
|
+
* leaf). The trust anchor itself is never part of the built path when it acts
|
|
264
|
+
* as the ISSUER of the top certificate, so its own expiry is never checked
|
|
265
|
+
* (old roots outlive their notAfter all the time); there is deliberately no
|
|
266
|
+
* object-identity shortcut here — whether the pinned match reuses the same
|
|
267
|
+
* parsed object or a second parse of the same DER, the outcome is identical.
|
|
268
|
+
*/
|
|
269
|
+
export function pathValidityWindowError(path, now) {
|
|
270
|
+
for (const [index, cert] of path.entries()) {
|
|
271
|
+
const window = `(valid ${new Date(cert.notBefore * 1000).toISOString()}..${new Date(cert.notAfter * 1000).toISOString()}, now ${new Date(now * 1000).toISOString()})`;
|
|
272
|
+
if (now < cert.notBefore) {
|
|
273
|
+
return {
|
|
274
|
+
reason: "not-yet-valid",
|
|
275
|
+
message: `certificate at index ${index} (issuer DN "${cert.issuer}") is not yet valid ${window}`,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
if (now > cert.notAfter) {
|
|
279
|
+
return {
|
|
280
|
+
reason: "expired",
|
|
281
|
+
message: `certificate at index ${index} (issuer DN "${cert.issuer}") expired ${window}`,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
/** Throwing wrapper around {@link pathValidityWindowError} (see it for the WHY). */
|
|
288
|
+
export function checkPathValidityWindow(path, now) {
|
|
289
|
+
const failure = pathValidityWindowError(path, now);
|
|
290
|
+
if (failure !== undefined) {
|
|
291
|
+
throw new TlsCertificateError(failure.reason, failure.message);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Issuer-authorization constraints for every intermediate on the built path
|
|
296
|
+
* (RFC 5280 §6.1.3 (k)/(l) + §4.2.1.3): an intermediate must carry
|
|
297
|
+
* basicConstraints cA=TRUE, and when it carries a keyUsage extension, that
|
|
298
|
+
* extension must assert keyCertSign. Absence of the keyUsage EXTENSION is
|
|
299
|
+
* tolerated (RFC 5280 makes it optional); absence of the cA flag is not. The
|
|
300
|
+
* anchor is exempt because it is trusted a priori AND never part of the built
|
|
301
|
+
* path when it acts as the issuer of the top certificate (a legacy root
|
|
302
|
+
* without basicConstraints or keyCertSign must still verify paths under it);
|
|
303
|
+
* a WIRE certificate that merely matches an anchor by DN+SPKI is checked like
|
|
304
|
+
* any other intermediate.
|
|
305
|
+
*/
|
|
306
|
+
export function pathCaConstraintsError(path) {
|
|
307
|
+
for (const [index, cert] of path.entries()) {
|
|
308
|
+
if (index === 0) {
|
|
309
|
+
// The leaf is the end-entity — it need not be a CA.
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (!cert.isCa) {
|
|
313
|
+
return {
|
|
314
|
+
reason: "not-a-ca",
|
|
315
|
+
message: `intermediate certificate at index ${index} (issuer DN "${cert.issuer}") is missing basicConstraints cA`,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
if (cert.keyUsagePresent && !cert.keyUsageKeyCertSign) {
|
|
319
|
+
return {
|
|
320
|
+
reason: "missing-key-cert-sign",
|
|
321
|
+
message: `intermediate certificate at index ${index} (issuer DN "${cert.issuer}") carries a keyUsage extension without the keyCertSign bit`,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
/** Throwing wrapper around {@link pathCaConstraintsError} (see it for the WHY). */
|
|
328
|
+
export function checkPathCaConstraints(path) {
|
|
329
|
+
const failure = pathCaConstraintsError(path);
|
|
330
|
+
if (failure !== undefined) {
|
|
331
|
+
throw new TlsCertificateError(failure.reason, failure.message);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* pathLenConstraint along the built path (RFC 5280 §4.2.1.9): a certificate's
|
|
336
|
+
* constraint bounds the number of NON-self-issued intermediates that may
|
|
337
|
+
* FOLLOW it ("below" it, towards the leaf). Every issuer on the path is
|
|
338
|
+
* checked — each intermediate, and the anchor as issuer of the top
|
|
339
|
+
* intermediate (RFC 5280 §6.1.1 (h) seeds max_path_length from the anchor's
|
|
340
|
+
* constraint; the anchor's own status is a priori trusted, but its constraint
|
|
341
|
+
* is a delegation LIMIT on the certificates below it). RFC 5280 counts
|
|
342
|
+
* non-self-issued certificates only; our `selfSigned` flag (subject DN bytes
|
|
343
|
+
* equal issuer DN bytes) is exactly the subject==issuer test. The counting
|
|
344
|
+
* loop skips `anchor` by POSITION (the certificate in anchor position is the
|
|
345
|
+
* issuer, never one of the intermediates it constrains) — a WIRE certificate
|
|
346
|
+
* that merely matches the anchor by DN+SPKI is counted like any other
|
|
347
|
+
* intermediate, so the outcome does not depend on whether the pinned match
|
|
348
|
+
* reused the same parsed object.
|
|
349
|
+
*/
|
|
350
|
+
export function pathLengthConstraintsError(path, anchor) {
|
|
351
|
+
// Non-self-issued intermediates BELOW the issuer currently being checked.
|
|
352
|
+
let nonSelfIssuedBelow = 0;
|
|
353
|
+
let index = 0;
|
|
354
|
+
for (const issuer of path) {
|
|
355
|
+
if (index >= 1 && issuer !== anchor) {
|
|
356
|
+
if (issuer.pathLenConstraint !== null && nonSelfIssuedBelow > issuer.pathLenConstraint) {
|
|
357
|
+
return {
|
|
358
|
+
reason: "path-too-long",
|
|
359
|
+
message: `pathLenConstraint violated at certificate index ${index} (issuer DN "${issuer.issuer}"): ` +
|
|
360
|
+
`${nonSelfIssuedBelow} non-self-issued intermediate(s) below it exceed the limit of ${issuer.pathLenConstraint}`,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
// This certificate sits BELOW the next issuer up the path — count it.
|
|
364
|
+
if (!issuer.selfSigned) {
|
|
365
|
+
nonSelfIssuedBelow++;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
index++;
|
|
369
|
+
}
|
|
370
|
+
if (anchor.pathLenConstraint !== null && nonSelfIssuedBelow > anchor.pathLenConstraint) {
|
|
371
|
+
return {
|
|
372
|
+
reason: "path-too-long",
|
|
373
|
+
message: `pathLenConstraint violated at the trust anchor (issuer DN "${anchor.issuer}"): ` +
|
|
374
|
+
`${nonSelfIssuedBelow} non-self-issued intermediate(s) below it exceed the limit of ${anchor.pathLenConstraint}`,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
return undefined;
|
|
378
|
+
}
|
|
379
|
+
/** Throwing wrapper around {@link pathLengthConstraintsError} (see it for the WHY). */
|
|
380
|
+
export function checkPathLengthConstraints(path, anchor) {
|
|
381
|
+
const failure = pathLengthConstraintsError(path, anchor);
|
|
382
|
+
if (failure !== undefined) {
|
|
383
|
+
throw new TlsCertificateError(failure.reason, failure.message);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Whether `cert` IS the trust anchor on a completed path — the pinned identity
|
|
388
|
+
* match (self-signed, subject DN bytes AND SPKI bytes equal to the anchor's),
|
|
389
|
+
* exactly {@link findPinnedAnchor}'s rule. Only the anchor can sit ON a built
|
|
390
|
+
* path this way: when an anchor acts as the ISSUER of the top certificate it
|
|
391
|
+
* is never part of `path`, and any intermediate that matched an anchor by
|
|
392
|
+
* identity would have ended the path at that point. BoringSSL exempts the
|
|
393
|
+
* root the same way: ProcessRootCertificate runs and then `continue`s —
|
|
394
|
+
* "Don't do any other checks for root certificates" — so the trust anchor is
|
|
395
|
+
* never EKU- or critical-extension-checked (unless anchor-constraint
|
|
396
|
+
* enforcement is switched on, which Chrome does not do for root-store
|
|
397
|
+
* anchors).
|
|
398
|
+
*/
|
|
399
|
+
function isAnchorOnPath(cert, anchor) {
|
|
400
|
+
return (cert.selfSigned &&
|
|
401
|
+
constantTimeEqual(anchor.subjectDn, cert.subjectDn) &&
|
|
402
|
+
constantTimeEqual(anchor.subjectPublicKeyInfo, cert.subjectPublicKeyInfo));
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* extendedKeyUsage as Chrome 140 enforces it (RFC 5280 §4.2.1.12, BoringSSL
|
|
406
|
+
* `VerifyExtendedKeyUsage` with KeyPurpose::SERVER_AUTH — the value
|
|
407
|
+
* cert_verify_proc_builtin.cc passes for every TLS-server verification): every
|
|
408
|
+
* certificate on the built path that carries an EKU extension must include
|
|
409
|
+
* id-kp-serverAuth (1.3.6.1.5.5.7.3.1) or anyExtendedKeyUsage (2.5.29.37.0);
|
|
410
|
+
* the deciding SERVER_AUTH error is `kEkuLacksServerAuth`, raised by "if
|
|
411
|
+
* (cert.has_extended_key_usage() && !has_server_auth_eku && !has_any_eku)" for
|
|
412
|
+
* the target and for EVERY issuer alike ("Legacy compatible" branch). A
|
|
413
|
+
* MISSING extension is accepted (leaf: only the kEkuNotPresent warning;
|
|
414
|
+
* intermediates: "At this time … some intermediates are lacking EKU in the
|
|
415
|
+
* world at large from common CA's, so we allow the noEKU case to permit
|
|
416
|
+
* everything."), and an EMPTY EKU sequence is present-but-permitting-nothing,
|
|
417
|
+
* so it fails. Code-signing/time-stamping/OCSP-signing EKUs alongside
|
|
418
|
+
* serverAuth are only warnings in this mode (`add_error_if_strict` → warning).
|
|
419
|
+
* The anchor is exempt (see {@link isAnchorOnPath}).
|
|
420
|
+
*/
|
|
421
|
+
export function pathServerAuthEkuError(path, anchor) {
|
|
422
|
+
for (const [index, cert] of path.entries()) {
|
|
423
|
+
if (isAnchorOnPath(cert, anchor)) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (!cert.extendedKeyUsagePresent) {
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
if (serverAuthEkuAcceptable(cert.extendedKeyUsage)) {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
reason: "missing-server-auth-eku",
|
|
434
|
+
message: `certificate at index ${index} (issuer DN "${cert.issuer}") carries an extendedKeyUsage ` +
|
|
435
|
+
`extension without id-kp-serverAuth (1.3.6.1.5.5.7.3.1) or anyExtendedKeyUsage (2.5.29.37.0): ` +
|
|
436
|
+
`not usable for TLS server authentication`,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
/** Throwing wrapper around {@link pathServerAuthEkuError} (see it for the WHY). */
|
|
442
|
+
export function checkPathServerAuthEku(path, anchor) {
|
|
443
|
+
const failure = pathServerAuthEkuError(path, anchor);
|
|
444
|
+
if (failure !== undefined) {
|
|
445
|
+
throw new TlsCertificateError(failure.reason, failure.message);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Unrecognized critical extensions on the built path (RFC 5280 §4.2: "A
|
|
450
|
+
* certificate-using system MUST reject the certificate if it encounters a
|
|
451
|
+
* critical extension it does not recognize or a critical extension that
|
|
452
|
+
* contains information that it cannot process."), applied exactly like
|
|
453
|
+
* Chrome's built-in verifier: BoringSSL's `VerifyNoUnconsumedCriticalExtensions`
|
|
454
|
+
* runs for the target certificate (WrapUp) and for every intermediate
|
|
455
|
+
* (PrepareForNextCertificate), but NOT for the trust anchor ("Don't do any
|
|
456
|
+
* other checks for root certificates" in the verification loop; the anchor is
|
|
457
|
+
* only checked when trust-anchor CONSTRAINT enforcement is enabled, which
|
|
458
|
+
* Chrome does not do by default) — hence the {@link isAnchorOnPath} exemption.
|
|
459
|
+
* The recognized set is decided by extension-policy.ts
|
|
460
|
+
* ({@link isCriticalExtensionHandled}); a critical nameConstraints is
|
|
461
|
+
* deliberately rejected there until the name-constraints subset lands (this
|
|
462
|
+
* verifier does not process name constraints, and RFC 5280 §4.2 requires
|
|
463
|
+
* rejecting unprocessed critical extensions).
|
|
464
|
+
*/
|
|
465
|
+
export function pathCriticalExtensionsError(path, anchor) {
|
|
466
|
+
for (const [index, cert] of path.entries()) {
|
|
467
|
+
if (isAnchorOnPath(cert, anchor)) {
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
for (const ext of cert.extensions) {
|
|
471
|
+
if (!ext.critical) {
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
if (isCriticalExtensionHandled(ext.oid, ext.value, cert.extendedKeyUsagePresent)) {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
const note = ext.oid === NAME_CONSTRAINTS_OID
|
|
478
|
+
? " (name constraints are not processed yet — RFC 5280 §4.2 requires rejecting an unprocessed critical extension)"
|
|
479
|
+
: ext.oid === CERTIFICATE_POLICIES_OID
|
|
480
|
+
? " (critical certificatePolicies must parse with only CPS or userNotice qualifiers — BoringSSL IsHandledCriticalExtension)"
|
|
481
|
+
: "";
|
|
482
|
+
return {
|
|
483
|
+
reason: "unsupported-critical-extension",
|
|
484
|
+
message: `certificate at index ${index} (issuer DN "${cert.issuer}") carries an unrecognized ` +
|
|
485
|
+
`critical extension (OID ${ext.oid}): RFC 5280 §4.2 requires rejecting critical ` +
|
|
486
|
+
`extensions that cannot be processed${note}`,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return undefined;
|
|
491
|
+
}
|
|
492
|
+
/** Throwing wrapper around {@link pathCriticalExtensionsError} (see it for the WHY). */
|
|
493
|
+
export function checkPathCriticalExtensions(path, anchor) {
|
|
494
|
+
const failure = pathCriticalExtensionsError(path, anchor);
|
|
495
|
+
if (failure !== undefined) {
|
|
496
|
+
throw new TlsCertificateError(failure.reason, failure.message);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
//# sourceMappingURL=certificate-path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"certificate-path.js","sourceRoot":"","sources":["../../src/certificates/certificate-path.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAGH,OAAO,EAAE,mBAAmB,EAAiC,MAAM,cAAc,CAAC;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACH,0BAA0B,EAC1B,uBAAuB,EACvB,wBAAwB,EACxB,oBAAoB,GACvB,MAAM,uBAAuB,CAAC;AAyF/B;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AA4ExC;;;;;GAKG;AACH,SAAS,iBAAiB,CACtB,OAA0B,EAC1B,UAAsB,EACtB,QAAwB;IAExB,MAAM,SAAS,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAC7C,IAAI,SAAS,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACjC,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,OAAO,QAAQ,CAAC,eAAe,CAAC,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC5G,CAAC;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,IAAuB,EAAE,YAAoC;IACnF,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;QACnB,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,OAAO,YAAY,CAAC,IAAI,CACpB,CAAC,MAAM,EAAE,EAAE,CACP,iBAAiB,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC;QACnD,iBAAiB,CAAC,MAAM,CAAC,oBAAoB,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAChF,CAAC;AACN,CAAC;AAED;;;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C;AACL,CAAC"}
|
|
@@ -10,72 +10,91 @@
|
|
|
10
10
|
* read and tested in isolation:
|
|
11
11
|
* - `der.ts` — DER tag-length-value primitives (no X.509 semantics)
|
|
12
12
|
* - `cert-extensions.ts` — subject/issuer names, SAN, KeyUsage, BasicConstraints
|
|
13
|
+
* - `ca-constraints.ts` — the path-validation delta: keyCertSign bit,
|
|
14
|
+
* BasicConstraints pathLenConstraint
|
|
15
|
+
* - `certificate-path.ts` — the pure RFC 5280 §6.1 path builder and its
|
|
16
|
+
* checks, plus the tls-internal ParsedCertificate /
|
|
17
|
+
* CertificateChain / TrustAnchor types (re-exported here
|
|
18
|
+
* for the stable import path)
|
|
13
19
|
* - `pem.ts` — PEM container decoding (the only place that knows about
|
|
14
20
|
* the BEGIN/END text wrapper; downstream code sees raw DER)
|
|
15
21
|
*/
|
|
16
|
-
import type { CryptoProvider } from "@browsercore/contracts";
|
|
17
|
-
import type
|
|
18
|
-
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/** DER-encoded SubjectPublicKeyInfo. */
|
|
23
|
-
readonly subjectPublicKeyInfo: Uint8Array;
|
|
24
|
-
/** Subject alternative DNS names (from the SAN extension), if any. */
|
|
25
|
-
readonly subjectAltNames: readonly string[];
|
|
26
|
-
/** Common Name from the subject DN, if present (legacy hostname match). */
|
|
27
|
-
readonly commonName?: string;
|
|
28
|
-
/** Not-before timestamp (epoch seconds). */
|
|
29
|
-
readonly notBefore: number;
|
|
30
|
-
/** Not-after timestamp (epoch seconds). */
|
|
31
|
-
readonly notAfter: number;
|
|
32
|
-
/** True if the certificate has the keyUsage bit for digital signature. */
|
|
33
|
-
readonly keyUsageDigitalSignature: boolean;
|
|
34
|
-
/** True if the certificate has the keyUsage bit for key encipherment/agreement. */
|
|
35
|
-
readonly keyUsageKeyEncipherment: boolean;
|
|
36
|
-
/** Signature algorithm used by the issuer over the TBSCertificate. */
|
|
37
|
-
readonly signatureScheme: SignatureScheme;
|
|
38
|
-
/** Raw signature value from the issuer. */
|
|
39
|
-
readonly signatureValue: Uint8Array;
|
|
40
|
-
/** DER-encoded SubjectPublicKeyInfo of the issuer (for chain building). */
|
|
41
|
-
readonly issuer: string;
|
|
42
|
-
/** True for a CA certificate. */
|
|
43
|
-
readonly isCa: boolean;
|
|
44
|
-
}
|
|
45
|
-
/** An ordered certificate chain: leaf first, intermediates follow, root last. */
|
|
46
|
-
export interface CertificateChain {
|
|
47
|
-
readonly leaf: Certificate;
|
|
48
|
-
readonly intermediates: readonly Certificate[];
|
|
49
|
-
readonly root: Certificate;
|
|
50
|
-
}
|
|
51
|
-
/** A trust anchor: a root certificate we trust a priori. */
|
|
52
|
-
export interface TrustAnchor {
|
|
53
|
-
readonly subjectPublicKeyInfo: Uint8Array;
|
|
54
|
-
readonly subject: string;
|
|
55
|
-
}
|
|
22
|
+
import type { Certificate, CryptoProvider } from "@browsercore/contracts";
|
|
23
|
+
import { type CertificateSignatureAlgorithm } from "./signature-algorithm.js";
|
|
24
|
+
import { type CertificateChain, type ParsedCertificate, type TrustAnchor } from "./certificate-path.js";
|
|
25
|
+
export type { Certificate };
|
|
26
|
+
export type { CertificateChain, CertificateExtension, CertificatePath, CertificatePathFailureReason, ParsedCertificate, TrustAnchor } from "./certificate-path.js";
|
|
27
|
+
export { buildCertificatePath, MAX_PATH_INTERMEDIATES } from "./certificate-path.js";
|
|
56
28
|
/**
|
|
57
|
-
* Parse a single DER-encoded X.509 certificate.
|
|
29
|
+
* Parse a single DER-encoded X.509 certificate (contract-type boundary).
|
|
30
|
+
*
|
|
31
|
+
* Thin wrapper over {@link parseCertificateDetails}: consumers see ONLY the
|
|
32
|
+
* canonical contract `Certificate`; the tls-internal DN byte spans and
|
|
33
|
+
* path-constraint fields ride along on the same object for path building
|
|
34
|
+
* (ParsedCertificate extends Certificate, so this is a plain upcast).
|
|
35
|
+
*
|
|
36
|
+
* Throws {@link TlsHandshakeError} with phase "certificate" on malformed input.
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseCertificate(buf: Uint8Array): Certificate;
|
|
39
|
+
/**
|
|
40
|
+
* Parse a single DER-encoded X.509 certificate into the FULL tls-internal
|
|
41
|
+
* form ({@link ParsedCertificate}).
|
|
58
42
|
*
|
|
59
43
|
* Decodes the outer Certificate SEQUENCE into its TBSCertificate, signature
|
|
60
44
|
* algorithm, and signature value, then extracts the validation-relevant fields.
|
|
61
45
|
* `tbsBytes` is captured as the EXACT DER span of the TBSCertificate so that
|
|
62
|
-
* signature verification over it is byte-correct.
|
|
46
|
+
* signature verification over it is byte-correct. The subject and issuer Name
|
|
47
|
+
* DER spans are captured as the candidate-filter key for path building (the
|
|
48
|
+
* signature check binds issuer to subject; DN equality only filters).
|
|
63
49
|
*
|
|
64
50
|
* Throws {@link TlsHandshakeError} with phase "certificate" on malformed input.
|
|
51
|
+
* A structurally valid certificate is NEVER rejected because of its signature
|
|
52
|
+
* algorithm — an unsupported/rejected algorithm is reported through
|
|
53
|
+
* {@link Certificate.signatureAlgorithm} and policed by the caller.
|
|
65
54
|
*/
|
|
66
|
-
export declare function
|
|
55
|
+
export declare function parseCertificateDetails(buf: Uint8Array): ParsedCertificate;
|
|
67
56
|
/**
|
|
68
57
|
* Validate that a certificate is valid for the given hostname per RFC 6125.
|
|
69
58
|
* (Moved to {@link ./hostname.js} — re-exported here for the stable import path.)
|
|
70
59
|
*/
|
|
71
60
|
export { validateHostname, matchDnsName } from "./hostname.js";
|
|
72
61
|
/**
|
|
73
|
-
* Verify a certificate chain
|
|
74
|
-
*
|
|
62
|
+
* Verify a certificate chain against trust anchors (RFC 5280 §6.1).
|
|
63
|
+
*
|
|
64
|
+
* The path is BUILT from the leaf: at each step the issuer is found among the
|
|
65
|
+
* trust anchors (preferred) and the remaining server-sent certificates — or a
|
|
66
|
+
* self-signed certificate matches an anchor by identity (subject DN + SPKI
|
|
67
|
+
* bytes), which ends the path without any signature verification. Candidates
|
|
68
|
+
* are searched depth-first with backtracking, and every completed candidate
|
|
69
|
+
* path is checked (validity window, basicConstraints/keyCertSign,
|
|
70
|
+
* pathLenConstraint); the first FULLY VALID path wins, so the outcome never
|
|
71
|
+
* depends on the order the server chose. Building IS the chain verification —
|
|
72
|
+
* no signature pair is verified twice. The server may omit the root (the
|
|
73
|
+
* normal case), send it, order intermediates arbitrarily, or add unrelated
|
|
74
|
+
* certificates; none of that is special-cased.
|
|
75
|
+
*
|
|
76
|
+
* Check order: hostname (always, first — the cheap, always-applicable check),
|
|
77
|
+
* then the no-anchors guard, then the signature-algorithm policy, then the
|
|
78
|
+
* builder (which runs the path checks per candidate path).
|
|
75
79
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
80
|
+
* Failures are typed: {@link TlsCertificateError} carries a discriminated
|
|
81
|
+
* `reason` ("no-anchors", "no-issuer", "bad-signature", "expired",
|
|
82
|
+
* "not-yet-valid", "not-a-ca", "missing-key-cert-sign", "path-too-long").
|
|
83
|
+
*
|
|
84
|
+
* Signature verification is delegated to @browsercore/crypto.
|
|
78
85
|
*/
|
|
79
86
|
export declare function verifyChain(chain: CertificateChain, trustAnchors: readonly TrustAnchor[], hostname: string, now: number, provider: CryptoProvider): Promise<void>;
|
|
80
87
|
export { pemToDer } from "./pem.js";
|
|
88
|
+
/** The `rejected` variant of {@link CertificateSignatureAlgorithm}. */
|
|
89
|
+
export type RejectedSignatureAlgorithm = Extract<CertificateSignatureAlgorithm, {
|
|
90
|
+
readonly kind: "rejected";
|
|
91
|
+
}>;
|
|
92
|
+
/**
|
|
93
|
+
* Diagnostic message for a certificate carrying a rejected signature
|
|
94
|
+
* algorithm. Names the certificate (chain index + issuer DN), the policy
|
|
95
|
+
* reason and the OID so an operator can identify the offending certificate
|
|
96
|
+
* without re-running the handshake. Shared by verifyChain and the
|
|
97
|
+
* validateCertificateChain policy sweep so the wording has one source.
|
|
98
|
+
*/
|
|
99
|
+
export declare function rejectedAlgorithmMessage(index: number, cert: Certificate, rejected: RejectedSignatureAlgorithm): string;
|
|
81
100
|
//# sourceMappingURL=certificates.d.ts.map
|