Files
unshackle/docs/guide/vaults.md
T

18 KiB

Key Vaults

A key vault is a store of DRM content keys that unshackle remembers between downloads. Once unshackle has recovered a title's keys, it saves them to your configured vaults. The next time you download something that reuses those same keys, unshackle finds them in a vault instead of performing another licence exchange. The download starts faster and puts less load on the service.

Vaults are entirely optional, but if you download regularly they are one of the biggest quality-of-life improvements you can configure.

!!! tip "Vaults save licence calls even on the very first download" The benefit is not limited to repeat downloads. Within a single run, a service often issues the same Key ID and content key for both the video and audio tracks. The same pair frequently covers several resolutions or bitrates of the same title. Once unshackle has recovered a key it writes it to your vaults immediately. The moment the next track presents a KID it has already seen, the key comes from the vault instead of another licence exchange. A single fetched key can therefore satisfy many tracks in one run.

What a vault stores

A vault stores every content key as a KID → KEY pair, grouped by service:

  • KID (Key ID): a 16-byte identifier for a specific encrypted track, stored as 32 lowercase hex characters with no dashes.
  • KEY: the 16-byte content key that decrypts that track, also 32 hex characters.
  • Service: the canonical service tag (for example EXAMPLE, not ExampleService) that namespaces the key. Each service gets its own logical table.

!!! note "Why keys are matched by KID" Vaults match keys by KID, never by PSSH/CENC header. A KID does not change unless the underlying video file itself changes, whereas the PSSH box can vary between requests for the very same content. That makes the KID a far more reliable match key.

A null content key (empty or all zeroes) means "no key": a lookup skips it and no vault stores it.

How lookups and caching work

When unshackle needs a content key during a download, it asks each local SQLite vault first, one at a time in the order of your config. Then it asks the other vaults at the same time, and the first content key that a vault returns wins. unshackle skips a vault that raises a permission error or is unreachable. unshackle does not ask a remote vault again for the rest of the run when it is unreachable or answers with a body that is not a JSON object. It always asks a local SQLite vault. Each failure of a vault shows one warning with the host and the reason; the same failure again goes to the debug log.

A track needs no licence when a vault holds the content key of the KID that encrypts it. The DRM header of the track can name more KIDs, for example those of other tracks. Before the decrypt, unshackle reads the downloaded file, and licenses each of those KIDs that the file uses and no vault holds. When unshackle cannot read which KIDs the file uses, it licenses all of them.

When unshackle recovers new keys from a licence, it pushes them to all configured vaults, so every vault gradually converges on the same set of keys. Two rules apply to pushing:

  • unshackle skips keys that already exist in a vault, and never overwrites them. Only a failed decode check removes a pair (see below).
  • A vault marked no_push: true receives lookups, but unshackle never writes to it. This is useful for read-only or shared upstream vaults you do not own.

unshackle pushes keys from every source, not only a local CDM. A run against a remote server that licenses with its own CDM (--remote) writes the keys the server returns, and unshackle import writes the keys it reads from an export file.

Wrong keys from a poisoned vault

A public vault can return the right KID with a wrong content key. The decrypters accept a wrong content key and write noise. unshackle therefore trusts a content key from a vault only after FFmpeg decodes the decrypted track with strict error detection.

For a fragmented MP4, unshackle reads from the encrypted file which fragments each KID encrypts. FFmpeg then decodes up to three windows of each KID: from its first, middle and last fragment on through the next fragments of that KID, for up to 4 seconds. A window never runs into a fragment of another KID or a clear fragment. A clear lead or a lead under another KID therefore cannot hide a wrong content key, and only the KID that fails is flagged.

When every KID in the map has a content key, the map is complete, and a KID that is not in it does not encrypt this file: the file of its own track judges it. A file with no encrypted sample entry and no encrypted fragment, for example a clear audio track, judges no KID, so it neither flags nor copies a content key. A KID that is not in an incomplete map, for example one with an all-zero KID, and every KID of any other file, gets one check of the whole file: FFmpeg decodes the first seconds and the last seconds, and a failure there flags each of those KIDs. When a mapped KID fails, the KIDs that are not in the map get no verdict from that pass; the retry with the replaced key judges them.

Content keys from a licence skip this check, with two exceptions: a FairPlay-only track that the FairPlay bridge licensed, and a track of a DRM plugin that licenses locally. A plugin track gets the check of the whole file after the merge of its decrypted segments. An import also checks these content keys when it reads them from an export file. When one of these fails, unshackle flags the pair, stops the track and keeps no output file.

A file this FFmpeg build cannot judge counts as a pass, never as a wrong content key: a codec it has no decoder for, or a header it cannot read. A plugin that decrypts each segment can fix the merged file before the check. A pass is not proof. A wrong content key for HEVC video can decode without an error for some seconds, most often at a low resolution, and that content key passes the check. Damage to a small part of the slice data, such as one corrupt NAL unit, also often decodes without an error. Without FFmpeg there is no check at all.

When the decode fails, unshackle:

  1. Flags the KID:KEY in the bad_keys table of every local SQLite vault, with the service and the name of the vault it came from. The pair also goes out of the service table, except in a vault marked no_push, where only the flag is written. A flagged pair cannot go back into a local vault from another vault, a later run, or kv copy. A licence that returns the pair clears the flag and stores it. An API vault that served the pair gets a report only when a different content key for that KID is accepted. When the CDM returns the same content key, that vault gets no report, and an API vault with no_push gets none. HTTP and MySQL vaults have no report route. A pair from a local vault goes to no remote vault, even when a remote vault holds the same pair.
  2. Restores the ciphertext from a hard link it made before decryption, then asks for the content key again. Every vault lookup skips a flagged pair, so the request reaches the remaining vaults and then the CDM. A remote answer that arrived after the winning one is reused instead of queried again.
  3. Removes the flag when the CDM returns the same content key, because then the decode check was wrong, not the content key. A content key from another vault is checked in its turn, and flagged as well if it also fails.

The download stops with an error instead of writing noise when no ciphertext is left to retry with, or when every vault and the CDM are used up. Without a local SQLite vault nothing remembers a flag, so the same vault serves the same pair again and the download stops there. A serve instance with server_cdm flags a pair the same way when a remote client reports it, so a server needs a SQLite vault in its key_vaults to remember a bad content key.

Per-segment decryption gives up the retry. With decrypt_segments on, and always with a plugin that decrypts each segment, the plaintext replaces each segment as it arrives, so no ciphertext is left. A failed decode then flags the pair and stops the download. The next run skips the flagged pair.

unshackle copies a content key from one vault to the others only after this check passes, so a content key that fails the check does not spread. Without FFmpeg, a content key from a vault is not copied at all. To see or clear the flags, open the SQLite file and read or delete rows in bad_keys.

!!! tip "A good two-vault setup" A common arrangement is a fast local SQLite vault plus a shared remote vault (MySQL, HTTP, or API). The local vault answers instantly and works offline; the remote vault lets you share keys across machines or with a group.

Configuring vaults

Declare vaults under the key_vaults list in your unshackle config file. Each entry needs a type (which backend to use) and a name (a label you choose), plus the settings of that backend. The fields of each backend, their defaults and vault_timeout are in the key vault reference.

key_vaults:
  - type: SQLite
    name: local
    path: ~/.local/share/unshackle/vaults/local.db
  - type: MySQL
    name: shared
    host: vault.example.com
    database: unshackle
    username: reader
    password: "s3cret"

The four backends that ship with unshackle are SQLite, MySQL, HTTP, and API. Write the type with this exact casing. The backend table also gives what a vault that fails to load does to a download.


SQLite backend

A local SQLite database file. This is the simplest and fastest option and needs no server. See the SQLite fields.

key_vaults:
  - type: SQLite
    name: local
    path: ~/.local/share/unshackle/vaults/local.db

!!! warning "Moving the .db file silently forks your vault" Because the file is created on demand, if you relocate an existing .db without updating path to match, unshackle will create a fresh empty database at the old location. That leaves you with two divergent vaults and no error to warn you. Always update the config path whenever you move the file.

!!! tip "Keep a local SQLite vault even when you share a MySQL one" The SQLite vault is best thought of as an offline-only backup should anything ever happen to a shared MySQL vault. Each member of a group should keep their own local SQLite vault alongside the shared database, so no one loses their keys if the shared vault goes down or away.


MySQL backend

A MySQL or MariaDB database on a server, to share keys across several machines. It connects through pymysql. Prefer MariaDB over MySQL where you have the choice. See the MySQL fields.

!!! warning "The vault connects directly to the database server" A MySQL vault (like SQLite) speaks the database wire protocol directly to the host or IP you configure. It cannot sit behind a PHP API or any other application layer. A common real-world snag: many hosting providers firewall their MySQL server to their own internal network. A database on shared hosting is therefore frequently unreachable from the outside world even when your credentials are perfectly correct. If connections time out, confirm the server accepts remote connections from your address before suspecting the config.

key_vaults:
  - type: MySQL
    name: shared
    host: vault.example.com
    database: unshackle
    username: unshackle
    password: "your-password"
    port: 3306

!!! warning "Account security is on you, not on unshackle" unshackle enforces none of this. It is operational policy for a database that may be shared with a group. Never connect with the root account, not even for yourself. Give every account a password, and never let two users share a username/password. Restrict each account's access to only the unshackle database, and grant normal users only SELECT and INSERT. Reserve the CREATE grant for a single trusted user who bootstraps the service tables (see kv prepare); everyone else can insert new keys but should not be creating tables.


HTTP backend

An HTTP vault that speaks one of three protocols, selected with api_mode. Use this to connect a community or self-hosted vault service. The fields and the three protocols are in the HTTP reference.

=== "query mode"

```yaml title="unshackle.yaml"
key_vaults:
  - type: HTTP
    name: community
    host: https://vault.example.com/api
    api_mode: query
    username: myuser
    password: "your-password"
```

=== "json mode"

```yaml title="unshackle.yaml"
key_vaults:
  - type: HTTP
    name: jsonvault
    host: https://vault.example.com/rpc
    api_mode: json
    password: "your-api-token"
```

=== "decrypt_labs mode"

```yaml title="unshackle.yaml"
key_vaults:
  - type: HTTP
    name: decrypt_labs
    host: https://api.decryptlabs.example/
    api_mode: decrypt_labs
    api_key: "your-decrypt-labs-key"
```

API backend

An HTTP vault with unshackle's own protocol and a bearer token. An API of another vault project does not work with it. See the API reference.

key_vaults:
  - type: API
    name: myapi
    uri: https://vault.example.com/api
    token: "your-bearer-token"
    headers:
      CF-Access-Client-Id: "CLIENT_ID_HERE"
      CF-Access-Client-Secret: "CLIENT_SECRET_HERE"

Managing vaults with unshackle kv

The kv command group manages the contents of your configured vaults. Every subcommand refers to vaults by the name you gave them in key_vaults; an unknown name is an error.

$ unshackle kv --help

kv add: import keys from a file

Add KID:KEY pairs to one or more vaults for a given service. The file must contain one pair per line as HEX:HEX (32 hex characters each); lines that do not match are ignored.

$ unshackle kv add keys.txt EXAMPLE local shared
9a04f07998404286ab92e65be0885f95:b1c2d3e4f5a6978012345678deadbeef
0e7cc1a4e2f9411db0f24a3fbb8f8b21:00112233445566778899aabbccddeeff

The service argument (EXAMPLE above) is normalized to its canonical tag automatically. Keys that already exist in a vault are reported as skipped.

If a vault answers and refuses the content keys, kv add shows one warning and continues with the next vault. If a vault is unreachable or still rate limited, kv add stops with an error.

kv search: find a content key by KID

Search your vaults for a KID and print any matching content key.

$ unshackle kv search 9a04f07998404286ab92e65be0885f95

The KID must be 32 hex characters (dashes are stripped for you). By default the search scans every configured vault and every service table; narrow it with options:

Option Description
-s, --service Limit the search to a single service tag.
-v, --vault Limit the search to one configured vault by name.

!!! note Remote vaults that cannot enumerate their service tables (for example an HTTP vault in json or decrypt_labs mode) can only be searched when you also pass --service. Without it, they are skipped.

kv copy: copy keys between vaults

Copy every content key from one or more source vaults into a single destination vault. kv copy never alters existing rows; it adds only genuinely new KIDs and skips null keys.

$ unshackle kv copy shared local

The first argument is the destination; the rest are sources. You can supply several sources at once:

$ unshackle kv copy local shared community
Option Description
-s, --service Only copy the given service.
-l, --local-only Only copy services you have installed locally, skipping tables for unknown services.

--service and --local-only are mutually exclusive.

To import an SQLite vault file that is not in the config, give its path as the source:

$ unshackle kv copy local ~/Downloads/key_store.db -s EXAMPLE

kv copy only reads from a source, so the file stays unchanged. kv sync also accepts a path, but it writes content keys into that file.

With --service, kv copy reads the table named by the service's vault tag and the table named by its plain tag. It writes both into the vault-tag table, which is the table a download reads. Without --service, each table keeps its name.

If the destination vault answers and refuses the content keys of a service, kv copy shows one warning and continues with the next service. If the destination vault is unreachable or still rate limited, kv copy stops with an error.

A pair the destination vault has flagged in bad_keys is left behind, so a copy does not undo a flag. bad_keys is never treated as a service, so its rows are never copied into a service table.

kv sync: mirror vaults both ways

Make each of two or more vaults hold every content key the others have. This is effectively a two-way copy between each pair, so afterwards all listed vaults hold the same set of keys.

$ unshackle kv sync local shared

It accepts the same --service and --local-only options as copy, and requires at least two vaults. It runs copy between each pair, so it skips flagged pairs and the bad_keys table in the same way.

kv prepare: pre-create service tables

Create the per-service tables on table-based vaults (SQLite, MySQL) ahead of time, for every service you have installed. Vaults that do not use tables (the network-backed HTTP and API backends) are skipped.

$ unshackle kv prepare shared

This is mainly useful with a MySQL vault where you want the tables created once by an account that has the CREATE grant.


See also

  • Downloading: how keys are recovered and used during a download.