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