xref: /openbsd-src/lib/libssl/ssl_init.c (revision f1cab7dbd58658972ff1b75f6c4f7c5965127235)
1 /* $OpenBSD: ssl_init.c,v 1.6 2023/11/22 15:53:53 tb 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/objects.h>
24 
25 #include "ssl_local.h"
26 
27 static pthread_t ssl_init_thread;
28 
29 int
SSL_library_init(void)30 SSL_library_init(void)
31 {
32 	return OPENSSL_init_ssl(0, NULL);
33 }
34 LSSL_ALIAS(SSL_library_init);
35 
36 static void
OPENSSL_init_ssl_internal(void)37 OPENSSL_init_ssl_internal(void)
38 {
39 	ssl_init_thread = pthread_self();
40 	SSL_load_error_strings();
41 }
42 
43 int
OPENSSL_init_ssl(uint64_t opts,const void * settings)44 OPENSSL_init_ssl(uint64_t opts, const void *settings)
45 {
46 	static pthread_once_t once = PTHREAD_ONCE_INIT;
47 
48 	if (pthread_equal(pthread_self(), ssl_init_thread))
49 		return 1; /* don't recurse */
50 
51 	OPENSSL_init_crypto(opts, settings);
52 
53 	if (pthread_once(&once, OPENSSL_init_ssl_internal) != 0)
54 		return 0;
55 
56 	return 1;
57 }
58 LSSL_ALIAS(OPENSSL_init_ssl);
59