Linux 系統管理員 - 建立 SSL 證書



TLS 和 SSL 背景

TLS 是套接字層安全的新標準,取代了 SSL。TLS 提供了更好的加密標準以及其他安全和協議包裝功能,從而超越了 SSL。通常,TLS 和 SSL 術語可以互換使用。但是,作為一名專業的 CentOS 管理員,瞭解兩者之間的區別和歷史非常重要。

SSL 最高版本為 3.0。SSL 在 Netscape 的推動下成為行業標準。在 Netscape 被 AOL(90 年代流行的 ISP,即美國線上)收購後,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 求職者看起來像一位經驗豐富的計算機科學專業人士。

TLS SSL
- 3.0
1.0 3.1
1.1 3.2
1.2 3.3

TLS 執行了當今網際網路使用者重要的兩個主要功能:第一,它驗證一方的身份,稱為身份驗證。第二,它在傳輸層為缺乏此原生功能的上層協議(ftp、http、電子郵件協議等)提供端到端加密

第一個,驗證一方的身份,對於端到端加密的安全至關重要。如果消費者與未被授權接受付款的網站建立了加密連線,則財務資料仍然存在風險。這就是每個網路釣魚網站都無法做到的:一個經過正確簽名的 TLS 證書,驗證網站運營商的身份,證明其來自受信任的 CA

只有兩種方法可以繞過沒有正確簽名證書的問題:誘騙使用者允許網路瀏覽器信任自簽名證書,或者希望使用者不瞭解技術,並且不知道受信任的證書頒發機構 (或 CA) 的重要性。

在本教程中,我們將使用所謂的自簽名證書。這意味著,如果沒有在訪問該網站的每個網路瀏覽器中明確授予此證書受信任狀態,則會顯示錯誤訊息,阻止使用者訪問該網站。然後,它將使使用者在訪問具有自簽名證書的網站之前執行一些操作。請記住,為了安全起見,這是一件好事。

安裝和配置 openssl

openssl 是 TLS 開源實現的標準。openssl 用於 Linux、BSD 發行版、OS X 等系統,甚至支援 Windows。

openssl 很重要,因為它提供了傳輸層安全,併為開發人員抽象了身份驗證端到端加密的詳細程式設計。這就是為什麼 openssl 與幾乎所有使用 TLS 的開源應用程式一起使用的原因。它也預設安裝在每個現代 Linux 版本上。

預設情況下,openssl 應該從 CentOS 5 版開始就已安裝。為了確保,讓我們嘗試透過 YUM 安裝 openssl。只需執行安裝,因為 YUM 足夠智慧,可以讓我們知道某個包是否已安裝。如果出於相容性原因我們正在執行舊版 CentOS,則執行yum -y install 將確保 openssl 針對最近的 Heartbleed 漏洞進行更新。

執行安裝程式時,發現實際上對 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命令使用的每個選項的說明 -

命令 操作
req -X509 使用 X.509 CSR 管理 PKI 標準進行金鑰管理。
-nodes 不要使用密碼保護我們的證書。Apache 必須能夠在不中斷密碼的情況下使用證書。
-days 2555 將證書的有效期設定為 7 年或 2555 天。時間段可以根據需要調整。
-newkey rsa:2048 指定使用 RSA 以 2048 位長度生成金鑰和證書。

接下來,我們希望為與客戶端協商 PFS 建立一個 Diffie-Hellman 組。

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

這將花費 5 到 15 分鐘。

完美前向保密 - 用於在私鑰遭到洩露的情況下保護會話資料。這將生成一個在客戶端和伺服器之間使用的金鑰,該金鑰對於每個會話都是唯一的。

現在,將完美前向保密配置新增到我們的證書中。

[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進行以下更改。但是,在執行此操作之前,我們應該備份原始檔案。在使用viemacs等高階文字編輯器對生產伺服器進行更改時,最佳實踐是在進行編輯之前始終備份配置檔案。

[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

現在我們要註釋掉以下行。

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

最後,我們需要允許傳入連線透過埠 443 到https

廣告

© . All rights reserved.