TExcelTAD
Home › Linux Server Administration › Certificates, CSRs & a Private CA

Certificates, CSRs & a Private CA

TLS explained what certificates are for: a server proves its identity with a certificate that a trusted certificate authority (CA) signed. As an admin you deal with the files: private keys, certificate signing requests (CSRs), certificates, chains, and the occasional private CA for internal services. openssl does all of it, and everything in this lesson runs for real in the sandbox.

The pieces

File Contains Secret?
private key (app.key) the key pair's private half yes: mode 600, never leaves the server, never in Git
CSR (app.csr) public key + requested names, signed with the private key no
certificate (app.crt) public key + names + validity + issuer's signature no
chain / intermediate (chain.crt) the CA certificates between yours and a trusted root no
full chain (fullchain.pem) your certificate followed by the intermediates: what most servers want no

All of them are usually PEM files: base64 between -----BEGIN …----- lines. Several certificates can sit in one PEM file, one after the other.

A private CA in five commands

Public CAs only sign names on the public internet. For internal services (db.internal, a staging environment, mutual TLS between services) teams run a private CA. Production setups use tooling (step-ca, HashiCorp Vault's PKI engine, AWS Private CA, cert-manager); the mechanics are the same as here:

bash · try it yourself
# 1. The root CA: a key and a self-signed certificate (in real life: offline, hardware-protected)
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
  -keyout ca.key -out ca.crt -days 3650 -subj "/O=TAD Lab/CN=TAD Lab Root CA" 2>/dev/null
# 2. The server's key and a CSR with its names (subjectAltName is what clients check)
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
  -keyout app.key -out app.csr -subj "/CN=app.example.test" \
  -addext "subjectAltName=DNS:app.example.test,DNS:www.example.test,IP:10.0.0.20" 2>/dev/null
openssl req -in app.csr -noout -verify -subject 2>&1
# 3. The CA decides what goes into the certificate (not the CSR): names, usage, lifetime
cat > server-ext.cnf <<'EOF'
basicConstraints = critical, CA:FALSE
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
subjectAltName = DNS:app.example.test, DNS:www.example.test, IP:10.0.0.20
EOF
openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -set_serial 0x1001 -days 90 \
  -extfile server-ext.cnf -out app.crt 2>&1
# 4. Inspect and verify
openssl x509 -in app.crt -noout -subject -issuer -serial -ext subjectAltName,extendedKeyUsage,basicConstraints
openssl verify -CAfile ca.crt app.crt
stat -c '%a %n' ca.key app.key
Certificate request self-signature verify OK
subject=CN = app.example.test
Certificate request self-signature ok
subject=CN = app.example.test
subject=CN = app.example.test
issuer=O = TAD Lab, CN = TAD Lab Root CA
serial=1001
X509v3 Basic Constraints: critical
    CA:FALSE
X509v3 Extended Key Usage:
    TLS Web Server Authentication
X509v3 Subject Alternative Name:
    DNS:app.example.test, DNS:www.example.test, IP Address:10.0.0.20
app.crt: OK
600 ca.key
600 app.key

Points worth noticing:

  • -noenc (OpenSSL 3; older versions use -nodes) leaves the key unencrypted, which a server needs to start unattended. Protect it with file permissions instead: OpenSSL already wrote it as 600.
  • Names live in the subjectAltName. Browsers have ignored the Common Name (CN) for host-name matching for years, so a certificate without SANs fails there. OpenSSL-based clients such as curl still fall back to the CN when there is no SAN at all, which is how CN-only certificates survive testing with curl and then fail in browsers. Always set SANs.
  • The CA, not the requester, decides the extensions. Signing with -copy_extensions copy would accept whatever names the CSR asks for, which is how a careless internal CA ends up issuing a certificate for *.example.com to a test box.
  • CA:FALSE and serverAuth limit what the certificate can be used for: it can't sign other certificates, and it is only valid for TLS servers.
  • Elliptic-curve keys (P-256 here) are smaller and faster than RSA; RSA 2048/3072 is still everywhere and fine.

Chains and intermediates

Real CAs don't sign servers with the root key; they use intermediate CAs, and the server must send its certificate plus the intermediates (the root is already in the clients' trust store). A missing intermediate is the most common TLS misconfiguration: browsers often paper over it (they cache or fetch intermediates), while curl, Java, Python and mobile apps fail. The session builds a root → intermediate → server chain and shows both outcomes:

bash · try it yourself
k() { openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$1" 2>/dev/null; }
k root.key; k inter.key; k srv.key
openssl req -x509 -new -key root.key -out root.crt -days 3650 -subj "/CN=Example Root" 2>/dev/null
openssl req -new -key inter.key -subj "/CN=Example Issuing CA 1" 2>/dev/null |
  openssl x509 -req -CA root.crt -CAkey root.key -set_serial 2 -days 1825 -out inter.crt \
    -extfile <(printf 'basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n') 2>/dev/null
openssl req -new -key srv.key -subj "/CN=shop.example.test" 2>/dev/null |
  openssl x509 -req -CA inter.crt -CAkey inter.key -set_serial 3 -days 90 -out srv.crt \
    -extfile <(printf 'subjectAltName=DNS:shop.example.test\nextendedKeyUsage=serverAuth\n') 2>/dev/null
cat srv.crt inter.crt > fullchain.pem
echo "--- server certificate alone:"
openssl verify -CAfile root.crt srv.crt
echo "--- with the intermediate:"
openssl verify -CAfile root.crt -untrusted inter.crt srv.crt
echo "--- what fullchain.pem contains:"
openssl crl2pkcs7 -nocrl -certfile fullchain.pem | openssl pkcs7 -print_certs -noout | grep subject
--- server certificate alone:
CN = shop.example.test
error 20 at 0 depth lookup: unable to get local issuer certificate
error srv.crt: verification failed
--- with the intermediate:
srv.crt: OK
--- what fullchain.pem contains:
subject=CN = shop.example.test
subject=CN = Example Issuing CA 1

fullchain.pem (leaf first, then intermediates) is what goes into Nginx's ssl_certificate; Let's Encrypt's certbot writes exactly that file.

Does this key belong to this certificate?

After a renewal, people often end up with a new certificate and the old key (or the other way round), and the server refuses to start with key values mismatch. Compare the public keys:

bash · try it yourself
for n in a b; do openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout $n.key -out $n.crt -days 30 -subj "/CN=$n" 2>/dev/null; done
pub_of_cert() { openssl x509 -in "$1" -noout -pubkey | openssl sha256 | cut -d' ' -f2; }
pub_of_key()  { openssl pkey -in "$1" -pubout | openssl sha256 | cut -d' ' -f2; }
for pair in "a.crt a.key" "a.crt b.key"; do
  set -- $pair
  if [ "$(pub_of_cert $1)" = "$(pub_of_key $2)" ]; then echo "$1 + $2: match"; else echo "$1 + $2: MISMATCH"; fi
done
a.crt + a.key: match
a.crt + b.key: MISMATCH

The same comparison works for a CSR (openssl req -in x.csr -noout -pubkey).

Formats you will be handed

Format Looks like Convert
PEM -----BEGIN CERTIFICATE----- text the default for Linux servers
DER binary .der/.cer openssl x509 -in x.der -inform der -out x.pem
PKCS#12 binary .p12/.pfx: key + certificate + chain, password-protected (Windows, Java) openssl pkcs12 -in x.p12 -out all.pem -noenc and back with openssl pkcs12 -export

To make a private CA trusted by a Linux machine's tools (curl, apt, Python's certifi aside), copy its certificate to /usr/local/share/ca-certificates/tad-lab.crt and run sudo update-ca-certificates (Debian/Ubuntu) or use /etc/pki/ca-trust/source/anchors/ and update-ca-trust (RHEL).

Test yourself

Which part of a certificate do clients check against the host name?

  1. The Common Name only
  2. The subjectAltName extension
  3. The serial number
  4. The issuer

openssl verify -CAfile root.crt srv.crt fails with unable to get local issuer certificate, but works with -untrusted inter.crt. What does the web server need?

  1. A new root CA
  2. To send the intermediate certificate too: use the full chain (leaf + intermediates) as its certificate file
  3. A longer key
  4. A different serial number

Why should a CA set the extensions itself instead of using -copy_extensions copy?

  1. It is faster
  2. Otherwise the requester chooses the names (and other extensions) that end up in a trusted certificate
  3. copy_extensions doesn't exist
  4. Extensions are optional