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