# Converting keys and certificates

CrushMFT reads its certificates from a **keystore**: one file that holds the private key, your certificate, and the certificates of the authority that signed it. It reads two kinds: Java keystores (`.jks`) and PKCS#12 files (`.pfx` or `.p12`). Certificates from elsewhere often come in other shapes. This page is about getting from those to a keystore.

The server's own tool for it is [the keystore utility](Prefs-Encryption-Keystore-Utility.md); nothing has to be installed. OpenSSL, where you have it, does the same from a command line.

## What the files are

| File | Holds | Looks like |
|---|---|---|
| `.key` | a private key | text that begins `-----BEGIN PRIVATE KEY-----` or `-----BEGIN RSA PRIVATE KEY-----` |
| `.crt`, `.cer`, `.pem` | one certificate, or several in a row | text that begins `-----BEGIN CERTIFICATE-----`; or the same in binary |
| `.ca-bundle`, `chain.pem` | the authority's certificates | several certificates in a row |
| `.csr` | a request for a certificate. No use after the certificate is issued. | `-----BEGIN CERTIFICATE REQUEST-----` |
| `.pfx`, `.p12` | a key with its certificates, in one file, with a password | binary |
| `.jks` | a Java keystore: any number of keys and certificates, with a password | binary |
| `.p7b` | certificates only, no key | text or binary |

The private key is the secret. Every other file can be shown to anybody.

## From two files

You have `server.key` and `server.crt`, as Apache or nginx use them, and perhaps a bundle of the authority.

**With the keystore utility:** open it from the SSL tab, make or open a keystore, and import the key together with its certificate. Then import the authority's certificates. See [From two files to a keystore](Prefs-Encryption-Keystore-Utility.md#from-two-files-to-a-keystore).

**With OpenSSL:** make a PKCS#12 file, which the server reads as it is:

```
openssl pkcs12 -export -inkey server.key -in server.crt -certfile ca-bundle.crt -name files.example.com -out files_example_com.pfx
```

It asks for a password for the new file. Point the server at the `.pfx` with that password: [SSL certificates and the guide](Prefs-Encryption-SSL.md#the-server-certificate-card).

## From IIS

A certificate of a Windows web server can be exported with its key and used directly.

1. Start `mmc` and add the snap-in **Certificates**, for the **computer account** of the local computer.
2. Under **Personal › Certificates**, right-click the certificate: **All Tasks › Export**.
3. **Yes, export the private key.** Without the key the file is useless.
4. Format PKCS #12 (`.pfx`); tick **Include all certificates in the certification path** and **Export all extended properties**. Do **not** tick "Delete the private key".
5. Protect it with a **password**, not with a Windows account.
6. Copy the file to the CrushMFT server and point the server at it, with the password.

## From a keystore to files

Another system wants your certificate as two files:

```
openssl pkcs12 -in files_example_com.pfx -nocerts -nodes -out server.key
openssl pkcs12 -in files_example_com.pfx -clcerts -nokeys -out server.crt
openssl pkcs12 -in files_example_com.pfx -cacerts -nokeys -out ca-bundle.crt
```

The first writes the private key **unprotected**. Delete it when it is no longer needed, and never mail it.

## Between the two keystore kinds

```
keytool -importkeystore -srckeystore files.jks -destkeystore files.pfx -deststoretype PKCS12
keytool -importkeystore -srckeystore files.pfx -srcstoretype PKCS12 -destkeystore files.jks
```

There is rarely a need: the server reads both. PKCS#12 is the form every other program reads too.

## A key in an old form

Some tools want a private key in the form that begins `-----BEGIN PRIVATE KEY-----` (PKCS#8) and are given one that begins `-----BEGIN RSA PRIVATE KEY-----`:

```
openssl pkcs8 -topk8 -nocrypt -in old.key -out new.key
```

## Certificates only: `.p7b`

A `.p7b` from an authority holds your certificate and the chain, without the key:

```
openssl pkcs7 -print_certs -in certificate.p7b -out certificates.pem
```

Then import the result as the reply into the keystore that holds the key: **Step 3: Import Reply**, see [Getting and installing certificates](Security-SSL-Certificates.md#3-bring-in-the-answer).

## Looking into a file

| To see | Run |
|---|---|
| a certificate's names and dates | `openssl x509 -in server.crt -noout -subject -issuer -dates -ext subjectAltName` |
| what a `.pfx` holds | `openssl pkcs12 -in file.pfx -info -nokeys` |
| what a `.jks` holds | `keytool -list -v -keystore file.jks` |
| whether a key and a certificate belong together | compare `openssl x509 -in server.crt -noout -pubkey` with `openssl pkey -in server.key -pubout`: the two outputs must be the same |

The keystore utility shows the same for a keystore, and **Test Certificate** on the SSL tab tells whether the server can open it.

## What goes wrong

| Sign | Cause |
|---|---|
| the server cannot open the keystore | the password; or the file is not `.jks` or PKCS#12 at all |
| it opens, and browsers see no chain | the authority's certificates are not in the keystore |
| "key password" errors | the key inside has another password than the file. Give both to the server, or export again with one password. |
| the certificate does not fit the key | the certificate was issued for another request. Find the keystore the request was made from. |
| a `.pfx` exported from Windows cannot be read | it was protected with a Windows account instead of a password, or exported without the private key |
