blob: 617dd8885a75294fe2a8735443b8800d31132432 [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
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020010void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000011{
12 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020013 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000014 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010016 memcpy(p, file, strlen(file));
17 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020018
19 *p++ = '(';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010020 *p++ = '0' + (line / 1000) % 10;
21 *p++ = '0' + (line / 100) % 10;
22 *p++ = '0' + (line / 10) % 10;
23 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020024 *p++ = ')';
25 *p++ = ':';
26 *p++ = ' ';
27
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020028#if defined(MBEDTLS_THREADING_C)
29 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010030 while (*str++ != ' ') {
31 ;
32 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020033#endif
34
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 memcpy(p, str, strlen(str));
36 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020037
38 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020040 *p++ = '*';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020042
43 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000044}
Paul Bakker33b43f12013-08-20 11:48:36 +020045/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000046
Paul Bakker33b43f12013-08-20 11:48:36 +020047/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020049 * END_DEPENDENCIES
50 */
Paul Bakker5690efc2011-05-26 13:16:06 +000051
Paul Bakker57ffa552014-04-25 14:29:10 +020052/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053void debug_print_msg_threshold(int threshold, int level, char *file,
54 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020057 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020058 struct buffer_data buffer;
59
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 mbedtls_ssl_init(&ssl);
61 mbedtls_ssl_config_init(&conf);
62 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020063 buffer.ptr = buffer.buf;
64
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 mbedtls_debug_set_threshold(threshold);
68 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 mbedtls_debug_print_msg(&ssl, level, file, line,
71 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020074
75exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_ssl_free(&ssl);
77 mbedtls_ssl_config_free(&conf);
Paul Bakkerc73079a2014-04-25 16:34:30 +020078}
79/* END_CASE */
80
81/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
83 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020086 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020087 struct buffer_data buffer;
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 mbedtls_ssl_init(&ssl);
90 mbedtls_ssl_config_init(&conf);
91 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +020092 buffer.ptr = buffer.buf;
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +020097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200101
102exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 mbedtls_ssl_free(&ssl);
104 mbedtls_ssl_config_free(&conf);
Paul Bakker57ffa552014-04-25 14:29:10 +0200105}
106/* END_CASE */
107
108/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109void mbedtls_debug_print_buf(char *file, int line, char *text,
110 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200113 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200114 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_ssl_init(&ssl);
117 mbedtls_ssl_config_init(&conf);
118 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200119 buffer.ptr = buffer.buf;
120
Paul Bakker57ffa552014-04-25 14:29:10 +0200121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200129
130exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 mbedtls_ssl_free(&ssl);
132 mbedtls_ssl_config_free(&conf);
Paul Bakker57ffa552014-04-25 14:29:10 +0200133}
134/* END_CASE */
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
138 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_crt crt;
141 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200142 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000143 struct buffer_data buffer;
144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 mbedtls_ssl_init(&ssl);
146 mbedtls_ssl_config_init(&conf);
147 mbedtls_x509_crt_init(&crt);
148 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200149 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
156 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100159
Paul Bakkerbd51b262014-07-10 15:26:12 +0200160exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 mbedtls_x509_crt_free(&crt);
162 mbedtls_ssl_free(&ssl);
163 mbedtls_ssl_config_free(&conf);
Paul Bakker1f761152010-02-18 18:16:31 +0000164}
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168void mbedtls_debug_print_mpi(char *value, char *file, int line,
169 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200172 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000173 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 mbedtls_ssl_init(&ssl);
177 mbedtls_ssl_config_init(&conf);
178 mbedtls_mpi_init(&val);
179 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200180 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 mbedtls_mpi_free(&val);
194 mbedtls_ssl_free(&ssl);
195 mbedtls_ssl_config_free(&conf);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000196}
Paul Bakker33b43f12013-08-20 11:48:36 +0200197/* END_CASE */
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400198
199/* BEGIN_CASE */
200void check_mbedtls_calloc_overallocation(int num, int size)
201{
202 unsigned char *buf;
203 buf = mbedtls_calloc((size_t) num, (size_t) size);
Andrzej Kurekf1e61fc2023-07-14 10:16:00 -0400204 /* Dummy usage of the pointer to prevent optimizing it */
205 mbedtls_printf("calloc pointer : %p\n", buf);
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400206 TEST_ASSERT(buf == NULL);
207
208exit:
209 mbedtls_free(buf);
210}
211/* END_CASE */