在Ruby上获取OpenSSL :: X509 :: CertificateError嵌套asn1错误

我有Apple的.p12文件,并尝试使用以下命令将其转换为.pem文件:

openssl pkcs12 -in cert.p12 -out apple_push_notification_development.pem -nodes -clcerts 

尝试使用时创建新的OpenSSL :: X509 :: Certificate对象

 OpenSSL::X509::Certificate.new(File.read('apple_push_notification_development.pem')) 

我收到以下错误:

 OpenSSL::X509::CertificateError: nested asn1 error from (irb):9:in `initialize' from (irb):9:in `new' ... 

我做错什么了吗 ? 卡住了,请帮忙。 谢谢

感谢它不是你完全相同的场景,但我试图在我的实例中读取PEM文件(PKCS7)。 OpenSSL CLI可以很好地解码它,但是当我试图将它加载到对象中时,ruby会继续抛出你描述的嵌套asn1错误。

在我的情况下,它需要一个新的行,即PEM文件末尾的’\ n’,以便它接受它。

我只在创建一个空对象并将生成的PEM输出与我试图加载的文件进行比较时才进行处理。

所以使用X509证书可能会尝试:

 cert = OpenSSL::X509::Certificate.new cert.to_pem => "-----BEGIN CERTIFICATE-----\nMCUwGwIAMAMGAQAwADAEHwAfADAAMAgwAwYBAAMBADADBgEAAwEA\n-----END CERTIFICATE-----\n" 

并将其与您的PEM文件进行比较

正如您所看到的那样,它终止了一个新行,并且我试图导入的文件中缺少该行。

我遇到了同样的问题,我的情况是我需要使用Base64解码文件内容。

 require 'openssl' require 'base64' encoded_content = File.read('apple_push_notification_development.pem') decoded_content = Base64.decode64(encoded_content) certificate = OpenSSL::X509::Certificate.new(decoded_content) 

当您忘记签署新生成的证书时,也可能发生这种情况。 我想使用自签名证书,但忘了签名部分。

 # Create key key = OpenSSL::PKey::RSA.new(2048) open("key.pem", "w") do |io| io.write(key.to_pem) end # Generate certificate name = OpenSSL::X509::Name.parse("CN=example.com/C=EE") cert = OpenSSL::X509::Certificate.new cert.version = 2 cert.serial = 0 cert.not_before = Time.now cert.not_after = cert.not_before + 1 * 365 * 24 * 60 * 60 # 1 year validity cert.public_key = key.public_key cert.subject = name 

这部分代码是我错过的:

 cert.issuer = name cert.sign key, OpenSSL::Digest::SHA1.new open "cert.pem", 'w' do |io| io.write cert.to_pem end