# Signing keys

> The keys that sign your realm's tokens, published ahead of use and rotated or revoked without any app refusing a token.

Every token your realm issues is signed, so an app or API can check that the realm made it and nobody changed it.
Each realm has its own signing keys. No API returns them, to you or anyone else; apps and APIs check signatures
against the public halves, which the realm publishes.

You rotate the keys whenever you choose: a new key is published before it signs anything, so no app ever meets a
token signed by a key it doesn't have yet. When you no longer trust a key, you revoke it, and nothing it signed
verifies again. Keys never rotate on their own.

## Before you begin

- **Privilege:** `MODIFY` on the realm's `keySettings`, which `RealmOwner` gives. Listing, rotating and revoking all
  need it.
- **Where:** the API. Keys in the dashboard are [planned](/platform/keys/whats-next/#keys-in-the-dashboard).
- **First:** your session in `$CVYUH_SESSION` ([Call the API](/develop/api/#get-a-session)).

## How it works

A realm is born with one signing key for each of three algorithms. Each signs different tokens:

| Algorithm | What it signs |
|---|---|
| ES256 | Access tokens, always. Status lists, security events and the bootstrap assertions you mint for an agent. ID tokens and signed userinfo answers for clients that ask for it, and ID tokens by default for a `fapi2` client |
| RS256 | ID tokens by default. Signed introspection answers by default, except for a `fapi2` client. Identity assertions, chaining grants and transaction tokens |
| PS256 | ID tokens and signed userinfo answers for clients that ask for it, such as FAPI 2.0 clients, once you add `PS256` to `keySettings.algorithm.oauth2.idToken` |

A client picks its ID-token algorithm with `idTokenSignedResponseAlg`, its userinfo algorithm with
`userinfoSignedResponseAlg`, and its signed introspection algorithm with `introspectionSignedResponseAlg`, each from
the list the realm offers. Logout tokens use the client's ID-token algorithm. A `fapi2` client never gets RS256.

### The published key set

The public keys are at `https://foobar.cvyuh.com/oauth2/jwks`. The address is public, and any web page may read it.
Each key's id (`kid`) is its RFC 7638 thumbprint. The set also carries the realm's one encryption key
(`"use": "enc"`), which apps encrypt [request objects](/workforce-identity/applications/client-security/request-objects/)
to; a verifier takes only the keys that sign.

The answer says how long it may be cached (`Cache-Control: public, max-age=300` by default), and that is what makes
rotation safe.

### Rotation

A rotation names one algorithm and the moment the new key starts signing, `validFrom`. That moment must be at least
the **publish lead** away (an hour by default):

1. You rotate. The new key is published at once, beside the old one.
2. Apps and APIs pick it up as their caches of the key set refresh.
3. At `validFrom`, the new key starts signing. The old one stays published so the tokens it signed still verify.

### Revocation

Revoking destroys one key version. It leaves the key set, and every token it signed stops verifying: at the realm at
once, and at your APIs and gateways as their caches drop it. The key that is signing now can't be revoked, so to
replace a key you don't trust, rotate first, then revoke the old version.

### What your apps should do

A verifier that follows three rules never needs you during a rotation:

- **Pick the key by `kid`.** Take the key whose `kid` matches the token's header, never "the first key".
- **Refetch on an unknown `kid`,** once, then refuse the token if the `kid` is still missing. Don't refetch more often
  than every five minutes on unknown `kid`s alone, so a stream of forged tokens can't make it hammer the realm.
- **Honor `Cache-Control`.** Keep the key set no longer than its `max-age`.

Every mainstream OAuth and JOSE library does this when given the realm's issuer or key set address. An app that copied
one key into its configuration doesn't, and must be given the new key before `validFrom`.

## Set it up

Rotate one algorithm, naming when the new key starts signing:

```sh
curl -X PUT https://foobar.cvyuh.com/api/v0/am/keys/v1/signing \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "algorithm": "ES256", "validFrom": "<RFC 3339 time, at least the publish lead from now>" }'
```

The realm answers `204`. Rotate each algorithm with its own call.

To see the keys, call `GET https://foobar.cvyuh.com/api/v0/am/keys/v1`. Each entry gives a `key`, its `version`,
`alg`, `state` and `validFrom`, and never the key itself:

- `oauth2/signing` entries are the signing keys. `valid` means it signs now, or will from its `validFrom`;
  `retained` means it is kept only to verify what it signed.
- `oauth2/signing/cluster-local` entries are the [internal signing key](#the-internal-signing-key).
- `store/encryption` entries are the realm's [storage key](/platform/data/encryption/). Its newest version is
  `valid`; older ones are `retained` only to open what they sealed.

To revoke a version, call `DELETE https://foobar.cvyuh.com/api/v0/am/keys/v1/signing/<version>`. The realm answers
`204`. The guides [Rotate signing keys](/platform/guides/rotate-signing-keys/) and
[Revoke a signing key](/platform/guides/revoke-a-signing-key/) take each from start to finish.

### The internal signing key

The realm holds one more signing key, `oauth2/signing/cluster-local`: the internal signing key. It signs only tokens
that never leave Cvyūh, passed between the parts of your realm's products. It is ES256, it is never in the published
key set, and no app or API of yours ever meets a token it signed.

**You never need to touch it.** Rotating it (`PUT https://foobar.cvyuh.com/api/v0/am/keys/v1/cluster-local`, with the
same body as a signing key's) or revoking a version
(`DELETE https://foobar.cvyuh.com/api/v0/am/keys/v1/cluster-local/<version>`) follows the same rules as a signing
key, and every part of your realm learns the new key before it signs; nothing you see changes. Do it only if Cvyūh
asks you to.

## What happens when it fails

| Answer | Cause | What to do |
|---|---|---|
| `400`, "a rotated key signs too soon" | `validFrom` is sooner than the publish lead; the detail names the earliest time allowed | Send a later `validFrom` |
| `400` on a revocation | The version is signing now, or became the signer within `skewSecs` | Rotate first, wait `skewSecs` past the new key's `validFrom`, then revoke |
| `400` on a rotation | The realm holds 100 versions and the oldest is still live | Revoke an old version first |
| `400` on a settings change | The publish lead no longer exceeds the cache time plus 300 seconds, or `skewSecs` | Raise `publishLeadSecs` first |
| `400` on the internal key | An algorithm other than ES256 | Send `ES256` |
| `403` | Your rights don't include `MODIFY` on `keySettings` | Ask for `RealmOwner` |
| `404` on a revocation | No such version, or it's already revoked | List the keys and check the version |

What users see: nothing, during a rotation that keeps to the publish lead. After a revocation, a user whose tokens the
revoked key signed is asked to sign in again by their app, or their app refreshes.

## Limits and defaults

All in the realm's `keySettings`, except the version cap:

| Setting | Default | Range | What it sets |
|---|---|---|---|
| `rotation.publishLeadSecs` | `3600` | 10 to 2,592,000 seconds | How far ahead a new key is published |
| `rotation.skewSecs` | `60` | 5 to 300 seconds | How far two clocks may disagree on a key change |
| `publication.maxAgeSecs` | `300` | 0 to 86,400 seconds | How long apps may cache the key set |
| `algorithm.oauth2.idToken` | `["RS256", "ES256"]` | `RS256`, `ES256`, `PS256` | What ID tokens, userinfo answers and logout tokens may be signed with. RS256 always stays |
| `algorithm.oauth2.introspection` | `["RS256", "ES256"]` | `RS256`, `ES256`, `PS256` | What a signed introspection answer may use |
| Signing-key versions | 100 per realm, across the three algorithms | Can't be raised | How many versions the realm keeps |

`rotation.publishLeadSecs` must exceed `publication.maxAgeSecs` plus 300 seconds, and must exceed
`rotation.skewSecs`; a lead under 301 seconds therefore needs a `maxAgeSecs` of 0. Change a setting with
`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/keySettings/keySettings`, for example
`{ "rotation": { "publishLeadSecs": 7200 } }`.

## Troubleshooting

- **An API refuses tokens right after `validFrom`.** It cached the key set before the new key was published, and
  doesn't refetch on an unknown `kid`. Give it the rules in [What your apps should do](#what-your-apps-should-do), or
  raise `publishLeadSecs` so its cache turns over first.
- **An app refuses every ID token after you added PS256.** The client asked for an algorithm its library can't check.
  Set its `idTokenSignedResponseAlg` to one the library supports.
- **Your policy says rotate yearly.** Keys never rotate on their own: set a reminder of your own, and follow
  [Rotate signing keys](/platform/guides/rotate-signing-keys/) for each algorithm.

## Audit record

Every rotation, import and revocation is recorded on the realm's audit trail under the administrator who made it,
whether it held or was refused, never the key: an `UPDATED` record for a rotation or import (an import marked
`imported`) and a `DELETED` record for a revocation, each naming the key and its version. These records aren't about
a user or agent, so no trail answer returns them yet; ask Cvyūh support for them. A change to `keySettings` is in its
[change history](/audit-reports/audit-trail/change-history/), at
`GET https://foobar.cvyuh.com/api/v0/am/objects/v1/keySettings/keySettings/history`.

## Good to know

- **You can bring your own key.** Once it's turned on for the realm, a realm signs with a key and certificate chain
  it supplies: see [Bring your own key](/platform/keys/bring-your-own-key/).
- **A deleted realm's keys go with it.** A deleted realm answers nothing, so nothing it signed can be checked. Its
  keys are destroyed when the realm is purged; see [Realms and sub-realms](/platform/realms/sub-realms/#deleting-a-realm).

## Coming next

- **[Ed25519 signing](/platform/keys/whats-next/#ed25519-signing)**, beside the three algorithms above.

## Related

- [Rotate signing keys](/platform/guides/rotate-signing-keys/): the rotation, start to finish.
- [Revoke a signing key](/platform/guides/revoke-a-signing-key/): and the order to do it in when a key is exposed.
- [Check what clients discover](/platform/guides/what-clients-discover/): what apps read from the published key set.
- [Bring your own key](/platform/keys/bring-your-own-key/): sign with a key your organization supplies.
