1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1.1.2 christos OPENSSL_INIT_new, OPENSSL_INIT_set_config_filename, 6 1.1.1.2 christos OPENSSL_INIT_set_config_appname, OPENSSL_INIT_set_config_file_flags, 7 1.1.1.2 christos OPENSSL_INIT_free, OPENSSL_init_crypto, OPENSSL_cleanup, OPENSSL_atexit, 8 1.1.1.5 christos OPENSSL_thread_stop_ex, OPENSSL_thread_stop - OpenSSL initialisation 9 1.1.1.2 christos and deinitialisation functions 10 1.1 christos 11 1.1 christos =head1 SYNOPSIS 12 1.1 christos 13 1.1 christos #include <openssl/crypto.h> 14 1.1 christos 15 1.1 christos void OPENSSL_cleanup(void); 16 1.1 christos int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings); 17 1.1 christos int OPENSSL_atexit(void (*handler)(void)); 18 1.1.1.5 christos void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx); 19 1.1 christos void OPENSSL_thread_stop(void); 20 1.1 christos 21 1.1 christos OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void); 22 1.1.1.2 christos int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *init, 23 1.1.1.2 christos const char* filename); 24 1.1.1.2 christos int OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *init, 25 1.1.1.2 christos unsigned long flags); 26 1.1 christos int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *init, 27 1.1 christos const char* name); 28 1.1 christos void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *init); 29 1.1 christos 30 1.1 christos =head1 DESCRIPTION 31 1.1 christos 32 1.1 christos During normal operation OpenSSL (libcrypto) will allocate various resources at 33 1.1 christos start up that must, subsequently, be freed on close down of the library. 34 1.1 christos Additionally some resources are allocated on a per thread basis (if the 35 1.1 christos application is multi-threaded), and these resources must be freed prior to the 36 1.1 christos thread closing. 37 1.1 christos 38 1.1 christos As of version 1.1.0 OpenSSL will automatically allocate all resources that it 39 1.1 christos needs so no explicit initialisation is required. Similarly it will also 40 1.1 christos automatically deinitialise as required. 41 1.1 christos 42 1.1.1.2 christos However, there may be situations when explicit initialisation is desirable or 43 1.1.1.3 christos needed, for example when some nondefault initialisation is required. The 44 1.1 christos function OPENSSL_init_crypto() can be used for this purpose for 45 1.1 christos libcrypto (see also L<OPENSSL_init_ssl(3)> for the libssl 46 1.1 christos equivalent). 47 1.1 christos 48 1.1 christos Numerous internal OpenSSL functions call OPENSSL_init_crypto(). 49 1.1.1.3 christos Therefore, in order to perform nondefault initialisation, 50 1.1 christos OPENSSL_init_crypto() MUST be called by application code prior to 51 1.1 christos any other OpenSSL function calls. 52 1.1 christos 53 1.1 christos The B<opts> parameter specifies which aspects of libcrypto should be 54 1.1 christos initialised. Valid options are: 55 1.1 christos 56 1.1 christos =over 4 57 1.1 christos 58 1.1 christos =item OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS 59 1.1 christos 60 1.1 christos Suppress automatic loading of the libcrypto error strings. This option is 61 1.1 christos not a default option. Once selected subsequent calls to 62 1.1 christos OPENSSL_init_crypto() with the option 63 1.1 christos B<OPENSSL_INIT_LOAD_CRYPTO_STRINGS> will be ignored. 64 1.1 christos 65 1.1 christos =item OPENSSL_INIT_LOAD_CRYPTO_STRINGS 66 1.1 christos 67 1.1 christos Automatic loading of the libcrypto error strings. With this option the 68 1.1 christos library will automatically load the libcrypto error strings. 69 1.1 christos This option is a default option. Once selected subsequent calls to 70 1.1 christos OPENSSL_init_crypto() with the option 71 1.1 christos B<OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS> will be ignored. 72 1.1 christos 73 1.1 christos =item OPENSSL_INIT_ADD_ALL_CIPHERS 74 1.1 christos 75 1.1 christos With this option the library will automatically load and make available all 76 1.1 christos libcrypto ciphers. This option is a default option. Once selected subsequent 77 1.1 christos calls to OPENSSL_init_crypto() with the option 78 1.1 christos B<OPENSSL_INIT_NO_ADD_ALL_CIPHERS> will be ignored. 79 1.1 christos 80 1.1 christos =item OPENSSL_INIT_ADD_ALL_DIGESTS 81 1.1 christos 82 1.1 christos With this option the library will automatically load and make available all 83 1.1 christos libcrypto digests. This option is a default option. Once selected subsequent 84 1.1 christos calls to OPENSSL_init_crypto() with the option 85 1.1.1.4 christos B<OPENSSL_INIT_NO_ADD_ALL_DIGESTS> will be ignored. 86 1.1 christos 87 1.1 christos =item OPENSSL_INIT_NO_ADD_ALL_CIPHERS 88 1.1 christos 89 1.1 christos With this option the library will suppress automatic loading of libcrypto 90 1.1 christos ciphers. This option is not a default option. Once selected subsequent 91 1.1 christos calls to OPENSSL_init_crypto() with the option 92 1.1 christos B<OPENSSL_INIT_ADD_ALL_CIPHERS> will be ignored. 93 1.1 christos 94 1.1 christos =item OPENSSL_INIT_NO_ADD_ALL_DIGESTS 95 1.1 christos 96 1.1 christos With this option the library will suppress automatic loading of libcrypto 97 1.1 christos digests. This option is not a default option. Once selected subsequent 98 1.1 christos calls to OPENSSL_init_crypto() with the option 99 1.1 christos B<OPENSSL_INIT_ADD_ALL_DIGESTS> will be ignored. 100 1.1 christos 101 1.1 christos =item OPENSSL_INIT_LOAD_CONFIG 102 1.1 christos 103 1.1 christos With this option an OpenSSL configuration file will be automatically loaded and 104 1.1.1.5 christos used by calling OPENSSL_config(). This is a default option. 105 1.1.1.5 christos Note that in OpenSSL 1.1.1 this was the default for libssl but not for 106 1.1.1.5 christos libcrypto (see L<OPENSSL_init_ssl(3)> for further details about libssl 107 1.1.1.5 christos initialisation). 108 1.1.1.5 christos In OpenSSL 1.1.0 this was a nondefault option for both libssl and libcrypto. 109 1.1.1.5 christos See the description of OPENSSL_INIT_new(), below. 110 1.1 christos 111 1.1 christos =item OPENSSL_INIT_NO_LOAD_CONFIG 112 1.1 christos 113 1.1 christos With this option the loading of OpenSSL configuration files will be suppressed. 114 1.1 christos It is the equivalent of calling OPENSSL_no_config(). This is not a default 115 1.1 christos option. 116 1.1 christos 117 1.1 christos =item OPENSSL_INIT_ASYNC 118 1.1 christos 119 1.1 christos With this option the library with automatically initialise the libcrypto async 120 1.1 christos sub-library (see L<ASYNC_start_job(3)>). This is a default option. 121 1.1 christos 122 1.1 christos =item OPENSSL_INIT_ENGINE_RDRAND 123 1.1 christos 124 1.1 christos With this option the library will automatically load and initialise the 125 1.1.1.5 christos RDRAND engine (if available). This not a default option and is deprecated 126 1.1.1.5 christos in OpenSSL 3.0. 127 1.1 christos 128 1.1 christos =item OPENSSL_INIT_ENGINE_DYNAMIC 129 1.1 christos 130 1.1 christos With this option the library will automatically load and initialise the 131 1.1.1.5 christos dynamic engine. This not a default option and is deprecated 132 1.1.1.5 christos in OpenSSL 3.0. 133 1.1 christos 134 1.1 christos =item OPENSSL_INIT_ENGINE_OPENSSL 135 1.1 christos 136 1.1 christos With this option the library will automatically load and initialise the 137 1.1.1.5 christos openssl engine. This not a default option and is deprecated 138 1.1.1.5 christos in OpenSSL 3.0. 139 1.1 christos 140 1.1 christos =item OPENSSL_INIT_ENGINE_CRYPTODEV 141 1.1 christos 142 1.1 christos With this option the library will automatically load and initialise the 143 1.1.1.5 christos cryptodev engine (if available). This not a default option and is deprecated 144 1.1.1.5 christos in OpenSSL 3.0. 145 1.1 christos 146 1.1 christos =item OPENSSL_INIT_ENGINE_CAPI 147 1.1 christos 148 1.1 christos With this option the library will automatically load and initialise the 149 1.1.1.5 christos CAPI engine (if available). This not a default option and is deprecated 150 1.1.1.5 christos in OpenSSL 3.0. 151 1.1 christos 152 1.1 christos =item OPENSSL_INIT_ENGINE_PADLOCK 153 1.1 christos 154 1.1 christos With this option the library will automatically load and initialise the 155 1.1.1.5 christos padlock engine (if available). This not a default option and is deprecated 156 1.1.1.5 christos in OpenSSL 3.0. 157 1.1 christos 158 1.1 christos =item OPENSSL_INIT_ENGINE_AFALG 159 1.1 christos 160 1.1 christos With this option the library will automatically load and initialise the 161 1.1.1.5 christos AFALG engine. This not a default option and is deprecated 162 1.1.1.5 christos in OpenSSL 3.0. 163 1.1 christos 164 1.1 christos =item OPENSSL_INIT_ENGINE_ALL_BUILTIN 165 1.1 christos 166 1.1 christos With this option the library will automatically load and initialise all the 167 1.1 christos built in engines listed above with the exception of the openssl and afalg 168 1.1.1.5 christos engines. This not a default option and is deprecated 169 1.1.1.5 christos in OpenSSL 3.0. 170 1.1 christos 171 1.1 christos =item OPENSSL_INIT_ATFORK 172 1.1 christos 173 1.1 christos With this option the library will register its fork handlers. 174 1.1 christos See OPENSSL_fork_prepare(3) for details. 175 1.1 christos 176 1.1.1.2 christos =item OPENSSL_INIT_NO_ATEXIT 177 1.1.1.2 christos 178 1.1.1.2 christos By default OpenSSL will attempt to clean itself up when the process exits via an 179 1.1.1.2 christos "atexit" handler. Using this option suppresses that behaviour. This means that 180 1.1.1.2 christos the application will have to clean up OpenSSL explicitly using 181 1.1.1.2 christos OPENSSL_cleanup(). 182 1.1.1.2 christos 183 1.1 christos =back 184 1.1 christos 185 1.1 christos Multiple options may be combined together in a single call to 186 1.1 christos OPENSSL_init_crypto(). For example: 187 1.1 christos 188 1.1 christos OPENSSL_init_crypto(OPENSSL_INIT_NO_ADD_ALL_CIPHERS 189 1.1 christos | OPENSSL_INIT_NO_ADD_ALL_DIGESTS, NULL); 190 1.1 christos 191 1.1 christos The OPENSSL_cleanup() function deinitialises OpenSSL (both libcrypto 192 1.1 christos and libssl). All resources allocated by OpenSSL are freed. Typically there 193 1.1 christos should be no need to call this function directly as it is initiated 194 1.1 christos automatically on application exit. This is done via the standard C library 195 1.1 christos atexit() function. In the event that the application will close in a manner 196 1.1 christos that will not call the registered atexit() handlers then the application should 197 1.1 christos call OPENSSL_cleanup() directly. Developers of libraries using OpenSSL 198 1.1 christos are discouraged from calling this function and should instead, typically, rely 199 1.1 christos on auto-deinitialisation. This is to avoid error conditions where both an 200 1.1 christos application and a library it depends on both use OpenSSL, and the library 201 1.1 christos deinitialises it before the application has finished using it. 202 1.1 christos 203 1.1 christos Once OPENSSL_cleanup() has been called the library cannot be reinitialised. 204 1.1 christos Attempts to call OPENSSL_init_crypto() will fail and an ERR_R_INIT_FAIL error 205 1.1 christos will be added to the error stack. Note that because initialisation has failed 206 1.1 christos OpenSSL error strings will not be available, only an error code. This code can 207 1.1 christos be put through the openssl errstr command line application to produce a human 208 1.1.1.5 christos readable error (see L<openssl-errstr(1)>). 209 1.1 christos 210 1.1 christos The OPENSSL_atexit() function enables the registration of a 211 1.1 christos function to be called during OPENSSL_cleanup(). Stop handlers are 212 1.1 christos called after deinitialisation of resources local to a thread, but before other 213 1.1 christos process wide resources are freed. In the event that multiple stop handlers are 214 1.1 christos registered, no guarantees are made about the order of execution. 215 1.1 christos 216 1.1.1.5 christos The OPENSSL_thread_stop_ex() function deallocates resources associated 217 1.1.1.5 christos with the current thread for the given OSSL_LIB_CTX B<ctx>. The B<ctx> parameter 218 1.1.1.5 christos can be NULL in which case the default OSSL_LIB_CTX is used. 219 1.1.1.5 christos 220 1.1.1.5 christos Typically, this function will be called automatically by the library when 221 1.1.1.5 christos the thread exits as long as the OSSL_LIB_CTX has not been freed before the thread 222 1.1.1.5 christos exits. If OSSL_LIB_CTX_free() is called OPENSSL_thread_stop_ex will be called 223 1.1.1.5 christos automatically for the current thread (but not any other threads that may have 224 1.1.1.5 christos used this OSSL_LIB_CTX). 225 1.1.1.5 christos 226 1.1.1.5 christos OPENSSL_thread_stop_ex should be called on all threads that will exit after the 227 1.1.1.5 christos OSSL_LIB_CTX is freed. 228 1.1.1.5 christos Typically this is not necessary for the default OSSL_LIB_CTX (because all 229 1.1.1.5 christos resources are cleaned up on library exit) except if thread local resources 230 1.1.1.5 christos should be freed before library exit, or under the circumstances described in 231 1.1.1.5 christos the NOTES section below. 232 1.1.1.5 christos 233 1.1.1.5 christos OPENSSL_thread_stop() is the same as OPENSSL_thread_stop_ex() except that the 234 1.1.1.5 christos default OSSL_LIB_CTX is always used. 235 1.1 christos 236 1.1.1.2 christos The B<OPENSSL_INIT_LOAD_CONFIG> flag will load a configuration file, as with 237 1.1.1.2 christos L<CONF_modules_load_file(3)> with NULL filename and application name and the 238 1.1.1.2 christos B<CONF_MFLAGS_IGNORE_MISSING_FILE>, B<CONF_MFLAGS_IGNORE_RETURN_CODES> and 239 1.1.1.2 christos B<CONF_MFLAGS_DEFAULT_SECTION> flags. 240 1.1.1.2 christos The filename, application name, and flags can be customized by providing a 241 1.1.1.2 christos non-null B<OPENSSL_INIT_SETTINGS> object. 242 1.1.1.5 christos The object can be allocated via B<OPENSSL_INIT_new()>. 243 1.1.1.2 christos The B<OPENSSL_INIT_set_config_filename()> function can be used to specify a 244 1.1.1.3 christos nondefault filename, which is copied and need not refer to persistent storage. 245 1.1.1.2 christos Similarly, OPENSSL_INIT_set_config_appname() can be used to specify a 246 1.1.1.3 christos nondefault application name. 247 1.1.1.3 christos Finally, OPENSSL_INIT_set_file_flags can be used to specify nondefault flags. 248 1.1.1.2 christos If the B<CONF_MFLAGS_IGNORE_RETURN_CODES> flag is not included, any errors in 249 1.1.1.2 christos the configuration file will cause an error return from B<OPENSSL_init_crypto> 250 1.1.1.2 christos or indirectly L<OPENSSL_init_ssl(3)>. 251 1.1.1.2 christos The object can be released with OPENSSL_INIT_free() when done. 252 1.1.1.6 christos If the argument to OPENSSL_INIT_free() is NULL, nothing is done. 253 1.1 christos 254 1.1 christos =head1 NOTES 255 1.1 christos 256 1.1 christos Resources local to a thread are deallocated automatically when the thread exits 257 1.1 christos (e.g. in a pthreads environment, when pthread_exit() is called). On Windows 258 1.1 christos platforms this is done in response to a DLL_THREAD_DETACH message being sent to 259 1.1 christos the libcrypto32.dll entry point. Some windows functions may cause threads to exit 260 1.1 christos without sending this message (for example ExitProcess()). If the application 261 1.1 christos uses such functions, then the application must free up OpenSSL resources 262 1.1 christos directly via a call to OPENSSL_thread_stop() on each thread. Similarly this 263 1.1 christos message will also not be sent if OpenSSL is linked statically, and therefore 264 1.1 christos applications using static linking should also call OPENSSL_thread_stop() on each 265 1.1 christos thread. Additionally if OpenSSL is loaded dynamically via LoadLibrary() and the 266 1.1 christos threads are not destroyed until after FreeLibrary() is called then each thread 267 1.1 christos should call OPENSSL_thread_stop() prior to the FreeLibrary() call. 268 1.1 christos 269 1.1 christos On Linux/Unix where OpenSSL has been loaded via dlopen() and the application is 270 1.1 christos multi-threaded and if dlclose() is subsequently called prior to the threads 271 1.1 christos being destroyed then OpenSSL will not be able to deallocate resources associated 272 1.1 christos with those threads. The application should either call OPENSSL_thread_stop() on 273 1.1 christos each thread prior to the dlclose() call, or alternatively the original dlopen() 274 1.1 christos call should use the RTLD_NODELETE flag (where available on the platform). 275 1.1 christos 276 1.1 christos =head1 RETURN VALUES 277 1.1 christos 278 1.1 christos The functions OPENSSL_init_crypto, OPENSSL_atexit() and 279 1.1 christos OPENSSL_INIT_set_config_appname() return 1 on success or 0 on error. 280 1.1 christos 281 1.1 christos =head1 SEE ALSO 282 1.1 christos 283 1.1 christos L<OPENSSL_init_ssl(3)> 284 1.1 christos 285 1.1 christos =head1 HISTORY 286 1.1 christos 287 1.1 christos The OPENSSL_init_crypto(), OPENSSL_cleanup(), OPENSSL_atexit(), 288 1.1 christos OPENSSL_thread_stop(), OPENSSL_INIT_new(), OPENSSL_INIT_set_config_appname() 289 1.1 christos and OPENSSL_INIT_free() functions were added in OpenSSL 1.1.0. 290 1.1 christos 291 1.1 christos =head1 COPYRIGHT 292 1.1 christos 293 1.1.1.6 christos Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved. 294 1.1 christos 295 1.1.1.5 christos Licensed under the Apache License 2.0 (the "License"). You may not use 296 1.1 christos this file except in compliance with the License. You can obtain a copy 297 1.1 christos in the file LICENSE in the source distribution or at 298 1.1 christos L<https://www.openssl.org/source/license.html>. 299 1.1 christos 300 1.1 christos =cut 301