Coverage Report

Created: 2022-08-24 06:30

/src/libressl/ssl/ssl_tlsext.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: ssl_tlsext.c,v 1.128 2022/08/04 09:27:36 tb Exp $ */
2
/*
3
 * Copyright (c) 2016, 2017, 2019 Joel Sing <jsing@openbsd.org>
4
 * Copyright (c) 2017 Doug Hogan <doug@openbsd.org>
5
 * Copyright (c) 2018-2019 Bob Beck <beck@openbsd.org>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/socket.h>
21
22
#include <arpa/inet.h>
23
#include <netinet/in.h>
24
25
#include <ctype.h>
26
27
#include <openssl/ocsp.h>
28
#include <openssl/opensslconf.h>
29
30
#include "bytestring.h"
31
#include "ssl_locl.h"
32
#include "ssl_sigalgs.h"
33
#include "ssl_tlsext.h"
34
35
/*
36
 * Supported Application-Layer Protocol Negotiation - RFC 7301
37
 */
38
39
static int
40
tlsext_alpn_client_needs(SSL *s, uint16_t msg_type)
41
0
{
42
  /* ALPN protos have been specified and this is the initial handshake */
43
0
  return s->internal->alpn_client_proto_list != NULL &&
44
0
      s->s3->hs.finished_len == 0;
45
0
}
46
47
static int
48
tlsext_alpn_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
49
0
{
50
0
  CBB protolist;
51
52
0
  if (!CBB_add_u16_length_prefixed(cbb, &protolist))
53
0
    return 0;
54
55
0
  if (!CBB_add_bytes(&protolist, s->internal->alpn_client_proto_list,
56
0
      s->internal->alpn_client_proto_list_len))
57
0
    return 0;
58
59
0
  if (!CBB_flush(cbb))
60
0
    return 0;
61
62
0
  return 1;
63
0
}
64
65
int
66
tlsext_alpn_check_format(CBS *cbs)
67
18
{
68
18
  CBS proto_name_list;
69
70
18
  if (CBS_len(cbs) == 0)
71
1
    return 0;
72
73
17
  CBS_dup(cbs, &proto_name_list);
74
248
  while (CBS_len(&proto_name_list) > 0) {
75
241
    CBS proto_name;
76
77
241
    if (!CBS_get_u8_length_prefixed(&proto_name_list, &proto_name))
78
6
      return 0;
79
235
    if (CBS_len(&proto_name) == 0)
80
4
      return 0;
81
235
  }
82
83
7
  return 1;
84
17
}
85
86
static int
87
tlsext_alpn_server_parse(SSL *s, uint16_t msg_types, CBS *cbs, int *alert)
88
20
{
89
20
  CBS alpn;
90
20
  const unsigned char *selected;
91
20
  unsigned char selected_len;
92
20
  int r;
93
94
20
  if (!CBS_get_u16_length_prefixed(cbs, &alpn))
95
2
    return 0;
96
97
18
  if (!tlsext_alpn_check_format(&alpn))
98
11
    return 0;
99
100
7
  if (s->ctx->internal->alpn_select_cb == NULL)
101
7
    return 1;
102
103
  /*
104
   * XXX - A few things should be considered here:
105
   * 1. Ensure that the same protocol is selected on session resumption.
106
   * 2. Should the callback be called even if no ALPN extension was sent?
107
   * 3. TLSv1.2 and earlier: ensure that SNI has already been processed.
108
   */
109
0
  r = s->ctx->internal->alpn_select_cb(s, &selected, &selected_len,
110
0
      CBS_data(&alpn), CBS_len(&alpn),
111
0
      s->ctx->internal->alpn_select_cb_arg);
112
113
0
  if (r == SSL_TLSEXT_ERR_OK) {
114
0
    CBS cbs;
115
116
0
    CBS_init(&cbs, selected, selected_len);
117
118
0
    if (!CBS_stow(&cbs, &s->s3->alpn_selected,
119
0
        &s->s3->alpn_selected_len)) {
120
0
      *alert = SSL_AD_INTERNAL_ERROR;
121
0
      return 0;
122
0
    }
123
124
0
    return 1;
125
0
  }
126
127
  /* On SSL_TLSEXT_ERR_NOACK behave as if no callback was present. */
128
0
  if (r == SSL_TLSEXT_ERR_NOACK)
129
0
    return 1;
130
131
0
  *alert = SSL_AD_NO_APPLICATION_PROTOCOL;
132
0
  SSLerror(s, SSL_R_NO_APPLICATION_PROTOCOL);
133
134
0
  return 0;
135
0
}
136
137
static int
138
tlsext_alpn_server_needs(SSL *s, uint16_t msg_type)
139
4.24k
{
140
4.24k
  return s->s3->alpn_selected != NULL;
141
4.24k
}
142
143
static int
144
tlsext_alpn_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
145
0
{
146
0
  CBB list, selected;
147
148
0
  if (!CBB_add_u16_length_prefixed(cbb, &list))
149
0
    return 0;
150
151
0
  if (!CBB_add_u8_length_prefixed(&list, &selected))
152
0
    return 0;
153
154
0
  if (!CBB_add_bytes(&selected, s->s3->alpn_selected,
155
0
      s->s3->alpn_selected_len))
156
0
    return 0;
157
158
0
  if (!CBB_flush(cbb))
159
0
    return 0;
160
161
0
  return 1;
162
0
}
163
164
static int
165
tlsext_alpn_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
166
0
{
167
0
  CBS list, proto;
168
169
0
  if (s->internal->alpn_client_proto_list == NULL) {
170
0
    *alert = SSL_AD_UNSUPPORTED_EXTENSION;
171
0
    return 0;
172
0
  }
173
174
0
  if (!CBS_get_u16_length_prefixed(cbs, &list))
175
0
    return 0;
176
177
0
  if (!CBS_get_u8_length_prefixed(&list, &proto))
178
0
    return 0;
179
180
0
  if (CBS_len(&list) != 0)
181
0
    return 0;
182
0
  if (CBS_len(&proto) == 0)
183
0
    return 0;
184
185
0
  if (!CBS_stow(&proto, &s->s3->alpn_selected, &s->s3->alpn_selected_len))
186
0
    return 0;
187
188
0
  return 1;
189
0
}
190
191
/*
192
 * Supported Groups - RFC 7919 section 2
193
 */
194
static int
195
tlsext_supportedgroups_client_needs(SSL *s, uint16_t msg_type)
196
0
{
197
0
  return ssl_has_ecc_ciphers(s) ||
198
0
      (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION);
199
0
}
200
201
static int
202
tlsext_supportedgroups_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
203
0
{
204
0
  const uint16_t *groups;
205
0
  size_t groups_len;
206
0
  CBB grouplist;
207
0
  int i;
208
209
0
  tls1_get_group_list(s, 0, &groups, &groups_len);
210
0
  if (groups_len == 0) {
211
0
    SSLerror(s, ERR_R_INTERNAL_ERROR);
212
0
    return 0;
213
0
  }
214
215
0
  if (!CBB_add_u16_length_prefixed(cbb, &grouplist))
216
0
    return 0;
217
218
0
  for (i = 0; i < groups_len; i++) {
219
0
    if (!ssl_security_supported_group(s, groups[i]))
220
0
      continue;
221
0
    if (!CBB_add_u16(&grouplist, groups[i]))
222
0
      return 0;
223
0
  }
224
225
0
  if (!CBB_flush(cbb))
226
0
    return 0;
227
228
0
  return 1;
229
0
}
230
231
static int
232
tlsext_supportedgroups_server_parse(SSL *s, uint16_t msg_type, CBS *cbs,
233
    int *alert)
234
3.58k
{
235
3.58k
  CBS grouplist;
236
3.58k
  uint16_t *groups;
237
3.58k
  size_t groups_len;
238
3.58k
  int i;
239
240
3.58k
  if (!CBS_get_u16_length_prefixed(cbs, &grouplist))
241
1
    return 0;
242
243
3.58k
  groups_len = CBS_len(&grouplist);
244
3.58k
  if (groups_len == 0 || groups_len % 2 != 0)
245
2
    return 0;
246
3.58k
  groups_len /= 2;
247
248
3.58k
  if (s->internal->hit)
249
0
    return 1;
250
251
3.58k
  if (s->s3->hs.tls13.hrr) {
252
10
    if (s->session->tlsext_supportedgroups == NULL) {
253
1
      *alert = SSL_AD_HANDSHAKE_FAILURE;
254
1
      return 0;
255
1
    }
256
257
    /*
258
     * The ClientHello extension hashing ensures that the client
259
     * did not change its list of supported groups.
260
     */
261
262
9
    return 1;
263
10
  }
264
265
3.57k
  if (s->session->tlsext_supportedgroups != NULL)
266
0
    return 0; /* XXX internal error? */
267
268
3.57k
  if ((groups = reallocarray(NULL, groups_len, sizeof(uint16_t))) == NULL) {
269
0
    *alert = SSL_AD_INTERNAL_ERROR;
270
0
    return 0;
271
0
  }
272
273
9.53k
  for (i = 0; i < groups_len; i++) {
274
5.96k
    if (!CBS_get_u16(&grouplist, &groups[i])) {
275
0
      free(groups);
276
0
      return 0;
277
0
    }
278
5.96k
  }
279
280
3.57k
  if (CBS_len(&grouplist) != 0) {
281
0
    free(groups);
282
0
    return 0;
283
0
  }
284
285
3.57k
  s->session->tlsext_supportedgroups = groups;
286
3.57k
  s->session->tlsext_supportedgroups_length = groups_len;
287
288
3.57k
  return 1;
289
3.57k
}
290
291
/* This extension is never used by the server. */
292
static int
293
tlsext_supportedgroups_server_needs(SSL *s, uint16_t msg_type)
294
4.24k
{
295
4.24k
  return 0;
296
4.24k
}
297
298
static int
299
tlsext_supportedgroups_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
300
0
{
301
0
  return 0;
302
0
}
303
304
static int
305
tlsext_supportedgroups_client_parse(SSL *s, uint16_t msg_type, CBS *cbs,
306
    int *alert)
307
0
{
308
  /*
309
   * Servers should not send this extension per the RFC.
310
   *
311
   * However, certain F5 BIG-IP systems incorrectly send it. This bug is
312
   * from at least 2014 but as of 2017, there are still large sites with
313
   * this unpatched in production. As a result, we need to currently skip
314
   * over the extension and ignore its content:
315
   *
316
   *  https://support.f5.com/csp/article/K37345003
317
   */
318
0
  if (!CBS_skip(cbs, CBS_len(cbs))) {
319
0
    *alert = SSL_AD_INTERNAL_ERROR;
320
0
    return 0;
321
0
  }
322
323
0
  return 1;
324
0
}
325
326
/*
327
 * Supported Point Formats Extension - RFC 4492 section 5.1.2
328
 */
329
static int
330
tlsext_ecpf_build(SSL *s, uint16_t msg_type, CBB *cbb)
331
43
{
332
43
  CBB ecpf;
333
43
  size_t formats_len;
334
43
  const uint8_t *formats;
335
336
43
  tls1_get_formatlist(s, 0, &formats, &formats_len);
337
338
43
  if (formats_len == 0) {
339
0
    SSLerror(s, ERR_R_INTERNAL_ERROR);
340
0
    return 0;
341
0
  }
342
343
43
  if (!CBB_add_u8_length_prefixed(cbb, &ecpf))
344
0
    return 0;
345
43
  if (!CBB_add_bytes(&ecpf, formats, formats_len))
346
0
    return 0;
347
43
  if (!CBB_flush(cbb))
348
0
    return 0;
349
350
43
  return 1;
351
43
}
352
353
static int
354
tlsext_ecpf_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
355
110
{
356
110
  CBS ecpf;
357
358
110
  if (!CBS_get_u8_length_prefixed(cbs, &ecpf))
359
2
    return 0;
360
108
  if (CBS_len(&ecpf) == 0)
361
1
    return 0;
362
363
  /* Must contain uncompressed (0) - RFC 8422, section 5.1.2. */
364
107
  if (!CBS_contains_zero_byte(&ecpf)) {
365
1
    SSLerror(s, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
366
1
    *alert = SSL_AD_ILLEGAL_PARAMETER;
367
1
    return 0;
368
1
  }
369
370
106
  if (!s->internal->hit) {
371
106
    if (!CBS_stow(&ecpf, &(s->session->tlsext_ecpointformatlist),
372
106
        &(s->session->tlsext_ecpointformatlist_length))) {
373
0
      *alert = SSL_AD_INTERNAL_ERROR;
374
0
      return 0;
375
0
    }
376
106
  }
377
378
106
  return 1;
379
106
}
380
381
static int
382
tlsext_ecpf_client_needs(SSL *s, uint16_t msg_type)
383
0
{
384
0
  return ssl_has_ecc_ciphers(s);
385
0
}
386
387
static int
388
tlsext_ecpf_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
389
0
{
390
0
  return tlsext_ecpf_build(s, msg_type, cbb);
391
0
}
392
393
static int
394
tlsext_ecpf_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
395
110
{
396
110
  return tlsext_ecpf_parse(s, msg_type, cbs, alert);
397
110
}
398
399
static int
400
tlsext_ecpf_server_needs(SSL *s, uint16_t msg_type)
401
4.24k
{
402
4.24k
  return ssl_using_ecc_cipher(s);
403
4.24k
}
404
405
static int
406
tlsext_ecpf_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
407
43
{
408
43
  return tlsext_ecpf_build(s, msg_type, cbb);
409
43
}
410
411
static int
412
tlsext_ecpf_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
413
0
{
414
0
  return tlsext_ecpf_parse(s, msg_type, cbs, alert);
415
0
}
416
417
/*
418
 * Renegotiation Indication - RFC 5746.
419
 */
420
static int
421
tlsext_ri_client_needs(SSL *s, uint16_t msg_type)
422
0
{
423
0
  return (s->internal->renegotiate);
424
0
}
425
426
static int
427
tlsext_ri_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
428
0
{
429
0
  CBB reneg;
430
431
0
  if (!CBB_add_u8_length_prefixed(cbb, &reneg))
432
0
    return 0;
433
0
  if (!CBB_add_bytes(&reneg, s->s3->previous_client_finished,
434
0
      s->s3->previous_client_finished_len))
435
0
    return 0;
436
0
  if (!CBB_flush(cbb))
437
0
    return 0;
438
439
0
  return 1;
440
0
}
441
442
static int
443
tlsext_ri_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
444
44
{
445
44
  CBS reneg;
446
447
44
  if (!CBS_get_u8_length_prefixed(cbs, &reneg)) {
448
3
    SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
449
3
    return 0;
450
3
  }
451
452
41
  if (!CBS_mem_equal(&reneg, s->s3->previous_client_finished,
453
41
      s->s3->previous_client_finished_len)) {
454
5
    SSLerror(s, SSL_R_RENEGOTIATION_MISMATCH);
455
5
    *alert = SSL_AD_HANDSHAKE_FAILURE;
456
5
    return 0;
457
5
  }
458
459
36
  s->s3->renegotiate_seen = 1;
460
36
  s->s3->send_connection_binding = 1;
461
462
36
  return 1;
463
41
}
464
465
static int
466
tlsext_ri_server_needs(SSL *s, uint16_t msg_type)
467
4.24k
{
468
4.24k
  return (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION &&
469
4.24k
      s->s3->send_connection_binding);
470
4.24k
}
471
472
static int
473
tlsext_ri_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
474
74
{
475
74
  CBB reneg;
476
477
74
  if (!CBB_add_u8_length_prefixed(cbb, &reneg))
478
0
    return 0;
479
74
  if (!CBB_add_bytes(&reneg, s->s3->previous_client_finished,
480
74
      s->s3->previous_client_finished_len))
481
0
    return 0;
482
74
  if (!CBB_add_bytes(&reneg, s->s3->previous_server_finished,
483
74
      s->s3->previous_server_finished_len))
484
0
    return 0;
485
74
  if (!CBB_flush(cbb))
486
0
    return 0;
487
488
74
  return 1;
489
74
}
490
491
static int
492
tlsext_ri_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
493
0
{
494
0
  CBS reneg, prev_client, prev_server;
495
496
  /*
497
   * Ensure that the previous client and server values are both not
498
   * present, or that they are both present.
499
   */
500
0
  if ((s->s3->previous_client_finished_len == 0 &&
501
0
      s->s3->previous_server_finished_len != 0) ||
502
0
      (s->s3->previous_client_finished_len != 0 &&
503
0
      s->s3->previous_server_finished_len == 0)) {
504
0
    *alert = SSL_AD_INTERNAL_ERROR;
505
0
    return 0;
506
0
  }
507
508
0
  if (!CBS_get_u8_length_prefixed(cbs, &reneg)) {
509
0
    SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
510
0
    return 0;
511
0
  }
512
0
  if (!CBS_get_bytes(&reneg, &prev_client,
513
0
      s->s3->previous_client_finished_len)) {
514
0
    SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
515
0
    return 0;
516
0
  }
517
0
  if (!CBS_get_bytes(&reneg, &prev_server,
518
0
      s->s3->previous_server_finished_len)) {
519
0
    SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
520
0
    return 0;
521
0
  }
522
0
  if (CBS_len(&reneg) != 0) {
523
0
    SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
524
0
    return 0;
525
0
  }
526
527
0
  if (!CBS_mem_equal(&prev_client, s->s3->previous_client_finished,
528
0
      s->s3->previous_client_finished_len)) {
529
0
    SSLerror(s, SSL_R_RENEGOTIATION_MISMATCH);
530
0
    *alert = SSL_AD_HANDSHAKE_FAILURE;
531
0
    return 0;
532
0
  }
533
0
  if (!CBS_mem_equal(&prev_server, s->s3->previous_server_finished,
534
0
      s->s3->previous_server_finished_len)) {
535
0
    SSLerror(s, SSL_R_RENEGOTIATION_MISMATCH);
536
0
    *alert = SSL_AD_HANDSHAKE_FAILURE;
537
0
    return 0;
538
0
  }
539
540
0
  s->s3->renegotiate_seen = 1;
541
0
  s->s3->send_connection_binding = 1;
542
543
0
  return 1;
544
0
}
545
546
/*
547
 * Signature Algorithms - RFC 5246 section 7.4.1.4.1.
548
 */
549
static int
550
tlsext_sigalgs_client_needs(SSL *s, uint16_t msg_type)
551
0
{
552
0
  return (s->s3->hs.our_max_tls_version >= TLS1_2_VERSION);
553
0
}
554
555
static int
556
tlsext_sigalgs_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
557
0
{
558
0
  uint16_t tls_version = s->s3->hs.negotiated_tls_version;
559
0
  CBB sigalgs;
560
561
0
  if (msg_type == SSL_TLSEXT_MSG_CH)
562
0
    tls_version = s->s3->hs.our_min_tls_version;
563
564
0
  if (!CBB_add_u16_length_prefixed(cbb, &sigalgs))
565
0
    return 0;
566
0
  if (!ssl_sigalgs_build(tls_version, &sigalgs, SSL_get_security_level(s)))
567
0
    return 0;
568
0
  if (!CBB_flush(cbb))
569
0
    return 0;
570
571
0
  return 1;
572
0
}
573
574
static int
575
tlsext_sigalgs_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
576
349
{
577
349
  CBS sigalgs;
578
579
349
  if (!CBS_get_u16_length_prefixed(cbs, &sigalgs))
580
2
    return 0;
581
347
  if (CBS_len(&sigalgs) % 2 != 0 || CBS_len(&sigalgs) > 64)
582
2
    return 0;
583
345
  if (!CBS_stow(&sigalgs, &s->s3->hs.sigalgs, &s->s3->hs.sigalgs_len))
584
0
    return 0;
585
586
345
  return 1;
587
345
}
588
589
static int
590
tlsext_sigalgs_server_needs(SSL *s, uint16_t msg_type)
591
4.15k
{
592
4.15k
  return (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION);
593
4.15k
}
594
595
static int
596
tlsext_sigalgs_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
597
0
{
598
0
  CBB sigalgs;
599
600
0
  if (!CBB_add_u16_length_prefixed(cbb, &sigalgs))
601
0
    return 0;
602
0
  if (!ssl_sigalgs_build(s->s3->hs.negotiated_tls_version, &sigalgs,
603
0
      SSL_get_security_level(s)))
604
0
    return 0;
605
0
  if (!CBB_flush(cbb))
606
0
    return 0;
607
608
0
  return 1;
609
0
}
610
611
static int
612
tlsext_sigalgs_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
613
0
{
614
0
  CBS sigalgs;
615
616
0
  if (ssl_effective_tls_version(s) < TLS1_3_VERSION)
617
0
    return 0;
618
619
0
  if (!CBS_get_u16_length_prefixed(cbs, &sigalgs))
620
0
    return 0;
621
0
  if (CBS_len(&sigalgs) % 2 != 0 || CBS_len(&sigalgs) > 64)
622
0
    return 0;
623
0
  if (!CBS_stow(&sigalgs, &s->s3->hs.sigalgs, &s->s3->hs.sigalgs_len))
624
0
    return 0;
625
626
0
  return 1;
627
0
}
628
629
/*
630
 * Server Name Indication - RFC 6066, section 3.
631
 */
632
static int
633
tlsext_sni_client_needs(SSL *s, uint16_t msg_type)
634
0
{
635
0
  return (s->tlsext_hostname != NULL);
636
0
}
637
638
static int
639
tlsext_sni_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
640
0
{
641
0
  CBB server_name_list, host_name;
642
643
0
  if (!CBB_add_u16_length_prefixed(cbb, &server_name_list))
644
0
    return 0;
645
0
  if (!CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name))
646
0
    return 0;
647
0
  if (!CBB_add_u16_length_prefixed(&server_name_list, &host_name))
648
0
    return 0;
649
0
  if (!CBB_add_bytes(&host_name, (const uint8_t *)s->tlsext_hostname,
650
0
      strlen(s->tlsext_hostname)))
651
0
    return 0;
652
0
  if (!CBB_flush(cbb))
653
0
    return 0;
654
655
0
  return 1;
656
0
}
657
658
static int
659
tlsext_sni_is_ip_literal(CBS *cbs, int *is_ip)
660
296
{
661
296
  union {
662
296
    struct in_addr ip4;
663
296
    struct in6_addr ip6;
664
296
  } addrbuf;
665
296
  char *hostname = NULL;
666
667
296
  *is_ip = 0;
668
669
296
  if (!CBS_strdup(cbs, &hostname))
670
4
    return 0;
671
672
292
  if (inet_pton(AF_INET, hostname, &addrbuf) == 1 ||
673
292
      inet_pton(AF_INET6, hostname, &addrbuf) == 1)
674
1
    *is_ip = 1;
675
676
292
  free(hostname);
677
678
292
  return 1;
679
296
}
680
681
/*
682
 * Validate that the CBS contains only a hostname consisting of RFC 5890
683
 * compliant A-labels (see RFC 6066 section 3). Not a complete check
684
 * since we don't parse punycode to verify its validity but limits to
685
 * correct structure and character set.
686
 */
687
int
688
tlsext_sni_is_valid_hostname(CBS *cbs, int *is_ip)
689
297
{
690
297
  uint8_t prev, c = 0;
691
297
  int component = 0;
692
297
  CBS hostname;
693
694
297
  *is_ip = 0;
695
696
297
  CBS_dup(cbs, &hostname);
697
698
297
  if (CBS_len(&hostname) > TLSEXT_MAXLEN_host_name)
699
1
    return 0;
700
701
  /* An IP literal is invalid as a host name (RFC 6066 section 3). */
702
296
  if (!tlsext_sni_is_ip_literal(&hostname, is_ip))
703
4
    return 0;
704
292
  if (*is_ip)
705
1
    return 0;
706
707
1.84k
  while (CBS_len(&hostname) > 0) {
708
1.57k
    prev = c;
709
1.57k
    if (!CBS_get_u8(&hostname, &c))
710
0
      return 0;
711
    /* Everything has to be ASCII, with no NUL byte. */
712
1.57k
    if (!isascii(c) || c == '\0')
713
4
      return 0;
714
    /* It must be alphanumeric, a '-', or a '.' */
715
1.57k
    if (!isalnum(c) && c != '-' && c != '.')
716
12
      return 0;
717
    /* '-' and '.' must not start a component or be at the end. */
718
1.56k
    if (component == 0 || CBS_len(&hostname) == 0) {
719
639
      if (c == '-' || c == '.')
720
3
        return 0;
721
639
    }
722
1.56k
    if (c == '.') {
723
      /* Components can not end with a dash. */
724
88
      if (prev == '-')
725
1
        return 0;
726
      /* Start new component */
727
87
      component = 0;
728
87
      continue;
729
88
    }
730
    /* Components must be 63 chars or less. */
731
1.47k
    if (++component > 63)
732
1
      return 0;
733
1.47k
  }
734
735
270
  return 1;
736
291
}
737
738
static int
739
tlsext_sni_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
740
314
{
741
314
  CBS server_name_list, host_name;
742
314
  uint8_t name_type;
743
314
  int is_ip;
744
745
314
  if (!CBS_get_u16_length_prefixed(cbs, &server_name_list))
746
8
    goto err;
747
748
306
  if (!CBS_get_u8(&server_name_list, &name_type))
749
1
    goto err;
750
751
  /*
752
   * RFC 6066 section 3, only one type (host_name) is specified.
753
   * We do not tolerate unknown types, neither does BoringSSL.
754
   * other implementations appear more tolerant.
755
   */
756
305
  if (name_type != TLSEXT_NAMETYPE_host_name) {
757
6
    *alert = SSL_AD_ILLEGAL_PARAMETER;
758
6
    goto err;
759
6
  }
760
761
  /*
762
   * RFC 6066 section 3 specifies a host name must be at least 1 byte
763
   * so 0 length is a decode error.
764
   */
765
299
  if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name))
766
1
    goto err;
767
298
  if (CBS_len(&host_name) < 1)
768
1
    goto err;
769
770
297
  if (!tlsext_sni_is_valid_hostname(&host_name, &is_ip)) {
771
    /*
772
     * Various pieces of software have been known to set the SNI
773
     * host name to an IP address, even though that violates the
774
     * RFC. If this is the case, pretend the SNI extension does
775
     * not exist.
776
     */
777
27
    if (is_ip)
778
1
      goto done;
779
780
26
    *alert = SSL_AD_ILLEGAL_PARAMETER;
781
26
    goto err;
782
27
  }
783
784
270
  if (s->internal->hit || s->s3->hs.tls13.hrr) {
785
2
    if (s->session->tlsext_hostname == NULL) {
786
2
      *alert = SSL_AD_UNRECOGNIZED_NAME;
787
2
      goto err;
788
2
    }
789
0
    if (!CBS_mem_equal(&host_name, s->session->tlsext_hostname,
790
0
        strlen(s->session->tlsext_hostname))) {
791
0
      *alert = SSL_AD_UNRECOGNIZED_NAME;
792
0
      goto err;
793
0
    }
794
268
  } else {
795
268
    if (s->session->tlsext_hostname != NULL)
796
0
      goto err;
797
268
    if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
798
0
      *alert = SSL_AD_INTERNAL_ERROR;
799
0
      goto err;
800
0
    }
801
268
  }
802
803
269
 done:
804
  /*
805
   * RFC 6066 section 3 forbids multiple host names with the same type,
806
   * therefore we allow only one entry.
807
   */
808
269
  if (CBS_len(&server_name_list) != 0) {
809
2
    *alert = SSL_AD_ILLEGAL_PARAMETER;
810
2
    goto err;
811
2
  }
812
813
267
  return 1;
814
815
47
 err:
816
47
  return 0;
817
269
}
818
819
static int
820
tlsext_sni_server_needs(SSL *s, uint16_t msg_type)
821
4.24k
{
822
4.24k
  if (s->internal->hit)
823
0
    return 0;
824
825
4.24k
  return (s->session->tlsext_hostname != NULL);
826
4.24k
}
827
828
static int
829
tlsext_sni_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
830
50
{
831
50
  return 1;
832
50
}
833
834
static int
835
tlsext_sni_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
836
0
{
837
0
  if (s->tlsext_hostname == NULL || CBS_len(cbs) != 0) {
838
0
    *alert = SSL_AD_UNRECOGNIZED_NAME;
839
0
    return 0;
840
0
  }
841
842
0
  if (s->internal->hit) {
843
0
    if (s->session->tlsext_hostname == NULL) {
844
0
      *alert = SSL_AD_UNRECOGNIZED_NAME;
845
0
      return 0;
846
0
    }
847
0
    if (strcmp(s->tlsext_hostname,
848
0
        s->session->tlsext_hostname) != 0) {
849
0
      *alert = SSL_AD_UNRECOGNIZED_NAME;
850
0
      return 0;
851
0
    }
852
0
  } else {
853
0
    if (s->session->tlsext_hostname != NULL)
854
0
      return 0;
855
0
    if ((s->session->tlsext_hostname =
856
0
        strdup(s->tlsext_hostname)) == NULL) {
857
0
      *alert = SSL_AD_INTERNAL_ERROR;
858
0
      return 0;
859
0
    }
860
0
  }
861
862
0
  return 1;
863
0
}
864
865
/*
866
 * Certificate Status Request - RFC 6066 section 8.
867
 */
868
869
static int
870
tlsext_ocsp_client_needs(SSL *s, uint16_t msg_type)
871
0
{
872
0
  if (msg_type != SSL_TLSEXT_MSG_CH)
873
0
    return 0;
874
875
0
  return (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp);
876
0
}
877
878
static int
879
tlsext_ocsp_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
880
0
{
881
0
  CBB respid_list, respid, exts;
882
0
  unsigned char *ext_data;
883
0
  size_t ext_len;
884
0
  int i;
885
886
0
  if (!CBB_add_u8(cbb, TLSEXT_STATUSTYPE_ocsp))
887
0
    return 0;
888
0
  if (!CBB_add_u16_length_prefixed(cbb, &respid_list))
889
0
    return 0;
890
0
  for (i = 0; i < sk_OCSP_RESPID_num(s->internal->tlsext_ocsp_ids); i++) {
891
0
    unsigned char *respid_data;
892
0
    OCSP_RESPID *id;
893
0
    size_t id_len;
894
895
0
    if ((id = sk_OCSP_RESPID_value(s->internal->tlsext_ocsp_ids,
896
0
        i)) ==  NULL)
897
0
      return 0;
898
0
    if ((id_len = i2d_OCSP_RESPID(id, NULL)) == -1)
899
0
      return 0;
900
0
    if (!CBB_add_u16_length_prefixed(&respid_list, &respid))
901
0
      return 0;
902
0
    if (!CBB_add_space(&respid, &respid_data, id_len))
903
0
      return 0;
904
0
    if ((i2d_OCSP_RESPID(id, &respid_data)) != id_len)
905
0
      return 0;
906
0
  }
907
0
  if (!CBB_add_u16_length_prefixed(cbb, &exts))
908
0
    return 0;
909
0
  if ((ext_len = i2d_X509_EXTENSIONS(s->internal->tlsext_ocsp_exts,
910
0
      NULL)) == -1)
911
0
    return 0;
912
0
  if (!CBB_add_space(&exts, &ext_data, ext_len))
913
0
    return 0;
914
0
  if ((i2d_X509_EXTENSIONS(s->internal->tlsext_ocsp_exts, &ext_data) !=
915
0
      ext_len))
916
0
    return 0;
917
0
  if (!CBB_flush(cbb))
918
0
    return 0;
919
0
  return 1;
920
0
}
921
922
static int
923
tlsext_ocsp_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
924
634
{
925
634
  int alert_desc = SSL_AD_DECODE_ERROR;
926
634
  CBS respid_list, respid, exts;
927
634
  const unsigned char *p;
928
634
  uint8_t status_type;
929
634
  int ret = 0;
930
931
634
  if (msg_type != SSL_TLSEXT_MSG_CH)
932
0
    goto err;
933
934
634
  if (!CBS_get_u8(cbs, &status_type))
935
2
    goto err;
936
632
  if (status_type != TLSEXT_STATUSTYPE_ocsp) {
937
    /* ignore unknown status types */
938
23
    s->tlsext_status_type = -1;
939
940
23
    if (!CBS_skip(cbs, CBS_len(cbs))) {
941
0
      *alert = SSL_AD_INTERNAL_ERROR;
942
0
      return 0;
943
0
    }
944
23
    return 1;
945
23
  }
946
609
  s->tlsext_status_type = status_type;
947
609
  if (!CBS_get_u16_length_prefixed(cbs, &respid_list))
948
1
    goto err;
949
950
  /* XXX */
951
608
  sk_OCSP_RESPID_pop_free(s->internal->tlsext_ocsp_ids, OCSP_RESPID_free);
952
608
  s->internal->tlsext_ocsp_ids = NULL;
953
608
  if (CBS_len(&respid_list) > 0) {
954
561
    s->internal->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
955
561
    if (s->internal->tlsext_ocsp_ids == NULL) {
956
0
      alert_desc = SSL_AD_INTERNAL_ERROR;
957
0
      goto err;
958
0
    }
959
561
  }
960
961
646
  while (CBS_len(&respid_list) > 0) {
962
584
    OCSP_RESPID *id;
963
964
584
    if (!CBS_get_u16_length_prefixed(&respid_list, &respid))
965
9
      goto err;
966
575
    p = CBS_data(&respid);
967
575
    if ((id = d2i_OCSP_RESPID(NULL, &p, CBS_len(&respid))) == NULL)
968
537
      goto err;
969
38
    if (!sk_OCSP_RESPID_push(s->internal->tlsext_ocsp_ids, id)) {
970
0
      alert_desc = SSL_AD_INTERNAL_ERROR;
971
0
      OCSP_RESPID_free(id);
972
0
      goto err;
973
0
    }
974
38
  }
975
976
  /* Read in request_extensions */
977
62
  if (!CBS_get_u16_length_prefixed(cbs, &exts))
978
16
    goto err;
979
46
  if (CBS_len(&exts) > 0) {
980
18
    sk_X509_EXTENSION_pop_free(s->internal->tlsext_ocsp_exts,
981
18
        X509_EXTENSION_free);
982
18
    p = CBS_data(&exts);
983
18
    if ((s->internal->tlsext_ocsp_exts = d2i_X509_EXTENSIONS(NULL,
984
18
        &p, CBS_len(&exts))) == NULL)
985
16
      goto err;
986
18
  }
987
988
30
  ret = 1;
989
611
 err:
990
611
  if (ret == 0)
991
581
    *alert = alert_desc;
992
611
  return ret;
993
30
}
994
995
static int
996
tlsext_ocsp_server_needs(SSL *s, uint16_t msg_type)
997
4.22k
{
998
4.22k
  if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION &&
999
4.22k
      s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
1000
4.22k
      s->ctx->internal->tlsext_status_cb != NULL) {
1001
0
    s->internal->tlsext_status_expected = 0;
1002
0
    if (s->ctx->internal->tlsext_status_cb(s,
1003
0
        s->ctx->internal->tlsext_status_arg) == SSL_TLSEXT_ERR_OK &&
1004
0
        s->internal->tlsext_ocsp_resp_len > 0)
1005
0
      s->internal->tlsext_status_expected = 1;
1006
0
  }
1007
4.22k
  return s->internal->tlsext_status_expected;
1008
4.22k
}
1009
1010
static int
1011
tlsext_ocsp_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1012
0
{
1013
0
  CBB ocsp_response;
1014
1015
0
  if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION) {
1016
0
    if (!CBB_add_u8(cbb, TLSEXT_STATUSTYPE_ocsp))
1017
0
      return 0;
1018
0
    if (!CBB_add_u24_length_prefixed(cbb, &ocsp_response))
1019
0
      return 0;
1020
0
    if (!CBB_add_bytes(&ocsp_response,
1021
0
        s->internal->tlsext_ocsp_resp,
1022
0
        s->internal->tlsext_ocsp_resp_len))
1023
0
      return 0;
1024
0
    if (!CBB_flush(cbb))
1025
0
      return 0;
1026
0
  }
1027
0
  return 1;
1028
0
}
1029
1030
static int
1031
tlsext_ocsp_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1032
0
{
1033
0
  uint8_t status_type;
1034
0
  CBS response;
1035
1036
0
  if (ssl_effective_tls_version(s) >= TLS1_3_VERSION) {
1037
0
    if (msg_type == SSL_TLSEXT_MSG_CR) {
1038
      /*
1039
       * RFC 8446, 4.4.2.1 - the server may request an OCSP
1040
       * response with an empty status_request.
1041
       */
1042
0
      if (CBS_len(cbs) == 0)
1043
0
        return 1;
1044
1045
0
      SSLerror(s, SSL_R_LENGTH_MISMATCH);
1046
0
      return 0;
1047
0
    }
1048
0
    if (!CBS_get_u8(cbs, &status_type)) {
1049
0
      SSLerror(s, SSL_R_LENGTH_MISMATCH);
1050
0
      return 0;
1051
0
    }
1052
0
    if (status_type != TLSEXT_STATUSTYPE_ocsp) {
1053
0
      SSLerror(s, SSL_R_UNSUPPORTED_STATUS_TYPE);
1054
0
      return 0;
1055
0
    }
1056
0
    if (!CBS_get_u24_length_prefixed(cbs, &response)) {
1057
0
      SSLerror(s, SSL_R_LENGTH_MISMATCH);
1058
0
      return 0;
1059
0
    }
1060
0
    if (CBS_len(&response) > 65536) {
1061
0
      SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG);
1062
0
      return 0;
1063
0
    }
1064
0
    if (!CBS_stow(&response, &s->internal->tlsext_ocsp_resp,
1065
0
        &s->internal->tlsext_ocsp_resp_len)) {
1066
0
      *alert = SSL_AD_INTERNAL_ERROR;
1067
0
      return 0;
1068
0
    }
1069
0
  } else {
1070
0
    if (s->tlsext_status_type == -1) {
1071
0
      *alert = SSL_AD_UNSUPPORTED_EXTENSION;
1072
0
      return 0;
1073
0
    }
1074
    /* Set flag to expect CertificateStatus message */
1075
0
    s->internal->tlsext_status_expected = 1;
1076
0
  }
1077
0
  return 1;
1078
0
}
1079
1080
/*
1081
 * SessionTicket extension - RFC 5077 section 3.2
1082
 */
1083
static int
1084
tlsext_sessionticket_client_needs(SSL *s, uint16_t msg_type)
1085
0
{
1086
  /*
1087
   * Send session ticket extension when enabled and not overridden.
1088
   *
1089
   * When renegotiating, send an empty session ticket to indicate support.
1090
   */
1091
0
  if ((SSL_get_options(s) & SSL_OP_NO_TICKET) != 0)
1092
0
    return 0;
1093
1094
0
  if (!ssl_security_tickets(s))
1095
0
    return 0;
1096
1097
0
  if (s->internal->new_session)
1098
0
    return 1;
1099
1100
0
  if (s->internal->tlsext_session_ticket != NULL &&
1101
0
      s->internal->tlsext_session_ticket->data == NULL)
1102
0
    return 0;
1103
1104
0
  return 1;
1105
0
}
1106
1107
static int
1108
tlsext_sessionticket_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1109
0
{
1110
  /*
1111
   * Signal that we support session tickets by sending an empty
1112
   * extension when renegotiating or no session found.
1113
   */
1114
0
  if (s->internal->new_session || s->session == NULL)
1115
0
    return 1;
1116
1117
0
  if (s->session->tlsext_tick != NULL) {
1118
    /* Attempt to resume with an existing session ticket */
1119
0
    if (!CBB_add_bytes(cbb, s->session->tlsext_tick,
1120
0
        s->session->tlsext_ticklen))
1121
0
      return 0;
1122
1123
0
  } else if (s->internal->tlsext_session_ticket != NULL) {
1124
    /*
1125
     * Attempt to resume with a custom provided session ticket set
1126
     * by SSL_set_session_ticket_ext().
1127
     */
1128
0
    if (s->internal->tlsext_session_ticket->length > 0) {
1129
0
      size_t ticklen = s->internal->tlsext_session_ticket->length;
1130
1131
0
      if ((s->session->tlsext_tick = malloc(ticklen)) == NULL)
1132
0
        return 0;
1133
0
      memcpy(s->session->tlsext_tick,
1134
0
          s->internal->tlsext_session_ticket->data,
1135
0
          ticklen);
1136
0
      s->session->tlsext_ticklen = ticklen;
1137
1138
0
      if (!CBB_add_bytes(cbb, s->session->tlsext_tick,
1139
0
          s->session->tlsext_ticklen))
1140
0
        return 0;
1141
0
    }
1142
0
  }
1143
1144
0
  if (!CBB_flush(cbb))
1145
0
    return 0;
1146
1147
0
  return 1;
1148
0
}
1149
1150
static int
1151
tlsext_sessionticket_server_parse(SSL *s, uint16_t msg_type, CBS *cbs,
1152
    int *alert)
1153
177
{
1154
177
  if (s->internal->tls_session_ticket_ext_cb) {
1155
0
    if (!s->internal->tls_session_ticket_ext_cb(s, CBS_data(cbs),
1156
0
        (int)CBS_len(cbs),
1157
0
        s->internal->tls_session_ticket_ext_cb_arg)) {
1158
0
      *alert = SSL_AD_INTERNAL_ERROR;
1159
0
      return 0;
1160
0
    }
1161
0
  }
1162
1163
  /* We need to signal that this was processed fully */
1164
177
  if (!CBS_skip(cbs, CBS_len(cbs))) {
1165
0
    *alert = SSL_AD_INTERNAL_ERROR;
1166
0
    return 0;
1167
0
  }
1168
1169
177
  return 1;
1170
177
}
1171
1172
static int
1173
tlsext_sessionticket_server_needs(SSL *s, uint16_t msg_type)
1174
4.24k
{
1175
4.24k
  return (s->internal->tlsext_ticket_expected &&
1176
4.24k
      !(SSL_get_options(s) & SSL_OP_NO_TICKET) &&
1177
4.24k
      ssl_security_tickets(s));
1178
4.24k
}
1179
1180
static int
1181
tlsext_sessionticket_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1182
65
{
1183
  /* Empty ticket */
1184
65
  return 1;
1185
65
}
1186
1187
static int
1188
tlsext_sessionticket_client_parse(SSL *s, uint16_t msg_type, CBS *cbs,
1189
    int *alert)
1190
0
{
1191
0
  if (s->internal->tls_session_ticket_ext_cb) {
1192
0
    if (!s->internal->tls_session_ticket_ext_cb(s, CBS_data(cbs),
1193
0
        (int)CBS_len(cbs),
1194
0
        s->internal->tls_session_ticket_ext_cb_arg)) {
1195
0
      *alert = SSL_AD_INTERNAL_ERROR;
1196
0
      return 0;
1197
0
    }
1198
0
  }
1199
1200
0
  if ((SSL_get_options(s) & SSL_OP_NO_TICKET) != 0 || CBS_len(cbs) > 0) {
1201
0
    *alert = SSL_AD_UNSUPPORTED_EXTENSION;
1202
0
    return 0;
1203
0
  }
1204
1205
0
  s->internal->tlsext_ticket_expected = 1;
1206
1207
0
  return 1;
1208
0
}
1209
1210
/*
1211
 * DTLS extension for SRTP key establishment - RFC 5764
1212
 */
1213
1214
#ifndef OPENSSL_NO_SRTP
1215
1216
static int
1217
tlsext_srtp_client_needs(SSL *s, uint16_t msg_type)
1218
0
{
1219
0
  return SSL_is_dtls(s) && SSL_get_srtp_profiles(s) != NULL;
1220
0
}
1221
1222
static int
1223
tlsext_srtp_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1224
0
{
1225
0
  CBB profiles, mki;
1226
0
  int ct, i;
1227
0
  STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = NULL;
1228
0
  const SRTP_PROTECTION_PROFILE *prof;
1229
1230
0
  if ((clnt = SSL_get_srtp_profiles(s)) == NULL) {
1231
0
    SSLerror(s, SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
1232
0
    return 0;
1233
0
  }
1234
1235
0
  if ((ct = sk_SRTP_PROTECTION_PROFILE_num(clnt)) < 1) {
1236
0
    SSLerror(s, SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
1237
0
    return 0;
1238
0
  }
1239
1240
0
  if (!CBB_add_u16_length_prefixed(cbb, &profiles))
1241
0
    return 0;
1242
1243
0
  for (i = 0; i < ct; i++) {
1244
0
    if ((prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i)) == NULL)
1245
0
      return 0;
1246
0
    if (!CBB_add_u16(&profiles, prof->id))
1247
0
      return 0;
1248
0
  }
1249
1250
0
  if (!CBB_add_u8_length_prefixed(cbb, &mki))
1251
0
    return 0;
1252
1253
0
  if (!CBB_flush(cbb))
1254
0
    return 0;
1255
1256
0
  return 1;
1257
0
}
1258
1259
static int
1260
tlsext_srtp_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1261
63
{
1262
63
  const SRTP_PROTECTION_PROFILE *cprof, *sprof;
1263
63
  STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = NULL, *srvr;
1264
63
  int i, j;
1265
63
  int ret;
1266
63
  uint16_t id;
1267
63
  CBS profiles, mki;
1268
1269
63
  ret = 0;
1270
1271
63
  if (!CBS_get_u16_length_prefixed(cbs, &profiles))
1272
2
    goto err;
1273
61
  if (CBS_len(&profiles) == 0 || CBS_len(&profiles) % 2 != 0)
1274
3
    goto err;
1275
1276
58
  if ((clnt = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL)
1277
0
    goto err;
1278
1279
1.55k
  while (CBS_len(&profiles) > 0) {
1280
1.49k
    if (!CBS_get_u16(&profiles, &id))
1281
0
      goto err;
1282
1283
1.49k
    if (!srtp_find_profile_by_num(id, &cprof)) {
1284
399
      if (!sk_SRTP_PROTECTION_PROFILE_push(clnt, cprof))
1285
0
        goto err;
1286
399
    }
1287
1.49k
  }
1288
1289
58
  if (!CBS_get_u8_length_prefixed(cbs, &mki) || CBS_len(&mki) != 0) {
1290
52
    SSLerror(s, SSL_R_BAD_SRTP_MKI_VALUE);
1291
52
    goto done;
1292
52
  }
1293
1294
  /*
1295
   * Per RFC 5764 section 4.1.1
1296
   *
1297
   * Find the server preferred profile using the client's list.
1298
   *
1299
   * The server MUST send a profile if it sends the use_srtp
1300
   * extension.  If one is not found, it should fall back to the
1301
   * negotiated DTLS cipher suite or return a DTLS alert.
1302
   */
1303
6
  if ((srvr = SSL_get_srtp_profiles(s)) == NULL)
1304
6
    goto err;
1305
0
  for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(srvr); i++) {
1306
0
    if ((sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i)) == NULL)
1307
0
      goto err;
1308
1309
0
    for (j = 0; j < sk_SRTP_PROTECTION_PROFILE_num(clnt); j++) {
1310
0
      if ((cprof = sk_SRTP_PROTECTION_PROFILE_value(clnt, j))
1311
0
          == NULL)
1312
0
        goto err;
1313
1314
0
      if (cprof->id == sprof->id) {
1315
0
        s->internal->srtp_profile = sprof;
1316
0
        ret = 1;
1317
0
        goto done;
1318
0
      }
1319
0
    }
1320
0
  }
1321
1322
  /* If we didn't find anything, fall back to the negotiated */
1323
0
  ret = 1;
1324
0
  goto done;
1325
1326
11
 err:
1327
11
  SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1328
1329
63
 done:
1330
63
  sk_SRTP_PROTECTION_PROFILE_free(clnt);
1331
63
  return ret;
1332
11
}
1333
1334
static int
1335
tlsext_srtp_server_needs(SSL *s, uint16_t msg_type)
1336
4.33k
{
1337
4.33k
  return SSL_is_dtls(s) && SSL_get_selected_srtp_profile(s) != NULL;
1338
4.33k
}
1339
1340
static int
1341
tlsext_srtp_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1342
0
{
1343
0
  SRTP_PROTECTION_PROFILE *profile;
1344
0
  CBB srtp, mki;
1345
1346
0
  if (!CBB_add_u16_length_prefixed(cbb, &srtp))
1347
0
    return 0;
1348
1349
0
  if ((profile = SSL_get_selected_srtp_profile(s)) == NULL)
1350
0
    return 0;
1351
1352
0
  if (!CBB_add_u16(&srtp, profile->id))
1353
0
    return 0;
1354
1355
0
  if (!CBB_add_u8_length_prefixed(cbb, &mki))
1356
0
    return 0;
1357
1358
0
  if (!CBB_flush(cbb))
1359
0
    return 0;
1360
1361
0
  return 1;
1362
0
}
1363
1364
static int
1365
tlsext_srtp_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1366
0
{
1367
0
  STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1368
0
  const SRTP_PROTECTION_PROFILE *prof;
1369
0
  int i;
1370
0
  uint16_t id;
1371
0
  CBS profile_ids, mki;
1372
1373
0
  if (!CBS_get_u16_length_prefixed(cbs, &profile_ids)) {
1374
0
    SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1375
0
    return 0;
1376
0
  }
1377
1378
0
  if (!CBS_get_u16(&profile_ids, &id) || CBS_len(&profile_ids) != 0) {
1379
0
    SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1380
0
    return 0;
1381
0
  }
1382
1383
0
  if (!CBS_get_u8_length_prefixed(cbs, &mki) || CBS_len(&mki) != 0) {
1384
0
    SSLerror(s, SSL_R_BAD_SRTP_MKI_VALUE);
1385
0
    *alert = SSL_AD_ILLEGAL_PARAMETER;
1386
0
    return 0;
1387
0
  }
1388
1389
0
  if ((clnt = SSL_get_srtp_profiles(s)) == NULL) {
1390
0
    SSLerror(s, SSL_R_NO_SRTP_PROFILES);
1391
0
    return 0;
1392
0
  }
1393
1394
0
  for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1395
0
    if ((prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i))
1396
0
        == NULL) {
1397
0
      SSLerror(s, SSL_R_NO_SRTP_PROFILES);
1398
0
      return 0;
1399
0
    }
1400
1401
0
    if (prof->id == id) {
1402
0
      s->internal->srtp_profile = prof;
1403
0
      return 1;
1404
0
    }
1405
0
  }
1406
1407
0
  SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1408
1409
0
  return 0;
1410
0
}
1411
1412
#endif /* OPENSSL_NO_SRTP */
1413
1414
/*
1415
 * TLSv1.3 Key Share - RFC 8446 section 4.2.8.
1416
 */
1417
static int
1418
tlsext_keyshare_client_needs(SSL *s, uint16_t msg_type)
1419
0
{
1420
0
  return (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION);
1421
0
}
1422
1423
static int
1424
tlsext_keyshare_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1425
0
{
1426
0
  CBB client_shares, key_exchange;
1427
1428
0
  if (!CBB_add_u16_length_prefixed(cbb, &client_shares))
1429
0
    return 0;
1430
1431
0
  if (!CBB_add_u16(&client_shares,
1432
0
      tls_key_share_group(s->s3->hs.key_share)))
1433
0
    return 0;
1434
0
  if (!CBB_add_u16_length_prefixed(&client_shares, &key_exchange))
1435
0
    return 0;
1436
0
  if (!tls_key_share_public(s->s3->hs.key_share, &key_exchange))
1437
0
    return 0;
1438
1439
0
  if (!CBB_flush(cbb))
1440
0
    return 0;
1441
1442
0
  return 1;
1443
0
}
1444
1445
static int
1446
tlsext_keyshare_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1447
278
{
1448
278
  CBS client_shares, key_exchange;
1449
278
  int decode_error;
1450
278
  uint16_t group;
1451
1452
278
  if (!CBS_get_u16_length_prefixed(cbs, &client_shares))
1453
2
    return 0;
1454
1455
1.22k
  while (CBS_len(&client_shares) > 0) {
1456
1457
    /* Unpack client share. */
1458
988
    if (!CBS_get_u16(&client_shares, &group))
1459
6
      return 0;
1460
982
    if (!CBS_get_u16_length_prefixed(&client_shares, &key_exchange))
1461
19
      return 0;
1462
1463
    /*
1464
     * XXX - check key exchange against supported groups from client.
1465
     * XXX - check that groups only appear once.
1466
     */
1467
1468
    /*
1469
     * Ignore this client share if we're using earlier than TLSv1.3
1470
     * or we've already selected a key share.
1471
     */
1472
963
    if (s->s3->hs.our_max_tls_version < TLS1_3_VERSION)
1473
0
      continue;
1474
963
    if (s->s3->hs.key_share != NULL)
1475
183
      continue;
1476
1477
    /* XXX - consider implementing server preference. */
1478
780
    if (!tls1_check_group(s, group))
1479
622
      continue;
1480
1481
    /* Decode and store the selected key share. */
1482
158
    if ((s->s3->hs.key_share = tls_key_share_new(group)) == NULL) {
1483
0
      *alert = SSL_AD_INTERNAL_ERROR;
1484
0
      return 0;
1485
0
    }
1486
158
    if (!tls_key_share_peer_public(s->s3->hs.key_share,
1487
158
        &key_exchange, &decode_error, NULL)) {
1488
12
      if (!decode_error)
1489
11
        *alert = SSL_AD_INTERNAL_ERROR;
1490
12
      return 0;
1491
12
    }
1492
158
  }
1493
1494
239
  return 1;
1495
276
}
1496
1497
static int
1498
tlsext_keyshare_server_needs(SSL *s, uint16_t msg_type)
1499
4.31k
{
1500
4.31k
  return (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION &&
1501
4.31k
      tlsext_extension_seen(s, TLSEXT_TYPE_key_share));
1502
4.31k
}
1503
1504
static int
1505
tlsext_keyshare_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1506
158
{
1507
158
  CBB key_exchange;
1508
1509
  /* In the case of a HRR, we only send the server selected group. */
1510
158
  if (s->s3->hs.tls13.hrr) {
1511
70
    if (s->s3->hs.tls13.server_group == 0)
1512
0
      return 0;
1513
70
    return CBB_add_u16(cbb, s->s3->hs.tls13.server_group);
1514
70
  }
1515
1516
88
  if (s->s3->hs.key_share == NULL)
1517
0
    return 0;
1518
1519
88
  if (!CBB_add_u16(cbb, tls_key_share_group(s->s3->hs.key_share)))
1520
0
    return 0;
1521
88
  if (!CBB_add_u16_length_prefixed(cbb, &key_exchange))
1522
0
    return 0;
1523
88
  if (!tls_key_share_public(s->s3->hs.key_share, &key_exchange))
1524
0
    return 0;
1525
1526
88
  if (!CBB_flush(cbb))
1527
0
    return 0;
1528
1529
88
  return 1;
1530
88
}
1531
1532
static int
1533
tlsext_keyshare_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1534
0
{
1535
0
  CBS key_exchange;
1536
0
  int decode_error;
1537
0
  uint16_t group;
1538
1539
  /* Unpack server share. */
1540
0
  if (!CBS_get_u16(cbs, &group))
1541
0
    return 0;
1542
1543
0
  if (CBS_len(cbs) == 0) {
1544
    /* HRR does not include an actual key share, only the group. */
1545
0
    if (msg_type != SSL_TLSEXT_MSG_HRR)
1546
0
      return 0;
1547
1548
0
    s->s3->hs.tls13.server_group = group;
1549
0
    return 1;
1550
0
  }
1551
1552
0
  if (!CBS_get_u16_length_prefixed(cbs, &key_exchange))
1553
0
    return 0;
1554
1555
0
  if (s->s3->hs.key_share == NULL) {
1556
0
    *alert = SSL_AD_INTERNAL_ERROR;
1557
0
    return 0;
1558
0
  }
1559
0
  if (tls_key_share_group(s->s3->hs.key_share) != group) {
1560
0
    *alert = SSL_AD_INTERNAL_ERROR;
1561
0
    return 0;
1562
0
  }
1563
0
  if (!tls_key_share_peer_public(s->s3->hs.key_share,
1564
0
      &key_exchange, &decode_error, NULL)) {
1565
0
    if (!decode_error)
1566
0
      *alert = SSL_AD_INTERNAL_ERROR;
1567
0
    return 0;
1568
0
  }
1569
1570
0
  return 1;
1571
0
}
1572
1573
/*
1574
 * Supported Versions - RFC 8446 section 4.2.1.
1575
 */
1576
static int
1577
tlsext_versions_client_needs(SSL *s, uint16_t msg_type)
1578
0
{
1579
0
  return (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION);
1580
0
}
1581
1582
static int
1583
tlsext_versions_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1584
0
{
1585
0
  uint16_t max, min;
1586
0
  uint16_t version;
1587
0
  CBB versions;
1588
1589
0
  max = s->s3->hs.our_max_tls_version;
1590
0
  min = s->s3->hs.our_min_tls_version;
1591
1592
0
  if (!CBB_add_u8_length_prefixed(cbb, &versions))
1593
0
    return 0;
1594
1595
  /* XXX - fix, but contiguous for now... */
1596
0
  for (version = max; version >= min; version--) {
1597
0
    if (!CBB_add_u16(&versions, version))
1598
0
      return 0;
1599
0
  }
1600
1601
0
  if (!CBB_flush(cbb))
1602
0
    return 0;
1603
1604
0
  return 1;
1605
0
}
1606
1607
static int
1608
tlsext_versions_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1609
284
{
1610
284
  CBS versions;
1611
284
  uint16_t version;
1612
284
  uint16_t max, min;
1613
284
  uint16_t matched_version = 0;
1614
1615
284
  max = s->s3->hs.our_max_tls_version;
1616
284
  min = s->s3->hs.our_min_tls_version;
1617
1618
284
  if (!CBS_get_u8_length_prefixed(cbs, &versions))
1619
3
    return 0;
1620
1621
2.12k
  while (CBS_len(&versions) > 0) {
1622
1.85k
    if (!CBS_get_u16(&versions, &version))
1623
10
      return 0;
1624
    /*
1625
     * XXX What is below implements client preference, and
1626
     * ignores any server preference entirely.
1627
     */
1628
1.84k
    if (matched_version == 0 && version >= min && version <= max)
1629
241
      matched_version = version;
1630
1.84k
  }
1631
1632
271
  if (matched_version > 0)  {
1633
    /* XXX - this should be stored for later processing. */
1634
237
    s->version = matched_version;
1635
237
    return 1;
1636
237
  }
1637
1638
34
  *alert = SSL_AD_PROTOCOL_VERSION;
1639
34
  return 0;
1640
271
}
1641
1642
static int
1643
tlsext_versions_server_needs(SSL *s, uint16_t msg_type)
1644
4.31k
{
1645
4.31k
  return (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION);
1646
4.31k
}
1647
1648
static int
1649
tlsext_versions_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1650
158
{
1651
158
  return CBB_add_u16(cbb, TLS1_3_VERSION);
1652
158
}
1653
1654
static int
1655
tlsext_versions_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1656
0
{
1657
0
  uint16_t selected_version;
1658
1659
0
  if (!CBS_get_u16(cbs, &selected_version))
1660
0
    return 0;
1661
1662
  /* XXX - need to fix for DTLS 1.3 */
1663
0
  if (selected_version < TLS1_3_VERSION) {
1664
0
    *alert = SSL_AD_ILLEGAL_PARAMETER;
1665
0
    return 0;
1666
0
  }
1667
1668
  /* XXX test between min and max once initialization code goes in */
1669
0
  s->s3->hs.tls13.server_version = selected_version;
1670
1671
0
  return 1;
1672
0
}
1673
1674
1675
/*
1676
 * Cookie - RFC 8446 section 4.2.2.
1677
 */
1678
1679
static int
1680
tlsext_cookie_client_needs(SSL *s, uint16_t msg_type)
1681
0
{
1682
0
  return (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION &&
1683
0
      s->s3->hs.tls13.cookie_len > 0 && s->s3->hs.tls13.cookie != NULL);
1684
0
}
1685
1686
static int
1687
tlsext_cookie_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1688
0
{
1689
0
  CBB cookie;
1690
1691
0
  if (!CBB_add_u16_length_prefixed(cbb, &cookie))
1692
0
    return 0;
1693
1694
0
  if (!CBB_add_bytes(&cookie, s->s3->hs.tls13.cookie,
1695
0
      s->s3->hs.tls13.cookie_len))
1696
0
    return 0;
1697
1698
0
  if (!CBB_flush(cbb))
1699
0
    return 0;
1700
1701
0
  return 1;
1702
0
}
1703
1704
static int
1705
tlsext_cookie_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1706
15
{
1707
15
  CBS cookie;
1708
1709
15
  if (!CBS_get_u16_length_prefixed(cbs, &cookie))
1710
2
    return 0;
1711
1712
13
  if (CBS_len(&cookie) != s->s3->hs.tls13.cookie_len)
1713
9
    return 0;
1714
1715
  /*
1716
   * Check provided cookie value against what server previously
1717
   * sent - client *MUST* send the same cookie with new CR after
1718
   * a cookie is sent by the server with an HRR.
1719
   */
1720
4
  if (!CBS_mem_equal(&cookie, s->s3->hs.tls13.cookie,
1721
4
      s->s3->hs.tls13.cookie_len)) {
1722
    /* XXX special cookie mismatch alert? */
1723
0
    *alert = SSL_AD_ILLEGAL_PARAMETER;
1724
0
    return 0;
1725
0
  }
1726
1727
4
  return 1;
1728
4
}
1729
1730
static int
1731
tlsext_cookie_server_needs(SSL *s, uint16_t msg_type)
1732
4.22k
{
1733
  /*
1734
   * Server needs to set cookie value in tls13 handshake
1735
   * in order to send one, should only be sent with HRR.
1736
   */
1737
4.22k
  return (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION &&
1738
4.22k
      s->s3->hs.tls13.cookie_len > 0 && s->s3->hs.tls13.cookie != NULL);
1739
4.22k
}
1740
1741
static int
1742
tlsext_cookie_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1743
0
{
1744
0
  CBB cookie;
1745
1746
  /* XXX deduplicate with client code */
1747
1748
0
  if (!CBB_add_u16_length_prefixed(cbb, &cookie))
1749
0
    return 0;
1750
1751
0
  if (!CBB_add_bytes(&cookie, s->s3->hs.tls13.cookie,
1752
0
      s->s3->hs.tls13.cookie_len))
1753
0
    return 0;
1754
1755
0
  if (!CBB_flush(cbb))
1756
0
    return 0;
1757
1758
0
  return 1;
1759
0
}
1760
1761
static int
1762
tlsext_cookie_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1763
0
{
1764
0
  CBS cookie;
1765
1766
  /*
1767
   * XXX This currently assumes we will not get a second
1768
   * HRR from a server with a cookie to process after accepting
1769
   * one from the server in the same handshake
1770
   */
1771
0
  if (s->s3->hs.tls13.cookie != NULL ||
1772
0
      s->s3->hs.tls13.cookie_len != 0) {
1773
0
    *alert = SSL_AD_ILLEGAL_PARAMETER;
1774
0
    return 0;
1775
0
  }
1776
1777
0
  if (!CBS_get_u16_length_prefixed(cbs, &cookie))
1778
0
    return 0;
1779
1780
0
  if (!CBS_stow(&cookie, &s->s3->hs.tls13.cookie,
1781
0
      &s->s3->hs.tls13.cookie_len))
1782
0
    return 0;
1783
1784
0
  return 1;
1785
0
}
1786
1787
/*
1788
 * Pre-Shared Key Exchange Modes - RFC 8446, 4.2.9.
1789
 */
1790
1791
static int
1792
tlsext_psk_kex_modes_client_needs(SSL *s, uint16_t msg_type)
1793
0
{
1794
0
  return (s->s3->hs.tls13.use_psk_dhe_ke &&
1795
0
      s->s3->hs.our_max_tls_version >= TLS1_3_VERSION);
1796
0
}
1797
1798
static int
1799
tlsext_psk_kex_modes_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1800
0
{
1801
0
  CBB ke_modes;
1802
1803
0
  if (!CBB_add_u8_length_prefixed(cbb, &ke_modes))
1804
0
    return 0;
1805
1806
  /* Only indicate support for PSK with DHE key establishment. */
1807
0
  if (!CBB_add_u8(&ke_modes, TLS13_PSK_DHE_KE))
1808
0
    return 0;
1809
1810
0
  if (!CBB_flush(cbb))
1811
0
    return 0;
1812
1813
0
  return 1;
1814
0
}
1815
1816
static int
1817
tlsext_psk_kex_modes_server_parse(SSL *s, uint16_t msg_type, CBS *cbs,
1818
    int *alert)
1819
80
{
1820
80
  CBS ke_modes;
1821
80
  uint8_t ke_mode;
1822
1823
80
  if (!CBS_get_u8_length_prefixed(cbs, &ke_modes))
1824
1
    return 0;
1825
1826
1.38k
  while (CBS_len(&ke_modes) > 0) {
1827
1.30k
    if (!CBS_get_u8(&ke_modes, &ke_mode))
1828
0
      return 0;
1829
1830
1.30k
    if (ke_mode == TLS13_PSK_DHE_KE)
1831
457
      s->s3->hs.tls13.use_psk_dhe_ke = 1;
1832
1.30k
  }
1833
1834
79
  return 1;
1835
79
}
1836
1837
static int
1838
tlsext_psk_kex_modes_server_needs(SSL *s, uint16_t msg_type)
1839
4.15k
{
1840
  /* Servers MUST NOT send this extension. */
1841
4.15k
  return 0;
1842
4.15k
}
1843
1844
static int
1845
tlsext_psk_kex_modes_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1846
0
{
1847
0
  return 0;
1848
0
}
1849
1850
static int
1851
tlsext_psk_kex_modes_client_parse(SSL *s, uint16_t msg_type, CBS *cbs,
1852
    int *alert)
1853
0
{
1854
0
  return 0;
1855
0
}
1856
1857
/*
1858
 * Pre-Shared Key Extension - RFC 8446, 4.2.11
1859
 */
1860
1861
static int
1862
tlsext_psk_client_needs(SSL *s, uint16_t msg_type)
1863
0
{
1864
0
  return 0;
1865
0
}
1866
1867
static int
1868
tlsext_psk_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
1869
0
{
1870
0
  return 0;
1871
0
}
1872
1873
static int
1874
tlsext_psk_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1875
0
{
1876
0
  return CBS_skip(cbs, CBS_len(cbs));
1877
0
}
1878
1879
static int
1880
tlsext_psk_server_needs(SSL *s, uint16_t msg_type)
1881
4.24k
{
1882
4.24k
  return 0;
1883
4.24k
}
1884
1885
static int
1886
tlsext_psk_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
1887
0
{
1888
0
  return 0;
1889
0
}
1890
1891
static int
1892
tlsext_psk_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
1893
34
{
1894
34
  return CBS_skip(cbs, CBS_len(cbs));
1895
34
}
1896
1897
/*
1898
 * QUIC transport parameters extension - RFC 9001 section 8.2.
1899
 */
1900
1901
static int
1902
tlsext_quic_transport_parameters_client_needs(SSL *s, uint16_t msg_type)
1903
0
{
1904
0
  return SSL_is_quic(s) && s->internal->quic_transport_params_len > 0;
1905
0
}
1906
1907
static int
1908
tlsext_quic_transport_parameters_client_build(SSL *s, uint16_t msg_type,
1909
    CBB *cbb)
1910
0
{
1911
0
  if (!CBB_add_bytes(cbb, s->internal->quic_transport_params,
1912
0
      s->internal->quic_transport_params_len))
1913
0
    return 0;
1914
1915
0
  return 1;
1916
0
}
1917
1918
static int
1919
tlsext_quic_transport_parameters_client_parse(SSL *s, uint16_t msg_type,
1920
    CBS *cbs, int *alert)
1921
0
{
1922
0
  if (!SSL_is_quic(s)) {
1923
0
    *alert = SSL_AD_UNSUPPORTED_EXTENSION;
1924
0
    return 0;
1925
0
  }
1926
1927
0
  if (!CBS_stow(cbs, &s->s3->peer_quic_transport_params,
1928
0
      &s->s3->peer_quic_transport_params_len))
1929
0
    return 0;
1930
0
  if (!CBS_skip(cbs, s->s3->peer_quic_transport_params_len))
1931
0
    return 0;
1932
1933
0
  return 1;
1934
0
}
1935
1936
static int
1937
tlsext_quic_transport_parameters_server_needs(SSL *s, uint16_t msg_type)
1938
4.24k
{
1939
4.24k
  return SSL_is_quic(s) && s->internal->quic_transport_params_len > 0;
1940
4.24k
}
1941
1942
static int
1943
tlsext_quic_transport_parameters_server_build(SSL *s, uint16_t msg_type,
1944
    CBB *cbb)
1945
0
{
1946
0
  if (!CBB_add_bytes(cbb, s->internal->quic_transport_params,
1947
0
      s->internal->quic_transport_params_len))
1948
0
    return 0;
1949
1950
0
  return 1;
1951
0
}
1952
1953
static int
1954
tlsext_quic_transport_parameters_server_parse(SSL *s, uint16_t msg_type,
1955
    CBS *cbs, int *alert)
1956
2
{
1957
2
  if (!SSL_is_quic(s)) {
1958
2
    *alert = SSL_AD_UNSUPPORTED_EXTENSION;
1959
2
    return 0;
1960
2
  }
1961
1962
0
  if (!CBS_stow(cbs, &s->s3->peer_quic_transport_params,
1963
0
      &s->s3->peer_quic_transport_params_len))
1964
0
    return 0;
1965
0
  if (!CBS_skip(cbs, s->s3->peer_quic_transport_params_len))
1966
0
    return 0;
1967
1968
0
  return 1;
1969
0
}
1970
1971
struct tls_extension_funcs {
1972
  int (*needs)(SSL *s, uint16_t msg_type);
1973
  int (*build)(SSL *s, uint16_t msg_type, CBB *cbb);
1974
  int (*parse)(SSL *s, uint16_t msg_type, CBS *cbs, int *alert);
1975
};
1976
1977
struct tls_extension {
1978
  uint16_t type;
1979
  uint16_t messages;
1980
  struct tls_extension_funcs client;
1981
  struct tls_extension_funcs server;
1982
};
1983
1984
static const struct tls_extension tls_extensions[] = {
1985
  {
1986
    .type = TLSEXT_TYPE_supported_versions,
1987
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH |
1988
        SSL_TLSEXT_MSG_HRR,
1989
    .client = {
1990
      .needs = tlsext_versions_client_needs,
1991
      .build = tlsext_versions_client_build,
1992
      .parse = tlsext_versions_client_parse,
1993
    },
1994
    .server = {
1995
      .needs = tlsext_versions_server_needs,
1996
      .build = tlsext_versions_server_build,
1997
      .parse = tlsext_versions_server_parse,
1998
    },
1999
  },
2000
  {
2001
    .type = TLSEXT_TYPE_key_share,
2002
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH |
2003
        SSL_TLSEXT_MSG_HRR,
2004
    .client = {
2005
      .needs = tlsext_keyshare_client_needs,
2006
      .build = tlsext_keyshare_client_build,
2007
      .parse = tlsext_keyshare_client_parse,
2008
    },
2009
    .server = {
2010
      .needs = tlsext_keyshare_server_needs,
2011
      .build = tlsext_keyshare_server_build,
2012
      .parse = tlsext_keyshare_server_parse,
2013
    },
2014
  },
2015
  {
2016
    .type = TLSEXT_TYPE_server_name,
2017
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
2018
    .client = {
2019
      .needs = tlsext_sni_client_needs,
2020
      .build = tlsext_sni_client_build,
2021
      .parse = tlsext_sni_client_parse,
2022
    },
2023
    .server = {
2024
      .needs = tlsext_sni_server_needs,
2025
      .build = tlsext_sni_server_build,
2026
      .parse = tlsext_sni_server_parse,
2027
    },
2028
  },
2029
  {
2030
    .type = TLSEXT_TYPE_renegotiate,
2031
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
2032
    .client = {
2033
      .needs = tlsext_ri_client_needs,
2034
      .build = tlsext_ri_client_build,
2035
      .parse = tlsext_ri_client_parse,
2036
    },
2037
    .server = {
2038
      .needs = tlsext_ri_server_needs,
2039
      .build = tlsext_ri_server_build,
2040
      .parse = tlsext_ri_server_parse,
2041
    },
2042
  },
2043
  {
2044
    .type = TLSEXT_TYPE_status_request,
2045
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_CR |
2046
        SSL_TLSEXT_MSG_CT,
2047
    .client = {
2048
      .needs = tlsext_ocsp_client_needs,
2049
      .build = tlsext_ocsp_client_build,
2050
      .parse = tlsext_ocsp_client_parse,
2051
    },
2052
    .server = {
2053
      .needs = tlsext_ocsp_server_needs,
2054
      .build = tlsext_ocsp_server_build,
2055
      .parse = tlsext_ocsp_server_parse,
2056
    },
2057
  },
2058
  {
2059
    .type = TLSEXT_TYPE_ec_point_formats,
2060
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
2061
    .client = {
2062
      .needs = tlsext_ecpf_client_needs,
2063
      .build = tlsext_ecpf_client_build,
2064
      .parse = tlsext_ecpf_client_parse,
2065
    },
2066
    .server = {
2067
      .needs = tlsext_ecpf_server_needs,
2068
      .build = tlsext_ecpf_server_build,
2069
      .parse = tlsext_ecpf_server_parse,
2070
    },
2071
  },
2072
  {
2073
    .type = TLSEXT_TYPE_supported_groups,
2074
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
2075
    .client = {
2076
      .needs = tlsext_supportedgroups_client_needs,
2077
      .build = tlsext_supportedgroups_client_build,
2078
      .parse = tlsext_supportedgroups_client_parse,
2079
    },
2080
    .server = {
2081
      .needs = tlsext_supportedgroups_server_needs,
2082
      .build = tlsext_supportedgroups_server_build,
2083
      .parse = tlsext_supportedgroups_server_parse,
2084
    },
2085
  },
2086
  {
2087
    .type = TLSEXT_TYPE_session_ticket,
2088
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
2089
    .client = {
2090
      .needs = tlsext_sessionticket_client_needs,
2091
      .build = tlsext_sessionticket_client_build,
2092
      .parse = tlsext_sessionticket_client_parse,
2093
    },
2094
    .server = {
2095
      .needs = tlsext_sessionticket_server_needs,
2096
      .build = tlsext_sessionticket_server_build,
2097
      .parse = tlsext_sessionticket_server_parse,
2098
    },
2099
  },
2100
  {
2101
    .type = TLSEXT_TYPE_signature_algorithms,
2102
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_CR,
2103
    .client = {
2104
      .needs = tlsext_sigalgs_client_needs,
2105
      .build = tlsext_sigalgs_client_build,
2106
      .parse = tlsext_sigalgs_client_parse,
2107
    },
2108
    .server = {
2109
      .needs = tlsext_sigalgs_server_needs,
2110
      .build = tlsext_sigalgs_server_build,
2111
      .parse = tlsext_sigalgs_server_parse,
2112
    },
2113
  },
2114
  {
2115
    .type = TLSEXT_TYPE_application_layer_protocol_negotiation,
2116
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
2117
    .client = {
2118
      .needs = tlsext_alpn_client_needs,
2119
      .build = tlsext_alpn_client_build,
2120
      .parse = tlsext_alpn_client_parse,
2121
    },
2122
    .server = {
2123
      .needs = tlsext_alpn_server_needs,
2124
      .build = tlsext_alpn_server_build,
2125
      .parse = tlsext_alpn_server_parse,
2126
    },
2127
  },
2128
  {
2129
    .type = TLSEXT_TYPE_cookie,
2130
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_HRR,
2131
    .client = {
2132
      .needs = tlsext_cookie_client_needs,
2133
      .build = tlsext_cookie_client_build,
2134
      .parse = tlsext_cookie_client_parse,
2135
    },
2136
    .server = {
2137
      .needs = tlsext_cookie_server_needs,
2138
      .build = tlsext_cookie_server_build,
2139
      .parse = tlsext_cookie_server_parse,
2140
    },
2141
  },
2142
#ifndef OPENSSL_NO_SRTP
2143
  {
2144
    .type = TLSEXT_TYPE_use_srtp,
2145
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH /* XXX */ |
2146
        SSL_TLSEXT_MSG_EE,
2147
    .client = {
2148
      .needs = tlsext_srtp_client_needs,
2149
      .build = tlsext_srtp_client_build,
2150
      .parse = tlsext_srtp_client_parse,
2151
    },
2152
    .server = {
2153
      .needs = tlsext_srtp_server_needs,
2154
      .build = tlsext_srtp_server_build,
2155
      .parse = tlsext_srtp_server_parse,
2156
    },
2157
  },
2158
#endif /* OPENSSL_NO_SRTP */
2159
  {
2160
    .type = TLSEXT_TYPE_quic_transport_parameters,
2161
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
2162
    .client = {
2163
      .needs = tlsext_quic_transport_parameters_client_needs,
2164
      .build = tlsext_quic_transport_parameters_client_build,
2165
      .parse = tlsext_quic_transport_parameters_client_parse,
2166
    },
2167
    .server = {
2168
      .needs = tlsext_quic_transport_parameters_server_needs,
2169
      .build = tlsext_quic_transport_parameters_server_build,
2170
      .parse = tlsext_quic_transport_parameters_server_parse,
2171
    },
2172
  },
2173
  {
2174
    .type = TLSEXT_TYPE_psk_key_exchange_modes,
2175
    .messages = SSL_TLSEXT_MSG_CH,
2176
    .client = {
2177
      .needs = tlsext_psk_kex_modes_client_needs,
2178
      .build = tlsext_psk_kex_modes_client_build,
2179
      .parse = tlsext_psk_kex_modes_client_parse,
2180
    },
2181
    .server = {
2182
      .needs = tlsext_psk_kex_modes_server_needs,
2183
      .build = tlsext_psk_kex_modes_server_build,
2184
      .parse = tlsext_psk_kex_modes_server_parse,
2185
    },
2186
  },
2187
  {
2188
    /* MUST be last extension in CH per RFC 8446 section 4.2. */
2189
2190
    .type = TLSEXT_TYPE_pre_shared_key,
2191
    .messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
2192
    .client = {
2193
      .needs = tlsext_psk_client_needs,
2194
      .build = tlsext_psk_client_build,
2195
      .parse = tlsext_psk_client_parse,
2196
    },
2197
    .server = {
2198
      .needs = tlsext_psk_server_needs,
2199
      .build = tlsext_psk_server_build,
2200
      .parse = tlsext_psk_server_parse,
2201
    },
2202
  },
2203
};
2204
2205
159k
#define N_TLS_EXTENSIONS (sizeof(tls_extensions) / sizeof(*tls_extensions))
2206
2207
/* Ensure that extensions fit in a uint32_t bitmask. */
2208
CTASSERT(N_TLS_EXTENSIONS <= (sizeof(uint32_t) * 8));
2209
2210
const struct tls_extension *
2211
tls_extension_find(uint16_t type, size_t *tls_extensions_idx)
2212
9.70k
{
2213
9.70k
  size_t i;
2214
2215
87.7k
  for (i = 0; i < N_TLS_EXTENSIONS; i++) {
2216
85.1k
    if (tls_extensions[i].type == type) {
2217
7.13k
      *tls_extensions_idx = i;
2218
7.13k
      return &tls_extensions[i];
2219
7.13k
    }
2220
85.1k
  }
2221
2222
2.57k
  return NULL;
2223
9.70k
}
2224
2225
int
2226
tlsext_extension_seen(SSL *s, uint16_t type)
2227
1.12k
{
2228
1.12k
  size_t idx;
2229
2230
1.12k
  if (tls_extension_find(type, &idx) == NULL)
2231
0
    return 0;
2232
1.12k
  return ((s->s3->hs.extensions_seen & (1 << idx)) != 0);
2233
1.12k
}
2234
2235
const struct tls_extension_funcs *
2236
tlsext_funcs(const struct tls_extension *tlsext, int is_server)
2237
72.9k
{
2238
72.9k
  if (is_server)
2239
72.9k
    return &tlsext->server;
2240
2241
0
  return &tlsext->client;
2242
72.9k
}
2243
2244
static int
2245
tlsext_build(SSL *s, int is_server, uint16_t msg_type, CBB *cbb)
2246
4.46k
{
2247
4.46k
  const struct tls_extension_funcs *ext;
2248
4.46k
  const struct tls_extension *tlsext;
2249
4.46k
  CBB extensions, extension_data;
2250
4.46k
  int extensions_present = 0;
2251
4.46k
  uint16_t tls_version;
2252
4.46k
  size_t i;
2253
2254
4.46k
  tls_version = ssl_effective_tls_version(s);
2255
2256
4.46k
  if (!CBB_add_u16_length_prefixed(cbb, &extensions))
2257
0
    return 0;
2258
2259
71.4k
  for (i = 0; i < N_TLS_EXTENSIONS; i++) {
2260
66.9k
    tlsext = &tls_extensions[i];
2261
66.9k
    ext = tlsext_funcs(tlsext, is_server);
2262
2263
    /* RFC 8446 Section 4.2 */
2264
66.9k
    if (tls_version >= TLS1_3_VERSION &&
2265
66.9k
        !(tlsext->messages & msg_type))
2266
3.28k
      continue;
2267
2268
63.7k
    if (!ext->needs(s, msg_type))
2269
63.1k
      continue;
2270
2271
548
    if (!CBB_add_u16(&extensions, tlsext->type))
2272
0
      return 0;
2273
548
    if (!CBB_add_u16_length_prefixed(&extensions, &extension_data))
2274
0
      return 0;
2275
2276
548
    if (!ext->build(s, msg_type, &extension_data))
2277
0
      return 0;
2278
2279
548
    extensions_present = 1;
2280
548
  }
2281
2282
4.46k
  if (!extensions_present &&
2283
4.46k
      (msg_type & (SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH)) != 0)
2284
4.04k
    CBB_discard_child(cbb);
2285
2286
4.46k
  if (!CBB_flush(cbb))
2287
0
    return 0;
2288
2289
4.46k
  return 1;
2290
4.46k
}
2291
2292
int
2293
tlsext_clienthello_hash_extension(SSL *s, uint16_t type, CBS *cbs)
2294
1.06k
{
2295
  /*
2296
   * RFC 8446 4.1.2. For subsequent CH, early data will be removed,
2297
   * cookie may be added, padding may be removed.
2298
   */
2299
1.06k
  struct tls13_ctx *ctx = s->internal->tls13;
2300
2301
1.06k
  if (type == TLSEXT_TYPE_early_data || type == TLSEXT_TYPE_cookie ||
2302
1.06k
      type == TLSEXT_TYPE_padding)
2303
5
    return 1;
2304
1.05k
  if (!tls13_clienthello_hash_update_bytes(ctx, (void *)&type,
2305
1.05k
      sizeof(type)))
2306
0
    return 0;
2307
  /*
2308
   * key_share data may be changed, and pre_shared_key data may
2309
   * be changed
2310
   */
2311
1.05k
  if (type == TLSEXT_TYPE_pre_shared_key || type == TLSEXT_TYPE_key_share)
2312
226
    return 1;
2313
832
  if (!tls13_clienthello_hash_update(ctx, cbs))
2314
0
    return 0;
2315
2316
832
  return 1;
2317
832
}
2318
2319
static int
2320
tlsext_parse(SSL *s, int is_server, uint16_t msg_type, CBS *cbs, int *alert)
2321
5.52k
{
2322
5.52k
  const struct tls_extension_funcs *ext;
2323
5.52k
  const struct tls_extension *tlsext;
2324
5.52k
  CBS extensions, extension_data;
2325
5.52k
  uint16_t type;
2326
5.52k
  size_t idx;
2327
5.52k
  uint16_t tls_version;
2328
5.52k
  int alert_desc;
2329
2330
5.52k
  tls_version = ssl_effective_tls_version(s);
2331
2332
5.52k
  s->s3->hs.extensions_seen = 0;
2333
2334
  /* An empty extensions block is valid. */
2335
5.52k
  if (CBS_len(cbs) == 0)
2336
938
    return 1;
2337
2338
4.58k
  alert_desc = SSL_AD_DECODE_ERROR;
2339
2340
4.58k
  if (!CBS_get_u16_length_prefixed(cbs, &extensions))
2341
0
    goto err;
2342
2343
12.2k
  while (CBS_len(&extensions) > 0) {
2344
8.59k
    if (!CBS_get_u16(&extensions, &type))
2345
1
      goto err;
2346
8.59k
    if (!CBS_get_u16_length_prefixed(&extensions, &extension_data))
2347
7
      goto err;
2348
2349
8.58k
    if (s->internal->tlsext_debug_cb != NULL)
2350
0
      s->internal->tlsext_debug_cb(s, !is_server, type,
2351
0
          (unsigned char *)CBS_data(&extension_data),
2352
0
          CBS_len(&extension_data),
2353
0
          s->internal->tlsext_debug_arg);
2354
2355
    /* Unknown extensions are ignored. */
2356
8.58k
    if ((tlsext = tls_extension_find(type, &idx)) == NULL)
2357
2.57k
      continue;
2358
2359
6.01k
    if (tls_version >= TLS1_3_VERSION && is_server &&
2360
6.01k
        msg_type == SSL_TLSEXT_MSG_CH) {
2361
1.06k
      if (!tlsext_clienthello_hash_extension(s, type,
2362
1.06k
          &extension_data))
2363
0
        goto err;
2364
1.06k
    }
2365
2366
    /* RFC 8446 Section 4.2 */
2367
6.01k
    if (tls_version >= TLS1_3_VERSION &&
2368
6.01k
        !(tlsext->messages & msg_type)) {
2369
0
      alert_desc = SSL_AD_ILLEGAL_PARAMETER;
2370
0
      goto err;
2371
0
    }
2372
2373
    /* Check for duplicate known extensions. */
2374
6.01k
    if ((s->s3->hs.extensions_seen & (1 << idx)) != 0)
2375
26
      goto err;
2376
5.98k
    s->s3->hs.extensions_seen |= (1 << idx);
2377
2378
5.98k
    ext = tlsext_funcs(tlsext, is_server);
2379
5.98k
    if (!ext->parse(s, msg_type, &extension_data, &alert_desc))
2380
824
      goto err;
2381
2382
5.16k
    if (CBS_len(&extension_data) != 0)
2383
38
      goto err;
2384
5.16k
  }
2385
2386
3.68k
  return 1;
2387
2388
896
 err:
2389
896
  *alert = alert_desc;
2390
2391
896
  return 0;
2392
4.58k
}
2393
2394
static void
2395
tlsext_server_reset_state(SSL *s)
2396
5.52k
{
2397
5.52k
  s->tlsext_status_type = -1;
2398
5.52k
  s->s3->renegotiate_seen = 0;
2399
5.52k
  free(s->s3->alpn_selected);
2400
5.52k
  s->s3->alpn_selected = NULL;
2401
5.52k
  s->s3->alpn_selected_len = 0;
2402
5.52k
  s->internal->srtp_profile = NULL;
2403
5.52k
}
2404
2405
int
2406
tlsext_server_build(SSL *s, uint16_t msg_type, CBB *cbb)
2407
4.46k
{
2408
4.46k
  return tlsext_build(s, 1, msg_type, cbb);
2409
4.46k
}
2410
2411
int
2412
tlsext_server_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
2413
5.52k
{
2414
  /* XXX - this should be done by the caller... */
2415
5.52k
  if (msg_type == SSL_TLSEXT_MSG_CH)
2416
5.52k
    tlsext_server_reset_state(s);
2417
2418
5.52k
  return tlsext_parse(s, 1, msg_type, cbs, alert);
2419
5.52k
}
2420
2421
static void
2422
tlsext_client_reset_state(SSL *s)
2423
0
{
2424
0
  s->s3->renegotiate_seen = 0;
2425
0
  free(s->s3->alpn_selected);
2426
0
  s->s3->alpn_selected = NULL;
2427
0
  s->s3->alpn_selected_len = 0;
2428
0
}
2429
2430
int
2431
tlsext_client_build(SSL *s, uint16_t msg_type, CBB *cbb)
2432
0
{
2433
0
  return tlsext_build(s, 0, msg_type, cbb);
2434
0
}
2435
2436
int
2437
tlsext_client_parse(SSL *s, uint16_t msg_type, CBS *cbs, int *alert)
2438
0
{
2439
  /* XXX - this should be done by the caller... */
2440
0
  if (msg_type == SSL_TLSEXT_MSG_SH)
2441
0
    tlsext_client_reset_state(s);
2442
2443
0
  return tlsext_parse(s, 0, msg_type, cbs, alert);
2444
0
}