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