Skip to content

Commit

Permalink
[fix] have a useful OPENSSL_VERSION_NUMBER
Browse files Browse the repository at this point in the history
some gems such as ruby-jwt check this to detect 3.0.0
https://github.com/jwt/ruby-jwt/blob/v2.9.3/lib/jwt/version.rb#L27
  • Loading branch information
kares committed Oct 9, 2024
1 parent 011aeb7 commit 7767343
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/jruby/ext/openssl/OpenSSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,26 @@ public static void createOpenSSL(final Ruby runtime) {

runtime.getLoadService().require("jopenssl/version");

final byte[] version = { '2','.','2','.','3' };
final byte[] version = { '2','.','2','.','3' }; // C OpenSSL gem version

_OpenSSL.setConstant("VERSION", StringHelper.newString(runtime, version));

final RubyModule JOpenSSL = runtime.getModule("JOpenSSL");
final RubyString jVERSION = JOpenSSL.getConstantAt("VERSION").asString();

final byte[] JRuby_OpenSSL_ = { 'J','R','u','b','y','-','O','p','e','n','S','S','L',' ' };
final int OPENSSL_VERSION_NUMBER = 999999999; // NOTE: smt more useful?

ByteList OPENSSL_VERSION = new ByteList( jVERSION.getByteList().getRealSize() + JRuby_OpenSSL_.length );
OPENSSL_VERSION.setEncoding( jVERSION.getEncoding() );
OPENSSL_VERSION.append( JRuby_OpenSSL_ );
OPENSSL_VERSION.append( jVERSION.getByteList() );

// < 3.0.0 until we have decent (full) compatibility
final byte OPENSSL_VERSION_MAJOR = 2;
final byte OPENSSL_VERSION_MINOR = 9;
final byte OPENSSL_VERSION_PATCH = 9;
final byte OPENSSL_VERSION_PRE_RELEASE = 0;
final int OPENSSL_VERSION_NUMBER = OPENSSL_VERSION_MAJOR << 28 | OPENSSL_VERSION_MINOR << 20 | OPENSSL_VERSION_PATCH << 4 | OPENSSL_VERSION_PRE_RELEASE;

final RubyString VERSION;
_OpenSSL.setConstant("OPENSSL_VERSION", VERSION = runtime.newString(OPENSSL_VERSION));
_OpenSSL.setConstant("OPENSSL_VERSION_NUMBER", runtime.newFixnum(OPENSSL_VERSION_NUMBER));
Expand Down
5 changes: 5 additions & 0 deletions src/test/ruby/test_openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def test_version
assert /\AOpenSSL +0\./ !~ OpenSSL::OPENSSL_LIBRARY_VERSION
end

# some gems check this - better to be conservative until 3.0.0 APIs are fully supported
def test_version_lt_3_0_0
assert OpenSSL::OPENSSL_VERSION_NUMBER < 3 * 0x10000000
end

def test_debug
debug = OpenSSL.debug
assert (OpenSSL.debug == true || OpenSSL.debug == false)
Expand Down

0 comments on commit 7767343

Please sign in to comment.