Skip to content

Commit 37f77f7

Browse files
add demo for DTLS server that offloads all crypto to wolfHSM server
1 parent 4e1a7d4 commit 37f77f7

8 files changed

Lines changed: 1410 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: DTLS Demo Test
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
jobs:
10+
dtls-demo:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout wolfHSM
15+
uses: actions/checkout@v4
16+
17+
- name: Checkout wolfSSL
18+
uses: actions/checkout@v4
19+
with:
20+
repository: wolfssl/wolfssl
21+
path: wolfssl
22+
23+
- name: Build wolfHSM POSIX server
24+
run: |
25+
cd examples/posix/wh_posix_server
26+
make -j DMA=1 WOLFSSL_DIR=../../../wolfssl
27+
28+
- name: Build DTLS server demo
29+
run: |
30+
cd examples/demo/dtls_server
31+
make -j WOLFSSL_DIR=../../../wolfssl
32+
33+
- name: Build wolfSSL with DTLS 1.3 support
34+
run: |
35+
cd wolfssl
36+
./autogen.sh
37+
./configure --enable-dtls --enable-dtls13
38+
make -j
39+
40+
- name: Run DTLS demo test
41+
run: |
42+
# Start the wolfHSM POSIX server in background
43+
cd examples/posix/wh_posix_server
44+
./Build/wh_posix_server.elf --type dma &
45+
WH_SERVER_PID=$!
46+
cd ../../..
47+
48+
# Give the server time to start
49+
sleep 1
50+
51+
# Start the DTLS server demo in background
52+
cd examples/demo/dtls_server
53+
./Build/wh_server.elf -A ../../../wolfssl/certs/client-cert.pem &
54+
DTLS_SERVER_PID=$!
55+
cd ../../..
56+
57+
# Give the DTLS server time to start
58+
sleep 1
59+
60+
# Run the wolfSSL client to connect
61+
cd wolfssl
62+
timeout 10 ./examples/client/client -u -v 4 || CLIENT_EXIT=$?
63+
64+
# Clean up background processes
65+
kill $DTLS_SERVER_PID 2>/dev/null || true
66+
kill $WH_SERVER_PID 2>/dev/null || true
67+
68+
# Check if client succeeded (exit code 0) or timed out gracefully
69+
if [ "${CLIENT_EXIT:-0}" -eq 0 ] || [ "${CLIENT_EXIT:-0}" -eq 124 ]; then
70+
echo "DTLS demo test passed"
71+
exit 0
72+
else
73+
echo "DTLS demo test failed with exit code $CLIENT_EXIT"
74+
exit 1
75+
fi

examples/demo/dtls_server/Makefile

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
## Makefile for TLS/DTLS Server using wolfHSM for crypto operations
2+
##
3+
## This example demonstrates a server that offloads all cryptographic
4+
## operations to a wolfHSM server running on the POSIX transport with
5+
## DMA support. By default, DTLS (UDP) mode is used.
6+
##
7+
## Usage:
8+
## 1. Build: make DEBUG=1
9+
## 2. Start the wolfHSM server: cd ../../posix/wh_posix_server && ./Build/wh_posix_server.elf --type dma
10+
## 3. Run this server: ./Build/wh_server.elf
11+
## 4. Connect with a client
12+
13+
## Project name - sets output filename
14+
BIN = wh_server
15+
16+
## Important directories
17+
PROJECT_DIR ?= .
18+
CONFIG_DIR ?= $(PROJECT_DIR)/config
19+
20+
# wolfSSL and wolfHSM directories (relative to this Makefile)
21+
WOLFSSL_DIR ?= ../../../../wolfssl
22+
WOLFHSM_DIR ?= ../../..
23+
WOLFHSM_PORT_DIR ?= $(WOLFHSM_DIR)/port/posix
24+
25+
# Output directory for build files
26+
BUILD_DIR ?= $(PROJECT_DIR)/Build
27+
28+
## Includes
29+
INC = -I$(PROJECT_DIR) \
30+
-I$(CONFIG_DIR) \
31+
-I$(WOLFSSL_DIR) \
32+
-I$(WOLFHSM_DIR) \
33+
-I$(WOLFHSM_PORT_DIR)
34+
35+
## Defines
36+
# POSIX requires C source be defined before any header
37+
DEF += -D_POSIX_C_SOURCE=200809L
38+
39+
# Library configuration defines for user-supplied settings
40+
DEF += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG
41+
42+
# Enable DMA transport by default (matches server --type dma)
43+
DEF += -DWOLFHSM_CFG_DMA
44+
45+
## Architecture flags
46+
ARCHFLAGS ?=
47+
48+
## Compiler and linker flags
49+
ASFLAGS ?= $(ARCHFLAGS)
50+
CFLAGS_EXTRA ?= -Wextra
51+
CFLAGS ?= $(ARCHFLAGS) -Wno-cpp -std=c99 -Wall -Werror $(CFLAGS_EXTRA)
52+
LDFLAGS ?= $(ARCHFLAGS)
53+
LIBS = -lc -lm
54+
55+
# Platform-specific linker flags for dead code stripping
56+
OS_NAME := $(shell uname -s | tr A-Z a-z)
57+
ifeq ($(OS_NAME),darwin)
58+
LDFLAGS += -Wl,-dead_strip
59+
else
60+
LDFLAGS += -Wl,--gc-sections
61+
endif
62+
63+
## Makefile options
64+
65+
# Set to @ to suppress command echo
66+
CMD_ECHO ?=
67+
68+
# Debug build
69+
ifeq ($(DEBUG),1)
70+
DBGFLAGS = -ggdb -g3 -O0
71+
CFLAGS += $(DBGFLAGS)
72+
LDFLAGS += $(DBGFLAGS)
73+
DEF += -DWOLFHSM_CFG_DEBUG
74+
endif
75+
76+
# Verbose debug output
77+
ifeq ($(DEBUG_VERBOSE),1)
78+
DBGFLAGS = -ggdb -g3 -O0
79+
CFLAGS += $(DBGFLAGS)
80+
LDFLAGS += $(DBGFLAGS)
81+
DEF += -DWOLFHSM_CFG_DEBUG -DWOLFHSM_CFG_DEBUG_VERBOSE
82+
endif
83+
84+
# Address sanitizer
85+
ifeq ($(ASAN),1)
86+
CFLAGS += -fsanitize=address
87+
LDFLAGS += -fsanitize=address
88+
endif
89+
90+
## Source files
91+
92+
# wolfCrypt source files
93+
SRC_C += $(wildcard $(WOLFSSL_DIR)/wolfcrypt/src/*.c)
94+
95+
# wolfSSL TLS source files
96+
SRC_C += $(wildcard $(WOLFSSL_DIR)/src/*.c)
97+
98+
# wolfHSM source files
99+
SRC_C += $(wildcard $(WOLFHSM_DIR)/src/*.c)
100+
101+
# wolfHSM POSIX port/HAL code
102+
SRC_C += $(wildcard $(WOLFHSM_PORT_DIR)/*.c)
103+
104+
# Project source files
105+
SRC_C += $(PROJECT_DIR)/server.c
106+
SRC_C += $(PROJECT_DIR)/server_io.c
107+
108+
## Automated processing
109+
110+
FILENAMES_C = $(notdir $(SRC_C))
111+
OBJS_C = $(addprefix $(BUILD_DIR)/, $(FILENAMES_C:.c=.o))
112+
vpath %.c $(dir $(SRC_C))
113+
114+
## Makefile Targets
115+
116+
.PHONY: all build clean help
117+
118+
all: build
119+
120+
build: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).elf
121+
122+
$(BUILD_DIR):
123+
$(CMD_ECHO) mkdir -p $(BUILD_DIR)
124+
125+
$(BUILD_DIR)/%.o: %.c
126+
@echo "Compiling: $(notdir $<)"
127+
$(CMD_ECHO) $(CC) $(CFLAGS) $(DEF) $(INC) -c -o $@ $<
128+
129+
$(BUILD_DIR)/$(BIN).elf: $(OBJS_C)
130+
@echo "Linking: $(notdir $@)"
131+
$(CMD_ECHO) $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
132+
133+
clean:
134+
@echo "Cleaning build files..."
135+
@rm -rf $(BUILD_DIR)
136+
137+
help:
138+
@echo "TLS/DTLS Server with wolfHSM Crypto Offload"
139+
@echo ""
140+
@echo "Options:"
141+
@echo " DEBUG=1 - Enable debug build with symbols"
142+
@echo " DEBUG_VERBOSE=1 - Enable verbose debug output"
143+
@echo " ASAN=1 - Enable address sanitizer"
144+
@echo ""
145+
@echo "Example:"
146+
@echo " make DEBUG=1"

0 commit comments

Comments
 (0)