Coverage Report

Created: 2023-03-26 07:33

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