1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _SSL_DEBUG_H_
16 #define _SSL_DEBUG_H_
17 
18 #include "platform/ssl_opt.h"
19 #include "platform/ssl_port.h"
20 
21 #ifdef __cplusplus
22  extern "C" {
23 #endif
24 
25 #ifdef CONFIG_OPENSSL_DEBUG_LEVEL
26     #define SSL_DEBUG_LEVEL CONFIG_OPENSSL_DEBUG_LEVEL
27 #else
28     #define SSL_DEBUG_LEVEL 0
29 #endif
30 
31 #define SSL_DEBUG_ON  (SSL_DEBUG_LEVEL + 1)
32 #define SSL_DEBUG_OFF (SSL_DEBUG_LEVEL - 1)
33 
34 #ifdef CONFIG_OPENSSL_DEBUG
35     #ifndef SSL_DEBUG_LOG
36         #error "SSL_DEBUG_LOG is not defined"
37     #endif
38 
39     #ifndef SSL_DEBUG_FL
40         #define SSL_DEBUG_FL "\n"
41     #endif
42 
43     #define SSL_SHOW_LOCATION()                         \
44         SSL_DEBUG_LOG("SSL assert : %s %d\n",           \
45             __FILE__, __LINE__)
46 
47     #define SSL_DEBUG(level, fmt, ...)                  \
48     {                                                   \
49         if (level > SSL_DEBUG_LEVEL) {                  \
50             SSL_DEBUG_LOG(fmt SSL_DEBUG_FL, ##__VA_ARGS__); \
51         }                                               \
52     }
53 #else /* CONFIG_OPENSSL_DEBUG */
54     #define SSL_SHOW_LOCATION()
55 
56     #define SSL_DEBUG(level, fmt, ...)
57 #endif /* CONFIG_OPENSSL_DEBUG */
58 
59 /**
60  * OpenSSL assert function
61  *
62  * if select "CONFIG_OPENSSL_ASSERT_DEBUG", SSL_ASSERT* will show error file name and line
63  * if select "CONFIG_OPENSSL_ASSERT_EXIT", SSL_ASSERT* will just return error code.
64  * if select "CONFIG_OPENSSL_ASSERT_DEBUG_EXIT" SSL_ASSERT* will show error file name and line,
65  * then return error code.
66  * if select "CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK", SSL_ASSERT* will show error file name and line,
67  * then block here with "while (1)"
68  *
69  * SSL_ASSERT1 may will return "-1", so function's return argument is integer.
70  * SSL_ASSERT2 may will return "NULL", so function's return argument is a point.
71  * SSL_ASSERT2 may will return nothing, so function's return argument is "void".
72  */
73 #if defined(CONFIG_OPENSSL_ASSERT_DEBUG)
74     #define SSL_ASSERT1(s)                              \
75     {                                                   \
76         if (!(s)) {                                     \
77             SSL_SHOW_LOCATION();                        \
78         }                                               \
79     }
80 
81     #define SSL_ASSERT2(s)                              \
82     {                                                   \
83         if (!(s)) {                                     \
84             SSL_SHOW_LOCATION();                        \
85         }                                               \
86     }
87 
88     #define SSL_ASSERT3(s)                              \
89     {                                                   \
90         if (!(s)) {                                     \
91             SSL_SHOW_LOCATION();                        \
92         }                                               \
93     }
94 #elif defined(CONFIG_OPENSSL_ASSERT_EXIT)
95     #define SSL_ASSERT1(s)                              \
96     {                                                   \
97         if (!(s)) {                                     \
98             return -1;                                  \
99         }                                               \
100     }
101 
102     #define SSL_ASSERT2(s)                              \
103     {                                                   \
104         if (!(s)) {                                     \
105             return NULL;                                \
106         }                                               \
107     }
108 
109     #define SSL_ASSERT3(s)                              \
110     {                                                   \
111         if (!(s)) {                                     \
112             return ;                                    \
113         }                                               \
114     }
115 #elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_EXIT)
116     #define SSL_ASSERT1(s)                              \
117     {                                                   \
118         if (!(s)) {                                     \
119             SSL_SHOW_LOCATION();                        \
120             return -1;                                  \
121         }                                               \
122     }
123 
124     #define SSL_ASSERT2(s)                              \
125     {                                                   \
126         if (!(s)) {                                     \
127             SSL_SHOW_LOCATION();                        \
128             return NULL;                                \
129         }                                               \
130     }
131 
132     #define SSL_ASSERT3(s)                              \
133     {                                                   \
134         if (!(s)) {                                     \
135             SSL_SHOW_LOCATION();                        \
136             return ;                                    \
137         }                                               \
138     }
139 #elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK)
140     #define SSL_ASSERT1(s)                              \
141     {                                                   \
142         if (!(s)) {                                     \
143             SSL_SHOW_LOCATION();                        \
144             while (1);                                  \
145         }                                               \
146     }
147 
148     #define SSL_ASSERT2(s)                              \
149     {                                                   \
150         if (!(s)) {                                     \
151             SSL_SHOW_LOCATION();                        \
152             while (1);                                  \
153         }                                               \
154     }
155 
156     #define SSL_ASSERT3(s)                              \
157     {                                                   \
158         if (!(s)) {                                     \
159             SSL_SHOW_LOCATION();                        \
160             while (1);                                  \
161         }                                               \
162     }
163 #else
164     #define SSL_ASSERT1(s)
165     #define SSL_ASSERT2(s)
166     #define SSL_ASSERT3(s)
167 #endif
168 
169 #define SSL_PLATFORM_DEBUG_LEVEL SSL_DEBUG_OFF
170 #define SSL_PLATFORM_ERROR_LEVEL SSL_DEBUG_ON
171 
172 #define SSL_CERT_DEBUG_LEVEL     SSL_DEBUG_OFF
173 #define SSL_CERT_ERROR_LEVEL     SSL_DEBUG_ON
174 
175 #define SSL_PKEY_DEBUG_LEVEL     SSL_DEBUG_OFF
176 #define SSL_PKEY_ERROR_LEVEL     SSL_DEBUG_ON
177 
178 #define SSL_X509_DEBUG_LEVEL     SSL_DEBUG_OFF
179 #define SSL_X509_ERROR_LEVEL     SSL_DEBUG_ON
180 
181 #define SSL_LIB_DEBUG_LEVEL      SSL_DEBUG_OFF
182 #define SSL_LIB_ERROR_LEVEL      SSL_DEBUG_ON
183 
184 #define SSL_STACK_DEBUG_LEVEL    SSL_DEBUG_OFF
185 #define SSL_STACK_ERROR_LEVEL    SSL_DEBUG_ON
186 
187 #ifdef __cplusplus
188  }
189 #endif
190 
191 #endif
192