PKI 與憑證管理

🔥 Vibe Prompt

「用 openssl 設定本地 CA。簽發伺服器憑證與客戶端憑證。驗證 mTLS 連線。」

# 步驟 1:根 CA
openssl genrsa -out root-ca.key 4096
openssl req -x509 -new -nodes -key root-ca.key \
  -days 3650 -out root-ca.crt \
  -subj "/C=TW/O=VibeTutor/CN=VibeTutor Root CA"

# 步驟 2:伺服器憑證
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr \
  -subj "/C=TW/O=VibeTutor/CN=api.vibetutor.com"

openssl x509 -req -in server.csr -CA root-ca.crt \
  -CAkey root-ca.key -CAcreateserial \
  -days 365 -out server.crt

# 步驟 3:驗證
openssl verify -CAfile root-ca.crt server.crt

mTLS(雙向 TLS)

# 客戶端憑證
openssl genrsa -out client.key 2048
openssl req -new -key client.key -out client.csr \
  -subj "/C=TW/O=VibeTutor/CN=client-1"
openssl x509 -req -in client.csr -CA root-ca.crt \
  -CAkey root-ca.key -days 365 -out client.crt

# 驗證鏈
openssl verify -CAfile root-ca.crt server.crt client.crt

憑證鏈

根 CA(自簽名,離線儲存)
  └── 中繼 CA(營運用)
        ├── 伺服器憑證(api.example.com)
        ├── 伺服器憑證(app.example.com)
        └── 客戶端憑證(微服務)

關鍵要點

  • ✅ 請根據本章主題補充具體的學習重點
  • ✅ 建議加入比較表格、程式碼範例或流程圖
  • ✅ 確保內容扎實且有價值

憑證鏈與驗證流程

完整憑證鏈

Root CA (根憑證,內建於瀏覽器/作業系統)
    └── Intermediate CA 1 (中繼憑證)
        └── Intermediate CA 2
            └── Server Certificate (你的網站)

瀏覽器驗證流程:

  1. 讀取 Server Certificate 中的 Issuer 欄位
  2. 找到簽署它的 Intermediate CA 2 憑證
  3. 讀取 Intermediate CA 2Issuer,找到 Intermediate CA 1
  4. 依此類推,直到找到內建在系統中的 Root CA
  5. 驗證 Root CA 的簽章是否有效

憑證格式轉換

| 格式 | 說明 | 轉換指令 | |:----:|------|---------| | PEM | Base64 編碼的文字格式(最常見) | — | | DER | 二進位格式 | openssl x509 -in cert.der -out cert.pem | | PFX/P12 | 包含私鑰的容器格式 | openssl pkcs12 -in cert.pfx -out cert.pem -nodes | | JKS | Java 專用格式 | keytool -importkeystore -srckeystore cert.jks -destkeystore cert.p12 |

使用 OpenSSL 檢查憑證

# 查看憑證詳細資訊
openssl x509 -in certificate.pem -text -noout

# 檢查憑證過期時間
openssl x509 -in certificate.pem -dates -noout

# 檢查 SSL 連線的憑證
echo | openssl s_client -connect example.com:443 -servername example.com


PKI:讓全世界信任你的網站

PKI(Public Key Infrastructure)是 HTTPS 信任鏈的基礎。當瀏覽器顯示綠色鎖頭時,背後是一整個 CA 階層架構在運作。

憑證鏈的結構

Root CA(根憑證)——內建在作業系統/瀏覽器中
  └─ Intermediate CA(中繼憑證)
       └─ 你的網站憑證(www.example.com)

瀏覽器信任 Root CA → Root CA 簽發 Intermediate CA → Intermediate CA 簽發你的網站憑證。這個鏈條確保了每個環節都可被驗證。

Let's Encrypt 與 ACME 協定

過去申請 SSL 憑證很貴(每年幾千元)。Let's Encrypt 讓任何人都可以免費取得憑證:

# 使用 Certbot 自動申請並安裝憑證
certbot --nginx -d example.com -d www.example.com

這個指令會自動驗證你擁有這個網域、簽發憑證、設定 Nginx——全部在 30 秒內完成。

常見的憑證錯誤

| 錯誤 | 原因 | 解法 | |:----|:----|:----| | 憑證過期 | 忘記更新 | 設定自動續期(Cron Job) | | 憑證名稱不匹配 | CN 是 example.com 但使用者在瀏覽 www.example.com | 加上 SAN(Subject Alternative Name) | | 自簽憑證 | 測試環境使用了自簽憑證 | 瀏覽器會顯示不安全 |

下一章預告:TLS 1.3

PKI 解決了「你是誰」的問題。下一章的 TLS 1.3 解決了「你們的通訊是否安全」的問題——加密、完整性驗證、前向安全性。

解鎖完整教學內容

本章為付費內容。加入專案即可解鎖超過 5000 字的深度解析,包含 10 個以上神級 Prompt 與真實 Source Code 範例!