1 /* 2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #ifndef OPENSSL_TRACE_H 11 # define OPENSSL_TRACE_H 12 # pragma once 13 14 # include <stdarg.h> 15 16 # include <openssl/bio.h> 17 18 # ifdef __cplusplus 19 extern "C" { 20 # endif 21 22 /* 23 * TRACE CATEGORIES 24 */ 25 26 /* 27 * The trace messages of the OpenSSL libraries are organized into different 28 * categories. For every trace category, the application can register a separate 29 * tracer callback. When a callback is registered, a so called trace channel is 30 * created for this category. This channel consists essentially of an internal 31 * BIO which sends all trace output it receives to the registered application 32 * callback. 33 * 34 * The ALL category can be used as a fallback category to register a single 35 * channel which receives the output from all categories. However, if the 36 * application intends to print the trace channel name in the line prefix, 37 * it is better to register channels for all categories separately. 38 * (This is how the openssl application does it.) 39 */ 40 # define OSSL_TRACE_CATEGORY_ALL 0 /* The fallback */ 41 # define OSSL_TRACE_CATEGORY_TRACE 1 42 # define OSSL_TRACE_CATEGORY_INIT 2 43 # define OSSL_TRACE_CATEGORY_TLS 3 44 # define OSSL_TRACE_CATEGORY_TLS_CIPHER 4 45 # define OSSL_TRACE_CATEGORY_CONF 5 46 # define OSSL_TRACE_CATEGORY_ENGINE_TABLE 6 47 # define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 7 48 # define OSSL_TRACE_CATEGORY_PKCS5V2 8 49 # define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN 9 50 # define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT 10 51 # define OSSL_TRACE_CATEGORY_X509V3_POLICY 11 52 # define OSSL_TRACE_CATEGORY_BN_CTX 12 53 # define OSSL_TRACE_CATEGORY_CMP 13 54 # define OSSL_TRACE_CATEGORY_STORE 14 55 # define OSSL_TRACE_CATEGORY_DECODER 15 56 # define OSSL_TRACE_CATEGORY_ENCODER 16 57 # define OSSL_TRACE_CATEGORY_REF_COUNT 17 58 # define OSSL_TRACE_CATEGORY_HTTP 18 59 # define OSSL_TRACE_CATEGORY_PROVIDER 19 60 # define OSSL_TRACE_CATEGORY_QUERY 20 61 # define OSSL_TRACE_CATEGORY_NUM 21 62 /* KEEP THIS LIST IN SYNC with trace_categories[] in crypto/trace.c */ 63 64 /* Returns the trace category number for the given |name| */ 65 int OSSL_trace_get_category_num(const char *name); 66 67 /* Returns the trace category name for the given |num| */ 68 const char *OSSL_trace_get_category_name(int num); 69 70 /* 71 * TRACE CONSUMERS 72 */ 73 74 /* 75 * Enables tracing for the given |category| by providing a BIO sink 76 * as |channel|. If a null pointer is passed as |channel|, an existing 77 * trace channel is removed and tracing for the category is disabled. 78 * 79 * Returns 1 on success and 0 on failure 80 */ 81 int OSSL_trace_set_channel(int category, BIO* channel); 82 83 /* 84 * Attach a prefix and a suffix to the given |category|, to be printed at the 85 * beginning and at the end of each trace output group, i.e. when 86 * OSSL_trace_begin() and OSSL_trace_end() are called. 87 * If a null pointer is passed as argument, the existing prefix or suffix is 88 * removed. 89 * 90 * They return 1 on success and 0 on failure 91 */ 92 int OSSL_trace_set_prefix(int category, const char *prefix); 93 int OSSL_trace_set_suffix(int category, const char *suffix); 94 95 /* 96 * OSSL_trace_cb is the type tracing callback provided by the application. 97 * It MUST return the number of bytes written, or 0 on error (in other words, 98 * it can never write zero bytes). 99 * 100 * The |buffer| will always contain text, which may consist of several lines. 101 * The |data| argument points to whatever data was provided by the application 102 * when registering the tracer function. 103 * 104 * The |category| number is given, as well as a |cmd| number, described below. 105 */ 106 typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count, 107 int category, int cmd, void *data); 108 /* 109 * Possible |cmd| numbers. 110 */ 111 # define OSSL_TRACE_CTRL_BEGIN 0 112 # define OSSL_TRACE_CTRL_WRITE 1 113 # define OSSL_TRACE_CTRL_END 2 114 115 /* 116 * Enables tracing for the given |category| by creating an internal 117 * trace channel which sends the output to the given |callback|. 118 * If a null pointer is passed as callback, an existing trace channel 119 * is removed and tracing for the category is disabled. 120 * 121 * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually 122 * exclusive. 123 * 124 * Returns 1 on success and 0 on failure 125 */ 126 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data); 127 128 /* 129 * TRACE PRODUCERS 130 */ 131 132 /* 133 * Returns 1 if tracing for the specified category is enabled, otherwise 0 134 */ 135 int OSSL_trace_enabled(int category); 136 137 /* 138 * Wrap a group of tracing output calls. OSSL_trace_begin() locks tracing and 139 * returns the trace channel associated with the given category, or NULL if no 140 * channel is associated with the category. OSSL_trace_end() unlocks tracing. 141 * 142 * Usage: 143 * 144 * BIO *out; 145 * if ((out = OSSL_trace_begin(category)) != NULL) { 146 * ... 147 * BIO_fprintf(out, ...); 148 * ... 149 * OSSL_trace_end(category, out); 150 * } 151 * 152 * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below. 153 */ 154 BIO *OSSL_trace_begin(int category); 155 void OSSL_trace_end(int category, BIO *channel); 156 157 /* 158 * OSSL_TRACE* Convenience Macros 159 */ 160 161 /* 162 * When the tracing feature is disabled, these macros are defined to 163 * produce dead code, which a good compiler should eliminate. 164 */ 165 166 /* 167 * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group 168 * 169 * These two macros can be used to create a block which is executed only 170 * if the corresponding trace category is enabled. Inside this block, a 171 * local variable named |trc_out| is defined, which points to the channel 172 * associated with the given trace category. 173 * 174 * Usage: (using 'TLS' as an example category) 175 * 176 * OSSL_TRACE_BEGIN(TLS) { 177 * 178 * BIO_fprintf(trc_out, ... ); 179 * 180 * } OSSL_TRACE_END(TLS); 181 * 182 * 183 * This expands to the following code 184 * 185 * do { 186 * BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS); 187 * if (trc_out != NULL) { 188 * ... 189 * BIO_fprintf(trc_out, ...); 190 * } 191 * OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out); 192 * } while (0); 193 * 194 * The use of the inner '{...}' group and the trailing ';' is enforced 195 * by the definition of the macros in order to make the code look as much 196 * like C code as possible. 197 * 198 * Before returning from inside the trace block, it is necessary to 199 * call OSSL_TRACE_CANCEL(category). 200 */ 201 202 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE 203 204 # define OSSL_TRACE_BEGIN(category) \ 205 do { \ 206 BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \ 207 \ 208 if (trc_out != NULL) 209 210 # define OSSL_TRACE_END(category) \ 211 OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \ 212 } while (0) 213 214 # define OSSL_TRACE_CANCEL(category) \ 215 OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \ 216 217 # else 218 219 # define OSSL_TRACE_BEGIN(category) \ 220 do { \ 221 BIO *trc_out = NULL; \ 222 if (0) 223 224 # define OSSL_TRACE_END(category) \ 225 } while(0) 226 227 # define OSSL_TRACE_CANCEL(category) \ 228 ((void)0) 229 230 # endif 231 232 /* 233 * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category| 234 * 235 * Usage: 236 * 237 * if (OSSL_TRACE_ENABLED(TLS)) { 238 * ... 239 * } 240 */ 241 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE 242 243 # define OSSL_TRACE_ENABLED(category) \ 244 OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category) 245 246 # else 247 248 # define OSSL_TRACE_ENABLED(category) (0) 249 250 # endif 251 252 /* 253 * OSSL_TRACE*() - OneShot Trace Macros 254 * 255 * These macros are intended to produce a simple printf-style trace output. 256 * Unfortunately, C90 macros don't support variable arguments, so the 257 * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern: 258 * 259 * OSSL_TRACEV(category, (trc_out, "format string", ...args...)); 260 * 261 * Where 'channel' is the literal symbol of this name, not a variable. 262 * For that reason, it is currently not intended to be used directly, 263 * but only as helper macro for the other oneshot trace macros 264 * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ... 265 * 266 * Usage: 267 * 268 * OSSL_TRACE(INIT, "Hello world!\n"); 269 * OSSL_TRACE1(TLS, "The answer is %d\n", 42); 270 * OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n", 271 * 42, "What do you get when you multiply six by nine?"); 272 */ 273 274 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE 275 276 # define OSSL_TRACEV(category, args) \ 277 OSSL_TRACE_BEGIN(category) \ 278 BIO_printf args; \ 279 OSSL_TRACE_END(category) 280 281 # else 282 283 # define OSSL_TRACEV(category, args) ((void)0) 284 285 # endif 286 287 # define OSSL_TRACE(category, text) \ 288 OSSL_TRACEV(category, (trc_out, "%s", text)) 289 290 # define OSSL_TRACE1(category, format, arg1) \ 291 OSSL_TRACEV(category, (trc_out, format, arg1)) 292 # define OSSL_TRACE2(category, format, arg1, arg2) \ 293 OSSL_TRACEV(category, (trc_out, format, arg1, arg2)) 294 # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \ 295 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3)) 296 # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \ 297 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4)) 298 # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \ 299 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5)) 300 # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \ 301 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6)) 302 # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \ 303 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7)) 304 # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \ 305 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)) 306 # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \ 307 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)) 308 309 #define OSSL_TRACE_STRING_MAX 80 310 int OSSL_trace_string(BIO *out, int text, int full, 311 const unsigned char *data, size_t size); 312 #define OSSL_TRACE_STRING(category, text, full, data, len) \ 313 OSSL_TRACE_BEGIN(category) { \ 314 OSSL_trace_string(trc_out, text, full, data, len); \ 315 } OSSL_TRACE_END(category) 316 317 # ifdef __cplusplus 318 } 319 # endif 320 321 #endif 322