micro509 0.9.0 → 0.11.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 -4
- package/dist/index.js +16 -15
- package/dist/internal/asn1/asn1.js +145 -186
- package/dist/internal/asn1/asn1.js.map +1 -0
- package/dist/internal/asn1/der.js +210 -227
- package/dist/internal/asn1/der.js.map +1 -0
- package/dist/internal/asn1/oids.js +297 -101
- package/dist/internal/asn1/oids.js.map +1 -0
- package/dist/internal/asn1/oids2.js +69 -0
- package/dist/internal/asn1/oids2.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +37 -35
- package/dist/internal/crypto/algorithm-names.js.map +1 -0
- package/dist/internal/crypto/ecdsa.js +25 -28
- package/dist/internal/crypto/ecdsa.js.map +1 -0
- package/dist/internal/crypto/hash.js +56 -117
- package/dist/internal/crypto/hash.js.map +1 -0
- package/dist/internal/crypto/pbes2.js +122 -133
- package/dist/internal/crypto/pbes2.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +118 -124
- package/dist/internal/crypto/rsa-pss.js.map +1 -0
- package/dist/internal/crypto/sig-verify.js +87 -99
- package/dist/internal/crypto/sig-verify.js.map +1 -0
- package/dist/internal/crypto/signing.d.ts +12 -8
- package/dist/internal/crypto/signing.js +53 -55
- package/dist/internal/crypto/signing.js.map +1 -0
- package/dist/internal/crypto/webcrypto.js +5 -3
- package/dist/internal/crypto/webcrypto.js.map +1 -0
- package/dist/internal/shared/base64.js +10 -9
- package/dist/internal/shared/base64.js.map +1 -0
- package/dist/internal/shared/dn.js +50 -57
- package/dist/internal/shared/dn.js.map +1 -0
- package/dist/internal/shared/ip.js +43 -53
- package/dist/internal/shared/ip.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +211 -239
- package/dist/internal/verify/name-constraints-engine.js.map +1 -0
- package/dist/internal/verify/policy-engine.js +228 -247
- package/dist/internal/verify/policy-engine.js.map +1 -0
- package/dist/internal/verify/verify-path.js +213 -228
- package/dist/internal/verify/verify-path.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +62 -59
- package/dist/internal/x509/extension-bits.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +133 -164
- package/dist/internal/x509/extension-registry.js.map +1 -0
- package/dist/internal/x509/name-fields.js +35 -41
- package/dist/internal/x509/name-fields.js.map +1 -0
- package/dist/keys/keys.d.ts +583 -547
- package/dist/keys/keys.js +519 -431
- package/dist/keys/keys.js.map +1 -0
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +46 -46
- package/dist/pem/pem.js +62 -81
- package/dist/pem/pem.js.map +1 -0
- package/dist/pem.js +2 -2
- package/dist/pkcs/pfx.d.ts +96 -80
- package/dist/pkcs/pfx.js +191 -231
- package/dist/pkcs/pfx.js.map +1 -0
- package/dist/pkcs/pkcs12-mac.d.ts +20 -18
- package/dist/pkcs/pkcs12-mac.js +90 -111
- package/dist/pkcs/pkcs12-mac.js.map +1 -0
- package/dist/pkcs/pkcs7.d.ts +118 -69
- package/dist/pkcs/pkcs7.js +401 -443
- package/dist/pkcs/pkcs7.js.map +1 -0
- package/dist/pkcs.d.ts +2 -2
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +37 -32
- package/dist/result/result.js +46 -44
- package/dist/result/result.js.map +1 -0
- package/dist/result.js +2 -2
- package/dist/revocation/chain.d.ts +138 -119
- package/dist/revocation/chain.js +248 -281
- package/dist/revocation/chain.js.map +1 -0
- package/dist/revocation/crl.d.ts +125 -123
- package/dist/revocation/crl.js +653 -745
- package/dist/revocation/crl.js.map +1 -0
- package/dist/revocation/ocsp.d.ts +183 -177
- package/dist/revocation/ocsp.js +579 -636
- package/dist/revocation/ocsp.js.map +1 -0
- package/dist/revocation/revocation.d.ts +26 -26
- package/dist/revocation/revocation.js +104 -125
- package/dist/revocation/revocation.js.map +1 -0
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +42 -42
- package/dist/verify/identity.js +158 -167
- package/dist/verify/identity.js.map +1 -0
- package/dist/verify/name-constraints.d.ts +7 -7
- package/dist/verify/policy.d.ts +10 -10
- package/dist/verify/verify.d.ts +163 -163
- package/dist/verify/verify.js +396 -392
- package/dist/verify/verify.js.map +1 -0
- package/dist/verify.js +3 -3
- package/dist/x509/certificate.d.ts +117 -117
- package/dist/x509/certificate.js +51 -60
- package/dist/x509/certificate.js.map +1 -0
- package/dist/x509/csr.d.ts +25 -25
- package/dist/x509/csr.js +26 -30
- package/dist/x509/csr.js.map +1 -0
- package/dist/x509/extensions.d.ts +228 -184
- package/dist/x509/extensions.js +175 -200
- package/dist/x509/extensions.js.map +1 -0
- package/dist/x509/fingerprint.d.ts +55 -0
- package/dist/x509/fingerprint.js +55 -0
- package/dist/x509/fingerprint.js.map +1 -0
- package/dist/x509/name.d.ts +94 -93
- package/dist/x509/name.js +30 -29
- package/dist/x509/name.js.map +1 -0
- package/dist/x509/parse.d.ts +288 -185
- package/dist/x509/parse.js +762 -677
- package/dist/x509/parse.js.map +1 -0
- package/dist/x509.d.ts +3 -2
- package/dist/x509.js +7 -6
- package/package.json +79 -41
package/dist/x509/parse.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { encodeLength, readElement, readRootElement, readSequenceChildren } from "../internal/asn1/der.js";
|
|
2
|
-
import { childrenOf, decodeBoolean, decodeIntegerNumber, decodeNonNegativeIntegerNumber, decodeObjectIdentifier, decodeString, extractBitStringValue, parseTime, requireElement, toHex } from "../internal/asn1/asn1.js";
|
|
3
|
-
import { OIDS } from "../internal/asn1/
|
|
4
|
-
import { failureResult, rethrowIfInvariant, successResult } from "../result/result.js";
|
|
5
|
-
import { pemDecodeOrThrow, splitPemBlocksOrThrow } from "../pem/pem.js";
|
|
6
|
-
import { importSpkiDer, importSpkiDerOrThrow } from "../keys/keys.js";
|
|
7
|
-
import { describePublicKeyAlgorithm, describeSignatureAlgorithm } from "../internal/crypto/algorithm-names.js";
|
|
8
|
-
import { decodeIpAddress } from "../internal/shared/ip.js";
|
|
9
|
-
import { parseDistributionPointReasonFlagsContent, parseKeyUsageExtension } from "../internal/x509/extension-bits.js";
|
|
10
|
-
import { nameFieldKeyFromOid } from "../internal/x509/name-fields.js";
|
|
11
|
-
import { parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid } from "./extensions.js";
|
|
12
|
-
import { decodeAndApplyKnownExtension } from "../internal/x509/extension-registry.js";
|
|
1
|
+
import { encodeLength as e, readElement as t, readRootElement as n, readSequenceChildren as r } from "../internal/asn1/der.js";
|
|
2
|
+
import { childrenOf as i, decodeBoolean as a, decodeIntegerNumber as o, decodeNonNegativeIntegerNumber as s, decodeObjectIdentifier as c, decodeString as l, extractBitStringValue as u, parseTime as d, requireElement as f, toHex as p } from "../internal/asn1/asn1.js";
|
|
3
|
+
import { OIDS as m } from "../internal/asn1/oids2.js";
|
|
4
|
+
import { failureResult as h, rethrowIfInvariant as g, successResult as _ } from "../result/result.js";
|
|
5
|
+
import { pemDecodeOrThrow as v, splitPemBlocksOrThrow as y } from "../pem/pem.js";
|
|
6
|
+
import { derivePublicKey as b, exportSpkiDer as x, importSpkiDer as S, importSpkiDerOrThrow as C } from "../keys/keys.js";
|
|
7
|
+
import { describePublicKeyAlgorithm as w, describeSignatureAlgorithm as T } from "../internal/crypto/algorithm-names.js";
|
|
8
|
+
import { decodeIpAddress as E } from "../internal/shared/ip.js";
|
|
9
|
+
import { parseDistributionPointReasonFlagsContent as D, parseKeyUsageExtension as O } from "../internal/x509/extension-bits.js";
|
|
10
|
+
import { nameFieldKeyFromOid as k } from "../internal/x509/name-fields.js";
|
|
11
|
+
import { parseAuthorityInfoAccessMethodOid as A, parseExtendedKeyUsageOid as j } from "./extensions.js";
|
|
12
|
+
import { decodeAndApplyKnownExtension as M } from "../internal/x509/extension-registry.js";
|
|
13
13
|
//#region src/x509/parse.ts
|
|
14
14
|
/**
|
|
15
15
|
* Certificate and CSR parse boundary.
|
|
@@ -22,15 +22,15 @@ import { decodeAndApplyKnownExtension } from "../internal/x509/extension-registr
|
|
|
22
22
|
* @module
|
|
23
23
|
*/
|
|
24
24
|
/** Shared UTF-8 decoder for IA5String / UTF8String values. */
|
|
25
|
-
const
|
|
25
|
+
const N = new TextDecoder();
|
|
26
26
|
/**
|
|
27
27
|
* Identity helper that narrows the type of a custom {@linkcode ExtensionDecoder} literal.
|
|
28
28
|
*
|
|
29
29
|
* @param decoder Decoder definition to return unchanged.
|
|
30
30
|
* @returns The same decoder, properly typed.
|
|
31
31
|
*/
|
|
32
|
-
function defineExtensionDecoder(
|
|
33
|
-
return
|
|
32
|
+
function defineExtensionDecoder(e) {
|
|
33
|
+
return e;
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
36
|
* Identity helper that narrows the type of a custom {@linkcode ExtensionDecoderMap} literal.
|
|
@@ -38,8 +38,8 @@ function defineExtensionDecoder(decoder) {
|
|
|
38
38
|
* @param decoderMap Map of named decoders to return unchanged.
|
|
39
39
|
* @returns The same map, properly typed.
|
|
40
40
|
*/
|
|
41
|
-
function defineExtensionDecoderMap(
|
|
42
|
-
return
|
|
41
|
+
function defineExtensionDecoderMap(e) {
|
|
42
|
+
return e;
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Throwing core for {@linkcode parseCertificateDer}.
|
|
@@ -52,94 +52,99 @@ function defineExtensionDecoderMap(decoderMap) {
|
|
|
52
52
|
* @param der Raw DER bytes of an X.509 certificate.
|
|
53
53
|
* @param options Custom extension decoders to apply during parsing.
|
|
54
54
|
*/
|
|
55
|
-
function parseCertificateDerOrThrow(
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const tbsChildren = childrenOf(der, tbsCertificate);
|
|
62
|
-
let index = 0;
|
|
63
|
-
let version = 1;
|
|
64
|
-
const maybeVersion = tbsChildren[index];
|
|
65
|
-
if (maybeVersion?.tag === 160) {
|
|
66
|
-
const versionChildren = childrenOf(der, maybeVersion);
|
|
67
|
-
const versionElement = requireElement(versionChildren[0], "version INTEGER");
|
|
68
|
-
if (versionChildren.length !== 1 || versionElement.tag !== 2) throw new Error("version must use INTEGER");
|
|
69
|
-
version = decodeIntegerNumber(versionElement.value) + 1;
|
|
70
|
-
if (version < 1 || version > 3) throw new Error(`Unsupported certificate version: ${String(version)}`);
|
|
71
|
-
index += 1;
|
|
72
|
-
}
|
|
73
|
-
const serialNumber = requireElement(tbsChildren[index], "serialNumber");
|
|
74
|
-
if (serialNumber.tag !== 2) throw new Error("serialNumber must use INTEGER");
|
|
75
|
-
const tbsSignatureAlgorithm = requireElement(tbsChildren[index + 1], "TBSCertificate signature");
|
|
76
|
-
const issuer = requireElement(tbsChildren[index + 2], "issuer");
|
|
77
|
-
const validity = requireElement(tbsChildren[index + 3], "validity");
|
|
78
|
-
const subject = requireElement(tbsChildren[index + 4], "subject");
|
|
79
|
-
const subjectPublicKeyInfo = requireElement(tbsChildren[index + 5], "subjectPublicKeyInfo");
|
|
80
|
-
let cursor = index + 6;
|
|
81
|
-
const issuerUniqueIdElement = tbsChildren[cursor];
|
|
82
|
-
if (issuerUniqueIdElement?.tag === 129) {
|
|
83
|
-
if (version < 2) throw new Error("issuerUniqueID requires certificate version 2 or 3");
|
|
84
|
-
validateImplicitBitStringContent(issuerUniqueIdElement.value, "issuerUniqueID");
|
|
85
|
-
cursor += 1;
|
|
86
|
-
}
|
|
87
|
-
const subjectUniqueIdElement = tbsChildren[cursor];
|
|
88
|
-
if (subjectUniqueIdElement?.tag === 130) {
|
|
89
|
-
if (version < 2) throw new Error("subjectUniqueID requires certificate version 2 or 3");
|
|
90
|
-
validateImplicitBitStringContent(subjectUniqueIdElement.value, "subjectUniqueID");
|
|
91
|
-
cursor += 1;
|
|
92
|
-
}
|
|
93
|
-
const extensions = tbsChildren[cursor]?.tag === 163 ? tbsChildren[cursor] : void 0;
|
|
94
|
-
if (extensions !== void 0) {
|
|
95
|
-
if (version !== 3) throw new Error("extensions require certificate version 3");
|
|
96
|
-
cursor += 1;
|
|
97
|
-
}
|
|
98
|
-
if (cursor !== tbsChildren.length) throw new Error(`Unsupported TBSCertificate field tag: ${String(requireElement(tbsChildren[cursor], "TBSCertificate field").tag)}`);
|
|
99
|
-
const parsedExtensions = parseExtensionContainer(der, extensions);
|
|
100
|
-
const parsedValidity = parseValidity(der, validity);
|
|
101
|
-
const parsedSpki = parseSubjectPublicKeyInfo(der, subjectPublicKeyInfo);
|
|
102
|
-
const parsedTbsSignatureAlgorithm = parseAlgorithmIdentifier(der, tbsSignatureAlgorithm);
|
|
103
|
-
const parsedSignatureAlgorithm = parseAlgorithmIdentifier(der, signatureAlgorithm);
|
|
104
|
-
assertMatchingCertificateSignatureAlgorithms(parsedTbsSignatureAlgorithm, parsedSignatureAlgorithm);
|
|
105
|
-
const decodedExtensions = options?.decoders === void 0 ? void 0 : decodeExtensions(parsedExtensions.all, options.decoders);
|
|
106
|
-
const decodedExtensionMap = options?.decoderMap === void 0 ? void 0 : decodeExtensionMap(parsedExtensions.all, options.decoderMap);
|
|
55
|
+
function parseCertificateDerOrThrow(e, t) {
|
|
56
|
+
let n = r(e, { maxDepth: 64 });
|
|
57
|
+
if (n.length !== 3) throw Error("Malformed Certificate");
|
|
58
|
+
let i = f(n[0], "TBSCertificate"), a = f(n[1], "signatureAlgorithm"), o = f(n[2], "signatureValue"), s = parseTbsCertificateFields(e, i), c = parseExtensionContainer(e, s.extensions), l = parseValidity(e, s.validity), d = parseSubjectPublicKeyInfo(e, s.subjectPublicKeyInfo), m = parseAlgorithmIdentifier(e, s.tbsSignatureAlgorithm), h = parseAlgorithmIdentifier(e, a);
|
|
59
|
+
assertMatchingCertificateSignatureAlgorithms(m, h);
|
|
60
|
+
let g = parseCustomExtensions(c.all, t);
|
|
107
61
|
return {
|
|
108
|
-
der: new Uint8Array(
|
|
109
|
-
version,
|
|
110
|
-
serialNumberHex:
|
|
111
|
-
tbsCertificateDer:
|
|
112
|
-
subjectPublicKeyInfoDer:
|
|
113
|
-
signatureValue:
|
|
114
|
-
issuer: parseName(
|
|
115
|
-
subject: parseName(
|
|
116
|
-
notBefore:
|
|
117
|
-
notAfter:
|
|
118
|
-
signatureAlgorithmOid:
|
|
119
|
-
signatureAlgorithmName:
|
|
120
|
-
...
|
|
121
|
-
publicKeyAlgorithmOid:
|
|
122
|
-
publicKeyAlgorithmName:
|
|
123
|
-
...
|
|
124
|
-
...
|
|
125
|
-
extensions:
|
|
126
|
-
...
|
|
127
|
-
...
|
|
128
|
-
...
|
|
129
|
-
...
|
|
130
|
-
...
|
|
131
|
-
...
|
|
132
|
-
...
|
|
133
|
-
...
|
|
134
|
-
...
|
|
135
|
-
...
|
|
136
|
-
...
|
|
137
|
-
...decodedExtensions
|
|
138
|
-
...decodedExtensionMap
|
|
139
|
-
...
|
|
140
|
-
...
|
|
62
|
+
der: new Uint8Array(e),
|
|
63
|
+
version: s.version,
|
|
64
|
+
serialNumberHex: p(s.serialNumber.value),
|
|
65
|
+
tbsCertificateDer: e.slice(i.start - i.headerLength, i.end),
|
|
66
|
+
subjectPublicKeyInfoDer: e.slice(s.subjectPublicKeyInfo.start - s.subjectPublicKeyInfo.headerLength, s.subjectPublicKeyInfo.end),
|
|
67
|
+
signatureValue: u(o),
|
|
68
|
+
issuer: parseName(e, s.issuer),
|
|
69
|
+
subject: parseName(e, s.subject),
|
|
70
|
+
notBefore: l.notBefore,
|
|
71
|
+
notAfter: l.notAfter,
|
|
72
|
+
signatureAlgorithmOid: h.oid,
|
|
73
|
+
signatureAlgorithmName: T(h.oid, h.parametersDer),
|
|
74
|
+
...h.parametersDer === void 0 ? {} : { signatureAlgorithmParametersDer: h.parametersDer },
|
|
75
|
+
publicKeyAlgorithmOid: d.oid,
|
|
76
|
+
publicKeyAlgorithmName: w(d.oid, d.parametersOid),
|
|
77
|
+
...d.parametersDer === void 0 ? {} : { publicKeyAlgorithmParametersDer: d.parametersDer },
|
|
78
|
+
...d.parametersOid === void 0 ? {} : { publicKeyParametersOid: d.parametersOid },
|
|
79
|
+
extensions: c.all,
|
|
80
|
+
...c.basicConstraints === void 0 ? {} : { basicConstraints: c.basicConstraints },
|
|
81
|
+
...c.keyUsage === void 0 ? {} : { keyUsage: c.keyUsage },
|
|
82
|
+
...c.extendedKeyUsage === void 0 ? {} : { extendedKeyUsage: c.extendedKeyUsage },
|
|
83
|
+
...c.subjectAltNames === void 0 ? {} : { subjectAltNames: c.subjectAltNames },
|
|
84
|
+
...c.nameConstraints === void 0 ? {} : { nameConstraints: c.nameConstraints },
|
|
85
|
+
...c.certificatePolicies === void 0 ? {} : { certificatePolicies: c.certificatePolicies },
|
|
86
|
+
...c.policyMappings === void 0 ? {} : { policyMappings: c.policyMappings },
|
|
87
|
+
...c.policyConstraints === void 0 ? {} : { policyConstraints: c.policyConstraints },
|
|
88
|
+
...c.inhibitAnyPolicy === void 0 ? {} : { inhibitAnyPolicy: c.inhibitAnyPolicy },
|
|
89
|
+
...c.authorityInfoAccess === void 0 ? {} : { authorityInfoAccess: c.authorityInfoAccess },
|
|
90
|
+
...c.crlDistributionPoints === void 0 ? {} : { crlDistributionPoints: c.crlDistributionPoints },
|
|
91
|
+
...g.decodedExtensions === void 0 ? {} : { decodedExtensions: g.decodedExtensions },
|
|
92
|
+
...g.decodedExtensionMap === void 0 ? {} : { decodedExtensionMap: g.decodedExtensionMap },
|
|
93
|
+
...c.subjectKeyIdentifier === void 0 ? {} : { subjectKeyIdentifier: c.subjectKeyIdentifier },
|
|
94
|
+
...c.authorityKeyIdentifier === void 0 ? {} : { authorityKeyIdentifier: c.authorityKeyIdentifier }
|
|
141
95
|
};
|
|
142
96
|
}
|
|
97
|
+
function parseCustomExtensions(e, t) {
|
|
98
|
+
return {
|
|
99
|
+
...t?.decoders === void 0 ? {} : { decodedExtensions: decodeExtensions(e, t.decoders) },
|
|
100
|
+
...t?.decoderMap === void 0 ? {} : { decodedExtensionMap: decodeExtensionMap(e, t.decoderMap) }
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** Extracts and validates the structural fields of a TBSCertificate sequence. */
|
|
104
|
+
function parseTbsCertificateFields(e, t) {
|
|
105
|
+
let n = i(e, t), r = parseCertificateVersionField(e, n), a = r.version, o = r.nextIndex, s = f(n[o], "serialNumber");
|
|
106
|
+
if (s.tag !== 2) throw Error("serialNumber must use INTEGER");
|
|
107
|
+
let c = validateOptionalTbsCertificateFields(n, o + 6, a);
|
|
108
|
+
return {
|
|
109
|
+
version: a,
|
|
110
|
+
serialNumber: s,
|
|
111
|
+
tbsSignatureAlgorithm: f(n[o + 1], "TBSCertificate signature"),
|
|
112
|
+
issuer: f(n[o + 2], "issuer"),
|
|
113
|
+
validity: f(n[o + 3], "validity"),
|
|
114
|
+
subject: f(n[o + 4], "subject"),
|
|
115
|
+
subjectPublicKeyInfo: f(n[o + 5], "subjectPublicKeyInfo"),
|
|
116
|
+
...n[c - 1]?.tag === 163 ? { extensions: n[c - 1] } : {}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function parseCertificateVersionField(e, t) {
|
|
120
|
+
let n = t[0];
|
|
121
|
+
if (n?.tag !== 160) return {
|
|
122
|
+
version: 1,
|
|
123
|
+
nextIndex: 0
|
|
124
|
+
};
|
|
125
|
+
let r = i(e, n), a = f(r[0], "version INTEGER");
|
|
126
|
+
if (r.length !== 1 || a.tag !== 2) throw Error("version must use INTEGER");
|
|
127
|
+
let s = o(a.value) + 1;
|
|
128
|
+
if (s < 1 || s > 3) throw Error(`Unsupported certificate version: ${String(s)}`);
|
|
129
|
+
return {
|
|
130
|
+
version: s,
|
|
131
|
+
nextIndex: 1
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function validateOptionalTbsCertificateFields(e, t, n) {
|
|
135
|
+
let r = t;
|
|
136
|
+
if (e[r]?.tag === 129 && (validateCertificateUniqueId(e[r], n, "issuerUniqueID"), r += 1), e[r]?.tag === 130 && (validateCertificateUniqueId(e[r], n, "subjectUniqueID"), r += 1), e[r]?.tag === 163) {
|
|
137
|
+
if (n !== 3) throw Error("extensions require certificate version 3");
|
|
138
|
+
r += 1;
|
|
139
|
+
}
|
|
140
|
+
if (r !== e.length) throw Error(`Unsupported TBSCertificate field tag: ${String(f(e[r], "TBSCertificate field").tag)}`);
|
|
141
|
+
return r;
|
|
142
|
+
}
|
|
143
|
+
function validateCertificateUniqueId(e, t, n) {
|
|
144
|
+
let r = f(e, n);
|
|
145
|
+
if (t < 2) throw Error(`${n} requires certificate version 2 or 3`);
|
|
146
|
+
validateImplicitBitStringContent(r.value, n);
|
|
147
|
+
}
|
|
143
148
|
/**
|
|
144
149
|
* Decode a DER-encoded X.509 certificate into a {@linkcode ParsedCertificate}.
|
|
145
150
|
*
|
|
@@ -156,12 +161,11 @@ function parseCertificateDerOrThrow(der, options) {
|
|
|
156
161
|
* @param der Raw DER bytes of an X.509 certificate.
|
|
157
162
|
* @param options Custom extension decoders to apply during parsing.
|
|
158
163
|
*/
|
|
159
|
-
function parseCertificateDer(
|
|
164
|
+
function parseCertificateDer(e, t) {
|
|
160
165
|
try {
|
|
161
|
-
return
|
|
162
|
-
} catch (
|
|
163
|
-
|
|
164
|
-
return failureResult("malformed", error instanceof Error ? error.message : "Malformed certificate");
|
|
166
|
+
return _(parseCertificateDerOrThrow(e, t));
|
|
167
|
+
} catch (e) {
|
|
168
|
+
return g(e), h("malformed", e instanceof Error ? e.message : "Malformed certificate");
|
|
165
169
|
}
|
|
166
170
|
}
|
|
167
171
|
/**
|
|
@@ -182,8 +186,8 @@ function parseCertificateDer(der, options) {
|
|
|
182
186
|
* @param pem PEM string with a CERTIFICATE block.
|
|
183
187
|
* @param options Custom extension decoders to apply during parsing.
|
|
184
188
|
*/
|
|
185
|
-
function parseCertificatePemOrThrow(
|
|
186
|
-
return parseCertificateDerOrThrow(
|
|
189
|
+
function parseCertificatePemOrThrow(e, t) {
|
|
190
|
+
return parseCertificateDerOrThrow(v("CERTIFICATE", e), t);
|
|
187
191
|
}
|
|
188
192
|
/**
|
|
189
193
|
* Decode a PEM-encoded X.509 certificate into a {@linkcode ParsedCertificate}.
|
|
@@ -194,23 +198,20 @@ function parseCertificatePemOrThrow(pem, options) {
|
|
|
194
198
|
* @param pem PEM string with a CERTIFICATE block.
|
|
195
199
|
* @param options Custom extension decoders to apply during parsing.
|
|
196
200
|
*/
|
|
197
|
-
function parseCertificatePem(
|
|
201
|
+
function parseCertificatePem(e, t) {
|
|
198
202
|
try {
|
|
199
|
-
return
|
|
200
|
-
} catch (
|
|
201
|
-
|
|
202
|
-
return failureResult("malformed", error instanceof Error ? error.message : "Malformed certificate");
|
|
203
|
+
return _(parseCertificatePemOrThrow(e, t));
|
|
204
|
+
} catch (e) {
|
|
205
|
+
return g(e), h("malformed", e instanceof Error ? e.message : "Malformed certificate");
|
|
203
206
|
}
|
|
204
207
|
}
|
|
205
208
|
/** Normalizes a PEM bundle or single DER certificate source into parsed certificates. */
|
|
206
|
-
function parseCertificatesFromSource(
|
|
207
|
-
return typeof
|
|
209
|
+
function parseCertificatesFromSource(e, t) {
|
|
210
|
+
return typeof e == "string" ? parseCertificatesFromPemBlocks(e, t) : [parseCertificateDerOrThrow(new Uint8Array(e), t)];
|
|
208
211
|
}
|
|
209
212
|
/** Normalizes a PEM, DER, or already-parsed certificate source into one parsed certificate. */
|
|
210
|
-
function parseCertificateFromSource(
|
|
211
|
-
|
|
212
|
-
if (hasParsedCertificateShape(source)) return source;
|
|
213
|
-
return parseCertificateDerOrThrow(new Uint8Array(source), options);
|
|
213
|
+
function parseCertificateFromSource(e, t) {
|
|
214
|
+
return typeof e == "string" ? parseCertificatePemOrThrow(e, t) : hasParsedCertificateShape(e) ? e : parseCertificateDerOrThrow(new Uint8Array(e), t);
|
|
214
215
|
}
|
|
215
216
|
/**
|
|
216
217
|
* Decode a PEM bundle containing one or more certificates.
|
|
@@ -220,8 +221,8 @@ function parseCertificateFromSource(source, options) {
|
|
|
220
221
|
* @param pemBundle PEM text that may contain multiple CERTIFICATE blocks.
|
|
221
222
|
* @param options Custom extension decoders to apply during parsing.
|
|
222
223
|
*/
|
|
223
|
-
function parseCertificateChainPem(
|
|
224
|
-
return parseCertificatesFromPemBlocks(
|
|
224
|
+
function parseCertificateChainPem(e, t) {
|
|
225
|
+
return parseCertificatesFromPemBlocks(e, t);
|
|
225
226
|
}
|
|
226
227
|
/**
|
|
227
228
|
* Decode a DER-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
|
|
@@ -229,54 +230,53 @@ function parseCertificateChainPem(pemBundle, options) {
|
|
|
229
230
|
* @param der Raw DER bytes of a PKCS#10 certificate signing request.
|
|
230
231
|
* @param options Custom extension decoders to apply during parsing.
|
|
231
232
|
*/
|
|
232
|
-
function parseCertificateSigningRequestDerOrThrow(
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
|
|
236
|
-
const signatureAlgorithm = requireElement(topLevel[1], "signatureAlgorithm");
|
|
237
|
-
const signatureValue = requireElement(topLevel[2], "signatureValue");
|
|
238
|
-
const criChildren = childrenOf(der, certificationRequestInfo);
|
|
239
|
-
if (criChildren.length < 3 || criChildren.length > 4) throw new Error("Malformed CertificationRequestInfo");
|
|
240
|
-
const versionElement = requireElement(criChildren[0], "version");
|
|
241
|
-
if (versionElement.tag !== 2) throw new Error("version must use INTEGER");
|
|
242
|
-
const version = decodeIntegerNumber(versionElement.value) + 1;
|
|
243
|
-
if (version !== 1) throw new Error(`Unsupported CertificationRequestInfo version: ${String(version)}`);
|
|
244
|
-
const subject = requireElement(criChildren[1], "subject");
|
|
245
|
-
const subjectPublicKeyInfo = requireElement(criChildren[2], "subjectPublicKeyInfo");
|
|
246
|
-
const attributes = criChildren[3];
|
|
247
|
-
if (attributes !== void 0 && attributes.tag !== 160) throw new Error("CertificationRequestInfo attributes must use [0]");
|
|
248
|
-
const parsedExtensions = parseRequestedExtensions(der, attributes);
|
|
249
|
-
const parsedSpki = parseSubjectPublicKeyInfo(der, subjectPublicKeyInfo);
|
|
250
|
-
const parsedSignatureAlgorithm = parseAlgorithmIdentifier(der, signatureAlgorithm);
|
|
251
|
-
const decodedExtensions = options?.decoders === void 0 ? void 0 : decodeExtensions(parsedExtensions.all, options.decoders);
|
|
252
|
-
const decodedExtensionMap = options?.decoderMap === void 0 ? void 0 : decodeExtensionMap(parsedExtensions.all, options.decoderMap);
|
|
233
|
+
function parseCertificateSigningRequestDerOrThrow(e, t) {
|
|
234
|
+
let n = r(e, { maxDepth: 64 });
|
|
235
|
+
if (n.length !== 3) throw Error("Malformed CertificationRequest");
|
|
236
|
+
let i = f(n[0], "CertificationRequestInfo"), a = f(n[1], "signatureAlgorithm"), o = f(n[2], "signatureValue"), s = parseCertificationRequestInfoFields(e, i), c = parseRequestedExtensions(e, s.attributes), l = parseSubjectPublicKeyInfo(e, s.subjectPublicKeyInfo), d = parseAlgorithmIdentifier(e, a), p = parseCustomExtensions(c.all, t);
|
|
253
237
|
return {
|
|
254
|
-
version,
|
|
255
|
-
certificationRequestInfoDer:
|
|
256
|
-
subjectPublicKeyInfoDer:
|
|
257
|
-
signatureValue:
|
|
258
|
-
subject: parseName(
|
|
259
|
-
signatureAlgorithmOid:
|
|
260
|
-
signatureAlgorithmName:
|
|
261
|
-
...
|
|
262
|
-
publicKeyAlgorithmOid:
|
|
263
|
-
publicKeyAlgorithmName:
|
|
264
|
-
...
|
|
265
|
-
...
|
|
266
|
-
requestedExtensions:
|
|
267
|
-
...
|
|
268
|
-
...
|
|
269
|
-
...
|
|
270
|
-
...
|
|
271
|
-
...
|
|
272
|
-
...
|
|
273
|
-
...
|
|
274
|
-
...
|
|
275
|
-
...
|
|
276
|
-
...
|
|
277
|
-
...
|
|
278
|
-
...decodedExtensions
|
|
279
|
-
...decodedExtensionMap
|
|
238
|
+
version: s.version,
|
|
239
|
+
certificationRequestInfoDer: e.slice(i.start - i.headerLength, i.end),
|
|
240
|
+
subjectPublicKeyInfoDer: e.slice(s.subjectPublicKeyInfo.start - s.subjectPublicKeyInfo.headerLength, s.subjectPublicKeyInfo.end),
|
|
241
|
+
signatureValue: u(o),
|
|
242
|
+
subject: parseName(e, s.subject),
|
|
243
|
+
signatureAlgorithmOid: d.oid,
|
|
244
|
+
signatureAlgorithmName: T(d.oid, d.parametersDer),
|
|
245
|
+
...d.parametersDer === void 0 ? {} : { signatureAlgorithmParametersDer: d.parametersDer },
|
|
246
|
+
publicKeyAlgorithmOid: l.oid,
|
|
247
|
+
publicKeyAlgorithmName: w(l.oid, l.parametersOid),
|
|
248
|
+
...l.parametersDer === void 0 ? {} : { publicKeyAlgorithmParametersDer: l.parametersDer },
|
|
249
|
+
...l.parametersOid === void 0 ? {} : { publicKeyParametersOid: l.parametersOid },
|
|
250
|
+
requestedExtensions: c.all,
|
|
251
|
+
...c.basicConstraints === void 0 ? {} : { basicConstraints: c.basicConstraints },
|
|
252
|
+
...c.keyUsage === void 0 ? {} : { keyUsage: c.keyUsage },
|
|
253
|
+
...c.extendedKeyUsage === void 0 ? {} : { extendedKeyUsage: c.extendedKeyUsage },
|
|
254
|
+
...c.subjectAltNames === void 0 ? {} : { subjectAltNames: c.subjectAltNames },
|
|
255
|
+
...c.nameConstraints === void 0 ? {} : { nameConstraints: c.nameConstraints },
|
|
256
|
+
...c.certificatePolicies === void 0 ? {} : { certificatePolicies: c.certificatePolicies },
|
|
257
|
+
...c.policyMappings === void 0 ? {} : { policyMappings: c.policyMappings },
|
|
258
|
+
...c.policyConstraints === void 0 ? {} : { policyConstraints: c.policyConstraints },
|
|
259
|
+
...c.inhibitAnyPolicy === void 0 ? {} : { inhibitAnyPolicy: c.inhibitAnyPolicy },
|
|
260
|
+
...c.authorityInfoAccess === void 0 ? {} : { authorityInfoAccess: c.authorityInfoAccess },
|
|
261
|
+
...c.crlDistributionPoints === void 0 ? {} : { crlDistributionPoints: c.crlDistributionPoints },
|
|
262
|
+
...p.decodedExtensions === void 0 ? {} : { decodedExtensions: p.decodedExtensions },
|
|
263
|
+
...p.decodedExtensionMap === void 0 ? {} : { decodedExtensionMap: p.decodedExtensionMap }
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function parseCertificationRequestInfoFields(e, t) {
|
|
267
|
+
let n = i(e, t);
|
|
268
|
+
if (n.length < 3 || n.length > 4) throw Error("Malformed CertificationRequestInfo");
|
|
269
|
+
let r = f(n[0], "version");
|
|
270
|
+
if (r.tag !== 2) throw Error("version must use INTEGER");
|
|
271
|
+
let a = o(r.value) + 1;
|
|
272
|
+
if (a !== 1) throw Error(`Unsupported CertificationRequestInfo version: ${String(a)}`);
|
|
273
|
+
let s = n[3];
|
|
274
|
+
if (s !== void 0 && s.tag !== 160) throw Error("CertificationRequestInfo attributes must use [0]");
|
|
275
|
+
return {
|
|
276
|
+
version: a,
|
|
277
|
+
subject: f(n[1], "subject"),
|
|
278
|
+
subjectPublicKeyInfo: f(n[2], "subjectPublicKeyInfo"),
|
|
279
|
+
...s === void 0 ? {} : { attributes: s }
|
|
280
280
|
};
|
|
281
281
|
}
|
|
282
282
|
/**
|
|
@@ -285,19 +285,18 @@ function parseCertificateSigningRequestDerOrThrow(der, options) {
|
|
|
285
285
|
* @param der Raw DER bytes of a PKCS#10 certificate signing request.
|
|
286
286
|
* @param options Custom extension decoders to apply during parsing.
|
|
287
287
|
*/
|
|
288
|
-
function parseCertificateSigningRequestDer(
|
|
288
|
+
function parseCertificateSigningRequestDer(e, t) {
|
|
289
289
|
try {
|
|
290
|
-
return
|
|
291
|
-
} catch (
|
|
292
|
-
|
|
293
|
-
return failureResult("malformed", error instanceof Error ? error.message : "Malformed certificate signing request");
|
|
290
|
+
return _(parseCertificateSigningRequestDerOrThrow(e, t));
|
|
291
|
+
} catch (e) {
|
|
292
|
+
return g(e), h("malformed", e instanceof Error ? e.message : "Malformed certificate signing request");
|
|
294
293
|
}
|
|
295
294
|
}
|
|
296
|
-
function parseCertificatesFromPemBlocks(
|
|
297
|
-
return
|
|
295
|
+
function parseCertificatesFromPemBlocks(e, t) {
|
|
296
|
+
return y(e).filter((e) => e.label === "CERTIFICATE").map((e) => parseCertificateDerOrThrow(e.bytes, t));
|
|
298
297
|
}
|
|
299
|
-
function hasParsedCertificateShape(
|
|
300
|
-
return "subjectPublicKeyInfoDer" in
|
|
298
|
+
function hasParsedCertificateShape(e) {
|
|
299
|
+
return "subjectPublicKeyInfoDer" in e;
|
|
301
300
|
}
|
|
302
301
|
/**
|
|
303
302
|
* Decode a PEM-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
|
|
@@ -305,8 +304,8 @@ function hasParsedCertificateShape(value) {
|
|
|
305
304
|
* @param pem PEM string with a CERTIFICATE REQUEST block.
|
|
306
305
|
* @param options Custom extension decoders to apply during parsing.
|
|
307
306
|
*/
|
|
308
|
-
function parseCertificateSigningRequestPemOrThrow(
|
|
309
|
-
return parseCertificateSigningRequestDerOrThrow(
|
|
307
|
+
function parseCertificateSigningRequestPemOrThrow(e, t) {
|
|
308
|
+
return parseCertificateSigningRequestDerOrThrow(v("CERTIFICATE REQUEST", e), t);
|
|
310
309
|
}
|
|
311
310
|
/**
|
|
312
311
|
* Decode a PEM-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
|
|
@@ -314,12 +313,11 @@ function parseCertificateSigningRequestPemOrThrow(pem, options) {
|
|
|
314
313
|
* @param pem PEM string with a CERTIFICATE REQUEST block.
|
|
315
314
|
* @param options Custom extension decoders to apply during parsing.
|
|
316
315
|
*/
|
|
317
|
-
function parseCertificateSigningRequestPem(
|
|
316
|
+
function parseCertificateSigningRequestPem(e, t) {
|
|
318
317
|
try {
|
|
319
|
-
return
|
|
320
|
-
} catch (
|
|
321
|
-
|
|
322
|
-
return failureResult("malformed", error instanceof Error ? error.message : "Malformed certificate signing request");
|
|
318
|
+
return _(parseCertificateSigningRequestPemOrThrow(e, t));
|
|
319
|
+
} catch (e) {
|
|
320
|
+
return g(e), h("malformed", e instanceof Error ? e.message : "Malformed certificate signing request");
|
|
323
321
|
}
|
|
324
322
|
}
|
|
325
323
|
/**
|
|
@@ -350,16 +348,139 @@ function parseCertificateSigningRequestPem(pem, options) {
|
|
|
350
348
|
*
|
|
351
349
|
* @see {@linkcode getSubjectPublicKey} for the non-throwing variant
|
|
352
350
|
*/
|
|
353
|
-
function getSubjectPublicKeyOrThrow(
|
|
354
|
-
return
|
|
351
|
+
function getSubjectPublicKeyOrThrow(e, t) {
|
|
352
|
+
return C(e.subjectPublicKeyInfoDer, t);
|
|
355
353
|
}
|
|
356
354
|
/**
|
|
357
355
|
* Import the subject public key of a parsed certificate or CSR as a WebCrypto `CryptoKey`.
|
|
358
356
|
*
|
|
359
357
|
* @see `getSubjectPublicKeyOrThrow` for the throwing variant
|
|
360
358
|
*/
|
|
361
|
-
function getSubjectPublicKey(
|
|
362
|
-
return
|
|
359
|
+
function getSubjectPublicKey(e, t) {
|
|
360
|
+
return S(e.subjectPublicKeyInfoDer, t);
|
|
361
|
+
}
|
|
362
|
+
/** Extract the AlgorithmIdentifier TLV from a SubjectPublicKeyInfo DER buffer. */
|
|
363
|
+
function subjectPublicKeyInfoAlgorithmDer(e) {
|
|
364
|
+
let t = f(r(e, { maxDepth: 64 })[0], "SubjectPublicKeyInfo algorithm");
|
|
365
|
+
return e.slice(t.start - t.headerLength, t.end);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Derive the public half of `privateKey`, export it as SPKI DER, and classify
|
|
369
|
+
* how it relates to a certificate's SubjectPublicKeyInfo. Single source of truth
|
|
370
|
+
* for {@linkcode certificateMatchesPrivateKey} and {@linkcode matchCertificatePrivateKey}.
|
|
371
|
+
*
|
|
372
|
+
* Equal SPKI bytes mean the key owns the certificate. When they differ, the
|
|
373
|
+
* AlgorithmIdentifier portions decide whether it is a same-algorithm key
|
|
374
|
+
* mismatch or a cross-algorithm (`key_type_mismatch`) one.
|
|
375
|
+
*/
|
|
376
|
+
async function compareCertificateToPrivateKey(e, t) {
|
|
377
|
+
let n = await x(await b(t));
|
|
378
|
+
return optionalBytesEqual(n, e) ? "match" : optionalBytesEqual(subjectPublicKeyInfoAlgorithmDer(n), subjectPublicKeyInfoAlgorithmDer(e)) ? "key_mismatch" : "key_type_mismatch";
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Check whether a certificate's subject public key belongs to a private key.
|
|
382
|
+
*
|
|
383
|
+
* Confirming that an uploaded private key actually matches the certificate it
|
|
384
|
+
* was submitted with is the first thing a key-intake or issuance endpoint must
|
|
385
|
+
* do. This derives the public half of `privateKey`, exports it as
|
|
386
|
+
* SubjectPublicKeyInfo DER, and compares those bytes against the certificate's
|
|
387
|
+
* own SubjectPublicKeyInfo — the canonical, algorithm-agnostic way to test key
|
|
388
|
+
* ownership. (Comparing JWKs field by field, or signing a probe and verifying
|
|
389
|
+
* it, are both more fragile.)
|
|
390
|
+
*
|
|
391
|
+
* A private key of a different type — e.g. an ECDSA key against an RSA
|
|
392
|
+
* certificate — simply produces different SPKI DER and returns `false`, so
|
|
393
|
+
* callers get a single boolean without branching on the kind of mismatch. Reach
|
|
394
|
+
* for {@linkcode matchCertificatePrivateKey} when you need the reason a match
|
|
395
|
+
* failed (or a typed failure instead of a thrown error) at a trust boundary.
|
|
396
|
+
*
|
|
397
|
+
* The comparison is over the exact DER encoding. A certificate whose
|
|
398
|
+
* SubjectPublicKeyInfo pins RSASSA-PSS parameters (rather than the plain
|
|
399
|
+
* `rsaEncryption` OID that WebCrypto emits) therefore will not match even for
|
|
400
|
+
* the same modulus; such certificates are rare in practice.
|
|
401
|
+
*
|
|
402
|
+
* @param certificate PEM string, DER bytes, or an already-parsed certificate.
|
|
403
|
+
* @param privateKey An extractable private `CryptoKey`.
|
|
404
|
+
* @returns `true` when the private key's public half matches the certificate's
|
|
405
|
+
* subject public key.
|
|
406
|
+
*
|
|
407
|
+
* @throws {Error} If `certificate` is malformed, or `privateKey` is not an
|
|
408
|
+
* extractable private key of a supported type (propagated from
|
|
409
|
+
* {@linkcode derivePublicKey}). Use {@linkcode matchCertificatePrivateKey} for a
|
|
410
|
+
* typed `Result` instead of thrown errors.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* const privateKey = await importPkcs8PemOrThrow(keyPem, { kind: 'ecdsa', curve: 'P-256' });
|
|
415
|
+
* if (!(await certificateMatchesPrivateKey(certificatePem, privateKey))) {
|
|
416
|
+
* throw new Error('uploaded key does not match the certificate');
|
|
417
|
+
* }
|
|
418
|
+
* ```
|
|
419
|
+
*
|
|
420
|
+
* @see {@linkcode matchCertificatePrivateKey} for the typed-`Result` variant with a mismatch reason
|
|
421
|
+
* @see {@linkcode getSubjectPublicKeyOrThrow} to obtain the certificate's public key directly
|
|
422
|
+
* @see {@linkcode derivePublicKey} for the private-to-public bridge this builds on
|
|
423
|
+
*/
|
|
424
|
+
async function certificateMatchesPrivateKey(e, t) {
|
|
425
|
+
return await compareCertificateToPrivateKey(parseCertificateFromSource(e).subjectPublicKeyInfoDer, t) === "match";
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Check whether a certificate's subject public key belongs to a private key,
|
|
429
|
+
* returning a typed {@linkcode MatchCertificatePrivateKeyResult}.
|
|
430
|
+
*
|
|
431
|
+
* The `Result`-returning companion to {@linkcode certificateMatchesPrivateKey}:
|
|
432
|
+
* where the boolean helper answers only "does it match?" (and throws on bad
|
|
433
|
+
* input), this surfaces the expected failures a key-intake or issuance endpoint
|
|
434
|
+
* meets on untrusted input as typed codes rather than exceptions — matching the
|
|
435
|
+
* house rule of returning `Result` for expected failures and throwing only for
|
|
436
|
+
* invariants. `ok: true` means the key owns the certificate; a failure carries
|
|
437
|
+
* one of:
|
|
438
|
+
*
|
|
439
|
+
* - `malformed_certificate` — `certificate` could not be parsed.
|
|
440
|
+
* - `unsupported_private_key` — `privateKey` is not an extractable private key
|
|
441
|
+
* of a supported type (from {@linkcode derivePublicKey}).
|
|
442
|
+
* - `key_type_mismatch` — the key is a different algorithm than the
|
|
443
|
+
* certificate's subject public key.
|
|
444
|
+
* - `key_mismatch` — the key is the right algorithm but a different key.
|
|
445
|
+
*
|
|
446
|
+
* As with {@linkcode certificateMatchesPrivateKey}, the comparison is over exact
|
|
447
|
+
* SPKI DER, so a certificate pinning RSASSA-PSS parameters reports
|
|
448
|
+
* `key_type_mismatch` against the `rsaEncryption` SPKI WebCrypto emits.
|
|
449
|
+
*
|
|
450
|
+
* @param certificate PEM string, DER bytes, or an already-parsed certificate.
|
|
451
|
+
* @param privateKey An extractable private `CryptoKey`.
|
|
452
|
+
* @returns A success when the key matches, or a typed failure otherwise.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```ts
|
|
456
|
+
* const result = await matchCertificatePrivateKey(certificatePem, privateKey);
|
|
457
|
+
* if (!result.ok) {
|
|
458
|
+
* // result.code is 'malformed_certificate' | 'unsupported_private_key'
|
|
459
|
+
* // | 'key_type_mismatch' | 'key_mismatch'
|
|
460
|
+
* throw new Error(`key does not match certificate: ${result.code}`);
|
|
461
|
+
* }
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @see {@linkcode certificateMatchesPrivateKey} for the plain-boolean variant
|
|
465
|
+
*/
|
|
466
|
+
async function matchCertificatePrivateKey(e, t) {
|
|
467
|
+
let n;
|
|
468
|
+
try {
|
|
469
|
+
n = parseCertificateFromSource(e);
|
|
470
|
+
} catch (e) {
|
|
471
|
+
return g(e), h("malformed_certificate", e instanceof Error ? e.message : "Malformed certificate");
|
|
472
|
+
}
|
|
473
|
+
let r;
|
|
474
|
+
try {
|
|
475
|
+
r = await compareCertificateToPrivateKey(n.subjectPublicKeyInfoDer, t);
|
|
476
|
+
} catch (e) {
|
|
477
|
+
return g(e), h("unsupported_private_key", e instanceof Error ? e.message : "Unsupported private key");
|
|
478
|
+
}
|
|
479
|
+
switch (r) {
|
|
480
|
+
case "match": return _(void 0);
|
|
481
|
+
case "key_mismatch": return h("key_mismatch", "Private key does not match the certificate's subject public key");
|
|
482
|
+
case "key_type_mismatch": return h("key_type_mismatch", "Private key algorithm does not match the certificate's subject public key algorithm");
|
|
483
|
+
}
|
|
363
484
|
}
|
|
364
485
|
/**
|
|
365
486
|
* Find a raw extension by OID within a parsed extension list.
|
|
@@ -368,8 +489,8 @@ function getSubjectPublicKey(parsed, algorithm) {
|
|
|
368
489
|
* @param oid Dotted-decimal OID to look up.
|
|
369
490
|
* @returns The matching extension, or `undefined` if not present.
|
|
370
491
|
*/
|
|
371
|
-
function findExtension(
|
|
372
|
-
return
|
|
492
|
+
function findExtension(e, t) {
|
|
493
|
+
return e.find((e) => e.oid === t);
|
|
373
494
|
}
|
|
374
495
|
/**
|
|
375
496
|
* Decode a single extension using a custom {@linkcode ExtensionDecoder}.
|
|
@@ -378,10 +499,9 @@ function findExtension(extensions, oid) {
|
|
|
378
499
|
* @param decoder Decoder whose OID will be matched.
|
|
379
500
|
* @returns The decoded value, or `undefined` if the extension is absent.
|
|
380
501
|
*/
|
|
381
|
-
function decodeExtension(
|
|
382
|
-
|
|
383
|
-
if (
|
|
384
|
-
return decoder.decode(extension);
|
|
502
|
+
function decodeExtension(e, t) {
|
|
503
|
+
let n = findExtension(e, t.oid);
|
|
504
|
+
if (n !== void 0) return t.decode(n);
|
|
385
505
|
}
|
|
386
506
|
/**
|
|
387
507
|
* Decode all matching extensions using an array of {@linkcode ExtensionDecoder}s.
|
|
@@ -389,18 +509,17 @@ function decodeExtension(extensions, decoder) {
|
|
|
389
509
|
* @param extensions Extension list to search.
|
|
390
510
|
* @param decoders Decoders to apply. Only matching OIDs produce output.
|
|
391
511
|
*/
|
|
392
|
-
function decodeExtensions(
|
|
393
|
-
|
|
394
|
-
for (
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
value: decoder.decode(extension)
|
|
512
|
+
function decodeExtensions(e, t) {
|
|
513
|
+
let n = [];
|
|
514
|
+
for (let r of t) {
|
|
515
|
+
let t = findExtension(e, r.oid);
|
|
516
|
+
t !== void 0 && n.push({
|
|
517
|
+
oid: t.oid,
|
|
518
|
+
critical: t.critical,
|
|
519
|
+
value: r.decode(t)
|
|
401
520
|
});
|
|
402
521
|
}
|
|
403
|
-
return
|
|
522
|
+
return n;
|
|
404
523
|
}
|
|
405
524
|
/**
|
|
406
525
|
* Decode all matching extensions using a named {@linkcode ExtensionDecoderMap}.
|
|
@@ -408,488 +527,446 @@ function decodeExtensions(extensions, decoders) {
|
|
|
408
527
|
* @param extensions Extension list to search.
|
|
409
528
|
* @param decoderMap Named decoders. Results are keyed by the same map keys.
|
|
410
529
|
*/
|
|
411
|
-
function decodeExtensionMap(
|
|
412
|
-
let
|
|
413
|
-
for (
|
|
414
|
-
|
|
415
|
-
if (
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
value: decoder.decode(extension)
|
|
530
|
+
function decodeExtensionMap(e, t) {
|
|
531
|
+
let n = {};
|
|
532
|
+
for (let r in t) {
|
|
533
|
+
let i = t[r];
|
|
534
|
+
if (i === void 0) continue;
|
|
535
|
+
let a = findExtension(e, i.oid);
|
|
536
|
+
a !== void 0 && (n = {
|
|
537
|
+
...n,
|
|
538
|
+
[r]: {
|
|
539
|
+
oid: a.oid,
|
|
540
|
+
critical: a.critical,
|
|
541
|
+
value: i.decode(a)
|
|
424
542
|
}
|
|
425
|
-
};
|
|
543
|
+
});
|
|
426
544
|
}
|
|
427
|
-
return
|
|
545
|
+
return n;
|
|
428
546
|
}
|
|
429
547
|
/** Decode the explicit [3] extensions wrapper from a TBSCertificate. */
|
|
430
|
-
function parseExtensionContainer(
|
|
431
|
-
|
|
432
|
-
return parseExtensionSequence(source, requireElement(childrenOf(source, container)[0], "extensions sequence"), "certificate");
|
|
548
|
+
function parseExtensionContainer(e, t) {
|
|
549
|
+
return t === void 0 ? { all: [] } : parseExtensionSequence(e, f(i(e, t)[0], "extensions sequence"), "certificate");
|
|
433
550
|
}
|
|
434
551
|
/** Extract extensions from the CSR extensionRequest attribute. */
|
|
435
|
-
function parseRequestedExtensions(
|
|
436
|
-
if (
|
|
437
|
-
if (
|
|
438
|
-
let
|
|
439
|
-
for (
|
|
440
|
-
|
|
441
|
-
if (
|
|
442
|
-
if (
|
|
443
|
-
if (
|
|
444
|
-
|
|
445
|
-
if (
|
|
446
|
-
|
|
447
|
-
if (
|
|
448
|
-
|
|
552
|
+
function parseRequestedExtensions(e, t) {
|
|
553
|
+
if (t === void 0) return { all: [] };
|
|
554
|
+
if (t.tag !== 160) throw Error("CertificationRequestInfo attributes must use [0]");
|
|
555
|
+
let n;
|
|
556
|
+
for (let r of i(e, t)) {
|
|
557
|
+
let t = i(e, r);
|
|
558
|
+
if (t.length !== 2) throw Error("Malformed CSR attribute");
|
|
559
|
+
if (c(f(t[0], "attribute OID").value) !== m.extensionRequest) continue;
|
|
560
|
+
if (n !== void 0) throw Error("extensionRequest attribute must not repeat");
|
|
561
|
+
let a = f(t[1], "attribute values");
|
|
562
|
+
if (a.tag !== 49) throw Error("extensionRequest attribute values must use SET");
|
|
563
|
+
let o = i(e, a);
|
|
564
|
+
if (o.length !== 1) throw Error("extensionRequest attribute must contain exactly one value");
|
|
565
|
+
n = parseExtensionSequence(e, f(o[0], "requested extensions"), "csr");
|
|
449
566
|
}
|
|
450
|
-
return
|
|
567
|
+
return n ?? { all: [] };
|
|
451
568
|
}
|
|
452
569
|
/** Walk a SEQUENCE OF Extension and decode each one, populating known-extension slots. */
|
|
453
|
-
function parseExtensionSequence(
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
if (
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
let
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
critical
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
if (extnValue.tag !== 4 || offset !== children.length - 1) throw new Error("Extension value must use OCTET STRING");
|
|
472
|
-
parsed.push({
|
|
473
|
-
oid,
|
|
474
|
-
critical,
|
|
475
|
-
valueDer: new Uint8Array(extnValue.value),
|
|
476
|
-
valueHex: toHex(extnValue.value)
|
|
477
|
-
});
|
|
478
|
-
decodeAndApplyKnownExtension(context, oid, knownParsed, extnValue.value);
|
|
570
|
+
function parseExtensionSequence(e, t, n) {
|
|
571
|
+
let r = [], o = {}, s = /* @__PURE__ */ new Set();
|
|
572
|
+
for (let l of i(e, t)) {
|
|
573
|
+
let t = i(e, l);
|
|
574
|
+
if (t.length < 2 || t.length > 3) throw Error("Malformed Extension");
|
|
575
|
+
let u = c(f(t[0], "extension OID").value);
|
|
576
|
+
if (s.has(u)) throw Error(`Duplicate extension OID: ${u}`);
|
|
577
|
+
s.add(u);
|
|
578
|
+
let d = 1, m = !1, h = t[d];
|
|
579
|
+
h?.tag === 1 && (m = a(h.value), d += 1);
|
|
580
|
+
let g = f(t[d], "extension value");
|
|
581
|
+
if (g.tag !== 4 || d !== t.length - 1) throw Error("Extension value must use OCTET STRING");
|
|
582
|
+
r.push({
|
|
583
|
+
oid: u,
|
|
584
|
+
critical: m,
|
|
585
|
+
valueDer: new Uint8Array(g.value),
|
|
586
|
+
valueHex: p(g.value)
|
|
587
|
+
}), M(n, u, o, g.value);
|
|
479
588
|
}
|
|
480
589
|
return {
|
|
481
|
-
all:
|
|
482
|
-
...
|
|
590
|
+
all: r,
|
|
591
|
+
...o
|
|
483
592
|
};
|
|
484
593
|
}
|
|
485
594
|
/** Decode an X.501 Name (issuer / subject) into a {@linkcode ParsedName}. */
|
|
486
|
-
function parseName(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
rdns.push(rdn);
|
|
493
|
-
for (const attribute of rdn.attributes) {
|
|
494
|
-
attributes.push(attribute);
|
|
495
|
-
if (attribute.key !== void 0 && values[attribute.key] === void 0) values[attribute.key] = attribute.value;
|
|
496
|
-
}
|
|
595
|
+
function parseName(e, t) {
|
|
596
|
+
let n = [], r = [], a = {};
|
|
597
|
+
for (let o of i(e, t)) {
|
|
598
|
+
let t = parseNameAttributeSet(e, o);
|
|
599
|
+
n.push(t);
|
|
600
|
+
for (let e of t.attributes) r.push(e), e.key !== void 0 && a[e.key] === void 0 && (a[e.key] = e.value);
|
|
497
601
|
}
|
|
498
602
|
return {
|
|
499
|
-
derHex:
|
|
500
|
-
rdns,
|
|
501
|
-
attributes,
|
|
502
|
-
values
|
|
603
|
+
derHex: p(e.slice(t.start - t.headerLength, t.end)),
|
|
604
|
+
rdns: n,
|
|
605
|
+
attributes: r,
|
|
606
|
+
values: a
|
|
503
607
|
};
|
|
504
608
|
}
|
|
505
609
|
/** Decode a single RDN SET element. */
|
|
506
|
-
function parseRelativeDistinguishedName(
|
|
507
|
-
return parseNameAttributeSet(
|
|
610
|
+
function parseRelativeDistinguishedName(e, t) {
|
|
611
|
+
return parseNameAttributeSet(e, t);
|
|
508
612
|
}
|
|
509
613
|
/** Decode the AttributeTypeAndValue pairs within a SET element. */
|
|
510
|
-
function parseNameAttributeSet(
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
const fieldKey = nameFieldKeyFromOid(oid);
|
|
518
|
-
const fieldValue = decodeString(valueElement.tag, valueElement.value);
|
|
519
|
-
const attribute = fieldKey !== void 0 ? {
|
|
520
|
-
oid,
|
|
521
|
-
key: fieldKey,
|
|
522
|
-
valueTag: valueElement.tag,
|
|
523
|
-
value: fieldValue
|
|
614
|
+
function parseNameAttributeSet(e, t) {
|
|
615
|
+
let n = [], r = {};
|
|
616
|
+
for (let a of i(e, t)) {
|
|
617
|
+
let t = i(e, a), o = c(f(t[0], "name OID").value), s = f(t[1], "name value"), u = k(o), d = l(s.tag, s.value), p = u === void 0 ? {
|
|
618
|
+
oid: o,
|
|
619
|
+
valueTag: s.tag,
|
|
620
|
+
value: d
|
|
524
621
|
} : {
|
|
525
|
-
oid,
|
|
526
|
-
|
|
527
|
-
|
|
622
|
+
oid: o,
|
|
623
|
+
key: u,
|
|
624
|
+
valueTag: s.tag,
|
|
625
|
+
value: d
|
|
528
626
|
};
|
|
529
|
-
|
|
530
|
-
if (fieldKey !== void 0 && values[fieldKey] === void 0) values[fieldKey] = fieldValue;
|
|
627
|
+
n.push(p), u !== void 0 && r[u] === void 0 && (r[u] = d);
|
|
531
628
|
}
|
|
532
629
|
return {
|
|
533
|
-
derHex:
|
|
534
|
-
attributes,
|
|
535
|
-
values
|
|
630
|
+
derHex: p(e.slice(t.start - t.headerLength, t.end)),
|
|
631
|
+
attributes: n,
|
|
632
|
+
values: r
|
|
536
633
|
};
|
|
537
634
|
}
|
|
538
635
|
/** Decode the Validity SEQUENCE into notBefore / notAfter Dates. */
|
|
539
|
-
function parseValidity(
|
|
540
|
-
|
|
636
|
+
function parseValidity(e, t) {
|
|
637
|
+
let n = i(e, t);
|
|
541
638
|
return {
|
|
542
|
-
notBefore:
|
|
543
|
-
notAfter:
|
|
639
|
+
notBefore: d(f(n[0], "notBefore")),
|
|
640
|
+
notAfter: d(f(n[1], "notAfter"))
|
|
544
641
|
};
|
|
545
642
|
}
|
|
546
643
|
/** Extract the algorithm identifier from a SubjectPublicKeyInfo SEQUENCE. */
|
|
547
|
-
function parseSubjectPublicKeyInfo(
|
|
548
|
-
if (
|
|
549
|
-
|
|
550
|
-
if (
|
|
551
|
-
|
|
552
|
-
if (
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
return algorithm;
|
|
644
|
+
function parseSubjectPublicKeyInfo(e, t) {
|
|
645
|
+
if (t.tag !== 48) throw Error("SubjectPublicKeyInfo must use SEQUENCE");
|
|
646
|
+
let n = i(e, t);
|
|
647
|
+
if (n.length !== 2) throw Error("SubjectPublicKeyInfo must contain algorithm and subjectPublicKey");
|
|
648
|
+
let r = f(n[0], "SPKI algorithm");
|
|
649
|
+
if (r.tag !== 48) throw Error("SubjectPublicKeyInfo algorithm must use SEQUENCE");
|
|
650
|
+
let a = parseAlgorithmIdentifier(e, r);
|
|
651
|
+
return u(f(n[1], "subjectPublicKey BIT STRING")), a;
|
|
556
652
|
}
|
|
557
653
|
/** Decode an AlgorithmIdentifier SEQUENCE (OID + optional parameters). */
|
|
558
|
-
function parseAlgorithmIdentifier(
|
|
559
|
-
|
|
560
|
-
if (
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
oid,
|
|
572
|
-
parametersDer
|
|
654
|
+
function parseAlgorithmIdentifier(e, t) {
|
|
655
|
+
let n = i(e, t);
|
|
656
|
+
if (n.length === 0 || n.length > 2) throw Error("Malformed AlgorithmIdentifier");
|
|
657
|
+
let r = c(f(n[0], "algorithm OID").value), a = n[1];
|
|
658
|
+
if (a === void 0) return { oid: r };
|
|
659
|
+
let o = e.slice(a.start - a.headerLength, a.end);
|
|
660
|
+
return a?.tag === 6 ? {
|
|
661
|
+
oid: r,
|
|
662
|
+
parametersDer: o,
|
|
663
|
+
parametersOid: c(a.value)
|
|
664
|
+
} : {
|
|
665
|
+
oid: r,
|
|
666
|
+
parametersDer: o
|
|
573
667
|
};
|
|
574
668
|
}
|
|
575
|
-
function assertMatchingCertificateSignatureAlgorithms(
|
|
576
|
-
if (
|
|
669
|
+
function assertMatchingCertificateSignatureAlgorithms(e, t) {
|
|
670
|
+
if (e.oid !== t.oid || !optionalBytesEqual(e.parametersDer, t.parametersDer)) throw Error("Certificate signatureAlgorithm must match TBSCertificate signature");
|
|
577
671
|
}
|
|
578
|
-
function optionalBytesEqual(
|
|
579
|
-
if (
|
|
580
|
-
if (
|
|
581
|
-
for (let
|
|
582
|
-
return
|
|
672
|
+
function optionalBytesEqual(e, t) {
|
|
673
|
+
if (e === void 0 || t === void 0) return e === t;
|
|
674
|
+
if (e.length !== t.length) return !1;
|
|
675
|
+
for (let n = 0; n < e.length; n += 1) if (e[n] !== t[n]) return !1;
|
|
676
|
+
return !0;
|
|
583
677
|
}
|
|
584
678
|
/** @internal Decode the Basic Constraints extension value DER. */
|
|
585
|
-
function parseBasicConstraints(
|
|
586
|
-
|
|
587
|
-
if (
|
|
588
|
-
|
|
589
|
-
let
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
if (child.tag === 1) {
|
|
595
|
-
if (sawCa) throw new Error("basicConstraints cA must not repeat");
|
|
596
|
-
if (sawPathLength) throw new Error("basicConstraints cA must precede pathLength");
|
|
597
|
-
sawCa = true;
|
|
598
|
-
ca = decodeBoolean(child.value);
|
|
679
|
+
function parseBasicConstraints(e) {
|
|
680
|
+
let t = n(e, { maxDepth: 64 });
|
|
681
|
+
if (t.tag !== 48) throw Error("basicConstraints must use SEQUENCE");
|
|
682
|
+
let r = i(e, t), o = !1, c, l = !1, u = !1;
|
|
683
|
+
for (let e of r) {
|
|
684
|
+
if (e.tag === 1) {
|
|
685
|
+
if (l) throw Error("basicConstraints cA must not repeat");
|
|
686
|
+
if (u) throw Error("basicConstraints cA must precede pathLength");
|
|
687
|
+
l = !0, o = a(e.value);
|
|
599
688
|
continue;
|
|
600
689
|
}
|
|
601
|
-
if (
|
|
602
|
-
if (
|
|
603
|
-
|
|
604
|
-
pathLength = decodeNonNegativeIntegerNumber(child.value, "basicConstraints pathLength");
|
|
690
|
+
if (e.tag === 2) {
|
|
691
|
+
if (u) throw Error("basicConstraints pathLength must not repeat");
|
|
692
|
+
u = !0, c = s(e.value, "basicConstraints pathLength");
|
|
605
693
|
continue;
|
|
606
694
|
}
|
|
607
|
-
throw
|
|
695
|
+
throw Error(`Unsupported basicConstraints field tag: ${String(e.tag)}`);
|
|
608
696
|
}
|
|
609
|
-
if (
|
|
610
|
-
|
|
611
|
-
ca:
|
|
612
|
-
pathLength
|
|
613
|
-
} : { ca:
|
|
614
|
-
return { ca: false };
|
|
697
|
+
if (c !== void 0 && o !== !0) throw Error("basicConstraints pathLength requires cA = true");
|
|
698
|
+
return o ? c === void 0 ? { ca: !0 } : {
|
|
699
|
+
ca: !0,
|
|
700
|
+
pathLength: c
|
|
701
|
+
} : { ca: !1 };
|
|
615
702
|
}
|
|
616
703
|
/** @internal Decode the Key Usage BIT STRING extension value. */
|
|
617
|
-
function parseKeyUsage(
|
|
618
|
-
return
|
|
704
|
+
function parseKeyUsage(e) {
|
|
705
|
+
return O(e);
|
|
619
706
|
}
|
|
620
707
|
/** @internal Decode the Extended Key Usage SEQUENCE OF OIDs. */
|
|
621
|
-
function parseExtendedKeyUsage(
|
|
622
|
-
return
|
|
708
|
+
function parseExtendedKeyUsage(e) {
|
|
709
|
+
return i(e, f(n(e, { maxDepth: 64 }), "extendedKeyUsage sequence")).map((e) => j(c(e.value)));
|
|
623
710
|
}
|
|
624
711
|
/** @internal Decode the Certificate Policies extension value. */
|
|
625
|
-
function parseCertificatePolicies(
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
return
|
|
712
|
+
function parseCertificatePolicies(e) {
|
|
713
|
+
let t = i(e, f(n(e, { maxDepth: 64 }), "certificatePolicies sequence"));
|
|
714
|
+
if (t.length === 0) throw Error("certificatePolicies must not be empty");
|
|
715
|
+
return t.map((t) => parsePolicyInformation(e, t));
|
|
629
716
|
}
|
|
630
717
|
/** Decode a single PolicyInformation SEQUENCE (OID + optional qualifiers). */
|
|
631
|
-
function parsePolicyInformation(
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
if (
|
|
637
|
-
const qualifiers = childrenOf(source, qualifiersElement);
|
|
638
|
-
if (qualifiers.length === 0) throw new Error("policyQualifiers must not be empty");
|
|
718
|
+
function parsePolicyInformation(e, t) {
|
|
719
|
+
let n = i(e, t), r = c(f(n[0], "policyIdentifier").value), a = n[1];
|
|
720
|
+
if (n.length > 2) throw Error("policyInformation has unexpected trailing fields");
|
|
721
|
+
if (a === void 0) return { policyIdentifier: r };
|
|
722
|
+
let o = i(e, a);
|
|
723
|
+
if (o.length === 0) throw Error("policyQualifiers must not be empty");
|
|
639
724
|
return {
|
|
640
|
-
policyIdentifier,
|
|
641
|
-
policyQualifiers:
|
|
725
|
+
policyIdentifier: r,
|
|
726
|
+
policyQualifiers: o.map((t) => parsePolicyQualifierInfo(e, t))
|
|
642
727
|
};
|
|
643
728
|
}
|
|
644
729
|
/** Decode a PolicyQualifierInfo (CPS URI, UserNotice, or opaque OID). */
|
|
645
|
-
function parsePolicyQualifierInfo(
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
if (qualifierId === OIDS.cpsPolicyQualifier) {
|
|
651
|
-
if (qualifierValue.tag !== 22) throw new Error("cps policy qualifier must use IA5String");
|
|
730
|
+
function parsePolicyQualifierInfo(e, t) {
|
|
731
|
+
let n = i(e, t), r = c(f(n[0], "policyQualifierId").value), a = f(n[1], "policyQualifier");
|
|
732
|
+
if (n.length > 2) throw Error("policyQualifierInfo has unexpected trailing fields");
|
|
733
|
+
if (r === m.cpsPolicyQualifier) {
|
|
734
|
+
if (a.tag !== 22) throw Error("cps policy qualifier must use IA5String");
|
|
652
735
|
return {
|
|
653
736
|
type: "cps",
|
|
654
|
-
uri:
|
|
737
|
+
uri: l(a.tag, a.value)
|
|
655
738
|
};
|
|
656
739
|
}
|
|
657
|
-
|
|
740
|
+
return r === m.userNoticePolicyQualifier ? {
|
|
658
741
|
type: "userNotice",
|
|
659
|
-
...parseUserNoticePolicyQualifierInfo(
|
|
660
|
-
}
|
|
661
|
-
return {
|
|
742
|
+
...parseUserNoticePolicyQualifierInfo(e, a)
|
|
743
|
+
} : {
|
|
662
744
|
type: "oid",
|
|
663
|
-
oid:
|
|
664
|
-
qualifierDer:
|
|
745
|
+
oid: r,
|
|
746
|
+
qualifierDer: e.slice(a.start - a.headerLength, a.end)
|
|
665
747
|
};
|
|
666
748
|
}
|
|
667
749
|
/** Decode a UserNotice qualifier (optional noticeRef + optional explicitText). */
|
|
668
|
-
function parseUserNoticePolicyQualifierInfo(
|
|
669
|
-
|
|
670
|
-
let
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
if (noticeRef !== void 0) throw new Error("userNotice must not contain multiple noticeRef values");
|
|
675
|
-
noticeRef = parsePolicyNoticeReference(source, child);
|
|
750
|
+
function parseUserNoticePolicyQualifierInfo(e, t) {
|
|
751
|
+
let n = i(e, t), r, a;
|
|
752
|
+
for (let t of n) {
|
|
753
|
+
if (t.tag === 48) {
|
|
754
|
+
if (r !== void 0) throw Error("userNotice must not contain multiple noticeRef values");
|
|
755
|
+
r = parsePolicyNoticeReference(e, t);
|
|
676
756
|
continue;
|
|
677
757
|
}
|
|
678
|
-
if (
|
|
679
|
-
|
|
758
|
+
if (a !== void 0) throw Error("userNotice must not contain multiple explicitText values");
|
|
759
|
+
a = parseDisplayText(t);
|
|
680
760
|
}
|
|
681
761
|
return {
|
|
682
|
-
...
|
|
683
|
-
...
|
|
762
|
+
...r === void 0 ? {} : { noticeRef: r },
|
|
763
|
+
...a === void 0 ? {} : { explicitText: a }
|
|
684
764
|
};
|
|
685
765
|
}
|
|
686
766
|
/** Decode a NoticeReference (organization name + notice number list). */
|
|
687
|
-
function parsePolicyNoticeReference(
|
|
688
|
-
if (
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
const noticeNumbersElement = requireElement(children[1], "noticeRef noticeNumbers");
|
|
692
|
-
if (children.length > 2) throw new Error("noticeRef has unexpected trailing fields");
|
|
767
|
+
function parsePolicyNoticeReference(e, t) {
|
|
768
|
+
if (t.tag !== 48) throw Error("noticeRef must use SEQUENCE");
|
|
769
|
+
let n = i(e, t), r = parseDisplayText(f(n[0], "noticeRef organization")), a = f(n[1], "noticeRef noticeNumbers");
|
|
770
|
+
if (n.length > 2) throw Error("noticeRef has unexpected trailing fields");
|
|
693
771
|
return {
|
|
694
|
-
organization,
|
|
695
|
-
noticeNumbers: parsePolicyNoticeNumbers(
|
|
772
|
+
organization: r,
|
|
773
|
+
noticeNumbers: parsePolicyNoticeNumbers(e, a)
|
|
696
774
|
};
|
|
697
775
|
}
|
|
698
776
|
/** Decode a SEQUENCE OF INTEGER notice numbers. */
|
|
699
|
-
function parsePolicyNoticeNumbers(
|
|
700
|
-
if (
|
|
701
|
-
|
|
702
|
-
if (
|
|
703
|
-
return
|
|
704
|
-
if (
|
|
705
|
-
return
|
|
777
|
+
function parsePolicyNoticeNumbers(e, t) {
|
|
778
|
+
if (t.tag !== 48) throw Error("noticeRef noticeNumbers must use SEQUENCE");
|
|
779
|
+
let n = i(e, t);
|
|
780
|
+
if (n.length === 0) throw Error("noticeRef noticeNumbers must not be empty");
|
|
781
|
+
return n.map((e) => {
|
|
782
|
+
if (e.tag !== 2) throw Error("noticeRef noticeNumber must use INTEGER");
|
|
783
|
+
return s(e.value, "noticeRef noticeNumber");
|
|
706
784
|
});
|
|
707
785
|
}
|
|
708
786
|
/** @internal Decode the Policy Mappings extension value. */
|
|
709
|
-
function parsePolicyMappings(
|
|
710
|
-
|
|
711
|
-
if (
|
|
712
|
-
return
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
if (children.length > 2) throw new Error("policyMappings entry has unexpected trailing fields");
|
|
717
|
-
if (issuerDomainPolicy === OIDS.anyPolicy || subjectDomainPolicy === OIDS.anyPolicy) throw new Error("policyMappings must not use anyPolicy");
|
|
787
|
+
function parsePolicyMappings(e) {
|
|
788
|
+
let t = i(e, f(n(e, { maxDepth: 64 }), "policyMappings sequence"));
|
|
789
|
+
if (t.length === 0) throw Error("policyMappings must not be empty");
|
|
790
|
+
return t.map((t) => {
|
|
791
|
+
let n = i(e, t), r = c(f(n[0], "policyMappings issuerDomainPolicy").value), a = c(f(n[1], "policyMappings subjectDomainPolicy").value);
|
|
792
|
+
if (n.length > 2) throw Error("policyMappings entry has unexpected trailing fields");
|
|
793
|
+
if (r === m.anyPolicy || a === m.anyPolicy) throw Error("policyMappings must not use anyPolicy");
|
|
718
794
|
return {
|
|
719
|
-
issuerDomainPolicy,
|
|
720
|
-
subjectDomainPolicy
|
|
795
|
+
issuerDomainPolicy: r,
|
|
796
|
+
subjectDomainPolicy: a
|
|
721
797
|
};
|
|
722
798
|
});
|
|
723
799
|
}
|
|
724
800
|
/** @internal Decode the Policy Constraints extension value. */
|
|
725
|
-
function parsePolicyConstraints(
|
|
726
|
-
|
|
727
|
-
let
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
if (requireExplicitPolicy !== void 0) throw new Error("policyConstraints must not repeat requireExplicitPolicy");
|
|
732
|
-
requireExplicitPolicy = decodeNonNegativeIntegerNumber(child.value, "policyConstraints requireExplicitPolicy");
|
|
801
|
+
function parsePolicyConstraints(e) {
|
|
802
|
+
let t = f(n(e, { maxDepth: 64 }), "policyConstraints sequence"), r, a;
|
|
803
|
+
for (let n of i(e, t)) {
|
|
804
|
+
if (n.tag === 128) {
|
|
805
|
+
if (r !== void 0) throw Error("policyConstraints must not repeat requireExplicitPolicy");
|
|
806
|
+
r = s(n.value, "policyConstraints requireExplicitPolicy");
|
|
733
807
|
continue;
|
|
734
808
|
}
|
|
735
|
-
if (
|
|
736
|
-
if (
|
|
737
|
-
|
|
809
|
+
if (n.tag === 129) {
|
|
810
|
+
if (a !== void 0) throw Error("policyConstraints must not repeat inhibitPolicyMapping");
|
|
811
|
+
a = s(n.value, "policyConstraints inhibitPolicyMapping");
|
|
738
812
|
continue;
|
|
739
813
|
}
|
|
740
|
-
throw
|
|
814
|
+
throw Error(`Unsupported policyConstraints field tag: ${n.tag}`);
|
|
741
815
|
}
|
|
742
|
-
if (
|
|
816
|
+
if (r === void 0 && a === void 0) throw Error("policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping");
|
|
743
817
|
return {
|
|
744
|
-
...
|
|
745
|
-
...
|
|
818
|
+
...r === void 0 ? {} : { requireExplicitPolicy: r },
|
|
819
|
+
...a === void 0 ? {} : { inhibitPolicyMapping: a }
|
|
746
820
|
};
|
|
747
821
|
}
|
|
748
822
|
/** @internal Decode the Inhibit anyPolicy extension (single INTEGER). */
|
|
749
|
-
function parseInhibitAnyPolicy(
|
|
750
|
-
|
|
751
|
-
if (
|
|
752
|
-
return { skipCerts:
|
|
823
|
+
function parseInhibitAnyPolicy(e) {
|
|
824
|
+
let t = f(n(e, { maxDepth: 64 }), "inhibitAnyPolicy integer");
|
|
825
|
+
if (t.tag !== 2) throw Error("inhibitAnyPolicy must be an INTEGER");
|
|
826
|
+
return { skipCerts: s(t.value, "inhibitAnyPolicy skipCerts") };
|
|
753
827
|
}
|
|
754
828
|
/** @internal Decode the Subject Alternative Names SEQUENCE OF GeneralName. */
|
|
755
|
-
function parseSubjectAltNames(
|
|
756
|
-
return
|
|
829
|
+
function parseSubjectAltNames(e) {
|
|
830
|
+
return i(e, f(n(e, { maxDepth: 64 }), "subjectAltName sequence")).map((t) => parseGeneralName(e, t));
|
|
757
831
|
}
|
|
758
832
|
/** @internal Decode the Authority Information Access extension value. */
|
|
759
|
-
function parseAuthorityInfoAccess(
|
|
760
|
-
|
|
761
|
-
if (
|
|
762
|
-
|
|
763
|
-
if (
|
|
764
|
-
return
|
|
765
|
-
if (
|
|
766
|
-
|
|
767
|
-
if (
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
if (
|
|
771
|
-
if (location.tag !== 134) throw new Error(`Unsupported authorityInfoAccess location tag: ${location.tag}`);
|
|
833
|
+
function parseAuthorityInfoAccess(e) {
|
|
834
|
+
let t = f(n(e, { maxDepth: 64 }), "authorityInfoAccess sequence");
|
|
835
|
+
if (t.tag !== 48) throw Error("authorityInfoAccess must use SEQUENCE");
|
|
836
|
+
let r = i(e, t);
|
|
837
|
+
if (r.length === 0) throw Error("authorityInfoAccess must not be empty");
|
|
838
|
+
return r.map((t) => {
|
|
839
|
+
if (t.tag !== 48) throw Error("authorityInfoAccess entry must use SEQUENCE");
|
|
840
|
+
let n = i(e, t);
|
|
841
|
+
if (n.length !== 2) throw Error("authorityInfoAccess entry must contain method and location only");
|
|
842
|
+
let r = f(n[0], "authorityInfoAccess method"), a = f(n[1], "authorityInfoAccess location");
|
|
843
|
+
if (r.tag !== 6) throw Error("authorityInfoAccess method must use OBJECT IDENTIFIER");
|
|
844
|
+
if (a.tag !== 134) throw Error(`Unsupported authorityInfoAccess location tag: ${a.tag}`);
|
|
772
845
|
return {
|
|
773
|
-
method:
|
|
774
|
-
uri:
|
|
846
|
+
method: A(c(r.value)),
|
|
847
|
+
uri: N.decode(a.value)
|
|
775
848
|
};
|
|
776
849
|
});
|
|
777
850
|
}
|
|
778
851
|
/** @internal Decode the CRL Distribution Points extension value. */
|
|
779
|
-
function parseCrlDistributionPoints(
|
|
780
|
-
|
|
781
|
-
if (
|
|
782
|
-
|
|
783
|
-
if (
|
|
784
|
-
|
|
785
|
-
for (
|
|
786
|
-
return
|
|
852
|
+
function parseCrlDistributionPoints(e) {
|
|
853
|
+
let t = f(n(e, { maxDepth: 64 }), "CRLDistributionPoints sequence");
|
|
854
|
+
if (t.tag !== 48) throw Error("CRLDistributionPoints must use SEQUENCE");
|
|
855
|
+
let r = i(e, t);
|
|
856
|
+
if (r.length === 0) throw Error("CRLDistributionPoints must not be empty");
|
|
857
|
+
let a = [];
|
|
858
|
+
for (let t of r) a.push(parseDistributionPoint(e, t));
|
|
859
|
+
return a;
|
|
787
860
|
}
|
|
788
861
|
/** Decode a single DistributionPoint SEQUENCE. */
|
|
789
|
-
function parseDistributionPoint(
|
|
790
|
-
if (
|
|
791
|
-
let
|
|
792
|
-
let
|
|
793
|
-
|
|
794
|
-
for (const child of childrenOf(source, element)) if (child.tag === 160) {
|
|
795
|
-
if (distributionPoint !== void 0) throw new Error("DistributionPoint distributionPoint must not repeat");
|
|
796
|
-
distributionPoint = parseDistributionPointName(source, child);
|
|
797
|
-
} else if (child.tag === 129) {
|
|
798
|
-
if (reasons !== void 0) throw new Error("DistributionPoint reasons must not repeat");
|
|
799
|
-
reasons = parseDistributionPointReasonFlagsContent(child.value);
|
|
800
|
-
} else if (child.tag === 162) {
|
|
801
|
-
if (crlIssuer !== void 0) throw new Error("DistributionPoint crlIssuer must not repeat");
|
|
802
|
-
crlIssuer = parseGeneralNames(source, child);
|
|
803
|
-
} else throw new Error(`Unsupported DistributionPoint field tag: ${String(child.tag)}`);
|
|
804
|
-
if (distributionPoint === void 0 && crlIssuer === void 0) throw new Error("DistributionPoint must include distributionPoint or crlIssuer");
|
|
862
|
+
function parseDistributionPoint(e, t) {
|
|
863
|
+
if (t.tag !== 48) throw Error("DistributionPoint must use SEQUENCE");
|
|
864
|
+
let n = {};
|
|
865
|
+
for (let r of i(e, t)) parseDistributionPointField(e, r, n);
|
|
866
|
+
if (n.distributionPoint === void 0 && n.crlIssuer === void 0) throw Error("DistributionPoint must include distributionPoint or crlIssuer");
|
|
805
867
|
return {
|
|
806
|
-
...distributionPoint === void 0 ? {} : { distributionPoint },
|
|
807
|
-
...reasons === void 0 ? {} : { reasons },
|
|
808
|
-
...crlIssuer === void 0 ? {} : { crlIssuer }
|
|
868
|
+
...n.distributionPoint === void 0 ? {} : { distributionPoint: n.distributionPoint },
|
|
869
|
+
...n.reasons === void 0 ? {} : { reasons: n.reasons },
|
|
870
|
+
...n.crlIssuer === void 0 ? {} : { crlIssuer: n.crlIssuer }
|
|
809
871
|
};
|
|
810
872
|
}
|
|
873
|
+
function parseDistributionPointField(e, t, n) {
|
|
874
|
+
switch (t.tag) {
|
|
875
|
+
case 160:
|
|
876
|
+
if (n.distributionPoint !== void 0) throw Error("DistributionPoint distributionPoint must not repeat");
|
|
877
|
+
n.distributionPoint = parseDistributionPointName(e, t);
|
|
878
|
+
return;
|
|
879
|
+
case 129:
|
|
880
|
+
if (n.reasons !== void 0) throw Error("DistributionPoint reasons must not repeat");
|
|
881
|
+
n.reasons = D(t.value);
|
|
882
|
+
return;
|
|
883
|
+
case 162:
|
|
884
|
+
if (n.crlIssuer !== void 0) throw Error("DistributionPoint crlIssuer must not repeat");
|
|
885
|
+
n.crlIssuer = parseGeneralNames(e, t);
|
|
886
|
+
return;
|
|
887
|
+
default: throw Error(`Unsupported DistributionPoint field tag: ${String(t.tag)}`);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
811
890
|
/** Decode a DistributionPointName (fullName or relativeName). */
|
|
812
|
-
function parseDistributionPointName(
|
|
813
|
-
|
|
814
|
-
if (
|
|
815
|
-
|
|
816
|
-
if (
|
|
817
|
-
|
|
818
|
-
if (
|
|
819
|
-
for (
|
|
820
|
-
return { fullName:
|
|
891
|
+
function parseDistributionPointName(e, t) {
|
|
892
|
+
let n = i(e, t);
|
|
893
|
+
if (n.length !== 1) throw Error("distributionPointName must contain exactly one choice");
|
|
894
|
+
let r = f(n[0], "distributionPointName");
|
|
895
|
+
if (r.tag === 160) {
|
|
896
|
+
let t = i(e, r);
|
|
897
|
+
if (t.length === 0) throw Error("distributionPointName fullName must not be empty");
|
|
898
|
+
for (let e of t) if ((e.tag & 192) != 128) throw Error("distributionPointName fullName must contain GeneralName entries");
|
|
899
|
+
return { fullName: t.map((t) => parseGeneralName(e, t)) };
|
|
821
900
|
}
|
|
822
|
-
if (
|
|
823
|
-
throw
|
|
901
|
+
if (r.tag === 161) return { relativeName: parseRelativeDistinguishedName(e, r) };
|
|
902
|
+
throw Error(`Unsupported distributionPointName tag: ${r.tag}`);
|
|
824
903
|
}
|
|
825
904
|
/** Decode a SEQUENCE OF GeneralName. */
|
|
826
|
-
function parseGeneralNames(
|
|
827
|
-
|
|
828
|
-
if (
|
|
829
|
-
for (
|
|
830
|
-
return
|
|
905
|
+
function parseGeneralNames(e, t) {
|
|
906
|
+
let n = i(e, t);
|
|
907
|
+
if (n.length === 0) throw Error("GeneralNames must not be empty");
|
|
908
|
+
for (let e of n) if ((e.tag & 192) != 128) throw Error("GeneralNames must contain GeneralName entries");
|
|
909
|
+
return n.map((t) => parseGeneralName(e, t));
|
|
831
910
|
}
|
|
832
911
|
/** Decode a single GeneralName from its implicit context tag. */
|
|
833
|
-
function parseGeneralName(
|
|
834
|
-
switch (
|
|
912
|
+
function parseGeneralName(e, t) {
|
|
913
|
+
switch (t.tag) {
|
|
835
914
|
case 160: {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
return {
|
|
915
|
+
let n = parseOtherName(e, t);
|
|
916
|
+
return n === void 0 ? {
|
|
839
917
|
type: "unknown",
|
|
840
|
-
tag:
|
|
841
|
-
value:
|
|
842
|
-
};
|
|
918
|
+
tag: t.tag,
|
|
919
|
+
value: e.slice(t.start, t.end)
|
|
920
|
+
} : n;
|
|
843
921
|
}
|
|
844
922
|
case 129: return {
|
|
845
923
|
type: "email",
|
|
846
|
-
value:
|
|
924
|
+
value: N.decode(t.value)
|
|
847
925
|
};
|
|
848
926
|
case 130: return {
|
|
849
927
|
type: "dns",
|
|
850
|
-
value:
|
|
928
|
+
value: N.decode(t.value)
|
|
851
929
|
};
|
|
852
930
|
case 134: return {
|
|
853
931
|
type: "uri",
|
|
854
|
-
value:
|
|
932
|
+
value: N.decode(t.value)
|
|
855
933
|
};
|
|
856
934
|
case 135: return {
|
|
857
935
|
type: "ip",
|
|
858
|
-
value:
|
|
936
|
+
value: E(t.value)
|
|
859
937
|
};
|
|
860
938
|
case 164: return {
|
|
861
939
|
type: "directoryName",
|
|
862
|
-
derHex:
|
|
940
|
+
derHex: p(rebuildDirectoryNameFromImplicit(t, e))
|
|
863
941
|
};
|
|
864
942
|
default: return {
|
|
865
943
|
type: "unknown",
|
|
866
|
-
tag:
|
|
867
|
-
value:
|
|
944
|
+
tag: t.tag,
|
|
945
|
+
value: e.slice(t.start, t.end)
|
|
868
946
|
};
|
|
869
947
|
}
|
|
870
948
|
}
|
|
871
949
|
/** Attempt to decode an otherName [0] as a known type (currently only SRV-ID). */
|
|
872
|
-
function parseOtherName(
|
|
873
|
-
|
|
874
|
-
if (
|
|
875
|
-
|
|
876
|
-
if (
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
if (
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
if (srvNameElement.tag !== 22) throw new Error("SRV-ID otherName value must be an IA5String");
|
|
950
|
+
function parseOtherName(e, t) {
|
|
951
|
+
let n = i(e, t);
|
|
952
|
+
if (n.length !== 1) throw Error("otherName must contain exactly one SEQUENCE");
|
|
953
|
+
let r = i(e, f(n[0], "otherName sequence"));
|
|
954
|
+
if (r.length !== 2) throw Error("otherName must contain exactly type-id and value");
|
|
955
|
+
let a = f(r[0], "otherName type-id"), o = f(r[1], "otherName value");
|
|
956
|
+
if (c(a.value) !== m.idOnDnsSrv) return;
|
|
957
|
+
if (o.tag !== 160) throw Error("SRV-ID otherName value must use explicit [0]");
|
|
958
|
+
let s = i(e, o);
|
|
959
|
+
if (s.length !== 1) throw Error("SRV-ID otherName value must contain exactly one IA5String");
|
|
960
|
+
let u = f(s[0], "SRV-ID IA5String");
|
|
961
|
+
if (u.tag !== 22) throw Error("SRV-ID otherName value must be an IA5String");
|
|
885
962
|
return {
|
|
886
963
|
type: "srv",
|
|
887
|
-
value:
|
|
964
|
+
value: l(u.tag, u.value)
|
|
888
965
|
};
|
|
889
966
|
}
|
|
890
967
|
/** @internal Decode the Name Constraints extension value. */
|
|
891
|
-
function parseNameConstraints(
|
|
892
|
-
|
|
968
|
+
function parseNameConstraints(e) {
|
|
969
|
+
let t = f(n(e, {
|
|
893
970
|
maxDepth: 64,
|
|
894
971
|
allowOpaqueConstructedTags: [
|
|
895
972
|
160,
|
|
@@ -897,96 +974,96 @@ function parseNameConstraints(bytes) {
|
|
|
897
974
|
165
|
|
898
975
|
]
|
|
899
976
|
}), "nameConstraints sequence");
|
|
900
|
-
if (
|
|
901
|
-
let
|
|
902
|
-
let
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
} else throw new Error(`Unsupported nameConstraints field tag: ${String(child.tag)}`);
|
|
977
|
+
if (t.tag !== 48) throw Error("nameConstraints must use SEQUENCE");
|
|
978
|
+
let r, a;
|
|
979
|
+
for (let n of i(e, t)) if (n.tag === 160) {
|
|
980
|
+
if (r !== void 0) throw Error("nameConstraints permittedSubtrees must not repeat");
|
|
981
|
+
r = parseGeneralSubtrees(e, n);
|
|
982
|
+
} else if (n.tag === 161) {
|
|
983
|
+
if (a !== void 0) throw Error("nameConstraints excludedSubtrees must not repeat");
|
|
984
|
+
a = parseGeneralSubtrees(e, n);
|
|
985
|
+
} else throw Error(`Unsupported nameConstraints field tag: ${String(n.tag)}`);
|
|
910
986
|
return {
|
|
911
|
-
...
|
|
912
|
-
...
|
|
987
|
+
...r === void 0 ? {} : { permittedSubtrees: r },
|
|
988
|
+
...a === void 0 ? {} : { excludedSubtrees: a }
|
|
913
989
|
};
|
|
914
990
|
}
|
|
915
991
|
/** Decode a SEQUENCE OF GeneralSubtree from a permittedSubtrees or excludedSubtrees wrapper. */
|
|
916
|
-
function parseGeneralSubtrees(
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
992
|
+
function parseGeneralSubtrees(e, t) {
|
|
993
|
+
let n = [], r = i(e, t);
|
|
994
|
+
if (r.length === 0) throw Error("name constraints GeneralSubtrees must not be empty");
|
|
995
|
+
for (let t of r) {
|
|
996
|
+
let r = parseGeneralSubtree(e, t);
|
|
997
|
+
r !== void 0 && n.push({ base: r });
|
|
998
|
+
}
|
|
999
|
+
return n;
|
|
1000
|
+
}
|
|
1001
|
+
function parseGeneralSubtree(e, t) {
|
|
1002
|
+
if (t.tag !== 48) throw Error("name constraints GeneralSubtree must use SEQUENCE");
|
|
1003
|
+
let n = i(e, t), r = n[0];
|
|
1004
|
+
if (r === void 0) throw Error("GeneralSubtree base is required");
|
|
1005
|
+
return validateGeneralSubtreeBounds(n.slice(1)), parseNameConstraintGeneralName(e, r);
|
|
1006
|
+
}
|
|
1007
|
+
function validateGeneralSubtreeBounds(e) {
|
|
1008
|
+
let t = !1;
|
|
1009
|
+
for (let n of e) {
|
|
1010
|
+
if (n.tag === 128) {
|
|
1011
|
+
if (t) throw Error("name constraints GeneralSubtree minimum must not repeat");
|
|
1012
|
+
if (t = !0, s(n.value, "name constraints GeneralSubtree minimum") !== 0) throw Error("name constraints GeneralSubtree minimum must be 0");
|
|
1013
|
+
continue;
|
|
935
1014
|
}
|
|
936
|
-
|
|
937
|
-
if (form !== void 0) subtrees.push({ base: form });
|
|
1015
|
+
throw n.tag === 129 ? Error("name constraints GeneralSubtree maximum is not supported") : Error(`Unsupported name constraints GeneralSubtree field tag: ${String(n.tag)}`);
|
|
938
1016
|
}
|
|
939
|
-
return subtrees;
|
|
940
1017
|
}
|
|
941
1018
|
/** Decode a GeneralName for use in name constraints (IP carries address+mask). */
|
|
942
|
-
function parseNameConstraintGeneralName(
|
|
943
|
-
switch (
|
|
1019
|
+
function parseNameConstraintGeneralName(e, t) {
|
|
1020
|
+
switch (t.tag) {
|
|
944
1021
|
case 160: return {
|
|
945
1022
|
type: "otherName",
|
|
946
|
-
value: new Uint8Array(
|
|
1023
|
+
value: new Uint8Array(t.value)
|
|
947
1024
|
};
|
|
948
1025
|
case 129: return {
|
|
949
1026
|
type: "email",
|
|
950
|
-
value:
|
|
1027
|
+
value: N.decode(t.value)
|
|
951
1028
|
};
|
|
952
1029
|
case 130: return {
|
|
953
1030
|
type: "dns",
|
|
954
|
-
value:
|
|
1031
|
+
value: N.decode(t.value)
|
|
955
1032
|
};
|
|
956
1033
|
case 163: return {
|
|
957
1034
|
type: "x400Address",
|
|
958
|
-
value: new Uint8Array(
|
|
1035
|
+
value: new Uint8Array(t.value)
|
|
959
1036
|
};
|
|
960
1037
|
case 134: return {
|
|
961
1038
|
type: "uri",
|
|
962
|
-
value:
|
|
1039
|
+
value: N.decode(t.value)
|
|
963
1040
|
};
|
|
964
1041
|
case 135:
|
|
965
|
-
if (
|
|
1042
|
+
if (t.value.length === 8) return {
|
|
966
1043
|
type: "ip",
|
|
967
|
-
addressBytes:
|
|
968
|
-
maskBytes:
|
|
1044
|
+
addressBytes: t.value.slice(0, 4),
|
|
1045
|
+
maskBytes: t.value.slice(4, 8)
|
|
969
1046
|
};
|
|
970
|
-
if (
|
|
1047
|
+
if (t.value.length === 32) return {
|
|
971
1048
|
type: "ip",
|
|
972
|
-
addressBytes:
|
|
973
|
-
maskBytes:
|
|
1049
|
+
addressBytes: t.value.slice(0, 16),
|
|
1050
|
+
maskBytes: t.value.slice(16, 32)
|
|
974
1051
|
};
|
|
975
|
-
throw
|
|
1052
|
+
throw Error(`Invalid IP name constraint: expected 8 (IPv4) or 32 (IPv6) bytes, got ${String(t.value.length)}`);
|
|
976
1053
|
case 164: return {
|
|
977
1054
|
type: "directoryName",
|
|
978
|
-
derHex:
|
|
1055
|
+
derHex: p(rebuildDirectoryNameFromImplicit(t, e))
|
|
979
1056
|
};
|
|
980
1057
|
case 165: return {
|
|
981
1058
|
type: "ediPartyName",
|
|
982
|
-
value: new Uint8Array(
|
|
1059
|
+
value: new Uint8Array(t.value)
|
|
983
1060
|
};
|
|
984
1061
|
case 136: return {
|
|
985
1062
|
type: "registeredID",
|
|
986
|
-
value:
|
|
1063
|
+
value: c(t.value)
|
|
987
1064
|
};
|
|
988
1065
|
}
|
|
989
|
-
throw
|
|
1066
|
+
throw Error(`Unsupported name constraint GeneralName tag: ${String(t.tag)}`);
|
|
990
1067
|
}
|
|
991
1068
|
/**
|
|
992
1069
|
* Extracts the Name SEQUENCE from an implicitly-tagged directoryName [4].
|
|
@@ -995,96 +1072,104 @@ function parseNameConstraintGeneralName(source, element) {
|
|
|
995
1072
|
* - Proper implicit: [4] replaces SEQUENCE tag, content is RDN SETs directly → wrap with 0x30
|
|
996
1073
|
* - Explicit-like: [4] wraps entire SEQUENCE, content starts with 0x30 → return content as-is
|
|
997
1074
|
*/
|
|
998
|
-
function rebuildDirectoryNameFromImplicit(
|
|
999
|
-
if (
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
result[0] = 48;
|
|
1003
|
-
result.set(lengthEncoded, 1);
|
|
1004
|
-
result.set(element.value, 1 + lengthEncoded.length);
|
|
1005
|
-
return result;
|
|
1075
|
+
function rebuildDirectoryNameFromImplicit(t, n) {
|
|
1076
|
+
if (t.value.length > 0 && t.value[0] === 48) return new Uint8Array(t.value);
|
|
1077
|
+
let r = e(t.value.length), i = new Uint8Array(1 + r.length + t.value.length);
|
|
1078
|
+
return i[0] = 48, i.set(r, 1), i.set(t.value, 1 + r.length), i;
|
|
1006
1079
|
}
|
|
1007
1080
|
/** Decode a DisplayText (UTF8String, IA5String, VisibleString, or BMPString). */
|
|
1008
|
-
function parseDisplayText(
|
|
1009
|
-
switch (
|
|
1081
|
+
function parseDisplayText(e) {
|
|
1082
|
+
switch (e.tag) {
|
|
1010
1083
|
case 12:
|
|
1011
1084
|
case 22:
|
|
1012
|
-
case 26: return
|
|
1013
|
-
case 30: return decodeBmpString(
|
|
1014
|
-
default: throw
|
|
1085
|
+
case 26: return N.decode(e.value);
|
|
1086
|
+
case 30: return decodeBmpString(e.value);
|
|
1087
|
+
default: throw Error(`Unsupported DisplayText tag: ${e.tag}`);
|
|
1015
1088
|
}
|
|
1016
1089
|
}
|
|
1017
1090
|
/** Decode a BMPString (UCS-2 big-endian) to a JS string. */
|
|
1018
|
-
function decodeBmpString(
|
|
1019
|
-
if (
|
|
1020
|
-
let
|
|
1021
|
-
for (let
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
value += String.fromCharCode(left << 8 | right);
|
|
1091
|
+
function decodeBmpString(e) {
|
|
1092
|
+
if (e.length % 2 != 0) throw Error("Invalid BMPString length");
|
|
1093
|
+
let t = "";
|
|
1094
|
+
for (let n = 0; n < e.length; n += 2) {
|
|
1095
|
+
let r = e[n], i = e[n + 1];
|
|
1096
|
+
if (r === void 0 || i === void 0) throw Error("Invalid BMPString content");
|
|
1097
|
+
t += String.fromCharCode(r << 8 | i);
|
|
1026
1098
|
}
|
|
1027
|
-
return
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1099
|
+
return t;
|
|
1100
|
+
}
|
|
1101
|
+
function parseAuthorityKeyIdentifier(e) {
|
|
1102
|
+
let n = f(t(e, 0), "authorityKeyIdentifier sequence");
|
|
1103
|
+
if (n.end !== e.length) throw Error("Trailing data after DER element");
|
|
1104
|
+
let r = {
|
|
1105
|
+
sawAuthorityCertIssuer: !1,
|
|
1106
|
+
sawAuthorityCertSerialNumber: !1,
|
|
1107
|
+
lastFieldOrder: -1
|
|
1108
|
+
}, i = n.start;
|
|
1109
|
+
for (; i < n.end;) {
|
|
1110
|
+
let a = t(e, i);
|
|
1111
|
+
if (a.end > n.end) throw Error("DER child exceeds parent length");
|
|
1112
|
+
parseAuthorityKeyIdentifierField(e, a, r), i = a.end;
|
|
1113
|
+
}
|
|
1114
|
+
if (i !== n.end) throw Error("Malformed DER sequence");
|
|
1115
|
+
if (r.sawAuthorityCertIssuer !== r.sawAuthorityCertSerialNumber) throw Error("authorityKeyIdentifier authorityCertIssuer and authorityCertSerialNumber must appear together");
|
|
1116
|
+
return r.keyIdentifier;
|
|
1117
|
+
}
|
|
1118
|
+
function parseAuthorityKeyIdentifierField(e, t, n) {
|
|
1119
|
+
switch (t.tag) {
|
|
1120
|
+
case 128:
|
|
1121
|
+
parseAuthorityKeyIdentifierKeyId(t, n);
|
|
1122
|
+
return;
|
|
1123
|
+
case 161:
|
|
1124
|
+
parseAuthorityKeyIdentifierIssuer(e, t, n);
|
|
1125
|
+
return;
|
|
1126
|
+
case 130:
|
|
1127
|
+
parseAuthorityKeyIdentifierSerial(t, n);
|
|
1128
|
+
return;
|
|
1129
|
+
default: throw Error(`Unsupported authorityKeyIdentifier field tag: ${String(t.tag)}`);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
function parseAuthorityKeyIdentifierKeyId(e, t) {
|
|
1133
|
+
if (t.keyIdentifier !== void 0) throw Error("authorityKeyIdentifier keyIdentifier must not repeat");
|
|
1134
|
+
if (t.lastFieldOrder >= 0) throw Error("authorityKeyIdentifier fields must preserve DER order");
|
|
1135
|
+
t.keyIdentifier = p(e.value), t.lastFieldOrder = 0;
|
|
1136
|
+
}
|
|
1137
|
+
function parseAuthorityKeyIdentifierIssuer(e, t, n) {
|
|
1138
|
+
if (n.sawAuthorityCertIssuer) throw Error("authorityKeyIdentifier authorityCertIssuer must not repeat");
|
|
1139
|
+
if (n.lastFieldOrder >= 1) throw Error("authorityKeyIdentifier fields must preserve DER order");
|
|
1140
|
+
let r = i(e, t);
|
|
1141
|
+
if (r.length === 0) throw Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
|
|
1142
|
+
for (let t of r) {
|
|
1143
|
+
if ((t.tag & 192) != 128) throw Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
|
|
1144
|
+
parseGeneralName(e, t);
|
|
1065
1145
|
}
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1146
|
+
n.sawAuthorityCertIssuer = !0, n.lastFieldOrder = 1;
|
|
1147
|
+
}
|
|
1148
|
+
function parseAuthorityKeyIdentifierSerial(e, t) {
|
|
1149
|
+
if (t.sawAuthorityCertSerialNumber) throw Error("authorityKeyIdentifier authorityCertSerialNumber must not repeat");
|
|
1150
|
+
if (t.lastFieldOrder >= 2 || !t.sawAuthorityCertIssuer) throw Error("authorityKeyIdentifier fields must preserve DER order");
|
|
1151
|
+
validateImplicitSerialNumberEncoding(e.value, "authorityKeyIdentifier authorityCertSerialNumber"), t.sawAuthorityCertSerialNumber = !0, t.lastFieldOrder = 2;
|
|
1152
|
+
}
|
|
1153
|
+
function validateImplicitSerialNumberEncoding(e, t) {
|
|
1154
|
+
let n = e[0];
|
|
1155
|
+
if (n === void 0) throw Error(`${t} must not be empty`);
|
|
1156
|
+
if (n & 128) throw Error(`${t} must be non-negative`);
|
|
1157
|
+
if (e.length > 1 && n === 0 && !((e[1] ?? 0) & 128)) throw Error(`${t} must use minimal encoding`);
|
|
1158
|
+
}
|
|
1159
|
+
function validateImplicitBitStringContent(e, t) {
|
|
1160
|
+
let n = e[0];
|
|
1161
|
+
if (n === void 0 || n > 7) throw Error(`${t} must use BIT STRING encoding`);
|
|
1162
|
+
let r = e.slice(1);
|
|
1163
|
+
if (r.length === 0) {
|
|
1164
|
+
if (n !== 0) throw Error(`${t} must use BIT STRING encoding`);
|
|
1082
1165
|
return;
|
|
1083
1166
|
}
|
|
1084
|
-
if (
|
|
1085
|
-
|
|
1086
|
-
if (
|
|
1087
|
-
if (
|
|
1167
|
+
if (n === 0) return;
|
|
1168
|
+
let i = r[r.length - 1];
|
|
1169
|
+
if (i === void 0) throw Error(`${t} must use BIT STRING encoding`);
|
|
1170
|
+
if (i & (1 << n) - 1) throw Error(`${t} BIT STRING must not set padding bits`);
|
|
1088
1171
|
}
|
|
1089
1172
|
//#endregion
|
|
1090
|
-
export { decodeExtension, decodeExtensionMap, decodeExtensions, defineExtensionDecoder, defineExtensionDecoderMap, findExtension, getSubjectPublicKey, getSubjectPublicKeyOrThrow, parseAuthorityInfoAccess, parseAuthorityKeyIdentifier, parseBasicConstraints, parseCertificateChainPem, parseCertificateDer, parseCertificateDerOrThrow, parseCertificateFromSource, parseCertificatePem, parseCertificatePemOrThrow, parseCertificatePolicies, parseCertificateSigningRequestDer, parseCertificateSigningRequestDerOrThrow, parseCertificateSigningRequestPem, parseCertificateSigningRequestPemOrThrow, parseCertificatesFromSource, parseCrlDistributionPoints, parseExtendedKeyUsage, parseInhibitAnyPolicy, parseKeyUsage, parseNameConstraints, parsePolicyConstraints, parsePolicyMappings, parseSubjectAltNames };
|
|
1173
|
+
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, parseExtendedKeyUsage, parseInhibitAnyPolicy, parseKeyUsage, parseNameConstraints, parsePolicyConstraints, parsePolicyMappings, parseSubjectAltNames };
|
|
1174
|
+
|
|
1175
|
+
//# sourceMappingURL=parse.js.map
|