Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:51

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 /* Count of available categories. */
0060 # define OSSL_TRACE_CATEGORY_NUM                19
0061 /* KEEP THIS LIST IN SYNC with trace_categories[] in crypto/trace.c */
0062 
0063 /* Returns the trace category number for the given |name| */
0064 int OSSL_trace_get_category_num(const char *name);
0065 
0066 /* Returns the trace category name for the given |num| */
0067 const char *OSSL_trace_get_category_name(int num);
0068 
0069 /*
0070  * TRACE CONSUMERS
0071  */
0072 
0073 /*
0074  * Enables tracing for the given |category| by providing a BIO sink
0075  * as |channel|. If a null pointer is passed as |channel|, an existing
0076  * trace channel is removed and tracing for the category is disabled.
0077  *
0078  * Returns 1 on success and 0 on failure
0079  */
0080 int OSSL_trace_set_channel(int category, BIO* channel);
0081 
0082 /*
0083  * Attach a prefix and a suffix to the given |category|, to be printed at the
0084  * beginning and at the end of each trace output group, i.e. when
0085  * OSSL_trace_begin() and OSSL_trace_end() are called.
0086  * If a null pointer is passed as argument, the existing prefix or suffix is
0087  * removed.
0088  *
0089  * They return 1 on success and 0 on failure
0090  */
0091 int OSSL_trace_set_prefix(int category, const char *prefix);
0092 int OSSL_trace_set_suffix(int category, const char *suffix);
0093 
0094 /*
0095  * OSSL_trace_cb is the type tracing callback provided by the application.
0096  * It MUST return the number of bytes written, or 0 on error (in other words,
0097  * it can never write zero bytes).
0098  *
0099  * The |buffer| will always contain text, which may consist of several lines.
0100  * The |data| argument points to whatever data was provided by the application
0101  * when registering the tracer function.
0102  *
0103  * The |category| number is given, as well as a |cmd| number, described below.
0104  */
0105 typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
0106                                 int category, int cmd, void *data);
0107 /*
0108  * Possible |cmd| numbers.
0109  */
0110 # define OSSL_TRACE_CTRL_BEGIN  0
0111 # define OSSL_TRACE_CTRL_WRITE  1
0112 # define OSSL_TRACE_CTRL_END    2
0113 
0114 /*
0115  * Enables tracing for the given |category| by creating an internal
0116  * trace channel which sends the output to the given |callback|.
0117  * If a null pointer is passed as callback, an existing trace channel
0118  * is removed and tracing for the category is disabled.
0119  *
0120  * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
0121  *       exclusive.
0122  *
0123  * Returns 1 on success and 0 on failure
0124  */
0125 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
0126 
0127 /*
0128  * TRACE PRODUCERS
0129  */
0130 
0131 /*
0132  * Returns 1 if tracing for the specified category is enabled, otherwise 0
0133  */
0134 int OSSL_trace_enabled(int category);
0135 
0136 /*
0137  * Wrap a group of tracing output calls.  OSSL_trace_begin() locks tracing and
0138  * returns the trace channel associated with the given category, or NULL if no
0139  * channel is associated with the category.  OSSL_trace_end() unlocks tracing.
0140  *
0141  * Usage:
0142  *
0143  *    BIO *out;
0144  *    if ((out = OSSL_trace_begin(category)) != NULL) {
0145  *        ...
0146  *        BIO_fprintf(out, ...);
0147  *        ...
0148  *        OSSL_trace_end(category, out);
0149  *    }
0150  *
0151  * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
0152  */
0153 BIO *OSSL_trace_begin(int category);
0154 void OSSL_trace_end(int category, BIO *channel);
0155 
0156 /*
0157  * OSSL_TRACE* Convenience Macros
0158  */
0159 
0160 /*
0161  * When the tracing feature is disabled, these macros are defined to
0162  * produce dead code, which a good compiler should eliminate.
0163  */
0164 
0165 /*
0166  * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
0167  *
0168  * These two macros can be used to create a block which is executed only
0169  * if the corresponding trace category is enabled. Inside this block, a
0170  * local variable named |trc_out| is defined, which points to the channel
0171  * associated with the given trace category.
0172  *
0173  * Usage: (using 'TLS' as an example category)
0174  *
0175  *     OSSL_TRACE_BEGIN(TLS) {
0176  *
0177  *         BIO_fprintf(trc_out, ... );
0178  *
0179  *     } OSSL_TRACE_END(TLS);
0180  *
0181  *
0182  * This expands to the following code
0183  *
0184  *     do {
0185  *         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
0186  *         if (trc_out != NULL) {
0187  *             ...
0188  *             BIO_fprintf(trc_out, ...);
0189  *         }
0190  *         OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
0191  *     } while (0);
0192  *
0193  * The use of the inner '{...}' group and the trailing ';' is enforced
0194  * by the definition of the macros in order to make the code look as much
0195  * like C code as possible.
0196  *
0197  * Before returning from inside the trace block, it is necessary to
0198  * call OSSL_TRACE_CANCEL(category).
0199  */
0200 
0201 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
0202 
0203 #  define OSSL_TRACE_BEGIN(category) \
0204     do { \
0205         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
0206  \
0207         if (trc_out != NULL)
0208 
0209 #  define OSSL_TRACE_END(category) \
0210         OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
0211     } while (0)
0212 
0213 #  define OSSL_TRACE_CANCEL(category) \
0214         OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
0215 
0216 # else
0217 
0218 #  define OSSL_TRACE_BEGIN(category)           \
0219     do {                                        \
0220         BIO *trc_out = NULL;                    \
0221         if (0)
0222 
0223 #  define OSSL_TRACE_END(category)             \
0224     } while(0)
0225 
0226 #  define OSSL_TRACE_CANCEL(category)          \
0227     ((void)0)
0228 
0229 # endif
0230 
0231 /*
0232  * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
0233  *
0234  * Usage:
0235  *
0236  *     if (OSSL_TRACE_ENABLED(TLS)) {
0237  *         ...
0238  *     }
0239  */
0240 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
0241 
0242 #  define OSSL_TRACE_ENABLED(category) \
0243     OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
0244 
0245 # else
0246 
0247 #  define OSSL_TRACE_ENABLED(category) (0)
0248 
0249 # endif
0250 
0251 /*
0252  * OSSL_TRACE*() - OneShot Trace Macros
0253  *
0254  * These macros are intended to produce a simple printf-style trace output.
0255  * Unfortunately, C90 macros don't support variable arguments, so the
0256  * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
0257  *
0258  *    OSSL_TRACEV(category, (trc_out, "format string", ...args...));
0259  *
0260  * Where 'channel' is the literal symbol of this name, not a variable.
0261  * For that reason, it is currently not intended to be used directly,
0262  * but only as helper macro for the other oneshot trace macros
0263  * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
0264  *
0265  * Usage:
0266  *
0267  *    OSSL_TRACE(INIT, "Hello world!\n");
0268  *    OSSL_TRACE1(TLS, "The answer is %d\n", 42);
0269  *    OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
0270  *                42, "What do you get when you multiply six by nine?");
0271  */
0272 
0273 # if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE
0274 
0275 #  define OSSL_TRACEV(category, args) \
0276     OSSL_TRACE_BEGIN(category) \
0277         BIO_printf args; \
0278     OSSL_TRACE_END(category)
0279 
0280 # else
0281 
0282 #  define OSSL_TRACEV(category, args) ((void)0)
0283 
0284 # endif
0285 
0286 # define OSSL_TRACE(category, text) \
0287     OSSL_TRACEV(category, (trc_out, "%s", text))
0288 
0289 # define OSSL_TRACE1(category, format, arg1) \
0290     OSSL_TRACEV(category, (trc_out, format, arg1))
0291 # define OSSL_TRACE2(category, format, arg1, arg2) \
0292     OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
0293 # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
0294     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
0295 # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
0296     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
0297 # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
0298     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
0299 # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
0300     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
0301 # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
0302     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
0303 # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
0304     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
0305 # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
0306     OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
0307 
0308 #define OSSL_TRACE_STRING_MAX 80
0309 int OSSL_trace_string(BIO *out, int text, int full,
0310                       const unsigned char *data, size_t size);
0311 #define OSSL_TRACE_STRING(category, text, full, data, len) \
0312     OSSL_TRACE_BEGIN(category) { \
0313         OSSL_trace_string(trc_out, text, full, data, len);  \
0314     } OSSL_TRACE_END(category)
0315 
0316 # ifdef  __cplusplus
0317 }
0318 # endif
0319 
0320 #endif