Skip to content

Commit 8c483e4

Browse files
committed
Cleanups. Fix some multi-test issues.
1 parent 50dea8f commit 8c483e4

8 files changed

Lines changed: 284 additions & 131 deletions

File tree

.github/workflows/async-examples.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ jobs:
2929
set -euo pipefail
3030
run_pair() {
3131
local mode="$1"
32-
./examples/async/async_server --"$mode" > "/tmp/async_server_${mode}.log" 2>&1 &
32+
local ready="/tmp/wolfssl_async_ready_${mode}"
33+
rm -f "$ready"
34+
WOLFSSL_ASYNC_READYFILE="$ready" \
35+
./examples/async/async_server --"$mode" > "/tmp/async_server_${mode}.log" 2>&1 &
3336
local pid=$!
34-
sleep 1
35-
./examples/async/async_client --"$mode" 127.0.0.1 11111 > "/tmp/async_client_${mode}.log" 2>&1
37+
WOLFSSL_ASYNC_READYFILE="$ready" \
38+
./examples/async/async_client --"$mode" 127.0.0.1 11111 > "/tmp/async_client_${mode}.log" 2>&1
3639
local rc=$?
3740
kill "$pid" >/dev/null 2>&1 || true
3841
wait "$pid" >/dev/null 2>&1 || true

.wolfssl_known_macro_extras

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ WC_DILITHIUM_CACHE_PRIV_VECTORS
622622
WC_DILITHIUM_CACHE_PUB_VECTORS
623623
WC_DILITHIUM_FIXED_ARRAY
624624
WC_DISABLE_RADIX_ZERO_PAD
625-
WC_ECC_NONBLOCK_ONLY
626625
WC_FLAG_DONT_USE_AESNI
627626
WC_FORCE_LINUXKM_FORTIFY_SOURCE
628627
WC_LMS_FULL_HASH
@@ -638,7 +637,6 @@ WC_RSA_NONBLOCK
638637
WC_RSA_NONBLOCK_TIME
639638
WC_RSA_NO_FERMAT_CHECK
640639
WC_RWLOCK_OPS_INLINE
641-
WC_SHA3_HARDEN
642640
WC_SHA384
643641
WC_SHA384_DIGEST_SIZE
644642
WC_SHA512

examples/async/Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ CFLAGS += -I.
1010
CFLAGS += -I$(WOLFSSL_TOP)
1111
CFLAGS += -I$(WOLFSSL_TOP)/wolfssl
1212
CFLAGS += -I$(WOLFSSL_TOP)/wolfssl/wolfcrypt
13+
CFLAGS += -Wall -Wextra -Wpedantic -Werror
1314
CFLAGS += -DWOLFSSL_USER_SETTINGS
15+
CFLAGS += -DHAVE_SYS_TIME_H
1416
CFLAGS += -DUSE_CERT_BUFFERS_256
1517

1618
LDFLAGS ?=
@@ -20,17 +22,21 @@ TARGETS = async_client async_server
2022

2123
WOLFSSL_SRC := $(wildcard $(WOLFSSL_TOP)/src/*.c)
2224
WOLFCRYPT_SRC := $(wildcard $(WOLFSSL_TOP)/wolfcrypt/src/*.c)
23-
LOCAL_SRC := async_client.c async_server.c
25+
LOCAL_SRC := async_client.c async_server.c async_tls.c
2426

2527
WOLFSSL_OBJS := $(patsubst $(WOLFSSL_TOP)/%, $(OBJDIR)/%, $(WOLFSSL_SRC:.c=.o))
2628
WOLFCRYPT_OBJS := $(patsubst $(WOLFSSL_TOP)/%, $(OBJDIR)/%, $(WOLFCRYPT_SRC:.c=.o))
2729
LOCAL_OBJS := $(patsubst %.c, $(OBJDIR)/%.o, $(LOCAL_SRC))
2830

29-
OBJS := $(LOCAL_OBJS) $(WOLFSSL_OBJS) $(WOLFCRYPT_OBJS)
31+
ASYNC_CLIENT_OBJS := $(OBJDIR)/async_client.o $(OBJDIR)/async_tls.o
32+
ASYNC_SERVER_OBJS := $(OBJDIR)/async_server.o $(OBJDIR)/async_tls.o
3033

3134
all: $(TARGETS)
3235

33-
$(TARGETS): %: $(OBJDIR)/%.o $(WOLFSSL_OBJS) $(WOLFCRYPT_OBJS)
36+
async_client: $(ASYNC_CLIENT_OBJS) $(WOLFSSL_OBJS) $(WOLFCRYPT_OBJS)
37+
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
38+
39+
async_server: $(ASYNC_SERVER_OBJS) $(WOLFSSL_OBJS) $(WOLFCRYPT_OBJS)
3440
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
3541

3642
$(OBJDIR)/%.o: %.c user_settings.h
@@ -41,5 +47,13 @@ $(OBJDIR)/%.o: $(WOLFSSL_TOP)/%.c
4147
@mkdir -p $(dir $@)
4248
$(CC) $(CFLAGS) -c $< -o $@
4349

50+
# Possibly empty files (avoids "warning: ISO C forbids an empty translation unit")
51+
$(OBJDIR)/wolfcrypt/src/ecc_fp.o: CFLAGS += -Wno-pedantic
52+
$(OBJDIR)/wolfcrypt/src/fips.o: CFLAGS += -Wno-pedantic
53+
$(OBJDIR)/wolfcrypt/src/fips_test.o: CFLAGS += -Wno-pedantic
54+
$(OBJDIR)/wolfcrypt/src/selftest.o: CFLAGS += -Wno-pedantic
55+
$(OBJDIR)/wolfcrypt/src/wolfcrypt_first.o: CFLAGS += -Wno-pedantic
56+
$(OBJDIR)/wolfcrypt/src/wolfcrypt_last.o: CFLAGS += -Wno-pedantic
57+
4458
clean:
4559
$(RM) -r $(OBJDIR) $(TARGETS)

examples/async/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ make -C examples/async
2121
./examples/async/async_client --x25519 ecc256.badssl.com 443
2222
```
2323

24+
Optional ready-file sync (CI-friendly, avoids sleeps):
25+
```
26+
export WOLFSSL_ASYNC_READYFILE=/tmp/wolfssl_async_ready
27+
./examples/async/async_server --ecc
28+
WOLFSSL_ASYNC_READYFILE=/tmp/wolfssl_async_ready ./examples/async/async_client --ecc 127.0.0.1 11111
29+
```
30+
31+
Porting the TCP/IP stack:
32+
Define `NET_USER_HEADER` to include your network shim and provide the
33+
`NET_*` macros plus `NET_IO_SEND_CB` / `NET_IO_RECV_CB`.
34+
2435
## Asynchronous Cryptography Design
2536

2637
When a cryptographic call is handed off to hardware it return `WC_PENDING_E` up to caller. Then it can keep calling until the operation completes. For some platforms it is required to call `wolfSSL_AsyncPoll`. At the TLS layer a "devId" (Device ID) must be set using `wolfSSL_CTX_SetDevId` to indicate desire to offload cryptography.

examples/async/async_client.c

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
#include <errno.h>
3434

3535
/* socket */
36+
#ifndef NET_USER_HEADER
3637
#include <fcntl.h>
3738
#include <netdb.h>
3839
#include <sys/socket.h>
3940
#include <sys/types.h>
4041
#include <sys/select.h>
4142
#include <unistd.h>
43+
#endif
4244

4345
/* wolfSSL */
4446
#ifdef WOLFSSL_USER_SETTINGS
@@ -55,6 +57,7 @@
5557
/* ------------------------------------------------------------------ */
5658
/* POSIX transport helpers (replace with your BSP/port layer). */
5759
/* ------------------------------------------------------------------ */
60+
#ifndef NET_USER_HEADER
5861
static int posix_set_nonblocking(int fd)
5962
{
6063
int flags = fcntl(fd, F_GETFL, 0);
@@ -144,54 +147,11 @@ static int posix_net_connect(const char* host, int port)
144147
}
145148
return fd;
146149
}
150+
#endif
147151

148152
/* ------------------------------------------------------------------ */
149153
/* WOLFSSL_USER_IO callbacks. */
150154
/* ------------------------------------------------------------------ */
151-
static int posix_send_cb(WOLFSSL* ssl, char* buf, int sz, void* ctx)
152-
{
153-
(void)ssl;
154-
int fd = (int)(intptr_t)ctx;
155-
int ret = (int)send(fd, buf, (size_t)sz, 0);
156-
if (ret >= 0) {
157-
return ret;
158-
}
159-
if (errno == EAGAIN || errno == EWOULDBLOCK) {
160-
return WOLFSSL_CBIO_ERR_WANT_WRITE;
161-
}
162-
return WOLFSSL_CBIO_ERR_GENERAL;
163-
}
164-
165-
static int posix_recv_cb(WOLFSSL* ssl, char* buf, int sz, void* ctx)
166-
{
167-
(void)ssl;
168-
int fd = (int)(intptr_t)ctx;
169-
int ret = (int)recv(fd, buf, (size_t)sz, 0);
170-
if (ret >= 0) {
171-
return ret;
172-
}
173-
if (errno == EAGAIN || errno == EWOULDBLOCK) {
174-
return WOLFSSL_CBIO_ERR_WANT_READ;
175-
}
176-
return WOLFSSL_CBIO_ERR_GENERAL;
177-
}
178-
179-
int posix_getdevrandom(unsigned char *out, unsigned int sz);
180-
int posix_getdevrandom(unsigned char *out, unsigned int sz)
181-
{
182-
ssize_t ret;
183-
int fd = open("/dev/urandom", O_RDONLY);
184-
if (fd < 0) {
185-
return -1;
186-
}
187-
ret = read(fd, out, sz);
188-
close(fd);
189-
if (ret != (ssize_t)sz) {
190-
return -1;
191-
}
192-
return 0;
193-
}
194-
195155
static void usage(const char* prog)
196156
{
197157
printf("usage: %s [--ecc|--x25519] [host] [port]\n", prog);
@@ -260,7 +220,14 @@ int client_async_test(int argc, char** argv)
260220
return 0;
261221
}
262222

263-
net = posix_net_connect(host, port);
223+
{
224+
const char* ready = getenv(WOLFSSL_ASYNC_READYFILE_ENV);
225+
if (ready != NULL) {
226+
(void)async_readyfile_wait(ready,
227+
WOLFSSL_ASYNC_READYFILE_TIMEOUT_MS);
228+
}
229+
}
230+
net = NET_CONNECT(host, port);
264231
if (net < 0) {
265232
return -1;
266233
}
@@ -288,10 +255,11 @@ int client_async_test(int argc, char** argv)
288255
/* Bare-metal style: disable verification unless you load CA/peer certs. */
289256
wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
290257

291-
wolfSSL_SetIORecv(ctx, posix_recv_cb);
292-
wolfSSL_SetIOSend(ctx, posix_send_cb);
258+
wolfSSL_SetIORecv(ctx, NET_IO_RECV_CB);
259+
wolfSSL_SetIOSend(ctx, NET_IO_SEND_CB);
293260

294-
wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, host, strlen(host));
261+
wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, host,
262+
(word16)XSTRLEN(host));
295263

296264
ssl = wolfSSL_new(ctx);
297265
if (ssl == NULL) {
@@ -300,7 +268,8 @@ int client_async_test(int argc, char** argv)
300268

301269
wolfSSL_SetIOReadCtx(ssl, (void*)(intptr_t)net);
302270
wolfSSL_SetIOWriteCtx(ssl, (void*)(intptr_t)net);
303-
(void)wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, host, (word16)XSTRLEN(host));
271+
(void)wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, host,
272+
(word16)XSTRLEN(host));
304273

305274
for (;;) {
306275
ret = wolfSSL_UseKeyShare(ssl, group);
@@ -328,10 +297,10 @@ int client_async_test(int argc, char** argv)
328297
break;
329298
}
330299
err = wolfSSL_get_error(ssl, 0);
331-
if (err == WC_PENDING_E ||
300+
if (err == WC_NO_ERR_TRACE(WC_PENDING_E) ||
332301
err == WOLFSSL_ERROR_WANT_READ ||
333302
err == WOLFSSL_ERROR_WANT_WRITE) {
334-
if (err == WC_PENDING_E) {
303+
if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
335304
#ifdef WOLFSSL_DEBUG_NONBLOCK
336305
pending_count++;
337306
#endif
@@ -368,10 +337,10 @@ int client_async_test(int argc, char** argv)
368337
break;
369338
}
370339
err = wolfSSL_get_error(ssl, 0);
371-
if (err == WC_PENDING_E ||
340+
if (err == WC_NO_ERR_TRACE(WC_PENDING_E) ||
372341
err == WOLFSSL_ERROR_WANT_READ ||
373342
err == WOLFSSL_ERROR_WANT_WRITE) {
374-
if (err == WC_PENDING_E) {
343+
if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
375344
#ifdef WOLFSSL_DEBUG_NONBLOCK
376345
pending_count++;
377346
#endif
@@ -400,10 +369,10 @@ int client_async_test(int argc, char** argv)
400369
break;
401370
}
402371
err = wolfSSL_get_error(ssl, 0);
403-
if (err == WC_PENDING_E ||
372+
if (err == WC_NO_ERR_TRACE(WC_PENDING_E) ||
404373
err == WOLFSSL_ERROR_WANT_READ ||
405374
err == WOLFSSL_ERROR_WANT_WRITE) {
406-
if (err == WC_PENDING_E) {
375+
if (err == WC_NO_ERR_TRACE(WC_PENDING_E)) {
407376
#ifdef WOLFSSL_DEBUG_NONBLOCK
408377
pending_count++;
409378
#endif
@@ -444,7 +413,7 @@ int client_async_test(int argc, char** argv)
444413
#endif
445414
wolfSSL_Cleanup();
446415
if (net >= 0) {
447-
close(net);
416+
NET_CLOSE(net);
448417
}
449418

450419
return ret;

0 commit comments

Comments
 (0)