# Incident runbooks These runbooks cover the incidents an envelope-encryption deployment must be ready for. Each starts with the signal, then gives containment, recovery, and what to verify. Rehearse them before you need them: RFC-0010 requires them exercised before a release candidate is promoted. The project exercises their steps on every change, with in-memory key services standing in for real ones (`tests/integration/test_runbooks.py`); rehearse them with your own key services, logs, and people as well. Two facts shape every runbook: - **Rewrap changes who can unwrap, not the data key.** `rewrap` wraps the same data key for a new recipient. It stops a compromised key from opening envelopes in the future, but anyone who already unwrapped an envelope's data key, and holds a copy of the envelope, can still read that copy. Data that must stay confidential against such an attacker is re-encrypted: opened and sealed again, which creates a new data key. - **Rewrap replaces one recipient.** Name it with `replace=` (or `--replace-key` on the command line) whenever it is not the primary key. Without it, the recipient with the destination's name is replaced, or else the first recipient, the primary key, whichever recipient unwrapped. ## A key-service key may be compromised **Signal.** Unexpected `Decrypt` or `Encrypt` calls in the key service's audit log, leaked credentials of a role that can use the key, or a key policy changed without review. **Contain.** 1. Revoke the credentials or grants involved, and restrict the key's policy to the identities that must keep using it during recovery: the services, and the rewrap job. 2. Find what was exposed. With AWS KMS, every call carries the encryption context `cryptography-suite:binding`, a digest of one envelope's header, and CloudTrail records it: compute the same context with `envelope_binding_context` from `cryptography_suite_aws_kms` over your stored envelopes to find the ones each suspicious call opened. It reads only an envelope's first bytes, up to the end of its header, and does not change when an envelope is rewrapped. The other services log the key version and the caller. **Recover.** 1. Create a new key. Make it the primary key of every `Protector`, and list the compromised key in `decryption_keys` so existing envelopes stay readable. 2. Rewrap every envelope from the compromised key to the new key, with a job that runs under the restricted identity. `rewrap` is idempotent, so the job can be rerun until it finds nothing to move. 3. Re-encrypt the envelopes found in step 2 of containment, if their confidentiality must hold against whoever made those calls. 4. Remove the compromised key from `decryption_keys`, then disable it and schedule its deletion. **Verify.** `inspect` on a sample of envelopes shows the new key; opening with a `Protector` that no longer trusts the old key succeeds. ## The escrow key may be compromised **Signal.** The escrow private key, or a copy of it, is missing or was handled outside its procedure. The escrow key opens every envelope it is a recipient of, so treat this as a compromise of all of them. 1. Generate a new escrow key pair offline, register its public key, and make it the escrow recipient of every `Protector`. 2. Rewrap every envelope to it, naming the recipient to replace: ```python protector.rewrap( envelope, destination_key=KeyRef(escrow.provider_id, "break-glass-2028"), replace=KeyRef(escrow.provider_id, "break-glass"), ) ``` The escrow recipient is replaced and the primary recipient is kept. Envelopes that already hold the new escrow key lose the old one, and envelopes already rewrapped are returned unchanged, so the job can be rerun. 3. Re-encrypt envelopes whose copies the holder of the old key may have. 4. Destroy every copy of the old escrow key once the rewrap is complete. **Verify.** On an isolated machine, open a sample with only the new escrow key. ## A quorum custodian leaves or loses a key Fewer than the threshold of custodians learn nothing, so one lost or departed custodian is not a compromise. Restore the margin before another one is lost: 1. Add a new quorum version without that custodian, with a new one if needed: `add_quorum` with the same name. 2. Rewrap every envelope to the new version. The name matches, so the quorum recipient is replaced and every other recipient kept. If the threshold of custodians may have colluded, treat it as the escrow compromise above. ## The key service is unavailable **Signal.** `PROVIDER_UNAVAILABLE`, `PROVIDER_TIMEOUT`, or `PROVIDER_RATE_LIMITED` rises; health checks report `unavailable`. - **Opening** falls back by itself to the next recipient the `Protector` can unwrap, such as a second region. Readers that hold only the primary key wait for the service. - **Sealing** fails closed: every envelope must be wrapped for every recipient key, so that no envelope is written without its escrow. If the outage outlasts what the application can queue, switch writers to a prepared configuration whose primary key is in the healthy region, and switch them back once the service recovers. Envelopes written in the meantime lack the failed region's recipient: re-encrypt them with the usual configuration to restore every recipient. A rewrap to the usual primary key also lets readers that hold only that key open them, but it replaces the healthy region's recipient rather than adding one. - **Throttling** is the service working as designed: raise the quota, or put [hierarchical keys](multi-tenant.md) in front of the service so that it sees one call per branch key per cache period. When the service will not come back, recover from the escrow or quorum key as described in [Rotation and recovery](rotation-and-recovery.md). ## Authentication failures rise **Signal.** `AUTHENTICATION_FAILED` or `CONTEXT_MISMATCH` above its usual level. 1. **Do not retry.** These failures are permanent for the same input. 2. Separate the causes. `CONTEXT_MISMATCH` means the caller supplies a different context: a deployment or routing error far more often than an attack. `AUTHENTICATION_FAILED` means the bytes changed, or no trusted key opens them correctly. 3. Check storage and transport first: truncated uploads, a proxy that rewrites bodies, a text-mode file transfer, or a mix-up of envelopes and their records. Such damage often fails before authentication, as `FORMAT_INVALID` (the framing no longer adds up, as after truncation) or `TRAILING_DATA` (bytes appended), so a rise of those codes at the same time points the same way. 4. `inspect` the failing envelopes. It reads them without keys and is safe on hostile input; recipients that name keys you do not use point to envelopes that are not yours. 5. Keep the failing envelopes and the audit trail as evidence, and verify the trail. ## The audit trail fails verification **Signal.** `verify_audit_chain` raises `AuditChainError`. | `reason` | Means | | --- | --- | | `malformed` | A line is not a valid record. | | `wrong_chain` | A record belongs to another chain. | | `sequence_gap` | A record is missing or inserted. | | `broken_link` | A record does not follow the one before it. | | `hash_mismatch` | A record was edited. | | `signature_invalid` | A record was not signed by this key. | | `checkpoint_mismatch` | The trail does not end at the published checkpoint: records were removed from the end, or the checkpoint is not this trail's. | 1. Preserve the trail and the storage's own access logs before anything else writes to them. 2. Verify the trail up to the last published checkpoint that still matches: pass the lines up to that checkpoint's `sequence`, with the checkpoint as `expected_end`. Everything up to it is intact. 3. Investigate who could write to the trail's storage. If the trail verifies record by record but no longer ends at a published checkpoint, or a published checkpoint names records that verify with different content, someone re-signed records: the signing key itself is compromised. Rotate it, and start a new chain. ## A vulnerability affects your version 1. Read the advisory: it states the affected versions, the fixed version, and whether envelopes written by affected versions need action. 2. Upgrade, and run `cryptosuite doctor` on the new build. 3. If the advisory says so, rewrap or re-encrypt the affected envelopes; the advisory names which, by format version, suite, or provider. Report suspected vulnerabilities privately, as described in [SECURITY.md](https://github.com/Psychevus/cryptography-suite/blob/main/SECURITY.md).