]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 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 | #define OSSL_FORCE_ERR_STATE |
11 | ||
d02b48c6 | 12 | #include <stdio.h> |
4565c3e3 | 13 | #include <stdarg.h> |
0a150c5c | 14 | #include <string.h> |
25f2138b | 15 | #include "crypto/cryptlib.h" |
176db6dc | 16 | #include "internal/err.h" |
25f2138b | 17 | #include "crypto/err.h" |
52df25cf | 18 | #include <openssl/err.h> |
ec577822 | 19 | #include <openssl/crypto.h> |
ec577822 | 20 | #include <openssl/buffer.h> |
e5c84d51 | 21 | #include <openssl/bio.h> |
98186eb4 | 22 | #include <openssl/opensslconf.h> |
176db6dc | 23 | #include "internal/thread_once.h" |
25f2138b | 24 | #include "crypto/ctype.h" |
706457b7 | 25 | #include "internal/constant_time.h" |
d5f9166b | 26 | #include "internal/e_os.h" |
706457b7 | 27 | #include "err_local.h" |
3c1d6bbc | 28 | |
14e275e8 RL |
29 | /* Forward declaration in case it's not published because of configuration */ |
30 | ERR_STATE *ERR_get_state(void); | |
31 | ||
52df25cf | 32 | static int err_load_strings(const ERR_STRING_DATA *str); |
cf5bfbfc | 33 | |
58964a49 | 34 | static void ERR_STATE_free(ERR_STATE *s); |
cf1b7d96 | 35 | #ifndef OPENSSL_NO_ERR |
0f113f3e MC |
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"}, | |
49844486 | 64 | {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"}, |
0f113f3e MC |
65 | {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"}, |
66 | {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"}, | |
a61b7f2f | 67 | {ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines"}, |
8869ad4a | 68 | {ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines"}, |
0f113f3e | 69 | {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"}, |
0cea8832 | 70 | {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"}, |
079a1a90 | 71 | {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"}, |
7c6a0d90 | 72 | {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"}, |
71a5516d | 73 | {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"}, |
3d328a44 | 74 | {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"}, |
e85d19c6 | 75 | {ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines"}, |
6caf7f3a | 76 | {ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines"}, |
ece9304c RL |
77 | {ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines"}, |
78 | {ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines"}, | |
29f178bd | 79 | {ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines"}, |
0f113f3e MC |
80 | {0, NULL}, |
81 | }; | |
82 | ||
3ae55288 DDO |
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 | */ | |
0f113f3e MC |
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"}, | |
3ae55288 | 99 | {ERR_R_CRYPTO_LIB, "CRYPTO lib"}, |
0f113f3e | 100 | {ERR_R_EC_LIB, "EC lib"}, |
0f113f3e MC |
101 | {ERR_R_BIO_LIB, "BIO lib"}, |
102 | {ERR_R_PKCS7_LIB, "PKCS7 lib"}, | |
103 | {ERR_R_X509V3_LIB, "X509V3 lib"}, | |
0f113f3e | 104 | {ERR_R_ENGINE_LIB, "ENGINE lib"}, |
c785fd48 | 105 | {ERR_R_UI_LIB, "UI lib"}, |
0f113f3e | 106 | {ERR_R_ECDSA_LIB, "ECDSA lib"}, |
3ae55288 DDO |
107 | {ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib"}, |
108 | {ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib"}, | |
0f113f3e MC |
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"}, | |
302f7588 | 117 | {ERR_R_INIT_FAIL, "init fail"}, |
3ae55288 | 118 | {ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument"}, |
9a32dcf4 | 119 | {ERR_R_OPERATION_FAIL, "operation fail"}, |
0d003c52 | 120 | {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"}, |
866234ac | 121 | {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"}, |
3ae55288 DDO |
122 | {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"}, |
123 | {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"}, | |
d6d42cda RL |
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"}, | |
9379bf94 | 133 | {ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition"}, |
e3c50779 P |
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"}, | |
0f113f3e MC |
136 | {0, NULL}, |
137 | }; | |
d02b48c6 | 138 | #endif |
0a150c5c | 139 | |
8509dcc9 | 140 | static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT; |
73bc5370 | 141 | static int set_err_thread_local; |
8509dcc9 AG |
142 | static CRYPTO_THREAD_LOCAL err_thread_local; |
143 | ||
7b8e12d2 AG |
144 | static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT; |
145 | static CRYPTO_RWLOCK *err_string_lock; | |
146 | ||
566bdf2b | 147 | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); |
566bdf2b | 148 | |
0f113f3e | 149 | /* |
3e47caff | 150 | * The internal state |
0f113f3e | 151 | */ |
f3f1cf84 | 152 | |
3c1d6bbc | 153 | static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; |
0f113f3e | 154 | static int int_err_library_number = ERR_LIB_USER; |
566bdf2b | 155 | |
b4570683 RS |
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); | |
566bdf2b | 164 | |
3c1d6bbc | 165 | static unsigned long err_string_data_hash(const ERR_STRING_DATA *a) |
0f113f3e MC |
166 | { |
167 | unsigned long ret, l; | |
168 | ||
169 | l = a->error; | |
aac96e27 | 170 | ret = l ^ ERR_GET_LIB(l); |
0f113f3e MC |
171 | return (ret ^ ret % 19 * 13); |
172 | } | |
3c1d6bbc | 173 | |
3c1d6bbc | 174 | static int err_string_data_cmp(const ERR_STRING_DATA *a, |
0f113f3e MC |
175 | const ERR_STRING_DATA *b) |
176 | { | |
c784a838 RS |
177 | if (a->error == b->error) |
178 | return 0; | |
179 | return a->error > b->error ? 1 : -1; | |
0f113f3e MC |
180 | } |
181 | ||
566bdf2b | 182 | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d) |
0f113f3e | 183 | { |
3e47caff | 184 | ERR_STRING_DATA *p = NULL; |
78f79235 | 185 | |
cd3f8c1b RS |
186 | if (!CRYPTO_THREAD_read_lock(err_string_lock)) |
187 | return NULL; | |
52df25cf | 188 | p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d); |
7b8e12d2 | 189 | CRYPTO_THREAD_unlock(err_string_lock); |
78f79235 | 190 | |
0f113f3e MC |
191 | return p; |
192 | } | |
567fef89 | 193 | |
6b691a5c | 194 | static void ERR_STATE_free(ERR_STATE *s) |
0f113f3e MC |
195 | { |
196 | int i; | |
58964a49 | 197 | |
e6e9170d RS |
198 | if (s == NULL) |
199 | return; | |
0f113f3e | 200 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
de4a88a9 | 201 | err_clear(s, i, 1); |
0f113f3e MC |
202 | } |
203 | OPENSSL_free(s); | |
204 | } | |
58964a49 | 205 | |
c2e4e5d2 | 206 | DEFINE_RUN_ONCE_STATIC(do_err_strings_init) |
7b8e12d2 | 207 | { |
12b4e582 | 208 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
eb2b9892 | 209 | return 0; |
63ab5ea1 | 210 | err_string_lock = CRYPTO_THREAD_lock_new(); |
eb2b9892 BE |
211 | if (err_string_lock == NULL) |
212 | return 0; | |
52df25cf RS |
213 | int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash, |
214 | err_string_data_cmp); | |
eb2b9892 BE |
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; | |
ff234405 MC |
221 | } |
222 | ||
223 | void err_cleanup(void) | |
224 | { | |
73bc5370 RS |
225 | if (set_err_thread_local != 0) |
226 | CRYPTO_THREAD_cleanup_local(&err_thread_local); | |
ff234405 MC |
227 | CRYPTO_THREAD_lock_free(err_string_lock); |
228 | err_string_lock = NULL; | |
52df25cf RS |
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 | /* | |
5884b051 | 245 | * Hash in |str| error strings. Assumes the RUN_ONCE was done. |
52df25cf RS |
246 | */ |
247 | static int err_load_strings(const ERR_STRING_DATA *str) | |
248 | { | |
cd3f8c1b RS |
249 | if (!CRYPTO_THREAD_write_lock(err_string_lock)) |
250 | return 0; | |
52df25cf RS |
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; | |
7b8e12d2 AG |
256 | } |
257 | ||
b93f6c2d | 258 | int ossl_err_load_ERR_strings(void) |
0f113f3e | 259 | { |
cf1b7d96 | 260 | #ifndef OPENSSL_NO_ERR |
69588edb KR |
261 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
262 | return 0; | |
7b8e12d2 | 263 | |
52df25cf RS |
264 | err_load_strings(ERR_str_libraries); |
265 | err_load_strings(ERR_str_reasons); | |
d02b48c6 | 266 | #endif |
69588edb | 267 | return 1; |
0f113f3e | 268 | } |
d02b48c6 | 269 | |
52df25cf | 270 | int ERR_load_strings(int lib, ERR_STRING_DATA *str) |
0f113f3e | 271 | { |
b93f6c2d | 272 | if (ossl_err_load_ERR_strings() == 0) |
52df25cf RS |
273 | return 0; |
274 | ||
275 | err_patch(lib, str); | |
276 | err_load_strings(str); | |
277 | return 1; | |
0f113f3e | 278 | } |
d02b48c6 | 279 | |
52df25cf | 280 | int ERR_load_strings_const(const ERR_STRING_DATA *str) |
0f113f3e | 281 | { |
b93f6c2d | 282 | if (ossl_err_load_ERR_strings() == 0) |
69588edb | 283 | return 0; |
52df25cf | 284 | err_load_strings(str); |
69588edb | 285 | return 1; |
0f113f3e | 286 | } |
cf5bfbfc | 287 | |
c2e4e5d2 | 288 | int ERR_unload_strings(int lib, ERR_STRING_DATA *str) |
0f113f3e | 289 | { |
c2e4e5d2 RL |
290 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
291 | return 0; | |
7b8e12d2 | 292 | |
cd3f8c1b RS |
293 | if (!CRYPTO_THREAD_write_lock(err_string_lock)) |
294 | return 0; | |
52df25cf RS |
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); | |
7b8e12d2 | 301 | CRYPTO_THREAD_unlock(err_string_lock); |
c2e4e5d2 RL |
302 | |
303 | return 1; | |
0f113f3e | 304 | } |
1738bb61 | 305 | |
b3599dbb | 306 | void err_free_strings_int(void) |
0f113f3e | 307 | { |
c2e4e5d2 RL |
308 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
309 | return; | |
0f113f3e | 310 | } |
d02b48c6 | 311 | |
58964a49 RE |
312 | /********************************************************/ |
313 | ||
6b691a5c | 314 | void ERR_clear_error(void) |
0f113f3e MC |
315 | { |
316 | int i; | |
317 | ERR_STATE *es; | |
d02b48c6 | 318 | |
78715dcc | 319 | es = ossl_err_get_state_int(); |
4fc426b7 BE |
320 | if (es == NULL) |
321 | return; | |
d02b48c6 | 322 | |
0f113f3e | 323 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
10f8b368 | 324 | err_clear(es, i, 0); |
0f113f3e MC |
325 | } |
326 | es->top = es->bottom = 0; | |
327 | } | |
d02b48c6 | 328 | |
6b691a5c | 329 | unsigned long ERR_get_error(void) |
0f113f3e | 330 | { |
b4570683 | 331 | return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL); |
0f113f3e | 332 | } |
d02b48c6 | 333 | |
b13342e9 RL |
334 | unsigned long ERR_get_error_all(const char **file, int *line, |
335 | const char **func, | |
336 | const char **data, int *flags) | |
337 | { | |
b4570683 | 338 | return get_error_values(EV_POP, file, line, func, data, flags); |
b13342e9 RL |
339 | } |
340 | ||
936c2b9e | 341 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
af2f14ac RL |
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 | ||
6b691a5c | 347 | unsigned long ERR_get_error_line_data(const char **file, int *line, |
0f113f3e MC |
348 | const char **data, int *flags) |
349 | { | |
b4570683 | 350 | return get_error_values(EV_POP, file, line, NULL, data, flags); |
0f113f3e | 351 | } |
b13342e9 | 352 | #endif |
273db408 | 353 | |
6b691a5c | 354 | unsigned long ERR_peek_error(void) |
0f113f3e | 355 | { |
b4570683 | 356 | return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL); |
0f113f3e | 357 | } |
d02b48c6 | 358 | |
a14e2d9d | 359 | unsigned long ERR_peek_error_line(const char **file, int *line) |
0f113f3e | 360 | { |
b4570683 | 361 | return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL); |
b13342e9 RL |
362 | } |
363 | ||
364 | unsigned long ERR_peek_error_func(const char **func) | |
365 | { | |
b4570683 | 366 | return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL); |
b13342e9 RL |
367 | } |
368 | ||
369 | unsigned long ERR_peek_error_data(const char **data, int *flags) | |
370 | { | |
b4570683 | 371 | return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags); |
b13342e9 RL |
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 | { | |
b4570683 | 378 | return get_error_values(EV_PEEK, file, line, func, data, flags); |
0f113f3e | 379 | } |
0fc5cf08 | 380 | |
936c2b9e | 381 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
6b691a5c | 382 | unsigned long ERR_peek_error_line_data(const char **file, int *line, |
0f113f3e MC |
383 | const char **data, int *flags) |
384 | { | |
b4570683 | 385 | return get_error_values(EV_PEEK, file, line, NULL, data, flags); |
0f113f3e | 386 | } |
b13342e9 | 387 | #endif |
273db408 BM |
388 | |
389 | unsigned long ERR_peek_last_error(void) | |
0f113f3e | 390 | { |
b4570683 | 391 | return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL); |
0f113f3e | 392 | } |
273db408 BM |
393 | |
394 | unsigned long ERR_peek_last_error_line(const char **file, int *line) | |
0f113f3e | 395 | { |
b4570683 | 396 | return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL); |
b13342e9 RL |
397 | } |
398 | ||
399 | unsigned long ERR_peek_last_error_func(const char **func) | |
400 | { | |
b4570683 | 401 | return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL); |
b13342e9 RL |
402 | } |
403 | ||
404 | unsigned long ERR_peek_last_error_data(const char **data, int *flags) | |
405 | { | |
b4570683 | 406 | return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags); |
b13342e9 RL |
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 | { | |
b4570683 | 413 | return get_error_values(EV_PEEK_LAST, file, line, func, data, flags); |
0f113f3e | 414 | } |
273db408 | 415 | |
936c2b9e | 416 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
a14e2d9d | 417 | unsigned long ERR_peek_last_error_line_data(const char **file, int *line, |
0f113f3e MC |
418 | const char **data, int *flags) |
419 | { | |
b4570683 | 420 | return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags); |
0f113f3e | 421 | } |
b13342e9 | 422 | #endif |
0f113f3e | 423 | |
b4570683 RS |
424 | static unsigned long get_error_values(ERR_GET_ACTION g, |
425 | const char **file, int *line, | |
426 | const char **func, | |
b13342e9 | 427 | const char **data, int *flags) |
0f113f3e MC |
428 | { |
429 | int i = 0; | |
430 | ERR_STATE *es; | |
431 | unsigned long ret; | |
432 | ||
78715dcc | 433 | es = ossl_err_get_state_int(); |
4fc426b7 BE |
434 | if (es == NULL) |
435 | return 0; | |
0f113f3e | 436 | |
b4570683 RS |
437 | /* |
438 | * Clear anything that should have been cleared earlier. We do this | |
439 | * here because this doesn't have constant-time issues. | |
440 | */ | |
94dc53a3 BE |
441 | while (es->bottom != es->top) { |
442 | if (es->err_flags[es->top] & ERR_FLAG_CLEAR) { | |
10f8b368 | 443 | err_clear(es, es->top, 0); |
94dc53a3 BE |
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; | |
10f8b368 | 450 | err_clear(es, es->bottom, 0); |
94dc53a3 BE |
451 | continue; |
452 | } | |
453 | break; | |
454 | } | |
455 | ||
b4570683 | 456 | /* If everything has been cleared, the stack is empty. */ |
0f113f3e MC |
457 | if (es->bottom == es->top) |
458 | return 0; | |
94dc53a3 | 459 | |
b4570683 RS |
460 | /* Which error, the top of stack (latest one) or the first one? */ |
461 | if (g == EV_PEEK_LAST) | |
462 | i = es->top; | |
0f113f3e | 463 | else |
b4570683 | 464 | i = (es->bottom + 1) % ERR_NUM_ERRORS; |
0f113f3e MC |
465 | |
466 | ret = es->err_buffer[i]; | |
b4570683 | 467 | if (g == EV_POP) { |
0f113f3e MC |
468 | es->bottom = i; |
469 | es->err_buffer[i] = 0; | |
470 | } | |
471 | ||
df082268 DDO |
472 | if (file != NULL) { |
473 | *file = es->err_file[i]; | |
474 | if (*file == NULL) | |
475 | *file = ""; | |
0f113f3e | 476 | } |
df082268 DDO |
477 | if (line != NULL) |
478 | *line = es->err_line[i]; | |
b13342e9 RL |
479 | if (func != NULL) { |
480 | *func = es->err_func[i]; | |
481 | if (*func == NULL) | |
df082268 | 482 | *func = ""; |
b13342e9 | 483 | } |
df082268 DDO |
484 | if (flags != NULL) |
485 | *flags = es->err_data_flags[i]; | |
0f113f3e | 486 | if (data == NULL) { |
b4570683 | 487 | if (g == EV_POP) { |
10f8b368 | 488 | err_clear_data(es, i, 0); |
0f113f3e MC |
489 | } |
490 | } else { | |
df082268 DDO |
491 | *data = es->err_data[i]; |
492 | if (*data == NULL) { | |
0f113f3e MC |
493 | *data = ""; |
494 | if (flags != NULL) | |
495 | *flags = 0; | |
0f113f3e MC |
496 | } |
497 | } | |
498 | return ret; | |
499 | } | |
d02b48c6 | 500 | |
63132c53 RL |
501 | void ossl_err_string_int(unsigned long e, const char *func, |
502 | char *buf, size_t len) | |
0f113f3e | 503 | { |
71f2994b RL |
504 | char lsbuf[64], rsbuf[256]; |
505 | const char *ls, *rs = NULL; | |
63132c53 | 506 | unsigned long l, r; |
0f113f3e | 507 | |
e5c13615 MC |
508 | if (len == 0) |
509 | return; | |
510 | ||
0f113f3e | 511 | l = ERR_GET_LIB(e); |
0f113f3e | 512 | ls = ERR_lib_error_string(e); |
fa4dd546 | 513 | if (ls == NULL) { |
0f113f3e | 514 | BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l); |
fa4dd546 RS |
515 | ls = lsbuf; |
516 | } | |
517 | ||
71f2994b RL |
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 | */ | |
fa4dd546 | 524 | r = ERR_GET_REASON(e); |
63132c53 | 525 | #ifndef OPENSSL_NO_ERR |
71f2994b RL |
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 | } | |
63132c53 | 532 | #endif |
fa4dd546 | 533 | if (rs == NULL) { |
9350aaa4 DDO |
534 | BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", |
535 | r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET)); | |
fa4dd546 RS |
536 | rs = rsbuf; |
537 | } | |
0f113f3e | 538 | |
63132c53 | 539 | BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs); |
0f113f3e | 540 | if (strlen(buf) == len - 1) { |
fa4dd546 | 541 | /* Didn't fit; use a minimal format. */ |
63132c53 | 542 | BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r); |
0f113f3e MC |
543 | } |
544 | } | |
e5c84d51 | 545 | |
63132c53 RL |
546 | |
547 | void ERR_error_string_n(unsigned long e, char *buf, size_t len) | |
548 | { | |
549 | ossl_err_string_int(e, "", buf, len); | |
550 | } | |
551 | ||
0f113f3e MC |
552 | /* |
553 | * ERR_error_string_n should be used instead for ret != NULL as | |
554 | * ERR_error_string cannot know how large the buffer is | |
555 | */ | |
e5c84d51 | 556 | char *ERR_error_string(unsigned long e, char *ret) |
0f113f3e MC |
557 | { |
558 | static char buf[256]; | |
e5c84d51 | 559 | |
0f113f3e MC |
560 | if (ret == NULL) |
561 | ret = buf; | |
fa4dd546 | 562 | ERR_error_string_n(e, ret, (int)sizeof(buf)); |
0f113f3e MC |
563 | return ret; |
564 | } | |
d02b48c6 | 565 | |
6b691a5c | 566 | const char *ERR_lib_error_string(unsigned long e) |
0f113f3e MC |
567 | { |
568 | ERR_STRING_DATA d, *p; | |
569 | unsigned long l; | |
d02b48c6 | 570 | |
c2e4e5d2 RL |
571 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
572 | return NULL; | |
573 | } | |
7b8e12d2 | 574 | |
0f113f3e MC |
575 | l = ERR_GET_LIB(e); |
576 | d.error = ERR_PACK(l, 0, 0); | |
3e47caff | 577 | p = int_err_get_item(&d); |
0f113f3e MC |
578 | return ((p == NULL) ? NULL : p->string); |
579 | } | |
d02b48c6 | 580 | |
936c2b9e | 581 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
6b691a5c | 582 | const char *ERR_func_error_string(unsigned long e) |
0f113f3e | 583 | { |
b13342e9 | 584 | return NULL; |
0f113f3e | 585 | } |
b13342e9 | 586 | #endif |
d02b48c6 | 587 | |
6b691a5c | 588 | const char *ERR_reason_error_string(unsigned long e) |
0f113f3e MC |
589 | { |
590 | ERR_STRING_DATA d, *p = NULL; | |
591 | unsigned long l, r; | |
592 | ||
c2e4e5d2 RL |
593 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
594 | return NULL; | |
595 | } | |
7b8e12d2 | 596 | |
71f2994b RL |
597 | /* |
598 | * ERR_reason_error_string() can't safely return system error strings, | |
599 | * since openssl_strerror_r() needs a buffer for thread safety, and we | |
600 | * haven't got one that would serve any sensible purpose. | |
601 | */ | |
602 | if (ERR_SYSTEM_ERROR(e)) | |
603 | return NULL; | |
604 | ||
0f113f3e MC |
605 | l = ERR_GET_LIB(e); |
606 | r = ERR_GET_REASON(e); | |
607 | d.error = ERR_PACK(l, 0, r); | |
3e47caff | 608 | p = int_err_get_item(&d); |
12a765a5 | 609 | if (p == NULL) { |
0f113f3e | 610 | d.error = ERR_PACK(0, 0, r); |
3e47caff | 611 | p = int_err_get_item(&d); |
0f113f3e MC |
612 | } |
613 | return ((p == NULL) ? NULL : p->string); | |
614 | } | |
d02b48c6 | 615 | |
edb7afb8 | 616 | static void err_delete_thread_state(void *unused) |
0f113f3e | 617 | { |
af6de400 | 618 | ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local); |
8509dcc9 AG |
619 | if (state == NULL) |
620 | return; | |
0f113f3e | 621 | |
8509dcc9 AG |
622 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
623 | ERR_STATE_free(state); | |
0f113f3e | 624 | } |
d02b48c6 | 625 | |
00db8c60 | 626 | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
21e00174 RL |
627 | void ERR_remove_thread_state(void *dummy) |
628 | { | |
629 | } | |
630 | #endif | |
631 | ||
00db8c60 | 632 | #ifndef OPENSSL_NO_DEPRECATED_1_0_0 |
4c329696 | 633 | void ERR_remove_state(unsigned long pid) |
0f113f3e | 634 | { |
0f113f3e | 635 | } |
4c329696 GT |
636 | #endif |
637 | ||
c2e4e5d2 | 638 | DEFINE_RUN_ONCE_STATIC(err_do_init) |
8509dcc9 | 639 | { |
73bc5370 | 640 | set_err_thread_local = 1; |
c2e4e5d2 | 641 | return CRYPTO_THREAD_init_local(&err_thread_local, NULL); |
8509dcc9 AG |
642 | } |
643 | ||
78715dcc | 644 | ERR_STATE *ossl_err_get_state_int(void) |
0f113f3e | 645 | { |
eb2b9892 | 646 | ERR_STATE *state; |
91c54730 | 647 | int saveerrno = get_last_sys_error(); |
eb2b9892 BE |
648 | |
649 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) | |
650 | return NULL; | |
8509dcc9 | 651 | |
c2e4e5d2 RL |
652 | if (!RUN_ONCE(&err_init, err_do_init)) |
653 | return NULL; | |
8509dcc9 AG |
654 | |
655 | state = CRYPTO_THREAD_get_local(&err_thread_local); | |
6b49b308 BE |
656 | if (state == (ERR_STATE*)-1) |
657 | return NULL; | |
8509dcc9 AG |
658 | |
659 | if (state == NULL) { | |
6b49b308 BE |
660 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1)) |
661 | return NULL; | |
662 | ||
cdb10bae | 663 | if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) { |
6b49b308 | 664 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
8509dcc9 | 665 | return NULL; |
cdb10bae | 666 | } |
8509dcc9 | 667 | |
6913f5fe | 668 | if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state) |
6b49b308 | 669 | || !CRYPTO_THREAD_set_local(&err_thread_local, state)) { |
8509dcc9 | 670 | ERR_STATE_free(state); |
6b49b308 | 671 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
8509dcc9 | 672 | return NULL; |
0f113f3e | 673 | } |
0fc32b07 MC |
674 | |
675 | /* Ignore failures from these */ | |
f672aee4 | 676 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
0f113f3e | 677 | } |
8509dcc9 | 678 | |
91c54730 | 679 | set_sys_error(saveerrno); |
8509dcc9 | 680 | return state; |
0f113f3e | 681 | } |
d02b48c6 | 682 | |
936c2b9e | 683 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
e5d4233f RL |
684 | ERR_STATE *ERR_get_state(void) |
685 | { | |
78715dcc | 686 | return ossl_err_get_state_int(); |
e5d4233f RL |
687 | } |
688 | #endif | |
689 | ||
690 | ||
eb2b9892 BE |
691 | /* |
692 | * err_shelve_state returns the current thread local error state | |
693 | * and freezes the error module until err_unshelve_state is called. | |
694 | */ | |
695 | int err_shelve_state(void **state) | |
696 | { | |
91c54730 RL |
697 | int saveerrno = get_last_sys_error(); |
698 | ||
df1f538f VD |
699 | /* |
700 | * Note, at present our only caller is OPENSSL_init_crypto(), indirectly | |
701 | * via ossl_init_load_crypto_nodelete(), by which point the requested | |
702 | * "base" initialization has already been performed, so the below call is a | |
703 | * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return. | |
704 | * | |
705 | * If are no other valid callers of this function, the call below can be | |
706 | * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are | |
707 | * potential uses that are not from inside OPENSSL_init_crypto(), then this | |
708 | * call is needed, but some care is required to make sure that the re-entry | |
709 | * remains a NOOP. | |
710 | */ | |
eb2b9892 BE |
711 | if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL)) |
712 | return 0; | |
713 | ||
714 | if (!RUN_ONCE(&err_init, err_do_init)) | |
715 | return 0; | |
716 | ||
717 | *state = CRYPTO_THREAD_get_local(&err_thread_local); | |
718 | if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1)) | |
719 | return 0; | |
720 | ||
91c54730 | 721 | set_sys_error(saveerrno); |
eb2b9892 BE |
722 | return 1; |
723 | } | |
724 | ||
725 | /* | |
726 | * err_unshelve_state restores the error state that was returned | |
727 | * by err_shelve_state previously. | |
728 | */ | |
729 | void err_unshelve_state(void* state) | |
730 | { | |
731 | if (state != (void*)-1) | |
732 | CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state); | |
733 | } | |
734 | ||
6b691a5c | 735 | int ERR_get_next_error_library(void) |
0f113f3e | 736 | { |
3e47caff RS |
737 | int ret; |
738 | ||
eb2b9892 | 739 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
c2e4e5d2 | 740 | return 0; |
7b8e12d2 | 741 | |
cd3f8c1b RS |
742 | if (!CRYPTO_THREAD_write_lock(err_string_lock)) |
743 | return 0; | |
3e47caff | 744 | ret = int_err_library_number++; |
7b8e12d2 | 745 | CRYPTO_THREAD_unlock(err_string_lock); |
3e47caff | 746 | return ret; |
0f113f3e | 747 | } |
58964a49 | 748 | |
10f8b368 RL |
749 | static int err_set_error_data_int(char *data, size_t size, int flags, |
750 | int deallocate) | |
0f113f3e MC |
751 | { |
752 | ERR_STATE *es; | |
58964a49 | 753 | |
78715dcc | 754 | es = ossl_err_get_state_int(); |
4fc426b7 | 755 | if (es == NULL) |
fa6b1ee1 | 756 | return 0; |
58964a49 | 757 | |
10f8b368 | 758 | err_clear_data(es, es->top, deallocate); |
8a4dc425 | 759 | err_set_data(es, es->top, data, size, flags); |
fa6b1ee1 MC |
760 | |
761 | return 1; | |
762 | } | |
763 | ||
764 | void ERR_set_error_data(char *data, int flags) | |
765 | { | |
766 | /* | |
767 | * This function is void so we cannot propagate the error return. Since it | |
768 | * is also in the public API we can't change the return type. | |
10f8b368 RL |
769 | * |
770 | * We estimate the size of the data. If it's not flagged as allocated, | |
771 | * then this is safe, and if it is flagged as allocated, then our size | |
772 | * may be smaller than the actual allocation, but that doesn't matter | |
773 | * too much, the buffer will remain untouched or will eventually be | |
774 | * reallocated to a new size. | |
775 | * | |
776 | * callers should be advised that this function takes over ownership of | |
777 | * the allocated memory, i.e. they can't count on the pointer to remain | |
778 | * valid. | |
fa6b1ee1 | 779 | */ |
10f8b368 | 780 | err_set_error_data_int(data, strlen(data) + 1, flags, 1); |
0f113f3e | 781 | } |
58964a49 | 782 | |
4565c3e3 | 783 | void ERR_add_error_data(int num, ...) |
0f113f3e MC |
784 | { |
785 | va_list args; | |
786 | va_start(args, num); | |
787 | ERR_add_error_vdata(num, args); | |
788 | va_end(args); | |
789 | } | |
78c45722 DSH |
790 | |
791 | void ERR_add_error_vdata(int num, va_list args) | |
0f113f3e | 792 | { |
8908d18c | 793 | int i, len, size; |
10f8b368 RL |
794 | int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING; |
795 | char *str, *arg; | |
8908d18c RS |
796 | ERR_STATE *es; |
797 | ||
798 | /* Get the current error data; if an allocated string get it. */ | |
78715dcc | 799 | es = ossl_err_get_state_int(); |
8908d18c RS |
800 | if (es == NULL) |
801 | return; | |
802 | i = es->top; | |
0f113f3e | 803 | |
10f8b368 RL |
804 | /* |
805 | * If err_data is allocated already, re-use the space. | |
806 | * Otherwise, allocate a small new buffer. | |
807 | */ | |
808 | if ((es->err_data_flags[i] & flags) == flags) { | |
809 | str = es->err_data[i]; | |
810 | size = es->err_data_size[i]; | |
811 | ||
812 | /* | |
813 | * To protect the string we just grabbed from tampering by other | |
814 | * functions we may call, or to protect them from freeing a pointer | |
815 | * that may no longer be valid at that point, we clear away the | |
816 | * data pointer and the flags. We will set them again at the end | |
817 | * of this function. | |
818 | */ | |
819 | es->err_data[i] = NULL; | |
820 | es->err_data_flags[i] = 0; | |
821 | } else if ((str = OPENSSL_malloc(size = 81)) == NULL) { | |
0f113f3e | 822 | return; |
10f8b368 RL |
823 | } else { |
824 | str[0] = '\0'; | |
cdb10bae | 825 | } |
10f8b368 | 826 | len = strlen(str); |
8908d18c | 827 | |
51fe9b00 | 828 | while (--num >= 0) { |
8908d18c RS |
829 | arg = va_arg(args, char *); |
830 | if (arg == NULL) | |
831 | arg = "<NULL>"; | |
832 | len += strlen(arg); | |
189dbdd9 | 833 | if (len >= size) { |
10f8b368 RL |
834 | char *p; |
835 | ||
8908d18c | 836 | size = len + 20; |
189dbdd9 | 837 | p = OPENSSL_realloc(str, size); |
0791bef0 RS |
838 | if (p == NULL) { |
839 | OPENSSL_free(str); | |
840 | return; | |
0f113f3e | 841 | } |
0791bef0 | 842 | str = p; |
0f113f3e | 843 | } |
189dbdd9 | 844 | OPENSSL_strlcat(str, arg, (size_t)size); |
0f113f3e | 845 | } |
10f8b368 | 846 | if (!err_set_error_data_int(str, size, flags, 0)) |
fa6b1ee1 | 847 | OPENSSL_free(str); |
0f113f3e | 848 | } |
54f64516 | 849 | |
f658a3b6 AP |
850 | void err_clear_last_constant_time(int clear) |
851 | { | |
852 | ERR_STATE *es; | |
853 | int top; | |
854 | ||
78715dcc | 855 | es = ossl_err_get_state_int(); |
f658a3b6 AP |
856 | if (es == NULL) |
857 | return; | |
858 | ||
859 | top = es->top; | |
860 | ||
94dc53a3 BE |
861 | /* |
862 | * Flag error as cleared but remove it elsewhere to avoid two errors | |
863 | * accessing the same error stack location, revealing timing information. | |
864 | */ | |
865 | clear = constant_time_select_int(constant_time_eq_int(clear, 0), | |
866 | 0, ERR_FLAG_CLEAR); | |
867 | es->err_flags[top] |= clear; | |
f658a3b6 | 868 | } |