Skip to content

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

Closed

Description

@scttfrdmn

CreateKey accepts any string as KeySpec and any string as KeyUsage, and no cryptographic operation checks either one against the operation it is being used for. Found while fixing #969, which needed the KeySpec → encryption-algorithm table and so had to establish what a key spec can legally be.

What substrate does today

createKey (emulator/kms_plugin.go) decodes both members, defaults them, and stores whatever it was handed:

if input.KeyUsage == "" {
	input.KeyUsage = "ENCRYPT_DECRYPT"
}
if input.KeySpec == "" {
	input.KeySpec = "SYMMETRIC_DEFAULT"
}

So CreateKey with KeySpec: "MADEUP" or KeyUsage: "SIGN_VERIFY" both answer 200, and DescribeKey reports the value back.

What AWS publishes

API_CreateKey gives both members a Valid Values line:

  • KeyUsage: SIGN_VERIFY | ENCRYPT_DECRYPT | GENERATE_VERIFY_MAC | KEY_AGREEMENT
  • KeySpec: SYMMETRIC_DEFAULT | RSA_2048 | RSA_3072 | RSA_4096 | ECC_NIST_P256 | ECC_NIST_P384 | ECC_NIST_P521 | ECC_SECG_P256K1 | ECC_NIST_EDWARDS25519 | ML_DSA_44 | ML_DSA_65 | ML_DSA_87 | SM2 | HMAC_224 | HMAC_256 | HMAC_384 | HMAC_512 — verify this list verbatim against the page before implementing, since it grows as AWS adds key types.

The pairing is constrained too, and the constraint is what makes the enum check worth more than a spelling test. The developer guide's key spec reference states which usages each spec admits: an RSA spec is "encryption and decryption -or- signing and verification"; the ECC specs are "signing and verification -or- deriving shared secrets"; ECC_NIST_EDWARDS25519 is "signing and verification only"; the HMAC specs are MAC generation only; the ML-DSA specs are signing only; SM2 admits all three. API_CreateKey publishes UnsupportedOperationException/400, whose gloss — "a specified parameter is not supported or a specified resource is not valid for this operation" — is the natural home for an inadmissible pairing.

The second half: no cryptographic operation checks KeyUsage

InvalidKeyUsageException/400 is published on API_Encrypt, API_Decrypt, API_ReEncrypt, API_GenerateDataKey and API_GenerateDataKeyWithoutPlaintext, and its gloss has two bullets:

  • The KeyUsage value of the KMS key is incompatible with the API operation.
  • The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (KeySpec).

For encrypting, decrypting, re-encrypting, and generating data keys, the KeyUsage must be ENCRYPT_DECRYPT.

#969 implements the second bullet and deliberately leaves the first. kmsResolveEncryptionAlgorithm (emulator/kms_encryption_algorithms.go) refuses an algorithm the key's KeySpec does not admit, which covers every key spec that supports no encryption algorithm at all — the ECC, HMAC and ML-DSA specs. It does not cover a SIGN_VERIFY RSA key: RSA_2048 admits RSAES_OAEP_SHA_1 and RSAES_OAEP_SHA_256 whatever the key's usage is, so an Encrypt against a signing key still succeeds where AWS answers InvalidKeyUsageException.

The check is one comparison against "ENCRYPT_DECRYPT" in a shared helper beside kmsKeyStateError, at the same five call sites. It is filed separately rather than folded into #969 because it is a different published bullet with a different provenance, and because the two together would make one PR that changed what five operations accept for two unrelated reasons.

Acceptance criteria

  • CreateKey refuses a KeySpec or KeyUsage outside the published Valid Values, with the code chosen from what API_CreateKey publishes and the choice recorded if it is substrate's reading.
  • An inadmissible spec/usage pairing is decided — refused with UnsupportedOperationException or deliberately accepted — and recorded either way.
  • The five cryptographic operations refuse a key whose KeyUsage is not ENCRYPT_DECRYPT, with InvalidKeyUsageException/400, through one shared helper rather than five call-site tests.
  • The published Valid Values are re-verified against API_CreateKey at implementation time rather than taken from this issue.
  • 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

  1. added this to the v0.118.0 milestone on Sep 17, 2026
  2. added
    type: bug
    on Sep 17, 2026
  3. scttfrdmn commented on Sep 17, 2026

    @scttfrdmn
    OwnerAuthor

    API_CreateKey re-verified at implementation time, per this issue's fourth criterion. Three things the issue asked to be checked, and one it did not anticipate.

    The Valid Values, verbatim

    KeySpec — 17, and the issue's list is complete and correctly ordered against the page:

    RSA_2048 | RSA_3072 | RSA_4096 | ECC_NIST_P256 | ECC_NIST_P384 | ECC_NIST_P521 | ECC_SECG_P256K1 | SYMMETRIC_DEFAULT | HMAC_224 | HMAC_256 | HMAC_384 | HMAC_512 | SM2 | ML_DSA_44 | ML_DSA_65 | ML_DSA_87 | ECC_NIST_EDWARDS25519

    KeyUsage — 4: SIGN_VERIFY | ENCRYPT_DECRYPT | GENERATE_VERIFY_MAC | KEY_AGREEMENT.

    The pairing rules are on API_CreateKey itself, not only in the developer guide, as seven bullets under KeyUsage: symmetric encryption → omit or ENCRYPT_DECRYPT; HMAC → GENERATE_VERIFY_MAC; RSA → ENCRYPT_DECRYPT or SIGN_VERIFY; NIST-standard elliptic curve → SIGN_VERIFY or KEY_AGREEMENT; ECC_SECG_P256K1 → SIGN_VERIFY; ML-DSA → SIGN_VERIFY; SM2 (China Regions only) → ENCRYPT_DECRYPT, SIGN_VERIFY or KEY_AGREEMENT. ECC_NIST_EDWARDS25519 is annotated "signing and verification only" in the KeySpec bullet list, so it is SIGN_VERIFY alone — it is not one of the "NIST-standard elliptic curve" specs for the key-agreement bullet's purposes, which matches #974's finding that it publishes no key-agreement algorithm.

    The error code, which the issue left open and the page does not settle

    API_CreateKey publishes thirteen errors and ValidationException is not among them: CloudHsmClusterInvalidConfigurationException/400, CustomKeyStoreInvalidStateException/400, CustomKeyStoreNotFoundException/400, DependencyTimeoutException/500, InvalidArnException/400, KMSInternalException/500, LimitExceededException/400, MalformedPolicyDocumentException/400, TagException/400, UnsupportedOperationException/400, XksKeyAlreadyInUseException/400, XksKeyInvalidConfigurationException/400, XksKeyNotFoundException/400.

    So there are two distinct refusals here and they get two different codes:

    • A value outside the published enum — KeySpec: "MADEUP" — is a constraint violation on the input, and the code is ValidationError/400 from CommonErrors.html ("The input fails to satisfy the constraints specified by an AWS service"), following the precedent kms: ReEncrypt decodes two of nine request members and answers three of seven response members, and an unenforced SourceKeyId hides IncorrectKeyException #969 set for the same class. Substrate's reading, since API_CreateKey publishes nothing narrower.
    • A well-formed but inadmissible pairing — KeySpec: "HMAC_256" with KeyUsage: "KEY_AGREEMENT" — is UnsupportedOperationException/400, as this issue proposed. Its gloss is "a specified parameter is not supported or a specified resource is not valid for this operation", and it is the only published 400 on the operation that fits. Also substrate's reading: AWS states the pairing rules and publishes no code against them.

    Keeping them separate matters, because they are distinguishable to a caller: one says that is not a key spec, the other says that is a key spec, and not with that usage.

    One requirement the issue does not mention: KeyUsage is not optional for most specs

    KeyUsage's own description, verbatim: "This parameter is optional when you are creating a symmetric encryption KMS key; otherwise, it is required." Substrate defaults it to ENCRYPT_DECRYPT unconditionally, so today an HMAC or ML-DSA key can be created with no KeyUsage at all and comes back as an encryption key — a second silent substitution alongside the enum gap.

    The rule that falls out is narrow and worth stating precisely: the default fires only when KeySpec is SYMMETRIC_DEFAULT, whether that value was sent or itself defaulted. An HMAC spec is symmetric but is not a symmetric encryption key, and its own bullet requires GENERATE_VERIFY_MAC explicitly. A missing required parameter is a constraint violation on the input, so it takes ValidationError/400 with the enum case rather than UnsupportedOperationException.

    What this closes for, and what it does not

    Also filed from the same verification pass, because none of the three is in scope here:

    #985 shares this issue's validation surface and must not end up with a different code for the same class of malformed input. It is either folded in here or lands immediately after reusing whatever this settles; the decision is recorded on #985.

    Finally, the test rework this forces, which is larger than it looks: kms_key_metadata_test.go (#974), kms_rotation_key_type_test.go (#972) and kms_encryption_algorithm_test.go (#969) all create keys through the gap this issue closes, and all three carry a note saying so. kms_key_metadata_test.go's _AnImpossiblePairReportsNoAlgorithmListAtAll is the sharpest case — it exists to pin what substrate answers for a pairing AWS would never accept, including KeyUsage: "TRANSMOGRIFY", and every one of its sub-cases becomes unreachable through CreateKey. That test does not get deleted; it gets re-pointed at the refusal, and #974's builder keeps its empty-list branch as the defence-in-depth it now is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions