micro509 0.13.0 → 0.15.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/CHANGELOG.md +2064 -0
- package/README.md +111 -183
- package/dist/crypto/crypto.d.ts +104 -0
- package/dist/crypto/crypto.js +105 -0
- package/dist/crypto/crypto.js.map +1 -0
- package/dist/crypto.d.ts +4 -0
- package/dist/crypto.js +1 -0
- package/dist/der/der.d.ts +41 -33
- package/dist/der/der.js +26 -24
- package/dist/der/der.js.map +1 -1
- package/dist/index.d.ts +18 -14
- package/dist/index.js +19 -17
- package/dist/internal/asn1/asn1.d.ts +3 -6
- package/dist/internal/asn1/asn1.js +355 -44
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/ber.js +146 -0
- package/dist/internal/asn1/ber.js.map +1 -0
- package/dist/internal/asn1/decode-refusal.js +29 -0
- package/dist/internal/asn1/decode-refusal.js.map +1 -0
- package/dist/internal/asn1/der.d.ts +42 -43
- package/dist/internal/asn1/der.js +148 -56
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +90 -54
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids2.js +1 -2
- package/dist/internal/asn1/oids2.js.map +1 -1
- package/dist/internal/crypto/algorithm-names.js +5 -2
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/ecdsa.js +13 -8
- package/dist/internal/crypto/ecdsa.js.map +1 -1
- package/dist/internal/crypto/hash.js +1 -2
- package/dist/internal/crypto/hash.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +51 -0
- package/dist/internal/crypto/pbes2.js +86 -34
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/rsa-pss.js +48 -44
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +28 -0
- package/dist/internal/crypto/sig-verify.js +111 -62
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/crypto/signing.d.ts +1 -4
- package/dist/internal/crypto/signing.js +28 -29
- package/dist/internal/crypto/signing.js.map +1 -1
- package/dist/internal/crypto/webcrypto.js +1 -2
- package/dist/internal/crypto/webcrypto.js.map +1 -1
- package/dist/internal/shared/base64.js +8 -3
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +13 -0
- package/dist/internal/shared/dn.js +101 -48
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/idna-tables.js +7217 -0
- package/dist/internal/shared/idna-tables.js.map +1 -0
- package/dist/internal/shared/idna.js +296 -0
- package/dist/internal/shared/idna.js.map +1 -0
- package/dist/internal/shared/ip.d.ts +66 -0
- package/dist/internal/shared/ip.js +19 -3
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/shared/mailbox.js +26 -0
- package/dist/internal/shared/mailbox.js.map +1 -0
- package/dist/internal/shared/punycode.js +101 -0
- package/dist/internal/shared/punycode.js.map +1 -0
- package/dist/internal/shared/rfc3454-tables.js +1 -2
- package/dist/internal/shared/rfc3454-tables.js.map +1 -1
- package/dist/internal/shared/rfc4518-tables.js +119 -0
- package/dist/internal/shared/rfc4518-tables.js.map +1 -0
- package/dist/internal/shared/uri-host.js +102 -0
- package/dist/internal/shared/uri-host.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +246 -131
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +6 -21
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/verify-path.d.ts +4 -0
- package/dist/internal/verify/verify-path.js +264 -128
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +23 -0
- package/dist/internal/x509/directory-name.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +15 -13
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +3 -0
- package/dist/internal/x509/extension-errors.js +19 -0
- package/dist/internal/x509/extension-errors.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +157 -81
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-profile.js +129 -0
- package/dist/internal/x509/general-name-profile.js.map +1 -0
- package/dist/internal/x509/general-name-tags.js +27 -0
- package/dist/internal/x509/general-name-tags.js.map +1 -0
- package/dist/internal/x509/general-name.js +183 -0
- package/dist/internal/x509/general-name.js.map +1 -0
- package/dist/internal/x509/name-fields.js +21 -12
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/internal/x509/or-address.js +217 -0
- package/dist/internal/x509/or-address.js.map +1 -0
- package/dist/keys/keys.d.ts +124 -95
- package/dist/keys/keys.js +362 -214
- 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 +17 -19
- package/dist/pem/pem.js +122 -41
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.js +1 -2
- package/dist/pkcs/pfx.d.ts +57 -32
- package/dist/pkcs/pfx.js +209 -173
- package/dist/pkcs/pfx.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.d.ts +104 -27
- package/dist/pkcs/pkcs12-mac.js +266 -79
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +123 -53
- package/dist/pkcs/pkcs7.js +314 -238
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +5 -4
- package/dist/result/result.d.ts +43 -28
- package/dist/result/result.js +27 -14
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +2 -2
- package/dist/result.js +1 -2
- package/dist/revocation/chain.d.ts +101 -23
- package/dist/revocation/chain.js +456 -206
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +140 -65
- package/dist/revocation/crl.js +691 -391
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +145 -69
- package/dist/revocation/ocsp.js +347 -297
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +48 -36
- package/dist/revocation/revocation.js +112 -63
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +5 -5
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +18 -20
- package/dist/verify/identity.js +120 -90
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +1 -4
- package/dist/verify/policy.d.ts +15 -8
- package/dist/verify/verify.d.ts +86 -46
- package/dist/verify/verify.js +309 -143
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +3 -2
- package/dist/verify.js +4 -3
- package/dist/x509/certificate.d.ts +50 -28
- package/dist/x509/certificate.js +69 -36
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +9 -8
- package/dist/x509/csr.js +10 -11
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +207 -82
- package/dist/x509/extensions.js +561 -128
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +4 -7
- package/dist/x509/fingerprint.js +1 -2
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +7 -9
- package/dist/x509/name-text.js +20 -4
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +15 -14
- package/dist/x509/name.js +23 -19
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +165 -64
- package/dist/x509/parse.js +444 -459
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +8 -5
- package/dist/x509.js +10 -8
- package/package.json +90 -65
package/dist/x509/extensions.js
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
1
|
+
import { DECODE_REFUSAL_CODES as e, isResultError as t } from "../result/result.js";
|
|
2
|
+
import { rethrowDecodeRefusal as n } from "../internal/asn1/decode-refusal.js";
|
|
3
|
+
import { bmpString as r, bool as i, concatBytes as a, explicitContext as o, hasLoneSurrogate as s, ia5Bytes as ee, ia5String as te, implicitConstructedContext as c, implicitPrimitiveContext as l, integerFromNumber as u, objectIdentifier as d, octetString as ne, readElement as f, readRootElement as p, readSequenceChildren as m, sequence as h, tlv as g, utf8String as _, visibleString as v } from "../internal/asn1/der.js";
|
|
4
|
+
import { canonicalizeOid as y, checkStrictDer as re, childrenOf as b, decodeObjectIdentifier as ie, hexToBytes as x, toHex as S } from "../internal/asn1/asn1.js";
|
|
5
|
+
import { OIDS as C } from "../internal/asn1/oids2.js";
|
|
6
|
+
import { sha1 as w } from "../internal/crypto/hash.js";
|
|
7
|
+
import { throwExtensionEncoderError as T } from "../internal/x509/extension-errors.js";
|
|
8
|
+
import { encodeDistributionPointReasonFlagsContent as E, encodeKeyUsageExtension as D } from "../internal/x509/extension-bits.js";
|
|
9
|
+
import { domainToAscii as O } from "../internal/shared/idna.js";
|
|
10
|
+
import { allOnesMaskForIpAddress as k, decodeIpAddress as A, normalizeIpAddress as j, parseIpAddressToBytes as M } from "../internal/shared/ip.js";
|
|
11
|
+
import { isMailboxDomain as N, isSmtpUtf8LocalPart as P } from "../internal/shared/mailbox.js";
|
|
12
|
+
import { typedOtherName as F } from "../internal/x509/general-name.js";
|
|
13
|
+
import { checkEdiPartyName as I, checkOrAddress as L, normalizeLabelSeparators as R, parsePresentedSrvName as z, parseSrvNameRestriction as B, parseUriNameConstraint as V } from "../internal/x509/general-name-profile.js";
|
|
14
|
+
import { GENERAL_NAME_WIRE_TAGS as H } from "../internal/x509/general-name-tags.js";
|
|
15
|
+
import { encodeRelativeDistinguishedName as U } from "./name.js";
|
|
16
|
+
import { AUTHORITY_INFO_ACCESS_EXTENSION_DEFINITION as W, AUTHORITY_KEY_IDENTIFIER_EXTENSION_DEFINITION as G, BASIC_CONSTRAINTS_EXTENSION_DEFINITION as K, CERTIFICATE_POLICIES_EXTENSION_DEFINITION as q, CRL_DISTRIBUTION_POINTS_EXTENSION_DEFINITION as J, EXTENDED_KEY_USAGE_EXTENSION_DEFINITION as Y, INHIBIT_ANY_POLICY_EXTENSION_DEFINITION as ae, KEY_USAGE_EXTENSION_DEFINITION as X, NAME_CONSTRAINTS_EXTENSION_DEFINITION as oe, NO_REV_AVAIL_EXTENSION_DEFINITION as se, POLICY_CONSTRAINTS_EXTENSION_DEFINITION as ce, POLICY_MAPPINGS_EXTENSION_DEFINITION as le, SUBJECT_ALT_NAME_EXTENSION_DEFINITION as Z, SUBJECT_KEY_IDENTIFIER_EXTENSION_DEFINITION as ue, getExtensionDefinition as Q } from "../internal/x509/extension-registry.js";
|
|
17
|
+
// src/x509/extensions.ts
|
|
10
18
|
/**
|
|
11
19
|
* Typed certificate and CSR extension model plus ASN.1 encoder helpers.
|
|
12
20
|
*
|
|
@@ -16,34 +24,35 @@ import { AUTHORITY_INFO_ACCESS_EXTENSION_DEFINITION as S, AUTHORITY_KEY_IDENTIFI
|
|
|
16
24
|
* @module
|
|
17
25
|
*/
|
|
18
26
|
/** 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:
|
|
27
|
+
const de = {
|
|
28
|
+
serverAuth: C.serverAuth,
|
|
29
|
+
clientAuth: C.clientAuth,
|
|
30
|
+
codeSigning: C.codeSigning,
|
|
31
|
+
emailProtection: C.emailProtection,
|
|
32
|
+
timeStamping: C.timeStamping,
|
|
33
|
+
ocspSigning: C.ocspSigning
|
|
34
|
+
}, fe = {
|
|
35
|
+
ocsp: C.ocspAccessMethod,
|
|
36
|
+
caIssuers: C.caIssuersAccessMethod
|
|
29
37
|
};
|
|
30
38
|
/**
|
|
31
39
|
* Build the v3 extensions block for a certificate.
|
|
32
40
|
*
|
|
33
|
-
* Automatically adds SKI, AKI (when
|
|
41
|
+
* Automatically adds SKI, AKI (when a key identifier is given), and
|
|
34
42
|
* basicConstraints (defaults to `{ ca: false }`). Additional extensions
|
|
35
43
|
* come from the caller's {@linkcode CertificateExtensionsInput}.
|
|
36
44
|
*
|
|
37
45
|
* @param subjectPublicKeyInfo DER-encoded SPKI of the subject.
|
|
38
|
-
* @param
|
|
46
|
+
* @param authorityKeyIdentifier Key identifier for the AKI, or `undefined` to omit it.
|
|
39
47
|
* @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.
|
|
48
|
+
* @param subjectIsEmpty Whether the certificate subject DN is empty. When `true`, a
|
|
49
|
+
* subjectAltName extension is required and marked critical per RFC 5280 §4.2.1.6.
|
|
42
50
|
* @returns Array of DER-encoded Extension SEQUENCEs.
|
|
43
51
|
*/
|
|
44
52
|
function buildCertificateExtensions(e, t, n, r = !1) {
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
assertCustomExtensionsValid(n, "certificate"), r && assertEmptySubjectHasCriticalSubjectAltName(n), assertPathLengthKeyUsage(n), assertSafeCurveKeyUsage(e, n), assertNoRevAvailProfile(n);
|
|
54
|
+
let i = [], a = /* @__PURE__ */ new Set(), o = n?.basicConstraints ?? { ca: !1 };
|
|
55
|
+
return pushKnownExtension(i, a, K, o), pushKnownExtension(i, a, ue, buildSubjectKeyIdentifier(e)), t !== void 0 && pushKnownExtension(i, a, G, t), appendConfiguredExtensions(i, a, n, {
|
|
47
56
|
includeBasicConstraints: !1,
|
|
48
57
|
subjectIsEmpty: r
|
|
49
58
|
}), i;
|
|
@@ -57,36 +66,215 @@ function buildCertificateExtensions(e, t, n, r = !1) {
|
|
|
57
66
|
* @returns Array of DER-encoded Extension SEQUENCEs.
|
|
58
67
|
*/
|
|
59
68
|
function buildRequestedExtensions(e) {
|
|
69
|
+
assertCustomExtensionsValid(e, "csr");
|
|
60
70
|
let t = [];
|
|
61
|
-
return appendConfiguredExtensions(t, /* @__PURE__ */ new Set(), e,
|
|
71
|
+
return appendConfiguredExtensions(t, /* @__PURE__ */ new Set(), e, { includeBasicConstraints: !0 }), t;
|
|
62
72
|
}
|
|
63
73
|
/** 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,
|
|
74
|
+
function appendConfiguredExtensions(e, t, n, r) {
|
|
75
|
+
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));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* RFC 5280 §4.2.1.9: pathLenConstraint requires the keyUsage extension to assert keyCertSign.
|
|
79
|
+
* Absent, empty, or keyCertSign-less keyUsage is rejected. Known extensions supplied through
|
|
80
|
+
* customExtensions participate in the effective view.
|
|
81
|
+
*/
|
|
82
|
+
function assertPathLengthKeyUsage(e) {
|
|
83
|
+
if (resolveEffectiveBasicConstraints(e)?.pathLength === void 0) return;
|
|
84
|
+
let t = resolveEffectiveKeyUsage(e);
|
|
85
|
+
t !== void 0 && t.length !== 0 && t.includes("keyCertSign") || T("path_length_requires_key_cert_sign", "basicConstraints pathLength requires the keyUsage keyCertSign bit");
|
|
86
|
+
}
|
|
87
|
+
/** RFC 9295 §3: an end-entity id-Ed25519 or id-Ed448 certificate needs one of these. */
|
|
88
|
+
const $ = [
|
|
89
|
+
"nonRepudiation",
|
|
90
|
+
"digitalSignature",
|
|
91
|
+
"cRLSign"
|
|
92
|
+
], pe = [
|
|
93
|
+
"keyEncipherment",
|
|
94
|
+
"dataEncipherment",
|
|
95
|
+
"keyAgreement",
|
|
96
|
+
"encipherOnly",
|
|
97
|
+
"decipherOnly"
|
|
98
|
+
], me = [
|
|
99
|
+
"digitalSignature",
|
|
100
|
+
"nonRepudiation",
|
|
101
|
+
"keyEncipherment",
|
|
102
|
+
"dataEncipherment",
|
|
103
|
+
"keyCertSign",
|
|
104
|
+
"cRLSign"
|
|
105
|
+
];
|
|
106
|
+
/**
|
|
107
|
+
* RFC 9295 §3, which replaces RFC 8410 §5, constrains a keyUsage extension on a
|
|
108
|
+
* certificate whose subject key names one of the four 1.3.101 curves.
|
|
109
|
+
*
|
|
110
|
+
* id-X25519 and id-X448 require `keyAgreement`, admit one of `encipherOnly` and
|
|
111
|
+
* `decipherOnly`, and forbid the rest.
|
|
112
|
+
*
|
|
113
|
+
* id-Ed25519 and id-Ed448 split on whether the certificate is a certification
|
|
114
|
+
* authority. A CA MUST assert `keyCertSign` and MAY add `nonRepudiation`,
|
|
115
|
+
* `digitalSignature`, or `cRLSign`. Any other certificate MUST assert at least
|
|
116
|
+
* one of those three and MUST NOT assert `keyCertSign`. Both forbid the key
|
|
117
|
+
* agreement and encipherment bits. A CRL issuer is the case where `cRLSign` is
|
|
118
|
+
* the bit chosen, and a CRL issuer that is also a CA is the CA case, so the two
|
|
119
|
+
* branches cover the section's four paragraphs.
|
|
120
|
+
*
|
|
121
|
+
* The rule binds only a keyUsage that reaches the wire, so an absent one, and an
|
|
122
|
+
* empty one the builder therefore omits, leave the key unconstrained.
|
|
123
|
+
*/
|
|
124
|
+
function assertSafeCurveKeyUsage(e, t) {
|
|
125
|
+
let n = resolveEffectiveKeyUsage(t);
|
|
126
|
+
if (n === void 0 || n.length === 0) return;
|
|
127
|
+
let r = readSubjectKeyAlgorithmOid(e);
|
|
128
|
+
r !== void 0 && (oidEquals(r, C.x25519) || oidEquals(r, C.x448) ? assertMontgomeryKeyUsage(n) : (oidEquals(r, C.ed25519) || oidEquals(r, C.ed448)) && assertEdwardsKeyUsage(n, resolveEffectiveBasicConstraints(t)?.ca === !0));
|
|
129
|
+
}
|
|
130
|
+
/** The X25519 and X448 half of RFC 9295 §3. */
|
|
131
|
+
function assertMontgomeryKeyUsage(e) {
|
|
132
|
+
e.includes("keyAgreement") || T("montgomery_key_usage_requires_key_agreement", "keyUsage on an X25519 or X448 certificate must set keyAgreement");
|
|
133
|
+
let t = me.filter((t) => e.includes(t));
|
|
134
|
+
t.length > 0 && T("montgomery_key_usage_forbids_signature_bit", `keyUsage on an X25519 or X448 certificate must not set ${t.join(", ")}`), e.includes("encipherOnly") && e.includes("decipherOnly") && T("montgomery_key_usage_forbids_both_cipher_bits", "keyUsage on an X25519 or X448 certificate must not set both encipherOnly and decipherOnly");
|
|
135
|
+
}
|
|
136
|
+
/** The Ed25519 and Ed448 half of RFC 9295 §3. */
|
|
137
|
+
function assertEdwardsKeyUsage(e, t) {
|
|
138
|
+
t ? e.includes("keyCertSign") || T("edwards_key_usage_requires_key_cert_sign", "keyUsage on an Ed25519 or Ed448 certification authority certificate must set keyCertSign") : $.some((t) => e.includes(t)) || T("edwards_key_usage_requires_signing_bit", `keyUsage on an Ed25519 or Ed448 certificate must set one of ${$.join(", ")}`);
|
|
139
|
+
let n = pe.filter((t) => e.includes(t));
|
|
140
|
+
n.length > 0 && T("edwards_key_usage_forbids_agreement_bit", `keyUsage on an Ed25519 or Ed448 certificate must not set ${n.join(", ")}`), !t && e.includes("keyCertSign") && T("edwards_key_usage_forbids_key_cert_sign", "keyUsage on an Ed25519 or Ed448 end-entity certificate must not set keyCertSign");
|
|
141
|
+
}
|
|
142
|
+
/** The algorithm OID of a SubjectPublicKeyInfo, or `undefined` when the field is not an OID. */
|
|
143
|
+
function readSubjectKeyAlgorithmOid(e) {
|
|
144
|
+
let t = m(e)[0];
|
|
145
|
+
if (t === void 0 || t.tag !== 48) return;
|
|
146
|
+
let n = b(e, t)[0];
|
|
147
|
+
return n?.tag === 6 ? ie(n.value) : void 0;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* RFC 5280 §4.2.1.9: basicConstraints is critical in a CA certificate whose key
|
|
151
|
+
* validates signatures on certificates. Both conjuncts live in other extensions,
|
|
152
|
+
* so the rule sits here rather than in the basicConstraints profile hook.
|
|
153
|
+
*
|
|
154
|
+
* §4.2.1.3 restricts a key only through a keyUsage extension that reaches the
|
|
155
|
+
* wire, so an absent keyUsage, and an empty one the builder therefore omits,
|
|
156
|
+
* leave the key free to validate certificate signatures. Only an emitted keyUsage
|
|
157
|
+
* without keyCertSign takes the certificate out of the clause's scope.
|
|
158
|
+
*
|
|
159
|
+
* Only a custom extension can carry the wrong criticality; the typed field is
|
|
160
|
+
* always emitted with the registry default.
|
|
161
|
+
*/
|
|
162
|
+
function assertCaBasicConstraintsCritical(e) {
|
|
163
|
+
let t = e?.customExtensions?.find((e) => oidEquals(e.oid, C.basicConstraints));
|
|
164
|
+
if (t === void 0 || t.critical === !0 || !K.decode(new Uint8Array(t.value)).ca) return;
|
|
165
|
+
let n = resolveEffectiveKeyUsage(e);
|
|
166
|
+
(n === void 0 || n.length === 0 || n.includes("keyCertSign")) && assertExtensionCriticality("basicConstraints", !0, !1);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* RFC 9608 §2 and §3: a certificate carrying noRevAvail is not a CA certificate
|
|
170
|
+
* and carries no cRLDistributionPoints, no freshestCRL, and no id-ad-ocsp
|
|
171
|
+
* authorityInfoAccess entry. Known extensions supplied through customExtensions
|
|
172
|
+
* participate in the effective view.
|
|
173
|
+
*/
|
|
174
|
+
function assertNoRevAvailProfile(e) {
|
|
175
|
+
(e?.noRevAvail === !0 || findCustomExtensionValue(e, C.noRevAvail) !== void 0) && (resolveEffectiveBasicConstraints(e)?.ca === !0 && throwNoRevAvailConflict("basicConstraints with cA TRUE"), ((e?.crlDistributionPoints?.length ?? 0) > 0 || findCustomExtensionValue(e, C.cRLDistributionPoints) !== void 0) && throwNoRevAvailConflict("cRLDistributionPoints"), findCustomExtensionValue(e, C.freshestCRL) !== void 0 && throwNoRevAvailConflict("freshestCRL"), resolveEffectiveAuthorityInfoAccess(e).some((e) => getAuthorityInfoAccessMethodOid(e.method) === C.ocspAccessMethod) && throwNoRevAvailConflict("an id-ad-ocsp authorityInfoAccess entry"));
|
|
176
|
+
}
|
|
177
|
+
/** The authorityInfoAccess entries the builder will emit: the typed list when non-empty, otherwise a custom-known extension. */
|
|
178
|
+
function resolveEffectiveAuthorityInfoAccess(e) {
|
|
179
|
+
if (e?.authorityInfoAccess !== void 0 && e.authorityInfoAccess.length > 0) return e.authorityInfoAccess;
|
|
180
|
+
let t = findCustomExtensionValue(e, C.authorityInfoAccess);
|
|
181
|
+
return t === void 0 ? [] : W.decode(t);
|
|
182
|
+
}
|
|
183
|
+
function throwNoRevAvailConflict(e) {
|
|
184
|
+
T("no_rev_avail_conflict", `A certificate carrying noRevAvail must not also carry ${e}`);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* RFC 5280 §4.2.1.6: an empty subject DN requires a subjectAltName extension
|
|
188
|
+
* present, marked critical, and carrying at least one non-empty GeneralName.
|
|
189
|
+
*/
|
|
190
|
+
function assertEmptySubjectHasCriticalSubjectAltName(e) {
|
|
191
|
+
e?.subjectAltNames?.some(subjectAltNameHasIdentity) !== !0 && e?.customExtensions?.some((e) => oidEquals(e.oid, C.subjectAltName) && e.critical === !0 && customSubjectAltNameHasIdentity(e.value)) !== !0 && T("empty_subject_requires_subject_alt_name", "An empty subject requires a critical subjectAltName extension");
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Canonical OID equality: matches even when arcs carry redundant leading zeros.
|
|
195
|
+
*
|
|
196
|
+
* Both sides must be encodable. Callers run after `assertCustomExtensionsValid`,
|
|
197
|
+
* which rejects an OID that cannot be canonicalized.
|
|
198
|
+
*/
|
|
199
|
+
function oidEquals(e, t) {
|
|
200
|
+
return e === t || y(e) === y(t);
|
|
201
|
+
}
|
|
202
|
+
/** First customExtensions value whose OID canonically matches `oid`. */
|
|
203
|
+
function findCustomExtensionValue(e, t) {
|
|
204
|
+
return e?.customExtensions?.find((e) => oidEquals(e.oid, t))?.value;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Rejects a custom extension carrying a known OID whose payload is not the DER
|
|
208
|
+
* that OID's schema defines, and a known extension offered in the wrong context.
|
|
209
|
+
*
|
|
210
|
+
* Runs before every cross-field guard so the effective-value resolvers below can
|
|
211
|
+
* decode a custom payload without having to tolerate failure.
|
|
212
|
+
*/
|
|
213
|
+
function assertCustomExtensionsValid(e, t) {
|
|
214
|
+
for (let n of e?.customExtensions ?? []) {
|
|
215
|
+
let e = validateOid(n.oid), r = Q(e);
|
|
216
|
+
r !== void 0 && (r.contexts.includes(t) || T("extension_not_supported_in_context", `Extension ${n.oid} is not supported in ${t} context`), assertKnownExtensionPayload(r, new Uint8Array(n.value), n.critical ?? !1, n.oid));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Decode a custom payload once through its known OID's definition and apply that
|
|
221
|
+
* extension's profile rules to the decoded value and its criticality.
|
|
222
|
+
*
|
|
223
|
+
* A profile violation already carries its own code and propagates unchanged; a
|
|
224
|
+
* decode failure means the payload is not the DER the OID's schema defines.
|
|
225
|
+
*/
|
|
226
|
+
function assertKnownExtensionPayload(e, n, r, i) {
|
|
227
|
+
try {
|
|
228
|
+
e.assertDerProfile(n, r);
|
|
229
|
+
} catch (n) {
|
|
230
|
+
if (t(n)) throw n;
|
|
231
|
+
T("malformed_known_extension_value", `Custom extension ${i} does not decode as ${e.oid}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** Effective basicConstraints across the typed field and any custom-known extension. */
|
|
235
|
+
function resolveEffectiveBasicConstraints(e) {
|
|
236
|
+
if (e?.basicConstraints !== void 0) return e.basicConstraints;
|
|
237
|
+
let t = findCustomExtensionValue(e, C.basicConstraints);
|
|
238
|
+
return t === void 0 ? void 0 : K.decode(t);
|
|
239
|
+
}
|
|
240
|
+
/** Effective keyUsage flags across the typed field and any custom-known extension. */
|
|
241
|
+
function resolveEffectiveKeyUsage(e) {
|
|
242
|
+
if (e?.keyUsage !== void 0) return e.keyUsage;
|
|
243
|
+
let t = findCustomExtensionValue(e, C.keyUsage);
|
|
244
|
+
return t === void 0 ? void 0 : X.decode(t).flags;
|
|
245
|
+
}
|
|
246
|
+
/** Whether a typed GeneralName carries a non-empty identity value. */
|
|
247
|
+
function subjectAltNameHasIdentity(e) {
|
|
248
|
+
switch (e.type) {
|
|
249
|
+
case "directoryName": return e.derHex.length > 0;
|
|
250
|
+
case "krb5PrincipalName": return e.realm.length > 0 || e.nameString.length > 0;
|
|
251
|
+
default: return e.value.length > 0;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Whether a custom subjectAltName value decodes to at least one non-empty GeneralName. */
|
|
255
|
+
function customSubjectAltNameHasIdentity(e) {
|
|
256
|
+
return Z.decode(e).some(subjectAltNameHasIdentity);
|
|
66
257
|
}
|
|
67
258
|
function appendConstraintExtensions(e, t, n, r) {
|
|
68
|
-
r && n.basicConstraints !== void 0 && pushKnownExtension(e, t,
|
|
259
|
+
assertPathLengthKeyUsage(n), assertCaBasicConstraintsCritical(n), r && n.basicConstraints !== void 0 && pushKnownExtension(e, t, K, n.basicConstraints), n.keyUsage !== void 0 && n.keyUsage.length > 0 && pushKnownExtension(e, t, X, n.keyUsage), n.nameConstraints !== void 0 && pushKnownExtension(e, t, oe, n.nameConstraints);
|
|
69
260
|
}
|
|
70
261
|
function appendIdentityExtensions(e, t, n, r) {
|
|
71
262
|
if (n.subjectAltNames !== void 0 && n.subjectAltNames.length > 0) {
|
|
72
263
|
let i = r ? !0 : void 0;
|
|
73
|
-
pushKnownExtension(e, t,
|
|
264
|
+
pushKnownExtension(e, t, Z, n.subjectAltNames, i);
|
|
74
265
|
}
|
|
75
|
-
n.extendedKeyUsage !== void 0 && n.extendedKeyUsage.length > 0 && pushKnownExtension(e, t,
|
|
266
|
+
n.extendedKeyUsage !== void 0 && n.extendedKeyUsage.length > 0 && pushKnownExtension(e, t, Y, n.extendedKeyUsage);
|
|
76
267
|
}
|
|
77
268
|
/** Appends configured certificate-policy extensions while enforcing unique OIDs. */
|
|
78
269
|
function appendPolicyExtensions(e, t, n) {
|
|
79
|
-
n.certificatePolicies !== void 0 && n.certificatePolicies.length > 0 && pushKnownExtension(e, t,
|
|
270
|
+
n.certificatePolicies !== void 0 && n.certificatePolicies.length > 0 && pushKnownExtension(e, t, q, n.certificatePolicies), n.policyMappings !== void 0 && n.policyMappings.length > 0 && pushKnownExtension(e, t, le, n.policyMappings), n.policyConstraints !== void 0 && pushKnownExtension(e, t, ce, n.policyConstraints), n.inhibitAnyPolicy !== void 0 && pushKnownExtension(e, t, ae, n.inhibitAnyPolicy);
|
|
80
271
|
}
|
|
81
272
|
function appendAccessExtensions(e, t, n) {
|
|
82
|
-
n.authorityInfoAccess !== void 0 && n.authorityInfoAccess.length > 0 && pushKnownExtension(e, t,
|
|
273
|
+
n.authorityInfoAccess !== void 0 && n.authorityInfoAccess.length > 0 && pushKnownExtension(e, t, W, n.authorityInfoAccess), n.crlDistributionPoints !== void 0 && n.crlDistributionPoints.length > 0 && pushKnownExtension(e, t, J, n.crlDistributionPoints), n.noRevAvail === !0 && pushKnownExtension(e, t, se, !0);
|
|
83
274
|
}
|
|
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
|
-
}
|
|
275
|
+
/** Push each custom extension. Context and payload were checked by `assertCustomExtensionsValid`. */
|
|
276
|
+
function appendCustomExtensions(e, t, n) {
|
|
277
|
+
for (let r of n.customExtensions ?? []) pushExtension(e, t, r.oid, new Uint8Array(r.value), r.critical ?? !1);
|
|
90
278
|
}
|
|
91
279
|
/** Encode a known extension via its registry definition and push it. */
|
|
92
280
|
function pushKnownExtension(e, t, n, r, i = n.defaultCritical) {
|
|
@@ -99,9 +287,10 @@ function pushKnownExtension(e, t, n, r, i = n.defaultCritical) {
|
|
|
99
287
|
* @param extnValue DER-encoded extension payload.
|
|
100
288
|
* @param critical Whether to mark the extension as critical. Default `false`.
|
|
101
289
|
*/
|
|
102
|
-
function encodeExtension(t, n
|
|
103
|
-
|
|
104
|
-
|
|
290
|
+
function encodeExtension(e, t, n = !1) {
|
|
291
|
+
validateOid(e);
|
|
292
|
+
let r = [d(e)];
|
|
293
|
+
return n && r.push(i(!0)), r.push(ne(t)), h(r);
|
|
105
294
|
}
|
|
106
295
|
/**
|
|
107
296
|
* DER-encode a {@linkcode BasicConstraints} value.
|
|
@@ -109,13 +298,9 @@ function encodeExtension(t, n, r = !1) {
|
|
|
109
298
|
* @param input CA flag and optional pathLength.
|
|
110
299
|
* @returns DER SEQUENCE suitable for wrapping in an Extension OCTET STRING.
|
|
111
300
|
*/
|
|
112
|
-
function encodeBasicConstraints(
|
|
113
|
-
let
|
|
114
|
-
|
|
115
|
-
if (!t.ca) throw Error("pathLength requires ca=true");
|
|
116
|
-
n.push(o(t.pathLength));
|
|
117
|
-
}
|
|
118
|
-
return f(n);
|
|
301
|
+
function encodeBasicConstraints(e) {
|
|
302
|
+
let t = [];
|
|
303
|
+
return e.ca && t.push(i(!0)), e.pathLength !== void 0 && (e.ca || T("path_length_requires_ca", "basicConstraints pathLength requires ca = true"), t.push(u(e.pathLength))), h(t);
|
|
119
304
|
}
|
|
120
305
|
/**
|
|
121
306
|
* DER-encode a Key Usage BIT STRING from an array of {@linkcode KeyUsage} flags.
|
|
@@ -123,7 +308,106 @@ function encodeBasicConstraints(t) {
|
|
|
123
308
|
* @param usages Flags to set in the bit string.
|
|
124
309
|
*/
|
|
125
310
|
function encodeKeyUsage(e) {
|
|
126
|
-
return
|
|
311
|
+
return D(e);
|
|
312
|
+
}
|
|
313
|
+
function encodeIa5Content(e) {
|
|
314
|
+
try {
|
|
315
|
+
return ee(e);
|
|
316
|
+
} catch {
|
|
317
|
+
T("invalid_ia5_string", "Invalid IA5String: contains non-ASCII characters");
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/** RFC 5280 §4.2.1.6: reject an empty GeneralName string value (empty dNSName, rfc822Name, URI, or SRV name). */
|
|
321
|
+
function requireNonEmptyName(e) {
|
|
322
|
+
return e.length === 0 && T("empty_general_name_value", "GeneralName value must not be empty"), e;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* RFC 9598 §3: a SmtpUTF8Mailbox carries no Byte Order Mark, is used only when
|
|
326
|
+
* the Local-part holds a non-ASCII character, and stores its domain as
|
|
327
|
+
* lowercase A-labels and NR-LDH labels.
|
|
328
|
+
*/
|
|
329
|
+
function assertSmtpUtf8Mailbox(e) {
|
|
330
|
+
let t = requireNonEmptyName(e).lastIndexOf("@"), n = t > 0 ? e.slice(0, t) : "", r = toAsciiDomain(e.slice(t + 1));
|
|
331
|
+
return (n.length === 0 || e.includes("") || !N(r, "registration")) && T("invalid_smtp_utf8_mailbox", "SmtpUTF8Mailbox must be Local-part@Domain with no Byte Order Mark and a domain of lowercase A-labels and NR-LDH labels"), [...n].some((e) => (e.codePointAt(0) ?? 0) > 127) || T("smtp_utf8_mailbox_ascii_local_part", "A mailbox with an ASCII Local-part must use rfc822Name (type email)"), P(n) || T("invalid_smtp_utf8_mailbox", "SmtpUTF8Mailbox Local-part must be an RFC 6531 Dot-string or Quoted-string"), `${n}@${r}`;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* @internal A decoded SAN or IAN entry of a custom extension, which is emitted
|
|
335
|
+
* as given: the typed builder's rules, with a SmtpUTF8Mailbox domain already
|
|
336
|
+
* stored in A-labels.
|
|
337
|
+
*/
|
|
338
|
+
function assertStoredSubjectAltName(e) {
|
|
339
|
+
encodeSubjectAltName(e), e.type === "smtpUtf8Mailbox" && assertSmtpUtf8Mailbox(e.value) !== e.value && T("invalid_smtp_utf8_mailbox", "SmtpUTF8Mailbox domain must be stored as lowercase A-labels and NR-LDH labels");
|
|
340
|
+
}
|
|
341
|
+
/** RFC 5891 §4: the domain with each U-label converted to its A-label and each A-label checked. */
|
|
342
|
+
function toAsciiDomain(e) {
|
|
343
|
+
let t = O(e, "registration");
|
|
344
|
+
return t.ok ? t.value : T("invalid_idn", `Domain name is not valid IDNA2008 (${t.reason})`);
|
|
345
|
+
}
|
|
346
|
+
/** RFC 5280 §4.2.1.6 dNSName and rfc822Name domains take the RFC 1034 §3.5 preferred name syntax, which has no trailing root dot. */
|
|
347
|
+
function requireRelativeDomain(e) {
|
|
348
|
+
return e.endsWith(".") && T("domain_trailing_dot", "A dNSName or rfc822Name domain must not end in \".\" (RFC 5280 §4.2.1.6)"), e;
|
|
349
|
+
}
|
|
350
|
+
/** A dNSName or dNSName constraint with its leading `*.` or `.` kept and its domain in A-labels. */
|
|
351
|
+
function toAsciiDnsName(e) {
|
|
352
|
+
let t = e.startsWith("*.") ? "*." : e.startsWith(".") ? "." : "";
|
|
353
|
+
return `${t}${toAsciiDomain(requireRelativeDomain(e.slice(t.length)))}`;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* RFC 4985 §3: a SRVName or SRVName restriction with its label separators
|
|
357
|
+
* normalized, its `_Service` label kept, and its Name in A-labels.
|
|
358
|
+
*/
|
|
359
|
+
function toAsciiSrvComponents(e) {
|
|
360
|
+
let t = R(e), n = t.indexOf(".");
|
|
361
|
+
if (!t.startsWith("_")) return toAsciiDomain(t);
|
|
362
|
+
if (n < 0) return t;
|
|
363
|
+
let r = t.slice(n + 1);
|
|
364
|
+
return `${t.slice(0, n + 1)}${r.length === 0 ? "" : toAsciiDomain(r)}`;
|
|
365
|
+
}
|
|
366
|
+
/** RFC 4985 §2 and RFC 6335 §5.1: a presented SRVName is `_Service.Name`. */
|
|
367
|
+
function toAsciiSrvName(e) {
|
|
368
|
+
let t = toAsciiSrvComponents(e);
|
|
369
|
+
return z(t) === void 0 && T("invalid_srv_name", "A SRVName is _Service.Name with an RFC 6335 service and an LDH Name (RFC 4985 §2)"), t;
|
|
370
|
+
}
|
|
371
|
+
/** RFC 4985 §4: a SRVName constraint is `_Service.Name`, `_Service`, or `Name`. */
|
|
372
|
+
function toAsciiSrvNameConstraint(e) {
|
|
373
|
+
let t = toAsciiSrvComponents(e);
|
|
374
|
+
return B(t) === void 0 && T("invalid_srv_name_constraint", "A SRVName constraint is _Service.Name, _Service, or Name (RFC 4985 §4)"), t;
|
|
375
|
+
}
|
|
376
|
+
/** RFC 4120 §5.2.1 KerberosString content: IA5String characters in a GeneralString, without code-extension controls. */
|
|
377
|
+
function kerberosString(e, t) {
|
|
378
|
+
let outside = (e) => e > 127 || e === 14 || e === 15 || e === 27;
|
|
379
|
+
return [...e].some((e) => outside(e.codePointAt(0) ?? 0)) && T("invalid_krb5_principal_name", `KRB5PrincipalName ${t} must hold IA5String characters other than SO, SI and ESC (RFC 4120 §5.2.1)`), g(27, new TextEncoder().encode(e));
|
|
380
|
+
}
|
|
381
|
+
/** RFC 4120 §5.2.4 Int32 as a DER-minimal two's-complement INTEGER. */
|
|
382
|
+
function int32(e) {
|
|
383
|
+
(!Number.isInteger(e) || e < -2147483648 || e > 2147483647) && T("invalid_krb5_principal_name", "KRB5PrincipalName nameType must be an Int32 (RFC 4120 §5.2.4)");
|
|
384
|
+
let t = [], n = e;
|
|
385
|
+
do
|
|
386
|
+
t.unshift(n & 255), n >>= 8;
|
|
387
|
+
while (!(n === 0 && (t[0] ?? 0) < 128) && !(n === -1 && (t[0] ?? 0) >= 128));
|
|
388
|
+
return g(2, Uint8Array.from(t));
|
|
389
|
+
}
|
|
390
|
+
/** RFC 4556 §3.2.2 KRB5PrincipalName in the EXPLICIT tagging of RFC 4120's module. */
|
|
391
|
+
function encodeKrb5PrincipalName(e) {
|
|
392
|
+
return e.realm.includes("\0") && T("invalid_krb5_principal_name", "KRB5PrincipalName realm must not contain NUL (RFC 4120 §5.2.2)"), h([o(0, kerberosString(e.realm, "realm")), o(1, h([o(0, int32(e.nameType)), o(1, h(e.nameString.map((e) => kerberosString(e, "nameString"))))]))]);
|
|
393
|
+
}
|
|
394
|
+
/** An otherName type-id that has no dedicated {@linkcode SubjectAltName} variant. */
|
|
395
|
+
function validateOtherNameTypeId(e) {
|
|
396
|
+
let t = validateOid(e);
|
|
397
|
+
return (t === C.idOnDnsSrv || t === C.idOnSmtpUtf8Mailbox) && T("other_name_type_id_has_variant", `otherName type-id ${t} is encoded through its dedicated SubjectAltName variant`), t;
|
|
398
|
+
}
|
|
399
|
+
function requireGeneralNameContent(e, t, n) {
|
|
400
|
+
let r = n(e);
|
|
401
|
+
return r.ok || T("invalid_general_name_content", r.reason === "unsupported" ? `${t} contents use an encoding micro509 cannot validate` : `${t} contents do not match the ${t === "x400Address" ? "ORAddress" : "EDIPartyName"} ASN.1 (RFC 5280)`), e;
|
|
402
|
+
}
|
|
403
|
+
function requireSingleDerElement(e) {
|
|
404
|
+
let t = re(e);
|
|
405
|
+
return t !== "valid" && T("invalid_other_name_value", t === "unsupported" ? "otherName value uses an encoding micro509 cannot validate" : "otherName value must be exactly one DER element"), e;
|
|
406
|
+
}
|
|
407
|
+
/** RFC 9549 §2.5: an rfc822Name or rfc822Name constraint with its host in A-labels. */
|
|
408
|
+
function toAsciiMailbox(e) {
|
|
409
|
+
let t = e.lastIndexOf("@");
|
|
410
|
+
return t < 0 ? toAsciiDnsName(e) : `${e.slice(0, t + 1)}${toAsciiDomain(requireRelativeDomain(e.slice(t + 1)))}`;
|
|
127
411
|
}
|
|
128
412
|
/**
|
|
129
413
|
* DER-encode a single {@linkcode SubjectAltName} GeneralName element.
|
|
@@ -132,13 +416,23 @@ function encodeKeyUsage(e) {
|
|
|
132
416
|
*/
|
|
133
417
|
function encodeSubjectAltName(e) {
|
|
134
418
|
switch (e.type) {
|
|
135
|
-
case "dns": return
|
|
136
|
-
case "email": return
|
|
137
|
-
case "uri": return
|
|
138
|
-
case "srv": return
|
|
139
|
-
case "
|
|
140
|
-
case "
|
|
141
|
-
case "
|
|
419
|
+
case "dns": return l(2, encodeIa5Content(toAsciiDnsName(requireNonEmptyName(e.value))));
|
|
420
|
+
case "email": return l(1, encodeIa5Content(toAsciiMailbox(requireNonEmptyName(e.value))));
|
|
421
|
+
case "uri": return l(6, encodeIa5Content(requireNonEmptyName(e.value)));
|
|
422
|
+
case "srv": return c(0, a([d(C.idOnDnsSrv), o(0, g(22, encodeIa5Content(toAsciiSrvName(requireNonEmptyName(e.value)))))]));
|
|
423
|
+
case "smtpUtf8Mailbox": return c(0, a([d(C.idOnSmtpUtf8Mailbox), o(0, _(assertSmtpUtf8Mailbox(e.value)))]));
|
|
424
|
+
case "upn": return c(0, a([d(C.ntPrincipalName), o(0, _(requireNonEmptyName(e.value)))]));
|
|
425
|
+
case "krb5PrincipalName": return c(0, a([d(C.idPkinitSan), o(0, encodeKrb5PrincipalName(e))]));
|
|
426
|
+
case "ip": return l(7, encodeIpAddress(e.value));
|
|
427
|
+
case "directoryName": return c(4, readDirectoryNameTlv(e.derHex));
|
|
428
|
+
case "otherName": {
|
|
429
|
+
let t = validateOtherNameTypeId(e.typeId), n = requireSingleDerElement(e.value);
|
|
430
|
+
return F(t, n, p(n)) !== void 0 && T("other_name_type_id_has_variant", `otherName type-id ${t} with this value is encoded through its dedicated SubjectAltName variant`), c(0, a([d(t), o(0, n)]));
|
|
431
|
+
}
|
|
432
|
+
case "x400Address": return requireGeneralNameContent(c(3, e.value), "x400Address", L);
|
|
433
|
+
case "ediPartyName": return requireGeneralNameContent(c(5, e.value), "ediPartyName", I);
|
|
434
|
+
case "registeredID": return l(8, p(d(validateOid(e.value))).value);
|
|
435
|
+
case "unknown": return H.has(e.tag) || T("invalid_general_name_tag", `Invalid GeneralName tag: ${e.tag}`), g(e.tag, e.value);
|
|
142
436
|
default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
|
|
143
437
|
}
|
|
144
438
|
}
|
|
@@ -148,7 +442,7 @@ function encodeSubjectAltName(e) {
|
|
|
148
442
|
* @param usages EKU purposes to encode.
|
|
149
443
|
*/
|
|
150
444
|
function encodeExtendedKeyUsage(e) {
|
|
151
|
-
return
|
|
445
|
+
return e.length === 0 && T("extended_key_usage_empty", "extendedKeyUsage must not be empty"), h(e.map((e) => d(getExtendedKeyUsageOid(e))));
|
|
152
446
|
}
|
|
153
447
|
/**
|
|
154
448
|
* DER-encode an Authority Information Access SEQUENCE.
|
|
@@ -156,7 +450,18 @@ function encodeExtendedKeyUsage(e) {
|
|
|
156
450
|
* @param entries AIA entries (OCSP, caIssuers, or custom) to encode.
|
|
157
451
|
*/
|
|
158
452
|
function encodeAuthorityInfoAccess(e) {
|
|
159
|
-
return
|
|
453
|
+
return assertAuthorityInfoAccessProfile(e), h(e.map((e) => h([d(getAuthorityInfoAccessMethodOid(e.method)), encodeSubjectAltName(e.location)])));
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* @internal RFC 5280 §4.2.2.1 and RFC 6960 §3.1 rules for Authority Information
|
|
457
|
+
* Access: at least one entry, and an id-ad-ocsp location that is a URI.
|
|
458
|
+
*
|
|
459
|
+
* The method OID is resolved before the URI check so a custom-OID wrapper cannot
|
|
460
|
+
* smuggle a non-URI OCSP location past the input union.
|
|
461
|
+
*/
|
|
462
|
+
function assertAuthorityInfoAccessProfile(e) {
|
|
463
|
+
e.length === 0 && T("authority_info_access_empty", "authorityInfoAccess must not be empty");
|
|
464
|
+
for (let t of e) getAuthorityInfoAccessMethodOid(t.method) === C.ocspAccessMethod && t.location.type !== "uri" && T("authority_info_access_ocsp_not_uri", "authorityInfoAccess OCSP location must be a URI");
|
|
160
465
|
}
|
|
161
466
|
/**
|
|
162
467
|
* DER-encode a CRL Distribution Points SEQUENCE.
|
|
@@ -164,7 +469,23 @@ function encodeAuthorityInfoAccess(e) {
|
|
|
164
469
|
* @param points Distribution points to encode.
|
|
165
470
|
*/
|
|
166
471
|
function encodeCrlDistributionPoints(e) {
|
|
167
|
-
return
|
|
472
|
+
return assertCrlDistributionPointsProfile(e), h(e.map((e) => h(encodeDistributionPoint(e))));
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* @internal RFC 5280 §4.2.1.13 profile rules for a CRLDistributionPoints value,
|
|
476
|
+
* stated over the fields a typed {@linkcode DistributionPoint} and a decoded
|
|
477
|
+
* `ParsedDistributionPoint` share.
|
|
478
|
+
*
|
|
479
|
+
* `TRelativeName` differs between the two: builder input carries an unencoded RDN,
|
|
480
|
+
* a decoded point carries the parsed one. The rules only read its presence.
|
|
481
|
+
*/
|
|
482
|
+
function assertCrlDistributionPointsProfile(e) {
|
|
483
|
+
e.length === 0 && T("crl_distribution_points_empty", "cRLDistributionPoints must not be empty");
|
|
484
|
+
for (let t of e) assertDistributionPointProfile(t);
|
|
485
|
+
}
|
|
486
|
+
/** RFC 5280 §4.2.1.13 rules for one distribution point. */
|
|
487
|
+
function assertDistributionPointProfile(e) {
|
|
488
|
+
e.crlIssuer !== void 0 && e.crlIssuer.length === 0 && T("distribution_point_crl_issuer_empty", "DistributionPoint crlIssuer must not be empty"), e.distributionPoint === void 0 && e.crlIssuer === void 0 && T("distribution_point_empty", "DistributionPoint must contain distributionPoint or crlIssuer"), assertCrlIssuerDistinguishedNames(e), e.distributionPoint?.type === "fullName" && e.distributionPoint.fullName.length === 0 && T("distribution_point_full_name_empty", "DistributionPointName fullName must not be empty");
|
|
168
489
|
}
|
|
169
490
|
/**
|
|
170
491
|
* DER-encode a Name Constraints extension value.
|
|
@@ -172,8 +493,30 @@ function encodeCrlDistributionPoints(e) {
|
|
|
172
493
|
* @param constraints Permitted and/or excluded subtrees.
|
|
173
494
|
*/
|
|
174
495
|
function encodeNameConstraints(e) {
|
|
175
|
-
|
|
176
|
-
|
|
496
|
+
assertNameConstraintsProfile(e);
|
|
497
|
+
let t = [];
|
|
498
|
+
return e.permittedSubtrees !== void 0 && e.permittedSubtrees.length > 0 && t.push(c(0, a(e.permittedSubtrees.map(encodeGeneralSubtree)))), e.excludedSubtrees !== void 0 && e.excludedSubtrees.length > 0 && t.push(c(1, a(e.excludedSubtrees.map(encodeGeneralSubtree)))), h(t);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* @internal RFC 5280 fixes the criticality of several extensions: nameConstraints
|
|
502
|
+
* (§4.2.1.10), policyConstraints (§4.2.1.11), and inhibitAnyPolicy (§4.2.1.14) are
|
|
503
|
+
* critical; authorityKeyIdentifier (§4.2.1.1), subjectKeyIdentifier (§4.2.1.2),
|
|
504
|
+
* and authorityInfoAccess (§4.2.2.1) are not.
|
|
505
|
+
*
|
|
506
|
+
* @param label Extension name quoted in the diagnostic.
|
|
507
|
+
* @param required The criticality RFC 5280 mandates.
|
|
508
|
+
* @param critical The criticality the caller asked for.
|
|
509
|
+
*/
|
|
510
|
+
function assertExtensionCriticality(e, t, n) {
|
|
511
|
+
n !== t && T(t ? "extension_must_be_critical" : "extension_must_be_non_critical", `The ${e} extension must be marked ${t ? "critical" : "non-critical"}`);
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* @internal RFC 5280 §4.2.1.10: a nameConstraints extension states at least one
|
|
515
|
+
* of permittedSubtrees and excludedSubtrees, and neither may be an empty set.
|
|
516
|
+
*/
|
|
517
|
+
function assertNameConstraintsProfile(e) {
|
|
518
|
+
let t = e.permittedSubtrees?.length ?? 0, n = e.excludedSubtrees?.length ?? 0;
|
|
519
|
+
t === 0 && n === 0 && T("name_constraints_empty", "nameConstraints must set permittedSubtrees or excludedSubtrees");
|
|
177
520
|
}
|
|
178
521
|
/**
|
|
179
522
|
* DER-encode a Certificate Policies extension value.
|
|
@@ -181,8 +524,13 @@ function encodeNameConstraints(e) {
|
|
|
181
524
|
* @param policies Non-empty array of policy information entries.
|
|
182
525
|
*/
|
|
183
526
|
function encodeCertificatePolicies(e) {
|
|
184
|
-
|
|
185
|
-
|
|
527
|
+
e.length === 0 && T("certificate_policies_empty", "certificatePolicies must not be empty");
|
|
528
|
+
let t = /* @__PURE__ */ new Set();
|
|
529
|
+
for (let n of e) {
|
|
530
|
+
let e = validatePolicyOid(n.policyIdentifier);
|
|
531
|
+
t.has(e) && T("duplicate_policy_oid", `Duplicate certificate policy OID: ${n.policyIdentifier}`), t.add(e);
|
|
532
|
+
}
|
|
533
|
+
return h(e.map(encodePolicyInformation));
|
|
186
534
|
}
|
|
187
535
|
/**
|
|
188
536
|
* DER-encode a Policy Mappings extension value.
|
|
@@ -190,10 +538,9 @@ function encodeCertificatePolicies(e) {
|
|
|
190
538
|
* @param mappings Non-empty array of issuer-to-subject policy pairs. Neither OID may be anyPolicy.
|
|
191
539
|
*/
|
|
192
540
|
function encodePolicyMappings(e) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return f([s(e.issuerDomainPolicy), s(e.subjectDomainPolicy)]);
|
|
541
|
+
return e.length === 0 && T("policy_mappings_empty", "policyMappings must not be empty"), h(e.map((e) => {
|
|
542
|
+
let t = validatePolicyOid(e.issuerDomainPolicy), n = validatePolicyOid(e.subjectDomainPolicy);
|
|
543
|
+
return (t === C.anyPolicy || n === C.anyPolicy) && T("policy_mappings_any_policy", "policyMappings must not use anyPolicy"), h([d(e.issuerDomainPolicy), d(e.subjectDomainPolicy)]);
|
|
197
544
|
}));
|
|
198
545
|
}
|
|
199
546
|
/**
|
|
@@ -203,8 +550,7 @@ function encodePolicyMappings(e) {
|
|
|
203
550
|
*/
|
|
204
551
|
function encodePolicyConstraints(e) {
|
|
205
552
|
let t = [];
|
|
206
|
-
|
|
207
|
-
return f(t);
|
|
553
|
+
return e.requireExplicitPolicy !== void 0 && t.push(l(0, encodeIntegerContent(e.requireExplicitPolicy))), e.inhibitPolicyMapping !== void 0 && t.push(l(1, encodeIntegerContent(e.inhibitPolicyMapping))), t.length === 0 && T("policy_constraints_empty", "policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping"), h(t);
|
|
208
554
|
}
|
|
209
555
|
/**
|
|
210
556
|
* DER-encode an Inhibit anyPolicy extension value (single INTEGER).
|
|
@@ -212,76 +558,131 @@ function encodePolicyConstraints(e) {
|
|
|
212
558
|
* @param input The skipCerts threshold.
|
|
213
559
|
*/
|
|
214
560
|
function encodeInhibitAnyPolicy(e) {
|
|
215
|
-
return
|
|
561
|
+
return u(e.skipCerts);
|
|
216
562
|
}
|
|
217
563
|
/** DER-encode a single PolicyInformation SEQUENCE. */
|
|
218
564
|
function encodePolicyInformation(e) {
|
|
219
565
|
validatePolicyOid(e.policyIdentifier);
|
|
220
|
-
let t = [
|
|
221
|
-
return e.policyQualifiers !== void 0 && e.policyQualifiers.length > 0 && t.push(
|
|
566
|
+
let t = [d(e.policyIdentifier)];
|
|
567
|
+
return e.policyQualifiers !== void 0 && e.policyQualifiers.length > 0 && t.push(h(e.policyQualifiers.map(encodePolicyQualifierInfo))), h(t);
|
|
222
568
|
}
|
|
223
569
|
/** DER-encode a single PolicyQualifierInfo SEQUENCE. */
|
|
224
570
|
function encodePolicyQualifierInfo(e) {
|
|
225
571
|
switch (e.type) {
|
|
226
|
-
case "cps": return
|
|
227
|
-
case "userNotice": return
|
|
228
|
-
case "oid":
|
|
572
|
+
case "cps": return h([d(C.cpsPolicyQualifier), te(e.uri)]);
|
|
573
|
+
case "userNotice": return h([d(C.userNoticePolicyQualifier), encodeUserNoticePolicyQualifierInfo(e)]);
|
|
574
|
+
case "oid": {
|
|
575
|
+
validateOid(e.oid);
|
|
576
|
+
let t = d(e.oid), n = S(t);
|
|
577
|
+
return (n === S(d(C.cpsPolicyQualifier)) || n === S(d(C.userNoticePolicyQualifier))) && T("reserved_policy_qualifier_oid", "Policy qualifier must use the typed cps or userNotice variant, not a built-in OID"), h([t, new Uint8Array(e.qualifierDer)]);
|
|
578
|
+
}
|
|
229
579
|
default: throw Error(`Unhandled PolicyQualifierInfo type: ${String(e)}`);
|
|
230
580
|
}
|
|
231
581
|
}
|
|
232
582
|
/** DER-encode a UserNotice qualifier SEQUENCE. */
|
|
583
|
+
function assertDisplayText(e) {
|
|
584
|
+
s(e) && T("display_text_lone_surrogate", "DisplayText must not hold a lone UTF-16 surrogate");
|
|
585
|
+
let t = [...e].length;
|
|
586
|
+
(t === 0 || t > 200) && T("display_text_out_of_range", "DisplayText must be 1 to 200 characters");
|
|
587
|
+
}
|
|
233
588
|
function encodeUserNoticePolicyQualifierInfo(e) {
|
|
234
589
|
let t = [];
|
|
235
|
-
return e.noticeRef !== void 0 && t.push(encodePolicyNoticeReference(e.noticeRef)), e.explicitText !== void 0 && t.push(
|
|
590
|
+
return e.noticeRef !== void 0 && t.push(encodePolicyNoticeReference(e.noticeRef)), e.explicitText !== void 0 && (assertDisplayText(e.explicitText), t.push(encodeExplicitText(e.explicitText, e.explicitTextType))), h(t);
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* RFC 6818 §3, replacing the explicitText paragraph of RFC 5280 §4.2.1.4:
|
|
594
|
+
* UTF8String is preferred, VisibleString and BMPString are acceptable,
|
|
595
|
+
* IA5String is forbidden, control characters should not appear, and UTF8String
|
|
596
|
+
* and BMPString text should be in Unicode normalization form C.
|
|
597
|
+
*/
|
|
598
|
+
function encodeExplicitText(e, t = "utf8String") {
|
|
599
|
+
switch ([...e].some(isControlCharacter) && T("display_text_control_character", "explicitText must not contain control characters"), t) {
|
|
600
|
+
case "utf8String": return assertNormalizationFormC(e), _(e);
|
|
601
|
+
case "bmpString": return assertNormalizationFormC(e), encodeBmpDisplayText(e);
|
|
602
|
+
case "visibleString": return encodeVisibleDisplayText(e);
|
|
603
|
+
case "ia5String": return T("display_text_ia5_string", "explicitText must not be encoded as IA5String");
|
|
604
|
+
default: throw Error(`Unhandled DisplayText type: ${String(t)}`);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
function isControlCharacter(e) {
|
|
608
|
+
let t = e.codePointAt(0) ?? 0;
|
|
609
|
+
return t <= 31 || t >= 127 && t <= 159;
|
|
610
|
+
}
|
|
611
|
+
function assertNormalizationFormC(e) {
|
|
612
|
+
e.normalize("NFC") !== e && T("display_text_not_nfc", "explicitText must be in Unicode normalization form C");
|
|
613
|
+
}
|
|
614
|
+
function encodeVisibleDisplayText(e) {
|
|
615
|
+
try {
|
|
616
|
+
return v(e);
|
|
617
|
+
} catch {
|
|
618
|
+
return T("invalid_visible_string", "VisibleString explicitText must be printable ASCII");
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function encodeBmpDisplayText(e) {
|
|
622
|
+
try {
|
|
623
|
+
return r(e);
|
|
624
|
+
} catch {
|
|
625
|
+
return T("invalid_bmp_string", "BMPString explicitText must hold Basic Multilingual Plane characters other than surrogates, U+FFFE and U+FFFF");
|
|
626
|
+
}
|
|
236
627
|
}
|
|
237
628
|
/** DER-encode a NoticeReference SEQUENCE. */
|
|
238
629
|
function encodePolicyNoticeReference(e) {
|
|
239
|
-
return
|
|
630
|
+
return assertDisplayText(e.organization), h([_(e.organization), h(e.noticeNumbers.map((e) => u(e)))]);
|
|
240
631
|
}
|
|
241
632
|
/** Encode an INTEGER and return only the content bytes (no TLV wrapper). */
|
|
242
633
|
function encodeIntegerContent(e) {
|
|
243
|
-
return
|
|
634
|
+
return f(u(e)).value;
|
|
244
635
|
}
|
|
245
636
|
/** DER-encode a GeneralSubtree SEQUENCE (base name only; min/max omitted per RFC 5280). */
|
|
246
637
|
function encodeGeneralSubtree(e) {
|
|
247
|
-
return
|
|
638
|
+
return h([encodeNameConstraintForm(e.base)]);
|
|
248
639
|
}
|
|
249
|
-
/**
|
|
640
|
+
/**
|
|
641
|
+
* RFC 5280 §4.2.1.13: cRLIssuer, when present, only contains the CRL issuer's
|
|
642
|
+
* distinguished name; nameRelativeToCRLIssuer additionally requires exactly one.
|
|
643
|
+
*
|
|
644
|
+
* Applies to a typed {@linkcode DistributionPoint} and to a decoded
|
|
645
|
+
* {@linkcode ParsedDistributionPoint} alike, so the rule holds whether the value
|
|
646
|
+
* arrives through `crlDistributionPoints` or through `customExtensions`.
|
|
647
|
+
*/
|
|
648
|
+
function assertCrlIssuerDistinguishedNames(e) {
|
|
649
|
+
e.crlIssuer !== void 0 && (e.crlIssuer.some((e) => e.type !== "directoryName") && T("distribution_point_crl_issuer_not_directory_name", "DistributionPoint cRLIssuer must only contain directoryName entries"), e.distributionPoint?.type === "relativeName" && e.crlIssuer.length > 1 && T("distribution_point_relative_name_multiple_crl_issuers", "DistributionPointName relativeName requires at most one cRLIssuer distinguished name"));
|
|
650
|
+
}
|
|
651
|
+
/** DER-encode the fields of a single DistributionPoint, already profile-checked. */
|
|
250
652
|
function encodeDistributionPoint(e) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
let n = [];
|
|
254
|
-
return e.distributionPoint !== void 0 && n.push(i(0, encodeDistributionPointName(e.distributionPoint))), e.reasons !== void 0 && e.reasons.length > 0 && n.push(a(1, y(e.reasons))), e.crlIssuer !== void 0 && e.crlIssuer.length > 0 && n.push(i(2, t(e.crlIssuer.map(encodeSubjectAltName)))), n;
|
|
653
|
+
let t = [];
|
|
654
|
+
return e.distributionPoint !== void 0 && t.push(c(0, encodeDistributionPointName(e.distributionPoint))), e.reasons !== void 0 && e.reasons.length > 0 && t.push(l(1, E(e.reasons))), e.crlIssuer !== void 0 && t.push(c(2, a(e.crlIssuer.map(encodeSubjectAltName)))), t;
|
|
255
655
|
}
|
|
256
656
|
/** DER-encode a DistributionPointName (fullName or relativeName). */
|
|
257
657
|
function encodeDistributionPointName(e) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
return i(1, t.slice(n.start, n.end));
|
|
658
|
+
switch (e.type) {
|
|
659
|
+
case "fullName": return e.fullName.length === 0 && T("distribution_point_full_name_empty", "DistributionPointName fullName must not be empty"), c(0, a(e.fullName.map(encodeSubjectAltName)));
|
|
660
|
+
case "relativeName": {
|
|
661
|
+
let t = U(e.relativeName), n = f(t);
|
|
662
|
+
return c(1, t.slice(n.start, n.end));
|
|
663
|
+
}
|
|
664
|
+
default: throw Error(`Unhandled DistributionPointName type: ${String(e)}`);
|
|
266
665
|
}
|
|
267
|
-
throw Error("DistributionPointName must contain fullName or relativeName");
|
|
268
666
|
}
|
|
269
667
|
/** DER-encode a NameConstraintForm as an implicit-tagged {@linkcode GeneralName}. */
|
|
270
668
|
function encodeNameConstraintForm(e) {
|
|
271
669
|
switch (e.type) {
|
|
272
|
-
case "dns": return
|
|
273
|
-
case "email": return
|
|
274
|
-
case "uri": return
|
|
275
|
-
case "ip":
|
|
276
|
-
|
|
670
|
+
case "dns": return l(2, encodeIa5Content(toAsciiDnsName(e.value)));
|
|
671
|
+
case "email": return e.value.includes("@") && T("email_name_constraint_names_mailbox", "An rfc822Name constraint names a host or a domain, not a particular mailbox (RFC 9549 §2.2)"), l(1, encodeIa5Content(toAsciiMailbox(e.value)));
|
|
672
|
+
case "uri": return V(e.value) === void 0 && T("invalid_uri_name_constraint", "A uniformResourceIdentifier constraint is a fully qualified domain name, with a leading period for its subdomains (RFC 5280 §4.2.1.10)"), l(6, encodeIa5Content(e.value));
|
|
673
|
+
case "ip": {
|
|
674
|
+
let t = e.addressBytes.length + e.maskBytes.length;
|
|
675
|
+
return (e.addressBytes.length !== e.maskBytes.length || t !== 8 && t !== 32) && T("invalid_ip_name_constraint", "IP name constraint must be 8 octets (IPv4) or 32 octets (IPv6)"), l(7, a([e.addressBytes, e.maskBytes]));
|
|
676
|
+
}
|
|
677
|
+
case "directoryName": return c(4, readDirectoryNameTlv(e.derHex));
|
|
678
|
+
case "srv": return c(0, a([d(C.idOnDnsSrv), o(0, g(22, encodeIa5Content(toAsciiSrvNameConstraint(e.value))))]));
|
|
277
679
|
default: throw Error(`Unhandled NameConstraintForm type: ${String(e)}`);
|
|
278
680
|
}
|
|
279
681
|
}
|
|
280
|
-
/**
|
|
281
|
-
function
|
|
282
|
-
let t =
|
|
283
|
-
|
|
284
|
-
return new Uint8Array(t.value);
|
|
682
|
+
/** Returns the complete Name TLV from a hex-encoded DER SEQUENCE, validating the root tag. */
|
|
683
|
+
function readDirectoryNameTlv(e) {
|
|
684
|
+
let t = x(e), n = p(t, { maxDepth: 64 });
|
|
685
|
+
return n.tag !== 48 && T("directory_name_not_sequence", "directoryName derHex must encode a DER SEQUENCE"), t.slice(n.start - n.headerLength, n.end);
|
|
285
686
|
}
|
|
286
687
|
/**
|
|
287
688
|
* Resolve an {@linkcode ExtendedKeyUsage} to its dotted-decimal OID.
|
|
@@ -289,7 +690,7 @@ function extractDirectoryNameContent(e) {
|
|
|
289
690
|
* @param usage Well-known string or custom OID object.
|
|
290
691
|
*/
|
|
291
692
|
function getExtendedKeyUsageOid(e) {
|
|
292
|
-
return typeof e == "string" ?
|
|
693
|
+
return typeof e == "string" ? de[e] : validateOid(e.value);
|
|
293
694
|
}
|
|
294
695
|
/**
|
|
295
696
|
* Map a dotted-decimal OID to an {@linkcode ExtendedKeyUsage} value.
|
|
@@ -298,12 +699,12 @@ function getExtendedKeyUsageOid(e) {
|
|
|
298
699
|
*/
|
|
299
700
|
function parseExtendedKeyUsageOid(e) {
|
|
300
701
|
switch (e) {
|
|
301
|
-
case
|
|
302
|
-
case
|
|
303
|
-
case
|
|
304
|
-
case
|
|
305
|
-
case
|
|
306
|
-
case
|
|
702
|
+
case C.serverAuth: return "serverAuth";
|
|
703
|
+
case C.clientAuth: return "clientAuth";
|
|
704
|
+
case C.codeSigning: return "codeSigning";
|
|
705
|
+
case C.emailProtection: return "emailProtection";
|
|
706
|
+
case C.timeStamping: return "timeStamping";
|
|
707
|
+
case C.ocspSigning: return "ocspSigning";
|
|
307
708
|
}
|
|
308
709
|
return {
|
|
309
710
|
type: "oid",
|
|
@@ -316,7 +717,7 @@ function parseExtendedKeyUsageOid(e) {
|
|
|
316
717
|
* @param method Well-known string or custom OID object.
|
|
317
718
|
*/
|
|
318
719
|
function getAuthorityInfoAccessMethodOid(e) {
|
|
319
|
-
return typeof e == "string" ?
|
|
720
|
+
return typeof e == "string" ? fe[e] : validateOid(e.value);
|
|
320
721
|
}
|
|
321
722
|
/**
|
|
322
723
|
* Map a dotted-decimal OID to an {@linkcode AuthorityInfoAccessMethod} value.
|
|
@@ -325,8 +726,8 @@ function getAuthorityInfoAccessMethodOid(e) {
|
|
|
325
726
|
*/
|
|
326
727
|
function parseAuthorityInfoAccessMethodOid(e) {
|
|
327
728
|
switch (e) {
|
|
328
|
-
case
|
|
329
|
-
case
|
|
729
|
+
case C.ocspAccessMethod: return "ocsp";
|
|
730
|
+
case C.caIssuersAccessMethod: return "caIssuers";
|
|
330
731
|
}
|
|
331
732
|
return {
|
|
332
733
|
type: "oid",
|
|
@@ -335,28 +736,60 @@ function parseAuthorityInfoAccessMethodOid(e) {
|
|
|
335
736
|
}
|
|
336
737
|
/** Parse a dotted-decimal/colon-hex IP string to raw address bytes. */
|
|
337
738
|
function encodeIpAddress(e) {
|
|
338
|
-
return
|
|
739
|
+
return M(e);
|
|
339
740
|
}
|
|
340
|
-
/**
|
|
741
|
+
/**
|
|
742
|
+
* Computes a key identifier from a DER `SubjectPublicKeyInfo` using method (1)
|
|
743
|
+
* of RFC 5280 §4.2.1.2: "the 160-bit SHA-1 hash of the value of the BIT STRING
|
|
744
|
+
* subjectPublicKey (excluding the tag, length, and number of unused bits)".
|
|
745
|
+
* A parsed certificate carries the input as `subjectPublicKeyInfoDer`.
|
|
746
|
+
*/
|
|
341
747
|
function buildSubjectKeyIdentifier(e) {
|
|
342
|
-
let t =
|
|
748
|
+
let t = m(e)[1];
|
|
343
749
|
if (t === void 0 || t.tag !== 3) throw Error("SPKI missing subject public key bit string");
|
|
344
|
-
|
|
750
|
+
let n = t.value.slice(1);
|
|
751
|
+
return w(n);
|
|
345
752
|
}
|
|
346
|
-
/**
|
|
347
|
-
|
|
348
|
-
|
|
753
|
+
/**
|
|
754
|
+
* The key identifier a certificate or CRL issued by `issuer` carries in its
|
|
755
|
+
* authority key identifier: the issuer's subject key identifier (RFC 5280
|
|
756
|
+
* §4.2.1.2, §5.2.1), or a method (1) identifier over its public key when it
|
|
757
|
+
* has none (§4.2.1.1).
|
|
758
|
+
*/
|
|
759
|
+
function issuerKeyIdentifier(e) {
|
|
760
|
+
return e.subjectKeyIdentifier === void 0 ? buildSubjectKeyIdentifier(e.subjectPublicKeyInfoDer) : x(e.subjectKeyIdentifier);
|
|
349
761
|
}
|
|
350
|
-
/**
|
|
762
|
+
/**
|
|
763
|
+
* Throw if the string is not an encodable dotted-decimal OID, and return its
|
|
764
|
+
* canonical spelling.
|
|
765
|
+
*
|
|
766
|
+
* Syntax alone is not enough. X.660 bounds the first arc to 0, 1, or 2 and the
|
|
767
|
+
* second to under 40 beneath arcs 0 and 1, so `3.1` and `1.40` parse as decimals
|
|
768
|
+
* yet cannot be encoded.
|
|
769
|
+
*/
|
|
770
|
+
function validateOid(t) {
|
|
771
|
+
/^\d+(?:\.\d+)+$/.test(t) || T("invalid_oid", `Invalid OID: ${t}`);
|
|
772
|
+
try {
|
|
773
|
+
return y(t);
|
|
774
|
+
} catch (r) {
|
|
775
|
+
n(r, e), T("invalid_oid", `Invalid OID: ${t}`);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
/** Validate a policy OID and return its canonical spelling. */
|
|
351
779
|
function validatePolicyOid(e) {
|
|
352
|
-
validateOid(e);
|
|
780
|
+
return validateOid(e);
|
|
353
781
|
}
|
|
354
|
-
/**
|
|
782
|
+
/**
|
|
783
|
+
* Encode and push an extension, rejecting duplicate OIDs.
|
|
784
|
+
*
|
|
785
|
+
* Duplicate identity is the canonical OID, because two spellings that differ only
|
|
786
|
+
* by leading zeros in an arc encode to the same wire bytes. The diagnostic quotes
|
|
787
|
+
* the OID as submitted, which is the string the caller can find in their input.
|
|
788
|
+
*/
|
|
355
789
|
function pushExtension(e, t, n, r, i = !1) {
|
|
356
|
-
|
|
357
|
-
t.
|
|
790
|
+
let a = validateOid(n);
|
|
791
|
+
t.has(a) && T("duplicate_extension_oid", `Duplicate extension OID: ${n}`), t.add(a), e.push(encodeExtension(n, r, i));
|
|
358
792
|
}
|
|
359
|
-
|
|
360
|
-
export { buildCertificateExtensions, buildRequestedExtensions, buildSubjectKeyIdentifier, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid };
|
|
793
|
+
export { k as allOnesMaskForIpAddress, assertAuthorityInfoAccessProfile, assertCrlDistributionPointsProfile, assertExtensionCriticality, assertNameConstraintsProfile, assertStoredSubjectAltName, buildCertificateExtensions, buildRequestedExtensions, buildSubjectKeyIdentifier, A as decodeIpAddress, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, issuerKeyIdentifier, j as normalizeIpAddress, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid, M as parseIpAddressToBytes };
|
|
361
794
|
|
|
362
795
|
//# sourceMappingURL=extensions.js.map
|