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