blob: 4e85d62bfb4bcf7d798bc3afc200c749f9c3873d [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;
Valerio Settic6240f72023-05-23 10:44:08 +020064 USE_PSA_INIT();
Paul Bakkerc73079a2014-04-25 16:34:30 +020065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 mbedtls_debug_set_threshold(threshold);
69 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 mbedtls_debug_print_msg(&ssl, level, file, line,
72 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020075
76exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mbedtls_ssl_free(&ssl);
78 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +020079 USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020080}
81/* END_CASE */
82
83/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
85 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020088 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020089 struct buffer_data buffer;
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 mbedtls_ssl_init(&ssl);
92 mbedtls_ssl_config_init(&conf);
93 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +020094 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +020095 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +020096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200104
105exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_ssl_free(&ssl);
107 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200108 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200109}
110/* END_CASE */
111
112/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113void mbedtls_debug_print_buf(char *file, int line, char *text,
114 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200117 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200118 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 mbedtls_ssl_init(&ssl);
121 mbedtls_ssl_config_init(&conf);
122 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200123 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200124 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200133
134exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_ssl_free(&ssl);
136 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200137 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200138}
139/* END_CASE */
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
143 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_crt crt;
146 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200147 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000148 struct buffer_data buffer;
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_ssl_init(&ssl);
151 mbedtls_ssl_config_init(&conf);
152 mbedtls_x509_crt_init(&crt);
Valerio Settic6240f72023-05-23 10:44:08 +0200153 USE_PSA_INIT();
154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200156 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
163 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100166
Paul Bakkerbd51b262014-07-10 15:26:12 +0200167exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 mbedtls_x509_crt_free(&crt);
169 mbedtls_ssl_free(&ssl);
170 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200171 USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000172}
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176void mbedtls_debug_print_mpi(char *value, char *file, int line,
177 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200180 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000181 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 mbedtls_ssl_init(&ssl);
185 mbedtls_ssl_config_init(&conf);
186 mbedtls_mpi_init(&val);
187 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200188 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200189 USE_PSA_INIT();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000200
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 mbedtls_mpi_free(&val);
203 mbedtls_ssl_free(&ssl);
204 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200205 USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000206}
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* END_CASE */