]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/fips/self_test.c
FIPS: rename the status call to is_running.
[thirdparty/openssl.git] / providers / fips / self_test.c
1 /*
2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11 #include <openssl/evp.h>
12 #include <openssl/params.h>
13 #include <openssl/crypto.h>
14 #include <openssl/fipskey.h>
15 #include <openssl/err.h>
16 #include "e_os.h"
17 #include "prov/providercommonerr.h"
18 #include "prov/providercommon.h"
19
20 /*
21 * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
22 * module because all such initialisation should be associated with an
23 * individual OPENSSL_CTX. That doesn't work with the self test though because
24 * it should be run once regardless of the number of OPENSSL_CTXs we have.
25 */
26 #define ALLOW_RUN_ONCE_IN_FIPS
27 #include <internal/thread_once.h>
28 #include "self_test.h"
29
30 #define FIPS_STATE_INIT 0
31 #define FIPS_STATE_SELFTEST 1
32 #define FIPS_STATE_RUNNING 2
33 #define FIPS_STATE_ERROR 3
34
35 /* The size of a temp buffer used to read in data */
36 #define INTEGRITY_BUF_SIZE (4096)
37 #define MAX_MD_SIZE 64
38 #define MAC_NAME "HMAC"
39 #define DIGEST_NAME "SHA256"
40
41 static int FIPS_state = FIPS_STATE_INIT;
42 static CRYPTO_RWLOCK *self_test_lock = NULL;
43 static unsigned char fixed_key[32] = { FIPS_KEY_ELEMENTS };
44
45 static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
46 DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
47 {
48 /*
49 * This lock gets freed in platform specific ways that may occur after we
50 * do mem leak checking. If we don't know how to free it for a particular
51 * platform then we just leak it deliberately. So we temporarily disable the
52 * mem leak checking while we allocate this.
53 */
54 self_test_lock = CRYPTO_THREAD_lock_new();
55 return self_test_lock != NULL;
56 }
57
58 #define DEP_DECLARE() \
59 void init(void); \
60 void cleanup(void);
61
62 /*
63 * This is the Default Entry Point (DEP) code. Every platform must have a DEP.
64 * See FIPS 140-2 IG 9.10
65 *
66 * If we're run on a platform where we don't know how to define the DEP then
67 * the self-tests will never get triggered (FIPS_state never moves to
68 * FIPS_STATE_SELFTEST). This will be detected as an error when SELF_TEST_post()
69 * is called from OSSL_provider_init(), and so the fips module will be unusable
70 * on those platforms.
71 */
72 #if defined(_WIN32) || defined(__CYGWIN__)
73 # ifdef __CYGWIN__
74 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
75 # include <windows.h>
76 /*
77 * this has side-effect of _WIN32 getting defined, which otherwise is
78 * mutually exclusive with __CYGWIN__...
79 */
80 # endif
81
82 DEP_DECLARE()
83 # define DEP_INIT_ATTRIBUTE
84 # define DEP_FINI_ATTRIBUTE
85 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
86 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
87 {
88 switch (fdwReason) {
89 case DLL_PROCESS_ATTACH:
90 init();
91 break;
92 case DLL_PROCESS_DETACH:
93 cleanup();
94 break;
95 default:
96 break;
97 }
98 return TRUE;
99 }
100 #elif defined(__sun) || defined(_AIX)
101
102 DEP_DECLARE() /* must be declared before pragma */
103 # define DEP_INIT_ATTRIBUTE
104 # define DEP_FINI_ATTRIBUTE
105 # pragma init(init)
106 # pragma fini(cleanup)
107
108 #elif defined(__hpux)
109
110 DEP_DECLARE()
111 # define DEP_INIT_ATTRIBUTE
112 # define DEP_FINI_ATTRIBUTE
113 # pragma init "init"
114 # pragma fini "cleanup"
115
116 #elif defined(__GNUC__)
117 # define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
118 # define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
119 #endif
120
121 #if defined(DEP_INIT_ATTRIBUTE) && defined(DEP_FINI_ATTRIBUTE)
122 DEP_INIT_ATTRIBUTE void init(void)
123 {
124 FIPS_state = FIPS_STATE_SELFTEST;
125 }
126
127 DEP_FINI_ATTRIBUTE void cleanup(void)
128 {
129 CRYPTO_THREAD_lock_free(self_test_lock);
130 }
131 #endif
132
133 /*
134 * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
135 * the result matches the expected value.
136 * Return 1 if verified, or 0 if it fails.
137 */
138 static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
139 unsigned char *expected, size_t expected_len,
140 OPENSSL_CTX *libctx, OSSL_SELF_TEST *ev,
141 const char *event_type)
142 {
143 int ret = 0, status;
144 unsigned char out[MAX_MD_SIZE];
145 unsigned char buf[INTEGRITY_BUF_SIZE];
146 size_t bytes_read = 0, out_len = 0;
147 EVP_MAC *mac = NULL;
148 EVP_MAC_CTX *ctx = NULL;
149 OSSL_PARAM params[3], *p = params;
150
151 OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
152
153 mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
154 ctx = EVP_MAC_CTX_new(mac);
155 if (mac == NULL || ctx == NULL)
156 goto err;
157
158 *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME,
159 strlen(DIGEST_NAME) + 1);
160 *p++ = OSSL_PARAM_construct_octet_string("key", fixed_key,
161 sizeof(fixed_key));
162 *p = OSSL_PARAM_construct_end();
163
164 if (EVP_MAC_CTX_set_params(ctx, params) <= 0
165 || !EVP_MAC_init(ctx))
166 goto err;
167
168 while (1) {
169 status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
170 if (status != 1)
171 break;
172 if (!EVP_MAC_update(ctx, buf, bytes_read))
173 goto err;
174 }
175 if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
176 goto err;
177
178 OSSL_SELF_TEST_oncorrupt_byte(ev, out);
179 if (expected_len != out_len
180 || memcmp(expected, out, out_len) != 0)
181 goto err;
182 ret = 1;
183 err:
184 OSSL_SELF_TEST_onend(ev, ret);
185 EVP_MAC_CTX_free(ctx);
186 EVP_MAC_free(mac);
187 return ret;
188 }
189
190 /* This API is triggered either on loading of the FIPS module or on demand */
191 int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
192 {
193 int ok = 0;
194 int kats_already_passed = 0;
195 long checksum_len;
196 OSSL_CORE_BIO *bio_module = NULL, *bio_indicator = NULL;
197 unsigned char *module_checksum = NULL;
198 unsigned char *indicator_checksum = NULL;
199 int loclstate;
200 OSSL_SELF_TEST *ev = NULL;
201
202 if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
203 return 0;
204
205 CRYPTO_THREAD_read_lock(self_test_lock);
206 loclstate = FIPS_state;
207 CRYPTO_THREAD_unlock(self_test_lock);
208
209 if (loclstate == FIPS_STATE_RUNNING) {
210 if (!on_demand_test)
211 return 1;
212 } else if (loclstate != FIPS_STATE_SELFTEST) {
213 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
214 return 0;
215 }
216
217 CRYPTO_THREAD_write_lock(self_test_lock);
218 if (FIPS_state == FIPS_STATE_RUNNING) {
219 if (!on_demand_test) {
220 CRYPTO_THREAD_unlock(self_test_lock);
221 return 1;
222 }
223 FIPS_state = FIPS_STATE_SELFTEST;
224 } else if (FIPS_state != FIPS_STATE_SELFTEST) {
225 CRYPTO_THREAD_unlock(self_test_lock);
226 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
227 return 0;
228 }
229 if (st == NULL
230 || st->module_checksum_data == NULL) {
231 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
232 goto end;
233 }
234
235 ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
236 if (ev == NULL)
237 goto end;
238
239 module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
240 &checksum_len);
241 if (module_checksum == NULL) {
242 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
243 goto end;
244 }
245 bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
246
247 /* Always check the integrity of the fips module */
248 if (bio_module == NULL
249 || !verify_integrity(bio_module, st->bio_read_ex_cb,
250 module_checksum, checksum_len, st->libctx,
251 ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
252 ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
253 goto end;
254 }
255
256 /* This will be NULL during installation - so the self test KATS will run */
257 if (st->indicator_data != NULL) {
258 /*
259 * If the kats have already passed indicator is set - then check the
260 * integrity of the indicator.
261 */
262 if (st->indicator_checksum_data == NULL) {
263 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
264 goto end;
265 }
266 indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
267 &checksum_len);
268 if (indicator_checksum == NULL) {
269 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
270 goto end;
271 }
272
273 bio_indicator =
274 (*st->bio_new_buffer_cb)(st->indicator_data,
275 strlen(st->indicator_data));
276 if (bio_indicator == NULL
277 || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
278 indicator_checksum, checksum_len,
279 st->libctx, ev,
280 OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY)) {
281 ERR_raise(ERR_LIB_PROV, PROV_R_INDICATOR_INTEGRITY_FAILURE);
282 goto end;
283 } else {
284 kats_already_passed = 1;
285 }
286 }
287
288 /* Only runs the KAT's during installation OR on_demand() */
289 if (on_demand_test || kats_already_passed == 0) {
290 if (!SELF_TEST_kats(ev, st->libctx)) {
291 ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
292 goto end;
293 }
294 }
295 ok = 1;
296 end:
297 OSSL_SELF_TEST_free(ev);
298 OPENSSL_free(module_checksum);
299 OPENSSL_free(indicator_checksum);
300
301 if (st != NULL) {
302 (*st->bio_free_cb)(bio_indicator);
303 (*st->bio_free_cb)(bio_module);
304 }
305 FIPS_state = ok ? FIPS_STATE_RUNNING : FIPS_STATE_ERROR;
306 CRYPTO_THREAD_unlock(self_test_lock);
307
308 return ok;
309 }
310
311
312 int ossl_prov_is_running(void)
313 {
314 return FIPS_state == FIPS_STATE_RUNNING
315 || FIPS_state == FIPS_STATE_SELFTEST;
316 }