ssl_init.c 3.1 KB

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