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 | |
| 109 | #include <openssl/err.h> |
| 110 | |
| 111 | #include <assert.h> |
| 112 | #include <errno.h> |
| 113 | #include <inttypes.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 114 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | |
Yoshisato Yanagisawa | b7725cf | 2014-08-18 11:39:09 -0700 | [diff] [blame] | 116 | #if defined(OPENSSL_WINDOWS) |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 117 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 118 | #include <windows.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 119 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Yoshisato Yanagisawa | b7725cf | 2014-08-18 11:39:09 -0700 | [diff] [blame] | 120 | #endif |
| 121 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | #include <openssl/mem.h> |
| 123 | #include <openssl/thread.h> |
| 124 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 125 | #include "../internal.h" |
| 126 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 127 | |
David Benjamin | d27eda0 | 2015-03-05 01:19:27 -0500 | [diff] [blame] | 128 | extern const uint32_t kOpenSSLReasonValues[]; |
| 129 | extern const size_t kOpenSSLReasonValuesLen; |
| 130 | extern const char kOpenSSLReasonStringData[]; |
| 131 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 132 | // err_clear_data frees the optional |data| member of the given error. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | static void err_clear_data(struct err_error_st *error) { |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 134 | if ((error->flags & ERR_FLAG_MALLOCED) != 0) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 135 | OPENSSL_free(error->data); |
| 136 | } |
| 137 | error->data = NULL; |
| 138 | error->flags &= ~ERR_FLAG_MALLOCED; |
| 139 | } |
| 140 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 141 | // err_clear clears the given queued error. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | static void err_clear(struct err_error_st *error) { |
| 143 | err_clear_data(error); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 144 | OPENSSL_memset(error, 0, sizeof(struct err_error_st)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | } |
| 146 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 147 | // global_next_library contains the next custom library value to return. |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 148 | static int global_next_library = ERR_NUM_LIBS; |
| 149 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 150 | // global_next_library_mutex protects |global_next_library| from concurrent |
| 151 | // updates. |
Adam Langley | 8bdd542 | 2015-04-13 11:04:09 -0700 | [diff] [blame] | 152 | static struct CRYPTO_STATIC_MUTEX global_next_library_mutex = |
| 153 | CRYPTO_STATIC_MUTEX_INIT; |
| 154 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 155 | static void err_state_free(void *statep) { |
| 156 | ERR_STATE *state = statep; |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 157 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 158 | if (state == NULL) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | unsigned i; |
| 163 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
| 164 | err_clear(&state->errors[i]); |
| 165 | } |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 166 | OPENSSL_free(state->to_free); |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 167 | OPENSSL_free(state); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 168 | } |
| 169 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 170 | // err_get_state gets the ERR_STATE object for the current thread. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | static ERR_STATE *err_get_state(void) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 172 | ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 173 | if (state == NULL) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 174 | state = OPENSSL_malloc(sizeof(ERR_STATE)); |
| 175 | if (state == NULL) { |
| 176 | return NULL; |
| 177 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 178 | OPENSSL_memset(state, 0, sizeof(ERR_STATE)); |
Adam Langley | 8bdd542 | 2015-04-13 11:04:09 -0700 | [diff] [blame] | 179 | if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state, |
| 180 | err_state_free)) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 185 | return state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | 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] | 189 | const char **data, int *flags) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | unsigned i = 0; |
| 191 | ERR_STATE *state; |
| 192 | struct err_error_st *error; |
| 193 | uint32_t ret; |
| 194 | |
| 195 | state = err_get_state(); |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 196 | if (state == NULL || state->bottom == state->top) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | if (top) { |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 201 | assert(!inc); |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 202 | // last error |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | i = state->top; |
| 204 | } else { |
| 205 | i = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 206 | } |
| 207 | |
| 208 | error = &state->errors[i]; |
| 209 | ret = error->packed; |
| 210 | |
| 211 | if (file != NULL && line != NULL) { |
| 212 | if (error->file == NULL) { |
| 213 | *file = "NA"; |
| 214 | *line = 0; |
| 215 | } else { |
| 216 | *file = error->file; |
| 217 | *line = error->line; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (data != NULL) { |
| 222 | if (error->data == NULL) { |
| 223 | *data = ""; |
| 224 | if (flags != NULL) { |
| 225 | *flags = 0; |
| 226 | } |
| 227 | } else { |
| 228 | *data = error->data; |
| 229 | if (flags != NULL) { |
| 230 | *flags = error->flags & ERR_FLAG_PUBLIC_MASK; |
| 231 | } |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 232 | // If this error is being removed, take ownership of data from |
| 233 | // the error. The semantics are such that the caller doesn't |
| 234 | // take ownership either. Instead the error system takes |
| 235 | // ownership and retains it until the next call that affects the |
| 236 | // error queue. |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 237 | if (inc) { |
| 238 | if (error->flags & ERR_FLAG_MALLOCED) { |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 239 | OPENSSL_free(state->to_free); |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 240 | state->to_free = error->data; |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 241 | } |
David Benjamin | 0d82482 | 2014-11-08 12:51:36 -0500 | [diff] [blame] | 242 | error->data = NULL; |
| 243 | error->flags = 0; |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | if (inc) { |
| 249 | assert(!top); |
| 250 | err_clear(error); |
| 251 | state->bottom = i; |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | uint32_t ERR_get_error(void) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 258 | return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | uint32_t ERR_get_error_line(const char **file, int *line) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 262 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | uint32_t ERR_get_error_line_data(const char **file, int *line, |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 266 | const char **data, int *flags) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 267 | return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | uint32_t ERR_peek_error(void) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 271 | return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | uint32_t ERR_peek_error_line(const char **file, int *line) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 275 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | uint32_t ERR_peek_error_line_data(const char **file, int *line, |
| 279 | const char **data, int *flags) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 280 | return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data, |
| 281 | flags); |
| 282 | } |
| 283 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | uint32_t ERR_peek_last_error(void) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 285 | return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | uint32_t ERR_peek_last_error_line(const char **file, int *line) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 289 | return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | uint32_t ERR_peek_last_error_line_data(const char **file, int *line, |
| 293 | const char **data, int *flags) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 294 | return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void ERR_clear_error(void) { |
| 298 | ERR_STATE *const state = err_get_state(); |
| 299 | unsigned i; |
| 300 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 301 | if (state == NULL) { |
| 302 | return; |
| 303 | } |
| 304 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
| 306 | err_clear(&state->errors[i]); |
| 307 | } |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 308 | OPENSSL_free(state->to_free); |
| 309 | state->to_free = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
| 311 | state->top = state->bottom = 0; |
| 312 | } |
| 313 | |
Adam Langley | 03d31ed | 2014-08-05 16:54:47 -0700 | [diff] [blame] | 314 | void ERR_remove_thread_state(const CRYPTO_THREADID *tid) { |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 315 | if (tid != NULL) { |
| 316 | assert(0); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 320 | ERR_clear_error(); |
Adam Langley | 03d31ed | 2014-08-05 16:54:47 -0700 | [diff] [blame] | 321 | } |
| 322 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 323 | int ERR_get_next_error_library(void) { |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 324 | int ret; |
| 325 | |
Adam Langley | 8bdd542 | 2015-04-13 11:04:09 -0700 | [diff] [blame] | 326 | CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 327 | ret = global_next_library++; |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 328 | CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex); |
Adam Langley | 2e0f071 | 2015-03-31 16:04:09 -0700 | [diff] [blame] | 329 | |
| 330 | return ret; |
Adam Langley | 4042667 | 2014-07-01 16:48:28 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Matt Braithwaite | 9b68e72 | 2015-05-06 13:57:29 -0700 | [diff] [blame] | 333 | void ERR_remove_state(unsigned long pid) { |
| 334 | ERR_clear_error(); |
| 335 | } |
| 336 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | void ERR_clear_system_error(void) { |
| 338 | errno = 0; |
| 339 | } |
| 340 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 341 | char *ERR_error_string(uint32_t packed_error, char *ret) { |
| 342 | static char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 343 | |
| 344 | if (ret == NULL) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 345 | // TODO(fork): remove this. |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 346 | ret = buf; |
| 347 | } |
| 348 | |
| 349 | #if !defined(NDEBUG) |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 350 | // This is aimed to help catch callers who don't provide |
| 351 | // |ERR_ERROR_STRING_BUF_LEN| bytes of space. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 352 | OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN); |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 353 | #endif |
| 354 | |
| 355 | ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN); |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 361 | char lib_buf[64], reason_buf[64]; |
| 362 | const char *lib_str, *reason_str; |
| 363 | unsigned lib, reason; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | |
| 365 | if (len == 0) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | lib = ERR_GET_LIB(packed_error); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 370 | reason = ERR_GET_REASON(packed_error); |
| 371 | |
| 372 | lib_str = ERR_lib_error_string(packed_error); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | reason_str = ERR_reason_error_string(packed_error); |
| 374 | |
| 375 | if (lib_str == NULL) { |
| 376 | BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib); |
| 377 | lib_str = lib_buf; |
| 378 | } |
| 379 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 380 | if (reason_str == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason); |
| 382 | reason_str = reason_buf; |
| 383 | } |
| 384 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 385 | BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s", |
| 386 | packed_error, lib_str, reason_str); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | |
| 388 | if (strlen(buf) == len - 1) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 389 | // output may be truncated; make sure we always have 5 colon-separated |
| 390 | // fields, i.e. 4 colons. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | static const unsigned num_colons = 4; |
| 392 | unsigned i; |
| 393 | char *s = buf; |
| 394 | |
| 395 | if (len <= num_colons) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 396 | // In this situation it's not possible to ensure that the correct number |
| 397 | // of colons are included in the output. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | |
| 401 | for (i = 0; i < num_colons; i++) { |
| 402 | char *colon = strchr(s, ':'); |
| 403 | char *last_pos = &buf[len - 1] - num_colons + i; |
| 404 | |
| 405 | if (colon == NULL || colon > last_pos) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 406 | // set colon |i| at last possible position (buf[len-1] is the |
| 407 | // terminating 0). If we're setting this colon, then all whole of the |
| 408 | // rest of the string must be colons in order to have the correct |
| 409 | // number. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 410 | OPENSSL_memset(last_pos, ':', num_colons - i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | s = colon + 1; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 419 | // err_string_cmp is a compare function for searching error values with |
| 420 | // |bsearch| in |err_string_lookup|. |
| 421 | static int err_string_cmp(const void *a, const void *b) { |
| 422 | const uint32_t a_key = *((const uint32_t*) a) >> 15; |
| 423 | const uint32_t b_key = *((const uint32_t*) b) >> 15; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 424 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 425 | if (a_key < b_key) { |
| 426 | return -1; |
| 427 | } else if (a_key > b_key) { |
| 428 | return 1; |
| 429 | } else { |
| 430 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | } |
| 433 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 434 | // err_string_lookup looks up the string associated with |lib| and |key| in |
| 435 | // |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] | 436 | static const char *err_string_lookup(uint32_t lib, uint32_t key, |
| 437 | const uint32_t *values, |
| 438 | size_t num_values, |
| 439 | const char *string_data) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 440 | // |values| points to data in err_data.h, which is generated by |
| 441 | // err_data_generate.go. It's an array of uint32_t values. Each value has the |
| 442 | // following structure: |
| 443 | // | lib | key | offset | |
| 444 | // |6 bits| 11 bits | 15 bits | |
| 445 | // |
| 446 | // The |lib| value is a library identifier: one of the |ERR_LIB_*| values. |
| 447 | // The |key| is a reason code, depending on the context. |
| 448 | // The |offset| is the number of bytes from the start of |string_data| where |
| 449 | // the (NUL terminated) string for this value can be found. |
| 450 | // |
| 451 | // Values are sorted based on treating the |lib| and |key| part as an |
| 452 | // unsigned integer. |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 453 | if (lib >= (1 << 6) || key >= (1 << 11)) { |
| 454 | return NULL; |
| 455 | } |
| 456 | uint32_t search_key = lib << 26 | key << 15; |
| 457 | const uint32_t *result = bsearch(&search_key, values, num_values, |
| 458 | sizeof(uint32_t), err_string_cmp); |
| 459 | if (result == NULL) { |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | return &string_data[(*result) & 0x7fff]; |
| 464 | } |
| 465 | |
| 466 | static const char *const kLibraryNames[ERR_NUM_LIBS] = { |
| 467 | "invalid library (0)", |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 468 | "unknown library", // ERR_LIB_NONE |
| 469 | "system library", // ERR_LIB_SYS |
| 470 | "bignum routines", // ERR_LIB_BN |
| 471 | "RSA routines", // ERR_LIB_RSA |
| 472 | "Diffie-Hellman routines", // ERR_LIB_DH |
| 473 | "public key routines", // ERR_LIB_EVP |
| 474 | "memory buffer routines", // ERR_LIB_BUF |
| 475 | "object identifier routines", // ERR_LIB_OBJ |
| 476 | "PEM routines", // ERR_LIB_PEM |
| 477 | "DSA routines", // ERR_LIB_DSA |
| 478 | "X.509 certificate routines", // ERR_LIB_X509 |
| 479 | "ASN.1 encoding routines", // ERR_LIB_ASN1 |
| 480 | "configuration file routines", // ERR_LIB_CONF |
| 481 | "common libcrypto routines", // ERR_LIB_CRYPTO |
| 482 | "elliptic curve routines", // ERR_LIB_EC |
| 483 | "SSL routines", // ERR_LIB_SSL |
| 484 | "BIO routines", // ERR_LIB_BIO |
| 485 | "PKCS7 routines", // ERR_LIB_PKCS7 |
| 486 | "PKCS8 routines", // ERR_LIB_PKCS8 |
| 487 | "X509 V3 routines", // ERR_LIB_X509V3 |
| 488 | "random number generator", // ERR_LIB_RAND |
| 489 | "ENGINE routines", // ERR_LIB_ENGINE |
| 490 | "OCSP routines", // ERR_LIB_OCSP |
| 491 | "UI routines", // ERR_LIB_UI |
| 492 | "COMP routines", // ERR_LIB_COMP |
| 493 | "ECDSA routines", // ERR_LIB_ECDSA |
| 494 | "ECDH routines", // ERR_LIB_ECDH |
| 495 | "HMAC routines", // ERR_LIB_HMAC |
| 496 | "Digest functions", // ERR_LIB_DIGEST |
| 497 | "Cipher functions", // ERR_LIB_CIPHER |
| 498 | "HKDF functions", // ERR_LIB_HKDF |
| 499 | "User defined functions", // ERR_LIB_USER |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | const char *ERR_lib_error_string(uint32_t packed_error) { |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 503 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 504 | |
| 505 | if (lib >= ERR_NUM_LIBS) { |
| 506 | return NULL; |
| 507 | } |
| 508 | return kLibraryNames[lib]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | const char *ERR_func_error_string(uint32_t packed_error) { |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 512 | return "OPENSSL_internal"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | const char *ERR_reason_error_string(uint32_t packed_error) { |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 516 | const uint32_t lib = ERR_GET_LIB(packed_error); |
| 517 | const uint32_t reason = ERR_GET_REASON(packed_error); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 519 | if (lib == ERR_LIB_SYS) { |
| 520 | if (reason < 127) { |
| 521 | return strerror(reason); |
| 522 | } |
| 523 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 526 | if (reason < ERR_NUM_LIBS) { |
| 527 | return kLibraryNames[reason]; |
| 528 | } |
| 529 | |
| 530 | if (reason < 100) { |
| 531 | switch (reason) { |
| 532 | case ERR_R_MALLOC_FAILURE: |
| 533 | return "malloc failure"; |
| 534 | case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED: |
| 535 | return "function should not have been called"; |
| 536 | case ERR_R_PASSED_NULL_PARAMETER: |
| 537 | return "passed a null parameter"; |
| 538 | case ERR_R_INTERNAL_ERROR: |
| 539 | return "internal error"; |
| 540 | case ERR_R_OVERFLOW: |
| 541 | return "overflow"; |
| 542 | default: |
| 543 | return NULL; |
| 544 | } |
| 545 | } |
| 546 | |
David Benjamin | d27eda0 | 2015-03-05 01:19:27 -0500 | [diff] [blame] | 547 | return err_string_lookup(lib, reason, kOpenSSLReasonValues, |
| 548 | kOpenSSLReasonValuesLen, kOpenSSLReasonStringData); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | 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] | 552 | char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 553 | char buf2[1024]; |
Adam Langley | 5f1374e | 2014-10-01 15:21:01 -0700 | [diff] [blame] | 554 | const char *file, *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | int line, flags; |
| 556 | uint32_t packed_error; |
| 557 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 558 | // thread_hash is the least-significant bits of the |ERR_STATE| pointer value |
| 559 | // for this thread. |
Adam Langley | 3367273 | 2015-03-31 18:55:53 -0700 | [diff] [blame] | 560 | const unsigned long thread_hash = (uintptr_t) err_get_state(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 561 | |
| 562 | for (;;) { |
| 563 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 564 | if (packed_error == 0) { |
| 565 | break; |
| 566 | } |
| 567 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 568 | ERR_error_string_n(packed_error, buf, sizeof(buf)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, |
| 570 | file, line, (flags & ERR_FLAG_STRING) ? data : ""); |
| 571 | if (callback(buf2, strlen(buf2), ctx) <= 0) { |
| 572 | break; |
| 573 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 577 | static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) { |
| 578 | assert(msg[msg_len] == '\0'); |
| 579 | FILE* fp = ctx; |
| 580 | int res = fputs(msg, fp); |
| 581 | return res < 0 ? 0 : 1; |
| 582 | } |
| 583 | |
| 584 | void ERR_print_errors_fp(FILE *file) { |
| 585 | ERR_print_errors_cb(print_errors_to_file, file); |
| 586 | } |
| 587 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 588 | // err_set_error_data sets the data on the most recent error. The |flags| |
| 589 | // argument is a combination of the |ERR_FLAG_*| values. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | static void err_set_error_data(char *data, int flags) { |
| 591 | ERR_STATE *const state = err_get_state(); |
| 592 | struct err_error_st *error; |
| 593 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 594 | if (state == NULL || state->top == state->bottom) { |
David Benjamin | 4aa86f1 | 2014-11-02 20:47:17 -0500 | [diff] [blame] | 595 | if (flags & ERR_FLAG_MALLOCED) { |
| 596 | OPENSSL_free(data); |
| 597 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | |
| 601 | error = &state->errors[state->top]; |
| 602 | |
| 603 | err_clear_data(error); |
| 604 | error->data = data; |
| 605 | error->flags = flags; |
| 606 | } |
| 607 | |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 608 | void ERR_put_error(int library, int unused, int reason, const char *file, |
| 609 | unsigned line) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 610 | ERR_STATE *const state = err_get_state(); |
| 611 | struct err_error_st *error; |
| 612 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 613 | if (state == NULL) { |
| 614 | return; |
| 615 | } |
| 616 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 617 | if (library == ERR_LIB_SYS && reason == 0) { |
Brian Smith | 8a36e53 | 2015-07-31 16:11:31 -0400 | [diff] [blame] | 618 | #if defined(OPENSSL_WINDOWS) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 619 | reason = GetLastError(); |
| 620 | #else |
| 621 | reason = errno; |
| 622 | #endif |
| 623 | } |
| 624 | |
| 625 | state->top = (state->top + 1) % ERR_NUM_ERRORS; |
| 626 | if (state->top == state->bottom) { |
| 627 | state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS; |
| 628 | } |
| 629 | |
| 630 | error = &state->errors[state->top]; |
| 631 | err_clear(error); |
| 632 | error->file = file; |
| 633 | error->line = line; |
David Benjamin | 34248d4 | 2015-06-28 23:36:21 -0400 | [diff] [blame] | 634 | error->packed = ERR_PACK(library, reason); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | } |
| 636 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 637 | // ERR_add_error_data_vdata takes a variable number of const char* pointers, |
| 638 | // concatenates them and sets the result as the data on the most recent |
| 639 | // error. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 640 | static void err_add_error_vdata(unsigned num, va_list args) { |
| 641 | size_t alloced, new_len, len = 0, substr_len; |
| 642 | char *buf; |
| 643 | const char *substr; |
| 644 | unsigned i; |
| 645 | |
| 646 | alloced = 80; |
| 647 | buf = OPENSSL_malloc(alloced + 1); |
| 648 | if (buf == NULL) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | for (i = 0; i < num; i++) { |
| 653 | substr = va_arg(args, const char *); |
| 654 | if (substr == NULL) { |
| 655 | continue; |
| 656 | } |
| 657 | |
| 658 | substr_len = strlen(substr); |
| 659 | new_len = len + substr_len; |
| 660 | if (new_len > alloced) { |
| 661 | char *new_buf; |
| 662 | |
| 663 | if (alloced + 20 + 1 < alloced) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 664 | // overflow. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 665 | OPENSSL_free(buf); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | alloced = new_len + 20; |
| 670 | new_buf = OPENSSL_realloc(buf, alloced + 1); |
| 671 | if (new_buf == NULL) { |
| 672 | OPENSSL_free(buf); |
| 673 | return; |
| 674 | } |
| 675 | buf = new_buf; |
| 676 | } |
| 677 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 678 | OPENSSL_memcpy(buf + len, substr, substr_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 679 | len = new_len; |
| 680 | } |
| 681 | |
| 682 | buf[len] = 0; |
| 683 | err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING); |
| 684 | } |
| 685 | |
| 686 | void ERR_add_error_data(unsigned count, ...) { |
| 687 | va_list args; |
| 688 | va_start(args, count); |
| 689 | err_add_error_vdata(count, args); |
| 690 | va_end(args); |
| 691 | } |
| 692 | |
| 693 | void ERR_add_error_dataf(const char *format, ...) { |
| 694 | va_list ap; |
| 695 | char *buf; |
| 696 | static const unsigned buf_len = 256; |
| 697 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 698 | // A fixed-size buffer is used because va_copy (which would be needed in |
| 699 | // order to call vsnprintf twice and measure the buffer) wasn't defined until |
| 700 | // C99. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 701 | buf = OPENSSL_malloc(buf_len + 1); |
| 702 | if (buf == NULL) { |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | va_start(ap, format); |
| 707 | BIO_vsnprintf(buf, buf_len, format, ap); |
| 708 | buf[buf_len] = 0; |
| 709 | va_end(ap); |
| 710 | |
| 711 | err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING); |
| 712 | } |
| 713 | |
| 714 | int ERR_set_mark(void) { |
| 715 | ERR_STATE *const state = err_get_state(); |
| 716 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 717 | if (state == NULL || state->bottom == state->top) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | state->errors[state->top].flags |= ERR_FLAG_MARK; |
| 721 | return 1; |
| 722 | } |
| 723 | |
| 724 | int ERR_pop_to_mark(void) { |
| 725 | ERR_STATE *const state = err_get_state(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 726 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 727 | if (state == NULL) { |
| 728 | return 0; |
| 729 | } |
| 730 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | while (state->bottom != state->top) { |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 732 | struct err_error_st *error = &state->errors[state->top]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 733 | |
| 734 | if ((error->flags & ERR_FLAG_MARK) != 0) { |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 735 | error->flags &= ~ERR_FLAG_MARK; |
| 736 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | err_clear(error); |
| 740 | if (state->top == 0) { |
| 741 | state->top = ERR_NUM_ERRORS - 1; |
| 742 | } else { |
| 743 | state->top--; |
| 744 | } |
| 745 | } |
| 746 | |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 747 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Adam Langley | 29b1867 | 2015-02-06 11:52:16 -0800 | [diff] [blame] | 750 | void ERR_load_crypto_strings(void) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 751 | |
Adam Langley | b9e77a0 | 2015-03-31 18:36:20 -0700 | [diff] [blame] | 752 | void ERR_free_strings(void) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 753 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 754 | void ERR_load_BIO_strings(void) {} |
Matt Braithwaite | 9febf19 | 2015-04-30 13:04:06 -0700 | [diff] [blame] | 755 | |
| 756 | void ERR_load_ERR_strings(void) {} |