blob: 6349a74bd8b900bdf47c1acec16198bb955ad545 [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>
114#include <stdarg.h>
115#include <stdio.h>
116
Yoshisato Yanagisawab7725cf2014-08-18 11:39:09 -0700117#if defined(OPENSSL_WINDOWS)
118#include <Windows.h>
119#endif
120
Adam Langley95c29f32014-06-20 12:00:00 -0700121#include <openssl/lhash.h>
122#include <openssl/mem.h>
123#include <openssl/thread.h>
124
125
126/* err_fns contains a pointer to the current error implementation. */
127static const struct ERR_FNS_st *err_fns = NULL;
128extern const struct ERR_FNS_st openssl_err_default_impl;
129
130#define ERRFN(a) err_fns->a
131
132/* err_fns_check is an internal function that checks whether "err_fns" is set
133 * and if not, sets it to the default. */
134static void err_fns_check(void) {
135 /* In practice, this is not a race problem because loading the error strings
136 * at init time will cause this pointer to be set before the process goes
137 * multithreaded. */
138 if (err_fns) {
139 return;
140 }
141
142 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
143 if (!err_fns) {
144 err_fns = &openssl_err_default_impl;
145 }
146 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
147}
148
149/* err_clear_data frees the optional |data| member of the given error. */
150static void err_clear_data(struct err_error_st *error) {
151 if (error->data != NULL && (error->flags & ERR_FLAG_MALLOCED) != 0) {
152 OPENSSL_free(error->data);
153 }
154 error->data = NULL;
155 error->flags &= ~ERR_FLAG_MALLOCED;
156}
157
158/* err_clear clears the given queued error. */
159static void err_clear(struct err_error_st *error) {
160 err_clear_data(error);
161 memset(error, 0, sizeof(struct err_error_st));
162}
163
164/* err_get_state gets the ERR_STATE object for the current thread. */
165static ERR_STATE *err_get_state(void) {
166 err_fns_check();
167 return ERRFN(get_state)();
168}
169
170static uint32_t get_error_values(int inc, int top, const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700171 const char **data, int *flags) {
Adam Langley95c29f32014-06-20 12:00:00 -0700172 unsigned i = 0;
173 ERR_STATE *state;
174 struct err_error_st *error;
175 uint32_t ret;
176
177 state = err_get_state();
Adam Langley69a01602014-11-17 17:26:55 -0800178 if (state == NULL || state->bottom == state->top) {
Adam Langley95c29f32014-06-20 12:00:00 -0700179 return 0;
180 }
181
182 if (top) {
David Benjamin0d824822014-11-08 12:51:36 -0500183 assert(!inc);
Adam Langley95c29f32014-06-20 12:00:00 -0700184 /* last error */
185 i = state->top;
186 } else {
187 i = (state->bottom + 1) % ERR_NUM_ERRORS;
188 }
189
190 error = &state->errors[i];
191 ret = error->packed;
192
193 if (file != NULL && line != NULL) {
194 if (error->file == NULL) {
195 *file = "NA";
196 *line = 0;
197 } else {
198 *file = error->file;
199 *line = error->line;
200 }
201 }
202
203 if (data != NULL) {
204 if (error->data == NULL) {
205 *data = "";
206 if (flags != NULL) {
207 *flags = 0;
208 }
209 } else {
210 *data = error->data;
211 if (flags != NULL) {
212 *flags = error->flags & ERR_FLAG_PUBLIC_MASK;
213 }
David Benjamin0d824822014-11-08 12:51:36 -0500214 /* If this error is being removed, take ownership of data from
215 * the error. The semantics are such that the caller doesn't
216 * take ownership either. Instead the error system takes
217 * ownership and retains it until the next call that affects the
218 * error queue. */
219 if (inc) {
220 if (error->flags & ERR_FLAG_MALLOCED) {
221 if (state->to_free) {
222 OPENSSL_free(state->to_free);
223 }
224 state->to_free = error->data;
Adam Langley5f1374e2014-10-01 15:21:01 -0700225 }
David Benjamin0d824822014-11-08 12:51:36 -0500226 error->data = NULL;
227 error->flags = 0;
Adam Langley5f1374e2014-10-01 15:21:01 -0700228 }
Adam Langley95c29f32014-06-20 12:00:00 -0700229 }
230 }
231
232 if (inc) {
233 assert(!top);
234 err_clear(error);
235 state->bottom = i;
236 }
237
238 return ret;
239}
240
241uint32_t ERR_get_error(void) {
242 return get_error_values(1, 0, NULL, NULL, NULL, NULL);
243}
244
245uint32_t ERR_get_error_line(const char **file, int *line) {
246 return get_error_values(1, 0, file, line, NULL, NULL);
247}
248
249uint32_t ERR_get_error_line_data(const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700250 const char **data, int *flags) {
Adam Langley95c29f32014-06-20 12:00:00 -0700251 return get_error_values(1, 0, file, line, data, flags);
252}
253
254uint32_t ERR_peek_error(void) {
255 return get_error_values(0, 0, NULL, NULL, NULL, NULL);
256}
257
258uint32_t ERR_peek_error_line(const char **file, int *line) {
259 return get_error_values(0, 0, file, line, NULL, NULL);
260}
261
262uint32_t ERR_peek_error_line_data(const char **file, int *line,
263 const char **data, int *flags) {
Adam Langley5f1374e2014-10-01 15:21:01 -0700264 return get_error_values(0, 0, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700265}
266
267uint32_t ERR_peek_last_error(void) {
268 return get_error_values(0, 1, NULL, NULL, NULL, NULL);
269}
270
271uint32_t ERR_peek_last_error_line(const char **file, int *line) {
272 return get_error_values(0, 1, file, line, NULL, NULL);
273}
274
275uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
276 const char **data, int *flags) {
Adam Langley5f1374e2014-10-01 15:21:01 -0700277 return get_error_values(0, 1, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700278}
279
280void ERR_clear_error(void) {
281 ERR_STATE *const state = err_get_state();
282 unsigned i;
283
Adam Langley69a01602014-11-17 17:26:55 -0800284 if (state == NULL) {
285 return;
286 }
287
Adam Langley95c29f32014-06-20 12:00:00 -0700288 for (i = 0; i < ERR_NUM_ERRORS; i++) {
289 err_clear(&state->errors[i]);
290 }
Adam Langley5f1374e2014-10-01 15:21:01 -0700291 if (state->to_free) {
292 OPENSSL_free(state->to_free);
Adam Langley57e52f22014-10-06 19:30:44 -0700293 state->to_free = NULL;
Adam Langley5f1374e2014-10-01 15:21:01 -0700294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
296 state->top = state->bottom = 0;
297}
298
Adam Langley03d31ed2014-08-05 16:54:47 -0700299void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
300 CRYPTO_THREADID current;
301 ERR_STATE *state;
302 unsigned i;
303
304 if (tid == NULL) {
305 CRYPTO_THREADID_current(&current);
306 tid = &current;
307 }
308
309 err_fns_check();
310 state = ERRFN(release_state)(tid);
311 if (state == NULL) {
312 return;
313 }
314
315 for (i = 0; i < ERR_NUM_ERRORS; i++) {
316 err_clear(&state->errors[i]);
317 }
Adam Langley5f1374e2014-10-01 15:21:01 -0700318 if (state->to_free) {
319 OPENSSL_free(state->to_free);
320 }
Adam Langley03d31ed2014-08-05 16:54:47 -0700321 OPENSSL_free(state);
322}
323
David Benjaminc44d2f42014-08-20 16:24:00 -0400324int ERR_get_next_error_library(void) {
Adam Langley40426672014-07-01 16:48:28 -0700325 err_fns_check();
326 return ERRFN(get_next_library)();
327}
328
Adam Langley95c29f32014-06-20 12:00:00 -0700329void ERR_clear_system_error(void) {
330 errno = 0;
331}
332
333char *ERR_error_string(uint32_t packed_error, char *ret) {
334 static char buf[ERR_ERROR_STRING_BUF_LEN];
335
336 if (ret == NULL) {
337 /* TODO(fork): remove this. */
338 ret = buf;
339 }
340
341#if !defined(NDEBUG)
342 /* This is aimed to help catch callers who don't provide
343 * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
344 memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
345#endif
346
347 ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
348
349 return ret;
350}
351
352void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
353 char lib_buf[64], func_buf[64], reason_buf[64];
354 const char *lib_str, *func_str, *reason_str;
355 unsigned lib, func, reason;
356
357 if (len == 0) {
358 return;
359 }
360
361 lib = ERR_GET_LIB(packed_error);
362 func = ERR_GET_FUNC(packed_error);
363 reason = ERR_GET_REASON(packed_error);
364
365 lib_str = ERR_lib_error_string(packed_error);
366 func_str = ERR_func_error_string(packed_error);
367 reason_str = ERR_reason_error_string(packed_error);
368
369 if (lib_str == NULL) {
370 BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
371 lib_str = lib_buf;
372 }
373
374 if (func_str == NULL) {
375 BIO_snprintf(func_buf, sizeof(func_buf), "func(%u)", func);
376 func_str = func_buf;
377 }
378
379 if (reason_str == NULL) {
380 BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
381 reason_str = reason_buf;
382 }
383
384 BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:%s:%s",
385 packed_error, lib_str, func_str, reason_str);
386
387 if (strlen(buf) == len - 1) {
388 /* output may be truncated; make sure we always have 5 colon-separated
389 * fields, i.e. 4 colons. */
390 static const unsigned num_colons = 4;
391 unsigned i;
392 char *s = buf;
393
394 if (len <= num_colons) {
395 /* In this situation it's not possible to ensure that the correct number
396 * of colons are included in the output. */
397 return;
398 }
399
400 for (i = 0; i < num_colons; i++) {
401 char *colon = strchr(s, ':');
402 char *last_pos = &buf[len - 1] - num_colons + i;
403
404 if (colon == NULL || colon > last_pos) {
405 /* set colon |i| at last possible position (buf[len-1] is the
406 * terminating 0). If we're setting this colon, then all whole of the
407 * rest of the string must be colons in order to have the correct
408 * number. */
409 memset(last_pos, ':', num_colons - i);
410 break;
411 }
412
413 s = colon + 1;
414 }
415 }
416}
417
418/* err_component_error_string returns the error string associated with
419 * |packed_error|, which must be of a special form matching the keys inserted
420 * into the error hash table. */
421static const char *err_component_error_string(uint32_t packed_error) {
422 ERR_STRING_DATA *p;
423
424 err_fns_check();
425 p = ERRFN(get_item)(packed_error);
426
427 if (p == NULL) {
428 return NULL;
429 }
430 return p->string;
431}
432
433const char *ERR_lib_error_string(uint32_t packed_error) {
434 return err_component_error_string(ERR_PACK(ERR_GET_LIB(packed_error), 0, 0));
435}
436
437const char *ERR_func_error_string(uint32_t packed_error) {
438 return err_component_error_string(
439 ERR_PACK(ERR_GET_LIB(packed_error), ERR_GET_FUNC(packed_error), 0));
440}
441
442const char *ERR_reason_error_string(uint32_t packed_error) {
443 const char *reason_str = err_component_error_string(
444 ERR_PACK(ERR_GET_LIB(packed_error), 0, ERR_GET_REASON(packed_error)));
445
446 if (reason_str != NULL) {
447 return reason_str;
448 }
449
450 return err_component_error_string(
451 ERR_PACK(0, 0, ERR_GET_REASON(packed_error)));
452}
453
454void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
455 CRYPTO_THREADID current_thread;
456 char buf[ERR_ERROR_STRING_BUF_LEN];
457 char buf2[1024];
458 unsigned long thread_hash;
Adam Langley5f1374e2014-10-01 15:21:01 -0700459 const char *file, *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700460 int line, flags;
461 uint32_t packed_error;
462
463 CRYPTO_THREADID_current(&current_thread);
464 thread_hash = CRYPTO_THREADID_hash(&current_thread);
465
466 for (;;) {
467 packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
468 if (packed_error == 0) {
469 break;
470 }
471
472 ERR_error_string_n(packed_error, buf, sizeof(buf));
473 BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
474 file, line, (flags & ERR_FLAG_STRING) ? data : "");
475 if (callback(buf2, strlen(buf2), ctx) <= 0) {
476 break;
477 }
Adam Langley95c29f32014-06-20 12:00:00 -0700478 }
479}
480
481/* err_set_error_data sets the data on the most recent error. The |flags|
482 * argument is a combination of the |ERR_FLAG_*| values. */
483static void err_set_error_data(char *data, int flags) {
484 ERR_STATE *const state = err_get_state();
485 struct err_error_st *error;
486
Adam Langley69a01602014-11-17 17:26:55 -0800487 if (state == NULL || state->top == state->bottom) {
David Benjamin4aa86f12014-11-02 20:47:17 -0500488 if (flags & ERR_FLAG_MALLOCED) {
489 OPENSSL_free(data);
490 }
Adam Langley95c29f32014-06-20 12:00:00 -0700491 return;
492 }
493
494 error = &state->errors[state->top];
495
496 err_clear_data(error);
497 error->data = data;
498 error->flags = flags;
499}
500
501void ERR_put_error(int library, int func, int reason, const char *file,
502 unsigned line) {
503 ERR_STATE *const state = err_get_state();
504 struct err_error_st *error;
505
Adam Langley69a01602014-11-17 17:26:55 -0800506 if (state == NULL) {
507 return;
508 }
509
Adam Langley95c29f32014-06-20 12:00:00 -0700510 if (library == ERR_LIB_SYS && reason == 0) {
511#if defined(WIN32)
512 reason = GetLastError();
513#else
514 reason = errno;
515#endif
516 }
517
518 state->top = (state->top + 1) % ERR_NUM_ERRORS;
519 if (state->top == state->bottom) {
520 state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
521 }
522
523 error = &state->errors[state->top];
524 err_clear(error);
525 error->file = file;
526 error->line = line;
527 error->packed = ERR_PACK(library, func, reason);
528}
529
530/* ERR_add_error_data_vdata takes a variable number of const char* pointers,
531 * concatenates them and sets the result as the data on the most recent
532 * error. */
533static void err_add_error_vdata(unsigned num, va_list args) {
534 size_t alloced, new_len, len = 0, substr_len;
535 char *buf;
536 const char *substr;
537 unsigned i;
538
539 alloced = 80;
540 buf = OPENSSL_malloc(alloced + 1);
541 if (buf == NULL) {
542 return;
543 }
544
545 for (i = 0; i < num; i++) {
546 substr = va_arg(args, const char *);
547 if (substr == NULL) {
548 continue;
549 }
550
551 substr_len = strlen(substr);
552 new_len = len + substr_len;
553 if (new_len > alloced) {
554 char *new_buf;
555
556 if (alloced + 20 + 1 < alloced) {
557 /* overflow. */
558 OPENSSL_free(buf);
559 return;
560 }
561
562 alloced = new_len + 20;
563 new_buf = OPENSSL_realloc(buf, alloced + 1);
564 if (new_buf == NULL) {
565 OPENSSL_free(buf);
566 return;
567 }
568 buf = new_buf;
569 }
570
571 memcpy(buf + len, substr, substr_len);
572 len = new_len;
573 }
574
575 buf[len] = 0;
576 err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
577}
578
579void ERR_add_error_data(unsigned count, ...) {
580 va_list args;
581 va_start(args, count);
582 err_add_error_vdata(count, args);
583 va_end(args);
584}
585
586void ERR_add_error_dataf(const char *format, ...) {
587 va_list ap;
588 char *buf;
589 static const unsigned buf_len = 256;
590
591 /* A fixed-size buffer is used because va_copy (which would be needed in
592 * order to call vsnprintf twice and measure the buffer) wasn't defined until
593 * C99. */
594 buf = OPENSSL_malloc(buf_len + 1);
595 if (buf == NULL) {
596 return;
597 }
598
599 va_start(ap, format);
600 BIO_vsnprintf(buf, buf_len, format, ap);
601 buf[buf_len] = 0;
602 va_end(ap);
603
604 err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
605}
606
607int ERR_set_mark(void) {
608 ERR_STATE *const state = err_get_state();
609
Adam Langley69a01602014-11-17 17:26:55 -0800610 if (state == NULL || state->bottom == state->top) {
Adam Langley95c29f32014-06-20 12:00:00 -0700611 return 0;
612 }
613 state->errors[state->top].flags |= ERR_FLAG_MARK;
614 return 1;
615}
616
617int ERR_pop_to_mark(void) {
618 ERR_STATE *const state = err_get_state();
619 struct err_error_st *error;
620
Adam Langley69a01602014-11-17 17:26:55 -0800621 if (state == NULL) {
622 return 0;
623 }
624
Adam Langley95c29f32014-06-20 12:00:00 -0700625 while (state->bottom != state->top) {
626 error = &state->errors[state->top];
627
628 if ((error->flags & ERR_FLAG_MARK) != 0) {
629 break;
630 }
631
632 err_clear(error);
633 if (state->top == 0) {
634 state->top = ERR_NUM_ERRORS - 1;
635 } else {
636 state->top--;
637 }
638 }
639
640 if (state->bottom == state->top) {
641 return 0;
642 }
643
644 error->flags &= ~ERR_FLAG_MARK;
645 return 1;
646}
647
648static const char *const kLibraryNames[ERR_NUM_LIBS] = {
649 "invalid library (0)",
650 "unknown library", /* ERR_LIB_NONE */
651 "system library", /* ERR_LIB_SYS */
652 "bignum routines", /* ERR_LIB_BN */
653 "RSA routines", /* ERR_LIB_RSA */
654 "Diffie-Hellman routines", /* ERR_LIB_DH */
655 "public key routines", /* ERR_LIB_EVP */
656 "memory buffer routines", /* ERR_LIB_BUF */
657 "object identifier routines", /* ERR_LIB_OBJ */
658 "PEM routines", /* ERR_LIB_PEM */
659 "DSA routines", /* ERR_LIB_DSA */
660 "X.509 certificate routines", /* ERR_LIB_X509 */
661 "ASN.1 encoding routines", /* ERR_LIB_ASN1 */
662 "configuration file routines", /* ERR_LIB_CONF */
663 "common libcrypto routines", /* ERR_LIB_CRYPTO */
664 "elliptic curve routines", /* ERR_LIB_EC */
665 "SSL routines", /* ERR_LIB_SSL */
666 "BIO routines", /* ERR_LIB_BIO */
667 "PKCS7 routines", /* ERR_LIB_PKCS7 */
668 "PKCS8 routines", /* ERR_LIB_PKCS8 */
669 "X509 V3 routines", /* ERR_LIB_X509V3 */
Adam Langley95c29f32014-06-20 12:00:00 -0700670 "random number generator", /* ERR_LIB_RAND */
671 "ENGINE routines", /* ERR_LIB_ENGINE */
672 "OCSP routines", /* ERR_LIB_OCSP */
673 "UI routines", /* ERR_LIB_UI */
674 "COMP routines", /* ERR_LIB_COMP */
675 "ECDSA routines", /* ERR_LIB_ECDSA */
676 "ECDH routines", /* ERR_LIB_ECDH */
677 "HMAC routines", /* ERR_LIB_HMAC */
678 "Digest functions", /* ERR_LIB_DIGEST */
679 "Cipher functions", /* ERR_LIB_CIPHER */
680 "User defined functions", /* ERR_LIB_USER */
681};
682
683#define NUM_SYS_ERRNOS 127
684
685/* kStaticErrors provides storage for ERR_STRING_DATA values that are created
686 * at init time because we assume that ERR_STRING_DATA structures aren't
687 * allocated on the heap. */
688static ERR_STRING_DATA kStaticErrors[ERR_NUM_LIBS * 2 + NUM_SYS_ERRNOS];
689
690static ERR_STRING_DATA kGlobalErrors[] = {
691 {ERR_R_MALLOC_FAILURE, "malloc failure"},
692 {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "function should not be called"},
693 {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
694 {ERR_R_INTERNAL_ERROR, "internal error"},
Adam Langley66014022014-08-13 10:31:04 -0700695 {ERR_R_OVERFLOW, "overflow"},
Adam Langley95c29f32014-06-20 12:00:00 -0700696
697 {ERR_PACK(ERR_LIB_SYS, SYS_F_fopen, 0), "fopen"},
698 {ERR_PACK(ERR_LIB_SYS, SYS_F_fclose, 0), "fclose"},
699 {ERR_PACK(ERR_LIB_SYS, SYS_F_fread, 0), "fread"},
700 {ERR_PACK(ERR_LIB_SYS, SYS_F_fwrite, 0), "fwrite"},
701 {ERR_PACK(ERR_LIB_SYS, SYS_F_socket, 0), "socket"},
702 {ERR_PACK(ERR_LIB_SYS, SYS_F_setsockopt, 0), "setsockopt"},
703 {ERR_PACK(ERR_LIB_SYS, SYS_F_connect, 0), "connect"},
704 {ERR_PACK(ERR_LIB_SYS, SYS_F_getaddrinfo, 0), "getaddrinfo"},
705
706 {0, NULL},
707};
708
709
710extern const ERR_STRING_DATA ASN1_error_string_data[];
711extern const ERR_STRING_DATA BIO_error_string_data[];
712extern const ERR_STRING_DATA BN_error_string_data[];
713extern const ERR_STRING_DATA BUF_error_string_data[];
714extern const ERR_STRING_DATA CIPHER_error_string_data[];
715extern const ERR_STRING_DATA CONF_error_string_data[];
716extern const ERR_STRING_DATA CRYPTO_error_string_data[];
717extern const ERR_STRING_DATA DH_error_string_data[];
718extern const ERR_STRING_DATA DIGEST_error_string_data[];
719extern const ERR_STRING_DATA DSA_error_string_data[];
720extern const ERR_STRING_DATA ECDH_error_string_data[];
721extern const ERR_STRING_DATA ECDSA_error_string_data[];
722extern const ERR_STRING_DATA EC_error_string_data[];
Adam Langley7ea84812014-10-09 16:26:09 -0700723extern const ERR_STRING_DATA ENGINE_error_string_data[];
Adam Langley95c29f32014-06-20 12:00:00 -0700724extern const ERR_STRING_DATA EVP_error_string_data[];
725extern const ERR_STRING_DATA OBJ_error_string_data[];
726extern const ERR_STRING_DATA PEM_error_string_data[];
Adam Langley8e16b6e2014-08-21 14:11:39 -0700727extern const ERR_STRING_DATA PKCS8_error_string_data[];
Adam Langley95c29f32014-06-20 12:00:00 -0700728extern const ERR_STRING_DATA RSA_error_string_data[];
729extern const ERR_STRING_DATA X509V3_error_string_data[];
730extern const ERR_STRING_DATA X509_error_string_data[];
731
732static void err_load_strings(void) {
733 unsigned i, j = 0;
734
735 err_fns_check();
736
737 /* This loop loads strings for the libraries for the ERR_R_*_LIB
738 * reasons. */
739 for (i = ERR_LIB_NONE; i < ERR_NUM_LIBS; i++) {
740 ERR_STRING_DATA *data = &kStaticErrors[j++];
741 data->string = kLibraryNames[i];
742 data->error = ERR_PACK(i, 0, 0);
743 ERRFN(set_item)(data);
744
745 data = &kStaticErrors[j++];
746 data->string = kLibraryNames[i];
747 data->error = ERR_PACK(0, 0, i);
748 ERRFN(set_item)(data);
749 }
750
751 for (i = 1; i < 1 + NUM_SYS_ERRNOS; i++) {
752 /* The "SYS" library sets errno values as the reason for its errors.
753 * Thus we load the first |NUM_SYS_ERRNOS| errno strings as the
754 * reason strings for that library. */
755
756 ERR_STRING_DATA *data = &kStaticErrors[j++];
757 data->string = strerror(i);
758 data->error = ERR_PACK(ERR_LIB_SYS, 0, i);
759 ERRFN(set_item)(data);
760 }
761
762 ERR_load_strings(kGlobalErrors);
763
764 ERR_load_strings(ASN1_error_string_data);
765 ERR_load_strings(BIO_error_string_data);
766 ERR_load_strings(BN_error_string_data);
767 ERR_load_strings(BUF_error_string_data);
768 ERR_load_strings(CIPHER_error_string_data);
769 ERR_load_strings(CONF_error_string_data);
770 ERR_load_strings(CRYPTO_error_string_data);
771 ERR_load_strings(DH_error_string_data);
772 ERR_load_strings(DIGEST_error_string_data);
773 ERR_load_strings(DSA_error_string_data);
774 ERR_load_strings(ECDH_error_string_data);
775 ERR_load_strings(ECDSA_error_string_data);
776 ERR_load_strings(EC_error_string_data);
Adam Langley7ea84812014-10-09 16:26:09 -0700777 ERR_load_strings(ENGINE_error_string_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700778 ERR_load_strings(EVP_error_string_data);
779 ERR_load_strings(OBJ_error_string_data);
780 ERR_load_strings(PEM_error_string_data);
Adam Langley8e16b6e2014-08-21 14:11:39 -0700781 ERR_load_strings(PKCS8_error_string_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700782 ERR_load_strings(RSA_error_string_data);
783 ERR_load_strings(X509V3_error_string_data);
784 ERR_load_strings(X509_error_string_data);
785}
786
787void ERR_load_strings(const ERR_STRING_DATA *str) {
788 err_fns_check();
789
790 while (str->error) {
791 ERRFN(set_item)(str);
792 str++;
793 }
794}
795
David Benjaminc44d2f42014-08-20 16:24:00 -0400796void ERR_load_crypto_strings(void) { err_load_strings(); }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
David Benjaminc44d2f42014-08-20 16:24:00 -0400798void ERR_free_strings(void) {
Adam Langley95c29f32014-06-20 12:00:00 -0700799 err_fns_check();
800 ERRFN(shutdown)();
801}
802
David Benjaminc44d2f42014-08-20 16:24:00 -0400803void ERR_load_BIO_strings(void) {}