OpenVPN是一个用于创建虚拟专用网络加密通道的软件包,允许创建的VPN使用公开密钥、数字证书、或者用户名/密码来进行身份验证。OpenVPN能在Solaris、Linux、OpenBSD、FreeBSD、NetBSD、Mac OS X与Windows 2000/XP/Vista/7以及Android和iOS上运行,并包含了许多安全性的功能。
配置OpenVPN 2.0的第一步是建立一个PKI(public key infrastructure 公钥基础设施)。PKI包括:
服务端和每个客户端都有一个证书(也称做公钥)和私钥
一个认证机构(CA)的证书和私钥,用来为每一个服务端和客户端颁发证书(sign each of the server and client certificates)。
OpenVPN 支持基于证书的双向认证,也就是说客户端需认证服务端,服务端也要认证客户端,
服务端和客户端验证对方的第一步是验证对方提供的证书是由认证机构(CA)颁发的。然后测试第一步认证通过的证书的头部信息,如:证书的common name,证书的类型(客户端还是服务端)。
从VPN的观点来看,这一安全模式满足一系列要求:
服务端仅仅需要它自己的证书/私钥对 -- 它不需要知道客户端的证书。
服务端仅仅接受拥有CA颁发的证书的客户端。因为服务端检查证书是否由CA颁发时并不需要访问 CA的私钥,这样就可以将CA的私钥(整个PKI中最重要的私钥)放在另一台机子上。
如果一个私钥(is compromised),通过将它的证书加入CRL(证书吊销列表)可以禁止这一私钥。 CRL允许有选择地拒绝compromised 证书而不需要重建整个PKI。
基于嵌入的证书域比如Vommon Name,服务端能够加强客户端待定(client-specific)访问权限 (access fights)。
服务器端配置
首先,我们必须安装OpenVPN软件。在Ubuntu 15.04和其它带有‘apt’包管理器的Unix系统中,可以通过如下命令安装:
开始之前,我们需要拷贝“easy-rsa”到openvpn文件夹。
首先,我们编辑一个“vars”文件。为了简化生成过程,我们需要在里面指定数据。这里是“vars”文件的一个样例:
export KEY_COUNTRY="CN"
export KEY_PROVINCE="BJ"
export KEY_CITY="Beijing"
export KEY_ORG="Linux.CN"
export KEY_EMAIL="open@vpn.linux.cn"
export KEY_OU=server
希望这些字段名称对你而言已经很清楚,不需要进一步说明了。
其次,我们需要拷贝openssl配置。另外一个版本已经有现成的配置文件,如果你没有特定要求,你可以使用它的上一个版本。这里是1.0.0版本。
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) [CN]:
State or Province Name (full name) [BJ]:
Locality Name (eg, city) [Beijing]:
Organization Name (eg, company) [Linux.CN]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) [Linux.CN CA]:
Name [EasyRSA]:
Email Address [open@vpn.linux.cn]:
接下来,我们需要生成一个服务器密钥
Generating a 2048 bit RSA private key
........................................................................+++
............................+++
writing new private key to 'server.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) [CN]:
State or Province Name (full name) [BJ]:
Locality Name (eg, city) [Beijing]:
Organization Name (eg, company) [Linux.CN]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) [Linux.CN server]:
Name [EasyRSA]:
Email Address [open@vpn.linux.cn]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/2.0/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :PRINTABLE:'BJ'
localityName :PRINTABLE:'Beijing'
organizationName :PRINTABLE:'Linux.CN'
organizationalUnitName:PRINTABLE:'Tech'
commonName :PRINTABLE:'Linux.CN server'
name :PRINTABLE:'EasyRSA'
emailAddress :IA5STRING:'open@vpn.linux.cn'
Certificate is to be certified until May 22 19:00:25 2025 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
这里,最后两个关于“签署证书”和“提交”的问题,我们必须回答“yes”。
现在,我们已经有了证书和服务器密钥。下一步,就是去省城Diffie-Hellman密钥。执行以下命令,耐心等待。在接下来的几分钟内,我们将看到许多点和加号。
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
................................+................<许多的点>
在漫长的等待之后,我们可以继续生成最后的密钥了,该密钥用于TLS验证。命令如下:
如果需要密码保护密钥,你需要运行另外一个命令,命令如下
现在,我们需要将以下文件从服务器拷贝到客户端/etc/openvpn/keys/文件夹。
服务器文件列表:
ca.crt,
dh2048.pem,
client.crt,
client.key,
ta.key.
在此之后,我们转到客户端,准备配置文件。配置文件位于/etc/openvpn/client.conf,内容如下
安卓客户端配置
安卓设备上的OpenVPN配置和Unix系统上的十分类似,我们需要一个含有配置文件、密钥和证书的包。文件列表如下:
配置文件 (扩展名 .ovpn),
ca.crt,
dh2048.pem,
client.crt,
client.key.
客户端密钥生成方式和先前章节所述的一样。
配置文件内容如下
然后,我们需要安装一个OpenVPN Connect 应用。
接下来,配置过程很是简单:
打开 OpenVPN 并选择“Import”选项
选择“Import Profile from SD card”
在打开的窗口中导航到我们放置好文件的目录,并选择那个 .ovpn 文件
应用会要求我们创建一个新的配置文件
点击“Connect”按钮并稍等一下
搞定。现在,我们的安卓设备已经通过安全的VPN连接连接到我们的专用网。
尾声
虽然OpenVPN初始配置花费不少时间,但是简易的客户端配置为我们弥补了时间上的损失,也提供了从任何设备连接的能力。此外,OpenVPN提供了一个很高的安全等级,以及从不同地方连接的能力,包括位于NAT后面的客户端。因此,OpenVPN可以同时在家和企业中使用。