Skip to content

binman: Add HSM signing support to pre-load entry type

yan wang u-boot 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest posting 2/24 patches 4 repliesIn review
  2. v3 posting 1/24 patches 0 repliesSuperseded
  3. v24 patches 4 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v15 patches7 repliesSuperseded

Changes in v3 from the cover letter

- Swap the order of patches 2 and 3: HSM signing support (pre_load) now comes before the key-name/integer-size fix patch. The HSM patch already needs to touch ReadNode() for URI parsing, so grouping the key-name check there with the HSM validation (in the following patch) is cleaner
- Add opensc_version() and _check_opensc_version() to pkcs11_tool; refuse to sign with OpenSC older than 0.15.0 (the first release that accepts 'env:' for --pin); older versions would take 'env:BINMAN_PKCS11_PIN' literally as the PIN and silently fail or lock the token
- Make output_file a required argument of sign() so the caller controls the path; removes the temp-file management from inside the bintool
- When tools.get_env_with_path() returns None, fall back to os.environ so PATH is always inherited by the tool subprocess
- Add _BuildHeader(sign) to pre_load.py, a shared helper that takes a sign() closure; refactor _CreateHeaderRsa() and _CreateHeaderEcdsa() to use it, removing the duplicated struct.pack() and padding logic
- Pre-compute hsm_hash_name, hsm_sign_name, hsm_padding and hsm_sig_size in ReadNode() so _SignHsmData() does not re-parse algo_name on every call
- Use state.GetEntryArg() directly for pre-load-hsm-pin to prevent the PIN from being silently read from a DT property
- Remove the hsm-library DT property from __init__(); the library path is only set by the entry arg or the URI module-path= attribute
- Move the key-name check into the else branch of 'if self.hsm_key_uri:' in ReadNode() so it is validated early, consistently with HSM validation
- Extend the bintool unit tests: OpenSC version parsing and refusal, env handling in run_cmd_result(), PIN passed only through the environment, token serial lookup and resolution

Patches in v3 posting 2/2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4binman: bintool: Add PKCS#11 HSM signing supporttools/binman/bintool.py tools/binman/bintool_test.py tools/binman/btool/pkcs11_tool.py+1032 -71 reply
2/4binman: pre_load: Add HSM signing supporttools/binman/etype/pre_load.py+240 -681 reply
3/4binman: pre_load: Require key-name for file-based signing and fix integer divisiontools/binman/etype/pre_load.py tools/binman/ftest.py tools/binman/test/security/pre_load_missing_key_name.dts+40 -61 reply
4/4binman: Add functional test for pre-load HSM signingtools/binman/ftest.py tools/binman/test/security/pre_load_hsm.dts tools/binman/test/security/pre_load_hsm_dt_pin.dts +7 more+449 -01 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v3 yet.

Read the v3 thread

Cover letter [PATCH v3 0/4 RESEND] binman: Add HSM signing support to pre-load entry type

The pre-load entry type currently only supports signing with a key file on disk. This series lets it sign through a Hardware Security Module (HSM) using PKCS#11, so that the private key never has to leave the token.

The pkcs11_tool bintool gains a sign() method which drives pkcs11-tool (RSA with PKCS#1 v1.5 or PSS, and ECDSA). The pre-load entry selects the HSM with a single hsm-key property holding a PKCS#11 URI (RFC 7512), as is done for FIT signing, e.g.:

   hsm-key = "pkcs11:token=uboot-hsm;object=uboot-key";

The PKCS#11 library and the PIN are not stored in the devicetree. They are passed with -a pre-load-hsm-library and -a pre-load-hsm-pin (the PIN may also come from PKCS11_SIGN_PIN). The library can also be given by the module-path attribute of the URI.

The PIN is forwarded to pkcs11-tool with '--pin env:BINMAN_PKCS11_PIN', which requires OpenSC 0.15.0 or later. A version check is added to the bintool and an informative error is raised if an older release is found.

The functional tests use SoftHSM.

Changes in v3:
- Swap the order of patches 2 and 3: HSM signing support (pre_load) now comes before the key-name/integer-size fix patch. The HSM patch already needs to touch ReadNode() for URI parsing, so grouping the key-name check there with the HSM validation (in the following patch) is cleaner
- Add opensc_version() and _check_opensc_version() to pkcs11_tool; refuse to sign with OpenSC older than 0.15.0 (the first release that accepts 'env:' for --pin); older versions would take 'env:BINMAN_PKCS11_PIN' literally as the PIN and silently fail or lock the token
- Make output_file a required argument of sign() so the caller controls the path; removes the temp-file management from inside the bintool
- When tools.get_env_with_path() returns None, fall back to os.environ so PATH is always inherited by the tool subprocess
- Add _BuildHeader(sign) to pre_load.py, a shared helper that takes a sign() closure; refactor _CreateHeaderRsa() and _CreateHeaderEcdsa() to use it, removing the duplicated struct.pack() and padding logic
- Pre-compute hsm_hash_name, hsm_sign_name, hsm_padding and hsm_sig_size in ReadNode() so _SignHsmData() does not re-parse algo_name on every call
- Use state.GetEntryArg() directly for pre-load-hsm-pin to prevent the PIN from being silently read from a DT property
- Remove the hsm-library DT property from __init__(); the library path is only set by the entry arg or the URI module-path= attribute
- Move the key-name check into the else branch of 'if self.hsm_key_uri:' in ReadNode() so it is validated early, consistently with HSM validation
- Extend the bintool unit tests: OpenSC version parsing and refusal, env handling in run_cmd_result(), PIN passed only through the environment, token serial lookup and resolution

yan wang (4):
binman: bintool: Add PKCS#11 HSM signing support
binman: pre_load: Add HSM signing support
binman: pre_load: Require key-name for file-based signing and fix integer division
binman: Add functional test for pre-load HSM signing

 tools/binman/bintool.py                       |  10 +-
 tools/binman/bintool_test.py                  | 650 ++++++++++++++++++
 tools/binman/btool/pkcs11_tool.py             | 379 +++++++++-
 tools/binman/etype/pre_load.py                | 323 +++++++--
 tools/binman/ftest.py                         | 268 ++++++++
 tools/binman/test/security/pre_load_hsm.dts   |  21 +
 .../test/security/pre_load_hsm_dt_pin.dts     |  22 +
 .../test/security/pre_load_hsm_ecdsa.dts      |  21 +
 .../security/pre_load_hsm_invalid_algo.dts    |  21 +
 .../security/pre_load_hsm_invalid_padding.dts |  22 +
 .../security/pre_load_hsm_invalid_sha.dts     |  21 +
 .../security/pre_load_hsm_invalid_uri.dts     |  21 +
 .../security/pre_load_hsm_module_path.dts     |  21 +
 .../test/security/pre_load_hsm_sign_pss.dts   |  22 +
 .../security/pre_load_missing_key_name.dts    |  20 +
 15 files changed, 1761 insertions(+), 81 deletions(-)
 create mode 100644 tools/binman/test/security/pre_load_hsm.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_dt_pin.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_ecdsa.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_invalid_algo.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_invalid_padding.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_invalid_sha.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_invalid_uri.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_module_path.dts
 create mode 100644 tools/binman/test/security/pre_load_hsm_sign_pss.dts
 create mode 100644 tools/binman/test/security/pre_load_missing_key_name.dts
Signature · 1 line
2.43.0