Skip to content

OpenSSL::PKey::EC#to_pem or .new appears to change EC keys created by CRuby's openssl #257

Closed

Description

@postmodern

Steps To Reproduce

CRuby

require 'openssl'
ec = OpenSSL::PKey::EC.generate('prime256v1')
ec.generate_key
pem = ec.to_pem
# => "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIFGFA23QpFp3ZgFniwlICLAjIc3Zxrt6Dw5ElfETPkoxoAoGCCqGSM49\nAwEHoUQDQgAEfAWhRpiAKNuIlXG54M/vSWN1kam9/1SKp8yeoHBN0YhpGDrh+JC0\ngwPqy/cOYhzzKUleb4+bjvImBkTB/1jvlQ==\n-----END EC PRIVATE KEY-----\n"

JRuby

require 'openssl'
pem = "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIFGFA23QpFp3ZgFniwlICLAjIc3Zxrt6Dw5ElfETPkoxoAoGCCqGSM49\nAwEHoUQDQgAEfAWhRpiAKNuIlXG54M/vSWN1kam9/1SKp8yeoHBN0YhpGDrh+JC0\ngwPqy/cOYhzzKUleb4+bjvImBkTB/1jvlQ==\n-----END EC PRIVATE KEY-----\n"
ec = OpenSSL::PKey::EC.new(pem)
pem2 = ec.to_pem

Expected Result

# => "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIFGFA23QpFp3ZgFniwlICLAjIc3Zxrt6Dw5ElfETPkoxoAoGCCqGSM49\nAwEHoUQDQgAEfAWhRpiAKNuIlXG54M/vSWN1kam9/1SKp8yeoHBN0YhpGDrh+JC0\ngwPqy/cOYhzzKUleb4+bjvImBkTB/1jvlQ==\n-----END EC PRIVATE KEY-----\n"

Actual Result

# => "-----BEGIN EC PRIVATE KEY-----\nMCUCAQEEIFGFA23QpFp3ZgFniwlICLAjIc3Zxrt6Dw5ElfETPkox\n-----END EC PRIVATE KEY-----\n"

The PEM exported by jruby-openssl appears to be much shorter than the PEM exported by CRuby's openssl.

Versions

  • jruby 9.3.2.0 (2.6.8) 2021-12-01 0b8223f905 OpenJDK 64-Bit Server VM 11.0.15+10 on 11.0.15+10 +jit [linux-x86_64]
  • jruby-openssl (0.12.2 java, default: 0.11.0 java)

Activity

  1. kares commented on Apr 11, 2024

    @kares
    Member

    not sure about whether the format is expected to be "byte level" same between C-OpenSSL and Java's Bouncy-Castle...
    as long as one is able to parse other's PEM encoded format and end up with the same object.

    The PEM exported by jruby-openssl appears to be much shorter than the PEM exported by CRuby's openssl.

    was due 401749e which explicitly skipped the public part when encoding the private key

  2. jcharaoui commented on Jan 17, 2026

    @jcharaoui

    I think the main problem is that it's not possible to instantiate an OpenSSL::PKey::EC object from the PEM format it generates itself:

    irb(main):002:0> ec = OpenSSL::PKey::EC.generate("prime256v1")
    => #<OpenSSL::PKey::EC:0x21f7e537>
    irb(main):003:0> OpenSSL::PKey::EC.new(ec.to_pem)
    org/jruby/ext/openssl/PKeyEC.java:356:in `initialize': Neither PUB key nor PRIV key:  (OpenSSL::PKey::ECError)
            from org/jruby/RubyClass.java:923:in `new'
            from (irb):3:in `evaluate'
            from org/jruby/RubyKernel.java:1277:in `eval'
            from org/jruby/RubyKernel.java:1725:in `loop'
            from org/jruby/RubyKernel.java:1426:in `catch'
            from org/jruby/RubyKernel.java:1426:in `catch'
            from /usr/bin/jirb:13:in `<main>'
    

    This works without issues with MRI.

  3. linked a pull request that will close this issuecompatibility improvements #343on Mar 6, 2026
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