Repository navigation
feat(kms): decode EncryptionContext, and refuse a ciphertext that disagrees (#979) - #990
Merged
Merged
Conversation
…agrees (#979) All six encryption-context members went undecoded, so a context was accepted and lost and the refusal AWS publishes for a mismatch was unreachable. The stub ciphertext becomes a base64-wrapped JSON envelope recording the key ID, the encryption algorithm, the encryption context and the plaintext, behind a format marker. Decrypt and ReEncrypt's source end now answer InvalidCiphertextException/400 for a context that is not an exact case-sensitive match, and for an encryption algorithm that the key admits but which did not write those bytes. A context is recorded only under a symmetric encryption key, because AWS's asymmetric ciphertext format holds no metadata. This changes the ciphertext format: a blob from an earlier release no longer decodes.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #979.
Encrypt,Decrypt, bothGenerateDataKey*operations and both ends ofReEncrypttake an encryption context. Substrate decoded none of the six members, so a context was accepted and lost, and the refusal AWS publishes for a mismatch could not be reached.API_Encryptstates the rule as a consequence rather than a footnote:An application that encrypts under
{"tenant": "acme"}and decrypts without it is broken in production and passed here. That is the whole of the issue.The format change, and why it is JSON
The stub ciphertext was
base64("kms:{keyID}:" + base64(plaintext))— enough forDecryptto find its key and nothing else. It is now a base64-wrapped JSON envelope carrying the key ID, the encryption algorithm, the encryption context and the plaintext, behind a format marker.JSON rather than a delimited string is a correctness requirement, not a preference. An encryption context is caller-supplied text, so a key or a value may contain any delimiter. A delimited format needs escaping, and an escaping bug produces a ciphertext that decrypts to the wrong context — the exact failure this feature exists to detect.
json.Marshalalso sorts map keys, so the blob is deterministic without a canonicalization pass of substrate's own; a test asserts that twoEncryptcalls whose contexts differ only in JSON key order return byte-identical ciphertext, because a blob varying with the caller's ordering could not be replayed.The marker is a version string rather than a bare tag, so a blob from the previous format fails to decode rather than being read with the wrong field meanings. A test walks five foreign blobs — not base64, base64 but not JSON, JSON without the marker, the pre-#979 format, and an envelope naming no key — and all five answer
InvalidCiphertextException/400.Two refusals, and which half of each is published
{}from an omitted memberInvalidCiphertextException/400API_Encrypt's sentence, the condition by the code's own gloss ("…such as the encryption context, is corrupted, missing, or otherwise invalid")InvalidCiphertextException/400Decryptoperation fails"); the code is substrate's reading, resting on "or otherwise invalid"Eight mismatch shapes are walked, including the two the issue names — recorded-and-none-supplied, and none-recorded-and-one-supplied. Neither is a special case in the code (
maps.Equalanswers both), which is exactly why both are asserted: an implementation comparing only the keys it was given passes the value rows and fails these two.The algorithm is checked before the context, and both after the named-key, key-usage, key-state and key-spec checks. AWS orders none of it, so both orderings are recorded as substrate's: the algorithm is what a real implementation needs to attempt a decryption at all, and an algorithm the key does not admit is a fact about the key, knowable without holding the ciphertext, where a mismatch is a fact about these particular bytes.
IncorrectKeyExceptionis the near miss for both and is wrong for both — it is about the key the caller named, whichcheckNamedKeyMatchesalready answers.Both messages render both contexts, which is licensed rather than assumed: the member's gloss is "a collection of non-secret key-value pairs", and
API_GenerateDataKeyadds "do not include confidential or sensitive information in this field. This field may be displayed in plaintext in CloudTrail logs and other output." A refusal a caller cannot act on is not worth answering.The asymmetric split is AWS's, in two different directions
The context is recorded only under a symmetric encryption key.
API_ReEncryptis decisive: "a destination encryption context is valid only when the destination KMS key is a symmetric encryption KMS key. The standard ciphertext format for asymmetric KMS keys does not include fields for metadata." An asymmetric ciphertext has nowhere to hold one and AWS publishes no code for sending one, so recording it would invent a refusal AWS cannot produce. Substrate accepts the member and ignores it — #827's honest reading — and the test for it asserts the opposite shape from every other one here: two different contexts across anEncryptand aDecryptunder an RSA key must succeed. A later change that recorded the context unconditionally fails exactly that and nothing else.The algorithm is recorded for every key regardless, although AWS's asymmetric format holds no metadata either. AWS reaches the same observable answer cryptographically — decrypting RSA ciphertext with the wrong OAEP hash fails — and substrate models the observation, not the mechanism. That is the scope rule applied rather than bent: the divergence is in bytes inside the blob, which no caller is entitled to read, and the agreement is in the answer, which every caller is. It is also the only way to reach the algorithm refusal at all, since
SYMMETRIC_DEFAULTadmits exactly one algorithm and so has nothing to disagree about.ReEncryptis where the two members are visibly different thingsSourceEncryptionContextis matched against the incoming blob — "enter the same encryption context that was used to encrypt the ciphertext" — whileDestinationEncryptionContextis written into the outgoing one. So aReEncryptis the call that changes a ciphertext's context, and one consequence is worth stating because nothing in AWS's text suggests otherwise: re-encrypting with no destination context strips the context rather than inheriting the source's. A caller relying on inheritance would find its data readable without the context it believed it had set, so a test pins it.Both
GenerateDataKey*operations record the context and neither publishesInvalidCiphertextException, which is consistent — the recording happens there and the refusal atDecrypt. The assertion is the only thing a caller can observe, since the data key is opaque: the wrapped key decrypts under the recorded context and is refused without it.One shared matcher
DecryptandReEncrypt's source end go through onekmsCheckCiphertextMatchesRequest, on the #961/#969 precedent — those issues found one operation refusing nothing that its four siblings refused, which is what a per-site check invites. The member names differ between the two (EncryptionAlgorithmagainstSourceEncryptionAlgorithm) so they are passed in, followingkmsResolveEncryptionAlgorithm.kmsIsSymmetricEncryptionKeyis a named predicate rather than an inline comparison because the condition is narrower than "not asymmetric" — the fourHMAC_*specs are symmetric, hold key material and encrypt nothing.kmsReportsKeyMaterialID(#978) now routes through the same predicate, since it decides the identical question.Recorded as still unmodelled
kms:EncryptionContext:<key>andkms:EncryptionContextKeysare not evaluated, so a key policy or grant constraining a context is accepted and has no effect on authorization. The published limits on a context's total size and on the reservedaws:key prefix are likewise unenforced. Both are stated indocs/services.mdrather than left to be discovered.Verification
The nine-command gate passes. Coverage holds the baseline at 84.4% emulator / 83.9% total. Every assertion goes over the wire (#765) and asserts the status alongside the code (#923).
Compatibility
A ciphertext written by an earlier release no longer decodes.
DecryptandReEncryptanswerInvalidCiphertextException/400 for it rather than reading its fields under the new meanings. Any test holding a hard-coded substrate ciphertext, or one persisted across releases, must be regenerated by callingEncryptagain. A caller that sends an encryption context on encrypt and not on decrypt now gets the refusal AWS gives, where it previously got a200.