fips_module.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. =pod
  2. =head1 NAME
  3. fips_module - OpenSSL fips module guide
  4. =head1 SYNOPSIS
  5. See the individual manual pages for details.
  6. =head1 DESCRIPTION
  7. This guide details different ways that OpenSSL can be used in conjunction
  8. with the FIPS module. Which is the correct approach to use will depend on your
  9. own specific circumstances and what you are attempting to achieve.
  10. Note that the old functions FIPS_mode() and FIPS_mode_set() are no longer
  11. present so you must remove them from your application if you use them.
  12. Applications written to use the OpenSSL 3.0 FIPS module should not use any
  13. legacy APIs or features that avoid the FIPS module. Specifically this includes:
  14. =over 4
  15. =item *
  16. Low level cryptographic APIs (use the high level APIs, such as EVP, instead)
  17. =item *
  18. Engines
  19. =item *
  20. Any functions that create or modify custom "METHODS" (for example
  21. EVP_MD_meth_new(), EVP_CIPHER_meth_new(), EVP_PKEY_meth_new(), RSA_meth_new(),
  22. EC_KEY_METHOD_new(), etc.)
  23. =back
  24. All of the above APIs are deprecated in OpenSSL 3.0 - so a simple rule is to
  25. avoid using all deprecated functions. See L<migration_guide(7)> for a list of
  26. deprecated functions.
  27. =head2 Making all applications use the FIPS module by default
  28. One simple approach is to cause all applications that are using OpenSSL to only
  29. use the FIPS module for cryptographic algorithms by default.
  30. This approach can be done purely via configuration. As long as applications are
  31. built and linked against OpenSSL 3.0 and do not override the loading of the
  32. default config file or its settings then they can automatically start using the
  33. FIPS module without the need for any further code changes.
  34. To do this the default OpenSSL config file will have to be modified. The
  35. location of this config file will depend on the platform, and any options that
  36. were given during the build process. You can check the location of the config
  37. file by running this command:
  38. $ openssl version -d
  39. OPENSSLDIR: "/usr/local/ssl"
  40. Caution: Many Operating Systems install OpenSSL by default. It is a common error
  41. to not have the correct version of OpenSSL in your $PATH. Check that you are
  42. running an OpenSSL 3.0 version like this:
  43. $ openssl version -v
  44. OpenSSL 3.0.0-dev xx XXX xxxx (Library: OpenSSL 3.0.0-dev xx XXX xxxx)
  45. The B<OPENSSLDIR> value above gives the directory name for where the default
  46. config file is stored. So in this case the default config file will be called
  47. F</usr/local/ssl/openssl.cnf>.
  48. Edit the config file to add the following lines near the beginning:
  49. config_diagnostics = 1
  50. openssl_conf = openssl_init
  51. .include /usr/local/ssl/fipsmodule.cnf
  52. [openssl_init]
  53. providers = provider_sect
  54. [provider_sect]
  55. fips = fips_sect
  56. base = base_sect
  57. [base_sect]
  58. activate = 1
  59. Obviously the include file location above should match the path and name of the
  60. FIPS module config file that you installed earlier.
  61. See L<https://github.com/openssl/openssl/blob/master/README-FIPS.md>.
  62. For FIPS usage, it is recommended that the B<config_diagnostics> option is
  63. enabled to prevent accidental use of non-FIPS validated algorithms via broken
  64. or mistaken configuration. See L<config(5)>.
  65. Any applications that use OpenSSL 3.0 and are started after these changes are
  66. made will start using only the FIPS module unless those applications take
  67. explicit steps to avoid this default behaviour. Note that this configuration
  68. also activates the "base" provider. The base provider does not include any
  69. cryptographic algorithms (and therefore does not impact the validation status of
  70. any cryptographic operations), but does include other supporting algorithms that
  71. may be required. It is designed to be used in conjunction with the FIPS module.
  72. This approach has the primary advantage that it is simple, and no code changes
  73. are required in applications in order to benefit from the FIPS module. There are
  74. some disadvantages to this approach:
  75. =over 4
  76. =item *
  77. You may not want all applications to use the FIPS module.
  78. It may be the case that some applications should and some should not use the
  79. FIPS module.
  80. =item *
  81. If applications take explicit steps to not load the default config file or
  82. set different settings.
  83. This method will not work for these cases.
  84. =item *
  85. The algorithms available in the FIPS module are a subset of the algorithms
  86. that are available in the default OpenSSL Provider.
  87. If any applications attempt to use any algorithms that are not present,
  88. then they will fail.
  89. =item *
  90. Usage of certain deprecated APIs avoids the use of the FIPS module.
  91. If any applications use those APIs then the FIPS module will not be used.
  92. =back
  93. =head2 Selectively making applications use the FIPS module by default
  94. A variation on the above approach is to do the same thing on an individual
  95. application basis. The default OpenSSL config file depends on the compiled in
  96. value for B<OPENSSLDIR> as described in the section above. However it is also
  97. possible to override the config file to be used via the B<OPENSSL_CONF>
  98. environment variable. For example the following, on Unix, will cause the
  99. application to be executed with a non-standard config file location:
  100. $ OPENSSL_CONF=/my/nondefault/openssl.cnf myapplication
  101. Using this mechanism you can control which config file is loaded (and hence
  102. whether the FIPS module is loaded) on an application by application basis.
  103. This removes the disadvantage listed above that you may not want all
  104. applications to use the FIPS module. All the other advantages and disadvantages
  105. still apply.
  106. =head2 Programmatically loading the FIPS module (default library context)
  107. Applications may choose to load the FIPS provider explicitly rather than relying
  108. on config to do this. The config file is still necessary in order to hold the
  109. FIPS module config data (such as its self test status and integrity data). But
  110. in this case we do not automatically activate the FIPS provider via that config
  111. file.
  112. To do things this way configure as per
  113. L</Making all applications use the FIPS module by default> above, but edit the
  114. F<fipsmodule.cnf> file to remove or comment out the line which says
  115. C<activate = 1> (note that setting this value to 0 is I<not> sufficient).
  116. This means all the required config information will be available to load the
  117. FIPS module, but it is not automatically loaded when the application starts. The
  118. FIPS provider can then be loaded programmatically like this:
  119. #include <openssl/provider.h>
  120. int main(void)
  121. {
  122. OSSL_PROVIDER *fips;
  123. OSSL_PROVIDER *base;
  124. fips = OSSL_PROVIDER_load(NULL, "fips");
  125. if (fips == NULL) {
  126. printf("Failed to load FIPS provider\n");
  127. exit(EXIT_FAILURE);
  128. }
  129. base = OSSL_PROVIDER_load(NULL, "base");
  130. if (base == NULL) {
  131. OSSL_PROVIDER_unload(fips);
  132. printf("Failed to load base provider\n");
  133. exit(EXIT_FAILURE);
  134. }
  135. /* Rest of application */
  136. OSSL_PROVIDER_unload(base);
  137. OSSL_PROVIDER_unload(fips);
  138. exit(EXIT_SUCCESS);
  139. }
  140. Note that this should be one of the first things that you do in your
  141. application. If any OpenSSL functions get called that require the use of
  142. cryptographic functions before this occurs then, if no provider has yet been
  143. loaded, then the default provider will be automatically loaded. If you then
  144. later explicitly load the FIPS provider then you will have both the FIPS and the
  145. default provider loaded at the same time. It is undefined which implementation
  146. of an algorithm will be used if multiple implementations are available and you
  147. have not explicitly specified via a property query (see below) which one should
  148. be used.
  149. Also note that in this example we have additionally loaded the "base" provider.
  150. This loads a sub-set of algorithms that are also available in the default
  151. provider - specifically non cryptographic ones which may be used in conjunction
  152. with the FIPS provider. For example this contains algorithms for encoding and
  153. decoding keys. If you decide not to load the default provider then you
  154. will usually want to load the base provider instead.
  155. In this example we are using the "default" library context. OpenSSL functions
  156. operate within the scope of a library context. If no library context is
  157. explicitly specified then the default library context is used. For further
  158. details about library contexts see the L<OSSL_LIB_CTX(3)> man page.
  159. =head2 Loading the FIPS module at the same time as other providers
  160. It is possible to have the FIPS provider and other providers (such as the
  161. default provider) all loaded at the same time into the same library context. You
  162. can use a property query string during algorithm fetches to specify which
  163. implementation you would like to use.
  164. For example to fetch an implementation of SHA256 which conforms to FIPS
  165. standards you can specify the property query C<fips=yes> like this:
  166. EVP_MD *sha256;
  167. sha256 = EVP_MD_fetch(NULL, "SHA2-256", "fips=yes");
  168. If no property query is specified, or more than one implementation matches the
  169. property query then it is undefined which implementation of a particular
  170. algorithm will be returned.
  171. This example shows an explicit request for an implementation of SHA256 from the
  172. default provider:
  173. EVP_MD *sha256;
  174. sha256 = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
  175. It is also possible to set a default property query string. The following
  176. example sets the default property query of C<fips=yes> for all fetches within
  177. the default library context:
  178. EVP_set_default_properties(NULL, "fips=yes");
  179. If a fetch function has both an explicit property query specified, and a
  180. default property query is defined then the two queries are merged together and
  181. both apply. The local property query overrides the default properties if the
  182. same property name is specified in both.
  183. There are two important built-in properties that you should be aware of:
  184. The "provider" property enables you to specify which provider you want an
  185. implementation to be fetched from, e.g. C<provider=default> or C<provider=fips>.
  186. All algorithms implemented in a provider have this property set on them.
  187. There is also the C<fips> property. All FIPS algorithms match against the
  188. property query C<fips=yes>. There are also some non-cryptographic algorithms
  189. available in the default and base providers that also have the C<fips=yes>
  190. property defined for them. These are the encoder and decoder algorithms that
  191. can (for example) be used to write out a key generated in the FIPS provider to a
  192. file. The encoder and decoder algorithms are not in the FIPS module itself but
  193. are allowed to be used in conjunction with the FIPS algorithms.
  194. It is possible to specify default properties within a config file. For example
  195. the following config file automatically loads the default and FIPS providers and
  196. sets the default property value to be C<fips=yes>. Note that this config file
  197. does not load the "base" provider. All supporting algorithms that are in "base"
  198. are also in "default", so it is unnecessary in this case:
  199. config_diagnostics = 1
  200. openssl_conf = openssl_init
  201. .include /usr/local/ssl/fipsmodule.cnf
  202. [openssl_init]
  203. providers = provider_sect
  204. alg_section = algorithm_sect
  205. [provider_sect]
  206. fips = fips_sect
  207. default = default_sect
  208. [default_sect]
  209. activate = 1
  210. [algorithm_sect]
  211. default_properties = fips=yes
  212. =head2 Programmatically loading the FIPS module (nondefault library context)
  213. In addition to using properties to separate usage of the FIPS module from other
  214. usages this can also be achieved using library contexts. In this example we
  215. create two library contexts. In one we assume the existence of a config file
  216. called F<openssl-fips.cnf> that automatically loads and configures the FIPS and
  217. base providers. The other library context will just use the default provider.
  218. OSSL_LIB_CTX *fips_libctx, *nonfips_libctx;
  219. OSSL_PROVIDER *defctxnull = NULL;
  220. EVP_MD *fipssha256 = NULL, *nonfipssha256 = NULL;
  221. int ret = 1;
  222. /*
  223. * Create two nondefault library contexts. One for fips usage and
  224. * one for non-fips usage
  225. */
  226. fips_libctx = OSSL_LIB_CTX_new();
  227. nonfips_libctx = OSSL_LIB_CTX_new();
  228. if (fips_libctx == NULL || nonfips_libctx == NULL)
  229. goto err;
  230. /* Prevent anything from using the default library context */
  231. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  232. /*
  233. * Load config file for the FIPS library context. We assume that
  234. * this config file will automatically activate the FIPS and base
  235. * providers so we don't need to explicitly load them here.
  236. */
  237. if (!OSSL_LIB_CTX_load_config(fips_libctx, "openssl-fips.cnf"))
  238. goto err;
  239. /*
  240. * We don't need to do anything special to load the default
  241. * provider into nonfips_libctx. This happens automatically if no
  242. * other providers are loaded.
  243. * Because we don't call OSSL_LIB_CTX_load_config() explicitly for
  244. * nonfips_libctx it will just use the default config file.
  245. */
  246. /* As an example get some digests */
  247. /* Get a FIPS validated digest */
  248. fipssha256 = EVP_MD_fetch(fips_libctx, "SHA2-256", NULL);
  249. if (fipssha256 == NULL)
  250. goto err;
  251. /* Get a non-FIPS validated digest */
  252. nonfipssha256 = EVP_MD_fetch(nonfips_libctx, "SHA2-256", NULL);
  253. if (nonfipssha256 == NULL)
  254. goto err;
  255. /* Use the digests */
  256. printf("Success\n");
  257. ret = 0;
  258. err:
  259. EVP_MD_free(fipssha256);
  260. EVP_MD_free(nonfipssha256);
  261. OSSL_LIB_CTX_free(fips_libctx);
  262. OSSL_LIB_CTX_free(nonfips_libctx);
  263. OSSL_PROVIDER_unload(defctxnull);
  264. return ret;
  265. Note that we have made use of the special "null" provider here which we load
  266. into the default library context. We could have chosen to use the default
  267. library context for FIPS usage, and just create one additional library context
  268. for other usages - or vice versa. However if code has not been converted to use
  269. library contexts then the default library context will be automatically used.
  270. This could be the case for your own existing applications as well as certain
  271. parts of OpenSSL itself. Not all parts of OpenSSL are library context aware. If
  272. this happens then you could "accidentally" use the wrong library context for a
  273. particular operation. To be sure this doesn't happen you can load the "null"
  274. provider into the default library context. Because a provider has been
  275. explicitly loaded, the default provider will not automatically load. This means
  276. code using the default context by accident will fail because no algorithms will
  277. be available.
  278. See L<migration_guide(7)/Library Context> for additional information about the
  279. Library Context.
  280. =head2 Using Encoders and Decoders with the FIPS module
  281. Encoders and decoders are used to read and write keys or parameters from or to
  282. some external format (for example a PEM file). If your application generates
  283. keys or parameters that then need to be written into PEM or DER format
  284. then it is likely that you will need to use an encoder to do this. Similarly
  285. you need a decoder to read previously saved keys and parameters. In most cases
  286. this will be invisible to you if you are using APIs that existed in
  287. OpenSSL 1.1.1 or earlier such as L<i2d_PrivateKey(3)>. However the appropriate
  288. encoder/decoder will need to be available in the library context associated with
  289. the key or parameter object. The built-in OpenSSL encoders and decoders are
  290. implemented in both the default and base providers and are not in the FIPS
  291. module boundary. However since they are not cryptographic algorithms themselves
  292. it is still possible to use them in conjunction with the FIPS module, and
  293. therefore these encoders/decoders have the C<fips=yes> property against them.
  294. You should ensure that either the default or base provider is loaded into the
  295. library context in this case.
  296. =head2 Using the FIPS module in SSL/TLS
  297. Writing an application that uses libssl in conjunction with the FIPS module is
  298. much the same as writing a normal libssl application. If you are using global
  299. properties and the default library context to specify usage of FIPS validated
  300. algorithms then this will happen automatically for all cryptographic algorithms
  301. in libssl. If you are using a nondefault library context to load the FIPS
  302. provider then you can supply this to libssl using the function
  303. L<SSL_CTX_new_ex(3)>. This works as a drop in replacement for the function
  304. L<SSL_CTX_new(3)> except it provides you with the capability to specify the
  305. library context to be used. You can also use the same function to specify
  306. libssl specific properties to use.
  307. In this first example we create two SSL_CTX objects using two different library
  308. contexts.
  309. /*
  310. * We assume that a nondefault library context with the FIPS
  311. * provider loaded has been created called fips_libctx.
  312. */
  313. SSL_CTX *fips_ssl_ctx = SSL_CTX_new_ex(fips_libctx, NULL, TLS_method());
  314. /*
  315. * We assume that a nondefault library context with the default
  316. * provider loaded has been created called non_fips_libctx.
  317. */
  318. SSL_CTX *non_fips_ssl_ctx = SSL_CTX_new_ex(non_fips_libctx, NULL,
  319. TLS_method());
  320. In this second example we create two SSL_CTX objects using different properties
  321. to specify FIPS usage:
  322. /*
  323. * The "fips=yes" property includes all FIPS approved algorithms
  324. * as well as encoders from the default provider that are allowed
  325. * to be used. The NULL below indicates that we are using the
  326. * default library context.
  327. */
  328. SSL_CTX *fips_ssl_ctx = SSL_CTX_new_ex(NULL, "fips=yes", TLS_method());
  329. /*
  330. * The "provider!=fips" property allows algorithms from any
  331. * provider except the FIPS provider
  332. */
  333. SSL_CTX *non_fips_ssl_ctx = SSL_CTX_new_ex(NULL, "provider!=fips",
  334. TLS_method());
  335. =head2 Confirming that an algorithm is being provided by the FIPS module
  336. A chain of links needs to be followed to go from an algorithm instance to the
  337. provider that implements it. The process is similar for all algorithms. Here the
  338. example of a digest is used.
  339. To go from an B<EVP_MD_CTX> to an B<EVP_MD>, use L<EVP_MD_CTX_md(3)> .
  340. To go from the B<EVP_MD> to its B<OSSL_PROVIDER>,
  341. use L<EVP_MD_get0_provider(3)>.
  342. To extract the name from the B<OSSL_PROVIDER>, use
  343. L<OSSL_PROVIDER_get0_name(3)>.
  344. =head1 SEE ALSO
  345. L<migration_guide(7)>, L<crypto(7)>, L<fips_config(5)>
  346. =head1 COPYRIGHT
  347. Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  348. Licensed under the Apache License 2.0 (the "License"). You may not use
  349. this file except in compliance with the License. You can obtain a copy
  350. in the file LICENSE in the source distribution or at
  351. L<https://www.openssl.org/source/license.html>.
  352. =cut