> For the complete documentation index, see [llms.txt](https://wiki.datadike.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.datadike.com/pam/administration/pgp-keys/custody.md). Agents can also search and read this documentation through its [MCP server](https://wiki.datadike.com/~gitbook/mcp).

# Discover and keep PGP keys in custody

Find the PGP keys kept in the GnuPG keyrings of your Linux servers, keep the private keys encrypted in the vault, and generate or import new keys.

## What this is and when to use it

PGP keys sign and encrypt files exchanged between systems: backups, bank and payroll files, reports sent to partners. They usually live in the GnuPG keyring of a service user on a server, protected only by that server's file permissions, and often nobody knows when they expire.

**Accounts › PGP keys** brings these keys under management:

* A **discovery plan** reads the GnuPG keyrings of every user on the selected Linux assets and lists the keys it finds.
* **Custody** keeps the private key, and its passphrase, encrypted in the vault. A key in custody can be rotated, reinstalled on the server and exported under audit.
* Keys can also be **generated** by the solution or **imported** from a key block.

Every key in the inventory is checked daily for expiration, with alerts in the same way as certificates.

## Before you start

* A role with permission to manage PGP keys. Exporting a private key is a separate permission, so that seeing the inventory does not imply reading the keys.
* Linux assets reachable over SSH, with GnuPG (`gpg`) installed.
* A privileged account on each asset, registered under **Accounts › Accounts**, that can read the home directories of the other users: `root`, or an account with sudo. Discovery runs the read as the owner of each keyring, so the keyrings and their permissions are not changed.

## Steps

### Discover the keys on your servers

1. Open **Accounts › PGP keys** and the **Discovery** tab.
2. Click **New discovery plan**.
3. Choose the **Assets** or **Nodes** to scan.
4. Turn on **Use sudo** if the asset's privileged account is not `root`.
5. Under **Schedule**, turn on **Periodic run** to repeat the scan, or leave it off.
6. Save, then click **Run now**.

The plan reads every `~/.gnupg` keyring on the asset. Each key found goes into the inventory with its location: the asset, the **Keyring owner** and the keyring path. Keys whose private part is on the server are marked **Private key on the asset**.

### Take a private key into custody

1. In the **PGP keys** tab, open a key whose private key is on the asset.
2. Click **Collect private key**.
3. Enter the key passphrase, if it has one, and confirm.

The solution exports the private key from the keyring, checks that the passphrase opens it, and stores both encrypted. The key on the server is not removed or changed.

### Generate a new key

1. In the **PGP keys** tab, click **Generate key**.
2. Fill in the **User IDs**, one per line, in the form `Name <address@example.com>`.
3. Choose the **Algorithm** and the **Validity of new subkeys**.
4. Turn on **Protect with a passphrase** for keys used by people. The solution generates the passphrase and keeps it in the vault. Leave it off for keys used by unattended jobs, which cannot type a passphrase.
5. To place the key on a server, fill in **Location on the asset**: the **Asset**, the **Keyring owner** (the Linux user who will use the key) and the **Access account**. Keep **Install on the asset now** on.
6. Save.

### Import an existing key

1. Click **Import key**.
2. Paste the **Key block**, in ASCII armor.
   * A **private** key block is taken into custody. Enter its **Key passphrase**, if it has one.
   * A **public** key block is only monitored for expiration: a partner's key you encrypt files to, for example.
3. Optionally fill in **Location on the asset** to install it on a server.
4. Save.

### Export a private key

1. Open the key and click **Export private key**.
2. Confirm with **Show private key**.

The key block and the passphrase are shown, and can be copied or downloaded. Every export is recorded in the audit trail as **Private key exported**, with the user and the IP address.

## What you should see

The inventory shows one card per key with its status, expiration date and custody:

| Indication                                           | Meaning                                                          |
| ---------------------------------------------------- | ---------------------------------------------------------------- |
| **In custody**                                       | The private key is in the vault                                  |
| **Protected by passphrase**                          | The key has a passphrase, also kept in the vault                 |
| **Vault only**                                       | The key is not installed on any asset                            |
| **Valid** / **Expiring** / **Expired** / **Revoked** | Status of the key. **Expiring** starts 30 days before expiration |

The key details list the **User IDs**, the **Fingerprint**, the **Subkeys** with their usage (**Signing**, **Encryption**, **Authentication**, **Certification**) and expiration dates, and the location on the asset. **Download public key** gives the public block to send to partners.

## How to verify it worked

1. On the server, as the keyring owner, list the key and compare the fingerprint and expiration dates with the ones the solution shows:

   ```bash
   gpg --list-secret-keys --with-subkey-fingerprints
   ```
2. For a key in custody, the **In custody** indication must be on, and **Audit › Console › PGP keys** must show **Private key in custody**, or **Key generated** or **Key imported**.
3. For a generated key installed on an asset, sign a test file on the server as the keyring owner:

   ```bash
   echo test | gpg --clearsign
   ```

## Changes made outside the solution

Every discovery run compares the keyrings with the inventory and records what changed on the server without going through the solution:

* **Key changed on the asset**: new subkeys or new expiration dates.
* **Key no longer on the asset**: the key was removed from the keyring. The key card shows that it was not found in the last discovery. A key in custody can be put back with **Install on the asset**.

## Expiration alerts

The daily check sends an expiration alert when a key crosses **30, 15 and 7 days** before expiration, and on the day it expires. The alerts are notifications of the **PGP key lifecycle** type, in the **PGP keys** category. Set the recipients under **System settings › Notifications › Subscription**.

## Common errors

| Symptom                                               | Cause                                                                                            | Fix                                                                     |
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------- |
| The plan lists the asset under **Assets with errors** | GnuPG is not installed, or the account cannot read the other users' home directories             | Install `gnupg` on the asset, or turn on **Use sudo** in the plan       |
| **Collect private key** refused: wrong passphrase     | The passphrase does not open the key                                                             | Enter the passphrase of that key. Nothing is stored when it fails       |
| **Collect private key** is not offered                | The private key is not on the asset, only the public key                                         | Import the private key from wherever it is kept, or generate a new key  |
| Installing the key fails                              | The **Keyring owner** does not exist on the asset, or the access account cannot act as that user | Check the Linux user name, and use a privileged account or **Use sudo** |
| A key shows **Vault only**                            | No location on an asset was set                                                                  | Edit the key and fill in **Location on the asset** to install it        |

## Field reference

| Field                     | What it does                                                               | Accepted values                          | Default                        |
| ------------------------- | -------------------------------------------------------------------------- | ---------------------------------------- | ------------------------------ |
| User IDs                  | Identities of a generated key                                              | Up to 10, `Name <address>`               | —                              |
| Algorithm                 | Algorithm of a generated key                                               | Ed25519 / Curve25519, RSA 3072, RSA 4096 | Ed25519 / Curve25519           |
| Validity of new subkeys   | Validity of the key generated, and of the subkeys created at each rotation | Days                                     | 365                            |
| Protect with a passphrase | The solution generates a passphrase and keeps it in the vault              | On, off                                  | Off                            |
| Key block                 | Key to import, in ASCII armor. Private or public                           | —                                        | —                              |
| Key passphrase            | Passphrase of the key being imported or collected                          | —                                        | Empty                          |
| Asset                     | Server where the key is installed                                          | A Linux asset                            | Empty                          |
| Keyring owner             | Linux user whose keyring holds the key                                     | User name                                | —                              |
| Access account            | Account used to connect to the asset                                       | An account of the asset                  | The asset's privileged account |
| Use sudo                  | Uses sudo to act as the keyring owner                                      | On, off                                  | Off                            |
| Install on the asset now  | Installs the key when it is saved                                          | On, off                                  | On                             |
| Monitored                 | Includes the key in the daily check and alerts                             | On, off                                  | On                             |

## Related pages

* [Rotate PGP subkeys](https://wiki.datadike.com/pam/administration/pgp-keys/rotation.md)
* [Certificate and PGP key audit](https://wiki.datadike.com/pam/administration/audit/certificate-and-key-audit.md)
* [The credential vault](https://wiki.datadike.com/pam/administration/accounts/vault.md)
* [Managed accounts](https://wiki.datadike.com/pam/administration/accounts/managed-accounts.md)
