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