Ruby - How to extract the rsa private and CA public keys from a ruby ​​.pfx file

I have a .pfx certificate and I need to extract the Public, Private and CA certificates using ruby.

Using a shell, I can do it like this:

# Extract Public Key (ask for password)
openssl pkcs12 -in file.pfx -out file_public.pem -clcerts -nokeys

# Extract Certificate Authority Key (ask for password)
openssl pkcs12 -in file.pfx -out file_ca.pem -cacerts -nokeys

# Extract Private Key (ask for password)
openssl pkcs12 -in file.pfx -out file_private.pem -nocerts -nodes

# Extract RSA Private Key
openssl rsa -in file_private.pfx -out file_private_rsa.key

# Create Combo file with Public and RSA Private Keys
cat file_private_rsa.key file_public.pem > file_combo.pem

      

In this post, DMKE shows how to convert keys to .PFX, but how to do the opposite?

+3


source to share


2 answers


pkcs = OpenSSL::PKCS12.new(File.read("file.pfx"), "password")

pkcs.key.to_pem

pkcs.certificate.to_pem

      



+2


source


pkcs = OpenSSL::PKCS12.new(File.read("xyz.p12"), "password_for_xyz.p12")
key = OpenSSL::PKey::RSA.new(pkcs.key.to_pem)
cert = OpenSSL::X509::Certificate.new(pkcs.certificate.to_pem)

      



+2


source







All Articles