> 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/rotation.md). Agents can also search and read this documentation through its [MCP server](https://wiki.datadike.com/~gitbook/mcp).

# Rotate PGP subkeys

Renew the signing and encryption subkeys of a PGP key on demand or before it expires, keeping the same fingerprint so partners do not need a new key.

## What this is and when to use it

A PGP key is made of a **primary key**, which identifies it through its fingerprint, and **subkeys** that do the daily work: one for signing, one for encryption. Rotating a key here means creating new subkeys and retiring the old ones. The primary key and the fingerprint stay the same.

Because the fingerprint does not change, the partners who trust the key keep trusting it. They only need the updated public key, which carries the new subkeys. Files encrypted to the old subkey can still be decrypted, because the old subkey is kept in the key.

Rotate when the subkeys are about to expire, on a fixed schedule set by your policy, or after any event that may have exposed the key.

## Before you start

* The key **In custody**: its private key and passphrase in the vault. See [Discover and keep PGP keys in custody](https://wiki.datadike.com/pam/administration/pgp-keys/custody.md).
* The **primary** private key in custody. A key whose primary key is kept offline, with only the subkeys on the server, cannot have its subkeys renewed by the solution.
* A role with permission to rotate and install PGP keys.
* For a key installed on an asset: SSH reachability and the access account, as for installing the key.

## Steps

### Rotate now

1. Open **Accounts › PGP keys**.
2. On the key, click **Rotate now**.

### Rotate automatically

1. Edit the key.
2. Under **Rotation**, turn on **Automatic rotation**.
3. Set **Rotate how many days before** expiration.
4. Set **Validity of new subkeys**. It must be longer than **Rotate how many days before**, or the key would be due for rotation again as soon as it is rotated.
5. Save.

The daily check rotates every key with **Automatic rotation** on whose expiration is within **Rotate how many days before**. After a failed attempt, the next one starts the following day.

## What happens in a rotation

1. The solution opens the key from the vault with its passphrase.
2. It creates a new **signing** subkey and a new **encryption** subkey, with the same algorithm as the current ones and the validity set in **Validity of new subkeys**. A key that signs with its primary key, without a signing subkey, gets only a new encryption subkey.
3. The current subkeys are set to expire **one day later**. They stay in the key, so signatures made with them can still be verified and files encrypted to them can still be opened.
4. If the primary key has an expiration date, it is extended to match the new subkeys.
5. If the key is installed on an asset, the updated key is installed in the keyring of its owner **before** the vault is updated. If the installation fails, the vault keeps the previous key and the rotation is recorded as failed.

## What you should see

The key shows the new expiration date, **Last rotation** with the current date, and, under **Subkeys**, the new signing and encryption subkeys next to the old ones, which expire the next day.

The recipients of the **PGP key lifecycle** notification are told when a key is rotated or a rotation fails.

## How to verify it worked

1. On the server, as the keyring owner, list the key:

   ```bash
   gpg --list-secret-keys --with-subkey-fingerprints
   ```

   The fingerprint of the primary key must be the same as before, and the new subkeys must appear with the new expiration date.
2. Encrypt and decrypt a test file with the key on the server:

   ```bash
   echo test | gpg --encrypt --recipient <fingerprint> | gpg --decrypt
   ```
3. Open **Audit › Console › PGP keys**. The **Subkeys rotated** event lists the new and the retired subkeys, whether the key was installed on the asset, and who started the rotation, or **System** when it was automatic.
4. Send the updated public key to the partners: **Download public key**.

## Common errors

| Symptom                                                           | Cause                                                                                             | Fix                                                                                               |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| **Rotate now** says the rotation needs the private key in custody | The key is monitored but its private key is not in the vault                                      | Use **Collect private key**, or import the private key                                            |
| Rotation failed: the primary private key is not in custody        | The primary key is kept offline                                                                   | Renew the subkeys where the primary key is kept, then import the key again                        |
| Rotation failed: the passphrase does not open the key             | The passphrase in the vault no longer matches, for example after someone changed it on the server | Collect the private key again with the current passphrase                                         |
| Rotation failed while installing on the asset                     | The asset or the keyring owner cannot be reached                                                  | Fix the access and rotate again. The vault still holds the previous key, which matches the server |
| A partner cannot encrypt to the key after the rotation            | The partner still has the previous public key, whose subkeys are now expired                      | Send the updated public key                                                                       |
| Saving the key is refused: rotation window                        | **Rotate how many days before** is not shorter than **Validity of new subkeys**                   | Shorten the window or lengthen the validity                                                       |

## Field reference

| Field                       | What it does                                                | Default |
| --------------------------- | ----------------------------------------------------------- | ------- |
| Automatic rotation          | Rotates the key from the daily check                        | Off     |
| Rotate how many days before | Days before expiration at which the automatic rotation runs | 30      |
| Validity of new subkeys     | Validity, in days, of the subkeys created at each rotation  | 365     |

## Related pages

* [Discover and keep PGP keys in custody](https://wiki.datadike.com/pam/administration/pgp-keys/custody.md)
* [Certificate and PGP key audit](https://wiki.datadike.com/pam/administration/audit/certificate-and-key-audit.md)
* [Rotate credentials](https://wiki.datadike.com/pam/administration/accounts/rotation.md)
