micro509 0.12.0 → 0.14.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 (123) hide show
  1. package/README.md +78 -30
  2. package/dist/crypto/crypto.d.ts +73 -0
  3. package/dist/crypto/crypto.js +74 -0
  4. package/dist/crypto/crypto.js.map +1 -0
  5. package/dist/crypto.d.ts +4 -0
  6. package/dist/crypto.js +2 -0
  7. package/dist/der/der.d.ts +127 -0
  8. package/dist/der/der.js +171 -0
  9. package/dist/der/der.js.map +1 -0
  10. package/dist/der.d.ts +4 -0
  11. package/dist/der.js +4 -0
  12. package/dist/index.d.ts +13 -9
  13. package/dist/index.js +17 -15
  14. package/dist/internal/asn1/asn1.d.ts +20 -0
  15. package/dist/internal/asn1/asn1.js +80 -37
  16. package/dist/internal/asn1/asn1.js.map +1 -1
  17. package/dist/internal/asn1/der.d.ts +173 -0
  18. package/dist/internal/asn1/der.js +116 -38
  19. package/dist/internal/asn1/der.js.map +1 -1
  20. package/dist/internal/asn1/oids.js +7 -1
  21. package/dist/internal/asn1/oids2.js +9 -2
  22. package/dist/internal/asn1/oids2.js.map +1 -1
  23. package/dist/internal/crypto/algorithm-names.js +4 -0
  24. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  25. package/dist/internal/crypto/pbes2.d.ts +38 -0
  26. package/dist/internal/crypto/pbes2.js +9 -10
  27. package/dist/internal/crypto/pbes2.js.map +1 -1
  28. package/dist/internal/crypto/rsa-pss.js +2 -0
  29. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  30. package/dist/internal/crypto/sig-verify.d.ts +31 -0
  31. package/dist/internal/crypto/sig-verify.js +10 -8
  32. package/dist/internal/crypto/sig-verify.js.map +1 -1
  33. package/dist/internal/shared/base64.js +7 -1
  34. package/dist/internal/shared/base64.js.map +1 -1
  35. package/dist/internal/shared/dn.d.ts +16 -0
  36. package/dist/internal/shared/dn.js +149 -12
  37. package/dist/internal/shared/dn.js.map +1 -1
  38. package/dist/internal/shared/ip.d.ts +69 -0
  39. package/dist/internal/shared/ip.js +18 -1
  40. package/dist/internal/shared/ip.js.map +1 -1
  41. package/dist/internal/shared/rfc3454-tables.js +1782 -0
  42. package/dist/internal/shared/rfc3454-tables.js.map +1 -0
  43. package/dist/internal/verify/name-constraints-engine.js +47 -15
  44. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  45. package/dist/internal/verify/policy-engine.js +85 -94
  46. package/dist/internal/verify/policy-engine.js.map +1 -1
  47. package/dist/internal/verify/verify-path.d.ts +7 -0
  48. package/dist/internal/verify/verify-path.js +49 -45
  49. package/dist/internal/verify/verify-path.js.map +1 -1
  50. package/dist/internal/x509/directory-name.js +24 -0
  51. package/dist/internal/x509/directory-name.js.map +1 -0
  52. package/dist/internal/x509/extension-bits.js +14 -11
  53. package/dist/internal/x509/extension-bits.js.map +1 -1
  54. package/dist/internal/x509/extension-errors.d.ts +6 -0
  55. package/dist/internal/x509/extension-errors.js +20 -0
  56. package/dist/internal/x509/extension-errors.js.map +1 -0
  57. package/dist/internal/x509/extension-registry.js +113 -55
  58. package/dist/internal/x509/extension-registry.js.map +1 -1
  59. package/dist/internal/x509/general-name-tags.js +28 -0
  60. package/dist/internal/x509/general-name-tags.js.map +1 -0
  61. package/dist/internal/x509/general-name.js +97 -0
  62. package/dist/internal/x509/general-name.js.map +1 -0
  63. package/dist/internal/x509/name-fields.js +20 -10
  64. package/dist/internal/x509/name-fields.js.map +1 -1
  65. package/dist/keys/keys.d.ts +16 -2
  66. package/dist/keys/keys.js +311 -170
  67. package/dist/keys/keys.js.map +1 -1
  68. package/dist/keys.d.ts +3 -2
  69. package/dist/keys.js +3 -2
  70. package/dist/pem/pem.d.ts +3 -2
  71. package/dist/pem/pem.js +112 -30
  72. package/dist/pem/pem.js.map +1 -1
  73. package/dist/pkcs/pkcs12-mac.js +3 -3
  74. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  75. package/dist/pkcs/pkcs7.d.ts +85 -21
  76. package/dist/pkcs/pkcs7.js +261 -186
  77. package/dist/pkcs/pkcs7.js.map +1 -1
  78. package/dist/pkcs.d.ts +2 -2
  79. package/dist/result/result.d.ts +18 -10
  80. package/dist/result/result.js +20 -12
  81. package/dist/result/result.js.map +1 -1
  82. package/dist/result.d.ts +2 -2
  83. package/dist/result.js +2 -2
  84. package/dist/revocation/chain.js +154 -103
  85. package/dist/revocation/chain.js.map +1 -1
  86. package/dist/revocation/crl.d.ts +13 -2
  87. package/dist/revocation/crl.js +415 -236
  88. package/dist/revocation/crl.js.map +1 -1
  89. package/dist/revocation/ocsp.d.ts +58 -17
  90. package/dist/revocation/ocsp.js +186 -174
  91. package/dist/revocation/ocsp.js.map +1 -1
  92. package/dist/revocation/revocation.d.ts +4 -4
  93. package/dist/revocation/revocation.js +48 -30
  94. package/dist/revocation/revocation.js.map +1 -1
  95. package/dist/revocation.d.ts +4 -4
  96. package/dist/revocation.js +5 -5
  97. package/dist/verify/identity.d.ts +2 -1
  98. package/dist/verify/identity.js +2 -1
  99. package/dist/verify/identity.js.map +1 -1
  100. package/dist/verify/policy.d.ts +6 -1
  101. package/dist/verify/verify.d.ts +18 -3
  102. package/dist/verify/verify.js +66 -41
  103. package/dist/verify/verify.js.map +1 -1
  104. package/dist/verify.d.ts +2 -1
  105. package/dist/verify.js +4 -3
  106. package/dist/x509/certificate.d.ts +36 -13
  107. package/dist/x509/certificate.js +36 -19
  108. package/dist/x509/certificate.js.map +1 -1
  109. package/dist/x509/csr.d.ts +6 -3
  110. package/dist/x509/extensions.d.ts +59 -22
  111. package/dist/x509/extensions.js +384 -117
  112. package/dist/x509/extensions.js.map +1 -1
  113. package/dist/x509/name-text.js +2 -0
  114. package/dist/x509/name-text.js.map +1 -1
  115. package/dist/x509/name.d.ts +8 -5
  116. package/dist/x509/name.js +17 -12
  117. package/dist/x509/name.js.map +1 -1
  118. package/dist/x509/parse.d.ts +104 -18
  119. package/dist/x509/parse.js +127 -163
  120. package/dist/x509/parse.js.map +1 -1
  121. package/dist/x509.d.ts +8 -5
  122. package/dist/x509.js +10 -8
  123. package/package.json +52 -34
package/dist/keys/keys.js CHANGED
@@ -1,12 +1,12 @@
1
- import { explicitContext as e, nullValue as t, objectIdentifier as n, octetString as r, readRootElement as i, readSequenceChildren as a, sequence as o } from "../internal/asn1/der.js";
2
- import { decodeObjectIdentifier as s, extractBitStringValue as c, hexToBytes as l, toArrayBuffer as u, toHex as d } from "../internal/asn1/asn1.js";
3
- import { OIDS as f } from "../internal/asn1/oids2.js";
4
- import { md5 as p } from "../internal/crypto/hash.js";
5
- import { getCrypto as m } from "../internal/crypto/webcrypto.js";
6
- import { decryptPbes2 as h, encryptPbes2 as g, isWrongPasswordError as _, wrongPasswordError as v } from "../internal/crypto/pbes2.js";
7
- import { base64Decode as y, base64Encode as b } from "../internal/shared/base64.js";
8
- import { failureResult as x, rethrowIfInvariant as S, successResult as C } from "../result/result.js";
9
- import { pemDecodeOrThrow as w, pemEncode as T } from "../pem/pem.js";
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
10
  //#region src/keys/keys.ts
11
11
  /**
12
12
  * WebCrypto key generation plus import/export for PKCS#1, PKCS#8, SEC1, SPKI, and JWK.
@@ -49,7 +49,7 @@ async function generateKeyPair(e = {
49
49
  kind: "ecdsa",
50
50
  curve: "P-256"
51
51
  }) {
52
- let t = m().subtle, n = isRsaOaepInput(e) ? ["encrypt", "decrypt"] : ["sign", "verify"], r = await t.generateKey(toGenerateKeyAlgorithm(e), !0, n);
52
+ let t = g().subtle, n = isRsaOaepInput(e) ? ["encrypt", "decrypt"] : ["sign", "verify"], r = await t.generateKey(toGenerateKeyAlgorithm(e), !0, n);
53
53
  if (!("publicKey" in r) || !("privateKey" in r)) throw Error("Expected an asymmetric key pair");
54
54
  return wrapKeyPair(r.publicKey, r.privateKey);
55
55
  }
@@ -58,22 +58,22 @@ function wrapKeyPair(e, t) {
58
58
  publicKey: e,
59
59
  privateKey: t,
60
60
  async exportSpkiDer() {
61
- return new Uint8Array(await m().subtle.exportKey("spki", e));
61
+ return new Uint8Array(await g().subtle.exportKey("spki", e));
62
62
  },
63
63
  async exportSpkiPem() {
64
- return T("PUBLIC KEY", await this.exportSpkiDer());
64
+ return k("PUBLIC KEY", await this.exportSpkiDer());
65
65
  },
66
66
  async exportPkcs8Der() {
67
- return new Uint8Array(await m().subtle.exportKey("pkcs8", t));
67
+ return new Uint8Array(await g().subtle.exportKey("pkcs8", t));
68
68
  },
69
69
  async exportPkcs8Pem() {
70
- return T("PRIVATE KEY", await this.exportPkcs8Der());
70
+ return k("PRIVATE KEY", await this.exportPkcs8Der());
71
71
  },
72
72
  exportPublicJwk() {
73
- return m().subtle.exportKey("jwk", e);
73
+ return g().subtle.exportKey("jwk", e);
74
74
  },
75
75
  exportPrivateJwk() {
76
- return m().subtle.exportKey("jwk", t);
76
+ return g().subtle.exportKey("jwk", t);
77
77
  }
78
78
  };
79
79
  }
@@ -84,7 +84,7 @@ function wrapKeyPair(e, t) {
84
84
  * @see {@linkcode exportSpkiPem} for PEM output
85
85
  */
86
86
  async function exportSpkiDer(e) {
87
- return new Uint8Array(await m().subtle.exportKey("spki", e));
87
+ return new Uint8Array(await g().subtle.exportKey("spki", e));
88
88
  }
89
89
  /**
90
90
  * Export a private key as DER-encoded PKCS#8 PrivateKeyInfo.
@@ -94,7 +94,7 @@ async function exportSpkiDer(e) {
94
94
  * @see {@linkcode exportEncryptedPkcs8Der} for password-protected export
95
95
  */
96
96
  async function exportPkcs8Der(e) {
97
- return new Uint8Array(await m().subtle.exportKey("pkcs8", e));
97
+ return new Uint8Array(await g().subtle.exportKey("pkcs8", e));
98
98
  }
99
99
  /**
100
100
  * Export a public key as a JSON Web Key.
@@ -106,7 +106,7 @@ async function exportPkcs8Der(e) {
106
106
  * ```
107
107
  */
108
108
  function exportPublicJwk(e) {
109
- return m().subtle.exportKey("jwk", e);
109
+ return g().subtle.exportKey("jwk", e);
110
110
  }
111
111
  /**
112
112
  * Export a private key as a JSON Web Key.
@@ -115,7 +115,7 @@ function exportPublicJwk(e) {
115
115
  * @see {@linkcode exportPublicJwk} for public key export
116
116
  */
117
117
  function exportPrivateJwk(e) {
118
- return m().subtle.exportKey("jwk", e);
118
+ return g().subtle.exportKey("jwk", e);
119
119
  }
120
120
  /**
121
121
  * Export a private key as PEM-encoded PKCS#8 PrivateKeyInfo.
@@ -133,7 +133,7 @@ function exportPrivateJwk(e) {
133
133
  * @see {@linkcode exportEncryptedPkcs8Pem} for password-protected export
134
134
  */
135
135
  async function exportPkcs8Pem(e) {
136
- return T("PRIVATE KEY", await exportPkcs8Der(e));
136
+ return k("PRIVATE KEY", await exportPkcs8Der(e));
137
137
  }
138
138
  /**
139
139
  * Export a private key as DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -147,8 +147,8 @@ async function exportPkcs8Pem(e) {
147
147
  * @see {@linkcode exportEncryptedPkcs8Pem} for PEM output
148
148
  */
149
149
  async function exportEncryptedPkcs8Der(e, t) {
150
- let n = await g(await exportPkcs8Der(e), t);
151
- return o([n.algorithmIdentifierDer, r(n.encryptedData)]);
150
+ let n = await v(await exportPkcs8Der(e), t);
151
+ return s([n.algorithmIdentifierDer, i(n.encryptedData)]);
152
152
  }
153
153
  /**
154
154
  * Export a private key as PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -165,7 +165,7 @@ async function exportEncryptedPkcs8Der(e, t) {
165
165
  * @see {@linkcode importEncryptedPkcs8Pem} for the inverse operation
166
166
  */
167
167
  async function exportEncryptedPkcs8Pem(e, t) {
168
- return T("ENCRYPTED PRIVATE KEY", await exportEncryptedPkcs8Der(e, t));
168
+ return k("ENCRYPTED PRIVATE KEY", await exportEncryptedPkcs8Der(e, t));
169
169
  }
170
170
  /**
171
171
  * Export an RSA private key as DER-encoded PKCS#1 RSAPrivateKey.
@@ -180,7 +180,7 @@ async function exportEncryptedPkcs8Pem(e, t) {
180
180
  */
181
181
  async function exportPkcs1Der(e) {
182
182
  let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
183
- if (t.algorithmOid !== f.rsaEncryption) throw Error("PKCS#1 export requires an RSA private key");
183
+ if (t.algorithmOid !== m.rsaEncryption) throw Error("PKCS#1 export requires an RSA private key");
184
184
  return t.privateKeyDer;
185
185
  }
186
186
  /**
@@ -192,7 +192,7 @@ async function exportPkcs1Der(e) {
192
192
  * @see {@linkcode exportEncryptedPkcs1Pem} for password-protected export
193
193
  */
194
194
  async function exportPkcs1Pem(e) {
195
- return T("RSA PRIVATE KEY", await exportPkcs1Der(e));
195
+ return k("RSA PRIVATE KEY", await exportPkcs1Der(e));
196
196
  }
197
197
  /**
198
198
  * Export an RSA private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
@@ -224,7 +224,7 @@ async function exportEncryptedPkcs1Pem(e, t) {
224
224
  */
225
225
  async function exportSec1Der(e) {
226
226
  let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
227
- if (t.algorithmOid !== f.ecPublicKey) throw Error("SEC1 export requires an EC private key");
227
+ if (t.algorithmOid !== m.ecPublicKey) throw Error("SEC1 export requires an EC private key");
228
228
  return ensureSec1NamedCurveParameters(t.privateKeyDer, t.parametersOid);
229
229
  }
230
230
  /**
@@ -236,7 +236,7 @@ async function exportSec1Der(e) {
236
236
  * @see {@linkcode exportEncryptedSec1Pem} for password-protected export
237
237
  */
238
238
  async function exportSec1Pem(e) {
239
- return T("EC PRIVATE KEY", await exportSec1Der(e));
239
+ return k("EC PRIVATE KEY", await exportSec1Der(e));
240
240
  }
241
241
  /**
242
242
  * Export an EC private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
@@ -261,7 +261,7 @@ async function exportEncryptedSec1Pem(e, t) {
261
261
  * ```
262
262
  */
263
263
  async function exportSpkiPem(e) {
264
- return T("PUBLIC KEY", await exportSpkiDer(e));
264
+ return k("PUBLIC KEY", await exportSpkiDer(e));
265
265
  }
266
266
  /**
267
267
  * Derive the matching public key from an imported (or generated) private key.
@@ -296,7 +296,7 @@ async function exportSpkiPem(e) {
296
296
  async function derivePublicKey(e) {
297
297
  if (e.type !== "private") throw Error("derivePublicKey requires a private CryptoKey");
298
298
  if (!e.extractable) throw Error("Cannot derive public key from a non-extractable private key");
299
- let t = m().subtle, n = e.algorithm.name === "RSA-OAEP" ? "encrypt" : "verify", r = await t.exportKey("jwk", e);
299
+ let t = g().subtle, n = e.algorithm.name === "RSA-OAEP" ? "encrypt" : "verify", r = await t.exportKey("jwk", e);
300
300
  return t.importKey("jwk", toPublicJwk(r, n), e.algorithm, !0, [n]);
301
301
  }
302
302
  /**
@@ -311,8 +311,8 @@ async function derivePublicKey(e) {
311
311
  * @see {@linkcode importPkcs8Base64} for private key import
312
312
  */
313
313
  async function exportBinaryBase64(e) {
314
- if (e.type === "public") return b(await exportSpkiDer(e));
315
- if (e.type === "private") return b(await exportPkcs8Der(e));
314
+ if (e.type === "public") return C(await exportSpkiDer(e));
315
+ if (e.type === "private") return C(await exportPkcs8Der(e));
316
316
  throw Error("Cannot export secret/symmetric CryptoKey");
317
317
  }
318
318
  /**
@@ -336,7 +336,7 @@ async function importSpkiDerOrThrow(e, t) {
336
336
  let n = parseSpkiDer(e), r;
337
337
  t === void 0 ? r = inferKeyImportInput(n, "Unsupported SubjectPublicKeyInfo algorithm") : (assertSpkiMatchesRequestedAlgorithm(n, t), r = t);
338
338
  try {
339
- return await m().subtle.importKey("spki", new Uint8Array(e), toImportAlgorithm(r), !0, publicKeyUsages(r));
339
+ return await g().subtle.importKey("spki", new Uint8Array(e), toImportAlgorithm(r), !0, publicKeyUsages(r));
340
340
  } catch {
341
341
  throw Error("Malformed SubjectPublicKeyInfo");
342
342
  }
@@ -344,17 +344,17 @@ async function importSpkiDerOrThrow(e, t) {
344
344
  /** Runs a throwing key import and maps an EXPECTED failure to a `'malformed'` result; invariants rethrow. */
345
345
  async function importResult(e) {
346
346
  try {
347
- return C(await e());
347
+ return E(await e());
348
348
  } catch (e) {
349
- return S(e), x("malformed", e instanceof Error ? e.message : "Malformed key");
349
+ return T(e), w("malformed", e instanceof Error ? e.message : "Malformed key");
350
350
  }
351
351
  }
352
352
  /** Like {@link importResult} but distinguishes a typed wrong-password failure (`'invalid_password'`). */
353
353
  async function encryptedImportResult(e) {
354
354
  try {
355
- return C(await e());
355
+ return E(await e());
356
356
  } catch (e) {
357
- return S(e), _(e) ? x("invalid_password", e.message) : x("malformed", e instanceof Error ? e.message : "Malformed encrypted key");
357
+ return T(e), y(e) ? w("invalid_password", e.message) : w("malformed", e instanceof Error ? e.message : "Malformed encrypted key");
358
358
  }
359
359
  }
360
360
  /**
@@ -379,7 +379,7 @@ function importSpkiDer(e, t) {
379
379
  * @see {@linkcode exportSpkiPem} for the inverse operation
380
380
  */
381
381
  function importSpkiPemOrThrow(e, t) {
382
- return importSpkiDerOrThrow(w("PUBLIC KEY", e), t);
382
+ return importSpkiDerOrThrow(O("PUBLIC KEY", e), t);
383
383
  }
384
384
  /**
385
385
  * Import a public key from PEM-encoded SubjectPublicKeyInfo.
@@ -398,7 +398,7 @@ function importSpkiPem(e, t) {
398
398
  function importSpkiBase64OrThrow(e, t) {
399
399
  let n;
400
400
  try {
401
- n = y(e);
401
+ n = S(e);
402
402
  } catch {
403
403
  throw Error("Invalid base64 SubjectPublicKeyInfo");
404
404
  }
@@ -420,11 +420,17 @@ function importSpkiBase64(e, t) {
420
420
  * keys whose type isn't known ahead of time. Pass `algorithm` to additionally
421
421
  * assert that the DER matches an expected algorithm.
422
422
  *
423
+ * An RFC 5958 v2 `OneAsymmetricKey` carrying `attributes [0]` or `publicKey [1]`
424
+ * is reduced to its v1 fields for the platform import, which Bun 1.3.14 and
425
+ * earlier rejects outright
426
+ * ([oven-sh/bun#35432](https://github.com/oven-sh/bun/issues/35432)). A
427
+ * `publicKey [1]` field is checked against the private key it accompanies.
428
+ *
423
429
  * @param der - DER-encoded PKCS#8 PrivateKeyInfo bytes
424
430
  * @param algorithm - Optional expected algorithm; must match key contents when given
425
431
  * @returns Extractable CryptoKey with `sign` usage
426
432
  *
427
- * @throws {Error} If DER is malformed, encodes an unsupported algorithm, or doesn't match `algorithm`
433
+ * @throws {Error} If DER is malformed, encodes an unsupported algorithm, doesn't match `algorithm`, or carries a `publicKey [1]` that is not the private key's own
428
434
  *
429
435
  * @see {@linkcode exportPkcs8Der} for the inverse operation
430
436
  * @see {@linkcode importPkcs8Pem} for PEM input
@@ -439,11 +445,28 @@ async function importPkcs8DerOrThrow(e, t) {
439
445
  }
440
446
  let r;
441
447
  t === void 0 ? r = inferKeyImportInput(n, "Unsupported PKCS#8 private key algorithm") : (assertPkcs8MatchesRequestedAlgorithm(n, t), r = t);
448
+ let i;
442
449
  try {
443
- return await m().subtle.importKey("pkcs8", new Uint8Array(e), toImportAlgorithm(r), !0, privateKeyUsages(r));
450
+ i = await g().subtle.importKey("pkcs8", new Uint8Array(n.v1Der), toImportAlgorithm(r), !0, privateKeyUsages(r));
444
451
  } catch {
445
452
  throw Error("Malformed PKCS#8 private key");
446
453
  }
454
+ let a = n.declaredPublicKey;
455
+ return a !== void 0 && await assertDeclaredPublicKeyMatches(i, a), i;
456
+ }
457
+ /**
458
+ * Reject a OneAsymmetricKey whose `publicKey [1]` field is not the public key of
459
+ * its own `privateKey`.
460
+ *
461
+ * RFC 8410 Appendix A: "Key mismatch errors: If a public key is provided, it may
462
+ * not agree with the private key because either it is wrong or the wrong
463
+ * algorithm was used."
464
+ */
465
+ 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");
447
470
  }
448
471
  /**
449
472
  * Import a private key from DER-encoded PKCS#8 PrivateKeyInfo.
@@ -466,7 +489,7 @@ function importPkcs8Der(e, t) {
466
489
  * ```
467
490
  */
468
491
  function importPkcs8PemOrThrow(e, t) {
469
- return importPkcs8DerOrThrow(w("PRIVATE KEY", e), t);
492
+ return importPkcs8DerOrThrow(O("PRIVATE KEY", e), t);
470
493
  }
471
494
  /**
472
495
  * Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
@@ -493,21 +516,32 @@ function importPkcs8Pem(e, t) {
493
516
  * @see {@linkcode exportEncryptedPkcs8Der} for the inverse operation
494
517
  */
495
518
  async function importEncryptedPkcs8DerOrThrow(e, t, n) {
496
- let r;
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);
521
+ }
522
+ /** Splits a DER `EncryptedPrivateKeyInfo` into its AlgorithmIdentifier DER and encryptedData bytes. */
523
+ function readEncryptedPkcs8Envelope(e) {
524
+ let t;
497
525
  try {
498
- r = a(e);
526
+ t = o(e);
499
527
  } catch {
500
528
  throw Error("Malformed EncryptedPrivateKeyInfo");
501
529
  }
502
- let i = r[0], o = r[1];
503
- if (r.length !== 2 || i === void 0 || o === void 0 || o.tag !== 4) throw Error("Malformed EncryptedPrivateKeyInfo");
504
- let s = await h(e.slice(i.start - i.headerLength, i.end), o.value, t);
505
- try {
506
- parsePkcs8PrivateKey(s);
507
- } catch {
508
- throw v("Invalid password or encrypted content");
509
- }
510
- return importPkcs8DerOrThrow(s, n);
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");
532
+ return {
533
+ algorithmIdentifierDer: e.slice(n.start - n.headerLength, n.end),
534
+ encryptedData: r.value
535
+ };
536
+ }
537
+ /**
538
+ * Reads the PBES2 encryption parameters of a DER PKCS#8
539
+ * `EncryptedPrivateKeyInfo` (RFC 5958 §3) without the password: PBKDF2
540
+ * iteration count, salt, and PRF, plus the AES-CBC variant and IV.
541
+ * Throws on malformed DER or a non-PBES2 encryption algorithm.
542
+ */
543
+ function inspectEncryptedPkcs8Der(e) {
544
+ return b(readEncryptedPkcs8Envelope(e).algorithmIdentifierDer);
511
545
  }
512
546
  /**
513
547
  * Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -530,7 +564,7 @@ function importEncryptedPkcs8Der(e, t, n) {
530
564
  * ```
531
565
  */
532
566
  function importEncryptedPkcs8PemOrThrow(e, t, n) {
533
- return importEncryptedPkcs8DerOrThrow(w("ENCRYPTED PRIVATE KEY", e), t, n);
567
+ return importEncryptedPkcs8DerOrThrow(O("ENCRYPTED PRIVATE KEY", e), t, n);
534
568
  }
535
569
  /**
536
570
  * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
@@ -568,7 +602,7 @@ function importPkcs1Der(e, t = { kind: "rsa" }) {
568
602
  * @see {@linkcode importEncryptedPkcs1Pem} for encrypted PEM
569
603
  */
570
604
  function importPkcs1PemOrThrow(e, t = { kind: "rsa" }) {
571
- return importPkcs1DerOrThrow(w("RSA PRIVATE KEY", e), t);
605
+ return importPkcs1DerOrThrow(O("RSA PRIVATE KEY", e), t);
572
606
  }
573
607
  /**
574
608
  * Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
@@ -587,7 +621,8 @@ function importPkcs1Pem(e, t = { kind: "rsa" }) {
587
621
  * @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
588
622
  */
589
623
  async function importEncryptedPkcs1PemOrThrow(e, t, n = { kind: "rsa" }) {
590
- return importPkcs1DerOrThrow(await decryptTraditionalPem("RSA PRIVATE KEY", e, t), n);
624
+ let r = await decryptTraditionalPem("RSA PRIVATE KEY", e, t);
625
+ return assertDecryptedPrivateKey(() => parsePkcs1PrivateKey(r), "Invalid password or encrypted PEM content"), importPkcs1DerOrThrow(r, n);
591
626
  }
592
627
  /**
593
628
  * Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
@@ -606,7 +641,7 @@ function importEncryptedPkcs1Pem(e, t, n = { kind: "rsa" }) {
606
641
  function importPkcs8Base64OrThrow(e, t) {
607
642
  let n;
608
643
  try {
609
- n = y(e);
644
+ n = S(e);
610
645
  } catch {
611
646
  throw Error("Invalid base64 PKCS#8 private key");
612
647
  }
@@ -666,7 +701,7 @@ function importSec1Der(e, t) {
666
701
  * @see {@linkcode importEncryptedSec1Pem} for encrypted PEM
667
702
  */
668
703
  function importSec1PemOrThrow(e, t) {
669
- return importSec1DerOrThrow(w("EC PRIVATE KEY", e), t);
704
+ return importSec1DerOrThrow(O("EC PRIVATE KEY", e), t);
670
705
  }
671
706
  /**
672
707
  * Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
@@ -685,7 +720,8 @@ function importSec1Pem(e, t) {
685
720
  * @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
686
721
  */
687
722
  async function importEncryptedSec1PemOrThrow(e, t, n) {
688
- return importSec1DerOrThrow(await decryptTraditionalPem("EC PRIVATE KEY", e, t), n);
723
+ let r = await decryptTraditionalPem("EC PRIVATE KEY", e, t);
724
+ return assertDecryptedPrivateKey(() => parseSec1PrivateKey(r), "Invalid password or encrypted PEM content"), importSec1DerOrThrow(r, n);
689
725
  }
690
726
  /**
691
727
  * Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
@@ -715,7 +751,7 @@ async function importPublicJwkOrThrow(e, t) {
715
751
  let n = t ?? inferJwkImportInput(e);
716
752
  assertPublicJwkMatchesRequestedAlgorithm(e, n);
717
753
  try {
718
- return await m().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, publicKeyUsages(n));
754
+ return await g().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, publicKeyUsages(n));
719
755
  } catch {
720
756
  throw Error("Malformed public JWK");
721
757
  }
@@ -754,7 +790,7 @@ async function importPrivateJwkOrThrow(e, t) {
754
790
  let n = t ?? inferJwkImportInput(e);
755
791
  assertPrivateJwkMatchesRequestedAlgorithm(e, n);
756
792
  try {
757
- return await m().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, privateKeyUsages(n));
793
+ return await g().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, privateKeyUsages(n));
758
794
  } catch {
759
795
  throw Error("Malformed private JWK");
760
796
  }
@@ -798,9 +834,9 @@ async function encryptRsaOaepOrThrow(e, t, n = {}) {
798
834
  let r = validateRsaOaepKey(e, "public", "encrypt");
799
835
  if (r !== void 0) throw Error(r);
800
836
  try {
801
- return new Uint8Array(await m().subtle.encrypt(toRsaOaepParams(n), e, u(t)));
837
+ return new Uint8Array(await g().subtle.encrypt(toRsaOaepParams(n), e, f(t)));
802
838
  } catch (e) {
803
- throw S(e), Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
839
+ throw T(e), Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
804
840
  }
805
841
  }
806
842
  /**
@@ -821,11 +857,11 @@ async function encryptRsaOaepOrThrow(e, t, n = {}) {
821
857
  */
822
858
  async function encryptRsaOaep(e, t, n = {}) {
823
859
  let r = validateRsaOaepKey(e, "public", "encrypt");
824
- if (r !== void 0) return x("invalid_key", r);
860
+ if (r !== void 0) return w("invalid_key", r);
825
861
  try {
826
- return C(await encryptRsaOaepOrThrow(e, t, n));
862
+ return E(await encryptRsaOaepOrThrow(e, t, n));
827
863
  } catch (e) {
828
- return S(e), x("message_too_long", e instanceof Error ? e.message : "Plaintext exceeds the RSA-OAEP capacity");
864
+ return T(e), w("message_too_long", e instanceof Error ? e.message : "Plaintext exceeds the RSA-OAEP capacity");
829
865
  }
830
866
  }
831
867
  /**
@@ -854,9 +890,9 @@ async function decryptRsaOaepOrThrow(e, t, n = {}) {
854
890
  let r = validateRsaOaepKey(e, "private", "decrypt");
855
891
  if (r !== void 0) throw Error(r);
856
892
  try {
857
- return new Uint8Array(await m().subtle.decrypt(toRsaOaepParams(n), e, u(t)));
893
+ return new Uint8Array(await g().subtle.decrypt(toRsaOaepParams(n), e, f(t)));
858
894
  } catch (e) {
859
- throw S(e), Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
895
+ throw T(e), Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
860
896
  }
861
897
  }
862
898
  /**
@@ -876,11 +912,11 @@ async function decryptRsaOaepOrThrow(e, t, n = {}) {
876
912
  */
877
913
  async function decryptRsaOaep(e, t, n = {}) {
878
914
  let r = validateRsaOaepKey(e, "private", "decrypt");
879
- if (r !== void 0) return x("invalid_key", r);
915
+ if (r !== void 0) return w("invalid_key", r);
880
916
  try {
881
- return C(await decryptRsaOaepOrThrow(e, t, n));
917
+ return E(await decryptRsaOaepOrThrow(e, t, n));
882
918
  } catch (e) {
883
- return S(e), x("decryption_failed", e instanceof Error ? e.message : "RSA-OAEP decryption failed");
919
+ return T(e), w("decryption_failed", e instanceof Error ? e.message : "RSA-OAEP decryption failed");
884
920
  }
885
921
  }
886
922
  /** Map a {@linkcode KeyAlgorithmInput} to the WebCrypto `generateKey` algorithm parameter. */
@@ -897,6 +933,7 @@ function toGenerateKeyAlgorithm(e) {
897
933
  namedCurve: e.curve ?? "P-256"
898
934
  };
899
935
  case "ed25519": return { name: "Ed25519" };
936
+ default: throw Error(`Unhandled KeyAlgorithmInput kind: ${String(e)}`);
900
937
  }
901
938
  }
902
939
  /** Map a {@linkcode PublicKeyImportInput} to the WebCrypto `importKey` algorithm parameter. */
@@ -911,37 +948,152 @@ function toImportAlgorithm(e) {
911
948
  namedCurve: e.curve
912
949
  };
913
950
  case "ed25519": return { name: "Ed25519" };
951
+ default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(e)}`);
914
952
  }
915
953
  }
954
+ /**
955
+ * Rejects decrypted plaintext that is not the private-key structure it should be.
956
+ *
957
+ * PBES2 and traditional PEM both encrypt with unauthenticated AES-CBC, where a
958
+ * wrong key clears the padding check roughly once in every 256 attempts and
959
+ * yields random plaintext. Structure is the only integrity signal left, so
960
+ * plaintext that fails to parse means the password was wrong or the ciphertext
961
+ * was corrupted, not that the enclosing input was malformed.
962
+ */
963
+ function assertDecryptedPrivateKey(e, t) {
964
+ try {
965
+ e();
966
+ } catch {
967
+ throw x(t);
968
+ }
969
+ }
970
+ /** Structural check for a PKCS#1 RSAPrivateKey, including optional multiprime fields. */
971
+ function parsePkcs1PrivateKey(e) {
972
+ let t = o(e), n = t[0];
973
+ 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
+ let r = n.value[0];
975
+ if (r === 0 && t.length === 9) return;
976
+ let i = t[9];
977
+ if (r !== 1 || t.length !== 10 || i === void 0 || !isOtherPrimeInfosShape(e, i)) throw Error("Malformed PKCS#1 private key");
978
+ }
979
+ /** RFC 8017 Appendix A.1.2: OtherPrimeInfos is a non-empty SEQUENCE OF three-INTEGER entries. */
980
+ function isOtherPrimeInfosShape(e, t) {
981
+ if (t.tag !== 48) return !1;
982
+ let n = e.slice(t.start - t.headerLength, t.end), r = o(n);
983
+ return r.length !== 0 && r.every((e) => {
984
+ if (e.tag !== 48) return !1;
985
+ let t = o(n.slice(e.start - e.headerLength, e.end));
986
+ return t.length === 3 && t.every((e) => e.tag === 2);
987
+ });
988
+ }
916
989
  /** Extract algorithm OID and inner key bytes from a PKCS#8 PrivateKeyInfo envelope. */
917
990
  function parsePkcs8PrivateKey(e) {
918
- let t = a(e), n = t[1], r = t[2];
919
- if (t.length < 3 || t.length > 4 || n === void 0 || n.tag !== 48 || r === void 0 || r.tag !== 4) throw Error("Malformed PKCS#8 private key");
920
- let i = a(e.slice(n.start - n.headerLength, n.end)), o = i[0];
921
- if (o === void 0 || o.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed PKCS#8 private key");
922
- let c = i[1];
923
- return {
924
- algorithmOid: s(o.value),
925
- ...c === void 0 ? {} : { parametersTag: c.tag },
926
- ...c?.tag === 6 ? { parametersOid: s(c.value) } : {},
927
- privateKeyDer: r.value
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)
1007
+ ]),
1008
+ ...u === void 0 ? {} : { declaredPublicKey: u }
928
1009
  };
929
1010
  }
1011
+ /** 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
1017
+ ];
1018
+ /**
1019
+ * Reject a `privateKey` field that does not hold exactly one DER `CurvePrivateKey`.
1020
+ *
1021
+ * RFC 8410 §7: "when encoding a OneAsymmetricKey object, the private key is
1022
+ * wrapped in a CurvePrivateKey object and wrapped by the OCTET STRING of the
1023
+ * 'privateKey' field", and `CurvePrivateKey ::= OCTET STRING`.
1024
+ */
1025
+ function assertCurvePrivateKey(e) {
1026
+ if (a(e).tag !== 4) throw Error("Malformed PKCS#8 private key");
1027
+ }
1028
+ /**
1029
+ * Validate the OneAsymmetricKey tail after `privateKey` and return the
1030
+ * `publicKey [1]` octets when the field is present.
1031
+ *
1032
+ * RFC 5958 §2 encodes `attributes [0]` as an IMPLICIT constructed `SET OF`
1033
+ * (tag `A0`); RFC 8410 §7 adds `publicKey [1]` as an IMPLICIT primitive
1034
+ * `BIT STRING` (tag `81`) after it. Each appears at most once and in that order.
1035
+ * The type's two X.680 extensibility markers admit later additions, which take
1036
+ * the next context-specific numbers, so `[2]` and above are tolerated and every
1037
+ * other class is malformed.
1038
+ */
1039
+ function validateOneAsymmetricKeyTail(e) {
1040
+ let t = !1, n, r = !1;
1041
+ for (let i of e) {
1042
+ let e = (i.tag & 192) == 128 ? i.tag & 31 : -1;
1043
+ if (e === 0) {
1044
+ if (t || n !== void 0 || r || i.tag !== 160) throw Error("Malformed PKCS#8 private key");
1045
+ assertOneAsymmetricKeyAttributes(i.value), t = !0;
1046
+ } else if (e === 1) {
1047
+ if (n !== void 0 || r || i.tag !== 129) throw Error("Malformed PKCS#8 private key");
1048
+ n = readPublicKeyBitString(i.value);
1049
+ } else if (e >= 2 && e <= 30) r = !0;
1050
+ else throw Error("Malformed PKCS#8 private key");
1051
+ }
1052
+ return n;
1053
+ }
1054
+ /**
1055
+ * Reject an `attributes [0]` field that does not hold a `SET OF Attribute`.
1056
+ *
1057
+ * RFC 5958 §2 types the field `Attributes ::= SET OF Attribute`, over the
1058
+ * `Attribute ::= SEQUENCE { attrType OBJECT IDENTIFIER, attrValues SET OF
1059
+ * AttributeValue }` of RFC 5652 §5.3. The implicit tag has replaced the SET OF
1060
+ * tag, so the members are read back under a universal constructed one.
1061
+ */
1062
+ function assertOneAsymmetricKeyAttributes(e) {
1063
+ let t = c(48, e);
1064
+ for (let e of o(t)) {
1065
+ 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];
1067
+ if (n.length !== 2 || r === void 0 || r.tag !== 6 || i === void 0 || i.tag !== 49) throw Error("Malformed PKCS#8 private key");
1068
+ }
1069
+ }
1070
+ /** Reject a `publicKey [1]` BIT STRING that is not octet-aligned or carries no key octets. */
1071
+ function readPublicKeyBitString(e) {
1072
+ if (e.length < 2 || e[0] !== 0) throw Error("Malformed PKCS#8 private key");
1073
+ return e.slice(1);
1074
+ }
1075
+ /** Decode the canonical RFC 5958 version INTEGER content, `v1(0)` or `v2(1)`. */
1076
+ function readPkcs8Version(e) {
1077
+ let t = e[0];
1078
+ if (e.length !== 1 || t !== 0 && t !== 1) throw Error("Malformed PKCS#8 private key");
1079
+ return t;
1080
+ }
930
1081
  /**
931
1082
  * Extract the optional `parameters [0]` curve identifier from a SEC 1 ECPrivateKey.
932
1083
  *
933
- * RFC 5915: `ECPrivateKey ::= SEQUENCE { version INTEGER, privateKey OCTET STRING,
934
- * parameters [0] ECParameters OPTIONAL, publicKey [1] BIT STRING OPTIONAL }`.
1084
+ * RFC 5915 §3: `ECPrivateKey ::= SEQUENCE { version INTEGER { ecPrivkeyVer1(1) }
1085
+ * (ecPrivkeyVer1), privateKey OCTET STRING, parameters [0] ECParameters
1086
+ * {{ NamedCurve }} OPTIONAL, publicKey [1] BIT STRING OPTIONAL }`.
935
1087
  */
936
1088
  function parseSec1PrivateKey(e) {
937
- let t = a(e), n = t[0], r = t[1], o = t[2], c = t[3];
938
- if (t.length < 2 || t.length > 4 || n === void 0 || n.tag !== 2 || r === void 0 || r.tag !== 4 || o !== void 0 && o.tag !== 160 && o.tag !== 161 || c !== void 0 && (o?.tag !== 160 || c.tag !== 161)) throw Error("Malformed SEC 1 private key");
939
- let l = o?.tag === 160 ? o : void 0;
940
- if (l === void 0) return {};
941
- let u = i(l.value);
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);
942
1094
  return {
943
1095
  parametersTag: u.tag,
944
- ...u.tag === 6 ? { parametersOid: s(u.value) } : {}
1096
+ ...u.tag === 6 ? { parametersOid: l(u.value) } : {}
945
1097
  };
946
1098
  }
947
1099
  /**
@@ -951,31 +1103,31 @@ function parseSec1PrivateKey(e) {
951
1103
  * standalone SEC 1 — and OpenSSL always writes it. Injecting it keeps exports
952
1104
  * self-describing, so they re-import without an explicit curve.
953
1105
  */
954
- function ensureSec1NamedCurveParameters(t, r) {
955
- let i = a(t);
1106
+ function ensureSec1NamedCurveParameters(t, n) {
1107
+ let i = o(t);
956
1108
  if (i.some((e) => e.tag === 160)) return new Uint8Array(t);
957
- if (r === void 0) throw Error("SEC1 export requires a named curve in the PKCS#8 privateKeyAlgorithm");
1109
+ if (n === void 0) throw Error("SEC1 export requires a named curve in the PKCS#8 privateKeyAlgorithm");
958
1110
  let raw = (e) => t.slice(e.start - e.headerLength, e.end);
959
- return o([
1111
+ return s([
960
1112
  ...i.slice(0, 2).map(raw),
961
- e(0, n(r)),
1113
+ e(0, r(n)),
962
1114
  ...i.slice(2).map(raw)
963
1115
  ]);
964
1116
  }
965
1117
  /** Wrap a PKCS#1 RSAPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
966
1118
  function wrapPkcs1InPkcs8(e) {
967
- return o([
1119
+ return s([
968
1120
  Uint8Array.of(2, 1, 0),
969
- o([n(f.rsaEncryption), t()]),
970
- r(new Uint8Array(e))
1121
+ s([r(m.rsaEncryption), n()]),
1122
+ i(new Uint8Array(e))
971
1123
  ]);
972
1124
  }
973
1125
  /** Wrap a SEC 1 ECPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
974
1126
  function wrapSec1InPkcs8(e, t) {
975
- return o([
1127
+ return s([
976
1128
  Uint8Array.of(2, 1, 0),
977
- o([n(f.ecPublicKey), n(curveToOid(t))]),
978
- r(new Uint8Array(e))
1129
+ s([r(m.ecPublicKey), r(curveToOid(t))]),
1130
+ i(new Uint8Array(e))
979
1131
  ]);
980
1132
  }
981
1133
  /** Project a private JWK onto its public-only members, discarding private key material. */
@@ -998,19 +1150,19 @@ function toPublicJwk(e, t) {
998
1150
  * Single source of truth for the NIST curve ↔ named-curve OID mapping, read in
999
1151
  * both directions by {@linkcode curveToOid} and {@linkcode oidToCurve}.
1000
1152
  */
1001
- const E = [
1002
- ["P-256", f.prime256v1],
1003
- ["P-384", f.secp384r1],
1004
- ["P-521", f.secp521r1]
1153
+ const M = [
1154
+ ["P-256", m.prime256v1],
1155
+ ["P-384", m.secp384r1],
1156
+ ["P-521", m.secp521r1]
1005
1157
  ];
1006
1158
  /** Map a curve name to its ASN.1 OID string. */
1007
1159
  function curveToOid(e) {
1008
- for (let [t, n] of E) if (t === e) return n;
1160
+ for (let [t, n] of M) if (t === e) return n;
1009
1161
  throw Error(`Unsupported EC curve: ${e}`);
1010
1162
  }
1011
1163
  /** Map an ASN.1 curve OID string back to its curve name, or `undefined` if unrecognized. */
1012
1164
  function oidToCurve(e) {
1013
- for (let [t, n] of E) if (n === e) return t;
1165
+ for (let [t, n] of M) if (n === e) return t;
1014
1166
  }
1015
1167
  /** Map an {@linkcode RsaScheme} to the WebCrypto algorithm name string. */
1016
1168
  function rsaSchemeToWebCryptoAlgorithmName(e) {
@@ -1045,53 +1197,63 @@ function validateRsaOaepKey(e, t, n) {
1045
1197
  function toRsaOaepParams(e) {
1046
1198
  return {
1047
1199
  name: "RSA-OAEP",
1048
- ...e.label === void 0 ? {} : { label: u(e.label) }
1200
+ ...e.label === void 0 ? {} : { label: f(e.label) }
1049
1201
  };
1050
1202
  }
1051
1203
  /** Encrypt DER key material as an OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM block. */
1052
1204
  async function encryptTraditionalPem(e, t, n) {
1053
- let r = n.iv ?? m().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
1205
+ let r = n.iv ?? g().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
1054
1206
  if (r.length !== 16) throw Error("Traditional PEM encryption requires a 16-byte IV");
1055
- let i = n.cipher ?? "AES-256-CBC", a = await importTraditionalPemAesKey(n.password, r.slice(0, 8), i, ["encrypt"]), o = b(new Uint8Array(await m().subtle.encrypt({
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({
1056
1208
  name: "AES-CBC",
1057
- iv: u(r)
1058
- }, a, u(t)))).match(/.{1,64}/g)?.join("\n") ?? "";
1209
+ iv: f(r)
1210
+ }, a, f(t)))).match(/.{1,64}/g)?.join("\n") ?? "";
1059
1211
  return [
1060
1212
  `-----BEGIN ${e}-----`,
1061
1213
  "Proc-Type: 4,ENCRYPTED",
1062
- `DEK-Info: ${i},${d(r).toUpperCase()}`,
1214
+ `DEK-Info: ${i},${p(r).toUpperCase()}`,
1063
1215
  "",
1064
1216
  o,
1065
1217
  `-----END ${e}-----`
1066
1218
  ].join("\n");
1067
1219
  }
1068
- /** Decrypt an OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM block back to plaintext DER. */
1220
+ /** Decrypt a `Proc-Type: 4,ENCRYPTED` PEM block, RFC 1421 §4.6 headers, to plaintext DER. */
1069
1221
  async function decryptTraditionalPem(e, t, n) {
1070
- let r = parseTraditionalPem(t);
1222
+ let r = D(t);
1071
1223
  if (r.label !== e) throw Error(`Expected ${e} PEM block`);
1072
- let i = r.headers.get("DEK-Info");
1073
- if (r.headers.get("Proc-Type") !== "4,ENCRYPTED" || i === void 0) throw Error("Traditional PEM encryption headers missing");
1074
- let [a, o] = i.split(",");
1075
- if (!isTraditionalPemCipher(a) || o === void 0) throw Error("Only AES-128-CBC, AES-192-CBC, and AES-256-CBC traditional PEM encryption is supported");
1076
- if (!isTraditionalPemIvHex(o)) throw Error("Traditional PEM encryption requires a 16-byte IV encoded as 32 hex characters");
1077
- let s = l(o), c = await importTraditionalPemAesKey(n, s.slice(0, 8), a, ["decrypt"]);
1224
+ let i = r.headers.get("DEK-Info"), a = r.headers.get("Proc-Type");
1225
+ if (a === void 0 || !isEncryptedProcType(a) || i === void 0) throw Error("Traditional PEM encryption headers missing");
1226
+ let [o, s] = i.split(",").map(A);
1227
+ 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
+ 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"]);
1078
1230
  try {
1079
- return new Uint8Array(await m().subtle.decrypt({
1231
+ return new Uint8Array(await g().subtle.decrypt({
1080
1232
  name: "AES-CBC",
1081
- iv: u(s)
1082
- }, c, u(y(r.base64Body))));
1233
+ iv: f(c)
1234
+ }, l, f(S(r.base64Body))));
1083
1235
  } catch {
1084
- throw v("Invalid password or encrypted PEM content");
1236
+ throw x("Invalid password or encrypted PEM content");
1085
1237
  }
1086
1238
  }
1087
1239
  /** Derive and import an AES-CBC key for legacy PEM encryption using OpenSSL `EVP_BytesToKey`. */
1088
1240
  function importTraditionalPemAesKey(e, t, n, r) {
1089
1241
  let i = traditionalPemCipherKeyLength(n), a = opensslBytesToKey(e, t, i / 8);
1090
- return m().subtle.importKey("raw", u(a), {
1242
+ return g().subtle.importKey("raw", f(a), {
1091
1243
  name: "AES-CBC",
1092
1244
  length: i
1093
1245
  }, !1, r);
1094
1246
  }
1247
+ /**
1248
+ * RFC 1421 §4.6.1.1 `Proc-Type: 4,ENCRYPTED`.
1249
+ *
1250
+ * Fields are trimmed because RFC 822 §3.1.1 unfolding leaves an LWSP-char
1251
+ * wherever the header was folded.
1252
+ */
1253
+ function isEncryptedProcType(e) {
1254
+ let t = e.split(",");
1255
+ return t.length === 2 && A(t[0] ?? "") === "4" && A(t[1] ?? "") === "ENCRYPTED";
1256
+ }
1095
1257
  /** Type guard for the three AES-CBC ciphers supported by legacy PEM encryption. */
1096
1258
  function isTraditionalPemCipher(e) {
1097
1259
  return e === "AES-128-CBC" || e === "AES-192-CBC" || e === "AES-256-CBC";
@@ -1105,6 +1267,7 @@ function traditionalPemCipherKeyLength(e) {
1105
1267
  case "AES-128-CBC": return 128;
1106
1268
  case "AES-192-CBC": return 192;
1107
1269
  case "AES-256-CBC": return 256;
1270
+ default: throw Error(`Unhandled traditional PEM cipher: ${String(e)}`);
1108
1271
  }
1109
1272
  }
1110
1273
  /** OpenSSL `EVP_BytesToKey` with MD5 — derives a fixed-length key from password + salt. */
@@ -1112,7 +1275,7 @@ function opensslBytesToKey(e, t, n) {
1112
1275
  let r = new TextEncoder().encode(e), i = [], a = /* @__PURE__ */ new Uint8Array(), o = 0;
1113
1276
  for (; o < n;) {
1114
1277
  let e = new Uint8Array(a.length + r.length + t.length);
1115
- e.set(a, 0), e.set(r, a.length), e.set(t, a.length + r.length), a = p(e), i.push(a), o += a.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;
1116
1279
  }
1117
1280
  let s = new Uint8Array(n), c = 0;
1118
1281
  for (let e of i) {
@@ -1121,46 +1284,20 @@ function opensslBytesToKey(e, t, n) {
1121
1284
  }
1122
1285
  return s;
1123
1286
  }
1124
- /** Parse a PEM block into its label, RFC 1421 headers, and base64 body. */
1125
- function parseTraditionalPem(e) {
1126
- let t = e.replace(/\r/g, "").trim().split("\n"), n = t[0], r = t[t.length - 1];
1127
- if (n === void 0 || r === void 0 || !n.startsWith("-----BEGIN ") || !r.startsWith("-----END ")) throw Error("Invalid PEM block");
1128
- let i = n.slice(11, -5);
1129
- if (r !== `-----END ${i}-----`) throw Error("PEM boundaries do not match");
1130
- let a = /* @__PURE__ */ new Map(), o = 1;
1131
- for (; o < t.length - 1;) {
1132
- let e = t[o];
1133
- if (e === void 0) break;
1134
- if (e.length === 0) {
1135
- o += 1;
1136
- break;
1137
- }
1138
- let n = e.indexOf(": ");
1139
- if (n === -1) break;
1140
- let r = e.slice(0, n);
1141
- if (a.has(r)) throw Error(`Duplicate PEM header: ${r}`);
1142
- a.set(r, e.slice(n + 2)), o += 1;
1143
- }
1144
- return {
1145
- label: i,
1146
- headers: a,
1147
- base64Body: t.slice(o, t.length - 1).join("")
1148
- };
1149
- }
1150
1287
  function parseSpkiDer(e) {
1151
1288
  try {
1152
- let t = a(e);
1289
+ let t = o(e);
1153
1290
  if (t.length !== 2) throw Error("Malformed SubjectPublicKeyInfo");
1154
1291
  let n = t[0], r = t[1];
1155
1292
  if (n === void 0 || n.tag !== 48 || r === void 0 || r.tag !== 3) throw Error("Malformed SubjectPublicKeyInfo");
1156
- c(r);
1157
- let i = a(e.slice(n.start - n.headerLength, n.end)), o = i[0];
1158
- if (o === void 0 || o.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed SubjectPublicKeyInfo");
1159
- let l = i[1];
1293
+ u(r);
1294
+ let i = o(e.slice(n.start - n.headerLength, n.end)), a = i[0];
1295
+ if (a === void 0 || a.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed SubjectPublicKeyInfo");
1296
+ let s = i[1];
1160
1297
  return {
1161
- algorithmOid: s(o.value),
1162
- ...l === void 0 ? {} : { parametersTag: l.tag },
1163
- ...l?.tag === 6 ? { parametersOid: s(l.value) } : {}
1298
+ algorithmOid: l(a.value),
1299
+ ...s === void 0 ? {} : { parametersTag: s.tag },
1300
+ ...s?.tag === 6 ? { parametersOid: l(s.value) } : {}
1164
1301
  };
1165
1302
  } catch {
1166
1303
  throw Error("Malformed SubjectPublicKeyInfo");
@@ -1176,10 +1313,10 @@ function parseSpkiDer(e) {
1176
1313
  */
1177
1314
  function inferKeyImportInput(e, t) {
1178
1315
  switch (e.algorithmOid) {
1179
- case f.rsaEncryption:
1316
+ case m.rsaEncryption:
1180
1317
  if (e.parametersTag === void 0 || e.parametersTag === 5) return { kind: "rsa" };
1181
1318
  break;
1182
- case f.ecPublicKey: {
1319
+ case m.ecPublicKey: {
1183
1320
  let t = e.parametersTag === 6 && e.parametersOid !== void 0 ? oidToCurve(e.parametersOid) : void 0;
1184
1321
  if (t !== void 0) return {
1185
1322
  kind: "ecdsa",
@@ -1187,7 +1324,7 @@ function inferKeyImportInput(e, t) {
1187
1324
  };
1188
1325
  break;
1189
1326
  }
1190
- case f.ed25519:
1327
+ case m.ed25519:
1191
1328
  if (e.parametersTag === void 0) return { kind: "ed25519" };
1192
1329
  break;
1193
1330
  }
@@ -1274,27 +1411,29 @@ function rsaImportInputFromJwkAlg(e) {
1274
1411
  function assertSpkiMatchesRequestedAlgorithm(e, t) {
1275
1412
  switch (t.kind) {
1276
1413
  case "rsa":
1277
- if (e.algorithmOid !== f.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1414
+ if (e.algorithmOid !== m.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1278
1415
  return;
1279
1416
  case "ecdsa":
1280
- if (e.algorithmOid !== f.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1417
+ if (e.algorithmOid !== m.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1281
1418
  return;
1282
1419
  case "ed25519":
1283
- if (e.algorithmOid !== f.ed25519 || e.parametersTag !== void 0) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1420
+ if (e.algorithmOid !== m.ed25519 || e.parametersTag !== void 0) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
1284
1421
  return;
1422
+ default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(t)}`);
1285
1423
  }
1286
1424
  }
1287
1425
  function assertPkcs8MatchesRequestedAlgorithm(e, t) {
1288
1426
  switch (t.kind) {
1289
1427
  case "rsa":
1290
- if (e.algorithmOid !== f.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
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");
1291
1429
  return;
1292
1430
  case "ecdsa":
1293
- if (e.algorithmOid !== f.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
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");
1294
1432
  return;
1295
1433
  case "ed25519":
1296
- if (e.algorithmOid !== f.ed25519 || e.parametersTag !== void 0) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1434
+ if (e.algorithmOid !== m.ed25519 || e.parametersTag !== void 0) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
1297
1435
  return;
1436
+ default: throw Error(`Unhandled PrivateKeyImportInput kind: ${String(t)}`);
1298
1437
  }
1299
1438
  }
1300
1439
  function assertSec1MatchesRequestedAlgorithm(e, t) {
@@ -1312,6 +1451,7 @@ function assertPublicJwkMatchesRequestedAlgorithm(e, t) {
1312
1451
  case "ed25519":
1313
1452
  if (e.kty !== "OKP" || e.crv !== "Ed25519" || typeof e.x != "string") throw Error("Public JWK algorithm does not match requested import algorithm");
1314
1453
  return;
1454
+ default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(t)}`);
1315
1455
  }
1316
1456
  }
1317
1457
  function assertPrivateJwkMatchesRequestedAlgorithm(e, t) {
@@ -1327,9 +1467,10 @@ function assertPrivateJwkMatchesRequestedAlgorithm(e, t) {
1327
1467
  case "ed25519":
1328
1468
  if (e.kty !== "OKP" || e.crv !== "Ed25519" || typeof e.x != "string") throw Error("Private JWK algorithm does not match requested import algorithm");
1329
1469
  return;
1470
+ default: throw Error(`Unhandled PrivateKeyImportInput kind: ${String(t)}`);
1330
1471
  }
1331
1472
  }
1332
1473
  //#endregion
1333
- 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 };
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 };
1334
1475
 
1335
1476
  //# sourceMappingURL=keys.js.map