2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include "internal/err.h"
13 #include <openssl/crypto.h>
14 #include <openssl/evp.h>
20 static void ssl_library_stop(void);
22 static CRYPTO_ONCE ssl_base = CRYPTO_ONCE_STATIC_INIT;
23 static int ssl_base_inited = 0;
24 static void ossl_init_ssl_base(void)
26 #ifdef OPENSSL_INIT_DEBUG
27 fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
28 "Adding SSL ciphers and digests\n");
30 #ifndef OPENSSL_NO_DES
31 EVP_add_cipher(EVP_des_cbc());
32 EVP_add_cipher(EVP_des_ede3_cbc());
34 #ifndef OPENSSL_NO_IDEA
35 EVP_add_cipher(EVP_idea_cbc());
37 #ifndef OPENSSL_NO_RC4
38 EVP_add_cipher(EVP_rc4());
39 # ifndef OPENSSL_NO_MD5
40 EVP_add_cipher(EVP_rc4_hmac_md5());
43 #ifndef OPENSSL_NO_RC2
44 EVP_add_cipher(EVP_rc2_cbc());
46 * Not actually used for SSL/TLS but this makes PKCS#12 work if an
47 * application only calls SSL_library_init().
49 EVP_add_cipher(EVP_rc2_40_cbc());
51 EVP_add_cipher(EVP_aes_128_cbc());
52 EVP_add_cipher(EVP_aes_192_cbc());
53 EVP_add_cipher(EVP_aes_256_cbc());
54 EVP_add_cipher(EVP_aes_128_gcm());
55 EVP_add_cipher(EVP_aes_256_gcm());
56 EVP_add_cipher(EVP_aes_128_ccm());
57 EVP_add_cipher(EVP_aes_256_ccm());
58 EVP_add_cipher(EVP_aes_128_cbc_hmac_sha1());
59 EVP_add_cipher(EVP_aes_256_cbc_hmac_sha1());
60 EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
61 EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
62 #ifndef OPENSSL_NO_CAMELLIA
63 EVP_add_cipher(EVP_camellia_128_cbc());
64 EVP_add_cipher(EVP_camellia_256_cbc());
66 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
67 EVP_add_cipher(EVP_chacha20_poly1305());
70 #ifndef OPENSSL_NO_SEED
71 EVP_add_cipher(EVP_seed_cbc());
74 #ifndef OPENSSL_NO_MD5
75 EVP_add_digest(EVP_md5());
76 EVP_add_digest_alias(SN_md5, "ssl3-md5");
77 EVP_add_digest(EVP_md5_sha1());
79 EVP_add_digest(EVP_sha1()); /* RSA with sha1 */
80 EVP_add_digest_alias(SN_sha1, "ssl3-sha1");
81 EVP_add_digest_alias(SN_sha1WithRSAEncryption, SN_sha1WithRSA);
82 EVP_add_digest(EVP_sha224());
83 EVP_add_digest(EVP_sha256());
84 EVP_add_digest(EVP_sha384());
85 EVP_add_digest(EVP_sha512());
86 #ifndef OPENSSL_NO_COMP
87 #ifdef OPENSSL_INIT_DEBUG
88 fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
89 "SSL_COMP_get_compression_methods()\n");
92 * This will initialise the built-in compression algorithms. The value
93 * returned is a STACK_OF(SSL_COMP), but that can be discarded safely
95 SSL_COMP_get_compression_methods();
97 /* initialize cipher/digest methods table */
100 #ifdef OPENSSL_INIT_DEBUG
101 fprintf(stderr, "OPENSSL_INIT: ossl_init_ssl_base: "
102 "SSL_add_ssl_module()\n");
104 SSL_add_ssl_module();
106 * We ignore an error return here. Not much we can do - but not that bad
107 * either. We can still safely continue.
109 OPENSSL_atexit(ssl_library_stop);
113 static CRYPTO_ONCE ssl_strings = CRYPTO_ONCE_STATIC_INIT;
114 static int ssl_strings_inited = 0;
115 static void ossl_init_load_ssl_strings(void)
118 * OPENSSL_NO_AUTOERRINIT is provided here to prevent at compile time
119 * pulling in all the error strings during static linking
121 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
122 # ifdef OPENSSL_INIT_DEBUG
123 fprintf(stderr, "OPENSSL_INIT: ossl_init_load_ssl_strings: "
124 "ERR_load_SSL_strings()\n");
126 ERR_load_SSL_strings();
128 ssl_strings_inited = 1;
131 static void ossl_init_no_load_ssl_strings(void)
133 /* Do nothing in this case */
137 static void ssl_library_stop(void)
139 /* Might be explicitly called and also by atexit */
144 if (ssl_base_inited) {
145 #ifndef OPENSSL_NO_COMP
146 #ifdef OPENSSL_INIT_DEBUG
147 fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
148 "ssl_comp_free_compression_methods_int()\n");
150 ssl_comp_free_compression_methods_int();
154 if (ssl_strings_inited) {
155 #ifdef OPENSSL_INIT_DEBUG
156 fprintf(stderr, "OPENSSL_INIT: ssl_library_stop: "
157 "err_free_strings_int()\n");
160 * If both crypto and ssl error strings are inited we will end up
161 * calling err_free_strings_int() twice - but that's ok. The second
162 * time will be a no-op. It's easier to do that than to try and track
163 * between the two libraries whether they have both been inited.
165 err_free_strings_int();
171 * If this function is called with a non NULL settings value then it must be
172 * called prior to any threads making calls to any OpenSSL functions,
173 * i.e. passing a non-null settings value is assumed to be single-threaded.
175 int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings)
177 static int stoperrset = 0;
182 * We only ever set this once to avoid getting into an infinite
183 * loop where the error system keeps trying to init and fails so
187 SSLerr(SSL_F_OPENSSL_INIT_SSL, ERR_R_INIT_FAIL);
192 if (!OPENSSL_init_crypto(opts | OPENSSL_INIT_ADD_ALL_CIPHERS
193 | OPENSSL_INIT_ADD_ALL_DIGESTS, settings))
196 if (!CRYPTO_THREAD_run_once(&ssl_base, ossl_init_ssl_base))
199 if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS)
200 && !CRYPTO_THREAD_run_once(&ssl_strings,
201 ossl_init_no_load_ssl_strings))
204 if ((opts & OPENSSL_INIT_LOAD_SSL_STRINGS)
205 && !CRYPTO_THREAD_run_once(&ssl_strings,
206 ossl_init_load_ssl_strings))