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