Skip to content

Commit 7eaba42

Browse files
Use TLS over transport for authentication of peer (#227)
* Use TLS over transport for authentication of peer * add test cases and update user_settings.h * handle race condition with connect * typo fix, trim down struct, connect fd variable update * use WOLFSSL_* instead of CTC_* and fix typo * add SHE test with client only TLS * update client only test case for TLS * combined client only test macro, added TLS option to example Makefiles, moved setting of TLS certificates into config and internal transport file * use WOLFHSM_CFG_TLS macro guard with posix_transport_tls.c|h files * updating test case for TLS use and macro guards in posix client example * clean up of macro names after refactor * result of running git-clang-format * fix spelling in comments, make fd use more clear, update to macro guards * add free'ing of ssl object on error
1 parent 79e2481 commit 7eaba42

22 files changed

+1343
-55
lines changed

.github/workflows/build-and-run-examples.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
build:
1111
strategy:
1212
matrix:
13-
transport: [ 'tcp', 'shm', 'dma' ]
13+
transport: [ 'tcp', 'shm', 'dma', 'tls', 'psk' ]
1414
asan: [ 'ASAN=1', 'ASAN=0' ]
1515
debug: [ '', 'DEBUG_VERBOSE=1' ]
1616
test: [ '', '--test' ]
@@ -27,35 +27,52 @@ jobs:
2727
repository: wolfssl/wolfssl
2828
path: wolfssl
2929

30+
- name: Set TLS Environment Variable
31+
run: |
32+
if [ "${{ matrix.transport }}" = "tls" ] || [ "${{ matrix.transport }}" = "psk" ]; then
33+
echo "TLS=1" >> $GITHUB_ENV
34+
else
35+
echo "TLS=0" >> $GITHUB_ENV
36+
fi
37+
3038
# Build examples
3139
- name: Build POSIX server
3240
run: |
3341
if [ "${{ matrix.transport }}" = "dma" ]; then
3442
cd examples/posix/wh_posix_server && ${{ matrix.asan }} ${{ matrix.debug }} DMA=1 make -j WOLFSSL_DIR=../../../wolfssl
3543
else
36-
cd examples/posix/wh_posix_server && ${{ matrix.asan }} ${{ matrix.debug }} make -j WOLFSSL_DIR=../../../wolfssl
44+
cd examples/posix/wh_posix_server && ${{ matrix.asan }} ${{ matrix.debug }} TLS=${{ env.TLS }} make -j WOLFSSL_DIR=../../../wolfssl
3745
fi
46+
3847
- name: Build POSIX client
3948
run: |
4049
if [ "${{ matrix.transport }}" = "dma" ]; then
4150
cd examples/posix/wh_posix_client && ${{ matrix.asan }} ${{ matrix.debug }} DMA=1 make -j WOLFSSL_DIR=../../../wolfssl
4251
else
43-
cd examples/posix/wh_posix_client && ${{ matrix.asan }} ${{ matrix.debug }} make -j WOLFSSL_DIR=../../../wolfssl
52+
cd examples/posix/wh_posix_client && ${{ matrix.asan }} ${{ matrix.debug }} TLS=${{ env.TLS }} make -j WOLFSSL_DIR=../../../wolfssl
4453
fi
4554
4655
# Start the server in the background
4756
- name: Run POSIX server
4857
run: |
4958
cd examples/posix/wh_posix_server
50-
./Build/wh_posix_server.elf --type ${{ matrix.transport }} &
59+
if [ "${{ matrix.transport }}" = "psk" ]; then
60+
echo "test_password" | ./Build/wh_posix_server.elf --type ${{ matrix.transport }} &
61+
else
62+
./Build/wh_posix_server.elf --type ${{ matrix.transport }} &
63+
fi
5164
POSIX_SERVER_PID=$!
5265
echo "POSIX_SERVER_PID=$POSIX_SERVER_PID" >> $GITHUB_ENV
5366
5467
# Run the client that connects to the server
5568
- name: Run POSIX client
5669
run: |
5770
cd examples/posix/wh_posix_client
58-
./Build/wh_posix_client.elf --type ${{ matrix.transport }} ${{ matrix.test }}
71+
if [ "${{ matrix.transport }}" = "psk" ]; then
72+
echo "test_password" | ./Build/wh_posix_client.elf --type ${{ matrix.transport }} ${{ matrix.test }}
73+
else
74+
./Build/wh_posix_client.elf --type ${{ matrix.transport }} ${{ matrix.test }}
75+
fi
5976
6077
# Optional: Kill the server process if it doesn't exit on its own
6178
- name: Cleanup POSIX server

.github/workflows/build-and-test-clientonly.yml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ on:
88

99
jobs:
1010
build:
11-
11+
strategy:
12+
matrix:
13+
transport: [ 'tcp', 'tls' ]
1214
runs-on: ubuntu-latest
1315

1416
steps:
@@ -33,43 +35,55 @@ jobs:
3335
- name: Build POSIX server
3436
run: |
3537
cd examples/posix/wh_posix_server
36-
make -j SHE=1 WOLFSSL_DIR=../../../wolfssl
38+
if [ "${{ matrix.transport }}" = "tcp" ]; then
39+
make -j SHE=1 WOLFSSL_DIR=../../../wolfssl
40+
else
41+
make -j TLS=1 SHE=1 WOLFSSL_DIR=../../../wolfssl
42+
fi
3743
3844
# Start the server in the background
3945
- name: Run POSIX server
4046
run: |
4147
cd examples/posix/wh_posix_server
42-
./Build/wh_posix_server.elf &
43-
TCP_SERVER_PID=$!
44-
echo "TCP_SERVER_PID=$TCP_SERVER_PID" >> $GITHUB_ENV
48+
./Build/wh_posix_server.elf --type ${{ matrix.transport }} &
49+
SERVER_PID=$!
50+
echo "SERVER_PID=$SERVER_PID" >> $GITHUB_ENV
4551
4652
# Build and test client-only build with everything enabled and ASAN
4753
- name: Build client-only unit tests with ASAN
4854
run: |
4955
cd test
5056
make clean
51-
make -j CLIENT_ONLY_TCP=1 SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
57+
if [ "${{ matrix.transport }}" = "tcp" ]; then
58+
make -j CLIENT_ONLY=1 SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
59+
else
60+
make -j CLIENT_ONLY=1 TLS=1 SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
61+
fi
5262
5363
# Restart server with fresh state for second test run
5464
- name: Restart POSIX server
5565
run: |
56-
kill $TCP_SERVER_PID || true
66+
kill $SERVER_PID || true
5767
cd examples/posix/wh_posix_server
5868
rm -f *.bin || true
59-
./Build/wh_posix_server.elf &
60-
TCP_SERVER_PID=$!
61-
echo "TCP_SERVER_PID=$TCP_SERVER_PID" >> $GITHUB_ENV
69+
./Build/wh_posix_server.elf --type ${{ matrix.transport }} &
70+
SERVER_PID=$!
71+
echo "SERVER_PID=$SERVER_PID" >> $GITHUB_ENV
6272
sleep 2
6373
6474
# Build and test client-only with DEBUG_VERBOSE=1 (includes DEBUG)
6575
- name: Build client-only unit tests with DEBUG_VERBOSE
6676
run: |
6777
cd test
6878
make clean
69-
make -j CLIENT_ONLY_TCP=1 SHE=1 DEBUG_VERBOSE=1 WOLFSSL_DIR=../wolfssl && make run
79+
if [ "${{ matrix.transport }}" = "tcp" ]; then
80+
make -j CLIENT_ONLY=1 SHE=1 DEBUG_VERBOSE=1 WOLFSSL_DIR=../wolfssl && make run
81+
else
82+
make -j CLIENT_ONLY=1 TLS=1 SHE=1 DEBUG_VERBOSE=1 WOLFSSL_DIR=../wolfssl && make run
83+
fi
7084
7185
# Optional: Kill the server process if it doesn't exit on its own
72-
- name: Cleanup POSIX TCP server
86+
- name: Cleanup POSIX server
7387
if: always()
74-
run: kill $TCP_SERVER_PID || true
75-
88+
run: kill $SERVER_PID || true
89+

examples/posix/wh_posix_client/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ DEF += -DWC_USE_DEVID=0x57444D41 -DWC_NO_DEFAULT_DEVID
112112
CFLAGS += -DWOLFHSM_CFG_DMA
113113
else
114114
DEF += -DWC_USE_DEVID=0x5748534D
115+
endif
115116

117+
ifeq ($(TLS),1)
118+
CFLAGS += -DWOLFHSM_CFG_TLS
116119
endif
117120

118121
#wolfCrypt test/benchmark source files

examples/posix/wh_posix_client/wh_posix_client.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,17 @@ void Usage(const char* exeName)
160160
{
161161
WOLFHSM_CFG_PRINTF("Usage: %s --type <type> --test\n", exeName);
162162
WOLFHSM_CFG_PRINTF("Example: %s --type tcp\n", exeName);
163-
WOLFHSM_CFG_PRINTF("type: tcp (default), shm\n");
163+
WOLFHSM_CFG_PRINTF("type: tcp (default), shm");
164+
#ifdef WOLFHSM_CFG_TLS
165+
WOLFHSM_CFG_PRINTF(", tls");
166+
#if !defined(NO_PSK)
167+
WOLFHSM_CFG_PRINTF(", psk");
168+
#endif
169+
#endif /* WOLFHSM_CFG_TLS */
170+
#ifdef WOLFSSL_STATIC_MEMORY
171+
WOLFHSM_CFG_PRINTF(", dma");
172+
#endif
173+
WOLFHSM_CFG_PRINTF("\n");
164174
}
165175

166176
int main(int argc, char** argv)
@@ -204,6 +214,18 @@ int main(int argc, char** argv)
204214
WOLFHSM_CFG_PRINTF("Using shared memory transport\n");
205215
wh_PosixClient_ExampleShmConfig(c_conf);
206216
}
217+
#ifdef WOLFHSM_CFG_TLS
218+
else if (strcmp(type, "tls") == 0) {
219+
WOLFHSM_CFG_PRINTF("Using TLS transport\n");
220+
wh_PosixClient_ExampleTlsConfig(c_conf);
221+
}
222+
#if !defined(NO_PSK)
223+
else if (strcmp(type, "psk") == 0) {
224+
WOLFHSM_CFG_PRINTF("Using TLS PSK transport\n");
225+
wh_PosixClient_ExamplePskConfig(c_conf);
226+
}
227+
#endif /* !NO_PSK */
228+
#endif /* WOLFHSM_CFG_TLS */
207229
#ifdef WOLFSSL_STATIC_MEMORY
208230
else if (strcmp(type, "dma") == 0) {
209231
WOLFHSM_CFG_PRINTF("Using DMA with shared memory transport\n");

examples/posix/wh_posix_client/wh_posix_client_cfg.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,31 @@
1010

1111
#include "port/posix/posix_transport_shm.h"
1212
#include "port/posix/posix_transport_tcp.h"
13+
#ifdef WOLFHSM_CFG_TLS
14+
#include "port/posix/posix_transport_tls.h"
15+
#endif
1316

1417
#include <string.h>
1518

1619
posixTransportShmClientContext tccShm;
1720
posixTransportTcpClientContext tccTcp;
21+
#ifdef WOLFHSM_CFG_TLS
22+
posixTransportTlsClientContext tccTls;
23+
#endif
1824

1925
posixTransportShmConfig shmConfig;
2026
posixTransportTcpConfig tcpConfig;
27+
#ifdef WOLFHSM_CFG_TLS
28+
posixTransportTlsConfig tlsConfig;
29+
#endif
2130

2231
whCommClientConfig c_comm;
2332

2433
whTransportClientCb shmCb = POSIX_TRANSPORT_SHM_CLIENT_CB;
2534
whTransportClientCb tcpCb = PTT_CLIENT_CB;
35+
#ifdef WOLFHSM_CFG_TLS
36+
whTransportClientCb tlsCb = PTTLS_CLIENT_CB;
37+
#endif
2638

2739
#ifdef WOLFSSL_STATIC_MEMORY
2840
whTransportClientCb dmaCb = POSIX_TRANSPORT_SHM_CLIENT_CB;
@@ -123,6 +135,85 @@ int wh_PosixClient_ExampleTcpConfig(void* conf)
123135
return WH_ERROR_OK;
124136
}
125137

138+
#if defined(WOLFHSM_CFG_TLS)
139+
/* client configuration setup example for TLS transport */
140+
#undef USE_CERT_BUFFERS_2048
141+
#define USE_CERT_BUFFERS_2048
142+
#include "wolfssl/certs_test.h"
143+
144+
int wh_PosixClient_ExampleTlsConfig(void* conf)
145+
{
146+
whClientConfig* c_conf = (whClientConfig*)conf;
147+
148+
memset(&tccTls, 0, sizeof(posixTransportTlsClientContext));
149+
150+
/* Initialize TLS context fields that need specific values */
151+
tccTls.state = 0;
152+
tccTls.connect_fd_p1 = 0; /* Invalid fd */
153+
154+
tlsConfig.server_ip_string = WH_POSIX_SERVER_TCP_IPSTRING;
155+
tlsConfig.server_port = WH_POSIX_SERVER_TCP_PORT;
156+
tlsConfig.disable_peer_verification = false;
157+
158+
tlsConfig.ca_cert = ca_cert_der_2048;
159+
tlsConfig.ca_cert_len = sizeof_ca_cert_der_2048;
160+
tlsConfig.cert = client_cert_der_2048;
161+
tlsConfig.cert_len = sizeof_client_cert_der_2048;
162+
tlsConfig.key = client_key_der_2048;
163+
tlsConfig.key_len = sizeof_client_key_der_2048;
164+
tlsConfig.heap_hint = NULL;
165+
166+
c_comm.transport_cb = &tlsCb;
167+
c_comm.transport_context = (void*)&tccTls;
168+
c_comm.transport_config = (void*)&tlsConfig;
169+
c_comm.client_id = WH_POSIX_CLIENT_ID;
170+
c_conf->comm = &c_comm;
171+
172+
return WH_ERROR_OK;
173+
}
174+
175+
176+
#ifndef NO_PSK
177+
/* Simple PSK example callback */
178+
static unsigned int psk_tls12_client_cb(WOLFSSL* ssl, const char* hint,
179+
char* identity, unsigned int id_max_len,
180+
unsigned char* key,
181+
unsigned int key_max_len)
182+
{
183+
size_t len;
184+
185+
memset(key, 0, key_max_len);
186+
const char* exampleIdentity = "PSK_EXAMPLE_CLIENT_IDENTITY";
187+
188+
printf("PSK server identity hint: %s\n", hint);
189+
printf("PSK using identity: %s\n", exampleIdentity);
190+
strncpy(identity, exampleIdentity, id_max_len);
191+
192+
printf("Enter PSK password: ");
193+
if (fgets((char*)key, key_max_len - 1, stdin) == NULL) {
194+
memset(key, 0, key_max_len);
195+
return 0U;
196+
}
197+
198+
(void)ssl;
199+
len = strcspn((char*)key, "\n");
200+
((char*)key)[len] = '\0';
201+
return (unsigned int)len;
202+
}
203+
204+
205+
int wh_PosixClient_ExamplePskConfig(void* conf)
206+
{
207+
if (wh_PosixClient_ExampleTlsConfig(conf) != WH_ERROR_OK) {
208+
return WH_ERROR_ABORTED;
209+
}
210+
tlsConfig.psk_client_cb = psk_tls12_client_cb;
211+
212+
return WH_ERROR_OK;
213+
}
214+
#endif /* NO_PSK */
215+
#endif /* WOLFHSM_CFG_TLS */
216+
126217

127218
/* client configuration setup example for transport */
128219
int wh_PosixClient_ExampleShmConfig(void* conf)

examples/posix/wh_posix_client/wh_posix_client_cfg.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
int wh_PosixClient_ExampleShmDmaConfig(void* c_conf);
55
int wh_PosixClient_ExampleShmConfig(void* c_conf);
66
int wh_PosixClient_ExampleTcpConfig(void* c_conf);
7+
#ifdef WOLFHSM_CFG_TLS
8+
int wh_PosixClient_ExampleTlsConfig(void* c_conf);
9+
#if !defined(NO_PSK)
10+
int wh_PosixClient_ExamplePskConfig(void* c_conf);
11+
#endif /* !NO_PSK */
12+
#endif /* WOLFHSM_CFG_TLS */
713
int wh_PosixClient_ExampleSetupDmaMemory(void* ctx, void* c_conf);
8-
#endif /* WH_POSIX_CLIENT_CFG_H */
14+
#endif /* WH_POSIX_CLIENT_CFG_H */

examples/posix/wh_posix_server/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ ifeq ($(SHE),1)
8787
CFLAGS += -DWOLFHSM_CFG_SHE_EXTENSION
8888
endif
8989

90+
# Support a TLS-capable build
91+
ifeq ($(TLS),1)
92+
CFLAGS += -DWOLFHSM_CFG_TLS
93+
endif
94+
9095
ifeq ($(DMA),1)
9196
CFLAGS += -DWOLFHSM_CFG_DMA
9297
endif

examples/posix/wh_posix_server/user_settings.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ extern "C" {
4747
#define WOLFSSL_BASE64_ENCODE
4848
#define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
4949

50-
/* For cert manager */
50+
#ifndef WOLFHSM_CFG_TLS
51+
/* These macros reduce footprint size when TLS functionality is not needed */
5152
#define NO_TLS
5253
/* Eliminates need for IO layer since we only use CM */
5354
#define WOLFSSL_USER_IO
55+
#define WOLFSSL_NO_TLS12
56+
#define NO_PSK
57+
#endif /* WOLFHSM_CFG_TLS */
58+
5459
/* For ACert support (also requires WOLFSSL_ASN_TEMPLATE) */
5560
#define WOLFSSL_ACERT
5661

@@ -71,7 +76,6 @@ extern "C" {
7176
#define NO_ERROR_QUEUE
7277
#define NO_INLINE
7378
#define NO_OLD_TLS
74-
#define WOLFSSL_NO_TLS12
7579
#define NO_DO178
7680
/* Prevents certain functions (SHA, hash.c) on server from falling back to
7781
* client cryptoCb when using non-devId APIs */
@@ -154,7 +158,6 @@ extern "C" {
154158
/* Remove unneeded crypto */
155159
#define NO_DSA
156160
#define NO_RC4
157-
#define NO_PSK
158161
#define NO_MD4
159162
#define NO_MD5
160163
#define NO_DES3
@@ -190,11 +193,17 @@ extern "C" {
190193
#endif /* optional malloc check */
191194
#endif /* optional static memory */
192195

193-
#ifdef WOLFHSM_CFG_DMA
196+
#if defined(WOLFHSM_CFG_DMA) || defined(WOLFHSM_CFG_TLS)
197+
/* If using DMA or TLS use static memory for no dynamic memory allocation */
194198
#undef WOLFSSL_STATIC_MEMORY
195199
#define WOLFSSL_STATIC_MEMORY
196200
#endif
197201

202+
/* additional memory debugging macros, prints out each alloc and free */
203+
/* #define WOLFSSL_DEBUG_MEMORY */
204+
/* #define WOLFSSL_DEBUG_MEMORY_PRINT */
205+
206+
/* #define DEBUG_WOLFSSL */
198207
#ifdef __cplusplus
199208
}
200209
#endif

0 commit comments

Comments
 (0)