Coverage Report

Created: 2026-10-14 06:53

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