This example demonstrates a TLS/DTLS server that offloads cryptographic operations to a wolfHSM server. By default, DTLS (UDP-based) is used, but the code can be adapted for TLS (TCP-based) connections.
The wolfHSM server runs separately and communicates via the chosen transport.
+-------------------+ Shared Memory (DMA) +-------------------+
| | <-----------------------------> | |
| TLS/DTLS Server | Crypto Operations Request | wolfHSM Server |
| (This Example) | <-----------------------------> | (wh_posix_server)|
| | Crypto Operations Response | |
+-------------------+ +-------------------+
| |
| TLS/DTLS | Performs all
| | crypto ops:
v | - Key Exchange
+-------------------+ | - Signing
| TLS/DTLS Client | | - Encryption
| | | - Hashing
+-------------------+ +
-
wolfHSM Server: The
wh_posix_serverruns with--type dmato provide crypto services over shared memory with DMA support. -
TLS/DTLS Server: This example connects to the wolfHSM server as a client and registers a crypto callback. All wolfSSL/wolfCrypt operations are forwarded to the HSM.
-
Crypto Offload: When
wolfSSL_CTX_SetDevId()is called withWH_DEV_ID, wolfSSL routes crypto operations through the registered callback to the wolfHSM server.
- wolfSSL library built with crypto callback support
- wolfHSM library with POSIX port
# Build the server
cd examples/demo/dtls_server
make
# Build the wolfHSM POSIX server (if not already built)
cd ../../posix/wh_posix_server
make DMA=1cd examples/posix/wh_posix_server
./Build/wh_posix_server.elf --type dmaYou should see:
Example wolfHSM POSIX server built with wolfSSL version X.X.X
Using DMA with shared memory transport
Waiting for connection...
In a new terminal:
cd examples/demo/dtls_server
./Build/wh_server.elfUsage: ./Build/wh_server.elf [options]
Options:
-A <file> CA certificate file (PEM or DER format)
If not specified, uses built-in test certificate
-c <file> Server certificate file (PEM or DER format)
-k <file> Server private key file (PEM or DER format)
-p <port> Port to listen on (default: 11111)
-h Show this help message
# Use default built-in certificates
./Build/wh_server.elf
# Use client-cert.pem from wolfssl bundle for example client to connect
./Build/wh_server.elf -A /path/to/wolfssl/certs/client-cert.pem
# Use custom certificates and port
./Build/wh_server.elf -A ca.pem -c server.pem -k server-key.pem -p 4433You should see:
DTLS server starting on port 11111...
Connected to wolfHSM server successfully
Waiting for client on port 11111...
In a third terminal:
# For DTLS (default mode) - using wolfssl with DTLS 1.3
./examples/examples/client -u -v 4In server.c, we register the wolfHSM crypto callbacks:
/* Register crypto callback for non-DMA operations */
wc_CryptoCb_RegisterDevice(WH_DEV_ID, wh_Client_CryptoCb, (void*)g_client);
/* Register crypto callback for DMA operations (larger data) */
wc_CryptoCb_RegisterDevice(WH_DEV_ID_DMA, wh_Client_CryptoCbDma, (void*)g_client);In server_io.c, we configure wolfSSL to use the HSM on the WOLFSSL_CTX object:
wolfSSL_CTX_SetDevId(ctx, WH_DEV_ID);-
WH_DEV_ID: Uses standard message-based crypto operations. Suitable for smaller data sizes.
-
WH_DEV_ID_DMA: Uses DMA (Direct Memory Access) for data transfer. More efficient for larger data like TLS record encryption.
Key settings for TLS/DTLS with wolfHSM:
/* Enable DTLS 1.3 (for UDP mode) */
#define WOLFSSL_DTLS
#define WOLFSSL_DTLS13
#define WOLFSSL_TLS13
/* Enable crypto callbacks for wolfHSM */
#define WOLF_CRYPTO_CB
/* Hash DRBG for RNG */
#define HAVE_HASHDRBGKey settings for wolfHSM client:
/* Enable wolfHSM client */
#define WOLFHSM_CFG_ENABLE_CLIENT
/* Enable DMA transport */
#define WOLFHSM_CFG_DMAMake sure the wh_posix_server is running with --type dma before starting
the DTLS server.
-
Check that both the wolfHSM server and DTLS server are built with the same wolfSSL version.
-
Verify the shared memory name matches between client and server (default: "wh_example_shm").
-
Enable verbose debugging:
make DEBUG_VERBOSE=1
To use TLS instead of DTLS:
-
In
server_io.c, change the method fromwolfDTLS_server_method()towolfTLSv1_3_server_method()or another TLS method. -
Change the socket initialization from UDP to TCP (replace
initialize_udp_socket()with a TCP equivalent). -
Remove the DTLS-specific peer address setup in
setup_ssl_accept().