Repository navigation
Description
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
EncryptionContextwhen 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 anInvalidCiphertextException.
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
CiphertextBlobbytes change. Nothing in the event log replays a literal ciphertext, but tests that pin one would move, andkmsStubCiphertext(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.Listis sorted. ReEncryptre-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.
-
DecryptandReEncryptrefuse a context that is not an exact case-sensitive match withInvalidCiphertextException/400, including the asymmetric cases — absent-on-encrypt-and-present-on-decrypt, and the reverse. -
Decryptrefuses 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,GenerateDataKeyWithoutPlaintextand both ofReEncrypt's context members decode the member rather than ignoring it. - The format change is stated in the CHANGELOG's Compatibility note, and
kmsStubCiphertextis 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
feat(kms): decode EncryptionContext, and refuse a ciphertext that dis…