1 | /*
|
---|
2 | * Copyright 1995-2021 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 | static int err_load_strings(const ERR_STRING_DATA *str);
|
---|
33 |
|
---|
34 | static void ERR_STATE_free(ERR_STATE *s);
|
---|
35 | #ifndef OPENSSL_NO_ERR
|
---|
36 | static 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 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_init = CRYPTO_ONCE_STATIC_INIT;
|
---|
141 | static int set_err_thread_local;
|
---|
142 | static CRYPTO_THREAD_LOCAL err_thread_local;
|
---|
143 |
|
---|
144 | static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
|
---|
145 | static CRYPTO_RWLOCK *err_string_lock;
|
---|
146 |
|
---|
147 | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * The internal state
|
---|
151 | */
|
---|
152 |
|
---|
153 | static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
|
---|
154 | static int int_err_library_number = ERR_LIB_USER;
|
---|
155 |
|
---|
156 | typedef enum ERR_GET_ACTION_e {
|
---|
157 | EV_POP, EV_PEEK, EV_PEEK_LAST
|
---|
158 | } ERR_GET_ACTION;
|
---|
159 |
|
---|
160 | static unsigned long get_error_values(ERR_GET_ACTION g,
|
---|
161 | const char **file, int *line,
|
---|
162 | const char **func, const char **data,
|
---|
163 | int *flags);
|
---|
164 |
|
---|
165 | static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
|
---|
166 | {
|
---|
167 | unsigned long ret, l;
|
---|
168 |
|
---|
169 | l = a->error;
|
---|
170 | ret = l ^ ERR_GET_LIB(l);
|
---|
171 | return (ret ^ ret % 19 * 13);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static int err_string_data_cmp(const ERR_STRING_DATA *a,
|
---|
175 | const ERR_STRING_DATA *b)
|
---|
176 | {
|
---|
177 | if (a->error == b->error)
|
---|
178 | return 0;
|
---|
179 | return a->error > b->error ? 1 : -1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
|
---|
183 | {
|
---|
184 | ERR_STRING_DATA *p = NULL;
|
---|
185 |
|
---|
186 | if (!CRYPTO_THREAD_read_lock(err_string_lock))
|
---|
187 | return NULL;
|
---|
188 | p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
|
---|
189 | CRYPTO_THREAD_unlock(err_string_lock);
|
---|
190 |
|
---|
191 | return p;
|
---|
192 | }
|
---|
193 |
|
---|
194 | static void ERR_STATE_free(ERR_STATE *s)
|
---|
195 | {
|
---|
196 | int i;
|
---|
197 |
|
---|
198 | if (s == NULL)
|
---|
199 | return;
|
---|
200 | for (i = 0; i < ERR_NUM_ERRORS; i++) {
|
---|
201 | err_clear(s, i, 1);
|
---|
202 | }
|
---|
203 | OPENSSL_free(s);
|
---|
204 | }
|
---|
205 |
|
---|
206 | DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
|
---|
207 | {
|
---|
208 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
---|
209 | return 0;
|
---|
210 | err_string_lock = CRYPTO_THREAD_lock_new();
|
---|
211 | if (err_string_lock == NULL)
|
---|
212 | return 0;
|
---|
213 | int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
|
---|
214 | err_string_data_cmp);
|
---|
215 | if (int_error_hash == NULL) {
|
---|
216 | CRYPTO_THREAD_lock_free(err_string_lock);
|
---|
217 | err_string_lock = NULL;
|
---|
218 | return 0;
|
---|
219 | }
|
---|
220 | return 1;
|
---|
221 | }
|
---|
222 |
|
---|
223 | void err_cleanup(void)
|
---|
224 | {
|
---|
225 | if (set_err_thread_local != 0)
|
---|
226 | CRYPTO_THREAD_cleanup_local(&err_thread_local);
|
---|
227 | CRYPTO_THREAD_lock_free(err_string_lock);
|
---|
228 | err_string_lock = NULL;
|
---|
229 | lh_ERR_STRING_DATA_free(int_error_hash);
|
---|
230 | int_error_hash = NULL;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Legacy; pack in the library.
|
---|
235 | */
|
---|
236 | static void err_patch(int lib, ERR_STRING_DATA *str)
|
---|
237 | {
|
---|
238 | unsigned long plib = ERR_PACK(lib, 0, 0);
|
---|
239 |
|
---|
240 | for (; str->error != 0; str++)
|
---|
241 | str->error |= plib;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Hash in |str| error strings. Assumes the RUN_ONCE was done.
|
---|
246 | */
|
---|
247 | static int err_load_strings(const ERR_STRING_DATA *str)
|
---|
248 | {
|
---|
249 | if (!CRYPTO_THREAD_write_lock(err_string_lock))
|
---|
250 | return 0;
|
---|
251 | for (; str->error; str++)
|
---|
252 | (void)lh_ERR_STRING_DATA_insert(int_error_hash,
|
---|
253 | (ERR_STRING_DATA *)str);
|
---|
254 | CRYPTO_THREAD_unlock(err_string_lock);
|
---|
255 | return 1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | int ossl_err_load_ERR_strings(void)
|
---|
259 | {
|
---|
260 | #ifndef OPENSSL_NO_ERR
|
---|
261 | if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
---|
262 | return 0;
|
---|
263 |
|
---|
264 | err_load_strings(ERR_str_libraries);
|
---|
265 | err_load_strings(ERR_str_reasons);
|
---|
266 | #endif
|
---|
267 | return 1;
|
---|
268 | }
|
---|
269 |
|
---|
270 | int ERR_load_strings(int lib, ERR_STRING_DATA *str)
|
---|
271 | {
|
---|
272 | if (ossl_err_load_ERR_strings() == 0)
|
---|
273 | return 0;
|
---|
274 |
|
---|
275 | err_patch(lib, str);
|
---|
276 | err_load_strings(str);
|
---|
277 | return 1;
|
---|
278 | }
|
---|
279 |
|
---|
280 | int ERR_load_strings_const(const ERR_STRING_DATA *str)
|
---|
281 | {
|
---|
282 | if (ossl_err_load_ERR_strings() == 0)
|
---|
283 | return 0;
|
---|
284 | err_load_strings(str);
|
---|
285 | return 1;
|
---|
286 | }
|
---|
287 |
|
---|
288 | int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
|
---|
289 | {
|
---|
290 | if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
---|
291 | return 0;
|
---|
292 |
|
---|
293 | if (!CRYPTO_THREAD_write_lock(err_string_lock))
|
---|
294 | return 0;
|
---|
295 | /*
|
---|
296 | * We don't need to ERR_PACK the lib, since that was done (to
|
---|
297 | * the table) when it was loaded.
|
---|
298 | */
|
---|
299 | for (; str->error; str++)
|
---|
300 | (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
|
---|
301 | CRYPTO_THREAD_unlock(err_string_lock);
|
---|
302 |
|
---|
303 | return 1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | void err_free_strings_int(void)
|
---|
307 | {
|
---|
308 | if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /********************************************************/
|
---|
313 |
|
---|
314 | void ERR_clear_error(void)
|
---|
315 | {
|
---|
316 | int i;
|
---|
317 | ERR_STATE *es;
|
---|
318 |
|
---|
319 | es = ossl_err_get_state_int();
|
---|
320 | if (es == NULL)
|
---|
321 | return;
|
---|
322 |
|
---|
323 | for (i = 0; i < ERR_NUM_ERRORS; i++) {
|
---|
324 | err_clear(es, i, 0);
|
---|
325 | }
|
---|
326 | es->top = es->bottom = 0;
|
---|
327 | }
|
---|
328 |
|
---|
329 | unsigned long ERR_get_error(void)
|
---|
330 | {
|
---|
331 | return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
|
---|
332 | }
|
---|
333 |
|
---|
334 | unsigned long ERR_get_error_all(const char **file, int *line,
|
---|
335 | const char **func,
|
---|
336 | const char **data, int *flags)
|
---|
337 | {
|
---|
338 | return get_error_values(EV_POP, file, line, func, data, flags);
|
---|
339 | }
|
---|
340 |
|
---|
341 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
342 | unsigned long ERR_get_error_line(const char **file, int *line)
|
---|
343 | {
|
---|
344 | return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
|
---|
345 | }
|
---|
346 |
|
---|
347 | unsigned long ERR_get_error_line_data(const char **file, int *line,
|
---|
348 | const char **data, int *flags)
|
---|
349 | {
|
---|
350 | return get_error_values(EV_POP, file, line, NULL, data, flags);
|
---|
351 | }
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | unsigned long ERR_peek_error(void)
|
---|
355 | {
|
---|
356 | return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
|
---|
357 | }
|
---|
358 |
|
---|
359 | unsigned long ERR_peek_error_line(const char **file, int *line)
|
---|
360 | {
|
---|
361 | return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
|
---|
362 | }
|
---|
363 |
|
---|
364 | unsigned long ERR_peek_error_func(const char **func)
|
---|
365 | {
|
---|
366 | return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
|
---|
367 | }
|
---|
368 |
|
---|
369 | unsigned long ERR_peek_error_data(const char **data, int *flags)
|
---|
370 | {
|
---|
371 | return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
|
---|
372 | }
|
---|
373 |
|
---|
374 | unsigned long ERR_peek_error_all(const char **file, int *line,
|
---|
375 | const char **func,
|
---|
376 | const char **data, int *flags)
|
---|
377 | {
|
---|
378 | return get_error_values(EV_PEEK, file, line, func, data, flags);
|
---|
379 | }
|
---|
380 |
|
---|
381 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
382 | unsigned long ERR_peek_error_line_data(const char **file, int *line,
|
---|
383 | const char **data, int *flags)
|
---|
384 | {
|
---|
385 | return get_error_values(EV_PEEK, file, line, NULL, data, flags);
|
---|
386 | }
|
---|
387 | #endif
|
---|
388 |
|
---|
389 | unsigned long ERR_peek_last_error(void)
|
---|
390 | {
|
---|
391 | return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
|
---|
392 | }
|
---|
393 |
|
---|
394 | unsigned long ERR_peek_last_error_line(const char **file, int *line)
|
---|
395 | {
|
---|
396 | return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
|
---|
397 | }
|
---|
398 |
|
---|
399 | unsigned long ERR_peek_last_error_func(const char **func)
|
---|
400 | {
|
---|
401 | return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
|
---|
402 | }
|
---|
403 |
|
---|
404 | unsigned long ERR_peek_last_error_data(const char **data, int *flags)
|
---|
405 | {
|
---|
406 | return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
|
---|
407 | }
|
---|
408 |
|
---|
409 | unsigned long ERR_peek_last_error_all(const char **file, int *line,
|
---|
410 | const char **func,
|
---|
411 | const char **data, int *flags)
|
---|
412 | {
|
---|
413 | return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
|
---|
414 | }
|
---|
415 |
|
---|
416 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
417 | unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
|
---|
418 | const char **data, int *flags)
|
---|
419 | {
|
---|
420 | return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
|
---|
421 | }
|
---|
422 | #endif
|
---|
423 |
|
---|
424 | static unsigned long get_error_values(ERR_GET_ACTION g,
|
---|
425 | const char **file, int *line,
|
---|
426 | const char **func,
|
---|
427 | const char **data, int *flags)
|
---|
428 | {
|
---|
429 | int i = 0;
|
---|
430 | ERR_STATE *es;
|
---|
431 | unsigned long ret;
|
---|
432 |
|
---|
433 | es = ossl_err_get_state_int();
|
---|
434 | if (es == NULL)
|
---|
435 | return 0;
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * Clear anything that should have been cleared earlier. We do this
|
---|
439 | * here because this doesn't have constant-time issues.
|
---|
440 | */
|
---|
441 | while (es->bottom != es->top) {
|
---|
442 | if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
|
---|
443 | err_clear(es, es->top, 0);
|
---|
444 | es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
|
---|
445 | continue;
|
---|
446 | }
|
---|
447 | i = (es->bottom + 1) % ERR_NUM_ERRORS;
|
---|
448 | if (es->err_flags[i] & ERR_FLAG_CLEAR) {
|
---|
449 | es->bottom = i;
|
---|
450 | err_clear(es, es->bottom, 0);
|
---|
451 | continue;
|
---|
452 | }
|
---|
453 | break;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /* If everything has been cleared, the stack is empty. */
|
---|
457 | if (es->bottom == es->top)
|
---|
458 | return 0;
|
---|
459 |
|
---|
460 | /* Which error, the top of stack (latest one) or the first one? */
|
---|
461 | if (g == EV_PEEK_LAST)
|
---|
462 | i = es->top;
|
---|
463 | else
|
---|
464 | i = (es->bottom + 1) % ERR_NUM_ERRORS;
|
---|
465 |
|
---|
466 | ret = es->err_buffer[i];
|
---|
467 | if (g == EV_POP) {
|
---|
468 | es->bottom = i;
|
---|
469 | es->err_buffer[i] = 0;
|
---|
470 | }
|
---|
471 |
|
---|
472 | if (file != NULL) {
|
---|
473 | *file = es->err_file[i];
|
---|
474 | if (*file == NULL)
|
---|
475 | *file = "";
|
---|
476 | }
|
---|
477 | if (line != NULL)
|
---|
478 | *line = es->err_line[i];
|
---|
479 | if (func != NULL) {
|
---|
480 | *func = es->err_func[i];
|
---|
481 | if (*func == NULL)
|
---|
482 | *func = "";
|
---|
483 | }
|
---|
484 | if (flags != NULL)
|
---|
485 | *flags = es->err_data_flags[i];
|
---|
486 | if (data == NULL) {
|
---|
487 | if (g == EV_POP) {
|
---|
488 | err_clear_data(es, i, 0);
|
---|
489 | }
|
---|
490 | } else {
|
---|
491 | *data = es->err_data[i];
|
---|
492 | if (*data == NULL) {
|
---|
493 | *data = "";
|
---|
494 | if (flags != NULL)
|
---|
495 | *flags = 0;
|
---|
496 | }
|
---|
497 | }
|
---|
498 | return ret;
|
---|
499 | }
|
---|
500 |
|
---|
501 | void ossl_err_string_int(unsigned long e, const char *func,
|
---|
502 | char *buf, size_t len)
|
---|
503 | {
|
---|
504 | char lsbuf[64], rsbuf[256];
|
---|
505 | const char *ls, *rs = NULL;
|
---|
506 | unsigned long l, r;
|
---|
507 |
|
---|
508 | if (len == 0)
|
---|
509 | return;
|
---|
510 |
|
---|
511 | l = ERR_GET_LIB(e);
|
---|
512 | ls = ERR_lib_error_string(e);
|
---|
513 | if (ls == NULL) {
|
---|
514 | BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
|
---|
515 | ls = lsbuf;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * ERR_reason_error_string() can't safely return system error strings,
|
---|
520 | * since it would call openssl_strerror_r(), which needs a buffer for
|
---|
521 | * thread safety. So for system errors, we call openssl_strerror_r()
|
---|
522 | * directly instead.
|
---|
523 | */
|
---|
524 | r = ERR_GET_REASON(e);
|
---|
525 | #ifndef OPENSSL_NO_ERR
|
---|
526 | if (ERR_SYSTEM_ERROR(e)) {
|
---|
527 | if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
|
---|
528 | rs = rsbuf;
|
---|
529 | } else {
|
---|
530 | rs = ERR_reason_error_string(e);
|
---|
531 | }
|
---|
532 | #endif
|
---|
533 | if (rs == NULL) {
|
---|
534 | BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
|
---|
535 | rs = rsbuf;
|
---|
536 | }
|
---|
537 |
|
---|
538 | BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
|
---|
539 | if (strlen(buf) == len - 1) {
|
---|
540 | /* Didn't fit; use a minimal format. */
|
---|
541 | BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | void ERR_error_string_n(unsigned long e, char *buf, size_t len)
|
---|
547 | {
|
---|
548 | ossl_err_string_int(e, "", buf, len);
|
---|
549 | }
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * ERR_error_string_n should be used instead for ret != NULL as
|
---|
553 | * ERR_error_string cannot know how large the buffer is
|
---|
554 | */
|
---|
555 | char *ERR_error_string(unsigned long e, char *ret)
|
---|
556 | {
|
---|
557 | static char buf[256];
|
---|
558 |
|
---|
559 | if (ret == NULL)
|
---|
560 | ret = buf;
|
---|
561 | ERR_error_string_n(e, ret, (int)sizeof(buf));
|
---|
562 | return ret;
|
---|
563 | }
|
---|
564 |
|
---|
565 | const char *ERR_lib_error_string(unsigned long e)
|
---|
566 | {
|
---|
567 | ERR_STRING_DATA d, *p;
|
---|
568 | unsigned long l;
|
---|
569 |
|
---|
570 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
|
---|
571 | return NULL;
|
---|
572 | }
|
---|
573 |
|
---|
574 | l = ERR_GET_LIB(e);
|
---|
575 | d.error = ERR_PACK(l, 0, 0);
|
---|
576 | p = int_err_get_item(&d);
|
---|
577 | return ((p == NULL) ? NULL : p->string);
|
---|
578 | }
|
---|
579 |
|
---|
580 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
581 | const char *ERR_func_error_string(unsigned long e)
|
---|
582 | {
|
---|
583 | return NULL;
|
---|
584 | }
|
---|
585 | #endif
|
---|
586 |
|
---|
587 | const char *ERR_reason_error_string(unsigned long e)
|
---|
588 | {
|
---|
589 | ERR_STRING_DATA d, *p = NULL;
|
---|
590 | unsigned long l, r;
|
---|
591 |
|
---|
592 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
|
---|
593 | return NULL;
|
---|
594 | }
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * ERR_reason_error_string() can't safely return system error strings,
|
---|
598 | * since openssl_strerror_r() needs a buffer for thread safety, and we
|
---|
599 | * haven't got one that would serve any sensible purpose.
|
---|
600 | */
|
---|
601 | if (ERR_SYSTEM_ERROR(e))
|
---|
602 | return NULL;
|
---|
603 |
|
---|
604 | l = ERR_GET_LIB(e);
|
---|
605 | r = ERR_GET_REASON(e);
|
---|
606 | d.error = ERR_PACK(l, 0, r);
|
---|
607 | p = int_err_get_item(&d);
|
---|
608 | if (p == NULL) {
|
---|
609 | d.error = ERR_PACK(0, 0, r);
|
---|
610 | p = int_err_get_item(&d);
|
---|
611 | }
|
---|
612 | return ((p == NULL) ? NULL : p->string);
|
---|
613 | }
|
---|
614 |
|
---|
615 | static void err_delete_thread_state(void *unused)
|
---|
616 | {
|
---|
617 | ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
|
---|
618 | if (state == NULL)
|
---|
619 | return;
|
---|
620 |
|
---|
621 | CRYPTO_THREAD_set_local(&err_thread_local, NULL);
|
---|
622 | ERR_STATE_free(state);
|
---|
623 | }
|
---|
624 |
|
---|
625 | #ifndef OPENSSL_NO_DEPRECATED_1_1_0
|
---|
626 | void ERR_remove_thread_state(void *dummy)
|
---|
627 | {
|
---|
628 | }
|
---|
629 | #endif
|
---|
630 |
|
---|
631 | #ifndef OPENSSL_NO_DEPRECATED_1_0_0
|
---|
632 | void ERR_remove_state(unsigned long pid)
|
---|
633 | {
|
---|
634 | }
|
---|
635 | #endif
|
---|
636 |
|
---|
637 | DEFINE_RUN_ONCE_STATIC(err_do_init)
|
---|
638 | {
|
---|
639 | set_err_thread_local = 1;
|
---|
640 | return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
|
---|
641 | }
|
---|
642 |
|
---|
643 | ERR_STATE *ossl_err_get_state_int(void)
|
---|
644 | {
|
---|
645 | ERR_STATE *state;
|
---|
646 | int saveerrno = get_last_sys_error();
|
---|
647 |
|
---|
648 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
---|
649 | return NULL;
|
---|
650 |
|
---|
651 | if (!RUN_ONCE(&err_init, err_do_init))
|
---|
652 | return NULL;
|
---|
653 |
|
---|
654 | state = CRYPTO_THREAD_get_local(&err_thread_local);
|
---|
655 | if (state == (ERR_STATE*)-1)
|
---|
656 | return NULL;
|
---|
657 |
|
---|
658 | if (state == NULL) {
|
---|
659 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
|
---|
660 | return NULL;
|
---|
661 |
|
---|
662 | if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
|
---|
663 | CRYPTO_THREAD_set_local(&err_thread_local, NULL);
|
---|
664 | return NULL;
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
|
---|
668 | || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
|
---|
669 | ERR_STATE_free(state);
|
---|
670 | CRYPTO_THREAD_set_local(&err_thread_local, NULL);
|
---|
671 | return NULL;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /* Ignore failures from these */
|
---|
675 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
|
---|
676 | }
|
---|
677 |
|
---|
678 | set_sys_error(saveerrno);
|
---|
679 | return state;
|
---|
680 | }
|
---|
681 |
|
---|
682 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
683 | ERR_STATE *ERR_get_state(void)
|
---|
684 | {
|
---|
685 | return ossl_err_get_state_int();
|
---|
686 | }
|
---|
687 | #endif
|
---|
688 |
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * err_shelve_state returns the current thread local error state
|
---|
692 | * and freezes the error module until err_unshelve_state is called.
|
---|
693 | */
|
---|
694 | int err_shelve_state(void **state)
|
---|
695 | {
|
---|
696 | int saveerrno = get_last_sys_error();
|
---|
697 |
|
---|
698 | /*
|
---|
699 | * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
|
---|
700 | * via ossl_init_load_crypto_nodelete(), by which point the requested
|
---|
701 | * "base" initialization has already been performed, so the below call is a
|
---|
702 | * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
|
---|
703 | *
|
---|
704 | * If are no other valid callers of this function, the call below can be
|
---|
705 | * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
|
---|
706 | * potential uses that are not from inside OPENSSL_init_crypto(), then this
|
---|
707 | * call is needed, but some care is required to make sure that the re-entry
|
---|
708 | * remains a NOOP.
|
---|
709 | */
|
---|
710 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
|
---|
711 | return 0;
|
---|
712 |
|
---|
713 | if (!RUN_ONCE(&err_init, err_do_init))
|
---|
714 | return 0;
|
---|
715 |
|
---|
716 | *state = CRYPTO_THREAD_get_local(&err_thread_local);
|
---|
717 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
|
---|
718 | return 0;
|
---|
719 |
|
---|
720 | set_sys_error(saveerrno);
|
---|
721 | return 1;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * err_unshelve_state restores the error state that was returned
|
---|
726 | * by err_shelve_state previously.
|
---|
727 | */
|
---|
728 | void err_unshelve_state(void* state)
|
---|
729 | {
|
---|
730 | if (state != (void*)-1)
|
---|
731 | CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
|
---|
732 | }
|
---|
733 |
|
---|
734 | int ERR_get_next_error_library(void)
|
---|
735 | {
|
---|
736 | int ret;
|
---|
737 |
|
---|
738 | if (!RUN_ONCE(&err_string_init, do_err_strings_init))
|
---|
739 | return 0;
|
---|
740 |
|
---|
741 | if (!CRYPTO_THREAD_write_lock(err_string_lock))
|
---|
742 | return 0;
|
---|
743 | ret = int_err_library_number++;
|
---|
744 | CRYPTO_THREAD_unlock(err_string_lock);
|
---|
745 | return ret;
|
---|
746 | }
|
---|
747 |
|
---|
748 | static int err_set_error_data_int(char *data, size_t size, int flags,
|
---|
749 | int deallocate)
|
---|
750 | {
|
---|
751 | ERR_STATE *es;
|
---|
752 |
|
---|
753 | es = ossl_err_get_state_int();
|
---|
754 | if (es == NULL)
|
---|
755 | return 0;
|
---|
756 |
|
---|
757 | err_clear_data(es, es->top, deallocate);
|
---|
758 | err_set_data(es, es->top, data, size, flags);
|
---|
759 |
|
---|
760 | return 1;
|
---|
761 | }
|
---|
762 |
|
---|
763 | void ERR_set_error_data(char *data, int flags)
|
---|
764 | {
|
---|
765 | /*
|
---|
766 | * This function is void so we cannot propagate the error return. Since it
|
---|
767 | * is also in the public API we can't change the return type.
|
---|
768 | *
|
---|
769 | * We estimate the size of the data. If it's not flagged as allocated,
|
---|
770 | * then this is safe, and if it is flagged as allocated, then our size
|
---|
771 | * may be smaller than the actual allocation, but that doesn't matter
|
---|
772 | * too much, the buffer will remain untouched or will eventually be
|
---|
773 | * reallocated to a new size.
|
---|
774 | *
|
---|
775 | * callers should be advised that this function takes over ownership of
|
---|
776 | * the allocated memory, i.e. they can't count on the pointer to remain
|
---|
777 | * valid.
|
---|
778 | */
|
---|
779 | err_set_error_data_int(data, strlen(data) + 1, flags, 1);
|
---|
780 | }
|
---|
781 |
|
---|
782 | void ERR_add_error_data(int num, ...)
|
---|
783 | {
|
---|
784 | va_list args;
|
---|
785 | va_start(args, num);
|
---|
786 | ERR_add_error_vdata(num, args);
|
---|
787 | va_end(args);
|
---|
788 | }
|
---|
789 |
|
---|
790 | void ERR_add_error_vdata(int num, va_list args)
|
---|
791 | {
|
---|
792 | int i, len, size;
|
---|
793 | int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
|
---|
794 | char *str, *arg;
|
---|
795 | ERR_STATE *es;
|
---|
796 |
|
---|
797 | /* Get the current error data; if an allocated string get it. */
|
---|
798 | es = ossl_err_get_state_int();
|
---|
799 | if (es == NULL)
|
---|
800 | return;
|
---|
801 | i = es->top;
|
---|
802 |
|
---|
803 | /*
|
---|
804 | * If err_data is allocated already, re-use the space.
|
---|
805 | * Otherwise, allocate a small new buffer.
|
---|
806 | */
|
---|
807 | if ((es->err_data_flags[i] & flags) == flags) {
|
---|
808 | str = es->err_data[i];
|
---|
809 | size = es->err_data_size[i];
|
---|
810 |
|
---|
811 | /*
|
---|
812 | * To protect the string we just grabbed from tampering by other
|
---|
813 | * functions we may call, or to protect them from freeing a pointer
|
---|
814 | * that may no longer be valid at that point, we clear away the
|
---|
815 | * data pointer and the flags. We will set them again at the end
|
---|
816 | * of this function.
|
---|
817 | */
|
---|
818 | es->err_data[i] = NULL;
|
---|
819 | es->err_data_flags[i] = 0;
|
---|
820 | } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
|
---|
821 | return;
|
---|
822 | } else {
|
---|
823 | str[0] = '\0';
|
---|
824 | }
|
---|
825 | len = strlen(str);
|
---|
826 |
|
---|
827 | while (--num >= 0) {
|
---|
828 | arg = va_arg(args, char *);
|
---|
829 | if (arg == NULL)
|
---|
830 | arg = "<NULL>";
|
---|
831 | len += strlen(arg);
|
---|
832 | if (len >= size) {
|
---|
833 | char *p;
|
---|
834 |
|
---|
835 | size = len + 20;
|
---|
836 | p = OPENSSL_realloc(str, size);
|
---|
837 | if (p == NULL) {
|
---|
838 | OPENSSL_free(str);
|
---|
839 | return;
|
---|
840 | }
|
---|
841 | str = p;
|
---|
842 | }
|
---|
843 | OPENSSL_strlcat(str, arg, (size_t)size);
|
---|
844 | }
|
---|
845 | if (!err_set_error_data_int(str, size, flags, 0))
|
---|
846 | OPENSSL_free(str);
|
---|
847 | }
|
---|
848 |
|
---|
849 | int ERR_set_mark(void)
|
---|
850 | {
|
---|
851 | ERR_STATE *es;
|
---|
852 |
|
---|
853 | es = ossl_err_get_state_int();
|
---|
854 | if (es == NULL)
|
---|
855 | return 0;
|
---|
856 |
|
---|
857 | if (es->bottom == es->top)
|
---|
858 | return 0;
|
---|
859 | es->err_marks[es->top]++;
|
---|
860 | return 1;
|
---|
861 | }
|
---|
862 |
|
---|
863 | int ERR_pop_to_mark(void)
|
---|
864 | {
|
---|
865 | ERR_STATE *es;
|
---|
866 |
|
---|
867 | es = ossl_err_get_state_int();
|
---|
868 | if (es == NULL)
|
---|
869 | return 0;
|
---|
870 |
|
---|
871 | while (es->bottom != es->top
|
---|
872 | && es->err_marks[es->top] == 0) {
|
---|
873 | err_clear(es, es->top, 0);
|
---|
874 | es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
|
---|
875 | }
|
---|
876 |
|
---|
877 | if (es->bottom == es->top)
|
---|
878 | return 0;
|
---|
879 | es->err_marks[es->top]--;
|
---|
880 | return 1;
|
---|
881 | }
|
---|
882 |
|
---|
883 | int ERR_clear_last_mark(void)
|
---|
884 | {
|
---|
885 | ERR_STATE *es;
|
---|
886 | int top;
|
---|
887 |
|
---|
888 | es = ossl_err_get_state_int();
|
---|
889 | if (es == NULL)
|
---|
890 | return 0;
|
---|
891 |
|
---|
892 | top = es->top;
|
---|
893 | while (es->bottom != top
|
---|
894 | && es->err_marks[top] == 0) {
|
---|
895 | top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
|
---|
896 | }
|
---|
897 |
|
---|
898 | if (es->bottom == top)
|
---|
899 | return 0;
|
---|
900 | es->err_marks[top]--;
|
---|
901 | return 1;
|
---|
902 | }
|
---|
903 |
|
---|
904 | void err_clear_last_constant_time(int clear)
|
---|
905 | {
|
---|
906 | ERR_STATE *es;
|
---|
907 | int top;
|
---|
908 |
|
---|
909 | es = ossl_err_get_state_int();
|
---|
910 | if (es == NULL)
|
---|
911 | return;
|
---|
912 |
|
---|
913 | top = es->top;
|
---|
914 |
|
---|
915 | /*
|
---|
916 | * Flag error as cleared but remove it elsewhere to avoid two errors
|
---|
917 | * accessing the same error stack location, revealing timing information.
|
---|
918 | */
|
---|
919 | clear = constant_time_select_int(constant_time_eq_int(clear, 0),
|
---|
920 | 0, ERR_FLAG_CLEAR);
|
---|
921 | es->err_flags[top] |= clear;
|
---|
922 | }
|
---|