Skip to content

feat(kms): validate KeySpec and KeyUsage, and pair them (#977) - #986

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/kms-key-spec-usage-validation
Sep 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/kms-key-spec-usage-validation

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #977.

CreateKey accepted any string for KeySpec and KeyUsage and paired them however a caller asked, so substrate could hold an ECC key with KeyUsage ENCRYPT_DECRYPT, or a key whose spec was rsa2048. Both members are immutable — "you can't change the KeySpec after the KMS key is created", and the same sentence for KeyUsage — so an unvalidated value is not a member a later call can correct. It is a key that will never behave as its own metadata says, and every operation reading either member reports a value AWS would never have stored.

Three defects, three answers

Request Substrate answers Provenance
A KeySpec or KeyUsage outside its published enum (17 specs, 4 usages) ValidationError/400, listing the enum Substrate's reading: API_CreateKey publishes 13 errors and ValidationException is not among them, so a malformed enum member comes from CommonErrors.html, as it does for #969's encryption algorithm
KeyUsage absent for any spec but SYMMETRIC_DEFAULT ValidationError/400, naming the spec and what it admits Published as a requirement — "this parameter is optional when you are creating a symmetric encryption KMS key; otherwise, it is required" — the code is substrate's reading, for the same reason
A well-formed pair the spec does not admit UnsupportedOperationException/400, naming both and what the spec admits Published for CreateKey, glossed "a specified parameter is not supported or a specified resource is not valid for this operation"

The two codes are load-bearing together rather than one code twice: the first says that is not a key spec, the second says that is a key spec, and not with that usage. A caller that misspelled a value and one that chose an impossible combination have different fixes, and the first is reported before the second is considered.

The default fires for exactly one spec, which AWS's HMAC guidance settles: "you must set the key usage even though GENERATE_VERIFY_MAC is the only valid key usage value for HMAC KMS keys." So KeyUsage is not defaulted to the single admissible value wherever there is one — only for SYMMETRIC_DEFAULT.

The pairing table is derived, not written

A spec admits a usage exactly when its algorithm list for that usage is non-empty, read from the four maps #974 already built for DescribeKey's algorithm members. kmsKeyMetadata's four-arm switch is replaced by a lookup in the same table the validator uses, so the renderer and the validator cannot disagree — a key cannot be accepted for a usage whose algorithm list it would then not carry.

Writing AWS's seven pairing bullets out a second time would have created the drift class this repo has recorded before: two tables that must agree, with nothing making them. Instead the test transcribes those bullets by hand — all 17 specs × 4 usages, 25 admissible pairs and 43 inadmissible — so the derivation and the page disagree unless both match.

InvalidKeyUsageException's first bullet

#969 implemented that code's second gloss bullet (algorithm incompatible with the key spec). This implements the first — "the KeyUsage value of the KMS key is incompatible with the API operation" — per AWS's requirement that "for encrypting, decrypting, re-encrypting, and generating data keys, the KeyUsage must be ENCRYPT_DECRYPT". Encrypt, Decrypt, GenerateDataKey, GenerateDataKeyWithoutPlaintext and both ends of ReEncrypt refuse a key whose usage is anything else.

Ordered before the key-state check, because a key usage is permanent where a state is not — telling a caller to enable a SIGN_VERIFY key sends it round a loop that cannot terminate — and before the encryption-algorithm check, so one condition yields one message: a SIGN_VERIFY RSA key addressed by Encrypt hears about its usage, not about SYMMETRIC_DEFAULT being inadmissible for RSA.

Consequence recorded rather than hidden: #974's empty-algorithm-list branch is now unreachable through substrate's own API. It is kept as the guard on a fifth algorithm member arriving without its pairing being thought through, and a test asserts that wording does not appear.

CloudFormation

AWS::KMS::Key documents KeySpec's default as SYMMETRIC_DEFAULT and KeyUsage's as ENCRYPT_DECRYPT, and deployKMSKey sends both unconditionally — so a template naming an asymmetric or HMAC KeySpec without a KeyUsage is now refused where it previously created a key AWS would not have. The defaults stay: they are the resource type's own, and that template is invalid at AWS too, where the property "is required for asymmetric KMS keys and HMAC KMS keys". A test pins all three cases.

Test rework

Every existing KMS test that created a non-symmetric key did so through the gap this closes, and 29 subtests failed once the validation was wired — each one a CreateKey AWS itself would refuse, so a rework list rather than a defect. createKMSKeySpec moved into the new file and now names an admissible usage; the metadata file's rows gained a keyUsage column; and TestKMSKeyMetadata_AnImpossiblePairReportsNoAlgorithmListAtAll was re-pointed at the refusal rather than deleted, since its five pairs are exactly the five CreateKey now rejects.

Verification

The nine-command gate passes. Coverage holds the baseline at 84.4% emulator / 83.9% total.

CreateKey accepted any string for either member and paired them however a
caller asked. Both members are immutable, so an unvalidated value is not
something a later call can correct — it is a key that will never behave as
its own metadata says.

Three refusals: a value outside its published enum, and an absent KeyUsage
for any spec but SYMMETRIC_DEFAULT, answer ValidationError/400; a
well-formed pair the spec does not admit answers
UnsupportedOperationException/400, which CreateKey publishes. The two codes
say different things — that is not a key spec, versus that is a key spec and
not with that usage — and the first is reported before the second.

The pairing table is derived rather than written: a spec admits a usage
exactly when its algorithm list for that usage is non-empty, read from the
four maps #974 built for DescribeKey. The metadata builder and the validator
now read them through one accessor, so a key cannot be accepted for a usage
whose algorithm list it would then not carry. The test transcribes AWS's
seven pairing bullets by hand across all 17 specs x 4 usages.

Also implements InvalidKeyUsageException's first gloss bullet, where #969
implemented the second: the five cryptographic operations refuse a key whose
usage is not ENCRYPT_DECRYPT, checked before the key state and before the
encryption algorithm.
@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
emulator/kms_key_specs.go 94.11% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 75890db into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the feat/kms-key-spec-usage-validation branch September 17, 2026 05:21
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.

kms: CreateKey validates neither KeySpec nor KeyUsage, and no cryptographic operation refuses a key whose KeyUsage is not ENCRYPT_DECRYPT

1 participant