OpenSSL 自建 CA 證書¶
OpenSSL 自建 CA 證書 是指利用 OpenSSL 工具建立私有的 憑證頒發機構,並用於簽發或管理數位憑證的流程。此方法常用於開發測試環境,以實現 HTTPS 加密或驗證特定的域名配置。^[600-developer__tools__security__CA.md]
產生 CA 私鑰¶
建立 CA 的第一步是產生私鑰。根據安全需求,可以選擇是否使用密碼對私鑰進行加密。^[600-developer__tools__security__CA.md]
-
使用密碼保護 (
-des3): 此方式會生成一個受 DES3 加密的私鑰檔案,在操作過程中需輸入密碼(例如:123456)^[600-developer__tools__security__CA.md]。openssl genrsa -des3 -out ca-des3.key 2048 -
不使用密碼: 若為了自動化流程方便,可以產生無密碼保護的私鑰^[600-developer__tools__security__CA.md]。
openssl genrsa -out ca.key 2048
簽發 CA 根憑證¶
擁有私鑰後,需使用 req 指令配合 -x509 參數來產生自我簽署的根憑證(CRT/CER)。此過程會要求輸入區分名稱,如國家、地區、組織與通用名稱等^[600-developer__tools__security__CA.md]。
openssl req -x509 -key ca-des3.key -out ca.crt -days 3650
在互動式輸入中,需填寫以下資訊^[600-developer__tools__security__CA.md]:
* Country Name (2 letter code):例如 TW。
* State or Province Name:例如 Taiwan。
* Locality Name:例如 taipei。
* Organization Name:例如 tommy-dev。
* Common Name (CN):例如 *.yudady.tk(可用於匹配域名)。
驗證與讀取憑證¶
產生憑證後,可使用 x509 指令讀取並驗證其詳細資訊^[600-developer__tools__security__CA.md]。
openssl x509 -in ca.crt -text -noout
此指令會顯示憑證的版本、序號、簽章演算法、頒發者、有效期間以及 X.509v3 extensions(如 Basic Constraints: CA:TRUE,確認該憑證具有 CA 權限)^[600-developer__tools__security__CA.md]。
域名配置¶
在本地環境測試自建憑證時,通常需要修改系統的 hosts 檔案,將 IP 位址映射至憑證中設定的域名^[600-developer__tools__security__CA.md]。
在 /etc 目錄下的 hosts 檔案(或 Windows 的 C:\Windows\System32\drivers\etc\hosts)中加入以下設定^[600-developer__tools__security__CA.md]:
127.0.0.1 <你的域名>
此外,若使用配置檔案(如 caconfig.cnf 或 exampleserver.cnf)來生成憑證簽署請求(CSR),需確保檔案中的 subjectAltName 或 commonName 欄位已更新為目標域名^[600-developer__tools__security__CA.md]。
相關概念¶
- [[X.509]]
- [[HTTPS]]
- 32-bit ODBC architecture requirement (System Configuration Context)