Coverage Report

Created: 2026-10-16 07:31

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