1*72c33676SMaxim Ag /* $OpenBSD: ssl_init.c,v 1.2 2018/03/30 14:59:46 jsing Exp $ */
2*72c33676SMaxim Ag /*
3*72c33676SMaxim Ag * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
4*72c33676SMaxim Ag *
5*72c33676SMaxim Ag * Permission to use, copy, modify, and distribute this software for any
6*72c33676SMaxim Ag * purpose with or without fee is hereby granted, provided that the above
7*72c33676SMaxim Ag * copyright notice and this permission notice appear in all copies.
8*72c33676SMaxim Ag *
9*72c33676SMaxim Ag * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*72c33676SMaxim Ag * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*72c33676SMaxim Ag * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*72c33676SMaxim Ag * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*72c33676SMaxim Ag * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*72c33676SMaxim Ag * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*72c33676SMaxim Ag * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*72c33676SMaxim Ag */
17*72c33676SMaxim Ag
18*72c33676SMaxim Ag /* OpenSSL style init */
19*72c33676SMaxim Ag
20*72c33676SMaxim Ag #include <pthread.h>
21*72c33676SMaxim Ag #include <stdio.h>
22*72c33676SMaxim Ag
23*72c33676SMaxim Ag #include <openssl/objects.h>
24*72c33676SMaxim Ag
25*72c33676SMaxim Ag #include "ssl_locl.h"
26*72c33676SMaxim Ag
27*72c33676SMaxim Ag static pthread_t ssl_init_thread;
28*72c33676SMaxim Ag
29*72c33676SMaxim Ag static void
OPENSSL_init_ssl_internal(void)30*72c33676SMaxim Ag OPENSSL_init_ssl_internal(void)
31*72c33676SMaxim Ag {
32*72c33676SMaxim Ag ssl_init_thread = pthread_self();
33*72c33676SMaxim Ag SSL_load_error_strings();
34*72c33676SMaxim Ag SSL_library_init();
35*72c33676SMaxim Ag }
36*72c33676SMaxim Ag
37*72c33676SMaxim Ag int
OPENSSL_init_ssl(uint64_t opts,const void * settings)38*72c33676SMaxim Ag OPENSSL_init_ssl(uint64_t opts, const void *settings)
39*72c33676SMaxim Ag {
40*72c33676SMaxim Ag static pthread_once_t once = PTHREAD_ONCE_INIT;
41*72c33676SMaxim Ag
42*72c33676SMaxim Ag if (pthread_equal(pthread_self(), ssl_init_thread))
43*72c33676SMaxim Ag return 1; /* don't recurse */
44*72c33676SMaxim Ag
45*72c33676SMaxim Ag OPENSSL_init_crypto(opts, settings);
46*72c33676SMaxim Ag
47*72c33676SMaxim Ag if (pthread_once(&once, OPENSSL_init_ssl_internal) != 0)
48*72c33676SMaxim Ag return 0;
49*72c33676SMaxim Ag
50*72c33676SMaxim Ag return 1;
51*72c33676SMaxim Ag }
52