#!/bin/bash
set -Eeuo pipefail
shopt -s inherit_errexit

# shellcheck source=scripts/libcc
. /usr/lib/common-criteria/scripts/libcc

###########################################
# FIPS mode (guide: "In the evaluated configuration FIPS mode MUST be
# enabled", verified with fips-mode-setup --check). FIPS mode is more than
# the kernel parameter: the fips pattern brings OpenSSL's FIPS provider and
# dracut's fips module, which checks the kernel's integrity at boot, and
# fips-mode-setup --enable installs the pattern before it adds fips=1 to the
# bootloader's kernel command line. fips=1 alone makes the kernel report
# FIPS mode, so the check tests each part: the pattern, the module in every
# initrd, fips=1 in the bootloader configuration, and on the running system
# the kernel's FIPS mode and OpenSSL's active FIPS provider.
# fips=1 takes effect only on the next boot, the same "configured now, active
# only after a reboot" shape as 50-config-crypto-hwaccel's clearcpuid=sha_ni.
# Every supported architecture boots the target kernel through GRUB: UEFI on
# x86_64/aarch64 (or BIOS on x86_64), PReP on ppc64le, and on s390x zipl
# loads grub2-s390x-emu, which reads the same /boot/grub2/grub.cfg.

GRUB_DEFAULT="/etc/default/grub"

# Does every initrd (not the kdump ones) contain dracut's fips module?
# False if there is no initrd.
cc52_initrd_fips() {
	local initrd
	local mods
	local found=0
	for initrd in /boot/initrd-*; do
		case "$initrd" in
		*-kdump) continue ;;
		esac
		[ -f "$initrd" ] || continue
		found=1
		mods=$(lsinitrd -m "$initrd" 2>/dev/null) || return 1
		grep -qx fips <<<"$mods" || return 1
	done
	[ "$found" = 1 ]
}

# Is OpenSSL's FIPS provider loaded and active?
cc52_openssl_fips_active() {
	openssl list -providers 2>/dev/null |
		awk '/^  [^ ]/ { p = $1 } p == "fips" && $1 == "status:" && $2 == "active" { f = 1 } END { exit !f }'
}

check_52_config_fips() {
	local arch
	arch=$(uname -m)
	case "$arch" in
	x86_64 | aarch64 | ppc64le | s390x)
		if ! rpm -q --whatprovides patterns-base-fips >/dev/null 2>&1; then
			echo "the fips pattern (patterns-base-fips) is not installed"
			return 1
		fi
		if ! cc52_initrd_fips; then
			echo "an initrd in /boot lacks dracut's fips module (dracut not run since dracut-fips was installed)"
			return 1
		fi
		if [ ! -f "$GRUB_DEFAULT" ]; then
			echo "$GRUB_DEFAULT missing"
			return 1
		fi
		if ! cc_grub_default_has "fips=1"; then
			echo "$GRUB_DEFAULT missing fips=1"
			return 1
		fi
		if ! cc_grub_cfg_has "fips=1"; then
			echo "not every grub.cfg has fips=1 (grub2-mkconfig not yet regenerated with it, or no grub.cfg found at all)"
			return 1
		fi
		if cc_in_chroot; then
			echo "configured, in effect once this system is booted"
			return 2
		fi
		if [[ " $(cat /proc/cmdline) " != *" fips=1 "* ]]; then
			echo "configured correctly, but the running kernel's cmdline does not have fips=1 yet - reboot required"
			return 2
		fi
		if [ "$(cat /proc/sys/crypto/fips_enabled 2>/dev/null)" != "1" ]; then
			echo "fips=1 is on the running kernel's cmdline, but /proc/sys/crypto/fips_enabled is not 1 - FIPS self-test may have failed"
			return 1
		fi
		# Without the openssl command (not part of a minimal system), the
		# pattern check above is all there is: it brings the provider.
		if command -v openssl >/dev/null 2>&1 && ! cc52_openssl_fips_active; then
			echo "the kernel runs in FIPS mode, but OpenSSL's FIPS provider is not active ('openssl list -providers')"
			return 1
		fi
		;;
	*)
		echo "FIPS mode enablement not implemented for $arch"
		return 1
		;;
	esac
}

apply_52_config_fips() {
	local arch
	local rc
	local out
	arch=$(uname -m)

	case "$arch" in
	x86_64 | aarch64 | ppc64le | s390x)
		if ! command -v fips-mode-setup >/dev/null 2>&1; then
			echo "FAILED: fips-mode-setup not found - is the 'fips' pattern/crypto-policies-scripts installed?"
			return 1
		fi

		rc=0
		check_52_config_fips >/dev/null 2>&1 || rc=$?
		if [ "$rc" -eq 0 ]; then
			echo "FIPS mode already active"
			return 0
		fi
		if [ "$rc" -eq 2 ]; then
			echo "FIPS mode already configured, reboot required before it's fully in effect"
			return 2
		fi

		# fips-mode-setup --enable interactively prompts with a 15-second
		# "press control-C to abort" warning unless this is set. It also
		# preserves an already FIPS-based crypto policy rather than
		# resetting it to plain "FIPS" (confirmed in its own source: it
		# only switches to plain FIPS when the current policy is not
		# already FIPS-based), so running this after
		# 51-config-crypto-policies has already set FIPS:OSPP:CC-STRICT
		# is safe regardless of dispatch order.
		FIPS_MODE_SETUP_SKIP_WARNING=1 fips-mode-setup --enable

		rc=0
		out=$(check_52_config_fips 2>&1) || rc=$?
		if [ "$rc" -eq 2 ]; then
			echo "fips=1 configured - reboot required before it's fully in effect"
			return 2
		fi
		if [ "$rc" -ne 0 ]; then
			echo "FAILED: FIPS mode not consistent after fips-mode-setup --enable: $out"
			return 1
		fi
		echo "FIPS mode active"
		;;
	*)
		echo "FIPS mode enablement not implemented for $arch"
		return 1
		;;
	esac
}
