OpenSSL Command-Line Guide

Last update: 2023-07-16 12:40:14 +0800 +0800
Available translations: 日本語 / 中文(简体) / 中文(繁體)

OpenSSL provides cryptographic primitives, protocol libraries, and command-line tools. Commands are useful for inspection, interoperability tests, and local administration; application code should use a maintained language library and its safe defaults.

Keys, CSRs, and Certificates #

Generate a private key with the generic key interface:

sh
openssl genpkey -algorithm RSA \
  -pkeyopt rsa_keygen_bits:3072 \
  -out private-key.pem
openssl pkey -in private-key.pem -pubout -out public-key.pem

Create a certificate-signing request and inspect it:

sh
openssl req -new -key private-key.pem -out request.csr
openssl req -in request.csr -noout -text -verify
openssl x509 -in certificate.pem -noout -text

req -verify checks the CSR’s proof of possession. It does not validate the requested identity or issue a certificate. A CSR contains a public key and request attributes, never the private key.

Verify a certificate path against an explicit trust store:

sh
openssl verify -CAfile roots.pem -untrusted intermediate.pem certificate.pem

PEM wraps a DER object in base64 text with type markers. Convert explicitly and retain the object type:

sh
openssl x509 -in certificate.pem -outform DER -out certificate.der
openssl x509 -inform DER -in certificate.der -out certificate.pem
openssl pkey -in private-key.pem -outform DER -out private-key.der

TLS clients also require hostname verification. For a diagnostic connection, pass the expected server name and verification options:

sh
openssl s_client -connect example.com:443 \
  -servername example.com -verify_hostname example.com

Digests and Signatures #

sh
openssl dgst -sha256 artifact.tar
openssl rand -hex 32
openssl dgst -sha256 -sign private-key.pem -out artifact.sig artifact.tar
openssl dgst -sha256 -verify public-key.pem -signature artifact.sig artifact.tar

A digest detects accidental changes only when its expected value arrives through an authenticated channel. A signature verifies both the digest and possession of the corresponding private key, but trust in the public key remains a separate deployment decision.

Password-Based Encryption #

sh
openssl enc -aes-256-cbc -pbkdf2 -salt \
  -in plaintext -out ciphertext

openssl enc does not provide modern AEAD modes through its traditional interface. Do not use it to invent an application encryption protocol. Use a specified AEAD format or a dedicated tool for files requiring authenticated encryption. When interoperability requires enc, state the digest and iteration count explicitly because defaults can change, and understand that CBC ciphertext remains unauthenticated.

Pass secrets through protected files or descriptors where the command supports them. A password written as a command argument can appear in process listings and shell history. Restrict private-key permissions and remove temporary plaintext only through storage-appropriate procedures.

TLS Diagnostics #

s_client is a diagnostic client rather than a policy-complete replacement for an application. Add -showcerts, -verify_return_error, and the application’s expected protocol options when investigating path or handshake failures:

sh
openssl s_client -connect example.com:443 -servername example.com \
  -verify_hostname example.com -verify_return_error -showcerts

Avoid deprecated commands such as rsautl in new automation; pkeyutl and genpkey expose current interfaces. Inspect locally installed documentation with openssl list -commands and openssl list -providers because providers, algorithms, and defaults vary by OpenSSL release.

Comments

Read or comment on GitHub (sign-in required to comment)