/** * Key formats: SPKI, PKCS#8, JWK, and PEM, with import-only PKCS#1 and SEC1. * * {@link decodePublic}, {@link decodePrivate}, and {@link decodeSecret} build * typed keys from encoded data, and {@link encode} writes a key in a named * format. They are plain functions, not a service: each returns an Effect that * requires nothing and runs on the `WebCrypto` reference. * * The algorithm decides the family of the result. Decoding with * `Signature.ecdsaP256` gives a `Signature` key, and with * `KeyAgreement.ecdhP256` a `KeyAgreement` key. Raw bytes are not a format * here: each family's own constructors read them. * * Format values carry their role, direction, and data type in the types: * * - {@link spki} (public) and {@link pkcs8} (private) read and write DER * bytes; * - {@link jwk} reads and writes JWK objects for every role; * - {@link pkcs1} (RSA) and {@link sec1} (EC) read private DER keys and * cannot be written; * - {@link pem} turns any DER format into PEM text. * * Decoded keys always hold bytes, in the forms each family stores, so they * work with every layer. * * **Gotchas** * * - EC private keys must include their public point. Safari and Firefox * before 132 cannot import one without it, so every runtime fails with an * `Unsupported` reason. * - SPKI accepts compressed EC points. Safari and Firefox before 146 reject * them in raw import, so the families' raw constructors take only * uncompressed points. * - Ed25519 and X25519 private JWKs need a correct `x`. Bun and Deno accept * `d` alone, and other runtimes reject it. * - JWK members other than `kty`, `crv`, `alg`, and the key material are * ignored on import, and written JWKs contain only `kty`, `crv`, and the key * material. * - Callers parse and serialize JSON themselves. * * @stability unstable * @since 4.0.0 */ import * as Effect from "../Effect.ts"; import type * as Aead from "./Aead.ts"; import type * as Cipher from "./Cipher.ts"; import * as CryptoError from "./CryptoError.ts"; import * as KeyAgreement from "./KeyAgreement.ts"; import * as KeyWrap from "./KeyWrap.ts"; import type * as Mac from "./Mac.ts"; import * as Signature from "./Signature.ts"; /** * The role of a key: public, private, or secret (symmetric). * * @stability unstable * @category models * @since 4.0.0 */ export type Role = "public" | "private" | "secret"; /** * Whether a format can be read, written, or both. * * @stability unstable * @category models * @since 4.0.0 */ export type Direction = "decode" | "encode"; /** * A JSON Web Key (RFC 7517) with the native Web Crypto field types and * mutability, usable without DOM declarations. * * @stability unstable * @category models * @since 4.1.1 */ export interface JsonWebKey { alg?: string; crv?: string; d?: string; dp?: string; dq?: string; e?: string; ext?: boolean; k?: string; key_ops?: Array; kty?: string; n?: string; oth?: Array<{ d?: string; r?: string; t?: string; }>; p?: string; q?: string; qi?: string; use?: string; x?: string; y?: string; } /** * A key format, such as {@link spki} or `pem(pkcs8)`. * * **Details** * * `~tags` exists only in the types. It records the roles the format holds, * whether it can be read and written, and the type of its data: `Uint8Array` * for DER, `string` for PEM, and {@link JsonWebKey} for JWK. * * @stability unstable * @category models * @since 4.0.0 */ export interface Format { readonly _tag: "spki" | "pkcs8" | "jwk" | "pkcs1" | "sec1"; readonly pem: boolean; readonly "~tags"?: { readonly role: (_: R) => void; readonly direction: (_: D) => void; readonly data: Data; } | undefined; } /** * SubjectPublicKeyInfo DER (RFC 5280), for public keys. * * @stability unstable * @category formats * @since 4.0.0 */ export declare const spki: Format<"public", Direction, Uint8Array>; /** * PKCS#8 PrivateKeyInfo DER (RFC 5208), for private keys. * * @stability unstable * @category formats * @since 4.0.0 */ export declare const pkcs8: Format<"private", Direction, Uint8Array>; /** * JSON Web Key (RFC 7517), for every role. * * **Details** * * `decodePublic` rejects a JWK with `d`, and `decodePrivate` requires it. * `kty`, `crv`, and `alg` must match the algorithm when present. Written JWKs * contain only `kty`, `crv`, and the key material. * * Ed25519 and X25519 private JWKs need a correct `x`: Bun and Deno accept `d` * alone, but other runtimes reject it. * * @stability unstable * @category formats * @since 4.0.0 */ export declare const jwk: Format; /** * PKCS#1 RSAPrivateKey DER (RFC 8017), read only. * * @stability unstable * @category formats * @since 4.0.0 */ export declare const pkcs1: Format<"private", "decode", Uint8Array>; /** * SEC1 ECPrivateKey DER (RFC 5915), read only. * * **Details** * * The curve parameters, when present, must match the algorithm's curve. The * public point must be present. * * @stability unstable * @category formats * @since 4.0.0 */ export declare const sec1: Format<"private", "decode", Uint8Array>; /** * PEM (RFC 7468) over a DER format. * * **Details** * * Reading accepts exactly one block whose label matches the format: * `PUBLIC KEY`, `PRIVATE KEY`, `RSA PRIVATE KEY`, or `EC PRIVATE KEY`. * Writing uses 64-column lines. * * @stability unstable * @category formats * @since 4.0.0 */ export declare const pem: (format: Format) => Format; /** * The algorithms whose public and private keys `KeyFormat` reads and writes. * * @stability unstable * @category models * @since 4.0.0 */ export type AsymmetricAlgorithm = Signature.Algorithm | KeyAgreement.Algorithm | KeyWrap.RsaOaep; /** * The algorithms whose secret keys `KeyFormat` reads and writes, and that * `KeyWrap` wraps. * * @stability unstable * @category models * @since 4.0.0 */ export type SecretAlgorithm = Aead.Algorithm | Cipher.Algorithm | Mac.Algorithm | KeyWrap.AesKw; /** * The public key type of an algorithm. * * @stability unstable * @category models * @since 4.0.0 */ export type PublicKey = A extends Signature.Algorithm ? Signature.PublicKey : A extends KeyAgreement.Algorithm ? KeyAgreement.PublicKey : A extends KeyWrap.RsaOaep ? KeyWrap.PublicKey : never; /** * The private key type of an algorithm. * * @stability unstable * @category models * @since 4.0.0 */ export type PrivateKey = A extends Signature.Algorithm ? Signature.PrivateKey : A extends KeyAgreement.Algorithm ? KeyAgreement.PrivateKey : A extends KeyWrap.RsaOaep ? KeyWrap.PrivateKey : never; /** * The secret key type of an algorithm. * * @stability unstable * @category models * @since 4.0.0 */ export type SecretKey = A extends Aead.Algorithm ? Aead.Key : A extends Cipher.Algorithm ? Cipher.Key : A extends Mac.Algorithm ? Mac.Key : A extends KeyWrap.AesKw ? KeyWrap.Key : never; /** * Any key `KeyFormat` can write. * * @stability unstable * @category models * @since 4.0.0 */ export type Key = PublicKey | PrivateKey | SecretKey; /** * The role of a key type. * * @stability unstable * @category models * @since 4.0.0 */ export type RoleOf = K extends PublicKey ? "public" : K extends PrivateKey ? "private" : "secret"; /** * Decodes a public key. * * **Details** * * The format must hold public keys: {@link spki}, {@link jwk}, or * `pem(spki)`. The key is imported on the `WebCrypto` reference and stored as * bytes. Malformed data, a key of another algorithm or curve, or a JWK with * `d` fails with an `InvalidKey` reason. * * **Example** (Reading a PEM public key) * * ```ts * import { Effect } from "effect" * import { KeyFormat, Signature } from "effect/crypto" * * declare const text: string * * const publicKey = KeyFormat.decodePublic(KeyFormat.pem(KeyFormat.spki), Signature.ecdsaP256, text) * ``` * * @stability unstable * @category decoding * @since 4.0.0 */ export declare const decodePublic: (format: Format<"public", "decode", Data>, algorithm: A, data: NoInfer) => Effect.Effect, CryptoError.CryptoError>; /** * Decodes a private key. * * **Details** * * The format must hold private keys: {@link pkcs8}, {@link jwk}, * {@link pkcs1}, {@link sec1}, or their PEM forms. PKCS#1 and SEC1 are * converted to PKCS#8 before the import. Malformed data, a key of another * algorithm or curve, or a JWK without `d` fails with an `InvalidKey` reason. * An EC private key without its public point fails with an `Unsupported` * reason on every runtime, because Safari and Firefox before 132 cannot import * it. * * @stability unstable * @category decoding * @since 4.0.0 */ export declare const decodePrivate: (format: Format<"private", "decode", Data>, algorithm: A, data: NoInfer) => Effect.Effect, CryptoError.CryptoError>; /** * Decodes a secret key from a JWK. * * **Details** * * The JWK must have `kty` `oct` when `kty` is present, and an `alg` matching * the algorithm when `alg` is present. The key bytes go through the family's * raw constructor, so a wrong length fails with an `InvalidKey` reason. * Secret keys never touch `crypto.subtle` here. * * @stability unstable * @category decoding * @since 4.0.0 */ export declare const decodeSecret: (format: Format<"secret", "decode", Data>, algorithm: A, data: NoInfer) => Effect.Effect, CryptoError.CryptoError>; /** * Encodes a key in a format. * * **Details** * * The format must hold the key's role and be writable: {@link spki} for * public keys, {@link pkcs8} for private keys, {@link jwk} for every role, * and the PEM forms of SPKI and PKCS#8. PKCS#1 and SEC1 cannot be written. * * PEM output uses 64-column lines. JWK output contains only `kty`, `crv`, and * the key material. A non-extractable `CryptoKey`-backed key fails with a * `NotExtractable` reason. * * **Example** (Writing a public key as JWK) * * ```ts import.meta.vitest * import { Effect, Layer } from "effect" * import { KeyFormat, SecureRandom, Signature } from "effect/crypto" * * const program = Effect.gen(function*() { * const signature = yield* Signature.Signature * const { publicKey } = yield* signature.generateKeyPair(Signature.ed25519) * const jwk = yield* KeyFormat.encode(KeyFormat.jwk, publicKey) * return [jwk.kty, jwk.crv] * }) * * const layer = Signature.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto)) * await Effect.runPromise(program.pipe(Effect.provide(layer))) // => ["OKP", "Ed25519"] * ``` * * @stability unstable * @category encoding * @since 4.0.0 */ export declare const encode: (format: Format, "encode", Data>, key: K) => Effect.Effect; //# sourceMappingURL=KeyFormat.d.ts.map