ossl-guide-libraries-introduction.pod 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. =pod
  2. =head1 NAME
  3. ossl-guide-libraries-introduction
  4. - OpenSSL Guide: An introduction to the OpenSSL libraries
  5. =head1 INTRODUCTION
  6. OpenSSL supplies two libraries that can be used by applications known as
  7. C<libcrypto> and C<libssl>.
  8. The C<libcrypto> library provides APIs for general purpose cryptography such as
  9. encryption, digital signatures, hash functions, etc. It additionally supplies
  10. supporting APIs for cryptography related standards, e.g. for reading and writing
  11. digital certificates (also known as X.509 certificates). Finally it also
  12. supplies various additional supporting APIs that are not directly cryptography
  13. related but are nonetheless useful and depended upon by other APIs. For
  14. example the "BIO" functions provide capabilities for abstracting I/O, e.g. via a
  15. file or over a network.
  16. The C<libssl> library provides functions to perform secure communication between
  17. two peers across a network. Most significantly it implements support for the
  18. SSL/TLS, DTLS and QUIC standards.
  19. The C<libssl> library depends on and uses many of the capabilities supplied by
  20. C<libcrypto>. Any application linked against C<libssl> will also link against
  21. C<libcrypto>, and most applications that do this will directly use API functions
  22. supplied by both libraries.
  23. Applications may be written that only use C<libcrypto> capabilities and do not
  24. link against C<libssl> at all.
  25. =head1 PROVIDERS
  26. As well as the two main libraries, OpenSSL also comes with a set of providers.
  27. A provider in OpenSSL is a component that collects together algorithm
  28. implementations (for example an implementation of the symmetric encryption
  29. algorithm AES). In order to use an algorithm you must have at least one
  30. provider loaded that contains an implementation of it. OpenSSL comes with a
  31. number of providers and they may also be obtained from third parties.
  32. Providers may either be "built-in" or in the form of a separate loadable module
  33. file (typically one ending in ".so" or ".dll" dependent on the platform). A
  34. built-in provider is one that is either already present in C<libcrypto> or one
  35. that the application has supplied itself directly. Third parties can also supply
  36. providers in the form of loadable modules.
  37. If you don't load a provider explicitly (either in program code or via config)
  38. then the OpenSSL built-in "default" provider will be automatically loaded.
  39. See L</OPENSSL PROVIDERS> below for a description of the providers that OpenSSL
  40. itself supplies.
  41. Loading and unloading providers is quite an expensive operation. It is normally
  42. done once, early on in the application lifecycle and those providers are kept
  43. loaded for the duration of the application execution.
  44. =head1 LIBRARY CONTEXTS
  45. Many OpenSSL API functions make use of a library context. A library context can
  46. be thought of as a "scope" within which configuration options take effect. When
  47. a provider is loaded, it is only loaded within the scope of a given library
  48. context. In this way it is possible for different components of a complex
  49. application to each use a different library context and have different providers
  50. loaded with different configuration settings.
  51. If an application does not explicitly create a library context then the
  52. "default" library context will be used.
  53. Library contexts are represented by the B<OSSL_LIB_CTX> type. Many OpenSSL API
  54. functions take a library context as a parameter. Applications can always pass
  55. B<NULL> for this parameter to just use the default library context.
  56. The default library context is automatically created the first time it is
  57. needed. This will automatically load any available configuration file and will
  58. initialise OpenSSL for use. Unlike in earlier versions of OpenSSL (prior to
  59. 1.1.0) no explicit initialisation steps need to be taken.
  60. Similarly when the application exits, the default library context is
  61. automatically destroyed. No explicit de-initialisation steps need to be taken.
  62. See L<OSSL_LIB_CTX(3)> for more information about library contexts.
  63. See also L<ossl-guide-libcrypto-introduction(7)/ALGORITHM FETCHING>.
  64. =head1 PROPERTY QUERY STRINGS
  65. In some cases the available providers may mean that more than one implementation
  66. of any given algorithm might be available. For example the OpenSSL FIPS provider
  67. supplies alternative implementations of many of the same algorithms that are
  68. available in the OpenSSL default provider.
  69. The process of selecting an algorithm implementation is known as "fetching".
  70. When OpenSSL fetches an algorithm to use it is possible to specify a "property
  71. query string" to guide the selection process. For example a property query
  72. string of "provider=default" could be used to force the selection to only
  73. consider algorithm implementations in the default provider.
  74. Property query strings can be specified explicitly as an argument to a function.
  75. It is also possible to specify a default property query string for the whole
  76. library context using the L<EVP_set_default_properties(3)> or
  77. L<EVP_default_properties_enable_fips(3)> functions. Where both
  78. default properties and function specific properties are specified then they are
  79. combined. Function specific properties will override default properties where
  80. there is a conflict.
  81. See L<ossl-guide-libcrypto-introduction(7)/ALGORITHM FETCHING> for more
  82. information about fetching. See L<property(7)> for more information about
  83. properties.
  84. =head1 MULTI-THREADED APPLICATIONS
  85. As long as OpenSSL has been built with support for threads (the default case
  86. on most platforms) then most OpenSSL I<functions> are thread-safe in the sense
  87. that it is safe to call the same function from multiple threads at the same
  88. time. However most OpenSSL I<data structures> are not thread-safe. For example
  89. the L<BIO_write(3)> and L<BIO_read(3)> functions are thread safe. However it
  90. would not be thread safe to call BIO_write() from one thread while calling
  91. BIO_read() in another where both functions are passed the same B<BIO> object
  92. since both of them may attempt to make changes to the same B<BIO> object.
  93. There are exceptions to these rules. A small number of functions are not thread
  94. safe at all. Where this is the case this restriction should be noted in the
  95. documentation for the function. Similarly some data structures may be partially
  96. or fully thread safe. For example it is always safe to use an B<OSSL_LIB_CTX> in
  97. multiple threads.
  98. See L<openssl-threads(7)> for a more detailed discussion on OpenSSL threading
  99. support.
  100. =head1 ERROR HANDLING
  101. Most OpenSSL functions will provide a return value indicating whether the
  102. function has been successful or not. It is considered best practice to always
  103. check the return value from OpenSSL functions (where one is available).
  104. Most functions that return a pointer value will return NULL in the event of a
  105. failure.
  106. Most functions that return an integer value will return a positive integer for
  107. success. Some of these functions will return 0 to indicate failure. Others may
  108. return 0 or a negative value for failure.
  109. Some functions cannot fail and have a B<void> return type. There are also a
  110. small number of functions that do not conform to the above conventions (e.g.
  111. they may return 0 to indicate success).
  112. Due to the above variations in behaviour it is important to check the
  113. documentation for each function for information about how to interpret the
  114. return value for it.
  115. It is sometimes necessary to get further information about the cause of a
  116. failure (e.g. for debugging or logging purposes). Many (but not all) functions
  117. will add further information about a failure to the OpenSSL error stack. By
  118. using the error stack you can find out information such as a reason code/string
  119. for the error as well as the exact file and source line within OpenSSL that
  120. emitted the error.
  121. OpenSSL supplies a set of error handling functions to query the error stack. See
  122. L<ERR_get_error(3)> for information about the functions available for querying
  123. error data. Also see L<ERR_print_errors(3)> for information on some simple
  124. helper functions for printing error data. Finally look at L<ERR_clear_error(3)>
  125. for how to clear old errors from the error stack.
  126. =head1 OPENSSL PROVIDERS
  127. OpenSSL comes with a set of providers.
  128. The algorithms available in each of these providers may vary due to build time
  129. configuration options. The L<openssl-list(1)> command can be used to list the
  130. currently available algorithms.
  131. The names of the algorithms shown from L<openssl-list(1)> can be used as an
  132. algorithm identifier to the appropriate fetching function. Also see the provider
  133. specific manual pages linked below for further details about using the
  134. algorithms available in each of the providers.
  135. As well as the OpenSSL providers third parties can also implement providers.
  136. For information on writing a provider see L<provider(7)>.
  137. =head2 Default provider
  138. The default provider is built-in as part of the F<libcrypto> library and
  139. contains all of the most commonly used algorithm implementations. Should it be
  140. needed (if other providers are loaded and offer implementations of the same
  141. algorithms), the property query string "provider=default" can be used as a
  142. search criterion for these implementations. The default provider includes all
  143. of the functionality in the base provider below.
  144. If you don't load any providers at all then the "default" provider will be
  145. automatically loaded. If you explicitly load any provider then the "default"
  146. provider would also need to be explicitly loaded if it is required.
  147. See L<OSSL_PROVIDER-default(7)>.
  148. =head2 Base provider
  149. The base provider is built in as part of the F<libcrypto> library and contains
  150. algorithm implementations for encoding and decoding of OpenSSL keys.
  151. Should it be needed (if other providers are loaded and offer
  152. implementations of the same algorithms), the property query string
  153. "provider=base" can be used as a search criterion for these implementations.
  154. Some encoding and decoding algorithm implementations are not FIPS algorithm
  155. implementations in themselves but support algorithms from the FIPS provider and
  156. are allowed for use in "FIPS mode". The property query string "fips=yes" can be
  157. used to select such algorithms.
  158. See L<OSSL_PROVIDER-base(7)>.
  159. =head2 FIPS provider
  160. The FIPS provider is a dynamically loadable module, and must therefore
  161. be loaded explicitly, either in code or through OpenSSL configuration
  162. (see L<config(5)>). It contains algorithm implementations that have been
  163. validated according to FIPS standards. Should it be needed (if other
  164. providers are loaded and offer implementations of the same algorithms), the
  165. property query string "provider=fips" can be used as a search criterion for
  166. these implementations. All approved algorithm implementations in the FIPS
  167. provider can also be selected with the property "fips=yes". The FIPS provider
  168. may also contain non-approved algorithm implementations and these can be
  169. selected with the property "fips=no".
  170. Typically the L</Base provider> will also need to be loaded because the FIPS
  171. provider does not support the encoding or decoding of keys.
  172. See L<OSSL_PROVIDER-FIPS(7)> and L<fips_module(7)>.
  173. =head2 Legacy provider
  174. The legacy provider is a dynamically loadable module, and must therefore
  175. be loaded explicitly, either in code or through OpenSSL configuration
  176. (see L<config(5)>). It contains algorithm implementations that are considered
  177. insecure, or are no longer in common use such as MD2 or RC4. Should it be needed
  178. (if other providers are loaded and offer implementations of the same algorithms),
  179. the property "provider=legacy" can be used as a search criterion for these
  180. implementations.
  181. See L<OSSL_PROVIDER-legacy(7)>.
  182. =head2 Null provider
  183. The null provider is built in as part of the F<libcrypto> library. It contains
  184. no algorithms in it at all. When fetching algorithms the default provider will
  185. be automatically loaded if no other provider has been explicitly loaded. To
  186. prevent that from happening you can explicitly load the null provider.
  187. You can use this if you create your own library context and want to ensure that
  188. all API calls have correctly passed the created library context and are not
  189. accidentally using the default library context. Load the null provider into the
  190. default library context so that the default library context has no algorithm
  191. implementations available.
  192. See L<OSSL_PROVIDER-null(7)>.
  193. =head1 CONFIGURATION
  194. By default OpenSSL will load a configuration file when it is first used. This
  195. will set up various configuration settings within the default library context.
  196. Applications that create their own library contexts may optionally configure
  197. them with a config file using the L<OSSL_LIB_CTX_load_config(3)> function.
  198. The configuration file can be used to automatically load providers and set up
  199. default property query strings.
  200. For information on the OpenSSL configuration file format see L<config(5)>.
  201. =head1 LIBRARY CONVENTIONS
  202. Many OpenSSL functions that "get" or "set" a value follow a naming convention
  203. using the numbers B<0> and B<1>, i.e. "get0", "get1", "set0" and "set1". This
  204. can also apply to some functions that "add" a value to an existing set, i.e.
  205. "add0" and "add1".
  206. For example the functions:
  207. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
  208. int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj);
  209. In the B<0> version the ownership of the object is passed to (for an add or set)
  210. or retained by (for a get) the parent object. For example after calling the
  211. X509_CRL_add0_revoked() function above, ownership of the I<rev> object is passed
  212. to the I<crl> object. Therefore, after calling this function I<rev> should not
  213. be freed directly. It will be freed implicitly when I<crl> is freed.
  214. In the B<1> version the ownership of the object is not passed to or retained by
  215. the parent object. Instead a copy or "up ref" of the object is performed. So
  216. after calling the X509_add1_trust_object() function above the application will
  217. still be responsible for freeing the I<obj> value where appropriate.
  218. Many OpenSSL functions conform to a naming convention of the form
  219. B<CLASSNAME_func_name()>. In this naming convention the B<CLASSNAME> is the name
  220. of an OpenSSL data structure (given in capital letters) that the function is
  221. primarily operating on. The B<func_name> portion of the name is usually in
  222. lowercase letters and indicates the purpose of the function.
  223. =head1 DEMO APPLICATIONS
  224. OpenSSL is distributed with a set of demo applications which provide some
  225. examples of how to use the various API functions. To look at them download the
  226. OpenSSL source code from the OpenSSL website
  227. (L<https://www.openssl.org/source/>). Extract the downloaded B<.tar.gz> file for
  228. the version of OpenSSL that you are using and look at the various files in the
  229. B<demos> sub-directory.
  230. The Makefiles in the subdirectories give instructions on how to build and run
  231. the demo applications.
  232. =head1 FURTHER READING
  233. See L<ossl-guide-libcrypto-introduction(7)> for a more detailed introduction to
  234. using C<libcrypto> and L<ossl-guide-libssl-introduction(7)> for more information
  235. on C<libssl>.
  236. =head1 SEE ALSO
  237. L<openssl(1)>, L<ssl(7)>, L<evp(7)>, L<OSSL_LIB_CTX(3)>, L<openssl-threads(7)>,
  238. L<property(7)>, L<OSSL_PROVIDER-default(7)>, L<OSSL_PROVIDER-base(7)>,
  239. L<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-legacy(7)>, L<OSSL_PROVIDER-null(7)>,
  240. L<openssl-glossary(7)>, L<provider(7)>
  241. =head1 COPYRIGHT
  242. Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
  243. Licensed under the Apache License 2.0 (the "License"). You may not use
  244. this file except in compliance with the License. You can obtain a copy
  245. in the file LICENSE in the source distribution or at
  246. L<https://www.openssl.org/source/license.html>.
  247. =cut