Skip to content

fix(kms): a named key and an encryption algorithm are checked, not discarded (#969) - #980

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/kms-reencrypt-source-key-and-algorithms
Sep 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/kms-reencrypt-source-key-and-algorithms

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #969.

The defect

Decrypt takes a KeyId and ReEncrypt takes a SourceKeyId, and neither names the key the operation will use — substrate finds that inside its own stub ciphertext. Both members are a constraint on which key the ciphertext may belong to, and both were decoded and then discarded, so naming a different key was indistinguishable from naming the right one: the operation used the ciphertext's key and answered 200.

Separately, four encryption-algorithm members — one each on Encrypt and Decrypt, two on ReEncrypt — were decoded by nobody and answered by nobody.

The named key

AWS glosses the two members with one sentence and gives them one code, which is why one check serves both:

Enter a key ID of the KMS key that was used to encrypt the ciphertext. If you identify a different KMS key, the Decrypt operation throws an IncorrectKeyException.

Request Answers Provenance
The member is absent 200, on the ciphertext's own key Both members are published Required: No
It names the ciphertext's key 200 The constraint is satisfied
It names a different key KMS holds IncorrectKeyException/400 Published, in the gloss above
It names no key at all NotFoundException/400 Published; which of the two codes wins is substrate's reading

The comparison is between key ARNs, not between strings. The member accepts all four forms a KeyId accepts, so it is resolved to a key record first and the two records' ARNs are compared. Comparing the member against the bare key ID inside the ciphertext would have refused three of those four correct requests, and would have been wrong across accounts besides. A test sends all four forms plus the absent case.

A member naming nothing answers NotFoundException, not IncorrectKeyException. AWS publishes both codes for both operations and orders neither; the two ask the caller for different things, and go find the right key is useless advice when the identifier names no key.

The wrong key is reported before the key state, following the direction #964 took — a defect in the request precedes a condition of a resource — so a caller is not sent after the state of a key it did not ask about. AWS publishes no ordering, so this is recorded rather than matched, and a test pins it with the two candidate answers visibly different.

The encryption algorithm

The absence was defensible while it lasted: substrate performs no cryptography, and echoing SYMMETRIC_DEFAULT unconditionally would have reported a value derived from nothing. What makes it derivable is that AWS does not let a key choose its algorithm — the key spec reference states "you cannot configure a KMS key to use a particular encryption algorithm" and fixes the admissible set per key spec:

Key spec Admissible
SYMMETRIC_DEFAULT SYMMETRIC_DEFAULT
RSA_2048, RSA_3072, RSA_4096 RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256
SM2 SM2PKE
The ECC, HMAC and ML-DSA specs None

Substrate still encrypts nothing. What it models is the refusal, in two codes on different evidence:

  • a value outside the published four → ValidationError/400, which is substrate's reading (no operation page gives a code for a malformed enum member, so it comes from CommonErrors.html);
  • a published value the key spec does not admit → InvalidKeyUsageException/400, which is published — it is that code's own second gloss bullet, "the encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (KeySpec)".

The two are checked at different points: the enum before the key is resolved, following #964's ordering for a number out of range, and the key-spec compatibility after the key-state check, so a caller holding a key pending deletion hears about the deletion.

AWS's separate rule that the member is "required only for asymmetric KMS keys" falls out with no branch of its own. The default is SYMMETRIC_DEFAULT unconditionally and an RSA key does not admit it, so omitting the member on an RSA key is refused for the ordinary reason. A test asserts it, because the alternative implementation — a required-ness check keyed on the key spec — is a second thing to keep in step with the table.

ReEncrypt's two ends are independent, each checked against its own key, source first, matching the order #961 established for the two key states. A test re-encrypts from a symmetric source to an RSA destination and then swaps each end for the other's value, because an implementation validating both members against one key would pass every other assertion here.

InvalidKeyUsageException's first gloss bullet — a KeyUsage incompatible with the operation — is not modelled, and is tracked with CreateKey's acceptance of any string as a KeySpec in #977. Every non-symmetric key in these tests is created through that gap, which is noted in the test file so #977 re-verifies them.

One correction on the record

The comment explaining why GetKeyRotationStatus is deliberately unguarded said the page publishes neither key-state code. It publishes KMSInvalidStateException/400. The conclusion is unchanged and better sourced — the page's own prose documents both states as successful ("the key rotation status does not change when you disable a KMS key", "while a KMS key is pending deletion, its key rotation status is false") — so the comment now cites that rather than an absence that is not there. The equivalent sentence in #971's shipped changelog section is left as written, since a released section is not rewritten.

Verification

Nine-command gate, all green. Coverage 84.4% emulator / 83.9% total, above the 84.3%/83.8% baseline.

Nine mutations, all caught: decrypt drops the KeyId check; reEncrypt drops the SourceKeyId check; the named key compared as a string rather than resolved; IncorrectKeyException moved after the state check; an unpublished algorithm accepted; the key spec not consulted; both algorithms checked against the destination; the enum checked after the key lookup; Encrypt hardcoding the reported algorithm.

Compatibility

Three refusals are new where a 200 used to be:

  • a Decrypt/ReEncrypt whose KeyId/SourceKeyId names a key other than the ciphertext's → IncorrectKeyException/400;
  • one naming no key → NotFoundException/400;
  • an encryption algorithm outside the published four → ValidationError/400, and one the key spec does not admit → InvalidKeyUsageException/400 — which includes omitting the member on an asymmetric key, since the default is symmetric-only.

Three response members are new: Encrypt and Decrypt carry EncryptionAlgorithm; ReEncrypt carries SourceEncryptionAlgorithm and DestinationEncryptionAlgorithm.

…scarded (#969)

Decrypt's KeyId and ReEncrypt's SourceKeyId were decoded and then dropped, so
naming a different key was indistinguishable from naming the right one: the
operation used the key inside substrate's own stub ciphertext and answered 200.
AWS glosses both members with one sentence and gives them one code, so one check
serves both, comparing resolved key ARNs rather than strings so that all four
accepted identifier forms still work.

The four encryption-algorithm members on Encrypt, Decrypt and both ends of
ReEncrypt were decoded by nobody and answered by nobody. They are derivable
because AWS does not let a key choose its algorithm — "you cannot configure a KMS
key to use a particular encryption algorithm" — and fixes the admissible set per
key spec instead. A value outside the published four answers ValidationError on
substrate's reading; one the key spec does not admit answers
InvalidKeyUsageException, which is that code's own second gloss bullet.
@scttfrdmn scttfrdmn added this to the v0.118.0 milestone Sep 17, 2026
@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.89474% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/kms_plugin.go 96.66% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit c041280 into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/kms-reencrypt-source-key-and-algorithms branch September 17, 2026 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant