UEFI Capsule - PKCS11 Support
v3 cover UEFI Capsule - PKCS11 Support
Add support for pkcs11 URI's when generating UEFI capsules and accept URI's for certificate in dts capsule nodes.
Example:
export PKCS11_MODULE_PATH=<pkcs11 provider path>/libsofthsm2.so tools/mkeficapsule --monotonic-count 1 \
--private-key "pkcs11:token=EX;object=capsule;type=private;pin-source=pin.txt" \
--certificate "pkcs11:token=EX;object=capsule;type=cert;pin-source=pin.txt" \
--index 1 \
--guid XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX \
"capsule-payload" \
"capsule.cap
---
Changes in v3:
* fix write file encoding, env setting and extra line in binman test after review
Changes in v2:
* allow mixed file/pkcs11 URI as key specification in mkeficapsule
* fix logic for accepting pkcs11 URI in binman device tree sections
* add binman test for UEFI capsule signature where private key comes from softHSM
---
Wojciech Dubowik (3):
tools: mkeficapsule: Add support for pkcs11
binman: Accept pkcs11 URI tokens for capsule updates
test: binman: Add test for pkcs11 signed capsule
tools/binman/etype/efi_capsule.py | 8 +- tools/binman/ftest.py | 42 +++++++ .../binman/test/351_capsule_signed_pkcs11.dts | 20 ++++ tools/mkeficapsule.c | 110 +++++++++++++----- 4 files changed, 152 insertions(+), 28 deletions(-) create mode 100644 tools/binman/test/351_capsule_signed_pkcs11.dts
Signature · 1 line
2.47.3
PATCH v3 1/3 tools: mkeficapsule: Add support for pkcs11
With pkcs11 support it's now possible to specify keys with URI format. To use this feature the filename must begin "pkcs11:.." and have valid URI pointing to certificate and private key in HSM.
The environment variable PKCS11_MODULE_PATH must point to the right pkcs11 provider i.e. with softhsm:
export PKCS11_MODULE_PATH=<path>/libsofthsm2.so
Example command line:tools/mkeficapsule --monotonic-count 1 \
--private-key "pkcs11:token=EX;object=capsule;type=private;pin-source=pin.txt" \
--certificate "pkcs11:token=EX;object=capsule;type=cert;pin-source=pin.txt" \
--index 1 \
--guid XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX \
"capsule-payload" \
"capsule.cap"
--- tools/mkeficapsule.c | 110 +++++++++++++++++++++++++++++++++---------- 1 file changed, 84 insertions(+), 26 deletions(-)
tools/mkeficapsule.c +84 -26
@@ -228,21 +228,54 @@ static int create_auth_data(struct auth_context *ctx)gnutls_pkcs7_t pkcs7;gnutls_datum_t data;gnutls_datum_t signature; +gnutls_pkcs11_obj_t *obj_list; +unsigned int obj_list_size =0 ; +const char *lib;int ret; +bool pkcs11_cert = false; +bool pkcs11_key = false; - ret =read_bin_file (ctx->cert_file, &cert.data, &file_size); -if (ret <0 ) -return -1 ; -if (file_size >UINT_MAX ) -return -1 ; - cert.size = file_size; +if (!strncmp (ctx->cert_file,"pkcs11:" ,7 )) + pkcs11_cert = true; - ret =read_bin_file (ctx->key_file, &key.data, &file_size); -if (ret <0 ) -return -1 ; -if (file_size >UINT_MAX ) -return -1 ; - key.size = file_size; +if (!strncmp (ctx->key_file,"pkcs11:" ,7 )) + pkcs11_key = true; + +if (pkcs11_cert || pkcs11_key) { + lib =getenv ("PKCS11_MODULE_PATH" ); +if (!lib) { +fprintf (stdout, +"PKCS11_MODULE_PATH not set in the environment\n" ); +return -1 ; + } + +gnutls_pkcs11_init (GNUTLS_PKCS11_FLAG_MANUAL ,NULL ); +gnutls_global_init (); + + ret =gnutls_pkcs11_add_provider (lib,"trusted" ); +if (ret <0 ) { +fprintf (stdout,"Failed to add pkcs11 provider\n" ); +return -1 ; + } + } + +if (!pkcs11_cert) { + ret =read_bin_file (ctx->cert_file, &cert.data, &file_size); +if (ret <0 ) +return -1 ; +if (file_size >UINT_MAX ) +return -1 ; + cert.size = file_size; + } + +if (!pkcs11_key) { + ret =read_bin_file (ctx->key_file, &key.data, &file_size); +if (ret <0 ) +return -1 ; +if (file_size >UINT_MAX ) +return -1 ; + key.size = file_size; + }/* * For debugging,
@@ -265,22 +298,42 @@ static int create_auth_data(struct auth_context *ctx)return -1 ; } -/* load a private key */ - ret =gnutls_privkey_import_x509_raw (pkey, &key,GNUTLS_X509_FMT_PEM , -0 ,0 ); -if (ret <0 ) { -fprintf (stderr, -"error in gnutls_privkey_import_x509_raw(): %s\n" , -gnutls_strerror (ret)); -return -1 ; +/* load x509 certificate */ +if (pkcs11_cert) { + ret =gnutls_pkcs11_obj_list_import_url4 (&obj_list, &obj_list_size, + ctx->cert_file,0 ); +if (ret <0 || obj_list_size ==0 ) { +fprintf (stdout,"Failed to import crt_file URI objects\n" ); +return -1 ; + } + +gnutls_x509_crt_import_pkcs11 (x509, obj_list[0 ]); + }else { + ret =gnutls_x509_crt_import (x509, &cert,GNUTLS_X509_FMT_PEM ); +if (ret <0 ) { +fprintf (stderr,"error in gnutls_x509_crt_import(): %s\n" , +gnutls_strerror (ret)); +return -1 ; + } } -/* load x509 certificate */ - ret =gnutls_x509_crt_import (x509, &cert,GNUTLS_X509_FMT_PEM ); -if (ret <0 ) { -fprintf (stderr,"error in gnutls_x509_crt_import(): %s\n" , -gnutls_strerror (ret)); -return -1 ; +/* load a private key */ +if (pkcs11_key) { + ret =gnutls_privkey_import_pkcs11_url (pkey, ctx->key_file); +if (ret <0 ) { +fprintf (stderr,"error in %d: %s\n" , __LINE__, +gnutls_strerror (ret)); +return -1 ; + } + }else { + ret =gnutls_privkey_import_x509_raw (pkey, &key,GNUTLS_X509_FMT_PEM , +0 ,0 ); +if (ret <0 ) { +fprintf (stderr, +"error in gnutls_privkey_import_x509_raw(): %s\n" , +gnutls_strerror (ret)); +return -1 ; + } }/* generate a PKCS #7 structure */
@@ -349,6 +402,11 @@ static int create_auth_data(struct auth_context *ctx)* gnutls_free(signature.data); */ +if (pkcs11_cert || pkcs11_key) { +gnutls_global_deinit (); +gnutls_pkcs11_deinit (); + } +return 0 ; }
Signature · 1 line
2.47.3
PATCH v3 2/3 binman: Accept pkcs11 URI tokens for capsule updates
In reply to Wojciech DubowikWith pkcs11 support in mkeficapsule we can now accept URI tokens and not only files.
--- tools/binman/etype/efi_capsule.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/binman/etype/efi_capsule.py +6 -2
@@ -125,10 +125,14 @@ class Entry_efi_capsule(Entry_section): private_key = '' public_key_cert = '' if self.auth: - if not os.path.isabs(self.private_key): + if not os.path.isabs(self.private_key) and not 'pkcs11:' in self.private_key: private_key = tools.get_input_filename(self.private_key) - if not os.path.isabs(self.public_key_cert): + if not os.path.isabs(self.public_key_cert) and not 'pkcs11:' in self.public_key_cert: public_key_cert = tools.get_input_filename(self.public_key_cert) + if 'pkcs11:' in self.private_key: + private_key = self.private_key + if 'pkcs11:' in self.public_key_cert: + public_key_cert = self.public_key_cert data, payload, uniq = self.collect_contents_to_file( self._entries.values(), 'capsule_in') outfile = self._filename if self._filename else 'capsule.%s' % uniq
Signature · 1 line
2.47.3
PATCH v3 3/3 test: binman: Add test for pkcs11 signed capsule
In reply to Wojciech DubowikTest pkcs11 URI support for UEFI capsule generation. For simplicity only private key is defined in binman section as softhsm tool doesn't support certificate import (yet).
--- tools/binman/ftest.py | 42 +++++++++++++++++++ .../binman/test/351_capsule_signed_pkcs11.dts | 20 +++++++++ 2 files changed, 62 insertions(+) create mode 100644 tools/binman/test/351_capsule_signed_pkcs11.dts
tools/binman/ftest.py +42 -0
@@ -7532,6 +7532,48 @@ fdt fdtmap Extract the devicetree blob from the fdtmap self._CheckCapsule(data, signed_capsule=True) + def testPkcs11SignedCapsuleGen(self): + """Test generation of EFI capsule (with PKCS11)""" + data = tools.read_file(self.TestFile("key.key")) + private_key = self._MakeInputFile("key.key", data) + data = tools.read_file(self.TestFile("key.pem")) + self._MakeInputFile("key.crt", data) + + softhsm2_util = bintool.Bintool.create('softhsm2_util') + self._CheckBintool(softhsm2_util) + + prefix = "testPkcs11SignedCapsuleGen." + # Configure SoftHSMv2 + data = tools.read_file(self.TestFile('340_softhsm2.conf')) + softhsm2_conf = self._MakeInputFile(f'{prefix}softhsm2.conf', data) + softhsm2_tokens_dir = self._MakeInputDir(f'{prefix}softhsm2.tokens') + tools.write_file(softhsm2_conf, data + + f'\ndirectories.tokendir = \ + {softhsm2_tokens_dir}\n'.encode("utf-8")) + + softhsm_paths="/usr/local/lib/softhsm/libsofthsm2.so \ + /usr/lib/softhsm/libsofthsm2.so \ + /usr/lib64/pkcs11/libsofthsm2.so \ + /usr/lib/i386-linux-gnu/softhsm/libsofthsm2.so \ + /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so" + + for softhsm2_lib_loc in softhsm_paths.split(): + if os.path.exists(softhsm2_lib_loc): + softhsm2_lib = softhsm2_lib_loc + + os.environ['SOFTHSM2_CONF'] = softhsm2_conf + tools.run('softhsm2-util', '--init-token', '--free', '--label', + 'U-Boot token', '--pin', '1111', '--so-pin', + '222222') + tools.run('softhsm2-util', '--import', private_key, '--token', + 'U-Boot token', '--label', 'test_key', '--id', '999999', + '--pin', '1111') + + os.environ['PKCS11_MODULE_PATH'] = softhsm2_lib + data = self._DoReadFile('351_capsule_signed_pkcs11.dts') + + self._CheckCapsule(data, signed_capsule=True) + def testCapsuleGenVersionSupport(self): """Test generation of EFI capsule with version support""" data = self._DoReadFile('313_capsule_version.dts')
tools/binman/test/351_capsule_signed_pkcs11.dts new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + binman { + efi-capsule { + image-index = <0x1>; + /* Image GUID for testing capsule update */ + image-guid = "binman-test"; + hardware-instance = <0x0>; + private-key = "pkcs11:token=U-Boot%20token;object=test_key;type=private;pin-value=1111"; + public-key-cert = "key.crt"; + + blob { + filename = "capsule_input.bin"; + }; + }; + }; +};
Signature · 1 line
2.47.3
Re: [PATCH v3 3/3] test: binman: Add test for pkcs11 signed capsule
Hi Wojciech,
I didn't see you had sent a v3 (going through my inbox from older to newer :) ). Please ignore review on v2, i'll repeat it here.
Quoted 39 lines · Wojciech Dubowik
Test pkcs11 URI support for UEFI capsule generation. For simplicity only private key is defined in binman section as softhsm tool doesn't support certificate import (yet).
Signed-off-by Wojciech Dubowik <[email protected]>Reviewed-by Simon Glass <[email protected]>--- tools/binman/ftest.py | 42 +++++++++++++++++++ .../binman/test/351_capsule_signed_pkcs11.dts | 20 +++++++++ 2 files changed, 62 insertions(+)create mode 100644
tools/binman/test/351_capsule_signed_pkcs11.dtstools/binman/ftest.py +19
-0@@ -7532,6 +7532,48 @@ fdt fdtmap Extract the devicetree blob from the fdtmap self._CheckCapsule(data, signed_capsule=True) + def testPkcs11SignedCapsuleGen(self): + """Test generation of EFI capsule (with PKCS11)""" + data = tools.read_file(self.TestFile("key.key")) + private_key = self._MakeInputFile("key.key", data) + data = tools.read_file(self.TestFile("key.pem")) + self._MakeInputFile("key.crt", data) + + softhsm2_util = bintool.Bintool.create('softhsm2_util') + self._CheckBintool(softhsm2_util) + + prefix = "testPkcs11SignedCapsuleGen." + # Configure SoftHSMv2 + data = tools.read_file(self.TestFile('340_softhsm2.conf')) + softhsm2_conf = self._MakeInputFile(f'{prefix}softhsm2.conf', data) + softhsm2_tokens_dir = self._MakeInputDir(f'{prefix}softhsm2.tokens') + tools.write_file(softhsm2_conf, data + + f'\ndirectories.tokendir = \ + {softhsm2_tokens_dir}\n'.encode("utf-8")) +
tools/binman/ftest.py fdt fdtmap Extract the devicetree blob from the fdtmap · line 7563
Quoted 10 lines
tools/binman/ftest.py +10
-0+ softhsm_paths="/usr/local/lib/softhsm/libsofthsm2.so \ + /usr/lib/softhsm/libsofthsm2.so \ + /usr/lib64/pkcs11/libsofthsm2.so \ + /usr/lib/i386-linux-gnu/softhsm/libsofthsm2.so \ + /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so" + + for softhsm2_lib_loc in softhsm_paths.split(): + if os.path.exists(softhsm2_lib_loc): + softhsm2_lib = softhsm2_lib_loc +
This seems brittle, isn't there a better mechanism than this that can be offered by distros? For openssl, installing libengine-pkcs11-openssl (and setting the provider in the OPENSSL_CONF env variable) was enough. Is there something similar to that for gnutls?
I don't think this will work on arm64 hosts, c.f.
https://debian.pkgs.org/13/debian-main-arm64/libsofthsm2_2.6.1-3_arm64.deb.html
tools/binman/ftest.py fdt fdtmap Extract the devicetree blob from the fdtmap · line 7576
Quoted 13 lines
tools/binman/ftest.py +13
-0+ os.environ['SOFTHSM2_CONF'] = softhsm2_conf + tools.run('softhsm2-util', '--init-token', '--free', '--label', + 'U-Boot token', '--pin', '1111', '--so-pin', + '222222') + tools.run('softhsm2-util', '--import', private_key, '--token', + 'U-Boot token', '--label', 'test_key', '--id', '999999', + '--pin', '1111') + + os.environ['PKCS11_MODULE_PATH'] = softhsm2_lib + data = self._DoReadFile('351_capsule_signed_pkcs11.dts') + + self._CheckCapsule(data, signed_capsule=True) +
Don't you want to validate it's properly signed?
Cheers,
Quentin
Re: EXTERNAL - [PATCH v3 3/3] test: binman: Add test for pkcs11 signed capsule
On Wed, Jan 14, 2026 at 05:36:40PM +0100, Quentin Schulz wrote:
Hello Quentin,
Quoted 50 lines
Hi Wojciech,
I didn't see you had sent a v3 (going through my inbox from older to newer :) ). Please ignore review on v2, i'll repeat it here.
Quoted 44 lines · Wojciech Dubowik
Test pkcs11 URI support for UEFI capsule generation. For simplicity only private key is defined in binman section as softhsm tool doesn't support certificate import (yet).
Signed-off-by Wojciech Dubowik <[email protected]>Reviewed-by Simon Glass <[email protected]>--- tools/binman/ftest.py | 42 +++++++++++++++++++ .../binman/test/351_capsule_signed_pkcs11.dts | 20 +++++++++ 2 files changed, 62 insertions(+)create mode 100644
tools/binman/test/351_capsule_signed_pkcs11.dtstools/binman/ftest.py +26
-0@@ -7532,6 +7532,48 @@ fdt fdtmap Extract the devicetree blob from the fdtmap self._CheckCapsule(data, signed_capsule=True) + def testPkcs11SignedCapsuleGen(self): + """Test generation of EFI capsule (with PKCS11)""" + data = tools.read_file(self.TestFile("key.key")) + private_key = self._MakeInputFile("key.key", data) + data = tools.read_file(self.TestFile("key.pem")) + self._MakeInputFile("key.crt", data) + + softhsm2_util = bintool.Bintool.create('softhsm2_util') + self._CheckBintool(softhsm2_util) + + prefix = "testPkcs11SignedCapsuleGen." + # Configure SoftHSMv2 + data = tools.read_file(self.TestFile('340_softhsm2.conf')) + softhsm2_conf = self._MakeInputFile(f'{prefix}softhsm2.conf', data) + softhsm2_tokens_dir = self._MakeInputDir(f'{prefix}softhsm2.tokens') + tools.write_file(softhsm2_conf, data + + f'\ndirectories.tokendir = \ + {softhsm2_tokens_dir}\n'.encode("utf-8")) + + softhsm_paths="/usr/local/lib/softhsm/libsofthsm2.so \ + /usr/lib/softhsm/libsofthsm2.so \ + /usr/lib64/pkcs11/libsofthsm2.so \ + /usr/lib/i386-linux-gnu/softhsm/libsofthsm2.so \ + /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so" + + for softhsm2_lib_loc in softhsm_paths.split():
Quoted 8 lines
tools/binman/ftest.py fdt fdtmap Extract the devicetree blob from the fdtmap · line 7563
Quoted 3 lines
tools/binman/ftest.py +3
-0+ if os.path.exists(softhsm2_lib_loc): + softhsm2_lib = softhsm2_lib_loc +This seems brittle, isn't there a better mechanism than this that can be offered by distros? For openssl, installing libengine-pkcs11-openssl (and setting the provider in the
OPENSSL_CONFenv variable) was enough. Is there something similar to that for gnutls?
I have based my code on gnutls test where the lib has been hardcoded as well. There could be a better way i.e. with pkg-config but I havn't analyzed it yet. Also p11 kit might give more info. Need to dig furher.
Wojtek
Quoted 22 lines
I don't think this will work on arm64 hosts, c.f.
https://debian.pkgs.org/13/debian-main-arm64/libsofthsm2_2.6.1-3_arm64.deb.htmltools/binman/ftest.py fdt fdtmap Extract the devicetree blob from the fdtmap · line 7576
Quoted 13 lines
tools/binman/ftest.py +13
-0+ os.environ['SOFTHSM2_CONF'] = softhsm2_conf + tools.run('softhsm2-util', '--init-token', '--free', '--label', + 'U-Boot token', '--pin', '1111', '--so-pin', + '222222') + tools.run('softhsm2-util', '--import', private_key, '--token', + 'U-Boot token', '--label', 'test_key', '--id', '999999', + '--pin', '1111') + + os.environ['PKCS11_MODULE_PATH'] = softhsm2_lib + data = self._DoReadFile('351_capsule_signed_pkcs11.dts') + + self._CheckCapsule(data, signed_capsule=True) +Don't you want to validate it's properly signed?
Cheers,
Quentin