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 "ssl_port.h" 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 #ifdef CONFIG_OPENSSL_DEBUG_LEVEL 25 #define SSL_DEBUG_LEVEL CONFIG_OPENSSL_DEBUG_LEVEL 26 #else 27 #define SSL_DEBUG_LEVEL 0 28 #endif 29 30 #define SSL_DEBUG_ON (SSL_DEBUG_LEVEL + 1) 31 #define SSL_DEBUG_OFF (SSL_DEBUG_LEVEL - 1) 32 33 #ifdef CONFIG_OPENSSL_DEBUG 34 #ifndef SSL_DEBUG_LOG 35 #error "SSL_DEBUG_LOG is not defined" 36 #endif 37 38 #ifndef SSL_DEBUG_FL 39 #define SSL_DEBUG_FL "\n" 40 #endif 41 42 #define SSL_SHOW_LOCATION() \ 43 SSL_DEBUG_LOG("SSL assert : %s %d\n", \ 44 __FILE__, __LINE__) 45 46 #define SSL_DEBUG(level, fmt, ...) \ 47 { \ 48 if (level > SSL_DEBUG_LEVEL) { \ 49 SSL_DEBUG_LOG(fmt SSL_DEBUG_FL, ##__VA_ARGS__); \ 50 } \ 51 } 52 #else /* CONFIG_OPENSSL_DEBUG */ 53 #define SSL_SHOW_LOCATION() 54 55 #define SSL_DEBUG(level, fmt, ...) 56 #endif /* CONFIG_OPENSSL_DEBUG */ 57 58 /** 59 * OpenSSL assert function 60 * 61 * if select "CONFIG_OPENSSL_ASSERT_DEBUG", SSL_ASSERT* will show error file name and line 62 * if select "CONFIG_OPENSSL_ASSERT_EXIT", SSL_ASSERT* will just return error code. 63 * if select "CONFIG_OPENSSL_ASSERT_DEBUG_EXIT" SSL_ASSERT* will show error file name and line, 64 * then return error code. 65 * if select "CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK", SSL_ASSERT* will show error file name and line, 66 * then block here with "while (1)" 67 * 68 * SSL_ASSERT1 may will return "-1", so function's return argument is integer. 69 * SSL_ASSERT2 may will return "NULL", so function's return argument is a point. 70 * SSL_ASSERT2 may will return nothing, so function's return argument is "void". 71 */ 72 #if defined(CONFIG_OPENSSL_ASSERT_DEBUG) 73 #define SSL_ASSERT1(s) \ 74 { \ 75 if (!(s)) { \ 76 SSL_SHOW_LOCATION(); \ 77 } \ 78 } 79 80 #define SSL_ASSERT2(s) \ 81 { \ 82 if (!(s)) { \ 83 SSL_SHOW_LOCATION(); \ 84 } \ 85 } 86 87 #define SSL_ASSERT3(s) \ 88 { \ 89 if (!(s)) { \ 90 SSL_SHOW_LOCATION(); \ 91 } \ 92 } 93 #elif defined(CONFIG_OPENSSL_ASSERT_EXIT) 94 #define SSL_ASSERT1(s) \ 95 { \ 96 if (!(s)) { \ 97 return -1; \ 98 } \ 99 } 100 101 #define SSL_ASSERT2(s) \ 102 { \ 103 if (!(s)) { \ 104 return NULL; \ 105 } \ 106 } 107 108 #define SSL_ASSERT3(s) \ 109 { \ 110 if (!(s)) { \ 111 return ; \ 112 } \ 113 } 114 #elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_EXIT) 115 #define SSL_ASSERT1(s) \ 116 { \ 117 if (!(s)) { \ 118 SSL_SHOW_LOCATION(); \ 119 return -1; \ 120 } \ 121 } 122 123 #define SSL_ASSERT2(s) \ 124 { \ 125 if (!(s)) { \ 126 SSL_SHOW_LOCATION(); \ 127 return NULL; \ 128 } \ 129 } 130 131 #define SSL_ASSERT3(s) \ 132 { \ 133 if (!(s)) { \ 134 SSL_SHOW_LOCATION(); \ 135 return ; \ 136 } \ 137 } 138 #elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK) 139 #define SSL_ASSERT1(s) \ 140 { \ 141 if (!(s)) { \ 142 SSL_SHOW_LOCATION(); \ 143 while (1); \ 144 } \ 145 } 146 147 #define SSL_ASSERT2(s) \ 148 { \ 149 if (!(s)) { \ 150 SSL_SHOW_LOCATION(); \ 151 while (1); \ 152 } \ 153 } 154 155 #define SSL_ASSERT3(s) \ 156 { \ 157 if (!(s)) { \ 158 SSL_SHOW_LOCATION(); \ 159 while (1); \ 160 } \ 161 } 162 #else 163 #define SSL_ASSERT1(s) 164 #define SSL_ASSERT2(s) 165 #define SSL_ASSERT3(s) 166 #endif 167 168 #define SSL_PLATFORM_DEBUG_LEVEL SSL_DEBUG_OFF 169 #define SSL_PLATFORM_ERROR_LEVEL SSL_DEBUG_ON 170 171 #define SSL_CERT_DEBUG_LEVEL SSL_DEBUG_OFF 172 #define SSL_CERT_ERROR_LEVEL SSL_DEBUG_ON 173 174 #define SSL_PKEY_DEBUG_LEVEL SSL_DEBUG_OFF 175 #define SSL_PKEY_ERROR_LEVEL SSL_DEBUG_ON 176 177 #define SSL_X509_DEBUG_LEVEL SSL_DEBUG_OFF 178 #define SSL_X509_ERROR_LEVEL SSL_DEBUG_ON 179 180 #define SSL_LIB_DEBUG_LEVEL SSL_DEBUG_OFF 181 #define SSL_LIB_ERROR_LEVEL SSL_DEBUG_ON 182 183 #define SSL_STACK_DEBUG_LEVEL SSL_DEBUG_OFF 184 #define SSL_STACK_ERROR_LEVEL SSL_DEBUG_ON 185 186 #ifdef __cplusplus 187 } 188 #endif 189 190 #endif 191