Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/priority.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2004-2015 Free Software Foundation, Inc.
3
 * Copyright (C) 2015-2019 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
/* Here lies the code of the gnutls_*_set_priority() functions.
25
 */
26
27
#include "gnutls_int.h"
28
#include "algorithms.h"
29
#include "errors.h"
30
#include "num.h"
31
#include <gnutls/x509.h>
32
#include <c-ctype.h>
33
#include "hello_ext.h"
34
#include <c-strcase.h>
35
#include "fips.h"
36
#include "errno.h"
37
#include "ext/srp.h"
38
#include <gnutls/gnutls.h>
39
#include "profiles.h"
40
#include "c-strcase.h"
41
#include "inih/ini.h"
42
#include "locks.h"
43
#include "profiles.h"
44
#include "name_val_array.h"
45
46
0
#define MAX_ELEMENTS GNUTLS_MAX_ALGORITHM_NUM
47
48
#define ENABLE_PROFILE(c, profile)                                \
49
0
  do {                                                      \
50
0
    c->additional_verify_flags &= 0x00ffffff;         \
51
0
    c->additional_verify_flags |=                     \
52
0
      GNUTLS_PROFILE_TO_VFLAGS(profile);        \
53
0
    c->level = _gnutls_profile_to_sec_level(profile); \
54
0
  } while (0)
55
56
/* This function is used by the test suite */
57
char *_gnutls_resolve_priorities(const char *priorities);
58
59
/* This variable points to either a constant value (DEFAULT_PRIORITY_STRING or
60
 * externally assigned) or heap-allocated
61
 * system_wide_config.default_priority_string. We can't move this to the
62
 * system_wide_config struct, because this variable is part of (private) ABI
63
 * exported for testing.
64
 */
65
const char *_gnutls_default_priority_string = DEFAULT_PRIORITY_STRING;
66
67
static void prio_remove(priority_st *priority_list, unsigned int algo);
68
static void prio_add(priority_st *priority_list, unsigned int algo);
69
static void break_list(char *etag, char *broken_etag[MAX_ELEMENTS], int *size);
70
71
typedef void(bulk_rmadd_func)(priority_st *priority_list, const int *);
72
73
inline static void _set_priority(priority_st *st, const int *list)
74
0
{
75
0
  int num = 0, i;
76
77
0
  while (list[num] != 0)
78
0
    num++;
79
0
  if (num > MAX_ALGOS)
80
0
    num = MAX_ALGOS;
81
0
  st->num_priorities = num;
82
83
0
  for (i = 0; i < num; i++) {
84
0
    st->priorities[i] = list[i];
85
0
  }
86
87
0
  return;
88
0
}
89
90
inline static void _add_priority(priority_st *st, const int *list)
91
0
{
92
0
  int num, i, j, init;
93
94
0
  init = i = st->num_priorities;
95
96
0
  for (num = 0; list[num] != 0; ++num) {
97
0
    if (i + 1 > MAX_ALGOS) {
98
0
      return;
99
0
    }
100
101
0
    for (j = 0; j < init; j++) {
102
0
      if (st->priorities[j] == (unsigned)list[num]) {
103
0
        break;
104
0
      }
105
0
    }
106
107
0
    if (j == init) {
108
0
      st->priorities[i++] = list[num];
109
0
      st->num_priorities++;
110
0
    }
111
0
  }
112
113
0
  return;
114
0
}
115
116
static void _clear_priorities(priority_st *st, const int *list)
117
0
{
118
0
  memset(st, 0, sizeof(*st));
119
0
}
120
121
static void _clear_given_priorities(priority_st *st, const int *list)
122
0
{
123
0
  unsigned i;
124
125
0
  for (i = 0; list[i] != 0; i++) {
126
0
    prio_remove(st, list[i]);
127
0
  }
128
0
}
129
130
static const int _supported_groups_dh[] = {
131
  GNUTLS_GROUP_FFDHE2048, GNUTLS_GROUP_FFDHE3072, GNUTLS_GROUP_FFDHE4096,
132
  GNUTLS_GROUP_FFDHE6144, GNUTLS_GROUP_FFDHE8192, 0
133
};
134
135
static const int _supported_groups_ecdh[] = { GNUTLS_GROUP_SECP256R1,
136
                GNUTLS_GROUP_SECP384R1,
137
                GNUTLS_GROUP_SECP521R1,
138
                GNUTLS_GROUP_X25519, /* RFC 8422 */
139
                GNUTLS_GROUP_X448, /* RFC 8422 */
140
                0 };
141
142
static const int _supported_groups_gost[] = {
143
#ifdef ENABLE_GOST
144
  GNUTLS_GROUP_GC256A,
145
  GNUTLS_GROUP_GC256B,
146
  GNUTLS_GROUP_GC256C,
147
  GNUTLS_GROUP_GC256D,
148
  GNUTLS_GROUP_GC512A,
149
  GNUTLS_GROUP_GC512B,
150
  GNUTLS_GROUP_GC512C,
151
#endif
152
  0
153
};
154
155
static const int _supported_groups_normal[] = {
156
  GNUTLS_GROUP_SECP256R1, GNUTLS_GROUP_SECP384R1, GNUTLS_GROUP_SECP521R1,
157
  GNUTLS_GROUP_X25519, /* RFC 8422 */
158
  GNUTLS_GROUP_X448, /* RFC 8422 */
159
160
  /* These should stay last as our default behavior
161
   * is to send key shares for two top types (GNUTLS_KEY_SHARE_TOP2)
162
   * and we wouldn't want to have these sent by all clients
163
   * by default as they are quite expensive CPU-wise. */
164
  GNUTLS_GROUP_FFDHE2048, GNUTLS_GROUP_FFDHE3072, GNUTLS_GROUP_FFDHE4096,
165
  GNUTLS_GROUP_FFDHE6144, GNUTLS_GROUP_FFDHE8192, 0
166
};
167
168
static const int *supported_groups_normal = _supported_groups_normal;
169
170
static const int _supported_groups_secure128[] = {
171
  GNUTLS_GROUP_SECP256R1, GNUTLS_GROUP_SECP384R1, GNUTLS_GROUP_SECP521R1,
172
  GNUTLS_GROUP_X25519, /* RFC 8422 */
173
  GNUTLS_GROUP_X448, /* RFC 8422 */
174
  GNUTLS_GROUP_FFDHE2048, GNUTLS_GROUP_FFDHE3072, GNUTLS_GROUP_FFDHE4096,
175
  GNUTLS_GROUP_FFDHE6144, GNUTLS_GROUP_FFDHE8192, 0
176
};
177
178
static const int *supported_groups_secure128 = _supported_groups_secure128;
179
180
static const int _supported_groups_suiteb128[] = { GNUTLS_GROUP_SECP256R1,
181
               GNUTLS_GROUP_SECP384R1, 0 };
182
183
static const int *supported_groups_suiteb128 = _supported_groups_suiteb128;
184
185
static const int _supported_groups_suiteb192[] = { GNUTLS_GROUP_SECP384R1, 0 };
186
187
static const int *supported_groups_suiteb192 = _supported_groups_suiteb192;
188
189
static const int _supported_groups_secure192[] = { GNUTLS_GROUP_SECP384R1,
190
               GNUTLS_GROUP_SECP521R1,
191
               GNUTLS_GROUP_FFDHE8192, 0 };
192
193
static const int *supported_groups_secure192 = _supported_groups_secure192;
194
195
static const int protocol_priority[] = { GNUTLS_TLS1_3,
196
           GNUTLS_TLS1_2,
197
           GNUTLS_TLS1_1,
198
           GNUTLS_TLS1_0,
199
           GNUTLS_DTLS1_2,
200
           GNUTLS_DTLS1_0,
201
           0 };
202
203
/* contains all the supported TLS protocols, intended to be used for eliminating them
204
 */
205
static const int stream_protocol_priority[] = { GNUTLS_TLS1_3, GNUTLS_TLS1_2,
206
            GNUTLS_TLS1_1, GNUTLS_TLS1_0,
207
            0 };
208
209
/* contains all the supported DTLS protocols, intended to be used for eliminating them
210
 */
211
static const int dgram_protocol_priority[] = { GNUTLS_DTLS1_2, GNUTLS_DTLS1_0,
212
                 GNUTLS_DTLS0_9, 0 };
213
214
static const int dtls_protocol_priority[] = { GNUTLS_DTLS1_2, GNUTLS_DTLS1_0,
215
                0 };
216
217
static const int _protocol_priority_suiteb[] = { GNUTLS_TLS1_2, 0 };
218
219
static const int *protocol_priority_suiteb = _protocol_priority_suiteb;
220
221
static const int _kx_priority_performance[] = { GNUTLS_KX_RSA,
222
#ifdef ENABLE_ECDHE
223
            GNUTLS_KX_ECDHE_ECDSA,
224
            GNUTLS_KX_ECDHE_RSA,
225
#endif
226
#ifdef ENABLE_DHE
227
            GNUTLS_KX_DHE_RSA,
228
#endif
229
            0 };
230
231
static const int *kx_priority_performance = _kx_priority_performance;
232
233
static const int _kx_priority_pfs[] = {
234
#ifdef ENABLE_ECDHE
235
  GNUTLS_KX_ECDHE_ECDSA, GNUTLS_KX_ECDHE_RSA,
236
#endif
237
#ifdef ENABLE_DHE
238
  GNUTLS_KX_DHE_RSA,
239
#endif
240
  0
241
};
242
243
static const int *kx_priority_pfs = _kx_priority_pfs;
244
245
static const int _kx_priority_suiteb[] = { GNUTLS_KX_ECDHE_ECDSA, 0 };
246
247
static const int *kx_priority_suiteb = _kx_priority_suiteb;
248
249
static const int _kx_priority_secure[] = {
250
/* The ciphersuites that offer forward secrecy take
251
   * precedence
252
   */
253
#ifdef ENABLE_ECDHE
254
  GNUTLS_KX_ECDHE_ECDSA, GNUTLS_KX_ECDHE_RSA,
255
#endif
256
  GNUTLS_KX_RSA,
257
/* KX-RSA is now ahead of DHE-RSA and DHE-DSS due to the compatibility
258
   * issues the DHE ciphersuites have. That is, one cannot enforce a specific
259
   * security level without dropping the connection.
260
   */
261
#ifdef ENABLE_DHE
262
  GNUTLS_KX_DHE_RSA,
263
#endif
264
  /* GNUTLS_KX_ANON_DH: Man-in-the-middle prone, don't add!
265
   */
266
  0
267
};
268
269
static const int *kx_priority_secure = _kx_priority_secure;
270
271
static const int _kx_priority_gost[] = {
272
#ifdef ENABLE_GOST
273
  GNUTLS_KX_VKO_GOST_12,
274
#endif
275
  0,
276
};
277
278
static const int *kx_priority_gost = _kx_priority_gost;
279
280
static const int _cipher_priority_performance_default[] = {
281
  GNUTLS_CIPHER_AES_128_GCM,   GNUTLS_CIPHER_AES_256_GCM,
282
  GNUTLS_CIPHER_CHACHA20_POLY1305, GNUTLS_CIPHER_AES_128_CCM,
283
  GNUTLS_CIPHER_AES_256_CCM,   GNUTLS_CIPHER_AES_128_CBC,
284
  GNUTLS_CIPHER_AES_256_CBC,   0
285
};
286
287
static const int _cipher_priority_performance_no_aesni[] = {
288
  GNUTLS_CIPHER_CHACHA20_POLY1305, GNUTLS_CIPHER_AES_128_GCM,
289
  GNUTLS_CIPHER_AES_256_GCM,   GNUTLS_CIPHER_AES_128_CCM,
290
  GNUTLS_CIPHER_AES_256_CCM,   GNUTLS_CIPHER_AES_128_CBC,
291
  GNUTLS_CIPHER_AES_256_CBC,   0
292
};
293
294
/* If GCM and AES acceleration is available then prefer
295
 * them over anything else. Overall we prioritise AEAD
296
 * over legacy ciphers, and 256-bit over 128 (for future
297
 * proof).
298
 */
299
static const int _cipher_priority_normal_default[] = {
300
  GNUTLS_CIPHER_AES_256_GCM, GNUTLS_CIPHER_CHACHA20_POLY1305,
301
  GNUTLS_CIPHER_AES_256_CCM,
302
303
  GNUTLS_CIPHER_AES_256_CBC,
304
305
  GNUTLS_CIPHER_AES_128_GCM, GNUTLS_CIPHER_AES_128_CCM,
306
307
  GNUTLS_CIPHER_AES_128_CBC, 0
308
};
309
310
static const int cipher_priority_performance_fips[] = {
311
  GNUTLS_CIPHER_AES_128_GCM,
312
  GNUTLS_CIPHER_AES_128_CCM,
313
  GNUTLS_CIPHER_AES_256_GCM,
314
  GNUTLS_CIPHER_AES_256_CCM,
315
316
  GNUTLS_CIPHER_AES_128_CBC,
317
  GNUTLS_CIPHER_AES_256_CBC,
318
  0
319
};
320
321
static const int cipher_priority_normal_fips[] = {
322
  GNUTLS_CIPHER_AES_256_GCM, GNUTLS_CIPHER_AES_256_CCM,
323
  GNUTLS_CIPHER_AES_256_CBC,
324
325
  GNUTLS_CIPHER_AES_128_GCM, GNUTLS_CIPHER_AES_128_CBC,
326
  GNUTLS_CIPHER_AES_128_CCM, 0
327
};
328
329
static const int _cipher_priority_suiteb128[] = { GNUTLS_CIPHER_AES_256_GCM,
330
              GNUTLS_CIPHER_AES_128_GCM,
331
              0 };
332
333
static const int *cipher_priority_suiteb128 = _cipher_priority_suiteb128;
334
335
static const int _cipher_priority_suiteb192[] = { GNUTLS_CIPHER_AES_256_GCM,
336
              0 };
337
338
static const int *cipher_priority_suiteb192 = _cipher_priority_suiteb192;
339
340
static const int _cipher_priority_secure128[] = {
341
  GNUTLS_CIPHER_AES_256_GCM, GNUTLS_CIPHER_CHACHA20_POLY1305,
342
  GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_256_CCM,
343
344
  GNUTLS_CIPHER_AES_128_GCM, GNUTLS_CIPHER_AES_128_CBC,
345
  GNUTLS_CIPHER_AES_128_CCM, 0
346
};
347
348
static const int *cipher_priority_secure128 = _cipher_priority_secure128;
349
350
static const int _cipher_priority_secure192[] = {
351
  GNUTLS_CIPHER_AES_256_GCM, GNUTLS_CIPHER_CHACHA20_POLY1305,
352
  GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_256_CCM, 0
353
};
354
355
static const int *cipher_priority_secure192 = _cipher_priority_secure192;
356
357
static const int _sign_priority_default[] = {
358
  GNUTLS_SIGN_RSA_SHA256,
359
  GNUTLS_SIGN_RSA_PSS_SHA256,
360
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA256,
361
  GNUTLS_SIGN_ECDSA_SHA256,
362
  GNUTLS_SIGN_ECDSA_SECP256R1_SHA256,
363
364
  GNUTLS_SIGN_EDDSA_ED25519,
365
366
  GNUTLS_SIGN_RSA_SHA384,
367
  GNUTLS_SIGN_RSA_PSS_SHA384,
368
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA384,
369
  GNUTLS_SIGN_ECDSA_SHA384,
370
  GNUTLS_SIGN_ECDSA_SECP384R1_SHA384,
371
372
  GNUTLS_SIGN_EDDSA_ED448,
373
374
  GNUTLS_SIGN_RSA_SHA512,
375
  GNUTLS_SIGN_RSA_PSS_SHA512,
376
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA512,
377
378
  GNUTLS_SIGN_ECDSA_SHA512,
379
  GNUTLS_SIGN_ECDSA_SECP521R1_SHA512,
380
381
  GNUTLS_SIGN_RSA_SHA1,
382
  GNUTLS_SIGN_ECDSA_SHA1,
383
384
  0
385
};
386
387
static const int *sign_priority_default = _sign_priority_default;
388
389
static const int _sign_priority_suiteb128[] = {
390
  GNUTLS_SIGN_ECDSA_SHA256, GNUTLS_SIGN_ECDSA_SECP256R1_SHA256,
391
  GNUTLS_SIGN_ECDSA_SHA384, GNUTLS_SIGN_ECDSA_SECP384R1_SHA384, 0
392
};
393
394
static const int *sign_priority_suiteb128 = _sign_priority_suiteb128;
395
396
static const int _sign_priority_suiteb192[] = {
397
  GNUTLS_SIGN_ECDSA_SHA384, GNUTLS_SIGN_ECDSA_SECP384R1_SHA384, 0
398
};
399
400
static const int *sign_priority_suiteb192 = _sign_priority_suiteb192;
401
402
static const int _sign_priority_secure128[] = {
403
  GNUTLS_SIGN_RSA_SHA256,
404
  GNUTLS_SIGN_RSA_PSS_SHA256,
405
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA256,
406
  GNUTLS_SIGN_ECDSA_SHA256,
407
  GNUTLS_SIGN_ECDSA_SECP256R1_SHA256,
408
409
  GNUTLS_SIGN_EDDSA_ED25519,
410
411
  GNUTLS_SIGN_RSA_SHA384,
412
  GNUTLS_SIGN_RSA_PSS_SHA384,
413
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA384,
414
  GNUTLS_SIGN_ECDSA_SHA384,
415
  GNUTLS_SIGN_ECDSA_SECP384R1_SHA384,
416
417
  GNUTLS_SIGN_EDDSA_ED448,
418
419
  GNUTLS_SIGN_RSA_SHA512,
420
  GNUTLS_SIGN_RSA_PSS_SHA512,
421
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA512,
422
  GNUTLS_SIGN_ECDSA_SHA512,
423
  GNUTLS_SIGN_ECDSA_SECP521R1_SHA512,
424
425
  0
426
};
427
428
static const int *sign_priority_secure128 = _sign_priority_secure128;
429
430
static const int _sign_priority_secure192[] = {
431
  GNUTLS_SIGN_RSA_SHA384,
432
  GNUTLS_SIGN_RSA_PSS_SHA384,
433
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA384,
434
  GNUTLS_SIGN_ECDSA_SHA384,
435
  GNUTLS_SIGN_ECDSA_SECP384R1_SHA384,
436
  GNUTLS_SIGN_EDDSA_ED448,
437
  GNUTLS_SIGN_RSA_SHA512,
438
  GNUTLS_SIGN_RSA_PSS_SHA512,
439
  GNUTLS_SIGN_RSA_PSS_RSAE_SHA512,
440
  GNUTLS_SIGN_ECDSA_SHA512,
441
  GNUTLS_SIGN_ECDSA_SECP521R1_SHA512,
442
443
  0
444
};
445
446
static const int *sign_priority_secure192 = _sign_priority_secure192;
447
448
static const int _sign_priority_gost[] = {
449
#ifdef ENABLE_GOST
450
  GNUTLS_SIGN_GOST_256, GNUTLS_SIGN_GOST_512,
451
#endif
452
  0
453
};
454
455
static const int *sign_priority_gost = _sign_priority_gost;
456
457
static const int mac_priority_normal_default[] = { GNUTLS_MAC_SHA1,
458
               GNUTLS_MAC_AEAD, 0 };
459
460
static const int mac_priority_normal_fips[] = { GNUTLS_MAC_SHA1,
461
            GNUTLS_MAC_AEAD, 0 };
462
463
static const int *cipher_priority_performance =
464
  _cipher_priority_performance_default;
465
static const int *cipher_priority_normal = _cipher_priority_normal_default;
466
static const int *mac_priority_normal = mac_priority_normal_default;
467
468
static const int _cipher_priority_gost[] = {
469
#ifdef ENABLE_GOST
470
  GNUTLS_CIPHER_GOST28147_TC26Z_CNT,
471
#endif
472
  0
473
};
474
475
static const int *cipher_priority_gost = _cipher_priority_gost;
476
477
static const int _mac_priority_gost[] = {
478
#ifdef ENABLE_GOST
479
  GNUTLS_MAC_GOST28147_TC26Z_IMIT,
480
#endif
481
  0
482
};
483
484
static const int *mac_priority_gost = _mac_priority_gost;
485
486
/* if called with replace the default priorities with the FIPS140 ones */
487
void _gnutls_priority_update_fips(void)
488
0
{
489
0
  cipher_priority_performance = cipher_priority_performance_fips;
490
0
  cipher_priority_normal = cipher_priority_normal_fips;
491
0
  mac_priority_normal = mac_priority_normal_fips;
492
0
}
493
494
void _gnutls_priority_update_non_aesni(void)
495
0
{
496
  /* if we have no AES acceleration in performance mode
497
   * prefer fast stream ciphers */
498
0
  if (_gnutls_fips_mode_enabled() == 0) {
499
0
    cipher_priority_performance =
500
0
      _cipher_priority_performance_no_aesni;
501
0
  }
502
0
}
503
504
static const int _mac_priority_suiteb[] = { GNUTLS_MAC_AEAD, 0 };
505
506
static const int *mac_priority_suiteb = _mac_priority_suiteb;
507
508
static const int _mac_priority_secure128[] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_AEAD,
509
                 0 };
510
511
static const int *mac_priority_secure128 = _mac_priority_secure128;
512
513
static const int _mac_priority_secure192[] = { GNUTLS_MAC_AEAD, 0 };
514
515
static const int *mac_priority_secure192 = _mac_priority_secure192;
516
517
static const int cert_type_priority_default[] = { GNUTLS_CRT_X509, 0 };
518
519
static const int cert_type_priority_all[] = { GNUTLS_CRT_X509, GNUTLS_CRT_RAWPK,
520
                0 };
521
522
typedef void(rmadd_func)(priority_st *priority_list, unsigned int alg);
523
524
static void prio_remove(priority_st *priority_list, unsigned int algo)
525
0
{
526
0
  unsigned int i;
527
528
0
  for (i = 0; i < priority_list->num_priorities; i++) {
529
0
    if (priority_list->priorities[i] == algo) {
530
0
      priority_list->num_priorities--;
531
0
      if ((priority_list->num_priorities - i) > 0)
532
0
        memmove(&priority_list->priorities[i],
533
0
          &priority_list->priorities[i + 1],
534
0
          (priority_list->num_priorities -
535
0
           i) * sizeof(priority_list
536
0
                   ->priorities[0]));
537
0
      priority_list
538
0
        ->priorities[priority_list->num_priorities] = 0;
539
0
      break;
540
0
    }
541
0
  }
542
543
0
  return;
544
0
}
545
546
static void prio_add(priority_st *priority_list, unsigned int algo)
547
0
{
548
0
  unsigned int i, l = priority_list->num_priorities;
549
550
0
  if (l >= MAX_ALGOS)
551
0
    return; /* can't add it anyway */
552
553
0
  for (i = 0; i < l; ++i) {
554
0
    if (algo == priority_list->priorities[i])
555
0
      return; /* if it exists */
556
0
  }
557
558
0
  priority_list->priorities[l] = algo;
559
0
  priority_list->num_priorities++;
560
561
0
  return;
562
0
}
563
564
/**
565
 * gnutls_priority_set:
566
 * @session: is a #gnutls_session_t type.
567
 * @priority: is a #gnutls_priority_t type.
568
 *
569
 * Sets the priorities to use on the ciphers, key exchange methods,
570
 * and macs. Note that this function is expected to be called once
571
 * per session; when called multiple times (e.g., before a re-handshake,
572
 * the caller should make sure that any new settings are not incompatible
573
 * with the original session).
574
 *
575
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code on error.
576
 **/
577
int gnutls_priority_set(gnutls_session_t session, gnutls_priority_t priority)
578
0
{
579
0
  int ret;
580
581
0
  if (priority == NULL || priority->protocol.num_priorities == 0 ||
582
0
      priority->cs.size == 0)
583
0
    return gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
584
585
  /* set the current version to the first in the chain, if this is
586
   * the call before the initial handshake. During a re-handshake
587
   * we do not set the version to avoid overriding the currently
588
   * negotiated version. */
589
0
  if (!session->internals.handshake_in_progress &&
590
0
      !session->internals.initial_negotiation_completed) {
591
0
    ret = _gnutls_set_current_version(
592
0
      session, priority->protocol.priorities[0]);
593
0
    if (ret < 0)
594
0
      return gnutls_assert_val(ret);
595
0
  }
596
597
  /* At this point the provided priorities passed the sanity tests */
598
599
0
  if (session->internals.priorities)
600
0
    gnutls_priority_deinit(session->internals.priorities);
601
602
0
  gnutls_atomic_increment(&priority->usage_cnt);
603
0
  session->internals.priorities = priority;
604
605
0
  if (priority->no_tickets != 0) {
606
0
    session->internals.flags |= GNUTLS_NO_TICKETS;
607
0
  }
608
609
0
  if (priority->no_tickets_tls12 != 0) {
610
    /* when PFS is explicitly requested, disable session tickets for TLS 1.2 */
611
0
    session->internals.flags |= GNUTLS_NO_TICKETS_TLS12;
612
0
  }
613
614
0
  if (priority->no_status_request)
615
0
    session->internals.flags |= GNUTLS_NO_STATUS_REQUEST;
616
617
0
  ADD_PROFILE_VFLAGS(session, priority->additional_verify_flags);
618
619
  /* mirror variables */
620
0
#undef COPY_TO_INTERNALS
621
0
#define COPY_TO_INTERNALS(xx) session->internals.xx = priority->_##xx
622
0
  COPY_TO_INTERNALS(allow_large_records);
623
0
  COPY_TO_INTERNALS(allow_small_records);
624
0
  COPY_TO_INTERNALS(no_etm);
625
0
  COPY_TO_INTERNALS(no_ext_master_secret);
626
0
  COPY_TO_INTERNALS(allow_key_usage_violation);
627
0
  COPY_TO_INTERNALS(dumbfw);
628
0
  COPY_TO_INTERNALS(dh_prime_bits);
629
630
0
  return 0;
631
0
}
632
633
0
#define LEVEL_NONE "NONE"
634
#define LEVEL_NORMAL "NORMAL"
635
#define LEVEL_PFS "PFS"
636
#define LEVEL_PERFORMANCE "PERFORMANCE"
637
#define LEVEL_SECURE128 "SECURE128"
638
#define LEVEL_SECURE192 "SECURE192"
639
#define LEVEL_SECURE256 "SECURE256"
640
#define LEVEL_SUITEB128 "SUITEB128"
641
#define LEVEL_SUITEB192 "SUITEB192"
642
#define LEVEL_LEGACY "LEGACY"
643
0
#define LEVEL_SYSTEM "SYSTEM"
644
645
struct priority_groups_st {
646
  const char *name;
647
  const char *alias;
648
  const int **proto_list;
649
  const int **cipher_list;
650
  const int **mac_list;
651
  const int **kx_list;
652
  const int **sign_list;
653
  const int **group_list;
654
  unsigned profile;
655
  int sec_param;
656
  bool no_tickets;
657
  bool no_tickets_tls12;
658
};
659
660
static const struct priority_groups_st pgroups[] = {
661
  { .name = LEVEL_NORMAL,
662
    .cipher_list = &cipher_priority_normal,
663
    .mac_list = &mac_priority_normal,
664
    .kx_list = &kx_priority_secure,
665
    .sign_list = &sign_priority_default,
666
    .group_list = &supported_groups_normal,
667
    .profile = GNUTLS_PROFILE_LOW,
668
    .sec_param = GNUTLS_SEC_PARAM_WEAK },
669
  { .name = LEVEL_PFS,
670
    .cipher_list = &cipher_priority_normal,
671
    .mac_list = &mac_priority_secure128,
672
    .kx_list = &kx_priority_pfs,
673
    .sign_list = &sign_priority_default,
674
    .group_list = &supported_groups_normal,
675
    .profile = GNUTLS_PROFILE_LOW,
676
    .sec_param = GNUTLS_SEC_PARAM_WEAK,
677
    .no_tickets_tls12 = 1 },
678
  { .name = LEVEL_SECURE128,
679
    .alias = "SECURE",
680
    .cipher_list = &cipher_priority_secure128,
681
    .mac_list = &mac_priority_secure128,
682
    .kx_list = &kx_priority_secure,
683
    .sign_list = &sign_priority_secure128,
684
    .group_list = &supported_groups_secure128,
685
    /* The profile should have been HIGH but if we don't allow
686
    * SHA-1 (80-bits) as signature algorithm we are not able
687
    * to connect anywhere with this level */
688
    .profile = GNUTLS_PROFILE_LOW,
689
    .sec_param = GNUTLS_SEC_PARAM_LOW },
690
  { .name = LEVEL_SECURE192,
691
    .alias = LEVEL_SECURE256,
692
    .cipher_list = &cipher_priority_secure192,
693
    .mac_list = &mac_priority_secure192,
694
    .kx_list = &kx_priority_secure,
695
    .sign_list = &sign_priority_secure192,
696
    .group_list = &supported_groups_secure192,
697
    .profile = GNUTLS_PROFILE_HIGH,
698
    .sec_param = GNUTLS_SEC_PARAM_HIGH },
699
  { .name = LEVEL_SUITEB128,
700
    .proto_list = &protocol_priority_suiteb,
701
    .cipher_list = &cipher_priority_suiteb128,
702
    .mac_list = &mac_priority_suiteb,
703
    .kx_list = &kx_priority_suiteb,
704
    .sign_list = &sign_priority_suiteb128,
705
    .group_list = &supported_groups_suiteb128,
706
    .profile = GNUTLS_PROFILE_SUITEB128,
707
    .sec_param = GNUTLS_SEC_PARAM_HIGH },
708
  { .name = LEVEL_SUITEB192,
709
    .proto_list = &protocol_priority_suiteb,
710
    .cipher_list = &cipher_priority_suiteb192,
711
    .mac_list = &mac_priority_suiteb,
712
    .kx_list = &kx_priority_suiteb,
713
    .sign_list = &sign_priority_suiteb192,
714
    .group_list = &supported_groups_suiteb192,
715
    .profile = GNUTLS_PROFILE_SUITEB192,
716
    .sec_param = GNUTLS_SEC_PARAM_ULTRA },
717
  { .name = LEVEL_LEGACY,
718
    .cipher_list = &cipher_priority_normal,
719
    .mac_list = &mac_priority_normal,
720
    .kx_list = &kx_priority_secure,
721
    .sign_list = &sign_priority_default,
722
    .group_list = &supported_groups_normal,
723
    .sec_param = GNUTLS_SEC_PARAM_VERY_WEAK },
724
  { .name = LEVEL_PERFORMANCE,
725
    .cipher_list = &cipher_priority_performance,
726
    .mac_list = &mac_priority_normal,
727
    .kx_list = &kx_priority_performance,
728
    .sign_list = &sign_priority_default,
729
    .group_list = &supported_groups_normal,
730
    .profile = GNUTLS_PROFILE_LOW,
731
    .sec_param = GNUTLS_SEC_PARAM_WEAK },
732
  {
733
    .name = NULL,
734
  }
735
};
736
737
#define SET_PROFILE(to_set)                                \
738
0
  profile = GNUTLS_VFLAGS_TO_PROFILE(                \
739
0
    priority_cache->additional_verify_flags);  \
740
0
  if (profile == 0 || profile > to_set) {            \
741
0
    priority_cache->additional_verify_flags &= \
742
0
      ~GNUTLS_VFLAGS_PROFILE_MASK;       \
743
0
    priority_cache->additional_verify_flags |= \
744
0
      GNUTLS_PROFILE_TO_VFLAGS(to_set);  \
745
0
  }
746
747
#define SET_LEVEL(to_set)                                       \
748
0
  if (priority_cache->level == 0 ||                       \
749
0
      (unsigned)priority_cache->level > (unsigned)to_set) \
750
0
  priority_cache->level = to_set
751
752
static int check_level(const char *level, gnutls_priority_t priority_cache,
753
           int add)
754
0
{
755
0
  bulk_rmadd_func *func;
756
0
  unsigned profile = 0;
757
0
  unsigned i;
758
0
  int j;
759
0
  const cipher_entry_st *centry;
760
761
0
  if (add)
762
0
    func = _add_priority;
763
0
  else
764
0
    func = _set_priority;
765
766
0
  for (i = 0;; i++) {
767
0
    if (pgroups[i].name == NULL)
768
0
      return 0;
769
770
0
    if (c_strcasecmp(level, pgroups[i].name) == 0 ||
771
0
        (pgroups[i].alias != NULL &&
772
0
         c_strcasecmp(level, pgroups[i].alias) == 0)) {
773
0
      if (pgroups[i].proto_list != NULL)
774
0
        func(&priority_cache->protocol,
775
0
             *pgroups[i].proto_list);
776
0
      func(&priority_cache->_cipher, *pgroups[i].cipher_list);
777
0
      func(&priority_cache->_kx, *pgroups[i].kx_list);
778
0
      func(&priority_cache->_mac, *pgroups[i].mac_list);
779
0
      func(&priority_cache->_sign_algo,
780
0
           *pgroups[i].sign_list);
781
0
      func(&priority_cache->_supported_ecc,
782
0
           *pgroups[i].group_list);
783
784
0
      if (pgroups[i].profile != 0) {
785
0
        SET_PROFILE(
786
0
          pgroups[i].profile); /* set certificate level */
787
0
      }
788
0
      SET_LEVEL(
789
0
        pgroups[i].sec_param); /* set DH params level */
790
0
      priority_cache->no_tickets = pgroups[i].no_tickets;
791
0
      priority_cache->no_tickets_tls12 =
792
0
        pgroups[i].no_tickets_tls12;
793
0
      if (priority_cache->have_cbc == 0) {
794
0
        for (j = 0; (*pgroups[i].cipher_list)[j] != 0;
795
0
             j++) {
796
0
          centry = cipher_to_entry(
797
0
            (*pgroups[i].cipher_list)[j]);
798
0
          if (centry != NULL &&
799
0
              centry->type == CIPHER_BLOCK) {
800
0
            priority_cache->have_cbc = 1;
801
0
            break;
802
0
          }
803
0
        }
804
0
      }
805
0
      return 1;
806
0
    }
807
0
  }
808
0
}
809
810
static void enable_compat(gnutls_priority_t c)
811
0
{
812
0
  ENABLE_PRIO_COMPAT(c);
813
0
}
814
815
static void enable_server_key_usage_violations(gnutls_priority_t c)
816
0
{
817
0
  c->allow_server_key_usage_violation = 1;
818
0
}
819
820
static void enable_allow_small_records(gnutls_priority_t c)
821
0
{
822
0
  c->_allow_small_records = 1;
823
0
}
824
825
static void enable_dumbfw(gnutls_priority_t c)
826
0
{
827
0
  c->_dumbfw = 1;
828
0
}
829
830
static void enable_no_extensions(gnutls_priority_t c)
831
0
{
832
0
  c->no_extensions = 1;
833
0
}
834
835
static void enable_no_status_request(gnutls_priority_t c)
836
0
{
837
0
  c->no_status_request = 1;
838
0
}
839
840
static void enable_no_ext_master_secret(gnutls_priority_t c)
841
0
{
842
0
  c->_no_ext_master_secret = 1;
843
0
}
844
845
static void enable_force_ext_master_secret(gnutls_priority_t c)
846
0
{
847
0
  c->force_ext_master_secret = EMS_REQUIRE;
848
0
}
849
850
static void enable_no_etm(gnutls_priority_t c)
851
0
{
852
0
  c->_no_etm = 1;
853
0
}
854
855
static void enable_force_etm(gnutls_priority_t c)
856
0
{
857
0
  c->force_etm = 1;
858
0
}
859
860
static void enable_no_tickets(gnutls_priority_t c)
861
0
{
862
0
  c->no_tickets = 1;
863
0
}
864
865
static void enable_no_tickets_tls12(gnutls_priority_t c)
866
0
{
867
0
  c->no_tickets_tls12 = 1;
868
0
}
869
870
static void disable_wildcards(gnutls_priority_t c)
871
0
{
872
0
  c->additional_verify_flags |= GNUTLS_VERIFY_DO_NOT_ALLOW_WILDCARDS;
873
0
}
874
875
static void enable_profile_very_weak(gnutls_priority_t c)
876
0
{
877
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_VERY_WEAK);
878
0
}
879
880
static void enable_profile_low(gnutls_priority_t c)
881
0
{
882
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_LOW);
883
0
}
884
885
static void enable_profile_legacy(gnutls_priority_t c)
886
0
{
887
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_LEGACY);
888
0
}
889
890
static void enable_profile_medium(gnutls_priority_t c)
891
0
{
892
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_MEDIUM);
893
0
}
894
895
static void enable_profile_high(gnutls_priority_t c)
896
0
{
897
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_HIGH);
898
0
}
899
900
static void enable_profile_ultra(gnutls_priority_t c)
901
0
{
902
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_ULTRA);
903
0
}
904
905
static void enable_profile_future(gnutls_priority_t c)
906
0
{
907
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_FUTURE);
908
0
}
909
910
static void enable_profile_suiteb128(gnutls_priority_t c)
911
0
{
912
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_SUITEB128);
913
0
}
914
915
static void enable_profile_suiteb192(gnutls_priority_t c)
916
0
{
917
0
  ENABLE_PROFILE(c, GNUTLS_PROFILE_SUITEB128);
918
0
}
919
920
static void enable_safe_renegotiation(gnutls_priority_t c)
921
0
{
922
0
  c->sr = SR_SAFE;
923
0
}
924
925
static void enable_unsafe_renegotiation(gnutls_priority_t c)
926
0
{
927
0
  c->sr = SR_UNSAFE;
928
0
}
929
930
static void enable_partial_safe_renegotiation(gnutls_priority_t c)
931
0
{
932
0
  c->sr = SR_PARTIAL;
933
0
}
934
935
static void disable_safe_renegotiation(gnutls_priority_t c)
936
0
{
937
0
  c->sr = SR_DISABLED;
938
0
}
939
940
static void enable_fallback_scsv(gnutls_priority_t c)
941
0
{
942
0
  c->fallback = 1;
943
0
}
944
945
static void enable_latest_record_version(gnutls_priority_t c)
946
0
{
947
0
  c->min_record_version = 0;
948
0
}
949
950
static void enable_ssl3_record_version(gnutls_priority_t c)
951
0
{
952
0
  c->min_record_version = 1;
953
0
}
954
955
static void enable_verify_allow_rsa_md5(gnutls_priority_t c)
956
0
{
957
0
  c->additional_verify_flags |= GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5;
958
0
}
959
960
static void enable_verify_allow_sha1(gnutls_priority_t c)
961
0
{
962
0
  c->additional_verify_flags |= GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1;
963
0
}
964
965
static void enable_verify_allow_broken(gnutls_priority_t c)
966
0
{
967
0
  c->additional_verify_flags |= GNUTLS_VERIFY_ALLOW_BROKEN;
968
0
}
969
970
static void disable_crl_checks(gnutls_priority_t c)
971
0
{
972
0
  c->additional_verify_flags |= GNUTLS_VERIFY_DISABLE_CRL_CHECKS;
973
0
}
974
975
static void enable_server_precedence(gnutls_priority_t c)
976
0
{
977
0
  c->server_precedence = 1;
978
0
}
979
980
static void disable_tls13_compat_mode(gnutls_priority_t c)
981
0
{
982
0
  c->tls13_compat_mode = false;
983
0
}
984
985
static void enable_no_shuffle_extensions(gnutls_priority_t c)
986
0
{
987
0
  c->no_shuffle_extensions = 1;
988
0
}
989
990
static void dummy_func(gnutls_priority_t c)
991
0
{
992
0
}
993
994
#include <priority_options.h>
995
996
struct cfg {
997
  bool allowlisting;
998
  bool ktls_enabled;
999
1000
  name_val_array_t priority_strings;
1001
  char *priority_string;
1002
  char *default_priority_string;
1003
  gnutls_certificate_verification_profiles_t verification_profile;
1004
1005
  gnutls_cipher_algorithm_t ciphers[MAX_ALGOS + 1];
1006
  gnutls_mac_algorithm_t macs[MAX_ALGOS + 1];
1007
  gnutls_group_t groups[MAX_ALGOS + 1];
1008
  gnutls_kx_algorithm_t kxs[MAX_ALGOS + 1];
1009
  gnutls_sign_algorithm_t sigs[MAX_ALGOS + 1];
1010
  gnutls_protocol_t versions[MAX_ALGOS + 1];
1011
1012
  gnutls_digest_algorithm_t hashes[MAX_ALGOS + 1];
1013
  gnutls_ecc_curve_t ecc_curves[MAX_ALGOS + 1];
1014
  gnutls_sign_algorithm_t sigs_for_cert[MAX_ALGOS + 1];
1015
1016
  ext_master_secret_t force_ext_master_secret;
1017
  bool force_ext_master_secret_set;
1018
};
1019
1020
static inline void cfg_deinit(struct cfg *cfg)
1021
0
{
1022
0
  if (cfg->priority_strings) {
1023
0
    _name_val_array_clear(&cfg->priority_strings);
1024
0
  }
1025
0
  gnutls_free(cfg->priority_string);
1026
0
  gnutls_free(cfg->default_priority_string);
1027
0
}
1028
1029
/* Lock for reading and writing system_wide_config */
1030
GNUTLS_RWLOCK(system_wide_config_rwlock);
1031
static struct cfg system_wide_config;
1032
1033
static unsigned fail_on_invalid_config = 0;
1034
static const char *system_priority_file = SYSTEM_PRIORITY_FILE;
1035
static time_t system_priority_last_mod = 0;
1036
static unsigned system_priority_file_loaded = 0;
1037
1038
0
#define GLOBAL_SECTION "global"
1039
0
#define CUSTOM_PRIORITY_SECTION "priorities"
1040
0
#define OVERRIDES_SECTION "overrides"
1041
0
#define MAX_ALGO_NAME 2048
1042
1043
bool _gnutls_allowlisting_mode(void)
1044
0
{
1045
0
  return system_wide_config.allowlisting;
1046
0
}
1047
1048
static void _clear_default_system_priority(void)
1049
0
{
1050
0
  gnutls_free(system_wide_config.default_priority_string);
1051
0
  system_wide_config.default_priority_string = NULL;
1052
1053
0
  _gnutls_default_priority_string = DEFAULT_PRIORITY_STRING;
1054
0
}
1055
1056
gnutls_certificate_verification_profiles_t
1057
_gnutls_get_system_wide_verification_profile(void)
1058
0
{
1059
0
  return system_wide_config.verification_profile;
1060
0
}
1061
1062
/* removes spaces */
1063
static char *clear_spaces(const char *str, char out[MAX_ALGO_NAME])
1064
0
{
1065
0
  const char *p = str;
1066
0
  unsigned i = 0;
1067
1068
0
  while (c_isspace(*p))
1069
0
    p++;
1070
1071
0
  while (!c_isspace(*p) && *p != 0) {
1072
0
    out[i++] = *p;
1073
0
    p++;
1074
1075
0
    if (i >= MAX_ALGO_NAME - 1)
1076
0
      break;
1077
0
  }
1078
0
  out[i] = 0;
1079
0
  return out;
1080
0
}
1081
1082
struct ini_ctx {
1083
  struct cfg cfg;
1084
1085
  gnutls_digest_algorithm_t *hashes;
1086
  size_t hashes_size;
1087
  gnutls_sign_algorithm_t *sigs;
1088
  size_t sigs_size;
1089
  gnutls_sign_algorithm_t *sigs_for_cert;
1090
  size_t sigs_for_cert_size;
1091
  gnutls_protocol_t *versions;
1092
  size_t versions_size;
1093
  gnutls_ecc_curve_t *curves;
1094
  size_t curves_size;
1095
};
1096
1097
static inline void ini_ctx_deinit(struct ini_ctx *ctx)
1098
0
{
1099
0
  cfg_deinit(&ctx->cfg);
1100
0
  gnutls_free(ctx->hashes);
1101
0
  gnutls_free(ctx->sigs);
1102
0
  gnutls_free(ctx->sigs_for_cert);
1103
0
  gnutls_free(ctx->versions);
1104
0
  gnutls_free(ctx->curves);
1105
0
}
1106
1107
static inline void cfg_steal(struct cfg *dst, struct cfg *src)
1108
0
{
1109
0
  dst->verification_profile = src->verification_profile;
1110
1111
0
  dst->priority_strings = src->priority_strings;
1112
0
  src->priority_strings = NULL;
1113
1114
0
  dst->priority_string = src->priority_string;
1115
0
  src->priority_string = NULL;
1116
1117
0
  dst->default_priority_string = src->default_priority_string;
1118
0
  src->default_priority_string = NULL;
1119
1120
0
  dst->allowlisting = src->allowlisting;
1121
0
  dst->ktls_enabled = src->ktls_enabled;
1122
0
  dst->force_ext_master_secret = src->force_ext_master_secret;
1123
0
  dst->force_ext_master_secret_set = src->force_ext_master_secret_set;
1124
0
  memcpy(dst->ciphers, src->ciphers, sizeof(src->ciphers));
1125
0
  memcpy(dst->macs, src->macs, sizeof(src->macs));
1126
0
  memcpy(dst->groups, src->groups, sizeof(src->groups));
1127
0
  memcpy(dst->kxs, src->kxs, sizeof(src->kxs));
1128
0
  memcpy(dst->hashes, src->hashes, sizeof(src->hashes));
1129
0
  memcpy(dst->ecc_curves, src->ecc_curves, sizeof(src->ecc_curves));
1130
0
  memcpy(dst->sigs, src->sigs, sizeof(src->sigs));
1131
0
  memcpy(dst->sigs_for_cert, src->sigs_for_cert,
1132
0
         sizeof(src->sigs_for_cert));
1133
0
}
1134
1135
/*
1136
 * synchronizing changes from struct cfg to global `lib/algorithms` arrays
1137
 */
1138
1139
/* global side-effect! modifies `flags` in `hash_algorithms[]` */
1140
static inline int /* allowlisting-only */ _cfg_hashes_remark(struct cfg *cfg)
1141
0
{
1142
0
  size_t i;
1143
0
  _gnutls_digest_mark_insecure_all();
1144
0
  for (i = 0; cfg->hashes[i] != 0; i++) {
1145
0
    int ret = _gnutls_digest_set_secure(cfg->hashes[i], 1);
1146
0
    if (unlikely(ret < 0)) {
1147
0
      return gnutls_assert_val(ret);
1148
0
    }
1149
0
  }
1150
0
  return 0;
1151
0
}
1152
1153
/* global side-effect! modifies `flags` in `sign_algorithms[]` */
1154
static inline int /* allowlisting-only */ _cfg_sigs_remark(struct cfg *cfg)
1155
0
{
1156
0
  size_t i;
1157
0
  _gnutls_sign_mark_insecure_all(_INSECURE);
1158
0
  for (i = 0; cfg->sigs[i] != 0; i++) {
1159
0
    int ret = _gnutls_sign_set_secure(cfg->sigs[i],
1160
0
              _INSECURE_FOR_CERTS);
1161
0
    if (unlikely(ret < 0)) {
1162
0
      return gnutls_assert_val(ret);
1163
0
    }
1164
0
  }
1165
0
  for (i = 0; cfg->sigs_for_cert[i] != 0; i++) {
1166
0
    int ret =
1167
0
      _gnutls_sign_set_secure(cfg->sigs_for_cert[i], _SECURE);
1168
0
    if (unlikely(ret < 0)) {
1169
0
      return gnutls_assert_val(ret);
1170
0
    }
1171
0
  }
1172
0
  return 0;
1173
0
}
1174
1175
/* global side-effect! modifies `supported` in `sup_versions[]` */
1176
static inline int /* allowlisting-only */ _cfg_versions_remark(struct cfg *cfg)
1177
0
{
1178
0
  size_t i;
1179
0
  _gnutls_version_mark_disabled_all();
1180
0
  for (i = 0; cfg->versions[i] != 0; i++) {
1181
0
    int ret = _gnutls_protocol_set_enabled(cfg->versions[i], 1);
1182
0
    if (unlikely(ret < 0)) {
1183
0
      return gnutls_assert_val(ret);
1184
0
    }
1185
0
  }
1186
0
  return 0;
1187
0
}
1188
1189
/* global side-effect! modifies `supported` in `ecc_curves[]` */
1190
static inline int /* allowlisting-only */
1191
_cfg_ecc_curves_remark(struct cfg *cfg)
1192
0
{
1193
0
  size_t i;
1194
0
  _gnutls_ecc_curve_mark_disabled_all();
1195
0
  for (i = 0; cfg->ecc_curves[i] != 0; i++) {
1196
0
    int ret = _gnutls_ecc_curve_set_enabled(cfg->ecc_curves[i], 1);
1197
0
    if (unlikely(ret < 0)) {
1198
0
      return gnutls_assert_val(ret);
1199
0
    }
1200
0
  }
1201
0
  return 0;
1202
0
}
1203
1204
/*
1205
 * setting arrays of struct cfg: from other arrays
1206
 */
1207
1208
static inline int /* allowlisting-only */
1209
cfg_hashes_set_array(struct cfg *cfg, gnutls_digest_algorithm_t *src,
1210
         size_t len)
1211
0
{
1212
0
  if (unlikely(len >= MAX_ALGOS)) {
1213
0
    return gnutls_assert_val(GNUTLS_A_INTERNAL_ERROR);
1214
0
  }
1215
0
  if (len) {
1216
0
    memcpy(cfg->hashes, src,
1217
0
           sizeof(gnutls_digest_algorithm_t) * len);
1218
0
  }
1219
0
  cfg->hashes[len] = 0;
1220
0
  return _cfg_hashes_remark(cfg);
1221
0
}
1222
1223
static inline int /* allowlisting-only */
1224
cfg_sigs_set_arrays(struct cfg *cfg, gnutls_sign_algorithm_t *src, size_t len,
1225
        gnutls_sign_algorithm_t *src_for_cert, size_t len_for_cert)
1226
0
{
1227
0
  if (unlikely(len >= MAX_ALGOS)) {
1228
0
    return gnutls_assert_val(GNUTLS_A_INTERNAL_ERROR);
1229
0
  }
1230
0
  if (unlikely(len_for_cert >= MAX_ALGOS)) {
1231
0
    return gnutls_assert_val(GNUTLS_A_INTERNAL_ERROR);
1232
0
  }
1233
0
  if (len) {
1234
0
    memcpy(cfg->sigs, src, sizeof(gnutls_sign_algorithm_t) * len);
1235
0
  }
1236
0
  if (len_for_cert) {
1237
0
    memcpy(cfg->sigs_for_cert, src_for_cert,
1238
0
           sizeof(gnutls_sign_algorithm_t) * len_for_cert);
1239
0
  }
1240
0
  cfg->sigs[len] = 0;
1241
0
  cfg->sigs_for_cert[len_for_cert] = 0;
1242
0
  return _cfg_sigs_remark(cfg);
1243
0
}
1244
1245
static inline int /* allowlisting-only */
1246
cfg_versions_set_array(struct cfg *cfg, gnutls_protocol_t *src, size_t len)
1247
0
{
1248
0
  if (unlikely(len >= MAX_ALGOS)) {
1249
0
    return gnutls_assert_val(GNUTLS_A_INTERNAL_ERROR);
1250
0
  }
1251
0
  if (len) {
1252
0
    memcpy(cfg->versions, src, sizeof(gnutls_protocol_t) * len);
1253
0
  }
1254
0
  cfg->versions[len] = 0;
1255
0
  return _cfg_versions_remark(cfg);
1256
0
}
1257
1258
static inline int /* allowlisting-only */
1259
cfg_ecc_curves_set_array(struct cfg *cfg, gnutls_ecc_curve_t *src, size_t len)
1260
0
{
1261
0
  if (unlikely(len >= MAX_ALGOS)) {
1262
0
    return gnutls_assert_val(GNUTLS_A_INTERNAL_ERROR);
1263
0
  }
1264
0
  if (len) {
1265
0
    memcpy(cfg->ecc_curves, src, sizeof(gnutls_ecc_curve_t) * len);
1266
0
  }
1267
0
  cfg->ecc_curves[len] = 0;
1268
0
  return _cfg_ecc_curves_remark(cfg);
1269
0
}
1270
1271
/*
1272
 * appending to arrays of struct cfg
1273
 */
1274
1275
/* polymorphic way to DRY this operation. other possible approaches:
1276
 * 1. just unmacro (long)
1277
 * 2. cast to ints and write a function operating on ints
1278
 *    (hacky, every call is +4 lines, needs a portable static assert)
1279
 * 3. macro whole functions, not just this operation (harder to find/read)
1280
 */
1281
#define APPEND_TO_NULL_TERMINATED_ARRAY(dst, element)                      \
1282
0
  do {                                                               \
1283
0
    size_t i;                                                  \
1284
0
    for (i = 0; dst[i] != 0; i++) {                            \
1285
0
      if (dst[i] == element) {                           \
1286
0
        return 0;                                  \
1287
0
      }                                                  \
1288
0
    }                                                          \
1289
0
    if (unlikely(i >= MAX_ALGOS)) {                            \
1290
0
      return gnutls_assert_val(GNUTLS_A_INTERNAL_ERROR); \
1291
0
    }                                                          \
1292
0
    dst[i] = element;                                          \
1293
0
    dst[i + 1] = 0;                                            \
1294
0
  } while (0)
1295
1296
static inline int /* allowlisting-only */
1297
cfg_hashes_add(struct cfg *cfg, gnutls_digest_algorithm_t dig)
1298
0
{
1299
0
  _gnutls_debug_log("cfg: enabling digest algorithm %s\n",
1300
0
        gnutls_digest_get_name(dig));
1301
0
  APPEND_TO_NULL_TERMINATED_ARRAY(cfg->hashes, dig);
1302
0
  return _cfg_hashes_remark(cfg);
1303
0
}
1304
1305
static inline int /* allowlisting-only */
1306
cfg_sigs_add(struct cfg *cfg, gnutls_sign_algorithm_t sig)
1307
0
{
1308
0
  _gnutls_debug_log("cfg: enabling signature algorithm "
1309
0
        "(for non-certificate usage) "
1310
0
        "%s\n",
1311
0
        gnutls_sign_get_name(sig));
1312
0
  APPEND_TO_NULL_TERMINATED_ARRAY(cfg->sigs, sig);
1313
0
  return _cfg_sigs_remark(cfg);
1314
0
}
1315
1316
static inline int /* allowlisting-only */
1317
cfg_sigs_for_cert_add(struct cfg *cfg, gnutls_sign_algorithm_t sig)
1318
0
{
1319
0
  _gnutls_debug_log("cfg: enabling signature algorithm"
1320
0
        "(for certificate usage) "
1321
0
        "%s\n",
1322
0
        gnutls_sign_get_name(sig));
1323
0
  APPEND_TO_NULL_TERMINATED_ARRAY(cfg->sigs_for_cert, sig);
1324
0
  return _cfg_sigs_remark(cfg);
1325
0
}
1326
1327
static inline int /* allowlisting-only */
1328
cfg_versions_add(struct cfg *cfg, gnutls_protocol_t prot)
1329
0
{
1330
0
  _gnutls_debug_log("cfg: enabling version %s\n",
1331
0
        gnutls_protocol_get_name(prot));
1332
0
  APPEND_TO_NULL_TERMINATED_ARRAY(cfg->versions, prot);
1333
0
  return _cfg_versions_remark(cfg);
1334
0
}
1335
1336
static inline int /* allowlisting-only */
1337
cfg_ecc_curves_add(struct cfg *cfg, gnutls_ecc_curve_t curve)
1338
0
{
1339
0
  _gnutls_debug_log("cfg: enabling curve %s\n",
1340
0
        gnutls_ecc_curve_get_name(curve));
1341
0
  APPEND_TO_NULL_TERMINATED_ARRAY(cfg->ecc_curves, curve);
1342
0
  return _cfg_ecc_curves_remark(cfg);
1343
0
}
1344
1345
#undef APPEND_TO_NULL_TERMINATED_ARRAY
1346
1347
/*
1348
 * removing from arrays of struct cfg
1349
 */
1350
1351
/* polymorphic way to DRY this removal, see APPEND_TO_NULL_TERMINATED_ARRAY */
1352
#define REMOVE_FROM_NULL_TERMINATED_ARRAY(dst, element)         \
1353
0
  do {                                                    \
1354
0
    size_t i, j;                                    \
1355
0
    for (i = 0; dst[i] != 0; i++) {                 \
1356
0
      if (dst[i] == element) {                \
1357
0
        for (j = i; dst[j] != 0; j++) { \
1358
0
          dst[j] = dst[j + 1];    \
1359
0
        }                               \
1360
0
      }                                       \
1361
0
    }                                               \
1362
0
  } while (0)
1363
1364
static inline int /* allowlisting-only */
1365
cfg_hashes_remove(struct cfg *cfg, gnutls_digest_algorithm_t dig)
1366
0
{
1367
0
  _gnutls_debug_log("cfg: disabling digest algorithm %s\n",
1368
0
        gnutls_digest_get_name(dig));
1369
0
  REMOVE_FROM_NULL_TERMINATED_ARRAY(cfg->hashes, dig);
1370
0
  return _cfg_hashes_remark(cfg);
1371
0
}
1372
1373
static inline int /* allowlisting-only */
1374
cfg_sigs_remove(struct cfg *cfg, gnutls_sign_algorithm_t sig)
1375
0
{
1376
0
  _gnutls_debug_log("cfg: disabling signature algorithm "
1377
0
        "(for non-certificate usage) "
1378
0
        "%s\n",
1379
0
        gnutls_sign_get_name(sig));
1380
0
  REMOVE_FROM_NULL_TERMINATED_ARRAY(cfg->sigs, sig);
1381
0
  return _cfg_sigs_remark(cfg);
1382
0
}
1383
1384
static inline int /* allowlisting-only */
1385
cfg_sigs_for_cert_remove(struct cfg *cfg, gnutls_sign_algorithm_t sig)
1386
0
{
1387
0
  _gnutls_debug_log("cfg: disabling signature algorithm"
1388
0
        "(for certificate usage) "
1389
0
        "%s\n",
1390
0
        gnutls_sign_get_name(sig));
1391
0
  REMOVE_FROM_NULL_TERMINATED_ARRAY(cfg->sigs_for_cert, sig);
1392
0
  return _cfg_sigs_remark(cfg);
1393
0
}
1394
1395
static inline int /* allowlisting-only */
1396
cfg_versions_remove(struct cfg *cfg, gnutls_protocol_t prot)
1397
0
{
1398
0
  _gnutls_debug_log("cfg: disabling version %s\n",
1399
0
        gnutls_protocol_get_name(prot));
1400
0
  REMOVE_FROM_NULL_TERMINATED_ARRAY(cfg->versions, prot);
1401
0
  return _cfg_versions_remark(cfg);
1402
0
}
1403
1404
static inline int /* allowlisting-only */
1405
cfg_ecc_curves_remove(struct cfg *cfg, gnutls_ecc_curve_t curve)
1406
0
{
1407
0
  _gnutls_debug_log("cfg: disabling curve %s\n",
1408
0
        gnutls_ecc_curve_get_name(curve));
1409
0
  REMOVE_FROM_NULL_TERMINATED_ARRAY(cfg->ecc_curves, curve);
1410
0
  return _cfg_ecc_curves_remark(cfg);
1411
0
}
1412
1413
static inline int cfg_apply(struct cfg *cfg, struct ini_ctx *ctx)
1414
0
{
1415
0
  size_t i;
1416
0
  int ret;
1417
1418
0
  cfg_steal(cfg, &ctx->cfg);
1419
1420
0
  if (cfg->default_priority_string) {
1421
0
    _gnutls_default_priority_string = cfg->default_priority_string;
1422
0
  }
1423
1424
0
  if (cfg->allowlisting) {
1425
    /* also updates `flags` of global `hash_algorithms[]` */
1426
0
    ret = cfg_hashes_set_array(cfg, ctx->hashes, ctx->hashes_size);
1427
0
    if (unlikely(ret < 0)) {
1428
0
      return gnutls_assert_val(ret);
1429
0
    }
1430
    /* also updates `flags` of global `sign_algorithms[]` */
1431
0
    ret = cfg_sigs_set_arrays(cfg, ctx->sigs, ctx->sigs_size,
1432
0
            ctx->sigs_for_cert,
1433
0
            ctx->sigs_for_cert_size);
1434
0
    if (unlikely(ret < 0)) {
1435
0
      return gnutls_assert_val(ret);
1436
0
    }
1437
    /* also updates `supported` field of global `sup_versions[]` */
1438
0
    ret = cfg_versions_set_array(cfg, ctx->versions,
1439
0
               ctx->versions_size);
1440
0
    if (unlikely(ret < 0)) {
1441
0
      return gnutls_assert_val(ret);
1442
0
    }
1443
    /* also updates `supported` field of global `ecc_curves[]` */
1444
0
    ret = cfg_ecc_curves_set_array(cfg, ctx->curves,
1445
0
                 ctx->curves_size);
1446
0
    if (unlikely(ret < 0)) {
1447
0
      return gnutls_assert_val(ret);
1448
0
    }
1449
0
  } else {
1450
    /* updates same global arrays as above, but doesn't store
1451
     * the algorithms into the `struct cfg` as allowlisting does.
1452
     * blocklisting doesn't allow relaxing the restrictions */
1453
0
    for (i = 0; i < ctx->hashes_size; i++) {
1454
0
      ret = _gnutls_digest_mark_insecure(ctx->hashes[i]);
1455
0
      if (unlikely(ret < 0)) {
1456
0
        return ret;
1457
0
      }
1458
0
    }
1459
0
    for (i = 0; i < ctx->sigs_size; i++) {
1460
0
      ret = _gnutls_sign_mark_insecure(ctx->sigs[i],
1461
0
               _INSECURE);
1462
0
      if (unlikely(ret < 0)) {
1463
0
        return ret;
1464
0
      }
1465
0
    }
1466
0
    for (i = 0; i < ctx->sigs_for_cert_size; i++) {
1467
0
      ret = _gnutls_sign_mark_insecure(ctx->sigs_for_cert[i],
1468
0
               _INSECURE_FOR_CERTS);
1469
0
      if (unlikely(ret < 0)) {
1470
0
        return ret;
1471
0
      }
1472
0
    }
1473
0
    for (i = 0; i < ctx->versions_size; i++) {
1474
0
      ret = _gnutls_version_mark_disabled(ctx->versions[i]);
1475
0
      if (unlikely(ret < 0)) {
1476
0
        return ret;
1477
0
      }
1478
0
    }
1479
0
    for (i = 0; i < ctx->curves_size; i++) {
1480
0
      ret = _gnutls_ecc_curve_mark_disabled(ctx->curves[i]);
1481
0
      if (unlikely(ret < 0)) {
1482
0
        return ret;
1483
0
      }
1484
0
    }
1485
0
  }
1486
1487
0
  return 0;
1488
0
}
1489
1490
/* This function parses the global section of the configuration file.
1491
 */
1492
static int global_ini_handler(void *ctx, const char *section, const char *name,
1493
            const char *value)
1494
0
{
1495
0
  char *p;
1496
0
  char str[MAX_ALGO_NAME];
1497
0
  struct cfg *cfg = ctx;
1498
1499
0
  if (section != NULL && c_strcasecmp(section, GLOBAL_SECTION) == 0) {
1500
0
    if (c_strcasecmp(name, "override-mode") == 0) {
1501
0
      p = clear_spaces(value, str);
1502
0
      if (c_strcasecmp(p, "allowlist") == 0) {
1503
0
        cfg->allowlisting = true;
1504
0
      } else if (c_strcasecmp(p, "blocklist") == 0) {
1505
0
        cfg->allowlisting = false;
1506
0
      } else {
1507
0
        _gnutls_debug_log(
1508
0
          "cfg: unknown override mode %s\n", p);
1509
0
        if (fail_on_invalid_config)
1510
0
          return 0;
1511
0
      }
1512
0
    } else if (c_strcasecmp(name, "ktls") == 0) {
1513
0
      p = clear_spaces(value, str);
1514
0
      if (c_strcasecmp(p, "true") == 0) {
1515
0
        cfg->ktls_enabled = true;
1516
0
      } else if (c_strcasecmp(p, "false") == 0) {
1517
0
        cfg->ktls_enabled = false;
1518
0
      } else {
1519
0
        _gnutls_debug_log("cfg: unknown ktls mode %s\n",
1520
0
              p);
1521
0
        if (fail_on_invalid_config)
1522
0
          return 0;
1523
0
      }
1524
0
    } else {
1525
0
      _gnutls_debug_log("unknown parameter %s\n", name);
1526
0
      if (fail_on_invalid_config)
1527
0
        return 0;
1528
0
    }
1529
0
  }
1530
1531
0
  return 1;
1532
0
}
1533
1534
static bool override_allowed(bool allowlisting, const char *name)
1535
0
{
1536
0
  static const struct {
1537
0
    const char *allowlist_name;
1538
0
    const char *blocklist_name;
1539
0
  } names[] = { { "secure-hash", "insecure-hash" },
1540
0
          { "secure-sig", "insecure-sig" },
1541
0
          { "secure-sig-for-cert", "insecure-sig-for-cert" },
1542
0
          { "enabled-version", "disabled-version" },
1543
0
          { "enabled-curve", "disabled-curve" },
1544
0
          { "tls-enabled-cipher", "tls-disabled-cipher" },
1545
0
          { "tls-enabled-group", "tls-disabled-group" },
1546
0
          { "tls-enabled-kx", "tls-disabled-kx" },
1547
0
          { "tls-enabled-mac", "tls-disabled-mac" } };
1548
0
  size_t i;
1549
1550
0
  for (i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
1551
0
    if (c_strcasecmp(name, allowlisting ?
1552
0
                 names[i].blocklist_name :
1553
0
                 names[i].allowlist_name) == 0)
1554
0
      return false;
1555
0
  }
1556
1557
0
  return true;
1558
0
}
1559
1560
/* This function parses a gnutls configuration file.  Updating internal settings
1561
 * according to the parsed configuration is done by cfg_apply.
1562
 */
1563
static int cfg_ini_handler(void *_ctx, const char *section, const char *name,
1564
         const char *value)
1565
0
{
1566
0
  char *p;
1567
0
  int ret;
1568
0
  unsigned i;
1569
0
  char str[MAX_ALGO_NAME];
1570
0
  struct ini_ctx *ctx = _ctx;
1571
0
  struct cfg *cfg = &ctx->cfg;
1572
1573
  /* Note that we intentionally overwrite the value above; inih does
1574
   * not use that value after we handle it. */
1575
1576
  /* Parse sections */
1577
0
  if (section == NULL || section[0] == 0 ||
1578
0
      c_strcasecmp(section, CUSTOM_PRIORITY_SECTION) == 0) {
1579
0
    _gnutls_debug_log("cfg: adding priority: %s -> %s\n", name,
1580
0
          value);
1581
1582
0
    ret = _name_val_array_append(&cfg->priority_strings, name,
1583
0
               value);
1584
0
    if (ret < 0)
1585
0
      return 0;
1586
0
  } else if (c_strcasecmp(section, OVERRIDES_SECTION) == 0) {
1587
0
    if (!override_allowed(cfg->allowlisting, name)) {
1588
0
      _gnutls_debug_log(
1589
0
        "cfg: %s is not allowed in this mode\n", name);
1590
0
      if (fail_on_invalid_config)
1591
0
        return 0;
1592
0
    } else if (c_strcasecmp(name, "default-priority-string") == 0) {
1593
0
      if (cfg->default_priority_string) {
1594
0
        gnutls_free(cfg->default_priority_string);
1595
0
        cfg->default_priority_string = NULL;
1596
0
      }
1597
0
      p = clear_spaces(value, str);
1598
0
      _gnutls_debug_log(
1599
0
        "cfg: setting default-priority-string to %s\n",
1600
0
        p);
1601
0
      if (strlen(p) > 0) {
1602
0
        cfg->default_priority_string = gnutls_strdup(p);
1603
0
        if (!cfg->default_priority_string) {
1604
0
          _gnutls_debug_log(
1605
0
            "cfg: failed setting default-priority-string\n");
1606
0
          return 0;
1607
0
        }
1608
0
      } else {
1609
0
        _gnutls_debug_log(
1610
0
          "cfg: empty default-priority-string, using default\n");
1611
0
        if (fail_on_invalid_config)
1612
0
          return 0;
1613
0
      }
1614
0
    } else if (c_strcasecmp(name, "insecure-hash") == 0 ||
1615
0
         c_strcasecmp(name, "secure-hash") == 0) {
1616
0
      gnutls_digest_algorithm_t dig, *tmp;
1617
1618
0
      p = clear_spaces(value, str);
1619
1620
0
      if (cfg->allowlisting) {
1621
0
        _gnutls_debug_log(
1622
0
          "cfg: marking hash %s as secure\n", p);
1623
0
      } else {
1624
0
        _gnutls_debug_log(
1625
0
          "cfg: marking hash %s as insecure\n",
1626
0
          p);
1627
0
      }
1628
1629
0
      dig = gnutls_digest_get_id(p);
1630
0
      if (dig == GNUTLS_DIG_UNKNOWN) {
1631
0
        _gnutls_debug_log(
1632
0
          "cfg: found unknown hash %s in %s\n", p,
1633
0
          name);
1634
0
        if (fail_on_invalid_config)
1635
0
          return 0;
1636
0
        goto exit;
1637
0
      }
1638
0
      tmp = _gnutls_reallocarray(
1639
0
        ctx->hashes, ctx->hashes_size + 1,
1640
0
        sizeof(gnutls_digest_algorithm_t));
1641
0
      if (!tmp) {
1642
0
        if (cfg->allowlisting) {
1643
0
          _gnutls_debug_log(
1644
0
            "cfg: failed marking hash %s as secure\n",
1645
0
            p);
1646
0
        } else {
1647
0
          _gnutls_debug_log(
1648
0
            "cfg: failed marking hash %s as insecure\n",
1649
0
            p);
1650
0
        }
1651
0
        if (fail_on_invalid_config)
1652
0
          return 0;
1653
0
        goto exit;
1654
0
      }
1655
1656
0
      ctx->hashes = tmp;
1657
0
      ctx->hashes[ctx->hashes_size] = dig;
1658
0
      ctx->hashes_size++;
1659
0
    } else if (c_strcasecmp(name, "insecure-sig") == 0 ||
1660
0
         c_strcasecmp(name, "secure-sig") == 0) {
1661
0
      gnutls_sign_algorithm_t sig, *tmp;
1662
1663
0
      p = clear_spaces(value, str);
1664
1665
0
      if (cfg->allowlisting) {
1666
0
        _gnutls_debug_log(
1667
0
          "cfg: marking signature %s as secure\n",
1668
0
          p);
1669
0
      } else {
1670
0
        _gnutls_debug_log(
1671
0
          "cfg: marking signature %s as insecure\n",
1672
0
          p);
1673
0
      }
1674
1675
0
      sig = gnutls_sign_get_id(p);
1676
0
      if (sig == GNUTLS_SIGN_UNKNOWN) {
1677
0
        _gnutls_debug_log(
1678
0
          "cfg: found unknown signature algorithm %s in %s\n",
1679
0
          p, name);
1680
0
        if (fail_on_invalid_config)
1681
0
          return 0;
1682
0
        goto exit;
1683
0
      }
1684
0
      tmp = _gnutls_reallocarray(
1685
0
        ctx->sigs, ctx->sigs_size + 1,
1686
0
        sizeof(gnutls_sign_algorithm_t));
1687
0
      if (!tmp) {
1688
0
        if (cfg->allowlisting) {
1689
0
          _gnutls_debug_log(
1690
0
            "cfg: failed marking signature %s as secure\n",
1691
0
            p);
1692
0
        } else {
1693
0
          _gnutls_debug_log(
1694
0
            "cfg: failed marking signature %s as insecure\n",
1695
0
            p);
1696
0
        }
1697
0
        if (fail_on_invalid_config)
1698
0
          return 0;
1699
0
        goto exit;
1700
0
      }
1701
1702
0
      ctx->sigs = tmp;
1703
0
      ctx->sigs[ctx->sigs_size] = sig;
1704
0
      ctx->sigs_size++;
1705
0
    } else if (c_strcasecmp(name, "insecure-sig-for-cert") == 0 ||
1706
0
         c_strcasecmp(name, "secure-sig-for-cert") == 0) {
1707
0
      gnutls_sign_algorithm_t sig, *tmp;
1708
1709
0
      p = clear_spaces(value, str);
1710
1711
0
      if (cfg->allowlisting) {
1712
0
        _gnutls_debug_log(
1713
0
          "cfg: marking signature %s as secure for certs\n",
1714
0
          p);
1715
0
      } else {
1716
0
        _gnutls_debug_log(
1717
0
          "cfg: marking signature %s as insecure for certs\n",
1718
0
          p);
1719
0
      }
1720
1721
0
      sig = gnutls_sign_get_id(p);
1722
0
      if (sig == GNUTLS_SIGN_UNKNOWN) {
1723
0
        _gnutls_debug_log(
1724
0
          "cfg: found unknown signature algorithm %s in %s\n",
1725
0
          p, name);
1726
0
        if (fail_on_invalid_config)
1727
0
          return 0;
1728
0
        goto exit;
1729
0
      }
1730
0
      tmp = _gnutls_reallocarray(
1731
0
        ctx->sigs_for_cert, ctx->sigs_for_cert_size + 1,
1732
0
        sizeof(gnutls_sign_algorithm_t));
1733
0
      if (!tmp) {
1734
0
        if (cfg->allowlisting) {
1735
0
          _gnutls_debug_log(
1736
0
            "cfg: failed marking signature %s as secure for certs\n",
1737
0
            p);
1738
0
        } else {
1739
0
          _gnutls_debug_log(
1740
0
            "cfg: failed marking signature %s as insecure for certs\n",
1741
0
            p);
1742
0
        }
1743
0
        if (fail_on_invalid_config)
1744
0
          return 0;
1745
0
        goto exit;
1746
0
      }
1747
1748
0
      ctx->sigs_for_cert = tmp;
1749
0
      ctx->sigs_for_cert[ctx->sigs_for_cert_size] = sig;
1750
0
      ctx->sigs_for_cert_size++;
1751
0
    } else if (c_strcasecmp(name, "disabled-version") == 0 ||
1752
0
         c_strcasecmp(name, "enabled-version") == 0) {
1753
0
      gnutls_protocol_t prot, *tmp;
1754
1755
0
      p = clear_spaces(value, str);
1756
1757
0
      if (cfg->allowlisting) {
1758
0
        _gnutls_debug_log("cfg: enabling version %s\n",
1759
0
              p);
1760
0
      } else {
1761
0
        _gnutls_debug_log("cfg: disabling version %s\n",
1762
0
              p);
1763
0
      }
1764
1765
0
      prot = gnutls_protocol_get_id(p);
1766
0
      if (prot == GNUTLS_VERSION_UNKNOWN) {
1767
0
        _gnutls_debug_log(
1768
0
          "cfg: found unknown version %s in %s\n",
1769
0
          p, name);
1770
0
        if (fail_on_invalid_config)
1771
0
          return 0;
1772
0
        goto exit;
1773
0
      }
1774
0
      tmp = _gnutls_reallocarray(ctx->versions,
1775
0
               ctx->versions_size + 1,
1776
0
               sizeof(gnutls_protocol_t));
1777
0
      if (!tmp) {
1778
0
        if (cfg->allowlisting) {
1779
0
          _gnutls_debug_log(
1780
0
            "cfg: failed enabling version %s\n",
1781
0
            p);
1782
0
        } else {
1783
0
          _gnutls_debug_log(
1784
0
            "cfg: failed disabling version %s\n",
1785
0
            p);
1786
0
        }
1787
0
        if (fail_on_invalid_config)
1788
0
          return 0;
1789
0
        goto exit;
1790
0
      }
1791
1792
0
      ctx->versions = tmp;
1793
0
      ctx->versions[ctx->versions_size] = prot;
1794
0
      ctx->versions_size++;
1795
0
    } else if (c_strcasecmp(name, "disabled-curve") == 0 ||
1796
0
         c_strcasecmp(name, "enabled-curve") == 0) {
1797
0
      gnutls_ecc_curve_t curve, *tmp;
1798
1799
0
      p = clear_spaces(value, str);
1800
1801
0
      if (cfg->allowlisting) {
1802
0
        _gnutls_debug_log("cfg: enabling curve %s\n",
1803
0
              p);
1804
0
      } else {
1805
0
        _gnutls_debug_log("cfg: disabling curve %s\n",
1806
0
              p);
1807
0
      }
1808
1809
0
      curve = gnutls_ecc_curve_get_id(p);
1810
0
      if (curve == GNUTLS_ECC_CURVE_INVALID) {
1811
0
        _gnutls_debug_log(
1812
0
          "cfg: found unknown curve %s in %s\n",
1813
0
          p, name);
1814
0
        if (fail_on_invalid_config)
1815
0
          return 0;
1816
0
        goto exit;
1817
0
      }
1818
0
      tmp = _gnutls_reallocarray(ctx->curves,
1819
0
               ctx->curves_size + 1,
1820
0
               sizeof(gnutls_ecc_curve_t));
1821
0
      if (!tmp) {
1822
0
        if (cfg->allowlisting) {
1823
0
          _gnutls_debug_log(
1824
0
            "cfg: failed enabling curve %s\n",
1825
0
            p);
1826
0
        } else {
1827
0
          _gnutls_debug_log(
1828
0
            "cfg: failed disabling curve %s\n",
1829
0
            p);
1830
0
        }
1831
0
        if (fail_on_invalid_config)
1832
0
          return 0;
1833
0
        goto exit;
1834
0
      }
1835
1836
0
      ctx->curves = tmp;
1837
0
      ctx->curves[ctx->curves_size] = curve;
1838
0
      ctx->curves_size++;
1839
0
    } else if (c_strcasecmp(name, "min-verification-profile") ==
1840
0
         0) {
1841
0
      gnutls_certificate_verification_profiles_t profile;
1842
0
      profile =
1843
0
        gnutls_certificate_verification_profile_get_id(
1844
0
          value);
1845
1846
0
      if (profile == GNUTLS_PROFILE_UNKNOWN) {
1847
0
        _gnutls_debug_log(
1848
0
          "cfg: found unknown profile %s in %s\n",
1849
0
          value, name);
1850
0
        if (fail_on_invalid_config)
1851
0
          return 0;
1852
0
        goto exit;
1853
0
      }
1854
1855
0
      cfg->verification_profile = profile;
1856
0
    } else if (c_strcasecmp(name, "tls-disabled-cipher") == 0 ||
1857
0
         c_strcasecmp(name, "tls-enabled-cipher") == 0) {
1858
0
      gnutls_cipher_algorithm_t algo;
1859
1860
0
      p = clear_spaces(value, str);
1861
1862
0
      if (cfg->allowlisting) {
1863
0
        _gnutls_debug_log(
1864
0
          "cfg: enabling cipher %s for TLS\n", p);
1865
0
      } else {
1866
0
        _gnutls_debug_log(
1867
0
          "cfg: disabling cipher %s for TLS\n",
1868
0
          p);
1869
0
      }
1870
1871
0
      algo = gnutls_cipher_get_id(p);
1872
0
      if (algo == GNUTLS_CIPHER_UNKNOWN) {
1873
0
        _gnutls_debug_log(
1874
0
          "cfg: unknown algorithm %s listed at %s\n",
1875
0
          p, name);
1876
0
        if (fail_on_invalid_config)
1877
0
          return 0;
1878
0
        goto exit;
1879
0
      }
1880
1881
0
      i = 0;
1882
0
      while (cfg->ciphers[i] != 0)
1883
0
        i++;
1884
1885
0
      if (i > MAX_ALGOS - 1) {
1886
0
        if (cfg->allowlisting) {
1887
0
          _gnutls_debug_log(
1888
0
            "cfg: too many (%d) enabled ciphers from %s\n",
1889
0
            i, name);
1890
0
        } else {
1891
0
          _gnutls_debug_log(
1892
0
            "cfg: too many (%d) disabled ciphers from %s\n",
1893
0
            i, name);
1894
0
        }
1895
0
        if (fail_on_invalid_config)
1896
0
          return 0;
1897
0
        goto exit;
1898
0
      }
1899
0
      cfg->ciphers[i] = algo;
1900
0
      cfg->ciphers[i + 1] = 0;
1901
1902
0
    } else if (c_strcasecmp(name, "tls-disabled-mac") == 0 ||
1903
0
         c_strcasecmp(name, "tls-enabled-mac") == 0) {
1904
0
      gnutls_mac_algorithm_t algo;
1905
1906
0
      p = clear_spaces(value, str);
1907
1908
0
      if (cfg->allowlisting) {
1909
0
        _gnutls_debug_log(
1910
0
          "cfg: enabling MAC %s for TLS\n", p);
1911
0
      } else {
1912
0
        _gnutls_debug_log(
1913
0
          "cfg: disabling MAC %s for TLS\n", p);
1914
0
      }
1915
1916
0
      algo = gnutls_mac_get_id(p);
1917
0
      if (algo == 0) {
1918
0
        _gnutls_debug_log(
1919
0
          "cfg: unknown algorithm %s listed at %s\n",
1920
0
          p, name);
1921
0
        if (fail_on_invalid_config)
1922
0
          return 0;
1923
0
        goto exit;
1924
0
      }
1925
1926
0
      i = 0;
1927
0
      while (cfg->macs[i] != 0)
1928
0
        i++;
1929
1930
0
      if (i > MAX_ALGOS - 1) {
1931
0
        if (cfg->allowlisting) {
1932
0
          _gnutls_debug_log(
1933
0
            "cfg: too many (%d) enabled MACs from %s\n",
1934
0
            i, name);
1935
0
        } else {
1936
0
          _gnutls_debug_log(
1937
0
            "cfg: too many (%d) disabled MACs from %s\n",
1938
0
            i, name);
1939
0
        }
1940
0
        if (fail_on_invalid_config)
1941
0
          return 0;
1942
0
        goto exit;
1943
0
      }
1944
0
      cfg->macs[i] = algo;
1945
0
      cfg->macs[i + 1] = 0;
1946
0
    } else if (c_strcasecmp(name, "tls-disabled-group") == 0 ||
1947
0
         c_strcasecmp(name, "tls-enabled-group") == 0) {
1948
0
      gnutls_group_t algo;
1949
1950
0
      p = clear_spaces(value, str);
1951
1952
0
      if (c_strncasecmp(p, "GROUP-", 6) == 0)
1953
0
        p += 6;
1954
1955
0
      if (cfg->allowlisting) {
1956
0
        _gnutls_debug_log(
1957
0
          "cfg: enabling group %s for TLS\n", p);
1958
0
      } else {
1959
0
        _gnutls_debug_log(
1960
0
          "cfg: disabling group %s for TLS\n", p);
1961
0
      }
1962
1963
0
      algo = _gnutls_group_get_id(p);
1964
0
      if (algo == 0) {
1965
0
        _gnutls_debug_log(
1966
0
          "cfg: unknown group %s listed at %s\n",
1967
0
          p, name);
1968
0
        if (fail_on_invalid_config)
1969
0
          return 0;
1970
0
        goto exit;
1971
0
      }
1972
1973
0
      i = 0;
1974
0
      while (cfg->groups[i] != 0)
1975
0
        i++;
1976
1977
0
      if (i > MAX_ALGOS - 1) {
1978
0
        if (cfg->allowlisting) {
1979
0
          _gnutls_debug_log(
1980
0
            "cfg: too many (%d) enabled groups from %s\n",
1981
0
            i, name);
1982
0
        } else {
1983
0
          _gnutls_debug_log(
1984
0
            "cfg: too many (%d) disabled groups from %s\n",
1985
0
            i, name);
1986
0
        }
1987
0
        if (fail_on_invalid_config)
1988
0
          return 0;
1989
0
        goto exit;
1990
0
      }
1991
0
      cfg->groups[i] = algo;
1992
0
      cfg->groups[i + 1] = 0;
1993
0
    } else if (c_strcasecmp(name, "tls-disabled-kx") == 0 ||
1994
0
         c_strcasecmp(name, "tls-enabled-kx") == 0) {
1995
0
      unsigned algo;
1996
1997
0
      p = clear_spaces(value, str);
1998
1999
0
      if (cfg->allowlisting) {
2000
0
        _gnutls_debug_log(
2001
0
          "cfg: enabling key exchange %s for TLS\n",
2002
0
          p);
2003
0
      } else {
2004
0
        _gnutls_debug_log(
2005
0
          "cfg: disabling key exchange %s for TLS\n",
2006
0
          p);
2007
0
      }
2008
2009
0
      algo = gnutls_kx_get_id(p);
2010
0
      if (algo == 0) {
2011
0
        _gnutls_debug_log(
2012
0
          "cfg: unknown key exchange %s listed at %s\n",
2013
0
          p, name);
2014
0
        if (fail_on_invalid_config)
2015
0
          return 0;
2016
0
        goto exit;
2017
0
      }
2018
2019
0
      i = 0;
2020
0
      while (cfg->kxs[i] != 0)
2021
0
        i++;
2022
2023
0
      if (i > MAX_ALGOS - 1) {
2024
0
        if (cfg->allowlisting) {
2025
0
          _gnutls_debug_log(
2026
0
            "cfg: too many (%d) enabled key exchanges from %s\n",
2027
0
            i, name);
2028
0
        } else {
2029
0
          _gnutls_debug_log(
2030
0
            "cfg: too many (%d) disabled key exchanges from %s\n",
2031
0
            i, name);
2032
0
        }
2033
0
        if (fail_on_invalid_config)
2034
0
          return 0;
2035
0
        goto exit;
2036
0
      }
2037
0
      cfg->kxs[i] = algo;
2038
0
      cfg->kxs[i + 1] = 0;
2039
0
    } else if (c_strcasecmp(name, "tls-session-hash") == 0) {
2040
0
      if (c_strcasecmp(value, "request") == 0) {
2041
0
        cfg->force_ext_master_secret = EMS_REQUEST;
2042
0
        cfg->force_ext_master_secret_set = true;
2043
0
      } else if (c_strcasecmp(value, "require") == 0) {
2044
0
        cfg->force_ext_master_secret = EMS_REQUIRE;
2045
0
        cfg->force_ext_master_secret_set = true;
2046
0
      } else {
2047
0
        _gnutls_debug_log(
2048
0
          "cfg: unknown value for %s: %s\n", name,
2049
0
          value);
2050
0
        if (fail_on_invalid_config)
2051
0
          return 0;
2052
0
        goto exit;
2053
0
      }
2054
0
    } else {
2055
0
      _gnutls_debug_log("unknown parameter %s\n", name);
2056
0
      if (fail_on_invalid_config)
2057
0
        return 0;
2058
0
    }
2059
0
  } else if (c_strcasecmp(section, GLOBAL_SECTION) != 0) {
2060
0
    _gnutls_debug_log("cfg: unknown section %s\n", section);
2061
0
    if (fail_on_invalid_config)
2062
0
      return 0;
2063
0
  }
2064
2065
0
exit:
2066
0
  return 1;
2067
0
}
2068
2069
static int /* not locking system_wide_config */
2070
construct_system_wide_priority_string(gnutls_buffer_st *buf)
2071
0
{
2072
0
  int ret;
2073
0
  size_t i;
2074
2075
0
  _gnutls_buffer_init(buf);
2076
2077
0
  ret = _gnutls_buffer_append_str(buf, "NONE");
2078
0
  if (ret < 0) {
2079
0
    _gnutls_buffer_clear(buf);
2080
0
    return ret;
2081
0
  }
2082
2083
0
  for (i = 0; system_wide_config.kxs[i] != 0; i++) {
2084
0
    ret = _gnutls_buffer_append_str(buf, ":+");
2085
0
    if (ret < 0) {
2086
0
      _gnutls_buffer_clear(buf);
2087
0
      return ret;
2088
0
    }
2089
2090
0
    ret = _gnutls_buffer_append_str(
2091
0
      buf, gnutls_kx_get_name(system_wide_config.kxs[i]));
2092
0
    if (ret < 0) {
2093
0
      _gnutls_buffer_clear(buf);
2094
0
      return ret;
2095
0
    }
2096
0
  }
2097
2098
0
  for (i = 0; system_wide_config.groups[i] != 0; i++) {
2099
0
    ret = _gnutls_buffer_append_str(buf, ":+GROUP-");
2100
0
    if (ret < 0) {
2101
0
      _gnutls_buffer_clear(buf);
2102
0
      return ret;
2103
0
    }
2104
2105
0
    ret = _gnutls_buffer_append_str(
2106
0
      buf,
2107
0
      gnutls_group_get_name(system_wide_config.groups[i]));
2108
0
    if (ret < 0) {
2109
0
      _gnutls_buffer_clear(buf);
2110
0
      return ret;
2111
0
    }
2112
0
  }
2113
2114
0
  for (i = 0; system_wide_config.ciphers[i] != 0; i++) {
2115
0
    ret = _gnutls_buffer_append_str(buf, ":+");
2116
0
    if (ret < 0) {
2117
0
      _gnutls_buffer_clear(buf);
2118
0
      return ret;
2119
0
    }
2120
2121
0
    ret = _gnutls_buffer_append_str(
2122
0
      buf,
2123
0
      gnutls_cipher_get_name(system_wide_config.ciphers[i]));
2124
0
    if (ret < 0) {
2125
0
      _gnutls_buffer_clear(buf);
2126
0
      return ret;
2127
0
    }
2128
0
  }
2129
2130
0
  for (i = 0; system_wide_config.macs[i] != 0; i++) {
2131
0
    ret = _gnutls_buffer_append_str(buf, ":+");
2132
0
    if (ret < 0) {
2133
0
      _gnutls_buffer_clear(buf);
2134
0
      return ret;
2135
0
    }
2136
2137
0
    ret = _gnutls_buffer_append_str(
2138
0
      buf, gnutls_mac_get_name(system_wide_config.macs[i]));
2139
0
    if (ret < 0) {
2140
0
      _gnutls_buffer_clear(buf);
2141
0
      return ret;
2142
0
    }
2143
0
  }
2144
2145
0
  for (i = 0; system_wide_config.sigs[i] != 0; i++) {
2146
0
    ret = _gnutls_buffer_append_str(buf, ":+SIGN-");
2147
0
    if (ret < 0) {
2148
0
      _gnutls_buffer_clear(buf);
2149
0
      return ret;
2150
0
    }
2151
2152
0
    ret = _gnutls_buffer_append_str(
2153
0
      buf, gnutls_sign_get_name(system_wide_config.sigs[i]));
2154
0
    if (ret < 0) {
2155
0
      _gnutls_buffer_clear(buf);
2156
0
      return ret;
2157
0
    }
2158
0
  }
2159
2160
0
  for (i = 0; system_wide_config.versions[i] != 0; i++) {
2161
0
    ret = _gnutls_buffer_append_str(buf, ":+VERS-");
2162
0
    if (ret < 0) {
2163
0
      _gnutls_buffer_clear(buf);
2164
0
      return ret;
2165
0
    }
2166
2167
0
    ret = _gnutls_buffer_append_str(
2168
0
      buf, gnutls_protocol_get_name(
2169
0
             system_wide_config.versions[i]));
2170
0
    if (ret < 0) {
2171
0
      _gnutls_buffer_clear(buf);
2172
0
      return ret;
2173
0
    }
2174
0
  }
2175
0
  return 0;
2176
0
}
2177
2178
static int /* not locking system_wide_config */
2179
update_system_wide_priority_string(void)
2180
0
{
2181
  /* doesn't do locking, _gnutls_update_system_priorities does */
2182
0
  gnutls_buffer_st buf;
2183
0
  int ret;
2184
2185
0
  ret = construct_system_wide_priority_string(&buf);
2186
0
  if (ret < 0) {
2187
0
    _gnutls_debug_log("cfg: unable to construct "
2188
0
          "system-wide priority string: %s",
2189
0
          gnutls_strerror(ret));
2190
0
    _gnutls_buffer_clear(&buf);
2191
0
    return ret;
2192
0
  }
2193
2194
0
  gnutls_free(system_wide_config.priority_string);
2195
0
  system_wide_config.priority_string = gnutls_strdup((char *)buf.data);
2196
0
  _gnutls_buffer_clear(&buf);
2197
2198
0
  return 0;
2199
0
}
2200
2201
static int _gnutls_update_system_priorities(bool defer_system_wide)
2202
2
{
2203
2
  int ret, err = 0;
2204
2
  struct stat sb;
2205
2
  FILE *fp;
2206
2
  gnutls_buffer_st buf;
2207
2
  struct ini_ctx ctx;
2208
2209
2
  ret = gnutls_rwlock_rdlock(&system_wide_config_rwlock);
2210
2
  if (ret < 0) {
2211
0
    return gnutls_assert_val(ret);
2212
0
  }
2213
2214
2
  if (stat(system_priority_file, &sb) < 0) {
2215
2
    _gnutls_debug_log("cfg: unable to access: %s: %d\n",
2216
2
          system_priority_file, errno);
2217
2
    goto out;
2218
2
  }
2219
2220
0
  if (system_priority_file_loaded &&
2221
0
      system_priority_last_mod == sb.st_mtime) {
2222
0
    _gnutls_debug_log("cfg: system priority %s has not changed\n",
2223
0
          system_priority_file);
2224
0
    if (system_wide_config.priority_string) {
2225
0
      goto out; /* nothing to do */
2226
0
    }
2227
0
  }
2228
2229
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
2230
2231
0
  ret = gnutls_rwlock_wrlock(&system_wide_config_rwlock);
2232
0
  if (ret < 0) {
2233
0
    return gnutls_assert_val(ret);
2234
0
  }
2235
2236
  /* Another thread could have successfully re-read system-wide config,
2237
   * skip re-reading if the mtime it has used is exactly the same.
2238
   */
2239
0
  if (system_priority_file_loaded) {
2240
0
    system_priority_file_loaded =
2241
0
      (system_priority_last_mod == sb.st_mtime);
2242
0
  }
2243
2244
0
  if (!system_priority_file_loaded) {
2245
0
    _name_val_array_clear(&system_wide_config.priority_strings);
2246
2247
0
    gnutls_free(system_wide_config.priority_string);
2248
0
    system_wide_config.priority_string = NULL;
2249
2250
0
    fp = fopen(system_priority_file, "re");
2251
0
    if (fp == NULL) {
2252
0
      _gnutls_debug_log("cfg: unable to open: %s: %d\n",
2253
0
            system_priority_file, errno);
2254
0
      goto out;
2255
0
    }
2256
    /* Parsing the configuration file needs to be done in 2 phases:
2257
     * first parsing the [global] section
2258
     * and then the other sections,
2259
     * because the [global] section modifies the parsing behavior.
2260
     */
2261
0
    memset(&ctx, 0, sizeof(ctx));
2262
0
    err = ini_parse_file(fp, global_ini_handler, &ctx);
2263
0
    if (!err) {
2264
0
      if (fseek(fp, 0L, SEEK_SET) < 0) {
2265
0
        _gnutls_debug_log("cfg: unable to rewind: %s\n",
2266
0
              system_priority_file);
2267
0
        if (fail_on_invalid_config)
2268
0
          exit(1);
2269
0
      }
2270
0
      err = ini_parse_file(fp, cfg_ini_handler, &ctx);
2271
0
    }
2272
0
    fclose(fp);
2273
0
    if (err) {
2274
0
      ini_ctx_deinit(&ctx);
2275
0
      _gnutls_debug_log("cfg: unable to parse: %s: %d\n",
2276
0
            system_priority_file, err);
2277
0
      goto out;
2278
0
    }
2279
0
    cfg_apply(&system_wide_config, &ctx);
2280
0
    ini_ctx_deinit(&ctx);
2281
0
    _gnutls_debug_log("cfg: loaded system config %s mtime %lld\n",
2282
0
          system_priority_file,
2283
0
          (unsigned long long)sb.st_mtime);
2284
0
  }
2285
2286
0
  if (system_wide_config.allowlisting) {
2287
0
    if (defer_system_wide) {
2288
      /* try constructing a priority string,
2289
       * but don't apply it yet, at this point
2290
       * we're only interested in whether we can */
2291
0
      ret = construct_system_wide_priority_string(&buf);
2292
0
      _gnutls_buffer_clear(&buf);
2293
0
      _gnutls_debug_log("cfg: deferred setting "
2294
0
            "system-wide priority string\n");
2295
0
    } else {
2296
0
      ret = update_system_wide_priority_string();
2297
0
      _gnutls_debug_log("cfg: finalized "
2298
0
            "system-wide priority string\n");
2299
0
    }
2300
0
    if (ret < 0) {
2301
0
      _gnutls_debug_log(
2302
0
        "cfg: unable to build priority string: %s\n",
2303
0
        gnutls_strerror(ret));
2304
0
      if (fail_on_invalid_config)
2305
0
        exit(1);
2306
0
      goto out;
2307
0
    }
2308
0
  }
2309
2310
0
  system_priority_file_loaded = 1;
2311
0
  system_priority_last_mod = sb.st_mtime;
2312
2313
2
out:
2314
2
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
2315
2316
2
  if (err && fail_on_invalid_config) {
2317
0
    exit(1);
2318
0
  }
2319
2320
2
  return ret;
2321
2
}
2322
2323
void _gnutls_prepare_to_load_system_priorities(void)
2324
2
{
2325
2
  const char *p;
2326
2
  int ret;
2327
2328
2
  p = secure_getenv("GNUTLS_SYSTEM_PRIORITY_FILE");
2329
2
  if (p != NULL)
2330
0
    system_priority_file = p;
2331
2332
2
  p = secure_getenv("GNUTLS_SYSTEM_PRIORITY_FAIL_ON_INVALID");
2333
2
  if (p != NULL && p[0] == '1' && p[1] == 0)
2334
0
    fail_on_invalid_config = 1;
2335
2336
2
  ret = _gnutls_update_system_priorities(true /* defer_system_wide */);
2337
2
  if (ret < 0) {
2338
0
    _gnutls_debug_log("failed to update system priorities: %s\n",
2339
0
          gnutls_strerror(ret));
2340
0
  }
2341
2
}
2342
2343
void _gnutls_unload_system_priorities(void)
2344
0
{
2345
0
  _name_val_array_clear(&system_wide_config.priority_strings);
2346
0
  gnutls_free(system_wide_config.priority_string);
2347
0
  _clear_default_system_priority();
2348
0
  system_priority_last_mod = 0;
2349
0
}
2350
2351
/**
2352
 * gnutls_get_system_config_file:
2353
 *
2354
 * Returns the filename of the system wide configuration
2355
 * file to be loaded by the library.
2356
 *
2357
 * Returns: a constant pointer to the config file path
2358
 *
2359
 * Since: 3.6.9
2360
 **/
2361
const char *gnutls_get_system_config_file(void)
2362
0
{
2363
0
  return system_priority_file;
2364
0
}
2365
2366
#define S(str) ((str != NULL) ? str : "")
2367
2368
/* Returns the new priorities if a priority string prefixed
2369
 * with '@' is provided, or just a copy of the provided
2370
 * priorities, appended with any additional present in
2371
 * the priorities string.
2372
 *
2373
 * The returned string must be released using gnutls_free().
2374
 */
2375
char *_gnutls_resolve_priorities(const char *priorities)
2376
0
{
2377
0
  const char *p = priorities;
2378
0
  char *additional = NULL;
2379
0
  char *resolved = NULL;
2380
0
  const char *ss, *ss_next;
2381
0
  unsigned ss_len, ss_next_len;
2382
0
  size_t n, n2 = 0;
2383
0
  int ret;
2384
2385
0
  while (c_isspace(*p)) {
2386
0
    p++;
2387
0
  }
2388
2389
  /* Cannot reduce further. */
2390
0
  if (*p != '@') {
2391
0
    return gnutls_strdup(p);
2392
0
  }
2393
2394
0
  ss = p + 1;
2395
0
  additional = strchr(ss, ':');
2396
0
  if (additional) {
2397
0
    additional++;
2398
0
  }
2399
2400
  /* Always try to refresh the cached data, to allow it to be
2401
   * updated without restarting all applications.
2402
   */
2403
0
  ret = _gnutls_update_system_priorities(false /* defer_system_wide */);
2404
0
  if (ret < 0) {
2405
0
    _gnutls_debug_log("failed to update system priorities: %s\n",
2406
0
          gnutls_strerror(ret));
2407
0
  }
2408
2409
0
  do {
2410
0
    ss_next = strchr(ss, ',');
2411
0
    if (ss_next) {
2412
0
      if (additional && ss_next > additional) {
2413
0
        ss_next = NULL;
2414
0
      } else {
2415
0
        ss_next++;
2416
0
      }
2417
0
    }
2418
2419
0
    if (ss_next) {
2420
0
      ss_len = ss_next - ss - 1;
2421
0
      ss_next_len = additional - ss_next - 1;
2422
0
    } else if (additional) {
2423
0
      ss_len = additional - ss - 1;
2424
0
      ss_next_len = 0;
2425
0
    } else {
2426
0
      ss_len = strlen(ss);
2427
0
      ss_next_len = 0;
2428
0
    }
2429
2430
0
    ret = gnutls_rwlock_rdlock(&system_wide_config_rwlock);
2431
0
    if (ret < 0) {
2432
0
      _gnutls_debug_log(
2433
0
        "cannot read system priority strings: %s\n",
2434
0
        gnutls_strerror(ret));
2435
0
      break;
2436
0
    }
2437
0
    if (system_wide_config.allowlisting &&
2438
0
        ss_len == sizeof(LEVEL_SYSTEM) - 1 &&
2439
0
        strncmp(LEVEL_SYSTEM, ss, ss_len) == 0) {
2440
0
      p = system_wide_config.priority_string;
2441
0
    } else {
2442
0
      p = _name_val_array_value(
2443
0
        system_wide_config.priority_strings, ss,
2444
0
        ss_len);
2445
0
    }
2446
2447
0
    _gnutls_debug_log("resolved '%.*s' to '%s', next '%.*s'\n",
2448
0
          ss_len, ss, S(p), ss_next_len, S(ss_next));
2449
2450
0
    if (p) {
2451
0
      n = strlen(p);
2452
0
      if (additional) {
2453
0
        n2 = strlen(additional);
2454
0
      }
2455
2456
0
      resolved = gnutls_malloc(n + n2 + 1 + 1);
2457
0
      if (resolved) {
2458
0
        memcpy(resolved, p, n);
2459
0
        if (additional) {
2460
0
          resolved[n] = ':';
2461
0
          memcpy(&resolved[n + 1], additional,
2462
0
                 n2);
2463
0
          resolved[n + n2 + 1] = 0;
2464
0
        } else {
2465
0
          resolved[n] = 0;
2466
0
        }
2467
0
      }
2468
0
    }
2469
2470
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
2471
2472
0
    ss = ss_next;
2473
0
  } while (ss && !resolved);
2474
2475
0
  if (resolved) {
2476
0
    _gnutls_debug_log("selected priority string: %s\n", resolved);
2477
0
  } else {
2478
0
    _gnutls_debug_log("unable to resolve %s\n", priorities);
2479
0
  }
2480
2481
0
  return resolved;
2482
0
}
2483
2484
static void add_ec(gnutls_priority_t priority_cache)
2485
0
{
2486
0
  const gnutls_group_entry_st *ge;
2487
0
  unsigned i;
2488
2489
0
  for (i = 0; i < priority_cache->_supported_ecc.num_priorities; i++) {
2490
0
    ge = _gnutls_id_to_group(
2491
0
      priority_cache->_supported_ecc.priorities[i]);
2492
0
    if (ge != NULL &&
2493
0
        priority_cache->groups.size <
2494
0
          sizeof(priority_cache->groups.entry) /
2495
0
            sizeof(priority_cache->groups.entry[0])) {
2496
      /* do not add groups which do not correspond to enabled ciphersuites */
2497
0
      if (!ge->curve)
2498
0
        continue;
2499
0
      priority_cache->groups
2500
0
        .entry[priority_cache->groups.size++] = ge;
2501
0
    }
2502
0
  }
2503
0
}
2504
2505
static void add_dh(gnutls_priority_t priority_cache)
2506
0
{
2507
0
  const gnutls_group_entry_st *ge;
2508
0
  unsigned i;
2509
2510
0
  for (i = 0; i < priority_cache->_supported_ecc.num_priorities; i++) {
2511
0
    ge = _gnutls_id_to_group(
2512
0
      priority_cache->_supported_ecc.priorities[i]);
2513
0
    if (ge != NULL &&
2514
0
        priority_cache->groups.size <
2515
0
          sizeof(priority_cache->groups.entry) /
2516
0
            sizeof(priority_cache->groups.entry[0])) {
2517
      /* do not add groups which do not correspond to enabled ciphersuites */
2518
0
      if (!ge->prime)
2519
0
        continue;
2520
0
      priority_cache->groups
2521
0
        .entry[priority_cache->groups.size++] = ge;
2522
0
      priority_cache->groups.have_ffdhe = 1;
2523
0
    }
2524
0
  }
2525
0
}
2526
2527
/* This function was originally precalculating ciphersuite-specific items, however
2528
 * it has now extended to much more than that. It provides a consistency check to
2529
 * set parameters, and in cases it applies policy specific items.
2530
 */
2531
static int set_ciphersuite_list(gnutls_priority_t priority_cache)
2532
0
{
2533
0
  unsigned i, j, z;
2534
0
  const gnutls_cipher_suite_entry_st *ce;
2535
0
  const gnutls_sign_entry_st *se;
2536
0
  unsigned have_ec = 0;
2537
0
  unsigned have_dh = 0;
2538
0
  unsigned tls_sig_sem = 0;
2539
0
  const version_entry_st *tlsmax = NULL, *vers;
2540
0
  const version_entry_st *dtlsmax = NULL;
2541
0
  const version_entry_st *tlsmin = NULL;
2542
0
  const version_entry_st *dtlsmin = NULL;
2543
0
  unsigned have_tls13 = 0, have_srp = 0;
2544
0
  unsigned have_pre_tls12 = 0, have_tls12 = 0;
2545
0
  unsigned have_psk = 0, have_null = 0, have_rsa_psk = 0;
2546
0
  gnutls_digest_algorithm_t prf_digest;
2547
0
  int ret = 0;
2548
2549
  /* have_psk indicates that a PSK key exchange compatible
2550
   * with TLS1.3 is enabled. */
2551
2552
0
  priority_cache->cs.size = 0;
2553
0
  priority_cache->sigalg.size = 0;
2554
0
  priority_cache->groups.size = 0;
2555
0
  priority_cache->groups.have_ffdhe = 0;
2556
2557
  /* The following requires a lock so there are no inconsistencies in the
2558
   * members of system_wide_config loaded from the config file. */
2559
0
  ret = gnutls_rwlock_rdlock(&system_wide_config_rwlock);
2560
0
  if (ret < 0) {
2561
0
    return gnutls_assert_val(ret);
2562
0
  }
2563
2564
  /* in blocklisting mode, apply system wide disablement of key exchanges,
2565
   * groups, MACs, and ciphers. */
2566
0
  if (!system_wide_config.allowlisting) {
2567
    /* disable key exchanges which are globally disabled */
2568
0
    z = 0;
2569
0
    while (system_wide_config.kxs[z] != 0) {
2570
0
      for (i = j = 0; i < priority_cache->_kx.num_priorities;
2571
0
           i++) {
2572
0
        if (priority_cache->_kx.priorities[i] !=
2573
0
            system_wide_config.kxs[z])
2574
0
          priority_cache->_kx.priorities[j++] =
2575
0
            priority_cache->_kx
2576
0
              .priorities[i];
2577
0
      }
2578
0
      priority_cache->_kx.num_priorities = j;
2579
0
      z++;
2580
0
    }
2581
2582
    /* disable groups which are globally disabled */
2583
0
    z = 0;
2584
0
    while (system_wide_config.groups[z] != 0) {
2585
0
      for (i = j = 0;
2586
0
           i < priority_cache->_supported_ecc.num_priorities;
2587
0
           i++) {
2588
0
        if (priority_cache->_supported_ecc
2589
0
              .priorities[i] !=
2590
0
            system_wide_config.groups[z])
2591
0
          priority_cache->_supported_ecc
2592
0
            .priorities[j++] =
2593
0
            priority_cache->_supported_ecc
2594
0
              .priorities[i];
2595
0
      }
2596
0
      priority_cache->_supported_ecc.num_priorities = j;
2597
0
      z++;
2598
0
    }
2599
2600
    /* disable ciphers which are globally disabled */
2601
0
    z = 0;
2602
0
    while (system_wide_config.ciphers[z] != 0) {
2603
0
      for (i = j = 0;
2604
0
           i < priority_cache->_cipher.num_priorities; i++) {
2605
0
        if (priority_cache->_cipher.priorities[i] !=
2606
0
            system_wide_config.ciphers[z])
2607
0
          priority_cache->_cipher.priorities[j++] =
2608
0
            priority_cache->_cipher
2609
0
              .priorities[i];
2610
0
      }
2611
0
      priority_cache->_cipher.num_priorities = j;
2612
0
      z++;
2613
0
    }
2614
2615
    /* disable MACs which are globally disabled */
2616
0
    z = 0;
2617
0
    while (system_wide_config.macs[z] != 0) {
2618
0
      for (i = j = 0; i < priority_cache->_mac.num_priorities;
2619
0
           i++) {
2620
0
        if (priority_cache->_mac.priorities[i] !=
2621
0
            system_wide_config.macs[z])
2622
0
          priority_cache->_mac.priorities[j++] =
2623
0
            priority_cache->_mac
2624
0
              .priorities[i];
2625
0
      }
2626
0
      priority_cache->_mac.num_priorities = j;
2627
0
      z++;
2628
0
    }
2629
0
  }
2630
2631
0
  for (j = 0; j < priority_cache->_cipher.num_priorities; j++) {
2632
0
    if (priority_cache->_cipher.priorities[j] ==
2633
0
        GNUTLS_CIPHER_NULL) {
2634
0
      have_null = 1;
2635
0
      break;
2636
0
    }
2637
0
  }
2638
2639
0
  for (i = 0; i < priority_cache->_kx.num_priorities; i++) {
2640
0
    if (IS_SRP_KX(priority_cache->_kx.priorities[i])) {
2641
0
      have_srp = 1;
2642
0
    } else if (_gnutls_kx_is_psk(
2643
0
           priority_cache->_kx.priorities[i])) {
2644
0
      if (priority_cache->_kx.priorities[i] ==
2645
0
          GNUTLS_KX_RSA_PSK)
2646
0
        have_rsa_psk = 1;
2647
0
      else
2648
0
        have_psk = 1;
2649
0
    }
2650
0
  }
2651
2652
  /* disable TLS versions which are added but are unsupported */
2653
0
  for (i = j = 0; i < priority_cache->protocol.num_priorities; i++) {
2654
0
    vers = version_to_entry(priority_cache->protocol.priorities[i]);
2655
0
    if (!vers || vers->supported ||
2656
0
        (system_wide_config.allowlisting &&
2657
0
         vers->supported_revertible))
2658
0
      priority_cache->protocol.priorities[j++] =
2659
0
        priority_cache->protocol.priorities[i];
2660
0
  }
2661
0
  priority_cache->protocol.num_priorities = j;
2662
2663
  /* if we have NULL ciphersuites, SRP, or RSA-PSK enabled remove TLS1.3+
2664
   * protocol versions; they cannot be negotiated under TLS1.3. */
2665
0
  if (have_null || have_srp || have_rsa_psk ||
2666
0
      priority_cache->no_extensions) {
2667
0
    for (i = j = 0; i < priority_cache->protocol.num_priorities;
2668
0
         i++) {
2669
0
      vers = version_to_entry(
2670
0
        priority_cache->protocol.priorities[i]);
2671
0
      if (!vers || !vers->tls13_sem)
2672
0
        priority_cache->protocol.priorities[j++] =
2673
0
          priority_cache->protocol.priorities[i];
2674
0
    }
2675
0
    priority_cache->protocol.num_priorities = j;
2676
0
  }
2677
2678
0
  for (i = 0; i < priority_cache->protocol.num_priorities; i++) {
2679
0
    vers = version_to_entry(priority_cache->protocol.priorities[i]);
2680
0
    if (!vers)
2681
0
      continue;
2682
2683
0
    if (vers->transport == GNUTLS_STREAM) { /* TLS */
2684
0
      tls_sig_sem |= vers->tls_sig_sem;
2685
0
      if (vers->tls13_sem)
2686
0
        have_tls13 = 1;
2687
2688
0
      if (vers->id == GNUTLS_TLS1_2)
2689
0
        have_tls12 = 1;
2690
0
      else if (vers->id < GNUTLS_TLS1_2)
2691
0
        have_pre_tls12 = 1;
2692
2693
0
      if (tlsmax == NULL || vers->age > tlsmax->age)
2694
0
        tlsmax = vers;
2695
0
      if (tlsmin == NULL || vers->age < tlsmin->age)
2696
0
        tlsmin = vers;
2697
0
    } else { /* dtls */
2698
0
      tls_sig_sem |= vers->tls_sig_sem;
2699
2700
      /* we need to introduce similar handling to above
2701
       * when DTLS1.3 is supported */
2702
2703
0
      if (dtlsmax == NULL || vers->age > dtlsmax->age)
2704
0
        dtlsmax = vers;
2705
0
      if (dtlsmin == NULL || vers->age < dtlsmin->age)
2706
0
        dtlsmin = vers;
2707
0
    }
2708
0
  }
2709
2710
  /* DTLS or TLS protocols must be present */
2711
0
  if ((!tlsmax || !tlsmin) && (!dtlsmax || !dtlsmin)) {
2712
0
    ret = gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
2713
0
    goto out;
2714
0
  }
2715
2716
0
  priority_cache->have_psk = have_psk;
2717
2718
  /* if we are have TLS1.3+ do not enable any key exchange algorithms,
2719
   * the protocol doesn't require any. */
2720
0
  if (tlsmin && tlsmin->tls13_sem && !have_psk) {
2721
0
    if (!dtlsmin || (dtlsmin && dtlsmin->tls13_sem))
2722
0
      priority_cache->_kx.num_priorities = 0;
2723
0
  }
2724
2725
  /* Add TLS 1.3 ciphersuites (no KX) */
2726
0
  for (j = 0; j < priority_cache->_cipher.num_priorities; j++) {
2727
0
    for (z = 0; z < priority_cache->_mac.num_priorities; z++) {
2728
0
      ce = cipher_suite_get(
2729
0
        0, priority_cache->_cipher.priorities[j],
2730
0
        priority_cache->_mac.priorities[z]);
2731
0
      if (ce == NULL)
2732
0
        continue;
2733
2734
0
      prf_digest = MAC_TO_DIG(ce->prf);
2735
0
      if (prf_digest == GNUTLS_DIG_UNKNOWN)
2736
0
        continue;
2737
0
      if (_gnutls_digest_is_insecure(prf_digest))
2738
0
        continue;
2739
2740
0
      if (priority_cache->cs.size < MAX_CIPHERSUITE_SIZE)
2741
0
        priority_cache->cs
2742
0
          .entry[priority_cache->cs.size++] = ce;
2743
0
    }
2744
0
  }
2745
2746
0
  for (i = 0; i < priority_cache->_kx.num_priorities; i++) {
2747
0
    for (j = 0; j < priority_cache->_cipher.num_priorities; j++) {
2748
0
      for (z = 0; z < priority_cache->_mac.num_priorities;
2749
0
           z++) {
2750
0
        ce = cipher_suite_get(
2751
0
          priority_cache->_kx.priorities[i],
2752
0
          priority_cache->_cipher.priorities[j],
2753
0
          priority_cache->_mac.priorities[z]);
2754
0
        if (ce == NULL)
2755
0
          continue;
2756
2757
0
        prf_digest = MAC_TO_DIG(ce->prf);
2758
0
        if (prf_digest == GNUTLS_DIG_UNKNOWN)
2759
0
          continue;
2760
0
        if (_gnutls_digest_is_insecure(prf_digest))
2761
0
          continue;
2762
2763
0
        if (priority_cache->cs.size ==
2764
0
            MAX_CIPHERSUITE_SIZE)
2765
0
          continue;
2766
0
        priority_cache->cs
2767
0
          .entry[priority_cache->cs.size++] = ce;
2768
0
        if (!have_ec &&
2769
0
            (_gnutls_kx_is_ecc(ce->kx_algorithm) ||
2770
0
             _gnutls_kx_is_vko_gost(ce->kx_algorithm))) {
2771
0
          have_ec = 1;
2772
0
          add_ec(priority_cache);
2773
0
        }
2774
0
        if (!have_dh &&
2775
0
            _gnutls_kx_is_dhe(ce->kx_algorithm)) {
2776
0
          have_dh = 1;
2777
0
          add_dh(priority_cache);
2778
0
        }
2779
0
      }
2780
0
    }
2781
0
  }
2782
2783
0
  if (have_tls13 && (!have_ec || !have_dh)) {
2784
    /* scan groups to determine have_ec and have_dh */
2785
0
    for (i = 0; i < priority_cache->_supported_ecc.num_priorities;
2786
0
         i++) {
2787
0
      const gnutls_group_entry_st *ge;
2788
0
      ge = _gnutls_id_to_group(
2789
0
        priority_cache->_supported_ecc.priorities[i]);
2790
0
      if (ge) {
2791
0
        if (ge->curve && !have_ec) {
2792
0
          add_ec(priority_cache);
2793
0
          have_ec = 1;
2794
0
        } else if (ge->prime && !have_dh) {
2795
0
          add_dh(priority_cache);
2796
0
          have_dh = 1;
2797
0
        }
2798
2799
0
        if (have_dh && have_ec)
2800
0
          break;
2801
0
      }
2802
0
    }
2803
0
  }
2804
2805
0
  for (i = 0; i < priority_cache->_sign_algo.num_priorities; i++) {
2806
0
    se = _gnutls_sign_to_entry(
2807
0
      priority_cache->_sign_algo.priorities[i]);
2808
0
    if (se != NULL &&
2809
0
        priority_cache->sigalg.size <
2810
0
          sizeof(priority_cache->sigalg.entry) /
2811
0
            sizeof(priority_cache->sigalg.entry[0])) {
2812
      /* if the signature algorithm semantics is not
2813
       * compatible with the protocol's, or the algorithm is
2814
       * marked as insecure, then skip. */
2815
0
      if ((se->aid.tls_sem & tls_sig_sem) == 0 ||
2816
0
          !_gnutls_sign_is_secure2(
2817
0
            se,
2818
0
            system_wide_config.allowlisting ?
2819
0
              GNUTLS_SIGN_FLAG_ALLOW_INSECURE_REVERTIBLE :
2820
0
              0)) {
2821
0
        continue;
2822
0
      }
2823
0
      priority_cache->sigalg
2824
0
        .entry[priority_cache->sigalg.size++] = se;
2825
0
    }
2826
0
  }
2827
2828
0
  _gnutls_debug_log(
2829
0
    "added %d protocols, %d ciphersuites, %d sig algos and %d groups into priority list\n",
2830
0
    priority_cache->protocol.num_priorities,
2831
0
    priority_cache->cs.size, priority_cache->sigalg.size,
2832
0
    priority_cache->groups.size);
2833
2834
0
  if (priority_cache->sigalg.size == 0) {
2835
    /* no signature algorithms; eliminate TLS 1.2 or DTLS 1.2 and later */
2836
0
    priority_st newp;
2837
0
    newp.num_priorities = 0;
2838
2839
    /* we need to eliminate TLS 1.2 or DTLS 1.2 and later protocols */
2840
0
    for (i = 0; i < priority_cache->protocol.num_priorities; i++) {
2841
0
      if (priority_cache->protocol.priorities[i] <
2842
0
          GNUTLS_TLS1_2) {
2843
0
        newp.priorities[newp.num_priorities++] =
2844
0
          priority_cache->protocol.priorities[i];
2845
0
      } else if (priority_cache->protocol.priorities[i] >=
2846
0
             GNUTLS_DTLS_VERSION_MIN &&
2847
0
           priority_cache->protocol.priorities[i] <
2848
0
             GNUTLS_DTLS1_2) {
2849
0
        newp.priorities[newp.num_priorities++] =
2850
0
          priority_cache->protocol.priorities[i];
2851
0
      }
2852
0
    }
2853
0
    memcpy(&priority_cache->protocol, &newp, sizeof(newp));
2854
0
  }
2855
2856
0
  if (unlikely(priority_cache->protocol.num_priorities == 0)) {
2857
0
    ret = gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
2858
0
    goto out;
2859
0
  }
2860
0
#ifndef ENABLE_SSL3
2861
0
  else if (unlikely(priority_cache->protocol.num_priorities == 1 &&
2862
0
        priority_cache->protocol.priorities[0] ==
2863
0
          GNUTLS_SSL3)) {
2864
0
    ret = gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
2865
0
    goto out;
2866
0
  }
2867
0
#endif
2868
2869
0
  if (unlikely(priority_cache->cs.size == 0)) {
2870
0
    ret = gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
2871
0
    goto out;
2872
0
  }
2873
2874
  /* when TLS 1.3 is available we must have groups set; additionally
2875
   * we require TLS1.2 to be enabled if TLS1.3 is asked for, and
2876
   * a pre-TLS1.2 protocol is there; that is because servers which
2877
   * do not support TLS1.3 will negotiate TLS1.2 if seen a TLS1.3 handshake */
2878
0
  if (unlikely((!have_psk && tlsmax && tlsmax->id >= GNUTLS_TLS1_3 &&
2879
0
          priority_cache->groups.size == 0)) ||
2880
0
      (!have_tls12 && have_pre_tls12 && have_tls13)) {
2881
0
    for (i = j = 0; i < priority_cache->protocol.num_priorities;
2882
0
         i++) {
2883
0
      vers = version_to_entry(
2884
0
        priority_cache->protocol.priorities[i]);
2885
0
      if (!vers || vers->transport != GNUTLS_STREAM ||
2886
0
          !vers->tls13_sem)
2887
0
        priority_cache->protocol.priorities[j++] =
2888
0
          priority_cache->protocol.priorities[i];
2889
0
    }
2890
0
    priority_cache->protocol.num_priorities = j;
2891
0
  }
2892
2893
  /* ensure that the verification profile is not lower from the configured */
2894
0
  if (system_wide_config.verification_profile) {
2895
0
    gnutls_sec_param_t level = priority_cache->level;
2896
0
    gnutls_sec_param_t system_wide_level =
2897
0
      _gnutls_profile_to_sec_level(
2898
0
        system_wide_config.verification_profile);
2899
2900
0
    if (level < system_wide_level) {
2901
0
      ENABLE_PROFILE(priority_cache,
2902
0
               system_wide_config.verification_profile);
2903
0
    }
2904
0
  }
2905
2906
0
out:
2907
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
2908
0
  return ret;
2909
0
}
2910
2911
/**
2912
 * gnutls_priority_init2:
2913
 * @priority_cache: is a #gnutls_priority_t type.
2914
 * @priorities: is a string describing priorities (may be %NULL)
2915
 * @err_pos: In case of an error this will have the position in the string the error occurred
2916
 * @flags: zero or %GNUTLS_PRIORITY_INIT_DEF_APPEND
2917
 *
2918
 * Sets priorities for the ciphers, key exchange methods, and macs.
2919
 * The @priority_cache should be deinitialized
2920
 * using gnutls_priority_deinit().
2921
 *
2922
 * The #priorities option allows you to specify a colon
2923
 * separated list of the cipher priorities to enable.
2924
 * Some keywords are defined to provide quick access
2925
 * to common preferences.
2926
 *
2927
 * When @flags is set to %GNUTLS_PRIORITY_INIT_DEF_APPEND then the @priorities
2928
 * specified will be appended to the default options.
2929
 *
2930
 * Unless there is a special need, use the "NORMAL" keyword to
2931
 * apply a reasonable security level, or "NORMAL:%%COMPAT" for compatibility.
2932
 *
2933
 * "PERFORMANCE" means all the "secure" ciphersuites are enabled,
2934
 * limited to 128 bit ciphers and sorted by terms of speed
2935
 * performance.
2936
 *
2937
 * "LEGACY" the NORMAL settings for GnuTLS 3.2.x or earlier. There is
2938
 * no verification profile set, and the allowed DH primes are considered
2939
 * weak today.
2940
 *
2941
 * "NORMAL" means all "secure" ciphersuites. The 256-bit ciphers are
2942
 * included as a fallback only.  The ciphers are sorted by security
2943
 * margin.
2944
 *
2945
 * "PFS" means all "secure" ciphersuites that support perfect forward secrecy.
2946
 * The 256-bit ciphers are included as a fallback only.
2947
 * The ciphers are sorted by security margin.
2948
 *
2949
 * "SECURE128" means all "secure" ciphersuites of security level 128-bit
2950
 * or more.
2951
 *
2952
 * "SECURE192" means all "secure" ciphersuites of security level 192-bit
2953
 * or more.
2954
 *
2955
 * "SUITEB128" means all the NSA SuiteB ciphersuites with security level
2956
 * of 128.
2957
 *
2958
 * "SUITEB192" means all the NSA SuiteB ciphersuites with security level
2959
 * of 192.
2960
 *
2961
 * "NONE" means nothing is enabled.  This disables everything, including protocols.
2962
 *
2963
 * "@@KEYWORD1,KEYWORD2,..." The system administrator imposed settings.
2964
 * The provided keyword(s) will be expanded from a configuration-time
2965
 * provided file - default is: /etc/gnutls/config.
2966
 * Any attributes that follow it, will be appended to the expanded
2967
 * string. If multiple keywords are provided, separated by commas,
2968
 * then the first keyword that exists in the configuration file
2969
 * will be used. At least one of the keywords must exist, or this
2970
 * function will return an error. Typical usage would be to specify
2971
 * an application specified keyword first, followed by "SYSTEM" as
2972
 * a default fallback. e.g., "@LIBVIRT,SYSTEM:!-VERS-SSL3.0" will
2973
 * first try to find a config file entry matching "LIBVIRT", but if
2974
 * that does not exist will use the entry for "SYSTEM". If "SYSTEM"
2975
 * does not exist either, an error will be returned. In all cases,
2976
 * the SSL3.0 protocol will be disabled. The system priority file
2977
 * entries should be formatted as "KEYWORD=VALUE", e.g.,
2978
 * "SYSTEM=NORMAL:+ARCFOUR-128".
2979
 *
2980
 * Special keywords are "!", "-" and "+".
2981
 * "!" or "-" appended with an algorithm will remove this algorithm.
2982
 * "+" appended with an algorithm will add this algorithm.
2983
 *
2984
 * Check the GnuTLS manual section "Priority strings" for detailed
2985
 * information.
2986
 *
2987
 * Examples:
2988
 *
2989
 * "NONE:+VERS-TLS-ALL:+MAC-ALL:+RSA:+AES-128-CBC:+SIGN-ALL:+COMP-NULL"
2990
 *
2991
 * "NORMAL:+ARCFOUR-128" means normal ciphers plus ARCFOUR-128.
2992
 *
2993
 * "SECURE128:-VERS-SSL3.0" means that only secure ciphers are
2994
 * and enabled, SSL3.0 is disabled.
2995
 *
2996
 * "NONE:+VERS-TLS-ALL:+AES-128-CBC:+RSA:+SHA1:+COMP-NULL:+SIGN-RSA-SHA1",
2997
 *
2998
 * "NONE:+VERS-TLS-ALL:+AES-128-CBC:+ECDHE-RSA:+SHA1:+COMP-NULL:+SIGN-RSA-SHA1:+CURVE-SECP256R1",
2999
 *
3000
 * "SECURE256:+SECURE128",
3001
 *
3002
 * Note that "NORMAL:%%COMPAT" is the most compatible mode.
3003
 *
3004
 * A %NULL @priorities string indicates the default priorities to be
3005
 * used (this is available since GnuTLS 3.3.0).
3006
 *
3007
 * Returns: On syntax error %GNUTLS_E_INVALID_REQUEST is returned,
3008
 * %GNUTLS_E_SUCCESS on success, or an error code.
3009
 *
3010
 * Since: 3.6.3
3011
 **/
3012
int gnutls_priority_init2(gnutls_priority_t *priority_cache,
3013
        const char *priorities, const char **err_pos,
3014
        unsigned flags)
3015
0
{
3016
0
  gnutls_buffer_st buf;
3017
0
  const char *ep;
3018
0
  int ret;
3019
3020
0
  *priority_cache = NULL;
3021
0
  if (flags & GNUTLS_PRIORITY_INIT_DEF_APPEND) {
3022
0
    if (priorities == NULL)
3023
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3024
3025
0
    if (err_pos)
3026
0
      *err_pos = priorities;
3027
3028
0
    _gnutls_buffer_init(&buf);
3029
3030
0
    ret = _gnutls_buffer_append_str(
3031
0
      &buf, _gnutls_default_priority_string);
3032
0
    if (ret < 0) {
3033
0
      _gnutls_buffer_clear(&buf);
3034
0
      return gnutls_assert_val(ret);
3035
0
    }
3036
3037
0
    ret = _gnutls_buffer_append_str(&buf, ":");
3038
0
    if (ret < 0) {
3039
0
      _gnutls_buffer_clear(&buf);
3040
0
      return gnutls_assert_val(ret);
3041
0
    }
3042
3043
0
    ret = _gnutls_buffer_append_str(&buf, priorities);
3044
0
    if (ret < 0) {
3045
0
      _gnutls_buffer_clear(&buf);
3046
0
      return gnutls_assert_val(ret);
3047
0
    }
3048
3049
0
    ret = gnutls_priority_init(priority_cache,
3050
0
             (const char *)buf.data, &ep);
3051
0
    if (ret < 0 && ep != (const char *)buf.data && ep != NULL) {
3052
0
      ptrdiff_t diff = (ptrdiff_t)ep - (ptrdiff_t)buf.data;
3053
0
      unsigned hlen =
3054
0
        strlen(_gnutls_default_priority_string) + 1;
3055
3056
0
      if (err_pos && diff > hlen) {
3057
0
        *err_pos = priorities + diff - hlen;
3058
0
      }
3059
0
    }
3060
0
    _gnutls_buffer_clear(&buf);
3061
0
    return ret;
3062
0
  } else {
3063
0
    return gnutls_priority_init(priority_cache, priorities,
3064
0
              err_pos);
3065
0
  }
3066
0
}
3067
3068
#define PRIO_MATCH(name) \
3069
0
  c_strncasecmp(&broken_list[i][1], name, sizeof(name) - 1)
3070
3071
/**
3072
 * gnutls_priority_init:
3073
 * @priority_cache: is a #gnutls_priority_t type.
3074
 * @priorities: is a string describing priorities (may be %NULL)
3075
 * @err_pos: In case of an error this will have the position in the string the error occurred
3076
 *
3077
 * For applications that do not modify their crypto settings per release, consider
3078
 * using gnutls_priority_init2() with %GNUTLS_PRIORITY_INIT_DEF_APPEND flag
3079
 * instead. We suggest to use centralized crypto settings handled by the GnuTLS
3080
 * library, and applications modifying the default settings to their needs.
3081
 *
3082
 * This function is identical to gnutls_priority_init2() with zero
3083
 * flags.
3084
 *
3085
 * A %NULL @priorities string indicates the default priorities to be
3086
 * used (this is available since GnuTLS 3.3.0).
3087
 *
3088
 * Returns: On syntax error %GNUTLS_E_INVALID_REQUEST is returned,
3089
 * %GNUTLS_E_SUCCESS on success, or an error code.
3090
 **/
3091
int gnutls_priority_init(gnutls_priority_t *priority_cache,
3092
       const char *priorities, const char **err_pos)
3093
0
{
3094
0
  char *broken_list[MAX_ELEMENTS];
3095
0
  int broken_list_size = 0, i = 0, j;
3096
0
  char *darg = NULL;
3097
0
  unsigned ikeyword_set = 0;
3098
0
  int algo;
3099
0
  int ret;
3100
0
  rmadd_func *fn;
3101
0
  bulk_rmadd_func *bulk_fn;
3102
0
  bulk_rmadd_func *bulk_given_fn;
3103
0
  const cipher_entry_st *centry;
3104
0
  unsigned resolved_match = 1;
3105
3106
0
  if (err_pos)
3107
0
    *err_pos = priorities;
3108
3109
0
  *priority_cache = gnutls_calloc(1, sizeof(struct gnutls_priority_st));
3110
0
  if (*priority_cache == NULL) {
3111
0
    gnutls_assert();
3112
0
    return GNUTLS_E_MEMORY_ERROR;
3113
0
  }
3114
3115
  /* for now unsafe renegotiation is default on everyone. To be removed
3116
   * when we make it the default.
3117
   */
3118
0
  (*priority_cache)->sr = SR_PARTIAL;
3119
  /* For now TLS 1.3 middlebox compatibility mode is enabled by default.
3120
   * This will eventually be disabled by default and moved to the %COMPAT
3121
   * setting.
3122
   */
3123
0
  (*priority_cache)->tls13_compat_mode = true;
3124
0
  (*priority_cache)->min_record_version = 1;
3125
0
  gnutls_atomic_init(&(*priority_cache)->usage_cnt);
3126
3127
0
  if (_gnutls_fips_mode_enabled()) {
3128
0
    (*priority_cache)->force_ext_master_secret = EMS_REQUIRE;
3129
0
  } else {
3130
0
    (*priority_cache)->force_ext_master_secret = EMS_REQUEST;
3131
0
  }
3132
3133
0
  if (system_wide_config.allowlisting && !priorities) {
3134
0
    priorities = "@" LEVEL_SYSTEM;
3135
0
  }
3136
0
  if (priorities == NULL) {
3137
0
    priorities = _gnutls_default_priority_string;
3138
0
    resolved_match = 0;
3139
0
  }
3140
3141
0
  darg = _gnutls_resolve_priorities(priorities);
3142
0
  if (darg == NULL) {
3143
0
    gnutls_assert();
3144
0
    goto error;
3145
0
  }
3146
3147
0
  if (strcmp(darg, priorities) != 0)
3148
0
    resolved_match = 0;
3149
3150
0
  break_list(darg, broken_list, &broken_list_size);
3151
  /* This is our default set of protocol version, certificate types.
3152
   */
3153
0
  if (c_strcasecmp(broken_list[0], LEVEL_NONE) != 0) {
3154
0
    _set_priority(&(*priority_cache)->protocol, protocol_priority);
3155
0
    _set_priority(&(*priority_cache)->client_ctype,
3156
0
            cert_type_priority_default);
3157
0
    _set_priority(&(*priority_cache)->server_ctype,
3158
0
            cert_type_priority_default);
3159
0
    _set_priority(&(*priority_cache)->_sign_algo,
3160
0
            sign_priority_default);
3161
0
    _set_priority(&(*priority_cache)->_supported_ecc,
3162
0
            supported_groups_normal);
3163
0
    i = 0;
3164
0
  } else {
3165
0
    ikeyword_set = 1;
3166
0
    i = 1;
3167
0
  }
3168
3169
0
  for (; i < broken_list_size; i++) {
3170
0
    if (check_level(broken_list[i], *priority_cache,
3171
0
        ikeyword_set) != 0) {
3172
0
      ikeyword_set = 1;
3173
0
      continue;
3174
0
    } else if (broken_list[i][0] == '!' ||
3175
0
         broken_list[i][0] == '+' ||
3176
0
         broken_list[i][0] == '-') {
3177
0
      if (broken_list[i][0] == '+') {
3178
0
        fn = prio_add;
3179
0
        bulk_fn = _add_priority;
3180
0
        bulk_given_fn = _add_priority;
3181
0
      } else {
3182
0
        fn = prio_remove;
3183
0
        bulk_fn = _clear_priorities;
3184
0
        bulk_given_fn = _clear_given_priorities;
3185
0
      }
3186
3187
0
      if (broken_list[i][0] == '+' &&
3188
0
          check_level(&broken_list[i][1], *priority_cache,
3189
0
          1) != 0) {
3190
0
        continue;
3191
0
      } else if ((algo = gnutls_mac_get_id(
3192
0
              &broken_list[i][1])) !=
3193
0
           GNUTLS_MAC_UNKNOWN) {
3194
0
        fn(&(*priority_cache)->_mac, algo);
3195
0
      } else if ((centry = cipher_name_to_entry(
3196
0
              &broken_list[i][1])) != NULL) {
3197
0
        if (_gnutls_cipher_exists(centry->id)) {
3198
0
          fn(&(*priority_cache)->_cipher,
3199
0
             centry->id);
3200
0
          if (centry->type == CIPHER_BLOCK)
3201
0
            (*priority_cache)->have_cbc = 1;
3202
0
        }
3203
0
      } else if ((algo = _gnutls_kx_get_id(
3204
0
              &broken_list[i][1])) !=
3205
0
           GNUTLS_KX_UNKNOWN) {
3206
0
        if (algo != GNUTLS_KX_INVALID)
3207
0
          fn(&(*priority_cache)->_kx, algo);
3208
0
      } else if (PRIO_MATCH("VERS-") == 0) {
3209
0
        if (PRIO_MATCH("VERS-TLS-ALL") == 0) {
3210
0
          bulk_given_fn(
3211
0
            &(*priority_cache)->protocol,
3212
0
            stream_protocol_priority);
3213
0
        } else if (PRIO_MATCH("VERS-DTLS-ALL") == 0) {
3214
0
          bulk_given_fn(
3215
0
            &(*priority_cache)->protocol,
3216
0
            (bulk_given_fn ==
3217
0
             _add_priority) ?
3218
0
              dtls_protocol_priority :
3219
0
              dgram_protocol_priority);
3220
0
        } else if (PRIO_MATCH("VERS-ALL") == 0) {
3221
0
          bulk_fn(&(*priority_cache)->protocol,
3222
0
            protocol_priority);
3223
0
        } else {
3224
0
          if ((algo = gnutls_protocol_get_id(
3225
0
                 &broken_list[i][6])) !=
3226
0
              GNUTLS_VERSION_UNKNOWN) {
3227
0
            fn(&(*priority_cache)->protocol,
3228
0
               algo);
3229
0
          } else
3230
0
            goto error;
3231
0
        }
3232
0
      } /* now check if the element is something like -ALGO */
3233
0
      else if (PRIO_MATCH("COMP-") == 0) {
3234
        /* ignore all compression methods */
3235
0
        continue;
3236
0
      } /* now check if the element is something like -ALGO */
3237
0
      else if (PRIO_MATCH("CURVE-") == 0) {
3238
0
        if (PRIO_MATCH("CURVE-ALL") == 0) {
3239
0
          bulk_fn(&(*priority_cache)
3240
0
               ->_supported_ecc,
3241
0
            supported_groups_normal);
3242
0
        } else {
3243
0
          if ((algo = gnutls_ecc_curve_get_id(
3244
0
                 &broken_list[i][7])) !=
3245
0
              GNUTLS_ECC_CURVE_INVALID)
3246
0
            fn(&(*priority_cache)
3247
0
                  ->_supported_ecc,
3248
0
               algo);
3249
0
          else
3250
0
            goto error;
3251
0
        }
3252
0
      } else if (PRIO_MATCH("GROUP-") == 0) {
3253
0
        if (PRIO_MATCH("GROUP-ALL") == 0) {
3254
0
          bulk_fn(&(*priority_cache)
3255
0
               ->_supported_ecc,
3256
0
            supported_groups_normal);
3257
0
        } else if (PRIO_MATCH("GROUP-DH-ALL") == 0) {
3258
0
          bulk_given_fn(&(*priority_cache)
3259
0
                     ->_supported_ecc,
3260
0
                  _supported_groups_dh);
3261
0
        } else if (PRIO_MATCH("GROUP-EC-ALL") == 0) {
3262
0
          bulk_given_fn(&(*priority_cache)
3263
0
                     ->_supported_ecc,
3264
0
                  _supported_groups_ecdh);
3265
0
        } else if (PRIO_MATCH("GROUP-GOST-ALL") == 0) {
3266
0
          bulk_given_fn(&(*priority_cache)
3267
0
                     ->_supported_ecc,
3268
0
                  _supported_groups_gost);
3269
0
        } else {
3270
0
          if ((algo = _gnutls_group_get_id(
3271
0
                 &broken_list[i][7])) !=
3272
0
              GNUTLS_GROUP_INVALID)
3273
0
            fn(&(*priority_cache)
3274
0
                  ->_supported_ecc,
3275
0
               algo);
3276
0
          else
3277
0
            goto error;
3278
0
        }
3279
0
      } else if (PRIO_MATCH("CTYPE-") == 0) {
3280
        // Certificate types
3281
0
        if (PRIO_MATCH("CTYPE-ALL") == 0) {
3282
          // Symmetric cert types, all types allowed
3283
0
          bulk_fn(&(*priority_cache)->client_ctype,
3284
0
            cert_type_priority_all);
3285
0
          bulk_fn(&(*priority_cache)->server_ctype,
3286
0
            cert_type_priority_all);
3287
0
        } else if (PRIO_MATCH("CTYPE-CLI-") == 0) {
3288
          // Client certificate types
3289
0
          if (PRIO_MATCH("CTYPE-CLI-ALL") == 0) {
3290
            // All client cert types allowed
3291
0
            bulk_fn(&(*priority_cache)
3292
0
                 ->client_ctype,
3293
0
              cert_type_priority_all);
3294
0
          } else if ((algo = gnutls_certificate_type_get_id(
3295
0
                  &broken_list[i]
3296
0
                  [11])) !=
3297
0
               GNUTLS_CRT_UNKNOWN) {
3298
            // Specific client cert type allowed
3299
0
            fn(&(*priority_cache)
3300
0
                  ->client_ctype,
3301
0
               algo);
3302
0
          } else
3303
0
            goto error;
3304
0
        } else if (PRIO_MATCH("CTYPE-SRV-") == 0) {
3305
          // Server certificate types
3306
0
          if (PRIO_MATCH("CTYPE-SRV-ALL") == 0) {
3307
            // All server cert types allowed
3308
0
            bulk_fn(&(*priority_cache)
3309
0
                 ->server_ctype,
3310
0
              cert_type_priority_all);
3311
0
          } else if ((algo = gnutls_certificate_type_get_id(
3312
0
                  &broken_list[i]
3313
0
                  [11])) !=
3314
0
               GNUTLS_CRT_UNKNOWN) {
3315
            // Specific server cert type allowed
3316
0
            fn(&(*priority_cache)
3317
0
                  ->server_ctype,
3318
0
               algo);
3319
0
          } else
3320
0
            goto error;
3321
0
        } else { // Symmetric certificate type
3322
0
          if ((algo = gnutls_certificate_type_get_id(
3323
0
                 &broken_list[i][7])) !=
3324
0
              GNUTLS_CRT_UNKNOWN) {
3325
0
            fn(&(*priority_cache)
3326
0
                  ->client_ctype,
3327
0
               algo);
3328
0
            fn(&(*priority_cache)
3329
0
                  ->server_ctype,
3330
0
               algo);
3331
0
          } else if (PRIO_MATCH(
3332
0
                 "CTYPE-OPENPGP") ==
3333
0
               0) {
3334
            /* legacy openpgp option - ignore */
3335
0
            continue;
3336
0
          } else
3337
0
            goto error;
3338
0
        }
3339
0
      } else if (PRIO_MATCH("SIGN-") == 0) {
3340
0
        if (PRIO_MATCH("SIGN-ALL") == 0) {
3341
0
          bulk_fn(&(*priority_cache)->_sign_algo,
3342
0
            sign_priority_default);
3343
0
        } else if (PRIO_MATCH("SIGN-GOST-ALL") == 0) {
3344
0
          bulk_fn(&(*priority_cache)->_sign_algo,
3345
0
            sign_priority_gost);
3346
0
        } else {
3347
0
          if ((algo = gnutls_sign_get_id(
3348
0
                 &broken_list[i][6])) !=
3349
0
              GNUTLS_SIGN_UNKNOWN)
3350
0
            fn(&(*priority_cache)
3351
0
                  ->_sign_algo,
3352
0
               algo);
3353
0
          else
3354
0
            goto error;
3355
0
        }
3356
0
      } else if (PRIO_MATCH("MAC-") == 0) {
3357
0
        if (PRIO_MATCH("MAC-ALL") == 0) {
3358
0
          bulk_fn(&(*priority_cache)->_mac,
3359
0
            mac_priority_normal);
3360
0
        } else if (PRIO_MATCH("MAC-GOST-ALL") == 0) {
3361
0
          bulk_fn(&(*priority_cache)->_mac,
3362
0
            mac_priority_gost);
3363
0
        }
3364
0
      } else if (PRIO_MATCH("CIPHER-") == 0) {
3365
0
        if (PRIO_MATCH("CIPHER-ALL") == 0) {
3366
0
          bulk_fn(&(*priority_cache)->_cipher,
3367
0
            cipher_priority_normal);
3368
0
        } else if (PRIO_MATCH("CIPHER-GOST-ALL") == 0) {
3369
0
          bulk_fn(&(*priority_cache)->_cipher,
3370
0
            cipher_priority_gost);
3371
0
        }
3372
0
      } else if (PRIO_MATCH("KX-") == 0) {
3373
0
        if (PRIO_MATCH("KX-ALL") == 0) {
3374
0
          bulk_fn(&(*priority_cache)->_kx,
3375
0
            kx_priority_secure);
3376
0
        } else if (PRIO_MATCH("KX-GOST-ALL") == 0) {
3377
0
          bulk_fn(&(*priority_cache)->_kx,
3378
0
            kx_priority_gost);
3379
0
        }
3380
0
      } else if (PRIO_MATCH("GOST") == 0) {
3381
0
        bulk_given_fn(
3382
0
          &(*priority_cache)->_supported_ecc,
3383
0
          _supported_groups_gost);
3384
0
        bulk_fn(&(*priority_cache)->_sign_algo,
3385
0
          sign_priority_gost);
3386
0
        bulk_fn(&(*priority_cache)->_mac,
3387
0
          mac_priority_gost);
3388
0
        bulk_fn(&(*priority_cache)->_cipher,
3389
0
          cipher_priority_gost);
3390
0
        bulk_fn(&(*priority_cache)->_kx,
3391
0
          kx_priority_gost);
3392
0
      } else
3393
0
        goto error;
3394
0
    } else if (broken_list[i][0] == '%') {
3395
0
      const struct priority_options_st *o;
3396
      /* to add a new option modify
3397
       * priority_options.gperf */
3398
0
      o = in_word_set(&broken_list[i][1],
3399
0
          strlen(&broken_list[i][1]));
3400
0
      if (o == NULL) {
3401
0
        goto error;
3402
0
      }
3403
0
      o->func(*priority_cache);
3404
0
    } else
3405
0
      goto error;
3406
0
  }
3407
3408
  /* This needs to be done after parsing modifiers, as
3409
   * tls-session-hash has precedence over modifiers.
3410
   */
3411
0
  if (system_wide_config.force_ext_master_secret_set) {
3412
0
    (*priority_cache)->force_ext_master_secret =
3413
0
      system_wide_config.force_ext_master_secret;
3414
0
    (*priority_cache)->_no_ext_master_secret = false;
3415
0
  }
3416
3417
0
  ret = set_ciphersuite_list(*priority_cache);
3418
0
  if (ret < 0) {
3419
0
    if (err_pos)
3420
0
      *err_pos = priorities;
3421
0
    goto error_cleanup;
3422
0
  }
3423
3424
0
  gnutls_free(darg);
3425
3426
0
  return 0;
3427
3428
0
error:
3429
0
  if (err_pos != NULL && i < broken_list_size && resolved_match) {
3430
0
    *err_pos = priorities;
3431
0
    for (j = 0; j < i; j++) {
3432
0
      (*err_pos) += strlen(broken_list[j]) + 1;
3433
0
    }
3434
0
  }
3435
0
  ret = GNUTLS_E_INVALID_REQUEST;
3436
3437
0
error_cleanup:
3438
0
  gnutls_free(darg);
3439
0
  gnutls_priority_deinit(*priority_cache);
3440
0
  *priority_cache = NULL;
3441
3442
0
  return ret;
3443
0
}
3444
3445
/**
3446
 * gnutls_priority_deinit:
3447
 * @priority_cache: is a #gnutls_priority_t type.
3448
 *
3449
 * Deinitializes the priority cache.
3450
 **/
3451
void gnutls_priority_deinit(gnutls_priority_t priority_cache)
3452
0
{
3453
0
  if (priority_cache == NULL)
3454
0
    return;
3455
3456
  /* Note that here we care about the following two cases:
3457
   * 1. Multiple sessions or different threads holding a reference + a global reference
3458
   * 2. One session holding a reference with a possible global reference
3459
   *
3460
   * As such, it will never be that two threads reach the
3461
   * zero state at the same time, unless the global reference
3462
   * is cleared too, which is invalid state.
3463
   */
3464
0
  if (gnutls_atomic_val(&priority_cache->usage_cnt) == 0) {
3465
0
    gnutls_atomic_deinit(&priority_cache->usage_cnt);
3466
0
    gnutls_free(priority_cache);
3467
0
    return;
3468
0
  } else {
3469
0
    gnutls_atomic_decrement(&priority_cache->usage_cnt);
3470
0
  }
3471
0
}
3472
3473
/**
3474
 * gnutls_priority_set_direct:
3475
 * @session: is a #gnutls_session_t type.
3476
 * @priorities: is a string describing priorities
3477
 * @err_pos: In case of an error this will have the position in the string the error occurred
3478
 *
3479
 * Sets the priorities to use on the ciphers, key exchange methods,
3480
 * and macs.  This function avoids keeping a
3481
 * priority cache and is used to directly set string priorities to a
3482
 * TLS session.  For documentation check the gnutls_priority_init().
3483
 *
3484
 * To use a reasonable default, consider using gnutls_set_default_priority(),
3485
 * or gnutls_set_default_priority_append() instead of this function.
3486
 *
3487
 * Returns: On syntax error %GNUTLS_E_INVALID_REQUEST is returned,
3488
 * %GNUTLS_E_SUCCESS on success, or an error code.
3489
 **/
3490
int gnutls_priority_set_direct(gnutls_session_t session, const char *priorities,
3491
             const char **err_pos)
3492
0
{
3493
0
  gnutls_priority_t prio;
3494
0
  int ret;
3495
3496
0
  ret = gnutls_priority_init(&prio, priorities, err_pos);
3497
0
  if (ret < 0) {
3498
0
    gnutls_assert();
3499
0
    return ret;
3500
0
  }
3501
3502
0
  ret = gnutls_priority_set(session, prio);
3503
0
  if (ret < 0) {
3504
0
    gnutls_assert();
3505
0
    return ret;
3506
0
  }
3507
3508
  /* ensure that the session holds the only reference for the struct */
3509
0
  gnutls_priority_deinit(prio);
3510
3511
0
  return 0;
3512
0
}
3513
3514
/* Breaks a list of "xxx", "yyy", to a character array, of
3515
 * MAX_COMMA_SEP_ELEMENTS size; Note that the given string is modified.
3516
  */
3517
static void break_list(char *list, char *broken_list[MAX_ELEMENTS], int *size)
3518
0
{
3519
0
  char *p = list;
3520
3521
0
  *size = 0;
3522
3523
0
  do {
3524
0
    broken_list[*size] = p;
3525
3526
0
    (*size)++;
3527
3528
0
    p = strchr(p, ':');
3529
0
    if (p) {
3530
0
      *p = 0;
3531
0
      p++; /* move to next entry and skip white
3532
         * space.
3533
         */
3534
0
      while (*p == ' ')
3535
0
        p++;
3536
0
    }
3537
0
  } while (p != NULL && *size < MAX_ELEMENTS);
3538
0
}
3539
3540
/**
3541
 * gnutls_set_default_priority:
3542
 * @session: is a #gnutls_session_t type.
3543
 *
3544
 * Sets the default priority on the ciphers, key exchange methods,
3545
 * and macs. This is the recommended method of
3546
 * setting the defaults, in order to promote consistency between applications
3547
 * using GnuTLS, and to allow GnuTLS using applications to update settings
3548
 * in par with the library. For client applications which require
3549
 * maximum compatibility consider calling gnutls_session_enable_compatibility_mode()
3550
 * after this function.
3551
 *
3552
 * For an application to specify additional options to priority string
3553
 * consider using gnutls_set_default_priority_append().
3554
 *
3555
 * To allow a user to override the defaults (e.g., when a user interface
3556
 * or configuration file is available), the functions
3557
 * gnutls_priority_set_direct() or gnutls_priority_set() can
3558
 * be used.
3559
 *
3560
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
3561
 *
3562
 * Since: 2.1.4
3563
 **/
3564
int gnutls_set_default_priority(gnutls_session_t session)
3565
0
{
3566
0
  return gnutls_priority_set_direct(session, NULL, NULL);
3567
0
}
3568
3569
/**
3570
 * gnutls_set_default_priority_append:
3571
 * @session: is a #gnutls_session_t type.
3572
 * @add_prio: is a string describing priorities to be appended to default
3573
 * @err_pos: In case of an error this will have the position in the string the error occurred
3574
 * @flags: must be zero
3575
 *
3576
 * Sets the default priority on the ciphers, key exchange methods,
3577
 * and macs with the additional options in @add_prio. This is the recommended method of
3578
 * setting the defaults when only few additional options are to be added. This promotes
3579
 * consistency between applications using GnuTLS, and allows GnuTLS using applications
3580
 * to update settings in par with the library.
3581
 *
3582
 * The @add_prio string should start as a normal priority string, e.g.,
3583
 * '-VERS-TLS-ALL:+VERS-TLS1.3:%%COMPAT' or '%%FORCE_ETM'. That is, it must not start
3584
 * with ':'.
3585
 *
3586
 * To allow a user to override the defaults (e.g., when a user interface
3587
 * or configuration file is available), the functions
3588
 * gnutls_priority_set_direct() or gnutls_priority_set() can
3589
 * be used.
3590
 *
3591
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
3592
 *
3593
 * Since: 3.6.3
3594
 **/
3595
int gnutls_set_default_priority_append(gnutls_session_t session,
3596
               const char *add_prio,
3597
               const char **err_pos, unsigned flags)
3598
0
{
3599
0
  gnutls_priority_t prio;
3600
0
  int ret;
3601
3602
0
  ret = gnutls_priority_init2(&prio, add_prio, err_pos,
3603
0
            GNUTLS_PRIORITY_INIT_DEF_APPEND);
3604
0
  if (ret < 0) {
3605
0
    gnutls_assert();
3606
0
    return ret;
3607
0
  }
3608
3609
0
  ret = gnutls_priority_set(session, prio);
3610
0
  if (ret < 0) {
3611
0
    gnutls_assert();
3612
0
    return ret;
3613
0
  }
3614
3615
  /* ensure that the session holds the only reference for the struct */
3616
0
  gnutls_priority_deinit(prio);
3617
3618
0
  return 0;
3619
0
}
3620
3621
/**
3622
 * gnutls_priority_ecc_curve_list:
3623
 * @pcache: is a #gnutls_priority_t type.
3624
 * @list: will point to an integer list
3625
 *
3626
 * Get a list of available elliptic curves in the priority
3627
 * structure.
3628
 *
3629
 * Deprecated: This function has been replaced by
3630
 * gnutls_priority_group_list() since 3.6.0.
3631
 *
3632
 * Returns: the number of items, or an error code.
3633
 *
3634
 * Since: 3.0
3635
 **/
3636
int gnutls_priority_ecc_curve_list(gnutls_priority_t pcache,
3637
           const unsigned int **list)
3638
0
{
3639
0
  unsigned i;
3640
3641
0
  if (pcache->_supported_ecc.num_priorities == 0)
3642
0
    return 0;
3643
3644
0
  *list = pcache->_supported_ecc.priorities;
3645
3646
  /* to ensure we don't confuse the caller, we do not include
3647
   * any FFDHE groups. This may return an incomplete list. */
3648
0
  for (i = 0; i < pcache->_supported_ecc.num_priorities; i++)
3649
0
    if (pcache->_supported_ecc.priorities[i] > GNUTLS_ECC_CURVE_MAX)
3650
0
      return i;
3651
3652
0
  return pcache->_supported_ecc.num_priorities;
3653
0
}
3654
3655
/**
3656
 * gnutls_priority_group_list:
3657
 * @pcache: is a #gnutls_priority_t type.
3658
 * @list: will point to an integer list
3659
 *
3660
 * Get a list of available groups in the priority
3661
 * structure.
3662
 *
3663
 * Returns: the number of items, or an error code.
3664
 *
3665
 * Since: 3.6.0
3666
 **/
3667
int gnutls_priority_group_list(gnutls_priority_t pcache,
3668
             const unsigned int **list)
3669
0
{
3670
0
  if (pcache->_supported_ecc.num_priorities == 0)
3671
0
    return 0;
3672
3673
0
  *list = pcache->_supported_ecc.priorities;
3674
0
  return pcache->_supported_ecc.num_priorities;
3675
0
}
3676
3677
/**
3678
 * gnutls_priority_kx_list:
3679
 * @pcache: is a #gnutls_priority_t type.
3680
 * @list: will point to an integer list
3681
 *
3682
 * Get a list of available key exchange methods in the priority
3683
 * structure.
3684
 *
3685
 * Returns: the number of items, or an error code.
3686
 * Since: 3.2.3
3687
 **/
3688
int gnutls_priority_kx_list(gnutls_priority_t pcache, const unsigned int **list)
3689
0
{
3690
0
  if (pcache->_kx.num_priorities == 0)
3691
0
    return 0;
3692
3693
0
  *list = pcache->_kx.priorities;
3694
0
  return pcache->_kx.num_priorities;
3695
0
}
3696
3697
/**
3698
 * gnutls_priority_cipher_list:
3699
 * @pcache: is a #gnutls_priority_t type.
3700
 * @list: will point to an integer list
3701
 *
3702
 * Get a list of available ciphers in the priority
3703
 * structure.
3704
 *
3705
 * Returns: the number of items, or an error code.
3706
 * Since: 3.2.3
3707
 **/
3708
int gnutls_priority_cipher_list(gnutls_priority_t pcache,
3709
        const unsigned int **list)
3710
0
{
3711
0
  if (pcache->_cipher.num_priorities == 0)
3712
0
    return 0;
3713
3714
0
  *list = pcache->_cipher.priorities;
3715
0
  return pcache->_cipher.num_priorities;
3716
0
}
3717
3718
/**
3719
 * gnutls_priority_mac_list:
3720
 * @pcache: is a #gnutls_priority_t type.
3721
 * @list: will point to an integer list
3722
 *
3723
 * Get a list of available MAC algorithms in the priority
3724
 * structure.
3725
 *
3726
 * Returns: the number of items, or an error code.
3727
 * Since: 3.2.3
3728
 **/
3729
int gnutls_priority_mac_list(gnutls_priority_t pcache,
3730
           const unsigned int **list)
3731
0
{
3732
0
  if (pcache->_mac.num_priorities == 0)
3733
0
    return 0;
3734
3735
0
  *list = pcache->_mac.priorities;
3736
0
  return pcache->_mac.num_priorities;
3737
0
}
3738
3739
/**
3740
 * gnutls_priority_compression_list:
3741
 * @pcache: is a #gnutls_priority_t type.
3742
 * @list: will point to an integer list
3743
 *
3744
 * Get a list of available compression method in the priority
3745
 * structure.
3746
 *
3747
 * Returns: the number of methods, or an error code.
3748
 * Since: 3.0
3749
 **/
3750
int gnutls_priority_compression_list(gnutls_priority_t pcache,
3751
             const unsigned int **list)
3752
0
{
3753
0
  static const unsigned int priority[1] = { GNUTLS_COMP_NULL };
3754
3755
0
  *list = priority;
3756
0
  return 1;
3757
0
}
3758
3759
/**
3760
 * gnutls_priority_protocol_list:
3761
 * @pcache: is a #gnutls_priority_t type.
3762
 * @list: will point to an integer list
3763
 *
3764
 * Get a list of available TLS version numbers in the priority
3765
 * structure.
3766
 *
3767
 * Returns: the number of protocols, or an error code.
3768
 * Since: 3.0
3769
 **/
3770
int gnutls_priority_protocol_list(gnutls_priority_t pcache,
3771
          const unsigned int **list)
3772
0
{
3773
0
  if (pcache->protocol.num_priorities == 0)
3774
0
    return 0;
3775
3776
0
  *list = pcache->protocol.priorities;
3777
0
  return pcache->protocol.num_priorities;
3778
0
}
3779
3780
/**
3781
 * gnutls_priority_sign_list:
3782
 * @pcache: is a #gnutls_priority_t type.
3783
 * @list: will point to an integer list
3784
 *
3785
 * Get a list of available signature algorithms in the priority
3786
 * structure.
3787
 *
3788
 * Returns: the number of algorithms, or an error code.
3789
 * Since: 3.0
3790
 **/
3791
int gnutls_priority_sign_list(gnutls_priority_t pcache,
3792
            const unsigned int **list)
3793
0
{
3794
0
  if (pcache->_sign_algo.num_priorities == 0)
3795
0
    return 0;
3796
3797
0
  *list = pcache->_sign_algo.priorities;
3798
0
  return pcache->_sign_algo.num_priorities;
3799
0
}
3800
3801
/**
3802
 * gnutls_priority_certificate_type_list:
3803
 * @pcache: is a #gnutls_priority_t type.
3804
 * @list: will point to an integer list
3805
 *
3806
 * Get a list of available certificate types in the priority
3807
 * structure.
3808
 *
3809
 * As of version 3.6.4 this function is an alias for
3810
 * gnutls_priority_certificate_type_list2 with the target parameter
3811
 * set to:
3812
 * - GNUTLS_CTYPE_SERVER, if the %SERVER_PRECEDENCE option is set
3813
 * - GNUTLS_CTYPE_CLIENT, otherwise.
3814
 *
3815
 * Returns: the number of certificate types, or an error code.
3816
 * Since: 3.0
3817
 **/
3818
int gnutls_priority_certificate_type_list(gnutls_priority_t pcache,
3819
            const unsigned int **list)
3820
0
{
3821
0
  gnutls_ctype_target_t target = pcache->server_precedence ?
3822
0
                 GNUTLS_CTYPE_SERVER :
3823
0
                 GNUTLS_CTYPE_CLIENT;
3824
3825
0
  return gnutls_priority_certificate_type_list2(pcache, list, target);
3826
0
}
3827
3828
/**
3829
 * gnutls_priority_certificate_type_list2:
3830
 * @pcache: is a #gnutls_priority_t type.
3831
 * @list: will point to an integer list.
3832
 * @target: is a #gnutls_ctype_target_t type. Valid arguments are
3833
 *   GNUTLS_CTYPE_CLIENT and GNUTLS_CTYPE_SERVER
3834
 *
3835
 * Get a list of available certificate types for the given target
3836
 * in the priority structure.
3837
 *
3838
 * Returns: the number of certificate types, or an error code.
3839
 *
3840
 * Since: 3.6.4
3841
 **/
3842
int gnutls_priority_certificate_type_list2(gnutls_priority_t pcache,
3843
             const unsigned int **list,
3844
             gnutls_ctype_target_t target)
3845
0
{
3846
0
  switch (target) {
3847
0
  case GNUTLS_CTYPE_CLIENT:
3848
0
    if (pcache->client_ctype.num_priorities > 0) {
3849
0
      *list = pcache->client_ctype.priorities;
3850
0
      return pcache->client_ctype.num_priorities;
3851
0
    }
3852
0
    break;
3853
0
  case GNUTLS_CTYPE_SERVER:
3854
0
    if (pcache->server_ctype.num_priorities > 0) {
3855
0
      *list = pcache->server_ctype.priorities;
3856
0
      return pcache->server_ctype.num_priorities;
3857
0
    }
3858
0
    break;
3859
0
  default:
3860
    // Invalid target given
3861
0
    gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3862
0
  }
3863
3864
  // Found a matching target but non of them had any ctypes set
3865
0
  return 0;
3866
0
}
3867
3868
/**
3869
 * gnutls_priority_string_list:
3870
 * @iter: an integer counter starting from zero
3871
 * @flags: one of %GNUTLS_PRIORITY_LIST_INIT_KEYWORDS, %GNUTLS_PRIORITY_LIST_SPECIAL
3872
 *
3873
 * Can be used to iterate all available priority strings.
3874
 * Due to internal implementation details, there are cases where this
3875
 * function can return the empty string. In that case that string should be ignored.
3876
 * When no strings are available it returns %NULL.
3877
 *
3878
 * Returns: a priority string
3879
 * Since: 3.4.0
3880
 **/
3881
const char *gnutls_priority_string_list(unsigned iter, unsigned int flags)
3882
0
{
3883
0
  if (flags & GNUTLS_PRIORITY_LIST_INIT_KEYWORDS) {
3884
0
    if (iter >= (sizeof(pgroups) / sizeof(pgroups[0])) - 1)
3885
0
      return NULL;
3886
0
    return pgroups[iter].name;
3887
0
  } else if (flags & GNUTLS_PRIORITY_LIST_SPECIAL) {
3888
0
    if (iter >= (sizeof(wordlist) / sizeof(wordlist[0])) - 1)
3889
0
      return NULL;
3890
0
    return wordlist[iter].name;
3891
0
  }
3892
0
  return NULL;
3893
0
}
3894
3895
bool _gnutls_config_is_ktls_enabled(void)
3896
0
{
3897
0
  return system_wide_config.ktls_enabled;
3898
0
}
3899
3900
/*
3901
 * high-level interface for overriding configuration files
3902
 */
3903
3904
static inline bool /* not locking system_wide_config */
3905
system_wide_config_is_malleable(void)
3906
0
{
3907
0
  if (!system_wide_config.allowlisting) {
3908
0
    _gnutls_audit_log(NULL, "allowlisting is not enabled!\n");
3909
0
    return false;
3910
0
  }
3911
0
  if (system_wide_config.priority_string) {
3912
0
    _gnutls_audit_log(NULL, "priority strings have already been "
3913
0
          "initialized!\n");
3914
0
    return false;
3915
0
  }
3916
0
  return true;
3917
0
}
3918
3919
/**
3920
 * gnutls_digest_set_secure:
3921
 * @dig: is a digest algorithm
3922
 * @secure: whether to mark the digest algorithm secure
3923
 *
3924
 * Modify the previous system wide setting that marked @dig as secure
3925
 * or insecure. This only has effect when the algorithm is enabled
3926
 * through the allowlisting mode in the configuration file, or when
3927
 * the setting is modified with a prior call to this function.
3928
 *
3929
 * Since: 3.7.3
3930
 */
3931
int gnutls_digest_set_secure(gnutls_digest_algorithm_t dig, unsigned int secure)
3932
0
{
3933
0
#ifndef DISABLE_SYSTEM_CONFIG
3934
0
  int ret;
3935
0
  ret = gnutls_rwlock_wrlock(&system_wide_config_rwlock);
3936
0
  if (ret < 0) {
3937
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
3938
0
    return gnutls_assert_val(ret);
3939
0
  }
3940
0
  if (!system_wide_config_is_malleable()) {
3941
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
3942
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3943
0
  }
3944
3945
0
  if (secure) {
3946
0
    ret = cfg_hashes_add(&system_wide_config, dig);
3947
0
  } else {
3948
0
    ret = cfg_hashes_remove(&system_wide_config, dig);
3949
0
  }
3950
3951
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
3952
0
  return ret;
3953
#else
3954
  return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3955
#endif
3956
0
}
3957
3958
/**
3959
 * gnutls_sign_set_secure:
3960
 * @sign: the sign algorithm
3961
 * @secure: whether to mark the sign algorithm secure
3962
 *
3963
 * Modify the previous system wide setting that marked @sign as secure
3964
 * or insecure.  Calling this function is allowed
3965
 * only if allowlisting mode is set in the configuration file,
3966
 * and only if the system-wide TLS priority string
3967
 * has not been initialized yet.
3968
 * The intended usage is to provide applications with a way
3969
 * to expressly deviate from the distribution or site defaults
3970
 * inherited from the configuration file.
3971
 * The modification is composable with further modifications
3972
 * performed through the priority string mechanism.
3973
 *
3974
 * This function is not thread-safe and is intended to be called
3975
 * in the main thread at the beginning of the process execution.
3976
 *
3977
 * Even when @secure is true, @sign is not marked as secure for the
3978
 * use in certificates.  Use gnutls_sign_set_secure_for_certs() to
3979
 * mark it secure as well for certificates.
3980
 *
3981
 * Returns: 0 on success or negative error code otherwise.
3982
 *
3983
 * Since: 3.7.3
3984
 */
3985
int gnutls_sign_set_secure(gnutls_sign_algorithm_t sign, unsigned int secure)
3986
0
{
3987
0
#ifndef DISABLE_SYSTEM_CONFIG
3988
0
  int ret;
3989
0
  ret = gnutls_rwlock_wrlock(&system_wide_config_rwlock);
3990
0
  if (ret < 0) {
3991
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
3992
0
    return gnutls_assert_val(ret);
3993
0
  }
3994
0
  if (!system_wide_config_is_malleable()) {
3995
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
3996
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
3997
0
  }
3998
3999
0
  if (secure) {
4000
0
    ret = cfg_sigs_add(&system_wide_config, sign);
4001
0
  } else {
4002
0
    ret = cfg_sigs_remove(&system_wide_config, sign);
4003
0
    if (ret < 0) {
4004
0
      (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4005
0
      return ret;
4006
0
    }
4007
    /* irregularity, distrusting also means distrusting for certs */
4008
0
    ret = cfg_sigs_for_cert_remove(&system_wide_config, sign);
4009
0
  }
4010
4011
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4012
0
  return ret;
4013
#else
4014
  return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4015
#endif
4016
0
}
4017
4018
/**
4019
 * gnutls_sign_set_secure_for_certs:
4020
 * @sign: the sign algorithm
4021
 * @secure: whether to mark the sign algorithm secure for certificates
4022
 *
4023
 * Modify the previous system wide setting that marked @sign as secure
4024
 * or insecure for the use in certificates.  Calling this function is allowed
4025
 * only if allowlisting mode is set in the configuration file,
4026
 * and only if the system-wide TLS priority string
4027
 * has not been initialized yet.
4028
 * The intended usage is to provide applications with a way
4029
 * to expressly deviate from the distribution or site defaults
4030
 * inherited from the configuration file.
4031
 * The modification is composable with further modifications
4032
 * performed through the priority string mechanism.
4033
 *
4034
 * This function is not thread-safe and is intended to be called
4035
 * in the main thread at the beginning of the process execution.
4036
4037
 * When @secure is true, @sign is marked as secure for any use unlike
4038
 * gnutls_sign_set_secure().  Otherwise, it is marked as insecure only
4039
 * for the use in certificates.  Use gnutls_sign_set_secure() to mark
4040
 * it insecure for any uses.
4041
 *
4042
 * Returns: 0 on success or negative error code otherwise.
4043
 *
4044
 * Since: 3.7.3
4045
 */
4046
int gnutls_sign_set_secure_for_certs(gnutls_sign_algorithm_t sign,
4047
             unsigned int secure)
4048
0
{
4049
0
#ifndef DISABLE_SYSTEM_CONFIG
4050
0
  int ret;
4051
0
  ret = gnutls_rwlock_wrlock(&system_wide_config_rwlock);
4052
0
  if (ret < 0) {
4053
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4054
0
    return gnutls_assert_val(ret);
4055
0
  }
4056
0
  if (!system_wide_config_is_malleable()) {
4057
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4058
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4059
0
  }
4060
4061
0
  if (secure) {
4062
    /* irregularity, trusting for certs means trusting in general */
4063
0
    ret = cfg_sigs_add(&system_wide_config, sign);
4064
0
    if (ret < 0) {
4065
0
      (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4066
0
      return ret;
4067
0
    }
4068
0
    ret = cfg_sigs_for_cert_add(&system_wide_config, sign);
4069
0
  } else {
4070
0
    ret = cfg_sigs_for_cert_remove(&system_wide_config, sign);
4071
0
  }
4072
4073
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4074
0
  return ret;
4075
#else
4076
  return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4077
#endif
4078
0
}
4079
4080
/**
4081
 * gnutls_protocol_set_enabled:
4082
 * @version: is a (gnutls) version number
4083
 * @enabled: whether to enable the protocol
4084
 *
4085
 * Control the previous system-wide setting that marked @version as
4086
 * enabled or disabled.  Calling this function is allowed
4087
 * only if allowlisting mode is set in the configuration file,
4088
 * and only if the system-wide TLS priority string
4089
 * has not been initialized yet.
4090
 * The intended usage is to provide applications with a way
4091
 * to expressly deviate from the distribution or site defaults
4092
 * inherited from the configuration file.
4093
 * The modification is composable with further modifications
4094
 * performed through the priority string mechanism.
4095
 *
4096
 * This function is not thread-safe and is intended to be called
4097
 * in the main thread at the beginning of the process execution.
4098
 *
4099
 * Returns: 0 on success or negative error code otherwise.
4100
 *
4101
 * Since: 3.7.3
4102
 */
4103
int /* allowlisting-only */
4104
/* not thread-safe */
4105
gnutls_protocol_set_enabled(gnutls_protocol_t version, unsigned int enabled)
4106
0
{
4107
0
#ifndef DISABLE_SYSTEM_CONFIG
4108
0
  int ret;
4109
0
  ret = gnutls_rwlock_wrlock(&system_wide_config_rwlock);
4110
0
  if (ret < 0) {
4111
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4112
0
    return gnutls_assert_val(ret);
4113
0
  }
4114
0
  if (!system_wide_config_is_malleable()) {
4115
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4116
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4117
0
  }
4118
4119
0
  if (enabled) {
4120
0
    ret = cfg_versions_add(&system_wide_config, version);
4121
0
  } else {
4122
0
    ret = cfg_versions_remove(&system_wide_config, version);
4123
0
  }
4124
4125
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4126
0
  return ret;
4127
#else
4128
  return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4129
#endif
4130
0
}
4131
4132
/**
4133
 * gnutls_ecc_curve_set_enabled:
4134
 * @curve: is an ECC curve
4135
 * @enabled: whether to enable the curve
4136
 *
4137
 * Modify the previous system wide setting that marked @curve as
4138
 * enabled or disabled.  Calling this function is allowed
4139
 * only if allowlisting mode is set in the configuration file,
4140
 * and only if the system-wide TLS priority string
4141
 * has not been initialized yet.
4142
 * The intended usage is to provide applications with a way
4143
 * to expressly deviate from the distribution or site defaults
4144
 * inherited from the configuration file.
4145
 * The modification is composable with further modifications
4146
 * performed through the priority string mechanism.
4147
 *
4148
 * This function is not thread-safe and is intended to be called
4149
 * in the main thread at the beginning of the process execution.
4150
 *
4151
 * Returns: 0 on success or negative error code otherwise.
4152
 *
4153
 * Since: 3.7.3
4154
 */
4155
int gnutls_ecc_curve_set_enabled(gnutls_ecc_curve_t curve, unsigned int enabled)
4156
0
{
4157
0
#ifndef DISABLE_SYSTEM_CONFIG
4158
0
  int ret;
4159
0
  ret = gnutls_rwlock_wrlock(&system_wide_config_rwlock);
4160
0
  if (ret < 0) {
4161
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4162
0
    return gnutls_assert_val(ret);
4163
0
  }
4164
0
  if (!system_wide_config_is_malleable()) {
4165
0
    (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4166
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4167
0
  }
4168
4169
0
  if (enabled) {
4170
0
    ret = cfg_ecc_curves_add(&system_wide_config, curve);
4171
0
  } else {
4172
0
    ret = cfg_ecc_curves_remove(&system_wide_config, curve);
4173
0
  }
4174
4175
0
  (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
4176
0
  return ret;
4177
#else
4178
  return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
4179
#endif
4180
0
}