Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@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@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@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@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@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@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 109 | // Ensure we can't call OPENSSL_malloc circularly. |
| 110 | #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <openssl/err.h> |
| 112 | |
| 113 | #include <assert.h> |
| 114 | #include <errno.h> |
| 115 | #include <inttypes.h> |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 116 | #include <limits.h> |
| 117 | #include <stdarg.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 118 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
Yoshisato Yanagisawa | b7725cf | 2014-08-18 11:39:09 -0700 | [diff] [blame] | 120 | #if defined(OPENSSL_WINDOWS) |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 121 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 122 | #include <windows.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 123 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Yoshisato Yanagisawa | b7725cf | 2014-08-18 11:39:09 -0700 | [diff] [blame] | 124 | #endif |
| 125 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | #include <openssl/mem.h> |
| 127 | #include <openssl/thread.h> |
| 128 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 129 | #include "../internal.h" |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 130 | #include "./internal.h" |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 131 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 133 | struct err_error_st { |
| 134 | // file contains the filename where the error occurred. |
| 135 | const char *file; |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 136 | // data contains a NUL-terminated string with optional data. It is allocated |
| 137 | // with system |malloc| and must be freed with |free| (not |OPENSSL_free|) |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 138 | char *data; |
| 139 | // packed contains the error library and reason, as packed by ERR_PACK. |
| 140 | uint32_t packed; |
| 141 | // line contains the line number where the error occurred. |
| 142 | uint16_t line; |
| 143 | // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|. |
| 144 | unsigned mark : 1; |
| 145 | }; |
| 146 | |
| 147 | // ERR_STATE contains the per-thread, error queue. |
| 148 | typedef struct err_state_st { |
David Benjamin | 0f222e6 | 2023-07-03 12:24:51 -0400 | [diff] [blame] | 149 | // errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a |
| 150 | // ring buffer. |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 151 | struct err_error_st errors[ERR_NUM_ERRORS]; |
David Benjamin | 0f222e6 | 2023-07-03 12:24:51 -0400 | [diff] [blame] | 152 | // top contains the index of the most recent error. If |top| equals |bottom| |
| 153 | // then the queue is empty. |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 154 | unsigned top; |
David Benjamin | 0f222e6 | 2023-07-03 12:24:51 -0400 | [diff] [blame] | 155 | // bottom contains the index before the least recent error in the queue. |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 156 | unsigned bottom; |
| 157 | |
| 158 | // to_free, if not NULL, contains a pointer owned by this structure that was |
| 159 | // previously a |data| pointer of one of the elements of |errors|. |
| 160 | void *to_free; |
| 161 | } ERR_STATE; |
| 162 | |
David Benjamin | d27eda0 | 2015-03-05 01:19:27 -0500 | [diff] [blame] | 163 | extern const uint32_t kOpenSSLReasonValues[]; |
| 164 | extern const size_t kOpenSSLReasonValuesLen; |
| 165 | extern const char kOpenSSLReasonStringData[]; |
| 166 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 167 | // err_clear clears the given queued error. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | static void err_clear(struct err_error_st *error) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 169 | free(error->data); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 170 | OPENSSL_memset(error, 0, sizeof(struct err_error_st)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | } |
| 172 | |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 173 | static void err_copy(struct err_error_st *dst, const struct err_error_st *src) { |
| 174 | err_clear(dst); |
| 175 | dst->file = src->file; |
| 176 | if (src->data != NULL) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 177 | // Disable deprecated functions on msvc so it doesn't complain about strdup. |
| 178 | OPENSSL_MSVC_PRAGMA(warning(push)) |
| 179 | OPENSSL_MSVC_PRAGMA(warning(disable : 4996)) |
| 180 | // We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
| 181 | // which can affect the error stack. |
| 182 | dst->data = strdup(src->data); |
| 183 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 184 | } |
| 185 | dst->packed = src->packed; |
| 186 | dst->line = src->line; |
| 187 | } |
| 188 | |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 189 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 190 | // global_next_library contains the next custom library value to return. |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 191 | static int global_next_library = ERR_NUM_LIBS; |
| 192 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 193 | // global_next_library_mutex protects |global_next_library| from concurrent |
| 194 | // updates. |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 195 | static CRYPTO_MUTEX global_next_library_mutex = CRYPTO_MUTEX_INIT; |
Adam Langley | 8bdd542 | 2015-04-13 11:04:09 -0700 | [diff] [blame] | 196 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 197 | static void err_state_free(void *statep) { |
| 198 | ERR_STATE *state = statep; |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 199 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 200 | if (state == NULL) { |
| 201 | return; |
| 202 | } |
| 203 | |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 204 | for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 205 | err_clear(&state->errors[i]); |
| 206 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 207 | free(state->to_free); |
| 208 | free(state); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 209 | } |
| 210 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 211 | // err_get_state gets the ERR_STATE object for the current thread. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 212 | static ERR_STATE *err_get_state(void) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 213 | ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 214 | if (state == NULL) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 215 | state = malloc(sizeof(ERR_STATE)); |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 216 | if (state == NULL) { |
| 217 | return NULL; |
| 218 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 219 | OPENSSL_memset(state, 0, sizeof(ERR_STATE)); |
Adam Langley | 8bdd542 | 2015-04-13 11:04:09 -0700 | [diff] [blame] | 220 | if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state, |
| 221 | err_state_free)) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 222 | return NULL; |
| 223 | } |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 226 | return state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static uint32_t get_error_values(int inc, int top, const char **file, int *line, |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 230 | const char **data, int *flags) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | unsigned i = 0; |
| 232 | ERR_STATE *state; |
| 233 | struct err_error_st *error; |
| 234 | uint32_t ret; |
| 235 | |
| 236 | state = err_get_state(); |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 237 | if (state == NULL || state->bottom == state->top) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | if (top) { |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 242 | assert(!inc); |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 243 | // last error |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | i = state->top; |
| 245 | } else { |
| 246 | i = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 247 | } |
| 248 | |
| 249 | error = &state->errors[i]; |
| 250 | ret = error->packed; |
| 251 | |
| 252 | if (file != NULL && line != NULL) { |
| 253 | if (error->file == NULL) { |
| 254 | *file = "NA"; |
| 255 | *line = 0; |
| 256 | } else { |
| 257 | *file = error->file; |
| 258 | *line = error->line; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (data != NULL) { |
| 263 | if (error->data == NULL) { |
| 264 | *data = ""; |
| 265 | if (flags != NULL) { |
| 266 | *flags = 0; |
| 267 | } |
| 268 | } else { |
| 269 | *data = error->data; |
| 270 | if (flags != NULL) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 271 | // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a |
| 272 | // static lifetime. In both cases, we retain ownership of the string, |
| 273 | // and the caller is not expected to free it. |
| 274 | *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | } |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 276 | // If this error is being removed, take ownership of data from |
| 277 | // the error. The semantics are such that the caller doesn't |
| 278 | // take ownership either. Instead the error system takes |
| 279 | // ownership and retains it until the next call that affects the |
| 280 | // error queue. |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 281 | if (inc) { |
David Benjamin | e1c3dad | 2017-09-28 16:33:09 -0400 | [diff] [blame] | 282 | if (error->data != NULL) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 283 | free(state->to_free); |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 284 | state->to_free = error->data; |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 285 | } |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 286 | error->data = NULL; |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 287 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | if (inc) { |
| 292 | assert(!top); |
| 293 | err_clear(error); |
| 294 | state->bottom = i; |
| 295 | } |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | uint32_t ERR_get_error(void) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 301 | return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | uint32_t ERR_get_error_line(const char **file, int *line) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 305 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | uint32_t ERR_get_error_line_data(const char **file, int *line, |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 309 | const char **data, int *flags) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 310 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | uint32_t ERR_peek_error(void) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 314 | return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | uint32_t ERR_peek_error_line(const char **file, int *line) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 318 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | uint32_t ERR_peek_error_line_data(const char **file, int *line, |
| 322 | const char **data, int *flags) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 323 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data, |
| 324 | flags); |
| 325 | } |
| 326 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | uint32_t ERR_peek_last_error(void) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 328 | return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | uint32_t ERR_peek_last_error_line(const char **file, int *line) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 332 | return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | uint32_t ERR_peek_last_error_line_data(const char **file, int *line, |
| 336 | const char **data, int *flags) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 337 | return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void ERR_clear_error(void) { |
| 341 | ERR_STATE *const state = err_get_state(); |
| 342 | unsigned i; |
| 343 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 344 | if (state == NULL) { |
| 345 | return; |
| 346 | } |
| 347 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
| 349 | err_clear(&state->errors[i]); |
| 350 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 351 | free(state->to_free); |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 352 | state->to_free = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
| 354 | state->top = state->bottom = 0; |
| 355 | } |
| 356 | |
Adam Langley | 03d31ed | 2014-08-05 16:54:47 -0700 | [diff] [blame] | 357 | void ERR_remove_thread_state(const CRYPTO_THREADID *tid) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 358 | if (tid != NULL) { |
| 359 | assert(0); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 363 | ERR_clear_error(); |
Adam Langley | 03d31ed | 2014-08-05 16:54:47 -0700 | [diff] [blame] | 364 | } |
| 365 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 366 | int ERR_get_next_error_library(void) { |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 367 | int ret; |
| 368 | |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 369 | CRYPTO_MUTEX_lock_write(&global_next_library_mutex); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 370 | ret = global_next_library++; |
David Benjamin | 04c3d40 | 2023-06-03 01:26:29 -0400 | [diff] [blame] | 371 | CRYPTO_MUTEX_unlock_write(&global_next_library_mutex); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 372 | |
| 373 | return ret; |
Adam Langley | 4042667 | 2014-07-01 16:48:28 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Matt Braithwaite | 9b68e72 | 2015-05-06 13:57:29 -0700 | [diff] [blame] | 376 | void ERR_remove_state(unsigned long pid) { |
| 377 | ERR_clear_error(); |
| 378 | } |
| 379 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | void ERR_clear_system_error(void) { |
| 381 | errno = 0; |
| 382 | } |
| 383 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 384 | // err_string_cmp is a compare function for searching error values with |
| 385 | // |bsearch| in |err_string_lookup|. |
| 386 | static int err_string_cmp(const void *a, const void *b) { |
| 387 | const uint32_t a_key = *((const uint32_t*) a) >> 15; |
| 388 | const uint32_t b_key = *((const uint32_t*) b) >> 15; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 389 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 390 | if (a_key < b_key) { |
| 391 | return -1; |
| 392 | } else if (a_key > b_key) { |
| 393 | return 1; |
| 394 | } else { |
| 395 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | } |
| 398 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 399 | // err_string_lookup looks up the string associated with |lib| and |key| in |
| 400 | // |values| and |string_data|. It returns the string or NULL if not found. |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 401 | static const char *err_string_lookup(uint32_t lib, uint32_t key, |
| 402 | const uint32_t *values, |
| 403 | size_t num_values, |
| 404 | const char *string_data) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 405 | // |values| points to data in err_data.h, which is generated by |
| 406 | // err_data_generate.go. It's an array of uint32_t values. Each value has the |
| 407 | // following structure: |
| 408 | // | lib | key | offset | |
| 409 | // |6 bits| 11 bits | 15 bits | |
| 410 | // |
| 411 | // The |lib| value is a library identifier: one of the |ERR_LIB_*| values. |
| 412 | // The |key| is a reason code, depending on the context. |
| 413 | // The |offset| is the number of bytes from the start of |string_data| where |
| 414 | // the (NUL terminated) string for this value can be found. |
| 415 | // |
| 416 | // Values are sorted based on treating the |lib| and |key| part as an |
| 417 | // unsigned integer. |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 418 | if (lib >= (1 << 6) || key >= (1 << 11)) { |
| 419 | return NULL; |
| 420 | } |
| 421 | uint32_t search_key = lib << 26 | key << 15; |
| 422 | const uint32_t *result = bsearch(&search_key, values, num_values, |
| 423 | sizeof(uint32_t), err_string_cmp); |
| 424 | if (result == NULL) { |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | return &string_data[(*result) & 0x7fff]; |
| 429 | } |
| 430 | |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 431 | typedef struct library_name_st { |
| 432 | const char *str; |
| 433 | const char *symbol; |
| 434 | const char *reason_symbol; |
| 435 | } LIBRARY_NAME; |
| 436 | |
| 437 | static const LIBRARY_NAME kLibraryNames[ERR_NUM_LIBS] = { |
| 438 | {"invalid library (0)", NULL, NULL}, |
| 439 | {"unknown library", "NONE", "NONE_LIB"}, |
| 440 | {"system library", "SYS", "SYS_LIB"}, |
| 441 | {"bignum routines", "BN", "BN_LIB"}, |
| 442 | {"RSA routines", "RSA", "RSA_LIB"}, |
| 443 | {"Diffie-Hellman routines", "DH", "DH_LIB"}, |
| 444 | {"public key routines", "EVP", "EVP_LIB"}, |
| 445 | {"memory buffer routines", "BUF", "BUF_LIB"}, |
| 446 | {"object identifier routines", "OBJ", "OBJ_LIB"}, |
| 447 | {"PEM routines", "PEM", "PEM_LIB"}, |
| 448 | {"DSA routines", "DSA", "DSA_LIB"}, |
| 449 | {"X.509 certificate routines", "X509", "X509_LIB"}, |
| 450 | {"ASN.1 encoding routines", "ASN1", "ASN1_LIB"}, |
| 451 | {"configuration file routines", "CONF", "CONF_LIB"}, |
| 452 | {"common libcrypto routines", "CRYPTO", "CRYPTO_LIB"}, |
| 453 | {"elliptic curve routines", "EC", "EC_LIB"}, |
| 454 | {"SSL routines", "SSL", "SSL_LIB"}, |
| 455 | {"BIO routines", "BIO", "BIO_LIB"}, |
| 456 | {"PKCS7 routines", "PKCS7", "PKCS7_LIB"}, |
| 457 | {"PKCS8 routines", "PKCS8", "PKCS8_LIB"}, |
| 458 | {"X509 V3 routines", "X509V3", "X509V3_LIB"}, |
| 459 | {"random number generator", "RAND", "RAND_LIB"}, |
| 460 | {"ENGINE routines", "ENGINE", "ENGINE_LIB"}, |
| 461 | {"OCSP routines", "OCSP", "OCSP_LIB"}, |
| 462 | {"UI routines", "UI", "UI_LIB"}, |
| 463 | {"COMP routines", "COMP", "COMP_LIB"}, |
| 464 | {"ECDSA routines", "ECDSA", "ECDSA_LIB"}, |
| 465 | {"ECDH routines", "ECDH", "ECDH_LIB"}, |
| 466 | {"HMAC routines", "HMAC", "HMAC_LIB"}, |
| 467 | {"Digest functions", "DIGEST", "DIGEST_LIB"}, |
| 468 | {"Cipher functions", "CIPHER", "CIPHER_LIB"}, |
| 469 | {"HKDF functions", "HKDF", "HKDF_LIB"}, |
| 470 | {"Trust Token functions", "TRUST_TOKEN", "TRUST_TOKEN_LIB"}, |
| 471 | {"User defined functions", "USER", "USER_LIB"}, |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 472 | }; |
| 473 | |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 474 | static const char *err_lib_error_string(uint32_t packed_error) { |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 475 | const uint32_t lib = ERR_GET_LIB(packed_error); |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 476 | return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].str; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | } |
| 478 | |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 479 | const char *ERR_lib_error_string(uint32_t packed_error) { |
| 480 | const char *ret = err_lib_error_string(packed_error); |
| 481 | return ret == NULL ? "unknown library" : ret; |
| 482 | } |
| 483 | |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 484 | const char *ERR_lib_symbol_name(uint32_t packed_error) { |
| 485 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 486 | return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].symbol; |
| 487 | } |
| 488 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | const char *ERR_func_error_string(uint32_t packed_error) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 490 | return "OPENSSL_internal"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | } |
| 492 | |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 493 | static const char *err_reason_error_string(uint32_t packed_error, int symbol) { |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 494 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 495 | const uint32_t reason = ERR_GET_REASON(packed_error); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 496 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 497 | if (lib == ERR_LIB_SYS) { |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 498 | if (!symbol && reason < 127) { |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 499 | return strerror(reason); |
| 500 | } |
| 501 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 504 | if (reason < ERR_NUM_LIBS) { |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 505 | return symbol ? kLibraryNames[reason].reason_symbol |
| 506 | : kLibraryNames[reason].str; |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (reason < 100) { |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 510 | // TODO(davidben): All our other reason strings match the symbol name. Only |
| 511 | // the common ones differ. Should we just consistently return the symbol |
| 512 | // name? |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 513 | switch (reason) { |
| 514 | case ERR_R_MALLOC_FAILURE: |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 515 | return symbol ? "MALLOC_FAILURE" : "malloc failure"; |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 516 | case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED: |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 517 | return symbol ? "SHOULD_NOT_HAVE_BEEN_CALLED" |
| 518 | : "function should not have been called"; |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 519 | case ERR_R_PASSED_NULL_PARAMETER: |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 520 | return symbol ? "PASSED_NULL_PARAMETER" : "passed a null parameter"; |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 521 | case ERR_R_INTERNAL_ERROR: |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 522 | return symbol ? "INTERNAL_ERROR" : "internal error"; |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 523 | case ERR_R_OVERFLOW: |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 524 | return symbol ? "OVERFLOW" : "overflow"; |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 525 | default: |
| 526 | return NULL; |
| 527 | } |
| 528 | } |
| 529 | |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 530 | // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we |
| 531 | // do not need to check |symbol|. |
David Benjamin | d27eda0 | 2015-03-05 01:19:27 -0500 | [diff] [blame] | 532 | return err_string_lookup(lib, reason, kOpenSSLReasonValues, |
| 533 | kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 534 | } |
| 535 | |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 536 | const char *ERR_reason_error_string(uint32_t packed_error) { |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 537 | const char *ret = err_reason_error_string(packed_error, /*symbol=*/0); |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 538 | return ret == NULL ? "unknown error" : ret; |
| 539 | } |
| 540 | |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 541 | const char *ERR_reason_symbol_name(uint32_t packed_error) { |
| 542 | return err_reason_error_string(packed_error, /*symbol=*/1); |
| 543 | } |
| 544 | |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 545 | char *ERR_error_string(uint32_t packed_error, char *ret) { |
| 546 | static char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 547 | |
| 548 | if (ret == NULL) { |
| 549 | // TODO(fork): remove this. |
| 550 | ret = buf; |
| 551 | } |
| 552 | |
| 553 | #if !defined(NDEBUG) |
| 554 | // This is aimed to help catch callers who don't provide |
| 555 | // |ERR_ERROR_STRING_BUF_LEN| bytes of space. |
| 556 | OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); |
| 557 | #endif |
| 558 | |
| 559 | return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN); |
| 560 | } |
| 561 | |
| 562 | char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { |
| 563 | if (len == 0) { |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | unsigned lib = ERR_GET_LIB(packed_error); |
| 568 | unsigned reason = ERR_GET_REASON(packed_error); |
| 569 | |
| 570 | const char *lib_str = err_lib_error_string(packed_error); |
David Benjamin | dbad745 | 2024-02-20 15:06:57 -0500 | [diff] [blame^] | 571 | const char *reason_str = err_reason_error_string(packed_error, /*symbol=*/0); |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 572 | |
David Benjamin | 6bd1e15 | 2023-07-19 13:56:50 -0400 | [diff] [blame] | 573 | char lib_buf[32], reason_buf[32]; |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 574 | if (lib_str == NULL) { |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 575 | snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib); |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 576 | lib_str = lib_buf; |
| 577 | } |
| 578 | |
David Benjamin | 6bd1e15 | 2023-07-19 13:56:50 -0400 | [diff] [blame] | 579 | if (reason_str == NULL) { |
| 580 | snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason); |
| 581 | reason_str = reason_buf; |
| 582 | } |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 583 | |
David Benjamin | 6bd1e15 | 2023-07-19 13:56:50 -0400 | [diff] [blame] | 584 | int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", |
| 585 | packed_error, lib_str, reason_str); |
| 586 | if (ret >= 0 && (size_t)ret >= len) { |
| 587 | // The output was truncated; make sure we always have 5 colon-separated |
David Benjamin | eec7f32 | 2021-04-21 14:49:45 -0400 | [diff] [blame] | 588 | // fields, i.e. 4 colons. |
| 589 | static const unsigned num_colons = 4; |
| 590 | unsigned i; |
| 591 | char *s = buf; |
| 592 | |
| 593 | if (len <= num_colons) { |
| 594 | // In this situation it's not possible to ensure that the correct number |
| 595 | // of colons are included in the output. |
| 596 | return buf; |
| 597 | } |
| 598 | |
| 599 | for (i = 0; i < num_colons; i++) { |
| 600 | char *colon = strchr(s, ':'); |
| 601 | char *last_pos = &buf[len - 1] - num_colons + i; |
| 602 | |
| 603 | if (colon == NULL || colon > last_pos) { |
| 604 | // set colon |i| at last possible position (buf[len-1] is the |
| 605 | // terminating 0). If we're setting this colon, then all whole of the |
| 606 | // rest of the string must be colons in order to have the correct |
| 607 | // number. |
| 608 | OPENSSL_memset(last_pos, ':', num_colons - i); |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | s = colon + 1; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return buf; |
| 617 | } |
| 618 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 619 | void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 620 | char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 621 | char buf2[1024]; |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 622 | const char *file, *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | int line, flags; |
| 624 | uint32_t packed_error; |
| 625 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 626 | // thread_hash is the least-significant bits of the |ERR_STATE| pointer value |
| 627 | // for this thread. |
Adam Langley | 3367273 | 2015-03-31 18:55:53 -0700 | [diff] [blame] | 628 | const unsigned long thread_hash = (uintptr_t) err_get_state(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 629 | |
| 630 | for (;;) { |
| 631 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 632 | if (packed_error == 0) { |
| 633 | break; |
| 634 | } |
| 635 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 636 | ERR_error_string_n(packed_error, buf, sizeof(buf)); |
David Benjamin | 23d6e4c | 2023-07-15 02:44:54 -0400 | [diff] [blame] | 637 | snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file, |
| 638 | line, (flags & ERR_FLAG_STRING) ? data : ""); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 639 | if (callback(buf2, strlen(buf2), ctx) <= 0) { |
| 640 | break; |
| 641 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 645 | static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) { |
| 646 | assert(msg[msg_len] == '\0'); |
| 647 | FILE* fp = ctx; |
| 648 | int res = fputs(msg, fp); |
| 649 | return res < 0 ? 0 : 1; |
| 650 | } |
| 651 | |
| 652 | void ERR_print_errors_fp(FILE *file) { |
| 653 | ERR_print_errors_cb(print_errors_to_file, file); |
| 654 | } |
| 655 | |
David Benjamin | e1c3dad | 2017-09-28 16:33:09 -0400 | [diff] [blame] | 656 | // err_set_error_data sets the data on the most recent error. |
| 657 | static void err_set_error_data(char *data) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 658 | ERR_STATE *const state = err_get_state(); |
| 659 | struct err_error_st *error; |
| 660 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 661 | if (state == NULL || state->top == state->bottom) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 662 | free(data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 663 | return; |
| 664 | } |
| 665 | |
| 666 | error = &state->errors[state->top]; |
| 667 | |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 668 | free(error->data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 669 | error->data = data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 670 | } |
| 671 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 672 | void ERR_put_error(int library, int unused, int reason, const char *file, |
| 673 | unsigned line) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | ERR_STATE *const state = err_get_state(); |
| 675 | struct err_error_st *error; |
| 676 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 677 | if (state == NULL) { |
| 678 | return; |
| 679 | } |
| 680 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 681 | if (library == ERR_LIB_SYS && reason == 0) { |
Brian Smith | 8a36e53 | 2015-07-31 16:11:31 -0400 | [diff] [blame] | 682 | #if defined(OPENSSL_WINDOWS) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 683 | reason = GetLastError(); |
| 684 | #else |
| 685 | reason = errno; |
| 686 | #endif |
| 687 | } |
| 688 | |
| 689 | state->top = (state->top + 1) % ERR_NUM_ERRORS; |
| 690 | if (state->top == state->bottom) { |
| 691 | state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 692 | } |
| 693 | |
| 694 | error = &state->errors[state->top]; |
| 695 | err_clear(error); |
| 696 | error->file = file; |
| 697 | error->line = line; |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 698 | error->packed = ERR_PACK(library, reason); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 699 | } |
| 700 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 701 | // ERR_add_error_data_vdata takes a variable number of const char* pointers, |
| 702 | // concatenates them and sets the result as the data on the most recent |
| 703 | // error. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 704 | static void err_add_error_vdata(unsigned num, va_list args) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 705 | size_t total_size = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 706 | const char *substr; |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 707 | char *buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 708 | |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 709 | va_list args_copy; |
| 710 | va_copy(args_copy, args); |
| 711 | for (size_t i = 0; i < num; i++) { |
| 712 | substr = va_arg(args_copy, const char *); |
| 713 | if (substr == NULL) { |
| 714 | continue; |
| 715 | } |
| 716 | size_t substr_len = strlen(substr); |
| 717 | if (SIZE_MAX - total_size < substr_len) { |
| 718 | return; // Would overflow. |
| 719 | } |
| 720 | total_size += substr_len; |
| 721 | } |
| 722 | va_end(args_copy); |
| 723 | if (total_size == SIZE_MAX) { |
| 724 | return; // Would overflow. |
| 725 | } |
| 726 | total_size += 1; // NUL terminator. |
| 727 | if ((buf = malloc(total_size)) == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | return; |
| 729 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 730 | buf[0] = '\0'; |
| 731 | for (size_t i = 0; i < num; i++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | substr = va_arg(args, const char *); |
| 733 | if (substr == NULL) { |
| 734 | continue; |
| 735 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 736 | if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) { |
| 737 | assert(0); // should not be possible. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 738 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 740 | va_end(args); |
David Benjamin | e1c3dad | 2017-09-28 16:33:09 -0400 | [diff] [blame] | 741 | err_set_error_data(buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void ERR_add_error_data(unsigned count, ...) { |
| 745 | va_list args; |
| 746 | va_start(args, count); |
| 747 | err_add_error_vdata(count, args); |
| 748 | va_end(args); |
| 749 | } |
| 750 | |
| 751 | void ERR_add_error_dataf(const char *format, ...) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 752 | char *buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 753 | va_list ap; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 754 | |
| 755 | va_start(ap, format); |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 756 | if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) { |
| 757 | return; |
| 758 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | va_end(ap); |
| 760 | |
David Benjamin | e1c3dad | 2017-09-28 16:33:09 -0400 | [diff] [blame] | 761 | err_set_error_data(buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 762 | } |
| 763 | |
David Benjamin | 731d6cb | 2021-11-30 15:32:25 -0500 | [diff] [blame] | 764 | void ERR_set_error_data(char *data, int flags) { |
| 765 | if (!(flags & ERR_FLAG_STRING)) { |
| 766 | // We do not support non-string error data. |
| 767 | assert(0); |
| 768 | return; |
| 769 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 770 | // Disable deprecated functions on msvc so it doesn't complain about strdup. |
| 771 | OPENSSL_MSVC_PRAGMA(warning(push)) |
| 772 | OPENSSL_MSVC_PRAGMA(warning(disable : 4996)) |
| 773 | // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc, |
| 774 | // which can affect the error stack. |
| 775 | char *copy = strdup(data); |
| 776 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 777 | if (copy != NULL) { |
| 778 | err_set_error_data(copy); |
| 779 | } |
David Benjamin | 731d6cb | 2021-11-30 15:32:25 -0500 | [diff] [blame] | 780 | if (flags & ERR_FLAG_MALLOCED) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 781 | // We can not take ownership of |data| directly because it is allocated with |
| 782 | // |OPENSSL_malloc| and we will free it with system |free| later. |
| 783 | OPENSSL_free(data); |
David Benjamin | 731d6cb | 2021-11-30 15:32:25 -0500 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 787 | int ERR_set_mark(void) { |
| 788 | ERR_STATE *const state = err_get_state(); |
| 789 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 790 | if (state == NULL || state->bottom == state->top) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 791 | return 0; |
| 792 | } |
David Benjamin | a65c252 | 2017-10-03 17:34:37 -0400 | [diff] [blame] | 793 | state->errors[state->top].mark = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | return 1; |
| 795 | } |
| 796 | |
| 797 | int ERR_pop_to_mark(void) { |
| 798 | ERR_STATE *const state = err_get_state(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 799 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 800 | if (state == NULL) { |
| 801 | return 0; |
| 802 | } |
| 803 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 804 | while (state->bottom != state->top) { |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 805 | struct err_error_st *error = &state->errors[state->top]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
David Benjamin | a65c252 | 2017-10-03 17:34:37 -0400 | [diff] [blame] | 807 | if (error->mark) { |
| 808 | error->mark = 0; |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 809 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | err_clear(error); |
| 813 | if (state->top == 0) { |
| 814 | state->top = ERR_NUM_ERRORS - 1; |
| 815 | } else { |
| 816 | state->top--; |
| 817 | } |
| 818 | } |
| 819 | |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 820 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 823 | void ERR_load_crypto_strings(void) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 824 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 825 | void ERR_free_strings(void) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 826 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 827 | void ERR_load_BIO_strings(void) {} |
Matt Braithwaite | 9febf19 | 2015-04-30 13:04:06 -0700 | [diff] [blame] | 828 | |
| 829 | void ERR_load_ERR_strings(void) {} |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 830 | |
David Benjamin | 5d626b2 | 2018-05-08 16:07:00 -0400 | [diff] [blame] | 831 | void ERR_load_RAND_strings(void) {} |
| 832 | |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 833 | struct err_save_state_st { |
| 834 | struct err_error_st *errors; |
| 835 | size_t num_errors; |
| 836 | }; |
| 837 | |
| 838 | void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) { |
| 839 | if (state == NULL) { |
| 840 | return; |
| 841 | } |
| 842 | for (size_t i = 0; i < state->num_errors; i++) { |
| 843 | err_clear(&state->errors[i]); |
| 844 | } |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 845 | free(state->errors); |
| 846 | free(state); |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | ERR_SAVE_STATE *ERR_save_state(void) { |
| 850 | ERR_STATE *const state = err_get_state(); |
| 851 | if (state == NULL || state->top == state->bottom) { |
| 852 | return NULL; |
| 853 | } |
| 854 | |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 855 | ERR_SAVE_STATE *ret = malloc(sizeof(ERR_SAVE_STATE)); |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 856 | if (ret == NULL) { |
| 857 | return NULL; |
| 858 | } |
| 859 | |
| 860 | // Errors are stored in the range (bottom, top]. |
| 861 | size_t num_errors = state->top >= state->bottom |
| 862 | ? state->top - state->bottom |
| 863 | : ERR_NUM_ERRORS + state->top - state->bottom; |
| 864 | assert(num_errors < ERR_NUM_ERRORS); |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 865 | ret->errors = malloc(num_errors * sizeof(struct err_error_st)); |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 866 | if (ret->errors == NULL) { |
Bob Beck | fc524c1 | 2023-02-07 14:32:41 -0700 | [diff] [blame] | 867 | free(ret); |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 868 | return NULL; |
| 869 | } |
| 870 | OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st)); |
| 871 | ret->num_errors = num_errors; |
| 872 | |
| 873 | for (size_t i = 0; i < num_errors; i++) { |
| 874 | size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS; |
| 875 | err_copy(&ret->errors[i], &state->errors[j]); |
| 876 | } |
| 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | void ERR_restore_state(const ERR_SAVE_STATE *state) { |
| 881 | if (state == NULL || state->num_errors == 0) { |
| 882 | ERR_clear_error(); |
| 883 | return; |
| 884 | } |
| 885 | |
David Benjamin | 0f222e6 | 2023-07-03 12:24:51 -0400 | [diff] [blame] | 886 | if (state->num_errors >= ERR_NUM_ERRORS) { |
| 887 | abort(); |
| 888 | } |
| 889 | |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 890 | ERR_STATE *const dst = err_get_state(); |
| 891 | if (dst == NULL) { |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | for (size_t i = 0; i < state->num_errors; i++) { |
| 896 | err_copy(&dst->errors[i], &state->errors[i]); |
| 897 | } |
David Benjamin | 0f222e6 | 2023-07-03 12:24:51 -0400 | [diff] [blame] | 898 | dst->top = (unsigned)(state->num_errors - 1); |
David Benjamin | b25a899 | 2017-10-01 21:47:07 -0400 | [diff] [blame] | 899 | dst->bottom = ERR_NUM_ERRORS - 1; |
| 900 | } |