micro509 0.1.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 (115) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +220 -0
  3. package/dist/index.d.ts +21 -0
  4. package/dist/index.js +1 -0
  5. package/dist/internal/asn1/asn1.js +2 -0
  6. package/dist/internal/asn1/asn1.js.map +1 -0
  7. package/dist/internal/asn1/der.js +2 -0
  8. package/dist/internal/asn1/der.js.map +1 -0
  9. package/dist/internal/asn1/oids.js +2 -0
  10. package/dist/internal/asn1/oids.js.map +1 -0
  11. package/dist/internal/crypto/algorithm-names.js +2 -0
  12. package/dist/internal/crypto/algorithm-names.js.map +1 -0
  13. package/dist/internal/crypto/ecdsa.js +2 -0
  14. package/dist/internal/crypto/ecdsa.js.map +1 -0
  15. package/dist/internal/crypto/hash.js +2 -0
  16. package/dist/internal/crypto/hash.js.map +1 -0
  17. package/dist/internal/crypto/pbes2.d.ts +23 -0
  18. package/dist/internal/crypto/pbes2.js +2 -0
  19. package/dist/internal/crypto/pbes2.js.map +1 -0
  20. package/dist/internal/crypto/rsa-pss.js +2 -0
  21. package/dist/internal/crypto/rsa-pss.js.map +1 -0
  22. package/dist/internal/crypto/sig-verify.js +2 -0
  23. package/dist/internal/crypto/sig-verify.js.map +1 -0
  24. package/dist/internal/crypto/signing.d.ts +16 -0
  25. package/dist/internal/crypto/signing.js +2 -0
  26. package/dist/internal/crypto/signing.js.map +1 -0
  27. package/dist/internal/crypto/webcrypto.js +2 -0
  28. package/dist/internal/crypto/webcrypto.js.map +1 -0
  29. package/dist/internal/shared/base64.js +2 -0
  30. package/dist/internal/shared/base64.js.map +1 -0
  31. package/dist/internal/shared/dn.js +2 -0
  32. package/dist/internal/shared/dn.js.map +1 -0
  33. package/dist/internal/shared/ip.js +2 -0
  34. package/dist/internal/shared/ip.js.map +1 -0
  35. package/dist/internal/verify/name-constraints-engine.js +2 -0
  36. package/dist/internal/verify/name-constraints-engine.js.map +1 -0
  37. package/dist/internal/verify/policy-engine.js +2 -0
  38. package/dist/internal/verify/policy-engine.js.map +1 -0
  39. package/dist/internal/verify/verify-path.js +2 -0
  40. package/dist/internal/verify/verify-path.js.map +1 -0
  41. package/dist/internal/x509/extension-bits.d.ts +18 -0
  42. package/dist/internal/x509/extension-bits.js +2 -0
  43. package/dist/internal/x509/extension-bits.js.map +1 -0
  44. package/dist/internal/x509/extension-registry.js +2 -0
  45. package/dist/internal/x509/extension-registry.js.map +1 -0
  46. package/dist/internal/x509/name-fields.js +2 -0
  47. package/dist/internal/x509/name-fields.js.map +1 -0
  48. package/dist/keys/keys.d.ts +431 -0
  49. package/dist/keys/keys.js +5 -0
  50. package/dist/keys/keys.js.map +1 -0
  51. package/dist/keys.d.ts +3 -0
  52. package/dist/keys.js +1 -0
  53. package/dist/pem/pem.d.ts +56 -0
  54. package/dist/pem/pem.js +6 -0
  55. package/dist/pem/pem.js.map +1 -0
  56. package/dist/pem.d.ts +2 -0
  57. package/dist/pem.js +1 -0
  58. package/dist/pkcs/pfx.d.ts +177 -0
  59. package/dist/pkcs/pfx.js +2 -0
  60. package/dist/pkcs/pfx.js.map +1 -0
  61. package/dist/pkcs/pkcs12-mac.d.ts +41 -0
  62. package/dist/pkcs/pkcs12-mac.js +2 -0
  63. package/dist/pkcs/pkcs12-mac.js.map +1 -0
  64. package/dist/pkcs/pkcs7.d.ts +131 -0
  65. package/dist/pkcs/pkcs7.js +2 -0
  66. package/dist/pkcs/pkcs7.js.map +1 -0
  67. package/dist/pkcs.d.ts +5 -0
  68. package/dist/pkcs.js +1 -0
  69. package/dist/result/result.d.ts +68 -0
  70. package/dist/result/result.js +2 -0
  71. package/dist/result/result.js.map +1 -0
  72. package/dist/result.d.ts +2 -0
  73. package/dist/result.js +1 -0
  74. package/dist/revocation/chain.d.ts +180 -0
  75. package/dist/revocation/chain.js +2 -0
  76. package/dist/revocation/chain.js.map +1 -0
  77. package/dist/revocation/crl.d.ts +316 -0
  78. package/dist/revocation/crl.js +2 -0
  79. package/dist/revocation/crl.js.map +1 -0
  80. package/dist/revocation/ocsp.d.ts +332 -0
  81. package/dist/revocation/ocsp.js +2 -0
  82. package/dist/revocation/ocsp.js.map +1 -0
  83. package/dist/revocation/revocation.d.ts +168 -0
  84. package/dist/revocation/revocation.js +2 -0
  85. package/dist/revocation/revocation.js.map +1 -0
  86. package/dist/revocation.d.ts +5 -0
  87. package/dist/revocation.js +1 -0
  88. package/dist/verify/identity.d.ts +129 -0
  89. package/dist/verify/identity.js +2 -0
  90. package/dist/verify/identity.js.map +1 -0
  91. package/dist/verify/name-constraints.d.ts +18 -0
  92. package/dist/verify/policy.d.ts +39 -0
  93. package/dist/verify/verify.d.ts +404 -0
  94. package/dist/verify/verify.js +2 -0
  95. package/dist/verify/verify.js.map +1 -0
  96. package/dist/verify.d.ts +5 -0
  97. package/dist/verify.js +1 -0
  98. package/dist/x509/certificate.d.ts +191 -0
  99. package/dist/x509/certificate.js +2 -0
  100. package/dist/x509/certificate.js.map +1 -0
  101. package/dist/x509/csr.d.ts +55 -0
  102. package/dist/x509/csr.js +2 -0
  103. package/dist/x509/csr.js.map +1 -0
  104. package/dist/x509/extensions.d.ts +550 -0
  105. package/dist/x509/extensions.js +2 -0
  106. package/dist/x509/extensions.js.map +1 -0
  107. package/dist/x509/name.d.ts +140 -0
  108. package/dist/x509/name.js +2 -0
  109. package/dist/x509/name.js.map +1 -0
  110. package/dist/x509/parse.d.ts +377 -0
  111. package/dist/x509/parse.js +2 -0
  112. package/dist/x509/parse.js.map +1 -0
  113. package/dist/x509.d.ts +8 -0
  114. package/dist/x509.js +1 -0
  115. package/package.json +153 -0
@@ -0,0 +1,377 @@
1
+ import { NameFieldKey } from "./name.js";
2
+ import { AuthorityInformationAccess, BasicConstraints, CertificatePolicies, DistributionPointReason, ExtendedKeyUsage, GeneralName, GeneralSubtree, InhibitAnyPolicy, KeyUsage, NameConstraints, ParsedNameConstraintForm, PolicyConstraints, PolicyMappings, SubjectAltName } from "./extensions.js";
3
+ import { ParsedBitFlags } from "../internal/x509/extension-bits.js";
4
+
5
+ //#region src/x509/parse.d.ts
6
+ /**
7
+ * A single decoded name attribute from an X.501 RelativeDistinguishedName.
8
+ *
9
+ * RFC 5280 / X.501 call this structure an `AttributeTypeAndValue`.
10
+ *
11
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}
12
+ */
13
+ interface ParsedNameAttribute {
14
+ /** Dotted-decimal OID of the attribute type (e.g. `"2.5.4.3"` for CN). */
15
+ readonly oid: string;
16
+ /** Friendly key when the OID maps to a well-known field (CN, O, etc.). */
17
+ readonly key?: NameFieldKey;
18
+ /** ASN.1 tag of the value encoding (UTF8String = 0x0c, PrintableString = 0x13, etc.). */
19
+ readonly valueTag: number;
20
+ /** Decoded string content of the attribute value. */
21
+ readonly value: string;
22
+ }
23
+ /**
24
+ * An X.501 Distinguished Name decoded from an issuer or subject field.
25
+ *
26
+ * Provides three views of the same data: ordered RDNs, a flat attribute
27
+ * list, and a convenience key-value map for well-known fields.
28
+ */
29
+ interface ParsedName {
30
+ /** Hex-encoded DER of the complete Name SEQUENCE, usable for byte-exact comparisons. */
31
+ readonly derHex: string;
32
+ /** Ordered list of RelativeDistinguishedNames, preserving multi-valued RDN structure. */
33
+ readonly rdns: readonly ParsedRelativeDistinguishedName[];
34
+ /** Flat list of every attribute across all RDNs, in encounter order. */
35
+ readonly attributes: readonly ParsedNameAttribute[];
36
+ /** First-occurrence map of well-known fields (CN, O, OU, etc.) for quick lookups. */
37
+ readonly values: Partial<Record<NameFieldKey, string>>;
38
+ }
39
+ /** A single RelativeDistinguishedName SET from an X.501 Name. */
40
+ interface ParsedRelativeDistinguishedName {
41
+ /** Hex-encoded DER of this RDN SET element. */
42
+ readonly derHex: string;
43
+ /** Attributes within this RDN (usually one, but multi-valued RDNs are legal). */
44
+ readonly attributes: readonly ParsedNameAttribute[];
45
+ /** First-occurrence map of well-known fields within this RDN. */
46
+ readonly values: Partial<Record<NameFieldKey, string>>;
47
+ }
48
+ /**
49
+ * The name component of a CRL Distribution Point (RFC 5280 §4.2.1.13).
50
+ * Exactly one of `fullName` or `relativeName` will be present.
51
+ */
52
+ interface ParsedDistributionPointName {
53
+ /** Absolute GeneralName(s) identifying the distribution point. */
54
+ readonly fullName?: readonly GeneralName[];
55
+ /** Name relative to the CRL issuer's distinguished name. */
56
+ readonly relativeName?: ParsedRelativeDistinguishedName;
57
+ }
58
+ /** A decoded DistributionPoint from the CRL Distribution Points extension. */
59
+ interface ParsedDistributionPoint {
60
+ /** Where to fetch the CRL — a fullName URI or relativeName. */
61
+ readonly distributionPoint?: ParsedDistributionPointName;
62
+ /** Revocation reason subset this distribution point covers. Absent means all reasons. */
63
+ readonly reasons?: ParsedBitFlags<DistributionPointReason>;
64
+ /** Entity that signed the CRL, when different from the certificate issuer. */
65
+ readonly crlIssuer?: readonly GeneralName[];
66
+ }
67
+ /**
68
+ * Decoded Issuing Distribution Point CRL extension (RFC 5280 §5.2.5).
69
+ * Constrains which certificates a CRL covers (scope, reasons, indirection).
70
+ */
71
+ interface ParsedIssuingDistributionPoint {
72
+ /** Where to fetch this CRL, if specified. */
73
+ readonly distributionPoint?: ParsedDistributionPointName;
74
+ /** When true, this CRL only covers end-entity certificates. Default false. */
75
+ readonly onlyContainsUserCerts?: boolean;
76
+ /** When true, this CRL only covers CA certificates. Default false. */
77
+ readonly onlyContainsCACerts?: boolean;
78
+ /** Limits the CRL to these revocation reasons. Absent means all reasons. */
79
+ readonly onlySomeReasons?: ParsedBitFlags<DistributionPointReason>;
80
+ /** When true, this CRL may contain entries from CAs other than the issuer. Default false. */
81
+ readonly indirectCrl?: boolean;
82
+ /** When true, this CRL only covers attribute certificates. Default false. */
83
+ readonly onlyContainsAttributeCerts?: boolean;
84
+ }
85
+ /** A raw X.509v3 extension before type-specific decoding. */
86
+ interface ParsedExtension {
87
+ /** Dotted-decimal OID identifying this extension. */
88
+ readonly oid: string;
89
+ /** Whether a validator MUST reject the certificate if it cannot process this extension. */
90
+ readonly critical: boolean;
91
+ /** DER-encoded OCTET STRING payload (extnValue). */
92
+ readonly valueDer: Uint8Array;
93
+ /** Hex-encoded form of `valueDer` for display and comparison. */
94
+ readonly valueHex: string;
95
+ }
96
+ /**
97
+ * User-supplied decoder for a single extension OID.
98
+ *
99
+ * Register with {@linkcode ParseOptions.decoders} or {@linkcode ParseOptions.decoderMap}
100
+ * to decode custom extensions during parsing.
101
+ */
102
+ interface ExtensionDecoder<TValue> {
103
+ /** OID this decoder handles. */
104
+ readonly oid: string;
105
+ /** Decode the raw {@linkcode ParsedExtension} into a typed value. */
106
+ decode(extension: ParsedExtension): TValue;
107
+ }
108
+ /**
109
+ * Identity helper that narrows the type of a custom {@linkcode ExtensionDecoder} literal.
110
+ *
111
+ * @param decoder Decoder definition to return unchanged.
112
+ * @returns The same decoder, properly typed.
113
+ */
114
+ declare function defineExtensionDecoder<TValue>(decoder: ExtensionDecoder<TValue>): ExtensionDecoder<TValue>;
115
+ /**
116
+ * Identity helper that narrows the type of a custom {@linkcode ExtensionDecoderMap} literal.
117
+ *
118
+ * @param decoderMap Map of named decoders to return unchanged.
119
+ * @returns The same map, properly typed.
120
+ */
121
+ declare function defineExtensionDecoderMap<TMap extends ExtensionDecoderMap>(decoderMap: TMap): TMap;
122
+ /** String-keyed map of {@linkcode ExtensionDecoder}s, used with {@linkcode ParseOptions.decoderMap}. */
123
+ type ExtensionDecoderMap = Record<string, ExtensionDecoder<unknown>>;
124
+ /** Inferred result type when decoding extensions via an {@linkcode ExtensionDecoderMap}. */
125
+ type DecodedExtensionMap<TMap extends ExtensionDecoderMap> = { [TKey in keyof TMap]?: TMap[TKey] extends ExtensionDecoder<infer TValue> ? DecodedExtensionValue<TValue> : never };
126
+ /** A successfully decoded extension value paired with its OID and criticality. */
127
+ interface DecodedExtensionValue<TValue> {
128
+ /** Dotted-decimal OID of the decoded extension. */
129
+ readonly oid: string;
130
+ /** Whether the extension was marked critical in the certificate. */
131
+ readonly critical: boolean;
132
+ /** Typed value produced by the {@linkcode ExtensionDecoder}. */
133
+ readonly value: TValue;
134
+ }
135
+ /**
136
+ * Options for {@linkcode parseCertificateDer}, {@linkcode parseCertificatePem},
137
+ * and CSR parse functions.
138
+ *
139
+ * Supply custom extension decoders to have their results included in the parsed output alongside
140
+ * the built-in extensions.
141
+ */
142
+ interface ParseOptions<TMap extends ExtensionDecoderMap = Record<never, never>> {
143
+ /** Array of decoders; decoded values appear in `decodedExtensions`. */
144
+ readonly decoders?: readonly ExtensionDecoder<unknown>[];
145
+ /** Named decoder map; decoded values appear in `decodedExtensionMap` keyed by map key. */
146
+ readonly decoderMap?: TMap;
147
+ }
148
+ /**
149
+ * A fully decoded X.509 certificate.
150
+ *
151
+ * Built-in extensions (basicConstraints, keyUsage, etc.) are decoded into
152
+ * typed fields automatically.\
153
+ * Supply {@linkcode ParseOptions} to also decode custom extensions.
154
+ */
155
+ interface ParsedCertificate<TMap extends ExtensionDecoderMap = Record<never, never>> {
156
+ /** Complete DER encoding of the certificate (copied from the input). */
157
+ readonly der: Uint8Array;
158
+ /** X.509 version number (1, 2, or 3). Almost always 3. */
159
+ readonly version: number;
160
+ /** Hex-encoded serial number assigned by the issuing CA. */
161
+ readonly serialNumberHex: string;
162
+ /** DER encoding of the TBSCertificate, used for signature verification. */
163
+ readonly tbsCertificateDer: Uint8Array;
164
+ /** DER encoding of the SubjectPublicKeyInfo, used for key import. */
165
+ readonly subjectPublicKeyInfoDer: Uint8Array;
166
+ /** Raw signature bytes (BIT STRING content, padding removed). */
167
+ readonly signatureValue: Uint8Array;
168
+ /** Distinguished name of the certificate issuer. */
169
+ readonly issuer: ParsedName;
170
+ /** Distinguished name of the certificate subject. */
171
+ readonly subject: ParsedName;
172
+ /** Start of the certificate validity period. */
173
+ readonly notBefore: Date;
174
+ /** End of the certificate validity period. */
175
+ readonly notAfter: Date;
176
+ /** OID of the algorithm used to sign this certificate (e.g. `"1.2.840.113549.1.1.11"` for SHA-256 with RSA). */
177
+ readonly signatureAlgorithmOid: string;
178
+ /** Human-readable signature algorithm name (e.g. `"ECDSA with SHA-256"`). */
179
+ readonly signatureAlgorithmName: string;
180
+ /** DER-encoded parameters for the signature algorithm. Absent for algorithms with no parameters. */
181
+ readonly signatureAlgorithmParametersDer?: Uint8Array;
182
+ /** OID of the subject's public key algorithm (e.g. `"1.2.840.10045.2.1"` for EC). */
183
+ readonly publicKeyAlgorithmOid: string;
184
+ /** Human-readable public key algorithm name (e.g. `"EC P-256"`). */
185
+ readonly publicKeyAlgorithmName: string;
186
+ /** DER-encoded parameters for the public key algorithm. Absent when implicit. */
187
+ readonly publicKeyAlgorithmParametersDer?: Uint8Array;
188
+ /** OID of the named curve or other key sub-parameter, when present. */
189
+ readonly publicKeyParametersOid?: string;
190
+ /** All extensions as raw {@linkcode ParsedExtension}s, in certificate order. */
191
+ readonly extensions: readonly ParsedExtension[];
192
+ /** Decoded Basic Constraints (RFC 5280 §4.2.1.9). */
193
+ readonly basicConstraints?: BasicConstraints;
194
+ /** Decoded Key Usage bit flags (RFC 5280 §4.2.1.3). */
195
+ readonly keyUsage?: ParsedBitFlags<KeyUsage>;
196
+ /** Decoded Extended Key Usage purposes (RFC 5280 §4.2.1.12). */
197
+ readonly extendedKeyUsage?: readonly ExtendedKeyUsage[];
198
+ /** Decoded Subject Alternative Names (RFC 5280 §4.2.1.6). */
199
+ readonly subjectAltNames?: readonly SubjectAltName[];
200
+ /** Decoded Name Constraints (RFC 5280 §4.2.1.10). */
201
+ readonly nameConstraints?: NameConstraints<ParsedNameConstraintForm>;
202
+ /** Decoded Certificate Policies (RFC 5280 §4.2.1.4). */
203
+ readonly certificatePolicies?: CertificatePolicies;
204
+ /** Decoded Policy Mappings (RFC 5280 §4.2.1.5). */
205
+ readonly policyMappings?: PolicyMappings;
206
+ /** Decoded Policy Constraints (RFC 5280 §4.2.1.11). */
207
+ readonly policyConstraints?: PolicyConstraints;
208
+ /** Decoded Inhibit anyPolicy (RFC 5280 §4.2.1.14). */
209
+ readonly inhibitAnyPolicy?: InhibitAnyPolicy;
210
+ /** Decoded Authority Information Access — OCSP and CA Issuer URIs (RFC 5280 §4.2.2.1). */
211
+ readonly authorityInfoAccess?: readonly AuthorityInformationAccess[];
212
+ /** Decoded CRL Distribution Points (RFC 5280 §4.2.1.13). */
213
+ readonly crlDistributionPoints?: readonly ParsedDistributionPoint[];
214
+ /** Custom-decoded extensions from {@linkcode ParseOptions.decoders}. */
215
+ readonly decodedExtensions?: readonly DecodedExtensionValue<unknown>[];
216
+ /** Custom-decoded extensions from {@linkcode ParseOptions.decoderMap}, keyed by map key. */
217
+ readonly decodedExtensionMap?: DecodedExtensionMap<TMap>;
218
+ /** Hex-encoded Subject Key Identifier (RFC 5280 §4.2.1.2). */
219
+ readonly subjectKeyIdentifier?: string;
220
+ /** Hex-encoded Authority Key Identifier (RFC 5280 §4.2.1.1). */
221
+ readonly authorityKeyIdentifier?: string;
222
+ }
223
+ /**
224
+ * A fully decoded PKCS#10 Certificate Signing Request.
225
+ *
226
+ * Extension fields mirror {@linkcode ParsedCertificate} but come from the
227
+ * CSR's extensionRequest attribute rather than the v3 extensions block.
228
+ */
229
+ interface ParsedCertificateSigningRequest<TMap extends ExtensionDecoderMap = Record<never, never>> {
230
+ /** PKCS#10 version number (always 1). */
231
+ readonly version: number;
232
+ /** DER encoding of the CertificationRequestInfo, used for signature verification. */
233
+ readonly certificationRequestInfoDer: Uint8Array;
234
+ /** DER encoding of the SubjectPublicKeyInfo. */
235
+ readonly subjectPublicKeyInfoDer: Uint8Array;
236
+ /** Raw signature bytes (BIT STRING content, padding removed). */
237
+ readonly signatureValue: Uint8Array;
238
+ /** Distinguished name the requester wants on the certificate. */
239
+ readonly subject: ParsedName;
240
+ /** OID of the algorithm used to sign this CSR. */
241
+ readonly signatureAlgorithmOid: string;
242
+ /** Human-readable signature algorithm name (e.g. `"ECDSA with SHA-256"`). */
243
+ readonly signatureAlgorithmName: string;
244
+ /** DER-encoded parameters for the signature algorithm. Absent for algorithms with no parameters. */
245
+ readonly signatureAlgorithmParametersDer?: Uint8Array;
246
+ /** OID of the subject's public key algorithm. */
247
+ readonly publicKeyAlgorithmOid: string;
248
+ /** Human-readable public key algorithm name (e.g. `"EC P-256"`). */
249
+ readonly publicKeyAlgorithmName: string;
250
+ /** DER-encoded parameters for the public key algorithm. */
251
+ readonly publicKeyAlgorithmParametersDer?: Uint8Array;
252
+ /** OID of the named curve or other key sub-parameter, when present. */
253
+ readonly publicKeyParametersOid?: string;
254
+ /** All requested extensions as raw {@linkcode ParsedExtension}s. */
255
+ readonly requestedExtensions: readonly ParsedExtension[];
256
+ /** Decoded Basic Constraints from the extensionRequest attribute. */
257
+ readonly basicConstraints?: BasicConstraints;
258
+ /** Decoded Key Usage from the extensionRequest attribute. */
259
+ readonly keyUsage?: ParsedBitFlags<KeyUsage>;
260
+ /** Decoded Extended Key Usage from the extensionRequest attribute. */
261
+ readonly extendedKeyUsage?: readonly ExtendedKeyUsage[];
262
+ /** Decoded Subject Alternative Names from the extensionRequest attribute. */
263
+ readonly subjectAltNames?: readonly SubjectAltName[];
264
+ /** Decoded Name Constraints from the extensionRequest attribute. */
265
+ readonly nameConstraints?: NameConstraints<ParsedNameConstraintForm>;
266
+ /** Decoded Certificate Policies from the extensionRequest attribute. */
267
+ readonly certificatePolicies?: CertificatePolicies;
268
+ /** Decoded Policy Mappings from the extensionRequest attribute. */
269
+ readonly policyMappings?: PolicyMappings;
270
+ /** Decoded Policy Constraints from the extensionRequest attribute. */
271
+ readonly policyConstraints?: PolicyConstraints;
272
+ /** Decoded Inhibit anyPolicy from the extensionRequest attribute. */
273
+ readonly inhibitAnyPolicy?: InhibitAnyPolicy;
274
+ /** Decoded Authority Information Access from the extensionRequest attribute. */
275
+ readonly authorityInfoAccess?: readonly AuthorityInformationAccess[];
276
+ /** Decoded CRL Distribution Points from the extensionRequest attribute. */
277
+ readonly crlDistributionPoints?: readonly ParsedDistributionPoint[];
278
+ /** Custom-decoded extensions from {@linkcode ParseOptions.decoders}. */
279
+ readonly decodedExtensions?: readonly DecodedExtensionValue<unknown>[];
280
+ /** Custom-decoded extensions from {@linkcode ParseOptions.decoderMap}. */
281
+ readonly decodedExtensionMap?: DecodedExtensionMap<TMap>;
282
+ }
283
+ /**
284
+ * Decode a DER-encoded X.509 certificate into a {@linkcode ParsedCertificate}.
285
+ *
286
+ * All built-in extensions (basicConstraints, keyUsage, subjectAltNames, etc.)
287
+ * are decoded automatically.\
288
+ * Pass {@linkcode ParseOptions} to also decode custom extensions.
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * import { parseCertificateDer } from 'micro509';
293
+ *
294
+ * const cert = parseCertificateDer(derBytes);
295
+ * console.log(cert.subject.values.commonName); // "example.com"
296
+ * console.log(cert.keyUsage); // ["digitalSignature", "keyEncipherment"]
297
+ * ```
298
+ *
299
+ * @param der Raw DER bytes of an X.509 certificate.
300
+ * @param options Custom extension decoders to apply during parsing.
301
+ */
302
+ declare function parseCertificateDer<TMap extends ExtensionDecoderMap = Record<never, never>>(der: Uint8Array, options?: ParseOptions<TMap>): ParsedCertificate<TMap>;
303
+ /**
304
+ * Decode a PEM-encoded X.509 certificate into a {@linkcode ParsedCertificate}.
305
+ *
306
+ * Expects a single `-----BEGIN CERTIFICATE-----` block. For bundles
307
+ * containing multiple certificates, use {@linkcode parseCertificateChainPem}.
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * import { parseCertificatePem } from 'micro509';
312
+ *
313
+ * const cert = parseCertificatePem(pemString);
314
+ * console.log(cert.issuer.values.organization); // "Let's Encrypt"
315
+ * console.log(cert.notAfter); // Date
316
+ * ```
317
+ *
318
+ * @param pem PEM string with a CERTIFICATE block.
319
+ * @param options Custom extension decoders to apply during parsing.
320
+ */
321
+ declare function parseCertificatePem<TMap extends ExtensionDecoderMap = Record<never, never>>(pem: string, options?: ParseOptions<TMap>): ParsedCertificate<TMap>;
322
+ /**
323
+ * Decode a PEM bundle containing one or more certificates.
324
+ *
325
+ * Non-CERTIFICATE blocks (e.g. private keys) are silently skipped.
326
+ *
327
+ * @param pemBundle PEM text that may contain multiple CERTIFICATE blocks.
328
+ * @param options Custom extension decoders to apply during parsing.
329
+ */
330
+ declare function parseCertificateChainPem<TMap extends ExtensionDecoderMap = Record<never, never>>(pemBundle: string, options?: ParseOptions<TMap>): readonly ParsedCertificate<TMap>[];
331
+ /**
332
+ * Decode a DER-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
333
+ *
334
+ * @param der Raw DER bytes of a PKCS#10 certificate signing request.
335
+ * @param options Custom extension decoders to apply during parsing.
336
+ */
337
+ declare function parseCertificateSigningRequestDer<TMap extends ExtensionDecoderMap = Record<never, never>>(der: Uint8Array, options?: ParseOptions<TMap>): ParsedCertificateSigningRequest<TMap>;
338
+ /**
339
+ * Decode a PEM-encoded PKCS#10 CSR into a {@linkcode ParsedCertificateSigningRequest}.
340
+ *
341
+ * @param pem PEM string with a CERTIFICATE REQUEST block.
342
+ * @param options Custom extension decoders to apply during parsing.
343
+ */
344
+ declare function parseCertificateSigningRequestPem<TMap extends ExtensionDecoderMap = Record<never, never>>(pem: string, options?: ParseOptions<TMap>): ParsedCertificateSigningRequest<TMap>;
345
+ /**
346
+ * Find a raw extension by OID within a parsed extension list.
347
+ *
348
+ * @param extensions Extension list from a {@linkcode ParsedCertificate} or CSR.
349
+ * @param oid Dotted-decimal OID to look up.
350
+ * @returns The matching extension, or `undefined` if not present.
351
+ */
352
+ declare function findExtension(extensions: readonly ParsedExtension[], oid: string): ParsedExtension | undefined;
353
+ /**
354
+ * Decode a single extension using a custom {@linkcode ExtensionDecoder}.
355
+ *
356
+ * @param extensions Extension list to search.
357
+ * @param decoder Decoder whose OID will be matched.
358
+ * @returns The decoded value, or `undefined` if the extension is absent.
359
+ */
360
+ declare function decodeExtension<TValue>(extensions: readonly ParsedExtension[], decoder: ExtensionDecoder<TValue>): TValue | undefined;
361
+ /**
362
+ * Decode all matching extensions using an array of {@linkcode ExtensionDecoder}s.
363
+ *
364
+ * @param extensions Extension list to search.
365
+ * @param decoders Decoders to apply. Only matching OIDs produce output.
366
+ */
367
+ declare function decodeExtensions(extensions: readonly ParsedExtension[], decoders: readonly ExtensionDecoder<unknown>[]): readonly DecodedExtensionValue<unknown>[];
368
+ /**
369
+ * Decode all matching extensions using a named {@linkcode ExtensionDecoderMap}.
370
+ *
371
+ * @param extensions Extension list to search.
372
+ * @param decoderMap Named decoders. Results are keyed by the same map keys.
373
+ */
374
+ declare function decodeExtensionMap<TMap extends ExtensionDecoderMap>(extensions: readonly ParsedExtension[], decoderMap: TMap): DecodedExtensionMap<TMap>;
375
+ //#endregion
376
+ export { DecodedExtensionMap, DecodedExtensionValue, ExtensionDecoder, ExtensionDecoderMap, ParseOptions, type ParsedBitFlags, ParsedCertificate, ParsedCertificateSigningRequest, ParsedDistributionPoint, ParsedDistributionPointName, ParsedExtension, ParsedIssuingDistributionPoint, ParsedName, ParsedNameAttribute, ParsedRelativeDistinguishedName, decodeExtension, decodeExtensionMap, decodeExtensions, defineExtensionDecoder, defineExtensionDecoderMap, findExtension, parseCertificateChainPem, parseCertificateDer, parseCertificatePem, parseCertificateSigningRequestDer, parseCertificateSigningRequestPem };
377
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1,2 @@
1
+ import{encodeLength as e,readElement as t,readRootElement as n,readSequenceChildren as r}from"../internal/asn1/der.js";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";import{OIDS as m}from"../internal/asn1/oids.js";import{pemDecode as h,splitPemBlocks as g}from"../pem/pem.js";import{describePublicKeyAlgorithm as _,describeSignatureAlgorithm as v}from"../internal/crypto/algorithm-names.js";import{decodeIpAddress as y}from"../internal/shared/ip.js";import{parseDistributionPointReasonFlagsContent as b,parseKeyUsageExtension as x}from"../internal/x509/extension-bits.js";import{nameFieldKeyFromOid as S}from"../internal/x509/name-fields.js";import{parseAuthorityInfoAccessMethodOid as C,parseExtendedKeyUsageOid as w}from"./extensions.js";import{decodeAndApplyKnownExtension as T}from"../internal/x509/extension-registry.js";const E=new TextDecoder;function D(e){return e}function O(e){return e}function k(e,t){let n=r(e,{maxDepth:64});if(n.length!==3)throw Error(`Malformed Certificate`);let a=f(n[0],`TBSCertificate`),s=f(n[1],`signatureAlgorithm`),c=f(n[2],`signatureValue`),l=i(e,a),d=0,m=1,h=l[d];if(h?.tag===160){let t=i(e,h),n=f(t[0],`version INTEGER`);if(t.length!==1||n.tag!==2)throw Error(`version must use INTEGER`);if(m=o(n.value)+1,m<1||m>3)throw Error(`Unsupported certificate version: ${String(m)}`);d+=1}let g=f(l[d],`serialNumber`);if(g.tag!==2)throw Error(`serialNumber must use INTEGER`);let y=f(l[d+1],`TBSCertificate signature`),b=f(l[d+2],`issuer`),x=f(l[d+3],`validity`),S=f(l[d+4],`subject`),C=f(l[d+5],`subjectPublicKeyInfo`),w=d+6,T=l[w];if(T?.tag===129){if(m<2)throw Error(`issuerUniqueID requires certificate version 2 or 3`);$(T.value,`issuerUniqueID`),w+=1}let E=l[w];if(E?.tag===130){if(m<2)throw Error(`subjectUniqueID requires certificate version 2 or 3`);$(E.value,`subjectUniqueID`),w+=1}let D=l[w]?.tag===163?l[w]:void 0;if(D!==void 0){if(m!==3)throw Error(`extensions require certificate version 3`);w+=1}if(w!==l.length)throw Error(`Unsupported TBSCertificate field tag: ${String(f(l[w],`TBSCertificate field`).tag)}`);let O=ne(e,D),k=ae(e,x),A=U(e,C),j=W(e,y),M=W(e,s);G(j,M);let N=t?.decoders===void 0?void 0:R(O.all,t.decoders),P=t?.decoderMap===void 0?void 0:z(O.all,t.decoderMap);return{der:new Uint8Array(e),version:m,serialNumberHex:p(g.value),tbsCertificateDer:e.slice(a.start-a.headerLength,a.end),subjectPublicKeyInfoDer:e.slice(C.start-C.headerLength,C.end),signatureValue:u(c),issuer:V(e,b),subject:V(e,S),notBefore:k.notBefore,notAfter:k.notAfter,signatureAlgorithmOid:M.oid,signatureAlgorithmName:v(M.oid,M.parametersDer),...M.parametersDer===void 0?{}:{signatureAlgorithmParametersDer:M.parametersDer},publicKeyAlgorithmOid:A.oid,publicKeyAlgorithmName:_(A.oid,A.parametersOid),...A.parametersDer===void 0?{}:{publicKeyAlgorithmParametersDer:A.parametersDer},...A.parametersOid===void 0?{}:{publicKeyParametersOid:A.parametersOid},extensions:O.all,...O.basicConstraints===void 0?{}:{basicConstraints:O.basicConstraints},...O.keyUsage===void 0?{}:{keyUsage:O.keyUsage},...O.extendedKeyUsage===void 0?{}:{extendedKeyUsage:O.extendedKeyUsage},...O.subjectAltNames===void 0?{}:{subjectAltNames:O.subjectAltNames},...O.nameConstraints===void 0?{}:{nameConstraints:O.nameConstraints},...O.certificatePolicies===void 0?{}:{certificatePolicies:O.certificatePolicies},...O.policyMappings===void 0?{}:{policyMappings:O.policyMappings},...O.policyConstraints===void 0?{}:{policyConstraints:O.policyConstraints},...O.inhibitAnyPolicy===void 0?{}:{inhibitAnyPolicy:O.inhibitAnyPolicy},...O.authorityInfoAccess===void 0?{}:{authorityInfoAccess:O.authorityInfoAccess},...O.crlDistributionPoints===void 0?{}:{crlDistributionPoints:O.crlDistributionPoints},...N===void 0?{}:{decodedExtensions:N},...P===void 0?{}:{decodedExtensionMap:P},...O.subjectKeyIdentifier===void 0?{}:{subjectKeyIdentifier:O.subjectKeyIdentifier},...O.authorityKeyIdentifier===void 0?{}:{authorityKeyIdentifier:O.authorityKeyIdentifier}}}function A(e,t){return k(h(`CERTIFICATE`,e),t)}function j(e,t){return typeof e==`string`?F(e,t):[k(new Uint8Array(e),t)]}function M(e,t){return typeof e==`string`?A(e,t):I(e)?e:k(new Uint8Array(e),t)}function N(e,t){return F(e,t)}function P(e,t){let n=r(e,{maxDepth:64});if(n.length!==3)throw Error(`Malformed CertificationRequest`);let a=f(n[0],`CertificationRequestInfo`),s=f(n[1],`signatureAlgorithm`),c=f(n[2],`signatureValue`),l=i(e,a);if(l.length<3||l.length>4)throw Error(`Malformed CertificationRequestInfo`);let d=f(l[0],`version`);if(d.tag!==2)throw Error(`version must use INTEGER`);let p=o(d.value)+1;if(p!==1)throw Error(`Unsupported CertificationRequestInfo version: ${String(p)}`);let m=f(l[1],`subject`),h=f(l[2],`subjectPublicKeyInfo`),g=l[3];if(g!==void 0&&g.tag!==160)throw Error(`CertificationRequestInfo attributes must use [0]`);let y=re(e,g),b=U(e,h),x=W(e,s),S=t?.decoders===void 0?void 0:R(y.all,t.decoders),C=t?.decoderMap===void 0?void 0:z(y.all,t.decoderMap);return{version:p,certificationRequestInfoDer:e.slice(a.start-a.headerLength,a.end),subjectPublicKeyInfoDer:e.slice(h.start-h.headerLength,h.end),signatureValue:u(c),subject:V(e,m),signatureAlgorithmOid:x.oid,signatureAlgorithmName:v(x.oid,x.parametersDer),...x.parametersDer===void 0?{}:{signatureAlgorithmParametersDer:x.parametersDer},publicKeyAlgorithmOid:b.oid,publicKeyAlgorithmName:_(b.oid,b.parametersOid),...b.parametersDer===void 0?{}:{publicKeyAlgorithmParametersDer:b.parametersDer},...b.parametersOid===void 0?{}:{publicKeyParametersOid:b.parametersOid},requestedExtensions:y.all,...y.basicConstraints===void 0?{}:{basicConstraints:y.basicConstraints},...y.keyUsage===void 0?{}:{keyUsage:y.keyUsage},...y.extendedKeyUsage===void 0?{}:{extendedKeyUsage:y.extendedKeyUsage},...y.subjectAltNames===void 0?{}:{subjectAltNames:y.subjectAltNames},...y.nameConstraints===void 0?{}:{nameConstraints:y.nameConstraints},...y.certificatePolicies===void 0?{}:{certificatePolicies:y.certificatePolicies},...y.policyMappings===void 0?{}:{policyMappings:y.policyMappings},...y.policyConstraints===void 0?{}:{policyConstraints:y.policyConstraints},...y.inhibitAnyPolicy===void 0?{}:{inhibitAnyPolicy:y.inhibitAnyPolicy},...y.authorityInfoAccess===void 0?{}:{authorityInfoAccess:y.authorityInfoAccess},...y.crlDistributionPoints===void 0?{}:{crlDistributionPoints:y.crlDistributionPoints},...S===void 0?{}:{decodedExtensions:S},...C===void 0?{}:{decodedExtensionMap:C}}}function F(e,t){return g(e).filter(e=>e.label===`CERTIFICATE`).map(e=>k(e.bytes,t))}function I(e){return`subjectPublicKeyInfoDer`in e}function ee(e,t){return P(h(`CERTIFICATE REQUEST`,e),t)}function L(e,t){return e.find(e=>e.oid===t)}function te(e,t){let n=L(e,t.oid);if(n!==void 0)return t.decode(n)}function R(e,t){let n=[];for(let r of t){let t=L(e,r.oid);t!==void 0&&n.push({oid:t.oid,critical:t.critical,value:r.decode(t)})}return n}function z(e,t){let n={};for(let r in t){let i=t[r];if(i===void 0)continue;let a=L(e,i.oid);a!==void 0&&(n={...n,[r]:{oid:a.oid,critical:a.critical,value:i.decode(a)}})}return n}function ne(e,t){return t===void 0?{all:[]}:B(e,f(i(e,t)[0],`extensions sequence`),`certificate`)}function re(e,t){if(t===void 0)return{all:[]};if(t.tag!==160)throw Error(`CertificationRequestInfo attributes must use [0]`);let n;for(let r of i(e,t)){let t=i(e,r);if(t.length!==2)throw Error(`Malformed CSR attribute`);if(c(f(t[0],`attribute OID`).value)!==m.extensionRequest)continue;if(n!==void 0)throw Error(`extensionRequest attribute must not repeat`);let a=f(t[1],`attribute values`);if(a.tag!==49)throw Error(`extensionRequest attribute values must use SET`);let o=i(e,a);if(o.length!==1)throw Error(`extensionRequest attribute must contain exactly one value`);n=B(e,f(o[0],`requested extensions`),`csr`)}return n??{all:[]}}function B(e,t,n){let r=[],o={},s=new Set;for(let l of i(e,t)){let t=i(e,l);if(t.length<2||t.length>3)throw Error(`Malformed Extension`);let u=c(f(t[0],`extension OID`).value);if(s.has(u))throw Error(`Duplicate extension OID: ${u}`);s.add(u);let d=1,m=!1,h=t[d];h?.tag===1&&(m=a(h.value),d+=1);let g=f(t[d],`extension value`);if(g.tag!==4||d!==t.length-1)throw Error(`Extension value must use OCTET STRING`);r.push({oid:u,critical:m,valueDer:new Uint8Array(g.value),valueHex:p(g.value)}),T(n,u,o,g.value)}return{all:r,...o}}function V(e,t){let n=[],r=[],a={};for(let o of i(e,t)){let t=H(e,o);n.push(t);for(let e of t.attributes)r.push(e),e.key!==void 0&&a[e.key]===void 0&&(a[e.key]=e.value)}return{derHex:p(e.slice(t.start-t.headerLength,t.end)),rdns:n,attributes:r,values:a}}function ie(e,t){return H(e,t)}function H(e,t){let n=[],r={};for(let a of i(e,t)){let t=i(e,a),o=c(f(t[0],`name OID`).value),s=f(t[1],`name value`),u=S(o),d=l(s.tag,s.value),p=u===void 0?{oid:o,valueTag:s.tag,value:d}:{oid:o,key:u,valueTag:s.tag,value:d};n.push(p),u!==void 0&&r[u]===void 0&&(r[u]=d)}return{derHex:p(e.slice(t.start-t.headerLength,t.end)),attributes:n,values:r}}function ae(e,t){let n=i(e,t);return{notBefore:d(f(n[0],`notBefore`)),notAfter:d(f(n[1],`notAfter`))}}function U(e,t){if(t.tag!==48)throw Error(`SubjectPublicKeyInfo must use SEQUENCE`);let n=i(e,t);if(n.length!==2)throw Error(`SubjectPublicKeyInfo must contain algorithm and subjectPublicKey`);let r=f(n[0],`SPKI algorithm`);if(r.tag!==48)throw Error(`SubjectPublicKeyInfo algorithm must use SEQUENCE`);let a=W(e,r);return u(f(n[1],`subjectPublicKey BIT STRING`)),a}function W(e,t){let n=i(e,t);if(n.length===0||n.length>2)throw Error(`Malformed AlgorithmIdentifier`);let r=c(f(n[0],`algorithm OID`).value),a=n[1];if(a===void 0)return{oid:r};let o=e.slice(a.start-a.headerLength,a.end);return a?.tag===6?{oid:r,parametersDer:o,parametersOid:c(a.value)}:{oid:r,parametersDer:o}}function G(e,t){if(e.oid!==t.oid||!K(e.parametersDer,t.parametersDer))throw Error(`Certificate signatureAlgorithm must match TBSCertificate signature`)}function K(e,t){if(e===void 0||t===void 0)return e===t;if(e.length!==t.length)return!1;for(let n=0;n<e.length;n+=1)if(e[n]!==t[n])return!1;return!0}function q(e){let t=n(e,{maxDepth:64});if(t.tag!==48)throw Error(`basicConstraints must use SEQUENCE`);let r=i(e,t),o=!1,c,l=!1,u=!1;for(let e of r){if(e.tag===1){if(l)throw Error(`basicConstraints cA must not repeat`);if(u)throw Error(`basicConstraints cA must precede pathLength`);l=!0,o=a(e.value);continue}if(e.tag===2){if(u)throw Error(`basicConstraints pathLength must not repeat`);u=!0,c=s(e.value,`basicConstraints pathLength`);continue}throw Error(`Unsupported basicConstraints field tag: ${String(e.tag)}`)}if(c!==void 0&&o!==!0)throw Error(`basicConstraints pathLength requires cA = true`);return c===void 0?{ca:o}:{ca:o,pathLength:c}}function oe(e){return x(e)}function se(e){return i(e,f(n(e,{maxDepth:64}),`extendedKeyUsage sequence`)).map(e=>w(c(e.value)))}function ce(e){let t=i(e,f(n(e,{maxDepth:64}),`certificatePolicies sequence`));if(t.length===0)throw Error(`certificatePolicies must not be empty`);return t.map(t=>le(e,t))}function le(e,t){let n=i(e,t),r=c(f(n[0],`policyIdentifier`).value),a=n[1];if(n.length>2)throw Error(`policyInformation has unexpected trailing fields`);if(a===void 0)return{policyIdentifier:r};let o=i(e,a);if(o.length===0)throw Error(`policyQualifiers must not be empty`);return{policyIdentifier:r,policyQualifiers:o.map(t=>ue(e,t))}}function ue(e,t){let n=i(e,t),r=c(f(n[0],`policyQualifierId`).value),a=f(n[1],`policyQualifier`);if(n.length>2)throw Error(`policyQualifierInfo has unexpected trailing fields`);if(r===m.cpsPolicyQualifier){if(a.tag!==22)throw Error(`cps policy qualifier must use IA5String`);return{type:`cps`,uri:l(a.tag,a.value)}}return r===m.userNoticePolicyQualifier?{type:`userNotice`,...J(e,a)}:{type:`oid`,oid:r,qualifierDer:e.slice(a.start-a.headerLength,a.end)}}function J(e,t){let n=i(e,t),r,a;for(let t of n){if(t.tag===48){if(r!==void 0)throw Error(`userNotice must not contain multiple noticeRef values`);r=de(e,t);continue}if(a!==void 0)throw Error(`userNotice must not contain multiple explicitText values`);a=Q(t)}return{...r===void 0?{}:{noticeRef:r},...a===void 0?{}:{explicitText:a}}}function de(e,t){if(t.tag!==48)throw Error(`noticeRef must use SEQUENCE`);let n=i(e,t),r=Q(f(n[0],`noticeRef organization`)),a=f(n[1],`noticeRef noticeNumbers`);if(n.length>2)throw Error(`noticeRef has unexpected trailing fields`);return{organization:r,noticeNumbers:fe(e,a)}}function fe(e,t){if(t.tag!==48)throw Error(`noticeRef noticeNumbers must use SEQUENCE`);let n=i(e,t);if(n.length===0)throw Error(`noticeRef noticeNumbers must not be empty`);return n.map(e=>{if(e.tag!==2)throw Error(`noticeRef noticeNumber must use INTEGER`);return s(e.value,`noticeRef noticeNumber`)})}function pe(e){let t=i(e,f(n(e,{maxDepth:64}),`policyMappings sequence`));if(t.length===0)throw Error(`policyMappings must not be empty`);return t.map(t=>{let n=i(e,t),r=c(f(n[0],`policyMappings issuerDomainPolicy`).value),a=c(f(n[1],`policyMappings subjectDomainPolicy`).value);if(n.length>2)throw Error(`policyMappings entry has unexpected trailing fields`);if(r===m.anyPolicy||a===m.anyPolicy)throw Error(`policyMappings must not use anyPolicy`);return{issuerDomainPolicy:r,subjectDomainPolicy:a}})}function me(e){let t=f(n(e,{maxDepth:64}),`policyConstraints sequence`),r,a;for(let n of i(e,t)){if(n.tag===128){if(r!==void 0)throw Error(`policyConstraints must not repeat requireExplicitPolicy`);r=s(n.value,`policyConstraints requireExplicitPolicy`);continue}if(n.tag===129){if(a!==void 0)throw Error(`policyConstraints must not repeat inhibitPolicyMapping`);a=s(n.value,`policyConstraints inhibitPolicyMapping`);continue}throw Error(`Unsupported policyConstraints field tag: ${n.tag}`)}if(r===void 0&&a===void 0)throw Error(`policyConstraints must set requireExplicitPolicy or inhibitPolicyMapping`);return{...r===void 0?{}:{requireExplicitPolicy:r},...a===void 0?{}:{inhibitPolicyMapping:a}}}function he(e){let t=f(n(e,{maxDepth:64}),`inhibitAnyPolicy integer`);if(t.tag!==2)throw Error(`inhibitAnyPolicy must be an INTEGER`);return{skipCerts:s(t.value,`inhibitAnyPolicy skipCerts`)}}function ge(e){return i(e,f(n(e,{maxDepth:64}),`subjectAltName sequence`)).map(t=>Y(e,t))}function _e(e){let t=f(n(e,{maxDepth:64}),`authorityInfoAccess sequence`);if(t.tag!==48)throw Error(`authorityInfoAccess must use SEQUENCE`);let r=i(e,t);if(r.length===0)throw Error(`authorityInfoAccess must not be empty`);return r.map(t=>{if(t.tag!==48)throw Error(`authorityInfoAccess entry must use SEQUENCE`);let n=i(e,t);if(n.length!==2)throw Error(`authorityInfoAccess entry must contain method and location only`);let r=f(n[0],`authorityInfoAccess method`),a=f(n[1],`authorityInfoAccess location`);if(r.tag!==6)throw Error(`authorityInfoAccess method must use OBJECT IDENTIFIER`);if(a.tag!==134)throw Error(`Unsupported authorityInfoAccess location tag: ${a.tag}`);return{method:C(c(r.value)),uri:E.decode(a.value)}})}function ve(e){let t=f(n(e,{maxDepth:64}),`CRLDistributionPoints sequence`);if(t.tag!==48)throw Error(`CRLDistributionPoints must use SEQUENCE`);let r=i(e,t);if(r.length===0)throw Error(`CRLDistributionPoints must not be empty`);let a=[];for(let t of r)a.push(ye(e,t));return a}function ye(e,t){if(t.tag!==48)throw Error(`DistributionPoint must use SEQUENCE`);let n,r,a;for(let o of i(e,t))if(o.tag===160){if(n!==void 0)throw Error(`DistributionPoint distributionPoint must not repeat`);n=be(e,o)}else if(o.tag===129){if(r!==void 0)throw Error(`DistributionPoint reasons must not repeat`);r=b(o.value)}else if(o.tag===162){if(a!==void 0)throw Error(`DistributionPoint crlIssuer must not repeat`);a=xe(e,o)}else throw Error(`Unsupported DistributionPoint field tag: ${String(o.tag)}`);if(n===void 0&&a===void 0)throw Error(`DistributionPoint must include distributionPoint or crlIssuer`);return{...n===void 0?{}:{distributionPoint:n},...r===void 0?{}:{reasons:r},...a===void 0?{}:{crlIssuer:a}}}function be(e,t){let n=i(e,t);if(n.length!==1)throw Error(`distributionPointName must contain exactly one choice`);let r=f(n[0],`distributionPointName`);if(r.tag===160){let t=i(e,r);if(t.length===0)throw Error(`distributionPointName fullName must not be empty`);for(let e of t)if((e.tag&192)!=128)throw Error(`distributionPointName fullName must contain GeneralName entries`);return{fullName:t.map(t=>Y(e,t))}}if(r.tag===161)return{relativeName:ie(e,r)};throw Error(`Unsupported distributionPointName tag: ${r.tag}`)}function xe(e,t){let n=i(e,t);if(n.length===0)throw Error(`GeneralNames must not be empty`);for(let e of n)if((e.tag&192)!=128)throw Error(`GeneralNames must contain GeneralName entries`);return n.map(t=>Y(e,t))}function Y(e,t){switch(t.tag){case 160:{let n=Se(e,t);return n===void 0?{type:`unknown`,tag:t.tag,value:e.slice(t.start,t.end)}:n}case 129:return{type:`email`,value:E.decode(t.value)};case 130:return{type:`dns`,value:E.decode(t.value)};case 134:return{type:`uri`,value:E.decode(t.value)};case 135:return{type:`ip`,value:y(t.value)};case 164:return{type:`directoryName`,derHex:p(Z(t,e))};default:return{type:`unknown`,tag:t.tag,value:e.slice(t.start,t.end)}}}function Se(e,t){let n=i(e,t);if(n.length!==1)throw Error(`otherName must contain exactly one SEQUENCE`);let r=i(e,f(n[0],`otherName sequence`));if(r.length!==2)throw Error(`otherName must contain exactly type-id and value`);let a=f(r[0],`otherName type-id`),o=f(r[1],`otherName value`);if(c(a.value)!==m.idOnDnsSrv)return;if(o.tag!==160)throw Error(`SRV-ID otherName value must use explicit [0]`);let s=i(e,o);if(s.length!==1)throw Error(`SRV-ID otherName value must contain exactly one IA5String`);let u=f(s[0],`SRV-ID IA5String`);if(u.tag!==22)throw Error(`SRV-ID otherName value must be an IA5String`);return{type:`srv`,value:l(u.tag,u.value)}}function Ce(e){let t=f(n(e,{maxDepth:64,allowOpaqueConstructedTags:[160,163,165]}),`nameConstraints sequence`);if(t.tag!==48)throw Error(`nameConstraints must use SEQUENCE`);let r,a;for(let n of i(e,t))if(n.tag===160){if(r!==void 0)throw Error(`nameConstraints permittedSubtrees must not repeat`);r=X(e,n)}else if(n.tag===161){if(a!==void 0)throw Error(`nameConstraints excludedSubtrees must not repeat`);a=X(e,n)}else throw Error(`Unsupported nameConstraints field tag: ${String(n.tag)}`);return{...r===void 0?{}:{permittedSubtrees:r},...a===void 0?{}:{excludedSubtrees:a}}}function X(e,t){let n=[],r=i(e,t);if(r.length===0)throw Error(`name constraints GeneralSubtrees must not be empty`);for(let t of r){if(t.tag!==48)throw Error(`name constraints GeneralSubtree must use SEQUENCE`);let r=i(e,t),a=r[0];if(a===void 0)throw Error(`name constraints GeneralSubtree base is required`);let o=!1;for(let e=1;e<r.length;e+=1){let t=r[e];if(t!==void 0)if(t.tag===128){if(o)throw Error(`name constraints GeneralSubtree minimum must not repeat`);if(o=!0,s(t.value,`name constraints GeneralSubtree minimum`)!==0)throw Error(`name constraints GeneralSubtree minimum must be 0`)}else if(t.tag===129)throw Error(`name constraints GeneralSubtree maximum is not supported`);else throw Error(`Unsupported name constraints GeneralSubtree field tag: ${String(t.tag)}`)}let c=we(e,a);c!==void 0&&n.push({base:c})}return n}function we(e,t){switch(t.tag){case 160:return{type:`otherName`,value:new Uint8Array(t.value)};case 129:return{type:`email`,value:E.decode(t.value)};case 130:return{type:`dns`,value:E.decode(t.value)};case 163:return{type:`x400Address`,value:new Uint8Array(t.value)};case 134:return{type:`uri`,value:E.decode(t.value)};case 135:if(t.value.length===8)return{type:`ip`,addressBytes:t.value.slice(0,4),maskBytes:t.value.slice(4,8)};if(t.value.length===32)return{type:`ip`,addressBytes:t.value.slice(0,16),maskBytes:t.value.slice(16,32)};throw Error(`Invalid IP name constraint: expected 8 (IPv4) or 32 (IPv6) bytes, got ${String(t.value.length)}`);case 164:return{type:`directoryName`,derHex:p(Z(t,e))};case 165:return{type:`ediPartyName`,value:new Uint8Array(t.value)};case 136:return{type:`registeredID`,value:c(t.value)}}throw Error(`Unsupported name constraint GeneralName tag: ${String(t.tag)}`)}function Z(t,n){if(t.value.length>0&&t.value[0]===48)return new Uint8Array(t.value);let r=e(t.value.length),i=new Uint8Array(1+r.length+t.value.length);return i[0]=48,i.set(r,1),i.set(t.value,1+r.length),i}function Q(e){switch(e.tag){case 12:case 22:case 26:return E.decode(e.value);case 30:return Te(e.value);default:throw Error(`Unsupported DisplayText tag: ${e.tag}`)}}function Te(e){if(e.length%2!=0)throw Error(`Invalid BMPString length`);let t=``;for(let n=0;n<e.length;n+=2){let r=e[n],i=e[n+1];if(r===void 0||i===void 0)throw Error(`Invalid BMPString content`);t+=String.fromCharCode(r<<8|i)}return t}function Ee(e){let n=f(t(e,0),`authorityKeyIdentifier sequence`);if(n.end!==e.length)throw Error(`Trailing data after DER element`);let r,a=!1,o=!1,s=-1,c=n.start;for(;c<n.end;){let l=t(e,c);if(l.end>n.end)throw Error(`DER child exceeds parent length`);if(l.tag===128){if(r!==void 0)throw Error(`authorityKeyIdentifier keyIdentifier must not repeat`);if(s>=0)throw Error(`authorityKeyIdentifier fields must preserve DER order`);r=p(l.value),s=0}else if(l.tag===161){if(a)throw Error(`authorityKeyIdentifier authorityCertIssuer must not repeat`);if(s>=1)throw Error(`authorityKeyIdentifier fields must preserve DER order`);let t=i(e,l);if(t.length===0)throw Error(`authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries`);for(let n of t){if((n.tag&192)!=128)throw Error(`authorityKeyIdentifier authorityCertIssuer must contain GeneralName entries`);Y(e,n)}a=!0,s=1}else if(l.tag===130){if(o)throw Error(`authorityKeyIdentifier authorityCertSerialNumber must not repeat`);if(s>=2||!a)throw Error(`authorityKeyIdentifier fields must preserve DER order`);De(l.value,`authorityKeyIdentifier authorityCertSerialNumber`),o=!0,s=2}else throw Error(`Unsupported authorityKeyIdentifier field tag: ${String(l.tag)}`);c=l.end}if(c!==n.end)throw Error(`Malformed DER sequence`);if(a!==o)throw Error(`authorityKeyIdentifier authorityCertIssuer and authorityCertSerialNumber must appear together`);return r}function De(e,t){let n=e[0];if(n===void 0)throw Error(`${t} must not be empty`);if(n&128)throw Error(`${t} must be non-negative`);if(e.length>1&&n===0&&!((e[1]??0)&128))throw Error(`${t} must use minimal encoding`)}function $(e,t){let n=e[0];if(n===void 0||n>7)throw Error(`${t} must use BIT STRING encoding`);let r=e.slice(1);if(r.length===0){if(n!==0)throw Error(`${t} must use BIT STRING encoding`);return}if(n===0)return;let i=r[r.length-1];if(i===void 0)throw Error(`${t} must use BIT STRING encoding`);if(i&(1<<n)-1)throw Error(`${t} BIT STRING must not set padding bits`)}export{te as decodeExtension,z as decodeExtensionMap,R as decodeExtensions,D as defineExtensionDecoder,O as defineExtensionDecoderMap,L as findExtension,_e as parseAuthorityInfoAccess,Ee as parseAuthorityKeyIdentifier,q as parseBasicConstraints,N as parseCertificateChainPem,k as parseCertificateDer,M as parseCertificateFromSource,A as parseCertificatePem,ce as parseCertificatePolicies,P as parseCertificateSigningRequestDer,ee as parseCertificateSigningRequestPem,j as parseCertificatesFromSource,ve as parseCrlDistributionPoints,se as parseExtendedKeyUsage,he as parseInhibitAnyPolicy,oe as parseKeyUsage,Ce as parseNameConstraints,me as parsePolicyConstraints,pe as parsePolicyMappings,ge as parseSubjectAltNames};
2
+ //# sourceMappingURL=parse.js.map