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