首先,OpenVPN使用X.509证书进行客户端身份验证和VPN流量加密,尽管可以禁用此支持。
X.509证书的私钥基础设施(Private Key Infrastructure——PKI)的设置和维护是一个困难的概念,而且可能是一项繁琐的任务。
OpenSSL二进制文件具有手动管理PKI所需的所有工具,但命令选项很复杂,如果不自动化会很容易出错。建议组织或个人使用脚本或其他包来管理其PKI。这不仅有助于限制错误,而且可以更好地遵守规则和其他一般标准。
有两个开源项目是专门为与OpenVPN实现良好配合而编写的。Easy-RSA是一个长期项目,一直与OpenVPN项目密切相关。最初是与OpenVPN一起编写的,其最初目的是构建一个证书颁发机构(Certificate Authority——CA)及其必要的组件。今天,这个项目仍然与OpenVPN项目一起维护,尽管它们在技术上是分开的。
另一个项目,ssl-admin,是一个Perl脚本,用于填补Easy-RSA代码中的空白。这两个项目对PKI管理任务的处理方式不同,都有独特的解决方案。ssl-admin项目是一个提供菜单和用户反馈的交互式脚本,而Easy-RSA主要是一个批处理使用程序。
如今,Easy-RSA和ssl-admin项目都由Eric Crist维护。Josh Cepek加入了,并编写了大部分最初的v3.0 Easy-RSA代码。目标是最终合并这两个项目,并保留两者的所有功能。
PKI通常是机密证书和密钥对(key pairs)的分层组织。通常,与大多数网站一样,层次结构的顶部是CA。这是整个树的根,信任植根于此级别。如果根(root)是可信的,那么所有底层密钥对也将是可信的。从根级别CA,可以有客户端证书、服务器证书、子CA(sub_CAs)和证书吊销列表(Certificate Revocation Lists——CRLs)。在每个子CA下,重复此可能性列表。
【此处有图,略】
为了充分发挥PKI的潜力,用户和系统需要信任根CA以及链中的任何中间CA。对于大多数现代网络浏览器,浏览器作者或供应商默认情况下已经审查并批准了大量可信任的根级证书颁发机构(root-level certificate authorities)。这些机构通常是商业供应商,如VeriSign、Go Daddy、Comodo、Trend Micro、各种政府实体等。
由于这个预先批准的浏览器列表,绝大多数互联网用户完全不知道PKI是如何工作的。因此,OpenVPN的新用户以及任何需要PKI的软件都会在概念和技术上遇到配置和管理障碍。对于公共网站,需要由可信机构对第三方网站进行验证。然而,在OpenVPN的背景下,组织内通常有一个隐含信任的实体,即IT部门。OpenVPN通常在单个组织内使用,对这种信任是固有的。Easy-RSA和ssl-admin都是为了帮助新手和高级用户更好地管理他们的PKI而编写的。
对于单个点对点链路,使用PKI的复杂性来保护隧道通常没有意义;预共享密钥就足够了。然而,当涉及许多用户时,丢失和被盗密钥以及员工流失的可能性要大得多。使用正确配置的PKI,撤销丢失的证书或离职员工的证书相对简单。一个新证书的也很容易生成和重新部署。
使用Easy-RSA和ssl-admin,我们将创建一个简单的PKI,其中包含CA、服务器证书、一些客户端证书和证书吊销列表。此外,我们将使用这些使用程序生成DH(Diffie-Hellman)参数,这些参数将在后面的章节中使用和讨论。
OpenVPN PKI流程如下:
xxxxxxxxxx
Client Makes connection to Server (客户端与服务器建立连接)
Client requests server certificate. (客户端请求服务器证书)
↓
xxxxxxxxxx
Server sends ceritificate to client. (服务器向客户端发送证书。)
↓
xxxxxxxxxx
Client reads certificate: (客户端读取证书:)
Verifies certificate against CA (根据CA验证证书)
↓
xxxxxxxxxx
Client verifies server Key Usage (客户端验证服务器密钥使用情况)
↓
xxxxxxxxxx
Server reads certificate: (服务器读取证书:)
Verifies certifies against CA (验证CA认证)
↓
xxxxxxxxxx
Server verifies client Key Usage (服务器验证客户端密钥使用情况)
↓
xxxxxxxxxx
Server verifies against CRL (服务器根据CRL进行验证)
↓
xxxxxxxxxx
Client and Server exchange crypto
details, accept connections (客户端和服务器交换加密细节,接受连接)
在撰写本章时,Easy-RSA 2.2.2是最新版本,与V3.0.0-rc2一起发布。【我在看这本书的时候(2024/11/26),FreeBSD中安装的Easy-RSA版本是3.2.1】
由于v3.0系列即将发布,本节重点介绍该版本。
本练习将演示如何从头开始构建CA。此处不介绍从Easy-RSA2.2升级。下载Easy-RSA软件包后,解压缩文件:
xxxxxxxxxx
ecrist@computer:~/Downloads-> tar -xzvf EasyRSA-3.0.0-rc2.tgz
x EasyRSA-3.0.0-rc2/
x EasyRSA-3.0.0-rc2/x509-types/
x EasyRSA-3.0.0-rc2/x509-types/server
x EasyRSA-3.0.0-rc2/x509-types/ca
x EasyRSA-3.0.0-rc2/x509-types/COMMON
x EasyRSA-3.0.0-rc2/x509-types/client
x EasyRSA-3.0.0-rc2/openssl-1.0.cnf
x EasyRSA-3.0.0-rc2/ChangeLog
x EasyRSA-3.0.0-rc2/Licensing/
x EasyRSA-3.0.0-rc2/Licensing/gpl-2.0.txt
x EasyRSA-3.0.0-rc2/COPYING
x EasyRSA-3.0.0-rc2/KNOWN_ISSUES
x EasyRSA-3.0.0-rc2/doc/
x EasyRSA-3.0.0-rc2/doc/Hacking.md
x EasyRSA-3.0.0-rc2/doc/EasyRSA-Upgrade-Notes.md
x EasyRSA-3.0.0-rc2/doc/EasyRSA-Readme.md
x EasyRSA-3.0.0-rc2/doc/EasyRSA-Advanced.md
x EasyRSA-3.0.0-rc2/doc/Intro-To-PKI.md
x EasyRSA-3.0.0-rc2/README.quickstart.md
x EasyRSA-3.0.0-rc2/vars.example
x EasyRSA-3.0.0-rc2/easyrsa
提取后,将文件vars.example
复制到 vars
。建议设置Easy-RSA工作目录。vars的第45行默认将EASYRSA定义为$PWD(当前工作目录)。这可能会有问题,特别是如果你使用Easy-RSA来管理多个证书颁发机构。取消此行注释,并将其更改为适合你环境的内容,例如 /usr/local/etc/easy-rsa
。
【此处与FreeBSD上的最新版不同】
小贴士:初始化Easy-RSA后,EASYRSA目录中的所有内容都将被删除。小心你在这里自定义的东西。EASYRSA目录是证书存储所在的位置,而不是可执行文件和变量所在的位置。
你可能需要设置组织字段,这些字段是以下变量:
EASYRSA_REQ_COUNTRY
EASYRSA_REQ_PROVICE
EASYRSA_REQ_CITY
EASYRSA_REQ_ORG
EASYRSA_REQ_EMAIL
EASYRSA_REQ_OU
【对于FreeBSD14.1,这些变量有预设值(被注释掉了):】
xxxxxxxxxx
#set_var EASYRSA_REQ_COUNTRY "US"
#set_var EASYRSA_REQ_PROVINCE "California"
#set_var EASYRSA_REQ_CITY "San Francisco"
#set_var EASYRSA_REQ_ORG "Copyleft Certificate Co"
#set_var EASYRSA_REQ_EMAIL "me@example.net"
#set_var EASYRSA_REQ_OU "My Organizational Unit"
这些值用于所有生成的证书请求的默认值,包括根CA。确保这些行在文件中没有被注释掉。不需要对变量进行其他更改。
如果你定义的EASYRSA目录不存在,请立即创建它,并将openssl-1.0.cnf文件从分发包复制到新目录。对于我们的示例,我们将EASYRSA证书存储放在/usr/local/etc/easy-rsa中:
xxxxxxxxxx
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> mkdir -p /usr/local/etc/easy-rsa
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> cp openssl-1.0.cnf /usr/local/etc/easy-rsa
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> cp –R x509-types /usr/local/etc/easy-rsa/
接下来,我们准备初始化Easy-RSA PKI:
xxxxxxxxxx
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> ./easyrsa init-pki
注意,我们使用的是来自./vars的Easy-RSA配置。
xxxxxxxxxx
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /usr/local/etc/easy-rsa/pki
在这种情况下,初始化过程会清除pki
目录中的内容,并创建private
和reqs
子目录。
你可以有多个vars
文件来管理多个CA,并将所有这些文件嵌套在同一个EASYRSA
根目录中。为此,你必须更改每个CA的EASYRSA_PKI
变量。
build-ca
子命令首先生成证书签名请求(Certificate Signing Request——CSR),随后对该请求进行自签名。
要构建根证书颁发机构证书/密钥对,运行build-ca
命令:
xecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> ./easyrsa build-ca
Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
.......+++
............+++
writing new private key to '/usr/local/etc/easy-rsa/pki/private/ca.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
你将被要求输入将纳入你的证书申请的信息。你要输入的是所谓的可分辨名称(Distinguished Name——DN)。有很多字段,但你可以留一些空白;某些字段将定义默认值,如果输入.
,该字段将留空。
xxxxxxxxxx
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:Mastering OpenVPN
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/usr/local/etc/easy-rsa/pki/ca.crt
小贴士:共用名称字段中不应使用空格。这将导致Easy-RSA出现问题,也可能导致CCD、用户名等常见名称出现问题。确保路径后有一个尾随的正斜杠 /
,否则一些子命令(gen-crl
和其他子命令)将无法运行。
自签名时,CA约束设置为true,并自定义密钥使用参数,允许此新证书对其他证书进行签名,包括证书吊销列表(CRL)。可以使用openssl
命令行使用程序验证此信息:
xxxxxxxxxx
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> openssl x509 -in /usr/local/etc/easy-rsa/pki/ca.crt -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
89:39:42:bb:f3:6b:a9:f6
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=Mastering OpenVPN
Validity
Not Before: Oct 1 15:02:44 2014 GMT
Not After : Sep 28 15:02:44 2024 GMT
Subject: CN=Mastering OpenVPN
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (2048 bit)
Modulus (2048 bit):
...
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
69:6C:A3:85:63:61:09:DE:8F:7D:38:F7:A2:CB:1C:31:75:90:34:93
X509v3 Authority Key Identifier:
keyid:69:6C:A3:85:63:61:09:DE:8F:7D:38:F7:A2:CB:1C:31:75:90:34:93
DirName:/CN=Mastering OpenVPN
serial:89:39:42:BB:F3:6B:A9:F6
X509v3 Basic Constraints:
CA:TRUE
X509v3 Key Usage:
Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
...
在openssl输出中,我们可以看到x509v3 Basic Constraints
以及 x509v3 Key Usage
参数。你的CA现在已经准备好开始对客户端和服务器证书进行签名。
gen-crl
子命令生成一个CRL。虽然目前我们只有一个CA证书,但建议生成一个空的CRL。这允许你使用文件暂存OpenVPN配置,并且以后不需要重新启动。如果配置中列出了不存在的CRL,OpenVPN将记录错误,但该文件可以动态替换,因为它在每个客户端连接上都会被重新读取。
xxxxxxxxxx
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> ./easyrsa gen-crl
Note: using Easy-RSA configuration from: ./vars
Using configuration from /usr/local/etc/easy-rsa/openssl-1.0.cnf
Enter pass phrase for /usr/local/etc/easy-rsa/pki/private/ca.key:
An updated CRL has been created.
CRL file: /usr/local/etc/easy-rsa/pki/crl.pem
我们可以使用openssl crl
命令验证CRL:
xxxxxxxxxx
ecrist@computer:~/Downloads/EasyRSA-3.0.0-rc2-> openssl crl -noout -text -in /usr/local/etc/easy-rsa/pki/crl.pem
Certificate Revocation List (CRL):
Version 2 (0x1)
Signature Algorithm: sha256WithRSAEncryption
Issuer: /CN=Mastering OpenVPN
Last Update: Oct 1 15:50:46 2014 GMT
Next Update: Mar 30 15:50:46 2015 GMT
CRL extensions:
X509v3 Authority Key Identifier:
keyid:69:6C:A3:85:63:61:09:DE:8F:7D:38:F7:A2:CB:1C:31:75:90:34:93
DirName:/CN=Mastering OpenVPN
serial:89:39:42:BB:F3:6B:A9:F6
No Revoked Certificates.
Signature Algorithm: sha256WithRSAEncryption
...
OpenVPN可以使用x509密钥使用的参数,确保客户端使用有效的客户端证书连接,并且客户端可以确保服务器被授权为服务器。这可以防止你的一个客户端证书在Man-in-The-Midle(MITM——中间人)攻击中被用作服务器。如果没有此约束,则可以使用VPN CA签名的任何证书来模拟客户端或服务器。因为流氓证书(rogue certifixage,行为失常的、离群的、暴戾的)驻留在同一个PKI中,所以证书本身仍然有效,并将通过通用PKI检查。
Easy-RSA支持使用 build-server-full
子命令使用服务器密钥使用的参数对证书进行签名。
xxxxxxxxxx
ecrist@computer:~/EasyRSA-3.0.0-rc2-> ./easyrsa build-server-full movpn-server
Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
............+++
..............................+++
writing new private key to '/usr/local/etc/easy-rsa/pki/private/movpn-server.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
Using configuration from /usr/local/etc/easy-rsa/openssl-1.0.cnf
Enter pass phrase for /usr/local/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 :PRINTABLE:'movpn-server'
Certificate is to be certified until Oct 18 19:15:31 2024 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
我们可以使用 openssl
命令验证服务器证书:
xxxxxxxxxx
ecrist@computer:~/EasyRSA-3.0.0-rc2-> openssl x509 -noout -text -in /usr/local/etc/easy-rsa/pki/issued/movpn-server.crt
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 1 (0x1)
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=Mastering OpenVPN
Validity
Not Before: Oct 21 19:15:31 2014 GMT
Not After : Oct 18 19:15:31 2024 GMT
Subject: CN=movpn-server
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
...
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
X509v3 Subject Key Identifier:
11:AD:47:E2:1C:87:91:3B:C0:A1:53:F5:77:A7:2F:9F:0B:0F:5D:9E
X509v3 Authority Key Identifier:
keyid:F0:AF:20:ED:6F:A7:47:0F:C7:F2:B0:EC:CF:8B:30:09:02:E4:81:A0
DirName:/CN=Mastering OpenVPN
serial:84:9E:B9:14:2B:62:B4:50
X509v3 Extended Key Usage:
TLS Web Server Authentication
X509v3 Key Usage:
Digital Signature, Key Encipherment
Signature Algorithm: sha256WithRSAEncryption
...
注意,x509v3密钥使用部分,以及它们对 TLS Web Server Authentication
的标识。这就是当前版本的OpenVPN在指定远程证书类型时所寻找的。
就像服务器证书一样,可以使用特定于客户端的证书对客户端进行身份验证。通过这种方法,可以要求每个客户端都有一个唯一的证书。证书通用名(CN——Common Name)可用于确定通过 client-connect
脚本或 client=config-dir
选项在给定连接上推送的其他参数。从OpenVPN2.3.7开始,仍然支持 -client-cert-not-required
。有传言称从未来的版本中删除此支持。client-cert-not-required
允许客户端在没有唯一(或任何)预定义证书的情况下进行连接,就像网络浏览器连接到网络服务器一样。
easyrsa
命令用来建立客户端证书,方法与建立服务器端证书一样:
xxxxxxxxxx
ecrist@computer:~/EasyRSA-3.0.0-rc2-> ./easyrsa build-client-full client1
Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
...............+++
..........................................................................................................................+++
writing new private key to '/home/ecrist/EasyRSA-3.0.0-rc2/pki/private/client1.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
Using configuration from /home/ecrist/EasyRSA-3.0.0-rc2/openssl-1.0.cnf
Enter pass phrase for /home/ecrist/EasyRSA-3.0.0-rc2/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :PRINTABLE:'client1'
Certificate is to be certified until Oct 18 19:37:40 2024 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
我们可以使用 openssl
命令检查密钥使用参数:
xxxxxxxxxx
ecrist@computer:~/EasyRSA-3.0.0-rc2-> openssl x509 -noout -text -in /usr/local/etc/easy-rsa/pki/issued/client1.crt
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 2 (0x2)
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=Mastering OpenVPN
Validity
Not Before: Oct 21 19:37:40 2014 GMT
Not After : Oct 18 19:37:40 2024 GMT
Subject: CN=client1
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
...
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
X509v3 Subject Key Identifier:
47:80:59:8D:5F:63:4B:1C:21:C6:DE:C6:C0:7B:DE:6A:5D:53:F9:37
X509v3 Authority Key Identifier:
keyid:F0:AF:20:ED:6F:A7:47:0F:C7:F2:B0:EC:CF:8B:30:09:02:E4:81:A0
DirName:/CN=Mastering OpenVPN
serial:84:9E:B9:14:2B:62:B4:50
X509v3 Extended Key Usage:
TLS Web Client Authentication
X509v3 Key Usage:
Digital Signature
Signature Algorithm: sha256WithRSAEncryption
...
如服务器部分所述,我们检查 x509v3 Extend Key Usage
。在客户端的情况下,我们寻找 TLS Web Client Authentication
。 同样,这在验证远程客户端证书类型时使用。
ssl-admin项目是在Easy-RSA被认为被抛弃和崩溃的时候启动的。它是一个用Perl编写的菜单驱动的交互式实用程序。与Easy-RSA一样,ssl-admin是OpenSSL命令行实用程序的包装器。
要在FreeBSD上安装ssl-admin,可安装security/ssl-admin port。对于所有其他基于Unix的系统,包括OS X, svn export是最简单的方法。以下示例在Mac OS X上运行,但在其他操作系统上也会类似。
要在FreeBSD以外的操作系统上获取ssl-admin,请实用SVN命令行实用程序导出当前版本:
xxxxxxxxxx
ftp://ftp.secure-computing.net/pub/ssl-admin/
ecrist@computer:~-> curl -o ssa.tgz ftp://ftp.secure-computing.net//pub/ssl-admin/ssl-admin-1.2.1.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11196 100 11196 0 0 17568 0 --:--:-- --:--:-- --:--:-- 17548
ecrist@computer:~-> tar -xzvf ssa.tgz
x ssl-admin-1.2.1/
x ssl-admin-1.2.1/man5/
x ssl-admin-1.2.1/man1/
x ssl-admin-1.2.1/ssl-admin
x ssl-admin-1.2.1/ssl-admin.conf
x ssl-admin-1.2.1/Makefile
x ssl-admin-1.2.1/configure
x ssl-admin-1.2.1/openssl.conf
x ssl-admin-1.2.1/ssl-admin-e
x ssl-admin-1.2.1/man1/ssl-admin.1
x ssl-admin-1.2.1/man1/ssl-admin.1-e
x ssl-admin-1.2.1/man5/ssl-admin.conf.5
x ssl-admin-1.2.1/man5/ssl-admin.conf.5-e
如果主服务器ftp.secure-computing.net脱机,则可以使用服务器ftp2.secure-computing.net作为替代。
导出后需要安装,更改目录到导出树中,运行 ./configure
,然后跟随 make install
:
xxxxxxxxxx
ecrist@computer:~/ssl-admin-1.2.1-> ./configure
ecrist@computer:~/ssl-admin-1.2.1-> sudo make install
bash是配置的唯一需求。这不是一个典型的配置规则集,它只是模仿了一个规则集的行为。
安装完成后,运行 ssl-admin
命令最初将显示一个错误:
xxxxxxxxxx
ecrist@computer:~/ssl-admin-> ssl-admin
/Library/ssl-admin/ssl-admin.conf doesn't exist. Did you copy the sample from /Library/ssl-admin/ssl-admin.conf.sample? at /usr/local/bin/ssl-admin line 40.
某些版本的ssl-admin引用以下文件: ssl-admin.conf.default。
要开始实用该软件,你必须将默认文件复制到错误列出的位置。这将根据操作系统的标准文件系统层次结构而有所不同。在这里,我们将把ssl-admin.conf.sample复制到ssl-admin.conf,并修复新文件的权限。
ssl-admin要求所有操作系统都以root身份完成。这是一种已知的不良做法,将在即将发布的版本中得到修正。
操作的命令如下:
xxxxxxxxxx
ecrist@computer:~/ssl-admin-> sudo csh
Password:
使用csh没有具体的原因,只是作者的偏好。
xxxxxxxxxx
root@computer:~/ssl-admin-> cd /Library/ssl-admin/
root@computer:/Library/ssl-admin-> cp ssl-admin.conf.sample ssl-admin.conf
root@computer:/Library/ssl-admin-> chmod ug+rw ssl-admin.conf
root@computer:/Library/ssl-admin-> ls -l
total 12
-r--r--r-- 1 root wheel 2511 Oct 1 12:02 openssl.conf.sample
-rw-rw-r-- 1 root wheel 531 Oct 1 12:11 ssl-admin.conf
-r--r--r-- 1 root wheel 531 Oct 1 12:02 ssl-admin.conf.sample
对于本例,我们编辑配置文件。通常,只需要更改底部变量:
xxxxxxxxxx
$ENV{'KEY_COUNTRY'}
$ENV{'KEY_PROVINCE'}
$ENV{'KEY_CITY'}
$ENV{'KEY_ORG'}
$ENV{'KEY_EMAIL'}
$ENV{'KEY_COUNTRY'}
变量KEY_COUNTRY
必须是两个字母。这是标准的限制/约束,而不是 ssl-admin
独有。这些值与Easy-RSA中的同名变量对齐。一旦生成CA,就不应更改这些值,因为openssl会为不匹配的值抛出错误。
如果你的组织没有有效的CRL分发URL,则应保留 $ENV{'KEY_CRL_LOC'}
。空置将导致openssl错误。
编辑完配置文件后,我们就可以启动程序了。在第一次执行时,ssl-admin将检查其证书存储。如果那里不存在有效的CA和结构,则用户可以导入现有的PKI或创建新的PKI。与Easy-RSA一样,我们正在生成一个新的CA。在某些操作系统上,需要将/etc/openssl.cnf文件手动复制到证书存储根目录。在MAC OS X上没有遇到此错误。在Linux系统上,只需将/etc/openssl.cnf复制到/etc/ssl-admin/openssl.cnf即可。这将在ssl-admin的后续版本中得到修复。
xxxxxxxxxx
root@computer:/Library/ssl-admin-> ssl-admin
This program will walk you through requesting, signing,
organizing and revoking SSL certificates.
Looks like this is a new install, installing...
You will first need to edit the /Library/ssl-admin/ssl-admin.conf
default variables. Have you done this? (y/n): y
I need the CA credentials. Would you like to create a new CA key and
certificate now? (y/n): y
Please enter certificate owner's name or ID.
Usual format is first initial-last name (jdoe) or
hostname of server which will use this certificate.
All lower case, numbers OK.
Owner []: Mastering OpenVPN
File names will use Mastering_OpenVPN.
===> Creating private key with 2048 bits and generating request.
Do you want to password protect your CA private key? (y/n): y
Generating RSA private key, 2048 bit long modulus
.....................................+++
............+++
e is 65537 (0x10001)
Enter pass phrase for Mastering_OpenVPN.key:
Verifying - Enter pass phrase for Mastering_OpenVPN.key:
===> Self-Signing request.
Enter pass phrase for /Library/ssl-admin/Mastering_OpenVPN.key:
===> Moving certficate and key to appropriate directory.
===> Creating initial CRL.Using configuration from /Library/ssl-admin/openssl.conf
Enter pass phrase for /Library/ssl-admin/active/ca.key:
ssl-admin installed Wed Oct 1 12:28:23 CDT 2014
OPTIONAL: I can't find your OpenVPN client config. Please copy your config to
/Library/ssl-admin/packages/client.ovpn
=====================================================
# SSL-ADMIN v1.2.1 #
=====================================================
Please enter the menu option from the following list:
1) Update run-time options:
Key Duration (days): 3650
Current Serial #: 01
Key Size (bits): 2048
Intermediate CA Signing: NO
2) Create new Certificate Request
3) Sign a Certificate Request
4) Perform a one-step request/sign
5) Revoke a Certificate
6) Renew/Re-sign a past Certificate Request
7) View current Certificate Revokation List
8) View index information for certificate.
i) Generate a user config with in-line certifcates and keys.
z) Zip files for end user.
dh) Generate Diffie Hellman parameters.
CA) Create new Self-Signed CA certificate.
S) Create new Signed Server certificate.
q) Quit ssl-admin
正如你所看到的,ssl-admin比Easy-RSA更冗长、更具有交互性。此外,ssl-admin会自动为你生成初始CRL。
在显示菜单之前,有一个关于OpenVPN配置的可选警告。如果你提供了client.ovpn配置,ssl-admin可以自动将配置文件与嵌入式证书或多文件ZIP文件打包在一起。配置文件的证书行应该是通用的:
xxxxxxxxxx
ca ca.crt
cert client.crt
key client.key
这些值将自动替换为内联密钥,或根据OpenVPN证书、密钥和配置的分发方式重命名文件。现在我们通过创建根证书颁发机构密钥对(key pair)初始化了PKI,我们可以开始创建服务器和客户端证书了。
首先,我们将创建在OpenVPN服务器上使用的证书。菜单选项 S
将生成一个CSR,一个密钥,并提示CA对证书进行签名:
xxxxxxxxxx
Menu Item: S
Please enter certificate owner's name or ID.
Usual format is first initial-last name (jdoe) or
hostname of server which will use this certificate.
All lower case, numbers OK.
Owner []: Mastering OpenVPN Server
File names will use Mastering_OpenVPN_Server.
Please enter certificate owner's name or ID.
Usual format is first initial-last name (jdoe) or
hostname of server which will use this certificate.
All lower case, numbers OK.
Owner [Mastering_OpenVPN_Server]:
Would you like to password protect the private key (y/n): y
Generating a 2048 bit RSA private key
..................+++
.........+++
writing new private key to 'Mastering_OpenVPN_Server.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
===> Serial Number = 01
Using configuration from /Library/ssl-admin/openssl.conf
Enter pass phrase for /Library/ssl-admin/active/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'ZA'
stateOrProvinceName :PRINTABLE:'Enlightenment'
localityName :PRINTABLE:'Overall'
organizationName :PRINTABLE:'Mastering OpenVPN'
commonName :PRINTABLE:'Mastering OpenVPN Server'
emailAddress :IA5STRING:'root@example.org'
Certificate is to be certified until Sep 28 17:48:20 2024 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
=========> Moving certificates and keys to /Library/ssl-admin/active for production.
Can I move signing request (Mastering_OpenVPN_Server.csr) to the csr directory for archiving? (y/n): y
===> Mastering_OpenVPN_Server.csr moved.
MENU
为了节省空间,ssl-admin
打印的菜单将被省略,取而代之的是MENU
一词。
在前面的代码中,我们使用带有空格的证书CN来演示ssl-admin
行为。在这里,它警告说,空格将被下划线字符替换,并在必要时为用户提供更改CN的机会。进一步地,我们选择用密码保护私钥。最后,询问用户是否可以存档CSR。
为了显示添加的服务器令牌,我们再次运行openssl
命令以输出证书详细信息。为简介起见,以下输出省略了一些关键细节:
xxxxxxxxxx
root@computer:/Library/ssl-admin-> openssl x509 -noout -text -in active/Mastering_OpenVPN_Server.crt
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 1 (0x1)
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=ZA, ST=Enlightenment, L=Overall, O=Mastering OpenVPN, CN=Mastering OpenVPN/emailAddress=root@example.org
Validity
Not Before: Oct 1 17:48:20 2014 GMT
Not After : Sep 28 17:48:20 2024 GMT
Subject: C=ZA, ST=Enlightenment, O=Mastering OpenVPN, CN=Mastering OpenVPN Server/emailAddress=root@example.org
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (2048 bit)
Modulus (2048 bit):
...
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Cert Type:
SSL Server
Netscape Comment:
ssl-admin (OpenSSL) Generated Server Certificate
X509v3 Subject Key Identifier:
FB:A8:91:01:E3:51:5D:A7:29:8C:54:63:9F:22:7F:F8:DE:AB:5A:39
X509v3 Authority Key Identifier:
keyid:1F:85:DF:90:5C:3F:73:A9:03:B9:F4:E6:C2:2C:A3:27:CF:5B:44:95
DirName:/C=ZA/ST=Enlightenment/L=Overall/O=Mastering OpenVPN/CN=Mastering OpenVPN/emailAddress=root@example.org
serial:D2:93:32:F0:8E:BC:58:EE
X509v3 Extended Key Usage:
TLS Web Server Authentication
X509v3 Key Usage:
Digital Signature, Key Encipherment
Signature Algorithm: sha1WithRSAEncryption
注意, x509v3 Extened Key Usage
包括 TLS Web Server Authentication
。为了向后兼容,还包括了一个旧的标准,即 Netscape Cert Type
。这不仅与OpenVPN有关,而且ssl-admin是作为通用的 x509 CA管理实用程序编写的。
客户端证书的生成方式与服务器证书非常相似。菜单上的选项4将创建证书签名请求(Certificate Signing Request——CSR),随后对CSR进行签名:
xxxxxxxxxx
Menu Item: 4
Please enter certificate owner's name or ID.
Usual format is first initial-last name (jdoe) or
hostname of server which will use this certificate.
All lower case, numbers OK.
Owner []: client1
File names will use client1.
Please enter certificate owner's name or ID.
Usual format is first initial-last name (jdoe) or
hostname of server which will use this certificate.
All lower case, numbers OK.
Owner [client1]:
Would you like to password protect the private key (y/n): n
Generating a 2048 bit RSA private key
.....................................................................................................................................+++
.........+++
writing new private key to 'client1.key'
-----
===> Serial Number = 02
=========> Signing request for client1
Using configuration from /Library/ssl-admin/openssl.conf
Enter pass phrase for /Library/ssl-admin/active/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'ZA'
stateOrProvinceName :PRINTABLE:'Enlightenment'
localityName :PRINTABLE:'Overall'
organizationName :PRINTABLE:'Mastering OpenVPN'
commonName :PRINTABLE:'client1'
emailAddress :IA5STRING:'root@example.org'
Certificate is to be certified until Sep 28 18:05:14 2024 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
=========> Moving certificates and keys to /Library/ssl-admin/active for production.
Can I move signing request (client1.csr) to the csr directory for archiving? (y/n): ===> client1.csr moved.
MENU
后面的练习将使用最多三个客户端证书,因此建议你对client2 和client3重复上述步骤。
使用 openssl
二进制文件检查证书,我们可以看到 client1
证书缺少我们之前创建的服务器证书中存在的服务器密钥使用扩展。
xxxxxxxxxx
root@computer:/Library/ssl-admin-> openssl x509 -noout -text -in active/client1.crt
Certificate:
Data:
Version: 1 (0x0)
Serial Number: 2 (0x2)
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=ZA, ST=Enlightenment, L=Overall, O=Mastering OpenVPN, CN=Mastering OpenVPN/emailAddress=root@example.org
Validity
Not Before: Oct 1 18:05:14 2014 GMT
Not After : Sep 28 18:05:14 2024 GMT
Subject: C=ZA, ST=Enlightenment, O=Mastering OpenVPN, CN=client1/emailAddress=root@example.org
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (2048 bit)
Modulus (2048 bit):
...
Exponent: 65537 (0x10001)
Signature Algorithm: sha1WithRSAEncryption
...
此证书的结构显然比服务器证书更简单,并且缺少服务器密钥使用参数。
在创建CA、服务器和三个客户端证书后,我们只剩下以下目录结构:
xxxxxxxxxx
root@computer:/Library/ssl-admin-> ls -lrth
total 16
-rw-rw-r-- 1 root wheel 541B Oct 1 12:22 ssl-admin.conf
drwxr-x--- 2 root wheel 68B Oct 1 12:24 revoked
-rw-rw---- 1 root wheel 2.5K Oct 1 12:27 openssl.conf
drwxr-x--- 2 root wheel 102B Oct 1 12:28 packages
-r--r--r-- 1 root wheel 531B Oct 1 12:43 ssl-admin.conf.sample
-r--r--r-- 1 root wheel 2.5K Oct 1 12:43 openssl.conf.sample
drwxr-x--- 2 root wheel 340B Oct 1 13:05 prog
drwxr-x--- 2 root wheel 340B Oct 1 13:05 csr
drwxr-x--- 2 root wheel 544B Oct 1 13:05 active
active
目录包含所有尚未吊销的证书和密钥,包括CA证书和密钥。当证书被吊销时,它们会从acitve
目录移动到revoked
目录。为了使用OpenSSL使用程序吊销证书,证书必须存在。没有证书,需要手动对 index.txt
文件进行可能有问题的编辑。顾名思义,csr
目录包含所有CSRs。这些通常可以安全删除,并且仅用于故障排除或需要重新生成证书时保留。
建议管理员将证书存储的内容留给实用程序的管理层。这适用于ssl-admin和Easy-RSA。
prog
目录包含openssl的操作文件和最新的CRL。不建议干扰这些文件,因为如果出错,可能会导致PKI无法实用。
最后,packages
目录将包含你可以分发给最终用户的所有文件:不仅是OpenVPN客户端,还有web服务器管理员等。打包证书和密钥可确保最终用户收到所有必要的文件,并且这些文件的格式正确。
ssl-admin实用程序还有一些其他功能,管理PKI的人可能会对此感兴趣。索引是可搜索的(选项8),它显示了给定证书的状态。也可以显示当前CRL(选项7)。ssl-admin能够将OpenVPN配置文件与用户证书打包成内联格式(选项i),以及单独的文件——所有文件都包含在zip文件中(选项z)。当我们生成服务器和客户端配置时,本书将进一步讨论最后两个选项。
Easy-RSA3.0相当容易地支持多个根CA。通过在EASYRSA
根目录下创建一个单独的CA目录,并为每个目录创建不同的vars
文件,可以使用Easy-RSA管理每个单独的CA。
目前,ssl-admin不支持多个根CAs,但支持创建中间CA(intermediate CA)。
使用OpenVPN,单个服务器实例可以支持多个根CA,接受由任一CA签名的客户端连接。为了启用这种支持,每个授权CA的CA证书需要连接在一起,形成一个可以用 --ca
OpenVPN选项调用的文件。证书吊销列表也可以做到这一点。
通常,不建议对单个OpenVPN实例使用多个CA证书;例外情况可能是服务器或证书颁发机构迁移、公司或组织收购等。
在任何情况下,为OpenVPN证书链使用web浏览器根证书颁发机构都不是理想的。无法确定谁拥有证书,属于CA层次结构的任何人都可能连接到你的VPN实例。
未来,计划将ssl-admin和Easy-RSA项目合并到一个功能齐全的PKI管理套件中。我们希望Easy-RSA4.0能够使用这两个实用程序的最佳功能来实现这些迁移。
在本节中,我们将提供一些关于加密硬件设备的背景信息。你将学到如何在硬件令牌上生成私钥,以及如何将相关的X.509证书复制到令牌。之后,我们将讨论OpenVPN如何找到并使用此证书/私钥对来建立VPN连接。
从2.1版本开始,OpenVPN通过提供PKCS#11支持来支持双因素身份验证(two-factor authentication)。双因素身份验证基于这样的理念,即未来使用系统(如VPN),你需要提供两件事:
PKCS#11是与智能卡或硬件令牌通信的行业标准,有开源和商业驱动程序可供选择。PKCS#11标准最初由RSA实验室发布,有时也被称为 cryptoki 标准,代表 CRYPtographic TOKen Interface (密码令牌接口)。
除了硬件令牌和智能卡这两个术语外,硬件安全模块(Hardware Security Module——HSM)也经常用于双因素身份验证。在本节中,我们将主要使用术语硬件令牌。硬件令牌和智能卡之间的主要区别在于形状因素:硬件令牌通常以USB设备的形式出现,而智能卡看起来像ATM卡或信用卡。为了使用智能卡,需要一个特殊的读卡器,有时会集成到笔记本电脑甚至一些台式电脑中。一些国家发行国家电子身份证,通常被归类为智能卡。
HSM通常是一种可以安全存储和管理加密密钥的设备,通常还提供硬件加速以加快加密和解密。
硬件令牌、智能卡或HSM通常是一种带有嵌入芯片的小型设备。这种嵌入式芯片运行一个微型操作系统(通常称为Card OS),负责安全地生成、存储和管理SSL私钥。大多数硬件令牌还能够存储其他信息,如SSL证书,以便有效的证书/私钥对可以安全地存储在单个设备上。
使用双因素身份验证的主要困难是不同平台上的软件支持。虽然大多数硬件令牌和智能卡供应商为MS提供操作系统驱动程序,但Linux甚至Mac OS X上支持的卡和令牌要少得多。请注意,这与OpenVPN本书无关:如果你使用的操作系统支持特定的硬件令牌,并且提供了PKCS#11驱动程序,那么一般来说,OpenVPN可以使用该硬件令牌或智能卡。
本书使用了Aladdin eToken Pro 72K USB硬件令牌。此硬件令牌仅支持使用闭源SafeNet身份验证客户端,该客户端可用于MS Windows、Mac OS X和Linux。
这些令牌的旧版本的优点是,它们可以使用SafeNet的付费闭源驱动程序或OpenSC项目的免费开源驱动程序。不幸的是,这些旧的令牌已无法购买到,SafeNet的当前硬件令牌使用的是OpenSC不支持的不同卡操作系统。
一般过程和概念适用于你可能使用的大多数硬件令牌。这些旧设备仅用于测试和演示目的。许多供应商使用移动应用程序(通常在用户的智能手机上)作为硬件令牌。SafeNet也有这样的产品:MobilePASS。
此外,OpenSC项目的驱动程序和工具不如商业软件供应商的软件成熟。
其他智能卡和硬件代币的供应商是Aktiv Co和Feitian(支持开源)。
请注意,OpenVPN完全依赖于工作的PKCS#11驱动程序。在选择硬件令牌时,重要的是验证设备是否在所需的平台上受支持,而不是它是否可以与OpenVPN本身一起使用。此外,请注意,在OpenVPN服务器上不需要(甚至不建议!)使用硬件令牌。
假设已安装Aladdin的pkiclient
或 SafeNet的 AuthenticationClinet
,并且驱动程序能够识别硬件令牌。如果eToken已经初始化,请跳过此步骤。
首先,打开eToken客户端属性窗口,然后单击初始化eToken。这将打开一个对话框。
填写令牌密码和管理员密码,取消选中“Token Password must be changed on first logon(令牌密码必须在首次登录时更改)”复选标记,然后单击“Start”。
令牌上的所有内容现在都将被销毁,eToken将使用新的令牌和管理员密码进行初始化。
使用硬件令牌时,生成证书和私钥对的过程与使用ssl-admin或Easy RSA工具略有不同。使用ssl-admin或Easy工具,可以在一个步骤中生成私钥、证书请求和X.509证书。对于硬件令牌,我们首先需要在令牌上生成私钥。
使用这个私钥,我们需要创建CSR。然后,此CSR由CA签名,从而生成X.509证书。然后将此证书写回令牌。ssl-admin和Easy RSA工具实际上遵循相同的过程,但它们对用户隐藏了CSR文件。
为了在eToken上生成私钥,我们需要pkcs11-tool
命令,它是OpenSC包的一部分。OpenSC软件包可用于Microsoft Windows、Mac OS X和Linux。pkcs11-tool
命令使用PKCS#11驱动程序提供硬件令牌的接口。Aladdin/SafeNet驱动程序软件附带的PKCS#11驱动程序是libeTPkcs11.so
(Linux和Mac OS X)或eTPkcs11.dll
(Windows)。以下命令是在64位Linux机器上发出的,它生成了一个2048位的RSA密钥,该密钥使用标签movpn
和ID 20141001
标识。当我们生成私钥时,必须登录令牌:
xxxxxxxxxx
# pkcs11-tool --module libeTPkcs11.so \
--keypairgen --key-type rsa:2048 \
--label "movpn" --id 20141001 --login
Using slot 0 with a present token (0x0)
Logging in to "Mastering OpenVPN".
Please enter User PIN: [enter Token password]
Key pair generated:
Private Key Object; RSA
label: movpn
ID: 20141001
Usage: decrypt, sign, unwrap
Public Key Object; RSA 2048 bits
label: movpn
ID: 20141001
Usage: encrypt, verify, wrap
在硬件令牌上生成2048位密钥需要一些时间,在此期间,硬件令牌上的红灯熄灭。之后,红灯将再次亮起。 上面命令的输出告诉我们生成了一个RSA私钥,以及一个公共的2048位RSA公钥。这与SSL证书不同。为了生成SSL或X.509证书,我们首先需要生成一个证书请求。
我们需要使用OpenSSL引擎engine_pkcs11
,使用硬件令牌中的私钥生成证书请求。engine_pkcs11
引擎最好与自定义openssl.cnf
文件一起使用。我们首先创建这个文件:
xxxxxxxxxx
openssl_conf = openssl_def
[ openssl_def ]
engines = engine_section
[ engine_section ]
pkcs11 = pkcs11_section
[ pkcs11_section ]
engine_id = pkcs11
dynamic_path = /usr/lib64/openssl/engines/engine_pkcs11.so
MODULE_PATH = /usr/lib64/libeTPkcs11.so
init = 0
[ req ]
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
此文件是为64位CentOS Linux系统生成的。在其他系统中,驱动程序的路径和名称将不同。请注意,openssl.cnf
文件中的语句区分大小写!
我们现在使用以下openssl
命令生成主题为/CN=movpn
的证书请求:
xxxxxxxxxx
$ openssl req -engine pkcs11 -keyform engine -key 20141001 \
-new -text -out movpn.csr -config openssl.cnf \
-subj "/CN=movpn"
engine "pkcs11" set.
PKCS#11 token PIN: [enter Token password]
成功后,该命令不会产生进一步的输出。现在应该有一个movpn.csr
文件,该文件需要由本章前面设置的CA签名。假设签名的证书将命名为movpn.crt
。
OpenVPN期望X.509证书出现在硬件令牌上。因此,我们必须首先将上一步中的X.509证书写入eToken。
首先,将签名的证书转换为DER
格式:
xxxxxxxxxx
$ openssl x509 -in movpn.crt -outform der -out movpn.der
接下来,我们将DER
文件写入令牌:
xxxxxxxxxx
$ pkcs11-tool --module libeTPkcs11.so \
--write-object movpn.der --type cert \
--label movpn --id 20141001 --login
Using slot 0 with a present token (0x0)
Logging in to "Mastering OpenVPN".
Please enter User PIN: [enter Token password]
Created certificate:
Certificate Object, type = X.509 cert
label: movpn
ID: 20141001
我们需要验证私钥和证书的ID是否匹配:
xxxxxxxxxx
$ pkcs11-tool --module libeTPkcs11.so --login -O
Using slot 0 with a present token (0x0)
Logging in to "Mastering OpenVPN".
Please enter User PIN: [enter Token password]
Private Key Object; RSA
label: movpn
ID: 20141001
Usage: decrypt, sign, unwrap
Public Key Object; RSA 2048 bits
label: movpn
ID: 20141001
Usage: encrypt, verify, wrap
Certificate Object, type = X.509 cert
label: movpn
ID: 20141001
该令牌现在已准备好与OpenVPN一起使用。
为了使用硬件令牌中的证书和私钥,您必须首先找出OpenVPN所期望的硬件令牌ID。这是使用--show-pkcs11-id
选项完成的:
xxxxxxxxxx
$ openvpn --show-pkcs11-ids /usr/lib64/libeTPkcs11.so
The following objects are available for use.
Each object shown below may be used as parameter to
--pkcs11-id option please remember to use single quote mark.
Certificate
DN: CN=movpn
Serial: 01
Serialized id: SafeNet\x20Inc\x2E/eToken/00a3659e/Mastering\x20OpenVPN/20141001
序列化ID由以下部分组成:
SafeNet Inc
)eToken
)00a3659e
)Mastering OpenVPN
)20141001
)不使用证书或私钥的标签,但保持它们彼此同步是一种很好的做法。
我们现在终于可以在OpenVPN中使用硬件令牌了。为了使用它,我们替换了OpenVPN配置文件中的行:
xxxxxxxxxx
cert myclient.crt
key myclient.key
使用选项pkcs11-providers
和pkcs11-id
:
xxxxxxxxxx
pkcs11-providers /usr/lib64/libeTPkcs11.so
pkcs11-id 'SafeNet\x20Inc\x2E/eToken/00a3659e/Mastering\x20OpenVPN/20141001'
在本章中,我们讨论了创建根证书颁发机构的工具和方法,以及底层服务器和客户端证书。此外,还涵盖了PKCS#11的概念,尽管底层技术在不断发展。现在,您应该有一个完整的PKI和扩展它的工具。
下一章将介绍路由VPN设置。还将讨论tun网络设备的使用和工作连接的第3层要求。