Convert p12 cert to pem format

p12 is the default cert format provided by Apple Push Notification services. However, most of the APN gem out there (E.g. grocer, lowdown, apnotic, apns) uses PEM as input. There are two ways to convert a p12 cert to equivalent pem format.

Programmatically in Ruby: (If you are managing multiple certs from multiple users in your system, it is good to have them upload a p12 and you convert it in the background)

str = File.open("cert.p12", "rb").read # Read P12 file

pkcs12 = OpenSSL::PKCS12.new str, "password123" 
out = pkcs12.certificate.to_s + pkcs12.key.to_s # Concat pkcs12 cert and private key into one string
File.open("cert.pem", 'w') { |file| file.write out } # Output as PEM

Manually: (If you are using only one cert in your application, then you only need to manually convert once.)

openssl pkcs12 -export -in "cert.p12" -out "cert.pem" -passin pass:password123 -nodes

AI Summary
Chrome On-device AI 2024-11-09 04:31:17

Share Article