]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
f32b0abe | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
0a150c5c | 3 | * |
aa6bb135 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
0a150c5c | 8 | */ |
d02b48c6 RE |
9 | |
10 | #include <stdio.h> | |
4565c3e3 | 11 | #include <stdarg.h> |
0a150c5c | 12 | #include <string.h> |
176db6dc RS |
13 | #include "internal/cryptlib_int.h" |
14 | #include "internal/err.h" | |
15 | #include "internal/err_int.h" | |
52df25cf | 16 | #include <openssl/err.h> |
ec577822 | 17 | #include <openssl/crypto.h> |
ec577822 | 18 | #include <openssl/buffer.h> |
e5c84d51 | 19 | #include <openssl/bio.h> |
98186eb4 | 20 | #include <openssl/opensslconf.h> |
176db6dc | 21 | #include "internal/thread_once.h" |
3c1d6bbc | 22 | |
52df25cf | 23 | static int err_load_strings(const ERR_STRING_DATA *str); |
cf5bfbfc | 24 | |
58964a49 | 25 | static void ERR_STATE_free(ERR_STATE *s); |
cf1b7d96 | 26 | #ifndef OPENSSL_NO_ERR |
0f113f3e MC |
27 | static ERR_STRING_DATA ERR_str_libraries[] = { |
28 | {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"}, | |
29 | {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"}, | |
30 | {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"}, | |
31 | {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"}, | |
32 | {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"}, | |
33 | {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"}, | |
34 | {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"}, | |
35 | {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"}, | |
36 | {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"}, | |
37 | {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"}, | |
38 | {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"}, | |
39 | {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"}, | |
40 | {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"}, | |
41 | {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"}, | |
42 | {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"}, | |
43 | {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"}, | |
44 | {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"}, | |
45 | {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"}, | |
46 | {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"}, | |
47 | {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"}, | |
48 | {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"}, | |
49 | {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"}, | |
50 | {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"}, | |
51 | {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"}, | |
52 | {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"}, | |
53 | {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"}, | |
54 | {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"}, | |
49844486 | 55 | {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"}, |
0f113f3e MC |
56 | {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"}, |
57 | {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"}, | |
58 | {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"}, | |
0cea8832 | 59 | {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"}, |
079a1a90 | 60 | {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"}, |
3e30fa0a | 61 | {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"}, |
71a5516d | 62 | {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"}, |
0f113f3e MC |
63 | {0, NULL}, |
64 | }; | |
65 | ||
66 | static ERR_STRING_DATA ERR_str_functs[] = { | |
67 | {ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"}, | |
68 | {ERR_PACK(0, SYS_F_CONNECT, 0), "connect"}, | |
69 | {ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"}, | |
70 | {ERR_PACK(0, SYS_F_SOCKET, 0), "socket"}, | |
71 | {ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"}, | |
72 | {ERR_PACK(0, SYS_F_BIND, 0), "bind"}, | |
73 | {ERR_PACK(0, SYS_F_LISTEN, 0), "listen"}, | |
74 | {ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"}, | |
75 | # ifdef OPENSSL_SYS_WINDOWS | |
76 | {ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"}, | |
77 | # endif | |
78 | {ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"}, | |
79 | {ERR_PACK(0, SYS_F_FREAD, 0), "fread"}, | |
83be2778 RL |
80 | {ERR_PACK(0, SYS_F_GETADDRINFO, 0), "getaddrinfo"}, |
81 | {ERR_PACK(0, SYS_F_GETNAMEINFO, 0), "getnameinfo"}, | |
d33b215b RL |
82 | {ERR_PACK(0, SYS_F_SETSOCKOPT, 0), "setsockopt"}, |
83 | {ERR_PACK(0, SYS_F_GETSOCKOPT, 0), "getsockopt"}, | |
84 | {ERR_PACK(0, SYS_F_GETSOCKNAME, 0), "getsockname"}, | |
c86d1f19 | 85 | {ERR_PACK(0, SYS_F_GETHOSTBYNAME, 0), "gethostbyname"}, |
595b2a42 | 86 | {ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"}, |
619eb33a RL |
87 | {ERR_PACK(0, SYS_F_OPEN, 0), "open"}, |
88 | {ERR_PACK(0, SYS_F_CLOSE, 0), "close"}, | |
89 | {ERR_PACK(0, SYS_F_IOCTL, 0), "ioctl"}, | |
970f467a | 90 | {ERR_PACK(0, SYS_F_STAT, 0), "stat"}, |
354ab365 | 91 | {ERR_PACK(0, SYS_F_FCNTL, 0), "fcntl"}, |
0f113f3e MC |
92 | {0, NULL}, |
93 | }; | |
94 | ||
95 | static ERR_STRING_DATA ERR_str_reasons[] = { | |
96 | {ERR_R_SYS_LIB, "system lib"}, | |
97 | {ERR_R_BN_LIB, "BN lib"}, | |
98 | {ERR_R_RSA_LIB, "RSA lib"}, | |
99 | {ERR_R_DH_LIB, "DH lib"}, | |
100 | {ERR_R_EVP_LIB, "EVP lib"}, | |
101 | {ERR_R_BUF_LIB, "BUF lib"}, | |
102 | {ERR_R_OBJ_LIB, "OBJ lib"}, | |
103 | {ERR_R_PEM_LIB, "PEM lib"}, | |
104 | {ERR_R_DSA_LIB, "DSA lib"}, | |
105 | {ERR_R_X509_LIB, "X509 lib"}, | |
106 | {ERR_R_ASN1_LIB, "ASN1 lib"}, | |
0f113f3e | 107 | {ERR_R_EC_LIB, "EC lib"}, |
0f113f3e MC |
108 | {ERR_R_BIO_LIB, "BIO lib"}, |
109 | {ERR_R_PKCS7_LIB, "PKCS7 lib"}, | |
110 | {ERR_R_X509V3_LIB, "X509V3 lib"}, | |
0f113f3e | 111 | {ERR_R_ENGINE_LIB, "ENGINE lib"}, |
c785fd48 | 112 | {ERR_R_UI_LIB, "UI lib"}, |
71a5516d | 113 | {ERR_R_OSSL_STORE_LIB, "STORE lib"}, |
0f113f3e MC |
114 | {ERR_R_ECDSA_LIB, "ECDSA lib"}, |
115 | ||
116 | {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"}, | |
0f113f3e MC |
117 | {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"}, |
118 | ||
119 | {ERR_R_FATAL, "fatal"}, | |
120 | {ERR_R_MALLOC_FAILURE, "malloc failure"}, | |
121 | {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, | |
122 | "called a function you should not call"}, | |
123 | {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"}, | |
124 | {ERR_R_INTERNAL_ERROR, "internal error"}, | |
125 | {ERR_R_DISABLED, "called a function that was disabled at compile-time"}, | |
302f7588 | 126 | {ERR_R_INIT_FAIL, "init fail"}, |
9a32dcf4 | 127 | {ERR_R_OPERATION_FAIL, "operation fail"}, |
0f113f3e MC |
128 | |
129 | {0, NULL}, | |
130 | }; | |
d02b48c6 | 131 | #endif |
0a150c5c | 132 | |
8509dcc9 | 133 | static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT; |
73bc5370 | 134 | static int set_err_thread_local; |
8509dcc9 AG |
135 | static CRYPTO_THREAD_LOCAL err_thread_local; |
136 | ||
7b8e12d2 AG |
137 | static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT; |
138 | static CRYPTO_RWLOCK *err_string_lock; | |
139 | ||
566bdf2b | 140 | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); |
566bdf2b | 141 | |
0f113f3e | 142 | /* |
3e47caff | 143 | * The internal state |
0f113f3e | 144 | */ |
f3f1cf84 | 145 | |
3c1d6bbc | 146 | static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; |
0f113f3e | 147 | static int int_err_library_number = ERR_LIB_USER; |
566bdf2b | 148 | |
0f113f3e MC |
149 | static unsigned long get_error_values(int inc, int top, const char **file, |
150 | int *line, const char **data, | |
151 | int *flags); | |
566bdf2b | 152 | |
3c1d6bbc | 153 | static unsigned long err_string_data_hash(const ERR_STRING_DATA *a) |
0f113f3e MC |
154 | { |
155 | unsigned long ret, l; | |
156 | ||
157 | l = a->error; | |
158 | ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l); | |
159 | return (ret ^ ret % 19 * 13); | |
160 | } | |
3c1d6bbc | 161 | |
3c1d6bbc | 162 | static int err_string_data_cmp(const ERR_STRING_DATA *a, |
0f113f3e MC |
163 | const ERR_STRING_DATA *b) |
164 | { | |
c784a838 RS |
165 | if (a->error == b->error) |
166 | return 0; | |
167 | return a->error > b->error ? 1 : -1; | |
0f113f3e MC |
168 | } |
169 | ||
566bdf2b | 170 | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d) |
0f113f3e | 171 | { |
3e47caff | 172 | ERR_STRING_DATA *p = NULL; |
78f79235 | 173 | |
7b8e12d2 | 174 | CRYPTO_THREAD_read_lock(err_string_lock); |
52df25cf | 175 | p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d); |
7b8e12d2 | 176 | CRYPTO_THREAD_unlock(err_string_lock); |
78f79235 | 177 | |
0f113f3e MC |
178 | return p; |
179 | } | |
567fef89 | 180 | |
758f942b | 181 | #ifndef OPENSSL_NO_ERR |
0f113f3e MC |
182 | # define NUM_SYS_STR_REASONS 127 |
183 | # define LEN_SYS_STR_REASON 32 | |
0a150c5c BM |
184 | |
185 | static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1]; | |
0f113f3e MC |
186 | /* |
187 | * SYS_str_reasons is filled with copies of strerror() results at | |
188 | * initialization. 'errno' values up to 127 should cover all usual errors, | |
189 | * others will be displayed numerically by ERR_error_string. It is crucial | |
190 | * that we have something for each reason code that occurs in | |
191 | * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(), | |
192 | * which always gets an errno value and never one of those 'standard' reason | |
193 | * codes. | |
194 | */ | |
0a150c5c | 195 | |
41a15c4f | 196 | static void build_SYS_str_reasons(void) |
0f113f3e MC |
197 | { |
198 | /* OPENSSL_malloc cannot be used here, use static storage instead */ | |
199 | static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON]; | |
0f113f3e | 200 | static int init = 1; |
3e47caff | 201 | int i; |
0f113f3e | 202 | |
7b8e12d2 | 203 | CRYPTO_THREAD_write_lock(err_string_lock); |
0f113f3e | 204 | if (!init) { |
7b8e12d2 | 205 | CRYPTO_THREAD_unlock(err_string_lock); |
0f113f3e MC |
206 | return; |
207 | } | |
208 | ||
209 | for (i = 1; i <= NUM_SYS_STR_REASONS; i++) { | |
210 | ERR_STRING_DATA *str = &SYS_str_reasons[i - 1]; | |
211 | ||
52df25cf | 212 | str->error = ERR_PACK(ERR_LIB_SYS, 0, i); |
0f113f3e MC |
213 | if (str->string == NULL) { |
214 | char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]); | |
7d37818d | 215 | if (openssl_strerror_r(i, *dest, sizeof(*dest))) |
0f113f3e | 216 | str->string = *dest; |
0f113f3e MC |
217 | } |
218 | if (str->string == NULL) | |
219 | str->string = "unknown"; | |
220 | } | |
221 | ||
222 | /* | |
223 | * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as | |
224 | * required by ERR_load_strings. | |
225 | */ | |
226 | ||
227 | init = 0; | |
228 | ||
7b8e12d2 | 229 | CRYPTO_THREAD_unlock(err_string_lock); |
52df25cf | 230 | err_load_strings(SYS_str_reasons); |
0f113f3e | 231 | } |
d02b48c6 RE |
232 | #endif |
233 | ||
58964a49 | 234 | #define err_clear_data(p,i) \ |
0f113f3e | 235 | do { \ |
b548a1f1 | 236 | if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) \ |
0f113f3e MC |
237 | { \ |
238 | OPENSSL_free((p)->err_data[i]); \ | |
239 | (p)->err_data[i]=NULL; \ | |
240 | } \ | |
241 | (p)->err_data_flags[i]=0; \ | |
242 | } while(0) | |
54f64516 RL |
243 | |
244 | #define err_clear(p,i) \ | |
0f113f3e MC |
245 | do { \ |
246 | (p)->err_flags[i]=0; \ | |
247 | (p)->err_buffer[i]=0; \ | |
248 | err_clear_data(p,i); \ | |
249 | (p)->err_file[i]=NULL; \ | |
250 | (p)->err_line[i]= -1; \ | |
251 | } while(0) | |
58964a49 | 252 | |
6b691a5c | 253 | static void ERR_STATE_free(ERR_STATE *s) |
0f113f3e MC |
254 | { |
255 | int i; | |
58964a49 | 256 | |
0f113f3e MC |
257 | if (s == NULL) |
258 | return; | |
e03ddfae | 259 | |
0f113f3e MC |
260 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
261 | err_clear_data(s, i); | |
262 | } | |
263 | OPENSSL_free(s); | |
264 | } | |
58964a49 | 265 | |
c2e4e5d2 | 266 | DEFINE_RUN_ONCE_STATIC(do_err_strings_init) |
7b8e12d2 | 267 | { |
135648bc | 268 | OPENSSL_init_crypto(0, NULL); |
ed6b2c79 | 269 | err_string_lock = CRYPTO_THREAD_glock_new("err_string"); |
52df25cf RS |
270 | int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash, |
271 | err_string_data_cmp); | |
272 | return err_string_lock != NULL && int_error_hash != NULL; | |
ff234405 MC |
273 | } |
274 | ||
275 | void err_cleanup(void) | |
276 | { | |
73bc5370 RS |
277 | if (set_err_thread_local != 0) |
278 | CRYPTO_THREAD_cleanup_local(&err_thread_local); | |
ff234405 MC |
279 | CRYPTO_THREAD_lock_free(err_string_lock); |
280 | err_string_lock = NULL; | |
52df25cf RS |
281 | lh_ERR_STRING_DATA_free(int_error_hash); |
282 | int_error_hash = NULL; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Legacy; pack in the library. | |
287 | */ | |
288 | static void err_patch(int lib, ERR_STRING_DATA *str) | |
289 | { | |
290 | unsigned long plib = ERR_PACK(lib, 0, 0); | |
291 | ||
292 | for (; str->error != 0; str++) | |
293 | str->error |= plib; | |
294 | } | |
295 | ||
296 | /* | |
297 | * Hash in |str| error strings. Assumes the URN_ONCE was done. | |
298 | */ | |
299 | static int err_load_strings(const ERR_STRING_DATA *str) | |
300 | { | |
301 | CRYPTO_THREAD_write_lock(err_string_lock); | |
302 | for (; str->error; str++) | |
303 | (void)lh_ERR_STRING_DATA_insert(int_error_hash, | |
304 | (ERR_STRING_DATA *)str); | |
305 | CRYPTO_THREAD_unlock(err_string_lock); | |
306 | return 1; | |
7b8e12d2 AG |
307 | } |
308 | ||
69588edb | 309 | int ERR_load_ERR_strings(void) |
0f113f3e | 310 | { |
cf1b7d96 | 311 | #ifndef OPENSSL_NO_ERR |
69588edb KR |
312 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
313 | return 0; | |
7b8e12d2 | 314 | |
52df25cf RS |
315 | err_load_strings(ERR_str_libraries); |
316 | err_load_strings(ERR_str_reasons); | |
317 | err_patch(ERR_LIB_SYS, ERR_str_functs); | |
318 | err_load_strings(ERR_str_functs); | |
0f113f3e | 319 | build_SYS_str_reasons(); |
d02b48c6 | 320 | #endif |
69588edb | 321 | return 1; |
0f113f3e | 322 | } |
d02b48c6 | 323 | |
52df25cf | 324 | int ERR_load_strings(int lib, ERR_STRING_DATA *str) |
0f113f3e | 325 | { |
52df25cf RS |
326 | if (ERR_load_ERR_strings() == 0) |
327 | return 0; | |
328 | ||
329 | err_patch(lib, str); | |
330 | err_load_strings(str); | |
331 | return 1; | |
0f113f3e | 332 | } |
d02b48c6 | 333 | |
52df25cf | 334 | int ERR_load_strings_const(const ERR_STRING_DATA *str) |
0f113f3e | 335 | { |
69588edb KR |
336 | if (ERR_load_ERR_strings() == 0) |
337 | return 0; | |
52df25cf | 338 | err_load_strings(str); |
69588edb | 339 | return 1; |
0f113f3e | 340 | } |
cf5bfbfc | 341 | |
c2e4e5d2 | 342 | int ERR_unload_strings(int lib, ERR_STRING_DATA *str) |
0f113f3e | 343 | { |
c2e4e5d2 RL |
344 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
345 | return 0; | |
7b8e12d2 AG |
346 | |
347 | CRYPTO_THREAD_write_lock(err_string_lock); | |
52df25cf RS |
348 | /* |
349 | * We don't need to ERR_PACK the lib, since that was done (to | |
350 | * the table) when it was loaded. | |
351 | */ | |
352 | for (; str->error; str++) | |
353 | (void)lh_ERR_STRING_DATA_delete(int_error_hash, str); | |
7b8e12d2 | 354 | CRYPTO_THREAD_unlock(err_string_lock); |
c2e4e5d2 RL |
355 | |
356 | return 1; | |
0f113f3e | 357 | } |
1738bb61 | 358 | |
b3599dbb | 359 | void err_free_strings_int(void) |
0f113f3e | 360 | { |
c2e4e5d2 RL |
361 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) |
362 | return; | |
0f113f3e | 363 | } |
d02b48c6 | 364 | |
58964a49 RE |
365 | /********************************************************/ |
366 | ||
0f113f3e MC |
367 | void ERR_put_error(int lib, int func, int reason, const char *file, int line) |
368 | { | |
369 | ERR_STATE *es; | |
d02b48c6 | 370 | |
a53955d8 | 371 | #ifdef _OSD_POSIX |
0f113f3e MC |
372 | /* |
373 | * In the BS2000-OSD POSIX subsystem, the compiler generates path names | |
374 | * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to | |
375 | * something sensible. @@@ We shouldn't modify a const string, though. | |
376 | */ | |
377 | if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) { | |
378 | char *end; | |
379 | ||
380 | /* Skip the "*POSIX(" prefix */ | |
381 | file += sizeof("*POSIX(") - 1; | |
382 | end = &file[strlen(file) - 1]; | |
383 | if (*end == ')') | |
384 | *end = '\0'; | |
385 | /* Optional: use the basename of the path only. */ | |
386 | if ((end = strrchr(file, '/')) != NULL) | |
387 | file = &end[1]; | |
388 | } | |
a53955d8 | 389 | #endif |
0f113f3e | 390 | es = ERR_get_state(); |
4fc426b7 BE |
391 | if (es == NULL) |
392 | return; | |
0f113f3e MC |
393 | |
394 | es->top = (es->top + 1) % ERR_NUM_ERRORS; | |
395 | if (es->top == es->bottom) | |
396 | es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS; | |
397 | es->err_flags[es->top] = 0; | |
398 | es->err_buffer[es->top] = ERR_PACK(lib, func, reason); | |
399 | es->err_file[es->top] = file; | |
400 | es->err_line[es->top] = line; | |
401 | err_clear_data(es, es->top); | |
402 | } | |
d02b48c6 | 403 | |
6b691a5c | 404 | void ERR_clear_error(void) |
0f113f3e MC |
405 | { |
406 | int i; | |
407 | ERR_STATE *es; | |
d02b48c6 | 408 | |
0f113f3e | 409 | es = ERR_get_state(); |
4fc426b7 BE |
410 | if (es == NULL) |
411 | return; | |
d02b48c6 | 412 | |
0f113f3e MC |
413 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
414 | err_clear(es, i); | |
415 | } | |
416 | es->top = es->bottom = 0; | |
417 | } | |
d02b48c6 | 418 | |
6b691a5c | 419 | unsigned long ERR_get_error(void) |
0f113f3e | 420 | { |
26a7d938 | 421 | return get_error_values(1, 0, NULL, NULL, NULL, NULL); |
0f113f3e | 422 | } |
d02b48c6 | 423 | |
0f113f3e MC |
424 | unsigned long ERR_get_error_line(const char **file, int *line) |
425 | { | |
26a7d938 | 426 | return get_error_values(1, 0, file, line, NULL, NULL); |
0f113f3e | 427 | } |
d02b48c6 | 428 | |
6b691a5c | 429 | unsigned long ERR_get_error_line_data(const char **file, int *line, |
0f113f3e MC |
430 | const char **data, int *flags) |
431 | { | |
26a7d938 | 432 | return get_error_values(1, 0, file, line, data, flags); |
0f113f3e | 433 | } |
273db408 | 434 | |
6b691a5c | 435 | unsigned long ERR_peek_error(void) |
0f113f3e | 436 | { |
26a7d938 | 437 | return get_error_values(0, 0, NULL, NULL, NULL, NULL); |
0f113f3e | 438 | } |
d02b48c6 | 439 | |
a14e2d9d | 440 | unsigned long ERR_peek_error_line(const char **file, int *line) |
0f113f3e | 441 | { |
26a7d938 | 442 | return get_error_values(0, 0, file, line, NULL, NULL); |
0f113f3e | 443 | } |
0fc5cf08 | 444 | |
6b691a5c | 445 | unsigned long ERR_peek_error_line_data(const char **file, int *line, |
0f113f3e MC |
446 | const char **data, int *flags) |
447 | { | |
26a7d938 | 448 | return get_error_values(0, 0, file, line, data, flags); |
0f113f3e | 449 | } |
273db408 BM |
450 | |
451 | unsigned long ERR_peek_last_error(void) | |
0f113f3e | 452 | { |
26a7d938 | 453 | return get_error_values(0, 1, NULL, NULL, NULL, NULL); |
0f113f3e | 454 | } |
273db408 BM |
455 | |
456 | unsigned long ERR_peek_last_error_line(const char **file, int *line) | |
0f113f3e | 457 | { |
26a7d938 | 458 | return get_error_values(0, 1, file, line, NULL, NULL); |
0f113f3e | 459 | } |
273db408 | 460 | |
a14e2d9d | 461 | unsigned long ERR_peek_last_error_line_data(const char **file, int *line, |
0f113f3e MC |
462 | const char **data, int *flags) |
463 | { | |
26a7d938 | 464 | return get_error_values(0, 1, file, line, data, flags); |
0f113f3e MC |
465 | } |
466 | ||
467 | static unsigned long get_error_values(int inc, int top, const char **file, | |
468 | int *line, const char **data, | |
469 | int *flags) | |
470 | { | |
471 | int i = 0; | |
472 | ERR_STATE *es; | |
473 | unsigned long ret; | |
474 | ||
475 | es = ERR_get_state(); | |
4fc426b7 BE |
476 | if (es == NULL) |
477 | return 0; | |
0f113f3e MC |
478 | |
479 | if (inc && top) { | |
480 | if (file) | |
481 | *file = ""; | |
482 | if (line) | |
483 | *line = 0; | |
484 | if (data) | |
485 | *data = ""; | |
486 | if (flags) | |
487 | *flags = 0; | |
488 | ||
489 | return ERR_R_INTERNAL_ERROR; | |
490 | } | |
491 | ||
492 | if (es->bottom == es->top) | |
493 | return 0; | |
494 | if (top) | |
495 | i = es->top; /* last error */ | |
496 | else | |
497 | i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */ | |
498 | ||
499 | ret = es->err_buffer[i]; | |
500 | if (inc) { | |
501 | es->bottom = i; | |
502 | es->err_buffer[i] = 0; | |
503 | } | |
504 | ||
505 | if ((file != NULL) && (line != NULL)) { | |
506 | if (es->err_file[i] == NULL) { | |
507 | *file = "NA"; | |
508 | if (line != NULL) | |
509 | *line = 0; | |
510 | } else { | |
511 | *file = es->err_file[i]; | |
512 | if (line != NULL) | |
513 | *line = es->err_line[i]; | |
514 | } | |
515 | } | |
516 | ||
517 | if (data == NULL) { | |
518 | if (inc) { | |
519 | err_clear_data(es, i); | |
520 | } | |
521 | } else { | |
522 | if (es->err_data[i] == NULL) { | |
523 | *data = ""; | |
524 | if (flags != NULL) | |
525 | *flags = 0; | |
526 | } else { | |
527 | *data = es->err_data[i]; | |
528 | if (flags != NULL) | |
529 | *flags = es->err_data_flags[i]; | |
530 | } | |
531 | } | |
532 | return ret; | |
533 | } | |
d02b48c6 | 534 | |
e5c84d51 | 535 | void ERR_error_string_n(unsigned long e, char *buf, size_t len) |
0f113f3e MC |
536 | { |
537 | char lsbuf[64], fsbuf[64], rsbuf[64]; | |
538 | const char *ls, *fs, *rs; | |
539 | unsigned long l, f, r; | |
540 | ||
e5c13615 MC |
541 | if (len == 0) |
542 | return; | |
543 | ||
0f113f3e | 544 | l = ERR_GET_LIB(e); |
0f113f3e | 545 | ls = ERR_lib_error_string(e); |
fa4dd546 | 546 | if (ls == NULL) { |
0f113f3e | 547 | BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l); |
fa4dd546 RS |
548 | ls = lsbuf; |
549 | } | |
550 | ||
551 | fs = ERR_func_error_string(e); | |
552 | f = ERR_GET_FUNC(e); | |
553 | if (fs == NULL) { | |
0f113f3e | 554 | BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f); |
fa4dd546 RS |
555 | fs = fsbuf; |
556 | } | |
557 | ||
558 | rs = ERR_reason_error_string(e); | |
559 | r = ERR_GET_REASON(e); | |
560 | if (rs == NULL) { | |
0f113f3e | 561 | BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r); |
fa4dd546 RS |
562 | rs = rsbuf; |
563 | } | |
0f113f3e | 564 | |
fa4dd546 | 565 | BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs); |
0f113f3e | 566 | if (strlen(buf) == len - 1) { |
fa4dd546 RS |
567 | /* Didn't fit; use a minimal format. */ |
568 | BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r); | |
0f113f3e MC |
569 | } |
570 | } | |
e5c84d51 | 571 | |
0f113f3e MC |
572 | /* |
573 | * ERR_error_string_n should be used instead for ret != NULL as | |
574 | * ERR_error_string cannot know how large the buffer is | |
575 | */ | |
e5c84d51 | 576 | char *ERR_error_string(unsigned long e, char *ret) |
0f113f3e MC |
577 | { |
578 | static char buf[256]; | |
e5c84d51 | 579 | |
0f113f3e MC |
580 | if (ret == NULL) |
581 | ret = buf; | |
fa4dd546 | 582 | ERR_error_string_n(e, ret, (int)sizeof(buf)); |
0f113f3e MC |
583 | return ret; |
584 | } | |
d02b48c6 | 585 | |
6b691a5c | 586 | const char *ERR_lib_error_string(unsigned long e) |
0f113f3e MC |
587 | { |
588 | ERR_STRING_DATA d, *p; | |
589 | unsigned long l; | |
d02b48c6 | 590 | |
c2e4e5d2 RL |
591 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
592 | return NULL; | |
593 | } | |
7b8e12d2 | 594 | |
0f113f3e MC |
595 | l = ERR_GET_LIB(e); |
596 | d.error = ERR_PACK(l, 0, 0); | |
3e47caff | 597 | p = int_err_get_item(&d); |
0f113f3e MC |
598 | return ((p == NULL) ? NULL : p->string); |
599 | } | |
d02b48c6 | 600 | |
6b691a5c | 601 | const char *ERR_func_error_string(unsigned long e) |
0f113f3e MC |
602 | { |
603 | ERR_STRING_DATA d, *p; | |
604 | unsigned long l, f; | |
605 | ||
c2e4e5d2 RL |
606 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
607 | return NULL; | |
608 | } | |
7b8e12d2 | 609 | |
0f113f3e MC |
610 | l = ERR_GET_LIB(e); |
611 | f = ERR_GET_FUNC(e); | |
612 | d.error = ERR_PACK(l, f, 0); | |
3e47caff | 613 | p = int_err_get_item(&d); |
0f113f3e MC |
614 | return ((p == NULL) ? NULL : p->string); |
615 | } | |
d02b48c6 | 616 | |
6b691a5c | 617 | const char *ERR_reason_error_string(unsigned long e) |
0f113f3e MC |
618 | { |
619 | ERR_STRING_DATA d, *p = NULL; | |
620 | unsigned long l, r; | |
621 | ||
c2e4e5d2 RL |
622 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
623 | return NULL; | |
624 | } | |
7b8e12d2 | 625 | |
0f113f3e MC |
626 | l = ERR_GET_LIB(e); |
627 | r = ERR_GET_REASON(e); | |
628 | d.error = ERR_PACK(l, 0, r); | |
3e47caff | 629 | p = int_err_get_item(&d); |
0f113f3e MC |
630 | if (!p) { |
631 | d.error = ERR_PACK(0, 0, r); | |
3e47caff | 632 | p = int_err_get_item(&d); |
0f113f3e MC |
633 | } |
634 | return ((p == NULL) ? NULL : p->string); | |
635 | } | |
d02b48c6 | 636 | |
21e00174 | 637 | void err_delete_thread_state(void) |
0f113f3e | 638 | { |
af6de400 | 639 | ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local); |
8509dcc9 AG |
640 | if (state == NULL) |
641 | return; | |
0f113f3e | 642 | |
8509dcc9 AG |
643 | CRYPTO_THREAD_set_local(&err_thread_local, NULL); |
644 | ERR_STATE_free(state); | |
0f113f3e | 645 | } |
d02b48c6 | 646 | |
1aca3618 | 647 | #if OPENSSL_API_COMPAT < 0x10100000L |
21e00174 RL |
648 | void ERR_remove_thread_state(void *dummy) |
649 | { | |
650 | } | |
651 | #endif | |
652 | ||
98186eb4 | 653 | #if OPENSSL_API_COMPAT < 0x10000000L |
4c329696 | 654 | void ERR_remove_state(unsigned long pid) |
0f113f3e | 655 | { |
0f113f3e | 656 | } |
4c329696 GT |
657 | #endif |
658 | ||
c2e4e5d2 | 659 | DEFINE_RUN_ONCE_STATIC(err_do_init) |
8509dcc9 | 660 | { |
73bc5370 | 661 | set_err_thread_local = 1; |
c2e4e5d2 | 662 | return CRYPTO_THREAD_init_local(&err_thread_local, NULL); |
8509dcc9 AG |
663 | } |
664 | ||
6b691a5c | 665 | ERR_STATE *ERR_get_state(void) |
0f113f3e | 666 | { |
8509dcc9 AG |
667 | ERR_STATE *state = NULL; |
668 | ||
c2e4e5d2 RL |
669 | if (!RUN_ONCE(&err_init, err_do_init)) |
670 | return NULL; | |
8509dcc9 AG |
671 | |
672 | state = CRYPTO_THREAD_get_local(&err_thread_local); | |
673 | ||
674 | if (state == NULL) { | |
675 | state = OPENSSL_zalloc(sizeof(*state)); | |
676 | if (state == NULL) | |
677 | return NULL; | |
678 | ||
af6de400 BE |
679 | if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE) |
680 | || !CRYPTO_THREAD_set_local(&err_thread_local, state)) { | |
8509dcc9 AG |
681 | ERR_STATE_free(state); |
682 | return NULL; | |
0f113f3e | 683 | } |
0fc32b07 MC |
684 | |
685 | /* Ignore failures from these */ | |
f672aee4 | 686 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
0f113f3e | 687 | } |
8509dcc9 AG |
688 | |
689 | return state; | |
0f113f3e | 690 | } |
d02b48c6 | 691 | |
6b691a5c | 692 | int ERR_get_next_error_library(void) |
0f113f3e | 693 | { |
3e47caff RS |
694 | int ret; |
695 | ||
c2e4e5d2 RL |
696 | if (!RUN_ONCE(&err_string_init, do_err_strings_init)) { |
697 | return 0; | |
698 | } | |
7b8e12d2 AG |
699 | |
700 | CRYPTO_THREAD_write_lock(err_string_lock); | |
3e47caff | 701 | ret = int_err_library_number++; |
7b8e12d2 | 702 | CRYPTO_THREAD_unlock(err_string_lock); |
3e47caff | 703 | return ret; |
0f113f3e | 704 | } |
58964a49 | 705 | |
6b691a5c | 706 | void ERR_set_error_data(char *data, int flags) |
0f113f3e MC |
707 | { |
708 | ERR_STATE *es; | |
709 | int i; | |
58964a49 | 710 | |
0f113f3e | 711 | es = ERR_get_state(); |
4fc426b7 BE |
712 | if (es == NULL) |
713 | return; | |
58964a49 | 714 | |
0f113f3e | 715 | i = es->top; |
58964a49 | 716 | |
0f113f3e MC |
717 | err_clear_data(es, i); |
718 | es->err_data[i] = data; | |
719 | es->err_data_flags[i] = flags; | |
720 | } | |
58964a49 | 721 | |
4565c3e3 | 722 | void ERR_add_error_data(int num, ...) |
0f113f3e MC |
723 | { |
724 | va_list args; | |
725 | va_start(args, num); | |
726 | ERR_add_error_vdata(num, args); | |
727 | va_end(args); | |
728 | } | |
78c45722 DSH |
729 | |
730 | void ERR_add_error_vdata(int num, va_list args) | |
0f113f3e MC |
731 | { |
732 | int i, n, s; | |
733 | char *str, *p, *a; | |
734 | ||
735 | s = 80; | |
736 | str = OPENSSL_malloc(s + 1); | |
737 | if (str == NULL) | |
738 | return; | |
739 | str[0] = '\0'; | |
740 | ||
741 | n = 0; | |
742 | for (i = 0; i < num; i++) { | |
743 | a = va_arg(args, char *); | |
e2dba64c RS |
744 | if (a == NULL) |
745 | a = "<NULL>"; | |
0791bef0 RS |
746 | n += strlen(a); |
747 | if (n > s) { | |
748 | s = n + 20; | |
749 | p = OPENSSL_realloc(str, s + 1); | |
750 | if (p == NULL) { | |
751 | OPENSSL_free(str); | |
752 | return; | |
0f113f3e | 753 | } |
0791bef0 | 754 | str = p; |
0f113f3e | 755 | } |
0791bef0 | 756 | OPENSSL_strlcat(str, a, (size_t)s + 1); |
0f113f3e MC |
757 | } |
758 | ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING); | |
759 | } | |
54f64516 RL |
760 | |
761 | int ERR_set_mark(void) | |
0f113f3e MC |
762 | { |
763 | ERR_STATE *es; | |
54f64516 | 764 | |
0f113f3e | 765 | es = ERR_get_state(); |
4fc426b7 BE |
766 | if (es == NULL) |
767 | return 0; | |
54f64516 | 768 | |
0f113f3e MC |
769 | if (es->bottom == es->top) |
770 | return 0; | |
771 | es->err_flags[es->top] |= ERR_FLAG_MARK; | |
772 | return 1; | |
773 | } | |
54f64516 RL |
774 | |
775 | int ERR_pop_to_mark(void) | |
0f113f3e MC |
776 | { |
777 | ERR_STATE *es; | |
778 | ||
779 | es = ERR_get_state(); | |
4fc426b7 BE |
780 | if (es == NULL) |
781 | return 0; | |
0f113f3e MC |
782 | |
783 | while (es->bottom != es->top | |
784 | && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) { | |
785 | err_clear(es, es->top); | |
8909c2ce | 786 | es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1; |
0f113f3e MC |
787 | } |
788 | ||
789 | if (es->bottom == es->top) | |
790 | return 0; | |
791 | es->err_flags[es->top] &= ~ERR_FLAG_MARK; | |
792 | return 1; | |
793 | } | |
e1a4ff76 RL |
794 | |
795 | int ERR_clear_last_mark(void) | |
796 | { | |
797 | ERR_STATE *es; | |
798 | int top; | |
799 | ||
800 | es = ERR_get_state(); | |
801 | if (es == NULL) | |
802 | return 0; | |
803 | ||
804 | top = es->top; | |
805 | while (es->bottom != top | |
806 | && (es->err_flags[top] & ERR_FLAG_MARK) == 0) { | |
8909c2ce | 807 | top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1; |
e1a4ff76 RL |
808 | } |
809 | ||
810 | if (es->bottom == top) | |
811 | return 0; | |
812 | es->err_flags[top] &= ~ERR_FLAG_MARK; | |
813 | return 1; | |
814 | } |