OpenVPN

更新日2017-10-29 (日) 15:18:17

インストール

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

CentOSでは以下のことが必要かも

RPMforgeリポジトリを導入して、標準リポジトリで提供されないパッケージをyumでインストールできるようにする

# rpm -ivh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
# wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm

/etc/yum.repos.d/CentOS-Base.repoに追加「[centosplus]」以降はenabled=0なので関係ないと思うが、 priority=1の追加で、priorityを設定することで同じパッケージがあった場合に、優先順位が高いほうが優先される。priorityは数値で1〜99を設定するが、数値が大きいほど「優先順位は低い」

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/? release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
priority=1   <==追加

#released updates
[updates]
(略)
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
priority=1   <==追加

#released updates
[extras]
(略)
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
priority=1   <==追加

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
(略)
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
priority=1   <==追加

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
(略)
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
priority=1   <==追加

設定

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

# cp -a /usr/share/openvpn/easy-rsa .
# cd easy-rsa/2.0/

以下を修正

vars

# generation process.
##export KEY_SIZE=1024
export KEY_SIZE=2048

最後の行あたり

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-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  <=出来ているファイル

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

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

/etc/openvpn/easy-rsa/2.0で

以前に実行されて場合は

# source ./vars

を実行

# ./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 <=出来ているファイル

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

1024bit

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

2048bit

vars内の以下を変更 

# generation process.
export KEY_SIZE=1024

または

# openssl dhparam -out dh2048.pem 2048
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
..........................................................................................
# 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

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

/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

最後パラメータ 0:サーバ側 1:クライアント側

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

対称鍵 : AES(256bit)

cipher AES-256-CBC

を追加。

クライアント側のconfigファイルにも必要

デフォルトではOpenVPNがBlowfish128ビット対称暗号方式を用いる。以下はクライアントのログ

固定IPをクライアントに設定

/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のルールがあるようだ。それ以外では接続できなかった。

また以下ようにすると指定クライアントにrouteが追加される

push "route 192.168.202.0 255.255.255.0"
client-to-client       ←OpenVPNクライアント同士の通信を許可(サーバのみ設定)
keepalive 10 120       ←サーバ・クライアント間の死活監視の設定

証明書廃止リスト作成 (ユーザの証明書の廃止)

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

crl-verify crl.pem 

を追加しておく

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

# cd /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
# ./build-key dummy
(すべて Enter)

/etc/openvpn/easy-rsa/2.0/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のルーティング

ここまで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

ここから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)

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

起動時スケジューラで動かす

コマンドプロンプトが最小化でタスクバーに残り、これを削除すると接続が切れる

openvpn.bat

@echo off
c:\usr\OpenVPN\bin\openvpn.exe --config c:\Usr\OpenVPN\config\okada2.ovpn

openvpn.vbs

Set objWSHShell = CreateObject("WScript.Shell")
objWSHShell.Run "C:\Usr\OpenVPN\bin\openvpn.bat", 7

を作成し、スケジューラにopenvpn.vbsを登録

Linux Client

# apt-get install openvpn

でインストール後

/etc/openvpn

に、Windowsと同様にca.crt、linuxuser.crt 、linuxuser.key 、ta.keyをサーバ側からコピー

linuxuser.confをWindowsと同じ用に作成

client
proto tcp
dev tun
remote my-disk.stock-cube.net 1194
ca /etc/openvpn/ca.crt
cert /etc/openvpn/linuxuser.crt
key /etc/openvpn/linuxuser.key
tls-auth /etc/openvpn/ta.key 1
ns-cert-type server
resolv-retry infinite
nobind
comp-lzo LZO圧縮の利用
persist-key 再接続を行う設定
persist-tun 再接続を行う設定

起動

# /usr/sbin/openvpn /etc/openvpn/linuxuser.conf

Windowsでは問題ないようだが、Linuxでは時間がサーバと一致していないと以下のようなエラーで接続できない

VERIFY ERROR: depth=1, error=certificate is not yet valid:

パスワードを聞かれないためには

# openssl rsa -in linuxuser.key -out linuxuser.key
Enter pass phrase for linuxuser.key:******
writing RSA key

とすればOK。Windowsにも同じkeyファイルを渡せばOKなようだ

iptablesについて

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

port 1194

参考

VPN比較表


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