xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/ssl/ssl_init.c (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos /*
2*4724848cSchristos  * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos  *
4*4724848cSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*4724848cSchristos  * this file except in compliance with the License.  You can obtain a copy
6*4724848cSchristos  * in the file LICENSE in the source distribution or at
7*4724848cSchristos  * https://www.openssl.org/source/license.html
8*4724848cSchristos  */
9*4724848cSchristos 
10*4724848cSchristos #include "e_os.h"
11*4724848cSchristos 
12*4724848cSchristos #include "internal/err.h"
13*4724848cSchristos #include <openssl/crypto.h>
14*4724848cSchristos #include <openssl/evp.h>
15*4724848cSchristos #include "ssl_local.h"
16*4724848cSchristos #include "internal/thread_once.h"
17*4724848cSchristos 
18*4724848cSchristos static int stopped;
19*4724848cSchristos 
20*4724848cSchristos static void ssl_library_stop(void);
21*4724848cSchristos 
22*4724848cSchristos static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
23*4724848cSchristos static int ssl_base_inited = 0;
DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)24*4724848cSchristos DEFINE_RUN_ONCE_STATIC(ossl_init_ssl_base)
25*4724848cSchristos {
26*4724848cSchristos #ifdef OPENSSL_INIT_DEBUG
27*4724848cSchristos     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
28*4724848cSchristos             "Adding SSL ciphers and digests\n");
29*4724848cSchristos #endif
30*4724848cSchristos #ifndef OPENSSL_NO_DES
31*4724848cSchristos     EVP_add_cipher(EVP_des_cbc());
32*4724848cSchristos     EVP_add_cipher(EVP_des_ede3_cbc());
33*4724848cSchristos #endif
34*4724848cSchristos #ifndef OPENSSL_NO_IDEA
35*4724848cSchristos     EVP_add_cipher(EVP_idea_cbc());
36*4724848cSchristos #endif
37*4724848cSchristos #ifndef OPENSSL_NO_RC4
38*4724848cSchristos     EVP_add_cipher(EVP_rc4());
39*4724848cSchristos # ifndef OPENSSL_NO_MD5
40*4724848cSchristos     EVP_add_cipher(EVP_rc4_hmac_md5());
41*4724848cSchristos # endif
42*4724848cSchristos #endif
43*4724848cSchristos #ifndef OPENSSL_NO_RC2
44*4724848cSchristos     EVP_add_cipher(EVP_rc2_cbc());
45*4724848cSchristos     /*
46*4724848cSchristos      * Not actually used for SSL/TLS but this makes PKCS#12 work if an
47*4724848cSchristos      * application only calls SSL_library_init().
48*4724848cSchristos      */
49*4724848cSchristos     EVP_add_cipher(EVP_rc2_40_cbc());
50*4724848cSchristos #endif
51*4724848cSchristos     EVP_add_cipher(EVP_aes_128_cbc());
52*4724848cSchristos     EVP_add_cipher(EVP_aes_192_cbc());
53*4724848cSchristos     EVP_add_cipher(EVP_aes_256_cbc());
54*4724848cSchristos     EVP_add_cipher(EVP_aes_128_gcm());
55*4724848cSchristos     EVP_add_cipher(EVP_aes_256_gcm());
56*4724848cSchristos     EVP_add_cipher(EVP_aes_128_ccm());
57*4724848cSchristos     EVP_add_cipher(EVP_aes_256_ccm());
58*4724848cSchristos     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1());
59*4724848cSchristos     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1());
60*4724848cSchristos     EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
61*4724848cSchristos     EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
62*4724848cSchristos #ifndef OPENSSL_NO_ARIA
63*4724848cSchristos     EVP_add_cipher(EVP_aria_128_gcm());
64*4724848cSchristos     EVP_add_cipher(EVP_aria_256_gcm());
65*4724848cSchristos #endif
66*4724848cSchristos #ifndef OPENSSL_NO_CAMELLIA
67*4724848cSchristos     EVP_add_cipher(EVP_camellia_128_cbc());
68*4724848cSchristos     EVP_add_cipher(EVP_camellia_256_cbc());
69*4724848cSchristos #endif
70*4724848cSchristos #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
71*4724848cSchristos     EVP_add_cipher(EVP_chacha20_poly1305());
72*4724848cSchristos #endif
73*4724848cSchristos 
74*4724848cSchristos #ifndef OPENSSL_NO_SEED
75*4724848cSchristos     EVP_add_cipher(EVP_seed_cbc());
76*4724848cSchristos #endif
77*4724848cSchristos 
78*4724848cSchristos #ifndef OPENSSL_NO_MD5
79*4724848cSchristos     EVP_add_digest(EVP_md5());
80*4724848cSchristos     EVP_add_digest_alias(SN_md5, "ssl3-md5");
81*4724848cSchristos     EVP_add_digest(EVP_md5_sha1());
82*4724848cSchristos #endif
83*4724848cSchristos     EVP_add_digest(EVP_sha1()); /* RSA with sha1 */
84*4724848cSchristos     EVP_add_digest_alias(SN_sha1, "ssl3-sha1");
85*4724848cSchristos     EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA);
86*4724848cSchristos     EVP_add_digest(EVP_sha224());
87*4724848cSchristos     EVP_add_digest(EVP_sha256());
88*4724848cSchristos     EVP_add_digest(EVP_sha384());
89*4724848cSchristos     EVP_add_digest(EVP_sha512());
90*4724848cSchristos #ifndef OPENSSL_NO_COMP
91*4724848cSchristos # ifdef OPENSSL_INIT_DEBUG
92*4724848cSchristos     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
93*4724848cSchristos             "SSL_COMP_get_compression_methods()\n");
94*4724848cSchristos # endif
95*4724848cSchristos     /*
96*4724848cSchristos      * This will initialise the built-in compression algorithms. The value
97*4724848cSchristos      * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
98*4724848cSchristos      */
99*4724848cSchristos     SSL_COMP_get_compression_methods();
100*4724848cSchristos #endif
101*4724848cSchristos     /* initialize cipher/digest methods table */
102*4724848cSchristos     if (!ssl_load_ciphers())
103*4724848cSchristos         return 0;
104*4724848cSchristos 
105*4724848cSchristos #ifdef OPENSSL_INIT_DEBUG
106*4724848cSchristos     fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
107*4724848cSchristos             "SSL_add_ssl_module()\n");
108*4724848cSchristos #endif
109*4724848cSchristos     /*
110*4724848cSchristos      * We ignore an error return here. Not much we can do - but not that bad
111*4724848cSchristos      * either. We can still safely continue.
112*4724848cSchristos      */
113*4724848cSchristos     OPENSSL_atexit(ssl_library_stop);
114*4724848cSchristos     ssl_base_inited = 1;
115*4724848cSchristos     return 1;
116*4724848cSchristos }
117*4724848cSchristos 
118*4724848cSchristos static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
119*4724848cSchristos 
DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)120*4724848cSchristos DEFINE_RUN_ONCE_STATIC(ossl_init_load_ssl_strings)
121*4724848cSchristos {
122*4724848cSchristos     /*
123*4724848cSchristos      * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
124*4724848cSchristos      * pulling in all the error strings during static linking
125*4724848cSchristos      */
126*4724848cSchristos #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
127*4724848cSchristos # ifdef OPENSSL_INIT_DEBUG
128*4724848cSchristos     fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: "
129*4724848cSchristos             "ERR_load_SSL_strings()\n");
130*4724848cSchristos # endif
131*4724848cSchristos     ERR_load_SSL_strings();
132*4724848cSchristos #endif
133*4724848cSchristos     return 1;
134*4724848cSchristos }
135*4724848cSchristos 
DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,ossl_init_load_ssl_strings)136*4724848cSchristos DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_no_load_ssl_strings,
137*4724848cSchristos                            ossl_init_load_ssl_strings)
138*4724848cSchristos {
139*4724848cSchristos     /* Do nothing in this case */
140*4724848cSchristos     return 1;
141*4724848cSchristos }
142*4724848cSchristos 
ssl_library_stop(void)143*4724848cSchristos static void ssl_library_stop(void)
144*4724848cSchristos {
145*4724848cSchristos     /* Might be explicitly called and also by atexit */
146*4724848cSchristos     if (stopped)
147*4724848cSchristos         return;
148*4724848cSchristos     stopped = 1;
149*4724848cSchristos 
150*4724848cSchristos     if (ssl_base_inited) {
151*4724848cSchristos #ifndef OPENSSL_NO_COMP
152*4724848cSchristos # ifdef OPENSSL_INIT_DEBUG
153*4724848cSchristos         fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
154*4724848cSchristos                 "ssl_comp_free_compression_methods_int()\n");
155*4724848cSchristos # endif
156*4724848cSchristos         ssl_comp_free_compression_methods_int();
157*4724848cSchristos #endif
158*4724848cSchristos     }
159*4724848cSchristos }
160*4724848cSchristos 
161*4724848cSchristos /*
162*4724848cSchristos  * If this function is called with a non NULL settings value then it must be
163*4724848cSchristos  * called prior to any threads making calls to any OpenSSL functions,
164*4724848cSchristos  * i.e. passing a non-null settings value is assumed to be single-threaded.
165*4724848cSchristos  */
OPENSSL_init_ssl(uint64_t opts,const OPENSSL_INIT_SETTINGS * settings)166*4724848cSchristos int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings)
167*4724848cSchristos {
168*4724848cSchristos     static int stoperrset = 0;
169*4724848cSchristos 
170*4724848cSchristos     if (stopped) {
171*4724848cSchristos         if (!stoperrset) {
172*4724848cSchristos             /*
173*4724848cSchristos              * We only ever set this once to avoid getting into an infinite
174*4724848cSchristos              * loop where the error system keeps trying to init and fails so
175*4724848cSchristos              * sets an error etc
176*4724848cSchristos              */
177*4724848cSchristos             stoperrset = 1;
178*4724848cSchristos             SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL);
179*4724848cSchristos         }
180*4724848cSchristos         return 0;
181*4724848cSchristos     }
182*4724848cSchristos 
183*4724848cSchristos     opts |= OPENSSL_INIT_ADD_ALL_CIPHERS
184*4724848cSchristos          |  OPENSSL_INIT_ADD_ALL_DIGESTS;
185*4724848cSchristos #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
186*4724848cSchristos     if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) == 0)
187*4724848cSchristos         opts |= OPENSSL_INIT_LOAD_CONFIG;
188*4724848cSchristos #endif
189*4724848cSchristos 
190*4724848cSchristos     if (!OPENSSL_init_crypto(opts, settings))
191*4724848cSchristos         return 0;
192*4724848cSchristos 
193*4724848cSchristos     if (!RUN_ONCE(&ssl_base, ossl_init_ssl_base))
194*4724848cSchristos         return 0;
195*4724848cSchristos 
196*4724848cSchristos     if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
197*4724848cSchristos         && !RUN_ONCE_ALT(&ssl_strings, ossl_init_no_load_ssl_strings,
198*4724848cSchristos                          ossl_init_load_ssl_strings))
199*4724848cSchristos         return 0;
200*4724848cSchristos 
201*4724848cSchristos     if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
202*4724848cSchristos         && !RUN_ONCE(&ssl_strings, ossl_init_load_ssl_strings))
203*4724848cSchristos         return 0;
204*4724848cSchristos 
205*4724848cSchristos     return 1;
206*4724848cSchristos }
207