Ruby Error reading in Certificate File with OpenSSL
"testuser.p12" seems to be a PKCS#12 file according to the postfix. Reading PKCS#12 format as X.509 certificate format causes ASN.1 decoding error.
You should do OpenSSL::PKCS12.new(File.read("testuser.p12"))
instead. If the file is protected with passphrase (it's normal), give the passphrase as the second parameter for PKCS12.new like OpenSSL::PKCS12.new(File.read("testuser.p12"), "pass")
You can extract certificate and CA certificates by PKCS12#certificate
and PKCS12#ca_certs
methods.
p12 = OpenSSL::PKCS12.new(File.read("testuser.p12"), "pass")
p p12.certificate
p p12.ca_certs
How can I fix these OpenSSL and Rails certificate verify failed errors?
http://railsapps.github.io/openssl-certificate-verify-failed.html
Details how to fix:
openssl::ssl::sslerror: ssl_connect returned=1 errno=0 state=sslv3 read server certificate b: certificate verify failed
or
could not load openssl. you must recompile ruby with openssl support or change the sources in your gemfile from 'https' to 'http'. instructions for compiling with openssl using rvm are available at rvm.io/packages/openssl.
Certificate verify failed OpenSSL error when using Ruby 1.9.3
There are lots of moving parts involved in the correct answer. Depends on your OS, Ruby version, OpenSSL version, Rubygems version. I ended up writing an article after researching it. My article explains the reasons for the error, offers steps for further diagnosis, shows several workarounds, and suggests possible solutions. This will be helpful:
OpenSSL Errors and Rails – Certificate Verify Failed
There are also links to the relevant commits and issues on GitHub.
Rails can't read certificate information from environment due to nested asn1 error
I finally found a way to do it... mixing it all up!
So the file, for example company.key looks like
-----BEGIN PRIVATE RSA KEY ----
Mumbojumbomummbojumbo
-----END RSA PRIVATE KEY----
So I switched it to a one liner, making explicit \n in the string (so its a real \n)
COMPANY_KEY=""-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA+ztKEj\n-----END RSA PRIVATE KEY-----\n"
Don't forget the last \n in the file.
Now, the last part, in the place where I used to do
@private_key = OpenSSL::PKey::RSA.new(File.read(private_key_file))
Now I do
@private_key = OpenSSL::PKey::RSA.new(ENV['COMPANY_KEY'].gsub("\\n", "\n"))
And now works like a charm! No public certificates, every piece of info in environment variables.
Related Topics
Ruby/Rails Array of Strings to Postgresql Insert
How to Figure Out Which Step I'Ve Just Executed in Cucumber's Afterstep Hook
Ruby: Any Gems for Threadpooling
The Program 'Rails' Is Currently Not Installed
How to Parse Xml Nodes to CSV with Ruby and Nokogiri
Ssl_Connect Returned=1 Errno=0 State=Sslv3 Read Server Certificate B: Certificate Verify Failed MAC
Ruby Is Already Using the Class Name of My Model
Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass
Ruby Error After Installing Heroku Toolbelt
Rails Cancan and State MAChine - Authorizing States
How to Include Ё in [А-Я] Regexp Char Interval
Scoping Date Attribute for This Week
Insert Rows on Specific Line in a File
Building a Simple Search Form in Rails
Parsing Xls Spreadsheet in Rails Using Roo Gem
Project Euler #3 in Ruby Solution Times Out