jcs's openbsd hax
openbsd
1/* $OpenBSD: crypto_init.c,v 1.26 2025/06/11 07:41:12 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/asn1.h>
24#include <openssl/conf.h>
25#include <openssl/evp.h>
26#include <openssl/objects.h>
27#include <openssl/x509v3.h>
28
29#include "crypto_internal.h"
30#include "err_local.h"
31#include "x509_issuer_cache.h"
32
33int OpenSSL_config(const char *);
34int OpenSSL_no_config(void);
35
36static pthread_once_t crypto_init_once = PTHREAD_ONCE_INIT;
37static pthread_t crypto_init_thread;
38static int crypto_init_cleaned_up;
39
40void openssl_init_crypto_constructor(void) __attribute__((constructor));
41
42#ifndef HAVE_CRYPTO_CPU_CAPS_INIT
43void
44crypto_cpu_caps_init(void)
45{
46}
47#endif
48
49/*
50 * This function is invoked as a constructor when the library is loaded. The
51 * code run from here must not allocate memory or trigger signals. The only
52 * safe code is to read data and update global variables.
53 */
54void
55openssl_init_crypto_constructor(void)
56{
57 crypto_cpu_caps_init();
58}
59
60/*
61 * This is used by various configure scripts to check availability of libcrypto,
62 * so we need to keep it.
63 */
64void
65OPENSSL_init(void)
66{
67}
68LCRYPTO_ALIAS(OPENSSL_init);
69
70static void
71OPENSSL_init_crypto_internal(void)
72{
73 crypto_init_thread = pthread_self();
74
75 ERR_load_crypto_strings();
76}
77
78int
79OPENSSL_init_crypto(uint64_t opts, const void *settings)
80{
81 if (crypto_init_cleaned_up) {
82 CRYPTOerror(ERR_R_INIT_FAIL);
83 return 0;
84 }
85
86 if (pthread_equal(pthread_self(), crypto_init_thread))
87 return 1; /* don't recurse */
88
89 if (pthread_once(&crypto_init_once, OPENSSL_init_crypto_internal) != 0)
90 return 0;
91
92 if ((opts & OPENSSL_INIT_NO_LOAD_CONFIG) &&
93 (OpenSSL_no_config() == 0))
94 return 0;
95
96 if ((opts & OPENSSL_INIT_LOAD_CONFIG) &&
97 (OpenSSL_config(NULL) == 0))
98 return 0;
99
100 return 1;
101}
102LCRYPTO_ALIAS(OPENSSL_init_crypto);
103
104void
105OPENSSL_cleanup(void)
106{
107 /* This currently calls init... */
108 ERR_free_strings();
109
110 CRYPTO_cleanup_all_ex_data();
111 EVP_cleanup();
112
113 X509_VERIFY_PARAM_table_cleanup();
114
115 x509_issuer_cache_free();
116
117 crypto_init_cleaned_up = 1;
118}
119LCRYPTO_ALIAS(OPENSSL_cleanup);
120
121void
122OpenSSL_add_all_ciphers(void)
123{
124}
125LCRYPTO_ALIAS(OpenSSL_add_all_ciphers);
126
127void
128OpenSSL_add_all_digests(void)
129{
130}
131LCRYPTO_ALIAS(OpenSSL_add_all_digests);
132
133void
134OPENSSL_add_all_algorithms_noconf(void)
135{
136}
137LCRYPTO_ALIAS(OPENSSL_add_all_algorithms_noconf);
138
139void
140OPENSSL_add_all_algorithms_conf(void)
141{
142 OPENSSL_config(NULL);
143}
144LCRYPTO_ALIAS(OPENSSL_add_all_algorithms_conf);