> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tposs/latest/configuration/auc-key-handling.md).

# Authentication Centre key management

Before a device can use a 4G network, it must prove that it belongs to a subscriber of that network. The proof relies on a secret stored separately on the SIM card and in the network. Neither side ever sends this secret over the air. If the network doesn't have a copy of that secret for a subscriber, that subscriber cannot access the network.

In TPOSS, this secret is stored by the Authentication Centre (AuC), a part of the TPOSS HSS. It consists of two values: 

- `Ki` - the secret key of the SIM
- `OPc` - an operator value derived from `Ki` and the operator's own `OP` value

These two values together are called key material.

> [!IMPORTANT]
> This key material is the most sensitive data that TPOSS holds. Anyone who obtains it can impersonate the subscriber in the network and obtain the keys that protect its traffic. The subscriber can neither notice nor prevent it. The values are burned into the SIM, so a leak cannot be repaired by changing them. It means issuing a new card.
>
>  TPOSS treats the key material as write-only. An operator provisions it through the REST API, and nothing hands it back afterwards. Reading a subscriber's record returns metadata only, such as whether key material is present and which key encrypts it. Provisioning the data for the existing subscriber replaces the stored values, which is the way to correct a provisioning mistake or move a subscriber onto a re-issued SIM.

TPOSS keeps this key material encrypted in the database. Each subscriber's key is associated with a separate key stored only at TPOSS to encrypt the key material.

This document describes how you provision key material and manage the key that protects it.

## Provisioning key material

An operator provisions the key material of a subscriber over the [REST API](https://docs.cennso.com/components/tposs/latest/api-reference/rest/api.md), in the body of the request. TPOSS rejects `ki`, `op`, or `opc` sent as query parameters, because query strings can end up in logs, proxies, and shell history.

If you provision a subscriber with `op` instead of `opc`, TPOSS derives `OPc` while it handles the request and stores only the derived value.

## Creating the keyring

> [!IMPORTANT]
> Every environment must have its own keyring, including a test one. Never share a keyring with any other deployment.

The keyring is a Kubernetes Secret with the keys that encrypt key material. Each entry represents one key, and its name is the key's name. An entry must hold either exactly `32` raw bytes or `64` hex characters, in upper or lower case, and a trailing newline is tolerated.

You can create the key using the following commands:

```bash
# Generate 32 random raw bytes, named key-1
openssl rand 32 > key-1.bin

# Create the secret
kubectl create secret generic tposs-auc-keyring-prod --from-file=key-1=key-1.bin
```

After you created the secret with the encryption keys, you should specify the name of the Secret using the following configuration options:

```yaml
hss:
  auc:
    keyring:
      # Name of a Kubernetes Secret holding key-encryption keys.
      secretName: "tposs-auc-keyring-prod"
      # The key in the Secret that new records are encrypted with.
      current: "key-1"
```

## Key rotation

The keyring can hold several keys at once, and every subscriber record keeps the name of the key that encrypted it. TPOSS decrypts each record with the key that the record names, so records written under an older key keep working. A key that any record still names must stay in the keyring.

Rotation puts a new key into service for new records. Rotate keys according to the schedule defined by your key-management policy, and immediately if a key may have been exposed. To do that, you can use the following commands:

  1. Add the new key to the Kubernetes Secret named by **hss.auc.keyring.secretName**, keeping the entries already in it:

     ```bash
     # Generate new key
     openssl rand 32 > key-2.bin

     # Add the generated key to the secret
     kubectl patch secret tposs-auc-keyring-prod \
       -p "{\"data\":{\"key-2\":\"$(base64 < key-2.bin | tr -d '\n')\"}}"
     ```

  2. Set **hss.auc.keyring.current** to the name of the new key and roll the deployment.
  3. Any older subscriber records keep their old key regardless of the new key. To re-provision the existing subscribers is the only way to move them to the new key.
