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/parse.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
|
|
1
|
+
import { DECODE_REFUSAL_CODES as e, failureResult as t, rethrowIfInvariant as n, successResult as r } from "../result/result.js";
|
|
2
|
+
import { decodeFailureResult as i, decodeRefusalOf as a } from "../internal/asn1/decode-refusal.js";
|
|
3
|
+
import { optionalBytesEqual as o, readElement as s, readRootElement as c, readSequenceChildren as l } from "../internal/asn1/der.js";
|
|
4
|
+
import { childrenOf as u, decodeBoolean as d, decodeIntegerNumber as f, decodeNonNegativeIntegerNumber as p, decodeObjectIdentifier as m, decodeString as h, decodeVisibleString as g, extractBitStringValue as _, parseTime as v, requireElement as y, toHex as b } from "../internal/asn1/asn1.js";
|
|
5
|
+
import { OIDS as x } from "../internal/asn1/oids2.js";
|
|
6
|
+
import { pemDecodeOrThrow as S, splitPemBlocksOrThrow as C } from "../pem/pem.js";
|
|
7
|
+
import { derivePublicKey as w, exportSpkiDer as T, importSpkiDer as E, importSpkiDerOrThrow as D } from "../keys/keys.js";
|
|
8
|
+
import { describePublicKeyAlgorithm as O, describeSignatureAlgorithm as k } from "../internal/crypto/algorithm-names.js";
|
|
9
|
+
import { readDirectoryNameTlv as A } from "../internal/x509/directory-name.js";
|
|
10
|
+
import { parseDistributionPointReasonFlagsContent as j, parseKeyUsageExtension as M } from "../internal/x509/extension-bits.js";
|
|
11
|
+
import { decodeSrvName as N, otherNameValueDer as P, parseGeneralName as F, parseGeneralNames as I, readOtherName as L } from "../internal/x509/general-name.js";
|
|
12
|
+
import { nameFieldKeyFromOid as R } from "../internal/x509/name-fields.js";
|
|
13
|
+
import { parseAuthorityInfoAccessMethodOid as z, parseExtendedKeyUsageOid as B } from "./extensions.js";
|
|
14
|
+
import { decodeAndApplyKnownExtension as V } from "../internal/x509/extension-registry.js";
|
|
15
|
+
// src/x509/parse.ts
|
|
14
16
|
/**
|
|
15
17
|
* Certificate and CSR parse boundary.
|
|
16
18
|
*
|
|
@@ -21,8 +23,6 @@ import { decodeAndApplyKnownExtension as M } from "../internal/x509/extension-re
|
|
|
21
23
|
*
|
|
22
24
|
* @module
|
|
23
25
|
*/
|
|
24
|
-
/** Shared UTF-8 decoder for IA5String / UTF8String values. */
|
|
25
|
-
const N = new TextDecoder();
|
|
26
26
|
/**
|
|
27
27
|
* Identity helper that narrows the type of a custom {@linkcode ExtensionDecoder} literal.
|
|
28
28
|
*
|
|
@@ -53,67 +53,71 @@ function defineExtensionDecoderMap(e) {
|
|
|
53
53
|
* @param options Custom extension decoders to apply during parsing.
|
|
54
54
|
*/
|
|
55
55
|
function parseCertificateDerOrThrow(e, t) {
|
|
56
|
-
let n =
|
|
56
|
+
let n = l(e, { maxDepth: 64 });
|
|
57
57
|
if (n.length !== 3) throw Error("Malformed Certificate");
|
|
58
|
-
let
|
|
59
|
-
assertMatchingCertificateSignatureAlgorithms(
|
|
60
|
-
let
|
|
58
|
+
let r = y(n[0], "TBSCertificate"), i = y(n[1], "signatureAlgorithm"), a = y(n[2], "signatureValue"), o = parseTbsCertificateFields(e, r), s = parseExtensionContainer(e, o.extensions), c = parseValidity(e, o.validity), u = parseSubjectPublicKeyInfo(e, o.subjectPublicKeyInfo), d = parseAlgorithmIdentifier(e, o.tbsSignatureAlgorithm), f = parseAlgorithmIdentifier(e, i);
|
|
59
|
+
assertMatchingCertificateSignatureAlgorithms(d, f);
|
|
60
|
+
let p = parseCustomExtensions(s.all, t);
|
|
61
61
|
return {
|
|
62
62
|
der: new Uint8Array(e),
|
|
63
|
-
version:
|
|
64
|
-
serialNumberHex:
|
|
65
|
-
tbsCertificateDer: e.slice(
|
|
66
|
-
subjectPublicKeyInfoDer: e.slice(
|
|
67
|
-
signatureValue:
|
|
68
|
-
issuer: parseName(e,
|
|
69
|
-
subject: parseName(e,
|
|
70
|
-
notBefore:
|
|
71
|
-
notAfter:
|
|
72
|
-
signatureAlgorithmOid:
|
|
73
|
-
signatureAlgorithmName:
|
|
74
|
-
...
|
|
75
|
-
publicKeyAlgorithmOid:
|
|
76
|
-
publicKeyAlgorithmName:
|
|
77
|
-
...
|
|
78
|
-
...
|
|
79
|
-
extensions:
|
|
80
|
-
...
|
|
81
|
-
...
|
|
82
|
-
...
|
|
83
|
-
...
|
|
84
|
-
...
|
|
85
|
-
...
|
|
86
|
-
...
|
|
87
|
-
...
|
|
88
|
-
...
|
|
89
|
-
...
|
|
90
|
-
...
|
|
91
|
-
...
|
|
92
|
-
...
|
|
93
|
-
...
|
|
94
|
-
...
|
|
63
|
+
version: o.version,
|
|
64
|
+
serialNumberHex: b(o.serialNumber.value),
|
|
65
|
+
tbsCertificateDer: e.slice(r.start - r.headerLength, r.end),
|
|
66
|
+
subjectPublicKeyInfoDer: e.slice(o.subjectPublicKeyInfo.start - o.subjectPublicKeyInfo.headerLength, o.subjectPublicKeyInfo.end),
|
|
67
|
+
signatureValue: _(a),
|
|
68
|
+
issuer: parseName(e, o.issuer),
|
|
69
|
+
subject: parseName(e, o.subject),
|
|
70
|
+
notBefore: c.notBefore,
|
|
71
|
+
notAfter: c.notAfter,
|
|
72
|
+
signatureAlgorithmOid: f.oid,
|
|
73
|
+
signatureAlgorithmName: k(f.oid, f.parametersDer),
|
|
74
|
+
...f.parametersDer === void 0 ? {} : { signatureAlgorithmParametersDer: f.parametersDer },
|
|
75
|
+
publicKeyAlgorithmOid: u.oid,
|
|
76
|
+
publicKeyAlgorithmName: O(u.oid, u.parametersOid),
|
|
77
|
+
...u.parametersDer === void 0 ? {} : { publicKeyAlgorithmParametersDer: u.parametersDer },
|
|
78
|
+
...u.parametersOid === void 0 ? {} : { publicKeyParametersOid: u.parametersOid },
|
|
79
|
+
extensions: s.all,
|
|
80
|
+
...s.basicConstraints === void 0 ? {} : { basicConstraints: s.basicConstraints },
|
|
81
|
+
...s.keyUsage === void 0 ? {} : { keyUsage: s.keyUsage },
|
|
82
|
+
...s.extendedKeyUsage === void 0 ? {} : { extendedKeyUsage: s.extendedKeyUsage },
|
|
83
|
+
...s.subjectAltNames === void 0 ? {} : { subjectAltNames: s.subjectAltNames },
|
|
84
|
+
...s.issuerAltNames === void 0 ? {} : { issuerAltNames: s.issuerAltNames },
|
|
85
|
+
...s.nameConstraints === void 0 ? {} : { nameConstraints: s.nameConstraints },
|
|
86
|
+
...s.certificatePolicies === void 0 ? {} : { certificatePolicies: s.certificatePolicies },
|
|
87
|
+
...s.policyMappings === void 0 ? {} : { policyMappings: s.policyMappings },
|
|
88
|
+
...s.policyConstraints === void 0 ? {} : { policyConstraints: s.policyConstraints },
|
|
89
|
+
...s.inhibitAnyPolicy === void 0 ? {} : { inhibitAnyPolicy: s.inhibitAnyPolicy },
|
|
90
|
+
...accessExtensionFields(s),
|
|
91
|
+
...p.decodedExtensions === void 0 ? {} : { decodedExtensions: p.decodedExtensions },
|
|
92
|
+
...p.decodedExtensionMap === void 0 ? {} : { decodedExtensionMap: p.decodedExtensionMap },
|
|
93
|
+
...s.subjectKeyIdentifier === void 0 ? {} : { subjectKeyIdentifier: s.subjectKeyIdentifier },
|
|
94
|
+
...s.authorityKeyIdentifier === void 0 ? {} : { authorityKeyIdentifier: s.authorityKeyIdentifier }
|
|
95
95
|
};
|
|
96
96
|
}
|
|
97
97
|
function parseCustomExtensions(e, t) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
try {
|
|
99
|
+
return {
|
|
100
|
+
...t?.decoders === void 0 ? {} : { decodedExtensions: decodeExtensions(e, t.decoders) },
|
|
101
|
+
...t?.decoderMap === void 0 ? {} : { decodedExtensionMap: decodeExtensionMap(e, t.decoderMap) }
|
|
102
|
+
};
|
|
103
|
+
} catch (e) {
|
|
104
|
+
throw Error(e instanceof Error ? e.message : "Custom extension decoder failed", { cause: e });
|
|
105
|
+
}
|
|
102
106
|
}
|
|
103
107
|
/** Extracts and validates the structural fields of a TBSCertificate sequence. */
|
|
104
108
|
function parseTbsCertificateFields(e, t) {
|
|
105
|
-
let n =
|
|
106
|
-
if (
|
|
107
|
-
let
|
|
109
|
+
let n = u(e, t), r = parseCertificateVersionField(e, n), i = r.version, a = r.nextIndex, o = y(n[a], "serialNumber");
|
|
110
|
+
if (o.tag !== 2) throw Error("serialNumber must use INTEGER");
|
|
111
|
+
let s = validateOptionalTbsCertificateFields(n, a + 6, i);
|
|
108
112
|
return {
|
|
109
|
-
version:
|
|
110
|
-
serialNumber:
|
|
111
|
-
tbsSignatureAlgorithm:
|
|
112
|
-
issuer:
|
|
113
|
-
validity:
|
|
114
|
-
subject:
|
|
115
|
-
subjectPublicKeyInfo:
|
|
116
|
-
...n[
|
|
113
|
+
version: i,
|
|
114
|
+
serialNumber: o,
|
|
115
|
+
tbsSignatureAlgorithm: y(n[a + 1], "TBSCertificate signature"),
|
|
116
|
+
issuer: y(n[a + 2], "issuer"),
|
|
117
|
+
validity: y(n[a + 3], "validity"),
|
|
118
|
+
subject: y(n[a + 4], "subject"),
|
|
119
|
+
subjectPublicKeyInfo: y(n[a + 5], "subjectPublicKeyInfo"),
|
|
120
|
+
...n[s - 1]?.tag === 163 ? { extensions: n[s - 1] } : {}
|
|
117
121
|
};
|
|
118
122
|
}
|
|
119
123
|
function parseCertificateVersionField(e, t) {
|
|
@@ -122,12 +126,12 @@ function parseCertificateVersionField(e, t) {
|
|
|
122
126
|
version: 1,
|
|
123
127
|
nextIndex: 0
|
|
124
128
|
};
|
|
125
|
-
let r =
|
|
126
|
-
if (r.length !== 1 ||
|
|
127
|
-
let
|
|
128
|
-
if (
|
|
129
|
+
let r = u(e, n), i = y(r[0], "version INTEGER");
|
|
130
|
+
if (r.length !== 1 || i.tag !== 2) throw Error("version must use INTEGER");
|
|
131
|
+
let a = f(i.value) + 1;
|
|
132
|
+
if (a < 1 || a > 3) throw Error(`Unsupported certificate version: ${String(a)}`);
|
|
129
133
|
return {
|
|
130
|
-
version:
|
|
134
|
+
version: a,
|
|
131
135
|
nextIndex: 1
|
|
132
136
|
};
|
|
133
137
|
}
|
|
@@ -137,11 +141,11 @@ function validateOptionalTbsCertificateFields(e, t, n) {
|
|
|
137
141
|
if (n !== 3) throw Error("extensions require certificate version 3");
|
|
138
142
|
r += 1;
|
|
139
143
|
}
|
|
140
|
-
if (r !== e.length) throw Error(`Unsupported TBSCertificate field tag: ${String(
|
|
144
|
+
if (r !== e.length) throw Error(`Unsupported TBSCertificate field tag: ${String(y(e[r], "TBSCertificate field").tag)}`);
|
|
141
145
|
return r;
|
|
142
146
|
}
|
|
143
147
|
function validateCertificateUniqueId(e, t, n) {
|
|
144
|
-
let r =
|
|
148
|
+
let r = y(e, n);
|
|
145
149
|
if (t < 2) throw Error(`${n} requires certificate version 2 or 3`);
|
|
146
150
|
validateImplicitBitStringContent(r.value, n);
|
|
147
151
|
}
|
|
@@ -161,11 +165,11 @@ function validateCertificateUniqueId(e, t, n) {
|
|
|
161
165
|
* @param der Raw DER bytes of an X.509 certificate.
|
|
162
166
|
* @param options Custom extension decoders to apply during parsing.
|
|
163
167
|
*/
|
|
164
|
-
function parseCertificateDer(
|
|
168
|
+
function parseCertificateDer(t, a) {
|
|
165
169
|
try {
|
|
166
|
-
return
|
|
167
|
-
} catch (
|
|
168
|
-
return
|
|
170
|
+
return r(parseCertificateDerOrThrow(t, a));
|
|
171
|
+
} catch (t) {
|
|
172
|
+
return n(t), i(t, e, "Malformed certificate");
|
|
169
173
|
}
|
|
170
174
|
}
|
|
171
175
|
/**
|
|
@@ -187,7 +191,7 @@ function parseCertificateDer(e, t) {
|
|
|
187
191
|
* @param options Custom extension decoders to apply during parsing.
|
|
188
192
|
*/
|
|
189
193
|
function parseCertificatePemOrThrow(e, t) {
|
|
190
|
-
return parseCertificateDerOrThrow(
|
|
194
|
+
return parseCertificateDerOrThrow(S("CERTIFICATE", e), t);
|
|
191
195
|
}
|
|
192
196
|
/**
|
|
193
197
|
* Decode a PEM-encoded X.509 certificate into a {@linkcode ParsedCertificate}.
|
|
@@ -201,11 +205,11 @@ function parseCertificatePemOrThrow(e, t) {
|
|
|
201
205
|
* @param pem PEM string with a CERTIFICATE block.
|
|
202
206
|
* @param options Custom extension decoders to apply during parsing.
|
|
203
207
|
*/
|
|
204
|
-
function parseCertificatePem(
|
|
208
|
+
function parseCertificatePem(t, a) {
|
|
205
209
|
try {
|
|
206
|
-
return
|
|
207
|
-
} catch (
|
|
208
|
-
return
|
|
210
|
+
return r(parseCertificatePemOrThrow(t, a));
|
|
211
|
+
} catch (t) {
|
|
212
|
+
return n(t), i(t, e, "Malformed certificate");
|
|
209
213
|
}
|
|
210
214
|
}
|
|
211
215
|
/** Normalizes a PEM bundle or single DER certificate source into parsed certificates. */
|
|
@@ -217,69 +221,84 @@ function parseCertificateFromSource(e, t) {
|
|
|
217
221
|
return typeof e == "string" ? parseCertificatePemOrThrow(e, t) : hasParsedCertificateShape(e) ? e : parseCertificateDerOrThrow(new Uint8Array(e), t);
|
|
218
222
|
}
|
|
219
223
|
/**
|
|
220
|
-
* Decode a PEM bundle containing one or more certificates.
|
|
224
|
+
* Decode a PEM bundle containing one or more certificates, throwing on malformed input.
|
|
221
225
|
*
|
|
222
226
|
* Non-CERTIFICATE blocks (e.g. private keys) are silently skipped.
|
|
223
227
|
*
|
|
224
228
|
* @param pemBundle PEM text that may contain multiple CERTIFICATE blocks.
|
|
225
229
|
* @param options Custom extension decoders to apply during parsing.
|
|
226
230
|
*/
|
|
227
|
-
function
|
|
231
|
+
function parseCertificateChainPemOrThrow(e, t) {
|
|
228
232
|
return parseCertificatesFromPemBlocks(e, t);
|
|
229
233
|
}
|
|
230
234
|
/**
|
|
235
|
+
* Decode a PEM bundle containing one or more certificates.
|
|
236
|
+
*
|
|
237
|
+
* Non-CERTIFICATE blocks (e.g. private keys) are silently skipped. Returns a
|
|
238
|
+
* typed `malformed` failure for invalid PEM or certificate DER.
|
|
239
|
+
*
|
|
240
|
+
* @param pemBundle PEM text that may contain multiple CERTIFICATE blocks.
|
|
241
|
+
* @param options Custom extension decoders to apply during parsing.
|
|
242
|
+
*/
|
|
243
|
+
function parseCertificateChainPem(t, a) {
|
|
244
|
+
try {
|
|
245
|
+
return r(parseCertificateChainPemOrThrow(t, a));
|
|
246
|
+
} catch (t) {
|
|
247
|
+
return n(t), i(t, e, "Malformed certificate chain");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
231
251
|
* Decode a DER-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
|
|
232
252
|
*
|
|
233
253
|
* @param der Raw DER bytes of a PKCS#10 certificate signing request.
|
|
234
254
|
* @param options Custom extension decoders to apply during parsing.
|
|
235
255
|
*/
|
|
236
256
|
function parseCertificateSigningRequestDerOrThrow(e, t) {
|
|
237
|
-
let n =
|
|
257
|
+
let n = l(e, { maxDepth: 64 });
|
|
238
258
|
if (n.length !== 3) throw Error("Malformed CertificationRequest");
|
|
239
|
-
let
|
|
259
|
+
let r = y(n[0], "CertificationRequestInfo"), i = y(n[1], "signatureAlgorithm"), a = y(n[2], "signatureValue"), o = parseCertificationRequestInfoFields(e, r), s = parseRequestedExtensions(e, o.attributes), c = parseSubjectPublicKeyInfo(e, o.subjectPublicKeyInfo), u = parseAlgorithmIdentifier(e, i), d = parseCustomExtensions(s.all, t);
|
|
240
260
|
return {
|
|
241
|
-
version:
|
|
242
|
-
certificationRequestInfoDer: e.slice(
|
|
243
|
-
subjectPublicKeyInfoDer: e.slice(
|
|
244
|
-
signatureValue:
|
|
245
|
-
subject: parseName(e,
|
|
246
|
-
signatureAlgorithmOid:
|
|
247
|
-
signatureAlgorithmName:
|
|
248
|
-
...
|
|
249
|
-
publicKeyAlgorithmOid:
|
|
250
|
-
publicKeyAlgorithmName:
|
|
251
|
-
...
|
|
252
|
-
...
|
|
253
|
-
requestedExtensions:
|
|
254
|
-
...
|
|
255
|
-
...
|
|
256
|
-
...
|
|
257
|
-
...
|
|
258
|
-
...
|
|
259
|
-
...
|
|
260
|
-
...
|
|
261
|
-
...
|
|
262
|
-
...
|
|
263
|
-
...
|
|
264
|
-
...
|
|
265
|
-
...
|
|
266
|
-
...p.decodedExtensionMap === void 0 ? {} : { decodedExtensionMap: p.decodedExtensionMap }
|
|
261
|
+
version: o.version,
|
|
262
|
+
certificationRequestInfoDer: e.slice(r.start - r.headerLength, r.end),
|
|
263
|
+
subjectPublicKeyInfoDer: e.slice(o.subjectPublicKeyInfo.start - o.subjectPublicKeyInfo.headerLength, o.subjectPublicKeyInfo.end),
|
|
264
|
+
signatureValue: _(a),
|
|
265
|
+
subject: parseName(e, o.subject),
|
|
266
|
+
signatureAlgorithmOid: u.oid,
|
|
267
|
+
signatureAlgorithmName: k(u.oid, u.parametersDer),
|
|
268
|
+
...u.parametersDer === void 0 ? {} : { signatureAlgorithmParametersDer: u.parametersDer },
|
|
269
|
+
publicKeyAlgorithmOid: c.oid,
|
|
270
|
+
publicKeyAlgorithmName: O(c.oid, c.parametersOid),
|
|
271
|
+
...c.parametersDer === void 0 ? {} : { publicKeyAlgorithmParametersDer: c.parametersDer },
|
|
272
|
+
...c.parametersOid === void 0 ? {} : { publicKeyParametersOid: c.parametersOid },
|
|
273
|
+
requestedExtensions: s.all,
|
|
274
|
+
...s.basicConstraints === void 0 ? {} : { basicConstraints: s.basicConstraints },
|
|
275
|
+
...s.keyUsage === void 0 ? {} : { keyUsage: s.keyUsage },
|
|
276
|
+
...s.extendedKeyUsage === void 0 ? {} : { extendedKeyUsage: s.extendedKeyUsage },
|
|
277
|
+
...s.subjectAltNames === void 0 ? {} : { subjectAltNames: s.subjectAltNames },
|
|
278
|
+
...s.nameConstraints === void 0 ? {} : { nameConstraints: s.nameConstraints },
|
|
279
|
+
...s.certificatePolicies === void 0 ? {} : { certificatePolicies: s.certificatePolicies },
|
|
280
|
+
...s.policyMappings === void 0 ? {} : { policyMappings: s.policyMappings },
|
|
281
|
+
...s.policyConstraints === void 0 ? {} : { policyConstraints: s.policyConstraints },
|
|
282
|
+
...s.inhibitAnyPolicy === void 0 ? {} : { inhibitAnyPolicy: s.inhibitAnyPolicy },
|
|
283
|
+
...accessExtensionFields(s),
|
|
284
|
+
...d.decodedExtensions === void 0 ? {} : { decodedExtensions: d.decodedExtensions },
|
|
285
|
+
...d.decodedExtensionMap === void 0 ? {} : { decodedExtensionMap: d.decodedExtensionMap }
|
|
267
286
|
};
|
|
268
287
|
}
|
|
269
288
|
function parseCertificationRequestInfoFields(e, t) {
|
|
270
|
-
let n =
|
|
271
|
-
if (n.length
|
|
272
|
-
let r =
|
|
289
|
+
let n = u(e, t);
|
|
290
|
+
if (n.length !== 4) throw Error("Malformed CertificationRequestInfo");
|
|
291
|
+
let r = y(n[0], "version");
|
|
273
292
|
if (r.tag !== 2) throw Error("version must use INTEGER");
|
|
274
|
-
let
|
|
275
|
-
if (
|
|
276
|
-
let
|
|
277
|
-
if (
|
|
293
|
+
let i = f(r.value) + 1;
|
|
294
|
+
if (i !== 1) throw Error(`Unsupported CertificationRequestInfo version: ${String(i)}`);
|
|
295
|
+
let a = y(n[3], "attributes");
|
|
296
|
+
if (a.tag !== 160) throw Error("CertificationRequestInfo attributes must use [0]");
|
|
278
297
|
return {
|
|
279
|
-
version:
|
|
280
|
-
subject:
|
|
281
|
-
subjectPublicKeyInfo:
|
|
282
|
-
|
|
298
|
+
version: i,
|
|
299
|
+
subject: y(n[1], "subject"),
|
|
300
|
+
subjectPublicKeyInfo: y(n[2], "subjectPublicKeyInfo"),
|
|
301
|
+
attributes: a
|
|
283
302
|
};
|
|
284
303
|
}
|
|
285
304
|
/**
|
|
@@ -288,15 +307,15 @@ function parseCertificationRequestInfoFields(e, t) {
|
|
|
288
307
|
* @param der Raw DER bytes of a PKCS#10 certificate signing request.
|
|
289
308
|
* @param options Custom extension decoders to apply during parsing.
|
|
290
309
|
*/
|
|
291
|
-
function parseCertificateSigningRequestDer(
|
|
310
|
+
function parseCertificateSigningRequestDer(t, a) {
|
|
292
311
|
try {
|
|
293
|
-
return
|
|
294
|
-
} catch (
|
|
295
|
-
return
|
|
312
|
+
return r(parseCertificateSigningRequestDerOrThrow(t, a));
|
|
313
|
+
} catch (t) {
|
|
314
|
+
return n(t), i(t, e, "Malformed certificate signing request");
|
|
296
315
|
}
|
|
297
316
|
}
|
|
298
317
|
function parseCertificatesFromPemBlocks(e, t) {
|
|
299
|
-
return
|
|
318
|
+
return C(e).filter((e) => e.label === "CERTIFICATE").map((e) => parseCertificateDerOrThrow(e.bytes, t));
|
|
300
319
|
}
|
|
301
320
|
function hasParsedCertificateShape(e) {
|
|
302
321
|
return "subjectPublicKeyInfoDer" in e;
|
|
@@ -308,7 +327,7 @@ function hasParsedCertificateShape(e) {
|
|
|
308
327
|
* @param options Custom extension decoders to apply during parsing.
|
|
309
328
|
*/
|
|
310
329
|
function parseCertificateSigningRequestPemOrThrow(e, t) {
|
|
311
|
-
return parseCertificateSigningRequestDerOrThrow(
|
|
330
|
+
return parseCertificateSigningRequestDerOrThrow(S("CERTIFICATE REQUEST", e), t);
|
|
312
331
|
}
|
|
313
332
|
/**
|
|
314
333
|
* Decode a PEM-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
|
|
@@ -316,11 +335,11 @@ function parseCertificateSigningRequestPemOrThrow(e, t) {
|
|
|
316
335
|
* @param pem PEM string with a CERTIFICATE REQUEST block.
|
|
317
336
|
* @param options Custom extension decoders to apply during parsing.
|
|
318
337
|
*/
|
|
319
|
-
function parseCertificateSigningRequestPem(
|
|
338
|
+
function parseCertificateSigningRequestPem(t, a) {
|
|
320
339
|
try {
|
|
321
|
-
return
|
|
322
|
-
} catch (
|
|
323
|
-
return
|
|
340
|
+
return r(parseCertificateSigningRequestPemOrThrow(t, a));
|
|
341
|
+
} catch (t) {
|
|
342
|
+
return n(t), i(t, e, "Malformed certificate signing request");
|
|
324
343
|
}
|
|
325
344
|
}
|
|
326
345
|
/**
|
|
@@ -352,7 +371,7 @@ function parseCertificateSigningRequestPem(e, t) {
|
|
|
352
371
|
* @see {@linkcode getSubjectPublicKey} for the non-throwing variant
|
|
353
372
|
*/
|
|
354
373
|
function getSubjectPublicKeyOrThrow(e, t) {
|
|
355
|
-
return
|
|
374
|
+
return D(e.subjectPublicKeyInfoDer, t);
|
|
356
375
|
}
|
|
357
376
|
/**
|
|
358
377
|
* Import the subject public key of a parsed certificate or CSR as a WebCrypto `CryptoKey`.
|
|
@@ -360,12 +379,12 @@ function getSubjectPublicKeyOrThrow(e, t) {
|
|
|
360
379
|
* @see `getSubjectPublicKeyOrThrow` for the throwing variant
|
|
361
380
|
*/
|
|
362
381
|
function getSubjectPublicKey(e, t) {
|
|
363
|
-
return
|
|
382
|
+
return E(e.subjectPublicKeyInfoDer, t);
|
|
364
383
|
}
|
|
365
384
|
/** Extract the AlgorithmIdentifier TLV from a SubjectPublicKeyInfo DER buffer. */
|
|
366
385
|
function subjectPublicKeyInfoAlgorithmDer(e) {
|
|
367
|
-
let t =
|
|
368
|
-
return e.slice(
|
|
386
|
+
let t = l(e, { maxDepth: 64 }), n = y(t[0], "SubjectPublicKeyInfo algorithm");
|
|
387
|
+
return e.slice(n.start - n.headerLength, n.end);
|
|
369
388
|
}
|
|
370
389
|
/**
|
|
371
390
|
* Derive the public half of `privateKey`, export it as SPKI DER, and classify
|
|
@@ -377,8 +396,8 @@ function subjectPublicKeyInfoAlgorithmDer(e) {
|
|
|
377
396
|
* mismatch or a cross-algorithm (`key_type_mismatch`) one.
|
|
378
397
|
*/
|
|
379
398
|
async function compareCertificateToPrivateKey(e, t) {
|
|
380
|
-
let n = await
|
|
381
|
-
return
|
|
399
|
+
let n = await T(await w(t));
|
|
400
|
+
return o(n, e) ? "match" : o(subjectPublicKeyInfoAlgorithmDer(n), subjectPublicKeyInfoAlgorithmDer(e)) ? "key_mismatch" : "key_type_mismatch";
|
|
382
401
|
}
|
|
383
402
|
/**
|
|
384
403
|
* Check whether a certificate's subject public key belongs to a private key.
|
|
@@ -440,6 +459,9 @@ async function certificateMatchesPrivateKey(e, t) {
|
|
|
440
459
|
* one of:
|
|
441
460
|
*
|
|
442
461
|
* - `malformed_certificate` — `certificate` could not be parsed.
|
|
462
|
+
* - `unsupported` or `limit_exceeded` — `certificate` holds a construct
|
|
463
|
+
* micro509 does not decode or exceeds a decoding limit (see
|
|
464
|
+
* {@linkcode ParseCertificateErrorCode}).
|
|
443
465
|
* - `unsupported_private_key` — `privateKey` is not an extractable private key
|
|
444
466
|
* of a supported type (from {@linkcode derivePublicKey}).
|
|
445
467
|
* - `key_type_mismatch` — the key is a different algorithm than the
|
|
@@ -460,29 +482,33 @@ async function certificateMatchesPrivateKey(e, t) {
|
|
|
460
482
|
* if (!result.ok) {
|
|
461
483
|
* // result.code is 'malformed_certificate' | 'unsupported_private_key'
|
|
462
484
|
* // | 'key_type_mismatch' | 'key_mismatch'
|
|
485
|
+
* // | 'unsupported' | 'limit_exceeded'
|
|
463
486
|
* throw new Error(`key does not match certificate: ${result.code}`);
|
|
464
487
|
* }
|
|
465
488
|
* ```
|
|
466
489
|
*
|
|
467
490
|
* @see {@linkcode certificateMatchesPrivateKey} for the plain-boolean variant
|
|
468
491
|
*/
|
|
469
|
-
async function matchCertificatePrivateKey(
|
|
470
|
-
let
|
|
492
|
+
async function matchCertificatePrivateKey(i, o) {
|
|
493
|
+
let s;
|
|
471
494
|
try {
|
|
472
|
-
|
|
473
|
-
} catch (
|
|
474
|
-
|
|
495
|
+
s = parseCertificateFromSource(i);
|
|
496
|
+
} catch (r) {
|
|
497
|
+
n(r);
|
|
498
|
+
let i = a(r, e);
|
|
499
|
+
return i === void 0 ? t("malformed_certificate", r instanceof Error ? r.message : "Malformed certificate") : t(i.code, i.message);
|
|
475
500
|
}
|
|
476
|
-
let
|
|
501
|
+
let c;
|
|
477
502
|
try {
|
|
478
|
-
|
|
503
|
+
c = await compareCertificateToPrivateKey(s.subjectPublicKeyInfoDer, o);
|
|
479
504
|
} catch (e) {
|
|
480
|
-
return
|
|
505
|
+
return n(e), t("unsupported_private_key", e instanceof Error ? e.message : "Unsupported private key");
|
|
481
506
|
}
|
|
482
|
-
switch (
|
|
483
|
-
case "match": return
|
|
484
|
-
case "key_mismatch": return
|
|
485
|
-
case "key_type_mismatch": return
|
|
507
|
+
switch (c) {
|
|
508
|
+
case "match": return r(void 0);
|
|
509
|
+
case "key_mismatch": return t("key_mismatch", "Private key does not match the certificate's subject public key");
|
|
510
|
+
case "key_type_mismatch": return t("key_type_mismatch", "Private key algorithm does not match the certificate's subject public key algorithm");
|
|
511
|
+
default: throw Error(`Unhandled key comparison result: ${String(c)}`);
|
|
486
512
|
}
|
|
487
513
|
}
|
|
488
514
|
/**
|
|
@@ -547,66 +573,79 @@ function decodeExtensionMap(e, t) {
|
|
|
547
573
|
}
|
|
548
574
|
return n;
|
|
549
575
|
}
|
|
576
|
+
/** The decoded authorityInfoAccess, cRLDistributionPoints and noRevAvail fields, each only when present. */
|
|
577
|
+
function accessExtensionFields(e) {
|
|
578
|
+
return {
|
|
579
|
+
...e.authorityInfoAccess === void 0 ? {} : { authorityInfoAccess: e.authorityInfoAccess },
|
|
580
|
+
...e.crlDistributionPoints === void 0 ? {} : { crlDistributionPoints: e.crlDistributionPoints },
|
|
581
|
+
...e.noRevAvail === !0 ? { noRevAvail: !0 } : {}
|
|
582
|
+
};
|
|
583
|
+
}
|
|
550
584
|
/** Decode the explicit [3] extensions wrapper from a TBSCertificate. */
|
|
551
585
|
function parseExtensionContainer(e, t) {
|
|
552
|
-
return t === void 0 ? { all: [] } : parseExtensionSequence(e,
|
|
586
|
+
return t === void 0 ? { all: [] } : parseExtensionSequence(e, y(u(e, t)[0], "extensions sequence"), "certificate");
|
|
553
587
|
}
|
|
554
588
|
/** Extract extensions from the CSR extensionRequest attribute. */
|
|
555
589
|
function parseRequestedExtensions(e, t) {
|
|
556
|
-
if (t === void 0) return { all: [] };
|
|
557
|
-
if (t.tag !== 160) throw Error("CertificationRequestInfo attributes must use [0]");
|
|
558
590
|
let n;
|
|
559
|
-
for (let r of
|
|
560
|
-
|
|
591
|
+
for (let r of u(e, t)) {
|
|
592
|
+
if (r.tag !== 48) throw Error("CSR attribute must use SEQUENCE");
|
|
593
|
+
let t = u(e, r);
|
|
561
594
|
if (t.length !== 2) throw Error("Malformed CSR attribute");
|
|
562
|
-
|
|
563
|
-
if (
|
|
564
|
-
let a =
|
|
565
|
-
if (a.tag !== 49) throw Error("
|
|
566
|
-
let o =
|
|
567
|
-
if (o.length
|
|
568
|
-
|
|
595
|
+
let i = y(t[0], "attribute OID");
|
|
596
|
+
if (i.tag !== 6) throw Error("CSR attribute type must use OBJECT IDENTIFIER");
|
|
597
|
+
let a = y(t[1], "attribute values");
|
|
598
|
+
if (a.tag !== 49) throw Error("CSR attribute values must use SET");
|
|
599
|
+
let o = u(e, a);
|
|
600
|
+
if (o.length === 0) throw Error("CSR attribute values must not be empty");
|
|
601
|
+
if (m(i.value) === x.extensionRequest) {
|
|
602
|
+
if (n !== void 0) throw Error("extensionRequest attribute must not repeat");
|
|
603
|
+
if (o.length !== 1) throw Error("extensionRequest attribute must contain exactly one value");
|
|
604
|
+
n = parseExtensionSequence(e, y(o[0], "requested extensions"), "csr");
|
|
605
|
+
}
|
|
569
606
|
}
|
|
570
607
|
return n ?? { all: [] };
|
|
571
608
|
}
|
|
572
609
|
/** Walk a SEQUENCE OF Extension and decode each one, populating known-extension slots. */
|
|
573
610
|
function parseExtensionSequence(e, t, n) {
|
|
574
|
-
let r = [],
|
|
575
|
-
for (let
|
|
576
|
-
let t =
|
|
611
|
+
let r = [], i = {}, a = /* @__PURE__ */ new Set();
|
|
612
|
+
for (let o of u(e, t)) {
|
|
613
|
+
let t = u(e, o);
|
|
577
614
|
if (t.length < 2 || t.length > 3) throw Error("Malformed Extension");
|
|
578
|
-
let
|
|
579
|
-
if (
|
|
580
|
-
|
|
581
|
-
let
|
|
582
|
-
|
|
583
|
-
let
|
|
584
|
-
if (
|
|
615
|
+
let s = y(t[0], "extension OID"), c = m(s.value);
|
|
616
|
+
if (a.has(c)) throw Error(`Duplicate extension OID: ${c}`);
|
|
617
|
+
a.add(c);
|
|
618
|
+
let l = 1, f = !1, p = t[l];
|
|
619
|
+
p?.tag === 1 && (f = d(p.value), l += 1);
|
|
620
|
+
let h = y(t[l], "extension value");
|
|
621
|
+
if (h.tag !== 4 || l !== t.length - 1) throw Error("Extension value must use OCTET STRING");
|
|
585
622
|
r.push({
|
|
586
|
-
oid:
|
|
587
|
-
critical:
|
|
588
|
-
valueDer: new Uint8Array(
|
|
589
|
-
valueHex:
|
|
590
|
-
}),
|
|
623
|
+
oid: c,
|
|
624
|
+
critical: f,
|
|
625
|
+
valueDer: new Uint8Array(h.value),
|
|
626
|
+
valueHex: b(h.value)
|
|
627
|
+
}), V(n, c, i, h.value);
|
|
591
628
|
}
|
|
592
629
|
return {
|
|
593
630
|
all: r,
|
|
594
|
-
...
|
|
631
|
+
...i
|
|
595
632
|
};
|
|
596
633
|
}
|
|
597
634
|
/** Decode an X.501 Name (issuer / subject) into a {@linkcode ParsedName}. */
|
|
598
635
|
function parseName(e, t) {
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
636
|
+
if (t.tag !== 48) throw Error("Name must use SEQUENCE");
|
|
637
|
+
let n = [], r = [], i = {};
|
|
638
|
+
for (let a of u(e, t)) {
|
|
639
|
+
if (a.tag !== 49) throw Error("RelativeDistinguishedName must use SET");
|
|
640
|
+
let t = parseNameAttributeSet(e, a);
|
|
602
641
|
n.push(t);
|
|
603
|
-
for (let e of t.attributes) r.push(e), e.key !== void 0 &&
|
|
642
|
+
for (let e of t.attributes) r.push(e), e.key !== void 0 && i[e.key] === void 0 && (i[e.key] = e.value);
|
|
604
643
|
}
|
|
605
644
|
return {
|
|
606
|
-
derHex:
|
|
645
|
+
derHex: b(e.slice(t.start - t.headerLength, t.end)),
|
|
607
646
|
rdns: n,
|
|
608
647
|
attributes: r,
|
|
609
|
-
values:
|
|
648
|
+
values: i
|
|
610
649
|
};
|
|
611
650
|
}
|
|
612
651
|
/** Decode a single RDN SET element. */
|
|
@@ -616,262 +655,265 @@ function parseRelativeDistinguishedName(e, t) {
|
|
|
616
655
|
/** Decode the AttributeTypeAndValue pairs within a SET element. */
|
|
617
656
|
function parseNameAttributeSet(e, t) {
|
|
618
657
|
let n = [], r = {};
|
|
619
|
-
for (let
|
|
620
|
-
|
|
658
|
+
for (let i of u(e, t)) {
|
|
659
|
+
if (i.tag !== 48) throw Error("AttributeTypeAndValue must use SEQUENCE");
|
|
660
|
+
let t = u(e, i);
|
|
661
|
+
if (t.length !== 2) throw Error("AttributeTypeAndValue must contain type and value");
|
|
662
|
+
let a = y(t[0], "name OID");
|
|
663
|
+
if (a.tag !== 6) throw Error("AttributeTypeAndValue type must use OBJECT IDENTIFIER");
|
|
664
|
+
let o = m(a.value), s = y(t[1], "name value"), c = R(o), l = h(s.tag, s.value), d = c === void 0 ? {
|
|
621
665
|
oid: o,
|
|
622
666
|
valueTag: s.tag,
|
|
623
|
-
value:
|
|
667
|
+
value: l
|
|
624
668
|
} : {
|
|
625
669
|
oid: o,
|
|
626
|
-
key:
|
|
670
|
+
key: c,
|
|
627
671
|
valueTag: s.tag,
|
|
628
|
-
value:
|
|
672
|
+
value: l
|
|
629
673
|
};
|
|
630
|
-
n.push(
|
|
674
|
+
n.push(d), c !== void 0 && r[c] === void 0 && (r[c] = l);
|
|
631
675
|
}
|
|
632
676
|
return {
|
|
633
|
-
derHex:
|
|
677
|
+
derHex: b(e.slice(t.start - t.headerLength, t.end)),
|
|
634
678
|
attributes: n,
|
|
635
679
|
values: r
|
|
636
680
|
};
|
|
637
681
|
}
|
|
638
682
|
/** Decode the Validity SEQUENCE into notBefore / notAfter Dates. */
|
|
639
683
|
function parseValidity(e, t) {
|
|
640
|
-
let n =
|
|
684
|
+
let n = u(e, t);
|
|
641
685
|
return {
|
|
642
|
-
notBefore:
|
|
643
|
-
notAfter:
|
|
686
|
+
notBefore: v(y(n[0], "notBefore")),
|
|
687
|
+
notAfter: v(y(n[1], "notAfter"))
|
|
644
688
|
};
|
|
645
689
|
}
|
|
646
690
|
/** Extract the algorithm identifier from a SubjectPublicKeyInfo SEQUENCE. */
|
|
647
691
|
function parseSubjectPublicKeyInfo(e, t) {
|
|
648
692
|
if (t.tag !== 48) throw Error("SubjectPublicKeyInfo must use SEQUENCE");
|
|
649
|
-
let n =
|
|
693
|
+
let n = u(e, t);
|
|
650
694
|
if (n.length !== 2) throw Error("SubjectPublicKeyInfo must contain algorithm and subjectPublicKey");
|
|
651
|
-
let r =
|
|
695
|
+
let r = y(n[0], "SPKI algorithm");
|
|
652
696
|
if (r.tag !== 48) throw Error("SubjectPublicKeyInfo algorithm must use SEQUENCE");
|
|
653
|
-
let
|
|
654
|
-
return
|
|
697
|
+
let i = parseAlgorithmIdentifier(e, r);
|
|
698
|
+
return _(y(n[1], "subjectPublicKey BIT STRING")), i;
|
|
655
699
|
}
|
|
656
700
|
/** Decode an AlgorithmIdentifier SEQUENCE (OID + optional parameters). */
|
|
657
701
|
function parseAlgorithmIdentifier(e, t) {
|
|
658
|
-
|
|
702
|
+
if (t.tag !== 48) throw Error("AlgorithmIdentifier must use SEQUENCE");
|
|
703
|
+
let n = u(e, t);
|
|
659
704
|
if (n.length === 0 || n.length > 2) throw Error("Malformed AlgorithmIdentifier");
|
|
660
|
-
let r =
|
|
661
|
-
if (
|
|
705
|
+
let r = y(n[0], "algorithm OID");
|
|
706
|
+
if (r.tag !== 6) throw Error("AlgorithmIdentifier algorithm must use OBJECT IDENTIFIER");
|
|
707
|
+
let i = m(r.value), a = n[1];
|
|
708
|
+
if (a === void 0) return { oid: i };
|
|
662
709
|
let o = e.slice(a.start - a.headerLength, a.end);
|
|
663
710
|
return a?.tag === 6 ? {
|
|
664
|
-
oid:
|
|
711
|
+
oid: i,
|
|
665
712
|
parametersDer: o,
|
|
666
|
-
parametersOid:
|
|
713
|
+
parametersOid: m(a.value)
|
|
667
714
|
} : {
|
|
668
|
-
oid:
|
|
715
|
+
oid: i,
|
|
669
716
|
parametersDer: o
|
|
670
717
|
};
|
|
671
718
|
}
|
|
672
719
|
function assertMatchingCertificateSignatureAlgorithms(e, t) {
|
|
673
|
-
if (e.oid !== t.oid || !
|
|
674
|
-
}
|
|
675
|
-
function optionalBytesEqual(e, t) {
|
|
676
|
-
if (e === void 0 || t === void 0) return e === t;
|
|
677
|
-
if (e.length !== t.length) return !1;
|
|
678
|
-
for (let n = 0; n < e.length; n += 1) if (e[n] !== t[n]) return !1;
|
|
679
|
-
return !0;
|
|
720
|
+
if (e.oid !== t.oid || !o(e.parametersDer, t.parametersDer)) throw Error("Certificate signatureAlgorithm must match TBSCertificate signature");
|
|
680
721
|
}
|
|
681
|
-
/**
|
|
722
|
+
/** Decodes the Basic Constraints extension value DER. */
|
|
682
723
|
function parseBasicConstraints(e) {
|
|
683
|
-
let t =
|
|
724
|
+
let t = c(e, { maxDepth: 64 });
|
|
684
725
|
if (t.tag !== 48) throw Error("basicConstraints must use SEQUENCE");
|
|
685
|
-
let
|
|
686
|
-
for (let e of
|
|
687
|
-
if (
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
continue;
|
|
697
|
-
}
|
|
698
|
-
throw Error(`Unsupported basicConstraints field tag: ${String(e.tag)}`);
|
|
699
|
-
}
|
|
700
|
-
if (c !== void 0 && o !== !0) throw Error("basicConstraints pathLength requires cA = true");
|
|
701
|
-
return o ? c === void 0 ? { ca: !0 } : {
|
|
726
|
+
let n = u(e, t), r = !1, i, a = !1, o = !1;
|
|
727
|
+
for (let e of n) if (e.tag === 1) {
|
|
728
|
+
if (a) throw Error("basicConstraints cA must not repeat");
|
|
729
|
+
if (o) throw Error("basicConstraints cA must precede pathLength");
|
|
730
|
+
a = !0, r = d(e.value);
|
|
731
|
+
} else if (e.tag === 2) {
|
|
732
|
+
if (o) throw Error("basicConstraints pathLength must not repeat");
|
|
733
|
+
o = !0, i = p(e.value, "basicConstraints pathLength");
|
|
734
|
+
} else throw Error(`Unsupported basicConstraints field tag: ${String(e.tag)}`);
|
|
735
|
+
if (i !== void 0 && r !== !0) throw Error("basicConstraints pathLength requires cA = true");
|
|
736
|
+
return r ? i === void 0 ? { ca: !0 } : {
|
|
702
737
|
ca: !0,
|
|
703
|
-
pathLength:
|
|
738
|
+
pathLength: i
|
|
704
739
|
} : { ca: !1 };
|
|
705
740
|
}
|
|
706
|
-
/**
|
|
741
|
+
/** Decodes the Key Usage BIT STRING extension value. */
|
|
707
742
|
function parseKeyUsage(e) {
|
|
708
|
-
return
|
|
743
|
+
return M(e);
|
|
709
744
|
}
|
|
710
|
-
/**
|
|
745
|
+
/** Decodes the Extended Key Usage SEQUENCE OF OIDs. */
|
|
711
746
|
function parseExtendedKeyUsage(e) {
|
|
712
|
-
|
|
747
|
+
let t = l(e, { maxDepth: 64 });
|
|
748
|
+
if (t.length === 0) throw Error("extendedKeyUsage must not be empty");
|
|
749
|
+
return t.map((e) => {
|
|
750
|
+
if (e.tag !== 6) throw Error("extendedKeyUsage entry must use OBJECT IDENTIFIER");
|
|
751
|
+
return B(m(e.value));
|
|
752
|
+
});
|
|
713
753
|
}
|
|
714
|
-
/**
|
|
754
|
+
/** Decodes the Certificate Policies extension value. */
|
|
715
755
|
function parseCertificatePolicies(e) {
|
|
716
|
-
let t =
|
|
717
|
-
if (
|
|
718
|
-
return
|
|
756
|
+
let t = y(c(e, { maxDepth: 64 }), "certificatePolicies sequence"), n = u(e, t);
|
|
757
|
+
if (n.length === 0) throw Error("certificatePolicies must not be empty");
|
|
758
|
+
return n.map((t) => parsePolicyInformation(e, t));
|
|
719
759
|
}
|
|
720
760
|
/** Decode a single PolicyInformation SEQUENCE (OID + optional qualifiers). */
|
|
721
761
|
function parsePolicyInformation(e, t) {
|
|
722
|
-
let n =
|
|
762
|
+
let n = u(e, t), r = m(y(n[0], "policyIdentifier").value), i = n[1];
|
|
723
763
|
if (n.length > 2) throw Error("policyInformation has unexpected trailing fields");
|
|
724
|
-
if (
|
|
725
|
-
let
|
|
726
|
-
if (
|
|
764
|
+
if (i === void 0) return { policyIdentifier: r };
|
|
765
|
+
let a = u(e, i);
|
|
766
|
+
if (a.length === 0) throw Error("policyQualifiers must not be empty");
|
|
727
767
|
return {
|
|
728
768
|
policyIdentifier: r,
|
|
729
|
-
policyQualifiers:
|
|
769
|
+
policyQualifiers: a.map((t) => parsePolicyQualifierInfo(e, t))
|
|
730
770
|
};
|
|
731
771
|
}
|
|
732
772
|
/** Decode a PolicyQualifierInfo (CPS URI, UserNotice, or opaque OID). */
|
|
733
773
|
function parsePolicyQualifierInfo(e, t) {
|
|
734
|
-
let n =
|
|
774
|
+
let n = u(e, t), r = m(y(n[0], "policyQualifierId").value), i = y(n[1], "policyQualifier");
|
|
735
775
|
if (n.length > 2) throw Error("policyQualifierInfo has unexpected trailing fields");
|
|
736
|
-
if (r ===
|
|
737
|
-
if (
|
|
776
|
+
if (r === x.cpsPolicyQualifier) {
|
|
777
|
+
if (i.tag !== 22) throw Error("cps policy qualifier must use IA5String");
|
|
738
778
|
return {
|
|
739
779
|
type: "cps",
|
|
740
|
-
uri:
|
|
780
|
+
uri: h(i.tag, i.value)
|
|
741
781
|
};
|
|
742
782
|
}
|
|
743
|
-
return r ===
|
|
783
|
+
return r === x.userNoticePolicyQualifier ? {
|
|
744
784
|
type: "userNotice",
|
|
745
|
-
...parseUserNoticePolicyQualifierInfo(e,
|
|
785
|
+
...parseUserNoticePolicyQualifierInfo(e, i)
|
|
746
786
|
} : {
|
|
747
787
|
type: "oid",
|
|
748
788
|
oid: r,
|
|
749
|
-
qualifierDer: e.slice(
|
|
789
|
+
qualifierDer: e.slice(i.start - i.headerLength, i.end)
|
|
750
790
|
};
|
|
751
791
|
}
|
|
752
792
|
/** Decode a UserNotice qualifier (optional noticeRef + optional explicitText). */
|
|
753
793
|
function parseUserNoticePolicyQualifierInfo(e, t) {
|
|
754
|
-
let n =
|
|
755
|
-
for (let t of n) {
|
|
756
|
-
if (
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
if (a !== void 0) throw Error("userNotice must not contain multiple explicitText values");
|
|
762
|
-
a = parseDisplayText(t);
|
|
794
|
+
let n = u(e, t), r, i;
|
|
795
|
+
for (let t of n) if (t.tag === 48) {
|
|
796
|
+
if (r !== void 0) throw Error("userNotice must not contain multiple noticeRef values");
|
|
797
|
+
r = parsePolicyNoticeReference(e, t);
|
|
798
|
+
} else {
|
|
799
|
+
if (i !== void 0) throw Error("userNotice must not contain multiple explicitText values");
|
|
800
|
+
i = parseExplicitText(t);
|
|
763
801
|
}
|
|
764
802
|
return {
|
|
765
803
|
...r === void 0 ? {} : { noticeRef: r },
|
|
766
|
-
...
|
|
804
|
+
...i
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
function parseExplicitText(e) {
|
|
808
|
+
let { text: t, oversized: n } = parseDisplayText(e);
|
|
809
|
+
return {
|
|
810
|
+
explicitText: t,
|
|
811
|
+
explicitTextType: displayTextType(e.tag),
|
|
812
|
+
...n === void 0 ? {} : { oversizedExplicitText: n }
|
|
767
813
|
};
|
|
768
814
|
}
|
|
769
815
|
/** Decode a NoticeReference (organization name + notice number list). */
|
|
770
816
|
function parsePolicyNoticeReference(e, t) {
|
|
771
817
|
if (t.tag !== 48) throw Error("noticeRef must use SEQUENCE");
|
|
772
|
-
let n =
|
|
818
|
+
let n = u(e, t), r = parseDisplayText(y(n[0], "noticeRef organization")), i = y(n[1], "noticeRef noticeNumbers");
|
|
773
819
|
if (n.length > 2) throw Error("noticeRef has unexpected trailing fields");
|
|
774
820
|
return {
|
|
775
|
-
organization: r,
|
|
776
|
-
noticeNumbers: parsePolicyNoticeNumbers(e,
|
|
821
|
+
organization: r.text,
|
|
822
|
+
noticeNumbers: parsePolicyNoticeNumbers(e, i),
|
|
823
|
+
...r.oversized === void 0 ? {} : { oversizedOrganization: r.oversized }
|
|
777
824
|
};
|
|
778
825
|
}
|
|
779
826
|
/** Decode a SEQUENCE OF INTEGER notice numbers. */
|
|
780
827
|
function parsePolicyNoticeNumbers(e, t) {
|
|
781
828
|
if (t.tag !== 48) throw Error("noticeRef noticeNumbers must use SEQUENCE");
|
|
782
|
-
let n =
|
|
829
|
+
let n = u(e, t);
|
|
783
830
|
if (n.length === 0) throw Error("noticeRef noticeNumbers must not be empty");
|
|
784
831
|
return n.map((e) => {
|
|
785
832
|
if (e.tag !== 2) throw Error("noticeRef noticeNumber must use INTEGER");
|
|
786
|
-
return
|
|
833
|
+
return p(e.value, "noticeRef noticeNumber");
|
|
787
834
|
});
|
|
788
835
|
}
|
|
789
|
-
/**
|
|
836
|
+
/** Decodes the Policy Mappings extension value. */
|
|
790
837
|
function parsePolicyMappings(e) {
|
|
791
|
-
let t =
|
|
792
|
-
if (
|
|
793
|
-
return
|
|
794
|
-
let n =
|
|
838
|
+
let t = y(c(e, { maxDepth: 64 }), "policyMappings sequence"), n = u(e, t);
|
|
839
|
+
if (n.length === 0) throw Error("policyMappings must not be empty");
|
|
840
|
+
return n.map((t) => {
|
|
841
|
+
let n = u(e, t), r = m(y(n[0], "policyMappings issuerDomainPolicy").value), i = m(y(n[1], "policyMappings subjectDomainPolicy").value);
|
|
795
842
|
if (n.length > 2) throw Error("policyMappings entry has unexpected trailing fields");
|
|
796
|
-
if (r ===
|
|
843
|
+
if (r === x.anyPolicy || i === x.anyPolicy) throw Error("policyMappings must not use anyPolicy");
|
|
797
844
|
return {
|
|
798
845
|
issuerDomainPolicy: r,
|
|
799
|
-
subjectDomainPolicy:
|
|
846
|
+
subjectDomainPolicy: i
|
|
800
847
|
};
|
|
801
848
|
});
|
|
802
849
|
}
|
|
803
|
-
/**
|
|
850
|
+
/** Decodes the Policy Constraints extension value. */
|
|
804
851
|
function parsePolicyConstraints(e) {
|
|
805
|
-
let t =
|
|
806
|
-
for (let
|
|
807
|
-
if (n
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
a = s(n.value, "policyConstraints inhibitPolicyMapping");
|
|
815
|
-
continue;
|
|
816
|
-
}
|
|
817
|
-
throw Error(`Unsupported policyConstraints field tag: ${n.tag}`);
|
|
818
|
-
}
|
|
819
|
-
if (r === void 0 && a === void 0) throw Error("policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping");
|
|
852
|
+
let t = y(c(e, { maxDepth: 64 }), "policyConstraints sequence"), n, r;
|
|
853
|
+
for (let i of u(e, t)) if (i.tag === 128) {
|
|
854
|
+
if (n !== void 0) throw Error("policyConstraints must not repeat requireExplicitPolicy");
|
|
855
|
+
n = p(i.value, "policyConstraints requireExplicitPolicy");
|
|
856
|
+
} else if (i.tag === 129) {
|
|
857
|
+
if (r !== void 0) throw Error("policyConstraints must not repeat inhibitPolicyMapping");
|
|
858
|
+
r = p(i.value, "policyConstraints inhibitPolicyMapping");
|
|
859
|
+
} else throw Error(`Unsupported policyConstraints field tag: ${i.tag}`);
|
|
860
|
+
if (n === void 0 && r === void 0) throw Error("policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping");
|
|
820
861
|
return {
|
|
821
|
-
...
|
|
822
|
-
...
|
|
862
|
+
...n === void 0 ? {} : { requireExplicitPolicy: n },
|
|
863
|
+
...r === void 0 ? {} : { inhibitPolicyMapping: r }
|
|
823
864
|
};
|
|
824
865
|
}
|
|
825
|
-
/**
|
|
866
|
+
/** Decodes the Inhibit anyPolicy extension (single INTEGER). */
|
|
826
867
|
function parseInhibitAnyPolicy(e) {
|
|
827
|
-
let t =
|
|
868
|
+
let t = y(c(e, { maxDepth: 64 }), "inhibitAnyPolicy integer");
|
|
828
869
|
if (t.tag !== 2) throw Error("inhibitAnyPolicy must be an INTEGER");
|
|
829
|
-
return { skipCerts:
|
|
870
|
+
return { skipCerts: p(t.value, "inhibitAnyPolicy skipCerts") };
|
|
830
871
|
}
|
|
831
|
-
/**
|
|
832
|
-
function parseSubjectAltNames(e) {
|
|
833
|
-
|
|
872
|
+
/** Decodes a subjectAltName or issuerAltName SEQUENCE OF GeneralName. */
|
|
873
|
+
function parseSubjectAltNames(e, t = "subjectAltName") {
|
|
874
|
+
let n = y(c(e, { maxDepth: 64 }), `${t} sequence`);
|
|
875
|
+
if (n.tag !== 48) throw Error(`${t} must use SEQUENCE`);
|
|
876
|
+
return I(e, n);
|
|
834
877
|
}
|
|
835
|
-
/**
|
|
878
|
+
/** Decodes a bare DER-encoded X.501 Name, as carried in a `directoryName` GeneralName. */
|
|
836
879
|
function parseDistinguishedNameDer(e) {
|
|
837
|
-
let t =
|
|
880
|
+
let t = y(c(e, { maxDepth: 64 }), "distinguished name sequence");
|
|
838
881
|
if (t.tag !== 48) throw Error("distinguished name must use SEQUENCE");
|
|
839
882
|
return parseName(e, t);
|
|
840
883
|
}
|
|
841
|
-
/**
|
|
884
|
+
/** Decodes the Authority Information Access extension value. */
|
|
842
885
|
function parseAuthorityInfoAccess(e) {
|
|
843
|
-
let t =
|
|
886
|
+
let t = y(c(e, { maxDepth: 64 }), "authorityInfoAccess sequence");
|
|
844
887
|
if (t.tag !== 48) throw Error("authorityInfoAccess must use SEQUENCE");
|
|
845
|
-
let
|
|
846
|
-
if (
|
|
847
|
-
return
|
|
888
|
+
let n = u(e, t);
|
|
889
|
+
if (n.length === 0) throw Error("authorityInfoAccess must not be empty");
|
|
890
|
+
return n.map((t) => {
|
|
848
891
|
if (t.tag !== 48) throw Error("authorityInfoAccess entry must use SEQUENCE");
|
|
849
|
-
let n =
|
|
892
|
+
let n = u(e, t);
|
|
850
893
|
if (n.length !== 2) throw Error("authorityInfoAccess entry must contain method and location only");
|
|
851
|
-
let r =
|
|
894
|
+
let r = y(n[0], "authorityInfoAccess method"), i = y(n[1], "authorityInfoAccess location");
|
|
852
895
|
if (r.tag !== 6) throw Error("authorityInfoAccess method must use OBJECT IDENTIFIER");
|
|
853
|
-
if (a.tag !== 134) throw Error(`Unsupported authorityInfoAccess location tag: ${a.tag}`);
|
|
854
896
|
return {
|
|
855
|
-
method:
|
|
856
|
-
|
|
897
|
+
method: z(m(r.value)),
|
|
898
|
+
location: F(e, i)
|
|
857
899
|
};
|
|
858
900
|
});
|
|
859
901
|
}
|
|
860
|
-
/**
|
|
902
|
+
/** Decodes the CRL Distribution Points extension value. */
|
|
861
903
|
function parseCrlDistributionPoints(e) {
|
|
862
|
-
let t =
|
|
904
|
+
let t = y(c(e, { maxDepth: 64 }), "CRLDistributionPoints sequence");
|
|
863
905
|
if (t.tag !== 48) throw Error("CRLDistributionPoints must use SEQUENCE");
|
|
864
|
-
let
|
|
865
|
-
if (
|
|
866
|
-
let
|
|
867
|
-
for (let t of
|
|
868
|
-
return
|
|
906
|
+
let n = u(e, t);
|
|
907
|
+
if (n.length === 0) throw Error("CRLDistributionPoints must not be empty");
|
|
908
|
+
let r = [];
|
|
909
|
+
for (let t of n) r.push(parseDistributionPoint(e, t));
|
|
910
|
+
return r;
|
|
869
911
|
}
|
|
870
912
|
/** Decode a single DistributionPoint SEQUENCE. */
|
|
871
913
|
function parseDistributionPoint(e, t) {
|
|
872
914
|
if (t.tag !== 48) throw Error("DistributionPoint must use SEQUENCE");
|
|
873
915
|
let n = {};
|
|
874
|
-
for (let r of
|
|
916
|
+
for (let r of u(e, t)) parseDistributionPointField(e, r, n);
|
|
875
917
|
if (n.distributionPoint === void 0 && n.crlIssuer === void 0) throw Error("DistributionPoint must include distributionPoint or crlIssuer");
|
|
876
918
|
return {
|
|
877
919
|
...n.distributionPoint === void 0 ? {} : { distributionPoint: n.distributionPoint },
|
|
@@ -887,95 +929,38 @@ function parseDistributionPointField(e, t, n) {
|
|
|
887
929
|
return;
|
|
888
930
|
case 129:
|
|
889
931
|
if (n.reasons !== void 0) throw Error("DistributionPoint reasons must not repeat");
|
|
890
|
-
n.reasons =
|
|
932
|
+
n.reasons = j(t.value);
|
|
891
933
|
return;
|
|
892
934
|
case 162:
|
|
893
935
|
if (n.crlIssuer !== void 0) throw Error("DistributionPoint crlIssuer must not repeat");
|
|
894
|
-
n.crlIssuer =
|
|
936
|
+
n.crlIssuer = I(e, t);
|
|
895
937
|
return;
|
|
896
938
|
default: throw Error(`Unsupported DistributionPoint field tag: ${String(t.tag)}`);
|
|
897
939
|
}
|
|
898
940
|
}
|
|
899
941
|
/** Decode a DistributionPointName (fullName or relativeName). */
|
|
900
942
|
function parseDistributionPointName(e, t) {
|
|
901
|
-
let n =
|
|
943
|
+
let n = u(e, t);
|
|
902
944
|
if (n.length !== 1) throw Error("distributionPointName must contain exactly one choice");
|
|
903
|
-
let r =
|
|
945
|
+
let r = y(n[0], "distributionPointName");
|
|
904
946
|
if (r.tag === 160) {
|
|
905
|
-
let t =
|
|
947
|
+
let t = u(e, r);
|
|
906
948
|
if (t.length === 0) throw Error("distributionPointName fullName must not be empty");
|
|
907
949
|
for (let e of t) if ((e.tag & 192) != 128) throw Error("distributionPointName fullName must contain GeneralName entries");
|
|
908
|
-
return {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
throw Error(`Unsupported distributionPointName tag: ${r.tag}`);
|
|
912
|
-
}
|
|
913
|
-
/** Decode a SEQUENCE OF GeneralName. */
|
|
914
|
-
function parseGeneralNames(e, t) {
|
|
915
|
-
let n = i(e, t);
|
|
916
|
-
if (n.length === 0) throw Error("GeneralNames must not be empty");
|
|
917
|
-
for (let e of n) if ((e.tag & 192) != 128) throw Error("GeneralNames must contain GeneralName entries");
|
|
918
|
-
return n.map((t) => parseGeneralName(e, t));
|
|
919
|
-
}
|
|
920
|
-
/** Decode a single GeneralName from its implicit context tag. */
|
|
921
|
-
function parseGeneralName(e, t) {
|
|
922
|
-
switch (t.tag) {
|
|
923
|
-
case 160: {
|
|
924
|
-
let n = parseOtherName(e, t);
|
|
925
|
-
return n === void 0 ? {
|
|
926
|
-
type: "unknown",
|
|
927
|
-
tag: t.tag,
|
|
928
|
-
value: e.slice(t.start, t.end)
|
|
929
|
-
} : n;
|
|
930
|
-
}
|
|
931
|
-
case 129: return {
|
|
932
|
-
type: "email",
|
|
933
|
-
value: N.decode(t.value)
|
|
934
|
-
};
|
|
935
|
-
case 130: return {
|
|
936
|
-
type: "dns",
|
|
937
|
-
value: N.decode(t.value)
|
|
938
|
-
};
|
|
939
|
-
case 134: return {
|
|
940
|
-
type: "uri",
|
|
941
|
-
value: N.decode(t.value)
|
|
942
|
-
};
|
|
943
|
-
case 135: return {
|
|
944
|
-
type: "ip",
|
|
945
|
-
value: E(t.value)
|
|
946
|
-
};
|
|
947
|
-
case 164: return {
|
|
948
|
-
type: "directoryName",
|
|
949
|
-
derHex: p(rebuildDirectoryNameFromImplicit(t, e))
|
|
950
|
-
};
|
|
951
|
-
default: return {
|
|
952
|
-
type: "unknown",
|
|
953
|
-
tag: t.tag,
|
|
954
|
-
value: e.slice(t.start, t.end)
|
|
950
|
+
return {
|
|
951
|
+
type: "fullName",
|
|
952
|
+
fullName: t.map((t) => F(e, t))
|
|
955
953
|
};
|
|
956
954
|
}
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
let n = i(e, t);
|
|
961
|
-
if (n.length !== 1) throw Error("otherName must contain exactly one SEQUENCE");
|
|
962
|
-
let r = i(e, f(n[0], "otherName sequence"));
|
|
963
|
-
if (r.length !== 2) throw Error("otherName must contain exactly type-id and value");
|
|
964
|
-
let a = f(r[0], "otherName type-id"), o = f(r[1], "otherName value");
|
|
965
|
-
if (c(a.value) !== m.idOnDnsSrv) return;
|
|
966
|
-
if (o.tag !== 160) throw Error("SRV-ID otherName value must use explicit [0]");
|
|
967
|
-
let s = i(e, o);
|
|
968
|
-
if (s.length !== 1) throw Error("SRV-ID otherName value must contain exactly one IA5String");
|
|
969
|
-
let u = f(s[0], "SRV-ID IA5String");
|
|
970
|
-
if (u.tag !== 22) throw Error("SRV-ID otherName value must be an IA5String");
|
|
971
|
-
return {
|
|
972
|
-
type: "srv",
|
|
973
|
-
value: l(u.tag, u.value)
|
|
955
|
+
if (r.tag === 161) return {
|
|
956
|
+
type: "relativeName",
|
|
957
|
+
relativeName: parseRelativeDistinguishedName(e, r)
|
|
974
958
|
};
|
|
959
|
+
throw Error(`Unsupported distributionPointName tag: ${r.tag}`);
|
|
975
960
|
}
|
|
976
|
-
/**
|
|
961
|
+
/** Decodes the Name Constraints extension value. */
|
|
977
962
|
function parseNameConstraints(e) {
|
|
978
|
-
let t =
|
|
963
|
+
let t = y(c(e, {
|
|
979
964
|
maxDepth: 64,
|
|
980
965
|
allowOpaqueConstructedTags: [
|
|
981
966
|
160,
|
|
@@ -984,22 +969,22 @@ function parseNameConstraints(e) {
|
|
|
984
969
|
]
|
|
985
970
|
}), "nameConstraints sequence");
|
|
986
971
|
if (t.tag !== 48) throw Error("nameConstraints must use SEQUENCE");
|
|
987
|
-
let
|
|
988
|
-
for (let
|
|
989
|
-
if (
|
|
990
|
-
|
|
991
|
-
} else if (
|
|
992
|
-
if (
|
|
993
|
-
|
|
994
|
-
} else throw Error(`Unsupported nameConstraints field tag: ${String(
|
|
972
|
+
let n, r;
|
|
973
|
+
for (let i of u(e, t)) if (i.tag === 160) {
|
|
974
|
+
if (n !== void 0) throw Error("nameConstraints permittedSubtrees must not repeat");
|
|
975
|
+
n = parseGeneralSubtrees(e, i);
|
|
976
|
+
} else if (i.tag === 161) {
|
|
977
|
+
if (r !== void 0) throw Error("nameConstraints excludedSubtrees must not repeat");
|
|
978
|
+
r = parseGeneralSubtrees(e, i);
|
|
979
|
+
} else throw Error(`Unsupported nameConstraints field tag: ${String(i.tag)}`);
|
|
995
980
|
return {
|
|
996
|
-
...
|
|
997
|
-
...
|
|
981
|
+
...n === void 0 ? {} : { permittedSubtrees: n },
|
|
982
|
+
...r === void 0 ? {} : { excludedSubtrees: r }
|
|
998
983
|
};
|
|
999
984
|
}
|
|
1000
985
|
/** Decode a SEQUENCE OF GeneralSubtree from a permittedSubtrees or excludedSubtrees wrapper. */
|
|
1001
986
|
function parseGeneralSubtrees(e, t) {
|
|
1002
|
-
let n = [], r =
|
|
987
|
+
let n = [], r = u(e, t);
|
|
1003
988
|
if (r.length === 0) throw Error("name constraints GeneralSubtrees must not be empty");
|
|
1004
989
|
for (let t of r) {
|
|
1005
990
|
let r = parseGeneralSubtree(e, t);
|
|
@@ -1009,35 +994,38 @@ function parseGeneralSubtrees(e, t) {
|
|
|
1009
994
|
}
|
|
1010
995
|
function parseGeneralSubtree(e, t) {
|
|
1011
996
|
if (t.tag !== 48) throw Error("name constraints GeneralSubtree must use SEQUENCE");
|
|
1012
|
-
let n =
|
|
997
|
+
let n = u(e, t), r = n[0];
|
|
1013
998
|
if (r === void 0) throw Error("GeneralSubtree base is required");
|
|
1014
999
|
return validateGeneralSubtreeBounds(n.slice(1)), parseNameConstraintGeneralName(e, r);
|
|
1015
1000
|
}
|
|
1016
1001
|
function validateGeneralSubtreeBounds(e) {
|
|
1017
1002
|
let t = !1;
|
|
1018
|
-
for (let n of e) {
|
|
1019
|
-
if (
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
continue;
|
|
1023
|
-
}
|
|
1024
|
-
throw n.tag === 129 ? Error("name constraints GeneralSubtree maximum is not supported") : Error(`Unsupported name constraints GeneralSubtree field tag: ${String(n.tag)}`);
|
|
1025
|
-
}
|
|
1003
|
+
for (let n of e) if (n.tag === 128) {
|
|
1004
|
+
if (t) throw Error("name constraints GeneralSubtree minimum must not repeat");
|
|
1005
|
+
if (t = !0, p(n.value, "name constraints GeneralSubtree minimum") !== 0) throw Error("name constraints GeneralSubtree minimum must be 0");
|
|
1006
|
+
} else throw n.tag === 129 ? Error("name constraints GeneralSubtree maximum is not supported") : Error(`Unsupported name constraints GeneralSubtree field tag: ${String(n.tag)}`);
|
|
1026
1007
|
}
|
|
1027
1008
|
/** Decode a GeneralName for use in name constraints (IP carries address+mask). */
|
|
1028
1009
|
function parseNameConstraintGeneralName(e, t) {
|
|
1029
1010
|
switch (t.tag) {
|
|
1030
|
-
case 160:
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1011
|
+
case 160: {
|
|
1012
|
+
let { typeId: n, value: r } = L(e, t);
|
|
1013
|
+
return n === x.idOnDnsSrv ? {
|
|
1014
|
+
type: "srv",
|
|
1015
|
+
value: N(r)
|
|
1016
|
+
} : {
|
|
1017
|
+
type: "otherName",
|
|
1018
|
+
typeId: n,
|
|
1019
|
+
value: P(e, r)
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1034
1022
|
case 129: return {
|
|
1035
1023
|
type: "email",
|
|
1036
|
-
value:
|
|
1024
|
+
value: h(22, t.value)
|
|
1037
1025
|
};
|
|
1038
1026
|
case 130: return {
|
|
1039
1027
|
type: "dns",
|
|
1040
|
-
value:
|
|
1028
|
+
value: h(22, t.value)
|
|
1041
1029
|
};
|
|
1042
1030
|
case 163: return {
|
|
1043
1031
|
type: "x400Address",
|
|
@@ -1045,7 +1033,7 @@ function parseNameConstraintGeneralName(e, t) {
|
|
|
1045
1033
|
};
|
|
1046
1034
|
case 134: return {
|
|
1047
1035
|
type: "uri",
|
|
1048
|
-
value:
|
|
1036
|
+
value: h(22, t.value)
|
|
1049
1037
|
};
|
|
1050
1038
|
case 135:
|
|
1051
1039
|
if (t.value.length === 8) return {
|
|
@@ -1061,7 +1049,7 @@ function parseNameConstraintGeneralName(e, t) {
|
|
|
1061
1049
|
throw Error(`Invalid IP name constraint: expected 8 (IPv4) or 32 (IPv6) bytes, got ${String(t.value.length)}`);
|
|
1062
1050
|
case 164: return {
|
|
1063
1051
|
type: "directoryName",
|
|
1064
|
-
derHex:
|
|
1052
|
+
derHex: b(A(t))
|
|
1065
1053
|
};
|
|
1066
1054
|
case 165: return {
|
|
1067
1055
|
type: "ediPartyName",
|
|
@@ -1069,60 +1057,58 @@ function parseNameConstraintGeneralName(e, t) {
|
|
|
1069
1057
|
};
|
|
1070
1058
|
case 136: return {
|
|
1071
1059
|
type: "registeredID",
|
|
1072
|
-
value:
|
|
1060
|
+
value: m(t.value)
|
|
1073
1061
|
};
|
|
1074
1062
|
}
|
|
1075
1063
|
throw Error(`Unsupported name constraint GeneralName tag: ${String(t.tag)}`);
|
|
1076
1064
|
}
|
|
1077
|
-
/**
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
if (t.value.length > 0 && t.value[0] === 48) return new Uint8Array(t.value);
|
|
1086
|
-
let r = e(t.value.length), i = new Uint8Array(1 + r.length + t.value.length);
|
|
1087
|
-
return i[0] = 48, i.set(r, 1), i.set(t.value, 1 + r.length), i;
|
|
1065
|
+
/** The DisplayText alternative a tag names. Call only after `decodeDisplayText` accepted the tag. */
|
|
1066
|
+
function displayTextType(e) {
|
|
1067
|
+
switch (e) {
|
|
1068
|
+
case 22: return "ia5String";
|
|
1069
|
+
case 26: return "visibleString";
|
|
1070
|
+
case 30: return "bmpString";
|
|
1071
|
+
default: return "utf8String";
|
|
1072
|
+
}
|
|
1088
1073
|
}
|
|
1089
|
-
/**
|
|
1074
|
+
/** RFC 5280 §4.2.1.4: `DisplayText` is an IA5String, VisibleString, BMPString or UTF8String of SIZE (1..200). */
|
|
1090
1075
|
function parseDisplayText(e) {
|
|
1076
|
+
let t = decodeDisplayText(e), n = [...t].length;
|
|
1077
|
+
if (n < 1) throw Error("DisplayText must hold at least one character");
|
|
1078
|
+
return {
|
|
1079
|
+
text: t,
|
|
1080
|
+
...n > 200 ? { oversized: {
|
|
1081
|
+
characters: n,
|
|
1082
|
+
limit: 200
|
|
1083
|
+
} } : {}
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
function decodeDisplayText(e) {
|
|
1091
1087
|
switch (e.tag) {
|
|
1092
1088
|
case 12:
|
|
1093
1089
|
case 22:
|
|
1094
|
-
case
|
|
1095
|
-
case
|
|
1090
|
+
case 30: return h(e.tag, e.value);
|
|
1091
|
+
case 26: return g(e.value);
|
|
1096
1092
|
default: throw Error(`Unsupported DisplayText tag: ${e.tag}`);
|
|
1097
1093
|
}
|
|
1098
1094
|
}
|
|
1099
|
-
/**
|
|
1100
|
-
function decodeBmpString(e) {
|
|
1101
|
-
if (e.length % 2 != 0) throw Error("Invalid BMPString length");
|
|
1102
|
-
let t = "";
|
|
1103
|
-
for (let n = 0; n < e.length; n += 2) {
|
|
1104
|
-
let r = e[n], i = e[n + 1];
|
|
1105
|
-
if (r === void 0 || i === void 0) throw Error("Invalid BMPString content");
|
|
1106
|
-
t += String.fromCharCode(r << 8 | i);
|
|
1107
|
-
}
|
|
1108
|
-
return t;
|
|
1109
|
-
}
|
|
1095
|
+
/** Decodes the Authority Key Identifier extension, returning the keyIdentifier hex or undefined. */
|
|
1110
1096
|
function parseAuthorityKeyIdentifier(e) {
|
|
1111
|
-
let
|
|
1112
|
-
if (
|
|
1113
|
-
let
|
|
1097
|
+
let t = y(s(e, 0), "authorityKeyIdentifier sequence");
|
|
1098
|
+
if (t.end !== e.length) throw Error("Trailing data after DER element");
|
|
1099
|
+
let n = {
|
|
1114
1100
|
sawAuthorityCertIssuer: !1,
|
|
1115
1101
|
sawAuthorityCertSerialNumber: !1,
|
|
1116
1102
|
lastFieldOrder: -1
|
|
1117
|
-
},
|
|
1118
|
-
for (;
|
|
1119
|
-
let
|
|
1120
|
-
if (
|
|
1121
|
-
parseAuthorityKeyIdentifierField(e,
|
|
1103
|
+
}, r = t.start;
|
|
1104
|
+
for (; r < t.end;) {
|
|
1105
|
+
let i = s(e, r);
|
|
1106
|
+
if (i.end > t.end) throw Error("DER child exceeds parent length");
|
|
1107
|
+
parseAuthorityKeyIdentifierField(e, i, n), r = i.end;
|
|
1122
1108
|
}
|
|
1123
|
-
if (
|
|
1124
|
-
if (
|
|
1125
|
-
return
|
|
1109
|
+
if (r !== t.end) throw Error("Malformed DER sequence");
|
|
1110
|
+
if (n.sawAuthorityCertIssuer !== n.sawAuthorityCertSerialNumber) throw Error("authorityKeyIdentifier authorityCertIssuer and authorityCertSerialNumber must appear together");
|
|
1111
|
+
return n.keyIdentifier;
|
|
1126
1112
|
}
|
|
1127
1113
|
function parseAuthorityKeyIdentifierField(e, t, n) {
|
|
1128
1114
|
switch (t.tag) {
|
|
@@ -1141,16 +1127,16 @@ function parseAuthorityKeyIdentifierField(e, t, n) {
|
|
|
1141
1127
|
function parseAuthorityKeyIdentifierKeyId(e, t) {
|
|
1142
1128
|
if (t.keyIdentifier !== void 0) throw Error("authorityKeyIdentifier keyIdentifier must not repeat");
|
|
1143
1129
|
if (t.lastFieldOrder >= 0) throw Error("authorityKeyIdentifier fields must preserve DER order");
|
|
1144
|
-
t.keyIdentifier =
|
|
1130
|
+
t.keyIdentifier = b(e.value), t.lastFieldOrder = 0;
|
|
1145
1131
|
}
|
|
1146
1132
|
function parseAuthorityKeyIdentifierIssuer(e, t, n) {
|
|
1147
1133
|
if (n.sawAuthorityCertIssuer) throw Error("authorityKeyIdentifier authorityCertIssuer must not repeat");
|
|
1148
1134
|
if (n.lastFieldOrder >= 1) throw Error("authorityKeyIdentifier fields must preserve DER order");
|
|
1149
|
-
let r =
|
|
1135
|
+
let r = u(e, t);
|
|
1150
1136
|
if (r.length === 0) throw Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
|
|
1151
1137
|
for (let t of r) {
|
|
1152
1138
|
if ((t.tag & 192) != 128) throw Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
|
|
1153
|
-
|
|
1139
|
+
F(e, t);
|
|
1154
1140
|
}
|
|
1155
1141
|
n.sawAuthorityCertIssuer = !0, n.lastFieldOrder = 1;
|
|
1156
1142
|
}
|
|
@@ -1178,7 +1164,6 @@ function validateImplicitBitStringContent(e, t) {
|
|
|
1178
1164
|
if (i === void 0) throw Error(`${t} must use BIT STRING encoding`);
|
|
1179
1165
|
if (i & (1 << n) - 1) throw Error(`${t} BIT STRING must not set padding bits`);
|
|
1180
1166
|
}
|
|
1181
|
-
|
|
1182
|
-
export { certificateMatchesPrivateKey, decodeExtension, decodeExtensionMap, decodeExtensions, defineExtensionDecoder, defineExtensionDecoderMap, findExtension, getSubjectPublicKey, getSubjectPublicKeyOrThrow, matchCertificatePrivateKey, parseAuthorityInfoAccess, parseAuthorityKeyIdentifier, parseBasicConstraints, parseCertificateChainPem, parseCertificateDer, parseCertificateDerOrThrow, parseCertificateFromSource, parseCertificatePem, parseCertificatePemOrThrow, parseCertificatePolicies, parseCertificateSigningRequestDer, parseCertificateSigningRequestDerOrThrow, parseCertificateSigningRequestPem, parseCertificateSigningRequestPemOrThrow, parseCertificatesFromSource, parseCrlDistributionPoints, parseDistinguishedNameDer, parseExtendedKeyUsage, parseInhibitAnyPolicy, parseKeyUsage, parseNameConstraints, parsePolicyConstraints, parsePolicyMappings, parseSubjectAltNames };
|
|
1167
|
+
export { certificateMatchesPrivateKey, decodeExtension, decodeExtensionMap, decodeExtensions, defineExtensionDecoder, defineExtensionDecoderMap, findExtension, getSubjectPublicKey, getSubjectPublicKeyOrThrow, matchCertificatePrivateKey, parseAuthorityInfoAccess, parseAuthorityKeyIdentifier, parseBasicConstraints, parseCertificateChainPem, parseCertificateChainPemOrThrow, parseCertificateDer, parseCertificateDerOrThrow, parseCertificateFromSource, parseCertificatePem, parseCertificatePemOrThrow, parseCertificatePolicies, parseCertificateSigningRequestDer, parseCertificateSigningRequestDerOrThrow, parseCertificateSigningRequestPem, parseCertificateSigningRequestPemOrThrow, parseCertificatesFromSource, parseCrlDistributionPoints, parseDistinguishedNameDer, parseExtendedKeyUsage, parseInhibitAnyPolicy, parseKeyUsage, parseNameConstraints, parsePolicyConstraints, parsePolicyMappings, parseSubjectAltNames };
|
|
1183
1168
|
|
|
1184
1169
|
//# sourceMappingURL=parse.js.map
|