# 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.

Mostly there is nothing to convert any more: drop the files as they are on the box under the list in [Certificates](Prefs-Certificates.md#adding-certificates). It reads every form in the table below, a zip of them too, matches the key to its certificate, completes the chain and keeps the result as a keystore of its own. The rest of this page is for the cases where you want a keystore file yourself, or files for another system. [Key store files](Certificates-Key-Store-Files.md) in Certificates, or OpenSSL where you have it, does that.

## 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.

**In Certificates:** drop `server.key`, `server.crt` and the bundle together on the box under the list, then **Save as a new certificate**. A key with a password is asked for.

**Into a key store file of your own:** in Key store files, open it and **Add a key and certificate** with both files. See [Key store files](Certificates-Key-Store-Files.md#what-is-inside).

**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. Drop the `.pfx` on the box in Certificates, or open it in **Key store files** with **Open a key store file**, with that password.

## 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. Drop the file on the box in Certificates and type the password when it is asked for. Or copy it to the CrushMFT server and open it in **Key store files** with **Open a key store file**.

## 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. **Copy with its key (PFX)** on a certificate's Details tab in Certificates writes one; a key store file of your own is turned into one with **Save as PKCS#12**.

## 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
```

Certificates reads the `.p7b` itself: drop it, and it is matched to the key it belongs to, such as the one waiting after a request to a certificate company. A certificate's Details tab also writes one, **Certificate (P7B)**.

## 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 |

**Key store files** shows the same for a key store, entry by entry. A certificate's tabs in Certificates show all of it, with the checks in words.

## 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 |
