Skip to content

kms: ReEncrypt decodes two of nine request members and answers three of seven response members, and an unenforced SourceKeyId hides IncorrectKeyException #969

Closed

Description

@scttfrdmn

ReEncrypt is the widest-shaped operation in the KMS plugin and substrate decodes two of its nine request members and answers three of its seven response members. Found while fixing #961, which needed the source key loaded and so had to read API_ReEncrypt end to end; #961 fixes SourceKeyId's value (it was the ciphertext blob) and adds the key-state guards, and deliberately leaves everything below.

What substrate decodes today

reEncrypt (emulator/kms_plugin.go) declares exactly two members:

var input struct {
	CiphertextBlob   string `json:"CiphertextBlob"`
	DestinationKeyId string `json:"DestinationKeyId"`
}

What API_ReEncrypt publishes

Nine request members. Verified verbatim from the reference:

Member Type Required Constraints Substrate
DestinationKeyId String Yes 1–2048 decoded, length unchecked
CiphertextBlob Blob No 1–6144 decoded, length unchecked
SourceKeyId String No 1–2048 ignored
SourceEncryptionAlgorithm String No SYMMETRIC_DEFAULT | RSAES_OAEP_SHA_1 | RSAES_OAEP_SHA_256 | SM2PKE ignored
DestinationEncryptionAlgorithm String No same four values ignored
SourceEncryptionContext String→string map No — ignored
DestinationEncryptionContext String→string map No — ignored
GrantTokens Array of strings No 0–10 items, each 1–8192 ignored
DryRun Boolean No — ignored
DryRunModifiers Array of strings No IGNORE_CIPHERTEXT ignored

Note CiphertextBlob is Required: No — "This parameter is required in all cases except when DryRun is true and DryRunModifiers is set to IGNORE_CIPHERTEXT." Substrate treats it as required in effect, since kmsDecryptStub fails on an empty string with InvalidCiphertextException. That happens to be right for every request that does not set DryRun.

Seven response members. Substrate answers CiphertextBlob, KeyId and SourceKeyId; the other four are absent:

  • SourceEncryptionAlgorithm — "The encryption algorithm that was used to decrypt the ciphertext before it was reencrypted."
  • DestinationEncryptionAlgorithm — "The encryption algorithm that was used to reencrypt the data."
  • SourceKeyMaterialId — "The identifier of the key material used to originally encrypt the data. This field is present only when the original encryption used a symmetric encryption KMS key." Fixed length 64, pattern ^[a-f0-9]+$.
  • DestinationKeyMaterialId — same gloss for the destination. Fixed length 64, same pattern.

The three questions, in the order they should be answered

1. SourceKeyId as a request member, and IncorrectKeyException. This is the one with a published refusal attached, and the only one of the three that adds an assertable outcome rather than a rendered string. API_ReEncrypt: "Enter a key ID of the KMS key that was used to encrypt the ciphertext. If you identify a different KMS key, the ReEncrypt operation throws an IncorrectKeyException." The error's own gloss: "The request was rejected because the specified KMS key cannot decrypt the data. The KeyId in a Decrypt request and the SourceKeyId in a ReEncrypt request must identify the same KMS key that was used to encrypt the ciphertext." HTTP 400.

Substrate can answer this exactly: the ciphertext stub already carries the key ID that encrypted it, and #961 now resolves and loads that key. So the check is a resolve of SourceKeyId through resolveKeyTarget and a comparison against the key ID the ciphertext names.

Decrypt has the identical gap and the identical fix, and should be done in the same change: its optional KeyId is "a constraint on which key may be used" — substrate's own comment in decrypt says exactly that and then does not enforce it. API_Decrypt publishes IncorrectKeyException for the same reason.

2. The two algorithm members. Both request members are ignored and both response members are absent. The absence is currently the honest answer and should not be changed casually: substrate models no cryptography, CreateKey accepts a KeySpec, and answering SYMMETRIC_DEFAULT unconditionally would report a value derived from nothing — the defect class v0.115.0 and v0.116.0 were both about. Two defensible outcomes:

  • Echo the requested algorithm, defaulting to SYMMETRIC_DEFAULT, and refuse a value outside the four published ones with ValidationError/400 — the value is then traceable to the request.
  • Derive it from the key's KeySpec and refuse a mismatch with InvalidKeyUsageException/400, which API_ReEncrypt publishes: "The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (KeySpec)".

The second is more faithful and needs a KeySpec → permitted-algorithms table that DescribeKey's unmodelled EncryptionAlgorithms member would also want. Prefer it, and note it makes this larger than it looks.

3. The two key-material IDs. Substrate models no key material at all — KMSKey has no equivalent of KeyMetadata's CurrentKeyMaterialId, which is the same 64-hex shape. Rendering these means minting a per-key material ID at CreateKey, deterministically, and reporting it from DescribeKey as well; otherwise the two operations would disagree, which is the invariant #848 exists to protect. That is the largest of the three and should probably be its own issue if taken.

Not in scope

DryRun/DryRunModifiers and GrantTokens. Grants are unmodelled across the whole plugin, and a dry run is an authorization-only path that would need substrate's authorizer to run without the operation — a design question well beyond this.

Acceptance criteria

  • SourceKeyId is decoded, resolved through resolveKeyTarget, and compared against the key the ciphertext names; a mismatch answers IncorrectKeyException/400 with the published gloss's reasoning in the message.
  • Decrypt's optional KeyId is enforced the same way, and its existing comment saying it is "a constraint" stops being aspirational.
  • A decision on the two algorithm members is recorded either way — echoed, derived, or deliberately absent with the reason.
  • The two key-material IDs are decided, and if taken, minted once per key and reported consistently by DescribeKey.
  • Every ignored member that stays ignored is named in a comment, so the next reader does not have to re-derive this table.
  • 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).

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