Coverage Report

Created: 2026-10-28 06:28

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