Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/err/err.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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
#define OSSL_FORCE_ERR_STATE
11
12
#include <stdio.h>
13
#include <stdarg.h>
14
#include <string.h>
15
#include "crypto/cryptlib.h"
16
#include "internal/err.h"
17
#include "crypto/err.h"
18
#include <openssl/err.h>
19
#include <openssl/crypto.h>
20
#include <openssl/buffer.h>
21
#include <openssl/bio.h>
22
#include <openssl/opensslconf.h>
23
#include "internal/thread_once.h"
24
#include "crypto/ctype.h"
25
#include "internal/constant_time.h"
26
#include "internal/e_os.h"
27
#include "err_local.h"
28
29
/* Forward declaration in case it's not published because of configuration */
30
ERR_STATE *ERR_get_state(void);
31
32
#ifndef OPENSSL_NO_ERR
33
static int err_load_strings(const ERR_STRING_DATA *str);
34
#endif
35
36
#ifndef OPENSSL_NO_ERR
37
static ERR_STRING_DATA ERR_str_libraries[] = {
38
    { ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library" },
39
    { ERR_PACK(ERR_LIB_SYS, 0, 0), "system library" },
40
    { ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines" },
41
    { ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines" },
42
    { ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines" },
43
    { ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines" },
44
    { ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines" },
45
    { ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines" },
46
    { ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines" },
47
    { ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines" },
48
    { ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines" },
49
    { ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines" },
50
    { ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines" },
51
    { ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines" },
52
    { ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines" },
53
    { ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines" },
54
    { ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines" },
55
    { ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines" },
56
    { ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines" },
57
    { ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines" },
58
    { ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines" },
59
    { ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines" },
60
    { ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator" },
61
    { ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines" },
62
    { ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines" },
63
    { ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines" },
64
    { ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines" },
65
    { ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines" },
66
    { ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines" },
67
    { ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines" },
68
    { ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines" },
69
    { ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines" },
70
    { ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines" },
71
    { ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines" },
72
    { ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines" },
73
    { ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines" },
74
    { ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines" },
75
    { ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines" },
76
    { ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines" },
77
    { ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines" },
78
    { ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines" },
79
    { ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines" },
80
    { ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines" },
81
    { 0, NULL },
82
};
83
84
/*
85
 * Should make sure that all ERR_R_ reasons defined in include/openssl/err.h.in
86
 * are listed.  For maintainability, please keep all reasons in the same order.
87
 */
88
static ERR_STRING_DATA ERR_str_reasons[] = {
89
    { ERR_R_SYS_LIB, "system lib" },
90
    { ERR_R_BN_LIB, "BN lib" },
91
    { ERR_R_RSA_LIB, "RSA lib" },
92
    { ERR_R_DH_LIB, "DH lib" },
93
    { ERR_R_EVP_LIB, "EVP lib" },
94
    { ERR_R_BUF_LIB, "BUF lib" },
95
    { ERR_R_OBJ_LIB, "OBJ lib" },
96
    { ERR_R_PEM_LIB, "PEM lib" },
97
    { ERR_R_DSA_LIB, "DSA lib" },
98
    { ERR_R_X509_LIB, "X509 lib" },
99
    { ERR_R_ASN1_LIB, "ASN1 lib" },
100
    { ERR_R_CRYPTO_LIB, "CRYPTO lib" },
101
    { ERR_R_EC_LIB, "EC lib" },
102
    { ERR_R_BIO_LIB, "BIO lib" },
103
    { ERR_R_PKCS7_LIB, "PKCS7 lib" },
104
    { ERR_R_X509V3_LIB, "X509V3 lib" },
105
    { ERR_R_ENGINE_LIB, "ENGINE lib" },
106
    { ERR_R_UI_LIB, "UI lib" },
107
    { ERR_R_ECDSA_LIB, "ECDSA lib" },
108
    { ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib" },
109
    { ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib" },
110
111
    { ERR_R_FATAL, "fatal" },
112
    { ERR_R_MALLOC_FAILURE, "malloc failure" },
113
    { ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
114
        "called a function you should not call" },
115
    { ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter" },
116
    { ERR_R_INTERNAL_ERROR, "internal error" },
117
    { ERR_R_DISABLED, "called a function that was disabled at compile-time" },
118
    { ERR_R_INIT_FAIL, "init fail" },
119
    { ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument" },
120
    { ERR_R_OPERATION_FAIL, "operation fail" },
121
    { ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions" },
122
    { ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled" },
123
    { ERR_R_NESTED_ASN1_ERROR, "nested asn1 error" },
124
    { ERR_R_MISSING_ASN1_EOS, "missing asn1 eos" },
125
    /*
126
     * Something is unsupported, exactly what is expressed with additional data
127
     */
128
    { ERR_R_UNSUPPORTED, "unsupported" },
129
    /*
130
     * A fetch failed for other reasons than the name to be fetched being
131
     * unsupported.
132
     */
133
    { ERR_R_FETCH_FAILED, "fetch failed" },
134
    { ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition" },
135
    { ERR_R_UNABLE_TO_GET_READ_LOCK, "unable to get read lock" },
136
    { ERR_R_UNABLE_TO_GET_WRITE_LOCK, "unable to get write lock" },
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
typedef enum ERR_GET_ACTION_e {
162
    EV_POP,
163
    EV_PEEK,
164
    EV_PEEK_LAST
165
} ERR_GET_ACTION;
166
167
static unsigned long get_error_values(ERR_GET_ACTION g,
168
    const char **file, int *line,
169
    const char **func, const char **data,
170
    int *flags);
171
172
#ifndef OPENSSL_NO_ERR
173
static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
174
2.15M
{
175
2.15M
    unsigned long ret, l;
176
177
2.15M
    l = a->error;
178
2.15M
    ret = l ^ ERR_GET_LIB(l);
179
2.15M
    return (ret ^ ret % 19 * 13);
180
2.15M
}
181
182
static int err_string_data_cmp(const ERR_STRING_DATA *a,
183
    const ERR_STRING_DATA *b)
184
1.65M
{
185
1.65M
    if (a->error == b->error)
186
1.57M
        return 0;
187
80.7k
    return a->error > b->error ? 1 : -1;
188
1.65M
}
189
190
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
191
681k
{
192
681k
    ERR_STRING_DATA *p = NULL;
193
194
681k
    if (!CRYPTO_THREAD_read_lock(err_string_lock))
195
0
        return NULL;
196
681k
    p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
197
681k
    CRYPTO_THREAD_unlock(err_string_lock);
198
199
681k
    return p;
200
681k
}
201
#endif
202
203
void OSSL_ERR_STATE_free(ERR_STATE *state)
204
159k
{
205
159k
    int i;
206
207
159k
    if (state == NULL)
208
25.8k
        return;
209
2.27M
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
210
2.14M
        err_clear(state, i, 1);
211
2.14M
    }
212
133k
    CRYPTO_free(state, OPENSSL_FILE, OPENSSL_LINE);
213
133k
}
214
215
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
216
326
{
217
326
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
218
0
        return 0;
219
326
    err_string_lock = CRYPTO_THREAD_lock_new();
220
326
    if (err_string_lock == NULL)
221
0
        return 0;
222
326
#ifndef OPENSSL_NO_ERR
223
326
    int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
224
326
        err_string_data_cmp);
225
326
    if (int_error_hash == NULL) {
226
0
        CRYPTO_THREAD_lock_free(err_string_lock);
227
0
        err_string_lock = NULL;
228
0
        return 0;
229
0
    }
230
326
#endif
231
326
    return 1;
232
326
}
233
234
void err_cleanup(void)
235
101
{
236
101
    if (set_err_thread_local != 0)
237
101
        CRYPTO_THREAD_cleanup_local(&err_thread_local);
238
101
    CRYPTO_THREAD_lock_free(err_string_lock);
239
101
    err_string_lock = NULL;
240
101
#ifndef OPENSSL_NO_ERR
241
101
    lh_ERR_STRING_DATA_free(int_error_hash);
242
101
    int_error_hash = NULL;
243
101
#endif
244
101
}
245
246
#ifndef OPENSSL_NO_ERR
247
/*
248
 * Legacy; pack in the library.
249
 */
250
static void err_patch(int lib, ERR_STRING_DATA *str)
251
0
{
252
0
    unsigned long plib = ERR_PACK(lib, 0, 0);
253
254
0
    for (; str->error != 0; str++)
255
0
        str->error |= plib;
256
0
}
257
258
/*
259
 * Hash in |str| error strings. Assumes the RUN_ONCE was done.
260
 */
261
static int err_load_strings(const ERR_STRING_DATA *str)
262
34.7k
{
263
34.7k
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
264
0
        return 0;
265
1.50M
    for (; str->error; str++)
266
1.47M
        (void)lh_ERR_STRING_DATA_insert(int_error_hash,
267
34.7k
            (ERR_STRING_DATA *)str);
268
34.7k
    CRYPTO_THREAD_unlock(err_string_lock);
269
34.7k
    return 1;
270
34.7k
}
271
#endif
272
273
int ossl_err_load_ERR_strings(void)
274
11.6k
{
275
11.6k
#ifndef OPENSSL_NO_ERR
276
11.6k
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
277
0
        return 0;
278
279
11.6k
    err_load_strings(ERR_str_libraries);
280
11.6k
    err_load_strings(ERR_str_reasons);
281
11.6k
#endif
282
11.6k
    return 1;
283
11.6k
}
284
285
int ERR_load_strings(int lib, ERR_STRING_DATA *str)
286
0
{
287
0
#ifndef OPENSSL_NO_ERR
288
0
    if (ossl_err_load_ERR_strings() == 0)
289
0
        return 0;
290
291
0
    err_patch(lib, str);
292
0
    err_load_strings(str);
293
0
#endif
294
295
0
    return 1;
296
0
}
297
298
int ERR_load_strings_const(const ERR_STRING_DATA *str)
299
11.3k
{
300
11.3k
#ifndef OPENSSL_NO_ERR
301
11.3k
    if (ossl_err_load_ERR_strings() == 0)
302
0
        return 0;
303
11.3k
    err_load_strings(str);
304
11.3k
#endif
305
306
11.3k
    return 1;
307
11.3k
}
308
309
int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
310
0
{
311
0
#ifndef OPENSSL_NO_ERR
312
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
313
0
        return 0;
314
315
    /* The error string table may already have been cleaned up. */
316
0
    if (err_string_lock == NULL)
317
0
        return 1;
318
319
0
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
320
0
        return 0;
321
    /*
322
     * We don't need to ERR_PACK the lib, since that was done (to
323
     * the table) when it was loaded.
324
     */
325
0
    for (; str->error; str++)
326
0
        (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
327
0
    CRYPTO_THREAD_unlock(err_string_lock);
328
0
#endif
329
330
0
    return 1;
331
0
}
332
333
void err_free_strings_int(void)
334
0
{
335
    /* obsolete */
336
0
}
337
338
/********************************************************/
339
340
void ERR_clear_error(void)
341
65.2M
{
342
65.2M
    int i;
343
65.2M
    ERR_STATE *es;
344
345
65.2M
    es = ossl_err_get_state_int();
346
65.2M
    if (es == NULL)
347
0
        return;
348
349
1.10G
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
350
1.04G
        err_clear(es, i, 0);
351
1.04G
    }
352
65.2M
    es->top = es->bottom = 0;
353
65.2M
}
354
355
unsigned long ERR_get_error(void)
356
0
{
357
0
    return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
358
0
}
359
360
unsigned long ERR_get_error_all(const char **file, int *line,
361
    const char **func,
362
    const char **data, int *flags)
363
417k
{
364
417k
    return get_error_values(EV_POP, file, line, func, data, flags);
365
417k
}
366
367
#ifndef OPENSSL_NO_DEPRECATED_3_0
368
unsigned long ERR_get_error_line(const char **file, int *line)
369
0
{
370
0
    return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
371
0
}
372
373
unsigned long ERR_get_error_line_data(const char **file, int *line,
374
    const char **data, int *flags)
375
0
{
376
0
    return get_error_values(EV_POP, file, line, NULL, data, flags);
377
0
}
378
#endif
379
380
unsigned long ERR_peek_error(void)
381
45.4k
{
382
45.4k
    return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
383
45.4k
}
384
385
unsigned long ERR_peek_error_line(const char **file, int *line)
386
0
{
387
0
    return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
388
0
}
389
390
unsigned long ERR_peek_error_func(const char **func)
391
0
{
392
0
    return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
393
0
}
394
395
unsigned long ERR_peek_error_data(const char **data, int *flags)
396
37.7k
{
397
37.7k
    return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
398
37.7k
}
399
400
unsigned long ERR_peek_error_all(const char **file, int *line,
401
    const char **func,
402
    const char **data, int *flags)
403
0
{
404
0
    return get_error_values(EV_PEEK, file, line, func, data, flags);
405
0
}
406
407
#ifndef OPENSSL_NO_DEPRECATED_3_0
408
unsigned long ERR_peek_error_line_data(const char **file, int *line,
409
    const char **data, int *flags)
410
0
{
411
0
    return get_error_values(EV_PEEK, file, line, NULL, data, flags);
412
0
}
413
#endif
414
415
unsigned long ERR_peek_last_error(void)
416
422M
{
417
422M
    return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
418
422M
}
419
420
unsigned long ERR_peek_last_error_line(const char **file, int *line)
421
0
{
422
0
    return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
423
0
}
424
425
unsigned long ERR_peek_last_error_func(const char **func)
426
0
{
427
0
    return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
428
0
}
429
430
unsigned long ERR_peek_last_error_data(const char **data, int *flags)
431
0
{
432
0
    return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
433
0
}
434
435
unsigned long ERR_peek_last_error_all(const char **file, int *line,
436
    const char **func,
437
    const char **data, int *flags)
438
22.7k
{
439
22.7k
    return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
440
22.7k
}
441
442
#ifndef OPENSSL_NO_DEPRECATED_3_0
443
unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
444
    const char **data, int *flags)
445
0
{
446
0
    return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
447
0
}
448
#endif
449
450
static unsigned long get_error_values(ERR_GET_ACTION g,
451
    const char **file, int *line,
452
    const char **func,
453
    const char **data, int *flags)
454
423M
{
455
423M
    int i = 0;
456
423M
    ERR_STATE *es;
457
423M
    unsigned long ret;
458
459
423M
    es = ossl_err_get_state_int();
460
423M
    if (es == NULL)
461
0
        return 0;
462
463
    /*
464
     * Clear anything that should have been cleared earlier. We do this
465
     * here because this doesn't have constant-time issues.
466
     */
467
423M
    while (es->bottom != es->top) {
468
421M
        if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
469
0
            err_clear(es, es->top, 0);
470
0
            es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
471
0
            continue;
472
0
        }
473
421M
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
474
421M
        if (es->err_flags[i] & ERR_FLAG_CLEAR) {
475
756
            es->bottom = i;
476
756
            err_clear(es, es->bottom, 0);
477
756
            continue;
478
756
        }
479
421M
        break;
480
421M
    }
481
482
    /* If everything has been cleared, the stack is empty. */
483
423M
    if (es->bottom == es->top)
484
1.64M
        return 0;
485
486
    /* Which error, the top of stack (latest one) or the first one? */
487
421M
    if (g == EV_PEEK_LAST)
488
421M
        i = es->top;
489
342k
    else
490
342k
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
491
492
421M
    ret = es->err_buffer[i];
493
421M
    if (g == EV_POP) {
494
264k
        es->bottom = i;
495
264k
        es->err_buffer[i] = 0;
496
264k
    }
497
498
421M
    if (file != NULL) {
499
286k
        *file = es->err_file[i];
500
286k
        if (*file == NULL)
501
0
            *file = "";
502
286k
    }
503
421M
    if (line != NULL)
504
286k
        *line = es->err_line[i];
505
421M
    if (func != NULL) {
506
286k
        *func = es->err_func[i];
507
286k
        if (*func == NULL)
508
0
            *func = "";
509
286k
    }
510
421M
    if (flags != NULL)
511
324k
        *flags = es->err_data_flags[i];
512
421M
    if (data == NULL) {
513
421M
        if (g == EV_POP) {
514
0
            err_clear_data(es, i, 0);
515
0
        }
516
421M
    } else {
517
324k
        *data = es->err_data[i];
518
324k
        if (*data == NULL) {
519
4
            *data = "";
520
4
            if (flags != NULL)
521
4
                *flags = 0;
522
4
        }
523
324k
    }
524
421M
    return ret;
525
423M
}
526
527
void ossl_err_string_int(unsigned long e, const char *func,
528
    char *buf, size_t len)
529
20.2k
{
530
20.2k
    char lsbuf[64], rsbuf[256];
531
20.2k
    const char *ls, *rs = NULL;
532
20.2k
    unsigned long l, r;
533
534
20.2k
    if (len == 0)
535
0
        return;
536
537
20.2k
    l = ERR_GET_LIB(e);
538
20.2k
    ls = ERR_lib_error_string(e);
539
20.2k
    if (ls == NULL) {
540
0
        BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
541
0
        ls = lsbuf;
542
0
    }
543
544
    /*
545
     * ERR_reason_error_string() can't safely return system error strings,
546
     * since it would call openssl_strerror_r(), which needs a buffer for
547
     * thread safety.  So for system errors, we call openssl_strerror_r()
548
     * directly instead.
549
     */
550
20.2k
    r = ERR_GET_REASON(e);
551
20.2k
#ifndef OPENSSL_NO_ERR
552
20.2k
    if (ERR_SYSTEM_ERROR(e)) {
553
0
        if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
554
0
            rs = rsbuf;
555
20.2k
    } else {
556
20.2k
        rs = ERR_reason_error_string(e);
557
20.2k
    }
558
20.2k
#endif
559
20.2k
    if (rs == NULL) {
560
0
        BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)",
561
0
            r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET));
562
0
        rs = rsbuf;
563
0
    }
564
565
20.2k
    BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
566
20.2k
    if (strlen(buf) == len - 1) {
567
        /* Didn't fit; use a minimal format. */
568
0
        BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
569
0
    }
570
20.2k
}
571
572
void ERR_error_string_n(unsigned long e, char *buf, size_t len)
573
0
{
574
0
    ossl_err_string_int(e, "", buf, len);
575
0
}
576
577
/*
578
 * ERR_error_string_n should be used instead for ret != NULL as
579
 * ERR_error_string cannot know how large the buffer is
580
 */
581
char *ERR_error_string(unsigned long e, char *ret)
582
0
{
583
0
    static char buf[256];
584
585
0
    if (ret == NULL)
586
0
        ret = buf;
587
0
    ERR_error_string_n(e, ret, (int)sizeof(buf));
588
0
    return ret;
589
0
}
590
591
const char *ERR_lib_error_string(unsigned long e)
592
301k
{
593
301k
#ifndef OPENSSL_NO_ERR
594
301k
    ERR_STRING_DATA d, *p;
595
301k
    unsigned long l;
596
597
301k
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
598
0
        return NULL;
599
0
    }
600
601
301k
    l = ERR_GET_LIB(e);
602
301k
    d.error = ERR_PACK(l, 0, 0);
603
301k
    p = int_err_get_item(&d);
604
301k
    return ((p == NULL) ? NULL : p->string);
605
#else
606
    return NULL;
607
#endif
608
301k
}
609
610
#ifndef OPENSSL_NO_DEPRECATED_3_0
611
const char *ERR_func_error_string(unsigned long e)
612
0
{
613
0
    return NULL;
614
0
}
615
#endif
616
617
const char *ERR_reason_error_string(unsigned long e)
618
351k
{
619
351k
#ifndef OPENSSL_NO_ERR
620
351k
    ERR_STRING_DATA d, *p = NULL;
621
351k
    unsigned long l, r;
622
623
351k
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
624
0
        return NULL;
625
0
    }
626
627
    /*
628
     * ERR_reason_error_string() can't safely return system error strings,
629
     * since openssl_strerror_r() needs a buffer for thread safety, and we
630
     * haven't got one that would serve any sensible purpose.
631
     */
632
351k
    if (ERR_SYSTEM_ERROR(e))
633
0
        return NULL;
634
635
351k
    l = ERR_GET_LIB(e);
636
351k
    r = ERR_GET_REASON(e);
637
351k
    d.error = ERR_PACK(l, 0, r);
638
351k
    p = int_err_get_item(&d);
639
351k
    if (p == NULL) {
640
28.1k
        d.error = ERR_PACK(0, 0, r);
641
28.1k
        p = int_err_get_item(&d);
642
28.1k
    }
643
351k
    return ((p == NULL) ? NULL : p->string);
644
#else
645
    return NULL;
646
#endif
647
351k
}
648
649
static void err_delete_thread_state(void *unused)
650
154
{
651
154
    ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
652
154
    if (state == NULL)
653
0
        return;
654
655
154
    CRYPTO_THREAD_set_local(&err_thread_local, NULL);
656
154
    OSSL_ERR_STATE_free(state);
657
154
}
658
659
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
660
void ERR_remove_thread_state(void *dummy)
661
0
{
662
0
}
663
#endif
664
665
#ifndef OPENSSL_NO_DEPRECATED_1_0_0
666
void ERR_remove_state(unsigned long pid)
667
0
{
668
0
}
669
#endif
670
671
DEFINE_RUN_ONCE_STATIC(err_do_init)
672
101
{
673
101
    set_err_thread_local = 1;
674
101
    return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
675
101
}
676
677
ERR_STATE *ossl_err_get_state_int(void)
678
1.03G
{
679
1.03G
    ERR_STATE *state;
680
1.03G
    int saveerrno = get_last_sys_error();
681
682
1.03G
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
683
0
        return NULL;
684
685
1.03G
    if (!RUN_ONCE(&err_init, err_do_init))
686
0
        return NULL;
687
688
1.03G
    state = CRYPTO_THREAD_get_local(&err_thread_local);
689
1.03G
    if (state == (ERR_STATE *)-1)
690
0
        return NULL;
691
692
1.03G
    if (state == NULL) {
693
101
        if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1))
694
0
            return NULL;
695
696
101
        state = OSSL_ERR_STATE_new();
697
101
        if (state == NULL) {
698
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
699
0
            return NULL;
700
0
        }
701
702
101
        if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
703
101
            || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
704
0
            OSSL_ERR_STATE_free(state);
705
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
706
0
            return NULL;
707
0
        }
708
709
        /* Ignore failures from these */
710
101
        OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
711
101
    }
712
713
1.03G
    set_sys_error(saveerrno);
714
1.03G
    return state;
715
1.03G
}
716
717
#ifndef OPENSSL_NO_DEPRECATED_3_0
718
ERR_STATE *ERR_get_state(void)
719
0
{
720
0
    return ossl_err_get_state_int();
721
0
}
722
#endif
723
724
/*
725
 * err_shelve_state returns the current thread local error state
726
 * and freezes the error module until err_unshelve_state is called.
727
 */
728
int err_shelve_state(void **state)
729
101
{
730
101
    int saveerrno = get_last_sys_error();
731
732
    /*
733
     * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
734
     * via ossl_init_load_crypto_nodelete(), by which point the requested
735
     * "base" initialization has already been performed, so the below call is a
736
     * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
737
     *
738
     * If are no other valid callers of this function, the call below can be
739
     * removed, avoiding the re-entry into OPENSSL_init_crypto().  If there are
740
     * potential uses that are not from inside OPENSSL_init_crypto(), then this
741
     * call is needed, but some care is required to make sure that the re-entry
742
     * remains a NOOP.
743
     */
744
101
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
745
0
        return 0;
746
747
101
    if (!RUN_ONCE(&err_init, err_do_init))
748
0
        return 0;
749
750
101
    *state = CRYPTO_THREAD_get_local(&err_thread_local);
751
101
    if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1))
752
0
        return 0;
753
754
101
    set_sys_error(saveerrno);
755
101
    return 1;
756
101
}
757
758
/*
759
 * err_unshelve_state restores the error state that was returned
760
 * by err_shelve_state previously.
761
 */
762
void err_unshelve_state(void *state)
763
326
{
764
326
    if (state != (void *)-1)
765
326
        CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)state);
766
326
}
767
768
int ERR_get_next_error_library(void)
769
279
{
770
279
    int ret;
771
772
279
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
773
0
        return 0;
774
775
279
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
776
0
        return 0;
777
279
    ret = int_err_library_number++;
778
279
    CRYPTO_THREAD_unlock(err_string_lock);
779
279
    return ret;
780
279
}
781
782
static int err_set_error_data_int(char *data, size_t size, int flags,
783
    int deallocate)
784
16.8M
{
785
16.8M
    ERR_STATE *es;
786
787
16.8M
    es = ossl_err_get_state_int();
788
16.8M
    if (es == NULL)
789
0
        return 0;
790
791
16.8M
    err_clear_data(es, es->top, deallocate);
792
16.8M
    err_set_data(es, es->top, data, size, flags);
793
794
16.8M
    return 1;
795
16.8M
}
796
797
void ERR_set_error_data(char *data, int flags)
798
0
{
799
    /*
800
     * This function is void so we cannot propagate the error return. Since it
801
     * is also in the public API we can't change the return type.
802
     *
803
     * We estimate the size of the data.  If it's not flagged as allocated,
804
     * then this is safe, and if it is flagged as allocated, then our size
805
     * may be smaller than the actual allocation, but that doesn't matter
806
     * too much, the buffer will remain untouched or will eventually be
807
     * reallocated to a new size.
808
     *
809
     * callers should be advised that this function takes over ownership of
810
     * the allocated memory, i.e. they can't count on the pointer to remain
811
     * valid.
812
     */
813
0
    err_set_error_data_int(data, strlen(data) + 1, flags, 1);
814
0
}
815
816
void ERR_add_error_data(int num, ...)
817
16.8M
{
818
16.8M
    va_list args;
819
16.8M
    va_start(args, num);
820
16.8M
    ERR_add_error_vdata(num, args);
821
16.8M
    va_end(args);
822
16.8M
}
823
824
void ERR_add_error_vdata(int num, va_list args)
825
16.8M
{
826
16.8M
    int i, len, size;
827
16.8M
    int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
828
16.8M
    char *str, *arg;
829
16.8M
    ERR_STATE *es;
830
831
    /* Get the current error data; if an allocated string get it. */
832
16.8M
    es = ossl_err_get_state_int();
833
16.8M
    if (es == NULL)
834
0
        return;
835
16.8M
    i = es->top;
836
837
    /*
838
     * If err_data is allocated already, reuse the space.
839
     * Otherwise, allocate a small new buffer.
840
     */
841
16.8M
    if ((es->err_data_flags[i] & flags) == flags
842
18.8k
        && ossl_assert(es->err_data[i] != NULL)) {
843
18.8k
        str = es->err_data[i];
844
18.8k
        size = es->err_data_size[i];
845
846
        /*
847
         * To protect the string we just grabbed from tampering by other
848
         * functions we may call, or to protect them from freeing a pointer
849
         * that may no longer be valid at that point, we clear away the
850
         * data pointer and the flags.  We will set them again at the end
851
         * of this function.
852
         */
853
18.8k
        es->err_data[i] = NULL;
854
18.8k
        es->err_data_flags[i] = 0;
855
16.8M
    } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
856
0
        return;
857
16.8M
    } else {
858
16.8M
        str[0] = '\0';
859
16.8M
    }
860
16.8M
    len = strlen(str);
861
862
72.1M
    while (--num >= 0) {
863
55.2M
        arg = va_arg(args, char *);
864
55.2M
        if (arg == NULL)
865
2.72k
            arg = "<NULL>";
866
55.2M
        len += strlen(arg);
867
55.2M
        if (len >= size) {
868
13.2k
            char *p;
869
870
13.2k
            size = len + 20;
871
13.2k
            p = OPENSSL_realloc(str, size);
872
13.2k
            if (p == NULL) {
873
0
                OPENSSL_free(str);
874
0
                return;
875
0
            }
876
13.2k
            str = p;
877
13.2k
        }
878
55.2M
        OPENSSL_strlcat(str, arg, (size_t)size);
879
55.2M
    }
880
16.8M
    if (!err_set_error_data_int(str, size, flags, 0))
881
0
        OPENSSL_free(str);
882
16.8M
}
883
884
void err_clear_last_constant_time(int clear)
885
10.2k
{
886
10.2k
    ERR_STATE *es;
887
10.2k
    int top;
888
889
10.2k
    es = ossl_err_get_state_int();
890
10.2k
    if (es == NULL)
891
0
        return;
892
893
10.2k
    top = es->top;
894
895
    /*
896
     * Flag error as cleared but remove it elsewhere to avoid two errors
897
     * accessing the same error stack location, revealing timing information.
898
     */
899
10.2k
    clear = constant_time_select_int(constant_time_eq_int(clear, 0),
900
10.2k
        0, ERR_FLAG_CLEAR);
901
10.2k
    es->err_flags[top] |= clear;
902
10.2k
}