1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/*
* Copyright (C) 2011-2012 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
*/
#include "gnutls_int.h"
#include <algorithms.h>
#include "errors.h"
#include <x509/common.h>
#include "c-strcase.h"
/* TLS Versions */
static SYSTEM_CONFIG_OR_CONST
version_entry_st sup_versions[] = {
{.name = "SSL3.0",
.id = GNUTLS_SSL3,
.age = 0,
.major = 3,
.minor = 0,
.transport = GNUTLS_STREAM,
#ifdef ENABLE_SSL3
.supported = 1,
#endif
.explicit_iv = 0,
.extensions = 0,
.selectable_sighash = 0,
.selectable_prf = 0,
.obsolete = 1,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 0
},
{.name = "TLS1.0",
.id = GNUTLS_TLS1,
.age = 1,
.major = 3,
.minor = 1,
.transport = GNUTLS_STREAM,
.supported = 1,
.explicit_iv = 0,
.extensions = 1,
.selectable_sighash = 0,
.selectable_prf = 0,
.obsolete = 0,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 0
},
{.name = "TLS1.1",
.id = GNUTLS_TLS1_1,
.age = 2,
.major = 3,
.minor = 2,
.transport = GNUTLS_STREAM,
.supported = 1,
.explicit_iv = 1,
.extensions = 1,
.selectable_sighash = 0,
.selectable_prf = 0,
.obsolete = 0,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 0
},
{.name = "TLS1.2",
.id = GNUTLS_TLS1_2,
.age = 3,
.major = 3,
.minor = 3,
.transport = GNUTLS_STREAM,
.supported = 1,
.explicit_iv = 1,
.extensions = 1,
.selectable_sighash = 1,
.selectable_prf = 1,
.obsolete = 0,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 1
},
{.name = "TLS1.3",
.id = GNUTLS_TLS1_3,
.age = 5,
.major = 3,
.minor = 4,
.transport = GNUTLS_STREAM,
.supported = 1,
.explicit_iv = 0,
.extensions = 1,
.selectable_sighash = 1,
.selectable_prf = 1,
.tls13_sem = 1,
.obsolete = 0,
.only_extension = 1,
.post_handshake_auth = 1,
.multi_ocsp = 1,
.key_shares = 1,
.false_start = 0, /* doesn't make sense */
.tls_sig_sem = SIG_SEM_TLS13
},
{.name = "DTLS0.9", /* Cisco AnyConnect (based on about OpenSSL 0.9.8e) */
.id = GNUTLS_DTLS0_9,
.age = 200,
.major = 1,
.minor = 0,
.transport = GNUTLS_DGRAM,
.supported = 1,
.explicit_iv = 1,
.extensions = 1,
.selectable_sighash = 0,
.selectable_prf = 0,
.obsolete = 0,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 0
},
{.name = "DTLS1.0",
.id = GNUTLS_DTLS1_0,
.age = 201,
.major = 254,
.minor = 255,
.transport = GNUTLS_DGRAM,
.supported = 1,
.explicit_iv = 1,
.extensions = 1,
.selectable_sighash = 0,
.selectable_prf = 0,
.obsolete = 0,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 0
},
{.name = "DTLS1.2",
.id = GNUTLS_DTLS1_2,
.age = 202,
.major = 254,
.minor = 253,
.transport = GNUTLS_DGRAM,
.supported = 1,
.explicit_iv = 1,
.extensions = 1,
.selectable_sighash = 1,
.selectable_prf = 1,
.obsolete = 0,
.only_extension = 0,
.tls_sig_sem = SIG_SEM_PRE_TLS12,
.false_start = 1
},
{0, 0, 0, 0, 0}
};
const version_entry_st *version_to_entry(gnutls_protocol_t version)
{
const version_entry_st *p;
for (p = sup_versions; p->name != NULL; p++)
if (p->id == version)
return p;
return NULL;
}
const version_entry_st *nversion_to_entry(uint8_t major, uint8_t minor)
{
const version_entry_st *p;
for (p = sup_versions; p->name != NULL; p++) {
if ((p->major == major) && (p->minor == minor))
return p;
}
return NULL;
}
static int
version_is_valid_for_session(gnutls_session_t session,
const version_entry_st *v)
{
if (!v->supported && !(v->supported_revertible && _gnutls_allowlisting_mode()))
return 0;
if (v->transport != session->internals.transport)
return 0;
return 1;
}
/* This is only called by cfg_apply in priority.c, in blocklisting mode. */
int _gnutls_version_mark_disabled(gnutls_protocol_t version)
{
#ifndef DISABLE_SYSTEM_CONFIG
version_entry_st *p;
for (p = sup_versions; p->name != NULL; p++)
if (p->id == version) {
p->supported = false;
return 0;
}
#endif
return GNUTLS_E_INVALID_REQUEST;
}
/* This is only called by cfg_apply in priority.c, in allowlisting mode. */
void _gnutls_version_mark_disabled_all(void)
{
#ifndef DISABLE_SYSTEM_CONFIG
version_entry_st *p;
for (p = sup_versions; p->name != NULL; p++) {
p->supported = false;
p->supported_revertible = true;
}
#endif
}
int
_gnutls_protocol_set_enabled(gnutls_protocol_t version,
unsigned int enabled)
{
#ifndef DISABLE_SYSTEM_CONFIG
version_entry_st *p;
for (p = sup_versions; p->name != NULL; p++)
if (p->id == version) {
if (!p->supported_revertible) {
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
}
p->supported = enabled;
return 0;
}
#endif
return GNUTLS_E_INVALID_REQUEST;
}
/* Return the priority of the provided version number */
int
_gnutls_version_priority(gnutls_session_t session,
gnutls_protocol_t version)
{
unsigned int i;
for (i = 0; i < session->internals.priorities->protocol.num_priorities;
i++) {
if (session->internals.priorities->protocol.priorities[i] ==
version)
return i;
}
return -1;
}
/* Returns the lowest TLS version number in the priorities.
*/
const version_entry_st *_gnutls_version_lowest(gnutls_session_t session)
{
unsigned int i;
gnutls_protocol_t cur_prot;
const version_entry_st *v, *min_v = NULL;
const version_entry_st *backup = NULL;
for (i=0;i < session->internals.priorities->protocol.num_priorities;i++) {
cur_prot =
session->internals.priorities->protocol.priorities[i];
v = version_to_entry(cur_prot);
if (v != NULL && version_is_valid_for_session(session, v)) {
if (min_v == NULL) {
if (v->obsolete != 0)
backup = v;
else
min_v = v;
} else if (v->obsolete == 0 && v->age < min_v->age) {
min_v = v;
}
}
}
if (min_v == NULL)
return backup;
return min_v;
}
/* Returns the maximum version in the priorities
*/
const version_entry_st *_gnutls_version_max(gnutls_session_t session)
{
unsigned int i;
gnutls_protocol_t cur_prot;
const version_entry_st *p, *max = NULL;
if (!session->internals.priorities) {
gnutls_assert();
return NULL;
}
for (i = 0; i < session->internals.priorities->protocol.num_priorities;
i++) {
cur_prot =
session->internals.priorities->protocol.priorities[i];
for (p = sup_versions; p->name != NULL; p++) {
if(p->id == cur_prot) {
#ifndef ENABLE_SSL3
if (p->obsolete != 0)
break;
#endif
if (!p->supported && !(p->supported_revertible && _gnutls_allowlisting_mode()))
break;
if (p->transport != session->internals.transport)
break;
if (p->tls13_sem && (session->internals.flags & INT_FLAG_NO_TLS13))
break;
if (max == NULL || cur_prot > max->id) {
max = p;
}
break;
}
}
}
return max;
}
const version_entry_st *_gnutls_legacy_version_max(gnutls_session_t session)
{
const version_entry_st *max = _gnutls_version_max(session);
if (max && max->only_extension != 0) {
/* TLS 1.3 or later found */
if (max->transport == GNUTLS_STREAM) {
return version_to_entry(GNUTLS_TLS1_2);
} else {
return version_to_entry(GNUTLS_DTLS1_2);
}
}
return max;
}
/* Returns the number of bytes written to buffer or a negative
* error code. It will return GNUTLS_E_UNSUPPORTED_VERSION_PACKET
* if there is no version >= TLS 1.3.
*/
int _gnutls_write_supported_versions(gnutls_session_t session, uint8_t *buffer, ssize_t buffer_size)
{
gnutls_protocol_t cur_prot;
size_t written_bytes = 0;
unsigned at_least_one_new = 0;
unsigned i;
const version_entry_st *p;
for (i = 0; i < session->internals.priorities->protocol.num_priorities; i++) {
cur_prot =
session->internals.priorities->protocol.priorities[i];
for (p = sup_versions; p->name != NULL; p++) {
if(p->id == cur_prot) {
if (p->obsolete != 0)
break;
if (!p->supported && !(p->supported_revertible && _gnutls_allowlisting_mode()))
break;
if (p->transport != session->internals.transport)
break;
if (p->only_extension)
at_least_one_new = 1;
if (buffer_size > 2) {
_gnutls_debug_log("Advertizing version %d.%d\n", (int)p->major, (int)p->minor);
buffer[0] = p->major;
buffer[1] = p->minor;
written_bytes += 2;
buffer += 2;
}
buffer_size -= 2;
if (buffer_size <= 0)
goto finish;
break;
}
}
}
finish:
if (written_bytes == 0)
return gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
if (at_least_one_new == 0)
return GNUTLS_E_UNSUPPORTED_VERSION_PACKET;
return written_bytes;
}
/* Returns true (1) if the given version is higher than the highest supported
* and (0) otherwise */
unsigned _gnutls_version_is_too_high(gnutls_session_t session, uint8_t major, uint8_t minor)
{
const version_entry_st *e;
e = _gnutls_legacy_version_max(session);
if (e == NULL) /* we don't know; but that means something is unconfigured */
return 1;
if (e->transport == GNUTLS_DGRAM) {
if (major < e->major)
return 1;
if (e->major == major && minor < e->minor)
return 1;
} else {
if (major > e->major)
return 1;
if (e->major == major && minor > e->minor)
return 1;
}
return 0;
}
/**
* gnutls_protocol_get_name:
* @version: is a (gnutls) version number
*
* Convert a #gnutls_protocol_t value to a string.
*
* Returns: a string that contains the name of the specified TLS
* version (e.g., "TLS1.0"), or %NULL.
**/
const char *gnutls_protocol_get_name(gnutls_protocol_t version)
{
const version_entry_st *p;
/* avoid prefix */
for (p = sup_versions; p->name != NULL; p++)
if (p->id == version)
return p->name;
return NULL;
}
/**
* gnutls_protocol_get_id:
* @name: is a protocol name
*
* The names are compared in a case insensitive way.
*
* Returns: an id of the specified protocol, or
* %GNUTLS_VERSION_UNKNOWN on error.
**/
gnutls_protocol_t gnutls_protocol_get_id(const char *name)
{
const version_entry_st *p;
gnutls_protocol_t ret = GNUTLS_VERSION_UNKNOWN;
for (p = sup_versions; p->name != NULL; p++) {
if (c_strcasecmp(p->name, name) == 0) {
ret = p->id;
break;
}
}
return ret;
}
/**
* gnutls_protocol_list:
*
* Get a list of supported protocols, e.g. SSL 3.0, TLS 1.0 etc.
*
* This function is not thread safe.
*
* Returns: a (0)-terminated list of #gnutls_protocol_t integers
* indicating the available protocols.
*
**/
const gnutls_protocol_t *gnutls_protocol_list(void)
{
const version_entry_st *p;
static gnutls_protocol_t supported_protocols[MAX_ALGOS] = { 0 };
if (supported_protocols[0] == 0) {
int i = 0;
for (p = sup_versions; p->name != NULL; p++) {
if (!p->supported)
continue;
supported_protocols[i++] = p->id;
}
supported_protocols[i++] = 0;
}
return supported_protocols;
}
/* Return all versions, including non-supported ones.
*/
const gnutls_protocol_t *_gnutls_protocol_list(void)
{
const version_entry_st *p;
static gnutls_protocol_t protocols[MAX_ALGOS] = { 0 };
if (protocols[0] == 0) {
int i = 0;
for (p = sup_versions; p->name != NULL; p++) {
protocols[i++] = p->id;
}
protocols[i++] = 0;
}
return protocols;
}
/* Returns a version number given the major and minor numbers.
*/
gnutls_protocol_t _gnutls_version_get(uint8_t major, uint8_t minor)
{
const version_entry_st *p;
int ret = GNUTLS_VERSION_UNKNOWN;
for (p = sup_versions; p->name != NULL; p++)
if ((p->major == major) && (p->minor == minor))
ret = p->id;
return ret;
}
/* Version Functions */
int
_gnutls_nversion_is_supported(gnutls_session_t session,
unsigned char major, unsigned char minor)
{
const version_entry_st *p;
int version = 0;
for (p = sup_versions; p->name != NULL; p++) {
if(p->major == major && p->minor == minor) {
#ifndef ENABLE_SSL3
if (p->obsolete != 0) return 0;
#endif
if (p->tls13_sem && (session->internals.flags & INT_FLAG_NO_TLS13))
return 0;
if (!p->supported && !(p->supported_revertible && _gnutls_allowlisting_mode()))
return 0;
if (p->transport != session->internals.transport)
return 0;
version = p->id;
break;
}
}
if (version == 0)
return 0;
if (_gnutls_version_priority(session, version) < 0)
return 0; /* disabled by the user */
else
return 1;
}
|