1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include <openssl/ocsp.h>
11*4724848cSchristos #include "../ssl_local.h"
12*4724848cSchristos #include "internal/cryptlib.h"
13*4724848cSchristos #include "statem_local.h"
14*4724848cSchristos
tls_construct_ctos_renegotiate(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)15*4724848cSchristos EXT_RETURN tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt,
16*4724848cSchristos unsigned int context, X509 *x,
17*4724848cSchristos size_t chainidx)
18*4724848cSchristos {
19*4724848cSchristos /* Add RI if renegotiating */
20*4724848cSchristos if (!s->renegotiate)
21*4724848cSchristos return EXT_RETURN_NOT_SENT;
22*4724848cSchristos
23*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
24*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
25*4724848cSchristos || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
26*4724848cSchristos s->s3->previous_client_finished_len)
27*4724848cSchristos || !WPACKET_close(pkt)) {
28*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE,
29*4724848cSchristos ERR_R_INTERNAL_ERROR);
30*4724848cSchristos return EXT_RETURN_FAIL;
31*4724848cSchristos }
32*4724848cSchristos
33*4724848cSchristos return EXT_RETURN_SENT;
34*4724848cSchristos }
35*4724848cSchristos
tls_construct_ctos_server_name(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)36*4724848cSchristos EXT_RETURN tls_construct_ctos_server_name(SSL *s, WPACKET *pkt,
37*4724848cSchristos unsigned int context, X509 *x,
38*4724848cSchristos size_t chainidx)
39*4724848cSchristos {
40*4724848cSchristos if (s->ext.hostname == NULL)
41*4724848cSchristos return EXT_RETURN_NOT_SENT;
42*4724848cSchristos
43*4724848cSchristos /* Add TLS extension servername to the Client Hello message */
44*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
45*4724848cSchristos /* Sub-packet for server_name extension */
46*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
47*4724848cSchristos /* Sub-packet for servername list (always 1 hostname)*/
48*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
49*4724848cSchristos || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
50*4724848cSchristos || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
51*4724848cSchristos strlen(s->ext.hostname))
52*4724848cSchristos || !WPACKET_close(pkt)
53*4724848cSchristos || !WPACKET_close(pkt)) {
54*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SERVER_NAME,
55*4724848cSchristos ERR_R_INTERNAL_ERROR);
56*4724848cSchristos return EXT_RETURN_FAIL;
57*4724848cSchristos }
58*4724848cSchristos
59*4724848cSchristos return EXT_RETURN_SENT;
60*4724848cSchristos }
61*4724848cSchristos
62*4724848cSchristos /* Push a Max Fragment Len extension into ClientHello */
tls_construct_ctos_maxfragmentlen(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)63*4724848cSchristos EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL *s, WPACKET *pkt,
64*4724848cSchristos unsigned int context, X509 *x,
65*4724848cSchristos size_t chainidx)
66*4724848cSchristos {
67*4724848cSchristos if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)
68*4724848cSchristos return EXT_RETURN_NOT_SENT;
69*4724848cSchristos
70*4724848cSchristos /* Add Max Fragment Length extension if client enabled it. */
71*4724848cSchristos /*-
72*4724848cSchristos * 4 bytes for this extension type and extension length
73*4724848cSchristos * 1 byte for the Max Fragment Length code value.
74*4724848cSchristos */
75*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
76*4724848cSchristos /* Sub-packet for Max Fragment Length extension (1 byte) */
77*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
78*4724848cSchristos || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)
79*4724848cSchristos || !WPACKET_close(pkt)) {
80*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
81*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);
82*4724848cSchristos return EXT_RETURN_FAIL;
83*4724848cSchristos }
84*4724848cSchristos
85*4724848cSchristos return EXT_RETURN_SENT;
86*4724848cSchristos }
87*4724848cSchristos
88*4724848cSchristos #ifndef OPENSSL_NO_SRP
tls_construct_ctos_srp(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)89*4724848cSchristos EXT_RETURN tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context,
90*4724848cSchristos X509 *x, size_t chainidx)
91*4724848cSchristos {
92*4724848cSchristos /* Add SRP username if there is one */
93*4724848cSchristos if (s->srp_ctx.login == NULL)
94*4724848cSchristos return EXT_RETURN_NOT_SENT;
95*4724848cSchristos
96*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
97*4724848cSchristos /* Sub-packet for SRP extension */
98*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
99*4724848cSchristos || !WPACKET_start_sub_packet_u8(pkt)
100*4724848cSchristos /* login must not be zero...internal error if so */
101*4724848cSchristos || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
102*4724848cSchristos || !WPACKET_memcpy(pkt, s->srp_ctx.login,
103*4724848cSchristos strlen(s->srp_ctx.login))
104*4724848cSchristos || !WPACKET_close(pkt)
105*4724848cSchristos || !WPACKET_close(pkt)) {
106*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SRP,
107*4724848cSchristos ERR_R_INTERNAL_ERROR);
108*4724848cSchristos return EXT_RETURN_FAIL;
109*4724848cSchristos }
110*4724848cSchristos
111*4724848cSchristos return EXT_RETURN_SENT;
112*4724848cSchristos }
113*4724848cSchristos #endif
114*4724848cSchristos
115*4724848cSchristos #ifndef OPENSSL_NO_EC
use_ecc(SSL * s)116*4724848cSchristos static int use_ecc(SSL *s)
117*4724848cSchristos {
118*4724848cSchristos int i, end, ret = 0;
119*4724848cSchristos unsigned long alg_k, alg_a;
120*4724848cSchristos STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
121*4724848cSchristos const uint16_t *pgroups = NULL;
122*4724848cSchristos size_t num_groups, j;
123*4724848cSchristos
124*4724848cSchristos /* See if we support any ECC ciphersuites */
125*4724848cSchristos if (s->version == SSL3_VERSION)
126*4724848cSchristos return 0;
127*4724848cSchristos
128*4724848cSchristos cipher_stack = SSL_get1_supported_ciphers(s);
129*4724848cSchristos end = sk_SSL_CIPHER_num(cipher_stack);
130*4724848cSchristos for (i = 0; i < end; i++) {
131*4724848cSchristos const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
132*4724848cSchristos
133*4724848cSchristos alg_k = c->algorithm_mkey;
134*4724848cSchristos alg_a = c->algorithm_auth;
135*4724848cSchristos if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
136*4724848cSchristos || (alg_a & SSL_aECDSA)
137*4724848cSchristos || c->min_tls >= TLS1_3_VERSION) {
138*4724848cSchristos ret = 1;
139*4724848cSchristos break;
140*4724848cSchristos }
141*4724848cSchristos }
142*4724848cSchristos
143*4724848cSchristos sk_SSL_CIPHER_free(cipher_stack);
144*4724848cSchristos if (!ret)
145*4724848cSchristos return 0;
146*4724848cSchristos
147*4724848cSchristos /* Check we have at least one EC supported group */
148*4724848cSchristos tls1_get_supported_groups(s, &pgroups, &num_groups);
149*4724848cSchristos for (j = 0; j < num_groups; j++) {
150*4724848cSchristos uint16_t ctmp = pgroups[j];
151*4724848cSchristos
152*4724848cSchristos if (tls_curve_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
153*4724848cSchristos return 1;
154*4724848cSchristos }
155*4724848cSchristos
156*4724848cSchristos return 0;
157*4724848cSchristos }
158*4724848cSchristos
tls_construct_ctos_ec_pt_formats(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)159*4724848cSchristos EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt,
160*4724848cSchristos unsigned int context, X509 *x,
161*4724848cSchristos size_t chainidx)
162*4724848cSchristos {
163*4724848cSchristos const unsigned char *pformats;
164*4724848cSchristos size_t num_formats;
165*4724848cSchristos
166*4724848cSchristos if (!use_ecc(s))
167*4724848cSchristos return EXT_RETURN_NOT_SENT;
168*4724848cSchristos
169*4724848cSchristos /* Add TLS extension ECPointFormats to the ClientHello message */
170*4724848cSchristos tls1_get_formatlist(s, &pformats, &num_formats);
171*4724848cSchristos
172*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
173*4724848cSchristos /* Sub-packet for formats extension */
174*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
175*4724848cSchristos || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
176*4724848cSchristos || !WPACKET_close(pkt)) {
177*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
178*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
179*4724848cSchristos return EXT_RETURN_FAIL;
180*4724848cSchristos }
181*4724848cSchristos
182*4724848cSchristos return EXT_RETURN_SENT;
183*4724848cSchristos }
184*4724848cSchristos
tls_construct_ctos_supported_groups(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)185*4724848cSchristos EXT_RETURN tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
186*4724848cSchristos unsigned int context, X509 *x,
187*4724848cSchristos size_t chainidx)
188*4724848cSchristos {
189*4724848cSchristos const uint16_t *pgroups = NULL;
190*4724848cSchristos size_t num_groups = 0, i;
191*4724848cSchristos
192*4724848cSchristos if (!use_ecc(s))
193*4724848cSchristos return EXT_RETURN_NOT_SENT;
194*4724848cSchristos
195*4724848cSchristos /*
196*4724848cSchristos * Add TLS extension supported_groups to the ClientHello message
197*4724848cSchristos */
198*4724848cSchristos /* TODO(TLS1.3): Add support for DHE groups */
199*4724848cSchristos tls1_get_supported_groups(s, &pgroups, &num_groups);
200*4724848cSchristos
201*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
202*4724848cSchristos /* Sub-packet for supported_groups extension */
203*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
204*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)) {
205*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
206*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
207*4724848cSchristos ERR_R_INTERNAL_ERROR);
208*4724848cSchristos return EXT_RETURN_FAIL;
209*4724848cSchristos }
210*4724848cSchristos /* Copy curve ID if supported */
211*4724848cSchristos for (i = 0; i < num_groups; i++) {
212*4724848cSchristos uint16_t ctmp = pgroups[i];
213*4724848cSchristos
214*4724848cSchristos if (tls_curve_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) {
215*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
216*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
217*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
218*4724848cSchristos ERR_R_INTERNAL_ERROR);
219*4724848cSchristos return EXT_RETURN_FAIL;
220*4724848cSchristos }
221*4724848cSchristos }
222*4724848cSchristos }
223*4724848cSchristos if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
224*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
225*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
226*4724848cSchristos ERR_R_INTERNAL_ERROR);
227*4724848cSchristos return EXT_RETURN_FAIL;
228*4724848cSchristos }
229*4724848cSchristos
230*4724848cSchristos return EXT_RETURN_SENT;
231*4724848cSchristos }
232*4724848cSchristos #endif
233*4724848cSchristos
tls_construct_ctos_session_ticket(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)234*4724848cSchristos EXT_RETURN tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
235*4724848cSchristos unsigned int context, X509 *x,
236*4724848cSchristos size_t chainidx)
237*4724848cSchristos {
238*4724848cSchristos size_t ticklen;
239*4724848cSchristos
240*4724848cSchristos if (!tls_use_ticket(s))
241*4724848cSchristos return EXT_RETURN_NOT_SENT;
242*4724848cSchristos
243*4724848cSchristos if (!s->new_session && s->session != NULL
244*4724848cSchristos && s->session->ext.tick != NULL
245*4724848cSchristos && s->session->ssl_version != TLS1_3_VERSION) {
246*4724848cSchristos ticklen = s->session->ext.ticklen;
247*4724848cSchristos } else if (s->session && s->ext.session_ticket != NULL
248*4724848cSchristos && s->ext.session_ticket->data != NULL) {
249*4724848cSchristos ticklen = s->ext.session_ticket->length;
250*4724848cSchristos s->session->ext.tick = OPENSSL_malloc(ticklen);
251*4724848cSchristos if (s->session->ext.tick == NULL) {
252*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
253*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET,
254*4724848cSchristos ERR_R_INTERNAL_ERROR);
255*4724848cSchristos return EXT_RETURN_FAIL;
256*4724848cSchristos }
257*4724848cSchristos memcpy(s->session->ext.tick,
258*4724848cSchristos s->ext.session_ticket->data, ticklen);
259*4724848cSchristos s->session->ext.ticklen = ticklen;
260*4724848cSchristos } else {
261*4724848cSchristos ticklen = 0;
262*4724848cSchristos }
263*4724848cSchristos
264*4724848cSchristos if (ticklen == 0 && s->ext.session_ticket != NULL &&
265*4724848cSchristos s->ext.session_ticket->data == NULL)
266*4724848cSchristos return EXT_RETURN_NOT_SENT;
267*4724848cSchristos
268*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
269*4724848cSchristos || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
270*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
271*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
272*4724848cSchristos return EXT_RETURN_FAIL;
273*4724848cSchristos }
274*4724848cSchristos
275*4724848cSchristos return EXT_RETURN_SENT;
276*4724848cSchristos }
277*4724848cSchristos
tls_construct_ctos_sig_algs(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)278*4724848cSchristos EXT_RETURN tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt,
279*4724848cSchristos unsigned int context, X509 *x,
280*4724848cSchristos size_t chainidx)
281*4724848cSchristos {
282*4724848cSchristos size_t salglen;
283*4724848cSchristos const uint16_t *salg;
284*4724848cSchristos
285*4724848cSchristos if (!SSL_CLIENT_USE_SIGALGS(s))
286*4724848cSchristos return EXT_RETURN_NOT_SENT;
287*4724848cSchristos
288*4724848cSchristos salglen = tls12_get_psigalgs(s, 1, &salg);
289*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
290*4724848cSchristos /* Sub-packet for sig-algs extension */
291*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
292*4724848cSchristos /* Sub-packet for the actual list */
293*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
294*4724848cSchristos || !tls12_copy_sigalgs(s, pkt, salg, salglen)
295*4724848cSchristos || !WPACKET_close(pkt)
296*4724848cSchristos || !WPACKET_close(pkt)) {
297*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS,
298*4724848cSchristos ERR_R_INTERNAL_ERROR);
299*4724848cSchristos return EXT_RETURN_FAIL;
300*4724848cSchristos }
301*4724848cSchristos
302*4724848cSchristos return EXT_RETURN_SENT;
303*4724848cSchristos }
304*4724848cSchristos
305*4724848cSchristos #ifndef OPENSSL_NO_OCSP
tls_construct_ctos_status_request(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)306*4724848cSchristos EXT_RETURN tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
307*4724848cSchristos unsigned int context, X509 *x,
308*4724848cSchristos size_t chainidx)
309*4724848cSchristos {
310*4724848cSchristos int i;
311*4724848cSchristos
312*4724848cSchristos /* This extension isn't defined for client Certificates */
313*4724848cSchristos if (x != NULL)
314*4724848cSchristos return EXT_RETURN_NOT_SENT;
315*4724848cSchristos
316*4724848cSchristos if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
317*4724848cSchristos return EXT_RETURN_NOT_SENT;
318*4724848cSchristos
319*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
320*4724848cSchristos /* Sub-packet for status request extension */
321*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
322*4724848cSchristos || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
323*4724848cSchristos /* Sub-packet for the ids */
324*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)) {
325*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
326*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
327*4724848cSchristos return EXT_RETURN_FAIL;
328*4724848cSchristos }
329*4724848cSchristos for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
330*4724848cSchristos unsigned char *idbytes;
331*4724848cSchristos OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
332*4724848cSchristos int idlen = i2d_OCSP_RESPID(id, NULL);
333*4724848cSchristos
334*4724848cSchristos if (idlen <= 0
335*4724848cSchristos /* Sub-packet for an individual id */
336*4724848cSchristos || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
337*4724848cSchristos || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
338*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
339*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
340*4724848cSchristos ERR_R_INTERNAL_ERROR);
341*4724848cSchristos return EXT_RETURN_FAIL;
342*4724848cSchristos }
343*4724848cSchristos }
344*4724848cSchristos if (!WPACKET_close(pkt)
345*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)) {
346*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
347*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
348*4724848cSchristos return EXT_RETURN_FAIL;
349*4724848cSchristos }
350*4724848cSchristos if (s->ext.ocsp.exts) {
351*4724848cSchristos unsigned char *extbytes;
352*4724848cSchristos int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
353*4724848cSchristos
354*4724848cSchristos if (extlen < 0) {
355*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
356*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
357*4724848cSchristos ERR_R_INTERNAL_ERROR);
358*4724848cSchristos return EXT_RETURN_FAIL;
359*4724848cSchristos }
360*4724848cSchristos if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
361*4724848cSchristos || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
362*4724848cSchristos != extlen) {
363*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
364*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
365*4724848cSchristos ERR_R_INTERNAL_ERROR);
366*4724848cSchristos return EXT_RETURN_FAIL;
367*4724848cSchristos }
368*4724848cSchristos }
369*4724848cSchristos if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
370*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
371*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
372*4724848cSchristos return EXT_RETURN_FAIL;
373*4724848cSchristos }
374*4724848cSchristos
375*4724848cSchristos return EXT_RETURN_SENT;
376*4724848cSchristos }
377*4724848cSchristos #endif
378*4724848cSchristos
379*4724848cSchristos #ifndef OPENSSL_NO_NEXTPROTONEG
tls_construct_ctos_npn(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)380*4724848cSchristos EXT_RETURN tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context,
381*4724848cSchristos X509 *x, size_t chainidx)
382*4724848cSchristos {
383*4724848cSchristos if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
384*4724848cSchristos return EXT_RETURN_NOT_SENT;
385*4724848cSchristos
386*4724848cSchristos /*
387*4724848cSchristos * The client advertises an empty extension to indicate its support
388*4724848cSchristos * for Next Protocol Negotiation
389*4724848cSchristos */
390*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
391*4724848cSchristos || !WPACKET_put_bytes_u16(pkt, 0)) {
392*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_NPN,
393*4724848cSchristos ERR_R_INTERNAL_ERROR);
394*4724848cSchristos return EXT_RETURN_FAIL;
395*4724848cSchristos }
396*4724848cSchristos
397*4724848cSchristos return EXT_RETURN_SENT;
398*4724848cSchristos }
399*4724848cSchristos #endif
400*4724848cSchristos
tls_construct_ctos_alpn(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)401*4724848cSchristos EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context,
402*4724848cSchristos X509 *x, size_t chainidx)
403*4724848cSchristos {
404*4724848cSchristos s->s3->alpn_sent = 0;
405*4724848cSchristos
406*4724848cSchristos if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
407*4724848cSchristos return EXT_RETURN_NOT_SENT;
408*4724848cSchristos
409*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt,
410*4724848cSchristos TLSEXT_TYPE_application_layer_protocol_negotiation)
411*4724848cSchristos /* Sub-packet ALPN extension */
412*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
413*4724848cSchristos || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
414*4724848cSchristos || !WPACKET_close(pkt)) {
415*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_ALPN,
416*4724848cSchristos ERR_R_INTERNAL_ERROR);
417*4724848cSchristos return EXT_RETURN_FAIL;
418*4724848cSchristos }
419*4724848cSchristos s->s3->alpn_sent = 1;
420*4724848cSchristos
421*4724848cSchristos return EXT_RETURN_SENT;
422*4724848cSchristos }
423*4724848cSchristos
424*4724848cSchristos
425*4724848cSchristos #ifndef OPENSSL_NO_SRTP
tls_construct_ctos_use_srtp(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)426*4724848cSchristos EXT_RETURN tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt,
427*4724848cSchristos unsigned int context, X509 *x,
428*4724848cSchristos size_t chainidx)
429*4724848cSchristos {
430*4724848cSchristos STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
431*4724848cSchristos int i, end;
432*4724848cSchristos
433*4724848cSchristos if (clnt == NULL)
434*4724848cSchristos return EXT_RETURN_NOT_SENT;
435*4724848cSchristos
436*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
437*4724848cSchristos /* Sub-packet for SRTP extension */
438*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
439*4724848cSchristos /* Sub-packet for the protection profile list */
440*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)) {
441*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP,
442*4724848cSchristos ERR_R_INTERNAL_ERROR);
443*4724848cSchristos return EXT_RETURN_FAIL;
444*4724848cSchristos }
445*4724848cSchristos
446*4724848cSchristos end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
447*4724848cSchristos for (i = 0; i < end; i++) {
448*4724848cSchristos const SRTP_PROTECTION_PROFILE *prof =
449*4724848cSchristos sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
450*4724848cSchristos
451*4724848cSchristos if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
452*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
453*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
454*4724848cSchristos return EXT_RETURN_FAIL;
455*4724848cSchristos }
456*4724848cSchristos }
457*4724848cSchristos if (!WPACKET_close(pkt)
458*4724848cSchristos /* Add an empty use_mki value */
459*4724848cSchristos || !WPACKET_put_bytes_u8(pkt, 0)
460*4724848cSchristos || !WPACKET_close(pkt)) {
461*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP,
462*4724848cSchristos ERR_R_INTERNAL_ERROR);
463*4724848cSchristos return EXT_RETURN_FAIL;
464*4724848cSchristos }
465*4724848cSchristos
466*4724848cSchristos return EXT_RETURN_SENT;
467*4724848cSchristos }
468*4724848cSchristos #endif
469*4724848cSchristos
tls_construct_ctos_etm(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)470*4724848cSchristos EXT_RETURN tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context,
471*4724848cSchristos X509 *x, size_t chainidx)
472*4724848cSchristos {
473*4724848cSchristos if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
474*4724848cSchristos return EXT_RETURN_NOT_SENT;
475*4724848cSchristos
476*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
477*4724848cSchristos || !WPACKET_put_bytes_u16(pkt, 0)) {
478*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_ETM,
479*4724848cSchristos ERR_R_INTERNAL_ERROR);
480*4724848cSchristos return EXT_RETURN_FAIL;
481*4724848cSchristos }
482*4724848cSchristos
483*4724848cSchristos return EXT_RETURN_SENT;
484*4724848cSchristos }
485*4724848cSchristos
486*4724848cSchristos #ifndef OPENSSL_NO_CT
tls_construct_ctos_sct(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)487*4724848cSchristos EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context,
488*4724848cSchristos X509 *x, size_t chainidx)
489*4724848cSchristos {
490*4724848cSchristos if (s->ct_validation_callback == NULL)
491*4724848cSchristos return EXT_RETURN_NOT_SENT;
492*4724848cSchristos
493*4724848cSchristos /* Not defined for client Certificates */
494*4724848cSchristos if (x != NULL)
495*4724848cSchristos return EXT_RETURN_NOT_SENT;
496*4724848cSchristos
497*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
498*4724848cSchristos || !WPACKET_put_bytes_u16(pkt, 0)) {
499*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SCT,
500*4724848cSchristos ERR_R_INTERNAL_ERROR);
501*4724848cSchristos return EXT_RETURN_FAIL;
502*4724848cSchristos }
503*4724848cSchristos
504*4724848cSchristos return EXT_RETURN_SENT;
505*4724848cSchristos }
506*4724848cSchristos #endif
507*4724848cSchristos
tls_construct_ctos_ems(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)508*4724848cSchristos EXT_RETURN tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context,
509*4724848cSchristos X509 *x, size_t chainidx)
510*4724848cSchristos {
511*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
512*4724848cSchristos || !WPACKET_put_bytes_u16(pkt, 0)) {
513*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EMS,
514*4724848cSchristos ERR_R_INTERNAL_ERROR);
515*4724848cSchristos return EXT_RETURN_FAIL;
516*4724848cSchristos }
517*4724848cSchristos
518*4724848cSchristos return EXT_RETURN_SENT;
519*4724848cSchristos }
520*4724848cSchristos
tls_construct_ctos_supported_versions(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)521*4724848cSchristos EXT_RETURN tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
522*4724848cSchristos unsigned int context, X509 *x,
523*4724848cSchristos size_t chainidx)
524*4724848cSchristos {
525*4724848cSchristos int currv, min_version, max_version, reason;
526*4724848cSchristos
527*4724848cSchristos reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
528*4724848cSchristos if (reason != 0) {
529*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
530*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS, reason);
531*4724848cSchristos return EXT_RETURN_FAIL;
532*4724848cSchristos }
533*4724848cSchristos
534*4724848cSchristos /*
535*4724848cSchristos * Don't include this if we can't negotiate TLSv1.3. We can do a straight
536*4724848cSchristos * comparison here because we will never be called in DTLS.
537*4724848cSchristos */
538*4724848cSchristos if (max_version < TLS1_3_VERSION)
539*4724848cSchristos return EXT_RETURN_NOT_SENT;
540*4724848cSchristos
541*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
542*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
543*4724848cSchristos || !WPACKET_start_sub_packet_u8(pkt)) {
544*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
545*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
546*4724848cSchristos ERR_R_INTERNAL_ERROR);
547*4724848cSchristos return EXT_RETURN_FAIL;
548*4724848cSchristos }
549*4724848cSchristos
550*4724848cSchristos for (currv = max_version; currv >= min_version; currv--) {
551*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, currv)) {
552*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
553*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
554*4724848cSchristos ERR_R_INTERNAL_ERROR);
555*4724848cSchristos return EXT_RETURN_FAIL;
556*4724848cSchristos }
557*4724848cSchristos }
558*4724848cSchristos if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
559*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
560*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
561*4724848cSchristos ERR_R_INTERNAL_ERROR);
562*4724848cSchristos return EXT_RETURN_FAIL;
563*4724848cSchristos }
564*4724848cSchristos
565*4724848cSchristos return EXT_RETURN_SENT;
566*4724848cSchristos }
567*4724848cSchristos
568*4724848cSchristos /*
569*4724848cSchristos * Construct a psk_kex_modes extension.
570*4724848cSchristos */
tls_construct_ctos_psk_kex_modes(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)571*4724848cSchristos EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt,
572*4724848cSchristos unsigned int context, X509 *x,
573*4724848cSchristos size_t chainidx)
574*4724848cSchristos {
575*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
576*4724848cSchristos int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
577*4724848cSchristos
578*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
579*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
580*4724848cSchristos || !WPACKET_start_sub_packet_u8(pkt)
581*4724848cSchristos || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
582*4724848cSchristos || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
583*4724848cSchristos || !WPACKET_close(pkt)
584*4724848cSchristos || !WPACKET_close(pkt)) {
585*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
586*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES, ERR_R_INTERNAL_ERROR);
587*4724848cSchristos return EXT_RETURN_FAIL;
588*4724848cSchristos }
589*4724848cSchristos
590*4724848cSchristos s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
591*4724848cSchristos if (nodhe)
592*4724848cSchristos s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
593*4724848cSchristos #endif
594*4724848cSchristos
595*4724848cSchristos return EXT_RETURN_SENT;
596*4724848cSchristos }
597*4724848cSchristos
598*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
add_key_share(SSL * s,WPACKET * pkt,unsigned int curve_id)599*4724848cSchristos static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id)
600*4724848cSchristos {
601*4724848cSchristos unsigned char *encoded_point = NULL;
602*4724848cSchristos EVP_PKEY *key_share_key = NULL;
603*4724848cSchristos size_t encodedlen;
604*4724848cSchristos
605*4724848cSchristos if (s->s3->tmp.pkey != NULL) {
606*4724848cSchristos if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
607*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
608*4724848cSchristos ERR_R_INTERNAL_ERROR);
609*4724848cSchristos return 0;
610*4724848cSchristos }
611*4724848cSchristos /*
612*4724848cSchristos * Could happen if we got an HRR that wasn't requesting a new key_share
613*4724848cSchristos */
614*4724848cSchristos key_share_key = s->s3->tmp.pkey;
615*4724848cSchristos } else {
616*4724848cSchristos key_share_key = ssl_generate_pkey_group(s, curve_id);
617*4724848cSchristos if (key_share_key == NULL) {
618*4724848cSchristos /* SSLfatal() already called */
619*4724848cSchristos return 0;
620*4724848cSchristos }
621*4724848cSchristos }
622*4724848cSchristos
623*4724848cSchristos /* Encode the public key. */
624*4724848cSchristos encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
625*4724848cSchristos &encoded_point);
626*4724848cSchristos if (encodedlen == 0) {
627*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE, ERR_R_EC_LIB);
628*4724848cSchristos goto err;
629*4724848cSchristos }
630*4724848cSchristos
631*4724848cSchristos /* Create KeyShareEntry */
632*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, curve_id)
633*4724848cSchristos || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
634*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
635*4724848cSchristos ERR_R_INTERNAL_ERROR);
636*4724848cSchristos goto err;
637*4724848cSchristos }
638*4724848cSchristos
639*4724848cSchristos /*
640*4724848cSchristos * TODO(TLS1.3): When changing to send more than one key_share we're
641*4724848cSchristos * going to need to be able to save more than one EVP_PKEY. For now
642*4724848cSchristos * we reuse the existing tmp.pkey
643*4724848cSchristos */
644*4724848cSchristos s->s3->tmp.pkey = key_share_key;
645*4724848cSchristos s->s3->group_id = curve_id;
646*4724848cSchristos OPENSSL_free(encoded_point);
647*4724848cSchristos
648*4724848cSchristos return 1;
649*4724848cSchristos err:
650*4724848cSchristos if (s->s3->tmp.pkey == NULL)
651*4724848cSchristos EVP_PKEY_free(key_share_key);
652*4724848cSchristos OPENSSL_free(encoded_point);
653*4724848cSchristos return 0;
654*4724848cSchristos }
655*4724848cSchristos #endif
656*4724848cSchristos
tls_construct_ctos_key_share(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)657*4724848cSchristos EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt,
658*4724848cSchristos unsigned int context, X509 *x,
659*4724848cSchristos size_t chainidx)
660*4724848cSchristos {
661*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
662*4724848cSchristos size_t i, num_groups = 0;
663*4724848cSchristos const uint16_t *pgroups = NULL;
664*4724848cSchristos uint16_t curve_id = 0;
665*4724848cSchristos
666*4724848cSchristos /* key_share extension */
667*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
668*4724848cSchristos /* Extension data sub-packet */
669*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
670*4724848cSchristos /* KeyShare list sub-packet */
671*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)) {
672*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
673*4724848cSchristos ERR_R_INTERNAL_ERROR);
674*4724848cSchristos return EXT_RETURN_FAIL;
675*4724848cSchristos }
676*4724848cSchristos
677*4724848cSchristos tls1_get_supported_groups(s, &pgroups, &num_groups);
678*4724848cSchristos
679*4724848cSchristos /*
680*4724848cSchristos * TODO(TLS1.3): Make the number of key_shares sent configurable. For
681*4724848cSchristos * now, just send one
682*4724848cSchristos */
683*4724848cSchristos if (s->s3->group_id != 0) {
684*4724848cSchristos curve_id = s->s3->group_id;
685*4724848cSchristos } else {
686*4724848cSchristos for (i = 0; i < num_groups; i++) {
687*4724848cSchristos
688*4724848cSchristos if (!tls_curve_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
689*4724848cSchristos continue;
690*4724848cSchristos
691*4724848cSchristos curve_id = pgroups[i];
692*4724848cSchristos break;
693*4724848cSchristos }
694*4724848cSchristos }
695*4724848cSchristos
696*4724848cSchristos if (curve_id == 0) {
697*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
698*4724848cSchristos SSL_R_NO_SUITABLE_KEY_SHARE);
699*4724848cSchristos return EXT_RETURN_FAIL;
700*4724848cSchristos }
701*4724848cSchristos
702*4724848cSchristos if (!add_key_share(s, pkt, curve_id)) {
703*4724848cSchristos /* SSLfatal() already called */
704*4724848cSchristos return EXT_RETURN_FAIL;
705*4724848cSchristos }
706*4724848cSchristos
707*4724848cSchristos if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
708*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
709*4724848cSchristos ERR_R_INTERNAL_ERROR);
710*4724848cSchristos return EXT_RETURN_FAIL;
711*4724848cSchristos }
712*4724848cSchristos return EXT_RETURN_SENT;
713*4724848cSchristos #else
714*4724848cSchristos return EXT_RETURN_NOT_SENT;
715*4724848cSchristos #endif
716*4724848cSchristos }
717*4724848cSchristos
tls_construct_ctos_cookie(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)718*4724848cSchristos EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
719*4724848cSchristos X509 *x, size_t chainidx)
720*4724848cSchristos {
721*4724848cSchristos EXT_RETURN ret = EXT_RETURN_FAIL;
722*4724848cSchristos
723*4724848cSchristos /* Should only be set if we've had an HRR */
724*4724848cSchristos if (s->ext.tls13_cookie_len == 0)
725*4724848cSchristos return EXT_RETURN_NOT_SENT;
726*4724848cSchristos
727*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
728*4724848cSchristos /* Extension data sub-packet */
729*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
730*4724848cSchristos || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
731*4724848cSchristos s->ext.tls13_cookie_len)
732*4724848cSchristos || !WPACKET_close(pkt)) {
733*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_COOKIE,
734*4724848cSchristos ERR_R_INTERNAL_ERROR);
735*4724848cSchristos goto end;
736*4724848cSchristos }
737*4724848cSchristos
738*4724848cSchristos ret = EXT_RETURN_SENT;
739*4724848cSchristos end:
740*4724848cSchristos OPENSSL_free(s->ext.tls13_cookie);
741*4724848cSchristos s->ext.tls13_cookie = NULL;
742*4724848cSchristos s->ext.tls13_cookie_len = 0;
743*4724848cSchristos
744*4724848cSchristos return ret;
745*4724848cSchristos }
746*4724848cSchristos
tls_construct_ctos_early_data(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)747*4724848cSchristos EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt,
748*4724848cSchristos unsigned int context, X509 *x,
749*4724848cSchristos size_t chainidx)
750*4724848cSchristos {
751*4724848cSchristos #ifndef OPENSSL_NO_PSK
752*4724848cSchristos char identity[PSK_MAX_IDENTITY_LEN + 1];
753*4724848cSchristos #endif /* OPENSSL_NO_PSK */
754*4724848cSchristos const unsigned char *id = NULL;
755*4724848cSchristos size_t idlen = 0;
756*4724848cSchristos SSL_SESSION *psksess = NULL;
757*4724848cSchristos SSL_SESSION *edsess = NULL;
758*4724848cSchristos const EVP_MD *handmd = NULL;
759*4724848cSchristos
760*4724848cSchristos if (s->hello_retry_request == SSL_HRR_PENDING)
761*4724848cSchristos handmd = ssl_handshake_md(s);
762*4724848cSchristos
763*4724848cSchristos if (s->psk_use_session_cb != NULL
764*4724848cSchristos && (!s->psk_use_session_cb(s, handmd, &id, &idlen, &psksess)
765*4724848cSchristos || (psksess != NULL
766*4724848cSchristos && psksess->ssl_version != TLS1_3_VERSION))) {
767*4724848cSchristos SSL_SESSION_free(psksess);
768*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
769*4724848cSchristos SSL_R_BAD_PSK);
770*4724848cSchristos return EXT_RETURN_FAIL;
771*4724848cSchristos }
772*4724848cSchristos
773*4724848cSchristos #ifndef OPENSSL_NO_PSK
774*4724848cSchristos if (psksess == NULL && s->psk_client_callback != NULL) {
775*4724848cSchristos unsigned char psk[PSK_MAX_PSK_LEN];
776*4724848cSchristos size_t psklen = 0;
777*4724848cSchristos
778*4724848cSchristos memset(identity, 0, sizeof(identity));
779*4724848cSchristos psklen = s->psk_client_callback(s, NULL, identity, sizeof(identity) - 1,
780*4724848cSchristos psk, sizeof(psk));
781*4724848cSchristos
782*4724848cSchristos if (psklen > PSK_MAX_PSK_LEN) {
783*4724848cSchristos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
784*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
785*4724848cSchristos return EXT_RETURN_FAIL;
786*4724848cSchristos } else if (psklen > 0) {
787*4724848cSchristos const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
788*4724848cSchristos const SSL_CIPHER *cipher;
789*4724848cSchristos
790*4724848cSchristos idlen = strlen(identity);
791*4724848cSchristos if (idlen > PSK_MAX_IDENTITY_LEN) {
792*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
793*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
794*4724848cSchristos ERR_R_INTERNAL_ERROR);
795*4724848cSchristos return EXT_RETURN_FAIL;
796*4724848cSchristos }
797*4724848cSchristos id = (unsigned char *)identity;
798*4724848cSchristos
799*4724848cSchristos /*
800*4724848cSchristos * We found a PSK using an old style callback. We don't know
801*4724848cSchristos * the digest so we default to SHA256 as per the TLSv1.3 spec
802*4724848cSchristos */
803*4724848cSchristos cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
804*4724848cSchristos if (cipher == NULL) {
805*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
806*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
807*4724848cSchristos ERR_R_INTERNAL_ERROR);
808*4724848cSchristos return EXT_RETURN_FAIL;
809*4724848cSchristos }
810*4724848cSchristos
811*4724848cSchristos psksess = SSL_SESSION_new();
812*4724848cSchristos if (psksess == NULL
813*4724848cSchristos || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
814*4724848cSchristos || !SSL_SESSION_set_cipher(psksess, cipher)
815*4724848cSchristos || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
816*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
817*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
818*4724848cSchristos ERR_R_INTERNAL_ERROR);
819*4724848cSchristos OPENSSL_cleanse(psk, psklen);
820*4724848cSchristos return EXT_RETURN_FAIL;
821*4724848cSchristos }
822*4724848cSchristos OPENSSL_cleanse(psk, psklen);
823*4724848cSchristos }
824*4724848cSchristos }
825*4724848cSchristos #endif /* OPENSSL_NO_PSK */
826*4724848cSchristos
827*4724848cSchristos SSL_SESSION_free(s->psksession);
828*4724848cSchristos s->psksession = psksess;
829*4724848cSchristos if (psksess != NULL) {
830*4724848cSchristos OPENSSL_free(s->psksession_id);
831*4724848cSchristos s->psksession_id = OPENSSL_memdup(id, idlen);
832*4724848cSchristos if (s->psksession_id == NULL) {
833*4724848cSchristos s->psksession_id_len = 0;
834*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
835*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
836*4724848cSchristos return EXT_RETURN_FAIL;
837*4724848cSchristos }
838*4724848cSchristos s->psksession_id_len = idlen;
839*4724848cSchristos }
840*4724848cSchristos
841*4724848cSchristos if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
842*4724848cSchristos || (s->session->ext.max_early_data == 0
843*4724848cSchristos && (psksess == NULL || psksess->ext.max_early_data == 0))) {
844*4724848cSchristos s->max_early_data = 0;
845*4724848cSchristos return EXT_RETURN_NOT_SENT;
846*4724848cSchristos }
847*4724848cSchristos edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
848*4724848cSchristos s->max_early_data = edsess->ext.max_early_data;
849*4724848cSchristos
850*4724848cSchristos if (edsess->ext.hostname != NULL) {
851*4724848cSchristos if (s->ext.hostname == NULL
852*4724848cSchristos || (s->ext.hostname != NULL
853*4724848cSchristos && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
854*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
855*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
856*4724848cSchristos SSL_R_INCONSISTENT_EARLY_DATA_SNI);
857*4724848cSchristos return EXT_RETURN_FAIL;
858*4724848cSchristos }
859*4724848cSchristos }
860*4724848cSchristos
861*4724848cSchristos if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
862*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
863*4724848cSchristos SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
864*4724848cSchristos return EXT_RETURN_FAIL;
865*4724848cSchristos }
866*4724848cSchristos
867*4724848cSchristos /*
868*4724848cSchristos * Verify that we are offering an ALPN protocol consistent with the early
869*4724848cSchristos * data.
870*4724848cSchristos */
871*4724848cSchristos if (edsess->ext.alpn_selected != NULL) {
872*4724848cSchristos PACKET prots, alpnpkt;
873*4724848cSchristos int found = 0;
874*4724848cSchristos
875*4724848cSchristos if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
876*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
877*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
878*4724848cSchristos return EXT_RETURN_FAIL;
879*4724848cSchristos }
880*4724848cSchristos while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
881*4724848cSchristos if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
882*4724848cSchristos edsess->ext.alpn_selected_len)) {
883*4724848cSchristos found = 1;
884*4724848cSchristos break;
885*4724848cSchristos }
886*4724848cSchristos }
887*4724848cSchristos if (!found) {
888*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
889*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
890*4724848cSchristos SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
891*4724848cSchristos return EXT_RETURN_FAIL;
892*4724848cSchristos }
893*4724848cSchristos }
894*4724848cSchristos
895*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
896*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
897*4724848cSchristos || !WPACKET_close(pkt)) {
898*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
899*4724848cSchristos ERR_R_INTERNAL_ERROR);
900*4724848cSchristos return EXT_RETURN_FAIL;
901*4724848cSchristos }
902*4724848cSchristos
903*4724848cSchristos /*
904*4724848cSchristos * We set this to rejected here. Later, if the server acknowledges the
905*4724848cSchristos * extension, we set it to accepted.
906*4724848cSchristos */
907*4724848cSchristos s->ext.early_data = SSL_EARLY_DATA_REJECTED;
908*4724848cSchristos s->ext.early_data_ok = 1;
909*4724848cSchristos
910*4724848cSchristos return EXT_RETURN_SENT;
911*4724848cSchristos }
912*4724848cSchristos
913*4724848cSchristos #define F5_WORKAROUND_MIN_MSG_LEN 0xff
914*4724848cSchristos #define F5_WORKAROUND_MAX_MSG_LEN 0x200
915*4724848cSchristos
916*4724848cSchristos /*
917*4724848cSchristos * PSK pre binder overhead =
918*4724848cSchristos * 2 bytes for TLSEXT_TYPE_psk
919*4724848cSchristos * 2 bytes for extension length
920*4724848cSchristos * 2 bytes for identities list length
921*4724848cSchristos * 2 bytes for identity length
922*4724848cSchristos * 4 bytes for obfuscated_ticket_age
923*4724848cSchristos * 2 bytes for binder list length
924*4724848cSchristos * 1 byte for binder length
925*4724848cSchristos * The above excludes the number of bytes for the identity itself and the
926*4724848cSchristos * subsequent binder bytes
927*4724848cSchristos */
928*4724848cSchristos #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
929*4724848cSchristos
tls_construct_ctos_padding(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)930*4724848cSchristos EXT_RETURN tls_construct_ctos_padding(SSL *s, WPACKET *pkt,
931*4724848cSchristos unsigned int context, X509 *x,
932*4724848cSchristos size_t chainidx)
933*4724848cSchristos {
934*4724848cSchristos unsigned char *padbytes;
935*4724848cSchristos size_t hlen;
936*4724848cSchristos
937*4724848cSchristos if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
938*4724848cSchristos return EXT_RETURN_NOT_SENT;
939*4724848cSchristos
940*4724848cSchristos /*
941*4724848cSchristos * Add padding to workaround bugs in F5 terminators. See RFC7685.
942*4724848cSchristos * This code calculates the length of all extensions added so far but
943*4724848cSchristos * excludes the PSK extension (because that MUST be written last). Therefore
944*4724848cSchristos * this extension MUST always appear second to last.
945*4724848cSchristos */
946*4724848cSchristos if (!WPACKET_get_total_written(pkt, &hlen)) {
947*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PADDING,
948*4724848cSchristos ERR_R_INTERNAL_ERROR);
949*4724848cSchristos return EXT_RETURN_FAIL;
950*4724848cSchristos }
951*4724848cSchristos
952*4724848cSchristos /*
953*4724848cSchristos * If we're going to send a PSK then that will be written out after this
954*4724848cSchristos * extension, so we need to calculate how long it is going to be.
955*4724848cSchristos */
956*4724848cSchristos if (s->session->ssl_version == TLS1_3_VERSION
957*4724848cSchristos && s->session->ext.ticklen != 0
958*4724848cSchristos && s->session->cipher != NULL) {
959*4724848cSchristos const EVP_MD *md = ssl_md(s->session->cipher->algorithm2);
960*4724848cSchristos
961*4724848cSchristos if (md != NULL) {
962*4724848cSchristos /*
963*4724848cSchristos * Add the fixed PSK overhead, the identity length and the binder
964*4724848cSchristos * length.
965*4724848cSchristos */
966*4724848cSchristos hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
967*4724848cSchristos + EVP_MD_size(md);
968*4724848cSchristos }
969*4724848cSchristos }
970*4724848cSchristos
971*4724848cSchristos if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
972*4724848cSchristos /* Calculate the amount of padding we need to add */
973*4724848cSchristos hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
974*4724848cSchristos
975*4724848cSchristos /*
976*4724848cSchristos * Take off the size of extension header itself (2 bytes for type and
977*4724848cSchristos * 2 bytes for length bytes), but ensure that the extension is at least
978*4724848cSchristos * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
979*4724848cSchristos * 8.x are intolerant of that condition)
980*4724848cSchristos */
981*4724848cSchristos if (hlen > 4)
982*4724848cSchristos hlen -= 4;
983*4724848cSchristos else
984*4724848cSchristos hlen = 1;
985*4724848cSchristos
986*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
987*4724848cSchristos || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
988*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PADDING,
989*4724848cSchristos ERR_R_INTERNAL_ERROR);
990*4724848cSchristos return EXT_RETURN_FAIL;
991*4724848cSchristos }
992*4724848cSchristos memset(padbytes, 0, hlen);
993*4724848cSchristos }
994*4724848cSchristos
995*4724848cSchristos return EXT_RETURN_SENT;
996*4724848cSchristos }
997*4724848cSchristos
998*4724848cSchristos /*
999*4724848cSchristos * Construct the pre_shared_key extension
1000*4724848cSchristos */
tls_construct_ctos_psk(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1001*4724848cSchristos EXT_RETURN tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context,
1002*4724848cSchristos X509 *x, size_t chainidx)
1003*4724848cSchristos {
1004*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
1005*4724848cSchristos uint32_t agesec, agems = 0;
1006*4724848cSchristos size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen;
1007*4724848cSchristos unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
1008*4724848cSchristos const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
1009*4724848cSchristos int dores = 0;
1010*4724848cSchristos
1011*4724848cSchristos s->ext.tick_identity = 0;
1012*4724848cSchristos
1013*4724848cSchristos /*
1014*4724848cSchristos * Note: At this stage of the code we only support adding a single
1015*4724848cSchristos * resumption PSK. If we add support for multiple PSKs then the length
1016*4724848cSchristos * calculations in the padding extension will need to be adjusted.
1017*4724848cSchristos */
1018*4724848cSchristos
1019*4724848cSchristos /*
1020*4724848cSchristos * If this is an incompatible or new session then we have nothing to resume
1021*4724848cSchristos * so don't add this extension.
1022*4724848cSchristos */
1023*4724848cSchristos if (s->session->ssl_version != TLS1_3_VERSION
1024*4724848cSchristos || (s->session->ext.ticklen == 0 && s->psksession == NULL))
1025*4724848cSchristos return EXT_RETURN_NOT_SENT;
1026*4724848cSchristos
1027*4724848cSchristos if (s->hello_retry_request == SSL_HRR_PENDING)
1028*4724848cSchristos handmd = ssl_handshake_md(s);
1029*4724848cSchristos
1030*4724848cSchristos if (s->session->ext.ticklen != 0) {
1031*4724848cSchristos /* Get the digest associated with the ciphersuite in the session */
1032*4724848cSchristos if (s->session->cipher == NULL) {
1033*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1034*4724848cSchristos ERR_R_INTERNAL_ERROR);
1035*4724848cSchristos return EXT_RETURN_FAIL;
1036*4724848cSchristos }
1037*4724848cSchristos mdres = ssl_md(s->session->cipher->algorithm2);
1038*4724848cSchristos if (mdres == NULL) {
1039*4724848cSchristos /*
1040*4724848cSchristos * Don't recognize this cipher so we can't use the session.
1041*4724848cSchristos * Ignore it
1042*4724848cSchristos */
1043*4724848cSchristos goto dopsksess;
1044*4724848cSchristos }
1045*4724848cSchristos
1046*4724848cSchristos if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
1047*4724848cSchristos /*
1048*4724848cSchristos * Selected ciphersuite hash does not match the hash for the session
1049*4724848cSchristos * so we can't use it.
1050*4724848cSchristos */
1051*4724848cSchristos goto dopsksess;
1052*4724848cSchristos }
1053*4724848cSchristos
1054*4724848cSchristos /*
1055*4724848cSchristos * Technically the C standard just says time() returns a time_t and says
1056*4724848cSchristos * nothing about the encoding of that type. In practice most
1057*4724848cSchristos * implementations follow POSIX which holds it as an integral type in
1058*4724848cSchristos * seconds since epoch. We've already made the assumption that we can do
1059*4724848cSchristos * this in multiple places in the code, so portability shouldn't be an
1060*4724848cSchristos * issue.
1061*4724848cSchristos */
1062*4724848cSchristos agesec = (uint32_t)(time(NULL) - s->session->time);
1063*4724848cSchristos /*
1064*4724848cSchristos * We calculate the age in seconds but the server may work in ms. Due to
1065*4724848cSchristos * rounding errors we could overestimate the age by up to 1s. It is
1066*4724848cSchristos * better to underestimate it. Otherwise, if the RTT is very short, when
1067*4724848cSchristos * the server calculates the age reported by the client it could be
1068*4724848cSchristos * bigger than the age calculated on the server - which should never
1069*4724848cSchristos * happen.
1070*4724848cSchristos */
1071*4724848cSchristos if (agesec > 0)
1072*4724848cSchristos agesec--;
1073*4724848cSchristos
1074*4724848cSchristos if (s->session->ext.tick_lifetime_hint < agesec) {
1075*4724848cSchristos /* Ticket is too old. Ignore it. */
1076*4724848cSchristos goto dopsksess;
1077*4724848cSchristos }
1078*4724848cSchristos
1079*4724848cSchristos /*
1080*4724848cSchristos * Calculate age in ms. We're just doing it to nearest second. Should be
1081*4724848cSchristos * good enough.
1082*4724848cSchristos */
1083*4724848cSchristos agems = agesec * (uint32_t)1000;
1084*4724848cSchristos
1085*4724848cSchristos if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
1086*4724848cSchristos /*
1087*4724848cSchristos * Overflow. Shouldn't happen unless this is a *really* old session.
1088*4724848cSchristos * If so we just ignore it.
1089*4724848cSchristos */
1090*4724848cSchristos goto dopsksess;
1091*4724848cSchristos }
1092*4724848cSchristos
1093*4724848cSchristos /*
1094*4724848cSchristos * Obfuscate the age. Overflow here is fine, this addition is supposed
1095*4724848cSchristos * to be mod 2^32.
1096*4724848cSchristos */
1097*4724848cSchristos agems += s->session->ext.tick_age_add;
1098*4724848cSchristos
1099*4724848cSchristos reshashsize = EVP_MD_size(mdres);
1100*4724848cSchristos s->ext.tick_identity++;
1101*4724848cSchristos dores = 1;
1102*4724848cSchristos }
1103*4724848cSchristos
1104*4724848cSchristos dopsksess:
1105*4724848cSchristos if (!dores && s->psksession == NULL)
1106*4724848cSchristos return EXT_RETURN_NOT_SENT;
1107*4724848cSchristos
1108*4724848cSchristos if (s->psksession != NULL) {
1109*4724848cSchristos mdpsk = ssl_md(s->psksession->cipher->algorithm2);
1110*4724848cSchristos if (mdpsk == NULL) {
1111*4724848cSchristos /*
1112*4724848cSchristos * Don't recognize this cipher so we can't use the session.
1113*4724848cSchristos * If this happens it's an application bug.
1114*4724848cSchristos */
1115*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1116*4724848cSchristos SSL_R_BAD_PSK);
1117*4724848cSchristos return EXT_RETURN_FAIL;
1118*4724848cSchristos }
1119*4724848cSchristos
1120*4724848cSchristos if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
1121*4724848cSchristos /*
1122*4724848cSchristos * Selected ciphersuite hash does not match the hash for the PSK
1123*4724848cSchristos * session. This is an application bug.
1124*4724848cSchristos */
1125*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1126*4724848cSchristos SSL_R_BAD_PSK);
1127*4724848cSchristos return EXT_RETURN_FAIL;
1128*4724848cSchristos }
1129*4724848cSchristos
1130*4724848cSchristos pskhashsize = EVP_MD_size(mdpsk);
1131*4724848cSchristos }
1132*4724848cSchristos
1133*4724848cSchristos /* Create the extension, but skip over the binder for now */
1134*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1135*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
1136*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)) {
1137*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1138*4724848cSchristos ERR_R_INTERNAL_ERROR);
1139*4724848cSchristos return EXT_RETURN_FAIL;
1140*4724848cSchristos }
1141*4724848cSchristos
1142*4724848cSchristos if (dores) {
1143*4724848cSchristos if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
1144*4724848cSchristos s->session->ext.ticklen)
1145*4724848cSchristos || !WPACKET_put_bytes_u32(pkt, agems)) {
1146*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1147*4724848cSchristos ERR_R_INTERNAL_ERROR);
1148*4724848cSchristos return EXT_RETURN_FAIL;
1149*4724848cSchristos }
1150*4724848cSchristos }
1151*4724848cSchristos
1152*4724848cSchristos if (s->psksession != NULL) {
1153*4724848cSchristos if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
1154*4724848cSchristos s->psksession_id_len)
1155*4724848cSchristos || !WPACKET_put_bytes_u32(pkt, 0)) {
1156*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1157*4724848cSchristos ERR_R_INTERNAL_ERROR);
1158*4724848cSchristos return EXT_RETURN_FAIL;
1159*4724848cSchristos }
1160*4724848cSchristos s->ext.tick_identity++;
1161*4724848cSchristos }
1162*4724848cSchristos
1163*4724848cSchristos if (!WPACKET_close(pkt)
1164*4724848cSchristos || !WPACKET_get_total_written(pkt, &binderoffset)
1165*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
1166*4724848cSchristos || (dores
1167*4724848cSchristos && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
1168*4724848cSchristos || (s->psksession != NULL
1169*4724848cSchristos && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
1170*4724848cSchristos || !WPACKET_close(pkt)
1171*4724848cSchristos || !WPACKET_close(pkt)
1172*4724848cSchristos || !WPACKET_get_total_written(pkt, &msglen)
1173*4724848cSchristos /*
1174*4724848cSchristos * We need to fill in all the sub-packet lengths now so we can
1175*4724848cSchristos * calculate the HMAC of the message up to the binders
1176*4724848cSchristos */
1177*4724848cSchristos || !WPACKET_fill_lengths(pkt)) {
1178*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1179*4724848cSchristos ERR_R_INTERNAL_ERROR);
1180*4724848cSchristos return EXT_RETURN_FAIL;
1181*4724848cSchristos }
1182*4724848cSchristos
1183*4724848cSchristos msgstart = WPACKET_get_curr(pkt) - msglen;
1184*4724848cSchristos
1185*4724848cSchristos if (dores
1186*4724848cSchristos && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
1187*4724848cSchristos resbinder, s->session, 1, 0) != 1) {
1188*4724848cSchristos /* SSLfatal() already called */
1189*4724848cSchristos return EXT_RETURN_FAIL;
1190*4724848cSchristos }
1191*4724848cSchristos
1192*4724848cSchristos if (s->psksession != NULL
1193*4724848cSchristos && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
1194*4724848cSchristos pskbinder, s->psksession, 1, 1) != 1) {
1195*4724848cSchristos /* SSLfatal() already called */
1196*4724848cSchristos return EXT_RETURN_FAIL;
1197*4724848cSchristos }
1198*4724848cSchristos
1199*4724848cSchristos return EXT_RETURN_SENT;
1200*4724848cSchristos #else
1201*4724848cSchristos return EXT_RETURN_NOT_SENT;
1202*4724848cSchristos #endif
1203*4724848cSchristos }
1204*4724848cSchristos
tls_construct_ctos_post_handshake_auth(SSL * s,WPACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1205*4724848cSchristos EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL *s, WPACKET *pkt,
1206*4724848cSchristos unsigned int context,
1207*4724848cSchristos X509 *x, size_t chainidx)
1208*4724848cSchristos {
1209*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
1210*4724848cSchristos if (!s->pha_enabled)
1211*4724848cSchristos return EXT_RETURN_NOT_SENT;
1212*4724848cSchristos
1213*4724848cSchristos /* construct extension - 0 length, no contents */
1214*4724848cSchristos if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
1215*4724848cSchristos || !WPACKET_start_sub_packet_u16(pkt)
1216*4724848cSchristos || !WPACKET_close(pkt)) {
1217*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1218*4724848cSchristos SSL_F_TLS_CONSTRUCT_CTOS_POST_HANDSHAKE_AUTH,
1219*4724848cSchristos ERR_R_INTERNAL_ERROR);
1220*4724848cSchristos return EXT_RETURN_FAIL;
1221*4724848cSchristos }
1222*4724848cSchristos
1223*4724848cSchristos s->post_handshake_auth = SSL_PHA_EXT_SENT;
1224*4724848cSchristos
1225*4724848cSchristos return EXT_RETURN_SENT;
1226*4724848cSchristos #else
1227*4724848cSchristos return EXT_RETURN_NOT_SENT;
1228*4724848cSchristos #endif
1229*4724848cSchristos }
1230*4724848cSchristos
1231*4724848cSchristos
1232*4724848cSchristos /*
1233*4724848cSchristos * Parse the server's renegotiation binding and abort if it's not right
1234*4724848cSchristos */
tls_parse_stoc_renegotiate(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1235*4724848cSchristos int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
1236*4724848cSchristos X509 *x, size_t chainidx)
1237*4724848cSchristos {
1238*4724848cSchristos size_t expected_len = s->s3->previous_client_finished_len
1239*4724848cSchristos + s->s3->previous_server_finished_len;
1240*4724848cSchristos size_t ilen;
1241*4724848cSchristos const unsigned char *data;
1242*4724848cSchristos
1243*4724848cSchristos /* Check for logic errors */
1244*4724848cSchristos if (!ossl_assert(expected_len == 0
1245*4724848cSchristos || s->s3->previous_client_finished_len != 0)
1246*4724848cSchristos || !ossl_assert(expected_len == 0
1247*4724848cSchristos || s->s3->previous_server_finished_len != 0)) {
1248*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1249*4724848cSchristos ERR_R_INTERNAL_ERROR);
1250*4724848cSchristos return 0;
1251*4724848cSchristos }
1252*4724848cSchristos
1253*4724848cSchristos /* Parse the length byte */
1254*4724848cSchristos if (!PACKET_get_1_len(pkt, &ilen)) {
1255*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1256*4724848cSchristos SSL_R_RENEGOTIATION_ENCODING_ERR);
1257*4724848cSchristos return 0;
1258*4724848cSchristos }
1259*4724848cSchristos
1260*4724848cSchristos /* Consistency check */
1261*4724848cSchristos if (PACKET_remaining(pkt) != ilen) {
1262*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1263*4724848cSchristos SSL_R_RENEGOTIATION_ENCODING_ERR);
1264*4724848cSchristos return 0;
1265*4724848cSchristos }
1266*4724848cSchristos
1267*4724848cSchristos /* Check that the extension matches */
1268*4724848cSchristos if (ilen != expected_len) {
1269*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1270*4724848cSchristos SSL_R_RENEGOTIATION_MISMATCH);
1271*4724848cSchristos return 0;
1272*4724848cSchristos }
1273*4724848cSchristos
1274*4724848cSchristos if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
1275*4724848cSchristos || memcmp(data, s->s3->previous_client_finished,
1276*4724848cSchristos s->s3->previous_client_finished_len) != 0) {
1277*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1278*4724848cSchristos SSL_R_RENEGOTIATION_MISMATCH);
1279*4724848cSchristos return 0;
1280*4724848cSchristos }
1281*4724848cSchristos
1282*4724848cSchristos if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
1283*4724848cSchristos || memcmp(data, s->s3->previous_server_finished,
1284*4724848cSchristos s->s3->previous_server_finished_len) != 0) {
1285*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1286*4724848cSchristos SSL_R_RENEGOTIATION_MISMATCH);
1287*4724848cSchristos return 0;
1288*4724848cSchristos }
1289*4724848cSchristos s->s3->send_connection_binding = 1;
1290*4724848cSchristos
1291*4724848cSchristos return 1;
1292*4724848cSchristos }
1293*4724848cSchristos
1294*4724848cSchristos /* Parse the server's max fragment len extension packet */
tls_parse_stoc_maxfragmentlen(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1295*4724848cSchristos int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
1296*4724848cSchristos X509 *x, size_t chainidx)
1297*4724848cSchristos {
1298*4724848cSchristos unsigned int value;
1299*4724848cSchristos
1300*4724848cSchristos if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
1301*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
1302*4724848cSchristos SSL_R_BAD_EXTENSION);
1303*4724848cSchristos return 0;
1304*4724848cSchristos }
1305*4724848cSchristos
1306*4724848cSchristos /* |value| should contains a valid max-fragment-length code. */
1307*4724848cSchristos if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
1308*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1309*4724848cSchristos SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
1310*4724848cSchristos SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1311*4724848cSchristos return 0;
1312*4724848cSchristos }
1313*4724848cSchristos
1314*4724848cSchristos /* Must be the same value as client-configured one who was sent to server */
1315*4724848cSchristos /*-
1316*4724848cSchristos * RFC 6066: if a client receives a maximum fragment length negotiation
1317*4724848cSchristos * response that differs from the length it requested, ...
1318*4724848cSchristos * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1319*4724848cSchristos */
1320*4724848cSchristos if (value != s->ext.max_fragment_len_mode) {
1321*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1322*4724848cSchristos SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
1323*4724848cSchristos SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1324*4724848cSchristos return 0;
1325*4724848cSchristos }
1326*4724848cSchristos
1327*4724848cSchristos /*
1328*4724848cSchristos * Maximum Fragment Length Negotiation succeeded.
1329*4724848cSchristos * The negotiated Maximum Fragment Length is binding now.
1330*4724848cSchristos */
1331*4724848cSchristos s->session->ext.max_fragment_len_mode = value;
1332*4724848cSchristos
1333*4724848cSchristos return 1;
1334*4724848cSchristos }
1335*4724848cSchristos
tls_parse_stoc_server_name(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1336*4724848cSchristos int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
1337*4724848cSchristos X509 *x, size_t chainidx)
1338*4724848cSchristos {
1339*4724848cSchristos if (s->ext.hostname == NULL) {
1340*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1341*4724848cSchristos ERR_R_INTERNAL_ERROR);
1342*4724848cSchristos return 0;
1343*4724848cSchristos }
1344*4724848cSchristos
1345*4724848cSchristos if (PACKET_remaining(pkt) > 0) {
1346*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1347*4724848cSchristos SSL_R_BAD_EXTENSION);
1348*4724848cSchristos return 0;
1349*4724848cSchristos }
1350*4724848cSchristos
1351*4724848cSchristos if (!s->hit) {
1352*4724848cSchristos if (s->session->ext.hostname != NULL) {
1353*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1354*4724848cSchristos ERR_R_INTERNAL_ERROR);
1355*4724848cSchristos return 0;
1356*4724848cSchristos }
1357*4724848cSchristos s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1358*4724848cSchristos if (s->session->ext.hostname == NULL) {
1359*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1360*4724848cSchristos ERR_R_INTERNAL_ERROR);
1361*4724848cSchristos return 0;
1362*4724848cSchristos }
1363*4724848cSchristos }
1364*4724848cSchristos
1365*4724848cSchristos return 1;
1366*4724848cSchristos }
1367*4724848cSchristos
1368*4724848cSchristos #ifndef OPENSSL_NO_EC
tls_parse_stoc_ec_pt_formats(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1369*4724848cSchristos int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
1370*4724848cSchristos X509 *x, size_t chainidx)
1371*4724848cSchristos {
1372*4724848cSchristos size_t ecpointformats_len;
1373*4724848cSchristos PACKET ecptformatlist;
1374*4724848cSchristos
1375*4724848cSchristos if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
1376*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS,
1377*4724848cSchristos SSL_R_BAD_EXTENSION);
1378*4724848cSchristos return 0;
1379*4724848cSchristos }
1380*4724848cSchristos if (!s->hit) {
1381*4724848cSchristos ecpointformats_len = PACKET_remaining(&ecptformatlist);
1382*4724848cSchristos if (ecpointformats_len == 0) {
1383*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR,
1384*4724848cSchristos SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, SSL_R_BAD_LENGTH);
1385*4724848cSchristos return 0;
1386*4724848cSchristos }
1387*4724848cSchristos
1388*4724848cSchristos s->ext.peer_ecpointformats_len = 0;
1389*4724848cSchristos OPENSSL_free(s->ext.peer_ecpointformats);
1390*4724848cSchristos s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);
1391*4724848cSchristos if (s->ext.peer_ecpointformats == NULL) {
1392*4724848cSchristos s->ext.peer_ecpointformats_len = 0;
1393*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1394*4724848cSchristos SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
1395*4724848cSchristos return 0;
1396*4724848cSchristos }
1397*4724848cSchristos
1398*4724848cSchristos s->ext.peer_ecpointformats_len = ecpointformats_len;
1399*4724848cSchristos
1400*4724848cSchristos if (!PACKET_copy_bytes(&ecptformatlist,
1401*4724848cSchristos s->ext.peer_ecpointformats,
1402*4724848cSchristos ecpointformats_len)) {
1403*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1404*4724848cSchristos SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
1405*4724848cSchristos return 0;
1406*4724848cSchristos }
1407*4724848cSchristos }
1408*4724848cSchristos
1409*4724848cSchristos return 1;
1410*4724848cSchristos }
1411*4724848cSchristos #endif
1412*4724848cSchristos
tls_parse_stoc_session_ticket(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1413*4724848cSchristos int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
1414*4724848cSchristos X509 *x, size_t chainidx)
1415*4724848cSchristos {
1416*4724848cSchristos if (s->ext.session_ticket_cb != NULL &&
1417*4724848cSchristos !s->ext.session_ticket_cb(s, PACKET_data(pkt),
1418*4724848cSchristos PACKET_remaining(pkt),
1419*4724848cSchristos s->ext.session_ticket_cb_arg)) {
1420*4724848cSchristos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1421*4724848cSchristos SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
1422*4724848cSchristos return 0;
1423*4724848cSchristos }
1424*4724848cSchristos
1425*4724848cSchristos if (!tls_use_ticket(s)) {
1426*4724848cSchristos SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
1427*4724848cSchristos SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
1428*4724848cSchristos return 0;
1429*4724848cSchristos }
1430*4724848cSchristos if (PACKET_remaining(pkt) > 0) {
1431*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR,
1432*4724848cSchristos SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
1433*4724848cSchristos return 0;
1434*4724848cSchristos }
1435*4724848cSchristos
1436*4724848cSchristos s->ext.ticket_expected = 1;
1437*4724848cSchristos
1438*4724848cSchristos return 1;
1439*4724848cSchristos }
1440*4724848cSchristos
1441*4724848cSchristos #ifndef OPENSSL_NO_OCSP
tls_parse_stoc_status_request(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1442*4724848cSchristos int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
1443*4724848cSchristos X509 *x, size_t chainidx)
1444*4724848cSchristos {
1445*4724848cSchristos if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1446*4724848cSchristos /* We ignore this if the server sends a CertificateRequest */
1447*4724848cSchristos /* TODO(TLS1.3): Add support for this */
1448*4724848cSchristos return 1;
1449*4724848cSchristos }
1450*4724848cSchristos
1451*4724848cSchristos /*
1452*4724848cSchristos * MUST only be sent if we've requested a status
1453*4724848cSchristos * request message. In TLS <= 1.2 it must also be empty.
1454*4724848cSchristos */
1455*4724848cSchristos if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
1456*4724848cSchristos SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
1457*4724848cSchristos SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
1458*4724848cSchristos return 0;
1459*4724848cSchristos }
1460*4724848cSchristos if (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
1461*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR,
1462*4724848cSchristos SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
1463*4724848cSchristos return 0;
1464*4724848cSchristos }
1465*4724848cSchristos
1466*4724848cSchristos if (SSL_IS_TLS13(s)) {
1467*4724848cSchristos /* We only know how to handle this if it's for the first Certificate in
1468*4724848cSchristos * the chain. We ignore any other responses.
1469*4724848cSchristos */
1470*4724848cSchristos if (chainidx != 0)
1471*4724848cSchristos return 1;
1472*4724848cSchristos
1473*4724848cSchristos /* SSLfatal() already called */
1474*4724848cSchristos return tls_process_cert_status_body(s, pkt);
1475*4724848cSchristos }
1476*4724848cSchristos
1477*4724848cSchristos /* Set flag to expect CertificateStatus message */
1478*4724848cSchristos s->ext.status_expected = 1;
1479*4724848cSchristos
1480*4724848cSchristos return 1;
1481*4724848cSchristos }
1482*4724848cSchristos #endif
1483*4724848cSchristos
1484*4724848cSchristos
1485*4724848cSchristos #ifndef OPENSSL_NO_CT
tls_parse_stoc_sct(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1486*4724848cSchristos int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1487*4724848cSchristos size_t chainidx)
1488*4724848cSchristos {
1489*4724848cSchristos if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1490*4724848cSchristos /* We ignore this if the server sends it in a CertificateRequest */
1491*4724848cSchristos /* TODO(TLS1.3): Add support for this */
1492*4724848cSchristos return 1;
1493*4724848cSchristos }
1494*4724848cSchristos
1495*4724848cSchristos /*
1496*4724848cSchristos * Only take it if we asked for it - i.e if there is no CT validation
1497*4724848cSchristos * callback set, then a custom extension MAY be processing it, so we
1498*4724848cSchristos * need to let control continue to flow to that.
1499*4724848cSchristos */
1500*4724848cSchristos if (s->ct_validation_callback != NULL) {
1501*4724848cSchristos size_t size = PACKET_remaining(pkt);
1502*4724848cSchristos
1503*4724848cSchristos /* Simply copy it off for later processing */
1504*4724848cSchristos OPENSSL_free(s->ext.scts);
1505*4724848cSchristos s->ext.scts = NULL;
1506*4724848cSchristos
1507*4724848cSchristos s->ext.scts_len = (uint16_t)size;
1508*4724848cSchristos if (size > 0) {
1509*4724848cSchristos s->ext.scts = OPENSSL_malloc(size);
1510*4724848cSchristos if (s->ext.scts == NULL) {
1511*4724848cSchristos s->ext.scts_len = 0;
1512*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SCT,
1513*4724848cSchristos ERR_R_MALLOC_FAILURE);
1514*4724848cSchristos return 0;
1515*4724848cSchristos }
1516*4724848cSchristos if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
1517*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SCT,
1518*4724848cSchristos ERR_R_INTERNAL_ERROR);
1519*4724848cSchristos return 0;
1520*4724848cSchristos }
1521*4724848cSchristos }
1522*4724848cSchristos } else {
1523*4724848cSchristos ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
1524*4724848cSchristos ? ENDPOINT_CLIENT : ENDPOINT_BOTH;
1525*4724848cSchristos
1526*4724848cSchristos /*
1527*4724848cSchristos * If we didn't ask for it then there must be a custom extension,
1528*4724848cSchristos * otherwise this is unsolicited.
1529*4724848cSchristos */
1530*4724848cSchristos if (custom_ext_find(&s->cert->custext, role,
1531*4724848cSchristos TLSEXT_TYPE_signed_certificate_timestamp,
1532*4724848cSchristos NULL) == NULL) {
1533*4724848cSchristos SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_SCT,
1534*4724848cSchristos SSL_R_BAD_EXTENSION);
1535*4724848cSchristos return 0;
1536*4724848cSchristos }
1537*4724848cSchristos
1538*4724848cSchristos if (!custom_ext_parse(s, context,
1539*4724848cSchristos TLSEXT_TYPE_signed_certificate_timestamp,
1540*4724848cSchristos PACKET_data(pkt), PACKET_remaining(pkt),
1541*4724848cSchristos x, chainidx)) {
1542*4724848cSchristos /* SSLfatal already called */
1543*4724848cSchristos return 0;
1544*4724848cSchristos }
1545*4724848cSchristos }
1546*4724848cSchristos
1547*4724848cSchristos return 1;
1548*4724848cSchristos }
1549*4724848cSchristos #endif
1550*4724848cSchristos
1551*4724848cSchristos
1552*4724848cSchristos #ifndef OPENSSL_NO_NEXTPROTONEG
1553*4724848cSchristos /*
1554*4724848cSchristos * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1555*4724848cSchristos * elements of zero length are allowed and the set of elements must exactly
1556*4724848cSchristos * fill the length of the block. Returns 1 on success or 0 on failure.
1557*4724848cSchristos */
ssl_next_proto_validate(SSL * s,PACKET * pkt)1558*4724848cSchristos static int ssl_next_proto_validate(SSL *s, PACKET *pkt)
1559*4724848cSchristos {
1560*4724848cSchristos PACKET tmp_protocol;
1561*4724848cSchristos
1562*4724848cSchristos while (PACKET_remaining(pkt)) {
1563*4724848cSchristos if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
1564*4724848cSchristos || PACKET_remaining(&tmp_protocol) == 0) {
1565*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_NEXT_PROTO_VALIDATE,
1566*4724848cSchristos SSL_R_BAD_EXTENSION);
1567*4724848cSchristos return 0;
1568*4724848cSchristos }
1569*4724848cSchristos }
1570*4724848cSchristos
1571*4724848cSchristos return 1;
1572*4724848cSchristos }
1573*4724848cSchristos
tls_parse_stoc_npn(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1574*4724848cSchristos int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1575*4724848cSchristos size_t chainidx)
1576*4724848cSchristos {
1577*4724848cSchristos unsigned char *selected;
1578*4724848cSchristos unsigned char selected_len;
1579*4724848cSchristos PACKET tmppkt;
1580*4724848cSchristos
1581*4724848cSchristos /* Check if we are in a renegotiation. If so ignore this extension */
1582*4724848cSchristos if (!SSL_IS_FIRST_HANDSHAKE(s))
1583*4724848cSchristos return 1;
1584*4724848cSchristos
1585*4724848cSchristos /* We must have requested it. */
1586*4724848cSchristos if (s->ctx->ext.npn_select_cb == NULL) {
1587*4724848cSchristos SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_NPN,
1588*4724848cSchristos SSL_R_BAD_EXTENSION);
1589*4724848cSchristos return 0;
1590*4724848cSchristos }
1591*4724848cSchristos
1592*4724848cSchristos /* The data must be valid */
1593*4724848cSchristos tmppkt = *pkt;
1594*4724848cSchristos if (!ssl_next_proto_validate(s, &tmppkt)) {
1595*4724848cSchristos /* SSLfatal() already called */
1596*4724848cSchristos return 0;
1597*4724848cSchristos }
1598*4724848cSchristos if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len,
1599*4724848cSchristos PACKET_data(pkt),
1600*4724848cSchristos PACKET_remaining(pkt),
1601*4724848cSchristos s->ctx->ext.npn_select_cb_arg) !=
1602*4724848cSchristos SSL_TLSEXT_ERR_OK) {
1603*4724848cSchristos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_STOC_NPN,
1604*4724848cSchristos SSL_R_BAD_EXTENSION);
1605*4724848cSchristos return 0;
1606*4724848cSchristos }
1607*4724848cSchristos
1608*4724848cSchristos /*
1609*4724848cSchristos * Could be non-NULL if server has sent multiple NPN extensions in
1610*4724848cSchristos * a single Serverhello
1611*4724848cSchristos */
1612*4724848cSchristos OPENSSL_free(s->ext.npn);
1613*4724848cSchristos s->ext.npn = OPENSSL_malloc(selected_len);
1614*4724848cSchristos if (s->ext.npn == NULL) {
1615*4724848cSchristos s->ext.npn_len = 0;
1616*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_NPN,
1617*4724848cSchristos ERR_R_INTERNAL_ERROR);
1618*4724848cSchristos return 0;
1619*4724848cSchristos }
1620*4724848cSchristos
1621*4724848cSchristos memcpy(s->ext.npn, selected, selected_len);
1622*4724848cSchristos s->ext.npn_len = selected_len;
1623*4724848cSchristos s->s3->npn_seen = 1;
1624*4724848cSchristos
1625*4724848cSchristos return 1;
1626*4724848cSchristos }
1627*4724848cSchristos #endif
1628*4724848cSchristos
tls_parse_stoc_alpn(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1629*4724848cSchristos int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1630*4724848cSchristos size_t chainidx)
1631*4724848cSchristos {
1632*4724848cSchristos size_t len;
1633*4724848cSchristos
1634*4724848cSchristos /* We must have requested it. */
1635*4724848cSchristos if (!s->s3->alpn_sent) {
1636*4724848cSchristos SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_ALPN,
1637*4724848cSchristos SSL_R_BAD_EXTENSION);
1638*4724848cSchristos return 0;
1639*4724848cSchristos }
1640*4724848cSchristos /*-
1641*4724848cSchristos * The extension data consists of:
1642*4724848cSchristos * uint16 list_length
1643*4724848cSchristos * uint8 proto_length;
1644*4724848cSchristos * uint8 proto[proto_length];
1645*4724848cSchristos */
1646*4724848cSchristos if (!PACKET_get_net_2_len(pkt, &len)
1647*4724848cSchristos || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
1648*4724848cSchristos || PACKET_remaining(pkt) != len) {
1649*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1650*4724848cSchristos SSL_R_BAD_EXTENSION);
1651*4724848cSchristos return 0;
1652*4724848cSchristos }
1653*4724848cSchristos OPENSSL_free(s->s3->alpn_selected);
1654*4724848cSchristos s->s3->alpn_selected = OPENSSL_malloc(len);
1655*4724848cSchristos if (s->s3->alpn_selected == NULL) {
1656*4724848cSchristos s->s3->alpn_selected_len = 0;
1657*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1658*4724848cSchristos ERR_R_INTERNAL_ERROR);
1659*4724848cSchristos return 0;
1660*4724848cSchristos }
1661*4724848cSchristos if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
1662*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1663*4724848cSchristos SSL_R_BAD_EXTENSION);
1664*4724848cSchristos return 0;
1665*4724848cSchristos }
1666*4724848cSchristos s->s3->alpn_selected_len = len;
1667*4724848cSchristos
1668*4724848cSchristos if (s->session->ext.alpn_selected == NULL
1669*4724848cSchristos || s->session->ext.alpn_selected_len != len
1670*4724848cSchristos || memcmp(s->session->ext.alpn_selected, s->s3->alpn_selected, len)
1671*4724848cSchristos != 0) {
1672*4724848cSchristos /* ALPN not consistent with the old session so cannot use early_data */
1673*4724848cSchristos s->ext.early_data_ok = 0;
1674*4724848cSchristos }
1675*4724848cSchristos if (!s->hit) {
1676*4724848cSchristos /*
1677*4724848cSchristos * This is a new session and so alpn_selected should have been
1678*4724848cSchristos * initialised to NULL. We should update it with the selected ALPN.
1679*4724848cSchristos */
1680*4724848cSchristos if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
1681*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1682*4724848cSchristos ERR_R_INTERNAL_ERROR);
1683*4724848cSchristos return 0;
1684*4724848cSchristos }
1685*4724848cSchristos s->session->ext.alpn_selected =
1686*4724848cSchristos OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
1687*4724848cSchristos if (s->session->ext.alpn_selected == NULL) {
1688*4724848cSchristos s->session->ext.alpn_selected_len = 0;
1689*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1690*4724848cSchristos ERR_R_INTERNAL_ERROR);
1691*4724848cSchristos return 0;
1692*4724848cSchristos }
1693*4724848cSchristos s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
1694*4724848cSchristos }
1695*4724848cSchristos
1696*4724848cSchristos return 1;
1697*4724848cSchristos }
1698*4724848cSchristos
1699*4724848cSchristos #ifndef OPENSSL_NO_SRTP
tls_parse_stoc_use_srtp(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1700*4724848cSchristos int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1701*4724848cSchristos size_t chainidx)
1702*4724848cSchristos {
1703*4724848cSchristos unsigned int id, ct, mki;
1704*4724848cSchristos int i;
1705*4724848cSchristos STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1706*4724848cSchristos SRTP_PROTECTION_PROFILE *prof;
1707*4724848cSchristos
1708*4724848cSchristos if (!PACKET_get_net_2(pkt, &ct) || ct != 2
1709*4724848cSchristos || !PACKET_get_net_2(pkt, &id)
1710*4724848cSchristos || !PACKET_get_1(pkt, &mki)
1711*4724848cSchristos || PACKET_remaining(pkt) != 0) {
1712*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1713*4724848cSchristos SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1714*4724848cSchristos return 0;
1715*4724848cSchristos }
1716*4724848cSchristos
1717*4724848cSchristos if (mki != 0) {
1718*4724848cSchristos /* Must be no MKI, since we never offer one */
1719*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1720*4724848cSchristos SSL_R_BAD_SRTP_MKI_VALUE);
1721*4724848cSchristos return 0;
1722*4724848cSchristos }
1723*4724848cSchristos
1724*4724848cSchristos /* Throw an error if the server gave us an unsolicited extension */
1725*4724848cSchristos clnt = SSL_get_srtp_profiles(s);
1726*4724848cSchristos if (clnt == NULL) {
1727*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1728*4724848cSchristos SSL_R_NO_SRTP_PROFILES);
1729*4724848cSchristos return 0;
1730*4724848cSchristos }
1731*4724848cSchristos
1732*4724848cSchristos /*
1733*4724848cSchristos * Check to see if the server gave us something we support (and
1734*4724848cSchristos * presumably offered)
1735*4724848cSchristos */
1736*4724848cSchristos for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1737*4724848cSchristos prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1738*4724848cSchristos
1739*4724848cSchristos if (prof->id == id) {
1740*4724848cSchristos s->srtp_profile = prof;
1741*4724848cSchristos return 1;
1742*4724848cSchristos }
1743*4724848cSchristos }
1744*4724848cSchristos
1745*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1746*4724848cSchristos SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1747*4724848cSchristos return 0;
1748*4724848cSchristos }
1749*4724848cSchristos #endif
1750*4724848cSchristos
tls_parse_stoc_etm(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1751*4724848cSchristos int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1752*4724848cSchristos size_t chainidx)
1753*4724848cSchristos {
1754*4724848cSchristos /* Ignore if inappropriate ciphersuite */
1755*4724848cSchristos if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
1756*4724848cSchristos && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
1757*4724848cSchristos && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4
1758*4724848cSchristos && s->s3->tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
1759*4724848cSchristos && s->s3->tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12)
1760*4724848cSchristos s->ext.use_etm = 1;
1761*4724848cSchristos
1762*4724848cSchristos return 1;
1763*4724848cSchristos }
1764*4724848cSchristos
tls_parse_stoc_ems(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1765*4724848cSchristos int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1766*4724848cSchristos size_t chainidx)
1767*4724848cSchristos {
1768*4724848cSchristos s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1769*4724848cSchristos if (!s->hit)
1770*4724848cSchristos s->session->flags |= SSL_SESS_FLAG_EXTMS;
1771*4724848cSchristos
1772*4724848cSchristos return 1;
1773*4724848cSchristos }
1774*4724848cSchristos
tls_parse_stoc_supported_versions(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1775*4724848cSchristos int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context,
1776*4724848cSchristos X509 *x, size_t chainidx)
1777*4724848cSchristos {
1778*4724848cSchristos unsigned int version;
1779*4724848cSchristos
1780*4724848cSchristos if (!PACKET_get_net_2(pkt, &version)
1781*4724848cSchristos || PACKET_remaining(pkt) != 0) {
1782*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR,
1783*4724848cSchristos SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
1784*4724848cSchristos SSL_R_LENGTH_MISMATCH);
1785*4724848cSchristos return 0;
1786*4724848cSchristos }
1787*4724848cSchristos
1788*4724848cSchristos /*
1789*4724848cSchristos * The only protocol version we support which is valid in this extension in
1790*4724848cSchristos * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
1791*4724848cSchristos */
1792*4724848cSchristos if (version != TLS1_3_VERSION) {
1793*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1794*4724848cSchristos SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
1795*4724848cSchristos SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1796*4724848cSchristos return 0;
1797*4724848cSchristos }
1798*4724848cSchristos
1799*4724848cSchristos /* We ignore this extension for HRRs except to sanity check it */
1800*4724848cSchristos if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
1801*4724848cSchristos return 1;
1802*4724848cSchristos
1803*4724848cSchristos /* We just set it here. We validate it in ssl_choose_client_version */
1804*4724848cSchristos s->version = version;
1805*4724848cSchristos
1806*4724848cSchristos return 1;
1807*4724848cSchristos }
1808*4724848cSchristos
tls_parse_stoc_key_share(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1809*4724848cSchristos int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1810*4724848cSchristos size_t chainidx)
1811*4724848cSchristos {
1812*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
1813*4724848cSchristos unsigned int group_id;
1814*4724848cSchristos PACKET encoded_pt;
1815*4724848cSchristos EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
1816*4724848cSchristos
1817*4724848cSchristos /* Sanity check */
1818*4724848cSchristos if (ckey == NULL || s->s3->peer_tmp != NULL) {
1819*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1820*4724848cSchristos ERR_R_INTERNAL_ERROR);
1821*4724848cSchristos return 0;
1822*4724848cSchristos }
1823*4724848cSchristos
1824*4724848cSchristos if (!PACKET_get_net_2(pkt, &group_id)) {
1825*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1826*4724848cSchristos SSL_R_LENGTH_MISMATCH);
1827*4724848cSchristos return 0;
1828*4724848cSchristos }
1829*4724848cSchristos
1830*4724848cSchristos if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
1831*4724848cSchristos const uint16_t *pgroups = NULL;
1832*4724848cSchristos size_t i, num_groups;
1833*4724848cSchristos
1834*4724848cSchristos if (PACKET_remaining(pkt) != 0) {
1835*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1836*4724848cSchristos SSL_R_LENGTH_MISMATCH);
1837*4724848cSchristos return 0;
1838*4724848cSchristos }
1839*4724848cSchristos
1840*4724848cSchristos /*
1841*4724848cSchristos * It is an error if the HelloRetryRequest wants a key_share that we
1842*4724848cSchristos * already sent in the first ClientHello
1843*4724848cSchristos */
1844*4724848cSchristos if (group_id == s->s3->group_id) {
1845*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1846*4724848cSchristos SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1847*4724848cSchristos return 0;
1848*4724848cSchristos }
1849*4724848cSchristos
1850*4724848cSchristos /* Validate the selected group is one we support */
1851*4724848cSchristos tls1_get_supported_groups(s, &pgroups, &num_groups);
1852*4724848cSchristos for (i = 0; i < num_groups; i++) {
1853*4724848cSchristos if (group_id == pgroups[i])
1854*4724848cSchristos break;
1855*4724848cSchristos }
1856*4724848cSchristos if (i >= num_groups
1857*4724848cSchristos || !tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)) {
1858*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1859*4724848cSchristos SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1860*4724848cSchristos return 0;
1861*4724848cSchristos }
1862*4724848cSchristos
1863*4724848cSchristos s->s3->group_id = group_id;
1864*4724848cSchristos EVP_PKEY_free(s->s3->tmp.pkey);
1865*4724848cSchristos s->s3->tmp.pkey = NULL;
1866*4724848cSchristos return 1;
1867*4724848cSchristos }
1868*4724848cSchristos
1869*4724848cSchristos if (group_id != s->s3->group_id) {
1870*4724848cSchristos /*
1871*4724848cSchristos * This isn't for the group that we sent in the original
1872*4724848cSchristos * key_share!
1873*4724848cSchristos */
1874*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1875*4724848cSchristos SSL_R_BAD_KEY_SHARE);
1876*4724848cSchristos return 0;
1877*4724848cSchristos }
1878*4724848cSchristos
1879*4724848cSchristos if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1880*4724848cSchristos || PACKET_remaining(&encoded_pt) == 0) {
1881*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1882*4724848cSchristos SSL_R_LENGTH_MISMATCH);
1883*4724848cSchristos return 0;
1884*4724848cSchristos }
1885*4724848cSchristos
1886*4724848cSchristos skey = EVP_PKEY_new();
1887*4724848cSchristos if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
1888*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1889*4724848cSchristos ERR_R_MALLOC_FAILURE);
1890*4724848cSchristos EVP_PKEY_free(skey);
1891*4724848cSchristos return 0;
1892*4724848cSchristos }
1893*4724848cSchristos if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
1894*4724848cSchristos PACKET_remaining(&encoded_pt))) {
1895*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1896*4724848cSchristos SSL_R_BAD_ECPOINT);
1897*4724848cSchristos EVP_PKEY_free(skey);
1898*4724848cSchristos return 0;
1899*4724848cSchristos }
1900*4724848cSchristos
1901*4724848cSchristos if (ssl_derive(s, ckey, skey, 1) == 0) {
1902*4724848cSchristos /* SSLfatal() already called */
1903*4724848cSchristos EVP_PKEY_free(skey);
1904*4724848cSchristos return 0;
1905*4724848cSchristos }
1906*4724848cSchristos s->s3->peer_tmp = skey;
1907*4724848cSchristos #endif
1908*4724848cSchristos
1909*4724848cSchristos return 1;
1910*4724848cSchristos }
1911*4724848cSchristos
tls_parse_stoc_cookie(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1912*4724848cSchristos int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1913*4724848cSchristos size_t chainidx)
1914*4724848cSchristos {
1915*4724848cSchristos PACKET cookie;
1916*4724848cSchristos
1917*4724848cSchristos if (!PACKET_as_length_prefixed_2(pkt, &cookie)
1918*4724848cSchristos || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
1919*4724848cSchristos &s->ext.tls13_cookie_len)) {
1920*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_COOKIE,
1921*4724848cSchristos SSL_R_LENGTH_MISMATCH);
1922*4724848cSchristos return 0;
1923*4724848cSchristos }
1924*4724848cSchristos
1925*4724848cSchristos return 1;
1926*4724848cSchristos }
1927*4724848cSchristos
tls_parse_stoc_early_data(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1928*4724848cSchristos int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
1929*4724848cSchristos X509 *x, size_t chainidx)
1930*4724848cSchristos {
1931*4724848cSchristos if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
1932*4724848cSchristos unsigned long max_early_data;
1933*4724848cSchristos
1934*4724848cSchristos if (!PACKET_get_net_4(pkt, &max_early_data)
1935*4724848cSchristos || PACKET_remaining(pkt) != 0) {
1936*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
1937*4724848cSchristos SSL_R_INVALID_MAX_EARLY_DATA);
1938*4724848cSchristos return 0;
1939*4724848cSchristos }
1940*4724848cSchristos
1941*4724848cSchristos s->session->ext.max_early_data = max_early_data;
1942*4724848cSchristos
1943*4724848cSchristos return 1;
1944*4724848cSchristos }
1945*4724848cSchristos
1946*4724848cSchristos if (PACKET_remaining(pkt) != 0) {
1947*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
1948*4724848cSchristos SSL_R_BAD_EXTENSION);
1949*4724848cSchristos return 0;
1950*4724848cSchristos }
1951*4724848cSchristos
1952*4724848cSchristos if (!s->ext.early_data_ok
1953*4724848cSchristos || !s->hit) {
1954*4724848cSchristos /*
1955*4724848cSchristos * If we get here then we didn't send early data, or we didn't resume
1956*4724848cSchristos * using the first identity, or the SNI/ALPN is not consistent so the
1957*4724848cSchristos * server should not be accepting it.
1958*4724848cSchristos */
1959*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
1960*4724848cSchristos SSL_R_BAD_EXTENSION);
1961*4724848cSchristos return 0;
1962*4724848cSchristos }
1963*4724848cSchristos
1964*4724848cSchristos s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1965*4724848cSchristos
1966*4724848cSchristos return 1;
1967*4724848cSchristos }
1968*4724848cSchristos
tls_parse_stoc_psk(SSL * s,PACKET * pkt,unsigned int context,X509 * x,size_t chainidx)1969*4724848cSchristos int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1970*4724848cSchristos size_t chainidx)
1971*4724848cSchristos {
1972*4724848cSchristos #ifndef OPENSSL_NO_TLS1_3
1973*4724848cSchristos unsigned int identity;
1974*4724848cSchristos
1975*4724848cSchristos if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
1976*4724848cSchristos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_PSK,
1977*4724848cSchristos SSL_R_LENGTH_MISMATCH);
1978*4724848cSchristos return 0;
1979*4724848cSchristos }
1980*4724848cSchristos
1981*4724848cSchristos if (identity >= (unsigned int)s->ext.tick_identity) {
1982*4724848cSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_PSK,
1983*4724848cSchristos SSL_R_BAD_PSK_IDENTITY);
1984*4724848cSchristos return 0;
1985*4724848cSchristos }
1986*4724848cSchristos
1987*4724848cSchristos /*
1988*4724848cSchristos * Session resumption tickets are always sent before PSK tickets. If the
1989*4724848cSchristos * ticket index is 0 then it must be for a session resumption ticket if we
1990*4724848cSchristos * sent two tickets, or if we didn't send a PSK ticket.
1991*4724848cSchristos */
1992*4724848cSchristos if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
1993*4724848cSchristos s->hit = 1;
1994*4724848cSchristos SSL_SESSION_free(s->psksession);
1995*4724848cSchristos s->psksession = NULL;
1996*4724848cSchristos return 1;
1997*4724848cSchristos }
1998*4724848cSchristos
1999*4724848cSchristos if (s->psksession == NULL) {
2000*4724848cSchristos /* Should never happen */
2001*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_PSK,
2002*4724848cSchristos ERR_R_INTERNAL_ERROR);
2003*4724848cSchristos return 0;
2004*4724848cSchristos }
2005*4724848cSchristos
2006*4724848cSchristos /*
2007*4724848cSchristos * If we used the external PSK for sending early_data then s->early_secret
2008*4724848cSchristos * is already set up, so don't overwrite it. Otherwise we copy the
2009*4724848cSchristos * early_secret across that we generated earlier.
2010*4724848cSchristos */
2011*4724848cSchristos if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
2012*4724848cSchristos && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
2013*4724848cSchristos || s->session->ext.max_early_data > 0
2014*4724848cSchristos || s->psksession->ext.max_early_data == 0)
2015*4724848cSchristos memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
2016*4724848cSchristos
2017*4724848cSchristos SSL_SESSION_free(s->session);
2018*4724848cSchristos s->session = s->psksession;
2019*4724848cSchristos s->psksession = NULL;
2020*4724848cSchristos s->hit = 1;
2021*4724848cSchristos /* Early data is only allowed if we used the first ticket */
2022*4724848cSchristos if (identity != 0)
2023*4724848cSchristos s->ext.early_data_ok = 0;
2024*4724848cSchristos #endif
2025*4724848cSchristos
2026*4724848cSchristos return 1;
2027*4724848cSchristos }
2028