Skip to content

Commit 3949138

Browse files
authored
Merge pull request #431 from dgarske/tls_shutdown
Improve the TLS bi-directional shutdown
2 parents cb1e647 + 877a43d commit 3949138

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

examples/tls/tls_client.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,17 @@ int TPM2_TLS_ClientArgs(void* userCtx, int argc, char *argv[])
639639
printf("Failure %d (0x%x): %s\n", rc, rc, wolfTPM2_GetRCString(rc));
640640
}
641641

642-
/* Bidirectional shutdown */
643-
while (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
644-
printf("Shutdown not complete\n");
642+
if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
643+
/* Bidirectional shutdown */
644+
if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) {
645+
int ret = wolfSSL_shutdown(ssl);
646+
if (ret == WOLFSSL_SUCCESS) {
647+
printf("Bidirectional shutdown complete\n");
648+
}
649+
else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
650+
fprintf(stderr, "Bidirectional shutdown failed\n");
651+
}
652+
}
645653
}
646654

647655
wolfSSL_free(ssl);

examples/tls/tls_common.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ static inline int SetupSocketAndConnect(SockIoCbCtx* sockIoCtx, const char* host
322322
return 0;
323323
}
324324

325+
static inline int SocketWaitData(SockIoCbCtx* sockIoCtx, int timeout_sec)
326+
{
327+
int res;
328+
struct timeval timeout;
329+
fd_set fds, errfds;
330+
FD_ZERO(&fds);
331+
FD_ZERO(&errfds);
332+
FD_SET(sockIoCtx->fd, &fds);
333+
FD_SET(sockIoCtx->fd, &errfds);
334+
timeout.tv_sec = timeout_sec;
335+
timeout.tv_usec = 0;
336+
res = select(sockIoCtx->fd + 1, &fds, NULL, &errfds, &timeout);
337+
if (res == 0) {
338+
return 0; /* timeout */
339+
}
340+
else if (res > 0) {
341+
if (FD_ISSET(sockIoCtx->fd, &fds)) {
342+
return 1; /* ready to read */
343+
}
344+
else if (FD_ISSET(sockIoCtx->fd, &errfds)) {
345+
return -1; /* error */
346+
}
347+
}
348+
return 0; /* select failed */
349+
}
350+
325351
static inline void CloseAndCleanupSocket(SockIoCbCtx* sockIoCtx)
326352
{
327353
if (sockIoCtx->fd != -1) {
@@ -343,6 +369,7 @@ static inline void CloseAndCleanupSocket(SockIoCbCtx* sockIoCtx)
343369

344370
int SetupSocketAndListen(SockIoCbCtx* sockIoCtx, word32 port);
345371
int SocketWaitClient(SockIoCbCtx* sockIoCtx);
372+
int SocketWaitData(SockIoCbCtx* sockIoCtx, int timeout_sec);
346373
#endif /* !WOLFSSL_USER_IO */
347374

348375
/******************************************************************************/

examples/tls/tls_server.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,17 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
672672
}
673673
}
674674

675+
if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
675676
/* Bidirectional shutdown */
676-
while (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
677-
printf("Shutdown not complete\n");
677+
if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) {
678+
int ret = wolfSSL_shutdown(ssl);
679+
if (ret == WOLFSSL_SUCCESS) {
680+
printf("Bidirectional shutdown complete\n");
681+
}
682+
else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
683+
fprintf(stderr, "Bidirectional shutdown failed\n");
684+
}
685+
}
678686
}
679687

680688
wolfSSL_free(ssl);
@@ -691,11 +699,6 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
691699
}
692700

693701
if (ssl != NULL) {
694-
/* Bidirectional shutdown */
695-
while (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
696-
printf("Shutdown not complete\n");
697-
}
698-
699702
wolfSSL_free(ssl);
700703
}
701704
wolfSSL_CTX_free(ctx);

src/tpm2_cryptocb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
120120
#endif
121121
rc = exit_rc;
122122
}
123+
#if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX > 0x05006000
123124
else if (info->pk.type == WC_PK_TYPE_RSA_GET_SIZE) {
124125
if (tlsCtx->rsaKey != NULL) {
125126
*info->pk.rsa_get_size.keySize =
@@ -128,6 +129,7 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
128129
rc = 0;
129130
}
130131
}
132+
#endif
131133
else if (info->pk.type == WC_PK_TYPE_RSA) {
132134
switch (info->pk.rsa.type) {
133135
case RSA_PUBLIC_ENCRYPT:

0 commit comments

Comments
 (0)