micro509 0.13.0 → 0.15.0

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