micro509 0.13.0 → 0.14.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/README.md +78 -30
- package/dist/crypto/crypto.d.ts +73 -0
- package/dist/crypto/crypto.js +74 -0
- package/dist/crypto/crypto.js.map +1 -0
- package/dist/crypto.d.ts +4 -0
- package/dist/crypto.js +2 -0
- package/dist/der/der.d.ts +1 -1
- package/dist/index.d.ts +13 -9
- package/dist/index.js +17 -15
- package/dist/internal/asn1/asn1.js +21 -10
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/der.d.ts +4 -2
- package/dist/internal/asn1/der.js +18 -5
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +7 -1
- package/dist/internal/crypto/algorithm-names.js +4 -0
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +38 -0
- package/dist/internal/crypto/pbes2.js +9 -10
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/rsa-pss.js +2 -0
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +31 -0
- package/dist/internal/crypto/sig-verify.js +10 -8
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/shared/base64.js +7 -1
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +16 -0
- package/dist/internal/shared/dn.js +13 -6
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/ip.d.ts +69 -0
- package/dist/internal/shared/ip.js +18 -1
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/verify/verify-path.d.ts +7 -0
- package/dist/internal/verify/verify-path.js +36 -36
- package/dist/internal/x509/directory-name.js +24 -0
- package/dist/internal/x509/directory-name.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +13 -10
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +6 -0
- package/dist/internal/x509/extension-errors.js +20 -0
- package/dist/internal/x509/extension-errors.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +113 -55
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-tags.js +28 -0
- package/dist/internal/x509/general-name-tags.js.map +1 -0
- package/dist/internal/x509/general-name.js +97 -0
- package/dist/internal/x509/general-name.js.map +1 -0
- package/dist/internal/x509/name-fields.js +20 -10
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/keys/keys.d.ts +16 -2
- package/dist/keys/keys.js +311 -170
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +3 -2
- package/dist/keys.js +3 -2
- package/dist/pem/pem.d.ts +3 -2
- package/dist/pem/pem.js +112 -30
- package/dist/pem/pem.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.js +3 -3
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +85 -21
- package/dist/pkcs/pkcs7.js +261 -186
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +2 -2
- package/dist/result/result.d.ts +18 -10
- package/dist/result/result.js +20 -12
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +2 -2
- package/dist/result.js +2 -2
- package/dist/revocation/chain.js +77 -81
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +13 -2
- package/dist/revocation/crl.js +415 -236
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +58 -17
- package/dist/revocation/ocsp.js +185 -173
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +4 -4
- package/dist/revocation/revocation.js +48 -30
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +4 -4
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +2 -1
- package/dist/verify/identity.js +2 -1
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/policy.d.ts +6 -1
- package/dist/verify/verify.d.ts +4 -2
- package/dist/verify/verify.js +32 -14
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +2 -1
- package/dist/verify.js +4 -3
- package/dist/x509/certificate.d.ts +36 -13
- package/dist/x509/certificate.js +36 -19
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +6 -3
- package/dist/x509/extensions.d.ts +58 -21
- package/dist/x509/extensions.js +384 -117
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/name-text.js +2 -0
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +8 -5
- package/dist/x509/name.js +17 -12
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +104 -18
- package/dist/x509/parse.js +127 -163
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +8 -5
- package/dist/x509.js +10 -8
- package/package.json +28 -20
package/dist/x509/extensions.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { bool as e, concatBytes as t, explicitContext as n,
|
|
2
|
-
import { hexToBytes as
|
|
3
|
-
import { OIDS as
|
|
4
|
-
import { sha1 as
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
1
|
+
import { bool as e, concatBytes as t, explicitContext as n, ia5Bytes as r, ia5String as i, implicitConstructedContext as a, implicitPrimitiveContext as o, integerFromNumber as s, objectIdentifier as c, octetString as l, readElement as u, readRootElement as d, readSequenceChildren as f, sequence as p, tlv as m, utf8String as h } from "../internal/asn1/der.js";
|
|
2
|
+
import { canonicalizeOid as g, childrenOf as _, decodeObjectIdentifier as v, hexToBytes as y, toHex as b } from "../internal/asn1/asn1.js";
|
|
3
|
+
import { OIDS as x } from "../internal/asn1/oids2.js";
|
|
4
|
+
import { sha1 as S } from "../internal/crypto/hash.js";
|
|
5
|
+
import { isResultError as C } from "../result/result.js";
|
|
6
|
+
import { throwExtensionEncoderError as w } from "../internal/x509/extension-errors.js";
|
|
7
|
+
import { encodeDistributionPointReasonFlagsContent as T, encodeKeyUsageExtension as E } from "../internal/x509/extension-bits.js";
|
|
8
|
+
import { allOnesMaskForIpAddress as D, decodeIpAddress as O, normalizeIpAddress as k, parseIpAddressToBytes as A } from "../internal/shared/ip.js";
|
|
9
|
+
import { GENERAL_NAME_WIRE_TAGS as j } from "../internal/x509/general-name-tags.js";
|
|
10
|
+
import { encodeRelativeDistinguishedName as M } from "./name.js";
|
|
11
|
+
import { AUTHORITY_INFO_ACCESS_EXTENSION_DEFINITION as N, AUTHORITY_KEY_IDENTIFIER_EXTENSION_DEFINITION as P, BASIC_CONSTRAINTS_EXTENSION_DEFINITION as F, CERTIFICATE_POLICIES_EXTENSION_DEFINITION as I, CRL_DISTRIBUTION_POINTS_EXTENSION_DEFINITION as L, EXTENDED_KEY_USAGE_EXTENSION_DEFINITION as R, INHIBIT_ANY_POLICY_EXTENSION_DEFINITION as z, KEY_USAGE_EXTENSION_DEFINITION as B, NAME_CONSTRAINTS_EXTENSION_DEFINITION as V, POLICY_CONSTRAINTS_EXTENSION_DEFINITION as H, POLICY_MAPPINGS_EXTENSION_DEFINITION as U, SUBJECT_ALT_NAME_EXTENSION_DEFINITION as W, SUBJECT_KEY_IDENTIFIER_EXTENSION_DEFINITION as G, getExtensionDefinition as K } from "../internal/x509/extension-registry.js";
|
|
9
12
|
//#region src/x509/extensions.ts
|
|
10
13
|
/**
|
|
11
14
|
* Typed certificate and CSR extension model plus ASN.1 encoder helpers.
|
|
@@ -16,16 +19,16 @@ import { AUTHORITY_INFO_ACCESS_EXTENSION_DEFINITION as S, AUTHORITY_KEY_IDENTIFI
|
|
|
16
19
|
* @module
|
|
17
20
|
*/
|
|
18
21
|
/** Map from well-known EKU names to their dotted-decimal OIDs. */
|
|
19
|
-
const
|
|
20
|
-
serverAuth:
|
|
21
|
-
clientAuth:
|
|
22
|
-
codeSigning:
|
|
23
|
-
emailProtection:
|
|
24
|
-
timeStamping:
|
|
25
|
-
ocspSigning:
|
|
26
|
-
},
|
|
27
|
-
ocsp:
|
|
28
|
-
caIssuers:
|
|
22
|
+
const q = {
|
|
23
|
+
serverAuth: x.serverAuth,
|
|
24
|
+
clientAuth: x.clientAuth,
|
|
25
|
+
codeSigning: x.codeSigning,
|
|
26
|
+
emailProtection: x.emailProtection,
|
|
27
|
+
timeStamping: x.timeStamping,
|
|
28
|
+
ocspSigning: x.ocspSigning
|
|
29
|
+
}, J = {
|
|
30
|
+
ocsp: x.ocspAccessMethod,
|
|
31
|
+
caIssuers: x.caIssuersAccessMethod
|
|
29
32
|
};
|
|
30
33
|
/**
|
|
31
34
|
* Build the v3 extensions block for a certificate.
|
|
@@ -37,13 +40,14 @@ const I = {
|
|
|
37
40
|
* @param subjectPublicKeyInfo DER-encoded SPKI of the subject.
|
|
38
41
|
* @param issuerPublicKeyInfo DER-encoded SPKI of the issuer, or `undefined` for self-signed.
|
|
39
42
|
* @param input Optional extension configuration.
|
|
40
|
-
* @param subjectIsEmpty Whether the certificate subject DN is empty. When `true`,
|
|
41
|
-
* subjectAltName extension is marked critical per RFC 5280 §4.2.1.6.
|
|
43
|
+
* @param subjectIsEmpty Whether the certificate subject DN is empty. When `true`, a
|
|
44
|
+
* subjectAltName extension is required and marked critical per RFC 5280 §4.2.1.6.
|
|
42
45
|
* @returns Array of DER-encoded Extension SEQUENCEs.
|
|
43
46
|
*/
|
|
44
47
|
function buildCertificateExtensions(e, t, n, r = !1) {
|
|
48
|
+
assertCustomExtensionsValid(n, "certificate"), r && assertEmptySubjectHasCriticalSubjectAltName(n), assertPathLengthKeyUsage(n), assertSafeCurveKeyUsage(e, n);
|
|
45
49
|
let i = [], a = /* @__PURE__ */ new Set();
|
|
46
|
-
return pushKnownExtension(i, a,
|
|
50
|
+
return pushKnownExtension(i, a, F, n?.basicConstraints ?? { ca: !1 }), pushKnownExtension(i, a, G, buildSubjectKeyIdentifier(e)), t !== void 0 && pushKnownExtension(i, a, P, buildSubjectKeyIdentifier(t)), appendConfiguredExtensions(i, a, n, {
|
|
47
51
|
includeBasicConstraints: !1,
|
|
48
52
|
subjectIsEmpty: r
|
|
49
53
|
}), i;
|
|
@@ -57,36 +61,199 @@ function buildCertificateExtensions(e, t, n, r = !1) {
|
|
|
57
61
|
* @returns Array of DER-encoded Extension SEQUENCEs.
|
|
58
62
|
*/
|
|
59
63
|
function buildRequestedExtensions(e) {
|
|
64
|
+
assertCustomExtensionsValid(e, "csr");
|
|
60
65
|
let t = [];
|
|
61
|
-
return appendConfiguredExtensions(t, /* @__PURE__ */ new Set(), e,
|
|
66
|
+
return appendConfiguredExtensions(t, /* @__PURE__ */ new Set(), e, { includeBasicConstraints: !0 }), t;
|
|
62
67
|
}
|
|
63
68
|
/** Encode and push each configured extension from the input, enforcing no duplicates. */
|
|
64
|
-
function appendConfiguredExtensions(e, t, n, r
|
|
65
|
-
n !== void 0 && (appendConstraintExtensions(e, t, n,
|
|
69
|
+
function appendConfiguredExtensions(e, t, n, r) {
|
|
70
|
+
n !== void 0 && (appendConstraintExtensions(e, t, n, r.includeBasicConstraints), appendIdentityExtensions(e, t, n, r.subjectIsEmpty === !0), appendPolicyExtensions(e, t, n), appendAccessExtensions(e, t, n), appendCustomExtensions(e, t, n));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* RFC 5280 §4.2.1.9: pathLenConstraint requires the keyUsage extension to assert keyCertSign.
|
|
74
|
+
* Absent, empty, or keyCertSign-less keyUsage is rejected. Known extensions supplied through
|
|
75
|
+
* customExtensions participate in the effective view.
|
|
76
|
+
*/
|
|
77
|
+
function assertPathLengthKeyUsage(e) {
|
|
78
|
+
if (resolveEffectiveBasicConstraints(e)?.pathLength === void 0) return;
|
|
79
|
+
let t = resolveEffectiveKeyUsage(e);
|
|
80
|
+
(t === void 0 || t.length === 0 || !t.includes("keyCertSign")) && w("path_length_requires_key_cert_sign", "basicConstraints pathLength requires the keyUsage keyCertSign bit");
|
|
81
|
+
}
|
|
82
|
+
/** RFC 9295 §3: an end-entity id-Ed25519 or id-Ed448 certificate needs one of these. */
|
|
83
|
+
const Y = [
|
|
84
|
+
"nonRepudiation",
|
|
85
|
+
"digitalSignature",
|
|
86
|
+
"cRLSign"
|
|
87
|
+
], X = [
|
|
88
|
+
"keyEncipherment",
|
|
89
|
+
"dataEncipherment",
|
|
90
|
+
"keyAgreement",
|
|
91
|
+
"encipherOnly",
|
|
92
|
+
"decipherOnly"
|
|
93
|
+
], Z = [
|
|
94
|
+
"digitalSignature",
|
|
95
|
+
"nonRepudiation",
|
|
96
|
+
"keyEncipherment",
|
|
97
|
+
"dataEncipherment",
|
|
98
|
+
"keyCertSign",
|
|
99
|
+
"cRLSign"
|
|
100
|
+
];
|
|
101
|
+
/**
|
|
102
|
+
* RFC 9295 §3, which replaces RFC 8410 §5, constrains a keyUsage extension on a
|
|
103
|
+
* certificate whose subject key names one of the four 1.3.101 curves.
|
|
104
|
+
*
|
|
105
|
+
* id-X25519 and id-X448 require `keyAgreement`, admit one of `encipherOnly` and
|
|
106
|
+
* `decipherOnly`, and forbid the rest.
|
|
107
|
+
*
|
|
108
|
+
* id-Ed25519 and id-Ed448 split on whether the certificate is a certification
|
|
109
|
+
* authority. A CA MUST assert `keyCertSign` and MAY add `nonRepudiation`,
|
|
110
|
+
* `digitalSignature`, or `cRLSign`. Any other certificate MUST assert at least
|
|
111
|
+
* one of those three and MUST NOT assert `keyCertSign`. Both forbid the key
|
|
112
|
+
* agreement and encipherment bits. A CRL issuer is the case where `cRLSign` is
|
|
113
|
+
* the bit chosen, and a CRL issuer that is also a CA is the CA case, so the two
|
|
114
|
+
* branches cover the section's four paragraphs.
|
|
115
|
+
*
|
|
116
|
+
* The rule binds only a keyUsage that reaches the wire, so an absent one, and an
|
|
117
|
+
* empty one the builder therefore omits, leave the key unconstrained.
|
|
118
|
+
*/
|
|
119
|
+
function assertSafeCurveKeyUsage(e, t) {
|
|
120
|
+
let n = resolveEffectiveKeyUsage(t);
|
|
121
|
+
if (n === void 0 || n.length === 0) return;
|
|
122
|
+
let r = readSubjectKeyAlgorithmOid(e);
|
|
123
|
+
if (r !== void 0) {
|
|
124
|
+
if (oidEquals(r, x.x25519) || oidEquals(r, x.x448)) {
|
|
125
|
+
assertMontgomeryKeyUsage(n);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
(oidEquals(r, x.ed25519) || oidEquals(r, x.ed448)) && assertEdwardsKeyUsage(n, resolveEffectiveBasicConstraints(t)?.ca === !0);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** The X25519 and X448 half of RFC 9295 §3. */
|
|
132
|
+
function assertMontgomeryKeyUsage(e) {
|
|
133
|
+
e.includes("keyAgreement") || w("montgomery_key_usage_requires_key_agreement", "keyUsage on an X25519 or X448 certificate must set keyAgreement");
|
|
134
|
+
let t = Z.filter((t) => e.includes(t));
|
|
135
|
+
t.length > 0 && w("montgomery_key_usage_forbids_signature_bit", `keyUsage on an X25519 or X448 certificate must not set ${t.join(", ")}`), e.includes("encipherOnly") && e.includes("decipherOnly") && w("montgomery_key_usage_forbids_both_cipher_bits", "keyUsage on an X25519 or X448 certificate must not set both encipherOnly and decipherOnly");
|
|
136
|
+
}
|
|
137
|
+
/** The Ed25519 and Ed448 half of RFC 9295 §3. */
|
|
138
|
+
function assertEdwardsKeyUsage(e, t) {
|
|
139
|
+
t ? e.includes("keyCertSign") || w("edwards_key_usage_requires_key_cert_sign", "keyUsage on an Ed25519 or Ed448 certification authority certificate must set keyCertSign") : Y.some((t) => e.includes(t)) || w("edwards_key_usage_requires_signing_bit", `keyUsage on an Ed25519 or Ed448 certificate must set one of ${Y.join(", ")}`);
|
|
140
|
+
let n = X.filter((t) => e.includes(t));
|
|
141
|
+
n.length > 0 && w("edwards_key_usage_forbids_agreement_bit", `keyUsage on an Ed25519 or Ed448 certificate must not set ${n.join(", ")}`), !t && e.includes("keyCertSign") && w("edwards_key_usage_forbids_key_cert_sign", "keyUsage on an Ed25519 or Ed448 end-entity certificate must not set keyCertSign");
|
|
142
|
+
}
|
|
143
|
+
/** The algorithm OID of a SubjectPublicKeyInfo, or `undefined` when the field is not an OID. */
|
|
144
|
+
function readSubjectKeyAlgorithmOid(e) {
|
|
145
|
+
let t = f(e)[0];
|
|
146
|
+
if (t === void 0 || t.tag !== 48) return;
|
|
147
|
+
let n = _(e, t)[0];
|
|
148
|
+
return n?.tag === 6 ? v(n.value) : void 0;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* RFC 5280 §4.2.1.9: basicConstraints is critical in a CA certificate whose key
|
|
152
|
+
* validates signatures on certificates. Both conjuncts live in other extensions,
|
|
153
|
+
* so the rule sits here rather than in the basicConstraints profile hook.
|
|
154
|
+
*
|
|
155
|
+
* §4.2.1.3 restricts a key only through a keyUsage extension that reaches the
|
|
156
|
+
* wire, so an absent keyUsage, and an empty one the builder therefore omits,
|
|
157
|
+
* leave the key free to validate certificate signatures. Only an emitted keyUsage
|
|
158
|
+
* without keyCertSign takes the certificate out of the clause's scope.
|
|
159
|
+
*
|
|
160
|
+
* Only a custom extension can carry the wrong criticality; the typed field is
|
|
161
|
+
* always emitted with the registry default.
|
|
162
|
+
*/
|
|
163
|
+
function assertCaBasicConstraintsCritical(e) {
|
|
164
|
+
let t = e?.customExtensions?.find((e) => oidEquals(e.oid, x.basicConstraints));
|
|
165
|
+
if (t === void 0 || t.critical === !0 || !F.decode(new Uint8Array(t.value)).ca) return;
|
|
166
|
+
let n = resolveEffectiveKeyUsage(e);
|
|
167
|
+
(n === void 0 || n.length === 0 || n.includes("keyCertSign")) && assertExtensionCriticality("basicConstraints", !0, !1);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* RFC 5280 §4.2.1.6: an empty subject DN requires a subjectAltName extension
|
|
171
|
+
* present, marked critical, and carrying at least one non-empty GeneralName.
|
|
172
|
+
*/
|
|
173
|
+
function assertEmptySubjectHasCriticalSubjectAltName(e) {
|
|
174
|
+
e?.subjectAltNames?.some(subjectAltNameHasIdentity) !== !0 && e?.customExtensions?.some((e) => oidEquals(e.oid, x.subjectAltName) && e.critical === !0 && customSubjectAltNameHasIdentity(e.value)) !== !0 && w("empty_subject_requires_subject_alt_name", "An empty subject requires a critical subjectAltName extension");
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Canonical OID equality: matches even when arcs carry redundant leading zeros.
|
|
178
|
+
*
|
|
179
|
+
* Both sides must be encodable. Callers run after `assertCustomExtensionsValid`,
|
|
180
|
+
* which rejects an OID that cannot be canonicalized.
|
|
181
|
+
*/
|
|
182
|
+
function oidEquals(e, t) {
|
|
183
|
+
return e === t || g(e) === g(t);
|
|
184
|
+
}
|
|
185
|
+
/** First customExtensions value whose OID canonically matches `oid`. */
|
|
186
|
+
function findCustomExtensionValue(e, t) {
|
|
187
|
+
return e?.customExtensions?.find((e) => oidEquals(e.oid, t))?.value;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Rejects a custom extension carrying a known OID whose payload is not the DER
|
|
191
|
+
* that OID's schema defines, and a known extension offered in the wrong context.
|
|
192
|
+
*
|
|
193
|
+
* Runs before every cross-field guard so the effective-value resolvers below can
|
|
194
|
+
* decode a custom payload without having to tolerate failure.
|
|
195
|
+
*/
|
|
196
|
+
function assertCustomExtensionsValid(e, t) {
|
|
197
|
+
for (let n of e?.customExtensions ?? []) {
|
|
198
|
+
let e = K(validateOid(n.oid));
|
|
199
|
+
e !== void 0 && (e.contexts.includes(t) || w("extension_not_supported_in_context", `Extension ${n.oid} is not supported in ${t} context`), assertKnownExtensionPayload(e, new Uint8Array(n.value), n.critical ?? !1, n.oid));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Decode a custom payload once through its known OID's definition and apply that
|
|
204
|
+
* extension's profile rules to the decoded value and its criticality.
|
|
205
|
+
*
|
|
206
|
+
* A profile violation already carries its own code and propagates unchanged; a
|
|
207
|
+
* decode failure means the payload is not the DER the OID's schema defines.
|
|
208
|
+
*/
|
|
209
|
+
function assertKnownExtensionPayload(e, t, n, r) {
|
|
210
|
+
try {
|
|
211
|
+
e.assertDerProfile(t, n);
|
|
212
|
+
} catch (t) {
|
|
213
|
+
if (C(t)) throw t;
|
|
214
|
+
w("malformed_known_extension_value", `Custom extension ${r} does not decode as ${e.oid}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Effective basicConstraints across the typed field and any custom-known extension. */
|
|
218
|
+
function resolveEffectiveBasicConstraints(e) {
|
|
219
|
+
if (e?.basicConstraints !== void 0) return e.basicConstraints;
|
|
220
|
+
let t = findCustomExtensionValue(e, x.basicConstraints);
|
|
221
|
+
return t === void 0 ? void 0 : F.decode(t);
|
|
222
|
+
}
|
|
223
|
+
/** Effective keyUsage flags across the typed field and any custom-known extension. */
|
|
224
|
+
function resolveEffectiveKeyUsage(e) {
|
|
225
|
+
if (e?.keyUsage !== void 0) return e.keyUsage;
|
|
226
|
+
let t = findCustomExtensionValue(e, x.keyUsage);
|
|
227
|
+
return t === void 0 ? void 0 : B.decode(t).flags;
|
|
228
|
+
}
|
|
229
|
+
/** Whether a typed GeneralName carries a non-empty identity value. */
|
|
230
|
+
function subjectAltNameHasIdentity(e) {
|
|
231
|
+
return e.type === "directoryName" ? e.derHex.length > 0 : e.value.length > 0;
|
|
232
|
+
}
|
|
233
|
+
/** Whether a custom subjectAltName value decodes to at least one non-empty GeneralName. */
|
|
234
|
+
function customSubjectAltNameHasIdentity(e) {
|
|
235
|
+
return W.decode(e).some(subjectAltNameHasIdentity);
|
|
66
236
|
}
|
|
67
237
|
function appendConstraintExtensions(e, t, n, r) {
|
|
68
|
-
r && n.basicConstraints !== void 0 && pushKnownExtension(e, t,
|
|
238
|
+
assertPathLengthKeyUsage(n), assertCaBasicConstraintsCritical(n), r && n.basicConstraints !== void 0 && pushKnownExtension(e, t, F, n.basicConstraints), n.keyUsage !== void 0 && n.keyUsage.length > 0 && pushKnownExtension(e, t, B, n.keyUsage), n.nameConstraints !== void 0 && pushKnownExtension(e, t, V, n.nameConstraints);
|
|
69
239
|
}
|
|
70
240
|
function appendIdentityExtensions(e, t, n, r) {
|
|
71
241
|
if (n.subjectAltNames !== void 0 && n.subjectAltNames.length > 0) {
|
|
72
242
|
let i = r ? !0 : void 0;
|
|
73
|
-
pushKnownExtension(e, t,
|
|
243
|
+
pushKnownExtension(e, t, W, n.subjectAltNames, i);
|
|
74
244
|
}
|
|
75
|
-
n.extendedKeyUsage !== void 0 && n.extendedKeyUsage.length > 0 && pushKnownExtension(e, t,
|
|
245
|
+
n.extendedKeyUsage !== void 0 && n.extendedKeyUsage.length > 0 && pushKnownExtension(e, t, R, n.extendedKeyUsage);
|
|
76
246
|
}
|
|
77
247
|
/** Appends configured certificate-policy extensions while enforcing unique OIDs. */
|
|
78
248
|
function appendPolicyExtensions(e, t, n) {
|
|
79
|
-
n.certificatePolicies !== void 0 && n.certificatePolicies.length > 0 && pushKnownExtension(e, t,
|
|
249
|
+
n.certificatePolicies !== void 0 && n.certificatePolicies.length > 0 && pushKnownExtension(e, t, I, n.certificatePolicies), n.policyMappings !== void 0 && n.policyMappings.length > 0 && pushKnownExtension(e, t, U, n.policyMappings), n.policyConstraints !== void 0 && pushKnownExtension(e, t, H, n.policyConstraints), n.inhibitAnyPolicy !== void 0 && pushKnownExtension(e, t, z, n.inhibitAnyPolicy);
|
|
80
250
|
}
|
|
81
251
|
function appendAccessExtensions(e, t, n) {
|
|
82
|
-
n.authorityInfoAccess !== void 0 && n.authorityInfoAccess.length > 0 && pushKnownExtension(e, t,
|
|
252
|
+
n.authorityInfoAccess !== void 0 && n.authorityInfoAccess.length > 0 && pushKnownExtension(e, t, N, n.authorityInfoAccess), n.crlDistributionPoints !== void 0 && n.crlDistributionPoints.length > 0 && pushKnownExtension(e, t, L, n.crlDistributionPoints);
|
|
83
253
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (n !== void 0 && !n.contexts.includes(r)) throw Error(`Extension ${i.oid} is not supported in ${r} context`);
|
|
88
|
-
pushExtension(e, t, i.oid, new Uint8Array(i.value), i.critical ?? !1);
|
|
89
|
-
}
|
|
254
|
+
/** Push each custom extension. Context and payload were checked by `assertCustomExtensionsValid`. */
|
|
255
|
+
function appendCustomExtensions(e, t, n) {
|
|
256
|
+
for (let r of n.customExtensions ?? []) pushExtension(e, t, r.oid, new Uint8Array(r.value), r.critical ?? !1);
|
|
90
257
|
}
|
|
91
258
|
/** Encode a known extension via its registry definition and push it. */
|
|
92
259
|
function pushKnownExtension(e, t, n, r, i = n.defaultCritical) {
|
|
@@ -100,8 +267,9 @@ function pushKnownExtension(e, t, n, r, i = n.defaultCritical) {
|
|
|
100
267
|
* @param critical Whether to mark the extension as critical. Default `false`.
|
|
101
268
|
*/
|
|
102
269
|
function encodeExtension(t, n, r = !1) {
|
|
103
|
-
|
|
104
|
-
|
|
270
|
+
validateOid(t);
|
|
271
|
+
let i = [c(t)];
|
|
272
|
+
return r && i.push(e(!0)), i.push(l(n)), p(i);
|
|
105
273
|
}
|
|
106
274
|
/**
|
|
107
275
|
* DER-encode a {@linkcode BasicConstraints} value.
|
|
@@ -111,11 +279,7 @@ function encodeExtension(t, n, r = !1) {
|
|
|
111
279
|
*/
|
|
112
280
|
function encodeBasicConstraints(t) {
|
|
113
281
|
let n = [];
|
|
114
|
-
|
|
115
|
-
if (!t.ca) throw Error("pathLength requires ca=true");
|
|
116
|
-
n.push(o(t.pathLength));
|
|
117
|
-
}
|
|
118
|
-
return f(n);
|
|
282
|
+
return t.ca && n.push(e(!0)), t.pathLength !== void 0 && (t.ca || w("path_length_requires_ca", "basicConstraints pathLength requires ca = true"), n.push(s(t.pathLength))), p(n);
|
|
119
283
|
}
|
|
120
284
|
/**
|
|
121
285
|
* DER-encode a Key Usage BIT STRING from an array of {@linkcode KeyUsage} flags.
|
|
@@ -123,7 +287,18 @@ function encodeBasicConstraints(t) {
|
|
|
123
287
|
* @param usages Flags to set in the bit string.
|
|
124
288
|
*/
|
|
125
289
|
function encodeKeyUsage(e) {
|
|
126
|
-
return
|
|
290
|
+
return E(e);
|
|
291
|
+
}
|
|
292
|
+
function encodeIa5Content(e) {
|
|
293
|
+
try {
|
|
294
|
+
return r(e);
|
|
295
|
+
} catch {
|
|
296
|
+
w("invalid_ia5_string", "Invalid IA5String: contains non-ASCII characters");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/** RFC 5280 §4.2.1.6: reject an empty GeneralName string value (empty dNSName, rfc822Name, URI, or SRV name). */
|
|
300
|
+
function requireNonEmptyName(e) {
|
|
301
|
+
return e.length === 0 && w("empty_general_name_value", "GeneralName value must not be empty"), e;
|
|
127
302
|
}
|
|
128
303
|
/**
|
|
129
304
|
* DER-encode a single {@linkcode SubjectAltName} GeneralName element.
|
|
@@ -132,13 +307,13 @@ function encodeKeyUsage(e) {
|
|
|
132
307
|
*/
|
|
133
308
|
function encodeSubjectAltName(e) {
|
|
134
309
|
switch (e.type) {
|
|
135
|
-
case "dns": return
|
|
136
|
-
case "email": return
|
|
137
|
-
case "uri": return
|
|
138
|
-
case "srv": return
|
|
139
|
-
case "ip": return
|
|
140
|
-
case "directoryName": return
|
|
141
|
-
case "unknown": return
|
|
310
|
+
case "dns": return o(2, encodeIa5Content(requireNonEmptyName(e.value)));
|
|
311
|
+
case "email": return o(1, encodeIa5Content(requireNonEmptyName(e.value)));
|
|
312
|
+
case "uri": return o(6, encodeIa5Content(requireNonEmptyName(e.value)));
|
|
313
|
+
case "srv": return a(0, t([c(x.idOnDnsSrv), n(0, m(22, encodeIa5Content(requireNonEmptyName(e.value))))]));
|
|
314
|
+
case "ip": return o(7, encodeIpAddress(e.value));
|
|
315
|
+
case "directoryName": return a(4, readDirectoryNameTlv(e.derHex));
|
|
316
|
+
case "unknown": return j.has(e.tag) || w("invalid_general_name_tag", `Invalid GeneralName tag: ${e.tag}`), m(e.tag, e.value);
|
|
142
317
|
default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
|
|
143
318
|
}
|
|
144
319
|
}
|
|
@@ -148,7 +323,7 @@ function encodeSubjectAltName(e) {
|
|
|
148
323
|
* @param usages EKU purposes to encode.
|
|
149
324
|
*/
|
|
150
325
|
function encodeExtendedKeyUsage(e) {
|
|
151
|
-
return
|
|
326
|
+
return e.length === 0 && w("extended_key_usage_empty", "extendedKeyUsage must not be empty"), p(e.map((e) => c(getExtendedKeyUsageOid(e))));
|
|
152
327
|
}
|
|
153
328
|
/**
|
|
154
329
|
* DER-encode an Authority Information Access SEQUENCE.
|
|
@@ -156,7 +331,18 @@ function encodeExtendedKeyUsage(e) {
|
|
|
156
331
|
* @param entries AIA entries (OCSP, caIssuers, or custom) to encode.
|
|
157
332
|
*/
|
|
158
333
|
function encodeAuthorityInfoAccess(e) {
|
|
159
|
-
return
|
|
334
|
+
return assertAuthorityInfoAccessProfile(e), p(e.map((e) => p([c(getAuthorityInfoAccessMethodOid(e.method)), encodeSubjectAltName(e.location)])));
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* @internal RFC 5280 §4.2.2.1 and RFC 6960 §3.1 rules for Authority Information
|
|
338
|
+
* Access: at least one entry, and an id-ad-ocsp location that is a URI.
|
|
339
|
+
*
|
|
340
|
+
* The method OID is resolved before the URI check so a custom-OID wrapper cannot
|
|
341
|
+
* smuggle a non-URI OCSP location past the input union.
|
|
342
|
+
*/
|
|
343
|
+
function assertAuthorityInfoAccessProfile(e) {
|
|
344
|
+
e.length === 0 && w("authority_info_access_empty", "authorityInfoAccess must not be empty");
|
|
345
|
+
for (let t of e) getAuthorityInfoAccessMethodOid(t.method) === x.ocspAccessMethod && t.location.type !== "uri" && w("authority_info_access_ocsp_not_uri", "authorityInfoAccess OCSP location must be a URI");
|
|
160
346
|
}
|
|
161
347
|
/**
|
|
162
348
|
* DER-encode a CRL Distribution Points SEQUENCE.
|
|
@@ -164,7 +350,23 @@ function encodeAuthorityInfoAccess(e) {
|
|
|
164
350
|
* @param points Distribution points to encode.
|
|
165
351
|
*/
|
|
166
352
|
function encodeCrlDistributionPoints(e) {
|
|
167
|
-
return
|
|
353
|
+
return assertCrlDistributionPointsProfile(e), p(e.map((e) => p(encodeDistributionPoint(e))));
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* @internal RFC 5280 §4.2.1.13 profile rules for a CRLDistributionPoints value,
|
|
357
|
+
* stated over the fields a typed {@linkcode DistributionPoint} and a decoded
|
|
358
|
+
* `ParsedDistributionPoint` share.
|
|
359
|
+
*
|
|
360
|
+
* `TRelativeName` differs between the two: builder input carries an unencoded RDN,
|
|
361
|
+
* a decoded point carries the parsed one. The rules only read its presence.
|
|
362
|
+
*/
|
|
363
|
+
function assertCrlDistributionPointsProfile(e) {
|
|
364
|
+
e.length === 0 && w("crl_distribution_points_empty", "cRLDistributionPoints must not be empty");
|
|
365
|
+
for (let t of e) assertDistributionPointProfile(t);
|
|
366
|
+
}
|
|
367
|
+
/** RFC 5280 §4.2.1.13 rules for one distribution point. */
|
|
368
|
+
function assertDistributionPointProfile(e) {
|
|
369
|
+
e.crlIssuer !== void 0 && e.crlIssuer.length === 0 && w("distribution_point_crl_issuer_empty", "DistributionPoint crlIssuer must not be empty"), e.distributionPoint === void 0 && e.crlIssuer === void 0 && w("distribution_point_empty", "DistributionPoint must contain distributionPoint or crlIssuer"), assertCrlIssuerDistinguishedNames(e), e.distributionPoint?.type === "fullName" && e.distributionPoint.fullName.length === 0 && w("distribution_point_full_name_empty", "DistributionPointName fullName must not be empty");
|
|
168
370
|
}
|
|
169
371
|
/**
|
|
170
372
|
* DER-encode a Name Constraints extension value.
|
|
@@ -172,8 +374,30 @@ function encodeCrlDistributionPoints(e) {
|
|
|
172
374
|
* @param constraints Permitted and/or excluded subtrees.
|
|
173
375
|
*/
|
|
174
376
|
function encodeNameConstraints(e) {
|
|
377
|
+
assertNameConstraintsProfile(e);
|
|
175
378
|
let n = [];
|
|
176
|
-
return e.permittedSubtrees !== void 0 && e.permittedSubtrees.length > 0 && n.push(
|
|
379
|
+
return e.permittedSubtrees !== void 0 && e.permittedSubtrees.length > 0 && n.push(a(0, t(e.permittedSubtrees.map(encodeGeneralSubtree)))), e.excludedSubtrees !== void 0 && e.excludedSubtrees.length > 0 && n.push(a(1, t(e.excludedSubtrees.map(encodeGeneralSubtree)))), p(n);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* @internal RFC 5280 fixes the criticality of several extensions: nameConstraints
|
|
383
|
+
* (§4.2.1.10), policyConstraints (§4.2.1.11), and inhibitAnyPolicy (§4.2.1.14) are
|
|
384
|
+
* critical; authorityKeyIdentifier (§4.2.1.1), subjectKeyIdentifier (§4.2.1.2),
|
|
385
|
+
* and authorityInfoAccess (§4.2.2.1) are not.
|
|
386
|
+
*
|
|
387
|
+
* @param label Extension name quoted in the diagnostic.
|
|
388
|
+
* @param required The criticality RFC 5280 mandates.
|
|
389
|
+
* @param critical The criticality the caller asked for.
|
|
390
|
+
*/
|
|
391
|
+
function assertExtensionCriticality(e, t, n) {
|
|
392
|
+
n !== t && w(t ? "extension_must_be_critical" : "extension_must_be_non_critical", `The ${e} extension must be marked ${t ? "critical" : "non-critical"}`);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* @internal RFC 5280 §4.2.1.10: a nameConstraints extension states at least one
|
|
396
|
+
* of permittedSubtrees and excludedSubtrees, and neither may be an empty set.
|
|
397
|
+
*/
|
|
398
|
+
function assertNameConstraintsProfile(e) {
|
|
399
|
+
let t = e.permittedSubtrees?.length ?? 0, n = e.excludedSubtrees?.length ?? 0;
|
|
400
|
+
t === 0 && n === 0 && w("name_constraints_empty", "nameConstraints must set permittedSubtrees or excludedSubtrees");
|
|
177
401
|
}
|
|
178
402
|
/**
|
|
179
403
|
* DER-encode a Certificate Policies extension value.
|
|
@@ -181,8 +405,13 @@ function encodeNameConstraints(e) {
|
|
|
181
405
|
* @param policies Non-empty array of policy information entries.
|
|
182
406
|
*/
|
|
183
407
|
function encodeCertificatePolicies(e) {
|
|
184
|
-
|
|
185
|
-
|
|
408
|
+
e.length === 0 && w("certificate_policies_empty", "certificatePolicies must not be empty");
|
|
409
|
+
let t = /* @__PURE__ */ new Set();
|
|
410
|
+
for (let n of e) {
|
|
411
|
+
let e = validatePolicyOid(n.policyIdentifier);
|
|
412
|
+
t.has(e) && w("duplicate_policy_oid", `Duplicate certificate policy OID: ${n.policyIdentifier}`), t.add(e);
|
|
413
|
+
}
|
|
414
|
+
return p(e.map(encodePolicyInformation));
|
|
186
415
|
}
|
|
187
416
|
/**
|
|
188
417
|
* DER-encode a Policy Mappings extension value.
|
|
@@ -190,10 +419,9 @@ function encodeCertificatePolicies(e) {
|
|
|
190
419
|
* @param mappings Non-empty array of issuer-to-subject policy pairs. Neither OID may be anyPolicy.
|
|
191
420
|
*/
|
|
192
421
|
function encodePolicyMappings(e) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return f([s(e.issuerDomainPolicy), s(e.subjectDomainPolicy)]);
|
|
422
|
+
return e.length === 0 && w("policy_mappings_empty", "policyMappings must not be empty"), p(e.map((e) => {
|
|
423
|
+
let t = validatePolicyOid(e.issuerDomainPolicy), n = validatePolicyOid(e.subjectDomainPolicy);
|
|
424
|
+
return (t === x.anyPolicy || n === x.anyPolicy) && w("policy_mappings_any_policy", "policyMappings must not use anyPolicy"), p([c(e.issuerDomainPolicy), c(e.subjectDomainPolicy)]);
|
|
197
425
|
}));
|
|
198
426
|
}
|
|
199
427
|
/**
|
|
@@ -203,8 +431,7 @@ function encodePolicyMappings(e) {
|
|
|
203
431
|
*/
|
|
204
432
|
function encodePolicyConstraints(e) {
|
|
205
433
|
let t = [];
|
|
206
|
-
|
|
207
|
-
return f(t);
|
|
434
|
+
return e.requireExplicitPolicy !== void 0 && t.push(o(0, encodeIntegerContent(e.requireExplicitPolicy))), e.inhibitPolicyMapping !== void 0 && t.push(o(1, encodeIntegerContent(e.inhibitPolicyMapping))), t.length === 0 && w("policy_constraints_empty", "policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping"), p(t);
|
|
208
435
|
}
|
|
209
436
|
/**
|
|
210
437
|
* DER-encode an Inhibit anyPolicy extension value (single INTEGER).
|
|
@@ -212,76 +439,93 @@ function encodePolicyConstraints(e) {
|
|
|
212
439
|
* @param input The skipCerts threshold.
|
|
213
440
|
*/
|
|
214
441
|
function encodeInhibitAnyPolicy(e) {
|
|
215
|
-
return
|
|
442
|
+
return s(e.skipCerts);
|
|
216
443
|
}
|
|
217
444
|
/** DER-encode a single PolicyInformation SEQUENCE. */
|
|
218
445
|
function encodePolicyInformation(e) {
|
|
219
446
|
validatePolicyOid(e.policyIdentifier);
|
|
220
|
-
let t = [
|
|
221
|
-
return e.policyQualifiers !== void 0 && e.policyQualifiers.length > 0 && t.push(
|
|
447
|
+
let t = [c(e.policyIdentifier)];
|
|
448
|
+
return e.policyQualifiers !== void 0 && e.policyQualifiers.length > 0 && t.push(p(e.policyQualifiers.map(encodePolicyQualifierInfo))), p(t);
|
|
222
449
|
}
|
|
223
450
|
/** DER-encode a single PolicyQualifierInfo SEQUENCE. */
|
|
224
451
|
function encodePolicyQualifierInfo(e) {
|
|
225
452
|
switch (e.type) {
|
|
226
|
-
case "cps": return
|
|
227
|
-
case "userNotice": return
|
|
228
|
-
case "oid":
|
|
453
|
+
case "cps": return p([c(x.cpsPolicyQualifier), i(e.uri)]);
|
|
454
|
+
case "userNotice": return p([c(x.userNoticePolicyQualifier), encodeUserNoticePolicyQualifierInfo(e)]);
|
|
455
|
+
case "oid": {
|
|
456
|
+
validateOid(e.oid);
|
|
457
|
+
let t = c(e.oid), n = b(t);
|
|
458
|
+
return (n === b(c(x.cpsPolicyQualifier)) || n === b(c(x.userNoticePolicyQualifier))) && w("reserved_policy_qualifier_oid", "Policy qualifier must use the typed cps or userNotice variant, not a built-in OID"), p([t, new Uint8Array(e.qualifierDer)]);
|
|
459
|
+
}
|
|
229
460
|
default: throw Error(`Unhandled PolicyQualifierInfo type: ${String(e)}`);
|
|
230
461
|
}
|
|
231
462
|
}
|
|
232
463
|
/** DER-encode a UserNotice qualifier SEQUENCE. */
|
|
464
|
+
function assertDisplayText(e) {
|
|
465
|
+
let t = [...e].length;
|
|
466
|
+
(t === 0 || t > 200) && w("display_text_out_of_range", "DisplayText must be 1 to 200 characters");
|
|
467
|
+
}
|
|
233
468
|
function encodeUserNoticePolicyQualifierInfo(e) {
|
|
234
469
|
let t = [];
|
|
235
|
-
return e.noticeRef !== void 0 && t.push(encodePolicyNoticeReference(e.noticeRef)), e.explicitText !== void 0 && t.push(
|
|
470
|
+
return e.noticeRef !== void 0 && t.push(encodePolicyNoticeReference(e.noticeRef)), e.explicitText !== void 0 && (assertDisplayText(e.explicitText), t.push(h(e.explicitText))), p(t);
|
|
236
471
|
}
|
|
237
472
|
/** DER-encode a NoticeReference SEQUENCE. */
|
|
238
473
|
function encodePolicyNoticeReference(e) {
|
|
239
|
-
return
|
|
474
|
+
return assertDisplayText(e.organization), p([h(e.organization), p(e.noticeNumbers.map((e) => s(e)))]);
|
|
240
475
|
}
|
|
241
476
|
/** Encode an INTEGER and return only the content bytes (no TLV wrapper). */
|
|
242
477
|
function encodeIntegerContent(e) {
|
|
243
|
-
return
|
|
478
|
+
return u(s(e)).value;
|
|
244
479
|
}
|
|
245
480
|
/** DER-encode a GeneralSubtree SEQUENCE (base name only; min/max omitted per RFC 5280). */
|
|
246
481
|
function encodeGeneralSubtree(e) {
|
|
247
|
-
return
|
|
482
|
+
return p([encodeNameConstraintForm(e.base)]);
|
|
248
483
|
}
|
|
249
|
-
/**
|
|
484
|
+
/**
|
|
485
|
+
* RFC 5280 §4.2.1.13: cRLIssuer, when present, only contains the CRL issuer's
|
|
486
|
+
* distinguished name; nameRelativeToCRLIssuer additionally requires exactly one.
|
|
487
|
+
*
|
|
488
|
+
* Applies to a typed {@linkcode DistributionPoint} and to a decoded
|
|
489
|
+
* {@linkcode ParsedDistributionPoint} alike, so the rule holds whether the value
|
|
490
|
+
* arrives through `crlDistributionPoints` or through `customExtensions`.
|
|
491
|
+
*/
|
|
492
|
+
function assertCrlIssuerDistinguishedNames(e) {
|
|
493
|
+
e.crlIssuer !== void 0 && (e.crlIssuer.some((e) => e.type !== "directoryName") && w("distribution_point_crl_issuer_not_directory_name", "DistributionPoint cRLIssuer must only contain directoryName entries"), e.distributionPoint?.type === "relativeName" && e.crlIssuer.length > 1 && w("distribution_point_relative_name_multiple_crl_issuers", "DistributionPointName relativeName requires at most one cRLIssuer distinguished name"));
|
|
494
|
+
}
|
|
495
|
+
/** DER-encode the fields of a single DistributionPoint, already profile-checked. */
|
|
250
496
|
function encodeDistributionPoint(e) {
|
|
251
|
-
if (e.crlIssuer !== void 0 && e.crlIssuer.length === 0) throw Error("DistributionPoint crlIssuer must not be empty");
|
|
252
|
-
if (e.distributionPoint === void 0 && e.crlIssuer === void 0) throw Error("DistributionPoint must contain distributionPoint or crlIssuer");
|
|
253
497
|
let n = [];
|
|
254
|
-
return e.distributionPoint !== void 0 && n.push(
|
|
498
|
+
return e.distributionPoint !== void 0 && n.push(a(0, encodeDistributionPointName(e.distributionPoint))), e.reasons !== void 0 && e.reasons.length > 0 && n.push(o(1, T(e.reasons))), e.crlIssuer !== void 0 && n.push(a(2, t(e.crlIssuer.map(encodeSubjectAltName)))), n;
|
|
255
499
|
}
|
|
256
500
|
/** DER-encode a DistributionPointName (fullName or relativeName). */
|
|
257
501
|
function encodeDistributionPointName(e) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
return i(1, t.slice(n.start, n.end));
|
|
502
|
+
switch (e.type) {
|
|
503
|
+
case "fullName": return e.fullName.length === 0 && w("distribution_point_full_name_empty", "DistributionPointName fullName must not be empty"), a(0, t(e.fullName.map(encodeSubjectAltName)));
|
|
504
|
+
case "relativeName": {
|
|
505
|
+
let t = M(e.relativeName), n = u(t);
|
|
506
|
+
return a(1, t.slice(n.start, n.end));
|
|
507
|
+
}
|
|
508
|
+
default: throw Error(`Unhandled DistributionPointName type: ${String(e)}`);
|
|
266
509
|
}
|
|
267
|
-
throw Error("DistributionPointName must contain fullName or relativeName");
|
|
268
510
|
}
|
|
269
511
|
/** DER-encode a NameConstraintForm as an implicit-tagged {@linkcode GeneralName}. */
|
|
270
512
|
function encodeNameConstraintForm(e) {
|
|
271
513
|
switch (e.type) {
|
|
272
|
-
case "dns": return
|
|
273
|
-
case "email": return
|
|
274
|
-
case "uri": return
|
|
275
|
-
case "ip":
|
|
276
|
-
|
|
514
|
+
case "dns": return o(2, r(e.value));
|
|
515
|
+
case "email": return o(1, r(e.value));
|
|
516
|
+
case "uri": return o(6, r(e.value));
|
|
517
|
+
case "ip": {
|
|
518
|
+
let n = e.addressBytes.length + e.maskBytes.length;
|
|
519
|
+
return (e.addressBytes.length !== e.maskBytes.length || n !== 8 && n !== 32) && w("invalid_ip_name_constraint", "IP name constraint must be 8 octets (IPv4) or 32 octets (IPv6)"), o(7, t([e.addressBytes, e.maskBytes]));
|
|
520
|
+
}
|
|
521
|
+
case "directoryName": return a(4, readDirectoryNameTlv(e.derHex));
|
|
277
522
|
default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
|
|
278
523
|
}
|
|
279
524
|
}
|
|
280
|
-
/**
|
|
281
|
-
function
|
|
282
|
-
let t =
|
|
283
|
-
|
|
284
|
-
return new Uint8Array(t.value);
|
|
525
|
+
/** Returns the complete Name TLV from a hex-encoded DER SEQUENCE, validating the root tag. */
|
|
526
|
+
function readDirectoryNameTlv(e) {
|
|
527
|
+
let t = y(e), n = d(t, { maxDepth: 64 });
|
|
528
|
+
return n.tag !== 48 && w("directory_name_not_sequence", "directoryName derHex must encode a DER SEQUENCE"), t.slice(n.start - n.headerLength, n.end);
|
|
285
529
|
}
|
|
286
530
|
/**
|
|
287
531
|
* Resolve an {@linkcode ExtendedKeyUsage} to its dotted-decimal OID.
|
|
@@ -289,7 +533,7 @@ function extractDirectoryNameContent(e) {
|
|
|
289
533
|
* @param usage Well-known string or custom OID object.
|
|
290
534
|
*/
|
|
291
535
|
function getExtendedKeyUsageOid(e) {
|
|
292
|
-
return typeof e == "string" ?
|
|
536
|
+
return typeof e == "string" ? q[e] : validateOid(e.value);
|
|
293
537
|
}
|
|
294
538
|
/**
|
|
295
539
|
* Map a dotted-decimal OID to an {@linkcode ExtendedKeyUsage} value.
|
|
@@ -298,12 +542,12 @@ function getExtendedKeyUsageOid(e) {
|
|
|
298
542
|
*/
|
|
299
543
|
function parseExtendedKeyUsageOid(e) {
|
|
300
544
|
switch (e) {
|
|
301
|
-
case
|
|
302
|
-
case
|
|
303
|
-
case
|
|
304
|
-
case
|
|
305
|
-
case
|
|
306
|
-
case
|
|
545
|
+
case x.serverAuth: return "serverAuth";
|
|
546
|
+
case x.clientAuth: return "clientAuth";
|
|
547
|
+
case x.codeSigning: return "codeSigning";
|
|
548
|
+
case x.emailProtection: return "emailProtection";
|
|
549
|
+
case x.timeStamping: return "timeStamping";
|
|
550
|
+
case x.ocspSigning: return "ocspSigning";
|
|
307
551
|
}
|
|
308
552
|
return {
|
|
309
553
|
type: "oid",
|
|
@@ -316,7 +560,7 @@ function parseExtendedKeyUsageOid(e) {
|
|
|
316
560
|
* @param method Well-known string or custom OID object.
|
|
317
561
|
*/
|
|
318
562
|
function getAuthorityInfoAccessMethodOid(e) {
|
|
319
|
-
return typeof e == "string" ?
|
|
563
|
+
return typeof e == "string" ? J[e] : validateOid(e.value);
|
|
320
564
|
}
|
|
321
565
|
/**
|
|
322
566
|
* Map a dotted-decimal OID to an {@linkcode AuthorityInfoAccessMethod} value.
|
|
@@ -325,8 +569,8 @@ function getAuthorityInfoAccessMethodOid(e) {
|
|
|
325
569
|
*/
|
|
326
570
|
function parseAuthorityInfoAccessMethodOid(e) {
|
|
327
571
|
switch (e) {
|
|
328
|
-
case
|
|
329
|
-
case
|
|
572
|
+
case x.ocspAccessMethod: return "ocsp";
|
|
573
|
+
case x.caIssuersAccessMethod: return "caIssuers";
|
|
330
574
|
}
|
|
331
575
|
return {
|
|
332
576
|
type: "oid",
|
|
@@ -335,28 +579,51 @@ function parseAuthorityInfoAccessMethodOid(e) {
|
|
|
335
579
|
}
|
|
336
580
|
/** Parse a dotted-decimal/colon-hex IP string to raw address bytes. */
|
|
337
581
|
function encodeIpAddress(e) {
|
|
338
|
-
return
|
|
582
|
+
return A(e);
|
|
339
583
|
}
|
|
340
|
-
/**
|
|
584
|
+
/**
|
|
585
|
+
* Computes a key identifier from a DER `SubjectPublicKeyInfo` using method (1)
|
|
586
|
+
* of RFC 5280 §4.2.1.2: "the 160-bit SHA-1 hash of the value of the BIT STRING
|
|
587
|
+
* subjectPublicKey (excluding the tag, length, and number of unused bits)".
|
|
588
|
+
* A parsed certificate carries the input as `subjectPublicKeyInfoDer`.
|
|
589
|
+
*/
|
|
341
590
|
function buildSubjectKeyIdentifier(e) {
|
|
342
|
-
let t =
|
|
591
|
+
let t = f(e)[1];
|
|
343
592
|
if (t === void 0 || t.tag !== 3) throw Error("SPKI missing subject public key bit string");
|
|
344
|
-
return
|
|
593
|
+
return S(t.value.slice(1));
|
|
345
594
|
}
|
|
346
|
-
/**
|
|
595
|
+
/**
|
|
596
|
+
* Throw if the string is not an encodable dotted-decimal OID, and return its
|
|
597
|
+
* canonical spelling.
|
|
598
|
+
*
|
|
599
|
+
* Syntax alone is not enough. X.660 bounds the first arc to 0, 1, or 2 and the
|
|
600
|
+
* second to under 40 beneath arcs 0 and 1, so `3.1` and `1.40` parse as decimals
|
|
601
|
+
* yet cannot be encoded.
|
|
602
|
+
*/
|
|
347
603
|
function validateOid(e) {
|
|
348
|
-
|
|
604
|
+
/^\d+(?:\.\d+)+$/.test(e) || w("invalid_oid", `Invalid OID: ${e}`);
|
|
605
|
+
try {
|
|
606
|
+
return g(e);
|
|
607
|
+
} catch {
|
|
608
|
+
w("invalid_oid", `Invalid OID: ${e}`);
|
|
609
|
+
}
|
|
349
610
|
}
|
|
350
|
-
/** Validate
|
|
611
|
+
/** Validate a policy OID and return its canonical spelling. */
|
|
351
612
|
function validatePolicyOid(e) {
|
|
352
|
-
validateOid(e);
|
|
613
|
+
return validateOid(e);
|
|
353
614
|
}
|
|
354
|
-
/**
|
|
615
|
+
/**
|
|
616
|
+
* Encode and push an extension, rejecting duplicate OIDs.
|
|
617
|
+
*
|
|
618
|
+
* Duplicate identity is the canonical OID, because two spellings that differ only
|
|
619
|
+
* by leading zeros in an arc encode to the same wire bytes. The diagnostic quotes
|
|
620
|
+
* the OID as submitted, which is the string the caller can find in their input.
|
|
621
|
+
*/
|
|
355
622
|
function pushExtension(e, t, n, r, i = !1) {
|
|
356
|
-
|
|
357
|
-
t.
|
|
623
|
+
let a = validateOid(n);
|
|
624
|
+
t.has(a) && w("duplicate_extension_oid", `Duplicate extension OID: ${n}`), t.add(a), e.push(encodeExtension(n, r, i));
|
|
358
625
|
}
|
|
359
626
|
//#endregion
|
|
360
|
-
export { buildCertificateExtensions, buildRequestedExtensions, buildSubjectKeyIdentifier, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid };
|
|
627
|
+
export { D as allOnesMaskForIpAddress, assertAuthorityInfoAccessProfile, assertCrlDistributionPointsProfile, assertExtensionCriticality, assertNameConstraintsProfile, buildCertificateExtensions, buildRequestedExtensions, buildSubjectKeyIdentifier, O as decodeIpAddress, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, k as normalizeIpAddress, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid, A as parseIpAddressToBytes };
|
|
361
628
|
|
|
362
629
|
//# sourceMappingURL=extensions.js.map
|