To read everything in a certificate, run openssl x509 -in certificate.pem -text -noout. To turn a PFX file into PEM, run openssl pkcs12 -in filename.pfx -out certificate.pem -noenc. For an NSS database (the kind Firefox and other NSS-based apps use), use certutil for certificates and trust settings and pk12util to move certificate-plus-key bundles in and out.
Greetings, geeks! I’ve spent almost 20 years around Linux and security, and these are the OpenSSL and NSS commands I reach for most. Each one is below, with the options that trip people up.
Quick reference
| Task | Command |
|---|---|
| Show all certificate details | openssl x509 -in certificate.pem -text -noout |
| Check expiry dates | openssl x509 -in certificate.pem -dates -noout |
| Check a private key | openssl pkey -in privatekey.pem -check -noout |
| Get a server’s certificate | openssl s_client -connect host:port -showcerts </dev/null |
| PFX → PEM (cert + key) | openssl pkcs12 -in filename.pfx -out certificate.pem -noenc |
| PEM → PFX | openssl pkcs12 -export -in cert.pem -inkey key.pem -out bundle.pfx |
| List certs in NSS DB | certutil -L -d sql:/path/to/nssdb |
| Add a CA cert to NSS DB | certutil -A -n "My CA" -t "CT,C,C" -i ca.pem -d sql:/path/to/nssdb |
| Import PFX into NSS DB | pk12util -i filename.pfx -d sql:/path/to/nssdb |
| List keys in NSS DB | certutil -K -d sql:/path/to/nssdb |
OpenSSL or certutil: which one do you need?
- OpenSSL works on files. Use it for PEM, DER and PKCS#12/PFX certificates and keys, and to test TLS connections. It is an open-source TLS toolkit and general-purpose crypto library, widely used by servers and security tools.
- certutil and pk12util work on an NSS database. NSS (Network Security Services) is Mozilla’s security library set. Applications built on it, such as Firefox and Thunderbird, keep their certificates, keys and trust settings in an NSS database rather than in loose files.
You’ll often need both. A common job is to convert or inspect a file with OpenSSL, then load it into an NSS database with certutil or pk12util.
Inspect certificates and keys with OpenSSL
Show everything in a certificate
openssl x509 -in certificate.pem -text -nooutThe output shows the subject, issuer, validity, public key, extensions (SAN, key usage) and signature.
Subject, issuer and validity
openssl x509 -in certificate.pem -subject -issuer -nooutopenssl x509 -in certificate.pem -dates -noout-dates prints notBefore and notAfter. To test whether a certificate expires within the next 24 hours (86400 seconds), use:
openssl x509 -in certificate.pem -checkend 86400 -nooutThe exit code is 0 if it is still valid at that point and 1 if it will have expired, which makes it handy in scripts.
What a certificate may be used for
openssl x509 -in certificate.pem -purpose -nooutExample output, from a CA certificate:
Certificate purposes:SSL client : NoSSL client CA : YesSSL server : NoSSL server CA : YesNetscape SSL server : NoNetscape SSL server CA : YesS/MIME signing : NoS/MIME signing CA : YesS/MIME encryption : NoS/MIME encryption CA : YesCRL signing : NoCRL signing CA : YesAny Purpose : YesAny Purpose CA : YesOCSP helper : YesOCSP helper CA : YesTime Stamp signing : NoTime Stamp signing CA : YesCheck a private key
For an RSA key:
openssl rsa -in privatekey.pem -check -nooutFor any key type (RSA, EC and others):
openssl pkey -in privatekey.pem -check -nooutCheck that a key matches a certificate
Compare the public key inside the certificate with the one derived from the private key:
openssl x509 -in certificate.pem -noout -pubkey | openssl sha256openssl pkey -in privatekey.pem -pubout | openssl sha256If the two hashes are identical, the key belongs to the certificate.
Download a certificate from a remote server
openssl s_client -connect ldap.example.com:636 -servername ldap.example.com -showcerts </dev/null 2>/dev/null \ | sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' > /tmp/ldap.pemHow it works:
-showcertsprints every certificate the server sends, so/tmp/ldap.pemholds the whole chain.-servernamesends SNI. Without it, servers hosting several names may return the wrong certificate.</dev/nullcloses the connection as soon as the handshake is done.
To keep only the server’s own (leaf) certificate:
openssl s_client -connect ldap.example.com:636 -servername ldap.example.com </dev/null 2>/dev/null \ | openssl x509 -outform PEM > /tmp/ldap-leaf.pemDon’t force
-ssl3. SSLv3 is insecure and modern OpenSSL builds don’t support it, so the command just fails. Let OpenSSL negotiate TLS on its own.
PKCS#12 (PFX) file management
A PKCS#12 file (.pfx or .p12) is a password-protected binary bundle. It usually holds a certificate, its private key and often the CA chain.
| Task | Command |
|---|---|
| Convert PFX to PEM (cert + unencrypted key) | openssl pkcs12 -in filename.pfx -out certificate.pem -noenc |
| Print info about a PFX | openssl pkcs12 -info -in filename.pfx |
| Print info about an older PFX (OpenSSL 3.x) | openssl pkcs12 -info -in filename.pfx -legacy |
| Extract client certificate + key (key stays encrypted) | openssl pkcs12 -in filename.pfx -clcerts -out clientcert.pem |
| Extract only the client certificate | openssl pkcs12 -in filename.pfx -clcerts -nokeys -out clientcert.pem |
| Extract the unencrypted private key | openssl pkcs12 -in filename.pfx -nocerts -noenc -out privatekey.pem |
| Extract the CA certificates | openssl pkcs12 -in filename.pfx -cacerts -nokeys -out cacert.pem |
| Build a PFX from PEM files | openssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -certfile cacert.pem -name "my cert" -out bundle.pfx |
Notes:
-noencvs-nodes. Both write the private key without a passphrase. OpenSSL 3.x prefers-noenc.-nodesis the older spelling and still works.- Protect unencrypted keys. Run
chmod 600 privatekey.pemand delete the file once you’re done with it. - When to use
-legacy. Many older PFX files use algorithms that OpenSSL 3.x no longer loads by default, such as RC2-40. If reading the file fails with an “unsupported” algorithm error, add-legacy.
NSS database basics
The NSS database is where NSS-based applications store certificates, private keys and trust settings. You point the tools at the directory that contains it with -d.
What’s inside
| Part | Holds | File (SQL format) | File (old DBM format) |
|---|---|---|---|
| Certificate DB | User, server and CA certificates plus trust flags | cert9.db | cert8.db |
| Key DB | Private keys for those certificates | key4.db | key3.db |
| Module config | PKCS#11 modules (smart cards, hardware security modules) | pkcs11.txt | secmod.db |
The sql: prefix in -d sql:/path/to/nssdb selects the SQLite-based format, which current NSS uses by default.
The tools
- certutil: create the database, and add, list, modify, export and delete certificates and keys.
- pk12util: import and export certificate-plus-key bundles in PKCS#12 format.
- modutil: manage PKCS#11 modules.
Manage an NSS database with certutil and pk12util
Create a new database
certutil -N -d sql:/path/to/nssdbYou’ll be asked for a password that protects the key database. Add --empty-password if you deliberately want none.
List certificates and their trust
certutil -L -d sql:/path/to/nssdbAdd a certificate (certificate only)
certutil -A -n "My CA" -t "CT,C,C" -i ca.pem -d sql:/path/to/nssdb-n is the nickname you’ll use to refer to the certificate later. certutil -A imports only the certificate, never a private key. To bring a key along, see Import a certificate with its private key below.
Trust flags (-t)
Trust is written as three comma-separated positions, always in this order: SSL/TLS, email, object signing. In each position, use none, any or all of these codes:
p: valid peerP: trusted peer (impliesp)c: valid CAC: trusted CA (impliesc)T: trusted CA for client authentication (SSL server only)
Common combinations:
| Trust string | Meaning |
|---|---|
"CT,C,C" | CA trusted for TLS (server and client certs), email and object signing |
"C,," | CA trusted for TLS server certificates only |
"P,," | Trust this specific server/peer certificate for TLS |
",," | Store the certificate with no explicit trust; it’s validated through its chain |
To change trust on a certificate that’s already in the database:
certutil -M -n "My CA" -t "C,," -d sql:/path/to/nssdbAbout the
uflag.certutil -Lsometimes showsuin the trust column, for exampleu,u,u. It means the database holds a private key for that certificate. NSS sets it on its own, and you can’t set it with-t, so leave it out of your trust strings.
Import a certificate with its private key
pk12util takes PKCS#12, so a PFX goes straight in:
pk12util -i filename.pfx -d sql:/path/to/nssdbIf you have separate PEM files, bundle them with OpenSSL first, then import:
openssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -name "my cert" -out bundle.pfxpk12util -i bundle.pfx -d sql:/path/to/nssdbThe -name becomes the certificate’s nickname in the database.
Export from the database
Export a certificate as PEM:
certutil -L -n "certificate name" -d sql:/path/to/nssdb -a > certificate.pemExport the certificate and key as PKCS#12:
pk12util -o export.p12 -n "certificate name" -d sql:/path/to/nssdbList private keys
certutil -K -d sql:/path/to/nssdbDelete a certificate or a key
Delete a certificate:
certutil -D -n "certificate name" -d sql:/path/to/nssdbDelete a private key:
certutil -F -n "certificate name" -d sql:/path/to/nssdb-D removes the certificate. Use -F when you need the private key gone.
FAQ
What’s the difference between PEM and PFX?
PEM is Base64 text wrapped in -----BEGIN …----- / -----END …----- lines, and one file can hold one or more certificates or keys. PFX (PKCS#12) is a binary, password-protected bundle that usually carries a certificate, its private key and the CA chain together.
Why does openssl pkcs12 fail on an old PFX with OpenSSL 3?
The file most likely uses an algorithm OpenSSL 3.x doesn’t load by default. Add -legacy to the command.
Can certutil import a private key?
No. certutil -A adds certificates only. Put the certificate and key into a PKCS#12 file and import it with pk12util -i.
What does sql: mean in -d sql:/path/to/nssdb?
It tells the NSS tools to use the SQLite database format (cert9.db, key4.db). That’s the current default, as opposed to the older DBM format (cert8.db, key3.db).
What does the u in certutil -L output mean?
The database holds a private key for that certificate. It’s set automatically and can’t be set with -t.
That’s the toolkit. See you next time!
[keep: comments]