16.5.OpenSSL

OpenSSL 是 SSL 与 TLS 协议的开源实现。它在普通通信层之上提供了一个加密传输层,可与许多网络应用程序和服务交织在一起。

FreeBSD 中包含的 OpenSSL 版本支持传输层安全性协议 1.0/1.1/1.2/1.3(TLSv1/TLSv1.1/TLSv1.2/TLSv1.3)网络安全协议,可以用作通用加密库。

OpenSSL 通常用于加密邮件客户端的身份验证,并保护基于 Web 的交易,如信用卡支付。一些 port,如 www/apache24databases/postgresql11-server,包含一个用于使用 OpenSSL 进行编译的编译选项。如果选定该 port,该 port 将从基本系统添加使用对 OpenSSL 的支持。要让 port 从 security/openssl 获取 OpenSSL 并进行编译,请将以下内容添加到 /etc/make.conf

DEFAULT_VERSIONS+= ssl=openssl

OpenSSL 的另一个常见用途是提供用于软件应用的证书。证书可以用来验证一个公司或个人的资质。如果一个证书没有经过外部 证书授权机构(CA)(如 http://www.verisign.com)的签名,使用该证书的应用程序将产生一个警告。获得签名证书是有成本的,但是使用签名证书并不是强制性的,因为证书可以是自签的。然而,使用外部权威机构可防止警告,可以让用户放心。

本节将演示如何在 FreeBSD 系统上创建和使用证书。有关如何创建用于签署自己的证书的 CA 的示例,请参阅“配置 LDAP 服务器”

有关 SSL 的更多信息,请阅读免费的 OpenSSL 说明书

16.5.1.生成证书

若要生成将由外部 CA 签名的证书,请执行以下命令并输入在提示符下请求的信息。此输入信息将写入证书。在 Common Name 提示符下,输入将使用该证书的系统的完全限定域名。如果此域名与服务器不匹配,则验证证书的应用程序将向用户发出警告,使证书提供的验证变得无用。

# openssl req -new -nodes -out req.pem -keyout cert.key -sha256 -newkey rsa:2048
Generating a 2048 bit RSA private key
..................+++
.............................................................+++
writing new private key to 'cert.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) [AU]:US
State or Province Name (full name) [Some-State]:PA
Locality Name (eg, city) []:Pittsburgh
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Company
Organizational Unit Name (eg, section) []:Systems Administrator
Common Name (eg, YOUR name) []:localhost.example.org
Email Address []:trhodes@FreeBSD.org

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:Another Name

创建证书时,可以使用其他选项,例如过期时间和备用加密算法。完整的选项列表在 openssl(1) 中进行了说明。

此命令将在当前目录中创建两个文件。证书请求 req.pem 可以发送到 CA,CA 将验证输入的凭据,对请求进行签名,并返回签名的证书。第二个文件 cert.key 是证书的私钥,应存储在安全的位置。如果落入他人手中,则可以使用它来模拟用户或服务器。

或者,如果不需要来自 CA 的签名,则可以创建自签名证书。首先,生成 RSA 密钥:

# openssl genrsa -rand -genkey -out cert.key 2048
0 semi-random bytes loaded
Generating RSA private key, 2048 bit long modulus
.............................................+++
.................................................................................................................+++
e is 65537 (0x10001)

使用此密钥创建自签名证书。按照创建证书的常规提示进行操作:

# openssl req -new -x509 -days 365 -key cert.key -out cert.crt -sha256
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) [AU]:US
State or Province Name (full name) [Some-State]:PA
Locality Name (eg, city) []:Pittsburgh
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Company
Organizational Unit Name (eg, section) []:Systems Administrator
Common Name (e.g. server FQDN or YOUR name) []:localhost.example.org
Email Address []:trhodes@FreeBSD.org

这将在当前目录中创建两个新文件:私钥文件 cert.key 证书和证书本身 cert.crt。这些应该放在同一个目录中,最好是在 /etc/ssl/ 下,只有 root 才可读。可以使用 chmod 设置权限为 0700,这比较适合于这些文件。

16.5.2.使用证书

证书的一种用途是加密与 sendmail 邮件服务器的连接,以防止使用明文身份验证。

注意

如果用户尚未安装证书的本地副本,某些邮件客户端将显示错误。有关证书安装的详细信息,请参阅软件附带的文档。

在 FreeBSD 10.0-RELEASE 及更高版本中。可以自动为 sendmail 创建一个自签名证书。若要启用此功能,请将以下行添加到 /etc/rc.conf

sendmail_enable="YES"
sendmail_cert_create="YES"
sendmail_cert_cn="localhost.example.org"

这将自动创建自签名证书,/etc/mail/certs/host.cert、签名密钥、/etc/mail/certs/host.key 和一个 CA 证书 /etc/mail/certs/cacert.pem。该证书将使用 sendmail_cert_cn 中指定的 Common Name。保存编辑内容后,重新启动 sendmailL:

# service sendmail restart

如果一切顺利,/var/log/maillog 中将不会出现错误消息。一个简单的测试:请使用 telnet 命令连接到邮件服务器的侦听端口:

# telnet example.com 25
Trying 192.0.34.166...
Connected to example.com.
Escape character is '^]'.
220 example.com ESMTP Sendmail 8.14.7/8.14.7; Fri, 18 Apr 2014 11:50:32 -0400 (EDT)
ehlo example.com
250-example.com Hello example.com [192.0.34.166], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-AUTH LOGIN PLAIN
250-STARTTLS
250-DELIVERBY
250 HELP
quit
221 2.0.0 example.com closing connection
Connection closed by foreign host.

如果 STARTTLS 出现在输出中,则一切正常。

最后更新于

FreeBSD 中文社区