Skip to content

Commit 3f3554e

Browse files
committed
Add support for wolfHSM multi-root certificate verification
1 parent 7b0bee5 commit 3f3554e

6 files changed

Lines changed: 50 additions & 19 deletions

File tree

docs/wolfHSM.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ To use certificate verification with wolfHSM:
5858

5959
1. Enable `WOLFBOOT_CERT_CHAIN_VERIFY` in your wolfBoot configuration
6060
2. Ensure the wolfHSM server is configured with certificate manager support (`WOLFHSM_CFG_CERTIFICATE_MANAGER`)
61-
3. Pre-provision the root CA certificate on the wolfHSM server at the NVM ID specified by the HAL `hsmNvmIdCertRootCA`
61+
3. Pre-provision one or more root CA certificates on the wolfHSM server at the NVM IDs listed in the HAL `hsmNvmIdCertRootCAList`. Verification succeeds if the embedded chain anchors to *any* root in the list (absent NVM IDs are silently skipped). The list length must not exceed `WOLFHSM_CFG_CERT_MAX_VERIFY_ROOTS` (default 8).
6262
4. Sign firmware images with the `--cert-chain` option, providing a DER-encoded certificate chain
6363

6464
To build the simulator using wolfHSM for certificate verification:
@@ -96,6 +96,7 @@ In addition to the standard wolfBoot HAL functions, wolfHSM-enabled platforms mu
9696
- `hsmDevIdHash`: The HSM device ID for hash operations. This is used to identify the HSM device to wolfBoot.
9797
- `hsmDevIdPubKey`: The HSM device ID for public key operations. This is used to identify the HSM device to wolfBoot.
9898
- `hsmKeyIdPubKey`: The HSM key ID for public key operations. This is used to identify the key to use for public key operations.
99+
- `hsmNvmIdCertRootCAList` / `hsmNvmIdCertRootCACount`: Array of NVM IDs identifying the trusted root CA certificate(s) and its element count. Only used when building with `WOLFBOOT_CERT_CHAIN_VERIFY`. The chain in the firmware header may anchor to any of the listed roots; the count is bounded by `WOLFHSM_CFG_CERT_MAX_VERIFY_ROOTS` (default 8). Each in-tree HAL provides a default of `{ 1 }`; override at build time by passing a comma-separated initializer in `WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST`, e.g. `make CFLAGS_EXTRA='-DWOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST="1, 2, 3"'`.
99100

100101
### Client HAL Functions
101102

hal/aurix_tc3xx.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,27 @@ const int hsmDevIdCrypt = WH_DEV_ID;
111111
const int hsmKeyIdCrypt = 0xFF;
112112
#endif
113113
#ifdef WOLFBOOT_CERT_CHAIN_VERIFY
114-
const whNvmId hsmNvmIdCertRootCA = 1;
114+
/* Override at build time, e.g. -DWOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST="1, 2, 3" */
115+
#ifndef WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST
116+
#define WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST 1
117+
#endif
118+
const whNvmId hsmNvmIdCertRootCAList[] = { WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST };
119+
const uint16_t hsmNvmIdCertRootCACount =
120+
sizeof(hsmNvmIdCertRootCAList) / sizeof(hsmNvmIdCertRootCAList[0]);
115121
#endif
116122

117123
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
118124

119125
/* map wolfBoot HAL layer wofHSM exports to their tchsm config vals */
120-
const int hsmDevIdHash = HSM_DEVID;
121-
const int hsmDevIdPubKey = HSM_DEVID;
122-
const whNvmId hsmNvmIdCertRootCA = 1;
126+
const int hsmDevIdHash = HSM_DEVID;
127+
const int hsmDevIdPubKey = HSM_DEVID;
128+
/* Override at build time, e.g. -DWOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST="1, 2, 3" */
129+
#ifndef WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST
130+
#define WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST 1
131+
#endif
132+
const whNvmId hsmNvmIdCertRootCAList[] = { WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST };
133+
const uint16_t hsmNvmIdCertRootCACount =
134+
sizeof(hsmNvmIdCertRootCAList) / sizeof(hsmNvmIdCertRootCAList[0]);
123135
#ifdef EXT_ENCRYPT
124136
#error "AURIX does not support firmware encryption with wolfHSM(yet)"
125137
const int hsmDevIdCrypt = INVALID_DEVID; /*HSM_DEVID once CCB enabled*/

hal/sim.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ const int hsmDevIdCrypt = WH_DEV_ID;
191191
const int hsmKeyIdCrypt = 0xFF;
192192
#endif
193193
#ifdef WOLFBOOT_CERT_CHAIN_VERIFY
194-
const whNvmId hsmNvmIdCertRootCA = 1;
194+
/* Override at build time, e.g. -DWOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST="1, 2, 3" */
195+
#ifndef WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST
196+
#define WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST 1
197+
#endif
198+
const whNvmId hsmNvmIdCertRootCAList[] = { WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST };
199+
const uint16_t hsmNvmIdCertRootCACount =
200+
sizeof(hsmNvmIdCertRootCAList) / sizeof(hsmNvmIdCertRootCAList[0]);
195201
#endif
196202

197203
int hal_hsm_init_connect(void);
@@ -257,9 +263,15 @@ whServerConfig s_conf[1] = {{
257263

258264
whServerContext hsmServerCtx = {0};
259265

260-
const int hsmDevIdHash = INVALID_DEVID;
261-
const int hsmDevIdPubKey = INVALID_DEVID;
262-
const whNvmId hsmNvmIdCertRootCA = 1;
266+
const int hsmDevIdHash = INVALID_DEVID;
267+
const int hsmDevIdPubKey = INVALID_DEVID;
268+
/* Override at build time, e.g. -DWOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST="1, 2, 3" */
269+
#ifndef WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST
270+
#define WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST 1
271+
#endif
272+
const whNvmId hsmNvmIdCertRootCAList[] = { WOLFBOOT_WOLFHSM_NVM_ROOT_CA_LIST };
273+
const uint16_t hsmNvmIdCertRootCACount =
274+
sizeof(hsmNvmIdCertRootCAList) / sizeof(hsmNvmIdCertRootCAList[0]);
263275
#ifdef EXT_ENCRYPT
264276
#error "Simulator does not support firmware encryption with wolfHSM(yet)"
265277
const int hsmDevIdCrypt = WH_DEV_ID;

include/hal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ extern const int hsmDevIdCrypt; /* devId for image (enc)decryption */
221221
extern const int hsmKeyIdCrypt; /* KeyId for image (enc/dec)ryption */
222222
#endif
223223
#ifdef WOLFBOOT_CERT_CHAIN_VERIFY
224-
/* NvmId for trusted root CA certificate */
225-
extern const whNvmId hsmNvmIdCertRootCA;
224+
/* List of NvmIds for trusted root CA certificates. Verification succeeds if
225+
* the cert chain anchors to any root in the list. The list length must not
226+
* exceed WOLFHSM_CFG_CERT_MAX_VERIFY_ROOTS. */
227+
extern const whNvmId hsmNvmIdCertRootCAList[];
228+
extern const uint16_t hsmNvmIdCertRootCACount;
226229
#endif
227230

228231
#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT || WOLFBOOT_ENABLE_WOLFHSM_SERVER */

lib/wolfHSM

Submodule wolfHSM updated 130 files

src/image.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,25 +2319,28 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
23192319
#if defined(WOLFHSM_CFG_DMA)
23202320
wolfBoot_printf(
23212321
"verifying cert chain and caching leaf pubkey (using DMA)\n");
2322-
hsm_ret = wh_Client_CertVerifyDmaAndCacheLeafPubKey(
2323-
&hsmClientCtx, cert_chain, cert_chain_size, hsmNvmIdCertRootCA,
2322+
hsm_ret = wh_Client_CertVerifyMultiRootDmaAndCacheLeafPubKey(
2323+
&hsmClientCtx, cert_chain, cert_chain_size,
2324+
hsmNvmIdCertRootCAList, hsmNvmIdCertRootCACount,
23242325
WH_NVM_FLAGS_USAGE_VERIFY, &g_certLeafKeyId, &cert_verify_result);
23252326
#else
23262327
wolfBoot_printf("verifying cert chain and caching leaf pubkey\n");
2327-
hsm_ret = wh_Client_CertVerifyAndCacheLeafPubKey(
2328-
&hsmClientCtx, cert_chain, cert_chain_size, hsmNvmIdCertRootCA,
2328+
hsm_ret = wh_Client_CertVerifyMultiRootAndCacheLeafPubKey(
2329+
&hsmClientCtx, cert_chain, cert_chain_size,
2330+
hsmNvmIdCertRootCAList, hsmNvmIdCertRootCACount,
23292331
WH_NVM_FLAGS_USAGE_VERIFY, &g_certLeafKeyId, &cert_verify_result);
23302332
#endif
23312333
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
23322334
wolfBoot_printf("verifying cert chain and caching leaf pubkey\n");
2333-
hsm_ret = wh_Server_CertVerify(
2334-
&hsmServerCtx, cert_chain, cert_chain_size, hsmNvmIdCertRootCA,
2335+
hsm_ret = wh_Server_CertVerifyMultiRoot(
2336+
&hsmServerCtx, cert_chain, cert_chain_size,
2337+
hsmNvmIdCertRootCAList, hsmNvmIdCertRootCACount,
23352338
WH_CERT_FLAGS_CACHE_LEAF_PUBKEY, WH_NVM_FLAGS_USAGE_VERIFY,
23362339
&g_certLeafKeyId);
23372340
if (hsm_ret == WH_ERROR_OK) {
23382341
cert_verify_result = 0;
23392342
}
2340-
wolfBoot_printf("wh_Server_CertVerify returned %d\n", hsm_ret);
2343+
wolfBoot_printf("wh_Server_CertVerifyMultiRoot returned %d\n", hsm_ret);
23412344
#endif
23422345

23432346
/* Error or verification failure results in standard auth check failure

0 commit comments

Comments
 (0)