micro509 0.7.2 → 0.9.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 (109) hide show
  1. package/dist/index.d.ts +5 -5
  2. package/dist/index.js +15 -1
  3. package/dist/internal/asn1/asn1.js +280 -2
  4. package/dist/internal/asn1/der.js +378 -2
  5. package/dist/internal/asn1/oids.js +103 -2
  6. package/dist/internal/crypto/algorithm-names.js +58 -2
  7. package/dist/internal/crypto/ecdsa.js +68 -2
  8. package/dist/internal/crypto/hash.js +286 -2
  9. package/dist/internal/crypto/pbes2.js +213 -2
  10. package/dist/internal/crypto/rsa-pss.js +184 -2
  11. package/dist/internal/crypto/sig-verify.js +192 -2
  12. package/dist/internal/crypto/signing.d.ts +1 -2
  13. package/dist/internal/crypto/signing.js +123 -2
  14. package/dist/internal/crypto/webcrypto.js +14 -2
  15. package/dist/internal/shared/base64.js +16 -2
  16. package/dist/internal/shared/dn.js +84 -2
  17. package/dist/internal/shared/ip.js +164 -2
  18. package/dist/internal/verify/name-constraints-engine.js +450 -2
  19. package/dist/internal/verify/policy-engine.js +382 -2
  20. package/dist/internal/verify/verify-path.js +298 -2
  21. package/dist/internal/x509/extension-bits.js +132 -2
  22. package/dist/internal/x509/extension-registry.js +229 -2
  23. package/dist/internal/x509/name-fields.js +93 -2
  24. package/dist/keys/keys.d.ts +380 -13
  25. package/dist/keys/keys.js +1247 -5
  26. package/dist/keys.d.ts +2 -2
  27. package/dist/keys.js +2 -1
  28. package/dist/pem/pem.d.ts +2 -2
  29. package/dist/pem/pem.js +172 -6
  30. package/dist/pem.js +2 -1
  31. package/dist/pkcs/pfx.d.ts +4 -5
  32. package/dist/pkcs/pfx.js +395 -2
  33. package/dist/pkcs/pkcs12-mac.d.ts +2 -2
  34. package/dist/pkcs/pkcs12-mac.js +175 -2
  35. package/dist/pkcs/pkcs7.d.ts +1 -2
  36. package/dist/pkcs/pkcs7.js +653 -2
  37. package/dist/pkcs.d.ts +2 -2
  38. package/dist/pkcs.js +4 -1
  39. package/dist/result/result.d.ts +1 -2
  40. package/dist/result/result.js +111 -2
  41. package/dist/result.js +2 -1
  42. package/dist/revocation/chain.d.ts +4 -5
  43. package/dist/revocation/chain.js +536 -2
  44. package/dist/revocation/crl.d.ts +2 -2
  45. package/dist/revocation/crl.js +1119 -2
  46. package/dist/revocation/ocsp.d.ts +5 -4
  47. package/dist/revocation/ocsp.js +958 -2
  48. package/dist/revocation/revocation.d.ts +1 -2
  49. package/dist/revocation/revocation.js +261 -2
  50. package/dist/revocation.d.ts +2 -2
  51. package/dist/revocation.js +5 -1
  52. package/dist/verify/identity.d.ts +1 -2
  53. package/dist/verify/identity.js +270 -2
  54. package/dist/verify/name-constraints.d.ts +1 -2
  55. package/dist/verify/policy.d.ts +1 -2
  56. package/dist/verify/verify.d.ts +1 -2
  57. package/dist/verify/verify.js +764 -2
  58. package/dist/verify.js +3 -1
  59. package/dist/x509/certificate.d.ts +1 -2
  60. package/dist/x509/certificate.js +151 -2
  61. package/dist/x509/csr.d.ts +1 -2
  62. package/dist/x509/csr.js +71 -2
  63. package/dist/x509/extensions.d.ts +1 -2
  64. package/dist/x509/extensions.js +387 -2
  65. package/dist/x509/name.d.ts +1 -2
  66. package/dist/x509/name.js +173 -2
  67. package/dist/x509/parse.d.ts +84 -2
  68. package/dist/x509/parse.js +1090 -2
  69. package/dist/x509.d.ts +2 -2
  70. package/dist/x509.js +6 -1
  71. package/package.json +74 -34
  72. package/dist/internal/asn1/asn1.js.map +0 -1
  73. package/dist/internal/asn1/der.js.map +0 -1
  74. package/dist/internal/asn1/oids.js.map +0 -1
  75. package/dist/internal/crypto/algorithm-names.js.map +0 -1
  76. package/dist/internal/crypto/ecdsa.js.map +0 -1
  77. package/dist/internal/crypto/hash.js.map +0 -1
  78. package/dist/internal/crypto/pbes2.d.ts +0 -23
  79. package/dist/internal/crypto/pbes2.js.map +0 -1
  80. package/dist/internal/crypto/rsa-pss.js.map +0 -1
  81. package/dist/internal/crypto/sig-verify.js.map +0 -1
  82. package/dist/internal/crypto/signing.js.map +0 -1
  83. package/dist/internal/crypto/webcrypto.js.map +0 -1
  84. package/dist/internal/shared/base64.js.map +0 -1
  85. package/dist/internal/shared/dn.js.map +0 -1
  86. package/dist/internal/shared/ip.js.map +0 -1
  87. package/dist/internal/verify/name-constraints-engine.js.map +0 -1
  88. package/dist/internal/verify/policy-engine.js.map +0 -1
  89. package/dist/internal/verify/verify-path.js.map +0 -1
  90. package/dist/internal/x509/extension-bits.js.map +0 -1
  91. package/dist/internal/x509/extension-registry.js.map +0 -1
  92. package/dist/internal/x509/name-fields.js.map +0 -1
  93. package/dist/keys/keys.js.map +0 -1
  94. package/dist/pem/pem.js.map +0 -1
  95. package/dist/pkcs/pfx.js.map +0 -1
  96. package/dist/pkcs/pkcs12-mac.js.map +0 -1
  97. package/dist/pkcs/pkcs7.js.map +0 -1
  98. package/dist/result/result.js.map +0 -1
  99. package/dist/revocation/chain.js.map +0 -1
  100. package/dist/revocation/crl.js.map +0 -1
  101. package/dist/revocation/ocsp.js.map +0 -1
  102. package/dist/revocation/revocation.js.map +0 -1
  103. package/dist/verify/identity.js.map +0 -1
  104. package/dist/verify/verify.js.map +0 -1
  105. package/dist/x509/certificate.js.map +0 -1
  106. package/dist/x509/csr.js.map +0 -1
  107. package/dist/x509/extensions.js.map +0 -1
  108. package/dist/x509/name.js.map +0 -1
  109. package/dist/x509/parse.js.map +0 -1
@@ -1,11 +1,15 @@
1
- import { Pbes2EncryptionOptions, Pbes2EncryptionScheme, Pbes2Prf } from "../internal/crypto/pbes2.js";
2
1
  import { ErrorResult, Micro509Error } from "../result/result.js";
3
2
 
4
3
  //#region src/keys/keys.d.ts
5
4
  /** Hash algorithm paired with an RSA key. */
6
5
  type RsaHash = "SHA-256" | "SHA-384" | "SHA-512";
7
6
  /** RSA signature padding scheme. */
8
- type RsaScheme = "pkcs1-v1_5" | "pss";
7
+ type RsaSignatureScheme = "pkcs1-v1_5" | "pss";
8
+ /**
9
+ * RSA padding scheme: a signature scheme, or `'oaep'` for RSA-OAEP encryption
10
+ * keys (usable with {@linkcode encryptRsaOaep} / {@linkcode decryptRsaOaep}).
11
+ */
12
+ type RsaScheme = RsaSignatureScheme | "oaep";
9
13
  /** NIST elliptic curve for ECDSA keys. */
10
14
  type EcNamedCurve = "P-256" | "P-384" | "P-521";
11
15
  /** RSA variant of {@linkcode KeyAlgorithmInput}. */
@@ -16,7 +20,10 @@ interface RsaKeyAlgorithmInput {
16
20
  readonly modulusLength?: 2048 | 3072 | 4096;
17
21
  /** Hash algorithm for the key. Defaults to `'SHA-256'`. */
18
22
  readonly hash?: RsaHash;
19
- /** Signature padding scheme. Defaults to `'pkcs1-v1_5'`. */
23
+ /**
24
+ * Padding scheme. Defaults to `'pkcs1-v1_5'`. Pass `'oaep'` to generate an
25
+ * RSA-OAEP encryption pair (`encrypt`/`decrypt` usages instead of `sign`/`verify`).
26
+ */
20
27
  readonly scheme?: RsaScheme;
21
28
  }
22
29
  /** ECDSA variant of {@linkcode KeyAlgorithmInput}. */
@@ -35,9 +42,9 @@ interface Ed25519KeyAlgorithmInput {
35
42
  type KeyAlgorithmInput = RsaKeyAlgorithmInput | EcKeyAlgorithmInput | Ed25519KeyAlgorithmInput;
36
43
  /** Key pair with convenience export helpers. Returned by {@linkcode generateKeyPair}. */
37
44
  interface KeyPairMaterial {
38
- /** The WebCrypto public key (extractable, `verify` usage). */
45
+ /** The WebCrypto public key (extractable, `verify` usage; `encrypt` for RSA-OAEP). */
39
46
  readonly publicKey: CryptoKey;
40
- /** The WebCrypto private key (extractable, `sign` usage). */
47
+ /** The WebCrypto private key (extractable, `sign` usage; `decrypt` for RSA-OAEP). */
41
48
  readonly privateKey: CryptoKey;
42
49
  /** Export the public key as DER-encoded SubjectPublicKeyInfo. */
43
50
  exportSpkiDer(): Promise<Uint8Array>;
@@ -58,7 +65,10 @@ interface ImportRsaKeyInput {
58
65
  readonly kind: "rsa";
59
66
  /** Hash algorithm. Defaults to `'SHA-256'`. */
60
67
  readonly hash?: RsaHash;
61
- /** Signature padding scheme. Defaults to `'pkcs1-v1_5'`. */
68
+ /**
69
+ * Padding scheme. Defaults to `'pkcs1-v1_5'`. Pass `'oaep'` to import an
70
+ * RSA-OAEP encryption key (`encrypt`/`decrypt` usage instead of `verify`/`sign`).
71
+ */
62
72
  readonly scheme?: RsaScheme;
63
73
  }
64
74
  /** ECDSA variant of {@linkcode PublicKeyImportInput} / {@linkcode PrivateKeyImportInput}. */
@@ -78,7 +88,20 @@ type PublicKeyImportInput = ImportRsaKeyInput | ImportEcKeyInput | ImportEd25519
78
88
  /** Algorithm descriptor for private key import functions. Same shape as {@linkcode PublicKeyImportInput}. */
79
89
  type PrivateKeyImportInput = PublicKeyImportInput;
80
90
  /** PBES2 encryption options for the encrypted PKCS#8 export/import functions. */
81
- type EncryptedPkcs8Options = Pbes2EncryptionOptions;
91
+ interface EncryptedPkcs8Options {
92
+ /** Password fed to PBKDF2 for key derivation. */
93
+ readonly password: string;
94
+ /** PBKDF2 iteration count. Default: `100_000`. */
95
+ readonly iterations?: number;
96
+ /** PBKDF2 salt. Default: 16 cryptographically random bytes. */
97
+ readonly salt?: Uint8Array;
98
+ /** AES-CBC initialization vector. Default: 16 cryptographically random bytes. */
99
+ readonly iv?: Uint8Array;
100
+ /** AES-CBC cipher. Default: `'AES-256-CBC'`. */
101
+ readonly cipher?: "AES-128-CBC" | "AES-192-CBC" | "AES-256-CBC";
102
+ /** PBKDF2 pseudo-random function. Default: `'HMAC-SHA-256'`. */
103
+ readonly prf?: "HMAC-SHA-1" | "HMAC-SHA-256";
104
+ }
82
105
  /** Options for OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM encryption (PKCS#1/SEC1). */
83
106
  interface LegacyPemEncryptionOptions {
84
107
  /** Passphrase used to derive the encryption key. */
@@ -92,6 +115,7 @@ interface LegacyPemEncryptionOptions {
92
115
  type ImportKeyErrorCode = "malformed";
93
116
  /** Structured failure payload for key import. */
94
117
  interface ImportKeyFailure extends Micro509Error<ImportKeyErrorCode> {
118
+ /** Always `false` for failures. */
95
119
  readonly ok: false;
96
120
  }
97
121
  /**
@@ -114,6 +138,7 @@ type ImportKeyResult<T> = {
114
138
  type ImportEncryptedKeyErrorCode = "malformed" | "invalid_password";
115
139
  /** Structured failure payload for encrypted key import. */
116
140
  interface ImportEncryptedKeyFailure extends Micro509Error<ImportEncryptedKeyErrorCode> {
141
+ /** Always `false` for failures. */
117
142
  readonly ok: false;
118
143
  }
119
144
  /**
@@ -126,14 +151,61 @@ type ImportEncryptedKeyResult<T> = {
126
151
  readonly ok: true;
127
152
  readonly value: T;
128
153
  } | ErrorResult<ImportEncryptedKeyErrorCode, Record<never, never>, ImportEncryptedKeyFailure>;
154
+ /** Options shared by {@linkcode encryptRsaOaep} and {@linkcode decryptRsaOaep}. */
155
+ interface RsaOaepOptions {
156
+ /**
157
+ * Optional OAEP label bound to the ciphertext. Not encrypted, but decryption
158
+ * fails unless the exact same label is presented. Default: empty.
159
+ */
160
+ readonly label?: Uint8Array;
161
+ }
129
162
  /**
130
- * Generate an asymmetric key pair for signing and verification.
163
+ * Machine-readable failure reason for {@linkcode encryptRsaOaep}.
164
+ *
165
+ * `'invalid_key'` when the key is not an RSA-OAEP public key with `encrypt`
166
+ * usage; `'message_too_long'` when the plaintext exceeds the OAEP capacity of
167
+ * the key (modulus bytes − 2 × hash bytes − 2).
168
+ */
169
+ type EncryptRsaOaepErrorCode = "invalid_key" | "message_too_long";
170
+ /** Structured failure payload for {@linkcode encryptRsaOaep}. */
171
+ interface EncryptRsaOaepFailure extends Micro509Error<EncryptRsaOaepErrorCode> {
172
+ /** Always `false` for failures. */
173
+ readonly ok: false;
174
+ }
175
+ /** Success-or-failure result returned by {@linkcode encryptRsaOaep}. */
176
+ type EncryptRsaOaepResult = {
177
+ readonly ok: true;
178
+ readonly value: Uint8Array;
179
+ } | ErrorResult<EncryptRsaOaepErrorCode, Record<never, never>, EncryptRsaOaepFailure>;
180
+ /**
181
+ * Machine-readable failure reason for {@linkcode decryptRsaOaep}.
182
+ *
183
+ * `'invalid_key'` when the key is not an RSA-OAEP private key with `decrypt`
184
+ * usage; `'decryption_failed'` for every ciphertext-level failure (wrong key,
185
+ * wrong label, tampered or truncated ciphertext) — OAEP deliberately does not
186
+ * reveal which.
187
+ */
188
+ type DecryptRsaOaepErrorCode = "invalid_key" | "decryption_failed";
189
+ /** Structured failure payload for {@linkcode decryptRsaOaep}. */
190
+ interface DecryptRsaOaepFailure extends Micro509Error<DecryptRsaOaepErrorCode> {
191
+ /** Always `false` for failures. */
192
+ readonly ok: false;
193
+ }
194
+ /** Success-or-failure result returned by {@linkcode decryptRsaOaep}. */
195
+ type DecryptRsaOaepResult = {
196
+ readonly ok: true;
197
+ readonly value: Uint8Array;
198
+ } | ErrorResult<DecryptRsaOaepErrorCode, Record<never, never>, DecryptRsaOaepFailure>;
199
+ /**
200
+ * Generate an asymmetric key pair for signing and verification, or — with
201
+ * `{ kind: 'rsa', scheme: 'oaep' }` — for RSA-OAEP encryption and decryption.
131
202
  *
132
203
  * @example
133
204
  * ```ts
134
205
  * const ecKeys = await generateKeyPair({ kind: 'ecdsa', curve: 'P-384' });
135
206
  * const rsaKeys = await generateKeyPair({ kind: 'rsa', modulusLength: 4096 });
136
207
  * const edKeys = await generateKeyPair({ kind: 'ed25519' });
208
+ * const oaepKeys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' });
137
209
  *
138
210
  * // Default: ECDSA P-256
139
211
  * const keys = await generateKeyPair();
@@ -291,6 +363,37 @@ declare function exportEncryptedSec1Pem(privateKey: CryptoKey, options: LegacyPe
291
363
  */
292
364
  declare function exportSpkiPem(publicKey: CryptoKey): Promise<string>;
293
365
  /**
366
+ * Derive the matching public key from an imported (or generated) private key.
367
+ *
368
+ * The `import*` functions that read a PKCS#8 / PKCS#1 / SEC 1 / JWK private key
369
+ * return a bare `CryptoKey` with only `sign` (or, for RSA-OAEP, `decrypt`)
370
+ * usage — there is no accompanying public handle. This bridges that gap: it
371
+ * exports the private key's JWK, strips the private components, and re-imports
372
+ * the public half with `verify` (RSA-OAEP: `encrypt`) usage, so callers can go
373
+ * straight to {@linkcode exportSpkiDer} / {@linkcode exportSpkiPem} (e.g. to
374
+ * rebuild a self-signed cert or distribute the public key when only the
375
+ * private key is on disk).
376
+ *
377
+ * Supports RSA (`n`/`e`), ECDSA (`x`/`y`), and Ed25519 (`x`). The derived key
378
+ * inherits the private key's algorithm parameters (hash, curve).
379
+ *
380
+ * @param privateKey - An extractable private `CryptoKey`
381
+ * @returns Extractable public `CryptoKey` with `verify` (RSA-OAEP: `encrypt`) usage
382
+ *
383
+ * @throws {Error} If the key is not a private key, is non-extractable, or uses
384
+ * an unsupported key type
385
+ *
386
+ * @example
387
+ * ```ts
388
+ * const privateKey = await importPkcs8PemOrThrow(pem, { kind: 'ecdsa', curve: 'P-256' });
389
+ * const publicKey = await derivePublicKey(privateKey);
390
+ * const spkiPem = await exportSpkiPem(publicKey);
391
+ * ```
392
+ *
393
+ * @see {@linkcode exportSpkiDer} for exporting the derived key
394
+ */
395
+ declare function derivePublicKey(privateKey: CryptoKey): Promise<CryptoKey>;
396
+ /**
294
397
  * Export a key as raw base64 (no PEM headers).
295
398
  *
296
399
  * Returns SPKI-encoded base64 for public keys, PKCS#8-encoded base64 for private keys.
@@ -305,21 +408,74 @@ declare function exportBinaryBase64(key: CryptoKey): Promise<string>;
305
408
  /**
306
409
  * Import a public key from DER-encoded SubjectPublicKeyInfo.
307
410
  *
411
+ * When `algorithm` is omitted, the algorithm (and, for EC keys, the curve) is
412
+ * inferred from the SPKI's own AlgorithmIdentifier — useful for keys whose type
413
+ * isn't known ahead of time. Pass `algorithm` to additionally assert that the
414
+ * DER matches an expected algorithm.
415
+ *
416
+ * @param der - DER-encoded SubjectPublicKeyInfo bytes
417
+ * @param algorithm - Optional expected algorithm; must match key contents when given
418
+ * @returns Extractable CryptoKey with `verify` usage
419
+ *
420
+ * @throws {Error} If DER is malformed, encodes an unsupported algorithm, or doesn't match `algorithm`
421
+ *
422
+ * @see {@linkcode exportSpkiDer} for the inverse operation
423
+ * @see {@linkcode importSpkiPem} for PEM input
424
+ */
425
+ declare function importSpkiDerOrThrow(der: Uint8Array, algorithm?: PublicKeyImportInput): Promise<CryptoKey>;
426
+ /**
427
+ * Import a public key from DER-encoded SubjectPublicKeyInfo.
428
+ *
308
429
  * @see `importSpkiDerOrThrow` for the throwing variant
309
430
  */
310
- declare function importSpkiDer(der: Uint8Array, algorithm: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
431
+ declare function importSpkiDer(der: Uint8Array, algorithm?: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
432
+ /**
433
+ * Import a public key from PEM-encoded SubjectPublicKeyInfo.
434
+ *
435
+ * @example
436
+ * ```ts
437
+ * const pem = `-----BEGIN PUBLIC KEY-----
438
+ * MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE...
439
+ * -----END PUBLIC KEY-----`;
440
+ * const key = await importSpkiPemOrThrow(pem, { kind: 'ecdsa', curve: 'P-256' });
441
+ * ```
442
+ *
443
+ * @see {@linkcode exportSpkiPem} for the inverse operation
444
+ */
445
+ declare function importSpkiPemOrThrow(pem: string, algorithm?: PublicKeyImportInput): Promise<CryptoKey>;
311
446
  /**
312
447
  * Import a public key from PEM-encoded SubjectPublicKeyInfo.
313
448
  *
314
449
  * @see `importSpkiPemOrThrow` for the throwing variant
315
450
  */
316
- declare function importSpkiPem(pem: string, algorithm: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
451
+ declare function importSpkiPem(pem: string, algorithm?: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
452
+ /**
453
+ * Import a public key from base64-encoded SubjectPublicKeyInfo (no PEM headers).
454
+ *
455
+ * @see {@linkcode exportBinaryBase64} for the inverse operation
456
+ * @see {@linkcode importSpkiPem} for PEM input with headers
457
+ */
458
+ declare function importSpkiBase64OrThrow(base64: string, algorithm?: PublicKeyImportInput): Promise<CryptoKey>;
317
459
  /**
318
460
  * Import a public key from base64-encoded SubjectPublicKeyInfo (no PEM headers).
319
461
  *
320
462
  * @see `importSpkiBase64OrThrow` for the throwing variant
321
463
  */
322
- declare function importSpkiBase64(base64: string, algorithm: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
464
+ declare function importSpkiBase64(base64: string, algorithm?: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
465
+ /**
466
+ * Import a private key from DER-encoded PKCS#8 PrivateKeyInfo.
467
+ *
468
+ * @param der - DER-encoded PKCS#8 PrivateKeyInfo bytes
469
+ * @param algorithm - Expected algorithm (must match key contents)
470
+ * @returns Extractable CryptoKey with `sign` usage
471
+ *
472
+ * @throws {Error} If DER is malformed or algorithm doesn't match key
473
+ *
474
+ * @see {@linkcode exportPkcs8Der} for the inverse operation
475
+ * @see {@linkcode importPkcs8Pem} for PEM input
476
+ * @see {@linkcode importEncryptedPkcs8Der} for encrypted PKCS#8
477
+ */
478
+ declare function importPkcs8DerOrThrow(der: Uint8Array, algorithm: PrivateKeyImportInput): Promise<CryptoKey>;
323
479
  /**
324
480
  * Import a private key from DER-encoded PKCS#8 PrivateKeyInfo.
325
481
  *
@@ -329,75 +485,286 @@ declare function importPkcs8Der(der: Uint8Array, algorithm: PrivateKeyImportInpu
329
485
  /**
330
486
  * Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
331
487
  *
488
+ * @example
489
+ * ```ts
490
+ * const key = await importPkcs8PemOrThrow(pemString, { kind: 'ecdsa', curve: 'P-256' });
491
+ * ```
492
+ */
493
+ declare function importPkcs8PemOrThrow(pem: string, algorithm: PrivateKeyImportInput): Promise<CryptoKey>;
494
+ /**
495
+ * Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
496
+ *
332
497
  * @see `importPkcs8PemOrThrow` for the throwing variant
333
498
  */
334
499
  declare function importPkcs8Pem(pem: string, algorithm: PrivateKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
335
500
  /**
336
501
  * Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
337
502
  *
503
+ * Decrypts the PBES2 envelope using the provided password, then imports the key.
504
+ *
505
+ * @param der - DER-encoded EncryptedPrivateKeyInfo bytes
506
+ * @param password - Decryption password
507
+ * @param algorithm - Expected algorithm (must match decrypted key)
508
+ *
509
+ * @throws {Error} If DER is malformed, password is wrong, or algorithm doesn't match
510
+ *
511
+ * @see {@linkcode exportEncryptedPkcs8Der} for the inverse operation
512
+ */
513
+ declare function importEncryptedPkcs8DerOrThrow(der: Uint8Array, password: string, algorithm: PrivateKeyImportInput): Promise<CryptoKey>;
514
+ /**
515
+ * Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
516
+ *
338
517
  * @see `importEncryptedPkcs8DerOrThrow` for the throwing variant
339
518
  */
340
519
  declare function importEncryptedPkcs8Der(der: Uint8Array, password: string, algorithm: PrivateKeyImportInput): Promise<ImportEncryptedKeyResult<CryptoKey>>;
341
520
  /**
342
521
  * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
343
522
  *
523
+ * @example
524
+ * ```ts
525
+ * const key = await importEncryptedPkcs8PemOrThrow(pem, 'secret', { kind: 'rsa' });
526
+ * ```
527
+ */
528
+ declare function importEncryptedPkcs8PemOrThrow(pem: string, password: string, algorithm: PrivateKeyImportInput): Promise<CryptoKey>;
529
+ /**
530
+ * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
531
+ *
344
532
  * @see `importEncryptedPkcs8PemOrThrow` for the throwing variant
345
533
  */
346
534
  declare function importEncryptedPkcs8Pem(pem: string, password: string, algorithm: PrivateKeyImportInput): Promise<ImportEncryptedKeyResult<CryptoKey>>;
347
535
  /**
348
536
  * Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey.
349
537
  *
538
+ * PKCS#1 is the legacy RSA-only format. Internally converts to PKCS#8 for import.
539
+ *
540
+ * @see {@linkcode exportPkcs1Der} for the inverse operation
541
+ * @see {@linkcode importPkcs1Pem} for PEM input
542
+ */
543
+ declare function importPkcs1DerOrThrow(der: Uint8Array, algorithm?: ImportRsaKeyInput): Promise<CryptoKey>;
544
+ /**
545
+ * Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey.
546
+ *
350
547
  * @see `importPkcs1DerOrThrow` for the throwing variant
351
548
  */
352
549
  declare function importPkcs1Der(der: Uint8Array, algorithm?: ImportRsaKeyInput): Promise<ImportKeyResult<CryptoKey>>;
353
550
  /**
354
551
  * Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
355
552
  *
553
+ * Expects the `-----BEGIN RSA PRIVATE KEY-----` PEM label.
554
+ *
555
+ * @see {@linkcode exportPkcs1Pem} for the inverse operation
556
+ * @see {@linkcode importEncryptedPkcs1Pem} for encrypted PEM
557
+ */
558
+ declare function importPkcs1PemOrThrow(pem: string, algorithm?: ImportRsaKeyInput): Promise<CryptoKey>;
559
+ /**
560
+ * Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
561
+ *
356
562
  * @see `importPkcs1PemOrThrow` for the throwing variant
357
563
  */
358
564
  declare function importPkcs1Pem(pem: string, algorithm?: ImportRsaKeyInput): Promise<ImportKeyResult<CryptoKey>>;
359
565
  /**
360
566
  * Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
361
567
  *
568
+ * Decrypts OpenSSL's traditional PEM encryption format.
569
+ *
570
+ * @see {@linkcode exportEncryptedPkcs1Pem} for the inverse operation
571
+ * @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
572
+ */
573
+ declare function importEncryptedPkcs1PemOrThrow(pem: string, password: string, algorithm?: ImportRsaKeyInput): Promise<CryptoKey>;
574
+ /**
575
+ * Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
576
+ *
362
577
  * @see `importEncryptedPkcs1PemOrThrow` for the throwing variant
363
578
  */
364
579
  declare function importEncryptedPkcs1Pem(pem: string, password: string, algorithm?: ImportRsaKeyInput): Promise<ImportEncryptedKeyResult<CryptoKey>>;
365
580
  /**
366
581
  * Import a private key from base64-encoded PKCS#8 PrivateKeyInfo (no PEM headers).
367
582
  *
583
+ * @see {@linkcode exportBinaryBase64} for the inverse operation
584
+ * @see {@linkcode importPkcs8Pem} for PEM input with headers
585
+ */
586
+ declare function importPkcs8Base64OrThrow(base64: string, algorithm: PrivateKeyImportInput): Promise<CryptoKey>;
587
+ /**
588
+ * Import a private key from base64-encoded PKCS#8 PrivateKeyInfo (no PEM headers).
589
+ *
368
590
  * @see `importPkcs8Base64OrThrow` for the throwing variant
369
591
  */
370
592
  declare function importPkcs8Base64(base64: string, algorithm: PrivateKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
371
593
  /**
372
594
  * Import an EC private key from DER-encoded SEC 1 ECPrivateKey.
373
595
  *
596
+ * SEC 1 is the legacy EC-only format. Internally converts to PKCS#8 for import.
597
+ * When the ECPrivateKey carries the optional RFC 5915 `parameters [0]` field
598
+ * (OpenSSL always writes it), its named-curve OID must match `algorithm.curve`;
599
+ * when the field is absent, the caller-supplied curve is trusted.
600
+ *
601
+ * @throws {Error} If DER is not an ECPrivateKey or its embedded curve doesn't match `algorithm`
602
+ *
603
+ * @see {@linkcode exportSec1Der} for the inverse operation
604
+ * @see {@linkcode importSec1Pem} for PEM input
605
+ */
606
+ declare function importSec1DerOrThrow(der: Uint8Array, algorithm: ImportEcKeyInput): Promise<CryptoKey>;
607
+ /**
608
+ * Import an EC private key from DER-encoded SEC 1 ECPrivateKey.
609
+ *
374
610
  * @see `importSec1DerOrThrow` for the throwing variant
375
611
  */
376
612
  declare function importSec1Der(der: Uint8Array, algorithm: ImportEcKeyInput): Promise<ImportKeyResult<CryptoKey>>;
377
613
  /**
378
614
  * Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
379
615
  *
616
+ * Expects the `-----BEGIN EC PRIVATE KEY-----` PEM label.
617
+ *
618
+ * @see {@linkcode exportSec1Pem} for the inverse operation
619
+ * @see {@linkcode importEncryptedSec1Pem} for encrypted PEM
620
+ */
621
+ declare function importSec1PemOrThrow(pem: string, algorithm: ImportEcKeyInput): Promise<CryptoKey>;
622
+ /**
623
+ * Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
624
+ *
380
625
  * @see `importSec1PemOrThrow` for the throwing variant
381
626
  */
382
627
  declare function importSec1Pem(pem: string, algorithm: ImportEcKeyInput): Promise<ImportKeyResult<CryptoKey>>;
383
628
  /**
384
629
  * Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
385
630
  *
631
+ * Decrypts OpenSSL's traditional PEM encryption format.
632
+ *
633
+ * @see {@linkcode exportEncryptedSec1Pem} for the inverse operation
634
+ * @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
635
+ */
636
+ declare function importEncryptedSec1PemOrThrow(pem: string, password: string, algorithm: ImportEcKeyInput): Promise<CryptoKey>;
637
+ /**
638
+ * Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
639
+ *
386
640
  * @see `importEncryptedSec1PemOrThrow` for the throwing variant
387
641
  */
388
642
  declare function importEncryptedSec1Pem(pem: string, password: string, algorithm: ImportEcKeyInput): Promise<ImportEncryptedKeyResult<CryptoKey>>;
389
643
  /**
390
644
  * Import a public verification key from a JSON Web Key.
391
645
  *
646
+ * @param jwk - JSON Web Key object with public key components
647
+ * @param algorithm - Expected algorithm (must match JWK's `kty` and `crv`)
648
+ * @returns Extractable CryptoKey with `verify` usage
649
+ *
650
+ * @throws {Error} If JWK is malformed or algorithm doesn't match
651
+ *
652
+ * @see {@linkcode exportPublicJwk} for the inverse operation
653
+ */
654
+ declare function importPublicJwkOrThrow(jwk: JsonWebKey, algorithm: PublicKeyImportInput): Promise<CryptoKey>;
655
+ /**
656
+ * Import a public verification key from a JSON Web Key.
657
+ *
392
658
  * @see `importPublicJwkOrThrow` for the throwing variant
393
659
  */
394
660
  declare function importPublicJwk(jwk: JsonWebKey, algorithm: PublicKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
395
661
  /**
396
662
  * Import a private signing key from a JSON Web Key.
397
663
  *
664
+ * @param jwk - JSON Web Key object with private key components
665
+ * @param algorithm - Expected algorithm (must match JWK's `kty` and `crv`)
666
+ * @returns Extractable CryptoKey with `sign` usage
667
+ *
668
+ * @throws {Error} If JWK is malformed, lacks private key material, or algorithm doesn't match
669
+ *
670
+ * @example
671
+ * ```ts
672
+ * const jwk = { kty: 'EC', crv: 'P-256', x: '...', y: '...', d: '...' };
673
+ * const key = await importPrivateJwkOrThrow(jwk, { kind: 'ecdsa', curve: 'P-256' });
674
+ * ```
675
+ *
676
+ * @see {@linkcode exportPrivateJwk} for the inverse operation
677
+ */
678
+ declare function importPrivateJwkOrThrow(jwk: JsonWebKey, algorithm: PrivateKeyImportInput): Promise<CryptoKey>;
679
+ /**
680
+ * Import a private signing key from a JSON Web Key.
681
+ *
398
682
  * @see `importPrivateJwkOrThrow` for the throwing variant
399
683
  */
400
684
  declare function importPrivateJwk(jwk: JsonWebKey, algorithm: PrivateKeyImportInput): Promise<ImportKeyResult<CryptoKey>>;
685
+ /**
686
+ * Encrypt a small message with an RSA-OAEP public key.
687
+ *
688
+ * The key must have been generated or imported with `{ kind: 'rsa', scheme: 'oaep' }`.
689
+ * RSA-OAEP encrypts at most modulus bytes − 2 × hash bytes − 2 per call
690
+ * (190 bytes for a 2048-bit key with SHA-256) — encrypt a symmetric key, not
691
+ * bulk data.
692
+ *
693
+ * @param publicKey - RSA-OAEP public `CryptoKey` with `encrypt` usage
694
+ * @param plaintext - Message bytes, at most the OAEP capacity of the key
695
+ * @param options - Optional OAEP label bound to the ciphertext
696
+ *
697
+ * @throws {Error} If the key is not an RSA-OAEP public encryption key, or the
698
+ * plaintext exceeds the key's OAEP capacity
699
+ *
700
+ * @example
701
+ * ```ts
702
+ * const keys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' });
703
+ * const ciphertext = await encryptRsaOaepOrThrow(
704
+ * keys.publicKey,
705
+ * new TextEncoder().encode('session key'),
706
+ * );
707
+ * ```
708
+ *
709
+ * @see {@linkcode decryptRsaOaepOrThrow} for the inverse operation
710
+ * @see `encryptRsaOaep` for the Result-returning variant
711
+ */
712
+ declare function encryptRsaOaepOrThrow(publicKey: CryptoKey, plaintext: Uint8Array, options?: RsaOaepOptions): Promise<Uint8Array>;
713
+ /**
714
+ * Encrypt a small message with an RSA-OAEP public key.
715
+ *
716
+ * @example
717
+ * ```ts
718
+ * const keys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' });
719
+ * const encrypted = await encryptRsaOaep(keys.publicKey, plaintext);
720
+ * if (!encrypted.ok) {
721
+ * // encrypted.code: 'invalid_key' | 'message_too_long'
722
+ * throw new Error(encrypted.message);
723
+ * }
724
+ * const ciphertext = encrypted.value;
725
+ * ```
726
+ *
727
+ * @see `encryptRsaOaepOrThrow` for the throwing variant
728
+ */
729
+ declare function encryptRsaOaep(publicKey: CryptoKey, plaintext: Uint8Array, options?: RsaOaepOptions): Promise<EncryptRsaOaepResult>;
730
+ /**
731
+ * Decrypt an RSA-OAEP ciphertext with the matching private key.
732
+ *
733
+ * The key must have been generated or imported with `{ kind: 'rsa', scheme: 'oaep' }`,
734
+ * and `options.label` must repeat the label used at encryption time (if any).
735
+ *
736
+ * @param privateKey - RSA-OAEP private `CryptoKey` with `decrypt` usage
737
+ * @param ciphertext - Ciphertext produced by {@linkcode encryptRsaOaep} (or any RSA-OAEP encryptor)
738
+ * @param options - OAEP label matching the one bound at encryption
739
+ *
740
+ * @throws {Error} If the key is not an RSA-OAEP private decryption key, or
741
+ * decryption fails — wrong key, wrong label, or corrupted ciphertext (OAEP
742
+ * deliberately does not reveal which)
743
+ *
744
+ * @example
745
+ * ```ts
746
+ * const plaintext = await decryptRsaOaepOrThrow(keys.privateKey, ciphertext);
747
+ * ```
748
+ *
749
+ * @see {@linkcode encryptRsaOaepOrThrow} for the inverse operation
750
+ * @see `decryptRsaOaep` for the Result-returning variant
751
+ */
752
+ declare function decryptRsaOaepOrThrow(privateKey: CryptoKey, ciphertext: Uint8Array, options?: RsaOaepOptions): Promise<Uint8Array>;
753
+ /**
754
+ * Decrypt an RSA-OAEP ciphertext with the matching private key.
755
+ *
756
+ * @example
757
+ * ```ts
758
+ * const decrypted = await decryptRsaOaep(keys.privateKey, ciphertext);
759
+ * if (!decrypted.ok) {
760
+ * // decrypted.code: 'invalid_key' | 'decryption_failed'
761
+ * throw new Error(decrypted.message);
762
+ * }
763
+ * const plaintext = decrypted.value;
764
+ * ```
765
+ *
766
+ * @see `decryptRsaOaepOrThrow` for the throwing variant
767
+ */
768
+ declare function decryptRsaOaep(privateKey: CryptoKey, ciphertext: Uint8Array, options?: RsaOaepOptions): Promise<DecryptRsaOaepResult>;
401
769
  //#endregion
402
- export { EcKeyAlgorithmInput, EcNamedCurve, Ed25519KeyAlgorithmInput, EncryptedPkcs8Options, ImportEcKeyInput, ImportEd25519KeyInput, ImportEncryptedKeyErrorCode, ImportEncryptedKeyFailure, ImportEncryptedKeyResult, ImportKeyErrorCode, ImportKeyFailure, ImportKeyResult, ImportRsaKeyInput, KeyAlgorithmInput, KeyPairMaterial, LegacyPemEncryptionOptions, PrivateKeyImportInput, PublicKeyImportInput, RsaHash, RsaKeyAlgorithmInput, RsaScheme, exportBinaryBase64, exportEncryptedPkcs1Pem, exportEncryptedPkcs8Der, exportEncryptedPkcs8Pem, exportEncryptedSec1Pem, exportPkcs1Der, exportPkcs1Pem, exportPkcs8Der, exportPkcs8Pem, exportPrivateJwk, exportPublicJwk, exportSec1Der, exportSec1Pem, exportSpkiDer, exportSpkiPem, generateKeyPair, importEncryptedPkcs1Pem, importEncryptedPkcs8Der, importEncryptedPkcs8Pem, importEncryptedSec1Pem, importPkcs1Der, importPkcs1Pem, importPkcs8Base64, importPkcs8Der, importPkcs8Pem, importPrivateJwk, importPublicJwk, importSec1Der, importSec1Pem, importSpkiBase64, importSpkiDer, importSpkiPem };
403
- //# sourceMappingURL=keys.d.ts.map
770
+ export { DecryptRsaOaepErrorCode, DecryptRsaOaepFailure, DecryptRsaOaepResult, EcKeyAlgorithmInput, EcNamedCurve, Ed25519KeyAlgorithmInput, EncryptRsaOaepErrorCode, EncryptRsaOaepFailure, EncryptRsaOaepResult, EncryptedPkcs8Options, ImportEcKeyInput, ImportEd25519KeyInput, ImportEncryptedKeyErrorCode, ImportEncryptedKeyFailure, ImportEncryptedKeyResult, ImportKeyErrorCode, ImportKeyFailure, ImportKeyResult, ImportRsaKeyInput, KeyAlgorithmInput, KeyPairMaterial, LegacyPemEncryptionOptions, PrivateKeyImportInput, PublicKeyImportInput, RsaHash, RsaKeyAlgorithmInput, RsaOaepOptions, RsaScheme, RsaSignatureScheme, 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 };