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