Coverage Report

Created: 2026-10-28 06:28

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