micro509 0.8.0 → 0.10.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.d.ts +4 -4
- package/dist/index.js +15 -15
- package/dist/internal/asn1/asn1.js +145 -186
- package/dist/internal/asn1/asn1.js.map +1 -0
- package/dist/internal/asn1/der.js +210 -227
- package/dist/internal/asn1/der.js.map +1 -0
- package/dist/internal/asn1/oids.js +297 -101
- package/dist/internal/asn1/oids.js.map +1 -0
- package/dist/internal/asn1/oids2.js +69 -0
- package/dist/internal/asn1/oids2.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +37 -35
- package/dist/internal/crypto/algorithm-names.js.map +1 -0
- package/dist/internal/crypto/ecdsa.js +25 -28
- package/dist/internal/crypto/ecdsa.js.map +1 -0
- package/dist/internal/crypto/hash.js +56 -117
- package/dist/internal/crypto/hash.js.map +1 -0
- package/dist/internal/crypto/pbes2.js +122 -133
- package/dist/internal/crypto/pbes2.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +118 -124
- package/dist/internal/crypto/rsa-pss.js.map +1 -0
- package/dist/internal/crypto/sig-verify.js +87 -99
- package/dist/internal/crypto/sig-verify.js.map +1 -0
- package/dist/internal/crypto/signing.d.ts +12 -8
- package/dist/internal/crypto/signing.js +53 -55
- package/dist/internal/crypto/signing.js.map +1 -0
- package/dist/internal/crypto/webcrypto.js +5 -3
- package/dist/internal/crypto/webcrypto.js.map +1 -0
- package/dist/internal/shared/base64.js +10 -9
- package/dist/internal/shared/base64.js.map +1 -0
- package/dist/internal/shared/dn.js +50 -57
- package/dist/internal/shared/dn.js.map +1 -0
- package/dist/internal/shared/ip.js +43 -53
- package/dist/internal/shared/ip.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +211 -239
- package/dist/internal/verify/name-constraints-engine.js.map +1 -0
- package/dist/internal/verify/policy-engine.js +228 -247
- package/dist/internal/verify/policy-engine.js.map +1 -0
- package/dist/internal/verify/verify-path.js +213 -228
- package/dist/internal/verify/verify-path.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +62 -59
- package/dist/internal/x509/extension-bits.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +133 -164
- package/dist/internal/x509/extension-registry.js.map +1 -0
- package/dist/internal/x509/name-fields.js +35 -41
- package/dist/internal/x509/name-fields.js.map +1 -0
- package/dist/keys/keys.d.ts +626 -447
- package/dist/keys/keys.js +652 -404
- package/dist/keys/keys.js.map +1 -0
- package/dist/keys.d.ts +2 -2
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +46 -46
- package/dist/pem/pem.js +62 -81
- package/dist/pem/pem.js.map +1 -0
- package/dist/pem.js +2 -2
- package/dist/pkcs/pfx.d.ts +96 -80
- package/dist/pkcs/pfx.js +191 -231
- package/dist/pkcs/pfx.js.map +1 -0
- package/dist/pkcs/pkcs12-mac.d.ts +20 -18
- package/dist/pkcs/pkcs12-mac.js +90 -111
- package/dist/pkcs/pkcs12-mac.js.map +1 -0
- package/dist/pkcs/pkcs7.d.ts +118 -69
- package/dist/pkcs/pkcs7.js +401 -443
- package/dist/pkcs/pkcs7.js.map +1 -0
- package/dist/pkcs.d.ts +2 -2
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +37 -32
- package/dist/result/result.js +46 -44
- package/dist/result/result.js.map +1 -0
- package/dist/result.js +2 -2
- package/dist/revocation/chain.d.ts +138 -119
- package/dist/revocation/chain.js +248 -281
- package/dist/revocation/chain.js.map +1 -0
- package/dist/revocation/crl.d.ts +125 -123
- package/dist/revocation/crl.js +653 -745
- package/dist/revocation/crl.js.map +1 -0
- package/dist/revocation/ocsp.d.ts +183 -177
- package/dist/revocation/ocsp.js +581 -638
- package/dist/revocation/ocsp.js.map +1 -0
- package/dist/revocation/revocation.d.ts +26 -26
- package/dist/revocation/revocation.js +104 -125
- package/dist/revocation/revocation.js.map +1 -0
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +42 -42
- package/dist/verify/identity.js +158 -167
- package/dist/verify/identity.js.map +1 -0
- package/dist/verify/name-constraints.d.ts +7 -7
- package/dist/verify/policy.d.ts +10 -10
- package/dist/verify/verify.d.ts +163 -163
- package/dist/verify/verify.js +396 -392
- package/dist/verify/verify.js.map +1 -0
- package/dist/verify.js +3 -3
- package/dist/x509/certificate.d.ts +117 -117
- package/dist/x509/certificate.js +51 -60
- package/dist/x509/certificate.js.map +1 -0
- package/dist/x509/csr.d.ts +25 -25
- package/dist/x509/csr.js +26 -30
- package/dist/x509/csr.js.map +1 -0
- package/dist/x509/extensions.d.ts +228 -184
- package/dist/x509/extensions.js +175 -200
- package/dist/x509/extensions.js.map +1 -0
- package/dist/x509/name.d.ts +94 -93
- package/dist/x509/name.js +30 -29
- package/dist/x509/name.js.map +1 -0
- package/dist/x509/parse.d.ts +185 -185
- package/dist/x509/parse.js +638 -676
- package/dist/x509/parse.js.map +1 -0
- package/dist/x509.js +6 -6
- package/package.json +79 -41
|
@@ -1,53 +1,52 @@
|
|
|
1
|
-
import { readRootElement } from "../asn1/der.js";
|
|
2
|
-
import { childrenOf, decodeObjectIdentifier, decodeString, hexToBytes, requireElement, toHex } from "../asn1/asn1.js";
|
|
3
|
-
import { OIDS } from "../asn1/
|
|
4
|
-
import { allOnesMaskForIpAddress, decodeIpAddress, parseIpAddressToBytes } from "../shared/ip.js";
|
|
5
|
-
import { nameFieldKeyFromOid } from "../x509/name-fields.js";
|
|
6
|
-
import { compareDistinguishedNames, isWithinDirectoryNameSubtree } from "../shared/dn.js";
|
|
1
|
+
import { readRootElement as e } from "../asn1/der.js";
|
|
2
|
+
import { childrenOf as t, decodeObjectIdentifier as n, decodeString as r, hexToBytes as i, requireElement as a, toHex as o } from "../asn1/asn1.js";
|
|
3
|
+
import { OIDS as s } from "../asn1/oids2.js";
|
|
4
|
+
import { allOnesMaskForIpAddress as c, decodeIpAddress as l, parseIpAddressToBytes as u } from "../shared/ip.js";
|
|
5
|
+
import { nameFieldKeyFromOid as d } from "../x509/name-fields.js";
|
|
6
|
+
import { compareDistinguishedNames as f, isWithinDirectoryNameSubtree as p } from "../shared/dn.js";
|
|
7
7
|
//#region src/internal/verify/name-constraints-engine.ts
|
|
8
8
|
/**
|
|
9
9
|
* Internal name-constraints evaluation engine.
|
|
10
10
|
*
|
|
11
|
-
* Accumulates and evaluates the shipped RFC 5280
|
|
11
|
+
* Accumulates and evaluates the shipped RFC 5280
|
|
12
|
+
* [§4.2.1.10](https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.10) / [§6.1](https://datatracker.ietf.org/doc/html/rfc5280#section-6.1)
|
|
12
13
|
* name-constraint subset during certificate path validation.
|
|
13
14
|
*
|
|
14
15
|
* @module
|
|
15
16
|
*/
|
|
16
17
|
/** Constructs a {@linkcode NameConstraintValidationFailure} with optional details. */
|
|
17
|
-
function nameConstraintFailure(
|
|
18
|
+
function nameConstraintFailure(e, t, n, r) {
|
|
18
19
|
return {
|
|
19
|
-
ok:
|
|
20
|
-
code,
|
|
21
|
-
message,
|
|
22
|
-
index,
|
|
23
|
-
...
|
|
20
|
+
ok: !1,
|
|
21
|
+
code: e,
|
|
22
|
+
message: t,
|
|
23
|
+
index: n,
|
|
24
|
+
...r === void 0 ? {} : { details: r }
|
|
24
25
|
};
|
|
25
26
|
}
|
|
26
27
|
/** Strips undefined fields and returns `undefined` when all fields are empty. */
|
|
27
|
-
function nameConstraintDetails(
|
|
28
|
-
|
|
29
|
-
...
|
|
30
|
-
...
|
|
28
|
+
function nameConstraintDetails(e) {
|
|
29
|
+
let t = {
|
|
30
|
+
...e.subjectCommonName === void 0 ? {} : { subjectCommonName: e.subjectCommonName },
|
|
31
|
+
...e.actual === void 0 ? {} : { actual: e.actual }
|
|
31
32
|
};
|
|
32
|
-
return Object.keys(
|
|
33
|
+
return Object.keys(t).length === 0 ? void 0 : t;
|
|
33
34
|
}
|
|
34
35
|
/** A certificate is self-issued when subject and issuer DNs are semantically equal (RFC 5280 §7.1). */
|
|
35
|
-
function isSelfIssued(
|
|
36
|
-
return
|
|
36
|
+
function isSelfIssued(e) {
|
|
37
|
+
return f(e.subject, e.issuer);
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Initializes validation state from caller-supplied initial constraints.
|
|
40
41
|
*
|
|
41
42
|
* Call once before {@linkcode evaluateNameConstraints}.
|
|
42
43
|
*/
|
|
43
|
-
function createNameConstraintValidationState(
|
|
44
|
+
function createNameConstraintValidationState(e) {
|
|
44
45
|
return {
|
|
45
|
-
initialPermittedSubtrees:
|
|
46
|
-
initialExcludedSubtrees:
|
|
46
|
+
initialPermittedSubtrees: e.permittedSubtrees?.map((e) => e.base) ?? [],
|
|
47
|
+
initialExcludedSubtrees: e.excludedSubtrees?.map((e) => e.base) ?? []
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
|
-
/** Empty SEQUENCE DER hex — represents an empty subject DN. */
|
|
50
|
-
const EMPTY_SEQUENCE_HEX = "3000";
|
|
51
50
|
/**
|
|
52
51
|
* Walks the chain root-to-leaf, accumulating nameConstraints from CA
|
|
53
52
|
* certificates and checking each non-self-issued certificate's names
|
|
@@ -55,30 +54,29 @@ const EMPTY_SEQUENCE_HEX = "3000";
|
|
|
55
54
|
*
|
|
56
55
|
* RFC 5280 §6.1.3(b)–(c) for intermediates, §6.1.5(g) for the leaf.
|
|
57
56
|
*/
|
|
58
|
-
function evaluateNameConstraints(
|
|
59
|
-
let
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!nameCheckResult.ok) return nameCheckResult;
|
|
57
|
+
function evaluateNameConstraints(e, t) {
|
|
58
|
+
let n = seedInitialNameConstraints(t), r = e[e.length - 1];
|
|
59
|
+
r?.nameConstraints !== void 0 && (n = accumulateConstraints(n, r.nameConstraints, hasCriticalNameConstraintsExtension(r)));
|
|
60
|
+
for (let t = e.length - 2; t >= 0; --t) {
|
|
61
|
+
let r = e[t];
|
|
62
|
+
if (r === void 0) throw Error(`Certificate chain contains undefined at index ${String(t)}`);
|
|
63
|
+
if (!isSelfIssued(r) || t === 0) {
|
|
64
|
+
let e = checkCertificateNames(r, n, t);
|
|
65
|
+
if (!e.ok) return e;
|
|
68
66
|
}
|
|
69
|
-
|
|
67
|
+
r.nameConstraints !== void 0 && (n = accumulateConstraints(n, r.nameConstraints, hasCriticalNameConstraintsExtension(r)));
|
|
70
68
|
}
|
|
71
|
-
return { ok:
|
|
69
|
+
return { ok: !0 };
|
|
72
70
|
}
|
|
73
71
|
/** True when the certificate carries a critical nameConstraints extension. */
|
|
74
|
-
function hasCriticalNameConstraintsExtension(
|
|
75
|
-
return
|
|
72
|
+
function hasCriticalNameConstraintsExtension(e) {
|
|
73
|
+
return e.extensions.some((e) => e.oid === s.nameConstraints && e.critical);
|
|
76
74
|
}
|
|
77
75
|
/** Converts initial state into the starting accumulated-constraints snapshot. */
|
|
78
|
-
function seedInitialNameConstraints(
|
|
76
|
+
function seedInitialNameConstraints(e) {
|
|
79
77
|
return {
|
|
80
|
-
permittedLevels:
|
|
81
|
-
excluded:
|
|
78
|
+
permittedLevels: e.initialPermittedSubtrees.length > 0 ? [e.initialPermittedSubtrees] : [],
|
|
79
|
+
excluded: e.initialExcludedSubtrees,
|
|
82
80
|
unsupportedCriticalForms: /* @__PURE__ */ new Set()
|
|
83
81
|
};
|
|
84
82
|
}
|
|
@@ -91,84 +89,92 @@ function seedInitialNameConstraints(state) {
|
|
|
91
89
|
* dropped — the RFC's rejection requirement is scoped to critical
|
|
92
90
|
* extensions (conforming CAs MUST mark nameConstraints critical anyway).
|
|
93
91
|
*/
|
|
94
|
-
function accumulateConstraints(
|
|
95
|
-
|
|
96
|
-
const excluded = constraints.excludedSubtrees !== void 0 && constraints.excludedSubtrees.length > 0 ? [...current.excluded, ...constraints.excludedSubtrees.flatMap((subtree) => isSupportedNameConstraintForm(subtree.base) ? [subtree.base] : [])] : current.excluded;
|
|
97
|
-
const newUnsupported = critical ? listUnsupportedNameConstraintTypes(constraints) : [];
|
|
92
|
+
function accumulateConstraints(e, t, n) {
|
|
93
|
+
let r = t.permittedSubtrees !== void 0 && t.permittedSubtrees.length > 0 ? [...e.permittedLevels, t.permittedSubtrees.flatMap((e) => isSupportedNameConstraintForm(e.base) ? [e.base] : [])] : e.permittedLevels, i = t.excludedSubtrees !== void 0 && t.excludedSubtrees.length > 0 ? [...e.excluded, ...t.excludedSubtrees.flatMap((e) => isSupportedNameConstraintForm(e.base) ? [e.base] : [])] : e.excluded, a = n ? listUnsupportedNameConstraintTypes(t) : [];
|
|
98
94
|
return {
|
|
99
|
-
permittedLevels,
|
|
100
|
-
excluded,
|
|
101
|
-
unsupportedCriticalForms:
|
|
95
|
+
permittedLevels: r,
|
|
96
|
+
excluded: i,
|
|
97
|
+
unsupportedCriticalForms: a.length > 0 ? /* @__PURE__ */ new Set([...e.unsupportedCriticalForms, ...a]) : e.unsupportedCriticalForms
|
|
102
98
|
};
|
|
103
99
|
}
|
|
104
100
|
/** Collects the distinct unsupported GeneralName form types from a nameConstraints extension. */
|
|
105
|
-
function listUnsupportedNameConstraintTypes(
|
|
106
|
-
|
|
107
|
-
for (
|
|
108
|
-
for (
|
|
109
|
-
return [...
|
|
101
|
+
function listUnsupportedNameConstraintTypes(e) {
|
|
102
|
+
let t = /* @__PURE__ */ new Set();
|
|
103
|
+
for (let n of e.permittedSubtrees ?? []) isSupportedNameConstraintForm(n.base) || t.add(n.base.type);
|
|
104
|
+
for (let n of e.excludedSubtrees ?? []) isSupportedNameConstraintForm(n.base) || t.add(n.base.type);
|
|
105
|
+
return [...t];
|
|
110
106
|
}
|
|
111
107
|
/** True for name forms this engine can evaluate: dns, email, uri, ip, directoryName. */
|
|
112
|
-
function isSupportedNameConstraintForm(
|
|
113
|
-
switch (
|
|
108
|
+
function isSupportedNameConstraintForm(e) {
|
|
109
|
+
switch (e.type) {
|
|
114
110
|
case "dns":
|
|
115
111
|
case "email":
|
|
116
112
|
case "uri":
|
|
117
113
|
case "ip":
|
|
118
|
-
case "directoryName": return
|
|
114
|
+
case "directoryName": return !0;
|
|
119
115
|
case "otherName":
|
|
120
116
|
case "x400Address":
|
|
121
117
|
case "ediPartyName":
|
|
122
|
-
case "registeredID": return
|
|
123
|
-
default: throw
|
|
118
|
+
case "registeredID": return !1;
|
|
119
|
+
default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
|
|
124
120
|
}
|
|
125
121
|
}
|
|
126
122
|
/**
|
|
127
123
|
* Checks a certificate's subject DN and SANs against accumulated
|
|
128
124
|
* name constraints. Returns a failure if any name violates constraints.
|
|
129
125
|
*/
|
|
130
|
-
function checkCertificateNames(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (checkable === void 0) continue;
|
|
150
|
-
if (!isNamePermitted(checkable, accumulated)) return nameConstraintFailure("name_constraints_violated", `SAN ${formatConstraintForm(checkable)} violates name constraints`, index, nameConstraintDetails({
|
|
151
|
-
subjectCommonName: certificate.subject.values.commonName,
|
|
152
|
-
actual: formatConstraintForm(checkable)
|
|
153
|
-
}));
|
|
154
|
-
}
|
|
155
|
-
if (accumulatedHasEmailConstraints(accumulated)) {
|
|
156
|
-
if (!(certificate.subjectAltNames?.some((san) => san.type === "email") ?? false) && certificate.subject.values.emailAddress !== void 0) {
|
|
157
|
-
if (!isNamePermitted({
|
|
158
|
-
type: "email",
|
|
159
|
-
value: certificate.subject.values.emailAddress
|
|
160
|
-
}, accumulated)) return nameConstraintFailure("name_constraints_violated", `subject emailAddress ${certificate.subject.values.emailAddress} violates name constraints`, index, nameConstraintDetails({
|
|
161
|
-
subjectCommonName: certificate.subject.values.commonName,
|
|
162
|
-
actual: certificate.subject.values.emailAddress
|
|
163
|
-
}));
|
|
164
|
-
}
|
|
126
|
+
function checkCertificateNames(e, t, n) {
|
|
127
|
+
let r = checkCertificateSubjectName(e, t, n);
|
|
128
|
+
if (!r.ok) return r;
|
|
129
|
+
let i = checkCertificateSubjectAltNames(e, t, n);
|
|
130
|
+
if (!i.ok) return i;
|
|
131
|
+
let a = checkCertificateSubjectEmailFallback(e, t, n);
|
|
132
|
+
return a.ok ? { ok: !0 } : a;
|
|
133
|
+
}
|
|
134
|
+
/** Checks a certificate subject DN against the accumulated directory-name constraints. */
|
|
135
|
+
function checkCertificateSubjectName(e, t, n) {
|
|
136
|
+
return e.subject.derHex === "3000" || isNamePermitted({
|
|
137
|
+
type: "directoryName",
|
|
138
|
+
derHex: e.subject.derHex
|
|
139
|
+
}, t) ? { ok: !0 } : nameConstraintFailure("name_constraints_violated", "subject distinguished name violates name constraints", n, nameConstraintDetails({ subjectCommonName: e.subject.values.commonName }));
|
|
140
|
+
}
|
|
141
|
+
function checkCertificateSubjectAltNames(e, t, n) {
|
|
142
|
+
for (let r of e.subjectAltNames ?? []) {
|
|
143
|
+
let i = checkCertificateSubjectAltName(e, t, r, n);
|
|
144
|
+
if (!i.ok) return i;
|
|
165
145
|
}
|
|
166
|
-
return { ok:
|
|
146
|
+
return { ok: !0 };
|
|
147
|
+
}
|
|
148
|
+
function checkCertificateSubjectAltName(e, t, n, r) {
|
|
149
|
+
let i = sanUnsupportedFormType(n);
|
|
150
|
+
if (i !== void 0 && t.unsupportedCriticalForms.has(i)) return nameConstraintFailure("unsupported_name_constraints", `critical name constraints impose ${i} constraints that cannot be processed, and the certificate contains a ${i} subject alternative name`, r, nameConstraintDetails({
|
|
151
|
+
subjectCommonName: e.subject.values.commonName,
|
|
152
|
+
actual: i
|
|
153
|
+
}));
|
|
154
|
+
let a = sanToConstraintCheckable(n);
|
|
155
|
+
if (!a.ok) return nameConstraintFailure("name_constraints_violated", `SAN ${a.actual} is malformed and cannot be checked against name constraints`, r, nameConstraintDetails({
|
|
156
|
+
subjectCommonName: e.subject.values.commonName,
|
|
157
|
+
actual: a.actual
|
|
158
|
+
}));
|
|
159
|
+
let o = a.value;
|
|
160
|
+
return o === void 0 || isNamePermitted(o, t) ? { ok: !0 } : nameConstraintFailure("name_constraints_violated", `SAN ${formatConstraintForm(o)} violates name constraints`, r, nameConstraintDetails({
|
|
161
|
+
subjectCommonName: e.subject.values.commonName,
|
|
162
|
+
actual: formatConstraintForm(o)
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
function checkCertificateSubjectEmailFallback(e, t, n) {
|
|
166
|
+
return accumulatedHasEmailConstraints(t) && !(e.subjectAltNames?.some((e) => e.type === "email") ?? !1) && e.subject.values.emailAddress !== void 0 && !isNamePermitted({
|
|
167
|
+
type: "email",
|
|
168
|
+
value: e.subject.values.emailAddress
|
|
169
|
+
}, t) ? nameConstraintFailure("name_constraints_violated", `subject emailAddress ${e.subject.values.emailAddress} violates name constraints`, n, nameConstraintDetails({
|
|
170
|
+
subjectCommonName: e.subject.values.commonName,
|
|
171
|
+
actual: e.subject.values.emailAddress
|
|
172
|
+
})) : { ok: !0 };
|
|
167
173
|
}
|
|
168
174
|
/** True when any level of accumulated constraints addresses the email name form. */
|
|
169
|
-
function accumulatedHasEmailConstraints(
|
|
170
|
-
for (
|
|
171
|
-
return
|
|
175
|
+
function accumulatedHasEmailConstraints(e) {
|
|
176
|
+
for (let t of e.permittedLevels) if (t.some((e) => e.type === "email")) return !0;
|
|
177
|
+
return e.excluded.some((e) => e.type === "email");
|
|
172
178
|
}
|
|
173
179
|
/**
|
|
174
180
|
* Maps a SAN to the unsupported constraint form it instantiates, if any.
|
|
@@ -177,10 +183,9 @@ function accumulatedHasEmailConstraints(accumulated) {
|
|
|
177
183
|
* tag byte: 0xa0 otherName [0], 0xa3 x400Address [3], 0xa5 ediPartyName [5],
|
|
178
184
|
* 0x88 registeredID [8].
|
|
179
185
|
*/
|
|
180
|
-
function sanUnsupportedFormType(
|
|
181
|
-
if (
|
|
182
|
-
if (
|
|
183
|
-
switch (san.tag) {
|
|
186
|
+
function sanUnsupportedFormType(e) {
|
|
187
|
+
if (e.type === "srv") return "otherName";
|
|
188
|
+
if (e.type === "unknown") switch (e.tag) {
|
|
184
189
|
case 160: return "otherName";
|
|
185
190
|
case 163: return "x400Address";
|
|
186
191
|
case 165: return "ediPartyName";
|
|
@@ -193,60 +198,60 @@ function sanUnsupportedFormType(san) {
|
|
|
193
198
|
* Returns `undefined` for name forms that don't participate in
|
|
194
199
|
* constraint checking (unknown tags).
|
|
195
200
|
*/
|
|
196
|
-
function sanToConstraintCheckable(
|
|
197
|
-
switch (
|
|
201
|
+
function sanToConstraintCheckable(e) {
|
|
202
|
+
switch (e.type) {
|
|
198
203
|
case "dns": return {
|
|
199
|
-
ok:
|
|
204
|
+
ok: !0,
|
|
200
205
|
value: {
|
|
201
206
|
type: "dns",
|
|
202
|
-
value:
|
|
207
|
+
value: e.value
|
|
203
208
|
}
|
|
204
209
|
};
|
|
205
210
|
case "email": return {
|
|
206
|
-
ok:
|
|
211
|
+
ok: !0,
|
|
207
212
|
value: {
|
|
208
213
|
type: "email",
|
|
209
|
-
value:
|
|
214
|
+
value: e.value
|
|
210
215
|
}
|
|
211
216
|
};
|
|
212
217
|
case "uri": return {
|
|
213
|
-
ok:
|
|
218
|
+
ok: !0,
|
|
214
219
|
value: {
|
|
215
220
|
type: "uri",
|
|
216
|
-
value:
|
|
221
|
+
value: e.value
|
|
217
222
|
}
|
|
218
223
|
};
|
|
219
224
|
case "srv": return {
|
|
220
|
-
ok:
|
|
225
|
+
ok: !0,
|
|
221
226
|
value: void 0
|
|
222
227
|
};
|
|
223
228
|
case "ip": try {
|
|
224
229
|
return {
|
|
225
|
-
ok:
|
|
230
|
+
ok: !0,
|
|
226
231
|
value: {
|
|
227
232
|
type: "ip",
|
|
228
|
-
addressBytes:
|
|
229
|
-
maskBytes:
|
|
233
|
+
addressBytes: u(e.value),
|
|
234
|
+
maskBytes: c(e.value)
|
|
230
235
|
}
|
|
231
236
|
};
|
|
232
237
|
} catch {
|
|
233
238
|
return {
|
|
234
|
-
ok:
|
|
235
|
-
actual: `ip:${
|
|
239
|
+
ok: !1,
|
|
240
|
+
actual: `ip:${e.value}`
|
|
236
241
|
};
|
|
237
242
|
}
|
|
238
243
|
case "directoryName": return {
|
|
239
|
-
ok:
|
|
244
|
+
ok: !0,
|
|
240
245
|
value: {
|
|
241
246
|
type: "directoryName",
|
|
242
|
-
derHex:
|
|
247
|
+
derHex: e.derHex
|
|
243
248
|
}
|
|
244
249
|
};
|
|
245
250
|
case "unknown": return {
|
|
246
|
-
ok:
|
|
251
|
+
ok: !0,
|
|
247
252
|
value: void 0
|
|
248
253
|
};
|
|
249
|
-
default: throw
|
|
254
|
+
default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
|
|
250
255
|
}
|
|
251
256
|
}
|
|
252
257
|
/**
|
|
@@ -256,35 +261,26 @@ function sanToConstraintCheckable(san) {
|
|
|
256
261
|
* 2. For every permitted level that contains constraints of the same
|
|
257
262
|
* name form, it matches at least one.
|
|
258
263
|
*/
|
|
259
|
-
function isNamePermitted(
|
|
260
|
-
for (
|
|
261
|
-
for (
|
|
262
|
-
|
|
263
|
-
if (
|
|
264
|
-
if (!relevant.some((constraint) => nameMatchesConstraint(name, constraint))) return false;
|
|
264
|
+
function isNamePermitted(e, t) {
|
|
265
|
+
for (let n of t.excluded) if (nameMatchesConstraint(e, n)) return !1;
|
|
266
|
+
for (let n of t.permittedLevels) {
|
|
267
|
+
let t = n.filter((t) => t.type === e.type);
|
|
268
|
+
if (t.length !== 0 && !t.some((t) => nameMatchesConstraint(e, t))) return !1;
|
|
265
269
|
}
|
|
266
|
-
return
|
|
270
|
+
return !0;
|
|
267
271
|
}
|
|
268
272
|
/** Dispatches to the type-specific matching function for the name form. */
|
|
269
|
-
function nameMatchesConstraint(
|
|
270
|
-
|
|
271
|
-
if (name.type === "email" && constraint.type === "email") return matchesEmailConstraint(name.value, constraint.value);
|
|
272
|
-
if (name.type === "uri" && constraint.type === "uri") return matchesUriConstraint(name.value, constraint.value);
|
|
273
|
-
if (name.type === "ip" && constraint.type === "ip") return matchesIpConstraint(name.addressBytes, constraint.addressBytes, constraint.maskBytes);
|
|
274
|
-
if (name.type === "directoryName" && constraint.type === "directoryName") return matchesDnConstraint(name.derHex, constraint.derHex);
|
|
275
|
-
return false;
|
|
273
|
+
function nameMatchesConstraint(e, t) {
|
|
274
|
+
return e.type === "dns" && t.type === "dns" ? matchesDnsConstraint(e.value, t.value) : e.type === "email" && t.type === "email" ? matchesEmailConstraint(e.value, t.value) : e.type === "uri" && t.type === "uri" ? matchesUriConstraint(e.value, t.value) : e.type === "ip" && t.type === "ip" ? matchesIpConstraint(e.addressBytes, t.addressBytes, t.maskBytes) : e.type === "directoryName" && t.type === "directoryName" && matchesDnConstraint(e.derHex, t.derHex);
|
|
276
275
|
}
|
|
277
276
|
/**
|
|
278
277
|
* RFC 5280 §4.2.1.10: DNS name constraint matching.
|
|
279
278
|
* Constraint "example.com" matches "example.com" and any subdomain.
|
|
280
279
|
* Constraint ".example.com" matches only subdomains, not "example.com" itself.
|
|
281
280
|
*/
|
|
282
|
-
function matchesDnsConstraint(
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
if (lowerConstraint.length === 0) return true;
|
|
286
|
-
if (lowerConstraint.startsWith(".")) return lowerName.endsWith(lowerConstraint);
|
|
287
|
-
return lowerName === lowerConstraint || lowerName.endsWith(`.${lowerConstraint}`);
|
|
281
|
+
function matchesDnsConstraint(e, t) {
|
|
282
|
+
let n = e.toLowerCase(), r = t.toLowerCase();
|
|
283
|
+
return r.length === 0 ? !0 : r.startsWith(".") ? n.endsWith(r) : n === r || n.endsWith(`.${r}`);
|
|
288
284
|
}
|
|
289
285
|
/**
|
|
290
286
|
* RFC 5280 §4.2.1.10: Email constraint matching.
|
|
@@ -292,15 +288,13 @@ function matchesDnsConstraint(name, constraint) {
|
|
|
292
288
|
* - "example.com" matches any address @example.com.
|
|
293
289
|
* - ".example.com" matches any address @subdomain.example.com.
|
|
294
290
|
*/
|
|
295
|
-
function matchesEmailConstraint(
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (lowerConstraint.startsWith(".")) return host.endsWith(lowerConstraint);
|
|
303
|
-
return host === lowerConstraint;
|
|
291
|
+
function matchesEmailConstraint(e, t) {
|
|
292
|
+
let n = e.toLowerCase(), r = t.toLowerCase();
|
|
293
|
+
if (r.includes("@")) return n === r;
|
|
294
|
+
let i = n.indexOf("@");
|
|
295
|
+
if (i < 0) return !1;
|
|
296
|
+
let a = n.slice(i + 1);
|
|
297
|
+
return r.startsWith(".") ? a.endsWith(r) : a === r;
|
|
304
298
|
}
|
|
305
299
|
/**
|
|
306
300
|
* RFC 5280 §4.2.1.10: URI constraint matching.
|
|
@@ -309,19 +303,16 @@ function matchesEmailConstraint(name, constraint) {
|
|
|
309
303
|
* - Constraint "example.com" matches ONLY that exact host (no subdomain
|
|
310
304
|
* expansion, unlike DNS constraints).
|
|
311
305
|
*/
|
|
312
|
-
function matchesUriConstraint(
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if (lowerConstraint.length === 0) return true;
|
|
318
|
-
if (lowerConstraint.startsWith(".")) return lowerHost.endsWith(lowerConstraint);
|
|
319
|
-
return lowerHost === lowerConstraint;
|
|
306
|
+
function matchesUriConstraint(e, t) {
|
|
307
|
+
let n = extractUriHost(e);
|
|
308
|
+
if (n === void 0) return !1;
|
|
309
|
+
let r = n.toLowerCase(), i = t.toLowerCase();
|
|
310
|
+
return i.length === 0 ? !0 : i.startsWith(".") ? r.endsWith(i) : r === i;
|
|
320
311
|
}
|
|
321
312
|
/** Extracts the host (reg-name) portion of a URI, stripping scheme, userinfo, port, and path. */
|
|
322
|
-
function extractUriHost(
|
|
313
|
+
function extractUriHost(e) {
|
|
323
314
|
try {
|
|
324
|
-
return new URL(
|
|
315
|
+
return new URL(e).hostname;
|
|
325
316
|
} catch {
|
|
326
317
|
return;
|
|
327
318
|
}
|
|
@@ -330,51 +321,40 @@ function extractUriHost(uri) {
|
|
|
330
321
|
* RFC 5280 §4.2.1.10: IP constraint matching.
|
|
331
322
|
* (nameiP & mask) == (constraintiP & mask)
|
|
332
323
|
*/
|
|
333
|
-
function matchesIpConstraint(
|
|
334
|
-
if (
|
|
335
|
-
for (let
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const maskByte = constraintMask[i] ?? 0;
|
|
339
|
-
if ((nameByte & maskByte) !== (addrByte & maskByte)) return false;
|
|
324
|
+
function matchesIpConstraint(e, t, n) {
|
|
325
|
+
if (e.length !== t.length) return !1;
|
|
326
|
+
for (let r = 0; r < e.length; r += 1) {
|
|
327
|
+
let i = e[r] ?? 0, a = t[r] ?? 0, o = n[r] ?? 0;
|
|
328
|
+
if ((i & o) !== (a & o)) return !1;
|
|
340
329
|
}
|
|
341
|
-
return
|
|
330
|
+
return !0;
|
|
342
331
|
}
|
|
343
332
|
/**
|
|
344
333
|
* RFC 5280 §4.2.1.10: DirectoryName constraint matching.
|
|
345
334
|
* The subject DN must equal or be subordinate to the constraint DN,
|
|
346
335
|
* using RFC 5280 section 7.1 name comparison semantics.
|
|
347
336
|
*/
|
|
348
|
-
function matchesDnConstraint(
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
if (subjectName === void 0 || constraintName === void 0) return false;
|
|
352
|
-
return isWithinDirectoryNameSubtree(subjectName, constraintName);
|
|
337
|
+
function matchesDnConstraint(e, t) {
|
|
338
|
+
let n = parseDirectoryNameDerHex(e), r = parseDirectoryNameDerHex(t);
|
|
339
|
+
return n === void 0 || r === void 0 ? !1 : p(n, r);
|
|
353
340
|
}
|
|
354
341
|
/** Re-parses a hex-encoded DER Name for RDN-by-RDN comparison. Returns `undefined` on malformed input. */
|
|
355
|
-
function parseDirectoryNameDerHex(
|
|
356
|
-
if (
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const rdn = parseDirectoryNameRdn(bytes, setElement);
|
|
366
|
-
if (rdn === void 0) return;
|
|
367
|
-
rdns.push(rdn);
|
|
368
|
-
for (const attribute of rdn.attributes) {
|
|
369
|
-
attributes.push(attribute);
|
|
370
|
-
if (attribute.key !== void 0 && values[attribute.key] === void 0) values[attribute.key] = attribute.value;
|
|
371
|
-
}
|
|
342
|
+
function parseDirectoryNameDerHex(n) {
|
|
343
|
+
if (/^(?:[0-9a-fA-F]{2})+$/.test(n)) try {
|
|
344
|
+
let r = i(n), a = unwrapDirectoryNameElement(r, e(r, { maxDepth: 64 }));
|
|
345
|
+
if (a.tag !== 48) return;
|
|
346
|
+
let s = [], c = [], l = {};
|
|
347
|
+
for (let e of t(r, a)) {
|
|
348
|
+
let t = parseDirectoryNameRdn(r, e);
|
|
349
|
+
if (t === void 0) return;
|
|
350
|
+
s.push(t);
|
|
351
|
+
for (let e of t.attributes) c.push(e), e.key !== void 0 && l[e.key] === void 0 && (l[e.key] = e.value);
|
|
372
352
|
}
|
|
373
353
|
return {
|
|
374
|
-
derHex:
|
|
375
|
-
rdns,
|
|
376
|
-
attributes,
|
|
377
|
-
values
|
|
354
|
+
derHex: o(r),
|
|
355
|
+
rdns: s,
|
|
356
|
+
attributes: c,
|
|
357
|
+
values: l
|
|
378
358
|
};
|
|
379
359
|
} catch {
|
|
380
360
|
return;
|
|
@@ -386,65 +366,57 @@ function parseDirectoryNameDerHex(derHex) {
|
|
|
386
366
|
* Some encoders produce `[4] { SEQ { SEQ { SET... } } }` instead of `[4] { SEQ { SET... } }`.
|
|
387
367
|
* A single SEQUENCE child indicates the extra wrapper (valid Names have SET children).
|
|
388
368
|
*/
|
|
389
|
-
function unwrapDirectoryNameElement(
|
|
390
|
-
if (
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (children.length === 1 && child?.tag === 48) return child;
|
|
394
|
-
return element;
|
|
369
|
+
function unwrapDirectoryNameElement(e, n) {
|
|
370
|
+
if (n.tag !== 48) return n;
|
|
371
|
+
let r = t(e, n), i = r[0];
|
|
372
|
+
return r.length === 1 && i?.tag === 48 ? i : n;
|
|
395
373
|
}
|
|
396
374
|
/** Parses one SET element (a single RDN) from the DER Name SEQUENCE. */
|
|
397
|
-
function parseDirectoryNameRdn(
|
|
398
|
-
if (
|
|
399
|
-
|
|
400
|
-
if (
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
const valueElement = parts[1];
|
|
408
|
-
if (oidElement === void 0 || valueElement === void 0 || parts.length !== 2) return;
|
|
409
|
-
if (oidElement.tag !== 6) return;
|
|
410
|
-
const oid = decodeObjectIdentifier(requireElement(oidElement, "directoryName OID").value);
|
|
411
|
-
let fieldValue;
|
|
375
|
+
function parseDirectoryNameRdn(e, i) {
|
|
376
|
+
if (i.tag !== 49) return;
|
|
377
|
+
let s = t(e, i);
|
|
378
|
+
if (s.length === 0) return;
|
|
379
|
+
let c = [], l = {};
|
|
380
|
+
for (let i of s) {
|
|
381
|
+
if (i.tag !== 48) return;
|
|
382
|
+
let o = t(e, i), s = o[0], u = o[1];
|
|
383
|
+
if (s === void 0 || u === void 0 || o.length !== 2 || s.tag !== 6) return;
|
|
384
|
+
let f = n(a(s, "directoryName OID").value), p;
|
|
412
385
|
try {
|
|
413
|
-
|
|
386
|
+
p = r(u.tag, a(u, "directoryName value").value);
|
|
414
387
|
} catch {
|
|
415
388
|
return;
|
|
416
389
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
valueTag: valueElement.tag,
|
|
422
|
-
value: fieldValue
|
|
390
|
+
let m = d(f), h = m === void 0 ? {
|
|
391
|
+
oid: f,
|
|
392
|
+
valueTag: u.tag,
|
|
393
|
+
value: p
|
|
423
394
|
} : {
|
|
424
|
-
oid,
|
|
425
|
-
|
|
426
|
-
|
|
395
|
+
oid: f,
|
|
396
|
+
key: m,
|
|
397
|
+
valueTag: u.tag,
|
|
398
|
+
value: p
|
|
427
399
|
};
|
|
428
|
-
|
|
429
|
-
if (fieldKey !== void 0 && values[fieldKey] === void 0) values[fieldKey] = fieldValue;
|
|
400
|
+
c.push(h), m !== void 0 && l[m] === void 0 && (l[m] = p);
|
|
430
401
|
}
|
|
431
|
-
if (
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
values
|
|
402
|
+
if (c.length !== 0) return {
|
|
403
|
+
derHex: o(e.slice(i.start - i.headerLength, i.end)),
|
|
404
|
+
attributes: c,
|
|
405
|
+
values: l
|
|
436
406
|
};
|
|
437
407
|
}
|
|
438
408
|
/** Human-readable label for a constraint form, used in error messages. */
|
|
439
|
-
function formatConstraintForm(
|
|
440
|
-
switch (
|
|
441
|
-
case "dns": return `dns:${
|
|
442
|
-
case "email": return `email:${
|
|
443
|
-
case "uri": return `uri:${
|
|
444
|
-
case "ip": return `ip:${
|
|
445
|
-
case "directoryName": return `dn:${
|
|
446
|
-
default: throw
|
|
409
|
+
function formatConstraintForm(e) {
|
|
410
|
+
switch (e.type) {
|
|
411
|
+
case "dns": return `dns:${e.value}`;
|
|
412
|
+
case "email": return `email:${e.value}`;
|
|
413
|
+
case "uri": return `uri:${e.value}`;
|
|
414
|
+
case "ip": return `ip:${l(e.addressBytes)}`;
|
|
415
|
+
case "directoryName": return `dn:${e.derHex.slice(0, 20)}${e.derHex.length > 20 ? "..." : ""}`;
|
|
416
|
+
default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
|
|
447
417
|
}
|
|
448
418
|
}
|
|
449
419
|
//#endregion
|
|
450
420
|
export { createNameConstraintValidationState, evaluateNameConstraints };
|
|
421
|
+
|
|
422
|
+
//# sourceMappingURL=name-constraints-engine.js.map
|