]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/err/err_prn.c
ERR: special case system errors
[thirdparty/openssl.git] / crypto / err / err_prn.c
CommitLineData
aa6bb135 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4ad239b8 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
d02b48c6
RE
8 */
9
14e275e8
RL
10/* TODO: When ERR_STATE becomes opaque, this musts be removed */
11#define OSSL_FORCE_ERR_STATE
12
d02b48c6 13#include <stdio.h>
b39fc560 14#include "internal/cryptlib.h"
ec577822 15#include <openssl/crypto.h>
ec577822
BM
16#include <openssl/buffer.h>
17#include <openssl/err.h>
706457b7 18#include "err_local.h"
d02b48c6 19
31b28ad9 20#define ERR_PRINT_BUF_SIZE 4096
0f113f3e
MC
21void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
22 void *u)
23{
2d905f67 24 CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
0f113f3e 25 unsigned long l;
b579014d 26 const char *file, *data, *func;
0f113f3e 27 int line, flags;
d02b48c6 28
b579014d 29 while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
71f2994b
RL
30 char buf[ERR_PRINT_BUF_SIZE], *hex;
31 const char *lib, *reason = NULL;
32 char rsbuf[256];
33 unsigned long r = ERR_GET_REASON(l);
34
b579014d 35 lib = ERR_lib_error_string(l);
71f2994b
RL
36
37 /*
38 * ERR_reason_error_string() can't safely return system error strings,
39 * since it would call openssl_strerror_r(), which needs a buffer for
40 * thread safety. So for system errors, we call openssl_strerror_r()
41 * directly instead.
42 */
43 if (ERR_SYSTEM_ERROR(l)) {
44 if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
45 reason = rsbuf;
46 } else {
47 reason = ERR_reason_error_string(l);
48 }
49
b579014d
RL
50 if (func == NULL)
51 func = "unknown function";
71f2994b
RL
52 if (reason == NULL) {
53 BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
54 reason = rsbuf;
55 }
b579014d
RL
56 if ((flags & ERR_TXT_STRING) == 0)
57 data = "";
f32af93c
SL
58 hex = openssl_buf2hexstr_sep((const unsigned char *)&tid, sizeof(tid),
59 '\0');
60 BIO_snprintf(buf, sizeof(buf), "%s:error::%s:%s:%s:%s:%d:%s\n",
17197a2f
MC
61 hex == NULL ? "<null>" : hex, lib, func, reason, file,
62 line, data);
2d905f67 63 OPENSSL_free(hex);
b579014d 64 if (cb(buf, strlen(buf), u) <= 0)
0f113f3e
MC
65 break; /* abort outputting the error report */
66 }
67}
8ada6e77 68
31b28ad9
DDO
69/* auxiliary function for incrementally reporting texts via the error queue */
70static void put_error(int lib, const char *func, int reason,
71 const char *file, int line)
72{
73 ERR_new();
74 ERR_set_debug(file, line, func);
75 ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
76}
77
78#define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
79#define MAX_DATA_LEN (ERR_PRINT_BUF_SIZE - TYPICAL_MAX_OUTPUT_BEFORE_DATA)
80void ERR_add_error_txt(const char *separator, const char *txt)
81{
82 const char *file = NULL;
83 int line;
84 const char *func = NULL;
85 const char *data = NULL;
86 int flags;
87 unsigned long err = ERR_peek_last_error();
88
89 if (separator == NULL)
90 separator = "";
91 if (err == 0)
4c7f8981 92 put_error(ERR_LIB_NONE, NULL, 0, "", 0);
31b28ad9
DDO
93
94 do {
95 size_t available_len, data_len;
96 const char *curr = txt, *next = txt;
97 const char *leading_separator = separator;
98 int trailing_separator = 0;
99 char *tmp;
100
101 ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
102 if ((flags & ERR_TXT_STRING) == 0) {
103 data = "";
104 leading_separator = "";
105 }
106 data_len = strlen(data);
107
108 /* workaround for limit of ERR_print_errors_cb() */
109 if (data_len >= MAX_DATA_LEN
110 || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
111 available_len = 0;
112 else
113 available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
114 /* MAX_DATA_LEN > available_len >= 0 */
115
116 if (*separator == '\0') {
117 const size_t len_next = strlen(next);
118
119 if (len_next <= available_len) {
120 next += len_next;
121 curr = NULL; /* no need to split */
122 } else {
123 next += available_len;
124 curr = next; /* will split at this point */
125 }
126 } else {
127 while (*next != '\0' && (size_t)(next - txt) <= available_len) {
128 curr = next;
129 next = strstr(curr, separator);
130 if (next != NULL) {
131 next += strlen(separator);
132 trailing_separator = *next == '\0';
133 } else {
134 next = curr + strlen(curr);
135 }
136 }
137 if ((size_t)(next - txt) <= available_len)
138 curr = NULL; /* the above loop implies *next == '\0' */
139 }
140 if (curr != NULL) {
141 /* split error msg at curr since error data would get too long */
142 if (curr != txt) {
143 tmp = OPENSSL_strndup(txt, curr - txt);
144 if (tmp == NULL)
145 return;
146 ERR_add_error_data(2, separator, tmp);
147 OPENSSL_free(tmp);
148 }
4c7f8981 149 put_error(ERR_GET_LIB(err), func, err, file, line);
31b28ad9
DDO
150 txt = curr;
151 } else {
152 if (trailing_separator) {
153 tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
154 if (tmp == NULL)
155 return;
156 /* output txt without the trailing separator */
157 ERR_add_error_data(2, leading_separator, tmp);
158 OPENSSL_free(tmp);
159 } else {
160 ERR_add_error_data(2, leading_separator, txt);
161 }
162 txt = next; /* finished */
163 }
164 } while (*txt != '\0');
165}
166
167void ERR_add_error_mem_bio(const char *separator, BIO *bio)
168{
169 if (bio != NULL) {
170 char *str;
171 long len = BIO_get_mem_data(bio, &str);
172
173 if (len > 0) {
174 if (str[len - 1] != '\0') {
175 if (BIO_write(bio, "", 1) <= 0)
176 return;
177
178 len = BIO_get_mem_data(bio, &str);
179 }
180 if (len > 1)
181 ERR_add_error_txt(separator, str);
182 }
183 }
184}
185
6a184a60 186static int print_bio(const char *str, size_t len, void *bp)
0f113f3e
MC
187{
188 return BIO_write((BIO *)bp, str, len);
189}
d02b48c6 190
0f113f3e
MC
191void ERR_print_errors(BIO *bp)
192{
193 ERR_print_errors_cb(print_bio, bp);
194}
1a50b813
F
195
196#ifndef OPENSSL_NO_STDIO
197void ERR_print_errors_fp(FILE *fp)
198{
199 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
200 if (bio == NULL)
201 return;
202
203 ERR_print_errors_cb(print_bio, bio);
204 BIO_free(bio);
205}
206#endif