Skip to content

KMS Encrypt answers InvalidCiphertextException, a code API_Encrypt does not publish, for a malformed Plaintext #991

Closed

Description

@scttfrdmn

KMSPlugin.encrypt answers a code API_Encrypt does not publish when Plaintext is not valid base64.

emulator/kms_plugin.go, in encrypt:

plaintext, err := base64.StdEncoding.DecodeString(input.Plaintext)
if err != nil {
    return nil, &AWSError{Code: "InvalidCiphertextException", Message: "invalid base64 plaintext", HTTPStatus: http.StatusBadRequest}
}

API_Encrypt publishes nine errors and InvalidCiphertextException is not among them. Verified against the page: DependencyTimeoutException/500, DisabledException/400, DryRunOperationException/400, InvalidGrantTokenException/400, InvalidKeyUsageException/400, KeyUnavailableException/500, KMSInternalException/500, KMSInvalidStateException/400, NotFoundException/400.

InvalidCiphertextException's own gloss says why it cannot be right here — it is scoped to two other operations and to a value Encrypt does not take:

From the Decrypt or ReEncrypt operation, the request was rejected because the specified ciphertext, or additional authenticated data incorporated into the ciphertext, such as the encryption context, is corrupted, missing, or otherwise invalid.

Encrypt receives no ciphertext. A caller matching on the code learns that its ciphertext was bad when what it sent was a malformed plaintext, which is the opposite end of the operation.

What it should answer

ValidationError/400, from CommonErrors.html — "the input doesn't meet the required format or constraints". That is the same reading kmsUnknownEncryptionAlgorithm records for an enum value outside the published set and kmsInvalidBody records for an unparseable body: no operation page gives a code for a malformed member, so CommonErrors.html is where it has to land. A helper beside those two, following kms_errors.go's one-per-condition convention.

Two adjacent gaps worth deciding in the same pass

Neither is this issue's subject, but both are about the same member and a fix that ignores them leaves the member half-validated:

  • Plaintext's published length range, 1–4096, is unenforced. Required: Yes with a Length Constraints line, and substrate accepts an absent member (decoding to "") as well as an arbitrarily large one.
  • The smaller per-spec maxima are unenforced. AWS publishes a maximum plaintext size per key spec — 4096 for SYMMETRIC_DEFAULT, and materially smaller figures for the RSA specs, which depend on the OAEP hash. An Encrypt of 4000 bytes under RSA_2048 succeeds here and cannot succeed at AWS.

Both need their codes verified against the page before implementing, and the second needs the per-spec table transcribed from the developer guide rather than recalled.

Acceptance criteria

  • A Plaintext that is not valid base64 answers ValidationError/400 rather than InvalidCiphertextException.
  • The refusal is built by a helper in kms_errors.go whose doc comment records that API_Encrypt publishes nine errors and that this is not one of them, with the CommonErrors.html citation for the replacement.
  • A test over the wire asserts the status alongside the code (fix(kms): NotFoundException is HTTP 400, and InvalidRequest is not a KMS code #923), and asserts that Encrypt answers no InvalidCiphertextException for any input — the code belongs to Decrypt and ReEncrypt alone.
  • The two adjacent gaps above are either fixed with their codes verified against the page, or recorded in docs/services.md as unenforced with the published figures named.
  • docs/services.md states the corrected code.

Surfaced while implementing #979, which touched this handler and deliberately left the code alone rather than folding an unrelated correction into a context change.

Activity

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

    @scttfrdmn
    OwnerAuthor

    Premise corrections before the PR opens, verified at 2c0dbaf (the #988 branch, which shifts every line number in kms_plugin.go cited below; the issue's own numbers were taken before #984 and #988).

    1. The per-spec maximum table is published on API_Encrypt itself. The issue sends a reader to the developer guide for it. No trip is needed — the page's own Plaintext parameter section carries the whole table:

    Key spec Maximum plaintext
    SYMMETRIC_DEFAULT 4096 bytes
    RSA_2048 214 bytes (RSAES_OAEP_SHA_1) / 190 bytes (RSAES_OAEP_SHA_256)
    RSA_3072 342 / 318
    RSA_4096 470 / 446
    SM2PKE 1024 bytes

    That matters beyond provenance: the maximum depends on the encryption algorithm as well as the spec for the three RSA sizes, so a check written per-spec alone would be wrong on four of the five rows. The member's own constraints are "Minimum length of 1. Maximum length of 4096".

    2. InvalidCiphertextException is not among API_Encrypt's published errors. The page publishes nine, all of them at 400 except the three 500s (DependencyTimeout, KeyUnavailable, KMSInternal): Disabled, DryRunOperation, InvalidGrantToken, InvalidKeyUsage, KMSInvalidState, NotFound. The code appears on the page only in prose about a later Decrypt failing on a mismatched encryption context — which is the opposite direction from an undecodable Plaintext on this request. ValidationError/400 is the landing place, matching kmsInvalidBody and kmsUnknownEncryptionAlgorithm.

    3. The guard is in the wrong position, which the issue does not mention. At kms_plugin.go:1369-1372 the base64 decode runs after resolveKeyTarget (:1342), the key load, the usage check, the state check and the algorithm-compatibility check. The 1–4096 range and base64 validity are key-independent, so they belong before :1342, following the rule kmsResolveEncryptionAlgorithm's own placement records at :1334-1335: a malformed member is not a statement about any key. Only the per-spec maximum needs the key and stays after :1351 — and, per correction 1, after the algorithm is resolved too.

    4. Two other sites use the code correctly and must be left alone. :1443 (Decrypt) and :1761 (ReEncrypt's source end) answer InvalidCiphertextException for an undecodable ciphertext, which is exactly what the code is published for on both of those pages.

    The PR is #3 of the KMS sequence, after #984 (merged) and #988 (open as #1045).

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