micro509 0.8.0 → 0.10.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.
Files changed (108) hide show
  1. package/dist/index.d.ts +4 -4
  2. package/dist/index.js +15 -15
  3. package/dist/internal/asn1/asn1.js +145 -186
  4. package/dist/internal/asn1/asn1.js.map +1 -0
  5. package/dist/internal/asn1/der.js +210 -227
  6. package/dist/internal/asn1/der.js.map +1 -0
  7. package/dist/internal/asn1/oids.js +297 -101
  8. package/dist/internal/asn1/oids.js.map +1 -0
  9. package/dist/internal/asn1/oids2.js +69 -0
  10. package/dist/internal/asn1/oids2.js.map +1 -0
  11. package/dist/internal/crypto/algorithm-names.js +37 -35
  12. package/dist/internal/crypto/algorithm-names.js.map +1 -0
  13. package/dist/internal/crypto/ecdsa.js +25 -28
  14. package/dist/internal/crypto/ecdsa.js.map +1 -0
  15. package/dist/internal/crypto/hash.js +56 -117
  16. package/dist/internal/crypto/hash.js.map +1 -0
  17. package/dist/internal/crypto/pbes2.js +122 -133
  18. package/dist/internal/crypto/pbes2.js.map +1 -0
  19. package/dist/internal/crypto/rsa-pss.js +118 -124
  20. package/dist/internal/crypto/rsa-pss.js.map +1 -0
  21. package/dist/internal/crypto/sig-verify.js +87 -99
  22. package/dist/internal/crypto/sig-verify.js.map +1 -0
  23. package/dist/internal/crypto/signing.d.ts +12 -8
  24. package/dist/internal/crypto/signing.js +53 -55
  25. package/dist/internal/crypto/signing.js.map +1 -0
  26. package/dist/internal/crypto/webcrypto.js +5 -3
  27. package/dist/internal/crypto/webcrypto.js.map +1 -0
  28. package/dist/internal/shared/base64.js +10 -9
  29. package/dist/internal/shared/base64.js.map +1 -0
  30. package/dist/internal/shared/dn.js +50 -57
  31. package/dist/internal/shared/dn.js.map +1 -0
  32. package/dist/internal/shared/ip.js +43 -53
  33. package/dist/internal/shared/ip.js.map +1 -0
  34. package/dist/internal/verify/name-constraints-engine.js +211 -239
  35. package/dist/internal/verify/name-constraints-engine.js.map +1 -0
  36. package/dist/internal/verify/policy-engine.js +228 -247
  37. package/dist/internal/verify/policy-engine.js.map +1 -0
  38. package/dist/internal/verify/verify-path.js +213 -228
  39. package/dist/internal/verify/verify-path.js.map +1 -0
  40. package/dist/internal/x509/extension-bits.js +62 -59
  41. package/dist/internal/x509/extension-bits.js.map +1 -0
  42. package/dist/internal/x509/extension-registry.js +133 -164
  43. package/dist/internal/x509/extension-registry.js.map +1 -0
  44. package/dist/internal/x509/name-fields.js +35 -41
  45. package/dist/internal/x509/name-fields.js.map +1 -0
  46. package/dist/keys/keys.d.ts +626 -447
  47. package/dist/keys/keys.js +652 -404
  48. package/dist/keys/keys.js.map +1 -0
  49. package/dist/keys.d.ts +2 -2
  50. package/dist/keys.js +2 -2
  51. package/dist/pem/pem.d.ts +46 -46
  52. package/dist/pem/pem.js +62 -81
  53. package/dist/pem/pem.js.map +1 -0
  54. package/dist/pem.js +2 -2
  55. package/dist/pkcs/pfx.d.ts +96 -80
  56. package/dist/pkcs/pfx.js +191 -231
  57. package/dist/pkcs/pfx.js.map +1 -0
  58. package/dist/pkcs/pkcs12-mac.d.ts +20 -18
  59. package/dist/pkcs/pkcs12-mac.js +90 -111
  60. package/dist/pkcs/pkcs12-mac.js.map +1 -0
  61. package/dist/pkcs/pkcs7.d.ts +118 -69
  62. package/dist/pkcs/pkcs7.js +401 -443
  63. package/dist/pkcs/pkcs7.js.map +1 -0
  64. package/dist/pkcs.d.ts +2 -2
  65. package/dist/pkcs.js +4 -4
  66. package/dist/result/result.d.ts +37 -32
  67. package/dist/result/result.js +46 -44
  68. package/dist/result/result.js.map +1 -0
  69. package/dist/result.js +2 -2
  70. package/dist/revocation/chain.d.ts +138 -119
  71. package/dist/revocation/chain.js +248 -281
  72. package/dist/revocation/chain.js.map +1 -0
  73. package/dist/revocation/crl.d.ts +125 -123
  74. package/dist/revocation/crl.js +653 -745
  75. package/dist/revocation/crl.js.map +1 -0
  76. package/dist/revocation/ocsp.d.ts +183 -177
  77. package/dist/revocation/ocsp.js +581 -638
  78. package/dist/revocation/ocsp.js.map +1 -0
  79. package/dist/revocation/revocation.d.ts +26 -26
  80. package/dist/revocation/revocation.js +104 -125
  81. package/dist/revocation/revocation.js.map +1 -0
  82. package/dist/revocation.js +5 -5
  83. package/dist/verify/identity.d.ts +42 -42
  84. package/dist/verify/identity.js +158 -167
  85. package/dist/verify/identity.js.map +1 -0
  86. package/dist/verify/name-constraints.d.ts +7 -7
  87. package/dist/verify/policy.d.ts +10 -10
  88. package/dist/verify/verify.d.ts +163 -163
  89. package/dist/verify/verify.js +396 -392
  90. package/dist/verify/verify.js.map +1 -0
  91. package/dist/verify.js +3 -3
  92. package/dist/x509/certificate.d.ts +117 -117
  93. package/dist/x509/certificate.js +51 -60
  94. package/dist/x509/certificate.js.map +1 -0
  95. package/dist/x509/csr.d.ts +25 -25
  96. package/dist/x509/csr.js +26 -30
  97. package/dist/x509/csr.js.map +1 -0
  98. package/dist/x509/extensions.d.ts +228 -184
  99. package/dist/x509/extensions.js +175 -200
  100. package/dist/x509/extensions.js.map +1 -0
  101. package/dist/x509/name.d.ts +94 -93
  102. package/dist/x509/name.js +30 -29
  103. package/dist/x509/name.js.map +1 -0
  104. package/dist/x509/parse.d.ts +185 -185
  105. package/dist/x509/parse.js +638 -676
  106. package/dist/x509/parse.js.map +1 -0
  107. package/dist/x509.js +6 -6
  108. package/package.json +79 -41
@@ -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/oids.js";
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 { importSpkiDer as b, importSpkiDerOrThrow as x } from "../keys/keys.js";
7
+ import { describePublicKeyAlgorithm as S, describeSignatureAlgorithm as C } from "../internal/crypto/algorithm-names.js";
8
+ import { decodeIpAddress as w } from "../internal/shared/ip.js";
9
+ import { parseDistributionPointReasonFlagsContent as T, parseKeyUsageExtension as E } from "../internal/x509/extension-bits.js";
10
+ import { nameFieldKeyFromOid as D } from "../internal/x509/name-fields.js";
11
+ import { parseAuthorityInfoAccessMethodOid as O, parseExtendedKeyUsageOid as k } from "./extensions.js";
12
+ import { decodeAndApplyKnownExtension as A } 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 textDecoder = new TextDecoder();
25
+ const j = 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(decoder) {
33
- return decoder;
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(decoderMap) {
42
- return decoderMap;
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(der, options) {
56
- const topLevel = readSequenceChildren(der, { maxDepth: 64 });
57
- if (topLevel.length !== 3) throw new Error("Malformed Certificate");
58
- const tbsCertificate = requireElement(topLevel[0], "TBSCertificate");
59
- const signatureAlgorithm = requireElement(topLevel[1], "signatureAlgorithm");
60
- const signatureValue = requireElement(topLevel[2], "signatureValue");
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);
61
+ return {
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: C(h.oid, h.parametersDer),
74
+ ...h.parametersDer === void 0 ? {} : { signatureAlgorithmParametersDer: h.parametersDer },
75
+ publicKeyAlgorithmOid: d.oid,
76
+ publicKeyAlgorithmName: S(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 }
95
+ };
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);
107
108
  return {
108
- der: new Uint8Array(der),
109
- version,
110
- serialNumberHex: toHex(serialNumber.value),
111
- tbsCertificateDer: der.slice(tbsCertificate.start - tbsCertificate.headerLength, tbsCertificate.end),
112
- subjectPublicKeyInfoDer: der.slice(subjectPublicKeyInfo.start - subjectPublicKeyInfo.headerLength, subjectPublicKeyInfo.end),
113
- signatureValue: extractBitStringValue(signatureValue),
114
- issuer: parseName(der, issuer),
115
- subject: parseName(der, subject),
116
- notBefore: parsedValidity.notBefore,
117
- notAfter: parsedValidity.notAfter,
118
- signatureAlgorithmOid: parsedSignatureAlgorithm.oid,
119
- signatureAlgorithmName: describeSignatureAlgorithm(parsedSignatureAlgorithm.oid, parsedSignatureAlgorithm.parametersDer),
120
- ...parsedSignatureAlgorithm.parametersDer !== void 0 ? { signatureAlgorithmParametersDer: parsedSignatureAlgorithm.parametersDer } : {},
121
- publicKeyAlgorithmOid: parsedSpki.oid,
122
- publicKeyAlgorithmName: describePublicKeyAlgorithm(parsedSpki.oid, parsedSpki.parametersOid),
123
- ...parsedSpki.parametersDer !== void 0 ? { publicKeyAlgorithmParametersDer: parsedSpki.parametersDer } : {},
124
- ...parsedSpki.parametersOid !== void 0 ? { publicKeyParametersOid: parsedSpki.parametersOid } : {},
125
- extensions: parsedExtensions.all,
126
- ...parsedExtensions.basicConstraints !== void 0 ? { basicConstraints: parsedExtensions.basicConstraints } : {},
127
- ...parsedExtensions.keyUsage !== void 0 ? { keyUsage: parsedExtensions.keyUsage } : {},
128
- ...parsedExtensions.extendedKeyUsage !== void 0 ? { extendedKeyUsage: parsedExtensions.extendedKeyUsage } : {},
129
- ...parsedExtensions.subjectAltNames !== void 0 ? { subjectAltNames: parsedExtensions.subjectAltNames } : {},
130
- ...parsedExtensions.nameConstraints !== void 0 ? { nameConstraints: parsedExtensions.nameConstraints } : {},
131
- ...parsedExtensions.certificatePolicies !== void 0 ? { certificatePolicies: parsedExtensions.certificatePolicies } : {},
132
- ...parsedExtensions.policyMappings !== void 0 ? { policyMappings: parsedExtensions.policyMappings } : {},
133
- ...parsedExtensions.policyConstraints !== void 0 ? { policyConstraints: parsedExtensions.policyConstraints } : {},
134
- ...parsedExtensions.inhibitAnyPolicy !== void 0 ? { inhibitAnyPolicy: parsedExtensions.inhibitAnyPolicy } : {},
135
- ...parsedExtensions.authorityInfoAccess !== void 0 ? { authorityInfoAccess: parsedExtensions.authorityInfoAccess } : {},
136
- ...parsedExtensions.crlDistributionPoints !== void 0 ? { crlDistributionPoints: parsedExtensions.crlDistributionPoints } : {},
137
- ...decodedExtensions !== void 0 ? { decodedExtensions } : {},
138
- ...decodedExtensionMap !== void 0 ? { decodedExtensionMap } : {},
139
- ...parsedExtensions.subjectKeyIdentifier !== void 0 ? { subjectKeyIdentifier: parsedExtensions.subjectKeyIdentifier } : {},
140
- ...parsedExtensions.authorityKeyIdentifier !== void 0 ? { authorityKeyIdentifier: parsedExtensions.authorityKeyIdentifier } : {}
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] } : {}
141
117
  };
142
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(der, options) {
164
+ function parseCertificateDer(e, t) {
160
165
  try {
161
- return successResult(parseCertificateDerOrThrow(der, options));
162
- } catch (error) {
163
- rethrowIfInvariant(error);
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(pem, options) {
186
- return parseCertificateDerOrThrow(pemDecodeOrThrow("CERTIFICATE", pem), options);
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(pem, options) {
201
+ function parseCertificatePem(e, t) {
198
202
  try {
199
- return successResult(parseCertificatePemOrThrow(pem, options));
200
- } catch (error) {
201
- rethrowIfInvariant(error);
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(source, options) {
207
- return typeof source === "string" ? parseCertificatesFromPemBlocks(source, options) : [parseCertificateDerOrThrow(new Uint8Array(source), options)];
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(source, options) {
211
- if (typeof source === "string") return parseCertificatePemOrThrow(source, options);
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(pemBundle, options) {
224
- return parseCertificatesFromPemBlocks(pemBundle, options);
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(der, options) {
233
- const topLevel = readSequenceChildren(der, { maxDepth: 64 });
234
- if (topLevel.length !== 3) throw new Error("Malformed CertificationRequest");
235
- const certificationRequestInfo = requireElement(topLevel[0], "CertificationRequestInfo");
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: der.slice(certificationRequestInfo.start - certificationRequestInfo.headerLength, certificationRequestInfo.end),
256
- subjectPublicKeyInfoDer: der.slice(subjectPublicKeyInfo.start - subjectPublicKeyInfo.headerLength, subjectPublicKeyInfo.end),
257
- signatureValue: extractBitStringValue(signatureValue),
258
- subject: parseName(der, subject),
259
- signatureAlgorithmOid: parsedSignatureAlgorithm.oid,
260
- signatureAlgorithmName: describeSignatureAlgorithm(parsedSignatureAlgorithm.oid, parsedSignatureAlgorithm.parametersDer),
261
- ...parsedSignatureAlgorithm.parametersDer !== void 0 ? { signatureAlgorithmParametersDer: parsedSignatureAlgorithm.parametersDer } : {},
262
- publicKeyAlgorithmOid: parsedSpki.oid,
263
- publicKeyAlgorithmName: describePublicKeyAlgorithm(parsedSpki.oid, parsedSpki.parametersOid),
264
- ...parsedSpki.parametersDer !== void 0 ? { publicKeyAlgorithmParametersDer: parsedSpki.parametersDer } : {},
265
- ...parsedSpki.parametersOid !== void 0 ? { publicKeyParametersOid: parsedSpki.parametersOid } : {},
266
- requestedExtensions: parsedExtensions.all,
267
- ...parsedExtensions.basicConstraints !== void 0 ? { basicConstraints: parsedExtensions.basicConstraints } : {},
268
- ...parsedExtensions.keyUsage !== void 0 ? { keyUsage: parsedExtensions.keyUsage } : {},
269
- ...parsedExtensions.extendedKeyUsage !== void 0 ? { extendedKeyUsage: parsedExtensions.extendedKeyUsage } : {},
270
- ...parsedExtensions.subjectAltNames !== void 0 ? { subjectAltNames: parsedExtensions.subjectAltNames } : {},
271
- ...parsedExtensions.nameConstraints !== void 0 ? { nameConstraints: parsedExtensions.nameConstraints } : {},
272
- ...parsedExtensions.certificatePolicies !== void 0 ? { certificatePolicies: parsedExtensions.certificatePolicies } : {},
273
- ...parsedExtensions.policyMappings !== void 0 ? { policyMappings: parsedExtensions.policyMappings } : {},
274
- ...parsedExtensions.policyConstraints !== void 0 ? { policyConstraints: parsedExtensions.policyConstraints } : {},
275
- ...parsedExtensions.inhibitAnyPolicy !== void 0 ? { inhibitAnyPolicy: parsedExtensions.inhibitAnyPolicy } : {},
276
- ...parsedExtensions.authorityInfoAccess !== void 0 ? { authorityInfoAccess: parsedExtensions.authorityInfoAccess } : {},
277
- ...parsedExtensions.crlDistributionPoints !== void 0 ? { crlDistributionPoints: parsedExtensions.crlDistributionPoints } : {},
278
- ...decodedExtensions !== void 0 ? { decodedExtensions } : {},
279
- ...decodedExtensionMap !== void 0 ? { 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: C(d.oid, d.parametersDer),
245
+ ...d.parametersDer === void 0 ? {} : { signatureAlgorithmParametersDer: d.parametersDer },
246
+ publicKeyAlgorithmOid: l.oid,
247
+ publicKeyAlgorithmName: S(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(der, options) {
288
+ function parseCertificateSigningRequestDer(e, t) {
289
289
  try {
290
- return successResult(parseCertificateSigningRequestDerOrThrow(der, options));
291
- } catch (error) {
292
- rethrowIfInvariant(error);
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(pemBundle, options) {
297
- return splitPemBlocksOrThrow(pemBundle).filter((block) => block.label === "CERTIFICATE").map((block) => parseCertificateDerOrThrow(block.bytes, options));
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(value) {
300
- return "subjectPublicKeyInfoDer" in value;
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(pem, options) {
309
- return parseCertificateSigningRequestDerOrThrow(pemDecodeOrThrow("CERTIFICATE REQUEST", pem), options);
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(pem, options) {
316
+ function parseCertificateSigningRequestPem(e, t) {
318
317
  try {
319
- return successResult(parseCertificateSigningRequestPemOrThrow(pem, options));
320
- } catch (error) {
321
- rethrowIfInvariant(error);
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,16 @@ function parseCertificateSigningRequestPem(pem, options) {
350
348
  *
351
349
  * @see {@linkcode getSubjectPublicKey} for the non-throwing variant
352
350
  */
353
- function getSubjectPublicKeyOrThrow(parsed, algorithm) {
354
- return importSpkiDerOrThrow(parsed.subjectPublicKeyInfoDer, algorithm);
351
+ function getSubjectPublicKeyOrThrow(e, t) {
352
+ return x(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(parsed, algorithm) {
362
- return importSpkiDer(parsed.subjectPublicKeyInfoDer, algorithm);
359
+ function getSubjectPublicKey(e, t) {
360
+ return b(e.subjectPublicKeyInfoDer, t);
363
361
  }
364
362
  /**
365
363
  * Find a raw extension by OID within a parsed extension list.
@@ -368,8 +366,8 @@ function getSubjectPublicKey(parsed, algorithm) {
368
366
  * @param oid Dotted-decimal OID to look up.
369
367
  * @returns The matching extension, or `undefined` if not present.
370
368
  */
371
- function findExtension(extensions, oid) {
372
- return extensions.find((extension) => extension.oid === oid);
369
+ function findExtension(e, t) {
370
+ return e.find((e) => e.oid === t);
373
371
  }
374
372
  /**
375
373
  * Decode a single extension using a custom {@linkcode ExtensionDecoder}.
@@ -378,10 +376,9 @@ function findExtension(extensions, oid) {
378
376
  * @param decoder Decoder whose OID will be matched.
379
377
  * @returns The decoded value, or `undefined` if the extension is absent.
380
378
  */
381
- function decodeExtension(extensions, decoder) {
382
- const extension = findExtension(extensions, decoder.oid);
383
- if (extension === void 0) return;
384
- return decoder.decode(extension);
379
+ function decodeExtension(e, t) {
380
+ let n = findExtension(e, t.oid);
381
+ if (n !== void 0) return t.decode(n);
385
382
  }
386
383
  /**
387
384
  * Decode all matching extensions using an array of {@linkcode ExtensionDecoder}s.
@@ -389,18 +386,17 @@ function decodeExtension(extensions, decoder) {
389
386
  * @param extensions Extension list to search.
390
387
  * @param decoders Decoders to apply. Only matching OIDs produce output.
391
388
  */
392
- function decodeExtensions(extensions, decoders) {
393
- const decoded = [];
394
- for (const decoder of decoders) {
395
- const extension = findExtension(extensions, decoder.oid);
396
- if (extension === void 0) continue;
397
- decoded.push({
398
- oid: extension.oid,
399
- critical: extension.critical,
400
- value: decoder.decode(extension)
389
+ function decodeExtensions(e, t) {
390
+ let n = [];
391
+ for (let r of t) {
392
+ let t = findExtension(e, r.oid);
393
+ t !== void 0 && n.push({
394
+ oid: t.oid,
395
+ critical: t.critical,
396
+ value: r.decode(t)
401
397
  });
402
398
  }
403
- return decoded;
399
+ return n;
404
400
  }
405
401
  /**
406
402
  * Decode all matching extensions using a named {@linkcode ExtensionDecoderMap}.
@@ -408,488 +404,446 @@ function decodeExtensions(extensions, decoders) {
408
404
  * @param extensions Extension list to search.
409
405
  * @param decoderMap Named decoders. Results are keyed by the same map keys.
410
406
  */
411
- function decodeExtensionMap(extensions, decoderMap) {
412
- let decoded = {};
413
- for (const key in decoderMap) {
414
- const decoder = decoderMap[key];
415
- if (decoder === void 0) continue;
416
- const extension = findExtension(extensions, decoder.oid);
417
- if (extension === void 0) continue;
418
- decoded = {
419
- ...decoded,
420
- [key]: {
421
- oid: extension.oid,
422
- critical: extension.critical,
423
- value: decoder.decode(extension)
407
+ function decodeExtensionMap(e, t) {
408
+ let n = {};
409
+ for (let r in t) {
410
+ let i = t[r];
411
+ if (i === void 0) continue;
412
+ let a = findExtension(e, i.oid);
413
+ a !== void 0 && (n = {
414
+ ...n,
415
+ [r]: {
416
+ oid: a.oid,
417
+ critical: a.critical,
418
+ value: i.decode(a)
424
419
  }
425
- };
420
+ });
426
421
  }
427
- return decoded;
422
+ return n;
428
423
  }
429
424
  /** Decode the explicit [3] extensions wrapper from a TBSCertificate. */
430
- function parseExtensionContainer(source, container) {
431
- if (container === void 0) return { all: [] };
432
- return parseExtensionSequence(source, requireElement(childrenOf(source, container)[0], "extensions sequence"), "certificate");
425
+ function parseExtensionContainer(e, t) {
426
+ return t === void 0 ? { all: [] } : parseExtensionSequence(e, f(i(e, t)[0], "extensions sequence"), "certificate");
433
427
  }
434
428
  /** Extract extensions from the CSR extensionRequest attribute. */
435
- function parseRequestedExtensions(source, attributes) {
436
- if (attributes === void 0) return { all: [] };
437
- if (attributes.tag !== 160) throw new Error("CertificationRequestInfo attributes must use [0]");
438
- let requestedExtensions;
439
- for (const attribute of childrenOf(source, attributes)) {
440
- const attributeChildren = childrenOf(source, attribute);
441
- if (attributeChildren.length !== 2) throw new Error("Malformed CSR attribute");
442
- if (decodeObjectIdentifier(requireElement(attributeChildren[0], "attribute OID").value) !== OIDS.extensionRequest) continue;
443
- if (requestedExtensions !== void 0) throw new Error("extensionRequest attribute must not repeat");
444
- const valuesSet = requireElement(attributeChildren[1], "attribute values");
445
- if (valuesSet.tag !== 49) throw new Error("extensionRequest attribute values must use SET");
446
- const values = childrenOf(source, valuesSet);
447
- if (values.length !== 1) throw new Error("extensionRequest attribute must contain exactly one value");
448
- requestedExtensions = parseExtensionSequence(source, requireElement(values[0], "requested extensions"), "csr");
429
+ function parseRequestedExtensions(e, t) {
430
+ if (t === void 0) return { all: [] };
431
+ if (t.tag !== 160) throw Error("CertificationRequestInfo attributes must use [0]");
432
+ let n;
433
+ for (let r of i(e, t)) {
434
+ let t = i(e, r);
435
+ if (t.length !== 2) throw Error("Malformed CSR attribute");
436
+ if (c(f(t[0], "attribute OID").value) !== m.extensionRequest) continue;
437
+ if (n !== void 0) throw Error("extensionRequest attribute must not repeat");
438
+ let a = f(t[1], "attribute values");
439
+ if (a.tag !== 49) throw Error("extensionRequest attribute values must use SET");
440
+ let o = i(e, a);
441
+ if (o.length !== 1) throw Error("extensionRequest attribute must contain exactly one value");
442
+ n = parseExtensionSequence(e, f(o[0], "requested extensions"), "csr");
449
443
  }
450
- return requestedExtensions ?? { all: [] };
444
+ return n ?? { all: [] };
451
445
  }
452
446
  /** Walk a SEQUENCE OF Extension and decode each one, populating known-extension slots. */
453
- function parseExtensionSequence(source, sequenceElement, context) {
454
- const parsed = [];
455
- const knownParsed = {};
456
- const seenOids = /* @__PURE__ */ new Set();
457
- for (const extension of childrenOf(source, sequenceElement)) {
458
- const children = childrenOf(source, extension);
459
- if (children.length < 2 || children.length > 3) throw new Error("Malformed Extension");
460
- const oid = decodeObjectIdentifier(requireElement(children[0], "extension OID").value);
461
- if (seenOids.has(oid)) throw new Error(`Duplicate extension OID: ${oid}`);
462
- seenOids.add(oid);
463
- let offset = 1;
464
- let critical = false;
465
- const maybeCritical = children[offset];
466
- if (maybeCritical?.tag === 1) {
467
- critical = decodeBoolean(maybeCritical.value);
468
- offset += 1;
469
- }
470
- const extnValue = requireElement(children[offset], "extension value");
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);
447
+ function parseExtensionSequence(e, t, n) {
448
+ let r = [], o = {}, s = /* @__PURE__ */ new Set();
449
+ for (let l of i(e, t)) {
450
+ let t = i(e, l);
451
+ if (t.length < 2 || t.length > 3) throw Error("Malformed Extension");
452
+ let u = c(f(t[0], "extension OID").value);
453
+ if (s.has(u)) throw Error(`Duplicate extension OID: ${u}`);
454
+ s.add(u);
455
+ let d = 1, m = !1, h = t[d];
456
+ h?.tag === 1 && (m = a(h.value), d += 1);
457
+ let g = f(t[d], "extension value");
458
+ if (g.tag !== 4 || d !== t.length - 1) throw Error("Extension value must use OCTET STRING");
459
+ r.push({
460
+ oid: u,
461
+ critical: m,
462
+ valueDer: new Uint8Array(g.value),
463
+ valueHex: p(g.value)
464
+ }), A(n, u, o, g.value);
479
465
  }
480
466
  return {
481
- all: parsed,
482
- ...knownParsed
467
+ all: r,
468
+ ...o
483
469
  };
484
470
  }
485
471
  /** Decode an X.501 Name (issuer / subject) into a {@linkcode ParsedName}. */
486
- function parseName(source, element) {
487
- const rdns = [];
488
- const attributes = [];
489
- const values = {};
490
- for (const setElement of childrenOf(source, element)) {
491
- const rdn = parseNameAttributeSet(source, setElement);
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
- }
472
+ function parseName(e, t) {
473
+ let n = [], r = [], a = {};
474
+ for (let o of i(e, t)) {
475
+ let t = parseNameAttributeSet(e, o);
476
+ n.push(t);
477
+ for (let e of t.attributes) r.push(e), e.key !== void 0 && a[e.key] === void 0 && (a[e.key] = e.value);
497
478
  }
498
479
  return {
499
- derHex: toHex(source.slice(element.start - element.headerLength, element.end)),
500
- rdns,
501
- attributes,
502
- values
480
+ derHex: p(e.slice(t.start - t.headerLength, t.end)),
481
+ rdns: n,
482
+ attributes: r,
483
+ values: a
503
484
  };
504
485
  }
505
486
  /** Decode a single RDN SET element. */
506
- function parseRelativeDistinguishedName(source, element) {
507
- return parseNameAttributeSet(source, element);
487
+ function parseRelativeDistinguishedName(e, t) {
488
+ return parseNameAttributeSet(e, t);
508
489
  }
509
490
  /** Decode the AttributeTypeAndValue pairs within a SET element. */
510
- function parseNameAttributeSet(source, setElement) {
511
- const attributes = [];
512
- const values = {};
513
- for (const attributeSequence of childrenOf(source, setElement)) {
514
- const parts = childrenOf(source, attributeSequence);
515
- const oid = decodeObjectIdentifier(requireElement(parts[0], "name OID").value);
516
- const valueElement = requireElement(parts[1], "name value");
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
491
+ function parseNameAttributeSet(e, t) {
492
+ let n = [], r = {};
493
+ for (let a of i(e, t)) {
494
+ let t = i(e, a), o = c(f(t[0], "name OID").value), s = f(t[1], "name value"), u = D(o), d = l(s.tag, s.value), p = u === void 0 ? {
495
+ oid: o,
496
+ valueTag: s.tag,
497
+ value: d
524
498
  } : {
525
- oid,
526
- valueTag: valueElement.tag,
527
- value: fieldValue
499
+ oid: o,
500
+ key: u,
501
+ valueTag: s.tag,
502
+ value: d
528
503
  };
529
- attributes.push(attribute);
530
- if (fieldKey !== void 0 && values[fieldKey] === void 0) values[fieldKey] = fieldValue;
504
+ n.push(p), u !== void 0 && r[u] === void 0 && (r[u] = d);
531
505
  }
532
506
  return {
533
- derHex: toHex(source.slice(setElement.start - setElement.headerLength, setElement.end)),
534
- attributes,
535
- values
507
+ derHex: p(e.slice(t.start - t.headerLength, t.end)),
508
+ attributes: n,
509
+ values: r
536
510
  };
537
511
  }
538
512
  /** Decode the Validity SEQUENCE into notBefore / notAfter Dates. */
539
- function parseValidity(source, element) {
540
- const children = childrenOf(source, element);
513
+ function parseValidity(e, t) {
514
+ let n = i(e, t);
541
515
  return {
542
- notBefore: parseTime(requireElement(children[0], "notBefore")),
543
- notAfter: parseTime(requireElement(children[1], "notAfter"))
516
+ notBefore: d(f(n[0], "notBefore")),
517
+ notAfter: d(f(n[1], "notAfter"))
544
518
  };
545
519
  }
546
520
  /** Extract the algorithm identifier from a SubjectPublicKeyInfo SEQUENCE. */
547
- function parseSubjectPublicKeyInfo(source, element) {
548
- if (element.tag !== 48) throw new Error("SubjectPublicKeyInfo must use SEQUENCE");
549
- const children = childrenOf(source, element);
550
- if (children.length !== 2) throw new Error("SubjectPublicKeyInfo must contain algorithm and subjectPublicKey");
551
- const algorithmElement = requireElement(children[0], "SPKI algorithm");
552
- if (algorithmElement.tag !== 48) throw new Error("SubjectPublicKeyInfo algorithm must use SEQUENCE");
553
- const algorithm = parseAlgorithmIdentifier(source, algorithmElement);
554
- extractBitStringValue(requireElement(children[1], "subjectPublicKey BIT STRING"));
555
- return algorithm;
521
+ function parseSubjectPublicKeyInfo(e, t) {
522
+ if (t.tag !== 48) throw Error("SubjectPublicKeyInfo must use SEQUENCE");
523
+ let n = i(e, t);
524
+ if (n.length !== 2) throw Error("SubjectPublicKeyInfo must contain algorithm and subjectPublicKey");
525
+ let r = f(n[0], "SPKI algorithm");
526
+ if (r.tag !== 48) throw Error("SubjectPublicKeyInfo algorithm must use SEQUENCE");
527
+ let a = parseAlgorithmIdentifier(e, r);
528
+ return u(f(n[1], "subjectPublicKey BIT STRING")), a;
556
529
  }
557
530
  /** Decode an AlgorithmIdentifier SEQUENCE (OID + optional parameters). */
558
- function parseAlgorithmIdentifier(source, element) {
559
- const children = childrenOf(source, element);
560
- if (children.length === 0 || children.length > 2) throw new Error("Malformed AlgorithmIdentifier");
561
- const oid = decodeObjectIdentifier(requireElement(children[0], "algorithm OID").value);
562
- const parameters = children[1];
563
- if (parameters === void 0) return { oid };
564
- const parametersDer = source.slice(parameters.start - parameters.headerLength, parameters.end);
565
- if (parameters?.tag === 6) return {
566
- oid,
567
- parametersDer,
568
- parametersOid: decodeObjectIdentifier(parameters.value)
569
- };
570
- return {
571
- oid,
572
- parametersDer
531
+ function parseAlgorithmIdentifier(e, t) {
532
+ let n = i(e, t);
533
+ if (n.length === 0 || n.length > 2) throw Error("Malformed AlgorithmIdentifier");
534
+ let r = c(f(n[0], "algorithm OID").value), a = n[1];
535
+ if (a === void 0) return { oid: r };
536
+ let o = e.slice(a.start - a.headerLength, a.end);
537
+ return a?.tag === 6 ? {
538
+ oid: r,
539
+ parametersDer: o,
540
+ parametersOid: c(a.value)
541
+ } : {
542
+ oid: r,
543
+ parametersDer: o
573
544
  };
574
545
  }
575
- function assertMatchingCertificateSignatureAlgorithms(tbsSignatureAlgorithm, signatureAlgorithm) {
576
- if (tbsSignatureAlgorithm.oid !== signatureAlgorithm.oid || !optionalBytesEqual(tbsSignatureAlgorithm.parametersDer, signatureAlgorithm.parametersDer)) throw new Error("Certificate signatureAlgorithm must match TBSCertificate signature");
546
+ function assertMatchingCertificateSignatureAlgorithms(e, t) {
547
+ if (e.oid !== t.oid || !optionalBytesEqual(e.parametersDer, t.parametersDer)) throw Error("Certificate signatureAlgorithm must match TBSCertificate signature");
577
548
  }
578
- function optionalBytesEqual(left, right) {
579
- if (left === void 0 || right === void 0) return left === right;
580
- if (left.length !== right.length) return false;
581
- for (let index = 0; index < left.length; index += 1) if (left[index] !== right[index]) return false;
582
- return true;
549
+ function optionalBytesEqual(e, t) {
550
+ if (e === void 0 || t === void 0) return e === t;
551
+ if (e.length !== t.length) return !1;
552
+ for (let n = 0; n < e.length; n += 1) if (e[n] !== t[n]) return !1;
553
+ return !0;
583
554
  }
584
555
  /** @internal Decode the Basic Constraints extension value DER. */
585
- function parseBasicConstraints(bytes) {
586
- const element = readRootElement(bytes, { maxDepth: 64 });
587
- if (element.tag !== 48) throw new Error("basicConstraints must use SEQUENCE");
588
- const children = childrenOf(bytes, element);
589
- let ca = false;
590
- let pathLength;
591
- let sawCa = false;
592
- let sawPathLength = false;
593
- for (const child of children) {
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);
556
+ function parseBasicConstraints(e) {
557
+ let t = n(e, { maxDepth: 64 });
558
+ if (t.tag !== 48) throw Error("basicConstraints must use SEQUENCE");
559
+ let r = i(e, t), o = !1, c, l = !1, u = !1;
560
+ for (let e of r) {
561
+ if (e.tag === 1) {
562
+ if (l) throw Error("basicConstraints cA must not repeat");
563
+ if (u) throw Error("basicConstraints cA must precede pathLength");
564
+ l = !0, o = a(e.value);
599
565
  continue;
600
566
  }
601
- if (child.tag === 2) {
602
- if (sawPathLength) throw new Error("basicConstraints pathLength must not repeat");
603
- sawPathLength = true;
604
- pathLength = decodeNonNegativeIntegerNumber(child.value, "basicConstraints pathLength");
567
+ if (e.tag === 2) {
568
+ if (u) throw Error("basicConstraints pathLength must not repeat");
569
+ u = !0, c = s(e.value, "basicConstraints pathLength");
605
570
  continue;
606
571
  }
607
- throw new Error(`Unsupported basicConstraints field tag: ${String(child.tag)}`);
572
+ throw Error(`Unsupported basicConstraints field tag: ${String(e.tag)}`);
608
573
  }
609
- if (pathLength !== void 0 && ca !== true) throw new Error("basicConstraints pathLength requires cA = true");
610
- if (ca) return pathLength !== void 0 ? {
611
- ca: true,
612
- pathLength
613
- } : { ca: true };
614
- return { ca: false };
574
+ if (c !== void 0 && o !== !0) throw Error("basicConstraints pathLength requires cA = true");
575
+ return o ? c === void 0 ? { ca: !0 } : {
576
+ ca: !0,
577
+ pathLength: c
578
+ } : { ca: !1 };
615
579
  }
616
580
  /** @internal Decode the Key Usage BIT STRING extension value. */
617
- function parseKeyUsage(bytes) {
618
- return parseKeyUsageExtension(bytes);
581
+ function parseKeyUsage(e) {
582
+ return E(e);
619
583
  }
620
584
  /** @internal Decode the Extended Key Usage SEQUENCE OF OIDs. */
621
- function parseExtendedKeyUsage(bytes) {
622
- return childrenOf(bytes, requireElement(readRootElement(bytes, { maxDepth: 64 }), "extendedKeyUsage sequence")).map((element) => parseExtendedKeyUsageOid(decodeObjectIdentifier(element.value)));
585
+ function parseExtendedKeyUsage(e) {
586
+ return i(e, f(n(e, { maxDepth: 64 }), "extendedKeyUsage sequence")).map((e) => k(c(e.value)));
623
587
  }
624
588
  /** @internal Decode the Certificate Policies extension value. */
625
- function parseCertificatePolicies(bytes) {
626
- const policyElements = childrenOf(bytes, requireElement(readRootElement(bytes, { maxDepth: 64 }), "certificatePolicies sequence"));
627
- if (policyElements.length === 0) throw new Error("certificatePolicies must not be empty");
628
- return policyElements.map((policyElement) => parsePolicyInformation(bytes, policyElement));
589
+ function parseCertificatePolicies(e) {
590
+ let t = i(e, f(n(e, { maxDepth: 64 }), "certificatePolicies sequence"));
591
+ if (t.length === 0) throw Error("certificatePolicies must not be empty");
592
+ return t.map((t) => parsePolicyInformation(e, t));
629
593
  }
630
594
  /** Decode a single PolicyInformation SEQUENCE (OID + optional qualifiers). */
631
- function parsePolicyInformation(source, element) {
632
- const children = childrenOf(source, element);
633
- const policyIdentifier = decodeObjectIdentifier(requireElement(children[0], "policyIdentifier").value);
634
- const qualifiersElement = children[1];
635
- if (children.length > 2) throw new Error("policyInformation has unexpected trailing fields");
636
- if (qualifiersElement === void 0) return { policyIdentifier };
637
- const qualifiers = childrenOf(source, qualifiersElement);
638
- if (qualifiers.length === 0) throw new Error("policyQualifiers must not be empty");
595
+ function parsePolicyInformation(e, t) {
596
+ let n = i(e, t), r = c(f(n[0], "policyIdentifier").value), a = n[1];
597
+ if (n.length > 2) throw Error("policyInformation has unexpected trailing fields");
598
+ if (a === void 0) return { policyIdentifier: r };
599
+ let o = i(e, a);
600
+ if (o.length === 0) throw Error("policyQualifiers must not be empty");
639
601
  return {
640
- policyIdentifier,
641
- policyQualifiers: qualifiers.map((qualifierElement) => parsePolicyQualifierInfo(source, qualifierElement))
602
+ policyIdentifier: r,
603
+ policyQualifiers: o.map((t) => parsePolicyQualifierInfo(e, t))
642
604
  };
643
605
  }
644
606
  /** Decode a PolicyQualifierInfo (CPS URI, UserNotice, or opaque OID). */
645
- function parsePolicyQualifierInfo(source, element) {
646
- const children = childrenOf(source, element);
647
- const qualifierId = decodeObjectIdentifier(requireElement(children[0], "policyQualifierId").value);
648
- const qualifierValue = requireElement(children[1], "policyQualifier");
649
- if (children.length > 2) throw new Error("policyQualifierInfo has unexpected trailing fields");
650
- if (qualifierId === OIDS.cpsPolicyQualifier) {
651
- if (qualifierValue.tag !== 22) throw new Error("cps policy qualifier must use IA5String");
607
+ function parsePolicyQualifierInfo(e, t) {
608
+ let n = i(e, t), r = c(f(n[0], "policyQualifierId").value), a = f(n[1], "policyQualifier");
609
+ if (n.length > 2) throw Error("policyQualifierInfo has unexpected trailing fields");
610
+ if (r === m.cpsPolicyQualifier) {
611
+ if (a.tag !== 22) throw Error("cps policy qualifier must use IA5String");
652
612
  return {
653
613
  type: "cps",
654
- uri: decodeString(qualifierValue.tag, qualifierValue.value)
614
+ uri: l(a.tag, a.value)
655
615
  };
656
616
  }
657
- if (qualifierId === OIDS.userNoticePolicyQualifier) return {
617
+ return r === m.userNoticePolicyQualifier ? {
658
618
  type: "userNotice",
659
- ...parseUserNoticePolicyQualifierInfo(source, qualifierValue)
660
- };
661
- return {
619
+ ...parseUserNoticePolicyQualifierInfo(e, a)
620
+ } : {
662
621
  type: "oid",
663
- oid: qualifierId,
664
- qualifierDer: source.slice(qualifierValue.start - qualifierValue.headerLength, qualifierValue.end)
622
+ oid: r,
623
+ qualifierDer: e.slice(a.start - a.headerLength, a.end)
665
624
  };
666
625
  }
667
626
  /** Decode a UserNotice qualifier (optional noticeRef + optional explicitText). */
668
- function parseUserNoticePolicyQualifierInfo(source, element) {
669
- const children = childrenOf(source, element);
670
- let noticeRef;
671
- let explicitText;
672
- for (const child of children) {
673
- if (child.tag === 48) {
674
- if (noticeRef !== void 0) throw new Error("userNotice must not contain multiple noticeRef values");
675
- noticeRef = parsePolicyNoticeReference(source, child);
627
+ function parseUserNoticePolicyQualifierInfo(e, t) {
628
+ let n = i(e, t), r, a;
629
+ for (let t of n) {
630
+ if (t.tag === 48) {
631
+ if (r !== void 0) throw Error("userNotice must not contain multiple noticeRef values");
632
+ r = parsePolicyNoticeReference(e, t);
676
633
  continue;
677
634
  }
678
- if (explicitText !== void 0) throw new Error("userNotice must not contain multiple explicitText values");
679
- explicitText = parseDisplayText(child);
635
+ if (a !== void 0) throw Error("userNotice must not contain multiple explicitText values");
636
+ a = parseDisplayText(t);
680
637
  }
681
638
  return {
682
- ...noticeRef === void 0 ? {} : { noticeRef },
683
- ...explicitText === void 0 ? {} : { explicitText }
639
+ ...r === void 0 ? {} : { noticeRef: r },
640
+ ...a === void 0 ? {} : { explicitText: a }
684
641
  };
685
642
  }
686
643
  /** Decode a NoticeReference (organization name + notice number list). */
687
- function parsePolicyNoticeReference(source, element) {
688
- if (element.tag !== 48) throw new Error("noticeRef must use SEQUENCE");
689
- const children = childrenOf(source, element);
690
- const organization = parseDisplayText(requireElement(children[0], "noticeRef organization"));
691
- const noticeNumbersElement = requireElement(children[1], "noticeRef noticeNumbers");
692
- if (children.length > 2) throw new Error("noticeRef has unexpected trailing fields");
644
+ function parsePolicyNoticeReference(e, t) {
645
+ if (t.tag !== 48) throw Error("noticeRef must use SEQUENCE");
646
+ let n = i(e, t), r = parseDisplayText(f(n[0], "noticeRef organization")), a = f(n[1], "noticeRef noticeNumbers");
647
+ if (n.length > 2) throw Error("noticeRef has unexpected trailing fields");
693
648
  return {
694
- organization,
695
- noticeNumbers: parsePolicyNoticeNumbers(source, noticeNumbersElement)
649
+ organization: r,
650
+ noticeNumbers: parsePolicyNoticeNumbers(e, a)
696
651
  };
697
652
  }
698
653
  /** Decode a SEQUENCE OF INTEGER notice numbers. */
699
- function parsePolicyNoticeNumbers(source, element) {
700
- if (element.tag !== 48) throw new Error("noticeRef noticeNumbers must use SEQUENCE");
701
- const noticeNumberElements = childrenOf(source, element);
702
- if (noticeNumberElements.length === 0) throw new Error("noticeRef noticeNumbers must not be empty");
703
- return noticeNumberElements.map((noticeNumberElement) => {
704
- if (noticeNumberElement.tag !== 2) throw new Error("noticeRef noticeNumber must use INTEGER");
705
- return decodeNonNegativeIntegerNumber(noticeNumberElement.value, "noticeRef noticeNumber");
654
+ function parsePolicyNoticeNumbers(e, t) {
655
+ if (t.tag !== 48) throw Error("noticeRef noticeNumbers must use SEQUENCE");
656
+ let n = i(e, t);
657
+ if (n.length === 0) throw Error("noticeRef noticeNumbers must not be empty");
658
+ return n.map((e) => {
659
+ if (e.tag !== 2) throw Error("noticeRef noticeNumber must use INTEGER");
660
+ return s(e.value, "noticeRef noticeNumber");
706
661
  });
707
662
  }
708
663
  /** @internal Decode the Policy Mappings extension value. */
709
- function parsePolicyMappings(bytes) {
710
- const mappingElements = childrenOf(bytes, requireElement(readRootElement(bytes, { maxDepth: 64 }), "policyMappings sequence"));
711
- if (mappingElements.length === 0) throw new Error("policyMappings must not be empty");
712
- return mappingElements.map((mappingElement) => {
713
- const children = childrenOf(bytes, mappingElement);
714
- const issuerDomainPolicy = decodeObjectIdentifier(requireElement(children[0], "policyMappings issuerDomainPolicy").value);
715
- const subjectDomainPolicy = decodeObjectIdentifier(requireElement(children[1], "policyMappings subjectDomainPolicy").value);
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");
664
+ function parsePolicyMappings(e) {
665
+ let t = i(e, f(n(e, { maxDepth: 64 }), "policyMappings sequence"));
666
+ if (t.length === 0) throw Error("policyMappings must not be empty");
667
+ return t.map((t) => {
668
+ let n = i(e, t), r = c(f(n[0], "policyMappings issuerDomainPolicy").value), a = c(f(n[1], "policyMappings subjectDomainPolicy").value);
669
+ if (n.length > 2) throw Error("policyMappings entry has unexpected trailing fields");
670
+ if (r === m.anyPolicy || a === m.anyPolicy) throw Error("policyMappings must not use anyPolicy");
718
671
  return {
719
- issuerDomainPolicy,
720
- subjectDomainPolicy
672
+ issuerDomainPolicy: r,
673
+ subjectDomainPolicy: a
721
674
  };
722
675
  });
723
676
  }
724
677
  /** @internal Decode the Policy Constraints extension value. */
725
- function parsePolicyConstraints(bytes) {
726
- const sequenceElement = requireElement(readRootElement(bytes, { maxDepth: 64 }), "policyConstraints sequence");
727
- let requireExplicitPolicy;
728
- let inhibitPolicyMapping;
729
- for (const child of childrenOf(bytes, sequenceElement)) {
730
- if (child.tag === 128) {
731
- if (requireExplicitPolicy !== void 0) throw new Error("policyConstraints must not repeat requireExplicitPolicy");
732
- requireExplicitPolicy = decodeNonNegativeIntegerNumber(child.value, "policyConstraints requireExplicitPolicy");
678
+ function parsePolicyConstraints(e) {
679
+ let t = f(n(e, { maxDepth: 64 }), "policyConstraints sequence"), r, a;
680
+ for (let n of i(e, t)) {
681
+ if (n.tag === 128) {
682
+ if (r !== void 0) throw Error("policyConstraints must not repeat requireExplicitPolicy");
683
+ r = s(n.value, "policyConstraints requireExplicitPolicy");
733
684
  continue;
734
685
  }
735
- if (child.tag === 129) {
736
- if (inhibitPolicyMapping !== void 0) throw new Error("policyConstraints must not repeat inhibitPolicyMapping");
737
- inhibitPolicyMapping = decodeNonNegativeIntegerNumber(child.value, "policyConstraints inhibitPolicyMapping");
686
+ if (n.tag === 129) {
687
+ if (a !== void 0) throw Error("policyConstraints must not repeat inhibitPolicyMapping");
688
+ a = s(n.value, "policyConstraints inhibitPolicyMapping");
738
689
  continue;
739
690
  }
740
- throw new Error(`Unsupported policyConstraints field tag: ${child.tag}`);
691
+ throw Error(`Unsupported policyConstraints field tag: ${n.tag}`);
741
692
  }
742
- if (requireExplicitPolicy === void 0 && inhibitPolicyMapping === void 0) throw new Error("policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping");
693
+ if (r === void 0 && a === void 0) throw Error("policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping");
743
694
  return {
744
- ...requireExplicitPolicy === void 0 ? {} : { requireExplicitPolicy },
745
- ...inhibitPolicyMapping === void 0 ? {} : { inhibitPolicyMapping }
695
+ ...r === void 0 ? {} : { requireExplicitPolicy: r },
696
+ ...a === void 0 ? {} : { inhibitPolicyMapping: a }
746
697
  };
747
698
  }
748
699
  /** @internal Decode the Inhibit anyPolicy extension (single INTEGER). */
749
- function parseInhibitAnyPolicy(bytes) {
750
- const integerElement = requireElement(readRootElement(bytes, { maxDepth: 64 }), "inhibitAnyPolicy integer");
751
- if (integerElement.tag !== 2) throw new Error("inhibitAnyPolicy must be an INTEGER");
752
- return { skipCerts: decodeNonNegativeIntegerNumber(integerElement.value, "inhibitAnyPolicy skipCerts") };
700
+ function parseInhibitAnyPolicy(e) {
701
+ let t = f(n(e, { maxDepth: 64 }), "inhibitAnyPolicy integer");
702
+ if (t.tag !== 2) throw Error("inhibitAnyPolicy must be an INTEGER");
703
+ return { skipCerts: s(t.value, "inhibitAnyPolicy skipCerts") };
753
704
  }
754
705
  /** @internal Decode the Subject Alternative Names SEQUENCE OF GeneralName. */
755
- function parseSubjectAltNames(bytes) {
756
- return childrenOf(bytes, requireElement(readRootElement(bytes, { maxDepth: 64 }), "subjectAltName sequence")).map((element) => parseGeneralName(bytes, element));
706
+ function parseSubjectAltNames(e) {
707
+ return i(e, f(n(e, { maxDepth: 64 }), "subjectAltName sequence")).map((t) => parseGeneralName(e, t));
757
708
  }
758
709
  /** @internal Decode the Authority Information Access extension value. */
759
- function parseAuthorityInfoAccess(bytes) {
760
- const sequenceElement = requireElement(readRootElement(bytes, { maxDepth: 64 }), "authorityInfoAccess sequence");
761
- if (sequenceElement.tag !== 48) throw new Error("authorityInfoAccess must use SEQUENCE");
762
- const accessDescriptions = childrenOf(bytes, sequenceElement);
763
- if (accessDescriptions.length === 0) throw new Error("authorityInfoAccess must not be empty");
764
- return accessDescriptions.map((element) => {
765
- if (element.tag !== 48) throw new Error("authorityInfoAccess entry must use SEQUENCE");
766
- const children = childrenOf(bytes, element);
767
- if (children.length !== 2) throw new Error("authorityInfoAccess entry must contain method and location only");
768
- const method = requireElement(children[0], "authorityInfoAccess method");
769
- const location = requireElement(children[1], "authorityInfoAccess location");
770
- if (method.tag !== 6) throw new Error("authorityInfoAccess method must use OBJECT IDENTIFIER");
771
- if (location.tag !== 134) throw new Error(`Unsupported authorityInfoAccess location tag: ${location.tag}`);
710
+ function parseAuthorityInfoAccess(e) {
711
+ let t = f(n(e, { maxDepth: 64 }), "authorityInfoAccess sequence");
712
+ if (t.tag !== 48) throw Error("authorityInfoAccess must use SEQUENCE");
713
+ let r = i(e, t);
714
+ if (r.length === 0) throw Error("authorityInfoAccess must not be empty");
715
+ return r.map((t) => {
716
+ if (t.tag !== 48) throw Error("authorityInfoAccess entry must use SEQUENCE");
717
+ let n = i(e, t);
718
+ if (n.length !== 2) throw Error("authorityInfoAccess entry must contain method and location only");
719
+ let r = f(n[0], "authorityInfoAccess method"), a = f(n[1], "authorityInfoAccess location");
720
+ if (r.tag !== 6) throw Error("authorityInfoAccess method must use OBJECT IDENTIFIER");
721
+ if (a.tag !== 134) throw Error(`Unsupported authorityInfoAccess location tag: ${a.tag}`);
772
722
  return {
773
- method: parseAuthorityInfoAccessMethodOid(decodeObjectIdentifier(method.value)),
774
- uri: textDecoder.decode(location.value)
723
+ method: O(c(r.value)),
724
+ uri: j.decode(a.value)
775
725
  };
776
726
  });
777
727
  }
778
728
  /** @internal Decode the CRL Distribution Points extension value. */
779
- function parseCrlDistributionPoints(bytes) {
780
- const sequenceElement = requireElement(readRootElement(bytes, { maxDepth: 64 }), "CRLDistributionPoints sequence");
781
- if (sequenceElement.tag !== 48) throw new Error("CRLDistributionPoints must use SEQUENCE");
782
- const elements = childrenOf(bytes, sequenceElement);
783
- if (elements.length === 0) throw new Error("CRLDistributionPoints must not be empty");
784
- const points = [];
785
- for (const distributionPoint of elements) points.push(parseDistributionPoint(bytes, distributionPoint));
786
- return points;
729
+ function parseCrlDistributionPoints(e) {
730
+ let t = f(n(e, { maxDepth: 64 }), "CRLDistributionPoints sequence");
731
+ if (t.tag !== 48) throw Error("CRLDistributionPoints must use SEQUENCE");
732
+ let r = i(e, t);
733
+ if (r.length === 0) throw Error("CRLDistributionPoints must not be empty");
734
+ let a = [];
735
+ for (let t of r) a.push(parseDistributionPoint(e, t));
736
+ return a;
787
737
  }
788
738
  /** Decode a single DistributionPoint SEQUENCE. */
789
- function parseDistributionPoint(source, element) {
790
- if (element.tag !== 48) throw new Error("DistributionPoint must use SEQUENCE");
791
- let distributionPoint;
792
- let reasons;
793
- let crlIssuer;
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");
739
+ function parseDistributionPoint(e, t) {
740
+ if (t.tag !== 48) throw Error("DistributionPoint must use SEQUENCE");
741
+ let n = {};
742
+ for (let r of i(e, t)) parseDistributionPointField(e, r, n);
743
+ if (n.distributionPoint === void 0 && n.crlIssuer === void 0) throw Error("DistributionPoint must include distributionPoint or crlIssuer");
805
744
  return {
806
- ...distributionPoint === void 0 ? {} : { distributionPoint },
807
- ...reasons === void 0 ? {} : { reasons },
808
- ...crlIssuer === void 0 ? {} : { crlIssuer }
745
+ ...n.distributionPoint === void 0 ? {} : { distributionPoint: n.distributionPoint },
746
+ ...n.reasons === void 0 ? {} : { reasons: n.reasons },
747
+ ...n.crlIssuer === void 0 ? {} : { crlIssuer: n.crlIssuer }
809
748
  };
810
749
  }
750
+ function parseDistributionPointField(e, t, n) {
751
+ switch (t.tag) {
752
+ case 160:
753
+ if (n.distributionPoint !== void 0) throw Error("DistributionPoint distributionPoint must not repeat");
754
+ n.distributionPoint = parseDistributionPointName(e, t);
755
+ return;
756
+ case 129:
757
+ if (n.reasons !== void 0) throw Error("DistributionPoint reasons must not repeat");
758
+ n.reasons = T(t.value);
759
+ return;
760
+ case 162:
761
+ if (n.crlIssuer !== void 0) throw Error("DistributionPoint crlIssuer must not repeat");
762
+ n.crlIssuer = parseGeneralNames(e, t);
763
+ return;
764
+ default: throw Error(`Unsupported DistributionPoint field tag: ${String(t.tag)}`);
765
+ }
766
+ }
811
767
  /** Decode a DistributionPointName (fullName or relativeName). */
812
- function parseDistributionPointName(source, element) {
813
- const children = childrenOf(source, element);
814
- if (children.length !== 1) throw new Error("distributionPointName must contain exactly one choice");
815
- const distributionPointName = requireElement(children[0], "distributionPointName");
816
- if (distributionPointName.tag === 160) {
817
- const fullName = childrenOf(source, distributionPointName);
818
- if (fullName.length === 0) throw new Error("distributionPointName fullName must not be empty");
819
- for (const name of fullName) if ((name.tag & 192) !== 128) throw new Error("distributionPointName fullName must contain GeneralName entries");
820
- return { fullName: fullName.map((name) => parseGeneralName(source, name)) };
768
+ function parseDistributionPointName(e, t) {
769
+ let n = i(e, t);
770
+ if (n.length !== 1) throw Error("distributionPointName must contain exactly one choice");
771
+ let r = f(n[0], "distributionPointName");
772
+ if (r.tag === 160) {
773
+ let t = i(e, r);
774
+ if (t.length === 0) throw Error("distributionPointName fullName must not be empty");
775
+ for (let e of t) if ((e.tag & 192) != 128) throw Error("distributionPointName fullName must contain GeneralName entries");
776
+ return { fullName: t.map((t) => parseGeneralName(e, t)) };
821
777
  }
822
- if (distributionPointName.tag === 161) return { relativeName: parseRelativeDistinguishedName(source, distributionPointName) };
823
- throw new Error(`Unsupported distributionPointName tag: ${distributionPointName.tag}`);
778
+ if (r.tag === 161) return { relativeName: parseRelativeDistinguishedName(e, r) };
779
+ throw Error(`Unsupported distributionPointName tag: ${r.tag}`);
824
780
  }
825
781
  /** Decode a SEQUENCE OF GeneralName. */
826
- function parseGeneralNames(source, element) {
827
- const names = childrenOf(source, element);
828
- if (names.length === 0) throw new Error("GeneralNames must not be empty");
829
- for (const name of names) if ((name.tag & 192) !== 128) throw new Error("GeneralNames must contain GeneralName entries");
830
- return names.map((name) => parseGeneralName(source, name));
782
+ function parseGeneralNames(e, t) {
783
+ let n = i(e, t);
784
+ if (n.length === 0) throw Error("GeneralNames must not be empty");
785
+ for (let e of n) if ((e.tag & 192) != 128) throw Error("GeneralNames must contain GeneralName entries");
786
+ return n.map((t) => parseGeneralName(e, t));
831
787
  }
832
788
  /** Decode a single GeneralName from its implicit context tag. */
833
- function parseGeneralName(source, element) {
834
- switch (element.tag) {
789
+ function parseGeneralName(e, t) {
790
+ switch (t.tag) {
835
791
  case 160: {
836
- const otherName = parseOtherName(source, element);
837
- if (otherName !== void 0) return otherName;
838
- return {
792
+ let n = parseOtherName(e, t);
793
+ return n === void 0 ? {
839
794
  type: "unknown",
840
- tag: element.tag,
841
- value: source.slice(element.start, element.end)
842
- };
795
+ tag: t.tag,
796
+ value: e.slice(t.start, t.end)
797
+ } : n;
843
798
  }
844
799
  case 129: return {
845
800
  type: "email",
846
- value: textDecoder.decode(element.value)
801
+ value: j.decode(t.value)
847
802
  };
848
803
  case 130: return {
849
804
  type: "dns",
850
- value: textDecoder.decode(element.value)
805
+ value: j.decode(t.value)
851
806
  };
852
807
  case 134: return {
853
808
  type: "uri",
854
- value: textDecoder.decode(element.value)
809
+ value: j.decode(t.value)
855
810
  };
856
811
  case 135: return {
857
812
  type: "ip",
858
- value: decodeIpAddress(element.value)
813
+ value: w(t.value)
859
814
  };
860
815
  case 164: return {
861
816
  type: "directoryName",
862
- derHex: toHex(rebuildDirectoryNameFromImplicit(element, source))
817
+ derHex: p(rebuildDirectoryNameFromImplicit(t, e))
863
818
  };
864
819
  default: return {
865
820
  type: "unknown",
866
- tag: element.tag,
867
- value: source.slice(element.start, element.end)
821
+ tag: t.tag,
822
+ value: e.slice(t.start, t.end)
868
823
  };
869
824
  }
870
825
  }
871
826
  /** Attempt to decode an otherName [0] as a known type (currently only SRV-ID). */
872
- function parseOtherName(source, element) {
873
- const otherNameElements = childrenOf(source, element);
874
- if (otherNameElements.length !== 1) throw new Error("otherName must contain exactly one SEQUENCE");
875
- const otherNameChildren = childrenOf(source, requireElement(otherNameElements[0], "otherName sequence"));
876
- if (otherNameChildren.length !== 2) throw new Error("otherName must contain exactly type-id and value");
877
- const typeId = requireElement(otherNameChildren[0], "otherName type-id");
878
- const valueElement = requireElement(otherNameChildren[1], "otherName value");
879
- if (decodeObjectIdentifier(typeId.value) !== OIDS.idOnDnsSrv) return;
880
- if (valueElement.tag !== 160) throw new Error("SRV-ID otherName value must use explicit [0]");
881
- const valueChildren = childrenOf(source, valueElement);
882
- if (valueChildren.length !== 1) throw new Error("SRV-ID otherName value must contain exactly one IA5String");
883
- const srvNameElement = requireElement(valueChildren[0], "SRV-ID IA5String");
884
- if (srvNameElement.tag !== 22) throw new Error("SRV-ID otherName value must be an IA5String");
827
+ function parseOtherName(e, t) {
828
+ let n = i(e, t);
829
+ if (n.length !== 1) throw Error("otherName must contain exactly one SEQUENCE");
830
+ let r = i(e, f(n[0], "otherName sequence"));
831
+ if (r.length !== 2) throw Error("otherName must contain exactly type-id and value");
832
+ let a = f(r[0], "otherName type-id"), o = f(r[1], "otherName value");
833
+ if (c(a.value) !== m.idOnDnsSrv) return;
834
+ if (o.tag !== 160) throw Error("SRV-ID otherName value must use explicit [0]");
835
+ let s = i(e, o);
836
+ if (s.length !== 1) throw Error("SRV-ID otherName value must contain exactly one IA5String");
837
+ let u = f(s[0], "SRV-ID IA5String");
838
+ if (u.tag !== 22) throw Error("SRV-ID otherName value must be an IA5String");
885
839
  return {
886
840
  type: "srv",
887
- value: decodeString(srvNameElement.tag, srvNameElement.value)
841
+ value: l(u.tag, u.value)
888
842
  };
889
843
  }
890
844
  /** @internal Decode the Name Constraints extension value. */
891
- function parseNameConstraints(bytes) {
892
- const sequenceElement = requireElement(readRootElement(bytes, {
845
+ function parseNameConstraints(e) {
846
+ let t = f(n(e, {
893
847
  maxDepth: 64,
894
848
  allowOpaqueConstructedTags: [
895
849
  160,
@@ -897,96 +851,96 @@ function parseNameConstraints(bytes) {
897
851
  165
898
852
  ]
899
853
  }), "nameConstraints sequence");
900
- if (sequenceElement.tag !== 48) throw new Error("nameConstraints must use SEQUENCE");
901
- let permittedSubtrees;
902
- let excludedSubtrees;
903
- for (const child of childrenOf(bytes, sequenceElement)) if (child.tag === 160) {
904
- if (permittedSubtrees !== void 0) throw new Error("nameConstraints permittedSubtrees must not repeat");
905
- permittedSubtrees = parseGeneralSubtrees(bytes, child);
906
- } else if (child.tag === 161) {
907
- if (excludedSubtrees !== void 0) throw new Error("nameConstraints excludedSubtrees must not repeat");
908
- excludedSubtrees = parseGeneralSubtrees(bytes, child);
909
- } else throw new Error(`Unsupported nameConstraints field tag: ${String(child.tag)}`);
854
+ if (t.tag !== 48) throw Error("nameConstraints must use SEQUENCE");
855
+ let r, a;
856
+ for (let n of i(e, t)) if (n.tag === 160) {
857
+ if (r !== void 0) throw Error("nameConstraints permittedSubtrees must not repeat");
858
+ r = parseGeneralSubtrees(e, n);
859
+ } else if (n.tag === 161) {
860
+ if (a !== void 0) throw Error("nameConstraints excludedSubtrees must not repeat");
861
+ a = parseGeneralSubtrees(e, n);
862
+ } else throw Error(`Unsupported nameConstraints field tag: ${String(n.tag)}`);
910
863
  return {
911
- ...permittedSubtrees !== void 0 ? { permittedSubtrees } : {},
912
- ...excludedSubtrees !== void 0 ? { excludedSubtrees } : {}
864
+ ...r === void 0 ? {} : { permittedSubtrees: r },
865
+ ...a === void 0 ? {} : { excludedSubtrees: a }
913
866
  };
914
867
  }
915
868
  /** Decode a SEQUENCE OF GeneralSubtree from a permittedSubtrees or excludedSubtrees wrapper. */
916
- function parseGeneralSubtrees(source, container) {
917
- const subtrees = [];
918
- const subtreeElements = childrenOf(source, container);
919
- if (subtreeElements.length === 0) throw new Error("name constraints GeneralSubtrees must not be empty");
920
- for (const subtreeElement of subtreeElements) {
921
- if (subtreeElement.tag !== 48) throw new Error("name constraints GeneralSubtree must use SEQUENCE");
922
- const children = childrenOf(source, subtreeElement);
923
- const baseElement = children[0];
924
- if (baseElement === void 0) throw new Error("name constraints GeneralSubtree base is required");
925
- let sawMinimum = false;
926
- for (let i = 1; i < children.length; i += 1) {
927
- const child = children[i];
928
- if (child === void 0) continue;
929
- if (child.tag === 128) {
930
- if (sawMinimum) throw new Error("name constraints GeneralSubtree minimum must not repeat");
931
- sawMinimum = true;
932
- if (decodeNonNegativeIntegerNumber(child.value, "name constraints GeneralSubtree minimum") !== 0) throw new Error("name constraints GeneralSubtree minimum must be 0");
933
- } else if (child.tag === 129) throw new Error("name constraints GeneralSubtree maximum is not supported");
934
- else throw new Error(`Unsupported name constraints GeneralSubtree field tag: ${String(child.tag)}`);
869
+ function parseGeneralSubtrees(e, t) {
870
+ let n = [], r = i(e, t);
871
+ if (r.length === 0) throw Error("name constraints GeneralSubtrees must not be empty");
872
+ for (let t of r) {
873
+ let r = parseGeneralSubtree(e, t);
874
+ r !== void 0 && n.push({ base: r });
875
+ }
876
+ return n;
877
+ }
878
+ function parseGeneralSubtree(e, t) {
879
+ if (t.tag !== 48) throw Error("name constraints GeneralSubtree must use SEQUENCE");
880
+ let n = i(e, t), r = n[0];
881
+ if (r === void 0) throw Error("GeneralSubtree base is required");
882
+ return validateGeneralSubtreeBounds(n.slice(1)), parseNameConstraintGeneralName(e, r);
883
+ }
884
+ function validateGeneralSubtreeBounds(e) {
885
+ let t = !1;
886
+ for (let n of e) {
887
+ if (n.tag === 128) {
888
+ if (t) throw Error("name constraints GeneralSubtree minimum must not repeat");
889
+ if (t = !0, s(n.value, "name constraints GeneralSubtree minimum") !== 0) throw Error("name constraints GeneralSubtree minimum must be 0");
890
+ continue;
935
891
  }
936
- const form = parseNameConstraintGeneralName(source, baseElement);
937
- if (form !== void 0) subtrees.push({ base: form });
892
+ throw n.tag === 129 ? Error("name constraints GeneralSubtree maximum is not supported") : Error(`Unsupported name constraints GeneralSubtree field tag: ${String(n.tag)}`);
938
893
  }
939
- return subtrees;
940
894
  }
941
895
  /** Decode a GeneralName for use in name constraints (IP carries address+mask). */
942
- function parseNameConstraintGeneralName(source, element) {
943
- switch (element.tag) {
896
+ function parseNameConstraintGeneralName(e, t) {
897
+ switch (t.tag) {
944
898
  case 160: return {
945
899
  type: "otherName",
946
- value: new Uint8Array(element.value)
900
+ value: new Uint8Array(t.value)
947
901
  };
948
902
  case 129: return {
949
903
  type: "email",
950
- value: textDecoder.decode(element.value)
904
+ value: j.decode(t.value)
951
905
  };
952
906
  case 130: return {
953
907
  type: "dns",
954
- value: textDecoder.decode(element.value)
908
+ value: j.decode(t.value)
955
909
  };
956
910
  case 163: return {
957
911
  type: "x400Address",
958
- value: new Uint8Array(element.value)
912
+ value: new Uint8Array(t.value)
959
913
  };
960
914
  case 134: return {
961
915
  type: "uri",
962
- value: textDecoder.decode(element.value)
916
+ value: j.decode(t.value)
963
917
  };
964
918
  case 135:
965
- if (element.value.length === 8) return {
919
+ if (t.value.length === 8) return {
966
920
  type: "ip",
967
- addressBytes: element.value.slice(0, 4),
968
- maskBytes: element.value.slice(4, 8)
921
+ addressBytes: t.value.slice(0, 4),
922
+ maskBytes: t.value.slice(4, 8)
969
923
  };
970
- if (element.value.length === 32) return {
924
+ if (t.value.length === 32) return {
971
925
  type: "ip",
972
- addressBytes: element.value.slice(0, 16),
973
- maskBytes: element.value.slice(16, 32)
926
+ addressBytes: t.value.slice(0, 16),
927
+ maskBytes: t.value.slice(16, 32)
974
928
  };
975
- throw new Error(`Invalid IP name constraint: expected 8 (IPv4) or 32 (IPv6) bytes, got ${String(element.value.length)}`);
929
+ throw Error(`Invalid IP name constraint: expected 8 (IPv4) or 32 (IPv6) bytes, got ${String(t.value.length)}`);
976
930
  case 164: return {
977
931
  type: "directoryName",
978
- derHex: toHex(rebuildDirectoryNameFromImplicit(element, source))
932
+ derHex: p(rebuildDirectoryNameFromImplicit(t, e))
979
933
  };
980
934
  case 165: return {
981
935
  type: "ediPartyName",
982
- value: new Uint8Array(element.value)
936
+ value: new Uint8Array(t.value)
983
937
  };
984
938
  case 136: return {
985
939
  type: "registeredID",
986
- value: decodeObjectIdentifier(element.value)
940
+ value: c(t.value)
987
941
  };
988
942
  }
989
- throw new Error(`Unsupported name constraint GeneralName tag: ${String(element.tag)}`);
943
+ throw Error(`Unsupported name constraint GeneralName tag: ${String(t.tag)}`);
990
944
  }
991
945
  /**
992
946
  * Extracts the Name SEQUENCE from an implicitly-tagged directoryName [4].
@@ -995,96 +949,104 @@ function parseNameConstraintGeneralName(source, element) {
995
949
  * - Proper implicit: [4] replaces SEQUENCE tag, content is RDN SETs directly → wrap with 0x30
996
950
  * - Explicit-like: [4] wraps entire SEQUENCE, content starts with 0x30 → return content as-is
997
951
  */
998
- function rebuildDirectoryNameFromImplicit(element, _source) {
999
- if (element.value.length > 0 && element.value[0] === 48) return new Uint8Array(element.value);
1000
- const lengthEncoded = encodeLength(element.value.length);
1001
- const result = new Uint8Array(1 + lengthEncoded.length + element.value.length);
1002
- result[0] = 48;
1003
- result.set(lengthEncoded, 1);
1004
- result.set(element.value, 1 + lengthEncoded.length);
1005
- return result;
952
+ function rebuildDirectoryNameFromImplicit(t, n) {
953
+ if (t.value.length > 0 && t.value[0] === 48) return new Uint8Array(t.value);
954
+ let r = e(t.value.length), i = new Uint8Array(1 + r.length + t.value.length);
955
+ return i[0] = 48, i.set(r, 1), i.set(t.value, 1 + r.length), i;
1006
956
  }
1007
957
  /** Decode a DisplayText (UTF8String, IA5String, VisibleString, or BMPString). */
1008
- function parseDisplayText(element) {
1009
- switch (element.tag) {
958
+ function parseDisplayText(e) {
959
+ switch (e.tag) {
1010
960
  case 12:
1011
961
  case 22:
1012
- case 26: return textDecoder.decode(element.value);
1013
- case 30: return decodeBmpString(element.value);
1014
- default: throw new Error(`Unsupported DisplayText tag: ${element.tag}`);
962
+ case 26: return j.decode(e.value);
963
+ case 30: return decodeBmpString(e.value);
964
+ default: throw Error(`Unsupported DisplayText tag: ${e.tag}`);
1015
965
  }
1016
966
  }
1017
967
  /** Decode a BMPString (UCS-2 big-endian) to a JS string. */
1018
- function decodeBmpString(bytes) {
1019
- if (bytes.length % 2 !== 0) throw new Error("Invalid BMPString length");
1020
- let value = "";
1021
- for (let index = 0; index < bytes.length; index += 2) {
1022
- const left = bytes[index];
1023
- const right = bytes[index + 1];
1024
- if (left === void 0 || right === void 0) throw new Error("Invalid BMPString content");
1025
- value += String.fromCharCode(left << 8 | right);
968
+ function decodeBmpString(e) {
969
+ if (e.length % 2 != 0) throw Error("Invalid BMPString length");
970
+ let t = "";
971
+ for (let n = 0; n < e.length; n += 2) {
972
+ let r = e[n], i = e[n + 1];
973
+ if (r === void 0 || i === void 0) throw Error("Invalid BMPString content");
974
+ t += String.fromCharCode(r << 8 | i);
1026
975
  }
1027
- return value;
1028
- }
1029
- /** @internal Decode the Authority Key Identifier extension, returning the keyIdentifier hex or undefined. */
1030
- function parseAuthorityKeyIdentifier(bytes) {
1031
- const sequenceElement = requireElement(readElement(bytes, 0), "authorityKeyIdentifier sequence");
1032
- if (sequenceElement.end !== bytes.length) throw new Error("Trailing data after DER element");
1033
- let keyIdentifier;
1034
- let sawAuthorityCertIssuer = false;
1035
- let sawAuthorityCertSerialNumber = false;
1036
- let lastFieldOrder = -1;
1037
- let offset = sequenceElement.start;
1038
- while (offset < sequenceElement.end) {
1039
- const child = readElement(bytes, offset);
1040
- if (child.end > sequenceElement.end) throw new Error("DER child exceeds parent length");
1041
- if (child.tag === 128) {
1042
- if (keyIdentifier !== void 0) throw new Error("authorityKeyIdentifier keyIdentifier must not repeat");
1043
- if (lastFieldOrder >= 0) throw new Error("authorityKeyIdentifier fields must preserve DER order");
1044
- keyIdentifier = toHex(child.value);
1045
- lastFieldOrder = 0;
1046
- } else if (child.tag === 161) {
1047
- if (sawAuthorityCertIssuer) throw new Error("authorityKeyIdentifier authorityCertIssuer must not repeat");
1048
- if (lastFieldOrder >= 1) throw new Error("authorityKeyIdentifier fields must preserve DER order");
1049
- const issuerNames = childrenOf(bytes, child);
1050
- if (issuerNames.length === 0) throw new Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
1051
- for (const issuerName of issuerNames) {
1052
- if ((issuerName.tag & 192) !== 128) throw new Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
1053
- parseGeneralName(bytes, issuerName);
1054
- }
1055
- sawAuthorityCertIssuer = true;
1056
- lastFieldOrder = 1;
1057
- } else if (child.tag === 130) {
1058
- if (sawAuthorityCertSerialNumber) throw new Error("authorityKeyIdentifier authorityCertSerialNumber must not repeat");
1059
- if (lastFieldOrder >= 2 || !sawAuthorityCertIssuer) throw new Error("authorityKeyIdentifier fields must preserve DER order");
1060
- validateImplicitSerialNumberEncoding(child.value, "authorityKeyIdentifier authorityCertSerialNumber");
1061
- sawAuthorityCertSerialNumber = true;
1062
- lastFieldOrder = 2;
1063
- } else throw new Error(`Unsupported authorityKeyIdentifier field tag: ${String(child.tag)}`);
1064
- offset = child.end;
976
+ return t;
977
+ }
978
+ function parseAuthorityKeyIdentifier(e) {
979
+ let n = f(t(e, 0), "authorityKeyIdentifier sequence");
980
+ if (n.end !== e.length) throw Error("Trailing data after DER element");
981
+ let r = {
982
+ sawAuthorityCertIssuer: !1,
983
+ sawAuthorityCertSerialNumber: !1,
984
+ lastFieldOrder: -1
985
+ }, i = n.start;
986
+ for (; i < n.end;) {
987
+ let a = t(e, i);
988
+ if (a.end > n.end) throw Error("DER child exceeds parent length");
989
+ parseAuthorityKeyIdentifierField(e, a, r), i = a.end;
1065
990
  }
1066
- if (offset !== sequenceElement.end) throw new Error("Malformed DER sequence");
1067
- if (sawAuthorityCertIssuer !== sawAuthorityCertSerialNumber) throw new Error("authorityKeyIdentifier authorityCertIssuer and authorityCertSerialNumber must appear together");
1068
- return keyIdentifier;
1069
- }
1070
- function validateImplicitSerialNumberEncoding(bytes, label) {
1071
- const first = bytes[0];
1072
- if (first === void 0) throw new Error(`${label} must not be empty`);
1073
- if ((first & 128) !== 0) throw new Error(`${label} must be non-negative`);
1074
- if (bytes.length > 1 && first === 0 && ((bytes[1] ?? 0) & 128) === 0) throw new Error(`${label} must use minimal encoding`);
1075
- }
1076
- function validateImplicitBitStringContent(bytes, label) {
1077
- const unusedBits = bytes[0];
1078
- if (unusedBits === void 0 || unusedBits > 7) throw new Error(`${label} must use BIT STRING encoding`);
1079
- const bitStringBytes = bytes.slice(1);
1080
- if (bitStringBytes.length === 0) {
1081
- if (unusedBits !== 0) throw new Error(`${label} must use BIT STRING encoding`);
991
+ if (i !== n.end) throw Error("Malformed DER sequence");
992
+ if (r.sawAuthorityCertIssuer !== r.sawAuthorityCertSerialNumber) throw Error("authorityKeyIdentifier authorityCertIssuer and authorityCertSerialNumber must appear together");
993
+ return r.keyIdentifier;
994
+ }
995
+ function parseAuthorityKeyIdentifierField(e, t, n) {
996
+ switch (t.tag) {
997
+ case 128:
998
+ parseAuthorityKeyIdentifierKeyId(t, n);
999
+ return;
1000
+ case 161:
1001
+ parseAuthorityKeyIdentifierIssuer(e, t, n);
1002
+ return;
1003
+ case 130:
1004
+ parseAuthorityKeyIdentifierSerial(t, n);
1005
+ return;
1006
+ default: throw Error(`Unsupported authorityKeyIdentifier field tag: ${String(t.tag)}`);
1007
+ }
1008
+ }
1009
+ function parseAuthorityKeyIdentifierKeyId(e, t) {
1010
+ if (t.keyIdentifier !== void 0) throw Error("authorityKeyIdentifier keyIdentifier must not repeat");
1011
+ if (t.lastFieldOrder >= 0) throw Error("authorityKeyIdentifier fields must preserve DER order");
1012
+ t.keyIdentifier = p(e.value), t.lastFieldOrder = 0;
1013
+ }
1014
+ function parseAuthorityKeyIdentifierIssuer(e, t, n) {
1015
+ if (n.sawAuthorityCertIssuer) throw Error("authorityKeyIdentifier authorityCertIssuer must not repeat");
1016
+ if (n.lastFieldOrder >= 1) throw Error("authorityKeyIdentifier fields must preserve DER order");
1017
+ let r = i(e, t);
1018
+ if (r.length === 0) throw Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
1019
+ for (let t of r) {
1020
+ if ((t.tag & 192) != 128) throw Error("authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries");
1021
+ parseGeneralName(e, t);
1022
+ }
1023
+ n.sawAuthorityCertIssuer = !0, n.lastFieldOrder = 1;
1024
+ }
1025
+ function parseAuthorityKeyIdentifierSerial(e, t) {
1026
+ if (t.sawAuthorityCertSerialNumber) throw Error("authorityKeyIdentifier authorityCertSerialNumber must not repeat");
1027
+ if (t.lastFieldOrder >= 2 || !t.sawAuthorityCertIssuer) throw Error("authorityKeyIdentifier fields must preserve DER order");
1028
+ validateImplicitSerialNumberEncoding(e.value, "authorityKeyIdentifier authorityCertSerialNumber"), t.sawAuthorityCertSerialNumber = !0, t.lastFieldOrder = 2;
1029
+ }
1030
+ function validateImplicitSerialNumberEncoding(e, t) {
1031
+ let n = e[0];
1032
+ if (n === void 0) throw Error(`${t} must not be empty`);
1033
+ if (n & 128) throw Error(`${t} must be non-negative`);
1034
+ if (e.length > 1 && n === 0 && !((e[1] ?? 0) & 128)) throw Error(`${t} must use minimal encoding`);
1035
+ }
1036
+ function validateImplicitBitStringContent(e, t) {
1037
+ let n = e[0];
1038
+ if (n === void 0 || n > 7) throw Error(`${t} must use BIT STRING encoding`);
1039
+ let r = e.slice(1);
1040
+ if (r.length === 0) {
1041
+ if (n !== 0) throw Error(`${t} must use BIT STRING encoding`);
1082
1042
  return;
1083
1043
  }
1084
- if (unusedBits === 0) return;
1085
- const lastByte = bitStringBytes[bitStringBytes.length - 1];
1086
- if (lastByte === void 0) throw new Error(`${label} must use BIT STRING encoding`);
1087
- if ((lastByte & (1 << unusedBits) - 1) !== 0) throw new Error(`${label} BIT STRING must not set padding bits`);
1044
+ if (n === 0) return;
1045
+ let i = r[r.length - 1];
1046
+ if (i === void 0) throw Error(`${t} must use BIT STRING encoding`);
1047
+ if (i & (1 << n) - 1) throw Error(`${t} BIT STRING must not set padding bits`);
1088
1048
  }
1089
1049
  //#endregion
1090
1050
  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 };
1051
+
1052
+ //# sourceMappingURL=parse.js.map