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

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

CC_STRICT_MODULE="/etc/crypto-policies/policies/modules/CC-STRICT.pmod"
# Rewritten by every update-crypto-policies --set.
CRYPTO_POLICY_STATE="/etc/crypto-policies/state/current"

# The policy's name in /etc/crypto-policies/config is only the
# configuration; programs read the back-end files update-crypto-policies
# --set generates from it. check therefore also requires that the
# configuration was applied (--is-applied), that the back-ends match what it
# generates (--check, which a hand-edited back-end fails), and that nothing
# changes the policy under the same name: a local.d file appended to a
# back-end, a FIPS.pol or OSPP.pmod in /etc/crypto-policies/policies (read
# before /usr/share/crypto-policies/policies), or an edited CC-STRICT.pmod.
check_51_config_crypto_policies() {
	local active changed booted f verify
	active=$(update-crypto-policies --show)
	if [ "$active" != "FIPS:OSPP:CC-STRICT" ]; then
		echo "active crypto policy is '$active', expected FIPS:OSPP:CC-STRICT"
		return 1
	fi
	if ! update-crypto-policies --is-applied >/dev/null 2>&1; then
		echo "crypto policy FIPS:OSPP:CC-STRICT is configured, but not applied (update-crypto-policies --set not run since)"
		return 1
	fi
	if ! update-crypto-policies --check >/dev/null 2>&1; then
		echo "the crypto policy back-end files differ from what FIPS:OSPP:CC-STRICT generates ('update-crypto-policies --check')"
		return 1
	fi
	for f in /etc/crypto-policies/local.d/*.config /etc/crypto-policies/policies/FIPS.pol /etc/crypto-policies/policies/modules/OSPP.pmod; do
		if [ -e "$f" ]; then
			echo "$f changes the FIPS:OSPP:CC-STRICT policy"
			return 1
		fi
	done
	# rpm -V exits non-zero for any difference in the package, so its exit
	# status says nothing about this file.
	verify=$(rpm -V --nomtime certification-sles-eal4 2>/dev/null) || true
	if grep -qF " $CC_STRICT_MODULE" <<<"$verify"; then
		echo "$CC_STRICT_MODULE differs from the one this package ships"
		return 1
	fi
	if cc_in_chroot; then
		echo "configured, in effect once this system is booted"
		return 2
	fi
	# Programs read the policy when they start, so a policy set since the
	# last boot isn't in effect for anything that was already running.
	changed=$(stat -c %Y "$CRYPTO_POLICY_STATE") || changed=0
	booted=$(awk '$1 == "btime" { print $2 }' /proc/stat)
	if [ "$changed" -ge "$booted" ]; then
		echo "crypto policy set to FIPS:OSPP:CC-STRICT since the last boot - reboot required before running programs use it"
		return 2
	fi
}

apply_51_config_crypto_policies() {
	[ -f "$CC_STRICT_MODULE" ] || {
		echo "FAILED: $CC_STRICT_MODULE missing - is this package installed correctly?"
		return 1
	}

	local rc out
	rc=0
	check_51_config_crypto_policies >/dev/null 2>&1 || rc=$?
	if [ "$rc" -eq 0 ]; then
		echo "Crypto policy already set to FIPS:OSPP:CC-STRICT"
		return 0
	fi
	if [ "$rc" -eq 2 ]; then
		echo "Crypto policy already set to FIPS:OSPP:CC-STRICT, reboot required before it's fully in effect"
		return 2
	fi

	update-crypto-policies --set FIPS:OSPP:CC-STRICT

	rc=0
	out=$(check_51_config_crypto_policies 2>&1) || rc=$?
	if [ "$rc" -eq 2 ]; then
		echo "Crypto policy set to FIPS:OSPP:CC-STRICT - reboot required before it's fully in effect"
		return 2
	fi
	if [ "$rc" -ne 0 ]; then
		echo "FAILED: crypto policy not in place after applying: $out"
		return 1
	fi
	echo "Crypto policy set to FIPS:OSPP:CC-STRICT"
}
