Files
floci/src/main/java
Ahmed Soliman 6a8c44fad5 fix(kms): non-deterministic Encrypt and EncryptionContext AAD validation (#825)
* fix(kms): non-deterministic Encrypt and EncryptionContext AAD validation

Two security-parity bugs in the KMS mock:

- Encrypt was deterministic: identical Plaintext + KeyId + EncryptionContext
  produced byte-identical CiphertextBlob, leaking plaintext equality. Real
  AWS derives a fresh AES-GCM key per call and is non-deterministic.

- Decrypt ignored EncryptionContext, so a ciphertext encrypted under
  {tenant: "1"} decrypted successfully under {tenant: "999"} (or no context
  at all). Real AWS binds EncryptionContext as AAD and fails with
  InvalidCiphertextException on any mismatch.

Switched the mock blob format to embed (a) a fresh 8-byte random nonce per
call and (b) a SHA-256 fingerprint of the canonicalized, sorted
EncryptionContext. Decrypt now rejects on fingerprint mismatch. Omitted
context and {} are interchangeable, matching AWS.

Threaded EncryptionContext through Encrypt / Decrypt / GenerateDataKey /
GenerateDataKeyWithoutPlaintext / ReEncrypt handlers (ReEncrypt honors
both SourceEncryptionContext and DestinationEncryptionContext).

Closes #816
Closes #817

* fix(kms): address review feedback on PR #825

- Accept legacy v1 blobs (kms:<keyId>:<base64>) in parseBlob, for
  back-compat with persistent stores that hold pre-v2 ciphertext.
- Cache SecureRandom as a static final field instead of allocating
  per Encrypt call.
- Replace the double-parseBlob pattern in handleDecrypt and
  handleReEncrypt with a new single-pass KmsService.decryptAndResolveKey
  method that returns plaintext + source key ARN in one record.
  decrypt() and decryptToKeyArn() remain independent primitives and
  do not delegate through the new composite.
- Add reEncryptHonorsSourceContext test asserting that wrong source
  context fails the decrypt half and that the destination blob is
  bound to destination context, not source.
- Add legacyV1BlobDecryptsForBackCompat test pinning the v1 fallback
  parser behavior (accepts empty/null context, rejects non-empty
  context, resolves source key ARN).
- Clean up inline fully-qualified class references in tests to use
  imports (Arrays, LinkedHashMap).

* test(kms): pin v1↔v2 prefix collision behavior

Locks in the documented limitation that a pre-PR v1 ciphertext encrypted
under an override-id of literally "v2" produces "kms:v2:<base64>", which
is indistinguishable by prefix from a malformed v2 blob and is rejected
by parseBlob with InvalidCiphertextException.

Trade-off, not a bug:
- Failure is loud (InvalidCiphertextException), not silent corruption.
- Conditions to hit are extremely narrow (persistent mode + override-id
  + key literally named "v2" + crossing the pre-PR upgrade boundary).
- Avoiding it would require either silently-lenient fallback parsing or
  a v2-prefix rename, both with their own trade-offs.

If a future refactor changes BLOB_PREFIX_V2 or adds fall-through parsing,
this test fails — prompting a deliberate decision rather than a silent
behavior change.
2026-05-14 07:12:29 -05:00
..