1 /** \file ssl_helpers.c
2 *
3 * \brief Helper functions to set up a TLS connection.
4 */
5
6 /*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23 #include <test/ssl_helpers.h>
24
25 #if defined(MBEDTLS_SSL_TLS_C)
26 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
27 static int rng_seed = 0xBEEF;
rng_get(void * p_rng,unsigned char * output,size_t output_len)28 static int rng_get(void *p_rng, unsigned char *output, size_t output_len)
29 {
30 (void) p_rng;
31 for (size_t i = 0; i < output_len; i++) {
32 output[i] = rand();
33 }
34
35 return 0;
36 }
37 #endif
38
mbedtls_test_ssl_log_analyzer(void * ctx,int level,const char * file,int line,const char * str)39 void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
40 const char *file, int line,
41 const char *str)
42 {
43 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
44
45 (void) level;
46 (void) line;
47 (void) file;
48
49 if (NULL != p &&
50 NULL != p->pattern &&
51 NULL != strstr(str, p->pattern)) {
52 p->counter++;
53 }
54 }
55
mbedtls_test_init_handshake_options(mbedtls_test_handshake_test_options * opts)56 void mbedtls_test_init_handshake_options(
57 mbedtls_test_handshake_test_options *opts)
58 {
59 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
60 srand(rng_seed);
61 rng_seed += 0xD0;
62 #endif
63 opts->cipher = "";
64 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
65 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
66 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
67 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
68 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2;
69 opts->expected_handshake_result = 0;
70 opts->expected_ciphersuite = 0;
71 opts->pk_alg = MBEDTLS_PK_RSA;
72 opts->opaque_alg = 0;
73 opts->opaque_alg2 = 0;
74 opts->opaque_usage = 0;
75 opts->psk_str = NULL;
76 opts->dtls = 0;
77 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
78 opts->serialize = 0;
79 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
80 opts->cli_msg_len = 100;
81 opts->srv_msg_len = 100;
82 opts->expected_cli_fragments = 1;
83 opts->expected_srv_fragments = 1;
84 opts->renegotiate = 0;
85 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
86 opts->srv_log_obj = NULL;
87 opts->srv_log_obj = NULL;
88 opts->srv_log_fun = NULL;
89 opts->cli_log_fun = NULL;
90 opts->resize_buffers = 1;
91 #if defined(MBEDTLS_SSL_CACHE_C)
92 opts->cache = NULL;
93 ASSERT_ALLOC(opts->cache, 1);
94 mbedtls_ssl_cache_init(opts->cache);
95 exit:
96 return;
97 #endif
98 }
99
mbedtls_test_free_handshake_options(mbedtls_test_handshake_test_options * opts)100 void mbedtls_test_free_handshake_options(
101 mbedtls_test_handshake_test_options *opts)
102 {
103 #if defined(MBEDTLS_SSL_CACHE_C)
104 mbedtls_ssl_cache_free(opts->cache);
105 mbedtls_free(opts->cache);
106 #else
107 (void) opts;
108 #endif
109 }
110
111 #if defined(MBEDTLS_TEST_HOOKS)
set_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args * args,unsigned char * cur,unsigned char * end,size_t need)112 static void set_chk_buf_ptr_args(
113 mbedtls_ssl_chk_buf_ptr_args *args,
114 unsigned char *cur, unsigned char *end, size_t need)
115 {
116 args->cur = cur;
117 args->end = end;
118 args->need = need;
119 }
120
reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args * args)121 static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
122 {
123 memset(args, 0, sizeof(*args));
124 }
125 #endif /* MBEDTLS_TEST_HOOKS */
126
127 /*
128 * Buffer structure for custom I/O callbacks.
129 */
130
mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer * buf)131 void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
132 {
133 memset(buf, 0, sizeof(*buf));
134 }
135
mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer * buf,size_t capacity)136 int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
137 size_t capacity)
138 {
139 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
140 sizeof(unsigned char));
141 if (NULL == buf->buffer) {
142 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
143 }
144 buf->capacity = capacity;
145
146 return 0;
147 }
148
mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer * buf)149 void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
150 {
151 if (buf->buffer != NULL) {
152 mbedtls_free(buf->buffer);
153 }
154
155 memset(buf, 0, sizeof(*buf));
156 }
157
mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer * buf,const unsigned char * input,size_t input_len)158 int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
159 const unsigned char *input, size_t input_len)
160 {
161 size_t overflow = 0;
162
163 if ((buf == NULL) || (buf->buffer == NULL)) {
164 return -1;
165 }
166
167 /* Reduce input_len to a number that fits in the buffer. */
168 if ((buf->content_length + input_len) > buf->capacity) {
169 input_len = buf->capacity - buf->content_length;
170 }
171
172 if (input == NULL) {
173 return (input_len == 0) ? 0 : -1;
174 }
175
176 /* Check if the buffer has not come full circle and free space is not in
177 * the middle */
178 if (buf->start + buf->content_length < buf->capacity) {
179
180 /* Calculate the number of bytes that need to be placed at lower memory
181 * address */
182 if (buf->start + buf->content_length + input_len
183 > buf->capacity) {
184 overflow = (buf->start + buf->content_length + input_len)
185 % buf->capacity;
186 }
187
188 memcpy(buf->buffer + buf->start + buf->content_length, input,
189 input_len - overflow);
190 memcpy(buf->buffer, input + input_len - overflow, overflow);
191
192 } else {
193 /* The buffer has come full circle and free space is in the middle */
194 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
195 input, input_len);
196 }
197
198 buf->content_length += input_len;
199 return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
200 }
201
mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer * buf,unsigned char * output,size_t output_len)202 int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
203 unsigned char *output, size_t output_len)
204 {
205 size_t overflow = 0;
206
207 if ((buf == NULL) || (buf->buffer == NULL)) {
208 return -1;
209 }
210
211 if (output == NULL && output_len == 0) {
212 return 0;
213 }
214
215 if (buf->content_length < output_len) {
216 output_len = buf->content_length;
217 }
218
219 /* Calculate the number of bytes that need to be drawn from lower memory
220 * address */
221 if (buf->start + output_len > buf->capacity) {
222 overflow = (buf->start + output_len) % buf->capacity;
223 }
224
225 if (output != NULL) {
226 memcpy(output, buf->buffer + buf->start, output_len - overflow);
227 memcpy(output + output_len - overflow, buf->buffer, overflow);
228 }
229
230 buf->content_length -= output_len;
231 buf->start = (buf->start + output_len) % buf->capacity;
232
233 return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
234 }
235
mbedtls_test_ssl_message_queue_setup(mbedtls_test_ssl_message_queue * queue,size_t capacity)236 int mbedtls_test_ssl_message_queue_setup(mbedtls_test_ssl_message_queue *queue,
237 size_t capacity)
238 {
239 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
240 if (NULL == queue->messages) {
241 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
242 }
243
244 queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
245 queue->pos = 0;
246 queue->num = 0;
247
248 return 0;
249 }
250
mbedtls_test_ssl_message_queue_free(mbedtls_test_ssl_message_queue * queue)251 void mbedtls_test_ssl_message_queue_free(mbedtls_test_ssl_message_queue *queue)
252 {
253 if (queue == NULL) {
254 return;
255 }
256
257 if (queue->messages != NULL) {
258 mbedtls_free(queue->messages);
259 }
260
261 memset(queue, 0, sizeof(*queue));
262 }
263
mbedtls_test_ssl_message_queue_push_info(mbedtls_test_ssl_message_queue * queue,size_t len)264 int mbedtls_test_ssl_message_queue_push_info(
265 mbedtls_test_ssl_message_queue *queue, size_t len)
266 {
267 int place;
268 if (queue == NULL) {
269 return MBEDTLS_TEST_ERROR_ARG_NULL;
270 }
271
272 if (queue->num >= queue->capacity) {
273 return MBEDTLS_ERR_SSL_WANT_WRITE;
274 }
275
276 place = (queue->pos + queue->num) % queue->capacity;
277 queue->messages[place] = len;
278 queue->num++;
279 return (len > INT_MAX) ? INT_MAX : (int) len;
280 }
281
mbedtls_test_ssl_message_queue_pop_info(mbedtls_test_ssl_message_queue * queue,size_t buf_len)282 int mbedtls_test_ssl_message_queue_pop_info(
283 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
284 {
285 size_t message_length;
286 if (queue == NULL) {
287 return MBEDTLS_TEST_ERROR_ARG_NULL;
288 }
289 if (queue->num == 0) {
290 return MBEDTLS_ERR_SSL_WANT_READ;
291 }
292
293 message_length = queue->messages[queue->pos];
294 queue->messages[queue->pos] = 0;
295 queue->num--;
296 queue->pos++;
297 queue->pos %= queue->capacity;
298 if (queue->pos < 0) {
299 queue->pos += queue->capacity;
300 }
301
302 return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
303 (message_length > buf_len) ? (int) buf_len : (int) message_length;
304 }
305
306 /*
307 * Take a peek on the info about the next message length from the queue.
308 * This will be the oldest inserted message length(fifo).
309 *
310 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
311 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
312 * \retval 0, if the peek was successful.
313 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
314 * too small to fit the message. In this case the \p msg_len will be
315 * set to the full message length so that the
316 * caller knows what portion of the message can be dropped.
317 */
mbedtls_test_message_queue_peek_info(mbedtls_test_ssl_message_queue * queue,size_t buf_len,size_t * msg_len)318 int mbedtls_test_message_queue_peek_info(mbedtls_test_ssl_message_queue *queue,
319 size_t buf_len, size_t *msg_len)
320 {
321 if (queue == NULL || msg_len == NULL) {
322 return MBEDTLS_TEST_ERROR_ARG_NULL;
323 }
324 if (queue->num == 0) {
325 return MBEDTLS_ERR_SSL_WANT_READ;
326 }
327
328 *msg_len = queue->messages[queue->pos];
329 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
330 }
331
mbedtls_mock_socket_init(mbedtls_test_mock_socket * socket)332 void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket)
333 {
334 memset(socket, 0, sizeof(*socket));
335 }
336
mbedtls_test_mock_socket_close(mbedtls_test_mock_socket * socket)337 void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
338 {
339 if (socket == NULL) {
340 return;
341 }
342
343 if (socket->input != NULL) {
344 mbedtls_test_ssl_buffer_free(socket->input);
345 mbedtls_free(socket->input);
346 }
347
348 if (socket->output != NULL) {
349 mbedtls_test_ssl_buffer_free(socket->output);
350 mbedtls_free(socket->output);
351 }
352
353 if (socket->peer != NULL) {
354 memset(socket->peer, 0, sizeof(*socket->peer));
355 }
356
357 memset(socket, 0, sizeof(*socket));
358 }
359
mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket * peer1,mbedtls_test_mock_socket * peer2,size_t bufsize)360 int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
361 mbedtls_test_mock_socket *peer2,
362 size_t bufsize)
363 {
364 int ret = -1;
365
366 peer1->output =
367 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
368 1, sizeof(mbedtls_test_ssl_buffer));
369 if (peer1->output == NULL) {
370 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
371 goto exit;
372 }
373 mbedtls_test_ssl_buffer_init(peer1->output);
374 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
375 goto exit;
376 }
377
378 peer2->output =
379 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
380 1, sizeof(mbedtls_test_ssl_buffer));
381 if (peer2->output == NULL) {
382 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
383 goto exit;
384 }
385 mbedtls_test_ssl_buffer_init(peer2->output);
386 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
387 goto exit;
388 }
389
390 peer1->peer = peer2;
391 peer2->peer = peer1;
392 peer1->input = peer2->output;
393 peer2->input = peer1->output;
394
395 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
396 ret = 0;
397
398 exit:
399
400 if (ret != 0) {
401 mbedtls_test_mock_socket_close(peer1);
402 mbedtls_test_mock_socket_close(peer2);
403 }
404
405 return ret;
406 }
407
mbedtls_test_mock_tcp_send_b(void * ctx,const unsigned char * buf,size_t len)408 int mbedtls_test_mock_tcp_send_b(void *ctx,
409 const unsigned char *buf, size_t len)
410 {
411 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
412
413 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
414 return -1;
415 }
416
417 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
418 }
419
mbedtls_test_mock_tcp_recv_b(void * ctx,unsigned char * buf,size_t len)420 int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
421 {
422 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
423
424 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
425 return -1;
426 }
427
428 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
429 }
430
mbedtls_test_mock_tcp_send_nb(void * ctx,const unsigned char * buf,size_t len)431 int mbedtls_test_mock_tcp_send_nb(void *ctx,
432 const unsigned char *buf, size_t len)
433 {
434 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
435
436 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
437 return -1;
438 }
439
440 if (socket->output->capacity == socket->output->content_length) {
441 return MBEDTLS_ERR_SSL_WANT_WRITE;
442 }
443
444 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
445 }
446
mbedtls_test_mock_tcp_recv_nb(void * ctx,unsigned char * buf,size_t len)447 int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
448 {
449 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
450
451 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
452 return -1;
453 }
454
455 if (socket->input->content_length == 0) {
456 return MBEDTLS_ERR_SSL_WANT_READ;
457 }
458
459 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
460 }
461
mbedtls_test_message_socket_init(mbedtls_test_message_socket_context * ctx)462 void mbedtls_test_message_socket_init(mbedtls_test_message_socket_context *ctx)
463 {
464 ctx->queue_input = NULL;
465 ctx->queue_output = NULL;
466 ctx->socket = NULL;
467 }
468
mbedtls_test_message_socket_setup(mbedtls_test_ssl_message_queue * queue_input,mbedtls_test_ssl_message_queue * queue_output,size_t queue_capacity,mbedtls_test_mock_socket * socket,mbedtls_test_message_socket_context * ctx)469 int mbedtls_test_message_socket_setup(
470 mbedtls_test_ssl_message_queue *queue_input,
471 mbedtls_test_ssl_message_queue *queue_output,
472 size_t queue_capacity,
473 mbedtls_test_mock_socket *socket,
474 mbedtls_test_message_socket_context *ctx)
475 {
476 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
477 if (ret != 0) {
478 return ret;
479 }
480 ctx->queue_input = queue_input;
481 ctx->queue_output = queue_output;
482 ctx->socket = socket;
483 mbedtls_mock_socket_init(socket);
484
485 return 0;
486 }
487
mbedtls_test_message_socket_close(mbedtls_test_message_socket_context * ctx)488 void mbedtls_test_message_socket_close(mbedtls_test_message_socket_context *ctx)
489 {
490 if (ctx == NULL) {
491 return;
492 }
493
494 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
495 mbedtls_test_mock_socket_close(ctx->socket);
496 memset(ctx, 0, sizeof(*ctx));
497 }
498
mbedtls_test_mock_tcp_send_msg(void * ctx,const unsigned char * buf,size_t len)499 int mbedtls_test_mock_tcp_send_msg(void *ctx,
500 const unsigned char *buf, size_t len)
501 {
502 mbedtls_test_ssl_message_queue *queue;
503 mbedtls_test_mock_socket *socket;
504 mbedtls_test_message_socket_context *context =
505 (mbedtls_test_message_socket_context *) ctx;
506
507 if (context == NULL || context->socket == NULL
508 || context->queue_output == NULL) {
509 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
510 }
511
512 queue = context->queue_output;
513 socket = context->socket;
514
515 if (queue->num >= queue->capacity) {
516 return MBEDTLS_ERR_SSL_WANT_WRITE;
517 }
518
519 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
520 return MBEDTLS_TEST_ERROR_SEND_FAILED;
521 }
522
523 return mbedtls_test_ssl_message_queue_push_info(queue, len);
524 }
525
mbedtls_test_mock_tcp_recv_msg(void * ctx,unsigned char * buf,size_t buf_len)526 int mbedtls_test_mock_tcp_recv_msg(void *ctx,
527 unsigned char *buf, size_t buf_len)
528 {
529 mbedtls_test_ssl_message_queue *queue;
530 mbedtls_test_mock_socket *socket;
531 mbedtls_test_message_socket_context *context =
532 (mbedtls_test_message_socket_context *) ctx;
533 size_t drop_len = 0;
534 size_t msg_len;
535 int ret;
536
537 if (context == NULL || context->socket == NULL
538 || context->queue_input == NULL) {
539 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
540 }
541
542 queue = context->queue_input;
543 socket = context->socket;
544
545 /* Peek first, so that in case of a socket error the data remains in
546 * the queue. */
547 ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len);
548 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
549 /* Calculate how much to drop */
550 drop_len = msg_len - buf_len;
551
552 /* Set the requested message len to be buffer length */
553 msg_len = buf_len;
554 } else if (ret != 0) {
555 return ret;
556 }
557
558 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
559 return MBEDTLS_TEST_ERROR_RECV_FAILED;
560 }
561
562 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
563 /* Drop the remaining part of the message */
564 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
565 (int) drop_len) {
566 /* Inconsistent state - part of the message was read,
567 * and a part couldn't. Not much we can do here, but it should not
568 * happen in test environment, unless forced manually. */
569 }
570 }
571 mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
572
573 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
574 }
575
576 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
577
578 /*
579 * Deinitializes certificates from endpoint represented by \p ep.
580 */
mbedtls_endpoint_certificate_free(mbedtls_test_ssl_endpoint * ep)581 void mbedtls_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
582 {
583 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
584 if (cert != NULL) {
585 if (cert->ca_cert != NULL) {
586 mbedtls_x509_crt_free(cert->ca_cert);
587 mbedtls_free(cert->ca_cert);
588 cert->ca_cert = NULL;
589 }
590 if (cert->cert != NULL) {
591 mbedtls_x509_crt_free(cert->cert);
592 mbedtls_free(cert->cert);
593 cert->cert = NULL;
594 }
595 if (cert->pkey != NULL) {
596 #if defined(MBEDTLS_USE_PSA_CRYPTO)
597 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
598 mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx;
599 psa_destroy_key(*key_slot);
600 }
601 #endif
602 mbedtls_pk_free(cert->pkey);
603 mbedtls_free(cert->pkey);
604 cert->pkey = NULL;
605 }
606 }
607 }
608
mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint * ep,int pk_alg,int opaque_alg,int opaque_alg2,int opaque_usage)609 int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
610 int pk_alg,
611 int opaque_alg, int opaque_alg2,
612 int opaque_usage)
613 {
614 int i = 0;
615 int ret = -1;
616 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
617 #if defined(MBEDTLS_USE_PSA_CRYPTO)
618 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
619 #endif
620
621 if (ep == NULL) {
622 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
623 }
624
625 cert = &(ep->cert);
626 ASSERT_ALLOC(cert->ca_cert, 1);
627 ASSERT_ALLOC(cert->cert, 1);
628 ASSERT_ALLOC(cert->pkey, 1);
629
630 mbedtls_x509_crt_init(cert->ca_cert);
631 mbedtls_x509_crt_init(cert->cert);
632 mbedtls_pk_init(cert->pkey);
633
634 /* Load the trusted CA */
635
636 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
637 ret = mbedtls_x509_crt_parse_der(
638 cert->ca_cert,
639 (const unsigned char *) mbedtls_test_cas_der[i],
640 mbedtls_test_cas_der_len[i]);
641 TEST_ASSERT(ret == 0);
642 }
643
644 /* Load own certificate and private key */
645
646 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
647 if (pk_alg == MBEDTLS_PK_RSA) {
648 ret = mbedtls_x509_crt_parse(
649 cert->cert,
650 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
651 mbedtls_test_srv_crt_rsa_sha256_der_len);
652 TEST_ASSERT(ret == 0);
653
654 ret = mbedtls_pk_parse_key(
655 cert->pkey,
656 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
657 mbedtls_test_srv_key_rsa_der_len, NULL, 0,
658 mbedtls_test_rnd_std_rand, NULL);
659 TEST_ASSERT(ret == 0);
660 } else {
661 ret = mbedtls_x509_crt_parse(
662 cert->cert,
663 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
664 mbedtls_test_srv_crt_ec_der_len);
665 TEST_ASSERT(ret == 0);
666
667 ret = mbedtls_pk_parse_key(
668 cert->pkey,
669 (const unsigned char *) mbedtls_test_srv_key_ec_der,
670 mbedtls_test_srv_key_ec_der_len, NULL, 0,
671 mbedtls_test_rnd_std_rand, NULL);
672 TEST_ASSERT(ret == 0);
673 }
674 } else {
675 if (pk_alg == MBEDTLS_PK_RSA) {
676 ret = mbedtls_x509_crt_parse(
677 cert->cert,
678 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
679 mbedtls_test_cli_crt_rsa_der_len);
680 TEST_ASSERT(ret == 0);
681
682 ret = mbedtls_pk_parse_key(
683 cert->pkey,
684 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
685 mbedtls_test_cli_key_rsa_der_len, NULL, 0,
686 mbedtls_test_rnd_std_rand, NULL);
687 TEST_ASSERT(ret == 0);
688 } else {
689 ret = mbedtls_x509_crt_parse(
690 cert->cert,
691 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
692 mbedtls_test_cli_crt_ec_len);
693 TEST_ASSERT(ret == 0);
694
695 ret = mbedtls_pk_parse_key(
696 cert->pkey,
697 (const unsigned char *) mbedtls_test_cli_key_ec_der,
698 mbedtls_test_cli_key_ec_der_len, NULL, 0,
699 mbedtls_test_rnd_std_rand, NULL);
700 TEST_ASSERT(ret == 0);
701 }
702 }
703
704 #if defined(MBEDTLS_USE_PSA_CRYPTO)
705 if (opaque_alg != 0) {
706 TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot,
707 opaque_alg, opaque_usage,
708 opaque_alg2), 0);
709 }
710 #else
711 (void) opaque_alg;
712 (void) opaque_alg2;
713 (void) opaque_usage;
714 #endif
715
716 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
717
718 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
719 cert->pkey);
720 TEST_ASSERT(ret == 0);
721 TEST_ASSERT(ep->conf.key_cert != NULL);
722
723 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
724 TEST_ASSERT(ret == 0);
725 TEST_ASSERT(ep->conf.key_cert == NULL);
726
727 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
728 cert->pkey);
729 TEST_ASSERT(ret == 0);
730
731 exit:
732 if (ret != 0) {
733 mbedtls_endpoint_certificate_free(ep);
734 }
735
736 return ret;
737 }
738
mbedtls_test_ssl_endpoint_init(mbedtls_test_ssl_endpoint * ep,int endpoint_type,mbedtls_test_handshake_test_options * options,mbedtls_test_message_socket_context * dtls_context,mbedtls_test_ssl_message_queue * input_queue,mbedtls_test_ssl_message_queue * output_queue,uint16_t * group_list)739 int mbedtls_test_ssl_endpoint_init(
740 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
741 mbedtls_test_handshake_test_options *options,
742 mbedtls_test_message_socket_context *dtls_context,
743 mbedtls_test_ssl_message_queue *input_queue,
744 mbedtls_test_ssl_message_queue *output_queue,
745 uint16_t *group_list)
746 {
747 int ret = -1;
748 uintptr_t user_data_n;
749
750 if (dtls_context != NULL &&
751 (input_queue == NULL || output_queue == NULL)) {
752 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
753
754 }
755
756 if (ep == NULL) {
757 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
758 }
759
760 memset(ep, 0, sizeof(*ep));
761
762 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
763
764 mbedtls_ssl_init(&(ep->ssl));
765 mbedtls_ssl_config_init(&(ep->conf));
766 mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL);
767
768 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
769 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
770 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
771 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
772
773 (void) mbedtls_test_rnd_std_rand(NULL,
774 (void *) &user_data_n,
775 sizeof(user_data_n));
776 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
777 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
778
779 if (dtls_context != NULL) {
780 TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue,
781 100, &(ep->socket),
782 dtls_context) == 0);
783 } else {
784 mbedtls_mock_socket_init(&(ep->socket));
785 }
786
787 /* Non-blocking callbacks without timeout */
788 if (dtls_context != NULL) {
789 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
790 mbedtls_test_mock_tcp_send_msg,
791 mbedtls_test_mock_tcp_recv_msg,
792 NULL);
793 } else {
794 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
795 mbedtls_test_mock_tcp_send_nb,
796 mbedtls_test_mock_tcp_recv_nb,
797 NULL);
798 }
799
800 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
801 (dtls_context != NULL) ?
802 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
803 MBEDTLS_SSL_TRANSPORT_STREAM,
804 MBEDTLS_SSL_PRESET_DEFAULT);
805 TEST_ASSERT(ret == 0);
806
807 if (group_list != NULL) {
808 mbedtls_ssl_conf_groups(&(ep->conf), group_list);
809 }
810
811 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
812
813 #if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
814 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
815 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
816 mbedtls_ssl_cache_get,
817 mbedtls_ssl_cache_set);
818 }
819 #endif
820
821 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
822 TEST_ASSERT(ret == 0);
823
824 #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
825 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
826 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
827 }
828 #endif
829
830 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
831 options->opaque_alg,
832 options->opaque_alg2,
833 options->opaque_usage);
834 TEST_ASSERT(ret == 0);
835
836 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
837 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
838 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
839 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
840
841 exit:
842 return ret;
843 }
844
mbedtls_test_ssl_endpoint_free(mbedtls_test_ssl_endpoint * ep,mbedtls_test_message_socket_context * context)845 void mbedtls_test_ssl_endpoint_free(
846 mbedtls_test_ssl_endpoint *ep,
847 mbedtls_test_message_socket_context *context)
848 {
849 mbedtls_endpoint_certificate_free(ep);
850
851 mbedtls_ssl_free(&(ep->ssl));
852 mbedtls_ssl_config_free(&(ep->conf));
853
854 if (context != NULL) {
855 mbedtls_test_message_socket_close(context);
856 } else {
857 mbedtls_test_mock_socket_close(&(ep->socket));
858 }
859 }
860
mbedtls_test_move_handshake_to_state(mbedtls_ssl_context * ssl,mbedtls_ssl_context * second_ssl,int state)861 int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
862 mbedtls_ssl_context *second_ssl,
863 int state)
864 {
865 enum { BUFFSIZE = 1024 };
866 int max_steps = 1000;
867 int ret = 0;
868
869 if (ssl == NULL || second_ssl == NULL) {
870 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
871 }
872
873 /* Perform communication via connected sockets */
874 while ((ssl->state != state) && (--max_steps >= 0)) {
875 /* If /p second_ssl ends the handshake procedure before /p ssl then
876 * there is no need to call the next step */
877 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
878 ret = mbedtls_ssl_handshake_step(second_ssl);
879 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
880 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
881 return ret;
882 }
883 }
884
885 /* We only care about the \p ssl state and returns, so we call it last,
886 * to leave the iteration as soon as the state is as expected. */
887 ret = mbedtls_ssl_handshake_step(ssl);
888 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
889 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
890 return ret;
891 }
892 }
893
894 return (max_steps >= 0) ? ret : -1;
895 }
896
897 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
898
899 /*
900 * Write application data. Increase write counter if necessary.
901 */
mbedtls_ssl_write_fragment(mbedtls_ssl_context * ssl,unsigned char * buf,int buf_len,int * written,const int expected_fragments)902 int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
903 unsigned char *buf, int buf_len,
904 int *written,
905 const int expected_fragments)
906 {
907 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
908 * a valid no-op for TLS connections. */
909 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
910 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
911 }
912
913 int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
914 if (ret > 0) {
915 *written += ret;
916 }
917
918 if (expected_fragments == 0) {
919 /* Used for DTLS and the message size larger than MFL. In that case
920 * the message can not be fragmented and the library should return
921 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
922 * to prevent a dead loop inside mbedtls_exchange_data(). */
923 return ret;
924 } else if (expected_fragments == 1) {
925 /* Used for TLS/DTLS and the message size lower than MFL */
926 TEST_ASSERT(ret == buf_len ||
927 ret == MBEDTLS_ERR_SSL_WANT_READ ||
928 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
929 } else {
930 /* Used for TLS and the message size larger than MFL */
931 TEST_ASSERT(expected_fragments > 1);
932 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
933 ret == MBEDTLS_ERR_SSL_WANT_READ ||
934 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
935 }
936
937 return 0;
938
939 exit:
940 /* Some of the tests failed */
941 return -1;
942 }
943
944 /*
945 * Read application data and increase read counter and fragments counter
946 * if necessary.
947 */
mbedtls_ssl_read_fragment(mbedtls_ssl_context * ssl,unsigned char * buf,int buf_len,int * read,int * fragments,const int expected_fragments)948 int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
949 unsigned char *buf, int buf_len,
950 int *read, int *fragments,
951 const int expected_fragments)
952 {
953 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
954 * a valid no-op for TLS connections. */
955 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
956 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
957 }
958
959 int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
960 if (ret > 0) {
961 (*fragments)++;
962 *read += ret;
963 }
964
965 if (expected_fragments == 0) {
966 TEST_ASSERT(ret == 0);
967 } else if (expected_fragments == 1) {
968 TEST_ASSERT(ret == buf_len ||
969 ret == MBEDTLS_ERR_SSL_WANT_READ ||
970 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
971 } else {
972 TEST_ASSERT(expected_fragments > 1);
973 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
974 ret == MBEDTLS_ERR_SSL_WANT_READ ||
975 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
976 }
977
978 return 0;
979
980 exit:
981 /* Some of the tests failed */
982 return -1;
983 }
984
set_ciphersuite(mbedtls_ssl_config * conf,const char * cipher,int * forced_ciphersuite)985 void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
986 int *forced_ciphersuite)
987 {
988 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
989 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
990 forced_ciphersuite[1] = 0;
991
992 ciphersuite_info =
993 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
994
995 TEST_ASSERT(ciphersuite_info != NULL);
996 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
997 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
998
999 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
1000 conf->max_tls_version = ciphersuite_info->max_tls_version;
1001 }
1002 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
1003 conf->min_tls_version = ciphersuite_info->min_tls_version;
1004 }
1005
1006 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1007
1008 exit:
1009 return;
1010 }
1011
psk_dummy_callback(void * p_info,mbedtls_ssl_context * ssl,const unsigned char * name,size_t name_len)1012 int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1013 const unsigned char *name, size_t name_len)
1014 {
1015 (void) p_info;
1016 (void) ssl;
1017 (void) name;
1018 (void) name_len;
1019
1020 return 0;
1021 }
1022
1023 #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1024 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1025 #else
1026 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1027 #endif
1028
1029 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1030 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform * transform,const unsigned char * iv,size_t iv_len,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen)1031 int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1032 const unsigned char *iv,
1033 size_t iv_len,
1034 const unsigned char *input,
1035 size_t ilen,
1036 unsigned char *output,
1037 size_t *olen)
1038 {
1039 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1041 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1042 size_t part_len;
1043
1044 status = psa_cipher_encrypt_setup(&cipher_op,
1045 transform->psa_key_enc,
1046 transform->psa_alg);
1047
1048 if (status != PSA_SUCCESS) {
1049 return PSA_TO_MBEDTLS_ERR(status);
1050 }
1051
1052 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1053
1054 if (status != PSA_SUCCESS) {
1055 return PSA_TO_MBEDTLS_ERR(status);
1056 }
1057
1058 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1059
1060 if (status != PSA_SUCCESS) {
1061 return PSA_TO_MBEDTLS_ERR(status);
1062 }
1063
1064 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1065 &part_len);
1066
1067 if (status != PSA_SUCCESS) {
1068 return PSA_TO_MBEDTLS_ERR(status);
1069 }
1070
1071 *olen += part_len;
1072 return 0;
1073 #else
1074 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1075 iv, iv_len, input, ilen, output, olen);
1076 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1077 }
1078 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
1079 MBEDTLS_AES_C */
1080
mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform * t_in,mbedtls_ssl_transform * t_out,int cipher_type,int hash_id,int etm,int tag_mode,mbedtls_ssl_protocol_version tls_version,size_t cid0_len,size_t cid1_len)1081 int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1082 mbedtls_ssl_transform *t_out,
1083 int cipher_type, int hash_id,
1084 int etm, int tag_mode,
1085 mbedtls_ssl_protocol_version tls_version,
1086 size_t cid0_len,
1087 size_t cid1_len)
1088 {
1089 mbedtls_cipher_info_t const *cipher_info;
1090 int ret = 0;
1091
1092 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1093 psa_key_type_t key_type;
1094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1095 psa_algorithm_t alg;
1096 size_t key_bits;
1097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1098 #endif
1099
1100 size_t keylen, maclen, ivlen;
1101 unsigned char *key0 = NULL, *key1 = NULL;
1102 unsigned char *md0 = NULL, *md1 = NULL;
1103 unsigned char iv_enc[16], iv_dec[16];
1104
1105 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1106 unsigned char cid0[SSL_CID_LEN_MIN];
1107 unsigned char cid1[SSL_CID_LEN_MIN];
1108
1109 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1110 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1111 #else
1112 ((void) cid0_len);
1113 ((void) cid1_len);
1114 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1115
1116 maclen = 0;
1117
1118 /* Pick cipher */
1119 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
1120 CHK(cipher_info != NULL);
1121 CHK(cipher_info->iv_size <= 16);
1122 CHK(cipher_info->key_bitlen % 8 == 0);
1123
1124 /* Pick keys */
1125 keylen = cipher_info->key_bitlen / 8;
1126 /* Allocate `keylen + 1` bytes to ensure that we get
1127 * a non-NULL pointers from `mbedtls_calloc` even if
1128 * `keylen == 0` in the case of the NULL cipher. */
1129 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1130 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1131 memset(key0, 0x1, keylen);
1132 memset(key1, 0x2, keylen);
1133
1134 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
1135 /* Setup cipher contexts */
1136 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1137 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1138 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1139 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1140
1141 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1142 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1143 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1144 MBEDTLS_PADDING_NONE) == 0);
1145 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1146 MBEDTLS_PADDING_NONE) == 0);
1147 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1148 MBEDTLS_PADDING_NONE) == 0);
1149 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1150 MBEDTLS_PADDING_NONE) == 0);
1151 }
1152 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1153
1154 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
1155 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1156 MBEDTLS_ENCRYPT)
1157 == 0);
1158 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
1159 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1160 MBEDTLS_DECRYPT)
1161 == 0);
1162 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
1163 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1164 MBEDTLS_ENCRYPT)
1165 == 0);
1166 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
1167 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1168 MBEDTLS_DECRYPT)
1169 == 0);
1170 #endif
1171
1172 /* Setup MAC contexts */
1173 #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1174 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1175 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1176 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
1177 mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id);
1178 CHK(md_info != NULL);
1179 #endif
1180 maclen = mbedtls_hash_info_get_size(hash_id);
1181 CHK(maclen != 0);
1182 /* Pick hash keys */
1183 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1184 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1185 memset(md0, 0x5, maclen);
1186 memset(md1, 0x6, maclen);
1187
1188 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1189 alg = mbedtls_hash_info_psa_from_md(hash_id);
1190
1191 CHK(alg != 0);
1192
1193 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1194 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1195 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1196 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1197 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1198 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1199
1200 psa_reset_key_attributes(&attributes);
1201 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1202 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1203 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1204
1205 CHK(psa_import_key(&attributes,
1206 md0, maclen,
1207 &t_in->psa_mac_enc) == PSA_SUCCESS);
1208
1209 CHK(psa_import_key(&attributes,
1210 md1, maclen,
1211 &t_out->psa_mac_enc) == PSA_SUCCESS);
1212
1213 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1214 etm == MBEDTLS_SSL_ETM_DISABLED) {
1215 /* mbedtls_ct_hmac() requires the key to be exportable */
1216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1217 PSA_KEY_USAGE_VERIFY_HASH);
1218 } else {
1219 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1220 }
1221
1222 CHK(psa_import_key(&attributes,
1223 md1, maclen,
1224 &t_in->psa_mac_dec) == PSA_SUCCESS);
1225
1226 CHK(psa_import_key(&attributes,
1227 md0, maclen,
1228 &t_out->psa_mac_dec) == PSA_SUCCESS);
1229 #else
1230 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1231 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1232 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1233 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1234
1235 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1236 md0, maclen) == 0);
1237 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1238 md1, maclen) == 0);
1239 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1240 md1, maclen) == 0);
1241 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1242 md0, maclen) == 0);
1243 #endif
1244 }
1245 #else
1246 ((void) hash_id);
1247 #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1248
1249
1250 /* Pick IV's (regardless of whether they
1251 * are being used by the transform). */
1252 ivlen = cipher_info->iv_size;
1253 memset(iv_enc, 0x3, sizeof(iv_enc));
1254 memset(iv_dec, 0x4, sizeof(iv_dec));
1255
1256 /*
1257 * Setup transforms
1258 */
1259
1260 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1261 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1262 t_out->encrypt_then_mac = etm;
1263 t_in->encrypt_then_mac = etm;
1264 #else
1265 ((void) etm);
1266 #endif
1267
1268 t_out->tls_version = tls_version;
1269 t_in->tls_version = tls_version;
1270 t_out->ivlen = ivlen;
1271 t_in->ivlen = ivlen;
1272
1273 switch (cipher_info->mode) {
1274 case MBEDTLS_MODE_GCM:
1275 case MBEDTLS_MODE_CCM:
1276 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1277 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1278 t_out->fixed_ivlen = 12;
1279 t_in->fixed_ivlen = 12;
1280 } else
1281 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1282 {
1283 t_out->fixed_ivlen = 4;
1284 t_in->fixed_ivlen = 4;
1285 }
1286 t_out->maclen = 0;
1287 t_in->maclen = 0;
1288 switch (tag_mode) {
1289 case 0: /* Full tag */
1290 t_out->taglen = 16;
1291 t_in->taglen = 16;
1292 break;
1293 case 1: /* Partial tag */
1294 t_out->taglen = 8;
1295 t_in->taglen = 8;
1296 break;
1297 default:
1298 ret = 1;
1299 goto cleanup;
1300 }
1301 break;
1302
1303 case MBEDTLS_MODE_CHACHAPOLY:
1304 t_out->fixed_ivlen = 12;
1305 t_in->fixed_ivlen = 12;
1306 t_out->maclen = 0;
1307 t_in->maclen = 0;
1308 switch (tag_mode) {
1309 case 0: /* Full tag */
1310 t_out->taglen = 16;
1311 t_in->taglen = 16;
1312 break;
1313 case 1: /* Partial tag */
1314 t_out->taglen = 8;
1315 t_in->taglen = 8;
1316 break;
1317 default:
1318 ret = 1;
1319 goto cleanup;
1320 }
1321 break;
1322
1323 case MBEDTLS_MODE_STREAM:
1324 case MBEDTLS_MODE_CBC:
1325 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1326 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1327 t_out->taglen = 0;
1328 t_in->taglen = 0;
1329 switch (tag_mode) {
1330 case 0: /* Full tag */
1331 t_out->maclen = maclen;
1332 t_in->maclen = maclen;
1333 break;
1334 default:
1335 ret = 1;
1336 goto cleanup;
1337 }
1338 break;
1339 default:
1340 ret = 1;
1341 goto cleanup;
1342 break;
1343 }
1344
1345 /* Setup IV's */
1346
1347 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1348 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1349 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1350 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1351
1352 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1353 /* Add CID */
1354 memcpy(&t_in->in_cid, cid0, cid0_len);
1355 memcpy(&t_in->out_cid, cid1, cid1_len);
1356 t_in->in_cid_len = (uint8_t) cid0_len;
1357 t_in->out_cid_len = (uint8_t) cid1_len;
1358 memcpy(&t_out->in_cid, cid1, cid1_len);
1359 memcpy(&t_out->out_cid, cid0, cid0_len);
1360 t_out->in_cid_len = (uint8_t) cid1_len;
1361 t_out->out_cid_len = (uint8_t) cid0_len;
1362 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1363
1364 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1365 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1366 t_in->taglen,
1367 &alg,
1368 &key_type,
1369 &key_bits);
1370
1371 if (status != PSA_SUCCESS) {
1372 ret = PSA_TO_MBEDTLS_ERR(status);
1373 goto cleanup;
1374 }
1375
1376 t_in->psa_alg = alg;
1377 t_out->psa_alg = alg;
1378
1379 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1380 psa_reset_key_attributes(&attributes);
1381 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1382 psa_set_key_algorithm(&attributes, alg);
1383 psa_set_key_type(&attributes, key_type);
1384
1385 status = psa_import_key(&attributes,
1386 key0,
1387 PSA_BITS_TO_BYTES(key_bits),
1388 &t_in->psa_key_enc);
1389
1390 if (status != PSA_SUCCESS) {
1391 ret = PSA_TO_MBEDTLS_ERR(status);
1392 goto cleanup;
1393 }
1394
1395 status = psa_import_key(&attributes,
1396 key1,
1397 PSA_BITS_TO_BYTES(key_bits),
1398 &t_out->psa_key_enc);
1399
1400 if (status != PSA_SUCCESS) {
1401 ret = PSA_TO_MBEDTLS_ERR(status);
1402 goto cleanup;
1403 }
1404
1405 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1406
1407 status = psa_import_key(&attributes,
1408 key1,
1409 PSA_BITS_TO_BYTES(key_bits),
1410 &t_in->psa_key_dec);
1411
1412 if (status != PSA_SUCCESS) {
1413 ret = PSA_TO_MBEDTLS_ERR(status);
1414 goto cleanup;
1415 }
1416
1417 status = psa_import_key(&attributes,
1418 key0,
1419 PSA_BITS_TO_BYTES(key_bits),
1420 &t_out->psa_key_dec);
1421
1422 if (status != PSA_SUCCESS) {
1423 ret = PSA_TO_MBEDTLS_ERR(status);
1424 goto cleanup;
1425 }
1426 }
1427 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1428
1429 cleanup:
1430
1431 mbedtls_free(key0);
1432 mbedtls_free(key1);
1433
1434 mbedtls_free(md0);
1435 mbedtls_free(md1);
1436
1437 return ret;
1438 }
1439
mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session * session,int ticket_len,const char * crt_file)1440 int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1441 int ticket_len,
1442 const char *crt_file)
1443 {
1444 #if defined(MBEDTLS_HAVE_TIME)
1445 session->start = mbedtls_time(NULL) - 42;
1446 #endif
1447 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1448 session->ciphersuite = 0xabcd;
1449 session->id_len = sizeof(session->id);
1450 memset(session->id, 66, session->id_len);
1451 memset(session->master, 17, sizeof(session->master));
1452
1453 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1454 if (crt_file != NULL && strlen(crt_file) != 0) {
1455 mbedtls_x509_crt tmp_crt;
1456 int ret;
1457
1458 mbedtls_x509_crt_init(&tmp_crt);
1459 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1460 if (ret != 0) {
1461 return ret;
1462 }
1463
1464 #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1465 /* Move temporary CRT. */
1466 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1467 if (session->peer_cert == NULL) {
1468 return -1;
1469 }
1470 *session->peer_cert = tmp_crt;
1471 memset(&tmp_crt, 0, sizeof(tmp_crt));
1472 #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1473 /* Calculate digest of temporary CRT. */
1474 session->peer_cert_digest =
1475 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1476 if (session->peer_cert_digest == NULL) {
1477 return -1;
1478 }
1479
1480 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1481 psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md(
1482 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1483 size_t hash_size = 0;
1484 psa_status_t status = psa_hash_compute(
1485 psa_alg, tmp_crt.raw.p,
1486 tmp_crt.raw.len,
1487 session->peer_cert_digest,
1488 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1489 &hash_size);
1490 ret = PSA_TO_MBEDTLS_ERR(status);
1491 #else
1492 ret = mbedtls_md(mbedtls_md_info_from_type(
1493 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1494 tmp_crt.raw.p, tmp_crt.raw.len,
1495 session->peer_cert_digest);
1496 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1497 if (ret != 0) {
1498 return ret;
1499 }
1500 session->peer_cert_digest_type =
1501 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1502 session->peer_cert_digest_len =
1503 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1504 #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1505
1506 mbedtls_x509_crt_free(&tmp_crt);
1507 }
1508 #else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1509 (void) crt_file;
1510 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1511 session->verify_result = 0xdeadbeef;
1512
1513 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1514 if (ticket_len != 0) {
1515 session->ticket = mbedtls_calloc(1, ticket_len);
1516 if (session->ticket == NULL) {
1517 return -1;
1518 }
1519 memset(session->ticket, 33, ticket_len);
1520 }
1521 session->ticket_len = ticket_len;
1522 session->ticket_lifetime = 86401;
1523 #else
1524 (void) ticket_len;
1525 #endif
1526
1527 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1528 session->mfl_code = 1;
1529 #endif
1530 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1531 session->encrypt_then_mac = 1;
1532 #endif
1533
1534 return 0;
1535 }
1536
1537 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session * session,int ticket_len,int endpoint_type)1538 int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1539 int ticket_len,
1540 int endpoint_type)
1541 {
1542 ((void) ticket_len);
1543 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1544 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1545 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1546 session->ciphersuite = 0xabcd;
1547 session->ticket_age_add = 0x87654321;
1548 session->ticket_flags = 0x7;
1549
1550 session->resumption_key_len = 32;
1551 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
1552
1553 #if defined(MBEDTLS_HAVE_TIME)
1554 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1555 session->start = mbedtls_time(NULL) - 42;
1556 }
1557 #endif
1558
1559 #if defined(MBEDTLS_SSL_CLI_C)
1560 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
1561 #if defined(MBEDTLS_HAVE_TIME)
1562 session->ticket_received = mbedtls_time(NULL) - 40;
1563 #endif
1564 session->ticket_lifetime = 0xfedcba98;
1565
1566 session->ticket_len = ticket_len;
1567 if (ticket_len != 0) {
1568 session->ticket = mbedtls_calloc(1, ticket_len);
1569 if (session->ticket == NULL) {
1570 return -1;
1571 }
1572 memset(session->ticket, 33, ticket_len);
1573 }
1574 }
1575 #endif /* MBEDTLS_SSL_CLI_C */
1576
1577 return 0;
1578 }
1579 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1580
mbedtls_exchange_data(mbedtls_ssl_context * ssl_1,int msg_len_1,const int expected_fragments_1,mbedtls_ssl_context * ssl_2,int msg_len_2,const int expected_fragments_2)1581 int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
1582 int msg_len_1, const int expected_fragments_1,
1583 mbedtls_ssl_context *ssl_2,
1584 int msg_len_2, const int expected_fragments_2)
1585 {
1586 unsigned char *msg_buf_1 = malloc(msg_len_1);
1587 unsigned char *msg_buf_2 = malloc(msg_len_2);
1588 unsigned char *in_buf_1 = malloc(msg_len_2);
1589 unsigned char *in_buf_2 = malloc(msg_len_1);
1590 int msg_type, ret = -1;
1591
1592 /* Perform this test with two message types. At first use a message
1593 * consisting of only 0x00 for the client and only 0xFF for the server.
1594 * At the second time use message with generated data */
1595 for (msg_type = 0; msg_type < 2; msg_type++) {
1596 int written_1 = 0;
1597 int written_2 = 0;
1598 int read_1 = 0;
1599 int read_2 = 0;
1600 int fragments_1 = 0;
1601 int fragments_2 = 0;
1602
1603 if (msg_type == 0) {
1604 memset(msg_buf_1, 0x00, msg_len_1);
1605 memset(msg_buf_2, 0xff, msg_len_2);
1606 } else {
1607 int i, j = 0;
1608 for (i = 0; i < msg_len_1; i++) {
1609 msg_buf_1[i] = j++ & 0xFF;
1610 }
1611 for (i = 0; i < msg_len_2; i++) {
1612 msg_buf_2[i] = (j -= 5) & 0xFF;
1613 }
1614 }
1615
1616 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1617 /* ssl_1 sending */
1618 if (msg_len_1 > written_1) {
1619 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1620 msg_len_1, &written_1,
1621 expected_fragments_1);
1622 if (expected_fragments_1 == 0) {
1623 /* This error is expected when the message is too large and
1624 * cannot be fragmented */
1625 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1626 msg_len_1 = 0;
1627 } else {
1628 TEST_ASSERT(ret == 0);
1629 }
1630 }
1631
1632 /* ssl_2 sending */
1633 if (msg_len_2 > written_2) {
1634 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1635 msg_len_2, &written_2,
1636 expected_fragments_2);
1637 if (expected_fragments_2 == 0) {
1638 /* This error is expected when the message is too large and
1639 * cannot be fragmented */
1640 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1641 msg_len_2 = 0;
1642 } else {
1643 TEST_ASSERT(ret == 0);
1644 }
1645 }
1646
1647 /* ssl_1 reading */
1648 if (read_1 < msg_len_2) {
1649 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1650 msg_len_2, &read_1,
1651 &fragments_2,
1652 expected_fragments_2);
1653 TEST_ASSERT(ret == 0);
1654 }
1655
1656 /* ssl_2 reading */
1657 if (read_2 < msg_len_1) {
1658 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1659 msg_len_1, &read_2,
1660 &fragments_1,
1661 expected_fragments_1);
1662 TEST_ASSERT(ret == 0);
1663 }
1664 }
1665
1666 ret = -1;
1667 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1668 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1669 TEST_ASSERT(fragments_1 == expected_fragments_1);
1670 TEST_ASSERT(fragments_2 == expected_fragments_2);
1671 }
1672
1673 ret = 0;
1674
1675 exit:
1676 free(msg_buf_1);
1677 free(in_buf_1);
1678 free(msg_buf_2);
1679 free(in_buf_2);
1680
1681 return ret;
1682 }
1683
1684 /*
1685 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1686 * must be initialized and connected beforehand.
1687 *
1688 * \retval 0 on success, otherwise error code.
1689 */
exchange_data(mbedtls_ssl_context * ssl_1,mbedtls_ssl_context * ssl_2)1690 int exchange_data(mbedtls_ssl_context *ssl_1,
1691 mbedtls_ssl_context *ssl_2)
1692 {
1693 return mbedtls_exchange_data(ssl_1, 256, 1,
1694 ssl_2, 256, 1);
1695 }
1696
1697 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
check_ssl_version(mbedtls_ssl_protocol_version expected_negotiated_version,const mbedtls_ssl_context * ssl)1698 static int check_ssl_version(
1699 mbedtls_ssl_protocol_version expected_negotiated_version,
1700 const mbedtls_ssl_context *ssl)
1701 {
1702 const char *version_string = mbedtls_ssl_get_version(ssl);
1703 mbedtls_ssl_protocol_version version_number =
1704 mbedtls_ssl_get_version_number(ssl);
1705
1706 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
1707
1708 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1709 TEST_EQUAL(version_string[0], 'D');
1710 ++version_string;
1711 }
1712
1713 switch (expected_negotiated_version) {
1714 case MBEDTLS_SSL_VERSION_TLS1_2:
1715 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
1716 TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0);
1717 break;
1718
1719 case MBEDTLS_SSL_VERSION_TLS1_3:
1720 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
1721 TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0);
1722 break;
1723
1724 default:
1725 TEST_ASSERT(
1726 !"Version check not implemented for this protocol version");
1727 }
1728
1729 return 1;
1730
1731 exit:
1732 return 0;
1733 }
1734 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1735
1736
1737 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
mbedtls_test_ssl_perform_handshake(mbedtls_test_handshake_test_options * options)1738 void mbedtls_test_ssl_perform_handshake(
1739 mbedtls_test_handshake_test_options *options)
1740 {
1741 /* forced_ciphersuite needs to last until the end of the handshake */
1742 int forced_ciphersuite[2];
1743 enum { BUFFSIZE = 17000 };
1744 mbedtls_test_ssl_endpoint client, server;
1745 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1746 const char *psk_identity = "foo";
1747 #endif
1748 #if defined(MBEDTLS_TIMING_C)
1749 mbedtls_timing_delay_context timer_client, timer_server;
1750 #endif
1751 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1752 unsigned char *context_buf = NULL;
1753 size_t context_buf_len;
1754 #endif
1755 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1756 int ret = -1;
1757 #endif
1758 int expected_handshake_result = options->expected_handshake_result;
1759
1760 USE_PSA_INIT();
1761 mbedtls_platform_zeroize(&client, sizeof(client));
1762 mbedtls_platform_zeroize(&server, sizeof(server));
1763 mbedtls_test_ssl_message_queue server_queue, client_queue;
1764 mbedtls_test_message_socket_context server_context, client_context;
1765 mbedtls_test_message_socket_init(&server_context);
1766 mbedtls_test_message_socket_init(&client_context);
1767
1768 /* Client side */
1769 if (options->dtls != 0) {
1770 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1771 MBEDTLS_SSL_IS_CLIENT,
1772 options, &client_context,
1773 &client_queue,
1774 &server_queue, NULL) == 0);
1775 #if defined(MBEDTLS_TIMING_C)
1776 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1777 mbedtls_timing_set_delay,
1778 mbedtls_timing_get_delay);
1779 #endif
1780 } else {
1781 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1782 MBEDTLS_SSL_IS_CLIENT,
1783 options, NULL, NULL,
1784 NULL, NULL) == 0);
1785 }
1786
1787 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
1788 mbedtls_ssl_conf_min_tls_version(&client.conf,
1789 options->client_min_version);
1790 }
1791
1792 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
1793 mbedtls_ssl_conf_max_tls_version(&client.conf,
1794 options->client_max_version);
1795 }
1796
1797 if (strlen(options->cipher) > 0) {
1798 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1799 }
1800
1801 #if defined(MBEDTLS_DEBUG_C)
1802 if (options->cli_log_fun) {
1803 mbedtls_debug_set_threshold(4);
1804 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1805 options->cli_log_obj);
1806 }
1807 #endif
1808
1809 /* Server side */
1810 if (options->dtls != 0) {
1811 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1812 MBEDTLS_SSL_IS_SERVER,
1813 options, &server_context,
1814 &server_queue,
1815 &client_queue, NULL) == 0);
1816 #if defined(MBEDTLS_TIMING_C)
1817 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1818 mbedtls_timing_set_delay,
1819 mbedtls_timing_get_delay);
1820 #endif
1821 } else {
1822 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1823 MBEDTLS_SSL_IS_SERVER,
1824 options, NULL, NULL, NULL,
1825 NULL) == 0);
1826 }
1827
1828 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1829
1830 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
1831 mbedtls_ssl_conf_min_tls_version(&server.conf,
1832 options->server_min_version);
1833 }
1834
1835 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
1836 mbedtls_ssl_conf_max_tls_version(&server.conf,
1837 options->server_max_version);
1838 }
1839
1840 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1841 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1842 (unsigned char) options->mfl)
1843 == 0);
1844 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1845 (unsigned char) options->mfl)
1846 == 0);
1847 #else
1848 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1849 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1850
1851 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1852 if (options->psk_str != NULL && options->psk_str->len > 0) {
1853 TEST_ASSERT(mbedtls_ssl_conf_psk(
1854 &client.conf, options->psk_str->x,
1855 options->psk_str->len,
1856 (const unsigned char *) psk_identity,
1857 strlen(psk_identity)) == 0);
1858
1859 TEST_ASSERT(mbedtls_ssl_conf_psk(
1860 &server.conf, options->psk_str->x,
1861 options->psk_str->len,
1862 (const unsigned char *) psk_identity,
1863 strlen(psk_identity)) == 0);
1864 #if defined(MBEDTLS_SSL_SRV_C)
1865 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1866 #endif
1867 }
1868 #endif
1869 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1870 if (options->renegotiate) {
1871 mbedtls_ssl_conf_renegotiation(&(server.conf),
1872 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1873 mbedtls_ssl_conf_renegotiation(&(client.conf),
1874 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1875
1876 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1877 options->legacy_renegotiation);
1878 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1879 options->legacy_renegotiation);
1880 }
1881 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1882
1883 #if defined(MBEDTLS_DEBUG_C)
1884 if (options->srv_log_fun) {
1885 mbedtls_debug_set_threshold(4);
1886 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1887 options->srv_log_obj);
1888 }
1889 #endif
1890
1891 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1892 &(server.socket),
1893 BUFFSIZE) == 0);
1894
1895 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1896 if (options->resize_buffers != 0) {
1897 /* Ensure that the buffer sizes are appropriate before resizes */
1898 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1899 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1900 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1901 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1902 }
1903 #endif
1904
1905 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
1906 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1907 }
1908
1909 TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl),
1910 &(server.ssl),
1911 MBEDTLS_SSL_HANDSHAKE_OVER)
1912 == expected_handshake_result);
1913
1914 if (expected_handshake_result != 0) {
1915 /* Connection will have failed by this point, skip to cleanup */
1916 goto exit;
1917 }
1918
1919 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1);
1920
1921 /* Make sure server state is moved to HANDSHAKE_OVER also. */
1922 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
1923 &(client.ssl),
1924 MBEDTLS_SSL_HANDSHAKE_OVER),
1925 0);
1926
1927 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1);
1928 /* Check that both sides have negotiated the expected version. */
1929 mbedtls_test_set_step(0);
1930 if (!check_ssl_version(options->expected_negotiated_version,
1931 &client.ssl)) {
1932 goto exit;
1933 }
1934
1935 mbedtls_test_set_step(1);
1936 if (!check_ssl_version(options->expected_negotiated_version,
1937 &server.ssl)) {
1938 goto exit;
1939 }
1940
1941 if (options->expected_ciphersuite != 0) {
1942 TEST_EQUAL(server.ssl.session->ciphersuite,
1943 options->expected_ciphersuite);
1944 }
1945
1946 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1947 if (options->resize_buffers != 0) {
1948 /* A server, when using DTLS, might delay a buffer resize to happen
1949 * after it receives a message, so we force it. */
1950 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1951
1952 TEST_ASSERT(client.ssl.out_buf_len ==
1953 mbedtls_ssl_get_output_buflen(&client.ssl));
1954 TEST_ASSERT(client.ssl.in_buf_len ==
1955 mbedtls_ssl_get_input_buflen(&client.ssl));
1956 TEST_ASSERT(server.ssl.out_buf_len ==
1957 mbedtls_ssl_get_output_buflen(&server.ssl));
1958 TEST_ASSERT(server.ssl.in_buf_len ==
1959 mbedtls_ssl_get_input_buflen(&server.ssl));
1960 }
1961 #endif
1962
1963 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1964 /* Start data exchanging test */
1965 TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len,
1966 options->expected_cli_fragments,
1967 &(server.ssl), options->srv_msg_len,
1968 options->expected_srv_fragments)
1969 == 0);
1970 }
1971 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1972 if (options->serialize == 1) {
1973 TEST_ASSERT(options->dtls == 1);
1974
1975 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1976 0, &context_buf_len)
1977 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1978
1979 context_buf = mbedtls_calloc(1, context_buf_len);
1980 TEST_ASSERT(context_buf != NULL);
1981
1982 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1983 context_buf_len,
1984 &context_buf_len)
1985 == 0);
1986
1987 mbedtls_ssl_free(&(server.ssl));
1988 mbedtls_ssl_init(&(server.ssl));
1989
1990 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
1991
1992 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
1993 mbedtls_test_mock_tcp_send_msg,
1994 mbedtls_test_mock_tcp_recv_msg,
1995 NULL);
1996
1997 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
1998
1999 #if defined(MBEDTLS_TIMING_C)
2000 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2001 mbedtls_timing_set_delay,
2002 mbedtls_timing_get_delay);
2003 #endif
2004 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2005 if (options->resize_buffers != 0) {
2006 /* Ensure that the buffer sizes are appropriate before resizes */
2007 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2008 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2009 }
2010 #endif
2011 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2012 context_buf_len) == 0);
2013
2014 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2015 /* Validate buffer sizes after context deserialization */
2016 if (options->resize_buffers != 0) {
2017 TEST_ASSERT(server.ssl.out_buf_len ==
2018 mbedtls_ssl_get_output_buflen(&server.ssl));
2019 TEST_ASSERT(server.ssl.in_buf_len ==
2020 mbedtls_ssl_get_input_buflen(&server.ssl));
2021 }
2022 #endif
2023 /* Retest writing/reading */
2024 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2025 TEST_ASSERT(mbedtls_exchange_data(
2026 &(client.ssl),
2027 options->cli_msg_len,
2028 options->expected_cli_fragments,
2029 &(server.ssl),
2030 options->srv_msg_len,
2031 options->expected_srv_fragments)
2032 == 0);
2033 }
2034 }
2035 #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2036
2037 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2038 if (options->renegotiate) {
2039 /* Start test with renegotiation */
2040 TEST_ASSERT(server.ssl.renego_status ==
2041 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2042 TEST_ASSERT(client.ssl.renego_status ==
2043 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2044
2045 /* After calling this function for the server, it only sends a handshake
2046 * request. All renegotiation should happen during data exchanging */
2047 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
2048 TEST_ASSERT(server.ssl.renego_status ==
2049 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2050 TEST_ASSERT(client.ssl.renego_status ==
2051 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2052
2053 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2054 TEST_ASSERT(server.ssl.renego_status ==
2055 MBEDTLS_SSL_RENEGOTIATION_DONE);
2056 TEST_ASSERT(client.ssl.renego_status ==
2057 MBEDTLS_SSL_RENEGOTIATION_DONE);
2058
2059 /* After calling mbedtls_ssl_renegotiate for the client,
2060 * all renegotiation should happen inside this function.
2061 * However in this test, we cannot perform simultaneous communication
2062 * between client and server so this function will return waiting error
2063 * on the socket. All rest of renegotiation should happen
2064 * during data exchanging */
2065 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2066 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2067 if (options->resize_buffers != 0) {
2068 /* Ensure that the buffer sizes are appropriate before resizes */
2069 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2070 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2071 }
2072 #endif
2073 TEST_ASSERT(ret == 0 ||
2074 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2075 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2076 TEST_ASSERT(server.ssl.renego_status ==
2077 MBEDTLS_SSL_RENEGOTIATION_DONE);
2078 TEST_ASSERT(client.ssl.renego_status ==
2079 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2080
2081 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2082 TEST_ASSERT(server.ssl.renego_status ==
2083 MBEDTLS_SSL_RENEGOTIATION_DONE);
2084 TEST_ASSERT(client.ssl.renego_status ==
2085 MBEDTLS_SSL_RENEGOTIATION_DONE);
2086 #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2087 /* Validate buffer sizes after renegotiation */
2088 if (options->resize_buffers != 0) {
2089 TEST_ASSERT(client.ssl.out_buf_len ==
2090 mbedtls_ssl_get_output_buflen(&client.ssl));
2091 TEST_ASSERT(client.ssl.in_buf_len ==
2092 mbedtls_ssl_get_input_buflen(&client.ssl));
2093 TEST_ASSERT(server.ssl.out_buf_len ==
2094 mbedtls_ssl_get_output_buflen(&server.ssl));
2095 TEST_ASSERT(server.ssl.in_buf_len ==
2096 mbedtls_ssl_get_input_buflen(&server.ssl));
2097 }
2098 #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2099 }
2100 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2101
2102 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2103 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2104 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2105 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2106
2107 exit:
2108 mbedtls_test_ssl_endpoint_free(&client,
2109 options->dtls != 0 ? &client_context : NULL);
2110 mbedtls_test_ssl_endpoint_free(&server,
2111 options->dtls != 0 ? &server_context : NULL);
2112 #if defined(MBEDTLS_DEBUG_C)
2113 if (options->cli_log_fun || options->srv_log_fun) {
2114 mbedtls_debug_set_threshold(0);
2115 }
2116 #endif
2117 #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2118 if (context_buf != NULL) {
2119 mbedtls_free(context_buf);
2120 }
2121 #endif
2122 USE_PSA_DONE();
2123 }
2124 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2125
2126 #if defined(MBEDTLS_TEST_HOOKS)
tweak_tls13_certificate_msg_vector_len(unsigned char * buf,unsigned char ** end,int tweak,int * expected_result,mbedtls_ssl_chk_buf_ptr_args * args)2127 int tweak_tls13_certificate_msg_vector_len(
2128 unsigned char *buf, unsigned char **end, int tweak,
2129 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2130 {
2131 /*
2132 * The definition of the tweaks assume that the certificate list contains only
2133 * one certificate.
2134 */
2135
2136 /*
2137 * struct {
2138 * opaque cert_data<1..2^24-1>;
2139 * Extension extensions<0..2^16-1>;
2140 * } CertificateEntry;
2141 *
2142 * struct {
2143 * opaque certificate_request_context<0..2^8-1>;
2144 * CertificateEntry certificate_list<0..2^24-1>;
2145 * } Certificate;
2146 */
2147 unsigned char *p_certificate_request_context_len = buf;
2148 size_t certificate_request_context_len = buf[0];
2149
2150 unsigned char *p_certificate_list_len =
2151 buf + 1 + certificate_request_context_len;
2152 unsigned char *certificate_list = p_certificate_list_len + 3;
2153 size_t certificate_list_len =
2154 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2155
2156 unsigned char *p_cert_data_len = certificate_list;
2157 unsigned char *cert_data = p_cert_data_len + 3;
2158 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2159
2160 unsigned char *p_extensions_len = cert_data + cert_data_len;
2161 unsigned char *extensions = p_extensions_len + 2;
2162 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2163
2164 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2165
2166 switch (tweak) {
2167 case 1:
2168 /* Failure when checking if the certificate request context length
2169 * and certificate list length can be read
2170 */
2171 *end = buf + 3;
2172 set_chk_buf_ptr_args(args, buf, *end, 4);
2173 break;
2174
2175 case 2:
2176 /* Invalid certificate request context length.
2177 */
2178 *p_certificate_request_context_len =
2179 (unsigned char) certificate_request_context_len + 1;
2180 reset_chk_buf_ptr_args(args);
2181 break;
2182
2183 case 3:
2184 /* Failure when checking if certificate_list data can be read. */
2185 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2186 p_certificate_list_len, 0);
2187 set_chk_buf_ptr_args(args, certificate_list, *end,
2188 certificate_list_len + 1);
2189 break;
2190
2191 case 4:
2192 /* Failure when checking if the cert_data length can be read. */
2193 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2194 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2195 break;
2196
2197 case 5:
2198 /* Failure when checking if cert_data data can be read. */
2199 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2200 p_cert_data_len, 0);
2201 set_chk_buf_ptr_args(args, cert_data,
2202 certificate_list + certificate_list_len,
2203 certificate_list_len - 3 + 1);
2204 break;
2205
2206 case 6:
2207 /* Failure when checking if the extensions length can be read. */
2208 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2209 p_certificate_list_len, 0);
2210 set_chk_buf_ptr_args(
2211 args, p_extensions_len,
2212 certificate_list + certificate_list_len - extensions_len - 1, 2);
2213 break;
2214
2215 case 7:
2216 /* Failure when checking if extensions data can be read. */
2217 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2218
2219 set_chk_buf_ptr_args(
2220 args, extensions,
2221 certificate_list + certificate_list_len, extensions_len + 1);
2222 break;
2223
2224 default:
2225 return -1;
2226 }
2227
2228 return 0;
2229 }
2230 #endif /* MBEDTLS_TEST_HOOKS */
2231 #endif /* MBEDTLS_SSL_TLS_C */
2232