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