Sub topic 16
Imported key material and custom key stores
This chapter covers two ways to control where KMS key material comes from or lives: importing your own key material, and using a custom key store. Both are for teams with compliance or sovereignty requirements. Both cost you availability, durability and convenience compared with a standard KMS key.
It assumes you know the earlier chapters: symmetric KMS keys, key policies, aliases, rotation and key states.
The default and the two exceptions
A standard customer managed key has Origin set to AWS_KMS. KMS generates the key material inside its HSMs (hardware security modules) and never exports it. Two other origins change that:
| Origin | Where the key material is made | Where it is used |
|---|---|---|
| `AWS_KMS` | KMS | KMS HSMs |
| `EXTERNAL` | You | KMS HSMs, after you import it |
| `AWS_CLOUDHSM` | CloudHSM cluster you own | Your CloudHSM cluster |
| `EXTERNAL_KEY_STORE` | An HSM or key manager outside AWS | That external system |
The first two rows use the regular KMS API. The last two use a custom key store, where KMS calls out to another system for every cryptographic operation.
Imported key material
With imported key material (often called BYOK, "bring your own key"), you generate the key bytes yourself and load them into a KMS key. The usual reasons are a policy that requires you to generate keys from your own entropy source, or a need to keep a copy outside AWS.
How the import works
You never send the key bytes in the clear. KMS gives you a wrapping public key and an import token. You encrypt your key material with that public key, then send the result and the token back.
- Create a KMS key with origin
EXTERNAL. It starts in thePendingImportstate and cannot be used. - Call
get-parameters-for-importto get a wrapping public key and an import token. Both are valid for 24 hours. - Generate the key material locally and encrypt it with the wrapping public key.
- Call
import-key-materialwith the wrapped material and the token. The key becomesEnabled.
KEY_ID=$(aws kms create-key \
--origin EXTERNAL \
--description "Imported key demo" \
--query KeyMetadata.KeyId --output text)
aws kms get-parameters-for-import \
--key-id "$KEY_ID" \
--wrapping-algorithm RSAES_OAEP_SHA_256 \
--wrapping-key-spec RSA_2048 \
--output json > params.json
jq -r .PublicKey params.json | base64 --decode > wrapping_key.der
jq -r .ImportToken params.json | base64 --decode > import_token.bin
# 256 bits of key material for a symmetric key
openssl rand -out key_material.bin 32
openssl pkeyutl -encrypt \
-in key_material.bin -out wrapped_material.bin \
-pubin -keyform DER -inkey wrapping_key.der \
-pkeyopt rsa_padding_mode:oaep \
-pkeyopt rsa_oaep_md:sha256 \
-pkeyopt rsa_mgf1_md:sha256
aws kms import-key-material \
--key-id "$KEY_ID" \
--encrypted-key-material fileb://wrapped_material.bin \
--import-token fileb://import_token.bin \
--expiration-model KEY_MATERIAL_DOES_NOT_EXPIRECall get-parameters-for-import only once per import attempt. Each call returns a new public key and token, and the token only works with the public key it was issued with. That is why the script saves the response to params.json instead of calling the API twice.
Once the key is enabled, it works like any other symmetric key. encrypt, decrypt, generate-data-key, aliases, key policies and grants behave the same way.
What changes after import
Imported material changes the operational rules:
- Durability is your job. KMS stores the material redundantly, but it can also be lost: it expires, or someone deletes it. If you did not keep a copy, ciphertext under that key is unrecoverable.
- Expiration is optional. With
KEY_MATERIAL_EXPIRESand a--valid-totimestamp, KMS deletes the material at that time. The key then goes back toPendingImportand every decrypt fails until you reimport. - You can delete the material on demand.
aws kms delete-imported-key-materialmakes the key unusable right away, without scheduling deletion of the key itself. It is a fast way to cut off access, but only if you can restore it. - Reimport must use the same bytes. KMS accepts only the original material for the same key. You cannot swap in new material to rotate. To keep decrypt working, you must still hold the original bytes.
- No automatic rotation.
enable-key-rotationis not supported for the simple case of a key with imported material. The usual pattern is to create a new key, import new material, and move the alias to it. Older ciphertext still needs the old key. Check the current KMS documentation, because rotation support for imported material has been evolving.
Imported keys also have a few other limits. Imported material cannot be used with the AWS_KMS automatic rotation feature. A Multi-Region primary key with imported material does not share its material with replicas, so you import the same bytes into each replica separately.
Custom key stores
A custom key store is a KMS resource that points to a different place to keep and use the key. KMS remains the front door: your applications, IAM policies, key policies and CloudTrail logs look the same. Behind the door, KMS forwards each operation to the key store. There are two types.
CloudHSM key store
A CloudHSM key store is backed by an AWS CloudHSM cluster that you own. The key material is generated in HSMs that only your account uses, and KMS performs operations on them. You pay for the HSMs, and you are responsible for the cluster's health.
Use it when a standard (multi-tenant) KMS HSM does not satisfy your requirements, but you still want KMS integration with S3, EBS and the other services.
Plan for these points:
- Run at least two HSMs in different Availability Zones. A single HSM is a single point of failure for every key in the store.
- Only symmetric encryption keys are supported, and the keys are created in the cluster with
--origin AWS_CLOUDHSM. - Throughput is limited by your cluster size, not by the KMS shared quotas.
External key store (XKS)
An external key store keeps the key material entirely outside AWS, in an HSM or key manager you operate. KMS reaches it through an XKS proxy, a service you run that implements the XKS proxy API. The key material never enters AWS. Every encrypt, decrypt and data key operation becomes a call to your proxy.
aws kms create-custom-key-store \
--custom-key-store-name xks-demo \
--custom-key-store-type EXTERNAL_KEY_STORE \
--xks-proxy-connectivity PUBLIC_ENDPOINT \
--xks-proxy-uri-endpoint https://xks.example.com \
--xks-proxy-uri-path /kms/xks/v1 \
--xks-proxy-authentication-credential \
AccessKeyId=<access-key-id>,RawSecretAccessKey=<secret-access-key>
aws kms connect-custom-key-store --custom-key-store-id <store-id>
aws kms create-key \
--origin EXTERNAL_KEY_STORE \
--custom-key-store-id <store-id> \
--xks-key-id <external-key-id> \
--description "Key backed by external key manager"The first command registers the proxy. The second connects KMS to it. The third creates a KMS key that refers to a key that already exists in the external manager, identified by --xks-key-id. Replace the placeholders with your own values. VPC_ENDPOINT_SERVICE connectivity is an alternative to a public endpoint.
Costs of a custom key store
Every custom key store trades away something that the standard path gives you for free.
- Availability. If the cluster or proxy is down or disconnected, every key in the store is unusable. S3 objects, EBS volumes and database snapshots encrypted with those keys become unreadable until the store is reachable again. For XKS, this includes outages of the network path between AWS and your proxy.
- Latency. Each operation makes a network round trip. XKS operations have a strict time limit on the proxy's response, so a slow proxy causes errors, not just slow calls.
- Features. Custom key stores support only symmetric encryption keys. They do not support automatic rotation or Multi-Region keys.
- Responsibility. You operate, patch, scale and back up the HSMs or the proxy.
Envelope encryption lessens the impact: a service that has already fetched and cached a data key (for example, through S3 Bucket Keys) calls KMS less often. It does not remove the dependency, because the data key still has to be decrypted at some point.
You can disconnect a store with disconnect-custom-key-store. Disconnecting is a deliberate kill switch: it makes every key in the store unusable until you reconnect.
Choosing between them
- Choose a standard key unless a written requirement says otherwise. It has the best availability and the most features.
- Choose imported material when you must generate the key yourself and are prepared to keep a durable copy.
- Choose a CloudHSM key store when you need single-tenant HSMs and can run a highly available cluster.
- Choose XKS only when key material must never be held by AWS, and you accept that your proxy becomes part of every request path.
Exercise
You imported 256-bit key material into a KMS key with an expiration date. The date passes, and your application starts failing on decrypt calls. What state is the key in, and what do you need in order to recover without losing any data?
Show answer
The key is back in the PendingImport state because KMS deleted the expired material. To recover, call get-parameters-for-import, wrap the original key material with the new public key, and call import-key-material for the same key. KMS accepts only the same bytes that were imported originally, so you need your retained copy of the plaintext key material. If you did not keep one, ciphertext under that key cannot be recovered.
Exercise
A team uses an external key store for an S3 bucket. The XKS proxy goes down for an hour. What happens to reads and writes on the bucket, and which setting or design choice reduces how often S3 calls KMS?
Show answer
Operations that need KMS to generate or decrypt a data key fail while the proxy is unreachable, so objects encrypted with that key cannot be read and new writes with that key fail. Enabling S3 Bucket Keys reduces the number of KMS calls, because S3 reuses a bucket-level key for many objects. It does not remove the dependency on the proxy.