Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes "Neither PUB key nor PRIV key:: nested asn1 error" when building a public key in ruby?

When building a public key using the OpenSSL::PKey::RSA module by passing it a .pem file, what is the cause for a response:

OpenSSL::PKey::RSAError: Neither PUB key nor PRIV key:: nested asn1 error from /Users/Matt/projects/placepop/lib/apn.rb:48:in `initialize' from /Users/Matt/projects/placepop/lib/apn.rb:48:in `new' from /Users/Matt/projects/placepop/lib/apn.rb:48:in `open' from (irb):1 

Here is the source:

cert = File.join(rails_root, 'config', 'apns', 'sandbox-cert.pem') APN_CONFIG = { :delivery => {                                :host => 'gateway.sandbox.push.apple.com',                                :cert => cert,                               :passphrase => "",                               :port => 2195 },                :feedback => {                                 :host => 'feedback.sandbox.push.apple.com',                               :port => 2196,                               :passphrase => "",                               :cert => cert} }   options = APN_CONFIG[:delivery].merge(options) cert = File.read(options[:cert]) ctx = OpenSSL::SSL::SSLContext.new ctx.key = OpenSSL::PKey::RSA.new(cert, options[:passphrase]) ctx.cert = OpenSSL::X509::Certificate.new(cert)  sock = TCPSocket.new(options[:host], options[:port]) ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx) ssl.sync = true ssl.connect 
like image 410
Matthew Avatar asked Feb 19 '10 01:02

Matthew


2 Answers

I've got the same problem and it had a different cause. Now guess what :)

...

The damn password was wrong :( Searched 3 days for that "solution". Could have been a "Sorry dude, that's the wrong password!" instead of "nested asn1 error" imho but anyways, maybe this will help somebody.

like image 170
2called-chaos Avatar answered Oct 15 '22 13:10

2called-chaos


If you are using dotenv for instance, you have to surround the value with " and have \n for newlines.

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nMIICW  ... UcuUtU0eIl\n-----END RSA PRIVATE KEY-----" 
like image 45
Dorian Avatar answered Oct 15 '22 15:10

Dorian