# Bring your own key

> Sign your realm's tokens with a key your organization made, and publish its certificate chain beside it.

Some organizations must sign with a key they generated themselves, under their own key ceremony, or must publish a
certificate that chains to their own authority so their partners can check it. Bring your own key lets a realm do
that: you import a signing key and its certificate chain, and from the moment you name the realm signs with it, the
same way it would sign with a key it made.

## Before you begin

- **Turned on by Cvyūh.** Bring your own key is turned on per realm, by Cvyūh, on request. A realm without it signs
  only with [keys the realm makes](/platform/keys/signing-keys/).
- **Privilege:** `MODIFY` on the realm's `keySettings`, which `RealmOwner` gives.
- **Where:** the API.
- **First:** the key as a PKCS#8 PEM, and optionally its certificate chain as PEMs, leaf first. Your session is in
  `$CVYUH_SESSION` ([Call the API](/develop/api/#get-a-session)).

## How it works

An imported key is a new version of the realm's signing key for one algorithm. It goes through the same rotation as
any other: published at once, signing from its `validFrom`, and kept after it is replaced so the tokens it signed
still verify. You revoke it the same way too.

The certificate chain you supply is published with the key, in its `x5c` member in the realm's key set, so a
verifier that wants a certificate path can build one. The chain is optional.

## Set it up

```sh
curl -X PUT https://foobar.cvyuh.com/api/v0/am/keys/v1/signing/imported \
  -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>",
       "privateKey": "-----BEGIN PRIVATE KEY-----\n…\n-----END PRIVATE KEY-----",
       "certificateChain": [
         "-----BEGIN CERTIFICATE-----\n…leaf…\n-----END CERTIFICATE-----",
         "-----BEGIN CERTIFICATE-----\n…issuer…\n-----END CERTIFICATE-----"
       ]
     }'
```

The realm answers `204`. The key then appears in `GET https://foobar.cvyuh.com/api/v0/am/keys/v1` like any other
version, and in the published key set at once.

| Field | What it is |
|---|---|
| `algorithm` | `ES256`, `RS256` or `PS256`: the algorithm the key signs with |
| `validFrom` | When it starts signing, as an RFC 3339 time, at least the publish lead from now |
| `privateKey` | The key, as a PKCS#8 `PRIVATE KEY` PEM |
| `certificateChain` | Optional. The chain for it, leaf first, each a `CERTIFICATE` PEM |

## What happens when it fails

| Answer | Cause | What to do |
|---|---|---|
| `403`, "this realm holds no grant to bring its own key" | Not turned on for the realm | Ask Cvyūh to turn it on |
| `400`, "privateKey does not read" or "certificateChain does not read" | Not a PEM of the right kind | Send a PKCS#8 `PRIVATE KEY` PEM, and `CERTIFICATE` PEMs |
| `400`, "the supplied key is refused" | The key doesn't fit the algorithm | ES256 needs a P-256 key; RS256 and PS256 need RSA of at least 2,048 bits |
| `400` on the chain | The leaf isn't for this key, or isn't valid at `validFrom` | Send the key's own certificate first, valid at `validFrom` |
| `400`, "a rotated key signs too soon" | `validFrom` is sooner than the publish lead | Send a later `validFrom` |
| `400` | The realm holds 100 versions and the oldest is still live | Revoke an old version first |

## Limits and defaults

| What | Default | Can it change |
|---|---|---|
| Bring your own key | Off | Cvyūh turns it on per realm, on request |
| Key sizes | P-256 for ES256; RSA 2,048 bits or more for RS256 and PS256 | No |
| Signing-key versions, imported ones included | 100 per realm | No |
| Publish lead | 3,600 seconds | `keySettings.rotation.publishLeadSecs`; see [Signing keys](/platform/keys/signing-keys/#limits-and-defaults) |

## Troubleshooting

- **A partner can't build a path to your authority.** The chain was left out, or doesn't reach an authority they
  trust. Import the key again as a new version, with the full chain.
- **You need the key back.** Keep your own copy: no API returns an imported key. Reading back a key you brought is
  [planned](/platform/keys/whats-next/#read-back-an-imported-key).

## Audit record

An import is recorded on the realm's audit trail as an `UPDATED` record for the signing key, under the administrator
who made it, marked `imported`, with whether it held; the key never is. See
[Signing keys](/platform/keys/signing-keys/#audit-record).

## Good to know

- **Withdrawing the grant closes the route, not the key.** The imported key in force keeps signing until you rotate
  to a new one.
- **The leaf certificate must be for this key.** Its first certificate must name the key's public half.

## Coming next

- **[Read back an imported key](/platform/keys/whats-next/#read-back-an-imported-key)**: see the key you brought.

## Related

- [Signing keys](/platform/keys/signing-keys/): rotation, revocation and the settings an import follows.
- [Rotate signing keys](/platform/guides/rotate-signing-keys/): the rotation an import takes part in.
