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