各類證書由於存儲的內容不同(如是否包含公鑰/私鑰是否加密存儲/單一證書或多證書等)、采用編 碼不同(DER/BASE64)、標准不同(如PEM/PKCS),所以盡管X.509標准規定了證書內容規范,但證書文件還是五花八門。好在 openssl對這些不同的標准都有着不錯的支持,可以用來進行不同格式證書的轉換。
大體來說,證書轉換要作的工作有這么幾種
編碼轉換:DER<-->BASE64
不同證書標准的轉換:PKCS系列<-->PEM/CER
私鑰的增/減/提取/轉換
...
PEM--DER/CER(BASE64--DER編碼的轉換)
openssl x509 -outform der -in certificate.pem -out certificate.derPEM--P7B(PEM--PKCS#7)
openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cerPEM--PFX(PEM--PKCS#12)
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crtPEM--p12(PEM--PKCS#12)
openssl pkcs12 -export -out Cert.p12 -in Cert.pem -inkey key.pemCER/DER--PEM(編碼DER--BASE64)
openssl x509 -inform der -in certificate.cer -out certificate.pemP7B--PEM(PKCS#7--PEM)
openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cerP7B--PFX(PKCS#7--PKCS#12)
openssl pkcs7 -print_certs -in certificate.p7b -out certificate.ceropenssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx -certfile CACert.cer
PFX/p12--PEM(PKCS#12--PEM)
openssl pkcs12 -in certificate.pfx -out certificate.cer如無需加密pem中私鑰,可以添加選項-nodes;
如無需導出私鑰,可以添加選項-nokeys;
PEM BASE64--X.509文本格式
openssl x509 -in Key.pem -text -out Cert.pemPFX文件中提取私鑰(.key)
openssl pkcs12 -in mycert.pfx -nocerts -nodes -out mycert.keyPEM--SPC
openssl crl2pkcs7 -nocrl -certfile venus.pem -outform DER -out venus.spcPEM--PVK(openssl 1.x開始支持)
openssl rsa -in mycert.pem -outform PVK -pvk-strong -out mypvk.pvkPEM--PVK(對於openssl 1.x之前的版本,可以下載pvk轉換器后通過以下命令完成)
pvk -in ca.key -out ca.pvk -nocrypt -topvkPVK格式更多參考:http://www.drh-consultancy.demon.co.uk/pvk.html
openssl更多選項及功能,請參考openssl手冊