Difference between revisions of "Easy-RSA"

From ArchWiki
Jump to: navigation, search
(clarify a little)
(Certificate Authority (CA): set EASYRSA env variable)
 
(124 intermediate revisions by 24 users not shown)
Line 1: Line 1:
 
[[Category:Virtual Private Network]]
 
[[Category:Virtual Private Network]]
 +
[[ja:Easy-RSA]]
 +
The first step when setting up [[OpenVPN]] is to create a [[Wikipedia:Public key infrastructure|Public Key Infrastructure (PKI)]].  In summary, this consists of:
 +
* A public master [[Wikipedia:Certificate Authority|Certificate Authority (CA)]] certificate and a private key.
 +
* A separate public certificate and private key pair for each server.
 +
* A separate public certificate and private key pair for each client.
 +
 +
One can think of the key-based authentication in terms similar to that of how [[SSH keys]] work with the added layer of a signing authority (the CA).  OpenVPN relies on a bidirectional authentication strategy, so the client must authenticate the server's certificate and in parallel, the server must authenticate the client's certificate.  This is accomplished by the 3rd party's signature (the CA) on both the client and server certificates.  Once this is established, further checks are performed before the authentication is complete.  For more details, see [https://www.secure-computing.net/openvpn/howto.php#pki secure-computing's guide].
 +
 +
{{Note|
 +
* The process outlined below requires users to securely transfer private key files to/from machines. For the purposes of this guide, using scp is shown, but readers may employ alternative methods as well.  Since the Arch default is to deny the root user over ssh, using scp requires transferring ownership of the files to be exported to a non-root user called ''foo'' throughout the guide.
 +
* Avoid generating keys on devices without a good entropy source. See [https://community.openvpn.net/openvpn/wiki/GettingStartedwithOVPN#Configuringencryption].
 +
* Due to a [https://github.com/OpenVPN/easy-rsa/issues/119 bug], the {{ic|vars}} file is not sourced if it is not specified explicitly.
 +
}}
 +
 +
== Certificate Authority (CA) ==
 +
For security purposes, it is recommended that the CA machine be separate from the machine running OpenVPN.
 +
 +
On the '''CA machine''', install {{pkg|easy-rsa}}, initialize a new PKI and generate a CA keypair that will be used to sign certificates:
 +
 +
# cd /etc/easy-rsa
 +
# export EASYRSA=$(pwd)
 +
# easyrsa init-pki
 +
# easyrsa build-ca
 +
 +
== OpenVPN server files ==
 +
 +
A functional OpenVPN server requires the following (in alphabetical order):
 +
 +
# The CA's public certificate
 +
# The Diffie-Hellman (DH) parameters file (needed for TLS mode which is recommended).
 +
# The server key pair (a public certificate and a private key).
 +
# The Hash-based Message Authentication Code (HMAC) key.
 +
 +
Upon completing the steps outlined in this article, users will have generated the following files on the server:
 +
 +
# {{ic|/etc/openvpn/server/ca.crt}}
 +
# {{ic|/etc/openvpn/server/dh.pem}}
 +
# {{ic|/etc/openvpn/server/servername.crt}} and {{ic|/etc/openvpn/server/servername.key}}
 +
# {{ic|/etc/openvpn/server/ta.key}}
 +
 +
=== CA public certificate ===
 +
 +
The CA public certificate {{ic|/etc/easy-rsa/pki/ca.crt}} generated in the previous step needs to be copied over to the machine that will be running OpenVPN.
 +
 +
On the '''CA machine''':
 +
 +
# cp /etc/easy-rsa/pki/ca.crt /tmp
 +
# chown foo /tmp/ca.crt
 +
 +
$ scp /tmp/ca.crt foo@hostname-of-openvpn-server:/tmp
 +
 +
On the '''OpenVPN server machine''':
 +
 +
# mv /tmp/ca.crt /etc/openvpn/server/
 +
# chown root:root /etc/openvpn/server/ca.crt
 +
 +
=== Server certificate and private key ===
 +
 +
On the '''OpenVPN server machine''', install {{pkg|easy-rsa}} and generate a key pair for the server:
 +
 +
# cd /etc/easy-rsa
 +
# easyrsa init-pki
 +
# easyrsa gen-req servername nopass
 +
# cp /etc/easy-rsa/pki/private/servername.key /etc/openvpn/server/
 +
 +
This will create two files:
 +
 +
{{ic|/etc/easy-rsa/pki/reqs/servername.req}}
 +
{{ic|/etc/easy-rsa/pki/private/servername.key}}
 +
 +
=== Diffie-Hellman (DH) parameters file ===
 +
 +
On the '''OpenVPN server machine''', create the initial dh.pem file:
 +
 +
# openssl dhparam -out /etc/openvpn/server/dh.pem 2048
  
The first step when setting up OpenVPN is to create a [[Wikipedia:Public key infrastructure|Public Key Infrastructure (PKI)]]. The PKI consists of:
+
{{Note|Although values higher than 2048 (4096 for example) may be used, they take considerably more time to generate and offer little benefit in security but advisable to have the DH prime number length to match the length of the RSA key. See [https://community.openvpn.net/openvpn/wiki/GettingStartedwithOVPN#Configuringencryption]}}
  
* A public master [[Wikipedia:Certificate Authority|Certificate Authority (CA)]] certificate and a private key.
+
=== Hash-based Message Authentication Code (HMAC) key ===
* A separate public certificate and private key pair (hereafter referred to as a certificate) for each server and each client.
+
 
 +
On the '''OpenVPN server machine''', create the HMAC key:
 +
 
 +
# openvpn --genkey --secret /etc/openvpn/server/ta.key
 +
 
 +
This will be used to add an additional HMAC signature to all SSL/TLS handshake packets. In addition any UDP packet not having the correct HMAC signature will be immediately dropped, protecting against:
  
To facilitate the certificate creation process, OpenVPN comes with a collection of [[Wikipedia:RSA (algorithm)|RSA]] key manangement scripts (based on the openssl command line tool) known as easy-rsa.
+
* Portscanning.
 +
* DOS attacks on the OpenVPN UDP port.
 +
* SSL/TLS handshake initiations from unauthorized machines.
 +
* Any eventual buffer overflow vulnerabilities in the SSL/TLS implementation.
  
{{Note| Only .key files need to be kept secret, .crt and .csr files can be sent over insecure channels such as plaintext email.}}
+
== OpenVPN client files ==
  
In this article the needed certificates are created by root in root's home directory. This ensures that the generated files have the right ownership and permissions, and are safe from other users.
+
=== Client certificate and private key ===
  
{{Note|The certificates can be created on any machine.  For the highest security, generate the certificates on a physically secure machine disconnected from any network, and make sure that the generated ca.key private key is backed up and never accessible to anyone.}}
+
Any machine can generate client files provided that {{pkg|easy-rsa}} is installed.
  
{{Warning|Make sure that the generated files are backed up, especially the ca.key and ca.crt files, since if lost you will not be able to create any new, nor revoke any comprised certificates, thus requiring the generation of a new [[Wikipedia:Certificate Authority|Certificate Authority (CA)]] certificate, invalidating the entire PKI infrastructure.}}
+
If the pki is not initialized, do so via:
  
===Installing the easy-rsa scripts===
+
# cd /etc/easy-rsa
 +
# easyrsa init-pki
  
Install the scripts by doing the following:
+
Generate the client key and certificate:
  
{{bc|# cp -r /usr/share/openvpn/easy-rsa /root}}
+
# cd /etc/easy-rsa
 +
# easyrsa gen-req client1 nopass
  
===Creating certificates===
+
This will create two files:
  
Change to the directory where you installed the scripts.
+
{{ic|/etc/easy-rsa/pki/reqs/client1.req}}
 +
{{ic|/etc/easy-rsa/pki/private/client1.key}}
  
{{bc|# cd /root/easy-rsa}}
+
The gen-req set can be repeated as many times as needed for additional clients.
  
To ensure the consistent use of values when generating the  PKI, set default values to be used by the PKI generating scripts. Edit /root/easy-rsa/vars and at a minimum set the KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, and KEY_EMAIL parameters (do not leave any of these parameters blank).  Change the KEY_SIZE parameter to 2048 for the SSL/TLS to use 2048bit RSA keys for authentication.
+
== Sign the certificates and pass them back to the server and clients ==
  
{{hc|/root/easy-rsa/vars|<nowiki>
+
=== Obtain and sign the certificates on the CA ===
# easy-rsa parameter settings
 
  
# NOTE: If you installed from an RPM,
+
The server and client(s) certificates need to be signed by the CA then transferred back to the OpenVPN server/client(s).
# do not edit this file in place in
 
# /usr/share/openvpn/easy-rsa --
 
# instead, you should copy the whole
 
# easy-rsa directory to another location
 
# (such as /etc/openvpn) so that your
 
# edits will not be wiped out by a future
 
# OpenVPN package upgrade.
 
  
# This variable should point to
+
On the '''OpenVPN server''' (or the box used to generate the certificate/key pairs):
# the top level of the easy-rsa
 
# tree.
 
export EASY_RSA="`pwd`"
 
  
#
+
# cp /etc/easy-rsa/pki/reqs/*.req /tmp
# This variable should point to
+
# chown foo /tmp/*.req
# the requested executables
 
#
 
export OPENSSL="openssl"
 
export PKCS11TOOL="pkcs11-tool"
 
export GREP="grep"
 
  
# This variable should point to
+
Securely transfer the files to the CA machine for signing:
# the openssl.cnf file included
 
# with easy-rsa.
 
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`
 
  
# Edit this variable to point to
+
$ scp /tmp/*.req foo@hostname-of-CA:/tmp
# your soon-to-be-created key
 
# directory.
 
#
 
# WARNING: clean-all will do
 
# a rm -rf on this directory
 
# so make sure you define
 
# it correctly!
 
export KEY_DIR="$EASY_RSA/keys"
 
  
# Issue rm -rf warning
+
On the '''CA machine''', import and sign the certificate requests:
echo NOTE: If you run ./clean-all, I will be doing a rm -rf on $KEY_DIR
 
  
# PKCS11 fixes
+
# cd /etc/easy-rsa
export PKCS11_MODULE_PATH="dummy"
+
# easyrsa import-req /tmp/servername.req servername
export PKCS11_PIN="dummy"
+
# easyrsa import-req /tmp/client1.req client1
 +
# easyrsa sign-req server servername
 +
# easyrsa sign-req client client1
  
# Increase this to 2048 if you
+
This will create the following signed certificates which can be transferred back to their respective machines:
# are paranoid.  This will slow
 
# down TLS negotiation performance
 
# as well as the one-time DH parms
 
# generation process.
 
</nowiki>'''export KEY_SIZE&#61;2048'''<nowiki>
 
  
# In how many days should the root CA key expire?
+
{{ic|/etc/easy-rsa/pki/issued/servername.crt}}
export CA_EXPIRE=3650
+
{{ic|/etc/easy-rsa/pki/issued/client1.crt}}
  
# In how many days should certificates expire?
+
The leftover .req files can be safely deleted:
export KEY_EXPIRE=3650
 
  
# These are the default values for fields
+
# rm -f /tmp/*.req
# which will be placed in the certificate.
 
# Do not leave any of these fields blank.
 
  
</nowiki>
+
=== Pass the signed certificates back to the server and client(s) ===
'''export KEY_COUNTRY&#61;"US"'''
 
'''export KEY_PROVINCE&#61;"CA"'''
 
'''export KEY_CITY&#61;"Acme Acres"'''
 
'''export KEY_ORG&#61;"Acme"'''
 
'''export KEY_EMAIL&#61;"roadrunner@acmecorp.org"'''
 
'''#export KEY_EMAIL&#61;mail@host.domain'''
 
'''export KEY_CN&#61;Acme-CA'''
 
'''export KEY_NAME&#61;Acme-CA'''
 
'''export KEY_OU&#61;""'''<nowiki>
 
export PKCS11_MODULE_PATH=changeme
 
export PKCS11_PIN=1234
 
</nowiki>}}
 
  
Export the environment variables.
+
On the '''CA machine''', copy the signed certificates and transfer them to the server/client(s):
  
{{bc|# source ./vars}}
+
# cp /etc/easy-rsa/pki/issued/*.crt /tmp
 +
# chown foo /tmp/*.crt
 +
$ scp /tmp/*.crt foo@hostname-of-openvpn_server:/tmp
  
Delete any previously created certificates.
+
On the '''OpenVPN server''', move the certificates in place and reassign ownership.
 +
For the server:
 +
# mv /tmp/servername.crt /etc/openvpn/server/
 +
# chown root:root /etc/openvpn/server/servername.crt
 +
For the client:
 +
# mkdir /etc/easy-rsa/pki/signed
 +
# mv /tmp/client1.crt /etc/easy-rsa/pki/signed
  
{{bc|# ./clean-all}}
+
That is it.  To generate the client profile. See: [[OpenVPN#ovpngen]].
  
{{Note| Entering a . (dot) when prompted for a value, blanks out the parameter.}}
+
== Revoking certificates and alerting the OpenVPN server ==
  
The build-ca script generates the [[Wikipedia:Certificate Authority|Certificate Authority (CA)]] certificate.
+
=== Revoke a certificate ===
  
{{hc|# ./build-ca|<nowiki>
+
Over time, it may become necessary to revoke a certificate thus denying access to the affected user(s). This example revokes the "client1" certificate.
Generating a 2048 bit RSA private key
 
..............++++++
 
...++++++
 
writing new private key to 'ca.key'
 
-----
 
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) [US]:
 
State or Province Name (full name) [CA]:
 
Locality Name (eg, city) [Acme Acres]:
 
Organization Name (eg, company) [Acme]:
 
Organizational Unit Name (eg, section) []:
 
Common Name (eg, your name or your server's hostname) [Acme-CA]:
 
Name [Acme-CA]:
 
Email Address [roadrunner@acmecorp.org]:
 
</nowiki>}}
 
  
The build-key-server script {{ic|# ./build-key-server <server name>}} generates a server certificate. Make sure that the server name (Common Name when running the script) is unique.
+
On the '''CA machine''':
  
{{Note|Do not enter a challenge password or company name when the script prompts you for one.}}
+
# cd /etc/easy-rsa
 +
# easyrsa revoke client1
 +
# easyrsa gen-crl
  
{{hc|# ./build-key-server elmer|<nowiki>
+
This will produce the CRL file {{ic|/etc/easy-rsa/pki/crl.pem}} that needs to be transferred to the OpenVPN server and made active there.
Generating a 2048 bit RSA private key
 
.....................++++++
 
.......................................................++++++
 
writing new private key to 'elmer.key'
 
-----
 
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) [US]:
 
State or Province Name (full name) [CA]:
 
Locality Name (eg, city) [Acme Acres]:
 
Organization Name (eg, company) [Acme]:
 
Organizational Unit Name (eg, section) []:
 
Common Name (eg, your name or your server's hostname) [elmer]:
 
Name [Acme-CA]:
 
Email Address [roadrunner@acmecorp.org]:
 
  
Please enter the following 'extra' attributes
+
=== Alert the OpenVPN server ===
to be sent with your certificate request
 
A challenge password []:
 
An optional company name []:
 
Using configuration from /root/easy-rsa/openssl-1.0.0.cnf
 
Check that the request matches the signature
 
Signature ok
 
The Subject's Distinguished Name is as follows
 
countryName          :PRINTABLE:'US'
 
stateOrProvinceName  :PRINTABLE:'CA'
 
localityName          :PRINTABLE:'Acme Acres'
 
organizationName      :PRINTABLE:'Acme'
 
commonName            :PRINTABLE:'elmer'
 
name                  :PRINTABLE:'Acme-CA'
 
emailAddress          :IA5STRING:'roadrunner@acmecorp.org'
 
Certificate is to be certified until Dec 27 19:11:59 2021 GMT (3650 days)
 
Sign the certificate? [y/n]:y
 
  
1 out of 1 certificate requests certified, commit? [y/n]y
+
On the '''CA machine''':
Write out database with 1 new entries
 
Data Base Updated
 
</nowiki>}}
 
  
The build-dh script generates the [http://www.rsa.com/rsalabs/node.asp?id=2248 Diffie-Hellman parameters] .pem file needed by the server.
+
# cp /etc/easy-rsa/pki/crl.pem /tmp
 +
# chown foo /tmp/crl.pem
  
{{Note|It would be better to generate a new one for each server, but you can use the same one if you want to.}}
+
On the '''OpenVPN machine''', copy {{ic|crl.pem}} and inform the server to read it:
 +
# mv /tmp/crl.pem /etc/openvpn/server/
 +
# chown root:root /etc/openvpn/server/crl.pem
  
{{hc|# ./build-dh|
+
Edit {{ic|/etc/openvpn/server/server.conf}} uncommenting the crl-verify directive, then [[restart]] openvpn-server@server.service to re-read it:
Generating DH parameters, 2048 bit long safe prime, generator 2
+
This is going to take a long time
+
{{hc|/etc/openvpn/server/server.conf|
..+.............................................................................
 
.
 
 
.
 
.
 +
crl-verify /etc/openvpn/server/crl.pem
 
.
 
.
............+...............+...................................................
+
}}
..................................................................++*++*}}
 
 
 
The build-key script {{ic|# ./build-key <client name>}} generates a client certificate. Make sure that the client name (Common Name when running the script) is unique.
 
  
{{Note|Do not enter a challenge password or company name when the script prompts you for one.}}
+
== Abbreviated example specifically for containerized Openvpn ==
 +
This section is specifically for users wanting to run Openvpn in a Linux container ([[LXC]]).  The code below is designed to be  pasted into a root shell; the standard hash has been omitted to allow for easy copy/paste operations.  It is recommended to have two different shell windows open, one for the host and one for the container.
  
{{hc|# ./build-key bugs|<nowiki>
+
{{Note|
Generating a 2048 bit RSA private key
+
* It is assumed that the CA machine is the host and the server machine is the container.
....++++++
+
* Both the host and container need to have both {{pkg|openvpn}} and {{pkg|easy-rsa}} installed.
.............................................................++++++
+
* The container needs to be running.
writing new private key to 'bugs.key'
+
* Define the name of the container in the CONTAINERNAME variable below.
-----
+
}}
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) [US]:
 
State or Province Name (full name) [CA]:
 
Locality Name (eg, city) [Acme Acres]:
 
Organization Name (eg, company) [Acme]:
 
Organizational Unit Name (eg, section) []:
 
Common Name (eg, your name or your server's hostname) [bugs]:
 
Name [Acme-CA]:
 
Email Address [roadrunner@acmecorp.org]:
 
  
Please enter the following 'extra' attributes
+
On the host:
to be sent with your certificate request
+
CONTAINERNAME=foo
A challenge password []:
+
/etc/easy-rsa
An optional company name []:
+
easyrsa init-pki && easyrsa build-ca
Using configuration from /root/easy-rsa/openssl-1.0.0.cnf
+
cp /etc/easy-rsa/pki/ca.crt /var/lib/lxc/$CONTAINERNAME/rootfs/etc/openvpn/server/
Check that the request matches the signature
 
Signature ok
 
The Subject's Distinguished Name is as follows
 
countryName          :PRINTABLE:'US'
 
stateOrProvinceName  :PRINTABLE:'CA'
 
localityName          :PRINTABLE:'Acme Acres'
 
organizationName      :PRINTABLE:'Acme'
 
commonName            :PRINTABLE:'bugs'
 
name                  :PRINTABLE:'Acme-CA'
 
emailAddress          :IA5STRING:'roadrunner@acmecorp.org'
 
Certificate is to be certified until Dec 27 19:18:27 2021 GMT (3650 days)
 
Sign the certificate? [y/n]:y
 
  
1 out of 1 certificate requests certified, commit? [y/n]y
+
{{Note|One may substitute other names in the 2nd line of this code (the for loop).  At a minimum, one needs to generate a key for the server and for at least 1 client.  The generic words "server" and "client" are shown, but in reality, these can by any words such as the hostname of the container or the name of the intended user.  As well, one can add additional words to the for loop if more than 2 keys are needed.  If that is the case, just be sure to add corresponding lines to the subsequent steps for each of them.}}
Write out database with 1 new entries
 
Data Base Updated
 
</nowiki>}}
 
  
Generate a secret [[Wikipedia:HMAC|Hash-based Message Authentication Code (HMAC)]] by running:
+
In the container:
{{ic|# openvpn --genkey --secret /root/easy-rsa/keys/ta.key}}
+
cd /etc/easy-rsa && easyrsa init-pki
 +
for i in server client; do easyrsa gen-req $i nopass; done
 +
cp /etc/easy-rsa/pki/private/server.key /etc/openvpn/server/
 +
openssl dhparam -out /etc/openvpn/server/dh.pem 2048
 +
openvpn --genkey --secret /etc/openvpn/server/ta.key
  
This will be used to add an additional HMAC signature to all SSL/TLS handshake packetsIn addition any UDP packet not having the correct HMAC signature will be immidiately dropped, protecting against:
+
Back on the host:
 +
easyrsa import-req /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/reqs/junk.req junk
 +
easyrsa import-req /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/reqs/client.req client
 +
easyrsa sign-req client client
 +
easyrsa sign-req server server
 +
mkdir /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/issued/
 +
  mkdir /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/signed/
 +
cp /etc/easy-rsa/pki/issued/*.crt /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/issued/
  
* Portscanning.
+
That will provide the needed files to make an OpenVPN compatible tunnel profile for the client, and the needed server key files for the server. To generate a client profile, refer to  [[OpenVPN#ovpngen]].
* DOS attacks on the OpenVPN UDP port.
 
* SSL/TLS handshake initiations from unauthorized machines.
 
* Any eventual buffer overflow vulnerabilities in the SSL/TLS implementation.
 
  
All the created keys and certificates have been stored in /root/easy-rsa/keys.  If you make a mistake, you can start over by running the clean-all script again.
+
== See also ==
  
{{Warning|This will delete any previously generated certificates stored in /root/easy-rsa/keys, including the [[Wikipedia:Certificate Authority|Certificate Authority (CA)]] certificate.}}
+
* [https://github.com/OpenVPN/easy-rsa/blob/master/README.quickstart.md README.quickstart].
 +
* [https://github.com/OpenVPN/easy-rsa/blob/master/doc/EasyRSA-Advanced.md EASYRSA-Advanced].

Latest revision as of 12:40, 4 September 2017

The first step when setting up OpenVPN is to create a Public Key Infrastructure (PKI). In summary, this consists of:

  • A public master Certificate Authority (CA) certificate and a private key.
  • A separate public certificate and private key pair for each server.
  • A separate public certificate and private key pair for each client.

One can think of the key-based authentication in terms similar to that of how SSH keys work with the added layer of a signing authority (the CA). OpenVPN relies on a bidirectional authentication strategy, so the client must authenticate the server's certificate and in parallel, the server must authenticate the client's certificate. This is accomplished by the 3rd party's signature (the CA) on both the client and server certificates. Once this is established, further checks are performed before the authentication is complete. For more details, see secure-computing's guide.

Note:
  • The process outlined below requires users to securely transfer private key files to/from machines. For the purposes of this guide, using scp is shown, but readers may employ alternative methods as well. Since the Arch default is to deny the root user over ssh, using scp requires transferring ownership of the files to be exported to a non-root user called foo throughout the guide.
  • Avoid generating keys on devices without a good entropy source. See [1].
  • Due to a bug, the vars file is not sourced if it is not specified explicitly.

Certificate Authority (CA)

For security purposes, it is recommended that the CA machine be separate from the machine running OpenVPN.

On the CA machine, install easy-rsa, initialize a new PKI and generate a CA keypair that will be used to sign certificates:

# cd /etc/easy-rsa
# export EASYRSA=$(pwd)
# easyrsa init-pki
# easyrsa build-ca

OpenVPN server files

A functional OpenVPN server requires the following (in alphabetical order):

  1. The CA's public certificate
  2. The Diffie-Hellman (DH) parameters file (needed for TLS mode which is recommended).
  3. The server key pair (a public certificate and a private key).
  4. The Hash-based Message Authentication Code (HMAC) key.

Upon completing the steps outlined in this article, users will have generated the following files on the server:

  1. /etc/openvpn/server/ca.crt
  2. /etc/openvpn/server/dh.pem
  3. /etc/openvpn/server/servername.crt and /etc/openvpn/server/servername.key
  4. /etc/openvpn/server/ta.key

CA public certificate

The CA public certificate /etc/easy-rsa/pki/ca.crt generated in the previous step needs to be copied over to the machine that will be running OpenVPN.

On the CA machine:

# cp /etc/easy-rsa/pki/ca.crt /tmp
# chown foo /tmp/ca.crt
$ scp /tmp/ca.crt foo@hostname-of-openvpn-server:/tmp

On the OpenVPN server machine:

# mv /tmp/ca.crt /etc/openvpn/server/
# chown root:root /etc/openvpn/server/ca.crt

Server certificate and private key

On the OpenVPN server machine, install easy-rsa and generate a key pair for the server:

# cd /etc/easy-rsa
# easyrsa init-pki
# easyrsa gen-req servername nopass
# cp /etc/easy-rsa/pki/private/servername.key /etc/openvpn/server/

This will create two files:

/etc/easy-rsa/pki/reqs/servername.req /etc/easy-rsa/pki/private/servername.key

Diffie-Hellman (DH) parameters file

On the OpenVPN server machine, create the initial dh.pem file:

# openssl dhparam -out /etc/openvpn/server/dh.pem 2048
Note: Although values higher than 2048 (4096 for example) may be used, they take considerably more time to generate and offer little benefit in security but advisable to have the DH prime number length to match the length of the RSA key. See [2]

Hash-based Message Authentication Code (HMAC) key

On the OpenVPN server machine, create the HMAC key:

# openvpn --genkey --secret /etc/openvpn/server/ta.key

This will be used to add an additional HMAC signature to all SSL/TLS handshake packets. In addition any UDP packet not having the correct HMAC signature will be immediately dropped, protecting against:

  • Portscanning.
  • DOS attacks on the OpenVPN UDP port.
  • SSL/TLS handshake initiations from unauthorized machines.
  • Any eventual buffer overflow vulnerabilities in the SSL/TLS implementation.

OpenVPN client files

Client certificate and private key

Any machine can generate client files provided that easy-rsa is installed.

If the pki is not initialized, do so via:

# cd /etc/easy-rsa
# easyrsa init-pki

Generate the client key and certificate:

# cd /etc/easy-rsa
# easyrsa gen-req client1 nopass

This will create two files:

/etc/easy-rsa/pki/reqs/client1.req /etc/easy-rsa/pki/private/client1.key

The gen-req set can be repeated as many times as needed for additional clients.

Sign the certificates and pass them back to the server and clients

Obtain and sign the certificates on the CA

The server and client(s) certificates need to be signed by the CA then transferred back to the OpenVPN server/client(s).

On the OpenVPN server (or the box used to generate the certificate/key pairs):

# cp /etc/easy-rsa/pki/reqs/*.req /tmp
# chown foo /tmp/*.req

Securely transfer the files to the CA machine for signing:

$ scp /tmp/*.req foo@hostname-of-CA:/tmp

On the CA machine, import and sign the certificate requests:

# cd /etc/easy-rsa
# easyrsa import-req /tmp/servername.req servername
# easyrsa import-req /tmp/client1.req client1
# easyrsa sign-req server servername
# easyrsa sign-req client client1

This will create the following signed certificates which can be transferred back to their respective machines:

/etc/easy-rsa/pki/issued/servername.crt /etc/easy-rsa/pki/issued/client1.crt

The leftover .req files can be safely deleted:

# rm -f /tmp/*.req

Pass the signed certificates back to the server and client(s)

On the CA machine, copy the signed certificates and transfer them to the server/client(s):

# cp /etc/easy-rsa/pki/issued/*.crt /tmp
# chown foo /tmp/*.crt
$ scp /tmp/*.crt foo@hostname-of-openvpn_server:/tmp

On the OpenVPN server, move the certificates in place and reassign ownership. For the server:

# mv /tmp/servername.crt /etc/openvpn/server/
# chown root:root /etc/openvpn/server/servername.crt

For the client:

# mkdir /etc/easy-rsa/pki/signed
# mv /tmp/client1.crt /etc/easy-rsa/pki/signed

That is it. To generate the client profile. See: OpenVPN#ovpngen.

Revoking certificates and alerting the OpenVPN server

Revoke a certificate

Over time, it may become necessary to revoke a certificate thus denying access to the affected user(s). This example revokes the "client1" certificate.

On the CA machine:

# cd /etc/easy-rsa
# easyrsa revoke client1
# easyrsa gen-crl

This will produce the CRL file /etc/easy-rsa/pki/crl.pem that needs to be transferred to the OpenVPN server and made active there.

Alert the OpenVPN server

On the CA machine:

# cp /etc/easy-rsa/pki/crl.pem /tmp
# chown foo /tmp/crl.pem

On the OpenVPN machine, copy crl.pem and inform the server to read it:

# mv /tmp/crl.pem /etc/openvpn/server/
# chown root:root /etc/openvpn/server/crl.pem

Edit /etc/openvpn/server/server.conf uncommenting the crl-verify directive, then restart openvpn-server@server.service to re-read it:

/etc/openvpn/server/server.conf
.
crl-verify /etc/openvpn/server/crl.pem
.

Abbreviated example specifically for containerized Openvpn

This section is specifically for users wanting to run Openvpn in a Linux container (LXC). The code below is designed to be pasted into a root shell; the standard hash has been omitted to allow for easy copy/paste operations. It is recommended to have two different shell windows open, one for the host and one for the container.

Note:
  • It is assumed that the CA machine is the host and the server machine is the container.
  • Both the host and container need to have both openvpn and easy-rsa installed.
  • The container needs to be running.
  • Define the name of the container in the CONTAINERNAME variable below.

On the host:

CONTAINERNAME=foo
/etc/easy-rsa
easyrsa init-pki && easyrsa build-ca
cp /etc/easy-rsa/pki/ca.crt /var/lib/lxc/$CONTAINERNAME/rootfs/etc/openvpn/server/
Note: One may substitute other names in the 2nd line of this code (the for loop). At a minimum, one needs to generate a key for the server and for at least 1 client. The generic words "server" and "client" are shown, but in reality, these can by any words such as the hostname of the container or the name of the intended user. As well, one can add additional words to the for loop if more than 2 keys are needed. If that is the case, just be sure to add corresponding lines to the subsequent steps for each of them.

In the container:

cd /etc/easy-rsa && easyrsa init-pki
for i in server client; do easyrsa gen-req $i nopass; done
cp /etc/easy-rsa/pki/private/server.key /etc/openvpn/server/
openssl dhparam -out /etc/openvpn/server/dh.pem 2048
openvpn --genkey --secret /etc/openvpn/server/ta.key

Back on the host:

easyrsa import-req /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/reqs/junk.req junk
easyrsa import-req /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/reqs/client.req client
easyrsa sign-req client client
easyrsa sign-req server server
mkdir /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/issued/
mkdir /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/signed/
cp /etc/easy-rsa/pki/issued/*.crt /var/lib/lxc/$CONTAINERNAME/rootfs/etc/easy-rsa/pki/issued/

That will provide the needed files to make an OpenVPN compatible tunnel profile for the client, and the needed server key files for the server. To generate a client profile, refer to OpenVPN#ovpngen.

See also