Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-09 10:20:13

0001 /*
0002  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
0003  *
0004  * Licensed under the Apache License 2.0 (the "License").  You may not use
0005  * this file except in compliance with the License.  You can obtain a copy
0006  * in the file LICENSE in the source distribution or at
0007  * https://www.openssl.org/source/license.html
0008  */
0009 
0010 #ifndef OPENSSL_TRACE_H
0011 # define OPENSSL_TRACE_H
0012 # pragma once
0013 
0014 # include <stdarg.h>
0015 
0016 # include <openssl/bio.h>
0017 
0018 # ifdef  __cplusplus
0019 extern "C" {
0020 # endif
0021 
0022 /*
0023  * TRACE CATEGORIES
0024  */
0025 
0026 /*
0027  * The trace messages of the OpenSSL libraries are organized into different
0028  * categories. For every trace category, the application can register a separate
0029  * tracer callback. When a callback is registered, a so called trace channel is
0030  * created for this category. This channel consists essentially of an internal
0031  * BIO which sends all trace output it receives to the registered application
0032  * callback.
0033  *
0034  * The ALL category can be used as a fallback category to register a single
0035  * channel which receives the output from all categories. However, if the
0036  * application intends to print the trace channel name in the line prefix,
0037  * it is better to register channels for all categories separately.
0038  * (This is how the openssl application does it.)
0039  */
0040 # define OSSL_TRACE_CATEGORY_ALL                 0 /* The fallback */
0041 # define OSSL_TRACE_CATEGORY_TRACE               1
0042 # define OSSL_TRACE_CATEGORY_INIT                2
0043 # define OSSL_TRACE_CATEGORY_TLS                 3
0044 # define OSSL_TRACE_CATEGORY_TLS_CIPHER          4
0045 # define OSSL_TRACE_CATEGORY_CONF                5
0046 # define OSSL_TRACE_CATEGORY_ENGINE_TABLE        6
0047 # define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT    7
0048 # define OSSL_TRACE_CATEGORY_PKCS5V2             8
0049 # define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN       9
0050 # define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT     10
0051 # define OSSL_TRACE_CATEGORY_X509V3_POLICY      11
0052 # define OSSL_TRACE_CATEGORY_BN_CTX             12
0053 # define OSSL_TRACE_CATEGORY_CMP                13
0054 # define OSSL_TRACE_CATEGORY_STORE              14
0055 # define OSSL_TRACE_CATEGORY_DECODER            15
0056 # define OSSL_TRACE_CATEGORY_ENCODER            16
0057 # define OSSL_TRACE_CATEGORY_REF_COUNT          17
0058 # define OSSL_TRACE_CATEGORY_HTTP               18
0059 # define OSSL_TRACE_CATEGORY_PROVIDER           19
0060 # define OSSL_TRACE_CATEGORY_QUERY              20
0061 # define OSSL_TRACE_CATEGORY_NUM                21
0062 /* KEEP THIS LIST IN SYNC with trace_categories[] in crypto/trace.c */
0063 
0064 /* Returns the trace category number for the given |name| */
0065 int OSSL_trace_get_category_num(const char *name);
0066 
0067 /* Returns the trace category name for the given |num| */
0068 const char *OSSL_trace_get_category_name(int num);
0069 
0070 /*
0071  * TRACE CONSUMERS
0072  */
0073 
0074 /*
0075  * Enables tracing for the given |category| by providing a BIO sink
0076  * as |channel|. If a null pointer is passed as |channel|, an existing
0077  * trace channel is removed and tracing for the category is disabled.
0078  *
0079  * Returns 1 on success and 0 on failure
0080  */
0081 int OSSL_trace_set_channel(int category, BIO* channel);
0082 
0083 /*
0084  * Attach a prefix and a suffix to the given |category|, to be printed at the
0085  * beginning and at the end of each trace output group, i.e. when
0086  * OSSL_trace_begin() and OSSL_trace_end() are called.
0087  * If a null pointer is passed as argument, the existing prefix or suffix is
0088  * removed.
0089  *
0090  * They return 1 on success and 0 on failure
0091  */
0092 int OSSL_trace_set_prefix(int category, const char *prefix);
0093 int OSSL_trace_set_suffix(int category, const char *suffix);
0094 
0095 /*
0096  * OSSL_trace_cb is the type tracing callback provided by the application.
0097  * It MUST return the number of bytes written, or 0 on error (in other words,
0098  * it can never write zero bytes).
0099  *
0100  * The |buffer| will always contain text, which may consist of several lines.
0101  * The |data| argument points to whatever data was provided by the application
0102  * when registering the tracer function.
0103  *
0104  * The |category| number is given, as well as a |cmd| number, described below.
0105  */
0106 typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
0107                                 int category, int cmd, void *data);
0108 /*
0109  * Possible |cmd| numbers.
0110  */
0111 # define OSSL_TRACE_CTRL_BEGIN  0
0112 # define OSSL_TRACE_CTRL_WRITE  1
0113 # define OSSL_TRACE_CTRL_END    2
0114 
0115 /*
0116  * Enables tracing for the given |category| by creating an internal
0117  * trace channel which sends the output to the given |callback|.
0118  * If a null pointer is passed as callback, an existing trace channel
0119  * is removed and tracing for the category is disabled.
0120  *
0121  * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
0122  *       exclusive.
0123  *
0124  * Returns 1 on success and 0 on failure
0125  */
0126 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
0127 
0128 /*
0129  * TRACE PRODUCERS
0130  */
0131 
0132 /*
0133  * Returns 1 if tracing for the specified category is enabled, otherwise 0
0134  */
0135 int OSSL_trace_enabled(int category);
0136 
0137 /*
0138  * Wrap a group of tracing output calls.  OSSL_trace_begin() locks tracing and
0139  * returns the trace channel associated with the given category, or NULL if no
0140  * channel is associated with the category.  OSSL_trace_end() unlocks tracing.
0141  *
0142  * Usage:
0143  *
0144  *    BIO *out;
0145  *    if ((out = OSSL_trace_begin(category)) != NULL) {
0146  *        ...
0147  *        BIO_fprintf(out, ...);
0148  *        ...
0149  *        OSSL_trace_end(category, out);
0150  *    }
0151  *
0152  * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
0153  */
0154 BIO *OSSL_trace_begin(int category);
0155 void OSSL_trace_end(int category, BIO *channel);
0156 
0157 /*
0158  * OSSL_TRACE* Convenience Macros
0159  */
0160 
0161 /*
0162  * When the tracing feature is disabled, these macros are defined to
0163  * produce dead code, which a good compiler should eliminate.
0164  */
0165 
0166 /*
0167  * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
0168  *
0169  * These two macros can be used to create a block which is executed only
0170  * if the corresponding trace category is enabled. Inside this block, a
0171  * local variable named |trc_out| is defined, which points to the channel
0172  * associated with the given trace category.
0173  *
0174  * Usage: (using 'TLS' as an example category)
0175  *
0176  *     OSSL_TRACE_BEGIN(TLS) {
0177  *
0178  *         BIO_fprintf(trc_out, ... );
0179  *
0180  *     } OSSL_TRACE_END(TLS);
0181  *
0182  *
0183  * This expands to the following code
0184  *
0185  *     do {
0186  *         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
0187  *         if (trc_out != NULL) {
0188  *             ...
0189  *             BIO_fprintf(trc_out, ...);
0190  *         }
0191  *         OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
0192  *     } while (0);
0193  *
0194  * The use of the inner '{...}' group and the trailing ';' is enforced
0195  * by the definition of the macros in order to make the code look as much
0196  * like C code as possible.
0197  *
0198  * Before returning from inside the trace block, it is necessary to
0199  * call OSSL_TRACE_CANCEL(category).
0200  */
0201 
0202 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
0203 
0204 #  define OSSL_TRACE_BEGIN(category) \
0205     do { \
0206         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
0207  \
0208         if (trc_out != NULL)
0209 
0210 #  define OSSL_TRACE_END(category) \
0211         OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
0212     } while (0)
0213 
0214 #  define OSSL_TRACE_CANCEL(category) \
0215         OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
0216 
0217 # else
0218 
0219 #  define OSSL_TRACE_BEGIN(category)           \
0220     do {                                        \
0221         BIO *trc_out = NULL;                    \
0222         if (0)
0223 
0224 #  define OSSL_TRACE_END(category)             \
0225     } while(0)
0226 
0227 #  define OSSL_TRACE_CANCEL(category)          \
0228     ((void)0)
0229 
0230 # endif
0231 
0232 /*
0233  * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
0234  *
0235  * Usage:
0236  *
0237  *     if (OSSL_TRACE_ENABLED(TLS)) {
0238  *         ...
0239  *     }
0240  */
0241 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
0242 
0243 #  define OSSL_TRACE_ENABLED(category) \
0244     OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
0245 
0246 # else
0247 
0248 #  define OSSL_TRACE_ENABLED(category) (0)
0249 
0250 # endif
0251 
0252 /*
0253  * OSSL_TRACE*() - OneShot Trace Macros
0254  *
0255  * These macros are intended to produce a simple printf-style trace output.
0256  * Unfortunately, C90 macros don't support variable arguments, so the
0257  * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
0258  *
0259  *    OSSL_TRACEV(category, (trc_out, "format string", ...args...));
0260  *
0261  * Where 'channel' is the literal symbol of this name, not a variable.
0262  * For that reason, it is currently not intended to be used directly,
0263  * but only as helper macro for the other oneshot trace macros
0264  * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
0265  *
0266  * Usage:
0267  *
0268  *    OSSL_TRACE(INIT, "Hello world!\n");
0269  *    OSSL_TRACE1(TLS, "The answer is %d\n", 42);
0270  *    OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
0271  *                42, "What do you get when you multiply six by nine?");
0272  */
0273 
0274 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
0275 
0276 #  define OSSL_TRACEV(category, args) \
0277     OSSL_TRACE_BEGIN(category) \
0278         BIO_printf args; \
0279     OSSL_TRACE_END(category)
0280 
0281 # else
0282 
0283 #  define OSSL_TRACEV(category, args) ((void)0)
0284 
0285 # endif
0286 
0287 # define OSSL_TRACE(category, text) \
0288     OSSL_TRACEV(category, (trc_out, "%s", text))
0289 
0290 # define OSSL_TRACE1(category, format, arg1) \
0291     OSSL_TRACEV(category, (trc_out, format, arg1))
0292 # define OSSL_TRACE2(category, format, arg1, arg2) \
0293     OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
0294 # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
0295     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
0296 # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
0297     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
0298 # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
0299     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
0300 # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
0301     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
0302 # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
0303     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
0304 # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
0305     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
0306 # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
0307     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
0308 
0309 #define OSSL_TRACE_STRING_MAX 80
0310 int OSSL_trace_string(BIO *out, int text, int full,
0311                       const unsigned char *data, size_t size);
0312 #define OSSL_TRACE_STRING(category, text, full, data, len) \
0313     OSSL_TRACE_BEGIN(category) { \
0314         OSSL_trace_string(trc_out, text, full, data, len);  \
0315     } OSSL_TRACE_END(category)
0316 
0317 # ifdef  __cplusplus
0318 }
0319 # endif
0320 
0321 #endif