Home | History | Annotate | Download | only in err
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) cryptsoft.com).
      6  * The implementation was written so as to conform with Netscapes SSL.
      7  *
      8  * This library is free for commercial and non-commercial use as long as
      9  * the following conditions are aheared to.  The following conditions
     10  * apply to all code found in this distribution, be it the RC4, RSA,
     11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     12  * included with this distribution is covered by the same copyright terms
     13  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     14  *
     15  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  * the code are not to be removed.
     17  * If this package is used in a product, Eric Young should be given attribution
     18  * as the author of the parts of the library used.
     19  * This can be in the form of a textual message at program startup or
     20  * in documentation (online or textual) provided with the package.
     21  *
     22  * Redistribution and use in source and binary forms, with or without
     23  * modification, are permitted provided that the following conditions
     24  * are met:
     25  * 1. Redistributions of source code must retain the copyright
     26  *    notice, this list of conditions and the following disclaimer.
     27  * 2. Redistributions in binary form must reproduce the above copyright
     28  *    notice, this list of conditions and the following disclaimer in the
     29  *    documentation and/or other materials provided with the distribution.
     30  * 3. All advertising materials mentioning features or use of this software
     31  *    must display the following acknowledgement:
     32  *    "This product includes cryptographic software written by
     33  *     Eric Young (eay (at) cryptsoft.com)"
     34  *    The word 'cryptographic' can be left out if the rouines from the library
     35  *    being used are not cryptographic related :-).
     36  * 4. If you include any Windows specific code (or a derivative thereof) from
     37  *    the apps directory (application code) you must include an acknowledgement:
     38  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     39  *
     40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  * SUCH DAMAGE.
     51  *
     52  * The licence and distribution terms for any publically available version or
     53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     54  * copied and put under another distribution licence
     55  * [including the GNU Public Licence.]
     56  */
     57 /* ====================================================================
     58  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
     59  *
     60  * Redistribution and use in source and binary forms, with or without
     61  * modification, are permitted provided that the following conditions
     62  * are met:
     63  *
     64  * 1. Redistributions of source code must retain the above copyright
     65  *    notice, this list of conditions and the following disclaimer.
     66  *
     67  * 2. Redistributions in binary form must reproduce the above copyright
     68  *    notice, this list of conditions and the following disclaimer in
     69  *    the documentation and/or other materials provided with the
     70  *    distribution.
     71  *
     72  * 3. All advertising materials mentioning features or use of this
     73  *    software must display the following acknowledgment:
     74  *    "This product includes software developed by the OpenSSL Project
     75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
     76  *
     77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
     78  *    endorse or promote products derived from this software without
     79  *    prior written permission. For written permission, please contact
     80  *    openssl-core (at) openssl.org.
     81  *
     82  * 5. Products derived from this software may not be called "OpenSSL"
     83  *    nor may "OpenSSL" appear in their names without prior written
     84  *    permission of the OpenSSL Project.
     85  *
     86  * 6. Redistributions of any form whatsoever must retain the following
     87  *    acknowledgment:
     88  *    "This product includes software developed by the OpenSSL Project
     89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
     90  *
     91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
     92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
     95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    102  * OF THE POSSIBILITY OF SUCH DAMAGE.
    103  * ====================================================================
    104  *
    105  * This product includes cryptographic software written by Eric Young
    106  * (eay (at) cryptsoft.com).  This product includes software written by Tim
    107  * Hudson (tjh (at) cryptsoft.com). */
    108 
    109 #include <openssl/err.h>
    110 
    111 #include <assert.h>
    112 #include <errno.h>
    113 #include <inttypes.h>
    114 #include <stdarg.h>
    115 #include <stdio.h>
    116 
    117 #if defined(OPENSSL_WINDOWS)
    118 #include <Windows.h>
    119 #endif
    120 
    121 #include <openssl/lhash.h>
    122 #include <openssl/mem.h>
    123 #include <openssl/thread.h>
    124 
    125 
    126 /* err_fns contains a pointer to the current error implementation. */
    127 static const struct ERR_FNS_st *err_fns = NULL;
    128 extern const struct ERR_FNS_st openssl_err_default_impl;
    129 
    130 #define ERRFN(a) err_fns->a
    131 
    132 /* err_fns_check is an internal function that checks whether "err_fns" is set
    133  * and if not, sets it to the default. */
    134 static void err_fns_check(void) {
    135   /* In practice, this is not a race problem because loading the error strings
    136    * at init time will cause this pointer to be set before the process goes
    137    * multithreaded. */
    138   if (err_fns) {
    139     return;
    140   }
    141 
    142   CRYPTO_w_lock(CRYPTO_LOCK_ERR);
    143   if (!err_fns) {
    144     err_fns = &openssl_err_default_impl;
    145   }
    146   CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
    147 }
    148 
    149 /* err_clear_data frees the optional |data| member of the given error. */
    150 static void err_clear_data(struct err_error_st *error) {
    151   if (error->data != NULL && (error->flags & ERR_FLAG_MALLOCED) != 0) {
    152     OPENSSL_free(error->data);
    153   }
    154   error->data = NULL;
    155   error->flags &= ~ERR_FLAG_MALLOCED;
    156 }
    157 
    158 /* err_clear clears the given queued error. */
    159 static void err_clear(struct err_error_st *error) {
    160   err_clear_data(error);
    161   memset(error, 0, sizeof(struct err_error_st));
    162 }
    163 
    164 /* err_get_state gets the ERR_STATE object for the current thread. */
    165 static ERR_STATE *err_get_state(void) {
    166   err_fns_check();
    167   return ERRFN(get_state)();
    168 }
    169 
    170 static uint32_t get_error_values(int inc, int top, const char **file, int *line,
    171                                  char **data, int *flags) {
    172   unsigned i = 0;
    173   ERR_STATE *state;
    174   struct err_error_st *error;
    175   uint32_t ret;
    176 
    177   state = err_get_state();
    178 
    179   if (state->bottom == state->top) {
    180     return 0;
    181   }
    182 
    183   if (top) {
    184     /* last error */
    185     i = state->top;
    186   } else {
    187     i = (state->bottom + 1) % ERR_NUM_ERRORS;
    188   }
    189 
    190   error = &state->errors[i];
    191   ret = error->packed;
    192 
    193   if (file != NULL && line != NULL) {
    194     if (error->file == NULL) {
    195       *file = "NA";
    196       *line = 0;
    197     } else {
    198       *file = error->file;
    199       *line = error->line;
    200     }
    201   }
    202 
    203   if (data != NULL) {
    204     if (error->data == NULL) {
    205       *data = "";
    206       if (flags != NULL) {
    207         *flags = 0;
    208       }
    209     } else {
    210       *data = error->data;
    211       if (flags != NULL) {
    212         *flags = error->flags & ERR_FLAG_PUBLIC_MASK;
    213       }
    214       error->data = NULL;
    215       error->flags = 0;
    216     }
    217   }
    218 
    219   if (inc) {
    220     assert(!top);
    221     err_clear(error);
    222     state->bottom = i;
    223   }
    224 
    225   return ret;
    226 }
    227 
    228 uint32_t ERR_get_error(void) {
    229   return get_error_values(1, 0, NULL, NULL, NULL, NULL);
    230 }
    231 
    232 uint32_t ERR_get_error_line(const char **file, int *line) {
    233   return get_error_values(1, 0, file, line, NULL, NULL);
    234 }
    235 
    236 uint32_t ERR_get_error_line_data(const char **file, int *line,
    237                                  char **data, int *flags) {
    238   return get_error_values(1, 0, file, line, data, flags);
    239 }
    240 
    241 uint32_t ERR_peek_error(void) {
    242   return get_error_values(0, 0, NULL, NULL, NULL, NULL);
    243 }
    244 
    245 uint32_t ERR_peek_error_line(const char **file, int *line) {
    246   return get_error_values(0, 0, file, line, NULL, NULL);
    247 }
    248 
    249 uint32_t ERR_peek_error_line_data(const char **file, int *line,
    250                                   const char **data, int *flags) {
    251   return get_error_values(0, 0, file, line, (char **) data, flags);
    252 }
    253 
    254 uint32_t ERR_peek_last_error(void) {
    255   return get_error_values(0, 1, NULL, NULL, NULL, NULL);
    256 }
    257 
    258 uint32_t ERR_peek_last_error_line(const char **file, int *line) {
    259   return get_error_values(0, 1, file, line, NULL, NULL);
    260 }
    261 
    262 uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
    263                                        const char **data, int *flags) {
    264   return get_error_values(0, 1, file, line, (char **) data, flags);
    265 }
    266 
    267 void ERR_clear_error(void) {
    268   ERR_STATE *const state = err_get_state();
    269   unsigned i;
    270 
    271   for (i = 0; i < ERR_NUM_ERRORS; i++) {
    272     err_clear(&state->errors[i]);
    273   }
    274 
    275   state->top = state->bottom = 0;
    276 }
    277 
    278 void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
    279   CRYPTO_THREADID current;
    280   ERR_STATE *state;
    281   unsigned i;
    282 
    283   if (tid == NULL) {
    284     CRYPTO_THREADID_current(&current);
    285     tid = &current;
    286   }
    287 
    288   err_fns_check();
    289   state = ERRFN(release_state)(tid);
    290   if (state == NULL) {
    291     return;
    292   }
    293 
    294   for (i = 0; i < ERR_NUM_ERRORS; i++) {
    295     err_clear(&state->errors[i]);
    296   }
    297 
    298   OPENSSL_free(state);
    299 }
    300 
    301 int ERR_get_next_error_library(void) {
    302   err_fns_check();
    303   return ERRFN(get_next_library)();
    304 }
    305 
    306 void ERR_clear_system_error(void) {
    307   errno = 0;
    308 }
    309 
    310 char *ERR_error_string(uint32_t packed_error, char *ret) {
    311   static char buf[ERR_ERROR_STRING_BUF_LEN];
    312 
    313   if (ret == NULL) {
    314     /* TODO(fork): remove this. */
    315     ret = buf;
    316   }
    317 
    318 #if !defined(NDEBUG)
    319   /* This is aimed to help catch callers who don't provide
    320    * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
    321   memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
    322 #endif
    323 
    324   ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
    325 
    326   return ret;
    327 }
    328 
    329 void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
    330   char lib_buf[64], func_buf[64], reason_buf[64];
    331   const char *lib_str, *func_str, *reason_str;
    332   unsigned lib, func, reason;
    333 
    334   if (len == 0) {
    335     return;
    336   }
    337 
    338   lib = ERR_GET_LIB(packed_error);
    339   func = ERR_GET_FUNC(packed_error);
    340   reason = ERR_GET_REASON(packed_error);
    341 
    342   lib_str = ERR_lib_error_string(packed_error);
    343   func_str = ERR_func_error_string(packed_error);
    344   reason_str = ERR_reason_error_string(packed_error);
    345 
    346   if (lib_str == NULL) {
    347     BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
    348     lib_str = lib_buf;
    349   }
    350 
    351   if (func_str == NULL) {
    352     BIO_snprintf(func_buf, sizeof(func_buf), "func(%u)", func);
    353     func_str = func_buf;
    354   }
    355 
    356   if (reason_str == NULL) {
    357     BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
    358     reason_str = reason_buf;
    359   }
    360 
    361   BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:%s:%s",
    362                packed_error, lib_str, func_str, reason_str);
    363 
    364   if (strlen(buf) == len - 1) {
    365     /* output may be truncated; make sure we always have 5 colon-separated
    366      * fields, i.e. 4 colons. */
    367     static const unsigned num_colons = 4;
    368     unsigned i;
    369     char *s = buf;
    370 
    371     if (len <= num_colons) {
    372       /* In this situation it's not possible to ensure that the correct number
    373        * of colons are included in the output. */
    374       return;
    375     }
    376 
    377     for (i = 0; i < num_colons; i++) {
    378       char *colon = strchr(s, ':');
    379       char *last_pos = &buf[len - 1] - num_colons + i;
    380 
    381       if (colon == NULL || colon > last_pos) {
    382         /* set colon |i| at last possible position (buf[len-1] is the
    383          * terminating 0). If we're setting this colon, then all whole of the
    384          * rest of the string must be colons in order to have the correct
    385          * number. */
    386         memset(last_pos, ':', num_colons - i);
    387         break;
    388       }
    389 
    390       s = colon + 1;
    391     }
    392   }
    393 }
    394 
    395 /* err_component_error_string returns the error string associated with
    396  * |packed_error|, which must be of a special form matching the keys inserted
    397  * into the error hash table. */
    398 static const char *err_component_error_string(uint32_t packed_error) {
    399   ERR_STRING_DATA *p;
    400 
    401   err_fns_check();
    402   p = ERRFN(get_item)(packed_error);
    403 
    404   if (p == NULL) {
    405     return NULL;
    406   }
    407   return p->string;
    408 }
    409 
    410 const char *ERR_lib_error_string(uint32_t packed_error) {
    411   return err_component_error_string(ERR_PACK(ERR_GET_LIB(packed_error), 0, 0));
    412 }
    413 
    414 const char *ERR_func_error_string(uint32_t packed_error) {
    415   return err_component_error_string(
    416       ERR_PACK(ERR_GET_LIB(packed_error), ERR_GET_FUNC(packed_error), 0));
    417 }
    418 
    419 const char *ERR_reason_error_string(uint32_t packed_error) {
    420   const char *reason_str = err_component_error_string(
    421       ERR_PACK(ERR_GET_LIB(packed_error), 0, ERR_GET_REASON(packed_error)));
    422 
    423   if (reason_str != NULL) {
    424     return reason_str;
    425   }
    426 
    427   return err_component_error_string(
    428       ERR_PACK(0, 0, ERR_GET_REASON(packed_error)));
    429 }
    430 
    431 void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
    432   CRYPTO_THREADID current_thread;
    433   char buf[ERR_ERROR_STRING_BUF_LEN];
    434   char buf2[1024];
    435   unsigned long thread_hash;
    436   const char *file;
    437   char *data;
    438   int line, flags;
    439   uint32_t packed_error;
    440 
    441   CRYPTO_THREADID_current(&current_thread);
    442   thread_hash = CRYPTO_THREADID_hash(&current_thread);
    443 
    444   for (;;) {
    445     packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
    446     if (packed_error == 0) {
    447       break;
    448     }
    449 
    450     ERR_error_string_n(packed_error, buf, sizeof(buf));
    451     BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
    452                  file, line, (flags & ERR_FLAG_STRING) ? data : "");
    453     if (callback(buf2, strlen(buf2), ctx) <= 0) {
    454       break;
    455     }
    456     if (flags & ERR_FLAG_MALLOCED) {
    457       OPENSSL_free(data);
    458     }
    459   }
    460 }
    461 
    462 /* err_set_error_data sets the data on the most recent error. The |flags|
    463  * argument is a combination of the |ERR_FLAG_*| values. */
    464 static void err_set_error_data(char *data, int flags) {
    465   ERR_STATE *const state = err_get_state();
    466   struct err_error_st *error;
    467 
    468   if (state->top == state->bottom) {
    469     return;
    470   }
    471 
    472   error = &state->errors[state->top];
    473 
    474   err_clear_data(error);
    475   error->data = data;
    476   error->flags = flags;
    477 }
    478 
    479 void ERR_put_error(int library, int func, int reason, const char *file,
    480                    unsigned line) {
    481   ERR_STATE *const state = err_get_state();
    482   struct err_error_st *error;
    483 
    484   if (library == ERR_LIB_SYS && reason == 0) {
    485 #if defined(WIN32)
    486     reason = GetLastError();
    487 #else
    488     reason = errno;
    489 #endif
    490   }
    491 
    492   state->top = (state->top + 1) % ERR_NUM_ERRORS;
    493   if (state->top == state->bottom) {
    494     state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
    495   }
    496 
    497   error = &state->errors[state->top];
    498   err_clear(error);
    499   error->file = file;
    500   error->line = line;
    501   error->packed = ERR_PACK(library, func, reason);
    502 }
    503 
    504 /* ERR_add_error_data_vdata takes a variable number of const char* pointers,
    505  * concatenates them and sets the result as the data on the most recent
    506  * error. */
    507 static void err_add_error_vdata(unsigned num, va_list args) {
    508   size_t alloced, new_len, len = 0, substr_len;
    509   char *buf;
    510   const char *substr;
    511   unsigned i;
    512 
    513   alloced = 80;
    514   buf = OPENSSL_malloc(alloced + 1);
    515   if (buf == NULL) {
    516     return;
    517   }
    518 
    519   for (i = 0; i < num; i++) {
    520     substr = va_arg(args, const char *);
    521     if (substr == NULL) {
    522       continue;
    523     }
    524 
    525     substr_len = strlen(substr);
    526     new_len = len + substr_len;
    527     if (new_len > alloced) {
    528       char *new_buf;
    529 
    530       if (alloced + 20 + 1 < alloced) {
    531         /* overflow. */
    532         OPENSSL_free(buf);
    533         return;
    534       }
    535 
    536       alloced = new_len + 20;
    537       new_buf = OPENSSL_realloc(buf, alloced + 1);
    538       if (new_buf == NULL) {
    539         OPENSSL_free(buf);
    540         return;
    541       }
    542       buf = new_buf;
    543     }
    544 
    545     memcpy(buf + len, substr, substr_len);
    546     len = new_len;
    547   }
    548 
    549   buf[len] = 0;
    550   err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
    551 }
    552 
    553 void ERR_add_error_data(unsigned count, ...) {
    554   va_list args;
    555   va_start(args, count);
    556   err_add_error_vdata(count, args);
    557   va_end(args);
    558 }
    559 
    560 void ERR_add_error_dataf(const char *format, ...) {
    561   va_list ap;
    562   char *buf;
    563   static const unsigned buf_len = 256;
    564 
    565   /* A fixed-size buffer is used because va_copy (which would be needed in
    566    * order to call vsnprintf twice and measure the buffer) wasn't defined until
    567    * C99. */
    568   buf = OPENSSL_malloc(buf_len + 1);
    569   if (buf == NULL) {
    570     return;
    571   }
    572 
    573   va_start(ap, format);
    574   BIO_vsnprintf(buf, buf_len, format, ap);
    575   buf[buf_len] = 0;
    576   va_end(ap);
    577 
    578   err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
    579 }
    580 
    581 int ERR_set_mark(void) {
    582   ERR_STATE *const state = err_get_state();
    583 
    584   if (state->bottom == state->top) {
    585     return 0;
    586   }
    587   state->errors[state->top].flags |= ERR_FLAG_MARK;
    588   return 1;
    589 }
    590 
    591 int ERR_pop_to_mark(void) {
    592   ERR_STATE *const state = err_get_state();
    593   struct err_error_st *error;
    594 
    595   while (state->bottom != state->top) {
    596     error = &state->errors[state->top];
    597 
    598     if ((error->flags & ERR_FLAG_MARK) != 0) {
    599       break;
    600     }
    601 
    602     err_clear(error);
    603     if (state->top == 0) {
    604       state->top = ERR_NUM_ERRORS - 1;
    605     } else {
    606       state->top--;
    607     }
    608   }
    609 
    610   if (state->bottom == state->top) {
    611     return 0;
    612   }
    613 
    614   error->flags &= ~ERR_FLAG_MARK;
    615   return 1;
    616 }
    617 
    618 static const char *const kLibraryNames[ERR_NUM_LIBS] = {
    619     "invalid library (0)",
    620     "unknown library",                            /* ERR_LIB_NONE */
    621     "system library",                             /* ERR_LIB_SYS */
    622     "bignum routines",                            /* ERR_LIB_BN */
    623     "RSA routines",                               /* ERR_LIB_RSA */
    624     "Diffie-Hellman routines",                    /* ERR_LIB_DH */
    625     "public key routines",                        /* ERR_LIB_EVP */
    626     "memory buffer routines",                     /* ERR_LIB_BUF */
    627     "object identifier routines",                 /* ERR_LIB_OBJ */
    628     "PEM routines",                               /* ERR_LIB_PEM */
    629     "DSA routines",                               /* ERR_LIB_DSA */
    630     "X.509 certificate routines",                 /* ERR_LIB_X509 */
    631     "ASN.1 encoding routines",                    /* ERR_LIB_ASN1 */
    632     "configuration file routines",                /* ERR_LIB_CONF */
    633     "common libcrypto routines",                  /* ERR_LIB_CRYPTO */
    634     "elliptic curve routines",                    /* ERR_LIB_EC */
    635     "SSL routines",                               /* ERR_LIB_SSL */
    636     "BIO routines",                               /* ERR_LIB_BIO */
    637     "PKCS7 routines",                             /* ERR_LIB_PKCS7 */
    638     "PKCS8 routines",                             /* ERR_LIB_PKCS8 */
    639     "X509 V3 routines",                           /* ERR_LIB_X509V3 */
    640     "PKCS12 routines",                            /* ERR_LIB_PKCS12 */
    641     "random number generator",                    /* ERR_LIB_RAND */
    642     "ENGINE routines",                            /* ERR_LIB_ENGINE */
    643     "OCSP routines",                              /* ERR_LIB_OCSP */
    644     "UI routines",                                /* ERR_LIB_UI */
    645     "COMP routines",                              /* ERR_LIB_COMP */
    646     "ECDSA routines",                             /* ERR_LIB_ECDSA */
    647     "ECDH routines",                              /* ERR_LIB_ECDH */
    648     "HMAC routines",                              /* ERR_LIB_HMAC */
    649     "Digest functions",                           /* ERR_LIB_DIGEST */
    650     "Cipher functions",                           /* ERR_LIB_CIPHER */
    651     "User defined functions",                     /* ERR_LIB_USER */
    652 };
    653 
    654 #define NUM_SYS_ERRNOS 127
    655 
    656 /* kStaticErrors provides storage for ERR_STRING_DATA values that are created
    657  * at init time because we assume that ERR_STRING_DATA structures aren't
    658  * allocated on the heap. */
    659 static ERR_STRING_DATA kStaticErrors[ERR_NUM_LIBS * 2 + NUM_SYS_ERRNOS];
    660 
    661 static ERR_STRING_DATA kGlobalErrors[] = {
    662     {ERR_R_MALLOC_FAILURE, "malloc failure"},
    663     {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "function should not be called"},
    664     {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
    665     {ERR_R_INTERNAL_ERROR, "internal error"},
    666     {ERR_R_OVERFLOW, "overflow"},
    667 
    668     {ERR_PACK(ERR_LIB_SYS, SYS_F_fopen, 0), "fopen"},
    669     {ERR_PACK(ERR_LIB_SYS, SYS_F_fclose, 0), "fclose"},
    670     {ERR_PACK(ERR_LIB_SYS, SYS_F_fread, 0), "fread"},
    671     {ERR_PACK(ERR_LIB_SYS, SYS_F_fwrite, 0), "fwrite"},
    672     {ERR_PACK(ERR_LIB_SYS, SYS_F_socket, 0), "socket"},
    673     {ERR_PACK(ERR_LIB_SYS, SYS_F_setsockopt, 0), "setsockopt"},
    674     {ERR_PACK(ERR_LIB_SYS, SYS_F_connect, 0), "connect"},
    675     {ERR_PACK(ERR_LIB_SYS, SYS_F_getaddrinfo, 0), "getaddrinfo"},
    676 
    677     {0, NULL},
    678 };
    679 
    680 
    681 extern const ERR_STRING_DATA ASN1_error_string_data[];
    682 extern const ERR_STRING_DATA BIO_error_string_data[];
    683 extern const ERR_STRING_DATA BN_error_string_data[];
    684 extern const ERR_STRING_DATA BUF_error_string_data[];
    685 extern const ERR_STRING_DATA CIPHER_error_string_data[];
    686 extern const ERR_STRING_DATA CONF_error_string_data[];
    687 extern const ERR_STRING_DATA CRYPTO_error_string_data[];
    688 extern const ERR_STRING_DATA DH_error_string_data[];
    689 extern const ERR_STRING_DATA DIGEST_error_string_data[];
    690 extern const ERR_STRING_DATA DSA_error_string_data[];
    691 extern const ERR_STRING_DATA ECDH_error_string_data[];
    692 extern const ERR_STRING_DATA ECDSA_error_string_data[];
    693 extern const ERR_STRING_DATA EC_error_string_data[];
    694 extern const ERR_STRING_DATA EVP_error_string_data[];
    695 extern const ERR_STRING_DATA OBJ_error_string_data[];
    696 extern const ERR_STRING_DATA PEM_error_string_data[];
    697 extern const ERR_STRING_DATA PKCS8_error_string_data[];
    698 extern const ERR_STRING_DATA RSA_error_string_data[];
    699 extern const ERR_STRING_DATA X509V3_error_string_data[];
    700 extern const ERR_STRING_DATA X509_error_string_data[];
    701 
    702 static void err_load_strings(void) {
    703   unsigned i, j = 0;
    704 
    705   err_fns_check();
    706 
    707   /* This loop loads strings for the libraries for the ERR_R_*_LIB
    708    * reasons. */
    709   for (i = ERR_LIB_NONE; i < ERR_NUM_LIBS; i++) {
    710     ERR_STRING_DATA *data = &kStaticErrors[j++];
    711     data->string = kLibraryNames[i];
    712     data->error = ERR_PACK(i, 0, 0);
    713     ERRFN(set_item)(data);
    714 
    715     data = &kStaticErrors[j++];
    716     data->string = kLibraryNames[i];
    717     data->error = ERR_PACK(0, 0, i);
    718     ERRFN(set_item)(data);
    719   }
    720 
    721   for (i = 1; i < 1 + NUM_SYS_ERRNOS; i++) {
    722     /* The "SYS" library sets errno values as the reason for its errors.
    723      * Thus we load the first |NUM_SYS_ERRNOS| errno strings as the
    724      * reason strings for that library. */
    725 
    726     ERR_STRING_DATA *data = &kStaticErrors[j++];
    727     data->string = strerror(i);
    728     data->error = ERR_PACK(ERR_LIB_SYS, 0, i);
    729     ERRFN(set_item)(data);
    730   }
    731 
    732   ERR_load_strings(kGlobalErrors);
    733 
    734   ERR_load_strings(ASN1_error_string_data);
    735   ERR_load_strings(BIO_error_string_data);
    736   ERR_load_strings(BN_error_string_data);
    737   ERR_load_strings(BUF_error_string_data);
    738   ERR_load_strings(CIPHER_error_string_data);
    739   ERR_load_strings(CONF_error_string_data);
    740   ERR_load_strings(CRYPTO_error_string_data);
    741   ERR_load_strings(DH_error_string_data);
    742   ERR_load_strings(DIGEST_error_string_data);
    743   ERR_load_strings(DSA_error_string_data);
    744   ERR_load_strings(ECDH_error_string_data);
    745   ERR_load_strings(ECDSA_error_string_data);
    746   ERR_load_strings(EC_error_string_data);
    747   ERR_load_strings(EVP_error_string_data);
    748   ERR_load_strings(OBJ_error_string_data);
    749   ERR_load_strings(PEM_error_string_data);
    750   ERR_load_strings(PKCS8_error_string_data);
    751   ERR_load_strings(RSA_error_string_data);
    752   ERR_load_strings(X509V3_error_string_data);
    753   ERR_load_strings(X509_error_string_data);
    754 }
    755 
    756 void ERR_load_strings(const ERR_STRING_DATA *str) {
    757   err_fns_check();
    758 
    759   while (str->error) {
    760     ERRFN(set_item)(str);
    761     str++;
    762   }
    763 }
    764 
    765 void ERR_load_crypto_strings(void) { err_load_strings(); }
    766 
    767 void ERR_free_strings(void) {
    768   err_fns_check();
    769   ERRFN(shutdown)();
    770 }
    771 
    772 void ERR_load_BIO_strings(void) {}
    773