OpenVPN Service Forward
In these instructions, commands that should be pasted or typed into your terminal session are in
blue.
The process may look daunting, but realize you are mostly entering only the stuff in blue.
The majority of the material here is explanation of what you are doing.
OpenVPN Service Creation via ssh Session
Create your own Certificate Authority (CA) on the router and use it to create and sign certificates for authentication.
We also create the OpenVPN service interface, firewall rules, and adjust DHCP listening
You should have already set up time zone and allowed NTP in the EdgeRouter's System panel
Your EdgerRouter WAN should be connected to the internet and clients on your main LAN should already be able to browse the internet. A live internet connection is needed during this process so time and date are correct doing the setup process.
Also enable ssh in the EdgeRouter system panel. You must "save" to make that setting take effect.
Most of the work is done as root via an SSH session. In the System tab of EdgeRouter GUI, enable SSH. Then, start SSH session in terminal of your computer. we will issue you are logging under the admin username.
ssh admin@192.168.91.1
A SSH session will be opened and you will be asked for your admin password. Enter your admin account password
Once logged in, change to superuser mode with….
sudo su -
Extend Expiration Periods of Created CA and server/client certificates.
Default expiration periods for Certificate Authority (CA) and server/client certificates are quite short. I prefer to adjust the expirations to something quite long.
That means editing some files using the vi editor. vi is a text editor built into the unix kernel of the EdgeRouter.
vi usage hints for once a file is opened
- scroll up and down using up down arrow keys
- Press "i" key (without quote) to start editing the contents (i starts insert mode)
- Once done editing, press ESC a couple times to end editing mode
- Once ready to quit vi, type the following
- to save the changes and quit, type
:wq followed by a return
- to simply quit, abandoning your edits, type
:q
Extend validity of the CA (Certificate Authority):
(I may already have this done for you)
vi /usr/lib/ssl/misc/CA.pl
Arrow down until you see
my $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} || "";
my $DAYS = "-days 365";
my $CADAYS = "-days 1095"; # 3 years
my $REQ = "$openssl req $OPENSSL_CONFIG";
Change $DAY and $CADAY to make them 10 years
my $DAYS = "-days 3650";
my $CADAYS = "-days 3650"; # 10 years
Once done editing hit ESC key a few times to end editing
:wq to write changes and quite vi
Extend the validity of the Server/Client Certificates
vi /usr/lib/ssl/openssl.cnf
Arrow down until you see
default_days = 365 # how long to certify for
Change
default_days to make it 3650 days, aka 10 years.
default_days = 3650 # how long to certify for
Once done editing hit ESC key a few times to end editing
:wq to write changes and quite vi
Create Certificate Authority
As a precaution we delete any existing demoCA directory
cd /usr/lib/ssl/misc/
rm -rf demoCA
Also delete old misc and auth from /config
cd /config
rm -rf misc
rm -rf auth
Recreate an auth directory
mkdir auth
Navigate back into the necessary directory
cd /usr/lib/ssl/misc/
Create our new CA certificate.
./CA.pl -newca
Type enter key to create a new certificate.
Every time we create a certificate we go through an interactive process where you will be asked for several things. Try to be consistent with your answers. The PEM pass phrase is the most critical item. You must be type in the pem pass phrase multiple times during OpenVPN certificate creation. You CANNOT paste in the pass phrase. It must by typed in. The other responses can be pasted. I usually keep a list of the responses in a separate file for easy cut/paste.
Enter PEM pass phrase:
pempassphrase (You cannot copy paste this! Must type it in manually)
You will be asked twice.
Country Name (2 letter code) [AU]:
US
State or Province Name (full name) [Some-State]: WA
Locality Name (eg, city) []:
cityName
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
MyCompanyName
Organizational Unit Name (eg, section) []:
divisionName
Common Name (e.g. server FQDN or YOUR name) []:
myServerName
Email Address []:
mymail@provider.com
Simply leave the following extra attributes blank
A challenge password []:
An optional company name []:
Eventually you will be asked for a pass phrase. Simple to just use same pass phrase
Enter pass phrase for ./demoCA/private/cakey.pem:
pempassphrase
You will see quite a bit of text scroll by as the certificate is generated.
eventually you should see something like...
Signature ok
Certificate Details:
Serial Number:
eb:b5:f3:45:e8:27:08:c7
Validity
Not Before: Jan 24 05:20:58 2020 GMT
Not After : Jan 21 05:20:58 2030 GMT
Subject:
countryName = US
stateOrProvinceName = WA
organizationName = MyCompanyName
organizationalUnitName = divisionName
commonName = myServerName
emailAddress = mymail@provider.com
X509v3 extensions:
X509v3 Subject Key Identifier:
DA:4E:4F:BF:8C:C3:4B:78:CF38:2B0:A7:57:337:A5:B5:64
X509v3 Authority Key Identifier:
keyidA:4E:4F:BF:8C:C3:4B:78:CF38:2B0:A7:57:337:A5:B5:64
X509v3 Basic Constraints: critical
CA:TRUE
Certificate is to be certified until Jan 21 05:20:58 2030 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
==> 0
====
CA certificate is in ./demoCA/cacert.pem
You can ignore the error messages about index.txt.attr.
What matters is that singnature was OK, you see the database was written with 1 new entry and CA certificate is in ./demoCA/cacert.pem
Once new certificate authority creation completes, you have a new directory called demoCA. The two most important items in there are as follows:
private/cakey.pem – This is the private key for your CA (always keep this secret)
cacert.pem – This the public key for your CA (you’ll be giving this out to your clients)
Backup Certificate Authority to preserve it across firmware updates
EdgeRouter firmware upgrades destroy the directory in which your certificate authority and keys exist.
/usr/lib/ssl/misc
Firmware upgrades, therefore, destroy the certificate authority and its keys.
To allow use to restore our CA after a firmware update, we duplicate the CA to a location that is saved during config exports.
We back up the CA
before generating and moving client keys to /config/auth. Backing up the CA later will destroy keys in /config/auth.
So, back up your CA NOW with this command.
cp -r /usr/lib/ssl/misc /config/
Again, above must be
before further key generation or restoring the backup will overwrite your generated keys.
How does this preserve your CA? Exporting a config file includes the copy of your CA along with the rest of your router configuration.
When config file is restored, the data for your CA is loaded in our copy location. We simply to copy it back to the CA's normal working location to restore the CA.
———— Begin Restore CA after a firmware upgrade - Do NOT do this now ——
Restore Certificate Authority after Firmware Update
Don't do this now. These instructions are for after a firmware update. It simply made sense to document the process here.
You must have an exported config file to upload.
Upload your config file via EdgeRouter System GUI.
Restore CA misc directory from /config back to /usr/lib/ssl with
ssh admin@192.168.91.1
sudo su -
cp -r /config/misc /usr/lib/ssl/
End ssh session
exit
———— End Restore CA after a firmware upgrade - Do NOT do this now ——
Create Server Certificate
Next, we generate a public/private key for the server.
./CA.pl -newreq
You will go through same interactive process of entering pass phrase and parameters.
The Common Name (CN) of your server certificate should be something unique like your dynamic DNS host name
Eventually you get to...
Request is in newreq.pem, private key is in newkey.pem
Sign the newly created server certificate with the following command
./CA.pl -sign
You will be asked for your pem passphrase.
Type it in correctly. If you make a mistake, you will see errors like….
unable to load CA private key
2013087232:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:../crypto/evp/evp_enc.c:536:
2013087232:error:23077074KCS12 routinesKCS12_pbe_cryptkcs12 cipherfinal error:../crypto/pkcs12/p12_decr.c:63:
2013087232:error:2306A075KCS12 routinesKCS12_item_decrypt_d2ikcs12 pbe crypt error:../crypto/pkcs12/p12_decr.c:94:
2013087232:error:0907B00DEM routinesEM_read_bio_PrivateKey:ASN1 lib:../crypto/pem/pem_pkey.c:87:
Assuming you enter the pass phrase correctly, you get details for the certificate and asked whether to sign and commit.
Answer Y to both. Eventually the database will be written one with 1 new entry.
Write out database with 1 new entries
Data Base Updated
==> 0
====
Signed certificate is in newcert.pem
Once this completes, you have three new files, as follows. You can list the files in directory with
ls command.
newkey.pem – This is the private key for your server (keep this secret)
newreq.pem – This is the unsigned public key of the server (this needs to be signed by your CA)
newcert.pem – This is the signed public key for your server
We previously backed up the CA. However, these new files should also be backed up.
Copy the important server files to a directory in config where they won’t be wiped out during a firmware upgrade.
In addition to moving the files, we’re also renaming them.
cp /usr/lib/ssl/misc/demoCA/cacert.pem /config/auth/
cp /usr/lib/ssl/misc/demoCA/private/cakey.pem /config/auth/
mv /usr/lib/ssl/misc/newcert.pem /config/auth/host.pem
mv /usr/lib/ssl/misc/newkey.pem /config/auth/host.key
Diffie-Hellman Parameters
Generate new Diffie-Hellman (DH) parameters to ensure Perfect Forward Secrecy (PFS).
Believe it when it says that it takes a long time. EdgeRouter will need 5 to 20 minutes to complete.
openssl dhparam -out /config/auth/dh2048.pem -2 2048
Decrypt Host Key
We must decrypt the password from the host and later generated client(s) keys so that OpenVPN can run in interactive mode.
openssl rsa -in /config/auth/host.key -out /config/auth/host-decrypted.key
You will be asked for your pempassphrase
We have now completed the certificates for the server. The next task is creating user certificates for each client.
Create & Decrypt Client Certificate(s)
For each client, we
- generate a request and sign it for each new user certificate.
- Move the new files into your preserved directory while renaming them.
The Common Name (CN) of your user certificate should be something
unique like the client machine name.
COMMON NAME must not have been previously used or it will not be accepted in the registration database.
Repeat for each client as below. Take care that file names are adjusted for each client (seen in red)
———
Begin certificate for phone
Create the certificate.
./CA.pl -newreq
You will be asked for the usual inputs for certificate creation.
When it comes to the common name, give each client a unique name. I use
myPhone here
Eventually you get....
Request is in newreq.pem, private key is in newkey.pem
Sign the freshly created newcert.pem and newkey.pem
./CA.pl -sign
Usual yes to sign and commit
We rename newcert.pem to myPhone.pem and newly.pem to myPhone.key
mv newcert.pem /config/auth/myPhone.pem
mv newkey.pem /config/auth/myPhone.key
Decrypt to allow OpenVPN run in interactive mode
openssl rsa -in /config/auth/myPhone.key -out /config/auth/myPhone-decrypted.key
End certificate for phone
———
Begin certificate for laptop
Create the certificate.
./CA.pl -newreq
You will be asked for the usual inputs for certificate creation.
When it comes to the common name, give each client a unique name. I use
myLaptop here
Eventually you get....
Request is in newreq.pem, private key is in newkey.pem
Sign the freshly created newcert.pem and newkey.pem
./CA.pl -sign
Usual yes to sign and commit
We rename newcert.pem to myLaptop.pem and newly.pem to myLaptop.key
mv newcert.pem /config/auth/myLaptop.pem
mv newkey.pem /config/auth/myLaptop.key
Decrypt to allow OpenVPN run in interactive mode
openssl rsa -in /config/auth/myLaptop.key -out /config/auth/myLaptop-decrypted.key
End certificate for laptop
———
Done Making Certificate Authority and Creating Certificates
Finally after all that, we are done creating certificates.
Exit back to normal ubnt user mode
exit
whoami
whoami should report admin instead of root.