Skip to main content

Generate an HAProxy server certificate for testing

Step-by-step guide to generate a self-signed HAProxy server certificate using OpenSC and HSM.
This section covers the following tasks:
  1. Install OpenSC.
  2. Generate a key pair on the CryptoHub by using pkcs11-tool.
  3. Create a tls directory for HAProxy and generate a self-signed certificate by using OpenSSL.
  4. Store a reference to the HSM-stored private key inside a special PEM object with the label PKCS#11 PROVIDER URI.

Install OpenSC

To generate a new key pair on the CryptoHub, use pkcs11-tool, which is included in the OpenSC package available through the default package manager in most Linux distributions. In a terminal, run the following commands to update the package index and install OpenSC:
Shell

Generate a key pair

Perform the following steps to generate a key pair on the CryptoHub using pkcs11-tool:
1
If you haven’t set the FXPKCS11_MODULE environment variable to the location of the FXPKCS11 library file, run the following command to set it:
Shell
You can also manually type out the full path to the module in the next step instead of setting the environment variable.
2
In a terminal, run the following command:
Shell
For a TLS server key used with the cipher configuration in this guide, the sign usage is sufficient.
3
Enter the identity password configured in the fxpkcs11.cfg file when prompted for the User PIN.
If the command succeeds, the keys display in the output, as shown in the following example:
Shell
The command creates the following keys:
  • A private RSA 2048 key with asymmetric sign and signRecover.
  • A public RSA 2048 key with verify and verifyRecover.

Create the tls directory and generate a self-signed certificate

Create a tls directory to hold the TLS certificate and key, and generate a self-signed certificate for HAProxy by using OpenSSL.
1
If you installed HAProxy through your Linux distribution default package manager (e.g., apt), the HAProxy configuration directory is located at /etc/haproxy/. In a terminal, create the directory tls to store the HAProxy certificate and private key, and change into that directory:
Shell
2
Use OpenSSL to generate a self-signed certificate for HAProxy from the key pair stored on the HSM with admin privileges:
Shell
If the command is successful, the tls directory should contain a new file named haproxy-cert.pem.

Create a reference to the private key

Perform the following steps to create a reference to the HSM-stored private key by using the uri2pem.py script:
1
Install the asn1crypto dependency, which you need to run the uri2pem.py script.
Shell
The command above installs asn1crypto system-wide using the OS package manager (apt). This is the simplest approach and is managed alongside your other system packages. If you prefer to keep dependencies isolated, you can instead install it in a Python virtual environment:
If you use a virtual environment, make sure it is activated when you run uri2pem.py, since the script depends on asn1crypto being available in the active environment.
2
Run the script with the following command:
Shell
To check if the command succeeded, run the following command to display the contents of the haproxy-cert.pem.key file:
None
It should look something similar to:
The preceding stanza decodes to the PKCS #11 URI of the private key stored on the HSM. This object is a container for the PKCS #11 URI and contains no keying material. Because it holds no key material, inspecting it with tools such as strings may show only fragments of the encoded URI — this is expected and does not indicate a problem.