1e1051a39Sopenharmony_ci=pod
2e1051a39Sopenharmony_ci
3e1051a39Sopenharmony_ci=head1 NAME
4e1051a39Sopenharmony_ci
5e1051a39Sopenharmony_cifips_module - OpenSSL fips module guide
6e1051a39Sopenharmony_ci
7e1051a39Sopenharmony_ci=head1 SYNOPSIS
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciSee the individual manual pages for details.
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci=head1 DESCRIPTION
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ciThis guide details different ways that OpenSSL can be used in conjunction
14e1051a39Sopenharmony_ciwith the FIPS module. Which is the correct approach to use will depend on your
15e1051a39Sopenharmony_ciown specific circumstances and what you are attempting to achieve.
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ciNote that the old functions FIPS_mode() and FIPS_mode_set() are no longer
18e1051a39Sopenharmony_cipresent so you must remove them from your application if you use them.
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ciApplications written to use the OpenSSL 3.0 FIPS module should not use any
21e1051a39Sopenharmony_cilegacy APIs or features that avoid the FIPS module. Specifically this includes:
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci=over 4
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ci=item *
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciLow level cryptographic APIs (use the high level APIs, such as EVP, instead)
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci=item *
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ciEngines
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci=item *
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ciAny functions that create or modify custom "METHODS" (for example
36e1051a39Sopenharmony_ciEVP_MD_meth_new(), EVP_CIPHER_meth_new(), EVP_PKEY_meth_new(), RSA_meth_new(),
37e1051a39Sopenharmony_ciEC_KEY_METHOD_new(), etc.)
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci=back
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ciAll of the above APIs are deprecated in OpenSSL 3.0 - so a simple rule is to
42e1051a39Sopenharmony_ciavoid using all deprecated functions. See L<migration_guide(7)> for a list of
43e1051a39Sopenharmony_cideprecated functions.
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci=head2 Making all applications use the FIPS module by default
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ciOne simple approach is to cause all applications that are using OpenSSL to only
48e1051a39Sopenharmony_ciuse the FIPS module for cryptographic algorithms by default.
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ciThis approach can be done purely via configuration. As long as applications are
51e1051a39Sopenharmony_cibuilt and linked against OpenSSL 3.0 and do not override the loading of the
52e1051a39Sopenharmony_cidefault config file or its settings then they can automatically start using the
53e1051a39Sopenharmony_ciFIPS module without the need for any further code changes.
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ciTo do this the default OpenSSL config file will have to be modified. The
56e1051a39Sopenharmony_cilocation of this config file will depend on the platform, and any options that
57e1051a39Sopenharmony_ciwere given during the build process. You can check the location of the config
58e1051a39Sopenharmony_cifile by running this command:
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    $ openssl version -d
61e1051a39Sopenharmony_ci    OPENSSLDIR: "/usr/local/ssl"
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ciCaution: Many Operating Systems install OpenSSL by default. It is a common error
64e1051a39Sopenharmony_cito not have the correct version of OpenSSL in your $PATH. Check that you are
65e1051a39Sopenharmony_cirunning an OpenSSL 3.0 version like this:
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci    $ openssl version -v
68e1051a39Sopenharmony_ci    OpenSSL 3.0.0-dev xx XXX xxxx (Library: OpenSSL 3.0.0-dev xx XXX xxxx)
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ciThe B<OPENSSLDIR> value above gives the directory name for where the default
71e1051a39Sopenharmony_ciconfig file is stored. So in this case the default config file will be called
72e1051a39Sopenharmony_ciF</usr/local/ssl/openssl.cnf>.
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ciEdit the config file to add the following lines near the beginning:
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci    config_diagnostics = 1
77e1051a39Sopenharmony_ci    openssl_conf = openssl_init
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    .include /usr/local/ssl/fipsmodule.cnf
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci    [openssl_init]
82e1051a39Sopenharmony_ci    providers = provider_sect
83e1051a39Sopenharmony_ci
84e1051a39Sopenharmony_ci    [provider_sect]
85e1051a39Sopenharmony_ci    fips = fips_sect
86e1051a39Sopenharmony_ci    base = base_sect
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci    [base_sect]
89e1051a39Sopenharmony_ci    activate = 1
90e1051a39Sopenharmony_ci
91e1051a39Sopenharmony_ciObviously the include file location above should match the path and name of the
92e1051a39Sopenharmony_ciFIPS module config file that you installed earlier.
93e1051a39Sopenharmony_ciSee L<https://github.com/openssl/openssl/blob/master/README-FIPS.md>.
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ciFor FIPS usage, it is recommened that the B<config_diagnostics> option is
96e1051a39Sopenharmony_cienabled to prevent accidental use of non-FIPS validated algorithms via broken
97e1051a39Sopenharmony_cior mistaken configuration.  See L<config(5)>.
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ciAny applications that use OpenSSL 3.0 and are started after these changes are
100e1051a39Sopenharmony_cimade will start using only the FIPS module unless those applications take
101e1051a39Sopenharmony_ciexplicit steps to avoid this default behaviour. Note that this configuration
102e1051a39Sopenharmony_cialso activates the "base" provider. The base provider does not include any
103e1051a39Sopenharmony_cicryptographic algorithms (and therefore does not impact the validation status of
104e1051a39Sopenharmony_ciany cryptographic operations), but does include other supporting algorithms that
105e1051a39Sopenharmony_cimay be required. It is designed to be used in conjunction with the FIPS module.
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ciThis approach has the primary advantage that it is simple, and no code changes
108e1051a39Sopenharmony_ciare required in applications in order to benefit from the FIPS module. There are
109e1051a39Sopenharmony_cisome disadvantages to this approach:
110e1051a39Sopenharmony_ci
111e1051a39Sopenharmony_ci=over 4
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ci=item *
114e1051a39Sopenharmony_ci
115e1051a39Sopenharmony_ciYou may not want all applications to use the FIPS module.
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_ciIt may be the case that some applications should and some should not use the
118e1051a39Sopenharmony_ciFIPS module.
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci=item *
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_ciIf applications take explicit steps to not load the default config file or
123e1051a39Sopenharmony_ciset different settings.
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_ciThis method will not work for these cases.
126e1051a39Sopenharmony_ci
127e1051a39Sopenharmony_ci=item *
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_ciThe algorithms available in the FIPS module are a subset of the algorithms
130e1051a39Sopenharmony_cithat are available in the default OpenSSL Provider.
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ciIf any applications attempt to use any algorithms that are not present,
133e1051a39Sopenharmony_cithen they will fail.
134e1051a39Sopenharmony_ci
135e1051a39Sopenharmony_ci=item *
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ciUsage of certain deprecated APIs avoids the use of the FIPS module.
138e1051a39Sopenharmony_ci
139e1051a39Sopenharmony_ciIf any applications use those APIs then the FIPS module will not be used.
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_ci=back
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci=head2 Selectively making applications use the FIPS module by default
144e1051a39Sopenharmony_ci
145e1051a39Sopenharmony_ciA variation on the above approach is to do the same thing on an individual
146e1051a39Sopenharmony_ciapplication basis. The default OpenSSL config file depends on the compiled in
147e1051a39Sopenharmony_civalue for B<OPENSSLDIR> as described in the section above. However it is also
148e1051a39Sopenharmony_cipossible to override the config file to be used via the B<OPENSSL_CONF>
149e1051a39Sopenharmony_cienvironment variable. For example the following, on Unix, will cause the
150e1051a39Sopenharmony_ciapplication to be executed with a non-standard config file location:
151e1051a39Sopenharmony_ci
152e1051a39Sopenharmony_ci    $ OPENSSL_CONF=/my/nondefault/openssl.cnf myapplication
153e1051a39Sopenharmony_ci
154e1051a39Sopenharmony_ciUsing this mechanism you can control which config file is loaded (and hence
155e1051a39Sopenharmony_ciwhether the FIPS module is loaded) on an application by application basis.
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ciThis removes the disadvantage listed above that you may not want all
158e1051a39Sopenharmony_ciapplications to use the FIPS module. All the other advantages and disadvantages
159e1051a39Sopenharmony_cistill apply.
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci=head2 Programmatically loading the FIPS module (default library context)
162e1051a39Sopenharmony_ci
163e1051a39Sopenharmony_ciApplications may choose to load the FIPS provider explicitly rather than relying
164e1051a39Sopenharmony_cion config to do this. The config file is still necessary in order to hold the
165e1051a39Sopenharmony_ciFIPS module config data (such as its self test status and integrity data). But
166e1051a39Sopenharmony_ciin this case we do not automatically activate the FIPS provider via that config
167e1051a39Sopenharmony_cifile.
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_ciTo do things this way configure as per
170e1051a39Sopenharmony_ciL</Making all applications use the FIPS module by default> above, but edit the
171e1051a39Sopenharmony_ciF<fipsmodule.cnf> file to remove or comment out the line which says
172e1051a39Sopenharmony_ciC<activate = 1> (note that setting this value to 0 is I<not> sufficient).
173e1051a39Sopenharmony_ciThis means all the required config information will be available to load the
174e1051a39Sopenharmony_ciFIPS module, but it is not automatically loaded when the application starts. The
175e1051a39Sopenharmony_ciFIPS provider can then be loaded programmatically like this:
176e1051a39Sopenharmony_ci
177e1051a39Sopenharmony_ci    #include <openssl/provider.h>
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ci    int main(void)
180e1051a39Sopenharmony_ci    {
181e1051a39Sopenharmony_ci        OSSL_PROVIDER *fips;
182e1051a39Sopenharmony_ci        OSSL_PROVIDER *base;
183e1051a39Sopenharmony_ci
184e1051a39Sopenharmony_ci        fips = OSSL_PROVIDER_load(NULL, "fips");
185e1051a39Sopenharmony_ci        if (fips == NULL) {
186e1051a39Sopenharmony_ci            printf("Failed to load FIPS provider\n");
187e1051a39Sopenharmony_ci            exit(EXIT_FAILURE);
188e1051a39Sopenharmony_ci        }
189e1051a39Sopenharmony_ci        base = OSSL_PROVIDER_load(NULL, "base");
190e1051a39Sopenharmony_ci        if (base == NULL) {
191e1051a39Sopenharmony_ci            OSSL_PROVIDER_unload(fips);
192e1051a39Sopenharmony_ci            printf("Failed to load base provider\n");
193e1051a39Sopenharmony_ci            exit(EXIT_FAILURE);
194e1051a39Sopenharmony_ci        }
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ci        /* Rest of application */
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci        OSSL_PROVIDER_unload(base);
199e1051a39Sopenharmony_ci        OSSL_PROVIDER_unload(fips);
200e1051a39Sopenharmony_ci        exit(EXIT_SUCCESS);
201e1051a39Sopenharmony_ci    }
202e1051a39Sopenharmony_ci
203e1051a39Sopenharmony_ciNote that this should be one of the first things that you do in your
204e1051a39Sopenharmony_ciapplication. If any OpenSSL functions get called that require the use of
205e1051a39Sopenharmony_cicryptographic functions before this occurs then, if no provider has yet been
206e1051a39Sopenharmony_ciloaded, then the default provider will be automatically loaded. If you then
207e1051a39Sopenharmony_cilater explicitly load the FIPS provider then you will have both the FIPS and the
208e1051a39Sopenharmony_cidefault provider loaded at the same time. It is undefined which implementation
209e1051a39Sopenharmony_ciof an algorithm will be used if multiple implementations are available and you
210e1051a39Sopenharmony_cihave not explicitly specified via a property query (see below) which one should
211e1051a39Sopenharmony_cibe used.
212e1051a39Sopenharmony_ci
213e1051a39Sopenharmony_ciAlso note that in this example we have additionally loaded the "base" provider.
214e1051a39Sopenharmony_ciThis loads a sub-set of algorithms that are also available in the default
215e1051a39Sopenharmony_ciprovider - specifically non cryptographic ones which may be used in conjunction
216e1051a39Sopenharmony_ciwith the FIPS provider. For example this contains algorithms for encoding and
217e1051a39Sopenharmony_cidecoding keys. If you decide not to load the default provider then you
218e1051a39Sopenharmony_ciwill usually want to load the base provider instead.
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_ciIn this example we are using the "default" library context. OpenSSL functions
221e1051a39Sopenharmony_cioperate within the scope of a library context. If no library context is
222e1051a39Sopenharmony_ciexplicitly specified then the default library context is used. For further
223e1051a39Sopenharmony_cidetails about library contexts see the L<OSSL_LIB_CTX(3)> man page.
224e1051a39Sopenharmony_ci
225e1051a39Sopenharmony_ci=head2 Loading the FIPS module at the same time as other providers
226e1051a39Sopenharmony_ci
227e1051a39Sopenharmony_ciIt is possible to have the FIPS provider and other providers (such as the
228e1051a39Sopenharmony_cidefault provider) all loaded at the same time into the same library context. You
229e1051a39Sopenharmony_cican use a property query string during algorithm fetches to specify which
230e1051a39Sopenharmony_ciimplementation you would like to use.
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ciFor example to fetch an implementation of SHA256 which conforms to FIPS
233e1051a39Sopenharmony_cistandards you can specify the property query C<fips=yes> like this:
234e1051a39Sopenharmony_ci
235e1051a39Sopenharmony_ci    EVP_MD *sha256;
236e1051a39Sopenharmony_ci
237e1051a39Sopenharmony_ci    sha256 = EVP_MD_fetch(NULL, "SHA2-256", "fips=yes");
238e1051a39Sopenharmony_ci
239e1051a39Sopenharmony_ciIf no property query is specified, or more than one implementation matches the
240e1051a39Sopenharmony_ciproperty query then it is undefined which implementation of a particular
241e1051a39Sopenharmony_cialgorithm will be returned.
242e1051a39Sopenharmony_ci
243e1051a39Sopenharmony_ciThis example shows an explicit request for an implementation of SHA256 from the
244e1051a39Sopenharmony_cidefault provider:
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ci    EVP_MD *sha256;
247e1051a39Sopenharmony_ci
248e1051a39Sopenharmony_ci    sha256 = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
249e1051a39Sopenharmony_ci
250e1051a39Sopenharmony_ciIt is also possible to set a default property query string. The following
251e1051a39Sopenharmony_ciexample sets the default property query of C<fips=yes> for all fetches within
252e1051a39Sopenharmony_cithe default library context:
253e1051a39Sopenharmony_ci
254e1051a39Sopenharmony_ci    EVP_set_default_properties(NULL, "fips=yes");
255e1051a39Sopenharmony_ci
256e1051a39Sopenharmony_ciIf a fetch function has both an explicit property query specified, and a
257e1051a39Sopenharmony_cidefault property query is defined then the two queries are merged together and
258e1051a39Sopenharmony_ciboth apply. The local property query overrides the default properties if the
259e1051a39Sopenharmony_cisame property name is specified in both.
260e1051a39Sopenharmony_ci
261e1051a39Sopenharmony_ciThere are two important built-in properties that you should be aware of:
262e1051a39Sopenharmony_ci
263e1051a39Sopenharmony_ciThe "provider" property enables you to specify which provider you want an
264e1051a39Sopenharmony_ciimplementation to be fetched from, e.g. C<provider=default> or C<provider=fips>.
265e1051a39Sopenharmony_ciAll algorithms implemented in a provider have this property set on them.
266e1051a39Sopenharmony_ci
267e1051a39Sopenharmony_ciThere is also the C<fips> property. All FIPS algorithms match against the
268e1051a39Sopenharmony_ciproperty query C<fips=yes>. There are also some non-cryptographic algorithms
269e1051a39Sopenharmony_ciavailable in the default and base providers that also have the C<fips=yes>
270e1051a39Sopenharmony_ciproperty defined for them. These are the encoder and decoder algorithms that
271e1051a39Sopenharmony_cican (for example) be used to write out a key generated in the FIPS provider to a
272e1051a39Sopenharmony_cifile. The encoder and decoder algorithms are not in the FIPS module itself but
273e1051a39Sopenharmony_ciare allowed to be used in conjunction with the FIPS algorithms.
274e1051a39Sopenharmony_ci
275e1051a39Sopenharmony_ciIt is possible to specify default properties within a config file. For example
276e1051a39Sopenharmony_cithe following config file automatically loads the default and FIPS providers and
277e1051a39Sopenharmony_cisets the default property value to be C<fips=yes>. Note that this config file
278e1051a39Sopenharmony_cidoes not load the "base" provider. All supporting algorithms that are in "base"
279e1051a39Sopenharmony_ciare also in "default", so it is unnecessary in this case:
280e1051a39Sopenharmony_ci
281e1051a39Sopenharmony_ci    config_diagnostics = 1
282e1051a39Sopenharmony_ci    openssl_conf = openssl_init
283e1051a39Sopenharmony_ci
284e1051a39Sopenharmony_ci    .include /usr/local/ssl/fipsmodule.cnf
285e1051a39Sopenharmony_ci
286e1051a39Sopenharmony_ci    [openssl_init]
287e1051a39Sopenharmony_ci    providers = provider_sect
288e1051a39Sopenharmony_ci    alg_section = algorithm_sect
289e1051a39Sopenharmony_ci
290e1051a39Sopenharmony_ci    [provider_sect]
291e1051a39Sopenharmony_ci    fips = fips_sect
292e1051a39Sopenharmony_ci    default = default_sect
293e1051a39Sopenharmony_ci
294e1051a39Sopenharmony_ci    [default_sect]
295e1051a39Sopenharmony_ci    activate = 1
296e1051a39Sopenharmony_ci
297e1051a39Sopenharmony_ci    [algorithm_sect]
298e1051a39Sopenharmony_ci    default_properties = fips=yes
299e1051a39Sopenharmony_ci
300e1051a39Sopenharmony_ci=head2 Programmatically loading the FIPS module (nondefault library context)
301e1051a39Sopenharmony_ci
302e1051a39Sopenharmony_ciIn addition to using properties to separate usage of the FIPS module from other
303e1051a39Sopenharmony_ciusages this can also be achieved using library contexts. In this example we
304e1051a39Sopenharmony_cicreate two library contexts. In one we assume the existence of a config file
305e1051a39Sopenharmony_cicalled F<openssl-fips.cnf> that automatically loads and configures the FIPS and
306e1051a39Sopenharmony_cibase providers. The other library context will just use the default provider.
307e1051a39Sopenharmony_ci
308e1051a39Sopenharmony_ci    OSSL_LIB_CTX *fips_libctx, *nonfips_libctx;
309e1051a39Sopenharmony_ci    OSSL_PROVIDER *defctxnull = NULL;
310e1051a39Sopenharmony_ci    EVP_MD *fipssha256 = NULL, *nonfipssha256 = NULL;
311e1051a39Sopenharmony_ci    int ret = 1;
312e1051a39Sopenharmony_ci
313e1051a39Sopenharmony_ci    /*
314e1051a39Sopenharmony_ci     * Create two nondefault library contexts. One for fips usage and
315e1051a39Sopenharmony_ci     * one for non-fips usage
316e1051a39Sopenharmony_ci     */
317e1051a39Sopenharmony_ci    fips_libctx = OSSL_LIB_CTX_new();
318e1051a39Sopenharmony_ci    nonfips_libctx = OSSL_LIB_CTX_new();
319e1051a39Sopenharmony_ci    if (fips_libctx == NULL || nonfips_libctx == NULL)
320e1051a39Sopenharmony_ci        goto err;
321e1051a39Sopenharmony_ci
322e1051a39Sopenharmony_ci    /* Prevent anything from using the default library context */
323e1051a39Sopenharmony_ci    defctxnull = OSSL_PROVIDER_load(NULL, "null");
324e1051a39Sopenharmony_ci
325e1051a39Sopenharmony_ci    /*
326e1051a39Sopenharmony_ci     * Load config file for the FIPS library context. We assume that
327e1051a39Sopenharmony_ci     * this config file will automatically activate the FIPS and base
328e1051a39Sopenharmony_ci     * providers so we don't need to explicitly load them here.
329e1051a39Sopenharmony_ci     */
330e1051a39Sopenharmony_ci    if (!OSSL_LIB_CTX_load_config(fips_libctx, "openssl-fips.cnf"))
331e1051a39Sopenharmony_ci        goto err;
332e1051a39Sopenharmony_ci
333e1051a39Sopenharmony_ci    /*
334e1051a39Sopenharmony_ci     * We don't need to do anything special to load the default
335e1051a39Sopenharmony_ci     * provider into nonfips_libctx. This happens automatically if no
336e1051a39Sopenharmony_ci     * other providers are loaded.
337e1051a39Sopenharmony_ci     * Because we don't call OSSL_LIB_CTX_load_config() explicitly for
338e1051a39Sopenharmony_ci     * nonfips_libctx it will just use the default config file.
339e1051a39Sopenharmony_ci     */
340e1051a39Sopenharmony_ci
341e1051a39Sopenharmony_ci    /* As an example get some digests */
342e1051a39Sopenharmony_ci
343e1051a39Sopenharmony_ci    /* Get a FIPS validated digest */
344e1051a39Sopenharmony_ci    fipssha256 = EVP_MD_fetch(fips_libctx, "SHA2-256", NULL);
345e1051a39Sopenharmony_ci    if (fipssha256 == NULL)
346e1051a39Sopenharmony_ci        goto err;
347e1051a39Sopenharmony_ci
348e1051a39Sopenharmony_ci    /* Get a non-FIPS validated digest */
349e1051a39Sopenharmony_ci    nonfipssha256 = EVP_MD_fetch(nonfips_libctx, "SHA2-256", NULL);
350e1051a39Sopenharmony_ci    if (nonfipssha256 == NULL)
351e1051a39Sopenharmony_ci        goto err;
352e1051a39Sopenharmony_ci
353e1051a39Sopenharmony_ci    /* Use the digests */
354e1051a39Sopenharmony_ci
355e1051a39Sopenharmony_ci    printf("Success\n");
356e1051a39Sopenharmony_ci    ret = 0;
357e1051a39Sopenharmony_ci
358e1051a39Sopenharmony_ci    err:
359e1051a39Sopenharmony_ci    EVP_MD_free(fipssha256);
360e1051a39Sopenharmony_ci    EVP_MD_free(nonfipssha256);
361e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(fips_libctx);
362e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(nonfips_libctx);
363e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(defctxnull);
364e1051a39Sopenharmony_ci
365e1051a39Sopenharmony_ci    return ret;
366e1051a39Sopenharmony_ci
367e1051a39Sopenharmony_ciNote that we have made use of the special "null" provider here which we load
368e1051a39Sopenharmony_ciinto the default library context. We could have chosen to use the default
369e1051a39Sopenharmony_cilibrary context for FIPS usage, and just create one additional library context
370e1051a39Sopenharmony_cifor other usages - or vice versa. However if code has not been converted to use
371e1051a39Sopenharmony_cilibrary contexts then the default library context will be automatically used.
372e1051a39Sopenharmony_ciThis could be the case for your own existing applications as well as certain
373e1051a39Sopenharmony_ciparts of OpenSSL itself. Not all parts of OpenSSL are library context aware. If
374e1051a39Sopenharmony_cithis happens then you could "accidentally" use the wrong library context for a
375e1051a39Sopenharmony_ciparticular operation. To be sure this doesn't happen you can load the "null"
376e1051a39Sopenharmony_ciprovider into the default library context. Because a provider has been
377e1051a39Sopenharmony_ciexplicitly loaded, the default provider will not automatically load. This means
378e1051a39Sopenharmony_cicode using the default context by accident will fail because no algorithms will
379e1051a39Sopenharmony_cibe available.
380e1051a39Sopenharmony_ci
381e1051a39Sopenharmony_ciSee L<migration_guide(7)/Library Context> for additional information about the
382e1051a39Sopenharmony_ciLibrary Context.
383e1051a39Sopenharmony_ci
384e1051a39Sopenharmony_ci=head2 Using Encoders and Decoders with the FIPS module
385e1051a39Sopenharmony_ci
386e1051a39Sopenharmony_ciEncoders and decoders are used to read and write keys or parameters from or to
387e1051a39Sopenharmony_cisome external format (for example a PEM file). If your application generates
388e1051a39Sopenharmony_cikeys or parameters that then need to be written into PEM or DER format
389e1051a39Sopenharmony_cithen it is likely that you will need to use an encoder to do this. Similarly
390e1051a39Sopenharmony_ciyou need a decoder to read previously saved keys and parameters. In most cases
391e1051a39Sopenharmony_cithis will be invisible to you if you are using APIs that existed in
392e1051a39Sopenharmony_ciOpenSSL 1.1.1 or earlier such as L<i2d_PrivateKey(3)>. However the appropriate
393e1051a39Sopenharmony_ciencoder/decoder will need to be available in the library context associated with
394e1051a39Sopenharmony_cithe key or parameter object. The built-in OpenSSL encoders and decoders are
395e1051a39Sopenharmony_ciimplemented in both the default and base providers and are not in the FIPS
396e1051a39Sopenharmony_cimodule boundary. However since they are not cryptographic algorithms themselves
397e1051a39Sopenharmony_ciit is still possible to use them in conjunction with the FIPS module, and
398e1051a39Sopenharmony_citherefore these encoders/decoders have the C<fips=yes> property against them.
399e1051a39Sopenharmony_ciYou should ensure that either the default or base provider is loaded into the
400e1051a39Sopenharmony_cilibrary context in this case.
401e1051a39Sopenharmony_ci
402e1051a39Sopenharmony_ci=head2 Using the FIPS module in SSL/TLS
403e1051a39Sopenharmony_ci
404e1051a39Sopenharmony_ciWriting an application that uses libssl in conjunction with the FIPS module is
405e1051a39Sopenharmony_cimuch the same as writing a normal libssl application. If you are using global
406e1051a39Sopenharmony_ciproperties and the default library context to specify usage of FIPS validated
407e1051a39Sopenharmony_cialgorithms then this will happen automatically for all cryptographic algorithms
408e1051a39Sopenharmony_ciin libssl. If you are using a nondefault library context to load the FIPS
409e1051a39Sopenharmony_ciprovider then you can supply this to libssl using the function
410e1051a39Sopenharmony_ciL<SSL_CTX_new_ex(3)>. This works as a drop in replacement for the function
411e1051a39Sopenharmony_ciL<SSL_CTX_new(3)> except it provides you with the capability to specify the
412e1051a39Sopenharmony_cilibrary context to be used. You can also use the same function to specify
413e1051a39Sopenharmony_cilibssl specific properties to use.
414e1051a39Sopenharmony_ci
415e1051a39Sopenharmony_ciIn this first example we create two SSL_CTX objects using two different library
416e1051a39Sopenharmony_cicontexts.
417e1051a39Sopenharmony_ci
418e1051a39Sopenharmony_ci    /*
419e1051a39Sopenharmony_ci     * We assume that a nondefault library context with the FIPS
420e1051a39Sopenharmony_ci     * provider loaded has been created called fips_libctx.
421e1051a39Sopenharmony_ci     */
422e1051a39Sopenharmony_ci    SSL_CTX *fips_ssl_ctx = SSL_CTX_new_ex(fips_libctx, NULL, TLS_method());
423e1051a39Sopenharmony_ci    /*
424e1051a39Sopenharmony_ci     * We assume that a nondefault library context with the default
425e1051a39Sopenharmony_ci     * provider loaded has been created called non_fips_libctx.
426e1051a39Sopenharmony_ci     */
427e1051a39Sopenharmony_ci    SSL_CTX *non_fips_ssl_ctx = SSL_CTX_new_ex(non_fips_libctx, NULL,
428e1051a39Sopenharmony_ci                                               TLS_method());
429e1051a39Sopenharmony_ci
430e1051a39Sopenharmony_ciIn this second example we create two SSL_CTX objects using different properties
431e1051a39Sopenharmony_cito specify FIPS usage:
432e1051a39Sopenharmony_ci
433e1051a39Sopenharmony_ci    /*
434e1051a39Sopenharmony_ci     * The "fips=yes" property includes all FIPS approved algorithms
435e1051a39Sopenharmony_ci     * as well as encoders from the default provider that are allowed
436e1051a39Sopenharmony_ci     * to be used. The NULL below indicates that we are using the
437e1051a39Sopenharmony_ci     * default library context.
438e1051a39Sopenharmony_ci     */
439e1051a39Sopenharmony_ci    SSL_CTX *fips_ssl_ctx = SSL_CTX_new_ex(NULL, "fips=yes", TLS_method());
440e1051a39Sopenharmony_ci    /*
441e1051a39Sopenharmony_ci     * The "provider!=fips" property allows algorithms from any
442e1051a39Sopenharmony_ci     * provider except the FIPS provider
443e1051a39Sopenharmony_ci     */
444e1051a39Sopenharmony_ci    SSL_CTX *non_fips_ssl_ctx = SSL_CTX_new_ex(NULL, "provider!=fips",
445e1051a39Sopenharmony_ci                                               TLS_method());
446e1051a39Sopenharmony_ci
447e1051a39Sopenharmony_ci=head2 Confirming that an algorithm is being provided by the FIPS module
448e1051a39Sopenharmony_ci
449e1051a39Sopenharmony_ciA chain of links needs to be followed to go from an algorithm instance to the
450e1051a39Sopenharmony_ciprovider that implements it. The process is similar for all algorithms. Here the
451e1051a39Sopenharmony_ciexample of a digest is used.
452e1051a39Sopenharmony_ci
453e1051a39Sopenharmony_ciTo go from an B<EVP_MD_CTX> to an B<EVP_MD>, use L<EVP_MD_CTX_md(3)> .
454e1051a39Sopenharmony_ciTo go from the B<EVP_MD> to its B<OSSL_PROVIDER>,
455e1051a39Sopenharmony_ciuse L<EVP_MD_get0_provider(3)>.
456e1051a39Sopenharmony_ciTo extract the name from the B<OSSL_PROVIDER>, use
457e1051a39Sopenharmony_ciL<OSSL_PROVIDER_get0_name(3)>.
458e1051a39Sopenharmony_ci
459e1051a39Sopenharmony_ci=head1 SEE ALSO
460e1051a39Sopenharmony_ci
461e1051a39Sopenharmony_ciL<migration_guide(7)>, L<crypto(7)>, L<fips_config(5)>
462e1051a39Sopenharmony_ci
463e1051a39Sopenharmony_ci=head1 HISTORY
464e1051a39Sopenharmony_ci
465e1051a39Sopenharmony_ciThe FIPS module guide was created for use with the new FIPS provider
466e1051a39Sopenharmony_ciin OpenSSL 3.0.
467e1051a39Sopenharmony_ci
468e1051a39Sopenharmony_ci=head1 COPYRIGHT
469e1051a39Sopenharmony_ci
470e1051a39Sopenharmony_ciCopyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
471e1051a39Sopenharmony_ci
472e1051a39Sopenharmony_ciLicensed under the Apache License 2.0 (the "License").  You may not use
473e1051a39Sopenharmony_cithis file except in compliance with the License.  You can obtain a copy
474e1051a39Sopenharmony_ciin the file LICENSE in the source distribution or at
475e1051a39Sopenharmony_ciL<https://www.openssl.org/source/license.html>.
476e1051a39Sopenharmony_ci
477e1051a39Sopenharmony_ci=cut
478