]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/extensions_clnt.c
Add client side support for parsing Hello Retry Request
[thirdparty/openssl.git] / ssl / statem / extensions_clnt.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 <assert.h>
11 #include <openssl/ocsp.h>
12 #include "../ssl_locl.h"
13 #include "statem_locl.h"
14
15 int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
16 X509 *x, size_t chainidx, int *al)
17 {
18 /* Add RI if renegotiating */
19 if (!s->renegotiate)
20 return 1;
21
22 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
23 || !WPACKET_start_sub_packet_u16(pkt)
24 || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
25 s->s3->previous_client_finished_len)
26 || !WPACKET_close(pkt)) {
27 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
28 return 0;
29 }
30
31 return 1;
32 }
33
34 int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, unsigned int context,
35 X509 *x, size_t chainidx, int *al)
36 {
37 if (s->ext.hostname == NULL)
38 return 1;
39
40 /* Add TLS extension servername to the Client Hello message */
41 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
42 /* Sub-packet for server_name extension */
43 || !WPACKET_start_sub_packet_u16(pkt)
44 /* Sub-packet for servername list (always 1 hostname)*/
45 || !WPACKET_start_sub_packet_u16(pkt)
46 || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
47 || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
48 strlen(s->ext.hostname))
49 || !WPACKET_close(pkt)
50 || !WPACKET_close(pkt)) {
51 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SERVER_NAME, ERR_R_INTERNAL_ERROR);
52 return 0;
53 }
54
55 return 1;
56 }
57
58 #ifndef OPENSSL_NO_SRP
59 int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
60 size_t chainidx, int *al)
61 {
62 /* Add SRP username if there is one */
63 if (s->srp_ctx.login == NULL)
64 return 1;
65
66 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
67 /* Sub-packet for SRP extension */
68 || !WPACKET_start_sub_packet_u16(pkt)
69 || !WPACKET_start_sub_packet_u8(pkt)
70 /* login must not be zero...internal error if so */
71 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
72 || !WPACKET_memcpy(pkt, s->srp_ctx.login,
73 strlen(s->srp_ctx.login))
74 || !WPACKET_close(pkt)
75 || !WPACKET_close(pkt)) {
76 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SRP, ERR_R_INTERNAL_ERROR);
77 return 0;
78 }
79
80 return 1;
81 }
82 #endif
83
84 #ifndef OPENSSL_NO_EC
85 static int use_ecc(SSL *s)
86 {
87 int i, end;
88 unsigned long alg_k, alg_a;
89 STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
90
91 /* See if we support any ECC ciphersuites */
92 if (s->version == SSL3_VERSION)
93 return 0;
94
95 cipher_stack = SSL_get_ciphers(s);
96 end = sk_SSL_CIPHER_num(cipher_stack);
97 for (i = 0; i < end; i++) {
98 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
99
100 alg_k = c->algorithm_mkey;
101 alg_a = c->algorithm_auth;
102 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
103 || (alg_a & SSL_aECDSA)
104 || c->min_tls >= TLS1_3_VERSION)
105 break;
106 }
107
108 return i < end;
109 }
110
111 int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
112 X509 *x, size_t chainidx, int *al)
113 {
114 const unsigned char *pformats;
115 size_t num_formats;
116
117 if (!use_ecc(s))
118 return 1;
119
120 /* Add TLS extension ECPointFormats to the ClientHello message */
121 tls1_get_formatlist(s, &pformats, &num_formats);
122
123 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
124 /* Sub-packet for formats extension */
125 || !WPACKET_start_sub_packet_u16(pkt)
126 || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
127 || !WPACKET_close(pkt)) {
128 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
129 return 0;
130 }
131
132 return 1;
133 }
134
135 int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
136 unsigned int context, X509 *x,
137 size_t chainidx, int *al)
138 {
139 const unsigned char *pcurves = NULL, *pcurvestmp;
140 size_t num_curves = 0, i;
141
142 if (!use_ecc(s))
143 return 1;
144
145 /*
146 * Add TLS extension supported_groups to the ClientHello message
147 */
148 /* TODO(TLS1.3): Add support for DHE groups */
149 pcurves = s->ext.supportedgroups;
150 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
151 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
152 ERR_R_INTERNAL_ERROR);
153 return 0;
154 }
155 pcurvestmp = pcurves;
156
157 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
158 /* Sub-packet for supported_groups extension */
159 || !WPACKET_start_sub_packet_u16(pkt)
160 || !WPACKET_start_sub_packet_u16(pkt)) {
161 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
162 ERR_R_INTERNAL_ERROR);
163 return 0;
164 }
165 /* Copy curve ID if supported */
166 for (i = 0; i < num_curves; i++, pcurvestmp += 2) {
167 if (tls_curve_allowed(s, pcurvestmp, SSL_SECOP_CURVE_SUPPORTED)) {
168 if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0])
169 || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) {
170 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
171 ERR_R_INTERNAL_ERROR);
172 return 0;
173 }
174 }
175 }
176 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
177 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
178 ERR_R_INTERNAL_ERROR);
179 return 0;
180 }
181
182 return 1;
183 }
184 #endif
185
186 int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
187 unsigned int context, X509 *x,
188 size_t chainidx, int *al)
189 {
190 size_t ticklen;
191
192 if (!tls_use_ticket(s))
193 return 1;
194
195 if (!s->new_session && s->session != NULL
196 && s->session->ext.tick != NULL
197 && s->session->ssl_version != TLS1_3_VERSION) {
198 ticklen = s->session->ext.ticklen;
199 } else if (s->session && s->ext.session_ticket != NULL
200 && s->ext.session_ticket->data != NULL) {
201 ticklen = s->ext.session_ticket->length;
202 s->session->ext.tick = OPENSSL_malloc(ticklen);
203 if (s->session->ext.tick == NULL) {
204 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET,
205 ERR_R_INTERNAL_ERROR);
206 return 0;
207 }
208 memcpy(s->session->ext.tick,
209 s->ext.session_ticket->data, ticklen);
210 s->session->ext.ticklen = ticklen;
211 } else {
212 ticklen = 0;
213 }
214
215 if (ticklen == 0 && s->ext.session_ticket != NULL &&
216 s->ext.session_ticket->data == NULL)
217 return 1;
218
219 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
220 || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
221 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
222 return 0;
223 }
224
225 return 1;
226 }
227
228 int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, unsigned int context,
229 X509 *x, size_t chainidx, int *al)
230 {
231 size_t salglen;
232 const uint16_t *salg;
233
234 if (!SSL_CLIENT_USE_SIGALGS(s))
235 return 1;
236
237 salglen = tls12_get_psigalgs(s, 1, &salg);
238 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
239 /* Sub-packet for sig-algs extension */
240 || !WPACKET_start_sub_packet_u16(pkt)
241 /* Sub-packet for the actual list */
242 || !WPACKET_start_sub_packet_u16(pkt)
243 || !tls12_copy_sigalgs(s, pkt, salg, salglen)
244 || !WPACKET_close(pkt)
245 || !WPACKET_close(pkt)) {
246 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS, ERR_R_INTERNAL_ERROR);
247 return 0;
248 }
249
250 return 1;
251 }
252
253 #ifndef OPENSSL_NO_OCSP
254 int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
255 unsigned int context, X509 *x,
256 size_t chainidx, int *al)
257 {
258 int i;
259
260 /* This extension isn't defined for client Certificates */
261 if (x != NULL)
262 return 1;
263
264 if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
265 return 1;
266
267 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
268 /* Sub-packet for status request extension */
269 || !WPACKET_start_sub_packet_u16(pkt)
270 || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
271 /* Sub-packet for the ids */
272 || !WPACKET_start_sub_packet_u16(pkt)) {
273 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
274 return 0;
275 }
276 for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
277 unsigned char *idbytes;
278 OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
279 int idlen = i2d_OCSP_RESPID(id, NULL);
280
281 if (idlen <= 0
282 /* Sub-packet for an individual id */
283 || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
284 || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
285 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
286 ERR_R_INTERNAL_ERROR);
287 return 0;
288 }
289 }
290 if (!WPACKET_close(pkt)
291 || !WPACKET_start_sub_packet_u16(pkt)) {
292 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
293 return 0;
294 }
295 if (s->ext.ocsp.exts) {
296 unsigned char *extbytes;
297 int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
298
299 if (extlen < 0) {
300 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
301 ERR_R_INTERNAL_ERROR);
302 return 0;
303 }
304 if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
305 || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
306 != extlen) {
307 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
308 ERR_R_INTERNAL_ERROR);
309 return 0;
310 }
311 }
312 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
313 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
314 return 0;
315 }
316
317 return 1;
318 }
319 #endif
320
321 #ifndef OPENSSL_NO_NEXTPROTONEG
322 int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
323 size_t chainidx, int *al)
324 {
325 if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
326 return 1;
327
328 /*
329 * The client advertises an empty extension to indicate its support
330 * for Next Protocol Negotiation
331 */
332 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
333 || !WPACKET_put_bytes_u16(pkt, 0)) {
334 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_NPN, ERR_R_INTERNAL_ERROR);
335 return 0;
336 }
337
338 return 1;
339 }
340 #endif
341
342 int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
343 size_t chainidx, int *al)
344 {
345 s->s3->alpn_sent = 0;
346
347 if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
348 return 1;
349
350 if (!WPACKET_put_bytes_u16(pkt,
351 TLSEXT_TYPE_application_layer_protocol_negotiation)
352 /* Sub-packet ALPN extension */
353 || !WPACKET_start_sub_packet_u16(pkt)
354 || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
355 || !WPACKET_close(pkt)) {
356 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ALPN, ERR_R_INTERNAL_ERROR);
357 return 0;
358 }
359 s->s3->alpn_sent = 1;
360
361 return 1;
362 }
363
364
365 #ifndef OPENSSL_NO_SRTP
366 int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
367 X509 *x, size_t chainidx, int *al)
368 {
369 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
370 int i, end;
371
372 if (clnt == NULL)
373 return 1;
374
375 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
376 /* Sub-packet for SRTP extension */
377 || !WPACKET_start_sub_packet_u16(pkt)
378 /* Sub-packet for the protection profile list */
379 || !WPACKET_start_sub_packet_u16(pkt)) {
380 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
381 return 0;
382 }
383
384 end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
385 for (i = 0; i < end; i++) {
386 const SRTP_PROTECTION_PROFILE *prof =
387 sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
388
389 if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
390 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
391 return 0;
392 }
393 }
394 if (!WPACKET_close(pkt)
395 /* Add an empty use_mki value */
396 || !WPACKET_put_bytes_u8(pkt, 0)
397 || !WPACKET_close(pkt)) {
398 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
399 return 0;
400 }
401
402 return 1;
403 }
404 #endif
405
406 int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
407 size_t chainidx, int *al)
408 {
409 if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
410 return 1;
411
412 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
413 || !WPACKET_put_bytes_u16(pkt, 0)) {
414 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_ETM, ERR_R_INTERNAL_ERROR);
415 return 0;
416 }
417
418 return 1;
419 }
420
421 #ifndef OPENSSL_NO_CT
422 int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
423 size_t chainidx, int *al)
424 {
425 if (s->ct_validation_callback == NULL)
426 return 1;
427
428 /* Not defined for client Certificates */
429 if (x != NULL)
430 return 1;
431
432 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
433 || !WPACKET_put_bytes_u16(pkt, 0)) {
434 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SCT, ERR_R_INTERNAL_ERROR);
435 return 0;
436 }
437
438 return 1;
439 }
440 #endif
441
442 int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
443 size_t chainidx, int *al)
444 {
445 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
446 || !WPACKET_put_bytes_u16(pkt, 0)) {
447 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_EMS, ERR_R_INTERNAL_ERROR);
448 return 0;
449 }
450
451 return 1;
452 }
453
454 int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
455 unsigned int context, X509 *x,
456 size_t chainidx, int *al)
457 {
458 int currv, min_version, max_version, reason;
459
460 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
461 || !WPACKET_start_sub_packet_u16(pkt)
462 || !WPACKET_start_sub_packet_u8(pkt)) {
463 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
464 ERR_R_INTERNAL_ERROR);
465 return 0;
466 }
467
468 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
469 if (reason != 0) {
470 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS, reason);
471 return 0;
472 }
473
474 /*
475 * TODO(TLS1.3): There is some discussion on the TLS list as to wheter
476 * we should include versions <TLS1.2. For the moment we do. To be
477 * reviewed later.
478 */
479 for (currv = max_version; currv >= min_version; currv--) {
480 /* TODO(TLS1.3): Remove this first if clause prior to release!! */
481 if (currv == TLS1_3_VERSION) {
482 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
483 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
484 ERR_R_INTERNAL_ERROR);
485 return 0;
486 }
487 } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
488 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
489 ERR_R_INTERNAL_ERROR);
490 return 0;
491 }
492 }
493 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
494 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
495 ERR_R_INTERNAL_ERROR);
496 return 0;
497 }
498
499 return 1;
500 }
501
502 /*
503 * Construct a psk_kex_modes extension. We only have two modes we know about
504 * at this stage, so we send both.
505 */
506 int tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, unsigned int context,
507 X509 *x, size_t chainidx, int *al)
508 {
509 #ifndef OPENSSL_NO_TLS1_3
510 /*
511 * TODO(TLS1.3): Do we want this list to be configurable? For now we always
512 * just send both supported modes
513 */
514 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
515 || !WPACKET_start_sub_packet_u16(pkt)
516 || !WPACKET_start_sub_packet_u8(pkt)
517 || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
518 || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE)
519 || !WPACKET_close(pkt)
520 || !WPACKET_close(pkt)) {
521 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES, ERR_R_INTERNAL_ERROR);
522 return 0;
523 }
524
525 s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE;
526 #endif
527
528 return 1;
529 }
530
531 #ifndef OPENSSL_NO_TLS1_3
532 static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id)
533 {
534 unsigned char *encodedPoint = NULL;
535 EVP_PKEY *key_share_key = NULL;
536 size_t encodedlen;
537
538 key_share_key = ssl_generate_pkey_curve(curve_id);
539 if (key_share_key == NULL) {
540 SSLerr(SSL_F_ADD_KEY_SHARE, ERR_R_EVP_LIB);
541 return 0;
542 }
543
544 /* Encode the public key. */
545 encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
546 &encodedPoint);
547 if (encodedlen == 0) {
548 SSLerr(SSL_F_ADD_KEY_SHARE, ERR_R_EC_LIB);
549 EVP_PKEY_free(key_share_key);
550 return 0;
551 }
552
553 /* Create KeyShareEntry */
554 if (!WPACKET_put_bytes_u16(pkt, curve_id)
555 || !WPACKET_sub_memcpy_u16(pkt, encodedPoint, encodedlen)) {
556 SSLerr(SSL_F_ADD_KEY_SHARE, ERR_R_INTERNAL_ERROR);
557 EVP_PKEY_free(key_share_key);
558 OPENSSL_free(encodedPoint);
559 return 0;
560 }
561
562 /*
563 * TODO(TLS1.3): When changing to send more than one key_share we're
564 * going to need to be able to save more than one EVP_PKEY. For now
565 * we reuse the existing tmp.pkey
566 */
567 s->s3->tmp.pkey = key_share_key;
568 s->s3->group_id = curve_id;
569 OPENSSL_free(encodedPoint);
570
571 return 1;
572 }
573 #endif
574
575 int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, unsigned int context,
576 X509 *x, size_t chainidx, int *al)
577 {
578 #ifndef OPENSSL_NO_TLS1_3
579 size_t i, num_curves = 0;
580 const unsigned char *pcurves = NULL;
581 unsigned int curve_id = 0;
582
583 /* key_share extension */
584 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
585 /* Extension data sub-packet */
586 || !WPACKET_start_sub_packet_u16(pkt)
587 /* KeyShare list sub-packet */
588 || !WPACKET_start_sub_packet_u16(pkt)) {
589 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
590 return 0;
591 }
592
593 pcurves = s->ext.supportedgroups;
594 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
595 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
596 return 0;
597 }
598
599 if (s->s3->tmp.pkey != NULL) {
600 /* Shouldn't happen! */
601 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
602 return 0;
603 }
604
605 /*
606 * TODO(TLS1.3): Make the number of key_shares sent configurable. For
607 * now, just send one
608 */
609 if (s->s3->group_id != 0) {
610 curve_id = s->s3->group_id;
611 } else {
612 for (i = 0; i < num_curves; i++, pcurves += 2) {
613
614 if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED))
615 continue;
616
617 curve_id = (pcurves[0] << 8) | pcurves[1];
618 break;
619 }
620 }
621
622 if (curve_id == 0) {
623 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
624 return 0;
625 }
626
627 if (!add_key_share(s, pkt, curve_id))
628 return 0;
629
630 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
631 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
632 return 0;
633 }
634 #endif
635
636 return 1;
637 }
638
639 #define F5_WORKAROUND_MIN_MSG_LEN 0xff
640 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
641
642 int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, unsigned int context,
643 X509 *x, size_t chainidx, int *al)
644 {
645 unsigned char *padbytes;
646 size_t hlen;
647
648 if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
649 return 1;
650
651 /*
652 * Add padding to workaround bugs in F5 terminators. See
653 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
654 * code calculates the length of all existing extensions it MUST always
655 * appear last.
656 */
657 if (!WPACKET_get_total_written(pkt, &hlen)) {
658 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PADDING, ERR_R_INTERNAL_ERROR);
659 return 0;
660 }
661
662 if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
663 /* Calculate the amond of padding we need to add */
664 hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
665
666 /*
667 * Take off the size of extension header itself (2 bytes for type and
668 * 2 bytes for length bytes)
669 */
670 if (hlen >= 4)
671 hlen -= 4;
672 else
673 hlen = 0;
674
675 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
676 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
677 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PADDING, ERR_R_INTERNAL_ERROR);
678 return 0;
679 }
680 memset(padbytes, 0, hlen);
681 }
682
683 return 1;
684 }
685
686 /*
687 * Construct the pre_shared_key extension
688 */
689 int tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
690 size_t chainidx, int *al)
691 {
692 #ifndef OPENSSL_NO_TLS1_3
693 uint32_t now, agesec, agems;
694 size_t hashsize, binderoffset, msglen;
695 unsigned char *binder = NULL, *msgstart = NULL;
696 const EVP_MD *md;
697 int ret = 0;
698
699 s->session->ext.tick_identity = TLSEXT_PSK_BAD_IDENTITY;
700
701 /*
702 * If this is an incompatible or new session then we have nothing to resume
703 * so don't add this extension.
704 */
705 if (s->session->ssl_version != TLS1_3_VERSION
706 || s->session->ext.ticklen == 0)
707 return 1;
708
709 if (s->session->cipher == NULL) {
710 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK, ERR_R_INTERNAL_ERROR);
711 goto err;
712 }
713
714 md = ssl_md(s->session->cipher->algorithm2);
715 if (md == NULL) {
716 /* Don't recognise this cipher so we can't use the session. Ignore it */
717 return 1;
718 }
719
720 /*
721 * Technically the C standard just says time() returns a time_t and says
722 * nothing about the encoding of that type. In practice most implementations
723 * follow POSIX which holds it as an integral type in seconds since epoch.
724 * We've already made the assumption that we can do this in multiple places
725 * in the code, so portability shouldn't be an issue.
726 */
727 now = (uint32_t)time(NULL);
728 agesec = now - (uint32_t)s->session->time;
729
730 if (s->session->ext.tick_lifetime_hint < agesec) {
731 /* Ticket is too old. Ignore it. */
732 return 1;
733 }
734
735 /*
736 * Calculate age in ms. We're just doing it to nearest second. Should be
737 * good enough.
738 */
739 agems = agesec * (uint32_t)1000;
740
741 if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
742 /*
743 * Overflow. Shouldn't happen unless this is a *really* old session. If
744 * so we just ignore it.
745 */
746 return 1;
747 }
748
749 /*
750 * Obfuscate the age. Overflow here is fine, this addition is supposed to
751 * be mod 2^32.
752 */
753 agems += s->session->ext.tick_age_add;
754
755 hashsize = EVP_MD_size(md);
756
757 /* Create the extension, but skip over the binder for now */
758 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
759 || !WPACKET_start_sub_packet_u16(pkt)
760 || !WPACKET_start_sub_packet_u16(pkt)
761 || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
762 s->session->ext.ticklen)
763 || !WPACKET_put_bytes_u32(pkt, agems)
764 || !WPACKET_close(pkt)
765 || !WPACKET_get_total_written(pkt, &binderoffset)
766 || !WPACKET_start_sub_packet_u16(pkt)
767 || !WPACKET_sub_allocate_bytes_u8(pkt, hashsize, &binder)
768 || !WPACKET_close(pkt)
769 || !WPACKET_close(pkt)
770 || !WPACKET_get_total_written(pkt, &msglen)
771 /*
772 * We need to fill in all the sub-packet lengths now so we can
773 * calculate the HMAC of the message up to the binders
774 */
775 || !WPACKET_fill_lengths(pkt)) {
776 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK, ERR_R_INTERNAL_ERROR);
777 goto err;
778 }
779
780 msgstart = WPACKET_get_curr(pkt) - msglen;
781
782 if (tls_psk_do_binder(s, md, msgstart, binderoffset, NULL, binder,
783 s->session, 1) != 1) {
784 SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_PSK, ERR_R_INTERNAL_ERROR);
785 goto err;
786 }
787
788 s->session->ext.tick_identity = 0;
789
790 ret = 1;
791 err:
792 return ret;
793 #else
794 return 1;
795 #endif
796 }
797
798 /*
799 * Parse the server's renegotiation binding and abort if it's not right
800 */
801 int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
802 X509 *x, size_t chainidx, int *al)
803 {
804 size_t expected_len = s->s3->previous_client_finished_len
805 + s->s3->previous_server_finished_len;
806 size_t ilen;
807 const unsigned char *data;
808
809 /* Check for logic errors */
810 assert(expected_len == 0 || s->s3->previous_client_finished_len != 0);
811 assert(expected_len == 0 || s->s3->previous_server_finished_len != 0);
812
813 /* Parse the length byte */
814 if (!PACKET_get_1_len(pkt, &ilen)) {
815 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
816 SSL_R_RENEGOTIATION_ENCODING_ERR);
817 *al = SSL_AD_ILLEGAL_PARAMETER;
818 return 0;
819 }
820
821 /* Consistency check */
822 if (PACKET_remaining(pkt) != ilen) {
823 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
824 SSL_R_RENEGOTIATION_ENCODING_ERR);
825 *al = SSL_AD_ILLEGAL_PARAMETER;
826 return 0;
827 }
828
829 /* Check that the extension matches */
830 if (ilen != expected_len) {
831 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
832 SSL_R_RENEGOTIATION_MISMATCH);
833 *al = SSL_AD_HANDSHAKE_FAILURE;
834 return 0;
835 }
836
837 if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
838 || memcmp(data, s->s3->previous_client_finished,
839 s->s3->previous_client_finished_len) != 0) {
840 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
841 SSL_R_RENEGOTIATION_MISMATCH);
842 *al = SSL_AD_HANDSHAKE_FAILURE;
843 return 0;
844 }
845
846 if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
847 || memcmp(data, s->s3->previous_server_finished,
848 s->s3->previous_server_finished_len) != 0) {
849 SSLerr(SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
850 SSL_R_RENEGOTIATION_MISMATCH);
851 *al = SSL_AD_ILLEGAL_PARAMETER;
852 return 0;
853 }
854 s->s3->send_connection_binding = 1;
855
856 return 1;
857 }
858
859 int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
860 X509 *x, size_t chainidx, int *al)
861 {
862 if (s->ext.hostname == NULL || PACKET_remaining(pkt) > 0) {
863 *al = SSL_AD_UNRECOGNIZED_NAME;
864 return 0;
865 }
866
867 if (!s->hit) {
868 if (s->session->ext.hostname != NULL) {
869 *al = SSL_AD_INTERNAL_ERROR;
870 return 0;
871 }
872 s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
873 if (s->session->ext.hostname == NULL) {
874 *al = SSL_AD_INTERNAL_ERROR;
875 return 0;
876 }
877 }
878
879 return 1;
880 }
881
882 #ifndef OPENSSL_NO_EC
883 int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
884 X509 *x, size_t chainidx, int *al)
885 {
886 unsigned int ecpointformats_len;
887 PACKET ecptformatlist;
888
889 if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
890 *al = SSL_AD_DECODE_ERROR;
891 return 0;
892 }
893 if (!s->hit) {
894 ecpointformats_len = PACKET_remaining(&ecptformatlist);
895 s->session->ext.ecpointformats_len = 0;
896
897 OPENSSL_free(s->session->ext.ecpointformats);
898 s->session->ext.ecpointformats = OPENSSL_malloc(ecpointformats_len);
899 if (s->session->ext.ecpointformats == NULL) {
900 *al = SSL_AD_INTERNAL_ERROR;
901 return 0;
902 }
903
904 s->session->ext.ecpointformats_len = ecpointformats_len;
905
906 if (!PACKET_copy_bytes(&ecptformatlist,
907 s->session->ext.ecpointformats,
908 ecpointformats_len)) {
909 *al = SSL_AD_INTERNAL_ERROR;
910 return 0;
911 }
912 }
913
914 return 1;
915 }
916 #endif
917
918 int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
919 X509 *x, size_t chainidx, int *al)
920 {
921 if (s->ext.session_ticket_cb != NULL &&
922 !s->ext.session_ticket_cb(s, PACKET_data(pkt),
923 PACKET_remaining(pkt),
924 s->ext.session_ticket_cb_arg)) {
925 *al = SSL_AD_INTERNAL_ERROR;
926 return 0;
927 }
928
929 if (!tls_use_ticket(s) || PACKET_remaining(pkt) > 0) {
930 *al = SSL_AD_UNSUPPORTED_EXTENSION;
931 return 0;
932 }
933
934 s->ext.ticket_expected = 1;
935
936 return 1;
937 }
938
939 #ifndef OPENSSL_NO_OCSP
940 int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
941 X509 *x, size_t chainidx, int *al)
942 {
943 /*
944 * MUST only be sent if we've requested a status
945 * request message. In TLS <= 1.2 it must also be empty.
946 */
947 if (s->ext.status_type == TLSEXT_STATUSTYPE_nothing
948 || (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0)) {
949 *al = SSL_AD_UNSUPPORTED_EXTENSION;
950 return 0;
951 }
952
953 if (SSL_IS_TLS13(s)) {
954 /* We only know how to handle this if it's for the first Certificate in
955 * the chain. We ignore any other repsonses.
956 */
957 if (chainidx != 0)
958 return 1;
959 return tls_process_cert_status_body(s, pkt, al);
960 }
961
962 /* Set flag to expect CertificateStatus message */
963 s->ext.status_expected = 1;
964
965 return 1;
966 }
967 #endif
968
969
970 #ifndef OPENSSL_NO_CT
971 int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
972 size_t chainidx, int *al)
973 {
974 /*
975 * Only take it if we asked for it - i.e if there is no CT validation
976 * callback set, then a custom extension MAY be processing it, so we
977 * need to let control continue to flow to that.
978 */
979 if (s->ct_validation_callback != NULL) {
980 size_t size = PACKET_remaining(pkt);
981
982 /* Simply copy it off for later processing */
983 OPENSSL_free(s->ext.scts);
984 s->ext.scts = NULL;
985
986 s->ext.scts_len = size;
987 if (size > 0) {
988 s->ext.scts = OPENSSL_malloc(size);
989 if (s->ext.scts == NULL
990 || !PACKET_copy_bytes(pkt, s->ext.scts, size)) {
991 *al = SSL_AD_INTERNAL_ERROR;
992 return 0;
993 }
994 }
995 } else {
996 if (custom_ext_parse(s, 0, TLSEXT_TYPE_signed_certificate_timestamp,
997 PACKET_data(pkt), PACKET_remaining(pkt), al) <= 0)
998 return 0;
999 }
1000
1001 return 1;
1002 }
1003 #endif
1004
1005
1006 #ifndef OPENSSL_NO_NEXTPROTONEG
1007 /*
1008 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1009 * elements of zero length are allowed and the set of elements must exactly
1010 * fill the length of the block. Returns 1 on success or 0 on failure.
1011 */
1012 static int ssl_next_proto_validate(PACKET *pkt)
1013 {
1014 PACKET tmp_protocol;
1015
1016 while (PACKET_remaining(pkt)) {
1017 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
1018 || PACKET_remaining(&tmp_protocol) == 0)
1019 return 0;
1020 }
1021
1022 return 1;
1023 }
1024
1025 int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1026 size_t chainidx, int *al)
1027 {
1028 unsigned char *selected;
1029 unsigned char selected_len;
1030 PACKET tmppkt;
1031
1032 /* Check if we are in a renegotiation. If so ignore this extension */
1033 if (!SSL_IS_FIRST_HANDSHAKE(s))
1034 return 1;
1035
1036 /* We must have requested it. */
1037 if (s->ctx->ext.npn_select_cb == NULL) {
1038 *al = SSL_AD_UNSUPPORTED_EXTENSION;
1039 return 0;
1040 }
1041
1042 /* The data must be valid */
1043 tmppkt = *pkt;
1044 if (!ssl_next_proto_validate(&tmppkt)) {
1045 *al = SSL_AD_DECODE_ERROR;
1046 return 0;
1047 }
1048 if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len,
1049 PACKET_data(pkt),
1050 PACKET_remaining(pkt),
1051 s->ctx->ext.npn_select_cb_arg) !=
1052 SSL_TLSEXT_ERR_OK) {
1053 *al = SSL_AD_INTERNAL_ERROR;
1054 return 0;
1055 }
1056
1057 /*
1058 * Could be non-NULL if server has sent multiple NPN extensions in
1059 * a single Serverhello
1060 */
1061 OPENSSL_free(s->ext.npn);
1062 s->ext.npn = OPENSSL_malloc(selected_len);
1063 if (s->ext.npn == NULL) {
1064 *al = SSL_AD_INTERNAL_ERROR;
1065 return 0;
1066 }
1067
1068 memcpy(s->ext.npn, selected, selected_len);
1069 s->ext.npn_len = selected_len;
1070 s->s3->npn_seen = 1;
1071
1072 return 1;
1073 }
1074 #endif
1075
1076 int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1077 size_t chainidx, int *al)
1078 {
1079 size_t len;
1080
1081 /* We must have requested it. */
1082 if (!s->s3->alpn_sent) {
1083 *al = SSL_AD_UNSUPPORTED_EXTENSION;
1084 return 0;
1085 }
1086 /*-
1087 * The extension data consists of:
1088 * uint16 list_length
1089 * uint8 proto_length;
1090 * uint8 proto[proto_length];
1091 */
1092 if (!PACKET_get_net_2_len(pkt, &len)
1093 || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
1094 || PACKET_remaining(pkt) != len) {
1095 *al = SSL_AD_DECODE_ERROR;
1096 return 0;
1097 }
1098 OPENSSL_free(s->s3->alpn_selected);
1099 s->s3->alpn_selected = OPENSSL_malloc(len);
1100 if (s->s3->alpn_selected == NULL) {
1101 *al = SSL_AD_INTERNAL_ERROR;
1102 return 0;
1103 }
1104 if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
1105 *al = SSL_AD_DECODE_ERROR;
1106 return 0;
1107 }
1108 s->s3->alpn_selected_len = len;
1109
1110 return 1;
1111 }
1112
1113 #ifndef OPENSSL_NO_SRTP
1114 int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1115 size_t chainidx, int *al)
1116 {
1117 unsigned int id, ct, mki;
1118 int i;
1119 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1120 SRTP_PROTECTION_PROFILE *prof;
1121
1122 if (!PACKET_get_net_2(pkt, &ct) || ct != 2
1123 || !PACKET_get_net_2(pkt, &id)
1124 || !PACKET_get_1(pkt, &mki)
1125 || PACKET_remaining(pkt) != 0) {
1126 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP,
1127 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1128 *al = SSL_AD_DECODE_ERROR;
1129 return 0;
1130 }
1131
1132 if (mki != 0) {
1133 /* Must be no MKI, since we never offer one */
1134 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
1135 *al = SSL_AD_ILLEGAL_PARAMETER;
1136 return 0;
1137 }
1138
1139 /* Throw an error if the server gave us an unsolicited extension */
1140 clnt = SSL_get_srtp_profiles(s);
1141 if (clnt == NULL) {
1142 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP, SSL_R_NO_SRTP_PROFILES);
1143 *al = SSL_AD_DECODE_ERROR;
1144 return 0;
1145 }
1146
1147 /*
1148 * Check to see if the server gave us something we support (and
1149 * presumably offered)
1150 */
1151 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1152 prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1153
1154 if (prof->id == id) {
1155 s->srtp_profile = prof;
1156 *al = 0;
1157 return 1;
1158 }
1159 }
1160
1161 SSLerr(SSL_F_TLS_PARSE_STOC_USE_SRTP,
1162 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1163 *al = SSL_AD_DECODE_ERROR;
1164 return 0;
1165 }
1166 #endif
1167
1168 int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1169 size_t chainidx, int *al)
1170 {
1171 /* Ignore if inappropriate ciphersuite */
1172 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
1173 && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
1174 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
1175 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
1176
1177 return 1;
1178 }
1179
1180 int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1181 size_t chainidx, int *al)
1182 {
1183 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1184 if (!s->hit)
1185 s->session->flags |= SSL_SESS_FLAG_EXTMS;
1186
1187 return 1;
1188 }
1189
1190 int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1191 size_t chainidx, int *al)
1192 {
1193 #ifndef OPENSSL_NO_TLS1_3
1194 unsigned int group_id;
1195 PACKET encoded_pt;
1196 EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
1197
1198 /* Sanity check */
1199 if (ckey == NULL) {
1200 *al = SSL_AD_INTERNAL_ERROR;
1201 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1202 return 0;
1203 }
1204
1205 if (!PACKET_get_net_2(pkt, &group_id)) {
1206 *al = SSL_AD_HANDSHAKE_FAILURE;
1207 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
1208 return 0;
1209 }
1210
1211 if ((context & EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
1212 unsigned const char *pcurves = NULL;
1213 size_t i, num_curves;
1214
1215 if (PACKET_remaining(pkt) != 0) {
1216 *al = SSL_AD_HANDSHAKE_FAILURE;
1217 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
1218 return 0;
1219 }
1220
1221 /*
1222 * It is an error if the HelloRetryRequest wants a key_share that we
1223 * already sent in the first ClientHello
1224 */
1225 if (group_id == s->s3->group_id) {
1226 *al = SSL_AD_ILLEGAL_PARAMETER;
1227 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1228 return 0;
1229 }
1230
1231 /* Validate the selected group is one we support */
1232 pcurves = s->ext.supportedgroups;
1233 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
1234 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1235 return 0;
1236 }
1237 for (i = 0; i < num_curves; i++, pcurves += 2) {
1238 if (group_id == (unsigned int)((pcurves[0] << 8) | pcurves[1]))
1239 break;
1240 }
1241 if (i >= num_curves
1242 || !tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1243 *al = SSL_AD_ILLEGAL_PARAMETER;
1244 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1245 return 0;
1246 }
1247
1248 s->s3->group_id = group_id;
1249 EVP_PKEY_free(s->s3->tmp.pkey);
1250 s->s3->tmp.pkey = NULL;
1251 return 1;
1252 }
1253
1254 if (group_id != s->s3->group_id) {
1255 /*
1256 * This isn't for the group that we sent in the original
1257 * key_share!
1258 */
1259 *al = SSL_AD_HANDSHAKE_FAILURE;
1260 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1261 return 0;
1262 }
1263
1264 if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1265 || PACKET_remaining(&encoded_pt) == 0) {
1266 *al = SSL_AD_DECODE_ERROR;
1267 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
1268 return 0;
1269 }
1270
1271 skey = ssl_generate_pkey(ckey);
1272 if (skey == NULL) {
1273 *al = SSL_AD_INTERNAL_ERROR;
1274 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE);
1275 return 0;
1276 }
1277 if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
1278 PACKET_remaining(&encoded_pt))) {
1279 *al = SSL_AD_DECODE_ERROR;
1280 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_ECPOINT);
1281 EVP_PKEY_free(skey);
1282 return 0;
1283 }
1284
1285 if (ssl_derive(s, ckey, skey, 1) == 0) {
1286 *al = SSL_AD_INTERNAL_ERROR;
1287 SSLerr(SSL_F_TLS_PARSE_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1288 EVP_PKEY_free(skey);
1289 return 0;
1290 }
1291 EVP_PKEY_free(skey);
1292 #endif
1293
1294 return 1;
1295 }
1296
1297 int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1298 size_t chainidx, int *al)
1299 {
1300 #ifndef OPENSSL_NO_TLS1_3
1301 unsigned int identity;
1302
1303 if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
1304 *al = SSL_AD_HANDSHAKE_FAILURE;
1305 SSLerr(SSL_F_TLS_PARSE_STOC_PSK, SSL_R_LENGTH_MISMATCH);
1306 return 0;
1307 }
1308
1309 if (s->session->ext.tick_identity != (int)identity) {
1310 *al = SSL_AD_HANDSHAKE_FAILURE;
1311 SSLerr(SSL_F_TLS_PARSE_STOC_PSK, SSL_R_BAD_PSK_IDENTITY);
1312 return 0;
1313 }
1314
1315 s->hit = 1;
1316 #endif
1317
1318 return 1;
1319 }