OpenSSL: common commands (and some others)

❝I'm writing this post for completely selfish reasons.❞
Contents

I’m writing this post for completely selfish reasons. I’ve caught myself looking up openssl commands from my previous SecureBoot-related posts, probably more than 10 times now. OpenSSL is rather amazing, but I have so little working memory of the command-line arguments, that I need to look up virtually every command. Now I’m writing down a bunch that I’ve needed recently.

This isn’t going to look pretty or have much in terms of clarification. I will likely update it in time, as I need other commands.

Generating certificates for server and clients

Generating certificates for a CA, a server and some clients, for any purpose that supports IEEE-802.1x.

# List available curves.
openssl ecparam -list_curves

# Generate a self-signed CA key and certificate.
openssl ecparam -name secp384r1 -genkey -noout -out CA.key
openssl req -new -x509 -subj "/CN=CA/" -key CA.key -out CA.crt -days 3650 -sha256

# Generate a (server/client) key.
openssl ecparam -name secp224r1 -genkey -out server.key

# Generate a certificate (signing request).
openssl req -new -sha256 -key server.key -out server.csr -subj '/CN=server/' -noenc
openssl req -in server.csr -noout -text

# Sign a certificate signing request (for server-role, according to IEEE-802.1x) on the machine maintaining the CA key.
openssl x509 -req -CA CA.crt -CAkey CA.key -subj '/CN=server/' -addtrust serverAuth -addreject clientAuth -days 3650 -sha256 -in server.csr -out server.crt
openssl x509 -in server.crt -noout -text

# Sign a certificate signing request (for client-role, according to IEEE-802.1x) on the machine maintaining the CA key.
openssl x509 -req -CA CA.crt -CAkey CA.key -subj '/CN=client/' -addtrust clientAuth -addreject serverAuth -days 3650 -sha256 -in client.csr -out client.crt
openssl x509 -in client.crt -noout -text

Note that this uses subcommand x509, thus avoiding the need for an openssl.cnf configuration-file that contains a selection of prescribed CA-related fields and values. For regular or long-term use, subcommand ca and an openssl.cnf file is probably better. In these one-off cases, I like to be able to generate certificates using the info I provide in command-line arguments.

Combine private key, certificate, and CA-certificate in PKCS12

Generate a PKCS12 (.pfx) for easy transport and import. This is accepted, for example, by Android-devices.

# Generate a PKCS12 (`.pfx`) file composed of private key and certificates.
# `-certfile <cert-file>` may be repeated to include other (intermediate) certificates.
openssl pkcs12 -in client.crt -inkey client.key -certfile CA.crt -export -out client.pfx

Generate an RSA key-pair

# Generate a 2048-bit RSA key-pair for use in SecureBoot.
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=PK/" -keyout PK.key -out PK.crt -days 3650 -nodes -sha256

Check your UEFI-firmware or TPM for supported algorithms. These often have a select few supported algorithms and key-sizes, usually 2048-bit RSA

Convert certificate-formats

# Convert a PEM-formatted certificate into a DER-formatted certificate.
# (You can include `inform PEM` to be explicit.)
openssl x509 -in test.crt -out test.der -outform DER

Generate a safe DH parameter for use in cryptographic applications.

Some security applications, such as OpenVPN, allow providing a (custom) DH-parameter for use in establishing ephemeral session-keys. (Usually, without such a DH-parameter, unless standardized DH-parameters are used, it means no ephemeral session-keys are established.)

# Generate a DH parameter, with `3072` the parameter size.
openssl dhparam -out dh.pem 3072

IIUC, this is used mainly in combination with RSA and necessarily with DSA algorithms.

Other tools

Some notes for other tooling.

tpm2-tools

# Check available subcommands.
tpm2_getcap -l
# Check TPM2 supported capabilities.
tpm2_getcap algorithms
# Check TPM2 supported ECC curves.
tpm2_getcap ecc-curves

These are low-level fundamental tools for interacting with TPM2. These are not recommended for any practical use. There are high-level tools available for that, as well as integration in security tooling such as GnuPG.

efitools

# Read an EFI-firmware SecureBoot variable.
efi-readvar -v db
# Unlock and update an EFI-firmware SecureBoot variable.
chattr -i /sys/firmware/efi/efivars/db-...
# Key needs only be specified if SecureBoot is in setup-mode (keys cleared).
efi-updatevar -a -k KEK.key -c db.crt db
# Update/Write PK certificate, usually must be signed auth-file.
cert-to-efi-sig-list PK.crt PK.esl
sign-efi-sig-list -k PK.key PK PK.esl PK.auth
efi-updatevar -f PK.auth PK
# An empty, i.e. 0-byte, PK.esl that is signed into an PK.auth can be used to clear the SecureBoot PK variable from a running system in (SecureBoot-enabled) user-mode. This file can be generated for later use.
touch empty.esl
sign-efi-sig-list -k PK.key PK empty.esl clear-PK.auth

sbsigntools

Tools for handling signatures and EFI-programs.

mokutil

bootctl (systemd)

efibootmgr

# List EFI boot-entries, which are enabled, currently booted, and the boot order.
efibootmgr
# Delete entry `0001`.
efibootmgr -b 0001 -B
# Configure boot-order.
efibootmgr -o 0001,0002,0003

Notes

Changelog

This article will receive updates, if necessary.