User Tools

Site Tools


networking:openvpn:openvpn-setup

This is an old revision of the document!


OpenVPN Setup & Certificates


1. Install Easy-RSA

The first step in building an OpenVPN configuration is to establish a PKI (public key infrastructure). The PKI consists of:

  • a separate certificate (also known as a public key) and private key for the server and each client, and
  • a master Certificate Authority (CA) certificate and key which is used to sign each of the server and client certificates.

Install easy-rsa package on your Debian system with the following command:

# apt install easy-rsa

Note: Debian's easy-rsa package installs the script and support files under /usr/share/easy-rsa/ rather than placing an easyrsa binary in /usr/bin or /usr/local/bin. Be aware that /usr/share/easy-rsa is not in your default PATH.

Create a directory where whole key structure will be stored:

# make-cadir /tmp/certs
# cd /tmp/certs

Edit the vars file to configure Certificate Authority (CA) variables. Note: we extend the standard 825 days certificate expiration to 1825 (5 years).

  #nano ./vars
  ------------------------------------------
  Uncomment: 
  
  set_var EASYRSA_KEY_SIZE        2048
  set_var EASYRSA_REQ_COUNTRY     "NL"
  set_var EASYRSA_REQ_PROVINCE    "Zuid-Holland"
  set_var EASYRSA_REQ_CITY        "Rijnsburg"
  set_var EASYRSA_REQ_ORG         "Oscar.de.Groot"
  set_var EASYRSA_REQ_EMAIL       "oscar@oscardegroot.nl"
  set_var EASYRSA_REQ_OU          "MySites"
  set_var EASYRSA_CERT_EXPIRE      1825


Generate the required certificates and keys:

$ /usr/share/easy-rsa/easyrsa init-pki
  
---------------Output-----------------
Using Easy-RSA 'vars' configuration:
* /tmp/certs/vars

Notice
------
'init-pki' complete; you may now create a CA or requests.

Your newly created PKI dir is:
* /tmp/certs/pki

Using Easy-RSA configuration:
* /tmp/certs/vars


2. Create own CA certificate

Create CA certificate by running easyrsa build-ca. It asks to enter information that will be incorporated into your certificate request.

$ /usr/share/easy-rsa/easyrsa build-ca
--------------------------------------
Enter New CA Key Passphrase: `my standard rootpasswd`.
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:`Oscar.de.Groot`

Notice
------
CA creation complete. Your new CA certificate is at:
* /tmp/certs/pki/ca.crt

Create an OpenVPN TLS-AUTH|TLS-CRYPT-V1 key now: See 'help gen-tls'

Build-ca completed successfully.

3. Create Server Certificate & Key

Creating a server key and certificate is a 2-step easyrsa:

  1. gen-req generates a private key and a sign request (CSR)
  2. sign-req uses the CA key and certificate to issue a signed public certificate

Generate server private key

The OpenVPN server’s common name we use is myvpnserver. Be sure to include the nopass option as well. Failing to do so will password-protect the request file, which could lead to permissions issues later on.

$ /usr/share/easy-rsa/easyrsa gen-req myvpnserver nopass
--------------------------------------------------------

Notice
------
Private-Key and Public-Certificate-Request files created.
Your files are:
* req: /tmp/certs/pki/reqs/myvpnserver.req
* key: /tmp/certs/pki/private/myvpnserver.key

This will:

  • Generate a private key (pki/private/myvpnserver.key) for the entity named myvpnserver.
  • Creates a certificate signing request (pki/reqs/myvpnserver.req) containing the corresponding public key and requested identity fields (such as Common Name).
  • nopass skips key encryption: Leaves the private key unencrypted on disk so automated services (like a VPN server daemon) can start unattended without prompting for a passphrase. Omit nopass if you want a passphrase-protected key (common for client keys or sensitive CA keys).

Sign server key

Then sign the request by running easyrsa with the sign-req option, followed by the request type and the common name. The request type can either be client or server, so for the OpenVPN server’s certificate request, be sure to use the server request type. In the output, you’ll be asked to verify that the request comes from a trusted source. Type yes and press ENTER to confirm this.

$ /usr/share/easy-rsa/easyrsa sign-req server myvpnserver

This will:

  • Reads the request from pki/reqs/myvpnserver.req.
  • Applies the server profile/extension set, marking the resulting certificate for server authentication (and typically setting EKU attributes appropriate for a TLS/VPN server rather than a client).
  • Signs with the CA using your CA private key and CA certificate (pki/ca.crt), producing the signed server certificate at pki/issued/myvpnserver.crt. (Easy-RSA usually prompts you to confirm the request details and enter your CA passphrase, if one is set.)

4. Generating Diffie-Hellman (DH) params

The Diffie–Hellman (DH) Algorithm is a key-exchange protocol that enables two parties communicating over public channel to establish a mutual secret without it being transmitted over the Internet. DH enables the two to use a public key to encrypt and decrypt their conversation or data using symmetric cryptography. After initializing a PKI, any entity can create DH params that needs them. DH key params can be generated with:

$ ./easyrsa gen-dh

This may take a few minutes to complete.

5. TLS-AUTH

The tls-auth directive adds an additional HMAC signature to all SSL/TLS handshake packets for integrity verification. Any UDP packet not bearing the correct HMAC signature can be dropped without further processing. The tls-auth HMAC signature provides an additional level of security above and beyond that provided by SSL/TLS. It can protect against:DoS attacks, port flooding, Port scanning, Buffer overflow vulnerabilities, etc.

Using tls-auth requires that you generate a shared-secret key that is used in addition to the standard RSA certificate/key. Generate an HMAC signature to strengthen the server’s TLS integrity verification capabilities:

$ openvpn --genkey secret pki/ta.key

This command will generate an OpenVPN static key and write it to the file ta.key. This key should be copied over a pre-existing secure channel to the server and all client machines. It can be placed in the same directory as the RSA .key and .crt files.

In the server configuration, add:

tls-auth ta.key 0

In the client configuration, add:

tls-auth ta.key 1

6. Create Client Certificate and Key Pair

n this step, you will first generate the client key and certificate pair. If you have more than one client, you can repeat this process for each one. Please note, though, that you will need to pass a unique name value to the script for every client. Throughout this tutorial, the first certificate/key pair is referred to as MyVPNClient.

$ easyrsa gen-req MyClientName nopass

This will create a private key for the client and a certificate request file called MyClientName.req. Then sign the request by running easyrsa with the sign-req option, followed by the request type and the common name. The request type can either be client or server, so for the OpenVPN server’s certificate request, be sure to use the server request type.

$ easyrsa sign-req server MyClientName

In the output, you’ll be asked to verify that the request comes from a trusted source. Type yes and press ENTER to confirm this.

7. Deploy Certificates & Keys

With that, all the certificate and key files needed by your server have been generated. You’re ready to deploy the corresponding certificates and keys to both OpenVPN Server and Client systems.

Now we will find our newly-generated keys and certificates in the keys subdirectory. Here is an explanation of the relevant files:

Filename Needed By Purpose Secret
ca.crt server + all clients Root CA certificate NO
ca.key key signing machine only Root CA key YES
dh2048.pem server only Diffie Hellman parameters NO
MyServerName.crt server only Server Certificate NO
MyServerName.key server only Server Key YES
MyClientName.crt client1 only Client1 Certificate NO
MyClientName.key client1 only Client1 Key YES

Server Deployment

Insert the following options in the openvpn configuration file:

vi /etc/config/openvpn
----------------------
option ca '/etc/easy-rsa/keys/ca.crt'
option key '/etc/easy-rsa/keys/myvpnserver.key'
option cert '/etc/easy-rsa/keys/myvpnserver.crt' 
option dh '/etc/easy-rsa/keys/dh2048.pem'

Client Deployment

Insert the various certificates and keys in the following sections of the client.ovpn configuration file:

  1. ca.cert –> insert contents –> between the <ca></ca>. Including the “—–BEGIN CERTIFICATE—–” and “—–END CERTIFICATE—–” lines.
  2. MyClientName.key –> insert contents –> between the <key></key>. Including the “—–BEGIN PRIVATE KEY—–” and “—–END PRIVATE KEY—–” lines.
  3. MyClientName.crt –> insert contents –> between the <cert></cert>. Including everything.
  4. ta.key –> insert contents –> between the <tls-auth></tls-auth>. Including everything.
networking/openvpn/openvpn-setup.1795857372.txt.gz · Last modified: by oscar