Skip to content

Commit 096e683

Browse files
authored
Merge pull request #804 from mhashizume/PA-5865/master/fips-detection
Apply Ruby patch to enable FIPS with OpenSSL 3
2 parents 0ec2d4e + 1de4038 commit 096e683

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

configs/components/ruby-3.2.3.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
pkg.apply_patch "#{base}/revert_ruby_utf8_default_encoding.patch"
5656
end
5757

58+
if platform.is_fips?
59+
# This is needed on Ruby < 3.3 until the fix is backported (if ever)
60+
# See: https://bugs.ruby-lang.org/issues/20000
61+
pkg.apply_patch "#{base}/openssl3_fips.patch"
62+
end
63+
5864
####################
5965
# ENVIRONMENT, FLAGS
6066
####################
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
From 678d41bc51fe31834eec0b653ba0e47de5420aa0 Mon Sep 17 00:00:00 2001
2+
From: Jun Aruga <[email protected]>
3+
Date: Thu, 16 Mar 2023 21:36:43 +0100
4+
Subject: [PATCH] [ruby/openssl] Implement FIPS functions on OpenSSL 3.
5+
6+
This commit is to implement the `OpenSSL::OPENSSL_FIPS`, `ossl_fips_mode_get`
7+
and `ossl_fips_mode_set` to pass the test `test/openssl/test_fips.rb`.
8+
9+
It seems that the `OPENSSL_FIPS` macro is not used on the FIPS mode case any
10+
more, and some FIPS related APIs also were removed in OpenSSL 3.
11+
12+
See the document <https://github.com/openssl/openssl/blob/master/doc/man7/migration_guide.pod#removed-fips_mode-and-fips_mode_set>
13+
the section OPENSSL 3.0 > Main Changes from OpenSSL 1.1.1 >
14+
Other notable deprecations and changes - Removed FIPS_mode() and FIPS_mode_set() .
15+
16+
The `OpenSSL::OPENSSL_FIPS` returns always true in OpenSSL 3 because the used
17+
functions `EVP_default_properties_enable_fips` and `EVP_default_properties_is_fips_enabled`
18+
works with the OpenSSL installed without FIPS option.
19+
20+
The `TEST_RUBY_OPENSSL_FIPS_ENABLED` is set on the FIPS mode case on the CI.
21+
Because I want to test that the `OpenSSL.fips_mode` returns the `true` or
22+
'false' surely in the CI. You can test the FIPS mode case by setting
23+
`TEST_RUBY_OPENSSL_FIPS_ENABLED` on local too. Right now I don't find a better
24+
way to get the status of the FIPS mode enabled or disabled for this purpose. I
25+
am afraid of the possibility that the FIPS test case is unintentionally skipped.
26+
27+
I also replaced the ambiguous "returns" with "should return" in the tests.
28+
29+
https://github.com/ruby/openssl/commit/c5b2bc1268
30+
---
31+
ext/openssl/ossl.c | 25 +++++++++++++++++++++----
32+
test/openssl/test_fips.rb | 32 ++++++++++++++++++++++++++++----
33+
2 files changed, 49 insertions(+), 8 deletions(-)
34+
35+
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c
36+
index 71ddcb9f02..be97b97a1a 100644
37+
--- a/ext/openssl/ossl.c
38+
+++ b/ext/openssl/ossl.c
39+
@@ -418,7 +418,11 @@ static VALUE
40+
ossl_fips_mode_get(VALUE self)
41+
{
42+
43+
-#ifdef OPENSSL_FIPS
44+
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
45+
+ VALUE enabled;
46+
+ enabled = EVP_default_properties_is_fips_enabled(NULL) ? Qtrue : Qfalse;
47+
+ return enabled;
48+
+#elif OPENSSL_FIPS
49+
VALUE enabled;
50+
enabled = FIPS_mode() ? Qtrue : Qfalse;
51+
return enabled;
52+
@@ -442,8 +446,18 @@ ossl_fips_mode_get(VALUE self)
53+
static VALUE
54+
ossl_fips_mode_set(VALUE self, VALUE enabled)
55+
{
56+
-
57+
-#ifdef OPENSSL_FIPS
58+
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
59+
+ if (RTEST(enabled)) {
60+
+ if (!EVP_default_properties_enable_fips(NULL, 1)) {
61+
+ ossl_raise(eOSSLError, "Turning on FIPS mode failed");
62+
+ }
63+
+ } else {
64+
+ if (!EVP_default_properties_enable_fips(NULL, 0)) {
65+
+ ossl_raise(eOSSLError, "Turning off FIPS mode failed");
66+
+ }
67+
+ }
68+
+ return enabled;
69+
+#elif OPENSSL_FIPS
70+
if (RTEST(enabled)) {
71+
int mode = FIPS_mode();
72+
if(!mode && !FIPS_mode_set(1)) /* turning on twice leads to an error */
73+
@@ -1198,7 +1212,10 @@ Init_openssl(void)
74+
* Boolean indicating whether OpenSSL is FIPS-capable or not
75+
*/
76+
rb_define_const(mOSSL, "OPENSSL_FIPS",
77+
-#ifdef OPENSSL_FIPS
78+
+/* OpenSSL 3 is FIPS-capable even when it is installed without fips option */
79+
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
80+
+ Qtrue
81+
+#elif OPENSSL_FIPS
82+
Qtrue
83+
#else
84+
Qfalse

0 commit comments

Comments
 (0)