OpenVPNのインストール

提供: とある社畜の頭脳整理
ナビゲーションに移動 検索に移動

Raspberry piのArchlinux化をもくろみ、ArchlinuxでVPNサーバーを立ち上げようかと思っています。
そこで、まずはVirtualBoxでOpenVPNを使用して、VPNサーバーを立ち上げてみました。


VPNサーバーと認証局サーバーを分離している前提で、手順を上げています。
もし、一緒にしているなら上手いこと読み替えてください。

鍵の準備

VPNを使用するにあたり、認証局の設立やら共通鍵の作成やら鍵ペアの作成やら色々していきます。

認証局の設立(認証局サーバー)

認証局にするサーバーに以下のコマンドを実行して「Easy-RSA」をインストールします。

# pacman -S easy-rsa

インストールしたら「easy-rsa」に移動しておきます。

# cd /etc/easy-rsa

認証局の構築をしていきます。
以下のコマンドで認証局の初期化を行います。

# easyrsa init-pki
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/easy-rsa/pki


完了したら、認証局の鍵ペアを生成します。

# easyrsa build-ca
Using SSL: opensslOpenSSL 1.1.1d  10 Sep 2019
 
Enter New CA Key Passphrase: [任意のパスフレーズを入力]
Re-Enter New CA Key Passphrase: [任意のパスフレーズを入力]
Generating RSA private key, 2048 bit long modulus (2 primes)
...........................+++++
.....................................+++++
e is 65537 (0x010001)
Can't load /etc/easy-rsa/pki/.rnd into RNG
139748243150080:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:98:Filename=/etc/easy-rsa/pki/.rnd
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.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:[任意の名前を入力]
 
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/easy-rsa/pki/ca.crt

作成された認証局の証明書をVPNサーバーに移します。
証明書なのでまぁ適当に移せば良いかと思います。
下の例ではSCPを使用していますが…私はSFTPを使用しました。

# cp /etc/easy-rsa/pki/ca.crt /tmp
# chown foo /tmp/ca.crt
$ scp /tmp/ca.crt foo@hostname-of-openvpn-server:/tmp

VPN用の鍵の生成(VPNサーバー)

まずは、ファイルの移動も考えて、以下のコマンドでOpenVPNをインストールします。

# pacman -S openvpn

インストールが完了したら、認証局から受け取った証明書を適切な場所に移動させます。

# mv /tmp/ca.crt /etc/openvpn/server/
# chown root:root /etc/openvpn/server/ca.crt

VPNサーバー側にも「Easy-RSA」をインストールして、ディレクトリを移動します。

# pacman -S easy-rsa
# cd /etc/easy-rsa

認証局と同様に初期化します。

easyrsa init-pki
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/easy-rsa/pki


VPNサーバー用鍵ペアの生成

以下のコマンドで、VPNサーバー用の鍵ペアを生成します。

easyrsa gen-req server nopass
Using SSL: openssl OpenSSL 1.1.1d  10 Sep 2019

Can't load /etc/easy-rsa/pki/.rnd into RNG
139780419302656:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:98:Filename=/etc/easy-rsa/pki/.rnd
Generating a RSA private key
..................................................+++++
.................................................................+++++
writing new private key to '/etc/easy-rsa/pki/private/server.key.TUQccMn1qp'
-----
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.
-----
Common Name (eg: your user, host, or server name) [server]:[任意の名前を入力]

Keypair and certificate request completed. Your files are:
req: /etc/easy-rsa/pki/reqs/server.req
key: /etc/easy-rsa/pki/private/server.key

生成されたファイルのうち、秘密鍵?を適切なディレクトリにコピーします。

# cp /etc/easy-rsa/pki/private/server.key /etc/openvpn/server/

以下のコマンドで暗号通信用のパラメータファイルを生成します。

# openssl dhparam -out /etc/openvpn/server/dh.pem 2048

以下のコマンドでHMAC共通鍵を生成します。

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

VPNクライアント用鍵ペアの作成

今度は、VPNクライアントで使用する鍵ペアを生成します。
VPNクライアント用の鍵ペアを生成するマシンはなんでも良いのですが…
もし、別のマシンで初めて作成する場合は初期化を忘れない様にしてください。

# cd /etc/easy-rsa
# easyrsa init-pki
# cd /etc/easy-rsa

「/etc/easy-rsa」ディレクトリにいることを確認して、以下のコマンドを実行します。

# easyrsa gen-req client nopass
Using SSL: openssl OpenSSL 1.1.1d  10 Sep 2019

Can't load /etc/easy-rsa/pki/.rnd into RNG
139780419302656:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:98:Filename=/etc/easy-rsa/pki/.rnd
Generating a RSA private key
..................................................+++++
.................................................................+++++
writing new private key to '/etc/easy-rsa/pki/private/client.key.TUQccMn1qp'
-----
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.
-----
Common Name (eg: your user, host, or server name) [client]:[任意の名前を入力]

Keypair and certificate request completed. Your files are:
req: /etc/easy-rsa/pki/reqs/client.req
key: /etc/easy-rsa/pki/private/client.key

生成されたファイルのうち、秘密鍵?を適切なディレクトリにコピーします。

# cp /etc/easy-rsa/pki/private/client.key /etc/openvpn/client/

証明書署名要求ファイルを認証局サーバーに移動させます。 前出同様にscpを使用していますが、SFTPなどでもOKです。

# cp /etc/easy-rsa/pki/reqs/*.req /tmp
# chown foo /tmp/*.req
$ scp /tmp/*.req foo@hostname-of-CA:/tmp

公開鍵に署名する(認証局サーバー)

VPNサーバー&クライアントのreqファイルに認証局の証明書で署名していきます。
(…上の文言、間違ってるかも…)

まずは以下のコマンドでディレクトリを移動します。

# cd /etc/easy-rsa

VPNサーバー用の公開鍵に署名

以下のコマンドを実行して、reqファイルをインポートします。

# easyrsa import-req /tmp/server.req server
Using SSL: openssl OpenSSL 1.1.1d  10 Sep 2019

The request has been successfully imported with a short name of: server
You may now use this name to perform signing operations on this request.


次に署名済みの公開鍵を取り出します。

# easyrsa sign-req server server
Using SSL: openssl OpenSSL 1.1.1d  10 Sep 2019


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 1080 days:

subject=
    commonName                = [鍵ペアを生成したときに設定した名前が表示されます]


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/easy-rsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /etc/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'xxxxxxxx'
Certificate is to be certified until Dec  9 05:16:12 2022 GMT (1080 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/easy-rsa/pki/issued/server.crt

VPNクライアント用の公開鍵に署名

VPNサーバーと同様にクライアント用の公開鍵に署名していきます。

# easyrsa import-req /tmp/client.req client
Using SSL: openssl OpenSSL 1.1.1d  10 Sep 2019

The request has been successfully imported with a short name of: client
You may now use this name to perform signing operations on this request.


# easyrsa sign-req client client
Using SSL: openssl OpenSSL 1.1.1d  10 Sep 2019


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate for 1080 days:

subject=
    commonName                = [鍵ペアを生成したときに設定した名前が表示されます]


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/easy-rsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /etc/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'yyyyyyyy'
Certificate is to be certified until Dec  9 05:17:51 2022 GMT (1080 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/easy-rsa/pki/issued/client.crt

認証局から署名済みの公開鍵をVPNサーバーに返却します。
まぁ適当に移してください。
クライアント用も同様にVPNサーバーに移します。
後で、クライアント用の設定ファイルを作成するので…

# cp /etc/easy-rsa/pki/issued/*.crt /tmp
# chown foo /tmp/*.crt
$ scp /tmp/*.crt foo@hostname-of-openvpn_server:/tmp

これで、鍵関連の作業は終了です。
ここから、VPNの設定をしていきます。

VPNの設定

VPNサーバー

以下のコマンドで、署名済み公開鍵を適切なディレクトリに移動します。

# mv /tmp/server.crt /etc/openvpn/server/
# chown root:root /etc/openvpn/server/server.crt
# mv /tmp/client.crt /etc/openvpn/client/
# chown root:root /etc/openvpn/client/client.crt

これまでの手順で以下のファイルが作成されているかと思います。

/etc/openvpn/server/ca.crt
/etc/openvpn/server/dh.pem
/etc/openvpn/server/server.crt
/etc/openvpn/server/server.key
/etc/openvpn/client/client.crt
/etc/openvpn/client/client.key
/etc/openvpn/server/ta.key

VPNサーバー設定ファイルの作成

以下のコマンドでサンプルファイルをコピーします

# cp /usr/share/openvpn/examples/server.conf /etc/openvpn/server/server.conf

以下のコマンドで設定ファイルを開いて編集していきます。

# nano /etc/openvpn/server/server.conf
port 1194 ←ポート番号を変更したい場合
…
proto tcp ←プロトコルを変更したい場合(プロキシ認証越えの場合はtcp。ipv6の場合はudp6/tcp6)
;proto udp
…
;explicit-exit-notify 1 ←プロトコルをtcpにした場合はこの行をコメントアウト
…
ca ca.crt ←認証局の証明書(ファイル名を変えている場合は修正)
…
cert server.crt ←VPNサーバーの公開鍵(ファイル名を変えている場合は修正)
…
key server.key  # This file should be kept secret ←VPNサーバーの秘密鍵(ファイル名を変えている場合は修正)
…
dh dh.pem ←暗号通信用のパラメータファイル(ファイル名を変えている場合は修正)
…
tls-auth ta.key 0 ←HMAC共通鍵(ファイル名を変えている場合は修正)
…
user nobody ←プロセスのユーザー:コメントアウトを外す
group nobody ←プロセスのユーザーグループ:コメントアウトを外す
…
cipher AES-256-CBC ←良くわからないけど、書いたほうが良いらしい…(デフォルトで有効になってるけど…)
…
auth SHA512 ←追記
tls-version-min 1.2 ←追記
tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA:TLS-DHE-RSA-WITH-AES-128-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA ←追記

push "dhcp-option DNS 8.8.8.8" ←追記:VPNクライアントで使用してほしいDNSサーバーのアドレスを指定
push "dhcp-option DNS 8.8.4.4" ←追記:VPNクライアントで使用してほしいDNSサーバーのアドレスを指定
push "route 192.168.56.0 255.255.255.0" ←追記:VPNクライアントがアクセスしたいローカルネットワークのアドレスとサブネットマスクを指定
push "block-outside-dns" ←追記:DNSリーク対策
push "redirect-gateway def1" ←追記:すべての通信をVPNサーバー経由で行う場合は設定
client-to-client ←追記:VPNクライアントどうして通信したい場合はコメントアウトを外す



設定が完了したら、以下のコマンドでサービスを起動します。

# systemctl start openvpn-server@server

因みに…@の後ろの「server」は設定ファイル名です。

VPNクライアント設定ファイル

引き続き、VPNクライアント用の設定ファイルを作成していきます。
ただ…あ~だこ~だファイルを移すのは面倒なので、すべてを包含した設定ファイルを作成します。

「ovpngen」のインストール

AURに「ovpngen」という、便利なツールがあります。
これを使用してVPNクライアント用の設定ファイルを作成していきます。
まずは、以下のコマンドを使用してツールをゲットします。
(バージョンは適宜最新のものに読み替えてください)

# cd /etc/openvpn/client
# wget https://github.com/graysky2/ovpngen/archive/v1.28.tar.gz
# tar -xvf v1.28.tar.gz
# cd ovpngen-1.28
# chmod +x ovpngen

このツールはスクリプトなので、これでインストール完了です。

VPNクライアント用設定ファイルの生成

「ovpngen」を使用して、設定ファイルを作成します。
(ポート番号、プロトコルは適宜読み替えてください)

./ovpngen [VPNサーバーのIPアドレスまたはURL] /etc/openvpn/server/ca.crt /etc/openvpn/client/client.crt /etc/openvpn/client/client.key /etc/openvpn/server/ta.key 1194 tcp > client.ovpn

このファイルをVPNクライアントに持って行って、OpenVPNクライアントアプリに読み込ませればOKです。

ただ…ここで作成した「client.ovpn」ファイルの以下の部分は編集した方が良いかもです…。
(自信なし…)


# nano /etc/openvpn/client/client.ovpn
cipher AES-256-CBC ←コメントアウトを外す
auth SHA512 ←コメントアウトを外す
proto xxxx ←VPNサーバーの設定に合わせる


参考サイト

OpenVPN - ArchWiki
Easy-RSA - ArchWiki
GitHub - graysky2/ovpngen: Generate an OpenVPN Connect private tunnel profile in the unified format