blob: 2c567cece8f7f0bfc51a9e2d52b668d2b16ed718 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Langley2b2d66d2015-01-30 17:08:37 -0800114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
Yoshisato Yanagisawab7725cf2014-08-18 11:39:09 -0700116#if defined(OPENSSL_WINDOWS)
David Benjamina353cdb2016-06-09 16:48:33 -0400117OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -0700118#include <windows.h>
David Benjamina353cdb2016-06-09 16:48:33 -0400119OPENSSL_MSVC_PRAGMA(warning(pop))
Yoshisato Yanagisawab7725cf2014-08-18 11:39:09 -0700120#endif
121
Adam Langley95c29f32014-06-20 12:00:00 -0700122#include <openssl/mem.h>
123#include <openssl/thread.h>
124
Adam Langleyb9e77a02015-03-31 18:36:20 -0700125#include "../internal.h"
126
Adam Langley95c29f32014-06-20 12:00:00 -0700127
David Benjamind27eda02015-03-05 01:19:27 -0500128extern const uint32_t kOpenSSLReasonValues[];
129extern const size_t kOpenSSLReasonValuesLen;
130extern const char kOpenSSLReasonStringData[];
131
David Benjamin808f8322017-08-18 14:06:02 -0400132// err_clear_data frees the optional |data| member of the given error.
Adam Langley95c29f32014-06-20 12:00:00 -0700133static void err_clear_data(struct err_error_st *error) {
David Benjamincca4ba72015-04-22 15:49:27 -0400134 if ((error->flags & ERR_FLAG_MALLOCED) != 0) {
Adam Langley95c29f32014-06-20 12:00:00 -0700135 OPENSSL_free(error->data);
136 }
137 error->data = NULL;
138 error->flags &= ~ERR_FLAG_MALLOCED;
139}
140
David Benjamin808f8322017-08-18 14:06:02 -0400141// err_clear clears the given queued error.
Adam Langley95c29f32014-06-20 12:00:00 -0700142static void err_clear(struct err_error_st *error) {
143 err_clear_data(error);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500144 OPENSSL_memset(error, 0, sizeof(struct err_error_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700145}
146
David Benjamin808f8322017-08-18 14:06:02 -0400147// global_next_library contains the next custom library value to return.
Adam Langley2e0f0712015-03-31 16:04:09 -0700148static int global_next_library = ERR_NUM_LIBS;
149
David Benjamin808f8322017-08-18 14:06:02 -0400150// global_next_library_mutex protects |global_next_library| from concurrent
151// updates.
Adam Langley8bdd5422015-04-13 11:04:09 -0700152static struct CRYPTO_STATIC_MUTEX global_next_library_mutex =
153 CRYPTO_STATIC_MUTEX_INIT;
154
Adam Langleyb9e77a02015-03-31 18:36:20 -0700155static void err_state_free(void *statep) {
156 ERR_STATE *state = statep;
Adam Langley2e0f0712015-03-31 16:04:09 -0700157
Adam Langleyb9e77a02015-03-31 18:36:20 -0700158 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 Benjamincca4ba72015-04-22 15:49:27 -0400166 OPENSSL_free(state->to_free);
Adam Langleyb9e77a02015-03-31 18:36:20 -0700167 OPENSSL_free(state);
Adam Langley2e0f0712015-03-31 16:04:09 -0700168}
169
David Benjamin808f8322017-08-18 14:06:02 -0400170// err_get_state gets the ERR_STATE object for the current thread.
Adam Langley95c29f32014-06-20 12:00:00 -0700171static ERR_STATE *err_get_state(void) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700172 ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
Adam Langley2e0f0712015-03-31 16:04:09 -0700173 if (state == NULL) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700174 state = OPENSSL_malloc(sizeof(ERR_STATE));
175 if (state == NULL) {
176 return NULL;
177 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500178 OPENSSL_memset(state, 0, sizeof(ERR_STATE));
Adam Langley8bdd5422015-04-13 11:04:09 -0700179 if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
180 err_state_free)) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700181 return NULL;
182 }
Adam Langley2e0f0712015-03-31 16:04:09 -0700183 }
184
Adam Langley2e0f0712015-03-31 16:04:09 -0700185 return state;
Adam Langley95c29f32014-06-20 12:00:00 -0700186}
187
188static uint32_t get_error_values(int inc, int top, const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700189 const char **data, int *flags) {
Adam Langley95c29f32014-06-20 12:00:00 -0700190 unsigned i = 0;
191 ERR_STATE *state;
192 struct err_error_st *error;
193 uint32_t ret;
194
195 state = err_get_state();
Adam Langley69a01602014-11-17 17:26:55 -0800196 if (state == NULL || state->bottom == state->top) {
Adam Langley95c29f32014-06-20 12:00:00 -0700197 return 0;
198 }
199
200 if (top) {
David Benjamin0d824822014-11-08 12:51:36 -0500201 assert(!inc);
David Benjamin808f8322017-08-18 14:06:02 -0400202 // last error
Adam Langley95c29f32014-06-20 12:00:00 -0700203 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 Benjamin808f8322017-08-18 14:06:02 -0400232 // 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 Benjamin0d824822014-11-08 12:51:36 -0500237 if (inc) {
238 if (error->flags & ERR_FLAG_MALLOCED) {
David Benjamincca4ba72015-04-22 15:49:27 -0400239 OPENSSL_free(state->to_free);
David Benjamin0d824822014-11-08 12:51:36 -0500240 state->to_free = error->data;
Adam Langley5f1374e2014-10-01 15:21:01 -0700241 }
David Benjamin0d824822014-11-08 12:51:36 -0500242 error->data = NULL;
243 error->flags = 0;
Adam Langley5f1374e2014-10-01 15:21:01 -0700244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245 }
246 }
247
248 if (inc) {
249 assert(!top);
250 err_clear(error);
251 state->bottom = i;
252 }
253
254 return ret;
255}
256
257uint32_t ERR_get_error(void) {
David Benjamin34248d42015-06-28 23:36:21 -0400258 return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700259}
260
261uint32_t ERR_get_error_line(const char **file, int *line) {
David Benjamin34248d42015-06-28 23:36:21 -0400262 return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700263}
264
265uint32_t ERR_get_error_line_data(const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700266 const char **data, int *flags) {
David Benjamin34248d42015-06-28 23:36:21 -0400267 return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700268}
269
270uint32_t ERR_peek_error(void) {
David Benjamin34248d42015-06-28 23:36:21 -0400271 return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700272}
273
274uint32_t ERR_peek_error_line(const char **file, int *line) {
David Benjamin34248d42015-06-28 23:36:21 -0400275 return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700276}
277
278uint32_t ERR_peek_error_line_data(const char **file, int *line,
279 const char **data, int *flags) {
David Benjamin34248d42015-06-28 23:36:21 -0400280 return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data,
281 flags);
282}
283
Adam Langley95c29f32014-06-20 12:00:00 -0700284uint32_t ERR_peek_last_error(void) {
David Benjamin34248d42015-06-28 23:36:21 -0400285 return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700286}
287
288uint32_t ERR_peek_last_error_line(const char **file, int *line) {
David Benjamin34248d42015-06-28 23:36:21 -0400289 return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700290}
291
292uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
293 const char **data, int *flags) {
David Benjamin34248d42015-06-28 23:36:21 -0400294 return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700295}
296
297void ERR_clear_error(void) {
298 ERR_STATE *const state = err_get_state();
299 unsigned i;
300
Adam Langley69a01602014-11-17 17:26:55 -0800301 if (state == NULL) {
302 return;
303 }
304
Adam Langley95c29f32014-06-20 12:00:00 -0700305 for (i = 0; i < ERR_NUM_ERRORS; i++) {
306 err_clear(&state->errors[i]);
307 }
David Benjamincca4ba72015-04-22 15:49:27 -0400308 OPENSSL_free(state->to_free);
309 state->to_free = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700310
311 state->top = state->bottom = 0;
312}
313
Adam Langley03d31ed2014-08-05 16:54:47 -0700314void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700315 if (tid != NULL) {
316 assert(0);
Adam Langley2e0f0712015-03-31 16:04:09 -0700317 return;
318 }
319
Adam Langleyb9e77a02015-03-31 18:36:20 -0700320 ERR_clear_error();
Adam Langley03d31ed2014-08-05 16:54:47 -0700321}
322
David Benjaminc44d2f42014-08-20 16:24:00 -0400323int ERR_get_next_error_library(void) {
Adam Langley2e0f0712015-03-31 16:04:09 -0700324 int ret;
325
Adam Langley8bdd5422015-04-13 11:04:09 -0700326 CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex);
Adam Langley2e0f0712015-03-31 16:04:09 -0700327 ret = global_next_library++;
David Benjamin29270de2016-05-24 15:28:36 +0000328 CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex);
Adam Langley2e0f0712015-03-31 16:04:09 -0700329
330 return ret;
Adam Langley40426672014-07-01 16:48:28 -0700331}
332
Matt Braithwaite9b68e722015-05-06 13:57:29 -0700333void ERR_remove_state(unsigned long pid) {
334 ERR_clear_error();
335}
336
Adam Langley95c29f32014-06-20 12:00:00 -0700337void ERR_clear_system_error(void) {
338 errno = 0;
339}
340
David Benjamin3fc138e2015-10-28 18:03:21 -0400341char *ERR_error_string(uint32_t packed_error, char *ret) {
342 static char buf[ERR_ERROR_STRING_BUF_LEN];
343
344 if (ret == NULL) {
David Benjamin808f8322017-08-18 14:06:02 -0400345 // TODO(fork): remove this.
David Benjamin3fc138e2015-10-28 18:03:21 -0400346 ret = buf;
347 }
348
349#if !defined(NDEBUG)
David Benjamin808f8322017-08-18 14:06:02 -0400350 // This is aimed to help catch callers who don't provide
351 // |ERR_ERROR_STRING_BUF_LEN| bytes of space.
David Benjamin17cf2cb2016-12-13 01:07:13 -0500352 OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
David Benjamin3fc138e2015-10-28 18:03:21 -0400353#endif
354
355 ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
356
357 return ret;
358}
359
360void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
David Benjamin34248d42015-06-28 23:36:21 -0400361 char lib_buf[64], reason_buf[64];
362 const char *lib_str, *reason_str;
363 unsigned lib, reason;
Adam Langley95c29f32014-06-20 12:00:00 -0700364
365 if (len == 0) {
366 return;
367 }
368
369 lib = ERR_GET_LIB(packed_error);
Adam Langley95c29f32014-06-20 12:00:00 -0700370 reason = ERR_GET_REASON(packed_error);
371
372 lib_str = ERR_lib_error_string(packed_error);
Adam Langley95c29f32014-06-20 12:00:00 -0700373 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 Benjamin3fc138e2015-10-28 18:03:21 -0400380 if (reason_str == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700381 BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
382 reason_str = reason_buf;
383 }
384
David Benjamin3fc138e2015-10-28 18:03:21 -0400385 BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
386 packed_error, lib_str, reason_str);
Adam Langley95c29f32014-06-20 12:00:00 -0700387
388 if (strlen(buf) == len - 1) {
David Benjamin808f8322017-08-18 14:06:02 -0400389 // output may be truncated; make sure we always have 5 colon-separated
390 // fields, i.e. 4 colons.
Adam Langley95c29f32014-06-20 12:00:00 -0700391 static const unsigned num_colons = 4;
392 unsigned i;
393 char *s = buf;
394
395 if (len <= num_colons) {
David Benjamin808f8322017-08-18 14:06:02 -0400396 // In this situation it's not possible to ensure that the correct number
397 // of colons are included in the output.
Adam Langley95c29f32014-06-20 12:00:00 -0700398 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 Benjamin808f8322017-08-18 14:06:02 -0400406 // 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 Benjamin17cf2cb2016-12-13 01:07:13 -0500410 OPENSSL_memset(last_pos, ':', num_colons - i);
Adam Langley95c29f32014-06-20 12:00:00 -0700411 break;
412 }
413
414 s = colon + 1;
415 }
416 }
417}
418
Adam Langley29b18672015-02-06 11:52:16 -0800419// err_string_cmp is a compare function for searching error values with
420// |bsearch| in |err_string_lookup|.
421static 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 Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langley29b18672015-02-06 11:52:16 -0800425 if (a_key < b_key) {
426 return -1;
427 } else if (a_key > b_key) {
428 return 1;
429 } else {
430 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700431 }
Adam Langley95c29f32014-06-20 12:00:00 -0700432}
433
David Benjamin808f8322017-08-18 14:06:02 -0400434// 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 Langley29b18672015-02-06 11:52:16 -0800436static 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 Benjamin808f8322017-08-18 14:06:02 -0400440 // |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 Langley29b18672015-02-06 11:52:16 -0800453 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
466static const char *const kLibraryNames[ERR_NUM_LIBS] = {
467 "invalid library (0)",
David Benjamin808f8322017-08-18 14:06:02 -0400468 "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 Langley29b18672015-02-06 11:52:16 -0800500};
501
Adam Langley95c29f32014-06-20 12:00:00 -0700502const char *ERR_lib_error_string(uint32_t packed_error) {
Adam Langley29b18672015-02-06 11:52:16 -0800503 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 Langley95c29f32014-06-20 12:00:00 -0700509}
510
511const char *ERR_func_error_string(uint32_t packed_error) {
David Benjamin34248d42015-06-28 23:36:21 -0400512 return "OPENSSL_internal";
Adam Langley95c29f32014-06-20 12:00:00 -0700513}
514
515const char *ERR_reason_error_string(uint32_t packed_error) {
Adam Langley29b18672015-02-06 11:52:16 -0800516 const uint32_t lib = ERR_GET_LIB(packed_error);
517 const uint32_t reason = ERR_GET_REASON(packed_error);
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langley29b18672015-02-06 11:52:16 -0800519 if (lib == ERR_LIB_SYS) {
520 if (reason < 127) {
521 return strerror(reason);
522 }
523 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700524 }
525
Adam Langley29b18672015-02-06 11:52:16 -0800526 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 Benjamind27eda02015-03-05 01:19:27 -0500547 return err_string_lookup(lib, reason, kOpenSSLReasonValues,
548 kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
Adam Langley95c29f32014-06-20 12:00:00 -0700549}
550
551void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700552 char buf[ERR_ERROR_STRING_BUF_LEN];
553 char buf2[1024];
Adam Langley5f1374e2014-10-01 15:21:01 -0700554 const char *file, *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700555 int line, flags;
556 uint32_t packed_error;
557
David Benjamin808f8322017-08-18 14:06:02 -0400558 // thread_hash is the least-significant bits of the |ERR_STATE| pointer value
559 // for this thread.
Adam Langley33672732015-03-31 18:55:53 -0700560 const unsigned long thread_hash = (uintptr_t) err_get_state();
Adam Langley95c29f32014-06-20 12:00:00 -0700561
562 for (;;) {
563 packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
564 if (packed_error == 0) {
565 break;
566 }
567
David Benjamin3fc138e2015-10-28 18:03:21 -0400568 ERR_error_string_n(packed_error, buf, sizeof(buf));
Adam Langley95c29f32014-06-20 12:00:00 -0700569 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 Langley95c29f32014-06-20 12:00:00 -0700574 }
575}
576
Brian Smith83a82982015-04-09 16:21:10 -1000577static 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
584void ERR_print_errors_fp(FILE *file) {
585 ERR_print_errors_cb(print_errors_to_file, file);
586}
587
David Benjamin808f8322017-08-18 14:06:02 -0400588// 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 Langley95c29f32014-06-20 12:00:00 -0700590static 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 Langley69a01602014-11-17 17:26:55 -0800594 if (state == NULL || state->top == state->bottom) {
David Benjamin4aa86f12014-11-02 20:47:17 -0500595 if (flags & ERR_FLAG_MALLOCED) {
596 OPENSSL_free(data);
597 }
Adam Langley95c29f32014-06-20 12:00:00 -0700598 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 Benjamin3fc138e2015-10-28 18:03:21 -0400608void ERR_put_error(int library, int unused, int reason, const char *file,
609 unsigned line) {
Adam Langley95c29f32014-06-20 12:00:00 -0700610 ERR_STATE *const state = err_get_state();
611 struct err_error_st *error;
612
Adam Langley69a01602014-11-17 17:26:55 -0800613 if (state == NULL) {
614 return;
615 }
616
Adam Langley95c29f32014-06-20 12:00:00 -0700617 if (library == ERR_LIB_SYS && reason == 0) {
Brian Smith8a36e532015-07-31 16:11:31 -0400618#if defined(OPENSSL_WINDOWS)
Adam Langley95c29f32014-06-20 12:00:00 -0700619 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 Benjamin34248d42015-06-28 23:36:21 -0400634 error->packed = ERR_PACK(library, reason);
Adam Langley95c29f32014-06-20 12:00:00 -0700635}
636
David Benjamin808f8322017-08-18 14:06:02 -0400637// 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 Langley95c29f32014-06-20 12:00:00 -0700640static 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 Benjamin808f8322017-08-18 14:06:02 -0400664 // overflow.
Adam Langley95c29f32014-06-20 12:00:00 -0700665 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 Benjamin17cf2cb2016-12-13 01:07:13 -0500678 OPENSSL_memcpy(buf + len, substr, substr_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700679 len = new_len;
680 }
681
682 buf[len] = 0;
683 err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
684}
685
686void 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
693void ERR_add_error_dataf(const char *format, ...) {
694 va_list ap;
695 char *buf;
696 static const unsigned buf_len = 256;
697
David Benjamin808f8322017-08-18 14:06:02 -0400698 // 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 Langley95c29f32014-06-20 12:00:00 -0700701 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
714int ERR_set_mark(void) {
715 ERR_STATE *const state = err_get_state();
716
Adam Langley69a01602014-11-17 17:26:55 -0800717 if (state == NULL || state->bottom == state->top) {
Adam Langley95c29f32014-06-20 12:00:00 -0700718 return 0;
719 }
720 state->errors[state->top].flags |= ERR_FLAG_MARK;
721 return 1;
722}
723
724int ERR_pop_to_mark(void) {
725 ERR_STATE *const state = err_get_state();
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langley69a01602014-11-17 17:26:55 -0800727 if (state == NULL) {
728 return 0;
729 }
730
Adam Langley95c29f32014-06-20 12:00:00 -0700731 while (state->bottom != state->top) {
Brian Smithefed2212015-01-28 16:20:02 -0800732 struct err_error_st *error = &state->errors[state->top];
Adam Langley95c29f32014-06-20 12:00:00 -0700733
734 if ((error->flags & ERR_FLAG_MARK) != 0) {
Brian Smithefed2212015-01-28 16:20:02 -0800735 error->flags &= ~ERR_FLAG_MARK;
736 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700737 }
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 Smithefed2212015-01-28 16:20:02 -0800747 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700748}
749
Adam Langley29b18672015-02-06 11:52:16 -0800750void ERR_load_crypto_strings(void) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700751
Adam Langleyb9e77a02015-03-31 18:36:20 -0700752void ERR_free_strings(void) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700753
David Benjaminc44d2f42014-08-20 16:24:00 -0400754void ERR_load_BIO_strings(void) {}
Matt Braithwaite9febf192015-04-30 13:04:06 -0700755
756void ERR_load_ERR_strings(void) {}