Skip to content

Commit 7ed8ade

Browse files
add test case for DTLS demo
1 parent 7f9ed6e commit 7ed8ade

7 files changed

Lines changed: 109 additions & 31 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ASFLAGS ?= $(ARCHFLAGS)
5050
CFLAGS_EXTRA ?= -Wextra
5151
CFLAGS ?= $(ARCHFLAGS) -Wno-cpp -std=c99 -Wall -Werror $(CFLAGS_EXTRA)
5252
LDFLAGS ?= $(ARCHFLAGS)
53+
LIBS = -lc -lm
5354

5455
# Platform-specific linker flags for dead code stripping
5556
OS_NAME := $(shell uname -s | tr A-Z a-z)

examples/demo/dtls_server/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This example demonstrates a TLS/DTLS server that offloads cryptographic
44
operations to a wolfHSM server. By default, DTLS (UDP-based) is used, but
55
the code can be adapted for TLS (TCP-based) connections.
66

7-
The wolfHSM server runs separately and communicates via the choosen transport.
7+
The wolfHSM server runs separately and communicates via the chosen transport.
88

99
## Architecture
1010

@@ -210,7 +210,7 @@ the DTLS server.
210210

211211
To use TLS instead of DTLS:
212212

213-
1. In `server_io.c`, change the method from `wolfDTLSv1_3_server_method()` to
213+
1. In `server_io.c`, change the method from `wolfDTLS_server_method()` to
214214
`wolfTLSv1_3_server_method()` or another TLS method.
215215

216216
2. Change the socket initialization from UDP to TCP (replace

examples/demo/dtls_server/config/user_settings.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,6 @@ extern "C" {
115115
#define NO_PSK
116116
#define NO_OLD_TLS
117117

118-
/* Note: Static memory is NOT enabled here.
119-
* DMA operations work without WOLFSSL_STATIC_MEMORY since
120-
* wolfHSM handles the DMA memory regions separately.
121-
*/
122-
123-
/* POSIX version of strcasecmp */
124-
#include <strings.h>
125-
126118
/* Test certificate buffers for demo */
127119
#define USE_CERT_BUFFERS_256
128120
#define USE_CERT_BUFFERS_2048
@@ -132,6 +124,10 @@ extern "C" {
132124
#define DEBUG_WOLFSSL
133125
#endif
134126

127+
/* Include for POSIX extensions needed by wolfSSL */
128+
#include <sys/time.h> /* for struct timeval, gettimeofday */
129+
#include <strings.h> /* for strcasecmp */
130+
135131
#ifdef __cplusplus
136132
}
137133
#endif

examples/demo/dtls_server/config/wolfhsm_cfg.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,4 @@
3838
/* Enable global keys feature */
3939
#define WOLFHSM_CFG_GLOBAL_KEYS
4040

41-
/* Printf function for debug output */
42-
#define WOLFHSM_CFG_PRINTF printf
43-
4441
#endif /* WOLFHSM_CFG_H_ */

examples/demo/dtls_server/server.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@
3030
#include "wolfssl/wolfcrypt/settings.h"
3131
#include "wolfssl/wolfcrypt/cryptocb.h"
3232

33-
/* Shared memory configuration - must match the posix server with --type dma */
34-
#define WH_SERVER_SHARED_MEMORY_NAME "wh_example_shm"
35-
#define WH_SERVER_CLIENT_ID 1
36-
#define WH_SERVER_REQ_SIZE 2048
37-
#define WH_SERVER_RESP_SIZE 2048
38-
#define WH_SERVER_DMA_SIZE 8000
33+
/* Shared POSIX example configuration */
34+
#include "examples/posix/wh_posix_cfg.h"
3935

4036
/* Global wolfHSM client context */
4137
static whClientContext g_client[1] = {{0}};
@@ -68,16 +64,16 @@ static int connect_to_hsm_server(void)
6864
memset(&g_client_config, 0, sizeof(g_client_config));
6965

7066
/* Configure shared memory transport with DMA */
71-
g_shm_config.name = WH_SERVER_SHARED_MEMORY_NAME;
72-
g_shm_config.req_size = WH_SERVER_REQ_SIZE;
73-
g_shm_config.resp_size = WH_SERVER_RESP_SIZE;
74-
g_shm_config.dma_size = WH_SERVER_DMA_SIZE;
67+
g_shm_config.name = WH_POSIX_SHARED_MEMORY_NAME;
68+
g_shm_config.req_size = WH_POSIX_REQ_SIZE;
69+
g_shm_config.resp_size = WH_POSIX_RESP_SIZE;
70+
g_shm_config.dma_size = WH_POSIX_DMA_SIZE;
7571

7672
/* Configure comm layer */
7773
g_comm_config.transport_cb = &shm_cb;
7874
g_comm_config.transport_context = (void*)&g_shm_client_ctx;
7975
g_comm_config.transport_config = (void*)&g_shm_config;
80-
g_comm_config.client_id = WH_SERVER_CLIENT_ID;
76+
g_comm_config.client_id = WH_POSIX_CLIENT_ID;
8177

8278
#ifdef WOLFHSM_CFG_DMA
8379
/* Configure DMA callbacks for static memory operations */
@@ -145,12 +141,6 @@ static void disconnect_from_hsm_server(void)
145141
wh_Client_Cleanup(g_client);
146142
}
147143

148-
/* Get the wolfHSM client context for crypto operations */
149-
whClientContext* get_wolfhsm_client(void)
150-
{
151-
return g_client;
152-
}
153-
154144
/* Print usage information */
155145
static void print_usage(const char* progname)
156146
{

examples/demo/dtls_server/server_io.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/* Standard includes */
2424
#include <stdio.h>
2525
#include <string.h>
26+
#include <limits.h>
2627
#include <sys/socket.h>
2728
#include <netinet/in.h>
2829
#include <arpa/inet.h>
@@ -55,7 +56,12 @@ struct SERVER_CONTEXT {
5556
};
5657

5758
/* Single static instance of the server */
58-
static SERVER_CONTEXT g_server = {0};
59+
static SERVER_CONTEXT g_server = {
60+
.ssl = NULL,
61+
.hsm_client = NULL,
62+
.listenfd = -1,
63+
.port = 0
64+
};
5965

6066
/*
6167
* Initialize wolfSSL library and create a context.
@@ -208,6 +214,7 @@ static int initialize_udp_socket(SERVER_CONTEXT* ctx)
208214
if (ret < 0) {
209215
perror("setsockopt(SO_REUSEADDR)");
210216
close(ctx->listenfd);
217+
ctx->listenfd = -1;
211218
return -1;
212219
}
213220

@@ -221,6 +228,7 @@ static int initialize_udp_socket(SERVER_CONTEXT* ctx)
221228
if (ret < 0) {
222229
perror("bind()");
223230
close(ctx->listenfd);
231+
ctx->listenfd = -1;
224232
return -1;
225233
}
226234

@@ -233,6 +241,7 @@ static int initialize_udp_socket(SERVER_CONTEXT* ctx)
233241
if (ret < 0) {
234242
perror("recvfrom()");
235243
close(ctx->listenfd);
244+
ctx->listenfd = -1;
236245
return -1;
237246
}
238247

@@ -326,6 +335,7 @@ int Server_Init(SERVER_CONTEXT* ctx, whClientContext* client,
326335
}
327336

328337
memset(ctx, 0, sizeof(SERVER_CONTEXT));
338+
ctx->listenfd = -1; /* Initialize to invalid fd to prevent closing stdin */
329339
ctx->hsm_client = client;
330340
ctx->port = (g_config.port > 0) ? g_config.port : SERVER_PORT_DEFAULT;
331341

@@ -350,6 +360,7 @@ int Server_Init(SERVER_CONTEXT* ctx, whClientContext* client,
350360
if (ret != 0) {
351361
fprintf(stderr, "Failed to set up SSL accept\n");
352362
close(ctx->listenfd);
363+
ctx->listenfd = -1;
353364
wolfSSL_CTX_free(g_ctx);
354365
g_ctx = NULL;
355366
return -1;
@@ -375,6 +386,10 @@ int Server_Read(SERVER_CONTEXT* ctx, unsigned char* data, size_t length)
375386
return -1;
376387
}
377388

389+
if (length > INT_MAX) {
390+
return -1;
391+
}
392+
378393
ret = wolfSSL_read(ctx->ssl, data, (int)length);
379394
if (ret < 0) {
380395
int err = wolfSSL_get_error(ctx->ssl, ret);
@@ -397,6 +412,10 @@ int Server_Write(SERVER_CONTEXT* ctx, unsigned char* data, size_t length)
397412
return -1;
398413
}
399414

415+
if (length > INT_MAX) {
416+
return -1;
417+
}
418+
400419
ret = wolfSSL_write(ctx->ssl, data, (int)length);
401420
if (ret < 0) {
402421
int err = wolfSSL_get_error(ctx->ssl, ret);

0 commit comments

Comments
 (0)