是什么导致“PUB key 和 PRIV key::nested asn1 错误"?在 ruby​​ 中构建公钥时? [英] What causes "Neither PUB key nor PRIV key:: nested asn1 error" when building a public key in ruby?

查看:41
本文介绍了是什么导致“PUB key 和 PRIV key::nested asn1 错误"?在 ruby​​ 中构建公钥时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 OpenSSL::PKey::RSA 模块通过传递一个 .pem 文件来构建公钥时,响应的原因是什么:

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

这里是来源:

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

推荐答案

pem 文件不是公钥,它是 base64 编码的 X509 证书,在其众多字段中包含一个公钥.我不知道 Ruby 或 OpenSSL ruby​​ 模块,但我会寻找一些读取 PEM 文件并输出 X509 证书的函数,然后是另一个从证书中提取公钥的函数.

A pem file is not a public key, it is a base64-encoded X509 certificate that contains, among its many fields, a public key. I don't know Ruby, or the OpenSSL ruby module, but I would look for some function that reads in PEM files and outputs an X509 certificate, then another function to extract the public key from the certificate.

这篇关于是什么导致“PUB key 和 PRIV key::nested asn1 错误"?在 ruby​​ 中构建公钥时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆