/src/openssl30/crypto/err/err.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #define OSSL_FORCE_ERR_STATE |
11 | | |
12 | | #include <stdio.h> |
13 | | #include <stdarg.h> |
14 | | #include <string.h> |
15 | | #include "crypto/cryptlib.h" |
16 | | #include "internal/err.h" |
17 | | #include "crypto/err.h" |
18 | | #include <openssl/err.h> |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/buffer.h> |
21 | | #include <openssl/bio.h> |
22 | | #include <openssl/opensslconf.h> |
23 | | #include "internal/thread_once.h" |
24 | | #include "crypto/ctype.h" |
25 | | #include "internal/constant_time.h" |
26 | | #include "e_os.h" |
27 | | #include "err_local.h" |
28 | | |
29 | | /* Forward declaration in case it's not published because of configuration */ |
30 | | ERR_STATE *ERR_get_state(void); |
31 | | |
32 | | #ifndef OPENSSL_NO_ERR |
33 | | static int err_load_strings(const ERR_STRING_DATA *str); |
34 | | #endif |
35 | | |
36 | | static void ERR_STATE_free(ERR_STATE *s); |
37 | | #ifndef OPENSSL_NO_ERR |
38 | | static ERR_STRING_DATA ERR_str_libraries[] = { |
39 | | { ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library" }, |
40 | | { ERR_PACK(ERR_LIB_SYS, 0, 0), "system library" }, |
41 | | { ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines" }, |
42 | | { ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines" }, |
43 | | { ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines" }, |
44 | | { ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines" }, |
45 | | { ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines" }, |
46 | | { ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines" }, |
47 | | { ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines" }, |
48 | | { ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines" }, |
49 | | { ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines" }, |
50 | | { ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines" }, |
51 | | { ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines" }, |
52 | | { ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines" }, |
53 | | { ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines" }, |
54 | | { ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines" }, |
55 | | { ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines" }, |
56 | | { ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines" }, |
57 | | { ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines" }, |
58 | | { ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines" }, |
59 | | { ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines" }, |
60 | | { ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines" }, |
61 | | { ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator" }, |
62 | | { ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines" }, |
63 | | { ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines" }, |
64 | | { ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines" }, |
65 | | { ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines" }, |
66 | | { ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines" }, |
67 | | { ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines" }, |
68 | | { ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines" }, |
69 | | { ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines" }, |
70 | | { ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines" }, |
71 | | { ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines" }, |
72 | | { ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines" }, |
73 | | { ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines" }, |
74 | | { ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines" }, |
75 | | { ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines" }, |
76 | | { ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines" }, |
77 | | { ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines" }, |
78 | | { ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines" }, |
79 | | { ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines" }, |
80 | | { ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines" }, |
81 | | { ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines" }, |
82 | | { 0, NULL }, |
83 | | }; |
84 | | |
85 | | /* |
86 | | * Should make sure that all ERR_R_ reasons defined in include/openssl/err.h.in |
87 | | * are listed. For maintainability, please keep all reasons in the same order. |
88 | | */ |
89 | | static ERR_STRING_DATA ERR_str_reasons[] = { |
90 | | { ERR_R_SYS_LIB, "system lib" }, |
91 | | { ERR_R_BN_LIB, "BN lib" }, |
92 | | { ERR_R_RSA_LIB, "RSA lib" }, |
93 | | { ERR_R_DH_LIB, "DH lib" }, |
94 | | { ERR_R_EVP_LIB, "EVP lib" }, |
95 | | { ERR_R_BUF_LIB, "BUF lib" }, |
96 | | { ERR_R_OBJ_LIB, "OBJ lib" }, |
97 | | { ERR_R_PEM_LIB, "PEM lib" }, |
98 | | { ERR_R_DSA_LIB, "DSA lib" }, |
99 | | { ERR_R_X509_LIB, "X509 lib" }, |
100 | | { ERR_R_ASN1_LIB, "ASN1 lib" }, |
101 | | { ERR_R_CRYPTO_LIB, "CRYPTO lib" }, |
102 | | { ERR_R_EC_LIB, "EC lib" }, |
103 | | { ERR_R_BIO_LIB, "BIO lib" }, |
104 | | { ERR_R_PKCS7_LIB, "PKCS7 lib" }, |
105 | | { ERR_R_X509V3_LIB, "X509V3 lib" }, |
106 | | { ERR_R_ENGINE_LIB, "ENGINE lib" }, |
107 | | { ERR_R_UI_LIB, "UI lib" }, |
108 | | { ERR_R_ECDSA_LIB, "ECDSA lib" }, |
109 | | { ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib" }, |
110 | | { ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib" }, |
111 | | |
112 | | { ERR_R_FATAL, "fatal" }, |
113 | | { ERR_R_MALLOC_FAILURE, "malloc failure" }, |
114 | | { ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, |
115 | | "called a function you should not call" }, |
116 | | { ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter" }, |
117 | | { ERR_R_INTERNAL_ERROR, "internal error" }, |
118 | | { ERR_R_DISABLED, "called a function that was disabled at compile-time" }, |
119 | | { ERR_R_INIT_FAIL, "init fail" }, |
120 | | { ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument" }, |
121 | | { ERR_R_OPERATION_FAIL, "operation fail" }, |
122 | | { ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions" }, |
123 | | { ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled" }, |
124 | | { ERR_R_NESTED_ASN1_ERROR, "nested asn1 error" }, |
125 | | { ERR_R_MISSING_ASN1_EOS, "missing asn1 eos" }, |
126 | | /* |
127 | | * Something is unsupported, exactly what is expressed with additional data |
128 | | */ |
129 | | { ERR_R_UNSUPPORTED, "unsupported" }, |
130 | | /* |
131 | | * A fetch failed for other reasons than the name to be fetched being |
132 | | * unsupported. |
133 | | */ |
134 | | { ERR_R_FETCH_FAILED, "fetch failed" }, |
135 | | { ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition" }, |
136 | | { ERR_R_UNABLE_TO_GET_READ_LOCK, "unable to get read lock" }, |
137 | | { ERR_R_UNABLE_TO_GET_WRITE_LOCK, "unable to get write lock" }, |
138 | | { 0, NULL }, |
139 | | }; |
140 | | #endif |
141 | | |
142 | | static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT; |
143 | | static int set_err_thread_local; |
144 | | static CRYPTO_THREAD_LOCAL err_thread_local; |
145 | | |
146 | | static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT; |
147 | | static CRYPTO_RWLOCK *err_string_lock = NULL; |
148 | | |
149 | | #ifndef OPENSSL_NO_ERR |
150 | | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); |
151 | | #endif |
152 | | |
153 | | /* |
154 | | * The internal state |
155 | | */ |
156 | | |
157 | | #ifndef OPENSSL_NO_ERR |
158 | | static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; |
159 | | #endif |
160 | | static int int_err_library_number = ERR_LIB_USER; |
161 | | |
162 | | typedef enum ERR_GET_ACTION_e { |
163 | | EV_POP, |
164 | | EV_PEEK, |
165 | | EV_PEEK_LAST |
166 | | } ERR_GET_ACTION; |
167 | | |
168 | | static unsigned long get_error_values(ERR_GET_ACTION g, |
169 | | const char **file, int *line, |
170 | | const char **func, const char **data, |
171 | | int *flags); |
172 | | |
173 | | #ifndef OPENSSL_NO_ERR |
174 | | static unsigned long err_string_data_hash(const ERR_STRING_DATA *a) |
175 | 1.94M | { |
176 | 1.94M | unsigned long ret, l; |
177 | | |
178 | 1.94M | l = a->error; |
179 | 1.94M | ret = l ^ ERR_GET_LIB(l); |
180 | 1.94M | return (ret ^ ret % 19 * 13); |
181 | 1.94M | } |
182 | | |
183 | | static int err_string_data_cmp(const ERR_STRING_DATA *a, |
184 | | const ERR_STRING_DATA *b) |
185 | 1.52M | { |
186 | 1.52M | if (a->error == b->error) |
187 | 1.45M | return 0; |
188 | 70.2k | return a->error > b->error ? 1 : -1; |
189 | 1.52M | } |
190 | | |
191 | | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d) |
192 | 734k | { |
193 | 734k | ERR_STRING_DATA *p = NULL; |
194 | | |
195 | 734k | if (!CRYPTO_THREAD_read_lock(err_string_lock)) |
196 | 0 | return NULL; |
197 | 734k | p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d); |
198 | 734k | CRYPTO_THREAD_unlock(err_string_lock); |
199 | | |
200 | 734k | return p; |
201 | 734k | } |
202 | | #endif |
203 | | |
204 | | static void ERR_STATE_free(ERR_STATE *s) |
205 | 24 | { |
206 | 24 | int i; |
207 | | |
208 | 24 | if (s == NULL) |
209 | 0 | return; |
210 | 408 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
211 | 384 | err_clear(s, i, 1); |
212 | 384 | } |
213 | 24 | OPENSSL_free(s); |
214 | 24 | } |
215 | | |
216 | | DEFINE_RUN_ONCE_STATIC(do_err_strings_init) |
217 | 273 | { |
218 | 273 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
219 | 0 | return 0; |
220 | 273 | err_string_lock = CRYPTO_THREAD_lock_new(); |
221 | 273 | if (err_string_lock == NULL) |
222 | 0 | return 0; |
223 | 273 | #ifndef OPENSSL_NO_ERR |
224 | 273 | int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash, |
225 | 273 | err_string_data_cmp); |
226 | 273 | if (int_error_hash == NULL) { |
227 | 0 | CRYPTO_THREAD_lock_free(err_string_lock); |
228 | 0 | err_string_lock = NULL; |
229 | 0 | return 0; |
230 | 0 | } |
231 | 273 | #endif |
232 | 273 | return 1; |
233 | 273 | } |
234 | | |
235 | | void err_cleanup(void) |
236 | 167 | { |
237 | 167 | if (set_err_thread_local != 0) |
238 | 167 | CRYPTO_THREAD_cleanup_local(&err_thread_local); |
239 | 167 | CRYPTO_THREAD_lock_free(err_string_lock); |
240 | 167 | err_string_lock = NULL; |
241 | 167 | #ifndef OPENSSL_NO_ERR |
242 | 167 | lh_ERR_STRING_DATA_free(int_error_hash); |
243 | 167 | int_error_hash = NULL; |
244 | 167 | #endif |
245 | 167 | } |
246 | | |
247 | | #ifndef OPENSSL_NO_ERR |
248 | | /* |
249 | | * Legacy; pack in the library. |
250 | | */ |
251 | | static void err_patch(int lib, ERR_STRING_DATA *str) |
252 | 0 | { |
253 | 0 | unsigned long plib = ERR_PACK(lib, 0, 0); |
254 | |
|
255 | 0 | for (; str->error != 0; str++) |
256 | 0 | str->error |= plib; |
257 | 0 | } |
258 | | |
259 | | /* |
260 | | * Hash in |str| error strings. Assumes the RUN_ONCE was done. |
261 | | */ |
262 | | static int err_load_strings(const ERR_STRING_DATA *str) |
263 | 28.5k | { |
264 | 28.5k | if (!CRYPTO_THREAD_write_lock(err_string_lock)) |
265 | 0 | return 0; |
266 | 1.23M | for (; str->error; str++) |
267 | 1.21M | (void)lh_ERR_STRING_DATA_insert(int_error_hash, |
268 | 28.5k | (ERR_STRING_DATA *)str); |
269 | 28.5k | CRYPTO_THREAD_unlock(err_string_lock); |
270 | 28.5k | return 1; |
271 | 28.5k | } |
272 | | #endif |
273 | | |
274 | | int ossl_err_load_ERR_strings(void) |
275 | 9.60k | { |
276 | 9.60k | #ifndef OPENSSL_NO_ERR |
277 | 9.60k | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
278 | 0 | return 0; |
279 | | |
280 | 9.60k | err_load_strings(ERR_str_libraries); |
281 | 9.60k | err_load_strings(ERR_str_reasons); |
282 | 9.60k | #endif |
283 | 9.60k | return 1; |
284 | 9.60k | } |
285 | | |
286 | | int ERR_load_strings(int lib, ERR_STRING_DATA *str) |
287 | 0 | { |
288 | 0 | #ifndef OPENSSL_NO_ERR |
289 | 0 | if (ossl_err_load_ERR_strings() == 0) |
290 | 0 | return 0; |
291 | | |
292 | 0 | err_patch(lib, str); |
293 | 0 | err_load_strings(str); |
294 | 0 | #endif |
295 | |
|
296 | 0 | return 1; |
297 | 0 | } |
298 | | |
299 | | int ERR_load_strings_const(const ERR_STRING_DATA *str) |
300 | 9.33k | { |
301 | 9.33k | #ifndef OPENSSL_NO_ERR |
302 | 9.33k | if (ossl_err_load_ERR_strings() == 0) |
303 | 0 | return 0; |
304 | 9.33k | err_load_strings(str); |
305 | 9.33k | #endif |
306 | | |
307 | 9.33k | return 1; |
308 | 9.33k | } |
309 | | |
310 | | int ERR_unload_strings(int lib, ERR_STRING_DATA *str) |
311 | 0 | { |
312 | 0 | #ifndef OPENSSL_NO_ERR |
313 | 0 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
314 | 0 | return 0; |
315 | | |
316 | 0 | if (!CRYPTO_THREAD_write_lock(err_string_lock)) |
317 | 0 | return 0; |
318 | | /* |
319 | | * We don't need to ERR_PACK the lib, since that was done (to |
320 | | * the table) when it was loaded. |
321 | | */ |
322 | 0 | for (; str->error; str++) |
323 | 0 | (void)lh_ERR_STRING_DATA_delete(int_error_hash, str); |
324 | 0 | CRYPTO_THREAD_unlock(err_string_lock); |
325 | 0 | #endif |
326 | |
|
327 | 0 | return 1; |
328 | 0 | } |
329 | | |
330 | | void err_free_strings_int(void) |
331 | 0 | { |
332 | | /* obsolete */ |
333 | 0 | } |
334 | | |
335 | | /********************************************************/ |
336 | | |
337 | | void ERR_clear_error(void) |
338 | 63.6M | { |
339 | 63.6M | int i; |
340 | 63.6M | ERR_STATE *es; |
341 | | |
342 | 63.6M | es = ossl_err_get_state_int(); |
343 | 63.6M | if (es == NULL) |
344 | 0 | return; |
345 | | |
346 | 1.08G | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
347 | 1.01G | err_clear(es, i, 0); |
348 | 1.01G | } |
349 | 63.6M | es->top = es->bottom = 0; |
350 | 63.6M | } |
351 | | |
352 | | unsigned long ERR_get_error(void) |
353 | 0 | { |
354 | 0 | return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL); |
355 | 0 | } |
356 | | |
357 | | unsigned long ERR_get_error_all(const char **file, int *line, |
358 | | const char **func, |
359 | | const char **data, int *flags) |
360 | 486k | { |
361 | 486k | return get_error_values(EV_POP, file, line, func, data, flags); |
362 | 486k | } |
363 | | |
364 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
365 | | unsigned long ERR_get_error_line(const char **file, int *line) |
366 | 0 | { |
367 | 0 | return get_error_values(EV_POP, file, line, NULL, NULL, NULL); |
368 | 0 | } |
369 | | |
370 | | unsigned long ERR_get_error_line_data(const char **file, int *line, |
371 | | const char **data, int *flags) |
372 | 0 | { |
373 | 0 | return get_error_values(EV_POP, file, line, NULL, data, flags); |
374 | 0 | } |
375 | | #endif |
376 | | |
377 | | unsigned long ERR_peek_error(void) |
378 | 3.62k | { |
379 | 3.62k | return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL); |
380 | 3.62k | } |
381 | | |
382 | | unsigned long ERR_peek_error_line(const char **file, int *line) |
383 | 0 | { |
384 | 0 | return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL); |
385 | 0 | } |
386 | | |
387 | | unsigned long ERR_peek_error_func(const char **func) |
388 | 0 | { |
389 | 0 | return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL); |
390 | 0 | } |
391 | | |
392 | | unsigned long ERR_peek_error_data(const char **data, int *flags) |
393 | 39.3k | { |
394 | 39.3k | return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags); |
395 | 39.3k | } |
396 | | |
397 | | unsigned long ERR_peek_error_all(const char **file, int *line, |
398 | | const char **func, |
399 | | const char **data, int *flags) |
400 | 0 | { |
401 | 0 | return get_error_values(EV_PEEK, file, line, func, data, flags); |
402 | 0 | } |
403 | | |
404 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
405 | | unsigned long ERR_peek_error_line_data(const char **file, int *line, |
406 | | const char **data, int *flags) |
407 | 0 | { |
408 | 0 | return get_error_values(EV_PEEK, file, line, NULL, data, flags); |
409 | 0 | } |
410 | | #endif |
411 | | |
412 | | unsigned long ERR_peek_last_error(void) |
413 | 262M | { |
414 | 262M | return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL); |
415 | 262M | } |
416 | | |
417 | | unsigned long ERR_peek_last_error_line(const char **file, int *line) |
418 | 0 | { |
419 | 0 | return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL); |
420 | 0 | } |
421 | | |
422 | | unsigned long ERR_peek_last_error_func(const char **func) |
423 | 0 | { |
424 | 0 | return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL); |
425 | 0 | } |
426 | | |
427 | | unsigned long ERR_peek_last_error_data(const char **data, int *flags) |
428 | 0 | { |
429 | 0 | return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags); |
430 | 0 | } |
431 | | |
432 | | unsigned long ERR_peek_last_error_all(const char **file, int *line, |
433 | | const char **func, |
434 | | const char **data, int *flags) |
435 | 20.8k | { |
436 | 20.8k | return get_error_values(EV_PEEK_LAST, file, line, func, data, flags); |
437 | 20.8k | } |
438 | | |
439 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
440 | | unsigned long ERR_peek_last_error_line_data(const char **file, int *line, |
441 | | const char **data, int *flags) |
442 | 0 | { |
443 | 0 | return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags); |
444 | 0 | } |
445 | | #endif |
446 | | |
447 | | static unsigned long get_error_values(ERR_GET_ACTION g, |
448 | | const char **file, int *line, |
449 | | const char **func, |
450 | | const char **data, int *flags) |
451 | 262M | { |
452 | 262M | int i = 0; |
453 | 262M | ERR_STATE *es; |
454 | 262M | unsigned long ret; |
455 | | |
456 | 262M | es = ossl_err_get_state_int(); |
457 | 262M | if (es == NULL) |
458 | 0 | return 0; |
459 | | |
460 | | /* |
461 | | * Clear anything that should have been cleared earlier. We do this |
462 | | * here because this doesn't have constant-time issues. |
463 | | */ |
464 | 262M | while (es->bottom != es->top) { |
465 | 261M | if (es->err_flags[es->top] & ERR_FLAG_CLEAR) { |
466 | 0 | err_clear(es, es->top, 0); |
467 | 0 | es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1; |
468 | 0 | continue; |
469 | 0 | } |
470 | 261M | i = (es->bottom + 1) % ERR_NUM_ERRORS; |
471 | 261M | if (es->err_flags[i] & ERR_FLAG_CLEAR) { |
472 | 0 | es->bottom = i; |
473 | 0 | err_clear(es, es->bottom, 0); |
474 | 0 | continue; |
475 | 0 | } |
476 | 261M | break; |
477 | 261M | } |
478 | | |
479 | | /* If everything has been cleared, the stack is empty. */ |
480 | 262M | if (es->bottom == es->top) |
481 | 1.55M | return 0; |
482 | | |
483 | | /* Which error, the top of stack (latest one) or the first one? */ |
484 | 261M | if (g == EV_PEEK_LAST) |
485 | 260M | i = es->top; |
486 | 330k | else |
487 | 330k | i = (es->bottom + 1) % ERR_NUM_ERRORS; |
488 | | |
489 | 261M | ret = es->err_buffer[i]; |
490 | 261M | if (g == EV_POP) { |
491 | 288k | es->bottom = i; |
492 | 288k | es->err_buffer[i] = 0; |
493 | 288k | } |
494 | | |
495 | 261M | if (file != NULL) { |
496 | 308k | *file = es->err_file[i]; |
497 | 308k | if (*file == NULL) |
498 | 0 | *file = ""; |
499 | 308k | } |
500 | 261M | if (line != NULL) |
501 | 308k | *line = es->err_line[i]; |
502 | 261M | if (func != NULL) { |
503 | 308k | *func = es->err_func[i]; |
504 | 308k | if (*func == NULL) |
505 | 0 | *func = ""; |
506 | 308k | } |
507 | 261M | if (flags != NULL) |
508 | 348k | *flags = es->err_data_flags[i]; |
509 | 261M | if (data == NULL) { |
510 | 260M | if (g == EV_POP) { |
511 | 0 | err_clear_data(es, i, 0); |
512 | 0 | } |
513 | 260M | } else { |
514 | 348k | *data = es->err_data[i]; |
515 | 348k | if (*data == NULL) { |
516 | 145 | *data = ""; |
517 | 145 | if (flags != NULL) |
518 | 145 | *flags = 0; |
519 | 145 | } |
520 | 348k | } |
521 | 261M | return ret; |
522 | 262M | } |
523 | | |
524 | | void ossl_err_string_int(unsigned long e, const char *func, |
525 | | char *buf, size_t len) |
526 | 30.1k | { |
527 | 30.1k | char lsbuf[64], rsbuf[256]; |
528 | 30.1k | const char *ls, *rs = NULL; |
529 | 30.1k | unsigned long l, r; |
530 | | |
531 | 30.1k | if (len == 0) |
532 | 0 | return; |
533 | | |
534 | 30.1k | l = ERR_GET_LIB(e); |
535 | 30.1k | ls = ERR_lib_error_string(e); |
536 | 30.1k | if (ls == NULL) { |
537 | 271 | BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l); |
538 | 271 | ls = lsbuf; |
539 | 271 | } |
540 | | |
541 | | /* |
542 | | * ERR_reason_error_string() can't safely return system error strings, |
543 | | * since it would call openssl_strerror_r(), which needs a buffer for |
544 | | * thread safety. So for system errors, we call openssl_strerror_r() |
545 | | * directly instead. |
546 | | */ |
547 | 30.1k | r = ERR_GET_REASON(e); |
548 | 30.1k | #ifndef OPENSSL_NO_ERR |
549 | 30.1k | if (ERR_SYSTEM_ERROR(e)) { |
550 | 0 | if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf))) |
551 | 0 | rs = rsbuf; |
552 | 30.1k | } else { |
553 | 30.1k | rs = ERR_reason_error_string(e); |
554 | 30.1k | } |
555 | 30.1k | #endif |
556 | 30.1k | if (rs == NULL) { |
557 | 271 | BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r); |
558 | 271 | rs = rsbuf; |
559 | 271 | } |
560 | | |
561 | 30.1k | BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs); |
562 | 30.1k | if (strlen(buf) == len - 1) { |
563 | | /* Didn't fit; use a minimal format. */ |
564 | 0 | BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r); |
565 | 0 | } |
566 | 30.1k | } |
567 | | |
568 | | void ERR_error_string_n(unsigned long e, char *buf, size_t len) |
569 | 271 | { |
570 | 271 | ossl_err_string_int(e, "", buf, len); |
571 | 271 | } |
572 | | |
573 | | /* |
574 | | * ERR_error_string_n should be used instead for ret != NULL as |
575 | | * ERR_error_string cannot know how large the buffer is |
576 | | */ |
577 | | char *ERR_error_string(unsigned long e, char *ret) |
578 | 0 | { |
579 | 0 | static char buf[256]; |
580 | |
|
581 | 0 | if (ret == NULL) |
582 | 0 | ret = buf; |
583 | 0 | ERR_error_string_n(e, ret, (int)sizeof(buf)); |
584 | 0 | return ret; |
585 | 0 | } |
586 | | |
587 | | const char *ERR_lib_error_string(unsigned long e) |
588 | 327k | { |
589 | 327k | #ifndef OPENSSL_NO_ERR |
590 | 327k | ERR_STRING_DATA d, *p; |
591 | 327k | unsigned long l; |
592 | | |
593 | 327k | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
594 | 0 | return NULL; |
595 | 0 | } |
596 | | |
597 | 327k | l = ERR_GET_LIB(e); |
598 | 327k | d.error = ERR_PACK(l, 0, 0); |
599 | 327k | p = int_err_get_item(&d); |
600 | 327k | return ((p == NULL) ? NULL : p->string); |
601 | | #else |
602 | | return NULL; |
603 | | #endif |
604 | 327k | } |
605 | | |
606 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
607 | | const char *ERR_func_error_string(unsigned long e) |
608 | 0 | { |
609 | 0 | return NULL; |
610 | 0 | } |
611 | | #endif |
612 | | |
613 | | const char *ERR_reason_error_string(unsigned long e) |
614 | 376k | { |
615 | 376k | #ifndef OPENSSL_NO_ERR |
616 | 376k | ERR_STRING_DATA d, *p = NULL; |
617 | 376k | unsigned long l, r; |
618 | | |
619 | 376k | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
620 | 0 | return NULL; |
621 | 0 | } |
622 | | |
623 | | /* |
624 | | * ERR_reason_error_string() can't safely return system error strings, |
625 | | * since openssl_strerror_r() needs a buffer for thread safety, and we |
626 | | * haven't got one that would serve any sensible purpose. |
627 | | */ |
628 | 376k | if (ERR_SYSTEM_ERROR(e)) |
629 | 0 | return NULL; |
630 | | |
631 | 376k | l = ERR_GET_LIB(e); |
632 | 376k | r = ERR_GET_REASON(e); |
633 | 376k | d.error = ERR_PACK(l, 0, r); |
634 | 376k | p = int_err_get_item(&d); |
635 | 376k | if (p == NULL) { |
636 | 30.4k | d.error = ERR_PACK(0, 0, r); |
637 | 30.4k | p = int_err_get_item(&d); |
638 | 30.4k | } |
639 | 376k | return ((p == NULL) ? NULL : p->string); |
640 | | #else |
641 | | return NULL; |
642 | | #endif |
643 | 376k | } |
644 | | |
645 | | static void err_delete_thread_state(void *unused) |
646 | 220 | { |
647 | 220 | ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local); |
648 | 220 | if (state == NULL) |
649 | 0 | return; |
650 | | |
651 | 220 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
652 | 220 | ERR_STATE_free(state); |
653 | 220 | } |
654 | | |
655 | | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
656 | | void ERR_remove_thread_state(void *dummy) |
657 | 0 | { |
658 | 0 | } |
659 | | #endif |
660 | | |
661 | | #ifndef OPENSSL_NO_DEPRECATED_1_0_0 |
662 | | void ERR_remove_state(unsigned long pid) |
663 | 0 | { |
664 | 0 | } |
665 | | #endif |
666 | | |
667 | | DEFINE_RUN_ONCE_STATIC(err_do_init) |
668 | 167 | { |
669 | 167 | set_err_thread_local = 1; |
670 | 167 | return CRYPTO_THREAD_init_local(&err_thread_local, NULL); |
671 | 167 | } |
672 | | |
673 | | ERR_STATE *ossl_err_get_state_int(void) |
674 | 1.27G | { |
675 | 1.27G | ERR_STATE *state; |
676 | 1.27G | int saveerrno = get_last_sys_error(); |
677 | | |
678 | 1.27G | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
679 | 0 | return NULL; |
680 | | |
681 | 1.27G | if (!RUN_ONCE(&err_init, err_do_init)) |
682 | 0 | return NULL; |
683 | | |
684 | 1.27G | state = CRYPTO_THREAD_get_local(&err_thread_local); |
685 | 1.27G | if (state == (ERR_STATE *)-1) |
686 | 0 | return NULL; |
687 | | |
688 | 1.27G | if (state == NULL) { |
689 | 167 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1)) |
690 | 0 | return NULL; |
691 | | |
692 | 167 | if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) { |
693 | 0 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
694 | 0 | return NULL; |
695 | 0 | } |
696 | | |
697 | 167 | if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state) |
698 | 167 | || !CRYPTO_THREAD_set_local(&err_thread_local, state)) { |
699 | 0 | ERR_STATE_free(state); |
700 | 0 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
701 | 0 | return NULL; |
702 | 0 | } |
703 | | |
704 | | /* Ignore failures from these */ |
705 | 167 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
706 | 167 | } |
707 | | |
708 | 1.27G | set_sys_error(saveerrno); |
709 | 1.27G | return state; |
710 | 1.27G | } |
711 | | |
712 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
713 | | ERR_STATE *ERR_get_state(void) |
714 | 0 | { |
715 | 0 | return ossl_err_get_state_int(); |
716 | 0 | } |
717 | | #endif |
718 | | |
719 | | /* |
720 | | * err_shelve_state returns the current thread local error state |
721 | | * and freezes the error module until err_unshelve_state is called. |
722 | | */ |
723 | | int err_shelve_state(void **state) |
724 | 143 | { |
725 | 143 | int saveerrno = get_last_sys_error(); |
726 | | |
727 | | /* |
728 | | * Note, at present our only caller is OPENSSL_init_crypto(), indirectly |
729 | | * via ossl_init_load_crypto_nodelete(), by which point the requested |
730 | | * "base" initialization has already been performed, so the below call is a |
731 | | * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return. |
732 | | * |
733 | | * If are no other valid callers of this function, the call below can be |
734 | | * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are |
735 | | * potential uses that are not from inside OPENSSL_init_crypto(), then this |
736 | | * call is needed, but some care is required to make sure that the re-entry |
737 | | * remains a NOOP. |
738 | | */ |
739 | 143 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
740 | 0 | return 0; |
741 | | |
742 | 143 | if (!RUN_ONCE(&err_init, err_do_init)) |
743 | 0 | return 0; |
744 | | |
745 | 143 | *state = CRYPTO_THREAD_get_local(&err_thread_local); |
746 | 143 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1)) |
747 | 0 | return 0; |
748 | | |
749 | 143 | set_sys_error(saveerrno); |
750 | 143 | return 1; |
751 | 143 | } |
752 | | |
753 | | /* |
754 | | * err_unshelve_state restores the error state that was returned |
755 | | * by err_shelve_state previously. |
756 | | */ |
757 | | void err_unshelve_state(void *state) |
758 | 249 | { |
759 | 249 | if (state != (void *)-1) |
760 | 249 | CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)state); |
761 | 249 | } |
762 | | |
763 | | int ERR_get_next_error_library(void) |
764 | 235 | { |
765 | 235 | int ret; |
766 | | |
767 | 235 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
768 | 0 | return 0; |
769 | | |
770 | 235 | if (!CRYPTO_THREAD_write_lock(err_string_lock)) |
771 | 0 | return 0; |
772 | 235 | ret = int_err_library_number++; |
773 | 235 | CRYPTO_THREAD_unlock(err_string_lock); |
774 | 235 | return ret; |
775 | 235 | } |
776 | | |
777 | | static int err_set_error_data_int(char *data, size_t size, int flags, |
778 | | int deallocate) |
779 | 17.4M | { |
780 | 17.4M | ERR_STATE *es; |
781 | | |
782 | 17.4M | es = ossl_err_get_state_int(); |
783 | 17.4M | if (es == NULL) |
784 | 0 | return 0; |
785 | | |
786 | 17.4M | err_clear_data(es, es->top, deallocate); |
787 | 17.4M | err_set_data(es, es->top, data, size, flags); |
788 | | |
789 | 17.4M | return 1; |
790 | 17.4M | } |
791 | | |
792 | | void ERR_set_error_data(char *data, int flags) |
793 | 0 | { |
794 | | /* |
795 | | * This function is void so we cannot propagate the error return. Since it |
796 | | * is also in the public API we can't change the return type. |
797 | | * |
798 | | * We estimate the size of the data. If it's not flagged as allocated, |
799 | | * then this is safe, and if it is flagged as allocated, then our size |
800 | | * may be smaller than the actual allocation, but that doesn't matter |
801 | | * too much, the buffer will remain untouched or will eventually be |
802 | | * reallocated to a new size. |
803 | | * |
804 | | * callers should be advised that this function takes over ownership of |
805 | | * the allocated memory, i.e. they can't count on the pointer to remain |
806 | | * valid. |
807 | | */ |
808 | 0 | err_set_error_data_int(data, strlen(data) + 1, flags, 1); |
809 | 0 | } |
810 | | |
811 | | void ERR_add_error_data(int num, ...) |
812 | 17.4M | { |
813 | 17.4M | va_list args; |
814 | 17.4M | va_start(args, num); |
815 | 17.4M | ERR_add_error_vdata(num, args); |
816 | 17.4M | va_end(args); |
817 | 17.4M | } |
818 | | |
819 | | void ERR_add_error_vdata(int num, va_list args) |
820 | 2.88M | { |
821 | 2.88M | int i, len, size; |
822 | 2.88M | int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING; |
823 | 2.88M | char *str, *arg; |
824 | 2.88M | ERR_STATE *es; |
825 | | |
826 | | /* Get the current error data; if an allocated string get it. */ |
827 | 2.88M | es = ossl_err_get_state_int(); |
828 | 2.88M | if (es == NULL) |
829 | 0 | return; |
830 | 2.88M | i = es->top; |
831 | | |
832 | | /* |
833 | | * If err_data is allocated already, re-use the space. |
834 | | * Otherwise, allocate a small new buffer. |
835 | | */ |
836 | 2.88M | if ((es->err_data_flags[i] & flags) == flags) { |
837 | 1.36k | str = es->err_data[i]; |
838 | 1.36k | size = es->err_data_size[i]; |
839 | | |
840 | | /* |
841 | | * To protect the string we just grabbed from tampering by other |
842 | | * functions we may call, or to protect them from freeing a pointer |
843 | | * that may no longer be valid at that point, we clear away the |
844 | | * data pointer and the flags. We will set them again at the end |
845 | | * of this function. |
846 | | */ |
847 | 1.36k | es->err_data[i] = NULL; |
848 | 1.36k | es->err_data_flags[i] = 0; |
849 | 2.87M | } else if ((str = OPENSSL_malloc(size = 81)) == NULL) { |
850 | 0 | return; |
851 | 2.87M | } else { |
852 | 2.87M | str[0] = '\0'; |
853 | 2.87M | } |
854 | 2.88M | len = strlen(str); |
855 | | |
856 | 12.4M | while (--num >= 0) { |
857 | 9.61M | arg = va_arg(args, char *); |
858 | 9.61M | if (arg == NULL) |
859 | 0 | arg = "<NULL>"; |
860 | 9.61M | len += strlen(arg); |
861 | 9.61M | if (len >= size) { |
862 | 1.21k | char *p; |
863 | | |
864 | 1.21k | size = len + 20; |
865 | 1.21k | p = OPENSSL_realloc(str, size); |
866 | 1.21k | if (p == NULL) { |
867 | 0 | OPENSSL_free(str); |
868 | 0 | return; |
869 | 0 | } |
870 | 1.21k | str = p; |
871 | 1.21k | } |
872 | 9.61M | OPENSSL_strlcat(str, arg, (size_t)size); |
873 | 9.61M | } |
874 | 2.88M | if (!err_set_error_data_int(str, size, flags, 0)) |
875 | 0 | OPENSSL_free(str); |
876 | 2.88M | } |
877 | | |
878 | | int ERR_set_mark(void) |
879 | 387M | { |
880 | 387M | ERR_STATE *es; |
881 | | |
882 | 387M | es = ossl_err_get_state_int(); |
883 | 387M | if (es == NULL) |
884 | 0 | return 0; |
885 | | |
886 | 387M | if (es->bottom == es->top) |
887 | 272M | return 0; |
888 | 114M | es->err_marks[es->top]++; |
889 | 114M | return 1; |
890 | 387M | } |
891 | | |
892 | | int ERR_pop_to_mark(void) |
893 | 362M | { |
894 | 362M | ERR_STATE *es; |
895 | | |
896 | 362M | es = ossl_err_get_state_int(); |
897 | 362M | if (es == NULL) |
898 | 0 | return 0; |
899 | | |
900 | 641M | while (es->bottom != es->top |
901 | 387M | && es->err_marks[es->top] == 0) { |
902 | 278M | err_clear(es, es->top, 0); |
903 | 278M | es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1; |
904 | 278M | } |
905 | | |
906 | 362M | if (es->bottom == es->top) |
907 | 253M | return 0; |
908 | 108M | es->err_marks[es->top]--; |
909 | 108M | return 1; |
910 | 362M | } |
911 | | |
912 | | int ERR_clear_last_mark(void) |
913 | 24.0M | { |
914 | 24.0M | ERR_STATE *es; |
915 | 24.0M | int top; |
916 | | |
917 | 24.0M | es = ossl_err_get_state_int(); |
918 | 24.0M | if (es == NULL) |
919 | 0 | return 0; |
920 | | |
921 | 24.0M | top = es->top; |
922 | 32.7M | while (es->bottom != top |
923 | 13.9M | && es->err_marks[top] == 0) { |
924 | 8.68M | top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1; |
925 | 8.68M | } |
926 | | |
927 | 24.0M | if (es->bottom == top) |
928 | 18.8M | return 0; |
929 | 5.25M | es->err_marks[top]--; |
930 | 5.25M | return 1; |
931 | 24.0M | } |
932 | | |
933 | | void err_clear_last_constant_time(int clear) |
934 | 11.3k | { |
935 | 11.3k | ERR_STATE *es; |
936 | 11.3k | int top; |
937 | | |
938 | 11.3k | es = ossl_err_get_state_int(); |
939 | 11.3k | if (es == NULL) |
940 | 0 | return; |
941 | | |
942 | 11.3k | top = es->top; |
943 | | |
944 | | /* |
945 | | * Flag error as cleared but remove it elsewhere to avoid two errors |
946 | | * accessing the same error stack location, revealing timing information. |
947 | | */ |
948 | 11.3k | clear = constant_time_select_int(constant_time_eq_int(clear, 0), |
949 | 11.3k | 0, ERR_FLAG_CLEAR); |
950 | 11.3k | es->err_flags[top] |= clear; |
951 | 11.3k | } |