Add IO wrappers to ssl_client2 as interm's between NET and SSL layer

This commit is contained in:
Hanno Becker 2019-07-03 17:02:43 +01:00 committed by Jarno Lamsa
parent cfe457921a
commit 8b1af2f89c

View File

@ -617,7 +617,8 @@ exit:
* Test recv/send functions that make sure each try returns
* WANT_READ/WANT_WRITE at least once before sucesseding
*/
static int my_recv( void *ctx, unsigned char *buf, size_t len )
static int delayed_recv( void *ctx, unsigned char *buf, size_t len )
{
static int first_try = 1;
int ret;
@ -634,7 +635,7 @@ static int my_recv( void *ctx, unsigned char *buf, size_t len )
return( ret );
}
static int my_send( void *ctx, const unsigned char *buf, size_t len )
static int delayed_send( void *ctx, const unsigned char *buf, size_t len )
{
static int first_try = 1;
int ret;
@ -651,6 +652,70 @@ static int my_send( void *ctx, const unsigned char *buf, size_t len )
return( ret );
}
typedef struct
{
mbedtls_ssl_context *ssl;
mbedtls_net_context *net;
} io_ctx_t;
static int recv_cb( void *ctx, unsigned char *buf, size_t len )
{
io_ctx_t *io_ctx = (io_ctx_t*) ctx;
size_t recv_len;
int ret;
if( opt.nbio == 2 )
ret = delayed_recv( io_ctx->net, buf, len );
else
ret = mbedtls_net_recv( io_ctx->net, buf, len );
if( ret < 0 )
return( ret );
recv_len = (size_t) ret;
if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
{
/* Here's the place to do any datagram/record checking
* in between receiving the packet from the underlying
* transport and passing it on to the TLS stack. */
mbedtls_printf( "[RECV] Datagram of size %u\n", (unsigned) recv_len );
}
return( (int) recv_len );
}
static int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
uint32_t timeout )
{
io_ctx_t *io_ctx = (io_ctx_t*) ctx;
int ret;
size_t recv_len;
ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
if( ret < 0 )
return( ret );
recv_len = (size_t) ret;
if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
{
/* Here's the place to do any datagram/record checking
* in between receiving the packet from the underlying
* transport and passing it on to the TLS stack. */
mbedtls_printf( "[RECV] Datagram of size %u\n", (unsigned) recv_len );
}
return( (int) recv_len );
}
static int send_cb( void *ctx, unsigned char const *buf, size_t len )
{
io_ctx_t *io_ctx = (io_ctx_t*) ctx;
if( opt.nbio == 2 )
return( delayed_send( io_ctx->net, buf, len ) );
return( mbedtls_net_send( io_ctx->net, buf, len ) );
}
#if defined(MBEDTLS_X509_CRT_PARSE_C)
static unsigned char peer_crt_info[1024];
@ -874,6 +939,7 @@ int main( int argc, char *argv[] )
{
int ret = 0, len, tail_len, i, written, frags, retry_left;
mbedtls_net_context server_fd;
io_ctx_t io_ctx;
unsigned char buf[MAX_REQUEST_SIZE + 1];
@ -2162,12 +2228,10 @@ int main( int argc, char *argv[] )
mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
#endif /* MBEDTLS_X509_CRT_PARSE_C */
if( opt.nbio == 2 )
mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
else
mbedtls_ssl_set_bio( &ssl, &server_fd,
mbedtls_net_send, mbedtls_net_recv,
opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
io_ctx.ssl = &ssl;
io_ctx.net = &server_fd;
mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
opt.nbio == 0 ? recv_timeout_cb : NULL );
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )