Is the key encrypted?

The first line of the PEM file gives it away:

-----BEGIN PRIVATE KEY----- → unencrypted (PKCS#8) -----BEGIN RSA PRIVATE KEY----- → unencrypted (legacy RSA format) -----BEGIN EC PRIVATE KEY----- → unencrypted (legacy EC format) -----BEGIN ENCRYPTED PRIVATE KEY----- → encrypted (PKCS#8) -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED → encrypted (legacy format)

head -2 privkey.pem is enough to check.

Removing the passphrase

openssl pkey -in privkey-encrypted.pem -out privkey.pem

openssl asks for the passphrase once and writes the key unencrypted in PKCS#8 format. openssl pkey works for RSA, EC and Ed25519 alike — the formerly common openssl rsa -in … -out … only handles RSA.

If an old program explicitly wants the BEGIN RSA PRIVATE KEY format:

openssl rsa -in privkey-encrypted.pem -traditional -out privkey-rsa.pem

Setting a passphrase

For storage in a backup or transport by mail the opposite direction makes sense:

openssl pkey -in privkey.pem -aes256 -out privkey-encrypted.pem

If the key arrives as a PFX there is no intermediate step: -noenc when extracting delivers it unencrypted directly, see Create and extract a PFX file.

Protecting the unencrypted key

Without a passphrase the file itself is the secret. Therefore:

  1. Set permissions before the content goes in. umask 077 before the openssl command, or chmod 600 privkey.pem right after. Otherwise the file is briefly world-readable between writing and chmod.
  2. Check the owner. Postfix and Dovecot read the key as root before dropping privileges — root:root with 600 is enough. nginx too. If a service group needs access: 640 with exactly that group.
  3. The encrypted copy does not belong next to it. An encrypted and an unencrypted version in the same directory protect nothing.
  4. Store it encrypted in the backup — that is where the passphrase makes sense, because nothing has to start unattended.

The start-up message when the key is still encrypted

Postfix then logs cannot get RSA private key from file "…": disabling TLS support (with smtpd_tls_chain_files: error loading private key) and switches TLS off: senders deliver unencrypted or get 454 4.7.0 TLS not available. nginx does not start at all or hangs at the prompt. After removing the passphrase, reload the service and check with openssl s_client that the new certificate is being served.

Verify your work

A test mail through your mail server to hello@analyzemy.email shows how it looks from outside: TLS path, SPF, DKIM, DMARC and blacklists in one report.

Analyze your email now

Last updated: · All guides