14.6. OpenSSL

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

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

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

  1. DEFAULT_VERSIONS+= ssl=openssl

OpenSSL 的另一个常见用途是提供用于软件应用程序的证书。证书可用于验证公司或个人的凭据。如果证书尚未由外部 证书颁发机构 (CA) 签名(如 http://www.verisign.com ),则使用该证书的应用程序将生成警告。获取签名证书会产生相关费用,并且使用签名证书不是强制性的,因为证书可以是自签名的。但是,使用外部权限将防止出现警告,并且可以使用户放心。

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

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

14.6.1. 生成证书

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

  1. # openssl req -new -nodes -out req.pem -keyout cert.key -sha256 -newkey rsa:2048
  2. Generating a 2048 bit RSA private key
  3. ..................+++
  4. .............................................................+++
  5. writing new private key to 'cert.key'
  6. -----
  7. You are about to be asked to enter information that will be incorporated
  8. into your certificate request.
  9. What you are about to enter is what is called a Distinguished Name or a DN.
  10. There are quite a few fields but you can leave some blank
  11. For some fields there will be a default value,
  12. If you enter '.', the field will be left blank.
  13. -----
  14. Country Name (2 letter code) [AU]:US
  15. State or Province Name (full name) [Some-State]:PA
  16. Locality Name (eg, city) []:Pittsburgh
  17. Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Company
  18. Organizational Unit Name (eg, section) []:Systems Administrator
  19. Common Name (eg, YOUR name) []:localhost.example.org
  20. Email Address []:trhodes@FreeBSD.org
  21. Please enter the following 'extra' attributes
  22. to be sent with your certificate request
  23. A challenge password []:
  24. An optional company name []:Another Name

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

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

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

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

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

  1. # openssl req -new -x509 -days 365 -key cert.key -out cert.crt -sha256
  2. You are about to be asked to enter information that will be incorporated
  3. into your certificate request.
  4. What you are about to enter is what is called a Distinguished Name or a DN.
  5. There are quite a few fields but you can leave some blank
  6. For some fields there will be a default value,
  7. If you enter '.', the field will be left blank.
  8. -----
  9. Country Name (2 letter code) [AU]:US
  10. State or Province Name (full name) [Some-State]:PA
  11. Locality Name (eg, city) []:Pittsburgh
  12. Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Company
  13. Organizational Unit Name (eg, section) []:Systems Administrator
  14. Common Name (e.g. server FQDN or YOUR name) []:localhost.example.org
  15. Email Address []:trhodes@FreeBSD.org

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

14.6.2. 使用证书

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

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

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

  1. sendmail_enable="YES"
  2. sendmail_cert_create="YES"
  3. 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。保存编辑内容后,重新启动 Sendmail 。

  1. # service sendmail restart

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

  1. # telnet example.com 25
  2. Trying 192.0.34.166...
  3. Connected to example.com.
  4. Escape character is '^]'.
  5. 220 example.com ESMTP Sendmail 8.14.7/8.14.7; Fri, 18 Apr 2014 11:50:32 -0400 (EDT)
  6. ehlo example.com
  7. 250-example.com Hello example.com [192.0.34.166], pleased to meet you
  8. 250-ENHANCEDSTATUSCODES
  9. 250-PIPELINING
  10. 250-8BITMIME
  11. 250-SIZE
  12. 250-DSN
  13. 250-ETRN
  14. 250-AUTH LOGIN PLAIN
  15. 250-STARTTLS
  16. 250-DELIVERBY
  17. 250 HELP
  18. quit
  19. 221 2.0.0 example.com closing connection
  20. Connection closed by foreign host.

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