195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * All rights reserved.
395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This package is an SSL implementation written
595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * by Eric Young (eay@cryptsoft.com).
695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The implementation was written so as to conform with Netscapes SSL.
795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This library is free for commercial and non-commercial use as long as
995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the following conditions are aheared to.  The following conditions
1095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * apply to all code found in this distribution, be it the RC4, RSA,
1195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * included with this distribution is covered by the same copyright terms
1395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
1595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Copyright remains Eric Young's, and as such any Copyright notices in
1695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the code are not to be removed.
1795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * If this package is used in a product, Eric Young should be given attribution
1895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * as the author of the parts of the library used.
1995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This can be in the form of a textual message at program startup or
2095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * in documentation (online or textual) provided with the package.
2195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
2295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Redistribution and use in source and binary forms, with or without
2395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * modification, are permitted provided that the following conditions
2495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * are met:
2595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 1. Redistributions of source code must retain the copyright
2695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    notice, this list of conditions and the following disclaimer.
2795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 2. Redistributions in binary form must reproduce the above copyright
2895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    notice, this list of conditions and the following disclaimer in the
2995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    documentation and/or other materials provided with the distribution.
3095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 3. All advertising materials mentioning features or use of this software
3195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    must display the following acknowledgement:
3295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    "This product includes cryptographic software written by
3395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *     Eric Young (eay@cryptsoft.com)"
3495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    The word 'cryptographic' can be left out if the rouines from the library
3595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    being used are not cryptographic related :-).
3695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 4. If you include any Windows specific code (or a derivative thereof) from
3795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    the apps directory (application code) you must include an acknowledgement:
3895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
3995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
4095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * SUCH DAMAGE.
5195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
5295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The licence and distribution terms for any publically available version or
5395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * derivative of this code cannot be changed.  i.e. this code cannot simply be
5495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * copied and put under another distribution licence
5595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * [including the GNU Public Licence.]
5695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley */
5795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* ====================================================================
5895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
5995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
6095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Redistribution and use in source and binary forms, with or without
6195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * modification, are permitted provided that the following conditions
6295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * are met:
6395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
6495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 1. Redistributions of source code must retain the above copyright
6595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    notice, this list of conditions and the following disclaimer.
6695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
6795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 2. Redistributions in binary form must reproduce the above copyright
6895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    notice, this list of conditions and the following disclaimer in
6995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    the documentation and/or other materials provided with the
7095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    distribution.
7195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
7295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 3. All advertising materials mentioning features or use of this
7395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    software must display the following acknowledgment:
7495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    "This product includes software developed by the OpenSSL Project
7595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
7795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    endorse or promote products derived from this software without
7995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    prior written permission. For written permission, please contact
8095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    openssl-core@openssl.org.
8195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
8295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 5. Products derived from this software may not be called "OpenSSL"
8395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    nor may "OpenSSL" appear in their names without prior written
8495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    permission of the OpenSSL Project.
8595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
8695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 6. Redistributions of any form whatsoever must retain the following
8795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    acknowledgment:
8895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    "This product includes software developed by the OpenSSL Project
8995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
9195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
9595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OF THE POSSIBILITY OF SUCH DAMAGE.
10395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ====================================================================
10495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
10595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This product includes cryptographic software written by Eric Young
10695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * (eay@cryptsoft.com).  This product includes software written by Tim
10795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Hudson (tjh@cryptsoft.com). */
10895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
10995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/err.h>
11095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
11195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <assert.h>
11295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <errno.h>
11395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <inttypes.h>
11495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <stdarg.h>
11595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <stdio.h>
11695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
117b7725cf3ecfcb5f31834a8fb30f2522fc5c76f8cYoshisato Yanagisawa#if defined(OPENSSL_WINDOWS)
118b7725cf3ecfcb5f31834a8fb30f2522fc5c76f8cYoshisato Yanagisawa#include <Windows.h>
119b7725cf3ecfcb5f31834a8fb30f2522fc5c76f8cYoshisato Yanagisawa#endif
120b7725cf3ecfcb5f31834a8fb30f2522fc5c76f8cYoshisato Yanagisawa
12195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/lhash.h>
12295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/mem.h>
12395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/thread.h>
12495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
12595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
12695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_fns contains a pointer to the current error implementation. */
12795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic const struct ERR_FNS_st *err_fns = NULL;
12895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const struct ERR_FNS_st openssl_err_default_impl;
12995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
13095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ERRFN(a) err_fns->a
13195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
13295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_fns_check is an internal function that checks whether "err_fns" is set
13395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * and if not, sets it to the default. */
13495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void err_fns_check(void) {
13595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  /* In practice, this is not a race problem because loading the error strings
13695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley   * at init time will cause this pointer to be set before the process goes
13795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley   * multithreaded. */
13895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (err_fns) {
13995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return;
14095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
14195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
14295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
14395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (!err_fns) {
14495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    err_fns = &openssl_err_default_impl;
14595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
14695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
14795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
14895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
14995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_clear_data frees the optional |data| member of the given error. */
15095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void err_clear_data(struct err_error_st *error) {
15195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (error->data != NULL && (error->flags & ERR_FLAG_MALLOCED) != 0) {
15295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    OPENSSL_free(error->data);
15395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
15495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->data = NULL;
15595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->flags &= ~ERR_FLAG_MALLOCED;
15695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
15795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
15895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_clear clears the given queued error. */
15995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void err_clear(struct err_error_st *error) {
16095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_clear_data(error);
16195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  memset(error, 0, sizeof(struct err_error_st));
16295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
16395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
16495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_get_state gets the ERR_STATE object for the current thread. */
16595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic ERR_STATE *err_get_state(void) {
16695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_fns_check();
16795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return ERRFN(get_state)();
16895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
16995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
17095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic uint32_t get_error_values(int inc, int top, const char **file, int *line,
17195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley                                 char **data, int *flags) {
17295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  unsigned i = 0;
17395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STATE *state;
17495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  struct err_error_st *error;
17595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  uint32_t ret;
17695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
17795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  state = err_get_state();
17895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
17995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (state->bottom == state->top) {
18095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return 0;
18195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
18295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
18395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (top) {
18495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    /* last error */
18595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    i = state->top;
18695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  } else {
18795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    i = (state->bottom + 1) % ERR_NUM_ERRORS;
18895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
18995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
19095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error = &state->errors[i];
19195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ret = error->packed;
19295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
19395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (file != NULL && line != NULL) {
19495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (error->file == NULL) {
19595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      *file = "NA";
19695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      *line = 0;
19795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    } else {
19895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      *file = error->file;
19995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      *line = error->line;
20095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
20195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
20295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
20395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (data != NULL) {
20495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (error->data == NULL) {
20595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      *data = "";
20695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      if (flags != NULL) {
20795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        *flags = 0;
20895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      }
20995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    } else {
21095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      *data = error->data;
21195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      if (flags != NULL) {
21295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        *flags = error->flags & ERR_FLAG_PUBLIC_MASK;
21395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      }
21495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      error->data = NULL;
21595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      error->flags = 0;
21695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
21795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
21895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
21995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (inc) {
22095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    assert(!top);
22195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    err_clear(error);
22295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    state->bottom = i;
22395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
22495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
22595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return ret;
22695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
22795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
22895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_get_error(void) {
22995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(1, 0, NULL, NULL, NULL, NULL);
23095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
23195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
23295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_get_error_line(const char **file, int *line) {
23395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(1, 0, file, line, NULL, NULL);
23495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
23595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
23695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_get_error_line_data(const char **file, int *line,
23795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley                                 char **data, int *flags) {
23895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(1, 0, file, line, data, flags);
23995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
24095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
24195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_peek_error(void) {
24295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(0, 0, NULL, NULL, NULL, NULL);
24395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
24495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
24595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_peek_error_line(const char **file, int *line) {
24695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(0, 0, file, line, NULL, NULL);
24795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
24895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
24995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_peek_error_line_data(const char **file, int *line,
25095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley                                  const char **data, int *flags) {
25195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(0, 0, file, line, (char **) data, flags);
25295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
25395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
25495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_peek_last_error(void) {
25595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(0, 1, NULL, NULL, NULL, NULL);
25695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
25795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
25895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_peek_last_error_line(const char **file, int *line) {
25995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(0, 1, file, line, NULL, NULL);
26095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
26195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
26295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyuint32_t ERR_peek_last_error_line_data(const char **file, int *line,
26395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley                                       const char **data, int *flags) {
26495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return get_error_values(0, 1, file, line, (char **) data, flags);
26595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
26695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
26795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_clear_error(void) {
26895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STATE *const state = err_get_state();
26995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  unsigned i;
27095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
27195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  for (i = 0; i < ERR_NUM_ERRORS; i++) {
27295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    err_clear(&state->errors[i]);
27395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
27495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
27595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  state->top = state->bottom = 0;
27695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
27795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
27803d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langleyvoid ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
27903d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  CRYPTO_THREADID current;
28003d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  ERR_STATE *state;
28103d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  unsigned i;
28203d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley
28303d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  if (tid == NULL) {
28403d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley    CRYPTO_THREADID_current(&current);
28503d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley    tid = &current;
28603d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  }
28703d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley
28803d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  err_fns_check();
28903d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  state = ERRFN(release_state)(tid);
29003d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  if (state == NULL) {
29103d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley    return;
29203d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  }
29303d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley
29403d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  for (i = 0; i < ERR_NUM_ERRORS; i++) {
29503d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley    err_clear(&state->errors[i]);
29603d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  }
29703d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley
29803d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley  OPENSSL_free(state);
29903d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley}
30003d31ed3e49a71e9ea09232d817cb1200b7a9149Adam Langley
301c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjaminint ERR_get_next_error_library(void) {
3024042667eb90b5619e6ba2d8f6f407cb55f71c05aAdam Langley  err_fns_check();
3034042667eb90b5619e6ba2d8f6f407cb55f71c05aAdam Langley  return ERRFN(get_next_library)();
3044042667eb90b5619e6ba2d8f6f407cb55f71c05aAdam Langley}
3054042667eb90b5619e6ba2d8f6f407cb55f71c05aAdam Langley
30695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_clear_system_error(void) {
30795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  errno = 0;
30895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
30995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
31095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleychar *ERR_error_string(uint32_t packed_error, char *ret) {
31195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  static char buf[ERR_ERROR_STRING_BUF_LEN];
31295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
31395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (ret == NULL) {
31495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    /* TODO(fork): remove this. */
31595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ret = buf;
31695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
31795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
31895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#if !defined(NDEBUG)
31995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  /* This is aimed to help catch callers who don't provide
32095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley   * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
32195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
32295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#endif
32395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
32495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
32595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
32695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return ret;
32795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
32895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
32995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
33095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  char lib_buf[64], func_buf[64], reason_buf[64];
33195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  const char *lib_str, *func_str, *reason_str;
33295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  unsigned lib, func, reason;
33395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
33495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (len == 0) {
33595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return;
33695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
33795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
33895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  lib = ERR_GET_LIB(packed_error);
33995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  func = ERR_GET_FUNC(packed_error);
34095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  reason = ERR_GET_REASON(packed_error);
34195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
34295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  lib_str = ERR_lib_error_string(packed_error);
34395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  func_str = ERR_func_error_string(packed_error);
34495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  reason_str = ERR_reason_error_string(packed_error);
34595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
34695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (lib_str == NULL) {
34795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
34895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    lib_str = lib_buf;
34995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
35095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
35195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (func_str == NULL) {
35295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    BIO_snprintf(func_buf, sizeof(func_buf), "func(%u)", func);
35395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    func_str = func_buf;
35495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
35595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
35695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (reason_str == NULL) {
35795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
35895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    reason_str = reason_buf;
35995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
36095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
36195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:%s:%s",
36295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley               packed_error, lib_str, func_str, reason_str);
36395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
36495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (strlen(buf) == len - 1) {
36595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    /* output may be truncated; make sure we always have 5 colon-separated
36695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley     * fields, i.e. 4 colons. */
36795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    static const unsigned num_colons = 4;
36895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    unsigned i;
36995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    char *s = buf;
37095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
37195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (len <= num_colons) {
37295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      /* In this situation it's not possible to ensure that the correct number
37395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley       * of colons are included in the output. */
37495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      return;
37595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
37695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
37795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    for (i = 0; i < num_colons; i++) {
37895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      char *colon = strchr(s, ':');
37995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      char *last_pos = &buf[len - 1] - num_colons + i;
38095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
38195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      if (colon == NULL || colon > last_pos) {
38295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        /* set colon |i| at last possible position (buf[len-1] is the
38395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley         * terminating 0). If we're setting this colon, then all whole of the
38495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley         * rest of the string must be colons in order to have the correct
38595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley         * number. */
38695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        memset(last_pos, ':', num_colons - i);
38795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        break;
38895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      }
38995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
39095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      s = colon + 1;
39195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
39295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
39395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
39495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
39595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_component_error_string returns the error string associated with
39695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * |packed_error|, which must be of a special form matching the keys inserted
39795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * into the error hash table. */
39895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic const char *err_component_error_string(uint32_t packed_error) {
39995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STRING_DATA *p;
40095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
40195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_fns_check();
40295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  p = ERRFN(get_item)(packed_error);
40395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
40495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (p == NULL) {
40595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return NULL;
40695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
40795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return p->string;
40895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
40995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
41095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyconst char *ERR_lib_error_string(uint32_t packed_error) {
41195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return err_component_error_string(ERR_PACK(ERR_GET_LIB(packed_error), 0, 0));
41295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
41395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
41495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyconst char *ERR_func_error_string(uint32_t packed_error) {
41595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return err_component_error_string(
41695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      ERR_PACK(ERR_GET_LIB(packed_error), ERR_GET_FUNC(packed_error), 0));
41795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
41895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
41995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyconst char *ERR_reason_error_string(uint32_t packed_error) {
42095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  const char *reason_str = err_component_error_string(
42195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      ERR_PACK(ERR_GET_LIB(packed_error), 0, ERR_GET_REASON(packed_error)));
42295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
42395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (reason_str != NULL) {
42495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return reason_str;
42595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
42695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
42795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return err_component_error_string(
42895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      ERR_PACK(0, 0, ERR_GET_REASON(packed_error)));
42995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
43095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
43195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
43295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  CRYPTO_THREADID current_thread;
43395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  char buf[ERR_ERROR_STRING_BUF_LEN];
43495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  char buf2[1024];
43595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  unsigned long thread_hash;
43695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  const char *file;
43795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  char *data;
43895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  int line, flags;
43995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  uint32_t packed_error;
44095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
44195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  CRYPTO_THREADID_current(&current_thread);
44295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  thread_hash = CRYPTO_THREADID_hash(&current_thread);
44395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
44495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  for (;;) {
44595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
44695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (packed_error == 0) {
44795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      break;
44895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
44995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
45095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERR_error_string_n(packed_error, buf, sizeof(buf));
45195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
45295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley                 file, line, (flags & ERR_FLAG_STRING) ? data : "");
45395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (callback(buf2, strlen(buf2), ctx) <= 0) {
45495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      break;
45595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
45695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (flags & ERR_FLAG_MALLOCED) {
45795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      OPENSSL_free(data);
45895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
45995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
46095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
46195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
46295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* err_set_error_data sets the data on the most recent error. The |flags|
46395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * argument is a combination of the |ERR_FLAG_*| values. */
46495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void err_set_error_data(char *data, int flags) {
46595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STATE *const state = err_get_state();
46695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  struct err_error_st *error;
46795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
46895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (state->top == state->bottom) {
46995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return;
47095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
47195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
47295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error = &state->errors[state->top];
47395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
47495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_clear_data(error);
47595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->data = data;
47695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->flags = flags;
47795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
47895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
47995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_put_error(int library, int func, int reason, const char *file,
48095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley                   unsigned line) {
48195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STATE *const state = err_get_state();
48295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  struct err_error_st *error;
48395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
48495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (library == ERR_LIB_SYS && reason == 0) {
48595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#if defined(WIN32)
48695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    reason = GetLastError();
48795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#else
48895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    reason = errno;
48995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#endif
49095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
49195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
49295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  state->top = (state->top + 1) % ERR_NUM_ERRORS;
49395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (state->top == state->bottom) {
49495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
49595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
49695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
49795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error = &state->errors[state->top];
49895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_clear(error);
49995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->file = file;
50095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->line = line;
50195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->packed = ERR_PACK(library, func, reason);
50295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
50395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
50495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* ERR_add_error_data_vdata takes a variable number of const char* pointers,
50595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * concatenates them and sets the result as the data on the most recent
50695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * error. */
50795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void err_add_error_vdata(unsigned num, va_list args) {
50895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  size_t alloced, new_len, len = 0, substr_len;
50995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  char *buf;
51095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  const char *substr;
51195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  unsigned i;
51295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
51395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  alloced = 80;
51495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  buf = OPENSSL_malloc(alloced + 1);
51595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (buf == NULL) {
51695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return;
51795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
51895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
51995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  for (i = 0; i < num; i++) {
52095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    substr = va_arg(args, const char *);
52195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (substr == NULL) {
52295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      continue;
52395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
52495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
52595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    substr_len = strlen(substr);
52695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    new_len = len + substr_len;
52795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (new_len > alloced) {
52895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      char *new_buf;
52995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
53095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      if (alloced + 20 + 1 < alloced) {
53195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        /* overflow. */
53295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        OPENSSL_free(buf);
53395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        return;
53495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      }
53595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
53695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      alloced = new_len + 20;
53795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      new_buf = OPENSSL_realloc(buf, alloced + 1);
53895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      if (new_buf == NULL) {
53995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        OPENSSL_free(buf);
54095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley        return;
54195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      }
54295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      buf = new_buf;
54395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
54495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
54595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    memcpy(buf + len, substr, substr_len);
54695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    len = new_len;
54795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
54895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
54995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  buf[len] = 0;
55095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
55195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
55295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
55395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_add_error_data(unsigned count, ...) {
55495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  va_list args;
55595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  va_start(args, count);
55695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_add_error_vdata(count, args);
55795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  va_end(args);
55895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
55995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
56095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_add_error_dataf(const char *format, ...) {
56195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  va_list ap;
56295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  char *buf;
56395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  static const unsigned buf_len = 256;
56495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
56595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  /* A fixed-size buffer is used because va_copy (which would be needed in
56695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley   * order to call vsnprintf twice and measure the buffer) wasn't defined until
56795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley   * C99. */
56895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  buf = OPENSSL_malloc(buf_len + 1);
56995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (buf == NULL) {
57095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return;
57195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
57295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
57395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  va_start(ap, format);
57495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  BIO_vsnprintf(buf, buf_len, format, ap);
57595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  buf[buf_len] = 0;
57695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  va_end(ap);
57795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
57895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
57995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
58095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
58195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyint ERR_set_mark(void) {
58295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STATE *const state = err_get_state();
58395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
58495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (state->bottom == state->top) {
58595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return 0;
58695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
58795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  state->errors[state->top].flags |= ERR_FLAG_MARK;
58895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return 1;
58995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
59095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
59195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyint ERR_pop_to_mark(void) {
59295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_STATE *const state = err_get_state();
59395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  struct err_error_st *error;
59495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
59595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  while (state->bottom != state->top) {
59695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    error = &state->errors[state->top];
59795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
59895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if ((error->flags & ERR_FLAG_MARK) != 0) {
59995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      break;
60095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
60195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
60295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    err_clear(error);
60395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    if (state->top == 0) {
60495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      state->top = ERR_NUM_ERRORS - 1;
60595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    } else {
60695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley      state->top--;
60795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    }
60895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
60995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
61095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  if (state->bottom == state->top) {
61195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    return 0;
61295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
61395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
61495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  error->flags &= ~ERR_FLAG_MARK;
61595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  return 1;
61695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
61795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
61895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic const char *const kLibraryNames[ERR_NUM_LIBS] = {
61995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "invalid library (0)",
62095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "unknown library",                            /* ERR_LIB_NONE */
62195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "system library",                             /* ERR_LIB_SYS */
62295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "bignum routines",                            /* ERR_LIB_BN */
62395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "RSA routines",                               /* ERR_LIB_RSA */
62495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "Diffie-Hellman routines",                    /* ERR_LIB_DH */
62595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "public key routines",                        /* ERR_LIB_EVP */
62695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "memory buffer routines",                     /* ERR_LIB_BUF */
62795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "object identifier routines",                 /* ERR_LIB_OBJ */
62895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "PEM routines",                               /* ERR_LIB_PEM */
62995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "DSA routines",                               /* ERR_LIB_DSA */
63095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "X.509 certificate routines",                 /* ERR_LIB_X509 */
63195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "ASN.1 encoding routines",                    /* ERR_LIB_ASN1 */
63295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "configuration file routines",                /* ERR_LIB_CONF */
63395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "common libcrypto routines",                  /* ERR_LIB_CRYPTO */
63495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "elliptic curve routines",                    /* ERR_LIB_EC */
63595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "SSL routines",                               /* ERR_LIB_SSL */
63695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "BIO routines",                               /* ERR_LIB_BIO */
63795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "PKCS7 routines",                             /* ERR_LIB_PKCS7 */
63895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "PKCS8 routines",                             /* ERR_LIB_PKCS8 */
63995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "X509 V3 routines",                           /* ERR_LIB_X509V3 */
64095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "PKCS12 routines",                            /* ERR_LIB_PKCS12 */
64195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "random number generator",                    /* ERR_LIB_RAND */
64295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "ENGINE routines",                            /* ERR_LIB_ENGINE */
64395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "OCSP routines",                              /* ERR_LIB_OCSP */
64495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "UI routines",                                /* ERR_LIB_UI */
64595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "COMP routines",                              /* ERR_LIB_COMP */
64695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "ECDSA routines",                             /* ERR_LIB_ECDSA */
64795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "ECDH routines",                              /* ERR_LIB_ECDH */
64895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "HMAC routines",                              /* ERR_LIB_HMAC */
64995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "Digest functions",                           /* ERR_LIB_DIGEST */
65095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "Cipher functions",                           /* ERR_LIB_CIPHER */
65195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    "User defined functions",                     /* ERR_LIB_USER */
65295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley};
65395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
65495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define NUM_SYS_ERRNOS 127
65595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
65695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* kStaticErrors provides storage for ERR_STRING_DATA values that are created
65795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * at init time because we assume that ERR_STRING_DATA structures aren't
65895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * allocated on the heap. */
65995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic ERR_STRING_DATA kStaticErrors[ERR_NUM_LIBS * 2 + NUM_SYS_ERRNOS];
66095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
66195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic ERR_STRING_DATA kGlobalErrors[] = {
66295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_R_MALLOC_FAILURE, "malloc failure"},
66395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "function should not be called"},
66495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
66595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_R_INTERNAL_ERROR, "internal error"},
666660140206ed32aa217ba3f299debae8d9ac472ecAdam Langley    {ERR_R_OVERFLOW, "overflow"},
66795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
66895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_fopen, 0), "fopen"},
66995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_fclose, 0), "fclose"},
67095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_fread, 0), "fread"},
67195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_fwrite, 0), "fwrite"},
67295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_socket, 0), "socket"},
67395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_setsockopt, 0), "setsockopt"},
67495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_connect, 0), "connect"},
67595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {ERR_PACK(ERR_LIB_SYS, SYS_F_getaddrinfo, 0), "getaddrinfo"},
67695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
67795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    {0, NULL},
67895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley};
67995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
68095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
68195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA ASN1_error_string_data[];
68295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA BIO_error_string_data[];
68395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA BN_error_string_data[];
68495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA BUF_error_string_data[];
68595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA CIPHER_error_string_data[];
68695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA CONF_error_string_data[];
68795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA CRYPTO_error_string_data[];
68895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA DH_error_string_data[];
68995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA DIGEST_error_string_data[];
69095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA DSA_error_string_data[];
69195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA ECDH_error_string_data[];
69295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA ECDSA_error_string_data[];
69395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA EC_error_string_data[];
69495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA EVP_error_string_data[];
69595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA OBJ_error_string_data[];
69695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA PEM_error_string_data[];
6978e16b6e6582810de46b4db7926a2462be8e789d2Adam Langleyextern const ERR_STRING_DATA PKCS8_error_string_data[];
69895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA RSA_error_string_data[];
69995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA X509V3_error_string_data[];
70095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyextern const ERR_STRING_DATA X509_error_string_data[];
70195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
70295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void err_load_strings(void) {
70395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  unsigned i, j = 0;
70495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
70595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_fns_check();
70695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
70795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  /* This loop loads strings for the libraries for the ERR_R_*_LIB
70895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley   * reasons. */
70995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  for (i = ERR_LIB_NONE; i < ERR_NUM_LIBS; i++) {
71095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERR_STRING_DATA *data = &kStaticErrors[j++];
71195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data->string = kLibraryNames[i];
71295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data->error = ERR_PACK(i, 0, 0);
71395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERRFN(set_item)(data);
71495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
71595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data = &kStaticErrors[j++];
71695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data->string = kLibraryNames[i];
71795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data->error = ERR_PACK(0, 0, i);
71895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERRFN(set_item)(data);
71995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
72095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
72195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  for (i = 1; i < 1 + NUM_SYS_ERRNOS; i++) {
72295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    /* The "SYS" library sets errno values as the reason for its errors.
72395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley     * Thus we load the first |NUM_SYS_ERRNOS| errno strings as the
72495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley     * reason strings for that library. */
72595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
72695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERR_STRING_DATA *data = &kStaticErrors[j++];
72795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data->string = strerror(i);
72895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    data->error = ERR_PACK(ERR_LIB_SYS, 0, i);
72995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERRFN(set_item)(data);
73095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
73195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
73295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(kGlobalErrors);
73395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
73495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(ASN1_error_string_data);
73595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(BIO_error_string_data);
73695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(BN_error_string_data);
73795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(BUF_error_string_data);
73895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(CIPHER_error_string_data);
73995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(CONF_error_string_data);
74095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(CRYPTO_error_string_data);
74195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(DH_error_string_data);
74295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(DIGEST_error_string_data);
74395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(DSA_error_string_data);
74495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(ECDH_error_string_data);
74595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(ECDSA_error_string_data);
74695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(EC_error_string_data);
74795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(EVP_error_string_data);
74895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(OBJ_error_string_data);
74995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(PEM_error_string_data);
7508e16b6e6582810de46b4db7926a2462be8e789d2Adam Langley  ERR_load_strings(PKCS8_error_string_data);
75195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(RSA_error_string_data);
75295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(X509V3_error_string_data);
75395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERR_load_strings(X509_error_string_data);
75495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
75595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
75695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ERR_load_strings(const ERR_STRING_DATA *str) {
75795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_fns_check();
75895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
75995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  while (str->error) {
76095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    ERRFN(set_item)(str);
76195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley    str++;
76295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  }
76395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
76495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
765c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjaminvoid ERR_load_crypto_strings(void) { err_load_strings(); }
76695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
767c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjaminvoid ERR_free_strings(void) {
76895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  err_fns_check();
76995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley  ERRFN(shutdown)();
77095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
77195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
772c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjaminvoid ERR_load_BIO_strings(void) {}
773