Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/err/err.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2023 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
1.94M
{
175
1.94M
    unsigned long ret, l;
176
177
1.94M
    l = a->error;
178
1.94M
    ret = l ^ ERR_GET_LIB(l);
179
1.94M
    return (ret ^ ret % 19 * 13);
180
1.94M
}
181
182
static int err_string_data_cmp(const ERR_STRING_DATA *a,
183
    const ERR_STRING_DATA *b)
184
1.52M
{
185
1.52M
    if (a->error == b->error)
186
1.45M
        return 0;
187
70.2k
    return a->error > b->error ? 1 : -1;
188
1.52M
}
189
190
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
191
734k
{
192
734k
    ERR_STRING_DATA *p = NULL;
193
194
734k
    if (!CRYPTO_THREAD_read_lock(err_string_lock))
195
0
        return NULL;
196
734k
    p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
197
734k
    CRYPTO_THREAD_unlock(err_string_lock);
198
199
734k
    return p;
200
734k
}
201
#endif
202
203
void OSSL_ERR_STATE_free(ERR_STATE *state)
204
151k
{
205
151k
    int i;
206
207
151k
    if (state == NULL)
208
25.0k
        return;
209
2.15M
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
210
2.02M
        err_clear(state, i, 1);
211
2.02M
    }
212
126k
    CRYPTO_free(state, OPENSSL_FILE, OPENSSL_LINE);
213
126k
}
214
215
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
216
273
{
217
273
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
218
0
        return 0;
219
273
    err_string_lock = CRYPTO_THREAD_lock_new();
220
273
    if (err_string_lock == NULL)
221
0
        return 0;
222
273
#ifndef OPENSSL_NO_ERR
223
273
    int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
224
273
        err_string_data_cmp);
225
273
    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
273
#endif
231
273
    return 1;
232
273
}
233
234
void err_cleanup(void)
235
167
{
236
167
    if (set_err_thread_local != 0)
237
167
        CRYPTO_THREAD_cleanup_local(&err_thread_local);
238
167
    CRYPTO_THREAD_lock_free(err_string_lock);
239
167
    err_string_lock = NULL;
240
167
#ifndef OPENSSL_NO_ERR
241
167
    lh_ERR_STRING_DATA_free(int_error_hash);
242
167
    int_error_hash = NULL;
243
167
#endif
244
167
}
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
28.5k
{
263
28.5k
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
264
0
        return 0;
265
1.23M
    for (; str->error; str++)
266
1.21M
        (void)lh_ERR_STRING_DATA_insert(int_error_hash,
267
28.5k
            (ERR_STRING_DATA *)str);
268
28.5k
    CRYPTO_THREAD_unlock(err_string_lock);
269
28.5k
    return 1;
270
28.5k
}
271
#endif
272
273
int ossl_err_load_ERR_strings(void)
274
9.60k
{
275
9.60k
#ifndef OPENSSL_NO_ERR
276
9.60k
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
277
0
        return 0;
278
279
9.60k
    err_load_strings(ERR_str_libraries);
280
9.60k
    err_load_strings(ERR_str_reasons);
281
9.60k
#endif
282
9.60k
    return 1;
283
9.60k
}
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
9.33k
{
300
9.33k
#ifndef OPENSSL_NO_ERR
301
9.33k
    if (ossl_err_load_ERR_strings() == 0)
302
0
        return 0;
303
9.33k
    err_load_strings(str);
304
9.33k
#endif
305
306
9.33k
    return 1;
307
9.33k
}
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
0
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
316
0
        return 0;
317
    /*
318
     * We don't need to ERR_PACK the lib, since that was done (to
319
     * the table) when it was loaded.
320
     */
321
0
    for (; str->error; str++)
322
0
        (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
323
0
    CRYPTO_THREAD_unlock(err_string_lock);
324
0
#endif
325
326
0
    return 1;
327
0
}
328
329
void err_free_strings_int(void)
330
0
{
331
    /* obsolete */
332
0
}
333
334
/********************************************************/
335
336
void ERR_clear_error(void)
337
63.6M
{
338
63.6M
    int i;
339
63.6M
    ERR_STATE *es;
340
341
63.6M
    es = ossl_err_get_state_int();
342
63.6M
    if (es == NULL)
343
0
        return;
344
345
1.08G
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
346
1.01G
        err_clear(es, i, 0);
347
1.01G
    }
348
63.6M
    es->top = es->bottom = 0;
349
63.6M
}
350
351
unsigned long ERR_get_error(void)
352
0
{
353
0
    return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
354
0
}
355
356
unsigned long ERR_get_error_all(const char **file, int *line,
357
    const char **func,
358
    const char **data, int *flags)
359
486k
{
360
486k
    return get_error_values(EV_POP, file, line, func, data, flags);
361
486k
}
362
363
#ifndef OPENSSL_NO_DEPRECATED_3_0
364
unsigned long ERR_get_error_line(const char **file, int *line)
365
0
{
366
0
    return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
367
0
}
368
369
unsigned long ERR_get_error_line_data(const char **file, int *line,
370
    const char **data, int *flags)
371
0
{
372
0
    return get_error_values(EV_POP, file, line, NULL, data, flags);
373
0
}
374
#endif
375
376
unsigned long ERR_peek_error(void)
377
3.62k
{
378
3.62k
    return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
379
3.62k
}
380
381
unsigned long ERR_peek_error_line(const char **file, int *line)
382
0
{
383
0
    return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
384
0
}
385
386
unsigned long ERR_peek_error_func(const char **func)
387
0
{
388
0
    return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
389
0
}
390
391
unsigned long ERR_peek_error_data(const char **data, int *flags)
392
39.3k
{
393
39.3k
    return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
394
39.3k
}
395
396
unsigned long ERR_peek_error_all(const char **file, int *line,
397
    const char **func,
398
    const char **data, int *flags)
399
0
{
400
0
    return get_error_values(EV_PEEK, file, line, func, data, flags);
401
0
}
402
403
#ifndef OPENSSL_NO_DEPRECATED_3_0
404
unsigned long ERR_peek_error_line_data(const char **file, int *line,
405
    const char **data, int *flags)
406
0
{
407
0
    return get_error_values(EV_PEEK, file, line, NULL, data, flags);
408
0
}
409
#endif
410
411
unsigned long ERR_peek_last_error(void)
412
262M
{
413
262M
    return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
414
262M
}
415
416
unsigned long ERR_peek_last_error_line(const char **file, int *line)
417
0
{
418
0
    return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
419
0
}
420
421
unsigned long ERR_peek_last_error_func(const char **func)
422
0
{
423
0
    return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
424
0
}
425
426
unsigned long ERR_peek_last_error_data(const char **data, int *flags)
427
0
{
428
0
    return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
429
0
}
430
431
unsigned long ERR_peek_last_error_all(const char **file, int *line,
432
    const char **func,
433
    const char **data, int *flags)
434
20.8k
{
435
20.8k
    return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
436
20.8k
}
437
438
#ifndef OPENSSL_NO_DEPRECATED_3_0
439
unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
440
    const char **data, int *flags)
441
0
{
442
0
    return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
443
0
}
444
#endif
445
446
static unsigned long get_error_values(ERR_GET_ACTION g,
447
    const char **file, int *line,
448
    const char **func,
449
    const char **data, int *flags)
450
262M
{
451
262M
    int i = 0;
452
262M
    ERR_STATE *es;
453
262M
    unsigned long ret;
454
455
262M
    es = ossl_err_get_state_int();
456
262M
    if (es == NULL)
457
0
        return 0;
458
459
    /*
460
     * Clear anything that should have been cleared earlier. We do this
461
     * here because this doesn't have constant-time issues.
462
     */
463
262M
    while (es->bottom != es->top) {
464
261M
        if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
465
0
            err_clear(es, es->top, 0);
466
0
            es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
467
0
            continue;
468
0
        }
469
261M
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
470
261M
        if (es->err_flags[i] & ERR_FLAG_CLEAR) {
471
0
            es->bottom = i;
472
0
            err_clear(es, es->bottom, 0);
473
0
            continue;
474
0
        }
475
261M
        break;
476
261M
    }
477
478
    /* If everything has been cleared, the stack is empty. */
479
262M
    if (es->bottom == es->top)
480
1.55M
        return 0;
481
482
    /* Which error, the top of stack (latest one) or the first one? */
483
261M
    if (g == EV_PEEK_LAST)
484
260M
        i = es->top;
485
330k
    else
486
330k
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
487
488
261M
    ret = es->err_buffer[i];
489
261M
    if (g == EV_POP) {
490
288k
        es->bottom = i;
491
288k
        es->err_buffer[i] = 0;
492
288k
    }
493
494
261M
    if (file != NULL) {
495
308k
        *file = es->err_file[i];
496
308k
        if (*file == NULL)
497
0
            *file = "";
498
308k
    }
499
261M
    if (line != NULL)
500
308k
        *line = es->err_line[i];
501
261M
    if (func != NULL) {
502
308k
        *func = es->err_func[i];
503
308k
        if (*func == NULL)
504
0
            *func = "";
505
308k
    }
506
261M
    if (flags != NULL)
507
348k
        *flags = es->err_data_flags[i];
508
261M
    if (data == NULL) {
509
260M
        if (g == EV_POP) {
510
0
            err_clear_data(es, i, 0);
511
0
        }
512
260M
    } else {
513
348k
        *data = es->err_data[i];
514
348k
        if (*data == NULL) {
515
145
            *data = "";
516
145
            if (flags != NULL)
517
145
                *flags = 0;
518
145
        }
519
348k
    }
520
261M
    return ret;
521
262M
}
522
523
void ossl_err_string_int(unsigned long e, const char *func,
524
    char *buf, size_t len)
525
30.1k
{
526
30.1k
    char lsbuf[64], rsbuf[256];
527
30.1k
    const char *ls, *rs = NULL;
528
30.1k
    unsigned long l, r;
529
530
30.1k
    if (len == 0)
531
0
        return;
532
533
30.1k
    l = ERR_GET_LIB(e);
534
30.1k
    ls = ERR_lib_error_string(e);
535
30.1k
    if (ls == NULL) {
536
271
        BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
537
271
        ls = lsbuf;
538
271
    }
539
540
    /*
541
     * ERR_reason_error_string() can't safely return system error strings,
542
     * since it would call openssl_strerror_r(), which needs a buffer for
543
     * thread safety.  So for system errors, we call openssl_strerror_r()
544
     * directly instead.
545
     */
546
30.1k
    r = ERR_GET_REASON(e);
547
30.1k
#ifndef OPENSSL_NO_ERR
548
30.1k
    if (ERR_SYSTEM_ERROR(e)) {
549
0
        if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
550
0
            rs = rsbuf;
551
30.1k
    } else {
552
30.1k
        rs = ERR_reason_error_string(e);
553
30.1k
    }
554
30.1k
#endif
555
30.1k
    if (rs == NULL) {
556
271
        BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)",
557
271
            r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET));
558
271
        rs = rsbuf;
559
271
    }
560
561
30.1k
    BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
562
30.1k
    if (strlen(buf) == len - 1) {
563
        /* Didn't fit; use a minimal format. */
564
0
        BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
565
0
    }
566
30.1k
}
567
568
void ERR_error_string_n(unsigned long e, char *buf, size_t len)
569
271
{
570
271
    ossl_err_string_int(e, "", buf, len);
571
271
}
572
573
/*
574
 * ERR_error_string_n should be used instead for ret != NULL as
575
 * ERR_error_string cannot know how large the buffer is
576
 */
577
char *ERR_error_string(unsigned long e, char *ret)
578
0
{
579
0
    static char buf[256];
580
581
0
    if (ret == NULL)
582
0
        ret = buf;
583
0
    ERR_error_string_n(e, ret, (int)sizeof(buf));
584
0
    return ret;
585
0
}
586
587
const char *ERR_lib_error_string(unsigned long e)
588
327k
{
589
327k
#ifndef OPENSSL_NO_ERR
590
327k
    ERR_STRING_DATA d, *p;
591
327k
    unsigned long l;
592
593
327k
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
594
0
        return NULL;
595
0
    }
596
597
327k
    l = ERR_GET_LIB(e);
598
327k
    d.error = ERR_PACK(l, 0, 0);
599
327k
    p = int_err_get_item(&d);
600
327k
    return ((p == NULL) ? NULL : p->string);
601
#else
602
    return NULL;
603
#endif
604
327k
}
605
606
#ifndef OPENSSL_NO_DEPRECATED_3_0
607
const char *ERR_func_error_string(unsigned long e)
608
0
{
609
0
    return NULL;
610
0
}
611
#endif
612
613
const char *ERR_reason_error_string(unsigned long e)
614
376k
{
615
376k
#ifndef OPENSSL_NO_ERR
616
376k
    ERR_STRING_DATA d, *p = NULL;
617
376k
    unsigned long l, r;
618
619
376k
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
620
0
        return NULL;
621
0
    }
622
623
    /*
624
     * ERR_reason_error_string() can't safely return system error strings,
625
     * since openssl_strerror_r() needs a buffer for thread safety, and we
626
     * haven't got one that would serve any sensible purpose.
627
     */
628
376k
    if (ERR_SYSTEM_ERROR(e))
629
0
        return NULL;
630
631
376k
    l = ERR_GET_LIB(e);
632
376k
    r = ERR_GET_REASON(e);
633
376k
    d.error = ERR_PACK(l, 0, r);
634
376k
    p = int_err_get_item(&d);
635
376k
    if (p == NULL) {
636
30.4k
        d.error = ERR_PACK(0, 0, r);
637
30.4k
        p = int_err_get_item(&d);
638
30.4k
    }
639
376k
    return ((p == NULL) ? NULL : p->string);
640
#else
641
    return NULL;
642
#endif
643
376k
}
644
645
static void err_delete_thread_state(void *unused)
646
220
{
647
220
    ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
648
220
    if (state == NULL)
649
0
        return;
650
651
220
    CRYPTO_THREAD_set_local(&err_thread_local, NULL);
652
220
    OSSL_ERR_STATE_free(state);
653
220
}
654
655
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
656
void ERR_remove_thread_state(void *dummy)
657
0
{
658
0
}
659
#endif
660
661
#ifndef OPENSSL_NO_DEPRECATED_1_0_0
662
void ERR_remove_state(unsigned long pid)
663
0
{
664
0
}
665
#endif
666
667
DEFINE_RUN_ONCE_STATIC(err_do_init)
668
167
{
669
167
    set_err_thread_local = 1;
670
167
    return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
671
167
}
672
673
ERR_STATE *ossl_err_get_state_int(void)
674
1.27G
{
675
1.27G
    ERR_STATE *state;
676
1.27G
    int saveerrno = get_last_sys_error();
677
678
1.27G
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
679
0
        return NULL;
680
681
1.27G
    if (!RUN_ONCE(&err_init, err_do_init))
682
0
        return NULL;
683
684
1.27G
    state = CRYPTO_THREAD_get_local(&err_thread_local);
685
1.27G
    if (state == (ERR_STATE *)-1)
686
0
        return NULL;
687
688
1.27G
    if (state == NULL) {
689
167
        if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1))
690
0
            return NULL;
691
692
167
        state = OSSL_ERR_STATE_new();
693
167
        if (state == NULL) {
694
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
695
0
            return NULL;
696
0
        }
697
698
167
        if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
699
167
            || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
700
0
            OSSL_ERR_STATE_free(state);
701
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
702
0
            return NULL;
703
0
        }
704
705
        /* Ignore failures from these */
706
167
        OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
707
167
    }
708
709
1.27G
    set_sys_error(saveerrno);
710
1.27G
    return state;
711
1.27G
}
712
713
#ifndef OPENSSL_NO_DEPRECATED_3_0
714
ERR_STATE *ERR_get_state(void)
715
0
{
716
0
    return ossl_err_get_state_int();
717
0
}
718
#endif
719
720
/*
721
 * err_shelve_state returns the current thread local error state
722
 * and freezes the error module until err_unshelve_state is called.
723
 */
724
int err_shelve_state(void **state)
725
143
{
726
143
    int saveerrno = get_last_sys_error();
727
728
    /*
729
     * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
730
     * via ossl_init_load_crypto_nodelete(), by which point the requested
731
     * "base" initialization has already been performed, so the below call is a
732
     * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
733
     *
734
     * If are no other valid callers of this function, the call below can be
735
     * removed, avoiding the re-entry into OPENSSL_init_crypto().  If there are
736
     * potential uses that are not from inside OPENSSL_init_crypto(), then this
737
     * call is needed, but some care is required to make sure that the re-entry
738
     * remains a NOOP.
739
     */
740
143
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
741
0
        return 0;
742
743
143
    if (!RUN_ONCE(&err_init, err_do_init))
744
0
        return 0;
745
746
143
    *state = CRYPTO_THREAD_get_local(&err_thread_local);
747
143
    if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1))
748
0
        return 0;
749
750
143
    set_sys_error(saveerrno);
751
143
    return 1;
752
143
}
753
754
/*
755
 * err_unshelve_state restores the error state that was returned
756
 * by err_shelve_state previously.
757
 */
758
void err_unshelve_state(void *state)
759
249
{
760
249
    if (state != (void *)-1)
761
249
        CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)state);
762
249
}
763
764
int ERR_get_next_error_library(void)
765
235
{
766
235
    int ret;
767
768
235
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
769
0
        return 0;
770
771
235
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
772
0
        return 0;
773
235
    ret = int_err_library_number++;
774
235
    CRYPTO_THREAD_unlock(err_string_lock);
775
235
    return ret;
776
235
}
777
778
static int err_set_error_data_int(char *data, size_t size, int flags,
779
    int deallocate)
780
17.4M
{
781
17.4M
    ERR_STATE *es;
782
783
17.4M
    es = ossl_err_get_state_int();
784
17.4M
    if (es == NULL)
785
0
        return 0;
786
787
17.4M
    err_clear_data(es, es->top, deallocate);
788
17.4M
    err_set_data(es, es->top, data, size, flags);
789
790
17.4M
    return 1;
791
17.4M
}
792
793
void ERR_set_error_data(char *data, int flags)
794
0
{
795
    /*
796
     * This function is void so we cannot propagate the error return. Since it
797
     * is also in the public API we can't change the return type.
798
     *
799
     * We estimate the size of the data.  If it's not flagged as allocated,
800
     * then this is safe, and if it is flagged as allocated, then our size
801
     * may be smaller than the actual allocation, but that doesn't matter
802
     * too much, the buffer will remain untouched or will eventually be
803
     * reallocated to a new size.
804
     *
805
     * callers should be advised that this function takes over ownership of
806
     * the allocated memory, i.e. they can't count on the pointer to remain
807
     * valid.
808
     */
809
0
    err_set_error_data_int(data, strlen(data) + 1, flags, 1);
810
0
}
811
812
void ERR_add_error_data(int num, ...)
813
17.4M
{
814
17.4M
    va_list args;
815
17.4M
    va_start(args, num);
816
17.4M
    ERR_add_error_vdata(num, args);
817
17.4M
    va_end(args);
818
17.4M
}
819
820
void ERR_add_error_vdata(int num, va_list args)
821
14.5M
{
822
14.5M
    int i, len, size;
823
14.5M
    int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
824
14.5M
    char *str, *arg;
825
14.5M
    ERR_STATE *es;
826
827
    /* Get the current error data; if an allocated string get it. */
828
14.5M
    es = ossl_err_get_state_int();
829
14.5M
    if (es == NULL)
830
0
        return;
831
14.5M
    i = es->top;
832
833
    /*
834
     * If err_data is allocated already, reuse the space.
835
     * Otherwise, allocate a small new buffer.
836
     */
837
14.5M
    if ((es->err_data_flags[i] & flags) == flags
838
13.6k
        && ossl_assert(es->err_data[i] != NULL)) {
839
13.6k
        str = es->err_data[i];
840
13.6k
        size = es->err_data_size[i];
841
842
        /*
843
         * To protect the string we just grabbed from tampering by other
844
         * functions we may call, or to protect them from freeing a pointer
845
         * that may no longer be valid at that point, we clear away the
846
         * data pointer and the flags.  We will set them again at the end
847
         * of this function.
848
         */
849
13.6k
        es->err_data[i] = NULL;
850
13.6k
        es->err_data_flags[i] = 0;
851
14.5M
    } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
852
0
        return;
853
14.5M
    } else {
854
14.5M
        str[0] = '\0';
855
14.5M
    }
856
14.5M
    len = strlen(str);
857
858
62.2M
    while (--num >= 0) {
859
47.6M
        arg = va_arg(args, char *);
860
47.6M
        if (arg == NULL)
861
0
            arg = "<NULL>";
862
47.6M
        len += strlen(arg);
863
47.6M
        if (len >= size) {
864
10.3k
            char *p;
865
866
10.3k
            size = len + 20;
867
10.3k
            p = OPENSSL_realloc(str, size);
868
10.3k
            if (p == NULL) {
869
0
                OPENSSL_free(str);
870
0
                return;
871
0
            }
872
10.3k
            str = p;
873
10.3k
        }
874
47.6M
        OPENSSL_strlcat(str, arg, (size_t)size);
875
47.6M
    }
876
14.5M
    if (!err_set_error_data_int(str, size, flags, 0))
877
0
        OPENSSL_free(str);
878
14.5M
}
879
880
void err_clear_last_constant_time(int clear)
881
11.3k
{
882
11.3k
    ERR_STATE *es;
883
11.3k
    int top;
884
885
11.3k
    es = ossl_err_get_state_int();
886
11.3k
    if (es == NULL)
887
0
        return;
888
889
11.3k
    top = es->top;
890
891
    /*
892
     * Flag error as cleared but remove it elsewhere to avoid two errors
893
     * accessing the same error stack location, revealing timing information.
894
     */
895
11.3k
    clear = constant_time_select_int(constant_time_eq_int(clear, 0),
896
11.3k
        0, ERR_FLAG_CLEAR);
897
11.3k
    es->err_flags[top] |= clear;
898
11.3k
}