Micheal
Micheal

Reputation: 2322

ruby encode a p12 certificate(binary) and send as a json response

I have a pem certificate with a private key. I am using the above information to generate a p12 certificate which is password protected as follows:

  def p12_cert
    ca_cert = x509_cert(File.open("#{root}/ca-cert.crt").read)
    p12 = OpenSSL::PKCS12.create(@random_pass, 'My Certificate',
          rsa_pkey(private_key), x509_cert(public_cert), [ca_cert])
    create_file('p12', p12.to_der, ':ASCII-8BIT')
  end

The issue is this is in binary format and cannot be transmitted via a json API.

Can someone how me how to encode it(maybe base64) so that this can be sent as a JSON response?

EDIT: I opened the p12 file for read and then tried to base64 encode, got the following:

irb(main):017:0> enc_p12 = Base64.encode64(p12) TypeError: no implicit conversion of OpenSSL::PKCS12 into String

Upvotes: 1

Views: 928

Answers (1)

Marc Rohloff
Marc Rohloff

Reputation: 1352

You don't usually encode the PKCS12 object itself but the raw file. Something like Base64.encode64( File.read(filename, mode: 'rb')

Upvotes: 3

Related Questions