1/* BEGIN_HEADER */ 2#include <mbedtls/ssl.h> 3#include <mbedtls/ssl_internal.h> 4#include <mbedtls/ctr_drbg.h> 5#include <mbedtls/entropy.h> 6#include <mbedtls/certs.h> 7#include <mbedtls/timing.h> 8#include <mbedtls/debug.h> 9#include <ssl_tls13_keys.h> 10 11#include <constant_time_internal.h> 12 13#include <test/constant_flow.h> 14 15enum 16{ 17#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ 18 tls1_3_label_ ## name, 19MBEDTLS_SSL_TLS1_3_LABEL_LIST 20#undef MBEDTLS_SSL_TLS1_3_LABEL 21}; 22 23typedef struct log_pattern 24{ 25 const char *pattern; 26 size_t counter; 27} log_pattern; 28 29/* 30 * This function can be passed to mbedtls to receive output logs from it. In 31 * this case, it will count the instances of a log_pattern in the received 32 * logged messages. 33 */ 34void log_analyzer( void *ctx, int level, 35 const char *file, int line, 36 const char *str ) 37{ 38 log_pattern *p = (log_pattern *) ctx; 39 40 (void) level; 41 (void) line; 42 (void) file; 43 44 if( NULL != p && 45 NULL != p->pattern && 46 NULL != strstr( str, p->pattern ) ) 47 { 48 p->counter++; 49 } 50} 51 52/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */ 53#define TEST_SSL_MINOR_VERSION_NONE -1 54 55typedef struct handshake_test_options 56{ 57 const char *cipher; 58 int client_min_version; 59 int client_max_version; 60 int server_min_version; 61 int server_max_version; 62 int expected_negotiated_version; 63 int pk_alg; 64 data_t *psk_str; 65 int dtls; 66 int srv_auth_mode; 67 int serialize; 68 int mfl; 69 int cli_msg_len; 70 int srv_msg_len; 71 int expected_cli_fragments; 72 int expected_srv_fragments; 73 int renegotiate; 74 int legacy_renegotiation; 75 void *srv_log_obj; 76 void *cli_log_obj; 77 void (*srv_log_fun)(void *, int, const char *, int, const char *); 78 void (*cli_log_fun)(void *, int, const char *, int, const char *); 79 int resize_buffers; 80} handshake_test_options; 81 82void init_handshake_options( handshake_test_options *opts ) 83{ 84 opts->cipher = ""; 85 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE; 86 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE; 87 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE; 88 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE; 89 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3; 90 opts->pk_alg = MBEDTLS_PK_RSA; 91 opts->psk_str = NULL; 92 opts->dtls = 0; 93 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; 94 opts->serialize = 0; 95 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; 96 opts->cli_msg_len = 100; 97 opts->srv_msg_len = 100; 98 opts->expected_cli_fragments = 1; 99 opts->expected_srv_fragments = 1; 100 opts->renegotiate = 0; 101 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; 102 opts->srv_log_obj = NULL; 103 opts->srv_log_obj = NULL; 104 opts->srv_log_fun = NULL; 105 opts->cli_log_fun = NULL; 106 opts->resize_buffers = 1; 107} 108/* 109 * Buffer structure for custom I/O callbacks. 110 */ 111 112typedef struct mbedtls_test_buffer 113{ 114 size_t start; 115 size_t content_length; 116 size_t capacity; 117 unsigned char *buffer; 118} mbedtls_test_buffer; 119 120/* 121 * Initialises \p buf. After calling this function it is safe to call 122 * `mbedtls_test_buffer_free()` on \p buf. 123 */ 124void mbedtls_test_buffer_init( mbedtls_test_buffer *buf ) 125{ 126 memset( buf, 0, sizeof( *buf ) ); 127} 128 129/* 130 * Sets up \p buf. After calling this function it is safe to call 131 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf. 132 */ 133int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity ) 134{ 135 buf->buffer = (unsigned char*) mbedtls_calloc( capacity, 136 sizeof(unsigned char) ); 137 if( NULL == buf->buffer ) 138 return MBEDTLS_ERR_SSL_ALLOC_FAILED; 139 buf->capacity = capacity; 140 141 return 0; 142} 143 144void mbedtls_test_buffer_free( mbedtls_test_buffer *buf ) 145{ 146 if( buf->buffer != NULL ) 147 mbedtls_free( buf->buffer ); 148 149 memset( buf, 0, sizeof( *buf ) ); 150} 151 152/* 153 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. 154 * 155 * \p buf must have been initialized and set up by calling 156 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. 157 * 158 * \retval \p input_len, if the data fits. 159 * \retval 0 <= value < \p input_len, if the data does not fit. 160 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not 161 * zero and \p input is NULL. 162 */ 163int mbedtls_test_buffer_put( mbedtls_test_buffer *buf, 164 const unsigned char *input, size_t input_len ) 165{ 166 size_t overflow = 0; 167 168 if( ( buf == NULL ) || ( buf->buffer == NULL ) ) 169 return -1; 170 171 /* Reduce input_len to a number that fits in the buffer. */ 172 if ( ( buf->content_length + input_len ) > buf->capacity ) 173 { 174 input_len = buf->capacity - buf->content_length; 175 } 176 177 if( input == NULL ) 178 { 179 return ( input_len == 0 ) ? 0 : -1; 180 } 181 182 /* Check if the buffer has not come full circle and free space is not in 183 * the middle */ 184 if( buf->start + buf->content_length < buf->capacity ) 185 { 186 187 /* Calculate the number of bytes that need to be placed at lower memory 188 * address */ 189 if( buf->start + buf->content_length + input_len 190 > buf->capacity ) 191 { 192 overflow = ( buf->start + buf->content_length + input_len ) 193 % buf->capacity; 194 } 195 196 memcpy( buf->buffer + buf->start + buf->content_length, input, 197 input_len - overflow ); 198 memcpy( buf->buffer, input + input_len - overflow, overflow ); 199 200 } 201 else 202 { 203 /* The buffer has come full circle and free space is in the middle */ 204 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity, 205 input, input_len ); 206 } 207 208 buf->content_length += input_len; 209 return input_len; 210} 211 212/* 213 * Gets \p output_len bytes from the ring buffer \p buf into the 214 * \p output buffer. The output buffer can be NULL, in this case a part of the 215 * ring buffer will be dropped, if the requested length is available. 216 * 217 * \p buf must have been initialized and set up by calling 218 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. 219 * 220 * \retval \p output_len, if the data is available. 221 * \retval 0 <= value < \p output_len, if the data is not available. 222 * \retval -1, if \buf is NULL or it hasn't been set up. 223 */ 224int mbedtls_test_buffer_get( mbedtls_test_buffer *buf, 225 unsigned char* output, size_t output_len ) 226{ 227 size_t overflow = 0; 228 229 if( ( buf == NULL ) || ( buf->buffer == NULL ) ) 230 return -1; 231 232 if( output == NULL && output_len == 0 ) 233 return 0; 234 235 if( buf->content_length < output_len ) 236 output_len = buf->content_length; 237 238 /* Calculate the number of bytes that need to be drawn from lower memory 239 * address */ 240 if( buf->start + output_len > buf->capacity ) 241 { 242 overflow = ( buf->start + output_len ) % buf->capacity; 243 } 244 245 if( output != NULL ) 246 { 247 memcpy( output, buf->buffer + buf->start, output_len - overflow ); 248 memcpy( output + output_len - overflow, buf->buffer, overflow ); 249 } 250 251 buf->content_length -= output_len; 252 buf->start = ( buf->start + output_len ) % buf->capacity; 253 254 return output_len; 255} 256 257/* 258 * Errors used in the message transport mock tests 259 */ 260 #define MBEDTLS_TEST_ERROR_ARG_NULL -11 261 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 262 263/* 264 * Context for a message metadata queue (fifo) that is on top of the ring buffer. 265 */ 266typedef struct mbedtls_test_message_queue 267{ 268 size_t *messages; 269 int pos; 270 int num; 271 int capacity; 272} mbedtls_test_message_queue; 273 274/* 275 * Setup and free functions for the message metadata queue. 276 * 277 * \p capacity describes the number of message metadata chunks that can be held 278 * within the queue. 279 * 280 * \retval 0, if a metadata queue of a given length can be allocated. 281 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. 282 */ 283int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue, 284 size_t capacity ) 285{ 286 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) ); 287 if( NULL == queue->messages ) 288 return MBEDTLS_ERR_SSL_ALLOC_FAILED; 289 290 queue->capacity = capacity; 291 queue->pos = 0; 292 queue->num = 0; 293 294 return 0; 295} 296 297void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue ) 298{ 299 if( queue == NULL ) 300 return; 301 302 if( queue->messages != NULL ) 303 mbedtls_free( queue->messages ); 304 305 memset( queue, 0, sizeof( *queue ) ); 306} 307 308/* 309 * Push message length information onto the message metadata queue. 310 * This will become the last element to leave it (fifo). 311 * 312 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. 313 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. 314 * \retval \p len, if the push was successful. 315 */ 316int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue, 317 size_t len ) 318{ 319 int place; 320 if( queue == NULL ) 321 return MBEDTLS_TEST_ERROR_ARG_NULL; 322 323 if( queue->num >= queue->capacity ) 324 return MBEDTLS_ERR_SSL_WANT_WRITE; 325 326 place = ( queue->pos + queue->num ) % queue->capacity; 327 queue->messages[place] = len; 328 queue->num++; 329 return len; 330} 331 332/* 333 * Pop information about the next message length from the queue. This will be 334 * the oldest inserted message length(fifo). \p msg_len can be null, in which 335 * case the data will be popped from the queue but not copied anywhere. 336 * 337 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. 338 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. 339 * \retval message length, if the pop was successful, up to the given 340 \p buf_len. 341 */ 342int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue, 343 size_t buf_len ) 344{ 345 size_t message_length; 346 if( queue == NULL ) 347 return MBEDTLS_TEST_ERROR_ARG_NULL; 348 if( queue->num == 0 ) 349 return MBEDTLS_ERR_SSL_WANT_READ; 350 351 message_length = queue->messages[queue->pos]; 352 queue->messages[queue->pos] = 0; 353 queue->num--; 354 queue->pos++; 355 queue->pos %= queue->capacity; 356 if( queue->pos < 0 ) 357 queue->pos += queue->capacity; 358 359 return ( message_length > buf_len ) ? buf_len : message_length; 360} 361 362/* 363 * Take a peek on the info about the next message length from the queue. 364 * This will be the oldest inserted message length(fifo). 365 * 366 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. 367 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. 368 * \retval 0, if the peek was successful. 369 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is 370 * too small to fit the message. In this case the \p msg_len will be 371 * set to the full message length so that the 372 * caller knows what portion of the message can be dropped. 373 */ 374int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue, 375 size_t buf_len, size_t* msg_len ) 376{ 377 if( queue == NULL || msg_len == NULL ) 378 return MBEDTLS_TEST_ERROR_ARG_NULL; 379 if( queue->num == 0 ) 380 return MBEDTLS_ERR_SSL_WANT_READ; 381 382 *msg_len = queue->messages[queue->pos]; 383 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; 384} 385/* 386 * Context for the I/O callbacks simulating network connection. 387 */ 388 389#define MBEDTLS_MOCK_SOCKET_CONNECTED 1 390 391typedef struct mbedtls_mock_socket 392{ 393 int status; 394 mbedtls_test_buffer *input; 395 mbedtls_test_buffer *output; 396 struct mbedtls_mock_socket *peer; 397} mbedtls_mock_socket; 398 399/* 400 * Setup and teardown functions for mock sockets. 401 */ 402void mbedtls_mock_socket_init( mbedtls_mock_socket *socket ) 403{ 404 memset( socket, 0, sizeof( *socket ) ); 405} 406 407/* 408 * Closes the socket \p socket. 409 * 410 * \p socket must have been previously initialized by calling 411 * mbedtls_mock_socket_init(). 412 * 413 * This function frees all allocated resources and both sockets are aware of the 414 * new connection state. 415 * 416 * That is, this function does not simulate half-open TCP connections and the 417 * phenomenon that when closing a UDP connection the peer is not aware of the 418 * connection having been closed. 419 */ 420void mbedtls_mock_socket_close( mbedtls_mock_socket* socket ) 421{ 422 if( socket == NULL ) 423 return; 424 425 if( socket->input != NULL ) 426 { 427 mbedtls_test_buffer_free( socket->input ); 428 mbedtls_free( socket->input ); 429 } 430 431 if( socket->output != NULL ) 432 { 433 mbedtls_test_buffer_free( socket->output ); 434 mbedtls_free( socket->output ); 435 } 436 437 if( socket->peer != NULL ) 438 memset( socket->peer, 0, sizeof( *socket->peer ) ); 439 440 memset( socket, 0, sizeof( *socket ) ); 441} 442 443/* 444 * Establishes a connection between \p peer1 and \p peer2. 445 * 446 * \p peer1 and \p peer2 must have been previously initialized by calling 447 * mbedtls_mock_socket_init(). 448 * 449 * The capacites of the internal buffers are set to \p bufsize. Setting this to 450 * the correct value allows for simulation of MTU, sanity testing the mock 451 * implementation and mocking TCP connections with lower memory cost. 452 */ 453int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1, 454 mbedtls_mock_socket* peer2, 455 size_t bufsize ) 456{ 457 int ret = -1; 458 459 peer1->output = 460 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) ); 461 if( peer1->output == NULL ) 462 { 463 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; 464 goto exit; 465 } 466 mbedtls_test_buffer_init( peer1->output ); 467 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) ) 468 { 469 goto exit; 470 } 471 472 peer2->output = 473 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) ); 474 if( peer2->output == NULL ) 475 { 476 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; 477 goto exit; 478 } 479 mbedtls_test_buffer_init( peer2->output ); 480 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) ) 481 { 482 goto exit; 483 } 484 485 peer1->peer = peer2; 486 peer2->peer = peer1; 487 peer1->input = peer2->output; 488 peer2->input = peer1->output; 489 490 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; 491 ret = 0; 492 493exit: 494 495 if( ret != 0 ) 496 { 497 mbedtls_mock_socket_close( peer1 ); 498 mbedtls_mock_socket_close( peer2 ); 499 } 500 501 return ret; 502} 503 504/* 505 * Callbacks for simulating blocking I/O over connection-oriented transport. 506 */ 507 508int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len ) 509{ 510 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; 511 512 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) 513 return -1; 514 515 return mbedtls_test_buffer_put( socket->output, buf, len ); 516} 517 518int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len ) 519{ 520 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; 521 522 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) 523 return -1; 524 525 return mbedtls_test_buffer_get( socket->input, buf, len ); 526} 527 528/* 529 * Callbacks for simulating non-blocking I/O over connection-oriented transport. 530 */ 531 532int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len ) 533{ 534 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; 535 536 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) 537 return -1; 538 539 if( socket->output->capacity == socket->output->content_length ) 540 { 541 return MBEDTLS_ERR_SSL_WANT_WRITE; 542 } 543 544 return mbedtls_test_buffer_put( socket->output, buf, len ); 545} 546 547int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len ) 548{ 549 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; 550 551 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) 552 return -1; 553 554 if( socket->input->content_length == 0 ) 555 { 556 return MBEDTLS_ERR_SSL_WANT_READ; 557 } 558 559 return mbedtls_test_buffer_get( socket->input, buf, len ); 560} 561 562/* Errors used in the message socket mocks */ 563 564#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 565#define MBEDTLS_TEST_ERROR_SEND_FAILED -66 566#define MBEDTLS_TEST_ERROR_RECV_FAILED -77 567 568/* 569 * Structure used as an addon, or a wrapper, around the mocked sockets. 570 * Contains an input queue, to which the other socket pushes metadata, 571 * and an output queue, to which this one pushes metadata. This context is 572 * considered as an owner of the input queue only, which is initialized and 573 * freed in the respective setup and free calls. 574 */ 575typedef struct mbedtls_test_message_socket_context 576{ 577 mbedtls_test_message_queue* queue_input; 578 mbedtls_test_message_queue* queue_output; 579 mbedtls_mock_socket* socket; 580} mbedtls_test_message_socket_context; 581 582void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx ) 583{ 584 ctx->queue_input = NULL; 585 ctx->queue_output = NULL; 586 ctx->socket = NULL; 587} 588 589/* 590 * Setup a given mesasge socket context including initialization of 591 * input/output queues to a chosen capacity of messages. Also set the 592 * corresponding mock socket. 593 * 594 * \retval 0, if everything succeeds. 595 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message 596 * queue failed. 597 */ 598int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input, 599 mbedtls_test_message_queue* queue_output, 600 size_t queue_capacity, 601 mbedtls_mock_socket* socket, 602 mbedtls_test_message_socket_context* ctx ) 603{ 604 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity ); 605 if( ret != 0 ) 606 return ret; 607 ctx->queue_input = queue_input; 608 ctx->queue_output = queue_output; 609 ctx->socket = socket; 610 mbedtls_mock_socket_init( socket ); 611 612 return 0; 613} 614 615/* 616 * Close a given message socket context, along with the socket itself. Free the 617 * memory allocated by the input queue. 618 */ 619void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx ) 620{ 621 if( ctx == NULL ) 622 return; 623 624 mbedtls_test_message_queue_free( ctx->queue_input ); 625 mbedtls_mock_socket_close( ctx->socket ); 626 memset( ctx, 0, sizeof( *ctx ) ); 627} 628 629/* 630 * Send one message through a given message socket context. 631 * 632 * \retval \p len, if everything succeeds. 633 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context 634 * elements or the context itself is null. 635 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed. 636 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. 637 * 638 * This function will also return any error from 639 * mbedtls_test_message_queue_push_info. 640 */ 641int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len ) 642{ 643 mbedtls_test_message_queue* queue; 644 mbedtls_mock_socket* socket; 645 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx; 646 647 if( context == NULL || context->socket == NULL 648 || context->queue_output == NULL ) 649 { 650 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; 651 } 652 653 queue = context->queue_output; 654 socket = context->socket; 655 656 if( queue->num >= queue->capacity ) 657 return MBEDTLS_ERR_SSL_WANT_WRITE; 658 659 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len ) 660 return MBEDTLS_TEST_ERROR_SEND_FAILED; 661 662 return mbedtls_test_message_queue_push_info( queue, len ); 663} 664 665/* 666 * Receive one message from a given message socket context and return message 667 * length or an error. 668 * 669 * \retval message length, if everything succeeds. 670 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context 671 * elements or the context itself is null. 672 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed. 673 * 674 * This function will also return any error other than 675 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info. 676 */ 677int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len ) 678{ 679 mbedtls_test_message_queue* queue; 680 mbedtls_mock_socket* socket; 681 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx; 682 size_t drop_len = 0; 683 size_t msg_len; 684 int ret; 685 686 if( context == NULL || context->socket == NULL 687 || context->queue_input == NULL ) 688 { 689 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; 690 } 691 692 queue = context->queue_input; 693 socket = context->socket; 694 695 /* Peek first, so that in case of a socket error the data remains in 696 * the queue. */ 697 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len ); 698 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED ) 699 { 700 /* Calculate how much to drop */ 701 drop_len = msg_len - buf_len; 702 703 /* Set the requested message len to be buffer length */ 704 msg_len = buf_len; 705 } else if( ret != 0 ) 706 { 707 return ret; 708 } 709 710 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len ) 711 return MBEDTLS_TEST_ERROR_RECV_FAILED; 712 713 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED ) 714 { 715 /* Drop the remaining part of the message */ 716 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len ) 717 { 718 /* Inconsistent state - part of the message was read, 719 * and a part couldn't. Not much we can do here, but it should not 720 * happen in test environment, unless forced manually. */ 721 } 722 } 723 mbedtls_test_message_queue_pop_info( queue, buf_len ); 724 725 return msg_len; 726} 727 728#if defined(MBEDTLS_X509_CRT_PARSE_C) && \ 729 defined(MBEDTLS_ENTROPY_C) && \ 730 defined(MBEDTLS_CTR_DRBG_C) 731 732/* 733 * Structure with endpoint's certificates for SSL communication tests. 734 */ 735typedef struct mbedtls_endpoint_certificate 736{ 737 mbedtls_x509_crt ca_cert; 738 mbedtls_x509_crt cert; 739 mbedtls_pk_context pkey; 740} mbedtls_endpoint_certificate; 741 742/* 743 * Endpoint structure for SSL communication tests. 744 */ 745typedef struct mbedtls_endpoint 746{ 747 const char *name; 748 mbedtls_ssl_context ssl; 749 mbedtls_ssl_config conf; 750 mbedtls_ctr_drbg_context ctr_drbg; 751 mbedtls_entropy_context entropy; 752 mbedtls_mock_socket socket; 753 mbedtls_endpoint_certificate cert; 754} mbedtls_endpoint; 755 756/* 757 * Initializes \p ep_cert structure and assigns it to endpoint 758 * represented by \p ep. 759 * 760 * \retval 0 on success, otherwise error code. 761 */ 762int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg ) 763{ 764 int i = 0; 765 int ret = -1; 766 mbedtls_endpoint_certificate *cert; 767 768 if( ep == NULL ) 769 { 770 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 771 } 772 773 cert = &( ep->cert ); 774 mbedtls_x509_crt_init( &( cert->ca_cert ) ); 775 mbedtls_x509_crt_init( &( cert->cert ) ); 776 mbedtls_pk_init( &( cert->pkey ) ); 777 778 /* Load the trusted CA */ 779 780 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ ) 781 { 782 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ), 783 (const unsigned char *) mbedtls_test_cas_der[i], 784 mbedtls_test_cas_der_len[i] ); 785 TEST_ASSERT( ret == 0 ); 786 } 787 788 /* Load own certificate and private key */ 789 790 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER ) 791 { 792 if( pk_alg == MBEDTLS_PK_RSA ) 793 { 794 ret = mbedtls_x509_crt_parse( &( cert->cert ), 795 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der, 796 mbedtls_test_srv_crt_rsa_sha256_der_len ); 797 TEST_ASSERT( ret == 0 ); 798 799 ret = mbedtls_pk_parse_key( &( cert->pkey ), 800 (const unsigned char*) mbedtls_test_srv_key_rsa_der, 801 mbedtls_test_srv_key_rsa_der_len, NULL, 0 ); 802 TEST_ASSERT( ret == 0 ); 803 } 804 else 805 { 806 ret = mbedtls_x509_crt_parse( &( cert->cert ), 807 (const unsigned char*) mbedtls_test_srv_crt_ec_der, 808 mbedtls_test_srv_crt_ec_der_len ); 809 TEST_ASSERT( ret == 0 ); 810 811 ret = mbedtls_pk_parse_key( &( cert->pkey ), 812 (const unsigned char*) mbedtls_test_srv_key_ec_der, 813 mbedtls_test_srv_key_ec_der_len, NULL, 0 ); 814 TEST_ASSERT( ret == 0 ); 815 } 816 } 817 else 818 { 819 if( pk_alg == MBEDTLS_PK_RSA ) 820 { 821 ret = mbedtls_x509_crt_parse( &( cert->cert ), 822 (const unsigned char *) mbedtls_test_cli_crt_rsa_der, 823 mbedtls_test_cli_crt_rsa_der_len ); 824 TEST_ASSERT( ret == 0 ); 825 826 ret = mbedtls_pk_parse_key( &( cert->pkey ), 827 (const unsigned char *) mbedtls_test_cli_key_rsa_der, 828 mbedtls_test_cli_key_rsa_der_len, NULL, 0 ); 829 TEST_ASSERT( ret == 0 ); 830 } 831 else 832 { 833 ret = mbedtls_x509_crt_parse( &( cert->cert ), 834 (const unsigned char *) mbedtls_test_cli_crt_ec_der, 835 mbedtls_test_cli_crt_ec_len ); 836 TEST_ASSERT( ret == 0 ); 837 838 ret = mbedtls_pk_parse_key( &( cert->pkey ), 839 (const unsigned char *) mbedtls_test_cli_key_ec_der, 840 mbedtls_test_cli_key_ec_der_len, NULL, 0 ); 841 TEST_ASSERT( ret == 0 ); 842 } 843 } 844 845 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL ); 846 847 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ), 848 &( cert->pkey ) ); 849 TEST_ASSERT( ret == 0 ); 850 851exit: 852 if( ret != 0 ) 853 { 854 mbedtls_x509_crt_free( &( cert->ca_cert ) ); 855 mbedtls_x509_crt_free( &( cert->cert ) ); 856 mbedtls_pk_free( &( cert->pkey ) ); 857 } 858 859 return ret; 860} 861 862/* 863 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()` 864 * after calling this function even if it fails. 865 * 866 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or 867 * MBEDTLS_SSL_IS_CLIENT. 868 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and 869 * MBEDTLS_PK_ECDSA are supported. 870 * \p dtls_context - in case of DTLS - this is the context handling metadata. 871 * \p input_queue - used only in case of DTLS. 872 * \p output_queue - used only in case of DTLS. 873 * 874 * \retval 0 on success, otherwise error code. 875 */ 876int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg, 877 mbedtls_test_message_socket_context *dtls_context, 878 mbedtls_test_message_queue *input_queue, 879 mbedtls_test_message_queue *output_queue ) 880{ 881 int ret = -1; 882 883 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) ) 884 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 885 886 if( ep == NULL ) 887 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 888 889 memset( ep, 0, sizeof( *ep ) ); 890 891 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client"; 892 893 mbedtls_ssl_init( &( ep->ssl ) ); 894 mbedtls_ssl_config_init( &( ep->conf ) ); 895 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) ); 896 mbedtls_ssl_conf_rng( &( ep->conf ), 897 mbedtls_ctr_drbg_random, 898 &( ep->ctr_drbg ) ); 899 mbedtls_entropy_init( &( ep->entropy ) ); 900 if( dtls_context != NULL ) 901 { 902 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue, 903 100, &( ep->socket ), 904 dtls_context ) == 0 ); 905 } 906 else 907 { 908 mbedtls_mock_socket_init( &( ep->socket ) ); 909 } 910 911 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func, 912 &( ep->entropy ), (const unsigned char *) ( ep->name ), 913 strlen( ep->name ) ); 914 TEST_ASSERT( ret == 0 ); 915 916 /* Non-blocking callbacks without timeout */ 917 if( dtls_context != NULL ) 918 { 919 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context, 920 mbedtls_mock_tcp_send_msg, 921 mbedtls_mock_tcp_recv_msg, 922 NULL ); 923 } 924 else 925 { 926 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ), 927 mbedtls_mock_tcp_send_nb, 928 mbedtls_mock_tcp_recv_nb, 929 NULL ); 930 } 931 932 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type, 933 ( dtls_context != NULL ) ? 934 MBEDTLS_SSL_TRANSPORT_DATAGRAM : 935 MBEDTLS_SSL_TRANSPORT_STREAM, 936 MBEDTLS_SSL_PRESET_DEFAULT ); 937 TEST_ASSERT( ret == 0 ); 938 939 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) ); 940 TEST_ASSERT( ret == 0 ); 941 942#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) 943 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL ) 944 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL ); 945#endif 946 947 ret = mbedtls_endpoint_certificate_init( ep, pk_alg ); 948 TEST_ASSERT( ret == 0 ); 949 950exit: 951 return ret; 952} 953 954/* 955 * Deinitializes certificates from endpoint represented by \p ep. 956 */ 957void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep ) 958{ 959 mbedtls_endpoint_certificate *cert = &( ep->cert ); 960 mbedtls_x509_crt_free( &( cert->ca_cert ) ); 961 mbedtls_x509_crt_free( &( cert->cert ) ); 962 mbedtls_pk_free( &( cert->pkey ) ); 963} 964 965/* 966 * Deinitializes endpoint represented by \p ep. 967 */ 968void mbedtls_endpoint_free( mbedtls_endpoint *ep, 969 mbedtls_test_message_socket_context *context ) 970{ 971 mbedtls_endpoint_certificate_free( ep ); 972 973 mbedtls_ssl_free( &( ep->ssl ) ); 974 mbedtls_ssl_config_free( &( ep->conf ) ); 975 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) ); 976 mbedtls_entropy_free( &( ep->entropy ) ); 977 978 if( context != NULL ) 979 { 980 mbedtls_message_socket_close( context ); 981 } 982 else 983 { 984 mbedtls_mock_socket_close( &( ep->socket ) ); 985 } 986} 987 988/* 989 * This function moves ssl handshake from \p ssl to prescribed \p state. 990 * /p second_ssl is used as second endpoint and their sockets have to be 991 * connected before calling this function. 992 * 993 * \retval 0 on success, otherwise error code. 994 */ 995int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl, 996 mbedtls_ssl_context *second_ssl, 997 int state ) 998{ 999 enum { BUFFSIZE = 1024 }; 1000 int max_steps = 1000; 1001 int ret = 0; 1002 1003 if( ssl == NULL || second_ssl == NULL ) 1004 { 1005 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 1006 } 1007 1008 /* Perform communication via connected sockets */ 1009 while( ( ssl->state != state ) && ( --max_steps >= 0 ) ) 1010 { 1011 /* If /p second_ssl ends the handshake procedure before /p ssl then 1012 * there is no need to call the next step */ 1013 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) 1014 { 1015 ret = mbedtls_ssl_handshake_step( second_ssl ); 1016 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && 1017 ret != MBEDTLS_ERR_SSL_WANT_WRITE ) 1018 { 1019 return ret; 1020 } 1021 } 1022 1023 /* We only care about the \p ssl state and returns, so we call it last, 1024 * to leave the iteration as soon as the state is as expected. */ 1025 ret = mbedtls_ssl_handshake_step( ssl ); 1026 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && 1027 ret != MBEDTLS_ERR_SSL_WANT_WRITE ) 1028 { 1029 return ret; 1030 } 1031 } 1032 1033 return ( max_steps >= 0 ) ? ret : -1; 1034} 1035 1036#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ 1037 1038/* 1039 * Write application data. Increase write counter if necessary. 1040 */ 1041int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf, 1042 int buf_len, int *written, 1043 const int expected_fragments ) 1044{ 1045 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written ); 1046 if( ret > 0 ) 1047 { 1048 *written += ret; 1049 } 1050 1051 if( expected_fragments == 0 ) 1052 { 1053 /* Used for DTLS and the message size larger than MFL. In that case 1054 * the message can not be fragmented and the library should return 1055 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned 1056 * to prevent a dead loop inside mbedtls_exchange_data(). */ 1057 return ret; 1058 } 1059 else if( expected_fragments == 1 ) 1060 { 1061 /* Used for TLS/DTLS and the message size lower than MFL */ 1062 TEST_ASSERT( ret == buf_len || 1063 ret == MBEDTLS_ERR_SSL_WANT_READ || 1064 ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 1065 } 1066 else 1067 { 1068 /* Used for TLS and the message size larger than MFL */ 1069 TEST_ASSERT( expected_fragments > 1 ); 1070 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) || 1071 ret == MBEDTLS_ERR_SSL_WANT_READ || 1072 ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 1073 } 1074 1075 return 0; 1076 1077exit: 1078 /* Some of the tests failed */ 1079 return -1; 1080} 1081 1082/* 1083 * Read application data and increase read counter and fragments counter if necessary. 1084 */ 1085int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf, 1086 int buf_len, int *read, 1087 int *fragments, const int expected_fragments ) 1088{ 1089 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read ); 1090 if( ret > 0 ) 1091 { 1092 ( *fragments )++; 1093 *read += ret; 1094 } 1095 1096 if( expected_fragments == 0 ) 1097 { 1098 TEST_ASSERT( ret == 0 ); 1099 } 1100 else if( expected_fragments == 1 ) 1101 { 1102 TEST_ASSERT( ret == buf_len || 1103 ret == MBEDTLS_ERR_SSL_WANT_READ || 1104 ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 1105 } 1106 else 1107 { 1108 TEST_ASSERT( expected_fragments > 1 ); 1109 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) || 1110 ret == MBEDTLS_ERR_SSL_WANT_READ || 1111 ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 1112 } 1113 1114 return 0; 1115 1116exit: 1117 /* Some of the tests failed */ 1118 return -1; 1119} 1120 1121/* 1122 * Helper function setting up inverse record transformations 1123 * using given cipher, hash, EtM mode, authentication tag length, 1124 * and version. 1125 */ 1126 1127#define CHK( x ) \ 1128 do \ 1129 { \ 1130 if( !( x ) ) \ 1131 { \ 1132 ret = -1; \ 1133 goto cleanup; \ 1134 } \ 1135 } while( 0 ) 1136 1137void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher, 1138 int* forced_ciphersuite ) 1139{ 1140 const mbedtls_ssl_ciphersuite_t *ciphersuite_info; 1141 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher ); 1142 forced_ciphersuite[1] = 0; 1143 1144 ciphersuite_info = 1145 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] ); 1146 1147 TEST_ASSERT( ciphersuite_info != NULL ); 1148 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver ); 1149 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver ); 1150 1151 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver ) 1152 { 1153 conf->max_minor_ver = ciphersuite_info->max_minor_ver; 1154 } 1155 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver ) 1156 { 1157 conf->min_minor_ver = ciphersuite_info->min_minor_ver; 1158 } 1159 1160 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite ); 1161 1162exit: 1163 return; 1164} 1165 1166int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl, 1167 const unsigned char *name, size_t name_len ) 1168{ 1169 (void) p_info; 1170 (void) ssl; 1171 (void) name; 1172 (void) name_len; 1173 1174 return ( 0 ); 1175} 1176 1177#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX 1178#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX 1179#else 1180#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX 1181#endif 1182 1183static int build_transforms( mbedtls_ssl_transform *t_in, 1184 mbedtls_ssl_transform *t_out, 1185 int cipher_type, int hash_id, 1186 int etm, int tag_mode, int ver, 1187 size_t cid0_len, 1188 size_t cid1_len ) 1189{ 1190 mbedtls_cipher_info_t const *cipher_info; 1191 int ret = 0; 1192 1193 size_t keylen, maclen, ivlen; 1194 unsigned char *key0 = NULL, *key1 = NULL; 1195 unsigned char *md0 = NULL, *md1 = NULL; 1196 unsigned char iv_enc[16], iv_dec[16]; 1197 1198#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 1199 unsigned char cid0[ SSL_CID_LEN_MIN ]; 1200 unsigned char cid1[ SSL_CID_LEN_MIN ]; 1201 1202 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) ); 1203 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) ); 1204#else 1205 ((void) cid0_len); 1206 ((void) cid1_len); 1207#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 1208 1209 maclen = 0; 1210 1211 /* Pick cipher */ 1212 cipher_info = mbedtls_cipher_info_from_type( cipher_type ); 1213 CHK( cipher_info != NULL ); 1214 CHK( cipher_info->iv_size <= 16 ); 1215 CHK( cipher_info->key_bitlen % 8 == 0 ); 1216 1217 /* Pick keys */ 1218 keylen = cipher_info->key_bitlen / 8; 1219 /* Allocate `keylen + 1` bytes to ensure that we get 1220 * a non-NULL pointers from `mbedtls_calloc` even if 1221 * `keylen == 0` in the case of the NULL cipher. */ 1222 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); 1223 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); 1224 memset( key0, 0x1, keylen ); 1225 memset( key1, 0x2, keylen ); 1226 1227 /* Setup cipher contexts */ 1228 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 ); 1229 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 ); 1230 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 ); 1231 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 ); 1232 1233#if defined(MBEDTLS_CIPHER_MODE_CBC) 1234 if( cipher_info->mode == MBEDTLS_MODE_CBC ) 1235 { 1236 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc, 1237 MBEDTLS_PADDING_NONE ) == 0 ); 1238 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec, 1239 MBEDTLS_PADDING_NONE ) == 0 ); 1240 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc, 1241 MBEDTLS_PADDING_NONE ) == 0 ); 1242 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec, 1243 MBEDTLS_PADDING_NONE ) == 0 ); 1244 } 1245#endif /* MBEDTLS_CIPHER_MODE_CBC */ 1246 1247 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0, 1248 keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); 1249 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1, 1250 keylen << 3, MBEDTLS_DECRYPT ) == 0 ); 1251 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1, 1252 keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); 1253 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0, 1254 keylen << 3, MBEDTLS_DECRYPT ) == 0 ); 1255 1256 /* Setup MAC contexts */ 1257#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) 1258 if( cipher_info->mode == MBEDTLS_MODE_CBC || 1259 cipher_info->mode == MBEDTLS_MODE_STREAM ) 1260 { 1261 mbedtls_md_info_t const *md_info; 1262 1263 /* Pick hash */ 1264 md_info = mbedtls_md_info_from_type( hash_id ); 1265 CHK( md_info != NULL ); 1266 1267 /* Pick hash keys */ 1268 maclen = mbedtls_md_get_size( md_info ); 1269 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL ); 1270 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL ); 1271 memset( md0, 0x5, maclen ); 1272 memset( md1, 0x6, maclen ); 1273 1274 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 ); 1275 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 ); 1276 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 ); 1277 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 ); 1278 1279 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 ) 1280 { 1281 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc, 1282 md0, maclen ) == 0 ); 1283 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec, 1284 md1, maclen ) == 0 ); 1285 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc, 1286 md1, maclen ) == 0 ); 1287 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec, 1288 md0, maclen ) == 0 ); 1289 } 1290#if defined(MBEDTLS_SSL_PROTO_SSL3) 1291 else 1292 { 1293 memcpy( &t_in->mac_enc, md0, maclen ); 1294 memcpy( &t_in->mac_dec, md1, maclen ); 1295 memcpy( &t_out->mac_enc, md1, maclen ); 1296 memcpy( &t_out->mac_dec, md0, maclen ); 1297 } 1298#endif 1299 } 1300#else 1301 ((void) hash_id); 1302#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ 1303 1304 1305 /* Pick IV's (regardless of whether they 1306 * are being used by the transform). */ 1307 ivlen = cipher_info->iv_size; 1308 memset( iv_enc, 0x3, sizeof( iv_enc ) ); 1309 memset( iv_dec, 0x4, sizeof( iv_dec ) ); 1310 1311 /* 1312 * Setup transforms 1313 */ 1314 1315#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ 1316 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) 1317 t_out->encrypt_then_mac = etm; 1318 t_in->encrypt_then_mac = etm; 1319#else 1320 ((void) etm); 1321#endif 1322 1323 t_out->minor_ver = ver; 1324 t_in->minor_ver = ver; 1325 t_out->ivlen = ivlen; 1326 t_in->ivlen = ivlen; 1327 1328 switch( cipher_info->mode ) 1329 { 1330 case MBEDTLS_MODE_GCM: 1331 case MBEDTLS_MODE_CCM: 1332#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) 1333 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 ) 1334 { 1335 t_out->fixed_ivlen = 12; 1336 t_in->fixed_ivlen = 12; 1337 } 1338 else 1339#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 1340 { 1341 t_out->fixed_ivlen = 4; 1342 t_in->fixed_ivlen = 4; 1343 } 1344 t_out->maclen = 0; 1345 t_in->maclen = 0; 1346 switch( tag_mode ) 1347 { 1348 case 0: /* Full tag */ 1349 t_out->taglen = 16; 1350 t_in->taglen = 16; 1351 break; 1352 case 1: /* Partial tag */ 1353 t_out->taglen = 8; 1354 t_in->taglen = 8; 1355 break; 1356 default: 1357 ret = 1; 1358 goto cleanup; 1359 } 1360 break; 1361 1362 case MBEDTLS_MODE_CHACHAPOLY: 1363 t_out->fixed_ivlen = 12; 1364 t_in->fixed_ivlen = 12; 1365 t_out->maclen = 0; 1366 t_in->maclen = 0; 1367 switch( tag_mode ) 1368 { 1369 case 0: /* Full tag */ 1370 t_out->taglen = 16; 1371 t_in->taglen = 16; 1372 break; 1373 case 1: /* Partial tag */ 1374 t_out->taglen = 8; 1375 t_in->taglen = 8; 1376 break; 1377 default: 1378 ret = 1; 1379 goto cleanup; 1380 } 1381 break; 1382 1383 case MBEDTLS_MODE_STREAM: 1384 case MBEDTLS_MODE_CBC: 1385 t_out->fixed_ivlen = 0; /* redundant, must be 0 */ 1386 t_in->fixed_ivlen = 0; /* redundant, must be 0 */ 1387 t_out->taglen = 0; 1388 t_in->taglen = 0; 1389 switch( tag_mode ) 1390 { 1391 case 0: /* Full tag */ 1392 t_out->maclen = maclen; 1393 t_in->maclen = maclen; 1394 break; 1395 case 1: /* Partial tag */ 1396 t_out->maclen = 10; 1397 t_in->maclen = 10; 1398 break; 1399 default: 1400 ret = 1; 1401 goto cleanup; 1402 } 1403 break; 1404 default: 1405 ret = 1; 1406 goto cleanup; 1407 break; 1408 } 1409 1410 /* Setup IV's */ 1411 1412 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) ); 1413 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) ); 1414 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) ); 1415 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) ); 1416 1417#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 1418 /* Add CID */ 1419 memcpy( &t_in->in_cid, cid0, cid0_len ); 1420 memcpy( &t_in->out_cid, cid1, cid1_len ); 1421 t_in->in_cid_len = cid0_len; 1422 t_in->out_cid_len = cid1_len; 1423 memcpy( &t_out->in_cid, cid1, cid1_len ); 1424 memcpy( &t_out->out_cid, cid0, cid0_len ); 1425 t_out->in_cid_len = cid1_len; 1426 t_out->out_cid_len = cid0_len; 1427#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 1428 1429cleanup: 1430 1431 mbedtls_free( key0 ); 1432 mbedtls_free( key1 ); 1433 1434 mbedtls_free( md0 ); 1435 mbedtls_free( md1 ); 1436 1437 return( ret ); 1438} 1439 1440/* 1441 * Populate a session structure for serialization tests. 1442 * Choose dummy values, mostly non-0 to distinguish from the init default. 1443 */ 1444static int ssl_populate_session( mbedtls_ssl_session *session, 1445 int ticket_len, 1446 const char *crt_file ) 1447{ 1448#if defined(MBEDTLS_HAVE_TIME) 1449 session->start = mbedtls_time( NULL ) - 42; 1450#endif 1451 session->ciphersuite = 0xabcd; 1452 session->compression = 1; 1453 session->id_len = sizeof( session->id ); 1454 memset( session->id, 66, session->id_len ); 1455 memset( session->master, 17, sizeof( session->master ) ); 1456 1457#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO) 1458 if( strlen( crt_file ) != 0 ) 1459 { 1460 mbedtls_x509_crt tmp_crt; 1461 int ret; 1462 1463 mbedtls_x509_crt_init( &tmp_crt ); 1464 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file ); 1465 if( ret != 0 ) 1466 return( ret ); 1467 1468#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) 1469 /* Move temporary CRT. */ 1470 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) ); 1471 if( session->peer_cert == NULL ) 1472 return( -1 ); 1473 *session->peer_cert = tmp_crt; 1474 memset( &tmp_crt, 0, sizeof( tmp_crt ) ); 1475#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ 1476 /* Calculate digest of temporary CRT. */ 1477 session->peer_cert_digest = 1478 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); 1479 if( session->peer_cert_digest == NULL ) 1480 return( -1 ); 1481 ret = mbedtls_md( mbedtls_md_info_from_type( 1482 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), 1483 tmp_crt.raw.p, tmp_crt.raw.len, 1484 session->peer_cert_digest ); 1485 if( ret != 0 ) 1486 return( ret ); 1487 session->peer_cert_digest_type = 1488 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; 1489 session->peer_cert_digest_len = 1490 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; 1491#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ 1492 1493 mbedtls_x509_crt_free( &tmp_crt ); 1494 } 1495#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ 1496 (void) crt_file; 1497#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ 1498 session->verify_result = 0xdeadbeef; 1499 1500#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) 1501 if( ticket_len != 0 ) 1502 { 1503 session->ticket = mbedtls_calloc( 1, ticket_len ); 1504 if( session->ticket == NULL ) 1505 return( -1 ); 1506 memset( session->ticket, 33, ticket_len ); 1507 } 1508 session->ticket_len = ticket_len; 1509 session->ticket_lifetime = 86401; 1510#else 1511 (void) ticket_len; 1512#endif 1513 1514#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 1515 session->mfl_code = 1; 1516#endif 1517#if defined(MBEDTLS_SSL_TRUNCATED_HMAC) 1518 session->trunc_hmac = 1; 1519#endif 1520#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) 1521 session->encrypt_then_mac = 1; 1522#endif 1523 1524 return( 0 ); 1525} 1526 1527/* 1528 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the 1529 * message was sent in the correct number of fragments. 1530 * 1531 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both 1532 * of them must be initialized and connected beforehand. 1533 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. 1534 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many 1535 * fragments the message should be sent. 1536 * expected_fragments is 0: can be used for DTLS testing while the message 1537 * size is larger than MFL. In that case the message 1538 * cannot be fragmented and sent to the second endpoint. 1539 * This value can be used for negative tests. 1540 * expected_fragments is 1: can be used for TLS/DTLS testing while the 1541 * message size is below MFL 1542 * expected_fragments > 1: can be used for TLS testing while the message 1543 * size is larger than MFL 1544 * 1545 * \retval 0 on success, otherwise error code. 1546 */ 1547int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1, 1548 int msg_len_1, const int expected_fragments_1, 1549 mbedtls_ssl_context *ssl_2, 1550 int msg_len_2, const int expected_fragments_2 ) 1551{ 1552 unsigned char *msg_buf_1 = malloc( msg_len_1 ); 1553 unsigned char *msg_buf_2 = malloc( msg_len_2 ); 1554 unsigned char *in_buf_1 = malloc( msg_len_2 ); 1555 unsigned char *in_buf_2 = malloc( msg_len_1 ); 1556 int msg_type, ret = -1; 1557 1558 /* Perform this test with two message types. At first use a message 1559 * consisting of only 0x00 for the client and only 0xFF for the server. 1560 * At the second time use message with generated data */ 1561 for( msg_type = 0; msg_type < 2; msg_type++ ) 1562 { 1563 int written_1 = 0; 1564 int written_2 = 0; 1565 int read_1 = 0; 1566 int read_2 = 0; 1567 int fragments_1 = 0; 1568 int fragments_2 = 0; 1569 1570 if( msg_type == 0 ) 1571 { 1572 memset( msg_buf_1, 0x00, msg_len_1 ); 1573 memset( msg_buf_2, 0xff, msg_len_2 ); 1574 } 1575 else 1576 { 1577 int i, j = 0; 1578 for( i = 0; i < msg_len_1; i++ ) 1579 { 1580 msg_buf_1[i] = j++ & 0xFF; 1581 } 1582 for( i = 0; i < msg_len_2; i++ ) 1583 { 1584 msg_buf_2[i] = ( j -= 5 ) & 0xFF; 1585 } 1586 } 1587 1588 while( read_1 < msg_len_2 || read_2 < msg_len_1 ) 1589 { 1590 /* ssl_1 sending */ 1591 if( msg_len_1 > written_1 ) 1592 { 1593 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1, 1594 msg_len_1, &written_1, 1595 expected_fragments_1 ); 1596 if( expected_fragments_1 == 0 ) 1597 { 1598 /* This error is expected when the message is too large and 1599 * cannot be fragmented */ 1600 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 1601 msg_len_1 = 0; 1602 } 1603 else 1604 { 1605 TEST_ASSERT( ret == 0 ); 1606 } 1607 } 1608 1609 /* ssl_2 sending */ 1610 if( msg_len_2 > written_2 ) 1611 { 1612 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2, 1613 msg_len_2, &written_2, 1614 expected_fragments_2 ); 1615 if( expected_fragments_2 == 0 ) 1616 { 1617 /* This error is expected when the message is too large and 1618 * cannot be fragmented */ 1619 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 1620 msg_len_2 = 0; 1621 } 1622 else 1623 { 1624 TEST_ASSERT( ret == 0 ); 1625 } 1626 } 1627 1628 /* ssl_1 reading */ 1629 if( read_1 < msg_len_2 ) 1630 { 1631 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1, 1632 msg_len_2, &read_1, 1633 &fragments_2, 1634 expected_fragments_2 ); 1635 TEST_ASSERT( ret == 0 ); 1636 } 1637 1638 /* ssl_2 reading */ 1639 if( read_2 < msg_len_1 ) 1640 { 1641 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2, 1642 msg_len_1, &read_2, 1643 &fragments_1, 1644 expected_fragments_1 ); 1645 TEST_ASSERT( ret == 0 ); 1646 } 1647 } 1648 1649 ret = -1; 1650 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) ); 1651 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) ); 1652 TEST_ASSERT( fragments_1 == expected_fragments_1 ); 1653 TEST_ASSERT( fragments_2 == expected_fragments_2 ); 1654 } 1655 1656 ret = 0; 1657 1658exit: 1659 free( msg_buf_1 ); 1660 free( in_buf_1 ); 1661 free( msg_buf_2 ); 1662 free( in_buf_2 ); 1663 1664 return ret; 1665} 1666 1667/* 1668 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints 1669 * must be initialized and connected beforehand. 1670 * 1671 * \retval 0 on success, otherwise error code. 1672 */ 1673int exchange_data( mbedtls_ssl_context *ssl_1, 1674 mbedtls_ssl_context *ssl_2 ) 1675{ 1676 return mbedtls_exchange_data( ssl_1, 256, 1, 1677 ssl_2, 256, 1 ); 1678} 1679 1680#if defined(MBEDTLS_X509_CRT_PARSE_C) && \ 1681 defined(MBEDTLS_ENTROPY_C) && \ 1682 defined(MBEDTLS_CTR_DRBG_C) 1683void perform_handshake( handshake_test_options* options ) 1684{ 1685 /* forced_ciphersuite needs to last until the end of the handshake */ 1686 int forced_ciphersuite[2]; 1687 enum { BUFFSIZE = 17000 }; 1688 mbedtls_endpoint client, server; 1689#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) 1690 const char *psk_identity = "foo"; 1691#endif 1692#if defined(MBEDTLS_TIMING_C) 1693 mbedtls_timing_delay_context timer_client, timer_server; 1694#endif 1695#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) 1696 unsigned char *context_buf = NULL; 1697 size_t context_buf_len; 1698#endif 1699#if defined(MBEDTLS_SSL_RENEGOTIATION) 1700 int ret = -1; 1701#endif 1702 int expected_handshake_result = 0; 1703 1704 mbedtls_test_message_queue server_queue, client_queue; 1705 mbedtls_test_message_socket_context server_context, client_context; 1706 mbedtls_message_socket_init( &server_context ); 1707 mbedtls_message_socket_init( &client_context ); 1708 1709 /* Client side */ 1710 if( options->dtls != 0 ) 1711 { 1712 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, 1713 options->pk_alg, &client_context, 1714 &client_queue, 1715 &server_queue ) == 0 ); 1716#if defined(MBEDTLS_TIMING_C) 1717 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client, 1718 mbedtls_timing_set_delay, 1719 mbedtls_timing_get_delay ); 1720#endif 1721 } 1722 else 1723 { 1724 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, 1725 options->pk_alg, NULL, NULL, 1726 NULL ) == 0 ); 1727 } 1728 1729 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE ) 1730 { 1731 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, 1732 options->client_min_version ); 1733 } 1734 1735 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE ) 1736 { 1737 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, 1738 options->client_max_version ); 1739 } 1740 1741 if( strlen( options->cipher ) > 0 ) 1742 { 1743 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite ); 1744 } 1745 1746#if defined (MBEDTLS_DEBUG_C) 1747 if( options->cli_log_fun ) 1748 { 1749 mbedtls_debug_set_threshold( 4 ); 1750 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun, 1751 options->cli_log_obj ); 1752 } 1753#endif 1754 1755 /* Server side */ 1756 if( options->dtls != 0 ) 1757 { 1758 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, 1759 options->pk_alg, &server_context, 1760 &server_queue, 1761 &client_queue) == 0 ); 1762#if defined(MBEDTLS_TIMING_C) 1763 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, 1764 mbedtls_timing_set_delay, 1765 mbedtls_timing_get_delay ); 1766#endif 1767 } 1768 else 1769 { 1770 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, 1771 options->pk_alg, NULL, NULL, NULL ) == 0 ); 1772 } 1773 1774 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode ); 1775 1776 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE ) 1777 { 1778 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, 1779 options->server_min_version ); 1780 } 1781 1782 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE ) 1783 { 1784 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, 1785 options->server_max_version ); 1786 } 1787 1788#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 1789 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf), 1790 (unsigned char) options->mfl ) == 0 ); 1791 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf), 1792 (unsigned char) options->mfl ) == 0 ); 1793#else 1794 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl ); 1795#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ 1796 1797#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) 1798 if( options->psk_str != NULL && options->psk_str->len > 0 ) 1799 { 1800 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x, 1801 options->psk_str->len, 1802 (const unsigned char *) psk_identity, 1803 strlen( psk_identity ) ) == 0 ); 1804 1805 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x, 1806 options->psk_str->len, 1807 (const unsigned char *) psk_identity, 1808 strlen( psk_identity ) ) == 0 ); 1809 1810 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL ); 1811 } 1812#endif 1813#if defined(MBEDTLS_SSL_RENEGOTIATION) 1814 if( options->renegotiate ) 1815 { 1816 mbedtls_ssl_conf_renegotiation( &(server.conf), 1817 MBEDTLS_SSL_RENEGOTIATION_ENABLED ); 1818 mbedtls_ssl_conf_renegotiation( &(client.conf), 1819 MBEDTLS_SSL_RENEGOTIATION_ENABLED ); 1820 1821 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf), 1822 options->legacy_renegotiation ); 1823 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf), 1824 options->legacy_renegotiation ); 1825 } 1826#endif /* MBEDTLS_SSL_RENEGOTIATION */ 1827 1828#if defined (MBEDTLS_DEBUG_C) 1829 if( options->srv_log_fun ) 1830 { 1831 mbedtls_debug_set_threshold( 4 ); 1832 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun, 1833 options->srv_log_obj ); 1834 } 1835#endif 1836 1837 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), 1838 &(server.socket), 1839 BUFFSIZE ) == 0 ); 1840 1841#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) 1842 if( options->resize_buffers != 0 ) 1843 { 1844 /* Ensure that the buffer sizes are appropriate before resizes */ 1845 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); 1846 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); 1847 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); 1848 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); 1849 } 1850#endif 1851 1852 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE ) 1853 { 1854 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION; 1855 } 1856 1857 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), 1858 &(server.ssl), 1859 MBEDTLS_SSL_HANDSHAKE_OVER ) 1860 == expected_handshake_result ); 1861 1862 if( expected_handshake_result != 0 ) 1863 { 1864 /* Connection will have failed by this point, skip to cleanup */ 1865 goto exit; 1866 } 1867 1868 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); 1869 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); 1870 1871 /* Check that we agree on the version... */ 1872 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver ); 1873 1874 /* And check that the version negotiated is the expected one. */ 1875 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version ); 1876 1877#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) 1878 if( options->resize_buffers != 0 ) 1879 { 1880 if( options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 && 1881 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1 ) 1882 { 1883 /* A server, when using DTLS, might delay a buffer resize to happen 1884 * after it receives a message, so we force it. */ 1885 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); 1886 1887 TEST_ASSERT( client.ssl.out_buf_len == 1888 mbedtls_ssl_get_output_buflen( &client.ssl ) ); 1889 TEST_ASSERT( client.ssl.in_buf_len == 1890 mbedtls_ssl_get_input_buflen( &client.ssl ) ); 1891 TEST_ASSERT( server.ssl.out_buf_len == 1892 mbedtls_ssl_get_output_buflen( &server.ssl ) ); 1893 TEST_ASSERT( server.ssl.in_buf_len == 1894 mbedtls_ssl_get_input_buflen( &server.ssl ) ); 1895 } 1896 } 1897#endif 1898 1899 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) 1900 { 1901 /* Start data exchanging test */ 1902 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len, 1903 options->expected_cli_fragments, 1904 &(server.ssl), options->srv_msg_len, 1905 options->expected_srv_fragments ) 1906 == 0 ); 1907 } 1908#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) 1909 if( options->serialize == 1 ) 1910 { 1911 TEST_ASSERT( options->dtls == 1 ); 1912 1913 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL, 1914 0, &context_buf_len ) 1915 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 1916 1917 context_buf = mbedtls_calloc( 1, context_buf_len ); 1918 TEST_ASSERT( context_buf != NULL ); 1919 1920 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf, 1921 context_buf_len, 1922 &context_buf_len ) == 0 ); 1923 1924 mbedtls_ssl_free( &(server.ssl) ); 1925 mbedtls_ssl_init( &(server.ssl) ); 1926 1927 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 ); 1928 1929 mbedtls_ssl_set_bio( &( server.ssl ), &server_context, 1930 mbedtls_mock_tcp_send_msg, 1931 mbedtls_mock_tcp_recv_msg, 1932 NULL ); 1933 1934#if defined(MBEDTLS_TIMING_C) 1935 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, 1936 mbedtls_timing_set_delay, 1937 mbedtls_timing_get_delay ); 1938#endif 1939#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) 1940 if( options->resize_buffers != 0 ) 1941 { 1942 /* Ensure that the buffer sizes are appropriate before resizes */ 1943 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); 1944 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); 1945 } 1946#endif 1947 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf, 1948 context_buf_len ) == 0 ); 1949 1950#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) 1951 /* Validate buffer sizes after context deserialization */ 1952 if( options->resize_buffers != 0 ) 1953 { 1954 TEST_ASSERT( server.ssl.out_buf_len == 1955 mbedtls_ssl_get_output_buflen( &server.ssl ) ); 1956 TEST_ASSERT( server.ssl.in_buf_len == 1957 mbedtls_ssl_get_input_buflen( &server.ssl ) ); 1958 } 1959#endif 1960 /* Retest writing/reading */ 1961 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) 1962 { 1963 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), 1964 options->cli_msg_len, 1965 options->expected_cli_fragments, 1966 &(server.ssl), 1967 options->srv_msg_len, 1968 options->expected_srv_fragments ) 1969 == 0 ); 1970 } 1971 } 1972#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ 1973 1974#if defined(MBEDTLS_SSL_RENEGOTIATION) 1975 if( options->renegotiate ) 1976 { 1977 /* Start test with renegotiation */ 1978 TEST_ASSERT( server.ssl.renego_status == 1979 MBEDTLS_SSL_INITIAL_HANDSHAKE ); 1980 TEST_ASSERT( client.ssl.renego_status == 1981 MBEDTLS_SSL_INITIAL_HANDSHAKE ); 1982 1983 /* After calling this function for the server, it only sends a handshake 1984 * request. All renegotiation should happen during data exchanging */ 1985 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 ); 1986 TEST_ASSERT( server.ssl.renego_status == 1987 MBEDTLS_SSL_RENEGOTIATION_PENDING ); 1988 TEST_ASSERT( client.ssl.renego_status == 1989 MBEDTLS_SSL_INITIAL_HANDSHAKE ); 1990 1991 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); 1992 TEST_ASSERT( server.ssl.renego_status == 1993 MBEDTLS_SSL_RENEGOTIATION_DONE ); 1994 TEST_ASSERT( client.ssl.renego_status == 1995 MBEDTLS_SSL_RENEGOTIATION_DONE ); 1996 1997 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation 1998 * should happen inside this function. However in this test, we cannot 1999 * perform simultaneous communication betwen client and server so this 2000 * function will return waiting error on the socket. All rest of 2001 * renegotiation should happen during data exchanging */ 2002 ret = mbedtls_ssl_renegotiate( &(client.ssl) ); 2003#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) 2004 if( options->resize_buffers != 0 ) 2005 { 2006 /* Ensure that the buffer sizes are appropriate before resizes */ 2007 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); 2008 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); 2009 } 2010#endif 2011 TEST_ASSERT( ret == 0 || 2012 ret == MBEDTLS_ERR_SSL_WANT_READ || 2013 ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 2014 TEST_ASSERT( server.ssl.renego_status == 2015 MBEDTLS_SSL_RENEGOTIATION_DONE ); 2016 TEST_ASSERT( client.ssl.renego_status == 2017 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ); 2018 2019 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); 2020 TEST_ASSERT( server.ssl.renego_status == 2021 MBEDTLS_SSL_RENEGOTIATION_DONE ); 2022 TEST_ASSERT( client.ssl.renego_status == 2023 MBEDTLS_SSL_RENEGOTIATION_DONE ); 2024#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) 2025 /* Validate buffer sizes after renegotiation */ 2026 if( options->resize_buffers != 0 ) 2027 { 2028 TEST_ASSERT( client.ssl.out_buf_len == 2029 mbedtls_ssl_get_output_buflen( &client.ssl ) ); 2030 TEST_ASSERT( client.ssl.in_buf_len == 2031 mbedtls_ssl_get_input_buflen( &client.ssl ) ); 2032 TEST_ASSERT( server.ssl.out_buf_len == 2033 mbedtls_ssl_get_output_buflen( &server.ssl ) ); 2034 TEST_ASSERT( server.ssl.in_buf_len == 2035 mbedtls_ssl_get_input_buflen( &server.ssl ) ); 2036 } 2037#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ 2038 } 2039#endif /* MBEDTLS_SSL_RENEGOTIATION */ 2040 2041exit: 2042 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL ); 2043 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL ); 2044#if defined (MBEDTLS_DEBUG_C) 2045 if( options->cli_log_fun || options->srv_log_fun ) 2046 { 2047 mbedtls_debug_set_threshold( 0 ); 2048 } 2049#endif 2050#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) 2051 if( context_buf != NULL ) 2052 mbedtls_free( context_buf ); 2053#endif 2054} 2055#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ 2056 2057/* END_HEADER */ 2058 2059/* BEGIN_DEPENDENCIES 2060 * depends_on:MBEDTLS_SSL_TLS_C 2061 * END_DEPENDENCIES 2062 */ 2063 2064/* BEGIN_CASE */ 2065void test_callback_buffer_sanity() 2066{ 2067 enum { MSGLEN = 10 }; 2068 mbedtls_test_buffer buf; 2069 unsigned char input[MSGLEN]; 2070 unsigned char output[MSGLEN]; 2071 2072 memset( input, 0, sizeof(input) ); 2073 2074 /* Make sure calling put and get on NULL buffer results in error. */ 2075 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) ) 2076 == -1 ); 2077 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) ) 2078 == -1 ); 2079 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 ); 2080 2081 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 ); 2082 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 ); 2083 2084 /* Make sure calling put and get on a buffer that hasn't been set up results 2085 * in eror. */ 2086 mbedtls_test_buffer_init( &buf ); 2087 2088 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 ); 2089 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) ) 2090 == -1 ); 2091 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); 2092 2093 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 ); 2094 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 ); 2095 2096 /* Make sure calling put and get on NULL input only results in 2097 * error if the length is not zero, and that a NULL output is valid for data 2098 * dropping. 2099 */ 2100 2101 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 ); 2102 2103 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); 2104 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) ) 2105 == 0 ); 2106 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 ); 2107 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 ); 2108 2109 /* Make sure calling put several times in the row is safe */ 2110 2111 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) 2112 == sizeof( input ) ); 2113 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 ); 2114 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 ); 2115 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 ); 2116 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 ); 2117 2118 2119exit: 2120 2121 mbedtls_test_buffer_free( &buf ); 2122} 2123/* END_CASE */ 2124 2125/* 2126 * Test if the implementation of `mbedtls_test_buffer` related functions is 2127 * correct and works as expected. 2128 * 2129 * That is 2130 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. 2131 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. 2132 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret 2133 * bytes. 2134 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. 2135 * - All of the bytes we got match the bytes we put in in a FIFO manner. 2136 */ 2137 2138/* BEGIN_CASE */ 2139void test_callback_buffer( int size, int put1, int put1_ret, 2140 int get1, int get1_ret, int put2, int put2_ret, 2141 int get2, int get2_ret ) 2142{ 2143 enum { ROUNDS = 2 }; 2144 size_t put[ROUNDS]; 2145 int put_ret[ROUNDS]; 2146 size_t get[ROUNDS]; 2147 int get_ret[ROUNDS]; 2148 mbedtls_test_buffer buf; 2149 unsigned char* input = NULL; 2150 size_t input_len; 2151 unsigned char* output = NULL; 2152 size_t output_len; 2153 size_t i, j, written, read; 2154 2155 mbedtls_test_buffer_init( &buf ); 2156 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 ); 2157 2158 /* Check the sanity of input parameters and initialise local variables. That 2159 * is, ensure that the amount of data is not negative and that we are not 2160 * expecting more to put or get than we actually asked for. */ 2161 TEST_ASSERT( put1 >= 0 ); 2162 put[0] = put1; 2163 put_ret[0] = put1_ret; 2164 TEST_ASSERT( put1_ret <= put1 ); 2165 TEST_ASSERT( put2 >= 0 ); 2166 put[1] = put2; 2167 put_ret[1] = put2_ret; 2168 TEST_ASSERT( put2_ret <= put2 ); 2169 2170 TEST_ASSERT( get1 >= 0 ); 2171 get[0] = get1; 2172 get_ret[0] = get1_ret; 2173 TEST_ASSERT( get1_ret <= get1 ); 2174 TEST_ASSERT( get2 >= 0 ); 2175 get[1] = get2; 2176 get_ret[1] = get2_ret; 2177 TEST_ASSERT( get2_ret <= get2 ); 2178 2179 input_len = 0; 2180 /* Calculate actual input and output lengths */ 2181 for( j = 0; j < ROUNDS; j++ ) 2182 { 2183 if( put_ret[j] > 0 ) 2184 { 2185 input_len += put_ret[j]; 2186 } 2187 } 2188 /* In order to always have a valid pointer we always allocate at least 1 2189 * byte. */ 2190 if( input_len == 0 ) 2191 input_len = 1; 2192 ASSERT_ALLOC( input, input_len ); 2193 2194 output_len = 0; 2195 for( j = 0; j < ROUNDS; j++ ) 2196 { 2197 if( get_ret[j] > 0 ) 2198 { 2199 output_len += get_ret[j]; 2200 } 2201 } 2202 TEST_ASSERT( output_len <= input_len ); 2203 /* In order to always have a valid pointer we always allocate at least 1 2204 * byte. */ 2205 if( output_len == 0 ) 2206 output_len = 1; 2207 ASSERT_ALLOC( output, output_len ); 2208 2209 /* Fill up the buffer with structured data so that unwanted changes 2210 * can be detected */ 2211 for( i = 0; i < input_len; i++ ) 2212 { 2213 input[i] = i & 0xFF; 2214 } 2215 2216 written = read = 0; 2217 for( j = 0; j < ROUNDS; j++ ) 2218 { 2219 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf, 2220 input + written, put[j] ) ); 2221 written += put_ret[j]; 2222 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf, 2223 output + read, get[j] ) ); 2224 read += get_ret[j]; 2225 TEST_ASSERT( read <= written ); 2226 if( get_ret[j] > 0 ) 2227 { 2228 TEST_ASSERT( memcmp( output + read - get_ret[j], 2229 input + read - get_ret[j], get_ret[j] ) 2230 == 0 ); 2231 } 2232 } 2233 2234exit: 2235 2236 mbedtls_free( input ); 2237 mbedtls_free( output ); 2238 mbedtls_test_buffer_free( &buf ); 2239} 2240/* END_CASE */ 2241 2242/* 2243 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is 2244 * correct and works as expected on unconnected sockets. 2245 */ 2246 2247/* BEGIN_CASE */ 2248void ssl_mock_sanity( ) 2249{ 2250 enum { MSGLEN = 105 }; 2251 unsigned char message[MSGLEN] = { 0 }; 2252 unsigned char received[MSGLEN] = { 0 }; 2253 mbedtls_mock_socket socket; 2254 2255 mbedtls_mock_socket_init( &socket ); 2256 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 ); 2257 mbedtls_mock_socket_close( &socket ); 2258 mbedtls_mock_socket_init( &socket ); 2259 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 ); 2260 mbedtls_mock_socket_close( &socket ); 2261 2262 mbedtls_mock_socket_init( &socket ); 2263 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 ); 2264 mbedtls_mock_socket_close( &socket ); 2265 mbedtls_mock_socket_init( &socket ); 2266 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 ); 2267 mbedtls_mock_socket_close( &socket ); 2268 2269exit: 2270 2271 mbedtls_mock_socket_close( &socket ); 2272} 2273/* END_CASE */ 2274 2275/* 2276 * Test if the implementation of `mbedtls_mock_socket` related functions can 2277 * send a single message from the client to the server. 2278 */ 2279 2280/* BEGIN_CASE */ 2281void ssl_mock_tcp( int blocking ) 2282{ 2283 enum { MSGLEN = 105 }; 2284 enum { BUFLEN = MSGLEN / 5 }; 2285 unsigned char message[MSGLEN]; 2286 unsigned char received[MSGLEN]; 2287 mbedtls_mock_socket client; 2288 mbedtls_mock_socket server; 2289 size_t written, read; 2290 int send_ret, recv_ret; 2291 mbedtls_ssl_send_t *send; 2292 mbedtls_ssl_recv_t *recv; 2293 unsigned i; 2294 2295 if( blocking == 0 ) 2296 { 2297 send = mbedtls_mock_tcp_send_nb; 2298 recv = mbedtls_mock_tcp_recv_nb; 2299 } 2300 else 2301 { 2302 send = mbedtls_mock_tcp_send_b; 2303 recv = mbedtls_mock_tcp_recv_b; 2304 } 2305 2306 mbedtls_mock_socket_init( &client ); 2307 mbedtls_mock_socket_init( &server ); 2308 2309 /* Fill up the buffer with structured data so that unwanted changes 2310 * can be detected */ 2311 for( i = 0; i < MSGLEN; i++ ) 2312 { 2313 message[i] = i & 0xFF; 2314 } 2315 2316 /* Make sure that sending a message takes a few iterations. */ 2317 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); 2318 2319 /* Send the message to the server */ 2320 send_ret = recv_ret = 1; 2321 written = read = 0; 2322 while( send_ret != 0 || recv_ret != 0 ) 2323 { 2324 send_ret = send( &client, message + written, MSGLEN - written ); 2325 2326 TEST_ASSERT( send_ret >= 0 ); 2327 TEST_ASSERT( send_ret <= BUFLEN ); 2328 written += send_ret; 2329 2330 /* If the buffer is full we can test blocking and non-blocking send */ 2331 if ( send_ret == BUFLEN ) 2332 { 2333 int blocking_ret = send( &client, message , 1 ); 2334 if ( blocking ) 2335 { 2336 TEST_ASSERT( blocking_ret == 0 ); 2337 } 2338 else 2339 { 2340 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 2341 } 2342 } 2343 2344 recv_ret = recv( &server, received + read, MSGLEN - read ); 2345 2346 /* The result depends on whether any data was sent */ 2347 if ( send_ret > 0 ) 2348 { 2349 TEST_ASSERT( recv_ret > 0 ); 2350 TEST_ASSERT( recv_ret <= BUFLEN ); 2351 read += recv_ret; 2352 } 2353 else if( blocking ) 2354 { 2355 TEST_ASSERT( recv_ret == 0 ); 2356 } 2357 else 2358 { 2359 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ ); 2360 recv_ret = 0; 2361 } 2362 2363 /* If the buffer is empty we can test blocking and non-blocking read */ 2364 if ( recv_ret == BUFLEN ) 2365 { 2366 int blocking_ret = recv( &server, received, 1 ); 2367 if ( blocking ) 2368 { 2369 TEST_ASSERT( blocking_ret == 0 ); 2370 } 2371 else 2372 { 2373 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); 2374 } 2375 } 2376 } 2377 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 2378 2379exit: 2380 2381 mbedtls_mock_socket_close( &client ); 2382 mbedtls_mock_socket_close( &server ); 2383} 2384/* END_CASE */ 2385 2386/* 2387 * Test if the implementation of `mbedtls_mock_socket` related functions can 2388 * send messages in both direction at the same time (with the I/O calls 2389 * interleaving). 2390 */ 2391 2392/* BEGIN_CASE */ 2393void ssl_mock_tcp_interleaving( int blocking ) 2394{ 2395 enum { ROUNDS = 2 }; 2396 enum { MSGLEN = 105 }; 2397 enum { BUFLEN = MSGLEN / 5 }; 2398 unsigned char message[ROUNDS][MSGLEN]; 2399 unsigned char received[ROUNDS][MSGLEN]; 2400 mbedtls_mock_socket client; 2401 mbedtls_mock_socket server; 2402 size_t written[ROUNDS]; 2403 size_t read[ROUNDS]; 2404 int send_ret[ROUNDS]; 2405 int recv_ret[ROUNDS]; 2406 unsigned i, j, progress; 2407 mbedtls_ssl_send_t *send; 2408 mbedtls_ssl_recv_t *recv; 2409 2410 if( blocking == 0 ) 2411 { 2412 send = mbedtls_mock_tcp_send_nb; 2413 recv = mbedtls_mock_tcp_recv_nb; 2414 } 2415 else 2416 { 2417 send = mbedtls_mock_tcp_send_b; 2418 recv = mbedtls_mock_tcp_recv_b; 2419 } 2420 2421 mbedtls_mock_socket_init( &client ); 2422 mbedtls_mock_socket_init( &server ); 2423 2424 /* Fill up the buffers with structured data so that unwanted changes 2425 * can be detected */ 2426 for( i = 0; i < ROUNDS; i++ ) 2427 { 2428 for( j = 0; j < MSGLEN; j++ ) 2429 { 2430 message[i][j] = ( i * MSGLEN + j ) & 0xFF; 2431 } 2432 } 2433 2434 /* Make sure that sending a message takes a few iterations. */ 2435 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); 2436 2437 /* Send the message from both sides, interleaving. */ 2438 progress = 1; 2439 for( i = 0; i < ROUNDS; i++ ) 2440 { 2441 written[i] = 0; 2442 read[i] = 0; 2443 } 2444 /* This loop does not stop as long as there was a successful write or read 2445 * of at least one byte on either side. */ 2446 while( progress != 0 ) 2447 { 2448 mbedtls_mock_socket *socket; 2449 2450 for( i = 0; i < ROUNDS; i++ ) 2451 { 2452 /* First sending is from the client */ 2453 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server ); 2454 2455 send_ret[i] = send( socket, message[i] + written[i], 2456 MSGLEN - written[i] ); 2457 TEST_ASSERT( send_ret[i] >= 0 ); 2458 TEST_ASSERT( send_ret[i] <= BUFLEN ); 2459 written[i] += send_ret[i]; 2460 2461 /* If the buffer is full we can test blocking and non-blocking 2462 * send */ 2463 if ( send_ret[i] == BUFLEN ) 2464 { 2465 int blocking_ret = send( socket, message[i] , 1 ); 2466 if ( blocking ) 2467 { 2468 TEST_ASSERT( blocking_ret == 0 ); 2469 } 2470 else 2471 { 2472 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); 2473 } 2474 } 2475 } 2476 2477 for( i = 0; i < ROUNDS; i++ ) 2478 { 2479 /* First receiving is from the server */ 2480 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client ); 2481 2482 recv_ret[i] = recv( socket, received[i] + read[i], 2483 MSGLEN - read[i] ); 2484 2485 /* The result depends on whether any data was sent */ 2486 if ( send_ret[i] > 0 ) 2487 { 2488 TEST_ASSERT( recv_ret[i] > 0 ); 2489 TEST_ASSERT( recv_ret[i] <= BUFLEN ); 2490 read[i] += recv_ret[i]; 2491 } 2492 else if( blocking ) 2493 { 2494 TEST_ASSERT( recv_ret[i] == 0 ); 2495 } 2496 else 2497 { 2498 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ); 2499 recv_ret[i] = 0; 2500 } 2501 2502 /* If the buffer is empty we can test blocking and non-blocking 2503 * read */ 2504 if ( recv_ret[i] == BUFLEN ) 2505 { 2506 int blocking_ret = recv( socket, received[i], 1 ); 2507 if ( blocking ) 2508 { 2509 TEST_ASSERT( blocking_ret == 0 ); 2510 } 2511 else 2512 { 2513 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); 2514 } 2515 } 2516 } 2517 2518 progress = 0; 2519 for( i = 0; i < ROUNDS; i++ ) 2520 { 2521 progress += send_ret[i] + recv_ret[i]; 2522 } 2523 } 2524 2525 for( i = 0; i < ROUNDS; i++ ) 2526 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 ); 2527 2528exit: 2529 2530 mbedtls_mock_socket_close( &client ); 2531 mbedtls_mock_socket_close( &server ); 2532} 2533/* END_CASE */ 2534 2535/* BEGIN_CASE */ 2536void ssl_message_queue_sanity( ) 2537{ 2538 mbedtls_test_message_queue queue; 2539 2540 /* Trying to push/pull to an empty queue */ 2541 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 ) 2542 == MBEDTLS_TEST_ERROR_ARG_NULL ); 2543 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 ) 2544 == MBEDTLS_TEST_ERROR_ARG_NULL ); 2545 2546 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); 2547 TEST_ASSERT( queue.capacity == 3 ); 2548 TEST_ASSERT( queue.num == 0 ); 2549 2550exit: 2551 mbedtls_test_message_queue_free( &queue ); 2552} 2553/* END_CASE */ 2554 2555/* BEGIN_CASE */ 2556void ssl_message_queue_basic( ) 2557{ 2558 mbedtls_test_message_queue queue; 2559 2560 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); 2561 2562 /* Sanity test - 3 pushes and 3 pops with sufficient space */ 2563 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); 2564 TEST_ASSERT( queue.capacity == 3 ); 2565 TEST_ASSERT( queue.num == 1 ); 2566 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); 2567 TEST_ASSERT( queue.capacity == 3 ); 2568 TEST_ASSERT( queue.num == 2 ); 2569 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); 2570 TEST_ASSERT( queue.capacity == 3 ); 2571 TEST_ASSERT( queue.num == 3 ); 2572 2573 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); 2574 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); 2575 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); 2576 2577exit: 2578 mbedtls_test_message_queue_free( &queue ); 2579} 2580/* END_CASE */ 2581 2582/* BEGIN_CASE */ 2583void ssl_message_queue_overflow_underflow( ) 2584{ 2585 mbedtls_test_message_queue queue; 2586 2587 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); 2588 2589 /* 4 pushes (last one with an error), 4 pops (last one with an error) */ 2590 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); 2591 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); 2592 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); 2593 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) 2594 == MBEDTLS_ERR_SSL_WANT_WRITE ); 2595 2596 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); 2597 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); 2598 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); 2599 2600 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) 2601 == MBEDTLS_ERR_SSL_WANT_READ ); 2602 2603exit: 2604 mbedtls_test_message_queue_free( &queue ); 2605} 2606/* END_CASE */ 2607 2608/* BEGIN_CASE */ 2609void ssl_message_queue_interleaved( ) 2610{ 2611 mbedtls_test_message_queue queue; 2612 2613 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); 2614 2615 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops 2616 * (to wrap around the buffer) */ 2617 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); 2618 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); 2619 2620 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); 2621 2622 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); 2623 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 ); 2624 2625 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); 2626 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); 2627 2628 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 ); 2629 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 ); 2630 2631 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 ); 2632 2633 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 ); 2634 2635 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 ); 2636 2637exit: 2638 mbedtls_test_message_queue_free( &queue ); 2639} 2640/* END_CASE */ 2641 2642/* BEGIN_CASE */ 2643void ssl_message_queue_insufficient_buffer( ) 2644{ 2645 mbedtls_test_message_queue queue; 2646 size_t message_len = 10; 2647 size_t buffer_len = 5; 2648 2649 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 ); 2650 2651 /* Popping without a sufficient buffer */ 2652 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len ) 2653 == (int) message_len ); 2654 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len ) 2655 == (int) buffer_len ); 2656exit: 2657 mbedtls_test_message_queue_free( &queue ); 2658} 2659/* END_CASE */ 2660 2661/* BEGIN_CASE */ 2662void ssl_message_mock_uninitialized( ) 2663{ 2664 enum { MSGLEN = 10 }; 2665 unsigned char message[MSGLEN] = {0}, received[MSGLEN]; 2666 mbedtls_mock_socket client, server; 2667 mbedtls_test_message_queue server_queue, client_queue; 2668 mbedtls_test_message_socket_context server_context, client_context; 2669 mbedtls_message_socket_init( &server_context ); 2670 mbedtls_message_socket_init( &client_context ); 2671 2672 /* Send with a NULL context */ 2673 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN ) 2674 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); 2675 2676 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN ) 2677 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); 2678 2679 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, 2680 &server, 2681 &server_context ) == 0 ); 2682 2683 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, 2684 &client, 2685 &client_context ) == 0 ); 2686 2687 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN ) 2688 == MBEDTLS_TEST_ERROR_SEND_FAILED ); 2689 2690 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2691 == MBEDTLS_ERR_SSL_WANT_READ ); 2692 2693 /* Push directly to a queue to later simulate a disconnected behavior */ 2694 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN ) 2695 == MSGLEN ); 2696 2697 /* Test if there's an error when trying to read from a disconnected 2698 * socket */ 2699 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2700 == MBEDTLS_TEST_ERROR_RECV_FAILED ); 2701 exit: 2702 mbedtls_message_socket_close( &server_context ); 2703 mbedtls_message_socket_close( &client_context ); 2704} 2705/* END_CASE */ 2706 2707/* BEGIN_CASE */ 2708void ssl_message_mock_basic( ) 2709{ 2710 enum { MSGLEN = 10 }; 2711 unsigned char message[MSGLEN], received[MSGLEN]; 2712 mbedtls_mock_socket client, server; 2713 unsigned i; 2714 mbedtls_test_message_queue server_queue, client_queue; 2715 mbedtls_test_message_socket_context server_context, client_context; 2716 mbedtls_message_socket_init( &server_context ); 2717 mbedtls_message_socket_init( &client_context ); 2718 2719 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, 2720 &server, 2721 &server_context ) == 0 ); 2722 2723 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, 2724 &client, 2725 &client_context ) == 0 ); 2726 2727 /* Fill up the buffer with structured data so that unwanted changes 2728 * can be detected */ 2729 for( i = 0; i < MSGLEN; i++ ) 2730 { 2731 message[i] = i & 0xFF; 2732 } 2733 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 2734 MSGLEN ) ); 2735 2736 /* Send the message to the server */ 2737 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2738 MSGLEN ) == MSGLEN ); 2739 2740 /* Read from the server */ 2741 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2742 == MSGLEN ); 2743 2744 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 2745 memset( received, 0, MSGLEN ); 2746 2747 /* Send the message to the client */ 2748 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, 2749 MSGLEN ) == MSGLEN ); 2750 2751 /* Read from the client */ 2752 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) 2753 == MSGLEN ); 2754 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 2755 2756 exit: 2757 mbedtls_message_socket_close( &server_context ); 2758 mbedtls_message_socket_close( &client_context ); 2759} 2760/* END_CASE */ 2761 2762/* BEGIN_CASE */ 2763void ssl_message_mock_queue_overflow_underflow( ) 2764{ 2765 enum { MSGLEN = 10 }; 2766 unsigned char message[MSGLEN], received[MSGLEN]; 2767 mbedtls_mock_socket client, server; 2768 unsigned i; 2769 mbedtls_test_message_queue server_queue, client_queue; 2770 mbedtls_test_message_socket_context server_context, client_context; 2771 mbedtls_message_socket_init( &server_context ); 2772 mbedtls_message_socket_init( &client_context ); 2773 2774 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, 2775 &server, 2776 &server_context ) == 0 ); 2777 2778 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, 2779 &client, 2780 &client_context ) == 0 ); 2781 2782 /* Fill up the buffer with structured data so that unwanted changes 2783 * can be detected */ 2784 for( i = 0; i < MSGLEN; i++ ) 2785 { 2786 message[i] = i & 0xFF; 2787 } 2788 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 2789 MSGLEN*2 ) ); 2790 2791 /* Send three message to the server, last one with an error */ 2792 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2793 MSGLEN - 1 ) == MSGLEN - 1 ); 2794 2795 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2796 MSGLEN ) == MSGLEN ); 2797 2798 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2799 MSGLEN ) 2800 == MBEDTLS_ERR_SSL_WANT_WRITE ); 2801 2802 /* Read three messages from the server, last one with an error */ 2803 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, 2804 MSGLEN - 1 ) == MSGLEN - 1 ); 2805 2806 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2807 == MSGLEN ); 2808 2809 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 2810 2811 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2812 == MBEDTLS_ERR_SSL_WANT_READ ); 2813 2814 exit: 2815 mbedtls_message_socket_close( &server_context ); 2816 mbedtls_message_socket_close( &client_context ); 2817} 2818/* END_CASE */ 2819 2820/* BEGIN_CASE */ 2821void ssl_message_mock_socket_overflow( ) 2822{ 2823 enum { MSGLEN = 10 }; 2824 unsigned char message[MSGLEN], received[MSGLEN]; 2825 mbedtls_mock_socket client, server; 2826 unsigned i; 2827 mbedtls_test_message_queue server_queue, client_queue; 2828 mbedtls_test_message_socket_context server_context, client_context; 2829 mbedtls_message_socket_init( &server_context ); 2830 mbedtls_message_socket_init( &client_context ); 2831 2832 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, 2833 &server, 2834 &server_context ) == 0 ); 2835 2836 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, 2837 &client, 2838 &client_context ) == 0 ); 2839 2840 /* Fill up the buffer with structured data so that unwanted changes 2841 * can be detected */ 2842 for( i = 0; i < MSGLEN; i++ ) 2843 { 2844 message[i] = i & 0xFF; 2845 } 2846 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 2847 MSGLEN ) ); 2848 2849 /* Send two message to the server, second one with an error */ 2850 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2851 MSGLEN ) == MSGLEN ); 2852 2853 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2854 MSGLEN ) 2855 == MBEDTLS_TEST_ERROR_SEND_FAILED ); 2856 2857 /* Read the only message from the server */ 2858 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2859 == MSGLEN ); 2860 2861 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 2862 2863 exit: 2864 mbedtls_message_socket_close( &server_context ); 2865 mbedtls_message_socket_close( &client_context ); 2866} 2867/* END_CASE */ 2868 2869/* BEGIN_CASE */ 2870void ssl_message_mock_truncated( ) 2871{ 2872 enum { MSGLEN = 10 }; 2873 unsigned char message[MSGLEN], received[MSGLEN]; 2874 mbedtls_mock_socket client, server; 2875 unsigned i; 2876 mbedtls_test_message_queue server_queue, client_queue; 2877 mbedtls_test_message_socket_context server_context, client_context; 2878 mbedtls_message_socket_init( &server_context ); 2879 mbedtls_message_socket_init( &client_context ); 2880 2881 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, 2882 &server, 2883 &server_context ) == 0 ); 2884 2885 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, 2886 &client, 2887 &client_context ) == 0 ); 2888 2889 memset( received, 0, MSGLEN ); 2890 /* Fill up the buffer with structured data so that unwanted changes 2891 * can be detected */ 2892 for( i = 0; i < MSGLEN; i++ ) 2893 { 2894 message[i] = i & 0xFF; 2895 } 2896 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 2897 2 * MSGLEN ) ); 2898 2899 /* Send two messages to the server, the second one small enough to fit in the 2900 * receiver's buffer. */ 2901 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2902 MSGLEN ) == MSGLEN ); 2903 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2904 MSGLEN / 2 ) == MSGLEN / 2 ); 2905 /* Read a truncated message from the server */ 2906 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) 2907 == MSGLEN/2 ); 2908 2909 /* Test that the first half of the message is valid, and second one isn't */ 2910 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); 2911 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 ) 2912 != 0 ); 2913 memset( received, 0, MSGLEN ); 2914 2915 /* Read a full message from the server */ 2916 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) 2917 == MSGLEN / 2 ); 2918 2919 /* Test that the first half of the message is valid */ 2920 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); 2921 2922 exit: 2923 mbedtls_message_socket_close( &server_context ); 2924 mbedtls_message_socket_close( &client_context ); 2925} 2926/* END_CASE */ 2927 2928/* BEGIN_CASE */ 2929void ssl_message_mock_socket_read_error( ) 2930{ 2931 enum { MSGLEN = 10 }; 2932 unsigned char message[MSGLEN], received[MSGLEN]; 2933 mbedtls_mock_socket client, server; 2934 unsigned i; 2935 mbedtls_test_message_queue server_queue, client_queue; 2936 mbedtls_test_message_socket_context server_context, client_context; 2937 mbedtls_message_socket_init( &server_context ); 2938 mbedtls_message_socket_init( &client_context ); 2939 2940 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, 2941 &server, 2942 &server_context ) == 0 ); 2943 2944 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, 2945 &client, 2946 &client_context ) == 0 ); 2947 2948 /* Fill up the buffer with structured data so that unwanted changes 2949 * can be detected */ 2950 for( i = 0; i < MSGLEN; i++ ) 2951 { 2952 message[i] = i & 0xFF; 2953 } 2954 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 2955 MSGLEN ) ); 2956 2957 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 2958 MSGLEN ) == MSGLEN ); 2959 2960 /* Force a read error by disconnecting the socket by hand */ 2961 server.status = 0; 2962 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2963 == MBEDTLS_TEST_ERROR_RECV_FAILED ); 2964 /* Return to a valid state */ 2965 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; 2966 2967 memset( received, 0, sizeof( received ) ); 2968 2969 /* Test that even though the server tried to read once disconnected, the 2970 * continuity is preserved */ 2971 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 2972 == MSGLEN ); 2973 2974 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 2975 2976 exit: 2977 mbedtls_message_socket_close( &server_context ); 2978 mbedtls_message_socket_close( &client_context ); 2979} 2980/* END_CASE */ 2981 2982/* BEGIN_CASE */ 2983void ssl_message_mock_interleaved_one_way( ) 2984{ 2985 enum { MSGLEN = 10 }; 2986 unsigned char message[MSGLEN], received[MSGLEN]; 2987 mbedtls_mock_socket client, server; 2988 unsigned i; 2989 mbedtls_test_message_queue server_queue, client_queue; 2990 mbedtls_test_message_socket_context server_context, client_context; 2991 mbedtls_message_socket_init( &server_context ); 2992 mbedtls_message_socket_init( &client_context ); 2993 2994 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, 2995 &server, 2996 &server_context ) == 0 ); 2997 2998 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, 2999 &client, 3000 &client_context ) == 0 ); 3001 3002 /* Fill up the buffer with structured data so that unwanted changes 3003 * can be detected */ 3004 for( i = 0; i < MSGLEN; i++ ) 3005 { 3006 message[i] = i & 0xFF; 3007 } 3008 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 3009 MSGLEN*3 ) ); 3010 3011 /* Interleaved test - [2 sends, 1 read] twice, and then two reads 3012 * (to wrap around the buffer) */ 3013 for( i = 0; i < 2; i++ ) 3014 { 3015 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 3016 MSGLEN ) == MSGLEN ); 3017 3018 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 3019 MSGLEN ) == MSGLEN ); 3020 3021 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, 3022 MSGLEN ) == MSGLEN ); 3023 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 3024 memset( received, 0, sizeof( received ) ); 3025 } 3026 3027 for( i = 0; i < 2; i++ ) 3028 { 3029 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, 3030 MSGLEN ) == MSGLEN ); 3031 3032 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 3033 } 3034 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 3035 == MBEDTLS_ERR_SSL_WANT_READ ); 3036 exit: 3037 mbedtls_message_socket_close( &server_context ); 3038 mbedtls_message_socket_close( &client_context ); 3039} 3040/* END_CASE */ 3041 3042/* BEGIN_CASE */ 3043void ssl_message_mock_interleaved_two_ways( ) 3044{ 3045 enum { MSGLEN = 10 }; 3046 unsigned char message[MSGLEN], received[MSGLEN]; 3047 mbedtls_mock_socket client, server; 3048 unsigned i; 3049 mbedtls_test_message_queue server_queue, client_queue; 3050 mbedtls_test_message_socket_context server_context, client_context; 3051 mbedtls_message_socket_init( &server_context ); 3052 mbedtls_message_socket_init( &client_context ); 3053 3054 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, 3055 &server, 3056 &server_context ) == 0 ); 3057 3058 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, 3059 &client, 3060 &client_context ) == 0 ); 3061 3062 /* Fill up the buffer with structured data so that unwanted changes 3063 * can be detected */ 3064 for( i = 0; i < MSGLEN; i++ ) 3065 { 3066 message[i] = i & 0xFF; 3067 } 3068 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, 3069 MSGLEN*3 ) ); 3070 3071 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads 3072 * (to wrap around the buffer) both ways. */ 3073 for( i = 0; i < 2; i++ ) 3074 { 3075 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 3076 MSGLEN ) == MSGLEN ); 3077 3078 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, 3079 MSGLEN ) == MSGLEN ); 3080 3081 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, 3082 MSGLEN ) == MSGLEN ); 3083 3084 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, 3085 MSGLEN ) == MSGLEN ); 3086 3087 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, 3088 MSGLEN ) == MSGLEN ); 3089 3090 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 3091 3092 memset( received, 0, sizeof( received ) ); 3093 3094 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, 3095 MSGLEN ) == MSGLEN ); 3096 3097 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 3098 3099 memset( received, 0, sizeof( received ) ); 3100 } 3101 3102 for( i = 0; i < 2; i++ ) 3103 { 3104 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, 3105 MSGLEN ) == MSGLEN ); 3106 3107 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 3108 memset( received, 0, sizeof( received ) ); 3109 3110 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, 3111 MSGLEN ) == MSGLEN ); 3112 3113 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); 3114 memset( received, 0, sizeof( received ) ); 3115 } 3116 3117 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) 3118 == MBEDTLS_ERR_SSL_WANT_READ ); 3119 3120 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) 3121 == MBEDTLS_ERR_SSL_WANT_READ ); 3122 exit: 3123 mbedtls_message_socket_close( &server_context ); 3124 mbedtls_message_socket_close( &client_context ); 3125} 3126/* END_CASE */ 3127 3128/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ 3129void ssl_dtls_replay( data_t * prevs, data_t * new, int ret ) 3130{ 3131 uint32_t len = 0; 3132 mbedtls_ssl_context ssl; 3133 mbedtls_ssl_config conf; 3134 3135 mbedtls_ssl_init( &ssl ); 3136 mbedtls_ssl_config_init( &conf ); 3137 3138 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, 3139 MBEDTLS_SSL_IS_CLIENT, 3140 MBEDTLS_SSL_TRANSPORT_DATAGRAM, 3141 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); 3142 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); 3143 3144 /* Read previous record numbers */ 3145 for( len = 0; len < prevs->len; len += 6 ) 3146 { 3147 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); 3148 mbedtls_ssl_dtls_replay_update( &ssl ); 3149 } 3150 3151 /* Check new number */ 3152 memcpy( ssl.in_ctr + 2, new->x, 6 ); 3153 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); 3154 3155 mbedtls_ssl_free( &ssl ); 3156 mbedtls_ssl_config_free( &conf ); 3157} 3158/* END_CASE */ 3159 3160/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ 3161void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) 3162{ 3163 mbedtls_ssl_context ssl; 3164 mbedtls_ssl_init( &ssl ); 3165 3166 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); 3167 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); 3168 3169 mbedtls_ssl_free( &ssl ); 3170} 3171/* END_CASE */ 3172 3173/* BEGIN_CASE */ 3174void ssl_crypt_record( int cipher_type, int hash_id, 3175 int etm, int tag_mode, int ver, 3176 int cid0_len, int cid1_len ) 3177{ 3178 /* 3179 * Test several record encryptions and decryptions 3180 * with plenty of space before and after the data 3181 * within the record buffer. 3182 */ 3183 3184 int ret; 3185 int num_records = 16; 3186 mbedtls_ssl_context ssl; /* ONLY for debugging */ 3187 3188 mbedtls_ssl_transform t0, t1; 3189 unsigned char *buf = NULL; 3190 size_t const buflen = 512; 3191 mbedtls_record rec, rec_backup; 3192 3193 mbedtls_ssl_init( &ssl ); 3194 mbedtls_ssl_transform_init( &t0 ); 3195 mbedtls_ssl_transform_init( &t1 ); 3196 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, 3197 etm, tag_mode, ver, 3198 (size_t) cid0_len, 3199 (size_t) cid1_len ) == 0 ); 3200 3201 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); 3202 3203 while( num_records-- > 0 ) 3204 { 3205 mbedtls_ssl_transform *t_dec, *t_enc; 3206 /* Take turns in who's sending and who's receiving. */ 3207 if( num_records % 3 == 0 ) 3208 { 3209 t_dec = &t0; 3210 t_enc = &t1; 3211 } 3212 else 3213 { 3214 t_dec = &t1; 3215 t_enc = &t0; 3216 } 3217 3218 /* 3219 * The record header affects the transformation in two ways: 3220 * 1) It determines the AEAD additional data 3221 * 2) The record counter sometimes determines the IV. 3222 * 3223 * Apart from that, the fields don't have influence. 3224 * In particular, it is currently not the responsibility 3225 * of ssl_encrypt/decrypt_buf to check if the transform 3226 * version matches the record version, or that the 3227 * type is sensible. 3228 */ 3229 3230 memset( rec.ctr, num_records, sizeof( rec.ctr ) ); 3231 rec.type = 42; 3232 rec.ver[0] = num_records; 3233 rec.ver[1] = num_records; 3234#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 3235 rec.cid_len = 0; 3236#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 3237 3238 rec.buf = buf; 3239 rec.buf_len = buflen; 3240 rec.data_offset = 16; 3241 /* Make sure to vary the length to exercise different 3242 * paddings. */ 3243 rec.data_len = 1 + num_records; 3244 3245 memset( rec.buf + rec.data_offset, 42, rec.data_len ); 3246 3247 /* Make a copy for later comparison */ 3248 rec_backup = rec; 3249 3250 /* Encrypt record */ 3251 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, 3252 mbedtls_test_rnd_std_rand, NULL ); 3253 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 3254 if( ret != 0 ) 3255 { 3256 continue; 3257 } 3258 3259#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 3260 if( rec.cid_len != 0 ) 3261 { 3262 /* DTLS 1.2 + CID hides the real content type and 3263 * uses a special CID content type in the protected 3264 * record. Double-check this. */ 3265 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); 3266 } 3267#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 3268 3269#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) 3270 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) 3271 { 3272 /* TLS 1.3 hides the real content type and 3273 * always uses Application Data as the content type 3274 * for protected records. Double-check this. */ 3275 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); 3276 } 3277#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 3278 3279 /* Decrypt record with t_dec */ 3280 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ); 3281 TEST_ASSERT( ret == 0 ); 3282 3283 /* Compare results */ 3284 TEST_ASSERT( rec.type == rec_backup.type ); 3285 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); 3286 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); 3287 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); 3288 TEST_ASSERT( rec.data_len == rec_backup.data_len ); 3289 TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); 3290 TEST_ASSERT( memcmp( rec.buf + rec.data_offset, 3291 rec_backup.buf + rec_backup.data_offset, 3292 rec.data_len ) == 0 ); 3293 } 3294 3295exit: 3296 3297 /* Cleanup */ 3298 mbedtls_ssl_free( &ssl ); 3299 mbedtls_ssl_transform_free( &t0 ); 3300 mbedtls_ssl_transform_free( &t1 ); 3301 3302 mbedtls_free( buf ); 3303} 3304/* END_CASE */ 3305 3306/* BEGIN_CASE */ 3307void ssl_crypt_record_small( int cipher_type, int hash_id, 3308 int etm, int tag_mode, int ver, 3309 int cid0_len, int cid1_len ) 3310{ 3311 /* 3312 * Test pairs of encryption and decryption with an increasing 3313 * amount of space in the record buffer - in more detail: 3314 * 1) Try to encrypt with 0, 1, 2, ... bytes available 3315 * in front of the plaintext, and expect the encryption 3316 * to succeed starting from some offset. Always keep 3317 * enough space in the end of the buffer. 3318 * 2) Try to encrypt with 0, 1, 2, ... bytes available 3319 * at the end of the plaintext, and expect the encryption 3320 * to succeed starting from some offset. Always keep 3321 * enough space at the beginning of the buffer. 3322 * 3) Try to encrypt with 0, 1, 2, ... bytes available 3323 * both at the front and end of the plaintext, 3324 * and expect the encryption to succeed starting from 3325 * some offset. 3326 * 3327 * If encryption succeeds, check that decryption succeeds 3328 * and yields the original record. 3329 */ 3330 3331 mbedtls_ssl_context ssl; /* ONLY for debugging */ 3332 3333 mbedtls_ssl_transform t0, t1; 3334 unsigned char *buf = NULL; 3335 size_t const buflen = 256; 3336 mbedtls_record rec, rec_backup; 3337 3338 int ret; 3339 int mode; /* Mode 1, 2 or 3 as explained above */ 3340 size_t offset; /* Available space at beginning/end/both */ 3341 size_t threshold = 96; /* Maximum offset to test against */ 3342 3343 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ 3344 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */ 3345 3346 int seen_success; /* Indicates if in the current mode we've 3347 * already seen a successful test. */ 3348 3349 mbedtls_ssl_init( &ssl ); 3350 mbedtls_ssl_transform_init( &t0 ); 3351 mbedtls_ssl_transform_init( &t1 ); 3352 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, 3353 etm, tag_mode, ver, 3354 (size_t) cid0_len, 3355 (size_t) cid1_len ) == 0 ); 3356 3357 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); 3358 3359 for( mode=1; mode <= 3; mode++ ) 3360 { 3361 seen_success = 0; 3362 for( offset=0; offset <= threshold; offset++ ) 3363 { 3364 mbedtls_ssl_transform *t_dec, *t_enc; 3365 t_dec = &t0; 3366 t_enc = &t1; 3367 3368 memset( rec.ctr, offset, sizeof( rec.ctr ) ); 3369 rec.type = 42; 3370 rec.ver[0] = offset; 3371 rec.ver[1] = offset; 3372 rec.buf = buf; 3373 rec.buf_len = buflen; 3374#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 3375 rec.cid_len = 0; 3376#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 3377 3378 switch( mode ) 3379 { 3380 case 1: /* Space in the beginning */ 3381 rec.data_offset = offset; 3382 rec.data_len = buflen - offset - default_post_padding; 3383 break; 3384 3385 case 2: /* Space in the end */ 3386 rec.data_offset = default_pre_padding; 3387 rec.data_len = buflen - default_pre_padding - offset; 3388 break; 3389 3390 case 3: /* Space in the beginning and end */ 3391 rec.data_offset = offset; 3392 rec.data_len = buflen - 2 * offset; 3393 break; 3394 3395 default: 3396 TEST_ASSERT( 0 ); 3397 break; 3398 } 3399 3400 memset( rec.buf + rec.data_offset, 42, rec.data_len ); 3401 3402 /* Make a copy for later comparison */ 3403 rec_backup = rec; 3404 3405 /* Encrypt record */ 3406 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, 3407 mbedtls_test_rnd_std_rand, NULL ); 3408 3409 if( ( mode == 1 || mode == 2 ) && seen_success ) 3410 { 3411 TEST_ASSERT( ret == 0 ); 3412 } 3413 else 3414 { 3415 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 3416 if( ret == 0 ) 3417 seen_success = 1; 3418 } 3419 3420 if( ret != 0 ) 3421 continue; 3422 3423#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 3424 if( rec.cid_len != 0 ) 3425 { 3426 /* DTLS 1.2 + CID hides the real content type and 3427 * uses a special CID content type in the protected 3428 * record. Double-check this. */ 3429 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); 3430 } 3431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 3432 3433#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) 3434 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) 3435 { 3436 /* TLS 1.3 hides the real content type and 3437 * always uses Application Data as the content type 3438 * for protected records. Double-check this. */ 3439 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); 3440 } 3441#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 3442 3443 /* Decrypt record with t_dec */ 3444 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 ); 3445 3446 /* Compare results */ 3447 TEST_ASSERT( rec.type == rec_backup.type ); 3448 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); 3449 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); 3450 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); 3451 TEST_ASSERT( rec.data_len == rec_backup.data_len ); 3452 TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); 3453 TEST_ASSERT( memcmp( rec.buf + rec.data_offset, 3454 rec_backup.buf + rec_backup.data_offset, 3455 rec.data_len ) == 0 ); 3456 } 3457 3458 TEST_ASSERT( seen_success == 1 ); 3459 } 3460 3461exit: 3462 3463 /* Cleanup */ 3464 mbedtls_ssl_free( &ssl ); 3465 mbedtls_ssl_transform_free( &t0 ); 3466 mbedtls_ssl_transform_free( &t1 ); 3467 3468 mbedtls_free( buf ); 3469} 3470/* END_CASE */ 3471 3472/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */ 3473void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac, 3474 int length_selector ) 3475{ 3476 /* 3477 * Test record decryption for CBC without EtM, focused on the verification 3478 * of padding and MAC. 3479 * 3480 * Actually depends on TLS >= 1.0 (SSL 3.0 computes the MAC differently), 3481 * and either AES, ARIA, Camellia or DES, but since the test framework 3482 * doesn't support alternation in dependency statements, just depend on 3483 * TLS 1.2 and AES. 3484 * 3485 * The length_selector argument is interpreted as follows: 3486 * - if it's -1, the plaintext length is 0 and minimal padding is applied 3487 * - if it's -2, the plaintext length is 0 and maximal padding is applied 3488 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: 3489 * it's the length of the rest of the padding, that is, excluding the 3490 * byte that encodes the length. The minimal non-zero plaintext length 3491 * that gives this padding_length is automatically selected. 3492 */ 3493 mbedtls_ssl_context ssl; /* ONLY for debugging */ 3494 mbedtls_ssl_transform t0, t1; 3495 mbedtls_record rec, rec_save; 3496 unsigned char *buf = NULL, *buf_save = NULL; 3497 size_t buflen, olen = 0; 3498 size_t plaintext_len, block_size, i; 3499 unsigned char padlen; /* excluding the padding_length byte */ 3500 unsigned char add_data[13]; 3501 unsigned char mac[MBEDTLS_MD_MAX_SIZE]; 3502 int exp_ret; 3503 const unsigned char pad_max_len = 255; /* Per the standard */ 3504 3505 mbedtls_ssl_init( &ssl ); 3506 mbedtls_ssl_transform_init( &t0 ); 3507 mbedtls_ssl_transform_init( &t1 ); 3508 3509 /* Set up transforms with dummy keys */ 3510 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, 3511 0, trunc_hmac, 3512 MBEDTLS_SSL_MINOR_VERSION_3, 3513 0 , 0 ) == 0 ); 3514 3515 /* Determine padding/plaintext length */ 3516 TEST_ASSERT( length_selector >= -2 && length_selector <= 255 ); 3517 block_size = t0.ivlen; 3518 if( length_selector < 0 ) 3519 { 3520 plaintext_len = 0; 3521 3522 /* Minimal padding 3523 * The +1 is for the padding_length byte, not counted in padlen. */ 3524 padlen = block_size - ( t0.maclen + 1 ) % block_size; 3525 3526 /* Maximal padding? */ 3527 if( length_selector == -2 ) 3528 padlen += block_size * ( ( pad_max_len - padlen ) / block_size ); 3529 } 3530 else 3531 { 3532 padlen = length_selector; 3533 3534 /* Minimal non-zero plaintext_length giving desired padding. 3535 * The +1 is for the padding_length byte, not counted in padlen. */ 3536 plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size; 3537 } 3538 3539 /* Prepare a buffer for record data */ 3540 buflen = block_size 3541 + plaintext_len 3542 + t0.maclen 3543 + padlen + 1; 3544 ASSERT_ALLOC( buf, buflen ); 3545 ASSERT_ALLOC( buf_save, buflen ); 3546 3547 /* Prepare a dummy record header */ 3548 memset( rec.ctr, 0, sizeof( rec.ctr ) ); 3549 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; 3550 rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3; 3551 rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3; 3552#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) 3553 rec.cid_len = 0; 3554#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ 3555 3556 /* Prepare dummy record content */ 3557 rec.buf = buf; 3558 rec.buf_len = buflen; 3559 rec.data_offset = block_size; 3560 rec.data_len = plaintext_len; 3561 memset( rec.buf + rec.data_offset, 42, rec.data_len ); 3562 3563 /* Serialized version of record header for MAC purposes */ 3564 memcpy( add_data, rec.ctr, 8 ); 3565 add_data[8] = rec.type; 3566 add_data[9] = rec.ver[0]; 3567 add_data[10] = rec.ver[1]; 3568 add_data[11] = ( rec.data_len >> 8 ) & 0xff; 3569 add_data[12] = ( rec.data_len >> 0 ) & 0xff; 3570 3571 /* Set dummy IV */ 3572 memset( t0.iv_enc, 0x55, t0.ivlen ); 3573 memcpy( rec.buf, t0.iv_enc, t0.ivlen ); 3574 3575 /* 3576 * Prepare a pre-encryption record (with MAC and padding), and save it. 3577 */ 3578 3579 /* MAC with additional data */ 3580 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) ); 3581 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, 3582 rec.buf + rec.data_offset, 3583 rec.data_len ) ); 3584 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) ); 3585 3586 memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen ); 3587 rec.data_len += t0.maclen; 3588 3589 /* Pad */ 3590 memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 ); 3591 rec.data_len += padlen + 1; 3592 3593 /* Save correct pre-encryption record */ 3594 rec_save = rec; 3595 rec_save.buf = buf_save; 3596 memcpy( buf_save, buf, buflen ); 3597 3598 /* 3599 * Encrypt and decrypt the correct record, expecting success 3600 */ 3601 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc, 3602 t0.iv_enc, t0.ivlen, 3603 rec.buf + rec.data_offset, rec.data_len, 3604 rec.buf + rec.data_offset, &olen ) ); 3605 rec.data_offset -= t0.ivlen; 3606 rec.data_len += t0.ivlen; 3607 3608 TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); 3609 3610 /* 3611 * Modify each byte of the pre-encryption record before encrypting and 3612 * decrypting it, expecting failure every time. 3613 */ 3614 for( i = block_size; i < buflen; i++ ) 3615 { 3616 mbedtls_test_set_step( i ); 3617 3618 /* Restore correct pre-encryption record */ 3619 rec = rec_save; 3620 rec.buf = buf; 3621 memcpy( buf, buf_save, buflen ); 3622 3623 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */ 3624 rec.buf[i] ^= 0x01; 3625 3626 /* Encrypt */ 3627 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc, 3628 t0.iv_enc, t0.ivlen, 3629 rec.buf + rec.data_offset, rec.data_len, 3630 rec.buf + rec.data_offset, &olen ) ); 3631 rec.data_offset -= t0.ivlen; 3632 rec.data_len += t0.ivlen; 3633 3634 /* Decrypt and expect failure */ 3635 TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC, 3636 mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); 3637 } 3638 3639 /* 3640 * Use larger values of the padding bytes - with small buffers, this tests 3641 * the case where the announced padlen would be larger than the buffer 3642 * (and before that, than the buffer minus the size of the MAC), to make 3643 * sure our padding checking code does not perform any out-of-bounds reads 3644 * in this case. (With larger buffers, ie when the plaintext is long or 3645 * maximal length padding is used, this is less relevant but still doesn't 3646 * hurt to test.) 3647 * 3648 * (Start the loop with correct padding, just to double-check that record 3649 * saving did work, and that we're overwriting the correct bytes.) 3650 */ 3651 for( i = padlen; i <= pad_max_len; i++ ) 3652 { 3653 mbedtls_test_set_step( i ); 3654 3655 /* Restore correct pre-encryption record */ 3656 rec = rec_save; 3657 rec.buf = buf; 3658 memcpy( buf, buf_save, buflen ); 3659 3660 /* Set padding bytes to new value */ 3661 memset( buf + buflen - padlen - 1, i, padlen + 1 ); 3662 3663 /* Encrypt */ 3664 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc, 3665 t0.iv_enc, t0.ivlen, 3666 rec.buf + rec.data_offset, rec.data_len, 3667 rec.buf + rec.data_offset, &olen ) ); 3668 rec.data_offset -= t0.ivlen; 3669 rec.data_len += t0.ivlen; 3670 3671 /* Decrypt and expect failure except the first time */ 3672 exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; 3673 TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); 3674 } 3675 3676exit: 3677 mbedtls_ssl_free( &ssl ); 3678 mbedtls_ssl_transform_free( &t0 ); 3679 mbedtls_ssl_transform_free( &t1 ); 3680 mbedtls_free( buf ); 3681 mbedtls_free( buf_save ); 3682} 3683/* END_CASE */ 3684 3685/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 3686void ssl_tls1_3_hkdf_expand_label( int hash_alg, 3687 data_t *secret, 3688 int label_idx, 3689 data_t *ctx, 3690 int desired_length, 3691 data_t *expected ) 3692{ 3693 unsigned char dst[ 100 ]; 3694 3695 unsigned char const *lbl = NULL; 3696 size_t lbl_len; 3697#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ 3698 if( label_idx == (int) tls1_3_label_ ## name ) \ 3699 { \ 3700 lbl = mbedtls_ssl_tls1_3_labels.name; \ 3701 lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \ 3702 } 3703MBEDTLS_SSL_TLS1_3_LABEL_LIST 3704#undef MBEDTLS_SSL_TLS1_3_LABEL 3705 TEST_ASSERT( lbl != NULL ); 3706 3707 /* Check sanity of test parameters. */ 3708 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); 3709 TEST_ASSERT( (size_t) desired_length == expected->len ); 3710 3711 TEST_ASSERT( mbedtls_ssl_tls1_3_hkdf_expand_label( 3712 (mbedtls_md_type_t) hash_alg, 3713 secret->x, secret->len, 3714 lbl, lbl_len, 3715 ctx->x, ctx->len, 3716 dst, desired_length ) == 0 ); 3717 3718 ASSERT_COMPARE( dst, (size_t) desired_length, 3719 expected->x, (size_t) expected->len ); 3720} 3721/* END_CASE */ 3722 3723/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 3724void ssl_tls1_3_traffic_key_generation( int hash_alg, 3725 data_t *server_secret, 3726 data_t *client_secret, 3727 int desired_iv_len, 3728 int desired_key_len, 3729 data_t *expected_server_write_key, 3730 data_t *expected_server_write_iv, 3731 data_t *expected_client_write_key, 3732 data_t *expected_client_write_iv ) 3733{ 3734 mbedtls_ssl_key_set keys; 3735 3736 /* Check sanity of test parameters. */ 3737 TEST_ASSERT( client_secret->len == server_secret->len ); 3738 TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len && 3739 expected_client_write_iv->len == (size_t) desired_iv_len ); 3740 TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len && 3741 expected_client_write_key->len == (size_t) desired_key_len ); 3742 3743 TEST_ASSERT( mbedtls_ssl_tls1_3_make_traffic_keys( 3744 (mbedtls_md_type_t) hash_alg, 3745 client_secret->x, 3746 server_secret->x, 3747 client_secret->len /* == server_secret->len */, 3748 desired_key_len, desired_iv_len, 3749 &keys ) == 0 ); 3750 3751 ASSERT_COMPARE( keys.client_write_key, 3752 keys.key_len, 3753 expected_client_write_key->x, 3754 (size_t) desired_key_len ); 3755 ASSERT_COMPARE( keys.server_write_key, 3756 keys.key_len, 3757 expected_server_write_key->x, 3758 (size_t) desired_key_len ); 3759 ASSERT_COMPARE( keys.client_write_iv, 3760 keys.iv_len, 3761 expected_client_write_iv->x, 3762 (size_t) desired_iv_len ); 3763 ASSERT_COMPARE( keys.server_write_iv, 3764 keys.iv_len, 3765 expected_server_write_iv->x, 3766 (size_t) desired_iv_len ); 3767} 3768/* END_CASE */ 3769 3770/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 3771void ssl_tls1_3_derive_secret( int hash_alg, 3772 data_t *secret, 3773 int label_idx, 3774 data_t *ctx, 3775 int desired_length, 3776 int already_hashed, 3777 data_t *expected ) 3778{ 3779 unsigned char dst[ 100 ]; 3780 3781 unsigned char const *lbl = NULL; 3782 size_t lbl_len; 3783#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ 3784 if( label_idx == (int) tls1_3_label_ ## name ) \ 3785 { \ 3786 lbl = mbedtls_ssl_tls1_3_labels.name; \ 3787 lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \ 3788 } 3789MBEDTLS_SSL_TLS1_3_LABEL_LIST 3790#undef MBEDTLS_SSL_TLS1_3_LABEL 3791 TEST_ASSERT( lbl != NULL ); 3792 3793 /* Check sanity of test parameters. */ 3794 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); 3795 TEST_ASSERT( (size_t) desired_length == expected->len ); 3796 3797 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_secret( 3798 (mbedtls_md_type_t) hash_alg, 3799 secret->x, secret->len, 3800 lbl, lbl_len, 3801 ctx->x, ctx->len, 3802 already_hashed, 3803 dst, desired_length ) == 0 ); 3804 3805 ASSERT_COMPARE( dst, desired_length, 3806 expected->x, desired_length ); 3807} 3808/* END_CASE */ 3809 3810/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ 3811void ssl_tls1_3_key_evolution( int hash_alg, 3812 data_t *secret, 3813 data_t *input, 3814 data_t *expected ) 3815{ 3816 unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ]; 3817 3818 TEST_ASSERT( mbedtls_ssl_tls1_3_evolve_secret( 3819 (mbedtls_md_type_t) hash_alg, 3820 secret->len ? secret->x : NULL, 3821 input->len ? input->x : NULL, input->len, 3822 secret_new ) == 0 ); 3823 3824 ASSERT_COMPARE( secret_new, (size_t) expected->len, 3825 expected->x, (size_t) expected->len ); 3826} 3827/* END_CASE */ 3828 3829/* BEGIN_CASE */ 3830void ssl_tls_prf( int type, data_t * secret, data_t * random, 3831 char *label, data_t *result_str, int exp_ret ) 3832{ 3833 unsigned char *output; 3834 3835 output = mbedtls_calloc( 1, result_str->len ); 3836 if( output == NULL ) 3837 goto exit; 3838 3839 USE_PSA_INIT( ); 3840 3841 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len, 3842 label, random->x, random->len, 3843 output, result_str->len ) == exp_ret ); 3844 3845 if( exp_ret == 0 ) 3846 { 3847 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, 3848 result_str->len, result_str->len ) == 0 ); 3849 } 3850exit: 3851 3852 mbedtls_free( output ); 3853 USE_PSA_DONE( ); 3854} 3855/* END_CASE */ 3856 3857/* BEGIN_CASE */ 3858void ssl_serialize_session_save_load( int ticket_len, char *crt_file ) 3859{ 3860 mbedtls_ssl_session original, restored; 3861 unsigned char *buf = NULL; 3862 size_t len; 3863 3864 /* 3865 * Test that a save-load pair is the identity 3866 */ 3867 3868 mbedtls_ssl_session_init( &original ); 3869 mbedtls_ssl_session_init( &restored ); 3870 3871 /* Prepare a dummy session to work on */ 3872 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 ); 3873 3874 /* Serialize it */ 3875 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len ) 3876 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 3877 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL ); 3878 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len ) 3879 == 0 ); 3880 3881 /* Restore session from serialized data */ 3882 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 ); 3883 3884 /* 3885 * Make sure both session structures are identical 3886 */ 3887#if defined(MBEDTLS_HAVE_TIME) 3888 TEST_ASSERT( original.start == restored.start ); 3889#endif 3890 TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); 3891 TEST_ASSERT( original.compression == restored.compression ); 3892 TEST_ASSERT( original.id_len == restored.id_len ); 3893 TEST_ASSERT( memcmp( original.id, 3894 restored.id, sizeof( original.id ) ) == 0 ); 3895 TEST_ASSERT( memcmp( original.master, 3896 restored.master, sizeof( original.master ) ) == 0 ); 3897 3898#if defined(MBEDTLS_X509_CRT_PARSE_C) 3899#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) 3900 TEST_ASSERT( ( original.peer_cert == NULL ) == 3901 ( restored.peer_cert == NULL ) ); 3902 if( original.peer_cert != NULL ) 3903 { 3904 TEST_ASSERT( original.peer_cert->raw.len == 3905 restored.peer_cert->raw.len ); 3906 TEST_ASSERT( memcmp( original.peer_cert->raw.p, 3907 restored.peer_cert->raw.p, 3908 original.peer_cert->raw.len ) == 0 ); 3909 } 3910#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ 3911 TEST_ASSERT( original.peer_cert_digest_type == 3912 restored.peer_cert_digest_type ); 3913 TEST_ASSERT( original.peer_cert_digest_len == 3914 restored.peer_cert_digest_len ); 3915 TEST_ASSERT( ( original.peer_cert_digest == NULL ) == 3916 ( restored.peer_cert_digest == NULL ) ); 3917 if( original.peer_cert_digest != NULL ) 3918 { 3919 TEST_ASSERT( memcmp( original.peer_cert_digest, 3920 restored.peer_cert_digest, 3921 original.peer_cert_digest_len ) == 0 ); 3922 } 3923#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ 3924#endif /* MBEDTLS_X509_CRT_PARSE_C */ 3925 TEST_ASSERT( original.verify_result == restored.verify_result ); 3926 3927#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) 3928 TEST_ASSERT( original.ticket_len == restored.ticket_len ); 3929 if( original.ticket_len != 0 ) 3930 { 3931 TEST_ASSERT( original.ticket != NULL ); 3932 TEST_ASSERT( restored.ticket != NULL ); 3933 TEST_ASSERT( memcmp( original.ticket, 3934 restored.ticket, original.ticket_len ) == 0 ); 3935 } 3936 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime ); 3937#endif 3938 3939#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) 3940 TEST_ASSERT( original.mfl_code == restored.mfl_code ); 3941#endif 3942 3943#if defined(MBEDTLS_SSL_TRUNCATED_HMAC) 3944 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac ); 3945#endif 3946 3947#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) 3948 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac ); 3949#endif 3950 3951exit: 3952 mbedtls_ssl_session_free( &original ); 3953 mbedtls_ssl_session_free( &restored ); 3954 mbedtls_free( buf ); 3955} 3956/* END_CASE */ 3957 3958/* BEGIN_CASE */ 3959void ssl_serialize_session_load_save( int ticket_len, char *crt_file ) 3960{ 3961 mbedtls_ssl_session session; 3962 unsigned char *buf1 = NULL, *buf2 = NULL; 3963 size_t len0, len1, len2; 3964 3965 /* 3966 * Test that a load-save pair is the identity 3967 */ 3968 3969 mbedtls_ssl_session_init( &session ); 3970 3971 /* Prepare a dummy session to work on */ 3972 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 ); 3973 3974 /* Get desired buffer size for serializing */ 3975 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 ) 3976 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 3977 3978 /* Allocate first buffer */ 3979 buf1 = mbedtls_calloc( 1, len0 ); 3980 TEST_ASSERT( buf1 != NULL ); 3981 3982 /* Serialize to buffer and free live session */ 3983 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 ) 3984 == 0 ); 3985 TEST_ASSERT( len0 == len1 ); 3986 mbedtls_ssl_session_free( &session ); 3987 3988 /* Restore session from serialized data */ 3989 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 ); 3990 3991 /* Allocate second buffer and serialize to it */ 3992 buf2 = mbedtls_calloc( 1, len0 ); 3993 TEST_ASSERT( buf2 != NULL ); 3994 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 ) 3995 == 0 ); 3996 3997 /* Make sure both serialized versions are identical */ 3998 TEST_ASSERT( len1 == len2 ); 3999 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 ); 4000 4001exit: 4002 mbedtls_ssl_session_free( &session ); 4003 mbedtls_free( buf1 ); 4004 mbedtls_free( buf2 ); 4005} 4006/* END_CASE */ 4007 4008/* BEGIN_CASE */ 4009void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file ) 4010{ 4011 mbedtls_ssl_session session; 4012 unsigned char *buf = NULL; 4013 size_t good_len, bad_len, test_len; 4014 4015 /* 4016 * Test that session_save() fails cleanly on small buffers 4017 */ 4018 4019 mbedtls_ssl_session_init( &session ); 4020 4021 /* Prepare dummy session and get serialized size */ 4022 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 ); 4023 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) 4024 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 4025 4026 /* Try all possible bad lengths */ 4027 for( bad_len = 1; bad_len < good_len; bad_len++ ) 4028 { 4029 /* Allocate exact size so that asan/valgrind can detect any overwrite */ 4030 mbedtls_free( buf ); 4031 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL ); 4032 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len, 4033 &test_len ) 4034 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 4035 TEST_ASSERT( test_len == good_len ); 4036 } 4037 4038exit: 4039 mbedtls_ssl_session_free( &session ); 4040 mbedtls_free( buf ); 4041} 4042/* END_CASE */ 4043 4044/* BEGIN_CASE */ 4045void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file ) 4046{ 4047 mbedtls_ssl_session session; 4048 unsigned char *good_buf = NULL, *bad_buf = NULL; 4049 size_t good_len, bad_len; 4050 4051 /* 4052 * Test that session_load() fails cleanly on small buffers 4053 */ 4054 4055 mbedtls_ssl_session_init( &session ); 4056 4057 /* Prepare serialized session data */ 4058 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 ); 4059 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) 4060 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 4061 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL ); 4062 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len, 4063 &good_len ) == 0 ); 4064 mbedtls_ssl_session_free( &session ); 4065 4066 /* Try all possible bad lengths */ 4067 for( bad_len = 0; bad_len < good_len; bad_len++ ) 4068 { 4069 /* Allocate exact size so that asan/valgrind can detect any overread */ 4070 mbedtls_free( bad_buf ); 4071 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 ); 4072 TEST_ASSERT( bad_buf != NULL ); 4073 memcpy( bad_buf, good_buf, bad_len ); 4074 4075 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len ) 4076 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 4077 } 4078 4079exit: 4080 mbedtls_ssl_session_free( &session ); 4081 mbedtls_free( good_buf ); 4082 mbedtls_free( bad_buf ); 4083} 4084/* END_CASE */ 4085 4086/* BEGIN_CASE */ 4087void ssl_session_serialize_version_check( int corrupt_major, 4088 int corrupt_minor, 4089 int corrupt_patch, 4090 int corrupt_config ) 4091{ 4092 unsigned char serialized_session[ 2048 ]; 4093 size_t serialized_session_len; 4094 unsigned cur_byte; 4095 mbedtls_ssl_session session; 4096 uint8_t should_corrupt_byte[] = { corrupt_major == 1, 4097 corrupt_minor == 1, 4098 corrupt_patch == 1, 4099 corrupt_config == 1, 4100 corrupt_config == 1 }; 4101 4102 mbedtls_ssl_session_init( &session ); 4103 4104 /* Infer length of serialized session. */ 4105 TEST_ASSERT( mbedtls_ssl_session_save( &session, 4106 serialized_session, 4107 sizeof( serialized_session ), 4108 &serialized_session_len ) == 0 ); 4109 4110 mbedtls_ssl_session_free( &session ); 4111 4112 /* Without any modification, we should be able to successfully 4113 * de-serialize the session - double-check that. */ 4114 TEST_ASSERT( mbedtls_ssl_session_load( &session, 4115 serialized_session, 4116 serialized_session_len ) == 0 ); 4117 mbedtls_ssl_session_free( &session ); 4118 4119 /* Go through the bytes in the serialized session header and 4120 * corrupt them bit-by-bit. */ 4121 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ ) 4122 { 4123 int cur_bit; 4124 unsigned char * const byte = &serialized_session[ cur_byte ]; 4125 4126 if( should_corrupt_byte[ cur_byte ] == 0 ) 4127 continue; 4128 4129 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ ) 4130 { 4131 unsigned char const corrupted_bit = 0x1u << cur_bit; 4132 /* Modify a single bit in the serialized session. */ 4133 *byte ^= corrupted_bit; 4134 4135 /* Attempt to deserialize */ 4136 TEST_ASSERT( mbedtls_ssl_session_load( &session, 4137 serialized_session, 4138 serialized_session_len ) == 4139 MBEDTLS_ERR_SSL_VERSION_MISMATCH ); 4140 4141 /* Undo the change */ 4142 *byte ^= corrupted_bit; 4143 } 4144 } 4145 4146} 4147/* END_CASE */ 4148 4149/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4150void mbedtls_endpoint_sanity( int endpoint_type ) 4151{ 4152 enum { BUFFSIZE = 1024 }; 4153 mbedtls_endpoint ep; 4154 int ret = -1; 4155 4156 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA, 4157 NULL, NULL, NULL ); 4158 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); 4159 4160 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA ); 4161 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); 4162 4163 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA, 4164 NULL, NULL, NULL ); 4165 TEST_ASSERT( ret == 0 ); 4166 4167exit: 4168 mbedtls_endpoint_free( &ep, NULL ); 4169} 4170/* END_CASE */ 4171 4172/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4173void move_handshake_to_state(int endpoint_type, int state, int need_pass) 4174{ 4175 enum { BUFFSIZE = 1024 }; 4176 mbedtls_endpoint base_ep, second_ep; 4177 int ret = -1; 4178 4179 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA, 4180 NULL, NULL, NULL ); 4181 TEST_ASSERT( ret == 0 ); 4182 4183 ret = mbedtls_endpoint_init( &second_ep, 4184 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? 4185 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, 4186 MBEDTLS_PK_RSA, NULL, NULL, NULL ); 4187 TEST_ASSERT( ret == 0 ); 4188 4189 ret = mbedtls_mock_socket_connect( &(base_ep.socket), 4190 &(second_ep.socket), 4191 BUFFSIZE ); 4192 TEST_ASSERT( ret == 0 ); 4193 4194 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl), 4195 &(second_ep.ssl), 4196 state ); 4197 if( need_pass ) 4198 { 4199 TEST_ASSERT( ret == 0 ); 4200 TEST_ASSERT( base_ep.ssl.state == state ); 4201 } 4202 else 4203 { 4204 TEST_ASSERT( ret != 0 ); 4205 TEST_ASSERT( base_ep.ssl.state != state ); 4206 } 4207 4208exit: 4209 mbedtls_endpoint_free( &base_ep, NULL ); 4210 mbedtls_endpoint_free( &second_ep, NULL ); 4211} 4212/* END_CASE */ 4213 4214/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4215void handshake_version( int dtls, int client_min_version, int client_max_version, 4216 int server_min_version, int server_max_version, 4217 int expected_negotiated_version ) 4218{ 4219 handshake_test_options options; 4220 init_handshake_options( &options ); 4221 4222 options.client_min_version = client_min_version; 4223 options.client_max_version = client_max_version; 4224 options.server_min_version = server_min_version; 4225 options.server_max_version = server_max_version; 4226 4227 options.expected_negotiated_version = expected_negotiated_version; 4228 4229 options.dtls = dtls; 4230 /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so 4231 * the number of fragments will be twice as big. */ 4232 if( expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 || 4233 expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1 ) 4234 { 4235 options.expected_cli_fragments = 2; 4236 options.expected_srv_fragments = 2; 4237 } 4238 perform_handshake( &options ); 4239 4240 /* The goto below is used to avoid an "unused label" warning.*/ 4241 goto exit; 4242} 4243/* END_CASE */ 4244 4245/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4246void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls ) 4247{ 4248 handshake_test_options options; 4249 init_handshake_options( &options ); 4250 4251 options.cipher = cipher; 4252 options.dtls = dtls; 4253 options.psk_str = psk_str; 4254 options.pk_alg = pk_alg; 4255 4256 perform_handshake( &options ); 4257 4258 /* The goto below is used to avoid an "unused label" warning.*/ 4259 goto exit; 4260} 4261/* END_CASE */ 4262 4263/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4264void handshake_cipher( char* cipher, int pk_alg, int dtls ) 4265{ 4266 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls ); 4267 4268 /* The goto below is used to avoid an "unused label" warning.*/ 4269 goto exit; 4270} 4271/* END_CASE */ 4272 4273/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4274void app_data( int mfl, int cli_msg_len, int srv_msg_len, 4275 int expected_cli_fragments, 4276 int expected_srv_fragments, int dtls ) 4277{ 4278 handshake_test_options options; 4279 init_handshake_options( &options ); 4280 4281 options.mfl = mfl; 4282 options.cli_msg_len = cli_msg_len; 4283 options.srv_msg_len = srv_msg_len; 4284 options.expected_cli_fragments = expected_cli_fragments; 4285 options.expected_srv_fragments = expected_srv_fragments; 4286 options.dtls = dtls; 4287 4288 perform_handshake( &options ); 4289 /* The goto below is used to avoid an "unused label" warning.*/ 4290 goto exit; 4291} 4292/* END_CASE */ 4293 4294/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4295void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len, 4296 int expected_cli_fragments, 4297 int expected_srv_fragments ) 4298{ 4299 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, 4300 expected_srv_fragments, 0 ); 4301 /* The goto below is used to avoid an "unused label" warning.*/ 4302 goto exit; 4303} 4304/* END_CASE */ 4305 4306/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4307void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len, 4308 int expected_cli_fragments, 4309 int expected_srv_fragments ) 4310{ 4311 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, 4312 expected_srv_fragments, 1 ); 4313 /* The goto below is used to avoid an "unused label" warning.*/ 4314 goto exit; 4315} 4316/* END_CASE */ 4317 4318/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4319void handshake_serialization( ) 4320{ 4321 handshake_test_options options; 4322 init_handshake_options( &options ); 4323 4324 options.serialize = 1; 4325 options.dtls = 1; 4326 perform_handshake( &options ); 4327 /* The goto below is used to avoid an "unused label" warning.*/ 4328 goto exit; 4329} 4330/* END_CASE */ 4331 4332/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4333void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation) 4334{ 4335 handshake_test_options options; 4336 log_pattern srv_pattern, cli_pattern; 4337 4338 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; 4339 srv_pattern.counter = 0; 4340 cli_pattern.counter = 0; 4341 4342 init_handshake_options( &options ); 4343 options.dtls = 1; 4344 options.mfl = mfl; 4345 /* Set cipher to one using CBC so that record splitting can be tested */ 4346 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; 4347 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; 4348 options.srv_log_obj = &srv_pattern; 4349 options.cli_log_obj = &cli_pattern; 4350 options.srv_log_fun = log_analyzer; 4351 options.cli_log_fun = log_analyzer; 4352 4353 perform_handshake( &options ); 4354 4355 /* Test if the server received a fragmented handshake */ 4356 if( expected_srv_hs_fragmentation ) 4357 { 4358 TEST_ASSERT( srv_pattern.counter >= 1 ); 4359 } 4360 /* Test if the client received a fragmented handshake */ 4361 if( expected_cli_hs_fragmentation ) 4362 { 4363 TEST_ASSERT( cli_pattern.counter >= 1 ); 4364 } 4365} 4366/* END_CASE */ 4367 4368/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4369void renegotiation( int legacy_renegotiation ) 4370{ 4371 handshake_test_options options; 4372 init_handshake_options( &options ); 4373 4374 options.renegotiate = 1; 4375 options.legacy_renegotiation = legacy_renegotiation; 4376 options.dtls = 1; 4377 4378 perform_handshake( &options ); 4379 /* The goto below is used to avoid an "unused label" warning.*/ 4380 goto exit; 4381} 4382/* END_CASE */ 4383 4384/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4385void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation, 4386 int serialize, int dtls, char *cipher ) 4387{ 4388 handshake_test_options options; 4389 init_handshake_options( &options ); 4390 4391 options.mfl = mfl; 4392 options.cipher = cipher; 4393 options.renegotiate = renegotiation; 4394 options.legacy_renegotiation = legacy_renegotiation; 4395 options.serialize = serialize; 4396 options.dtls = dtls; 4397 options.resize_buffers = 1; 4398 4399 perform_handshake( &options ); 4400 /* The goto below is used to avoid an "unused label" warning.*/ 4401 goto exit; 4402} 4403/* END_CASE */ 4404 4405/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4406void resize_buffers_serialize_mfl( int mfl ) 4407{ 4408 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, 4409 (char *) "" ); 4410 4411 /* The goto below is used to avoid an "unused label" warning.*/ 4412 goto exit; 4413} 4414/* END_CASE */ 4415 4416/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ 4417void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation, 4418 char *cipher ) 4419{ 4420 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher ); 4421 4422 /* The goto below is used to avoid an "unused label" warning.*/ 4423 goto exit; 4424} 4425/* END_CASE */ 4426 4427/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */ 4428void ssl_cf_hmac( int hash ) 4429{ 4430 /* 4431 * Test the function mbedtls_ct_hmac() against a reference 4432 * implementation. 4433 */ 4434 mbedtls_md_context_t ctx, ref_ctx; 4435 const mbedtls_md_info_t *md_info; 4436 size_t out_len, block_size; 4437 size_t min_in_len, in_len, max_in_len, i; 4438 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */ 4439 unsigned char add_data[13]; 4440 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE]; 4441 unsigned char *data = NULL; 4442 unsigned char *out = NULL; 4443 unsigned char rec_num = 0; 4444 4445 mbedtls_md_init( &ctx ); 4446 mbedtls_md_init( &ref_ctx ); 4447 4448 md_info = mbedtls_md_info_from_type( hash ); 4449 TEST_ASSERT( md_info != NULL ); 4450 out_len = mbedtls_md_get_size( md_info ); 4451 TEST_ASSERT( out_len != 0 ); 4452 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64; 4453 4454 /* Use allocated out buffer to catch overwrites */ 4455 ASSERT_ALLOC( out, out_len ); 4456 4457 /* Set up contexts with the given hash and a dummy key */ 4458 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) ); 4459 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) ); 4460 memset( ref_out, 42, sizeof( ref_out ) ); 4461 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) ); 4462 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) ); 4463 memset( ref_out, 0, sizeof( ref_out ) ); 4464 4465 /* 4466 * Test all possible lengths up to a point. The difference between 4467 * max_in_len and min_in_len is at most 255, and make sure they both vary 4468 * by at least one block size. 4469 */ 4470 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ ) 4471 { 4472 mbedtls_test_set_step( max_in_len * 10000 ); 4473 4474 /* Use allocated in buffer to catch overreads */ 4475 ASSERT_ALLOC( data, max_in_len ); 4476 4477 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0; 4478 for( in_len = min_in_len; in_len <= max_in_len; in_len++ ) 4479 { 4480 mbedtls_test_set_step( max_in_len * 10000 + in_len ); 4481 4482 /* Set up dummy data and add_data */ 4483 rec_num++; 4484 memset( add_data, rec_num, sizeof( add_data ) ); 4485 for( i = 0; i < in_len; i++ ) 4486 data[i] = ( i & 0xff ) ^ rec_num; 4487 4488 /* Get the function's result */ 4489 TEST_CF_SECRET( &in_len, sizeof( in_len ) ); 4490 TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ), 4491 data, in_len, 4492 min_in_len, max_in_len, 4493 out ) ); 4494 TEST_CF_PUBLIC( &in_len, sizeof( in_len ) ); 4495 TEST_CF_PUBLIC( out, out_len ); 4496 4497 /* Compute the reference result */ 4498 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data, 4499 sizeof( add_data ) ) ); 4500 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) ); 4501 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) ); 4502 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) ); 4503 4504 /* Compare */ 4505 ASSERT_COMPARE( out, out_len, ref_out, out_len ); 4506 } 4507 4508 mbedtls_free( data ); 4509 data = NULL; 4510 } 4511 4512exit: 4513 mbedtls_md_free( &ref_ctx ); 4514 mbedtls_md_free( &ctx ); 4515 4516 mbedtls_free( data ); 4517 mbedtls_free( out ); 4518} 4519/* END_CASE */ 4520 4521/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */ 4522void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len ) 4523{ 4524 unsigned char *dst = NULL; 4525 unsigned char *src = NULL; 4526 size_t src_len = offset_max + len; 4527 size_t secret; 4528 4529 ASSERT_ALLOC( dst, len ); 4530 ASSERT_ALLOC( src, src_len ); 4531 4532 /* Fill src in a way that we can detect if we copied the right bytes */ 4533 mbedtls_test_rnd_std_rand( NULL, src, src_len ); 4534 4535 for( secret = offset_min; secret <= (size_t) offset_max; secret++ ) 4536 { 4537 mbedtls_test_set_step( (int) secret ); 4538 4539 TEST_CF_SECRET( &secret, sizeof( secret ) ); 4540 mbedtls_ct_memcpy_offset( dst, src, secret, 4541 offset_min, offset_max, len ); 4542 TEST_CF_PUBLIC( &secret, sizeof( secret ) ); 4543 TEST_CF_PUBLIC( dst, len ); 4544 4545 ASSERT_COMPARE( dst, len, src + secret, len ); 4546 } 4547 4548exit: 4549 mbedtls_free( dst ); 4550 mbedtls_free( src ); 4551} 4552/* END_CASE */ 4553