「StrongSwan」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
108行目: 108行目:
   
 
$ cd /etc/ipsec.d/
 
$ cd /etc/ipsec.d/
$ ipsec pki --gen --type rsa --size 2048 \
+
$ ipsec pki --gen --type rsa --size 2048 --outform pem > private/ClientKey.pem
--outform pem \
 
> private/ClientKey.pem
 
 
$ chmod 600 private/ClientKey.pem
 
$ chmod 600 private/ClientKey.pem
 
$ ipsec pki --pub --in private/ClientKey.pem --type rsa | \
 
$ ipsec pki --pub --in private/ClientKey.pem --type rsa | \
ipsec pki --issue --lifetime 730 \
+
ipsec pki --issue --lifetime 730 --outform pem \
--cacert cacerts/strongswanCert.pem \
+
--cacert cacerts/strongswanCert.pem \
--cakey private/strongswanKey.pem \
+
--cakey private/strongswanKey.pem \
--dn "C=CH, O=strongSwan, CN=myself@example.com" \
+
--dn "C=CH, O=strongSwan, CN=myself@example.com" \
--san myself@example.com \
+
--san myself@example.com \
--outform pem > certs/ClientCert.pem
+
> certs/ClientCert.pem
$ 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
 
   
 
2048ビットの RSA 秘密鍵 {{ic|ClientKey.pem}} (4行目) が作成されます。6行目では公開鍵を抽出してパイプで渡し、クライアント証明書 {{ic|ClientCert.pem}} (12行目) を発行して CA で署名しています。証明書の有効期限は2年 (730日) で、メールアドレスによってクライアントは識別されます (例: {{ic|myself@example.com}})。最後のコマンドは必要な証明書と鍵を全て PKCS#12 ファイルに詰め込んでいます。クライアントが使用するときに便利なフォーマットです。
 
2048ビットの RSA 秘密鍵 {{ic|ClientKey.pem}} (4行目) が作成されます。6行目では公開鍵を抽出してパイプで渡し、クライアント証明書 {{ic|ClientCert.pem}} (12行目) を発行して CA で署名しています。証明書の有効期限は2年 (730日) で、メールアドレスによってクライアントは識別されます (例: {{ic|myself@example.com}})。最後のコマンドは必要な証明書と鍵を全て PKCS#12 ファイルに詰め込んでいます。クライアントが使用するときに便利なフォーマットです。
  +
  +
最後に、必要なすべての証明書とキーをパスフレーズを含む PKCS#12 ファイルにバンドルします。これはクライアントにとって最も便利な形式です。
  +
  +
$ openssl pkcs12 -export -name "My own VPN client certificate" \
  +
-inkey private/ClientKey.pem \
  +
-in certs/ClientCert.pem \
  +
-certfile cacerts/strongswanCert.pem \
  +
-caname "strongSwan Root CA" \
  +
-out Client.p12
   
 
== VPN ==
 
== VPN ==

2023年6月13日 (火) 03:04時点における版

関連記事

IPSec はセキュアな仮想プライベートネットワーク (VPN) を作成するための暗号化・認証標準です。

Linux カーネルによってネイティブにサポートされていますが、暗号鍵の設定はユーザーが行う必要があります。IPSec VPN では、証明書や事前共有鍵、あるいはその両方を使って自動的に鍵の交換を行うために IKE プロトコルが使われます。

IKE プロトコルは通常、サーバー側のユーザースペースデーモンで実装されます。strongSwan は IKEv1 と IKEv2 を完全にサポートしている IKE デーモンです。Linux, Windows 7, Apple iOS, Mac OSX, FreeBSD, BlackBerry OS など多数のクライアントがネイティブでサポートしています。

インストール

strongswan パッケージをインストールしてください。

証明書

まずは X.509 証明書を生成します。認証局 (CA)、サーバー証明書、(最低でもひとつの) クライアント証明書が必要です。

認証局

自己署名ルート CA 証明書を作成:

$ cd /etc/ipsec.d/
$ ipsec pki --gen --type rsa --size 4096 \
	   --outform pem \
	   > private/strongswanKey.pem
$ chmod 600 private/strongswanKey.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

4096ビットの RSA 秘密鍵 strongswanKey.pem (4行目) と10年間 (3650日間) 有効な自己署名 CA 証明書 strongswanCert.pem (10行目) が作成されます。ファイルは PEM 形式で保存されます。

識別名 (DN) の 国 (C), 組織 (O), 一般名 (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 サーバーの認証に使用する証明書です。以下のコマンドを実行してください:

$ cd /etc/ipsec.d/
$ ipsec pki --gen --type rsa --size 2048 --outform pem > private/vpnHostKey.pem
$ chmod 600 private/vpnHostKey.pem
$ ipsec pki --pub --in private/vpnHostKey.pem --type rsa | \
      ipsec pki --issue --lifetime 730 --outform pem \
                --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 \
          > certs/vpnHostCert.pem

2048ビットの RSA 秘密鍵 vpnHostKey.pem (4行目) が作成されます。6行目では公開鍵を抽出してパイプで渡し vpnHostCert.pem (13行目) を発行してホスト証明書を CA で署名しています。証明書の有効年数は2年 (730日) です。VPN ホストは完全修飾ドメイン名 (FQDN) で識別します (例: vpn.example.com)

警告: 後でクライアントの接続プロパティに入力する VPN サーバーのドメイン名または IP アドレスは、サブジェクトの識別名 (CN=) および/またはサブジェクトの代替名のいずれかに含まれている必要があります。(--san) ただし、両方であることが望ましいです。両方とも vpn.example.com を VPN のホスト名に置き換えてください。そうしないと、クライアントとサーバー間の接続が失敗します。
ノート: Windows 7 に同梱されている VPN クライアントを使用する場合は、上のコマンドにあるようにホスト証明書に serverAuth 拡張鍵使用フラグを追加してください。追加しないとクライアントが接続を拒否します。さらに、OS X 10.7.3 以下では ikeIntermediate フラグが必須です。フラグを追加することで何か問題が発生したりはしないので、毎回追加するべきです。

新しく生成された証明書のプロパティを確認するには、次のコマンドを実行:

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

出力:

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 を使用するには個人証明書が必要になります。ホスト証明書の生成とほとんど同じですが、ホストネームではなくクライアントのメールアドレスを使ってクライアント証明書を識別するところだけ違います。

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

2048ビットの RSA 秘密鍵 ClientKey.pem (4行目) が作成されます。6行目では公開鍵を抽出してパイプで渡し、クライアント証明書 ClientCert.pem (12行目) を発行して CA で署名しています。証明書の有効期限は2年 (730日) で、メールアドレスによってクライアントは識別されます (例: myself@example.com)。最後のコマンドは必要な証明書と鍵を全て PKCS#12 ファイルに詰め込んでいます。クライアントが使用するときに便利なフォーマットです。

最後に、必要なすべての証明書とキーをパスフレーズを含む PKCS#12 ファイルにバンドルします。これはクライアントにとって最も便利な形式です。

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

VPN

IPSec をトンネルモードで使うのが一番簡単な設定です。

トンネルモードの IPSec

VPN の設定は /etc/ipsec.conf にあります。以下はベーシックな 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/L2TP

L2TP/IPsec VPN クライアント設定のページには IPSec/L2TP サーバーに接続するクライアントの設定方法が書かれています。追加の L2TP デーモンを実行する必要がありますが、純粋な IPSec と比べて、IP 以外のパケットをトンネルできるという利点があります。

シークレット

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

ネットワーク

サーバーの設定はほとんど完了です。後は 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): 標準の IPSec トラフィック
  • UDP 4500: "NAT Traversal" モードの IPSec トラフィック
  • UDP 500: 鍵交換 (IKE)

最後に、strongswan サービスを起動有効化してください。

Strongswan をコンテナで実行

systemd-nspawn などのコンテナで strongswan を実行するには、以下のサービスファイルが必要です:

/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 

参照