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