Provide documentation for auto-init/auto-deinit
[openssl.git] / doc / crypto / OPENSSL_INIT_crypto_library_start.pod
1 =pod
2
3 =head1 NAME
4
5 OPENSSL_INIT_library_stop, OPENSSL_INIT_crypto_library_start,
6 OPENSSL_INIT_register_stop_handler, OPENSSL_INIT_thread_stop - OpenSSL
7 initialisation and deinitialisation functions
8
9 =head1 SYNOPSIS
10
11  #include <openssl/crypto.h>
12
13  void OPENSSL_INIT_library_stop(void);
14  void OPENSSL_INIT_crypto_library_start(uint64_t opts,
15                                         const OPENSSL_INIT_SETTINGS *settings);
16  int OPENSSL_INIT_register_stop_handler(void (*handler)(void));
17  void OPENSSL_INIT_thread_stop(void);
18
19 =head1 DESCRIPTION
20
21 During normal operation OpenSSL (libcrypto) will allocate various resources at
22 start up that must, subsequently, be freed on close down of the library.
23 Additionally some resources are allocated on a per thread basis (if the
24 application is multi-threaded), and these resources must be freed prior to the
25 thread closing.
26
27 As of version 1.1.0 OpenSSL will automatically allocate all resources that it
28 needs so no explicit initialisation is required. Similarly it will also
29 automatically deinitialise as required.
30
31 However, there way be situations when explicit initialisation is desirable or
32 needed, for example when some non-default initialisation is required. The
33 function OPENSSL_INIT_crypto_library_start() can be used for this purpose for
34 libcrypto (see also L<OPENSSL_INIT_ssl_library_start(3)> for the libssl
35 equivalent). In order to perform non-default initialisation it MUST be called
36 prior to any other calls of this function. As numerous internal OpenSSL
37 functions also call this, this usually means you should call it prior to ANY
38 other OpenSSL function calls.
39
40 The B<opts> parameter specifies which aspects of libcrypto should be
41 initialised. Valid options are:
42
43 =over 4
44
45 =item OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS
46
47 Suppress automatic loading of the libcrypto error strings. With this option the
48 library will not automatically call ERR_load_crypto_strings(). This option is
49 not a default option. Once selected subsequent calls to
50 OPENSSL_INIT_crypto_library_start() with the option
51 B<OPENSSL_INIT_LOAD_CRYPTO_STRINGS> will be ignored. Applications may call
52 ERR_load_crypto_strings() directly if they wish even if this option has been
53 selected. If they do so then they must also explicitly call ERR_free_strings()
54 on application close down.
55
56 =item OPENSSL_INIT_LOAD_CRYPTO_STRINGS
57
58 Automatic loading of the libcrypto error strings. With this option the
59 library will automatically call ERR_load_crypto_strings(). This option is a
60 default option. Once selected subsequent calls to
61 OPENSSL_INIT_crypto_library_start() with the option
62 B<OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS> will be ignored.
63
64 =item OPENSSL_INIT_ADD_ALL_CIPHERS
65
66 With this option the library will automatically load and make available all
67 libcrypto ciphers. This option is a default option. Once selected subsequent
68 calls to OPENSSL_INIT_crypto_library_start() with the option
69 B<OPENSSL_INIT_NO_ADD_ALL_CIPHERS> will be ignored.
70
71 =item OPENSSL_INIT_ADD_ALL_DIGESTS
72
73 With this option the library will automatically load and make available all
74 libcrypto digests. This option is a default option. Once selected subsequent
75 calls to OPENSSL_INIT_crypto_library_start() with the option
76 B<OPENSSL_INIT_NO_ADD_ALL_CIPHERS> will be ignored.
77
78 =item OPENSSL_INIT_NO_ADD_ALL_CIPHERS
79
80 With this option the library will suppress automatic loading of libcrypto
81 ciphers. This option is not a default option. Once selected subsequent
82 calls to OPENSSL_INIT_crypto_library_start() with the option
83 B<OPENSSL_INIT_ADD_ALL_CIPHERS> will be ignored.
84
85 =item OPENSSL_INIT_NO_ADD_ALL_DIGESTS
86
87 With this option the library will suppress automatic loading of libcrypto
88 digests. This option is not a default option. Once selected subsequent
89 calls to OPENSSL_INIT_crypto_library_start() with the option
90 B<OPENSSL_INIT_ADD_ALL_DIGESTS> will be ignored.
91
92 =item OPENSSL_INIT_LOAD_CONFIG
93
94 With this option an OpenSSL configuration file will be automatically loaded and
95 used by calling OPENSSL_config(). This is not a default option.
96
97 =item OPENSSL_INIT_NO_LOAD_CONFIG
98
99 With this option the loading of OpenSSL configuration files will be suppressed.
100 It is the equivalent of calling OPENSSL_no_config(). This is not a default
101 option.
102
103 =item OPENSSL_INIT_ASYNC
104
105 With this option the library with automatically initialise the libcrypto async
106 sub-library (see L<ASYNC_start_job(3)>). This is a default option.
107
108 =item OPENSSL_INIT_ENGINE_RDRAND
109
110 With this option the library will automatically load and initialise the
111 RDRAND engine (if available). This not a default option.
112
113 =item OPENSSL_INIT_ENGINE_DYNAMIC
114
115 With this option the library will automatically load and initialise the
116 dynamic engine. This not a default option.
117
118 =item OPENSSL_INIT_ENGINE_OPENSSL
119
120 With this option the library will automatically load and initialise the
121 openssl engine. This not a default option.
122
123 =item OPENSSL_INIT_ENGINE_CRYPTODEV
124
125 With this option the library will automatically load and initialise the
126 cryptodev engine (if available). This not a default option.
127
128 =item OPENSSL_INIT_ENGINE_CAPI
129
130 With this option the library will automatically load and initialise the
131 CAPI engine (if available). This not a default option.
132
133 =item OPENSSL_INIT_ENGINE_PADLOCK
134
135 With this option the library will automatically load and initialise the
136 padlock engine (if available). This not a default option.
137
138 =item OPENSSL_INIT_ENGINE_DASYNC
139
140 With this option the library will automatically load and initialise the
141 DASYNC engine. This not a default option.
142
143 =item OPENSSL_INIT_ENGINE_ALL_BUILTIN
144
145 With this option the library will automatically load and initialise all the
146 built in engines listed above with the exception of the openssl and dasync
147 engines. This not a default option.
148
149 =back
150
151 Multiple options may be combined together in a single call to
152 OPENSSL_INIT_start_library(). For example:
153
154  OPENSSL_INIT_start_library(OPENSSL_INIT_NO_ADD_ALL_CIPHERS
155                             | OPENSSL_INIT_NO_ADD_ALL_DIGESTS, NULL);
156
157
158 The B<settings> parameter to OPENSSL_INIT_start_library() may be used to
159 provide optional settings values to an option. Currently the only option this
160 applies to is OPENSSL_INIT_LOAD_CONFIG. This provides the optional
161 OPENSSL_INIT_SET_CONF_FILENAME parameter to provide a filename to load
162 configuration from. If no filename is provided then the system default
163 configuration file is assumed. For example
164
165  const OPENSSL_INIT_SETTINGS settings[2] = {
166      { OPENSSL_INIT_SET_CONF_FILENAME, .value.type_string = "myconf.cnf" },
167      { OPENSSL_INIT_SET_END, .value.type_int = 0 }
168  };
169  OPENSSL_INIT_crypto_library_start(OPENSSL_INIT_LOAD_CONFIG,
170                                    (const OPENSSL_INIT_SETTINGS *)&settings);
171
172 The B<settings> parameter must be an array of OPENSSL_INIT_SETTINGS values
173 terminated with an OPENSSL_INIT_SET_END entry.
174
175 The OPENSSL_INIT_library_stop() 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 L<atexit(3)> 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_INIT_library_stop() 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 The OPENSSL_INIT_register_stop_handler() function enables the registration of a
188 function to be called during OPENSSL_INIT_library_stop(). Stop handlers are
189 called after deinitialisation of resources local to a thread, but before other
190 process wide resources are freed. In the event that multiple stop handlers are
191 registered, no guarantees are made about the order of execution.
192
193 The OPENSSL_INIT_thread_stop() function deallocates resources associated
194 with the current thread. Typically this function will be called automatically by
195 the library when the thread exits. This should only be called directly if
196 resources should be freed at an earlier time, or under the circumstances
197 described in the NOTES section below.
198
199 =head1 NOTES
200
201 Resources local to a thread are deallocated automatically when the thread exits
202 (e.g. in a pthreads environment, when pthread_exit() is called). On Windows
203 platforms this is done in response to a DLL_THREAD_DETACH message being sent to
204 the libeay32.dll entry point. Some windows functions may cause threads to exit
205 without sending this message (for example ExitProcess()). If the application
206 uses such functions, then the application must free up OpenSSL resources
207 directly via a call to OPENSSL_INIT_thread_stop(). Similarly this message will
208 also not be sent if OpenSSL is linked statically, and therefore applications
209 using static linking should also call OPENSSL_INIT_thread_stop().
210
211 =head1 RETURN VALUES
212
213 The function OPENSSL_INIT_register_stop_handler() returns 1 on success or 0 on
214 error.
215
216 =head1 SEE ALSO
217
218 L<OPENSSL_INIT_ssl_library_start(3)>
219
220 =head1 HISTORY
221
222 The OPENSSL_INIT_library_stop, OPENSSL_INIT_crypto_library_start,
223 OPENSSL_INIT_register_stop_handler and OPENSSL_INIT_thread_stop functions were
224 first added in OpenSSL 1.1.0.
225
226 =cut