1b077aed3SPierre Pronchery=pod
2b077aed3SPierre Pronchery
3b077aed3SPierre Pronchery=head1 NAME
4b077aed3SPierre Pronchery
5b077aed3SPierre Proncheryfips_module - OpenSSL fips module guide
6b077aed3SPierre Pronchery
7b077aed3SPierre Pronchery=head1 SYNOPSIS
8b077aed3SPierre Pronchery
9b077aed3SPierre ProncherySee the individual manual pages for details.
10b077aed3SPierre Pronchery
11b077aed3SPierre Pronchery=head1 DESCRIPTION
12b077aed3SPierre Pronchery
13b077aed3SPierre ProncheryThis guide details different ways that OpenSSL can be used in conjunction
14b077aed3SPierre Proncherywith the FIPS module. Which is the correct approach to use will depend on your
15b077aed3SPierre Proncheryown specific circumstances and what you are attempting to achieve.
16b077aed3SPierre Pronchery
17*aa795734SPierre ProncheryFor information related to installing the FIPS module see
18*aa795734SPierre ProncheryL<https://github.com/openssl/openssl/blob/master/README-FIPS.md>.
19*aa795734SPierre Pronchery
20b077aed3SPierre ProncheryNote that the old functions FIPS_mode() and FIPS_mode_set() are no longer
21b077aed3SPierre Proncherypresent so you must remove them from your application if you use them.
22b077aed3SPierre Pronchery
23b077aed3SPierre ProncheryApplications written to use the OpenSSL 3.0 FIPS module should not use any
24b077aed3SPierre Proncherylegacy APIs or features that avoid the FIPS module. Specifically this includes:
25b077aed3SPierre Pronchery
26b077aed3SPierre Pronchery=over 4
27b077aed3SPierre Pronchery
28b077aed3SPierre Pronchery=item *
29b077aed3SPierre Pronchery
30b077aed3SPierre ProncheryLow level cryptographic APIs (use the high level APIs, such as EVP, instead)
31b077aed3SPierre Pronchery
32b077aed3SPierre Pronchery=item *
33b077aed3SPierre Pronchery
34b077aed3SPierre ProncheryEngines
35b077aed3SPierre Pronchery
36b077aed3SPierre Pronchery=item *
37b077aed3SPierre Pronchery
38b077aed3SPierre ProncheryAny functions that create or modify custom "METHODS" (for example
39b077aed3SPierre ProncheryEVP_MD_meth_new(), EVP_CIPHER_meth_new(), EVP_PKEY_meth_new(), RSA_meth_new(),
40b077aed3SPierre ProncheryEC_KEY_METHOD_new(), etc.)
41b077aed3SPierre Pronchery
42b077aed3SPierre Pronchery=back
43b077aed3SPierre Pronchery
44b077aed3SPierre ProncheryAll of the above APIs are deprecated in OpenSSL 3.0 - so a simple rule is to
45b077aed3SPierre Proncheryavoid using all deprecated functions. See L<migration_guide(7)> for a list of
46b077aed3SPierre Proncherydeprecated functions.
47b077aed3SPierre Pronchery
48b077aed3SPierre Pronchery=head2 Making all applications use the FIPS module by default
49b077aed3SPierre Pronchery
50b077aed3SPierre ProncheryOne simple approach is to cause all applications that are using OpenSSL to only
51b077aed3SPierre Proncheryuse the FIPS module for cryptographic algorithms by default.
52b077aed3SPierre Pronchery
53b077aed3SPierre ProncheryThis approach can be done purely via configuration. As long as applications are
54b077aed3SPierre Proncherybuilt and linked against OpenSSL 3.0 and do not override the loading of the
55b077aed3SPierre Proncherydefault config file or its settings then they can automatically start using the
56b077aed3SPierre ProncheryFIPS module without the need for any further code changes.
57b077aed3SPierre Pronchery
58b077aed3SPierre ProncheryTo do this the default OpenSSL config file will have to be modified. The
59b077aed3SPierre Proncherylocation of this config file will depend on the platform, and any options that
60b077aed3SPierre Proncherywere given during the build process. You can check the location of the config
61b077aed3SPierre Proncheryfile by running this command:
62b077aed3SPierre Pronchery
63b077aed3SPierre Pronchery    $ openssl version -d
64b077aed3SPierre Pronchery    OPENSSLDIR: "/usr/local/ssl"
65b077aed3SPierre Pronchery
66b077aed3SPierre ProncheryCaution: Many Operating Systems install OpenSSL by default. It is a common error
67b077aed3SPierre Proncheryto not have the correct version of OpenSSL in your $PATH. Check that you are
68b077aed3SPierre Proncheryrunning an OpenSSL 3.0 version like this:
69b077aed3SPierre Pronchery
70b077aed3SPierre Pronchery    $ openssl version -v
71b077aed3SPierre Pronchery    OpenSSL 3.0.0-dev xx XXX xxxx (Library: OpenSSL 3.0.0-dev xx XXX xxxx)
72b077aed3SPierre Pronchery
73b077aed3SPierre ProncheryThe B<OPENSSLDIR> value above gives the directory name for where the default
74b077aed3SPierre Proncheryconfig file is stored. So in this case the default config file will be called
75b077aed3SPierre ProncheryF</usr/local/ssl/openssl.cnf>.
76b077aed3SPierre Pronchery
77b077aed3SPierre ProncheryEdit the config file to add the following lines near the beginning:
78b077aed3SPierre Pronchery
79b077aed3SPierre Pronchery    config_diagnostics = 1
80b077aed3SPierre Pronchery    openssl_conf = openssl_init
81b077aed3SPierre Pronchery
82b077aed3SPierre Pronchery    .include /usr/local/ssl/fipsmodule.cnf
83b077aed3SPierre Pronchery
84b077aed3SPierre Pronchery    [openssl_init]
85b077aed3SPierre Pronchery    providers = provider_sect
86b077aed3SPierre Pronchery
87b077aed3SPierre Pronchery    [provider_sect]
88b077aed3SPierre Pronchery    fips = fips_sect
89b077aed3SPierre Pronchery    base = base_sect
90b077aed3SPierre Pronchery
91b077aed3SPierre Pronchery    [base_sect]
92b077aed3SPierre Pronchery    activate = 1
93b077aed3SPierre Pronchery
94b077aed3SPierre ProncheryObviously the include file location above should match the path and name of the
95b077aed3SPierre ProncheryFIPS module config file that you installed earlier.
96b077aed3SPierre ProncherySee L<https://github.com/openssl/openssl/blob/master/README-FIPS.md>.
97b077aed3SPierre Pronchery
98*aa795734SPierre ProncheryFor FIPS usage, it is recommended that the B<config_diagnostics> option is
99b077aed3SPierre Proncheryenabled to prevent accidental use of non-FIPS validated algorithms via broken
100b077aed3SPierre Proncheryor mistaken configuration.  See L<config(5)>.
101b077aed3SPierre Pronchery
102b077aed3SPierre ProncheryAny applications that use OpenSSL 3.0 and are started after these changes are
103b077aed3SPierre Proncherymade will start using only the FIPS module unless those applications take
104b077aed3SPierre Proncheryexplicit steps to avoid this default behaviour. Note that this configuration
105b077aed3SPierre Proncheryalso activates the "base" provider. The base provider does not include any
106b077aed3SPierre Proncherycryptographic algorithms (and therefore does not impact the validation status of
107b077aed3SPierre Proncheryany cryptographic operations), but does include other supporting algorithms that
108b077aed3SPierre Proncherymay be required. It is designed to be used in conjunction with the FIPS module.
109b077aed3SPierre Pronchery
110b077aed3SPierre ProncheryThis approach has the primary advantage that it is simple, and no code changes
111b077aed3SPierre Proncheryare required in applications in order to benefit from the FIPS module. There are
112b077aed3SPierre Proncherysome disadvantages to this approach:
113b077aed3SPierre Pronchery
114b077aed3SPierre Pronchery=over 4
115b077aed3SPierre Pronchery
116b077aed3SPierre Pronchery=item *
117b077aed3SPierre Pronchery
118b077aed3SPierre ProncheryYou may not want all applications to use the FIPS module.
119b077aed3SPierre Pronchery
120b077aed3SPierre ProncheryIt may be the case that some applications should and some should not use the
121b077aed3SPierre ProncheryFIPS module.
122b077aed3SPierre Pronchery
123b077aed3SPierre Pronchery=item *
124b077aed3SPierre Pronchery
125b077aed3SPierre ProncheryIf applications take explicit steps to not load the default config file or
126b077aed3SPierre Proncheryset different settings.
127b077aed3SPierre Pronchery
128b077aed3SPierre ProncheryThis method will not work for these cases.
129b077aed3SPierre Pronchery
130b077aed3SPierre Pronchery=item *
131b077aed3SPierre Pronchery
132b077aed3SPierre ProncheryThe algorithms available in the FIPS module are a subset of the algorithms
133b077aed3SPierre Proncherythat are available in the default OpenSSL Provider.
134b077aed3SPierre Pronchery
135b077aed3SPierre ProncheryIf any applications attempt to use any algorithms that are not present,
136b077aed3SPierre Proncherythen they will fail.
137b077aed3SPierre Pronchery
138b077aed3SPierre Pronchery=item *
139b077aed3SPierre Pronchery
140b077aed3SPierre ProncheryUsage of certain deprecated APIs avoids the use of the FIPS module.
141b077aed3SPierre Pronchery
142b077aed3SPierre ProncheryIf any applications use those APIs then the FIPS module will not be used.
143b077aed3SPierre Pronchery
144b077aed3SPierre Pronchery=back
145b077aed3SPierre Pronchery
146b077aed3SPierre Pronchery=head2 Selectively making applications use the FIPS module by default
147b077aed3SPierre Pronchery
148b077aed3SPierre ProncheryA variation on the above approach is to do the same thing on an individual
149b077aed3SPierre Proncheryapplication basis. The default OpenSSL config file depends on the compiled in
150b077aed3SPierre Proncheryvalue for B<OPENSSLDIR> as described in the section above. However it is also
151b077aed3SPierre Proncherypossible to override the config file to be used via the B<OPENSSL_CONF>
152b077aed3SPierre Proncheryenvironment variable. For example the following, on Unix, will cause the
153b077aed3SPierre Proncheryapplication to be executed with a non-standard config file location:
154b077aed3SPierre Pronchery
155b077aed3SPierre Pronchery    $ OPENSSL_CONF=/my/nondefault/openssl.cnf myapplication
156b077aed3SPierre Pronchery
157b077aed3SPierre ProncheryUsing this mechanism you can control which config file is loaded (and hence
158b077aed3SPierre Proncherywhether the FIPS module is loaded) on an application by application basis.
159b077aed3SPierre Pronchery
160b077aed3SPierre ProncheryThis removes the disadvantage listed above that you may not want all
161b077aed3SPierre Proncheryapplications to use the FIPS module. All the other advantages and disadvantages
162b077aed3SPierre Proncherystill apply.
163b077aed3SPierre Pronchery
164b077aed3SPierre Pronchery=head2 Programmatically loading the FIPS module (default library context)
165b077aed3SPierre Pronchery
166b077aed3SPierre ProncheryApplications may choose to load the FIPS provider explicitly rather than relying
167b077aed3SPierre Proncheryon config to do this. The config file is still necessary in order to hold the
168b077aed3SPierre ProncheryFIPS module config data (such as its self test status and integrity data). But
169b077aed3SPierre Proncheryin this case we do not automatically activate the FIPS provider via that config
170b077aed3SPierre Proncheryfile.
171b077aed3SPierre Pronchery
172b077aed3SPierre ProncheryTo do things this way configure as per
173b077aed3SPierre ProncheryL</Making all applications use the FIPS module by default> above, but edit the
174b077aed3SPierre ProncheryF<fipsmodule.cnf> file to remove or comment out the line which says
175b077aed3SPierre ProncheryC<activate = 1> (note that setting this value to 0 is I<not> sufficient).
176b077aed3SPierre ProncheryThis means all the required config information will be available to load the
177b077aed3SPierre ProncheryFIPS module, but it is not automatically loaded when the application starts. The
178b077aed3SPierre ProncheryFIPS provider can then be loaded programmatically like this:
179b077aed3SPierre Pronchery
180b077aed3SPierre Pronchery    #include <openssl/provider.h>
181b077aed3SPierre Pronchery
182b077aed3SPierre Pronchery    int main(void)
183b077aed3SPierre Pronchery    {
184b077aed3SPierre Pronchery        OSSL_PROVIDER *fips;
185b077aed3SPierre Pronchery        OSSL_PROVIDER *base;
186b077aed3SPierre Pronchery
187b077aed3SPierre Pronchery        fips = OSSL_PROVIDER_load(NULL, "fips");
188b077aed3SPierre Pronchery        if (fips == NULL) {
189b077aed3SPierre Pronchery            printf("Failed to load FIPS provider\n");
190b077aed3SPierre Pronchery            exit(EXIT_FAILURE);
191b077aed3SPierre Pronchery        }
192b077aed3SPierre Pronchery        base = OSSL_PROVIDER_load(NULL, "base");
193b077aed3SPierre Pronchery        if (base == NULL) {
194b077aed3SPierre Pronchery            OSSL_PROVIDER_unload(fips);
195b077aed3SPierre Pronchery            printf("Failed to load base provider\n");
196b077aed3SPierre Pronchery            exit(EXIT_FAILURE);
197b077aed3SPierre Pronchery        }
198b077aed3SPierre Pronchery
199b077aed3SPierre Pronchery        /* Rest of application */
200b077aed3SPierre Pronchery
201b077aed3SPierre Pronchery        OSSL_PROVIDER_unload(base);
202b077aed3SPierre Pronchery        OSSL_PROVIDER_unload(fips);
203b077aed3SPierre Pronchery        exit(EXIT_SUCCESS);
204b077aed3SPierre Pronchery    }
205b077aed3SPierre Pronchery
206b077aed3SPierre ProncheryNote that this should be one of the first things that you do in your
207b077aed3SPierre Proncheryapplication. If any OpenSSL functions get called that require the use of
208b077aed3SPierre Proncherycryptographic functions before this occurs then, if no provider has yet been
209b077aed3SPierre Proncheryloaded, then the default provider will be automatically loaded. If you then
210b077aed3SPierre Proncherylater explicitly load the FIPS provider then you will have both the FIPS and the
211b077aed3SPierre Proncherydefault provider loaded at the same time. It is undefined which implementation
212b077aed3SPierre Proncheryof an algorithm will be used if multiple implementations are available and you
213b077aed3SPierre Proncheryhave not explicitly specified via a property query (see below) which one should
214b077aed3SPierre Proncherybe used.
215b077aed3SPierre Pronchery
216b077aed3SPierre ProncheryAlso note that in this example we have additionally loaded the "base" provider.
217b077aed3SPierre ProncheryThis loads a sub-set of algorithms that are also available in the default
218b077aed3SPierre Proncheryprovider - specifically non cryptographic ones which may be used in conjunction
219b077aed3SPierre Proncherywith the FIPS provider. For example this contains algorithms for encoding and
220b077aed3SPierre Proncherydecoding keys. If you decide not to load the default provider then you
221b077aed3SPierre Proncherywill usually want to load the base provider instead.
222b077aed3SPierre Pronchery
223b077aed3SPierre ProncheryIn this example we are using the "default" library context. OpenSSL functions
224b077aed3SPierre Proncheryoperate within the scope of a library context. If no library context is
225b077aed3SPierre Proncheryexplicitly specified then the default library context is used. For further
226b077aed3SPierre Proncherydetails about library contexts see the L<OSSL_LIB_CTX(3)> man page.
227b077aed3SPierre Pronchery
228b077aed3SPierre Pronchery=head2 Loading the FIPS module at the same time as other providers
229b077aed3SPierre Pronchery
230b077aed3SPierre ProncheryIt is possible to have the FIPS provider and other providers (such as the
231b077aed3SPierre Proncherydefault provider) all loaded at the same time into the same library context. You
232b077aed3SPierre Proncherycan use a property query string during algorithm fetches to specify which
233b077aed3SPierre Proncheryimplementation you would like to use.
234b077aed3SPierre Pronchery
235b077aed3SPierre ProncheryFor example to fetch an implementation of SHA256 which conforms to FIPS
236b077aed3SPierre Proncherystandards you can specify the property query C<fips=yes> like this:
237b077aed3SPierre Pronchery
238b077aed3SPierre Pronchery    EVP_MD *sha256;
239b077aed3SPierre Pronchery
240b077aed3SPierre Pronchery    sha256 = EVP_MD_fetch(NULL, "SHA2-256", "fips=yes");
241b077aed3SPierre Pronchery
242b077aed3SPierre ProncheryIf no property query is specified, or more than one implementation matches the
243b077aed3SPierre Proncheryproperty query then it is undefined which implementation of a particular
244b077aed3SPierre Proncheryalgorithm will be returned.
245b077aed3SPierre Pronchery
246b077aed3SPierre ProncheryThis example shows an explicit request for an implementation of SHA256 from the
247b077aed3SPierre Proncherydefault provider:
248b077aed3SPierre Pronchery
249b077aed3SPierre Pronchery    EVP_MD *sha256;
250b077aed3SPierre Pronchery
251b077aed3SPierre Pronchery    sha256 = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
252b077aed3SPierre Pronchery
253b077aed3SPierre ProncheryIt is also possible to set a default property query string. The following
254b077aed3SPierre Proncheryexample sets the default property query of C<fips=yes> for all fetches within
255b077aed3SPierre Proncherythe default library context:
256b077aed3SPierre Pronchery
257b077aed3SPierre Pronchery    EVP_set_default_properties(NULL, "fips=yes");
258b077aed3SPierre Pronchery
259b077aed3SPierre ProncheryIf a fetch function has both an explicit property query specified, and a
260b077aed3SPierre Proncherydefault property query is defined then the two queries are merged together and
261b077aed3SPierre Proncheryboth apply. The local property query overrides the default properties if the
262b077aed3SPierre Proncherysame property name is specified in both.
263b077aed3SPierre Pronchery
264b077aed3SPierre ProncheryThere are two important built-in properties that you should be aware of:
265b077aed3SPierre Pronchery
266b077aed3SPierre ProncheryThe "provider" property enables you to specify which provider you want an
267b077aed3SPierre Proncheryimplementation to be fetched from, e.g. C<provider=default> or C<provider=fips>.
268b077aed3SPierre ProncheryAll algorithms implemented in a provider have this property set on them.
269b077aed3SPierre Pronchery
270b077aed3SPierre ProncheryThere is also the C<fips> property. All FIPS algorithms match against the
271b077aed3SPierre Proncheryproperty query C<fips=yes>. There are also some non-cryptographic algorithms
272b077aed3SPierre Proncheryavailable in the default and base providers that also have the C<fips=yes>
273b077aed3SPierre Proncheryproperty defined for them. These are the encoder and decoder algorithms that
274b077aed3SPierre Proncherycan (for example) be used to write out a key generated in the FIPS provider to a
275b077aed3SPierre Proncheryfile. The encoder and decoder algorithms are not in the FIPS module itself but
276b077aed3SPierre Proncheryare allowed to be used in conjunction with the FIPS algorithms.
277b077aed3SPierre Pronchery
278b077aed3SPierre ProncheryIt is possible to specify default properties within a config file. For example
279b077aed3SPierre Proncherythe following config file automatically loads the default and FIPS providers and
280b077aed3SPierre Proncherysets the default property value to be C<fips=yes>. Note that this config file
281b077aed3SPierre Proncherydoes not load the "base" provider. All supporting algorithms that are in "base"
282b077aed3SPierre Proncheryare also in "default", so it is unnecessary in this case:
283b077aed3SPierre Pronchery
284b077aed3SPierre Pronchery    config_diagnostics = 1
285b077aed3SPierre Pronchery    openssl_conf = openssl_init
286b077aed3SPierre Pronchery
287b077aed3SPierre Pronchery    .include /usr/local/ssl/fipsmodule.cnf
288b077aed3SPierre Pronchery
289b077aed3SPierre Pronchery    [openssl_init]
290b077aed3SPierre Pronchery    providers = provider_sect
291b077aed3SPierre Pronchery    alg_section = algorithm_sect
292b077aed3SPierre Pronchery
293b077aed3SPierre Pronchery    [provider_sect]
294b077aed3SPierre Pronchery    fips = fips_sect
295b077aed3SPierre Pronchery    default = default_sect
296b077aed3SPierre Pronchery
297b077aed3SPierre Pronchery    [default_sect]
298b077aed3SPierre Pronchery    activate = 1
299b077aed3SPierre Pronchery
300b077aed3SPierre Pronchery    [algorithm_sect]
301b077aed3SPierre Pronchery    default_properties = fips=yes
302b077aed3SPierre Pronchery
303b077aed3SPierre Pronchery=head2 Programmatically loading the FIPS module (nondefault library context)
304b077aed3SPierre Pronchery
305b077aed3SPierre ProncheryIn addition to using properties to separate usage of the FIPS module from other
306b077aed3SPierre Proncheryusages this can also be achieved using library contexts. In this example we
307b077aed3SPierre Proncherycreate two library contexts. In one we assume the existence of a config file
308b077aed3SPierre Proncherycalled F<openssl-fips.cnf> that automatically loads and configures the FIPS and
309b077aed3SPierre Proncherybase providers. The other library context will just use the default provider.
310b077aed3SPierre Pronchery
311b077aed3SPierre Pronchery    OSSL_LIB_CTX *fips_libctx, *nonfips_libctx;
312b077aed3SPierre Pronchery    OSSL_PROVIDER *defctxnull = NULL;
313b077aed3SPierre Pronchery    EVP_MD *fipssha256 = NULL, *nonfipssha256 = NULL;
314b077aed3SPierre Pronchery    int ret = 1;
315b077aed3SPierre Pronchery
316b077aed3SPierre Pronchery    /*
317b077aed3SPierre Pronchery     * Create two nondefault library contexts. One for fips usage and
318b077aed3SPierre Pronchery     * one for non-fips usage
319b077aed3SPierre Pronchery     */
320b077aed3SPierre Pronchery    fips_libctx = OSSL_LIB_CTX_new();
321b077aed3SPierre Pronchery    nonfips_libctx = OSSL_LIB_CTX_new();
322b077aed3SPierre Pronchery    if (fips_libctx == NULL || nonfips_libctx == NULL)
323b077aed3SPierre Pronchery        goto err;
324b077aed3SPierre Pronchery
325b077aed3SPierre Pronchery    /* Prevent anything from using the default library context */
326b077aed3SPierre Pronchery    defctxnull = OSSL_PROVIDER_load(NULL, "null");
327b077aed3SPierre Pronchery
328b077aed3SPierre Pronchery    /*
329b077aed3SPierre Pronchery     * Load config file for the FIPS library context. We assume that
330b077aed3SPierre Pronchery     * this config file will automatically activate the FIPS and base
331b077aed3SPierre Pronchery     * providers so we don't need to explicitly load them here.
332b077aed3SPierre Pronchery     */
333b077aed3SPierre Pronchery    if (!OSSL_LIB_CTX_load_config(fips_libctx, "openssl-fips.cnf"))
334b077aed3SPierre Pronchery        goto err;
335b077aed3SPierre Pronchery
336b077aed3SPierre Pronchery    /*
337b077aed3SPierre Pronchery     * We don't need to do anything special to load the default
338b077aed3SPierre Pronchery     * provider into nonfips_libctx. This happens automatically if no
339b077aed3SPierre Pronchery     * other providers are loaded.
340b077aed3SPierre Pronchery     * Because we don't call OSSL_LIB_CTX_load_config() explicitly for
341b077aed3SPierre Pronchery     * nonfips_libctx it will just use the default config file.
342b077aed3SPierre Pronchery     */
343b077aed3SPierre Pronchery
344b077aed3SPierre Pronchery    /* As an example get some digests */
345b077aed3SPierre Pronchery
346b077aed3SPierre Pronchery    /* Get a FIPS validated digest */
347b077aed3SPierre Pronchery    fipssha256 = EVP_MD_fetch(fips_libctx, "SHA2-256", NULL);
348b077aed3SPierre Pronchery    if (fipssha256 == NULL)
349b077aed3SPierre Pronchery        goto err;
350b077aed3SPierre Pronchery
351b077aed3SPierre Pronchery    /* Get a non-FIPS validated digest */
352b077aed3SPierre Pronchery    nonfipssha256 = EVP_MD_fetch(nonfips_libctx, "SHA2-256", NULL);
353b077aed3SPierre Pronchery    if (nonfipssha256 == NULL)
354b077aed3SPierre Pronchery        goto err;
355b077aed3SPierre Pronchery
356b077aed3SPierre Pronchery    /* Use the digests */
357b077aed3SPierre Pronchery
358b077aed3SPierre Pronchery    printf("Success\n");
359b077aed3SPierre Pronchery    ret = 0;
360b077aed3SPierre Pronchery
361b077aed3SPierre Pronchery    err:
362b077aed3SPierre Pronchery    EVP_MD_free(fipssha256);
363b077aed3SPierre Pronchery    EVP_MD_free(nonfipssha256);
364b077aed3SPierre Pronchery    OSSL_LIB_CTX_free(fips_libctx);
365b077aed3SPierre Pronchery    OSSL_LIB_CTX_free(nonfips_libctx);
366b077aed3SPierre Pronchery    OSSL_PROVIDER_unload(defctxnull);
367b077aed3SPierre Pronchery
368b077aed3SPierre Pronchery    return ret;
369b077aed3SPierre Pronchery
370b077aed3SPierre ProncheryNote that we have made use of the special "null" provider here which we load
371b077aed3SPierre Proncheryinto the default library context. We could have chosen to use the default
372b077aed3SPierre Proncherylibrary context for FIPS usage, and just create one additional library context
373b077aed3SPierre Proncheryfor other usages - or vice versa. However if code has not been converted to use
374b077aed3SPierre Proncherylibrary contexts then the default library context will be automatically used.
375b077aed3SPierre ProncheryThis could be the case for your own existing applications as well as certain
376b077aed3SPierre Proncheryparts of OpenSSL itself. Not all parts of OpenSSL are library context aware. If
377b077aed3SPierre Proncherythis happens then you could "accidentally" use the wrong library context for a
378b077aed3SPierre Proncheryparticular operation. To be sure this doesn't happen you can load the "null"
379b077aed3SPierre Proncheryprovider into the default library context. Because a provider has been
380b077aed3SPierre Proncheryexplicitly loaded, the default provider will not automatically load. This means
381b077aed3SPierre Proncherycode using the default context by accident will fail because no algorithms will
382b077aed3SPierre Proncherybe available.
383b077aed3SPierre Pronchery
384b077aed3SPierre ProncherySee L<migration_guide(7)/Library Context> for additional information about the
385b077aed3SPierre ProncheryLibrary Context.
386b077aed3SPierre Pronchery
387b077aed3SPierre Pronchery=head2 Using Encoders and Decoders with the FIPS module
388b077aed3SPierre Pronchery
389b077aed3SPierre ProncheryEncoders and decoders are used to read and write keys or parameters from or to
390b077aed3SPierre Proncherysome external format (for example a PEM file). If your application generates
391b077aed3SPierre Proncherykeys or parameters that then need to be written into PEM or DER format
392b077aed3SPierre Proncherythen it is likely that you will need to use an encoder to do this. Similarly
393b077aed3SPierre Proncheryyou need a decoder to read previously saved keys and parameters. In most cases
394b077aed3SPierre Proncherythis will be invisible to you if you are using APIs that existed in
395b077aed3SPierre ProncheryOpenSSL 1.1.1 or earlier such as L<i2d_PrivateKey(3)>. However the appropriate
396b077aed3SPierre Proncheryencoder/decoder will need to be available in the library context associated with
397b077aed3SPierre Proncherythe key or parameter object. The built-in OpenSSL encoders and decoders are
398b077aed3SPierre Proncheryimplemented in both the default and base providers and are not in the FIPS
399b077aed3SPierre Proncherymodule boundary. However since they are not cryptographic algorithms themselves
400b077aed3SPierre Proncheryit is still possible to use them in conjunction with the FIPS module, and
401b077aed3SPierre Proncherytherefore these encoders/decoders have the C<fips=yes> property against them.
402b077aed3SPierre ProncheryYou should ensure that either the default or base provider is loaded into the
403b077aed3SPierre Proncherylibrary context in this case.
404b077aed3SPierre Pronchery
405b077aed3SPierre Pronchery=head2 Using the FIPS module in SSL/TLS
406b077aed3SPierre Pronchery
407b077aed3SPierre ProncheryWriting an application that uses libssl in conjunction with the FIPS module is
408b077aed3SPierre Proncherymuch the same as writing a normal libssl application. If you are using global
409b077aed3SPierre Proncheryproperties and the default library context to specify usage of FIPS validated
410b077aed3SPierre Proncheryalgorithms then this will happen automatically for all cryptographic algorithms
411b077aed3SPierre Proncheryin libssl. If you are using a nondefault library context to load the FIPS
412b077aed3SPierre Proncheryprovider then you can supply this to libssl using the function
413b077aed3SPierre ProncheryL<SSL_CTX_new_ex(3)>. This works as a drop in replacement for the function
414b077aed3SPierre ProncheryL<SSL_CTX_new(3)> except it provides you with the capability to specify the
415b077aed3SPierre Proncherylibrary context to be used. You can also use the same function to specify
416b077aed3SPierre Proncherylibssl specific properties to use.
417b077aed3SPierre Pronchery
418b077aed3SPierre ProncheryIn this first example we create two SSL_CTX objects using two different library
419b077aed3SPierre Proncherycontexts.
420b077aed3SPierre Pronchery
421b077aed3SPierre Pronchery    /*
422b077aed3SPierre Pronchery     * We assume that a nondefault library context with the FIPS
423b077aed3SPierre Pronchery     * provider loaded has been created called fips_libctx.
424b077aed3SPierre Pronchery     */
425b077aed3SPierre Pronchery    SSL_CTX *fips_ssl_ctx = SSL_CTX_new_ex(fips_libctx, NULL, TLS_method());
426b077aed3SPierre Pronchery    /*
427b077aed3SPierre Pronchery     * We assume that a nondefault library context with the default
428b077aed3SPierre Pronchery     * provider loaded has been created called non_fips_libctx.
429b077aed3SPierre Pronchery     */
430b077aed3SPierre Pronchery    SSL_CTX *non_fips_ssl_ctx = SSL_CTX_new_ex(non_fips_libctx, NULL,
431b077aed3SPierre Pronchery                                               TLS_method());
432b077aed3SPierre Pronchery
433b077aed3SPierre ProncheryIn this second example we create two SSL_CTX objects using different properties
434b077aed3SPierre Proncheryto specify FIPS usage:
435b077aed3SPierre Pronchery
436b077aed3SPierre Pronchery    /*
437b077aed3SPierre Pronchery     * The "fips=yes" property includes all FIPS approved algorithms
438b077aed3SPierre Pronchery     * as well as encoders from the default provider that are allowed
439b077aed3SPierre Pronchery     * to be used. The NULL below indicates that we are using the
440b077aed3SPierre Pronchery     * default library context.
441b077aed3SPierre Pronchery     */
442b077aed3SPierre Pronchery    SSL_CTX *fips_ssl_ctx = SSL_CTX_new_ex(NULL, "fips=yes", TLS_method());
443b077aed3SPierre Pronchery    /*
444b077aed3SPierre Pronchery     * The "provider!=fips" property allows algorithms from any
445b077aed3SPierre Pronchery     * provider except the FIPS provider
446b077aed3SPierre Pronchery     */
447b077aed3SPierre Pronchery    SSL_CTX *non_fips_ssl_ctx = SSL_CTX_new_ex(NULL, "provider!=fips",
448b077aed3SPierre Pronchery                                               TLS_method());
449b077aed3SPierre Pronchery
450b077aed3SPierre Pronchery=head2 Confirming that an algorithm is being provided by the FIPS module
451b077aed3SPierre Pronchery
452b077aed3SPierre ProncheryA chain of links needs to be followed to go from an algorithm instance to the
453b077aed3SPierre Proncheryprovider that implements it. The process is similar for all algorithms. Here the
454b077aed3SPierre Proncheryexample of a digest is used.
455b077aed3SPierre Pronchery
456b077aed3SPierre ProncheryTo go from an B<EVP_MD_CTX> to an B<EVP_MD>, use L<EVP_MD_CTX_md(3)> .
457b077aed3SPierre ProncheryTo go from the B<EVP_MD> to its B<OSSL_PROVIDER>,
458b077aed3SPierre Proncheryuse L<EVP_MD_get0_provider(3)>.
459b077aed3SPierre ProncheryTo extract the name from the B<OSSL_PROVIDER>, use
460b077aed3SPierre ProncheryL<OSSL_PROVIDER_get0_name(3)>.
461b077aed3SPierre Pronchery
462*aa795734SPierre Pronchery=head1 NOTES
463*aa795734SPierre Pronchery
464*aa795734SPierre ProncherySome released versions of OpenSSL do not include a validated
465*aa795734SPierre ProncheryFIPS provider.  To determine which versions have undergone
466*aa795734SPierre Proncherythe validation process, please refer to the
467*aa795734SPierre ProncheryL<OpenSSL Downloads page|https://www.openssl.org/source/>.  If you
468*aa795734SPierre Proncheryrequire FIPS-approved functionality, it is essential to build your FIPS
469*aa795734SPierre Proncheryprovider using one of the validated versions listed there.  Normally,
470*aa795734SPierre Proncheryit is possible to utilize a FIPS provider constructed from one of the
471*aa795734SPierre Proncheryvalidated versions alongside F<libcrypto> and F<libssl> compiled from any
472*aa795734SPierre Proncheryrelease within the same major release series.  This flexibility enables
473*aa795734SPierre Proncheryyou to address bug fixes and CVEs that fall outside the FIPS boundary.
474*aa795734SPierre Pronchery
475b077aed3SPierre Pronchery=head1 SEE ALSO
476b077aed3SPierre Pronchery
477*aa795734SPierre ProncheryL<migration_guide(7)>, L<crypto(7)>, L<fips_config(5)>,
478*aa795734SPierre ProncheryL<https://www.openssl.org/source/>
479b077aed3SPierre Pronchery
480b077aed3SPierre Pronchery=head1 HISTORY
481b077aed3SPierre Pronchery
482b077aed3SPierre ProncheryThe FIPS module guide was created for use with the new FIPS provider
483b077aed3SPierre Proncheryin OpenSSL 3.0.
484b077aed3SPierre Pronchery
485b077aed3SPierre Pronchery=head1 COPYRIGHT
486b077aed3SPierre Pronchery
487*aa795734SPierre ProncheryCopyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
488b077aed3SPierre Pronchery
489b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
490b077aed3SPierre Proncherythis file except in compliance with the License.  You can obtain a copy
491b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at
492b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
493b077aed3SPierre Pronchery
494b077aed3SPierre Pronchery=cut
495