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