# AS2 certificates

AS2 rests on certificates. Each side has a **key pair**: a private key that it keeps, and a certificate, the public half, that it gives to the other side.

| Key | Used to |
|---|---|
| your private key | decrypt what partners send you; sign what you send, and your receipts |
| your certificate, held by the partner | encrypt what they send you; check your signatures |
| the partner's certificate, held by you | encrypt what you send them; check their signatures and receipts |

You never see the partner's private key, and they never see yours. If a partner asks for your private key, they are asking for the wrong file.

## Making your key pair

A certificate for AS2 does not have to be bought. Partners trust it because you gave it to them, not because an authority signed it. A self-signed certificate is normal.

Make it with the [keystore utility](Prefs-Encryption-Keystore-Utility.md) of the server, or with any tool that makes keystores:

| Choose | |
|---|---|
| kind of key | RSA, 2048 bits or more. Every AS2 product can use it. |
| validity | two or three years. Longer means fewer changes; shorter means less harm from a lost key. |
| the name in it | your company and "AS2", so that partners recognise it in their lists |
| keystore format | PKCS12 (`.p12`, `.pfx`) or JKS |
| passwords | one for the keystore, and the same for the key. JKS stores always use one for both. |

Put the keystore file somewhere on the server that only the server reads, outside every user's folders.

Then export the **certificate alone**, without the private key, as a `.cer` or `.pem` file. That is the file for partners.

## Exchanging certificates

1. Send the partner your certificate file. Mail is fine: it is public.
2. Get theirs.
3. Check theirs by another way than it came: read the **fingerprint** to each other on the phone. A certificate that was swapped on the way lets somebody else read your files.
4. Import theirs into a keystore, under a name that says whose it is, with the keystore utility. One keystore can hold the certificates of all partners.

## Where the keys are named

| Engine | Your key pair | The partner's certificate |
|---|---|---|
| version 1, receiving | **AS2/3 Decryption Key** of the partner's user: [A user's keys and certificates](Users-Settings-Keys.md). It also signs the receipts you send. | **AS2/3 Signature Verification Key** of the same user |
| version 1, sending | **Signing** in the [AS2 task](Task-AS2.md) | **Encryption** in the task. The same certificate checks the partner's receipts. |
| version 2 | the **station**: its decryption keys and its receipt key, each a keystore path, an alias and passwords | the **partner** entry: the certificates it may sign with, and the one to encrypt for |

In every case the server reads the keystore file where it lies, by its path and the name (alias) of the key in it. Nothing is copied into the server's settings but the path and the passwords, which are stored encrypted.

Use **Test Certificate** on the user's keys: a wrong alias is the most common reason for a message that cannot be decrypted.

## One key or two

You can use one key pair for everything, and most do. Two things to know:

- **Version 1 signs receipts with the decryption key.** A partner who receives your receipts must check them against the same certificate they encrypt with. That is what they expect anyway.
- **Version 2 can use separate keys** for decrypting and for signing receipts, and several decryption keys at once, which makes a change of key smooth.

## When a certificate ends

A certificate has an end date. The day after, the partner's system refuses it, and files stop.

Change it before:

1. A month ahead, make a new key pair.
2. Send partners the new certificate, with the date from which you will use it.
3. On that date, switch the keystore alias on your side.
4. Keep the old key until every partner has confirmed. In version 2, list both as decryption keys during the change, so that messages encrypted for either are read. In version 1 there is one key per user: agree the switch date exactly, or make a second user for the change.

The same in the other direction: when a partner sends a new certificate, import it under a new alias, and switch on the agreed date.

Put every end date, yours and theirs, into a calendar. Version 2 lists certificates that end soon in its health answer; for version 1, look into the keystores with the keystore utility.

## Things to know

- **Back up the keystore and its password**, apart from the server. Without the private key, nothing that partners send can be read, and every partner must be given a new certificate.
- **In a cluster**, the keystore file must be at the same path on every server.
- **With a DMZ server**, version 1 hands the keys to the DMZ server's memory, because the message is decrypted there. Version 2 never does: it runs on the internal server only.
- **In [FIPS mode](FIPS-Mode.md)**, keystores are read in the form the validated module uses, and their passwords must be 14 characters or longer.
- **Certificates from an authority** work as well as self-signed ones. Some partners require them by policy; the server does not care.
