2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #define OSSL_FORCE_ERR_STATE
13 #include "internal/cryptlib.h"
14 #include <openssl/crypto.h>
15 #include <openssl/buffer.h>
16 #include <openssl/err.h>
17 #include "err_local.h"
19 #define ERR_PRINT_BUF_SIZE 4096
20 void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
23 CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
25 const char *file, *data, *func;
28 while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
29 char buf[ERR_PRINT_BUF_SIZE] = "";
33 if ((flags & ERR_TXT_STRING) == 0)
36 hex = ossl_buf2hexstr_sep((const unsigned char *)&tid, sizeof(tid), '\0');
37 BIO_snprintf(buf, sizeof(buf), "%s:", hex == NULL ? "<null>" : hex);
39 ossl_err_string_int(l, func, buf + offset, sizeof(buf) - offset);
40 offset += strlen(buf + offset);
41 BIO_snprintf(buf + offset, sizeof(buf) - offset, ":%s:%d:%s\n",
44 if (cb(buf, strlen(buf), u) <= 0)
45 break; /* abort outputting the error report */
49 /* auxiliary function for incrementally reporting texts via the error queue */
50 static void put_error(int lib, const char *func, int reason,
51 const char *file, int line)
54 ERR_set_debug(file, line, func);
55 ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
58 #define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
59 #define MAX_DATA_LEN (ERR_PRINT_BUF_SIZE - TYPICAL_MAX_OUTPUT_BEFORE_DATA)
60 void ERR_add_error_txt(const char *separator, const char *txt)
62 const char *file = NULL;
64 const char *func = NULL;
65 const char *data = NULL;
67 unsigned long err = ERR_peek_last_error();
69 if (separator == NULL)
72 put_error(ERR_LIB_NONE, NULL, 0, "", 0);
75 size_t available_len, data_len;
76 const char *curr = txt, *next = txt;
77 const char *leading_separator = separator;
78 int trailing_separator = 0;
81 ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
82 if ((flags & ERR_TXT_STRING) == 0) {
84 leading_separator = "";
86 data_len = strlen(data);
88 /* workaround for limit of ERR_print_errors_cb() */
89 if (data_len >= MAX_DATA_LEN
90 || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
93 available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
94 /* MAX_DATA_LEN > available_len >= 0 */
96 if (*separator == '\0') {
97 const size_t len_next = strlen(next);
99 if (len_next <= available_len) {
101 curr = NULL; /* no need to split */
103 next += available_len;
104 curr = next; /* will split at this point */
107 while (*next != '\0' && (size_t)(next - txt) <= available_len) {
109 next = strstr(curr, separator);
111 next += strlen(separator);
112 trailing_separator = *next == '\0';
114 next = curr + strlen(curr);
117 if ((size_t)(next - txt) <= available_len)
118 curr = NULL; /* the above loop implies *next == '\0' */
121 /* split error msg at curr since error data would get too long */
123 tmp = OPENSSL_strndup(txt, curr - txt);
126 ERR_add_error_data(2, separator, tmp);
129 put_error(ERR_GET_LIB(err), func, err, file, line);
132 if (trailing_separator) {
133 tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
136 /* output txt without the trailing separator */
137 ERR_add_error_data(2, leading_separator, tmp);
140 ERR_add_error_data(2, leading_separator, txt);
142 txt = next; /* finished */
144 } while (*txt != '\0');
147 void ERR_add_error_mem_bio(const char *separator, BIO *bio)
151 long len = BIO_get_mem_data(bio, &str);
154 if (str[len - 1] != '\0') {
155 if (BIO_write(bio, "", 1) <= 0)
158 len = BIO_get_mem_data(bio, &str);
161 ERR_add_error_txt(separator, str);
166 static int print_bio(const char *str, size_t len, void *bp)
168 return BIO_write((BIO *)bp, str, len);
171 void ERR_print_errors(BIO *bp)
173 ERR_print_errors_cb(print_bio, bp);
176 #ifndef OPENSSL_NO_STDIO
177 void ERR_print_errors_fp(FILE *fp)
179 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
183 ERR_print_errors_cb(print_bio, bio);