Coverage Report

Created: 2025-11-16 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/fips.c
Line
Count
Source
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
#define HMAC_SIZE 32
161
#define HMAC_ALGO GNUTLS_MAC_SHA256
162
#define HMAC_FORMAT_VERSION 1
163
164
struct hmac_entry {
165
  char path[GNUTLS_PATH_MAX];
166
  uint8_t hmac[HMAC_SIZE];
167
};
168
169
struct hmac_file {
170
  int version;
171
  struct hmac_entry gnutls;
172
#ifdef NETTLE_LIBRARY_SONAME
173
  struct hmac_entry nettle;
174
#endif
175
#ifdef HOGWEED_LIBRARY_SONAME
176
  struct hmac_entry hogweed;
177
#endif
178
#ifdef GMP_LIBRARY_SONAME
179
  struct hmac_entry gmp;
180
#endif
181
};
182
183
struct lib_paths {
184
  char gnutls[GNUTLS_PATH_MAX];
185
#ifdef NETTLE_LIBRARY_SONAME
186
  char nettle[GNUTLS_PATH_MAX];
187
#endif
188
#ifdef HOGWEED_LIBRARY_SONAME
189
  char hogweed[GNUTLS_PATH_MAX];
190
#endif
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
#ifdef NETTLE_LIBRARY_SONAME
254
  } else if (!strcmp(section, NETTLE_LIBRARY_SONAME)) {
255
    return lib_handler(&p->nettle, section, name, value);
256
#endif
257
#ifdef HOGWEED_LIBRARY_SONAME
258
  } else if (!strcmp(section, HOGWEED_LIBRARY_SONAME)) {
259
    return lib_handler(&p->hogweed, section, name, value);
260
#endif
261
#ifdef GMP_LIBRARY_SONAME
262
  } else if (!strcmp(section, GMP_LIBRARY_SONAME)) {
263
    return lib_handler(&p->gmp, section, name, value);
264
#endif
265
  } else {
266
    return 0;
267
  }
268
  return 1;
269
}
270
271
/*
272
 * get_hmac_path:
273
 * @mac_file: buffer where the hmac file path will be written to
274
 * @mac_file_size: size of the mac_file buffer
275
 * @gnutls_path: path to the gnutls library, used to deduce hmac file path
276
 * 
277
 * Deduces hmac file path from the gnutls library path.
278
 *
279
 * Returns: 0 on success, a negative error code otherwise
280
 */
281
static int get_hmac_path(char *mac_file, size_t mac_file_size,
282
       const char *gnutls_path)
283
{
284
  int ret;
285
  char *p;
286
287
  p = strrchr(gnutls_path, '/');
288
289
  if (p == NULL)
290
    ret = snprintf(mac_file, mac_file_size, ".%s.hmac",
291
             gnutls_path);
292
  else
293
    ret = snprintf(mac_file, mac_file_size, "%.*s/.%s.hmac",
294
             (int)(p - gnutls_path), gnutls_path, p + 1);
295
296
  if ((size_t)ret >= mac_file_size)
297
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
298
299
  ret = _gnutls_file_exists(mac_file);
300
  if (ret == 0)
301
    return GNUTLS_E_SUCCESS;
302
303
  if (p == NULL)
304
    ret = snprintf(mac_file, mac_file_size, "fipscheck/.%s.hmac",
305
             gnutls_path);
306
  else
307
    ret = snprintf(mac_file, mac_file_size,
308
             "%.*s/fipscheck/.%s.hmac",
309
             (int)(p - gnutls_path), gnutls_path, p + 1);
310
311
  if ((size_t)ret >= mac_file_size)
312
    return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
313
314
  ret = _gnutls_file_exists(mac_file);
315
  if (ret == 0)
316
    return GNUTLS_E_SUCCESS;
317
318
  return GNUTLS_E_FILE_ERROR;
319
}
320
321
/*
322
 * load_hmac_file:
323
 * @hmac_file: hmac file structure
324
 * @hmac_path: path to the hmac file
325
 *
326
 * Loads the hmac file into the hmac file structure.
327
 *
328
 * Returns: 0 on success, a negative error code otherwise
329
 */
330
static int load_hmac_file(struct hmac_file *hmac_file, const char *hmac_path)
331
{
332
  int ret;
333
  FILE *stream;
334
335
  stream = fopen(hmac_path, "r");
336
  if (stream == NULL)
337
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
338
339
  gnutls_memset(hmac_file, 0, sizeof(*hmac_file));
340
  ret = ini_parse_file(stream, handler, hmac_file);
341
  fclose(stream);
342
  if (ret < 0)
343
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
344
345
  if (hmac_file->version != HMAC_FORMAT_VERSION)
346
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
347
348
  return 0;
349
}
350
351
/*
352
 * check_lib_hmac:
353
 * @entry: hmac file entry
354
 * @path: path to the library which hmac should be compared
355
 *
356
 * Verify that HMAC from hmac file entry matches HMAC of given library.
357
 *
358
 * Returns: 0 on successful HMAC verification, a negative error code otherwise
359
 */
360
static int check_lib_hmac(struct hmac_entry *entry, const char *path)
361
{
362
  int ret;
363
  unsigned prev;
364
  uint8_t hmac[HMAC_SIZE];
365
  gnutls_datum_t data;
366
367
  _gnutls_debug_log("Loading: %s\n", path);
368
  ret = gnutls_load_file(path, &data);
369
  if (ret < 0) {
370
    _gnutls_debug_log("Could not load %s: %s\n", path,
371
          gnutls_strerror(ret));
372
    return gnutls_assert_val(ret);
373
  }
374
375
  prev = _gnutls_get_lib_state();
376
  _gnutls_switch_lib_state(LIB_STATE_OPERATIONAL);
377
  ret = gnutls_hmac_fast(HMAC_ALGO, FIPS_KEY, sizeof(FIPS_KEY) - 1,
378
             data.data, data.size, hmac);
379
  _gnutls_switch_lib_state(prev);
380
381
  gnutls_free(data.data);
382
  if (ret < 0) {
383
    _gnutls_debug_log("Could not calculate HMAC for %s: %s\n", path,
384
          gnutls_strerror(ret));
385
    return gnutls_assert_val(ret);
386
  }
387
388
  if (gnutls_memcmp(entry->hmac, hmac, HMAC_SIZE)) {
389
    _gnutls_debug_log("Calculated MAC for %s does not match\n",
390
          path);
391
    gnutls_memset(hmac, 0, HMAC_SIZE);
392
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
393
  }
394
  _gnutls_debug_log("Successfully verified MAC for %s\n", path);
395
396
  gnutls_memset(hmac, 0, HMAC_SIZE);
397
  return 0;
398
}
399
400
#ifdef HAVE_DL_ITERATE_PHDR
401
402
static int callback(struct dl_phdr_info *info, size_t size, void *data)
403
{
404
  const char *path = info->dlpi_name;
405
  const char *soname = last_component(path);
406
  struct lib_paths *paths = (struct lib_paths *)data;
407
408
  if (!strcmp(soname, GNUTLS_LIBRARY_SONAME))
409
    _gnutls_str_cpy(paths->gnutls, GNUTLS_PATH_MAX, path);
410
#ifdef NETTLE_LIBRARY_SONAME
411
  else if (!strcmp(soname, NETTLE_LIBRARY_SONAME))
412
    _gnutls_str_cpy(paths->nettle, GNUTLS_PATH_MAX, path);
413
#endif
414
#ifdef HOGWEED_LIBRARY_SONAME
415
  else if (!strcmp(soname, HOGWEED_LIBRARY_SONAME))
416
    _gnutls_str_cpy(paths->hogweed, GNUTLS_PATH_MAX, path);
417
#endif
418
#ifdef GMP_LIBRARY_SONAME
419
  else if (!strcmp(soname, GMP_LIBRARY_SONAME))
420
    _gnutls_str_cpy(paths->gmp, GNUTLS_PATH_MAX, path);
421
#endif
422
  return 0;
423
}
424
425
static int load_lib_paths(struct lib_paths *paths)
426
{
427
  memset(paths, 0, sizeof(*paths));
428
  dl_iterate_phdr(callback, paths);
429
430
  if (paths->gnutls[0] == '\0') {
431
    _gnutls_debug_log("Gnutls library path was not found\n");
432
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
433
  }
434
#ifdef NETTLE_LIBRARY_SONAME
435
  if (paths->nettle[0] == '\0') {
436
    _gnutls_debug_log("Nettle library path was not found\n");
437
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
438
  }
439
#endif
440
#ifdef HOGWEED_LIBRARY_SONAME
441
  if (paths->hogweed[0] == '\0') {
442
    _gnutls_debug_log("Hogweed library path was not found\n");
443
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
444
  }
445
#endif
446
#ifdef GMP_LIBRARY_SONAME
447
  if (paths->gmp[0] == '\0') {
448
    _gnutls_debug_log("Gmp library path was not found\n");
449
    return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
450
  }
451
#endif
452
453
  return GNUTLS_E_SUCCESS;
454
}
455
456
#else
457
458
static int load_lib_paths(struct lib_paths *paths)
459
{
460
  (void)paths;
461
  _gnutls_debug_log("Function dl_iterate_phdr is missing\n");
462
  return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
463
}
464
465
#endif /* HAVE_DL_ITERATE_PHDR */
466
467
static int check_binary_integrity(void)
468
{
469
  int ret;
470
  struct lib_paths paths;
471
  struct hmac_file hmac;
472
  char hmac_path[GNUTLS_PATH_MAX];
473
474
  ret = load_lib_paths(&paths);
475
  if (ret < 0) {
476
    _gnutls_debug_log("Could not load library paths: %s\n",
477
          gnutls_strerror(ret));
478
    return ret;
479
  }
480
481
  ret = get_hmac_path(hmac_path, sizeof(hmac_path), paths.gnutls);
482
  if (ret < 0) {
483
    _gnutls_debug_log("Could not get hmac file path: %s\n",
484
          gnutls_strerror(ret));
485
    return ret;
486
  }
487
488
  ret = load_hmac_file(&hmac, hmac_path);
489
  if (ret < 0) {
490
    _gnutls_debug_log("Could not load hmac file: %s\n",
491
          gnutls_strerror(ret));
492
    return ret;
493
  }
494
495
  ret = check_lib_hmac(&hmac.gnutls, paths.gnutls);
496
  if (ret < 0)
497
    return ret;
498
#ifdef NETTLE_LIBRARY_SONAME
499
  ret = check_lib_hmac(&hmac.nettle, paths.nettle);
500
  if (ret < 0)
501
    return ret;
502
#endif
503
#ifdef HOGWEED_LIBRARY_SONAME
504
  ret = check_lib_hmac(&hmac.hogweed, paths.hogweed);
505
  if (ret < 0)
506
    return ret;
507
#endif
508
#ifdef GMP_LIBRARY_SONAME
509
  ret = check_lib_hmac(&hmac.gmp, paths.gmp);
510
  if (ret < 0)
511
    return ret;
512
#endif
513
514
  return 0;
515
}
516
517
int _gnutls_fips_perform_self_checks1(void)
518
{
519
  int ret;
520
521
  /* Tests the FIPS algorithms used by nettle internally.
522
   * In our case we test AES-CBC since nettle's AES is used by
523
   * the DRBG-AES.
524
   */
525
526
  /* ciphers - one test per cipher */
527
  ret = gnutls_cipher_self_test(GNUTLS_SELF_TEST_FLAG_NO_COMPAT,
528
              GNUTLS_CIPHER_AES_128_CBC);
529
  if (ret < 0) {
530
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
531
  }
532
533
  return 0;
534
}
535
536
int _gnutls_fips_perform_self_checks2(void)
537
{
538
  int ret;
539
540
  /* Tests the FIPS algorithms */
541
542
  /* ciphers - one test per cipher */
543
  ret = gnutls_cipher_self_test(GNUTLS_SELF_TEST_FLAG_NO_COMPAT,
544
              GNUTLS_CIPHER_AES_256_GCM);
545
  if (ret < 0) {
546
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
547
  }
548
549
  /* Digest tests */
550
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_224);
551
  if (ret < 0) {
552
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
553
  }
554
555
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_256);
556
  if (ret < 0) {
557
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
558
  }
559
560
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_384);
561
  if (ret < 0) {
562
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
563
  }
564
565
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHA3_512);
566
  if (ret < 0) {
567
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
568
  }
569
570
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHAKE_128);
571
  if (ret < 0) {
572
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
573
  }
574
575
  ret = gnutls_digest_self_test(0, GNUTLS_DIG_SHAKE_256);
576
  if (ret < 0) {
577
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
578
  }
579
580
  /* MAC (includes message digest test) */
581
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA1);
582
  if (ret < 0) {
583
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
584
  }
585
586
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA224);
587
  if (ret < 0) {
588
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
589
  }
590
591
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA256);
592
  if (ret < 0) {
593
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
594
  }
595
596
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA384);
597
  if (ret < 0) {
598
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
599
  }
600
601
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_SHA512);
602
  if (ret < 0) {
603
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
604
  }
605
606
  ret = gnutls_mac_self_test(0, GNUTLS_MAC_AES_CMAC_256);
607
  if (ret < 0) {
608
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
609
  }
610
611
  /* PK */
612
  ret = gnutls_pk_self_test(0, GNUTLS_PK_RSA_PSS);
613
  if (ret < 0) {
614
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
615
  }
616
617
  ret = gnutls_pk_self_test(0, GNUTLS_PK_EC);
618
  if (ret < 0) {
619
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
620
  }
621
622
  ret = gnutls_pk_self_test(0, GNUTLS_PK_EDDSA_ED25519);
623
  if (ret < 0) {
624
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
625
  }
626
627
  ret = gnutls_pk_self_test(0, GNUTLS_PK_EDDSA_ED448);
628
  if (ret < 0) {
629
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
630
  }
631
632
  ret = gnutls_pk_self_test(0, GNUTLS_PK_DH);
633
  if (ret < 0) {
634
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
635
  }
636
637
  /* HKDF */
638
  ret = gnutls_hkdf_self_test(0, GNUTLS_MAC_SHA256);
639
  if (ret < 0) {
640
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
641
  }
642
643
  /* PBKDF2 */
644
  ret = gnutls_pbkdf2_self_test(0, GNUTLS_MAC_SHA256);
645
  if (ret < 0) {
646
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
647
  }
648
649
  /* TLS-PRF */
650
  ret = gnutls_tlsprf_self_test(0, GNUTLS_MAC_SHA256);
651
  if (ret < 0) {
652
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
653
  }
654
655
  if (_gnutls_rnd_ops.self_test == NULL) {
656
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
657
  }
658
659
  /* this does not require rng initialization */
660
  ret = _gnutls_rnd_ops.self_test();
661
  if (ret < 0) {
662
    return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
663
  }
664
665
  if (_skip_integrity_checks == 0) {
666
    ret = check_binary_integrity();
667
    if (ret < 0) {
668
      return gnutls_assert_val(GNUTLS_E_SELF_TEST_ERROR);
669
    }
670
  }
671
672
  return 0;
673
}
674
#endif
675
676
/**
677
 * gnutls_fips140_mode_enabled:
678
 *
679
 * Checks whether this library is in FIPS140 mode. The returned
680
 * value corresponds to the library mode as set with
681
 * gnutls_fips140_set_mode().
682
 *
683
 * If gnutls_fips140_set_mode() was called with %GNUTLS_FIPS140_SET_MODE_THREAD
684
 * then this function will return the current thread's FIPS140 mode, otherwise
685
 * the global value is returned.
686
 *
687
 * Returns: return non-zero if true or zero if false.
688
 *
689
 * Since: 3.3.0
690
 **/
691
unsigned gnutls_fips140_mode_enabled(void)
692
0
{
693
#ifdef ENABLE_FIPS140
694
  unsigned ret = _gnutls_fips_mode_enabled();
695
696
  if (ret > GNUTLS_FIPS140_DISABLED) {
697
    /* If the previous run of selftests has failed, return as if
698
     * the FIPS mode is disabled. We could use HAVE_LIB_ERROR, if
699
     * we can assume that all the selftests run atomically from
700
     * the ELF constructor.
701
     */
702
    if (_gnutls_get_lib_state() == LIB_STATE_ERROR)
703
      return 0;
704
705
    return ret;
706
  }
707
#endif
708
0
  return 0;
709
0
}
710
711
/**
712
 * gnutls_fips140_set_mode:
713
 * @mode: the FIPS140-2 mode to switch to
714
 * @flags: should be zero or %GNUTLS_FIPS140_SET_MODE_THREAD
715
 *
716
 * That function is not thread-safe when changing the mode with no flags
717
 * (globally), and should be called prior to creating any threads. Its
718
 * behavior with no flags after threads are created is undefined.
719
 *
720
 * When the flag %GNUTLS_FIPS140_SET_MODE_THREAD is specified
721
 * then this call will change the FIPS140-2 mode for this particular
722
 * thread and not for the whole process. That way an application
723
 * can utilize this function to set and reset mode for specific
724
 * operations.
725
 *
726
 * This function never fails but will be a no-op if used when
727
 * the library is not in FIPS140-2 mode. When asked to switch to unknown
728
 * values for @mode or to %GNUTLS_FIPS140_SELFTESTS mode, the library
729
 * switches to %GNUTLS_FIPS140_STRICT mode.
730
 *
731
 * Since: 3.6.2
732
 **/
733
void gnutls_fips140_set_mode(gnutls_fips_mode_t mode, unsigned flags)
734
0
{
735
#ifdef ENABLE_FIPS140
736
  gnutls_fips_mode_t prev = _gnutls_fips_mode_enabled();
737
  if (prev == GNUTLS_FIPS140_DISABLED ||
738
      prev == GNUTLS_FIPS140_SELFTESTS) {
739
    /* we need to run self-tests first to be in FIPS140-2 mode */
740
    _gnutls_audit_log(
741
      NULL,
742
      "The library should be initialized in FIPS140-2 mode to do that operation\n");
743
    return;
744
  }
745
746
  switch (mode) {
747
  case GNUTLS_FIPS140_STRICT:
748
  case GNUTLS_FIPS140_LAX:
749
  case GNUTLS_FIPS140_LOG:
750
  case GNUTLS_FIPS140_DISABLED:
751
    break;
752
  case GNUTLS_FIPS140_SELFTESTS:
753
    _gnutls_audit_log(
754
      NULL,
755
      "Cannot switch library to FIPS140-2 self-tests mode; defaulting to strict\n");
756
    mode = GNUTLS_FIPS140_STRICT;
757
    break;
758
  default:
759
    _gnutls_audit_log(
760
      NULL,
761
      "Cannot switch library to mode %u; defaulting to strict\n",
762
      (unsigned)mode);
763
    mode = GNUTLS_FIPS140_STRICT;
764
    break;
765
  }
766
767
  if (flags & GNUTLS_FIPS140_SET_MODE_THREAD)
768
    _tfips_mode = mode;
769
  else {
770
    _global_fips_mode = mode;
771
    _tfips_mode = -1;
772
  }
773
#endif
774
0
}
775
776
void _gnutls_lib_simulate_error(void)
777
0
{
778
0
  _gnutls_switch_lib_state(LIB_STATE_ERROR);
779
0
}
780
781
void _gnutls_lib_force_operational(void)
782
0
{
783
0
  _gnutls_switch_lib_state(LIB_STATE_OPERATIONAL);
784
0
}
785
786
/**
787
 * gnutls_fips140_context_init:
788
 * @context: location to store @gnutls_fips140_context_t
789
 *
790
 * Create and initialize the FIPS context object.
791
 *
792
 * Returns: 0 upon success, a negative error code otherwise
793
 *
794
 * Since: 3.7.3
795
 */
796
int gnutls_fips140_context_init(gnutls_fips140_context_t *context)
797
0
{
798
0
  *context = gnutls_malloc(sizeof(struct gnutls_fips140_context_st));
799
0
  if (!*context) {
800
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
801
0
  }
802
0
  (*context)->state = GNUTLS_FIPS140_OP_INITIAL;
803
0
  return 0;
804
0
}
805
806
/**
807
 * gnutls_fips140_context_deinit:
808
 * @context: a #gnutls_fips140_context_t
809
 *
810
 * Uninitialize and release the FIPS context @context.
811
 *
812
 * Since: 3.7.3
813
 */
814
void gnutls_fips140_context_deinit(gnutls_fips140_context_t context)
815
0
{
816
0
  gnutls_free(context);
817
0
}
818
819
/**
820
 * gnutls_fips140_get_operation_state:
821
 * @context: a #gnutls_fips140_context_t
822
 *
823
 * Get the previous operation state of @context in terms of FIPS.
824
 *
825
 * Returns: a #gnutls_fips140_operation_state_t
826
 *
827
 * Since: 3.7.3
828
 */
829
gnutls_fips140_operation_state_t
830
gnutls_fips140_get_operation_state(gnutls_fips140_context_t context)
831
0
{
832
0
  return context->state;
833
0
}
834
835
/**
836
 * gnutls_fips140_push_context:
837
 * @context: a #gnutls_fips140_context_t
838
 *
839
 * Associate the FIPS @context to the current thread, diverting the
840
 * currently active context. If a cryptographic operation is ongoing
841
 * in the current thread, e.g., gnutls_aead_cipher_init() is called
842
 * but gnutls_aead_cipher_deinit() is not yet called, it returns an
843
 * error %GNUTLS_E_INVALID_REQUEST.
844
 *
845
 * The operation state of @context will be reset to
846
 * %GNUTLS_FIPS140_OP_INITIAL.
847
 *
848
 * This function is no-op if FIPS140 is not compiled in nor enabled
849
 * at run-time.
850
 *
851
 * Returns: 0 upon success, a negative error code otherwise
852
 *
853
 * Since: 3.7.3
854
 */
855
int gnutls_fips140_push_context(gnutls_fips140_context_t context)
856
0
{
857
#ifdef ENABLE_FIPS140
858
  if (_gnutls_fips_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
859
    context->next = _tfips_context;
860
    _tfips_context = context;
861
862
    context->state = GNUTLS_FIPS140_OP_INITIAL;
863
  }
864
  return 0;
865
#else
866
0
  return GNUTLS_E_INVALID_REQUEST;
867
0
#endif
868
0
}
869
870
/**
871
 * gnutls_fips140_pop_context:
872
 *
873
 * Dissociate the FIPS context currently
874
 * active on the current thread, reverting to the previously active
875
 * context. If a cryptographic operation is ongoing in the current
876
 * thread, e.g., gnutls_aead_cipher_init() is called but
877
 * gnutls_aead_cipher_deinit() is not yet called, it returns an error
878
 * %GNUTLS_E_INVALID_REQUEST.
879
 *
880
 * This function is no-op if FIPS140 is not compiled in nor enabled
881
 * at run-time.
882
 *
883
 * Returns: 0 upon success, a negative error code otherwise
884
 *
885
 * Since: 3.7.3
886
 */
887
int gnutls_fips140_pop_context(void)
888
0
{
889
#ifdef ENABLE_FIPS140
890
  if (_gnutls_fips_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
891
    if (!_tfips_context) {
892
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
893
    }
894
895
    _tfips_context = _tfips_context->next;
896
  }
897
  return 0;
898
#else
899
0
  return GNUTLS_E_INVALID_REQUEST;
900
0
#endif
901
0
}
902
903
#ifdef ENABLE_FIPS140
904
905
static inline const char *
906
operation_state_to_string(gnutls_fips140_operation_state_t state)
907
{
908
  switch (state) {
909
  case GNUTLS_FIPS140_OP_INITIAL:
910
    return "initial";
911
  case GNUTLS_FIPS140_OP_APPROVED:
912
    return "approved";
913
  case GNUTLS_FIPS140_OP_NOT_APPROVED:
914
    return "not-approved";
915
  case GNUTLS_FIPS140_OP_ERROR:
916
    return "error";
917
  default:
918
    /*NOTREACHED*/ assert(0);
919
    return NULL;
920
  }
921
}
922
923
void _gnutls_switch_fips_state(gnutls_fips140_operation_state_t state)
924
{
925
  gnutls_fips_mode_t mode = _gnutls_fips_mode_enabled();
926
  if (mode == GNUTLS_FIPS140_DISABLED) {
927
    return;
928
  }
929
930
  if (!_tfips_context) {
931
    _gnutls_debug_log("FIPS140-2 context is not set\n");
932
    return;
933
  }
934
935
  if (_tfips_context->state == state) {
936
    return;
937
  }
938
939
  switch (_tfips_context->state) {
940
  case GNUTLS_FIPS140_OP_INITIAL:
941
    /* initial can be transitioned to any state */
942
    if (mode != GNUTLS_FIPS140_LAX) {
943
      _gnutls_audit_log(
944
        NULL,
945
        "FIPS140-2 operation mode switched from initial to %s\n",
946
        operation_state_to_string(state));
947
    }
948
    _tfips_context->state = state;
949
    break;
950
  case GNUTLS_FIPS140_OP_APPROVED:
951
    /* approved can only be transitioned to not-approved */
952
    if (likely(state == GNUTLS_FIPS140_OP_NOT_APPROVED)) {
953
      if (mode != GNUTLS_FIPS140_LAX) {
954
        _gnutls_audit_log(
955
          NULL,
956
          "FIPS140-2 operation mode switched from approved to %s\n",
957
          operation_state_to_string(state));
958
      }
959
      _tfips_context->state = state;
960
      return;
961
    }
962
    FALLTHROUGH;
963
  default:
964
    /* other transitions are prohibited */
965
    if (mode != GNUTLS_FIPS140_LAX) {
966
      _gnutls_audit_log(
967
        NULL,
968
        "FIPS140-2 operation mode cannot be switched from %s to %s\n",
969
        operation_state_to_string(
970
          _tfips_context->state),
971
        operation_state_to_string(state));
972
    }
973
    break;
974
  }
975
}
976
977
#else
978
979
void _gnutls_switch_fips_state(gnutls_fips140_operation_state_t state)
980
0
{
981
0
  (void)state;
982
0
}
983
984
#endif
985
986
/**
987
 * gnutls_fips140_run_self_tests:
988
 *
989
 * Manually perform the second round of the FIPS140 self-tests,
990
 * including:
991
 *
992
 * - Known answer tests (KAT) for the selected set of symmetric
993
 *   cipher, MAC, public key, KDF, and DRBG
994
 * - Library integrity checks
995
 *
996
 * Upon failure with FIPS140 mode enabled, it makes the library
997
 * unusable.  This function is not thread-safe.
998
 *
999
 * Returns: 0 upon success, a negative error code otherwise
1000
 *
1001
 * Since: 3.7.7
1002
 */
1003
int gnutls_fips140_run_self_tests(void)
1004
0
{
1005
#ifdef ENABLE_FIPS140
1006
  int ret;
1007
  unsigned prev_lib_state;
1008
  gnutls_fips140_context_t fips_context = NULL;
1009
1010
  /* Save the FIPS context, because self tests change it */
1011
  if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED) {
1012
    if (gnutls_fips140_context_init(&fips_context) < 0 ||
1013
        gnutls_fips140_push_context(fips_context) < 0) {
1014
      gnutls_fips140_context_deinit(fips_context);
1015
      fips_context = NULL;
1016
    }
1017
  }
1018
1019
  /* Temporarily switch to LIB_STATE_SELFTEST as some of the
1020
   * algorithms are implemented using special constructs in
1021
   * self-tests (such as deterministic variants) */
1022
  prev_lib_state = _gnutls_get_lib_state();
1023
  _gnutls_switch_lib_state(LIB_STATE_SELFTEST);
1024
1025
  ret = _gnutls_fips_perform_self_checks2();
1026
  if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED &&
1027
      ret < 0) {
1028
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
1029
    _gnutls_audit_log(NULL,
1030
          "FIPS140-2 self testing part 2 failed\n");
1031
  } else {
1032
    /* Restore the previous library state */
1033
    _gnutls_switch_lib_state(prev_lib_state);
1034
  }
1035
1036
  /* Restore the previous FIPS context */
1037
  if (gnutls_fips140_mode_enabled() != GNUTLS_FIPS140_DISABLED &&
1038
      fips_context) {
1039
    if (gnutls_fips140_pop_context() < 0) {
1040
      _gnutls_switch_lib_state(LIB_STATE_ERROR);
1041
      _gnutls_audit_log(
1042
        NULL, "FIPS140-2 context restoration failed\n");
1043
    }
1044
    gnutls_fips140_context_deinit(fips_context);
1045
  }
1046
  return ret;
1047
#else
1048
0
  return 0;
1049
0
#endif
1050
0
}