2014-01-15 76 views
21

使用openssl,我創建了一個私有密鑰如下:創建.p12文件

openssl genrsa -out myKey.pem 

然後,生成由CA要求的csr,我執行以下操作:

openssl req -new -key myKey.pem -out cert.csr 

CA的迴應,這是我保存在一個文件名爲證書myCert.cer

我現在想捆綁必要的組件(私鑰,公鑰(?)和證書證書)成一個單一的.p12。要做到這一點,我運行以下命令:

openssl pkcs12 -export -out keyStore.p12 -inkey myKey.pem -in myCert.cer 

但我發現了以下錯誤消息:

No certificate matches private key 

我怎樣才能做到這一點?

+6

順便說一句,如果有人想籤'cert.csr '他自己,那麼可以使用'openssl x509 -req -in cert.csr -signkey key.pem -out cert.crt',然後'openssl pkcs12 -export -in cert.crt -inkey key.pem -out cert.p12 '。無需使用答案中的附加命令。 – Tom

+0

@Tom謝謝,您的評論解決了我的問題。 –

回答

22

openssldocumentation說,作爲-in參數提供文件必須PEM格式。

事實證明,違背了CA的手冊,我存儲在myCert.cer由CA返回的證書是不PEM格式,而它是PKCS7

爲了創建我.p12,我只好先證書轉換爲PEM

openssl pkcs7 -in myCert.cer -print_certs -out certs.pem 

,然後執行

openssl pkcs12 -export -out keyStore.p12 -inkey myKey.pem -in certs.pem 
+6

什麼是「myKey.pem」?是哪個文件? – Rodniko

+0

這就是擁有私鑰的人;看問題的開始。 –

+1

如何創建p12文件的一步一步的最佳解釋。謝謝! – raspayu