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