micro509 0.7.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -5
- package/dist/index.js +15 -1
- package/dist/internal/asn1/asn1.js +280 -2
- package/dist/internal/asn1/der.js +378 -2
- package/dist/internal/asn1/oids.js +103 -2
- package/dist/internal/crypto/algorithm-names.js +58 -2
- package/dist/internal/crypto/ecdsa.js +68 -2
- package/dist/internal/crypto/hash.js +286 -2
- package/dist/internal/crypto/pbes2.js +213 -2
- package/dist/internal/crypto/rsa-pss.js +184 -2
- package/dist/internal/crypto/sig-verify.js +192 -2
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +123 -2
- package/dist/internal/crypto/webcrypto.js +14 -2
- package/dist/internal/shared/base64.js +16 -2
- package/dist/internal/shared/dn.js +84 -2
- package/dist/internal/shared/ip.js +164 -2
- package/dist/internal/verify/name-constraints-engine.js +450 -2
- package/dist/internal/verify/policy-engine.js +382 -2
- package/dist/internal/verify/verify-path.js +298 -2
- package/dist/internal/x509/extension-bits.js +132 -2
- package/dist/internal/x509/extension-registry.js +229 -2
- package/dist/internal/x509/name-fields.js +93 -2
- package/dist/keys/keys.d.ts +380 -13
- package/dist/keys/keys.js +1247 -5
- package/dist/keys.d.ts +2 -2
- package/dist/keys.js +2 -1
- package/dist/pem/pem.d.ts +2 -2
- package/dist/pem/pem.js +172 -6
- package/dist/pem.js +2 -1
- package/dist/pkcs/pfx.d.ts +4 -5
- package/dist/pkcs/pfx.js +395 -2
- package/dist/pkcs/pkcs12-mac.d.ts +2 -2
- package/dist/pkcs/pkcs12-mac.js +175 -2
- package/dist/pkcs/pkcs7.d.ts +1 -2
- package/dist/pkcs/pkcs7.js +653 -2
- package/dist/pkcs.d.ts +2 -2
- package/dist/pkcs.js +4 -1
- package/dist/result/result.d.ts +1 -2
- package/dist/result/result.js +111 -2
- package/dist/result.js +2 -1
- package/dist/revocation/chain.d.ts +4 -5
- package/dist/revocation/chain.js +536 -2
- package/dist/revocation/crl.d.ts +2 -2
- package/dist/revocation/crl.js +1119 -2
- package/dist/revocation/ocsp.d.ts +5 -4
- package/dist/revocation/ocsp.js +958 -2
- package/dist/revocation/revocation.d.ts +1 -2
- package/dist/revocation/revocation.js +261 -2
- package/dist/revocation.d.ts +2 -2
- package/dist/revocation.js +5 -1
- package/dist/verify/identity.d.ts +1 -2
- package/dist/verify/identity.js +270 -2
- package/dist/verify/name-constraints.d.ts +1 -2
- package/dist/verify/policy.d.ts +1 -2
- package/dist/verify/verify.d.ts +1 -2
- package/dist/verify/verify.js +764 -2
- package/dist/verify.js +3 -1
- package/dist/x509/certificate.d.ts +1 -2
- package/dist/x509/certificate.js +151 -2
- package/dist/x509/csr.d.ts +1 -2
- package/dist/x509/csr.js +71 -2
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +387 -2
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +173 -2
- package/dist/x509/parse.d.ts +84 -2
- package/dist/x509/parse.js +1090 -2
- package/dist/x509.d.ts +2 -2
- package/dist/x509.js +6 -1
- package/package.json +74 -34
- package/dist/internal/asn1/asn1.js.map +0 -1
- package/dist/internal/asn1/der.js.map +0 -1
- package/dist/internal/asn1/oids.js.map +0 -1
- package/dist/internal/crypto/algorithm-names.js.map +0 -1
- package/dist/internal/crypto/ecdsa.js.map +0 -1
- package/dist/internal/crypto/hash.js.map +0 -1
- package/dist/internal/crypto/pbes2.d.ts +0 -23
- package/dist/internal/crypto/pbes2.js.map +0 -1
- package/dist/internal/crypto/rsa-pss.js.map +0 -1
- package/dist/internal/crypto/sig-verify.js.map +0 -1
- package/dist/internal/crypto/signing.js.map +0 -1
- package/dist/internal/crypto/webcrypto.js.map +0 -1
- package/dist/internal/shared/base64.js.map +0 -1
- package/dist/internal/shared/dn.js.map +0 -1
- package/dist/internal/shared/ip.js.map +0 -1
- package/dist/internal/verify/name-constraints-engine.js.map +0 -1
- package/dist/internal/verify/policy-engine.js.map +0 -1
- package/dist/internal/verify/verify-path.js.map +0 -1
- package/dist/internal/x509/extension-bits.js.map +0 -1
- package/dist/internal/x509/extension-registry.js.map +0 -1
- package/dist/internal/x509/name-fields.js.map +0 -1
- package/dist/keys/keys.js.map +0 -1
- package/dist/pem/pem.js.map +0 -1
- package/dist/pkcs/pfx.js.map +0 -1
- package/dist/pkcs/pkcs12-mac.js.map +0 -1
- package/dist/pkcs/pkcs7.js.map +0 -1
- package/dist/result/result.js.map +0 -1
- package/dist/revocation/chain.js.map +0 -1
- package/dist/revocation/crl.js.map +0 -1
- package/dist/revocation/ocsp.js.map +0 -1
- package/dist/revocation/revocation.js.map +0 -1
- package/dist/verify/identity.js.map +0 -1
- package/dist/verify/verify.js.map +0 -1
- package/dist/x509/certificate.js.map +0 -1
- package/dist/x509/csr.js.map +0 -1
- package/dist/x509/extensions.js.map +0 -1
- package/dist/x509/name.js.map +0 -1
- package/dist/x509/parse.js.map +0 -1
package/dist/x509/extensions.js
CHANGED
|
@@ -1,2 +1,387 @@
|
|
|
1
|
-
import{bool
|
|
2
|
-
|
|
1
|
+
import { bool, concatBytes, explicitContext, ia5String, implicitConstructedContext, implicitPrimitiveContext, integerFromNumber, objectIdentifier, octetString, readElement, readRootElement, readSequenceChildren, sequence, tlv, utf8String } from "../internal/asn1/der.js";
|
|
2
|
+
import { hexToBytes } from "../internal/asn1/asn1.js";
|
|
3
|
+
import { OIDS } from "../internal/asn1/oids.js";
|
|
4
|
+
import { sha1 } from "../internal/crypto/hash.js";
|
|
5
|
+
import { parseIpAddressToBytes } from "../internal/shared/ip.js";
|
|
6
|
+
import { encodeDistributionPointReasonFlagsContent, encodeKeyUsageExtension } from "../internal/x509/extension-bits.js";
|
|
7
|
+
import { encodeRelativeDistinguishedName } from "./name.js";
|
|
8
|
+
import { AUTHORITY_INFO_ACCESS_EXTENSION_DEFINITION, AUTHORITY_KEY_IDENTIFIER_EXTENSION_DEFINITION, BASIC_CONSTRAINTS_EXTENSION_DEFINITION, CERTIFICATE_POLICIES_EXTENSION_DEFINITION, CRL_DISTRIBUTION_POINTS_EXTENSION_DEFINITION, EXTENDED_KEY_USAGE_EXTENSION_DEFINITION, INHIBIT_ANY_POLICY_EXTENSION_DEFINITION, KEY_USAGE_EXTENSION_DEFINITION, NAME_CONSTRAINTS_EXTENSION_DEFINITION, POLICY_CONSTRAINTS_EXTENSION_DEFINITION, POLICY_MAPPINGS_EXTENSION_DEFINITION, SUBJECT_ALT_NAME_EXTENSION_DEFINITION, SUBJECT_KEY_IDENTIFIER_EXTENSION_DEFINITION, getExtensionDefinition } from "../internal/x509/extension-registry.js";
|
|
9
|
+
//#region src/x509/extensions.ts
|
|
10
|
+
/**
|
|
11
|
+
* Typed certificate and CSR extension model plus ASN.1 encoder helpers.
|
|
12
|
+
*
|
|
13
|
+
* Defines input types for every supported X.509v3 extension (RFC 5280) and
|
|
14
|
+
* the DER encoders consumed by certificate and CSR builder flows.
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
/** Map from well-known EKU names to their dotted-decimal OIDs. */
|
|
19
|
+
const EXTENDED_KEY_USAGE_OIDS = {
|
|
20
|
+
serverAuth: OIDS.serverAuth,
|
|
21
|
+
clientAuth: OIDS.clientAuth,
|
|
22
|
+
codeSigning: OIDS.codeSigning,
|
|
23
|
+
emailProtection: OIDS.emailProtection,
|
|
24
|
+
timeStamping: OIDS.timeStamping,
|
|
25
|
+
ocspSigning: OIDS.ocspSigning
|
|
26
|
+
};
|
|
27
|
+
/** Map from well-known AIA method names to their dotted-decimal OIDs. */
|
|
28
|
+
const AUTHORITY_INFO_ACCESS_METHOD_OIDS = {
|
|
29
|
+
ocsp: OIDS.ocspAccessMethod,
|
|
30
|
+
caIssuers: OIDS.caIssuersAccessMethod
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Build the v3 extensions block for a certificate.
|
|
34
|
+
*
|
|
35
|
+
* Automatically adds SKI, AKI (when issuer key is available), and
|
|
36
|
+
* basicConstraints (defaults to `{ ca: false }`). Additional extensions
|
|
37
|
+
* come from the caller's {@linkcode CertificateExtensionsInput}.
|
|
38
|
+
*
|
|
39
|
+
* @param subjectPublicKeyInfo DER-encoded SPKI of the subject.
|
|
40
|
+
* @param issuerPublicKeyInfo DER-encoded SPKI of the issuer, or `undefined` for self-signed.
|
|
41
|
+
* @param input Optional extension configuration.
|
|
42
|
+
* @param subjectIsEmpty Whether the certificate subject DN is empty. When `true`, the
|
|
43
|
+
* subjectAltName extension is marked critical per RFC 5280 §4.2.1.6.
|
|
44
|
+
* @returns Array of DER-encoded Extension SEQUENCEs.
|
|
45
|
+
*/
|
|
46
|
+
function buildCertificateExtensions(subjectPublicKeyInfo, issuerPublicKeyInfo, input, subjectIsEmpty = false) {
|
|
47
|
+
const extensions = [];
|
|
48
|
+
const seen = /* @__PURE__ */ new Set();
|
|
49
|
+
pushKnownExtension(extensions, seen, BASIC_CONSTRAINTS_EXTENSION_DEFINITION, input?.basicConstraints ?? { ca: false });
|
|
50
|
+
pushKnownExtension(extensions, seen, SUBJECT_KEY_IDENTIFIER_EXTENSION_DEFINITION, buildSubjectKeyIdentifier(subjectPublicKeyInfo));
|
|
51
|
+
if (issuerPublicKeyInfo !== void 0) pushKnownExtension(extensions, seen, AUTHORITY_KEY_IDENTIFIER_EXTENSION_DEFINITION, buildSubjectKeyIdentifier(issuerPublicKeyInfo));
|
|
52
|
+
appendConfiguredExtensions(extensions, seen, input, "certificate", {
|
|
53
|
+
includeBasicConstraints: false,
|
|
54
|
+
subjectIsEmpty
|
|
55
|
+
});
|
|
56
|
+
return extensions;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Build the extensions for a CSR's extensionRequest attribute.
|
|
60
|
+
*
|
|
61
|
+
* Unlike {@linkcode buildCertificateExtensions}, SKI/AKI are not auto-generated.
|
|
62
|
+
*
|
|
63
|
+
* @param input Optional extension configuration.
|
|
64
|
+
* @returns Array of DER-encoded Extension SEQUENCEs.
|
|
65
|
+
*/
|
|
66
|
+
function buildRequestedExtensions(input) {
|
|
67
|
+
const extensions = [];
|
|
68
|
+
appendConfiguredExtensions(extensions, /* @__PURE__ */ new Set(), input, "csr", { includeBasicConstraints: true });
|
|
69
|
+
return extensions;
|
|
70
|
+
}
|
|
71
|
+
/** Encode and push each configured extension from the input, enforcing no duplicates. */
|
|
72
|
+
function appendConfiguredExtensions(encoded, seen, input, context, options) {
|
|
73
|
+
if (input === void 0) return;
|
|
74
|
+
if (options.includeBasicConstraints && input.basicConstraints !== void 0) pushKnownExtension(encoded, seen, BASIC_CONSTRAINTS_EXTENSION_DEFINITION, input.basicConstraints);
|
|
75
|
+
if (input.keyUsage !== void 0 && input.keyUsage.length > 0) pushKnownExtension(encoded, seen, KEY_USAGE_EXTENSION_DEFINITION, input.keyUsage);
|
|
76
|
+
if (input.subjectAltNames !== void 0 && input.subjectAltNames.length > 0) {
|
|
77
|
+
const sanCritical = options.subjectIsEmpty === true ? true : void 0;
|
|
78
|
+
pushKnownExtension(encoded, seen, SUBJECT_ALT_NAME_EXTENSION_DEFINITION, input.subjectAltNames, sanCritical);
|
|
79
|
+
}
|
|
80
|
+
if (input.extendedKeyUsage !== void 0 && input.extendedKeyUsage.length > 0) pushKnownExtension(encoded, seen, EXTENDED_KEY_USAGE_EXTENSION_DEFINITION, input.extendedKeyUsage);
|
|
81
|
+
if (input.nameConstraints !== void 0) pushKnownExtension(encoded, seen, NAME_CONSTRAINTS_EXTENSION_DEFINITION, input.nameConstraints);
|
|
82
|
+
if (input.certificatePolicies !== void 0 && input.certificatePolicies.length > 0) pushKnownExtension(encoded, seen, CERTIFICATE_POLICIES_EXTENSION_DEFINITION, input.certificatePolicies);
|
|
83
|
+
if (input.policyMappings !== void 0 && input.policyMappings.length > 0) pushKnownExtension(encoded, seen, POLICY_MAPPINGS_EXTENSION_DEFINITION, input.policyMappings);
|
|
84
|
+
if (input.policyConstraints !== void 0) pushKnownExtension(encoded, seen, POLICY_CONSTRAINTS_EXTENSION_DEFINITION, input.policyConstraints);
|
|
85
|
+
if (input.inhibitAnyPolicy !== void 0) pushKnownExtension(encoded, seen, INHIBIT_ANY_POLICY_EXTENSION_DEFINITION, input.inhibitAnyPolicy);
|
|
86
|
+
if (input.authorityInfoAccess !== void 0 && input.authorityInfoAccess.length > 0) pushKnownExtension(encoded, seen, AUTHORITY_INFO_ACCESS_EXTENSION_DEFINITION, input.authorityInfoAccess);
|
|
87
|
+
if (input.crlDistributionPoints !== void 0 && input.crlDistributionPoints.length > 0) pushKnownExtension(encoded, seen, CRL_DISTRIBUTION_POINTS_EXTENSION_DEFINITION, input.crlDistributionPoints);
|
|
88
|
+
if (input.customExtensions !== void 0) for (const extension of input.customExtensions) {
|
|
89
|
+
const knownDefinition = getExtensionDefinition(extension.oid);
|
|
90
|
+
if (knownDefinition !== void 0 && !knownDefinition.contexts.includes(context)) throw new Error(`Extension ${extension.oid} is not supported in ${context} context`);
|
|
91
|
+
pushExtension(encoded, seen, extension.oid, new Uint8Array(extension.value), extension.critical ?? false);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Encode a known extension via its registry definition and push it. */
|
|
95
|
+
function pushKnownExtension(encoded, seen, definition, value, critical = definition.defaultCritical) {
|
|
96
|
+
pushExtension(encoded, seen, definition.oid, definition.encode(value), critical);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Encode a single X.509 Extension SEQUENCE (OID + optional critical BOOLEAN + OCTET STRING).
|
|
100
|
+
*
|
|
101
|
+
* @param oid Dotted-decimal extension OID.
|
|
102
|
+
* @param extnValue DER-encoded extension payload.
|
|
103
|
+
* @param critical Whether to mark the extension as critical. Default `false`.
|
|
104
|
+
*/
|
|
105
|
+
function encodeExtension(oid, extnValue, critical = false) {
|
|
106
|
+
const fields = [objectIdentifier(oid)];
|
|
107
|
+
if (critical) fields.push(bool(true));
|
|
108
|
+
fields.push(octetString(extnValue));
|
|
109
|
+
return sequence(fields);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* DER-encode a {@linkcode BasicConstraints} value.
|
|
113
|
+
*
|
|
114
|
+
* @param input CA flag and optional pathLength.
|
|
115
|
+
* @returns DER SEQUENCE suitable for wrapping in an Extension OCTET STRING.
|
|
116
|
+
*/
|
|
117
|
+
function encodeBasicConstraints(input) {
|
|
118
|
+
const fields = [];
|
|
119
|
+
if (input.ca) fields.push(bool(true));
|
|
120
|
+
if (input.pathLength !== void 0) {
|
|
121
|
+
if (!input.ca) throw new Error("pathLength requires ca=true");
|
|
122
|
+
fields.push(integerFromNumber(input.pathLength));
|
|
123
|
+
}
|
|
124
|
+
return sequence(fields);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* DER-encode a Key Usage BIT STRING from an array of {@linkcode KeyUsage} flags.
|
|
128
|
+
*
|
|
129
|
+
* @param usages Flags to set in the bit string.
|
|
130
|
+
*/
|
|
131
|
+
function encodeKeyUsage(usages) {
|
|
132
|
+
return encodeKeyUsageExtension(usages);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* DER-encode a single {@linkcode SubjectAltName} GeneralName element.
|
|
136
|
+
*
|
|
137
|
+
* @param value The SAN entry to encode.
|
|
138
|
+
*/
|
|
139
|
+
function encodeSubjectAltName(value) {
|
|
140
|
+
switch (value.type) {
|
|
141
|
+
case "dns": return implicitPrimitiveContext(2, new TextEncoder().encode(value.value));
|
|
142
|
+
case "email": return implicitPrimitiveContext(1, new TextEncoder().encode(value.value));
|
|
143
|
+
case "uri": return implicitPrimitiveContext(6, new TextEncoder().encode(value.value));
|
|
144
|
+
case "srv": return implicitConstructedContext(0, sequence([objectIdentifier(OIDS.idOnDnsSrv), explicitContext(0, ia5String(value.value))]));
|
|
145
|
+
case "ip": return implicitPrimitiveContext(7, encodeIpAddress(value.value));
|
|
146
|
+
case "directoryName": return implicitConstructedContext(4, extractDirectoryNameContent(value.derHex));
|
|
147
|
+
case "unknown": return tlv(value.tag, value.value);
|
|
148
|
+
default: throw new Error(`Unhandled SubjectAltName type: ${String(value)}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* DER-encode an Extended Key Usage SEQUENCE OF OIDs.
|
|
153
|
+
*
|
|
154
|
+
* @param usages EKU purposes to encode.
|
|
155
|
+
*/
|
|
156
|
+
function encodeExtendedKeyUsage(usages) {
|
|
157
|
+
return sequence(usages.map((usage) => objectIdentifier(getExtendedKeyUsageOid(usage))));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* DER-encode an Authority Information Access SEQUENCE.
|
|
161
|
+
*
|
|
162
|
+
* @param entries AIA entries (OCSP, caIssuers, or custom) to encode.
|
|
163
|
+
*/
|
|
164
|
+
function encodeAuthorityInfoAccess(entries) {
|
|
165
|
+
return sequence(entries.map((entry) => sequence([objectIdentifier(getAuthorityInfoAccessMethodOid(entry.method)), implicitPrimitiveContext(6, new TextEncoder().encode(entry.uri))])));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* DER-encode a CRL Distribution Points SEQUENCE.
|
|
169
|
+
*
|
|
170
|
+
* @param points Distribution points to encode.
|
|
171
|
+
*/
|
|
172
|
+
function encodeCrlDistributionPoints(points) {
|
|
173
|
+
return sequence(points.map((point) => sequence(encodeDistributionPoint(point))));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* DER-encode a Name Constraints extension value.
|
|
177
|
+
*
|
|
178
|
+
* @param constraints Permitted and/or excluded subtrees.
|
|
179
|
+
*/
|
|
180
|
+
function encodeNameConstraints(constraints) {
|
|
181
|
+
const parts = [];
|
|
182
|
+
if (constraints.permittedSubtrees !== void 0 && constraints.permittedSubtrees.length > 0) parts.push(implicitConstructedContext(0, concatBytes(constraints.permittedSubtrees.map(encodeGeneralSubtree))));
|
|
183
|
+
if (constraints.excludedSubtrees !== void 0 && constraints.excludedSubtrees.length > 0) parts.push(implicitConstructedContext(1, concatBytes(constraints.excludedSubtrees.map(encodeGeneralSubtree))));
|
|
184
|
+
return sequence(parts);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* DER-encode a Certificate Policies extension value.
|
|
188
|
+
*
|
|
189
|
+
* @param policies Non-empty array of policy information entries.
|
|
190
|
+
*/
|
|
191
|
+
function encodeCertificatePolicies(policies) {
|
|
192
|
+
if (policies.length === 0) throw new Error("certificatePolicies must not be empty");
|
|
193
|
+
return sequence(policies.map(encodePolicyInformation));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* DER-encode a Policy Mappings extension value.
|
|
197
|
+
*
|
|
198
|
+
* @param mappings Non-empty array of issuer-to-subject policy pairs. Neither OID may be anyPolicy.
|
|
199
|
+
*/
|
|
200
|
+
function encodePolicyMappings(mappings) {
|
|
201
|
+
if (mappings.length === 0) throw new Error("policyMappings must not be empty");
|
|
202
|
+
return sequence(mappings.map((mapping) => {
|
|
203
|
+
validatePolicyOid(mapping.issuerDomainPolicy);
|
|
204
|
+
validatePolicyOid(mapping.subjectDomainPolicy);
|
|
205
|
+
if (mapping.issuerDomainPolicy === OIDS.anyPolicy || mapping.subjectDomainPolicy === OIDS.anyPolicy) throw new Error("policyMappings must not use anyPolicy");
|
|
206
|
+
return sequence([objectIdentifier(mapping.issuerDomainPolicy), objectIdentifier(mapping.subjectDomainPolicy)]);
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* DER-encode a Policy Constraints extension value.
|
|
211
|
+
*
|
|
212
|
+
* @param constraints At least one of `requireExplicitPolicy` or `inhibitPolicyMapping` must be set.
|
|
213
|
+
*/
|
|
214
|
+
function encodePolicyConstraints(constraints) {
|
|
215
|
+
const fields = [];
|
|
216
|
+
if (constraints.requireExplicitPolicy !== void 0) fields.push(implicitPrimitiveContext(0, encodeIntegerContent(constraints.requireExplicitPolicy)));
|
|
217
|
+
if (constraints.inhibitPolicyMapping !== void 0) fields.push(implicitPrimitiveContext(1, encodeIntegerContent(constraints.inhibitPolicyMapping)));
|
|
218
|
+
if (fields.length === 0) throw new Error("policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping");
|
|
219
|
+
return sequence(fields);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* DER-encode an Inhibit anyPolicy extension value (single INTEGER).
|
|
223
|
+
*
|
|
224
|
+
* @param input The skipCerts threshold.
|
|
225
|
+
*/
|
|
226
|
+
function encodeInhibitAnyPolicy(input) {
|
|
227
|
+
return integerFromNumber(input.skipCerts);
|
|
228
|
+
}
|
|
229
|
+
/** DER-encode a single PolicyInformation SEQUENCE. */
|
|
230
|
+
function encodePolicyInformation(policy) {
|
|
231
|
+
validatePolicyOid(policy.policyIdentifier);
|
|
232
|
+
const fields = [objectIdentifier(policy.policyIdentifier)];
|
|
233
|
+
if (policy.policyQualifiers !== void 0 && policy.policyQualifiers.length > 0) fields.push(sequence(policy.policyQualifiers.map(encodePolicyQualifierInfo)));
|
|
234
|
+
return sequence(fields);
|
|
235
|
+
}
|
|
236
|
+
/** DER-encode a single PolicyQualifierInfo SEQUENCE. */
|
|
237
|
+
function encodePolicyQualifierInfo(qualifier) {
|
|
238
|
+
switch (qualifier.type) {
|
|
239
|
+
case "cps": return sequence([objectIdentifier(OIDS.cpsPolicyQualifier), ia5String(qualifier.uri)]);
|
|
240
|
+
case "userNotice": return sequence([objectIdentifier(OIDS.userNoticePolicyQualifier), encodeUserNoticePolicyQualifierInfo(qualifier)]);
|
|
241
|
+
case "oid":
|
|
242
|
+
validateOid(qualifier.oid);
|
|
243
|
+
return sequence([objectIdentifier(qualifier.oid), new Uint8Array(qualifier.qualifierDer)]);
|
|
244
|
+
default: throw new Error(`Unhandled PolicyQualifierInfo type: ${String(qualifier)}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/** DER-encode a UserNotice qualifier SEQUENCE. */
|
|
248
|
+
function encodeUserNoticePolicyQualifierInfo(qualifier) {
|
|
249
|
+
const fields = [];
|
|
250
|
+
if (qualifier.noticeRef !== void 0) fields.push(encodePolicyNoticeReference(qualifier.noticeRef));
|
|
251
|
+
if (qualifier.explicitText !== void 0) fields.push(utf8String(qualifier.explicitText));
|
|
252
|
+
return sequence(fields);
|
|
253
|
+
}
|
|
254
|
+
/** DER-encode a NoticeReference SEQUENCE. */
|
|
255
|
+
function encodePolicyNoticeReference(reference) {
|
|
256
|
+
return sequence([utf8String(reference.organization), sequence(reference.noticeNumbers.map((noticeNumber) => integerFromNumber(noticeNumber)))]);
|
|
257
|
+
}
|
|
258
|
+
/** Encode an INTEGER and return only the content bytes (no TLV wrapper). */
|
|
259
|
+
function encodeIntegerContent(value) {
|
|
260
|
+
return readElement(integerFromNumber(value)).value;
|
|
261
|
+
}
|
|
262
|
+
/** DER-encode a GeneralSubtree SEQUENCE (base name only; min/max omitted per RFC 5280). */
|
|
263
|
+
function encodeGeneralSubtree(subtree) {
|
|
264
|
+
return sequence([encodeNameConstraintForm(subtree.base)]);
|
|
265
|
+
}
|
|
266
|
+
/** DER-encode the fields of a single DistributionPoint. */
|
|
267
|
+
function encodeDistributionPoint(point) {
|
|
268
|
+
if (point.crlIssuer !== void 0 && point.crlIssuer.length === 0) throw new Error("DistributionPoint crlIssuer must not be empty");
|
|
269
|
+
if (point.distributionPoint === void 0 && point.crlIssuer === void 0) throw new Error("DistributionPoint must contain distributionPoint or crlIssuer");
|
|
270
|
+
const fields = [];
|
|
271
|
+
if (point.distributionPoint !== void 0) fields.push(implicitConstructedContext(0, encodeDistributionPointName(point.distributionPoint)));
|
|
272
|
+
if (point.reasons !== void 0 && point.reasons.length > 0) fields.push(implicitPrimitiveContext(1, encodeDistributionPointReasonFlagsContent(point.reasons)));
|
|
273
|
+
if (point.crlIssuer !== void 0 && point.crlIssuer.length > 0) fields.push(implicitConstructedContext(2, concatBytes(point.crlIssuer.map(encodeSubjectAltName))));
|
|
274
|
+
return fields;
|
|
275
|
+
}
|
|
276
|
+
/** DER-encode a DistributionPointName (fullName or relativeName). */
|
|
277
|
+
function encodeDistributionPointName(name) {
|
|
278
|
+
if (name.fullName !== void 0 && name.relativeName !== void 0) throw new Error("DistributionPointName cannot contain both fullName and relativeName");
|
|
279
|
+
if (name.fullName !== void 0) {
|
|
280
|
+
if (name.fullName.length === 0) throw new Error("DistributionPointName fullName must not be empty");
|
|
281
|
+
return implicitConstructedContext(0, concatBytes(name.fullName.map(encodeSubjectAltName)));
|
|
282
|
+
}
|
|
283
|
+
if (name.relativeName !== void 0) {
|
|
284
|
+
const relativeName = encodeRelativeDistinguishedName(name.relativeName);
|
|
285
|
+
const relativeNameElement = readElement(relativeName);
|
|
286
|
+
return implicitConstructedContext(1, relativeName.slice(relativeNameElement.start, relativeNameElement.end));
|
|
287
|
+
}
|
|
288
|
+
throw new Error("DistributionPointName must contain fullName or relativeName");
|
|
289
|
+
}
|
|
290
|
+
/** DER-encode a NameConstraintForm as an implicit-tagged {@linkcode GeneralName}. */
|
|
291
|
+
function encodeNameConstraintForm(form) {
|
|
292
|
+
switch (form.type) {
|
|
293
|
+
case "dns": return implicitPrimitiveContext(2, new TextEncoder().encode(form.value));
|
|
294
|
+
case "email": return implicitPrimitiveContext(1, new TextEncoder().encode(form.value));
|
|
295
|
+
case "uri": return implicitPrimitiveContext(6, new TextEncoder().encode(form.value));
|
|
296
|
+
case "ip": return implicitPrimitiveContext(7, concatBytes([form.addressBytes, form.maskBytes]));
|
|
297
|
+
case "directoryName": return implicitConstructedContext(4, extractDirectoryNameContent(form.derHex));
|
|
298
|
+
default: throw new Error(`Unhandled NameConstraintForm type: ${String(form)}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/** Extract the content bytes from a hex-encoded DER SEQUENCE (strips the outer TLV). */
|
|
302
|
+
function extractDirectoryNameContent(derHex) {
|
|
303
|
+
const element = readRootElement(hexToBytes(derHex), { maxDepth: 64 });
|
|
304
|
+
if (element.tag !== 48) throw new Error("directoryName derHex must encode a DER SEQUENCE");
|
|
305
|
+
return new Uint8Array(element.value);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Resolve an {@linkcode ExtendedKeyUsage} to its dotted-decimal OID.
|
|
309
|
+
*
|
|
310
|
+
* @param usage Well-known string or custom OID object.
|
|
311
|
+
*/
|
|
312
|
+
function getExtendedKeyUsageOid(usage) {
|
|
313
|
+
if (typeof usage === "string") return EXTENDED_KEY_USAGE_OIDS[usage];
|
|
314
|
+
validateOid(usage.value);
|
|
315
|
+
return usage.value;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Map a dotted-decimal OID to an {@linkcode ExtendedKeyUsage} value.
|
|
319
|
+
*
|
|
320
|
+
* Returns a well-known string for recognized OIDs, or `{ type: 'oid', value }` otherwise.
|
|
321
|
+
*/
|
|
322
|
+
function parseExtendedKeyUsageOid(oid) {
|
|
323
|
+
switch (oid) {
|
|
324
|
+
case OIDS.serverAuth: return "serverAuth";
|
|
325
|
+
case OIDS.clientAuth: return "clientAuth";
|
|
326
|
+
case OIDS.codeSigning: return "codeSigning";
|
|
327
|
+
case OIDS.emailProtection: return "emailProtection";
|
|
328
|
+
case OIDS.timeStamping: return "timeStamping";
|
|
329
|
+
case OIDS.ocspSigning: return "ocspSigning";
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
type: "oid",
|
|
333
|
+
value: oid
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Resolve an {@linkcode AuthorityInfoAccessMethod} to its dotted-decimal OID.
|
|
338
|
+
*
|
|
339
|
+
* @param method Well-known string or custom OID object.
|
|
340
|
+
*/
|
|
341
|
+
function getAuthorityInfoAccessMethodOid(method) {
|
|
342
|
+
if (typeof method === "string") return AUTHORITY_INFO_ACCESS_METHOD_OIDS[method];
|
|
343
|
+
validateOid(method.value);
|
|
344
|
+
return method.value;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Map a dotted-decimal OID to an {@linkcode AuthorityInfoAccessMethod} value.
|
|
348
|
+
*
|
|
349
|
+
* Returns `'ocsp'` or `'caIssuers'` for recognized OIDs, or `{ type: 'oid', value }` otherwise.
|
|
350
|
+
*/
|
|
351
|
+
function parseAuthorityInfoAccessMethodOid(oid) {
|
|
352
|
+
switch (oid) {
|
|
353
|
+
case OIDS.ocspAccessMethod: return "ocsp";
|
|
354
|
+
case OIDS.caIssuersAccessMethod: return "caIssuers";
|
|
355
|
+
}
|
|
356
|
+
return {
|
|
357
|
+
type: "oid",
|
|
358
|
+
value: oid
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
/** Parse a dotted-decimal/colon-hex IP string to raw address bytes. */
|
|
362
|
+
function encodeIpAddress(input) {
|
|
363
|
+
return parseIpAddressToBytes(input);
|
|
364
|
+
}
|
|
365
|
+
/** @internal Compute the SKI as SHA-1 of the subjectPublicKey BIT STRING content. */
|
|
366
|
+
function buildSubjectKeyIdentifier(subjectPublicKeyInfo) {
|
|
367
|
+
const subjectPublicKey = readSequenceChildren(subjectPublicKeyInfo)[1];
|
|
368
|
+
if (subjectPublicKey === void 0 || subjectPublicKey.tag !== 3) throw new Error("SPKI missing subject public key bit string");
|
|
369
|
+
return sha1(subjectPublicKey.value.slice(1));
|
|
370
|
+
}
|
|
371
|
+
/** Throw if the string is not a valid dotted-decimal OID. */
|
|
372
|
+
function validateOid(oid) {
|
|
373
|
+
if (!/^\d+(?:\.\d+)+$/.test(oid)) throw new Error(`Invalid OID: ${oid}`);
|
|
374
|
+
}
|
|
375
|
+
/** Validate that a policy OID is syntactically valid. */
|
|
376
|
+
function validatePolicyOid(oid) {
|
|
377
|
+
validateOid(oid);
|
|
378
|
+
}
|
|
379
|
+
/** Encode and push an extension, rejecting duplicate OIDs. */
|
|
380
|
+
function pushExtension(encoded, seen, oid, value, critical = false) {
|
|
381
|
+
validateOid(oid);
|
|
382
|
+
if (seen.has(oid)) throw new Error(`Duplicate extension OID: ${oid}`);
|
|
383
|
+
seen.add(oid);
|
|
384
|
+
encoded.push(encodeExtension(oid, value, critical));
|
|
385
|
+
}
|
|
386
|
+
//#endregion
|
|
387
|
+
export { buildCertificateExtensions, buildRequestedExtensions, buildSubjectKeyIdentifier, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid };
|
package/dist/x509/name.d.ts
CHANGED
|
@@ -136,5 +136,4 @@ declare function encodeName(input: NameInput): Uint8Array;
|
|
|
136
136
|
*/
|
|
137
137
|
declare function encodeRelativeDistinguishedName(attributes: RelativeDistinguishedNameInput): Uint8Array;
|
|
138
138
|
//#endregion
|
|
139
|
-
export { NameAttribute, NameFieldKey, NameInput, NameObject, RelativeDistinguishedNameInput, encodeName, encodeRelativeDistinguishedName };
|
|
140
|
-
//# sourceMappingURL=name.d.ts.map
|
|
139
|
+
export { NameAttribute, NameFieldKey, NameInput, NameObject, RelativeDistinguishedNameInput, encodeName, encodeRelativeDistinguishedName };
|
package/dist/x509/name.js
CHANGED
|
@@ -1,2 +1,173 @@
|
|
|
1
|
-
import{objectIdentifier
|
|
2
|
-
|
|
1
|
+
import { objectIdentifier, sequence, setOf } from "../internal/asn1/der.js";
|
|
2
|
+
import { NAME_FIELD_DEFINITIONS, NAME_OBJECT_ORDER, nameFieldKeyFromOid } from "../internal/x509/name-fields.js";
|
|
3
|
+
//#region src/x509/name.ts
|
|
4
|
+
/**
|
|
5
|
+
* Distinguished-name input and DER encoding helpers.
|
|
6
|
+
*
|
|
7
|
+
* Encodes X.509 `Name` values used for certificate subjects, issuers,
|
|
8
|
+
* and CSR subjects.\
|
|
9
|
+
* A `Name` is a DER `SEQUENCE` of RelativeDistinguishedNames (RDNs), and each
|
|
10
|
+
* RDN is a `SET OF` one or more name attributes.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}
|
|
13
|
+
*
|
|
14
|
+
* Use {@linkcode encodeName} for the common case where each attribute occupies
|
|
15
|
+
* its own RDN.\
|
|
16
|
+
* Use {@linkcode encodeRelativeDistinguishedName} when you need one
|
|
17
|
+
* multi-valued RDN.
|
|
18
|
+
*
|
|
19
|
+
* `NameObject` favors convenience: populated fields are emitted in the
|
|
20
|
+
* canonical order from `NAME_OBJECT_ORDER`.\
|
|
21
|
+
* `NameAttribute` arrays favor control: caller order is preserved, but each
|
|
22
|
+
* entry still becomes its own single-attribute RDN.
|
|
23
|
+
*
|
|
24
|
+
* Attribute OIDs and ASN.1 string encodings come from
|
|
25
|
+
* `NAME_FIELD_DEFINITIONS`.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { encodeName } from 'micro509/x509';
|
|
30
|
+
*
|
|
31
|
+
* const subjectDer = encodeName({
|
|
32
|
+
* country: 'US',
|
|
33
|
+
* organization: 'ACME Inc',
|
|
34
|
+
* commonName: 'example.com',
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // DER for: C=US, O=ACME Inc, CN=example.com
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { encodeName } from 'micro509/x509';
|
|
43
|
+
*
|
|
44
|
+
* const subjectDer = encodeName([
|
|
45
|
+
* { type: 'commonName', value: 'example.com' },
|
|
46
|
+
* { type: 'organization', value: 'ACME Inc' },
|
|
47
|
+
* { type: 'country', value: 'US' },
|
|
48
|
+
* ]);
|
|
49
|
+
*
|
|
50
|
+
* // preserves caller-supplied attribute order
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { encodeRelativeDistinguishedName } from 'micro509/x509';
|
|
56
|
+
*
|
|
57
|
+
* const rdnDer = encodeRelativeDistinguishedName([
|
|
58
|
+
* { type: 'commonName', value: 'example.com' },
|
|
59
|
+
* { type: 'serialNumber', value: 'device-7' },
|
|
60
|
+
* ]);
|
|
61
|
+
*
|
|
62
|
+
* // one multi-valued RDN containing CN + serialNumber
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @module
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
68
|
+
* DER-encodes an X.509 `Name`.
|
|
69
|
+
*
|
|
70
|
+
* Returns a DER `SEQUENCE` of RelativeDistinguishedNames (RDNs).\
|
|
71
|
+
* Each RDN emitted by this helper contains exactly one name attribute.
|
|
72
|
+
*
|
|
73
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}
|
|
74
|
+
*
|
|
75
|
+
* {@linkcode NameObject} input emits populated fields in the canonical order from `NAME_OBJECT_ORDER`.\
|
|
76
|
+
* {@linkcode NameAttribute} array input preserves caller-supplied ordering,
|
|
77
|
+
* but each entry still becomes its own single-attribute RDN.
|
|
78
|
+
*
|
|
79
|
+
* Attribute OIDs and ASN.1 string encodings come from `NAME_FIELD_DEFINITIONS`.\
|
|
80
|
+
* Empty strings and `undefined` fields are ignored when the input is a {@linkcode NameObject}.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const der = encodeName({ country: 'US', commonName: 'example.com' });
|
|
85
|
+
*
|
|
86
|
+
* // emits two single-attribute RDNs: C=US, then CN=example.com
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const der = encodeName([
|
|
92
|
+
* { type: 'country', value: 'US' },
|
|
93
|
+
* { type: 'commonName', value: 'example.com' },
|
|
94
|
+
* ]);
|
|
95
|
+
*
|
|
96
|
+
* // preserves caller order: C first, then CN
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @param input Name fields in convenience-object form or caller-ordered attribute form.
|
|
100
|
+
* @returns DER-encoded X.509 `Name` bytes.
|
|
101
|
+
* @throws {Error} If the input produces no attributes, contains an unsupported field key, or uses an invalid country code.
|
|
102
|
+
*/
|
|
103
|
+
function encodeName(input) {
|
|
104
|
+
const attributes = isNameAttributes(input) ? input : nameObjectToAttributes(input);
|
|
105
|
+
if (attributes.length === 0) return sequence([]);
|
|
106
|
+
return sequence(attributes.map(encodeNameAttributeAsSet));
|
|
107
|
+
}
|
|
108
|
+
/** True when a {@linkcode NameInput} resolves to zero attributes (empty DN). */
|
|
109
|
+
function isNameInputEmpty(input) {
|
|
110
|
+
return (isNameAttributes(input) ? input : nameObjectToAttributes(input)).length === 0;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* DER-encodes a single RelativeDistinguishedName (RDN).
|
|
114
|
+
*
|
|
115
|
+
* Returns a DER `SET OF` name attributes for one X.509 name segment.\
|
|
116
|
+
* Use this when you need a multi-valued RDN.
|
|
117
|
+
*
|
|
118
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}
|
|
119
|
+
*
|
|
120
|
+
* Attribute OIDs and ASN.1 string encodings come from
|
|
121
|
+
* `NAME_FIELD_DEFINITIONS`.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const rdn = encodeRelativeDistinguishedName([
|
|
126
|
+
* { type: 'commonName', value: 'example.com' },
|
|
127
|
+
* { type: 'serialNumber', value: 'device-7' },
|
|
128
|
+
* ]);
|
|
129
|
+
*
|
|
130
|
+
* // emits one RDN with both attributes in the same SET
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @param attributes Attribute list to encode inside one RDN.
|
|
134
|
+
* @returns DER-encoded RelativeDistinguishedName bytes.
|
|
135
|
+
* @throws {Error} If the attribute list is empty, contains an unsupported field key, or uses an invalid country code.
|
|
136
|
+
*/
|
|
137
|
+
function encodeRelativeDistinguishedName(attributes) {
|
|
138
|
+
if (attributes.length === 0) throw new Error("Relative distinguished name must contain at least one attribute");
|
|
139
|
+
return setOf(attributes.map(encodeNameAttribute));
|
|
140
|
+
}
|
|
141
|
+
/** Narrows a {@linkcode NameInput} to the ordered-attribute-array form. */
|
|
142
|
+
function isNameAttributes(input) {
|
|
143
|
+
return Array.isArray(input);
|
|
144
|
+
}
|
|
145
|
+
/** Wraps a single attribute in a SET OF for the Name SEQUENCE. */
|
|
146
|
+
function encodeNameAttributeAsSet(attribute) {
|
|
147
|
+
return setOf([encodeNameAttribute(attribute)]);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* DER-encodes one name attribute SEQUENCE.
|
|
151
|
+
*
|
|
152
|
+
* Throws for unsupported field keys and invalid country codes.
|
|
153
|
+
*/
|
|
154
|
+
function encodeNameAttribute(attribute) {
|
|
155
|
+
const definition = NAME_FIELD_DEFINITIONS[attribute.type];
|
|
156
|
+
if (definition === void 0) throw new Error(`Unsupported name field: ${attribute.type}`);
|
|
157
|
+
if (attribute.type === "country" && attribute.value.length !== 2) throw new Error("Country must be a 2-character code");
|
|
158
|
+
return sequence([objectIdentifier(definition.oid), definition.encode(attribute.value)]);
|
|
159
|
+
}
|
|
160
|
+
/** Converts a {@linkcode NameObject} to ordered attributes per `NAME_OBJECT_ORDER`. */
|
|
161
|
+
function nameObjectToAttributes(input) {
|
|
162
|
+
const attributes = [];
|
|
163
|
+
for (const key of NAME_OBJECT_ORDER) {
|
|
164
|
+
const value = input[key];
|
|
165
|
+
if (typeof value === "string" && value.length > 0) attributes.push({
|
|
166
|
+
type: key,
|
|
167
|
+
value
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return attributes;
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
export { encodeName, encodeRelativeDistinguishedName, isNameInputEmpty, nameFieldKeyFromOid };
|