
我有 OpenSSL 憑證。我還需要私鑰。有人告訴我它位於伺服器上的某個位置,確實如此,我發現了多個 SSL 金鑰檔案。
在我透過暴力嘗試所有這些之前:有沒有辦法可以在終端上找出是否使用私鑰來建立憑證?
答案1
這方面的經典參考是這個常見問題解答在線上 Apache 文件中。
在該文件中,md5
使用了模數,因為
由於公共指數通常為 65537,並且很難透過肉眼檢查長模數是否相同,因此可以使用以下方法
這給出:
$ openssl rsa -noout -modulus -in key.pem.decrypted | openssl md5
(stdin)= 9fxxfoobar558d9xx0045a89467d2bxx
$ openssl x509 -noout -modulus -in crt.pem | openssl md5
(stdin)= 9fxxfoobar558d9xx0045a89467d2bxx
我建議使用過程替換來避免模數的視覺比較:
$ diff \
<(openssl x509 -noout -modulus -in cert.pem) \
<(openssl rsa -noout -modulus -in key.pem.decrypted)
如果輸出為空,則私鑰與憑證相符。
這只是一種比安德魯舒爾曼的答案中的方法更方便的比較字串的方法,當然,這也是有效的。
另一個參考這裡。
答案2
若要檢查憑證檔案是否證明與給定私鑰檔案關聯的公鑰,請比較與每個私鑰檔案關聯的公鑰:
openssl rsa -in KEYFILE -pubout
openssl x509 -in CERTFILE -pubkey -noout
當且僅當公鑰相同時,憑證和私鑰才會匹配。