1 /* $OpenBSD: crypto_init.c,v 1.22 2024/10/17 14:27:57 jsing 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 #include <openssl/err.h>
26 #include <openssl/evp.h>
27 #include <openssl/objects.h>
28 #include <openssl/x509v3.h>
29
30 #include "crypto_internal.h"
31 #include "x509_issuer_cache.h"
32
33 int OpenSSL_config(const char *);
34 int OpenSSL_no_config(void);
35
36 static pthread_once_t crypto_init_once = PTHREAD_ONCE_INIT;
37 static pthread_t crypto_init_thread;
38 static int crypto_init_cleaned_up;
39
40 void
OPENSSL_init(void)41 OPENSSL_init(void)
42 {
43 }
44 LCRYPTO_ALIAS(OPENSSL_init);
45
46 static void
OPENSSL_init_crypto_internal(void)47 OPENSSL_init_crypto_internal(void)
48 {
49 crypto_init_thread = pthread_self();
50
51 crypto_cpu_caps_init();
52
53 ERR_load_crypto_strings();
54 }
55
56 int
OPENSSL_init_crypto(uint64_t opts,const void * settings)57 OPENSSL_init_crypto(uint64_t opts, const void *settings)
58 {
59 if (crypto_init_cleaned_up) {
60 CRYPTOerror(ERR_R_INIT_FAIL);
61 return 0;
62 }
63
64 if (pthread_equal(pthread_self(), crypto_init_thread))
65 return 1; /* don't recurse */
66
67 if (pthread_once(&crypto_init_once, OPENSSL_init_crypto_internal) != 0)
68 return 0;
69
70 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) &&
71 (OpenSSL_no_config() == 0))
72 return 0;
73
74 if ((opts & OPENSSL_INIT_LOAD_CONFIG) &&
75 (OpenSSL_config(NULL) == 0))
76 return 0;
77
78 return 1;
79 }
80 LCRYPTO_ALIAS(OPENSSL_init_crypto);
81
82 void
OPENSSL_cleanup(void)83 OPENSSL_cleanup(void)
84 {
85 /* This currently calls init... */
86 ERR_free_strings();
87
88 CRYPTO_cleanup_all_ex_data();
89 EVP_cleanup();
90
91 X509_VERIFY_PARAM_table_cleanup();
92
93 x509_issuer_cache_free();
94
95 crypto_init_cleaned_up = 1;
96 }
97 LCRYPTO_ALIAS(OPENSSL_cleanup);
98
99 void
OpenSSL_add_all_ciphers(void)100 OpenSSL_add_all_ciphers(void)
101 {
102 }
103 LCRYPTO_ALIAS(OpenSSL_add_all_ciphers);
104
105 void
OpenSSL_add_all_digests(void)106 OpenSSL_add_all_digests(void)
107 {
108 }
109 LCRYPTO_ALIAS(OpenSSL_add_all_digests);
110
111 void
OPENSSL_add_all_algorithms_noconf(void)112 OPENSSL_add_all_algorithms_noconf(void)
113 {
114 }
115 LCRYPTO_ALIAS(OPENSSL_add_all_algorithms_noconf);
116
117 void
OPENSSL_add_all_algorithms_conf(void)118 OPENSSL_add_all_algorithms_conf(void)
119 {
120 OPENSSL_config(NULL);
121 }
122 LCRYPTO_ALIAS(OPENSSL_add_all_algorithms_conf);
123