Skip to content

OpenSSL::PKCS12 error: PKCS12_parse: unsupported #611

Closed

Description

@marcosdemelo

I'm having trouble using the OpenSSL::PKCS12 class in Ruby 3.1.x onwards.

Some .p12 files still work, but the vast majority do not.

The error I am getting is: PKCS12_parse: unsupported (OpenSSL::PKCS12::PKCS12Error)

To simulate, I'm going to use a .pfx that I have to use as support for automated tests. It's a self-signed certificate that was generated a while ago. But it generates the same error to which I quoted. I don't know how to create a .p12 to simulate the problem, due to lack of knowledge. And I cannot expose a real .p12 here because it violates my users' privacy policy.
Hope this is enough to explain the error.

Reproducing steps

irb

require 'openssl'
file = File.binread('expired-certificate.pfx');
certificate = OpenSSL::PKCS12.new(file, '123456');
puts certificate.certificate.to_pem
puts certificate.key.to_pem

=> (irb):3:in `initialize': PKCS12_parse: unsupported (OpenSSL::PKCS12::PKCS12Error)

The expired-certificate.pfx file is inside the attached zip.

About the environment

❯ irb
irb(main):001:0> require 'openssl'
=> true
irb(main):002:0> OpenSSL::OPENSSL_LIBRARY_VERSION
=> "OpenSSL 3.0.7 1 Nov 2022"
irb(main):003:0> OpenSSL::OPENSSL_VERSION
=> "OpenSSL 3.0.7 1 Nov 2022"
irb(main):004:0> OpenSSL::VERSION
=> "3.0.1"
irb(main):005:0> RUBY_VERSION
=> "3.1.3"

Same problem in Ruby 3.2.1.

Any other information you need, just let me know.

Thank you in advance for all your help or information that may allow me to move forward in solving this problem.

expired-certificate.pfx.zip

Activity

  1. marcosdemelo commented on Apr 30, 2023

    @marcosdemelo
    Author

    On Ruby 3.2.2 the error persists...

    => (irb):3:in `initialize': PKCS12_parse: unsupported (OpenSSL::PKCS12::PKCS12Error)
  2. linked a pull request that will close this issueadd OpenSSL Provider support #635on Jun 13, 2023
  3. rhenium commented on Jun 13, 2023

    @rhenium
    Member
    $ openssl asn1parse -inform DER -offset 30 -in expired-certificate.pfx
        0:d=0  hl=4 l=1712 cons: SEQUENCE
        4:d=1  hl=4 l= 943 cons: SEQUENCE
        8:d=2  hl=2 l=   9 prim: OBJECT            :pkcs7-encryptedData
       19:d=2  hl=4 l= 928 cons: cont [ 0 ]
       23:d=3  hl=4 l= 924 cons: SEQUENCE
       27:d=4  hl=2 l=   1 prim: INTEGER           :00
       30:d=4  hl=4 l= 917 cons: SEQUENCE
       34:d=5  hl=2 l=   9 prim: OBJECT            :pkcs7-data
       45:d=5  hl=2 l=  28 cons: SEQUENCE
       47:d=6  hl=2 l=  10 prim: OBJECT            :pbeWithSHA1And40BitRC2-CBC
       59:d=6  hl=2 l=  14 cons: SEQUENCE
       61:d=7  hl=2 l=   8 prim: OCTET STRING      [HEX DUMP]:9EA1631619B43919
       71:d=7  hl=2 l=   2 prim: INTEGER           :0800
       75:d=5  hl=4 l= 872 prim: cont [ 0 ]
      951:d=1  hl=4 l= 761 cons: SEQUENCE
      955:d=2  hl=2 l=   9 prim: OBJECT            :pkcs7-data
      966:d=2  hl=4 l= 746 cons: cont [ 0 ]
    [...]
    

    :pbeWithSHA1And40BitRC2-CBC

    This is the problem (cf. #500). RC2 belongs to the legacy provider in OpenSSL >= 3.0, which is not enabled by default. openssl pkcs12 will also refuse to load this file without the -provider default -provider legacy options.

    #635 will allow loading providers explicitly with:

    OpenSSL::Provider.load("default")
    OpenSSL::Provider.load("legacy")
    
    certificate = OpenSSL::PKCS12.new(file, '123456')
    [...]
  4. dtonon commented on Jul 1, 2023

    @dtonon

    I have the same problem:

    rails c
    Loading development environment (Rails 7.0.4.2)
    irb(main):001:0> require 'openssl'
    => false
    irb(main):002:0> OpenSSL::OPENSSL_LIBRARY_VERSION
    => "OpenSSL 3.1.0 14 Mar 2023"
    irb(main):003:0> OpenSSL::OPENSSL_VERSION
    => "OpenSSL 3.1.0 14 Mar 2023"
    irb(main):004:0> OpenSSL::VERSION
    => "3.1.0"
    irb(main):005:0> RUBY_VERSION
    => "3.2.1"
    irb(main):007:0> key = OpenSSL::PKCS12::new( File.read("#{Rails.root}/config/xxxxxxxxx.p12"), '123456' ).key
    (irb):7:in `initialize': PKCS12_parse: unsupported (OpenSSL::PKCS12::PKCS12Error)
    irb(main):008:0> OpenSSL::Provider.load("default")
    (irb):8:in `<main>': uninitialized constant OpenSSL::Provider (NameError)

    I tried to vendorize the last github version too, same problem.
    Any help, please?

  5. abiagini commented on Jul 7, 2023

    @abiagini

    @dtonon is not released yet, the last realease was on december 22.
    You should go with the approach of #635

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions