Coverage Report

Created: 2024-07-23 07:36

/src/gnutls/lib/fips.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2013 Red Hat
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GnuTLS.
7
 *
8
 * The GnuTLS is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
#include "gnutls_int.h"
23
#include <gnutls/gnutls.h>
24
#include <gnutls/crypto.h>
25
#include <unistd.h>
26
#include "dirname.h"
27
#include "errors.h"
28
#include "file.h"
29
#include "inih/ini.h"
30
#include "str.h"
31
#include "fips.h"
32
#include <gnutls/self-test.h>
33
#include <stdio.h>
34
#include "extras/hex.h"
35
#include "random.h"
36
37
#include "gthreads.h"
38
39
#ifdef HAVE_DL_ITERATE_PHDR
40
#include <link.h>
41
#endif
42
43
unsigned int _gnutls_lib_state = LIB_STATE_POWERON;
44
45
struct gnutls_fips140_context_st {
46
  gnutls_fips140_operation_state_t state;
47
  struct gnutls_fips140_context_st *next;
48
};
49
50
#ifdef ENABLE_FIPS140
51
52
#include <dlfcn.h>
53
54
#define FIPS_KERNEL_FILE "/proc/sys/crypto/fips_enabled"
55
#define FIPS_SYSTEM_FILE "/etc/system-fips"
56
57
/* We provide a per-thread FIPS-mode so that an application
58
 * can use gnutls_fips140_set_mode() to override a specific
59
 * operation on a thread */
60
static gnutls_fips_mode_t _global_fips_mode = -1;
61
static _Thread_local gnutls_fips_mode_t _tfips_mode = -1;
62
63
static _Thread_local gnutls_fips140_context_t _tfips_context = NULL;
64
65
static int _skip_integrity_checks = 0;
66
67
/* Returns:
68
 * a gnutls_fips_mode_t value
69
 */
70
unsigned _gnutls_fips_mode_enabled(void)
71
{
72
  unsigned f1p = 0, f2p;
73
  FILE *fd;
74
  const char *p;
75
  unsigned ret;
76
77
  /* We initialize this threads' mode, and
78
   * the global mode if not already initialized.
79
   * When the global mode is initialized, then
80
   * the thread mode is copied from it. As this
81
   * is called on library initialization, the
82
   * _global_fips_mode is always set during app run.
83
   */
84
  if (_tfips_mode != (gnutls_fips_mode_t)-1)
85
    return _tfips_mode;
86
87
  if (_global_fips_mode != (gnutls_fips_mode_t)-1) {
88
    return _global_fips_mode;
89
  }
90
91
  p = secure_getenv("GNUTLS_SKIP_FIPS_INTEGRITY_CHECKS");
92
  if (p && p[0] == '1') {
93
    _skip_integrity_checks = 1;
94
  }
95
96
  p = secure_getenv("GNUTLS_FORCE_FIPS_MODE");
97
  if (p) {
98
    if (p[0] == '1')
99
      ret = GNUTLS_FIPS140_STRICT;
100
    else if (p[0] == '2')
101
      ret = GNUTLS_FIPS140_SELFTESTS;
102
    else if (p[0] == '3')
103
      ret = GNUTLS_FIPS140_LAX;
104
    else if (p[0] == '4')
105
      ret = GNUTLS_FIPS140_LOG;
106
    else
107
      ret = GNUTLS_FIPS140_DISABLED;
108
109
    goto exit;
110
  }
111
112
  fd = fopen(FIPS_KERNEL_FILE, "re");
113
  if (fd != NULL) {
114
    f1p = fgetc(fd);
115
    fclose(fd);
116
117
    if (f1p == '1')
118
      f1p = 1;
119
    else
120
      f1p = 0;
121
  }
122
123
  if (f1p != 0) {
124
    _gnutls_debug_log("FIPS140-2 mode enabled\n");
125
    ret = GNUTLS_FIPS140_STRICT;
126
    goto exit;
127
  }
128
129
  f2p = !access(FIPS_SYSTEM_FILE, F_OK);
130
  if (f2p != 0) {
131
    /* a funny state where self tests are performed
132
     * and ignored */
133
    _gnutls_debug_log("FIPS140-2 ZOMBIE mode enabled\n");
134
    ret = GNUTLS_FIPS140_SELFTESTS;
135
    goto exit;
136
  }
137
138
  ret = GNUTLS_FIPS140_DISABLED;
139
  goto exit;
140
141
exit:
142
  _global_fips_mode = ret;
143
  return ret;
144
}
145
146
/* This _fips_mode == 2 is a strange mode where checks are being
147
 * performed, but its output is ignored. */
148
void _gnutls_fips_mode_reset_zombie(void)
149
{
150
  if (_global_fips_mode == GNUTLS_FIPS140_SELFTESTS) {
151
    _global_fips_mode = GNUTLS_FIPS140_DISABLED;
152
  }
153
}
154
155
/* These only works with the platform where SONAME is part of the ABI. */
156
#ifndef GNUTLS_LIBRARY_SONAME
157
#define GNUTLS_LIBRARY_SONAME "none"
158
#endif
159
160
#ifndef NETTLE_LIBRARY_SONAME
161
#define NETTLE_LIBRARY_SONAME "none"
162
#endif
163
164
#ifndef HOGWEED_LIBRARY_SONAME
165
#define HOGWEED_LIBRARY_SONAME "none"
166
#endif
167
168
#define HMAC_SIZE 32
169
#define HMAC_ALGO GNUTLS_MAC_SHA256
170
#define HMAC_FORMAT_VERSION 1
171
172
struct hmac_entry {
173
  char path[GNUTLS_PATH_MAX];
174
  uint8_t hmac[HMAC_SIZE];
175
};
176
177
struct hmac_file {
178
  int version;
179
  struct hmac_entry gnutls;
180
  struct hmac_entry nettle;
181
  struct hmac_entry hogweed;
182
#ifdef GMP_LIBRARY_SONAME
183
  struct hmac_entry gmp;
184
#endif
185
};
186
187
struct lib_paths {
188
  char gnutls[GNUTLS_PATH_MAX];
189
  char nettle[GNUTLS_PATH_MAX];
190
  char hogweed[GNUTLS_PATH_MAX];
191
#ifdef GMP_LIBRARY_SONAME
192
  char gmp[GNUTLS_PATH_MAX];
193
#endif
194
};
195
196
/*
197
 * get_hmac:
198
 * @dest: buffer for the hex value
199
 * @value: hmac value
200
 *
201
 * Parses hmac data and copies hex value into dest.
202
 * dest must point to at least HMAC_SIZE amount of memory
203
 *
204
 * Returns: 0 on success, a negative error code otherwise
205
 */
206
static int get_hmac(uint8_t *dest, const char *value)
207
{
208
  int ret;
209
  size_t hmac_size;
210
  gnutls_datum_t data;
211
212
  data.size = strlen(value);
213
  data.data = (unsigned char *)value;
214
215
  hmac_size = HMAC_SIZE;
216
  ret = gnutls_hex_decode(&data, dest, &hmac_size);
217
  if (ret < 0)
218
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
219
220
  if (hmac_size != HMAC_SIZE)
221
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
222
223
  return 0;
224
}
225
226
static int lib_handler(struct hmac_entry *entry, const char *section,
227
           const char *name, const char *value)
228
{
229
  if (!strcmp(name, "path")) {
230
    snprintf(entry->path, GNUTLS_PATH_MAX, "%s", value);
231
  } else if (!strcmp(name, "hmac")) {
232
    if (get_hmac(entry->hmac, value) < 0)
233
      return 0;
234
  } else {
235
    return 0;
236
  }
237
  return 1;
238
}
239
240
static int handler(void *user, const char *section, const char *name,
241
       const char *value)
242
{
243
  struct hmac_file *p = (struct hmac_file *)user;
244
245
  if (!strcmp(section, "global")) {
246
    if (!strcmp(name, "format-version")) {
247
      p->version = strtol(value, NULL, 10);
248
    } else {
249
      return 0;
250
    }
251
  } else if (!strcmp(section, GNUTLS_LIBRARY_SONAME)) {
252
    return lib_handler(&p->gnutls, section, name, value);
253
  } else if (!strcmp(section, NETTLE_LIBRARY_SONAME)) {
254
    return lib_handler(&p->nettle, section, name, value);
255
  } else if (!strcmp(section, HOGWEED_LIBRARY_SONAME)) {
256
    return lib_handler(&p->hogweed, section, name, value);
257
#ifdef GMP_LIBRARY_SONAME
258
  } else if (!strcmp(section, GMP_LIBRARY_SONAME)) {
259
    return lib_handler(&p->gmp, section, name, value);
260
#endif
261
  } else {
262
    return 0;
263
  }
264
  return 1;
265
}
266
267
/*
268
 * get_hmac_path:
269
 * @mac_file: buffer where the hmac file path will be written to
270
 * @mac_file_size: size of the mac_file buffer
271
 * @gnutls_path: path to the gnutls library, used to deduce hmac file path
272
 * 
273
 * Deduces hmac file path from the gnutls library path.
274
 *
275
 * Returns: 0 on success, a negative error code otherwise
276
 */
277
static int get_hmac_path(char *mac_file, size_t mac_file_size,
278
       const char *gnutls_path)
279
{
280
  int ret;
281
  char *p;
282
283
  p = strrchr(gnutls_path, '/');
284
285
  if (p == NULL)
286
    ret = snprintf(mac_file, mac_file_size, ".%s.hmac",
287
             gnutls_path);
288
  else
289
    ret = snprintf(mac_file, mac_file_size, "%.*s/.%s.hmac",
290
             (int)(p - gnutls_path), gnutls_path, p + 1);
291
292
  if ((size_t)ret >= mac_file_size)
293
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
294
295
  ret = _gnutls_file_exists(mac_file);
296
  if (ret == 0)
297
    return GNUTLS_E_SUCCESS;
298
299
  if (p == NULL)
300
    ret = snprintf(mac_file, mac_file_size, "fipscheck/.%s.hmac",
301
             gnutls_path);
302
  else
303
    ret = snprintf(mac_file, mac_file_size,
304
             "%.*s/fipscheck/.%s.hmac",
305
             (int)(p - gnutls_path), gnutls_path, p + 1);
306
307
  if ((size_t)ret >= mac_file_size)
308
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
309
310
  ret = _gnutls_file_exists(mac_file);
311
  if (ret == 0)
312
    return GNUTLS_E_SUCCESS;
313
314
  return GNUTLS_E_FILE_ERROR;
315
}
316
317
/*
318
 * load_hmac_file:
319
 * @hmac_file: hmac file structure
320
 * @hmac_path: path to the hmac file
321
 *
322
 * Loads the hmac file into the hmac file structure.
323
 *
324
 * Returns: 0 on success, a negative error code otherwise
325
 */
326
static int load_hmac_file(struct hmac_file *hmac_file, const char *hmac_path)
327
{
328
  int ret;
329
  FILE *stream;
330
331
  stream = fopen(hmac_path, "r");
332
  if (stream == NULL)
333
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
334
335
  gnutls_memset(hmac_file, 0, sizeof(*hmac_file));
336
  ret = ini_parse_file(stream, handler, hmac_file);
337
  fclose(stream);
338
  if (ret < 0)
339
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
340
341
  if (hmac_file->version != HMAC_FORMAT_VERSION)
342
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
343
344
  return 0;
345
}
346
347
/*
348
 * check_lib_hmac:
349
 * @entry: hmac file entry
350
 * @path: path to the library which hmac should be compared
351
 *
352
 * Verify that HMAC from hmac file entry matches HMAC of given library.
353
 *
354
 * Returns: 0 on successful HMAC verification, a negative error code otherwise
355
 */
356
static int check_lib_hmac(struct hmac_entry *entry, const char *path)
357
{
358
  int ret;
359
  unsigned prev;
360
  uint8_t hmac[HMAC_SIZE];
361
  gnutls_datum_t data;
362
363
  _gnutls_debug_log("Loading: %s\n", path);
364
  ret = gnutls_load_file(path, &data);
365
  if (ret < 0) {
366
    _gnutls_debug_log("Could not load %s: %s\n", path,
367
          gnutls_strerror(ret));
368
    return gnutls_assert_val(ret);
369
  }
370
371
  prev = _gnutls_get_lib_state();
372
  _gnutls_switch_lib_state(LIB_STATE_OPERATIONAL);
373
  ret = gnutls_hmac_fast(HMAC_ALGO, FIPS_KEY, sizeof(FIPS_KEY) - 1,
374
             data.data, data.size, hmac);
375
  _gnutls_switch_lib_state(prev);
376
377
  gnutls_free(data.data);
378
  if (ret < 0) {
379
    _gnutls_debug_log("Could not calculate HMAC for %s: %s\n", path,
380
          gnutls_strerror(ret));
381
    return gnutls_assert_val(ret);
382
  }
383
384
  if (gnutls_memcmp(entry->hmac, hmac, HMAC_SIZE)) {
385
    _gnutls_debug_log("Calculated MAC for %s does not match\n",
386
          path);
387
    gnutls_memset(hmac, 0, HMAC_SIZE);
388
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
389
  }
390
  _gnutls_debug_log("Successfully verified MAC for %s\n", path);
391
392
  gnutls_memset(hmac, 0, HMAC_SIZE);
393
  return 0;
394
}
395
396
#ifdef HAVE_DL_ITERATE_PHDR
397
398
static int callback(struct dl_phdr_info *info, size_t size, void *data)
399
{
400
  const char *path = info->dlpi_name;
401
  const char *soname = last_component(path);
402
  struct lib_paths *paths = (struct lib_paths *)data;
403
404
  if (!strcmp(soname, GNUTLS_LIBRARY_SONAME))
405
    _gnutls_str_cpy(paths->gnutls, GNUTLS_PATH_MAX, path);
406
  else if (!strcmp(soname, NETTLE_LIBRARY_SONAME))
407
    _gnutls_str_cpy(paths->nettle, GNUTLS_PATH_MAX, path);
408
  else if (!strcmp(soname, HOGWEED_LIBRARY_SONAME))
409
    _gnutls_str_cpy(paths->hogweed, GNUTLS_PATH_MAX, path);
410
#ifdef GMP_LIBRARY_SONAME
411
  else if (!strcmp(soname, GMP_LIBRARY_SONAME))
412
    _gnutls_str_cpy(paths->gmp, GNUTLS_PATH_MAX, path);
413
#endif
414
  return 0;
415
}
416
417
static int load_lib_paths(struct lib_paths *paths)
418
{
419
  memset(paths, 0, sizeof(*paths));
420
  dl_iterate_phdr(callback, paths);
421
422
  if (paths->gnutls[0] == '\0') {
423
    _gnutls_debug_log("Gnutls library path was not found\n");
424
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
425
  }
426
  if (paths->nettle[0] == '\0') {
427
    _gnutls_debug_log("Nettle library path was not found\n");
428
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
429
  }
430
  if (paths->hogweed[0] == '\0') {
431
    _gnutls_debug_log("Hogweed library path was not found\n");
432
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
433
  }
434
#ifdef GMP_LIBRARY_SONAME
435
  if (paths->gmp[0] == '\0') {
436
    _gnutls_debug_log("Gmp library path was not found\n");
437
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
438
  }
439
#endif
440
441
  return GNUTLS_E_SUCCESS;
442
}
443
444
#else
445
446
static int load_lib_paths(struct lib_paths *paths)
447
{
448
  (void)paths;
449
  _gnutls_debug_log("Function dl_iterate_phdr is missing\n");
450
  return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
451
}
452
453
#endif /* HAVE_DL_ITERATE_PHDR */
454
455
static int check_binary_integrity(void)
456
{
457
  int ret;
458
  struct lib_paths paths;
459
  struct hmac_file hmac;
460
  char hmac_path[GNUTLS_PATH_MAX];
461
462
  ret = load_lib_paths(&paths);
463
  if (ret < 0) {
464
    _gnutls_debug_log("Could not load library paths: %s\n",
465
          gnutls_strerror(ret));
466
    return ret;
467
  }
468
469
  ret = get_hmac_path(hmac_path, sizeof(hmac_path), paths.gnutls);
470
  if (ret < 0) {
471
    _gnutls_debug_log("Could not get hmac file path: %s\n",
472
          gnutls_strerror(ret));
473
    return ret;
474
  }
475
476
  ret = load_hmac_file(&hmac, hmac_path);
477
  if (ret < 0) {
478
    _gnutls_debug_log("Could not load hmac file: %s\n",
479
          gnutls_strerror(ret));
480
    return ret;
481
  }
482
483
  ret = check_lib_hmac(&hmac.gnutls, paths.gnutls);
484
  if (ret < 0)
485
    return ret;
486
  ret = check_lib_hmac(&hmac.nettle, paths.nettle);
487
  if (ret < 0)
488
    return ret;
489
  ret = check_lib_hmac(&hmac.hogweed, paths.hogweed);
490
  if (ret < 0)
491
    return ret;
492
#ifdef GMP_LIBRARY_SONAME
493
  ret = check_lib_hmac(&hmac.gmp, paths.gmp);
494
  if (ret < 0)
495
    return ret;
496
#endif
497
498
  return 0;
499
}
500
501
int _gnutls_fips_perform_self_checks1(void)
502
{
503
  int ret;
504
505
  /* Tests the FIPS algorithms used by nettle internally.
506
   * In our case we test AES-CBC since nettle's AES is used by
507
   * the DRBG-AES.
508
   */
509
510
  /* ciphers - one test per cipher */
511
  ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_128_CBC);
512
  if (ret < 0) {
513
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
514
  }
515
516
  return 0;
517
}
518
519
int _gnutls_fips_perform_self_checks2(void)
520
{
521
  int ret;
522
523
  /* Tests the FIPS algorithms */
524
525
  /* ciphers - one test per cipher */
526
  ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_CBC);
527
  if (ret < 0) {
528
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
529
  }
530
531
  ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_GCM);
532
  if (ret < 0) {
533
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
534
  }
535
536
  ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_XTS);
537
  if (ret < 0) {
538
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
539
  }
540
541
  ret = gnutls_cipher_self_test(0, GNUTLS_CIPHER_AES_256_CFB8);
542
  if (ret < 0) {
543
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
544
  }
545
546
  /* Digest tests */
547
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_224);
548
  if (ret < 0) {
549
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
550
  }
551
552
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_256);
553
  if (ret < 0) {
554
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
555
  }
556
557
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_384);
558
  if (ret < 0) {
559
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
560
  }
561
562
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_512);
563
  if (ret < 0) {
564
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
565
  }
566
567
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHAKE_128);
568
  if (ret < 0) {
569
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
570
  }
571
572
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHAKE_256);
573
  if (ret < 0) {
574
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
575
  }
576
577
  /* MAC (includes message digest test) */
578
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA1);
579
  if (ret < 0) {
580
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
581
  }
582
583
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA224);
584
  if (ret < 0) {
585
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
586
  }
587
588
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA256);
589
  if (ret < 0) {
590
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
591
  }
592
593
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA384);
594
  if (ret < 0) {
595
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
596
  }
597
598
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA512);
599
  if (ret < 0) {
600
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
601
  }
602
603
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_AES_CMAC_256);
604
  if (ret < 0) {
605
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
606
  }
607
608
  /* PK */
609
  if (_gnutls_config_is_rsa_pkcs1_encrypt_allowed()) {
610
    ret = gnutls_pk_self_test(0, GNUTLS_PK_RSA);
611
    if (ret < 0) {
612
      return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
613
    }
614
  }
615
616
  ret = gnutls_pk_self_test(0, GNUTLS_PK_DSA);
617
  if (ret < 0) {
618
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
619
  }
620
621
  ret = gnutls_pk_self_test(0, GNUTLS_PK_EC);
622
  if (ret < 0) {
623
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
624
  }
625
626
  ret = gnutls_pk_self_test(0, GNUTLS_PK_DH);
627
  if (ret < 0) {
628
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
629
  }
630
631
  /* HKDF */
632
  ret = gnutls_hkdf_self_test(0, GNUTLS_MAC_SHA256);
633
  if (ret < 0) {
634
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
635
  }
636
637
  /* PBKDF2 */
638
  ret = gnutls_pbkdf2_self_test(0, GNUTLS_MAC_SHA256);
639
  if (ret < 0) {
640
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
641
  }
642
643
  /* TLS-PRF */
644
  ret = gnutls_tlsprf_self_test(0, GNUTLS_MAC_SHA256);
645
  if (ret < 0) {
646
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
647
  }
648
649
  if (_gnutls_rnd_ops.self_test == NULL) {
650
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
651
  }
652
653
  /* this does not require rng initialization */
654
  ret = _gnutls_rnd_ops.self_test();
655
  if (ret < 0) {
656
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
657
  }
658
659
  if (_skip_integrity_checks == 0) {
660
    ret = check_binary_integrity();
661
    if (ret < 0) {
662
      return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
663
    }
664
  }
665
666
  return 0;
667
}
668
#endif
669
670
/**
671
 * gnutls_fips140_mode_enabled:
672
 *
673
 * Checks whether this library is in FIPS140 mode. The returned
674
 * value corresponds to the library mode as set with
675
 * gnutls_fips140_set_mode().
676
 *
677
 * If gnutls_fips140_set_mode() was called with %GNUTLS_FIPS140_SET_MODE_THREAD
678
 * then this function will return the current thread's FIPS140 mode, otherwise
679
 * the global value is returned.
680
 *
681
 * Returns: return non-zero if true or zero if false.
682
 *
683
 * Since: 3.3.0
684
 **/
685
unsigned gnutls_fips140_mode_enabled(void)
686
0
{
687
#ifdef ENABLE_FIPS140
688
  unsigned ret = _gnutls_fips_mode_enabled();
689
690
  if (ret > GNUTLS_FIPS140_DISABLED) {
691
    /* If the previous run of selftests has failed, return as if
692
     * the FIPS mode is disabled. We could use HAVE_LIB_ERROR, if
693
     * we can assume that all the selftests run atomically from
694
     * the ELF constructor.
695
     */
696
    if (_gnutls_get_lib_state() == LIB_STATE_ERROR)
697
      return 0;
698
699
    return ret;
700
  }
701
#endif
702
0
  return 0;
703
0
}
704
705
/**
706
 * gnutls_fips140_set_mode:
707
 * @mode: the FIPS140-2 mode to switch to
708
 * @flags: should be zero or %GNUTLS_FIPS140_SET_MODE_THREAD
709
 *
710
 * That function is not thread-safe when changing the mode with no flags
711
 * (globally), and should be called prior to creating any threads. Its
712
 * behavior with no flags after threads are created is undefined.
713
 *
714
 * When the flag %GNUTLS_FIPS140_SET_MODE_THREAD is specified
715
 * then this call will change the FIPS140-2 mode for this particular
716
 * thread and not for the whole process. That way an application
717
 * can utilize this function to set and reset mode for specific
718
 * operations.
719
 *
720
 * This function never fails but will be a no-op if used when
721
 * the library is not in FIPS140-2 mode. When asked to switch to unknown
722
 * values for @mode or to %GNUTLS_FIPS140_SELFTESTS mode, the library
723
 * switches to %GNUTLS_FIPS140_STRICT mode.
724
 *
725
 * Since: 3.6.2
726
 **/
727
void gnutls_fips140_set_mode(gnutls_fips_mode_t mode, unsigned flags)
728
0
{
729
#ifdef ENABLE_FIPS140
730
  gnutls_fips_mode_t prev = _gnutls_fips_mode_enabled();
731
  if (prev == GNUTLS_FIPS140_DISABLED ||
732
      prev == GNUTLS_FIPS140_SELFTESTS) {
733
    /* we need to run self-tests first to be in FIPS140-2 mode */
734
    _gnutls_audit_log(
735
      NULL,
736
      "The library should be initialized in FIPS140-2 mode to do that operation\n");
737
    return;
738
  }
739
740
  switch (mode) {
741
  case GNUTLS_FIPS140_STRICT:
742
  case GNUTLS_FIPS140_LAX:
743
  case GNUTLS_FIPS140_LOG:
744
  case GNUTLS_FIPS140_DISABLED:
745
    break;
746
  case GNUTLS_FIPS140_SELFTESTS:
747
    _gnutls_audit_log(
748
      NULL,
749
      "Cannot switch library to FIPS140-2 self-tests mode; defaulting to strict\n");
750
    mode = GNUTLS_FIPS140_STRICT;
751
    break;
752
  default:
753
    _gnutls_audit_log(
754
      NULL,
755
      "Cannot switch library to mode %u; defaulting to strict\n",
756
      (unsigned)mode);
757
    mode = GNUTLS_FIPS140_STRICT;
758
    break;
759
  }
760
761
  if (flags & GNUTLS_FIPS140_SET_MODE_THREAD)
762
    _tfips_mode = mode;
763
  else {
764
    _global_fips_mode = mode;
765
    _tfips_mode = -1;
766
  }
767
#endif
768
0
}
769
770
void _gnutls_lib_simulate_error(void)
771
0
{
772
0
  _gnutls_switch_lib_state(LIB_STATE_ERROR);
773
0
}
774
775
void _gnutls_lib_force_operational(void)
776
0
{
777
0
  _gnutls_switch_lib_state(LIB_STATE_OPERATIONAL);
778
0
}
779
780
/**
781
 * gnutls_fips140_context_init:
782
 * @context: location to store @gnutls_fips140_context_t
783
 *
784
 * Create and initialize the FIPS context object.
785
 *
786
 * Returns: 0 upon success, a negative error code otherwise
787
 *
788
 * Since: 3.7.3
789
 */
790
int gnutls_fips140_context_init(gnutls_fips140_context_t *context)
791
0
{
792
0
  *context = gnutls_malloc(sizeof(struct gnutls_fips140_context_st));
793
0
  if (!*context) {
794
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
795
0
  }
796
0
  (*context)->state = GNUTLS_FIPS140_OP_INITIAL;
797
0
  return 0;
798
0
}
799
800
/**
801
 * gnutls_fips140_context_deinit:
802
 * @context: a #gnutls_fips140_context_t
803
 *
804
 * Uninitialize and release the FIPS context @context.
805
 *
806
 * Since: 3.7.3
807
 */
808
void gnutls_fips140_context_deinit(gnutls_fips140_context_t context)
809
0
{
810
0
  gnutls_free(context);
811
0
}
812
813
/**
814
 * gnutls_fips140_get_operation_state:
815
 * @context: a #gnutls_fips140_context_t
816
 *
817
 * Get the previous operation state of @context in terms of FIPS.
818
 *
819
 * Returns: a #gnutls_fips140_operation_state_t
820
 *
821
 * Since: 3.7.3
822
 */
823
gnutls_fips140_operation_state_t
824
gnutls_fips140_get_operation_state(gnutls_fips140_context_t context)
825
0
{
826
0
  return context->state;
827
0
}
828
829
/**
830
 * gnutls_fips140_push_context:
831
 * @context: a #gnutls_fips140_context_t
832
 *
833
 * Associate the FIPS @context to the current thread, diverting the
834
 * currently active context. If a cryptographic operation is ongoing
835
 * in the current thread, e.g., gnutls_aead_cipher_init() is called
836
 * but gnutls_aead_cipher_deinit() is not yet called, it returns an
837
 * error %GNUTLS_E_INVALID_REQUEST.
838
 *
839
 * The operation state of @context will be reset to
840
 * %GNUTLS_FIPS140_OP_INITIAL.
841
 *
842
 * This function is no-op if FIPS140 is not compiled in nor enabled
843
 * at run-time.
844
 *
845
 * Returns: 0 upon success, a negative error code otherwise
846
 *
847
 * Since: 3.7.3
848
 */
849
int gnutls_fips140_push_context(gnutls_fips140_context_t context)
850
0
{
851
#ifdef ENABLE_FIPS140
852
  if (_gnutls_fips_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
853
    context->next = _tfips_context;
854
    _tfips_context = context;
855
856
    context->state = GNUTLS_FIPS140_OP_INITIAL;
857
  }
858
  return 0;
859
#else
860
0
  return GNUTLS_E_INVALID_REQUEST;
861
0
#endif
862
0
}
863
864
/**
865
 * gnutls_fips140_pop_context:
866
 *
867
 * Dissociate the FIPS context currently
868
 * active on the current thread, reverting to the previously active
869
 * context. If a cryptographic operation is ongoing in the current
870
 * thread, e.g., gnutls_aead_cipher_init() is called but
871
 * gnutls_aead_cipher_deinit() is not yet called, it returns an error
872
 * %GNUTLS_E_INVALID_REQUEST.
873
 *
874
 * This function is no-op if FIPS140 is not compiled in nor enabled
875
 * at run-time.
876
 *
877
 * Returns: 0 upon success, a negative error code otherwise
878
 *
879
 * Since: 3.7.3
880
 */
881
int gnutls_fips140_pop_context(void)
882
0
{
883
#ifdef ENABLE_FIPS140
884
  if (_gnutls_fips_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
885
    if (!_tfips_context) {
886
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
887
    }
888
889
    _tfips_context = _tfips_context->next;
890
  }
891
  return 0;
892
#else
893
0
  return GNUTLS_E_INVALID_REQUEST;
894
0
#endif
895
0
}
896
897
#ifdef ENABLE_FIPS140
898
899
static inline const char *
900
operation_state_to_string(gnutls_fips140_operation_state_t state)
901
{
902
  switch (state) {
903
  case GNUTLS_FIPS140_OP_INITIAL:
904
    return "initial";
905
  case GNUTLS_FIPS140_OP_APPROVED:
906
    return "approved";
907
  case GNUTLS_FIPS140_OP_NOT_APPROVED:
908
    return "not-approved";
909
  case GNUTLS_FIPS140_OP_ERROR:
910
    return "error";
911
  default:
912
    /*NOTREACHED*/ assert(0);
913
    return NULL;
914
  }
915
}
916
917
void _gnutls_switch_fips_state(gnutls_fips140_operation_state_t state)
918
{
919
  gnutls_fips_mode_t mode = _gnutls_fips_mode_enabled();
920
  if (mode == GNUTLS_FIPS140_DISABLED) {
921
    return;
922
  }
923
924
  if (!_tfips_context) {
925
    _gnutls_debug_log("FIPS140-2 context is not set\n");
926
    return;
927
  }
928
929
  if (_tfips_context->state == state) {
930
    return;
931
  }
932
933
  switch (_tfips_context->state) {
934
  case GNUTLS_FIPS140_OP_INITIAL:
935
    /* initial can be transitioned to any state */
936
    if (mode != GNUTLS_FIPS140_LAX) {
937
      _gnutls_audit_log(
938
        NULL,
939
        "FIPS140-2 operation mode switched from initial to %s\n",
940
        operation_state_to_string(state));
941
    }
942
    _tfips_context->state = state;
943
    break;
944
  case GNUTLS_FIPS140_OP_APPROVED:
945
    /* approved can only be transitioned to not-approved */
946
    if (likely(state == GNUTLS_FIPS140_OP_NOT_APPROVED)) {
947
      if (mode != GNUTLS_FIPS140_LAX) {
948
        _gnutls_audit_log(
949
          NULL,
950
          "FIPS140-2 operation mode switched from approved to %s\n",
951
          operation_state_to_string(state));
952
      }
953
      _tfips_context->state = state;
954
      return;
955
    }
956
    FALLTHROUGH;
957
  default:
958
    /* other transitions are prohibited */
959
    if (mode != GNUTLS_FIPS140_LAX) {
960
      _gnutls_audit_log(
961
        NULL,
962
        "FIPS140-2 operation mode cannot be switched from %s to %s\n",
963
        operation_state_to_string(
964
          _tfips_context->state),
965
        operation_state_to_string(state));
966
    }
967
    break;
968
  }
969
}
970
971
#else
972
973
void _gnutls_switch_fips_state(gnutls_fips140_operation_state_t state)
974
0
{
975
0
  (void)state;
976
0
}
977
978
#endif
979
980
/**
981
 * gnutls_fips140_run_self_tests:
982
 *
983
 * Manually perform the second round of the FIPS140 self-tests,
984
 * including:
985
 *
986
 * - Known answer tests (KAT) for the selected set of symmetric
987
 *   cipher, MAC, public key, KDF, and DRBG
988
 * - Library integrity checks
989
 *
990
 * Upon failure with FIPS140 mode enabled, it makes the library
991
 * unusable.  This function is not thread-safe.
992
 *
993
 * Returns: 0 upon success, a negative error code otherwise
994
 *
995
 * Since: 3.7.7
996
 */
997
int gnutls_fips140_run_self_tests(void)
998
0
{
999
#ifdef ENABLE_FIPS140
1000
  int ret;
1001
  unsigned prev_lib_state;
1002
  gnutls_fips140_context_t fips_context = NULL;
1003
1004
  /* Save the FIPS context, because self tests change it */
1005
  if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
1006
    if (gnutls_fips140_context_init(&fips_context) < 0 ||
1007
        gnutls_fips140_push_context(fips_context) < 0) {
1008
      gnutls_fips140_context_deinit(fips_context);
1009
      fips_context = NULL;
1010
    }
1011
  }
1012
1013
  /* Temporarily switch to LIB_STATE_SELFTEST as some of the
1014
   * algorithms are implemented using special constructs in
1015
   * self-tests (such as deterministic variants) */
1016
  prev_lib_state = _gnutls_get_lib_state();
1017
  _gnutls_switch_lib_state(LIB_STATE_SELFTEST);
1018
1019
  ret = _gnutls_fips_perform_self_checks2();
1020
  if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED &&
1021
      ret < 0) {
1022
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
1023
    _gnutls_audit_log(NULL,
1024
          "FIPS140-2 self testing part 2 failed\n");
1025
  } else {
1026
    /* Restore the previous library state */
1027
    _gnutls_switch_lib_state(prev_lib_state);
1028
  }
1029
1030
  /* Restore the previous FIPS context */
1031
  if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED &&
1032
      fips_context) {
1033
    if (gnutls_fips140_pop_context() < 0) {
1034
      _gnutls_switch_lib_state(LIB_STATE_ERROR);
1035
      _gnutls_audit_log(
1036
        NULL, "FIPS140-2 context restoration failed\n");
1037
    }
1038
    gnutls_fips140_context_deinit(fips_context);
1039
  }
1040
  return ret;
1041
#else
1042
0
  return 0;
1043
0
#endif
1044
0
}