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_ctos_renegotiate(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
18 /* Add RI if renegotiating */
22 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
23 || !WPACKET_start_sub_packet_u16(pkt)
24 || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
25 s->s3->previous_client_finished_len)
26 || !WPACKET_close(pkt)) {
27 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
34 int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
37 if (s->tlsext_hostname == NULL)
40 /* Add TLS extension servername to the Client Hello message */
41 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
42 /* Sub-packet for server_name extension */
43 || !WPACKET_start_sub_packet_u16(pkt)
44 /* Sub-packet for servername list (always 1 hostname)*/
45 || !WPACKET_start_sub_packet_u16(pkt)
46 || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
47 || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,
48 strlen(s->tlsext_hostname))
49 || !WPACKET_close(pkt)
50 || !WPACKET_close(pkt)) {
51 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SERVER_NAME, ERR_R_INTERNAL_ERROR);
58 #ifndef OPENSSL_NO_SRP
59 int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al)
61 /* Add SRP username if there is one */
62 if (s->srp_ctx.login == NULL)
65 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
66 /* Sub-packet for SRP extension */
67 || !WPACKET_start_sub_packet_u16(pkt)
68 || !WPACKET_start_sub_packet_u8(pkt)
69 /* login must not be zero...internal error if so */
70 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
71 || !WPACKET_memcpy(pkt, s->srp_ctx.login,
72 strlen(s->srp_ctx.login))
73 || !WPACKET_close(pkt)
74 || !WPACKET_close(pkt)) {
75 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SRP, ERR_R_INTERNAL_ERROR);
84 static int use_ecc(SSL *s)
87 unsigned long alg_k, alg_a;
88 STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
90 /* See if we support any ECC ciphersuites */
91 if (s->version == SSL3_VERSION)
94 cipher_stack = SSL_get_ciphers(s);
95 end = sk_SSL_CIPHER_num(cipher_stack);
96 for (i = 0; i < end; 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)
110 int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, X509 *x,
111 size_t chain, int *al)
113 const unsigned char *pformats;
119 /* Add TLS extension ECPointFormats to the ClientHello message */
120 tls1_get_formatlist(s, &pformats, &num_formats);
122 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
123 /* Sub-packet for formats extension */
124 || !WPACKET_start_sub_packet_u16(pkt)
125 || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
126 || !WPACKET_close(pkt)) {
127 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
134 int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt, X509 *x,
135 size_t chain, int *al)
137 const unsigned char *pcurves = NULL, *pcurvestmp;
138 size_t num_curves = 0, i;
144 * Add TLS extension supported_groups to the ClientHello message
146 /* TODO(TLS1.3): Add support for DHE groups */
147 pcurves = s->tlsext_supportedgroupslist;
148 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
149 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
150 ERR_R_INTERNAL_ERROR);
153 pcurvestmp = pcurves;
155 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
156 /* Sub-packet for supported_groups extension */
157 || !WPACKET_start_sub_packet_u16(pkt)
158 || !WPACKET_start_sub_packet_u16(pkt)) {
159 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
160 ERR_R_INTERNAL_ERROR);
163 /* Copy curve ID if supported */
164 for (i = 0; i < num_curves; i++, pcurvestmp += 2) {
165 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
166 if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0])
167 || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) {
168 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
169 ERR_R_INTERNAL_ERROR);
174 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
175 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
176 ERR_R_INTERNAL_ERROR);
184 int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt, X509 *x,
185 size_t chain, int *al)
189 if (!tls_use_ticket(s))
192 if (!s->new_session && s->session != NULL
193 && s->session->tlsext_tick != NULL) {
194 ticklen = s->session->tlsext_ticklen;
195 } else if (s->session && s->tlsext_session_ticket != NULL
196 && s->tlsext_session_ticket->data != NULL) {
197 ticklen = s->tlsext_session_ticket->length;
198 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
199 if (s->session->tlsext_tick == NULL) {
200 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET,
201 ERR_R_INTERNAL_ERROR);
204 memcpy(s->session->tlsext_tick,
205 s->tlsext_session_ticket->data, ticklen);
206 s->session->tlsext_ticklen = ticklen;
211 if (ticklen == 0 && s->tlsext_session_ticket != NULL &&
212 s->tlsext_session_ticket->data == NULL)
215 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
216 || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick, ticklen)) {
217 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
224 int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
228 const unsigned char *salg;
230 if (!SSL_CLIENT_USE_SIGALGS(s))
233 salglen = tls12_get_psigalgs(s, &salg);
234 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
235 /* Sub-packet for sig-algs extension */
236 || !WPACKET_start_sub_packet_u16(pkt)
237 /* Sub-packet for the actual list */
238 || !WPACKET_start_sub_packet_u16(pkt)
239 || !tls12_copy_sigalgs(s, pkt, salg, salglen)
240 || !WPACKET_close(pkt)
241 || !WPACKET_close(pkt)) {
242 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS, ERR_R_INTERNAL_ERROR);
249 #ifndef OPENSSL_NO_OCSP
250 int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt, X509 *x,
251 size_t chain, 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_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
267 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
268 unsigned char *idbytes;
269 OCSP_RESPID *id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
270 int idlen = i2d_OCSP_RESPID(id, NULL);
273 /* Sub-packet for an individual id */
274 || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
275 || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
276 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
277 ERR_R_INTERNAL_ERROR);
281 if (!WPACKET_close(pkt)
282 || !WPACKET_start_sub_packet_u16(pkt)) {
283 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
286 if (s->tlsext_ocsp_exts) {
287 unsigned char *extbytes;
288 int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
291 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
292 ERR_R_INTERNAL_ERROR);
295 if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
296 || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)
298 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
299 ERR_R_INTERNAL_ERROR);
303 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
304 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
312 #ifndef OPENSSL_NO_NEXTPROTONEG
313 int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al)
315 if (s->ctx->next_proto_select_cb == NULL || s->s3->tmp.finish_md_len != 0)
319 * The client advertises an empty extension to indicate its support
320 * for Next Protocol Negotiation
322 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
323 || !WPACKET_put_bytes_u16(pkt, 0)) {
324 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_NPN, ERR_R_INTERNAL_ERROR);
332 int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
335 s->s3->alpn_sent = 0;
338 * finish_md_len is non-zero during a renegotiation, so
339 * this avoids sending ALPN during the renegotiation
341 if (s->alpn_client_proto_list == NULL || s->s3->tmp.finish_md_len != 0)
344 if (!WPACKET_put_bytes_u16(pkt,
345 TLSEXT_TYPE_application_layer_protocol_negotiation)
346 /* Sub-packet ALPN extension */
347 || !WPACKET_start_sub_packet_u16(pkt)
348 || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,
349 s->alpn_client_proto_list_len)
350 || !WPACKET_close(pkt)) {
351 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ALPN, ERR_R_INTERNAL_ERROR);
354 s->s3->alpn_sent = 1;
360 #ifndef OPENSSL_NO_SRTP
361 int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
364 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
370 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
371 /* Sub-packet for SRTP extension */
372 || !WPACKET_start_sub_packet_u16(pkt)
373 /* Sub-packet for the protection profile list */
374 || !WPACKET_start_sub_packet_u16(pkt)) {
375 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
379 end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
380 for (i = 0; i < end; i++) {
381 const SRTP_PROTECTION_PROFILE *prof =
382 sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
384 if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
385 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
389 if (!WPACKET_close(pkt)
390 /* Add an empty use_mki value */
391 || !WPACKET_put_bytes_u8(pkt, 0)
392 || !WPACKET_close(pkt)) {
393 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
401 int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al)
403 if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
406 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
407 || !WPACKET_put_bytes_u16(pkt, 0)) {
408 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ETM, ERR_R_INTERNAL_ERROR);
415 #ifndef OPENSSL_NO_CT
416 int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al)
418 if (s->ct_validation_callback == NULL)
421 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
422 || !WPACKET_put_bytes_u16(pkt, 0)) {
423 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SCT, ERR_R_INTERNAL_ERROR);
431 int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al)
433 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
434 || !WPACKET_put_bytes_u16(pkt, 0)) {
435 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EMS, ERR_R_INTERNAL_ERROR);
442 int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt, X509 *x,
443 size_t chain, int *al)
445 int currv, min_version, max_version, reason;
447 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
448 || !WPACKET_start_sub_packet_u16(pkt)
449 || !WPACKET_start_sub_packet_u8(pkt)) {
450 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
451 ERR_R_INTERNAL_ERROR);
455 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
457 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS, reason);
462 * TODO(TLS1.3): There is some discussion on the TLS list as to wheter
463 * we should include versions <TLS1.2. For the moment we do. To be
466 for (currv = max_version; currv >= min_version; currv--) {
467 /* TODO(TLS1.3): Remove this first if clause prior to release!! */
468 if (currv == TLS1_3_VERSION) {
469 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
470 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
471 ERR_R_INTERNAL_ERROR);
474 } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
475 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
476 ERR_R_INTERNAL_ERROR);
480 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
481 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
482 ERR_R_INTERNAL_ERROR);
489 int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
492 #ifndef OPENSSL_NO_TLS1_3
493 size_t i, sharessent = 0, num_curves = 0;
494 const unsigned char *pcurves = NULL;
496 /* key_share extension */
497 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
498 /* Extension data sub-packet */
499 || !WPACKET_start_sub_packet_u16(pkt)
500 /* KeyShare list sub-packet */
501 || !WPACKET_start_sub_packet_u16(pkt)) {
502 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
506 pcurves = s->tlsext_supportedgroupslist;
507 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
508 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
513 * TODO(TLS1.3): Make the number of key_shares sent configurable. For
516 for (i = 0; i < num_curves && sharessent < 1; i++, pcurves += 2) {
517 unsigned char *encodedPoint = NULL;
518 unsigned int curve_id = 0;
519 EVP_PKEY *key_share_key = NULL;
522 if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED))
525 if (s->s3->tmp.pkey != NULL) {
526 /* Shouldn't happen! */
527 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
531 /* Generate a key for this key_share */
532 curve_id = (pcurves[0] << 8) | pcurves[1];
533 key_share_key = ssl_generate_pkey_curve(curve_id);
534 if (key_share_key == NULL) {
535 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
539 /* Encode the public key. */
540 encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
542 if (encodedlen == 0) {
543 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_EC_LIB);
544 EVP_PKEY_free(key_share_key);
548 /* Create KeyShareEntry */
549 if (!WPACKET_put_bytes_u16(pkt, curve_id)
550 || !WPACKET_sub_memcpy_u16(pkt, encodedPoint, encodedlen)) {
551 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
552 EVP_PKEY_free(key_share_key);
553 OPENSSL_free(encodedPoint);
558 * TODO(TLS1.3): When changing to send more than one key_share we're
559 * going to need to be able to save more than one EVP_PKEY. For now
560 * we reuse the existing tmp.pkey
562 s->s3->group_id = curve_id;
563 s->s3->tmp.pkey = key_share_key;
565 OPENSSL_free(encodedPoint);
568 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
569 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
577 #define F5_WORKAROUND_MIN_MSG_LEN 0xff
578 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
580 int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
583 unsigned char *padbytes;
586 if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
590 * Add padding to workaround bugs in F5 terminators. See
591 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
592 * code calculates the length of all existing extensions it MUST always
595 if (!WPACKET_get_total_written(pkt, &hlen)) {
596 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PADDING, ERR_R_INTERNAL_ERROR);
600 if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
601 /* Calculate the amond of padding we need to add */
602 hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
605 * Take off the size of extension header itself (2 bytes for type and
606 * 2 bytes for length bytes)
613 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
614 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
615 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PADDING, ERR_R_INTERNAL_ERROR);
618 memset(padbytes, 0, hlen);
625 * Parse the server's renegotiation binding and abort if it's not right
627 int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, X509 *x, size_t chain,
630 size_t expected_len = s->s3->previous_client_finished_len
631 + s->s3->previous_server_finished_len;
633 const unsigned char *data;
635 /* Check for logic errors */
636 assert(expected_len == 0 || s->s3->previous_client_finished_len != 0);
637 assert(expected_len == 0 || s->s3->previous_server_finished_len != 0);
639 /* Parse the length byte */
640 if (!PACKET_get_1_len(pkt, &ilen)) {
641 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
642 SSL_R_RENEGOTIATION_ENCODING_ERR);
643 *al = SSL_AD_ILLEGAL_PARAMETER;
647 /* Consistency check */
648 if (PACKET_remaining(pkt) != ilen) {
649 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
650 SSL_R_RENEGOTIATION_ENCODING_ERR);
651 *al = SSL_AD_ILLEGAL_PARAMETER;
655 /* Check that the extension matches */
656 if (ilen != expected_len) {
657 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
658 SSL_R_RENEGOTIATION_MISMATCH);
659 *al = SSL_AD_HANDSHAKE_FAILURE;
663 if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
664 || memcmp(data, s->s3->previous_client_finished,
665 s->s3->previous_client_finished_len) != 0) {
666 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
667 SSL_R_RENEGOTIATION_MISMATCH);
668 *al = SSL_AD_HANDSHAKE_FAILURE;
672 if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
673 || memcmp(data, s->s3->previous_server_finished,
674 s->s3->previous_server_finished_len) != 0) {
675 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
676 SSL_R_RENEGOTIATION_MISMATCH);
677 *al = SSL_AD_ILLEGAL_PARAMETER;
680 s->s3->send_connection_binding = 1;
685 int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, X509 *x, size_t chain,
688 if (s->tlsext_hostname == NULL || PACKET_remaining(pkt) > 0) {
689 *al = SSL_AD_UNRECOGNIZED_NAME;
694 if (s->session->tlsext_hostname != NULL) {
695 *al = SSL_AD_INTERNAL_ERROR;
698 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
699 if (s->session->tlsext_hostname == NULL) {
700 *al = SSL_AD_INTERNAL_ERROR;
708 #ifndef OPENSSL_NO_EC
709 int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, X509 *x, size_t chain,
712 unsigned int ecpointformatlist_length;
713 PACKET ecptformatlist;
715 if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
716 *al = SSL_AD_DECODE_ERROR;
720 ecpointformatlist_length = PACKET_remaining(&ecptformatlist);
721 s->session->tlsext_ecpointformatlist_length = 0;
723 OPENSSL_free(s->session->tlsext_ecpointformatlist);
724 s->session->tlsext_ecpointformatlist =
725 OPENSSL_malloc(ecpointformatlist_length);
726 if (s->session->tlsext_ecpointformatlist == NULL) {
727 *al = SSL_AD_INTERNAL_ERROR;
731 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
733 if (!PACKET_copy_bytes(&ecptformatlist,
734 s->session->tlsext_ecpointformatlist,
735 ecpointformatlist_length)) {
736 *al = SSL_AD_INTERNAL_ERROR;
745 int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, X509 *x, size_t chain,
748 if (s->tls_session_ticket_ext_cb != NULL &&
749 !s->tls_session_ticket_ext_cb(s, PACKET_data(pkt),
750 PACKET_remaining(pkt),
751 s->tls_session_ticket_ext_cb_arg)) {
752 *al = SSL_AD_INTERNAL_ERROR;
756 if (!tls_use_ticket(s) || PACKET_remaining(pkt) > 0) {
757 *al = SSL_AD_UNSUPPORTED_EXTENSION;
761 s->tlsext_ticket_expected = 1;
766 #ifndef OPENSSL_NO_OCSP
767 int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, X509 *x, size_t chain,
771 * MUST be empty and only sent if we've requested a status
774 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_nothing
775 || PACKET_remaining(pkt) > 0) {
776 *al = SSL_AD_UNSUPPORTED_EXTENSION;
779 /* Set flag to expect CertificateStatus message */
780 s->tlsext_status_expected = 1;
787 #ifndef OPENSSL_NO_CT
788 int tls_parse_stoc_sct(SSL *s, PACKET *pkt, X509 *x, size_t chain, int *al)
791 * Only take it if we asked for it - i.e if there is no CT validation
792 * callback set, then a custom extension MAY be processing it, so we
793 * need to let control continue to flow to that.
795 if (s->ct_validation_callback != NULL) {
796 size_t size = PACKET_remaining(pkt);
798 /* Simply copy it off for later processing */
799 OPENSSL_free(s->tlsext_scts);
800 s->tlsext_scts = NULL;
802 s->tlsext_scts_len = size;
804 s->tlsext_scts = OPENSSL_malloc(size);
805 if (s->tlsext_scts == NULL
806 || !PACKET_copy_bytes(pkt, s->tlsext_scts, size)) {
807 *al = SSL_AD_INTERNAL_ERROR;
812 if (custom_ext_parse(s, 0, TLSEXT_TYPE_signed_certificate_timestamp,
813 PACKET_data(pkt), PACKET_remaining(pkt), al) <= 0)
822 #ifndef OPENSSL_NO_NEXTPROTONEG
824 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
825 * elements of zero length are allowed and the set of elements must exactly
826 * fill the length of the block. Returns 1 on success or 0 on failure.
828 static int ssl_next_proto_validate(PACKET *pkt)
832 while (PACKET_remaining(pkt)) {
833 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
834 || PACKET_remaining(&tmp_protocol) == 0)
841 int tls_parse_stoc_npn(SSL *s, PACKET *pkt, X509 *x, size_t chain, int *al)
843 unsigned char *selected;
844 unsigned char selected_len;
847 /* Check if we are in a renegotiation. If so ignore this extension */
848 if (s->s3->tmp.finish_md_len != 0)
851 /* We must have requested it. */
852 if (s->ctx->next_proto_select_cb == NULL) {
853 *al = SSL_AD_UNSUPPORTED_EXTENSION;
857 /* The data must be valid */
859 if (!ssl_next_proto_validate(&tmppkt)) {
860 *al = SSL_AD_DECODE_ERROR;
863 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
865 PACKET_remaining(pkt),
866 s->ctx->next_proto_select_cb_arg) !=
868 *al = SSL_AD_INTERNAL_ERROR;
873 * Could be non-NULL if server has sent multiple NPN extensions in
874 * a single Serverhello
876 OPENSSL_free(s->next_proto_negotiated);
877 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
878 if (s->next_proto_negotiated == NULL) {
879 *al = SSL_AD_INTERNAL_ERROR;
883 memcpy(s->next_proto_negotiated, selected, selected_len);
884 s->next_proto_negotiated_len = selected_len;
885 s->s3->next_proto_neg_seen = 1;
891 int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, X509 *x, size_t chain, int *al)
895 /* We must have requested it. */
896 if (!s->s3->alpn_sent) {
897 *al = SSL_AD_UNSUPPORTED_EXTENSION;
901 * The extension data consists of:
903 * uint8 proto_length;
904 * uint8 proto[proto_length];
906 if (!PACKET_get_net_2_len(pkt, &len)
907 || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
908 || PACKET_remaining(pkt) != len) {
909 *al = SSL_AD_DECODE_ERROR;
912 OPENSSL_free(s->s3->alpn_selected);
913 s->s3->alpn_selected = OPENSSL_malloc(len);
914 if (s->s3->alpn_selected == NULL) {
915 *al = SSL_AD_INTERNAL_ERROR;
918 if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
919 *al = SSL_AD_DECODE_ERROR;
922 s->s3->alpn_selected_len = len;
927 #ifndef OPENSSL_NO_SRTP
928 int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, X509 *x, size_t chain, int *al)
930 unsigned int id, ct, mki;
932 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
933 SRTP_PROTECTION_PROFILE *prof;
935 if (!PACKET_get_net_2(pkt, &ct) || ct != 2
936 || !PACKET_get_net_2(pkt, &id)
937 || !PACKET_get_1(pkt, &mki)
938 || PACKET_remaining(pkt) != 0) {
939 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP,
940 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
941 *al = SSL_AD_DECODE_ERROR;
946 /* Must be no MKI, since we never offer one */
947 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
948 *al = SSL_AD_ILLEGAL_PARAMETER;
952 /* Throw an error if the server gave us an unsolicited extension */
953 clnt = SSL_get_srtp_profiles(s);
955 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP, SSL_R_NO_SRTP_PROFILES);
956 *al = SSL_AD_DECODE_ERROR;
961 * Check to see if the server gave us something we support (and
962 * presumably offered)
964 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
965 prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
967 if (prof->id == id) {
968 s->srtp_profile = prof;
974 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP,
975 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
976 *al = SSL_AD_DECODE_ERROR;
981 int tls_parse_stoc_etm(SSL *s, PACKET *pkt, X509 *x, size_t chain, int *al)
983 /* Ignore if inappropriate ciphersuite */
984 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
985 && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
986 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
987 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
992 int tls_parse_stoc_ems(SSL *s, PACKET *pkt, X509 *x, size_t chain, int *al)
994 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
996 s->session->flags |= SSL_SESS_FLAG_EXTMS;
1001 int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, X509 *x, size_t chain,
1004 #ifndef OPENSSL_NO_TLS1_3
1005 unsigned int group_id;
1007 EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
1011 *al = SSL_AD_INTERNAL_ERROR;
1012 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1016 if (!PACKET_get_net_2(pkt, &group_id)) {
1017 *al = SSL_AD_HANDSHAKE_FAILURE;
1018 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
1022 if (group_id != s->s3->group_id) {
1024 * This isn't for the group that we sent in the original
1027 *al = SSL_AD_HANDSHAKE_FAILURE;
1028 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1032 if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1033 || PACKET_remaining(&encoded_pt) == 0) {
1034 *al = SSL_AD_DECODE_ERROR;
1035 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
1039 skey = ssl_generate_pkey(ckey);
1041 *al = SSL_AD_INTERNAL_ERROR;
1042 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE);
1045 if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
1046 PACKET_remaining(&encoded_pt))) {
1047 *al = SSL_AD_DECODE_ERROR;
1048 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_ECPOINT);
1049 EVP_PKEY_free(skey);
1053 if (ssl_derive(s, ckey, skey, 1) == 0) {
1054 *al = SSL_AD_INTERNAL_ERROR;
1055 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1056 EVP_PKEY_free(skey);
1059 EVP_PKEY_free(skey);