Skip to content

fix(kms): data-key generation refuses a key that is not a symmetric encryption key (#988) - #1045

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/988-kms-data-key-spec
Sep 18, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/988-kms-data-key-spec

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

What

GenerateDataKey and GenerateDataKeyWithoutPlaintext require a symmetric encryption KMS key. Substrate checked KeyUsage and key state only, so an RSA_2048 key created with KeyUsage: ENCRYPT_DECRYPT — a pair AWS publishes and CreateKey must accept — passed both checks and was handed a wrapped data key at 200.

Both pages state the restriction four times over, twice in a description and twice on KeyId itself:

To generate a data key, specify the symmetric encryption KMS key that will be used to encrypt the data key. You cannot use an asymmetric KMS key to encrypt data keys.

…you cannot use an asymmetric KMS key or a key in a custom key store to generate a data key.

Specifies the symmetric encryption KMS key that encrypts the data key. You cannot specify an asymmetric KMS key or a KMS key in a custom key store.

The plural in "data keys" is load-bearing: it is why this is a condition on the two GenerateDataKey* operations and not on Encrypt, which AWS explicitly permits under an asymmetric key.

Scope

Only what the API model states (#671). The custom-key-store half of the restriction is unreachable — #984 refuses CustomKeyStoreId at CreateKey — and that is recorded rather than coded.

Request Substrate answers
SYMMETRIC_DEFAULT 200, unchanged
RSA_2048 / RSA_3072 / RSA_4096 / SM2 + ENCRYPT_DECRYPT InvalidKeyUsageException / 400, naming the spec
HMAC_*, signing and key-agreement specs InvalidKeyUsageException / 400 from the usage check, which runs first — unreachable here
a key in a custom key store unreachable since #984

The code is substrate's reading

InvalidKeyUsageException/400 on three grounds, argued in kmsInvalidKeySpecForDataKey: it is the only code either page publishes about a key being the wrong kind; the restriction is published four times so the refusal itself is not a reading; and the operation's wrap algorithm is fixed, so the code's second gloss bullet fits with the member elided. Neither of its two bullets describes this condition exactly, because the KeyUsage is the one thing such a key is right about — so the message leads with the spec, and names GenerateDataKeyPair, the operation AWS directs such a caller to, as unimplemented. Redirecting a caller to an unknown-action error would be worse than the truth.

Ordering

After kmsKeyUsageError, before kmsKeyStateError: a spec and a usage are permanent where a key state has a remedy, so a disabled RSA encryption key hears "has key spec" rather than DisabledException, which would imply that enabling it would help. Both sides are asserted through the message, since the usage neighbor shares this code and the state neighbor shares the 400.

One helper shared by both operations, per #961 — the issue that found GenerateDataKeyWithoutPlaintext refusing nothing its four siblings refused. kmsIsSymmetricEncryptionKey is reused, so this refusal and #978's two key-material omissions read one published condition in one place.

#978's omission becomes a guard

kmsReportsKeyMaterialID returned no KeyMaterialId at these two sites for an asymmetric key, as a workaround for substrate accepting a request AWS refuses. The request is now refused, so TestKMSKeyMaterialID_TheTwoOperationsThatReportNone narrows to …TheOneOperationThatReportsNone (Encrypt) and the asymmetric case moves to TestKMSDataKeySpec_AnAsymmetricKeyIsRefused, which asserts the refusal in place of the absent member. The guard is kept and documented as one.

Tests

New emulator/kms_data_key_spec_test.go, five assertions, every call over the wire (#765) and every refusal pairing the code with the status (#923): a symmetric key still wraps a data key at both operations with both published members intact; all four reachable specs × both operations are refused; the error body carries neither CiphertextBlob nor Plaintext, which is the observable form of the check running before the mint; both neighbors are ordered against; and the two operations' messages are compared to each other, which is what holds the shared helper in place.

Premise corrections

Posted on the issue before this PR (comment 5723154565): kmsKeyUsageError lives in kms_key_specs.go, not kms_errors.go; the pages publish nine errors, not ten; the quote is "…encrypt data keys".

Verification

Nine-command gate, all green: gofmt/build/vet, golangci-lint, make test, GOTOOLCHAIN=go1.26.6 make test, coverage, docs-reference/docs-reference-check/docs-versions/authz-reference-check, npm --prefix docs run build, and the e2e suite. Coverage holds at 85.1% emulator / 84.6% total.

Closes #988

…ncryption key (#988)

GenerateDataKey and GenerateDataKeyWithoutPlaintext checked KeyUsage and key
state only, never the key spec, so an RSA_2048 key created with KeyUsage
ENCRYPT_DECRYPT — a pair AWS publishes and CreateKey must accept — was handed a
wrapped data key and a 200. Both pages state the restriction four times over,
twice in a description and twice on the KeyId parameter itself: "you cannot use
an asymmetric KMS key to encrypt data keys", and "You cannot specify an
asymmetric KMS key or a KMS key in a custom key store".

kmsDataKeyKeySpecError reuses kmsIsSymmetricEncryptionKey, so the refusal and
the two key-material omissions read one published condition in one place, and
is shared by both operations so they cannot diverge — the #961 precedent. It
runs after the usage check and before the key state, because a spec is
permanent where a state has a remedy.

InvalidKeyUsageException/400 is substrate's reading of an unsplit bullet: it is
the only published code about a key being the wrong kind, and the KeyUsage is
the one thing such a key is right about. The message therefore leads with the
spec rather than the usage, and names GenerateDataKeyPair — the operation AWS
directs such a caller to — as unimplemented rather than redirecting a caller to
an unknown-action error.

#978's omission at these two sites becomes a guard: the request that reached it
is now refused, so kms_key_material_id_test.go asserts the refusal in place of
the absent member.

Closes #988
@codecov

codecov Bot commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit c833eff into main Sep 18, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the feat/988-kms-data-key-spec branch September 18, 2026 00:50
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.

fix(kms): GenerateDataKey and GenerateDataKeyWithoutPlaintext accept an asymmetric key

1 participant