]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_init.c
Raise an error on syscall failure in tls_retry_write_records
[thirdparty/openssl.git] / ssl / ssl_init.c
CommitLineData
b184e3ef 1/*
556009c5 2 * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
b184e3ef 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
b184e3ef
MC
8 */
9
d5f9166b 10#include "internal/e_os.h"
b184e3ef 11
6827cb36 12#include "internal/err.h"
b184e3ef
MC
13#include <openssl/crypto.h>
14#include <openssl/evp.h>
5c641735 15#include <openssl/trace.h>
706457b7 16#include "ssl_local.h"
f5a46ed7 17#include "sslerr.h"
c2e4e5d2 18#include "internal/thread_once.h"
b184e3ef 19
dd27f16e
RS
20static int stopped;
21
b184e3ef
MC
22static void ssl_library_stop(void);
23
b1f1e7ae 24static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
b184e3ef 25static int ssl_base_inited = 0;
c2e4e5d2 26DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)
b184e3ef 27{
b184e3ef 28#ifndef OPENSSL_NO_COMP
5c641735
RL
29 OSSL_TRACE(INIT, "ossl_init_ssl_base: "
30 "SSL_COMP_get_compression_methods()\n");
b184e3ef
MC
31 /*
32 * This will initialise the built-in compression algorithms. The value
33 * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
34 */
35 SSL_COMP_get_compression_methods();
36#endif
c8f6c28a 37 ssl_sort_cipher_list();
1287dabd 38 OSSL_TRACE(INIT, "ossl_init_ssl_base: SSL_add_ssl_module()\n");
b184e3ef
MC
39 /*
40 * We ignore an error return here. Not much we can do - but not that bad
41 * either. We can still safely continue.
42 */
f672aee4 43 OPENSSL_atexit(ssl_library_stop);
b184e3ef 44 ssl_base_inited = 1;
c2e4e5d2 45 return 1;
b184e3ef
MC
46}
47
b1f1e7ae 48static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
1c8787d5 49
c2e4e5d2 50DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
b184e3ef 51{
498abff0
MC
52 /*
53 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
54 * pulling in all the error strings during static linking
55 */
56#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
b93f6c2d
P
57 OSSL_TRACE(INIT, "ossl_init_load_ssl_strings: ossl_err_load_SSL_strings()\n");
58 ossl_err_load_SSL_strings();
10281e83 59#endif
c2e4e5d2 60 return 1;
b184e3ef
MC
61}
62
660a1e04
MC
63DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,
64 ossl_init_load_ssl_strings)
b184e3ef
MC
65{
66 /* Do nothing in this case */
c2e4e5d2 67 return 1;
b184e3ef
MC
68}
69
70static void ssl_library_stop(void)
71{
dd27f16e
RS
72 /* Might be explicitly called and also by atexit */
73 if (stopped)
74 return;
75 stopped = 1;
76
b184e3ef
MC
77 if (ssl_base_inited) {
78#ifndef OPENSSL_NO_COMP
5c641735
RL
79 OSSL_TRACE(INIT, "ssl_library_stop: "
80 "ssl_comp_free_compression_methods_int()\n");
b3599dbb 81 ssl_comp_free_compression_methods_int();
b184e3ef
MC
82#endif
83 }
b184e3ef
MC
84}
85
86/*
87 * If this function is called with a non NULL settings value then it must be
88 * called prior to any threads making calls to any OpenSSL functions,
89 * i.e. passing a non-null settings value is assumed to be single-threaded.
90 */
bbaeadb0 91int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
b184e3ef 92{
302f7588
MC
93 static int stoperrset = 0;
94
95 if (stopped) {
96 if (!stoperrset) {
97 /*
98 * We only ever set this once to avoid getting into an infinite
99 * loop where the error system keeps trying to init and fails so
100 * sets an error etc
101 */
102 stoperrset = 1;
6849b73c 103 ERR_raise(ERR_LIB_SSL, ERR_R_INIT_FAIL);
302f7588 104 }
0fc32b07 105 return 0;
302f7588 106 }
dd27f16e 107
df1f538f 108 opts |= OPENSSL_INIT_ADD_ALL_CIPHERS
e74bd290 109 | OPENSSL_INIT_ADD_ALL_DIGESTS;
dbabc862 110#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
df1f538f
VD
111 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
112 opts |= OPENSSL_INIT_LOAD_CONFIG;
dbabc862 113#endif
df1f538f
VD
114
115 if (!OPENSSL_init_crypto(opts, settings))
0fc32b07 116 return 0;
b184e3ef 117
d8f031e8 118 if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))
b1f1e7ae 119 return 0;
b184e3ef 120
b1f1e7ae 121 if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
660a1e04
MC
122 && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
123 ossl_init_load_ssl_strings))
b1f1e7ae 124 return 0;
b184e3ef 125
b1f1e7ae 126 if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
a230b26e 127 && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
b1f1e7ae 128 return 0;
0fc32b07
MC
129
130 return 1;
b184e3ef 131}