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