/src/libressl/crypto/err/err.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: err.c,v 1.48 2019/10/17 14:28:53 jsing Exp $ */ |
2 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | | * All rights reserved. |
4 | | * |
5 | | * This package is an SSL implementation written |
6 | | * by Eric Young (eay@cryptsoft.com). |
7 | | * The implementation was written so as to conform with Netscapes SSL. |
8 | | * |
9 | | * This library is free for commercial and non-commercial use as long as |
10 | | * the following conditions are aheared to. The following conditions |
11 | | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | | * included with this distribution is covered by the same copyright terms |
14 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | | * |
16 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | | * the code are not to be removed. |
18 | | * If this package is used in a product, Eric Young should be given attribution |
19 | | * as the author of the parts of the library used. |
20 | | * This can be in the form of a textual message at program startup or |
21 | | * in documentation (online or textual) provided with the package. |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions |
25 | | * are met: |
26 | | * 1. Redistributions of source code must retain the copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * 2. Redistributions in binary form must reproduce the above copyright |
29 | | * notice, this list of conditions and the following disclaimer in the |
30 | | * documentation and/or other materials provided with the distribution. |
31 | | * 3. All advertising materials mentioning features or use of this software |
32 | | * must display the following acknowledgement: |
33 | | * "This product includes cryptographic software written by |
34 | | * Eric Young (eay@cryptsoft.com)" |
35 | | * The word 'cryptographic' can be left out if the rouines from the library |
36 | | * being used are not cryptographic related :-). |
37 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | | * the apps directory (application code) you must include an acknowledgement: |
39 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | | * SUCH DAMAGE. |
52 | | * |
53 | | * The licence and distribution terms for any publically available version or |
54 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | | * copied and put under another distribution licence |
56 | | * [including the GNU Public Licence.] |
57 | | */ |
58 | | /* ==================================================================== |
59 | | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
60 | | * |
61 | | * Redistribution and use in source and binary forms, with or without |
62 | | * modification, are permitted provided that the following conditions |
63 | | * are met: |
64 | | * |
65 | | * 1. Redistributions of source code must retain the above copyright |
66 | | * notice, this list of conditions and the following disclaimer. |
67 | | * |
68 | | * 2. Redistributions in binary form must reproduce the above copyright |
69 | | * notice, this list of conditions and the following disclaimer in |
70 | | * the documentation and/or other materials provided with the |
71 | | * distribution. |
72 | | * |
73 | | * 3. All advertising materials mentioning features or use of this |
74 | | * software must display the following acknowledgment: |
75 | | * "This product includes software developed by the OpenSSL Project |
76 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
77 | | * |
78 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
79 | | * endorse or promote products derived from this software without |
80 | | * prior written permission. For written permission, please contact |
81 | | * openssl-core@openssl.org. |
82 | | * |
83 | | * 5. Products derived from this software may not be called "OpenSSL" |
84 | | * nor may "OpenSSL" appear in their names without prior written |
85 | | * permission of the OpenSSL Project. |
86 | | * |
87 | | * 6. Redistributions of any form whatsoever must retain the following |
88 | | * acknowledgment: |
89 | | * "This product includes software developed by the OpenSSL Project |
90 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
91 | | * |
92 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
93 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
94 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
95 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
96 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
97 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
98 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
99 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
100 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
101 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
102 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
103 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
104 | | * ==================================================================== |
105 | | * |
106 | | * This product includes cryptographic software written by Eric Young |
107 | | * (eay@cryptsoft.com). This product includes software written by Tim |
108 | | * Hudson (tjh@cryptsoft.com). |
109 | | * |
110 | | */ |
111 | | |
112 | | #include <pthread.h> |
113 | | #include <stdarg.h> |
114 | | #include <stdio.h> |
115 | | #include <string.h> |
116 | | |
117 | | #include <openssl/opensslconf.h> |
118 | | |
119 | | #include <openssl/bio.h> |
120 | | #include <openssl/buffer.h> |
121 | | #include <openssl/crypto.h> |
122 | | #include <openssl/err.h> |
123 | | #include <openssl/lhash.h> |
124 | | |
125 | | DECLARE_LHASH_OF(ERR_STRING_DATA); |
126 | | DECLARE_LHASH_OF(ERR_STATE); |
127 | | |
128 | | static void err_load_strings(int lib, ERR_STRING_DATA *str); |
129 | | |
130 | | static void ERR_STATE_free(ERR_STATE *s); |
131 | | #ifndef OPENSSL_NO_ERR |
132 | | static ERR_STRING_DATA ERR_str_libraries[] = { |
133 | | {ERR_PACK(ERR_LIB_NONE,0,0), "unknown library"}, |
134 | | {ERR_PACK(ERR_LIB_SYS,0,0), "system library"}, |
135 | | {ERR_PACK(ERR_LIB_BN,0,0), "bignum routines"}, |
136 | | {ERR_PACK(ERR_LIB_RSA,0,0), "rsa routines"}, |
137 | | {ERR_PACK(ERR_LIB_DH,0,0), "Diffie-Hellman routines"}, |
138 | | {ERR_PACK(ERR_LIB_EVP,0,0), "digital envelope routines"}, |
139 | | {ERR_PACK(ERR_LIB_BUF,0,0), "memory buffer routines"}, |
140 | | {ERR_PACK(ERR_LIB_OBJ,0,0), "object identifier routines"}, |
141 | | {ERR_PACK(ERR_LIB_PEM,0,0), "PEM routines"}, |
142 | | {ERR_PACK(ERR_LIB_DSA,0,0), "dsa routines"}, |
143 | | {ERR_PACK(ERR_LIB_X509,0,0), "x509 certificate routines"}, |
144 | | {ERR_PACK(ERR_LIB_ASN1,0,0), "asn1 encoding routines"}, |
145 | | {ERR_PACK(ERR_LIB_CONF,0,0), "configuration file routines"}, |
146 | | {ERR_PACK(ERR_LIB_CRYPTO,0,0), "common libcrypto routines"}, |
147 | | {ERR_PACK(ERR_LIB_EC,0,0), "elliptic curve routines"}, |
148 | | {ERR_PACK(ERR_LIB_SSL,0,0), "SSL routines"}, |
149 | | {ERR_PACK(ERR_LIB_BIO,0,0), "BIO routines"}, |
150 | | {ERR_PACK(ERR_LIB_PKCS7,0,0), "PKCS7 routines"}, |
151 | | {ERR_PACK(ERR_LIB_X509V3,0,0), "X509 V3 routines"}, |
152 | | {ERR_PACK(ERR_LIB_PKCS12,0,0), "PKCS12 routines"}, |
153 | | {ERR_PACK(ERR_LIB_RAND,0,0), "random number generator"}, |
154 | | {ERR_PACK(ERR_LIB_DSO,0,0), "DSO support routines"}, |
155 | | {ERR_PACK(ERR_LIB_TS,0,0), "time stamp routines"}, |
156 | | {ERR_PACK(ERR_LIB_ENGINE,0,0), "engine routines"}, |
157 | | {ERR_PACK(ERR_LIB_OCSP,0,0), "OCSP routines"}, |
158 | | {ERR_PACK(ERR_LIB_FIPS,0,0), "FIPS routines"}, |
159 | | {ERR_PACK(ERR_LIB_CMS,0,0), "CMS routines"}, |
160 | | {ERR_PACK(ERR_LIB_HMAC,0,0), "HMAC routines"}, |
161 | | {ERR_PACK(ERR_LIB_GOST,0,0), "GOST routines"}, |
162 | | {0, NULL}, |
163 | | }; |
164 | | |
165 | | static ERR_STRING_DATA ERR_str_functs[] = { |
166 | | {ERR_PACK(0,SYS_F_FOPEN, 0), "fopen"}, |
167 | | {ERR_PACK(0,SYS_F_CONNECT, 0), "connect"}, |
168 | | {ERR_PACK(0,SYS_F_GETSERVBYNAME, 0), "getservbyname"}, |
169 | | {ERR_PACK(0,SYS_F_SOCKET, 0), "socket"}, |
170 | | {ERR_PACK(0,SYS_F_IOCTLSOCKET, 0), "ioctl"}, |
171 | | {ERR_PACK(0,SYS_F_BIND, 0), "bind"}, |
172 | | {ERR_PACK(0,SYS_F_LISTEN, 0), "listen"}, |
173 | | {ERR_PACK(0,SYS_F_ACCEPT, 0), "accept"}, |
174 | | {ERR_PACK(0,SYS_F_OPENDIR, 0), "opendir"}, |
175 | | {ERR_PACK(0,SYS_F_FREAD, 0), "fread"}, |
176 | | {0, NULL}, |
177 | | }; |
178 | | |
179 | | static ERR_STRING_DATA ERR_str_reasons[] = { |
180 | | {ERR_R_SYS_LIB, "system lib"}, |
181 | | {ERR_R_BN_LIB, "BN lib"}, |
182 | | {ERR_R_RSA_LIB, "RSA lib"}, |
183 | | {ERR_R_DH_LIB, "DH lib"}, |
184 | | {ERR_R_EVP_LIB, "EVP lib"}, |
185 | | {ERR_R_BUF_LIB, "BUF lib"}, |
186 | | {ERR_R_OBJ_LIB, "OBJ lib"}, |
187 | | {ERR_R_PEM_LIB, "PEM lib"}, |
188 | | {ERR_R_DSA_LIB, "DSA lib"}, |
189 | | {ERR_R_X509_LIB, "X509 lib"}, |
190 | | {ERR_R_ASN1_LIB, "ASN1 lib"}, |
191 | | {ERR_R_CONF_LIB, "CONF lib"}, |
192 | | {ERR_R_CRYPTO_LIB, "CRYPTO lib"}, |
193 | | {ERR_R_EC_LIB, "EC lib"}, |
194 | | {ERR_R_SSL_LIB, "SSL lib"}, |
195 | | {ERR_R_BIO_LIB, "BIO lib"}, |
196 | | {ERR_R_PKCS7_LIB, "PKCS7 lib"}, |
197 | | {ERR_R_X509V3_LIB, "X509V3 lib"}, |
198 | | {ERR_R_PKCS12_LIB, "PKCS12 lib"}, |
199 | | {ERR_R_RAND_LIB, "RAND lib"}, |
200 | | {ERR_R_DSO_LIB, "DSO lib"}, |
201 | | {ERR_R_ENGINE_LIB, "ENGINE lib"}, |
202 | | {ERR_R_OCSP_LIB, "OCSP lib"}, |
203 | | {ERR_R_TS_LIB, "TS lib"}, |
204 | | |
205 | | {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"}, |
206 | | {ERR_R_BAD_ASN1_OBJECT_HEADER, "bad asn1 object header"}, |
207 | | {ERR_R_BAD_GET_ASN1_OBJECT_CALL, "bad get asn1 object call"}, |
208 | | {ERR_R_EXPECTING_AN_ASN1_SEQUENCE, "expecting an asn1 sequence"}, |
209 | | {ERR_R_ASN1_LENGTH_MISMATCH, "asn1 length mismatch"}, |
210 | | {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"}, |
211 | | |
212 | | {ERR_R_FATAL, "fatal"}, |
213 | | {ERR_R_MALLOC_FAILURE, "malloc failure"}, |
214 | | {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "called a function you should not call"}, |
215 | | {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"}, |
216 | | {ERR_R_INTERNAL_ERROR, "internal error"}, |
217 | | {ERR_R_DISABLED , "called a function that was disabled at compile-time"}, |
218 | | |
219 | | {0, NULL}, |
220 | | }; |
221 | | #endif |
222 | | |
223 | | |
224 | | /* Define the predeclared (but externally opaque) "ERR_FNS" type */ |
225 | | struct st_ERR_FNS { |
226 | | /* Works on the "error_hash" string table */ |
227 | | LHASH_OF(ERR_STRING_DATA) *(*cb_err_get)(int create); |
228 | | void (*cb_err_del)(void); |
229 | | ERR_STRING_DATA *(*cb_err_get_item)(const ERR_STRING_DATA *); |
230 | | ERR_STRING_DATA *(*cb_err_set_item)(ERR_STRING_DATA *); |
231 | | ERR_STRING_DATA *(*cb_err_del_item)(ERR_STRING_DATA *); |
232 | | /* Works on the "thread_hash" error-state table */ |
233 | | LHASH_OF(ERR_STATE) *(*cb_thread_get)(int create); |
234 | | void (*cb_thread_release)(LHASH_OF(ERR_STATE) **hash); |
235 | | ERR_STATE *(*cb_thread_get_item)(const ERR_STATE *); |
236 | | ERR_STATE *(*cb_thread_set_item)(ERR_STATE *); |
237 | | void (*cb_thread_del_item)(const ERR_STATE *); |
238 | | /* Returns the next available error "library" numbers */ |
239 | | int (*cb_get_next_lib)(void); |
240 | | }; |
241 | | |
242 | | /* Predeclarations of the "err_defaults" functions */ |
243 | | static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create); |
244 | | static void int_err_del(void); |
245 | | static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); |
246 | | static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *); |
247 | | static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *); |
248 | | static LHASH_OF(ERR_STATE) *int_thread_get(int create); |
249 | | static void int_thread_release(LHASH_OF(ERR_STATE) **hash); |
250 | | static ERR_STATE *int_thread_get_item(const ERR_STATE *); |
251 | | static ERR_STATE *int_thread_set_item(ERR_STATE *); |
252 | | static void int_thread_del_item(const ERR_STATE *); |
253 | | static int int_err_get_next_lib(void); |
254 | | |
255 | | /* The static ERR_FNS table using these defaults functions */ |
256 | | static const ERR_FNS err_defaults = { |
257 | | int_err_get, |
258 | | int_err_del, |
259 | | int_err_get_item, |
260 | | int_err_set_item, |
261 | | int_err_del_item, |
262 | | int_thread_get, |
263 | | int_thread_release, |
264 | | int_thread_get_item, |
265 | | int_thread_set_item, |
266 | | int_thread_del_item, |
267 | | int_err_get_next_lib |
268 | | }; |
269 | | |
270 | | /* The replacable table of ERR_FNS functions we use at run-time */ |
271 | | static const ERR_FNS *err_fns = NULL; |
272 | | |
273 | | /* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */ |
274 | 26.9M | #define ERRFN(a) err_fns->cb_##a |
275 | | |
276 | | /* The internal state used by "err_defaults" - as such, the setting, reading, |
277 | | * creating, and deleting of this data should only be permitted via the |
278 | | * "err_defaults" functions. This way, a linked module can completely defer all |
279 | | * ERR state operation (together with requisite locking) to the implementations |
280 | | * and state in the loading application. */ |
281 | | static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; |
282 | | static LHASH_OF(ERR_STATE) *int_thread_hash = NULL; |
283 | | static int int_thread_hash_references = 0; |
284 | | static int int_err_library_number = ERR_LIB_USER; |
285 | | |
286 | | static pthread_t err_init_thread; |
287 | | |
288 | | /* Internal function that checks whether "err_fns" is set and if not, sets it to |
289 | | * the defaults. */ |
290 | | static void |
291 | | err_fns_check(void) |
292 | 17.9M | { |
293 | 17.9M | if (err_fns) |
294 | 17.9M | return; |
295 | | |
296 | 22 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
297 | 22 | if (!err_fns) |
298 | 22 | err_fns = &err_defaults; |
299 | 22 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
300 | 22 | } |
301 | | |
302 | | /* API functions to get or set the underlying ERR functions. */ |
303 | | |
304 | | const ERR_FNS * |
305 | | ERR_get_implementation(void) |
306 | 0 | { |
307 | 0 | err_fns_check(); |
308 | 0 | return err_fns; |
309 | 0 | } |
310 | | |
311 | | int |
312 | | ERR_set_implementation(const ERR_FNS *fns) |
313 | 0 | { |
314 | 0 | int ret = 0; |
315 | |
|
316 | 0 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
317 | | /* It's too late if 'err_fns' is non-NULL. BTW: not much point setting |
318 | | * an error is there?! */ |
319 | 0 | if (!err_fns) { |
320 | 0 | err_fns = fns; |
321 | 0 | ret = 1; |
322 | 0 | } |
323 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
324 | 0 | return ret; |
325 | 0 | } |
326 | | |
327 | | /* These are the callbacks provided to "lh_new()" when creating the LHASH tables |
328 | | * internal to the "err_defaults" implementation. */ |
329 | | |
330 | | static unsigned long get_error_values(int inc, int top, const char **file, |
331 | | int *line, const char **data, int *flags); |
332 | | |
333 | | /* The internal functions used in the "err_defaults" implementation */ |
334 | | |
335 | | static unsigned long |
336 | | err_string_data_hash(const ERR_STRING_DATA *a) |
337 | 84.9k | { |
338 | 84.9k | unsigned long ret, l; |
339 | | |
340 | 84.9k | l = a->error; |
341 | 84.9k | ret = l^ERR_GET_LIB(l)^ERR_GET_FUNC(l); |
342 | 84.9k | return (ret^ret % 19*13); |
343 | 84.9k | } |
344 | | static IMPLEMENT_LHASH_HASH_FN(err_string_data, ERR_STRING_DATA) |
345 | | |
346 | | static int |
347 | | err_string_data_cmp(const ERR_STRING_DATA *a, const ERR_STRING_DATA *b) |
348 | 56.0k | { |
349 | 56.0k | return (int)(a->error - b->error); |
350 | 56.0k | } |
351 | | static IMPLEMENT_LHASH_COMP_FN(err_string_data, ERR_STRING_DATA) |
352 | | |
353 | | static |
354 | | LHASH_OF(ERR_STRING_DATA) *int_err_get(int create) |
355 | 84.9k | { |
356 | 84.9k | LHASH_OF(ERR_STRING_DATA) *ret = NULL; |
357 | | |
358 | 84.9k | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
359 | 84.9k | if (!int_error_hash && create) { |
360 | 22 | CRYPTO_push_info("int_err_get (err.c)"); |
361 | 22 | int_error_hash = lh_ERR_STRING_DATA_new(); |
362 | 22 | CRYPTO_pop_info(); |
363 | 22 | } |
364 | 84.9k | if (int_error_hash) |
365 | 84.9k | ret = int_error_hash; |
366 | 84.9k | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
367 | | |
368 | 84.9k | return ret; |
369 | 84.9k | } |
370 | | |
371 | | static void |
372 | | int_err_del(void) |
373 | 0 | { |
374 | 0 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
375 | 0 | if (int_error_hash) { |
376 | 0 | lh_ERR_STRING_DATA_free(int_error_hash); |
377 | 0 | int_error_hash = NULL; |
378 | 0 | } |
379 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
380 | 0 | } |
381 | | |
382 | | static ERR_STRING_DATA * |
383 | | int_err_get_item(const ERR_STRING_DATA *d) |
384 | 57.5k | { |
385 | 57.5k | ERR_STRING_DATA *p; |
386 | 57.5k | LHASH_OF(ERR_STRING_DATA) *hash; |
387 | | |
388 | 57.5k | err_fns_check(); |
389 | 57.5k | hash = ERRFN(err_get)(0); |
390 | 57.5k | if (!hash) |
391 | 0 | return NULL; |
392 | | |
393 | 57.5k | CRYPTO_r_lock(CRYPTO_LOCK_ERR); |
394 | 57.5k | p = lh_ERR_STRING_DATA_retrieve(hash, d); |
395 | 57.5k | CRYPTO_r_unlock(CRYPTO_LOCK_ERR); |
396 | | |
397 | 57.5k | return p; |
398 | 57.5k | } |
399 | | |
400 | | static ERR_STRING_DATA * |
401 | | int_err_set_item(ERR_STRING_DATA *d) |
402 | 27.3k | { |
403 | 27.3k | ERR_STRING_DATA *p; |
404 | 27.3k | LHASH_OF(ERR_STRING_DATA) *hash; |
405 | | |
406 | 27.3k | err_fns_check(); |
407 | 27.3k | hash = ERRFN(err_get)(1); |
408 | 27.3k | if (!hash) |
409 | 0 | return NULL; |
410 | | |
411 | 27.3k | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
412 | 27.3k | p = lh_ERR_STRING_DATA_insert(hash, d); |
413 | 27.3k | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
414 | | |
415 | 27.3k | return p; |
416 | 27.3k | } |
417 | | |
418 | | static ERR_STRING_DATA * |
419 | | int_err_del_item(ERR_STRING_DATA *d) |
420 | 0 | { |
421 | 0 | ERR_STRING_DATA *p; |
422 | 0 | LHASH_OF(ERR_STRING_DATA) *hash; |
423 | |
|
424 | 0 | err_fns_check(); |
425 | 0 | hash = ERRFN(err_get)(0); |
426 | 0 | if (!hash) |
427 | 0 | return NULL; |
428 | | |
429 | 0 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
430 | 0 | p = lh_ERR_STRING_DATA_delete(hash, d); |
431 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
432 | |
|
433 | 0 | return p; |
434 | 0 | } |
435 | | |
436 | | static unsigned long |
437 | | err_state_hash(const ERR_STATE *a) |
438 | 8.91M | { |
439 | 8.91M | return CRYPTO_THREADID_hash(&a->tid) * 13; |
440 | 8.91M | } |
441 | | static IMPLEMENT_LHASH_HASH_FN(err_state, ERR_STATE) |
442 | | |
443 | | static int |
444 | | err_state_cmp(const ERR_STATE *a, const ERR_STATE *b) |
445 | 8.91M | { |
446 | 8.91M | return CRYPTO_THREADID_cmp(&a->tid, &b->tid); |
447 | 8.91M | } |
448 | | static IMPLEMENT_LHASH_COMP_FN(err_state, ERR_STATE) |
449 | | |
450 | | static |
451 | | LHASH_OF(ERR_STATE) *int_thread_get(int create) |
452 | 8.91M | { |
453 | 8.91M | LHASH_OF(ERR_STATE) *ret = NULL; |
454 | | |
455 | 8.91M | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
456 | 8.91M | if (!int_thread_hash && create) { |
457 | 21 | CRYPTO_push_info("int_thread_get (err.c)"); |
458 | 21 | int_thread_hash = lh_ERR_STATE_new(); |
459 | 21 | CRYPTO_pop_info(); |
460 | 21 | } |
461 | 8.91M | if (int_thread_hash) { |
462 | 8.91M | int_thread_hash_references++; |
463 | 8.91M | ret = int_thread_hash; |
464 | 8.91M | } |
465 | 8.91M | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
466 | 8.91M | return ret; |
467 | 8.91M | } |
468 | | |
469 | | static void |
470 | | int_thread_release(LHASH_OF(ERR_STATE) **hash) |
471 | 8.91M | { |
472 | 8.91M | int i; |
473 | | |
474 | 8.91M | if (hash == NULL || *hash == NULL) |
475 | 0 | return; |
476 | | |
477 | 8.91M | i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR); |
478 | 8.91M | if (i > 0) |
479 | 0 | return; |
480 | | |
481 | 8.91M | *hash = NULL; |
482 | 8.91M | } |
483 | | |
484 | | static ERR_STATE * |
485 | | int_thread_get_item(const ERR_STATE *d) |
486 | 8.91M | { |
487 | 8.91M | ERR_STATE *p; |
488 | 8.91M | LHASH_OF(ERR_STATE) *hash; |
489 | | |
490 | 8.91M | err_fns_check(); |
491 | 8.91M | hash = ERRFN(thread_get)(0); |
492 | 8.91M | if (!hash) |
493 | 21 | return NULL; |
494 | | |
495 | 8.91M | CRYPTO_r_lock(CRYPTO_LOCK_ERR); |
496 | 8.91M | p = lh_ERR_STATE_retrieve(hash, d); |
497 | 8.91M | CRYPTO_r_unlock(CRYPTO_LOCK_ERR); |
498 | | |
499 | 8.91M | ERRFN(thread_release)(&hash); |
500 | 8.91M | return p; |
501 | 8.91M | } |
502 | | |
503 | | static ERR_STATE * |
504 | | int_thread_set_item(ERR_STATE *d) |
505 | 21 | { |
506 | 21 | ERR_STATE *p; |
507 | 21 | LHASH_OF(ERR_STATE) *hash; |
508 | | |
509 | 21 | err_fns_check(); |
510 | 21 | hash = ERRFN(thread_get)(1); |
511 | 21 | if (!hash) |
512 | 0 | return NULL; |
513 | | |
514 | 21 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
515 | 21 | p = lh_ERR_STATE_insert(hash, d); |
516 | 21 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
517 | | |
518 | 21 | ERRFN(thread_release)(&hash); |
519 | 21 | return p; |
520 | 21 | } |
521 | | |
522 | | static void |
523 | | int_thread_del_item(const ERR_STATE *d) |
524 | 0 | { |
525 | 0 | ERR_STATE *p; |
526 | 0 | LHASH_OF(ERR_STATE) *hash; |
527 | |
|
528 | 0 | err_fns_check(); |
529 | 0 | hash = ERRFN(thread_get)(0); |
530 | 0 | if (!hash) |
531 | 0 | return; |
532 | | |
533 | 0 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
534 | 0 | p = lh_ERR_STATE_delete(hash, d); |
535 | | /* make sure we don't leak memory */ |
536 | 0 | if (int_thread_hash_references == 1 && |
537 | 0 | int_thread_hash && lh_ERR_STATE_num_items(int_thread_hash) == 0) { |
538 | 0 | lh_ERR_STATE_free(int_thread_hash); |
539 | 0 | int_thread_hash = NULL; |
540 | 0 | } |
541 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
542 | |
|
543 | 0 | ERRFN(thread_release)(&hash); |
544 | 0 | if (p) |
545 | 0 | ERR_STATE_free(p); |
546 | 0 | } |
547 | | |
548 | | static int |
549 | | int_err_get_next_lib(void) |
550 | 0 | { |
551 | 0 | int ret; |
552 | |
|
553 | 0 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
554 | 0 | ret = int_err_library_number++; |
555 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
556 | |
|
557 | 0 | return ret; |
558 | 0 | } |
559 | | |
560 | | |
561 | | #ifndef OPENSSL_NO_ERR |
562 | 2.81k | #define NUM_SYS_STR_REASONS 127 |
563 | | #define LEN_SYS_STR_REASON 32 |
564 | | |
565 | | static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1]; |
566 | | /* SYS_str_reasons is filled with copies of strerror() results at |
567 | | * initialization. |
568 | | * 'errno' values up to 127 should cover all usual errors, |
569 | | * others will be displayed numerically by ERR_error_string. |
570 | | * It is crucial that we have something for each reason code |
571 | | * that occurs in ERR_str_reasons, or bogus reason strings |
572 | | * will be returned for SYSerror(which always gets an errno |
573 | | * value and never one of those 'standard' reason codes. */ |
574 | | |
575 | | static void |
576 | | build_SYS_str_reasons(void) |
577 | 22 | { |
578 | | /* malloc cannot be used here, use static storage instead */ |
579 | 22 | static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON]; |
580 | 22 | int i; |
581 | 22 | static int init = 1; |
582 | | |
583 | 22 | CRYPTO_r_lock(CRYPTO_LOCK_ERR); |
584 | 22 | if (!init) { |
585 | 0 | CRYPTO_r_unlock(CRYPTO_LOCK_ERR); |
586 | 0 | return; |
587 | 0 | } |
588 | | |
589 | 22 | CRYPTO_r_unlock(CRYPTO_LOCK_ERR); |
590 | 22 | CRYPTO_w_lock(CRYPTO_LOCK_ERR); |
591 | 22 | if (!init) { |
592 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
593 | 0 | return; |
594 | 0 | } |
595 | | |
596 | 2.81k | for (i = 1; i <= NUM_SYS_STR_REASONS; i++) { |
597 | 2.79k | ERR_STRING_DATA *str = &SYS_str_reasons[i - 1]; |
598 | | |
599 | 2.79k | str->error = (unsigned long)i; |
600 | 2.79k | if (str->string == NULL) { |
601 | 2.79k | char (*dest)[LEN_SYS_STR_REASON] = |
602 | 2.79k | &(strerror_tab[i - 1]); |
603 | 2.79k | const char *src = strerror(i); |
604 | 2.79k | if (src != NULL) { |
605 | 2.79k | strlcpy(*dest, src, sizeof *dest); |
606 | 2.79k | str->string = *dest; |
607 | 2.79k | } |
608 | 2.79k | } |
609 | 2.79k | if (str->string == NULL) |
610 | 0 | str->string = "unknown"; |
611 | 2.79k | } |
612 | | |
613 | | /* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, |
614 | | * as required by ERR_load_strings. */ |
615 | | |
616 | 22 | init = 0; |
617 | | |
618 | 22 | CRYPTO_w_unlock(CRYPTO_LOCK_ERR); |
619 | 22 | } |
620 | | #endif |
621 | | |
622 | | #define err_clear_data(p,i) \ |
623 | 13.0M | do { \ |
624 | 13.0M | if (((p)->err_data[i] != NULL) && \ |
625 | 13.0M | (p)->err_data_flags[i] & ERR_TXT_MALLOCED) { \ |
626 | 2.26M | free((p)->err_data[i]); \ |
627 | 2.26M | (p)->err_data[i] = NULL; \ |
628 | 2.26M | } \ |
629 | 13.0M | (p)->err_data_flags[i] = 0; \ |
630 | 13.0M | } while(0) |
631 | | |
632 | | #define err_clear(p,i) \ |
633 | 4.59M | do { \ |
634 | 4.59M | (p)->err_flags[i] = 0; \ |
635 | 4.59M | (p)->err_buffer[i] = 0; \ |
636 | 4.59M | err_clear_data(p, i); \ |
637 | 4.59M | (p)->err_file[i] = NULL; \ |
638 | 4.59M | (p)->err_line[i] = -1; \ |
639 | 4.59M | } while(0) |
640 | | |
641 | | static void |
642 | | ERR_STATE_free(ERR_STATE *s) |
643 | 0 | { |
644 | 0 | int i; |
645 | |
|
646 | 0 | if (s == NULL) |
647 | 0 | return; |
648 | | |
649 | 0 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
650 | 0 | err_clear_data(s, i); |
651 | 0 | } |
652 | 0 | free(s); |
653 | 0 | } |
654 | | |
655 | | void |
656 | | ERR_load_ERR_strings_internal(void) |
657 | 22 | { |
658 | 22 | err_init_thread = pthread_self(); |
659 | 22 | err_fns_check(); |
660 | 22 | #ifndef OPENSSL_NO_ERR |
661 | 22 | err_load_strings(0, ERR_str_libraries); |
662 | 22 | err_load_strings(0, ERR_str_reasons); |
663 | 22 | err_load_strings(ERR_LIB_SYS, ERR_str_functs); |
664 | 22 | build_SYS_str_reasons(); |
665 | 22 | err_load_strings(ERR_LIB_SYS, SYS_str_reasons); |
666 | 22 | #endif |
667 | 22 | } |
668 | | |
669 | | |
670 | | void |
671 | | ERR_load_ERR_strings(void) |
672 | 1.24k | { |
673 | 1.24k | static pthread_once_t once = PTHREAD_ONCE_INIT; |
674 | | |
675 | 1.24k | if (pthread_equal(pthread_self(), err_init_thread)) |
676 | 1.24k | return; /* don't recurse */ |
677 | | |
678 | | /* Prayer and clean living lets you ignore errors, OpenSSL style */ |
679 | 0 | (void) OPENSSL_init_crypto(0, NULL); |
680 | |
|
681 | 0 | (void) pthread_once(&once, ERR_load_ERR_strings_internal); |
682 | 0 | } |
683 | | |
684 | | static void |
685 | | err_load_strings(int lib, ERR_STRING_DATA *str) |
686 | 1.33k | { |
687 | 28.7k | while (str->error) { |
688 | 27.3k | if (lib) |
689 | 4.84k | str->error |= ERR_PACK(lib, 0, 0); |
690 | 27.3k | ERRFN(err_set_item)(str); |
691 | 27.3k | str++; |
692 | 27.3k | } |
693 | 1.33k | } |
694 | | |
695 | | void |
696 | | ERR_load_strings(int lib, ERR_STRING_DATA *str) |
697 | 1.24k | { |
698 | 1.24k | ERR_load_ERR_strings(); |
699 | 1.24k | err_load_strings(lib, str); |
700 | 1.24k | } |
701 | | |
702 | | void |
703 | | ERR_unload_strings(int lib, ERR_STRING_DATA *str) |
704 | 0 | { |
705 | | /* Prayer and clean living lets you ignore errors, OpenSSL style */ |
706 | 0 | (void) OPENSSL_init_crypto(0, NULL); |
707 | |
|
708 | 0 | while (str->error) { |
709 | 0 | if (lib) |
710 | 0 | str->error |= ERR_PACK(lib, 0, 0); |
711 | 0 | ERRFN(err_del_item)(str); |
712 | 0 | str++; |
713 | 0 | } |
714 | 0 | } |
715 | | |
716 | | void |
717 | | ERR_free_strings(void) |
718 | 0 | { |
719 | | /* Prayer and clean living lets you ignore errors, OpenSSL style */ |
720 | 0 | (void) OPENSSL_init_crypto(0, NULL); |
721 | |
|
722 | 0 | err_fns_check(); |
723 | 0 | ERRFN(err_del)(); |
724 | 0 | } |
725 | | |
726 | | /********************************************************/ |
727 | | |
728 | | void |
729 | | ERR_put_error(int lib, int func, int reason, const char *file, int line) |
730 | 6.17M | { |
731 | 6.17M | ERR_STATE *es; |
732 | 6.17M | int save_errno = errno; |
733 | | |
734 | 6.17M | es = ERR_get_state(); |
735 | | |
736 | 6.17M | es->top = (es->top + 1) % ERR_NUM_ERRORS; |
737 | 6.17M | if (es->top == es->bottom) |
738 | 5.66M | es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS; |
739 | 6.17M | es->err_flags[es->top] = 0; |
740 | 6.17M | es->err_buffer[es->top] = ERR_PACK(lib, func, reason); |
741 | 6.17M | es->err_file[es->top] = file; |
742 | 6.17M | es->err_line[es->top] = line; |
743 | 6.17M | err_clear_data(es, es->top); |
744 | 6.17M | errno = save_errno; |
745 | 6.17M | } |
746 | | |
747 | | void |
748 | | ERR_clear_error(void) |
749 | 287k | { |
750 | 287k | int i; |
751 | 287k | ERR_STATE *es; |
752 | | |
753 | 287k | es = ERR_get_state(); |
754 | | |
755 | 4.88M | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
756 | 4.59M | err_clear(es, i); |
757 | 4.59M | } |
758 | 287k | es->top = es->bottom = 0; |
759 | 287k | } |
760 | | |
761 | | |
762 | | unsigned long |
763 | | ERR_get_error(void) |
764 | 0 | { |
765 | 0 | return (get_error_values(1, 0, NULL, NULL, NULL, NULL)); |
766 | 0 | } |
767 | | |
768 | | unsigned long |
769 | | ERR_get_error_line(const char **file, int *line) |
770 | 0 | { |
771 | 0 | return (get_error_values(1, 0, file, line, NULL, NULL)); |
772 | 0 | } |
773 | | |
774 | | unsigned long |
775 | | ERR_get_error_line_data(const char **file, int *line, |
776 | | const char **data, int *flags) |
777 | 29.8k | { |
778 | 29.8k | return (get_error_values(1, 0, file, line, data, flags)); |
779 | 29.8k | } |
780 | | |
781 | | |
782 | | unsigned long |
783 | | ERR_peek_error(void) |
784 | 156k | { |
785 | 156k | return (get_error_values(0, 0, NULL, NULL, NULL, NULL)); |
786 | 156k | } |
787 | | |
788 | | unsigned long |
789 | | ERR_peek_error_line(const char **file, int *line) |
790 | 0 | { |
791 | 0 | return (get_error_values(0, 0, file, line, NULL, NULL)); |
792 | 0 | } |
793 | | |
794 | | unsigned long |
795 | | ERR_peek_error_line_data(const char **file, int *line, |
796 | | const char **data, int *flags) |
797 | 0 | { |
798 | 0 | return (get_error_values(0, 0, file, line, data, flags)); |
799 | 0 | } |
800 | | |
801 | | unsigned long |
802 | | ERR_peek_last_error(void) |
803 | 4.07k | { |
804 | 4.07k | return (get_error_values(0, 1, NULL, NULL, NULL, NULL)); |
805 | 4.07k | } |
806 | | |
807 | | unsigned long |
808 | | ERR_peek_last_error_line(const char **file, int *line) |
809 | 0 | { |
810 | 0 | return (get_error_values(0, 1, file, line, NULL, NULL)); |
811 | 0 | } |
812 | | |
813 | | unsigned long |
814 | | ERR_peek_last_error_line_data(const char **file, int *line, |
815 | | const char **data, int *flags) |
816 | 0 | { |
817 | 0 | return (get_error_values(0, 1, file, line, data, flags)); |
818 | 0 | } |
819 | | |
820 | | static unsigned long |
821 | | get_error_values(int inc, int top, const char **file, int *line, |
822 | | const char **data, int *flags) |
823 | 190k | { |
824 | 190k | int i = 0; |
825 | 190k | ERR_STATE *es; |
826 | 190k | unsigned long ret; |
827 | | |
828 | 190k | es = ERR_get_state(); |
829 | | |
830 | 190k | if (inc && top) { |
831 | 0 | if (file) |
832 | 0 | *file = ""; |
833 | 0 | if (line) |
834 | 0 | *line = 0; |
835 | 0 | if (data) |
836 | 0 | *data = ""; |
837 | 0 | if (flags) |
838 | 0 | *flags = 0; |
839 | |
|
840 | 0 | return ERR_R_INTERNAL_ERROR; |
841 | 0 | } |
842 | | |
843 | 190k | if (es->bottom == es->top) |
844 | 12.1k | return 0; |
845 | 178k | if (top) |
846 | 4.07k | i = es->top; /* last error */ |
847 | 174k | else |
848 | 174k | i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */ |
849 | | |
850 | 178k | ret = es->err_buffer[i]; |
851 | 178k | if (inc) { |
852 | 17.8k | es->bottom = i; |
853 | 17.8k | es->err_buffer[i] = 0; |
854 | 17.8k | } |
855 | | |
856 | 178k | if ((file != NULL) && (line != NULL)) { |
857 | 17.8k | if (es->err_file[i] == NULL) { |
858 | 0 | *file = "NA"; |
859 | 0 | if (line != NULL) |
860 | 0 | *line = 0; |
861 | 17.8k | } else { |
862 | 17.8k | *file = es->err_file[i]; |
863 | 17.8k | if (line != NULL) |
864 | 17.8k | *line = es->err_line[i]; |
865 | 17.8k | } |
866 | 17.8k | } |
867 | | |
868 | 178k | if (data == NULL) { |
869 | 160k | if (inc) { |
870 | 0 | err_clear_data(es, i); |
871 | 0 | } |
872 | 160k | } else { |
873 | 17.8k | if (es->err_data[i] == NULL) { |
874 | 17.1k | *data = ""; |
875 | 17.1k | if (flags != NULL) |
876 | 17.1k | *flags = 0; |
877 | 17.1k | } else { |
878 | 681 | *data = es->err_data[i]; |
879 | 681 | if (flags != NULL) |
880 | 681 | *flags = es->err_data_flags[i]; |
881 | 681 | } |
882 | 17.8k | } |
883 | 178k | return ret; |
884 | 190k | } |
885 | | |
886 | | void |
887 | | ERR_error_string_n(unsigned long e, char *buf, size_t len) |
888 | 17.8k | { |
889 | 17.8k | char lsbuf[30], fsbuf[30], rsbuf[30]; |
890 | 17.8k | const char *ls, *fs, *rs; |
891 | 17.8k | int l, f, r, ret; |
892 | | |
893 | 17.8k | l = ERR_GET_LIB(e); |
894 | 17.8k | f = ERR_GET_FUNC(e); |
895 | 17.8k | r = ERR_GET_REASON(e); |
896 | | |
897 | 17.8k | ls = ERR_lib_error_string(e); |
898 | 17.8k | fs = ERR_func_error_string(e); |
899 | 17.8k | rs = ERR_reason_error_string(e); |
900 | | |
901 | 17.8k | if (ls == NULL) { |
902 | 0 | (void) snprintf(lsbuf, sizeof(lsbuf), "lib(%d)", l); |
903 | 0 | ls = lsbuf; |
904 | 0 | } |
905 | 17.8k | if (fs == NULL) { |
906 | 0 | (void) snprintf(fsbuf, sizeof(fsbuf), "func(%d)", f); |
907 | 0 | fs = fsbuf; |
908 | 0 | } |
909 | 17.8k | if (rs == NULL) { |
910 | 1 | (void) snprintf(rsbuf, sizeof(rsbuf), "reason(%d)", r); |
911 | 1 | rs = rsbuf; |
912 | 1 | } |
913 | | |
914 | 17.8k | ret = snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs); |
915 | 17.8k | if (ret == -1) |
916 | 0 | return; /* can't happen, and can't do better if it does */ |
917 | 17.8k | if (ret >= len) { |
918 | | /* output may be truncated; make sure we always have 5 |
919 | | * colon-separated fields, i.e. 4 colons ... */ |
920 | 0 | #define NUM_COLONS 4 |
921 | 0 | if (len > NUM_COLONS) /* ... if possible */ |
922 | 0 | { |
923 | 0 | int i; |
924 | 0 | char *s = buf; |
925 | |
|
926 | 0 | for (i = 0; i < NUM_COLONS; i++) { |
927 | 0 | char *colon = strchr(s, ':'); |
928 | 0 | if (colon == NULL || |
929 | 0 | colon > &buf[len - 1] - NUM_COLONS + i) { |
930 | | /* set colon no. i at last possible position |
931 | | * (buf[len-1] is the terminating 0)*/ |
932 | 0 | colon = &buf[len - 1] - NUM_COLONS + i; |
933 | 0 | *colon = ':'; |
934 | 0 | } |
935 | 0 | s = colon + 1; |
936 | 0 | } |
937 | 0 | } |
938 | 0 | } |
939 | 17.8k | } |
940 | | |
941 | | /* BAD for multi-threading: uses a local buffer if ret == NULL */ |
942 | | /* ERR_error_string_n should be used instead for ret != NULL |
943 | | * as ERR_error_string cannot know how large the buffer is */ |
944 | | char * |
945 | | ERR_error_string(unsigned long e, char *ret) |
946 | 0 | { |
947 | 0 | static char buf[256]; |
948 | |
|
949 | 0 | if (ret == NULL) |
950 | 0 | ret = buf; |
951 | 0 | ERR_error_string_n(e, ret, 256); |
952 | |
|
953 | 0 | return ret; |
954 | 0 | } |
955 | | |
956 | | LHASH_OF(ERR_STRING_DATA) *ERR_get_string_table(void) |
957 | 0 | { |
958 | 0 | err_fns_check(); |
959 | 0 | return ERRFN(err_get)(0); |
960 | 0 | } |
961 | | |
962 | | LHASH_OF(ERR_STATE) *ERR_get_err_state_table(void) |
963 | 0 | { |
964 | 0 | err_fns_check(); |
965 | 0 | return ERRFN(thread_get)(0); |
966 | 0 | } |
967 | | |
968 | | void |
969 | | ERR_release_err_state_table(LHASH_OF(ERR_STATE) **hash) |
970 | 0 | { |
971 | 0 | err_fns_check(); |
972 | 0 | ERRFN(thread_release)(hash); |
973 | 0 | } |
974 | | |
975 | | const char * |
976 | | ERR_lib_error_string(unsigned long e) |
977 | 17.8k | { |
978 | 17.8k | ERR_STRING_DATA d, *p; |
979 | 17.8k | unsigned long l; |
980 | | |
981 | 17.8k | if (!OPENSSL_init_crypto(0, NULL)) |
982 | 0 | return NULL; |
983 | | |
984 | 17.8k | err_fns_check(); |
985 | 17.8k | l = ERR_GET_LIB(e); |
986 | 17.8k | d.error = ERR_PACK(l, 0, 0); |
987 | 17.8k | p = ERRFN(err_get_item)(&d); |
988 | 17.8k | return ((p == NULL) ? NULL : p->string); |
989 | 17.8k | } |
990 | | |
991 | | const char * |
992 | | ERR_func_error_string(unsigned long e) |
993 | 18.4k | { |
994 | 18.4k | ERR_STRING_DATA d, *p; |
995 | 18.4k | unsigned long l, f; |
996 | | |
997 | 18.4k | err_fns_check(); |
998 | 18.4k | l = ERR_GET_LIB(e); |
999 | 18.4k | f = ERR_GET_FUNC(e); |
1000 | 18.4k | d.error = ERR_PACK(l, f, 0); |
1001 | 18.4k | p = ERRFN(err_get_item)(&d); |
1002 | 18.4k | return ((p == NULL) ? NULL : p->string); |
1003 | 18.4k | } |
1004 | | |
1005 | | const char * |
1006 | | ERR_reason_error_string(unsigned long e) |
1007 | 17.8k | { |
1008 | 17.8k | ERR_STRING_DATA d, *p = NULL; |
1009 | 17.8k | unsigned long l, r; |
1010 | | |
1011 | 17.8k | err_fns_check(); |
1012 | 17.8k | l = ERR_GET_LIB(e); |
1013 | 17.8k | r = ERR_GET_REASON(e); |
1014 | 17.8k | d.error = ERR_PACK(l, 0, r); |
1015 | 17.8k | p = ERRFN(err_get_item)(&d); |
1016 | 17.8k | if (!p) { |
1017 | 3.43k | d.error = ERR_PACK(0, 0, r); |
1018 | 3.43k | p = ERRFN(err_get_item)(&d); |
1019 | 3.43k | } |
1020 | 17.8k | return ((p == NULL) ? NULL : p->string); |
1021 | 17.8k | } |
1022 | | |
1023 | | void |
1024 | | ERR_remove_thread_state(const CRYPTO_THREADID *id) |
1025 | 0 | { |
1026 | 0 | ERR_STATE tmp; |
1027 | |
|
1028 | 0 | if (id) |
1029 | 0 | CRYPTO_THREADID_cpy(&tmp.tid, id); |
1030 | 0 | else |
1031 | 0 | CRYPTO_THREADID_current(&tmp.tid); |
1032 | 0 | err_fns_check(); |
1033 | | /* thread_del_item automatically destroys the LHASH if the number of |
1034 | | * items reaches zero. */ |
1035 | 0 | ERRFN(thread_del_item)(&tmp); |
1036 | 0 | } |
1037 | | |
1038 | | #ifndef OPENSSL_NO_DEPRECATED |
1039 | | void |
1040 | | ERR_remove_state(unsigned long pid) |
1041 | 0 | { |
1042 | 0 | ERR_remove_thread_state(NULL); |
1043 | 0 | } |
1044 | | #endif |
1045 | | |
1046 | | ERR_STATE * |
1047 | | ERR_get_state(void) |
1048 | 8.91M | { |
1049 | 8.91M | static ERR_STATE fallback; |
1050 | 8.91M | ERR_STATE *ret, tmp, *tmpp = NULL; |
1051 | 8.91M | int i; |
1052 | 8.91M | CRYPTO_THREADID tid; |
1053 | | |
1054 | 8.91M | err_fns_check(); |
1055 | 8.91M | CRYPTO_THREADID_current(&tid); |
1056 | 8.91M | CRYPTO_THREADID_cpy(&tmp.tid, &tid); |
1057 | 8.91M | ret = ERRFN(thread_get_item)(&tmp); |
1058 | | |
1059 | | /* ret == the error state, if NULL, make a new one */ |
1060 | 8.91M | if (ret == NULL) { |
1061 | 21 | ret = malloc(sizeof(ERR_STATE)); |
1062 | 21 | if (ret == NULL) |
1063 | 0 | return (&fallback); |
1064 | 21 | CRYPTO_THREADID_cpy(&ret->tid, &tid); |
1065 | 21 | ret->top = 0; |
1066 | 21 | ret->bottom = 0; |
1067 | 357 | for (i = 0; i < ERR_NUM_ERRORS; i++) { |
1068 | 336 | ret->err_data[i] = NULL; |
1069 | 336 | ret->err_data_flags[i] = 0; |
1070 | 336 | } |
1071 | 21 | tmpp = ERRFN(thread_set_item)(ret); |
1072 | | /* To check if insertion failed, do a get. */ |
1073 | 21 | if (ERRFN(thread_get_item)(ret) != ret) { |
1074 | 0 | ERR_STATE_free(ret); /* could not insert it */ |
1075 | 0 | return (&fallback); |
1076 | 0 | } |
1077 | | /* If a race occured in this function and we came second, tmpp |
1078 | | * is the first one that we just replaced. */ |
1079 | 21 | if (tmpp) |
1080 | 0 | ERR_STATE_free(tmpp); |
1081 | 21 | } |
1082 | 8.91M | return ret; |
1083 | 8.91M | } |
1084 | | |
1085 | | int |
1086 | | ERR_get_next_error_library(void) |
1087 | 0 | { |
1088 | 0 | err_fns_check(); |
1089 | 0 | return ERRFN(get_next_lib)(); |
1090 | 0 | } |
1091 | | |
1092 | | void |
1093 | | ERR_set_error_data(char *data, int flags) |
1094 | 2.26M | { |
1095 | 2.26M | ERR_STATE *es; |
1096 | 2.26M | int i; |
1097 | | |
1098 | 2.26M | es = ERR_get_state(); |
1099 | | |
1100 | 2.26M | i = es->top; |
1101 | 2.26M | if (i == 0) |
1102 | 121k | i = ERR_NUM_ERRORS - 1; |
1103 | | |
1104 | 2.26M | err_clear_data(es, i); |
1105 | 2.26M | es->err_data[i] = data; |
1106 | 2.26M | es->err_data_flags[i] = flags; |
1107 | 2.26M | } |
1108 | | |
1109 | | void |
1110 | | ERR_asprintf_error_data(char * format, ...) |
1111 | 2.26M | { |
1112 | 2.26M | char *errbuf = NULL; |
1113 | 2.26M | va_list ap; |
1114 | 2.26M | int r; |
1115 | | |
1116 | 2.26M | va_start(ap, format); |
1117 | 2.26M | r = vasprintf(&errbuf, format, ap); |
1118 | 2.26M | va_end(ap); |
1119 | 2.26M | if (r == -1) |
1120 | 0 | ERR_set_error_data("malloc failed", ERR_TXT_STRING); |
1121 | 2.26M | else |
1122 | 2.26M | ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); |
1123 | 2.26M | } |
1124 | | |
1125 | | void |
1126 | | ERR_add_error_vdata(int num, va_list args) |
1127 | 0 | { |
1128 | 0 | char format[129]; |
1129 | 0 | char *errbuf; |
1130 | 0 | int i; |
1131 | |
|
1132 | 0 | format[0] = '\0'; |
1133 | 0 | for (i = 0; i < num; i++) { |
1134 | 0 | if (strlcat(format, "%s", sizeof(format)) >= sizeof(format)) { |
1135 | 0 | ERR_set_error_data("too many errors", ERR_TXT_STRING); |
1136 | 0 | return; |
1137 | 0 | } |
1138 | 0 | } |
1139 | 0 | if (vasprintf(&errbuf, format, args) == -1) |
1140 | 0 | ERR_set_error_data("malloc failed", ERR_TXT_STRING); |
1141 | 0 | else |
1142 | 0 | ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); |
1143 | 0 | } |
1144 | | |
1145 | | void |
1146 | | ERR_add_error_data(int num, ...) |
1147 | 0 | { |
1148 | 0 | va_list args; |
1149 | 0 | va_start(args, num); |
1150 | 0 | ERR_add_error_vdata(num, args); |
1151 | 0 | va_end(args); |
1152 | 0 | } |
1153 | | |
1154 | | int |
1155 | | ERR_set_mark(void) |
1156 | 0 | { |
1157 | 0 | ERR_STATE *es; |
1158 | |
|
1159 | 0 | es = ERR_get_state(); |
1160 | |
|
1161 | 0 | if (es->bottom == es->top) |
1162 | 0 | return 0; |
1163 | 0 | es->err_flags[es->top] |= ERR_FLAG_MARK; |
1164 | 0 | return 1; |
1165 | 0 | } |
1166 | | |
1167 | | int |
1168 | | ERR_pop_to_mark(void) |
1169 | 0 | { |
1170 | 0 | ERR_STATE *es; |
1171 | |
|
1172 | 0 | es = ERR_get_state(); |
1173 | |
|
1174 | 0 | while (es->bottom != es->top && |
1175 | 0 | (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) { |
1176 | 0 | err_clear(es, es->top); |
1177 | 0 | es->top -= 1; |
1178 | 0 | if (es->top == -1) |
1179 | 0 | es->top = ERR_NUM_ERRORS - 1; |
1180 | 0 | } |
1181 | |
|
1182 | 0 | if (es->bottom == es->top) |
1183 | 0 | return 0; |
1184 | 0 | es->err_flags[es->top]&=~ERR_FLAG_MARK; |
1185 | 0 | return 1; |
1186 | 0 | } |
1187 | | |
1188 | | void |
1189 | | err_clear_last_constant_time(int clear) |
1190 | 0 | { |
1191 | 0 | ERR_STATE *es; |
1192 | 0 | int top; |
1193 | |
|
1194 | 0 | es = ERR_get_state(); |
1195 | 0 | if (es == NULL) |
1196 | 0 | return; |
1197 | | |
1198 | 0 | top = es->top; |
1199 | |
|
1200 | 0 | es->err_flags[top] &= ~(0 - clear); |
1201 | 0 | es->err_buffer[top] &= ~(0UL - clear); |
1202 | 0 | es->err_file[top] = (const char *)((uintptr_t)es->err_file[top] & |
1203 | 0 | ~((uintptr_t)0 - clear)); |
1204 | 0 | es->err_line[top] |= 0 - clear; |
1205 | |
|
1206 | 0 | es->top = (top + ERR_NUM_ERRORS - clear) % ERR_NUM_ERRORS; |
1207 | 0 | } |