xref: /freebsd/crypto/openssl/test/defltfips_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/evp.h>
12*e0c4386eSCy Schubert #include <openssl/provider.h>
13*e0c4386eSCy Schubert #include "testutil.h"
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert static int is_fips;
16*e0c4386eSCy Schubert static int bad_fips;
17*e0c4386eSCy Schubert 
test_is_fips_enabled(void)18*e0c4386eSCy Schubert static int test_is_fips_enabled(void)
19*e0c4386eSCy Schubert {
20*e0c4386eSCy Schubert     int is_fips_enabled, is_fips_loaded;
21*e0c4386eSCy Schubert     EVP_MD *sha256 = NULL;
22*e0c4386eSCy Schubert 
23*e0c4386eSCy Schubert     /*
24*e0c4386eSCy Schubert      * Check we're in FIPS mode when we're supposed to be. We do this early to
25*e0c4386eSCy Schubert      * confirm that EVP_default_properties_is_fips_enabled() works even before
26*e0c4386eSCy Schubert      * other function calls have auto-loaded the config file.
27*e0c4386eSCy Schubert      */
28*e0c4386eSCy Schubert     is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL);
29*e0c4386eSCy Schubert     is_fips_loaded = OSSL_PROVIDER_available(NULL, "fips");
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert     /*
32*e0c4386eSCy Schubert      * Check we're in an expected state. EVP_default_properties_is_fips_enabled
33*e0c4386eSCy Schubert      * can return true even if the FIPS provider isn't loaded - it is only based
34*e0c4386eSCy Schubert      * on the default properties. However we only set those properties if also
35*e0c4386eSCy Schubert      * loading the FIPS provider.
36*e0c4386eSCy Schubert      */
37*e0c4386eSCy Schubert     if (!TEST_int_eq(is_fips || bad_fips, is_fips_enabled)
38*e0c4386eSCy Schubert             || !TEST_int_eq(is_fips && !bad_fips, is_fips_loaded))
39*e0c4386eSCy Schubert         return 0;
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert     /*
42*e0c4386eSCy Schubert      * Fetching an algorithm shouldn't change the state and should come from
43*e0c4386eSCy Schubert      * expected provider.
44*e0c4386eSCy Schubert      */
45*e0c4386eSCy Schubert     sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL);
46*e0c4386eSCy Schubert     if (bad_fips) {
47*e0c4386eSCy Schubert         if (!TEST_ptr_null(sha256)) {
48*e0c4386eSCy Schubert             EVP_MD_free(sha256);
49*e0c4386eSCy Schubert             return 0;
50*e0c4386eSCy Schubert         }
51*e0c4386eSCy Schubert     } else {
52*e0c4386eSCy Schubert         if (!TEST_ptr(sha256))
53*e0c4386eSCy Schubert             return 0;
54*e0c4386eSCy Schubert         if (is_fips
55*e0c4386eSCy Schubert             && !TEST_str_eq(OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(sha256)),
56*e0c4386eSCy Schubert                             "fips")) {
57*e0c4386eSCy Schubert             EVP_MD_free(sha256);
58*e0c4386eSCy Schubert             return 0;
59*e0c4386eSCy Schubert         }
60*e0c4386eSCy Schubert         EVP_MD_free(sha256);
61*e0c4386eSCy Schubert     }
62*e0c4386eSCy Schubert 
63*e0c4386eSCy Schubert     /* State should still be consistent */
64*e0c4386eSCy Schubert     is_fips_enabled = EVP_default_properties_is_fips_enabled(NULL);
65*e0c4386eSCy Schubert     if (!TEST_int_eq(is_fips || bad_fips, is_fips_enabled))
66*e0c4386eSCy Schubert         return 0;
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert     return 1;
69*e0c4386eSCy Schubert }
70*e0c4386eSCy Schubert 
setup_tests(void)71*e0c4386eSCy Schubert int setup_tests(void)
72*e0c4386eSCy Schubert {
73*e0c4386eSCy Schubert     size_t argc;
74*e0c4386eSCy Schubert     char *arg1;
75*e0c4386eSCy Schubert 
76*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
77*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
78*e0c4386eSCy Schubert         return 0;
79*e0c4386eSCy Schubert     }
80*e0c4386eSCy Schubert 
81*e0c4386eSCy Schubert     argc = test_get_argument_count();
82*e0c4386eSCy Schubert     switch(argc) {
83*e0c4386eSCy Schubert     case 0:
84*e0c4386eSCy Schubert         is_fips = 0;
85*e0c4386eSCy Schubert         bad_fips = 0;
86*e0c4386eSCy Schubert         break;
87*e0c4386eSCy Schubert     case 1:
88*e0c4386eSCy Schubert         arg1 = test_get_argument(0);
89*e0c4386eSCy Schubert         if (strcmp(arg1, "fips") == 0) {
90*e0c4386eSCy Schubert             is_fips = 1;
91*e0c4386eSCy Schubert             bad_fips = 0;
92*e0c4386eSCy Schubert             break;
93*e0c4386eSCy Schubert         } else if (strcmp(arg1, "badfips") == 0) {
94*e0c4386eSCy Schubert             /* Configured for FIPS, but the module fails to load */
95*e0c4386eSCy Schubert             is_fips = 0;
96*e0c4386eSCy Schubert             bad_fips = 1;
97*e0c4386eSCy Schubert             break;
98*e0c4386eSCy Schubert         }
99*e0c4386eSCy Schubert         /* fall through */
100*e0c4386eSCy Schubert     default:
101*e0c4386eSCy Schubert         TEST_error("Invalid argument\n");
102*e0c4386eSCy Schubert         return 0;
103*e0c4386eSCy Schubert     }
104*e0c4386eSCy Schubert 
105*e0c4386eSCy Schubert     /* Must be the first test before any other libcrypto calls are made */
106*e0c4386eSCy Schubert     ADD_TEST(test_is_fips_enabled);
107*e0c4386eSCy Schubert     return 1;
108*e0c4386eSCy Schubert }
109