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