"openssl x509 -key" self-signs, costing a PIN entry or touch with a hardware token, and does not catch a key not matching the certificate. Compare public keys instead. Signed-off-by: David Seifert <so…@gentoo.org> --- eclass/kernel-build.eclass | 46 +++++--------------------------- eclass/secureboot.eclass | 54 ++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass index 2681be4781dd1..4400fcc9754b2 100644 --- a/eclass/kernel-build.eclass +++ b/eclass/kernel-build.eclass @@ -182,29 +182,10 @@ kernel-build_pkg_setup() { secureboot_pkg_setup if use modules-sign && [[ -n ${MODULES_SIGN_KEY} ]]; then - # Sanity check: fail early if key/cert in DER format or does not exist - local openssl_args=( - -noout -nocert - ) - if [[ -n ${MODULES_SIGN_CERT} ]]; then - openssl_args+=( -inform PEM -in "${MODULES_SIGN_CERT}" ) - else - # If no cert specified, we assume the pem key also contains the cert - openssl_args+=( -inform PEM -in "${MODULES_SIGN_KEY}" ) - fi - if [[ ${MODULES_SIGN_KEY} == pkcs11:* ]]; then - # OpenSSL 4 removed the ENGINE API - if has_version -b ">=dev-libs/openssl-4"; then - openssl_args+=( -provider default -provider pkcs11 -key "${MODULES_SIGN_KEY}" ) - else - openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MODULES_SIGN_KEY}" ) - fi - else - openssl_args+=( -keyform PEM -key "${MODULES_SIGN_KEY}" ) - fi - - openssl x509 "${openssl_args[@]}" || - die "Kernel module signing certificate or key not found or not PEM format." + # Sanity check: fail early if key/cert in DER format or does not exist. + # If no cert specified, we assume the pem key also contains the cert + _secureboot_check_keypair "${MODULES_SIGN_KEY}" "${MODULES_SIGN_CERT:-${MODULES_SIGN_KEY}}" || + die "Kernel module signing certificate or key not found, not PEM format, or not matching." if [[ ${MODULES_SIGN_KEY} != pkcs11:* ]]; then if [[ -n ${MODULES_SIGN_CERT} && ${MODULES_SIGN_CERT} != ${MODULES_SIGN_KEY} ]]; then @@ -234,23 +215,8 @@ kernel-build_pkg_setup() { fi # Sanity check: fail early if key/cert in DER format or does not exist - local openssl_args=( - -inform PEM -in "${MEASUREDBOOT_SIGN_CERT}" - -noout -nocert - ) - if [[ ${MEASUREDBOOT_SIGN_KEY} == pkcs11:* ]]; then - # OpenSSL 4 removed the ENGINE API - if has_version -b ">=dev-libs/openssl-4"; then - openssl_args+=( -provider default -provider pkcs11 -key "${MEASUREDBOOT_SIGN_KEY}" ) - else - openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MEASUREDBOOT_SIGN_KEY}" ) - fi - else - openssl_args+=( -keyform PEM -key "${MEASUREDBOOT_SIGN_KEY}" ) - fi - - openssl x509 "${openssl_args[@]}" || - die "Measured Boot signing certificate or key not found or not PEM format." + _secureboot_check_keypair "${MEASUREDBOOT_SIGN_KEY}" "${MEASUREDBOOT_SIGN_CERT}" || + die "Measured Boot signing certificate or key not found, not PEM format, or not matching." fi fi fi diff --git a/eclass/secureboot.eclass b/eclass/secureboot.eclass index a259ce362833f..91c3b4f99b4e5 100644 --- a/eclass/secureboot.eclass +++ b/eclass/secureboot.eclass @@ -91,6 +91,41 @@ _SECUREBOOT_ECLASS=1 inherit linux-info +# @FUNCTION: _secureboot_check_keypair +# @USAGE: <key> <cert> +# @INTERNAL +# @DESCRIPTION: +# Check that <key>, a PEM file or PKCS#11 URI, and the PEM certificate +# <cert> exist and belong together. Compares public keys instead of +# signing anything, which would cost a PIN or touch on a hardware token. +_secureboot_check_keypair() { + local key=${1} cert=${2} key_args=() key_pub cert_pub + + if [[ ${key} == pkcs11:* ]]; then + # OpenSSL 4 removed the ENGINE API + if has_version -b ">=dev-libs/openssl-4"; then + # Load default first: without a property query, OpenSSL uses + # the first registered implementation, and pkcs11-provider + # also offers digests if the PKCS#11 module does, which would + # route hashing through the module. + key_args=( -provider default -provider pkcs11 ) + else + key_args=( -engine pkcs11 -inform ENGINE ) + fi + else + # Without it, pkey also accepts DER, which signing tools reject + key_args=( -inform PEM ) + fi + + # Re-encode with the default provider only: pkcs11-provider exports + # PKCS#1 instead of SubjectPublicKeyInfo, and libp11's ENGINE may + # print diagnostics to stdout, which the PEM reader skips. + key_pub=$(openssl pkey "${key_args[@]}" -in "${key}" -pubout | + openssl pkey -pubin -pubout) || return 1 + cert_pub=$(openssl x509 -inform PEM -in "${cert}" -noout -pubkey) || return 1 + [[ ${key_pub} == "${cert_pub}" ]] +} + # @FUNCTION: secureboot_pkg_setup # @DESCRIPTION: # Checks if required user variables are set before starting the build @@ -197,23 +232,8 @@ secureboot_pkg_setup() { fi # Sanity check: fail early if key/cert in DER format or does not exist - local openssl_args=( - -inform PEM -in "${SECUREBOOT_SIGN_CERT}" - -noout -nocert - ) - if [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* ]]; then - # OpenSSL 4 removed the ENGINE API - if has_version -b ">=dev-libs/openssl-4"; then - openssl_args+=( -provider default -provider pkcs11 -key "${SECUREBOOT_SIGN_KEY}" ) - else - openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${SECUREBOOT_SIGN_KEY}" ) - fi - else - openssl_args+=( -keyform PEM -key "${SECUREBOOT_SIGN_KEY}" ) - fi - - openssl x509 "${openssl_args[@]}" || - die "Secure Boot signing certificate or key not found or not PEM format." + _secureboot_check_keypair "${SECUREBOOT_SIGN_KEY}" "${SECUREBOOT_SIGN_CERT}" || + die "Secure Boot signing certificate or key not found, not PEM format, or not matching." fi }
-- 2.56.1