本集重點在設定Certificate Authority (CA) 。要進入設定OpenVPN的第一步是,建立PKI(公鑰架構),部份相關的基礎知識,可以參考這篇 如何製作 SSL X.509憑證。
OpenVPN: Building and Integrating Virtual Private Networks
前情提要:OpenVPN 建置筆記(第3集)
PKI ,包含那些東西呢?
1. 為伺服器及使用者端建立公鑰及私鑰。
2. 建立Root CA ,以便幫伺服器及使用者端,簽發憑證。
OpenVPN ,可以使用憑證,進行雙向的認證作業;也就是在建立VPN 連線的同時,除了Server 要認證Client 端的憑證,Client 也要認證Server 端的憑證。
當Server 及Client 端在互相認證時,會利用Root CA 的公鑰,進行對方憑證的驗證,然後會再進行一些其它資訊的測試,例如,驗證對方憑證上的common name ,或憑證的類別。
這樣的安全機制,有什麼好處呢?
1. Server 端只需要知道它自己的憑證及私鑰,並不需要去知道,個別可能會連線進來的Client 的憑證及私鑰。
2. Server 只接受由同一個Root CA 所簽發出來的Client 端憑證。Server 不需要Root CA 的私鑰,即可進行驗證。所以 Root CA 私鑰,可以在安全的保存在其它的機器上。
3. 如果Client 端的私鑰,有問題的話,可以把它加入失效列表,這樣即可在連線時,拒絕VPN 連線。
4. Server 可以利用憑證中的資訊,例如:common name 限制特定User 的存取權限。
好處講完了,接著來建立Root CA 及相關的憑證吧!
這次測試我們將建立Root CA , Server 憑證,及3個 User 端憑證。
在PKI 的管理上,我們使用OpenVPN 內建的Script 來進行相關作業。
在Linux 下,這些Script 位於easy-rsa 目錄下,在安裝時,會按安裝方式的不同,而建立在不同的目錄下。如果是用RPM 的方式安裝,目錄在,/usr/share/doc/packages/openvpn 或 /usr/share/doc/openvpn-2.0 ,如果是使用tar.gz 安裝的話,這個easy-rsa 目錄,會在解開這個tar.gz檔的第1層目錄下。最好是把這個目錄,另外複製出來,放在其它目錄下,例如:/etc/openvpn 下,未來如果OpenVPN 升級時,就不會把我們手動的設定檔蓋掉。
小瑞的測試環境 easy-rsa 目錄放在 /usr/share/doc/openvpn-2.1_rc7 先把它copy 到/etc/openvpn 下
[root@openvpn ~]#cp -rf /usr/share/doc/openvpn-2.1_rc7/easy-rsa /etc/openvpn/easy-rsa
切換到 /etc/openvpn/easy-rsa/2.0目錄,修改一下 vars 檔案,針對KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, 及 KEY_EMAIL 這幾個參數輸入,我們的自己的設定值,請不要留空哦!
KEY_SIZE 原本是1024,為了增加安全性,改為 2048 ,當然有些代價;在做TLS 溝通時,及產生DH 參數時,效能會受影響。
其它參數:
export KEY_COUNTRY=”TW”
export KEY_PROVINCE=”CA”
export KEY_CITY=”Taipei”
export KEY_ORG=”nuface”
export KEY_EMAIL=”rico@nuface.tw”
執行 vars 這個檔案,設定環境變數,接著使用clean-all ,重新洗牌;build-ca 建立Root CA。
ps: clean-all 只有在第1次建立PKI 架構時,使用!
[root@openvpn ~]#cd /etc/openvpn/easy-rsa/2.0
[root@openvpn ~]#. ./vars (注意,有2個點哦)
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/2.0/keys[root@openvpn ~]#./clean-all
Please source the vars script first (i.e. “source ./vars”)
Make sure you have edited it to reflect your configuration.[root@openvpn ~]#./build-ca
Generating a 2048 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) [TW]:
State or Province Name (full name) [CA]:
Locality Name (eg, city) [Taipei]:
Organization Name (eg, company) [nuface]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) [nuface CA]:
Email Address [rico@nuface.tw]:
這樣我們就建好了Root CA 的Key 了!
接著使用build-key-server 這支script 為 OpenVPN Server 建立憑證:
[root@openvpn ~]#./build-key-server server
Generating a 2048 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) [TW]:
State or Province Name (full name) [CA]:
Locality Name (eg, city) [Taipei]:
Organization Name (eg, company) [nuface]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) [server]:
Email Address [rico@nuface.tw]:
以上的作業,基本上使用預設值即可,至於那個common name ,小瑞使用server 這個名字,基本上可以按各自實際的狀況,輸入自己的實際host name 即可,例如:openvpn.nuface.tw ,另外有2個問句,回答y 即可。
接著來按照劇本,建立3個User 的憑證。
直接使用build-key 這個scripts 來進行建立。
[root@openvpn ~]#./build-key client1
[root@openvpn ~]#./build-key client2
[root@openvpn ~]#./build-key client3
基本上跟上面的server 建置的方式類似,只有common name 不同,做個別修改即可!那個common name ,必須保持唯一值,也就是client1 使用過了之後,對第2個User 就不行用client1 來當成他的common name ,必須另外取一個 exp: client2 做為common name。
接著來做一個D-H 的密鑰交換參數。 ( Diffie Hellman parameters)
這個要做什麼用呢?嗯..有機會再細說,主要用途在加密通道未建立前,在不安全的通道下建立一個密鑰,可以用這個密鑰,進行第1次憑證交換時,加密使用。這個密鑰只使用一次,用完了就丟,下次要用時,會再重新產生。
使用build-dh 這個Script 來建立DH paras。
[root@openvpn ~]#./build-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
...............................................++*++*
好啦,目前我們要的Key 都有了,存在/etc/openvpn/easy-rsa/2.0/keys,做一個列表如下:
Filename | Needed By | Purpose | Secret |
ca.crt | server + all clients | Root CA certificate | NO |
ca.key | key signing machine only | Root CA key | YES |
dh2048.pem | server only | Diffie Hellman parameters | NO |
server.crt | server only | Server Certificate | NO |
server.key | server only | Server Key | YES |
client1.crt | client1 only | Client1 Certificate | NO |
client1.key | client1 only | Client1 Key | YES |
client2.crt | client2 only | Client2 Certificate | NO |
client2.key | client2 only | Client2 Key | YES |
client3.crt | client3 only | Client3 Certificate | NO |
client3.key | client3 only | Client3 Key | YES |
Secret 為Yes 就是要保密的私鑰,NO 的就是可以公開,到處散播的公鑰。當然Client要用的,就只給那個User ,可以透過SSL 加密的方式,使用網際網路來傳送給User ,如何使用SSL 加密的通訊服務,請參考這篇 各項網路服務 + SSL 應用。
如果不想使用安全的通道,傳送這些公鑰及私鑰,還有什麼方式呢?
有的。在我們的測試中,都是在同一台機器上,產生client端的公鑰及私鑰。可以改為由User 在自己機器上,產生自己的私鑰,再提交一個憑證簽名的請求(CSR),送至專門做簽發憑證的機器上,再由簽發憑證的機器,執行簽發作業,再把簽好的憑證,回覆給使用者。這樣的作法,user的私鑰,就不會離開user自己的機器,也就不需要使用加密通道,來傳送私鑰!
搞定了CA 憑證 之後,接著就可以進行OpenVPN Server 及Client 端的設定了,不好意思,又要待續囉!
[…] OpenVPN 建置筆記(第4集) 日期:2010/03/29 | 留言:0 個 | 作者:Rico | 瀏覽:5人次 分類:MIS易筋經, 網路篇 標籤:broadcasts, IANA, IPX, MTU, Open VPN, route, Routing, tap, tun, WINS 留言 (0) 引用 (0) […]
[…] Logo Ref Open VPN Project 前情提要:OpenVPN 建置筆記(第4集) […]