📜  Linux管理员-创建SSL证书

📅  最后修改于: 2020-10-31 13:34:01             🧑  作者: Mango


TLS和SSL背景

TLS是套接字层安全性的新标准,继承于SSL。 TLS提供了更好的加密标准,同时还具有其他提高SSL的安全性和协议包装功能。术语TLS和SSL通常可以互换使用。但是,作为专业的CentOS管理员,重要的是要注意两者之间的区别和历史。

SSL升级到3.0版。 SSL是作为Netscape的行业标准开发和推广的。在Netscape被AOL(在90年代流行的ISP,也被称为America Online)收购之后,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专业。

TLS SSL
3.0
1.0 3.1
1.1 3.2
1.2 3.3

TLS执行了两个对于当今的Internet用户来说很重要的主要功能:一,它可以验证一方是谁,称为验证。第二,它为缺乏此本地功能的高层协议(ftp,http,电子邮件协议等)在传输层提供端到端加密

首先,验证一方谁,并作为端到端加密对安全性很重要。如果消费者与未经授权付款的网站建立了加密连接,则财务数据仍然存在风险。这就是每个网络钓鱼站点都将无法拥有的:经过正确签名的TLS证书,可验证网站运算符是否声称自己来自受信任的CA。

仅有两种方法可以解决没有正确签名的证书的问题:诱使用户信任Web浏览器的自签名证书,或者希望用户不懂技术,并且不知道可信证书的重要性授权(或CA)。

在本教程中,我们将使用所谓的自签名证书。这意味着,如果没有在访问该网站的每个Web浏览器中都明确授予此证书可信任的状态,则将显示错误,使用户无法访问该网站。然后,它将使用户跳过一些操作,然后再访问带有自签名证书的网站。请记住,出于安全考虑,这是一件好事。

安装和配置openssl

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

openssl很重要,因为它提供了传输层的安全性,并为开发人员抽象了身份验证端到端加密的详细程序。这就是为什么opensl与几乎所有使用TLS的单个开源应用程序一起使用的原因。默认情况下,它也安装在每个现代Linux版本上。

默认情况下, openssl至少应从版本5开始安装在CentOS上。为了保证,让我们尝试通过YUM安装openssl 。只需运行install,因为YUM足够智能,可以让我们知道是否已安装软件包。如果出于兼容性原因我们正在运行旧版本的CentOS,则执行yum -y安装将确保针对半最新的心脏出血漏洞更新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-创建自签名的ssl证书。

首先,让我们配置我们的私有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命令一起使用的每个选项的解释-

Command Action
req -X509 Use X.509 CSR management PKI standard for key management.
-nodes Do not secure our certificate with a passphrase. Apache must be able to use the certificate without interruption of a passphrase.
-days 2555 Tells the validity of the certificate to 7 years or 2555 days. Time period can be adjusted as needed.
-newkey rsa:2048 Specified to generate both key and certificate using RSA at 2048 bits in length.

接下来,我们要创建一个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进行以下更改。但是,在执行此操作之前,我们应该备份原始文件。在像viemcas这样的高级文本编辑器中对生产服务器进行更改时,最佳做法是始终在进行编辑之前备份配置文件。

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

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

  • 定位
  • 如下编辑DocumentRoot和ServerName。
\\# General setup for the virtual host, inherited from global configuration
DocumentRoot "/var/www/html"
ServerName centos.vmnet.local:443

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

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

现在,我们要注释掉以下几行。

SSL协议

#   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

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