/src/openssl/crypto/err/err.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <stdarg.h> |
12 | | #include <string.h> |
13 | | #include "internal/cryptlib_int.h" |
14 | | #include "internal/err.h" |
15 | | #include "internal/err_int.h" |
16 | | #include <openssl/err.h> |
17 | | #include <openssl/crypto.h> |
18 | | #include <openssl/buffer.h> |
19 | | #include <openssl/bio.h> |
20 | | #include <openssl/opensslconf.h> |
21 | | #include "internal/thread_once.h" |
22 | | |
23 | | static int err_load_strings(const ERR_STRING_DATA *str); |
24 | | |
25 | | static void ERR_STATE_free(ERR_STATE *s); |
26 | | #ifndef OPENSSL_NO_ERR |
27 | | static ERR_STRING_DATA ERR_str_libraries[] = { |
28 | | {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"}, |
29 | | {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"}, |
30 | | {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"}, |
31 | | {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"}, |
32 | | {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"}, |
33 | | {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"}, |
34 | | {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"}, |
35 | | {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"}, |
36 | | {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"}, |
37 | | {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"}, |
38 | | {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"}, |
39 | | {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"}, |
40 | | {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"}, |
41 | | {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"}, |
42 | | {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"}, |
43 | | {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"}, |
44 | | {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"}, |
45 | | {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"}, |
46 | | {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"}, |
47 | | {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"}, |
48 | | {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"}, |
49 | | {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"}, |
50 | | {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"}, |
51 | | {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"}, |
52 | | {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"}, |
53 | | {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"}, |
54 | | {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"}, |
55 | | {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"}, |
56 | | {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"}, |
57 | | {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"}, |
58 | | {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"}, |
59 | | {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"}, |
60 | | {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"}, |
61 | | {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"}, |
62 | | {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"}, |
63 | | {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"}, |
64 | | {0, NULL}, |
65 | | }; |
66 | | |
67 | | static ERR_STRING_DATA ERR_str_functs[] = { |
68 | | {ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"}, |
69 | | {ERR_PACK(0, SYS_F_CONNECT, 0), "connect"}, |
70 | | {ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"}, |
71 | | {ERR_PACK(0, SYS_F_SOCKET, 0), "socket"}, |
72 | | {ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"}, |
73 | | {ERR_PACK(0, SYS_F_BIND, 0), "bind"}, |
74 | | {ERR_PACK(0, SYS_F_LISTEN, 0), "listen"}, |
75 | | {ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"}, |
76 | | # ifdef OPENSSL_SYS_WINDOWS |
77 | | {ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"}, |
78 | | # endif |
79 | | {ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"}, |
80 | | {ERR_PACK(0, SYS_F_FREAD, 0), "fread"}, |
81 | | {ERR_PACK(0, SYS_F_GETADDRINFO, 0), "getaddrinfo"}, |
82 | | {ERR_PACK(0, SYS_F_GETNAMEINFO, 0), "getnameinfo"}, |
83 | | {ERR_PACK(0, SYS_F_SETSOCKOPT, 0), "setsockopt"}, |
84 | | {ERR_PACK(0, SYS_F_GETSOCKOPT, 0), "getsockopt"}, |
85 | | {ERR_PACK(0, SYS_F_GETSOCKNAME, 0), "getsockname"}, |
86 | | {ERR_PACK(0, SYS_F_GETHOSTBYNAME, 0), "gethostbyname"}, |
87 | | {ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"}, |
88 | | {ERR_PACK(0, SYS_F_OPEN, 0), "open"}, |
89 | | {ERR_PACK(0, SYS_F_CLOSE, 0), "close"}, |
90 | | {ERR_PACK(0, SYS_F_IOCTL, 0), "ioctl"}, |
91 | | {ERR_PACK(0, SYS_F_STAT, 0), "stat"}, |
92 | | {ERR_PACK(0, SYS_F_FCNTL, 0), "fcntl"}, |
93 | | {ERR_PACK(0, SYS_F_FSTAT, 0), "fstat"}, |
94 | | {0, NULL}, |
95 | | }; |
96 | | |
97 | | static ERR_STRING_DATA ERR_str_reasons[] = { |
98 | | {ERR_R_SYS_LIB, "system lib"}, |
99 | | {ERR_R_BN_LIB, "BN lib"}, |
100 | | {ERR_R_RSA_LIB, "RSA lib"}, |
101 | | {ERR_R_DH_LIB, "DH lib"}, |
102 | | {ERR_R_EVP_LIB, "EVP lib"}, |
103 | | {ERR_R_BUF_LIB, "BUF lib"}, |
104 | | {ERR_R_OBJ_LIB, "OBJ lib"}, |
105 | | {ERR_R_PEM_LIB, "PEM lib"}, |
106 | | {ERR_R_DSA_LIB, "DSA lib"}, |
107 | | {ERR_R_X509_LIB, "X509 lib"}, |
108 | | {ERR_R_ASN1_LIB, "ASN1 lib"}, |
109 | | {ERR_R_EC_LIB, "EC lib"}, |
110 | | {ERR_R_BIO_LIB, "BIO lib"}, |
111 | | {ERR_R_PKCS7_LIB, "PKCS7 lib"}, |
112 | | {ERR_R_X509V3_LIB, "X509V3 lib"}, |
113 | | {ERR_R_ENGINE_LIB, "ENGINE lib"}, |
114 | | {ERR_R_UI_LIB, "UI lib"}, |
115 | | {ERR_R_OSSL_STORE_LIB, "STORE lib"}, |
116 | | {ERR_R_ECDSA_LIB, "ECDSA lib"}, |
117 | | |
118 | | {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"}, |
119 | | {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"}, |
120 | | |
121 | | {ERR_R_FATAL, "fatal"}, |
122 | | {ERR_R_MALLOC_FAILURE, "malloc failure"}, |
123 | | {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, |
124 | | "called a function you should not call"}, |
125 | | {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"}, |
126 | | {ERR_R_INTERNAL_ERROR, "internal error"}, |
127 | | {ERR_R_DISABLED, "called a function that was disabled at compile-time"}, |
128 | | {ERR_R_INIT_FAIL, "init fail"}, |
129 | | {ERR_R_OPERATION_FAIL, "operation fail"}, |
130 | | |
131 | | {0, NULL}, |
132 | | }; |
133 | | #endif |
134 | | |
135 | | static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT; |
136 | | static int set_err_thread_local; |
137 | | static CRYPTO_THREAD_LOCAL err_thread_local; |
138 | | |
139 | | static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT; |
140 | | static CRYPTO_RWLOCK *err_string_lock; |
141 | | |
142 | | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); |
143 | | |
144 | | /* |
145 | | * The internal state |
146 | | */ |
147 | | |
148 | | static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; |
149 | | static int int_err_library_number = ERR_LIB_USER; |
150 | | |
151 | | static unsigned long get_error_values(int inc, int top, const char **file, |
152 | | int *line, const char **data, |
153 | | int *flags); |
154 | | |
155 | | static unsigned long err_string_data_hash(const ERR_STRING_DATA *a) |
156 | 1.95M | { |
157 | 1.95M | unsigned long ret, l; |
158 | 1.95M | |
159 | 1.95M | l = a->error; |
160 | 1.95M | ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l); |
161 | 1.95M | return (ret ^ ret % 19 * 13); |
162 | 1.95M | } |
163 | | |
164 | | static int err_string_data_cmp(const ERR_STRING_DATA *a, |
165 | | const ERR_STRING_DATA *b) |
166 | 1.83M | { |
167 | 1.83M | if (a->error == b->error) |
168 | 1.61M | return 0; |
169 | 220k | return a->error > b->error ? 1 : -1; |
170 | 220k | } |
171 | | |
172 | | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d) |
173 | 1.89M | { |
174 | 1.89M | ERR_STRING_DATA *p = NULL; |
175 | 1.89M | |
176 | 1.89M | CRYPTO_THREAD_read_lock(err_string_lock); |
177 | 1.89M | p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d); |
178 | 1.89M | CRYPTO_THREAD_unlock(err_string_lock); |
179 | 1.89M | |
180 | 1.89M | return p; |
181 | 1.89M | } |
182 | | |
183 | | #ifndef OPENSSL_NO_ERR |
184 | 1.02k | # define NUM_SYS_STR_REASONS 127 |
185 | | # define LEN_SYS_STR_REASON 32 |
186 | | |
187 | | static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1]; |
188 | | /* |
189 | | * SYS_str_reasons is filled with copies of strerror() results at |
190 | | * initialization. 'errno' values up to 127 should cover all usual errors, |
191 | | * others will be displayed numerically by ERR_error_string. It is crucial |
192 | | * that we have something for each reason code that occurs in |
193 | | * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(), |
194 | | * which always gets an errno value and never one of those 'standard' reason |
195 | | * codes. |
196 | | */ |
197 | | |
198 | | static void build_SYS_str_reasons(void) |
199 | 472 | { |
200 | 472 | /* OPENSSL_malloc cannot be used here, use static storage instead */ |
201 | 472 | static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON]; |
202 | 472 | static int init = 1; |
203 | 472 | int i; |
204 | 472 | |
205 | 472 | CRYPTO_THREAD_write_lock(err_string_lock); |
206 | 472 | if (!init) { |
207 | 464 | CRYPTO_THREAD_unlock(err_string_lock); |
208 | 464 | return; |
209 | 464 | } |
210 | 8 | |
211 | 1.02k | for (i = 1; i <= NUM_SYS_STR_REASONS; i++) { |
212 | 1.01k | ERR_STRING_DATA *str = &SYS_str_reasons[i - 1]; |
213 | 1.01k | |
214 | 1.01k | str->error = ERR_PACK(ERR_LIB_SYS, 0, i); |
215 | 1.01k | if (str->string == NULL) { |
216 | 1.01k | char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]); |
217 | 1.01k | if (openssl_strerror_r(i, *dest, sizeof(*dest))) |
218 | 864 | str->string = *dest; |
219 | 1.01k | } |
220 | 1.01k | if (str->string == NULL) |
221 | 1.01k | str->string = "unknown"; |
222 | 1.01k | } |
223 | 8 | |
224 | 8 | /* |
225 | 8 | * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as |
226 | 8 | * required by ERR_load_strings. |
227 | 8 | */ |
228 | 8 | |
229 | 8 | init = 0; |
230 | 8 | |
231 | 8 | CRYPTO_THREAD_unlock(err_string_lock); |
232 | 8 | err_load_strings(SYS_str_reasons); |
233 | 8 | } |
234 | | #endif |
235 | | |
236 | | #define err_clear_data(p, i) \ |
237 | 1.21M | do { \ |
238 | 1.21M | if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\ |
239 | 168k | OPENSSL_free((p)->err_data[i]); \ |
240 | 168k | (p)->err_data[i] = NULL; \ |
241 | 168k | } \ |
242 | 1.21M | (p)->err_data_flags[i] = 0; \ |
243 | 1.21M | } while (0) |
244 | | |
245 | | #define err_clear(p, i) \ |
246 | 0 | do { \ |
247 | 0 | err_clear_data(p, i); \ |
248 | 0 | (p)->err_flags[i] = 0; \ |
249 | 0 | (p)->err_buffer[i] = 0; \ |
250 | 0 | (p)->err_file[i] = NULL; \ |
251 | 0 | (p)->err_line[i] = -1; \ |
252 | 0 | } while (0) |
253 | | |
254 | | static void ERR_STATE_free(ERR_STATE *s) |
255 | 8 | { |
256 | 8 | int i; |
257 | 8 | |
258 | 8 | if (s == NULL) |
259 | 8 | return; |
260 | 136 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
261 | 128 | err_clear_data(s, i); |
262 | 128 | } |
263 | 8 | OPENSSL_free(s); |
264 | 8 | } |
265 | | |
266 | | DEFINE_RUN_ONCE_STATIC(do_err_strings_init) |
267 | 8 | { |
268 | 8 | if (!OPENSSL_init_crypto(0, NULL)) |
269 | 0 | return 0; |
270 | 8 | err_string_lock = CRYPTO_THREAD_lock_new(); |
271 | 8 | if (err_string_lock == NULL) |
272 | 8 | return 0; |
273 | 8 | int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash, |
274 | 8 | err_string_data_cmp); |
275 | 8 | if (int_error_hash == NULL) { |
276 | 0 | CRYPTO_THREAD_lock_free(err_string_lock); |
277 | 0 | err_string_lock = NULL; |
278 | 0 | return 0; |
279 | 0 | } |
280 | 8 | return 1; |
281 | 8 | } |
282 | | |
283 | | void err_cleanup(void) |
284 | 8 | { |
285 | 8 | if (set_err_thread_local != 0) |
286 | 8 | CRYPTO_THREAD_cleanup_local(&err_thread_local); |
287 | 8 | CRYPTO_THREAD_lock_free(err_string_lock); |
288 | 8 | err_string_lock = NULL; |
289 | 8 | lh_ERR_STRING_DATA_free(int_error_hash); |
290 | 8 | int_error_hash = NULL; |
291 | 8 | } |
292 | | |
293 | | /* |
294 | | * Legacy; pack in the library. |
295 | | */ |
296 | | static void err_patch(int lib, ERR_STRING_DATA *str) |
297 | 472 | { |
298 | 472 | unsigned long plib = ERR_PACK(lib, 0, 0); |
299 | 472 | |
300 | 11.3k | for (; str->error != 0; str++) |
301 | 10.8k | str->error |= plib; |
302 | 472 | } |
303 | | |
304 | | /* |
305 | | * Hash in |str| error strings. Assumes the URN_ONCE was done. |
306 | | */ |
307 | | static int err_load_strings(const ERR_STRING_DATA *str) |
308 | 1.88k | { |
309 | 1.88k | CRYPTO_THREAD_write_lock(err_string_lock); |
310 | 61.9k | for (; str->error; str++) |
311 | 60.0k | (void)lh_ERR_STRING_DATA_insert(int_error_hash, |
312 | 60.0k | (ERR_STRING_DATA *)str); |
313 | 1.88k | CRYPTO_THREAD_unlock(err_string_lock); |
314 | 1.88k | return 1; |
315 | 1.88k | } |
316 | | |
317 | | int ERR_load_ERR_strings(void) |
318 | 472 | { |
319 | 472 | #ifndef OPENSSL_NO_ERR |
320 | 472 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
321 | 472 | return 0; |
322 | 472 | |
323 | 472 | err_load_strings(ERR_str_libraries); |
324 | 472 | err_load_strings(ERR_str_reasons); |
325 | 472 | err_patch(ERR_LIB_SYS, ERR_str_functs); |
326 | 472 | err_load_strings(ERR_str_functs); |
327 | 472 | build_SYS_str_reasons(); |
328 | 472 | #endif |
329 | 472 | return 1; |
330 | 472 | } |
331 | | |
332 | | int ERR_load_strings(int lib, ERR_STRING_DATA *str) |
333 | 0 | { |
334 | 0 | if (ERR_load_ERR_strings() == 0) |
335 | 0 | return 0; |
336 | 0 | |
337 | 0 | err_patch(lib, str); |
338 | 0 | err_load_strings(str); |
339 | 0 | return 1; |
340 | 0 | } |
341 | | |
342 | | int ERR_load_strings_const(const ERR_STRING_DATA *str) |
343 | 464 | { |
344 | 464 | if (ERR_load_ERR_strings() == 0) |
345 | 0 | return 0; |
346 | 464 | err_load_strings(str); |
347 | 464 | return 1; |
348 | 464 | } |
349 | | |
350 | | int ERR_unload_strings(int lib, ERR_STRING_DATA *str) |
351 | 0 | { |
352 | 0 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
353 | 0 | return 0; |
354 | 0 | |
355 | 0 | CRYPTO_THREAD_write_lock(err_string_lock); |
356 | 0 | /* |
357 | 0 | * We don't need to ERR_PACK the lib, since that was done (to |
358 | 0 | * the table) when it was loaded. |
359 | 0 | */ |
360 | 0 | for (; str->error; str++) |
361 | 0 | (void)lh_ERR_STRING_DATA_delete(int_error_hash, str); |
362 | 0 | CRYPTO_THREAD_unlock(err_string_lock); |
363 | 0 |
|
364 | 0 | return 1; |
365 | 0 | } |
366 | | |
367 | | void err_free_strings_int(void) |
368 | 8 | { |
369 | 8 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
370 | 8 | return; |
371 | 8 | } |
372 | | |
373 | | /********************************************************/ |
374 | | |
375 | | void ERR_put_error(int lib, int func, int reason, const char *file, int line) |
376 | 524k | { |
377 | 524k | ERR_STATE *es; |
378 | 524k | |
379 | | #ifdef _OSD_POSIX |
380 | | /* |
381 | | * In the BS2000-OSD POSIX subsystem, the compiler generates path names |
382 | | * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to |
383 | | * something sensible. @@@ We shouldn't modify a const string, though. |
384 | | */ |
385 | | if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) { |
386 | | char *end; |
387 | | |
388 | | /* Skip the "*POSIX(" prefix */ |
389 | | file += sizeof("*POSIX(") - 1; |
390 | | end = &file[strlen(file) - 1]; |
391 | | if (*end == ')') |
392 | | *end = '\0'; |
393 | | /* Optional: use the basename of the path only. */ |
394 | | if ((end = strrchr(file, '/')) != NULL) |
395 | | file = &end[1]; |
396 | | } |
397 | | #endif |
398 | | es = ERR_get_state(); |
399 | 524k | if (es == NULL) |
400 | 524k | return; |
401 | 524k | |
402 | 524k | es->top = (es->top + 1) % ERR_NUM_ERRORS; |
403 | 524k | if (es->top == es->bottom) |
404 | 0 | es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS; |
405 | 524k | es->err_flags[es->top] = 0; |
406 | 524k | es->err_buffer[es->top] = ERR_PACK(lib, func, reason); |
407 | 524k | es->err_file[es->top] = file; |
408 | 524k | es->err_line[es->top] = line; |
409 | 524k | err_clear_data(es, es->top); |
410 | 524k | } |
411 | | |
412 | | void ERR_clear_error(void) |
413 | 0 | { |
414 | 0 | int i; |
415 | 0 | ERR_STATE *es; |
416 | 0 |
|
417 | 0 | es = ERR_get_state(); |
418 | 0 | if (es == NULL) |
419 | 0 | return; |
420 | 0 | |
421 | 0 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
422 | 0 | err_clear(es, i); |
423 | 0 | } |
424 | 0 | es->top = es->bottom = 0; |
425 | 0 | } |
426 | | |
427 | | unsigned long ERR_get_error(void) |
428 | 620k | { |
429 | 620k | return get_error_values(1, 0, NULL, NULL, NULL, NULL); |
430 | 620k | } |
431 | | |
432 | | unsigned long ERR_get_error_line(const char **file, int *line) |
433 | 0 | { |
434 | 0 | return get_error_values(1, 0, file, line, NULL, NULL); |
435 | 0 | } |
436 | | |
437 | | unsigned long ERR_get_error_line_data(const char **file, int *line, |
438 | | const char **data, int *flags) |
439 | 0 | { |
440 | 0 | return get_error_values(1, 0, file, line, data, flags); |
441 | 0 | } |
442 | | |
443 | | unsigned long ERR_peek_error(void) |
444 | 18.4k | { |
445 | 18.4k | return get_error_values(0, 0, NULL, NULL, NULL, NULL); |
446 | 18.4k | } |
447 | | |
448 | | unsigned long ERR_peek_error_line(const char **file, int *line) |
449 | 0 | { |
450 | 0 | return get_error_values(0, 0, file, line, NULL, NULL); |
451 | 0 | } |
452 | | |
453 | | unsigned long ERR_peek_error_line_data(const char **file, int *line, |
454 | | const char **data, int *flags) |
455 | 0 | { |
456 | 0 | return get_error_values(0, 0, file, line, data, flags); |
457 | 0 | } |
458 | | |
459 | | unsigned long ERR_peek_last_error(void) |
460 | 0 | { |
461 | 0 | return get_error_values(0, 1, NULL, NULL, NULL, NULL); |
462 | 0 | } |
463 | | |
464 | | unsigned long ERR_peek_last_error_line(const char **file, int *line) |
465 | 0 | { |
466 | 0 | return get_error_values(0, 1, file, line, NULL, NULL); |
467 | 0 | } |
468 | | |
469 | | unsigned long ERR_peek_last_error_line_data(const char **file, int *line, |
470 | | const char **data, int *flags) |
471 | 0 | { |
472 | 0 | return get_error_values(0, 1, file, line, data, flags); |
473 | 0 | } |
474 | | |
475 | | static unsigned long get_error_values(int inc, int top, const char **file, |
476 | | int *line, const char **data, |
477 | | int *flags) |
478 | 638k | { |
479 | 638k | int i = 0; |
480 | 638k | ERR_STATE *es; |
481 | 638k | unsigned long ret; |
482 | 638k | |
483 | 638k | es = ERR_get_state(); |
484 | 638k | if (es == NULL) |
485 | 638k | return 0; |
486 | 638k | |
487 | 638k | if (inc && top) { |
488 | 0 | if (file) |
489 | 0 | *file = ""; |
490 | 0 | if (line) |
491 | 0 | *line = 0; |
492 | 0 | if (data) |
493 | 0 | *data = ""; |
494 | 0 | if (flags) |
495 | 0 | *flags = 0; |
496 | 0 |
|
497 | 0 | return ERR_R_INTERNAL_ERROR; |
498 | 0 | } |
499 | 638k | |
500 | 638k | if (es->bottom == es->top) |
501 | 99.5k | return 0; |
502 | 539k | if (top) |
503 | 0 | i = es->top; /* last error */ |
504 | 539k | else |
505 | 539k | i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */ |
506 | 539k | |
507 | 539k | ret = es->err_buffer[i]; |
508 | 539k | if (inc) { |
509 | 524k | es->bottom = i; |
510 | 524k | es->err_buffer[i] = 0; |
511 | 524k | } |
512 | 539k | |
513 | 539k | if (file != NULL && line != NULL) { |
514 | 0 | if (es->err_file[i] == NULL) { |
515 | 0 | *file = "NA"; |
516 | 0 | *line = 0; |
517 | 0 | } else { |
518 | 0 | *file = es->err_file[i]; |
519 | 0 | *line = es->err_line[i]; |
520 | 0 | } |
521 | 0 | } |
522 | 539k | |
523 | 539k | if (data == NULL) { |
524 | 539k | if (inc) { |
525 | 524k | err_clear_data(es, i); |
526 | 524k | } |
527 | 539k | } else { |
528 | 0 | if (es->err_data[i] == NULL) { |
529 | 0 | *data = ""; |
530 | 0 | if (flags != NULL) |
531 | 0 | *flags = 0; |
532 | 0 | } else { |
533 | 0 | *data = es->err_data[i]; |
534 | 0 | if (flags != NULL) |
535 | 0 | *flags = es->err_data_flags[i]; |
536 | 0 | } |
537 | 0 | } |
538 | 539k | return ret; |
539 | 539k | } |
540 | | |
541 | | void ERR_error_string_n(unsigned long e, char *buf, size_t len) |
542 | 0 | { |
543 | 0 | char lsbuf[64], fsbuf[64], rsbuf[64]; |
544 | 0 | const char *ls, *fs, *rs; |
545 | 0 | unsigned long l, f, r; |
546 | 0 |
|
547 | 0 | if (len == 0) |
548 | 0 | return; |
549 | 0 | |
550 | 0 | l = ERR_GET_LIB(e); |
551 | 0 | ls = ERR_lib_error_string(e); |
552 | 0 | if (ls == NULL) { |
553 | 0 | BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l); |
554 | 0 | ls = lsbuf; |
555 | 0 | } |
556 | 0 |
|
557 | 0 | fs = ERR_func_error_string(e); |
558 | 0 | f = ERR_GET_FUNC(e); |
559 | 0 | if (fs == NULL) { |
560 | 0 | BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f); |
561 | 0 | fs = fsbuf; |
562 | 0 | } |
563 | 0 |
|
564 | 0 | rs = ERR_reason_error_string(e); |
565 | 0 | r = ERR_GET_REASON(e); |
566 | 0 | if (rs == NULL) { |
567 | 0 | BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r); |
568 | 0 | rs = rsbuf; |
569 | 0 | } |
570 | 0 |
|
571 | 0 | BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs); |
572 | 0 | if (strlen(buf) == len - 1) { |
573 | 0 | /* Didn't fit; use a minimal format. */ |
574 | 0 | BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r); |
575 | 0 | } |
576 | 0 | } |
577 | | |
578 | | /* |
579 | | * ERR_error_string_n should be used instead for ret != NULL as |
580 | | * ERR_error_string cannot know how large the buffer is |
581 | | */ |
582 | | char *ERR_error_string(unsigned long e, char *ret) |
583 | 0 | { |
584 | 0 | static char buf[256]; |
585 | 0 |
|
586 | 0 | if (ret == NULL) |
587 | 0 | ret = buf; |
588 | 0 | ERR_error_string_n(e, ret, (int)sizeof(buf)); |
589 | 0 | return ret; |
590 | 0 | } |
591 | | |
592 | | const char *ERR_lib_error_string(unsigned long e) |
593 | 524k | { |
594 | 524k | ERR_STRING_DATA d, *p; |
595 | 524k | unsigned long l; |
596 | 524k | |
597 | 524k | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
598 | 0 | return NULL; |
599 | 0 | } |
600 | 524k | |
601 | 524k | l = ERR_GET_LIB(e); |
602 | 524k | d.error = ERR_PACK(l, 0, 0); |
603 | 524k | p = int_err_get_item(&d); |
604 | 524k | return ((p == NULL) ? NULL : p->string); |
605 | 524k | } |
606 | | |
607 | | const char *ERR_func_error_string(unsigned long e) |
608 | 524k | { |
609 | 524k | ERR_STRING_DATA d, *p; |
610 | 524k | unsigned long l, f; |
611 | 524k | |
612 | 524k | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
613 | 0 | return NULL; |
614 | 0 | } |
615 | 524k | |
616 | 524k | l = ERR_GET_LIB(e); |
617 | 524k | f = ERR_GET_FUNC(e); |
618 | 524k | d.error = ERR_PACK(l, f, 0); |
619 | 524k | p = int_err_get_item(&d); |
620 | 524k | return ((p == NULL) ? NULL : p->string); |
621 | 524k | } |
622 | | |
623 | | const char *ERR_reason_error_string(unsigned long e) |
624 | 524k | { |
625 | 524k | ERR_STRING_DATA d, *p = NULL; |
626 | 524k | unsigned long l, r; |
627 | 524k | |
628 | 524k | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
629 | 0 | return NULL; |
630 | 0 | } |
631 | 524k | |
632 | 524k | l = ERR_GET_LIB(e); |
633 | 524k | r = ERR_GET_REASON(e); |
634 | 524k | d.error = ERR_PACK(l, 0, r); |
635 | 524k | p = int_err_get_item(&d); |
636 | 524k | if (!p) { |
637 | 323k | d.error = ERR_PACK(0, 0, r); |
638 | 323k | p = int_err_get_item(&d); |
639 | 323k | } |
640 | 524k | return ((p == NULL) ? NULL : p->string); |
641 | 524k | } |
642 | | |
643 | | void err_delete_thread_state(void) |
644 | 8 | { |
645 | 8 | ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local); |
646 | 8 | if (state == NULL) |
647 | 8 | return; |
648 | 8 | |
649 | 8 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
650 | 8 | ERR_STATE_free(state); |
651 | 8 | } |
652 | | |
653 | | #if OPENSSL_API_COMPAT < 0x10100000L |
654 | | void ERR_remove_thread_state(void *dummy) |
655 | 0 | { |
656 | 0 | } |
657 | | #endif |
658 | | |
659 | | #if OPENSSL_API_COMPAT < 0x10000000L |
660 | | void ERR_remove_state(unsigned long pid) |
661 | 0 | { |
662 | 0 | } |
663 | | #endif |
664 | | |
665 | | DEFINE_RUN_ONCE_STATIC(err_do_init) |
666 | 8 | { |
667 | 8 | set_err_thread_local = 1; |
668 | 8 | return CRYPTO_THREAD_init_local(&err_thread_local, NULL); |
669 | 8 | } |
670 | | |
671 | | ERR_STATE *ERR_get_state(void) |
672 | 1.33M | { |
673 | 1.33M | ERR_STATE *state; |
674 | 1.33M | |
675 | 1.33M | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
676 | 0 | return NULL; |
677 | 1.33M | |
678 | 1.33M | if (!RUN_ONCE(&err_init, err_do_init)) |
679 | 1.33M | return NULL; |
680 | 1.33M | |
681 | 1.33M | state = CRYPTO_THREAD_get_local(&err_thread_local); |
682 | 1.33M | if (state == (ERR_STATE*)-1) |
683 | 0 | return NULL; |
684 | 1.33M | |
685 | 1.33M | if (state == NULL) { |
686 | 8 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1)) |
687 | 0 | return NULL; |
688 | 8 | |
689 | 8 | if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) { |
690 | 0 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
691 | 0 | return NULL; |
692 | 0 | } |
693 | 8 | |
694 | 8 | if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE) |
695 | 8 | || !CRYPTO_THREAD_set_local(&err_thread_local, state)) { |
696 | 0 | ERR_STATE_free(state); |
697 | 0 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
698 | 0 | return NULL; |
699 | 0 | } |
700 | 8 | |
701 | 8 | /* Ignore failures from these */ |
702 | 8 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
703 | 8 | } |
704 | 1.33M | |
705 | 1.33M | return state; |
706 | 1.33M | } |
707 | | |
708 | | /* |
709 | | * err_shelve_state returns the current thread local error state |
710 | | * and freezes the error module until err_unshelve_state is called. |
711 | | */ |
712 | | int err_shelve_state(void **state) |
713 | 0 | { |
714 | 0 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
715 | 0 | return 0; |
716 | 0 | |
717 | 0 | if (!RUN_ONCE(&err_init, err_do_init)) |
718 | 0 | return 0; |
719 | 0 | |
720 | 0 | *state = CRYPTO_THREAD_get_local(&err_thread_local); |
721 | 0 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1)) |
722 | 0 | return 0; |
723 | 0 | |
724 | 0 | return 1; |
725 | 0 | } |
726 | | |
727 | | /* |
728 | | * err_unshelve_state restores the error state that was returned |
729 | | * by err_shelve_state previously. |
730 | | */ |
731 | | void err_unshelve_state(void* state) |
732 | 0 | { |
733 | 0 | if (state != (void*)-1) |
734 | 0 | CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state); |
735 | 0 | } |
736 | | |
737 | | int ERR_get_next_error_library(void) |
738 | 0 | { |
739 | 0 | int ret; |
740 | 0 |
|
741 | 0 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
742 | 0 | return 0; |
743 | 0 | |
744 | 0 | CRYPTO_THREAD_write_lock(err_string_lock); |
745 | 0 | ret = int_err_library_number++; |
746 | 0 | CRYPTO_THREAD_unlock(err_string_lock); |
747 | 0 | return ret; |
748 | 0 | } |
749 | | |
750 | | void ERR_set_error_data(char *data, int flags) |
751 | 168k | { |
752 | 168k | ERR_STATE *es; |
753 | 168k | int i; |
754 | 168k | |
755 | 168k | es = ERR_get_state(); |
756 | 168k | if (es == NULL) |
757 | 168k | return; |
758 | 168k | |
759 | 168k | i = es->top; |
760 | 168k | |
761 | 168k | err_clear_data(es, i); |
762 | 168k | es->err_data[i] = data; |
763 | 168k | es->err_data_flags[i] = flags; |
764 | 168k | } |
765 | | |
766 | | void ERR_add_error_data(int num, ...) |
767 | 168k | { |
768 | 168k | va_list args; |
769 | 168k | va_start(args, num); |
770 | 168k | ERR_add_error_vdata(num, args); |
771 | 168k | va_end(args); |
772 | 168k | } |
773 | | |
774 | | void ERR_add_error_vdata(int num, va_list args) |
775 | 168k | { |
776 | 168k | int i, n, s; |
777 | 168k | char *str, *p, *a; |
778 | 168k | |
779 | 168k | s = 80; |
780 | 168k | if ((str = OPENSSL_malloc(s + 1)) == NULL) { |
781 | 0 | /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */ |
782 | 0 | return; |
783 | 0 | } |
784 | 168k | str[0] = '\0'; |
785 | 168k | |
786 | 168k | n = 0; |
787 | 763k | for (i = 0; i < num; i++) { |
788 | 595k | a = va_arg(args, char *); |
789 | 595k | if (a == NULL) |
790 | 595k | a = "<NULL>"; |
791 | 595k | n += strlen(a); |
792 | 595k | if (n > s) { |
793 | 4.55k | s = n + 20; |
794 | 4.55k | p = OPENSSL_realloc(str, s + 1); |
795 | 4.55k | if (p == NULL) { |
796 | 0 | OPENSSL_free(str); |
797 | 0 | return; |
798 | 0 | } |
799 | 4.55k | str = p; |
800 | 4.55k | } |
801 | 595k | OPENSSL_strlcat(str, a, (size_t)s + 1); |
802 | 595k | } |
803 | 168k | ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING); |
804 | 168k | } |
805 | | |
806 | | int ERR_set_mark(void) |
807 | 0 | { |
808 | 0 | ERR_STATE *es; |
809 | 0 |
|
810 | 0 | es = ERR_get_state(); |
811 | 0 | if (es == NULL) |
812 | 0 | return 0; |
813 | 0 | |
814 | 0 | if (es->bottom == es->top) |
815 | 0 | return 0; |
816 | 0 | es->err_flags[es->top] |= ERR_FLAG_MARK; |
817 | 0 | return 1; |
818 | 0 | } |
819 | | |
820 | | int ERR_pop_to_mark(void) |
821 | 0 | { |
822 | 0 | ERR_STATE *es; |
823 | 0 |
|
824 | 0 | es = ERR_get_state(); |
825 | 0 | if (es == NULL) |
826 | 0 | return 0; |
827 | 0 | |
828 | 0 | while (es->bottom != es->top |
829 | 0 | && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) { |
830 | 0 | err_clear(es, es->top); |
831 | 0 | es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1; |
832 | 0 | } |
833 | 0 |
|
834 | 0 | if (es->bottom == es->top) |
835 | 0 | return 0; |
836 | 0 | es->err_flags[es->top] &= ~ERR_FLAG_MARK; |
837 | 0 | return 1; |
838 | 0 | } |
839 | | |
840 | | int ERR_clear_last_mark(void) |
841 | 0 | { |
842 | 0 | ERR_STATE *es; |
843 | 0 | int top; |
844 | 0 |
|
845 | 0 | es = ERR_get_state(); |
846 | 0 | if (es == NULL) |
847 | 0 | return 0; |
848 | 0 | |
849 | 0 | top = es->top; |
850 | 0 | while (es->bottom != top |
851 | 0 | && (es->err_flags[top] & ERR_FLAG_MARK) == 0) { |
852 | 0 | top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1; |
853 | 0 | } |
854 | 0 |
|
855 | 0 | if (es->bottom == top) |
856 | 0 | return 0; |
857 | 0 | es->err_flags[top] &= ~ERR_FLAG_MARK; |
858 | 0 | return 1; |
859 | 0 | } |