/**
 * XML Security Library (http://www.aleksey.com/xmlsec).
 *
 * This is free software; see the Copyright file in the source distribution for precise wording.
 *
 * Copyright (C) 2002-2026 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
 */
/**
 * @brief Internal global header for OpenSSL used during compilation.
 */
#ifndef __XMLSEC_GLOBALS_H__
#define __XMLSEC_GLOBALS_H__

/**
 * Use autoconf defines if present.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <openssl/crypto.h>

#define IN_XMLSEC_CRYPTO
#define XMLSEC_PRIVATE

/* Include common error helper macros. */
#include "../errors_helpers.h"

/**
 * @brief Buffer size for OpenSSL error messages.
 * @details Macro. The buffer size for reporting OpenSSL errors.
 */
#define XMLSEC_OPENSSL_ERROR_BUFFER_SIZE                1024

/** AWS LC and OpenSSL have different types for error code type */
#ifdef OPENSSL_IS_AWSLC
typedef uint32_t xmlSecOpenSSLErrorType;
#else /* OPENSSL_IS_AWSLC */
typedef unsigned long xmlSecOpenSSLErrorType;
#endif /* ! OPENSSL_IS_AWSLC */


/**
 * @brief Macro. Reports OpenSSL crypto errors.
 * @details Macro. The XMLSec library macro for reporting OpenSSL crypro errors.
 * @param errorFunction the failed function name.
 * @param errorObject the error specific error object (e.g. transform, key data, etc).
 */
#define xmlSecOpenSSLError(errorFunction, errorObject)      \
    {                                                       \
        char _openssl_error_buf[XMLSEC_OPENSSL_ERROR_BUFFER_SIZE]; \
        xmlSecOpenSSLErrorType _openssl_error_code = ERR_peek_last_error(); \
        ERR_error_string_n(_openssl_error_code, _openssl_error_buf, sizeof(_openssl_error_buf)); \
        xmlSecError(XMLSEC_ERRORS_HERE,                     \
                    (const char*)(errorObject),             \
                    (errorFunction),                        \
                    XMLSEC_ERRORS_R_CRYPTO_FAILED,          \
                    "openssl error: %s",                    \
                    xmlSecErrorsSafeString(_openssl_error_buf) \
        );                                                  \
    }


/**
 * @brief Macro. Reports OpenSSL crypto errors.
 * @details Macro. The XMLSec library macro for reporting OpenSSL crypro errors.
 * @param errorFunction the failed function name.
 * @param errorObject the error specific error object (e.g. transform, key data, etc).
 * @param msg the extra message.
 * @param param the extra message param.
 */
#define xmlSecOpenSSLError2(errorFunction, errorObject, msg, param) \
        char _openssl_error_buf[XMLSEC_OPENSSL_ERROR_BUFFER_SIZE];  \
        xmlSecOpenSSLErrorType _openssl_error_code = ERR_peek_last_error();  \
        ERR_error_string_n(_openssl_error_code, _openssl_error_buf, sizeof(_openssl_error_buf)); \
        xmlSecError(XMLSEC_ERRORS_HERE,                     \
                    (const char*)(errorObject),             \
                    (errorFunction),                        \
                    XMLSEC_ERRORS_R_CRYPTO_FAILED,          \
                    msg "; openssl error: %s",              \
                    (param),                                \
                    xmlSecErrorsSafeString(_openssl_error_buf) \
        );                                                  \

/**
 * @brief Macro. Reports OpenSSL crypto errors.
 * @details Macro. The XMLSec library macro for reporting OpenSSL crypro errors.
 * @param errorFunction the failed function name.
 * @param errorObject the error specific error object (e.g. transform, key data, etc).
 * @param msg the extra message.
 * @param param1 the extra message param1.
 * @param param2 the extra message param2.
 */
#define xmlSecOpenSSLError3(errorFunction, errorObject, msg, param1, param2) \
        char _openssl_error_buf[XMLSEC_OPENSSL_ERROR_BUFFER_SIZE];  \
        xmlSecOpenSSLErrorType _openssl_error_code = ERR_peek_last_error();  \
        ERR_error_string_n(_openssl_error_code, _openssl_error_buf, sizeof(_openssl_error_buf)); \
        xmlSecError(XMLSEC_ERRORS_HERE,                     \
                    (const char*)(errorObject),             \
                    (errorFunction),                        \
                    XMLSEC_ERRORS_R_CRYPTO_FAILED,          \
                    msg "; openssl error: %s",              \
                    (param1),                               \
                    (param2),                               \
                    xmlSecErrorsSafeString(_openssl_error_buf) \
        );                                                  \

#endif /* ! __XMLSEC_GLOBALS_H__ */
