Coverage Report

Created: 2024-02-25 06:27

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