OpenSSL Certificate Manual

This article was created with the help of a translation tool. Although we check the translation, errors in the content cannot be ruled out. Please bear this in mind when using the content. Thank you for your understanding.

Welcome to the ultimate OpenSSL certificate guide

OpenSSL is an open source toolkit for implementing the SSL and TLS protocols as well as a general cryptography toolkit. It offers a variety of commands for generating and managing certificates, private keys and other cryptographic elements. This article presents the most common OpenSSL commands with corresponding examples.

The article contains both the older and the new commands (OpenSSL ≥ 3.0). The new commands are recommended in newer OpenSSL versions, especially if you need to work with different algorithms (e.g. elliptic curve keys, DSA keys, etc.).

Further reading:

OpenSSL

All commands etc. presented in this OpenSSL certificate manual are carried out with OpenSSL, a free software for TLS (then SSL). We recommend using the latest version. At the time of writing this article, this is version 3.3.2, which was released on 03.09.2024. If you already have OpenSSL installed, you can easily check the version:

Bash
openssl version

Issue:

OpenSSL 3.3.2 3 Sep 2024 (Library: OpenSSL 3.3.2 3 Sep 2024)

If you have not yet installed OpenSSL, you can obtain it from the following sites:

Windows: https://slproweb.com/products/Win32OpenSSL.html*

Linux: https://openssl-library.org/source/index.html or via the package manager of your distribution (APT, YUM etc.)

GitHub: https://github.com/openssl/openssl

Of course, you can also compile your own build under Linux using make, but we will not go into this in this article.

* This is not the official OpenSSL website, as OpenSSL itself does not provide precompiled packages for Windows.

Certificate types

SSL/TLS

SSL/TLS certificates are used to enable secure connections (HTTPS) between a web browser (client) and a web server. They guarantee encryption and authentication to ensure that communication is secure and trustworthy.

Certificates differ firstly in the way in which validation is carried out:

  • Domain Validated (DV): This certificate only validates the domain. It checks whether the applicant has control over the domain, but does not require a deeper identity check. Validation is usually carried out by replying to an e-mail sent by the certification authority to admin@, administrator@, hostmaster@ or postmaster@. Alternatively, a TXT entry specified by the certification authority can be set in the DNS. It is one of the most commonly used certificates, especially for small websites.
  • Organization Validated (OV): In contrast to the DV certificate, the identity of the organization is also checked. This is done by submitting an extract from the commercial register.
  • Extended Validation (EV): These certificates are subject to a strict review. Applicants must fulfill criteria such as: Domain, domain owner, identity, postal address, legal status, operational existence. EV certificates are available for large companies, banks, registered associations and public authorities.

Furthermore, there are different types of SSL/TLS certificates (most of these types can be obtained as DV, OV or EV):

  • Single certificate: This certificate secures a single domain, e.g. it-tech.wiki. The www. subdomain is often also secured free of charge.
  • Multidomain certificates (MDC): Several domains, so-called SANs (Subject Alternative Names), can be secured here. Up to 100 different domains and subdomains can be specified.
  • Unified Communication (UC) certificates: This is a special version of multidomain certificates. It was originally designed for Microsoft Exchange and Microsoft Office Communication Server.
  • Wildcard certificates: A wildcard certificate can secure any number of subdomains of a level. This means, for example, that you can secure forum.it-tech.wiki, blog.it-tech.wiki, store.it-tech.wiki etc., but not de.blog.it-tech.wiki or static.cdn.it-tech.wiki. Even the domain itself, it-tech.wiki, is not technically part of this. However, some certification authorities often offer a second SAN free of charge, so that it-tech.wiki can also be secured with the wildcard certificate in addition to *.it-tech.wiki. Multi-level wildcard certificates (*.*.it-tech.wiki) are not available (although some browsers have support for this on board).
  • Multidomain wildcard certificates: As the name suggests, this certificate offers the advantages of a wildcard certificate paired with those of a multidomain certificate. These generally secure the TLDs, i.e. it-tech.wiki and e.g. it-tech.store, but also all subdomains of the second level, e.g. de.it-tech.wiki, cdn.it-tech.wiki, mail.it-tech.store, support.it-tech.store etc.

E-mail certificates (S/MIME)

S/MIME (Secure/Multipurpose Internet Mail Extensions) certificates are used to sign and encrypt emails. The digital signature guarantees the authenticity and integrity of the email, i.e. ensures that it has not been tampered with. Encryption also ensures that only the addressed recipient can decrypt and read the message.

There are also different forms of validation for e-mail certificates:

  • E-mail validated (DV): This variant only checks whether the applicant has access to the e-mail address, e.g. by calling up a link sent to the e-mail address or entering a code received at the certification authority.
  • Individual Validated (IV): Like DV certificates, but the name of the applicant is also included in the certificate. The identity of the applicant is verified with an official ID document and, if necessary, video verification.
  • Organization validated (OV): The validation process corresponds to that of an organization-validated SSL/TLS certificate. In addition to the person, the organization (extract from the commercial register, tax documents, etc.) is also checked.

Further certificates

There are also many other formats, e.g. code signing certificates to create software signatures, client certificates to ensure the authentication of users and devices, document signing certificates to digitally sign digital documents (e.g. PDFs) and, of course, intermediate and root certificates. However, we will not go into these in detail in this article.

Certificate formats

  1. incomplete list, only exemplary ↩︎

General information

Establishment of a certificate chain

A certificate chain contains several certificates within one file. These are usually in PEM format and have the following format:

Bash
-----BEGIN CERTIFICATE-----
...    (Zertifikat)     ...
------END CERTIFICATE------
-----BEGIN CERTIFICATE-----
...(Zwischenzertifikat) ...
------END CERTIFICATE------
-----BEGIN CERTIFICATE-----
...  (Root-Zertifikat)  ...
------END CERTIFICATE------

Private keys with RSA

Always keep your private keys in a safe place!

Overview of RSA keys

Generating a private key with RSA

Bash
openssl genpkey -algorithm RSA -out private_key.pem
Bash
openssl genrsa -out private.pem

These commands create an RSA key with the standard size of 2048 bits.

Generate a private key of a specific length with RSA

If you need a key of a different size, you can pass this as a parameter and thus generate a key with a specific length. In the following example, we create a private key with a length of 4096 bits:

Bash
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:4096
Bash
openssl genrsa -out private.pem 4096

Private keys with elliptic curves (ECDSA)

The advantage of elliptic curves are the significantly shorter keys compared to their equivalent RSA keys with the same level of security. This makes them particularly efficient, as less computing power and memory is required for the same level of security.

For keys with elliptical curves, the key length is not defined by specifying the length in bits, as with RSA, but by selecting the corresponding curve.

Overview of the most important elliptical curves

List of all supported curve parameters

Bash
openssl ecparam -list_curves

Generating a private key with ECDSA

Bash
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:prime256v1 -out private_key_ecdsa.pem
Bash
openssl ecparam -genkey -name prime256v1 -out private_key_ecdsa.pem

Generating a certificate request

CSR with parameters

During execution, you will be asked to enter various information. For the Common Name (CN) query, enter the domain name, e.g. it-tech.wiki or *.it-tech.wiki for a wildcard certificate.

Bash
# Generiere einen privaten Schlüssel
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
# Erstelle eine CSR mit interaktiver Eingabe
openssl req -new -key private_key.pem -out request.csr
Bash
# Generiere einen privaten Schlüssel
openssl genrsa -out private_key.pem 2048
# Erstelle eine CSR mit interaktiver Eingabe
openssl req -new -key private_key.pem -out request.csr

CSR with a configuration file

req.conf
[req]
prompt = no
distinguished_name = req_distinguished_name

[req_distinguished_name]
C = DE
ST = NRW
L = Duisburg
O = IT-Tech
OU = IT
CN = *.it-tech.wiki

Now use the configuration file to create a private key (if not already available) and the CSR.

Bash
# Generiere einen privaten Schlüssel
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
# Erstelle die CSR mit der Konfigurationsdatei
openssl req -new -key private_key.pem -out request.csr -config req.conf
Bash
# Generiere einen privaten Schlüssel
openssl genrsa -out private_key.pem 2048
# Erstelle den CSR mit der Konfigurationsdatei
openssl req -new -key private_key.pem -out request.csr -config req.conf

CSR with one configuration file and multiple SANs

req.conf
[req]
prompt = no
distinguished_name = req_distinguished_name
req_extensions = v3_req

[req_distinguished_name]
C = DE
ST = NRW
L = Duisburg
O = IT-Tech
OU = IT
CN = *.it-tech.wiki

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = *.it-tech.wiki
DNS.2 = it-tech.wiki

Use the notation *.domain.tld for so-called wildcard certificates. Many certificate providers secure an additional SAN free of charge, in this case it is highly recommended to specify the domain (domain.tld) yourself (see example above).

The command for generating the private key and the CSR are identical to 2.2.

CSR with ECDSA and SANs

Bash
# Generiere einen ECDSA-Schlüssel
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:prime256v1 -out private_key_ecdsa.pem
# Erstelle einen CSR mit der Konfigurationsdatei
openssl req -new -key private_key_ecdsa.pem -out request_ecdsa.csr -config req.conf
Bash
# Generiere einen ECDSA-Schlüssel
openssl ecparam -genkey -name prime256v1 -out private_key_ecdsa.pem
# Erstelle einen CSR mit der Konfigurationsdatei
openssl req -new -key private_key_ecdsa.pem -out request_ecdsa.csr -config req.conf

Convert private keys and certificates

PEM to THE

Bash
openssl x509 -in cert.pem -outform der -out cert.der

DER to PEM

Bash
openssl x509 -in cert.der -inform der -out cert.pem -outform pem

PEM to PKCS12

Bash
openssl pkcs12 -export -out cert.p12 -inkey private_key.pem -in cert.pem -certfile chain.pem

The parameter -certfile for specifying intermediate certificates is optional.

If you also want to include the root certificate, add it to the chain in the chain.pem file.

PKCS12 to PEM

Bash
openssl pkcs12 -in cert.p12 -out cert.pem -nodes

Check private keys and certificates

Checking a private key

Bash
openssl pkey -in private_key.pem -check
Bash
openssl rsa -in private_key.pem -check

Checking a certificate

Bash
openssl x509 -in cert.pem -text -noout

Checking a CSR

Bash
openssl req -in request.csr -text -noout

Check whether a private key, a certificate and a CSR match

To check whether a private key, a certificate and a CSR match, you can perform a comparison with OpenSSL. This works by comparing the "modulus" of all files. It is important that you use the correct format for each file. The private key must be in PEM format, for example, and the CSR should also be in a standardized format such as PEM.

First you have to check the modulus of the private key:

Bash
openssl rsa -noout -modulus -in privkey.pem | openssl md5

Then extract the modulus of the certificate:

Bash
openssl x509 -noout -modulus -in cert.pem | openssl md5

Now show the modulus of the CSR:

Bash
openssl req -noout -modulus -in request.csr | openssl md5

All three MD5 hashes should be identical if the private key, the certificate and the CSR match. If one of the values is different, then something is wrong.

Signing certificates

Create a self-signed certificate

Bash
openssl req -new -x509 -key private_key.pem -out cert.pem -days 365

Signing a certificate using a CA

Bash
openssl x509 -req -in request.csr -CA ca_cert.pem -CAkey ca_key.pem -CAcreateserial -out cert.pem -days 365

Manipulating keys

Extract private key from a PEM file

Bash
openssl pkey -in cert_and_key.pem -out private_key.pem
Bash
openssl rsa -in cert_and_key.pem -out private_key.pem

Extract private key from a PFX/P12 file (PKCS12)

Bash
openssl pkcs12 -in file.pfx -nocerts -out private_key.pem -nodes

Protect private key with a passphrase

Bash
openssl pkey -in private_key.pem -aes256 -out encrypted_key.pem
Bash
openssl rsa -in private_key.pem -des3 -out encrypted_key.pem

Remove passphrase from a private key

Bash
openssl pkey -in encrypted_key.pem -out unencrypted_key.pem
Bash
openssl rsa -in encrypted_key.pem -out unencrypted_key.pem

Miscellaneous

Display certificate chain of a website

Bash
openssl s_client -connect it-tech.wiki:443 -showcerts

Appendix

Abbreviations

Leave a Reply

Comments are not displayed directly, as they are released in moderation.


WordPress Cookie Plugin by Real Cookie Banner