1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
* Copyright (C) 2013 Red Hat
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
#include <gnutls_int.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include <unistd.h>
#include <gnutls_errors.h>
#include <fips.h>
#include <gnutls/self-test.h>
#include <stdio.h>
#include <random.h>
unsigned int _gnutls_lib_mode = LIB_STATE_POWERON;
#ifdef ENABLE_FIPS140
#include <dlfcn.h>
#define FIPS_KERNEL_FILE "/proc/sys/crypto/fips_enabled"
#define FIPS_SYSTEM_FILE "/etc/system-fips"
static int _fips_mode = -1;
static int _skip_integrity_checks = 0;
/* Returns:
* 0 - FIPS mode disabled
* 1 - FIPS mode enabled and enforced
* 2 - FIPS in testing mode
*/
unsigned _gnutls_fips_mode_enabled(void)
{
unsigned f1p = 0, f2p;
FILE* fd;
const char *p;
if (_fips_mode != -1)
return _fips_mode;
p = getenv("GNUTLS_SKIP_FIPS_INTEGRITY_CHECKS");
if (p && p[0] == '1') {
_skip_integrity_checks = 1;
}
p = getenv("GNUTLS_FORCE_FIPS_MODE");
if (p) {
if (p[0] == '1')
_fips_mode = 1;
else if (p[0] == '2')
_fips_mode = 2;
else
_fips_mode = 0;
return _fips_mode;
}
fd = fopen(FIPS_KERNEL_FILE, "r");
if (fd != NULL) {
f1p = fgetc(fd);
fclose(fd);
if (f1p == '1') f1p = 1;
else f1p = 0;
}
f2p = !access(FIPS_SYSTEM_FILE, F_OK);
if (f1p != 0 && f2p != 0) {
_gnutls_debug_log("FIPS140-2 mode enabled\n");
_fips_mode = 1;
return _fips_mode;
}
if (f2p != 0) {
/* a funny state where self tests are performed
* and ignored */
_gnutls_debug_log("FIPS140-2 ZOMBIE mode enabled\n");
_fips_mode = 2;
return _fips_mode;
}
_fips_mode = 0;
return _fips_mode;
}
/* This _fips_mode == 2 is a strange mode where checks are being
* performed, but its output is ignored. */
void _gnutls_fips_mode_reset_zombie(void)
{
if (_fips_mode == 2) {
_fips_mode = 0;
}
}
#define GNUTLS_LIBRARY_NAME "libgnutls.so.28"
#define NETTLE_LIBRARY_NAME "libnettle.so.4"
#define HOGWEED_LIBRARY_NAME "libhogweed.so.2"
#define GMP_LIBRARY_NAME "libgmp.so.10"
static const char fips_key[] = "orboDeJITITejsirpADONivirpUkvarP";
#define HMAC_SUFFIX ".hmac"
#define HMAC_SIZE 32
#define HMAC_ALGO GNUTLS_MAC_SHA256
static int get_library_path(const char* lib, const char* symbol, char* path, size_t path_size)
{
Dl_info info;
int ret;
void *dl, *sym;
dl = dlopen(lib, RTLD_LAZY);
if (dl == NULL)
return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
sym = dlsym(dl, symbol);
if (sym == NULL) {
ret = gnutls_assert_val(GNUTLS_E_FILE_ERROR);
goto cleanup;
}
ret = dladdr(sym, &info);
if (ret == 0) {
ret = gnutls_assert_val(GNUTLS_E_FILE_ERROR);
goto cleanup;
}
snprintf(path, path_size, "%s", info.dli_fname);
ret = 0;
cleanup:
dlclose(dl);
return ret;
}
static void get_hmac_file(char *mac_file, size_t mac_file_size, const char* orig)
{
char* p;
p = strrchr(orig, '/');
if (p==NULL) {
snprintf(mac_file, mac_file_size, ".%s"HMAC_SUFFIX, orig);
return;
}
snprintf(mac_file, mac_file_size, "%.*s/.%s"HMAC_SUFFIX, (int)(p-orig), orig, p+1);
}
static void get_hmac_file2(char *mac_file, size_t mac_file_size, const char* orig)
{
char* p;
p = strrchr(orig, '/');
if (p==NULL) {
snprintf(mac_file, mac_file_size, "fipscheck/%s"HMAC_SUFFIX, orig);
return;
}
snprintf(mac_file, mac_file_size, "%.*s/fipscheck/%s"HMAC_SUFFIX, (int)(p-orig), orig, p+1);
}
/* Run an HMAC using the key above on the library binary data.
* Returns true on success and false on error.
*/
static unsigned check_binary_integrity(const char* libname, const char* symbol)
{
int ret;
unsigned prev;
char mac_file[GNUTLS_PATH_MAX];
char file[GNUTLS_PATH_MAX];
uint8_t hmac[HMAC_SIZE];
uint8_t new_hmac[HMAC_SIZE];
size_t hmac_size;
gnutls_datum_t data;
ret = get_library_path(libname, symbol, file, sizeof(file));
if (ret < 0) {
_gnutls_debug_log("Could not get path for library %s\n", libname);
return 0;
}
_gnutls_debug_log("Loading: %s\n", file);
ret = gnutls_load_file(file, &data);
if (ret < 0) {
_gnutls_debug_log("Could not load: %s\n", file);
return gnutls_assert_val(0);
}
prev = _gnutls_get_lib_state();
_gnutls_switch_lib_state(LIB_STATE_OPERATIONAL);
ret = gnutls_hmac_fast(HMAC_ALGO, fips_key, sizeof(fips_key)-1,
data.data, data.size, new_hmac);
_gnutls_switch_lib_state(prev);
gnutls_free(data.data);
if (ret < 0)
return gnutls_assert_val(0);
/* now open the .hmac file and compare */
get_hmac_file(mac_file, sizeof(mac_file), file);
ret = gnutls_load_file(mac_file, &data);
if (ret < 0) {
get_hmac_file2(mac_file, sizeof(mac_file), file);
ret = gnutls_load_file(mac_file, &data);
if (ret < 0) {
_gnutls_debug_log("Could not open %s for MAC testing: %s\n", mac_file, gnutls_strerror(ret));
return gnutls_assert_val(0);
}
}
hmac_size = sizeof(hmac);
ret = _gnutls_hex2bin((void*)data.data, data.size, hmac, &hmac_size);
gnutls_free(data.data);
if (ret < 0) {
_gnutls_debug_log("Could not convert hex data to binary for MAC testing for %s.\n", libname);
return gnutls_assert_val(0);
}
if (hmac_size != sizeof(hmac) ||
memcmp(hmac, new_hmac, sizeof(hmac)) != 0) {
_gnutls_debug_log("Calculated MAC for %s does not match\n", libname);
return gnutls_assert_val(0);
}
_gnutls_debug_log("Successfully verified MAC for %s (%s)\n", mac_file, libname);
return 1;
}
int _gnutls_fips_perform_self_checks1(void)
{
int ret;
_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
/* Tests the FIPS algorithms used by nettle internally.
* In our case we test AES-CBC since nettle's AES is used by
* the DRBG-AES.
*/
/* ciphers - one test per cipher */
ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_128_CBC);
if (ret < 0) {
gnutls_assert();
goto error;
}
return 0;
error:
_gnutls_switch_lib_state(LIB_STATE_ERROR);
_gnutls_audit_log(NULL, "FIPS140-2 self testing part1 failed\n");
return GNUTLS_E_SELF_TEST_ERROR;
}
int _gnutls_fips_perform_self_checks2(void)
{
int ret;
_gnutls_switch_lib_state(LIB_STATE_SELFTEST);
/* Tests the FIPS algorithms */
/* ciphers - one test per cipher */
ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_3DES_CBC);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_GCM);
if (ret < 0) {
gnutls_assert();
goto error;
}
/* MAC (includes message digest test) */
ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA1);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA224);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA256);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA384);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA512);
if (ret < 0) {
gnutls_assert();
goto error;
}
/* PK */
ret = gnutls_pk_self_test(0, GNUTLS_PK_RSA);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_pk_self_test(0, GNUTLS_PK_DSA);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_pk_self_test(0, GNUTLS_PK_EC);
if (ret < 0) {
gnutls_assert();
goto error;
}
ret = gnutls_pk_self_test(0, GNUTLS_PK_DH);
if (ret < 0) {
gnutls_assert();
goto error;
}
if (_gnutls_rnd_ops.self_test == NULL) {
gnutls_assert();
goto error;
}
ret = _gnutls_rnd_ops.self_test();
if (ret < 0) {
gnutls_assert();
goto error;
}
if (_skip_integrity_checks == 0) {
ret = check_binary_integrity(GNUTLS_LIBRARY_NAME, "gnutls_global_init");
if (ret == 0) {
gnutls_assert();
goto error;
}
ret = check_binary_integrity(NETTLE_LIBRARY_NAME, "nettle_aes_set_encrypt_key");
if (ret == 0) {
gnutls_assert();
goto error;
}
ret = check_binary_integrity(HOGWEED_LIBRARY_NAME, "nettle_mpz_sizeinbase_256_u");
if (ret == 0) {
gnutls_assert();
goto error;
}
ret = check_binary_integrity(GMP_LIBRARY_NAME, "__gmpz_init");
if (ret == 0) {
gnutls_assert();
goto error;
}
}
return 0;
error:
_gnutls_switch_lib_state(LIB_STATE_ERROR);
_gnutls_audit_log(NULL, "FIPS140-2 self testing part 2 failed\n");
return GNUTLS_E_SELF_TEST_ERROR;
}
#endif
/**
* gnutls_fips140_mode_enabled:
*
* Checks whether this library is in FIPS140 mode.
*
* Returns: return non-zero if true or zero if false.
*
* Since: 3.3.0
**/
int gnutls_fips140_mode_enabled(void)
{
#ifdef ENABLE_FIPS140
int ret = _gnutls_fips_mode_enabled();
if (ret == 1)
return ret;
#endif
return 0;
}
void _gnutls_lib_simulate_error(void)
{
_gnutls_switch_lib_state(LIB_STATE_ERROR);
}
|