openssl equivalent command in ruby
Asked Answered
T

1

1

I have to convert certificate file(pem format) into pfx using private key. The command perfectly works in linux is

openssl pkcs12 -export -out certificate1.pfx -inkey myPrivateKey.key -in myCert.pem

Can anyone help me to write equivalent code in ruby using ruby-openssl.

Transverse answered 16/5, 2013 at 14:9 Comment(2)
If you're only going to run on Linux, just run this command with system.Gareri
This command will be part of ruby api so it should not be system dependent.Transverse
P
6

Should be easy:

#!/usr/bin/env ruby
# export-der.rb

require 'openssl'

def export_der(pass, key, cert, out)
  key    = OpenSSL::PKey.read File.read(key)
  cert   = OpenSSL::X509::Certificate.new File.read(cert)
  name   = nil # not sure whether this is allowed
  pkcs12 = OpenSSL::PKCS12.create(pass, name, key, cert)
  File.open(out, 'w'){|f| f << pkcs12.to_der }
end

puts 'Password:'
export_der($stdin.read, *ARGV)

And call it this way (untested ;-)):

$ ruby export-der.rb myPrivateKey.key myCert.pem certificate1.pfx
Pentadactyl answered 16/5, 2013 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.