crypto.pod 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. =pod
  2. =head1 NAME
  3. crypto - OpenSSL cryptographic library
  4. =head1 SYNOPSIS
  5. See the individual manual pages for details.
  6. =head1 DESCRIPTION
  7. The OpenSSL crypto library (C<libcrypto>) implements a wide range of
  8. cryptographic algorithms used in various Internet standards. The services
  9. provided by this library are used by the OpenSSL implementations of TLS and
  10. CMS, and they have also been used to implement many other third party products
  11. and protocols.
  12. The functionality includes symmetric encryption, public key cryptography, key
  13. agreement, certificate handling, cryptographic hash functions, cryptographic
  14. pseudo-random number generators, message authentication codes (MACs), key
  15. derivation functions (KDFs), and various utilities.
  16. =head2 Algorithms
  17. Cryptographic primitives such as the SHA256 digest, or AES encryption are
  18. referred to in OpenSSL as "algorithms". Each algorithm may have multiple
  19. implementations available for use. For example the RSA algorithm is available as
  20. a "default" implementation suitable for general use, and a "fips" implementation
  21. which has been validated to FIPS standards for situations where that is
  22. important. It is also possible that a third party could add additional
  23. implementations such as in a hardware security module (HSM).
  24. =head2 Operations
  25. Different algorithms can be grouped together by their purpose. For example there
  26. are algorithms for encryption, and different algorithms for digesting data.
  27. These different groups are known as "operations" in OpenSSL. Each operation
  28. has a different set of functions associated with it. For example to perform an
  29. encryption operation using AES (or any other encryption algorithm) you would use
  30. the encryption functions detailed on the L<EVP_EncryptInit(3)> page. Or to
  31. perform a digest operation using SHA256 then you would use the digesting
  32. functions on the L<EVP_DigestInit(3)> page.
  33. =head2 Providers
  34. A provider in OpenSSL is a component that collects together algorithm
  35. implementations. In order to use an algorithm you must have at least one
  36. provider loaded that contains an implementation of it. OpenSSL comes with a
  37. number of providers and they may also be obtained from third parties. If you
  38. don't load a provider explicitly (either in program code or via config) then the
  39. OpenSSL built-in "default" provider will be automatically loaded.
  40. =head2 Library contexts
  41. A library context can be thought of as a "scope" within which configuration
  42. options take effect. When a provider is loaded, it is only loaded within the
  43. scope of a given library context. In this way it is possible for different
  44. components of a complex application to each use a different library context and
  45. have different providers loaded with different configuration settings.
  46. If an application does not explicitly create a library context then the
  47. "default" library context will be used.
  48. Library contexts are represented by the B<OSSL_LIB_CTX> type. Many OpenSSL API
  49. functions take a library context as a parameter. Applications can always pass
  50. B<NULL> for this parameter to just use the default library context.
  51. The default library context is automatically created the first time it is
  52. needed. This will automatically load any available configuration file and will
  53. initialise OpenSSL for use. Unlike in earlier versions of OpenSSL (prior to
  54. 1.1.0) no explicit initialisation steps need to be taken.
  55. Similarly when the application exits the default library context is
  56. automatically destroyed. No explicit de-initialisation steps need to be taken.
  57. See L<OSSL_LIB_CTX(3)> for more information about library contexts.
  58. See also L</ALGORITHM FETCHING>.
  59. =head2 Multi-threaded applications
  60. As long as OpenSSL has been built with support for threads (the default case
  61. on most platforms) then most OpenSSL I<functions> are thread-safe in the sense
  62. that it is safe to call the same function from multiple threads at the same
  63. time. However most OpenSSL I<data structures> are not thread-safe. For example
  64. the L<BIO_write(3)> and L<BIO_read(3)> functions are thread safe. However it
  65. would not be thread safe to call BIO_write() from one thread while calling
  66. BIO_read() in another where both functions are passed the same B<BIO> object
  67. since both of them may attempt to make changes to the same B<BIO> object.
  68. There are exceptions to these rules. A small number of functions are not thread
  69. safe at all. Where this is the case this restriction should be noted in the
  70. documentation for the function. Similarly some data structures may be partially
  71. or fully thread safe. For example it is safe to use an B<OSSL_LIB_CTX> in
  72. multiple threads.
  73. See L<openssl-threads(7)> for a more detailed discussion on OpenSSL threading
  74. support.
  75. =head1 ALGORITHM FETCHING
  76. In order to use an algorithm an implementation for it must first be "fetched".
  77. Fetching is the process of looking through the available implementations,
  78. applying selection criteria (via a property query string), and finally choosing
  79. the implementation that will be used.
  80. Two types of fetching are supported by OpenSSL - explicit fetching and implicit
  81. fetching.
  82. =head2 Property query strings
  83. When fetching an algorithm it is possible to specify a property query string to
  84. guide the selection process. For example a property query string of
  85. "provider=default" could be used to force the selection to only consider
  86. algorithm implementations in the default provider.
  87. Property query strings can be specified explicitly as an argument to a function.
  88. It is also possible to specify a default property query string for the whole
  89. library context using the L<EVP_set_default_properties(3)> function. Where both
  90. default properties and function specific properties are specified then they are
  91. combined. Function specific properties will override default properties where
  92. there is a conflict.
  93. See L<property(7)> for more information about properties.
  94. =head2 Explicit fetching
  95. Users of the OpenSSL libraries never query a provider directly for an algorithm
  96. implementation. Instead, the diverse OpenSSL APIs often have explicit fetching
  97. functions that do the work, and they return an appropriate algorithm object back
  98. to the user. These functions usually have the name C<APINAME_fetch>, where
  99. C<APINAME> is the name of the operation. For example L<EVP_MD_fetch(3)> can
  100. be used to explicitly fetch a digest algorithm implementation. The user is
  101. responsible for freeing the object returned from the C<APINAME_fetch> function
  102. using C<APINAME_free> when it is no longer needed.
  103. These fetching functions follow a fairly common pattern, where three
  104. arguments are passed:
  105. =over 4
  106. =item The library context
  107. See L<OSSL_LIB_CTX(3)> for a more detailed description.
  108. This may be NULL to signify the default (global) library context, or a
  109. context created by the user. Only providers loaded in this library context (see
  110. L<OSSL_PROVIDER_load(3)>) will be considered by the fetching function. In case
  111. no provider has been loaded in this library context then the default provider
  112. will be loaded as a fallback (see L<OSSL_PROVIDER-default(7)>).
  113. =item An identifier
  114. For all currently implemented fetching functions this is the algorithm name.
  115. =item A property query string
  116. The property query string used to guide selection of the algorithm
  117. implementation.
  118. =back
  119. The algorithm implementation that is fetched can then be used with other diverse
  120. functions that use them. For example the L<EVP_DigestInit_ex(3)> function takes
  121. as a parameter an B<EVP_MD> object which may have been returned from an earlier
  122. call to L<EVP_MD_fetch(3)>.
  123. =head2 Implicit fetch
  124. OpenSSL has a number of functions that return an algorithm object with no
  125. associated implementation, such as L<EVP_sha256(3)>,
  126. L<EVP_blake2b512(3)> or L<EVP_aes_128_cbc(3)>. These are present for
  127. compatibility with OpenSSL before version 3.0 where explicit fetching was not
  128. available.
  129. When they are used with functions like L<EVP_DigestInit_ex(3)> or
  130. L<EVP_CipherInit_ex(3)>, the actual implementation to be used is
  131. fetched implicitly using default search criteria.
  132. In some cases implicit fetching can also occur when a NULL algorithm parameter
  133. is supplied. In this case an algorithm implementation is implicitly fetched
  134. using default search criteria and an algorithm name that is consistent with
  135. the context in which it is being used.
  136. =head1 FETCHING EXAMPLES
  137. The following section provides a series of examples of fetching algorithm
  138. implementations.
  139. Fetch any available implementation of SHA2-256 in the default context. Note
  140. that some algorithms have aliases. So "SHA256" and "SHA2-256" are synonymous:
  141. EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
  142. ...
  143. EVP_MD_free(md);
  144. Fetch any available implementation of AES-128-CBC in the default context:
  145. EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES-128-CBC", NULL);
  146. ...
  147. EVP_CIPHER_free(cipher);
  148. Fetch an implementation of SHA2-256 from the default provider in the default
  149. context:
  150. EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
  151. ...
  152. EVP_MD_free(md);
  153. Fetch an implementation of SHA2-256 that is not from the default provider in the
  154. default context:
  155. EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider!=default");
  156. ...
  157. EVP_MD_free(md);
  158. Fetch an implementation of SHA2-256 from the default provider in the specified
  159. context:
  160. EVP_MD *md = EVP_MD_fetch(ctx, "SHA2-256", "provider=default");
  161. ...
  162. EVP_MD_free(md);
  163. Load the legacy provider into the default context and then fetch an
  164. implementation of WHIRLPOOL from it:
  165. /* This only needs to be done once - usually at application start up */
  166. OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
  167. EVP_MD *md = EVP_MD_fetch(NULL, "WHIRLPOOL", "provider=legacy");
  168. ...
  169. EVP_MD_free(md);
  170. Note that in the above example the property string "provider=legacy" is optional
  171. since, assuming no other providers have been loaded, the only implementation of
  172. the "whirlpool" algorithm is in the "legacy" provider. Also note that the
  173. default provider should be explicitly loaded if it is required in addition to
  174. other providers:
  175. /* This only needs to be done once - usually at application start up */
  176. OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
  177. OSSL_PROVIDER *default = OSSL_PROVIDER_load(NULL, "default");
  178. EVP_MD *md_whirlpool = EVP_MD_fetch(NULL, "whirlpool", NULL);
  179. EVP_MD *md_sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL);
  180. ...
  181. EVP_MD_free(md_whirlpool);
  182. EVP_MD_free(md_sha256);
  183. =head1 OPENSSL PROVIDERS
  184. OpenSSL comes with a set of providers.
  185. The algorithms available in each of these providers may vary due to build time
  186. configuration options. The L<openssl-list(1)> command can be used to list the
  187. currently available algorithms.
  188. The names of the algorithms shown from L<openssl-list(1)> can be used as an
  189. algorithm identifier to the appropriate fetching function. Also see the provider
  190. specific manual pages linked below for further details about using the
  191. algorithms available in each of the providers.
  192. As well as the OpenSSL providers third parties can also implemment providers.
  193. For information on writing a provider see L<provider(7)>.
  194. =head2 Default provider
  195. The default provider is built in as part of the F<libcrypto> library and
  196. contains all of the most commonly used algorithm implementations. Should it be
  197. needed (if other providers are loaded and offer implementations of the same
  198. algorithms), the property query string "provider=default" can be used as a
  199. search criterion for these implementations. The default provider includes all
  200. of the functionality in the base provider below.
  201. If you don't load any providers at all then the "default" provider will be
  202. automatically loaded. If you explicitly load any provider then the "default"
  203. provider would also need to be explicitly loaded if it is required.
  204. See L<OSSL_PROVIDER-default(7)>.
  205. =head2 Base provider
  206. The base provider is built in as part of the F<libcrypto> library and contains
  207. algorithm implementations for encoding and decoding for OpenSSL keys.
  208. Should it be needed (if other providers are loaded and offer
  209. implementations of the same algorithms), the property query string
  210. "provider=base" can be used as a search criterion for these implementations.
  211. Some encoding and decoding algorithm implementations are not FIPS algorithm
  212. implementations in themselves but support algorithms from the FIPS provider and
  213. are allowed for use in "FIPS mode". The property query string "fips=yes" can be
  214. used to select such algorithms.
  215. See L<OSSL_PROVIDER-base(7)>.
  216. =head2 FIPS provider
  217. The FIPS provider is a dynamically loadable module, and must therefore
  218. be loaded explicitly, either in code or through OpenSSL configuration
  219. (see L<config(5)>). It contains algorithm implementations that have been
  220. validated according to the FIPS 140-2 standard. Should it be needed (if other
  221. providers are loaded and offer implementations of the same algorithms), the
  222. property query string "provider=fips" can be used as a search criterion for
  223. these implementations. All approved algorithm implementations in the FIPS
  224. provider can also be selected with the property "fips=yes". The FIPS provider
  225. may also contain non-approved algorithm implementations and these can be
  226. selected with the property "fips=no".
  227. See L<OSSL_PROVIDER-FIPS(7)> and L<fips_module(7)>.
  228. =head2 Legacy provider
  229. The legacy provider is a dynamically loadable module, and must therefore
  230. be loaded explicitly, either in code or through OpenSSL configuration
  231. (see L<config(5)>). It contains algorithm implementations that are considered
  232. insecure, or are no longer in common use such as MD2 or RC4. Should it be needed
  233. (if other providers are loaded and offer implementations of the same algorithms),
  234. the property "provider=legacy" can be used as a search criterion for these
  235. implementations.
  236. See L<OSSL_PROVIDER-legacy(7)>.
  237. =head2 Null provider
  238. The null provider is built in as part of the F<libcrypto> library. It contains
  239. no algorithms in it at all. When fetching algorithms the default provider will
  240. be automatically loaded if no other provider has been explicitly loaded. To
  241. prevent that from happening you can explicitly load the null provider.
  242. See L<OSSL_PROVIDER-null(7)>.
  243. =head1 USING ALGORITHMS IN APPLICATIONS
  244. Cryptographic algorithms are made available to applications through use of the
  245. "EVP" APIs. Each of the various operations such as encryption, digesting,
  246. message authentication codes, etc., have a set of EVP function calls that can
  247. be invoked to use them. See the L<evp(7)> page for further details.
  248. Most of these follow a common pattern. A "context" object is first created. For
  249. example for a digest operation you would use an B<EVP_MD_CTX>, and for an
  250. encryption/decryption operation you would use an B<EVP_CIPHER_CTX>. The
  251. operation is then initialised ready for use via an "init" function - optionally
  252. passing in a set of parameters (using the B<OSSL_PARAM> type) to configure how
  253. the operation should behave. Next data is fed into the operation in a series of
  254. "update" calls. The operation is finalised using a "final" call which will
  255. typically provide some kind of output. Finally the context is cleaned up and
  256. freed.
  257. The following shows a complete example for doing this process for digesting
  258. data using SHA256. The process is similar for other operations such as
  259. encryption/decryption, signatures, message authentication codes, etc.
  260. #include <stdio.h>
  261. #include <openssl/evp.h>
  262. #include <openssl/bio.h>
  263. int main(void)
  264. {
  265. EVP_MD_CTX *ctx = NULL;
  266. EVP_MD *sha256 = NULL;
  267. const unsigned char msg[] = {
  268. 0x00, 0x01, 0x02, 0x03
  269. };
  270. unsigned int len = 0;
  271. unsigned char *outdigest = NULL;
  272. /* Create a context for the digest operation */
  273. ctx = EVP_MD_CTX_new();
  274. if (ctx == NULL)
  275. goto err;
  276. /*
  277. * Fetch the SHA256 algorithm implementation for doing the digest. We're
  278. * using the "default" library context here (first NULL parameter), and
  279. * we're not supplying any particular search criteria for our SHA256
  280. * implementation (second NULL parameter). Any SHA256 implementation will
  281. * do.
  282. */
  283. sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
  284. if (sha256 == NULL)
  285. goto err;
  286. /* Initialise the digest operation */
  287. if (!EVP_DigestInit_ex(ctx, sha256, NULL))
  288. goto err;
  289. /*
  290. * Pass the message to be digested. This can be passed in over multiple
  291. * EVP_DigestUpdate calls if necessary
  292. */
  293. if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
  294. goto err;
  295. /* Allocate the output buffer */
  296. outdigest = OPENSSL_malloc(EVP_MD_get_size(sha256));
  297. if (outdigest == NULL)
  298. goto err;
  299. /* Now calculate the digest itself */
  300. if (!EVP_DigestFinal_ex(ctx, outdigest, &len))
  301. goto err;
  302. /* Print out the digest result */
  303. BIO_dump_fp(stdout, outdigest, len);
  304. err:
  305. /* Clean up all the resources we allocated */
  306. OPENSSL_free(outdigest);
  307. EVP_MD_free(sha256);
  308. EVP_MD_CTX_free(ctx);
  309. }
  310. =head1 CONFIGURATION
  311. By default OpenSSL will load a configuration file when it is first used. This
  312. will set up various configuration settings within the default library context.
  313. Applications that create their own library contexts may optionally configure
  314. them with a config file using the L<OSSL_LIB_CTX_load_config(3)> function.
  315. The configuration file can be used to automatically load providers and set up
  316. default property query strings.
  317. For information on the OpenSSL configuration file format see L<config(5)>.
  318. =head1 ENCODING AND DECODING KEYS
  319. Many algorithms require the use of a key. Keys can be generated dynamically
  320. using the EVP APIs (for example see L<EVP_PKEY_Q_keygen(3)>). However it is often
  321. necessary to save or load keys (or their associated parameters) to or from some
  322. external format such as PEM or DER (see L<openssl-glossary(7)>). OpenSSL uses
  323. encoders and decoders to perform this task.
  324. Encoders and decoders are just algorithm implementations in the same way as
  325. any other algorithm implementation in OpenSSL. They are implemented by
  326. providers. The OpenSSL encoders and decoders are available in the default
  327. provider. They are also duplicated in the base provider.
  328. For information about encoders see L<OSSL_ENCODER_CTX_new_for_pkey(3)>. For
  329. information about decoders see L<OSSL_DECODER_CTX_new_for_pkey(3)>.
  330. =head1 LIBRARY CONVENTIONS
  331. Many OpenSSL functions that "get" or "set" a value follow a naming convention
  332. using the numbers B<0> and B<1>, i.e. "get0", "get1", "set0" and "set1". This
  333. can also apply to some functions that "add" a value to an existing set, i.e.
  334. "add0" and "add1".
  335. For example the functions:
  336. int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
  337. int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj);
  338. In the B<0> version the ownership of the object is passed to (for an add or set)
  339. or retained by (for a get) the parent object. For example after calling the
  340. X509_CRL_add0_revoked() function above, ownership of the I<rev> object is passed
  341. to the I<crl> object. Therefore, after calling this function I<rev> should not
  342. be freed directly. It will be freed implicitly when I<crl> is freed.
  343. In the B<1> version the ownership of the object is not passed to or retained by
  344. the parent object. Instead a copy or "up ref" of the object is performed. So
  345. after calling the X509_add1_trust_object() function above the application will
  346. still be responsible for freeing the I<obj> value where appropriate.
  347. =head1 SEE ALSO
  348. L<openssl(1)>, L<ssl(7)>, L<evp(7)>, L<OSSL_LIB_CTX(3)>, L<openssl-threads(7)>,
  349. L<property(7)>, L<OSSL_PROVIDER-default(7)>, L<OSSL_PROVIDER-base(7)>,
  350. L<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-legacy(7)>, L<OSSL_PROVIDER-null(7)>,
  351. L<openssl-glossary(7)>, L<provider(7)>
  352. =head1 COPYRIGHT
  353. Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
  354. Licensed under the Apache License 2.0 (the "License"). You may not use
  355. this file except in compliance with the License. You can obtain a copy
  356. in the file LICENSE in the source distribution or at
  357. L<https://www.openssl.org/source/license.html>.
  358. =cut