micro509 0.14.0 → 0.16.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 (164) hide show
  1. package/CHANGELOG.md +2119 -0
  2. package/README.md +105 -225
  3. package/dist/crypto/crypto.d.ts +42 -11
  4. package/dist/crypto/crypto.js +53 -22
  5. package/dist/crypto/crypto.js.map +1 -1
  6. package/dist/crypto.d.ts +2 -2
  7. package/dist/crypto.js +1 -2
  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 +11 -11
  12. package/dist/index.js +19 -19
  13. package/dist/internal/asn1/asn1.d.ts +3 -6
  14. package/dist/internal/asn1/asn1.js +343 -43
  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 +38 -41
  21. package/dist/internal/asn1/der.js +131 -52
  22. package/dist/internal/asn1/der.js.map +1 -1
  23. package/dist/internal/asn1/oids.js +83 -53
  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 +1 -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 +24 -11
  34. package/dist/internal/crypto/pbes2.js +79 -26
  35. package/dist/internal/crypto/pbes2.js.map +1 -1
  36. package/dist/internal/crypto/rsa-pss.js +46 -44
  37. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  38. package/dist/internal/crypto/sig-verify.d.ts +4 -7
  39. package/dist/internal/crypto/sig-verify.js +107 -60
  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 +1 -2
  47. package/dist/internal/shared/base64.js.map +1 -1
  48. package/dist/internal/shared/dn.d.ts +3 -6
  49. package/dist/internal/shared/dn.js +99 -53
  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 +4 -7
  56. package/dist/internal/shared/ip.js +1 -2
  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/spki-distrust.js +23 -0
  73. package/dist/internal/verify/spki-distrust.js.map +1 -0
  74. package/dist/internal/verify/verify-path.d.ts +1 -4
  75. package/dist/internal/verify/verify-path.js +282 -129
  76. package/dist/internal/verify/verify-path.js.map +1 -1
  77. package/dist/internal/x509/directory-name.js +1 -2
  78. package/dist/internal/x509/directory-name.js.map +1 -1
  79. package/dist/internal/x509/extension-bits.js +2 -3
  80. package/dist/internal/x509/extension-bits.js.map +1 -1
  81. package/dist/internal/x509/extension-errors.d.ts +1 -4
  82. package/dist/internal/x509/extension-errors.js +1 -2
  83. package/dist/internal/x509/extension-errors.js.map +1 -1
  84. package/dist/internal/x509/extension-registry.js +107 -89
  85. package/dist/internal/x509/extension-registry.js.map +1 -1
  86. package/dist/internal/x509/general-name-profile.js +129 -0
  87. package/dist/internal/x509/general-name-profile.js.map +1 -0
  88. package/dist/internal/x509/general-name-tags.js +1 -2
  89. package/dist/internal/x509/general-name-tags.js.map +1 -1
  90. package/dist/internal/x509/general-name.js +128 -42
  91. package/dist/internal/x509/general-name.js.map +1 -1
  92. package/dist/internal/x509/name-fields.js +1 -2
  93. package/dist/internal/x509/name-fields.js.map +1 -1
  94. package/dist/internal/x509/or-address.js +217 -0
  95. package/dist/internal/x509/or-address.js.map +1 -0
  96. package/dist/keys/keys.d.ts +122 -104
  97. package/dist/keys/keys.js +211 -204
  98. package/dist/keys/keys.js.map +1 -1
  99. package/dist/keys.d.ts +3 -3
  100. package/dist/pem/pem.d.ts +14 -17
  101. package/dist/pem/pem.js +16 -17
  102. package/dist/pem/pem.js.map +1 -1
  103. package/dist/pem.js +1 -2
  104. package/dist/pkcs/pfx.d.ts +57 -32
  105. package/dist/pkcs/pfx.js +209 -173
  106. package/dist/pkcs/pfx.js.map +1 -1
  107. package/dist/pkcs/pkcs12-mac.d.ts +104 -27
  108. package/dist/pkcs/pkcs12-mac.js +266 -79
  109. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  110. package/dist/pkcs/pkcs7.d.ts +43 -37
  111. package/dist/pkcs/pkcs7.js +218 -217
  112. package/dist/pkcs/pkcs7.js.map +1 -1
  113. package/dist/pkcs.d.ts +4 -3
  114. package/dist/result/result.d.ts +27 -20
  115. package/dist/result/result.js +8 -3
  116. package/dist/result/result.js.map +1 -1
  117. package/dist/result.d.ts +2 -2
  118. package/dist/result.js +1 -2
  119. package/dist/revocation/chain.d.ts +101 -23
  120. package/dist/revocation/chain.js +436 -182
  121. package/dist/revocation/chain.js.map +1 -1
  122. package/dist/revocation/crl.d.ts +131 -67
  123. package/dist/revocation/crl.js +443 -327
  124. package/dist/revocation/crl.js.map +1 -1
  125. package/dist/revocation/ocsp.d.ts +94 -59
  126. package/dist/revocation/ocsp.js +331 -293
  127. package/dist/revocation/ocsp.js.map +1 -1
  128. package/dist/revocation/revocation.d.ts +47 -35
  129. package/dist/revocation/revocation.js +88 -57
  130. package/dist/revocation/revocation.js.map +1 -1
  131. package/dist/revocation.d.ts +3 -3
  132. package/dist/verify/identity.d.ts +16 -19
  133. package/dist/verify/identity.js +118 -89
  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 +9 -7
  137. package/dist/verify/verify.d.ts +115 -46
  138. package/dist/verify/verify.js +373 -183
  139. package/dist/verify/verify.js.map +1 -1
  140. package/dist/verify.d.ts +2 -2
  141. package/dist/x509/certificate.d.ts +20 -21
  142. package/dist/x509/certificate.js +62 -46
  143. package/dist/x509/certificate.js.map +1 -1
  144. package/dist/x509/csr.d.ts +5 -7
  145. package/dist/x509/csr.js +10 -11
  146. package/dist/x509/csr.js.map +1 -1
  147. package/dist/x509/extensions.d.ts +154 -66
  148. package/dist/x509/extensions.js +317 -151
  149. package/dist/x509/extensions.js.map +1 -1
  150. package/dist/x509/fingerprint.d.ts +15 -7
  151. package/dist/x509/fingerprint.js +20 -3
  152. package/dist/x509/fingerprint.js.map +1 -1
  153. package/dist/x509/name-text.d.ts +7 -9
  154. package/dist/x509/name-text.js +18 -4
  155. package/dist/x509/name-text.js.map +1 -1
  156. package/dist/x509/name.d.ts +9 -11
  157. package/dist/x509/name.js +18 -19
  158. package/dist/x509/name.js.map +1 -1
  159. package/dist/x509/parse.d.ts +84 -69
  160. package/dist/x509/parse.js +421 -400
  161. package/dist/x509/parse.js.map +1 -1
  162. package/dist/x509.d.ts +3 -3
  163. package/dist/x509.js +3 -3
  164. package/package.json +76 -59
package/dist/keys/keys.js CHANGED
@@ -1,13 +1,14 @@
1
- import { explicitContext as e, integer as t, nullValue as n, objectIdentifier as r, octetString as i, readRootElement as a, readSequenceChildren as o, sequence as s, tlv as c } from "../internal/asn1/der.js";
2
- import { decodeObjectIdentifier as l, extractBitStringValue as u, hexToBytes as d, toArrayBuffer as f, toHex as p } from "../internal/asn1/asn1.js";
3
- import { OIDS as m } from "../internal/asn1/oids2.js";
4
- import { md5 as h } from "../internal/crypto/hash.js";
5
- import { getCrypto as g } from "../internal/crypto/webcrypto.js";
6
- import { decryptPbes2 as _, encryptPbes2 as v, isWrongPasswordError as y, parsePbes2AlgorithmIdentifier as b, wrongPasswordError as x } from "../internal/crypto/pbes2.js";
7
- import { base64Decode as S, base64Encode as C } from "../internal/shared/base64.js";
8
- import { failureResult as w, rethrowIfInvariant as T, successResult as E } from "../result/result.js";
9
- import { parseTraditionalPemOrThrow as D, pemDecodeOrThrow as O, pemEncode as k, trimLwsp as A } from "../pem/pem.js";
10
- //#region src/keys/keys.ts
1
+ import { failureResult as e, rethrowIfInvariant as t, successResult as n } from "../result/result.js";
2
+ import { DECODE_LIMIT_CODES as r, decodeFailureResult as i, rethrowDecodeRefusal as a } from "../internal/asn1/decode-refusal.js";
3
+ import { explicitContext as o, integer as s, nullValue as c, objectIdentifier as l, octetString as u, readRootElement as d, readSequenceChildren as f, sequence as p, tlv as m } from "../internal/asn1/der.js";
4
+ import { decodeObjectIdentifier as h, extractBitStringValue as g, hexToBytes as _, toArrayBuffer as v, toHex as y } from "../internal/asn1/asn1.js";
5
+ import { OIDS as b } from "../internal/asn1/oids2.js";
6
+ import { getCrypto as x } from "../internal/crypto/webcrypto.js";
7
+ import { md5 as S } from "../internal/crypto/hash.js";
8
+ import { createKdfBudget as C, decryptPbes2 as w, encryptPbes2 as T, isKdfIterationLimitError as E, isWrongPasswordError as D, parsePbes2AlgorithmIdentifier as O, wrongPasswordError as k } from "../internal/crypto/pbes2.js";
9
+ import { base64Decode as A, base64Encode as j } from "../internal/shared/base64.js";
10
+ import { parseTraditionalPemOrThrow as M, pemDecodeOrThrow as N, pemEncode as P, trimLwsp as F } from "../pem/pem.js";
11
+ // src/keys/keys.ts
11
12
  /**
12
13
  * WebCrypto key generation plus import/export for PKCS#1, PKCS#8, SEC1, SPKI, and JWK.
13
14
  *
@@ -49,7 +50,7 @@ async function generateKeyPair(e = {
49
50
  kind: "ecdsa",
50
51
  curve: "P-256"
51
52
  }) {
52
- let t = g().subtle, n = isRsaOaepInput(e) ? ["encrypt", "decrypt"] : ["sign", "verify"], r = await t.generateKey(toGenerateKeyAlgorithm(e), !0, n);
53
+ let t = x().subtle, n = isRsaOaepInput(e) ? ["encrypt", "decrypt"] : ["sign", "verify"], r = await t.generateKey(toGenerateKeyAlgorithm(e), !0, n);
53
54
  if (!("publicKey" in r) || !("privateKey" in r)) throw Error("Expected an asymmetric key pair");
54
55
  return wrapKeyPair(r.publicKey, r.privateKey);
55
56
  }
@@ -58,22 +59,22 @@ function wrapKeyPair(e, t) {
58
59
  publicKey: e,
59
60
  privateKey: t,
60
61
  async exportSpkiDer() {
61
- return new Uint8Array(await g().subtle.exportKey("spki", e));
62
+ return new Uint8Array(await x().subtle.exportKey("spki", e));
62
63
  },
63
64
  async exportSpkiPem() {
64
- return k("PUBLIC KEY", await this.exportSpkiDer());
65
+ return P("PUBLIC KEY", await this.exportSpkiDer());
65
66
  },
66
67
  async exportPkcs8Der() {
67
- return new Uint8Array(await g().subtle.exportKey("pkcs8", t));
68
+ return new Uint8Array(await x().subtle.exportKey("pkcs8", t));
68
69
  },
69
70
  async exportPkcs8Pem() {
70
- return k("PRIVATE KEY", await this.exportPkcs8Der());
71
+ return P("PRIVATE KEY", await this.exportPkcs8Der());
71
72
  },
72
73
  exportPublicJwk() {
73
- return g().subtle.exportKey("jwk", e);
74
+ return x().subtle.exportKey("jwk", e);
74
75
  },
75
76
  exportPrivateJwk() {
76
- return g().subtle.exportKey("jwk", t);
77
+ return x().subtle.exportKey("jwk", t);
77
78
  }
78
79
  };
79
80
  }
@@ -84,7 +85,7 @@ function wrapKeyPair(e, t) {
84
85
  * @see {@linkcode exportSpkiPem} for PEM output
85
86
  */
86
87
  async function exportSpkiDer(e) {
87
- return new Uint8Array(await g().subtle.exportKey("spki", e));
88
+ return new Uint8Array(await x().subtle.exportKey("spki", e));
88
89
  }
89
90
  /**
90
91
  * Export a private key as DER-encoded PKCS#8 PrivateKeyInfo.
@@ -94,7 +95,7 @@ async function exportSpkiDer(e) {
94
95
  * @see {@linkcode exportEncryptedPkcs8Der} for password-protected export
95
96
  */
96
97
  async function exportPkcs8Der(e) {
97
- return new Uint8Array(await g().subtle.exportKey("pkcs8", e));
98
+ return new Uint8Array(await x().subtle.exportKey("pkcs8", e));
98
99
  }
99
100
  /**
100
101
  * Export a public key as a JSON Web Key.
@@ -106,7 +107,7 @@ async function exportPkcs8Der(e) {
106
107
  * ```
107
108
  */
108
109
  function exportPublicJwk(e) {
109
- return g().subtle.exportKey("jwk", e);
110
+ return x().subtle.exportKey("jwk", e);
110
111
  }
111
112
  /**
112
113
  * Export a private key as a JSON Web Key.
@@ -115,7 +116,7 @@ function exportPublicJwk(e) {
115
116
  * @see {@linkcode exportPublicJwk} for public key export
116
117
  */
117
118
  function exportPrivateJwk(e) {
118
- return g().subtle.exportKey("jwk", e);
119
+ return x().subtle.exportKey("jwk", e);
119
120
  }
120
121
  /**
121
122
  * Export a private key as PEM-encoded PKCS#8 PrivateKeyInfo.
@@ -133,7 +134,7 @@ function exportPrivateJwk(e) {
133
134
  * @see {@linkcode exportEncryptedPkcs8Pem} for password-protected export
134
135
  */
135
136
  async function exportPkcs8Pem(e) {
136
- return k("PRIVATE KEY", await exportPkcs8Der(e));
137
+ return P("PRIVATE KEY", await exportPkcs8Der(e));
137
138
  }
138
139
  /**
139
140
  * Export a private key as DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -143,12 +144,14 @@ async function exportPkcs8Pem(e) {
143
144
  * @param privateKey - The private key to export
144
145
  * @param options - Encryption options including password and optional algorithm settings
145
146
  *
147
+ * @throws {RangeError} If `options.iterations` is not an integer from 1 to 4294967295
148
+ *
146
149
  * @see {@linkcode importEncryptedPkcs8Der} for the inverse operation
147
150
  * @see {@linkcode exportEncryptedPkcs8Pem} for PEM output
148
151
  */
149
152
  async function exportEncryptedPkcs8Der(e, t) {
150
- let n = await v(await exportPkcs8Der(e), t);
151
- return s([n.algorithmIdentifierDer, i(n.encryptedData)]);
153
+ let n = await exportPkcs8Der(e), r = await T(n, t);
154
+ return p([r.algorithmIdentifierDer, u(r.encryptedData)]);
152
155
  }
153
156
  /**
154
157
  * Export a private key as PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -165,7 +168,7 @@ async function exportEncryptedPkcs8Der(e, t) {
165
168
  * @see {@linkcode importEncryptedPkcs8Pem} for the inverse operation
166
169
  */
167
170
  async function exportEncryptedPkcs8Pem(e, t) {
168
- return k("ENCRYPTED PRIVATE KEY", await exportEncryptedPkcs8Der(e, t));
171
+ return P("ENCRYPTED PRIVATE KEY", await exportEncryptedPkcs8Der(e, t));
169
172
  }
170
173
  /**
171
174
  * Export an RSA private key as DER-encoded PKCS#1 RSAPrivateKey.
@@ -180,7 +183,7 @@ async function exportEncryptedPkcs8Pem(e, t) {
180
183
  */
181
184
  async function exportPkcs1Der(e) {
182
185
  let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
183
- if (t.algorithmOid !== m.rsaEncryption) throw Error("PKCS#1 export requires an RSA private key");
186
+ if (t.algorithmOid !== b.rsaEncryption) throw Error("PKCS#1 export requires an RSA private key");
184
187
  return t.privateKeyDer;
185
188
  }
186
189
  /**
@@ -192,7 +195,7 @@ async function exportPkcs1Der(e) {
192
195
  * @see {@linkcode exportEncryptedPkcs1Pem} for password-protected export
193
196
  */
194
197
  async function exportPkcs1Pem(e) {
195
- return k("RSA PRIVATE KEY", await exportPkcs1Der(e));
198
+ return P("RSA PRIVATE KEY", await exportPkcs1Der(e));
196
199
  }
197
200
  /**
198
201
  * Export an RSA private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
@@ -224,7 +227,7 @@ async function exportEncryptedPkcs1Pem(e, t) {
224
227
  */
225
228
  async function exportSec1Der(e) {
226
229
  let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
227
- if (t.algorithmOid !== m.ecPublicKey) throw Error("SEC1 export requires an EC private key");
230
+ if (t.algorithmOid !== b.ecPublicKey) throw Error("SEC1 export requires an EC private key");
228
231
  return ensureSec1NamedCurveParameters(t.privateKeyDer, t.parametersOid);
229
232
  }
230
233
  /**
@@ -236,7 +239,7 @@ async function exportSec1Der(e) {
236
239
  * @see {@linkcode exportEncryptedSec1Pem} for password-protected export
237
240
  */
238
241
  async function exportSec1Pem(e) {
239
- return k("EC PRIVATE KEY", await exportSec1Der(e));
242
+ return P("EC PRIVATE KEY", await exportSec1Der(e));
240
243
  }
241
244
  /**
242
245
  * Export an EC private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
@@ -261,7 +264,7 @@ async function exportEncryptedSec1Pem(e, t) {
261
264
  * ```
262
265
  */
263
266
  async function exportSpkiPem(e) {
264
- return k("PUBLIC KEY", await exportSpkiDer(e));
267
+ return P("PUBLIC KEY", await exportSpkiDer(e));
265
268
  }
266
269
  /**
267
270
  * Derive the matching public key from an imported (or generated) private key.
@@ -296,7 +299,7 @@ async function exportSpkiPem(e) {
296
299
  async function derivePublicKey(e) {
297
300
  if (e.type !== "private") throw Error("derivePublicKey requires a private CryptoKey");
298
301
  if (!e.extractable) throw Error("Cannot derive public key from a non-extractable private key");
299
- let t = g().subtle, n = e.algorithm.name === "RSA-OAEP" ? "encrypt" : "verify", r = await t.exportKey("jwk", e);
302
+ let t = x().subtle, n = e.algorithm.name === "RSA-OAEP" ? "encrypt" : "verify", r = await t.exportKey("jwk", e);
300
303
  return t.importKey("jwk", toPublicJwk(r, n), e.algorithm, !0, [n]);
301
304
  }
302
305
  /**
@@ -311,8 +314,8 @@ async function derivePublicKey(e) {
311
314
  * @see {@linkcode importPkcs8Base64} for private key import
312
315
  */
313
316
  async function exportBinaryBase64(e) {
314
- if (e.type === "public") return C(await exportSpkiDer(e));
315
- if (e.type === "private") return C(await exportPkcs8Der(e));
317
+ if (e.type === "public") return j(await exportSpkiDer(e));
318
+ if (e.type === "private") return j(await exportPkcs8Der(e));
316
319
  throw Error("Cannot export secret/symmetric CryptoKey");
317
320
  }
318
321
  /**
@@ -336,25 +339,25 @@ async function importSpkiDerOrThrow(e, t) {
336
339
  let n = parseSpkiDer(e), r;
337
340
  t === void 0 ? r = inferKeyImportInput(n, "Unsupported SubjectPublicKeyInfo algorithm") : (assertSpkiMatchesRequestedAlgorithm(n, t), r = t);
338
341
  try {
339
- return await g().subtle.importKey("spki", new Uint8Array(e), toImportAlgorithm(r), !0, publicKeyUsages(r));
342
+ return await x().subtle.importKey("spki", new Uint8Array(e), toImportAlgorithm(r), !0, publicKeyUsages(r));
340
343
  } catch {
341
344
  throw Error("Malformed SubjectPublicKeyInfo");
342
345
  }
343
346
  }
344
- /** Runs a throwing key import and maps an EXPECTED failure to a `'malformed'` result; invariants rethrow. */
347
+ /** Runs a throwing key import and maps an EXPECTED failure to a `'malformed'` or `'limit_exceeded'` result; invariants rethrow. */
345
348
  async function importResult(e) {
346
349
  try {
347
- return E(await e());
350
+ return n(await e());
348
351
  } catch (e) {
349
- return T(e), w("malformed", e instanceof Error ? e.message : "Malformed key");
352
+ return t(e), i(e, r, "Malformed key");
350
353
  }
351
354
  }
352
355
  /** Like {@link importResult} but distinguishes a typed wrong-password failure (`'invalid_password'`). */
353
- async function encryptedImportResult(e) {
356
+ async function encryptedImportResult(a) {
354
357
  try {
355
- return E(await e());
356
- } catch (e) {
357
- return T(e), y(e) ? w("invalid_password", e.message) : w("malformed", e instanceof Error ? e.message : "Malformed encrypted key");
358
+ return n(await a());
359
+ } catch (n) {
360
+ return t(n), D(n) ? e("invalid_password", n.message) : E(n) ? e("kdf_iterations_exceeded", n.message) : i(n, r, "Malformed encrypted key");
358
361
  }
359
362
  }
360
363
  /**
@@ -379,7 +382,7 @@ function importSpkiDer(e, t) {
379
382
  * @see {@linkcode exportSpkiPem} for the inverse operation
380
383
  */
381
384
  function importSpkiPemOrThrow(e, t) {
382
- return importSpkiDerOrThrow(O("PUBLIC KEY", e), t);
385
+ return importSpkiDerOrThrow(N("PUBLIC KEY", e), t);
383
386
  }
384
387
  /**
385
388
  * Import a public key from PEM-encoded SubjectPublicKeyInfo.
@@ -398,7 +401,7 @@ function importSpkiPem(e, t) {
398
401
  function importSpkiBase64OrThrow(e, t) {
399
402
  let n;
400
403
  try {
401
- n = S(e);
404
+ n = A(e);
402
405
  } catch {
403
406
  throw Error("Invalid base64 SubjectPublicKeyInfo");
404
407
  }
@@ -440,19 +443,19 @@ async function importPkcs8DerOrThrow(e, t) {
440
443
  let n;
441
444
  try {
442
445
  n = parsePkcs8PrivateKey(e);
443
- } catch {
444
- throw Error("Malformed PKCS#8 private key");
446
+ } catch (e) {
447
+ throw a(e, r), Error("Malformed PKCS#8 private key");
445
448
  }
446
- let r;
447
- t === void 0 ? r = inferKeyImportInput(n, "Unsupported PKCS#8 private key algorithm") : (assertPkcs8MatchesRequestedAlgorithm(n, t), r = t);
448
449
  let i;
450
+ t === void 0 ? i = inferKeyImportInput(n, "Unsupported PKCS#8 private key algorithm") : (assertPkcs8MatchesRequestedAlgorithm(n, t), i = t);
451
+ let o;
449
452
  try {
450
- i = await g().subtle.importKey("pkcs8", new Uint8Array(n.v1Der), toImportAlgorithm(r), !0, privateKeyUsages(r));
453
+ o = await x().subtle.importKey("pkcs8", new Uint8Array(n.v1Der), toImportAlgorithm(i), !0, privateKeyUsages(i));
451
454
  } catch {
452
455
  throw Error("Malformed PKCS#8 private key");
453
456
  }
454
- let a = n.declaredPublicKey;
455
- return a !== void 0 && await assertDeclaredPublicKeyMatches(i, a), i;
457
+ let s = n.declaredPublicKey;
458
+ return s !== void 0 && await assertDeclaredPublicKeyMatches(o, s), o;
456
459
  }
457
460
  /**
458
461
  * Reject a OneAsymmetricKey whose `publicKey [1]` field is not the public key of
@@ -463,10 +466,10 @@ async function importPkcs8DerOrThrow(e, t) {
463
466
  * algorithm was used."
464
467
  */
465
468
  async function assertDeclaredPublicKeyMatches(e, t) {
466
- let n = o(await exportSpkiDer(await derivePublicKey(e)))[1];
467
- if (n === void 0 || n.tag !== 3) throw Error("Malformed PKCS#8 private key");
468
- let r = n.value.slice(1);
469
- if (r.length !== t.length || r.some((e, n) => e !== t[n])) throw Error("PKCS#8 publicKey does not match the private key");
469
+ let n = await exportSpkiDer(await derivePublicKey(e)), r = f(n)[1];
470
+ if (r === void 0 || r.tag !== 3) throw Error("Malformed PKCS#8 private key");
471
+ let i = r.value.slice(1);
472
+ if (i.length !== t.length || i.some((e, n) => e !== t[n])) throw Error("PKCS#8 publicKey does not match the private key");
470
473
  }
471
474
  /**
472
475
  * Import a private key from DER-encoded PKCS#8 PrivateKeyInfo.
@@ -489,7 +492,7 @@ function importPkcs8Der(e, t) {
489
492
  * ```
490
493
  */
491
494
  function importPkcs8PemOrThrow(e, t) {
492
- return importPkcs8DerOrThrow(O("PRIVATE KEY", e), t);
495
+ return importPkcs8DerOrThrow(N("PRIVATE KEY", e), t);
493
496
  }
494
497
  /**
495
498
  * Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
@@ -510,46 +513,52 @@ function importPkcs8Pem(e, t) {
510
513
  * @param der - DER-encoded EncryptedPrivateKeyInfo bytes
511
514
  * @param password - Decryption password
512
515
  * @param algorithm - Optional expected algorithm; must match decrypted key when given
516
+ * @param options - KDF work-factor limit
513
517
  *
514
- * @throws {Error} If DER is malformed, password is wrong, or algorithm doesn't match
518
+ * @throws {Error} If DER is malformed, password is wrong, the PBKDF2 iteration
519
+ * count exceeds `options.maxKdfIterations`, or algorithm doesn't match
515
520
  *
516
521
  * @see {@linkcode exportEncryptedPkcs8Der} for the inverse operation
517
522
  */
518
- async function importEncryptedPkcs8DerOrThrow(e, t, n) {
519
- let r = readEncryptedPkcs8Envelope(e), i = await _(r.algorithmIdentifierDer, r.encryptedData, t);
520
- return assertDecryptedPrivateKey(() => parsePkcs8PrivateKey(i), "Invalid password or encrypted content"), importPkcs8DerOrThrow(i, n);
523
+ async function importEncryptedPkcs8DerOrThrow(e, t, n, r) {
524
+ return await importEncryptedPkcs8WithBudget(e, t, n, C(r));
525
+ }
526
+ async function importEncryptedPkcs8WithBudget(e, t, n, r) {
527
+ let i = readEncryptedPkcs8Envelope(e), a = await w(i.algorithmIdentifierDer, i.encryptedData, t, r);
528
+ return assertDecryptedPrivateKey(() => parsePkcs8PrivateKey(a), "Invalid password or encrypted content"), importPkcs8DerOrThrow(a, n);
521
529
  }
522
530
  /** Splits a DER `EncryptedPrivateKeyInfo` into its AlgorithmIdentifier DER and encryptedData bytes. */
523
531
  function readEncryptedPkcs8Envelope(e) {
524
532
  let t;
525
533
  try {
526
- t = o(e);
527
- } catch {
528
- throw Error("Malformed EncryptedPrivateKeyInfo");
534
+ t = f(e);
535
+ } catch (e) {
536
+ throw a(e, r), Error("Malformed EncryptedPrivateKeyInfo");
529
537
  }
530
- let n = t[0], r = t[1];
531
- if (t.length !== 2 || n === void 0 || r === void 0 || r.tag !== 4) throw Error("Malformed EncryptedPrivateKeyInfo");
538
+ let n = t[0], i = t[1];
539
+ if (t.length !== 2 || n === void 0 || i === void 0 || i.tag !== 4) throw Error("Malformed EncryptedPrivateKeyInfo");
532
540
  return {
533
541
  algorithmIdentifierDer: e.slice(n.start - n.headerLength, n.end),
534
- encryptedData: r.value
542
+ encryptedData: i.value
535
543
  };
536
544
  }
537
545
  /**
538
546
  * Reads the PBES2 encryption parameters of a DER PKCS#8
539
547
  * `EncryptedPrivateKeyInfo` (RFC 5958 §3) without the password: PBKDF2
540
548
  * iteration count, salt, and PRF, plus the AES-CBC variant and IV.
541
- * Throws on malformed DER or a non-PBES2 encryption algorithm.
549
+ * Throws on malformed DER, a non-PBES2 encryption algorithm, or a PBKDF2
550
+ * iteration count outside 1 to 4294967295.
542
551
  */
543
552
  function inspectEncryptedPkcs8Der(e) {
544
- return b(readEncryptedPkcs8Envelope(e).algorithmIdentifierDer);
553
+ return O(readEncryptedPkcs8Envelope(e).algorithmIdentifierDer);
545
554
  }
546
555
  /**
547
556
  * Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
548
557
  *
549
558
  * @see `importEncryptedPkcs8DerOrThrow` for the throwing variant
550
559
  */
551
- function importEncryptedPkcs8Der(e, t, n) {
552
- return encryptedImportResult(() => importEncryptedPkcs8DerOrThrow(e, t, n));
560
+ function importEncryptedPkcs8Der(e, t, n, r) {
561
+ return encryptedImportResult(() => importEncryptedPkcs8DerOrThrow(e, t, n, r));
553
562
  }
554
563
  /**
555
564
  * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -563,16 +572,17 @@ function importEncryptedPkcs8Der(e, t, n) {
563
572
  * const inferred = await importEncryptedPkcs8PemOrThrow(pem, 'secret');
564
573
  * ```
565
574
  */
566
- function importEncryptedPkcs8PemOrThrow(e, t, n) {
567
- return importEncryptedPkcs8DerOrThrow(O("ENCRYPTED PRIVATE KEY", e), t, n);
575
+ async function importEncryptedPkcs8PemOrThrow(e, t, n, r) {
576
+ let i = C(r);
577
+ return await importEncryptedPkcs8WithBudget(N("ENCRYPTED PRIVATE KEY", e), t, n, i);
568
578
  }
569
579
  /**
570
580
  * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
571
581
  *
572
582
  * @see `importEncryptedPkcs8PemOrThrow` for the throwing variant
573
583
  */
574
- function importEncryptedPkcs8Pem(e, t, n) {
575
- return encryptedImportResult(() => importEncryptedPkcs8PemOrThrow(e, t, n));
584
+ function importEncryptedPkcs8Pem(e, t, n, r) {
585
+ return encryptedImportResult(() => importEncryptedPkcs8PemOrThrow(e, t, n, r));
576
586
  }
577
587
  /**
578
588
  * Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey.
@@ -602,7 +612,7 @@ function importPkcs1Der(e, t = { kind: "rsa" }) {
602
612
  * @see {@linkcode importEncryptedPkcs1Pem} for encrypted PEM
603
613
  */
604
614
  function importPkcs1PemOrThrow(e, t = { kind: "rsa" }) {
605
- return importPkcs1DerOrThrow(O("RSA PRIVATE KEY", e), t);
615
+ return importPkcs1DerOrThrow(N("RSA PRIVATE KEY", e), t);
606
616
  }
607
617
  /**
608
618
  * Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
@@ -641,7 +651,7 @@ function importEncryptedPkcs1Pem(e, t, n = { kind: "rsa" }) {
641
651
  function importPkcs8Base64OrThrow(e, t) {
642
652
  let n;
643
653
  try {
644
- n = S(e);
654
+ n = A(e);
645
655
  } catch {
646
656
  throw Error("Invalid base64 PKCS#8 private key");
647
657
  }
@@ -676,11 +686,11 @@ async function importSec1DerOrThrow(e, t) {
676
686
  let n;
677
687
  try {
678
688
  n = parseSec1PrivateKey(e);
679
- } catch {
680
- throw Error("Malformed SEC 1 private key");
689
+ } catch (e) {
690
+ throw a(e, r), Error("Malformed SEC 1 private key");
681
691
  }
682
- let r;
683
- return t === void 0 ? r = inferSec1ImportInput(n) : (assertSec1MatchesRequestedAlgorithm(n, t), r = t), await importPkcs8DerOrThrow(wrapSec1InPkcs8(e, r.curve), r);
692
+ let i;
693
+ return t === void 0 ? i = inferSec1ImportInput(n) : (assertSec1MatchesRequestedAlgorithm(n, t), i = t), await importPkcs8DerOrThrow(wrapSec1InPkcs8(e, i.curve), i);
684
694
  }
685
695
  /**
686
696
  * Import an EC private key from DER-encoded SEC 1 ECPrivateKey.
@@ -701,7 +711,7 @@ function importSec1Der(e, t) {
701
711
  * @see {@linkcode importEncryptedSec1Pem} for encrypted PEM
702
712
  */
703
713
  function importSec1PemOrThrow(e, t) {
704
- return importSec1DerOrThrow(O("EC PRIVATE KEY", e), t);
714
+ return importSec1DerOrThrow(N("EC PRIVATE KEY", e), t);
705
715
  }
706
716
  /**
707
717
  * Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
@@ -751,7 +761,7 @@ async function importPublicJwkOrThrow(e, t) {
751
761
  let n = t ?? inferJwkImportInput(e);
752
762
  assertPublicJwkMatchesRequestedAlgorithm(e, n);
753
763
  try {
754
- return await g().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, publicKeyUsages(n));
764
+ return await x().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, publicKeyUsages(n));
755
765
  } catch {
756
766
  throw Error("Malformed public JWK");
757
767
  }
@@ -790,7 +800,7 @@ async function importPrivateJwkOrThrow(e, t) {
790
800
  let n = t ?? inferJwkImportInput(e);
791
801
  assertPrivateJwkMatchesRequestedAlgorithm(e, n);
792
802
  try {
793
- return await g().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, privateKeyUsages(n));
803
+ return await x().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, privateKeyUsages(n));
794
804
  } catch {
795
805
  throw Error("Malformed private JWK");
796
806
  }
@@ -830,13 +840,13 @@ function importPrivateJwk(e, t) {
830
840
  * @see {@linkcode decryptRsaOaepOrThrow} for the inverse operation
831
841
  * @see `encryptRsaOaep` for the Result-returning variant
832
842
  */
833
- async function encryptRsaOaepOrThrow(e, t, n = {}) {
834
- let r = validateRsaOaepKey(e, "public", "encrypt");
835
- if (r !== void 0) throw Error(r);
843
+ async function encryptRsaOaepOrThrow(e, n, r = {}) {
844
+ let i = validateRsaOaepKey(e, "public", "encrypt");
845
+ if (i !== void 0) throw Error(i);
836
846
  try {
837
- return new Uint8Array(await g().subtle.encrypt(toRsaOaepParams(n), e, f(t)));
847
+ return new Uint8Array(await x().subtle.encrypt(toRsaOaepParams(r), e, v(n)));
838
848
  } catch (e) {
839
- throw T(e), Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
849
+ throw t(e), Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
840
850
  }
841
851
  }
842
852
  /**
@@ -855,13 +865,13 @@ async function encryptRsaOaepOrThrow(e, t, n = {}) {
855
865
  *
856
866
  * @see `encryptRsaOaepOrThrow` for the throwing variant
857
867
  */
858
- async function encryptRsaOaep(e, t, n = {}) {
859
- let r = validateRsaOaepKey(e, "public", "encrypt");
860
- if (r !== void 0) return w("invalid_key", r);
868
+ async function encryptRsaOaep(r, i, a = {}) {
869
+ let o = validateRsaOaepKey(r, "public", "encrypt");
870
+ if (o !== void 0) return e("invalid_key", o);
861
871
  try {
862
- return E(await encryptRsaOaepOrThrow(e, t, n));
863
- } catch (e) {
864
- return T(e), w("message_too_long", e instanceof Error ? e.message : "Plaintext exceeds the RSA-OAEP capacity");
872
+ return n(await encryptRsaOaepOrThrow(r, i, a));
873
+ } catch (n) {
874
+ return t(n), e("message_too_long", n instanceof Error ? n.message : "Plaintext exceeds the RSA-OAEP capacity");
865
875
  }
866
876
  }
867
877
  /**
@@ -886,13 +896,13 @@ async function encryptRsaOaep(e, t, n = {}) {
886
896
  * @see {@linkcode encryptRsaOaepOrThrow} for the inverse operation
887
897
  * @see `decryptRsaOaep` for the Result-returning variant
888
898
  */
889
- async function decryptRsaOaepOrThrow(e, t, n = {}) {
890
- let r = validateRsaOaepKey(e, "private", "decrypt");
891
- if (r !== void 0) throw Error(r);
899
+ async function decryptRsaOaepOrThrow(e, n, r = {}) {
900
+ let i = validateRsaOaepKey(e, "private", "decrypt");
901
+ if (i !== void 0) throw Error(i);
892
902
  try {
893
- return new Uint8Array(await g().subtle.decrypt(toRsaOaepParams(n), e, f(t)));
903
+ return new Uint8Array(await x().subtle.decrypt(toRsaOaepParams(r), e, v(n)));
894
904
  } catch (e) {
895
- throw T(e), Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
905
+ throw t(e), Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
896
906
  }
897
907
  }
898
908
  /**
@@ -910,13 +920,13 @@ async function decryptRsaOaepOrThrow(e, t, n = {}) {
910
920
  *
911
921
  * @see `decryptRsaOaepOrThrow` for the throwing variant
912
922
  */
913
- async function decryptRsaOaep(e, t, n = {}) {
914
- let r = validateRsaOaepKey(e, "private", "decrypt");
915
- if (r !== void 0) return w("invalid_key", r);
923
+ async function decryptRsaOaep(r, i, a = {}) {
924
+ let o = validateRsaOaepKey(r, "private", "decrypt");
925
+ if (o !== void 0) return e("invalid_key", o);
916
926
  try {
917
- return E(await decryptRsaOaepOrThrow(e, t, n));
918
- } catch (e) {
919
- return T(e), w("decryption_failed", e instanceof Error ? e.message : "RSA-OAEP decryption failed");
927
+ return n(await decryptRsaOaepOrThrow(r, i, a));
928
+ } catch (n) {
929
+ return t(n), e("decryption_failed", n instanceof Error ? n.message : "RSA-OAEP decryption failed");
920
930
  }
921
931
  }
922
932
  /** Map a {@linkcode KeyAlgorithmInput} to the WebCrypto `generateKey` algorithm parameter. */
@@ -963,13 +973,13 @@ function toImportAlgorithm(e) {
963
973
  function assertDecryptedPrivateKey(e, t) {
964
974
  try {
965
975
  e();
966
- } catch {
967
- throw x(t);
976
+ } catch (e) {
977
+ throw a(e, r), k(t);
968
978
  }
969
979
  }
970
980
  /** Structural check for a PKCS#1 RSAPrivateKey, including optional multiprime fields. */
971
981
  function parsePkcs1PrivateKey(e) {
972
- let t = o(e), n = t[0];
982
+ let t = f(e), n = t[0];
973
983
  if (n === void 0 || n.tag !== 2 || n.value.length !== 1 || !t.slice(1, 9).every((e) => e.tag === 2)) throw Error("Malformed PKCS#1 private key");
974
984
  let r = n.value[0];
975
985
  if (r === 0 && t.length === 9) return;
@@ -979,41 +989,41 @@ function parsePkcs1PrivateKey(e) {
979
989
  /** RFC 8017 Appendix A.1.2: OtherPrimeInfos is a non-empty SEQUENCE OF three-INTEGER entries. */
980
990
  function isOtherPrimeInfosShape(e, t) {
981
991
  if (t.tag !== 48) return !1;
982
- let n = e.slice(t.start - t.headerLength, t.end), r = o(n);
992
+ let n = e.slice(t.start - t.headerLength, t.end), r = f(n);
983
993
  return r.length !== 0 && r.every((e) => {
984
994
  if (e.tag !== 48) return !1;
985
- let t = o(n.slice(e.start - e.headerLength, e.end));
995
+ let t = f(n.slice(e.start - e.headerLength, e.end));
986
996
  return t.length === 3 && t.every((e) => e.tag === 2);
987
997
  });
988
998
  }
989
999
  /** Extract algorithm OID and inner key bytes from a PKCS#8 PrivateKeyInfo envelope. */
990
1000
  function parsePkcs8PrivateKey(e) {
991
- let n = o(e), r = n[0], a = n[1], c = n[2];
992
- if (n.length < 3 || r === void 0 || r.tag !== 2 || a === void 0 || a.tag !== 48 || c === void 0 || c.tag !== 4) throw Error("Malformed PKCS#8 private key");
993
- let u = validateOneAsymmetricKeyTail(n.slice(3));
994
- if (readPkcs8Version(r.value) !== (u === void 0 ? 0 : 1)) throw Error("Malformed PKCS#8 private key");
995
- let d = e.slice(a.start - a.headerLength, a.end), f = o(d), p = f[0];
996
- if (p === void 0 || p.tag !== 6 || f.length < 1 || f.length > 2) throw Error("Malformed PKCS#8 private key");
997
- let m = f[1], h = l(p.value);
998
- return j.includes(h) && assertCurvePrivateKey(c.value), {
999
- algorithmOid: h,
1000
- ...m === void 0 ? {} : { parametersTag: m.tag },
1001
- ...m?.tag === 6 ? { parametersOid: l(m.value) } : {},
1002
- privateKeyDer: c.value,
1003
- v1Der: n.length === 3 ? e : s([
1004
- t(Uint8Array.of(0)),
1005
- d,
1006
- i(c.value)
1001
+ let t = f(e), n = t[0], r = t[1], i = t[2];
1002
+ if (t.length < 3 || n === void 0 || n.tag !== 2 || r === void 0 || r.tag !== 48 || i === void 0 || i.tag !== 4) throw Error("Malformed PKCS#8 private key");
1003
+ let a = validateOneAsymmetricKeyTail(t.slice(3));
1004
+ if (readPkcs8Version(n.value) !== (a === void 0 ? 0 : 1)) throw Error("Malformed PKCS#8 private key");
1005
+ let o = e.slice(r.start - r.headerLength, r.end), c = f(o), l = c[0];
1006
+ if (l === void 0 || l.tag !== 6 || c.length < 1 || c.length > 2) throw Error("Malformed PKCS#8 private key");
1007
+ let d = c[1], m = h(l.value);
1008
+ return I.includes(m) && assertCurvePrivateKey(i.value), {
1009
+ algorithmOid: m,
1010
+ ...d === void 0 ? {} : { parametersTag: d.tag },
1011
+ ...d?.tag === 6 ? { parametersOid: h(d.value) } : {},
1012
+ privateKeyDer: i.value,
1013
+ v1Der: t.length === 3 ? e : p([
1014
+ s(Uint8Array.of(0)),
1015
+ o,
1016
+ u(i.value)
1007
1017
  ]),
1008
- ...u === void 0 ? {} : { declaredPublicKey: u }
1018
+ ...a === void 0 ? {} : { declaredPublicKey: a }
1009
1019
  };
1010
1020
  }
1011
1021
  /** Algorithms whose `privateKey` field holds an RFC 8410 §7 `CurvePrivateKey`. */
1012
- const j = [
1013
- m.x25519,
1014
- m.x448,
1015
- m.ed25519,
1016
- m.ed448
1022
+ const I = [
1023
+ b.x25519,
1024
+ b.x448,
1025
+ b.ed25519,
1026
+ b.ed448
1017
1027
  ];
1018
1028
  /**
1019
1029
  * Reject a `privateKey` field that does not hold exactly one DER `CurvePrivateKey`.
@@ -1023,7 +1033,7 @@ const j = [
1023
1033
  * 'privateKey' field", and `CurvePrivateKey ::= OCTET STRING`.
1024
1034
  */
1025
1035
  function assertCurvePrivateKey(e) {
1026
- if (a(e).tag !== 4) throw Error("Malformed PKCS#8 private key");
1036
+ if (d(e).tag !== 4) throw Error("Malformed PKCS#8 private key");
1027
1037
  }
1028
1038
  /**
1029
1039
  * Validate the OneAsymmetricKey tail after `privateKey` and return the
@@ -1039,14 +1049,14 @@ function assertCurvePrivateKey(e) {
1039
1049
  function validateOneAsymmetricKeyTail(e) {
1040
1050
  let t = !1, n, r = !1;
1041
1051
  for (let i of e) {
1042
- let e = (i.tag & 192) == 128 ? i.tag & 31 : -1;
1052
+ let e = (i.tag & 192) == 128 ? i.tagNumber : -1;
1043
1053
  if (e === 0) {
1044
1054
  if (t || n !== void 0 || r || i.tag !== 160) throw Error("Malformed PKCS#8 private key");
1045
1055
  assertOneAsymmetricKeyAttributes(i.value), t = !0;
1046
1056
  } else if (e === 1) {
1047
1057
  if (n !== void 0 || r || i.tag !== 129) throw Error("Malformed PKCS#8 private key");
1048
1058
  n = readPublicKeyBitString(i.value);
1049
- } else if (e >= 2 && e <= 30) r = !0;
1059
+ } else if (e >= 2) r = !0;
1050
1060
  else throw Error("Malformed PKCS#8 private key");
1051
1061
  }
1052
1062
  return n;
@@ -1060,10 +1070,10 @@ function validateOneAsymmetricKeyTail(e) {
1060
1070
  * tag, so the members are read back under a universal constructed one.
1061
1071
  */
1062
1072
  function assertOneAsymmetricKeyAttributes(e) {
1063
- let t = c(48, e);
1064
- for (let e of o(t)) {
1073
+ let t = m(48, e);
1074
+ for (let e of f(t)) {
1065
1075
  if (e.tag !== 48) throw Error("Malformed PKCS#8 private key");
1066
- let n = o(t.slice(e.start - e.headerLength, e.end)), r = n[0], i = n[1];
1076
+ let n = f(t.slice(e.start - e.headerLength, e.end)), r = n[0], i = n[1];
1067
1077
  if (n.length !== 2 || r === void 0 || r.tag !== 6 || i === void 0 || i.tag !== 49) throw Error("Malformed PKCS#8 private key");
1068
1078
  }
1069
1079
  }
@@ -1086,14 +1096,14 @@ function readPkcs8Version(e) {
1086
1096
  * {{ NamedCurve }} OPTIONAL, publicKey [1] BIT STRING OPTIONAL }`.
1087
1097
  */
1088
1098
  function parseSec1PrivateKey(e) {
1089
- let t = o(e), n = t[0], r = t[1], i = t[2], s = t[3];
1090
- if (t.length < 2 || t.length > 4 || n === void 0 || n.tag !== 2 || n.value.length !== 1 || n.value[0] !== 1 || r === void 0 || r.tag !== 4 || i !== void 0 && i.tag !== 160 && i.tag !== 161 || s !== void 0 && (i?.tag !== 160 || s.tag !== 161)) throw Error("Malformed SEC 1 private key");
1091
- let c = i?.tag === 160 ? i : void 0;
1092
- if (c === void 0) return {};
1093
- let u = a(c.value);
1099
+ let t = f(e), n = t[0], r = t[1], i = t[2], a = t[3];
1100
+ if (t.length < 2 || t.length > 4 || n === void 0 || n.tag !== 2 || n.value.length !== 1 || n.value[0] !== 1 || r === void 0 || r.tag !== 4 || i !== void 0 && i.tag !== 160 && i.tag !== 161 || a !== void 0 && (i?.tag !== 160 || a.tag !== 161)) throw Error("Malformed SEC 1 private key");
1101
+ let o = i?.tag === 160 ? i : void 0;
1102
+ if (o === void 0) return {};
1103
+ let s = d(o.value);
1094
1104
  return {
1095
- parametersTag: u.tag,
1096
- ...u.tag === 6 ? { parametersOid: l(u.value) } : {}
1105
+ parametersTag: s.tag,
1106
+ ...s.tag === 6 ? { parametersOid: h(s.value) } : {}
1097
1107
  };
1098
1108
  }
1099
1109
  /**
@@ -1103,31 +1113,31 @@ function parseSec1PrivateKey(e) {
1103
1113
  * standalone SEC 1 — and OpenSSL always writes it. Injecting it keeps exports
1104
1114
  * self-describing, so they re-import without an explicit curve.
1105
1115
  */
1106
- function ensureSec1NamedCurveParameters(t, n) {
1107
- let i = o(t);
1108
- if (i.some((e) => e.tag === 160)) return new Uint8Array(t);
1109
- if (n === void 0) throw Error("SEC1 export requires a named curve in the PKCS#8 privateKeyAlgorithm");
1110
- let raw = (e) => t.slice(e.start - e.headerLength, e.end);
1111
- return s([
1112
- ...i.slice(0, 2).map(raw),
1113
- e(0, r(n)),
1114
- ...i.slice(2).map(raw)
1116
+ function ensureSec1NamedCurveParameters(e, t) {
1117
+ let n = f(e);
1118
+ if (n.some((e) => e.tag === 160)) return new Uint8Array(e);
1119
+ if (t === void 0) throw Error("SEC1 export requires a named curve in the PKCS#8 privateKeyAlgorithm");
1120
+ let raw = (t) => e.slice(t.start - t.headerLength, t.end);
1121
+ return p([
1122
+ ...n.slice(0, 2).map(raw),
1123
+ o(0, l(t)),
1124
+ ...n.slice(2).map(raw)
1115
1125
  ]);
1116
1126
  }
1117
1127
  /** Wrap a PKCS#1 RSAPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
1118
1128
  function wrapPkcs1InPkcs8(e) {
1119
- return s([
1129
+ return p([
1120
1130
  Uint8Array.of(2, 1, 0),
1121
- s([r(m.rsaEncryption), n()]),
1122
- i(new Uint8Array(e))
1131
+ p([l(b.rsaEncryption), c()]),
1132
+ u(new Uint8Array(e))
1123
1133
  ]);
1124
1134
  }
1125
1135
  /** Wrap a SEC 1 ECPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
1126
1136
  function wrapSec1InPkcs8(e, t) {
1127
- return s([
1137
+ return p([
1128
1138
  Uint8Array.of(2, 1, 0),
1129
- s([r(m.ecPublicKey), r(curveToOid(t))]),
1130
- i(new Uint8Array(e))
1139
+ p([l(b.ecPublicKey), l(curveToOid(t))]),
1140
+ u(new Uint8Array(e))
1131
1141
  ]);
1132
1142
  }
1133
1143
  /** Project a private JWK onto its public-only members, discarding private key material. */
@@ -1150,19 +1160,19 @@ function toPublicJwk(e, t) {
1150
1160
  * Single source of truth for the NIST curve ↔ named-curve OID mapping, read in
1151
1161
  * both directions by {@linkcode curveToOid} and {@linkcode oidToCurve}.
1152
1162
  */
1153
- const M = [
1154
- ["P-256", m.prime256v1],
1155
- ["P-384", m.secp384r1],
1156
- ["P-521", m.secp521r1]
1163
+ const L = [
1164
+ ["P-256", b.prime256v1],
1165
+ ["P-384", b.secp384r1],
1166
+ ["P-521", b.secp521r1]
1157
1167
  ];
1158
1168
  /** Map a curve name to its ASN.1 OID string. */
1159
1169
  function curveToOid(e) {
1160
- for (let [t, n] of M) if (t === e) return n;
1170
+ for (let [t, n] of L) if (t === e) return n;
1161
1171
  throw Error(`Unsupported EC curve: ${e}`);
1162
1172
  }
1163
1173
  /** Map an ASN.1 curve OID string back to its curve name, or `undefined` if unrecognized. */
1164
1174
  function oidToCurve(e) {
1165
- for (let [t, n] of M) if (n === e) return t;
1175
+ for (let [t, n] of L) if (n === e) return t;
1166
1176
  }
1167
1177
  /** Map an {@linkcode RsaScheme} to the WebCrypto algorithm name string. */
1168
1178
  function rsaSchemeToWebCryptoAlgorithmName(e) {
@@ -1197,49 +1207,49 @@ function validateRsaOaepKey(e, t, n) {
1197
1207
  function toRsaOaepParams(e) {
1198
1208
  return {
1199
1209
  name: "RSA-OAEP",
1200
- ...e.label === void 0 ? {} : { label: f(e.label) }
1210
+ ...e.label === void 0 ? {} : { label: v(e.label) }
1201
1211
  };
1202
1212
  }
1203
1213
  /** Encrypt DER key material as an OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM block. */
1204
1214
  async function encryptTraditionalPem(e, t, n) {
1205
- let r = n.iv ?? g().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
1215
+ let r = n.iv ?? x().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
1206
1216
  if (r.length !== 16) throw Error("Traditional PEM encryption requires a 16-byte IV");
1207
- let i = n.cipher ?? "AES-256-CBC", a = await importTraditionalPemAesKey(n.password, r.slice(0, 8), i, ["encrypt"]), o = C(new Uint8Array(await g().subtle.encrypt({
1217
+ let i = n.cipher ?? "AES-256-CBC", a = await importTraditionalPemAesKey(n.password, r.slice(0, 8), i, ["encrypt"]), o = new Uint8Array(await x().subtle.encrypt({
1208
1218
  name: "AES-CBC",
1209
- iv: f(r)
1210
- }, a, f(t)))).match(/.{1,64}/g)?.join("\n") ?? "";
1219
+ iv: v(r)
1220
+ }, a, v(t))), s = j(o).match(/.{1,64}/g)?.join("\n") ?? "";
1211
1221
  return [
1212
1222
  `-----BEGIN ${e}-----`,
1213
1223
  "Proc-Type: 4,ENCRYPTED",
1214
- `DEK-Info: ${i},${p(r).toUpperCase()}`,
1224
+ `DEK-Info: ${i},${y(r).toUpperCase()}`,
1215
1225
  "",
1216
- o,
1226
+ s,
1217
1227
  `-----END ${e}-----`
1218
1228
  ].join("\n");
1219
1229
  }
1220
1230
  /** Decrypt a `Proc-Type: 4,ENCRYPTED` PEM block, RFC 1421 §4.6 headers, to plaintext DER. */
1221
1231
  async function decryptTraditionalPem(e, t, n) {
1222
- let r = D(t);
1232
+ let r = M(t);
1223
1233
  if (r.label !== e) throw Error(`Expected ${e} PEM block`);
1224
1234
  let i = r.headers.get("DEK-Info"), a = r.headers.get("Proc-Type");
1225
1235
  if (a === void 0 || !isEncryptedProcType(a) || i === void 0) throw Error("Traditional PEM encryption headers missing");
1226
- let [o, s] = i.split(",").map(A);
1236
+ let [o, s] = i.split(",").map(F);
1227
1237
  if (!isTraditionalPemCipher(o) || s === void 0) throw Error("Only AES-128-CBC, AES-192-CBC, and AES-256-CBC traditional PEM encryption is supported");
1228
1238
  if (!isTraditionalPemIvHex(s)) throw Error("Traditional PEM encryption requires a 16-byte IV encoded as 32 hex characters");
1229
- let c = d(s), l = await importTraditionalPemAesKey(n, c.slice(0, 8), o, ["decrypt"]);
1239
+ let c = _(s), l = await importTraditionalPemAesKey(n, c.slice(0, 8), o, ["decrypt"]);
1230
1240
  try {
1231
- return new Uint8Array(await g().subtle.decrypt({
1241
+ return new Uint8Array(await x().subtle.decrypt({
1232
1242
  name: "AES-CBC",
1233
- iv: f(c)
1234
- }, l, f(S(r.base64Body))));
1243
+ iv: v(c)
1244
+ }, l, v(A(r.base64Body))));
1235
1245
  } catch {
1236
- throw x("Invalid password or encrypted PEM content");
1246
+ throw k("Invalid password or encrypted PEM content");
1237
1247
  }
1238
1248
  }
1239
1249
  /** Derive and import an AES-CBC key for legacy PEM encryption using OpenSSL `EVP_BytesToKey`. */
1240
1250
  function importTraditionalPemAesKey(e, t, n, r) {
1241
1251
  let i = traditionalPemCipherKeyLength(n), a = opensslBytesToKey(e, t, i / 8);
1242
- return g().subtle.importKey("raw", f(a), {
1252
+ return x().subtle.importKey("raw", v(a), {
1243
1253
  name: "AES-CBC",
1244
1254
  length: i
1245
1255
  }, !1, r);
@@ -1252,7 +1262,7 @@ function importTraditionalPemAesKey(e, t, n, r) {
1252
1262
  */
1253
1263
  function isEncryptedProcType(e) {
1254
1264
  let t = e.split(",");
1255
- return t.length === 2 && A(t[0] ?? "") === "4" && A(t[1] ?? "") === "ENCRYPTED";
1265
+ return t.length === 2 && F(t[0] ?? "") === "4" && F(t[1] ?? "") === "ENCRYPTED";
1256
1266
  }
1257
1267
  /** Type guard for the three AES-CBC ciphers supported by legacy PEM encryption. */
1258
1268
  function isTraditionalPemCipher(e) {
@@ -1275,7 +1285,7 @@ function opensslBytesToKey(e, t, n) {
1275
1285
  let r = new TextEncoder().encode(e), i = [], a = /* @__PURE__ */ new Uint8Array(), o = 0;
1276
1286
  for (; o < n;) {
1277
1287
  let e = new Uint8Array(a.length + r.length + t.length);
1278
- e.set(a, 0), e.set(r, a.length), e.set(t, a.length + r.length), a = h(e), i.push(a), o += a.length;
1288
+ e.set(a, 0), e.set(r, a.length), e.set(t, a.length + r.length), a = S(e), i.push(a), o += a.length;
1279
1289
  }
1280
1290
  let s = new Uint8Array(n), c = 0;
1281
1291
  for (let e of i) {
@@ -1286,21 +1296,21 @@ function opensslBytesToKey(e, t, n) {
1286
1296
  }
1287
1297
  function parseSpkiDer(e) {
1288
1298
  try {
1289
- let t = o(e);
1299
+ let t = f(e);
1290
1300
  if (t.length !== 2) throw Error("Malformed SubjectPublicKeyInfo");
1291
1301
  let n = t[0], r = t[1];
1292
1302
  if (n === void 0 || n.tag !== 48 || r === void 0 || r.tag !== 3) throw Error("Malformed SubjectPublicKeyInfo");
1293
- u(r);
1294
- let i = o(e.slice(n.start - n.headerLength, n.end)), a = i[0];
1303
+ g(r);
1304
+ let i = f(e.slice(n.start - n.headerLength, n.end)), a = i[0];
1295
1305
  if (a === void 0 || a.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed SubjectPublicKeyInfo");
1296
- let s = i[1];
1306
+ let o = i[1];
1297
1307
  return {
1298
- algorithmOid: l(a.value),
1299
- ...s === void 0 ? {} : { parametersTag: s.tag },
1300
- ...s?.tag === 6 ? { parametersOid: l(s.value) } : {}
1308
+ algorithmOid: h(a.value),
1309
+ ...o === void 0 ? {} : { parametersTag: o.tag },
1310
+ ...o?.tag === 6 ? { parametersOid: h(o.value) } : {}
1301
1311
  };
1302
- } catch {
1303
- throw Error("Malformed SubjectPublicKeyInfo");
1312
+ } catch (e) {
1313
+ throw a(e, r), Error("Malformed SubjectPublicKeyInfo");
1304
1314
  }
1305
1315
  }
1306
1316
  /**
@@ -1313,10 +1323,10 @@ function parseSpkiDer(e) {
1313
1323
  */
1314
1324
  function inferKeyImportInput(e, t) {
1315
1325
  switch (e.algorithmOid) {
1316
- case m.rsaEncryption:
1326
+ case b.rsaEncryption:
1317
1327
  if (e.parametersTag === void 0 || e.parametersTag === 5) return { kind: "rsa" };
1318
1328
  break;
1319
- case m.ecPublicKey: {
1329
+ case b.ecPublicKey: {
1320
1330
  let t = e.parametersTag === 6 && e.parametersOid !== void 0 ? oidToCurve(e.parametersOid) : void 0;
1321
1331
  if (t !== void 0) return {
1322
1332
  kind: "ecdsa",
@@ -1324,9 +1334,7 @@ function inferKeyImportInput(e, t) {
1324
1334
  };
1325
1335
  break;
1326
1336
  }
1327
- case m.ed25519:
1328
- if (e.parametersTag === void 0) return { kind: "ed25519" };
1329
- break;
1337
+ case b.ed25519: if (e.parametersTag === void 0) return { kind: "ed25519" };
1330
1338
  }
1331
1339
  throw Error(t);
1332
1340
  }
@@ -1411,13 +1419,13 @@ function rsaImportInputFromJwkAlg(e) {
1411
1419
  function assertSpkiMatchesRequestedAlgorithm(e, t) {
1412
1420
  switch (t.kind) {
1413
1421
  case "rsa":
1414
- if (e.algorithmOid !== m.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1422
+ if (e.algorithmOid !== b.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1415
1423
  return;
1416
1424
  case "ecdsa":
1417
- if (e.algorithmOid !== m.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1425
+ if (e.algorithmOid !== b.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1418
1426
  return;
1419
1427
  case "ed25519":
1420
- if (e.algorithmOid !== m.ed25519 || e.parametersTag !== void 0) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1428
+ if (e.algorithmOid !== b.ed25519 || e.parametersTag !== void 0) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1421
1429
  return;
1422
1430
  default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(t)}`);
1423
1431
  }
@@ -1425,13 +1433,13 @@ function assertSpkiMatchesRequestedAlgorithm(e, t) {
1425
1433
  function assertPkcs8MatchesRequestedAlgorithm(e, t) {
1426
1434
  switch (t.kind) {
1427
1435
  case "rsa":
1428
- if (e.algorithmOid !== m.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1436
+ if (e.algorithmOid !== b.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1429
1437
  return;
1430
1438
  case "ecdsa":
1431
- if (e.algorithmOid !== m.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1439
+ if (e.algorithmOid !== b.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1432
1440
  return;
1433
1441
  case "ed25519":
1434
- if (e.algorithmOid !== m.ed25519 || e.parametersTag !== void 0) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1442
+ if (e.algorithmOid !== b.ed25519 || e.parametersTag !== void 0) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1435
1443
  return;
1436
1444
  default: throw Error(`Unhandled PrivateKeyImportInput kind: ${String(t)}`);
1437
1445
  }
@@ -1470,7 +1478,6 @@ function assertPrivateJwkMatchesRequestedAlgorithm(e, t) {
1470
1478
  default: throw Error(`Unhandled PrivateKeyImportInput kind: ${String(t)}`);
1471
1479
  }
1472
1480
  }
1473
- //#endregion
1474
- export { decryptRsaOaep, decryptRsaOaepOrThrow, derivePublicKey, encryptRsaOaep, encryptRsaOaepOrThrow, exportBinaryBase64, exportEncryptedPkcs1Pem, exportEncryptedPkcs8Der, exportEncryptedPkcs8Pem, exportEncryptedSec1Pem, exportPkcs1Der, exportPkcs1Pem, exportPkcs8Der, exportPkcs8Pem, exportPrivateJwk, exportPublicJwk, exportSec1Der, exportSec1Pem, exportSpkiDer, exportSpkiPem, generateKeyPair, importEncryptedPkcs1Pem, importEncryptedPkcs1PemOrThrow, importEncryptedPkcs8Der, importEncryptedPkcs8DerOrThrow, importEncryptedPkcs8Pem, importEncryptedPkcs8PemOrThrow, importEncryptedSec1Pem, importEncryptedSec1PemOrThrow, importPkcs1Der, importPkcs1DerOrThrow, importPkcs1Pem, importPkcs1PemOrThrow, importPkcs8Base64, importPkcs8Base64OrThrow, importPkcs8Der, importPkcs8DerOrThrow, importPkcs8Pem, importPkcs8PemOrThrow, importPrivateJwk, importPrivateJwkOrThrow, importPublicJwk, importPublicJwkOrThrow, importSec1Der, importSec1DerOrThrow, importSec1Pem, importSec1PemOrThrow, importSpkiBase64, importSpkiBase64OrThrow, importSpkiDer, importSpkiDerOrThrow, importSpkiPem, importSpkiPemOrThrow, inspectEncryptedPkcs8Der, b as parsePbes2AlgorithmIdentifier };
1481
+ export { decryptRsaOaep, decryptRsaOaepOrThrow, derivePublicKey, encryptRsaOaep, encryptRsaOaepOrThrow, exportBinaryBase64, exportEncryptedPkcs1Pem, exportEncryptedPkcs8Der, exportEncryptedPkcs8Pem, exportEncryptedSec1Pem, exportPkcs1Der, exportPkcs1Pem, exportPkcs8Der, exportPkcs8Pem, exportPrivateJwk, exportPublicJwk, exportSec1Der, exportSec1Pem, exportSpkiDer, exportSpkiPem, generateKeyPair, importEncryptedPkcs1Pem, importEncryptedPkcs1PemOrThrow, importEncryptedPkcs8Der, importEncryptedPkcs8DerOrThrow, importEncryptedPkcs8Pem, importEncryptedPkcs8PemOrThrow, importEncryptedSec1Pem, importEncryptedSec1PemOrThrow, importPkcs1Der, importPkcs1DerOrThrow, importPkcs1Pem, importPkcs1PemOrThrow, importPkcs8Base64, importPkcs8Base64OrThrow, importPkcs8Der, importPkcs8DerOrThrow, importPkcs8Pem, importPkcs8PemOrThrow, importPrivateJwk, importPrivateJwkOrThrow, importPublicJwk, importPublicJwkOrThrow, importSec1Der, importSec1DerOrThrow, importSec1Pem, importSec1PemOrThrow, importSpkiBase64, importSpkiBase64OrThrow, importSpkiDer, importSpkiDerOrThrow, importSpkiPem, importSpkiPemOrThrow, inspectEncryptedPkcs8Der, O as parsePbes2AlgorithmIdentifier };
1475
1482
 
1476
1483
  //# sourceMappingURL=keys.js.map