1 /* $OpenBSD: crypto_init.c,v 1.11 2023/07/08 08:28:23 beck Exp $ */ 2 /* 3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 /* OpenSSL style init */ 19 20 #include <pthread.h> 21 #include <stdio.h> 22 23 #include <openssl/asn1.h> 24 #include <openssl/conf.h> 25 #ifndef OPENSSL_NO_ENGINE 26 #include <openssl/engine.h> 27 #endif 28 #include <openssl/err.h> 29 #include <openssl/evp.h> 30 #include <openssl/objects.h> 31 #include <openssl/x509v3.h> 32 33 #include "cryptlib.h" 34 #include "x509_issuer_cache.h" 35 36 int OpenSSL_config(const char *); 37 int OpenSSL_no_config(void); 38 39 static pthread_once_t crypto_init_once = PTHREAD_ONCE_INIT; 40 static pthread_t crypto_init_thread; 41 static int crypto_init_cleaned_up; 42 43 static void 44 OPENSSL_init_crypto_internal(void) 45 { 46 crypto_init_thread = pthread_self(); 47 48 OPENSSL_cpuid_setup(); 49 ERR_load_crypto_strings(); 50 OpenSSL_add_all_ciphers(); 51 OpenSSL_add_all_digests(); 52 } 53 54 int 55 OPENSSL_init_crypto(uint64_t opts, const void *settings) 56 { 57 if (crypto_init_cleaned_up) { 58 CRYPTOerror(ERR_R_INIT_FAIL); 59 return 0; 60 } 61 62 if (pthread_equal(pthread_self(), crypto_init_thread)) 63 return 1; /* don't recurse */ 64 65 if (pthread_once(&crypto_init_once, OPENSSL_init_crypto_internal) != 0) 66 return 0; 67 68 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) && 69 (OpenSSL_no_config() == 0)) 70 return 0; 71 72 if ((opts & OPENSSL_INIT_LOAD_CONFIG) && 73 (OpenSSL_config(NULL) == 0)) 74 return 0; 75 76 return 1; 77 } 78 LCRYPTO_ALIAS(OPENSSL_init_crypto); 79 80 void 81 OPENSSL_cleanup(void) 82 { 83 /* This currently calls init... */ 84 ERR_free_strings(); 85 86 CRYPTO_cleanup_all_ex_data(); 87 #ifndef OPENSSL_NO_ENGINE 88 ENGINE_cleanup(); 89 #endif 90 EVP_cleanup(); 91 92 ASN1_STRING_TABLE_cleanup(); 93 X509V3_EXT_cleanup(); 94 X509_PURPOSE_cleanup(); 95 X509_TRUST_cleanup(); 96 X509_VERIFY_PARAM_table_cleanup(); 97 98 x509_issuer_cache_free(); 99 100 crypto_init_cleaned_up = 1; 101 } 102 LCRYPTO_ALIAS(OPENSSL_cleanup); 103