NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / ENTERPRISE / SECRET VAULTS AND KEYS

Secret vaults and keys

A secret vault keeps an application's secrets (passwords, API keys, connection strings, certificates) in the database server itself, in place of a cloud key vault or a secrets manager. Each value is encrypted under keys held by an HSM, a post-quantum key pair or (for development) a key file; every version is kept; deletes are soft and purge-protected; and every read and change is audited and hash-chained. A vault can also hold keys that the server uses for you without ever returning them, and keys that encrypted columns are sealed with.

Vaults are part of the enterprise server (rasterizeddb-enterprise). A vault is a ledger table of a fixed shape, so it lives under the same chain, anchors and verification.

Create a vault

sql
CREATE VAULT app_secrets
  KEY PROVIDER 'pkcs11:module=/usr/lib/softhsm/libsofthsm2.so;token=rdb;key=vault-kek;pin-source=/etc/rasterizeddb/hsm.pin'
  RETAIN DELETED 30 DAYS;
ClauseRequiredWhat it does
KEY PROVIDER '<uri>'YesWhere the vault's key-encryption key lives. See Key providers.
RETAIN DELETED <n> <unit>NoHow long a deleted secret is kept before it may be purged. Default 90 days. Units as for ledger tables: SECOND to YEAR, where a month is 31 days and a year 366.
NO DROP UNTIL <n> <unit> IDLENoDROP TABLE on the vault is refused until nothing has been written to it for this long.
ANCHOR TO '<sink>' [, …]NoAnchor sinks for the database's chain, as for a ledger table.

CREATE VAULT IF NOT EXISTS name … is accepted; over an existing vault it changes nothing and opens no key provider. A vault takes no column list.

The key provider is opened when the vault is created, so a missing PIN, an unreadable key file or an unreachable token fails the CREATE VAULT and no vault is made:

text
ERROR:  the vault's key provider does not open: pkcs11: give the PIN by pin-source=<file> or pin-env=<VAR>

A vault is dropped with DROP TABLE app_secrets, subject to its legal holds and NO DROP UNTIL clause.

Key providers

Every secret version is sealed with AES-256-GCM under a data key of its own. Data keys are stored only wrapped by the provider's key-encryption key, in the vault's keyring. The provider URI has no spaces.

ProviderFormWhat it is
PKCS#11pkcs11:module=<library>;token=<label>;key=<label>;pin-source=<file> or …;pin-env=<VAR>, optionally ;mechanism=gcm|kwp|cbcA key inside an HSM, a network or cloud HSM, or a TPM 2.0 through tpm2-pkcs11. The key never leaves the device: data keys are wrapped and unwrapped inside it.
ML-KEMmlkem:<path> or mlkem:<path>;createA hybrid post-quantum key pair, ML-KEM-768 with X25519, in a file: breaking the wrapping needs breaking both.
Filefile:<path>A 32-byte key, or 64 hexadecimal characters, in a file on the database host. For tests and development only.
TPMtpm:<path>[;tcti=<tcti>][;create]A key sealed by this machine's TPM 2.0. Not in the release builds: use a TPM through pkcs11: (tpm2-pkcs11) instead.

PKCS#11

AttributeRequiredWhat it is
moduleYesThe vendor's PKCS#11 library, loaded when the vault opens.
tokenYesThe token's label.
keyYesThe label of an AES secret-key object on the token, made with your HSM's tools before the vault.
pin-source=<file>One of theseA file holding the user PIN.
pin-env=<VAR>One of theseAn environment variable of the server holding the user PIN.
mechanismNogcm (AES-256-GCM on the token, the default), kwp (AES key wrap with padding) or cbc (AES-CBC with padding), for tokens without GCM.

The PIN is never written in SQL. SHOW LEDGER shows the provider with the PIN source cut (pin-source=…). Several vaults may share one token.

ML-KEM key files

The file holds 192 hexadecimal characters. With ;create, the server makes one (mode 600) the first time the vault is opened, if the file does not exist; its directory must exist. Keep a copy of the file somewhere safe: without it, no secret of the vault can be read again.

sql
CREATE VAULT app_secrets KEY PROVIDER 'mlkem:/etc/rasterizeddb/app_secrets.mlkem;create';

Key files

A file: provider keeps the key on the same host as the data, so the server refuses it unless the server's environment has RDB_VAULT_ALLOW_FILE_KEY=1:

sh
openssl rand -hex 32 > /etc/rasterizeddb/dev-vault.key
chmod 600 /etc/rasterizeddb/dev-vault.key
export RDB_VAULT_ALLOW_FILE_KEY=1
sql
CREATE VAULT dev_secrets KEY PROVIDER 'file:/etc/rasterizeddb/dev-vault.key';

On Linux and macOS, file: and mlkem: key files must be readable by their owner only; a file with group or other permissions is refused.

Secrets

The VAULT statements read and write a vault. Each answers with text rows.

sql
VAULT app_secrets SET SECRET 'db/password' = 'correct horse battery staple';
text
db/password version 1
sql
VAULT app_secrets GET SECRET 'db/password';
text
correct horse battery staple

Setting a secret

text
VAULT <vault> SET SECRET '<name>' = '<value>'
      [CONTENT TYPE '<type>'] [EXPIRES '<time>'] [NOT BEFORE '<time>'] [TAGS '<text>']
  • A name is 1 to 256 characters of A-Z a-z 0-9 . _ - / :. Names starting key: belong to the vault's keys.
  • Setting an existing name adds a new version. Older versions stay readable with VERSION n until the secret is purged.
  • Times are UTC, 'YYYY-MM-DD' or 'YYYY-MM-DDTHH:MM:SSZ'. A secret is not served after EXPIRES or before NOT BEFORE.
  • The value is a quoted literal; double a quote inside it ('it''s'). Wherever the server keeps statement text (its log, statement statistics), a statement is cut at the word SECRET, so the value is not kept. Only the ciphertext is written to the table, the write-ahead log, the replication stream and backups. Use TLS between client and server.
  • Setting a deleted secret is refused: recover or purge it first. Setting a disabled secret adds a version and makes the secret active again.

Reading

StatementAnswer
VAULT v GET SECRET 'name'The newest version's value, one row.
VAULT v GET SECRET 'name' VERSION 3That version's value.
VAULT v GET ALLEvery active, current, unexpired secret, one JSON row each: {"name":"db/password","version":1,"value":"…"}.
VAULT v GET ALL PREFIX 'app/'The same, for names starting with the prefix.
VAULT v LISTOne row per secret and key: name vN state, then type=…, expires_us=… and updated_us=… when set. No values.
VAULT v VERSIONS SECRET 'name'The secret's history: vN set by <role> at <micros> per version, (purged) after a purged one, and a line per state change.
text
db/password v2 active type=text/plain updated_us=1795701600000000
key:pii v1 active type=aes256-gcm updated_us=1795701500000000

Every read is audited before the value is returned; if the audit cannot be written, nothing is returned. A read that is refused is audited too, with why:

text
ERROR:  secret 'db/password': it is disabled

The reasons are no such secret, it is disabled (or deleted, purged), no such version, it has expired, it is not valid yet, and its key was purged; it can no longer be read.

States: disable, delete, recover, purge

StatementFromTo
VAULT v DISABLE SECRET 'name'activedisabled
VAULT v ENABLE SECRET 'name'disabledactive
VAULT v DELETE SECRET 'name'active or disableddeleted
VAULT v RECOVER SECRET 'name'deletedactive
VAULT v PURGE SECRET 'name'deleted, once RETAIN DELETED has passed since the deletepurged

Only an active secret is served. A PURGE before the retention has passed is refused (secret 'x' is purge-protected for 2592000 s after its deletion).

A purge crypto-shreds the secret: its data keys are removed from the keyring, so its sealed values can never be opened again, by anyone. The rows and their digests stay, so the ledger still verifies. A purged secret cannot be recovered; setting the name again starts a new version after the old ones.

text
db/password purged: 2 version(s) can never be decrypted again

PURGE and ROTATE KEY change the keyring at once, which a ROLLBACK could not undo, so they are refused inside a transaction block (SQLSTATE 25001). A batch of statements that holds one runs statement by statement. The other VAULT statements take part in the surrounding transaction: a SET that rolls back leaves no version.

Plain SQL on a vault

A vault's table has fixed columns: name, version, value, state, content_type, not_before, expires_at, tags, created_by, created_at and __ledger_ts. A role with SELECT on it can query them, but value holds only the sealed value (base64), never the secret. INSERT, UPDATE, DELETE and TRUNCATE on a vault are refused: only the VAULT statements write it.

Keys used in place

A vault can hold keys that the server uses and never returns:

sql
VAULT app_secrets CREATE KEY 'pii';                           -- AES-256-GCM, the default
VAULT app_secrets CREATE KEY 'tokens' TYPE 'mldsa65';         -- ML-DSA-65 signing
VAULT app_secrets CREATE KEY 'hsm-aes' TYPE 'hsm-aes256-gcm'; -- generated inside the HSM
VAULT app_secrets CREATE KEY 'hsm-sig' TYPE 'hsm-ecdsa-p256'; -- generated inside the HSM
TypeOperations
aes256-gcmENCRYPT, DECRYPT. Also what an encrypted column is sealed with.
mldsa65SIGN, VERIFY, PUBLIC KEY.
hsm-aes256-gcmENCRYPT, DECRYPT, run on the token. Needs a pkcs11: vault. Can seal encrypted columns.
hsm-ecdsa-p256SIGN, VERIFY (of SHA-256 of the value), PUBLIC KEY, run on the token. Needs a pkcs11: vault.

A key's name has no :. A software key is a random 256-bit key wrapped in the keyring like any data key; an HSM key is generated on the token, cannot be extracted, and the keyring only records that it exists.

sql
VAULT app_secrets ENCRYPT KEY 'pii' VALUE 'call me at 555-0100';     -- base64 ciphertext
VAULT app_secrets DECRYPT KEY 'pii' VALUE '<base64 from ENCRYPT>';  -- the plaintext
VAULT app_secrets SIGN KEY 'tokens' VALUE 'order 1182 approved';    -- hex signature
VAULT app_secrets VERIFY KEY 'tokens' VALUE 'order 1182 approved' SIGNATURE '<hex>';  -- true or false
VAULT app_secrets PUBLIC KEY 'tokens';                              -- hex public key

Every use is audited. A key appears in VAULT … LIST as key:<name>, with its type. The secret statements (GET, DISABLE, ENABLE, DELETE, RECOVER, PURGE) do not apply to keys and are refused (42602).

Rotating the key-encryption key

sql
VAULT app_secrets ROTATE KEY;                                   -- rewrap under the same provider
VAULT app_secrets ROTATE KEY TO 'mlkem:/etc/rasterizeddb/app_secrets-2026.mlkem;create';
text
14 data key(s) rewrapped under mlkem:/etc/rasterizeddb/app_secrets-2026.mlkem (ML-KEM-768 + X25519)

Rotation unwraps every data key with the current provider and wraps it again with the new one, replacing the keyring in one atomic step. TO moves the vault to another provider (another HSM key, from a file to an HSM, to a new ML-KEM key pair). Keys generated inside an HSM stay where they are. Secret values and encrypted-column values are not re-encrypted: their data keys are only rewrapped. Rotation is audited. Keep the old provider's key until the rotation has returned.

The audit trail

sql
VAULT app_secrets AUDIT;
VAULT app_secrets AUDIT SECRET 'db/password';
text
chained   1795701600000000 SET app_secrets 'db/password' v1 by deploy
chained   1795701660000000 GET app_secrets 'db/password' v1 by app
chained   1795701700000000 GET app_secrets 'db/password' by intern REFUSED: it is disabled

Each line is one event (time in microseconds since 1970, what, which secret and version, which role), marked chained when the hash chain holds it or UNCHAINED when it does not: an UNCHAINED line was added to the audit file past the server. Reads and key uses are audited when they happen, whatever becomes of their transaction; changes (SET, DISABLE, ENABLE, DELETE, RECOVER, CREATE KEY) are audited when their transaction commits, and not at all if it rolls back. Values never appear in the audit.

Privileges

StatementNeeds
CREATE VAULTWhoever may create a table.
GET, GET ALL, LIST, VERSIONS, ENCRYPT, DECRYPT, SIGN, VERIFY, PUBLIC KEYSELECT on the vault.
SET, DISABLE, ENABLE, DELETE, RECOVERINSERT on the vault.
PURGE, ROTATE KEY, AUDIT, CREATE KEYThe vault's owner or a superuser.
sql
GRANT SELECT ON app_secrets TO app;              -- the application reads its secrets
GRANT SELECT, INSERT ON app_secrets TO deploy;   -- the deploy pipeline sets them

SELECT on a vault is also what a role needs to read and write encrypted columns sealed with its keys.

Files beside the chain

A vault's keyring, the vault audit log and the encrypted-column keys are kept in the database's chain directory (see Where the chain lives):

FileWhat it holds
vault-<vault id>.keysThe vault's wrapped data keys, and the provider that wrapped them. Without it no secret of the vault can be read, even with the provider's key.
vault-audit.logThe audit lines. Each is also chained.
encrypted_columnsEach encrypted column's data key, wrapped by its vault key.

Back up the chain directory with the data, and keep the provider's key (the HSM key, the ML-KEM file) backed up separately from both.

The Studio LEDGER page

Studio built with the enterprise features (rdb-studio-enterprise, see Installing) has a LEDGER tab for ledger tables and vaults. It builds the statements on this page and the ledger tables page and runs them as the logged-in role; the server decides what that role may do, and a refusal is shown as the answer. Destructive actions (placing or releasing a hold, raising a retention, acknowledging a rollback, deleting or purging a secret, rotating a key) ask for confirmation first.

  • Chain. Tiles for the chain's entries, head, anchors and location, the server's warnings, and VERIFY ALL, which shows VERIFY LEDGER's findings marked INTACT or PROBLEMS FOUND.
  • Ledger tables. A grid of the tables (rows, mode, retention, legal holds, own anchors), each with VERIFY, MANAGE and DATA. MANAGE raises the retention, places and releases holds, adds an anchor, and shows a row's history or receipt by its stamp. A form creates a ledger table, with versioned mode, a drop-idle time, an anchor and encrypted columns. AFTER A RESTORE runs ACKNOWLEDGE LEDGER ROLLBACK with your reason.
  • Encrypted columns. Every encrypted column, with its table, vault, key and type.
  • Vaults. Per vault: tiles for its secrets, keys, key provider and deleted-secret retention; a grid of secrets with REVEAL (the value shows in a marked box until HIDE, and never past a tab or vault switch), VERSIONS, DISABLE or ENABLE, DELETE or RECOVER, and PURGE; forms to set a secret, create and use keys, and rotate the key; the audit trail; and a form that creates a vault with any of the providers above.

On a server without the enterprise features, the tab says the server has no ledger tables or vaults. Elsewhere in Studio, an encrypted value the role cannot decrypt shows as "🔒 encrypted (no access to its vault)". See Studio for the rest of Studio.

From a Rust application

The enterprise Rust client crate, rasterizeddb_client_enterprise (in the repository, not on crates.io), wraps these statements: Vault::new(&client, "app_secrets") then set, get, get_all, list, rotate_key, audit, create_key, encrypt and the rest, with values returned as a string that is zeroed when dropped, and cached(ttl) for a short-lived cache.

Replication and backups

  • A vault replicates as a vault: a replica creates it from the same CREATE VAULT statement, without opening its key provider, and stores the same ciphertext. The keyring and audit log stay on the server that wrote them, so read secrets on the primary.
  • A backup carries the vault's ciphertext. Restoring it is useful only with the keyring of the same moment (see Files beside the chain) and the provider's key.

Limits

  • tpm: is not in the release builds; use a TPM through pkcs11: and tpm2-pkcs11.
  • A secret value is a quoted literal in the statement; it cannot be a bound parameter.
  • The value given to ENCRYPT, SIGN and VERIFY KEY is not cut from statement text the server keeps: only statements carrying the word SECRET are.
  • Values are text: a secret that is not valid UTF-8 cannot be read back.
  • A GET refused because the role lacks SELECT on the vault is not audited: the privilege check comes first.
  • Secrets are read on the server that holds the keyring, not on replicas.
  • A plain SELECT on a vault shows its metadata (names, versions, states, who set them) to any role with SELECT on it.
  • Once a vault is used, the server turns off core dumps and debugger attachment for its process (on Linux), and keeps keys and values in locked memory.
✓ page rendered in 234 µs axum + tera + htmx · no other javascript