1 /*
2  *  Message Processing Stack, Trace module
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of Mbed TLS (https://tls.mbed.org)
20  */
21 
22 #include "common.h"
23 
24 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
25 
26 #include "mps_common.h"
27 
28 #if defined(MBEDTLS_MPS_ENABLE_TRACE)
29 
30 #include "mps_trace.h"
31 #include <stdarg.h>
32 
33 static int trace_depth = 0;
34 
35 #define color_default  "\x1B[0m"
36 #define color_red      "\x1B[1;31m"
37 #define color_green    "\x1B[1;32m"
38 #define color_yellow   "\x1B[1;33m"
39 #define color_blue     "\x1B[1;34m"
40 #define color_magenta  "\x1B[1;35m"
41 #define color_cyan     "\x1B[1;36m"
42 #define color_white    "\x1B[1;37m"
43 
44 static char const *colors[] =
45 {
46     color_default,
47     color_green,
48     color_yellow,
49     color_magenta,
50     color_cyan,
51     color_blue,
52     color_white
53 };
54 
55 #define MPS_TRACE_BUF_SIZE 100
56 
mbedtls_mps_trace_print_msg(int id,int line,const char * format,...)57 void mbedtls_mps_trace_print_msg(int id, int line, const char *format, ...)
58 {
59     int ret;
60     char str[MPS_TRACE_BUF_SIZE];
61     va_list argp;
62     va_start(argp, format);
63     ret = mbedtls_vsnprintf(str, MPS_TRACE_BUF_SIZE, format, argp);
64     va_end(argp);
65 
66     if (ret >= 0 && ret < MPS_TRACE_BUF_SIZE) {
67         str[ret] = '\0';
68         mbedtls_printf("[%d|L%d]: %s\n", id, line, str);
69     }
70 }
71 
mbedtls_mps_trace_get_depth()72 int mbedtls_mps_trace_get_depth()
73 {
74     return trace_depth;
75 }
mbedtls_mps_trace_dec_depth()76 void mbedtls_mps_trace_dec_depth()
77 {
78     trace_depth--;
79 }
mbedtls_mps_trace_inc_depth()80 void mbedtls_mps_trace_inc_depth()
81 {
82     trace_depth++;
83 }
84 
mbedtls_mps_trace_color(int id)85 void mbedtls_mps_trace_color(int id)
86 {
87     if (id > (int) (sizeof(colors) / sizeof(*colors))) {
88         return;
89     }
90     printf("%s", colors[id]);
91 }
92 
mbedtls_mps_trace_indent(int level,mbedtls_mps_trace_type ty)93 void mbedtls_mps_trace_indent(int level, mbedtls_mps_trace_type ty)
94 {
95     if (level > 0) {
96         while (--level) {
97             printf("|  ");
98         }
99 
100         printf("|  ");
101     }
102 
103     switch (ty) {
104         case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
105             mbedtls_printf("@ ");
106             break;
107 
108         case MBEDTLS_MPS_TRACE_TYPE_CALL:
109             mbedtls_printf("+--> ");
110             break;
111 
112         case MBEDTLS_MPS_TRACE_TYPE_ERROR:
113             mbedtls_printf("E ");
114             break;
115 
116         case MBEDTLS_MPS_TRACE_TYPE_RETURN:
117             mbedtls_printf("< ");
118             break;
119 
120         default:
121             break;
122     }
123 }
124 
125 #endif /* MBEDTLS_MPS_ENABLE_TRACE */
126 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
127