OPENSSL_init_crypto.pod 12 KB

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