Endpoints
Each vault has two endpoints, and most "it doesn't work" reports come from using the wrong one. Every oci kms command needs an explicit --endpoint.
| Endpoint | Used by | Operations |
|---|---|---|
| Management endpoint | oci kms management key ..., key-version ... | Create, list, enable, disable, rotate, schedule deletion of keys |
| Crypto endpoint | oci kms crypto ... | Encrypt, decrypt, generate data encryption key, sign, verify, export |
| None (regional KMS API) | oci kms management vault ... | Create and list vaults, replicas, backups |
VAULT=ocid1.vault.oc1..xxxx
MGMT=$(oci kms management vault get --vault-id "$VAULT" --query 'data."management-endpoint"' --raw-output)
CRYPTO=$(oci kms management vault get --vault-id "$VAULT" --query 'data."crypto-endpoint"' --raw-output)
echo "$MGMT"; echo "$CRYPTO"
Tip
Private vaults and vaults in other regions have their own endpoint hostnames. Always read the endpoints from
vault getand don't build them from a pattern. For private endpoints, the DNS name has to resolve inside your VCN.
Keys
# List keys in the vault (the endpoint selects the vault)
oci kms management key list -c "$COMP" --endpoint "$MGMT" --all \
--query 'data[].{name:"display-name", id:id, state:"lifecycle-state", mode:"protection-mode", alg:algorithm}' \
--output table
# Create an AES-256 key. Protection mode can't be changed later
oci kms management key create -c "$COMP" --endpoint "$MGMT" \
--display-name app-orders-mek \
--key-shape '{"algorithm":"AES","length":32}' \
--protection-mode HSM \
--wait-for-state ENABLED
# Inspect
oci kms management key get --key-id "$KEY" --endpoint "$MGMT"
# Disable and re-enable (anything that depends on it stops working while disabled)
oci kms management key disable --key-id "$KEY" --endpoint "$MGMT"
oci kms management key enable --key-id "$KEY" --endpoint "$MGMT"
--protection-mode | Where the key material lives | Notes |
|---|---|---|
HSM (default) | FIPS-validated HSM | Key material never leaves the HSM in plaintext. Required for many compliance regimes |
SOFTWARE | KMS server, software-protected | Exportable, cheaper. Check current pricing |
EXTERNAL | Your third-party key manager (External KMS) | Needs --external-key-reference. Availability depends on your vault type |
--length is in bytes for AES (16, 24 or 32). A value of 256 is rejected.
Encrypt and decrypt small payloads
--plaintext takes base64. decrypt returns base64 that you have to decode yourself.
PT=$(printf 'db-password-or-small-secret' | base64 -w0)
CT=$(oci kms crypto encrypt --key-id "$KEY" --endpoint "$CRYPTO" \
--plaintext "$PT" --query 'data.ciphertext' --raw-output)
oci kms crypto decrypt --key-id "$KEY" --endpoint "$CRYPTO" \
--ciphertext "$CT" --query 'data.plaintext' --raw-output | base64 -d
Bind ciphertext to a context with associated data. You must pass the same value to decrypt, or decryption fails:
oci kms crypto encrypt --key-id "$KEY" --endpoint "$CRYPTO" --plaintext "$PT" \
--associated-data '{"tenant":"acme","purpose":"orders-db"}' --query 'data.ciphertext' --raw-output
The ciphertext records which key version encrypted it, so decryption after a rotation still works without --key-version-id. Direct encrypt is meant for small values such as passwords and tokens. Check the API's payload size limit and use envelope encryption for files.
Envelope encryption (files and large data)
Generate a data encryption key (DEK). The data is encrypted locally with the DEK, and you store only the wrapped DEK.
oci kms crypto generate-data-encryption-key --key-id "$KEY" --endpoint "$CRYPTO" \
--key-shape '{"algorithm":"AES","length":32}' \
--include-plaintext-key true > dek.json
jq -r '.data.ciphertext' dek.json > dek.wrapped # keep this next to the data
DEK_HEX=$(jq -r '.data.plaintext' dek.json | base64 -d | xxd -p -c 64)
rm -f dek.json
IV=$(openssl rand -hex 16); echo "$IV" > backup.tar.iv
openssl enc -aes-256-cbc -K "$DEK_HEX" -iv "$IV" -in backup.tar -out backup.tar.enc
unset DEK_HEX
To decrypt, unwrap the DEK with oci kms crypto decrypt --ciphertext "$(cat dek.wrapped)", decode it, then decrypt locally.
Caution
The plaintext DEK can decrypt the data without KMS. Never log it, write it to disk unencrypted, or keep it in shell history. In real tooling, do this in a process with the SDK (below), not in shell variables. If a DEK has been exposed, every object encrypted with it has to be re-encrypted.
The same flow with the Python SDK and AES-GCM:
import base64, os, oci
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
cfg = oci.config.from_file()
kms = oci.key_management.KmsCryptoClient(cfg, service_endpoint=CRYPTO)
resp = kms.generate_data_encryption_key(
oci.key_management.models.GenerateKeyDetails(
key_id=KEY, include_plaintext_key=True,
key_shape=oci.key_management.models.KeyShape(algorithm="AES", length=32)))
dek = base64.b64decode(resp.data.plaintext)
nonce = os.urandom(12)
blob = AESGCM(dek).encrypt(nonce, open("backup.tar", "rb").read(), None)
open("backup.tar.enc", "wb").write(nonce + blob)
open("backup.tar.dek", "w").write(resp.data.ciphertext) # wrapped DEK only
del dek
On OCI compute, replace from_file() with an instance principal signer, so the host has no API key on disk.
Key versions and rotation
# Manual rotation: new version becomes current for new encrypt calls
oci kms management key-version create --key-id "$KEY" --endpoint "$MGMT"
oci kms management key-version list --key-id "$KEY" --endpoint "$MGMT" --all \
--query 'data[].{id:id, created:"time-created", state:"lifecycle-state"}' --output table
# Which version is current
oci kms management key get --key-id "$KEY" --endpoint "$MGMT" --query 'data."current-key-version"'
Auto-rotation is set with --is-auto-rotation-enabled true and --auto-key-rotation-details on key create or key update. Generate the JSON skeleton with --generate-param-json-input auto-key-rotation-details, because field names vary by CLI version.
What rotation does and does not do:
| Asset | After rotation |
|---|---|
| New encrypt / DEK calls | Use the new version |
| Existing ciphertext and wrapped DEKs | Still decrypt with the old version. They aren't re-encrypted |
| Block volumes, buckets, databases using the key | Keep working. Each service rewraps on its own schedule. Check the service docs |
Old version deleted (key-version schedule-deletion) | Anything still wrapped under it can't be decrypted |
To actually retire old material, decrypt and re-encrypt your own wrapped DEKs. There is no server-side re-encrypt command, so script it with decrypt followed by encrypt against the current version.
Deletion
oci kms management key schedule-deletion --key-id "$KEY" --endpoint "$MGMT" \
--time-of-deletion "$(date -u -d '+30 days' +%Y-%m-%dT%H:%M:%SZ)"
oci kms management key cancel-deletion --key-id "$KEY" --endpoint "$MGMT"
Warning
After the deletion date, every volume, bucket, database and wrapped DEK that uses the key is permanently unrecoverable. Before you schedule deletion, find every consumer. Check the
kms-key-idfield on volumes, buckets and databases, and search Audit for recentDecryptandGenerateDataEncryptionKeycalls against the key. Disable the key for a few weeks first, because disabling can be reversed.
Errors
| Error | Cause | Fix |
|---|---|---|
NotAuthorizedOrNotFound on crypto calls | Missing use keys permission, or the wrong endpoint for that vault | Allow group X to use keys in compartment Y where target.key.id = '...', and re-read the endpoint |
Could not connect to endpoint | Private vault endpoint not resolvable or routable | Fix the DNS or route from your VCN to the private endpoint |
InvalidParameter on --plaintext | Input not base64 | base64 -w0 the input |
| Decrypt fails after encrypt worked | Different --associated-data, or a deleted key version | Pass identical AAD. Restore the version with cancel-deletion if you are still inside the window |
Conflict / key in UPDATING | Previous operation still running | Wait, then retry. Back off on 429s |
Key won't create in EXTERNAL mode | Vault type doesn't support external keys | Use an External KMS vault |
Least-privilege policy patterns
Allow group vault-admins to manage keys in compartment security
Allow dynamic-group orders-app to use keys in compartment security where target.key.id = 'ocid1.key.oc1..xxxx'
Allow service blockstorage, objectstorage-<region> to use keys in compartment security
Keep application principals at use keys. That covers crypto operations, but they can't disable, rotate or schedule deletion.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get