Linux Admin - 创建SSL证书

TLS和SSL背景

TLS是套接字层安全性的新标准,正在进行SSL. TLS提供了更好的加密标准,其他安全性和协议包装功能推动了SSL.通常,术语TLS和SSL可互换使用.但是,作为专业的CentOS管理员,重要的是要注意区分每个的差异和历史.

SSL 升级到3.0版. SSL是在Netscape下开发和推广的行业标准.在AOL(一个在90年代流行的ISP,也称为America Online)购买Netscape之后,AOL从未真正推动过SSL安全性改进所需的更改.

在版本3.1中, SSL 技术已进入开放系统标准,并已更改为 TLS .由于 SSL 的版权仍由AOL拥有,因此创造了一个新术语: TLS  - 传输层安全性.因此,重要的是要承认 TLS 实际上与 SSL 不同.特别是,由于旧的 SSL 技术已知安全问题,而且有些已被认为已过时.

注意 : 在谈到技术3.1及更高版本时,本教程将使用术语 TLS .然后在特定于SSL技术3.0及更低版本的评论时 SSL .

SSL与TLS版本控制

下表显示了TLS和SSL版本控制将彼此相关.我听说过一些人用 SSL 3.2版说话.但是,他们可能从阅读博客中获得了术语.作为专业管理员,我们总是希望使用标准术语.因此,虽然说 SSL 应该是对过去技术的参考.简单的事情可以使CentOS求职者看起来像经验丰富的CS Major.

TLSSSL
-3.0
1.03.1
1.13.2
1.23.3

TLS 执行当今互联网用户的两个主要功能:一,它验证一方是谁,称为身份验证.第二,它在传输层提供端到端加密,用于缺少此本机功能的上层协议(ftp,http,电子邮件协议等).

第一个,验证一方是谁,并且作为端到端加密对安全性很重要.如果消费者与未授权进行付款的网站建立加密连接,则财务数据仍然存在风险.这是每个网络钓鱼站点都没有的:正确签署的TLS证书,验证网站运营商是他们声称来自可信任CA的人.

只有两种方法可以避免使用正确签名的证书:欺骗用户允许信任Web浏览器以获得自签名证书,或者希望用户不懂技术,也不会知道可信证书颁发机构的重要性(或者CA).

在本教程中,我们将使用所谓的自签名证书.这意味着,如果没有明确地在访问该网站的每个Web浏览器中为此证书提供受信任状态,将显示一个错误,阻止用户访问该站点.然后,它将使用户在使用自签名证书访问站点之前跳过一些操作.请记住,为了安全起见,这是一件好事.

安装和配置openssl

openssl 是标准TLS的开源实现. openssl 用于Linux,BSD发行版,OS X等系统,甚至支持Windows.

openssl很重要,因为它提供了传输层安全性和摘要为开发人员详细编程身份验证端到端加密.这就是使用TLS几乎每个开源应用程序都使用openssl的原因.它也默认安装在每个现代版本的Linux上.

默认情况下, openssl 应至少从版本5开始安装在CentOS上.为了保证,让我们尝试通过YUM安装 openssl .只需运行安装,因为YUM非常智能,可以告诉我们是否已安装软件包.如果我们出于兼容性原因运行较旧版本的CentOS,那么执行 yum -y install 将确保openssl针对半新近出血的漏洞进行更新.

运行安装程序时,发现实际上有 openssl 的更新.

[root@centos]# yum -y install openssl
Resolving Dependencies
--> Running transaction check
---> Package openssl.x86_64 1:1.0.1e-60.el7 will be updated
---> Package openssl.x86_64 1:1.0.1e-60.el7_3.1 will be an update
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.1e-60.el7_3.1 for 
package: 1:openssl-1.0.1e-60.el7_3.1.x86_64
--> Running transaction check
---> Package openssl-libs.x86_64 1:1.0.1e-60.el7 will be updated
---> Package openssl-libs.x86_64 1:1.0.1e-60.el7_3.1 will be an update
--> Finished Dependency Resolution 
Dependencies Resolved

===============================================================================
=============================================================================== 
 Package                               Arch
 Version                            Repository                        Size 
=============================================================================== 
=============================================================================== 
Updating: 
openssl                               x86_64                          
1:1.0.1e-60.el7_3.1                 updates                           713 k
Updating for dependencies:

为OpenLDAP创建自签名证书

这是为我们之前的 OpenLDAP 安装创建自签名的方法.

创建自签名的OpenLDAP证书.

openssl req -new -x509 -nodes -out /etc/openldap/certs/myldaplocal.pem -keyout
/etc/openldap/certs/myldaplocal.pem -days 365

[root@centos]# openssl req -new -x509 -nodes -out /etc/openldap/certs/vmnet.pem 
-keyout /etc/openldap/certs/vmnet.pem -days 365 
Generating a 2048 bit RSA private key
.............................................+++
................................................+++
writing new private key to '/etc/openldap/certs/vmnet.pem'
-----
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) [XX]:US
State or Province Name (full name) []:Califonia
Locality Name (eg, city) [Default City]:LA
Organization Name (eg, company) [Default Company Ltd]:vmnet
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:centos
Email Address []:bob@bobber.net
[root@centos]#

现在我们的OpenLDAP证书应该放在/etc/openldap/certs/

 
 [root @ centos ] #ls/etc/openldap/certs/*.pem 
/etc/openldap/certs/vmnetcert.pem/etc/openldap/certs/vmnetkey.pem 
 [root @ centos]#

如您所见,我们在/etc/openldap/certs/目录中安装了证书和密钥.最后,我们需要更改每个权限,因为它们当前归root用户所有.

 
 [root @ centos] #chown  - R ldap:ldap/etc/openldap/certs/*.pem 
 [root @ centos] #ls -ld/etc/openldap/certs/*.pem 
 -rw-r  -  r-- . 1 ldap ldap 1395 Feb 20 10:00/etc/openldap/certs/vmnetcert.pem 
 -rw-r  -  r--. 1 ldap ldap 1704 Feb 20 10:00/etc/openldap/certs/vmnetkey.pem 
 [root @ centos]#

创建自我已签名的Apache Web服务器证书

在本教程中,我们假设已经安装了Apache.我们在另一个教程(配置CentOS防火墙)中安装了Apache,并将进入Apache的高级安装以供将来的教程使用.因此,如果您还没有安装Apache,请按照以下步骤进行操作.

一旦使用以下步骤安装Apache HTTPd :

第1步 : 为Apache httpd服务器安装mod_ssl.

首先,我们需要使用mod_ssl配置Apache.使用YUM包管理器这非常简单去;

 
 [root @ centos] #yum -y install mod_ssl

然后重新加载Apache守护进程以确保Apache使用新配置.

 
 [root @ centos] #systemctl reload httpd

此时,Apache配置为支持本地主机上的TLS连接.

第2步 : 创建自签名证书.

首先,让我们配置我们的私人TLS密钥目录.

 
 [root @ centos] #mkdir/etc/ssl/private 
 [root @ centos] #chmod 700/etc/ssl/private/

注意 : 确保只有root用户具有对此目录的读/写访问权限.通过全局读/写访问,您的私钥可用于解密嗅探流量.

生成证书和密钥文件.

[root@centos]# sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout 
/etc/ssl/private/self-gen-apache.key -out /etc/ssl/certs/self-sign-apache.crt 
Generating a 2048 bit RSA private key
..........+++
....+++
-----
Country Name (2 letter code) [XX]:US
State or Province Name (full name) []:xx
Locality Name (eg, city) [Default City]:xxxx
Organization Name (eg, company) [Default Company Ltd]:VMNET
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:centos.vmnet.local
Email Address []:

[root@centos]#

注意号;如果您没有注册域名,可以使用服务器的公共IP地址.

让我们来看看我们的证书 :

[root@centos]# openssl x509 -in self-sign-apache.crt -text -noout
Certificate:
   Data:
      Version: 3 (0x2)
      Serial Number: 17620849408802622302 (0xf489d52d94550b5e)
   Signature Algorithm: sha256WithRSAEncryption
   Issuer: C=US, ST=UT, L=xxxx, O=VMNET, CN=centos.vmnet.local
   Validity
      Not Before: Feb 24 07:07:55 2017 GMT
      Not After : Feb 24 07:07:55 2018 GMT
   Subject: C=US, ST=UT, L=xxxx, O=VMNET, CN=centos.vmnet.local
   Subject Public Key Info:
      Public Key Algorithm: rsaEncryption
         Public-Key: (2048 bit)
            Modulus:
               00:c1:74:3e:fc:03:ca:06:95:8d:3a:0b:7e:1a:56:
               f3:8d:de:c4:7e:ee:f9:fa:79:82:bf:db:a9:6d:2a:
               57:e5:4c:31:83:cf:92:c4:e7:16:57:59:02:9e:38:
               47:00:cd:b8:31:b8:34:55:1c:a3:5d:cd:b4:8c:b0:
               66:0c:0c:81:8b:7e:65:26:50:9d:b7:ab:78:95:a5:
               31:5e:87:81:cd:43:fc:4d:00:47:5e:06:d0:cb:71:
               9b:2a:ab:f0:90:ce:81:45:0d:ae:a8:84:80:c5:0e:
               79:8a:c1:9b:f4:38:5d:9e:94:4e:3a:3f:bd:cc:89:
               e5:96:4a:44:f5:3d:13:20:3d:6a:c6:4d:91:be:aa:
               ef:2e:d5:81:ea:82:c6:09:4f:40:74:c1:b1:37:6c:
               ff:50:08:dc:c8:f0:67:75:12:ab:cd:8d:3e:7b:59:
               e0:83:64:5d:0c:ab:93:e2:1c:78:f0:f4:80:9e:42: 
               7d:49:57:71:a2:96:c6:b8:44:16:93:6c:62:87:0f:
               5c:fe:df:29:89:03:6e:e5:6d:db:0a:65:b2:5e:1d:
               c8:07:3d:8a:f0:6c:7f:f3:b9:32:b4:97:f6:71:81:
               6b:97:e3:08:bd:d6:f8:19:40:f1:15:7e:f2:fd:a5:
               12:24:08:39:fa:b6:cc:69:4e:53:1d:7e:9a:be:4b:

这是一个解释对于我们使用 openssl 命令 :

命令动作
req -X509使用X.509 CSR管理PKI标准进行密钥管理.
-  nodes请勿使用密码保护我们的证书. Apache必须能够在不中断密码的情况下使用证书.
-  days 2555告知证书的有效期为7年或2555天.可以根据需要调整时间段.
-  newkey rsa:2048指定使用长度为2048位的RSA生成密钥和证书.

接下来,我们要创建一个Diffie-Heliman组来与客户协商PFS.

 
 [centos#] openssl dhparam -out/etc/ssl/certs/dhparam.pem 2048

这需要5到15分钟.

完美向前保密 : 用于在私钥被泄露的情况下保护会话数据.这将生成客户端和服务器之间用于每个会话唯一的密钥.

现在,将Perfect Forward Secrecy配置添加到我们的证书中.

 
 [root @ centos] #cat/etc/ssl/certs/dhparam.pem | tee -a/etc/ssl/certs/self-sign-apache.crt

配置Apache使用密钥和证书文件

我们将更改/etc/httpd/conf.d/ssl.conf :

我们将对<进行以下更改 ssl.conf中的.但是,在我们这样做之前,我们应该备份原始文件.在高级文本编辑器(如 vi emcas )中更改生产服务器时,最佳做法是在进行编辑之前始终备份配置文件.

 
 [root @ centos] #cp/etc/httpd/conf.d/ssl.conf~/

现在让我们在将已知工作的ssl.conf副本复制到主文件夹的根目录后继续我们的编辑.

  • 定位

  • 按如下方式编辑DocumentRoot和ServerName.

 
 \\#虚拟主机的常规设置,继承自全局配置
 DocumentRoot"/var/www/html"
 ServerName centos.vmnet.local:443

DocumentRoot 这是默认apache目录的路径.在此文件夹中应该是一个默认页面,它将显示一个HTTP请求,询问您的Web服务器或站点的默认页面.

ServerName 是服务器名称可以是IP地址或服务器的主机名.对于TLS,最佳做法是使用主机名创建证书.在我们的OpenLdap教程中,我们在本地企业域上创建了一个centos的主机名: vmnet.local

现在我们要评论以下几行.

SSLProtocol

#   SSL Protocol support:
# List the enable protocol levels with which clients will be able to
# connect.  Disable SSLv2 access by default:
 ~~~~> #SSLProtocol all -SSLv2
 
#   SSL Cipher Suite:
#   List the ciphers that the client is permitted to negotiate.
#   See the mod_ssl documentation for a complete list.
 ~~~~> #SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA

然后让Apache知道去哪里找到我们的证书和私钥/公钥对.

指定我们自签名证书文件的路径

#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
~~~~> SSLCertificateFile /etc/ssl/certs/self-sign-apache.crt
specify path to our private key file
#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
~~~~> SSLCertificateKeyFile /etc/ssl/private/self-gen-apache.key

最后,我们需要允许到 https的入站连接通过443端口.