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