Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include <mbedtls/ssl.h> |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 3 | #include <mbedtls/ssl_internal.h> |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4 | #include <mbedtls/ctr_drbg.h> |
| 5 | #include <mbedtls/entropy.h> |
| 6 | #include <mbedtls/certs.h> |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * Buffer structure for custom I/O callbacks. |
| 10 | */ |
| 11 | |
| 12 | typedef struct mbedtls_test_buffer |
| 13 | { |
| 14 | size_t start; |
| 15 | size_t content_length; |
| 16 | size_t capacity; |
| 17 | unsigned char *buffer; |
| 18 | } mbedtls_test_buffer; |
| 19 | |
| 20 | /* |
| 21 | * Initialises \p buf. After calling this function it is safe to call |
| 22 | * `mbedtls_test_buffer_free()` on \p buf. |
| 23 | */ |
| 24 | void mbedtls_test_buffer_init( mbedtls_test_buffer *buf ) |
| 25 | { |
| 26 | memset( buf, 0, sizeof( *buf ) ); |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Sets up \p buf. After calling this function it is safe to call |
| 31 | * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf. |
| 32 | */ |
| 33 | int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity ) |
| 34 | { |
| 35 | buf->buffer = (unsigned char*) mbedtls_calloc( capacity, |
| 36 | sizeof(unsigned char) ); |
| 37 | if( NULL == buf->buffer ) |
| 38 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 39 | buf->capacity = capacity; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | void mbedtls_test_buffer_free( mbedtls_test_buffer *buf ) |
| 45 | { |
| 46 | if( buf->buffer != NULL ) |
| 47 | mbedtls_free( buf->buffer ); |
| 48 | |
| 49 | memset( buf, 0, sizeof( *buf ) ); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 54 | * |
| 55 | * \p buf must have been initialized and set up by calling |
| 56 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 57 | * |
| 58 | * \retval \p input_len, if the data fits. |
| 59 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 60 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 61 | * zero and \p input is NULL. |
| 62 | */ |
| 63 | int mbedtls_test_buffer_put( mbedtls_test_buffer *buf, |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 64 | const unsigned char *input, size_t input_len ) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 65 | { |
| 66 | size_t overflow = 0; |
| 67 | |
| 68 | if( ( buf == NULL ) || ( buf->buffer == NULL ) ) |
| 69 | return -1; |
| 70 | |
| 71 | /* Reduce input_len to a number that fits in the buffer. */ |
| 72 | if ( ( buf->content_length + input_len ) > buf->capacity ) |
| 73 | { |
| 74 | input_len = buf->capacity - buf->content_length; |
| 75 | } |
| 76 | |
| 77 | if( input == NULL ) |
| 78 | { |
| 79 | return ( input_len == 0 ) ? 0 : -1; |
| 80 | } |
| 81 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 82 | /* Check if the buffer has not come full circle and free space is not in |
| 83 | * the middle */ |
| 84 | if( buf->start + buf->content_length < buf->capacity ) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 85 | { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 86 | |
| 87 | /* Calculate the number of bytes that need to be placed at lower memory |
| 88 | * address */ |
| 89 | if( buf->start + buf->content_length + input_len |
| 90 | > buf->capacity ) |
| 91 | { |
| 92 | overflow = ( buf->start + buf->content_length + input_len ) |
| 93 | % buf->capacity; |
| 94 | } |
| 95 | |
| 96 | memcpy( buf->buffer + buf->start + buf->content_length, input, |
| 97 | input_len - overflow ); |
| 98 | memcpy( buf->buffer, input + input_len - overflow, overflow ); |
| 99 | |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | /* The buffer has come full circle and free space is in the middle */ |
| 104 | memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 105 | input, input_len ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 108 | buf->content_length += input_len; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 109 | return input_len; |
| 110 | } |
| 111 | |
| 112 | /* |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 113 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 114 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 115 | * ring buffer will be dropped, if the requested length is available. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 116 | * |
| 117 | * \p buf must have been initialized and set up by calling |
| 118 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 119 | * |
| 120 | * \retval \p output_len, if the data is available. |
| 121 | * \retval 0 <= value < \p output_len, if the data is not available. |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 122 | * \retval -1, if \buf is NULL or it hasn't been set up. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 123 | */ |
| 124 | int mbedtls_test_buffer_get( mbedtls_test_buffer *buf, |
| 125 | unsigned char* output, size_t output_len ) |
| 126 | { |
| 127 | size_t overflow = 0; |
| 128 | |
| 129 | if( ( buf == NULL ) || ( buf->buffer == NULL ) ) |
| 130 | return -1; |
| 131 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 132 | if( output == NULL && output_len == 0 ) |
| 133 | return 0; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 134 | |
| 135 | if( buf->content_length < output_len ) |
| 136 | output_len = buf->content_length; |
| 137 | |
| 138 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 139 | * address */ |
| 140 | if( buf->start + output_len > buf->capacity ) |
| 141 | { |
| 142 | overflow = ( buf->start + output_len ) % buf->capacity; |
| 143 | } |
| 144 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 145 | if( output != NULL ) |
| 146 | { |
| 147 | memcpy( output, buf->buffer + buf->start, output_len - overflow ); |
| 148 | memcpy( output + output_len - overflow, buf->buffer, overflow ); |
| 149 | } |
| 150 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 151 | buf->content_length -= output_len; |
| 152 | buf->start = ( buf->start + output_len ) % buf->capacity; |
| 153 | |
| 154 | return output_len; |
| 155 | } |
| 156 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 157 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 158 | * Errors used in the message transport mock tests |
| 159 | */ |
| 160 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
| 161 | #define MBEDTLS_TEST_ERROR_QUEUE_FULL -22 |
| 162 | #define MBEDTLS_TEST_ERROR_QUEUE_EMPTY -33 |
| 163 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 164 | |
| 165 | /* |
| 166 | * Context for a message metadata queue (fifo) that is on top of the ring buffer. |
| 167 | */ |
| 168 | typedef struct mbedtls_test_message_queue |
| 169 | { |
| 170 | size_t *messages; |
| 171 | int pos; |
| 172 | int num; |
| 173 | int capacity; |
| 174 | } mbedtls_test_message_queue; |
| 175 | |
| 176 | /* |
| 177 | * Setup and free functions for the message metadata queue. |
| 178 | * |
| 179 | * \p capacity describes the number of message metadata chunks that can be held |
| 180 | * within the queue. |
| 181 | * |
| 182 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 183 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 184 | */ |
| 185 | int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue, |
| 186 | size_t capacity ) |
| 187 | { |
| 188 | queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) ); |
| 189 | if( NULL == queue->messages ) |
| 190 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 191 | |
| 192 | queue->capacity = capacity; |
| 193 | queue->pos = 0; |
| 194 | queue->num = 0; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue ) |
| 200 | { |
| 201 | if( queue == NULL ) |
| 202 | return; |
| 203 | |
| 204 | if( queue->messages != NULL ) |
| 205 | mbedtls_free( queue->messages ); |
| 206 | |
| 207 | memset( queue, 0, sizeof( *queue ) ); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Push message length information onto the message metadata queue. |
| 212 | * This will become the last element to leave it (fifo). |
| 213 | * |
| 214 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
| 215 | * \retval MBEDTLS_TEST_ERROR_QUEUE_FULL, if the queue is full. |
| 216 | * \retval \p len, if the push was successful. |
| 217 | */ |
| 218 | int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue, |
| 219 | size_t len ) |
| 220 | { |
| 221 | int place; |
| 222 | if( queue == NULL ) |
| 223 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 224 | |
| 225 | if( queue->num >= queue->capacity ) |
| 226 | return MBEDTLS_TEST_ERROR_QUEUE_FULL; |
| 227 | |
| 228 | place = ( queue->pos + queue->num ) % queue->capacity; |
| 229 | queue->messages[place] = len; |
| 230 | queue->num++; |
| 231 | return len; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Pop information about the next message length from the queue. This will be |
| 236 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 237 | * case the data will be popped from the queue but not copied anywhere. |
| 238 | * |
| 239 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
| 240 | * \retval MBEDTLS_TEST_ERROR_QUEUE_EMPTY, if the queue is empty. |
| 241 | * \retval message length, if the pop was successful, up to the given |
| 242 | \p buf_len. |
| 243 | */ |
| 244 | int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue, |
| 245 | size_t buf_len ) |
| 246 | { |
| 247 | size_t message_length; |
| 248 | if( queue == NULL ) |
| 249 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 250 | if( queue->num == 0 ) |
| 251 | return MBEDTLS_TEST_ERROR_QUEUE_EMPTY; |
| 252 | |
| 253 | message_length = queue->messages[queue->pos]; |
| 254 | queue->messages[queue->pos] = 0; |
| 255 | queue->num--; |
| 256 | queue->pos++; |
| 257 | queue->pos %= queue->capacity; |
| 258 | if( queue->pos < 0 ) |
| 259 | queue->pos += queue->capacity; |
| 260 | |
| 261 | return ( message_length > buf_len ) ? buf_len : message_length; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Take a peek on the info about the next message length from the queue. |
| 266 | * This will be the oldest inserted message length(fifo). |
| 267 | * |
| 268 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
| 269 | * \retval MBEDTLS_TEST_ERROR_QUEUE_EMPTY, if the queue is empty. |
| 270 | * \retval 0, if the peek was successful. |
| 271 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 272 | * too small to fit the message. In this case the \p msg_len will be |
| 273 | * set to the full message length so that the |
| 274 | * caller knows what portion of the message can be dropped. |
| 275 | */ |
| 276 | int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue, |
| 277 | size_t buf_len, size_t* msg_len ) |
| 278 | { |
| 279 | if( queue == NULL || msg_len == NULL ) |
| 280 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 281 | if( queue->num == 0 ) |
| 282 | return MBEDTLS_TEST_ERROR_QUEUE_EMPTY; |
| 283 | |
| 284 | *msg_len = queue->messages[queue->pos]; |
| 285 | return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
| 286 | } |
| 287 | /* |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 288 | * Context for the I/O callbacks simulating network connection. |
| 289 | */ |
| 290 | |
| 291 | #define MBEDTLS_MOCK_SOCKET_CONNECTED 1 |
| 292 | |
| 293 | typedef struct mbedtls_mock_socket |
| 294 | { |
| 295 | int status; |
| 296 | mbedtls_test_buffer *input; |
| 297 | mbedtls_test_buffer *output; |
| 298 | struct mbedtls_mock_socket *peer; |
| 299 | } mbedtls_mock_socket; |
| 300 | |
| 301 | /* |
| 302 | * Setup and teardown functions for mock sockets. |
| 303 | */ |
| 304 | void mbedtls_mock_socket_init( mbedtls_mock_socket *socket ) |
| 305 | { |
| 306 | memset( socket, 0, sizeof( *socket ) ); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Closes the socket \p socket. |
| 311 | * |
| 312 | * \p socket must have been previously initialized by calling |
| 313 | * mbedtls_mock_socket_init(). |
| 314 | * |
| 315 | * This function frees all allocated resources and both sockets are aware of the |
| 316 | * new connection state. |
| 317 | * |
| 318 | * That is, this function does not simulate half-open TCP connections and the |
| 319 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 320 | * connection having been closed. |
| 321 | */ |
| 322 | void mbedtls_mock_socket_close( mbedtls_mock_socket* socket ) |
| 323 | { |
| 324 | if( socket == NULL ) |
| 325 | return; |
| 326 | |
| 327 | if( socket->input != NULL ) |
| 328 | { |
| 329 | mbedtls_test_buffer_free( socket->input ); |
| 330 | mbedtls_free( socket->input ); |
| 331 | } |
| 332 | |
| 333 | if( socket->output != NULL ) |
| 334 | { |
| 335 | mbedtls_test_buffer_free( socket->output ); |
| 336 | mbedtls_free( socket->output ); |
| 337 | } |
| 338 | |
| 339 | if( socket->peer != NULL ) |
| 340 | memset( socket->peer, 0, sizeof( *socket->peer ) ); |
| 341 | |
| 342 | memset( socket, 0, sizeof( *socket ) ); |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Establishes a connection between \p peer1 and \p peer2. |
| 347 | * |
| 348 | * \p peer1 and \p peer2 must have been previously initialized by calling |
| 349 | * mbedtls_mock_socket_init(). |
| 350 | * |
| 351 | * The capacites of the internal buffers are set to \p bufsize. Setting this to |
| 352 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 353 | * implementation and mocking TCP connections with lower memory cost. |
| 354 | */ |
| 355 | int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1, |
| 356 | mbedtls_mock_socket* peer2, |
| 357 | size_t bufsize ) |
| 358 | { |
| 359 | int ret = -1; |
| 360 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 361 | peer1->output = |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 362 | (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) ); |
| 363 | if( peer1->output == NULL ) |
| 364 | { |
| 365 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 366 | goto exit; |
| 367 | } |
| 368 | mbedtls_test_buffer_init( peer1->output ); |
| 369 | if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) ) |
| 370 | { |
| 371 | goto exit; |
| 372 | } |
| 373 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 374 | peer2->output = |
| 375 | (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) ); |
| 376 | if( peer2->output == NULL ) |
| 377 | { |
| 378 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 379 | goto exit; |
| 380 | } |
| 381 | mbedtls_test_buffer_init( peer2->output ); |
| 382 | if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) ) |
| 383 | { |
| 384 | goto exit; |
| 385 | } |
| 386 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 387 | peer1->peer = peer2; |
| 388 | peer2->peer = peer1; |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 389 | peer1->input = peer2->output; |
| 390 | peer2->input = peer1->output; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 391 | |
| 392 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 393 | ret = 0; |
| 394 | |
| 395 | exit: |
| 396 | |
| 397 | if( ret != 0 ) |
| 398 | { |
| 399 | mbedtls_mock_socket_close( peer1 ); |
| 400 | mbedtls_mock_socket_close( peer2 ); |
| 401 | } |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 408 | */ |
| 409 | |
| 410 | int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len ) |
| 411 | { |
| 412 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 413 | |
| 414 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 415 | return -1; |
| 416 | |
| 417 | return mbedtls_test_buffer_put( socket->output, buf, len ); |
| 418 | } |
| 419 | |
| 420 | int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len ) |
| 421 | { |
| 422 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 423 | |
| 424 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 425 | return -1; |
| 426 | |
| 427 | return mbedtls_test_buffer_get( socket->input, buf, len ); |
| 428 | } |
| 429 | |
| 430 | /* |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 431 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 432 | */ |
| 433 | |
| 434 | int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len ) |
| 435 | { |
| 436 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 437 | |
| 438 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 439 | return -1; |
| 440 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 441 | if( socket->output->capacity == socket->output->content_length ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 442 | { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 443 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 444 | } |
| 445 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 446 | return mbedtls_test_buffer_put( socket->output, buf, len ); |
| 447 | } |
| 448 | |
| 449 | int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len ) |
| 450 | { |
| 451 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 452 | |
| 453 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 454 | return -1; |
| 455 | |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 456 | if( socket->input->content_length == 0 ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 457 | { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 458 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 459 | } |
| 460 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 461 | return mbedtls_test_buffer_get( socket->input, buf, len ); |
| 462 | } |
| 463 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 464 | /* Errors used in the message socket mocks */ |
| 465 | |
| 466 | #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 |
| 467 | #define MBEDTLS_TEST_ERROR_SEND_FAILED -66 |
| 468 | #define MBEDTLS_TEST_ERROR_RECV_FAILED -77 |
| 469 | |
| 470 | /* |
| 471 | * Structure used as an addon, or a wrapper, around the mocked sockets. |
| 472 | * Contains an input queue, to which the other socket pushes metadata, |
| 473 | * and an output queue, to which this one pushes metadata. This context is |
| 474 | * considered as an owner of the input queue only, which is initialized and |
| 475 | * freed in the respective setup and free calls. |
| 476 | */ |
| 477 | typedef struct mbedtls_test_message_socket_context |
| 478 | { |
| 479 | mbedtls_test_message_queue* queue_input; |
| 480 | mbedtls_test_message_queue* queue_output; |
| 481 | mbedtls_mock_socket* socket; |
| 482 | } mbedtls_test_message_socket_context; |
| 483 | |
| 484 | /* |
| 485 | * Setup a given mesasge socket context including initialization of |
| 486 | * input/output queues to a chosen capacity of messages. Also set the |
| 487 | * corresponding mock socket. |
| 488 | * |
| 489 | * \retval 0, if everything succeeds. |
| 490 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 491 | * queue failed. |
| 492 | */ |
| 493 | int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input, |
| 494 | mbedtls_test_message_queue* queue_output, |
| 495 | size_t queue_capacity, |
| 496 | mbedtls_mock_socket* socket, |
| 497 | mbedtls_test_message_socket_context* ctx ) |
| 498 | { |
| 499 | int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity ); |
| 500 | if( ret != 0 ) |
| 501 | return ret; |
| 502 | ctx->queue_input = queue_input; |
| 503 | ctx->queue_output = queue_output; |
| 504 | ctx->socket = socket; |
| 505 | mbedtls_mock_socket_init( socket ); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Close a given message socket context, along with the socket itself. Free the |
| 512 | * memory allocated by the input queue. |
| 513 | */ |
| 514 | void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx ) |
| 515 | { |
| 516 | if( ctx == NULL ) |
| 517 | return; |
| 518 | |
| 519 | mbedtls_test_message_queue_free( ctx->queue_input ); |
| 520 | mbedtls_mock_socket_close( ctx->socket ); |
| 521 | memset( ctx, 0, sizeof( *ctx ) ); |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Send one message through a given message socket context. |
| 526 | * |
| 527 | * \retval \p len, if everything succeeds. |
| 528 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 529 | * elements or the context itself is null. |
| 530 | * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed. |
| 531 | * \retval MBEDTLS_TEST_ERROR_QUEUE_FULL, if the output queue is full. |
| 532 | * |
| 533 | * This function will also return any error from |
| 534 | * mbedtls_test_message_queue_push_info. |
| 535 | */ |
| 536 | int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len ) |
| 537 | { |
| 538 | mbedtls_test_message_queue* queue; |
| 539 | mbedtls_mock_socket* socket; |
| 540 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx; |
| 541 | |
| 542 | if( context == NULL || context->socket == NULL |
| 543 | || context->queue_output == NULL ) |
| 544 | { |
| 545 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 546 | } |
| 547 | |
| 548 | queue = context->queue_output; |
| 549 | socket = context->socket; |
| 550 | |
| 551 | if( queue->num >= queue->capacity ) |
| 552 | return MBEDTLS_TEST_ERROR_QUEUE_FULL; |
| 553 | |
| 554 | if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len ) |
| 555 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
| 556 | |
| 557 | return mbedtls_test_message_queue_push_info( queue, len ); |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Receive one message from a given message socket context and return message |
| 562 | * length or an error. |
| 563 | * |
| 564 | * \retval message length, if everything succeeds. |
| 565 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 566 | * elements or the context itself is null. |
| 567 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed. |
| 568 | * |
| 569 | * This function will also return any error other than |
| 570 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info. |
| 571 | */ |
| 572 | int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len ) |
| 573 | { |
| 574 | mbedtls_test_message_queue* queue; |
| 575 | mbedtls_mock_socket* socket; |
| 576 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx; |
| 577 | size_t drop_len; |
| 578 | size_t msg_len; |
| 579 | int ret; |
| 580 | |
| 581 | if( context == NULL || context->socket == NULL |
| 582 | || context->queue_input == NULL ) |
| 583 | { |
| 584 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 585 | } |
| 586 | |
| 587 | queue = context->queue_input; |
| 588 | socket = context->socket; |
| 589 | |
| 590 | /* Peek first, so that in case of a socket error the data remains in |
| 591 | * the queue. */ |
| 592 | ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len ); |
| 593 | if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED ) |
| 594 | { |
| 595 | /* Calculate how much to drop */ |
| 596 | drop_len = msg_len - buf_len; |
| 597 | |
| 598 | /* Set the requested message len to be buffer length */ |
| 599 | msg_len = buf_len; |
| 600 | } else if( ret != 0 ) |
| 601 | { |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len ) |
| 606 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
| 607 | |
| 608 | if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED ) |
| 609 | { |
| 610 | /* Drop the remaining part of the message */ |
| 611 | if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len ) |
| 612 | { |
| 613 | /* Inconsistent state - part of the message was read, |
| 614 | * and a part couldn't. Not much we can do here, but it should not |
| 615 | * happen in test environment, unless forced manually. */ |
| 616 | } |
| 617 | } |
| 618 | mbedtls_test_message_queue_pop_info( queue, buf_len ); |
| 619 | |
| 620 | return msg_len; |
| 621 | } |
| 622 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 623 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 624 | |
| 625 | /* |
| 626 | * Structure with endpoint's certificates for SSL communication tests. |
| 627 | */ |
| 628 | typedef struct mbedtls_endpoint_certificate |
| 629 | { |
| 630 | mbedtls_x509_crt ca_cert; |
| 631 | mbedtls_x509_crt cert; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 632 | mbedtls_pk_context pkey; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 633 | } mbedtls_endpoint_certificate; |
| 634 | |
| 635 | /* |
| 636 | * Endpoint structure for SSL communication tests. |
| 637 | */ |
| 638 | typedef struct mbedtls_endpoint |
| 639 | { |
| 640 | const char *name; |
| 641 | mbedtls_ssl_context ssl; |
| 642 | mbedtls_ssl_config conf; |
| 643 | mbedtls_ctr_drbg_context ctr_drbg; |
| 644 | mbedtls_entropy_context entropy; |
| 645 | mbedtls_mock_socket socket; |
| 646 | mbedtls_endpoint_certificate cert; |
| 647 | } mbedtls_endpoint; |
| 648 | |
| 649 | /* |
| 650 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 651 | * represented by \p ep. |
| 652 | * |
| 653 | * \retval 0 on success, otherwise error code. |
| 654 | */ |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 655 | int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 656 | { |
| 657 | int i = 0; |
| 658 | int ret = -1; |
| 659 | mbedtls_endpoint_certificate *cert; |
| 660 | |
| 661 | if( ep == NULL ) |
| 662 | { |
| 663 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 664 | } |
| 665 | |
| 666 | cert = &( ep->cert ); |
| 667 | mbedtls_x509_crt_init( &( cert->ca_cert ) ); |
| 668 | mbedtls_x509_crt_init( &( cert->cert ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 669 | mbedtls_pk_init( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 670 | |
| 671 | /* Load the trusted CA */ |
| 672 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 673 | for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ ) |
| 674 | { |
| 675 | ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ), |
| 676 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 677 | mbedtls_test_cas_der_len[i] ); |
| 678 | TEST_ASSERT( ret == 0 ); |
| 679 | } |
| 680 | |
| 681 | /* Load own certificate and private key */ |
| 682 | |
| 683 | if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 684 | { |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 685 | if( pk_alg == MBEDTLS_PK_RSA ) |
| 686 | { |
| 687 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 688 | (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der, |
| 689 | mbedtls_test_srv_crt_rsa_sha256_der_len ); |
| 690 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 691 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 692 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 693 | (const unsigned char*) mbedtls_test_srv_key_rsa_der, |
| 694 | mbedtls_test_srv_key_rsa_der_len, NULL, 0 ); |
| 695 | TEST_ASSERT( ret == 0 ); |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 700 | (const unsigned char*) mbedtls_test_srv_crt_ec_der, |
| 701 | mbedtls_test_srv_crt_ec_der_len ); |
| 702 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 703 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 704 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 705 | (const unsigned char*) mbedtls_test_srv_key_ec_der, |
| 706 | mbedtls_test_srv_key_ec_der_len, NULL, 0 ); |
| 707 | TEST_ASSERT( ret == 0 ); |
| 708 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 709 | } |
| 710 | else |
| 711 | { |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 712 | if( pk_alg == MBEDTLS_PK_RSA ) |
| 713 | { |
| 714 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 715 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 716 | mbedtls_test_cli_crt_rsa_der_len ); |
| 717 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 718 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 719 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 720 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 721 | mbedtls_test_cli_key_rsa_der_len, NULL, 0 ); |
| 722 | TEST_ASSERT( ret == 0 ); |
| 723 | } |
| 724 | else |
| 725 | { |
| 726 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 727 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 728 | mbedtls_test_cli_crt_ec_len ); |
| 729 | TEST_ASSERT( ret == 0 ); |
| 730 | |
| 731 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 732 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 733 | mbedtls_test_cli_key_ec_der_len, NULL, 0 ); |
| 734 | TEST_ASSERT( ret == 0 ); |
| 735 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL ); |
| 739 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 740 | ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ), |
| 741 | &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 742 | TEST_ASSERT( ret == 0 ); |
| 743 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 744 | exit: |
| 745 | if( ret != 0 ) |
| 746 | { |
| 747 | mbedtls_x509_crt_free( &( cert->ca_cert ) ); |
| 748 | mbedtls_x509_crt_free( &( cert->cert ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 749 | mbedtls_pk_free( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()` |
| 757 | * after calling this function even if it fails. |
| 758 | * |
| 759 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 760 | * MBEDTLS_SSL_IS_CLIENT. |
| 761 | * |
| 762 | * \retval 0 on success, otherwise error code. |
| 763 | */ |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 764 | int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 765 | { |
| 766 | int ret = -1; |
| 767 | |
| 768 | if( ep == NULL ) |
| 769 | { |
| 770 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 771 | } |
| 772 | |
| 773 | memset( ep, 0, sizeof( *ep ) ); |
| 774 | |
| 775 | ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client"; |
| 776 | |
| 777 | mbedtls_ssl_init( &( ep->ssl ) ); |
| 778 | mbedtls_ssl_config_init( &( ep->conf ) ); |
| 779 | mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) ); |
| 780 | mbedtls_ssl_conf_rng( &( ep->conf ), |
| 781 | mbedtls_ctr_drbg_random, |
| 782 | &( ep->ctr_drbg ) ); |
| 783 | mbedtls_entropy_init( &( ep->entropy ) ); |
| 784 | mbedtls_mock_socket_init( &( ep->socket ) ); |
| 785 | |
| 786 | ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func, |
| 787 | &( ep->entropy ), (const unsigned char *) ( ep->name ), |
| 788 | strlen( ep->name ) ); |
| 789 | TEST_ASSERT( ret == 0 ); |
| 790 | |
| 791 | /* Non-blocking callbacks without timeout */ |
| 792 | mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ), |
| 793 | mbedtls_mock_tcp_send_nb, |
| 794 | mbedtls_mock_tcp_recv_nb, |
| 795 | NULL ); |
| 796 | |
| 797 | ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) ); |
| 798 | TEST_ASSERT( ret == 0 ); |
| 799 | |
| 800 | ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type, |
| 801 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 802 | MBEDTLS_SSL_PRESET_DEFAULT ); |
| 803 | TEST_ASSERT( ret == 0 ); |
| 804 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 805 | ret = mbedtls_endpoint_certificate_init( ep, pk_alg ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 806 | TEST_ASSERT( ret == 0 ); |
| 807 | |
| 808 | exit: |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Deinitializes certificates from endpoint represented by \p ep. |
| 814 | */ |
| 815 | void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep ) |
| 816 | { |
| 817 | mbedtls_endpoint_certificate *cert = &( ep->cert ); |
| 818 | mbedtls_x509_crt_free( &( cert->ca_cert ) ); |
| 819 | mbedtls_x509_crt_free( &( cert->cert ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 820 | mbedtls_pk_free( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Deinitializes endpoint represented by \p ep. |
| 825 | */ |
| 826 | void mbedtls_endpoint_free( mbedtls_endpoint *ep ) |
| 827 | { |
| 828 | mbedtls_endpoint_certificate_free( ep ); |
| 829 | |
| 830 | mbedtls_ssl_free( &( ep->ssl ) ); |
| 831 | mbedtls_ssl_config_free( &( ep->conf ) ); |
| 832 | mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) ); |
| 833 | mbedtls_entropy_free( &( ep->entropy ) ); |
| 834 | mbedtls_mock_socket_close( &( ep->socket ) ); |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 839 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 840 | * connected before calling this function. |
| 841 | * |
| 842 | * \retval 0 on success, otherwise error code. |
| 843 | */ |
| 844 | int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl, |
| 845 | mbedtls_ssl_context *second_ssl, |
| 846 | int state ) |
| 847 | { |
| 848 | enum { BUFFSIZE = 1024 }; |
| 849 | int max_steps = 1000; |
| 850 | int ret = 0; |
| 851 | |
| 852 | if( ssl == NULL || second_ssl == NULL ) |
| 853 | { |
| 854 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 855 | } |
| 856 | |
| 857 | /* Perform communication via connected sockets */ |
| 858 | while( ( ssl->state != state ) && ( --max_steps >= 0 ) ) |
| 859 | { |
| 860 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 861 | * there is no need to call the next step */ |
| 862 | if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 863 | { |
| 864 | ret = mbedtls_ssl_handshake_step( second_ssl ); |
| 865 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 866 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 867 | { |
| 868 | return ret; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* We only care about the \p ssl state and returns, so we call it last, |
| 873 | * to leave the iteration as soon as the state is as expected. */ |
| 874 | ret = mbedtls_ssl_handshake_step( ssl ); |
| 875 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 876 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 877 | { |
| 878 | return ret; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return ( max_steps >= 0 ) ? ret : -1; |
| 883 | } |
| 884 | |
| 885 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 886 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 887 | /* |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 888 | * Write application data. Then increase write and fragments counter |
| 889 | */ |
| 890 | int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 891 | int ln, int *writen, int *fragments ) |
| 892 | { |
| 893 | int ret = mbedtls_ssl_write( ssl, buf + *writen, ln - *writen ); |
| 894 | if( ret >= 0 ) |
| 895 | { |
| 896 | (*fragments)++; |
| 897 | *writen += ret; |
| 898 | } |
| 899 | return ret; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Read application data and increase read counter |
| 904 | */ |
| 905 | int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf, int ln, int *read ) |
| 906 | { |
| 907 | int ret = mbedtls_ssl_read( ssl, buf + *read, ln - *read ); |
| 908 | if( ret >= 0 ) |
| 909 | { |
| 910 | *read += ret; |
| 911 | } |
| 912 | return ret; |
| 913 | } |
| 914 | |
| 915 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 916 | * Helper function setting up inverse record transformations |
| 917 | * using given cipher, hash, EtM mode, authentication tag length, |
| 918 | * and version. |
| 919 | */ |
| 920 | |
| 921 | #define CHK( x ) \ |
| 922 | do \ |
| 923 | { \ |
| 924 | if( !( x ) ) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 925 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 926 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 927 | goto cleanup; \ |
| 928 | } \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 929 | } while( 0 ) |
| 930 | |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 931 | void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher, |
| 932 | int* forced_ciphersuite ) |
| 933 | { |
| 934 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 935 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher ); |
| 936 | forced_ciphersuite[1] = 0; |
| 937 | |
| 938 | ciphersuite_info = |
| 939 | mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] ); |
| 940 | |
| 941 | TEST_ASSERT( ciphersuite_info != NULL ); |
| 942 | TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver ); |
| 943 | TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver ); |
| 944 | |
| 945 | if( conf->max_minor_ver > ciphersuite_info->max_minor_ver ) |
| 946 | { |
| 947 | conf->max_minor_ver = ciphersuite_info->max_minor_ver; |
| 948 | } |
| 949 | if( conf->min_minor_ver < ciphersuite_info->min_minor_ver ) |
| 950 | { |
| 951 | conf->min_minor_ver = ciphersuite_info->min_minor_ver; |
| 952 | } |
| 953 | |
| 954 | mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite ); |
| 955 | |
| 956 | exit: |
| 957 | return; |
| 958 | } |
| 959 | |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 960 | int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl, |
| 961 | const unsigned char *name, size_t name_len ) |
| 962 | { |
| 963 | (void) p_info; |
| 964 | (void) ssl; |
| 965 | (void) name; |
| 966 | (void) name_len; |
| 967 | |
| 968 | return ( 0 ); |
| 969 | } |
| 970 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 971 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 972 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 973 | #else |
| 974 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 975 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 976 | |
| 977 | static int build_transforms( mbedtls_ssl_transform *t_in, |
| 978 | mbedtls_ssl_transform *t_out, |
| 979 | int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 980 | int etm, int tag_mode, int ver, |
| 981 | size_t cid0_len, |
| 982 | size_t cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 983 | { |
| 984 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 985 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 986 | |
| 987 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 988 | unsigned char *key0 = NULL, *key1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 989 | unsigned char iv_enc[16], iv_dec[16]; |
| 990 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 991 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 992 | unsigned char cid0[ SSL_CID_LEN_MIN ]; |
| 993 | unsigned char cid1[ SSL_CID_LEN_MIN ]; |
| 994 | |
| 995 | rnd_std_rand( NULL, cid0, sizeof( cid0 ) ); |
| 996 | rnd_std_rand( NULL, cid1, sizeof( cid1 ) ); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 997 | #else |
| 998 | ((void) cid0_len); |
| 999 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1000 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1001 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1002 | maclen = 0; |
| 1003 | |
| 1004 | /* Pick cipher */ |
| 1005 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
| 1006 | CHK( cipher_info != NULL ); |
| 1007 | CHK( cipher_info->iv_size <= 16 ); |
| 1008 | CHK( cipher_info->key_bitlen % 8 == 0 ); |
| 1009 | |
| 1010 | /* Pick keys */ |
| 1011 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1012 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1013 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1014 | * `keylen == 0` in the case of the NULL cipher. */ |
| 1015 | CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
| 1016 | CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1017 | memset( key0, 0x1, keylen ); |
| 1018 | memset( key1, 0x2, keylen ); |
| 1019 | |
| 1020 | /* Setup cipher contexts */ |
| 1021 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 ); |
| 1022 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 ); |
| 1023 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 ); |
| 1024 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 ); |
| 1025 | |
| 1026 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1027 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
| 1028 | { |
| 1029 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc, |
| 1030 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1031 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec, |
| 1032 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1033 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc, |
| 1034 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1035 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec, |
| 1036 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1037 | } |
| 1038 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1039 | |
| 1040 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0, |
| 1041 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 1042 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1, |
| 1043 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
| 1044 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1, |
| 1045 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 1046 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0, |
| 1047 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1048 | |
| 1049 | /* Setup MAC contexts */ |
| 1050 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1051 | if( cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1052 | cipher_info->mode == MBEDTLS_MODE_STREAM ) |
| 1053 | { |
| 1054 | mbedtls_md_info_t const *md_info; |
| 1055 | unsigned char *md0, *md1; |
| 1056 | |
| 1057 | /* Pick hash */ |
| 1058 | md_info = mbedtls_md_info_from_type( hash_id ); |
| 1059 | CHK( md_info != NULL ); |
| 1060 | |
| 1061 | /* Pick hash keys */ |
| 1062 | maclen = mbedtls_md_get_size( md_info ); |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1063 | CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
| 1064 | CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1065 | memset( md0, 0x5, maclen ); |
| 1066 | memset( md1, 0x6, maclen ); |
| 1067 | |
| 1068 | CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1069 | CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1070 | CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1071 | CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1072 | |
| 1073 | if( ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 1074 | { |
| 1075 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc, |
| 1076 | md0, maclen ) == 0 ); |
| 1077 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec, |
| 1078 | md1, maclen ) == 0 ); |
| 1079 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc, |
| 1080 | md1, maclen ) == 0 ); |
| 1081 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec, |
| 1082 | md0, maclen ) == 0 ); |
| 1083 | } |
| 1084 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1085 | else |
| 1086 | { |
| 1087 | memcpy( &t_in->mac_enc, md0, maclen ); |
| 1088 | memcpy( &t_in->mac_dec, md1, maclen ); |
| 1089 | memcpy( &t_out->mac_enc, md1, maclen ); |
| 1090 | memcpy( &t_out->mac_dec, md0, maclen ); |
| 1091 | } |
| 1092 | #endif |
| 1093 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1094 | mbedtls_free( md0 ); |
| 1095 | mbedtls_free( md1 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1096 | } |
| 1097 | #else |
| 1098 | ((void) hash_id); |
| 1099 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1100 | |
| 1101 | |
| 1102 | /* Pick IV's (regardless of whether they |
| 1103 | * are being used by the transform). */ |
| 1104 | ivlen = cipher_info->iv_size; |
| 1105 | memset( iv_enc, 0x3, sizeof( iv_enc ) ); |
| 1106 | memset( iv_dec, 0x4, sizeof( iv_dec ) ); |
| 1107 | |
| 1108 | /* |
| 1109 | * Setup transforms |
| 1110 | */ |
| 1111 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1112 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1113 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1114 | t_out->encrypt_then_mac = etm; |
| 1115 | t_in->encrypt_then_mac = etm; |
| 1116 | #else |
| 1117 | ((void) etm); |
| 1118 | #endif |
| 1119 | |
| 1120 | t_out->minor_ver = ver; |
| 1121 | t_in->minor_ver = ver; |
| 1122 | t_out->ivlen = ivlen; |
| 1123 | t_in->ivlen = ivlen; |
| 1124 | |
| 1125 | switch( cipher_info->mode ) |
| 1126 | { |
| 1127 | case MBEDTLS_MODE_GCM: |
| 1128 | case MBEDTLS_MODE_CCM: |
| 1129 | t_out->fixed_ivlen = 4; |
| 1130 | t_in->fixed_ivlen = 4; |
| 1131 | t_out->maclen = 0; |
| 1132 | t_in->maclen = 0; |
| 1133 | switch( tag_mode ) |
| 1134 | { |
| 1135 | case 0: /* Full tag */ |
| 1136 | t_out->taglen = 16; |
| 1137 | t_in->taglen = 16; |
| 1138 | break; |
| 1139 | case 1: /* Partial tag */ |
| 1140 | t_out->taglen = 8; |
| 1141 | t_in->taglen = 8; |
| 1142 | break; |
| 1143 | default: |
| 1144 | return( 1 ); |
| 1145 | } |
| 1146 | break; |
| 1147 | |
| 1148 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1149 | t_out->fixed_ivlen = 12; |
| 1150 | t_in->fixed_ivlen = 12; |
| 1151 | t_out->maclen = 0; |
| 1152 | t_in->maclen = 0; |
| 1153 | switch( tag_mode ) |
| 1154 | { |
| 1155 | case 0: /* Full tag */ |
| 1156 | t_out->taglen = 16; |
| 1157 | t_in->taglen = 16; |
| 1158 | break; |
| 1159 | case 1: /* Partial tag */ |
| 1160 | t_out->taglen = 8; |
| 1161 | t_in->taglen = 8; |
| 1162 | break; |
| 1163 | default: |
| 1164 | return( 1 ); |
| 1165 | } |
| 1166 | break; |
| 1167 | |
| 1168 | case MBEDTLS_MODE_STREAM: |
| 1169 | case MBEDTLS_MODE_CBC: |
| 1170 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1171 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1172 | t_out->taglen = 0; |
| 1173 | t_in->taglen = 0; |
| 1174 | switch( tag_mode ) |
| 1175 | { |
| 1176 | case 0: /* Full tag */ |
| 1177 | t_out->maclen = maclen; |
| 1178 | t_in->maclen = maclen; |
| 1179 | break; |
| 1180 | case 1: /* Partial tag */ |
| 1181 | t_out->maclen = 10; |
| 1182 | t_in->maclen = 10; |
| 1183 | break; |
| 1184 | default: |
| 1185 | return( 1 ); |
| 1186 | } |
| 1187 | break; |
| 1188 | default: |
| 1189 | return( 1 ); |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | /* Setup IV's */ |
| 1194 | |
| 1195 | memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) ); |
| 1196 | memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) ); |
| 1197 | memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) ); |
| 1198 | memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) ); |
| 1199 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1200 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1201 | /* Add CID */ |
| 1202 | memcpy( &t_in->in_cid, cid0, cid0_len ); |
| 1203 | memcpy( &t_in->out_cid, cid1, cid1_len ); |
| 1204 | t_in->in_cid_len = cid0_len; |
| 1205 | t_in->out_cid_len = cid1_len; |
| 1206 | memcpy( &t_out->in_cid, cid1, cid1_len ); |
| 1207 | memcpy( &t_out->out_cid, cid0, cid0_len ); |
| 1208 | t_out->in_cid_len = cid1_len; |
| 1209 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1210 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1211 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1212 | cleanup: |
| 1213 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1214 | mbedtls_free( key0 ); |
| 1215 | mbedtls_free( key1 ); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1216 | |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1217 | return( ret ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1220 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1221 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1222 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1223 | */ |
| 1224 | static int ssl_populate_session( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1225 | int ticket_len, |
| 1226 | const char *crt_file ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1227 | { |
| 1228 | #if defined(MBEDTLS_HAVE_TIME) |
| 1229 | session->start = mbedtls_time( NULL ) - 42; |
| 1230 | #endif |
| 1231 | session->ciphersuite = 0xabcd; |
| 1232 | session->compression = 1; |
| 1233 | session->id_len = sizeof( session->id ); |
| 1234 | memset( session->id, 66, session->id_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1235 | memset( session->master, 17, sizeof( session->master ) ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1236 | |
Manuel Pégourié-Gonnard | 1f6033a | 2019-05-24 10:17:52 +0200 | [diff] [blame] | 1237 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1238 | if( strlen( crt_file ) != 0 ) |
| 1239 | { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1240 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1241 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1242 | |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1243 | mbedtls_x509_crt_init( &tmp_crt ); |
| 1244 | ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file ); |
| 1245 | if( ret != 0 ) |
| 1246 | return( ret ); |
| 1247 | |
| 1248 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1249 | /* Move temporary CRT. */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1250 | session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) ); |
| 1251 | if( session->peer_cert == NULL ) |
| 1252 | return( -1 ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1253 | *session->peer_cert = tmp_crt; |
| 1254 | memset( &tmp_crt, 0, sizeof( tmp_crt ) ); |
| 1255 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1256 | /* Calculate digest of temporary CRT. */ |
| 1257 | session->peer_cert_digest = |
| 1258 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 1259 | if( session->peer_cert_digest == NULL ) |
| 1260 | return( -1 ); |
| 1261 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 1262 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 1263 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1264 | session->peer_cert_digest ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1265 | if( ret != 0 ) |
| 1266 | return( ret ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1267 | session->peer_cert_digest_type = |
| 1268 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1269 | session->peer_cert_digest_len = |
| 1270 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1271 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1272 | |
| 1273 | mbedtls_x509_crt_free( &tmp_crt ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1274 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1275 | #else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1276 | (void) crt_file; |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1277 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1278 | session->verify_result = 0xdeadbeef; |
| 1279 | |
| 1280 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 1281 | if( ticket_len != 0 ) |
| 1282 | { |
| 1283 | session->ticket = mbedtls_calloc( 1, ticket_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1284 | if( session->ticket == NULL ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1285 | return( -1 ); |
| 1286 | memset( session->ticket, 33, ticket_len ); |
| 1287 | } |
| 1288 | session->ticket_len = ticket_len; |
| 1289 | session->ticket_lifetime = 86401; |
| 1290 | #else |
| 1291 | (void) ticket_len; |
| 1292 | #endif |
| 1293 | |
| 1294 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1295 | session->mfl_code = 1; |
| 1296 | #endif |
| 1297 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1298 | session->trunc_hmac = 1; |
| 1299 | #endif |
| 1300 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1301 | session->encrypt_then_mac = 1; |
| 1302 | #endif |
| 1303 | |
| 1304 | return( 0 ); |
| 1305 | } |
| 1306 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1307 | /* END_HEADER */ |
| 1308 | |
| 1309 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1310 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1311 | * END_DEPENDENCIES |
| 1312 | */ |
| 1313 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1314 | /* BEGIN_CASE */ |
| 1315 | void test_callback_buffer_sanity() |
| 1316 | { |
| 1317 | enum { MSGLEN = 10 }; |
| 1318 | mbedtls_test_buffer buf; |
| 1319 | unsigned char input[MSGLEN]; |
| 1320 | unsigned char output[MSGLEN]; |
| 1321 | |
| 1322 | memset( input, 0, sizeof(input) ); |
| 1323 | |
| 1324 | /* Make sure calling put and get on NULL buffer results in error. */ |
| 1325 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) ) |
| 1326 | == -1 ); |
| 1327 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) ) |
| 1328 | == -1 ); |
| 1329 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1330 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1331 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 ); |
| 1332 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 ); |
| 1333 | |
| 1334 | /* Make sure calling put and get on a buffer that hasn't been set up results |
| 1335 | * in eror. */ |
| 1336 | mbedtls_test_buffer_init( &buf ); |
| 1337 | |
| 1338 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 ); |
| 1339 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) ) |
| 1340 | == -1 ); |
| 1341 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1342 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1343 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 ); |
| 1344 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 ); |
| 1345 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1346 | /* Make sure calling put and get on NULL input only results in |
| 1347 | * error if the length is not zero, and that a NULL output is valid for data |
| 1348 | * dropping. |
| 1349 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1350 | |
| 1351 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 ); |
| 1352 | |
| 1353 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
| 1354 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) ) |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1355 | == 0 ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1356 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 ); |
| 1357 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 ); |
| 1358 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 1359 | /* Make sure calling put several times in the row is safe */ |
| 1360 | |
| 1361 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) |
| 1362 | == sizeof( input ) ); |
| 1363 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 ); |
| 1364 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 ); |
| 1365 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 ); |
| 1366 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 ); |
| 1367 | |
| 1368 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1369 | exit: |
| 1370 | |
| 1371 | mbedtls_test_buffer_free( &buf ); |
| 1372 | } |
| 1373 | /* END_CASE */ |
| 1374 | |
| 1375 | /* |
| 1376 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 1377 | * correct and works as expected. |
| 1378 | * |
| 1379 | * That is |
| 1380 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 1381 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 1382 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 1383 | * bytes. |
| 1384 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 1385 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 1386 | */ |
| 1387 | |
| 1388 | /* BEGIN_CASE */ |
| 1389 | void test_callback_buffer( int size, int put1, int put1_ret, |
| 1390 | int get1, int get1_ret, int put2, int put2_ret, |
| 1391 | int get2, int get2_ret ) |
| 1392 | { |
| 1393 | enum { ROUNDS = 2 }; |
| 1394 | size_t put[ROUNDS]; |
| 1395 | int put_ret[ROUNDS]; |
| 1396 | size_t get[ROUNDS]; |
| 1397 | int get_ret[ROUNDS]; |
| 1398 | mbedtls_test_buffer buf; |
| 1399 | unsigned char* input = NULL; |
| 1400 | size_t input_len; |
| 1401 | unsigned char* output = NULL; |
| 1402 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1403 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1404 | |
| 1405 | mbedtls_test_buffer_init( &buf ); |
| 1406 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 ); |
| 1407 | |
| 1408 | /* Check the sanity of input parameters and initialise local variables. That |
| 1409 | * is, ensure that the amount of data is not negative and that we are not |
| 1410 | * expecting more to put or get than we actually asked for. */ |
| 1411 | TEST_ASSERT( put1 >= 0 ); |
| 1412 | put[0] = put1; |
| 1413 | put_ret[0] = put1_ret; |
| 1414 | TEST_ASSERT( put1_ret <= put1 ); |
| 1415 | TEST_ASSERT( put2 >= 0 ); |
| 1416 | put[1] = put2; |
| 1417 | put_ret[1] = put2_ret; |
| 1418 | TEST_ASSERT( put2_ret <= put2 ); |
| 1419 | |
| 1420 | TEST_ASSERT( get1 >= 0 ); |
| 1421 | get[0] = get1; |
| 1422 | get_ret[0] = get1_ret; |
| 1423 | TEST_ASSERT( get1_ret <= get1 ); |
| 1424 | TEST_ASSERT( get2 >= 0 ); |
| 1425 | get[1] = get2; |
| 1426 | get_ret[1] = get2_ret; |
| 1427 | TEST_ASSERT( get2_ret <= get2 ); |
| 1428 | |
| 1429 | input_len = 0; |
| 1430 | /* Calculate actual input and output lengths */ |
| 1431 | for( j = 0; j < ROUNDS; j++ ) |
| 1432 | { |
| 1433 | if( put_ret[j] > 0 ) |
| 1434 | { |
| 1435 | input_len += put_ret[j]; |
| 1436 | } |
| 1437 | } |
| 1438 | /* In order to always have a valid pointer we always allocate at least 1 |
| 1439 | * byte. */ |
| 1440 | if( input_len == 0 ) |
| 1441 | input_len = 1; |
| 1442 | ASSERT_ALLOC( input, input_len ); |
| 1443 | |
| 1444 | output_len = 0; |
| 1445 | for( j = 0; j < ROUNDS; j++ ) |
| 1446 | { |
| 1447 | if( get_ret[j] > 0 ) |
| 1448 | { |
| 1449 | output_len += get_ret[j]; |
| 1450 | } |
| 1451 | } |
| 1452 | TEST_ASSERT( output_len <= input_len ); |
| 1453 | /* In order to always have a valid pointer we always allocate at least 1 |
| 1454 | * byte. */ |
| 1455 | if( output_len == 0 ) |
| 1456 | output_len = 1; |
| 1457 | ASSERT_ALLOC( output, output_len ); |
| 1458 | |
| 1459 | /* Fill up the buffer with structured data so that unwanted changes |
| 1460 | * can be detected */ |
| 1461 | for( i = 0; i < input_len; i++ ) |
| 1462 | { |
| 1463 | input[i] = i & 0xFF; |
| 1464 | } |
| 1465 | |
| 1466 | written = read = 0; |
| 1467 | for( j = 0; j < ROUNDS; j++ ) |
| 1468 | { |
| 1469 | TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf, |
| 1470 | input + written, put[j] ) ); |
| 1471 | written += put_ret[j]; |
| 1472 | TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf, |
| 1473 | output + read, get[j] ) ); |
| 1474 | read += get_ret[j]; |
| 1475 | TEST_ASSERT( read <= written ); |
| 1476 | if( get_ret[j] > 0 ) |
| 1477 | { |
| 1478 | TEST_ASSERT( memcmp( output + read - get_ret[j], |
| 1479 | input + read - get_ret[j], get_ret[j] ) |
| 1480 | == 0 ); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | exit: |
| 1485 | |
| 1486 | mbedtls_free( input ); |
| 1487 | mbedtls_free( output ); |
| 1488 | mbedtls_test_buffer_free( &buf ); |
| 1489 | } |
| 1490 | /* END_CASE */ |
| 1491 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1492 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1493 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 1494 | * correct and works as expected on unconnected sockets. |
| 1495 | */ |
| 1496 | |
| 1497 | /* BEGIN_CASE */ |
| 1498 | void ssl_mock_sanity( ) |
| 1499 | { |
| 1500 | enum { MSGLEN = 105 }; |
| 1501 | unsigned char message[MSGLEN]; |
| 1502 | unsigned char received[MSGLEN]; |
| 1503 | mbedtls_mock_socket socket; |
| 1504 | |
| 1505 | mbedtls_mock_socket_init( &socket ); |
| 1506 | TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 ); |
| 1507 | mbedtls_mock_socket_close( &socket ); |
| 1508 | mbedtls_mock_socket_init( &socket ); |
| 1509 | TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 ); |
| 1510 | mbedtls_mock_socket_close( &socket ); |
| 1511 | |
| 1512 | mbedtls_mock_socket_init( &socket ); |
| 1513 | TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 ); |
| 1514 | mbedtls_mock_socket_close( &socket ); |
| 1515 | mbedtls_mock_socket_init( &socket ); |
| 1516 | TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 ); |
| 1517 | mbedtls_mock_socket_close( &socket ); |
| 1518 | |
| 1519 | exit: |
| 1520 | |
| 1521 | mbedtls_mock_socket_close( &socket ); |
| 1522 | } |
| 1523 | /* END_CASE */ |
| 1524 | |
| 1525 | /* |
| 1526 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 1527 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1528 | */ |
| 1529 | |
| 1530 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1531 | void ssl_mock_tcp( int blocking ) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1532 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1533 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1534 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1535 | unsigned char message[MSGLEN]; |
| 1536 | unsigned char received[MSGLEN]; |
| 1537 | mbedtls_mock_socket client; |
| 1538 | mbedtls_mock_socket server; |
| 1539 | size_t written, read; |
| 1540 | int send_ret, recv_ret; |
| 1541 | mbedtls_ssl_send_t *send; |
| 1542 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1543 | unsigned i; |
| 1544 | |
| 1545 | if( blocking == 0 ) |
| 1546 | { |
| 1547 | send = mbedtls_mock_tcp_send_nb; |
| 1548 | recv = mbedtls_mock_tcp_recv_nb; |
| 1549 | } |
| 1550 | else |
| 1551 | { |
| 1552 | send = mbedtls_mock_tcp_send_b; |
| 1553 | recv = mbedtls_mock_tcp_recv_b; |
| 1554 | } |
| 1555 | |
| 1556 | mbedtls_mock_socket_init( &client ); |
| 1557 | mbedtls_mock_socket_init( &server ); |
| 1558 | |
| 1559 | /* Fill up the buffer with structured data so that unwanted changes |
| 1560 | * can be detected */ |
| 1561 | for( i = 0; i < MSGLEN; i++ ) |
| 1562 | { |
| 1563 | message[i] = i & 0xFF; |
| 1564 | } |
| 1565 | |
| 1566 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1567 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1568 | |
| 1569 | /* Send the message to the server */ |
| 1570 | send_ret = recv_ret = 1; |
| 1571 | written = read = 0; |
| 1572 | while( send_ret != 0 || recv_ret != 0 ) |
| 1573 | { |
| 1574 | send_ret = send( &client, message + written, MSGLEN - written ); |
| 1575 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1576 | TEST_ASSERT( send_ret >= 0 ); |
| 1577 | TEST_ASSERT( send_ret <= BUFLEN ); |
| 1578 | written += send_ret; |
| 1579 | |
| 1580 | /* If the buffer is full we can test blocking and non-blocking send */ |
| 1581 | if ( send_ret == BUFLEN ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1582 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1583 | int blocking_ret = send( &client, message , 1 ); |
| 1584 | if ( blocking ) |
| 1585 | { |
| 1586 | TEST_ASSERT( blocking_ret == 0 ); |
| 1587 | } |
| 1588 | else |
| 1589 | { |
| 1590 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1591 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1592 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1593 | |
| 1594 | recv_ret = recv( &server, received + read, MSGLEN - read ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1595 | |
| 1596 | /* The result depends on whether any data was sent */ |
| 1597 | if ( send_ret > 0 ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1598 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1599 | TEST_ASSERT( recv_ret > 0 ); |
| 1600 | TEST_ASSERT( recv_ret <= BUFLEN ); |
| 1601 | read += recv_ret; |
| 1602 | } |
| 1603 | else if( blocking ) |
| 1604 | { |
| 1605 | TEST_ASSERT( recv_ret == 0 ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1606 | } |
| 1607 | else |
| 1608 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1609 | TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1610 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1611 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1612 | |
| 1613 | /* If the buffer is empty we can test blocking and non-blocking read */ |
| 1614 | if ( recv_ret == BUFLEN ) |
| 1615 | { |
| 1616 | int blocking_ret = recv( &server, received, 1 ); |
| 1617 | if ( blocking ) |
| 1618 | { |
| 1619 | TEST_ASSERT( blocking_ret == 0 ); |
| 1620 | } |
| 1621 | else |
| 1622 | { |
| 1623 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1624 | } |
| 1625 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1626 | } |
| 1627 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 1628 | |
| 1629 | exit: |
| 1630 | |
| 1631 | mbedtls_mock_socket_close( &client ); |
| 1632 | mbedtls_mock_socket_close( &server ); |
| 1633 | } |
| 1634 | /* END_CASE */ |
| 1635 | |
| 1636 | /* |
| 1637 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 1638 | * send messages in both direction at the same time (with the I/O calls |
| 1639 | * interleaving). |
| 1640 | */ |
| 1641 | |
| 1642 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1643 | void ssl_mock_tcp_interleaving( int blocking ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1644 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1645 | enum { ROUNDS = 2 }; |
| 1646 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1647 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1648 | unsigned char message[ROUNDS][MSGLEN]; |
| 1649 | unsigned char received[ROUNDS][MSGLEN]; |
| 1650 | mbedtls_mock_socket client; |
| 1651 | mbedtls_mock_socket server; |
| 1652 | size_t written[ROUNDS]; |
| 1653 | size_t read[ROUNDS]; |
| 1654 | int send_ret[ROUNDS]; |
| 1655 | int recv_ret[ROUNDS]; |
| 1656 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1657 | mbedtls_ssl_send_t *send; |
| 1658 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1659 | |
| 1660 | if( blocking == 0 ) |
| 1661 | { |
| 1662 | send = mbedtls_mock_tcp_send_nb; |
| 1663 | recv = mbedtls_mock_tcp_recv_nb; |
| 1664 | } |
| 1665 | else |
| 1666 | { |
| 1667 | send = mbedtls_mock_tcp_send_b; |
| 1668 | recv = mbedtls_mock_tcp_recv_b; |
| 1669 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1670 | |
| 1671 | mbedtls_mock_socket_init( &client ); |
| 1672 | mbedtls_mock_socket_init( &server ); |
| 1673 | |
| 1674 | /* Fill up the buffers with structured data so that unwanted changes |
| 1675 | * can be detected */ |
| 1676 | for( i = 0; i < ROUNDS; i++ ) |
| 1677 | { |
| 1678 | for( j = 0; j < MSGLEN; j++ ) |
| 1679 | { |
| 1680 | message[i][j] = ( i * MSGLEN + j ) & 0xFF; |
| 1681 | } |
| 1682 | } |
| 1683 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1684 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1685 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1686 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1687 | /* Send the message from both sides, interleaving. */ |
| 1688 | progress = 1; |
| 1689 | for( i = 0; i < ROUNDS; i++ ) |
| 1690 | { |
| 1691 | written[i] = 0; |
| 1692 | read[i] = 0; |
| 1693 | } |
| 1694 | /* This loop does not stop as long as there was a successful write or read |
| 1695 | * of at least one byte on either side. */ |
| 1696 | while( progress != 0 ) |
| 1697 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1698 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1699 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1700 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1701 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1702 | /* First sending is from the client */ |
| 1703 | socket = ( i % 2 == 0 ) ? ( &client ) : ( &server ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1704 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1705 | send_ret[i] = send( socket, message[i] + written[i], |
| 1706 | MSGLEN - written[i] ); |
| 1707 | TEST_ASSERT( send_ret[i] >= 0 ); |
| 1708 | TEST_ASSERT( send_ret[i] <= BUFLEN ); |
| 1709 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1710 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1711 | /* If the buffer is full we can test blocking and non-blocking |
| 1712 | * send */ |
| 1713 | if ( send_ret[i] == BUFLEN ) |
| 1714 | { |
| 1715 | int blocking_ret = send( socket, message[i] , 1 ); |
| 1716 | if ( blocking ) |
| 1717 | { |
| 1718 | TEST_ASSERT( blocking_ret == 0 ); |
| 1719 | } |
| 1720 | else |
| 1721 | { |
| 1722 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1723 | } |
| 1724 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1725 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1726 | |
| 1727 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1728 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1729 | /* First receiving is from the server */ |
| 1730 | socket = ( i % 2 == 0 ) ? ( &server ) : ( &client ); |
| 1731 | |
| 1732 | recv_ret[i] = recv( socket, received[i] + read[i], |
| 1733 | MSGLEN - read[i] ); |
| 1734 | |
| 1735 | /* The result depends on whether any data was sent */ |
| 1736 | if ( send_ret[i] > 0 ) |
| 1737 | { |
| 1738 | TEST_ASSERT( recv_ret[i] > 0 ); |
| 1739 | TEST_ASSERT( recv_ret[i] <= BUFLEN ); |
| 1740 | read[i] += recv_ret[i]; |
| 1741 | } |
| 1742 | else if( blocking ) |
| 1743 | { |
| 1744 | TEST_ASSERT( recv_ret[i] == 0 ); |
| 1745 | } |
| 1746 | else |
| 1747 | { |
| 1748 | TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1749 | recv_ret[i] = 0; |
| 1750 | } |
| 1751 | |
| 1752 | /* If the buffer is empty we can test blocking and non-blocking |
| 1753 | * read */ |
| 1754 | if ( recv_ret[i] == BUFLEN ) |
| 1755 | { |
| 1756 | int blocking_ret = recv( socket, received[i], 1 ); |
| 1757 | if ( blocking ) |
| 1758 | { |
| 1759 | TEST_ASSERT( blocking_ret == 0 ); |
| 1760 | } |
| 1761 | else |
| 1762 | { |
| 1763 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1764 | } |
| 1765 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1766 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1767 | |
| 1768 | progress = 0; |
| 1769 | for( i = 0; i < ROUNDS; i++ ) |
| 1770 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1771 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | for( i = 0; i < ROUNDS; i++ ) |
| 1776 | TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 ); |
| 1777 | |
| 1778 | exit: |
| 1779 | |
| 1780 | mbedtls_mock_socket_close( &client ); |
| 1781 | mbedtls_mock_socket_close( &server ); |
| 1782 | } |
| 1783 | /* END_CASE */ |
| 1784 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 1785 | /* BEGIN_CASE */ |
| 1786 | void ssl_message_queue_sanity( ) |
| 1787 | { |
| 1788 | mbedtls_test_message_queue queue; |
| 1789 | |
| 1790 | /* Trying to push/pull to an empty queue */ |
| 1791 | TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 ) |
| 1792 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 1793 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 ) |
| 1794 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 1795 | |
| 1796 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1797 | TEST_ASSERT( queue.capacity == 3 ); |
| 1798 | TEST_ASSERT( queue.num == 0 ); |
| 1799 | |
| 1800 | exit: |
| 1801 | mbedtls_test_message_queue_free( &queue ); |
| 1802 | } |
| 1803 | /* END_CASE */ |
| 1804 | |
| 1805 | /* BEGIN_CASE */ |
| 1806 | void ssl_message_queue_basic( ) |
| 1807 | { |
| 1808 | mbedtls_test_message_queue queue; |
| 1809 | |
| 1810 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1811 | |
| 1812 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
| 1813 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1814 | TEST_ASSERT( queue.capacity == 3 ); |
| 1815 | TEST_ASSERT( queue.num == 1 ); |
| 1816 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1817 | TEST_ASSERT( queue.capacity == 3 ); |
| 1818 | TEST_ASSERT( queue.num == 2 ); |
| 1819 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 1820 | TEST_ASSERT( queue.capacity == 3 ); |
| 1821 | TEST_ASSERT( queue.num == 3 ); |
| 1822 | |
| 1823 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1824 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1825 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 1826 | |
| 1827 | exit: |
| 1828 | mbedtls_test_message_queue_free( &queue ); |
| 1829 | } |
| 1830 | /* END_CASE */ |
| 1831 | |
| 1832 | /* BEGIN_CASE */ |
| 1833 | void ssl_message_queue_overflow_underflow( ) |
| 1834 | { |
| 1835 | mbedtls_test_message_queue queue; |
| 1836 | |
| 1837 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1838 | |
| 1839 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
| 1840 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1841 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1842 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 1843 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) |
| 1844 | == MBEDTLS_TEST_ERROR_QUEUE_FULL ); |
| 1845 | |
| 1846 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1847 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1848 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 1849 | |
| 1850 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) |
| 1851 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 1852 | |
| 1853 | exit: |
| 1854 | mbedtls_test_message_queue_free( &queue ); |
| 1855 | } |
| 1856 | /* END_CASE */ |
| 1857 | |
| 1858 | /* BEGIN_CASE */ |
| 1859 | void ssl_message_queue_interleaved( ) |
| 1860 | { |
| 1861 | mbedtls_test_message_queue queue; |
| 1862 | |
| 1863 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1864 | |
| 1865 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 1866 | * (to wrap around the buffer) */ |
| 1867 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1868 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1869 | |
| 1870 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1871 | |
| 1872 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 1873 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 ); |
| 1874 | |
| 1875 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1876 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 1877 | |
| 1878 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 ); |
| 1879 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 ); |
| 1880 | |
| 1881 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 ); |
| 1882 | |
| 1883 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 ); |
| 1884 | |
| 1885 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 ); |
| 1886 | |
| 1887 | exit: |
| 1888 | mbedtls_test_message_queue_free( &queue ); |
| 1889 | } |
| 1890 | /* END_CASE */ |
| 1891 | |
| 1892 | /* BEGIN_CASE */ |
| 1893 | void ssl_message_queue_insufficient_buffer( ) |
| 1894 | { |
| 1895 | mbedtls_test_message_queue queue; |
| 1896 | size_t message_len = 10; |
| 1897 | size_t buffer_len = 5; |
| 1898 | |
| 1899 | mbedtls_test_message_queue_setup( &queue, 1 ); |
| 1900 | |
| 1901 | /* Popping without a sufficient buffer */ |
| 1902 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len ) |
| 1903 | == (int) message_len ); |
| 1904 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len ) |
| 1905 | == (int) buffer_len ); |
| 1906 | exit: |
| 1907 | mbedtls_test_message_queue_free( &queue ); |
| 1908 | } |
| 1909 | /* END_CASE */ |
| 1910 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1911 | /* BEGIN_CASE */ |
| 1912 | void ssl_message_mock_uninitialized( ) |
| 1913 | { |
| 1914 | enum { MSGLEN = 10 }; |
| 1915 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 1916 | mbedtls_mock_socket client, server; |
| 1917 | mbedtls_test_message_queue server_queue, client_queue; |
| 1918 | mbedtls_test_message_socket_context server_context, client_context; |
| 1919 | |
| 1920 | /* Send with a NULL context */ |
| 1921 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN ) |
| 1922 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 1923 | |
| 1924 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN ) |
| 1925 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 1926 | |
| 1927 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 1928 | &server, |
| 1929 | &server_context ) == 0 ); |
| 1930 | |
| 1931 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 1932 | &client, |
| 1933 | &client_context ) == 0 ); |
| 1934 | |
| 1935 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN ) |
| 1936 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 1937 | |
| 1938 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1939 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 1940 | |
| 1941 | /* Push directly to a queue to later simulate a disconnected behavior */ |
| 1942 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN ) |
| 1943 | == MSGLEN ); |
| 1944 | |
| 1945 | /* Test if there's an error when trying to read from a disconnected |
| 1946 | * socket */ |
| 1947 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1948 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 1949 | exit: |
| 1950 | mbedtls_message_socket_close( &server_context ); |
| 1951 | mbedtls_message_socket_close( &client_context ); |
| 1952 | } |
| 1953 | /* END_CASE */ |
| 1954 | |
| 1955 | /* BEGIN_CASE */ |
| 1956 | void ssl_message_mock_basic( ) |
| 1957 | { |
| 1958 | enum { MSGLEN = 10 }; |
| 1959 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 1960 | mbedtls_mock_socket client, server; |
| 1961 | unsigned i; |
| 1962 | mbedtls_test_message_queue server_queue, client_queue; |
| 1963 | mbedtls_test_message_socket_context server_context, client_context; |
| 1964 | |
| 1965 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 1966 | &server, |
| 1967 | &server_context ) == 0 ); |
| 1968 | |
| 1969 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 1970 | &client, |
| 1971 | &client_context ) == 0 ); |
| 1972 | |
| 1973 | /* Fill up the buffer with structured data so that unwanted changes |
| 1974 | * can be detected */ |
| 1975 | for( i = 0; i < MSGLEN; i++ ) |
| 1976 | { |
| 1977 | message[i] = i & 0xFF; |
| 1978 | } |
| 1979 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 1980 | MSGLEN ) ); |
| 1981 | |
| 1982 | /* Send the message to the server */ |
| 1983 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 1984 | MSGLEN ) == MSGLEN ); |
| 1985 | |
| 1986 | /* Read from the server */ |
| 1987 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1988 | == MSGLEN ); |
| 1989 | |
| 1990 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 1991 | memset( received, 0, MSGLEN ); |
| 1992 | |
| 1993 | /* Send the message to the client */ |
| 1994 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 1995 | MSGLEN ) == MSGLEN ); |
| 1996 | |
| 1997 | /* Read from the client */ |
| 1998 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 1999 | == MSGLEN ); |
| 2000 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2001 | |
| 2002 | exit: |
| 2003 | mbedtls_message_socket_close( &server_context ); |
| 2004 | mbedtls_message_socket_close( &client_context ); |
| 2005 | } |
| 2006 | /* END_CASE */ |
| 2007 | |
| 2008 | /* BEGIN_CASE */ |
| 2009 | void ssl_message_mock_queue_overflow_underflow( ) |
| 2010 | { |
| 2011 | enum { MSGLEN = 10 }; |
| 2012 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2013 | mbedtls_mock_socket client, server; |
| 2014 | unsigned i; |
| 2015 | mbedtls_test_message_queue server_queue, client_queue; |
| 2016 | mbedtls_test_message_socket_context server_context, client_context; |
| 2017 | |
| 2018 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2019 | &server, |
| 2020 | &server_context ) == 0 ); |
| 2021 | |
| 2022 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2023 | &client, |
| 2024 | &client_context ) == 0 ); |
| 2025 | |
| 2026 | /* Fill up the buffer with structured data so that unwanted changes |
| 2027 | * can be detected */ |
| 2028 | for( i = 0; i < MSGLEN; i++ ) |
| 2029 | { |
| 2030 | message[i] = i & 0xFF; |
| 2031 | } |
| 2032 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2033 | MSGLEN*2 ) ); |
| 2034 | |
| 2035 | /* Send three message to the server, last one with an error */ |
| 2036 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2037 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 2038 | |
| 2039 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2040 | MSGLEN ) == MSGLEN ); |
| 2041 | |
| 2042 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2043 | MSGLEN ) |
| 2044 | == MBEDTLS_TEST_ERROR_QUEUE_FULL ); |
| 2045 | |
| 2046 | /* Read three messages from the server, last one with an error */ |
| 2047 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2048 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 2049 | |
| 2050 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2051 | == MSGLEN ); |
| 2052 | |
| 2053 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2054 | |
| 2055 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2056 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2057 | |
| 2058 | exit: |
| 2059 | mbedtls_message_socket_close( &server_context ); |
| 2060 | mbedtls_message_socket_close( &client_context ); |
| 2061 | } |
| 2062 | /* END_CASE */ |
| 2063 | |
| 2064 | /* BEGIN_CASE */ |
| 2065 | void ssl_message_mock_socket_overflow( ) |
| 2066 | { |
| 2067 | enum { MSGLEN = 10 }; |
| 2068 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2069 | mbedtls_mock_socket client, server; |
| 2070 | unsigned i; |
| 2071 | mbedtls_test_message_queue server_queue, client_queue; |
| 2072 | mbedtls_test_message_socket_context server_context, client_context; |
| 2073 | |
| 2074 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2075 | &server, |
| 2076 | &server_context ) == 0 ); |
| 2077 | |
| 2078 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2079 | &client, |
| 2080 | &client_context ) == 0 ); |
| 2081 | |
| 2082 | /* Fill up the buffer with structured data so that unwanted changes |
| 2083 | * can be detected */ |
| 2084 | for( i = 0; i < MSGLEN; i++ ) |
| 2085 | { |
| 2086 | message[i] = i & 0xFF; |
| 2087 | } |
| 2088 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2089 | MSGLEN ) ); |
| 2090 | |
| 2091 | /* Send two message to the server, second one with an error */ |
| 2092 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2093 | MSGLEN ) == MSGLEN ); |
| 2094 | |
| 2095 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2096 | MSGLEN ) |
| 2097 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 2098 | |
| 2099 | /* Read the only message from the server */ |
| 2100 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2101 | == MSGLEN ); |
| 2102 | |
| 2103 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2104 | |
| 2105 | exit: |
| 2106 | mbedtls_message_socket_close( &server_context ); |
| 2107 | mbedtls_message_socket_close( &client_context ); |
| 2108 | } |
| 2109 | /* END_CASE */ |
| 2110 | |
| 2111 | /* BEGIN_CASE */ |
| 2112 | void ssl_message_mock_truncated( ) |
| 2113 | { |
| 2114 | enum { MSGLEN = 10 }; |
| 2115 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2116 | mbedtls_mock_socket client, server; |
| 2117 | unsigned i; |
| 2118 | mbedtls_test_message_queue server_queue, client_queue; |
| 2119 | mbedtls_test_message_socket_context server_context, client_context; |
| 2120 | |
| 2121 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2122 | &server, |
| 2123 | &server_context ) == 0 ); |
| 2124 | |
| 2125 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2126 | &client, |
| 2127 | &client_context ) == 0 ); |
| 2128 | |
| 2129 | memset( received, 0, MSGLEN ); |
| 2130 | /* Fill up the buffer with structured data so that unwanted changes |
| 2131 | * can be detected */ |
| 2132 | for( i = 0; i < MSGLEN; i++ ) |
| 2133 | { |
| 2134 | message[i] = i & 0xFF; |
| 2135 | } |
| 2136 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2137 | 2 * MSGLEN ) ); |
| 2138 | |
| 2139 | /* Send two messages to the server, the second one small enough to fit in the |
| 2140 | * receiver's buffer. */ |
| 2141 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2142 | MSGLEN ) == MSGLEN ); |
| 2143 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2144 | MSGLEN / 2 ) == MSGLEN / 2 ); |
| 2145 | /* Read a truncated message from the server */ |
| 2146 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 2147 | == MSGLEN/2 ); |
| 2148 | |
| 2149 | /* Test that the first half of the message is valid, and second one isn't */ |
| 2150 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 2151 | TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 ) |
| 2152 | != 0 ); |
| 2153 | memset( received, 0, MSGLEN ); |
| 2154 | |
| 2155 | /* Read a full message from the server */ |
| 2156 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 2157 | == MSGLEN / 2 ); |
| 2158 | |
| 2159 | /* Test that the first half of the message is valid */ |
| 2160 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 2161 | |
| 2162 | exit: |
| 2163 | mbedtls_message_socket_close( &server_context ); |
| 2164 | mbedtls_message_socket_close( &client_context ); |
| 2165 | } |
| 2166 | /* END_CASE */ |
| 2167 | |
| 2168 | /* BEGIN_CASE */ |
| 2169 | void ssl_message_mock_socket_read_error( ) |
| 2170 | { |
| 2171 | enum { MSGLEN = 10 }; |
| 2172 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2173 | mbedtls_mock_socket client, server; |
| 2174 | unsigned i; |
| 2175 | mbedtls_test_message_queue server_queue, client_queue; |
| 2176 | mbedtls_test_message_socket_context server_context, client_context; |
| 2177 | |
| 2178 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 2179 | &server, |
| 2180 | &server_context ) == 0 ); |
| 2181 | |
| 2182 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 2183 | &client, |
| 2184 | &client_context ) == 0 ); |
| 2185 | |
| 2186 | /* Fill up the buffer with structured data so that unwanted changes |
| 2187 | * can be detected */ |
| 2188 | for( i = 0; i < MSGLEN; i++ ) |
| 2189 | { |
| 2190 | message[i] = i & 0xFF; |
| 2191 | } |
| 2192 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2193 | MSGLEN ) ); |
| 2194 | |
| 2195 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2196 | MSGLEN ) == MSGLEN ); |
| 2197 | |
| 2198 | /* Force a read error by disconnecting the socket by hand */ |
| 2199 | server.status = 0; |
| 2200 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2201 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 2202 | /* Return to a valid state */ |
| 2203 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 2204 | |
| 2205 | memset( received, 0, sizeof( received ) ); |
| 2206 | |
| 2207 | /* Test that even though the server tried to read once disconnected, the |
| 2208 | * continuity is preserved */ |
| 2209 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2210 | == MSGLEN ); |
| 2211 | |
| 2212 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2213 | |
| 2214 | exit: |
| 2215 | mbedtls_message_socket_close( &server_context ); |
| 2216 | mbedtls_message_socket_close( &client_context ); |
| 2217 | } |
| 2218 | /* END_CASE */ |
| 2219 | |
| 2220 | /* BEGIN_CASE */ |
| 2221 | void ssl_message_mock_interleaved_one_way( ) |
| 2222 | { |
| 2223 | enum { MSGLEN = 10 }; |
| 2224 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2225 | mbedtls_mock_socket client, server; |
| 2226 | unsigned i; |
| 2227 | mbedtls_test_message_queue server_queue, client_queue; |
| 2228 | mbedtls_test_message_socket_context server_context, client_context; |
| 2229 | |
| 2230 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 2231 | &server, |
| 2232 | &server_context ) == 0 ); |
| 2233 | |
| 2234 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 2235 | &client, |
| 2236 | &client_context ) == 0 ); |
| 2237 | |
| 2238 | /* Fill up the buffer with structured data so that unwanted changes |
| 2239 | * can be detected */ |
| 2240 | for( i = 0; i < MSGLEN; i++ ) |
| 2241 | { |
| 2242 | message[i] = i & 0xFF; |
| 2243 | } |
| 2244 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2245 | MSGLEN*3 ) ); |
| 2246 | |
| 2247 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 2248 | * (to wrap around the buffer) */ |
| 2249 | for( i = 0; i < 2; i++ ) |
| 2250 | { |
| 2251 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2252 | MSGLEN ) == MSGLEN ); |
| 2253 | |
| 2254 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2255 | MSGLEN ) == MSGLEN ); |
| 2256 | |
| 2257 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2258 | MSGLEN ) == MSGLEN ); |
| 2259 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2260 | memset( received, 0, sizeof( received ) ); |
| 2261 | } |
| 2262 | |
| 2263 | for( i = 0; i < 2; i++ ) |
| 2264 | { |
| 2265 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2266 | MSGLEN ) == MSGLEN ); |
| 2267 | |
| 2268 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2269 | } |
| 2270 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2271 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2272 | exit: |
| 2273 | mbedtls_message_socket_close( &server_context ); |
| 2274 | mbedtls_message_socket_close( &client_context ); |
| 2275 | } |
| 2276 | /* END_CASE */ |
| 2277 | |
| 2278 | /* BEGIN_CASE */ |
| 2279 | void ssl_message_mock_interleaved_two_ways( ) |
| 2280 | { |
| 2281 | enum { MSGLEN = 10 }; |
| 2282 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2283 | mbedtls_mock_socket client, server; |
| 2284 | unsigned i; |
| 2285 | mbedtls_test_message_queue server_queue, client_queue; |
| 2286 | mbedtls_test_message_socket_context server_context, client_context; |
| 2287 | |
| 2288 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 2289 | &server, |
| 2290 | &server_context ) == 0 ); |
| 2291 | |
| 2292 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 2293 | &client, |
| 2294 | &client_context ) == 0 ); |
| 2295 | |
| 2296 | /* Fill up the buffer with structured data so that unwanted changes |
| 2297 | * can be detected */ |
| 2298 | for( i = 0; i < MSGLEN; i++ ) |
| 2299 | { |
| 2300 | message[i] = i & 0xFF; |
| 2301 | } |
| 2302 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2303 | MSGLEN*3 ) ); |
| 2304 | |
| 2305 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 2306 | * (to wrap around the buffer) both ways. */ |
| 2307 | for( i = 0; i < 2; i++ ) |
| 2308 | { |
| 2309 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2310 | MSGLEN ) == MSGLEN ); |
| 2311 | |
| 2312 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2313 | MSGLEN ) == MSGLEN ); |
| 2314 | |
| 2315 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 2316 | MSGLEN ) == MSGLEN ); |
| 2317 | |
| 2318 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 2319 | MSGLEN ) == MSGLEN ); |
| 2320 | |
| 2321 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2322 | MSGLEN ) == MSGLEN ); |
| 2323 | |
| 2324 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2325 | |
| 2326 | memset( received, 0, sizeof( received ) ); |
| 2327 | |
| 2328 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 2329 | MSGLEN ) == MSGLEN ); |
| 2330 | |
| 2331 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2332 | |
| 2333 | memset( received, 0, sizeof( received ) ); |
| 2334 | } |
| 2335 | |
| 2336 | for( i = 0; i < 2; i++ ) |
| 2337 | { |
| 2338 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2339 | MSGLEN ) == MSGLEN ); |
| 2340 | |
| 2341 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2342 | memset( received, 0, sizeof( received ) ); |
| 2343 | |
| 2344 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 2345 | MSGLEN ) == MSGLEN ); |
| 2346 | |
| 2347 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2348 | memset( received, 0, sizeof( received ) ); |
| 2349 | } |
| 2350 | |
| 2351 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2352 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2353 | |
| 2354 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 2355 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2356 | exit: |
| 2357 | mbedtls_message_socket_close( &server_context ); |
| 2358 | mbedtls_message_socket_close( &client_context ); |
| 2359 | } |
| 2360 | /* END_CASE */ |
| 2361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2362 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 2363 | void ssl_dtls_replay( data_t * prevs, data_t * new, int ret ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2364 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2365 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2366 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2367 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2368 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 2369 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2370 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 2371 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 2372 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 2373 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 2374 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 2375 | MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2376 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2377 | |
| 2378 | /* Read previous record numbers */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2379 | for( len = 0; len < prevs->len; len += 6 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2380 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2381 | memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2382 | mbedtls_ssl_dtls_replay_update( &ssl ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /* Check new number */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2386 | memcpy( ssl.in_ctr + 2, new->x, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2387 | TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2389 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2390 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2391 | } |
| 2392 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 2393 | |
| 2394 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 2395 | void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) |
| 2396 | { |
| 2397 | mbedtls_ssl_context ssl; |
| 2398 | mbedtls_ssl_init( &ssl ); |
| 2399 | |
| 2400 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); |
| 2401 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); |
| 2402 | |
| 2403 | mbedtls_ssl_free( &ssl ); |
| 2404 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 2405 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2406 | |
| 2407 | /* BEGIN_CASE */ |
| 2408 | void ssl_crypt_record( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2409 | int etm, int tag_mode, int ver, |
| 2410 | int cid0_len, int cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2411 | { |
| 2412 | /* |
| 2413 | * Test several record encryptions and decryptions |
| 2414 | * with plenty of space before and after the data |
| 2415 | * within the record buffer. |
| 2416 | */ |
| 2417 | |
| 2418 | int ret; |
| 2419 | int num_records = 16; |
| 2420 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 2421 | |
| 2422 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2423 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2424 | size_t const buflen = 512; |
| 2425 | mbedtls_record rec, rec_backup; |
| 2426 | |
| 2427 | mbedtls_ssl_init( &ssl ); |
| 2428 | mbedtls_ssl_transform_init( &t0 ); |
| 2429 | mbedtls_ssl_transform_init( &t1 ); |
| 2430 | TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2431 | etm, tag_mode, ver, |
| 2432 | (size_t) cid0_len, |
| 2433 | (size_t) cid1_len ) == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2434 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2435 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2436 | |
| 2437 | while( num_records-- > 0 ) |
| 2438 | { |
| 2439 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 2440 | /* Take turns in who's sending and who's receiving. */ |
| 2441 | if( num_records % 3 == 0 ) |
| 2442 | { |
| 2443 | t_dec = &t0; |
| 2444 | t_enc = &t1; |
| 2445 | } |
| 2446 | else |
| 2447 | { |
| 2448 | t_dec = &t1; |
| 2449 | t_enc = &t0; |
| 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | * The record header affects the transformation in two ways: |
| 2454 | * 1) It determines the AEAD additional data |
| 2455 | * 2) The record counter sometimes determines the IV. |
| 2456 | * |
| 2457 | * Apart from that, the fields don't have influence. |
| 2458 | * In particular, it is currently not the responsibility |
| 2459 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 2460 | * version matches the record version, or that the |
| 2461 | * type is sensible. |
| 2462 | */ |
| 2463 | |
| 2464 | memset( rec.ctr, num_records, sizeof( rec.ctr ) ); |
| 2465 | rec.type = 42; |
| 2466 | rec.ver[0] = num_records; |
| 2467 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2468 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2469 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2470 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2471 | |
| 2472 | rec.buf = buf; |
| 2473 | rec.buf_len = buflen; |
| 2474 | rec.data_offset = 16; |
| 2475 | /* Make sure to vary the length to exercise different |
| 2476 | * paddings. */ |
| 2477 | rec.data_len = 1 + num_records; |
| 2478 | |
| 2479 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 2480 | |
| 2481 | /* Make a copy for later comparison */ |
| 2482 | rec_backup = rec; |
| 2483 | |
| 2484 | /* Encrypt record */ |
| 2485 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
| 2486 | rnd_std_rand, NULL ); |
| 2487 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2488 | if( ret != 0 ) |
| 2489 | { |
| 2490 | continue; |
| 2491 | } |
| 2492 | |
| 2493 | /* Decrypt record with t_dec */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2494 | ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ); |
| 2495 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2496 | |
| 2497 | /* Compare results */ |
| 2498 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 2499 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 2500 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 2501 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 2502 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 2503 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 2504 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 2505 | rec_backup.buf + rec_backup.data_offset, |
| 2506 | rec.data_len ) == 0 ); |
| 2507 | } |
| 2508 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2509 | exit: |
| 2510 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2511 | /* Cleanup */ |
| 2512 | mbedtls_ssl_free( &ssl ); |
| 2513 | mbedtls_ssl_transform_free( &t0 ); |
| 2514 | mbedtls_ssl_transform_free( &t1 ); |
| 2515 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2516 | mbedtls_free( buf ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2517 | } |
| 2518 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2519 | |
| 2520 | /* BEGIN_CASE */ |
| 2521 | void ssl_crypt_record_small( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2522 | int etm, int tag_mode, int ver, |
| 2523 | int cid0_len, int cid1_len ) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2524 | { |
| 2525 | /* |
| 2526 | * Test pairs of encryption and decryption with an increasing |
| 2527 | * amount of space in the record buffer - in more detail: |
| 2528 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 2529 | * in front of the plaintext, and expect the encryption |
| 2530 | * to succeed starting from some offset. Always keep |
| 2531 | * enough space in the end of the buffer. |
| 2532 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 2533 | * at the end of the plaintext, and expect the encryption |
| 2534 | * to succeed starting from some offset. Always keep |
| 2535 | * enough space at the beginning of the buffer. |
| 2536 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 2537 | * both at the front and end of the plaintext, |
| 2538 | * and expect the encryption to succeed starting from |
| 2539 | * some offset. |
| 2540 | * |
| 2541 | * If encryption succeeds, check that decryption succeeds |
| 2542 | * and yields the original record. |
| 2543 | */ |
| 2544 | |
| 2545 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 2546 | |
| 2547 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2548 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2549 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2550 | mbedtls_record rec, rec_backup; |
| 2551 | |
| 2552 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2553 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 2554 | size_t offset; /* Available space at beginning/end/both */ |
| 2555 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2556 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2557 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 2558 | size_t default_post_padding = 128; /* Post-padding to use in mode 1 */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2559 | |
| 2560 | int seen_success; /* Indicates if in the current mode we've |
| 2561 | * already seen a successful test. */ |
| 2562 | |
| 2563 | mbedtls_ssl_init( &ssl ); |
| 2564 | mbedtls_ssl_transform_init( &t0 ); |
| 2565 | mbedtls_ssl_transform_init( &t1 ); |
| 2566 | TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2567 | etm, tag_mode, ver, |
| 2568 | (size_t) cid0_len, |
| 2569 | (size_t) cid1_len ) == 0 ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2570 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2571 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2572 | |
| 2573 | for( mode=1; mode <= 3; mode++ ) |
| 2574 | { |
| 2575 | seen_success = 0; |
| 2576 | for( offset=0; offset <= threshold; offset++ ) |
| 2577 | { |
| 2578 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 2579 | t_dec = &t0; |
| 2580 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2581 | |
| 2582 | memset( rec.ctr, offset, sizeof( rec.ctr ) ); |
| 2583 | rec.type = 42; |
| 2584 | rec.ver[0] = offset; |
| 2585 | rec.ver[1] = offset; |
| 2586 | rec.buf = buf; |
| 2587 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2588 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2589 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2590 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2591 | |
| 2592 | switch( mode ) |
| 2593 | { |
| 2594 | case 1: /* Space in the beginning */ |
| 2595 | rec.data_offset = offset; |
| 2596 | rec.data_len = buflen - offset - default_post_padding; |
| 2597 | break; |
| 2598 | |
| 2599 | case 2: /* Space in the end */ |
| 2600 | rec.data_offset = default_pre_padding; |
| 2601 | rec.data_len = buflen - default_pre_padding - offset; |
| 2602 | break; |
| 2603 | |
| 2604 | case 3: /* Space in the beginning and end */ |
| 2605 | rec.data_offset = offset; |
| 2606 | rec.data_len = buflen - 2 * offset; |
| 2607 | break; |
| 2608 | |
| 2609 | default: |
| 2610 | TEST_ASSERT( 0 ); |
| 2611 | break; |
| 2612 | } |
| 2613 | |
| 2614 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 2615 | |
| 2616 | /* Make a copy for later comparison */ |
| 2617 | rec_backup = rec; |
| 2618 | |
| 2619 | /* Encrypt record */ |
| 2620 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL ); |
| 2621 | |
| 2622 | if( ( mode == 1 || mode == 2 ) && seen_success ) |
| 2623 | { |
| 2624 | TEST_ASSERT( ret == 0 ); |
| 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2629 | if( ret == 0 ) |
| 2630 | seen_success = 1; |
| 2631 | } |
| 2632 | |
| 2633 | if( ret != 0 ) |
| 2634 | continue; |
| 2635 | |
| 2636 | /* Decrypt record with t_dec */ |
| 2637 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 ); |
| 2638 | |
| 2639 | /* Compare results */ |
| 2640 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 2641 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 2642 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 2643 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 2644 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 2645 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 2646 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 2647 | rec_backup.buf + rec_backup.data_offset, |
| 2648 | rec.data_len ) == 0 ); |
| 2649 | } |
| 2650 | |
| 2651 | TEST_ASSERT( seen_success == 1 ); |
| 2652 | } |
| 2653 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2654 | exit: |
| 2655 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2656 | /* Cleanup */ |
| 2657 | mbedtls_ssl_free( &ssl ); |
| 2658 | mbedtls_ssl_transform_free( &t0 ); |
| 2659 | mbedtls_ssl_transform_free( &t1 ); |
| 2660 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2661 | mbedtls_free( buf ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2662 | } |
| 2663 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 2664 | |
| 2665 | /* BEGIN_CASE */ |
| 2666 | void ssl_tls_prf( int type, data_t * secret, data_t * random, |
| 2667 | char *label, data_t *result_hex_str, int exp_ret ) |
| 2668 | { |
| 2669 | unsigned char *output; |
| 2670 | |
| 2671 | output = mbedtls_calloc( 1, result_hex_str->len ); |
| 2672 | if( output == NULL ) |
| 2673 | goto exit; |
| 2674 | |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 2675 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2676 | TEST_ASSERT( psa_crypto_init() == 0 ); |
| 2677 | #endif |
| 2678 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 2679 | TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len, |
| 2680 | label, random->x, random->len, |
| 2681 | output, result_hex_str->len ) == exp_ret ); |
| 2682 | |
| 2683 | if( exp_ret == 0 ) |
| 2684 | { |
| 2685 | TEST_ASSERT( hexcmp( output, result_hex_str->x, |
| 2686 | result_hex_str->len, result_hex_str->len ) == 0 ); |
| 2687 | } |
| 2688 | exit: |
| 2689 | |
| 2690 | mbedtls_free( output ); |
| 2691 | } |
| 2692 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2693 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2694 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2695 | void ssl_serialize_session_save_load( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2696 | { |
| 2697 | mbedtls_ssl_session original, restored; |
| 2698 | unsigned char *buf = NULL; |
| 2699 | size_t len; |
| 2700 | |
| 2701 | /* |
| 2702 | * Test that a save-load pair is the identity |
| 2703 | */ |
| 2704 | |
| 2705 | mbedtls_ssl_session_init( &original ); |
| 2706 | mbedtls_ssl_session_init( &restored ); |
| 2707 | |
| 2708 | /* Prepare a dummy session to work on */ |
| 2709 | TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 ); |
| 2710 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2711 | /* Serialize it */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2712 | TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len ) |
| 2713 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2714 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL ); |
| 2715 | TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len ) |
| 2716 | == 0 ); |
| 2717 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2718 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2719 | TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 ); |
| 2720 | |
| 2721 | /* |
| 2722 | * Make sure both session structures are identical |
| 2723 | */ |
| 2724 | #if defined(MBEDTLS_HAVE_TIME) |
| 2725 | TEST_ASSERT( original.start == restored.start ); |
| 2726 | #endif |
| 2727 | TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); |
| 2728 | TEST_ASSERT( original.compression == restored.compression ); |
| 2729 | TEST_ASSERT( original.id_len == restored.id_len ); |
| 2730 | TEST_ASSERT( memcmp( original.id, |
| 2731 | restored.id, sizeof( original.id ) ) == 0 ); |
| 2732 | TEST_ASSERT( memcmp( original.master, |
| 2733 | restored.master, sizeof( original.master ) ) == 0 ); |
| 2734 | |
| 2735 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2736 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2737 | TEST_ASSERT( ( original.peer_cert == NULL ) == |
| 2738 | ( restored.peer_cert == NULL ) ); |
| 2739 | if( original.peer_cert != NULL ) |
| 2740 | { |
| 2741 | TEST_ASSERT( original.peer_cert->raw.len == |
| 2742 | restored.peer_cert->raw.len ); |
| 2743 | TEST_ASSERT( memcmp( original.peer_cert->raw.p, |
| 2744 | restored.peer_cert->raw.p, |
| 2745 | original.peer_cert->raw.len ) == 0 ); |
| 2746 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2747 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2748 | TEST_ASSERT( original.peer_cert_digest_type == |
| 2749 | restored.peer_cert_digest_type ); |
| 2750 | TEST_ASSERT( original.peer_cert_digest_len == |
| 2751 | restored.peer_cert_digest_len ); |
| 2752 | TEST_ASSERT( ( original.peer_cert_digest == NULL ) == |
| 2753 | ( restored.peer_cert_digest == NULL ) ); |
| 2754 | if( original.peer_cert_digest != NULL ) |
| 2755 | { |
| 2756 | TEST_ASSERT( memcmp( original.peer_cert_digest, |
| 2757 | restored.peer_cert_digest, |
| 2758 | original.peer_cert_digest_len ) == 0 ); |
| 2759 | } |
| 2760 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2761 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2762 | TEST_ASSERT( original.verify_result == restored.verify_result ); |
| 2763 | |
| 2764 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 2765 | TEST_ASSERT( original.ticket_len == restored.ticket_len ); |
| 2766 | if( original.ticket_len != 0 ) |
| 2767 | { |
| 2768 | TEST_ASSERT( original.ticket != NULL ); |
| 2769 | TEST_ASSERT( restored.ticket != NULL ); |
| 2770 | TEST_ASSERT( memcmp( original.ticket, |
| 2771 | restored.ticket, original.ticket_len ) == 0 ); |
| 2772 | } |
| 2773 | TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime ); |
| 2774 | #endif |
| 2775 | |
| 2776 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2777 | TEST_ASSERT( original.mfl_code == restored.mfl_code ); |
| 2778 | #endif |
| 2779 | |
| 2780 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2781 | TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac ); |
| 2782 | #endif |
| 2783 | |
| 2784 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2785 | TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac ); |
| 2786 | #endif |
| 2787 | |
| 2788 | exit: |
| 2789 | mbedtls_ssl_session_free( &original ); |
| 2790 | mbedtls_ssl_session_free( &restored ); |
| 2791 | mbedtls_free( buf ); |
| 2792 | } |
| 2793 | /* END_CASE */ |
| 2794 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 2795 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2796 | void ssl_serialize_session_load_save( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2797 | { |
| 2798 | mbedtls_ssl_session session; |
| 2799 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 2800 | size_t len0, len1, len2; |
| 2801 | |
| 2802 | /* |
| 2803 | * Test that a load-save pair is the identity |
| 2804 | */ |
| 2805 | |
| 2806 | mbedtls_ssl_session_init( &session ); |
| 2807 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 2808 | /* Prepare a dummy session to work on */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 2809 | TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 2810 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2811 | /* Get desired buffer size for serializing */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2812 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 ) |
| 2813 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2814 | |
| 2815 | /* Allocate first buffer */ |
| 2816 | buf1 = mbedtls_calloc( 1, len0 ); |
| 2817 | TEST_ASSERT( buf1 != NULL ); |
| 2818 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2819 | /* Serialize to buffer and free live session */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2820 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 ) |
| 2821 | == 0 ); |
| 2822 | TEST_ASSERT( len0 == len1 ); |
| 2823 | mbedtls_ssl_session_free( &session ); |
| 2824 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2825 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 2826 | TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2827 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2828 | /* Allocate second buffer and serialize to it */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2829 | buf2 = mbedtls_calloc( 1, len0 ); |
Manuel Pégourié-Gonnard | b407990 | 2019-05-24 09:52:10 +0200 | [diff] [blame] | 2830 | TEST_ASSERT( buf2 != NULL ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2831 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 ) |
| 2832 | == 0 ); |
| 2833 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2834 | /* Make sure both serialized versions are identical */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2835 | TEST_ASSERT( len1 == len2 ); |
| 2836 | TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 ); |
| 2837 | |
| 2838 | exit: |
| 2839 | mbedtls_ssl_session_free( &session ); |
| 2840 | mbedtls_free( buf1 ); |
| 2841 | mbedtls_free( buf2 ); |
| 2842 | } |
| 2843 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2844 | |
| 2845 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2846 | void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2847 | { |
| 2848 | mbedtls_ssl_session session; |
| 2849 | unsigned char *buf = NULL; |
| 2850 | size_t good_len, bad_len, test_len; |
| 2851 | |
| 2852 | /* |
| 2853 | * Test that session_save() fails cleanly on small buffers |
| 2854 | */ |
| 2855 | |
| 2856 | mbedtls_ssl_session_init( &session ); |
| 2857 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2858 | /* Prepare dummy session and get serialized size */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 2859 | TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2860 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 2861 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2862 | |
| 2863 | /* Try all possible bad lengths */ |
| 2864 | for( bad_len = 1; bad_len < good_len; bad_len++ ) |
| 2865 | { |
| 2866 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
| 2867 | mbedtls_free( buf ); |
| 2868 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL ); |
| 2869 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len, |
| 2870 | &test_len ) |
| 2871 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2872 | TEST_ASSERT( test_len == good_len ); |
| 2873 | } |
| 2874 | |
| 2875 | exit: |
| 2876 | mbedtls_ssl_session_free( &session ); |
| 2877 | mbedtls_free( buf ); |
| 2878 | } |
| 2879 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2880 | |
| 2881 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2882 | void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2883 | { |
| 2884 | mbedtls_ssl_session session; |
| 2885 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 2886 | size_t good_len, bad_len; |
| 2887 | |
| 2888 | /* |
| 2889 | * Test that session_load() fails cleanly on small buffers |
| 2890 | */ |
| 2891 | |
| 2892 | mbedtls_ssl_session_init( &session ); |
| 2893 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2894 | /* Prepare serialized session data */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 2895 | TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2896 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 2897 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2898 | TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL ); |
| 2899 | TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len, |
| 2900 | &good_len ) == 0 ); |
| 2901 | mbedtls_ssl_session_free( &session ); |
| 2902 | |
| 2903 | /* Try all possible bad lengths */ |
| 2904 | for( bad_len = 0; bad_len < good_len; bad_len++ ) |
| 2905 | { |
| 2906 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
| 2907 | mbedtls_free( bad_buf ); |
| 2908 | bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 ); |
| 2909 | TEST_ASSERT( bad_buf != NULL ); |
| 2910 | memcpy( bad_buf, good_buf, bad_len ); |
| 2911 | |
| 2912 | TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len ) |
| 2913 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2914 | } |
| 2915 | |
| 2916 | exit: |
| 2917 | mbedtls_ssl_session_free( &session ); |
| 2918 | mbedtls_free( good_buf ); |
| 2919 | mbedtls_free( bad_buf ); |
| 2920 | } |
| 2921 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2922 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2923 | /* BEGIN_CASE */ |
| 2924 | void ssl_session_serialize_version_check( int corrupt_major, |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2925 | int corrupt_minor, |
| 2926 | int corrupt_patch, |
| 2927 | int corrupt_config ) |
| 2928 | { |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2929 | unsigned char serialized_session[ 2048 ]; |
| 2930 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2931 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2932 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2933 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 2934 | corrupt_minor == 1, |
| 2935 | corrupt_patch == 1, |
| 2936 | corrupt_config == 1, |
| 2937 | corrupt_config == 1 }; |
| 2938 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2939 | mbedtls_ssl_session_init( &session ); |
| 2940 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2941 | /* Infer length of serialized session. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2942 | TEST_ASSERT( mbedtls_ssl_session_save( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2943 | serialized_session, |
| 2944 | sizeof( serialized_session ), |
| 2945 | &serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2946 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2947 | mbedtls_ssl_session_free( &session ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2948 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2949 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2950 | * de-serialize the session - double-check that. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2951 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2952 | serialized_session, |
| 2953 | serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2954 | mbedtls_ssl_session_free( &session ); |
| 2955 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2956 | /* Go through the bytes in the serialized session header and |
| 2957 | * corrupt them bit-by-bit. */ |
| 2958 | for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ ) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2959 | { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2960 | int cur_bit; |
| 2961 | unsigned char * const byte = &serialized_session[ cur_byte ]; |
| 2962 | |
| 2963 | if( should_corrupt_byte[ cur_byte ] == 0 ) |
| 2964 | continue; |
| 2965 | |
| 2966 | for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ ) |
| 2967 | { |
| 2968 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 2969 | /* Modify a single bit in the serialized session. */ |
| 2970 | *byte ^= corrupted_bit; |
| 2971 | |
| 2972 | /* Attempt to deserialize */ |
| 2973 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
| 2974 | serialized_session, |
| 2975 | serialized_session_len ) == |
Hanno Becker | f9b3303 | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 2976 | MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2977 | |
| 2978 | /* Undo the change */ |
| 2979 | *byte ^= corrupted_bit; |
| 2980 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2981 | } |
| 2982 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2983 | } |
| 2984 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2985 | |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 2986 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15 */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2987 | void mbedtls_endpoint_sanity( int endpoint_type ) |
| 2988 | { |
| 2989 | enum { BUFFSIZE = 1024 }; |
| 2990 | mbedtls_endpoint ep; |
| 2991 | int ret = -1; |
| 2992 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 2993 | ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2994 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 2995 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 2996 | ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2997 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 2998 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 2999 | ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 3000 | TEST_ASSERT( ret == 0 ); |
| 3001 | |
| 3002 | exit: |
| 3003 | mbedtls_endpoint_free( &ep ); |
| 3004 | } |
| 3005 | /* END_CASE */ |
| 3006 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 3007 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15 */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 3008 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 3009 | { |
| 3010 | enum { BUFFSIZE = 1024 }; |
| 3011 | mbedtls_endpoint base_ep, second_ep; |
| 3012 | int ret = -1; |
| 3013 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 3014 | ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 3015 | TEST_ASSERT( ret == 0 ); |
| 3016 | |
| 3017 | ret = mbedtls_endpoint_init( &second_ep, |
| 3018 | ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 3019 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 3020 | MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 3021 | TEST_ASSERT( ret == 0 ); |
| 3022 | |
| 3023 | ret = mbedtls_mock_socket_connect( &(base_ep.socket), |
| 3024 | &(second_ep.socket), |
| 3025 | BUFFSIZE ); |
| 3026 | TEST_ASSERT( ret == 0 ); |
| 3027 | |
| 3028 | ret = mbedtls_move_handshake_to_state( &(base_ep.ssl), |
| 3029 | &(second_ep.ssl), |
| 3030 | state ); |
| 3031 | if( need_pass ) |
| 3032 | { |
| 3033 | TEST_ASSERT( ret == 0 ); |
| 3034 | TEST_ASSERT( base_ep.ssl.state == state ); |
| 3035 | } |
| 3036 | else |
| 3037 | { |
| 3038 | TEST_ASSERT( ret != 0 ); |
| 3039 | TEST_ASSERT( base_ep.ssl.state != state ); |
| 3040 | } |
| 3041 | |
| 3042 | exit: |
| 3043 | mbedtls_endpoint_free( &base_ep ); |
| 3044 | mbedtls_endpoint_free( &second_ep ); |
| 3045 | } |
| 3046 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 3047 | |
| 3048 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15 */ |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 3049 | void handshake( const char *cipher, int version, int pk_alg, |
| 3050 | data_t *psk_str ) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 3051 | { |
| 3052 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 3053 | int forced_ciphersuite[2]; |
| 3054 | enum { BUFFSIZE = 1024 }; |
| 3055 | mbedtls_endpoint client, server; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 3056 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 3057 | const char *psk_identity = "foo"; |
| 3058 | #else |
| 3059 | (void) psk_str; |
| 3060 | #endif |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 3061 | /* Client side */ |
| 3062 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
| 3063 | pk_alg ) == 0 ); |
| 3064 | |
| 3065 | mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 3066 | version ); |
| 3067 | mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 3068 | version ); |
| 3069 | |
| 3070 | if( strlen( cipher ) > 0 ) |
| 3071 | { |
| 3072 | set_ciphersuite( &client.conf, cipher, forced_ciphersuite ); |
| 3073 | } |
| 3074 | /* Server side */ |
| 3075 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
| 3076 | pk_alg ) == 0 ); |
| 3077 | |
| 3078 | mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 3079 | version ); |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 3080 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 3081 | if( psk_str->len > 0 ) |
| 3082 | { |
| 3083 | TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, psk_str->x, |
| 3084 | psk_str->len, |
| 3085 | (const unsigned char *) psk_identity, |
| 3086 | strlen( psk_identity ) ) == 0 ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 3087 | |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 3088 | TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, psk_str->x, |
| 3089 | psk_str->len, |
| 3090 | (const unsigned char *) psk_identity, |
| 3091 | strlen( psk_identity ) ) == 0 ); |
| 3092 | |
| 3093 | mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL ); |
| 3094 | } |
| 3095 | #endif |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 3096 | TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), |
| 3097 | &(server.socket), |
| 3098 | BUFFSIZE ) == 0 ); |
| 3099 | |
| 3100 | TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), |
| 3101 | &(server.ssl), |
| 3102 | MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3103 | == 0 ); |
| 3104 | TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 3105 | TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 3106 | |
| 3107 | exit: |
| 3108 | mbedtls_endpoint_free( &client ); |
| 3109 | mbedtls_endpoint_free( &server ); |
| 3110 | } |
| 3111 | /* END_CASE */ |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 3112 | |
| 3113 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15 */ |
| 3114 | void send_application_data( int mfl, int cli_msg_len, int srv_msg_len, |
| 3115 | const int expected_cli_frames, |
| 3116 | const int expected_srv_frames ) |
| 3117 | { |
| 3118 | enum { BUFFSIZE = 2048 }; |
| 3119 | mbedtls_endpoint server, client; |
| 3120 | unsigned char *cli_msg_buf = malloc( cli_msg_len ); |
| 3121 | unsigned char *cli_in_buf = malloc( srv_msg_len ); |
| 3122 | unsigned char *srv_msg_buf = malloc( srv_msg_len ); |
| 3123 | unsigned char *srv_in_buf = malloc( cli_msg_len ); |
| 3124 | int ret = -1; |
| 3125 | |
| 3126 | ret = mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, MBEDTLS_PK_RSA ); |
| 3127 | TEST_ASSERT( ret == 0 ); |
| 3128 | |
| 3129 | ret = mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_PK_RSA ); |
| 3130 | TEST_ASSERT( ret == 0 ); |
| 3131 | |
| 3132 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3133 | ret = mbedtls_ssl_conf_max_frag_len( &(server.conf), (unsigned char) mfl ); |
| 3134 | TEST_ASSERT( ret == 0 ); |
| 3135 | |
| 3136 | ret = mbedtls_ssl_conf_max_frag_len( &(client.conf), (unsigned char) mfl ); |
| 3137 | TEST_ASSERT( ret == 0 ); |
| 3138 | #else |
| 3139 | TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == mfl ); |
| 3140 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3141 | |
| 3142 | ret = mbedtls_mock_socket_connect( &(server.socket), &(client.socket), |
| 3143 | BUFFSIZE ); |
| 3144 | TEST_ASSERT( ret == 0 ); |
| 3145 | |
| 3146 | ret = mbedtls_move_handshake_to_state( &(client.ssl), |
| 3147 | &(server.ssl), |
| 3148 | MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 3149 | TEST_ASSERT( ret == 0 ); |
| 3150 | TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 3151 | TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 3152 | |
| 3153 | /* Perform this test with two message types. At first use a message |
| 3154 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 3155 | * At the second time use message with generated data */ |
| 3156 | for( int msg_type = 0; msg_type < 2; msg_type++ ) |
| 3157 | { |
| 3158 | int cli_writen = 0; |
| 3159 | int srv_writen = 0; |
| 3160 | int cli_read = 0; |
| 3161 | int srv_read = 0; |
| 3162 | int cli_fragments = 0; |
| 3163 | int srv_fragments = 0; |
| 3164 | |
| 3165 | if( msg_type == 0 ) |
| 3166 | { |
| 3167 | memset( cli_msg_buf, 0x00, cli_msg_len ); |
| 3168 | memset( srv_msg_buf, 0xff, srv_msg_len ); |
| 3169 | } |
| 3170 | else |
| 3171 | { |
| 3172 | int j = 0; |
| 3173 | for( int i = 0; i < cli_msg_len; i++ ) |
| 3174 | cli_msg_buf[i] = j++ & 0xFF; |
| 3175 | for( int i = 0; i < srv_msg_len; i++ ) |
| 3176 | srv_msg_buf[i] = ( j -= 5 ) & 0xFF; |
| 3177 | } |
| 3178 | |
| 3179 | while( cli_read < srv_msg_len || srv_read < cli_msg_len ) |
| 3180 | { |
| 3181 | /* Client sending */ |
| 3182 | if( cli_msg_len > cli_writen ) |
| 3183 | { |
| 3184 | ret = mbedtls_ssl_write_fragment( &(client.ssl), cli_msg_buf, |
| 3185 | cli_msg_len, &cli_writen, &cli_fragments ); |
| 3186 | TEST_ASSERT( ( ret >= 0 && ret <= cli_msg_len ) || |
| 3187 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 3188 | } |
| 3189 | |
| 3190 | /* Server sending */ |
| 3191 | if( srv_msg_len > srv_writen ) |
| 3192 | { |
| 3193 | ret = mbedtls_ssl_write_fragment( &(server.ssl), srv_msg_buf, |
| 3194 | srv_msg_len, &srv_writen, &srv_fragments ); |
| 3195 | TEST_ASSERT( ( ret >= 0 && ret <= srv_msg_len ) || |
| 3196 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 3197 | } |
| 3198 | |
| 3199 | /* Client reading */ |
| 3200 | if( cli_read < srv_msg_len ) |
| 3201 | { |
| 3202 | ret = mbedtls_ssl_read_fragment( &(client.ssl), cli_in_buf, |
| 3203 | srv_msg_len, &cli_read ); |
| 3204 | TEST_ASSERT( ( ret >= 0 && ret <= srv_msg_len ) || |
| 3205 | ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 3206 | } |
| 3207 | |
| 3208 | /* Server reading */ |
| 3209 | if( srv_read < cli_msg_len ) |
| 3210 | { |
| 3211 | ret = mbedtls_ssl_read_fragment( &(server.ssl), srv_in_buf, |
| 3212 | cli_msg_len, &srv_read ); |
| 3213 | TEST_ASSERT( ( ret >= 0 && ret <= cli_msg_len ) || |
| 3214 | ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | TEST_ASSERT( 0 == memcmp( cli_msg_buf, srv_in_buf, cli_msg_len ) ); |
| 3219 | TEST_ASSERT( 0 == memcmp( srv_msg_buf, cli_in_buf, srv_msg_len ) ); |
| 3220 | TEST_ASSERT( cli_fragments == expected_cli_frames ); |
| 3221 | TEST_ASSERT( srv_fragments == expected_srv_frames ); |
| 3222 | } |
| 3223 | |
| 3224 | exit: |
| 3225 | mbedtls_endpoint_free( &client ); |
| 3226 | mbedtls_endpoint_free( &server ); |
| 3227 | free( cli_msg_buf ); |
| 3228 | free( cli_in_buf ); |
| 3229 | free( srv_msg_buf ); |
| 3230 | free( srv_in_buf ); |
| 3231 | } |
| 3232 | /* END_CASE */ |