summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/err/err.c
diff options
context:
space:
mode:
authorbeck <>2002-05-15 02:29:21 +0000
committerbeck <>2002-05-15 02:29:21 +0000
commitb64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch)
treefa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/err/err.c
parente471e1ea98d673597b182ea85f29e30c97cd08b5 (diff)
downloadopenbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz
openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2
openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/err/err.c')
-rw-r--r--src/lib/libcrypto/err/err.c615
1 files changed, 423 insertions, 192 deletions
diff --git a/src/lib/libcrypto/err/err.c b/src/lib/libcrypto/err/err.c
index 839f4ab81a..04773d65a6 100644
--- a/src/lib/libcrypto/err/err.c
+++ b/src/lib/libcrypto/err/err.c
@@ -56,7 +56,7 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58/* ==================================================================== 58/* ====================================================================
59 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 59 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60 * 60 *
61 * Redistribution and use in source and binary forms, with or without 61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions 62 * modification, are permitted provided that the following conditions
@@ -119,38 +119,28 @@
119#include <openssl/bio.h> 119#include <openssl/bio.h>
120#include <openssl/err.h> 120#include <openssl/err.h>
121 121
122static void err_load_strings(int lib, ERR_STRING_DATA *str);
122 123
123static LHASH *error_hash=NULL;
124static LHASH *thread_hash=NULL;
125
126static unsigned long err_hash(ERR_STRING_DATA *a);
127static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b);
128static unsigned long pid_hash(ERR_STATE *pid);
129static int pid_cmp(ERR_STATE *a,ERR_STATE *pid);
130static unsigned long get_error_values(int inc,const char **file,int *line,
131 const char **data,int *flags);
132static void ERR_STATE_free(ERR_STATE *s); 124static void ERR_STATE_free(ERR_STATE *s);
133#ifndef NO_ERR 125#ifndef OPENSSL_NO_ERR
134static ERR_STRING_DATA ERR_str_libraries[]= 126static ERR_STRING_DATA ERR_str_libraries[]=
135 { 127 {
136{ERR_PACK(ERR_LIB_NONE,0,0) ,"unknown library"}, 128{ERR_PACK(ERR_LIB_NONE,0,0) ,"unknown library"},
137{ERR_PACK(ERR_LIB_SYS,0,0) ,"system library"}, 129{ERR_PACK(ERR_LIB_SYS,0,0) ,"system library"},
138{ERR_PACK(ERR_LIB_BN,0,0) ,"bignum routines"}, 130{ERR_PACK(ERR_LIB_BN,0,0) ,"bignum routines"},
139{ERR_PACK(ERR_LIB_RSA,0,0) ,"rsa routines"}, 131{ERR_PACK(ERR_LIB_RSA,0,0) ,"rsa routines"},
140{ERR_PACK(ERR_LIB_DSA,0,0) ,"dsa routines"},
141{ERR_PACK(ERR_LIB_DH,0,0) ,"Diffie-Hellman routines"}, 132{ERR_PACK(ERR_LIB_DH,0,0) ,"Diffie-Hellman routines"},
142{ERR_PACK(ERR_LIB_EVP,0,0) ,"digital envelope routines"}, 133{ERR_PACK(ERR_LIB_EVP,0,0) ,"digital envelope routines"},
143{ERR_PACK(ERR_LIB_BUF,0,0) ,"memory buffer routines"}, 134{ERR_PACK(ERR_LIB_BUF,0,0) ,"memory buffer routines"},
144{ERR_PACK(ERR_LIB_BIO,0,0) ,"BIO routines"},
145{ERR_PACK(ERR_LIB_OBJ,0,0) ,"object identifier routines"}, 135{ERR_PACK(ERR_LIB_OBJ,0,0) ,"object identifier routines"},
146{ERR_PACK(ERR_LIB_PEM,0,0) ,"PEM routines"}, 136{ERR_PACK(ERR_LIB_PEM,0,0) ,"PEM routines"},
147{ERR_PACK(ERR_LIB_ASN1,0,0) ,"asn1 encoding routines"}, 137{ERR_PACK(ERR_LIB_DSA,0,0) ,"dsa routines"},
148{ERR_PACK(ERR_LIB_X509,0,0) ,"x509 certificate routines"}, 138{ERR_PACK(ERR_LIB_X509,0,0) ,"x509 certificate routines"},
139{ERR_PACK(ERR_LIB_ASN1,0,0) ,"asn1 encoding routines"},
149{ERR_PACK(ERR_LIB_CONF,0,0) ,"configuration file routines"}, 140{ERR_PACK(ERR_LIB_CONF,0,0) ,"configuration file routines"},
150{ERR_PACK(ERR_LIB_METH,0,0) ,"X509 lookup 'method' routines"}, 141{ERR_PACK(ERR_LIB_CRYPTO,0,0) ,"common libcrypto routines"},
142{ERR_PACK(ERR_LIB_EC,0,0) ,"elliptic curve routines"},
151{ERR_PACK(ERR_LIB_SSL,0,0) ,"SSL routines"}, 143{ERR_PACK(ERR_LIB_SSL,0,0) ,"SSL routines"},
152{ERR_PACK(ERR_LIB_RSAREF,0,0) ,"RSAref routines"},
153{ERR_PACK(ERR_LIB_PROXY,0,0) ,"Proxy routines"},
154{ERR_PACK(ERR_LIB_BIO,0,0) ,"BIO routines"}, 144{ERR_PACK(ERR_LIB_BIO,0,0) ,"BIO routines"},
155{ERR_PACK(ERR_LIB_PKCS7,0,0) ,"PKCS7 routines"}, 145{ERR_PACK(ERR_LIB_PKCS7,0,0) ,"PKCS7 routines"},
156{ERR_PACK(ERR_LIB_X509V3,0,0) ,"X509 V3 routines"}, 146{ERR_PACK(ERR_LIB_X509V3,0,0) ,"X509 V3 routines"},
@@ -158,6 +148,7 @@ static ERR_STRING_DATA ERR_str_libraries[]=
158{ERR_PACK(ERR_LIB_RAND,0,0) ,"random number generator"}, 148{ERR_PACK(ERR_LIB_RAND,0,0) ,"random number generator"},
159{ERR_PACK(ERR_LIB_DSO,0,0) ,"DSO support routines"}, 149{ERR_PACK(ERR_LIB_DSO,0,0) ,"DSO support routines"},
160{ERR_PACK(ERR_LIB_ENGINE,0,0) ,"engine routines"}, 150{ERR_PACK(ERR_LIB_ENGINE,0,0) ,"engine routines"},
151{ERR_PACK(ERR_LIB_OCSP,0,0) ,"OCSP routines"},
161{0,NULL}, 152{0,NULL},
162 }; 153 };
163 154
@@ -171,7 +162,7 @@ static ERR_STRING_DATA ERR_str_functs[]=
171 {ERR_PACK(0,SYS_F_BIND,0), "bind"}, 162 {ERR_PACK(0,SYS_F_BIND,0), "bind"},
172 {ERR_PACK(0,SYS_F_LISTEN,0), "listen"}, 163 {ERR_PACK(0,SYS_F_LISTEN,0), "listen"},
173 {ERR_PACK(0,SYS_F_ACCEPT,0), "accept"}, 164 {ERR_PACK(0,SYS_F_ACCEPT,0), "accept"},
174#ifdef WINDOWS 165#ifdef OPENSSL_SYS_WINDOWS
175 {ERR_PACK(0,SYS_F_WSASTARTUP,0), "WSAstartup"}, 166 {ERR_PACK(0,SYS_F_WSASTARTUP,0), "WSAstartup"},
176#endif 167#endif
177 {ERR_PACK(0,SYS_F_OPENDIR,0), "opendir"}, 168 {ERR_PACK(0,SYS_F_OPENDIR,0), "opendir"},
@@ -180,41 +171,325 @@ static ERR_STRING_DATA ERR_str_functs[]=
180 171
181static ERR_STRING_DATA ERR_str_reasons[]= 172static ERR_STRING_DATA ERR_str_reasons[]=
182 { 173 {
183{ERR_R_FATAL ,"fatal"},
184{ERR_R_SYS_LIB ,"system lib"}, 174{ERR_R_SYS_LIB ,"system lib"},
185{ERR_R_BN_LIB ,"BN lib"}, 175{ERR_R_BN_LIB ,"BN lib"},
186{ERR_R_RSA_LIB ,"RSA lib"}, 176{ERR_R_RSA_LIB ,"RSA lib"},
187{ERR_R_DH_LIB ,"DH lib"}, 177{ERR_R_DH_LIB ,"DH lib"},
188{ERR_R_EVP_LIB ,"EVP lib"}, 178{ERR_R_EVP_LIB ,"EVP lib"},
189{ERR_R_BUF_LIB ,"BUF lib"}, 179{ERR_R_BUF_LIB ,"BUF lib"},
190{ERR_R_BIO_LIB ,"BIO lib"},
191{ERR_R_OBJ_LIB ,"OBJ lib"}, 180{ERR_R_OBJ_LIB ,"OBJ lib"},
192{ERR_R_PEM_LIB ,"PEM lib"}, 181{ERR_R_PEM_LIB ,"PEM lib"},
182{ERR_R_DSA_LIB ,"DSA lib"},
193{ERR_R_X509_LIB ,"X509 lib"}, 183{ERR_R_X509_LIB ,"X509 lib"},
194{ERR_R_METH_LIB ,"METH lib"},
195{ERR_R_ASN1_LIB ,"ASN1 lib"}, 184{ERR_R_ASN1_LIB ,"ASN1 lib"},
196{ERR_R_CONF_LIB ,"CONF lib"}, 185{ERR_R_CONF_LIB ,"CONF lib"},
186{ERR_R_CRYPTO_LIB ,"CRYPTO lib"},
187{ERR_R_EC_LIB ,"EC lib"},
197{ERR_R_SSL_LIB ,"SSL lib"}, 188{ERR_R_SSL_LIB ,"SSL lib"},
198{ERR_R_PROXY_LIB ,"PROXY lib"},
199{ERR_R_BIO_LIB ,"BIO lib"}, 189{ERR_R_BIO_LIB ,"BIO lib"},
200{ERR_R_PKCS7_LIB ,"PKCS7 lib"}, 190{ERR_R_PKCS7_LIB ,"PKCS7 lib"},
191{ERR_R_X509V3_LIB ,"X509V3 lib"},
201{ERR_R_PKCS12_LIB ,"PKCS12 lib"}, 192{ERR_R_PKCS12_LIB ,"PKCS12 lib"},
202{ERR_R_MALLOC_FAILURE ,"Malloc failure"}, 193{ERR_R_RAND_LIB ,"RAND lib"},
203{ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED ,"called a function you should not call"}, 194{ERR_R_DSO_LIB ,"DSO lib"},
204{ERR_R_PASSED_NULL_PARAMETER ,"passed a null parameter"}, 195{ERR_R_ENGINE_LIB ,"ENGINE lib"},
196{ERR_R_OCSP_LIB ,"OCSP lib"},
197
205{ERR_R_NESTED_ASN1_ERROR ,"nested asn1 error"}, 198{ERR_R_NESTED_ASN1_ERROR ,"nested asn1 error"},
206{ERR_R_BAD_ASN1_OBJECT_HEADER ,"bad asn1 object header"}, 199{ERR_R_BAD_ASN1_OBJECT_HEADER ,"bad asn1 object header"},
207{ERR_R_BAD_GET_ASN1_OBJECT_CALL ,"bad get asn1 object call"}, 200{ERR_R_BAD_GET_ASN1_OBJECT_CALL ,"bad get asn1 object call"},
208{ERR_R_EXPECTING_AN_ASN1_SEQUENCE ,"expecting an asn1 sequence"}, 201{ERR_R_EXPECTING_AN_ASN1_SEQUENCE ,"expecting an asn1 sequence"},
209{ERR_R_ASN1_LENGTH_MISMATCH ,"asn1 length mismatch"}, 202{ERR_R_ASN1_LENGTH_MISMATCH ,"asn1 length mismatch"},
210{ERR_R_MISSING_ASN1_EOS ,"missing asn1 eos"}, 203{ERR_R_MISSING_ASN1_EOS ,"missing asn1 eos"},
211{ERR_R_DSO_LIB ,"DSO lib"}, 204
212{ERR_R_ENGINE_LIB ,"ENGINE lib"}, 205{ERR_R_FATAL ,"fatal"},
206{ERR_R_MALLOC_FAILURE ,"malloc failure"},
207{ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED ,"called a function you should not call"},
208{ERR_R_PASSED_NULL_PARAMETER ,"passed a null parameter"},
209{ERR_R_INTERNAL_ERROR ,"internal error"},
213 210
214{0,NULL}, 211{0,NULL},
215 }; 212 };
216 213
217 214
215/* Define the predeclared (but externally opaque) "ERR_FNS" type */
216struct st_ERR_FNS
217 {
218 /* Works on the "error_hash" string table */
219 LHASH *(*cb_err_get)(int create);
220 void (*cb_err_del)(void);
221 ERR_STRING_DATA *(*cb_err_get_item)(const ERR_STRING_DATA *);
222 ERR_STRING_DATA *(*cb_err_set_item)(ERR_STRING_DATA *);
223 ERR_STRING_DATA *(*cb_err_del_item)(ERR_STRING_DATA *);
224 /* Works on the "thread_hash" error-state table */
225 LHASH *(*cb_thread_get)(int create);
226 ERR_STATE *(*cb_thread_get_item)(const ERR_STATE *);
227 ERR_STATE *(*cb_thread_set_item)(ERR_STATE *);
228 void (*cb_thread_del_item)(const ERR_STATE *);
229 /* Returns the next available error "library" numbers */
230 int (*cb_get_next_lib)(void);
231 };
232
233/* Predeclarations of the "err_defaults" functions */
234static LHASH *int_err_get(int create);
235static void int_err_del(void);
236static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
237static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *);
238static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *);
239static LHASH *int_thread_get(int create);
240static ERR_STATE *int_thread_get_item(const ERR_STATE *);
241static ERR_STATE *int_thread_set_item(ERR_STATE *);
242static void int_thread_del_item(const ERR_STATE *);
243static int int_err_get_next_lib(void);
244/* The static ERR_FNS table using these defaults functions */
245static const ERR_FNS err_defaults =
246 {
247 int_err_get,
248 int_err_del,
249 int_err_get_item,
250 int_err_set_item,
251 int_err_del_item,
252 int_thread_get,
253 int_thread_get_item,
254 int_thread_set_item,
255 int_thread_del_item,
256 int_err_get_next_lib
257 };
258
259/* The replacable table of ERR_FNS functions we use at run-time */
260static const ERR_FNS *err_fns = NULL;
261
262/* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */
263#define ERRFN(a) err_fns->cb_##a
264
265/* The internal state used by "err_defaults" - as such, the setting, reading,
266 * creating, and deleting of this data should only be permitted via the
267 * "err_defaults" functions. This way, a linked module can completely defer all
268 * ERR state operation (together with requisite locking) to the implementations
269 * and state in the loading application. */
270static LHASH *int_error_hash = NULL;
271static LHASH *int_thread_hash = NULL;
272static int int_err_library_number= ERR_LIB_USER;
273
274/* Internal function that checks whether "err_fns" is set and if not, sets it to
275 * the defaults. */
276static void err_fns_check(void)
277 {
278 if (err_fns) return;
279
280 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
281 if (!err_fns)
282 err_fns = &err_defaults;
283 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
284 }
285
286/* API functions to get or set the underlying ERR functions. */
287
288const ERR_FNS *ERR_get_implementation(void)
289 {
290 err_fns_check();
291 return err_fns;
292 }
293
294int ERR_set_implementation(const ERR_FNS *fns)
295 {
296 int ret = 0;
297
298 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
299 /* It's too late if 'err_fns' is non-NULL. BTW: not much point setting
300 * an error is there?! */
301 if (!err_fns)
302 {
303 err_fns = fns;
304 ret = 1;
305 }
306 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
307 return ret;
308 }
309
310/* These are the callbacks provided to "lh_new()" when creating the LHASH tables
311 * internal to the "err_defaults" implementation. */
312
313/* static unsigned long err_hash(ERR_STRING_DATA *a); */
314static unsigned long err_hash(const void *a_void);
315/* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b); */
316static int err_cmp(const void *a_void, const void *b_void);
317/* static unsigned long pid_hash(ERR_STATE *pid); */
318static unsigned long pid_hash(const void *pid_void);
319/* static int pid_cmp(ERR_STATE *a,ERR_STATE *pid); */
320static int pid_cmp(const void *a_void,const void *pid_void);
321static unsigned long get_error_values(int inc,int top,const char **file,int *line,
322 const char **data,int *flags);
323
324/* The internal functions used in the "err_defaults" implementation */
325
326static LHASH *int_err_get(int create)
327 {
328 LHASH *ret = NULL;
329
330 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
331 if (!int_error_hash && create)
332 {
333 CRYPTO_push_info("int_err_get (err.c)");
334 int_error_hash = lh_new(err_hash, err_cmp);
335 CRYPTO_pop_info();
336 }
337 if (int_error_hash)
338 ret = int_error_hash;
339 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
340
341 return ret;
342 }
343
344static void int_err_del(void)
345 {
346 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
347 if (int_error_hash)
348 {
349 lh_free(int_error_hash);
350 int_error_hash = NULL;
351 }
352 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
353 }
354
355static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
356 {
357 ERR_STRING_DATA *p;
358 LHASH *hash;
359
360 err_fns_check();
361 hash = ERRFN(err_get)(0);
362 if (!hash)
363 return NULL;
364
365 CRYPTO_r_lock(CRYPTO_LOCK_ERR);
366 p = (ERR_STRING_DATA *)lh_retrieve(hash, d);
367 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
368
369 return p;
370 }
371
372static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *d)
373 {
374 ERR_STRING_DATA *p;
375 LHASH *hash;
376
377 err_fns_check();
378 hash = ERRFN(err_get)(1);
379 if (!hash)
380 return NULL;
381
382 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
383 p = (ERR_STRING_DATA *)lh_insert(hash, d);
384 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
385
386 return p;
387 }
388
389static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *d)
390 {
391 ERR_STRING_DATA *p;
392 LHASH *hash;
393
394 err_fns_check();
395 hash = ERRFN(err_get)(0);
396 if (!hash)
397 return NULL;
398
399 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
400 p = (ERR_STRING_DATA *)lh_delete(hash, d);
401 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
402
403 return p;
404 }
405
406static LHASH *int_thread_get(int create)
407 {
408 LHASH *ret = NULL;
409
410 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
411 if (!int_thread_hash && create)
412 {
413 CRYPTO_push_info("int_thread_get (err.c)");
414 int_thread_hash = lh_new(pid_hash, pid_cmp);
415 CRYPTO_pop_info();
416 }
417 if (int_thread_hash)
418 ret = int_thread_hash;
419 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
420 return ret;
421 }
422
423static ERR_STATE *int_thread_get_item(const ERR_STATE *d)
424 {
425 ERR_STATE *p;
426 LHASH *hash;
427
428 err_fns_check();
429 hash = ERRFN(thread_get)(0);
430 if (!hash)
431 return NULL;
432
433 CRYPTO_r_lock(CRYPTO_LOCK_ERR);
434 p = (ERR_STATE *)lh_retrieve(hash, d);
435 CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
436
437 return p;
438 }
439
440static ERR_STATE *int_thread_set_item(ERR_STATE *d)
441 {
442 ERR_STATE *p;
443 LHASH *hash;
444
445 err_fns_check();
446 hash = ERRFN(thread_get)(1);
447 if (!hash)
448 return NULL;
449
450 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
451 p = (ERR_STATE *)lh_insert(hash, d);
452 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
453
454 return p;
455 }
456
457static void int_thread_del_item(const ERR_STATE *d)
458 {
459 ERR_STATE *p;
460 LHASH *hash;
461
462 err_fns_check();
463 hash = ERRFN(thread_get)(0);
464 if (!hash)
465 return;
466
467 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
468 p = (ERR_STATE *)lh_delete(hash, d);
469 /* make sure we don't leak memory */
470 if (int_thread_hash && (lh_num_items(int_thread_hash) == 0))
471 {
472 lh_free(int_thread_hash);
473 int_thread_hash = NULL;
474 }
475 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
476
477 if (p)
478 ERR_STATE_free(p);
479 }
480
481static int int_err_get_next_lib(void)
482 {
483 int ret;
484
485 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
486 ret = int_err_library_number++;
487 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
488
489 return ret;
490 }
491
492
218#define NUM_SYS_STR_REASONS 127 493#define NUM_SYS_STR_REASONS 127
219#define LEN_SYS_STR_REASON 32 494#define LEN_SYS_STR_REASON 32
220 495
@@ -233,8 +508,11 @@ static void build_SYS_str_reasons()
233 /* OPENSSL_malloc cannot be used here, use static storage instead */ 508 /* OPENSSL_malloc cannot be used here, use static storage instead */
234 static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON]; 509 static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
235 int i; 510 int i;
511 static int init = 1;
512
513 if (!init) return;
236 514
237 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH); 515 CRYPTO_w_lock(CRYPTO_LOCK_ERR);
238 516
239 for (i = 1; i <= NUM_SYS_STR_REASONS; i++) 517 for (i = 1; i <= NUM_SYS_STR_REASONS; i++)
240 { 518 {
@@ -259,7 +537,9 @@ static void build_SYS_str_reasons()
259 /* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, 537 /* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL},
260 * as required by ERR_load_strings. */ 538 * as required by ERR_load_strings. */
261 539
262 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH); 540 init = 0;
541
542 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
263 } 543 }
264#endif 544#endif
265 545
@@ -276,7 +556,7 @@ static void ERR_STATE_free(ERR_STATE *s)
276 { 556 {
277 int i; 557 int i;
278 558
279 if(s == NULL) 559 if (s == NULL)
280 return; 560 return;
281 561
282 for (i=0; i<ERR_NUM_ERRORS; i++) 562 for (i=0; i<ERR_NUM_ERRORS; i++)
@@ -288,66 +568,46 @@ static void ERR_STATE_free(ERR_STATE *s)
288 568
289void ERR_load_ERR_strings(void) 569void ERR_load_ERR_strings(void)
290 { 570 {
291 static int init=1; 571 err_fns_check();
572#ifndef OPENSSL_NO_ERR
573 err_load_strings(0,ERR_str_libraries);
574 err_load_strings(0,ERR_str_reasons);
575 err_load_strings(ERR_LIB_SYS,ERR_str_functs);
576 build_SYS_str_reasons();
577 err_load_strings(ERR_LIB_SYS,SYS_str_reasons);
578#endif
579 }
292 580
293 if (init) 581static void err_load_strings(int lib, ERR_STRING_DATA *str)
582 {
583 while (str->error)
294 { 584 {
295 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 585 str->error|=ERR_PACK(lib,0,0);
296 if (init == 0) 586 ERRFN(err_set_item)(str);
297 { 587 str++;
298 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
299 return;
300 }
301 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
302
303#ifndef NO_ERR
304 ERR_load_strings(0,ERR_str_libraries);
305 ERR_load_strings(0,ERR_str_reasons);
306 ERR_load_strings(ERR_LIB_SYS,ERR_str_functs);
307 build_SYS_str_reasons();
308 ERR_load_strings(ERR_LIB_SYS,SYS_str_reasons);
309#endif
310 init=0;
311 } 588 }
312 } 589 }
313 590
314void ERR_load_strings(int lib, ERR_STRING_DATA *str) 591void ERR_load_strings(int lib, ERR_STRING_DATA *str)
315 { 592 {
316 if (error_hash == NULL) 593 ERR_load_ERR_strings();
317 { 594 err_load_strings(lib, str);
318 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH); 595 }
319 error_hash=lh_new(err_hash,err_cmp);
320 if (error_hash == NULL)
321 {
322 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
323 return;
324 }
325 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
326
327 ERR_load_ERR_strings();
328 }
329 596
330 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH); 597void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
598 {
331 while (str->error) 599 while (str->error)
332 { 600 {
333 str->error|=ERR_PACK(lib,0,0); 601 str->error|=ERR_PACK(lib,0,0);
334 lh_insert(error_hash,str); 602 ERRFN(err_del_item)(str);
335 str++; 603 str++;
336 } 604 }
337 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
338 } 605 }
339 606
340void ERR_free_strings(void) 607void ERR_free_strings(void)
341 { 608 {
342 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 609 err_fns_check();
343 610 ERRFN(err_del)();
344 if (error_hash != NULL)
345 {
346 lh_free(error_hash);
347 error_hash=NULL;
348 }
349
350 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
351 } 611 }
352 612
353/********************************************************/ 613/********************************************************/
@@ -406,30 +666,40 @@ void ERR_clear_error(void)
406 666
407 667
408unsigned long ERR_get_error(void) 668unsigned long ERR_get_error(void)
409 { return(get_error_values(1,NULL,NULL,NULL,NULL)); } 669 { return(get_error_values(1,0,NULL,NULL,NULL,NULL)); }
410 670
411unsigned long ERR_get_error_line(const char **file, 671unsigned long ERR_get_error_line(const char **file,
412 int *line) 672 int *line)
413 { return(get_error_values(1,file,line,NULL,NULL)); } 673 { return(get_error_values(1,0,file,line,NULL,NULL)); }
414 674
415unsigned long ERR_get_error_line_data(const char **file, int *line, 675unsigned long ERR_get_error_line_data(const char **file, int *line,
416 const char **data, int *flags) 676 const char **data, int *flags)
417 { return(get_error_values(1,file,line, 677 { return(get_error_values(1,0,file,line,data,flags)); }
418 data,flags)); } 678
419 679
420unsigned long ERR_peek_error(void) 680unsigned long ERR_peek_error(void)
421 { return(get_error_values(0,NULL,NULL,NULL,NULL)); } 681 { return(get_error_values(0,0,NULL,NULL,NULL,NULL)); }
422 682
423unsigned long ERR_peek_error_line(const char **file, 683unsigned long ERR_peek_error_line(const char **file, int *line)
424 int *line) 684 { return(get_error_values(0,0,file,line,NULL,NULL)); }
425 { return(get_error_values(0,file,line,NULL,NULL)); }
426 685
427unsigned long ERR_peek_error_line_data(const char **file, int *line, 686unsigned long ERR_peek_error_line_data(const char **file, int *line,
428 const char **data, int *flags) 687 const char **data, int *flags)
429 { return(get_error_values(0,file,line, 688 { return(get_error_values(0,0,file,line,data,flags)); }
430 data,flags)); } 689
690
691unsigned long ERR_peek_last_error(void)
692 { return(get_error_values(0,1,NULL,NULL,NULL,NULL)); }
693
694unsigned long ERR_peek_last_error_line(const char **file, int *line)
695 { return(get_error_values(0,1,file,line,NULL,NULL)); }
696
697unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
698 const char **data, int *flags)
699 { return(get_error_values(0,1,file,line,data,flags)); }
700
431 701
432static unsigned long get_error_values(int inc, const char **file, int *line, 702static unsigned long get_error_values(int inc, int top, const char **file, int *line,
433 const char **data, int *flags) 703 const char **data, int *flags)
434 { 704 {
435 int i=0; 705 int i=0;
@@ -438,8 +708,21 @@ static unsigned long get_error_values(int inc, const char **file, int *line,
438 708
439 es=ERR_get_state(); 709 es=ERR_get_state();
440 710
441 if (es->bottom == es->top) return(0); 711 if (inc && top)
442 i=(es->bottom+1)%ERR_NUM_ERRORS; 712 {
713 if (file) *file = "";
714 if (line) *line = 0;
715 if (data) *data = "";
716 if (flags) *flags = 0;
717
718 return ERR_R_INTERNAL_ERROR;
719 }
720
721 if (es->bottom == es->top) return 0;
722 if (top)
723 i=es->top; /* last error */
724 else
725 i=(es->bottom+1)%ERR_NUM_ERRORS; /* first error */
443 726
444 ret=es->err_buffer[i]; 727 ret=es->err_buffer[i];
445 if (inc) 728 if (inc)
@@ -482,7 +765,7 @@ static unsigned long get_error_values(int inc, const char **file, int *line,
482 if (flags != NULL) *flags=es->err_data_flags[i]; 765 if (flags != NULL) *flags=es->err_data_flags[i];
483 } 766 }
484 } 767 }
485 return(ret); 768 return ret;
486 } 769 }
487 770
488void ERR_error_string_n(unsigned long e, char *buf, size_t len) 771void ERR_error_string_n(unsigned long e, char *buf, size_t len)
@@ -544,58 +827,43 @@ char *ERR_error_string(unsigned long e, char *ret)
544 if (ret == NULL) ret=buf; 827 if (ret == NULL) ret=buf;
545 ERR_error_string_n(e, ret, 256); 828 ERR_error_string_n(e, ret, 256);
546 829
547 return(ret); 830 return ret;
548 } 831 }
549 832
550LHASH *ERR_get_string_table(void) 833LHASH *ERR_get_string_table(void)
551 { 834 {
552 return(error_hash); 835 err_fns_check();
836 return ERRFN(err_get)(0);
553 } 837 }
554 838
555/* not thread-safe */
556LHASH *ERR_get_err_state_table(void) 839LHASH *ERR_get_err_state_table(void)
557 { 840 {
558 return(thread_hash); 841 err_fns_check();
842 return ERRFN(thread_get)(0);
559 } 843 }
560 844
561const char *ERR_lib_error_string(unsigned long e) 845const char *ERR_lib_error_string(unsigned long e)
562 { 846 {
563 ERR_STRING_DATA d,*p=NULL; 847 ERR_STRING_DATA d,*p;
564 unsigned long l; 848 unsigned long l;
565 849
850 err_fns_check();
566 l=ERR_GET_LIB(e); 851 l=ERR_GET_LIB(e);
567 852 d.error=ERR_PACK(l,0,0);
568 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH); 853 p=ERRFN(err_get_item)(&d);
569
570 if (error_hash != NULL)
571 {
572 d.error=ERR_PACK(l,0,0);
573 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,&d);
574 }
575
576 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
577
578 return((p == NULL)?NULL:p->string); 854 return((p == NULL)?NULL:p->string);
579 } 855 }
580 856
581const char *ERR_func_error_string(unsigned long e) 857const char *ERR_func_error_string(unsigned long e)
582 { 858 {
583 ERR_STRING_DATA d,*p=NULL; 859 ERR_STRING_DATA d,*p;
584 unsigned long l,f; 860 unsigned long l,f;
585 861
862 err_fns_check();
586 l=ERR_GET_LIB(e); 863 l=ERR_GET_LIB(e);
587 f=ERR_GET_FUNC(e); 864 f=ERR_GET_FUNC(e);
588 865 d.error=ERR_PACK(l,f,0);
589 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH); 866 p=ERRFN(err_get_item)(&d);
590
591 if (error_hash != NULL)
592 {
593 d.error=ERR_PACK(l,f,0);
594 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,&d);
595 }
596
597 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
598
599 return((p == NULL)?NULL:p->string); 867 return((p == NULL)?NULL:p->string);
600 } 868 }
601 869
@@ -604,93 +872,73 @@ const char *ERR_reason_error_string(unsigned long e)
604 ERR_STRING_DATA d,*p=NULL; 872 ERR_STRING_DATA d,*p=NULL;
605 unsigned long l,r; 873 unsigned long l,r;
606 874
875 err_fns_check();
607 l=ERR_GET_LIB(e); 876 l=ERR_GET_LIB(e);
608 r=ERR_GET_REASON(e); 877 r=ERR_GET_REASON(e);
609 878 d.error=ERR_PACK(l,0,r);
610 CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH); 879 p=ERRFN(err_get_item)(&d);
611 880 if (!p)
612 if (error_hash != NULL)
613 { 881 {
614 d.error=ERR_PACK(l,0,r); 882 d.error=ERR_PACK(0,0,r);
615 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,&d); 883 p=ERRFN(err_get_item)(&d);
616 if (p == NULL)
617 {
618 d.error=ERR_PACK(0,0,r);
619 p=(ERR_STRING_DATA *)lh_retrieve(error_hash,&d);
620 }
621 } 884 }
622
623 CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
624
625 return((p == NULL)?NULL:p->string); 885 return((p == NULL)?NULL:p->string);
626 } 886 }
627 887
628static unsigned long err_hash(ERR_STRING_DATA *a) 888/* static unsigned long err_hash(ERR_STRING_DATA *a) */
889static unsigned long err_hash(const void *a_void)
629 { 890 {
630 unsigned long ret,l; 891 unsigned long ret,l;
631 892
632 l=a->error; 893 l=((ERR_STRING_DATA *)a_void)->error;
633 ret=l^ERR_GET_LIB(l)^ERR_GET_FUNC(l); 894 ret=l^ERR_GET_LIB(l)^ERR_GET_FUNC(l);
634 return(ret^ret%19*13); 895 return(ret^ret%19*13);
635 } 896 }
636 897
637static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b) 898/* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b) */
899static int err_cmp(const void *a_void, const void *b_void)
638 { 900 {
639 return((int)(a->error-b->error)); 901 return((int)(((ERR_STRING_DATA *)a_void)->error -
902 ((ERR_STRING_DATA *)b_void)->error));
640 } 903 }
641 904
642static unsigned long pid_hash(ERR_STATE *a) 905/* static unsigned long pid_hash(ERR_STATE *a) */
906static unsigned long pid_hash(const void *a_void)
643 { 907 {
644 return(a->pid*13); 908 return(((ERR_STATE *)a_void)->pid*13);
645 } 909 }
646 910
647static int pid_cmp(ERR_STATE *a, ERR_STATE *b) 911/* static int pid_cmp(ERR_STATE *a, ERR_STATE *b) */
912static int pid_cmp(const void *a_void, const void *b_void)
648 { 913 {
649 return((int)((long)a->pid - (long)b->pid)); 914 return((int)((long)((ERR_STATE *)a_void)->pid -
915 (long)((ERR_STATE *)b_void)->pid));
650 } 916 }
651 917
652void ERR_remove_state(unsigned long pid) 918void ERR_remove_state(unsigned long pid)
653 { 919 {
654 ERR_STATE *p = NULL,tmp; 920 ERR_STATE tmp;
655 921
656 if (thread_hash == NULL) 922 err_fns_check();
657 return;
658 if (pid == 0) 923 if (pid == 0)
659 pid=(unsigned long)CRYPTO_thread_id(); 924 pid=(unsigned long)CRYPTO_thread_id();
660 tmp.pid=pid; 925 tmp.pid=pid;
661 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 926 /* thread_del_item automatically destroys the LHASH if the number of
662 if (thread_hash) 927 * items reaches zero. */
663 { 928 ERRFN(thread_del_item)(&tmp);
664 p=(ERR_STATE *)lh_delete(thread_hash,&tmp);
665 if (lh_num_items(thread_hash) == 0)
666 {
667 /* make sure we don't leak memory */
668 lh_free(thread_hash);
669 thread_hash = NULL;
670 }
671 }
672 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
673
674 if (p != NULL) ERR_STATE_free(p);
675 } 929 }
676 930
677ERR_STATE *ERR_get_state(void) 931ERR_STATE *ERR_get_state(void)
678 { 932 {
679 static ERR_STATE fallback; 933 static ERR_STATE fallback;
680 ERR_STATE *ret=NULL,tmp,*tmpp=NULL; 934 ERR_STATE *ret,tmp,*tmpp=NULL;
681 int thread_state_exists;
682 int i; 935 int i;
683 unsigned long pid; 936 unsigned long pid;
684 937
938 err_fns_check();
685 pid=(unsigned long)CRYPTO_thread_id(); 939 pid=(unsigned long)CRYPTO_thread_id();
686 940 tmp.pid=pid;
687 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 941 ret=ERRFN(thread_get_item)(&tmp);
688 if (thread_hash != NULL)
689 {
690 tmp.pid=pid;
691 ret=(ERR_STATE *)lh_retrieve(thread_hash,&tmp);
692 }
693 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
694 942
695 /* ret == the error state, if NULL, make a new one */ 943 /* ret == the error state, if NULL, make a new one */
696 if (ret == NULL) 944 if (ret == NULL)
@@ -705,42 +953,25 @@ ERR_STATE *ERR_get_state(void)
705 ret->err_data[i]=NULL; 953 ret->err_data[i]=NULL;
706 ret->err_data_flags[i]=0; 954 ret->err_data_flags[i]=0;
707 } 955 }
708 956 tmpp = ERRFN(thread_set_item)(ret);
709 CRYPTO_w_lock(CRYPTO_LOCK_ERR); 957 /* To check if insertion failed, do a get. */
710 958 if (ERRFN(thread_get_item)(ret) != ret)
711 /* no entry yet in thread_hash for current thread -
712 * thus, it may have changed since we last looked at it */
713 if (thread_hash == NULL)
714 thread_hash = lh_new(pid_hash, pid_cmp);
715 if (thread_hash == NULL)
716 thread_state_exists = 0; /* allocation error */
717 else
718 {
719 tmpp=(ERR_STATE *)lh_insert(thread_hash,ret);
720 thread_state_exists = 1;
721 }
722
723 CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
724
725 if (!thread_state_exists)
726 { 959 {
727 ERR_STATE_free(ret); /* could not insert it */ 960 ERR_STATE_free(ret); /* could not insert it */
728 return(&fallback); 961 return(&fallback);
729 } 962 }
730 963 /* If a race occured in this function and we came second, tmpp
731 if (tmpp != NULL) /* old entry - should not happen */ 964 * is the first one that we just replaced. */
732 { 965 if (tmpp)
733 ERR_STATE_free(tmpp); 966 ERR_STATE_free(tmpp);
734 }
735 } 967 }
736 return(ret); 968 return ret;
737 } 969 }
738 970
739int ERR_get_next_error_library(void) 971int ERR_get_next_error_library(void)
740 { 972 {
741 static int value=ERR_LIB_USER; 973 err_fns_check();
742 974 return ERRFN(get_next_lib)();
743 return(value++);
744 } 975 }
745 976
746void ERR_set_error_data(char *data, int flags) 977void ERR_set_error_data(char *data, int flags)
@@ -786,7 +1017,7 @@ void ERR_add_error_data(int num, ...)
786 if (p == NULL) 1017 if (p == NULL)
787 { 1018 {
788 OPENSSL_free(str); 1019 OPENSSL_free(str);
789 return; 1020 goto err;
790 } 1021 }
791 else 1022 else
792 str=p; 1023 str=p;
@@ -796,6 +1027,6 @@ void ERR_add_error_data(int num, ...)
796 } 1027 }
797 ERR_set_error_data(str,ERR_TXT_MALLOCED|ERR_TXT_STRING); 1028 ERR_set_error_data(str,ERR_TXT_MALLOCED|ERR_TXT_STRING);
798 1029
1030err:
799 va_end(args); 1031 va_end(args);
800 } 1032 }
801