|
| 1 | +/* emnet_nonblock_test.c -- non-blocking TLS 1.3 handshake over a |
| 2 | + * socketpair, with wolfSSL built for WOLFSSL_EMNET and the recv/send |
| 3 | + * error surface translated by emnet_shim.c. |
| 4 | + * |
| 5 | + * Purpose: expose the unreachable `#elif defined(WOLFSSL_EMNET)` branch |
| 6 | + * in src/wolfio.c:172 (shadowed by the WOLFSSL_LINUXKM|WOLFSSL_EMNET |
| 7 | + * branch at line 153). On current master the shadowing branch returns |
| 8 | + * `-err`, flipping IP_ERR_WOULD_BLOCK (-6) to +6, which no longer |
| 9 | + * matches SOCKET_EWOULDBLOCK, so TranslateIoReturnCode falls through to |
| 10 | + * WOLFSSL_CBIO_ERR_GENERAL and the handshake aborts the first time |
| 11 | + * the socket would block. The test exits non-zero with a diagnostic |
| 12 | + * naming the unexpected error; the follow-up fix will make it pass. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <errno.h> |
| 16 | +#include <fcntl.h> |
| 17 | +#include <poll.h> |
| 18 | +#include <pthread.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | +#include <sys/socket.h> |
| 23 | +#include <sys/types.h> |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +#include <wolfssl/options.h> |
| 27 | +#include <wolfssl/ssl.h> |
| 28 | + |
| 29 | +#define MAX_ITERS 200 /* handshake loop safety cap */ |
| 30 | +#define POLL_MS 500 |
| 31 | + |
| 32 | +#define CERT_PATH "certs/server-ecc.pem" |
| 33 | +#define KEY_PATH "certs/ecc-key.pem" |
| 34 | +#define CA_PATH "certs/ca-ecc-cert.pem" |
| 35 | + |
| 36 | +struct side { |
| 37 | + int fd; |
| 38 | + const char *name; |
| 39 | + WOLFSSL *ssl; |
| 40 | + int (*fn)(WOLFSSL *); |
| 41 | + int saw_would_block; |
| 42 | + int completed; |
| 43 | + int failed; |
| 44 | + int last_err; |
| 45 | +}; |
| 46 | + |
| 47 | +static void set_nonblock(int fd) |
| 48 | +{ |
| 49 | + int flags = fcntl(fd, F_GETFL, 0); |
| 50 | + if (flags < 0) { |
| 51 | + perror("fcntl F_GETFL"); |
| 52 | + exit(2); |
| 53 | + } |
| 54 | + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { |
| 55 | + perror("fcntl F_SETFL O_NONBLOCK"); |
| 56 | + exit(2); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +static void *run_side(void *arg) |
| 61 | +{ |
| 62 | + struct side *s = (struct side *)arg; |
| 63 | + int iter; |
| 64 | + |
| 65 | + for (iter = 0; iter < MAX_ITERS; iter++) { |
| 66 | + int ret = s->fn(s->ssl); |
| 67 | + if (ret == WOLFSSL_SUCCESS) { |
| 68 | + s->completed = 1; |
| 69 | + return NULL; |
| 70 | + } |
| 71 | + |
| 72 | + int err = wolfSSL_get_error(s->ssl, ret); |
| 73 | + s->last_err = err; |
| 74 | + |
| 75 | + if (err == WOLFSSL_ERROR_WANT_READ) { |
| 76 | + struct pollfd pfd = { .fd = s->fd, .events = POLLIN }; |
| 77 | + s->saw_would_block = 1; |
| 78 | + poll(&pfd, 1, POLL_MS); |
| 79 | + continue; |
| 80 | + } |
| 81 | + if (err == WOLFSSL_ERROR_WANT_WRITE) { |
| 82 | + struct pollfd pfd = { .fd = s->fd, .events = POLLOUT }; |
| 83 | + s->saw_would_block = 1; |
| 84 | + poll(&pfd, 1, POLL_MS); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + /* Anything else on a non-blocking handshake is a failure. */ |
| 89 | + char errstr[WOLFSSL_MAX_ERROR_SZ]; |
| 90 | + wolfSSL_ERR_error_string_n((unsigned long)err, |
| 91 | + errstr, sizeof(errstr)); |
| 92 | + fprintf(stderr, |
| 93 | + "FAIL: %s: wolfSSL_get_error=%d (%s) after iter=%d. " |
| 94 | + "Expected WANT_READ/WANT_WRITE on a non-blocking socketpair. " |
| 95 | + "This is the emNET non-blocking error-translation bug in " |
| 96 | + "src/wolfio.c (unreachable WOLFSSL_EMNET branch in " |
| 97 | + "wolfSSL_LastError).\n", |
| 98 | + s->name, err, errstr, iter); |
| 99 | + s->failed = 1; |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + fprintf(stderr, "FAIL: %s: handshake did not complete within %d " |
| 104 | + "iterations (last err=%d)\n", |
| 105 | + s->name, MAX_ITERS, s->last_err); |
| 106 | + s->failed = 1; |
| 107 | + return NULL; |
| 108 | +} |
| 109 | + |
| 110 | +int main(void) |
| 111 | +{ |
| 112 | + int sv[2]; |
| 113 | + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) { |
| 114 | + perror("socketpair"); |
| 115 | + return 2; |
| 116 | + } |
| 117 | + set_nonblock(sv[0]); |
| 118 | + set_nonblock(sv[1]); |
| 119 | + |
| 120 | + wolfSSL_Init(); |
| 121 | + |
| 122 | + WOLFSSL_CTX *sctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()); |
| 123 | + WOLFSSL_CTX *cctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()); |
| 124 | + if (!sctx || !cctx) { |
| 125 | + fprintf(stderr, "wolfSSL_CTX_new failed\n"); |
| 126 | + return 2; |
| 127 | + } |
| 128 | + |
| 129 | + if (wolfSSL_CTX_use_certificate_file(sctx, CERT_PATH, |
| 130 | + WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) { |
| 131 | + fprintf(stderr, "failed to load server cert %s\n", CERT_PATH); |
| 132 | + return 2; |
| 133 | + } |
| 134 | + if (wolfSSL_CTX_use_PrivateKey_file(sctx, KEY_PATH, |
| 135 | + WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) { |
| 136 | + fprintf(stderr, "failed to load server key %s\n", KEY_PATH); |
| 137 | + return 2; |
| 138 | + } |
| 139 | + if (wolfSSL_CTX_load_verify_locations(cctx, CA_PATH, NULL) |
| 140 | + != WOLFSSL_SUCCESS) { |
| 141 | + fprintf(stderr, "failed to load CA %s\n", CA_PATH); |
| 142 | + return 2; |
| 143 | + } |
| 144 | + |
| 145 | + WOLFSSL *server_ssl = wolfSSL_new(sctx); |
| 146 | + WOLFSSL *client_ssl = wolfSSL_new(cctx); |
| 147 | + if (!server_ssl || !client_ssl) { |
| 148 | + fprintf(stderr, "wolfSSL_new failed\n"); |
| 149 | + return 2; |
| 150 | + } |
| 151 | + |
| 152 | + wolfSSL_set_fd(server_ssl, sv[0]); |
| 153 | + wolfSSL_set_fd(client_ssl, sv[1]); |
| 154 | + |
| 155 | + struct side server = { .fd = sv[0], .name = "server", |
| 156 | + .ssl = server_ssl, .fn = wolfSSL_accept }; |
| 157 | + struct side client = { .fd = sv[1], .name = "client", |
| 158 | + .ssl = client_ssl, .fn = wolfSSL_connect }; |
| 159 | + |
| 160 | + pthread_t st, ct; |
| 161 | + pthread_create(&st, NULL, run_side, &server); |
| 162 | + pthread_create(&ct, NULL, run_side, &client); |
| 163 | + pthread_join(st, NULL); |
| 164 | + pthread_join(ct, NULL); |
| 165 | + |
| 166 | + int rc = 0; |
| 167 | + if (server.failed || client.failed) { |
| 168 | + rc = 1; |
| 169 | + } else if (!server.completed || !client.completed) { |
| 170 | + fprintf(stderr, "FAIL: handshake incomplete (server=%d client=%d)\n", |
| 171 | + server.completed, client.completed); |
| 172 | + rc = 1; |
| 173 | + } else if (!server.saw_would_block && !client.saw_would_block) { |
| 174 | + fprintf(stderr, "FAIL: handshake completed but never hit a " |
| 175 | + "non-blocking path. Test scaffolding not exercising " |
| 176 | + "the WOLFSSL_EMNET error-translation code.\n"); |
| 177 | + rc = 1; |
| 178 | + } else { |
| 179 | + printf("OK: handshake completed, would-block paths exercised\n"); |
| 180 | + } |
| 181 | + |
| 182 | + wolfSSL_free(server_ssl); |
| 183 | + wolfSSL_free(client_ssl); |
| 184 | + wolfSSL_CTX_free(sctx); |
| 185 | + wolfSSL_CTX_free(cctx); |
| 186 | + wolfSSL_Cleanup(); |
| 187 | + close(sv[0]); |
| 188 | + close(sv[1]); |
| 189 | + return rc; |
| 190 | +} |
0 commit comments