Coverage Report

Created: 2025-07-23 07:16

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