|
29 | 29 | import subprocess |
30 | 30 | import sys |
31 | 31 | import tarfile |
| 32 | +from textwrap import dedent |
32 | 33 | from urllib.error import HTTPError |
33 | 34 | from urllib.request import urlopen |
34 | 35 |
|
|
133 | 134 | ) |
134 | 135 | parser.add_argument( |
135 | 136 | '--steps', |
136 | | - choices=['library', 'modules', 'tests'], |
| 137 | + choices=['library', 'modules', 'tests', 'fips'], |
137 | 138 | default='tests', |
138 | 139 | help=( |
139 | 140 | "Which steps to perform. 'library' downloads and compiles a crypto" |
140 | 141 | "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." |
142 | 145 | ) |
143 | 146 | ) |
144 | 147 | parser.add_argument( |
@@ -397,6 +400,9 @@ def check_pyssl(self): |
397 | 400 | if self.version not in version: |
398 | 401 | raise ValueError(version) |
399 | 402 |
|
| 403 | + def fips_env(self): |
| 404 | + raise ValueError(f"FIPS is not supported for {self}") |
| 405 | + |
400 | 406 | def run_python_tests(self, tests, network=True): |
401 | 407 | if not tests: |
402 | 408 | cmd = [ |
@@ -452,6 +458,57 @@ def _post_install_3xx(self): |
452 | 458 | def _post_install_4xx(self): |
453 | 459 | self._post_install_3xx() |
454 | 460 |
|
| 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 | + |
455 | 512 | @property |
456 | 513 | def short_version(self): |
457 | 514 | """Short version for OpenSSL download URL""" |
@@ -556,6 +613,14 @@ def main(): |
556 | 613 | build.install() |
557 | 614 | builds.append(build) |
558 | 615 |
|
| 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 | + |
559 | 624 | if args.steps in {'modules', 'tests'}: |
560 | 625 | for build in builds: |
561 | 626 | try: |
|
0 commit comments