Skip to content

kms: the stub ciphertext carries only a key ID, so an encryption-context or algorithm mismatch cannot answer InvalidCiphertextException #979

Closed

Description

@scttfrdmn

Substrate's stub ciphertext carries a key ID and nothing else, so two refusals AWS publishes on Decrypt and ReEncrypt are unreachable: a decrypt with the wrong encryption context, and a decrypt with an algorithm other than the one that encrypted the data. Both answer InvalidCiphertextException/400 on AWS. Found while fixing #969, which added the algorithm members and could enforce a key's admissible set but not a round-trip match.

What the stub holds

kmsEncryptStub (emulator/kms_types.go):

// Format: base64(kms:{keyID}:{base64(plaintext)}).

kmsDecryptStub reverses it and returns (keyID, plaintext). Nothing else survives an encryption, so on decrypt there is nothing to compare a caller's encryption context or algorithm against.

What AWS publishes

The encryption context is the load-bearing half. API_Encrypt states the rule as a consequence rather than a footnote:

If you specify an EncryptionContext when encrypting data, you must specify the same encryption context (a case-sensitive exact match) when decrypting the data. Otherwise, the request to decrypt fails with an InvalidCiphertextException.

And InvalidCiphertextException's own gloss names it: "the request was rejected because the specified ciphertext, or additional authenticated data incorporated into the ciphertext, such as the encryption context, is corrupted, missing, or otherwise invalid." HTTP 400.

This is worth having because it is a mistake real callers make and a path real code handles. An application that encrypts with EncryptionContext: {"tenant": "acme"} and decrypts without it is broken in production and passes against substrate today, since EncryptionContext is decoded by nobody at any of the operations that take it (Encrypt, Decrypt, GenerateDataKey, GenerateDataKeyWithoutPlaintext, and ReEncrypt's source and destination pair).

The algorithm half is narrower. API_Decrypt: "Specify the same algorithm that was used to encrypt the data. If you specify a different algorithm, the Decrypt operation fails." #969 already refuses an algorithm the key's KeySpec does not admit, and a symmetric key admits exactly one algorithm — so the mismatch is reachable only for an RSA key, where RSAES_OAEP_SHA_1 and RSAES_OAEP_SHA_256 are both admissible and only one of them encrypted the data. Substrate cannot tell which without recording it.

Why it is one issue rather than two

Both need the same change and it is the only expensive part: the stub format grows, from kms:{keyID}:{plaintext} to something that also carries the algorithm and the encryption context. That has consequences worth deciding deliberately rather than in passing:

  • Every ciphertext substrate has ever produced changes shape, so a recorded run's CiphertextBlob bytes change. Nothing in the event log replays a literal ciphertext, but tests that pin one would move, and kmsStubCiphertext (a test helper) mints the old format.
  • The context is a map, so the format needs a canonical ordering to stay deterministic — sorted keys, for the reason MemoryStateManager.List is sorted.
  • ReEncrypt re-encrypts under the destination context, which is a separate member from the source one, so the operation reads one and writes the other.

Encryption context also has an authorization dimension that should be noted and not taken here: kms:EncryptionContext:<key> and kms:EncryptionContextKeys are IAM condition keys, and substrate's authorizer models neither. Decoding the member is a prerequisite for ever modelling them.

Acceptance criteria

  • The stub ciphertext carries the encryption context and the encryption algorithm alongside the key ID, canonically ordered so the blob stays deterministic.
  • Decrypt and ReEncrypt refuse a context that is not an exact case-sensitive match with InvalidCiphertextException/400, including the asymmetric cases — absent-on-encrypt-and-present-on-decrypt, and the reverse.
  • Decrypt refuses an algorithm other than the one recorded, with the same code, and the test uses an RSA key since that is the only spec where the case is reachable.
  • Encrypt, GenerateDataKey, GenerateDataKeyWithoutPlaintext and both of ReEncrypt's context members decode the member rather than ignoring it.
  • The format change is stated in the CHANGELOG's Compatibility note, and kmsStubCiphertext is updated with it.
  • The kms:EncryptionContext: condition keys are recorded as still unmodelled rather than silently implied.
  • Tests go over the wire (cfn: the aws:cloudformation:* stamp covers EC2 only, so the rest of a stack is unstamped #765) and assert the status alongside the code (fix(kms): NotFoundException is HTTP 400, and InvalidRequest is not a KMS code #923).

Activity

  1. added this to the v0.118.0 milestone on Sep 17, 2026
  2. added
    type: bug
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions