2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/ocsp.h>
12 #include "../ssl_locl.h"
13 #include "statem_locl.h"
15 int tls_construct_client_renegotiate(SSL *s, WPACKET *pkt, int *al)
17 /* Add RI if renegotiating */
21 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
22 || !WPACKET_start_sub_packet_u16(pkt)
23 || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
24 s->s3->previous_client_finished_len)
25 || !WPACKET_close(pkt)) {
26 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
33 int tls_construct_client_server_name(SSL *s, WPACKET *pkt, int *al)
35 if (s->tlsext_hostname == NULL)
38 /* Add TLS extension servername to the Client Hello message */
39 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
40 /* Sub-packet for server_name extension */
41 || !WPACKET_start_sub_packet_u16(pkt)
42 /* Sub-packet for servername list (always 1 hostname)*/
43 || !WPACKET_start_sub_packet_u16(pkt)
44 || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
45 || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,
46 strlen(s->tlsext_hostname))
47 || !WPACKET_close(pkt)
48 || !WPACKET_close(pkt)) {
49 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SERVER_NAME, ERR_R_INTERNAL_ERROR);
56 #ifndef OPENSSL_NO_SRP
57 int tls_construct_client_srp(SSL *s, WPACKET *pkt, int *al)
59 /* Add SRP username if there is one */
60 if (s->srp_ctx.login == NULL)
63 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
64 /* Sub-packet for SRP extension */
65 || !WPACKET_start_sub_packet_u16(pkt)
66 || !WPACKET_start_sub_packet_u8(pkt)
67 /* login must not be zero...internal error if so */
68 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
69 || !WPACKET_memcpy(pkt, s->srp_ctx.login,
70 strlen(s->srp_ctx.login))
71 || !WPACKET_close(pkt)
72 || !WPACKET_close(pkt)) {
73 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SRP, ERR_R_INTERNAL_ERROR);
82 static int use_ecc(SSL *s)
86 unsigned long alg_k, alg_a;
87 STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
89 /* See if we support any ECC ciphersuites */
90 if (s->version == SSL3_VERSION)
94 cipher_stack = SSL_get_ciphers(s);
96 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
97 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
99 alg_k = c->algorithm_mkey;
100 alg_a = c->algorithm_auth;
101 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
102 || (alg_a & SSL_aECDSA)
103 || c->min_tls >= TLS1_3_VERSION) {
112 int tls_construct_client_ec_pt_formats(SSL *s, WPACKET *pkt, int *al)
114 const unsigned char *pformats;
120 /* Add TLS extension ECPointFormats to the ClientHello message */
122 tls1_get_formatlist(s, &pformats, &num_formats);
124 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
125 /* Sub-packet for formats extension */
126 || !WPACKET_start_sub_packet_u16(pkt)
127 || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
128 || !WPACKET_close(pkt)) {
129 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
137 int tls_construct_client_supported_groups(SSL *s, WPACKET *pkt, int *al)
139 const unsigned char *pcurves = NULL, *pcurvestmp;
140 size_t num_curves = 0, i;
146 * Add TLS extension supported_groups to the ClientHello message
148 /* TODO(TLS1.3): Add support for DHE groups */
149 pcurves = s->tlsext_supportedgroupslist;
150 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
151 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,
152 ERR_R_INTERNAL_ERROR);
155 pcurvestmp = pcurves;
157 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
158 /* Sub-packet for supported_groups extension */
159 || !WPACKET_start_sub_packet_u16(pkt)
160 || !WPACKET_start_sub_packet_u16(pkt)) {
161 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,
162 ERR_R_INTERNAL_ERROR);
165 /* Copy curve ID if supported */
166 for (i = 0; i < num_curves; i++, pcurvestmp += 2) {
167 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
168 if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0])
169 || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) {
170 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,
171 ERR_R_INTERNAL_ERROR);
176 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
177 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_GROUPS,
178 ERR_R_INTERNAL_ERROR);
186 int tls_construct_client_session_ticket(SSL *s, WPACKET *pkt, int *al)
190 if (!tls_use_ticket(s))
193 if (!s->new_session && s->session != NULL
194 && s->session->tlsext_tick != NULL) {
195 ticklen = s->session->tlsext_ticklen;
196 } else if (s->session && s->tlsext_session_ticket != NULL
197 && s->tlsext_session_ticket->data != NULL) {
198 ticklen = s->tlsext_session_ticket->length;
199 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
200 if (s->session->tlsext_tick == NULL) {
201 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SESSION_TICKET,
202 ERR_R_INTERNAL_ERROR);
205 memcpy(s->session->tlsext_tick,
206 s->tlsext_session_ticket->data, ticklen);
207 s->session->tlsext_ticklen = ticklen;
212 if (ticklen == 0 && s->tlsext_session_ticket != NULL &&
213 s->tlsext_session_ticket->data == NULL)
216 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
217 || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick, ticklen)) {
218 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
225 int tls_construct_client_sig_algs(SSL *s, WPACKET *pkt, int *al)
228 const unsigned char *salg;
230 if (!SSL_CLIENT_USE_SIGALGS(s))
233 salglen = tls12_get_psigalgs(s, &salg);
235 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
236 /* Sub-packet for sig-algs extension */
237 || !WPACKET_start_sub_packet_u16(pkt)
238 /* Sub-packet for the actual list */
239 || !WPACKET_start_sub_packet_u16(pkt)
240 || !tls12_copy_sigalgs(s, pkt, salg, salglen)
241 || !WPACKET_close(pkt)
242 || !WPACKET_close(pkt)) {
243 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SIG_ALGS, ERR_R_INTERNAL_ERROR);
250 #ifndef OPENSSL_NO_OCSP
251 int tls_construct_client_status_request(SSL *s, WPACKET *pkt, int *al)
255 if (s->tlsext_status_type != TLSEXT_STATUSTYPE_ocsp)
258 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
259 /* Sub-packet for status request extension */
260 || !WPACKET_start_sub_packet_u16(pkt)
261 || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
262 /* Sub-packet for the ids */
263 || !WPACKET_start_sub_packet_u16(pkt)) {
264 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
267 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
268 unsigned char *idbytes;
272 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
273 idlen = i2d_OCSP_RESPID(id, NULL);
275 /* Sub-packet for an individual id */
276 || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
277 || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
278 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_STATUS_REQUEST,
279 ERR_R_INTERNAL_ERROR);
283 if (!WPACKET_close(pkt)
284 || !WPACKET_start_sub_packet_u16(pkt)) {
285 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
288 if (s->tlsext_ocsp_exts) {
289 unsigned char *extbytes;
290 int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
293 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_STATUS_REQUEST,
294 ERR_R_INTERNAL_ERROR);
297 if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
298 || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)
300 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_STATUS_REQUEST,
301 ERR_R_INTERNAL_ERROR);
305 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
306 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
314 #ifndef OPENSSL_NO_NEXTPROTONEG
315 int tls_construct_client_npn(SSL *s, WPACKET *pkt, int *al)
317 if (s->ctx->next_proto_select_cb == NULL || s->s3->tmp.finish_md_len != 0)
321 * The client advertises an empty extension to indicate its support
322 * for Next Protocol Negotiation
324 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
325 || !WPACKET_put_bytes_u16(pkt, 0)) {
326 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_NPN, ERR_R_INTERNAL_ERROR);
334 int tls_construct_client_alpn(SSL *s, WPACKET *pkt, int *al)
336 s->s3->alpn_sent = 0;
339 * finish_md_len is non-zero during a renegotiation, so
340 * this avoids sending ALPN during the renegotiation
342 if (s->alpn_client_proto_list == NULL || s->s3->tmp.finish_md_len != 0)
345 if (!WPACKET_put_bytes_u16(pkt,
346 TLSEXT_TYPE_application_layer_protocol_negotiation)
347 /* Sub-packet ALPN extension */
348 || !WPACKET_start_sub_packet_u16(pkt)
349 || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,
350 s->alpn_client_proto_list_len)
351 || !WPACKET_close(pkt)) {
352 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_ALPN, ERR_R_INTERNAL_ERROR);
355 s->s3->alpn_sent = 1;
361 #ifndef OPENSSL_NO_SRTP
362 int tls_construct_client_use_srtp(SSL *s, WPACKET *pkt, int *al)
364 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
365 SRTP_PROTECTION_PROFILE *prof;
371 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
372 /* Sub-packet for SRTP extension */
373 || !WPACKET_start_sub_packet_u16(pkt)
374 /* Sub-packet for the protection profile list */
375 || !WPACKET_start_sub_packet_u16(pkt)) {
376 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_USE_SRTP, ERR_R_INTERNAL_ERROR);
379 ct = sk_SRTP_PROTECTION_PROFILE_num(clnt);
380 for (i = 0; i < ct; i++) {
381 prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
382 if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
383 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_USE_SRTP, ERR_R_INTERNAL_ERROR);
387 if (!WPACKET_close(pkt)
388 /* Add an empty use_mki value */
389 || !WPACKET_put_bytes_u8(pkt, 0)
390 || !WPACKET_close(pkt)) {
391 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_USE_SRTP, ERR_R_INTERNAL_ERROR);
399 int tls_construct_client_etm(SSL *s, WPACKET *pkt, int *al)
401 if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
404 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
405 || !WPACKET_put_bytes_u16(pkt, 0)) {
406 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_ETM, ERR_R_INTERNAL_ERROR);
413 #ifndef OPENSSL_NO_CT
414 int tls_construct_client_sct(SSL *s, WPACKET *pkt, int *al)
416 if (s->ct_validation_callback == NULL)
419 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
420 || !WPACKET_put_bytes_u16(pkt, 0)) {
421 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SCT, ERR_R_INTERNAL_ERROR);
429 int tls_construct_client_ems(SSL *s, WPACKET *pkt, int *al)
431 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
432 || !WPACKET_put_bytes_u16(pkt, 0)) {
433 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_EMS, ERR_R_INTERNAL_ERROR);
440 int tls_construct_client_supported_versions(SSL *s, WPACKET *pkt, int *al)
442 int currv, min_version, max_version, reason;
444 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
445 || !WPACKET_start_sub_packet_u16(pkt)
446 || !WPACKET_start_sub_packet_u8(pkt)) {
447 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_VERSIONS,
448 ERR_R_INTERNAL_ERROR);
452 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
454 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_VERSIONS, reason);
459 * TODO(TLS1.3): There is some discussion on the TLS list as to wheter
460 * we should include versions <TLS1.2. For the moment we do. To be
463 for (currv = max_version; currv >= min_version; currv--) {
464 /* TODO(TLS1.3): Remove this first if clause prior to release!! */
465 if (currv == TLS1_3_VERSION) {
466 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
467 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_VERSIONS,
468 ERR_R_INTERNAL_ERROR);
471 } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
472 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_VERSIONS,
473 ERR_R_INTERNAL_ERROR);
477 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
478 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_SUPPORTED_VERSIONS,
479 ERR_R_INTERNAL_ERROR);
487 int tls_construct_client_key_share(SSL *s, WPACKET *pkt, int *al)
489 size_t i, sharessent = 0, num_curves = 0;
490 const unsigned char *pcurves = NULL;
492 /* key_share extension */
493 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
494 /* Extension data sub-packet */
495 || !WPACKET_start_sub_packet_u16(pkt)
496 /* KeyShare list sub-packet */
497 || !WPACKET_start_sub_packet_u16(pkt)) {
498 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR);
502 pcurves = s->tlsext_supportedgroupslist;
503 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
504 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR);
509 * TODO(TLS1.3): Make the number of key_shares sent configurable. For
512 for (i = 0; i < num_curves && sharessent < 1; i++, pcurves += 2) {
513 unsigned char *encodedPoint = NULL;
514 unsigned int curve_id = 0;
515 EVP_PKEY *key_share_key = NULL;
518 if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED))
521 if (s->s3->tmp.pkey != NULL) {
522 /* Shouldn't happen! */
523 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR);
527 /* Generate a key for this key_share */
528 curve_id = (pcurves[0] << 8) | pcurves[1];
529 key_share_key = ssl_generate_pkey_curve(curve_id);
530 if (key_share_key == NULL) {
531 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_EVP_LIB);
535 /* Encode the public key. */
536 encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
538 if (encodedlen == 0) {
539 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_EC_LIB);
540 EVP_PKEY_free(key_share_key);
544 /* Create KeyShareEntry */
545 if (!WPACKET_put_bytes_u16(pkt, curve_id)
546 || !WPACKET_sub_memcpy_u16(pkt, encodedPoint, encodedlen)) {
547 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR);
548 EVP_PKEY_free(key_share_key);
549 OPENSSL_free(encodedPoint);
554 * TODO(TLS1.3): When changing to send more than one key_share we're
555 * going to need to be able to save more than one EVP_PKEY. For now
556 * we reuse the existing tmp.pkey
558 s->s3->group_id = curve_id;
559 s->s3->tmp.pkey = key_share_key;
561 OPENSSL_free(encodedPoint);
564 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
565 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR);
572 int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al)
574 unsigned char *padbytes;
577 if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
581 * Add padding to workaround bugs in F5 terminators. See
582 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
583 * code works out the length of all existing extensions it MUST always
586 if (!WPACKET_get_total_written(pkt, &hlen)) {
587 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_PADDING, ERR_R_INTERNAL_ERROR);
591 if (hlen > 0xff && hlen < 0x200) {
598 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
599 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
600 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_PADDING, ERR_R_INTERNAL_ERROR);
603 memset(padbytes, 0, hlen);
610 * Parse the server's renegotiation binding and abort if it's not right
612 int tls_parse_server_renegotiate(SSL *s, PACKET *pkt, int *al)
614 size_t expected_len = s->s3->previous_client_finished_len
615 + s->s3->previous_server_finished_len;
617 const unsigned char *data;
619 /* Check for logic errors */
620 assert(expected_len == 0 || s->s3->previous_client_finished_len != 0);
621 assert(expected_len == 0 || s->s3->previous_server_finished_len != 0);
623 /* Parse the length byte */
624 if (!PACKET_get_1_len(pkt, &ilen)) {
625 SSLerr(SSL_F_TLS_PARSE_SERVER_RENEGOTIATE,
626 SSL_R_RENEGOTIATION_ENCODING_ERR);
627 *al = SSL_AD_ILLEGAL_PARAMETER;
631 /* Consistency check */
632 if (PACKET_remaining(pkt) != ilen) {
633 SSLerr(SSL_F_TLS_PARSE_SERVER_RENEGOTIATE,
634 SSL_R_RENEGOTIATION_ENCODING_ERR);
635 *al = SSL_AD_ILLEGAL_PARAMETER;
639 /* Check that the extension matches */
640 if (ilen != expected_len) {
641 SSLerr(SSL_F_TLS_PARSE_SERVER_RENEGOTIATE,
642 SSL_R_RENEGOTIATION_MISMATCH);
643 *al = SSL_AD_HANDSHAKE_FAILURE;
647 if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
648 || memcmp(data, s->s3->previous_client_finished,
649 s->s3->previous_client_finished_len) != 0) {
650 SSLerr(SSL_F_TLS_PARSE_SERVER_RENEGOTIATE,
651 SSL_R_RENEGOTIATION_MISMATCH);
652 *al = SSL_AD_HANDSHAKE_FAILURE;
656 if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
657 || memcmp(data, s->s3->previous_server_finished,
658 s->s3->previous_server_finished_len) != 0) {
659 SSLerr(SSL_F_TLS_PARSE_SERVER_RENEGOTIATE,
660 SSL_R_RENEGOTIATION_MISMATCH);
661 *al = SSL_AD_ILLEGAL_PARAMETER;
664 s->s3->send_connection_binding = 1;
669 int tls_parse_server_server_name(SSL *s, PACKET *pkt, int *al)
671 if (s->tlsext_hostname == NULL || PACKET_remaining(pkt) > 0) {
672 *al = SSL_AD_UNRECOGNIZED_NAME;
677 if (s->session->tlsext_hostname != NULL) {
678 *al = SSL_AD_INTERNAL_ERROR;
681 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
682 if (s->session->tlsext_hostname == NULL) {
683 *al = SSL_AD_INTERNAL_ERROR;
691 #ifndef OPENSSL_NO_EC
692 int tls_parse_server_ec_pt_formats(SSL *s, PACKET *pkt, int *al)
694 unsigned int ecpointformatlist_length;
695 PACKET ecptformatlist;
697 if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
698 *al = SSL_AD_DECODE_ERROR;
702 ecpointformatlist_length = PACKET_remaining(&ecptformatlist);
703 s->session->tlsext_ecpointformatlist_length = 0;
705 OPENSSL_free(s->session->tlsext_ecpointformatlist);
706 s->session->tlsext_ecpointformatlist =
707 OPENSSL_malloc(ecpointformatlist_length);
708 if (s->session->tlsext_ecpointformatlist == NULL) {
709 *al = SSL_AD_INTERNAL_ERROR;
713 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
715 if (!PACKET_copy_bytes(&ecptformatlist,
716 s->session->tlsext_ecpointformatlist,
717 ecpointformatlist_length)) {
718 *al = SSL_AD_INTERNAL_ERROR;
727 int tls_parse_server_session_ticket(SSL *s, PACKET *pkt, int *al)
729 if (s->tls_session_ticket_ext_cb &&
730 !s->tls_session_ticket_ext_cb(s, PACKET_data(pkt),
731 PACKET_remaining(pkt),
732 s->tls_session_ticket_ext_cb_arg)) {
733 *al = SSL_AD_INTERNAL_ERROR;
736 if (!tls_use_ticket(s) || PACKET_remaining(pkt) > 0) {
737 *al = SSL_AD_UNSUPPORTED_EXTENSION;
740 s->tlsext_ticket_expected = 1;
745 #ifndef OPENSSL_NO_OCSP
746 int tls_parse_server_status_request(SSL *s, PACKET *pkt, int *al)
749 * MUST be empty and only sent if we've requested a status
752 if (s->tlsext_status_type == -1 || PACKET_remaining(pkt) > 0) {
753 *al = SSL_AD_UNSUPPORTED_EXTENSION;
756 /* Set flag to expect CertificateStatus message */
757 s->tlsext_status_expected = 1;
764 #ifndef OPENSSL_NO_CT
765 int tls_parse_server_sct(SSL *s, PACKET *pkt, int *al)
768 * Only take it if we asked for it - i.e if there is no CT validation
769 * callback set, then a custom extension MAY be processing it, so we
770 * need to let control continue to flow to that.
772 if (s->ct_validation_callback != NULL) {
773 size_t size = PACKET_remaining(pkt);
775 /* Simply copy it off for later processing */
776 if (s->tlsext_scts != NULL) {
777 OPENSSL_free(s->tlsext_scts);
778 s->tlsext_scts = NULL;
780 s->tlsext_scts_len = size;
782 s->tlsext_scts = OPENSSL_malloc(size);
783 if (s->tlsext_scts == NULL
784 || !PACKET_copy_bytes(pkt, s->tlsext_scts, size)) {
785 *al = SSL_AD_INTERNAL_ERROR;
790 if (custom_ext_parse(s, 0, TLSEXT_TYPE_signed_certificate_timestamp,
791 PACKET_data(pkt), PACKET_remaining(pkt), al) <= 0)
800 #ifndef OPENSSL_NO_NEXTPROTONEG
802 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
803 * elements of zero length are allowed and the set of elements must exactly
804 * fill the length of the block. Returns 1 on success or 0 on failure.
806 static int ssl_next_proto_validate(PACKET *pkt)
810 while (PACKET_remaining(pkt)) {
811 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
812 || PACKET_remaining(&tmp_protocol) == 0)
819 int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al)
821 unsigned char *selected;
822 unsigned char selected_len;
825 if (s->s3->tmp.finish_md_len != 0)
828 /* We must have requested it. */
829 if (s->ctx->next_proto_select_cb == NULL) {
830 *al = SSL_AD_UNSUPPORTED_EXTENSION;
833 /* The data must be valid */
835 if (!ssl_next_proto_validate(&tmppkt)) {
836 *al = SSL_AD_DECODE_ERROR;
839 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
841 PACKET_remaining(pkt),
842 s->ctx->next_proto_select_cb_arg) !=
844 *al = SSL_AD_INTERNAL_ERROR;
848 * Could be non-NULL if server has sent multiple NPN extensions in
849 * a single Serverhello
851 OPENSSL_free(s->next_proto_negotiated);
852 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
853 if (s->next_proto_negotiated == NULL) {
854 *al = SSL_AD_INTERNAL_ERROR;
858 memcpy(s->next_proto_negotiated, selected, selected_len);
859 s->next_proto_negotiated_len = selected_len;
860 s->s3->next_proto_neg_seen = 1;
866 int tls_parse_server_alpn(SSL *s, PACKET *pkt, int *al)
870 /* We must have requested it. */
871 if (!s->s3->alpn_sent) {
872 *al = SSL_AD_UNSUPPORTED_EXTENSION;
876 * The extension data consists of:
878 * uint8 proto_length;
879 * uint8 proto[proto_length];
881 if (!PACKET_get_net_2_len(pkt, &len)
882 || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
883 || PACKET_remaining(pkt) != len) {
884 *al = SSL_AD_DECODE_ERROR;
887 OPENSSL_free(s->s3->alpn_selected);
888 s->s3->alpn_selected = OPENSSL_malloc(len);
889 if (s->s3->alpn_selected == NULL) {
890 *al = SSL_AD_INTERNAL_ERROR;
893 if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
894 *al = SSL_AD_DECODE_ERROR;
897 s->s3->alpn_selected_len = len;
902 #ifndef OPENSSL_NO_SRTP
903 int tls_parse_server_use_srtp(SSL *s, PACKET *pkt, int *al)
905 unsigned int id, ct, mki;
907 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
908 SRTP_PROTECTION_PROFILE *prof;
910 if (!PACKET_get_net_2(pkt, &ct)
911 || ct != 2 || !PACKET_get_net_2(pkt, &id)
912 || !PACKET_get_1(pkt, &mki)
913 || PACKET_remaining(pkt) != 0) {
914 SSLerr(SSL_F_TLS_PARSE_SERVER_USE_SRTP,
915 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
916 *al = SSL_AD_DECODE_ERROR;
921 /* Must be no MKI, since we never offer one */
922 SSLerr(SSL_F_TLS_PARSE_SERVER_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
923 *al = SSL_AD_ILLEGAL_PARAMETER;
927 clnt = SSL_get_srtp_profiles(s);
929 /* Throw an error if the server gave us an unsolicited extension */
931 SSLerr(SSL_F_TLS_PARSE_SERVER_USE_SRTP, SSL_R_NO_SRTP_PROFILES);
932 *al = SSL_AD_DECODE_ERROR;
937 * Check to see if the server gave us something we support (and
938 * presumably offered)
940 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
941 prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
943 if (prof->id == id) {
944 s->srtp_profile = prof;
950 SSLerr(SSL_F_TLS_PARSE_SERVER_USE_SRTP,
951 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
952 *al = SSL_AD_DECODE_ERROR;
957 int tls_parse_server_etm(SSL *s, PACKET *pkt, int *al)
959 /* Ignore if inappropriate ciphersuite */
960 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
961 && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
962 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
963 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
968 int tls_parse_server_ems(SSL *s, PACKET *pkt, int *al)
970 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
972 s->session->flags |= SSL_SESS_FLAG_EXTMS;
977 int tls_parse_server_key_share(SSL *s, PACKET *pkt, int *al)
979 unsigned int group_id;
981 EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
985 *al = SSL_AD_INTERNAL_ERROR;
986 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, ERR_R_INTERNAL_ERROR);
990 if (!PACKET_get_net_2(pkt, &group_id)) {
991 *al = SSL_AD_HANDSHAKE_FAILURE;
992 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
996 if (group_id != s->s3->group_id) {
998 * This isn't for the group that we sent in the original
1001 *al = SSL_AD_HANDSHAKE_FAILURE;
1002 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1006 if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1007 || PACKET_remaining(&encoded_pt) == 0) {
1008 *al = SSL_AD_DECODE_ERROR;
1009 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
1013 skey = ssl_generate_pkey(ckey);
1015 *al = SSL_AD_INTERNAL_ERROR;
1016 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, ERR_R_MALLOC_FAILURE);
1019 if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
1020 PACKET_remaining(&encoded_pt))) {
1021 *al = SSL_AD_DECODE_ERROR;
1022 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, SSL_R_BAD_ECPOINT);
1026 if (ssl_derive(s, ckey, skey, 1) == 0) {
1027 *al = SSL_AD_INTERNAL_ERROR;
1028 SSLerr(SSL_F_TLS_PARSE_SERVER_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1029 EVP_PKEY_free(skey);
1032 EVP_PKEY_free(skey);
1037 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
1039 RAW_EXTENSION *extensions = NULL;
1042 #ifndef OPENSSL_NO_NEXTPROTONEG
1043 s->s3->next_proto_neg_seen = 0;
1045 s->tlsext_ticket_expected = 0;
1047 OPENSSL_free(s->s3->alpn_selected);
1048 s->s3->alpn_selected = NULL;
1050 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1052 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1054 if (!PACKET_as_length_prefixed_2(pkt, &extpkt)) {
1055 /* Extensions block may be completely absent in SSLv3 */
1056 if (s->version != SSL3_VERSION || PACKET_remaining(pkt) != 0) {
1057 *al = SSL_AD_DECODE_ERROR;
1058 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_BAD_LENGTH);
1061 PACKET_null_init(&extpkt);
1065 * TODO(TLS1.3): We give multiple contexts for now until we're ready to
1066 * give something more specific
1069 if (!tls_collect_extensions(s, &extpkt, EXT_TLS1_2_SERVER_HELLO
1070 | EXT_TLS1_3_SERVER_HELLO
1071 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1072 | EXT_TLS1_3_CERTIFICATE,
1077 * Determine if we need to see RI. Strictly speaking if we want to avoid
1078 * an attack we should *always* see RI even on initial server hello
1079 * because the client doesn't see any renegotiation during an attack.
1080 * However this would mean we could not connect to any server which
1081 * doesn't support RI so for the immediate future tolerate RI absence
1083 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1084 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1085 && !extensions[TLSEXT_IDX_renegotiate].present) {
1086 *al = SSL_AD_HANDSHAKE_FAILURE;
1087 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
1088 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1092 if (!tls_parse_all_extensions(s, EXT_TLS1_2_SERVER_HELLO
1093 | EXT_TLS1_3_SERVER_HELLO
1094 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1095 | EXT_TLS1_3_CERTIFICATE,
1101 * Check extended master secret extension is consistent with
1104 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
1105 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
1106 *al = SSL_AD_HANDSHAKE_FAILURE;
1107 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
1115 static int ssl_check_serverhello_tlsext(SSL *s)
1117 int ret = SSL_TLSEXT_ERR_NOACK;
1118 int al = SSL_AD_UNRECOGNIZED_NAME;
1120 #ifndef OPENSSL_NO_EC
1122 * If we are client and using an elliptic curve cryptography cipher
1123 * suite, then if server returns an EC point formats lists extension it
1124 * must contain uncompressed.
1126 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1127 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1128 if ((s->tlsext_ecpointformatlist != NULL)
1129 && (s->tlsext_ecpointformatlist_length > 0)
1130 && (s->session->tlsext_ecpointformatlist != NULL)
1131 && (s->session->tlsext_ecpointformatlist_length > 0)
1132 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
1133 /* we are using an ECC cipher */
1135 unsigned char *list;
1136 int found_uncompressed = 0;
1137 list = s->session->tlsext_ecpointformatlist;
1138 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
1139 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
1140 found_uncompressed = 1;
1144 if (!found_uncompressed) {
1145 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
1146 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1150 ret = SSL_TLSEXT_ERR_OK;
1151 #endif /* OPENSSL_NO_EC */
1153 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1155 s->ctx->tlsext_servername_callback(s, &al,
1156 s->ctx->tlsext_servername_arg);
1157 else if (s->initial_ctx != NULL
1158 && s->initial_ctx->tlsext_servername_callback != 0)
1160 s->initial_ctx->tlsext_servername_callback(s, &al,
1162 initial_ctx->tlsext_servername_arg);
1165 * Ensure we get sensible values passed to tlsext_status_cb in the event
1166 * that we don't receive a status message
1168 OPENSSL_free(s->tlsext_ocsp_resp);
1169 s->tlsext_ocsp_resp = NULL;
1170 s->tlsext_ocsp_resplen = 0;
1173 case SSL_TLSEXT_ERR_ALERT_FATAL:
1174 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1177 case SSL_TLSEXT_ERR_ALERT_WARNING:
1178 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1181 case SSL_TLSEXT_ERR_NOACK:
1182 s->servername_done = 0;
1188 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
1191 if (s->version < SSL3_VERSION)
1193 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
1194 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1198 if (ssl_check_serverhello_tlsext(s) <= 0) {
1199 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);