Skip to content

Commit 898e9df

Browse files
committed
gh-163154: Run SSL tests on FIPS mode on the CI
Assisted-By: Claude Sonnet 5.5
1 parent 5b6dd1e commit 898e9df

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ jobs:
268268
- { name: openssl, version: 1.1.1w }
269269
- { name: openssl, version: 3.5.10 }
270270
- { name: openssl, version: 4.0.4 }
271+
- { name: openssl, version: 3.5.10, fips: true }
272+
- { name: openssl, version: 4.0.4, fips: true }
271273
## AWS-LC
272274
- { name: aws-lc, version: 5.15.0 }
273275
env:
@@ -316,6 +318,18 @@ jobs:
316318
run: |
317319
./python -c 'import ssl; print(ssl.OPENSSL_VERSION)' \
318320
| grep -iE '${{ matrix.ssllib.name }}.*${{ matrix.ssllib.version }}'
321+
- name: Enable FIPS mode for the tests
322+
if: ${{ matrix.ssllib.fips }}
323+
run: |
324+
python3 Tools/ssl/multissltests.py \
325+
--steps=fips \
326+
--base-directory "$MULTISSL_DIR" \
327+
'--${{ matrix.ssllib.name }}' '${{ matrix.ssllib.version }}' \
328+
--system Linux \
329+
>> "$GITHUB_ENV"
330+
- name: Verify FIPS mode is enabled
331+
if: ${{ matrix.ssllib.fips }}
332+
run: ./python -c 'import _hashlib; assert _hashlib.get_fips_mode()'
319333
- name: SSL tests
320334
run: ./python Lib/test/ssltests.py
321335

‎Tools/ssl/multissltests.py‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import subprocess
3030
import sys
3131
import tarfile
32+
from textwrap import dedent
3233
from urllib.error import HTTPError
3334
from urllib.request import urlopen
3435

@@ -133,12 +134,14 @@
133134
)
134135
parser.add_argument(
135136
'--steps',
136-
choices=['library', 'modules', 'tests'],
137+
choices=['library', 'modules', 'tests', 'fips'],
137138
default='tests',
138139
help=(
139140
"Which steps to perform. 'library' downloads and compiles a crypto"
140141
"library. 'module' also compiles Python modules. 'tests' builds "
141-
"all and runs the test suite."
142+
"all and runs the test suite. 'fips' only prints the environment "
143+
"variables (KEY=value lines) that enable the OpenSSL 3.x+ FIPS "
144+
"provider for the given library."
142145
)
143146
)
144147
parser.add_argument(
@@ -397,6 +400,9 @@ def check_pyssl(self):
397400
if self.version not in version:
398401
raise ValueError(version)
399402

403+
def fips_env(self):
404+
raise ValueError(f"FIPS is not supported for {self}")
405+
400406
def run_python_tests(self, tests, network=True):
401407
if not tests:
402408
cmd = [
@@ -452,6 +458,57 @@ def _post_install_3xx(self):
452458
def _post_install_4xx(self):
453459
self._post_install_3xx()
454460

461+
def fips_env(self):
462+
"""Create an OpenSSL config that activates the FIPS provider
463+
and return environment variables to be appended to $GITHUB_ENV"""
464+
if not self.version.startswith(("3.", "4.")):
465+
return super().fips_env()
466+
modules_dir = os.path.join(self.lib_dir, "ossl-modules")
467+
fips_module = os.path.join(modules_dir, "fips.so")
468+
ssl_dir = os.path.join(self.install_dir, "ssl")
469+
fipsmodule_cnf = os.path.join(ssl_dir, "fipsmodule.cnf")
470+
fips_cnf = os.path.join(ssl_dir, "openssl-fips.cnf")
471+
env = os.environ.copy()
472+
env["LD_LIBRARY_PATH"] = self.lib_dir
473+
self._subprocess_call(
474+
[
475+
self.openssl_cli, "fipsinstall",
476+
"-out", fipsmodule_cnf,
477+
"-module", fips_module,
478+
],
479+
env=env,
480+
stdout=sys.stderr, # stdout is reserved for KEY=value output
481+
)
482+
with open(fips_cnf, "w", encoding="utf-8") as f:
483+
f.write(dedent(f"""\
484+
config_diagnostics = 1
485+
openssl_conf = openssl_init
486+
487+
.include {fipsmodule_cnf}
488+
489+
[openssl_init]
490+
providers = provider_sect
491+
alg_section = algorithm_sect
492+
493+
[provider_sect]
494+
fips = fips_sect
495+
base = base_sect
496+
default = default_sect
497+
498+
[base_sect]
499+
activate = 1
500+
501+
[default_sect]
502+
activate = 1
503+
504+
[algorithm_sect]
505+
default_properties = fips=yes
506+
"""))
507+
return {
508+
"OPENSSL_CONF": fips_cnf,
509+
"OPENSSL_MODULES": modules_dir,
510+
}
511+
455512
@property
456513
def short_version(self):
457514
"""Short version for OpenSSL download URL"""
@@ -556,6 +613,14 @@ def main():
556613
build.install()
557614
builds.append(build)
558615

616+
if args.steps == 'fips':
617+
# stdout is meant to be appended to $GITHUB_ENV
618+
if len(builds) != 1:
619+
parser.error("--steps=fips needs exactly one library version")
620+
for key, value in builds[0].fips_env().items():
621+
print(f"{key}={value}")
622+
return
623+
559624
if args.steps in {'modules', 'tests'}:
560625
for build in builds:
561626
try:

0 commit comments

Comments
 (0)