1/* BEGIN_HEADER */ 2#include "mbedtls/debug.h" 3#include "string.h" 4#include "mbedtls/legacy_or_psa.h" 5#include "mbedtls/pk.h" 6 7struct buffer_data { 8 char buf[2000]; 9 char *ptr; 10}; 11 12void string_debug(void *data, int level, const char *file, int line, const char *str) 13{ 14 struct buffer_data *buffer = (struct buffer_data *) data; 15 char *p = buffer->ptr; 16 ((void) level); 17 18 memcpy(p, file, strlen(file)); 19 p += strlen(file); 20 21 *p++ = '('; 22 *p++ = '0' + (line / 1000) % 10; 23 *p++ = '0' + (line / 100) % 10; 24 *p++ = '0' + (line / 10) % 10; 25 *p++ = '0' + (line / 1) % 10; 26 *p++ = ')'; 27 *p++ = ':'; 28 *p++ = ' '; 29 30#if defined(MBEDTLS_THREADING_C) 31 /* Skip "thread ID" (up to the first space) as it is not predictable */ 32 while (*str++ != ' ') { 33 ; 34 } 35#endif 36 37 memcpy(p, str, strlen(str)); 38 p += strlen(str); 39 40 /* Detect if debug messages output partial lines and mark them */ 41 if (p[-1] != '\n') { 42 *p++ = '*'; 43 } 44 45 buffer->ptr = p; 46} 47/* END_HEADER */ 48 49/* BEGIN_DEPENDENCIES 50 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C 51 * END_DEPENDENCIES 52 */ 53 54/* BEGIN_CASE */ 55void debug_print_msg_threshold(int threshold, int level, char *file, 56 int line, char *result_str) 57{ 58 mbedtls_ssl_context ssl; 59 mbedtls_ssl_config conf; 60 struct buffer_data buffer; 61 62 mbedtls_ssl_init(&ssl); 63 mbedtls_ssl_config_init(&conf); 64 memset(buffer.buf, 0, 2000); 65 buffer.ptr = buffer.buf; 66 67 mbedtls_ssl_config_defaults(&conf, 68 MBEDTLS_SSL_IS_CLIENT, 69 MBEDTLS_SSL_TRANSPORT_STREAM, 70 MBEDTLS_SSL_PRESET_DEFAULT); 71 72 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); 73 74 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); 75 76 mbedtls_debug_set_threshold(threshold); 77 78 mbedtls_debug_print_msg(&ssl, level, file, line, 79 "Text message, 2 == %d", 2); 80 81 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); 82 83exit: 84 mbedtls_ssl_free(&ssl); 85 mbedtls_ssl_config_free(&conf); 86} 87/* END_CASE */ 88 89/* BEGIN_CASE */ 90void mbedtls_debug_print_ret(char *file, int line, char *text, int value, 91 char *result_str) 92{ 93 mbedtls_ssl_context ssl; 94 mbedtls_ssl_config conf; 95 struct buffer_data buffer; 96 97 mbedtls_ssl_init(&ssl); 98 mbedtls_ssl_config_init(&conf); 99 memset(buffer.buf, 0, 2000); 100 buffer.ptr = buffer.buf; 101 102 mbedtls_ssl_config_defaults(&conf, 103 MBEDTLS_SSL_IS_CLIENT, 104 MBEDTLS_SSL_TRANSPORT_STREAM, 105 MBEDTLS_SSL_PRESET_DEFAULT); 106 107 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); 108 109 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); 110 111 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value); 112 113 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); 114 115exit: 116 mbedtls_ssl_free(&ssl); 117 mbedtls_ssl_config_free(&conf); 118} 119/* END_CASE */ 120 121/* BEGIN_CASE */ 122void mbedtls_debug_print_buf(char *file, int line, char *text, 123 data_t *data, char *result_str) 124{ 125 mbedtls_ssl_context ssl; 126 mbedtls_ssl_config conf; 127 struct buffer_data buffer; 128 129 mbedtls_ssl_init(&ssl); 130 mbedtls_ssl_config_init(&conf); 131 memset(buffer.buf, 0, 2000); 132 buffer.ptr = buffer.buf; 133 134 mbedtls_ssl_config_defaults(&conf, 135 MBEDTLS_SSL_IS_CLIENT, 136 MBEDTLS_SSL_TRANSPORT_STREAM, 137 MBEDTLS_SSL_PRESET_DEFAULT); 138 139 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); 140 141 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); 142 143 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len); 144 145 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); 146 147exit: 148 mbedtls_ssl_free(&ssl); 149 mbedtls_ssl_config_free(&conf); 150} 151/* END_CASE */ 152 153/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ 154void mbedtls_debug_print_crt(char *crt_file, char *file, int line, 155 char *prefix, char *result_str) 156{ 157 mbedtls_x509_crt crt; 158 mbedtls_ssl_context ssl; 159 mbedtls_ssl_config conf; 160 struct buffer_data buffer; 161 162 mbedtls_ssl_init(&ssl); 163 mbedtls_ssl_config_init(&conf); 164 mbedtls_x509_crt_init(&crt); 165 memset(buffer.buf, 0, 2000); 166 buffer.ptr = buffer.buf; 167 168 mbedtls_ssl_config_defaults(&conf, 169 MBEDTLS_SSL_IS_CLIENT, 170 MBEDTLS_SSL_TRANSPORT_STREAM, 171 MBEDTLS_SSL_PRESET_DEFAULT); 172 173 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); 174 175 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); 176 177 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0); 178 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt); 179 180 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); 181 182exit: 183 mbedtls_x509_crt_free(&crt); 184 mbedtls_ssl_free(&ssl); 185 mbedtls_ssl_config_free(&conf); 186} 187/* END_CASE */ 188 189/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */ 190void mbedtls_debug_print_mpi(char *value, char *file, int line, 191 char *prefix, char *result_str) 192{ 193 mbedtls_ssl_context ssl; 194 mbedtls_ssl_config conf; 195 struct buffer_data buffer; 196 mbedtls_mpi val; 197 198 mbedtls_ssl_init(&ssl); 199 mbedtls_ssl_config_init(&conf); 200 mbedtls_mpi_init(&val); 201 memset(buffer.buf, 0, 2000); 202 buffer.ptr = buffer.buf; 203 204 mbedtls_ssl_config_defaults(&conf, 205 MBEDTLS_SSL_IS_CLIENT, 206 MBEDTLS_SSL_TRANSPORT_STREAM, 207 MBEDTLS_SSL_PRESET_DEFAULT); 208 209 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); 210 211 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); 212 213 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0); 214 215 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val); 216 217 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); 218 219exit: 220 mbedtls_mpi_free(&val); 221 mbedtls_ssl_free(&ssl); 222 mbedtls_ssl_config_free(&conf); 223} 224/* END_CASE */ 225