blob: 6390b9a47bedd9d6241656cc56d6b53dda6b812e [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();
178
179 if (state->bottom == state->top) {
180 return 0;
181 }
182
183 if (top) {
David Benjamin0d824822014-11-08 12:51:36 -0500184 assert(!inc);
Adam Langley95c29f32014-06-20 12:00:00 -0700185 /* last error */
186 i = state->top;
187 } else {
188 i = (state->bottom + 1) % ERR_NUM_ERRORS;
189 }
190
191 error = &state->errors[i];
192 ret = error->packed;
193
194 if (file != NULL && line != NULL) {
195 if (error->file == NULL) {
196 *file = "NA";
197 *line = 0;
198 } else {
199 *file = error->file;
200 *line = error->line;
201 }
202 }
203
204 if (data != NULL) {
205 if (error->data == NULL) {
206 *data = "";
207 if (flags != NULL) {
208 *flags = 0;
209 }
210 } else {
211 *data = error->data;
212 if (flags != NULL) {
213 *flags = error->flags & ERR_FLAG_PUBLIC_MASK;
214 }
David Benjamin0d824822014-11-08 12:51:36 -0500215 /* If this error is being removed, take ownership of data from
216 * the error. The semantics are such that the caller doesn't
217 * take ownership either. Instead the error system takes
218 * ownership and retains it until the next call that affects the
219 * error queue. */
220 if (inc) {
221 if (error->flags & ERR_FLAG_MALLOCED) {
222 if (state->to_free) {
223 OPENSSL_free(state->to_free);
224 }
225 state->to_free = error->data;
Adam Langley5f1374e2014-10-01 15:21:01 -0700226 }
David Benjamin0d824822014-11-08 12:51:36 -0500227 error->data = NULL;
228 error->flags = 0;
Adam Langley5f1374e2014-10-01 15:21:01 -0700229 }
Adam Langley95c29f32014-06-20 12:00:00 -0700230 }
231 }
232
233 if (inc) {
234 assert(!top);
235 err_clear(error);
236 state->bottom = i;
237 }
238
239 return ret;
240}
241
242uint32_t ERR_get_error(void) {
243 return get_error_values(1, 0, NULL, NULL, NULL, NULL);
244}
245
246uint32_t ERR_get_error_line(const char **file, int *line) {
247 return get_error_values(1, 0, file, line, NULL, NULL);
248}
249
250uint32_t ERR_get_error_line_data(const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700251 const char **data, int *flags) {
Adam Langley95c29f32014-06-20 12:00:00 -0700252 return get_error_values(1, 0, file, line, data, flags);
253}
254
255uint32_t ERR_peek_error(void) {
256 return get_error_values(0, 0, NULL, NULL, NULL, NULL);
257}
258
259uint32_t ERR_peek_error_line(const char **file, int *line) {
260 return get_error_values(0, 0, file, line, NULL, NULL);
261}
262
263uint32_t ERR_peek_error_line_data(const char **file, int *line,
264 const char **data, int *flags) {
Adam Langley5f1374e2014-10-01 15:21:01 -0700265 return get_error_values(0, 0, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700266}
267
268uint32_t ERR_peek_last_error(void) {
269 return get_error_values(0, 1, NULL, NULL, NULL, NULL);
270}
271
272uint32_t ERR_peek_last_error_line(const char **file, int *line) {
273 return get_error_values(0, 1, file, line, NULL, NULL);
274}
275
276uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
277 const char **data, int *flags) {
Adam Langley5f1374e2014-10-01 15:21:01 -0700278 return get_error_values(0, 1, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700279}
280
281void ERR_clear_error(void) {
282 ERR_STATE *const state = err_get_state();
283 unsigned i;
284
285 for (i = 0; i < ERR_NUM_ERRORS; i++) {
286 err_clear(&state->errors[i]);
287 }
Adam Langley5f1374e2014-10-01 15:21:01 -0700288 if (state->to_free) {
289 OPENSSL_free(state->to_free);
Adam Langley57e52f22014-10-06 19:30:44 -0700290 state->to_free = NULL;
Adam Langley5f1374e2014-10-01 15:21:01 -0700291 }
Adam Langley95c29f32014-06-20 12:00:00 -0700292
293 state->top = state->bottom = 0;
294}
295
Adam Langley03d31ed2014-08-05 16:54:47 -0700296void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
297 CRYPTO_THREADID current;
298 ERR_STATE *state;
299 unsigned i;
300
301 if (tid == NULL) {
302 CRYPTO_THREADID_current(&current);
303 tid = &current;
304 }
305
306 err_fns_check();
307 state = ERRFN(release_state)(tid);
308 if (state == NULL) {
309 return;
310 }
311
312 for (i = 0; i < ERR_NUM_ERRORS; i++) {
313 err_clear(&state->errors[i]);
314 }
Adam Langley5f1374e2014-10-01 15:21:01 -0700315 if (state->to_free) {
316 OPENSSL_free(state->to_free);
317 }
Adam Langley03d31ed2014-08-05 16:54:47 -0700318 OPENSSL_free(state);
319}
320
David Benjaminc44d2f42014-08-20 16:24:00 -0400321int ERR_get_next_error_library(void) {
Adam Langley40426672014-07-01 16:48:28 -0700322 err_fns_check();
323 return ERRFN(get_next_library)();
324}
325
Adam Langley95c29f32014-06-20 12:00:00 -0700326void ERR_clear_system_error(void) {
327 errno = 0;
328}
329
330char *ERR_error_string(uint32_t packed_error, char *ret) {
331 static char buf[ERR_ERROR_STRING_BUF_LEN];
332
333 if (ret == NULL) {
334 /* TODO(fork): remove this. */
335 ret = buf;
336 }
337
338#if !defined(NDEBUG)
339 /* This is aimed to help catch callers who don't provide
340 * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
341 memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
342#endif
343
344 ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
345
346 return ret;
347}
348
349void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
350 char lib_buf[64], func_buf[64], reason_buf[64];
351 const char *lib_str, *func_str, *reason_str;
352 unsigned lib, func, reason;
353
354 if (len == 0) {
355 return;
356 }
357
358 lib = ERR_GET_LIB(packed_error);
359 func = ERR_GET_FUNC(packed_error);
360 reason = ERR_GET_REASON(packed_error);
361
362 lib_str = ERR_lib_error_string(packed_error);
363 func_str = ERR_func_error_string(packed_error);
364 reason_str = ERR_reason_error_string(packed_error);
365
366 if (lib_str == NULL) {
367 BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
368 lib_str = lib_buf;
369 }
370
371 if (func_str == NULL) {
372 BIO_snprintf(func_buf, sizeof(func_buf), "func(%u)", func);
373 func_str = func_buf;
374 }
375
376 if (reason_str == NULL) {
377 BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
378 reason_str = reason_buf;
379 }
380
381 BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:%s:%s",
382 packed_error, lib_str, func_str, reason_str);
383
384 if (strlen(buf) == len - 1) {
385 /* output may be truncated; make sure we always have 5 colon-separated
386 * fields, i.e. 4 colons. */
387 static const unsigned num_colons = 4;
388 unsigned i;
389 char *s = buf;
390
391 if (len <= num_colons) {
392 /* In this situation it's not possible to ensure that the correct number
393 * of colons are included in the output. */
394 return;
395 }
396
397 for (i = 0; i < num_colons; i++) {
398 char *colon = strchr(s, ':');
399 char *last_pos = &buf[len - 1] - num_colons + i;
400
401 if (colon == NULL || colon > last_pos) {
402 /* set colon |i| at last possible position (buf[len-1] is the
403 * terminating 0). If we're setting this colon, then all whole of the
404 * rest of the string must be colons in order to have the correct
405 * number. */
406 memset(last_pos, ':', num_colons - i);
407 break;
408 }
409
410 s = colon + 1;
411 }
412 }
413}
414
415/* err_component_error_string returns the error string associated with
416 * |packed_error|, which must be of a special form matching the keys inserted
417 * into the error hash table. */
418static const char *err_component_error_string(uint32_t packed_error) {
419 ERR_STRING_DATA *p;
420
421 err_fns_check();
422 p = ERRFN(get_item)(packed_error);
423
424 if (p == NULL) {
425 return NULL;
426 }
427 return p->string;
428}
429
430const char *ERR_lib_error_string(uint32_t packed_error) {
431 return err_component_error_string(ERR_PACK(ERR_GET_LIB(packed_error), 0, 0));
432}
433
434const char *ERR_func_error_string(uint32_t packed_error) {
435 return err_component_error_string(
436 ERR_PACK(ERR_GET_LIB(packed_error), ERR_GET_FUNC(packed_error), 0));
437}
438
439const char *ERR_reason_error_string(uint32_t packed_error) {
440 const char *reason_str = err_component_error_string(
441 ERR_PACK(ERR_GET_LIB(packed_error), 0, ERR_GET_REASON(packed_error)));
442
443 if (reason_str != NULL) {
444 return reason_str;
445 }
446
447 return err_component_error_string(
448 ERR_PACK(0, 0, ERR_GET_REASON(packed_error)));
449}
450
451void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
452 CRYPTO_THREADID current_thread;
453 char buf[ERR_ERROR_STRING_BUF_LEN];
454 char buf2[1024];
455 unsigned long thread_hash;
Adam Langley5f1374e2014-10-01 15:21:01 -0700456 const char *file, *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700457 int line, flags;
458 uint32_t packed_error;
459
460 CRYPTO_THREADID_current(&current_thread);
461 thread_hash = CRYPTO_THREADID_hash(&current_thread);
462
463 for (;;) {
464 packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
465 if (packed_error == 0) {
466 break;
467 }
468
469 ERR_error_string_n(packed_error, buf, sizeof(buf));
470 BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
471 file, line, (flags & ERR_FLAG_STRING) ? data : "");
472 if (callback(buf2, strlen(buf2), ctx) <= 0) {
473 break;
474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475 }
476}
477
478/* err_set_error_data sets the data on the most recent error. The |flags|
479 * argument is a combination of the |ERR_FLAG_*| values. */
480static void err_set_error_data(char *data, int flags) {
481 ERR_STATE *const state = err_get_state();
482 struct err_error_st *error;
483
484 if (state->top == state->bottom) {
David Benjamin4aa86f12014-11-02 20:47:17 -0500485 if (flags & ERR_FLAG_MALLOCED) {
486 OPENSSL_free(data);
487 }
Adam Langley95c29f32014-06-20 12:00:00 -0700488 return;
489 }
490
491 error = &state->errors[state->top];
492
493 err_clear_data(error);
494 error->data = data;
495 error->flags = flags;
496}
497
498void ERR_put_error(int library, int func, int reason, const char *file,
499 unsigned line) {
500 ERR_STATE *const state = err_get_state();
501 struct err_error_st *error;
502
503 if (library == ERR_LIB_SYS && reason == 0) {
504#if defined(WIN32)
505 reason = GetLastError();
506#else
507 reason = errno;
508#endif
509 }
510
511 state->top = (state->top + 1) % ERR_NUM_ERRORS;
512 if (state->top == state->bottom) {
513 state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
514 }
515
516 error = &state->errors[state->top];
517 err_clear(error);
518 error->file = file;
519 error->line = line;
520 error->packed = ERR_PACK(library, func, reason);
521}
522
523/* ERR_add_error_data_vdata takes a variable number of const char* pointers,
524 * concatenates them and sets the result as the data on the most recent
525 * error. */
526static void err_add_error_vdata(unsigned num, va_list args) {
527 size_t alloced, new_len, len = 0, substr_len;
528 char *buf;
529 const char *substr;
530 unsigned i;
531
532 alloced = 80;
533 buf = OPENSSL_malloc(alloced + 1);
534 if (buf == NULL) {
535 return;
536 }
537
538 for (i = 0; i < num; i++) {
539 substr = va_arg(args, const char *);
540 if (substr == NULL) {
541 continue;
542 }
543
544 substr_len = strlen(substr);
545 new_len = len + substr_len;
546 if (new_len > alloced) {
547 char *new_buf;
548
549 if (alloced + 20 + 1 < alloced) {
550 /* overflow. */
551 OPENSSL_free(buf);
552 return;
553 }
554
555 alloced = new_len + 20;
556 new_buf = OPENSSL_realloc(buf, alloced + 1);
557 if (new_buf == NULL) {
558 OPENSSL_free(buf);
559 return;
560 }
561 buf = new_buf;
562 }
563
564 memcpy(buf + len, substr, substr_len);
565 len = new_len;
566 }
567
568 buf[len] = 0;
569 err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
570}
571
572void ERR_add_error_data(unsigned count, ...) {
573 va_list args;
574 va_start(args, count);
575 err_add_error_vdata(count, args);
576 va_end(args);
577}
578
579void ERR_add_error_dataf(const char *format, ...) {
580 va_list ap;
581 char *buf;
582 static const unsigned buf_len = 256;
583
584 /* A fixed-size buffer is used because va_copy (which would be needed in
585 * order to call vsnprintf twice and measure the buffer) wasn't defined until
586 * C99. */
587 buf = OPENSSL_malloc(buf_len + 1);
588 if (buf == NULL) {
589 return;
590 }
591
592 va_start(ap, format);
593 BIO_vsnprintf(buf, buf_len, format, ap);
594 buf[buf_len] = 0;
595 va_end(ap);
596
597 err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
598}
599
600int ERR_set_mark(void) {
601 ERR_STATE *const state = err_get_state();
602
603 if (state->bottom == state->top) {
604 return 0;
605 }
606 state->errors[state->top].flags |= ERR_FLAG_MARK;
607 return 1;
608}
609
610int ERR_pop_to_mark(void) {
611 ERR_STATE *const state = err_get_state();
612 struct err_error_st *error;
613
614 while (state->bottom != state->top) {
615 error = &state->errors[state->top];
616
617 if ((error->flags & ERR_FLAG_MARK) != 0) {
618 break;
619 }
620
621 err_clear(error);
622 if (state->top == 0) {
623 state->top = ERR_NUM_ERRORS - 1;
624 } else {
625 state->top--;
626 }
627 }
628
629 if (state->bottom == state->top) {
630 return 0;
631 }
632
633 error->flags &= ~ERR_FLAG_MARK;
634 return 1;
635}
636
637static const char *const kLibraryNames[ERR_NUM_LIBS] = {
638 "invalid library (0)",
639 "unknown library", /* ERR_LIB_NONE */
640 "system library", /* ERR_LIB_SYS */
641 "bignum routines", /* ERR_LIB_BN */
642 "RSA routines", /* ERR_LIB_RSA */
643 "Diffie-Hellman routines", /* ERR_LIB_DH */
644 "public key routines", /* ERR_LIB_EVP */
645 "memory buffer routines", /* ERR_LIB_BUF */
646 "object identifier routines", /* ERR_LIB_OBJ */
647 "PEM routines", /* ERR_LIB_PEM */
648 "DSA routines", /* ERR_LIB_DSA */
649 "X.509 certificate routines", /* ERR_LIB_X509 */
650 "ASN.1 encoding routines", /* ERR_LIB_ASN1 */
651 "configuration file routines", /* ERR_LIB_CONF */
652 "common libcrypto routines", /* ERR_LIB_CRYPTO */
653 "elliptic curve routines", /* ERR_LIB_EC */
654 "SSL routines", /* ERR_LIB_SSL */
655 "BIO routines", /* ERR_LIB_BIO */
656 "PKCS7 routines", /* ERR_LIB_PKCS7 */
657 "PKCS8 routines", /* ERR_LIB_PKCS8 */
658 "X509 V3 routines", /* ERR_LIB_X509V3 */
Adam Langley95c29f32014-06-20 12:00:00 -0700659 "random number generator", /* ERR_LIB_RAND */
660 "ENGINE routines", /* ERR_LIB_ENGINE */
661 "OCSP routines", /* ERR_LIB_OCSP */
662 "UI routines", /* ERR_LIB_UI */
663 "COMP routines", /* ERR_LIB_COMP */
664 "ECDSA routines", /* ERR_LIB_ECDSA */
665 "ECDH routines", /* ERR_LIB_ECDH */
666 "HMAC routines", /* ERR_LIB_HMAC */
667 "Digest functions", /* ERR_LIB_DIGEST */
668 "Cipher functions", /* ERR_LIB_CIPHER */
669 "User defined functions", /* ERR_LIB_USER */
670};
671
672#define NUM_SYS_ERRNOS 127
673
674/* kStaticErrors provides storage for ERR_STRING_DATA values that are created
675 * at init time because we assume that ERR_STRING_DATA structures aren't
676 * allocated on the heap. */
677static ERR_STRING_DATA kStaticErrors[ERR_NUM_LIBS * 2 + NUM_SYS_ERRNOS];
678
679static ERR_STRING_DATA kGlobalErrors[] = {
680 {ERR_R_MALLOC_FAILURE, "malloc failure"},
681 {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "function should not be called"},
682 {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
683 {ERR_R_INTERNAL_ERROR, "internal error"},
Adam Langley66014022014-08-13 10:31:04 -0700684 {ERR_R_OVERFLOW, "overflow"},
Adam Langley95c29f32014-06-20 12:00:00 -0700685
686 {ERR_PACK(ERR_LIB_SYS, SYS_F_fopen, 0), "fopen"},
687 {ERR_PACK(ERR_LIB_SYS, SYS_F_fclose, 0), "fclose"},
688 {ERR_PACK(ERR_LIB_SYS, SYS_F_fread, 0), "fread"},
689 {ERR_PACK(ERR_LIB_SYS, SYS_F_fwrite, 0), "fwrite"},
690 {ERR_PACK(ERR_LIB_SYS, SYS_F_socket, 0), "socket"},
691 {ERR_PACK(ERR_LIB_SYS, SYS_F_setsockopt, 0), "setsockopt"},
692 {ERR_PACK(ERR_LIB_SYS, SYS_F_connect, 0), "connect"},
693 {ERR_PACK(ERR_LIB_SYS, SYS_F_getaddrinfo, 0), "getaddrinfo"},
694
695 {0, NULL},
696};
697
698
699extern const ERR_STRING_DATA ASN1_error_string_data[];
700extern const ERR_STRING_DATA BIO_error_string_data[];
701extern const ERR_STRING_DATA BN_error_string_data[];
702extern const ERR_STRING_DATA BUF_error_string_data[];
703extern const ERR_STRING_DATA CIPHER_error_string_data[];
704extern const ERR_STRING_DATA CONF_error_string_data[];
705extern const ERR_STRING_DATA CRYPTO_error_string_data[];
706extern const ERR_STRING_DATA DH_error_string_data[];
707extern const ERR_STRING_DATA DIGEST_error_string_data[];
708extern const ERR_STRING_DATA DSA_error_string_data[];
709extern const ERR_STRING_DATA ECDH_error_string_data[];
710extern const ERR_STRING_DATA ECDSA_error_string_data[];
711extern const ERR_STRING_DATA EC_error_string_data[];
Adam Langley7ea84812014-10-09 16:26:09 -0700712extern const ERR_STRING_DATA ENGINE_error_string_data[];
Adam Langley95c29f32014-06-20 12:00:00 -0700713extern const ERR_STRING_DATA EVP_error_string_data[];
714extern const ERR_STRING_DATA OBJ_error_string_data[];
715extern const ERR_STRING_DATA PEM_error_string_data[];
Adam Langley8e16b6e2014-08-21 14:11:39 -0700716extern const ERR_STRING_DATA PKCS8_error_string_data[];
Adam Langley95c29f32014-06-20 12:00:00 -0700717extern const ERR_STRING_DATA RSA_error_string_data[];
718extern const ERR_STRING_DATA X509V3_error_string_data[];
719extern const ERR_STRING_DATA X509_error_string_data[];
720
721static void err_load_strings(void) {
722 unsigned i, j = 0;
723
724 err_fns_check();
725
726 /* This loop loads strings for the libraries for the ERR_R_*_LIB
727 * reasons. */
728 for (i = ERR_LIB_NONE; i < ERR_NUM_LIBS; i++) {
729 ERR_STRING_DATA *data = &kStaticErrors[j++];
730 data->string = kLibraryNames[i];
731 data->error = ERR_PACK(i, 0, 0);
732 ERRFN(set_item)(data);
733
734 data = &kStaticErrors[j++];
735 data->string = kLibraryNames[i];
736 data->error = ERR_PACK(0, 0, i);
737 ERRFN(set_item)(data);
738 }
739
740 for (i = 1; i < 1 + NUM_SYS_ERRNOS; i++) {
741 /* The "SYS" library sets errno values as the reason for its errors.
742 * Thus we load the first |NUM_SYS_ERRNOS| errno strings as the
743 * reason strings for that library. */
744
745 ERR_STRING_DATA *data = &kStaticErrors[j++];
746 data->string = strerror(i);
747 data->error = ERR_PACK(ERR_LIB_SYS, 0, i);
748 ERRFN(set_item)(data);
749 }
750
751 ERR_load_strings(kGlobalErrors);
752
753 ERR_load_strings(ASN1_error_string_data);
754 ERR_load_strings(BIO_error_string_data);
755 ERR_load_strings(BN_error_string_data);
756 ERR_load_strings(BUF_error_string_data);
757 ERR_load_strings(CIPHER_error_string_data);
758 ERR_load_strings(CONF_error_string_data);
759 ERR_load_strings(CRYPTO_error_string_data);
760 ERR_load_strings(DH_error_string_data);
761 ERR_load_strings(DIGEST_error_string_data);
762 ERR_load_strings(DSA_error_string_data);
763 ERR_load_strings(ECDH_error_string_data);
764 ERR_load_strings(ECDSA_error_string_data);
765 ERR_load_strings(EC_error_string_data);
Adam Langley7ea84812014-10-09 16:26:09 -0700766 ERR_load_strings(ENGINE_error_string_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700767 ERR_load_strings(EVP_error_string_data);
768 ERR_load_strings(OBJ_error_string_data);
769 ERR_load_strings(PEM_error_string_data);
Adam Langley8e16b6e2014-08-21 14:11:39 -0700770 ERR_load_strings(PKCS8_error_string_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700771 ERR_load_strings(RSA_error_string_data);
772 ERR_load_strings(X509V3_error_string_data);
773 ERR_load_strings(X509_error_string_data);
774}
775
776void ERR_load_strings(const ERR_STRING_DATA *str) {
777 err_fns_check();
778
779 while (str->error) {
780 ERRFN(set_item)(str);
781 str++;
782 }
783}
784
David Benjaminc44d2f42014-08-20 16:24:00 -0400785void ERR_load_crypto_strings(void) { err_load_strings(); }
Adam Langley95c29f32014-06-20 12:00:00 -0700786
David Benjaminc44d2f42014-08-20 16:24:00 -0400787void ERR_free_strings(void) {
Adam Langley95c29f32014-06-20 12:00:00 -0700788 err_fns_check();
789 ERRFN(shutdown)();
790}
791
David Benjaminc44d2f42014-08-20 16:24:00 -0400792void ERR_load_BIO_strings(void) {}