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