Ignore:
Timestamp:
Feb 7, 2019, 8:36:33 AM (5 years ago)
Author:
coas-nagasima
Message:

wolfsslを3.15.7にバージョンアップ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/logging.c

    r337 r372  
    3131#include <wolfssl/wolfcrypt/logging.h>
    3232#include <wolfssl/wolfcrypt/error-crypt.h>
    33 
    34 
    35 #ifdef __cplusplus
    36     extern "C" {
    37 #endif
    38     WOLFSSL_API int  wolfSSL_Debugging_ON(void);
    39     WOLFSSL_API void wolfSSL_Debugging_OFF(void);
    40 #ifdef __cplusplus
    41     }
     33#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
     34/* avoid adding WANT_READ and WANT_WRITE to error queue */
     35#include <wolfssl/error-ssl.h>
    4236#endif
    4337
     
    5852};
    5953volatile struct wc_error_queue* wc_errors;
     54static struct wc_error_queue* wc_current_node;
    6055static struct wc_error_queue* wc_last_node;
    6156/* pointer to last node in queue to make insertion O(1) */
    6257#endif
    6358
    64 
    65 
    66 #if defined(DEBUG_WOLFSSL)
     59#ifdef WOLFSSL_FUNC_TIME
     60/* WARNING: This code is only to be used for debugging performance.
     61 *          The code is not thread-safe.
     62 *          Do not use WOLFSSL_FUNC_TIME in production code.
     63 */
     64static double wc_func_start[WC_FUNC_COUNT];
     65static double wc_func_time[WC_FUNC_COUNT] = { 0, };
     66static const char* wc_func_name[WC_FUNC_COUNT] = {
     67    "SendHelloRequest",
     68    "DoHelloRequest",
     69    "SendClientHello",
     70    "DoClientHello",
     71    "SendServerHello",
     72    "DoServerHello",
     73    "SendEncryptedExtensions",
     74    "DoEncryptedExtensions",
     75    "SendCertificateRequest",
     76    "DoCertificateRequest",
     77    "SendCertificate",
     78    "DoCertificate",
     79    "SendCertificateVerify",
     80    "DoCertificateVerify",
     81    "SendFinished",
     82    "DoFinished",
     83    "SendKeyUpdate",
     84    "DoKeyUpdate",
     85    "SendEarlyData",
     86    "DoEarlyData",
     87    "SendNewSessionTicket",
     88    "DoNewSessionTicket",
     89    "SendServerHelloDone",
     90    "DoServerHelloDone",
     91    "SendTicket",
     92    "DoTicket",
     93    "SendClientKeyExchange",
     94    "DoClientKeyExchange",
     95    "SendCertificateStatus",
     96    "DoCertificateStatus",
     97    "SendServerKeyExchange",
     98    "DoServerKeyExchange",
     99    "SendEarlyData",
     100    "DoEarlyData",
     101};
     102
     103#include <sys/time.h>
     104
     105/* WARNING: This function is not portable. */
     106static WC_INLINE double current_time(int reset)
     107{
     108    struct timeval tv;
     109    gettimeofday(&tv, 0);
     110    (void)reset;
     111
     112    return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
     113}
     114#endif /* WOLFSSL_FUNC_TIME */
     115
     116#ifdef DEBUG_WOLFSSL
    67117
    68118/* Set these to default values initially. */
     
    70120static int loggingEnabled = 0;
    71121
     122#if defined(WOLFSSL_APACHE_MYNEWT)
     123#include "log/log.h"
     124static struct log mynewt_log;
     125#endif /* WOLFSSL_APACHE_MYNEWT */
     126
    72127#endif /* DEBUG_WOLFSSL */
    73128
    74129
     130/* allow this to be set to NULL, so logs can be redirected to default output */
    75131int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f)
    76132{
    77133#ifdef DEBUG_WOLFSSL
    78     int res = 0;
    79 
    80     if (f)
    81134        log_function = f;
    82     else
    83         res = BAD_FUNC_ARG;
    84 
    85     return res;
     135    return 0;
    86136#else
    87137    (void)f;
     
    95145#ifdef DEBUG_WOLFSSL
    96146    loggingEnabled = 1;
     147#if defined(WOLFSSL_APACHE_MYNEWT)
     148    log_register("wolfcrypt", &mynewt_log, &log_console_handler, NULL, LOG_SYSLEVEL);
     149#endif /* WOLFSSL_APACHE_MYNEWT */
    97150    return 0;
    98151#else
     
    109162}
    110163
     164#ifdef WOLFSSL_FUNC_TIME
     165/* WARNING: This code is only to be used for debugging performance.
     166 *          The code is not thread-safe.
     167 *          Do not use WOLFSSL_FUNC_TIME in production code.
     168 */
     169void WOLFSSL_START(int funcNum)
     170{
     171    double now = current_time(0) * 1000.0;
     172#ifdef WOLFSSL_FUNC_TIME_LOG
     173    fprintf(stderr, "%17.3f: START - %s\n", now, wc_func_name[funcNum]);
     174#endif
     175    wc_func_start[funcNum] = now;
     176}
     177
     178void WOLFSSL_END(int funcNum)
     179{
     180    double now = current_time(0) * 1000.0;
     181    wc_func_time[funcNum] += now - wc_func_start[funcNum];
     182#ifdef WOLFSSL_FUNC_TIME_LOG
     183    fprintf(stderr, "%17.3f: END   - %s\n", now, wc_func_name[funcNum]);
     184#endif
     185}
     186
     187void WOLFSSL_TIME(int count)
     188{
     189    int i;
     190    double avg, total = 0;
     191
     192    for (i = 0; i < WC_FUNC_COUNT; i++) {
     193        if (wc_func_time[i] > 0) {
     194            avg = wc_func_time[i] / count;
     195            fprintf(stderr, "%8.3f ms: %s\n", avg, wc_func_name[i]);
     196            total += avg;
     197        }
     198    }
     199    fprintf(stderr, "%8.3f ms\n", total);
     200}
     201#endif
    111202
    112203#ifdef DEBUG_WOLFSSL
     
    122213    int sprintf(char* buf, const char *fmt, ...);
    123214#elif defined(MICRIUM)
     215    #if (BSP_SER_COMM_EN  == DEF_ENABLED)
    124216    #include <bsp_ser.h>
     217    #endif
     218#elif defined(WOLFSSL_USER_LOG)
     219    /* user includes their own headers */
     220#elif defined(WOLFSSL_ESPIDF)
     221    #include "esp_types.h"
     222    #include "esp_log.h"
    125223#else
    126224    #include <stdio.h>   /* for default printf stuff */
     
    136234        log_function(logLevel, logMessage);
    137235    else {
    138         if (loggingEnabled) {
    139 #if defined(THREADX) && !defined(THREADX_NO_DC_PRINTF)
     236#if defined(WOLFSSL_USER_LOG)
     237        WOLFSSL_USER_LOG(logMessage);
     238#elif defined(WOLFSSL_LOG_PRINTF)
     239        printf("%s\n", logMessage);
     240
     241#elif defined(THREADX) && !defined(THREADX_NO_DC_PRINTF)
    140242            dc_log_printf("%s\n", logMessage);
    141243#elif defined(MICRIUM)
     
    145247            printf("%s\n", logMessage);
    146248            fflush(stdout) ;
    147 #elif defined(WOLFSSL_LOG_PRINTF)
    148             printf("%s\n", logMessage);
    149249#elif defined(WOLFSSL_UTASKER)
    150250            fnDebugMsg((char*)logMessage);
     
    152252#elif defined(MQX_USE_IO_OLD)
    153253            fprintf(_mqxio_stderr, "%s\n", logMessage);
     254
     255#elif defined(WOLFSSL_APACHE_MYNEWT)
     256        LOG_DEBUG(&mynewt_log, LOG_MODULE_DEFAULT, "%s\n", logMessage);
     257#elif defined(WOLFSSL_ESPIDF)
     258        extern char* TAG;
     259        ESP_LOGI(TAG, "%s", logMessage);
    154260#else
    155261            fprintf(stderr, "%s\n", logMessage);
     
    157263        }
    158264    }
    159 }
    160 
    161 
     265
     266#ifndef WOLFSSL_DEBUG_ERRORS_ONLY
    162267void WOLFSSL_MSG(const char* msg)
    163268{
     
    208313{
    209314    if (loggingEnabled) {
    210         char buffer[80];
    211         sprintf(buffer, "wolfSSL Entering %s", msg);
     315        char buffer[WOLFSSL_MAX_ERROR_SZ];
     316        XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg);
    212317        wolfssl_log(ENTER_LOG , buffer);
    213318    }
     
    218323{
    219324    if (loggingEnabled) {
    220         char buffer[80];
    221         sprintf(buffer, "wolfSSL Leaving %s, return %d", msg, ret);
     325        char buffer[WOLFSSL_MAX_ERROR_SZ];
     326        XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d",
     327                msg, ret);
    222328        wolfssl_log(LEAVE_LOG , buffer);
    223329    }
    224330}
     331#endif /* !WOLFSSL_DEBUG_ERRORS_ONLY */
    225332#endif  /* DEBUG_WOLFSSL */
    226333
     
    230337 * name where WOLFSSL_ERROR is called at.
    231338 */
    232 #if (defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)) || defined(WOLFSSL_HAPROXY)
    233     #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE))
     339#if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || \
     340    defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
     341
     342#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
    234343void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line,
    235344            const char* file, void* usrCtx)
     
    238347    #endif
    239348{
    240     #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_NGINX)
    241     if (loggingEnabled && error != WC_PENDING_E)
     349#ifdef WOLFSSL_ASYNC_CRYPT
     350    if (error != WC_PENDING_E)
    242351    #endif
    243352    {
    244         char buffer[80];
     353        char buffer[WOLFSSL_MAX_ERROR_SZ];
     354
    245355        #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
    246356            (void)usrCtx; /* a user ctx for future flexibility */
     
    249359            if (wc_LockMutex(&debug_mutex) != 0) {
    250360                WOLFSSL_MSG("Lock debug mutex failed");
    251                 sprintf(buffer, "wolfSSL error occurred, error = %d", error);
     361            XSNPRINTF(buffer, sizeof(buffer),
     362                    "wolfSSL error occurred, error = %d", error);
    252363            }
    253364            else {
    254                 if (error < 0) error = error - (2*error); /*get absolute value*/
    255                 sprintf(buffer, "wolfSSL error occurred, error = %d line:%d file:%s",
     365            #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
     366            /* If running in compatibility mode do not add want read and
     367               want right to error queue */
     368            if (error != WANT_READ && error != WANT_WRITE) {
     369            #endif
     370            if (error < 0)
     371                error = error - (2 * error); /* get absolute value */
     372            XSNPRINTF(buffer, sizeof(buffer),
     373                    "wolfSSL error occurred, error = %d line:%d file:%s",
    256374                    error, line, file);
    257375                if (wc_AddErrorNode(error, line, buffer, (char*)file) != 0) {
     
    260378                     * to unlock mutex and log what buffer was created. */
    261379                }
     380            #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
     381            }
     382            else {
     383                XSNPRINTF(buffer, sizeof(buffer),
     384                    "wolfSSL error occurred, error = %d", error);
     385
     386            }
     387            #endif
    262388
    263389                wc_UnLockMutex(&debug_mutex);
    264390            }
    265391        #else
    266             sprintf(buffer, "wolfSSL error occurred, error = %d", error);
     392        XSNPRINTF(buffer, sizeof(buffer),
     393                "wolfSSL error occurred, error = %d", error);
    267394        #endif
     395
    268396        #ifdef DEBUG_WOLFSSL
     397        if (loggingEnabled)
    269398        wolfssl_log(ERROR_LOG , buffer);
    270399        #endif
    271400    }
     401}
     402
     403void WOLFSSL_ERROR_MSG(const char* msg)
     404{
     405#ifdef DEBUG_WOLFSSL
     406    if (loggingEnabled)
     407        wolfssl_log(ERROR_LOG , msg);
     408#else
     409    (void)msg;
     410#endif
    272411}
    273412
     
    283422    }
    284423    wc_errors          = NULL;
     424    wc_current_node    = NULL;
    285425    wc_last_node       = NULL;
    286426
     
    304444
    305445
    306 #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || \
    307     defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_MYSQL_COMPATIBLE)
    308446/* peek at an error node
    309447 *
     
    367505
    368506
     507/* Pulls the current node from error queue and increments current state.
     508 * Note: this does not delete nodes because input arguments are pointing to
     509 *       node buffers.
     510 *
     511 * file   pointer to file that error was in. Can be NULL to return no file.
     512 * reason error string giving reason for error. Can be NULL to return no reason.
     513 * line   return line number of where error happened.
     514 *
     515 * returns the error value on success and BAD_MUTEX_E or BAD_STATE_E on failure
     516 */
     517int wc_PullErrorNode(const char **file, const char **reason, int *line)
     518{
     519    struct wc_error_queue* err;
     520    int value;
     521
     522    if (wc_LockMutex(&debug_mutex) != 0) {
     523        WOLFSSL_MSG("Lock debug mutex failed");
     524        return BAD_MUTEX_E;
     525    }
     526
     527    err = wc_current_node;
     528    if (err == NULL) {
     529        WOLFSSL_MSG("No Errors in queue");
     530        wc_UnLockMutex(&debug_mutex);
     531        return BAD_STATE_E;
     532    }
     533
     534    if (file != NULL) {
     535        *file = err->file;
     536    }
     537
     538    if (reason != NULL) {
     539        *reason = err->error;
     540    }
     541
     542    if (line != NULL) {
     543        *line = err->line;
     544    }
     545
     546    value = err->value;
     547    wc_current_node = err->next;
     548    wc_UnLockMutex(&debug_mutex);
     549
     550    return value;
     551}
     552
     553
    369554/* create new error node and add it to the queue
    370555 * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal
     
    416601                /* check for unexpected case before over writing wc_errors */
    417602                WOLFSSL_MSG("ERROR in adding new node to logging queue!!\n");
     603                /* In the event both wc_last_node and wc_errors are NULL, err
     604                 * goes unassigned to external wc_errors, wc_last_node. Free
     605                 * err in this instance since wc_ClearErrorNodes will not
     606                 */
     607                XFREE(err, wc_error_heap, DYNAMIC_TYPE_LOG);
    418608            }
    419609            else {
    420610                wc_errors    = err;
    421611                wc_last_node = err;
     612                wc_current_node = err;
    422613            }
    423614        }
     
    426617            err->prev = wc_last_node;
    427618            wc_last_node = err;
     619
     620            /* check the case where have read to the end of the queue and the
     621             * current node to read needs updated */
     622            if (wc_current_node == NULL) {
     623                wc_current_node = err;
     624            }
    428625        }
    429626    }
     
    465662}
    466663
    467 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX */
    468664
    469665/* Clears out the list of error nodes.
     
    471667void wc_ClearErrorNodes(void)
    472668{
     669#if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || \
     670    defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
     671
    473672    if (wc_LockMutex(&debug_mutex) != 0) {
    474673        WOLFSSL_MSG("Lock debug mutex failed");
     
    491690    wc_errors    = NULL;
    492691    wc_last_node = NULL;
     692    wc_current_node = NULL;
    493693    wc_UnLockMutex(&debug_mutex);
     694#endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX */
    494695}
    495696
     
    505706}
    506707
     708
     709/* frees all nodes in the queue
     710 *
     711 * id  this is the thread id
     712 */
     713int wc_ERR_remove_state(void)
     714{
     715    struct wc_error_queue* current;
     716    struct wc_error_queue* next;
     717
     718    if (wc_LockMutex(&debug_mutex) != 0) {
     719        WOLFSSL_MSG("Lock debug mutex failed");
     720        return BAD_MUTEX_E;
     721    }
     722
     723    /* free all nodes from error queue */
     724    current = (struct wc_error_queue*)wc_errors;
     725    while (current != NULL) {
     726        next = current->next;
     727        XFREE(current, current->heap, DYNAMIC_TYPE_LOG);
     728        current = next;
     729    }
     730
     731    wc_errors          = NULL;
     732    wc_last_node       = NULL;
     733
     734    wc_UnLockMutex(&debug_mutex);
     735
     736    return 0;
     737}
     738
     739
    507740#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
    508741/* empties out the error queue into the file */
    509 void wc_ERR_print_errors_fp(FILE* fp)
     742void wc_ERR_print_errors_fp(XFILE fp)
    510743{
    511744    WOLFSSL_ENTER("wc_ERR_print_errors_fp");
    512745
    513     if (wc_LockMutex(&debug_mutex) != 0) {
     746        if (wc_LockMutex(&debug_mutex) != 0)
     747        {
    514748        WOLFSSL_MSG("Lock debug mutex failed");
    515749    }
    516     else {
     750        else
     751        {
    517752        /* free all nodes from error queue and print them to file */
    518753        {
     
    521756
    522757            current = (struct wc_error_queue*)wc_errors;
    523             while (current != NULL) {
     758                while (current != NULL)
     759                {
    524760                next = current->next;
    525761                fprintf(fp, "%s\n", current->error);
Note: See TracChangeset for help on using the changeset viewer.