一、open***原理
二、安装open***
三、制作相关证书
3.1 制作CA证书3.2 制作Server端证书3.3 制作Client端证书
四、配置Server端
五、配置Client端
5.1 在Windows 系统上5.2 在Open××× server上5.3 配置client段配置文件
一、open***原理
open***通过使用公开密钥(非对称密钥,加密解密使用不同的key,一个称为Publice key,另外一个是Privatekey)对数据进行加密的。这种方式称为TLS加密open***使用TLS加密的工作过程是,首先×××Sevrver端和×××Client端要有相同的CA证书,双方通过交换证书验证双方的合法性,用于决定是否建立×××连接。然后使用对方的CA证书,把自己目前使用的数据加密方法加密后发送给对方,由于使用的是对方CA证书加密,所以只有对方CA证书对应的Privatekey才能解密该数据,这样就保证了此密钥的安全性,并且此密钥是定期改变的,对于窃听者来说,可能还没有破解出此密钥,×××通信双方可能就已经更换密钥了。
二、安装open***
yum方式安装,此处统一使用aliyun中centos和epel源
# rm /etc/yum.repos.d/* -fr# vim /etc/yum.repos.d/ali.repo
[centos6]name=centeros6 basebaseurl= name=epel basebaseurl=http://mirrors.aliyun.com/epel/6/x86_64gpgcheck=0
为避免出现错误,关闭selinux
# setenforce 0;sed -i 's/SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
开始安装open*** server
# yum install -y open***
open***安装完毕后,我们来查看open***的版本,如下:
# open*** --versionOpen××× 2.3.10 x86_64-redhat-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Jan 4 2016library versions: OpenSSL 1.0.1e-fips 11 Feb 2013, LZO 2.03Originally developed by James Yonan Copyright (C) 2002-2010 Open××× Technologies, Inc.Compile time defines: enable_crypto=yes enable_crypto_ofb_cfb=yes enable_debug=yes enable_def_auth=yes enable_dlopen=unknown enable_dlopen_self=unknown enable_dlopen_self_static=unknown enable_fast_install=yes enable_fragment=yes enable_http_proxy=yes enable_iproute2=yes enable_libtool_lock=yes enable_lzo=yes enable_lzo_stub=noenable_management=yes enable_multi=yes enable_multihome=yes enable_pam_dlopen=no enable_password_save=yes enable_pedantic=no enable_pf=yes enable_pkcs11=yes enable_plugin_auth_pam=yes enable_plugin_down_root=yes enable_plugins=yes enable_port_share=yes enable_pthread=yes enable_selinux=no enable_server=yes enable_shared=yes enable_shared_with_static_runtimes=no enable_small=no enable_socks=yes enable_ssl=yes enable_static=yes enable_strict=no enable_strict_options=no enable_systemd=no enable_win32_dll=yes enable_x509_alt_username=yes with_crypto_library=openssl with_gnu_ld=yes with_iproute_path=/sbin/ip with_mem_check=no with_plugindir='$(libdir)/open***/plugins' with_sysroot=no
open***安装完毕后,我们再来安装easy-rsa。
easy-rsa是用来制作open***相关证书的。
安装easy-rsa,使用如下命令:
# yum install -y easy-rsa
查看easy-rsa安装的文件,如下:
[root@centos6 open***]# rpm -ql easy-rsa /usr/share/doc/easy-rsa-2.2.2/usr/share/doc/easy-rsa-2.2.2/COPYING /usr/share/doc/easy-rsa-2.2.2/COPYRIGHT.GPL /usr/share/doc/easy-rsa-2.2.2/doc /usr/share/doc/easy-rsa-2.2.2/doc/Makefile.am /usr/share/doc/easy-rsa-2.2.2/doc/README-2.0/usr/share/easy-rsa /usr/share/easy-rsa/2.0/build-ca /usr/share/easy-rsa/2.0/build-dh /usr/share/easy-rsa/2.0/build-inter /usr/share/easy-rsa/2.0/build-key /usr/share/easy-rsa/2.0/build-key-pass /usr/share/easy-rsa/2.0/build-key-pkcs12 /usr/share/easy-rsa/2.0/build-key-server /usr/share/easy-rsa/2.0/build-req /usr/share/easy-rsa/2.0/build-req-pass /usr/share/easy-rsa/2.0/clean-all /usr/share/easy-rsa/2.0/inherit-inter /usr/share/easy-rsa/2.0/list-crl /usr/share/easy-rsa/2.0/openssl-0.9.6.cnf /usr/share/easy-rsa/2.0/openssl-0.9.8.cnf /usr/share/easy-rsa/2.0/openssl-1.0.0.cnf /usr/share/easy-rsa/2.0/pkitool /usr/share/easy-rsa/2.0/revoke-full /usr/share/easy-rsa/2.0/sign-req /usr/share/easy-rsa/2.0/vars /usr/share/easy-rsa/2.0/whichopensslcnf
我们可以看到easy-rsa已经安装到/usr/share/easy-rsa/目录下。
三、制作相关证书
根据第一章节open***的工作原理,我们可以知道open***的证书分为三部分:CA证书、Server端证书、Client端证书。 下面我们通过easy-rsa分别对其进行制作。
3.1 制作CA证书
open***与easy-rsa安装完毕后,我们可以直接在/usr/share/easy-rsa/2.0 制作相关的证书,但是为了后续的管理证书的方便,我们需要在/etc/open***/目录下创建easy-rsa文件夹, 然后把/usr/share/easy-rsa/目录下的所有文件全部复制到/etc/open***/easy-rsa/下:
# mkdir /etc/open***/easy-rsa/ # cp -r /usr/share/easy-rsa/2.0/* /etc/open***/easy-rsa/ # ll /etc/open***/easy-rsa/ total 116 -rwxr-xr-x. 1 root root 119 Apr 20 15:15 build-ca -rwxr-xr-x. 1 root root 352 Apr 20 15:15 build-dh -rwxr-xr-x. 1 root root 188 Apr 20 15:15 build-inter -rwxr-xr-x. 1 root root 163 Apr 20 15:15 build-key -rwxr-xr-x. 1 root root 157 Apr 20 15:15 build-key-pass -rwxr-xr-x. 1 root root 249 Apr 20 15:15 build-key-pkcs12 -rwxr-xr-x. 1 root root 268 Apr 20 15:15 build-key-server -rwxr-xr-x. 1 root root 213 Apr 20 15:15 build -req-rwxr-xr-x. 1 root root 158 Apr 20 15:15 build-req-pass -rwxr-xr-x. 1 root root 449 Apr 20 15:15 clean-all -rwxr-xr-x. 1 root root 1471 Apr 20 15:15 inherit-interd -rwx------. 2 root root 4096 Apr 26 21:31 keys -rwxr-xr-x. 1 root root 302 Apr 20 15:15 list-crl -rw-r--r--. 1 root root 7791 Apr 20 15:15 openssl-0.9.6.cnf -rw-r--r--. 1 root root 8348 Apr 20 15:15 openssl-0.9.8.cnf -rw-r--r--. 1 root root 8245 Apr 20 15:15 openssl-1.0.0.cnf -rwxr-xr-x. 1 root root 12966 Apr 20 15:15 pkitool -rwxr-xr-x. 1 root root 928 Apr 20 15:15 revoke-full -rwxr-xr-x. 1 root root 178 Apr 20 15:15 sign-req -rw-r--r--. 1 root root 2042 Apr 20 17:01 vars -rwxr-xr-x. 1 root root 740 Apr 20 15:15 whichopensslcnf
在开始制作CA证书之前,我们还需要编辑vars文件,修改如下相关选项内容即可。如下:
# vim /etc/open***/easy-rsa/vars export KEY_COUNTRY="CN" export KEY_PROVINCE="FJ" export KEY_CITY="XiaMEN" export KEY_ORG="user" export KEY_EMAIL="[email protected]" export KEY_OU="user" export KEY_NAME="user"
vars文件主要用于设置证书的相关组织信息,引号部分的内容可以根据自己的实际情况自行修改。
然后使用source vars命令使其生效。
注意:执行clean-all命令会删除,当前目录下keys文件夹里证书等文件。
# source vars.# ./clean-all
现在开始正式制作CA证书,使用如下命令:
# ./build-caGenerating 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 incorporatedinto 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 blankFor 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) [TZ]: Organization Name (eg, company) [CTG]: Organizational Unit Name (eg, section) [open***]: Common Name (eg, your name or your server's hostname) [CTG CA]:Name [open***]: Email Address [[email protected]]:
一路按回车键即可。制作完成
后,我们可以查看keys目录。
# ll /etc/open***/easy-rsa/keys/total 32-rw-r--r--. 1 root root 1639 Apr 27 13:06 ca.crt -rw-------. 1 root root 1704 Apr 27 13:06 ca.key -rw-r--r--. 1 root root 341 Apr 26 21:31 index.txt -rw-r--r--. 1 root root 21 Apr 25 23:16 index.txt.attr -rw-r--r--. 1 root root 21 Apr 25 23:16 index.txt.attr.old -rw-r--r--. 1 root root 2131 Apr 25 23:16 index.txt.old -rw-r--r--. 1 root root3 Apr 25 23:16 serial -rw-r--r--. 1 root root3 Apr 25 23:16 serial.old
我们可以看到已经生成了ca.crt和ca.key两个文件,其中ca.crt就是我们所说的CA证书。至此,CA证书制作完毕。现在把该CA证书的ca.crt文件复制到open***的启动目录/etc/open***下:
# cp /etc/open***/easy-rsa/keys/ca.crt /etc/open*** # ll /etc/open***/total 16268 -rw-r--r--. 1 root root 1639 Apr 20 17:02 ca.crt drwxr-xr-x. 3 root root 4096 Apr 27 13:00 easy-rsa
3.2 制作Server端证书
CA证书制作完成后,我们现在开始制作Server端证书。如下:
# ./build-key-server ***serverGenerating 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 incorporatedinto 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 blankFor 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) [TZ]: Organization Name (eg, company) [CTG]: Organizational Unit Name (eg, section) [open***]: Common Name (eg, your name or your server's hostname) [***server]:Name [open***]: Email Address [[email protected]]: Please enter the following 'extra' attributesto be sent with your certificate request A challenge password []: An optional company name []:Using configuration from /etc/open***/easy-rsa/openssl-1.0.0.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as followscountryName :PRINTABLE:'CN' stateOrProvinceName :PRINTABLE:'BJ' localityName :PRINTABLE:'TZ' organizationName :PRINTABLE:'CTG' organizationalUnitName:PRINTABLE:'open***' commonName:PRINTABLE:'***server' name :PRINTABLE:'open***' emailAddress :IA5STRING:'[email protected]' Certificate is to be certified until Apr 25 05:10:49 2026 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
一路执行并点击两次“y”即可,查看生成的Server端证书:
# ll /etc/open***/easy-rsa/keys/total 56-rw-r--r--. 1 root root 1639 Apr 27 13:06 ca.crt -rw-------. 1 root root 1704 Apr 27 13:06 ca.key -rw-r--r--. 1 root root 458 Apr 27 13:10 index.txt -rw-r--r--. 1 root root 5346 Apr 27 13:10 ***server.crt -rw-r--r--. 1 root root 1058 Apr 27 13:10 ***server.csr -rw-------. 1 root root 1704 Apr 27 13:10 ***server.key
可以看到已经生成了***server.crt、***server.key和***server.csr三个文件。其中***server.crt和***server.key两个文件是我们要使用的。
现在再为服务器生成加密交换时的Diffie-Hellman文件
# ./build-dh Generating DH parameters, 2048 bit long safe prime, generator 2This is going to take a long time ..........+.................................................................. ............................................................................. ...........................+................................................. ....+.......................................................................+ ............................................................................. ........................................+.................................+.. ..................................+.......................................... .................................+........................................... ............................................................................. ..............................+.............................................. .................................................................+........... ..........+..................................................+............... ............................................................................. ............................................................................. .............................+.......................................+....... .....................+....................................................... ............................................................................. .+........................................................................... ..............+.............................................................. ............................................................................. ..............................................+.............................. ............................................................................. .........+.....................................+.........................+... .................+.................................++*++* [root@centos6 easy-rsa]# ll keys/total 60-rw-r--r--. 1 root root 1639 Apr 27 13:06 ca.crt -rw-------. 1 root root 1704 Apr 27 13:06 ca.key -rw-r--r--. 1 root root 424 Apr 27 13:14 dh2048.pem -rw-r--r--. 1 root root 458 Apr 27 13:10 index.txt -rw-r--r--. 1 root root3 Apr 27 13:10 serial -rw-r--r--. 1 root root 5346 Apr 27 13:10 ***server.crt -rw-r--r--. 1 root root 1058 Apr 27 13:10 ***server.csr -rw-------. 1 root root 1704 Apr 27 13:10 ***server.key [root@centos6 easy-rsa]#
已经生成了dh文件dh2048.pem。把***server.crt、***server.key、dh2048.pem复制到/etc/open***/目录下
# cd /etc/open***/easy-rsa # cp keys/***server.crt keys/***server.key keys/dh2048.pem /etc/open***/# ll /etc/open***/total 16492 -rw-r--r--. 1 root root 1639 Apr 20 17:02 ca.crt -rw-r--r--. 1 root root 424 Apr 20 17:07 dh2048.pem drwxr-xr-x. 3 root root 4096 Apr 27 13:00 easy-rsa -rw-r--r--. 1 root root 5342 Apr 20 17:07 open***.crt -rw-------. 1 root root 1704 Apr 20 17:07 open***.key
至此,Server端证书就制作完毕。
3.3 制作Client端证书
Server端证书制作完成后,我们现在开始制作Client端证书。 新建user1的证书
# cd /etc/open***/easy-rsa # ./build-key user1Generating a 2048 bit RSA private key.........+++ ......................................+++ writing new private key to 'user1.key'----- You are about to be asked to enter information that will be incorporatedinto 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 blankFor 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) [TZ]: Organization Name (eg, company) [CTG]: Organizational Unit Name (eg, section) [open***]: Common Name (eg, your name or your server's hostname) [user1]:Name [open***]: Email Address [[email protected]]: Please enter the following 'extra' attributesto be sent with your certificate request A challenge password []: An optional company name []:Using configuration from /etc/open***/easy-rsa/openssl-1.0.0.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as followscountryName :PRINTABLE:'CN' stateOrProvinceName :PRINTABLE:'BJ' localityName :PRINTABLE:'TZ' organizationName :PRINTABLE:'CTG' organizationalUnitName:PRINTABLE:'open***' commonName:PRINTABLE:'user1'name :PRINTABLE:'open***' emailAddress :IA5STRING:'[email protected]' Certificate is to be certified until Apr 25 05:19:17 2026 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 [root@centos6 easy-rsa]# ll keys/total 84-rw-r--r--. 1 root root 1639 Apr 27 13:06 ca.crt -rw-------. 1 root root 1704 Apr 27 13:06 ca.key-rw-r--r--. 1 root root 424 Apr 27 13:14 dh2048.pem -rw-r--r--. 1 root root 571 Apr 27 13:19 index.txt -rw-r--r--. 1 root root3 Apr 27 13:19 serial -rw-r--r--. 1 root root 5216 Apr 27 13:19 user1.crt -rw-r--r--. 1 root root 1050 Apr 27 13:19 user1.csr -rw-------. 1 root root 1704 Apr 27 13:19 user1.key-rw-r--r--. 1 root root 5346 Apr 27 13:10 ***server.crt -rw-r--r--. 1 root root 1058 Apr 27 13:10 ***server.csr -rw-------. 1 root root 1704 Apr 27 13:10 ***server.key[root@centos6 easy-rsa]#
已经生成了user1.csr、user1.crt和user1.key这个三个文件,user1.crt和user1.key两个文件是我们要使用的。
至此,Client端证书就制作完毕。
如果你想快速生成用户证书不需要手工交互的话,可以使用如下命令,例如生成user2
# ./build-key --batch user2Generating a 2048 bit RSA private key..................... ..............................+++ .......................................+++ writing new private key to 'user2.key' -----Using configuration from /etc/open***/easy-rsa/openssl-1.0.0.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as followscountryName :PRINTABLE:'CN' stateOrProvinceName :PRINTABLE:'BJ' localityName :PRINTABLE:'TZ' organizationName :PRINTABLE:'CTG' organizationalUnitName:PRINTABLE:'open***' commonName:PRINTABLE:'user2' name :PRINTABLE:'open***' emailAddress :IA5STRING:'[email protected]' Certificate is to be certified until Apr 25 05:21:01 2026 GMT (3650 days) failed to update database TXT_DB error number 2You have new mail in /var/spool/mail/root
四、配置Server端
所有证书制作完毕后,我们现在开始配置Server端。Server端的配置文件,我们可以从open***自带的模版中进行复制。
# cp /usr/share/doc/open***-2.3.10/sample/sample-config-files/server.conf /etc/open***/server.conf.bak # cd /etc/open***/# lltotal 16720 -rw-r--r--. 1 root root 1639 Apr 20 17:02 ca.crt -rw-r--r--. 1 root root 424 Apr 20 17:07 dh2048.pem drwxr-xr-x. 3 root root 4096 Apr 27 13:00 easy-rsa -rw-r--r--. 1 root root 5342 Apr 20 17:07 open***.crt -rw-------. 1 root root 1704 Apr 20 17:07 open***.key -rw-r--r--. 1 root root10441 Apr 20 17:20 server.conf.bak
我们通过grep修改server.conf.bak文件来生成server.conf文件
# grep -vE "^#|^;|^$" server.conf.bak > server.conf# vim server.conflocal x.x.x.x port 34186proto udp dev tun keepalive 10 120comp-lzo user open*** group open*** persist-key persist-tun server 172.16.98.0 255.255.255.0 push "redirect-gateway def1 bypass-dhcp" //设定所有流量都从这里出去 push "dhcp-option DNS 202.45.84.58" //设定获取到的DNS,这个 香港行政区DNS#push "dhcp-option DNS 223.6.6.6"ca ca.crt cert ***server.crt key ***server.key dh dh2048.pem client-to-client duplicate-cn ifconfig-pool-persist ipp.txt status open***-status.logverb 3
与原模版文件相比,在此我修改几个地方。
第一、修改了open***运行时使用的协议,由原来的UDP协议修改为TCP协议。生成环境建议使用TCP协议。
第二、修改了open***服务器的相关证书,由原来的server.csr、server.key修改为***server.crt、***server.key。
注意:上述server.conf文件中***server.crt、***server.key、dh2048.pem要与/etc/open***/目录下的相关文件一一对应。 同时,如果上述文件如果没有存放在/etc/open***/目录下,在server.conf文件中,我们要填写该文件的绝对路径。
配置文件修改完毕后,我们现在来启动open***,使用如下命令:
# service open*** start Starting open***: [ OK ] # ss -tnlp |grep 1194LISTEN 0 1 *:1194 *:* users:(("open***",1765,5))#
可以的看出open***已经在此启动,使用的TCP协议的1194端口。
五、配置Client端
Server端配置并启动后,我们现在来配置Client端。我们主要在Windows OS上。
5.1 在Windows OS上
下载安装“open***-2.1.1-gui-1.0.3-install-cn-64bit”,地址为: https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/***tech/open***-2.1.1-gui-1.0.3-install-cn-64bit.zip
在windows的Client段,安装完毕Open***后,程序安装路径 缺省目录是:C:\Program Files (x86)\Open×××\ 在C:\Program Files (x86)\Open×××\config下新建一个文件夹存放用户的配置证书
5.2 在Open××× server上
我们都需要把Client证书、CA证书以及Client配置文件下载到Client端。 Client证书我们主要使用crt和key结尾的两个文件,而CA证书我们主要使用crt结尾的文件。在Server端新建一个用户user1存放证书的目录,并将需要的证书文件存放到此目录。
# mkdir /root/user1/ -pv # cp /usr/share/doc/open***-2.3.10/sample/sample-config-files/client.conf /root/user1/client.o*** # cd /etc/open***/easy-rsa/keys# cp user1.crt user1.key /root/user1/# ll /root/user1total 16 -rw-r--r--. 1 root root 3441 Apr 27 13:31 client.o*** -rw-r--r--. 1 root root 5216 Apr 27 13:32 user1.crt -rw-------. 1 root root 1704 Apr 27 13:32 user1.key
存放完毕后,通过sz将这几个文件下载到存放user1配置文件的目录
# cd /root/user1# sz *
5.3 配置client段配置文件
下载完毕后,然后编辑client.o***,如下
client dev tun proto udp remote x.x.x.x 34186resolv-retry infinite nobind persist-key persist-tun ca ca.crtcert user1.crtkey user1.keyns-cert-type server comp-lzo verb 3
Client配置文件client.o***,修改了几个地方:
第一、使用的协议,由原来的UDP修改为TCP,这个一定要和Server端保持一致。否则Client无法连接。
第二、remote地址,这个地址要修改为Server端的地址。
第三、Client证书名称,这个要和我们现在使用用户的Client证书名称保持一致。
现在我们来启动open***客户端连接Server,如下:
点击“连接服务”,会出现如下的弹窗: 如果配置都正确的话,会出现如下的提示:
通过上图,我们可以很明显的看到Client已经正确连接Server端,并且获得的IP地址是1172.1698.0.x。
到此为止,open***的配置完成,
ps: 开启内核转发,做NAT地址命令如下行:
NAT地址转换
# iptables -t nat -A POSTROUTING -s 172.16.98.0/24 -o eth0 -j MASQUERADE # /etc/init.d/iptables save
临时内核转发
# echo 1 > /proc/sys/net/ipv4/ip_forward
永久内核转发
# cat /etc/sysctl.conf | tail -10 | grep "ipv4"net.ipv4.conf.all.arp_notify = 1 # sysctl -p net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 ... kernel.shmmax = 68719476736 kernel.shmall = 4294967296net.ipv4.conf.all.arp_notify = 1