blob: 6c58d56bc8e7f3956dba244a5a162f456bf759ba [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úti94b0eea2025-02-28 22:32:15 +010054/* BEGIN_CASE */
55void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
56 intmax_t sizeof_x, intmax_t x, char *result)
57{
58 const char *format = (char *) ((uintptr_t) smuggle_format_expr);
59 char *output = NULL;
60 const size_t n = strlen(result);
61
62 /* Nominal case: buffer just large enough */
63 TEST_CALLOC(output, n + 1);
64 if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
65 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
66 } else if (sizeof_x == sizeof(long)) {
67 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
68 } else if (sizeof_x == sizeof(long long)) {
69 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
70 } else {
71 TEST_FAIL(
72 "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
73 }
74 TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
75
76exit:
77 mbedtls_free(output);
78 output = NULL;
79}
80/* END_CASE */
81
Bence Szépkúti27da54d2025-02-28 16:22:33 +010082/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083void debug_print_msg_threshold(int threshold, int level, char *file,
84 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020087 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020088 struct buffer_data buffer;
89
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 mbedtls_ssl_init(&ssl);
91 mbedtls_ssl_config_init(&conf);
92 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020093 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +020094 USE_PSA_INIT();
Paul Bakkerc73079a2014-04-25 16:34:30 +020095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 mbedtls_debug_set_threshold(threshold);
99 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 mbedtls_debug_print_msg(&ssl, level, file, line,
102 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200105
106exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 mbedtls_ssl_free(&ssl);
108 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200109 USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200110}
111/* END_CASE */
112
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100113/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
115 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200118 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200119 struct buffer_data buffer;
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_ssl_init(&ssl);
122 mbedtls_ssl_config_init(&conf);
123 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200124 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200125 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200134
135exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_ssl_free(&ssl);
137 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200138 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200139}
140/* END_CASE */
141
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100142/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143void mbedtls_debug_print_buf(char *file, int line, char *text,
144 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200147 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200148 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_ssl_init(&ssl);
151 mbedtls_ssl_config_init(&conf);
152 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200153 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200154 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200163
164exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 mbedtls_ssl_free(&ssl);
166 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200167 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200168}
169/* END_CASE */
170
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100171/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
173 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_x509_crt crt;
176 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200177 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000178 struct buffer_data buffer;
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 mbedtls_ssl_init(&ssl);
181 mbedtls_ssl_config_init(&conf);
182 mbedtls_x509_crt_init(&crt);
Valerio Settic6240f72023-05-23 10:44:08 +0200183 USE_PSA_INIT();
184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200186 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
193 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100196
Paul Bakkerbd51b262014-07-10 15:26:12 +0200197exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_x509_crt_free(&crt);
199 mbedtls_ssl_free(&ssl);
200 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200201 USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000202}
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000204
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100205/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206void mbedtls_debug_print_mpi(char *value, char *file, int line,
207 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200210 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000211 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 mbedtls_ssl_init(&ssl);
215 mbedtls_ssl_config_init(&conf);
216 mbedtls_mpi_init(&val);
217 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200218 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200219 USE_PSA_INIT();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000230
Paul Bakkerbd51b262014-07-10 15:26:12 +0200231exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 mbedtls_mpi_free(&val);
233 mbedtls_ssl_free(&ssl);
234 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200235 USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000236}
Paul Bakker33b43f12013-08-20 11:48:36 +0200237/* END_CASE */
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400238
239/* BEGIN_CASE */
240void check_mbedtls_calloc_overallocation(int num, int size)
241{
242 unsigned char *buf;
Andrzej Kurek0841b5a2023-07-14 15:16:35 -0400243 buf = mbedtls_calloc((size_t) num * SIZE_MAX/2, (size_t) size * SIZE_MAX/2);
Andrzej Kurekf1e61fc2023-07-14 10:16:00 -0400244 /* Dummy usage of the pointer to prevent optimizing it */
245 mbedtls_printf("calloc pointer : %p\n", buf);
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400246 TEST_ASSERT(buf == NULL);
247
248exit:
249 mbedtls_free(buf);
250}
251/* END_CASE */