Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/global.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2016 Free Software Foundation, Inc.
3
 * Copyright (C) 2015-2016 Red Hat, Inc.
4
 *
5
 * Author: Nikos Mavrogiannopoulos
6
 *
7
 * This file is part of GnuTLS.
8
 *
9
 * The GnuTLS is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; either version 2.1 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
#include "gnutls_int.h"
25
#include "errors.h"
26
#include <libtasn1.h>
27
#include "dh.h"
28
#include "random.h"
29
#include <gnutls/pkcs11.h>
30
31
#include "hello_ext.h" /* for _gnutls_hello_ext_init */
32
#include "supplemental.h" /* for _gnutls_supplemental_deinit */
33
#include "locks.h"
34
#include "system.h"
35
#include "accelerated/cryptodev.h"
36
#include "accelerated/afalg.h"
37
#include "accelerated/accelerated.h"
38
#include "fips.h"
39
#include "atfork.h"
40
#include "system-keys.h"
41
#include "str.h"
42
#include "global.h"
43
44
/* Minimum library versions we accept. */
45
2
#define GNUTLS_MIN_LIBTASN1_VERSION "0.3.4"
46
47
#ifdef __sun
48
#pragma fini(lib_deinit)
49
#pragma init(lib_init)
50
#define _CONSTRUCTOR
51
#define _DESTRUCTOR
52
#else
53
#define _CONSTRUCTOR __attribute__((constructor))
54
#define _DESTRUCTOR __attribute__((destructor))
55
#endif
56
57
#ifndef _WIN32
58
int __attribute__((weak)) _gnutls_global_init_skip(void);
59
int _gnutls_global_init_skip(void)
60
2
{
61
2
  return 0;
62
2
}
63
#else
64
inline static int _gnutls_global_init_skip(void)
65
{
66
  return 0;
67
}
68
#endif
69
70
/* created by asn1c */
71
extern const asn1_static_node gnutls_asn1_tab[];
72
extern const asn1_static_node pkix_asn1_tab[];
73
74
asn1_node _gnutls_pkix1_asn = NULL;
75
asn1_node _gnutls_gnutls_asn = NULL;
76
77
gnutls_log_func _gnutls_log_func = NULL;
78
gnutls_audit_log_func _gnutls_audit_log_func = NULL;
79
int _gnutls_log_level = 0; /* default log level */
80
81
unsigned int _gnutls_global_version = GNUTLS_VERSION_NUMBER;
82
83
static int _gnutls_global_init(unsigned constructor);
84
static void _gnutls_global_deinit(unsigned destructor);
85
86
static void default_log_func(int level, const char *str)
87
0
{
88
0
  fprintf(stderr, "gnutls[%d]: %s", level, str);
89
0
}
90
91
/**
92
 * gnutls_global_set_log_function:
93
 * @log_func: it's a log function
94
 *
95
 * This is the function where you set the logging function gnutls is
96
 * going to use.  This function only accepts a character array.
97
 * Normally you may not use this function since it is only used for
98
 * debugging purposes.
99
 *
100
 * @gnutls_log_func is of the form,
101
 * void (*gnutls_log_func)( int level, const char*);
102
 **/
103
void gnutls_global_set_log_function(gnutls_log_func log_func)
104
0
{
105
0
  _gnutls_log_func = log_func;
106
0
}
107
108
/**
109
 * gnutls_global_set_audit_log_function:
110
 * @log_func: it is the audit log function
111
 *
112
 * This is the function to set the audit logging function. This
113
 * is a function to report important issues, such as possible
114
 * attacks in the protocol. This is different from gnutls_global_set_log_function()
115
 * because it will report also session-specific events. The session
116
 * parameter will be null if there is no corresponding TLS session.
117
 *
118
 * @gnutls_audit_log_func is of the form,
119
 * void (*gnutls_audit_log_func)( gnutls_session_t, const char*);
120
 *
121
 * Since: 3.0
122
 **/
123
void gnutls_global_set_audit_log_function(gnutls_audit_log_func log_func)
124
0
{
125
0
  _gnutls_audit_log_func = log_func;
126
0
}
127
128
static void gettime_from_time(struct timespec *t)
129
0
{
130
0
  t->tv_sec = gnutls_time(NULL);
131
0
  t->tv_nsec = 0;
132
0
}
133
134
/**
135
 * gnutls_global_set_time_function:
136
 * @time_func: it's the system time function, a gnutls_time_func() callback.
137
 *
138
 * This is the function where you can override the default system time
139
 * function.  The application provided function should behave the same
140
 * as the standard function.
141
 *
142
 * Since: 2.12.0
143
 **/
144
void gnutls_global_set_time_function(gnutls_time_func time_func)
145
0
{
146
0
  gnutls_time = time_func;
147
148
  /* When the time function is overridden, also override the
149
   * gettime function to use the derived value, even if its
150
   * resolution is lower.
151
   */
152
0
  _gnutls_global_set_gettime_function(gettime_from_time);
153
0
}
154
155
/**
156
 * gnutls_global_set_log_level:
157
 * @level: it's an integer from 0 to 99.
158
 *
159
 * This is the function that allows you to set the log level.  The
160
 * level is an integer between 0 and 9.  Higher values mean more
161
 * verbosity. The default value is 0.  Larger values should only be
162
 * used with care, since they may reveal sensitive information.
163
 *
164
 * Use a log level over 10 to enable all debugging options.
165
 **/
166
void gnutls_global_set_log_level(int level)
167
0
{
168
0
  _gnutls_log_level = level;
169
0
}
170
171
/**
172
 * gnutls_global_set_mem_functions:
173
 * @alloc_func: it's the default memory allocation function. Like malloc().
174
 * @secure_alloc_func: This is the memory allocation function that will be used for sensitive data.
175
 * @is_secure_func: a function that returns 0 if the memory given is not secure. May be NULL.
176
 * @realloc_func: A realloc function
177
 * @free_func: The function that frees allocated data. Must accept a NULL pointer.
178
 *
179
 * Deprecated: since 3.3.0 it is no longer possible to replace the internally used 
180
 *  memory allocation functions
181
 *
182
 * This is the function where you set the memory allocation functions
183
 * gnutls is going to use. By default the libc's allocation functions
184
 * (malloc(), free()), are used by gnutls, to allocate both sensitive
185
 * and not sensitive data.  This function is provided to set the
186
 * memory allocation functions to something other than the defaults
187
 *
188
 * This function must be called before gnutls_global_init() is called.
189
 * This function is not thread safe.
190
 **/
191
void gnutls_global_set_mem_functions(gnutls_alloc_function alloc_func,
192
             gnutls_alloc_function secure_alloc_func,
193
             gnutls_is_secure_function is_secure_func,
194
             gnutls_realloc_function realloc_func,
195
             gnutls_free_function free_func)
196
0
{
197
0
  _gnutls_debug_log(
198
0
    "called the deprecated gnutls_global_set_mem_functions()\n");
199
0
}
200
201
GNUTLS_STATIC_MUTEX(global_init_mutex);
202
static int _gnutls_init = 0;
203
204
/* cache the return code */
205
static int _gnutls_init_ret = 0;
206
207
/**
208
 * gnutls_global_init:
209
 *
210
 * Since GnuTLS 3.3.0 this function is no longer necessary to be explicitly
211
 * called. To disable the implicit call (in a library constructor) of this
212
 * function set the environment variable %GNUTLS_NO_IMPLICIT_INIT to 1.
213
 *
214
 * This function performs any required precalculations, detects
215
 * the supported CPU capabilities and initializes the underlying
216
 * cryptographic backend. In order to free any resources 
217
 * taken by this call you should gnutls_global_deinit() 
218
 * when gnutls usage is no longer needed.
219
 *
220
 * This function increments a global counter, so that
221
 * gnutls_global_deinit() only releases resources when it has been
222
 * called as many times as gnutls_global_init().  This is useful when
223
 * GnuTLS is used by more than one library in an application.  This
224
 * function can be called many times, but will only do something the
225
 * first time. It is thread safe since GnuTLS 3.3.0.
226
 *
227
 * A subsequent call of this function if the initial has failed will
228
 * return the same error code.
229
 *
230
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
231
 *   otherwise a negative error code is returned.
232
 **/
233
int gnutls_global_init(void)
234
0
{
235
0
  return _gnutls_global_init(0);
236
0
}
237
238
static int _gnutls_global_init(unsigned constructor)
239
2
{
240
2
  int ret = 0, res;
241
2
  int level;
242
2
  const char *e;
243
244
2
  if (!constructor) {
245
0
    ret = gnutls_static_mutex_lock(&global_init_mutex);
246
0
    if (ret < 0) {
247
0
      return gnutls_assert_val(ret);
248
0
    }
249
0
  }
250
251
2
  _gnutls_init++;
252
2
  if (_gnutls_init > 1) {
253
0
    ret = _gnutls_init_ret;
254
0
    goto out;
255
0
  }
256
257
2
  _gnutls_switch_lib_state(LIB_STATE_INIT);
258
259
2
  e = secure_getenv("GNUTLS_DEBUG_LEVEL");
260
2
  if (e != NULL) {
261
0
    level = atoi(e);
262
0
    gnutls_global_set_log_level(level);
263
0
    if (_gnutls_log_func == NULL)
264
0
      gnutls_global_set_log_function(default_log_func);
265
0
    _gnutls_debug_log("Enabled GnuTLS " VERSION " logging...\n");
266
0
  }
267
268
2
#ifdef HAVE_DCGETTEXT
269
2
  bindtextdomain(PACKAGE, LOCALEDIR);
270
2
#endif
271
272
2
  res = gnutls_crypto_init();
273
2
  if (res != 0) {
274
0
    gnutls_assert();
275
0
    ret = GNUTLS_E_CRYPTO_INIT_FAILED;
276
0
    goto out;
277
0
  }
278
279
2
  ret = _gnutls_system_key_init();
280
2
  if (ret != 0) {
281
0
    gnutls_assert();
282
0
  }
283
284
  /* initialize ASN.1 parser
285
   */
286
2
  if (asn1_check_version(GNUTLS_MIN_LIBTASN1_VERSION) == NULL) {
287
0
    gnutls_assert();
288
0
    _gnutls_debug_log("Checking for libtasn1 failed: %s < %s\n",
289
0
          asn1_check_version(NULL),
290
0
          GNUTLS_MIN_LIBTASN1_VERSION);
291
0
    ret = GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY;
292
0
    goto out;
293
0
  }
294
295
2
  _gnutls_pkix1_asn = NULL;
296
2
  res = asn1_array2tree(pkix_asn1_tab, &_gnutls_pkix1_asn, NULL);
297
2
  if (res != ASN1_SUCCESS) {
298
0
    gnutls_assert();
299
0
    ret = _gnutls_asn2err(res);
300
0
    goto out;
301
0
  }
302
303
2
  res = asn1_array2tree(gnutls_asn1_tab, &_gnutls_gnutls_asn, NULL);
304
2
  if (res != ASN1_SUCCESS) {
305
0
    gnutls_assert();
306
0
    ret = _gnutls_asn2err(res);
307
0
    goto out;
308
0
  }
309
310
  /* Initialize the random generator */
311
2
  ret = _gnutls_rnd_preinit();
312
2
  if (ret < 0) {
313
0
    gnutls_assert();
314
0
    goto out;
315
0
  }
316
317
  /* Initialize the default TLS extensions */
318
2
  ret = _gnutls_hello_ext_init();
319
2
  if (ret < 0) {
320
0
    gnutls_assert();
321
0
    goto out;
322
0
  }
323
324
2
  ret = gnutls_system_global_init();
325
2
  if (ret < 0) {
326
0
    gnutls_assert();
327
0
    goto out;
328
0
  }
329
330
2
#ifndef _WIN32
331
2
  ret = _gnutls_register_fork_handler();
332
2
  if (ret < 0) {
333
0
    gnutls_assert();
334
0
    goto out;
335
0
  }
336
2
#endif
337
338
#ifdef ENABLE_FIPS140
339
  res = _gnutls_fips_mode_enabled();
340
  /* res == 1 -> fips140-2 mode enabled
341
   * res == 2 -> only self checks performed - but no failure
342
   * res == not in fips140 mode
343
   */
344
  if (res != 0) {
345
    _gnutls_debug_log("FIPS140-2 mode: %d\n", res);
346
    _gnutls_priority_update_fips();
347
348
    /* first round of self checks, these are done on the
349
     * nettle algorithms which are used internally */
350
    _gnutls_switch_lib_state(LIB_STATE_SELFTEST);
351
    ret = _gnutls_fips_perform_self_checks1();
352
    if (ret < 0) {
353
      _gnutls_switch_lib_state(LIB_STATE_ERROR);
354
      _gnutls_audit_log(
355
        NULL, "FIPS140-2 self testing part1 failed\n");
356
      if (res != 2) {
357
        gnutls_assert();
358
        goto out;
359
      }
360
    }
361
  }
362
#endif
363
364
2
  _gnutls_register_accel_crypto();
365
2
  _gnutls_cryptodev_init();
366
2
  _gnutls_afalg_init();
367
368
#ifdef ENABLE_FIPS140
369
  /* These self tests are performed on the overridden algorithms
370
   * (e.g., AESNI overridden AES). They are after _gnutls_register_accel_crypto()
371
   * intentionally */
372
  if (res != 0) {
373
    _gnutls_switch_lib_state(LIB_STATE_SELFTEST);
374
    ret = _gnutls_fips_perform_self_checks2();
375
    if (ret < 0) {
376
      _gnutls_switch_lib_state(LIB_STATE_ERROR);
377
      _gnutls_audit_log(
378
        NULL, "FIPS140-2 self testing part 2 failed\n");
379
      if (res != 2) {
380
        gnutls_assert();
381
        goto out;
382
      }
383
    }
384
    _gnutls_fips_mode_reset_zombie();
385
  }
386
#endif
387
2
  _gnutls_prepare_to_load_system_priorities();
388
2
  _gnutls_switch_lib_state(LIB_STATE_OPERATIONAL);
389
2
  ret = 0;
390
391
2
out:
392
2
  _gnutls_init_ret = ret;
393
2
  if (!constructor) {
394
0
    (void)gnutls_static_mutex_unlock(&global_init_mutex);
395
0
  }
396
2
  return ret;
397
2
}
398
399
static void _gnutls_global_deinit(unsigned destructor)
400
0
{
401
0
  if (!destructor) {
402
0
    if (gnutls_static_mutex_lock(&global_init_mutex) < 0) {
403
0
      return;
404
0
    }
405
0
  }
406
407
0
  if (_gnutls_init == 1) {
408
0
    _gnutls_init = 0;
409
0
    if (_gnutls_init_ret < 0) {
410
      /* only deinitialize if gnutls_global_init() has
411
       * succeeded */
412
0
      gnutls_assert();
413
0
      goto fail;
414
0
    }
415
416
0
    _gnutls_system_key_deinit();
417
0
    gnutls_crypto_deinit();
418
0
    _gnutls_rnd_deinit();
419
0
    _gnutls_hello_ext_deinit();
420
0
    asn1_delete_structure(&_gnutls_gnutls_asn);
421
0
    asn1_delete_structure(&_gnutls_pkix1_asn);
422
423
0
    _gnutls_crypto_deregister();
424
0
    gnutls_system_global_deinit();
425
0
    _gnutls_cryptodev_deinit();
426
427
0
    _gnutls_supplemental_deinit();
428
0
    _gnutls_unload_system_priorities();
429
430
#ifdef ENABLE_PKCS11
431
    /* Do not try to deinitialize the PKCS #11 libraries
432
     * from the destructor. If we do and the PKCS #11 modules
433
     * are already being unloaded, we may crash.
434
     */
435
    if (destructor == 0) {
436
      gnutls_pkcs11_deinit();
437
    }
438
#endif
439
#ifdef HAVE_TROUSERS
440
    _gnutls_tpm_global_deinit();
441
#endif
442
#ifdef HAVE_TPM2
443
    _gnutls_tpm2_deinit();
444
#endif
445
446
0
    _gnutls_nss_keylog_deinit();
447
0
  } else {
448
0
    if (_gnutls_init > 0)
449
0
      _gnutls_init--;
450
0
  }
451
452
0
fail:
453
0
  if (!destructor) {
454
0
    (void)gnutls_static_mutex_unlock(&global_init_mutex);
455
0
  }
456
0
}
457
458
/**
459
 * gnutls_global_deinit:
460
 *
461
 * This function deinitializes the global data, that were initialized
462
 * using gnutls_global_init().
463
 *
464
 * Since GnuTLS 3.3.0 this function is no longer necessary to be explicitly
465
 * called. GnuTLS will automatically deinitialize on library destructor. See
466
 * gnutls_global_init() for disabling the implicit initialization/deinitialization.
467
 *
468
 **/
469
void gnutls_global_deinit(void)
470
0
{
471
0
  _gnutls_global_deinit(0);
472
0
}
473
474
/**
475
 * gnutls_check_version:
476
 * @req_version: version string to compare with, or %NULL.
477
 *
478
 * Check the GnuTLS Library version against the provided string.
479
 * See %GNUTLS_VERSION for a suitable @req_version string.
480
 *
481
 * See also gnutls_check_version_numeric(), which provides this
482
 * functionality as a macro.
483
 *
484
 * Returns: Check that the version of the library is at
485
 *   minimum the one given as a string in @req_version and return the
486
 *   actual version string of the library; return %NULL if the
487
 *   condition is not met.  If %NULL is passed to this function no
488
 *   check is done and only the version string is returned.
489
  **/
490
const char *gnutls_check_version(const char *req_version)
491
0
{
492
0
  if (!req_version || strverscmp(req_version, VERSION) <= 0)
493
0
    return VERSION;
494
495
0
  return NULL;
496
0
}
497
498
static void _CONSTRUCTOR lib_init(void)
499
2
{
500
2
  int ret;
501
2
  const char *e;
502
503
2
  if (_gnutls_global_init_skip() != 0)
504
0
    return;
505
506
2
  e = secure_getenv("GNUTLS_NO_IMPLICIT_INIT");
507
2
  if (e != NULL) {
508
0
    ret = atoi(e);
509
0
    if (ret == 1)
510
0
      return;
511
0
  }
512
513
2
  e = secure_getenv("GNUTLS_NO_EXPLICIT_INIT");
514
2
  if (e != NULL) {
515
0
    _gnutls_debug_log(
516
0
      "GNUTLS_NO_EXPLICIT_INIT is deprecated; use GNUTLS_NO_IMPLICIT_INIT\n");
517
0
    ret = atoi(e);
518
0
    if (ret == 1)
519
0
      return;
520
0
  }
521
522
2
  ret = _gnutls_global_init(1);
523
2
  if (ret < 0) {
524
0
    fprintf(stderr, "Error in GnuTLS initialization: %s\n",
525
0
      gnutls_strerror(ret));
526
0
    _gnutls_switch_lib_state(LIB_STATE_ERROR);
527
0
  }
528
2
}
529
530
static void _DESTRUCTOR lib_deinit(void)
531
0
{
532
0
  int ret;
533
0
  const char *e;
534
535
0
  if (_gnutls_global_init_skip() != 0)
536
0
    return;
537
538
0
  e = secure_getenv("GNUTLS_NO_IMPLICIT_INIT");
539
0
  if (e != NULL) {
540
0
    ret = atoi(e);
541
0
    if (ret == 1)
542
0
      return;
543
0
  }
544
545
0
  e = secure_getenv("GNUTLS_NO_EXPLICIT_INIT");
546
0
  if (e != NULL) {
547
0
    _gnutls_debug_log(
548
0
      "GNUTLS_NO_EXPLICIT_INIT is deprecated; use GNUTLS_NO_IMPLICIT_INIT\n");
549
0
    ret = atoi(e);
550
0
    if (ret == 1)
551
0
      return;
552
0
  }
553
554
0
  _gnutls_global_deinit(1);
555
0
}
556
557
static const struct gnutls_library_config_st _gnutls_library_config[] = {
558
#ifdef FIPS_MODULE_NAME
559
  { "fips-module-name", FIPS_MODULE_NAME },
560
#endif
561
#ifdef FIPS_MODULE_VERSION
562
  { "fips-module-version", FIPS_MODULE_VERSION },
563
#endif
564
  { "libgnutls-soname", GNUTLS_LIBRARY_SONAME },
565
  { "libnettle-soname", NETTLE_LIBRARY_SONAME },
566
  { "libhogweed-soname", HOGWEED_LIBRARY_SONAME },
567
  { "libgmp-soname", GMP_LIBRARY_SONAME },
568
  { "hardware-features", HW_FEATURES },
569
  { "tls-features", TLS_FEATURES },
570
  { "default-system-config", SYSTEM_PRIORITY_FILE },
571
  { NULL, NULL }
572
};
573
574
/**
575
 * gnutls_get_library_config:
576
 *
577
 * Returns the library configuration as key value pairs.
578
 * Currently defined keys are:
579
 *
580
 *  - fips-module-name: the name of the FIPS140 module
581
 *
582
 *  - fips-module-version: the version of the FIPS140 module
583
 *
584
 *  - libgnutls-soname: the SONAME of the library itself
585
 *
586
 *  - libnettle-soname: the library SONAME of linked libnettle
587
 *
588
 *  - libhogweed-soname: the library SONAME of linked libhogweed
589
 *
590
 *  - libgmp-soname: the library SONAME of linked libgmp
591
 *
592
 *  - hardware-features: enabled hardware support features
593
 *
594
 *  - tls-features: enabled TLS protocol features
595
 *
596
 * Returns: a NUL-terminated %gnutls_library_config_st array
597
 *
598
 * Since: 3.7.3
599
 */
600
const gnutls_library_config_st *gnutls_get_library_config(void)
601
0
{
602
0
  return _gnutls_library_config;
603
0
}