Certificate management (PKI) - easy-rsa

Translation of the original in Spanish. Read it in: CatalàCastellano

Part 1 of 5Certificates, PKI and MQTT over TLS
  1. Certificate management (PKI) - easy-rsa
  2. MQTT Broker (Mosquitto) with server certificate (self-signed)
  3. MQTT Broker (Mosquitto) with server certificate (self-signed) and client certificate
  4. MQTT Broker (Mosquitto) with server certificate (Let's Encrypt)
  5. Certificate formats: PEM, DER and PKCS#12 and private key handling

Introduction to Public Key Infrastructure (PKI)

A PKI, or Public Key Infrastructure, is a system that manages digital certificates and keys to guarantee secure communications. Let's break down the basic concepts.

Key concepts

  • PKI: It is the complete system that connects Certificates of Authority (CA), keys and certificates to guarantee trust between entities.

  • CA (Certificate Authority): It is the "boss" of the PKI. It signs and validates certificates, guaranteeing that they are trustworthy.

  • Certificate: A digital document that includes a public key, information about the entity, and the CA's signature. It is like a digital passport.

  • Certificate Request: It is a "draft" of the certificate that an entity sends to the CA so that it signs it.

  • Keypair: A set of two keys: one public (to share) and one private (secret). For example, the public one is like your email address and the private one like the password.

What is a CA and why is it important?

The CA is the core of a PKI and its private key is extremely sensitive because it signs every certificate. To protect it, it is recommended to keep it in a secure environment, such as a dedicated system or even one disconnected from the network.

How does all this work?

  • CA creation: A keypair and the structure needed to manage certificates are generated.

  • Certificate Request: A client (for example, a VPN server) creates a keypair and sends its request to the CA.

  • Signing and certificate: The CA verifies the request and uses its private key to sign the certificate, which it then returns to the client.

How is a certificate verified? Practical examples

Certificate verification varies depending on the context, and I want to explain it with two clear examples: a VPN connection, where I have a keypair and a certificate, and a connection to a bank, where I do not need a keypair, only trust in the CA.

Example 1: VPN connection (I have my keypair and certificate)

When I connect to a VPN, both the client and the server need a keypair (public and private) and certificates signed by the same CA. The process is as follows:

  • I generate my keypair and request a certificate from the CA, which verifies my identity and signs my certificate.

  • When I try to connect to the VPN server, it presents me with its certificate.

  • I verify that the server's certificate was signed by the CA I trust.

  • Then I send my own certificate to the server, which does the same verification with its copy of the CA.

  • If everything is valid, we both use our private keys to exchange encrypted information and establish a secure connection.

For example, if I work remotely and need to access my company's network, I configure a VPN client with my certificate and keys. The server verifies that I am who I say I am and lets me in only if the verification is correct.

Example 2: Connection to a bank (no keypair, only trust in the CA)

When I access my bank's website from my browser, I do not need a personal keypair. I simply trust the certificates issued by the CAs included in my browser's trust list. It works like this:

  • The bank's server sends me its certificate.

  • My browser verifies that the certificate was signed by a CA in its trust list and that it is neither expired nor revoked.

  • If the verification is successful, a secure connection is established via TLS/SSL.

  • During the connection, the bank uses its private key to prove that it is legitimate, signing data that my browser validates with the public key included in the bank's certificate.

For example, when I access https://mi-banco.com, my browser shows a padlock in the address bar. This means it has validated the bank's certificate and that the connection is now secure. Even though I have no keys in this case, I trust the authority that signed the bank's certificate.

Let's create our own PKI and certificate management using easy-rsa

Easy-RSA is a powerful and simple tool for managing a PKI infrastructure (Public Key Infrastructure). With it I can issue certificates for VPN servers, websites, and clients that need secure connections. Here I will explain how to use Easy-RSA to build this infrastructure and how to interpret its results.

It is important to mention that easy-rsa is nothing more than a set of scripts that simplify the use of OpenSSL, which is the real tool behind it that manages the certificates, keys and the rest.

Getting easy-rsa:

git clone https://github.com/OpenVPN/easy-rsa.git

Configuring the vars file in Easy-RSA

The vars file lets you configure basic and advanced parameters before starting the PKI with init-pki, saving time and guaranteeing consistency. To start, I copy vars.example to vars and fill in the parts I am interested in, such as:

  • Certificate data: Country, city, organisation, etc.

  • Keys and algorithms: Size (2048 bits) and type (RSA or EC).

  • Duration: Expiry of the CA and of the certificates.

set_var EASYRSA_REQ_COUNTRY "ES"
set_var EASYRSA_REQ_ORG "MiEmpresa"
set_var EASYRSA_KEY_SIZE 2048
set_var EASYRSA_CA_EXPIRE 3650
set_var EASYRSA_CERT_EXPIRE 825

Step 1: Prepare Easy-RSA

First, I download Easy-RSA from its official repository. Since it does not require installation, I simply extract the compressed file (.tar.gz for Linux/Unix or .zip for Windows) into the directory of my choice.

To begin, I initialise the PKI directory with:

./easyrsa init-pki

This creates a basic directory structure for managing the certificates. The pki directory includes:

  • private/: Stores private keys (must be kept very secure).

  • reqs/: Contains certificate requests.

  • issued/: Stores issued certificates.

  • ca.crt: The CA (Certificate Authority) certificate.

  • private/ca.key: The CA's private key, critical for the security of the whole infrastructure.

Step 2: Create the Certificate Authority (CA)

The CA is the heart of my PKI. It is the one that signs certificates and guarantees their validity. I create the CA with:

./easyrsa build-ca

During this process, I define a common name (Common Name, CN) to identify my CA, and I will be asked to configure a strong passphrase to protect the private key (ca.key). This file is extremely sensitive, and its security is critical.

The main result of this step is:

  • ca.crt: The CA's public certificate, which I will share with clients and servers to verify certificates.

Step 3: Generate keys and certificate requests

For a VPN client or server to work, I first generate a keypair (private and public) together with a certificate request (CSR). This is done with:

./easyrsa gen-req <nombre-del-certificado|FQDN>
# muy importante usar FQDN (Fully Qualified Domain Names) para evitar
# problemas de matching con los hostnames usados en la conexión.

For example, for a VPN server:

./easyrsa gen-req vpn-server.example.tld

The command generates two files:

  • private/<nombre-del-certificado>.key: The private key. This file must remain on the corresponding server or client and must never be shared.

  • reqs/<nombre-del-certificado>.req: The certificate request (CSR). This file is sent to the CA to be signed.

Step 4: Sign certificate requests

Once I have the request (.req), I import it into the CA and sign it. This is done with:

  1. Import the request
# solo hay que hacer este paso si el certificate request 
# lo hemos copiado de otra máquina. Esto lo coloca en path
# para que todo vaya automático, incluidas futuras 
# renovaciones.
./easyrsa import-req /the-path/vpn-server.example.tld.req vpn-server.example.tld
  1. Sign the request
./easyrsa sign-req server vpn-server.example.tld

Easy-RSA will generate a signed certificate in pki/issued/servidor-vpn.crt. This file is what the VPN server uses to identify itself securely to its clients.

Step 5: Configuring a VPN server and client

VPN server

To configure a VPN server (such as OpenVPN), I will need the following files generated by Easy-RSA:

  • Private key (private/servidor-vpn.key): Used by the server to encrypt communications.

  • Signed certificate (issued/servidor-vpn.crt): Guarantees that the server is trustworthy.

  • CA certificate (ca.crt): certificate and public key of our PKI.

These files are placed in the VPN server's configuration directory and referenced in its configuration.

VPN client

Step 4 has to be repeated; in this case, a good practice is to use as "nombre-del-certificado" the name of the user, or of the client, requesting the access certificate. In some use cases, that avoids having to go through an authentication process.

After generating the request and having imported it, as indicated in the first part of step 5. You have to be very careful with the second step. When signing the certificate request you have to do it indicating that its use will be as a client, not as a server.

# creamos el certificate request
./easyrsa gen-req mi-nombre-de-usuario

# importamos el certificado si es necesario en la PKI
./easyrsa import-req /the-path/mi-nombre-de-usuario.req mi-nombre-de-usuario

# firmamos el certificate request indicando el uso como cliente
./easyrsa sign-req client mi-nombre-de-usuario

After signing the certificate-request we already have the files the client must use to connect to the server:

  • Private key (private/mi-nombre-de-usuario.key): Used by the client to encrypt communications.

  • Signed certificate (issued/mi-nombre-de-usuario.crt): Guarantees that the server is trustworthy.

  • CA certificate (ca.crt): certificate and public key of our PKI.

NOTE:

When a certificate is signed, the possible options to select are:

  • client - it is for TLS client, typically used for VPN users, or for web browsers

  • server - for TLS server, typically web servers and VPN servers.

  • ca - when we want to create CA (Certificate Authorities) in cascade, this issues an intermediate certificate. Technically, they call it *chaining *certificate.

  • serverClient - certificate that serves both as client and as TLS server.

Step 6: Configuration for web servers

The process is similar for a web server (HTTPS). I generate a certificate request for the server's domain (for example, mi-servidor.com) and sign it with the CA. The necessary files will be:

  • Private key (private/mi-servidor.key): Exclusive to the web server.

  • Signed certificate (issued/mi-servidor.crt): Identifies the web server.

  • CA certificate (ca.crt): Lets browsers validate the connection.

These files are configured on the web server (such as Apache or Nginx) to enable HTTPs.

Managing issued certificates, CRL and real-time validation with OCSP

Easy-RSA not only allows issuing and revoking certificates, but also facilitates revocation management through the generation of certificate revocation lists (CRL) and the implementation of real-time validation via OCSP. When it is detected that a private key has been compromised or a certificate is no longer needed, it is revoked:

  • Revoke a certificate: Use the command: ./easyrsa revoke <nombre-del-certificado> This marks the certificate as revoked within the PKI's database.

  • Generate the CRL: Once the certificates are revoked, a new revocation list must be generated. This is done with: ./easyrsa gen-crl This command generates two files:

  • crl.pem: The CRL in Base64 format.

  • crl.der: The CRL in binary format. These files must be published on an accessible server so that clients can download them and check the status of the certificates.

Although the CRL is the traditional solution for managing revoked certificates, it has limitations such as the size of the list and the need to download it completely on every check. To overcome these shortcomings, OCSP (Online Certificate Status Protocol) can be implemented.

OCSP is a protocol defined in RFC 2560 that allows a client to check the status of a certificate in a targeted, real-time way, without having to process the whole CRL. Its advantages include:

  • Up-to-date information: It allows obtaining the revocation status of a certificate at the moment of the query.

  • Efficiency: It reduces traffic and processing load on the client, since only the specific certificate is queried.

  • Privacy: It avoids exposing potentially sensitive information contained in the CRL.

  • Scalability: It uses optimised mechanisms (often based on database engines) to handle queries even in environments with a large number of certificates.

Implementing an OCSP server with OpenSSL:

To start an OCSP server, use the following command:

openssl ocsp -index /ruta/a/tu/index.txt -port 2560 -ignore_err -rsigner /ruta/a/tu/ocsp.crt -rkey /ruta/a/tu/ocsp.key -CA /ruta/a/tu/ca.crt -text -out /ruta/a/tu/log_ocsp.txt &amp;

Where:

  • -index indicates the file that contains the index of issued certificates.

  • -port defines the port on which the OCSP server will listen (in this example, 2560).

  • -ignore_err allows the server to keep operating even if it receives malformed requests.

  • -rsigner and -rkey specify the certificate and private key of the OCSP responder.

  • -CA points to the certificate of the issuing CA.

  • -text -out saves a detailed log of the operations.

To query the status of a certificate (for example, newcert.pem), run:

openssl ocsp -CAfile /ruta/a/tu/ca.crt -issuer /ruta/a/tu/ca.crt -cert newcert.pem -url http://tu-dominio:2560 -resp_text

This query lets you verify immediately whether the certificate is revoked or still valid.

Integrating the generation and publication of the CRL with an OCSP server provides a complete mechanism for managing issued certificates. While the CRL offers a traditional solution based on downloading a complete list, OCSP lets clients obtain the status of a certificate quickly and in real time, optimising the validation process and reducing the load on the network. This combined approach ensures that, both in test and production environments, robust mechanisms are available to maintain the integrity and security of the PKI infrastructure.

Renewing a certificate

Normally, shortly before a certificate expires, its renewal is requested. This will issue another certificate file signed by the CA like the one issued the first time, but with a new expiry date:

./easyrsa renew &lt;nombre-del-certificado>

once the new certificate has been generated in:

  • issued/mqtt.example.tld.crt - renewed certificate

it is a good idea to run the following command to make sure the old certificate becomes obsolete:

./easyrsa revoke-renewed &lt;nombre-del-certificado>

this ends up forcing us to generate a new certificate revocation list. That is, we have to run:

./easyrsa gen-crl

and consequently distribute the new CRL file.

Conclusion

Easy-RSA simplifies the creation and management of a PKI infrastructure, which lets me issue and administer certificates for VPNs and web servers. Its outputs, such as private keys, signed certificates, and the CA certificate, are the essential components for guaranteeing secure connections. With this tool, I can build my own trusted network to secure both servers and clients in different scenarios.

References