Article View: linux.gentoo.dev
Article #235[gentoo-dev] [PATCH 4/6] secureboot.eclass, kernel-build.eclass: check key/cert without signing
From: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
148 lines
7071 bytes
7071 bytes
"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 <soap@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
Technical Details
Message-ID:
<O5yqR-fmyc-1@gated-at.bofh.it>Path:
mbox-mo1.pugleaf.net!news-ams.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomodReferences:
<O5yqR-fmyc-3@gated-at.bofh.it>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-ams.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod From: David Seifert <soap@gentoo.org> Newsgroups: linux.gentoo.dev Subject: [gentoo-dev] [PATCH 4/6] secureboot.eclass, kernel-build.eclass: check key/cert without signing Date: Tue, 01 Dec 2026 17:00:01 +0100 Message-ID: <O5yqR-fmyc-1@gated-at.bofh.it> References: <O5yqR-fmyc-3@gated-at.bofh.it> X-Mailer: git-send-email 2.56.1 List-ID: Gentoo Linux mail <gentoo-dev.gentoo.org> Reply-To: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Approved: robomod@news.nic.it Lines: 148 Organization: linux.* mail to news gateway Sender: robomod@news.nic.it X-Original-Cc: David Seifert <soap@gentoo.org> X-Original-Date: Tue, 1 Dec 2026 16:49:22 +0100 X-Original-Message-ID: <20261201154924.70252-4-soap@gentoo.org> X-Original-References: <20261201154924.70252-1-soap@gentoo.org>