# Certificates

The Certificates page keeps the certificates that CrushBalance's TLS and HTTPS listeners and its admin page serve. You can add the files a certificate company sent, request a certificate from one, make a self-signed certificate, or get a free one from Let's Encrypt or another ACME authority. CrushBalance then renews that free certificate by itself.

![The Certificates page: four certificates, the first one's card open with its checks, chain and kept version](Certificates/certificates.png)

The page has four tabs: **Certificates**, **Where used**, **Chain** and **Let's Encrypt**. Viewing it needs the permission `certificates.view`, changing certificates needs `certificates.manage`, and downloading one with its private key needs `certificates.export` ([Admin accounts and roles](Admin-Accounts-and-Roles.md)).

## The list and a certificate's card

Each row shows the certificate's state, its name and the names it is for, when it ends, who issued it, and where it is used. Click a row to open its card:

- **The checks, in words:**
  - it holds its private key;
  - the dates;
  - the names it is for;
  - whether its chain reaches a root that clients trust;
  - a weak key.

  Anything that needs you is marked **Look** or **Problem**.
- **Chain**: the certificates that are sent along with it, in order.
- **Versions**: the last three versions are kept, each with **Put back**.
- The buttons:

| Button | What it does |
|---|---|
| **Use on…** | puts the certificate in use on listeners or the admin page |
| **Fetch missing chain** | looks for a missing middle certificate at the address the certificate gives for it (public addresses only) |
| **Rename** | changes the name shown |
| **Download certificate** | the certificate alone, as PEM |
| **Copy with its key (PFX)** | the certificate with its private key, under a password you type |
| **Remove** | moves its files to `certificates/backup/`; a certificate in use cannot be removed |

The chips in the first column read **OK**, **Ends in … days**, **Needs a look**, **Expired** or **Request out**.

## Adding the files a certificate company sent

**Add certificate files** takes every file at once, in any shape:
- a PFX or P12, or a JKS;
- the `.crt` and `.key` files;
- a bundle or chain file;
- a `.p7b`;
- or the zip they came in.

CrushBalance reads each file by what it contains, not by its name. Nothing is saved until you choose.

A locked file asks for its password by name:

![A locked PFX asking for its password](Certificates/import-locked.png)

Once it is open, the private key is paired with its certificate and the checks are shown before anything is saved. Give it a label if you like, then **Add as a new certificate**. When it renews a certificate you already have, the button is **New version of …** instead.

![The PFX read: its checks before it is added](Certificates/import-read.png)

Here the company's file held no middle certificate, so the new certificate's chain does not reach a root clients trust. The card says so and says what to do:

![A certificate whose middle certificate is missing](Certificates/chain-missing.png)

## Chain

The **Chain** tab keeps middle and root certificates that complete the chains of the certificates you add: a bundle file from your certificate company, for example. **Completes** shows which certificates each one is used for.

![The chain pool with the company's middle and root certificate](Certificates/chain.png)

## Using a certificate

On a certificate's card, **Use on…** lists the TLS and HTTPS listeners and the admin page. Tick where it should be served, then **Use it**. The change is saved like any other change of the settings, and the certificate is served at once, without a restart.

![Use on: the Partner portal listener ticked](Certificates/use-on.png)

A listener can serve several certificates. A client that asks for a name (SNI) gets the one whose names cover it; a wildcard covers one label. The first certificate is the default for clients that ask for no name. You can also type the names on the listener's **TLS** tab ([Listener editor](Listener-Editor.md#tls)) and in **Settings › Admin** ([Settings](Settings.md#admin)). While a listener names certificates, its keystore setting is not used.

**Where used** lists every TLS and HTTPS listener and the admin page with what each one serves: certificates from this page, a keystore file, or the certificate CrushBalance generated.

![Where used](Certificates/where-used.png)

## Let's Encrypt and other ACME authorities

**Free certificate** asks for:
- the names;
- how each name is proven;
- the authority: Let's Encrypt, ZeroSSL, Google Trust Services, SSL.com, or any ACME directory URL, with an EAB key where the authority asks for one;
- an email;
- the key;
- the listeners to use the certificate on.

**Only test** stops after the test.

![The Free certificate dialog](Certificates/free-certificate.png)

Every order is tried against the authority's test service first. The real certificate is ordered only when the test passes; a test with the same settings in the last 30 days counts. The steps show as they happen:
1. the names point here;
2. this balancer is ready to answer;
3. the test;
4. the account;
5. each name proven;
6. the key;
7. the certificate issued and checked;
8. kept;
9. in use;
10. when it renews.

The picture below is a renewal (**Renew now**): its test steps are skipped because a test with the same settings passed that day.

![A renewal and its steps, on the Let's Encrypt tab](Certificates/acme.png)

A name is proven in one of three ways:

| Way | How |
|---|---|
| **Port 80 (http-01)** | Every HTTP and HTTPS listener answers the authority's request for `/.well-known/acme-challenge/…` itself, before choosing a backend. The internet's port 80 for the name must reach one of them; a router may forward port 80 to any listener port. |
| **Port 443 (tls-alpn-01)** | Every TLS and HTTPS listener answers the authority's proof handshake; every other client gets the listener's own certificate. |
| **DNS** | A TXT record `_acme-challenge.<name>`, either added by hand (the steps show it and wait for it; such a certificate cannot renew by itself) or by a DNS server that takes RFC 2136 updates signed with TSIG. Wildcards need DNS. |

Certificates from an authority renew by themselves, when the authority suggests it (ARI) or else when a third of their life is left. A renewal is a new version of the same certificate, so every listener that serves it serves the new one at once. A renewal that fails tries again later and raises the notice `cert_renew_failed` ([Notices](Notices.md)). **Stop renewing** keeps the certificate but stops the renewals.

## Requesting a certificate from a company

**Request from a company** makes a new key on this balancer, keeps it apart, and downloads the request (CSR) to send to your certificate company.

![Request a certificate from a company](Certificates/request.png)

Until their certificate comes back, the card shows **Request out**, with **Request (CSR)** to download the request again. Add the files they send with **Add certificate files**: CrushBalance pairs the certificate with the waiting key.

![A request waiting for the company's certificate](Certificates/request-card.png)

## A self-signed certificate

**Self-signed** makes a certificate for the names and addresses you type, with an EC or RSA key. It is for tests and for clients you tell to trust it; browsers warn about it.

![Make a self-signed certificate](Certificates/self-signed.png)

## Where the certificates are kept

In `certificates/` in the CrushBalance folder:
- each certificate in its own file, with a password derived from `crushbalance.key` (nobody types it);
- its earlier versions in `versions/`;
- removed ones in `backup/`;
- the ACME accounts and settings in `acme/`. EAB and TSIG secrets are sealed with `crushbalance.key`.

Back up `certificates/` and `crushbalance.key` together: the certificates open only with the key file they were written with ([Files](Files.md)).

No call leaves the balancer for certificates except to the ACME authority you chose, and, for **Fetch missing chain**, to the address in the certificate.
