本文介紹在Linux系統上利用OpenSSL驗證數字證書的完整流程。
第一步:安裝OpenSSL
大多數Linux發行版預裝OpenSSL。若未安裝,請使用以下命令安裝:
sudo apt-get update sudo apt-get install openssl
第二步:查看證書信息
假設你的證書文件名為certificate.crt,使用以下命令查看其詳細信息:
openssl x509 -in certificate.crt -text -noout
第三步:驗證證書鏈
對于由中間證書簽發的證書,需驗證完整證書鏈。假設你擁有:certificate.crt (你的證書),intermediate.crt (中間證書),root.crt (根證書)。 將它們合并,并驗證:
cat certificate.crt intermediate.crt root.crt > fullchain.crt openssl verify -CAfile root.crt fullchain.crt
成功驗證后,輸出顯示 fullchain.crt: OK。
第四步:檢查證書有效期
使用以下命令查看證書有效期:
openssl x509 -in certificate.crt -noout -dates
輸出格式例如:notBefore=Jan 1 12:00:00 2020 GMT notAfter=Dec 31 23:59:59 2020 GMT
第五步:驗證證書簽名
驗證證書簽名是否有效:
openssl x509 -in certificate.crt -noout -modulus | openssl md5 openssl rsa -in certificate.key -noout -modulus | openssl md5
兩個命令輸出一致則簽名有效。
第六步:檢查證書吊銷狀態 (可選)
使用OCSP (Online Certificate Status Protocol) 或CRL (Certificate Revocation List) 檢查證書吊銷狀態。 以下為OCSP示例:
openssl ocsp -issuer issuer.crt -cert certificate.crt -url http://ocsp.example.com
成功驗證且證書未吊銷,輸出顯示 OCSP response: success。 請將 http://ocsp.example.com 替換為實際的OCSP URL。
第七步:驗證證書主題和頒發者
檢查證書的主題和頒發者信息:
openssl x509 -in certificate.crt -noout -subject openssl x509 -in certificate.crt -noout -issuer
輸出類似:subject=CN=example.com issuer=CN=Root CA,O=Example Org,C=US
通過以上步驟,即可在Linux環境下全面驗證數字證書的有效性和安全性。 請注意替換示例中的文件名和URL為你的實際值。