Skip to content

Commit

Permalink
Repeat gnutls_handshake until success
Browse files Browse the repository at this point in the history
It's blocking, but better than SSL errors.
  • Loading branch information
rkd77 committed Nov 15, 2017
1 parent 91beac7 commit 4c4717b
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/network/ssl/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

#elif defined(CONFIG_GNUTLS)

#define ssl_do_connect(conn) gnutls_handshake(*((ssl_t *) socket->ssl))
#define ssl_do_write(socket, data, len) gnutls_record_send(*((ssl_t *) socket->ssl), data, len)
#define ssl_do_read(socket, data, len) gnutls_record_recv(*((ssl_t *) socket->ssl), data, len)
/* We probably don't handle this entirely correctly.. */
Expand Down Expand Up @@ -365,6 +364,22 @@ verify_callback(int preverify_ok, X509_STORE_CTX *ctx)

#endif /* USE_OPENSSL */

#if defined(CONFIG_GNUTLS)
static int
ssl_do_connect(struct socket *socket)
{
int ret;

gnutls_handshake_set_timeout(*(ssl_t *)(socket->ssl), GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);

do {
ret = gnutls_handshake(*(ssl_t *)socket->ssl);
} while (ret < 0 && !gnutls_error_is_fatal(ret));

return ret;
}
#endif

static void
ssl_want_read(struct socket *socket)
{
Expand Down Expand Up @@ -553,8 +568,12 @@ ssl_read(struct socket *socket, unsigned char *data, int len)
#endif

#ifdef CONFIG_GNUTLS
if (err == GNUTLS_E_REHANDSHAKE)
return -1;
if (err == GNUTLS_E_REHANDSHAKE) {
err = ssl_do_connect(socket);
if (err == 0) {
return SOCKET_SSL_WANT_READ;
}
}
#endif

if (err == SSL_ERROR_WANT_READ ||
Expand Down

0 comments on commit 4c4717b

Please sign in to comment.