Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - How to extract public, rsa private and CA keys from .pfx file on ruby

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

Using the shell I can make it this way:

# 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 from private .pem
openssl rsa -in file_private.pem -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

On this post DMKE shows how to transform the keys to .PFX, but how to do the other way round?

like image 827
arthurnobrega Avatar asked Jul 24 '15 14:07

arthurnobrega


1 Answers

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)
like image 139
Fredy Janeta Bastidas Avatar answered Sep 27 '22 22:09

Fredy Janeta Bastidas