搭建Open××× Server路由模式、证书认证

 

一、搭建Open××× Server 路由模式、证书认证
1、安装Open×××
 a) 安装openssl

  1. rpm -qa |grep openssl

  2. yum install -y openssl openssl-deve

 b) 安装 lzo
  1. cd /opt

  2. wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz 

  3. tar -zxvf lzo-2.03.tar.gz

  4. cd lzo-2.03

  5. ./configure

  6. make;make install

c)安装Open×××
  1. cd /opt

  2. wget http://open***.net/release/open***-2.1_rc15.tar.gz

  3. tar -zxvf open***-2.1_rc15.tar.gz

  4. cd open***-2.1_rc15

  5. ./configure

  6. make;make install

  7. mkdir -p /etc/open*** 

  8. cp -r /opt/open***-2.1_rc15/easy-rsa /etc/open***/easy-rs

2、配置 Open×××
a)初始化PKI 
设置环境变量
  1. vi /root/.bash_profile 

复制代码
KEY_COUNTRY=CN
KEY_PROVINCE=GD
KEY_CITY=SZ
KEY_ORG="kerryhu"
KEY_OU="IT"
KEY_EMAIL="king_819@163.com"

或者手动通过export执行
cd /etc/open***/easy-rsa/2.0
export KEY_COUNTRY=CN
# 定义你所在的国家,2个字符 
export KEY_PROVINCE=GD
# 你所在的省份 
export KEY_CITY=SZ
# 你所在的城市 
export KEY_ORG="kerryhu"
# 你所在的组织 
export KEY_OU="IT" 
# 你的部门
export KEY_EMAIL="king_819@163.com"
# 你的邮件地址

或者从vars中读取
  1. export KEY_COUNTRY=CN

  2. export KEY_PROVINCE=GD

  3. export KEY_CITY=SZ 

  4. export KEY_ORG="kerryhu"

  5. export KEY_OU="IT" 

  6. export KEY_EMAIL="king_819@163.com"

复制代码
  1. cd /etc/open***/easy-rsa/2.0

  2. source ./vars

复制代码
b) 创建证书颁发机构(CA)
  1. cd /etc/open***/easy-rsa/2.0

  2. ./clean-all

  3. ./build-ca

复制代码
  1. Generating a 1024 bit RSA private key

  2. .................................++++++

  3. .............++++++

  4. writing new private key to 'ca.key'

  5. -----

  6. You are about to be asked to enter information that will be incorporated

  7. into your certificate request.

  8. What you are about to enter is what is called a Distinguished Name or a DN.

  9. There are quite a few fields but you can leave some blank

  10. For some fields there will be a default value,

  11. If you enter '.', the field will be left blank.

  12. -----

  13. Country Name (2 letter code) [CN]: //2位的国家代码

  14. State or Province Name (full name) [GD]: //省份

  15. Locality Name (eg, city) [SZ]: //城市名称

  16. Organization Name (eg, company) [kerryhu]: //公司名称

  17. Organizational Unit Name (eg, section) []: IT //部门名称

  18. Common Name (eg, your name or your server's hostname) [kerryhu CA]: 

  19. Name []:

  20. Email Address [[email]king_819@163.com[/email]]:

复制代码
c) 建立server key
  1. ./build-key-server server

复制代码
  1. Generating a 1024 bit RSA private key

  2. ...............................................++++++

  3. ....................++++++

  4. writing new private key to 'kerryhu_CA.key'

  5. -----

  6. You are about to be asked to enter information that will be incorporated

  7. into your certificate request.

  8. What you are about to enter is what is called a Distinguished Name or a DN.

  9. There are quite a few fields but you can leave some blank

  10. For some fields there will be a default value,

  11. If you enter '.', the field will be left blank.

  12. -----

  13. Country Name (2 letter code) [CN]:

  14. State or Province Name (full name) [GD]:

  15. Locality Name (eg, city) [SZ]:

  16. Organization Name (eg, company) [kerryhu]:

  17. Organizational Unit Name (eg, section) []:IT

  18. Common Name (eg, your name or your server's hostname) [kerryhu_CA]:

  19. Name []:

  20. Email Address [[email]king_819@163.com[/email]]:

  21.  

  22. Please enter the following 'extra' attributes

  23. to be sent with your certificate request

  24. A challenge password []:1q2w3e4r

  25. An optional company name []:kerryhu

  26. Using configuration from /usr/local/open***/easy-rsa/2.0/openssl.cnf

  27. Check that the request matches the signature

  28. Signature ok

  29. The Subject's Distinguished Name is as follows

  30. countryName :PRINTABLE:'CN'

  31. stateOrProvinceName :PRINTABLE:'GD'

  32. localityName :PRINTABLE:'SZ'

  33. organizationName :PRINTABLE:'kerryhu'

  34. organizationalUnitName:PRINTABLE:'IT'

  35. commonName :T61STRING:'kerryhu_CA'

  36. emailAddress :IA5STRING:'king_819@163.com'

  37. Certificate is to be certified until Apr 24 08:06:41 2020 GMT (3650 days)

  38. Sign the certificate? [y/n]:y

  39.  

  40.  

  41. 1 out of 1 certificate requests certified, commit? [y/n]y

  42. Write out database with 1 new entries

  43. Data Base Updated

复制代码
d) 生成客户端 key
  1. ./build-key client1

复制代码
  1. Generating a 1024 bit RSA private key

  2. ..++++++

  3. .........................++++++

  4. writing new private key to 'client1.key'

  5. -----

  6. You are about to be asked to enter information that will be incorporated

  7. into your certificate request.

  8. What you are about to enter is what is called a Distinguished Name or a DN.

  9. There are quite a few fields but you can leave some blank

  10. For some fields there will be a default value,

  11. If you enter '.', the field will be left blank.

  12. -----

  13. Country Name (2 letter code) [CN]:

  14. State or Province Name (full name) [GD]:

  15. Locality Name (eg, city) [SZ]:

  16. Organization Name (eg, company) [kerryhu]:

  17. Organizational Unit Name (eg, section) []:IT

  18. Common Name (eg, your name or your server's hostname) [client1]: //每个不同的client生成的证书, 名字必须不同

  19. Name []:

  20. Email Address [[email]king_819@163.com[/email]]:client1@163.com //那么Email地址也不能相同

  21.  

  22. Please enter the following 'extra' attributes

  23. to be sent with your certificate request

  24. A challenge password []:1q2w3e 

  25. An optional company name []:kerryhu

  26. Using configuration from /usr/local/open***/easy-rsa/2.0/openssl.cnf

  27. Check that the request matches the signature

  28. Signature ok

  29. The Subject's Distinguished Name is as follows

  30. countryName :PRINTABLE:'CN'

  31. stateOrProvinceName :PRINTABLE:'GD'

  32. localityName :PRINTABLE:'SZ'

  33. organizationName :PRINTABLE:'kerryhu'

  34. organizationalUnitName:PRINTABLE:'IT'

  35. commonName :PRINTABLE:'client1'

  36. emailAddress :IA5STRING:'king_819@163.com'

  37. Certificate is to be certified until Apr 24 08:09:57 2020 GMT (3650 days)

  38. Sign the certificate? [y/n]:y

  39.  

  40. 1 out of 1 certificate requests certified, commit? [y/n]y

  41. Write out database with 1 new entries

  42. Data Base Updated

复制代码
以此类推建立其他客户端 key
  1. ./build-key tom

  2. ./build-key jemes

复制代码
e) 创建Diffie Hellman 参数,Diffie Hellman 用于增强安全性,在Open×××是必须的
  1. ./build-dh 

复制代码
问题:./build-dh: line 7: dhparam: command not found
解决办法:
  1. vi build-dh

复制代码
把$OPENSSL 改成 openssl



生成 ta.key
  1. cd keys/ 

  2. open*** --genkey --secret ta.key 

  3.  

  4. cp ca.crt server.crt server.key dh1024.pem ta.key /etc/open***/conf/

复制代码
f) 将keys下的ca.crt、clent1.crt、clent1.key打包下载到本地open***安装目录下的config目录中,可以通过winscp,http,ftp等等......)
  1. tar -zcvf keys.tar.gz keys/

复制代码
g) 创建服务端配置文件
  1. vi /etc/open***/server.conf

复制代码
  1. #申明本机使用的IP地址,也可以不说明 

  2. ;local a.b.c.d 

  3. #申明使用的端口,默认1194 

  4. port 1194 

  5. #申明使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议 

  6. ;proto tcp 

  7. proto udp 

  8. #申明使用的设备可选tap和tun,tap是二层设备,支持链路层协议。 

  9. #tun是ip层的点对点协议,限制稍微多一些 

  10. dev tap 

  11. ;dev tun 

  12. #Open×××使用的ROOT CA,使用build-ca生成的,用于验证客户是证书是否合法 

  13. ca conf/ca.crt 

  14. #Server使用的证书文件 

  15. cert conf/server.crt 

  16. #Server使用的证书对应的key,注意文件的权限,防止被盗 

  17. key conf/server.key # This file should be kept secret 

  18. #CRL文件的申明,被吊销的证书链,这些证书将无法登录 

  19. crl-verify ***crl.pem 

  20. #上面提到的生成的Diffie-Hellman文件 

  21. dh conf/dh1024.pem 

  22. #这是一条命令的合集,如果你是Open×××的老用户,就知道这条命令的来由 

  23. #这条命令等效于: 

  24. # mode server #Open×××工作在Server模式,可以支持多client同时动态接入 

  25. # tls-server #使用TLS加密传输,本端为Server,Client端为tls-client

  26. tls-auth conf/ta.key 0 


  27. # if dev tun: #如果使用tun设备,等效于以下配置 

  28. # ifconfig 10.8.0.1 10.8.0.2 #设置本地tun设备的地址 

  29. # ifconfig-pool 10.8.0.4 10.8.0.251 #说明Open×××使用的地址池(用于分配给客户),分别是起始地址、结束地址 

  30. # route 10.8.0.0 255.255.255.0 #增加一条静态路由,省略下一跳地址,下一跳为对端地址,这里是: 10.8.0.2 

  31. # if client-to-client: #如果使用client-to-client这个选项 

  32. # push "route 10.8.0.0 255.255.255.0" #把这条路由发送给客户端,客户连接成功后自动加入路由表,省略了下一跳地址: 10.8.0.1 

  33. # else 

  34. # push "route 10.8.0.1" #否则发送本条路由,这是一个主机路由,省略了子网掩码和下一跳地址,分别为: 255.255.255.255 10.8.0.1 


  35. # if dev tap: #如果使用tap设备,则等效于以下命令 

  36. # ifconfig 10.8.0.1 255.255.255.0 #配置tap设备的地址 

  37. # ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0 #客户端使用的地址池,分别是起始地址、结束地址、子网掩码 

  38. # push "route-gateway 10.8.0.1" #把环境变量route-gateway传递给客户机 


  39. server 10.8.0.0 255.255.255.0 #地址池 

  40. #用于记录某个Client获得的IP地址,类似于dhcpd.lease文件, 

  41. #防止open***重新启动后“忘记”Client曾经使用过的IP地址 

  42. ifconfig-pool-persist ipp.txt 

  43. #Bridge状态下类似DHCPD的配置,为客户分配地址,由于这里工作在路由模式,所以不使用 

  44. ;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100 

  45. #通过××× Server往Client push路由,client通过pull指令获得Server push的所有选项并应用 

  46. ;push "route 192.168.10.0 255.255.255.0" 

  47. ;push "route 192.168.20.0 255.255.255.0" 

  48. #×××启动后,在××× Server上增加的路由,×××停止后自动删除 

  49. ;route 10.9.0.0 255.255.255.252 

  50. #Run script or shell command cmd to validate client 

  51. #virtual addresses or routes. 具体查看manual 

  52. ;learn-address ./script 

  53. #其他的一些需要PUSH给Client的选项 


  54. #使Client的默认网关指向×××,让Client的所有Traffic都通过×××走 

  55. ;push "redirect-gateway" 

  56. #DHCP的一些选项,具体查看Manual 

  57. ;push "dhcp-option DNS 10.8.0.1" 

  58. ;push "dhcp-option WINS 10.8.0.1" 

  59. #如果可以让××× Client之间相互访问直接通过open***程序转发, 

  60. #不用发送到tun或者tap设备后重新转发,优化Client to Client的访问效率 

  61. client-to-client 

  62. #如果Client使用的CA的Common Name有重复了,或者说客户都使用相同的CA 

  63. #和keys连接×××,一定要打开这个选项,否则只允许一个人连接××× 

  64. ;duplicate-cn 

  65. #NAT后面使用×××,如果×××长时间不通信,NAT Session可能会失效, 

  66. #导致×××连接丢失,为防止之类事情的发生,keepalive提供一个类似于ping的机制, 

  67. #下面表示每10秒通过×××的Control通道ping对方,如果连续120秒无法ping通, 

  68. #认为连接丢失,并重新启动×××,重新连接 

  69. #(对于mode server模式下的open***不会重新连接)。 

  70. keepalive 10 120 

  71. #上面提到的HMAC防火墙,防止DOS***,对于所有的控制信息,都使用HMAC signature, 

  72. #没有HMAC signature的控制信息不予处理,注意server端后面的数字肯定使用0,client使用1 

  73. tls-auth ta.key 0 # This file is secret 

  74. #对数据进行压缩,注意Server和Client一致 

  75. comp-lzo 

  76. #定义最大连接数 

  77. ;max-clients 100 

  78. #定义运行open***的用户 

  79. user nobody 

  80. group nobody 

  81. #通过keepalive检测超时后,重新启动×××,不重新读取keys,保留第一次使用的keys 

  82. persist-key 

  83. #通过keepalive检测超时后,重新启动×××,一直保持tun或者tap设备是linkup的, 

  84. #否则网络连接会先linkdown然后linkup 

  85. persist-tun 

  86. #定期把open***的一些状态信息写到文件中,以便自己写程序计费或者进行其他操作 

  87. status /var/logs/open***-status.log 

  88. #记录日志,每次重新启动open***后删除原有的log信息 

  89. log /var/log/open***.log 

  90. #和log一致,每次重新启动open***后保留原有的log信息,新信息追加到文件最后 

  91. ;log-append open***.log 

  92. #相当于debug level,具体查看manual 

  93. verb 3 

复制代码
h)启动Open×××
  1. /usr/local/sbin/open*** --daemon --config /etc/open***/server.conf 

复制代码
  1. echo "/usr/local/sbin/open*** --daemon --config /etc/open***/server.conf" >> /etc/rc.local

复制代码
i)配置Open××× Server防火墙(开启nat转发);如果出果客户端连接×××后无法上网,那应该是nat转发没打开
并且启用 IP 转发:
  1. echo 1 > /proc/sys/net/ipv4/ip_forward

复制代码
或者:
  1. sysctl -w net.ipv4.ip_forward=1

复制代码
  1. iptables -t nat -A POSTROUTING -j MASQUERADE

  2. iptables -A INPUT -p udp --dport 1194 -j ACCEPT

复制代码
客户端分配静态IP:

在/etc/open***/server.conf中增加\
client-config-dir /etc/open***/ccd

然后在 /etc/open***/ccd目录中放针对每个客户端的个性化配置文件。
文件名就用客户端名 生成key的时候输入的 "Common Name" 名字

比如要设置客户端 kerryhu 为 10.8.0.8 
只要在 /etc/open***/ccd/liushiwei文件中包含一行:
  1. ifconfig-push 10.8.0.8 255.255.255.0

复制代码
3、客户端配置
安装open***-2.1_rc20-install.exe,修改虚拟网卡名(haw),编辑安装目录config 文件中的 client.o*** 文件
  1. client

  2. dev tap0

  3. dev-node haw //本地虚拟网卡名称

  4. proto udp

  5. remote 221.31.15.49 1194

  6. nobind

  7. persist-key

  8. persist-tun

  9. ca ca.crt

  10. cert james.crt //客户端证书名称

  11. key james.key

  12. ns-cert-type server

  13. comp-lzo

  14. verb 3

  15. tls-auth ta.key 1

复制代码
4、吊销Open×××客户端(下面例子中将×××客户端king证书吊销)。
生成吊销证书列表。
  1. cd open***/easy-rsa/2.0

  2. source ./vars

  3. ./revoke-full king 

复制代码
在上述命令执行完成后会在open***/easy-rsa/2.0/keys目录下生成一个crl.pem文件,该文件中包含了吊销证书的名单。也可通过open***/easy-rsa/2.0/keys/index.txt 文件,可以看到被注销的证书前面,已标记为R。
将crl.pem复制到/etc/open***,并在server.conf中加入如下内容,重启open***服务即可。 crl-verify /etc/open***/crl.pem 

 

本文转自http://bbs.chinaunix.net/thread-3629213-1-1.html