Build a local certificate authority

Generate a private key

Create a modern private key, inspect only its public properties, and protect the secret material.

Everything in this module rests on one secret: a private key. The certificate, the signatures, the trust decisions. All of them assume that only you control this one file. A private key is proof of control over an identity. Generate it on the machine where it will live, and never copy it through chat, logs, or a Git repository.

We start with the key for our lab certificate authority. Let’s create it, encrypted:

openssl genpkey -algorithm RSA -aes-256-cbc -out lab-ca.key -pkeyopt rsa_keygen_bits:3072

OpenSSL asks for a passphrase and writes the key encrypted with AES-256. -pkeyopt rsa_keygen_bits:3072 picks a 3072-bit RSA key, a good size for a CA key that will sign other certificates. The passphrase means a stolen file is not an instant stolen identity.

Now check the file exists and that only you can read it:

chmod 600 lab-ca.key
ls -l lab-ca.key
# -rw-------  1 flavio  staff  2666 Sep  8 18:45 lab-ca.key

-rw------- means the owner can read and write, and nobody else can do anything. OpenSSL 3 already writes private keys with these permissions, but I run chmod 600 anyway. On a shared machine that line is the difference between a secret and a leak.

Let’s confirm the key is well formed:

openssl pkey -in lab-ca.key -check -noout
# Key is valid

You’ll be asked for the passphrase, then OpenSSL checks the key’s internal consistency. Key is valid is what you want. Notice that this command never prints the key material. That’s how you should always inspect keys: look at properties, never at contents.

If you’re curious what the file looks like, head -1 lab-ca.key shows -----BEGIN ENCRYPTED PRIVATE KEY-----. The word ENCRYPTED confirms the passphrase is in effect.

A realistic failure. You generate the key as root during setup, then the server runs as another user and can’t read it. The symptom is a permission denied error at startup, not a TLS error. Check ownership with ls -l before you suspect the key itself.

One note on the passphrase. An encrypted server key needs someone or something to type the passphrase at every restart. That’s a real operational cost. Never strip the encryption just because automation got annoying. Decide where the secret lives first, then choose whether the file on disk is encrypted.

Lesson completed