fips_module.pod 20 KB

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