err.c revision 1.26
1/* crypto/err/err.c */
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 <stdio.h>
113#include <stdarg.h>
114#include <string.h>
115#include "cryptlib.h"
116#include <openssl/lhash.h>
117#include <openssl/crypto.h>
118#include <openssl/buffer.h>
119#include <openssl/bio.h>
120#include <openssl/err.h>
121
122DECLARE_LHASH_OF(ERR_STRING_DATA);
123DECLARE_LHASH_OF(ERR_STATE);
124
125static void err_load_strings(int lib, ERR_STRING_DATA *str);
126
127static void ERR_STATE_free(ERR_STATE *s);
128#ifndef OPENSSL_NO_ERR
129static ERR_STRING_DATA ERR_str_libraries[]=
130	{
131{ERR_PACK(ERR_LIB_NONE,0,0)		,"unknown library"},
132{ERR_PACK(ERR_LIB_SYS,0,0)		,"system library"},
133{ERR_PACK(ERR_LIB_BN,0,0)		,"bignum routines"},
134{ERR_PACK(ERR_LIB_RSA,0,0)		,"rsa routines"},
135{ERR_PACK(ERR_LIB_DH,0,0)		,"Diffie-Hellman routines"},
136{ERR_PACK(ERR_LIB_EVP,0,0)		,"digital envelope routines"},
137{ERR_PACK(ERR_LIB_BUF,0,0)		,"memory buffer routines"},
138{ERR_PACK(ERR_LIB_OBJ,0,0)		,"object identifier routines"},
139{ERR_PACK(ERR_LIB_PEM,0,0)		,"PEM routines"},
140{ERR_PACK(ERR_LIB_DSA,0,0)		,"dsa routines"},
141{ERR_PACK(ERR_LIB_X509,0,0)		,"x509 certificate routines"},
142{ERR_PACK(ERR_LIB_ASN1,0,0)		,"asn1 encoding routines"},
143{ERR_PACK(ERR_LIB_CONF,0,0)		,"configuration file routines"},
144{ERR_PACK(ERR_LIB_CRYPTO,0,0)		,"common libcrypto routines"},
145{ERR_PACK(ERR_LIB_EC,0,0)		,"elliptic curve routines"},
146{ERR_PACK(ERR_LIB_SSL,0,0)		,"SSL routines"},
147{ERR_PACK(ERR_LIB_BIO,0,0)		,"BIO routines"},
148{ERR_PACK(ERR_LIB_PKCS7,0,0)		,"PKCS7 routines"},
149{ERR_PACK(ERR_LIB_X509V3,0,0)		,"X509 V3 routines"},
150{ERR_PACK(ERR_LIB_PKCS12,0,0)		,"PKCS12 routines"},
151{ERR_PACK(ERR_LIB_RAND,0,0)		,"random number generator"},
152{ERR_PACK(ERR_LIB_DSO,0,0)		,"DSO support routines"},
153{ERR_PACK(ERR_LIB_TS,0,0)		,"time stamp routines"},
154{ERR_PACK(ERR_LIB_ENGINE,0,0)		,"engine routines"},
155{ERR_PACK(ERR_LIB_OCSP,0,0)		,"OCSP routines"},
156{ERR_PACK(ERR_LIB_FIPS,0,0)		,"FIPS routines"},
157{ERR_PACK(ERR_LIB_CMS,0,0)		,"CMS routines"},
158{ERR_PACK(ERR_LIB_HMAC,0,0)		,"HMAC routines"},
159{0,NULL},
160	};
161
162static ERR_STRING_DATA ERR_str_functs[]=
163	{
164	{ERR_PACK(0,SYS_F_FOPEN,0),     	"fopen"},
165	{ERR_PACK(0,SYS_F_CONNECT,0),		"connect"},
166	{ERR_PACK(0,SYS_F_GETSERVBYNAME,0),	"getservbyname"},
167	{ERR_PACK(0,SYS_F_SOCKET,0),		"socket"},
168	{ERR_PACK(0,SYS_F_IOCTLSOCKET,0),	"ioctl"},
169	{ERR_PACK(0,SYS_F_BIND,0),		"bind"},
170	{ERR_PACK(0,SYS_F_LISTEN,0),		"listen"},
171	{ERR_PACK(0,SYS_F_ACCEPT,0),		"accept"},
172#ifdef OPENSSL_SYS_WINDOWS
173	{ERR_PACK(0,SYS_F_WSASTARTUP,0),	"WSAstartup"},
174#endif
175	{ERR_PACK(0,SYS_F_OPENDIR,0),		"opendir"},
176	{ERR_PACK(0,SYS_F_FREAD,0),		"fread"},
177	{0,NULL},
178	};
179
180static ERR_STRING_DATA ERR_str_reasons[]=
181	{
182{ERR_R_SYS_LIB				,"system lib"},
183{ERR_R_BN_LIB				,"BN lib"},
184{ERR_R_RSA_LIB				,"RSA lib"},
185{ERR_R_DH_LIB				,"DH lib"},
186{ERR_R_EVP_LIB				,"EVP lib"},
187{ERR_R_BUF_LIB				,"BUF lib"},
188{ERR_R_OBJ_LIB				,"OBJ lib"},
189{ERR_R_PEM_LIB				,"PEM lib"},
190{ERR_R_DSA_LIB				,"DSA lib"},
191{ERR_R_X509_LIB				,"X509 lib"},
192{ERR_R_ASN1_LIB				,"ASN1 lib"},
193{ERR_R_CONF_LIB				,"CONF lib"},
194{ERR_R_CRYPTO_LIB			,"CRYPTO lib"},
195{ERR_R_EC_LIB				,"EC lib"},
196{ERR_R_SSL_LIB				,"SSL lib"},
197{ERR_R_BIO_LIB				,"BIO lib"},
198{ERR_R_PKCS7_LIB			,"PKCS7 lib"},
199{ERR_R_X509V3_LIB			,"X509V3 lib"},
200{ERR_R_PKCS12_LIB			,"PKCS12 lib"},
201{ERR_R_RAND_LIB				,"RAND lib"},
202{ERR_R_DSO_LIB				,"DSO lib"},
203{ERR_R_ENGINE_LIB			,"ENGINE lib"},
204{ERR_R_OCSP_LIB				,"OCSP lib"},
205{ERR_R_TS_LIB				,"TS lib"},
206
207{ERR_R_NESTED_ASN1_ERROR		,"nested asn1 error"},
208{ERR_R_BAD_ASN1_OBJECT_HEADER		,"bad asn1 object header"},
209{ERR_R_BAD_GET_ASN1_OBJECT_CALL		,"bad get asn1 object call"},
210{ERR_R_EXPECTING_AN_ASN1_SEQUENCE	,"expecting an asn1 sequence"},
211{ERR_R_ASN1_LENGTH_MISMATCH		,"asn1 length mismatch"},
212{ERR_R_MISSING_ASN1_EOS			,"missing asn1 eos"},
213
214{ERR_R_FATAL                            ,"fatal"},
215{ERR_R_MALLOC_FAILURE			,"malloc failure"},
216{ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED	,"called a function you should not call"},
217{ERR_R_PASSED_NULL_PARAMETER		,"passed a null parameter"},
218{ERR_R_INTERNAL_ERROR			,"internal error"},
219{ERR_R_DISABLED				,"called a function that was disabled at compile-time"},
220
221{0,NULL},
222	};
223#endif
224
225
226/* Define the predeclared (but externally opaque) "ERR_FNS" type */
227struct st_ERR_FNS
228	{
229	/* Works on the "error_hash" string table */
230	LHASH_OF(ERR_STRING_DATA) *(*cb_err_get)(int create);
231	void (*cb_err_del)(void);
232	ERR_STRING_DATA *(*cb_err_get_item)(const ERR_STRING_DATA *);
233	ERR_STRING_DATA *(*cb_err_set_item)(ERR_STRING_DATA *);
234	ERR_STRING_DATA *(*cb_err_del_item)(ERR_STRING_DATA *);
235	/* Works on the "thread_hash" error-state table */
236	LHASH_OF(ERR_STATE) *(*cb_thread_get)(int create);
237	void (*cb_thread_release)(LHASH_OF(ERR_STATE) **hash);
238	ERR_STATE *(*cb_thread_get_item)(const ERR_STATE *);
239	ERR_STATE *(*cb_thread_set_item)(ERR_STATE *);
240	void (*cb_thread_del_item)(const ERR_STATE *);
241	/* Returns the next available error "library" numbers */
242	int (*cb_get_next_lib)(void);
243	};
244
245/* Predeclarations of the "err_defaults" functions */
246static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create);
247static void int_err_del(void);
248static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
249static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *);
250static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *);
251static LHASH_OF(ERR_STATE) *int_thread_get(int create);
252static void int_thread_release(LHASH_OF(ERR_STATE) **hash);
253static ERR_STATE *int_thread_get_item(const ERR_STATE *);
254static ERR_STATE *int_thread_set_item(ERR_STATE *);
255static void int_thread_del_item(const ERR_STATE *);
256static int int_err_get_next_lib(void);
257/* The static ERR_FNS table using these defaults functions */
258static const ERR_FNS err_defaults =
259	{
260	int_err_get,
261	int_err_del,
262	int_err_get_item,
263	int_err_set_item,
264	int_err_del_item,
265	int_thread_get,
266	int_thread_release,
267	int_thread_get_item,
268	int_thread_set_item,
269	int_thread_del_item,
270	int_err_get_next_lib
271	};
272
273/* The replacable table of ERR_FNS functions we use at run-time */
274static const ERR_FNS *err_fns = NULL;
275
276/* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */
277#define ERRFN(a) err_fns->cb_##a
278
279/* The internal state used by "err_defaults" - as such, the setting, reading,
280 * creating, and deleting of this data should only be permitted via the
281 * "err_defaults" functions. This way, a linked module can completely defer all
282 * ERR state operation (together with requisite locking) to the implementations
283 * and state in the loading application. */
284static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
285static LHASH_OF(ERR_STATE) *int_thread_hash = NULL;
286static int int_thread_hash_references = 0;
287static int int_err_library_number= ERR_LIB_USER;
288
289/* Internal function that checks whether "err_fns" is set and if not, sets it to
290 * the defaults. */
291static void err_fns_check(void)
292	{
293	if (err_fns) return;
294
295	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
296	if (!err_fns)
297		err_fns = &err_defaults;
298	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
299	}
300
301/* API functions to get or set the underlying ERR functions. */
302
303const ERR_FNS *ERR_get_implementation(void)
304	{
305	err_fns_check();
306	return err_fns;
307	}
308
309int ERR_set_implementation(const ERR_FNS *fns)
310	{
311	int ret = 0;
312
313	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
314	/* It's too late if 'err_fns' is non-NULL. BTW: not much point setting
315	 * an error is there?! */
316	if (!err_fns)
317		{
318		err_fns = fns;
319		ret = 1;
320		}
321	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
322	return ret;
323	}
324
325/* These are the callbacks provided to "lh_new()" when creating the LHASH tables
326 * internal to the "err_defaults" implementation. */
327
328static unsigned long get_error_values(int inc,int top,const char **file,int *line,
329				      const char **data,int *flags);
330
331/* The internal functions used in the "err_defaults" implementation */
332
333static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
334	{
335	unsigned long ret,l;
336
337	l=a->error;
338	ret=l^ERR_GET_LIB(l)^ERR_GET_FUNC(l);
339	return(ret^ret%19*13);
340	}
341static IMPLEMENT_LHASH_HASH_FN(err_string_data, ERR_STRING_DATA)
342
343static int err_string_data_cmp(const ERR_STRING_DATA *a,
344			       const ERR_STRING_DATA *b)
345	{
346	return (int)(a->error - b->error);
347	}
348static IMPLEMENT_LHASH_COMP_FN(err_string_data, ERR_STRING_DATA)
349
350static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create)
351	{
352	LHASH_OF(ERR_STRING_DATA) *ret = NULL;
353
354	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
355	if (!int_error_hash && create)
356		{
357		CRYPTO_push_info("int_err_get (err.c)");
358		int_error_hash = lh_ERR_STRING_DATA_new();
359		CRYPTO_pop_info();
360		}
361	if (int_error_hash)
362		ret = int_error_hash;
363	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
364
365	return ret;
366	}
367
368static void int_err_del(void)
369	{
370	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
371	if (int_error_hash)
372		{
373		lh_ERR_STRING_DATA_free(int_error_hash);
374		int_error_hash = NULL;
375		}
376	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
377	}
378
379static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
380	{
381	ERR_STRING_DATA *p;
382	LHASH_OF(ERR_STRING_DATA) *hash;
383
384	err_fns_check();
385	hash = ERRFN(err_get)(0);
386	if (!hash)
387		return NULL;
388
389	CRYPTO_r_lock(CRYPTO_LOCK_ERR);
390	p = lh_ERR_STRING_DATA_retrieve(hash, d);
391	CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
392
393	return p;
394	}
395
396static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *d)
397	{
398	ERR_STRING_DATA *p;
399	LHASH_OF(ERR_STRING_DATA) *hash;
400
401	err_fns_check();
402	hash = ERRFN(err_get)(1);
403	if (!hash)
404		return NULL;
405
406	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
407	p = lh_ERR_STRING_DATA_insert(hash, d);
408	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
409
410	return p;
411	}
412
413static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *d)
414	{
415	ERR_STRING_DATA *p;
416	LHASH_OF(ERR_STRING_DATA) *hash;
417
418	err_fns_check();
419	hash = ERRFN(err_get)(0);
420	if (!hash)
421		return NULL;
422
423	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
424	p = lh_ERR_STRING_DATA_delete(hash, d);
425	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
426
427	return p;
428	}
429
430static unsigned long err_state_hash(const ERR_STATE *a)
431	{
432	return CRYPTO_THREADID_hash(&a->tid) * 13;
433	}
434static IMPLEMENT_LHASH_HASH_FN(err_state, ERR_STATE)
435
436static int err_state_cmp(const ERR_STATE *a, const ERR_STATE *b)
437	{
438	return CRYPTO_THREADID_cmp(&a->tid, &b->tid);
439	}
440static IMPLEMENT_LHASH_COMP_FN(err_state, ERR_STATE)
441
442static LHASH_OF(ERR_STATE) *int_thread_get(int create)
443	{
444	LHASH_OF(ERR_STATE) *ret = NULL;
445
446	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
447	if (!int_thread_hash && create)
448		{
449		CRYPTO_push_info("int_thread_get (err.c)");
450		int_thread_hash = lh_ERR_STATE_new();
451		CRYPTO_pop_info();
452		}
453	if (int_thread_hash)
454		{
455		int_thread_hash_references++;
456		ret = int_thread_hash;
457		}
458	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
459	return ret;
460	}
461
462static void int_thread_release(LHASH_OF(ERR_STATE) **hash)
463	{
464	int i;
465
466	if (hash == NULL || *hash == NULL)
467		return;
468
469	i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR);
470	if (i > 0) return;
471
472	*hash = NULL;
473	}
474
475static ERR_STATE *int_thread_get_item(const ERR_STATE *d)
476	{
477	ERR_STATE *p;
478	LHASH_OF(ERR_STATE) *hash;
479
480	err_fns_check();
481	hash = ERRFN(thread_get)(0);
482	if (!hash)
483		return NULL;
484
485	CRYPTO_r_lock(CRYPTO_LOCK_ERR);
486	p = lh_ERR_STATE_retrieve(hash, d);
487	CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
488
489	ERRFN(thread_release)(&hash);
490	return p;
491	}
492
493static ERR_STATE *int_thread_set_item(ERR_STATE *d)
494	{
495	ERR_STATE *p;
496	LHASH_OF(ERR_STATE) *hash;
497
498	err_fns_check();
499	hash = ERRFN(thread_get)(1);
500	if (!hash)
501		return NULL;
502
503	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
504	p = lh_ERR_STATE_insert(hash, d);
505	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
506
507	ERRFN(thread_release)(&hash);
508	return p;
509	}
510
511static void int_thread_del_item(const ERR_STATE *d)
512	{
513	ERR_STATE *p;
514	LHASH_OF(ERR_STATE) *hash;
515
516	err_fns_check();
517	hash = ERRFN(thread_get)(0);
518	if (!hash)
519		return;
520
521	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
522	p = lh_ERR_STATE_delete(hash, d);
523	/* make sure we don't leak memory */
524	if (int_thread_hash_references == 1
525	    && int_thread_hash && lh_ERR_STATE_num_items(int_thread_hash) == 0)
526		{
527		lh_ERR_STATE_free(int_thread_hash);
528		int_thread_hash = NULL;
529		}
530	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
531
532	ERRFN(thread_release)(&hash);
533	if (p)
534		ERR_STATE_free(p);
535	}
536
537static int int_err_get_next_lib(void)
538	{
539	int ret;
540
541	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
542	ret = int_err_library_number++;
543	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
544
545	return ret;
546	}
547
548
549#ifndef OPENSSL_NO_ERR
550#define NUM_SYS_STR_REASONS 127
551#define LEN_SYS_STR_REASON 32
552
553static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
554/* SYS_str_reasons is filled with copies of strerror() results at
555 * initialization.
556 * 'errno' values up to 127 should cover all usual errors,
557 * others will be displayed numerically by ERR_error_string.
558 * It is crucial that we have something for each reason code
559 * that occurs in ERR_str_reasons, or bogus reason strings
560 * will be returned for SYSerr(), which always gets an errno
561 * value and never one of those 'standard' reason codes. */
562
563static void build_SYS_str_reasons(void)
564	{
565	/* malloc cannot be used here, use static storage instead */
566	static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
567	int i;
568	static int init = 1;
569
570	CRYPTO_r_lock(CRYPTO_LOCK_ERR);
571	if (!init)
572		{
573		CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
574		return;
575		}
576
577	CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
578	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
579	if (!init)
580		{
581		CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
582		return;
583		}
584
585	for (i = 1; i <= NUM_SYS_STR_REASONS; i++)
586		{
587		ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
588
589		str->error = (unsigned long)i;
590		if (str->string == NULL)
591			{
592			char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
593			char *src = strerror(i);
594			if (src != NULL)
595				{
596				strlcpy(*dest, src, sizeof *dest);
597				str->string = *dest;
598				}
599			}
600		if (str->string == NULL)
601			str->string = "unknown";
602		}
603
604	/* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL},
605	 * as required by ERR_load_strings. */
606
607	init = 0;
608
609	CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
610	}
611#endif
612
613#define err_clear_data(p,i) \
614	do { \
615	if (((p)->err_data[i] != NULL) && \
616		(p)->err_data_flags[i] & ERR_TXT_MALLOCED) \
617		{  \
618		free((p)->err_data[i]); \
619		(p)->err_data[i]=NULL; \
620		} \
621	(p)->err_data_flags[i]=0; \
622	} while(0)
623
624#define err_clear(p,i) \
625	do { \
626	(p)->err_flags[i]=0; \
627	(p)->err_buffer[i]=0; \
628	err_clear_data(p,i); \
629	(p)->err_file[i]=NULL; \
630	(p)->err_line[i]= -1; \
631	} while(0)
632
633static void ERR_STATE_free(ERR_STATE *s)
634	{
635	int i;
636
637	if (s == NULL)
638	    return;
639
640	for (i=0; i<ERR_NUM_ERRORS; i++)
641		{
642		err_clear_data(s,i);
643		}
644	free(s);
645	}
646
647void ERR_load_ERR_strings(void)
648	{
649	err_fns_check();
650#ifndef OPENSSL_NO_ERR
651	err_load_strings(0,ERR_str_libraries);
652	err_load_strings(0,ERR_str_reasons);
653	err_load_strings(ERR_LIB_SYS,ERR_str_functs);
654	build_SYS_str_reasons();
655	err_load_strings(ERR_LIB_SYS,SYS_str_reasons);
656#endif
657	}
658
659static void err_load_strings(int lib, ERR_STRING_DATA *str)
660	{
661	while (str->error)
662		{
663		if (lib)
664			str->error|=ERR_PACK(lib,0,0);
665		ERRFN(err_set_item)(str);
666		str++;
667		}
668	}
669
670void ERR_load_strings(int lib, ERR_STRING_DATA *str)
671	{
672	ERR_load_ERR_strings();
673	err_load_strings(lib, str);
674	}
675
676void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
677	{
678	while (str->error)
679		{
680		if (lib)
681			str->error|=ERR_PACK(lib,0,0);
682		ERRFN(err_del_item)(str);
683		str++;
684		}
685	}
686
687void ERR_free_strings(void)
688	{
689	err_fns_check();
690	ERRFN(err_del)();
691	}
692
693/********************************************************/
694
695void ERR_put_error(int lib, int func, int reason, const char *file,
696	     int line)
697	{
698	ERR_STATE *es;
699
700	es=ERR_get_state();
701
702	es->top=(es->top+1)%ERR_NUM_ERRORS;
703	if (es->top == es->bottom)
704		es->bottom=(es->bottom+1)%ERR_NUM_ERRORS;
705	es->err_flags[es->top]=0;
706	es->err_buffer[es->top]=ERR_PACK(lib,func,reason);
707	es->err_file[es->top]=file;
708	es->err_line[es->top]=line;
709	err_clear_data(es,es->top);
710	}
711
712void ERR_clear_error(void)
713	{
714	int i;
715	ERR_STATE *es;
716
717	es=ERR_get_state();
718
719	for (i=0; i<ERR_NUM_ERRORS; i++)
720		{
721		err_clear(es,i);
722		}
723	es->top=es->bottom=0;
724	}
725
726
727unsigned long ERR_get_error(void)
728	{ return(get_error_values(1,0,NULL,NULL,NULL,NULL)); }
729
730unsigned long ERR_get_error_line(const char **file,
731	     int *line)
732	{ return(get_error_values(1,0,file,line,NULL,NULL)); }
733
734unsigned long ERR_get_error_line_data(const char **file, int *line,
735	     const char **data, int *flags)
736	{ return(get_error_values(1,0,file,line,data,flags)); }
737
738
739unsigned long ERR_peek_error(void)
740	{ return(get_error_values(0,0,NULL,NULL,NULL,NULL)); }
741
742unsigned long ERR_peek_error_line(const char **file, int *line)
743	{ return(get_error_values(0,0,file,line,NULL,NULL)); }
744
745unsigned long ERR_peek_error_line_data(const char **file, int *line,
746	     const char **data, int *flags)
747	{ return(get_error_values(0,0,file,line,data,flags)); }
748
749
750unsigned long ERR_peek_last_error(void)
751	{ return(get_error_values(0,1,NULL,NULL,NULL,NULL)); }
752
753unsigned long ERR_peek_last_error_line(const char **file, int *line)
754	{ return(get_error_values(0,1,file,line,NULL,NULL)); }
755
756unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
757	     const char **data, int *flags)
758	{ return(get_error_values(0,1,file,line,data,flags)); }
759
760
761static unsigned long get_error_values(int inc, int top, const char **file, int *line,
762	     const char **data, int *flags)
763	{
764	int i=0;
765	ERR_STATE *es;
766	unsigned long ret;
767
768	es=ERR_get_state();
769
770	if (inc && top)
771		{
772		if (file) *file = "";
773		if (line) *line = 0;
774		if (data) *data = "";
775		if (flags) *flags = 0;
776
777		return ERR_R_INTERNAL_ERROR;
778		}
779
780	if (es->bottom == es->top) return 0;
781	if (top)
782		i=es->top;			 /* last error */
783	else
784		i=(es->bottom+1)%ERR_NUM_ERRORS; /* first error */
785
786	ret=es->err_buffer[i];
787	if (inc)
788		{
789		es->bottom=i;
790		es->err_buffer[i]=0;
791		}
792
793	if ((file != NULL) && (line != NULL))
794		{
795		if (es->err_file[i] == NULL)
796			{
797			*file="NA";
798			if (line != NULL) *line=0;
799			}
800		else
801			{
802			*file=es->err_file[i];
803			if (line != NULL) *line=es->err_line[i];
804			}
805		}
806
807	if (data == NULL)
808		{
809		if (inc)
810			{
811			err_clear_data(es, i);
812			}
813		}
814	else
815		{
816		if (es->err_data[i] == NULL)
817			{
818			*data="";
819			if (flags != NULL) *flags=0;
820			}
821		else
822			{
823			*data=es->err_data[i];
824			if (flags != NULL) *flags=es->err_data_flags[i];
825			}
826		}
827	return ret;
828	}
829
830void ERR_error_string_n(unsigned long e, char *buf, size_t len)
831	{
832	char lsbuf[64], fsbuf[64], rsbuf[64];
833	const char *ls,*fs,*rs;
834	unsigned long l,f,r;
835
836	l=ERR_GET_LIB(e);
837	f=ERR_GET_FUNC(e);
838	r=ERR_GET_REASON(e);
839
840	ls=ERR_lib_error_string(e);
841	fs=ERR_func_error_string(e);
842	rs=ERR_reason_error_string(e);
843
844	if (ls == NULL)
845		(void) snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
846	if (fs == NULL)
847		(void) snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
848	if (rs == NULL)
849		(void) snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
850
851	(void) snprintf(buf, len,"error:%08lX:%s:%s:%s", e, ls?ls:lsbuf,
852		fs?fs:fsbuf, rs?rs:rsbuf);
853	if (strlen(buf) == len-1)
854		{
855		/* output may be truncated; make sure we always have 5
856		 * colon-separated fields, i.e. 4 colons ... */
857#define NUM_COLONS 4
858		if (len > NUM_COLONS) /* ... if possible */
859			{
860			int i;
861			char *s = buf;
862
863			for (i = 0; i < NUM_COLONS; i++)
864				{
865				char *colon = strchr(s, ':');
866				if (colon == NULL || colon > &buf[len-1] - NUM_COLONS + i)
867					{
868					/* set colon no. i at last possible position
869					 * (buf[len-1] is the terminating 0)*/
870					colon = &buf[len-1] - NUM_COLONS + i;
871					*colon = ':';
872					}
873				s = colon + 1;
874				}
875			}
876		}
877	}
878
879/* BAD for multi-threading: uses a local buffer if ret == NULL */
880/* ERR_error_string_n should be used instead for ret != NULL
881 * as ERR_error_string cannot know how large the buffer is */
882char *ERR_error_string(unsigned long e, char *ret)
883	{
884	static char buf[256];
885
886	if (ret == NULL) ret=buf;
887	ERR_error_string_n(e, ret, 256);
888
889	return ret;
890	}
891
892LHASH_OF(ERR_STRING_DATA) *ERR_get_string_table(void)
893	{
894	err_fns_check();
895	return ERRFN(err_get)(0);
896	}
897
898LHASH_OF(ERR_STATE) *ERR_get_err_state_table(void)
899	{
900	err_fns_check();
901	return ERRFN(thread_get)(0);
902	}
903
904void ERR_release_err_state_table(LHASH_OF(ERR_STATE) **hash)
905	{
906	err_fns_check();
907	ERRFN(thread_release)(hash);
908	}
909
910const char *ERR_lib_error_string(unsigned long e)
911	{
912	ERR_STRING_DATA d,*p;
913	unsigned long l;
914
915	err_fns_check();
916	l=ERR_GET_LIB(e);
917	d.error=ERR_PACK(l,0,0);
918	p=ERRFN(err_get_item)(&d);
919	return((p == NULL)?NULL:p->string);
920	}
921
922const char *ERR_func_error_string(unsigned long e)
923	{
924	ERR_STRING_DATA d,*p;
925	unsigned long l,f;
926
927	err_fns_check();
928	l=ERR_GET_LIB(e);
929	f=ERR_GET_FUNC(e);
930	d.error=ERR_PACK(l,f,0);
931	p=ERRFN(err_get_item)(&d);
932	return((p == NULL)?NULL:p->string);
933	}
934
935const char *ERR_reason_error_string(unsigned long e)
936	{
937	ERR_STRING_DATA d,*p=NULL;
938	unsigned long l,r;
939
940	err_fns_check();
941	l=ERR_GET_LIB(e);
942	r=ERR_GET_REASON(e);
943	d.error=ERR_PACK(l,0,r);
944	p=ERRFN(err_get_item)(&d);
945	if (!p)
946		{
947		d.error=ERR_PACK(0,0,r);
948		p=ERRFN(err_get_item)(&d);
949		}
950	return((p == NULL)?NULL:p->string);
951	}
952
953void ERR_remove_thread_state(const CRYPTO_THREADID *id)
954	{
955	ERR_STATE tmp;
956
957	if (id)
958		CRYPTO_THREADID_cpy(&tmp.tid, id);
959	else
960		CRYPTO_THREADID_current(&tmp.tid);
961	err_fns_check();
962	/* thread_del_item automatically destroys the LHASH if the number of
963	 * items reaches zero. */
964	ERRFN(thread_del_item)(&tmp);
965	}
966
967#ifndef OPENSSL_NO_DEPRECATED
968void ERR_remove_state(unsigned long pid)
969	{
970	ERR_remove_thread_state(NULL);
971	}
972#endif
973
974ERR_STATE *ERR_get_state(void)
975	{
976	static ERR_STATE fallback;
977	ERR_STATE *ret,tmp,*tmpp=NULL;
978	int i;
979	CRYPTO_THREADID tid;
980
981	err_fns_check();
982	CRYPTO_THREADID_current(&tid);
983	CRYPTO_THREADID_cpy(&tmp.tid, &tid);
984	ret=ERRFN(thread_get_item)(&tmp);
985
986	/* ret == the error state, if NULL, make a new one */
987	if (ret == NULL)
988		{
989		ret=(ERR_STATE *)malloc(sizeof(ERR_STATE));
990		if (ret == NULL) return(&fallback);
991		CRYPTO_THREADID_cpy(&ret->tid, &tid);
992		ret->top=0;
993		ret->bottom=0;
994		for (i=0; i<ERR_NUM_ERRORS; i++)
995			{
996			ret->err_data[i]=NULL;
997			ret->err_data_flags[i]=0;
998			}
999		tmpp = ERRFN(thread_set_item)(ret);
1000		/* To check if insertion failed, do a get. */
1001		if (ERRFN(thread_get_item)(ret) != ret)
1002			{
1003			ERR_STATE_free(ret); /* could not insert it */
1004			return(&fallback);
1005			}
1006		/* If a race occured in this function and we came second, tmpp
1007		 * is the first one that we just replaced. */
1008		if (tmpp)
1009			ERR_STATE_free(tmpp);
1010		}
1011	return ret;
1012	}
1013
1014int ERR_get_next_error_library(void)
1015	{
1016	err_fns_check();
1017	return ERRFN(get_next_lib)();
1018	}
1019
1020void ERR_set_error_data(char *data, int flags)
1021	{
1022	ERR_STATE *es;
1023	int i;
1024
1025	es=ERR_get_state();
1026
1027	i=es->top;
1028	if (i == 0)
1029		i=ERR_NUM_ERRORS-1;
1030
1031	err_clear_data(es,i);
1032	es->err_data[i]=data;
1033	es->err_data_flags[i]=flags;
1034	}
1035
1036void ERR_add_error_data(int num, ...)
1037	{
1038	va_list args;
1039	va_start(args, num);
1040	ERR_add_error_vdata(num, args);
1041	va_end(args);
1042	}
1043
1044void ERR_add_error_vdata(int num, va_list args)
1045	{
1046	int i,n,s;
1047	char *str,*p,*a;
1048
1049	s=80;
1050	str=malloc(s+1);
1051	if (str == NULL) return;
1052	str[0]='\0';
1053
1054	n=0;
1055	for (i=0; i<num; i++)
1056		{
1057		a=va_arg(args, char*);
1058		/* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */
1059		if (a != NULL)
1060			{
1061			n+=strlen(a);
1062			if (n > s)
1063				{
1064				s=n+20;
1065				p=realloc(str,s+1);
1066				if (p == NULL)
1067					{
1068					free(str);
1069					return;
1070					}
1071				else
1072					str=p;
1073				}
1074			strlcat(str,a,(size_t)s+1);
1075			}
1076		}
1077	ERR_set_error_data(str,ERR_TXT_MALLOCED|ERR_TXT_STRING);
1078	}
1079
1080int ERR_set_mark(void)
1081	{
1082	ERR_STATE *es;
1083
1084	es=ERR_get_state();
1085
1086	if (es->bottom == es->top) return 0;
1087	es->err_flags[es->top]|=ERR_FLAG_MARK;
1088	return 1;
1089	}
1090
1091int ERR_pop_to_mark(void)
1092	{
1093	ERR_STATE *es;
1094
1095	es=ERR_get_state();
1096
1097	while(es->bottom != es->top
1098		&& (es->err_flags[es->top] & ERR_FLAG_MARK) == 0)
1099		{
1100		err_clear(es,es->top);
1101		es->top-=1;
1102		if (es->top == -1) es->top=ERR_NUM_ERRORS-1;
1103		}
1104
1105	if (es->bottom == es->top) return 0;
1106	es->err_flags[es->top]&=~ERR_FLAG_MARK;
1107	return 1;
1108	}
1109