Skip to content

Commit 0592ea7

Browse files
committed
Merge remote-tracking branch 'public/pr/1140' into development
2 parents 1afc767 + 6a4f224 commit 0592ea7

File tree

3 files changed

+663
-75
lines changed

3 files changed

+663
-75
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Bugfix
1010

1111
Changes
1212
* Add tests for session resumption in DTLS.
13+
* Close a test gap in (D)TLS between the client side and the server side:
14+
test the handling of large packets and small packets on the client side
15+
in the same way as on the server side.
1316

1417
= mbed TLS 2.13.1 branch released 2018-09-06
1518

programs/ssl/ssl_server2.c

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int main( void )
103103

104104
#define DFL_SERVER_ADDR NULL
105105
#define DFL_SERVER_PORT "4433"
106+
#define DFL_RESPONSE_SIZE -1
106107
#define DFL_DEBUG_LEVEL 0
107108
#define DFL_NBIO 0
108109
#define DFL_EVENT 0
@@ -177,7 +178,7 @@ int main( void )
177178
* You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
178179
* if you change this value to something outside the range <= 100 or > 500
179180
*/
180-
#define IO_BUF_LEN 200
181+
#define DFL_IO_BUF_LEN 200
181182

182183
#if defined(MBEDTLS_X509_CRT_PARSE_C)
183184
#if defined(MBEDTLS_FS_IO)
@@ -356,6 +357,11 @@ int main( void )
356357
" server_addr=%%s default: (all interfaces)\n" \
357358
" server_port=%%d default: 4433\n" \
358359
" debug_level=%%d default: 0 (disabled)\n" \
360+
" buffer_size=%%d default: 200 \n" \
361+
" (minimum: 1, max: 16385)\n" \
362+
" response_size=%%d default: about 152 (basic response)\n" \
363+
" (minimum: 0, max: 16384)\n" \
364+
" increases buffer_size if bigger\n"\
359365
" nbio=%%d default: 0 (blocking I/O)\n" \
360366
" options: 1 (non-blocking), 2 (added delays)\n" \
361367
" event=%%d default: 0 (loop)\n" \
@@ -431,6 +437,8 @@ struct options
431437
int nbio; /* should I/O be blocking? */
432438
int event; /* loop or event-driven IO? level or edge triggered? */
433439
uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
440+
int response_size; /* pad response with header to requested size */
441+
uint16_t buffer_size; /* IO buffer size */
434442
const char *ca_file; /* the file with the CA certificate(s) */
435443
const char *ca_path; /* the path with the CA certificate(s) reside */
436444
const char *crt_file; /* the file with the server certificate */
@@ -1166,7 +1174,7 @@ int main( int argc, char *argv[] )
11661174
{
11671175
int ret = 0, len, written, frags, exchanges_left;
11681176
int version_suites[4][2];
1169-
unsigned char buf[IO_BUF_LEN];
1177+
unsigned char* buf = 0;
11701178
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11711179
unsigned char psk[MBEDTLS_PSK_MAX_LEN];
11721180
size_t psk_len = 0;
@@ -1297,10 +1305,12 @@ int main( int argc, char *argv[] )
12971305
goto exit;
12981306
}
12991307

1308+
opt.buffer_size = DFL_IO_BUF_LEN;
13001309
opt.server_addr = DFL_SERVER_ADDR;
13011310
opt.server_port = DFL_SERVER_PORT;
13021311
opt.debug_level = DFL_DEBUG_LEVEL;
13031312
opt.event = DFL_EVENT;
1313+
opt.response_size = DFL_RESPONSE_SIZE;
13041314
opt.nbio = DFL_NBIO;
13051315
opt.read_timeout = DFL_READ_TIMEOUT;
13061316
opt.ca_file = DFL_CA_FILE;
@@ -1393,6 +1403,20 @@ int main( int argc, char *argv[] )
13931403
}
13941404
else if( strcmp( p, "read_timeout" ) == 0 )
13951405
opt.read_timeout = atoi( q );
1406+
else if( strcmp( p, "buffer_size" ) == 0 )
1407+
{
1408+
opt.buffer_size = atoi( q );
1409+
if( opt.buffer_size < 1 || opt.buffer_size > MBEDTLS_SSL_MAX_CONTENT_LEN + 1 )
1410+
goto usage;
1411+
}
1412+
else if( strcmp( p, "response_size" ) == 0 )
1413+
{
1414+
opt.response_size = atoi( q );
1415+
if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_MAX_CONTENT_LEN )
1416+
goto usage;
1417+
if( opt.buffer_size < opt.response_size )
1418+
opt.buffer_size = opt.response_size;
1419+
}
13961420
else if( strcmp( p, "ca_file" ) == 0 )
13971421
opt.ca_file = q;
13981422
else if( strcmp( p, "ca_path" ) == 0 )
@@ -1729,6 +1753,13 @@ int main( int argc, char *argv[] )
17291753
#if defined(MBEDTLS_DEBUG_C)
17301754
mbedtls_debug_set_threshold( opt.debug_level );
17311755
#endif
1756+
buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
1757+
if( buf == NULL )
1758+
{
1759+
mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
1760+
ret = 3;
1761+
goto exit;
1762+
}
17321763

17331764
if( opt.force_ciphersuite[0] > 0 )
17341765
{
@@ -2745,8 +2776,8 @@ int main( int argc, char *argv[] )
27452776
do
27462777
{
27472778
int terminated = 0;
2748-
len = sizeof( buf ) - 1;
2749-
memset( buf, 0, sizeof( buf ) );
2779+
len = opt.buffer_size - 1;
2780+
memset( buf, 0, opt.buffer_size );
27502781
ret = mbedtls_ssl_read( &ssl, buf, len );
27512782

27522783
if( mbedtls_status_is_ssl_in_progress( ret ) )
@@ -2846,8 +2877,8 @@ int main( int argc, char *argv[] )
28462877
}
28472878
else /* Not stream, so datagram */
28482879
{
2849-
len = sizeof( buf ) - 1;
2850-
memset( buf, 0, sizeof( buf ) );
2880+
len = opt.buffer_size - 1;
2881+
memset( buf, 0, opt.buffer_size );
28512882

28522883
do
28532884
{
@@ -2945,6 +2976,25 @@ int main( int argc, char *argv[] )
29452976
len = sprintf( (char *) buf, HTTP_RESPONSE,
29462977
mbedtls_ssl_get_ciphersuite( &ssl ) );
29472978

2979+
/* Add padding to the response to reach opt.response_size in length */
2980+
if( opt.response_size != DFL_RESPONSE_SIZE &&
2981+
len < opt.response_size )
2982+
{
2983+
memset( buf + len, 'B', opt.response_size - len );
2984+
len += opt.response_size - len;
2985+
}
2986+
2987+
/* Truncate if response size is smaller than the "natural" size */
2988+
if( opt.response_size != DFL_RESPONSE_SIZE &&
2989+
len > opt.response_size )
2990+
{
2991+
len = opt.response_size;
2992+
2993+
/* Still end with \r\n unless that's really not possible */
2994+
if( len >= 2 ) buf[len - 2] = '\r';
2995+
if( len >= 1 ) buf[len - 1] = '\n';
2996+
}
2997+
29482998
if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
29492999
{
29503000
for( written = 0, frags = 0; written < len; written += ret, frags++ )
@@ -3103,6 +3153,7 @@ int main( int argc, char *argv[] )
31033153
mbedtls_memory_buffer_alloc_free();
31043154
#endif
31053155

3156+
mbedtls_free( buf );
31063157
mbedtls_printf( " done.\n" );
31073158

31083159
#if defined(_WIN32)

0 commit comments

Comments
 (0)