import type { JweCipher, JweDecryptOptions, JweEncryptOptions, JweHeaderParams } from './header.js'; import type { JweKeyManagementDecryptKey, JweKeyManagementEncryptKey } from './key-management.js'; /** * Parameters required for decrypting a flattened JWE. */ export interface FlattenedJweDecryptParams { /** The flattened JWE. */ jwe: FlattenedJweParams | FlattenedJwe; /** * The decryption key which can be a Key Identifier such as a KMS key URI, a JSON Web Key (JWK), * raw key material represented as a byte array, or an ECDH-ES key agreement input. */ key: JweKeyManagementDecryptKey; /** * Cipher used to decrypt the JWE payload when the Content Encryption Key is referenced by a * Key Identifier (e.g. a KMS URI) rather than provided as a JWK. Required only for * Key Identifier CEKs. */ keyManager?: JweCipher; /** {@inheritDoc JweDecryptOptions} */ options: JweDecryptOptions; } /** * Result of decrypting a flattened JWE, containing the plaintext and related information. */ export interface FlattenedJweDecryptResult { /** JWE Additional Authenticated Data (AAD). */ additionalAuthenticatedData?: Uint8Array; /** Plaintext. */ plaintext: Uint8Array; /** JWE Protected Header. */ protectedHeader?: Partial; /** JWE Shared Unprotected Header. */ sharedUnprotectedHeader?: Partial; /** JWE Per-Recipient Unprotected Header. */ unprotectedHeader?: Partial; } /** * Parameters for encrypting data into a flattened JWE format. */ export interface FlattenedJweEncryptParams extends FlattenedJweDecryptResult { /** * The encryption key which can be a Key Identifier such as a KMS key URI, a JSON Web Key (JWK), * raw key material represented as a byte array, or an ECDH-ES key agreement input. */ key: JweKeyManagementEncryptKey; /** * Cipher used to encrypt the JWE payload when the Content Encryption Key is referenced by a * Key Identifier (e.g. a KMS URI) rather than provided as a JWK. Required only for * Key Identifier CEKs. */ keyManager?: JweCipher; /** {@inheritDoc JweEncryptOptions} */ options?: JweEncryptOptions; } /** * Represents the parameters for a flattened JWE object, typically used in single-recipient * scenarios. */ export interface FlattenedJweParams { /** Base64URL encoded additional authenticated data. */ aad?: string; /** Base64URL encoded ciphertext. */ ciphertext: string; /** Base64URL encoded encrypted key. */ encrypted_key?: string; /** Per-Recipient Unprotected Header parameters. */ header?: Partial; /** Base64URL encoded initialization vector. */ iv?: string; /** Base64URL encoded string of the Protected Header. */ protected?: string; /** Base64URL encoded authentication tag. */ tag?: string; /** Shared Unprotected Header parameters. */ unprotected?: Partial; } /** * The `FlattenedJwe` class handles the encryption and decryption of JSON Web Encryption (JWE) * objects in the flattened serialization format. This format is a compact, URL-safe means of * representing encrypted content, typically used when dealing with a single recipient or when * bandwidth efficiency is important. * * This class provides methods to encrypt plaintext to a flattened JWE and decrypt a flattened JWE * back to plaintext, utilizing a variety of supported cryptographic algorithms as specified in the * JWE header parameters. * * @example * ```ts * // Example usage of encrypt method * const plaintext = new TextEncoder().encode("Secret Message"); * const key = { kty: "oct", k: "your-secret-key" }; // Example symmetric key * const protectedHeader = { alg: "dir", enc: "A256GCM" }; * const encryptedJwe = await FlattenedJwe.encrypt({ * plaintext, * protectedHeader, * key, * }); * ``` * * @example * // Decryption example * const { plaintext, protectedHeader } = await FlattenedJwe.decrypt({ * jwe: yourFlattenedJweObject, * key: yourDecryptionKey, * options: { allowedAlgs: ['dir'], allowedEncs: ['A256GCM'] }, * }); */ export declare class FlattenedJwe { /** Base64URL encoded additional authenticated data. */ aad?: string; /** Base64URL encoded ciphertext. */ ciphertext: string; /** Base64URL encoded encrypted key. */ encrypted_key?: string; /** Per-Recipient Unprotected Header parameters. */ header?: Partial; /** Base64URL encoded initialization vector. */ iv?: string; /** Base64URL encoded string of the Protected Header. */ protected?: string; /** Base64URL encoded authentication tag. */ tag?: string; /** Shared Unprotected Header parameters. */ unprotected?: Partial; constructor(params: FlattenedJweParams); static decrypt({ jwe, key, keyManager, options }: FlattenedJweDecryptParams): Promise; static encrypt({ key, plaintext, additionalAuthenticatedData, protectedHeader, sharedUnprotectedHeader, unprotectedHeader, keyManager, }: FlattenedJweEncryptParams): Promise; /** * Parses and validates the JOSE Header components of a flattened JWE (`protected`, `header`, * `unprotected`), verifies the JWE Ciphertext is present, decodes the JWE Protected Header, * checks for duplicate Header Parameter names, and validates that the resulting JOSE Header * contains the required "alg" and "enc" values. * * @param jwe - The flattened JWE (or its parameter shape) to parse. * @returns The parsed JWE Protected Header (if present) and the merged, validated JOSE Header. * @throws {@link CryptoError} if the JOSE header objects or Ciphertext are missing. * @throws Throws a plain `Error` if the JWE Protected Header is malformed, contains duplicate * Header Parameter names, or the merged JOSE Header is missing required parameters. */ private static parseAndValidateJoseHeader; /** * Enforces the caller-supplied "alg" and "enc" allow-lists on the resolved JOSE Header, * preventing algorithm-confusion attacks between callers that share the same decryption engine. * * @param joseHeader - The validated JOSE Header. * @param options - The decrypt options containing the allow-lists. * @throws {@link CryptoError} if the "alg" or "enc" value is not in the caller's allow-list. */ private static enforceAllowedAlgorithms; /** * Resolves the Content Encryption Key (CEK) for decryption by delegating to * {@link JweKeyManagement.decrypt}. If key management processing fails for a reason other than * an invalid JWE or unsupported algorithm, a random CEK is substituted instead of propagating * the error, per * {@link https://datatracker.ietf.org/doc/html/rfc7516#section-11.5 | RFC 7516 Section 11.5} and * {@link https://datatracker.ietf.org/doc/html/rfc3218 | RFC 3218} timing-attack mitigations. * * @param params - The CEK resolution parameters. * @returns A Promise that resolves to the CEK (a Key Identifier or JWK). * @throws {@link CryptoError} with code `InvalidJwe` or `AlgorithmNotSupported` if key * management processing fails for those reasons. */ private static resolveContentEncryptionKey; /** * Decrypts the JWE Ciphertext using the resolved Content Encryption Key (CEK): via the * injected `keyManager` when the CEK is a Key Identifier, or via the content encryption * primitives when the CEK is a JWK. * * @param params - The ciphertext decryption parameters. * @returns A Promise that resolves to the decrypted plaintext. * @throws {@link CryptoError} if the CEK is a Key Identifier and no `keyManager` was provided. */ private static decryptCiphertext; /** * Validates the inputs to {@link FlattenedJwe.encrypt} and builds the merged JOSE Header. * * Verifies that at least one JOSE header object is present, that the plaintext is a byte array, * that there are no duplicate Header Parameter names across the header objects, and that the * merged JOSE Header contains the required "alg" and "enc" values. * * @param params - The encrypt inputs to validate. * @returns The merged, validated JOSE Header. * @throws {@link CryptoError} if the JOSE header objects or plaintext are missing. * @throws Throws a plain `Error` if there are duplicate Header Parameter names or the merged * JOSE Header is missing required parameters. */ private static validateAndBuildEncryptJoseHeader; /** * Encrypts the plaintext using the resolved Content Encryption Key (CEK): via the injected * `keyManager` when the CEK is a Key Identifier, or via the content encryption primitives when * the CEK is a JWK. * * @param params - The plaintext encryption parameters. * @returns A Promise that resolves to the ciphertext with the authentication tag appended. * @throws {@link CryptoError} if the CEK is a Key Identifier and no `keyManager` was provided. */ private static encryptCiphertext; /** * Assembles the Flattened JWE JSON Serialization output from its encoded components. * * This is based upon the General syntax, but flattened for the single-recipient case: it * removes the "recipients" member and instead places the members defined for use in the * "recipients" array (the "header" and "encrypted_key" members) in the top-level JSON object * (at the same level as the "ciphertext" member). * * @param params - The encoded JWE components. * @returns The assembled {@link FlattenedJwe}. */ private static buildFlattenedJwe; } //# sourceMappingURL=flattened.d.ts.map