OpenSSL 提供密碼學基本元件、通訊協定程式庫與命令列 工具。命令適合用於檢查、互通性測試與本機 管理;應用程式碼則應採用持續維護的語言程式庫及其 安全預設值。
金鑰、CSR 與憑證 #
使用通用金鑰介面產生私密金鑰:
openssl genpkey -algorithm RSA \
-pkeyopt rsa_keygen_bits:3072 \
-out private-key.pem
openssl pkey -in private-key.pem -pubout -out public-key.pem建立憑證簽署要求並檢查其內容:
openssl req -new -key private-key.pem -out request.csr
openssl req -in request.csr -noout -text -verify
openssl x509 -in certificate.pem -noout -textreq -verify 會檢查 CSR 的持有證明,但不會驗證
要求的身分或簽發憑證。CSR 包含公開金鑰與
要求屬性,絕不包含私密金鑰。
使用明確指定的信任存放區驗證憑證路徑:
openssl verify -CAfile roots.pem -untrusted intermediate.pem certificate.pemPEM 會以帶有型別標記的 base64 文字包裝 DER 物件。轉換時應明確指定格式,並 保留物件型別:
openssl x509 -in certificate.pem -outform DER -out certificate.der
openssl x509 -inform DER -in certificate.der -out certificate.pem
openssl pkey -in private-key.pem -outform DER -out private-key.derTLS 用戶端也需要驗證主機名稱。進行診斷連線時, 應傳入預期的伺服器名稱及驗證選項:
openssl s_client -connect example.com:443 \
-servername example.com -verify_hostname example.com摘要與簽章 #
openssl dgst -sha256 artifact.tar
openssl rand -hex 32
openssl dgst -sha256 -sign private-key.pem -out artifact.sig artifact.tar
openssl dgst -sha256 -verify public-key.pem -signature artifact.sig artifact.tar只有在預期摘要值經由 已驗證的通道傳送時,摘要才能偵測意外變更。簽章同時驗證摘要及 對應私密金鑰的持有權,但是否信任公開金鑰仍是另一項 部署決策。
密碼式加密 #
openssl enc -aes-256-cbc -pbkdf2 -salt \
-in plaintext -out ciphertextopenssl enc 的傳統介面不提供現代 AEAD 模式。
請勿使用它自行設計應用程式加密通訊協定。需要經驗證
加密的檔案應使用有明確規範的 AEAD 格式或專用工具。
當互通性要求使用 enc 時,應明確指定摘要與迭代
次數,因為預設值可能變更;同時必須了解 CBC 密文
仍未經驗證。
若命令支援,應透過受保護的檔案或描述元傳遞機密資料。 以命令引數寫入的密碼可能出現在程序清單與 Shell 歷程記錄中。應限制私密金鑰權限,並僅透過 適合儲存媒體的程序移除暫存明文。
TLS 診斷 #
s_client 是診斷用戶端,不能取代
具備完整原則的應用程式。調查憑證路徑或交握失敗時,應加入
-showcerts、-verify_return_error,以及應用程式
預期的通訊協定選項:
openssl s_client -connect example.com:443 -servername example.com \
-verify_hostname example.com -verify_return_error -showcerts新的自動化流程應避免使用 rsautl 等已棄用命令;pkeyutl 與
genpkey 提供目前的介面。請使用
openssl list -commands 與 openssl list -providers 檢查本機安裝的文件,因為提供者、
演算法與預設值會因 OpenSSL 版本而異。
評論
在 GitHub 上閱讀或評論(發表評論需要登入)