cd5466
From 202ff1372a40a8adf9aac74bfe8a39141b0c57e5 Mon Sep 17 00:00:00 2001
cd5466
From: Kazuki Yamaguchi <k@rhe.jp>
cd5466
Date: Mon, 27 Sep 2021 00:38:38 +0900
cd5466
Subject: [PATCH] ext/openssl/extconf.rb: require OpenSSL version >= 1.0.1, < 3
cd5466
cd5466
Ruby/OpenSSL 2.1.x and 2.2.x will not support OpenSSL 3.0 API. Let's
cd5466
make extconf.rb explicitly check the version number to be within the
cd5466
acceptable range, since it will not compile anyway.
cd5466
cd5466
Reference: https://bugs.ruby-lang.org/issues/18192
cd5466
---
cd5466
 ext/openssl/extconf.rb | 43 ++++++++++++++++++++++++------------------
cd5466
 1 file changed, 25 insertions(+), 18 deletions(-)
cd5466
cd5466
diff --git a/ext/openssl/extconf.rb b/ext/openssl/extconf.rb
cd5466
index 264130bb..7e817ae2 100644
cd5466
--- a/ext/openssl/extconf.rb
cd5466
+++ b/ext/openssl/extconf.rb
cd5466
@@ -33,9 +33,6 @@
cd5466
   have_library("ws2_32")
cd5466
 end
cd5466
 
cd5466
-Logging::message "=== Checking for required stuff... ===\n"
cd5466
-result = pkg_config("openssl") && have_header("openssl/ssl.h")
cd5466
-
cd5466
 if $mingw
cd5466
   append_cflags '-D_FORTIFY_SOURCE=2'
cd5466
   append_ldflags '-fstack-protector'
cd5466
@@ -92,19 +89,33 @@ def find_openssl_library
cd5466
   return false
cd5466
 end
cd5466
 
cd5466
-unless result
cd5466
-  unless find_openssl_library
cd5466
-    Logging::message "=== Checking for required stuff failed. ===\n"
cd5466
-    Logging::message "Makefile wasn't created. Fix the errors above.\n"
cd5466
-    raise "OpenSSL library could not be found. You might want to use " \
cd5466
-      "--with-openssl-dir=<dir> option to specify the prefix where OpenSSL " \
cd5466
-      "is installed."
cd5466
-  end
cd5466
+Logging::message "=== Checking for required stuff... ===\n"
cd5466
+pkg_config_found = pkg_config("openssl") && have_header("openssl/ssl.h")
cd5466
+
cd5466
+if !pkg_config_found && !find_openssl_library
cd5466
+  Logging::message "=== Checking for required stuff failed. ===\n"
cd5466
+  Logging::message "Makefile wasn't created. Fix the errors above.\n"
cd5466
+  raise "OpenSSL library could not be found. You might want to use " \
cd5466
+    "--with-openssl-dir=<dir> option to specify the prefix where OpenSSL " \
cd5466
+    "is installed."
cd5466
 end
cd5466
 
cd5466
-unless checking_for("OpenSSL version is 1.0.1 or later") {
cd5466
-    try_static_assert("OPENSSL_VERSION_NUMBER >= 0x10001000L", "openssl/opensslv.h") }
cd5466
-  raise "OpenSSL >= 1.0.1 or LibreSSL is required"
cd5466
+version_ok = if have_macro("LIBRESSL_VERSION_NUMBER", "openssl/opensslv.h")
cd5466
+  is_libressl = true
cd5466
+  checking_for("LibreSSL version >= 2.5.0") {
cd5466
+    try_static_assert("LIBRESSL_VERSION_NUMBER >= 0x20500000L", "openssl/opensslv.h") }
cd5466
+else
cd5466
+  checking_for("OpenSSL version >= 1.0.1 and < 3.0.0") {
cd5466
+    try_static_assert("OPENSSL_VERSION_NUMBER >= 0x10001000L", "openssl/opensslv.h") &&
cd5466
+    !try_static_assert("OPENSSL_VERSION_MAJOR >= 3", "openssl/opensslv.h") }
cd5466
+end
cd5466
+unless version_ok
cd5466
+  raise "OpenSSL >= 1.0.1, < 3.0.0 or LibreSSL >= 2.5.0 is required"
cd5466
+end
cd5466
+
cd5466
+# Prevent wincrypt.h from being included, which defines conflicting macro with openssl/x509.h
cd5466
+if is_libressl && ($mswin || $mingw)
cd5466
+  $defs.push("-DNOCRYPT")
cd5466
 end
cd5466
 
cd5466
 Logging::message "=== Checking for OpenSSL features... ===\n"
cd5466
@@ -116,10 +127,6 @@ def find_openssl_library
cd5466
   have_func("ENGINE_load_#{name}()", "openssl/engine.h")
cd5466
 }
cd5466
 
cd5466
-if ($mswin || $mingw) && have_macro("LIBRESSL_VERSION_NUMBER", "openssl/opensslv.h")
cd5466
-  $defs.push("-DNOCRYPT")
cd5466
-end
cd5466
-
cd5466
 # added in 1.0.2
cd5466
 have_func("EC_curve_nist2nid")
cd5466
 have_func("X509_REVOKED_dup")