[Server] TLS/SSL憑證(Certificate)指南 – 常用指令|製作CSR

SSL基本指令 | CSR 製作與設定 | 憑證檔案標準編碼格式 | SSL進階指令 | SSL安裝 | 交叉認證

Intro

Public key certificate - 公開金鑰認證 - Wikipedia

認證原理

Chain_Of_Trust

  • 中英文通用名詞:
    • Root certificate:根憑證
    • Intermediate certificate:中介憑證 / 中繼憑證
    • End-entity / leaf certificate:終端實體憑證

工具指令

詳細指令的可以參考: The Most Common OpenSSL Commands

申請憑證流程概念:

自產Server Key生成CSR > 上傳至憑證組織基於CSR產出憑證下載 > 設定至Web Server

SSL基本指令

產生自簽RootCA證書

1. 產生 Private Key

$ openssl genrsa -out root-ca.key

使用DES3密碼私鑰: openssl genrsa -des3 -out rootca.key 2048

2. 產生 CSR/REQ (certificate signing request)

$ openssl req -new -key root-ca.key -out root-ca.csr

3. 產生自簽署RootCA憑證

$ openssl x509 -req -days 3650 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_ca -in root-ca.csr -signkey root-ca.key -out root-ca.crt

自簽快速指令:openssl req -days 3650 -nodes -new -x509 -keyout root-ca.key -out root-ca.crt

簽署終端憑證

1. 產生 Private Key 與 CSR/REQ

$ openssl genrsa -out server.key
$ openssl req -new -key server.key -out server.csr

2. 從RootCA簽署終端憑證

$ openssl x509 -req -days 365 -sha1 -extfile /etc/ssl/openssl.cnf -extensions v3_req -CA root-ca.crt -CAkey root-ca.key -CAserial rootca.srl -CAcreateserial -in server.csr -out server.crt

CSR/CRT設定含SAN多網域:Certificate(CSR) configuration file


CSR 製作與設定

生成CSR時需輸入Owner資訊如下:

Country Name (2 letter code) [XX]:  TW
State or Province Name (full name) []:  Taiwan
Locality Name (eg, city) [Default City]:  Taipei
Organization Name (eg, company) [Default Company Ltd]: YIDAS Co., Ltd
Organizational Unit Name (eg, section) []: IT
Common Name (eg, your name or your server's hostname) []: code.yidas.com
Email Address []:myintaer@gmail.com

後面Extra的部分可以直接Enter略過

Common Name Case

1.單網域 (Single Domain)

code.yidas.com

2.多網域 (Multi-Domin)

Multi-Domain SSL Setup with “Subject Alternative Names”

可依據廠商的要求分隔

3.萬用網域 (Wildcard)

*.yidas.com

Private Key

Private key作為解密用途,通常會多包passpharse。

檢查Private Key

$ openssl rsa -check -in privateKey.key

移除passpharse

$ openssl rsa -in privateKey.pem -out privateKey-nopass.pem

憑證檔案標準編碼格式

一般為兩種,分Binary以及Text:

DER (Distinguished Encoding Rules)

  • 檔案內容: 二進位格式
  • 副檔名: .der, .cer

PEM (Privacy Enhanced Mail)

  • 檔案內容: 文字格式 (Base64 with BEGIN & END)
  • 副檔名: .pem, .cert

PEM Wiki: https://en.wikipedia.org/wiki/Privacy-Enhanced\_Mail

副檔名.crt並未定義歸類,一般兩種格式都有混用

憑證檔案其他格式

P7B / PKCS#7

  • 檔案內容: PEM編碼格式 (RFC 2315)
  • 副檔名: .p7b, .p7c, .keystore
  • 說明: 檔案只會包含憑證與中繼憑證,不會包含私密金鑰。主要用來對訊息簽章或加解密。

PKCS#8

  • 檔案內容: PEM編碼格式 (RFC 5958, RFC 5208)
  • 副檔名: .key, .pkcs8.key
  • 說明: 檔案主要包含私密金鑰,且可為一組密碼保護內容

PFX / PKCS#12 (predecessor of PKCS#12)

  • 檔案內容: 二進位格式 (RFC 7292)
  • 副檔名: .pfx, .p12, pkcs12
  • 說明: 檔案中同時包含憑證、中繼憑證、私密金鑰,且為一組密碼保護內容。
# 查看 P12 憑證內容
$ openssl pkcs12 -in mycert.p12 -nodes -passin pass:"password" | openssl x509 -noout -text

# 解包轉成PEM (Cert & Key)
$ openssl pkcs12 -in mycert.p12 -out file.key.pem -nocerts -nodes
$ openssl pkcs12 -in mycert.p12 -out file.crt.pem -clcerts -nokeys

# 解包轉成Bundle PEM (Cert + Key + 中繼憑證)
$ openssl pkcs12 -in mycert.p12  -out file.bundle.pem -nodes

SSL進階指令

檢視 CSR

openssl req -text -noout -verify -in CSR.csr

檢視 CRT (PEM & DER)

openssl x509 -noout -text -in cert.pem

# 檢視DER格式證書
openssl x509 -text -noout -inform DER -in cert.der

# 顯示全部憑證鏈(View full-chain)
openssl crl2pkcs7 -nocrl -certfile full-chain.pem | openssl pkcs7 -print_certs -text -noout

# 檢視Fingerprint
openssl x509 -noout -fingerprint -sha256 -inform pem -in [cert.pem]
openssl x509 -noout -fingerprint -sha1 -inform pem -in [cert.pem]
openssl x509 -noout -fingerprint -md5 -inform pem -in [cert.pem]

可由CA:FALSE欄位辨識憑證是否為終端憑證 Subject Key Identifier為目前憑證指紋、 Authority Key Identifier為上層憑證指紋

顯示目標Web server提供的憑證

// -servername 代表實際訪問使用的 SNI,不填等同於直接用 server IP 訪問
$ echo | openssl s_client -showcerts -servername www.example.com -connect example.com:443 2>/dev/null | openssl x509 -noout -text

// SMTP 支援 SNI
$ echo | openssl s_client -showcerts -starttls smtp -servername mail.example.com -connect example.com:587 2>/dev/null | openssl x509 -noout -text

// FTP Server 尚不支援 TLS SNI,加參數無效果
$ echo | openssl s_client -showcerts -starttls ftp -connect example.com:21 2>/dev/null | openssl x509 -noout -text

驗證

openssl verify server-cert.pem

# 驗證包含中繼憑證
openssl verify -untrusted intermediate.cert.pem cert.pem

# 驗證是否為CA簽發
openssl verify -CAfile ca.pem -untrusted intermediate.cert.pem cert.pem

憑證格式轉換

# DER to PEM
openssl x509 -inform der -in certificate.cer -out certificate.pem

# PEM to DER
openssl x509 -outform der -in your-cert.pem -out your-cert.cer

# to PFX(PKCS#12)
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.cert -certfile CACert.cert

SSL安裝

SSL提供商檔案(TWCA多中繼憑證為例):

root.cer //根憑證檔
server.cer //伺服器憑證檔(網域憑證)
uca_1.cer //中繼憑證檔1
uca_2.cer //中繼憑證檔1

其中,中繼憑證若為多個如上例,則將中繼憑證倒序合併至單檔:

cat uca_2.cer uca_1.cer > uca.cer

目前我尚未看過有人將Root CA也加入Chain

另外我方會有當時拿去申請憑證的CSR及其Pricate Key:

self-ssl.key // 所謂Server.key,HTTPS Server設定所需
self-ssl.csr // 由Private Key產生之CSR用於上傳申請憑證

Nginx SSL安裝

Configuring HTTPS servers - SSL certificate chains

不同於Apache,中繼憑證是可以直接Bundle至網域憑證中:

cat uca.cer server.cer > full-chained.cer

server {
    listen              443 ssl;
    server_name         www.example.com;
    ssl_certificate     full-chained.cer;
    ssl_certificate_key server.key;
    ...
}

憑證檔常態目錄:/etc/pki/tls/

注意:Nginx若沒有設定SSL憑證路徑,則HTTPS連線會自動被中斷

Apache SSL安裝

Apache SSL/TLS Strong Encryption: How-To Apache SSLCertificateChainFile Directive

Apache的中繼憑證是獨立設定的。

SSLEngine On
SSLCertificateFile /etc/ssl/server.cer
SSLCertificateKeyFile /etc/ssl/server.key
SSLCertificateChainFile /etc/ssl/uca.cer

交叉認證 - cross-certification

Wiki: 憑證鏈和交叉認證

  • 原理上主要是在 X.509 信任鍊終端/中繼憑證上設多個簽發參照值(Authority Key Identifier),讓憑證可以一對多向上對應到不同張CA憑證來達到多條合法的憑證鏈
  • 而不同的CA憑證具有相同的主題Subject Key Identifier與公鑰,其中透過交互認證簽發的CA憑證會有Authority Key Identifier再做對應,
  • 交互認證簽發的CA憑證應用上會一併由 Server 提供,讓 Client 可以走這條信任鍊提高對應到local根憑證的可能。

Cross-certification_diagram


延伸文章

網站SSL加密原理簡介 深度解析HTTPS原理 憑證串鍊的解釋 SSL X.509 憑證教學 憑證鏈和交叉認證 The Complete Guide To Switching From HTTP To HTTPS Wikipedia - X.509 Certificate chains and cross-certification

Leave a Reply

Your email address will not be published. Required fields are marked *