一.strongsman ipsec
IPsec是虚拟私密网络(VPN)的一种,用于在服务器和客户端之间建立加密隧道并传输敏感数据之用。它由两个阶段组成,第一阶段(Phrase 1, ph1),交换密钥建立连接,使用互联网密钥交换(ike)协议; 第二阶段(Phrase 2, ph2),连接建立后对数据进行加密传输,使用封装安全载荷(esp)协议。
其中,第一阶段和第二阶段可以使用不同的加密方法(cipher suites)。甚至,第一阶段ike协议的第一版(ikev1)有两种模式,主力模式(main mode)和积极模式(aggressive mode),主力模式进行六次加密握手,而积极模式并不加密,以实现快速建立连接的目的。
二.生成秘钥和证书
(1)生成ca的秘钥和证书
strongswan pki --gen --outform pem > caKey.pem
strongswan pki --self --outform pem --in caKey.pem --dn "C=CN, O=TZ, CN=TZ CA" --ca > caCert.pem
(2)生成服务端的秘钥和证书
strongswan pki --gen --outform pem > serverKey.pem
strongswan pki --pub --outform pem --in serverKey.pem > serverPub.pem
strongswan pki --issue --outform pem --cacert caCert.pem --cakey caKey.pem \
--in serverPub.pem --dn "C=CN, O=TZ, CN=TZ Server" --san="52.34.162.76" \
--flag serverAuth --flag ikeIntermediate > serverCert.pem
(3)生成客户端的秘钥和证书
strongswan pki --gen --outform pem > clientKey.pem
strongswan pki --pub --outform pem --in clientKey.pem > clientPub.pem
strongswan pki --issue --outform pem --cacert caCert.pem --cakey caKey.pem \
--in clientPub.pem --dn "C=CN, O=TZ, CN=TZ Client" > clientCert.pem
(4)复制安装证书
cp caCert.pem /etc/strongswan/ipsec.d/cacerts/
cp serverCert.pem /etc/strongswan/ipsec.d/certs/
cp serverKey.pem /etc/strongswan/ipsec.d/private/
cp clientCert.pem /etc/strongswan/ipsec.d/certs/
cp clientKey.pem /etc/strongswan/ipsec.d/private/
三.修改配置文件
(1)编辑/etc/strongswan/ipsec.conf
config setup
uniqueids=never #允许多个客户端使用同一个证书
conn IKEv2-EAP
keyexchange=ikev2 #密钥交换算法
left=%any #服务器端标识,%any表示任意
leftid=服务器公网ip #服务器端ID标识
leftsubnet=0.0.0.0/0 #服务器端虚拟ip, 0.0.0.0/0表示通配.
leftcert=serverCert.pem #服务器端证书
leftauth=pubkey #服务器校验方式,使用证书
right=%any #客户端标识,%any表示任意
rightsourceip=192.168.0.0/24 #客户端IP地址分配范围
rightauth=eap-mschapv2 #eap-md5#客户端校验方式#KEv2 EAP(Username/Password)
#rightauth=rsa #客户端校验方式,使用证书#IKEv2 Certificate
#rightcert=clientCert.pem #客户端端证书#IKEv2 Certificate
#eap_identity=%any #
auto=add
(2)编辑/etc/strongswan/ipsec.secrets
: RSA serverKey.pem
: PSK "12345678"
test : EAP "123456"
e : EAP "e"
d : EAP "d"
a : EAP "a"
(3)编辑配置文件/etc/strongswan/strongswan.conf
# strongswan.conf - strongSwan configuration file
# Refer to the strongswan.conf(5) manpage for details
# Configuration changes should be made in the included files
charon {
load_modular = yes
duplicheck.enable = no
compress = yes
dns1 = 8.8.8.8
dns2 = 8.8.4.4
nbns1 = 8.8.8.8
nbns2 = 8.8.4.4
plugins {
include strongswan.d/charon/*.conf
}
#以下是日志输出, 生产环境请关闭.
filelog {
/etc/strongswan/charon.log {
# add a timestamp prefix
time_format = %b %e %T
# prepend connection name, simplifies grepping
ike_name = yes
# overwrite existing files
append = no
# increase default loglevel for all daemon subsystems
default = 1
# flush each line to disk
flush_line = yes
}
}
}
include strongswan.d/*.conf