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