From 05cfcf3f03c320555a96bbec70a038a6c8d2e29d Mon Sep 17 00:00:00 2001 From: Jun Aruga Date: Mon, 14 Aug 2023 16:02:42 +0200 Subject: [PATCH] Enhance printing OpenSSL versions. * Updated the `OpenSSL::OPENSSL_VERSION_NUMBER` comment explaining the format. * Added the `OpenSSL::LIBRESSL_VERSION_NUMBER` to print LibreSSL version number, in the case that Ruby OpenSSL binding is compiled with LibreSSL. Note `test/openssl/utils.rb#libressl?` is not using this value in it for now. * Update `rake debug` to print the values in a readable way, adding `OpenSSL::OPENSSL_VERSION_NUMBER` and `OpenSSL::LIBRESSL_VERSION_NUMBER`. * Use `Rake.rake_output_message` instead of `puts` to print the message in a proper order, as well as the case of the `debug_compiler` task in the `Rakefile`. --- Rakefile | 12 +++++++++++- ext/openssl/ossl.c | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index ab708bd22..f45d1ecec 100644 --- a/Rakefile +++ b/Rakefile @@ -38,7 +38,17 @@ task :debug_compiler do end task :debug do - ruby "-I./lib -ropenssl -ve'puts OpenSSL::OPENSSL_VERSION, OpenSSL::OPENSSL_LIBRARY_VERSION'" + ruby_code = <<~'EOF' + libre_version_number_str = (defined? OpenSSL::LIBRESSL_VERSION_NUMBER) ? + OpenSSL::LIBRESSL_VERSION_NUMBER.to_s(16) : "undefined" + Rake.rake_output_message <<~MESSAGE + OpenSSL::OPENSSL_VERSION: #{OpenSSL::OPENSSL_VERSION} + OpenSSL::OPENSSL_LIBRARY_VERSION: #{OpenSSL::OPENSSL_LIBRARY_VERSION} + OpenSSL::OPENSSL_VERSION_NUMBER: #{OpenSSL::OPENSSL_VERSION_NUMBER.to_s(16)} + OpenSSL::LIBRESSL_VERSION_NUMBER: #{libre_version_number_str} + MESSAGE + EOF + ruby %Q(-I./lib -ropenssl -rrake -ve'#{ruby_code}') end task :default => :test diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c index e67832d46..e21d13722 100644 --- a/ext/openssl/ossl.c +++ b/ext/openssl/ossl.c @@ -1149,10 +1149,27 @@ Init_openssl(void) /* * Version number of OpenSSL the ruby OpenSSL extension was built with - * (base 16) + * (base 16). The formats are below. + * + * [OpenSSL 3] 0xMNN00PP0 (major minor 00 patch 0) + * [OpenSSL before 3] 0xMNNFFPPS (major minor fix patch status) + * [LibreSSL] 0x20000000 (fixed value) + * + * See also the man page OPENSSL_VERSION_NUMBER(3). */ rb_define_const(mOSSL, "OPENSSL_VERSION_NUMBER", INT2NUM(OPENSSL_VERSION_NUMBER)); +#if defined(LIBRESSL_VERSION_NUMBER) + /* + * Version number of LibreSSL the ruby OpenSSL extension was built with + * (base 16). The format is 0xMNNFFPPS (major minor fix patch + * status). This constant is only defined in LibreSSL cases. + * + * See also the man page OPENSSL_VERSION_NUMBER(3). + */ + rb_define_const(mOSSL, "LIBRESSL_VERSION_NUMBER", INT2NUM(LIBRESSL_VERSION_NUMBER)); +#endif + /* * Boolean indicating whether OpenSSL is FIPS-capable or not */