StrongSwan

出自 Arch Linux 中文维基

IPSec 是一個加密和認證標準,可以組建安全虛擬專用網路(VPN).

Linux kernel 已經支持這個標準,但是用戶需要配置加密密鑰。IPSec VPN 使用 IKE 協議自動協商密鑰的交換,交換方式包括認證、提前共享或同時使用。

通常用伺服器上的一個後台進程實現,strongSwan 是一個完全支持 IKEv1 和 IKEv2 的 IKE 後台進程。大部分現代的客戶端都支持,包括 Linux, Windows 7, Apple iOS, Mac OSX, FreeBSD and BlackBerry OS.

安裝[編輯 | 編輯原始碼]

安裝軟件包 strongswan

證書[編輯 | 編輯原始碼]

第一步是生成 X.509 認證,包括一個認證機構(CA)證書,一個伺服器證書和至少一個客戶端證書。

認證機構[編輯 | 編輯原始碼]

首先創建一個自己簽名的根 CA 證書:

生成 4096 位 RSA 私鑰 strongswanKey.pem:

$ cd /etc/ipsec.d/
$ ipsec pki --gen --type rsa --size 4096 --outform pem > private/strongswanKey.pem
$ chmod 600 private/strongswanKey.pem

生成自簽名的 CA 證書 strongswanCert.pem,期限是 10 年(3650天),用 PEM 編碼格式保存:

$ ipsec pki --self --ca --lifetime 3650 --in private/strongswanKey.pem --type rsa --dn "C=CH, O=strongSwan, CN=strongSwan Root CA" \
	   --outform pem > cacerts/strongswanCert.pem

可以根據需要修改 Distinguished Name (DN),country (C), organization (O), and common name (CN)。

通過下面命令查看新生成的證書:

$ ipsec pki --print --in cacerts/strongswanCert.pem

輸出:

cert:      X509
subject:  "C=CH, O=strongSwan, CN=strongSwan Root CA"
issuer:   "C=CH, O=strongSwan, CN=strongSwan Root CA"
validity:  not before Nov 22 11:55:41 2013, ok
           not after  Nov 20 11:55:41 2023, ok (expires in 3649 days)
serial:    65:39:93:df:a0:f8:40:03
flags:     CA CRLSign self-signed
authkeyId: 45:30:11:da:a4:0e:0b:0a:a3:41:a5:81:41:ab:d8:04:7a:40:6c:c0
subjkeyId: 45:30:11:da:a4:0e:0b:0a:a3:41:a5:81:41:ab:d8:04:7a:40:6c:c0
pubkey:    RSA 4096 bits
keyid:     dc:15:91:95:04:07:a5:13:69:5f:77:65:26:d7:02:3f:60:ec:73:c8
subjkey:   45:30:11:da:a4:0e:0b:0a:a3:41:a5:81:41:ab:d8:04:7a:40:6c:c0
警告: CA 的私鑰 /etc/ipsec.d/private/strongswanKey.pem 必須保存在安全位置,最好放在一個沒有網絡訪問的簽名專用伺服器。如果此密鑰泄露,整個公鑰系統就土崩瓦解了。

主機證書[編輯 | 編輯原始碼]

此證書被用來認證 VPN 伺服器,運行下面命令:

生成 2048 位 RSA 私鑰:

$ cd /etc/ipsec.d/
$ ipsec pki --gen --type rsa --size 2048 --outform pem > private/vpnHostKey.pem
$ chmod 600 private/vpnHostKey.pem

導出公鑰並生成被 CA 簽名的 vpnHostCert.pem,證書有效期是兩年(730天),使用完整域名 vpn.example.com 作為身份

$ ipsec pki --pub --in private/vpnHostKey.pem --type rsa | \
	 ipsec pki --issue --lifetime 730 \
	  --cacert cacerts/strongswanCert.pem \
	  --cakey private/strongswanKey.pem \
	  --dn "C=CH, O=strongSwan, CN=vpn.example.com" \
	  --san vpn.example.com \
	  --flag serverAuth --flag ikeIntermediate \
	  --outform pem > certs/vpnHostCert.pem

後面的客戶端連接配置中,會使用 VPN 伺服器的域名或 IP 地址,必須將其加入 Distinguished Name 或 Alternative Name (line11), 最好都包含。請注意把 vpn.example.com 修改為 VPN 的主機名 - 否則客戶端和伺服器的連接會失敗。

注意: 如果要使用 Win7 內置的 VPN 客戶端,必須按上面例子在主機證書中添加 serverAuth 標記。OS X 10.7.3 和更早的版本需要 ikeIntermediate 標記。這些標記不影響其它功能,可以放心使用。

查看新生成的證書:

$ ipsec pki --print --in certs/vpnHostCert.pem

Output:

cert:      X509
subject:  "C=CH, O=strongSwan, CN=vpn.example.com"
issuer:   "C=CH, O=strongSwan, CN=strongSwan Root CA"
validity:  not before Nov 22 21:16:51 2013, ok
           not after  Nov 22 21:16:51 2015, ok (expires in 729 days)
serial:    0c:05:d7:d5:57:0e:d9:48
altNames:  vpn.zeitgeist.se
flags:     serverAuth iKEIntermediate 
authkeyId: 9b:57:35:fb:cd:9e:2d:20:37:1d:61:4c:e7:c4:5b:5e:dc:64:ad:fc
subjkeyId: 5f:12:c2:06:ee:2b:1e:cc:5f:78:54:ff:f0:f3:7b:a0:2b:c0:b4:d6
pubkey:    RSA 2048 bits
keyid:     6f:a7:99:60:27:27:09:96:02:c1:b9:d9:7d:c1:b0:10:e3:e1:d5:45
subjkey:   5f:12:c2:06:ee:2b:1e:cc:5f:78:54:ff:f0:f3:7b:a0:2b:c0:b4:d6

客戶端證書[編輯 | 編輯原始碼]

客戶端需要個人證書才能使用 VPN. 和生成主機證書類似,只是這裏用客戶端的電子郵件而不是主機名:

生成 2048 位 RSA 私鑰:

$ cd /etc/ipsec.d/
$ ipsec pki --gen --type rsa --size 2048 --outform pem > private/ClientKey.pem
$ chmod 600 private/ClientKey.pem

導出公鑰並用 CA 簽名,證書時間是兩年。

$ ipsec pki --pub --in private/ClientKey.pem --type rsa | \
	 ipsec pki --issue --lifetime 730 \
	  --cacert cacerts/strongswanCert.pem \
	  --cakey private/strongswanKey.pem \
	  --dn "C=CH, O=strongSwan, CN=myself@example.com" \
	  --san myself@example.com \
	  --outform pem > certs/ClientCert.pem

將所有證書和密鑰用密碼打包成 PKCS#12 文件,以方便客戶端使用。

$ openssl pkcs12 -export -inkey private/ClientKey.pem \
	  -in certs/ClientCert.pem -name "My own VPN client certificate" \
	  -certfile cacerts/strongswanCert.pem \
	  -caname "strongSwan Root CA" \
	  -out Client.p12

不同的 VPN 模式[編輯 | 編輯原始碼]

最簡單的配置方式是用隧道模式運行 IPSec。

IPSec 隧道模式[編輯 | 編輯原始碼]

可以在 /etc/ipsec.conf 找到 VPN 配置,下面配置包含了基本 VPN 伺服器需要的選項:

/etc/ipsec.conf
# ipsec.conf - strongSwan IPsec configuration file
config setup

  # By default only one client can connect at the same time with an identical
  # certificate and/or password combination. Enable this option to disable
  # this behavior.
  # uniqueids=never

  # Slightly more verbose logging. Very useful for debugging.
  charondebug="cfg 2, dmn 2, ike 2, net 2"

# Default configuration options, used below if an option is not specified.
# See: https://wiki.strongswan.org/projects/strongswan/wiki/ConnSection
conn %default

  # Use IKEv2 by default
  keyexchange=ikev2

  # Prefer modern cipher suites that allow PFS (Perfect Forward Secrecy)
  ike=aes128-sha256-ecp256,aes256-sha384-ecp384,aes128-sha256-modp2048,aes128-sha1-modp2048,aes256-sha384-modp4096,aes256-sha256-modp4096,aes256-sha1-modp4096,aes128-sha256-modp1536,aes128-sha1-modp1536,aes256-sha384-modp2048,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp1024,aes128-sha1-modp1024,aes256-sha384-modp1536,aes256-sha256-modp1536,aes256-sha1-modp1536,aes256-sha384-modp1024,aes256-sha256-modp1024,aes256-sha1-modp1024!
  esp=aes128gcm16-ecp256,aes256gcm16-ecp384,aes128-sha256-ecp256,aes256-sha384-ecp384,aes128-sha256-modp2048,aes128-sha1-modp2048,aes256-sha384-modp4096,aes256-sha256-modp4096,aes256-sha1-modp4096,aes128-sha256-modp1536,aes128-sha1-modp1536,aes256-sha384-modp2048,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp1024,aes128-sha1-modp1024,aes256-sha384-modp1536,aes256-sha256-modp1536,aes256-sha1-modp1536,aes256-sha384-modp1024,aes256-sha256-modp1024,aes256-sha1-modp1024,aes128gcm16,aes256gcm16,aes128-sha256,aes128-sha1,aes256-sha384,aes256-sha256,aes256-sha1!

  # Dead Peer Discovery
  dpdaction=clear
  dpddelay=300s

  # Do not renegotiate a connection if it is about to expire
  rekey=no

  # Server side
  left=%any
  leftsubnet=0.0.0.0/0
  leftcert=vpnHostCert.pem

  # Client side
  right=%any
  rightdns=8.8.8.8,8.8.4.4
  rightsourceip=%dhcp

# IKEv2: Newer version of the IKE protocol
conn IPSec-IKEv2
  keyexchange=ikev2
  auto=add

# IKEv2-EAP
conn IPSec-IKEv2-EAP
  also="IPSec-IKEv2"
  rightauth=eap-mschapv2
  rightsendcert=never
  eap_identity=%any

# IKEv1 (Cisco-compatible version)
conn CiscoIPSec
  keyexchange=ikev1
  # forceencaps=yes
  rightauth=pubkey
  rightauth2=xauth
  auto=add

IPSec in transport mode[編輯 | 編輯原始碼]

這篇文章的某些內容需要擴充。

原因: Need to better explain the differences, advantages and inconvenients, then write a tutorial. (在 Talk:StrongSwan 中討論)

IPSec/L2TP[編輯 | 編輯原始碼]

L2TP/IPsec VPN client setup 頁面包含了如何建立一個連接到 IPSec/L2TP 伺服器的客戶端。這種 IPSec VPN 和純 IPSec 相比,可以將隧道用於非 IP 包,缺點是需要運行一個額外的 L2TP 後台進程。

這篇文章的某些內容需要擴充。

原因: Need to better explain the differences, advantages and inconvenients, then write a tutorial. (在 Talk:StrongSwan 中討論)

Secrets[編輯 | 編輯原始碼]

strongSwan 需要知道哪些客戶端是允許連接到 VPN 的,通過 /etc/ipsec.secrets 文件進行配置:

/etc/ipsec.secrets
# This file holds shared secrets or RSA private keys for authentication.

# RSA private key for this host, authenticating it to any other host
# which knows the public part.  Suitable public keys, for ipsec.conf, DNS,
# or configuration of other implementations, can be extracted conveniently
# with "ipsec showhostkey".

: RSA ClientKey.pem
user1 : EAP "topsecretpassword"
user2 : XAUTH "evenmoretopsecretpassword"

當 strongSwan 運行時,如果編輯了 /etc/ipsec.secrets,需要重新加載:

$ ipsec rereadsecrets

Networking[編輯 | 編輯原始碼]

需要執行下面設備讓 VPN 伺服器正常的進行 VPN 隧道傳輸:

/etc/sysctl.d/10-net-forward.conf
# VPN
net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0

上面的 VPN 配置用 DHCP 自動給客戶端分配 IP 地址,所以需要一個 DHCP 服務,如果這個服務是運行在 strongSwan 相同的伺服器上,需要編輯 /etc/strongswan.d/charon/dhcp.conf:

/etc/strongswan.d/charon/dhcp.conf
dhcp {
 force_server_address = yes
 server = 192.168.0.255
}

在防火牆中開放如下協議:

  • ESP (Encrypted Secure Payload): Standard IPSec traffic
  • UDP 4500: IPSec traffic in "NAT Traversal" mode
  • UDP 500: Key exchanges (IKE)

最後,startenable strongswan 服務。

在容器中運行 Strongswan[編輯 | 編輯原始碼]

要在容器中運行 strongswan,比如在 systemd-nspawn 中,需要類似下面的服務文件:

/etc/systemd/system/systemd-nspawn@.service.d/override.conf}
[Service]
ExecStart=
ExecStart=/usr/bin/systemd-nspawn --quiet --keep-unit --boot --link-journal=try-guest --settings=override --machine=%I --capability=CAP_NET_ADMIN --network-veth 

Troubleshooting[編輯 | 編輯原始碼]

這篇文章的某些內容需要擴充。

原因: Need to explain at least ip xfrm and common issues (在 Talk:StrongSwan 中討論)

參閱[編輯 | 編輯原始碼]