fips_module.pod 18 KB

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