Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(kms): add GenerateDataKeyPair and GenerateDataKeyPairWithoutPlaintext (#1107)
yleced3548b5cb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9226ddf0..a7d97532 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,12 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]### Added+- **IoT — device shadow operations** — `GetThingShadow`, `UpdateThingShadow`, and `DeleteThingShadow` on the `iot-data` endpoint (classic and named shadows via `?name=`) previously returned `InvalidRequestException: Unsupported iot-data path: .../shadow`. Shadows now store `desired`/`reported` state: updates deep-merge into the stored document (a `null` value removes the field), bump the version, and stamp per-field `metadata` timestamps; the `/accepted` response echoes only the sections sent, while `GetThingShadow` returns the full document including the computed `state.delta` (desired fields that differ from reported). A stale `version` in an update is rejected with a `409`, and reading or deleting a nonexistent shadow returns `ResourceNotFoundException`. Shadows are account-scoped and persisted with the rest of the IoT state.+- **KMS — `GenerateDataKeyPair` and `GenerateDataKeyPairWithoutPlaintext`** — the emulator implemented `GenerateDataKey` and `GenerateDataKeyWithoutPlaintext` but no data key *pair* operation, so both returned `InvalidAction: Unknown action`. These provision an asymmetric data key pair wrapped under a symmetric CMK: `GenerateDataKeyPair` returns `PrivateKeyPlaintext`, `PrivateKeyCiphertextBlob` and `PublicKey`, while the `WithoutPlaintext` variant omits `PrivateKeyPlaintext` so the private key never reaches the caller in the clear — the caller persists the ciphertext plus public key and nothing else. Supported `KeyPairSpec` values follow the real AWS enum — `RSA_2048/3072/4096`, `ECC_NIST_P256/P384/P521`, `ECC_SECG_P256K1`, `ECC_NIST_EDWARDS25519` (`SM2` is excluded, as `CreateKey` does not implement it either). Note this is a superset of what `CreateKey` accepts today: `CreateKey` still lacks `RSA_3072`, a pre-existing gap these operations do not share. The wrapping CMK must be symmetric, `EncryptionContext` binds the wrapped private key as it does for `Encrypt`, and the blob decrypts back to a PKCS#8 DER private key that pairs with the returned public key. The wrapped private key uses the emulator's ciphertext layout, now extended with a per-blob random nonce (`key_id(36) + context_hash(32) + nonce(16) + data`) that is mixed into key derivation; `Decrypt` reads these blobs exactly as it reads `Encrypt`'s and `GenerateDataKey`'s. The nonce is required for correctness of the `WithoutPlaintext` guarantee: without it the XOR keystream repeats across every blob sharing a CMK and `EncryptionContext`, and because a key pair's plaintext embeds its (public) modulus, the wrapped private key could be recovered from the public outputs alone. `Encrypt` and `GenerateDataKey` adopt the same nonced layout so all three wrap through an identical format.++### Fixed+- **Lambda — Node warm-worker invoke no longer fails on fd 1 writes** — `fs.writeSync` / `fs.write` on stdout bypassed the existing `process.stdout.write` redirect to stderr (#373), so sync loggers (e.g. pino with `{ sync: true }`) could emit on the JSON-line protocol channel and surface false `Runtime.HandlerError` / `Unknown error` responses even when the handler succeeded. The Node worker bootstrap now redirects fd 1 through `fs.writeSync` / `fs.write`, and the warm-worker stdout reader accepts only protocol lines whose `status` is `ok` or `error`. Reported by @kofuk (#1093).+- **Cognito — `ListUsers` `status` filter matched against the wrong field** — `Filter='status = "Enabled"'`/`"Disabled"` was compared against `UserStatus` (the confirmation-state enum: `CONFIRMED`, `FORCE_CHANGE_PASSWORD`, `UNCONFIRMED`, etc.), which never equals the literal string `"Enabled"`/`"Disabled"`. As a result, filtering by `status` always returned an empty list, regardless of how many users existed or their actual enabled/disabled state. `status` now correctly reflects the account's `Enabled` boolean toggled by `AdminEnableUser`/`AdminDisableUser`, matching real AWS Cognito's `ListUsers` Filter semantics. Contributed by @jey-mfv.- **Batch — `UpdateComputeEnvironment`** — Terraform `aws_batch_compute_environment` with an `update_policy` block calls `UpdateComputeEnvironment`, which previously failed with `InvalidAction: Unsupported batch path: /v1/updatecomputeenvironment`. The endpoint now resolves compute environments by name or ARN, applies present fields (`state`, `serviceRole`, shallow-merged `computeResources`, `updatePolicy`, `unmanagedvCpus`, `context`), and returns them from `DescribeComputeEnvironments`. Reported by @smoores-dev (#1088).## [1.4.3] — 2026-07-18diff --git a/README.md b/README.mdindex da081056..b3173fd1 100644--- a/README.md+++ b/README.md@@ -432,7 +432,7 @@ subnet = ec2.create_subnet(| **API Gateway v2** | CreateApi, GetApi, GetApis, UpdateApi, DeleteApi, CreateRoute, GetRoute, GetRoutes, UpdateRoute, DeleteRoute, CreateIntegration, GetIntegration, GetIntegrations, UpdateIntegration, DeleteIntegration, CreateRouteResponse, GetRouteResponse, GetRouteResponses, UpdateRouteResponse, DeleteRouteResponse, CreateIntegrationResponse, GetIntegrationResponse, GetIntegrationResponses, UpdateIntegrationResponse, DeleteIntegrationResponse, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateDeployment, GetDeployment, GetDeployments, DeleteDeployment, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, TagResource, UntagResource, GetTags, PostToConnection, GetConnection, DeleteConnection | **HTTP API** and **WebSocket API** (`protocolType=WEBSOCKET`); Lambda proxy (`AWS_PROXY`), HTTP proxy (`HTTP_PROXY`), and MOCK integrations; HTTP data plane via `{apiId}.execute-api.localhost` Host header or path-based `/_aws/execute-api/{apiId}/{stage}/{path}` (no DNS/Host override needed — works from browsers on macOS and strict clients); `$default` stage served from the URL root (no stage segment in the path); per-API `corsConfiguration` applied to preflights + dispatched responses; request parameter mapping for HTTP_PROXY (`append/overwrite/remove` for headers/querystring plus `overwrite:path`) with context variables including `$context.authorizer.jwt.claims`; JWT data-plane authorization for HTTP routes (issuer/audience/time/scope checks) and claim propagation to integrations; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; WebSocket data plane on the same two URL forms, with `$connect` / `$disconnect` / `$default` / custom-action routing, `$request.body.*` RouteSelectionExpression, `@connections` management API (PostToConnection / GetConnection / DeleteConnection), per-connection outbox for server-side push; `{param}` / `{proxy+}` matching; JWT/Lambda authorizer CRUD; pin `apiId` across runs with the `ms-custom-id` tag || **API Gateway v1** | CreateRestApi, GetRestApi, GetRestApis, UpdateRestApi, DeleteRestApi, CreateResource, GetResource, GetResources, UpdateResource, DeleteResource, PutMethod, GetMethod, DeleteMethod, UpdateMethod, PutMethodResponse, GetMethodResponse, DeleteMethodResponse, PutIntegration, GetIntegration, DeleteIntegration, UpdateIntegration, PutIntegrationResponse, GetIntegrationResponse, DeleteIntegrationResponse, CreateDeployment, GetDeployment, GetDeployments, UpdateDeployment, DeleteDeployment, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateModel, GetModel, GetModels, DeleteModel, CreateApiKey, GetApiKey, GetApiKeys, UpdateApiKey, DeleteApiKey, CreateUsagePlan, GetUsagePlan, GetUsagePlans, UpdateUsagePlan, DeleteUsagePlan, CreateUsagePlanKey, GetUsagePlanKeys, DeleteUsagePlanKey, CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName, CreateBasePathMapping, GetBasePathMapping, GetBasePathMappings, DeleteBasePathMapping, TagResource, UntagResource, GetTags | REST API (v1) protocol; Lambda proxy format 1.0 (AWS_PROXY), HTTP proxy (HTTP_PROXY), MOCK integration; data plane via `{apiId}.execute-api.localhost` Host header, path-based `/_aws/execute-api/{apiId}/{stage}/{path}`, or legacy `/restapis/{apiId}/{stage}/_user_request_/{path}`; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; resource tree with `{param}` and `{proxy+}` path matching; JSON Patch for all PATCH operations; state persistence; pin `id` across runs with the `ms-custom-id` tag || **ELBv2 / ALB** | CreateLoadBalancer, DescribeLoadBalancers, DeleteLoadBalancer, DescribeLoadBalancerAttributes, ModifyLoadBalancerAttributes, CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup, DeleteTargetGroup, DescribeTargetGroupAttributes, ModifyTargetGroupAttributes, CreateListener, DescribeListeners, ModifyListener, DeleteListener, CreateRule, DescribeRules, ModifyRule, DeleteRule, SetRulePriorities, SetSubnets, SetIpAddressType, SetSecurityGroups, RegisterTargets, DeregisterTargets, DescribeTargetHealth, AddTags, RemoveTags, DescribeTags | Control plane + data plane; ALB→Lambda live traffic routing; `path-pattern`, `host-header`, `http-method`, `query-string`, `http-header` rule conditions; `forward`, `redirect`, `fixed-response` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix |-| **KMS** | CreateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateDataKey, GenerateDataKeyWithoutPlaintext, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags | 27 actions; RSA (2048/4096), ECC (SECG_P256K1, NIST P-256/384/521), Edwards25519 (`ECC_NIST_EDWARDS25519` — pure Ed25519 via `ED25519_SHA_512` with `MessageType=RAW`; `ED25519_PH_SHA_512` advertised in `SigningAlgorithms` but returns `UnsupportedOperationException` until Ed25519ph lands), and symmetric keys; PKCS1v15, PSS, and ECDSA signing; envelope encryption; alias resolution; key rotation; key policies; tags; enable/disable/schedule deletion; full Terraform `aws_kms_key` compatible; `cryptography` package included in Docker image |+| **KMS** | CreateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateDataKey, GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair, GenerateDataKeyPairWithoutPlaintext, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags | 29 actions; CMK specs: RSA (2048/4096), ECC (SECG_P256K1, NIST P-256/384/521), Edwards25519 (`ECC_NIST_EDWARDS25519` — pure Ed25519 via `ED25519_SHA_512` with `MessageType=RAW`; `ED25519_PH_SHA_512` advertised in `SigningAlgorithms` but returns `UnsupportedOperationException` until Ed25519ph lands), and symmetric keys; PKCS1v15, PSS, and ECDSA signing; envelope encryption, incl. asymmetric data key pairs (`KeyPairSpec` adds RSA_3072); alias resolution; key rotation; key policies; tags; enable/disable/schedule deletion; full Terraform `aws_kms_key` compatible; `cryptography` package included in Docker image || **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`, no viewer-request JS execution at the edge; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again || **CloudFront KeyValueStore (data plane)** | DescribeKeyValueStore, ListKeys, GetKey, PutKey, DeleteKey, UpdateKeys | Separate `cloudfront-keyvaluestore` SDK service (REST/JSON, signing name `cloudfront-keyvaluestore`); ETag-based optimistic concurrency on every mutating op; `UpdateKeys` is atomic (validates whole batch, rejects on first invalid entry); `ListKeys` paginates with opaque `NextToken` capped at 50 results per AWS spec; bridges to the management plane via the shared KVS ARN || **CloudTrail** | LookupEvents, CreateTrail, DeleteTrail, GetTrail, DescribeTrails, ListTrails, UpdateTrail, GetTrailStatus, StartLogging, StopLogging, PutEventSelectors, GetEventSelectors, AddTags, ListTags, RemoveTags | In-memory audit log; recording opt-in via `CLOUDTRAIL_RECORDING=1` (or runtime config endpoint); per-account ring buffer (`CLOUDTRAIL_MAX_EVENTS=10000`); `LookupEvents` supports all 8 AWS `LookupAttributes`; `IsLogging` flips on Start/StopLogging; `CreateTrail`/`UpdateTrail` persist `KmsKeyId` (normalized to a key ARN; omitted when unset) so `aws_cloudtrail` converges without drift |diff --git a/ministack/services/kms.py b/ministack/services/kms.pyindex 8522687c..d67479d0 100644--- a/ministack/services/kms.py+++ b/ministack/services/kms.py@@ -3,7 +3,8 @@ KMS (Key Management Service) Emulator.JSON-based API via X-Amz-Target (prefix: TrentService).Supports: CreateKey, ListKeys, DescribeKey, Sign, Verify,Encrypt, Decrypt, GenerateDataKey,- GenerateDataKeyWithoutPlaintext.+ GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair,+ GenerateDataKeyPairWithoutPlaintext."""import base64@@ -688,13 +689,15 @@ def _encrypt(data):# ciphertext is: key_id_bytes(36) + context_hash(32) + xor_encrypted_data.# EncryptionContext is mixed into key derivation so decrypt# must supply the same context or get different plaintext.- key_bytes = _derive_with_context(rec["_symmetric_key"], enc_context)+ nonce = os.urandom(16)+ key_bytes = _derive_with_context(rec["_symmetric_key"], enc_context, nonce)pad_stream = _expand_key(key_bytes, len(plaintext))encrypted = bytes(a ^ b for a, b in zip(plaintext, pad_stream))ctx_hash = hashlib.sha256(json.dumps(enc_context, sort_keys=True).encode()).digest()- ciphertext = rec["KeyId"].encode() + ctx_hash + encrypted+ # Layout: key_id(36) + ctx_hash(32) + nonce(16) + xor_encrypted_data.+ ciphertext = rec["KeyId"].encode() + ctx_hash + nonce + encryptedelif "_private_key" in rec and rec["KeyUsage"] == "ENCRYPT_DECRYPT":if enc_context:return error_response_json(@@ -777,8 +780,10 @@ def _decrypt(data):"EncryptionContext does not match",400,)- encrypted_data = ciphertext[68:]- key_bytes = _derive_with_context(rec["_symmetric_key"], enc_context)+ # Layout: key_id(36) + ctx_hash(32) + nonce(16) + xor_encrypted_data.+ nonce = ciphertext[68:84]+ encrypted_data = ciphertext[84:]+ key_bytes = _derive_with_context(rec["_symmetric_key"], enc_context, nonce)pad_stream = _expand_key(key_bytes, len(encrypted_data))plaintext = bytes(a ^ b for a, b in zip(encrypted_data, pad_stream))elif "_private_key" in rec:@@ -855,13 +860,15 @@ def _generate_data_key_common(data):data_key = os.urandom(32)enc_context = data.get("EncryptionContext", {})- cmk_bytes = _derive_with_context(rec["_symmetric_key"], enc_context)+ nonce = os.urandom(16)+ cmk_bytes = _derive_with_context(rec["_symmetric_key"], enc_context, nonce)pad_stream = _expand_key(cmk_bytes, len(data_key))encrypted = bytes(a ^ b for a, b in zip(data_key, pad_stream))ctx_hash = hashlib.sha256(json.dumps(enc_context, sort_keys=True).encode()).digest()- ciphertext = rec["KeyId"].encode() + ctx_hash + encrypted+ # Layout: key_id(36) + ctx_hash(32) + nonce(16) + xor_encrypted_data.+ ciphertext = rec["KeyId"].encode() + ctx_hash + nonce + encryptedreturn rec, data_key, ciphertext@@ -878,6 +885,128 @@ def _generate_data_key(data):})+def _generate_data_key_pair_common(data, action):+ """Shared logic for GenerateDataKeyPair and GenerateDataKeyPairWithoutPlaintext.++ Generates a data key pair and wraps the private key under the CMK. `action`+ is the caller's operation name, so errors name the operation the client+ actually invoked rather than whichever variant this helper was written for.++ Returns (payload, private_key_der, None) on success, or (None, None, error)+ on failure. The two operations differ in exactly one thing: whether+ PrivateKeyPlaintext gets added to the payload -- so the caller decides that+ and nothing else.++ KeyPairSpec follows the real AWS enum, minus SM2 (which _create_key does not+ implement either). Note this is a superset of _create_key's asymmetric specs:+ _create_key still lacks RSA_3072, a pre-existing gap not repeated here.+ """+ key_id = data.get("KeyId", "")+ rec = _resolve_key(key_id)+ if not rec:+ return None, None, error_response_json(+ "NotFoundException", f"Key {key_id} not found", 400+ )+ err = _check_key_state(rec)+ if err:+ return None, None, err+ # The CMK must be symmetric: it wraps the generated private key.+ if "_symmetric_key" not in rec:+ return None, None, error_response_json(+ "UnsupportedOperationException",+ f"{action} requires a symmetric key",+ 400,+ )+ err = _require_crypto(action)+ if err:+ return None, None, err++ spec = data.get("KeyPairSpec", "")+ if spec in ("RSA_2048", "RSA_3072", "RSA_4096"):+ private_key = rsa.generate_private_key(+ public_exponent=65537, key_size=int(spec.split("_")[1])+ )+ elif spec in ("ECC_NIST_P256", "ECC_NIST_P384", "ECC_NIST_P521", "ECC_SECG_P256K1"):+ curve_map = {+ "ECC_NIST_P256": ec.SECP256R1(),+ "ECC_NIST_P384": ec.SECP384R1(),+ "ECC_NIST_P521": ec.SECP521R1(),+ "ECC_SECG_P256K1": ec.SECP256K1(),+ }+ private_key = ec.generate_private_key(curve_map[spec])+ elif spec == "ECC_NIST_EDWARDS25519":+ private_key = ed25519.Ed25519PrivateKey.generate()+ else:+ return None, None, error_response_json(+ "ValidationException",+ f"1 validation error detected: Value '{spec}' at 'keyPairSpec' "+ "failed to satisfy constraint: Member must satisfy enum value set: "+ "[RSA_2048, RSA_3072, RSA_4096, ECC_NIST_P256, ECC_NIST_P384, "+ "ECC_NIST_P521, ECC_SECG_P256K1, ECC_NIST_EDWARDS25519]",+ 400,+ )++ private_der = private_key.private_bytes(+ serialization.Encoding.DER,+ serialization.PrivateFormat.PKCS8,+ serialization.NoEncryption(),+ )+ public_der = private_key.public_key().public_bytes(+ serialization.Encoding.DER,+ serialization.PublicFormat.SubjectPublicKeyInfo,+ )++ enc_context = data.get("EncryptionContext", {})+ # Ciphertext layout: key_id(36) + ctx_hash(32) + nonce(16) + xor_encrypted_data.+ # The per-blob nonce is what stops the wrapped private key from leaking:+ # without it the keystream repeats across every blob sharing this CMK and+ # EncryptionContext. Must stay in step with _encrypt,+ # _generate_data_key_common and _decrypt, which spell out this layout by hand.+ nonce = os.urandom(16)+ cmk_bytes = _derive_with_context(rec["_symmetric_key"], enc_context, nonce)+ pad_stream = _expand_key(cmk_bytes, len(private_der))+ encrypted = bytes(a ^ b for a, b in zip(private_der, pad_stream))+ ctx_hash = hashlib.sha256(+ json.dumps(enc_context, sort_keys=True).encode()+ ).digest()+ ciphertext = rec["KeyId"].encode() + ctx_hash + nonce + encrypted++ logger.info("Generated data key pair %s under CMK %s", spec, rec["KeyId"])+ payload = {+ "KeyId": rec["Arn"],+ "KeyPairSpec": spec,+ "PrivateKeyCiphertextBlob": base64.b64encode(ciphertext).decode(),+ "PublicKey": base64.b64encode(public_der).decode(),+ }+ return payload, private_der, None+++def _generate_data_key_pair(data):+ payload, private_der, err = _generate_data_key_pair_common(+ data, "GenerateDataKeyPair"+ )+ if err:+ return err+ payload["PrivateKeyPlaintext"] = base64.b64encode(private_der).decode()+ return json_response(payload)+++def _generate_data_key_pair_without_plaintext(data):+ """Same as GenerateDataKeyPair, minus PrivateKeyPlaintext.++ That omission is the whole point of this variant: the private key exists+ only inside this process for the moment it takes to wrap it, so the caller+ can persist the ciphertext + public key without ever holding the private+ key material.+ """+ payload, _private_der, err = _generate_data_key_pair_common(+ data, "GenerateDataKeyPairWithoutPlaintext"+ )+ if err:+ return err+ return json_response(payload)++def _generate_data_key_without_plaintext(data):rec, _data_key, result = _generate_data_key_common(data)if rec is None:@@ -888,10 +1017,17 @@ def _generate_data_key_without_plaintext(data):})-def _derive_with_context(key_bytes, enc_context):- """Mix EncryptionContext into key material so decrypt requires the same context."""+def _derive_with_context(key_bytes, enc_context, nonce):+ """Mix EncryptionContext and a per-blob nonce into key material.++ The nonce makes the keystream unique per ciphertext even under the same CMK+ and EncryptionContext, so wrapping a partly-predictable plaintext (e.g. a+ key pair's DER, whose public half is recoverable from the returned+ PublicKey) no longer leaks the keystream. decrypt reads the nonce back out+ of the ciphertext layout.+ """ctx_bytes = json.dumps(enc_context, sort_keys=True).encode()- return hashlib.sha256(key_bytes + ctx_bytes).digest()+ return hashlib.sha256(key_bytes + ctx_bytes + nonce).digest()def _expand_key(key_bytes, length):@@ -1125,6 +1261,8 @@ async def handle_request(method, path, headers, body, query_params):"Decrypt": _decrypt,"GenerateDataKey": _generate_data_key,"GenerateDataKeyWithoutPlaintext": _generate_data_key_without_plaintext,+ "GenerateDataKeyPair": _generate_data_key_pair,+ "GenerateDataKeyPairWithoutPlaintext": _generate_data_key_pair_without_plaintext,"CreateAlias": _create_alias,"DeleteAlias": _delete_alias,"ListAliases": _list_aliases,diff --git a/tests/test_kms.py b/tests/test_kms.pyindex 0fce92a6..943d9bf4 100644--- a/tests/test_kms.py+++ b/tests/test_kms.py@@ -321,6 +321,284 @@ def test_kms_generate_data_key_without_plaintext(kms_client):assert resp["CiphertextBlob"]assert "Plaintext" not in resp+def test_kms_generate_data_key_pair_ed25519(kms_client):+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ resp = kms_client.generate_data_key_pair(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ assert key_id in resp["KeyId"]+ assert resp["KeyPairSpec"] == "ECC_NIST_EDWARDS25519"+ assert resp["PrivateKeyCiphertextBlob"]+ # Unlike the WithoutPlaintext variant, this one does hand back the private key.+ assert len(resp["PrivateKeyPlaintext"]) == 48+ assert len(resp["PublicKey"]) == 44++def test_kms_generate_data_key_pair_plaintext_matches_ciphertext(kms_client):+ """PrivateKeyPlaintext must be the same key that PrivateKeyCiphertextBlob wraps."""+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ resp = kms_client.generate_data_key_pair(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ dec_resp = kms_client.decrypt(+ CiphertextBlob=resp["PrivateKeyCiphertextBlob"], KeyId=key_id+ )+ assert dec_resp["Plaintext"] == resp["PrivateKeyPlaintext"]++def test_kms_generate_data_key_pair_rsa(kms_client):+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ resp = kms_client.generate_data_key_pair(KeyId=key_id, KeyPairSpec="RSA_2048")+ assert resp["KeyPairSpec"] == "RSA_2048"+ assert resp["PrivateKeyPlaintext"]+ assert resp["PublicKey"].startswith(bytes.fromhex("3082012230"))++def test_kms_generate_data_key_pair_variants_agree(kms_client):+ """The two variants differ in exactly one field: PrivateKeyPlaintext."""+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ with_pt = kms_client.generate_data_key_pair(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ without_pt = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )++ def shape(resp):+ return {k for k in resp if k != "ResponseMetadata"}++ assert shape(with_pt) - shape(without_pt) == {"PrivateKeyPlaintext"}+ assert shape(without_pt) - shape(with_pt) == set()++def test_kms_generate_data_key_pair_usable_for_signing(kms_client):+ """The returned plaintext private key must actually work with the public key."""+ serialization = pytest.importorskip(+ "cryptography.hazmat.primitives.serialization"+ )+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ resp = kms_client.generate_data_key_pair(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ private_key = serialization.load_der_private_key(+ resp["PrivateKeyPlaintext"], password=None+ )+ public_key = serialization.load_der_public_key(resp["PublicKey"])+ public_key.verify(private_key.sign(b"message"), b"message")++def test_kms_generate_data_key_pair_requires_symmetric_key(kms_client):+ key = kms_client.create_key(KeySpec="RSA_2048", KeyUsage="SIGN_VERIFY")+ key_id = key["KeyMetadata"]["KeyId"]++ with pytest.raises(ClientError) as exc:+ kms_client.generate_data_key_pair(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ assert exc.value.response["Error"]["Code"] == "UnsupportedOperationException"+ assert "GenerateDataKeyPair requires" in exc.value.response["Error"]["Message"]++def test_kms_generate_data_key_pair_without_plaintext_ed25519(kms_client):+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ resp = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ assert key_id in resp["KeyId"]+ assert resp["KeyPairSpec"] == "ECC_NIST_EDWARDS25519"+ assert resp["PrivateKeyCiphertextBlob"]+ # The entire point of the WithoutPlaintext variant: the private key+ # plaintext is never returned to the caller.+ assert "PrivateKeyPlaintext" not in resp+ # Ed25519 SubjectPublicKeyInfo is a fixed 44 bytes: a 12-byte header+ # (SEQUENCE + AlgorithmIdentifier for OID 1.3.101.112) + the 32-byte key.+ assert resp["PublicKey"].startswith(bytes.fromhex("302a300506032b6570032100"))+ assert len(resp["PublicKey"]) == 44++def test_kms_generate_data_key_pair_without_plaintext_rsa(kms_client):+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ resp = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="RSA_2048"+ )+ assert resp["KeyPairSpec"] == "RSA_2048"+ assert resp["PrivateKeyCiphertextBlob"]+ assert "PrivateKeyPlaintext" not in resp+ # SubjectPublicKeyInfo for RSA-2048.+ assert resp["PublicKey"].startswith(bytes.fromhex("3082012230"))++def test_kms_generate_data_key_pair_without_plaintext_decrypt_roundtrip(kms_client):+ """The wrapped private key decrypts back to a PKCS#8 DER private key."""+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ gen_resp = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ dec_resp = kms_client.decrypt(+ CiphertextBlob=gen_resp["PrivateKeyCiphertextBlob"], KeyId=key_id+ )+ # PKCS#8 DER for an Ed25519 private key is a fixed 48 bytes.+ assert len(dec_resp["Plaintext"]) == 48+ assert dec_resp["Plaintext"].startswith(bytes.fromhex("302e020100300506032b657004220420"))++def test_kms_generate_data_key_pair_without_plaintext_encryption_context(kms_client):+ """EncryptionContext binds the wrapped private key: decrypt must supply the same one."""+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]+ context = {"purpose": "signing", "owner": "svc-a"}++ gen_resp = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id,+ KeyPairSpec="ECC_NIST_EDWARDS25519",+ EncryptionContext=context,+ )+ dec_resp = kms_client.decrypt(+ CiphertextBlob=gen_resp["PrivateKeyCiphertextBlob"],+ KeyId=key_id,+ EncryptionContext=context,+ )+ assert len(dec_resp["Plaintext"]) == 48++ with pytest.raises(ClientError) as exc:+ kms_client.decrypt(+ CiphertextBlob=gen_resp["PrivateKeyCiphertextBlob"],+ KeyId=key_id,+ EncryptionContext={"purpose": "signing", "owner": "svc-b"},+ )+ assert exc.value.response["Error"]["Code"] == "InvalidCiphertextException"++def test_kms_generate_data_key_pair_without_plaintext_keys_match(kms_client):+ """The wrapped private key must actually pair with the returned public key.++ Guards against a key pair whose metadata and material disagree — the public+ key is useless if it does not verify what the private key signs.+ """+ serialization = pytest.importorskip(+ "cryptography.hazmat.primitives.serialization"+ )+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ gen_resp = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ dec_resp = kms_client.decrypt(+ CiphertextBlob=gen_resp["PrivateKeyCiphertextBlob"], KeyId=key_id+ )++ private_key = serialization.load_der_private_key(dec_resp["Plaintext"], password=None)+ derived_public = private_key.public_key().public_bytes(+ serialization.Encoding.DER,+ serialization.PublicFormat.SubjectPublicKeyInfo,+ )+ assert derived_public == gen_resp["PublicKey"]++ # And the pair actually works end to end.+ signature = private_key.sign(b"message")+ public_key = serialization.load_der_public_key(gen_resp["PublicKey"])+ public_key.verify(signature, b"message")++def test_kms_generate_data_key_pair_without_plaintext_requires_symmetric_key(kms_client):+ """The CMK wraps the generated private key, so it must be symmetric."""+ key = kms_client.create_key(KeySpec="RSA_2048", KeyUsage="SIGN_VERIFY")+ key_id = key["KeyMetadata"]["KeyId"]++ with pytest.raises(ClientError) as exc:+ kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ assert exc.value.response["Error"]["Code"] == "UnsupportedOperationException"+ # Names this operation, not the GenerateDataKeyPair variant it shares a helper with.+ assert (+ "GenerateDataKeyPairWithoutPlaintext requires"+ in exc.value.response["Error"]["Message"]+ )++def test_kms_generate_data_key_pair_without_plaintext_rejects_bad_spec(kms_client):+ key = kms_client.create_key(+ KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"+ )+ key_id = key["KeyMetadata"]["KeyId"]++ with pytest.raises(ClientError) as exc:+ kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_P224"+ )+ assert exc.value.response["Error"]["Code"] == "ValidationException"++def test_kms_generate_data_key_pair_without_plaintext_nonexistent_key(kms_client):+ with pytest.raises(ClientError) as exc:+ kms_client.generate_data_key_pair_without_plaintext(+ KeyId=str(_uuid_mod.uuid4()), KeyPairSpec="ECC_NIST_EDWARDS25519"+ )+ assert exc.value.response["Error"]["Code"] == "NotFoundException"++def test_kms_ciphertext_uses_per_blob_nonce(kms_client):+ """Same plaintext + CMK + EncryptionContext must not yield identical+ ciphertext. Each blob carries a random nonce mixed into key derivation, so+ the XOR keystream never repeats across blobs. Without it the keystream would+ recur, and a wrapped key pair's private half would be recoverable from the+ public outputs (the DER embeds the public modulus). Both blobs must still+ decrypt back to the same plaintext."""+ key = kms_client.create_key()+ key_id = key["KeyMetadata"]["KeyId"]+ ctx = {"app": "prod"}+ zeros = b"\x00" * 48+ c1 = kms_client.encrypt(KeyId=key_id, Plaintext=zeros, EncryptionContext=ctx)["CiphertextBlob"]+ c2 = kms_client.encrypt(KeyId=key_id, Plaintext=zeros, EncryptionContext=ctx)["CiphertextBlob"]+ assert c1 != c2, "identical ciphertext means the keystream repeats (no nonce)"+ assert kms_client.decrypt(CiphertextBlob=c1, EncryptionContext=ctx)["Plaintext"] == zeros+ assert kms_client.decrypt(CiphertextBlob=c2, EncryptionContext=ctx)["Plaintext"] == zeros++def test_kms_data_key_pair_private_key_resists_keystream_attack(kms_client):+ """A wrapped private key must not fall out of a keystream-reuse attack.+ The attacker grabs a keystream via a chosen-plaintext Encrypt under the same+ CMK + EncryptionContext, then XORs it into the pair blob's data region. The+ per-blob nonce makes the two keystreams differ, so the recovered bytes are+ not a valid private key. Without the nonce this would reconstruct the DER."""+ serialization = pytest.importorskip("cryptography.hazmat.primitives.serialization")+ key = kms_client.create_key()+ key_id = key["KeyMetadata"]["KeyId"]+ ctx = {"app": "prod"}+ blob = kms_client.generate_data_key_pair_without_plaintext(+ KeyId=key_id, KeyPairSpec="ECC_NIST_EDWARDS25519", EncryptionContext=ctx+ )["PrivateKeyCiphertextBlob"]+ keystream_blob = kms_client.encrypt(+ KeyId=key_id, Plaintext=b"\x00" * len(blob), EncryptionContext=ctx+ )["CiphertextBlob"]+ # data region starts after key_id(36) + ctx_hash(32) + nonce(16) = 84+ guess = bytes(a ^ b for a, b in zip(blob[84:], keystream_blob[84:]))+ with pytest.raises(Exception):+ serialization.load_der_private_key(guess, password=None)+def test_kms_get_public_key(kms_client):key = kms_client.create_key(KeySpec="RSA_2048", KeyUsage="SIGN_VERIFY")key_id = key["KeyMetadata"]["KeyId"]