Skip to content

Commit c4659fb

Browse files
Adjust Makefile to match more with DTLS client example
1 parent 8b3c7be commit c4659fb

2 files changed

Lines changed: 137 additions & 113 deletions

File tree

hsm/dtls_server/Makefile

Lines changed: 135 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,169 @@
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
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+
# make download_repos # Clone wolfSSL and wolfHSM repos
9+
# make all # Build everything (wolfSSL, wolfHSM server, DTLS server)
10+
# make run_hsm_server # Start wolfHSM server
11+
# make run_dtls_server # Start wolfSSL DTLS server (this example)
12+
# make run_client # Run the wolfSSL DTLS client
13+
# make clean # Clean build artifacts
14+
# make clean_repos # Remove cloned repositories
2415

25-
# Output directory for build files
26-
BUILD_DIR ?= $(PROJECT_DIR)/Build
16+
BIN = wh_server
2717

28-
## Includes
29-
INC = -I$(PROJECT_DIR) \
30-
-I$(CONFIG_DIR) \
31-
-I$(WOLFSSL_DIR) \
32-
-I$(WOLFHSM_DIR) \
33-
-I$(WOLFHSM_PORT_DIR)
18+
WOLFSSL_DIR ?= ./wolfssl
19+
WOLFHSM_DIR ?= ./wolfhsm
20+
WOLFHSM_PORT_DIR = $(WOLFHSM_DIR)/port/posix
21+
WOLFHSM_SERVER_DIR = $(WOLFHSM_DIR)/examples/posix/wh_posix_server
3422

35-
## Defines
36-
# POSIX requires C source be defined before any header
37-
DEF += -D_POSIX_C_SOURCE=200809L
23+
PROJECT_DIR = .
24+
CONFIG_DIR = $(PROJECT_DIR)/config
25+
BUILD_DIR = $(PROJECT_DIR)/Build
3826

39-
# Library configuration defines for user-supplied settings
40-
DEF += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG
27+
# Compiler settings
28+
CC = gcc
29+
CSTD = -std=c99
30+
CFLAGS_EXTRA = -Werror -Wall -Wextra -ffunction-sections -fdata-sections
31+
CFLAGS = $(CSTD) $(CFLAGS_EXTRA)
4132

42-
# Enable DMA transport by default (matches server --type dma)
33+
# Defines
34+
DEF = -D_POSIX_C_SOURCE=200809L -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG
4335
DEF += -DWOLFHSM_CFG_DMA
4436

45-
## Architecture flags
46-
ARCHFLAGS ?=
37+
# Includes
38+
INC = -I$(PROJECT_DIR) -I$(CONFIG_DIR) -I$(WOLFSSL_DIR) -I$(WOLFHSM_DIR) -I$(WOLFHSM_PORT_DIR)
4739

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
40+
# Linker settings (platform-specific: darwin uses -dead_strip, others use --gc-sections)
5641
OS_NAME := $(shell uname -s | tr A-Z a-z)
5742
ifeq ($(OS_NAME),darwin)
58-
LDFLAGS += -Wl,-dead_strip
43+
LDFLAGS = -Wl,-dead_strip
5944
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
45+
LDFLAGS = -Wl,--gc-sections
8246
endif
47+
LIBS = -lc -lm
8348

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
49+
# Source files (wolfCrypt, wolfSSL, wolfHSM, port, project)
50+
SRC_C = $(wildcard $(WOLFSSL_DIR)/wolfcrypt/src/*.c)
9651
SRC_C += $(wildcard $(WOLFSSL_DIR)/src/*.c)
97-
98-
# wolfHSM source files
9952
SRC_C += $(wildcard $(WOLFHSM_DIR)/src/*.c)
100-
101-
# wolfHSM POSIX port/HAL code
10253
SRC_C += $(wildcard $(WOLFHSM_PORT_DIR)/*.c)
54+
SRC_C += $(PROJECT_DIR)/server.c $(PROJECT_DIR)/server_io.c
10355

104-
# Project source files
105-
SRC_C += $(PROJECT_DIR)/server.c
106-
SRC_C += $(PROJECT_DIR)/server_io.c
107-
108-
## Automated processing
56+
# Debug support
57+
ifeq ($(DEBUG),1)
58+
CFLAGS += -ggdb -g3
59+
LDFLAGS += -ggdb -g3
60+
DEF += -DWOLFHSM_CFG_DEBUG
61+
endif
10962

63+
# Object files
11064
FILENAMES_C = $(notdir $(SRC_C))
11165
OBJS_C = $(addprefix $(BUILD_DIR)/, $(FILENAMES_C:.c=.o))
11266
vpath %.c $(dir $(SRC_C))
11367

114-
## Makefile Targets
115-
116-
.PHONY: all build clean help
117-
118-
all: build
119-
120-
build: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).elf
68+
# Phony targets
69+
.PHONY: all download_repos build_wolfssl build_wolfhsm_server build_app run_hsm_server run_dtls_server run_client clean clean_repos
70+
71+
# Default target
72+
all: check_repos build_wolfssl build_wolfhsm_server build_app
73+
@echo "Build complete. Run 'make run_hsm_server', 'make run_dtls_server', 'make run_client' in separate terminals."
74+
75+
# Clone repositories
76+
download_repos:
77+
@echo "=== Cloning repositories ==="
78+
@if [ ! -d "$(WOLFSSL_DIR)" ]; then \
79+
git clone --depth 1 https://github.com/wolfssl/wolfssl.git $(WOLFSSL_DIR); \
80+
else \
81+
echo "wolfssl already exists, skipping clone"; \
82+
fi
83+
@if [ ! -d "$(WOLFHSM_DIR)" ]; then \
84+
git clone --depth 1 https://github.com/wolfssl/wolfhsm.git $(WOLFHSM_DIR); \
85+
else \
86+
echo "wolfhsm already exists, skipping clone"; \
87+
fi
88+
89+
# Check that repos exist
90+
check_repos:
91+
@if [ ! -d "$(WOLFSSL_DIR)" ] || [ ! -d "$(WOLFHSM_DIR)" ]; then \
92+
echo "Error: Repositories not found. Run 'make download_repos' first."; \
93+
exit 1; \
94+
fi
95+
96+
# Build wolfSSL (for example client)
97+
build_wolfssl: check_repos
98+
@echo "=== Building wolfSSL ==="
99+
@if [ ! -f "$(WOLFSSL_DIR)/examples/server/server" ]; then \
100+
cd $(WOLFSSL_DIR) && \
101+
./autogen.sh && \
102+
./configure --enable-dtls --enable-dtls13 --enable-ecc && \
103+
make -j; \
104+
else \
105+
echo "wolfSSL already built, skipping"; \
106+
fi
107+
108+
# Build wolfHSM POSIX server (with DMA for this example)
109+
# Note: The wolfHSM server Makefile expects WOLFSSL_DIR relative to its location
110+
# Server is at ./wolfhsm/examples/posix/wh_posix_server/
111+
# wolfssl is at ./wolfssl/
112+
# So from server: ../../../../wolfssl
113+
build_wolfhsm_server: check_repos
114+
@echo "=== Building wolfHSM server ==="
115+
@if [ ! -f "$(WOLFHSM_SERVER_DIR)/Build/wh_posix_server.elf" ]; then \
116+
$(MAKE) -C $(WOLFHSM_SERVER_DIR) clean || true; \
117+
$(MAKE) -C $(WOLFHSM_SERVER_DIR) WOLFSSL_DIR=../../../../wolfssl DMA=1 -j; \
118+
else \
119+
echo "wolfHSM server already built, skipping"; \
120+
fi
121+
122+
# Build DTLS server
123+
build_app: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).elf
124+
@echo "DTLS server built: $(BUILD_DIR)/$(BIN).elf"
121125

122126
$(BUILD_DIR):
123-
$(CMD_ECHO) mkdir -p $(BUILD_DIR)
127+
mkdir -p $(BUILD_DIR)
124128

125129
$(BUILD_DIR)/%.o: %.c
126130
@echo "Compiling: $(notdir $<)"
127-
$(CMD_ECHO) $(CC) $(CFLAGS) $(DEF) $(INC) -c -o $@ $<
131+
$(CC) $(CFLAGS) $(DEF) $(INC) -c -o $@ $<
128132

129133
$(BUILD_DIR)/$(BIN).elf: $(OBJS_C)
130134
@echo "Linking: $(notdir $@)"
131-
$(CMD_ECHO) $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
135+
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
132136

133-
clean:
134-
@echo "Cleaning build files..."
135-
@rm -rf $(BUILD_DIR)
137+
# Convenience targets for running each component in separate terminals
136138

137-
help:
138-
@echo "TLS/DTLS Server with wolfHSM Crypto Offload"
139+
run_hsm_server: all
140+
@echo "Starting wolfHSM server..."
141+
@echo "Press Ctrl+C to stop"
139142
@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"
143+
$(WOLFHSM_SERVER_DIR)/Build/wh_posix_server.elf --type dma
144+
145+
run_dtls_server: all
146+
@echo "Starting wolfSSL DTLS server (wolfHSM crypto offload)..."
147+
@echo "Press Ctrl+C to stop"
144148
@echo ""
145-
@echo "Example:"
146-
@echo " make DEBUG=1"
149+
$(BUILD_DIR)/$(BIN).elf -p 11111 -A $(WOLFSSL_DIR)/certs/client-cert.pem
150+
151+
run_client: all
152+
cd $(WOLFSSL_DIR) && ./examples/client/client -u -v 4 -h 127.0.0.1 -p 11111
153+
154+
# Clean build artifacts
155+
clean:
156+
@echo "Cleaning build files..."
157+
rm -rf $(BUILD_DIR)
158+
@# Clean wolfHSM server build
159+
@if [ -d "$(WOLFHSM_SERVER_DIR)" ]; then \
160+
$(MAKE) -C $(WOLFHSM_SERVER_DIR) clean 2>/dev/null || true; \
161+
fi
162+
@# Clean wolfSSL build
163+
@if [ -f "$(WOLFSSL_DIR)/Makefile" ]; then \
164+
$(MAKE) -C $(WOLFSSL_DIR) clean 2>/dev/null || true; \
165+
fi
166+
167+
clean_repos: clean
168+
@echo "Removing cloned repositories"
169+
rm -rf $(WOLFSSL_DIR) $(WOLFHSM_DIR)

hsm/dtls_server/server.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ int main(int argc, char** argv)
214214
}
215215
}
216216

217-
printf("DTLS server starting on port %d...\n", config.port);
217+
printf("Example DTLS server using wolfHSM is starting on port %d\n",
218+
config.port);
218219

219220
/* Initialize wolfCrypt */
220221
ret = wolfCrypt_Init();

0 commit comments

Comments
 (0)