Advertisement
ABIX_Edukacja

openvpn_howto

Dec 6th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. Setting up your own Certificate Authority (CA) and generating certificates and keys for an OpenVPN server and multiple clients
  2. Overview
  3.  
  4. The first step in building an OpenVPN 2.0 configuration is to establish a PKI (public key infrastructure). The PKI consists of:
  5.  
  6. * a separate certificate (also known as a public key) and private key for the server and each client, and
  7. * a master Certificate Authority (CA) certificate and key which is used to sign each of the server and client certificates.
  8.  
  9. OpenVPN supports bidirectional authentication based on certificates, meaning that the client must authenticate the server certificate and the server must authenticate the client certificate before mutual trust is established.
  10.  
  11. Both server and client will authenticate the other by first verifying that the presented certificate was signed by the master certificate authority (CA), and then by testing information in the now-authenticated certificate header, such as the certificate common name or ce
  12. rtificate type (client or server).
  13.  
  14. This security model has a number of desirable features from the VPN perspective:
  15.  
  16. * The server only needs its own certificate/key -- it doesn't need to know the individual certificates of every client which might possibly connect to it.
  17. * The server will only accept clients whose certificates were signed by the master CA certificate (which we will generate below). And because the server can perform this signature verification without needing access to the CA private key itself, it is possible for the
  18. CA key (the most sensitive key in the entire PKI) to reside on a completely different machine, even one without a network connection.
  19. * If a private key is compromised, it can be disabled by adding its certificate to a CRL (certificate revocation list). The CRL allows compromised certificates to be selectively rejected without requiring that the entire PKI be rebuilt.
  20. * The server can enforce client-specific access rights based on embedded certificate fields, such as the Common Name.
  21.  
  22. Generate the master Certificate Authority (CA) certificate & key
  23.  
  24. In this section we will generate a master CA certificate/key, a server certificate/key, and certificates/keys for 3 separate clients.
  25.  
  26. For PKI management, we will use a set of scripts bundled with OpenVPN.
  27.  
  28. If you are using Linux, BSD, or a unix-like OS, open a shell and cd to the easy-rsa subdirectory of the OpenVPN distribution. If you installed OpenVPN from an RPM file, the easy-rsa directory can usually be found in /usr/share/doc/packages/openvpn or /usr/share/doc/openvp
  29. n-2.0 (it's best to copy this directory to another location such as /etc/openvpn, before any edits, so that future OpenVPN package upgrades won't overwrite your modifications). If you installed from a .tar.gz file, the easy-rsa directory will be in the top level directory
  30. of the expanded source tree.
  31.  
  32. If you are using Windows, open up a Command Prompt window and cd to \Program Files\OpenVPN\easy-rsa. Run the following batch file to copy configuration files into place (this will overwrite any preexisting vars.bat and openssl.cnf files):
  33.  
  34. init-config
  35.  
  36. Now edit the vars file (called vars.bat on Windows) and set the KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, and KEY_EMAIL parameters. Don't leave any of these parameters blank.
  37.  
  38. Next, initialize the PKI. On Linux/BSD/Unix:
  39.  
  40. . ./vars
  41. ./clean-all
  42. ./build-ca
  43.  
  44. On Windows:
  45.  
  46. vars
  47. clean-all
  48. build-ca
  49.  
  50. The final command (build-ca) will build the certificate authority (CA) certificate and key by invoking the interactive openssl command:
  51.  
  52. ai:easy-rsa # ./build-ca
  53. Generating a 1024 bit RSA private key
  54. ............++++++
  55. ...........++++++
  56. writing new private key to 'ca.key'
  57. -----
  58. You are about to be asked to enter information that will be incorporated
  59. into your certificate request.
  60. What you are about to enter is what is called a Distinguished Name or a DN.
  61. There are quite a few fields but you can leave some blank
  62. For some fields there will be a default value,
  63. If you enter '.', the field will be left blank.
  64. -----
  65. -----
  66. Country Name (2 letter code) [KG]:
  67. State or Province Name (full name) [NA]:
  68. Locality Name (eg, city) [BISHKEK]:
  69. Organization Name (eg, company) [OpenVPN-TEST]:
  70. Organizational Unit Name (eg, section) []:
  71. Common Name (eg, your name or your server's hostname) []:OpenVPN-CA
  72. Email Address [me@myhost.mydomain]:
  73.  
  74. Note that in the above sequence, most queried parameters were defaulted to the values set in the vars or vars.bat files. The only parameter which must be explicitly entered is the Common Name. In the example above, I used "OpenVPN-CA".
  75. Generate certificate & key for server
  76.  
  77. Next, we will generate a certificate and private key for the server. On Linux/BSD/Unix:
  78.  
  79. ./build-key-server server
  80.  
  81. On Windows:
  82.  
  83. build-key-server server
  84.  
  85. As in the previous step, most parameters can be defaulted. When the Common Name is queried, enter "server". Two other queries require positive responses, "Sign the certificate? [y/n]" and "1 out of 1 certificate requests certified, commit? [y/n]".
  86. Generate certificates & keys for 3 clients
  87.  
  88. Generating client certificates is very similar to the previous step. On Linux/BSD/Unix:
  89.  
  90. ./build-key client1
  91. ./build-key client2
  92. ./build-key client3
  93.  
  94. On Windows:
  95.  
  96. build-key client1
  97. build-key client2
  98. build-key client3
  99.  
  100. If you would like to password-protect your client keys, substitute the build-key-pass script.
  101.  
  102. Remember that for each client, make sure to type the appropriate Common Name when prompted, i.e. "client1", "client2", or "client3". Always use a unique common name for each client.
  103. Generate Diffie Hellman parameters
  104.  
  105. Diffie Hellman parameters must be generated for the OpenVPN server. On Linux/BSD/Unix:
  106.  
  107. ./build-dh
  108.  
  109. On Windows:
  110.  
  111. build-dh
  112.  
  113. Output:
  114.  
  115. ai:easy-rsa # ./build-dh
  116. Generating DH parameters, 1024 bit long safe prime, generator 2
  117. This is going to take a long time
  118. .................+...........................................
  119. ...................+.............+.................+.........
  120. ......................................
  121.  
  122. Key Files
  123.  
  124. Now we will find our newly-generated keys and certificates in the keys subdirectory. Here is an explanation of the relevant files:
  125. Filename Needed By Purpose Secret
  126. ca.crt server + all clients Root CA certificate NO
  127. ca.key key signing machine only Root CA key YES
  128. dh{n}.pem server only Diffie Hellman parameters NO
  129. server.crt server only Server Certificate NO
  130. server.key server only Server Key YES
  131. client1.crt client1 only Client1 Certificate NO
  132. client1.key client1 only Client1 Key YES
  133. client2.crt client2 only Client2 Certificate NO
  134. client2.key client2 only Client2 Key YES
  135. client3.crt client3 only Client3 Certificate NO
  136. client3.key client3 only Client3 Key YES
  137.  
  138. The final step in the key generation process is to copy all files to the machines which need them, taking care to copy secret files over a secure channel.
  139.  
  140. Now wait, you may say. Shouldn't it be possible to set up the PKI without a pre-existing secure channel?
  141.  
  142. The answer is ostensibly yes. In the example above, for the sake of brevity, we generated all private keys in the same place. With a bit more effort, we could have done this differently. For example, instead of generating the client certificate and keys on the server, we
  143. could have had the client generate its own private key locally, and then submit a Certificate Signing Request (CSR) to the key-signing machine. In turn, the key-signing machine could have processed the CSR and returned a signed certificate to the client. This could have b
  144. een done without ever requiring that a secret .key file leave the hard drive of the machine on which it was generated.
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement