binman: nxp_imxcst: support PKCS#11 URIs with percent-encoding
PATCH 1/1 binman: nxp_imxcst: support PKCS#11 URIs with percent-encoding
The CST configuration file is generated with configparser, using the default basic interpolation. Setting a value that contains a '%' not followed by '%' or '(' raises a ValueError, so signing fails when a key is given as a PKCS#11 URI with percent-encoded characters, which is common since token and object labels often contain spaces (e.g. "pkcs11:token=my%20token").
The values written to the CST configuration file never use interpolation, so disable it. This applies to both the i.MX8M and the i.MX93 etypes, which share the configuration code.
Add a test to check that percent-encoded PKCS#11 URIs given as keys are written unchanged to the CST configuration file.
--- tools/binman/etype/nxp_imxcst.py | 4 +++- tools/binman/ftest.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-)
tools/binman/etype/nxp_imxcst.py +3 -1
@@ -78,7 +78,9 @@ class Entry_nxp_imxcst(Entry_mkimage): Returns: ConfigParser instance """ - config = configparser.ConfigParser() + # Disable interpolation, since values like PKCS#11 URIs may contain + # percent-encoded characters + config = configparser.ConfigParser(interpolation=None) # Do not make key names lowercase config.optionxform = str config.read_string(template)
tools/binman/ftest.py +23 -0
@@ -8155,6 +8155,29 @@ fdt fdtmap Extract the devicetree blob from the fdtmap err = stderr.getvalue() self.assertRegex(err, "Image 'image'.*missing bintools.*: cst") + def testNxpImx8mCSTPKCS11Uri(self): + """Test CST signing with percent-encoded PKCS#11 URIs as keys""" + ivt_data = struct.pack('<I', 0x412000d1) + ivt_data += b'\x00' * 20 + ivt_data += struct.pack('<I', 0) + ivt_data += b'\x00' * 36 + self._MakeInputFile('imx8m-ivt.bin', ivt_data) + csf_key = 'pkcs11:token=my%20token;object=CSF1_1' + img_key = 'pkcs11:token=my%20token;object=IMG1_1' + with unittest.mock.patch.dict('os.environ', {'CSF_KEY': csf_key, + 'IMG_KEY': img_key}): + with terminal.capture() as (_, stderr): + self._DoTestFile('vendor/nxp_imx8_csf_pkcs11.dts', + force_missing_bintools='cst') + err = stderr.getvalue() + self.assertRegex(err, "Image 'image'.*missing bintools.*: cst") + + cfg_fname = glob.glob( + tools.get_output_filename('nxp.csf-config-txt.*'))[0] + config = tools.read_file(cfg_fname, binary=False) + self.assertIn(f'File = "{csf_key}"', config) + self.assertIn(f'File = "{img_key}"', config) + def testNxpImx8mCSTFCFB(self): """Test CST signing with FCFB-format input (normal auth, no unlock)""" # Create fake FCFB blob: magic(4) + padding(4116)
Signature · 1 line
2.34.1
Quoted 11 lines
The CST configuration file is generated with configparser, using the default basic interpolation. Setting a value that contains a '%' not followed by '%' or '(' raises a ValueError, so signing fails when a key is given as a PKCS#11 URI with percent-encoded characters, which is common since token and object labels often contain spaces (e.g. "pkcs11:token=my%20token").
The values written to the CST configuration file never use interpolation, so disable it. This applies to both the i.MX8M and the i.MX93 etypes, which share the configuration code.
Quoted 5 lines
Add a test to check that percent-encoded PKCS#11 URIs given as keys are written unchanged to the CST configuration file.
Signed-off-by Sergio Prado <[email protected]>---
Hi Fabio,
I recently volunteered to look after binman [1], so happy to pick this patch up, otherwise you can take it through NXP.
[1]: https://lore.kernel.org/u-boot/[email protected]/
Signature · 1 line
David Lechner <[email protected]>
Fabio Estevam
reviewerHi David,
Quoted 8 lines · David Lechner
Hi Fabio,
I recently volunteered to look after binman [1], so happy to pick this patch up, otherwise you can take it through NXP.
[1]: https://lore.kernel.org/u-boot/[email protected]/
Reviewed-by David Lechner <[email protected]>
Thanks for volunteering to be a binman maintainer.
Please take this through your tree.
Cheers