import { Pbes2EncryptionOptions, Pbes2EncryptionScheme, Pbes2Parameters, Pbes2Prf, parsePbes2AlgorithmIdentifier } from "../internal/crypto/pbes2.js"; import { ErrorResult, Micro509Error } from "../result/result.js"; //#region src/keys/keys.d.ts /** Hash algorithm paired with an RSA key. */ type RsaHash = "SHA-256" | "SHA-384" | "SHA-512"; /** RSA signature padding scheme. */ type RsaSignatureScheme = "pkcs1-v1_5" | "pss"; /** * RSA padding scheme: a signature scheme, or `'oaep'` for RSA-OAEP encryption * keys (usable with {@linkcode encryptRsaOaep} / {@linkcode decryptRsaOaep}). */ type RsaScheme = RsaSignatureScheme | "oaep"; /** NIST elliptic curve for ECDSA keys. */ type EcNamedCurve = "P-256" | "P-384" | "P-521"; /** RSA variant of {@linkcode KeyAlgorithmInput}. */ interface RsaKeyAlgorithmInput { /** Discriminant selecting RSA key generation. */ readonly kind: "rsa"; /** RSA modulus size in bits. Defaults to `2048`. */ readonly modulusLength?: 2048 | 3072 | 4096; /** Hash algorithm for the key. Defaults to `'SHA-256'`. */ readonly hash?: RsaHash; /** * Padding scheme. Defaults to `'pkcs1-v1_5'`. Pass `'oaep'` to generate an * RSA-OAEP encryption pair (`encrypt`/`decrypt` usages instead of `sign`/`verify`). */ readonly scheme?: RsaScheme; } /** ECDSA variant of {@linkcode KeyAlgorithmInput}. */ interface EcKeyAlgorithmInput { /** Discriminant selecting ECDSA key generation. */ readonly kind: "ecdsa"; /** NIST curve. Defaults to `'P-256'`. */ readonly curve?: EcNamedCurve; } /** Ed25519 variant of {@linkcode KeyAlgorithmInput}. */ interface Ed25519KeyAlgorithmInput { /** Discriminant selecting Ed25519 key generation. */ readonly kind: "ed25519"; } /** Input for {@linkcode generateKeyPair}. Selects algorithm family and parameters. */ type KeyAlgorithmInput = RsaKeyAlgorithmInput | EcKeyAlgorithmInput | Ed25519KeyAlgorithmInput; /** Key pair with convenience export helpers. Returned by {@linkcode generateKeyPair}. */ interface KeyPairMaterial { /** The WebCrypto public key (extractable, `verify` usage; `encrypt` for RSA-OAEP). */ readonly publicKey: CryptoKey; /** The WebCrypto private key (extractable, `sign` usage; `decrypt` for RSA-OAEP). */ readonly privateKey: CryptoKey; /** Export the public key as DER-encoded SubjectPublicKeyInfo. */ exportSpkiDer(): Promise; /** Export the public key as PEM-encoded SubjectPublicKeyInfo. */ exportSpkiPem(): Promise; /** Export the private key as DER-encoded PKCS#8 PrivateKeyInfo. */ exportPkcs8Der(): Promise; /** Export the private key as PEM-encoded PKCS#8 PrivateKeyInfo. */ exportPkcs8Pem(): Promise; /** Export the public key as a JSON Web Key. */ exportPublicJwk(): Promise; /** Export the private key as a JSON Web Key. */ exportPrivateJwk(): Promise; } /** RSA variant of {@linkcode PublicKeyImportInput} / {@linkcode PrivateKeyImportInput}. */ interface ImportRsaKeyInput { /** Discriminant selecting RSA import. */ readonly kind: "rsa"; /** Hash algorithm. Defaults to `'SHA-256'`. */ readonly hash?: RsaHash; /** * Padding scheme. Defaults to `'pkcs1-v1_5'`. Pass `'oaep'` to import an * RSA-OAEP encryption key (`encrypt`/`decrypt` usage instead of `verify`/`sign`). */ readonly scheme?: RsaScheme; } /** ECDSA variant of {@linkcode PublicKeyImportInput} / {@linkcode PrivateKeyImportInput}. */ interface ImportEcKeyInput { /** Discriminant selecting ECDSA import. */ readonly kind: "ecdsa"; /** NIST curve the key belongs to. Required for EC import. */ readonly curve: EcNamedCurve; } /** Ed25519 variant of {@linkcode PublicKeyImportInput} / {@linkcode PrivateKeyImportInput}. */ interface ImportEd25519KeyInput { /** Discriminant selecting Ed25519 import. */ readonly kind: "ed25519"; } /** Algorithm descriptor for public key import functions. */ type PublicKeyImportInput = ImportRsaKeyInput | ImportEcKeyInput | ImportEd25519KeyInput; /** Algorithm descriptor for private key import functions. Same shape as {@linkcode PublicKeyImportInput}. */ type PrivateKeyImportInput = PublicKeyImportInput; /** PBES2 encryption options for the encrypted PKCS#8 export/import functions. */ interface EncryptedPkcs8Options { /** Password fed to PBKDF2 for key derivation. */ readonly password: string; /** PBKDF2 iteration count. Default: `100_000`. */ readonly iterations?: number; /** PBKDF2 salt. Default: 16 cryptographically random bytes. */ readonly salt?: Uint8Array; /** AES-CBC initialization vector. Default: 16 cryptographically random bytes. */ readonly iv?: Uint8Array; /** AES-CBC cipher. Default: `'AES-256-CBC'`. */ readonly cipher?: "AES-128-CBC" | "AES-192-CBC" | "AES-256-CBC"; /** PBKDF2 pseudo-random function. Default: `'HMAC-SHA-256'`. */ readonly prf?: "HMAC-SHA-1" | "HMAC-SHA-256"; } /** Options for OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM encryption (PKCS#1/SEC1). */ interface LegacyPemEncryptionOptions { /** Passphrase used to derive the encryption key. */ readonly password: string; /** 16-byte initialization vector. Random when omitted. */ readonly iv?: Uint8Array; /** AES-CBC cipher. Defaults to `'AES-256-CBC'`. */ readonly cipher?: "AES-128-CBC" | "AES-192-CBC" | "AES-256-CBC"; } /** Machine-readable failure reason for the `import*` key functions. */ type ImportKeyErrorCode = "malformed"; /** Structured failure payload for key import. */ interface ImportKeyFailure extends Micro509Error { /** Always `false` for failures. */ readonly ok: false; } /** * Success-or-failure result returned by the public `import*` key functions. * * On failure, `code` is always `'malformed'`: structurally invalid input, * algorithm mismatches, and wrong-password decryption failures all surface * the same way (see the throwing `*OrThrow` variants for raw error messages). */ type ImportKeyResult = { readonly ok: true; readonly value: T; } | ErrorResult, ImportKeyFailure>; /** * Machine-readable failure reason for the `importEncrypted*` key functions. * * Distinguishes a wrong decryption password (`'invalid_password'`) from * structurally invalid input or algorithm mismatches (`'malformed'`). */ type ImportEncryptedKeyErrorCode = "malformed" | "invalid_password"; /** Structured failure payload for encrypted key import. */ interface ImportEncryptedKeyFailure extends Micro509Error { /** Always `false` for failures. */ readonly ok: false; } /** * Success-or-failure result returned by the public `importEncrypted*` key functions. * * On failure, `code` is `'invalid_password'` when decryption failed (wrong * password or corrupted ciphertext) and `'malformed'` for everything else. */ type ImportEncryptedKeyResult = { readonly ok: true; readonly value: T; } | ErrorResult, ImportEncryptedKeyFailure>; /** Options shared by {@linkcode encryptRsaOaep} and {@linkcode decryptRsaOaep}. */ interface RsaOaepOptions { /** * Optional OAEP label bound to the ciphertext. Not encrypted, but decryption * fails unless the exact same label is presented. Default: empty. */ readonly label?: Uint8Array; } /** * Machine-readable failure reason for {@linkcode encryptRsaOaep}. * * `'invalid_key'` when the key is not an RSA-OAEP public key with `encrypt` * usage; `'message_too_long'` when the plaintext exceeds the OAEP capacity of * the key (modulus bytes − 2 × hash bytes − 2). */ type EncryptRsaOaepErrorCode = "invalid_key" | "message_too_long"; /** Structured failure payload for {@linkcode encryptRsaOaep}. */ interface EncryptRsaOaepFailure extends Micro509Error { /** Always `false` for failures. */ readonly ok: false; } /** Success-or-failure result returned by {@linkcode encryptRsaOaep}. */ type EncryptRsaOaepResult = { readonly ok: true; readonly value: Uint8Array; } | ErrorResult, EncryptRsaOaepFailure>; /** * Machine-readable failure reason for {@linkcode decryptRsaOaep}. * * `'invalid_key'` when the key is not an RSA-OAEP private key with `decrypt` * usage; `'decryption_failed'` for every ciphertext-level failure (wrong key, * wrong label, tampered or truncated ciphertext) — OAEP deliberately does not * reveal which. */ type DecryptRsaOaepErrorCode = "invalid_key" | "decryption_failed"; /** Structured failure payload for {@linkcode decryptRsaOaep}. */ interface DecryptRsaOaepFailure extends Micro509Error { /** Always `false` for failures. */ readonly ok: false; } /** Success-or-failure result returned by {@linkcode decryptRsaOaep}. */ type DecryptRsaOaepResult = { readonly ok: true; readonly value: Uint8Array; } | ErrorResult, DecryptRsaOaepFailure>; /** * Generate an asymmetric key pair for signing and verification, or — with * `{ kind: 'rsa', scheme: 'oaep' }` — for RSA-OAEP encryption and decryption. * * @example * ```ts * const ecKeys = await generateKeyPair({ kind: 'ecdsa', curve: 'P-384' }); * const rsaKeys = await generateKeyPair({ kind: 'rsa', modulusLength: 4096 }); * const edKeys = await generateKeyPair({ kind: 'ed25519' }); * const oaepKeys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' }); * * // Default: ECDSA P-256 * const keys = await generateKeyPair(); * const pem = await keys.exportPkcs8Pem(); * ``` */ declare function generateKeyPair(algorithm?: KeyAlgorithmInput): Promise; /** * Export a public key as DER-encoded SubjectPublicKeyInfo. * * @see {@linkcode importSpkiDer} for the inverse operation * @see {@linkcode exportSpkiPem} for PEM output */ declare function exportSpkiDer(publicKey: CryptoKey): Promise; /** * Export a private key as DER-encoded PKCS#8 PrivateKeyInfo. * * @see {@linkcode importPkcs8Der} for the inverse operation * @see {@linkcode exportPkcs8Pem} for PEM output * @see {@linkcode exportEncryptedPkcs8Der} for password-protected export */ declare function exportPkcs8Der(privateKey: CryptoKey): Promise; /** * Export a public key as a JSON Web Key. * * @example * ```ts * const keys = await generateKeyPair({ kind: 'ecdsa', curve: 'P-256' }); * const jwk = await exportPublicJwk(keys.publicKey); * ``` */ declare function exportPublicJwk(publicKey: CryptoKey): Promise; /** * Export a private key as a JSON Web Key. * * @see {@linkcode importPrivateJwk} for the inverse operation * @see {@linkcode exportPublicJwk} for public key export */ declare function exportPrivateJwk(privateKey: CryptoKey): Promise; /** * Export a private key as PEM-encoded PKCS#8 PrivateKeyInfo. * * @example * ```ts * const keys = await generateKeyPair(); * const pem = await exportPkcs8Pem(keys.privateKey); * // -----BEGIN PRIVATE KEY----- * // MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEH... * // -----END PRIVATE KEY----- * ``` * * @see {@linkcode importPkcs8Pem} for the inverse operation * @see {@linkcode exportEncryptedPkcs8Pem} for password-protected export */ declare function exportPkcs8Pem(privateKey: CryptoKey): Promise; /** * Export a private key as DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo. * * Uses PBES2 (PKCS#5 v2.1) with AES-CBC and PBKDF2. Compatible with OpenSSL. * * @param privateKey - The private key to export * @param options - Encryption options including password and optional algorithm settings * * @see {@linkcode importEncryptedPkcs8Der} for the inverse operation * @see {@linkcode exportEncryptedPkcs8Pem} for PEM output */ declare function exportEncryptedPkcs8Der(privateKey: CryptoKey, options: EncryptedPkcs8Options): Promise; /** * Export a private key as PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo. * * @example * ```ts * const keys = await generateKeyPair(); * const pem = await exportEncryptedPkcs8Pem(keys.privateKey, { password: 'secret' }); * // -----BEGIN ENCRYPTED PRIVATE KEY----- * // MIHsMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAc... * // -----END ENCRYPTED PRIVATE KEY----- * ``` * * @see {@linkcode importEncryptedPkcs8Pem} for the inverse operation */ declare function exportEncryptedPkcs8Pem(privateKey: CryptoKey, options: EncryptedPkcs8Options): Promise; /** * Export an RSA private key as DER-encoded PKCS#1 RSAPrivateKey. * * PKCS#1 is the legacy RSA-only format. For algorithm-agnostic export, use * {@linkcode exportPkcs8Der}. * * @throws {Error} If the key is not an RSA key * * @see {@linkcode importPkcs1Der} for the inverse operation * @see {@linkcode exportPkcs1Pem} for PEM output */ declare function exportPkcs1Der(privateKey: CryptoKey): Promise; /** * Export an RSA private key as PEM-encoded PKCS#1 RSAPrivateKey. * * @throws {Error} If the key is not an RSA key * * @see {@linkcode importPkcs1Pem} for the inverse operation * @see {@linkcode exportEncryptedPkcs1Pem} for password-protected export */ declare function exportPkcs1Pem(privateKey: CryptoKey): Promise; /** * Export an RSA private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1). * * Uses OpenSSL's traditional PEM encryption with MD5-based key derivation. * For modern encryption, prefer {@linkcode exportEncryptedPkcs8Pem}. * * @throws {Error} If the key is not an RSA key * * @see {@linkcode importEncryptedPkcs1Pem} for the inverse operation */ declare function exportEncryptedPkcs1Pem(privateKey: CryptoKey, options: LegacyPemEncryptionOptions): Promise; /** * Export an EC private key as DER-encoded SEC 1 ECPrivateKey. * * SEC 1 is the legacy EC-only format. For algorithm-agnostic export, use * {@linkcode exportPkcs8Der}. * * The output always carries the RFC 5915 `parameters [0]` named curve * (matching OpenSSL), so it re-imports via {@linkcode importSec1Der} without * an explicit curve. * * @throws {Error} If the key is not an EC key * * @see {@linkcode importSec1Der} for the inverse operation * @see {@linkcode exportSec1Pem} for PEM output */ declare function exportSec1Der(privateKey: CryptoKey): Promise; /** * Export an EC private key as PEM-encoded SEC 1 ECPrivateKey. * * @throws {Error} If the key is not an EC key * * @see {@linkcode importSec1Pem} for the inverse operation * @see {@linkcode exportEncryptedSec1Pem} for password-protected export */ declare function exportSec1Pem(privateKey: CryptoKey): Promise; /** * Export an EC private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1). * * Uses OpenSSL's traditional PEM encryption with MD5-based key derivation. * For modern encryption, prefer {@linkcode exportEncryptedPkcs8Pem}. * * @throws {Error} If the key is not an EC key * * @see {@linkcode importEncryptedSec1Pem} for the inverse operation */ declare function exportEncryptedSec1Pem(privateKey: CryptoKey, options: LegacyPemEncryptionOptions): Promise; /** * Export a public key as PEM-encoded SubjectPublicKeyInfo. * * @example * ```ts * const keys = await generateKeyPair(); * const pem = await exportSpkiPem(keys.publicKey); * ``` */ declare function exportSpkiPem(publicKey: CryptoKey): Promise; /** * Derive the matching public key from an imported (or generated) private key. * * The `import*` functions that read a PKCS#8 / PKCS#1 / SEC 1 / JWK private key * return a bare `CryptoKey` with only `sign` (or, for RSA-OAEP, `decrypt`) * usage — there is no accompanying public handle. This bridges that gap: it * exports the private key's JWK, strips the private components, and re-imports * the public half with `verify` (RSA-OAEP: `encrypt`) usage, so callers can go * straight to {@linkcode exportSpkiDer} / {@linkcode exportSpkiPem} (e.g. to * rebuild a self-signed cert or distribute the public key when only the * private key is on disk). * * Supports RSA (`n`/`e`), ECDSA (`x`/`y`), and Ed25519 (`x`). The derived key * inherits the private key's algorithm parameters (hash, curve). * * @param privateKey - An extractable private `CryptoKey` * @returns Extractable public `CryptoKey` with `verify` (RSA-OAEP: `encrypt`) usage * * @throws {Error} If the key is not a private key, is non-extractable, or uses * an unsupported key type * * @example * ```ts * const privateKey = await importPkcs8PemOrThrow(pem, { kind: 'ecdsa', curve: 'P-256' }); * const publicKey = await derivePublicKey(privateKey); * const spkiPem = await exportSpkiPem(publicKey); * ``` * * @see {@linkcode exportSpkiDer} for exporting the derived key */ declare function derivePublicKey(privateKey: CryptoKey): Promise; /** * Export a key as raw base64 (no PEM headers). * * Returns SPKI-encoded base64 for public keys, PKCS#8-encoded base64 for private keys. * Useful for compact storage or transmission where PEM overhead is undesirable. * * @throws {Error} If the key is a symmetric/secret key * * @see {@linkcode importSpkiBase64} for public key import * @see {@linkcode importPkcs8Base64} for private key import */ declare function exportBinaryBase64(key: CryptoKey): Promise; /** * Import a public key from DER-encoded SubjectPublicKeyInfo. * * When `algorithm` is omitted, the algorithm (and, for EC keys, the curve) is * inferred from the SPKI's own AlgorithmIdentifier — useful for keys whose type * isn't known ahead of time. Pass `algorithm` to additionally assert that the * DER matches an expected algorithm. * * @param der - DER-encoded SubjectPublicKeyInfo bytes * @param algorithm - Optional expected algorithm; must match key contents when given * @returns Extractable CryptoKey with `verify` usage * * @throws {Error} If DER is malformed, encodes an unsupported algorithm, or doesn't match `algorithm` * * @see {@linkcode exportSpkiDer} for the inverse operation * @see {@linkcode importSpkiPem} for PEM input */ declare function importSpkiDerOrThrow(der: Uint8Array, algorithm?: PublicKeyImportInput): Promise; /** * Import a public key from DER-encoded SubjectPublicKeyInfo. * * @see `importSpkiDerOrThrow` for the throwing variant */ declare function importSpkiDer(der: Uint8Array, algorithm?: PublicKeyImportInput): Promise>; /** * Import a public key from PEM-encoded SubjectPublicKeyInfo. * * @example * ```ts * const pem = `-----BEGIN PUBLIC KEY----- * MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE... * -----END PUBLIC KEY-----`; * const key = await importSpkiPemOrThrow(pem, { kind: 'ecdsa', curve: 'P-256' }); * ``` * * @see {@linkcode exportSpkiPem} for the inverse operation */ declare function importSpkiPemOrThrow(pem: string, algorithm?: PublicKeyImportInput): Promise; /** * Import a public key from PEM-encoded SubjectPublicKeyInfo. * * @see `importSpkiPemOrThrow` for the throwing variant */ declare function importSpkiPem(pem: string, algorithm?: PublicKeyImportInput): Promise>; /** * Import a public key from base64-encoded SubjectPublicKeyInfo (no PEM headers). * * @see {@linkcode exportBinaryBase64} for the inverse operation * @see {@linkcode importSpkiPem} for PEM input with headers */ declare function importSpkiBase64OrThrow(base64: string, algorithm?: PublicKeyImportInput): Promise; /** * Import a public key from base64-encoded SubjectPublicKeyInfo (no PEM headers). * * @see `importSpkiBase64OrThrow` for the throwing variant */ declare function importSpkiBase64(base64: string, algorithm?: PublicKeyImportInput): Promise>; /** * Import a private key from DER-encoded PKCS#8 PrivateKeyInfo. * * When `algorithm` is omitted, the algorithm (and, for EC keys, the curve) is * inferred from the PrivateKeyInfo's own `privateKeyAlgorithm` — useful for * keys whose type isn't known ahead of time. Pass `algorithm` to additionally * assert that the DER matches an expected algorithm. * * An RFC 5958 v2 `OneAsymmetricKey` carrying `attributes [0]` or `publicKey [1]` * is reduced to its v1 fields for the platform import, which Bun 1.3.14 and * earlier rejects outright * ([oven-sh/bun#35432](https://github.com/oven-sh/bun/issues/35432)). A * `publicKey [1]` field is checked against the private key it accompanies. * * @param der - DER-encoded PKCS#8 PrivateKeyInfo bytes * @param algorithm - Optional expected algorithm; must match key contents when given * @returns Extractable CryptoKey with `sign` usage * * @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 * * @see {@linkcode exportPkcs8Der} for the inverse operation * @see {@linkcode importPkcs8Pem} for PEM input * @see {@linkcode importEncryptedPkcs8Der} for encrypted PKCS#8 */ declare function importPkcs8DerOrThrow(der: Uint8Array, algorithm?: PrivateKeyImportInput): Promise; /** * Import a private key from DER-encoded PKCS#8 PrivateKeyInfo. * * @see `importPkcs8DerOrThrow` for the throwing variant */ declare function importPkcs8Der(der: Uint8Array, algorithm?: PrivateKeyImportInput): Promise>; /** * Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo. * * When `algorithm` is omitted, it is inferred from the key's own * `privateKeyAlgorithm` (see {@linkcode importPkcs8DerOrThrow}). * * @example * ```ts * const key = await importPkcs8PemOrThrow(pemString, { kind: 'ecdsa', curve: 'P-256' }); * const inferred = await importPkcs8PemOrThrow(pemString); * ``` */ declare function importPkcs8PemOrThrow(pem: string, algorithm?: PrivateKeyImportInput): Promise; /** * Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo. * * @see `importPkcs8PemOrThrow` for the throwing variant */ declare function importPkcs8Pem(pem: string, algorithm?: PrivateKeyImportInput): Promise>; /** * Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo. * * Decrypts the PBES2 envelope using the provided password, then imports the key. * * When `algorithm` is omitted, it is inferred from the decrypted key's own * `privateKeyAlgorithm` (see {@linkcode importPkcs8DerOrThrow}). * * @param der - DER-encoded EncryptedPrivateKeyInfo bytes * @param password - Decryption password * @param algorithm - Optional expected algorithm; must match decrypted key when given * * @throws {Error} If DER is malformed, password is wrong, or algorithm doesn't match * * @see {@linkcode exportEncryptedPkcs8Der} for the inverse operation */ declare function importEncryptedPkcs8DerOrThrow(der: Uint8Array, password: string, algorithm?: PrivateKeyImportInput): Promise; /** * Reads the PBES2 encryption parameters of a DER PKCS#8 * `EncryptedPrivateKeyInfo` (RFC 5958 §3) without the password: PBKDF2 * iteration count, salt, and PRF, plus the AES-CBC variant and IV. * Throws on malformed DER or a non-PBES2 encryption algorithm. */ declare function inspectEncryptedPkcs8Der(der: Uint8Array): Pbes2Parameters; /** * Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo. * * @see `importEncryptedPkcs8DerOrThrow` for the throwing variant */ declare function importEncryptedPkcs8Der(der: Uint8Array, password: string, algorithm?: PrivateKeyImportInput): Promise>; /** * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo. * * When `algorithm` is omitted, it is inferred from the decrypted key's own * `privateKeyAlgorithm` (see {@linkcode importPkcs8DerOrThrow}). * * @example * ```ts * const key = await importEncryptedPkcs8PemOrThrow(pem, 'secret', { kind: 'rsa' }); * const inferred = await importEncryptedPkcs8PemOrThrow(pem, 'secret'); * ``` */ declare function importEncryptedPkcs8PemOrThrow(pem: string, password: string, algorithm?: PrivateKeyImportInput): Promise; /** * Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo. * * @see `importEncryptedPkcs8PemOrThrow` for the throwing variant */ declare function importEncryptedPkcs8Pem(pem: string, password: string, algorithm?: PrivateKeyImportInput): Promise>; /** * Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey. * * PKCS#1 is the legacy RSA-only format. Internally converts to PKCS#8 for import. * * @see {@linkcode exportPkcs1Der} for the inverse operation * @see {@linkcode importPkcs1Pem} for PEM input */ declare function importPkcs1DerOrThrow(der: Uint8Array, algorithm?: ImportRsaKeyInput): Promise; /** * Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey. * * @see `importPkcs1DerOrThrow` for the throwing variant */ declare function importPkcs1Der(der: Uint8Array, algorithm?: ImportRsaKeyInput): Promise>; /** * Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey. * * Expects the `-----BEGIN RSA PRIVATE KEY-----` PEM label. * * @see {@linkcode exportPkcs1Pem} for the inverse operation * @see {@linkcode importEncryptedPkcs1Pem} for encrypted PEM */ declare function importPkcs1PemOrThrow(pem: string, algorithm?: ImportRsaKeyInput): Promise; /** * Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey. * * @see `importPkcs1PemOrThrow` for the throwing variant */ declare function importPkcs1Pem(pem: string, algorithm?: ImportRsaKeyInput): Promise>; /** * Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1). * * Decrypts OpenSSL's traditional PEM encryption format. * * @see {@linkcode exportEncryptedPkcs1Pem} for the inverse operation * @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption */ declare function importEncryptedPkcs1PemOrThrow(pem: string, password: string, algorithm?: ImportRsaKeyInput): Promise; /** * Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1). * * @see `importEncryptedPkcs1PemOrThrow` for the throwing variant */ declare function importEncryptedPkcs1Pem(pem: string, password: string, algorithm?: ImportRsaKeyInput): Promise>; /** * Import a private key from base64-encoded PKCS#8 PrivateKeyInfo (no PEM headers). * * @see {@linkcode exportBinaryBase64} for the inverse operation * @see {@linkcode importPkcs8Pem} for PEM input with headers */ declare function importPkcs8Base64OrThrow(base64: string, algorithm?: PrivateKeyImportInput): Promise; /** * Import a private key from base64-encoded PKCS#8 PrivateKeyInfo (no PEM headers). * * @see `importPkcs8Base64OrThrow` for the throwing variant */ declare function importPkcs8Base64(base64: string, algorithm?: PrivateKeyImportInput): Promise>; /** * Import an EC private key from DER-encoded SEC 1 ECPrivateKey. * * SEC 1 is the legacy EC-only format. Internally converts to PKCS#8 for import. * When the ECPrivateKey carries the optional RFC 5915 `parameters [0]` field * (OpenSSL always writes it), its named-curve OID must match `algorithm.curve`; * when the field is absent, the caller-supplied curve is trusted. * * When `algorithm` is omitted, the curve is inferred from the embedded * `parameters [0]` field; a key without a supported named curve then fails. * * @throws {Error} If DER is not an ECPrivateKey, its embedded curve doesn't * match `algorithm`, or no curve is available (neither embedded nor supplied) * * @see {@linkcode exportSec1Der} for the inverse operation * @see {@linkcode importSec1Pem} for PEM input */ declare function importSec1DerOrThrow(der: Uint8Array, algorithm?: ImportEcKeyInput): Promise; /** * Import an EC private key from DER-encoded SEC 1 ECPrivateKey. * * @see `importSec1DerOrThrow` for the throwing variant */ declare function importSec1Der(der: Uint8Array, algorithm?: ImportEcKeyInput): Promise>; /** * Import an EC private key from PEM-encoded SEC 1 ECPrivateKey. * * Expects the `-----BEGIN EC PRIVATE KEY-----` PEM label. When `algorithm` is * omitted, the curve is inferred from the embedded `parameters [0]` field * (see {@linkcode importSec1DerOrThrow}). * * @see {@linkcode exportSec1Pem} for the inverse operation * @see {@linkcode importEncryptedSec1Pem} for encrypted PEM */ declare function importSec1PemOrThrow(pem: string, algorithm?: ImportEcKeyInput): Promise; /** * Import an EC private key from PEM-encoded SEC 1 ECPrivateKey. * * @see `importSec1PemOrThrow` for the throwing variant */ declare function importSec1Pem(pem: string, algorithm?: ImportEcKeyInput): Promise>; /** * Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1). * * Decrypts OpenSSL's traditional PEM encryption format. * * @see {@linkcode exportEncryptedSec1Pem} for the inverse operation * @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption */ declare function importEncryptedSec1PemOrThrow(pem: string, password: string, algorithm?: ImportEcKeyInput): Promise; /** * Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1). * * @see `importEncryptedSec1PemOrThrow` for the throwing variant */ declare function importEncryptedSec1Pem(pem: string, password: string, algorithm?: ImportEcKeyInput): Promise>; /** * Import a public verification key from a JSON Web Key. * * When `algorithm` is omitted, it is inferred from the JWK's own `kty`, `crv`, * and `alg` members (e.g. `PS256` → RSA-PSS/SHA-256, `RSA-OAEP-256` → * RSA-OAEP/SHA-256; an RSA JWK without `alg` defaults to PKCS#1 v1.5 with * SHA-256). Pass `algorithm` to additionally assert an expected algorithm. * * @param jwk - JSON Web Key object with public key components * @param algorithm - Optional expected algorithm; must match JWK's `kty` and `crv` when given * @returns Extractable CryptoKey with `verify` usage * * @throws {Error} If JWK is malformed, encodes an unsupported algorithm, or doesn't match `algorithm` * * @see {@linkcode exportPublicJwk} for the inverse operation */ declare function importPublicJwkOrThrow(jwk: JsonWebKey, algorithm?: PublicKeyImportInput): Promise; /** * Import a public verification key from a JSON Web Key. * * @see `importPublicJwkOrThrow` for the throwing variant */ declare function importPublicJwk(jwk: JsonWebKey, algorithm?: PublicKeyImportInput): Promise>; /** * Import a private signing key from a JSON Web Key. * * When `algorithm` is omitted, it is inferred from the JWK's own `kty`, `crv`, * and `alg` members (see {@linkcode importPublicJwkOrThrow}). * * @param jwk - JSON Web Key object with private key components * @param algorithm - Optional expected algorithm; must match JWK's `kty` and `crv` when given * @returns Extractable CryptoKey with `sign` usage * * @throws {Error} If JWK is malformed, lacks private key material, encodes an * unsupported algorithm, or doesn't match `algorithm` * * @example * ```ts * const jwk = { kty: 'EC', crv: 'P-256', x: '...', y: '...', d: '...' }; * const key = await importPrivateJwkOrThrow(jwk, { kind: 'ecdsa', curve: 'P-256' }); * const inferred = await importPrivateJwkOrThrow(jwk); * ``` * * @see {@linkcode exportPrivateJwk} for the inverse operation */ declare function importPrivateJwkOrThrow(jwk: JsonWebKey, algorithm?: PrivateKeyImportInput): Promise; /** * Import a private signing key from a JSON Web Key. * * @see `importPrivateJwkOrThrow` for the throwing variant */ declare function importPrivateJwk(jwk: JsonWebKey, algorithm?: PrivateKeyImportInput): Promise>; /** * Encrypt a small message with an RSA-OAEP public key. * * The key must have been generated or imported with `{ kind: 'rsa', scheme: 'oaep' }`. * RSA-OAEP encrypts at most modulus bytes − 2 × hash bytes − 2 per call * (190 bytes for a 2048-bit key with SHA-256) — encrypt a symmetric key, not * bulk data. * * @param publicKey - RSA-OAEP public `CryptoKey` with `encrypt` usage * @param plaintext - Message bytes, at most the OAEP capacity of the key * @param options - Optional OAEP label bound to the ciphertext * * @throws {Error} If the key is not an RSA-OAEP public encryption key, or the * plaintext exceeds the key's OAEP capacity * * @example * ```ts * const keys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' }); * const ciphertext = await encryptRsaOaepOrThrow( * keys.publicKey, * new TextEncoder().encode('session key'), * ); * ``` * * @see {@linkcode decryptRsaOaepOrThrow} for the inverse operation * @see `encryptRsaOaep` for the Result-returning variant */ declare function encryptRsaOaepOrThrow(publicKey: CryptoKey, plaintext: Uint8Array, options?: RsaOaepOptions): Promise; /** * Encrypt a small message with an RSA-OAEP public key. * * @example * ```ts * const keys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' }); * const encrypted = await encryptRsaOaep(keys.publicKey, plaintext); * if (!encrypted.ok) { * // encrypted.code: 'invalid_key' | 'message_too_long' * throw new Error(encrypted.message); * } * const ciphertext = encrypted.value; * ``` * * @see `encryptRsaOaepOrThrow` for the throwing variant */ declare function encryptRsaOaep(publicKey: CryptoKey, plaintext: Uint8Array, options?: RsaOaepOptions): Promise; /** * Decrypt an RSA-OAEP ciphertext with the matching private key. * * The key must have been generated or imported with `{ kind: 'rsa', scheme: 'oaep' }`, * and `options.label` must repeat the label used at encryption time (if any). * * @param privateKey - RSA-OAEP private `CryptoKey` with `decrypt` usage * @param ciphertext - Ciphertext produced by {@linkcode encryptRsaOaep} (or any RSA-OAEP encryptor) * @param options - OAEP label matching the one bound at encryption * * @throws {Error} If the key is not an RSA-OAEP private decryption key, or * decryption fails — wrong key, wrong label, or corrupted ciphertext (OAEP * deliberately does not reveal which) * * @example * ```ts * const plaintext = await decryptRsaOaepOrThrow(keys.privateKey, ciphertext); * ``` * * @see {@linkcode encryptRsaOaepOrThrow} for the inverse operation * @see `decryptRsaOaep` for the Result-returning variant */ declare function decryptRsaOaepOrThrow(privateKey: CryptoKey, ciphertext: Uint8Array, options?: RsaOaepOptions): Promise; /** * Decrypt an RSA-OAEP ciphertext with the matching private key. * * @example * ```ts * const decrypted = await decryptRsaOaep(keys.privateKey, ciphertext); * if (!decrypted.ok) { * // decrypted.code: 'invalid_key' | 'decryption_failed' * throw new Error(decrypted.message); * } * const plaintext = decrypted.value; * ``` * * @see `decryptRsaOaepOrThrow` for the throwing variant */ declare function decryptRsaOaep(privateKey: CryptoKey, ciphertext: Uint8Array, options?: RsaOaepOptions): Promise; //#endregion export { DecryptRsaOaepErrorCode, DecryptRsaOaepFailure, DecryptRsaOaepResult, EcKeyAlgorithmInput, EcNamedCurve, Ed25519KeyAlgorithmInput, EncryptRsaOaepErrorCode, EncryptRsaOaepFailure, EncryptRsaOaepResult, EncryptedPkcs8Options, ImportEcKeyInput, ImportEd25519KeyInput, ImportEncryptedKeyErrorCode, ImportEncryptedKeyFailure, ImportEncryptedKeyResult, ImportKeyErrorCode, ImportKeyFailure, ImportKeyResult, ImportRsaKeyInput, KeyAlgorithmInput, KeyPairMaterial, LegacyPemEncryptionOptions, type Pbes2EncryptionOptions, type Pbes2EncryptionScheme, type Pbes2Parameters, type Pbes2Prf, 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, inspectEncryptedPkcs8Der, parsePbes2AlgorithmIdentifier }; //# sourceMappingURL=keys.d.ts.map