]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/extensions.c
Only accept early_data if the negotiated ALPN is the same
[thirdparty/openssl.git] / ssl / statem / extensions.c
1 /*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11 #include "../ssl_locl.h"
12 #include "statem_locl.h"
13
14 static int final_renegotiate(SSL *s, unsigned int context, int sent,
15 int *al);
16 static int init_server_name(SSL *s, unsigned int context);
17 static int final_server_name(SSL *s, unsigned int context, int sent,
18 int *al);
19 #ifndef OPENSSL_NO_EC
20 static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
21 int *al);
22 #endif
23 static int init_session_ticket(SSL *s, unsigned int context);
24 #ifndef OPENSSL_NO_OCSP
25 static int init_status_request(SSL *s, unsigned int context);
26 #endif
27 #ifndef OPENSSL_NO_NEXTPROTONEG
28 static int init_npn(SSL *s, unsigned int context);
29 #endif
30 static int init_alpn(SSL *s, unsigned int context);
31 static int final_alpn(SSL *s, unsigned int context, int sent, int *al);
32 static int init_sig_algs(SSL *s, unsigned int context);
33 #ifndef OPENSSL_NO_SRP
34 static int init_srp(SSL *s, unsigned int context);
35 #endif
36 static int init_etm(SSL *s, unsigned int context);
37 static int init_ems(SSL *s, unsigned int context);
38 static int final_ems(SSL *s, unsigned int context, int sent, int *al);
39 static int init_psk_kex_modes(SSL *s, unsigned int context);
40 #ifndef OPENSSL_NO_EC
41 static int final_key_share(SSL *s, unsigned int context, int sent, int *al);
42 #endif
43 #ifndef OPENSSL_NO_SRTP
44 static int init_srtp(SSL *s, unsigned int context);
45 #endif
46 static int final_sig_algs(SSL *s, unsigned int context, int sent, int *al);
47 static int final_early_data(SSL *s, unsigned int context, int sent, int *al);
48
49 /* Structure to define a built-in extension */
50 typedef struct extensions_definition_st {
51 /* The defined type for the extension */
52 unsigned int type;
53 /*
54 * The context that this extension applies to, e.g. what messages and
55 * protocol versions
56 */
57 unsigned int context;
58 /*
59 * Initialise extension before parsing. Always called for relevant contexts
60 * even if extension not present
61 */
62 int (*init)(SSL *s, unsigned int context);
63 /* Parse extension sent from client to server */
64 int (*parse_ctos)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
65 size_t chainidx, int *al);
66 /* Parse extension send from server to client */
67 int (*parse_stoc)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
68 size_t chainidx, int *al);
69 /* Construct extension sent from server to client */
70 int (*construct_stoc)(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
71 size_t chainidx, int *al);
72 /* Construct extension sent from client to server */
73 int (*construct_ctos)(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
74 size_t chainidx, int *al);
75 /*
76 * Finalise extension after parsing. Always called where an extensions was
77 * initialised even if the extension was not present. |sent| is set to 1 if
78 * the extension was seen, or 0 otherwise.
79 */
80 int (*final)(SSL *s, unsigned int context, int sent, int *al);
81 } EXTENSION_DEFINITION;
82
83 /*
84 * Definitions of all built-in extensions. NOTE: Changes in the number or order
85 * of these extensions should be mirrored with equivalent changes to the
86 * indexes ( TLSEXT_IDX_* ) defined in ssl_locl.h.
87 * Each extension has an initialiser, a client and
88 * server side parser and a finaliser. The initialiser is called (if the
89 * extension is relevant to the given context) even if we did not see the
90 * extension in the message that we received. The parser functions are only
91 * called if we see the extension in the message. The finalisers are always
92 * called if the initialiser was called.
93 * There are also server and client side constructor functions which are always
94 * called during message construction if the extension is relevant for the
95 * given context.
96 * The initialisation, parsing, finalisation and construction functions are
97 * always called in the order defined in this list. Some extensions may depend
98 * on others having been processed first, so the order of this list is
99 * significant.
100 * The extension context is defined by a series of flags which specify which
101 * messages the extension is relevant to. These flags also specify whether the
102 * extension is relevant to a particular protocol or protocol version.
103 *
104 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
105 */
106 #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
107 static const EXTENSION_DEFINITION ext_defs[] = {
108 {
109 TLSEXT_TYPE_renegotiate,
110 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
111 | EXT_TLS1_2_AND_BELOW_ONLY,
112 NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
113 tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
114 final_renegotiate
115 },
116 {
117 TLSEXT_TYPE_server_name,
118 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
119 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
120 init_server_name,
121 tls_parse_ctos_server_name, tls_parse_stoc_server_name,
122 tls_construct_stoc_server_name, tls_construct_ctos_server_name,
123 final_server_name
124 },
125 #ifndef OPENSSL_NO_SRP
126 {
127 TLSEXT_TYPE_srp,
128 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
129 init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
130 },
131 #else
132 INVALID_EXTENSION,
133 #endif
134 {
135 TLSEXT_TYPE_early_data_info,
136 EXT_TLS1_3_NEW_SESSION_TICKET,
137 NULL, NULL, tls_parse_stoc_early_data_info,
138 tls_construct_stoc_early_data_info, NULL, NULL
139 },
140 #ifndef OPENSSL_NO_EC
141 {
142 TLSEXT_TYPE_ec_point_formats,
143 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
144 NULL, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
145 tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
146 final_ec_pt_formats
147 },
148 {
149 TLSEXT_TYPE_supported_groups,
150 EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
151 NULL, tls_parse_ctos_supported_groups, NULL,
152 NULL /* TODO(TLS1.3): Need to add this */,
153 tls_construct_ctos_supported_groups, NULL
154 },
155 #else
156 INVALID_EXTENSION,
157 INVALID_EXTENSION,
158 #endif
159 {
160 TLSEXT_TYPE_session_ticket,
161 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
162 init_session_ticket, tls_parse_ctos_session_ticket,
163 tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
164 tls_construct_ctos_session_ticket, NULL
165 },
166 {
167 TLSEXT_TYPE_signature_algorithms,
168 EXT_CLIENT_HELLO,
169 init_sig_algs, tls_parse_ctos_sig_algs, NULL, NULL,
170 tls_construct_ctos_sig_algs, final_sig_algs
171 },
172 #ifndef OPENSSL_NO_OCSP
173 {
174 TLSEXT_TYPE_status_request,
175 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
176 | EXT_TLS1_3_CERTIFICATE,
177 init_status_request, tls_parse_ctos_status_request,
178 tls_parse_stoc_status_request, tls_construct_stoc_status_request,
179 tls_construct_ctos_status_request, NULL
180 },
181 #else
182 INVALID_EXTENSION,
183 #endif
184 #ifndef OPENSSL_NO_NEXTPROTONEG
185 {
186 TLSEXT_TYPE_next_proto_neg,
187 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
188 init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
189 tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
190 },
191 #else
192 INVALID_EXTENSION,
193 #endif
194 {
195 /*
196 * Must appear in this list after server_name so that finalisation
197 * happens after server_name callbacks
198 */
199 TLSEXT_TYPE_application_layer_protocol_negotiation,
200 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
201 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
202 init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
203 tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
204 },
205 #ifndef OPENSSL_NO_SRTP
206 {
207 TLSEXT_TYPE_use_srtp,
208 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
209 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY,
210 init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
211 tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
212 },
213 #else
214 INVALID_EXTENSION,
215 #endif
216 {
217 TLSEXT_TYPE_encrypt_then_mac,
218 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY | EXT_SSL3_ALLOWED,
219 init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
220 tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
221 },
222 #ifndef OPENSSL_NO_CT
223 {
224 TLSEXT_TYPE_signed_certificate_timestamp,
225 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
226 | EXT_TLS1_3_CERTIFICATE,
227 NULL,
228 /*
229 * No server side support for this, but can be provided by a custom
230 * extension. This is an exception to the rule that custom extensions
231 * cannot override built in ones.
232 */
233 NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
234 },
235 #else
236 INVALID_EXTENSION,
237 #endif
238 {
239 TLSEXT_TYPE_extended_master_secret,
240 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
241 init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
242 tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
243 },
244 {
245 TLSEXT_TYPE_supported_versions,
246 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY,
247 NULL,
248 /* Processed inline as part of version selection */
249 NULL, NULL, NULL, tls_construct_ctos_supported_versions, NULL
250 },
251 {
252 TLSEXT_TYPE_psk_kex_modes,
253 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY,
254 init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
255 tls_construct_ctos_psk_kex_modes, NULL
256 },
257 #ifndef OPENSSL_NO_EC
258 {
259 /*
260 * Must be in this list after supported_groups. We need that to have
261 * been parsed before we do this one.
262 */
263 TLSEXT_TYPE_key_share,
264 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
265 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
266 | EXT_TLS1_3_ONLY,
267 NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
268 tls_construct_stoc_key_share, tls_construct_ctos_key_share,
269 final_key_share
270 },
271 #endif
272 {
273 /*
274 * Special unsolicited ServerHello extension only used when
275 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
276 */
277 TLSEXT_TYPE_cryptopro_bug,
278 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
279 NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
280 },
281 {
282 TLSEXT_TYPE_early_data,
283 EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
284 NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
285 tls_construct_stoc_early_data, tls_construct_ctos_early_data,
286 final_early_data
287 },
288 {
289 /* Must be immediately before pre_shared_key */
290 /* TODO(TLS1.3): Fix me */
291 TLSEXT_TYPE_padding,
292 EXT_CLIENT_HELLO,
293 NULL,
294 /* We send this, but don't read it */
295 NULL, NULL, NULL, tls_construct_ctos_padding, NULL
296 },
297 {
298 /* Required by the TLSv1.3 spec to always be the last extension */
299 TLSEXT_TYPE_psk,
300 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO | EXT_TLS_IMPLEMENTATION_ONLY
301 | EXT_TLS1_3_ONLY,
302 NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
303 tls_construct_ctos_psk, NULL
304 }
305 };
306
307 /*
308 * Verify whether we are allowed to use the extension |type| in the current
309 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
310 * indicate the extension is not allowed. If returning 1 then |*found| is set to
311 * 1 if we found a definition for the extension, and |*idx| is set to its index
312 */
313 static int verify_extension(SSL *s, unsigned int context, unsigned int type,
314 custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
315 RAW_EXTENSION **found)
316 {
317 size_t i;
318 size_t builtin_num = OSSL_NELEM(ext_defs);
319 const EXTENSION_DEFINITION *thisext;
320
321 for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
322 if (type == thisext->type) {
323 /* Check we're allowed to use this extension in this context */
324 if ((context & thisext->context) == 0)
325 return 0;
326
327 if (SSL_IS_DTLS(s)) {
328 if ((thisext->context & EXT_TLS_ONLY) != 0)
329 return 0;
330 } else if ((thisext->context & EXT_DTLS_ONLY) != 0) {
331 return 0;
332 }
333
334 *found = &rawexlist[i];
335 return 1;
336 }
337 }
338
339 if ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) == 0) {
340 /*
341 * Custom extensions only apply to <=TLS1.2. This extension is unknown
342 * in this context - we allow it
343 */
344 *found = NULL;
345 return 1;
346 }
347
348 /* Check the custom extensions */
349 if (meths != NULL) {
350 for (i = builtin_num; i < builtin_num + meths->meths_count; i++) {
351 if (meths->meths[i - builtin_num].ext_type == type) {
352 *found = &rawexlist[i];
353 return 1;
354 }
355 }
356 }
357
358 /* Unknown extension. We allow it */
359 *found = NULL;
360 return 1;
361 }
362
363 /*
364 * Check whether the context defined for an extension |extctx| means whether
365 * the extension is relevant for the current context |thisctx| or not. Returns
366 * 1 if the extension is relevant for this context, and 0 otherwise
367 */
368 static int extension_is_relevant(SSL *s, unsigned int extctx,
369 unsigned int thisctx)
370 {
371 if ((SSL_IS_DTLS(s)
372 && (extctx & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
373 || (s->version == SSL3_VERSION
374 && (extctx & EXT_SSL3_ALLOWED) == 0)
375 || (SSL_IS_TLS13(s)
376 && (extctx & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
377 || (!SSL_IS_TLS13(s) && (extctx & EXT_TLS1_3_ONLY) != 0))
378 return 0;
379
380 return 1;
381 }
382
383 /*
384 * Gather a list of all the extensions from the data in |packet]. |context|
385 * tells us which message this extension is for. The raw extension data is
386 * stored in |*res| on success. In the event of an error the alert type to use
387 * is stored in |*al|. We don't actually process the content of the extensions
388 * yet, except to check their types. This function also runs the initialiser
389 * functions for all known extensions (whether we have collected them or not).
390 * If successful the caller is responsible for freeing the contents of |*res|.
391 *
392 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
393 * more than one extension of the same type in a ClientHello or ServerHello.
394 * This function returns 1 if all extensions are unique and we have parsed their
395 * types, and 0 if the extensions contain duplicates, could not be successfully
396 * found, or an internal error occurred. We only check duplicates for
397 * extensions that we know about. We ignore others.
398 */
399 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
400 RAW_EXTENSION **res, int *al, size_t *len)
401 {
402 PACKET extensions = *packet;
403 size_t i = 0;
404 size_t num_exts;
405 custom_ext_methods *exts = NULL;
406 RAW_EXTENSION *raw_extensions = NULL;
407 const EXTENSION_DEFINITION *thisexd;
408
409 *res = NULL;
410
411 /*
412 * Initialise server side custom extensions. Client side is done during
413 * construction of extensions for the ClientHello.
414 */
415 if ((context & EXT_CLIENT_HELLO) != 0) {
416 exts = &s->cert->srv_ext;
417 custom_ext_init(&s->cert->srv_ext);
418 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
419 exts = &s->cert->cli_ext;
420 }
421
422 num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
423 raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
424 if (raw_extensions == NULL) {
425 *al = SSL_AD_INTERNAL_ERROR;
426 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
427 return 0;
428 }
429
430 while (PACKET_remaining(&extensions) > 0) {
431 unsigned int type;
432 PACKET extension;
433 RAW_EXTENSION *thisex;
434
435 if (!PACKET_get_net_2(&extensions, &type) ||
436 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
437 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
438 *al = SSL_AD_DECODE_ERROR;
439 goto err;
440 }
441 /*
442 * Verify this extension is allowed. We only check duplicates for
443 * extensions that we recognise.
444 */
445 if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
446 || (thisex != NULL && thisex->present == 1)) {
447 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
448 *al = SSL_AD_ILLEGAL_PARAMETER;
449 goto err;
450 }
451 if (thisex != NULL) {
452 thisex->data = extension;
453 thisex->present = 1;
454 thisex->type = type;
455 }
456 }
457
458 /*
459 * Initialise all known extensions relevant to this context, whether we have
460 * found them or not
461 */
462 for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
463 if(thisexd->init != NULL && (thisexd->context & context) != 0
464 && extension_is_relevant(s, thisexd->context, context)
465 && !thisexd->init(s, context)) {
466 *al = SSL_AD_INTERNAL_ERROR;
467 goto err;
468 }
469 }
470
471 *res = raw_extensions;
472 if (len != NULL)
473 *len = num_exts;
474 return 1;
475
476 err:
477 OPENSSL_free(raw_extensions);
478 return 0;
479 }
480
481 /*
482 * Runs the parser for a given extension with index |idx|. |exts| contains the
483 * list of all parsed extensions previously collected by
484 * tls_collect_extensions(). The parser is only run if it is applicable for the
485 * given |context| and the parser has not already been run. If this is for a
486 * Certificate message, then we also provide the parser with the relevant
487 * Certificate |x| and its position in the |chainidx| with 0 being the first
488 * Certificate. Returns 1 on success or 0 on failure. In the event of a failure
489 * |*al| is populated with a suitable alert code. If an extension is not present
490 * this counted as success.
491 */
492 int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
493 RAW_EXTENSION *exts, X509 *x, size_t chainidx, int *al)
494 {
495 RAW_EXTENSION *currext = &exts[idx];
496 int (*parser)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
497 size_t chainidx, int *al) = NULL;
498
499 /* Skip if the extension is not present */
500 if (!currext->present)
501 return 1;
502
503 if (s->ext.debug_cb)
504 s->ext.debug_cb(s, !s->server, currext->type,
505 PACKET_data(&currext->data),
506 PACKET_remaining(&currext->data),
507 s->ext.debug_arg);
508
509 /* Skip if we've already parsed this extension */
510 if (currext->parsed)
511 return 1;
512
513 currext->parsed = 1;
514
515 if (idx < OSSL_NELEM(ext_defs)) {
516 /* We are handling a built-in extension */
517 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
518
519 /* Check if extension is defined for our protocol. If not, skip */
520 if (!extension_is_relevant(s, extdef->context, context))
521 return 1;
522
523 parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
524
525 if (parser != NULL)
526 return parser(s, &currext->data, context, x, chainidx, al);
527
528 /*
529 * If the parser is NULL we fall through to the custom extension
530 * processing
531 */
532 }
533
534 /*
535 * This is a custom extension. We only allow this if it is a non
536 * resumed session on the server side.
537 *chain
538 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
539 * We're going to need a new mechanism for TLS1.3 to specify which
540 * messages to add the custom extensions to.
541 */
542 if ((!s->hit || !s->server)
543 && (context
544 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
545 && custom_ext_parse(s, s->server, currext->type,
546 PACKET_data(&currext->data),
547 PACKET_remaining(&currext->data),
548 al) <= 0)
549 return 0;
550
551 return 1;
552 }
553
554 /*
555 * Parse all remaining extensions that have not yet been parsed. Also calls the
556 * finalisation for all extensions at the end, whether we collected them or not.
557 * Returns 1 for success or 0 for failure. If we are working on a Certificate
558 * message then we also pass the Certificate |x| and its position in the
559 * |chainidx|, with 0 being the first certificate. On failure, |*al| is
560 * populated with a suitable alert code.
561 */
562 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, X509 *x,
563 size_t chainidx, int *al)
564 {
565 size_t i, numexts = OSSL_NELEM(ext_defs);
566 const EXTENSION_DEFINITION *thisexd;
567
568 /* Calculate the number of extensions in the extensions list */
569 if ((context & EXT_CLIENT_HELLO) != 0) {
570 numexts += s->cert->srv_ext.meths_count;
571 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
572 numexts += s->cert->cli_ext.meths_count;
573 }
574
575 /* Parse each extension in turn */
576 for (i = 0; i < numexts; i++) {
577 if (!tls_parse_extension(s, i, context, exts, x, chainidx, al))
578 return 0;
579 }
580
581 /*
582 * Finalise all known extensions relevant to this context, whether we have
583 * found them or not
584 */
585 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
586 if(thisexd->final != NULL
587 && (thisexd->context & context) != 0
588 && !thisexd->final(s, context, exts[i].present, al))
589 return 0;
590 }
591
592 return 1;
593 }
594
595 /*
596 * Construct all the extensions relevant to the current |context| and write
597 * them to |pkt|. If this is an extension for a Certificate in a Certificate
598 * message, then |x| will be set to the Certificate we are handling, and
599 * |chainidx| will indicate the position in the chainidx we are processing (with
600 * 0 being the first in the chain). Returns 1 on success or 0 on failure. If a
601 * failure occurs then |al| is populated with a suitable alert code. On a
602 * failure construction stops at the first extension to fail to construct.
603 */
604 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
605 X509 *x, size_t chainidx, int *al)
606 {
607 size_t i;
608 int addcustom = 0, min_version, max_version = 0, reason, tmpal;
609 const EXTENSION_DEFINITION *thisexd;
610
611 /*
612 * Normally if something goes wrong during construction it's an internal
613 * error. We can always override this later.
614 */
615 tmpal = SSL_AD_INTERNAL_ERROR;
616
617 if (!WPACKET_start_sub_packet_u16(pkt)
618 /*
619 * If extensions are of zero length then we don't even add the
620 * extensions length bytes to a ClientHello/ServerHello in SSLv3
621 */
622 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
623 && s->version == SSL3_VERSION
624 && !WPACKET_set_flags(pkt,
625 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
626 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
627 goto err;
628 }
629
630 if ((context & EXT_CLIENT_HELLO) != 0) {
631 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
632 if (reason != 0) {
633 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
634 goto err;
635 }
636 }
637
638 /* Add custom extensions first */
639 if ((context & EXT_CLIENT_HELLO) != 0) {
640 custom_ext_init(&s->cert->cli_ext);
641 addcustom = 1;
642 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
643 /*
644 * We already initialised the custom extensions during ClientHello
645 * parsing.
646 *
647 * TODO(TLS1.3): We're going to need a new custom extension mechanism
648 * for TLS1.3, so that custom extensions can specify which of the
649 * multiple message they wish to add themselves to.
650 */
651 addcustom = 1;
652 }
653
654 if (addcustom && !custom_ext_add(s, s->server, pkt, &tmpal)) {
655 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
656 goto err;
657 }
658
659 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
660 int (*construct)(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
661 size_t chainidx, int *al);
662
663 /* Skip if not relevant for our context */
664 if ((thisexd->context & context) == 0)
665 continue;
666
667 construct = s->server ? thisexd->construct_stoc
668 : thisexd->construct_ctos;
669
670 /* Check if this extension is defined for our protocol. If not, skip */
671 if ((SSL_IS_DTLS(s)
672 && (thisexd->context & EXT_TLS_IMPLEMENTATION_ONLY)
673 != 0)
674 || (s->version == SSL3_VERSION
675 && (thisexd->context & EXT_SSL3_ALLOWED) == 0)
676 || (SSL_IS_TLS13(s)
677 && (thisexd->context & EXT_TLS1_2_AND_BELOW_ONLY)
678 != 0)
679 || (!SSL_IS_TLS13(s)
680 && (thisexd->context & EXT_TLS1_3_ONLY) != 0
681 && (context & EXT_CLIENT_HELLO) == 0)
682 || ((thisexd->context & EXT_TLS1_3_ONLY) != 0
683 && (context & EXT_CLIENT_HELLO) != 0
684 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION))
685 || construct == NULL)
686 continue;
687
688 if (!construct(s, pkt, context, x, chainidx, &tmpal))
689 goto err;
690 }
691
692 if (!WPACKET_close(pkt)) {
693 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
694 goto err;
695 }
696
697 return 1;
698
699 err:
700 *al = tmpal;
701 return 0;
702 }
703
704 /*
705 * Built in extension finalisation and initialisation functions. All initialise
706 * or finalise the associated extension type for the given |context|. For
707 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
708 * otherwise. These functions return 1 on success or 0 on failure. In the event
709 * of a failure then |*al| is populated with a suitable error code.
710 */
711
712 static int final_renegotiate(SSL *s, unsigned int context, int sent,
713 int *al)
714 {
715 if (!s->server) {
716 /*
717 * Check if we can connect to a server that doesn't support safe
718 * renegotiation
719 */
720 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
721 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
722 && !sent) {
723 *al = SSL_AD_HANDSHAKE_FAILURE;
724 SSLerr(SSL_F_FINAL_RENEGOTIATE,
725 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
726 return 0;
727 }
728
729 return 1;
730 }
731
732 /* Need RI if renegotiating */
733 if (s->renegotiate
734 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
735 && !sent) {
736 *al = SSL_AD_HANDSHAKE_FAILURE;
737 SSLerr(SSL_F_FINAL_RENEGOTIATE,
738 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
739 return 0;
740 }
741
742
743 return 1;
744 }
745
746 static int init_server_name(SSL *s, unsigned int context)
747 {
748 if (s->server)
749 s->servername_done = 0;
750
751 return 1;
752 }
753
754 static int final_server_name(SSL *s, unsigned int context, int sent,
755 int *al)
756 {
757 int ret = SSL_TLSEXT_ERR_NOACK;
758 int altmp = SSL_AD_UNRECOGNIZED_NAME;
759
760 if (s->ctx != NULL && s->ctx->ext.servername_cb != 0)
761 ret = s->ctx->ext.servername_cb(s, &altmp,
762 s->ctx->ext.servername_arg);
763 else if (s->session_ctx != NULL
764 && s->session_ctx->ext.servername_cb != 0)
765 ret = s->session_ctx->ext.servername_cb(s, &altmp,
766 s->session_ctx->ext.servername_arg);
767
768 switch (ret) {
769 case SSL_TLSEXT_ERR_ALERT_FATAL:
770 *al = altmp;
771 return 0;
772
773 case SSL_TLSEXT_ERR_ALERT_WARNING:
774 *al = altmp;
775 return 1;
776
777 case SSL_TLSEXT_ERR_NOACK:
778 s->servername_done = 0;
779 return 1;
780
781 default:
782 return 1;
783 }
784 }
785
786 #ifndef OPENSSL_NO_EC
787 static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
788 int *al)
789 {
790 unsigned long alg_k, alg_a;
791
792 if (s->server)
793 return 1;
794
795 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
796 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
797
798 /*
799 * If we are client and using an elliptic curve cryptography cipher
800 * suite, then if server returns an EC point formats lists extension it
801 * must contain uncompressed.
802 */
803 if (s->ext.ecpointformats != NULL
804 && s->ext.ecpointformats_len > 0
805 && s->session->ext.ecpointformats != NULL
806 && s->session->ext.ecpointformats_len > 0
807 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
808 /* we are using an ECC cipher */
809 size_t i;
810 unsigned char *list = s->session->ext.ecpointformats;
811
812 for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
813 if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
814 break;
815 }
816 if (i == s->session->ext.ecpointformats_len) {
817 SSLerr(SSL_F_FINAL_EC_PT_FORMATS,
818 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
819 return 0;
820 }
821 }
822
823 return 1;
824 }
825 #endif
826
827 static int init_session_ticket(SSL *s, unsigned int context)
828 {
829 if (!s->server)
830 s->ext.ticket_expected = 0;
831
832 return 1;
833 }
834
835 #ifndef OPENSSL_NO_OCSP
836 static int init_status_request(SSL *s, unsigned int context)
837 {
838 if (s->server) {
839 s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
840 } else {
841 /*
842 * Ensure we get sensible values passed to tlsext_status_cb in the event
843 * that we don't receive a status message
844 */
845 OPENSSL_free(s->ext.ocsp.resp);
846 s->ext.ocsp.resp = NULL;
847 s->ext.ocsp.resp_len = 0;
848 }
849
850 return 1;
851 }
852 #endif
853
854 #ifndef OPENSSL_NO_NEXTPROTONEG
855 static int init_npn(SSL *s, unsigned int context)
856 {
857 s->s3->npn_seen = 0;
858
859 return 1;
860 }
861 #endif
862
863 static int init_alpn(SSL *s, unsigned int context)
864 {
865 OPENSSL_free(s->s3->alpn_selected);
866 s->s3->alpn_selected = NULL;
867 if (s->server) {
868 s->s3->alpn_selected_len = 0;
869 OPENSSL_free(s->s3->alpn_proposed);
870 s->s3->alpn_proposed = NULL;
871 s->s3->alpn_proposed_len = 0;
872 }
873 return 1;
874 }
875
876 static int final_alpn(SSL *s, unsigned int context, int sent, int *al)
877 {
878 const unsigned char *selected = NULL;
879 unsigned char selected_len = 0;
880
881 if (!s->server)
882 return 1;
883
884 if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
885 int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
886 s->s3->alpn_proposed,
887 (unsigned int)s->s3->alpn_proposed_len,
888 s->ctx->ext.alpn_select_cb_arg);
889
890 if (r == SSL_TLSEXT_ERR_OK) {
891 OPENSSL_free(s->s3->alpn_selected);
892 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
893 if (s->s3->alpn_selected == NULL) {
894 *al = SSL_AD_INTERNAL_ERROR;
895 return 0;
896 }
897 s->s3->alpn_selected_len = selected_len;
898 #ifndef OPENSSL_NO_NEXTPROTONEG
899 /* ALPN takes precedence over NPN. */
900 s->s3->npn_seen = 0;
901 #endif
902 } else {
903 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
904 return 0;
905 }
906 }
907
908 return 1;
909 }
910
911 static int init_sig_algs(SSL *s, unsigned int context)
912 {
913 /* Clear any signature algorithms extension received */
914 OPENSSL_free(s->s3->tmp.peer_sigalgs);
915 s->s3->tmp.peer_sigalgs = NULL;
916
917 return 1;
918 }
919
920 #ifndef OPENSSL_NO_SRP
921 static int init_srp(SSL *s, unsigned int context)
922 {
923 OPENSSL_free(s->srp_ctx.login);
924 s->srp_ctx.login = NULL;
925
926 return 1;
927 }
928 #endif
929
930 static int init_etm(SSL *s, unsigned int context)
931 {
932 s->ext.use_etm = 0;
933
934 return 1;
935 }
936
937 static int init_ems(SSL *s, unsigned int context)
938 {
939 if (!s->server)
940 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
941
942 return 1;
943 }
944
945 static int final_ems(SSL *s, unsigned int context, int sent, int *al)
946 {
947 if (!s->server && s->hit) {
948 /*
949 * Check extended master secret extension is consistent with
950 * original session.
951 */
952 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
953 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
954 *al = SSL_AD_HANDSHAKE_FAILURE;
955 SSLerr(SSL_F_FINAL_EMS, SSL_R_INCONSISTENT_EXTMS);
956 return 0;
957 }
958 }
959
960 return 1;
961 }
962
963 #ifndef OPENSSL_NO_SRTP
964 static int init_srtp(SSL *s, unsigned int context)
965 {
966 if (s->server)
967 s->srtp_profile = NULL;
968
969 return 1;
970 }
971 #endif
972
973 static int final_sig_algs(SSL *s, unsigned int context, int sent, int *al)
974 {
975 if (!sent && SSL_IS_TLS13(s)) {
976 *al = TLS13_AD_MISSING_EXTENSION;
977 SSLerr(SSL_F_FINAL_SIG_ALGS, SSL_R_MISSING_SIGALGS_EXTENSION);
978 return 0;
979 }
980
981 return 1;
982 }
983
984 #ifndef OPENSSL_NO_EC
985 static int final_key_share(SSL *s, unsigned int context, int sent, int *al)
986 {
987 if (!SSL_IS_TLS13(s))
988 return 1;
989
990 /*
991 * If
992 * we are a client
993 * AND
994 * we have no key_share
995 * AND
996 * (we are not resuming
997 * OR the kex_mode doesn't allow non key_share resumes)
998 * THEN
999 * fail;
1000 */
1001 if (!s->server
1002 && !sent
1003 && (!s->hit
1004 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
1005 /* Nothing left we can do - just fail */
1006 *al = SSL_AD_HANDSHAKE_FAILURE;
1007 SSLerr(SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
1008 return 0;
1009 }
1010 /*
1011 * If
1012 * we are a server
1013 * AND
1014 * we have no key_share
1015 * THEN
1016 * If
1017 * we didn't already send a HelloRetryRequest
1018 * AND
1019 * the client sent a key_share extension
1020 * AND
1021 * (we are not resuming
1022 * OR the kex_mode allows key_share resumes)
1023 * AND
1024 * a shared group exists
1025 * THEN
1026 * send a HelloRetryRequest
1027 * ELSE If
1028 * we are not resuming
1029 * OR
1030 * the kex_mode doesn't allow non key_share resumes
1031 * THEN
1032 * fail;
1033 */
1034 if (s->server && s->s3->peer_tmp == NULL) {
1035 /* No suitable share */
1036 if (s->hello_retry_request == 0 && sent
1037 && (!s->hit
1038 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
1039 != 0)) {
1040 const unsigned char *pcurves, *pcurvestmp, *clntcurves;
1041 size_t num_curves, clnt_num_curves, i;
1042 unsigned int group_id = 0;
1043
1044 /* Check if a shared group exists */
1045
1046 /* Get the clients list of supported groups. */
1047 if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) {
1048 *al = SSL_AD_INTERNAL_ERROR;
1049 SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1050 return 0;
1051 }
1052
1053 /* Get our list of available groups */
1054 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
1055 *al = SSL_AD_INTERNAL_ERROR;
1056 SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1057 return 0;
1058 }
1059
1060 /* Find the first group we allow that is also in client's list */
1061 for (i = 0, pcurvestmp = pcurves; i < num_curves;
1062 i++, pcurvestmp += 2) {
1063 group_id = bytestogroup(pcurvestmp);
1064
1065 if (check_in_list(s, group_id, clntcurves, clnt_num_curves, 1))
1066 break;
1067 }
1068
1069 if (i < num_curves) {
1070 /* A shared group exists so send a HelloRetryRequest */
1071 s->s3->group_id = group_id;
1072 s->hello_retry_request = 1;
1073 return 1;
1074 }
1075 }
1076 if (!s->hit
1077 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1078 /* Nothing left we can do - just fail */
1079 *al = SSL_AD_HANDSHAKE_FAILURE;
1080 SSLerr(SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
1081 return 0;
1082 }
1083 }
1084
1085 /* We have a key_share so don't send any more HelloRetryRequest messages */
1086 if (s->server)
1087 s->hello_retry_request = 0;
1088
1089 /*
1090 * For a client side resumption with no key_share we need to generate
1091 * the handshake secret (otherwise this is done during key_share
1092 * processing).
1093 */
1094 if (!sent && !s->server && !tls13_generate_handshake_secret(s, NULL, 0)) {
1095 *al = SSL_AD_INTERNAL_ERROR;
1096 SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1097 return 0;
1098 }
1099
1100 return 1;
1101 }
1102 #endif
1103
1104 static int init_psk_kex_modes(SSL *s, unsigned int context)
1105 {
1106 s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
1107 return 1;
1108 }
1109
1110 int tls_psk_do_binder(SSL *s, const EVP_MD *md, const unsigned char *msgstart,
1111 size_t binderoffset, const unsigned char *binderin,
1112 unsigned char *binderout,
1113 SSL_SESSION *sess, int sign)
1114 {
1115 EVP_PKEY *mackey = NULL;
1116 EVP_MD_CTX *mctx = NULL;
1117 unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1118 unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
1119 const char resumption_label[] = "resumption psk binder key";
1120 size_t bindersize, hashsize = EVP_MD_size(md);
1121 int ret = -1;
1122
1123 /* Generate the early_secret */
1124 if (!tls13_generate_secret(s, md, NULL, sess->master_key,
1125 sess->master_key_length,
1126 (unsigned char *)&s->early_secret)) {
1127 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1128 goto err;
1129 }
1130
1131 /*
1132 * Create the handshake hash for the binder key...the messages so far are
1133 * empty!
1134 */
1135 mctx = EVP_MD_CTX_new();
1136 if (mctx == NULL
1137 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1138 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1139 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1140 goto err;
1141 }
1142
1143 /* Generate the binder key */
1144 if (!tls13_hkdf_expand(s, md, s->early_secret,
1145 (unsigned char *)resumption_label,
1146 sizeof(resumption_label) - 1, hash, binderkey,
1147 hashsize)) {
1148 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1149 goto err;
1150 }
1151
1152 /* Generate the finished key */
1153 if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
1154 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1155 goto err;
1156 }
1157
1158 if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
1159 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1160 goto err;
1161 }
1162
1163 /*
1164 * Get a hash of the ClientHello up to the start of the binders. If we are
1165 * following a HelloRetryRequest then this includes the hash of the first
1166 * ClientHello and the HelloRetryRequest itself.
1167 */
1168 if (s->hello_retry_request) {
1169 size_t hdatalen;
1170 void *hdata;
1171
1172 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
1173 if (hdatalen <= 0) {
1174 SSLerr(SSL_F_TLS_PSK_DO_BINDER, SSL_R_BAD_HANDSHAKE_LENGTH);
1175 goto err;
1176 }
1177
1178 /*
1179 * For servers the handshake buffer data will include the second
1180 * ClientHello - which we don't want - so we need to take that bit off.
1181 */
1182 if (s->server) {
1183 if (hdatalen < s->init_num + SSL3_HM_HEADER_LENGTH) {
1184 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1185 goto err;
1186 }
1187 hdatalen -= s->init_num + SSL3_HM_HEADER_LENGTH;
1188 }
1189
1190 if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
1191 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1192 goto err;
1193 }
1194 }
1195
1196 if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1197 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1198 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1199 goto err;
1200 }
1201
1202 mackey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, finishedkey, hashsize);
1203 if (mackey == NULL) {
1204 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1205 goto err;
1206 }
1207
1208 if (!sign)
1209 binderout = tmpbinder;
1210
1211 bindersize = hashsize;
1212 if (EVP_DigestSignInit(mctx, NULL, md, NULL, mackey) <= 0
1213 || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1214 || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1215 || bindersize != hashsize) {
1216 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1217 goto err;
1218 }
1219
1220 if (sign) {
1221 ret = 1;
1222 } else {
1223 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1224 ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
1225 }
1226
1227 err:
1228 OPENSSL_cleanse(binderkey, sizeof(binderkey));
1229 OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1230 EVP_PKEY_free(mackey);
1231 EVP_MD_CTX_free(mctx);
1232
1233 return ret;
1234 }
1235
1236 static int final_early_data(SSL *s, unsigned int context, int sent, int *al)
1237 {
1238 if (!s->server || !sent)
1239 return 1;
1240
1241 if (s->max_early_data == 0
1242 || !s->hit
1243 || s->session->ext.tick_identity != 0
1244 || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1245 || !s->ext.early_data_ok
1246 || s->hello_retry_request
1247 || s->s3->alpn_selected_len != s->session->ext.alpn_selected_len
1248 || memcmp(s->s3->alpn_selected, s->session->ext.alpn_selected,
1249 s->s3->alpn_selected_len) != 0){
1250 s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1251 } else {
1252 s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1253
1254 if (!tls13_change_cipher_state(s,
1255 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
1256 *al = SSL_AD_INTERNAL_ERROR;
1257 return 0;
1258 }
1259 }
1260
1261 return 1;
1262 }