Coverage Report

Created: 2024-07-27 06:37

/src/openssl/ssl/ssl_init.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-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
#include "internal/e_os.h"
11
12
#include "internal/err.h"
13
#include <openssl/crypto.h>
14
#include <openssl/evp.h>
15
#include <openssl/trace.h>
16
#include "ssl_local.h"
17
#include "sslerr.h"
18
#include "internal/thread_once.h"
19
20
static int stopped;
21
22
static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
23
static int ssl_base_inited = 0;
24
DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)
25
2
{
26
2
#ifndef OPENSSL_NO_COMP
27
2
    OSSL_TRACE(INIT, "ossl_init_ssl_base: "
28
2
               "SSL_COMP_get_compression_methods()\n");
29
    /*
30
     * This will initialise the built-in compression algorithms. The value
31
     * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
32
     */
33
2
    SSL_COMP_get_compression_methods();
34
2
#endif
35
2
    ssl_sort_cipher_list();
36
2
    OSSL_TRACE(INIT, "ossl_init_ssl_base: SSL_add_ssl_module()\n");
37
2
    ssl_base_inited = 1;
38
2
    return 1;
39
2
}
40
41
static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
42
43
DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
44
2
{
45
    /*
46
     * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
47
     * pulling in all the error strings during static linking
48
     */
49
2
#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
50
2
    OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ossl_err_load_SSL_strings()\n");
51
2
    ossl_err_load_SSL_strings();
52
2
#endif
53
2
    return 1;
54
2
}
55
56
DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,
57
                           ossl_init_load_ssl_strings)
58
0
{
59
    /* Do nothing in this case */
60
0
    return 1;
61
0
}
62
63
/*
64
 * If this function is called with a non NULL settings value then it must be
65
 * called prior to any threads making calls to any OpenSSL functions,
66
 * i.e. passing a non-null settings value is assumed to be single-threaded.
67
 */
68
int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
69
14.3k
{
70
14.3k
    static int stoperrset = 0;
71
72
14.3k
    if (stopped) {
73
0
        if (!stoperrset) {
74
            /*
75
             * We only ever set this once to avoid getting into an infinite
76
             * loop where the error system keeps trying to init and fails so
77
             * sets an error etc
78
             */
79
0
            stoperrset = 1;
80
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INIT_FAIL);
81
0
        }
82
0
        return 0;
83
0
    }
84
85
14.3k
    opts |= OPENSSL_INIT_ADD_ALL_CIPHERS
86
14.3k
         |  OPENSSL_INIT_ADD_ALL_DIGESTS;
87
14.3k
#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
88
14.3k
    if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
89
14.3k
        opts |= OPENSSL_INIT_LOAD_CONFIG;
90
14.3k
#endif
91
92
14.3k
    if (!OPENSSL_init_crypto(opts, settings))
93
0
        return 0;
94
95
14.3k
    if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))
96
0
        return 0;
97
98
14.3k
    if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
99
14.3k
        && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
100
14.3k
                         ossl_init_load_ssl_strings))
101
0
        return 0;
102
103
14.3k
    if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
104
14.3k
        && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
105
0
        return 0;
106
107
14.3k
    return 1;
108
14.3k
}