Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/err/err.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <stdarg.h>
12
#include <string.h>
13
#include "crypto/cryptlib.h"
14
#include "internal/err.h"
15
#include "crypto/err.h"
16
#include <openssl/err.h>
17
#include <openssl/crypto.h>
18
#include <openssl/buffer.h>
19
#include <openssl/bio.h>
20
#include <openssl/opensslconf.h>
21
#include "internal/thread_once.h"
22
#include "crypto/ctype.h"
23
#include "internal/constant_time.h"
24
#include "e_os.h"
25
26
#ifndef OPENSSL_NO_ERR
27
static int err_load_strings(const ERR_STRING_DATA *str);
28
#endif
29
30
static void ERR_STATE_free(ERR_STATE *s);
31
#ifndef OPENSSL_NO_ERR
32
static ERR_STRING_DATA ERR_str_libraries[] = {
33
    {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
34
    {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
35
    {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
36
    {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
37
    {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
38
    {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
39
    {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
40
    {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
41
    {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
42
    {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
43
    {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
44
    {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
45
    {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
46
    {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
47
    {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
48
    {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
49
    {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
50
    {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
51
    {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
52
    {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
53
    {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
54
    {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
55
    {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
56
    {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
57
    {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
58
    {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
59
    {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
60
    {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
61
    {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
62
    {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
63
    {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
64
    {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
65
    {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
66
    {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
67
    {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
68
    {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
69
    {0, NULL},
70
};
71
72
static ERR_STRING_DATA ERR_str_functs[] = {
73
    {ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"},
74
    {ERR_PACK(0, SYS_F_CONNECT, 0), "connect"},
75
    {ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"},
76
    {ERR_PACK(0, SYS_F_SOCKET, 0), "socket"},
77
    {ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"},
78
    {ERR_PACK(0, SYS_F_BIND, 0), "bind"},
79
    {ERR_PACK(0, SYS_F_LISTEN, 0), "listen"},
80
    {ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"},
81
#ifdef OPENSSL_SYS_WINDOWS
82
    {ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"},
83
#endif
84
    {ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"},
85
    {ERR_PACK(0, SYS_F_FREAD, 0), "fread"},
86
    {ERR_PACK(0, SYS_F_GETADDRINFO, 0), "getaddrinfo"},
87
    {ERR_PACK(0, SYS_F_GETNAMEINFO, 0), "getnameinfo"},
88
    {ERR_PACK(0, SYS_F_SETSOCKOPT, 0), "setsockopt"},
89
    {ERR_PACK(0, SYS_F_GETSOCKOPT, 0), "getsockopt"},
90
    {ERR_PACK(0, SYS_F_GETSOCKNAME, 0), "getsockname"},
91
    {ERR_PACK(0, SYS_F_GETHOSTBYNAME, 0), "gethostbyname"},
92
    {ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"},
93
    {ERR_PACK(0, SYS_F_OPEN, 0), "open"},
94
    {ERR_PACK(0, SYS_F_CLOSE, 0), "close"},
95
    {ERR_PACK(0, SYS_F_IOCTL, 0), "ioctl"},
96
    {ERR_PACK(0, SYS_F_STAT, 0), "stat"},
97
    {ERR_PACK(0, SYS_F_FCNTL, 0), "fcntl"},
98
    {ERR_PACK(0, SYS_F_FSTAT, 0), "fstat"},
99
    {0, NULL},
100
};
101
102
static ERR_STRING_DATA ERR_str_reasons[] = {
103
    {ERR_R_SYS_LIB, "system lib"},
104
    {ERR_R_BN_LIB, "BN lib"},
105
    {ERR_R_RSA_LIB, "RSA lib"},
106
    {ERR_R_DH_LIB, "DH lib"},
107
    {ERR_R_EVP_LIB, "EVP lib"},
108
    {ERR_R_BUF_LIB, "BUF lib"},
109
    {ERR_R_OBJ_LIB, "OBJ lib"},
110
    {ERR_R_PEM_LIB, "PEM lib"},
111
    {ERR_R_DSA_LIB, "DSA lib"},
112
    {ERR_R_X509_LIB, "X509 lib"},
113
    {ERR_R_ASN1_LIB, "ASN1 lib"},
114
    {ERR_R_EC_LIB, "EC lib"},
115
    {ERR_R_BIO_LIB, "BIO lib"},
116
    {ERR_R_PKCS7_LIB, "PKCS7 lib"},
117
    {ERR_R_X509V3_LIB, "X509V3 lib"},
118
    {ERR_R_ENGINE_LIB, "ENGINE lib"},
119
    {ERR_R_UI_LIB, "UI lib"},
120
    {ERR_R_OSSL_STORE_LIB, "STORE lib"},
121
    {ERR_R_ECDSA_LIB, "ECDSA lib"},
122
123
    {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
124
    {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
125
126
    {ERR_R_FATAL, "fatal"},
127
    {ERR_R_MALLOC_FAILURE, "malloc failure"},
128
    {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
129
     "called a function you should not call"},
130
    {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
131
    {ERR_R_INTERNAL_ERROR, "internal error"},
132
    {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
133
    {ERR_R_INIT_FAIL, "init fail"},
134
    {ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument"},
135
    {ERR_R_OPERATION_FAIL, "operation fail"},
136
137
    {0, NULL},
138
};
139
#endif
140
141
static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
142
static int set_err_thread_local;
143
static CRYPTO_THREAD_LOCAL err_thread_local;
144
145
static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
146
static CRYPTO_RWLOCK *err_string_lock = NULL;
147
148
#ifndef OPENSSL_NO_ERR
149
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
150
#endif
151
152
/*
153
 * The internal state
154
 */
155
156
#ifndef OPENSSL_NO_ERR
157
static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
158
#endif
159
static int int_err_library_number = ERR_LIB_USER;
160
161
static unsigned long get_error_values(int inc, int top, const char **file,
162
                                      int *line, const char **data,
163
                                      int *flags);
164
165
#ifndef OPENSSL_NO_ERR
166
static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
167
15.2k
{
168
15.2k
    unsigned long ret, l;
169
170
15.2k
    l = a->error;
171
15.2k
    ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
172
15.2k
    return (ret ^ ret % 19 * 13);
173
15.2k
}
174
175
static int err_string_data_cmp(const ERR_STRING_DATA *a,
176
                               const ERR_STRING_DATA *b)
177
10.5k
{
178
10.5k
    if (a->error == b->error)
179
10.3k
        return 0;
180
246
    return a->error > b->error ? 1 : -1;
181
10.5k
}
182
183
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
184
58
{
185
58
    ERR_STRING_DATA *p = NULL;
186
187
58
    CRYPTO_THREAD_read_lock(err_string_lock);
188
58
    p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
189
58
    CRYPTO_THREAD_unlock(err_string_lock);
190
191
58
    return p;
192
58
}
193
194
/* 2019-05-21: Russian and Ukrainian locales on Linux require more than 6,5 kB */
195
# define SPACE_SYS_STR_REASONS 8 * 1024
196
256
# define NUM_SYS_STR_REASONS 127
197
198
static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
199
/*
200
 * SYS_str_reasons is filled with copies of strerror() results at
201
 * initialization. 'errno' values up to 127 should cover all usual errors,
202
 * others will be displayed numerically by ERR_error_string. It is crucial
203
 * that we have something for each reason code that occurs in
204
 * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
205
 * which always gets an errno value and never one of those 'standard' reason
206
 * codes.
207
 */
208
209
static void build_SYS_str_reasons(void)
210
118
{
211
    /* OPENSSL_malloc cannot be used here, use static storage instead */
212
118
    static char strerror_pool[SPACE_SYS_STR_REASONS];
213
118
    char *cur = strerror_pool;
214
118
    size_t cnt = 0;
215
118
    static int init = 1;
216
118
    int i;
217
118
    int saveerrno = get_last_sys_error();
218
219
118
    CRYPTO_THREAD_write_lock(err_string_lock);
220
118
    if (!init) {
221
116
        CRYPTO_THREAD_unlock(err_string_lock);
222
116
        return;
223
116
    }
224
225
256
    for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
226
254
        ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
227
228
254
        str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
229
        /*
230
         * If we have used up all the space in strerror_pool,
231
         * there's no point in calling openssl_strerror_r()
232
         */
233
254
        if (str->string == NULL && cnt < sizeof(strerror_pool)) {
234
254
            if (openssl_strerror_r(i, cur, sizeof(strerror_pool) - cnt)) {
235
250
                size_t l = strlen(cur);
236
237
250
                str->string = cur;
238
250
                cnt += l;
239
250
                cur += l;
240
241
                /*
242
                 * VMS has an unusual quirk of adding spaces at the end of
243
                 * some (most? all?) messages. Lets trim them off.
244
                 */
245
250
                while (cur > strerror_pool && ossl_isspace(cur[-1])) {
246
0
                    cur--;
247
0
                    cnt--;
248
0
                }
249
250
                *cur++ = '\0';
250
250
                cnt++;
251
250
            }
252
254
        }
253
254
        if (str->string == NULL)
254
4
            str->string = "unknown";
255
254
    }
256
257
    /*
258
     * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
259
     * required by ERR_load_strings.
260
     */
261
262
2
    init = 0;
263
264
2
    CRYPTO_THREAD_unlock(err_string_lock);
265
    /* openssl_strerror_r could change errno, but we want to preserve it */
266
2
    set_sys_error(saveerrno);
267
2
    err_load_strings(SYS_str_reasons);
268
2
}
269
#endif
270
271
#define err_clear_data(p, i) \
272
53.2k
        do { \
273
53.2k
            if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
274
0
                OPENSSL_free((p)->err_data[i]); \
275
0
                (p)->err_data[i] = NULL; \
276
0
            } \
277
53.2k
            (p)->err_data_flags[i] = 0; \
278
53.2k
        } while (0)
279
280
#define err_clear(p, i) \
281
53.2k
        do { \
282
53.2k
            err_clear_data(p, i); \
283
53.2k
            (p)->err_flags[i] = 0; \
284
53.2k
            (p)->err_buffer[i] = 0; \
285
53.2k
            (p)->err_file[i] = NULL; \
286
53.2k
            (p)->err_line[i] = -1; \
287
53.2k
        } while (0)
288
289
static void ERR_STATE_free(ERR_STATE *s)
290
2
{
291
2
    int i;
292
293
2
    if (s == NULL)
294
0
        return;
295
34
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
296
32
        err_clear_data(s, i);
297
32
    }
298
2
    OPENSSL_free(s);
299
2
}
300
301
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
302
2
{
303
2
    if (!OPENSSL_init_crypto(0, NULL))
304
0
        return 0;
305
2
    err_string_lock = CRYPTO_THREAD_lock_new();
306
2
    if (err_string_lock == NULL)
307
0
        return 0;
308
2
#ifndef OPENSSL_NO_ERR
309
2
    int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
310
2
                                            err_string_data_cmp);
311
2
    if (int_error_hash == NULL) {
312
0
        CRYPTO_THREAD_lock_free(err_string_lock);
313
0
        err_string_lock = NULL;
314
0
        return 0;
315
0
    }
316
2
#endif
317
2
    return 1;
318
2
}
319
320
void err_cleanup(void)
321
2
{
322
2
    if (set_err_thread_local != 0)
323
2
        CRYPTO_THREAD_cleanup_local(&err_thread_local);
324
2
    CRYPTO_THREAD_lock_free(err_string_lock);
325
2
    err_string_lock = NULL;
326
2
#ifndef OPENSSL_NO_ERR
327
2
    lh_ERR_STRING_DATA_free(int_error_hash);
328
2
    int_error_hash = NULL;
329
2
#endif
330
2
}
331
332
#ifndef OPENSSL_NO_ERR
333
/*
334
 * Legacy; pack in the library.
335
 */
336
static void err_patch(int lib, ERR_STRING_DATA *str)
337
118
{
338
118
    unsigned long plib = ERR_PACK(lib, 0, 0);
339
340
2.83k
    for (; str->error != 0; str++)
341
2.71k
        str->error |= plib;
342
118
}
343
344
/*
345
 * Hash in |str| error strings. Assumes the URN_ONCE was done.
346
 */
347
static int err_load_strings(const ERR_STRING_DATA *str)
348
472
{
349
472
    CRYPTO_THREAD_write_lock(err_string_lock);
350
15.6k
    for (; str->error; str++)
351
15.1k
        (void)lh_ERR_STRING_DATA_insert(int_error_hash,
352
15.1k
                                       (ERR_STRING_DATA *)str);
353
472
    CRYPTO_THREAD_unlock(err_string_lock);
354
472
    return 1;
355
472
}
356
#endif
357
358
int ERR_load_ERR_strings(void)
359
118
{
360
118
#ifndef OPENSSL_NO_ERR
361
118
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
362
0
        return 0;
363
364
118
    err_load_strings(ERR_str_libraries);
365
118
    err_load_strings(ERR_str_reasons);
366
118
    err_patch(ERR_LIB_SYS, ERR_str_functs);
367
118
    err_load_strings(ERR_str_functs);
368
118
    build_SYS_str_reasons();
369
118
#endif
370
118
    return 1;
371
118
}
372
373
int ERR_load_strings(int lib, ERR_STRING_DATA *str)
374
0
{
375
0
#ifndef OPENSSL_NO_ERR
376
0
    if (ERR_load_ERR_strings() == 0)
377
0
        return 0;
378
379
0
    err_patch(lib, str);
380
0
    err_load_strings(str);
381
0
#endif
382
383
0
    return 1;
384
0
}
385
386
int ERR_load_strings_const(const ERR_STRING_DATA *str)
387
116
{
388
116
#ifndef OPENSSL_NO_ERR
389
116
    if (ERR_load_ERR_strings() == 0)
390
0
        return 0;
391
116
    err_load_strings(str);
392
116
#endif
393
394
116
    return 1;
395
116
}
396
397
int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
398
0
{
399
0
#ifndef OPENSSL_NO_ERR
400
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
401
0
        return 0;
402
403
0
    CRYPTO_THREAD_write_lock(err_string_lock);
404
    /*
405
     * We don't need to ERR_PACK the lib, since that was done (to
406
     * the table) when it was loaded.
407
     */
408
0
    for (; str->error; str++)
409
0
        (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
410
0
    CRYPTO_THREAD_unlock(err_string_lock);
411
0
#endif
412
413
0
    return 1;
414
0
}
415
416
void err_free_strings_int(void)
417
0
{
418
    /* obsolete */
419
0
}
420
421
/********************************************************/
422
423
void ERR_put_error(int lib, int func, int reason, const char *file, int line)
424
0
{
425
0
    ERR_STATE *es;
426
427
#ifdef _OSD_POSIX
428
    /*
429
     * In the BS2000-OSD POSIX subsystem, the compiler generates path names
430
     * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
431
     * something sensible. @@@ We shouldn't modify a const string, though.
432
     */
433
    if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
434
        char *end;
435
436
        /* Skip the "*POSIX(" prefix */
437
        file += sizeof("*POSIX(") - 1;
438
        end = &file[strlen(file) - 1];
439
        if (*end == ')')
440
            *end = '\0';
441
        /* Optional: use the basename of the path only. */
442
        if ((end = strrchr(file, '/')) != NULL)
443
            file = &end[1];
444
    }
445
#endif
446
0
    es = ERR_get_state();
447
0
    if (es == NULL)
448
0
        return;
449
450
0
    es->top = (es->top + 1) % ERR_NUM_ERRORS;
451
0
    if (es->top == es->bottom)
452
0
        es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
453
0
    es->err_flags[es->top] = 0;
454
0
    es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
455
0
    es->err_file[es->top] = file;
456
0
    es->err_line[es->top] = line;
457
0
    err_clear_data(es, es->top);
458
0
}
459
460
void ERR_clear_error(void)
461
3.32k
{
462
3.32k
    int i;
463
3.32k
    ERR_STATE *es;
464
465
3.32k
    es = ERR_get_state();
466
3.32k
    if (es == NULL)
467
0
        return;
468
469
56.5k
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
470
53.2k
        err_clear(es, i);
471
53.2k
    }
472
3.32k
    es->top = es->bottom = 0;
473
3.32k
}
474
475
unsigned long ERR_get_error(void)
476
0
{
477
0
    return get_error_values(1, 0, NULL, NULL, NULL, NULL);
478
0
}
479
480
unsigned long ERR_get_error_line(const char **file, int *line)
481
0
{
482
0
    return get_error_values(1, 0, file, line, NULL, NULL);
483
0
}
484
485
unsigned long ERR_get_error_line_data(const char **file, int *line,
486
                                      const char **data, int *flags)
487
0
{
488
0
    return get_error_values(1, 0, file, line, data, flags);
489
0
}
490
491
unsigned long ERR_peek_error(void)
492
0
{
493
0
    return get_error_values(0, 0, NULL, NULL, NULL, NULL);
494
0
}
495
496
unsigned long ERR_peek_error_line(const char **file, int *line)
497
0
{
498
0
    return get_error_values(0, 0, file, line, NULL, NULL);
499
0
}
500
501
unsigned long ERR_peek_error_line_data(const char **file, int *line,
502
                                       const char **data, int *flags)
503
0
{
504
0
    return get_error_values(0, 0, file, line, data, flags);
505
0
}
506
507
unsigned long ERR_peek_last_error(void)
508
0
{
509
0
    return get_error_values(0, 1, NULL, NULL, NULL, NULL);
510
0
}
511
512
unsigned long ERR_peek_last_error_line(const char **file, int *line)
513
0
{
514
0
    return get_error_values(0, 1, file, line, NULL, NULL);
515
0
}
516
517
unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
518
                                            const char **data, int *flags)
519
0
{
520
0
    return get_error_values(0, 1, file, line, data, flags);
521
0
}
522
523
static unsigned long get_error_values(int inc, int top, const char **file,
524
                                      int *line, const char **data,
525
                                      int *flags)
526
0
{
527
0
    int i = 0;
528
0
    ERR_STATE *es;
529
0
    unsigned long ret;
530
531
0
    es = ERR_get_state();
532
0
    if (es == NULL)
533
0
        return 0;
534
535
0
    if (inc && top) {
536
0
        if (file)
537
0
            *file = "";
538
0
        if (line)
539
0
            *line = 0;
540
0
        if (data)
541
0
            *data = "";
542
0
        if (flags)
543
0
            *flags = 0;
544
545
0
        return ERR_R_INTERNAL_ERROR;
546
0
    }
547
548
0
    while (es->bottom != es->top) {
549
0
        if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
550
0
            err_clear(es, es->top);
551
0
            es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
552
0
            continue;
553
0
        }
554
0
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
555
0
        if (es->err_flags[i] & ERR_FLAG_CLEAR) {
556
0
            es->bottom = i;
557
0
            err_clear(es, es->bottom);
558
0
            continue;
559
0
        }
560
0
        break;
561
0
    }
562
563
0
    if (es->bottom == es->top)
564
0
        return 0;
565
566
0
    if (top)
567
0
        i = es->top;            /* last error */
568
0
    else
569
0
        i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
570
571
0
    ret = es->err_buffer[i];
572
0
    if (inc) {
573
0
        es->bottom = i;
574
0
        es->err_buffer[i] = 0;
575
0
    }
576
577
0
    if (file != NULL && line != NULL) {
578
0
        if (es->err_file[i] == NULL) {
579
0
            *file = "NA";
580
0
            *line = 0;
581
0
        } else {
582
0
            *file = es->err_file[i];
583
0
            *line = es->err_line[i];
584
0
        }
585
0
    }
586
587
0
    if (data == NULL) {
588
0
        if (inc) {
589
0
            err_clear_data(es, i);
590
0
        }
591
0
    } else {
592
0
        if (es->err_data[i] == NULL) {
593
0
            *data = "";
594
0
            if (flags != NULL)
595
0
                *flags = 0;
596
0
        } else {
597
0
            *data = es->err_data[i];
598
0
            if (flags != NULL)
599
0
                *flags = es->err_data_flags[i];
600
0
        }
601
0
    }
602
0
    return ret;
603
0
}
604
605
void ERR_error_string_n(unsigned long e, char *buf, size_t len)
606
0
{
607
0
    char lsbuf[64], fsbuf[64], rsbuf[64];
608
0
    const char *ls, *fs, *rs;
609
0
    unsigned long l, f, r;
610
611
0
    if (len == 0)
612
0
        return;
613
614
0
    l = ERR_GET_LIB(e);
615
0
    ls = ERR_lib_error_string(e);
616
0
    if (ls == NULL) {
617
0
        BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
618
0
        ls = lsbuf;
619
0
    }
620
621
0
    fs = ERR_func_error_string(e);
622
0
    f = ERR_GET_FUNC(e);
623
0
    if (fs == NULL) {
624
0
        BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
625
0
        fs = fsbuf;
626
0
    }
627
628
0
    rs = ERR_reason_error_string(e);
629
0
    r = ERR_GET_REASON(e);
630
0
    if (rs == NULL) {
631
0
        BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
632
0
        rs = rsbuf;
633
0
    }
634
635
0
    BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
636
0
    if (strlen(buf) == len - 1) {
637
        /* Didn't fit; use a minimal format. */
638
0
        BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
639
0
    }
640
0
}
641
642
/*
643
 * ERR_error_string_n should be used instead for ret != NULL as
644
 * ERR_error_string cannot know how large the buffer is
645
 */
646
char *ERR_error_string(unsigned long e, char *ret)
647
0
{
648
0
    static char buf[256];
649
650
0
    if (ret == NULL)
651
0
        ret = buf;
652
0
    ERR_error_string_n(e, ret, (int)sizeof(buf));
653
0
    return ret;
654
0
}
655
656
const char *ERR_lib_error_string(unsigned long e)
657
0
{
658
0
#ifndef OPENSSL_NO_ERR
659
0
    ERR_STRING_DATA d, *p;
660
0
    unsigned long l;
661
662
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
663
0
        return NULL;
664
0
    }
665
666
0
    l = ERR_GET_LIB(e);
667
0
    d.error = ERR_PACK(l, 0, 0);
668
0
    p = int_err_get_item(&d);
669
0
    return ((p == NULL) ? NULL : p->string);
670
#else
671
    return NULL;
672
#endif
673
0
}
674
675
const char *ERR_func_error_string(unsigned long e)
676
58
{
677
58
#ifndef OPENSSL_NO_ERR
678
58
    ERR_STRING_DATA d, *p;
679
58
    unsigned long l, f;
680
681
58
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
682
0
        return NULL;
683
0
    }
684
685
58
    l = ERR_GET_LIB(e);
686
58
    f = ERR_GET_FUNC(e);
687
58
    d.error = ERR_PACK(l, f, 0);
688
58
    p = int_err_get_item(&d);
689
58
    return ((p == NULL) ? NULL : p->string);
690
#else
691
    return NULL;
692
#endif
693
58
}
694
695
const char *ERR_reason_error_string(unsigned long e)
696
0
{
697
0
#ifndef OPENSSL_NO_ERR
698
0
    ERR_STRING_DATA d, *p = NULL;
699
0
    unsigned long l, r;
700
701
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
702
0
        return NULL;
703
0
    }
704
705
0
    l = ERR_GET_LIB(e);
706
0
    r = ERR_GET_REASON(e);
707
0
    d.error = ERR_PACK(l, 0, r);
708
0
    p = int_err_get_item(&d);
709
0
    if (!p) {
710
0
        d.error = ERR_PACK(0, 0, r);
711
0
        p = int_err_get_item(&d);
712
0
    }
713
0
    return ((p == NULL) ? NULL : p->string);
714
#else
715
    return NULL;
716
#endif
717
0
}
718
719
void err_delete_thread_state(void)
720
2
{
721
2
    ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
722
2
    if (state == NULL)
723
0
        return;
724
725
2
    CRYPTO_THREAD_set_local(&err_thread_local, NULL);
726
2
    ERR_STATE_free(state);
727
2
}
728
729
#if OPENSSL_API_COMPAT < 0x10100000L
730
void ERR_remove_thread_state(void *dummy)
731
0
{
732
0
}
733
#endif
734
735
#if OPENSSL_API_COMPAT < 0x10000000L
736
void ERR_remove_state(unsigned long pid)
737
0
{
738
0
}
739
#endif
740
741
DEFINE_RUN_ONCE_STATIC(err_do_init)
742
2
{
743
2
    set_err_thread_local = 1;
744
2
    return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
745
2
}
746
747
ERR_STATE *ERR_get_state(void)
748
3.32k
{
749
3.32k
    ERR_STATE *state;
750
3.32k
    int saveerrno = get_last_sys_error();
751
752
3.32k
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
753
0
        return NULL;
754
755
3.32k
    if (!RUN_ONCE(&err_init, err_do_init))
756
0
        return NULL;
757
758
3.32k
    state = CRYPTO_THREAD_get_local(&err_thread_local);
759
3.32k
    if (state == (ERR_STATE*)-1)
760
0
        return NULL;
761
762
3.32k
    if (state == NULL) {
763
2
        if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
764
0
            return NULL;
765
766
2
        if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
767
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
768
0
            return NULL;
769
0
        }
770
771
2
        if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)
772
2
                || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
773
0
            ERR_STATE_free(state);
774
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
775
0
            return NULL;
776
0
        }
777
778
        /* Ignore failures from these */
779
2
        OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
780
2
    }
781
782
3.32k
    set_sys_error(saveerrno);
783
3.32k
    return state;
784
3.32k
}
785
786
/*
787
 * err_shelve_state returns the current thread local error state
788
 * and freezes the error module until err_unshelve_state is called.
789
 */
790
int err_shelve_state(void **state)
791
0
{
792
0
    int saveerrno = get_last_sys_error();
793
794
    /*
795
     * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
796
     * via ossl_init_load_crypto_nodelete(), by which point the requested
797
     * "base" initialization has already been performed, so the below call is a
798
     * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
799
     *
800
     * If are no other valid callers of this function, the call below can be
801
     * removed, avoiding the re-entry into OPENSSL_init_crypto().  If there are
802
     * potential uses that are not from inside OPENSSL_init_crypto(), then this
803
     * call is needed, but some care is required to make sure that the re-entry
804
     * remains a NOOP.
805
     */
806
0
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
807
0
        return 0;
808
809
0
    if (!RUN_ONCE(&err_init, err_do_init))
810
0
        return 0;
811
812
0
    *state = CRYPTO_THREAD_get_local(&err_thread_local);
813
0
    if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
814
0
        return 0;
815
816
0
    set_sys_error(saveerrno);
817
0
    return 1;
818
0
}
819
820
/*
821
 * err_unshelve_state restores the error state that was returned
822
 * by err_shelve_state previously.
823
 */
824
void err_unshelve_state(void* state)
825
0
{
826
0
    if (state != (void*)-1)
827
0
        CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
828
0
}
829
830
int ERR_get_next_error_library(void)
831
0
{
832
0
    int ret;
833
834
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
835
0
        return 0;
836
837
0
    CRYPTO_THREAD_write_lock(err_string_lock);
838
0
    ret = int_err_library_number++;
839
0
    CRYPTO_THREAD_unlock(err_string_lock);
840
0
    return ret;
841
0
}
842
843
static int err_set_error_data_int(char *data, int flags)
844
0
{
845
0
    ERR_STATE *es;
846
0
    int i;
847
848
0
    es = ERR_get_state();
849
0
    if (es == NULL)
850
0
        return 0;
851
852
0
    i = es->top;
853
854
0
    err_clear_data(es, i);
855
0
    es->err_data[i] = data;
856
0
    es->err_data_flags[i] = flags;
857
858
0
    return 1;
859
0
}
860
861
void ERR_set_error_data(char *data, int flags)
862
0
{
863
    /*
864
     * This function is void so we cannot propagate the error return. Since it
865
     * is also in the public API we can't change the return type.
866
     */
867
0
    err_set_error_data_int(data, flags);
868
0
}
869
870
void ERR_add_error_data(int num, ...)
871
0
{
872
0
    va_list args;
873
0
    va_start(args, num);
874
0
    ERR_add_error_vdata(num, args);
875
0
    va_end(args);
876
0
}
877
878
void ERR_add_error_vdata(int num, va_list args)
879
0
{
880
0
    int i, n, s;
881
0
    char *str, *p, *a;
882
883
0
    s = 80;
884
0
    if ((str = OPENSSL_malloc(s + 1)) == NULL) {
885
        /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
886
0
        return;
887
0
    }
888
0
    str[0] = '\0';
889
890
0
    n = 0;
891
0
    for (i = 0; i < num; i++) {
892
0
        a = va_arg(args, char *);
893
0
        if (a == NULL)
894
0
            a = "<NULL>";
895
0
        n += strlen(a);
896
0
        if (n > s) {
897
0
            s = n + 20;
898
0
            p = OPENSSL_realloc(str, s + 1);
899
0
            if (p == NULL) {
900
0
                OPENSSL_free(str);
901
0
                return;
902
0
            }
903
0
            str = p;
904
0
        }
905
0
        OPENSSL_strlcat(str, a, (size_t)s + 1);
906
0
    }
907
0
    if (!err_set_error_data_int(str, ERR_TXT_MALLOCED | ERR_TXT_STRING))
908
0
        OPENSSL_free(str);
909
0
}
910
911
int ERR_set_mark(void)
912
0
{
913
0
    ERR_STATE *es;
914
915
0
    es = ERR_get_state();
916
0
    if (es == NULL)
917
0
        return 0;
918
919
0
    if (es->bottom == es->top)
920
0
        return 0;
921
0
    es->err_flags[es->top] |= ERR_FLAG_MARK;
922
0
    return 1;
923
0
}
924
925
int ERR_pop_to_mark(void)
926
0
{
927
0
    ERR_STATE *es;
928
929
0
    es = ERR_get_state();
930
0
    if (es == NULL)
931
0
        return 0;
932
933
0
    while (es->bottom != es->top
934
0
           && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
935
0
        err_clear(es, es->top);
936
0
        es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
937
0
    }
938
939
0
    if (es->bottom == es->top)
940
0
        return 0;
941
0
    es->err_flags[es->top] &= ~ERR_FLAG_MARK;
942
0
    return 1;
943
0
}
944
945
int ERR_clear_last_mark(void)
946
0
{
947
0
    ERR_STATE *es;
948
0
    int top;
949
950
0
    es = ERR_get_state();
951
0
    if (es == NULL)
952
0
        return 0;
953
954
0
    top = es->top;
955
0
    while (es->bottom != top
956
0
           && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
957
0
        top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
958
0
    }
959
960
0
    if (es->bottom == top)
961
0
        return 0;
962
0
    es->err_flags[top] &= ~ERR_FLAG_MARK;
963
0
    return 1;
964
0
}
965
966
void err_clear_last_constant_time(int clear)
967
0
{
968
0
    ERR_STATE *es;
969
0
    int top;
970
971
0
    es = ERR_get_state();
972
0
    if (es == NULL)
973
0
        return;
974
975
0
    top = es->top;
976
977
    /*
978
     * Flag error as cleared but remove it elsewhere to avoid two errors
979
     * accessing the same error stack location, revealing timing information.
980
     */
981
0
    clear = constant_time_select_int(constant_time_eq_int(clear, 0),
982
0
                                     0, ERR_FLAG_CLEAR);
983
0
    es->err_flags[top] |= clear;
984
0
}