OPENSSL_init_crypto.pod 11 KB

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