Skip to content

fix: write encryption headers when finalizing empty streams - #2201

Open
pcbeingused333 wants to merge 2 commits into
aws:masterfrom
pcbeingused333:fix/c3647-empty-output-stream
Open

pcbeingused333 wants to merge 2 commits into
aws:masterfrom
pcbeingused333:fix/c3647-empty-output-stream

Conversation

@pcbeingused333

Copy link
Copy Markdown

Fixes #218

Closing an encrypting CryptoOutputStream without writing calls EncryptionHandler.doFinal() before processBytes(). Since only processBytes() emits the message header, the resulting ciphertext contains a body and possibly a footer but cannot be decrypted.

Initialize the first encryption operation with zero input during finalization when necessary. This writes the header and includes it in the trailing signature before finalizing the body. Account for the header offset without signing it twice.

Replace the ineffective no-op stream check with a regression that decrypts the actual bytes produced by closing without writing. Cover every algorithm with both non-framed and framed messages, and verify the empty plaintext, algorithm, and encryption context using a local JCE master key.

Testing and validation

  • An independent Java regression compiled the production sources and exercised all 11 algorithms, frame sizes 0 and 4096, and empty/nonempty input. Baseline: 22 empty-message failures and 22 nonempty passes; patched: 44 passes.
  • mvn com.coveo:fmt-maven-plugin:2.13:check passed (192 files, 0 non-complying).
  • The repository JUnit run was attempted but could not resolve the unpublished software.amazon.cryptography:TestAwsCryptographicMaterialProviders:1.7.0 artifact. The standard CI builds this test-vector dependency separately; the independent regression does not use it or AWS credentials.

AI assistance (Codex) was used to prepare this change and its regression tests.

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 16, 2026 16:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CryptoOutputStream outputs invalid message when used to encrypt empty plaintext

1 participant