Contents

OpenSSL for X.509 certificates

Contents

Recently I’d to generate an X.509 certificate to enable encryption connections to a web app. The browser performs a check to ensure that the connection is via a valid, trusted certificate, later on we’ll be using a 3rd party entity known as a Certificate Authority (CA) to do just that.

We’ll be using openssl to generate a private key. This key manifests in the form of a file, so we’ll call it a .key file, which we’ll also make read only:

$ openssl genrsa -out privkey.pem
Generating RSA private key, 2048 bit long modulus
.........................................................+++
.......................+++
$ chmod 400 privkey.key

If you cat privkey.key, the first and last lines of your .key will be -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY-----.

This key file is the first half of the equation in public key cryptography. For the second half, we need to submit a Certificate Signing Request (CSR) to a 3rd party entity known as a Certificate Authority (CA).

The CSR is generated using openssl, with privkey.pem as an inputs. A CSR manifests in the form of a file, which we’ll call a .csr file. Samples for the various other inputs are also provided below:

$ openssl req -new -sha256 -days 825 -key privkey.pem -out signme.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [SG]:SG
State or Province Name (full name) [Singapore]:Singapore
Locality Name (eg, city) [Singapore]:Singapore
Organization Name (eg, company) [Internet Widgits Pty Ltd]:GitHub
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:waynekhan.github.io
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

If you cat signme.csr, the first and last lines will be -----BEGIN CERTIFICATE REQUEST----- and -----END CERTIFICATE REQUEST-----.

You must therefore provide this file to your CA in order to generate a public certificate, also in the form of a file. If you cat this file, the first and last lines will be -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----. You may also see more than one of these; i.e., a certificate chain, so I’ve creatively named this file as fullchain.pem.

Thereafter, depending on what kind of application you use (e.g., grafana-server, httpd, nginx), the precise configuration is a bit different, but you’ll always need to use your private key privkey.pem and public certificate fullchain.pem in order for HTTPS clients to be able to (i) identify your server; (ii) encrypt the connection.