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