1
0
mirror of https://github.com/ARMmbed/mbedtls.git synced 2025-05-12 01:42:21 +08:00

Fix ssl_read() and close_notify error handling in programs

This commit is contained in:
Manuel Pégourié-Gonnard 2014-08-16 11:28:40 +02:00
parent 67686c42e6
commit e08660e612
2 changed files with 49 additions and 27 deletions

View File

@ -1108,23 +1108,29 @@ send_request:
memset( buf, 0, sizeof( buf ) ); memset( buf, 0, sizeof( buf ) );
ret = ssl_read( &ssl, buf, len ); ret = ssl_read( &ssl, buf, len );
if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) if( ret == POLARSSL_ERR_NET_WANT_READ ||
ret == POLARSSL_ERR_NET_WANT_WRITE )
continue; continue;
if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) if( ret <= 0 )
break;
if( ret < 0 )
{ {
printf( "failed\n ! ssl_read returned -0x%x\n\n", -ret ); switch( ret )
break; {
case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
printf( " connection was closed gracefully\n" );
ret = 0;
goto reconnect;
case 0:
case POLARSSL_ERR_NET_CONN_RESET:
printf( " connection was reset by peer\n" );
ret = 0;
goto reconnect;
default:
printf( " ssl_read returned -0x%x\n", -ret );
goto exit;
} }
if( ret == 0 )
{
printf("\n\nEOF\n\n");
ssl_close_notify( &ssl );
break;
} }
len = ret; len = ret;
@ -1133,6 +1139,10 @@ send_request:
} }
while( 1 ); while( 1 );
/*
* 9. Reconnect?
*/
reconnect:
if( opt.reconnect != 0 ) if( opt.reconnect != 0 )
{ {
--opt.reconnect; --opt.reconnect;
@ -1181,10 +1191,10 @@ send_request:
goto send_request; goto send_request;
} }
/*
* Cleanup and exit
*/
exit: exit:
if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
ret = 0;
#ifdef POLARSSL_ERROR_C #ifdef POLARSSL_ERROR_C
if( ret != 0 ) if( ret != 0 )
{ {
@ -1196,6 +1206,7 @@ exit:
if( server_fd ) if( server_fd )
net_close( server_fd ); net_close( server_fd );
#if defined(POLARSSL_X509_CRT_PARSE_C) #if defined(POLARSSL_X509_CRT_PARSE_C)
x509_crt_free( &clicert ); x509_crt_free( &clicert );
x509_crt_free( &cacert ); x509_crt_free( &cacert );
@ -1206,8 +1217,6 @@ exit:
ctr_drbg_free( &ctr_drbg ); ctr_drbg_free( &ctr_drbg );
entropy_free( &entropy ); entropy_free( &entropy );
memset( &ssl, 0, sizeof( ssl ) );
#if defined(_WIN32) #if defined(_WIN32)
printf( " + Press Enter to exit this program.\n" ); printf( " + Press Enter to exit this program.\n" );
fflush( stdout ); getchar(); fflush( stdout ); getchar();

View File

@ -1514,7 +1514,8 @@ reset:
memset( buf, 0, sizeof( buf ) ); memset( buf, 0, sizeof( buf ) );
ret = ssl_read( &ssl, buf, len ); ret = ssl_read( &ssl, buf, len );
if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) if( ret == POLARSSL_ERR_NET_WANT_READ ||
ret == POLARSSL_ERR_NET_WANT_WRITE )
continue; continue;
if( ret <= 0 ) if( ret <= 0 )
@ -1523,18 +1524,18 @@ reset:
{ {
case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
printf( " connection was closed gracefully\n" ); printf( " connection was closed gracefully\n" );
break; goto close_notify;
case 0:
case POLARSSL_ERR_NET_CONN_RESET: case POLARSSL_ERR_NET_CONN_RESET:
printf( " connection was reset by peer\n" ); printf( " connection was reset by peer\n" );
break; ret = POLARSSL_ERR_NET_CONN_RESET;
goto reset;
default: default:
printf( " ssl_read returned -0x%x\n", -ret ); printf( " ssl_read returned -0x%x\n", -ret );
break; goto close_notify;
} }
break;
} }
if( ssl_get_bytes_avail( &ssl ) == 0 ) if( ssl_get_bytes_avail( &ssl ) == 0 )
@ -1658,10 +1659,22 @@ reset:
printf( " ok\n" ); printf( " ok\n" );
} }
/*
* 8. Close the connection cleanly
*/
close_notify:
printf( " . Closing the connection..." ); printf( " . Closing the connection..." );
while( ( ret = ssl_close_notify( &ssl ) ) < 0 ) while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
{ {
printf( " ret = %d (-0x%04X)", ret, -ret );
if( ret == POLARSSL_ERR_NET_CONN_RESET )
{
printf( " connection was reset by peer\n" );
ret = 0;
goto reset;
}
if( ret != POLARSSL_ERR_NET_WANT_READ && if( ret != POLARSSL_ERR_NET_WANT_READ &&
ret != POLARSSL_ERR_NET_WANT_WRITE ) ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
@ -1671,12 +1684,12 @@ reset:
} }
printf( " ok\n" ); printf( " ok\n" );
ret = 0;
goto reset; goto reset;
/*
* Cleanup and exit
*/
exit: exit:
#ifdef POLARSSL_ERROR_C #ifdef POLARSSL_ERROR_C
if( ret != 0 ) if( ret != 0 )
{ {