*OpenVPN [#d4842728]

RIGHT:更新日&lastmod();

**インストール [#of1fa316]

OpenVPNをトンネルをトンネルモードでインストール。2つのネットワークのサブネットが異なる設定

 (10.0.0.0/8) === VPN(192.168.33.0/24) === (192.168.31.0/24)

OpenSSLの確認
 # rpm -q openssl
 openssl-1.0.0j-1vl6.x86_64

openvonのインストール
 # apt-get install openvpn

**設定 [#v86ae830]

 # cd /etc/openvpn
 # cp /usr/share/doc/openvpn-2.1.3/sample-config-files/server.conf .
 
 # cp -a /usr/share/openvpn/easy-rsa .

-プライベートCAの構築とCA証明書・秘密鍵の作成


 # cd easy-rsa/2.0/


以下の最後の行あたりを修正

''vars''

 export KEY_COUNTRY="JP"
 export KEY_PROVINCE="MIE"
 export KEY_CITY="MEIWA"
 export KEY_ORG="TEST"
 export KEY_EMAIL="okada@ism21.net"


''/etc/openvpn/easy-rsa/2.0''で

 # source ./vars
 NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/2.0/keys

以下のコマンドは証明書/鍵作成先ディレクトリ初期化~
※OpenVPNインストール後最初の1回のみ

 # ./clean-all


-認証局の作成
 # ./build-ca
 Generating a 1024 bit RSA private key
 .............................++++++
 .....................................++++++
 writing new private key to 'ca.key'
 -----
 You are about to be asked to enter information that will be incorporated
 into your certificate request.
 What you are about to enter is what is called a Distinguished Name or a DN.
 There are quite a few fields but you can leave some blank
 For some fields there will be a default value,
 If you enter '.', the field will be left blank.
 -----
 Country Name (2 letter code) [JP]: <= Enter
 State or Province Name (full name) [MIE]: <= Enter
 Locality Name (eg, city) [MEIWA]: <= Enter
 Organization Name (eg, company) [TEST]: <= Enter
 Organizational Unit Name (eg, section) []: <= Enter
 Common Name (eg, your name or your server's hostname) [TEST CA]: <= Enter
 Name []: <= Enter
 Email Address [okada@ism21.net]: <= Enter

-作成ファイル
 # ls -l keys
 合計 12
 -rw-r--r-- 1 root root 1155  9月 11 14:54 ca.crt  <=出来ているファイル
 -rw------- 1 root root  916  9月 11 14:54 ca.key  <=出来ているファイル
 -rw-r--r-- 1 root root    0  9月 11 14:53 index.txt
 -rw-r--r-- 1 root root    3  9月 11 14:53 serial


-サーバ証明書・サーバ秘密鍵の作成
サーバ秘密鍵を作成し、先ほど構築したプライベートCAで署名

 #  ./build-key-server server
 Generating a 1024 bit RSA private key
 .........................++++++
 .++++++
 writing new private key to 'server.key'
 -----
 You are about to be asked to enter information that will be incorporated
 into your certificate request.
 What you are about to enter is what is called a Distinguished Name or a DN.
 There are quite a few fields but you can leave some blank
 For some fields there will be a default value,
 If you enter '.', the field will be left blank.
 -----
 Country Name (2 letter code) [JP]: <= Enter
 State or Province Name (full name) [MIE]: <= Enter
 Locality Name (eg, city) [MEIWA]: <= Enter
 Organization Name (eg, company) [TEST]: <= Enter
 Organizational Unit Name (eg, section) []: <= Enter
 Common Name (eg, your name or your server's hostname) [server]:
 Name []: <= Enter
 Email Address [okada@ism21.net]: <= Enter
 
 Please enter the following 'extra' attributes
 to be sent with your certificate request
 A challenge password []: <= Enter
 An optional company name []: <= Enter
 Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
 Check that the request matches the signature
 Signature ok
 The Subject's Distinguished Name is as follows
 countryName           :PRINTABLE:'JP'
 stateOrProvinceName   :PRINTABLE:'MIE'
 localityName          :PRINTABLE:'MEIWA'
 organizationName      :PRINTABLE:'TEST'
 commonName            :PRINTABLE:'server'
 emailAddress          :IA5STRING:'okada@ism21.net'
 Certificate is to be certified until Sep  9 05:57:54 2022 GMT (3650 days)
 Sign the certificate? [y/n]:y <= y Enter 
 
 
 1 out of 1 certificate requests certified, commit? [y/n]y <= y Enter
 Write out database with 1 new entries
 Data Base Updated


-確認

以下のようにファイルが増えているserver.crtとserver.keyは確認すること

 # ls -l keys
 合計 40
 -rw-r--r-- 1 root root 3761  9月 11 14:58 01.pem
 -rw-r--r-- 1 root root 1155  9月 11 14:54 ca.crt
 -rw------- 1 root root  916  9月 11 14:54 ca.key
 -rw-r--r-- 1 root root   95  9月 11 14:58 index.txt
 -rw-r--r-- 1 root root   21  9月 11 14:58 index.txt.attr
 -rw-r--r-- 1 root root    0  9月 11 14:53 index.txt.old
 -rw-r--r-- 1 root root    3  9月 11 14:58 serial
 -rw-r--r-- 1 root root    3  9月 11 14:53 serial.old
 -rw-r--r-- 1 root root 3761  9月 11 14:58 server.crt  <=出来ているファイル
 -rw-r--r-- 1 root root  655  9月 11 14:57 server.csr
 -rw------- 1 root root  916  9月 11 14:57 server.key  <=出来ているファイル


***クライアント証明書・クライアント秘密鍵の作成(ユーザ用証明書作成) [#p27ca7f4]

ここではsample1として1クライアントを作成、クライアントの人数に応じて作成する

/etc/openvpn/easy-rsa/2.0で

以前に実行されて場合は

 # source ./vars

を実行

 # source ./build-key-pass sample1
 Generating a 1024 bit RSA private key
 ...++++++
 .............................................................++++++
 writing new private key to 'sample1.key'
 Enter PEM pass phrase:******   (1)
 Verifying - Enter PEM pass phrase:********
 -----
 You are about to be asked to enter information that will be incorporated
 into your certificate request.
 What you are about to enter is what is called a Distinguished Name or a DN.
 There are quite a few fields but you can leave some blank
 For some fields there will be a default value,
 If you enter '.', the field will be left blank.
 -----
 Country Name (2 letter code) [JP]:
 State or Province Name (full name) [MIE]:
 Locality Name (eg, city) [MEIWA]:
 Organization Name (eg, company) [TEST]:
 Organizational Unit Name (eg, section) []:
 Common Name (eg, your name or your server's hostname) [sample1]:
 Name []:
 Email Address [okada@ism21.net]:
 
 Please enter the following 'extra' attributes
 to be sent with your certificate request
 A challenge password []:
 An optional company name []:
 Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
 Check that the request matches the signature
 Signature ok
 The Subject's Distinguished Name is as follows
 countryName           :PRINTABLE:'JP'
 stateOrProvinceName   :PRINTABLE:'MIE'
 localityName          :PRINTABLE:'MEIWA'
 organizationName      :PRINTABLE:'TEST'
 commonName            :PRINTABLE:'sample1'
 emailAddress          :IA5STRING:'okada@ism21.net'
 Certificate is to be certified until Sep  9 06:03:01 2022 GMT (3650 days)
 Sign the certificate? [y/n]:y
 
 
 1 out of 1 certificate requests certified, commit? [y/n]y
 Write out database with 1 new entries
 Data Base Updated




-確認

 # ls -l keys
 合計 64
 -rw-r--r-- 1 root root 3761  9月 11 14:58 01.pem
 -rw-r--r-- 1 root root 3645  9月 11 15:03 02.pem
 -rw-r--r-- 1 root root 1155  9月 11 14:54 ca.crt
 -rw------- 1 root root  916  9月 11 14:54 ca.key
 -rw-r--r-- 1 root root  191  9月 11 15:03 index.txt
 -rw-r--r-- 1 root root   21  9月 11 15:03 index.txt.attr
 -rw-r--r-- 1 root root   21  9月 11 14:58 index.txt.attr.old
 -rw-r--r-- 1 root root   95  9月 11 14:58 index.txt.old
 -rw-r--r-- 1 root root 3645  9月 11 15:03 sample1.crt
 -rw-r--r-- 1 root root  655  9月 11 15:03 sample1.csr
 -rw------- 1 root root 1041  9月 11 15:03 sample1.key
 -rw-r--r-- 1 root root    3  9月 11 15:03 serial
 -rw-r--r-- 1 root root    3  9月 11 14:58 serial.old
 -rw-r--r-- 1 root root 3761  9月 11 14:58 server.crt <=出来ているファイル
 -rw-r--r-- 1 root root  655  9月 11 14:57 server.csr
 -rw------- 1 root root  916  9月 11 14:57 server.key <=出来ているファイル


-サーバで使用するDH(Diffie-Hellman)[鍵交換方式]パラメータを生成。

デフォルトでは1024bitなので2048で作成

''1024bit''
 # ./build-dh
 Generating DH parameters, 1024 bit long safe prime, generator 2
 This is going to take a long time
 .........................................................................++*++*++*

''2048bit''
 # openssl dhparam -out dh2048.pem 2048
 Generating DH parameters, 2048 bit long safe prime, generator 2
 This is going to take a long time
 ..........................................................................................




-CA証明書、サーバ証明書、サーバ秘密鍵、DHパラメータを/etc/openvpn/にコピー


 # cp keys/ca.crt /etc/openvpn/
 # cp keys/server.crt /etc/openvpn/
 # cp keys/server.key /etc/openvpn/
 # cp keys/dh1024.pem /etc/openvpn/



上記のファイル名を以下のファイルの中身を確認

''/etc/openvpn/server.conf''

&color(red){conquestでK-PACSとの接続はイメージ送信時にudpだと転送に失敗するようでだ};

 # TCP or UDP server?
 proto tcp
 ;;proto udp

 # On most systems, the VPN will not function
 # unless you partially or fully disable
 # the firewall for the TUN/TAP interface.
 ;dev tap <=L2レベルのトンネル(ブリッジなどような場合tap0を指定するようだ)
 dev tun  <=トンネルモードを指定(IPレベル)

 # Any X509 key management system can be used.
 # OpenVPN can also use a PKCS #12 formatted key file
 # (see "pkcs12" directive in man page).
 ca ca.crt <=確認
 cert server.crt  <=確認
 key server.key   <=確認 # This file should be kept secret

 # Diffie hellman parameters.
 # Generate your own with:
 #   openssl dhparam -out dh1024.pem 1024
 # Substitute 2048 for 1024 if you are using
 # 2048 bit keys.
 ;dh dh1024.pem  
 dh dh2048.pem 


以下のように指定するとVPNネットワークが192.168.33.0/24でサーバのVPN側のIPは
192.168.33.1になる。

 # on 10.8.0.1. Comment this line out if you are
 # ethernet bridging. See the man page for more info.
 ;server 10.8.0.0 255.255.255.0
 server 192.168.33.0 255.255.255.0


以下の設定でクライアント側にrouteを設定する。192.168.31.0/24はVPN側のインターフェイス側にあることを設定できる。

Active Routes:~
Network Destination     Netmask          Gateway       Interface  Metric~
192.168.31.0     255.255.255.0    192.168.33.22   192.168.33.21       1~
                 ^^^^^^^^^^^^^   ^^^^^^^^^^^^^~
                 VPN 側GW        VPNのIPアドレス~

のようなrouteが追加される

 # to know to route the OpenVPN client
 # address pool (10.8.0.0/255.255.255.0)
 # back to the OpenVPN server.
 ;push "route 192.168.10.0 255.255.255.0"
 ;push "route 192.168.20.0 255.255.255.0"
 push "route 192.168.31.0 255.255.255.0"
 push "route 192.168.33.0 255.255.255.0"



実行アクセス権を下げる
 # You can uncomment this out on
 # non-Windows systems.
 ;user nobody
 ;group nobody
 user nobody
 group nobody


-TLS化認証を有効化

/etc/openvpn/にkeyを作成

 # openvpn --genkey --secret /etc/openvpn/ta.key

''server.conf''
 # The second parameter should be '0'
 # on the server and '1' on the clients.
 ;tls-auth ta.key 0 # This file is secret
 tls-auth ta.key 0 # This file is secret

-logの作成

;log-append  openvpn.log
log-append  /var/log/openvpn.log


***固定IPをクライアントに設定 [#tfab2d89]

''/etc/openvpn/server.conf''

 # EXAMPLE: Suppose the client
 # having the certificate common name "Thelonious"
 # also has a small subnet behind his connecting
 # machine, such as 192.168.40.128/255.255.255.248.
 # First, uncomment out these lines:
 ;client-config-dir ccd
 client-config-dir ccd  <=コメント削除
 ;route 192.168.40.128 255.255.255.248


ccdディレクトリ作成

 # mkdir /etc/openvpn/ccd

sample1として1クライアントを作成した場合は

ccdの下にsample1というファイルを作成し

 ifconfig-push 192.168.33.21 192.168.33.22

の一行追加すると

192.168.33.21が割り振られる


ifconfig-push 192.168.33.x 192.168.33.y

x=4n+1, y=4n+2のルールがあるようだ。それ以外では接続できなかった。


***証明書廃止リスト作成 (ユーザの証明書の廃止)[#e49b47f3]

廃止リストファイルcrl.pem を作成しておきserver.conf の最後に

 crl-verify crl.pem 

を追加しておく

crl.pem作成のためdummyを作成し、廃棄する


 # cd /etc/openvpn/easy-rsa
 # source ./vars
 NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/2.0/keys
 # ./build-key dummy
 (すべて Enter)

''openssl.cnf'' 以下をコメントアウト

 ##[ pkcs11_section ]
 ##engine_id = pkcs11
 ##dynamic_path = /usr/lib/engines/engine_pkcs11.so
 ##MODULE_PATH = $ENV::PKCS11_MODULE_PATH
 ##PIN = $ENV::PKCS11_PIN
 ##init = 0

-廃棄

 # ./revoke-full dummy
 Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
 unable to load certificate
 139693420025512:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:696:Expecting: TRUSTED CERTIFICATE
 Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
 unable to load certificate
 140523615540904:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:696:Expecting: TRUSTED CERTIFICATE


 # cp /etc/openvpn/easy-rsa/2.0/keys/crl.pem /etc/openvpn/

新しい廃棄を行うごとにcrl.pem が出来るので/etc/openvpn/にコピーする
削除したユーザを復旧するときは以前のcrl.pem を書き戻せばOK

openvpnの再起動は不要だが、すでに接続済ユーザは再接続するまで切れないようだ。
そのためにも再起動したほうが良いようだ。

ただ、再起動すると未廃棄ユーザも一度切れるので再接続が費用になるようだ。
運用を考える



***IPのルーティング [#n4ba2507]

ここまでVPNがルーティングVPN (dev tun)として接続できる。IPで接続をコントロールする


 (自分のLAN 10.0.0.0/8)
 ----------------------
 | Client             |
 |             VPN IP | 192.168.33.21     VPN Client GW 192.168.33.22
 |                    |-----------------------------|
 |                    |                             |
 |                    |                             |
 ----------------------                             |
                                                    |
                                                    |
                                                    |
 ----------------------                   VPN Server|GW 192.168.33.2
 |VPN Server          |                             |
 |             VPN IP | 192.168.33.1                |
 |                    |------------------------------
 |                 IP | | 182.236.42.XX(eth0)
 |                    | | 192.168.31.8(eth0:1)
 |                    | |
 ---------------------- |
                        |
                        |eth0 192.168.31.14
           ----------------------------
           |   Data Server            | Defalut GW 192.168.31.1
           |                          |そのため 192.168.33.0/24は192.168.31.8に
           ----------------------------route addされている

上のような構成のときClientのIPアドレスでDataServerに接続する場合は
Data ServerにはいかのようにRoute addの必要がある

 C:\Windows\system32>route add 192.168.33.0 mask 255.255.255.0 192.168.31.8 metric 1

クライアント側にはVPNサーバのserver.confに
 push "route 192.168.31.0 255.255.255.0"
があるので接続Route addされる


あとはVPNServerがパケットを転送する必要があるので

 # echo 1 > /proc/sys/net/ipv4/ip_forward

***Windows client [#abf25e6b]

[[ここ>http://openvpn.jp/openvpn-gui-jp.html]]からDownload.

OpenVPN\configにサーバで作成した/etc/openvpn/easy-rsa/2.0/keys/のca.crt、sample1.crt、sample1.key、ta.keyをコピー。~
OpenVPN\sample-config\client.ovpnもここにコピー

''client.ovpn''

host名とポートを指定
 remote 182.236.42.23 1194

 ca ca.crt
 cert sample1.crt
 key sample1.key

 # then every client must also have the key.
 ;tls-auth ta.key 1
 tls-auth ta.key 1


を修正


後は起動してタスクトレイから接続

接続時のパスワードはクライアントの証明書作成(# source ./build-key-pass sample1)に入力したもの(1)


&color(red){vista,7については管理者権限で起動しないとサーバ側のserver.confで指定したpush "route 192.168.31.0 255.255.255.0"のroute追加が権限上の問題で追加されない。また、vistaではクライアント側の設定ファイルclient.ovpn等に以下の2行をファイル末尾に追加の必要があるかもしれない};

 route-method exe
 route-delay 2

***iptablesについて [#ka35bb7b]

VPNサーバ上でudpまたはtcpでserver.confで指定したportを通過させるデフォルトは

 port 1194

**参考 [#m32fa21a]

-http://linux-story.seesaa.net/article/193524147.html

-http://kilin.clas.kitasato-u.ac.jp/howto/openvpn.html

-http://centossrv.com/openvpn.shtml

-http://jehupc.exblog.jp/14096411/

-http://freescitech.net/2/ovpn2_howto_ja.html

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS