IPsecする 1. カーネルをIPsec対応にする options IPSEC options IPSEC_ESP 2. 手動設定でIPsecする 2台とも手動でSADとSPDを設定する 1台目のIPv6アドレス 2001:240:7:101:260:1dff:fef2:7648 2台目のIPv6アドレス 2001:240:7:100::1:6 1台目の設定 spdflush; spdadd 2001:240:7:101:260:1dff:fef2:7648 2001:240:7:100::1:6 any -P out ipsec esp/transport//require; spdadd 2001:240:7:100::1:6 2001:240:7:101:260:1dff:fef2:7648 any -P in ipsec esp/transport//require; flush; add 2001:240:7:101:260:1dff:fef2:7648 2001:240:7:100::1:6 esp 0x111 -E 3des-cbc "This is a Secret Key !!!" -A hmac-sha1 "This is a Secret Key"; add 2001:240:7:100::1:6 2001:240:7:101:260:1dff:fef2:7648 esp 0x112 -E 3des-cbc "This is a Secret Key !!!" -A hmac-sha1 "This is a Secret Key"; 2台目の設定 spdflush; spdadd 2001:240:7:100::1:6 2001:240:7:101:260:1dff:fef2:7648 any -P out ipsec esp/transport//require; spdadd 2001:240:7:101:260:1dff:fef2:7648 2001:240:7:100::1:6 any -P in ipsec esp/transport//require; flush; add 2001:240:7:101:260:1dff:fef2:7648 2001:240:7:100::1:6 esp 0x111 -E 3des-cbc "This is a Secret Key !!!" -A hmac-sha1 "This is a Secret Key"; add 2001:240:7:100::1:6 2001:240:7:101:260:1dff:fef2:7648 esp 0x112 -E 3des-cbc "This is a Secret Key !!!" -A hmac-sha1 "This is a Secret Key"; ※ 指定した2台間通信にIPsecを使うことを必須としている ※ SPIは1台目->2台目を 0x111 とし2台目->1台目を 0x112 としている ※ ESPを利用し、暗号化と認証を行う。ペイロードのみ認証されIPヘッダは認証 されないことに注意 ※ 暗号化のアルゴリズムに3des-cbcを使用し、鍵を "This is a Secret Key!!!"(24文字 192bit) としている。 ※ 認証のアルゴリズムにhmac-sha1を使用し、鍵を "This is a Secret Key"(20文字 160bit) としている。 設定ファイルを ipsec.conf などに保存し次のコマンドで読み込む # setkey -f ipsec.conf 片方だけ設定した状態では通信が止まるが、もう一方も設定すれば通信は継続さ れる 3. 自動設定でIPsecする (racoonのインストール) racoonを使って自動的に鍵の交換とSADの作成を行う portsでインストール cd /usr/ports/security/racoon/ make install /usr/local/etc/rc.d/racoon.sh があるか確認 インストールするracoonのバージョン(portsの日付)によって入る場合と 入らない場合がある。ない場合は以下の内容で作成 #!/bin/sh case "$1" in start) if [ -x /usr/local/sbin/racoon ]; then /usr/local/sbin/racoon -f /usr/local/etc/racoon/racoon.conf && echo -n ' racoon' fi ;; stop) /usr/bin/killall racoon && echo -n ' racoon' ;; *) echo "Usage: `basename $0` { start | stop }" exit 64 ;; esac 4. SSLの証明書と秘密鍵を作成 相手ホストの認証はSSLの証明書を使う RISTの認証局で証明書と秘密鍵を作成 できなければ、自分でCAを作る apacheで既に使用している場合は cp /usr/local/apache2/conf/ssl.crt/server.crt host_cert.pem cp /usr/local/apache2/conf/ssl.key/server.key host_priv.pem 秘密鍵のパスフレーズを外すのを忘れないように 作成したら以下に置く /usr/local/etc/racoon/cert/host_cert.pem (自ホストの証明書) /usr/local/etc/racoon/cert/host_priv.pem (自ホストの秘密鍵) /usr/local/etc/racoon/cert/cacert.pem (CAの証明書) ※ hostはホスト名に置き換える 複数のCAを信頼することができるようだ。例えばベリサインの公開鍵証明書をIE でexportして持ってくる。ここでは、 /usr/local/etc/racoon/cert/veri_cert.pem とした。これで、verisignでサインしてもらった公開鍵証明書をもつ相手とも 通信できるようになる。 設置したらCAの証明書を参照できるようにsymlinkを張る # ln -s cacert.pem `openssl x509 -noout -hash -in cacert.pem`.0 # ln -s veri_cert.pem `openssl x509 -noout -hash -in veri_cert.pem`.0 (ベリサインの公開鍵証明書を持ってきた場合) # common nameとIPアドレスの対応はみてないような気がする。 IPアドレスの対応をみても、DNSを引かないと分からないのでDNSも 認証をしないと意味ないかも。 自分の信頼するCAで署名された公開鍵証明書をもつ相手と通信できるように なるようだ。 5. /usr/local/etc/racoon/racoon.confを書く path include "/usr/local/etc/racoon"; path certificate "/usr/local/etc/racoon/cert"; path backupsa "/usr/local/etc/racoon/backupsa"; remote anonymous { exchange_mode main,aggressive; certificate_type x509 "host_cert.pem" "host_priv.pem"; my_identifier asn1dn; proposal { authentication_method rsasig; encryption_algorithm 3des; hash_algorithm sha1; dh_group 2 ; } } sainfo anonymous { lifetime time 3 min; encryption_algorithm 3des ; authentication_algorithm hmac_sha1; compression_algorithm deflate ; } ※ lifetime time 3 min; <- SAの有効期限を3分にする ※ backupsa 指定しても再起動した時にやっぱり変になるようだ # touch backupsa; chmod 600 backupsa する 6. racoonを起動 # /usr/local/etc/rc.d/racoon.sh start 7. /etc/ipsec.confを書く spdflush; spdadd ::/0 ::/0 any -P out ipsec esp/transport//use; spdadd ::/0 ::/0 any -P in ipsec esp/transport//use; flush; ※ SPDのみ記述 ※ 全てのホストに対し、SAを作成できればIPsecを使い、なければIPsec を使わない通信を行う 8. setkeyでSPを読み込む setkey -f /etc/ipsec.conf 起動時に自動的に読み込むには /etc/rc.confに ipsec_enable="YES" ipsec_file="/etc/ipsec.conf" と設定 9. 通信する 2台とも設定していれば自動的に鍵交換が始まり # tcpdump -i wi0 -n -l -p で 16:20:53.249947 2001:240:7:101:260:1dff:fef2:7648 > 2001:240:7:1::2: ESP(spi=0x07b5230d,seq=0x2d) 16:20:53.499938 2001:240:7:1::2 > 2001:240:7:101:260:1dff:fef2:7648: ESP(spi=0x009f0fca,seq=0x1e) [flowlabel 0x67766] 16:20:53.500814 2001:240:7:1::2 > 2001:240:7:101:260:1dff:fef2:7648: ESP(spi=0x009f0fca,seq=0x1f) [flowlabel 0x67766] 16:20:53.501318 2001:240:7:101:260:1dff:fef2:7648 > 2001:240:7:1::2: ESP(spi=0x07b5230d,seq=0x2e) のようにESPで暗号化されているのが分かる ESPされない時は、/var/log/all.log をみて考える ※ SPDにuseで登録しているので最初の数パケットは暗号化されずに送信される SAとSPを確認するには setkey -D setkey -DP 10. その他例 ・暗号アルゴリズムにdes-cbcを、認証アルゴリズムにhmac-md5を使った例 (鍵長 暗号:64bit 認証:128bit) add 2001:240:7:101:280:4cff:fe20:5450 2001:240:7:101:290:99ff:fe2a:1139 esp 0x111 -E des-cbc "kw,s.ql3" -A hmac-md5 "ks,dlw3k529a0cks"; add 2001:240:7:101:290:99ff:fe2a:1139 2001:240:7:101:280:4cff:fe20:5450 esp 0x112 -E des-cbc "kw,s.ql3" -A hmac-md5 "ks,dlw3k529a0cks"; # setkey -D 2001:240:7:101:290:99ff:fe2a:1139 2001:240:7:101:280:4cff:fe20:5450 esp mode=any spi=274(0x00000112) reqid=0(0x00000000) E: des-cbc 6b772c73 2e716c33 A: hmac-md5 6b732c64 6c77336b 35323961 30636b73 seq=0x00000006 replay=0 flags=0x00000040 state=mature created: Jan 25 16:24:59 2003 current: Jan 25 16:26:32 2003 diff: 93(s) hard: 0(s) soft: 0(s) last: Jan 25 16:25:11 2003 hard: 0(s) soft: 0(s) current: 576(bytes) hard: 0(bytes) soft: 0(bytes) allocated: 6 hard: 0 soft: 0 sadb_seq=1 pid=2143 refcnt=2 ・暗号アルゴリズムにsimpleを、認証アルゴリズムにkeyed-md5を使った例 (鍵長 暗号:任意 認証:128bit) add 2001:240:7:101:280:4cff:fe20:5450 2001:240:7:101:290:99ff:fe2a:1139 esp 0x111 -E simple "asa;32qiqj5q2p983sdkfjxcjv" -A keyed-md5 "ks,dlw3k529a0cks"; add 2001:240:7:101:290:99ff:fe2a:1139 2001:240:7:101:280:4cff:fe20:5450 esp 0x112 -E simple "asa;32qiqj5q2p983sdkfjxcjv" -A keyed-md5 "ks,dlw3k529a0cks"; # setkey -D 2001:240:7:101:290:99ff:fe2a:1139 2001:240:7:101:280:4cff:fe20:5450 esp mode=any spi=274(0x00000112) reqid=0(0x00000000) E: null 6173613b 33327169 716a3571 32703938 3373646b 666a7863 6a76 A: md5 6b732c64 6c77336b 35323961 30636b73 seq=0x00000007 replay=0 flags=0x00000040 state=mature created: Jan 25 16:30:06 2003 current: Jan 25 16:31:58 2003 diff: 112(s) hard: 0(s) soft: 0(s) last: Jan 25 16:30:26 2003 hard: 0(s) soft: 0(s) current: 584(bytes) hard: 0(bytes) soft: 0(bytes) allocated: 7 hard: 0 soft: 0 sadb_seq=1 pid=2154 refcnt=2 ・暗号アルゴリズムにblowfish-cbcを、認証アルゴリズムにkeyed-sha1を使った例 (鍵長 暗号:40 〜 448 認証:160bit) add 2001:240:7:101:280:4cff:fe20:5450 2001:240:7:101:290:99ff:fe2a:1139 esp 0x111 -E blowfish-cbc ".w,emslk3i" -A keyed-sha1 "ks,dlw3k529a0cks2k3j"; add 2001:240:7:101:290:99ff:fe2a:1139 2001:240:7:101:280:4cff:fe20:5450 esp 0x112 -E blowfish-cbc ".w,emslk3i" -A keyed-sha1 "ks,dlw3k529a0cks2k3j"; # setkey -D 2001:240:7:101:290:99ff:fe2a:1139 2001:240:7:101:280:4cff:fe20:5450 esp mode=any spi=274(0x00000112) reqid=0(0x00000000) E: blowfish-cbc 2e772c65 6d736c6b 3369 A: sha 6b732c64 6c77336b 35323961 30636b73 326b336a seq=0x00000004 replay=0 flags=0x00000040 state=mature created: Jan 25 16:35:09 2003 current: Jan 25 16:36:53 2003 diff: 104(s) hard: 0(s) soft: 0(s) last: Jan 25 16:35:22 2003 hard: 0(s) soft: 0(s) current: 392(bytes) hard: 0(bytes) soft: 0(bytes) allocated: 4 hard: 0 soft: 0 sadb_seq=1 pid=2165 refcnt=2 ・racoonで相手を指定してアルゴリズムなど変更する例 sainfo address 2001:240:7:101:290:99ff:fe2a:1139 any address 2001:240:7:101:280:4cff:fe20:5450 any { encryption_algorithm blowfish ; authentication_algorithm hmac_md5; compression_algorithm deflate ; } ・racoonでSAの期限を設定する例 sainfo address 2001:240:7:101:290:99ff:fe2a:1139 any address 2001:240:7:101:280:4cff:fe20:5450 any { lifetime time 30 sec; encryption_algorithm blowfish ; authentication_algorithm hmac_md5; compression_algorithm deflate ; } 11. スループット測定 shiov601 -> aercgw (TCPIPV6_STREAMのテスト) shiov601: Pentium III 1GHz mem 64MB aercgw: Pentium II 300MHz mem 128MB enc_algo auth_algo throughput --------------------------------------------- none: 92.27Mbps 3des-cbc hmac-sha1 deflate: 8.76Mbps des-cbc hmac-sha1 deflate: 12.85Mbps blowfish-cbc hmac-sha1 deflate: 13.89Mbps cast128-cbc hmac-sha1 deflate: 14.50Mbps rijndael-cbc hmac-sha1 deflate: 13.50Mbps null hmac-sha1 deflate: 23.09Mbps 12. 現在IPsec readyになっているホスト ホスト名 IPv6アドレス ----------------------------------------------------------------------- onion.v6.mimora.com 2001:240:7:101:260:1dff:fef2:7648 canbe.v6.mimora.com 2001:240:7:101:280:4cff:fe20:5450 na12.v6.mimora.com 2001:240:7:101:290:99ff:fe2a:1139 cucu.v6.mimora.com 2001:240:7:101::1 carrot.mimora.com 2001:240:7:100::1:1 ican.v6.mimora.com 2001:240:7:100::1:6 soseki.v6.mimora.com 2001:200:561:1::1 2001:200:5ff:3::2 aercgw.kmt-iri.go.jp 2001:240:7:1::2 shiov601.v6.mimora.com 3ffe:516:8141:1::1 3ffe:516:8024::3