Skip to content

Commit e9a4e7e

Browse files
improvements to return value check and cleanup on failure cases
1 parent 1a5ac9a commit e9a4e7e

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

examples/posix/wh_posix_client/wh_posix_client_cfg.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,25 @@ static unsigned int psk_tls12_client_cb(WOLFSSL* ssl, const char* hint,
198198
unsigned char* key,
199199
unsigned int key_max_len)
200200
{
201+
size_t len;
202+
201203
memset(key, 0, key_max_len);
202204
const char* exampleIdentity = "PSK_EXAMPLE_CLIENT_IDENTITY";
203205

204206
printf("PSK server identity hint: %s\n", hint);
205207
printf("PSK using identity: %s\n", exampleIdentity);
206208
strncpy(identity, exampleIdentity, id_max_len);
207209

208-
printf("Enter a PSK example password to use :");
209-
fgets((char*)key, key_max_len - 1, stdin);
210+
printf("Enter PSK password: ");
211+
if (fgets((char*)key, key_max_len - 1, stdin) == NULL) {
212+
memset(key, 0, key_max_len);
213+
return 0U;
214+
}
210215

211216
(void)ssl;
212-
return strlen((char*)key);
217+
len = strcspn((char*)key, "\n");
218+
((char*)key)[len] = '\0';
219+
return (unsigned int)len;
213220
}
214221

215222
/* Setup WOLFSSL_CTX for use with PSK */

examples/posix/wh_posix_server/wh_posix_server_cfg.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,20 @@ static unsigned int psk_tls12_server_cb(WOLFSSL* ssl, const char* identity,
144144
unsigned char* key,
145145
unsigned int key_max_len)
146146
{
147+
size_t len;
148+
147149
memset(key, 0, key_max_len);
148150
printf("PSK TLS12 server callback\n");
149151
printf("PSK client identity: %s\n", identity);
150-
151-
printf("Enter a PSK example password to accept :");
152-
fgets((char*)key, key_max_len - 1, stdin);
153-
152+
printf("Enter PSK password to accept: ");
153+
if (fgets((char*)key, key_max_len - 1, stdin) == NULL) {
154+
memset(key, 0, key_max_len);
155+
return 0U;
156+
}
157+
len = strcspn((char*)key, "\n");
158+
((char*)key)[len] = '\0';
154159
(void)ssl;
155-
return strlen((char*)key);
160+
return (unsigned int)len;
156161
}
157162

158163
#ifdef WOLFSSL_TLS13
@@ -161,16 +166,23 @@ static unsigned int psk_tls13_server_cb(WOLFSSL* ssl, const char* identity,
161166
unsigned int key_max_len,
162167
const char** ciphersuite)
163168
{
169+
size_t len;
170+
164171
memset(key, 0, key_max_len);
165172
printf("PSK TLS13 server callback\n");
166173
printf("PSK client identity: %s\n", identity);
167174
*ciphersuite = "TLS13-AES128-GCM-SHA256";
168175

169-
printf("Enter a PSK example password to accept :");
170-
fgets((char*)key, key_max_len - 1, stdin);
176+
printf("Enter PSK password: ");
177+
if (fgets((char*)key, key_max_len - 1, stdin) == NULL) {
178+
memset(key, 0, key_max_len);
179+
return 0U;
180+
}
181+
len = strcspn((char*)key, "\n");
182+
((char*)key)[len] = '\0';
171183

172184
(void)ssl;
173-
return strlen((char*)key);
185+
return (unsigned int)len;
174186
}
175187
#endif /* WOLFSSL_TLS13 */
176188

port/posix/posix_transport_tls.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,20 @@ int posixTransportTls_InitConnect(void* context, const void* config,
8686
if (!ctx->ssl) {
8787
wolfSSL_CTX_free(ctx->ssl_ctx);
8888
ctx->ssl_ctx = NULL;
89+
posixTransportTcp_CleanupConnect((void*)&ctx->tcpCtx);
8990
return WH_ERROR_ABORTED;
9091
}
9192

9293
/* Set the socket file descriptor */
93-
wolfSSL_set_fd(ctx->ssl, ctx->connect_fd_p1);
94+
rc = wolfSSL_set_fd(ctx->ssl, ctx->connect_fd_p1);
95+
if (rc != WOLFSSL_SUCCESS) {
96+
wolfSSL_free(ctx->ssl);
97+
ctx->ssl = NULL;
98+
wolfSSL_CTX_free(ctx->ssl_ctx);
99+
ctx->ssl_ctx = NULL;
100+
posixTransportTcp_CleanupConnect((void*)&ctx->tcpCtx);
101+
return WH_ERROR_ABORTED;
102+
}
94103
}
95104
if (ctx->connectcb != NULL) {
96105
ctx->connectcb(ctx->connectcb_arg, WH_COMM_CONNECTED);
@@ -175,15 +184,7 @@ int posixTransportTls_RecvResponse(void* context, uint16_t* out_size,
175184

176185
/* Create SSL object if not already created */
177186
if (ctx->ssl == NULL) {
178-
ctx->ssl = wolfSSL_new(ctx->ssl_ctx);
179-
if (!ctx->ssl) {
180-
wolfSSL_CTX_free(ctx->ssl_ctx);
181-
ctx->ssl_ctx = NULL;
182-
return WH_ERROR_ABORTED;
183-
}
184-
185-
/* Set the socket file descriptor */
186-
wolfSSL_set_fd(ctx->ssl, ctx->connect_fd_p1 - 1);
187+
return WH_ERROR_BADARGS;
187188
}
188189

189190
rc = wolfSSL_read(ctx->ssl, data, PTTLS_PACKET_MAX_SIZE);
@@ -216,7 +217,10 @@ int posixTransportTls_CleanupConnect(void* context)
216217
if (!ctx) {
217218
return WH_ERROR_BADARGS;
218219
}
219-
wolfSSL_free(ctx->ssl);
220+
if (ctx->ssl) {
221+
(void)wolfSSL_shutdown(ctx->ssl);
222+
wolfSSL_free(ctx->ssl);
223+
}
220224
ctx->ssl = NULL;
221225
wolfSSL_CTX_free(ctx->ssl_ctx);
222226
ctx->ssl_ctx = NULL;
@@ -304,9 +308,6 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
304308
return WH_ERROR_NOTREADY;
305309

306310
default:
307-
/* Other error. Assume fatal. */
308-
close(ctx->listen_fd_p1 - 1);
309-
ctx->listen_fd_p1 = 0;
310311
return WH_ERROR_ABORTED;
311312
}
312313
}
@@ -323,7 +324,10 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
323324
}
324325

325326
/* Set the socket file descriptor */
326-
wolfSSL_set_fd(ctx->ssl, ctx->accept_fd_p1 - 1);
327+
rc = wolfSSL_set_fd(ctx->ssl, ctx->accept_fd_p1 - 1);
328+
if (rc != WOLFSSL_SUCCESS) {
329+
return WH_ERROR_ABORTED;
330+
}
327331

328332
/* Perform TLS handshake */
329333
rc = wolfSSL_accept(ctx->ssl);
@@ -333,8 +337,6 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
333337
err == WOLFSSL_ERROR_WANT_WRITE) {
334338
return WH_ERROR_NOTREADY;
335339
}
336-
wolfSSL_free(ctx->ssl);
337-
ctx->ssl = NULL;
338340
return WH_ERROR_ABORTED;
339341
}
340342

@@ -356,9 +358,6 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
356358
}
357359
else {
358360
/* Connection closed */
359-
wolfSSL_free(ctx->ssl);
360-
ctx->ssl = NULL;
361-
posixTransportTcp_CleanupListen((void*)&ctx->tcpCtx);
362361
return WH_ERROR_ABORTED;
363362
}
364363
#else
@@ -416,6 +415,7 @@ int posixTransportTls_CleanupListen(void* context)
416415
}
417416
/* Clean up SSL objects */
418417
if (ctx->ssl) {
418+
(void)wolfSSL_shutdown(ctx->ssl);
419419
wolfSSL_free(ctx->ssl);
420420
ctx->ssl = NULL;
421421
}

0 commit comments

Comments
 (0)