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 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [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 | |
| 673 | for( i = 0; mbedtls_test_cas[i] != NULL; i++ ) |
| 674 | { |
| 675 | ret = mbedtls_x509_crt_parse( &( cert->ca_cert ), |
| 676 | (const unsigned char *) mbedtls_test_cas[i], |
| 677 | mbedtls_test_cas_len[i] ); |
| 678 | TEST_ASSERT( ret == 0 ); |
| 679 | } |
| 680 | |
| 681 | for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ ) |
| 682 | { |
| 683 | ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ), |
| 684 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 685 | mbedtls_test_cas_der_len[i] ); |
| 686 | TEST_ASSERT( ret == 0 ); |
| 687 | } |
| 688 | |
| 689 | /* Load own certificate and private key */ |
| 690 | |
| 691 | if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 692 | { |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 693 | if( pk_alg == MBEDTLS_PK_RSA ) |
| 694 | { |
| 695 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 696 | (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der, |
| 697 | mbedtls_test_srv_crt_rsa_sha256_der_len ); |
| 698 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 699 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 700 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 701 | (const unsigned char*) mbedtls_test_srv_key_rsa_der, |
| 702 | mbedtls_test_srv_key_rsa_der_len, NULL, 0 ); |
| 703 | TEST_ASSERT( ret == 0 ); |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 708 | (const unsigned char*) mbedtls_test_srv_crt_ec_der, |
| 709 | mbedtls_test_srv_crt_ec_der_len ); |
| 710 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 711 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 712 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 713 | (const unsigned char*) mbedtls_test_srv_key_ec_der, |
| 714 | mbedtls_test_srv_key_ec_der_len, NULL, 0 ); |
| 715 | TEST_ASSERT( ret == 0 ); |
| 716 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 717 | } |
| 718 | else |
| 719 | { |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 720 | if( pk_alg == MBEDTLS_PK_RSA ) |
| 721 | { |
| 722 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 723 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 724 | mbedtls_test_cli_crt_rsa_der_len ); |
| 725 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 726 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 727 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 728 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 729 | mbedtls_test_cli_key_rsa_der_len, NULL, 0 ); |
| 730 | TEST_ASSERT( ret == 0 ); |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 735 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 736 | mbedtls_test_cli_crt_ec_len ); |
| 737 | TEST_ASSERT( ret == 0 ); |
| 738 | |
| 739 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 740 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 741 | mbedtls_test_cli_key_ec_der_len, NULL, 0 ); |
| 742 | TEST_ASSERT( ret == 0 ); |
| 743 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL ); |
| 747 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 748 | ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ), |
| 749 | &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 750 | TEST_ASSERT( ret == 0 ); |
| 751 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 752 | exit: |
| 753 | if( ret != 0 ) |
| 754 | { |
| 755 | mbedtls_x509_crt_free( &( cert->ca_cert ) ); |
| 756 | mbedtls_x509_crt_free( &( cert->cert ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 757 | mbedtls_pk_free( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()` |
| 765 | * after calling this function even if it fails. |
| 766 | * |
| 767 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 768 | * MBEDTLS_SSL_IS_CLIENT. |
| 769 | * |
| 770 | * \retval 0 on success, otherwise error code. |
| 771 | */ |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 772 | 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] | 773 | { |
| 774 | int ret = -1; |
| 775 | |
| 776 | if( ep == NULL ) |
| 777 | { |
| 778 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 779 | } |
| 780 | |
| 781 | memset( ep, 0, sizeof( *ep ) ); |
| 782 | |
| 783 | ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client"; |
| 784 | |
| 785 | mbedtls_ssl_init( &( ep->ssl ) ); |
| 786 | mbedtls_ssl_config_init( &( ep->conf ) ); |
| 787 | mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) ); |
| 788 | mbedtls_ssl_conf_rng( &( ep->conf ), |
| 789 | mbedtls_ctr_drbg_random, |
| 790 | &( ep->ctr_drbg ) ); |
| 791 | mbedtls_entropy_init( &( ep->entropy ) ); |
| 792 | mbedtls_mock_socket_init( &( ep->socket ) ); |
| 793 | |
| 794 | ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func, |
| 795 | &( ep->entropy ), (const unsigned char *) ( ep->name ), |
| 796 | strlen( ep->name ) ); |
| 797 | TEST_ASSERT( ret == 0 ); |
| 798 | |
| 799 | /* Non-blocking callbacks without timeout */ |
| 800 | mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ), |
| 801 | mbedtls_mock_tcp_send_nb, |
| 802 | mbedtls_mock_tcp_recv_nb, |
| 803 | NULL ); |
| 804 | |
| 805 | ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) ); |
| 806 | TEST_ASSERT( ret == 0 ); |
| 807 | |
| 808 | ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type, |
| 809 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 810 | MBEDTLS_SSL_PRESET_DEFAULT ); |
| 811 | TEST_ASSERT( ret == 0 ); |
| 812 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 813 | ret = mbedtls_endpoint_certificate_init( ep, pk_alg ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 814 | TEST_ASSERT( ret == 0 ); |
| 815 | |
| 816 | exit: |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Deinitializes certificates from endpoint represented by \p ep. |
| 822 | */ |
| 823 | void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep ) |
| 824 | { |
| 825 | mbedtls_endpoint_certificate *cert = &( ep->cert ); |
| 826 | mbedtls_x509_crt_free( &( cert->ca_cert ) ); |
| 827 | mbedtls_x509_crt_free( &( cert->cert ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 828 | mbedtls_pk_free( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Deinitializes endpoint represented by \p ep. |
| 833 | */ |
| 834 | void mbedtls_endpoint_free( mbedtls_endpoint *ep ) |
| 835 | { |
| 836 | mbedtls_endpoint_certificate_free( ep ); |
| 837 | |
| 838 | mbedtls_ssl_free( &( ep->ssl ) ); |
| 839 | mbedtls_ssl_config_free( &( ep->conf ) ); |
| 840 | mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) ); |
| 841 | mbedtls_entropy_free( &( ep->entropy ) ); |
| 842 | mbedtls_mock_socket_close( &( ep->socket ) ); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 847 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 848 | * connected before calling this function. |
| 849 | * |
| 850 | * \retval 0 on success, otherwise error code. |
| 851 | */ |
| 852 | int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl, |
| 853 | mbedtls_ssl_context *second_ssl, |
| 854 | int state ) |
| 855 | { |
| 856 | enum { BUFFSIZE = 1024 }; |
| 857 | int max_steps = 1000; |
| 858 | int ret = 0; |
| 859 | |
| 860 | if( ssl == NULL || second_ssl == NULL ) |
| 861 | { |
| 862 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 863 | } |
| 864 | |
| 865 | /* Perform communication via connected sockets */ |
| 866 | while( ( ssl->state != state ) && ( --max_steps >= 0 ) ) |
| 867 | { |
| 868 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 869 | * there is no need to call the next step */ |
| 870 | if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 871 | { |
| 872 | ret = mbedtls_ssl_handshake_step( second_ssl ); |
| 873 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 874 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 875 | { |
| 876 | return ret; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /* We only care about the \p ssl state and returns, so we call it last, |
| 881 | * to leave the iteration as soon as the state is as expected. */ |
| 882 | ret = mbedtls_ssl_handshake_step( ssl ); |
| 883 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 884 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 885 | { |
| 886 | return ret; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | return ( max_steps >= 0 ) ? ret : -1; |
| 891 | } |
| 892 | |
| 893 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 894 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 895 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 896 | * Helper function setting up inverse record transformations |
| 897 | * using given cipher, hash, EtM mode, authentication tag length, |
| 898 | * and version. |
| 899 | */ |
| 900 | |
| 901 | #define CHK( x ) \ |
| 902 | do \ |
| 903 | { \ |
| 904 | if( !( x ) ) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 905 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 906 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 907 | goto cleanup; \ |
| 908 | } \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 909 | } while( 0 ) |
| 910 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 911 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 912 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 913 | #else |
| 914 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 915 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 916 | |
| 917 | static int build_transforms( mbedtls_ssl_transform *t_in, |
| 918 | mbedtls_ssl_transform *t_out, |
| 919 | int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 920 | int etm, int tag_mode, int ver, |
| 921 | size_t cid0_len, |
| 922 | size_t cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 923 | { |
| 924 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 925 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 926 | |
| 927 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 928 | unsigned char *key0 = NULL, *key1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 929 | unsigned char iv_enc[16], iv_dec[16]; |
| 930 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 931 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 932 | unsigned char cid0[ SSL_CID_LEN_MIN ]; |
| 933 | unsigned char cid1[ SSL_CID_LEN_MIN ]; |
| 934 | |
| 935 | rnd_std_rand( NULL, cid0, sizeof( cid0 ) ); |
| 936 | rnd_std_rand( NULL, cid1, sizeof( cid1 ) ); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 937 | #else |
| 938 | ((void) cid0_len); |
| 939 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 940 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 941 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 942 | maclen = 0; |
| 943 | |
| 944 | /* Pick cipher */ |
| 945 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
| 946 | CHK( cipher_info != NULL ); |
| 947 | CHK( cipher_info->iv_size <= 16 ); |
| 948 | CHK( cipher_info->key_bitlen % 8 == 0 ); |
| 949 | |
| 950 | /* Pick keys */ |
| 951 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 952 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 953 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 954 | * `keylen == 0` in the case of the NULL cipher. */ |
| 955 | CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
| 956 | CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 957 | memset( key0, 0x1, keylen ); |
| 958 | memset( key1, 0x2, keylen ); |
| 959 | |
| 960 | /* Setup cipher contexts */ |
| 961 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 ); |
| 962 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 ); |
| 963 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 ); |
| 964 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 ); |
| 965 | |
| 966 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 967 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
| 968 | { |
| 969 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc, |
| 970 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 971 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec, |
| 972 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 973 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc, |
| 974 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 975 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec, |
| 976 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 977 | } |
| 978 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 979 | |
| 980 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0, |
| 981 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 982 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1, |
| 983 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
| 984 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1, |
| 985 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 986 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0, |
| 987 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 988 | |
| 989 | /* Setup MAC contexts */ |
| 990 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 991 | if( cipher_info->mode == MBEDTLS_MODE_CBC || |
| 992 | cipher_info->mode == MBEDTLS_MODE_STREAM ) |
| 993 | { |
| 994 | mbedtls_md_info_t const *md_info; |
| 995 | unsigned char *md0, *md1; |
| 996 | |
| 997 | /* Pick hash */ |
| 998 | md_info = mbedtls_md_info_from_type( hash_id ); |
| 999 | CHK( md_info != NULL ); |
| 1000 | |
| 1001 | /* Pick hash keys */ |
| 1002 | maclen = mbedtls_md_get_size( md_info ); |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1003 | CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
| 1004 | CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1005 | memset( md0, 0x5, maclen ); |
| 1006 | memset( md1, 0x6, maclen ); |
| 1007 | |
| 1008 | CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1009 | CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1010 | CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1011 | CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1012 | |
| 1013 | if( ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 1014 | { |
| 1015 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc, |
| 1016 | md0, maclen ) == 0 ); |
| 1017 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec, |
| 1018 | md1, maclen ) == 0 ); |
| 1019 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc, |
| 1020 | md1, maclen ) == 0 ); |
| 1021 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec, |
| 1022 | md0, maclen ) == 0 ); |
| 1023 | } |
| 1024 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1025 | else |
| 1026 | { |
| 1027 | memcpy( &t_in->mac_enc, md0, maclen ); |
| 1028 | memcpy( &t_in->mac_dec, md1, maclen ); |
| 1029 | memcpy( &t_out->mac_enc, md1, maclen ); |
| 1030 | memcpy( &t_out->mac_dec, md0, maclen ); |
| 1031 | } |
| 1032 | #endif |
| 1033 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1034 | mbedtls_free( md0 ); |
| 1035 | mbedtls_free( md1 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1036 | } |
| 1037 | #else |
| 1038 | ((void) hash_id); |
| 1039 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1040 | |
| 1041 | |
| 1042 | /* Pick IV's (regardless of whether they |
| 1043 | * are being used by the transform). */ |
| 1044 | ivlen = cipher_info->iv_size; |
| 1045 | memset( iv_enc, 0x3, sizeof( iv_enc ) ); |
| 1046 | memset( iv_dec, 0x4, sizeof( iv_dec ) ); |
| 1047 | |
| 1048 | /* |
| 1049 | * Setup transforms |
| 1050 | */ |
| 1051 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1052 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1053 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1054 | t_out->encrypt_then_mac = etm; |
| 1055 | t_in->encrypt_then_mac = etm; |
| 1056 | #else |
| 1057 | ((void) etm); |
| 1058 | #endif |
| 1059 | |
| 1060 | t_out->minor_ver = ver; |
| 1061 | t_in->minor_ver = ver; |
| 1062 | t_out->ivlen = ivlen; |
| 1063 | t_in->ivlen = ivlen; |
| 1064 | |
| 1065 | switch( cipher_info->mode ) |
| 1066 | { |
| 1067 | case MBEDTLS_MODE_GCM: |
| 1068 | case MBEDTLS_MODE_CCM: |
| 1069 | t_out->fixed_ivlen = 4; |
| 1070 | t_in->fixed_ivlen = 4; |
| 1071 | t_out->maclen = 0; |
| 1072 | t_in->maclen = 0; |
| 1073 | switch( tag_mode ) |
| 1074 | { |
| 1075 | case 0: /* Full tag */ |
| 1076 | t_out->taglen = 16; |
| 1077 | t_in->taglen = 16; |
| 1078 | break; |
| 1079 | case 1: /* Partial tag */ |
| 1080 | t_out->taglen = 8; |
| 1081 | t_in->taglen = 8; |
| 1082 | break; |
| 1083 | default: |
| 1084 | return( 1 ); |
| 1085 | } |
| 1086 | break; |
| 1087 | |
| 1088 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1089 | t_out->fixed_ivlen = 12; |
| 1090 | t_in->fixed_ivlen = 12; |
| 1091 | t_out->maclen = 0; |
| 1092 | t_in->maclen = 0; |
| 1093 | switch( tag_mode ) |
| 1094 | { |
| 1095 | case 0: /* Full tag */ |
| 1096 | t_out->taglen = 16; |
| 1097 | t_in->taglen = 16; |
| 1098 | break; |
| 1099 | case 1: /* Partial tag */ |
| 1100 | t_out->taglen = 8; |
| 1101 | t_in->taglen = 8; |
| 1102 | break; |
| 1103 | default: |
| 1104 | return( 1 ); |
| 1105 | } |
| 1106 | break; |
| 1107 | |
| 1108 | case MBEDTLS_MODE_STREAM: |
| 1109 | case MBEDTLS_MODE_CBC: |
| 1110 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1111 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1112 | t_out->taglen = 0; |
| 1113 | t_in->taglen = 0; |
| 1114 | switch( tag_mode ) |
| 1115 | { |
| 1116 | case 0: /* Full tag */ |
| 1117 | t_out->maclen = maclen; |
| 1118 | t_in->maclen = maclen; |
| 1119 | break; |
| 1120 | case 1: /* Partial tag */ |
| 1121 | t_out->maclen = 10; |
| 1122 | t_in->maclen = 10; |
| 1123 | break; |
| 1124 | default: |
| 1125 | return( 1 ); |
| 1126 | } |
| 1127 | break; |
| 1128 | default: |
| 1129 | return( 1 ); |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | /* Setup IV's */ |
| 1134 | |
| 1135 | memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) ); |
| 1136 | memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) ); |
| 1137 | memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) ); |
| 1138 | memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) ); |
| 1139 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1140 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1141 | /* Add CID */ |
| 1142 | memcpy( &t_in->in_cid, cid0, cid0_len ); |
| 1143 | memcpy( &t_in->out_cid, cid1, cid1_len ); |
| 1144 | t_in->in_cid_len = cid0_len; |
| 1145 | t_in->out_cid_len = cid1_len; |
| 1146 | memcpy( &t_out->in_cid, cid1, cid1_len ); |
| 1147 | memcpy( &t_out->out_cid, cid0, cid0_len ); |
| 1148 | t_out->in_cid_len = cid1_len; |
| 1149 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1150 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1151 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1152 | cleanup: |
| 1153 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1154 | mbedtls_free( key0 ); |
| 1155 | mbedtls_free( key1 ); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1156 | |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1157 | return( ret ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1160 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1161 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1162 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1163 | */ |
| 1164 | static int ssl_populate_session( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1165 | int ticket_len, |
| 1166 | const char *crt_file ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1167 | { |
| 1168 | #if defined(MBEDTLS_HAVE_TIME) |
| 1169 | session->start = mbedtls_time( NULL ) - 42; |
| 1170 | #endif |
| 1171 | session->ciphersuite = 0xabcd; |
| 1172 | session->compression = 1; |
| 1173 | session->id_len = sizeof( session->id ); |
| 1174 | memset( session->id, 66, session->id_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1175 | memset( session->master, 17, sizeof( session->master ) ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1176 | |
Manuel Pégourié-Gonnard | 1f6033a | 2019-05-24 10:17:52 +0200 | [diff] [blame] | 1177 | #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] | 1178 | if( strlen( crt_file ) != 0 ) |
| 1179 | { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1180 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1181 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1182 | |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1183 | mbedtls_x509_crt_init( &tmp_crt ); |
| 1184 | ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file ); |
| 1185 | if( ret != 0 ) |
| 1186 | return( ret ); |
| 1187 | |
| 1188 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1189 | /* Move temporary CRT. */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1190 | session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) ); |
| 1191 | if( session->peer_cert == NULL ) |
| 1192 | return( -1 ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1193 | *session->peer_cert = tmp_crt; |
| 1194 | memset( &tmp_crt, 0, sizeof( tmp_crt ) ); |
| 1195 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1196 | /* Calculate digest of temporary CRT. */ |
| 1197 | session->peer_cert_digest = |
| 1198 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 1199 | if( session->peer_cert_digest == NULL ) |
| 1200 | return( -1 ); |
| 1201 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 1202 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 1203 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1204 | session->peer_cert_digest ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1205 | if( ret != 0 ) |
| 1206 | return( ret ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1207 | session->peer_cert_digest_type = |
| 1208 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1209 | session->peer_cert_digest_len = |
| 1210 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1211 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1212 | |
| 1213 | mbedtls_x509_crt_free( &tmp_crt ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1214 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1215 | #else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1216 | (void) crt_file; |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1217 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1218 | session->verify_result = 0xdeadbeef; |
| 1219 | |
| 1220 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 1221 | if( ticket_len != 0 ) |
| 1222 | { |
| 1223 | session->ticket = mbedtls_calloc( 1, ticket_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1224 | if( session->ticket == NULL ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1225 | return( -1 ); |
| 1226 | memset( session->ticket, 33, ticket_len ); |
| 1227 | } |
| 1228 | session->ticket_len = ticket_len; |
| 1229 | session->ticket_lifetime = 86401; |
| 1230 | #else |
| 1231 | (void) ticket_len; |
| 1232 | #endif |
| 1233 | |
| 1234 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1235 | session->mfl_code = 1; |
| 1236 | #endif |
| 1237 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1238 | session->trunc_hmac = 1; |
| 1239 | #endif |
| 1240 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1241 | session->encrypt_then_mac = 1; |
| 1242 | #endif |
| 1243 | |
| 1244 | return( 0 ); |
| 1245 | } |
| 1246 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1247 | /* END_HEADER */ |
| 1248 | |
| 1249 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1251 | * END_DEPENDENCIES |
| 1252 | */ |
| 1253 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1254 | /* BEGIN_CASE */ |
| 1255 | void test_callback_buffer_sanity() |
| 1256 | { |
| 1257 | enum { MSGLEN = 10 }; |
| 1258 | mbedtls_test_buffer buf; |
| 1259 | unsigned char input[MSGLEN]; |
| 1260 | unsigned char output[MSGLEN]; |
| 1261 | |
| 1262 | memset( input, 0, sizeof(input) ); |
| 1263 | |
| 1264 | /* Make sure calling put and get on NULL buffer results in error. */ |
| 1265 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) ) |
| 1266 | == -1 ); |
| 1267 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) ) |
| 1268 | == -1 ); |
| 1269 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1270 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1271 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 ); |
| 1272 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 ); |
| 1273 | |
| 1274 | /* Make sure calling put and get on a buffer that hasn't been set up results |
| 1275 | * in eror. */ |
| 1276 | mbedtls_test_buffer_init( &buf ); |
| 1277 | |
| 1278 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 ); |
| 1279 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) ) |
| 1280 | == -1 ); |
| 1281 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1282 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1283 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 ); |
| 1284 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 ); |
| 1285 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1286 | /* Make sure calling put and get on NULL input only results in |
| 1287 | * error if the length is not zero, and that a NULL output is valid for data |
| 1288 | * dropping. |
| 1289 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1290 | |
| 1291 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 ); |
| 1292 | |
| 1293 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
| 1294 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) ) |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 1295 | == 0 ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1296 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 ); |
| 1297 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 ); |
| 1298 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 1299 | /* Make sure calling put several times in the row is safe */ |
| 1300 | |
| 1301 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) |
| 1302 | == sizeof( input ) ); |
| 1303 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 ); |
| 1304 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 ); |
| 1305 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 ); |
| 1306 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 ); |
| 1307 | |
| 1308 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1309 | exit: |
| 1310 | |
| 1311 | mbedtls_test_buffer_free( &buf ); |
| 1312 | } |
| 1313 | /* END_CASE */ |
| 1314 | |
| 1315 | /* |
| 1316 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 1317 | * correct and works as expected. |
| 1318 | * |
| 1319 | * That is |
| 1320 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 1321 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 1322 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 1323 | * bytes. |
| 1324 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 1325 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 1326 | */ |
| 1327 | |
| 1328 | /* BEGIN_CASE */ |
| 1329 | void test_callback_buffer( int size, int put1, int put1_ret, |
| 1330 | int get1, int get1_ret, int put2, int put2_ret, |
| 1331 | int get2, int get2_ret ) |
| 1332 | { |
| 1333 | enum { ROUNDS = 2 }; |
| 1334 | size_t put[ROUNDS]; |
| 1335 | int put_ret[ROUNDS]; |
| 1336 | size_t get[ROUNDS]; |
| 1337 | int get_ret[ROUNDS]; |
| 1338 | mbedtls_test_buffer buf; |
| 1339 | unsigned char* input = NULL; |
| 1340 | size_t input_len; |
| 1341 | unsigned char* output = NULL; |
| 1342 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1343 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 1344 | |
| 1345 | mbedtls_test_buffer_init( &buf ); |
| 1346 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 ); |
| 1347 | |
| 1348 | /* Check the sanity of input parameters and initialise local variables. That |
| 1349 | * is, ensure that the amount of data is not negative and that we are not |
| 1350 | * expecting more to put or get than we actually asked for. */ |
| 1351 | TEST_ASSERT( put1 >= 0 ); |
| 1352 | put[0] = put1; |
| 1353 | put_ret[0] = put1_ret; |
| 1354 | TEST_ASSERT( put1_ret <= put1 ); |
| 1355 | TEST_ASSERT( put2 >= 0 ); |
| 1356 | put[1] = put2; |
| 1357 | put_ret[1] = put2_ret; |
| 1358 | TEST_ASSERT( put2_ret <= put2 ); |
| 1359 | |
| 1360 | TEST_ASSERT( get1 >= 0 ); |
| 1361 | get[0] = get1; |
| 1362 | get_ret[0] = get1_ret; |
| 1363 | TEST_ASSERT( get1_ret <= get1 ); |
| 1364 | TEST_ASSERT( get2 >= 0 ); |
| 1365 | get[1] = get2; |
| 1366 | get_ret[1] = get2_ret; |
| 1367 | TEST_ASSERT( get2_ret <= get2 ); |
| 1368 | |
| 1369 | input_len = 0; |
| 1370 | /* Calculate actual input and output lengths */ |
| 1371 | for( j = 0; j < ROUNDS; j++ ) |
| 1372 | { |
| 1373 | if( put_ret[j] > 0 ) |
| 1374 | { |
| 1375 | input_len += put_ret[j]; |
| 1376 | } |
| 1377 | } |
| 1378 | /* In order to always have a valid pointer we always allocate at least 1 |
| 1379 | * byte. */ |
| 1380 | if( input_len == 0 ) |
| 1381 | input_len = 1; |
| 1382 | ASSERT_ALLOC( input, input_len ); |
| 1383 | |
| 1384 | output_len = 0; |
| 1385 | for( j = 0; j < ROUNDS; j++ ) |
| 1386 | { |
| 1387 | if( get_ret[j] > 0 ) |
| 1388 | { |
| 1389 | output_len += get_ret[j]; |
| 1390 | } |
| 1391 | } |
| 1392 | TEST_ASSERT( output_len <= input_len ); |
| 1393 | /* In order to always have a valid pointer we always allocate at least 1 |
| 1394 | * byte. */ |
| 1395 | if( output_len == 0 ) |
| 1396 | output_len = 1; |
| 1397 | ASSERT_ALLOC( output, output_len ); |
| 1398 | |
| 1399 | /* Fill up the buffer with structured data so that unwanted changes |
| 1400 | * can be detected */ |
| 1401 | for( i = 0; i < input_len; i++ ) |
| 1402 | { |
| 1403 | input[i] = i & 0xFF; |
| 1404 | } |
| 1405 | |
| 1406 | written = read = 0; |
| 1407 | for( j = 0; j < ROUNDS; j++ ) |
| 1408 | { |
| 1409 | TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf, |
| 1410 | input + written, put[j] ) ); |
| 1411 | written += put_ret[j]; |
| 1412 | TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf, |
| 1413 | output + read, get[j] ) ); |
| 1414 | read += get_ret[j]; |
| 1415 | TEST_ASSERT( read <= written ); |
| 1416 | if( get_ret[j] > 0 ) |
| 1417 | { |
| 1418 | TEST_ASSERT( memcmp( output + read - get_ret[j], |
| 1419 | input + read - get_ret[j], get_ret[j] ) |
| 1420 | == 0 ); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | exit: |
| 1425 | |
| 1426 | mbedtls_free( input ); |
| 1427 | mbedtls_free( output ); |
| 1428 | mbedtls_test_buffer_free( &buf ); |
| 1429 | } |
| 1430 | /* END_CASE */ |
| 1431 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1432 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1433 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 1434 | * correct and works as expected on unconnected sockets. |
| 1435 | */ |
| 1436 | |
| 1437 | /* BEGIN_CASE */ |
| 1438 | void ssl_mock_sanity( ) |
| 1439 | { |
| 1440 | enum { MSGLEN = 105 }; |
| 1441 | unsigned char message[MSGLEN]; |
| 1442 | unsigned char received[MSGLEN]; |
| 1443 | mbedtls_mock_socket socket; |
| 1444 | |
| 1445 | mbedtls_mock_socket_init( &socket ); |
| 1446 | TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 ); |
| 1447 | mbedtls_mock_socket_close( &socket ); |
| 1448 | mbedtls_mock_socket_init( &socket ); |
| 1449 | TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 ); |
| 1450 | mbedtls_mock_socket_close( &socket ); |
| 1451 | |
| 1452 | mbedtls_mock_socket_init( &socket ); |
| 1453 | TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 ); |
| 1454 | mbedtls_mock_socket_close( &socket ); |
| 1455 | mbedtls_mock_socket_init( &socket ); |
| 1456 | TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 ); |
| 1457 | mbedtls_mock_socket_close( &socket ); |
| 1458 | |
| 1459 | exit: |
| 1460 | |
| 1461 | mbedtls_mock_socket_close( &socket ); |
| 1462 | } |
| 1463 | /* END_CASE */ |
| 1464 | |
| 1465 | /* |
| 1466 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 1467 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1468 | */ |
| 1469 | |
| 1470 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1471 | void ssl_mock_tcp( int blocking ) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1472 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1473 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1474 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1475 | unsigned char message[MSGLEN]; |
| 1476 | unsigned char received[MSGLEN]; |
| 1477 | mbedtls_mock_socket client; |
| 1478 | mbedtls_mock_socket server; |
| 1479 | size_t written, read; |
| 1480 | int send_ret, recv_ret; |
| 1481 | mbedtls_ssl_send_t *send; |
| 1482 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1483 | unsigned i; |
| 1484 | |
| 1485 | if( blocking == 0 ) |
| 1486 | { |
| 1487 | send = mbedtls_mock_tcp_send_nb; |
| 1488 | recv = mbedtls_mock_tcp_recv_nb; |
| 1489 | } |
| 1490 | else |
| 1491 | { |
| 1492 | send = mbedtls_mock_tcp_send_b; |
| 1493 | recv = mbedtls_mock_tcp_recv_b; |
| 1494 | } |
| 1495 | |
| 1496 | mbedtls_mock_socket_init( &client ); |
| 1497 | mbedtls_mock_socket_init( &server ); |
| 1498 | |
| 1499 | /* Fill up the buffer with structured data so that unwanted changes |
| 1500 | * can be detected */ |
| 1501 | for( i = 0; i < MSGLEN; i++ ) |
| 1502 | { |
| 1503 | message[i] = i & 0xFF; |
| 1504 | } |
| 1505 | |
| 1506 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1507 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1508 | |
| 1509 | /* Send the message to the server */ |
| 1510 | send_ret = recv_ret = 1; |
| 1511 | written = read = 0; |
| 1512 | while( send_ret != 0 || recv_ret != 0 ) |
| 1513 | { |
| 1514 | send_ret = send( &client, message + written, MSGLEN - written ); |
| 1515 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1516 | TEST_ASSERT( send_ret >= 0 ); |
| 1517 | TEST_ASSERT( send_ret <= BUFLEN ); |
| 1518 | written += send_ret; |
| 1519 | |
| 1520 | /* If the buffer is full we can test blocking and non-blocking send */ |
| 1521 | if ( send_ret == BUFLEN ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1522 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1523 | int blocking_ret = send( &client, message , 1 ); |
| 1524 | if ( blocking ) |
| 1525 | { |
| 1526 | TEST_ASSERT( blocking_ret == 0 ); |
| 1527 | } |
| 1528 | else |
| 1529 | { |
| 1530 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1531 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1532 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1533 | |
| 1534 | recv_ret = recv( &server, received + read, MSGLEN - read ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1535 | |
| 1536 | /* The result depends on whether any data was sent */ |
| 1537 | if ( send_ret > 0 ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1538 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1539 | TEST_ASSERT( recv_ret > 0 ); |
| 1540 | TEST_ASSERT( recv_ret <= BUFLEN ); |
| 1541 | read += recv_ret; |
| 1542 | } |
| 1543 | else if( blocking ) |
| 1544 | { |
| 1545 | TEST_ASSERT( recv_ret == 0 ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1546 | } |
| 1547 | else |
| 1548 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1549 | TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1550 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1551 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1552 | |
| 1553 | /* If the buffer is empty we can test blocking and non-blocking read */ |
| 1554 | if ( recv_ret == BUFLEN ) |
| 1555 | { |
| 1556 | int blocking_ret = recv( &server, received, 1 ); |
| 1557 | if ( blocking ) |
| 1558 | { |
| 1559 | TEST_ASSERT( blocking_ret == 0 ); |
| 1560 | } |
| 1561 | else |
| 1562 | { |
| 1563 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1564 | } |
| 1565 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1566 | } |
| 1567 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 1568 | |
| 1569 | exit: |
| 1570 | |
| 1571 | mbedtls_mock_socket_close( &client ); |
| 1572 | mbedtls_mock_socket_close( &server ); |
| 1573 | } |
| 1574 | /* END_CASE */ |
| 1575 | |
| 1576 | /* |
| 1577 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 1578 | * send messages in both direction at the same time (with the I/O calls |
| 1579 | * interleaving). |
| 1580 | */ |
| 1581 | |
| 1582 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1583 | void ssl_mock_tcp_interleaving( int blocking ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 1584 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1585 | enum { ROUNDS = 2 }; |
| 1586 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1587 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1588 | unsigned char message[ROUNDS][MSGLEN]; |
| 1589 | unsigned char received[ROUNDS][MSGLEN]; |
| 1590 | mbedtls_mock_socket client; |
| 1591 | mbedtls_mock_socket server; |
| 1592 | size_t written[ROUNDS]; |
| 1593 | size_t read[ROUNDS]; |
| 1594 | int send_ret[ROUNDS]; |
| 1595 | int recv_ret[ROUNDS]; |
| 1596 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1597 | mbedtls_ssl_send_t *send; |
| 1598 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1599 | |
| 1600 | if( blocking == 0 ) |
| 1601 | { |
| 1602 | send = mbedtls_mock_tcp_send_nb; |
| 1603 | recv = mbedtls_mock_tcp_recv_nb; |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | send = mbedtls_mock_tcp_send_b; |
| 1608 | recv = mbedtls_mock_tcp_recv_b; |
| 1609 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1610 | |
| 1611 | mbedtls_mock_socket_init( &client ); |
| 1612 | mbedtls_mock_socket_init( &server ); |
| 1613 | |
| 1614 | /* Fill up the buffers with structured data so that unwanted changes |
| 1615 | * can be detected */ |
| 1616 | for( i = 0; i < ROUNDS; i++ ) |
| 1617 | { |
| 1618 | for( j = 0; j < MSGLEN; j++ ) |
| 1619 | { |
| 1620 | message[i][j] = ( i * MSGLEN + j ) & 0xFF; |
| 1621 | } |
| 1622 | } |
| 1623 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1624 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1625 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1626 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1627 | /* Send the message from both sides, interleaving. */ |
| 1628 | progress = 1; |
| 1629 | for( i = 0; i < ROUNDS; i++ ) |
| 1630 | { |
| 1631 | written[i] = 0; |
| 1632 | read[i] = 0; |
| 1633 | } |
| 1634 | /* This loop does not stop as long as there was a successful write or read |
| 1635 | * of at least one byte on either side. */ |
| 1636 | while( progress != 0 ) |
| 1637 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1638 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1639 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1640 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1641 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1642 | /* First sending is from the client */ |
| 1643 | socket = ( i % 2 == 0 ) ? ( &client ) : ( &server ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1644 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1645 | send_ret[i] = send( socket, message[i] + written[i], |
| 1646 | MSGLEN - written[i] ); |
| 1647 | TEST_ASSERT( send_ret[i] >= 0 ); |
| 1648 | TEST_ASSERT( send_ret[i] <= BUFLEN ); |
| 1649 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1650 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1651 | /* If the buffer is full we can test blocking and non-blocking |
| 1652 | * send */ |
| 1653 | if ( send_ret[i] == BUFLEN ) |
| 1654 | { |
| 1655 | int blocking_ret = send( socket, message[i] , 1 ); |
| 1656 | if ( blocking ) |
| 1657 | { |
| 1658 | TEST_ASSERT( blocking_ret == 0 ); |
| 1659 | } |
| 1660 | else |
| 1661 | { |
| 1662 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1663 | } |
| 1664 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1665 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1666 | |
| 1667 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1668 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1669 | /* First receiving is from the server */ |
| 1670 | socket = ( i % 2 == 0 ) ? ( &server ) : ( &client ); |
| 1671 | |
| 1672 | recv_ret[i] = recv( socket, received[i] + read[i], |
| 1673 | MSGLEN - read[i] ); |
| 1674 | |
| 1675 | /* The result depends on whether any data was sent */ |
| 1676 | if ( send_ret[i] > 0 ) |
| 1677 | { |
| 1678 | TEST_ASSERT( recv_ret[i] > 0 ); |
| 1679 | TEST_ASSERT( recv_ret[i] <= BUFLEN ); |
| 1680 | read[i] += recv_ret[i]; |
| 1681 | } |
| 1682 | else if( blocking ) |
| 1683 | { |
| 1684 | TEST_ASSERT( recv_ret[i] == 0 ); |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1689 | recv_ret[i] = 0; |
| 1690 | } |
| 1691 | |
| 1692 | /* If the buffer is empty we can test blocking and non-blocking |
| 1693 | * read */ |
| 1694 | if ( recv_ret[i] == BUFLEN ) |
| 1695 | { |
| 1696 | int blocking_ret = recv( socket, received[i], 1 ); |
| 1697 | if ( blocking ) |
| 1698 | { |
| 1699 | TEST_ASSERT( blocking_ret == 0 ); |
| 1700 | } |
| 1701 | else |
| 1702 | { |
| 1703 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 1704 | } |
| 1705 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1706 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1707 | |
| 1708 | progress = 0; |
| 1709 | for( i = 0; i < ROUNDS; i++ ) |
| 1710 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 1711 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | for( i = 0; i < ROUNDS; i++ ) |
| 1716 | TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 ); |
| 1717 | |
| 1718 | exit: |
| 1719 | |
| 1720 | mbedtls_mock_socket_close( &client ); |
| 1721 | mbedtls_mock_socket_close( &server ); |
| 1722 | } |
| 1723 | /* END_CASE */ |
| 1724 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 1725 | /* BEGIN_CASE */ |
| 1726 | void ssl_message_queue_sanity( ) |
| 1727 | { |
| 1728 | mbedtls_test_message_queue queue; |
| 1729 | |
| 1730 | /* Trying to push/pull to an empty queue */ |
| 1731 | TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 ) |
| 1732 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 1733 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 ) |
| 1734 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 1735 | |
| 1736 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1737 | TEST_ASSERT( queue.capacity == 3 ); |
| 1738 | TEST_ASSERT( queue.num == 0 ); |
| 1739 | |
| 1740 | exit: |
| 1741 | mbedtls_test_message_queue_free( &queue ); |
| 1742 | } |
| 1743 | /* END_CASE */ |
| 1744 | |
| 1745 | /* BEGIN_CASE */ |
| 1746 | void ssl_message_queue_basic( ) |
| 1747 | { |
| 1748 | mbedtls_test_message_queue queue; |
| 1749 | |
| 1750 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1751 | |
| 1752 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
| 1753 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1754 | TEST_ASSERT( queue.capacity == 3 ); |
| 1755 | TEST_ASSERT( queue.num == 1 ); |
| 1756 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1757 | TEST_ASSERT( queue.capacity == 3 ); |
| 1758 | TEST_ASSERT( queue.num == 2 ); |
| 1759 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 1760 | TEST_ASSERT( queue.capacity == 3 ); |
| 1761 | TEST_ASSERT( queue.num == 3 ); |
| 1762 | |
| 1763 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1764 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1765 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 1766 | |
| 1767 | exit: |
| 1768 | mbedtls_test_message_queue_free( &queue ); |
| 1769 | } |
| 1770 | /* END_CASE */ |
| 1771 | |
| 1772 | /* BEGIN_CASE */ |
| 1773 | void ssl_message_queue_overflow_underflow( ) |
| 1774 | { |
| 1775 | mbedtls_test_message_queue queue; |
| 1776 | |
| 1777 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1778 | |
| 1779 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
| 1780 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1781 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1782 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 1783 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) |
| 1784 | == MBEDTLS_TEST_ERROR_QUEUE_FULL ); |
| 1785 | |
| 1786 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1787 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1788 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 1789 | |
| 1790 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) |
| 1791 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 1792 | |
| 1793 | exit: |
| 1794 | mbedtls_test_message_queue_free( &queue ); |
| 1795 | } |
| 1796 | /* END_CASE */ |
| 1797 | |
| 1798 | /* BEGIN_CASE */ |
| 1799 | void ssl_message_queue_interleaved( ) |
| 1800 | { |
| 1801 | mbedtls_test_message_queue queue; |
| 1802 | |
| 1803 | mbedtls_test_message_queue_setup( &queue, 3 ); |
| 1804 | |
| 1805 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 1806 | * (to wrap around the buffer) */ |
| 1807 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1808 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 1809 | |
| 1810 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1811 | |
| 1812 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 1813 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 ); |
| 1814 | |
| 1815 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 1816 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 1817 | |
| 1818 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 ); |
| 1819 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 ); |
| 1820 | |
| 1821 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 ); |
| 1822 | |
| 1823 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 ); |
| 1824 | |
| 1825 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 ); |
| 1826 | |
| 1827 | exit: |
| 1828 | mbedtls_test_message_queue_free( &queue ); |
| 1829 | } |
| 1830 | /* END_CASE */ |
| 1831 | |
| 1832 | /* BEGIN_CASE */ |
| 1833 | void ssl_message_queue_insufficient_buffer( ) |
| 1834 | { |
| 1835 | mbedtls_test_message_queue queue; |
| 1836 | size_t message_len = 10; |
| 1837 | size_t buffer_len = 5; |
| 1838 | |
| 1839 | mbedtls_test_message_queue_setup( &queue, 1 ); |
| 1840 | |
| 1841 | /* Popping without a sufficient buffer */ |
| 1842 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len ) |
| 1843 | == (int) message_len ); |
| 1844 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len ) |
| 1845 | == (int) buffer_len ); |
| 1846 | exit: |
| 1847 | mbedtls_test_message_queue_free( &queue ); |
| 1848 | } |
| 1849 | /* END_CASE */ |
| 1850 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1851 | /* BEGIN_CASE */ |
| 1852 | void ssl_message_mock_uninitialized( ) |
| 1853 | { |
| 1854 | enum { MSGLEN = 10 }; |
| 1855 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 1856 | mbedtls_mock_socket client, server; |
| 1857 | mbedtls_test_message_queue server_queue, client_queue; |
| 1858 | mbedtls_test_message_socket_context server_context, client_context; |
| 1859 | |
| 1860 | /* Send with a NULL context */ |
| 1861 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN ) |
| 1862 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 1863 | |
| 1864 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN ) |
| 1865 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 1866 | |
| 1867 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 1868 | &server, |
| 1869 | &server_context ) == 0 ); |
| 1870 | |
| 1871 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 1872 | &client, |
| 1873 | &client_context ) == 0 ); |
| 1874 | |
| 1875 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN ) |
| 1876 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 1877 | |
| 1878 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1879 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 1880 | |
| 1881 | /* Push directly to a queue to later simulate a disconnected behavior */ |
| 1882 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN ) |
| 1883 | == MSGLEN ); |
| 1884 | |
| 1885 | /* Test if there's an error when trying to read from a disconnected |
| 1886 | * socket */ |
| 1887 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1888 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 1889 | exit: |
| 1890 | mbedtls_message_socket_close( &server_context ); |
| 1891 | mbedtls_message_socket_close( &client_context ); |
| 1892 | } |
| 1893 | /* END_CASE */ |
| 1894 | |
| 1895 | /* BEGIN_CASE */ |
| 1896 | void ssl_message_mock_basic( ) |
| 1897 | { |
| 1898 | enum { MSGLEN = 10 }; |
| 1899 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 1900 | mbedtls_mock_socket client, server; |
| 1901 | unsigned i; |
| 1902 | mbedtls_test_message_queue server_queue, client_queue; |
| 1903 | mbedtls_test_message_socket_context server_context, client_context; |
| 1904 | |
| 1905 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 1906 | &server, |
| 1907 | &server_context ) == 0 ); |
| 1908 | |
| 1909 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 1910 | &client, |
| 1911 | &client_context ) == 0 ); |
| 1912 | |
| 1913 | /* Fill up the buffer with structured data so that unwanted changes |
| 1914 | * can be detected */ |
| 1915 | for( i = 0; i < MSGLEN; i++ ) |
| 1916 | { |
| 1917 | message[i] = i & 0xFF; |
| 1918 | } |
| 1919 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 1920 | MSGLEN ) ); |
| 1921 | |
| 1922 | /* Send the message to the server */ |
| 1923 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 1924 | MSGLEN ) == MSGLEN ); |
| 1925 | |
| 1926 | /* Read from the server */ |
| 1927 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1928 | == MSGLEN ); |
| 1929 | |
| 1930 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 1931 | memset( received, 0, MSGLEN ); |
| 1932 | |
| 1933 | /* Send the message to the client */ |
| 1934 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 1935 | MSGLEN ) == MSGLEN ); |
| 1936 | |
| 1937 | /* Read from the client */ |
| 1938 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 1939 | == MSGLEN ); |
| 1940 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 1941 | |
| 1942 | exit: |
| 1943 | mbedtls_message_socket_close( &server_context ); |
| 1944 | mbedtls_message_socket_close( &client_context ); |
| 1945 | } |
| 1946 | /* END_CASE */ |
| 1947 | |
| 1948 | /* BEGIN_CASE */ |
| 1949 | void ssl_message_mock_queue_overflow_underflow( ) |
| 1950 | { |
| 1951 | enum { MSGLEN = 10 }; |
| 1952 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 1953 | mbedtls_mock_socket client, server; |
| 1954 | unsigned i; |
| 1955 | mbedtls_test_message_queue server_queue, client_queue; |
| 1956 | mbedtls_test_message_socket_context server_context, client_context; |
| 1957 | |
| 1958 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 1959 | &server, |
| 1960 | &server_context ) == 0 ); |
| 1961 | |
| 1962 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 1963 | &client, |
| 1964 | &client_context ) == 0 ); |
| 1965 | |
| 1966 | /* Fill up the buffer with structured data so that unwanted changes |
| 1967 | * can be detected */ |
| 1968 | for( i = 0; i < MSGLEN; i++ ) |
| 1969 | { |
| 1970 | message[i] = i & 0xFF; |
| 1971 | } |
| 1972 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 1973 | MSGLEN*2 ) ); |
| 1974 | |
| 1975 | /* Send three message to the server, last one with an error */ |
| 1976 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 1977 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 1978 | |
| 1979 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 1980 | MSGLEN ) == MSGLEN ); |
| 1981 | |
| 1982 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 1983 | MSGLEN ) |
| 1984 | == MBEDTLS_TEST_ERROR_QUEUE_FULL ); |
| 1985 | |
| 1986 | /* Read three messages from the server, last one with an error */ |
| 1987 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 1988 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 1989 | |
| 1990 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1991 | == MSGLEN ); |
| 1992 | |
| 1993 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 1994 | |
| 1995 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 1996 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 1997 | |
| 1998 | exit: |
| 1999 | mbedtls_message_socket_close( &server_context ); |
| 2000 | mbedtls_message_socket_close( &client_context ); |
| 2001 | } |
| 2002 | /* END_CASE */ |
| 2003 | |
| 2004 | /* BEGIN_CASE */ |
| 2005 | void ssl_message_mock_socket_overflow( ) |
| 2006 | { |
| 2007 | enum { MSGLEN = 10 }; |
| 2008 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2009 | mbedtls_mock_socket client, server; |
| 2010 | unsigned i; |
| 2011 | mbedtls_test_message_queue server_queue, client_queue; |
| 2012 | mbedtls_test_message_socket_context server_context, client_context; |
| 2013 | |
| 2014 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2015 | &server, |
| 2016 | &server_context ) == 0 ); |
| 2017 | |
| 2018 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2019 | &client, |
| 2020 | &client_context ) == 0 ); |
| 2021 | |
| 2022 | /* Fill up the buffer with structured data so that unwanted changes |
| 2023 | * can be detected */ |
| 2024 | for( i = 0; i < MSGLEN; i++ ) |
| 2025 | { |
| 2026 | message[i] = i & 0xFF; |
| 2027 | } |
| 2028 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2029 | MSGLEN ) ); |
| 2030 | |
| 2031 | /* Send two message to the server, second one with an error */ |
| 2032 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2033 | MSGLEN ) == MSGLEN ); |
| 2034 | |
| 2035 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2036 | MSGLEN ) |
| 2037 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 2038 | |
| 2039 | /* Read the only message from the server */ |
| 2040 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2041 | == MSGLEN ); |
| 2042 | |
| 2043 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2044 | |
| 2045 | exit: |
| 2046 | mbedtls_message_socket_close( &server_context ); |
| 2047 | mbedtls_message_socket_close( &client_context ); |
| 2048 | } |
| 2049 | /* END_CASE */ |
| 2050 | |
| 2051 | /* BEGIN_CASE */ |
| 2052 | void ssl_message_mock_truncated( ) |
| 2053 | { |
| 2054 | enum { MSGLEN = 10 }; |
| 2055 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2056 | mbedtls_mock_socket client, server; |
| 2057 | unsigned i; |
| 2058 | mbedtls_test_message_queue server_queue, client_queue; |
| 2059 | mbedtls_test_message_socket_context server_context, client_context; |
| 2060 | |
| 2061 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2062 | &server, |
| 2063 | &server_context ) == 0 ); |
| 2064 | |
| 2065 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2066 | &client, |
| 2067 | &client_context ) == 0 ); |
| 2068 | |
| 2069 | memset( received, 0, MSGLEN ); |
| 2070 | /* Fill up the buffer with structured data so that unwanted changes |
| 2071 | * can be detected */ |
| 2072 | for( i = 0; i < MSGLEN; i++ ) |
| 2073 | { |
| 2074 | message[i] = i & 0xFF; |
| 2075 | } |
| 2076 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2077 | 2 * MSGLEN ) ); |
| 2078 | |
| 2079 | /* Send two messages to the server, the second one small enough to fit in the |
| 2080 | * receiver's buffer. */ |
| 2081 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2082 | MSGLEN ) == MSGLEN ); |
| 2083 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2084 | MSGLEN / 2 ) == MSGLEN / 2 ); |
| 2085 | /* Read a truncated message from the server */ |
| 2086 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 2087 | == MSGLEN/2 ); |
| 2088 | |
| 2089 | /* Test that the first half of the message is valid, and second one isn't */ |
| 2090 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 2091 | TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 ) |
| 2092 | != 0 ); |
| 2093 | memset( received, 0, MSGLEN ); |
| 2094 | |
| 2095 | /* Read a full message from the server */ |
| 2096 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 2097 | == MSGLEN / 2 ); |
| 2098 | |
| 2099 | /* Test that the first half of the message is valid */ |
| 2100 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 2101 | |
| 2102 | exit: |
| 2103 | mbedtls_message_socket_close( &server_context ); |
| 2104 | mbedtls_message_socket_close( &client_context ); |
| 2105 | } |
| 2106 | /* END_CASE */ |
| 2107 | |
| 2108 | /* BEGIN_CASE */ |
| 2109 | void ssl_message_mock_socket_read_error( ) |
| 2110 | { |
| 2111 | enum { MSGLEN = 10 }; |
| 2112 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2113 | mbedtls_mock_socket client, server; |
| 2114 | unsigned i; |
| 2115 | mbedtls_test_message_queue server_queue, client_queue; |
| 2116 | mbedtls_test_message_socket_context server_context, client_context; |
| 2117 | |
| 2118 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 2119 | &server, |
| 2120 | &server_context ) == 0 ); |
| 2121 | |
| 2122 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 2123 | &client, |
| 2124 | &client_context ) == 0 ); |
| 2125 | |
| 2126 | /* Fill up the buffer with structured data so that unwanted changes |
| 2127 | * can be detected */ |
| 2128 | for( i = 0; i < MSGLEN; i++ ) |
| 2129 | { |
| 2130 | message[i] = i & 0xFF; |
| 2131 | } |
| 2132 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2133 | MSGLEN ) ); |
| 2134 | |
| 2135 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2136 | MSGLEN ) == MSGLEN ); |
| 2137 | |
| 2138 | /* Force a read error by disconnecting the socket by hand */ |
| 2139 | server.status = 0; |
| 2140 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2141 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 2142 | /* Return to a valid state */ |
| 2143 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 2144 | |
| 2145 | memset( received, 0, sizeof( received ) ); |
| 2146 | |
| 2147 | /* Test that even though the server tried to read once disconnected, the |
| 2148 | * continuity is preserved */ |
| 2149 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2150 | == MSGLEN ); |
| 2151 | |
| 2152 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2153 | |
| 2154 | exit: |
| 2155 | mbedtls_message_socket_close( &server_context ); |
| 2156 | mbedtls_message_socket_close( &client_context ); |
| 2157 | } |
| 2158 | /* END_CASE */ |
| 2159 | |
| 2160 | /* BEGIN_CASE */ |
| 2161 | void ssl_message_mock_interleaved_one_way( ) |
| 2162 | { |
| 2163 | enum { MSGLEN = 10 }; |
| 2164 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2165 | mbedtls_mock_socket client, server; |
| 2166 | unsigned i; |
| 2167 | mbedtls_test_message_queue server_queue, client_queue; |
| 2168 | mbedtls_test_message_socket_context server_context, client_context; |
| 2169 | |
| 2170 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 2171 | &server, |
| 2172 | &server_context ) == 0 ); |
| 2173 | |
| 2174 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 2175 | &client, |
| 2176 | &client_context ) == 0 ); |
| 2177 | |
| 2178 | /* Fill up the buffer with structured data so that unwanted changes |
| 2179 | * can be detected */ |
| 2180 | for( i = 0; i < MSGLEN; i++ ) |
| 2181 | { |
| 2182 | message[i] = i & 0xFF; |
| 2183 | } |
| 2184 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2185 | MSGLEN*3 ) ); |
| 2186 | |
| 2187 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 2188 | * (to wrap around the buffer) */ |
| 2189 | for( i = 0; i < 2; i++ ) |
| 2190 | { |
| 2191 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2192 | MSGLEN ) == MSGLEN ); |
| 2193 | |
| 2194 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2195 | MSGLEN ) == MSGLEN ); |
| 2196 | |
| 2197 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2198 | MSGLEN ) == MSGLEN ); |
| 2199 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2200 | memset( received, 0, sizeof( received ) ); |
| 2201 | } |
| 2202 | |
| 2203 | for( i = 0; i < 2; i++ ) |
| 2204 | { |
| 2205 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2206 | MSGLEN ) == MSGLEN ); |
| 2207 | |
| 2208 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2209 | } |
| 2210 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2211 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2212 | exit: |
| 2213 | mbedtls_message_socket_close( &server_context ); |
| 2214 | mbedtls_message_socket_close( &client_context ); |
| 2215 | } |
| 2216 | /* END_CASE */ |
| 2217 | |
| 2218 | /* BEGIN_CASE */ |
| 2219 | void ssl_message_mock_interleaved_two_ways( ) |
| 2220 | { |
| 2221 | enum { MSGLEN = 10 }; |
| 2222 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2223 | mbedtls_mock_socket client, server; |
| 2224 | unsigned i; |
| 2225 | mbedtls_test_message_queue server_queue, client_queue; |
| 2226 | mbedtls_test_message_socket_context server_context, client_context; |
| 2227 | |
| 2228 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 2229 | &server, |
| 2230 | &server_context ) == 0 ); |
| 2231 | |
| 2232 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 2233 | &client, |
| 2234 | &client_context ) == 0 ); |
| 2235 | |
| 2236 | /* Fill up the buffer with structured data so that unwanted changes |
| 2237 | * can be detected */ |
| 2238 | for( i = 0; i < MSGLEN; i++ ) |
| 2239 | { |
| 2240 | message[i] = i & 0xFF; |
| 2241 | } |
| 2242 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2243 | MSGLEN*3 ) ); |
| 2244 | |
| 2245 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 2246 | * (to wrap around the buffer) both ways. */ |
| 2247 | for( i = 0; i < 2; i++ ) |
| 2248 | { |
| 2249 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2250 | MSGLEN ) == MSGLEN ); |
| 2251 | |
| 2252 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2253 | MSGLEN ) == MSGLEN ); |
| 2254 | |
| 2255 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 2256 | MSGLEN ) == MSGLEN ); |
| 2257 | |
| 2258 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 2259 | MSGLEN ) == MSGLEN ); |
| 2260 | |
| 2261 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2262 | MSGLEN ) == MSGLEN ); |
| 2263 | |
| 2264 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2265 | |
| 2266 | memset( received, 0, sizeof( received ) ); |
| 2267 | |
| 2268 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 2269 | MSGLEN ) == MSGLEN ); |
| 2270 | |
| 2271 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2272 | |
| 2273 | memset( received, 0, sizeof( received ) ); |
| 2274 | } |
| 2275 | |
| 2276 | for( i = 0; i < 2; i++ ) |
| 2277 | { |
| 2278 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2279 | MSGLEN ) == MSGLEN ); |
| 2280 | |
| 2281 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2282 | memset( received, 0, sizeof( received ) ); |
| 2283 | |
| 2284 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 2285 | MSGLEN ) == MSGLEN ); |
| 2286 | |
| 2287 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2288 | memset( received, 0, sizeof( received ) ); |
| 2289 | } |
| 2290 | |
| 2291 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2292 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2293 | |
| 2294 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 2295 | == MBEDTLS_TEST_ERROR_QUEUE_EMPTY ); |
| 2296 | exit: |
| 2297 | mbedtls_message_socket_close( &server_context ); |
| 2298 | mbedtls_message_socket_close( &client_context ); |
| 2299 | } |
| 2300 | /* END_CASE */ |
| 2301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2302 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 2303 | 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] | 2304 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2305 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2306 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2307 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2308 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 2309 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2310 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 2311 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 2312 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 2313 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 2314 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 2315 | MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2316 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2317 | |
| 2318 | /* Read previous record numbers */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2319 | for( len = 0; len < prevs->len; len += 6 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2320 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2321 | memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2322 | mbedtls_ssl_dtls_replay_update( &ssl ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | /* Check new number */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 2326 | memcpy( ssl.in_ctr + 2, new->x, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2327 | TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2328 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2329 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2330 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2331 | } |
| 2332 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 2333 | |
| 2334 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 2335 | void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) |
| 2336 | { |
| 2337 | mbedtls_ssl_context ssl; |
| 2338 | mbedtls_ssl_init( &ssl ); |
| 2339 | |
| 2340 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); |
| 2341 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); |
| 2342 | |
| 2343 | mbedtls_ssl_free( &ssl ); |
| 2344 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 2345 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2346 | |
| 2347 | /* BEGIN_CASE */ |
| 2348 | void ssl_crypt_record( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2349 | int etm, int tag_mode, int ver, |
| 2350 | int cid0_len, int cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2351 | { |
| 2352 | /* |
| 2353 | * Test several record encryptions and decryptions |
| 2354 | * with plenty of space before and after the data |
| 2355 | * within the record buffer. |
| 2356 | */ |
| 2357 | |
| 2358 | int ret; |
| 2359 | int num_records = 16; |
| 2360 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 2361 | |
| 2362 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2363 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2364 | size_t const buflen = 512; |
| 2365 | mbedtls_record rec, rec_backup; |
| 2366 | |
| 2367 | mbedtls_ssl_init( &ssl ); |
| 2368 | mbedtls_ssl_transform_init( &t0 ); |
| 2369 | mbedtls_ssl_transform_init( &t1 ); |
| 2370 | TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2371 | etm, tag_mode, ver, |
| 2372 | (size_t) cid0_len, |
| 2373 | (size_t) cid1_len ) == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2374 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2375 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2376 | |
| 2377 | while( num_records-- > 0 ) |
| 2378 | { |
| 2379 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 2380 | /* Take turns in who's sending and who's receiving. */ |
| 2381 | if( num_records % 3 == 0 ) |
| 2382 | { |
| 2383 | t_dec = &t0; |
| 2384 | t_enc = &t1; |
| 2385 | } |
| 2386 | else |
| 2387 | { |
| 2388 | t_dec = &t1; |
| 2389 | t_enc = &t0; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * The record header affects the transformation in two ways: |
| 2394 | * 1) It determines the AEAD additional data |
| 2395 | * 2) The record counter sometimes determines the IV. |
| 2396 | * |
| 2397 | * Apart from that, the fields don't have influence. |
| 2398 | * In particular, it is currently not the responsibility |
| 2399 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 2400 | * version matches the record version, or that the |
| 2401 | * type is sensible. |
| 2402 | */ |
| 2403 | |
| 2404 | memset( rec.ctr, num_records, sizeof( rec.ctr ) ); |
| 2405 | rec.type = 42; |
| 2406 | rec.ver[0] = num_records; |
| 2407 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2408 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2409 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2410 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2411 | |
| 2412 | rec.buf = buf; |
| 2413 | rec.buf_len = buflen; |
| 2414 | rec.data_offset = 16; |
| 2415 | /* Make sure to vary the length to exercise different |
| 2416 | * paddings. */ |
| 2417 | rec.data_len = 1 + num_records; |
| 2418 | |
| 2419 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 2420 | |
| 2421 | /* Make a copy for later comparison */ |
| 2422 | rec_backup = rec; |
| 2423 | |
| 2424 | /* Encrypt record */ |
| 2425 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
| 2426 | rnd_std_rand, NULL ); |
| 2427 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2428 | if( ret != 0 ) |
| 2429 | { |
| 2430 | continue; |
| 2431 | } |
| 2432 | |
| 2433 | /* Decrypt record with t_dec */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2434 | ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ); |
| 2435 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2436 | |
| 2437 | /* Compare results */ |
| 2438 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 2439 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 2440 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 2441 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 2442 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 2443 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 2444 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 2445 | rec_backup.buf + rec_backup.data_offset, |
| 2446 | rec.data_len ) == 0 ); |
| 2447 | } |
| 2448 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2449 | exit: |
| 2450 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2451 | /* Cleanup */ |
| 2452 | mbedtls_ssl_free( &ssl ); |
| 2453 | mbedtls_ssl_transform_free( &t0 ); |
| 2454 | mbedtls_ssl_transform_free( &t1 ); |
| 2455 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2456 | mbedtls_free( buf ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2457 | } |
| 2458 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2459 | |
| 2460 | /* BEGIN_CASE */ |
| 2461 | void ssl_crypt_record_small( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2462 | int etm, int tag_mode, int ver, |
| 2463 | int cid0_len, int cid1_len ) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2464 | { |
| 2465 | /* |
| 2466 | * Test pairs of encryption and decryption with an increasing |
| 2467 | * amount of space in the record buffer - in more detail: |
| 2468 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 2469 | * in front of the plaintext, and expect the encryption |
| 2470 | * to succeed starting from some offset. Always keep |
| 2471 | * enough space in the end of the buffer. |
| 2472 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 2473 | * at the end of the plaintext, and expect the encryption |
| 2474 | * to succeed starting from some offset. Always keep |
| 2475 | * enough space at the beginning of the buffer. |
| 2476 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 2477 | * both at the front and end of the plaintext, |
| 2478 | * and expect the encryption to succeed starting from |
| 2479 | * some offset. |
| 2480 | * |
| 2481 | * If encryption succeeds, check that decryption succeeds |
| 2482 | * and yields the original record. |
| 2483 | */ |
| 2484 | |
| 2485 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 2486 | |
| 2487 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2488 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2489 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2490 | mbedtls_record rec, rec_backup; |
| 2491 | |
| 2492 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2493 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 2494 | size_t offset; /* Available space at beginning/end/both */ |
| 2495 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2496 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2497 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 2498 | 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] | 2499 | |
| 2500 | int seen_success; /* Indicates if in the current mode we've |
| 2501 | * already seen a successful test. */ |
| 2502 | |
| 2503 | mbedtls_ssl_init( &ssl ); |
| 2504 | mbedtls_ssl_transform_init( &t0 ); |
| 2505 | mbedtls_ssl_transform_init( &t1 ); |
| 2506 | TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2507 | etm, tag_mode, ver, |
| 2508 | (size_t) cid0_len, |
| 2509 | (size_t) cid1_len ) == 0 ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2510 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2511 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2512 | |
| 2513 | for( mode=1; mode <= 3; mode++ ) |
| 2514 | { |
| 2515 | seen_success = 0; |
| 2516 | for( offset=0; offset <= threshold; offset++ ) |
| 2517 | { |
| 2518 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 2519 | t_dec = &t0; |
| 2520 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2521 | |
| 2522 | memset( rec.ctr, offset, sizeof( rec.ctr ) ); |
| 2523 | rec.type = 42; |
| 2524 | rec.ver[0] = offset; |
| 2525 | rec.ver[1] = offset; |
| 2526 | rec.buf = buf; |
| 2527 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2528 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 2529 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2530 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2531 | |
| 2532 | switch( mode ) |
| 2533 | { |
| 2534 | case 1: /* Space in the beginning */ |
| 2535 | rec.data_offset = offset; |
| 2536 | rec.data_len = buflen - offset - default_post_padding; |
| 2537 | break; |
| 2538 | |
| 2539 | case 2: /* Space in the end */ |
| 2540 | rec.data_offset = default_pre_padding; |
| 2541 | rec.data_len = buflen - default_pre_padding - offset; |
| 2542 | break; |
| 2543 | |
| 2544 | case 3: /* Space in the beginning and end */ |
| 2545 | rec.data_offset = offset; |
| 2546 | rec.data_len = buflen - 2 * offset; |
| 2547 | break; |
| 2548 | |
| 2549 | default: |
| 2550 | TEST_ASSERT( 0 ); |
| 2551 | break; |
| 2552 | } |
| 2553 | |
| 2554 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 2555 | |
| 2556 | /* Make a copy for later comparison */ |
| 2557 | rec_backup = rec; |
| 2558 | |
| 2559 | /* Encrypt record */ |
| 2560 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL ); |
| 2561 | |
| 2562 | if( ( mode == 1 || mode == 2 ) && seen_success ) |
| 2563 | { |
| 2564 | TEST_ASSERT( ret == 0 ); |
| 2565 | } |
| 2566 | else |
| 2567 | { |
| 2568 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2569 | if( ret == 0 ) |
| 2570 | seen_success = 1; |
| 2571 | } |
| 2572 | |
| 2573 | if( ret != 0 ) |
| 2574 | continue; |
| 2575 | |
| 2576 | /* Decrypt record with t_dec */ |
| 2577 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 ); |
| 2578 | |
| 2579 | /* Compare results */ |
| 2580 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 2581 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 2582 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 2583 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 2584 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 2585 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 2586 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 2587 | rec_backup.buf + rec_backup.data_offset, |
| 2588 | rec.data_len ) == 0 ); |
| 2589 | } |
| 2590 | |
| 2591 | TEST_ASSERT( seen_success == 1 ); |
| 2592 | } |
| 2593 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 2594 | exit: |
| 2595 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2596 | /* Cleanup */ |
| 2597 | mbedtls_ssl_free( &ssl ); |
| 2598 | mbedtls_ssl_transform_free( &t0 ); |
| 2599 | mbedtls_ssl_transform_free( &t1 ); |
| 2600 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 2601 | mbedtls_free( buf ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 2602 | } |
| 2603 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 2604 | |
| 2605 | /* BEGIN_CASE */ |
| 2606 | void ssl_tls_prf( int type, data_t * secret, data_t * random, |
| 2607 | char *label, data_t *result_hex_str, int exp_ret ) |
| 2608 | { |
| 2609 | unsigned char *output; |
| 2610 | |
| 2611 | output = mbedtls_calloc( 1, result_hex_str->len ); |
| 2612 | if( output == NULL ) |
| 2613 | goto exit; |
| 2614 | |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 2615 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2616 | TEST_ASSERT( psa_crypto_init() == 0 ); |
| 2617 | #endif |
| 2618 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 2619 | TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len, |
| 2620 | label, random->x, random->len, |
| 2621 | output, result_hex_str->len ) == exp_ret ); |
| 2622 | |
| 2623 | if( exp_ret == 0 ) |
| 2624 | { |
| 2625 | TEST_ASSERT( hexcmp( output, result_hex_str->x, |
| 2626 | result_hex_str->len, result_hex_str->len ) == 0 ); |
| 2627 | } |
| 2628 | exit: |
| 2629 | |
| 2630 | mbedtls_free( output ); |
| 2631 | } |
| 2632 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2633 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2634 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2635 | 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] | 2636 | { |
| 2637 | mbedtls_ssl_session original, restored; |
| 2638 | unsigned char *buf = NULL; |
| 2639 | size_t len; |
| 2640 | |
| 2641 | /* |
| 2642 | * Test that a save-load pair is the identity |
| 2643 | */ |
| 2644 | |
| 2645 | mbedtls_ssl_session_init( &original ); |
| 2646 | mbedtls_ssl_session_init( &restored ); |
| 2647 | |
| 2648 | /* Prepare a dummy session to work on */ |
| 2649 | TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 ); |
| 2650 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2651 | /* Serialize it */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2652 | TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len ) |
| 2653 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2654 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL ); |
| 2655 | TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len ) |
| 2656 | == 0 ); |
| 2657 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2658 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2659 | TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 ); |
| 2660 | |
| 2661 | /* |
| 2662 | * Make sure both session structures are identical |
| 2663 | */ |
| 2664 | #if defined(MBEDTLS_HAVE_TIME) |
| 2665 | TEST_ASSERT( original.start == restored.start ); |
| 2666 | #endif |
| 2667 | TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); |
| 2668 | TEST_ASSERT( original.compression == restored.compression ); |
| 2669 | TEST_ASSERT( original.id_len == restored.id_len ); |
| 2670 | TEST_ASSERT( memcmp( original.id, |
| 2671 | restored.id, sizeof( original.id ) ) == 0 ); |
| 2672 | TEST_ASSERT( memcmp( original.master, |
| 2673 | restored.master, sizeof( original.master ) ) == 0 ); |
| 2674 | |
| 2675 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2676 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2677 | TEST_ASSERT( ( original.peer_cert == NULL ) == |
| 2678 | ( restored.peer_cert == NULL ) ); |
| 2679 | if( original.peer_cert != NULL ) |
| 2680 | { |
| 2681 | TEST_ASSERT( original.peer_cert->raw.len == |
| 2682 | restored.peer_cert->raw.len ); |
| 2683 | TEST_ASSERT( memcmp( original.peer_cert->raw.p, |
| 2684 | restored.peer_cert->raw.p, |
| 2685 | original.peer_cert->raw.len ) == 0 ); |
| 2686 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2687 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2688 | TEST_ASSERT( original.peer_cert_digest_type == |
| 2689 | restored.peer_cert_digest_type ); |
| 2690 | TEST_ASSERT( original.peer_cert_digest_len == |
| 2691 | restored.peer_cert_digest_len ); |
| 2692 | TEST_ASSERT( ( original.peer_cert_digest == NULL ) == |
| 2693 | ( restored.peer_cert_digest == NULL ) ); |
| 2694 | if( original.peer_cert_digest != NULL ) |
| 2695 | { |
| 2696 | TEST_ASSERT( memcmp( original.peer_cert_digest, |
| 2697 | restored.peer_cert_digest, |
| 2698 | original.peer_cert_digest_len ) == 0 ); |
| 2699 | } |
| 2700 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2701 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2702 | TEST_ASSERT( original.verify_result == restored.verify_result ); |
| 2703 | |
| 2704 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 2705 | TEST_ASSERT( original.ticket_len == restored.ticket_len ); |
| 2706 | if( original.ticket_len != 0 ) |
| 2707 | { |
| 2708 | TEST_ASSERT( original.ticket != NULL ); |
| 2709 | TEST_ASSERT( restored.ticket != NULL ); |
| 2710 | TEST_ASSERT( memcmp( original.ticket, |
| 2711 | restored.ticket, original.ticket_len ) == 0 ); |
| 2712 | } |
| 2713 | TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime ); |
| 2714 | #endif |
| 2715 | |
| 2716 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2717 | TEST_ASSERT( original.mfl_code == restored.mfl_code ); |
| 2718 | #endif |
| 2719 | |
| 2720 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2721 | TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac ); |
| 2722 | #endif |
| 2723 | |
| 2724 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2725 | TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac ); |
| 2726 | #endif |
| 2727 | |
| 2728 | exit: |
| 2729 | mbedtls_ssl_session_free( &original ); |
| 2730 | mbedtls_ssl_session_free( &restored ); |
| 2731 | mbedtls_free( buf ); |
| 2732 | } |
| 2733 | /* END_CASE */ |
| 2734 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 2735 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2736 | 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] | 2737 | { |
| 2738 | mbedtls_ssl_session session; |
| 2739 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 2740 | size_t len0, len1, len2; |
| 2741 | |
| 2742 | /* |
| 2743 | * Test that a load-save pair is the identity |
| 2744 | */ |
| 2745 | |
| 2746 | mbedtls_ssl_session_init( &session ); |
| 2747 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 2748 | /* Prepare a dummy session to work on */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 2749 | 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] | 2750 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2751 | /* Get desired buffer size for serializing */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2752 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 ) |
| 2753 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2754 | |
| 2755 | /* Allocate first buffer */ |
| 2756 | buf1 = mbedtls_calloc( 1, len0 ); |
| 2757 | TEST_ASSERT( buf1 != NULL ); |
| 2758 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2759 | /* Serialize to buffer and free live session */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2760 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 ) |
| 2761 | == 0 ); |
| 2762 | TEST_ASSERT( len0 == len1 ); |
| 2763 | mbedtls_ssl_session_free( &session ); |
| 2764 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2765 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 2766 | TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2767 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2768 | /* Allocate second buffer and serialize to it */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2769 | buf2 = mbedtls_calloc( 1, len0 ); |
Manuel Pégourié-Gonnard | b407990 | 2019-05-24 09:52:10 +0200 | [diff] [blame] | 2770 | TEST_ASSERT( buf2 != NULL ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2771 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 ) |
| 2772 | == 0 ); |
| 2773 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2774 | /* Make sure both serialized versions are identical */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2775 | TEST_ASSERT( len1 == len2 ); |
| 2776 | TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 ); |
| 2777 | |
| 2778 | exit: |
| 2779 | mbedtls_ssl_session_free( &session ); |
| 2780 | mbedtls_free( buf1 ); |
| 2781 | mbedtls_free( buf2 ); |
| 2782 | } |
| 2783 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2784 | |
| 2785 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2786 | 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] | 2787 | { |
| 2788 | mbedtls_ssl_session session; |
| 2789 | unsigned char *buf = NULL; |
| 2790 | size_t good_len, bad_len, test_len; |
| 2791 | |
| 2792 | /* |
| 2793 | * Test that session_save() fails cleanly on small buffers |
| 2794 | */ |
| 2795 | |
| 2796 | mbedtls_ssl_session_init( &session ); |
| 2797 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2798 | /* Prepare dummy session and get serialized size */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 2799 | 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] | 2800 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 2801 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2802 | |
| 2803 | /* Try all possible bad lengths */ |
| 2804 | for( bad_len = 1; bad_len < good_len; bad_len++ ) |
| 2805 | { |
| 2806 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
| 2807 | mbedtls_free( buf ); |
| 2808 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL ); |
| 2809 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len, |
| 2810 | &test_len ) |
| 2811 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2812 | TEST_ASSERT( test_len == good_len ); |
| 2813 | } |
| 2814 | |
| 2815 | exit: |
| 2816 | mbedtls_ssl_session_free( &session ); |
| 2817 | mbedtls_free( buf ); |
| 2818 | } |
| 2819 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2820 | |
| 2821 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2822 | 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] | 2823 | { |
| 2824 | mbedtls_ssl_session session; |
| 2825 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 2826 | size_t good_len, bad_len; |
| 2827 | |
| 2828 | /* |
| 2829 | * Test that session_load() fails cleanly on small buffers |
| 2830 | */ |
| 2831 | |
| 2832 | mbedtls_ssl_session_init( &session ); |
| 2833 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2834 | /* Prepare serialized session data */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 2835 | 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] | 2836 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 2837 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2838 | TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL ); |
| 2839 | TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len, |
| 2840 | &good_len ) == 0 ); |
| 2841 | mbedtls_ssl_session_free( &session ); |
| 2842 | |
| 2843 | /* Try all possible bad lengths */ |
| 2844 | for( bad_len = 0; bad_len < good_len; bad_len++ ) |
| 2845 | { |
| 2846 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
| 2847 | mbedtls_free( bad_buf ); |
| 2848 | bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 ); |
| 2849 | TEST_ASSERT( bad_buf != NULL ); |
| 2850 | memcpy( bad_buf, good_buf, bad_len ); |
| 2851 | |
| 2852 | TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len ) |
| 2853 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2854 | } |
| 2855 | |
| 2856 | exit: |
| 2857 | mbedtls_ssl_session_free( &session ); |
| 2858 | mbedtls_free( good_buf ); |
| 2859 | mbedtls_free( bad_buf ); |
| 2860 | } |
| 2861 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2862 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2863 | /* BEGIN_CASE */ |
| 2864 | void ssl_session_serialize_version_check( int corrupt_major, |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2865 | int corrupt_minor, |
| 2866 | int corrupt_patch, |
| 2867 | int corrupt_config ) |
| 2868 | { |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2869 | unsigned char serialized_session[ 2048 ]; |
| 2870 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2871 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2872 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2873 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 2874 | corrupt_minor == 1, |
| 2875 | corrupt_patch == 1, |
| 2876 | corrupt_config == 1, |
| 2877 | corrupt_config == 1 }; |
| 2878 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2879 | mbedtls_ssl_session_init( &session ); |
| 2880 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2881 | /* Infer length of serialized session. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2882 | TEST_ASSERT( mbedtls_ssl_session_save( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2883 | serialized_session, |
| 2884 | sizeof( serialized_session ), |
| 2885 | &serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2886 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2887 | mbedtls_ssl_session_free( &session ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2888 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2889 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2890 | * de-serialize the session - double-check that. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2891 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2892 | serialized_session, |
| 2893 | serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2894 | mbedtls_ssl_session_free( &session ); |
| 2895 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2896 | /* Go through the bytes in the serialized session header and |
| 2897 | * corrupt them bit-by-bit. */ |
| 2898 | for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ ) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2899 | { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2900 | int cur_bit; |
| 2901 | unsigned char * const byte = &serialized_session[ cur_byte ]; |
| 2902 | |
| 2903 | if( should_corrupt_byte[ cur_byte ] == 0 ) |
| 2904 | continue; |
| 2905 | |
| 2906 | for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ ) |
| 2907 | { |
| 2908 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 2909 | /* Modify a single bit in the serialized session. */ |
| 2910 | *byte ^= corrupted_bit; |
| 2911 | |
| 2912 | /* Attempt to deserialize */ |
| 2913 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
| 2914 | serialized_session, |
| 2915 | serialized_session_len ) == |
Hanno Becker | f9b3303 | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 2916 | MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2917 | |
| 2918 | /* Undo the change */ |
| 2919 | *byte ^= corrupted_bit; |
| 2920 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2923 | } |
| 2924 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2925 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2926 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2927 | void mbedtls_endpoint_sanity( int endpoint_type ) |
| 2928 | { |
| 2929 | enum { BUFFSIZE = 1024 }; |
| 2930 | mbedtls_endpoint ep; |
| 2931 | int ret = -1; |
| 2932 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2933 | ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2934 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 2935 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2936 | ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2937 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 2938 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2939 | ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2940 | TEST_ASSERT( ret == 0 ); |
| 2941 | |
| 2942 | exit: |
| 2943 | mbedtls_endpoint_free( &ep ); |
| 2944 | } |
| 2945 | /* END_CASE */ |
| 2946 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2947 | /* 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] | 2948 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 2949 | { |
| 2950 | enum { BUFFSIZE = 1024 }; |
| 2951 | mbedtls_endpoint base_ep, second_ep; |
| 2952 | int ret = -1; |
| 2953 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2954 | ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2955 | TEST_ASSERT( ret == 0 ); |
| 2956 | |
| 2957 | ret = mbedtls_endpoint_init( &second_ep, |
| 2958 | ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame^] | 2959 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 2960 | MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2961 | TEST_ASSERT( ret == 0 ); |
| 2962 | |
| 2963 | ret = mbedtls_mock_socket_connect( &(base_ep.socket), |
| 2964 | &(second_ep.socket), |
| 2965 | BUFFSIZE ); |
| 2966 | TEST_ASSERT( ret == 0 ); |
| 2967 | |
| 2968 | ret = mbedtls_move_handshake_to_state( &(base_ep.ssl), |
| 2969 | &(second_ep.ssl), |
| 2970 | state ); |
| 2971 | if( need_pass ) |
| 2972 | { |
| 2973 | TEST_ASSERT( ret == 0 ); |
| 2974 | TEST_ASSERT( base_ep.ssl.state == state ); |
| 2975 | } |
| 2976 | else |
| 2977 | { |
| 2978 | TEST_ASSERT( ret != 0 ); |
| 2979 | TEST_ASSERT( base_ep.ssl.state != state ); |
| 2980 | } |
| 2981 | |
| 2982 | exit: |
| 2983 | mbedtls_endpoint_free( &base_ep ); |
| 2984 | mbedtls_endpoint_free( &second_ep ); |
| 2985 | } |
| 2986 | /* END_CASE */ |