Coverage Report

Created: 2025-06-13 06:36

/src/openssl/crypto/err/err.c
Line
Count
Source (jump to first uncovered line)
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, EV_PEEK, EV_PEEK_LAST
163
} ERR_GET_ACTION;
164
165
static unsigned long get_error_values(ERR_GET_ACTION g,
166
                                      const char **file, int *line,
167
                                      const char **func, const char **data,
168
                                      int *flags);
169
170
#ifndef OPENSSL_NO_ERR
171
static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
172
4.37k
{
173
4.37k
    unsigned long ret, l;
174
175
4.37k
    l = a->error;
176
4.37k
    ret = l ^ ERR_GET_LIB(l);
177
4.37k
    return (ret ^ ret % 19 * 13);
178
4.37k
}
179
180
static int err_string_data_cmp(const ERR_STRING_DATA *a,
181
                               const ERR_STRING_DATA *b)
182
2.94k
{
183
2.94k
    if (a->error == b->error)
184
2.78k
        return 0;
185
158
    return a->error > b->error ? 1 : -1;
186
2.94k
}
187
188
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
189
68
{
190
68
    ERR_STRING_DATA *p = NULL;
191
192
68
    if (!CRYPTO_THREAD_read_lock(err_string_lock))
193
0
        return NULL;
194
68
    p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
195
68
    CRYPTO_THREAD_unlock(err_string_lock);
196
197
68
    return p;
198
68
}
199
#endif
200
201
void OSSL_ERR_STATE_free(ERR_STATE *state)
202
1
{
203
1
    int i;
204
205
1
    if (state == NULL)
206
0
        return;
207
17
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
208
16
        err_clear(state, i, 1);
209
16
    }
210
1
    CRYPTO_free(state, OPENSSL_FILE, OPENSSL_LINE);
211
1
}
212
213
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
214
1
{
215
1
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
216
0
        return 0;
217
1
    err_string_lock = CRYPTO_THREAD_lock_new();
218
1
    if (err_string_lock == NULL)
219
0
        return 0;
220
1
#ifndef OPENSSL_NO_ERR
221
1
    int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
222
1
                                            err_string_data_cmp);
223
1
    if (int_error_hash == NULL) {
224
0
        CRYPTO_THREAD_lock_free(err_string_lock);
225
0
        err_string_lock = NULL;
226
0
        return 0;
227
0
    }
228
1
#endif
229
1
    return 1;
230
1
}
231
232
void err_cleanup(void)
233
1
{
234
1
    if (set_err_thread_local != 0)
235
1
        CRYPTO_THREAD_cleanup_local(&err_thread_local);
236
1
    CRYPTO_THREAD_lock_free(err_string_lock);
237
1
    err_string_lock = NULL;
238
1
#ifndef OPENSSL_NO_ERR
239
1
    lh_ERR_STRING_DATA_free(int_error_hash);
240
1
    int_error_hash = NULL;
241
1
#endif
242
1
}
243
244
#ifndef OPENSSL_NO_ERR
245
/*
246
 * Legacy; pack in the library.
247
 */
248
static void err_patch(int lib, ERR_STRING_DATA *str)
249
0
{
250
0
    unsigned long plib = ERR_PACK(lib, 0, 0);
251
252
0
    for (; str->error != 0; str++)
253
0
        str->error |= plib;
254
0
}
255
256
/*
257
 * Hash in |str| error strings. Assumes the RUN_ONCE was done.
258
 */
259
static int err_load_strings(const ERR_STRING_DATA *str)
260
104
{
261
104
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
262
0
        return 0;
263
4.41k
    for (; str->error; str++)
264
4.31k
        (void)lh_ERR_STRING_DATA_insert(int_error_hash,
265
104
                                       (ERR_STRING_DATA *)str);
266
104
    CRYPTO_THREAD_unlock(err_string_lock);
267
104
    return 1;
268
104
}
269
#endif
270
271
int ossl_err_load_ERR_strings(void)
272
35
{
273
35
#ifndef OPENSSL_NO_ERR
274
35
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
275
0
        return 0;
276
277
35
    err_load_strings(ERR_str_libraries);
278
35
    err_load_strings(ERR_str_reasons);
279
35
#endif
280
35
    return 1;
281
35
}
282
283
int ERR_load_strings(int lib, ERR_STRING_DATA *str)
284
0
{
285
0
#ifndef OPENSSL_NO_ERR
286
0
    if (ossl_err_load_ERR_strings() == 0)
287
0
        return 0;
288
289
0
    err_patch(lib, str);
290
0
    err_load_strings(str);
291
0
#endif
292
293
0
    return 1;
294
0
}
295
296
int ERR_load_strings_const(const ERR_STRING_DATA *str)
297
34
{
298
34
#ifndef OPENSSL_NO_ERR
299
34
    if (ossl_err_load_ERR_strings() == 0)
300
0
        return 0;
301
34
    err_load_strings(str);
302
34
#endif
303
304
34
    return 1;
305
34
}
306
307
int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
308
0
{
309
0
#ifndef OPENSSL_NO_ERR
310
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
311
0
        return 0;
312
313
0
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
314
0
        return 0;
315
    /*
316
     * We don't need to ERR_PACK the lib, since that was done (to
317
     * the table) when it was loaded.
318
     */
319
0
    for (; str->error; str++)
320
0
        (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
321
0
    CRYPTO_THREAD_unlock(err_string_lock);
322
0
#endif
323
324
0
    return 1;
325
0
}
326
327
void err_free_strings_int(void)
328
0
{
329
    /* obsolete */
330
0
}
331
332
/********************************************************/
333
334
void ERR_clear_error(void)
335
0
{
336
0
    int i;
337
0
    ERR_STATE *es;
338
339
0
    es = ossl_err_get_state_int();
340
0
    if (es == NULL)
341
0
        return;
342
343
0
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
344
0
        err_clear(es, i, 0);
345
0
    }
346
0
    es->top = es->bottom = 0;
347
0
}
348
349
unsigned long ERR_get_error(void)
350
0
{
351
0
    return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
352
0
}
353
354
unsigned long ERR_get_error_all(const char **file, int *line,
355
                                const char **func,
356
                                const char **data, int *flags)
357
0
{
358
0
    return get_error_values(EV_POP, file, line, func, data, flags);
359
0
}
360
361
#ifndef OPENSSL_NO_DEPRECATED_3_0
362
unsigned long ERR_get_error_line(const char **file, int *line)
363
0
{
364
0
    return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
365
0
}
366
367
unsigned long ERR_get_error_line_data(const char **file, int *line,
368
                                      const char **data, int *flags)
369
0
{
370
0
    return get_error_values(EV_POP, file, line, NULL, data, flags);
371
0
}
372
#endif
373
374
unsigned long ERR_peek_error(void)
375
0
{
376
0
    return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
377
0
}
378
379
unsigned long ERR_peek_error_line(const char **file, int *line)
380
0
{
381
0
    return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
382
0
}
383
384
unsigned long ERR_peek_error_func(const char **func)
385
0
{
386
0
    return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
387
0
}
388
389
unsigned long ERR_peek_error_data(const char **data, int *flags)
390
0
{
391
0
    return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
392
0
}
393
394
unsigned long ERR_peek_error_all(const char **file, int *line,
395
                                 const char **func,
396
                                 const char **data, int *flags)
397
0
{
398
0
    return get_error_values(EV_PEEK, file, line, func, data, flags);
399
0
}
400
401
#ifndef OPENSSL_NO_DEPRECATED_3_0
402
unsigned long ERR_peek_error_line_data(const char **file, int *line,
403
                                       const char **data, int *flags)
404
0
{
405
0
    return get_error_values(EV_PEEK, file, line, NULL, data, flags);
406
0
}
407
#endif
408
409
unsigned long ERR_peek_last_error(void)
410
2
{
411
2
    return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
412
2
}
413
414
unsigned long ERR_peek_last_error_line(const char **file, int *line)
415
0
{
416
0
    return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
417
0
}
418
419
unsigned long ERR_peek_last_error_func(const char **func)
420
0
{
421
0
    return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
422
0
}
423
424
unsigned long ERR_peek_last_error_data(const char **data, int *flags)
425
0
{
426
0
    return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
427
0
}
428
429
unsigned long ERR_peek_last_error_all(const char **file, int *line,
430
                                      const char **func,
431
                                      const char **data, int *flags)
432
0
{
433
0
    return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
434
0
}
435
436
#ifndef OPENSSL_NO_DEPRECATED_3_0
437
unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
438
                                            const char **data, int *flags)
439
0
{
440
0
    return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
441
0
}
442
#endif
443
444
static unsigned long get_error_values(ERR_GET_ACTION g,
445
                                      const char **file, int *line,
446
                                      const char **func,
447
                                      const char **data, int *flags)
448
2
{
449
2
    int i = 0;
450
2
    ERR_STATE *es;
451
2
    unsigned long ret;
452
453
2
    es = ossl_err_get_state_int();
454
2
    if (es == NULL)
455
0
        return 0;
456
457
    /*
458
     * Clear anything that should have been cleared earlier. We do this
459
     * here because this doesn't have constant-time issues.
460
     */
461
2
    while (es->bottom != es->top) {
462
2
        if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
463
0
            err_clear(es, es->top, 0);
464
0
            es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
465
0
            continue;
466
0
        }
467
2
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
468
2
        if (es->err_flags[i] & ERR_FLAG_CLEAR) {
469
0
            es->bottom = i;
470
0
            err_clear(es, es->bottom, 0);
471
0
            continue;
472
0
        }
473
2
        break;
474
2
    }
475
476
    /* If everything has been cleared, the stack is empty. */
477
2
    if (es->bottom == es->top)
478
0
        return 0;
479
480
    /* Which error, the top of stack (latest one) or the first one? */
481
2
    if (g == EV_PEEK_LAST)
482
2
        i = es->top;
483
0
    else
484
0
        i = (es->bottom + 1) % ERR_NUM_ERRORS;
485
486
2
    ret = es->err_buffer[i];
487
2
    if (g == EV_POP) {
488
0
        es->bottom = i;
489
0
        es->err_buffer[i] = 0;
490
0
    }
491
492
2
    if (file != NULL) {
493
0
        *file = es->err_file[i];
494
0
        if (*file == NULL)
495
0
            *file = "";
496
0
    }
497
2
    if (line != NULL)
498
0
        *line = es->err_line[i];
499
2
    if (func != NULL) {
500
0
        *func = es->err_func[i];
501
0
        if (*func == NULL)
502
0
            *func = "";
503
0
    }
504
2
    if (flags != NULL)
505
0
        *flags = es->err_data_flags[i];
506
2
    if (data == NULL) {
507
2
        if (g == EV_POP) {
508
0
            err_clear_data(es, i, 0);
509
0
        }
510
2
    } else {
511
0
        *data = es->err_data[i];
512
0
        if (*data == NULL) {
513
0
            *data = "";
514
0
            if (flags != NULL)
515
0
                *flags = 0;
516
0
        }
517
0
    }
518
2
    return ret;
519
2
}
520
521
void ossl_err_string_int(unsigned long e, const char *func,
522
                         char *buf, size_t len)
523
0
{
524
0
    char lsbuf[64], rsbuf[256];
525
0
    const char *ls, *rs = NULL;
526
0
    unsigned long l, r;
527
528
0
    if (len == 0)
529
0
        return;
530
531
0
    l = ERR_GET_LIB(e);
532
0
    ls = ERR_lib_error_string(e);
533
0
    if (ls == NULL) {
534
0
        BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
535
0
        ls = lsbuf;
536
0
    }
537
538
    /*
539
     * ERR_reason_error_string() can't safely return system error strings,
540
     * since it would call openssl_strerror_r(), which needs a buffer for
541
     * thread safety.  So for system errors, we call openssl_strerror_r()
542
     * directly instead.
543
     */
544
0
    r = ERR_GET_REASON(e);
545
0
#ifndef OPENSSL_NO_ERR
546
0
    if (ERR_SYSTEM_ERROR(e)) {
547
0
        if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
548
0
            rs = rsbuf;
549
0
    } else {
550
0
        rs = ERR_reason_error_string(e);
551
0
    }
552
0
#endif
553
0
    if (rs == NULL) {
554
0
        BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)",
555
0
                     r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET));
556
0
        rs = rsbuf;
557
0
    }
558
559
0
    BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
560
0
    if (strlen(buf) == len - 1) {
561
        /* Didn't fit; use a minimal format. */
562
0
        BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
563
0
    }
564
0
}
565
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
0
{
588
0
#ifndef OPENSSL_NO_ERR
589
0
    ERR_STRING_DATA d, *p;
590
0
    unsigned long l;
591
592
0
    if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
593
0
        return NULL;
594
0
    }
595
596
0
    l = ERR_GET_LIB(e);
597
0
    d.error = ERR_PACK(l, 0, 0);
598
0
    p = int_err_get_item(&d);
599
0
    return ((p == NULL) ? NULL : p->string);
600
#else
601
    return NULL;
602
#endif
603
0
}
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
34
{
614
34
#ifndef OPENSSL_NO_ERR
615
34
    ERR_STRING_DATA d, *p = NULL;
616
34
    unsigned long l, r;
617
618
34
    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
34
    if (ERR_SYSTEM_ERROR(e))
628
0
        return NULL;
629
630
34
    l = ERR_GET_LIB(e);
631
34
    r = ERR_GET_REASON(e);
632
34
    d.error = ERR_PACK(l, 0, r);
633
34
    p = int_err_get_item(&d);
634
34
    if (p == NULL) {
635
34
        d.error = ERR_PACK(0, 0, r);
636
34
        p = int_err_get_item(&d);
637
34
    }
638
34
    return ((p == NULL) ? NULL : p->string);
639
#else
640
    return NULL;
641
#endif
642
34
}
643
644
static void err_delete_thread_state(void *unused)
645
1
{
646
1
    ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
647
1
    if (state == NULL)
648
0
        return;
649
650
1
    CRYPTO_THREAD_set_local(&err_thread_local, NULL);
651
1
    OSSL_ERR_STATE_free(state);
652
1
}
653
654
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
655
void ERR_remove_thread_state(void *dummy)
656
0
{
657
0
}
658
#endif
659
660
#ifndef OPENSSL_NO_DEPRECATED_1_0_0
661
void ERR_remove_state(unsigned long pid)
662
0
{
663
0
}
664
#endif
665
666
DEFINE_RUN_ONCE_STATIC(err_do_init)
667
1
{
668
1
    set_err_thread_local = 1;
669
1
    return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
670
1
}
671
672
ERR_STATE *ossl_err_get_state_int(void)
673
2.35k
{
674
2.35k
    ERR_STATE *state;
675
2.35k
    int saveerrno = get_last_sys_error();
676
677
2.35k
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
678
0
        return NULL;
679
680
2.35k
    if (!RUN_ONCE(&err_init, err_do_init))
681
0
        return NULL;
682
683
2.35k
    state = CRYPTO_THREAD_get_local(&err_thread_local);
684
2.35k
    if (state == (ERR_STATE*)-1)
685
0
        return NULL;
686
687
2.35k
    if (state == NULL) {
688
1
        if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
689
0
            return NULL;
690
691
1
        state = OSSL_ERR_STATE_new();
692
1
        if (state == NULL) {
693
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
694
0
            return NULL;
695
0
        }
696
697
1
        if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
698
1
                || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
699
0
            OSSL_ERR_STATE_free(state);
700
0
            CRYPTO_THREAD_set_local(&err_thread_local, NULL);
701
0
            return NULL;
702
0
        }
703
704
        /* Ignore failures from these */
705
1
        OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
706
1
    }
707
708
2.35k
    set_sys_error(saveerrno);
709
2.35k
    return state;
710
2.35k
}
711
712
#ifndef OPENSSL_NO_DEPRECATED_3_0
713
ERR_STATE *ERR_get_state(void)
714
0
{
715
0
    return ossl_err_get_state_int();
716
0
}
717
#endif
718
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
1
{
726
1
    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
1
    if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
741
0
        return 0;
742
743
1
    if (!RUN_ONCE(&err_init, err_do_init))
744
0
        return 0;
745
746
1
    *state = CRYPTO_THREAD_get_local(&err_thread_local);
747
1
    if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
748
0
        return 0;
749
750
1
    set_sys_error(saveerrno);
751
1
    return 1;
752
1
}
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
1
{
760
1
    if (state != (void*)-1)
761
1
        CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
762
1
}
763
764
int ERR_get_next_error_library(void)
765
4
{
766
4
    int ret;
767
768
4
    if (!RUN_ONCE(&err_string_init, do_err_strings_init))
769
0
        return 0;
770
771
4
    if (!CRYPTO_THREAD_write_lock(err_string_lock))
772
0
        return 0;
773
4
    ret = int_err_library_number++;
774
4
    CRYPTO_THREAD_unlock(err_string_lock);
775
4
    return ret;
776
4
}
777
778
static int err_set_error_data_int(char *data, size_t size, int flags,
779
                                  int deallocate)
780
0
{
781
0
    ERR_STATE *es;
782
783
0
    es = ossl_err_get_state_int();
784
0
    if (es == NULL)
785
0
        return 0;
786
787
0
    err_clear_data(es, es->top, deallocate);
788
0
    err_set_data(es, es->top, data, size, flags);
789
790
0
    return 1;
791
0
}
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
0
{
814
0
    va_list args;
815
0
    va_start(args, num);
816
0
    ERR_add_error_vdata(num, args);
817
0
    va_end(args);
818
0
}
819
820
void ERR_add_error_vdata(int num, va_list args)
821
0
{
822
0
    int i, len, size;
823
0
    int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
824
0
    char *str, *arg;
825
0
    ERR_STATE *es;
826
827
    /* Get the current error data; if an allocated string get it. */
828
0
    es = ossl_err_get_state_int();
829
0
    if (es == NULL)
830
0
        return;
831
0
    i = es->top;
832
833
    /*
834
     * If err_data is allocated already, reuse the space.
835
     * Otherwise, allocate a small new buffer.
836
     */
837
0
    if ((es->err_data_flags[i] & flags) == flags
838
0
            && ossl_assert(es->err_data[i] != NULL)) {
839
0
        str = es->err_data[i];
840
0
        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
0
        es->err_data[i] = NULL;
850
0
        es->err_data_flags[i] = 0;
851
0
    } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
852
0
        return;
853
0
    } else {
854
0
        str[0] = '\0';
855
0
    }
856
0
    len = strlen(str);
857
858
0
    while (--num >= 0) {
859
0
        arg = va_arg(args, char *);
860
0
        if (arg == NULL)
861
0
            arg = "<NULL>";
862
0
        len += strlen(arg);
863
0
        if (len >= size) {
864
0
            char *p;
865
866
0
            size = len + 20;
867
0
            p = OPENSSL_realloc(str, size);
868
0
            if (p == NULL) {
869
0
                OPENSSL_free(str);
870
0
                return;
871
0
            }
872
0
            str = p;
873
0
        }
874
0
        OPENSSL_strlcat(str, arg, (size_t)size);
875
0
    }
876
0
    if (!err_set_error_data_int(str, size, flags, 0))
877
0
        OPENSSL_free(str);
878
0
}
879
880
void err_clear_last_constant_time(int clear)
881
0
{
882
0
    ERR_STATE *es;
883
0
    int top;
884
885
0
    es = ossl_err_get_state_int();
886
0
    if (es == NULL)
887
0
        return;
888
889
0
    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
0
    clear = constant_time_select_int(constant_time_eq_int(clear, 0),
896
0
                                     0, ERR_FLAG_CLEAR);
897
0
    es->err_flags[top] |= clear;
898
0
}