Skip to main content
Version: 0.9.1

SQL Server backup keys

A SQL Server backup written WITH ENCRYPTION, or a backup of a TDE database, is read by mssql (the DuckDB extension) with the certificate's private key. With this service the private key never reaches a DuckDB: mssql sends the backup's wrapped content key (at most 512 bytes), the service unwraps it and answers the 44-byte PLAINTEXTKEYBLOB, the key of that one backup. Every unwrap needs use on the secret, and is rate-limited and audited (spec 039; the protocol's side is tresor's specs 048 and 050, capabilities.mint).

It needs tresor 0.2.0 or later in the DuckDB (computed parameters) and mssql with secret_compute (duckdb-ext-common 0.17.0); with an older tresor the secret is fetched as before and mssql reads no computed key.

DuckDB (mssql) --- POST /v1/secrets/restore/mint {param: private_key, operation: unwrap, input: <wrapped key>, context: {thumbprint}} ---> tresor-server
<-- {value: <PLAINTEXTKEYBLOB>, cache_ttl} --------------------------------------------------------- (the RSA key: read by the service, or in Key Vault)

The service is a store of secrets. It knows no secret type and checks no value of a consumer's secret by its content. The key is where a reference says, written like any secret, with no command or import of its own:

The computed param's valueThe private keyDecryptedSQL Server
mint+unwrap://key/<name>a key record of this service, tresor_unwrap_keyhere: OAEP, then PKCS#1 v1.52017-2025
mint+unwrap://ref+<source>://…a secret in your vault (Key Vault, OpenBao, AWS, GCP, Kubernetes), read at each minthere: OAEP, then PKCS#1 v1.52017-2025
mint+unwrap://azkv/<vault>/<key>a key in Azure Key Vault, never exportedin Key Vault: OAEP only2025; EKM 2019-2025

SQL Server 2025 wraps a backup's key with RSA-OAEP; 2017-2022 with PKCS#1 v1.5. Keys of 1024 to 4096 bits are taken: an older certificate's 1024-bit key is still the one its backups need.

The key as PEM​

The service reads a private key as PEM, PKCS#8 (PRIVATE KEY) or PKCS#1 (RSA PRIVATE KEY), unencrypted, or as base64 of the PKCS#8 or PKCS#1 DER. One value may hold several PEM blocks, one after the other. SQL Server writes a PVK (BACKUP CERTIFICATE … WITH PRIVATE KEY (FILE = …, ENCRYPTION BY PASSWORD = …)) or a PFX (FORMAT = 'PFX'). Convert it with OpenSSL on a trusted host:

# a PVK (OpenSSL 3: the legacy provider reads its RC4)
openssl rsa -provider legacy -provider default -inform PVK -passin file:pw -in backup.pvk -out backup.key.pem
# a PFX
openssl pkcs12 -in backup.pfx -passin file:pw -nocerts -nodes | openssl pkey -out backup.key.pem
# the certificate, base64 DER, for the secret's certificates
base64 < backup.cer | tr -d '\n'

The .pem is the key in the clear while it is on disk. Make it, write it to the service or the vault, and delete it.

A key record: tresor_unwrap_key​

A secret of the service's own type tresor_unwrap_key holds the private keys in its one param, keys: one VARCHAR - a PEM, several PEM blocks one after another, base64 DER, or a ref+ reference to one (tresor's DuckDB type sends this) - or a VARCHAR[] of such values. Like a service login (tresor_service_login), it is never served. It is not listed, a read of its material is 404, and a grant on it is refused (422). Only the service reads it, for the secrets that reference it. A replace never changes a secret's type into or out of tresor_unwrap_key (422): a granted secret never becomes a key record that keeps its grants, nor a key record a type that would be served.

An administrator writes it with an ordinary PUT, CREATE PERSISTENT SECRET … IN corp, or the console's editor:

CREATE PERSISTENT SECRET sql_backup_keys IN corp (
TYPE tresor_unwrap_key,
KEYS '-----BEGIN PRIVATE KEY-----
MIIG/QIBADANBgkqhkiG9w0BAQEFAASCBucwggbjAgEAAoIBgQ…
-----END PRIVATE KEY-----
-----BEGIN PRIVATE KEY-----
MIIE/gIBADANBgkqhkiG9w0BAQEFAASCBOgwggTkAgEAAoIBAQ…
-----END PRIVATE KEY-----' -- one value: the keys one after another (or KEYS 'ref+azkv://…')
);
curl -X PUT https://secrets.example.com/v1/secrets/sql_backup_keys -H "Authorization: Bearer $ADMIN" \
-H 'Content-Type: application/json' -H 'If-None-Match: *' --data-binary @- <<EOF
{"type": "tresor_unwrap_key", "params": {"keys": {"type": "VARCHAR[]", "value": [$(jq -Rs . < backup.key.pem)]}},
"redact_keys": ["keys"]}
EOF
  • keys is redacted whatever the write says. The console's shape names it and never shows it.
  • A key written inline is checked at the write: it parses, it is RSA, and it has 1024-4096 bits. Otherwise the write is 422 invalid_secret, naming the key by its place (key 2), never a byte. A ref+ value must be within its source's allowlist at the write. It is read, and so checked, at each mint: a value that is no key, or a key outside 1024-4096 bits, is then 500 service_error, with the reason in the service's log.
  • A key record holds keys only, and no operation.
  • CREATE PERSISTENT SECRET … KEYS '…' passes the PEM through the administrator's DuckDB once, as any secret they create. Where that is unwanted, write a ref+ reference (the key stays in your vault), or use the console or the PUT.

A key in your vault: mint+unwrap://ref+…​

The key may stay in a secret of your vault, read by the service at each mint with its own identity, under the same allowlist as any ref+ reference and never stored:

az keyvault secret set --vault-name corp-vault -n sql-backup-key --file backup.key.pem
bao kv put secret/sql/backup key_pem=@backup.key.pem
aws secretsmanager create-secret --name sql/backup-key --secret-string file://backup.key.pem
CREATE PERSISTENT SECRET restore_sales IN corp (
TYPE mssql_backup_key,
SCOPE 'az://backups/sales/', -- the backups' path prefix, as mssql matches it
private_key 'mint+unwrap://ref+azkv://corp-vault/sql-backup-key', -- or ref+vault://secret/sql/backup#key_pem, ref+aws://…
certificates ['MIIDazCCAlOgAwIBAgI…'],
thumbprints ['9C82C6069FDE173E437E9EDD304DC6F4111246EF']
);
  • The reference is checked at the write against the configuration: a source configured for its scheme, and within its allowlist. Its value is read at each mint, as a ref+ value at each fetch. A source that does not answer is 503 service_unavailable, and a value that is no key is 500. The parse is kept a minute in memory, by the reference and the value's hash, so a changed value is parsed again.
  • The value may hold several PEM blocks (several certificates' keys in one secret of the vault).

The secret mssql reads​

An administrator writes the secret that mssql reads, naming the key and the certificates it may unwrap with:

CREATE PERSISTENT SECRET restore_sales IN corp (
TYPE mssql_backup_key,
SCOPE 'az://backups/sales/', -- the backups' path prefix, as mssql matches it
private_key 'mint+unwrap://key/sql_backup_keys',
certificates ['MIIDazCCAlOgAwIBAgI…', 'MIIEbjCCAtagAwIBAgI…'], -- base64 DER
thumbprints ['9C82C6069FDE173E437E9EDD304DC6F4111246EF', '0F77E08D6880342520D92875966594232846F9B6']
);
GRANT … -- use, to the narrow role that restores backups
  • private_key is computed: a param of any name is, when its value (a string, or a VARCHAR) is the reference to its operation, mint+unwrap://…. The material serves it as stored, the reference and never a key; tresor computes it by a mint that names the param and the operation. The form is reserved: a VARCHAR beginning mint+<operation>:// (an operation is lower case, [a-z0-9_-]+; mint+Unwrap:// is no reference to unwrap) is a computed param, never a literal. So a write is refused (422 invalid_secret, naming the param) when a VARCHAR - a string or a typed VARCHAR - would be served so without being a reference this service computes: the generators' escape of any operation (mint++unwrap://…, mint++claim://…), or mint+<operation>://… for an operation it does not serve (mint+sign://…). A string inside a list or a map is not such a VARCHAR.

  • Parameter names are case-insensitive, as DuckDB's: two that differ only in case (private_key and Private_Key) are refused at the write (422 invalid_secret, naming both), and a mint names its param in any case. The operation it names is compared as written.

  • At the write the service checks the reference, and only that:

    • key/<name>: a tresor_unwrap_key of that name, on which the writer holds update (tresor specs/050). Otherwise anyone allowed to create a secret could compute with a key they cannot manage. The check is the policy's verbs - an administrator's, or those the policy lets a server pass on under a grant - not verbs of the record's own: a server acting for an administrator passes only when its policy gives it update.
    • ref+…: a source configured for it, within its allowlist.
    • azkv/…: the source and its keys' allowlist (below).
    • Another KMS's reference (mint+unwrap://aws/…, gcp/…, vault/…) is refused by this version ("not served by this version"), as is an operation in a service login or a key record.

    The secret's own values are stored as given: no certificate is matched against the key at the write, and one missing, unparsable or matching no key is the mint's one 422.

  • certificates (VARCHAR[] of base64 DER), or one certificate, is the allow-list: a mint uses only a key whose public key is one of these certificates'. A certificate that matches no key is that mint's one 422.

  • thumbprints are for mssql: it picks the secret by scope and checks the thumbprint in the material it fetches. The service chooses by the certificates' own thumbprints (the SHA-1 of their DER).

The mint​

POST /v1/secrets/{name}/mint:

{"param": "private_key", "operation": "unwrap", "input": "<base64>", "context": {"thumbprint": "9C82…"}}
  1. Who. The caller holds use on the secret, as for a read. Under a delegation grant, that is the server's own grants, and both identities are audited. A secret it cannot see is 404; one it sees with no use is 403 no_verb (under a grant actor_not_allowed). A param whose stored value is not mint+<operation>://… for the operation the request names (unwrap: the one this version serves) is 404; a request with no operation is the one 422. The record is judged as read with its material: deleted and written anew between the reads (as a key record, or without the caller's use), it is not served. The key record needs no grant: the service reads it itself.
  2. Rate. A token bucket per caller and secret. Under a delegation grant it is the server's, which all the users it acts for share (K users are not K bursts). A second bucket per key and certificate collects every caller's mints. Each has mint.burst, mint.per_second (64 and 8/s by default, a striped backup's files at once). Past either: 429 rate_limited with Retry-After. The buckets are in each replica's memory, so the limits are per replica. The key's bucket is shared by every caller, so one holder of use can drain it for all: a limit of the key, by design. Which certificates a key record lacks shows in a failure's time (no decryption is tried for them): that is the configuration's, not a secret.
  3. The certificate. context.thumbprint (any case) chooses one of the secret's certificates. With none, each certificate whose key has the input's size is tried in turn.
  4. The unwrap. The input is the wrapped key as the backup stores it (CryptoAPI's little-endian): base64 (standard, padded), at most 512 bytes, the key's modulus in size, then reversed. With a key the service reads it is decrypted with RSA-OAEP (SHA-256 for the hash and MGF1, no label), then PKCS#1 v1.5 when OAEP does not give a blob. v1.5 uses implicit rejection: rsa.DecryptPKCS1v15SessionKey into 44 fresh random bytes, which a bad padding leaves as they are, so a padding failure is a blob that does not check, the same as any other. Every failure costs both paddings for every key tried. context.padding (oaep-sha256 or pkcs1v15) may restrict it to one step. A key in Key Vault is OAEP only: pkcs1v15 asked of it is refused with no call. What is decrypted is checked as a PLAINTEXTKEYBLOB: bType 0x08, bVersion 0x02, aiKeyAlg AES-128/192/256 (0x660E/0x660F/0x6610) or 3DES (0x6603), dwKeySize the algorithm's, zero-padded to 44 bytes.
  5. One error. Whatever does not compute is the same 422 invalid_input, with one fixed detail: the base64, the size, an unknown thumbprint or padding, a certificate no key holds, either padding, the blob's shape. The step is in the service's log only, never a byte. A key record that is gone or no key record, or a reference whose value is no key, is 500 service_error (an operator's to look at). A store or a source that does not answer is 503. A key in Key Vault adds the vault's answers (its table).
  6. The answer. 200 {"value": "<base64 PLAINTEXTKEYBLOB>", "cache_ttl": 300}, with Cache-Control: no-store. tresor keeps the value in memory for at most mint.cache_ttl (5 minutes by default; 0: none), for the same caller, secret version, input and context; a revoked grant takes effect within it.

The audit​

Each mint is an event of kind mint: the secret, the caller (principal, and actor under a grant), param (by the secret's own name for it), operation (the request's, as sent when it is spelled as one; else (not an operation)), thumbprint (the certificate used), bits (its key's size, so an operator sees 1024-bit keys), padding (the one that served: oaep-sha256 or pkcs1v15), input (sha256:<hex> of the input), context, and the outcome: ok, invalid_input, rate_limited, denied (no use), refused (Key Vault refused the service), not_found (a secret it cannot see, a key record, a param or an operation the secret does not compute), or error (500, 503). Every mint has one. It never holds the key, the input or the value. The thumbprints show which certificates are still read; tresor.mint.operations counts the mints by outcome.

In Azure Key Vault​

For SQL Server 2025 (OAEP), the key may stay in Azure Key Vault as a key, never exported: the service calls the vault's unwrapKey (RSA-OAEP-256) for each mint, with its own identity, and reads nothing but the key's public parts. (A key SQL Server Connector keeps there for EKM is below.)

CREATE PERSISTENT SECRET restore_sales IN corp (
TYPE mssql_backup_key,
SCOPE 'az://backups/sales/', -- the backups' path prefix, as mssql matches it
private_key 'mint+unwrap://azkv/corp-vault/sql-backup', -- the key, every version of it: no version here
certificates ['MIIDazCCAlOgAwIBAgI…', 'MIIEbjCCAtagAwIBAgI…'],
thumbprints ['9C82C6069FDE173E437E9EDD304DC6F4111246EF', '0F77E08D6880342520D92875966594232846F9B6']
);
  • The reference is mint+unwrap://azkv/<vault>/<key>, the key with no version: the secret's certificates choose the versions that serve it. A reference with a version (…/<key>/<32 hex>) is refused at the write ("a key, all its versions: no version in the reference").

  • Where it may point: the vault and the key are checked against the same source as ref+azkv:// (material.azkv.allow, the same azure.identity), but against a list of their own: an entry's keys, the prefixes of the keys an unwrap may use. An entry without keys admits no key, so reading a vault's secrets never implies decrypting with its keys.

    material:
    azkv:
    allow:
    - vault: corp-vault
    prefixes: [lake-] # ref+azkv:// secrets
    keys: [sql-backup] # mint+unwrap://azkv/ keys

    A named source of kind azkv serves mint+unwrap://<its name>/<vault>/<key> with its own credential, its allow[].keys the same. Outside the allowlist the reference is refused at the write, and never called at a mint.

  • Never the service's own key: a reference to its KEK (keys.key, and every keys.previous) or to a key it signs its client assertions with (client_auth: keyvault) is refused at the write and at every use, whatever the allowlist says, any version of it. Keep the backup keys apart from the service's anyway: another vault, or at least other names.

  • The versions. The vault is not called at the write. At a mint the service lists the key's versions and reads each enabled one's public key (an RSA or RSA-HSM key allowed unwrapKey or decrypt). A version serves the secret only when its public key is one of the secret's certificates' (never "the latest"), and only within its dates (not before, expires: checked at each use). The list is kept for 5 minutes. A certificate with no version in it reads it again (a version imported since), at most once every 30 seconds per key whatever the mints (so a version imported in a rotation serves within 30 seconds; nothing reads the vault at a write), one read at a time; a read that failed is answered so for 5 seconds. A certificate no version holds is the mint's 422.

Importing the key​

Import the certificate's private key into Key Vault with its own tools, not exportable, allowed unwrapKey (and decrypt):

# a PEM (converted from the PVK or PFX as above), or a BYOK file from an HSM
az keyvault key import --vault-name corp-vault -n sql-backup --pem-file backup.key.pem --ops unwrapKey decrypt
az keyvault key import --vault-name corp-vault -n sql-backup --byok-file backup.byok --ops unwrapKey decrypt

# or the PFX as a Key Vault certificate: its backing key, of the same name, is the key to reference
az keyvault certificate import --vault-name corp-vault -n sql-backup -f backup.pfx --password "$(cat pw)"
  • The key's name: not backup, which Key Vault refuses (a word of its own API); sql-backup is fine.

  • A Key Vault certificate has a backing key of the same name; mint+unwrap://azkv/corp-vault/sql-backup is that key, its versions the certificate's versions. Import it with a policy whose key is not exportable (exportable: false); the certificate's secret (the PFX) is then not readable either.

  • Key Vault holds no RSA key under 2048 bits: a 1024-bit certificate's key is a key record or a vault secret.

  • The service's identity needs, on the key (or the vault), the data actions Microsoft.KeyVault/vaults/keys/read (list the versions, read their public keys), Microsoft.KeyVault/vaults/keys/unwrap/action and Microsoft.KeyVault/vaults/keys/decrypt/action (for a version allowed decrypt only). The built-in Key Vault Crypto User has them and more (encrypt, wrap, sign, verify). A custom role with exactly these three is the narrow one:

    az role definition create --role-definition '{"Name": "tresor unwrap", "Actions": [],
    "DataActions": ["Microsoft.KeyVault/vaults/keys/read", "Microsoft.KeyVault/vaults/keys/unwrap/action",
    "Microsoft.KeyVault/vaults/keys/decrypt/action"],
    "AssignableScopes": ["/subscriptions/<subscription>"]}'

    It needs no access to secrets or certificates.

  • No rotation policy on such a key: Key Vault's rotation makes new key material no certificate holds, a version that would serve no secret. Rotation is SQL Server's: below.

The mint's answers​

The certificate is chosen before the vault is called. An unknown thumbprint, an input not the certificate's key size, or pkcs1v15 asked are refused at once, with no call. Then the version's rate bucket is taken, its version found (the cached list, or one read on a miss), and one unwrapKey of it made, whose answer is checked as a PLAINTEXTKEYBLOB.

The vaultThe mint
400 BadParameter - the input does not decrypt with the versionthe one 422 invalid_input
another 400 - a request it cannot take500 service_error; the status and code in the log
401 / 403 - it refuses the service (its role); a credential refused or not usable here403 mint_refused, audited refused; the reason in the log; the key's list read again at the next mint
403 KeyDisabled - a version disabled since the list was read; 404 - the key or the version is gone500 service_error (an operator's); the list read again at the next mint
429, 5xx, no answer in 15 s503 service_unavailable, with the vault's Retry-After when it gave one

EKM: an asymmetric key in Key Vault​

A backup WITH ENCRYPTION (… SERVER ASYMMETRIC KEY = k), or a TDE database whose protector is such a key, has its key wrapped by an ASYMMETRIC KEY that SQL Server Connector keeps in Azure Key Vault (EKM). The service unwraps it in Key Vault too, and the key never leaves it. Such a key has no certificate: the secret names it by the thumbprint SQL Server shows for it (SELECT thumbprint FROM sys.asymmetric_keys), which is not derived from the public key.

CREATE PERSISTENT SECRET restore_ekm IN corp (
TYPE mssql_backup_key,
SCOPE 'az://backups/ekm/',
private_key 'mint+unwrap://azkv/corp-vault/ekm-rsa2048', -- the Connector's key: every version
thumbprints ['33F0C71AEB8DA5C8BC7716E8C28616109DB930C8'] -- as sys.asymmetric_keys shows it; no certificates
);
  • A secret with thumbprints (or thumbprint) and no certificates is an EKM key's. The thumbprint is the mint's context.thumbprint (any case) among the secret's, or its one; with several and none in the context, the one 422.
  • The wrapped key is decrypted as the backup stores it (not reversed), with Key Vault's RSA-OAEP (SHA-1), which is the Connector's padding. The result is the same 44-byte PLAINTEXTKEYBLOB, checked the same way. A context.padding is the one 422.
  • The key's enabled versions of the input's size are tried: the one that answered last for that thumbprint first, then the newest, at most five. The blob's check tells the right one. Every call to the vault costs one token of the key's bucket, so a failing mint costs as many tokens as versions it tried. The allowlist (allow[].keys), the role and the vault's answers are a certificate key's (above).

Rotation​

A SQL Server certificate's key never changes: rotating is a new certificate, which encrypts new backups while the old ones still need the old key. So:

  1. Add the new key to the key record's keys (or the new PEM block to the vault's secret).
  2. Add the new certificate (and its thumbprint) to the secret's certificates (and thumbprints).
  3. Once no backup you keep needs the old certificate (the mints' audit, by thumbprint, shows which are still read), remove it from the secret, then its key.

In Key Vault the same: import the new certificate's key as a new version of the same key (az keyvault key import under the same name, or the new PFX as a new version of the certificate) and add the certificate to the secret. To retire the old one, first remove its certificate from the secret (no mint asks for it then), and only then disable or delete its version in the vault. Never a rotation policy on the key: it makes versions no certificate holds.

tresor-server refs -resolve (or the console's references check) lists every secret whose mint+unwrap:// does not serve: no such key record, a reference outside the allowlist, a key that does not read, a certificate no key holds.

Mode B: the key delivered to mssql​

A secret may instead hold the certificate and its private key as ordinary params (certificate, private_key, password, the last two redacted, each a value or a ref+ reference): mssql then unwraps locally. The service needs nothing for it. Mode A (a computed param, above) is the stronger one: in mode B the key reaches the DuckDB process's memory.

Security​

  • The mint is a decryption oracle for whoever holds use: grant the secret to the narrow role that restores backups only. The rate limit and the audit bound and record it. use on a certificate's secret unwraps whatever that certificate wraps in this format: a certificate shared across purposes shares its use too.
  • PKCS#1 v1.5 only with implicit rejection, and only where the service performs the RSA operation; one error, the same cost for every failure; nothing decrypted echoed.
  • A key record is never served and never granted, and only a writer who holds update on it can reference it. Its keys are sealed under the KEK like every value at rest (Encryption); a backup of the store holds them sealed. A key in Key Vault never leaves it.
  • Fail closed: a key that does not read is an error, never a fallback.
  • A ref+ key is within its source's allowlist, and the service's own secrets are outside every allowlist (the configuration refuses a state.password_ref an allowlist admits); its signing keys are files or KMS keys, which mint+unwrap://azkv/ refuses (above).
  • The service refuses to start under GODEBUG=fips140=only: FIPS 140-3 mode refuses PKCS#1 v1.5 decryption, RSA keys under 2048 bits and RSA-OAEP with SHA-1, which the unwrap needs.

Who may write a reference. A secret holding a reference (ref+…) or a mint+… value is written only by an administrator: create, update, delete and grant are administrators' verbs, and a grant gives only use. So an administrator of this service is trusted with whatever the service may read from its sources. Keep the allowlists and the rights of the service's identity in each vault narrow.