fit: allow signing with an OpenSSL engine
v2 cover fit: allow signing with an OpenSSL engine
I have a couple of products whose U-Boot FIT is signed via a proprietary OpenSSL engine which only expects the name of a "slot" to select the key to sign data with.
Currently mkimage fit support expects either a key-dir (-k) or a key-file (-G) as a toggle for signing, however this doesn't apply to our usecase because we use an OpenSSL engine (so no key-file to provide) which doesn't mimic a directory layout like key-dir implies. Moreover, binman really expects private keys (.key extension) to be available in this key-dir directory, which we of course cannot provide.
This series allows to sign a FIT image with mkimage (and binman) with an OpenSSL engine, including PKCS11 and custom engines. If a key-dir needs to be passed (which is typical for PKCS11), one can do so by using fit,engine-keydir.
Note that the public key (.crt extension) still needs to be available if one wants to embed it for signature verification (which is probably what one wants to do :) ). It is probably possible to use the engine for getting the public key instead of storing it on disk, but this needs to be added to fdt_add_pubkey and then binman, through a mechanism different from fit,engine*.
One issue though is that since binman resolves key paths absolutely and that I don't believe an OpenSSL engine would happen to have the exact same key_id value than a local absolute path, fit,encrypt and fit,engine cannot cohabit. An issue for the next person who wants an OpenSSL engine AND encrypt the same FIT image, I don't.
Note that LibreSSL supports neither engines nor providers as far as I could tell (engine support has been explicitly removed).
Note that OpenSSL engines have been deprecated since 3.0 (Q3-2021), however note that OpenSSL 3.5 still seems to support engines (git grep) and is EOL end of Q1 2030.
I am not entirely sure if the setup for the PKCS11 tests package requirements is implemented the proper way (the double try:catch).
+Cc Eddie who's working on OpenSSL provider support, maybe we can work together on supporting both engines and providers via the same API we expose to the user?
+Cc Wolfgang who seems interested in signing with PKCS11 engine which should now be supported.
+Cc Peter who seems interested according to v1
@Wolfgang, key-name-hint will only be a hint to use when verifying (as well as being used for the node name), but if you have a fit,engine-keydir which contains object= or id= in it, key-name-hint won't be passed to your engine.
---
Changes in v2:
- added R-b on patch 1,
- did NOT add T-b and R-B on patch 3 since there's been some rework,
- added binman test for signing with dummy RSA engine,
- added binman test for signing with SoftHSMv2 PKCS11 engine,
- added binman test for signing with SoftHSMv2 PKCS11 engine and
specific keydir (which contains an object= to tackle that special
case)
- added dummy RSA engine for binman test,
- renamed property to fit,engine as my hunch is that it could be used
for encrypting as well (from the same engine with the same keydir but likely a different key-name-hint so that a different keypair is used)
- added fit,engine-keydir to easily support PKCS11 with arguments other than object= taken from key-name-hint,
- Link to v1: https://patch.msgid.link/[email protected]
---
Quentin Schulz (4):
fit: support signing with only an engine_id
tools: binman: mkimage: add support for passing the engine
tools: binman: fit: add support for OpenSSL engines
tools: binman: fit: add tests for signing with an OpenSSL engine
tools/binman/btool/mkimage.py | 5 +- tools/binman/btool/softhsm2_util.py | 21 +++ tools/binman/entries.rst | 45 ++++- tools/binman/etype/fit.py | 61 ++++++- tools/binman/ftest.py | 193 +++++++++++++++++++++ tools/binman/test/340_dummy-rsa4096.crt | 31 ++++ tools/binman/test/340_fit_signature_engine.dts | 99 +++++++++++ .../test/340_fit_signature_engine_pkcs11.dts | 99 +++++++++++ .../340_fit_signature_engine_pkcs11_object.dts | 100 +++++++++++ tools/binman/test/340_openssl.conf | 10 ++ tools/binman/test/340_softhsm2.conf | 16 ++ tools/binman/test/Makefile | 6 +- tools/binman/test/dummy-rsa-engine.c | 149 ++++++++++++++++ tools/fit_image.c | 3 +- tools/image-host.c | 4 +- 15 files changed, 830 insertions(+), 12 deletions(-)
---
base-commit: 089d92860e1ba63c52fba9b2ac8aa42bf03ebc03
change-id: 20251030-binman-engine-e349b02696d0
Best regards,
Signature · 1 line
Quentin Schulz <[email protected]>
PATCH v2 1/4 fit: support signing with only an engine_id
From: Quentin Schulz <[email protected]>
Currently, when one wants to use an OpenSSL engine to sign a FIT image, one needs to pass a keydir (via -k) to mkimage which will then be prepended to the value of the key-name-hint before being passed as key_id argument to the OpenSSL Engine API, or pass a keyfile (via -G) to mkimage.
My OpenSSL engine only has "slots" which are not mapped like directories, so using keydir is not proper, though I could simply have -k '' I guess but this won't work currently with binman anyway.
Additionally, passing a keyfile (-G) when using an engine doesn't make sense as the key is stored in the engine.
Let simply allow FIT images be signed if both keydir and keyfile are missing but an engine is to be used.
The keyname member is already filled by looking at key-name-hint property in the FIT and passed to the engine, which is exactly what is needed here.
--- tools/fit_image.c | 3 ++- tools/image-host.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-)
tools/fit_image.c +2 -1
@@ -26,7 +26,8 @@ static struct legacy_img_hdr header;static int fit_estimate_hash_sig_size (struct image_tool_params *params,const char *fname) { -bool signing =IMAGE_ENABLE_SIGN && (params->keydir || params->keyfile); +bool signing =IMAGE_ENABLE_SIGN && + (params->keydir || params->keyfile || params->engine_id);struct stat sbuf;void *fdt;int fd;
tools/image-host.c +2 -2
@@ -696,7 +696,7 @@ int fit_image_add_verification_data(const char *keydir, const char *keyfile, strlen(FIT_HASH_NODENAME))) { ret =fit_image_process_hash (fit, image_name, noffset, data, size); - } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) && + } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile || engine_id) && !strncmp(node_name, FIT_SIG_NODENAME,strlen (FIT_SIG_NODENAME ))) { ret = fit_image_process_sig(keydir, keyfile, keydest,
@@ -1366,7 +1366,7 @@ int fit_add_verification_data(const char *keydir, const char *keyfile, }/* If there are no keys, we can't sign configurations */ -if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile)) +if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile || engine_id))return 0 ;/* Find configurations parent node offset */
Signature · 1 line
2.51.1
PATCH v2 2/4 tools: binman: mkimage: add support for passing the engine
In reply to Quentin SchulzFrom: Quentin Schulz <[email protected]>
mkimage has support for OpenSSL engines but binman currently doesn't for direct callers of mkimage (e.g. the fit etype). This prepares for adding support for OpenSSL engines for signing elements of a FIT image, which will done in the next commit.
--- tools/binman/btool/mkimage.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/binman/btool/mkimage.py +4 -1
@@ -22,7 +22,7 @@ class Bintoolmkimage(bintool.Bintool): # pylint: disable=R0913 def run(self, reset_timestamp=False, output_fname=None, external=False, - pad=None, align=None, keys_dir=None): + pad=None, align=None, keys_dir=None, engine=None): """Run mkimage Args:
@@ -35,6 +35,7 @@ class Bintoolmkimage(bintool.Bintool): signatures align: Bytes to use for alignment of the FIT and its external data keys_dir: Path to directory containing private and encryption keys + engine: Name of the OpenSSL engine to use """ args = [] if external:
@@ -49,6 +50,8 @@ class Bintoolmkimage(bintool.Bintool): args += ['-k', f'{keys_dir}'] if output_fname: args += ['-F', output_fname] + if engine: + args += ['-N', engine] return self.run_cmd(*args) def fetch(self, method):
Signature · 1 line
2.51.1
PATCH v2 3/4 tools: binman: fit: add support for OpenSSL engines
In reply to Quentin SchulzFrom: Quentin Schulz <[email protected]>
This adds support for using an OpenSSL engine for signing a FIT image. To use it, one should set the fit,engine property at the FIT node level with the engine to use. This will in turn call mkimage with the -N option.
The -k argument to mkimage can be specified via fit,engine-keydir. This is especially useful for pkcs11 engine to specify slots, token label, etc...
The -k argument to mkimage can be specified by setting fit,engine-keydir property, if missing no -k argument will be passed.
As far as I could tell, mkimage encrypts and signs a FIT in one go, thus the -k argument applies to both signing and encrypting. Considering we reuse the -k argument for two different meanings (info to pass to the engine when using an engine otherwise the directory where keys are stored), we cannot reasonably encrypt using local keys and signing with an engine, hence the enforced check. I believe it should be possible to support encrypting and signing with the same engine (using different key pairs of course, via different key-name-hint likely), but this is left for the next person to implement.
This is why the property is named fit,engine and not fit,sign-engine. Ditto for fit,engine-keydir.
The public key (with .crt extension) is still required if it needs to be embedded in the SPL DTB for example. We could probably support retrieving the public key from an engine, but this is a change to make to fdt_add_pubkey.c.
--- tools/binman/entries.rst | 45 +++++++++++++++++++++++++++++++--- tools/binman/etype/fit.py | 61 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 7 deletions(-)
tools/binman/entries.rst +42 -3
@@ -885,9 +885,10 @@ The top-level 'fit' node supports the following special properties: fit,sign Enable signing FIT images via mkimage as described in - verified-boot.rst. If the property is found, the private keys path - is detected among binman include directories and passed to mkimage - via -k flag. All the keys required for signing FIT must be + verified-boot.rst. + If the property is found and fit,engine is not set, the private + keys path is detected among binman include directories and passed to + mkimage via -k flag. All the keys required for signing FIT must be available at time of signing and must be located in single include directory.
@@ -898,6 +899,44 @@ The top-level 'fit' node supports the following special properties: required for encrypting the FIT must be available at the time of encrypting and must be located in a single include directory. + Incompatible with fit,engine. + + fit,engine + Indicates the OpenSSL engine to use for signing the FIT image. This + is passed to mkimage via the `-N` flag. Example:: + + fit,engine = "my-engine"; + + A `-k` argument for mkimage may be passed via `fit,engine-keydir`. + + When `fit,engine` is set to `pkcs11`, the value of + `fit,engine-keydir` (if present) is concatenated with `;object=` and + the value of `key-name-hint`, and passed as-is to the OpenSSL engine + API. If the value of `fit,engine-keydir` contains `object=` or + `id=`, only the value of `fit,engine-keydir` is passed as-is to the + OpenSSL engine API. If no `fit,engine-keydir` is present, + `pkcs11:object=` is concatenated with the value of `key-name-hint`, + and passed as-is to the OpenSSL engine API. + + For all other values of `fit,engine`, the value of + `fit,engine-keydir` (if present) is concatenated with the value of + `key-name-hint` and passed as-is to the OpenSSL engine API. + + Depends on fit,sign. + + Incompatible with fit,encrypt. + + fit,engine-keydir + Indicates the `-k` argument to pass to mkimage if an OpenSSL engine + is to be used for signing the FIT image. Example:: + + fit,engine-keydir = "pkcs11:model=xxx;manufacturer=xxx"; + + Read `fit,engine` documentation for more info on special cases when + using `pkcs11` as engine. + + Depends on fit,engine. + Substitutions ~~~~~~~~~~~~~
tools/binman/etype/fit.py +57 -4
79 lines not on this page · raw message
Signature · 1 line
2.51.1
PATCH v2 4/4 tools: binman: fit: add tests for signing with an OpenSSL engine
In reply to Quentin SchulzFrom: Quentin Schulz <[email protected]>
This adds a test that signs a FIT and verifies the signature with fit_check_sign.
OpenSSL engines are typically for signing with external HW so it's not that straight-forward to simulate.
For a simple RSA OpenSSL engine, a dummy engine with a hardcoded RSA 4096 private key is made available. It can be selected by setting the OpenSSL engine argument to dummy-rsa-engine. This can only be done if the engine is detected by OpenSSL, which works by setting the OPENSSL_ENGINES environment variable. I have no clue if dummy-rsa-engine is properly implementing what is expected from an RSA engine, but it seems to be enough for testing.
For a simple PKCS11 engine, SoftHSMv2 is used, which allows to do PKCS11 without specific hardware. The keypairs and tokens are generated on the fly. The "prod" token is generated with a different PIN (1234 instead of 1111) than the configured value in OpenSSL to force the use of fit,engine-keypair.
Binman will not mess with the local SoftHSMv2 setup as it will only use tokens from a temporary directory enforced via the temporary configuration file set via SOFTHSM2_CONF env variable in the tests.
Once signed, it's checked with fit_check_sign with the associated certificate.
Finally, a new softhsm2_util bintool is added so that we can initialize the token and import keypairs. On Debian, the package also brings libsofthsm2 which is required for OpenSSL to interact with SoftHSMv2. It is not the only package required though, as it also needs p11-kit and libengine-pkcs11-openssl (the latter bringing the former, only the latter is explicitly installed), which we can detect with openssl engine dynamic -c pkcs11. If that fails, we manually install the aforementioned library packages within tests.
--- tools/binman/btool/softhsm2_util.py | 21 +++ tools/binman/ftest.py | 193 +++++++++++++++++++++ tools/binman/test/340_dummy-rsa4096.crt | 31 ++++ tools/binman/test/340_fit_signature_engine.dts | 99 +++++++++++ .../test/340_fit_signature_engine_pkcs11.dts | 99 +++++++++++ .../340_fit_signature_engine_pkcs11_object.dts | 100 +++++++++++ tools/binman/test/340_openssl.conf | 10 ++ tools/binman/test/340_softhsm2.conf | 16 ++ tools/binman/test/Makefile | 6 +- tools/binman/test/dummy-rsa-engine.c | 149 ++++++++++++++++ 10 files changed, 723 insertions(+), 1 deletion(-)
tools/binman/btool/softhsm2_util.py new file +21 -0
@@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright 2025 Cherry Embedded Solutions GmbH +# +"""Bintool implementation for SoftHSMv2 (softhsm2-util)""" + +from binman import bintool + + +class Bintoolsofthsm2_util(bintool.Bintool): + """SoftHSMv2 -- support tool for libsofthsm2""" + def __init__(self, name): + super().__init__('softhsm2-util', + 'SoftHSMv2 support tool for libsofthsm2', + version_args='-v') + + def fetch(self, method): + """Install softhsm2-util via APT """ + if method != bintool.FETCH_BIN: + return None + + return self.apt_install('softhsm2')
tools/binman/ftest.py +193 -0
199 lines not on this page · raw message
tools/binman/test/340_dummy-rsa4096.crt new file +31 -0
@@ -0,0 +1,31 @@ +-----BEGIN CERTIFICATE----- +MIIFZTCCA02gAwIBAgIUHBvjPF93z8GaT9YkFleXIvBtt+8wDQYJKoZIhvcNAQEL +BQAwQjELMAkGA1UEBhMCWFgxFTATBgNVBAcMDERlZmF1bHQgQ2l0eTEcMBoGA1UE +CgwTRGVmYXVsdCBDb21wYW55IEx0ZDAeFw0yNTExMTQxMDAxNDRaFw0yNTEyMTQx +MDAxNDRaMEIxCzAJBgNVBAYTAlhYMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxHDAa +BgNVBAoME0RlZmF1bHQgQ29tcGFueSBMdGQwggIiMA0GCSqGSIb3DQEBAQUAA4IC +DwAwggIKAoICAQC6cUpK5P8J7E+flOwYFI2vh4qQ2mNbDvNfLG0DYMbMuWvWjwd5 +j88UuPCGdptLUL9QGsA0ZreewA3zBh2hAei0ryieYBSq6ez9kG6pAYcI6+c9VCro +jH9Tv+w/UEtNQUsn40j5c8koAqizEqGpwThM+gm+ftWBVCQHDscI9JXac4V/69OE +Oegf8NI4FoC/EQqLDVNxlVaSwxFC9ATEEuO9++rdaHiicmGnt27UOsgM9WO+gZpk +BgicLUp05qFymCwSPsiM6ELT9QYSxAMUi5u6gX2V5d444lSicaaZ2nRQXzOBqnsN +KQksObV2MiJ//jr3/M83TE23eJun6lACvu/5G/sJzJ+aqzBdpHx1DIGYKkQU5jA7 +n/FrHNAWVdyDhLYfaLOZKq7I/pcOzsjYFAjVbPnbVXqujuYj5YXsFttIo+PXTRzY +WBsEcBojqxOhEw8SpQOoeVYgvksKrw3u5RDO7XxkogMRuyO/DuBfm0dDvDf3Anlb +BjCsHHoJAuuOUKidGMkIK37lR6N6NcdZ4g3aSXvjl785/jCzQBl9HnrrGQV5rLHn +PYQkNAXrW+bXil4m4LB07e0/4aUrV8A/5SMolzWpfSjeFuLliZzBlAORNmtfKmT+ +F1WrNNnzUa11iRKe9JA7X+dBtC/nbxB2liBwzl8KdWNZOAAP5zQdcJQFMQIDAQAB +o1MwUTAdBgNVHQ4EFgQUy7zUkFMKw/hlJjHqjOYbClVCM3AwHwYDVR0jBBgwFoAU +y7zUkFMKw/hlJjHqjOYbClVCM3AwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0B +AQsFAAOCAgEAGpsQ5klxd4itvbjVCCf9usItnwlhrf6GxwvGpDBEa8oJbkcQNI+9 +GHVVE+/RO3jx/hjBItLUruKK7mmauO1K2qPLKK8nwcGYV2oMTxTbhZD93a61YBVO +KSWKi5czN8qCU2vkCc9LJM0y2aZzTjVDZCO6b7lYxCZTZcX2Ygcmt3Zv5sv3wrAF +9h0nv8CD4soqsE31GZmfuSiZ/lfBdX7Awr9WI6t++zOKBSZmxmLhfG+Fplak10Xv +TR+yOXHmy+aLegUbrsNgH8ktDAyvM7Aq6+EvbTD6lT06T328cA+AWrKRzYcEGjE0 +wdohmfaKoOch3oZNaXR4fiXDuwTBZ6xHsDpa0RDhvu/YhBP50CxQSqQNuURhFYJB +qBq3E0tpM4cCABBDA8ci2i1ilYb1AeL1LMqYQJXdIa8X5YP1NMdNyMO+4Aizc1J6 +wNvBqItgShvLjBWoQGYWKiiqgz8UoWWMjp0VuKmv0fu/71rbBarg2JDMsC7U5yno +rsfCDgcLGhemFe2EpvlLd8j+rmzjl4Kx1UGKC0EbqBFFaGj8ZlCu20KHpPYiOzNd +p2dU6LpeQYvOfQOCMjNe6BPKCxE4FEkQpdXuF2aKq9rlbZtcVeo1W/NDn2JHaDZ2 +BhHQfv/vugdB9E6JtqoQsME4rDkE3+0AxA7qvVkWFnI0cFtHsqZD+N4= +-----END CERTIFICATE-----
tools/binman/test/340_fit_signature_engine.dts new file +99 -0
99 lines not on this page · raw message
tools/binman/test/340_fit_signature_engine_pkcs11.dts new file +99 -0
99 lines not on this page · raw message
tools/binman/test/340_fit_signature_engine_pkcs11_object.dts new file +100 -0
100 lines not on this page · raw message
tools/binman/test/340_openssl.conf new file +10 -0
@@ -0,0 +1,10 @@ +openssl_conf = openssl_init + +[openssl_init] +engines = engine_section + +[engine_section] +pkcs11 = pkcs11_section + +[pkcs11_section] +PIN=1111
tools/binman/test/340_softhsm2.conf new file +16 -0
@@ -0,0 +1,16 @@ +# SoftHSM v2 configuration file + +# directories.tokendir = /path/to/binman/test/softhsm2.tokens/ +objectstore.backend = file + +# ERROR, WARNING, INFO, DEBUG +log.level = ERROR + +# If CKF_REMOVABLE_DEVICE flag should be set +slots.removable = false + +# Enable and disable PKCS#11 mechanisms using slots.mechanisms. +slots.mechanisms = ALL + +# If the library should reset the state on fork +library.reset_on_fork = false
tools/binman/test/Makefile +5 -1
@@ -35,7 +35,8 @@ LDS_BLOB := -T $(SRC)blob_syms.lds TARGETS = u_boot_ucode_ptr u_boot_no_ucode_ptr bss_data bss_data_zero \ u_boot_binman_syms u_boot_binman_syms.bin u_boot_binman_syms_bad \ u_boot_binman_syms_size u_boot_binman_syms_x86 embed_data \ - u_boot_binman_embed u_boot_binman_embed_sm elf_sections blob_syms.bin + u_boot_binman_embed u_boot_binman_embed_sm elf_sections blob_syms.bin \ + dummy-rsa-engine.so all: $(TARGETS)
@@ -84,6 +85,9 @@ blob_syms: blob_syms.c elf_sections: CFLAGS += $(LDS_EFL_SECTIONS) elf_sections: elf_sections.c +dummy-rsa-engine.so: dummy-rsa-engine.c + $(CC) -fPIC -shared -lcrypto -lssl -o $@ $< + clean: rm -f $(TARGETS)
tools/binman/test/dummy-rsa-engine.c new file +149 -0
149 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v2 3/4] tools: binman: fit: add support for OpenSSL engines
In reply to Quentin SchulzHi Quentin,
Quoted 10 lines
From: Quentin Schulz <[email protected]>
This adds support for using an OpenSSL engine for signing a FIT image. To use it, one should set the fit,engine property at the FIT node level with the engine to use. This will in turn call mkimage with the -N option.
The -k argument to mkimage can be specified via fit,engine-keydir. This is especially useful for pkcs11 engine to specify slots, token label, etc...
Thanks a lot, v2 of the patch series is really an improvement for the PKCS#11 use case. I was not aware that there was already code that could automatically prefix 'pkcs11:object=' to the key-name-hint. (Yes, I know, it is part of the existing documentation, and I have read that, but that just shows how much I miss even when the reading docs ...)
Quoted 2 lines
The -k argument to mkimage can be specified by setting fit,engine-keydir property, if missing no -k argument will be passed
Small nitpick: the above two paragraphs seem repetitive, how about the following?
The -k argument to mkimage can be specified via fit,engine-keydir. If fit,engine-keydir is not specified, no -k argument will be passed to mkimage. The attribute fit,engine-keydir is especially useful for a pkcs11 engine to specify slots, token label, etc...
Quoted 53 lines
As far as I could tell, mkimage encrypts and signs a FIT in one go, thus the -k argument applies to both signing and encrypting. Considering we reuse the -k argument for two different meanings (info to pass to the engine when using an engine otherwise the directory where keys are stored), we cannot reasonably encrypt using local keys and signing with an engine, hence the enforced check. I believe it should be possible to support encrypting and signing with the same engine (using different key pairs of course, via different key-name-hint likely), but this is left for the next person to implement.
This is why the property is named fit,engine and not fit,sign-engine. Ditto for fit,engine-keydir.The public key (with .crt extension) is still required if it needs to be embedded in the SPL DTB for example. We could probably support retrieving the public key from an engine, but this is a change to make to
fdt_add_pubkey.c.Signed-off-by Quentin Schulz <[email protected]>---
tools/binman/entries.rst | 45 +++++++++++++++++++++++++++++++---tools/binman/etype/fit.py| 61 +++++++++++++++++++++++++++++++++++++++++++----2 files changed, 99 insertions(+), 7 deletions(-)tools/binman/entries.rst +0
-0@@ -885,9 +885,10 @@ The top-level 'fit' node supports the following special properties:fit,sign
Enable signing FIT images via mkimage as described in
-verified-boot.rst. If the property is found, the private keys path - is detected among binman include directories and passed to mkimage - via -k flag. All the keys required for signing FIT must be +verified-boot.rst.
+ If the property is found and fit,engine is not set, the private + keys path is detected among binman include directories and passed to + mkimage via -k flag. All the keys required for signing FIT must be available at time of signing and must be located in single include directory.tools/binman/entries.rst +0
-0@@ -898,6 +899,44 @@ The top-level 'fit' node supports the following special properties:required for encrypting the FIT must be available at the time of encrypting and must be located in a single include directory.
+ Incompatible with fit,engine.
tools/binman/entries.rst +7
-0+ + fit,engine + Indicates the OpenSSL engine to use for signing the FIT image. This + is passed to mkimage via the `-N` flag. Example:: + + fit,engine = "my-engine"; +
tools/binman/entries.rst The top-level 'fit' node supports the following special properties: · line 919
Quoted 10 lines
tools/binman/entries.rst +10
-0+ A `-k` argument for mkimage may be passed via `fit,engine-keydir`. + + When `fit,engine` is set to `pkcs11`, the value of + `fit,engine-keydir` (if present) is concatenated with `;object=` and + the value of `key-name-hint`, and passed as-is to the OpenSSL engine + API. If the value of `fit,engine-keydir` contains `object=` or + `id=`, only the value of `fit,engine-keydir` is passed as-is to the + OpenSSL engine API. If no `fit,engine-keydir` is present, + `pkcs11:object=` is concatenated with the value of `key-name-hint`, + and passed as-is to the OpenSSL engine API.
This description is quite convoluted. I would propose to describe it as a list, something like the following:
When fit,engine is set to pkcs11, the following cases are distinguished regarding the value of fit,engine-keydir:
- If fit,engine-keydir is not present, value of key-name-hint is prefixed with pkcs11:object=, and then passed as-is to the OpenSSL engine API.
PKCS#11 id: pkcs11:object=<key-name-hint>
- If fit,engine-keydir is present, and its value contains either object= or id=, then the value of fit,engine-keydir is passed as-is to theOpenSSL engine API. The value of fit,engine-keydir has to start with pkcs11:.
PKCS#11 id: <fit,engine-keydir>
- If fit,engine-keydir is present, but its value does not contain either object= or id=, then the value of fit,engine-keydir is concatenated with ;object= and the value of key-name-hint, and passed as-is to the OpenSSL engine API. The value of fit,engine-keydir has to start with pkcs11:.
PKCS#11 id: <fit,engine-keydir>; object=<key-name-hint>
In all of the above cases, the value of <key-name-hint> is used for verification to find the matching public key.
Quoted 52 lines
+ For all other values of
fit,engine, the value of
+fit,engine-keydir(if present) is concatenated with the value of +key-name-hintand passed as-is to the OpenSSL engine API.tools/binman/entries.rst +16
-0+ + Depends on fit,sign. + + Incompatible with fit,encrypt. + + fit,engine-keydir + Indicates the `-k` argument to pass to mkimage if an OpenSSL engine + is to be used for signing the FIT image. Example:: + + fit,engine-keydir = "pkcs11:model=xxx;manufacturer=xxx"; + + Read `fit,engine` documentation for more info on special cases when + using `pkcs11` as engine. + + Depends on fit,engine. +Substitutions
~~~~~~~~~~~~~tools/binman/etype/fit.py +0
-0@@ -104,9 +104,10 @@ class Entry_fit(Entry_section):fit,sign
Enable signing FIT images via mkimage as described in
-verified-boot.rst. If the property is found, the private keys path - is detected among binman include directories and passed to mkimage - via -k flag. All the keys required for signing FIT must be +verified-boot.rst.
+ If the property is found and fit,engine is not set, the private + keys path is detected among binman include directories and passed to + mkimage via -k flag. All the keys required for signing FIT must be available at time of signing and must be located in single include directory.tools/binman/etype/fit.py +0
-0@@ -117,6 +118,44 @@ class Entry_fit(Entry_section):required for encrypting the FIT must be available at the time of encrypting and must be located in a single include directory.
+ Incompatible with fit,engine.
tools/binman/etype/fit.py +7
-0+ + fit,engine + Indicates the OpenSSL engine to use for signing the FIT image. This + is passed to mkimage via the `-N` flag. Example:: + + fit,engine = "my-engine"; +
tools/binman/etype/fit.py
Quoted 10 lines
tools/binman/etype/fit.py +10
-0+ A `-k` argument for mkimage may be passed via `fit,engine-keydir`. + + When `fit,engine` is set to `pkcs11`, the value of + `fit,engine-keydir` (if present) is concatenated with `;object=` and + the value of `key-name-hint`, and passed as-is to the OpenSSL engine + API. If the value of `fit,engine-keydir` contains `object=` or + `id=`, only the value of `fit,engine-keydir` is passed as-is to the + OpenSSL engine API. If no `fit,engine-keydir` is present, + `pkcs11:object=` is concatenated with the value of `key-name-hint`, + and passed as-is to the OpenSSL engine API.
Same as above.
Quoted 38 lines
tools/binman/etype/fit.py +20
-0+ + For all other values of `fit,engine`, the value of + `fit,engine-keydir` (if present) is concatenated with the value of + `key-name-hint` and passed as-is to the OpenSSL engine API. + + Depends on fit,sign. + + Incompatible with fit,encrypt. + + fit,engine-keydir + Indicates the `-k` argument to pass to mkimage if an OpenSSL engine + is to be used for signing the FIT image. Example:: + + fit,engine-keydir = "pkcs11:model=xxx;manufacturer=xxx"; + + Read `fit,engine` documentation for more info on special cases when + using `pkcs11` as engine. + + Depends on fit,engine. +Substitutions
~~~~~~~~~~~~~tools/binman/etype/fit.py +0
-0@@ -620,7 +659,21 @@ class Entry_fit(Entry_section): args.update({'align': fdt_util.fdt32_to_cpu(align.value)})if (
self._fit_props.get('fit,sign')is not None orself._fit_props.get('fit,encrypt')is not None):
-args.update({'keys_dir': self._get_keys_dir(data)})+ engine = None
+ ifself._fit_props.get('fit,sign')is not None:
+engine_prop=self._fit_props.get('fit,engine')+ ifengine_propis not None:
+ engine = engine_prop.value
+args.update({'engine': engine})
+engine_keydir_prop=self._fit_props.get('fit,engine-keydir')+ ifengine_keydir_propis not None:
+engine_keydir= engine_keydir_prop.value
+ ifengine_keydir:
Quoted 8 lines
+
args.update({'keys_dir': engine_keydir})+ if engine is None:
+args.update({'keys_dir': self._get_keys_dir(data)})+ elifself._fit_props.get('fit,encrypt')is not None:
+self.Raise('fit,engine currently does not support encryption')ifself.mkimage.run(reset_timestamp=True, output_fname=output_fname, **args)is None:
if notself.GetAllowMissing():
I did test the 3 cases listed in the description of fit,engine:
Test 1: No fit,engine-keydir:
fit,sign;
fit,engine = "pkcs11";
key-name-hint = "<object-id-of-my-key>";Test 2: fit,engine-keydir present, and contains the string 'object=':
fit,sign;
fit,engine = "pkcs11";
fit,engine-keydir = "pkcs11:object=<object-id-of-my-key>";
key-name-hint = "MyTestKey";Test 3: fit,engine-keydir present, but does not contain'object=' of 'id=':
fit,sign;
fit,engine = "pkcs11";
fit,engine-keydir = "pkcs11:serial=<serial-of-my-key>";
key-name-hint = "<object-id-of-my-key>";I think the suggestions above would improve the clarity, but even if you choose to keep the text as is would be fine for me.
Both code + documentation:
regards, Wolfgang
Re: [PATCH v2 2/4] tools: binman: mkimage: add support for passing the engine
In reply to Quentin SchulzHi Quentin,
From: Quentin Schulz <[email protected]>
Quoted 10 lines
mkimage has support for OpenSSL engines but binman currently doesn't for direct callers of mkimage (e.g. the fit etype). This prepares for adding support for OpenSSL engines for signing elements of a FIT image, which will done in the next commit.
Signed-off-by Quentin Schulz <[email protected]>---
tools/binman/btool/mkimage.py | 5 ++++-1 file changed, 4 insertions(+), 1 deletion(-)
Re: [PATCH v2 3/4] tools: binman: fit: add support for OpenSSL engines
In reply to Wolfgang WallnerHi Wolfgang,
Quoted 22 lines · Wolfgang Wallner
Hi Quentin,
Quoted 10 lines
From: Quentin Schulz <[email protected]>
This adds support for using an OpenSSL engine for signing a FIT image. To use it, one should set the fit,engine property at the FIT node level with the engine to use. This will in turn call mkimage with the -N option.
The -k argument to mkimage can be specified via fit,engine-keydir. This is especially useful for pkcs11 engine to specify slots, token label, etc...
Thanks a lot, v2 of the patch series is really an improvement for the PKCS#11 use case. I was not aware that there was already code that could automatically prefix 'pkcs11:object=' to the key-name-hint. (Yes, I know, it is part of the existing documentation, and I have read that, but that just shows how much I miss even when the reading docs ...)
Quoted 2 lines
The -k argument to mkimage can be specified by setting fit,engine-keydir property, if missing no -k argument will be passed
Quoted 7 lines
Small nitpick: the above two paragraphs seem repetitive, how about the following?
The -k argument to mkimage can be specified via fit,engine-keydir. If fit,engine-keydir is not specified, no -k argument will be passed to mkimage. The attribute fit,engine-keydir is especially useful for a pkcs11 engine to specify slots, token label, etc...
I think I simply forgot to remove one sentence when rewording the commit log when I added support for PKCS#11 :)
[...]
Quoted 2 lines
This description is quite convoluted. I would propose to describe it as a list,
I'm very good at writing convoluted paragraphs, so thanks for suggesting something more readable :)
Quoted 9 lines
something like the following:
When
fit,engineis set topkcs11, the following cases are distinguished regarding the value offit,engine-keydir:- If
fit,engine-keydiris not present, value ofkey-name-hintis prefixed withpkcs11:object=, and then passed as-is to the OpenSSL engine API.PKCS#11 id:
pkcs11:object=<key-name-hint>
I have no experience with PKCS#11, but shouldn't that rather be a "PKCS#11 URI" instead if " PKCS#11 id"? c.f. https://www.rfc-editor.org/rfc/rfc7512 Especially since there is actually a possible id field, see https://www.rfc-editor.org/rfc/rfc7512#section-2.3 pk11-id.
Quoted 7 lines
- If
fit,engine-keydiris present, and its value contains eitherobject=orid=, then the value offit,engine-keydiris passed as-is to theOpenSSL engine API. The value offit,engine-keydirhas to start withpkcs11:.PKCS#11 id:
<fit,engine-keydir>
Quoted 8 lines
- If
fit,engine-keydiris present, but its value does not contain eitherobject=orid=, then the value offit,engine-keydiris concatenated with;object=and the value ofkey-name-hint, and passed as-is to the OpenSSL engine API. The value offit,engine-keydirhas to start withpkcs11:.PKCS#11 id:
<fit,engine-keydir>; object=<key-name-hint>
*Technically*, pkcs11: prefix isn't required as it'll be automatically added if it's missing (and you'll be warned about it), c.f. https://elixir.bootlin.com/u-boot/v2025.10/source/lib/rsa/rsa-sign.c#L120
But yes, I'll do this rewording in a v3. This is not up for merging until 2026.01 is released as it's not a bugfix, so we've time for some other feedback/review before I send a v3.
[...]
Quoted 25 lines
Tested-by Wolfgang Wallner <[email protected]>I did test the 3 cases listed in the description of fit,engine:
Test 1: No fit,engine-keydir:
fit,sign; fit,engine = "pkcs11"; key-name-hint = "<object-id-of-my-key>";Test 2: fit,engine-keydir present, and contains the string 'object=':
fit,sign; fit,engine = "pkcs11"; fit,engine-keydir = "pkcs11:object=<object-id-of-my-key>"; key-name-hint = "MyTestKey";Test 3: fit,engine-keydir present, but does not contain'object=' of 'id=':
fit,sign; fit,engine = "pkcs11"; fit,engine-keydir = "pkcs11:serial=<serial-of-my-key>"; key-name-hint = "<object-id-of-my-key>";
Quoted 5 lines
I think the suggestions above would improve the clarity, but even if you choose to keep the text as is would be fine for me.
Both code + documentation:Reviewed-by Wolfgang Wallner <[email protected]>
Great, thanks!
I was wondering if you had experience with using id= in the pkcs11 URI? It'd be nice if I could add a test with SoftHSMv2 for that as well but I wasn't able to do that fiddling around.
See 0707f73a8ba2 ("lib/rsa: allow matching pkcs11 path by object id") for the commit that added support for it.
Cheers,
Quentin
Re: [PATCH v2 2/4] tools: binman: mkimage: add support for passing the engine
In reply to Quentin SchulzHi Quentin,
Quoted 8 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
mkimage has support for OpenSSL engines but binman currently doesn't for direct callers of mkimage (e.g. the fit etype). This prepares for adding support for OpenSSL engines for signing elements of a FIT image, which will done in the next commit.
Quoted 4 lines
Signed-off-by Quentin Schulz <[email protected]>--- tools/binman/btool/mkimage.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
patchwork seems to be done, so I cannot try the test coverage
Re: [PATCH v2 4/4] tools: binman: fit: add tests for signing with an OpenSSL engine
In reply to Quentin SchulzQuoted 40 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
This adds a test that signs a FIT and verifies the signature with
fit_check_sign.OpenSSL engines are typically for signing with external HW so it's not that straight-forward to simulate.
For a simple RSA OpenSSL engine, a dummy engine with a hardcoded RSA 4096 private key is made available. It can be selected by setting the OpenSSL engine argument to dummy-rsa-engine. This can only be done if the engine is detected by OpenSSL, which works by setting the
OPENSSL_ENGINESenvironment variable. I have no clue if dummy-rsa-engine is properly implementing what is expected from an RSA engine, but it seems to be enough for testing.For a simple PKCS11 engine, SoftHSMv2 is used, which allows to do PKCS11 without specific hardware. The keypairs and tokens are generated on the fly. The "prod" token is generated with a different PIN (1234 instead of 1111) than the configured value in OpenSSL to force the use of fit,engine-keypair.
Binman will not mess with the local SoftHSMv2 setup as it will only use tokens from a temporary directory enforced via the temporary configuration file set via
SOFTHSM2_CONFenv variable in the tests.Once signed, it's checked with
fit_check_signwith the associated certificate.Finally, a new
softhsm2_utilbintool is added so that we can initialize the token and import keypairs. On Debian, the package also brings libsofthsm2 which is required for OpenSSL to interact with SoftHSMv2. It is not the only package required though, as it also needs p11-kit and libengine-pkcs11-openssl (the latter bringing the former, only the latter is explicitly installed), which we can detect with openssl engine dynamic -c pkcs11. If that fails, we manually install the aforementioned library packages within tests.Signed-off-by Quentin Schulz <[email protected]>
Quoted 12 lines
--- tools/binman/btool/softhsm2_util.py | 21 +++ tools/binman/ftest.py | 193 +++++++++++++++++++++ tools/binman/test/340_dummy-rsa4096.crt | 31 ++++ tools/binman/test/340_fit_signature_engine.dts | 99 +++++++++++ .../test/340_fit_signature_engine_pkcs11.dts | 99 +++++++++++ .../340_fit_signature_engine_pkcs11_object.dts | 100 +++++++++++ tools/binman/test/340_openssl.conf | 10 ++ tools/binman/test/340_softhsm2.conf | 16 ++ tools/binman/test/Makefile | 6 +- tools/binman/test/dummy-rsa-engine.c | 149 ++++++++++++++++ 10 files changed, 723 insertions(+), 1 deletion(-)
Looks like a solid test.
Re: [PATCH v2 3/4] tools: binman: fit: add support for OpenSSL engines
In reply to Quentin SchulzHi Quentin,
Quoted 174 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
This adds support for using an OpenSSL engine for signing a FIT image. To use it, one should set the fit,engine property at the FIT node level with the engine to use. This will in turn call mkimage with the -N option.
The -k argument to mkimage can be specified via fit,engine-keydir. This is especially useful for pkcs11 engine to specify slots, token label, etc...
The -k argument to mkimage can be specified by setting fit,engine-keydir property, if missing no -k argument will be passed.
As far as I could tell, mkimage encrypts and signs a FIT in one go, thus the -k argument applies to both signing and encrypting. Considering we reuse the -k argument for two different meanings (info to pass to the engine when using an engine otherwise the directory where keys are stored), we cannot reasonably encrypt using local keys and signing with an engine, hence the enforced check. I believe it should be possible to support encrypting and signing with the same engine (using different key pairs of course, via different key-name-hint likely), but this is left for the next person to implement.
This is why the property is named fit,engine and not fit,sign-engine. Ditto for fit,engine-keydir.The public key (with .crt extension) is still required if it needs to be embedded in the SPL DTB for example. We could probably support retrieving the public key from an engine, but this is a change to make to
fdt_add_pubkey.c.Signed-off-by Quentin Schulz <[email protected]>--- tools/binman/entries.rst | 45 +++++++++++++++++++++++++++++++--- tools/binman/etype/fit.py | 61 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 7 deletions(-)tools/binman/entries.rst +42
-3@@ -885,9 +885,10 @@ The top-level 'fit' node supports the following special properties: fit,sign Enable signing FIT images via mkimage as described in - verified-boot.rst. If the property is found, the private keys path - is detected among binman include directories and passed to mkimage - via -k flag. All the keys required for signing FIT must be + verified-boot.rst. + If the property is found and fit,engine is not set, the private + keys path is detected among binman include directories and passed to + mkimage via -k flag. All the keys required for signing FIT must be available at time of signing and must be located in single include directory.@@ -898,6 +899,44 @@ The top-level 'fit' node supports the following special properties: required for encrypting the FIT must be available at the time of encrypting and must be located in a single include directory. + Incompatible with fit,engine. + + fit,engine + Indicates the OpenSSL engine to use for signing the FIT image. This + is passed to mkimage via the `-N` flag. Example:: + + fit,engine = "my-engine"; + + A `-k` argument for mkimage may be passed via `fit,engine-keydir`. + + When `fit,engine` is set to `pkcs11`, the value of + `fit,engine-keydir` (if present) is concatenated with `;object=` and + the value of `key-name-hint`, and passed as-is to the OpenSSL engine + API. If the value of `fit,engine-keydir` contains `object=` or + `id=`, only the value of `fit,engine-keydir` is passed as-is to the + OpenSSL engine API. If no `fit,engine-keydir` is present, + `pkcs11:object=` is concatenated with the value of `key-name-hint`, + and passed as-is to the OpenSSL engine API. + + For all other values of `fit,engine`, the value of + `fit,engine-keydir` (if present) is concatenated with the value of + `key-name-hint` and passed as-is to the OpenSSL engine API. + + Depends on fit,sign. + + Incompatible with fit,encrypt. + + fit,engine-keydir + Indicates the `-k` argument to pass to mkimage if an OpenSSL engine + is to be used for signing the FIT image. Example:: + + fit,engine-keydir = "pkcs11:model=xxx;manufacturer=xxx"; + + Read `fit,engine` documentation for more info on special cases when + using `pkcs11` as engine. + + Depends on fit,engine. + Substitutions ~~~~~~~~~~~~~tools/binman/etype/fit.py +47
-4@@ -104,9 +104,10 @@ class Entry_fit(Entry_section): fit,sign Enable signing FIT images via mkimage as described in - verified-boot.rst. If the property is found, the private keys path - is detected among binman include directories and passed to mkimage - via -k flag. All the keys required for signing FIT must be + verified-boot.rst. + If the property is found and fit,engine is not set, the private + keys path is detected among binman include directories and passed to + mkimage via -k flag. All the keys required for signing FIT must be available at time of signing and must be located in single include directory.@@ -117,6 +118,44 @@ class Entry_fit(Entry_section): required for encrypting the FIT must be available at the time of encrypting and must be located in a single include directory. + Incompatible with fit,engine. + + fit,engine + Indicates the OpenSSL engine to use for signing the FIT image. This + is passed to mkimage via the `-N` flag. Example:: + + fit,engine = "my-engine"; + + A `-k` argument for mkimage may be passed via `fit,engine-keydir`. + + When `fit,engine` is set to `pkcs11`, the value of + `fit,engine-keydir` (if present) is concatenated with `;object=` and + the value of `key-name-hint`, and passed as-is to the OpenSSL engine + API. If the value of `fit,engine-keydir` contains `object=` or + `id=`, only the value of `fit,engine-keydir` is passed as-is to the + OpenSSL engine API. If no `fit,engine-keydir` is present, + `pkcs11:object=` is concatenated with the value of `key-name-hint`, + and passed as-is to the OpenSSL engine API. + + For all other values of `fit,engine`, the value of + `fit,engine-keydir` (if present) is concatenated with the value of + `key-name-hint` and passed as-is to the OpenSSL engine API. + + Depends on fit,sign. + + Incompatible with fit,encrypt. + + fit,engine-keydir + Indicates the `-k` argument to pass to mkimage if an OpenSSL engine + is to be used for signing the FIT image. Example:: + + fit,engine-keydir = "pkcs11:model=xxx;manufacturer=xxx"; + + Read `fit,engine` documentation for more info on special cases when + using `pkcs11` as engine. + + Depends on fit,engine. + Substitutions ~~~~~~~~~~~~~@@ -620,7 +659,21 @@ class Entry_fit(Entry_section): args.update({'align': fdt_util.fdt32_to_cpu(align.value)}) if (self._fit_props.get('fit,sign') is not None or self._fit_props.get('fit,encrypt') is not None): - args.update({'keys_dir': self._get_keys_dir(data)}) + engine = None + if self._fit_props.get('fit,sign') is not None: + engine_prop = self._fit_props.get('fit,engine') + if engine_prop is not None: + engine = engine_prop.value
tools/binman/etype/fit.py class Entry_fit(Entry_section): · line 676
Quoted 10 lines
tools/binman/etype/fit.py +10
-0+ args.update({'engine': engine}) + engine_keydir_prop = self._fit_props.get('fit,engine-keydir') + if engine_keydir_prop is not None: + engine_keydir = engine_keydir_prop.value + if engine_keydir: + args.update({'keys_dir': engine_keydir}) + if engine is None: + args.update({'keys_dir': self._get_keys_dir(data)}) + elif self._fit_props.get('fit,encrypt') is not None: + self.Raise('fit,engine currently does not support encryption')
The added code above would be better in a separate function.
Quoted 7 lines
tools/binman/etype/fit.py +0
-1if self.mkimage.run(reset_timestamp=True, output_fname=output_fname, **args) is None: if not self.GetAllowMissing(): --2.51.1
For the code parts:
Re: [PATCH v2 2/4] tools: binman: mkimage: add support for passing the engine
In reply to Simon GlassHi Simon,
Quoted 11 lines · Simon Glass
Hi Quentin,
Quoted 8 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
mkimage has support for OpenSSL engines but binman currently doesn't for direct callers of mkimage (e.g. the fit etype). This prepares for adding support for OpenSSL engines for signing elements of a FIT image, which will done in the next commit.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Quentin Schulz <[email protected]>--- tools/binman/btool/mkimage.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)patchwork seems to be done, so I cannot try the test coverage
b4 shazam https://lore.kernel.org/u-boot/[email protected]/
should do the trick to fetch the code easily and run whatever you want to run locally.
I only tested 4/4 tests individually (you cannot actually pass multiple even if the help text claims the opposite). The pip dependencies aren't installable on Fedora 43 (or Debian Trixie AFAICT) because the libgit2 dependency for building the wheels is too recent compared to what's expected. Anyway, building in the same container as the CI and repeating the same steps, it seems that my tests are actually breaking other tests now when running them all at once, I'll check. I suspect something related to the environment variables persisting across tests.
I'll run the coverage test as well, thanks for the reminder, to see what I can do, we're most likely missing the check for fit,engine + fit,encrypt raised Error.
Cheers,
Quentin
Re: [PATCH v2 3/4] tools: binman: fit: add support for OpenSSL engines
In reply to Quentin SchulzHi Quentin,
From: Quentin Schulz <[email protected]>
Quoted 15 lines
Hi Wolfgang,
[...]
Quoted 2 lines
This description is quite convoluted. I would propose to describe it as a list,
I'm very good at writing convoluted paragraphs, so thanks for suggesting something more readable :)
Quoted 5 lines
something like the following:
When
fit,engineis set topkcs11, the following cases are distinguished regarding the value offit,engine-keydir:
Quoted 7 lines
Quoted 4 lines
- If
fit,engine-keydiris not present, value ofkey-name-hintis prefixed withpkcs11:object=, and then passed as-is to the OpenSSL engine API.PKCS#11 id:
pkcs11:object=<key-name-hint>I have no experience with PKCS#11, but shouldn't that rather be a "PKCS#11 URI" instead if " PKCS#11 id"?
Yes, of course, my formulation was sloppy here. Please use the term URI.
Quoted 18 lines
c.f.
https://urldefense.com/v3/__https://www.rfc-editor.org/rfc/rfc7512__;!!NLW3fF9v!Kt-msudWj52ujfJPS_zBSV_Jf8pvI1_WgEK0f17edIPw_o_DMpGzg7pNOQtcBIFfoTYDZoLczC-z4B0OcGBBqqPWDZ5RHT8LGN3kRdkY$ Especially since there is actually a possible id field, see
https://urldefense.com/v3/__https://www.rfc-editor.org/rfc/rfc7512*section-2.3__;Iw!!NLW3fF9v!Kt-msudWj52ujfJPS_zBSV_Jf8pvI1_WgEK0f17edIPw_o_DMpGzg7pNOQtcBIFfoTYDZoLczC-z4B0OcGBBqqPWDZ5RHT8LGPYmW1op$ pk11-id.Quoted 13 lines
- If
fit,engine-keydiris present, and its value contains eitherobject=orid=, then the value offit,engine-keydiris passed as-is to theOpenSSL engine API. The value offit,engine-keydirhas to start withpkcs11:.PKCS#11 id:
<fit,engine-keydir>- If
fit,engine-keydiris present, but its value does not contain eitherobject=orid=, then the value offit,engine-keydiris concatenated with;object=and the value ofkey-name-hint, and passed as-is to the OpenSSL engine API. The value offit,engine-keydirhas to start withpkcs11:.
Quoted 6 lines
Quoted 2 lines
PKCS#11 id:
<fit,engine-keydir>; object=<key-name-hint>*Technically*, pkcs11: prefix isn't required as it'll be automatically added if it's missing (and you'll be warned about it), c.f.
https://urldefense.com/v3/__https://elixir.bootlin.com/u-boot/v2025.10/source/lib/rsa/rsa-sign.c*L120__;Iw!!NLW3fF9v!Kt-msudWj52ujfJPS_zBSV_Jf8pvI1_WgEK0f17edIPw_o_DMpGzg7pNOQtcBIFfoTYDZoLczC-z4B0OcGBBqqPWDZ5RHT8LGL2ft2Fl$
Oh, I missed that. Somehow I thought it would warn and then abort.
Quoted 37 lines
But yes, I'll do this rewording in a v3. This is not up for merging until 2026.01 is released as it's not a bugfix, so we've time for some other feedback/review before I send a v3.
[...]
Quoted 25 lines
Tested-by Wolfgang Wallner <[email protected]>I did test the 3 cases listed in the description of fit,engine:
Test 1: No fit,engine-keydir:
fit,sign;
fit,engine = "pkcs11";
key-name-hint = "<object-id-of-my-key>";Test 2: fit,engine-keydir present, and contains the string 'object=':
fit,sign;
fit,engine = "pkcs11";
fit,engine-keydir = "pkcs11:object=<object-id-of-my-key>"; key-name-hint = "MyTestKey";Test 3: fit,engine-keydir present, but does not contain'object=' of 'id=':
fit,sign;
fit,engine = "pkcs11";
fit,engine-keydir = "pkcs11:serial=<serial-of-my-key>"; key-name-hint = "<object-id-of-my-key>";Quoted 5 lines
I think the suggestions above would improve the clarity, but even if you choose to keep the text as is would be fine for me.
Both code + documentation:Reviewed-by Wolfgang Wallner <[email protected]>
Quoted 3 lines
Great, thanks!
I was wondering if you had experience with using id= in the pkcs11 URI?
Thanks for pointing that out, I have now additionally tested with 'id=%01%02%03':
fit,sign;
fit,engine = "pkcs11";
fit,engine-keydir = "pkcs11:id=<id-of-my-key>";
key-name-hint = "MyTestKey";There is one other aspect of the current solution that I'm not sure about:
* key-name-hint is an attribute per signature
* fit,engine-keydir is an attribute once per FIT description
I don't know wheter this is a use case for anyone, but I think if one would like to have multiple signature nodes in a FIT description (e.g. multiple configurations) and would like to have them signed with different keys, then it would only be possible to do that via the key-name-hint, and thus only via "object=xxx", but not via "id=xxx".
Quoted 5 lines
It'd be nice if I could add a test with SoftHSMv2 for that as well but I wasn't able to do that fiddling around.
See
0707f73a8ba2("lib/rsa: allow matching pkcs11 path by object id") for the commit that added support for it.
regards, Wolfgang
Re: [PATCH v2 3/4] tools: binman: fit: add support for OpenSSL engines
Quoted 21 lines · Wolfgang Wallner
Hi Quentin,
From: Quentin Schulz <[email protected]>
Quoted 18 lines
Hi Wolfgang,
[...]
Quoted 2 lines
This description is quite convoluted. I would propose to describe it as a list,
I'm very good at writing convoluted paragraphs, so thanks for suggesting something more readable :)
Quoted 8 lines
something like the following:
When
fit,engineis set topkcs11, the following cases are distinguished regarding the value offit,engine-keydir:- If
fit,engine-keydiris not present, value ofkey-name-hintis prefixed withpkcs11:object=, and then passed as-is to the OpenSSL engine API.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
PKCS#11 id:
pkcs11:object=<key-name-hint>I have no experience with PKCS#11, but shouldn't that rather be a "PKCS#11 URI" instead if " PKCS#11 id"?
Yes, of course, my formulation was sloppy here. Please use the term URI.
I'm now wondering if I'm not just going too far into explaining what happens to fit,engine-keydir. It is after all passed verbatim as -k argument to mkimage. The special cases for pkcs11 are all internal to mkimage so I'm not sure if fit,engine-keydir is the best place to document that? That could drastically simplify the documentation for the fit entry in binman.
[...]
Quoted 8 lines
Quoted 1 line
I was wondering if you had experience with using id= in the pkcs11 URI?
Thanks for pointing that out, I have now additionally tested with 'id=%01%02%03':
fit,sign; fit,engine = "pkcs11"; fit,engine-keydir = "pkcs11:id=<id-of-my-key>"; key-name-hint = "MyTestKey";
Any chance I can get some softhsm2-util command to generate that object with that id :) I wasn't able to last time I tried with id=999999 or id=%999999 (since that's what I set the id of the key when importing) in the test :/ Possibly PEBKAC though :)
Quoted 10 lines
There is one other aspect of the current solution that I'm not sure about:
* key-name-hint is an attribute per signature
* fit,engine-keydir is an attribute once per FIT descriptionI don't know wheter this is a use case for anyone, but I think if one would like to have multiple signature nodes in a FIT description (e.g. multiple configurations) and would like to have them signed with different keys, then it would only be possible to do that via the key-name-hint, and thus only via "object=xxx", but not via "id=xxx".
As far as I know (but happy to be proven otherwise), this is not possible with mkimage right now. But note that I too was bothered while looking at what was possible with mkimage. In the end, I don't need it but I could see it being useful. On Rockchip, we typically only need to sign one FIT: U-Boot proper, and everything is validated by SPL before loading TF-A (bl31), TEE (bl32) and U-Boot proper before jumping into BL31 so there really isn't a use case here. The kernel FIT which could be verified by U-Boot proper before booting is anyway a different FIT so can have a different engine or engine-keydir.
I think we would need to use a per-signature-node Device Tree property to specify which engine (if any) to use and which arguments to pass with the engine as well (still per-signature-node; not using the -k argument anymore for that). This is something that needs to be supported in mkimage though, with possible additions to the FIT spec (though it is arguable). This would allow to use a different engine (or not even using an engine) for encryption vs signing, which we currently cannot do.
Cheers,
Quentin
Re: [PATCH v2 2/4] tools: binman: mkimage: add support for passing the engine
In reply to Quentin SchulzHi Quentin,
Quoted 38 lines · Quentin Schulz
Hi Simon,
Quoted 11 lines · Simon Glass
Hi Quentin,
Quoted 8 lines · Quentin Schulz
From: Quentin Schulz <[email protected]>
mkimage has support for OpenSSL engines but binman currently doesn't for direct callers of mkimage (e.g. the fit etype). This prepares for adding support for OpenSSL engines for signing elements of a FIT image, which will done in the next commit.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Quentin Schulz <[email protected]>--- tools/binman/btool/mkimage.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)patchwork seems to be done, so I cannot try the test coverage
b4 shazam
https://lore.kernel.org/u-boot/[email protected]/should do the trick to fetch the code easily and run whatever you want to run locally.
I only tested 4/4 tests individually (you cannot actually pass multiple even if the help text claims the opposite). The pip dependencies aren't installable on Fedora 43 (or Debian Trixie AFAICT) because the libgit2 dependency for building the wheels is too recent compared to what's expected. Anyway, building in the same container as the CI and repeating the same steps, it seems that my tests are actually breaking other tests now when running them all at once, I'll check. I suspect something related to the environment variables persisting across tests.
Quoted 3 lines
I'll run the coverage test as well, thanks for the reminder, to see what I can do, we're most likely missing the check for fit,engine + fit,encrypt raised Error.
OK thanks, that worked. It looks quite good, but you have 4 lines missing coverage in fit.py, from what I can see.
Regards,
Simon