当使用OpenSSL::PKey::RSA模块通过传递.pem文件,响应的原因是什么:

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证书,然后是另一个从证书中提取公钥的函数.

Ruby-on-rails相关问答推荐

Rails turbo流只更新页面上的一个元素

在 Rails 7 中,可以将导入映射与 js Bundle 解决方案一起使用,还是它们是专有的?

在单个继承表和另一个表中的类之间建立多对多关联

Rails - 如何使用子对象的嵌套属性和强参数填充父对象ID?

Rails:使用 RSpec 测试命名范围

简单表单关联自定义标签名称

不带参数的 request.fullpath

奇怪的406 不可接受错误

Ruby on Rails:在模型中验证还是在数据库中验证更好?

在本地 Rails 开发环境中获取真实 IP 地址

批量查找 mongoDB 记录(使用 mongoid ruby​​ 适配器)

在rails国际化yml文件中传递变量

我怎样才能看到水豚在失败的黄瓜步骤中发现了什么?

rails 获取应用程序根/基本 url

rails respond_to format.js API

如何翻译 ActiveRecord 模型类名称?

如何使用 url 连接到 postgresql

按照给定 ID 数组的顺序按 ID 查找模型记录

简单的 rails rake 任务拒绝运行并出现错误不知道如何构建任务,为什么?

在 Ruby on Rails 中设置日志(log)记录级别