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