# Crypto Policy

---

# How Crypto Policy Works

_Peios / Using Peios / Crypto Policy_

> How Peios decides which cryptography programs can use, records the weak cryptography in use, and lets particular programs keep using what they need.

Peios applies one crypto policy to every program on the machine that uses
libpcrypto or libpssl, its libraries with OpenSSL's API. The policy decides
which algorithms a program can use, records the use of weak ones, and lets
named programs keep using an algorithm that they need and that the policy
would otherwise refuse.

The policy is configured under `Machine\System\Cryptography`. cryptod, the
crypto policy service, publishes it as a file that the libraries read, and
records what programs report of their weak cryptography.

## Grades

Each use of cryptography is graded as an **entry**: an algorithm and the
operation it's used for, such as `md5.hmac` for HMAC-MD5 or `md5.digest`
for an MD5 digest. The same algorithm can have different grades for
different operations. MD5 is broken as a digest, but HMAC-MD5 has no
practical attack.

Grades run from U to A:

| Grade | Meaning |
|---|---|
| U | Practically broken, or forbidden by current standards |
| D | No practical break, but under 112-bit security or deprecated |
| C | 112-bit security |
| B | 128-bit security |
| A | 192-bit security or more |

Separately, each entry records whether FIPS 140-3 approves it. FIPS
approval isn't a grade: FIPS approves some entries below A, such as
HMAC-SHA-1, and doesn't approve some A-grade ones.

`/usr/share/peios-crypto/algorithms.tsv` lists every entry, with its grade
and whether FIPS approves it.

A few entries are on the **floor**: they're refused under every policy, and
no exception can allow them. The floor holds ciphers and TLS suites that
encrypt nothing, the 40-bit export-grade RC2 cipher, RSA keys and
Diffie-Hellman parameters under 1024 bits, and ECDSA signing with a fixed
nonce, which gives away the private key.

## Thresholds

The policy has two thresholds:

- **Reject:** entries below this grade are refused. By default it's U, so
  only the floor is refused.
- **Warn:** entries below this grade are allowed, but their use is
  recorded. By default it's C, so the use of anything graded U or D is
  recorded.

Each can also apply to every entry FIPS doesn't approve, whatever its
grade. Turning that on for Reject doesn't make the machine operate in a
FIPS-approved mode, because the libraries' module isn't validated.

A refused call fails as if the algorithm weren't supported. A few
low-level functions can't report failure, such as `SHA256` and `MD5_Init`,
so a program that calls one for a refused entry ends. Before raising the
Reject threshold, find out what would stop working: see
[Configuring Crypto Policy](/peios/using-peios/crypto-policy/configuring-crypto-policy.md).

## Exceptions

An exception lets particular programs use particular entries: a RADIUS
server that must use HMAC-MD5 because the protocol requires it, for
example. Each exception is a key under
`Machine\System\Cryptography\Exceptions`, named `Owner.Purpose`, such as
`Radius.PacketAuth`. It names the entries it allows, and has a security
descriptor, `ProgramSecurity`, granting the programs that can use it.

Packages add the exceptions their software needs when they install, and
remove them when they uninstall.

> [!IMPORTANT]
> **An exception applies to a program, not to whoever it's acting for.**
> `ProgramSecurity` is checked against each process's primary token, the
> identity the program runs as. Impersonation is ignored. This is unusual
> on Peios, where access is normally decided by whose behalf a thread is
> acting on.
>
> An exception answers "which software needs this algorithm". If it
> followed impersonation, a service would lose its exception while it
> served a client, and a client could weaken a service's cryptography.
>
> Grant a service's own SID, `S-1-5-80-...`, to except one service. A user
> or group SID is accepted, but it excepts every program that runs as that
> user or group, including ones that don't need the algorithm.

An exception allows an entry the Reject threshold refuses only while
`Exceptions\AllowExceptionsToReject` is 1, its default. When it's 0,
exceptions only stop warnings. An exception never allows an entry on the
floor.

## When changes apply

cryptod publishes the policy when it starts, and again a moment after
`Machine\System\Cryptography` changes. Each process reads the policy once,
at its first use of the libraries, and keeps it for as long as it runs.

So a change applies to processes started after it's published. A service
that's already running keeps the policy it started with until it's
restarted. Exceptions work the same way, so a service doesn't lose one
partway through a request.

cryptod also keeps a copy of the policy that survives a reboot. A process
that starts before cryptod reads that copy. A process uses Peios's built-in
defaults only on a machine where cryptod has never run, or if it can't use
either copy.

## What's recorded

The libraries report to cryptod, and cryptod records each report as an
event:

- `cryptod.algorithm.warned`: a process's first use of each entry below
  the Warn threshold.
- `cryptod.exception.used`: a process's first use of each exception,
  whether or not the exception warns. An audit can always find the weak
  cryptography in use.
- `cryptod.policy.failed`: a policy cryptod couldn't publish as written,
  or one a process couldn't use.
- `cryptod.policy.applied`: each policy cryptod publishes.

Each process reports each use once, so the records show which programs
use what without one for every call. A process is named by the identity
and process ID the kernel recorded when it connected to cryptod.

cryptod also keeps a summary of what's been used, and by whom, since it
started. See the [cryptod reference](/peios/using-peios/crypto-policy/cryptod-reference.md).

## When something is wrong

cryptod checks the policy when it publishes it:

- If a setting such as a threshold can't be read, cryptod keeps the policy
  it last published, and records `cryptod.policy.failed`. Falling back to
  defaults could loosen a policy an administrator had tightened.
- If an exception can't apply as written, cryptod drops that exception
  alone, so it allows nothing, publishes the rest, and records why. A
  misspelled entry, such as `md5.hmca`, is the usual cause.

A process uses a copy of the policy only if SYSTEM or cryptod owns it and
nobody else can change it. Otherwise it uses the next copy, or the
defaults, and reports it.

## Limits

- A process that starts before cryptod is listening can't report, so its
  first uses aren't recorded. It still applies the policy.
- A report is attributed to the identity of the reporting thread when it
  connected. A thread that was impersonating a user then is recorded as
  that user. The process ID still identifies the process.
- A process that replaces its primary token keeps the exceptions of the
  token it started with.
- Policy is applied to programs that use libpcrypto and libpssl. A
  program with cryptography of its own, or a statically linked copy of
  another library, is outside it.

---

# Configuring Crypto Policy

_Peios / Using Peios / Crypto Policy_

> Find the weak cryptography in use, tighten the machine's crypto policy, and let a particular service keep an algorithm it needs.

Tighten the crypto policy in steps: find what would stop working, give the
services that need a weak algorithm an exception, then raise the threshold.
Check the result after each change.

The regman entry for `Machine\System\Cryptography` owns the complete
configuration reference. `evman cryptod` describes the records cryptod
writes.

## Before you start

- Check that cryptod is running: `svctl status cryptod`.
- Obtain write access to `Machine\System\Cryptography`. SYSTEM and
  Administrators have it.
- Obtain read access to cryptod's events in eventd.
- Allow time for the machine to run its normal workload under the current
  policy, so that the records show everything it uses.

## Find the weak cryptography in use

1. List the programs that used an entry below the Warn threshold:

   ```sh
   evctl 'EVENTS cryptod.algorithm.warned SINCE 7d ago'
   ```

   Each record names the entry, its grade, and the process that used it.

2. List the uses of a grade that a new threshold would refuse. For
   example, before refusing everything below C:

   ```sh
   evctl 'EVENTS cryptod.algorithm.warned SINCE 7d ago WHERE object.crypto-algorithm.grade == "U"'
   evctl 'EVENTS cryptod.algorithm.warned SINCE 7d ago WHERE object.crypto-algorithm.grade == "D"'
   ```

3. List the exceptions in use, and by which programs:

   ```sh
   evctl 'EVENTS cryptod.exception.used SINCE 7d ago'
   ```

Every program in these results would fail to use its entry under the new
threshold, unless it has an exception. A program that calls a low-level
digest function, such as `SHA256` or `MD5_Init`, for a refused entry
ends.

## Let a service keep an algorithm

Give an exception to a service that must keep using an entry, such as a
RADIUS server that needs HMAC-MD5. Grant it to the service's own SID, so
that no other program gets it.

1. Find the service's SID. Read its main process ID from
   `svctl status <service>`, then list that process's token groups:

   ```sh
   token groups --pid <pid>
   ```

   The service SID is the group that begins `S-1-5-80-`.

2. Create the exception, named for its owner and purpose. This one allows
   HMAC-MD5 for one service:

   ```sh
   reg set -p Machine/System/Cryptography/Exceptions/Example.LegacyRadius \
       Algorithms multi:md5.hmac
   reg set Machine/System/Cryptography/Exceptions/Example.LegacyRadius \
       ProgramSecurity 'sz:O:SYG:SYD:(A;;0x1;;;S-1-5-80-<service SID>)'
   ```

   Name each entry as an algorithm and an operation. `md5.hmac` doesn't
   allow `md5.digest`. The descriptor needs an owner, `O:SY` here, as an
   access check refuses one without, and a DACL granting the Use right,
   `0x1`.

3. Check that cryptod published it, and didn't drop it:

   ```sh
   evctl 'EVENTS cryptod.policy.* SINCE 10m ago'
   ```

   A `cryptod.policy.failed` record naming the exception says why it was
   dropped. A dropped exception allows nothing.

4. Restart the service, so that it reads the new policy:

   ```sh
   svctl restart <service>
   ```

## Raise the reject threshold

1. Set the lowest grade the policy allows. This refuses everything graded
   U or D:

   ```sh
   reg set Machine/System/Cryptography/Reject MinimumGrade sz:C
   ```

2. Check that cryptod published the policy, as above. If a
   `cryptod.policy.failed` record says a setting is invalid, cryptod kept
   the previous policy: correct the value and check again.

3. Restart the services that should take up the new policy. Processes
   started from now on use it; running ones keep the policy they started
   with.

To refuse everything FIPS 140-3 doesn't approve, whatever its grade, set
`Reject\FIPS` to 1:

```sh
reg set Machine/System/Cryptography/Reject FIPS dword:1
```

## Check the result

Watch for refusals and new warnings after the change:

```sh
evctl 'EVENTS cryptod.* SINCE 1h ago'
```

A program refused an algorithm reports an error from its own calls, such
as a failed TLS handshake with an `insufficient_security` alert. If a
service stops working, give it an exception or lower the threshold, then
restart it.

To undo a change, set the value back, or delete it to return to Peios's
default, then restart the affected services:

```sh
reg del Machine/System/Cryptography/Reject MinimumGrade
```

---

# cryptod Reference

_Peios / Using Peios / Crypto Policy_

> The files and sockets of cryptod, the crypto policy service, the policy file's format, the rules a process uses to trust it, and the reports and posture cryptod handles.

cryptod compiles `Machine\System\Cryptography` into the policy file that
libpcrypto reads, and records what processes report of their weak
cryptography. Its configuration is documented by regman, and its records
by evman.

## Service

| Property | Value |
|---|---|
| Service | `cryptod` |
| Program | `/usr/sbin/cryptod` |
| Identity | `Service`: its own SID, `S-1-5-80-2691053820-547492864-353945177-641201782-1210658655` |
| Privileges | `SeChangeNotifyPrivilege`, `SeAuditPrivilege` |
| Readiness | Notify, once it has published the policy and is listening |
| Event root | `cryptod` |

cryptod must run as its own service SID. Processes trust a policy copy only
if SYSTEM or that SID owns it, and a file cryptod writes is owned by its
token's user.

## Files and sockets

| Path | What it is |
|---|---|
| `/run/cryptod/policy` | The current policy |
| `/var/state/cryptod/policy` | The policy kept across reboots, for processes that start before cryptod |
| `/run/cryptod/report` | Where processes report, a `SOCK_SEQPACKET` socket everyone can connect to |
| `/run/cryptod/posture` | Where the posture is served, a `SOCK_STREAM` socket for SYSTEM and Administrators |
| `/usr/share/peios-crypto/algorithms.tsv` | Every graded entry, which cryptod checks policies against |

A process reads `/run/cryptod/policy`, then `/var/state/cryptod/policy`,
then uses Peios's defaults: refuse only the floor, warn below C, FIPS
ignored, exceptions allowed to override refusal. It moves to the next if a
copy is absent or can't be used, and reports why.

cryptod writes each copy whole: to a new file beside it, which it gives
the copy's descriptor, then renames over the copy. Each copy's descriptor
is protected, grants full access to SYSTEM and cryptod, and grants reading
to Everyone, `S-1-15-2-1` and `S-1-15-2-2`, so that confined applications
can read it too. Both directories grant the same readers traversal.

`algorithms.tsv` has one line per entry: its identifier, its grade or
`floor`, and `1` or `0` for whether FIPS 140-3 approves it, separated by
tabs. cryptod doesn't start without it.

## Trusting a copy

A process uses a copy only if its descriptor shows that nobody but SYSTEM
and cryptod could have written it:

- Its owner is SYSTEM or cryptod's SID.
- It has a DACL. A copy with none lets everyone change it.
- No entry of the DACL grants anyone else a right that could change the
  file: writing or appending data, writing extended attributes or
  attributes, deleting it, changing its descriptor or owner, or a generic
  or maximum-allowed right that includes them. Entries that deny, and
  entries that only pass to children, are ignored.

The check fails closed. A descriptor over 4 KiB, or an entry granting
CREATOR OWNER a right to change the file, is refused as insecure, even
where it would be harmless. A process that refuses a copy reports
`copy-insecure`.

## The policy file

The file is version 1 of a binary format, at most 1 MiB less one byte.
Integers are big-endian. A process reads the whole file before using any
of it, and refuses a file of another version.

| Field | Size | Meaning |
|---|---|---|
| Magic | 8 bytes | `PEIOSCPL` |
| Version | 2 | 1 |
| Reject below | 1 | A grade: 0 for U, then 1 to 4 for D to A |
| Warn below | 1 | A grade, as above |
| Flags | 1 | `0x01` reject non-FIPS, `0x02` warn non-FIPS, `0x04` exceptions can override refusal |
| Exception count | 2 | The number of exceptions that follow |

Each exception follows in turn:

| Field | Size | Meaning |
|---|---|---|
| Name | 1-byte length, then the name | `Owner.Purpose` |
| Flags | 1 | `0x01` warn about its use |
| ProgramSecurity | 2-byte length, then the descriptor | A self-relative security descriptor |
| Entry count | 2 | The number of entries that follow |
| Entries | each a 1-byte length, then the identifier | Such as `md5.hmac` |

cryptod resolves `Exceptions\Enabled`, `WarnByDefault` and `WarnAlways`
as it compiles, so the file holds only what applies. It leaves out
exceptions it dropped. A process ignores an entry it doesn't grade, and
any entry on the floor.

## Reports

A process connects to `/run/cryptod/report`, sends one datagram, and
closes. It doesn't wait, and doesn't retry: a report that can't be sent is
lost. A datagram has four fields:

| Field | Size | Meaning |
|---|---|---|
| Kind | 1 | 1 warned, 2 exception used, 3 policy copy unusable |
| Problem | 1 | For kind 3, why; otherwise 0 |
| Entry | 1-byte length, then the identifier | For kinds 1 and 2; otherwise empty |
| Exception | 1-byte length, then the name | For kind 2; otherwise empty |

The problems are 1 unreadable, 2 insecure, 3 malformed, 4 an unknown
version, and 5 the process's token unreadable.

cryptod takes the sender's identity from the connection: the user of the
token the kernel captured at connect, and the process ID. Nothing in the
datagram names the sender. cryptod discards a datagram that libpcrypto
couldn't have sent, such as one naming an entry it doesn't grade.

## Rates

cryptod relays at most 512 reports at once from each user, then 8 a
second. It writes its records at up to 80 a second, below the lowest rate
KMES allows a process without `SeTcbPrivilege`, and queues up to 4096
records when more arrive. A full queue drops its oldest standard record to
make room, and turns a record away only when everything it holds is
essential. The posture counts what's turned away.

## Posture

Connecting to `/run/cryptod/posture` returns one MessagePack map, then the
connection closes. It describes the policy in force, and what's been used
since cryptod started:

| Key | Value |
|---|---|
| `policy` | The thresholds, flags, and exceptions in force; `dropped`, the exceptions dropped and why; `defaults`; and `last-failure`, a compile that failed since, or nil |
| `fips` | `requested`; `approved-mode`, always false while the module isn't validated; `reasons`; and `exceptions-admitting-non-fips` |
| `use` | One map per user SID: the exceptions it used, by entry, and the entries it was warned about, each with a count of reports |
| `turned-away` | Counts of reports `rate-limited`, `unattributed`, and `invalid`, and `overflow`: what didn't fit the queue or a table |

The posture is held in memory, so it starts again when cryptod restarts.
Its tables are bounded, at 4096 users and 256 exceptions each.
