Skip to content

Commit c106d92

Browse files
bigbrettdanielinux
authored andcommitted
wolfHSM server support with X509 auth
1 parent 499c081 commit c106d92

20 files changed

+407
-56
lines changed

.github/workflows/test-configs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ jobs:
474474
uses: ./.github/workflows/test-build.yml
475475
with:
476476
arch: host
477-
config-file: ./config/examples/sim-wolfHSM.config
477+
config-file: ./config/examples/sim-wolfHSM-client.config
478478

479479
rp2350_test:
480480
uses: ./.github/workflows/test-build-pico-sdk.yml

.github/workflows/test-wolfhsm-simulator.yml

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ jobs:
1515
matrix:
1616
config:
1717
- name: "Standard wolfHSM"
18-
file: "config/examples/sim-wolfHSM.config"
18+
file: "config/examples/sim-wolfHSM-client.config"
1919
- name: "wolfHSM ML-DSA"
20-
file: "config/examples/sim-wolfHSM-mldsa.config"
20+
file: "config/examples/sim-wolfHSM-client-mldsa.config"
2121
- name: "wolfHSM cert chain verify"
22-
file: "config/examples/sim-wolfHSM-certchain.config"
22+
file: "config/examples/sim-wolfHSM-client-certchain.config"
23+
- name: "wolfHSM server cert chain verify"
24+
file: "config/examples/sim-wolfHSM-server-certchain.config"
25+
2326
fail-fast: false
2427

2528
runs-on: ubuntu-latest
@@ -50,39 +53,42 @@ jobs:
5053
run: |
5154
make clean && make test-sim-internal-flash-with-update
5255
53-
# checkout and build wolfHSM examples repo
54-
- name: Checkout wolfHSM-examples
55-
uses: actions/checkout@v4
56-
with:
57-
repository: wolfssl/wolfHSM-examples
58-
# Make sure to update this when the wolfHSM submodule is updated!
59-
ref: wolfHSM-examples-v1.2.0
60-
path: wolfHSM-examples
61-
6256
- name: Build example POSIX TCP server
63-
run: cd wolfHSM-examples/posix/tcp/wh_server_tcp && make WOLFSSL_DIR=../../../../lib/wolfssl WOLFHSM_DIR=../../../../lib/wolfHSM
57+
if: matrix.config.name != 'wolfHSM server cert chain verify'
58+
run: cd lib/wolfHSM/examples/posix/tcp/wh_server_tcp && make WOLFSSL_DIR=../../../../../wolfssl
6459

6560
# Start the server in the background
6661
- name: Run POSIX TCP server
62+
if: matrix.config.name != 'wolfHSM server cert chain verify'
6763
run: |
68-
cd wolfHSM-examples/posix/tcp/wh_server_tcp
64+
cd lib/wolfHSM/examples/posix/tcp/wh_server_tcp
6965
if [ "${{ matrix.config.name }}" = "wolfHSM cert chain verify" ]; then
7066
tmpfile=$(mktemp)
71-
echo "obj 1 0xFFFF 0x0000 \"cert CA\" ../../../../test-dummy-ca/root-cert.der" >> $tmpfile
67+
echo "obj 1 0xFFFF 0x0000 \"cert CA\" ../../../../../../test-dummy-ca/root-cert.der" >> $tmpfile
7268
./Build/wh_server_tcp.elf --nvminit $tmpfile &
7369
else
74-
./Build/wh_server_tcp.elf --client 12 --id 255 --key ../../../../wolfboot_signing_private_key_pub.der &
70+
./Build/wh_server_tcp.elf --client 12 --id 255 --key ../../../../../../wolfboot_signing_private_key_pub.der &
7571
fi
7672
TCP_SERVER_PID=$!
7773
echo "TCP_SERVER_PID=$TCP_SERVER_PID" >> $GITHUB_ENV
7874
75+
# For testing the wolfHSM server cert chain verify feature, we need to create an NVM image containing our root CA that
76+
# the internal wolfHSM server can load.
77+
- name: Create NVM image for wolfHSM server cert chain verify
78+
if: matrix.config.name == 'wolfHSM server cert chain verify'
79+
run: |
80+
make -C lib/wolfHSM/tools/whnvmtool
81+
tmpfile=$(mktemp)
82+
echo "obj 1 0xFFFF 0x0000 \"cert CA\" test-dummy-ca/root-cert.der" >> $tmpfile
83+
./lib/wolfHSM/tools/whnvmtool/whnvmtool --image=wolfBoot_wolfHSM_NVM.bin --size=16348 --invert-erased-byte $tmpfile
84+
7985
# Run the sunny day update test against the server
8086
- name: Run sunny day update test
8187
run: |
8288
tools/scripts/sim-sunnyday-update.sh
8389
8490
# Kill the server if it is still running
8591
- name: Kill POSIX TCP server
86-
if: always()
92+
if: always() && matrix.config.name != 'wolfHSM server cert chain verify'
8793
run: |
8894
kill $TCP_SERVER_PID || true

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ endif
5454
WOLFCRYPT_OBJS:=
5555
SECURE_OBJS:=
5656
PUBLIC_KEY_OBJS:=
57-
WOLFHSM_CLIENT_OBJS:=
57+
WOLFHSM_OBJS:=
5858
ifneq ("$(NO_LOADER)","1")
5959
OBJS+=./src/loader.o
6060
endif
@@ -67,7 +67,7 @@ include options.mk
6767

6868
OBJS+=$(WOLFCRYPT_OBJS)
6969
OBJS+=$(PUBLIC_KEY_OBJS)
70-
OBJS+=$(WOLFHSM_CLIENT_OBJS)
70+
OBJS+=$(WOLFHSM_OBJS)
7171

7272
CFLAGS+= \
7373
-I"." -I"include/" -I"lib/wolfssl" \

arch.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ ifeq ($(ARCH),sim)
11251125
CFLAGS+=-DWOLFSSL_SP_DIV_WORD_HALF
11261126
endif
11271127
ifeq ($(WOLFHSM_CLIENT),1)
1128-
WOLFHSM_CLIENT_OBJS += $(LIBDIR)/wolfHSM/port/posix/posix_transport_tcp.o
1128+
WOLFHSM_OBJS += $(LIBDIR)/wolfHSM/port/posix/posix_transport_tcp.o
11291129
endif
11301130
endif
11311131

config/examples/sim-wolfHSM-certchain.config renamed to config/examples/sim-wolfHSM-client-certchain.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ SIGN?=ECC256
44
HASH?=SHA256
55
WOLFBOOT_SMALL_STACK?=0
66
SPI_FLASH=0
7-
DEBUG=1
7+
DEBUG=0
8+
SPMATH=1
89

910
# Cert chain options
1011
CERT_CHAIN_VERIFY=1

config/examples/sim-wolfHSM-mldsa.config renamed to config/examples/sim-wolfHSM-client-mldsa.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ SIGN?=ML_DSA
44
HASH?=SHA256
55
WOLFBOOT_SMALL_STACK?=0
66
SPI_FLASH=0
7-
DEBUG=1
7+
DEBUG=0
8+
SPMATH=1
89

910
#
1011
# ML-DSA config examples:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HASH?=SHA256
55
WOLFBOOT_SMALL_STACK?=0
66
SPI_FLASH=0
77
DEBUG=0
8+
SPMATH=1
89

910
# sizes should be multiple of system page size
1011
WOLFBOOT_PARTITION_SIZE=0x100000
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
ARCH=sim
2+
TARGET=sim
3+
SIGN?=ECC256
4+
HASH?=SHA256
5+
WOLFBOOT_SMALL_STACK?=0
6+
SPI_FLASH=0
7+
DEBUG=0
8+
SPMATH=1
9+
10+
# Cert chain options
11+
CERT_CHAIN_VERIFY=1
12+
CERT_CHAIN_GEN=1
13+
14+
# Ensure header is large enough to hold the cert chain (check sign tool output)
15+
# for actual length
16+
IMAGE_HEADER_SIZE=2048
17+
18+
# If SIGN=RSA4096, use the below options
19+
#WOLFBOOT_HUGE_STACK=1
20+
#IMAGE_HEADER_SIZE=4096
21+
22+
# wolfHSM options
23+
WOLFHSM_SERVER=1
24+
25+
# sizes should be multiple of system page size
26+
WOLFBOOT_PARTITION_SIZE=0x200000
27+
WOLFBOOT_SECTOR_SIZE=0x1000
28+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80000
29+
# if on external flash, it should be multiple of system page size
30+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x280000
31+
WOLFBOOT_PARTITION_SWAP_ADDRESS=0x480000
32+
33+
# required for keytools
34+
WOLFBOOT_FIXED_PARTITIONS=1
35+
36+
# For debugging XMALLOC/XFREE
37+
#CFLAGS_EXTRA+=-DWOLFBOOT_DEBUG_MALLOC

hal/sim.c

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@
5151
#include "wolfhsm/wh_error.h"
5252
#include "wolfhsm/wh_client.h"
5353
#include "port/posix/posix_transport_tcp.h"
54-
#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */
54+
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
55+
#include "wolfhsm/wh_error.h"
56+
#include "wolfhsm/wh_server.h"
57+
#include "wolfhsm/wh_server_keystore.h"
58+
#include "wolfhsm/wh_nvm.h"
59+
#include "wolfhsm/wh_nvm_flash.h"
60+
#include "wolfhsm/wh_transport_mem.h"
61+
#include "port/posix/posix_flash_file.h"
62+
#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER */
5563

5664
/* Global pointer to the internal and external flash base */
5765
uint8_t *sim_ram_base;
@@ -107,7 +115,76 @@ const whNvmId hsmClientNvmIdCertRootCA = 1;
107115
int hal_hsm_init_connect(void);
108116
int hal_hsm_disconnect(void);
109117

110-
#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */
118+
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
119+
120+
/* HAL Flash state and configuration */
121+
const whFlashCb fcb[1] = {POSIX_FLASH_FILE_CB};
122+
posixFlashFileContext fc[1] = {0};
123+
posixFlashFileConfig fc_conf[1] = {{
124+
.filename = "wolfBoot_wolfHSM_NVM.bin",
125+
.partition_size = 16384,
126+
.erased_byte = (uint8_t)0,
127+
}};
128+
/* NVM Configuration using PosixSim HAL Flash */
129+
whNvmFlashConfig nf_conf[1] = {{
130+
.cb = fcb,
131+
.context = fc,
132+
.config = fc_conf,
133+
}};
134+
whNvmFlashContext nfc[1] = {0};
135+
whNvmCb nfcb[1] = {WH_NVM_FLASH_CB};
136+
137+
whNvmConfig n_conf[1] = {{
138+
.cb = nfcb,
139+
.context = nfc,
140+
.config = nf_conf,
141+
}};
142+
whNvmContext nvm[1] = {{0}};
143+
144+
static uint8_t req[] = {0};
145+
static uint8_t resp[] = {0};
146+
147+
whTransportMemConfig tmcf[1] = {{
148+
.req = (whTransportMemCsr*)req,
149+
.req_size = sizeof(req),
150+
.resp = (whTransportMemCsr*)resp,
151+
.resp_size = sizeof(resp),
152+
}};
153+
whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB};
154+
whTransportMemServerContext tmsc[1] = {0};
155+
/* Dummy comm server config */
156+
whCommServerConfig cs_conf[1] = {{
157+
.transport_cb = tscb,
158+
.transport_context = &tmsc,
159+
.transport_config = &tmcf,
160+
.server_id = 0,
161+
}};
162+
163+
/* Crypto context */
164+
whServerCryptoContext crypto[1] = {{
165+
.devId = INVALID_DEVID,
166+
}};
167+
168+
#if defined(WOLFHSM_CFG_SHE_EXTENSION)
169+
whServerSheContext she[1] = {{0}};
170+
#endif
171+
172+
whServerConfig s_conf[1] = {{
173+
.comm_config = cs_conf,
174+
.nvm = nvm,
175+
.crypto = crypto,
176+
}};
177+
178+
whServerContext hsmServerCtx = {0};
179+
180+
const int hsmServerDevIdHash = INVALID_DEVID;
181+
const int hsmServerDevIdPubKey = INVALID_DEVID;
182+
const whNvmId hsmServerNvmIdCertRootCA = 1;
183+
184+
int hal_hsm_server_init(void);
185+
int hal_hsm_server_cleanup(void);
186+
187+
#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER*/
111188

112189
static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address)
113190
{
@@ -410,7 +487,6 @@ int wolfBoot_dualboot_candidate(void)
410487

411488
#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
412489

413-
414490
int hal_hsm_init_connect(void)
415491
{
416492
int rc = 0;
@@ -449,4 +525,57 @@ int hal_hsm_disconnect(void)
449525
return rc;
450526
}
451527

452-
#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */
528+
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/
529+
530+
int hal_hsm_server_init(void)
531+
{
532+
int rc = 0;
533+
534+
rc = wh_Nvm_Init(nvm, n_conf);
535+
if (rc != 0) {
536+
fprintf(stderr, "Failed to initialize NVM: %d\n", rc);
537+
exit(-1);
538+
}
539+
540+
wolfCrypt_Init();
541+
542+
rc = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID);
543+
if (rc != 0) {
544+
fprintf(stderr, "Failed to initialize RNG: %d\n", rc);
545+
exit(-1);
546+
}
547+
548+
rc = wh_Server_Init(&hsmServerCtx, s_conf);
549+
if (rc != 0) {
550+
fprintf(stderr, "Failed to initialize HSM server: %d\n", rc);
551+
exit(-1);
552+
}
553+
554+
return rc;
555+
}
556+
557+
int hal_hsm_server_cleanup(void)
558+
{
559+
int rc = 0;
560+
561+
rc = wh_Server_Cleanup(&hsmServerCtx);
562+
if (rc != 0) {
563+
fprintf(stderr, "Failed to cleanup HSM server: %d\n", rc);
564+
exit(-1);
565+
}
566+
567+
rc = wc_FreeRng(crypto->rng);
568+
if (rc != 0) {
569+
fprintf(stderr, "Failed to cleanup RNG: %d\n", rc);
570+
exit(-1);
571+
}
572+
573+
rc = wolfCrypt_Cleanup();
574+
if (rc != 0) {
575+
fprintf(stderr, "Failed to cleanup wolfCrypt: %d\n", rc);
576+
exit(-1);
577+
}
578+
579+
return rc;
580+
}
581+
#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER */

include/hal.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,32 @@ extern const whNvmId hsmClientNvmIdCertRootCA;
170170
/* Implementation of functions provided by HAL */
171171
int hal_hsm_init_connect(void);
172172
int hal_hsm_disconnect(void);
173+
174+
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
175+
176+
#include "wolfhsm/wh_error.h"
177+
#include "wolfhsm/wh_server.h"
178+
#include "wolfhsm/wh_server_crypto.h"
179+
#include "wolfhsm/wh_server_keystore.h"
180+
#if defined(WOLFBOOT_CERT_CHAIN_VERIFY)
181+
#include "wolfhsm/wh_server_cert.h"
182+
#endif
183+
184+
extern whServerContext hsmServerCtx; /* global wolfHSM server context */
185+
186+
extern const int hsmServerDevIdHash; /* devId for image digest */
187+
extern const int hsmServerDevIdPubKey; /* devId for signature verification */
188+
#ifdef EXT_ENCRYPTED
189+
extern const int hsmServerDevIdCrypt; /* devId for image (enc)decryption */
190+
#endif
191+
#ifdef WOLFBOOT_CERT_CHAIN_VERIFY
192+
/* NvmId for trusted root CA certificate */
193+
extern const whNvmId hsmServerNvmIdCertRootCA;
194+
#endif
195+
196+
int hal_hsm_server_init(void);
197+
int hal_hsm_server_cleanup(void);
198+
173199
#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */
174200

175201
#ifdef __cplusplus

0 commit comments

Comments
 (0)