# Rotate signing keys

This guide replaces your signing key without breaking installed copies of your application. At the end, new builds embed the new public key, older builds keep working, and you can delete the old key when no copy uses it.

## When to rotate

A signing key does not expire, and Seatify does not require a rotation. Rotate when your security policy requires a regular change of keys, or when you want to stop trusting a key.

A rotation alone does not stop a key. A retired key still signs for every copy of your application that names it. Only deletion stops the key. See [Active and retired keys](../../../concepts/signing-keys/#active-and-retired-keys).

## Before you start

- Your application verifies signed responses with an embedded public key. See [Verify signed responses](../verify-signed-responses/).
- Your application keeps its embedded keys in a map from key ID to public key. The examples in [Verify the response](../verify-signed-responses/#verify-the-response) and [Verify the file](../../offline-use/offline-validation/#verify-the-file) do this.
- Your application names its key in the `Seatify-Signing-Key` header on every request to the License API and the Distribution API. That includes license file check-outs. See [Key selection](../../../concepts/signing-keys/#key-selection).

{{< callout type="warning" >}}
If an installed copy sends no `Seatify-Signing-Key` header, Seatify signs its requests with the active key. After a rotation, that copy has no key for the new key ID, and it rejects every answer. Ship a build that sends the header before you rotate.
{{< /callout >}}

The examples call `https://license.seatify.dev`. In curl, the keys are the variables `$SEATIFY_PUBLISHABLE_KEY` and `$SEATIFY_SIGNING_KEY_ID`. In the other languages, they are variables of your application.

## Steps

{{% steps %}}

### Rotate the key

Rotate the signing key in the dashboard. Seatify creates a new active key, and the old key becomes retired. Copy the key ID of the new key and its public key in Base64.

The rotation does not affect installed copies. They name the old key, and the retired key still signs for them. A request that names no key is signed with the new key. New files that you publish are signed with the new key.

### Embed the new key

Add the new key to the map of embedded keys. Keep the old key in the map.

Your application still needs the old key after the update:

- A license file that the device stored before the update is signed with the old key. It stays valid until it expires.
- A file of a release that you published before the rotation is signed with the old key until you re-sign it.

{{< tabs >}}
{{< tab name="curl" >}}
This step runs inside your application. Choose a language tab to see the code.
{{< /tab >}}
{{< tab name="Swift" >}}
```swift
let publicKeys = [
    "<new key id>": "<new public key in Base64>",
    "<old key id>": "<old public key in Base64>",
]
```
{{< /tab >}}
{{< tab name="C#" >}}
```csharp
var publicKeys = new Dictionary<string, string>
{
    ["<new key id>"] = "<new public key in Base64>",
    ["<old key id>"] = "<old public key in Base64>",
};
```
{{< /tab >}}
{{< tab name="Go" >}}
```go
var publicKeys = map[string]string{
	"<new key id>": "<new public key in Base64>",
	"<old key id>": "<old public key in Base64>",
}
```
{{< /tab >}}
{{< tab name="JavaScript" >}}
```javascript
const publicKeys = {
  "<new key id>": "<new public key in Base64>",
  "<old key id>": "<old public key in Base64>",
};
```
{{< /tab >}}
{{< /tabs >}}

### Name the new key

Send the key ID of the new key in the `Seatify-Signing-Key` header. Seatify then signs the answers and new license files of this build with the new key.

{{< tabs >}}
{{< tab name="curl" >}}
```bash
curl -i https://license.seatify.dev/v1/ping \
  -H "Seatify-Publishable-Key: $SEATIFY_PUBLISHABLE_KEY" \
  -H "Seatify-Signing-Key: $SEATIFY_SIGNING_KEY_ID" \
  -H "Seatify-Nonce: $(uuidgen)"
```
{{< /tab >}}
{{< tab name="Swift" >}}
```swift
let signingKeyID = "<new key id>"
let nonce = UUID().uuidString

var request = URLRequest(url: URL(string: "https://license.seatify.dev/v1/ping")!)
request.setValue(publishableKey, forHTTPHeaderField: "Seatify-Publishable-Key")
request.setValue(signingKeyID, forHTTPHeaderField: "Seatify-Signing-Key")
request.setValue(nonce, forHTTPHeaderField: "Seatify-Nonce")

let (data, response) = try await URLSession.shared.data(for: request)
```
{{< /tab >}}
{{< tab name="C#" >}}
```csharp
const string signingKeyId = "<new key id>";
var nonce = Guid.NewGuid().ToString();

using var client = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Get, "https://license.seatify.dev/v1/ping");
request.Headers.Add("Seatify-Publishable-Key", publishableKey);
request.Headers.Add("Seatify-Signing-Key", signingKeyId);
request.Headers.Add("Seatify-Nonce", nonce);

using var response = await client.SendAsync(request);
```
{{< /tab >}}
{{< tab name="Go" >}}
```go
const signingKeyID = "<new key id>"

nonce := rand.Text()

req, err := http.NewRequest("GET", "https://license.seatify.dev/v1/ping", nil)
if err != nil {
	return err
}
req.Header.Set("Seatify-Publishable-Key", publishableKey)
req.Header.Set("Seatify-Signing-Key", signingKeyID)
req.Header.Set("Seatify-Nonce", nonce)

res, err := http.DefaultClient.Do(req)
if err != nil {
	return err
}
defer res.Body.Close()
```
{{< /tab >}}
{{< tab name="JavaScript" >}}
```javascript
const signingKeyId = "<new key id>";
const nonce = crypto.randomUUID();

const response = await fetch("https://license.seatify.dev/v1/ping", {
  headers: {
    "Seatify-Publishable-Key": publishableKey,
    "Seatify-Signing-Key": signingKeyId,
    "Seatify-Nonce": nonce,
  },
});
```
{{< /tab >}}
{{< /tabs >}}

The `keyid` in the `Seatify-Signature` header of the answer is the new key ID:

```text
Seatify-Signature: keyid="<new key id>", algorithm="ed25519", signature="<signature in Base64>", headers="(request-target) host date digest seatify-nonce"
```

### Ship the new build

Publish the new build as a release. Installed copies update over time. Until then, they keep working with the old key.

An older build that checks for this update finds no embedded key for the signature of the new file. See [Updates across a rotation](#updates-across-a-rotation).

### Wait for old copies

The dashboard shows the last used time of each key. Seatify updates this time at most once an hour. The time includes signed answers and license files, also the files that you download in the dashboard.

A copy that runs rarely can name the old key long after the last use. Wait as long as your users take to update.

### Re-sign the files

Re-sign in the dashboard each file that the old key still signs. Seatify signs the file again with the active key. The Distribution API shows the key ID of each file in `signing_key_id`.

Seatify refuses to delete a key that still signs files.

### Delete the old key

Delete the retired key in the dashboard. Deletion is permanent. See [After deletion](#after-deletion).

This step is optional. A retired key can stay as long as you need it.

{{% /steps %}}

## Updates across a rotation

Seatify signs every new file with the active key. After a rotation, an older build checks an update that the new key signed. The older build does not embed the new key, so it cannot check the signature of the file.

The older build can still trust the SHA-256 of the file. The release data comes in an answer that the old key signed, because the older build names the old key. The SHA-256 in this answer is as trustworthy as the signature.

Check a downloaded file in this order:

1. Verify the answer that lists the file. See [Verify signed responses](../verify-signed-responses/).
2. Compute the SHA-256 of the file. It must equal `sha256` from the verified answer.
3. If an embedded key has the key ID in `signing_key_id`, check `signature` with this key. See [Verifying files](../../../concepts/releases-and-artifacts/#verifying-files).
4. If no embedded key has this key ID, the SHA-256 check is enough.

Never trust a SHA-256 from an answer that your application did not verify.

## Devices with no internet

A device with no internet gets its license file from you. When you download the file in the dashboard, choose the key that the build of the customer embeds. For a build from before the rotation, choose the retired key. See [Devices with no internet](../../offline-use/devices-with-no-internet/#download-the-license-file).

Before you delete the retired key, check that no customer with no internet still runs a build that embeds only this key.

## After deletion

- Every request that names the deleted key fails with the status 400 and the message "The signing key is not known." Seatify refuses the request before it makes any change.
- Seatify signs no new license files with the deleted key.
- A license file that the deleted key signed still verifies on the device until the file expires. The device checks the file with its embedded key.
- The active key cannot be deleted. Rotate first.

## Next steps

- [Verify signed responses](../verify-signed-responses/)
- [Offline validation](../../offline-use/offline-validation/)
- [Devices with no internet](../../offline-use/devices-with-no-internet/)
- [Signing keys](../../../concepts/signing-keys/)

