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

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

View file

@ -728,7 +728,7 @@ 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;
@ -745,7 +745,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;
@ -762,6 +762,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 ) );
}
/*
* Return authmode from string, or -1 on error
*/
@ -1514,6 +1578,7 @@ int main( int argc, char *argv[] )
{
int ret = 0, len, written, frags, exchanges_left;
int version_suites[4][2];
io_ctx_t io_ctx;
unsigned char* buf = 0;
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
#if defined(MBEDTLS_USE_PSA_CRYPTO)
@ -3170,11 +3235,10 @@ int main( int argc, char *argv[] )
goto exit;
}
if( opt.nbio == 2 )
mbedtls_ssl_set_bio( &ssl, &client_fd, my_send, my_recv, NULL );
else
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv,
opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
io_ctx.ssl = &ssl;
io_ctx.net = &client_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 )