GeekCoding101 make your way to geek

OpenSSL & certutil Cheat Sheet: Certs, PFX, NSS DB

OpenSSL and certutil commands: inspect certificates, convert PFX to PEM, fetch a server cert, and import, trust or export certs in an NSS database.

8 min read
Mastering Openssl Command And Nss Database Management
On this pageQuick reference

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

TaskCommand
Show all certificate detailsopenssl x509 -in certificate.pem -text -noout
Check expiry datesopenssl x509 -in certificate.pem -dates -noout
Check a private keyopenssl pkey -in privatekey.pem -check -noout
Get a server’s certificateopenssl s_client -connect host:port -showcerts </dev/null
PFX → PEM (cert + key)openssl pkcs12 -in filename.pfx -out certificate.pem -noenc
PEM → PFXopenssl pkcs12 -export -in cert.pem -inkey key.pem -out bundle.pfx
List certs in NSS DBcertutil -L -d sql:/path/to/nssdb
Add a CA cert to NSS DBcertutil -A -n "My CA" -t "CT,C,C" -i ca.pem -d sql:/path/to/nssdb
Import PFX into NSS DBpk12util -i filename.pfx -d sql:/path/to/nssdb
List keys in NSS DBcertutil -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

Terminal window
openssl x509 -in certificate.pem -text -noout

The output shows the subject, issuer, validity, public key, extensions (SAN, key usage) and signature.

Subject, issuer and validity

Terminal window
openssl x509 -in certificate.pem -subject -issuer -noout
openssl 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:

Terminal window
openssl x509 -in certificate.pem -checkend 86400 -noout

The 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

Terminal window
openssl x509 -in certificate.pem -purpose -noout

Example output, from a CA certificate:

Certificate purposes:
SSL client : No
SSL client CA : Yes
SSL server : No
SSL server CA : Yes
Netscape SSL server : No
Netscape SSL server CA : Yes
S/MIME signing : No
S/MIME signing CA : Yes
S/MIME encryption : No
S/MIME encryption CA : Yes
CRL signing : No
CRL signing CA : Yes
Any Purpose : Yes
Any Purpose CA : Yes
OCSP helper : Yes
OCSP helper CA : Yes
Time Stamp signing : No
Time Stamp signing CA : Yes

Check a private key

For an RSA key:

Terminal window
openssl rsa -in privatekey.pem -check -noout

For any key type (RSA, EC and others):

Terminal window
openssl pkey -in privatekey.pem -check -noout

Check that a key matches a certificate

Compare the public key inside the certificate with the one derived from the private key:

Terminal window
openssl x509 -in certificate.pem -noout -pubkey | openssl sha256
openssl pkey -in privatekey.pem -pubout | openssl sha256

If the two hashes are identical, the key belongs to the certificate.

Download a certificate from a remote server

Terminal window
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.pem

How it works:

  • -showcerts prints every certificate the server sends, so /tmp/ldap.pem holds the whole chain.
  • -servername sends SNI. Without it, servers hosting several names may return the wrong certificate.
  • </dev/null closes the connection as soon as the handshake is done.

To keep only the server’s own (leaf) certificate:

Terminal window
openssl s_client -connect ldap.example.com:636 -servername ldap.example.com </dev/null 2>/dev/null \
| openssl x509 -outform PEM > /tmp/ldap-leaf.pem

Don’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.

TaskCommand
Convert PFX to PEM (cert + unencrypted key)openssl pkcs12 -in filename.pfx -out certificate.pem -noenc
Print info about a PFXopenssl 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 certificateopenssl pkcs12 -in filename.pfx -clcerts -nokeys -out clientcert.pem
Extract the unencrypted private keyopenssl pkcs12 -in filename.pfx -nocerts -noenc -out privatekey.pem
Extract the CA certificatesopenssl pkcs12 -in filename.pfx -cacerts -nokeys -out cacert.pem
Build a PFX from PEM filesopenssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -certfile cacert.pem -name "my cert" -out bundle.pfx

Notes:

  • -noenc vs -nodes. Both write the private key without a passphrase. OpenSSL 3.x prefers -noenc. -nodes is the older spelling and still works.
  • Protect unencrypted keys. Run chmod 600 privatekey.pem and 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

PartHoldsFile (SQL format)File (old DBM format)
Certificate DBUser, server and CA certificates plus trust flagscert9.dbcert8.db
Key DBPrivate keys for those certificateskey4.dbkey3.db
Module configPKCS#11 modules (smart cards, hardware security modules)pkcs11.txtsecmod.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

Terminal window
certutil -N -d sql:/path/to/nssdb

You’ll be asked for a password that protects the key database. Add --empty-password if you deliberately want none.

List certificates and their trust

Terminal window
certutil -L -d sql:/path/to/nssdb

Add a certificate (certificate only)

Terminal window
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 peer
  • P: trusted peer (implies p)
  • c: valid CA
  • C: trusted CA (implies c)
  • T: trusted CA for client authentication (SSL server only)

Common combinations:

Trust stringMeaning
"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:

Terminal window
certutil -M -n "My CA" -t "C,," -d sql:/path/to/nssdb

About the u flag. certutil -L sometimes shows u in the trust column, for example u,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:

Terminal window
pk12util -i filename.pfx -d sql:/path/to/nssdb

If you have separate PEM files, bundle them with OpenSSL first, then import:

Terminal window
openssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -name "my cert" -out bundle.pfx
pk12util -i bundle.pfx -d sql:/path/to/nssdb

The -name becomes the certificate’s nickname in the database.

Export from the database

Export a certificate as PEM:

Terminal window
certutil -L -n "certificate name" -d sql:/path/to/nssdb -a > certificate.pem

Export the certificate and key as PKCS#12:

Terminal window
pk12util -o export.p12 -n "certificate name" -d sql:/path/to/nssdb

List private keys

Terminal window
certutil -K -d sql:/path/to/nssdb

Delete a certificate or a key

Delete a certificate:

Terminal window
certutil -D -n "certificate name" -d sql:/path/to/nssdb

Delete a private key:

Terminal window
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]

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts