blob: 8a632fcd913cc3e9ddb3cd4a1e6688cdfc0edbc8 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/debug.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Paul Bakker1f761152010-02-18 18:16:31 +00004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +00006 char buf[2000];
7 char *ptr;
8};
9
Bence Szépkúti27da54d2025-02-28 16:22:33 +010010#if defined(MBEDTLS_SSL_TLS_C)
11static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000012{
13 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020014 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000015 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010017 memcpy(p, file, strlen(file));
18 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020019
20 *p++ = '(';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021 *p++ = '0' + (line / 1000) % 10;
22 *p++ = '0' + (line / 100) % 10;
23 *p++ = '0' + (line / 10) % 10;
24 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020025 *p++ = ')';
26 *p++ = ':';
27 *p++ = ' ';
28
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020029#if defined(MBEDTLS_THREADING_C)
30 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031 while (*str++ != ' ') {
32 ;
33 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020034#endif
35
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036 memcpy(p, str, strlen(str));
37 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020038
39 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020041 *p++ = '*';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020043
44 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000045}
Bence Szépkúti27da54d2025-02-28 16:22:33 +010046#endif /* MBEDTLS_SSL_TLS_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020047/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000048
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* BEGIN_DEPENDENCIES
Bence Szépkúti27da54d2025-02-28 16:22:33 +010050 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020051 * END_DEPENDENCIES
52 */
Paul Bakker5690efc2011-05-26 13:16:06 +000053
Bence Szépkúti27da54d2025-02-28 16:22:33 +010054/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055void debug_print_msg_threshold(int threshold, int level, char *file,
56 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020059 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020060 struct buffer_data buffer;
61
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 mbedtls_ssl_init(&ssl);
63 mbedtls_ssl_config_init(&conf);
64 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020065 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +020066 USE_PSA_INIT();
Paul Bakkerc73079a2014-04-25 16:34:30 +020067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 mbedtls_debug_set_threshold(threshold);
71 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_debug_print_msg(&ssl, level, file, line,
74 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020077
78exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 mbedtls_ssl_free(&ssl);
80 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +020081 USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020082}
83/* END_CASE */
84
Bence Szépkúti27da54d2025-02-28 16:22:33 +010085/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
87 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020090 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020091 struct buffer_data buffer;
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 mbedtls_ssl_init(&ssl);
94 mbedtls_ssl_config_init(&conf);
95 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +020096 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +020097 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +020098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200106
107exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 mbedtls_ssl_free(&ssl);
109 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200110 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200111}
112/* END_CASE */
113
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100114/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115void mbedtls_debug_print_buf(char *file, int line, char *text,
116 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200119 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200120 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 mbedtls_ssl_init(&ssl);
123 mbedtls_ssl_config_init(&conf);
124 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200125 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200126 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200135
136exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 mbedtls_ssl_free(&ssl);
138 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200139 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200140}
141/* END_CASE */
142
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100143/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
145 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_crt crt;
148 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200149 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000150 struct buffer_data buffer;
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 mbedtls_ssl_init(&ssl);
153 mbedtls_ssl_config_init(&conf);
154 mbedtls_x509_crt_init(&crt);
Valerio Settic6240f72023-05-23 10:44:08 +0200155 USE_PSA_INIT();
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200158 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
165 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100168
Paul Bakkerbd51b262014-07-10 15:26:12 +0200169exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_x509_crt_free(&crt);
171 mbedtls_ssl_free(&ssl);
172 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200173 USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000174}
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000176
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100177/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178void mbedtls_debug_print_mpi(char *value, char *file, int line,
179 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200182 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000183 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_ssl_init(&ssl);
187 mbedtls_ssl_config_init(&conf);
188 mbedtls_mpi_init(&val);
189 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200190 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200191 USE_PSA_INIT();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000202
Paul Bakkerbd51b262014-07-10 15:26:12 +0200203exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 mbedtls_mpi_free(&val);
205 mbedtls_ssl_free(&ssl);
206 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200207 USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208}
Paul Bakker33b43f12013-08-20 11:48:36 +0200209/* END_CASE */
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400210
211/* BEGIN_CASE */
212void check_mbedtls_calloc_overallocation(int num, int size)
213{
214 unsigned char *buf;
Andrzej Kurek0841b5a2023-07-14 15:16:35 -0400215 buf = mbedtls_calloc((size_t) num * SIZE_MAX/2, (size_t) size * SIZE_MAX/2);
Andrzej Kurekf1e61fc2023-07-14 10:16:00 -0400216 /* Dummy usage of the pointer to prevent optimizing it */
217 mbedtls_printf("calloc pointer : %p\n", buf);
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400218 TEST_ASSERT(buf == NULL);
219
220exit:
221 mbedtls_free(buf);
222}
223/* END_CASE */