]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/statem_srvr.c
87813633e87d9f4469451f782efc0c82f1d05983
[thirdparty/openssl.git] / ssl / statem / statem_srvr.c
1 /*
2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
5 *
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
10 */
11
12 #include <stdio.h>
13 #include "../ssl_local.h"
14 #include "statem_local.h"
15 #include "internal/constant_time.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/buffer.h>
18 #include <openssl/rand.h>
19 #include <openssl/objects.h>
20 #include <openssl/evp.h>
21 #include <openssl/x509.h>
22 #include <openssl/dh.h>
23 #include <openssl/rsa.h>
24 #include <openssl/bn.h>
25 #include <openssl/md5.h>
26 #include <openssl/trace.h>
27 #include <openssl/core_names.h>
28 #include <openssl/asn1t.h>
29
30 #define TICKET_NONCE_SIZE 8
31
32 typedef struct {
33 ASN1_TYPE *kxBlob;
34 ASN1_TYPE *opaqueBlob;
35 } GOST_KX_MESSAGE;
36
37 DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
38
39 ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
40 ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
41 ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
42 } ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
43
44 IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
45
46 static int tls_construct_encrypted_extensions(SSL_CONNECTION *s, WPACKET *pkt);
47
48 /*
49 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
50 * handshake state transitions when a TLSv1.3 server is reading messages from
51 * the client. The message type that the client has sent is provided in |mt|.
52 * The current state is in |s->statem.hand_state|.
53 *
54 * Return values are 1 for success (transition allowed) and 0 on error
55 * (transition not allowed)
56 */
57 static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt)
58 {
59 OSSL_STATEM *st = &s->statem;
60
61 /*
62 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
63 * not negotiated TLSv1.3 yet, so that case is handled by
64 * ossl_statem_server_read_transition()
65 */
66 switch (st->hand_state) {
67 default:
68 break;
69
70 case TLS_ST_EARLY_DATA:
71 if (s->hello_retry_request == SSL_HRR_PENDING) {
72 if (mt == SSL3_MT_CLIENT_HELLO) {
73 st->hand_state = TLS_ST_SR_CLNT_HELLO;
74 return 1;
75 }
76 break;
77 } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
78 if (mt == SSL3_MT_END_OF_EARLY_DATA) {
79 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
80 return 1;
81 }
82 break;
83 }
84 /* Fall through */
85
86 case TLS_ST_SR_END_OF_EARLY_DATA:
87 case TLS_ST_SW_FINISHED:
88 if (s->s3.tmp.cert_request) {
89 if (mt == SSL3_MT_CERTIFICATE) {
90 st->hand_state = TLS_ST_SR_CERT;
91 return 1;
92 }
93 } else {
94 if (mt == SSL3_MT_FINISHED) {
95 st->hand_state = TLS_ST_SR_FINISHED;
96 return 1;
97 }
98 }
99 break;
100
101 case TLS_ST_SR_CERT:
102 if (s->session->peer == NULL) {
103 if (mt == SSL3_MT_FINISHED) {
104 st->hand_state = TLS_ST_SR_FINISHED;
105 return 1;
106 }
107 } else {
108 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
109 st->hand_state = TLS_ST_SR_CERT_VRFY;
110 return 1;
111 }
112 }
113 break;
114
115 case TLS_ST_SR_CERT_VRFY:
116 if (mt == SSL3_MT_FINISHED) {
117 st->hand_state = TLS_ST_SR_FINISHED;
118 return 1;
119 }
120 break;
121
122 case TLS_ST_OK:
123 /*
124 * Its never ok to start processing handshake messages in the middle of
125 * early data (i.e. before we've received the end of early data alert)
126 */
127 if (s->early_data_state == SSL_EARLY_DATA_READING)
128 break;
129
130 if (mt == SSL3_MT_CERTIFICATE
131 && s->post_handshake_auth == SSL_PHA_REQUESTED) {
132 st->hand_state = TLS_ST_SR_CERT;
133 return 1;
134 }
135
136 if (mt == SSL3_MT_KEY_UPDATE) {
137 st->hand_state = TLS_ST_SR_KEY_UPDATE;
138 return 1;
139 }
140 break;
141 }
142
143 /* No valid transition found */
144 return 0;
145 }
146
147 /*
148 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
149 * handshake state transitions when the server is reading messages from the
150 * client. The message type that the client has sent is provided in |mt|. The
151 * current state is in |s->statem.hand_state|.
152 *
153 * Return values are 1 for success (transition allowed) and 0 on error
154 * (transition not allowed)
155 */
156 int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt)
157 {
158 OSSL_STATEM *st = &s->statem;
159
160 if (SSL_CONNECTION_IS_TLS13(s)) {
161 if (!ossl_statem_server13_read_transition(s, mt))
162 goto err;
163 return 1;
164 }
165
166 switch (st->hand_state) {
167 default:
168 break;
169
170 case TLS_ST_BEFORE:
171 case TLS_ST_OK:
172 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
173 if (mt == SSL3_MT_CLIENT_HELLO) {
174 st->hand_state = TLS_ST_SR_CLNT_HELLO;
175 return 1;
176 }
177 break;
178
179 case TLS_ST_SW_SRVR_DONE:
180 /*
181 * If we get a CKE message after a ServerDone then either
182 * 1) We didn't request a Certificate
183 * OR
184 * 2) If we did request one then
185 * a) We allow no Certificate to be returned
186 * AND
187 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
188 * list if we requested a certificate)
189 */
190 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
191 if (s->s3.tmp.cert_request) {
192 if (s->version == SSL3_VERSION) {
193 if ((s->verify_mode & SSL_VERIFY_PEER)
194 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
195 /*
196 * This isn't an unexpected message as such - we're just
197 * not going to accept it because we require a client
198 * cert.
199 */
200 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
201 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
202 return 0;
203 }
204 st->hand_state = TLS_ST_SR_KEY_EXCH;
205 return 1;
206 }
207 } else {
208 st->hand_state = TLS_ST_SR_KEY_EXCH;
209 return 1;
210 }
211 } else if (s->s3.tmp.cert_request) {
212 if (mt == SSL3_MT_CERTIFICATE) {
213 st->hand_state = TLS_ST_SR_CERT;
214 return 1;
215 }
216 }
217 break;
218
219 case TLS_ST_SR_CERT:
220 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
221 st->hand_state = TLS_ST_SR_KEY_EXCH;
222 return 1;
223 }
224 break;
225
226 case TLS_ST_SR_KEY_EXCH:
227 /*
228 * We should only process a CertificateVerify message if we have
229 * received a Certificate from the client. If so then |s->session->peer|
230 * will be non NULL. In some instances a CertificateVerify message is
231 * not required even if the peer has sent a Certificate (e.g. such as in
232 * the case of static DH). In that case |st->no_cert_verify| should be
233 * set.
234 */
235 if (s->session->peer == NULL || st->no_cert_verify) {
236 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
237 /*
238 * For the ECDH ciphersuites when the client sends its ECDH
239 * pub key in a certificate, the CertificateVerify message is
240 * not sent. Also for GOST ciphersuites when the client uses
241 * its key from the certificate for key exchange.
242 */
243 st->hand_state = TLS_ST_SR_CHANGE;
244 return 1;
245 }
246 } else {
247 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
248 st->hand_state = TLS_ST_SR_CERT_VRFY;
249 return 1;
250 }
251 }
252 break;
253
254 case TLS_ST_SR_CERT_VRFY:
255 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
256 st->hand_state = TLS_ST_SR_CHANGE;
257 return 1;
258 }
259 break;
260
261 case TLS_ST_SR_CHANGE:
262 #ifndef OPENSSL_NO_NEXTPROTONEG
263 if (s->s3.npn_seen) {
264 if (mt == SSL3_MT_NEXT_PROTO) {
265 st->hand_state = TLS_ST_SR_NEXT_PROTO;
266 return 1;
267 }
268 } else {
269 #endif
270 if (mt == SSL3_MT_FINISHED) {
271 st->hand_state = TLS_ST_SR_FINISHED;
272 return 1;
273 }
274 #ifndef OPENSSL_NO_NEXTPROTONEG
275 }
276 #endif
277 break;
278
279 #ifndef OPENSSL_NO_NEXTPROTONEG
280 case TLS_ST_SR_NEXT_PROTO:
281 if (mt == SSL3_MT_FINISHED) {
282 st->hand_state = TLS_ST_SR_FINISHED;
283 return 1;
284 }
285 break;
286 #endif
287
288 case TLS_ST_SW_FINISHED:
289 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
290 st->hand_state = TLS_ST_SR_CHANGE;
291 return 1;
292 }
293 break;
294 }
295
296 err:
297 /* No valid transition found */
298 if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
299 BIO *rbio;
300
301 /*
302 * CCS messages don't have a message sequence number so this is probably
303 * because of an out-of-order CCS. We'll just drop it.
304 */
305 s->init_num = 0;
306 s->rwstate = SSL_READING;
307 rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
308 BIO_clear_retry_flags(rbio);
309 BIO_set_retry_read(rbio);
310 return 0;
311 }
312 SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
313 return 0;
314 }
315
316 /*
317 * Should we send a ServerKeyExchange message?
318 *
319 * Valid return values are:
320 * 1: Yes
321 * 0: No
322 */
323 static int send_server_key_exchange(SSL_CONNECTION *s)
324 {
325 unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
326
327 /*
328 * only send a ServerKeyExchange if DH or fortezza but we have a
329 * sign only certificate PSK: may send PSK identity hints For
330 * ECC ciphersuites, we send a serverKeyExchange message only if
331 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
332 * the server certificate contains the server's public key for
333 * key exchange.
334 */
335 if (alg_k & (SSL_kDHE | SSL_kECDHE)
336 /*
337 * PSK: send ServerKeyExchange if PSK identity hint if
338 * provided
339 */
340 #ifndef OPENSSL_NO_PSK
341 /* Only send SKE if we have identity hint for plain PSK */
342 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
343 && s->cert->psk_identity_hint)
344 /* For other PSK always send SKE */
345 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
346 #endif
347 #ifndef OPENSSL_NO_SRP
348 /* SRP: send ServerKeyExchange */
349 || (alg_k & SSL_kSRP)
350 #endif
351 ) {
352 return 1;
353 }
354
355 return 0;
356 }
357
358 /*
359 * Should we send a CertificateRequest message?
360 *
361 * Valid return values are:
362 * 1: Yes
363 * 0: No
364 */
365 int send_certificate_request(SSL_CONNECTION *s)
366 {
367 if (
368 /* don't request cert unless asked for it: */
369 s->verify_mode & SSL_VERIFY_PEER
370 /*
371 * don't request if post-handshake-only unless doing
372 * post-handshake in TLSv1.3:
373 */
374 && (!SSL_CONNECTION_IS_TLS13(s)
375 || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
376 || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
377 /*
378 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
379 * a second time:
380 */
381 && (s->certreqs_sent < 1 ||
382 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
383 /*
384 * never request cert in anonymous ciphersuites (see
385 * section "Certificate request" in SSL 3 drafts and in
386 * RFC 2246):
387 */
388 && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
389 /*
390 * ... except when the application insists on
391 * verification (against the specs, but statem_clnt.c accepts
392 * this for SSL 3)
393 */
394 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
395 /* don't request certificate for SRP auth */
396 && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
397 /*
398 * With normal PSK Certificates and Certificate Requests
399 * are omitted
400 */
401 && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
402 return 1;
403 }
404
405 return 0;
406 }
407
408 /*
409 * ossl_statem_server13_write_transition() works out what handshake state to
410 * move to next when a TLSv1.3 server is writing messages to be sent to the
411 * client.
412 */
413 static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s)
414 {
415 OSSL_STATEM *st = &s->statem;
416
417 /*
418 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
419 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
420 */
421
422 switch (st->hand_state) {
423 default:
424 /* Shouldn't happen */
425 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
426 return WRITE_TRAN_ERROR;
427
428 case TLS_ST_OK:
429 if (s->key_update != SSL_KEY_UPDATE_NONE) {
430 st->hand_state = TLS_ST_SW_KEY_UPDATE;
431 return WRITE_TRAN_CONTINUE;
432 }
433 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
434 st->hand_state = TLS_ST_SW_CERT_REQ;
435 return WRITE_TRAN_CONTINUE;
436 }
437 if (s->ext.extra_tickets_expected > 0) {
438 st->hand_state = TLS_ST_SW_SESSION_TICKET;
439 return WRITE_TRAN_CONTINUE;
440 }
441 /* Try to read from the client instead */
442 return WRITE_TRAN_FINISHED;
443
444 case TLS_ST_SR_CLNT_HELLO:
445 st->hand_state = TLS_ST_SW_SRVR_HELLO;
446 return WRITE_TRAN_CONTINUE;
447
448 case TLS_ST_SW_SRVR_HELLO:
449 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
450 && s->hello_retry_request != SSL_HRR_COMPLETE)
451 st->hand_state = TLS_ST_SW_CHANGE;
452 else if (s->hello_retry_request == SSL_HRR_PENDING)
453 st->hand_state = TLS_ST_EARLY_DATA;
454 else
455 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
456 return WRITE_TRAN_CONTINUE;
457
458 case TLS_ST_SW_CHANGE:
459 if (s->hello_retry_request == SSL_HRR_PENDING)
460 st->hand_state = TLS_ST_EARLY_DATA;
461 else
462 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
463 return WRITE_TRAN_CONTINUE;
464
465 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
466 if (s->hit)
467 st->hand_state = TLS_ST_SW_FINISHED;
468 else if (send_certificate_request(s))
469 st->hand_state = TLS_ST_SW_CERT_REQ;
470 else
471 st->hand_state = TLS_ST_SW_CERT;
472
473 return WRITE_TRAN_CONTINUE;
474
475 case TLS_ST_SW_CERT_REQ:
476 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
477 s->post_handshake_auth = SSL_PHA_REQUESTED;
478 st->hand_state = TLS_ST_OK;
479 } else {
480 st->hand_state = TLS_ST_SW_CERT;
481 }
482 return WRITE_TRAN_CONTINUE;
483
484 case TLS_ST_SW_CERT:
485 st->hand_state = TLS_ST_SW_CERT_VRFY;
486 return WRITE_TRAN_CONTINUE;
487
488 case TLS_ST_SW_CERT_VRFY:
489 st->hand_state = TLS_ST_SW_FINISHED;
490 return WRITE_TRAN_CONTINUE;
491
492 case TLS_ST_SW_FINISHED:
493 st->hand_state = TLS_ST_EARLY_DATA;
494 return WRITE_TRAN_CONTINUE;
495
496 case TLS_ST_EARLY_DATA:
497 return WRITE_TRAN_FINISHED;
498
499 case TLS_ST_SR_FINISHED:
500 /*
501 * Technically we have finished the handshake at this point, but we're
502 * going to remain "in_init" for now and write out any session tickets
503 * immediately.
504 */
505 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
506 s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
507 } else if (!s->ext.ticket_expected) {
508 /*
509 * If we're not going to renew the ticket then we just finish the
510 * handshake at this point.
511 */
512 st->hand_state = TLS_ST_OK;
513 return WRITE_TRAN_CONTINUE;
514 }
515 if (s->num_tickets > s->sent_tickets)
516 st->hand_state = TLS_ST_SW_SESSION_TICKET;
517 else
518 st->hand_state = TLS_ST_OK;
519 return WRITE_TRAN_CONTINUE;
520
521 case TLS_ST_SR_KEY_UPDATE:
522 case TLS_ST_SW_KEY_UPDATE:
523 st->hand_state = TLS_ST_OK;
524 return WRITE_TRAN_CONTINUE;
525
526 case TLS_ST_SW_SESSION_TICKET:
527 /* In a resumption we only ever send a maximum of one new ticket.
528 * Following an initial handshake we send the number of tickets we have
529 * been configured for.
530 */
531 if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) {
532 return WRITE_TRAN_CONTINUE;
533 } else if (s->hit || s->num_tickets <= s->sent_tickets) {
534 /* We've written enough tickets out. */
535 st->hand_state = TLS_ST_OK;
536 }
537 return WRITE_TRAN_CONTINUE;
538 }
539 }
540
541 /*
542 * ossl_statem_server_write_transition() works out what handshake state to move
543 * to next when the server is writing messages to be sent to the client.
544 */
545 WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s)
546 {
547 OSSL_STATEM *st = &s->statem;
548
549 /*
550 * Note that before the ClientHello we don't know what version we are going
551 * to negotiate yet, so we don't take this branch until later
552 */
553
554 if (SSL_CONNECTION_IS_TLS13(s))
555 return ossl_statem_server13_write_transition(s);
556
557 switch (st->hand_state) {
558 default:
559 /* Shouldn't happen */
560 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
561 return WRITE_TRAN_ERROR;
562
563 case TLS_ST_OK:
564 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
565 /* We must be trying to renegotiate */
566 st->hand_state = TLS_ST_SW_HELLO_REQ;
567 st->request_state = TLS_ST_BEFORE;
568 return WRITE_TRAN_CONTINUE;
569 }
570 /* Must be an incoming ClientHello */
571 if (!tls_setup_handshake(s)) {
572 /* SSLfatal() already called */
573 return WRITE_TRAN_ERROR;
574 }
575 /* Fall through */
576
577 case TLS_ST_BEFORE:
578 /* Just go straight to trying to read from the client */
579 return WRITE_TRAN_FINISHED;
580
581 case TLS_ST_SW_HELLO_REQ:
582 st->hand_state = TLS_ST_OK;
583 return WRITE_TRAN_CONTINUE;
584
585 case TLS_ST_SR_CLNT_HELLO:
586 if (SSL_CONNECTION_IS_DTLS(s) && !s->d1->cookie_verified
587 && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) {
588 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
589 } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
590 /* We must have rejected the renegotiation */
591 st->hand_state = TLS_ST_OK;
592 return WRITE_TRAN_CONTINUE;
593 } else {
594 st->hand_state = TLS_ST_SW_SRVR_HELLO;
595 }
596 return WRITE_TRAN_CONTINUE;
597
598 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
599 return WRITE_TRAN_FINISHED;
600
601 case TLS_ST_SW_SRVR_HELLO:
602 if (s->hit) {
603 if (s->ext.ticket_expected)
604 st->hand_state = TLS_ST_SW_SESSION_TICKET;
605 else
606 st->hand_state = TLS_ST_SW_CHANGE;
607 } else {
608 /* Check if it is anon DH or anon ECDH, */
609 /* normal PSK or SRP */
610 if (!(s->s3.tmp.new_cipher->algorithm_auth &
611 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
612 st->hand_state = TLS_ST_SW_CERT;
613 } else if (send_server_key_exchange(s)) {
614 st->hand_state = TLS_ST_SW_KEY_EXCH;
615 } else if (send_certificate_request(s)) {
616 st->hand_state = TLS_ST_SW_CERT_REQ;
617 } else {
618 st->hand_state = TLS_ST_SW_SRVR_DONE;
619 }
620 }
621 return WRITE_TRAN_CONTINUE;
622
623 case TLS_ST_SW_CERT:
624 if (s->ext.status_expected) {
625 st->hand_state = TLS_ST_SW_CERT_STATUS;
626 return WRITE_TRAN_CONTINUE;
627 }
628 /* Fall through */
629
630 case TLS_ST_SW_CERT_STATUS:
631 if (send_server_key_exchange(s)) {
632 st->hand_state = TLS_ST_SW_KEY_EXCH;
633 return WRITE_TRAN_CONTINUE;
634 }
635 /* Fall through */
636
637 case TLS_ST_SW_KEY_EXCH:
638 if (send_certificate_request(s)) {
639 st->hand_state = TLS_ST_SW_CERT_REQ;
640 return WRITE_TRAN_CONTINUE;
641 }
642 /* Fall through */
643
644 case TLS_ST_SW_CERT_REQ:
645 st->hand_state = TLS_ST_SW_SRVR_DONE;
646 return WRITE_TRAN_CONTINUE;
647
648 case TLS_ST_SW_SRVR_DONE:
649 return WRITE_TRAN_FINISHED;
650
651 case TLS_ST_SR_FINISHED:
652 if (s->hit) {
653 st->hand_state = TLS_ST_OK;
654 return WRITE_TRAN_CONTINUE;
655 } else if (s->ext.ticket_expected) {
656 st->hand_state = TLS_ST_SW_SESSION_TICKET;
657 } else {
658 st->hand_state = TLS_ST_SW_CHANGE;
659 }
660 return WRITE_TRAN_CONTINUE;
661
662 case TLS_ST_SW_SESSION_TICKET:
663 st->hand_state = TLS_ST_SW_CHANGE;
664 return WRITE_TRAN_CONTINUE;
665
666 case TLS_ST_SW_CHANGE:
667 st->hand_state = TLS_ST_SW_FINISHED;
668 return WRITE_TRAN_CONTINUE;
669
670 case TLS_ST_SW_FINISHED:
671 if (s->hit) {
672 return WRITE_TRAN_FINISHED;
673 }
674 st->hand_state = TLS_ST_OK;
675 return WRITE_TRAN_CONTINUE;
676 }
677 }
678
679 /*
680 * Perform any pre work that needs to be done prior to sending a message from
681 * the server to the client.
682 */
683 WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
684 {
685 OSSL_STATEM *st = &s->statem;
686 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
687
688 switch (st->hand_state) {
689 default:
690 /* No pre work to be done */
691 break;
692
693 case TLS_ST_SW_HELLO_REQ:
694 s->shutdown = 0;
695 if (SSL_CONNECTION_IS_DTLS(s))
696 dtls1_clear_sent_buffer(s);
697 break;
698
699 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
700 s->shutdown = 0;
701 if (SSL_CONNECTION_IS_DTLS(s)) {
702 dtls1_clear_sent_buffer(s);
703 /* We don't buffer this message so don't use the timer */
704 st->use_timer = 0;
705 }
706 break;
707
708 case TLS_ST_SW_SRVR_HELLO:
709 if (SSL_CONNECTION_IS_DTLS(s)) {
710 /*
711 * Messages we write from now on should be buffered and
712 * retransmitted if necessary, so we need to use the timer now
713 */
714 st->use_timer = 1;
715 }
716 break;
717
718 case TLS_ST_SW_SRVR_DONE:
719 #ifndef OPENSSL_NO_SCTP
720 if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
721 /* Calls SSLfatal() as required */
722 return dtls_wait_for_dry(s);
723 }
724 #endif
725 return WORK_FINISHED_CONTINUE;
726
727 case TLS_ST_SW_SESSION_TICKET:
728 if (SSL_CONNECTION_IS_TLS13(s) && s->sent_tickets == 0
729 && s->ext.extra_tickets_expected == 0) {
730 /*
731 * Actually this is the end of the handshake, but we're going
732 * straight into writing the session ticket out. So we finish off
733 * the handshake, but keep the various buffers active.
734 *
735 * Calls SSLfatal as required.
736 */
737 return tls_finish_handshake(s, wst, 0, 0);
738 }
739 if (SSL_CONNECTION_IS_DTLS(s)) {
740 /*
741 * We're into the last flight. We don't retransmit the last flight
742 * unless we need to, so we don't use the timer
743 */
744 st->use_timer = 0;
745 }
746 break;
747
748 case TLS_ST_SW_CHANGE:
749 if (SSL_CONNECTION_IS_TLS13(s))
750 break;
751 /* Writes to s->session are only safe for initial handshakes */
752 if (s->session->cipher == NULL) {
753 s->session->cipher = s->s3.tmp.new_cipher;
754 } else if (s->session->cipher != s->s3.tmp.new_cipher) {
755 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
756 return WORK_ERROR;
757 }
758 if (!ssl->method->ssl3_enc->setup_key_block(s)) {
759 /* SSLfatal() already called */
760 return WORK_ERROR;
761 }
762 if (SSL_CONNECTION_IS_DTLS(s)) {
763 /*
764 * We're into the last flight. We don't retransmit the last flight
765 * unless we need to, so we don't use the timer. This might have
766 * already been set to 0 if we sent a NewSessionTicket message,
767 * but we'll set it again here in case we didn't.
768 */
769 st->use_timer = 0;
770 }
771 return WORK_FINISHED_CONTINUE;
772
773 case TLS_ST_EARLY_DATA:
774 if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
775 && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
776 return WORK_FINISHED_CONTINUE;
777 /* Fall through */
778
779 case TLS_ST_OK:
780 /* Calls SSLfatal() as required */
781 return tls_finish_handshake(s, wst, 1, 1);
782 }
783
784 return WORK_FINISHED_CONTINUE;
785 }
786
787 static ossl_inline int conn_is_closed(void)
788 {
789 switch (get_last_sys_error()) {
790 #if defined(EPIPE)
791 case EPIPE:
792 return 1;
793 #endif
794 #if defined(ECONNRESET)
795 case ECONNRESET:
796 return 1;
797 #endif
798 #if defined(WSAECONNRESET)
799 case WSAECONNRESET:
800 return 1;
801 #endif
802 default:
803 return 0;
804 }
805 }
806
807 /*
808 * Perform any work that needs to be done after sending a message from the
809 * server to the client.
810 */
811 WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst)
812 {
813 OSSL_STATEM *st = &s->statem;
814 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
815
816 s->init_num = 0;
817
818 switch (st->hand_state) {
819 default:
820 /* No post work to be done */
821 break;
822
823 case TLS_ST_SW_HELLO_REQ:
824 if (statem_flush(s) != 1)
825 return WORK_MORE_A;
826 if (!ssl3_init_finished_mac(s)) {
827 /* SSLfatal() already called */
828 return WORK_ERROR;
829 }
830 break;
831
832 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
833 if (statem_flush(s) != 1)
834 return WORK_MORE_A;
835 /* HelloVerifyRequest resets Finished MAC */
836 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
837 /* SSLfatal() already called */
838 return WORK_ERROR;
839 }
840 /*
841 * The next message should be another ClientHello which we need to
842 * treat like it was the first packet
843 */
844 s->first_packet = 1;
845 break;
846
847 case TLS_ST_SW_SRVR_HELLO:
848 if (SSL_CONNECTION_IS_TLS13(s)
849 && s->hello_retry_request == SSL_HRR_PENDING) {
850 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
851 && statem_flush(s) != 1)
852 return WORK_MORE_A;
853 break;
854 }
855 #ifndef OPENSSL_NO_SCTP
856 if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
857 unsigned char sctpauthkey[64];
858 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
859 size_t labellen;
860
861 /*
862 * Add new shared key for SCTP-Auth, will be ignored if no
863 * SCTP used.
864 */
865 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
866 sizeof(DTLS1_SCTP_AUTH_LABEL));
867
868 /* Don't include the terminating zero. */
869 labellen = sizeof(labelbuffer) - 1;
870 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
871 labellen += 1;
872
873 if (SSL_export_keying_material(ssl, sctpauthkey,
874 sizeof(sctpauthkey), labelbuffer,
875 labellen, NULL, 0,
876 0) <= 0) {
877 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
878 return WORK_ERROR;
879 }
880
881 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
882 sizeof(sctpauthkey), sctpauthkey);
883 }
884 #endif
885 if (!SSL_CONNECTION_IS_TLS13(s)
886 || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
887 && s->hello_retry_request != SSL_HRR_COMPLETE))
888 break;
889 /* Fall through */
890
891 case TLS_ST_SW_CHANGE:
892 if (s->hello_retry_request == SSL_HRR_PENDING) {
893 if (!statem_flush(s))
894 return WORK_MORE_A;
895 break;
896 }
897
898 if (SSL_CONNECTION_IS_TLS13(s)) {
899 if (!ssl->method->ssl3_enc->setup_key_block(s)
900 || !ssl->method->ssl3_enc->change_cipher_state(s,
901 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
902 /* SSLfatal() already called */
903 return WORK_ERROR;
904 }
905
906 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
907 && !ssl->method->ssl3_enc->change_cipher_state(s,
908 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
909 /* SSLfatal() already called */
910 return WORK_ERROR;
911 }
912 /*
913 * We don't yet know whether the next record we are going to receive
914 * is an unencrypted alert, an encrypted alert, or an encrypted
915 * handshake message. We temporarily tolerate unencrypted alerts.
916 */
917 s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
918 break;
919 }
920
921 #ifndef OPENSSL_NO_SCTP
922 if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) {
923 /*
924 * Change to new shared key of SCTP-Auth, will be ignored if
925 * no SCTP used.
926 */
927 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
928 0, NULL);
929 }
930 #endif
931 if (!ssl->method->ssl3_enc->change_cipher_state(s,
932 SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
933 /* SSLfatal() already called */
934 return WORK_ERROR;
935 }
936
937 if (SSL_CONNECTION_IS_DTLS(s))
938 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
939 break;
940
941 case TLS_ST_SW_SRVR_DONE:
942 if (statem_flush(s) != 1)
943 return WORK_MORE_A;
944 break;
945
946 case TLS_ST_SW_FINISHED:
947 if (statem_flush(s) != 1)
948 return WORK_MORE_A;
949 #ifndef OPENSSL_NO_SCTP
950 if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
951 /*
952 * Change to new shared key of SCTP-Auth, will be ignored if
953 * no SCTP used.
954 */
955 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
956 0, NULL);
957 }
958 #endif
959 if (SSL_CONNECTION_IS_TLS13(s)) {
960 /* TLS 1.3 gets the secret size from the handshake md */
961 size_t dummy;
962 if (!ssl->method->ssl3_enc->generate_master_secret(s,
963 s->master_secret, s->handshake_secret, 0,
964 &dummy)
965 || !ssl->method->ssl3_enc->change_cipher_state(s,
966 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
967 /* SSLfatal() already called */
968 return WORK_ERROR;
969 }
970 break;
971
972 case TLS_ST_SW_CERT_REQ:
973 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
974 if (statem_flush(s) != 1)
975 return WORK_MORE_A;
976 }
977 break;
978
979 case TLS_ST_SW_KEY_UPDATE:
980 if (statem_flush(s) != 1)
981 return WORK_MORE_A;
982 if (!tls13_update_key(s, 1)) {
983 /* SSLfatal() already called */
984 return WORK_ERROR;
985 }
986 break;
987
988 case TLS_ST_SW_SESSION_TICKET:
989 clear_sys_error();
990 if (SSL_CONNECTION_IS_TLS13(s) && statem_flush(s) != 1) {
991 if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL
992 && conn_is_closed()) {
993 /*
994 * We ignore connection closed errors in TLSv1.3 when sending a
995 * NewSessionTicket and behave as if we were successful. This is
996 * so that we are still able to read data sent to us by a client
997 * that closes soon after the end of the handshake without
998 * waiting to read our post-handshake NewSessionTickets.
999 */
1000 s->rwstate = SSL_NOTHING;
1001 break;
1002 }
1003
1004 return WORK_MORE_A;
1005 }
1006 break;
1007 }
1008
1009 return WORK_FINISHED_CONTINUE;
1010 }
1011
1012 /*
1013 * Get the message construction function and message type for sending from the
1014 * server
1015 *
1016 * Valid return values are:
1017 * 1: Success
1018 * 0: Error
1019 */
1020 int ossl_statem_server_construct_message(SSL_CONNECTION *s,
1021 confunc_f *confunc, int *mt)
1022 {
1023 OSSL_STATEM *st = &s->statem;
1024
1025 switch (st->hand_state) {
1026 default:
1027 /* Shouldn't happen */
1028 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
1029 return 0;
1030
1031 case TLS_ST_SW_CHANGE:
1032 if (SSL_CONNECTION_IS_DTLS(s))
1033 *confunc = dtls_construct_change_cipher_spec;
1034 else
1035 *confunc = tls_construct_change_cipher_spec;
1036 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1037 break;
1038
1039 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1040 *confunc = dtls_construct_hello_verify_request;
1041 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
1042 break;
1043
1044 case TLS_ST_SW_HELLO_REQ:
1045 /* No construction function needed */
1046 *confunc = NULL;
1047 *mt = SSL3_MT_HELLO_REQUEST;
1048 break;
1049
1050 case TLS_ST_SW_SRVR_HELLO:
1051 *confunc = tls_construct_server_hello;
1052 *mt = SSL3_MT_SERVER_HELLO;
1053 break;
1054
1055 case TLS_ST_SW_CERT:
1056 *confunc = tls_construct_server_certificate;
1057 *mt = SSL3_MT_CERTIFICATE;
1058 break;
1059
1060 case TLS_ST_SW_CERT_VRFY:
1061 *confunc = tls_construct_cert_verify;
1062 *mt = SSL3_MT_CERTIFICATE_VERIFY;
1063 break;
1064
1065
1066 case TLS_ST_SW_KEY_EXCH:
1067 *confunc = tls_construct_server_key_exchange;
1068 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
1069 break;
1070
1071 case TLS_ST_SW_CERT_REQ:
1072 *confunc = tls_construct_certificate_request;
1073 *mt = SSL3_MT_CERTIFICATE_REQUEST;
1074 break;
1075
1076 case TLS_ST_SW_SRVR_DONE:
1077 *confunc = tls_construct_server_done;
1078 *mt = SSL3_MT_SERVER_DONE;
1079 break;
1080
1081 case TLS_ST_SW_SESSION_TICKET:
1082 *confunc = tls_construct_new_session_ticket;
1083 *mt = SSL3_MT_NEWSESSION_TICKET;
1084 break;
1085
1086 case TLS_ST_SW_CERT_STATUS:
1087 *confunc = tls_construct_cert_status;
1088 *mt = SSL3_MT_CERTIFICATE_STATUS;
1089 break;
1090
1091 case TLS_ST_SW_FINISHED:
1092 *confunc = tls_construct_finished;
1093 *mt = SSL3_MT_FINISHED;
1094 break;
1095
1096 case TLS_ST_EARLY_DATA:
1097 *confunc = NULL;
1098 *mt = SSL3_MT_DUMMY;
1099 break;
1100
1101 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
1102 *confunc = tls_construct_encrypted_extensions;
1103 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
1104 break;
1105
1106 case TLS_ST_SW_KEY_UPDATE:
1107 *confunc = tls_construct_key_update;
1108 *mt = SSL3_MT_KEY_UPDATE;
1109 break;
1110 }
1111
1112 return 1;
1113 }
1114
1115 /*
1116 * Maximum size (excluding the Handshake header) of a ClientHello message,
1117 * calculated as follows:
1118 *
1119 * 2 + # client_version
1120 * 32 + # only valid length for random
1121 * 1 + # length of session_id
1122 * 32 + # maximum size for session_id
1123 * 2 + # length of cipher suites
1124 * 2^16-2 + # maximum length of cipher suites array
1125 * 1 + # length of compression_methods
1126 * 2^8-1 + # maximum length of compression methods
1127 * 2 + # length of extensions
1128 * 2^16-1 # maximum length of extensions
1129 */
1130 #define CLIENT_HELLO_MAX_LENGTH 131396
1131
1132 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1133 #define NEXT_PROTO_MAX_LENGTH 514
1134
1135 /*
1136 * Returns the maximum allowed length for the current message that we are
1137 * reading. Excludes the message header.
1138 */
1139 size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s)
1140 {
1141 OSSL_STATEM *st = &s->statem;
1142
1143 switch (st->hand_state) {
1144 default:
1145 /* Shouldn't happen */
1146 return 0;
1147
1148 case TLS_ST_SR_CLNT_HELLO:
1149 return CLIENT_HELLO_MAX_LENGTH;
1150
1151 case TLS_ST_SR_END_OF_EARLY_DATA:
1152 return END_OF_EARLY_DATA_MAX_LENGTH;
1153
1154 case TLS_ST_SR_CERT:
1155 return s->max_cert_list;
1156
1157 case TLS_ST_SR_KEY_EXCH:
1158 return CLIENT_KEY_EXCH_MAX_LENGTH;
1159
1160 case TLS_ST_SR_CERT_VRFY:
1161 return SSL3_RT_MAX_PLAIN_LENGTH;
1162
1163 #ifndef OPENSSL_NO_NEXTPROTONEG
1164 case TLS_ST_SR_NEXT_PROTO:
1165 return NEXT_PROTO_MAX_LENGTH;
1166 #endif
1167
1168 case TLS_ST_SR_CHANGE:
1169 return CCS_MAX_LENGTH;
1170
1171 case TLS_ST_SR_FINISHED:
1172 return FINISHED_MAX_LENGTH;
1173
1174 case TLS_ST_SR_KEY_UPDATE:
1175 return KEY_UPDATE_MAX_LENGTH;
1176 }
1177 }
1178
1179 /*
1180 * Process a message that the server has received from the client.
1181 */
1182 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s,
1183 PACKET *pkt)
1184 {
1185 OSSL_STATEM *st = &s->statem;
1186
1187 switch (st->hand_state) {
1188 default:
1189 /* Shouldn't happen */
1190 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1191 return MSG_PROCESS_ERROR;
1192
1193 case TLS_ST_SR_CLNT_HELLO:
1194 return tls_process_client_hello(s, pkt);
1195
1196 case TLS_ST_SR_END_OF_EARLY_DATA:
1197 return tls_process_end_of_early_data(s, pkt);
1198
1199 case TLS_ST_SR_CERT:
1200 return tls_process_client_certificate(s, pkt);
1201
1202 case TLS_ST_SR_KEY_EXCH:
1203 return tls_process_client_key_exchange(s, pkt);
1204
1205 case TLS_ST_SR_CERT_VRFY:
1206 return tls_process_cert_verify(s, pkt);
1207
1208 #ifndef OPENSSL_NO_NEXTPROTONEG
1209 case TLS_ST_SR_NEXT_PROTO:
1210 return tls_process_next_proto(s, pkt);
1211 #endif
1212
1213 case TLS_ST_SR_CHANGE:
1214 return tls_process_change_cipher_spec(s, pkt);
1215
1216 case TLS_ST_SR_FINISHED:
1217 return tls_process_finished(s, pkt);
1218
1219 case TLS_ST_SR_KEY_UPDATE:
1220 return tls_process_key_update(s, pkt);
1221
1222 }
1223 }
1224
1225 /*
1226 * Perform any further processing required following the receipt of a message
1227 * from the client
1228 */
1229 WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s,
1230 WORK_STATE wst)
1231 {
1232 OSSL_STATEM *st = &s->statem;
1233
1234 switch (st->hand_state) {
1235 default:
1236 /* Shouldn't happen */
1237 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1238 return WORK_ERROR;
1239
1240 case TLS_ST_SR_CLNT_HELLO:
1241 return tls_post_process_client_hello(s, wst);
1242
1243 case TLS_ST_SR_KEY_EXCH:
1244 return tls_post_process_client_key_exchange(s, wst);
1245 }
1246 }
1247
1248 #ifndef OPENSSL_NO_SRP
1249 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1250 static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s)
1251 {
1252 int ret;
1253 int al = SSL_AD_UNRECOGNIZED_NAME;
1254
1255 if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1256 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1257 if (s->srp_ctx.login == NULL) {
1258 /*
1259 * RFC 5054 says SHOULD reject, we do so if There is no srp
1260 * login name
1261 */
1262 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1263 SSL_R_PSK_IDENTITY_NOT_FOUND);
1264 return -1;
1265 } else {
1266 ret = ssl_srp_server_param_with_username_intern(s, &al);
1267 if (ret < 0)
1268 return 0;
1269 if (ret == SSL3_AL_FATAL) {
1270 SSLfatal(s, al,
1271 al == SSL_AD_UNKNOWN_PSK_IDENTITY
1272 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1273 : SSL_R_CLIENTHELLO_TLSEXT);
1274 return -1;
1275 }
1276 }
1277 }
1278 return 1;
1279 }
1280 #endif
1281
1282 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1283 size_t cookie_len)
1284 {
1285 /* Always use DTLS 1.0 version: see RFC 6347 */
1286 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1287 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1288 return 0;
1289
1290 return 1;
1291 }
1292
1293 int dtls_construct_hello_verify_request(SSL_CONNECTION *s, WPACKET *pkt)
1294 {
1295 unsigned int cookie_leni;
1296 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1297
1298 if (sctx->app_gen_cookie_cb == NULL
1299 || sctx->app_gen_cookie_cb(SSL_CONNECTION_GET_SSL(s), s->d1->cookie,
1300 &cookie_leni) == 0
1301 || cookie_leni > DTLS1_COOKIE_LENGTH) {
1302 SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1303 return 0;
1304 }
1305 s->d1->cookie_len = cookie_leni;
1306
1307 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1308 s->d1->cookie_len)) {
1309 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
1310 return 0;
1311 }
1312
1313 return 1;
1314 }
1315
1316 /*-
1317 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1318 * SecureTransport using the TLS extension block in |hello|.
1319 * Safari, since 10.6, sends exactly these extensions, in this order:
1320 * SNI,
1321 * elliptic_curves
1322 * ec_point_formats
1323 * signature_algorithms (for TLSv1.2 only)
1324 *
1325 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1326 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1327 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1328 * 10.8..10.8.3 (which don't work).
1329 */
1330 static void ssl_check_for_safari(SSL_CONNECTION *s,
1331 const CLIENTHELLO_MSG *hello)
1332 {
1333 static const unsigned char kSafariExtensionsBlock[] = {
1334 0x00, 0x0a, /* elliptic_curves extension */
1335 0x00, 0x08, /* 8 bytes */
1336 0x00, 0x06, /* 6 bytes of curve ids */
1337 0x00, 0x17, /* P-256 */
1338 0x00, 0x18, /* P-384 */
1339 0x00, 0x19, /* P-521 */
1340
1341 0x00, 0x0b, /* ec_point_formats */
1342 0x00, 0x02, /* 2 bytes */
1343 0x01, /* 1 point format */
1344 0x00, /* uncompressed */
1345 /* The following is only present in TLS 1.2 */
1346 0x00, 0x0d, /* signature_algorithms */
1347 0x00, 0x0c, /* 12 bytes */
1348 0x00, 0x0a, /* 10 bytes */
1349 0x05, 0x01, /* SHA-384/RSA */
1350 0x04, 0x01, /* SHA-256/RSA */
1351 0x02, 0x01, /* SHA-1/RSA */
1352 0x04, 0x03, /* SHA-256/ECDSA */
1353 0x02, 0x03, /* SHA-1/ECDSA */
1354 };
1355 /* Length of the common prefix (first two extensions). */
1356 static const size_t kSafariCommonExtensionsLength = 18;
1357 unsigned int type;
1358 PACKET sni, tmppkt;
1359 size_t ext_len;
1360
1361 tmppkt = hello->extensions;
1362
1363 if (!PACKET_forward(&tmppkt, 2)
1364 || !PACKET_get_net_2(&tmppkt, &type)
1365 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1366 return;
1367 }
1368
1369 if (type != TLSEXT_TYPE_server_name)
1370 return;
1371
1372 ext_len = TLS1_get_client_version(
1373 SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION ?
1374 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1375
1376 s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1377 ext_len);
1378 }
1379
1380 #define RENEG_OPTIONS_OK(options) \
1381 ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
1382 && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
1383
1384 MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt)
1385 {
1386 /* |cookie| will only be initialized for DTLS. */
1387 PACKET session_id, compression, extensions, cookie;
1388 static const unsigned char null_compression = 0;
1389 CLIENTHELLO_MSG *clienthello = NULL;
1390
1391 /* Check if this is actually an unexpected renegotiation ClientHello */
1392 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1393 if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s))) {
1394 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1395 goto err;
1396 }
1397 if (!RENEG_OPTIONS_OK(s->options)
1398 || (!s->s3.send_connection_binding
1399 && (s->options
1400 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
1401 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1402 return MSG_PROCESS_FINISHED_READING;
1403 }
1404 s->renegotiate = 1;
1405 s->new_session = 1;
1406 }
1407
1408 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1409 if (clienthello == NULL) {
1410 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1411 goto err;
1412 }
1413
1414 /*
1415 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1416 */
1417 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1418 PACKET_null_init(&cookie);
1419
1420 if (clienthello->isv2) {
1421 unsigned int mt;
1422
1423 if (!SSL_IS_FIRST_HANDSHAKE(s)
1424 || s->hello_retry_request != SSL_HRR_NONE) {
1425 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1426 goto err;
1427 }
1428
1429 /*-
1430 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1431 * header is sent directly on the wire, not wrapped as a TLS
1432 * record. Our record layer just processes the message length and passes
1433 * the rest right through. Its format is:
1434 * Byte Content
1435 * 0-1 msg_length - decoded by the record layer
1436 * 2 msg_type - s->init_msg points here
1437 * 3-4 version
1438 * 5-6 cipher_spec_length
1439 * 7-8 session_id_length
1440 * 9-10 challenge_length
1441 * ... ...
1442 */
1443
1444 if (!PACKET_get_1(pkt, &mt)
1445 || mt != SSL2_MT_CLIENT_HELLO) {
1446 /*
1447 * Should never happen. We should have tested this in the record
1448 * layer in order to have determined that this is a SSLv2 record
1449 * in the first place
1450 */
1451 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1452 goto err;
1453 }
1454 }
1455
1456 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1457 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
1458 goto err;
1459 }
1460
1461 /* Parse the message and load client random. */
1462 if (clienthello->isv2) {
1463 /*
1464 * Handle an SSLv2 backwards compatible ClientHello
1465 * Note, this is only for SSLv3+ using the backward compatible format.
1466 * Real SSLv2 is not supported, and is rejected below.
1467 */
1468 unsigned int ciphersuite_len, session_id_len, challenge_len;
1469 PACKET challenge;
1470
1471 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1472 || !PACKET_get_net_2(pkt, &session_id_len)
1473 || !PACKET_get_net_2(pkt, &challenge_len)) {
1474 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
1475 goto err;
1476 }
1477
1478 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1479 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);
1480 goto err;
1481 }
1482
1483 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1484 ciphersuite_len)
1485 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1486 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1487 /* No extensions. */
1488 || PACKET_remaining(pkt) != 0) {
1489 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
1490 goto err;
1491 }
1492 clienthello->session_id_len = session_id_len;
1493
1494 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1495 * here rather than sizeof(clienthello->random) because that is the limit
1496 * for SSLv3 and it is fixed. It won't change even if
1497 * sizeof(clienthello->random) does.
1498 */
1499 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1500 ? SSL3_RANDOM_SIZE : challenge_len;
1501 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1502 if (!PACKET_copy_bytes(&challenge,
1503 clienthello->random + SSL3_RANDOM_SIZE -
1504 challenge_len, challenge_len)
1505 /* Advertise only null compression. */
1506 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1507 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1508 goto err;
1509 }
1510
1511 PACKET_null_init(&clienthello->extensions);
1512 } else {
1513 /* Regular ClientHello. */
1514 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1515 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1516 || !PACKET_copy_all(&session_id, clienthello->session_id,
1517 SSL_MAX_SSL_SESSION_ID_LENGTH,
1518 &clienthello->session_id_len)) {
1519 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1520 goto err;
1521 }
1522
1523 if (SSL_CONNECTION_IS_DTLS(s)) {
1524 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1525 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1526 goto err;
1527 }
1528 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1529 DTLS1_COOKIE_LENGTH,
1530 &clienthello->dtls_cookie_len)) {
1531 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1532 goto err;
1533 }
1534 /*
1535 * If we require cookies and this ClientHello doesn't contain one,
1536 * just return since we do not want to allocate any memory yet.
1537 * So check cookie length...
1538 */
1539 if (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE) {
1540 if (clienthello->dtls_cookie_len == 0) {
1541 OPENSSL_free(clienthello);
1542 return MSG_PROCESS_FINISHED_READING;
1543 }
1544 }
1545 }
1546
1547 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1548 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1549 goto err;
1550 }
1551
1552 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1553 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1554 goto err;
1555 }
1556
1557 /* Could be empty. */
1558 if (PACKET_remaining(pkt) == 0) {
1559 PACKET_null_init(&clienthello->extensions);
1560 } else {
1561 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1562 || PACKET_remaining(pkt) != 0) {
1563 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1564 goto err;
1565 }
1566 }
1567 }
1568
1569 if (!PACKET_copy_all(&compression, clienthello->compressions,
1570 MAX_COMPRESSIONS_SIZE,
1571 &clienthello->compressions_len)) {
1572 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1573 goto err;
1574 }
1575
1576 /* Preserve the raw extensions PACKET for later use */
1577 extensions = clienthello->extensions;
1578 if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1579 &clienthello->pre_proc_exts,
1580 &clienthello->pre_proc_exts_len, 1)) {
1581 /* SSLfatal already been called */
1582 goto err;
1583 }
1584 s->clienthello = clienthello;
1585
1586 return MSG_PROCESS_CONTINUE_PROCESSING;
1587
1588 err:
1589 if (clienthello != NULL)
1590 OPENSSL_free(clienthello->pre_proc_exts);
1591 OPENSSL_free(clienthello);
1592
1593 return MSG_PROCESS_ERROR;
1594 }
1595
1596 static int tls_early_post_process_client_hello(SSL_CONNECTION *s)
1597 {
1598 unsigned int j;
1599 int i, al = SSL_AD_INTERNAL_ERROR;
1600 int protverr;
1601 size_t loop;
1602 unsigned long id;
1603 #ifndef OPENSSL_NO_COMP
1604 SSL_COMP *comp = NULL;
1605 #endif
1606 const SSL_CIPHER *c;
1607 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1608 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1609 CLIENTHELLO_MSG *clienthello = s->clienthello;
1610 DOWNGRADE dgrd = DOWNGRADE_NONE;
1611 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1612 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1613
1614 /* Finished parsing the ClientHello, now we can start processing it */
1615 /* Give the ClientHello callback a crack at things */
1616 if (sctx->client_hello_cb != NULL) {
1617 /* A failure in the ClientHello callback terminates the connection. */
1618 switch (sctx->client_hello_cb(ssl, &al, sctx->client_hello_cb_arg)) {
1619 case SSL_CLIENT_HELLO_SUCCESS:
1620 break;
1621 case SSL_CLIENT_HELLO_RETRY:
1622 s->rwstate = SSL_CLIENT_HELLO_CB;
1623 return -1;
1624 case SSL_CLIENT_HELLO_ERROR:
1625 default:
1626 SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
1627 goto err;
1628 }
1629 }
1630
1631 /* Set up the client_random */
1632 memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
1633
1634 /* Choose the version */
1635
1636 if (clienthello->isv2) {
1637 if (clienthello->legacy_version == SSL2_VERSION
1638 || (clienthello->legacy_version & 0xff00)
1639 != (SSL3_VERSION_MAJOR << 8)) {
1640 /*
1641 * This is real SSLv2 or something completely unknown. We don't
1642 * support it.
1643 */
1644 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL);
1645 goto err;
1646 }
1647 /* SSLv3/TLS */
1648 s->client_version = clienthello->legacy_version;
1649 }
1650 /*
1651 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1652 * versions are potentially compatible. Version negotiation comes later.
1653 */
1654 if (!SSL_CONNECTION_IS_DTLS(s)) {
1655 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1656 } else if (ssl->method->version != DTLS_ANY_VERSION &&
1657 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1658 protverr = SSL_R_VERSION_TOO_LOW;
1659 } else {
1660 protverr = 0;
1661 }
1662
1663 if (protverr) {
1664 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1665 /* like ssl3_get_record, send alert using remote version number */
1666 s->version = s->client_version = clienthello->legacy_version;
1667 }
1668 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
1669 goto err;
1670 }
1671
1672 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1673 if (SSL_CONNECTION_IS_TLS13(s)
1674 && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1675 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
1676 goto err;
1677 }
1678
1679 if (SSL_CONNECTION_IS_DTLS(s)) {
1680 /* Empty cookie was already handled above by returning early. */
1681 if (SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) {
1682 if (sctx->app_verify_cookie_cb != NULL) {
1683 if (sctx->app_verify_cookie_cb(ssl, clienthello->dtls_cookie,
1684 clienthello->dtls_cookie_len) == 0) {
1685 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1686 SSL_R_COOKIE_MISMATCH);
1687 goto err;
1688 /* else cookie verification succeeded */
1689 }
1690 /* default verification */
1691 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1692 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1693 s->d1->cookie_len) != 0) {
1694 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);
1695 goto err;
1696 }
1697 s->d1->cookie_verified = 1;
1698 }
1699 if (ssl->method->version == DTLS_ANY_VERSION) {
1700 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1701 if (protverr != 0) {
1702 s->version = s->client_version;
1703 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
1704 goto err;
1705 }
1706 }
1707 }
1708
1709 s->hit = 0;
1710
1711 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1712 clienthello->isv2) ||
1713 !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers,
1714 &scsvs, clienthello->isv2, 1)) {
1715 /* SSLfatal() already called */
1716 goto err;
1717 }
1718
1719 s->s3.send_connection_binding = 0;
1720 /* Check what signalling cipher-suite values were received. */
1721 if (scsvs != NULL) {
1722 for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1723 c = sk_SSL_CIPHER_value(scsvs, i);
1724 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1725 if (s->renegotiate) {
1726 /* SCSV is fatal if renegotiating */
1727 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1728 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1729 goto err;
1730 }
1731 s->s3.send_connection_binding = 1;
1732 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1733 !ssl_check_version_downgrade(s)) {
1734 /*
1735 * This SCSV indicates that the client previously tried
1736 * a higher version. We should fail if the current version
1737 * is an unexpected downgrade, as that indicates that the first
1738 * connection may have been tampered with in order to trigger
1739 * an insecure downgrade.
1740 */
1741 SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
1742 SSL_R_INAPPROPRIATE_FALLBACK);
1743 goto err;
1744 }
1745 }
1746 }
1747
1748 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1749 if (SSL_CONNECTION_IS_TLS13(s)) {
1750 const SSL_CIPHER *cipher =
1751 ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl));
1752
1753 if (cipher == NULL) {
1754 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
1755 goto err;
1756 }
1757 if (s->hello_retry_request == SSL_HRR_PENDING
1758 && (s->s3.tmp.new_cipher == NULL
1759 || s->s3.tmp.new_cipher->id != cipher->id)) {
1760 /*
1761 * A previous HRR picked a different ciphersuite to the one we
1762 * just selected. Something must have changed.
1763 */
1764 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
1765 goto err;
1766 }
1767 s->s3.tmp.new_cipher = cipher;
1768 }
1769
1770 /* We need to do this before getting the session */
1771 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1772 SSL_EXT_CLIENT_HELLO,
1773 clienthello->pre_proc_exts, NULL, 0)) {
1774 /* SSLfatal() already called */
1775 goto err;
1776 }
1777
1778 /*
1779 * We don't allow resumption in a backwards compatible ClientHello.
1780 * In TLS1.1+, session_id MUST be empty.
1781 *
1782 * Versions before 0.9.7 always allow clients to resume sessions in
1783 * renegotiation. 0.9.7 and later allow this by default, but optionally
1784 * ignore resumption requests with flag
1785 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1786 * than a change to default behavior so that applications relying on
1787 * this for security won't even compile against older library versions).
1788 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1789 * request renegotiation but not a new session (s->new_session remains
1790 * unset): for servers, this essentially just means that the
1791 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1792 * ignored.
1793 */
1794 if (clienthello->isv2 ||
1795 (s->new_session &&
1796 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1797 if (!ssl_get_new_session(s, 1)) {
1798 /* SSLfatal() already called */
1799 goto err;
1800 }
1801 } else {
1802 i = ssl_get_prev_session(s, clienthello);
1803 if (i == 1) {
1804 /* previous session */
1805 s->hit = 1;
1806 } else if (i == -1) {
1807 /* SSLfatal() already called */
1808 goto err;
1809 } else {
1810 /* i == 0 */
1811 if (!ssl_get_new_session(s, 1)) {
1812 /* SSLfatal() already called */
1813 goto err;
1814 }
1815 }
1816 }
1817
1818 if (SSL_CONNECTION_IS_TLS13(s)) {
1819 memcpy(s->tmp_session_id, s->clienthello->session_id,
1820 s->clienthello->session_id_len);
1821 s->tmp_session_id_len = s->clienthello->session_id_len;
1822 }
1823
1824 /*
1825 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1826 * ciphersuite compatibility with the session as part of resumption.
1827 */
1828 if (!SSL_CONNECTION_IS_TLS13(s) && s->hit) {
1829 j = 0;
1830 id = s->session->cipher->id;
1831
1832 OSSL_TRACE_BEGIN(TLS_CIPHER) {
1833 BIO_printf(trc_out, "client sent %d ciphers\n",
1834 sk_SSL_CIPHER_num(ciphers));
1835 }
1836 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1837 c = sk_SSL_CIPHER_value(ciphers, i);
1838 if (trc_out != NULL)
1839 BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
1840 sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1841 if (c->id == id) {
1842 j = 1;
1843 break;
1844 }
1845 }
1846 if (j == 0) {
1847 /*
1848 * we need to have the cipher in the cipher list if we are asked
1849 * to reuse it
1850 */
1851 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1852 SSL_R_REQUIRED_CIPHER_MISSING);
1853 OSSL_TRACE_CANCEL(TLS_CIPHER);
1854 goto err;
1855 }
1856 OSSL_TRACE_END(TLS_CIPHER);
1857 }
1858
1859 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1860 if (clienthello->compressions[loop] == 0)
1861 break;
1862 }
1863
1864 if (loop >= clienthello->compressions_len) {
1865 /* no compress */
1866 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);
1867 goto err;
1868 }
1869
1870 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1871 ssl_check_for_safari(s, clienthello);
1872
1873 /* TLS extensions */
1874 if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1875 clienthello->pre_proc_exts, NULL, 0, 1)) {
1876 /* SSLfatal() already called */
1877 goto err;
1878 }
1879
1880 /*
1881 * Check if we want to use external pre-shared secret for this handshake
1882 * for not reused session only. We need to generate server_random before
1883 * calling tls_session_secret_cb in order to allow SessionTicket
1884 * processing to use it in key derivation.
1885 */
1886 {
1887 unsigned char *pos;
1888 pos = s->s3.server_random;
1889 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1890 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1891 goto err;
1892 }
1893 }
1894
1895 if (!s->hit
1896 && s->version >= TLS1_VERSION
1897 && !SSL_CONNECTION_IS_TLS13(s)
1898 && !SSL_CONNECTION_IS_DTLS(s)
1899 && s->ext.session_secret_cb != NULL) {
1900 const SSL_CIPHER *pref_cipher = NULL;
1901 /*
1902 * s->session->master_key_length is a size_t, but this is an int for
1903 * backwards compat reasons
1904 */
1905 int master_key_length;
1906
1907 master_key_length = sizeof(s->session->master_key);
1908 if (s->ext.session_secret_cb(ssl, s->session->master_key,
1909 &master_key_length, ciphers,
1910 &pref_cipher,
1911 s->ext.session_secret_cb_arg)
1912 && master_key_length > 0) {
1913 s->session->master_key_length = master_key_length;
1914 s->hit = 1;
1915 s->peer_ciphers = ciphers;
1916 s->session->verify_result = X509_V_OK;
1917
1918 ciphers = NULL;
1919
1920 /* check if some cipher was preferred by call back */
1921 if (pref_cipher == NULL)
1922 pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
1923 SSL_get_ciphers(ssl));
1924 if (pref_cipher == NULL) {
1925 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
1926 goto err;
1927 }
1928
1929 s->session->cipher = pref_cipher;
1930 sk_SSL_CIPHER_free(s->cipher_list);
1931 s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
1932 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1933 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
1934 }
1935 }
1936
1937 /*
1938 * Worst case, we will use the NULL compression, but if we have other
1939 * options, we will now look for them. We have complen-1 compression
1940 * algorithms from the client, starting at q.
1941 */
1942 s->s3.tmp.new_compression = NULL;
1943 if (SSL_CONNECTION_IS_TLS13(s)) {
1944 /*
1945 * We already checked above that the NULL compression method appears in
1946 * the list. Now we check there aren't any others (which is illegal in
1947 * a TLSv1.3 ClientHello.
1948 */
1949 if (clienthello->compressions_len != 1) {
1950 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1951 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1952 goto err;
1953 }
1954 }
1955 #ifndef OPENSSL_NO_COMP
1956 /* This only happens if we have a cache hit */
1957 else if (s->session->compress_meth != 0) {
1958 int m, comp_id = s->session->compress_meth;
1959 unsigned int k;
1960 /* Perform sanity checks on resumed compression algorithm */
1961 /* Can't disable compression */
1962 if (!ssl_allow_compression(s)) {
1963 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1964 SSL_R_INCONSISTENT_COMPRESSION);
1965 goto err;
1966 }
1967 /* Look for resumed compression method */
1968 for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) {
1969 comp = sk_SSL_COMP_value(sctx->comp_methods, m);
1970 if (comp_id == comp->id) {
1971 s->s3.tmp.new_compression = comp;
1972 break;
1973 }
1974 }
1975 if (s->s3.tmp.new_compression == NULL) {
1976 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1977 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1978 goto err;
1979 }
1980 /* Look for resumed method in compression list */
1981 for (k = 0; k < clienthello->compressions_len; k++) {
1982 if (clienthello->compressions[k] == comp_id)
1983 break;
1984 }
1985 if (k >= clienthello->compressions_len) {
1986 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1987 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1988 goto err;
1989 }
1990 } else if (s->hit) {
1991 comp = NULL;
1992 } else if (ssl_allow_compression(s) && sctx->comp_methods) {
1993 /* See if we have a match */
1994 int m, nn, v, done = 0;
1995 unsigned int o;
1996
1997 nn = sk_SSL_COMP_num(sctx->comp_methods);
1998 for (m = 0; m < nn; m++) {
1999 comp = sk_SSL_COMP_value(sctx->comp_methods, m);
2000 v = comp->id;
2001 for (o = 0; o < clienthello->compressions_len; o++) {
2002 if (v == clienthello->compressions[o]) {
2003 done = 1;
2004 break;
2005 }
2006 }
2007 if (done)
2008 break;
2009 }
2010 if (done)
2011 s->s3.tmp.new_compression = comp;
2012 else
2013 comp = NULL;
2014 }
2015 #else
2016 /*
2017 * If compression is disabled we'd better not try to resume a session
2018 * using compression.
2019 */
2020 if (s->session->compress_meth != 0) {
2021 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
2022 goto err;
2023 }
2024 #endif
2025
2026 /*
2027 * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2028 */
2029
2030 if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
2031 sk_SSL_CIPHER_free(s->peer_ciphers);
2032 s->peer_ciphers = ciphers;
2033 if (ciphers == NULL) {
2034 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2035 goto err;
2036 }
2037 ciphers = NULL;
2038 }
2039
2040 if (!s->hit) {
2041 #ifdef OPENSSL_NO_COMP
2042 s->session->compress_meth = 0;
2043 #else
2044 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
2045 #endif
2046 if (!tls1_set_server_sigalgs(s)) {
2047 /* SSLfatal() already called */
2048 goto err;
2049 }
2050 }
2051
2052 sk_SSL_CIPHER_free(ciphers);
2053 sk_SSL_CIPHER_free(scsvs);
2054 OPENSSL_free(clienthello->pre_proc_exts);
2055 OPENSSL_free(s->clienthello);
2056 s->clienthello = NULL;
2057 return 1;
2058 err:
2059 sk_SSL_CIPHER_free(ciphers);
2060 sk_SSL_CIPHER_free(scsvs);
2061 OPENSSL_free(clienthello->pre_proc_exts);
2062 OPENSSL_free(s->clienthello);
2063 s->clienthello = NULL;
2064
2065 return 0;
2066 }
2067
2068 /*
2069 * Call the status request callback if needed. Upon success, returns 1.
2070 * Upon failure, returns 0.
2071 */
2072 static int tls_handle_status_request(SSL_CONNECTION *s)
2073 {
2074 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2075
2076 s->ext.status_expected = 0;
2077
2078 /*
2079 * If status request then ask callback what to do. Note: this must be
2080 * called after servername callbacks in case the certificate has changed,
2081 * and must be called after the cipher has been chosen because this may
2082 * influence which certificate is sent
2083 */
2084 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL
2085 && sctx->ext.status_cb != NULL) {
2086 int ret;
2087
2088 /* If no certificate can't return certificate status */
2089 if (s->s3.tmp.cert != NULL) {
2090 /*
2091 * Set current certificate to one we will use so SSL_get_certificate
2092 * et al can pick it up.
2093 */
2094 s->cert->key = s->s3.tmp.cert;
2095 ret = sctx->ext.status_cb(SSL_CONNECTION_GET_SSL(s),
2096 sctx->ext.status_arg);
2097 switch (ret) {
2098 /* We don't want to send a status request response */
2099 case SSL_TLSEXT_ERR_NOACK:
2100 s->ext.status_expected = 0;
2101 break;
2102 /* status request response should be sent */
2103 case SSL_TLSEXT_ERR_OK:
2104 if (s->ext.ocsp.resp)
2105 s->ext.status_expected = 1;
2106 break;
2107 /* something bad happened */
2108 case SSL_TLSEXT_ERR_ALERT_FATAL:
2109 default:
2110 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);
2111 return 0;
2112 }
2113 }
2114 }
2115
2116 return 1;
2117 }
2118
2119 /*
2120 * Call the alpn_select callback if needed. Upon success, returns 1.
2121 * Upon failure, returns 0.
2122 */
2123 int tls_handle_alpn(SSL_CONNECTION *s)
2124 {
2125 const unsigned char *selected = NULL;
2126 unsigned char selected_len = 0;
2127 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2128
2129 if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
2130 int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_SSL(s),
2131 &selected, &selected_len,
2132 s->s3.alpn_proposed,
2133 (unsigned int)s->s3.alpn_proposed_len,
2134 sctx->ext.alpn_select_cb_arg);
2135
2136 if (r == SSL_TLSEXT_ERR_OK) {
2137 OPENSSL_free(s->s3.alpn_selected);
2138 s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
2139 if (s->s3.alpn_selected == NULL) {
2140 s->s3.alpn_selected_len = 0;
2141 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2142 return 0;
2143 }
2144 s->s3.alpn_selected_len = selected_len;
2145 #ifndef OPENSSL_NO_NEXTPROTONEG
2146 /* ALPN takes precedence over NPN. */
2147 s->s3.npn_seen = 0;
2148 #endif
2149
2150 /* Check ALPN is consistent with session */
2151 if (s->session->ext.alpn_selected == NULL
2152 || selected_len != s->session->ext.alpn_selected_len
2153 || memcmp(selected, s->session->ext.alpn_selected,
2154 selected_len) != 0) {
2155 /* Not consistent so can't be used for early_data */
2156 s->ext.early_data_ok = 0;
2157
2158 if (!s->hit) {
2159 /*
2160 * This is a new session and so alpn_selected should have
2161 * been initialised to NULL. We should update it with the
2162 * selected ALPN.
2163 */
2164 if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2165 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2166 ERR_R_INTERNAL_ERROR);
2167 return 0;
2168 }
2169 s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2170 selected_len);
2171 if (s->session->ext.alpn_selected == NULL) {
2172 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2173 ERR_R_INTERNAL_ERROR);
2174 return 0;
2175 }
2176 s->session->ext.alpn_selected_len = selected_len;
2177 }
2178 }
2179
2180 return 1;
2181 } else if (r != SSL_TLSEXT_ERR_NOACK) {
2182 SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,
2183 SSL_R_NO_APPLICATION_PROTOCOL);
2184 return 0;
2185 }
2186 /*
2187 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2188 * present.
2189 */
2190 }
2191
2192 /* Check ALPN is consistent with session */
2193 if (s->session->ext.alpn_selected != NULL) {
2194 /* Not consistent so can't be used for early_data */
2195 s->ext.early_data_ok = 0;
2196 }
2197
2198 return 1;
2199 }
2200
2201 WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst)
2202 {
2203 const SSL_CIPHER *cipher;
2204 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2205
2206 if (wst == WORK_MORE_A) {
2207 int rv = tls_early_post_process_client_hello(s);
2208 if (rv == 0) {
2209 /* SSLfatal() was already called */
2210 goto err;
2211 }
2212 if (rv < 0)
2213 return WORK_MORE_A;
2214 wst = WORK_MORE_B;
2215 }
2216 if (wst == WORK_MORE_B) {
2217 if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
2218 /* Let cert callback update server certificates if required */
2219 if (!s->hit && s->cert->cert_cb != NULL) {
2220 int rv = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
2221 if (rv == 0) {
2222 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);
2223 goto err;
2224 }
2225 if (rv < 0) {
2226 s->rwstate = SSL_X509_LOOKUP;
2227 return WORK_MORE_B;
2228 }
2229 s->rwstate = SSL_NOTHING;
2230 }
2231
2232 /* In TLSv1.3 we selected the ciphersuite before resumption */
2233 if (!SSL_CONNECTION_IS_TLS13(s)) {
2234 cipher =
2235 ssl3_choose_cipher(s, s->peer_ciphers,
2236 SSL_get_ciphers(ssl));
2237
2238 if (cipher == NULL) {
2239 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2240 SSL_R_NO_SHARED_CIPHER);
2241 goto err;
2242 }
2243 s->s3.tmp.new_cipher = cipher;
2244 }
2245 if (!s->hit) {
2246 if (!tls_choose_sigalg(s, 1)) {
2247 /* SSLfatal already called */
2248 goto err;
2249 }
2250 /* check whether we should disable session resumption */
2251 if (s->not_resumable_session_cb != NULL)
2252 s->session->not_resumable =
2253 s->not_resumable_session_cb(ssl,
2254 ((s->s3.tmp.new_cipher->algorithm_mkey
2255 & (SSL_kDHE | SSL_kECDHE)) != 0));
2256 if (s->session->not_resumable)
2257 /* do not send a session ticket */
2258 s->ext.ticket_expected = 0;
2259 }
2260 } else {
2261 /* Session-id reuse */
2262 s->s3.tmp.new_cipher = s->session->cipher;
2263 }
2264
2265 /*-
2266 * we now have the following setup.
2267 * client_random
2268 * cipher_list - our preferred list of ciphers
2269 * ciphers - the clients preferred list of ciphers
2270 * compression - basically ignored right now
2271 * ssl version is set - sslv3
2272 * s->session - The ssl session has been setup.
2273 * s->hit - session reuse flag
2274 * s->s3.tmp.new_cipher - the new cipher to use.
2275 */
2276
2277 /*
2278 * Call status_request callback if needed. Has to be done after the
2279 * certificate callbacks etc above.
2280 */
2281 if (!tls_handle_status_request(s)) {
2282 /* SSLfatal() already called */
2283 goto err;
2284 }
2285 /*
2286 * Call alpn_select callback if needed. Has to be done after SNI and
2287 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2288 * we already did this because cipher negotiation happens earlier, and
2289 * we must handle ALPN before we decide whether to accept early_data.
2290 */
2291 if (!SSL_CONNECTION_IS_TLS13(s) && !tls_handle_alpn(s)) {
2292 /* SSLfatal() already called */
2293 goto err;
2294 }
2295
2296 wst = WORK_MORE_C;
2297 }
2298 #ifndef OPENSSL_NO_SRP
2299 if (wst == WORK_MORE_C) {
2300 int ret;
2301 if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2302 /*
2303 * callback indicates further work to be done
2304 */
2305 s->rwstate = SSL_X509_LOOKUP;
2306 return WORK_MORE_C;
2307 }
2308 if (ret < 0) {
2309 /* SSLfatal() already called */
2310 goto err;
2311 }
2312 }
2313 #endif
2314
2315 return WORK_FINISHED_STOP;
2316 err:
2317 return WORK_ERROR;
2318 }
2319
2320 int tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt)
2321 {
2322 int compm;
2323 size_t sl, len;
2324 int version;
2325 unsigned char *session_id;
2326 int usetls13 = SSL_CONNECTION_IS_TLS13(s)
2327 || s->hello_retry_request == SSL_HRR_PENDING;
2328
2329 version = usetls13 ? TLS1_2_VERSION : s->version;
2330 if (!WPACKET_put_bytes_u16(pkt, version)
2331 /*
2332 * Random stuff. Filling of the server_random takes place in
2333 * tls_process_client_hello()
2334 */
2335 || !WPACKET_memcpy(pkt,
2336 s->hello_retry_request == SSL_HRR_PENDING
2337 ? hrrrandom : s->s3.server_random,
2338 SSL3_RANDOM_SIZE)) {
2339 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2340 return 0;
2341 }
2342
2343 /*-
2344 * There are several cases for the session ID to send
2345 * back in the server hello:
2346 * - For session reuse from the session cache,
2347 * we send back the old session ID.
2348 * - If stateless session reuse (using a session ticket)
2349 * is successful, we send back the client's "session ID"
2350 * (which doesn't actually identify the session).
2351 * - If it is a new session, we send back the new
2352 * session ID.
2353 * - However, if we want the new session to be single-use,
2354 * we send back a 0-length session ID.
2355 * - In TLSv1.3 we echo back the session id sent to us by the client
2356 * regardless
2357 * s->hit is non-zero in either case of session reuse,
2358 * so the following won't overwrite an ID that we're supposed
2359 * to send back.
2360 */
2361 if (s->session->not_resumable ||
2362 (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER)
2363 && !s->hit))
2364 s->session->session_id_length = 0;
2365
2366 if (usetls13) {
2367 sl = s->tmp_session_id_len;
2368 session_id = s->tmp_session_id;
2369 } else {
2370 sl = s->session->session_id_length;
2371 session_id = s->session->session_id;
2372 }
2373
2374 if (sl > sizeof(s->session->session_id)) {
2375 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2376 return 0;
2377 }
2378
2379 /* set up the compression method */
2380 #ifdef OPENSSL_NO_COMP
2381 compm = 0;
2382 #else
2383 if (usetls13 || s->s3.tmp.new_compression == NULL)
2384 compm = 0;
2385 else
2386 compm = s->s3.tmp.new_compression->id;
2387 #endif
2388
2389 if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2390 || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher,
2391 pkt, &len)
2392 || !WPACKET_put_bytes_u8(pkt, compm)) {
2393 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2394 return 0;
2395 }
2396
2397 if (!tls_construct_extensions(s, pkt,
2398 s->hello_retry_request == SSL_HRR_PENDING
2399 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2400 : (SSL_CONNECTION_IS_TLS13(s)
2401 ? SSL_EXT_TLS1_3_SERVER_HELLO
2402 : SSL_EXT_TLS1_2_SERVER_HELLO),
2403 NULL, 0)) {
2404 /* SSLfatal() already called */
2405 return 0;
2406 }
2407
2408 if (s->hello_retry_request == SSL_HRR_PENDING) {
2409 /* Ditch the session. We'll create a new one next time around */
2410 SSL_SESSION_free(s->session);
2411 s->session = NULL;
2412 s->hit = 0;
2413
2414 /*
2415 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2416 * a synthetic message_hash in place of ClientHello1.
2417 */
2418 if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
2419 /* SSLfatal() already called */
2420 return 0;
2421 }
2422 } else if (!(s->verify_mode & SSL_VERIFY_PEER)
2423 && !ssl3_digest_cached_records(s, 0)) {
2424 /* SSLfatal() already called */;
2425 return 0;
2426 }
2427
2428 return 1;
2429 }
2430
2431 int tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt)
2432 {
2433 if (!s->s3.tmp.cert_request) {
2434 if (!ssl3_digest_cached_records(s, 0)) {
2435 /* SSLfatal() already called */
2436 return 0;
2437 }
2438 }
2439 return 1;
2440 }
2441
2442 int tls_construct_server_key_exchange(SSL_CONNECTION *s, WPACKET *pkt)
2443 {
2444 EVP_PKEY *pkdh = NULL;
2445 unsigned char *encodedPoint = NULL;
2446 size_t encodedlen = 0;
2447 int curve_id = 0;
2448 const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
2449 int i;
2450 unsigned long type;
2451 BIGNUM *r[4];
2452 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2453 EVP_PKEY_CTX *pctx = NULL;
2454 size_t paramlen, paramoffset;
2455 int freer = 0, ret = 0;
2456 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2457
2458 if (!WPACKET_get_total_written(pkt, &paramoffset)) {
2459 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2460 goto err;
2461 }
2462
2463 if (md_ctx == NULL) {
2464 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2465 goto err;
2466 }
2467
2468 type = s->s3.tmp.new_cipher->algorithm_mkey;
2469
2470 r[0] = r[1] = r[2] = r[3] = NULL;
2471 #ifndef OPENSSL_NO_PSK
2472 /* Plain PSK or RSAPSK nothing to do */
2473 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2474 } else
2475 #endif /* !OPENSSL_NO_PSK */
2476 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2477 CERT *cert = s->cert;
2478 EVP_PKEY *pkdhp = NULL;
2479
2480 if (s->cert->dh_tmp_auto) {
2481 pkdh = ssl_get_auto_dh(s);
2482 if (pkdh == NULL) {
2483 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2484 goto err;
2485 }
2486 pkdhp = pkdh;
2487 } else {
2488 pkdhp = cert->dh_tmp;
2489 }
2490 #if !defined(OPENSSL_NO_DEPRECATED_3_0)
2491 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2492 pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s),
2493 0, 1024));
2494 if (pkdh == NULL) {
2495 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2496 goto err;
2497 }
2498 pkdhp = pkdh;
2499 }
2500 #endif
2501 if (pkdhp == NULL) {
2502 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
2503 goto err;
2504 }
2505 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2506 EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
2507 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
2508 goto err;
2509 }
2510 if (s->s3.tmp.pkey != NULL) {
2511 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2512 goto err;
2513 }
2514
2515 s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
2516 if (s->s3.tmp.pkey == NULL) {
2517 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2518 goto err;
2519 }
2520
2521 EVP_PKEY_free(pkdh);
2522 pkdh = NULL;
2523
2524 /* These BIGNUMs need to be freed when we're finished */
2525 freer = 1;
2526 if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
2527 &r[0])
2528 || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
2529 &r[1])
2530 || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
2531 OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
2532 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2533 goto err;
2534 }
2535 } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2536
2537 if (s->s3.tmp.pkey != NULL) {
2538 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2539 goto err;
2540 }
2541
2542 /* Get NID of appropriate shared curve */
2543 curve_id = tls1_shared_group(s, -2);
2544 if (curve_id == 0) {
2545 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2546 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2547 goto err;
2548 }
2549 /* Cache the group used in the SSL_SESSION */
2550 s->session->kex_group = curve_id;
2551 /* Generate a new key for this curve */
2552 s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
2553 if (s->s3.tmp.pkey == NULL) {
2554 /* SSLfatal() already called */
2555 goto err;
2556 }
2557
2558 /* Encode the public key. */
2559 encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
2560 &encodedPoint);
2561 if (encodedlen == 0) {
2562 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2563 goto err;
2564 }
2565
2566 /*
2567 * We'll generate the serverKeyExchange message explicitly so we
2568 * can set these to NULLs
2569 */
2570 r[0] = NULL;
2571 r[1] = NULL;
2572 r[2] = NULL;
2573 r[3] = NULL;
2574 } else
2575 #ifndef OPENSSL_NO_SRP
2576 if (type & SSL_kSRP) {
2577 if ((s->srp_ctx.N == NULL) ||
2578 (s->srp_ctx.g == NULL) ||
2579 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2580 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
2581 goto err;
2582 }
2583 r[0] = s->srp_ctx.N;
2584 r[1] = s->srp_ctx.g;
2585 r[2] = s->srp_ctx.s;
2586 r[3] = s->srp_ctx.B;
2587 } else
2588 #endif
2589 {
2590 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2591 goto err;
2592 }
2593
2594 if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2595 || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2596 lu = NULL;
2597 } else if (lu == NULL) {
2598 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
2599 goto err;
2600 }
2601
2602 #ifndef OPENSSL_NO_PSK
2603 if (type & SSL_PSK) {
2604 size_t len = (s->cert->psk_identity_hint == NULL)
2605 ? 0 : strlen(s->cert->psk_identity_hint);
2606
2607 /*
2608 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2609 * checked this when we set the identity hint - but just in case
2610 */
2611 if (len > PSK_MAX_IDENTITY_LEN
2612 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2613 len)) {
2614 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2615 goto err;
2616 }
2617 }
2618 #endif
2619
2620 for (i = 0; i < 4 && r[i] != NULL; i++) {
2621 unsigned char *binval;
2622 int res;
2623
2624 #ifndef OPENSSL_NO_SRP
2625 if ((i == 2) && (type & SSL_kSRP)) {
2626 res = WPACKET_start_sub_packet_u8(pkt);
2627 } else
2628 #endif
2629 res = WPACKET_start_sub_packet_u16(pkt);
2630
2631 if (!res) {
2632 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2633 goto err;
2634 }
2635
2636 /*-
2637 * for interoperability with some versions of the Microsoft TLS
2638 * stack, we need to zero pad the DHE pub key to the same length
2639 * as the prime
2640 */
2641 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2642 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2643
2644 if (len > 0) {
2645 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2646 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2647 goto err;
2648 }
2649 memset(binval, 0, len);
2650 }
2651 }
2652
2653 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2654 || !WPACKET_close(pkt)) {
2655 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2656 goto err;
2657 }
2658
2659 BN_bn2bin(r[i], binval);
2660 }
2661
2662 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2663 /*
2664 * We only support named (not generic) curves. In this situation, the
2665 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2666 * [1 byte length of encoded point], followed by the actual encoded
2667 * point itself
2668 */
2669 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2670 || !WPACKET_put_bytes_u8(pkt, 0)
2671 || !WPACKET_put_bytes_u8(pkt, curve_id)
2672 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2673 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2674 goto err;
2675 }
2676 OPENSSL_free(encodedPoint);
2677 encodedPoint = NULL;
2678 }
2679
2680 /* not anonymous */
2681 if (lu != NULL) {
2682 EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
2683 const EVP_MD *md;
2684 unsigned char *sigbytes1, *sigbytes2, *tbs;
2685 size_t siglen = 0, tbslen;
2686
2687 if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) {
2688 /* Should never happen */
2689 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2690 goto err;
2691 }
2692 /* Get length of the parameters we have written above */
2693 if (!WPACKET_get_length(pkt, &paramlen)) {
2694 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2695 goto err;
2696 }
2697 /* send signature algorithm */
2698 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
2699 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2700 goto err;
2701 }
2702
2703 if (EVP_DigestSignInit_ex(md_ctx, &pctx,
2704 md == NULL ? NULL : EVP_MD_get0_name(md),
2705 sctx->libctx, sctx->propq, pkey,
2706 NULL) <= 0) {
2707 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2708 goto err;
2709 }
2710 if (lu->sig == EVP_PKEY_RSA_PSS) {
2711 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2712 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2713 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2714 goto err;
2715 }
2716 }
2717 tbslen = construct_key_exchange_tbs(s, &tbs,
2718 s->init_buf->data + paramoffset,
2719 paramlen);
2720 if (tbslen == 0) {
2721 /* SSLfatal() already called */
2722 goto err;
2723 }
2724
2725 if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0
2726 || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2727 || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
2728 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2729 || sigbytes1 != sigbytes2) {
2730 OPENSSL_free(tbs);
2731 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2732 goto err;
2733 }
2734 OPENSSL_free(tbs);
2735 }
2736
2737 ret = 1;
2738 err:
2739 EVP_PKEY_free(pkdh);
2740 OPENSSL_free(encodedPoint);
2741 EVP_MD_CTX_free(md_ctx);
2742 if (freer) {
2743 BN_free(r[0]);
2744 BN_free(r[1]);
2745 BN_free(r[2]);
2746 BN_free(r[3]);
2747 }
2748 return ret;
2749 }
2750
2751 int tls_construct_certificate_request(SSL_CONNECTION *s, WPACKET *pkt)
2752 {
2753 if (SSL_CONNECTION_IS_TLS13(s)) {
2754 /* Send random context when doing post-handshake auth */
2755 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
2756 OPENSSL_free(s->pha_context);
2757 s->pha_context_len = 32;
2758 if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
2759 s->pha_context_len = 0;
2760 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2761 return 0;
2762 }
2763 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
2764 s->pha_context, s->pha_context_len, 0) <= 0
2765 || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
2766 s->pha_context_len)) {
2767 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2768 return 0;
2769 }
2770 /* reset the handshake hash back to just after the ClientFinished */
2771 if (!tls13_restore_handshake_digest_for_pha(s)) {
2772 /* SSLfatal() already called */
2773 return 0;
2774 }
2775 } else {
2776 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2777 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2778 return 0;
2779 }
2780 }
2781
2782 if (!tls_construct_extensions(s, pkt,
2783 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2784 0)) {
2785 /* SSLfatal() already called */
2786 return 0;
2787 }
2788 goto done;
2789 }
2790
2791 /* get the list of acceptable cert types */
2792 if (!WPACKET_start_sub_packet_u8(pkt)
2793 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2794 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2795 return 0;
2796 }
2797
2798 if (SSL_USE_SIGALGS(s)) {
2799 const uint16_t *psigs;
2800 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2801
2802 if (!WPACKET_start_sub_packet_u16(pkt)
2803 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2804 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2805 || !WPACKET_close(pkt)) {
2806 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2807 return 0;
2808 }
2809 }
2810
2811 if (!construct_ca_names(s, get_ca_names(s), pkt)) {
2812 /* SSLfatal() already called */
2813 return 0;
2814 }
2815
2816 done:
2817 s->certreqs_sent++;
2818 s->s3.tmp.cert_request = 1;
2819 return 1;
2820 }
2821
2822 static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
2823 {
2824 #ifndef OPENSSL_NO_PSK
2825 unsigned char psk[PSK_MAX_PSK_LEN];
2826 size_t psklen;
2827 PACKET psk_identity;
2828
2829 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2830 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2831 return 0;
2832 }
2833 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2834 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
2835 return 0;
2836 }
2837 if (s->psk_server_callback == NULL) {
2838 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
2839 return 0;
2840 }
2841
2842 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2843 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2844 return 0;
2845 }
2846
2847 psklen = s->psk_server_callback(SSL_CONNECTION_GET_SSL(s),
2848 s->session->psk_identity,
2849 psk, sizeof(psk));
2850
2851 if (psklen > PSK_MAX_PSK_LEN) {
2852 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2853 return 0;
2854 } else if (psklen == 0) {
2855 /*
2856 * PSK related to the given identity not found
2857 */
2858 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
2859 return 0;
2860 }
2861
2862 OPENSSL_free(s->s3.tmp.psk);
2863 s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
2864 OPENSSL_cleanse(psk, psklen);
2865
2866 if (s->s3.tmp.psk == NULL) {
2867 s->s3.tmp.psklen = 0;
2868 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2869 return 0;
2870 }
2871
2872 s->s3.tmp.psklen = psklen;
2873
2874 return 1;
2875 #else
2876 /* Should never happen */
2877 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2878 return 0;
2879 #endif
2880 }
2881
2882 static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt)
2883 {
2884 size_t outlen;
2885 PACKET enc_premaster;
2886 EVP_PKEY *rsa = NULL;
2887 unsigned char *rsa_decrypt = NULL;
2888 int ret = 0;
2889 EVP_PKEY_CTX *ctx = NULL;
2890 OSSL_PARAM params[3], *p = params;
2891 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2892
2893 rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
2894 if (rsa == NULL) {
2895 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
2896 return 0;
2897 }
2898
2899 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2900 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2901 enc_premaster = *pkt;
2902 } else {
2903 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2904 || PACKET_remaining(pkt) != 0) {
2905 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2906 return 0;
2907 }
2908 }
2909
2910 outlen = SSL_MAX_MASTER_KEY_LENGTH;
2911 rsa_decrypt = OPENSSL_malloc(outlen);
2912 if (rsa_decrypt == NULL) {
2913 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2914 return 0;
2915 }
2916
2917 ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq);
2918 if (ctx == NULL) {
2919 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2920 goto err;
2921 }
2922
2923 /*
2924 * We must not leak whether a decryption failure occurs because of
2925 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2926 * section 7.4.7.1). We use the special padding type
2927 * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the
2928 * RSA, check the padding and check that the client version is as expected
2929 * in the premaster secret. If any of that fails then the function appears
2930 * to return successfully but with a random result. The call below could
2931 * still fail if the input is publicly invalid.
2932 * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2933 */
2934 if (EVP_PKEY_decrypt_init(ctx) <= 0
2935 || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
2936 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
2937 goto err;
2938 }
2939
2940 *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
2941 (unsigned int *)&s->client_version);
2942 if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
2943 *p++ = OSSL_PARAM_construct_uint(
2944 OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
2945 (unsigned int *)&s->version);
2946 *p++ = OSSL_PARAM_construct_end();
2947
2948 if (!EVP_PKEY_CTX_set_params(ctx, params)
2949 || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
2950 PACKET_data(&enc_premaster),
2951 PACKET_remaining(&enc_premaster)) <= 0) {
2952 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
2953 goto err;
2954 }
2955
2956 /*
2957 * This test should never fail (otherwise we should have failed above) but
2958 * we double check anyway.
2959 */
2960 if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
2961 OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
2962 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
2963 goto err;
2964 }
2965
2966 /* Also cleanses rsa_decrypt (on success or failure) */
2967 if (!ssl_generate_master_secret(s, rsa_decrypt,
2968 SSL_MAX_MASTER_KEY_LENGTH, 0)) {
2969 /* SSLfatal() already called */
2970 goto err;
2971 }
2972
2973 ret = 1;
2974 err:
2975 OPENSSL_free(rsa_decrypt);
2976 EVP_PKEY_CTX_free(ctx);
2977 return ret;
2978 }
2979
2980 static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt)
2981 {
2982 EVP_PKEY *skey = NULL;
2983 unsigned int i;
2984 const unsigned char *data;
2985 EVP_PKEY *ckey = NULL;
2986 int ret = 0;
2987
2988 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2989 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2990 goto err;
2991 }
2992 skey = s->s3.tmp.pkey;
2993 if (skey == NULL) {
2994 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
2995 goto err;
2996 }
2997
2998 if (PACKET_remaining(pkt) == 0L) {
2999 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
3000 goto err;
3001 }
3002 if (!PACKET_get_bytes(pkt, &data, i)) {
3003 /* We already checked we have enough data */
3004 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3005 goto err;
3006 }
3007 ckey = EVP_PKEY_new();
3008 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
3009 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
3010 goto err;
3011 }
3012
3013 if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) {
3014 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3015 goto err;
3016 }
3017
3018 if (ssl_derive(s, skey, ckey, 1) == 0) {
3019 /* SSLfatal() already called */
3020 goto err;
3021 }
3022
3023 ret = 1;
3024 EVP_PKEY_free(s->s3.tmp.pkey);
3025 s->s3.tmp.pkey = NULL;
3026 err:
3027 EVP_PKEY_free(ckey);
3028 return ret;
3029 }
3030
3031 static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt)
3032 {
3033 EVP_PKEY *skey = s->s3.tmp.pkey;
3034 EVP_PKEY *ckey = NULL;
3035 int ret = 0;
3036
3037 if (PACKET_remaining(pkt) == 0L) {
3038 /* We don't support ECDH client auth */
3039 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
3040 goto err;
3041 } else {
3042 unsigned int i;
3043 const unsigned char *data;
3044
3045 /*
3046 * Get client's public key from encoded point in the
3047 * ClientKeyExchange message.
3048 */
3049
3050 /* Get encoded point length */
3051 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
3052 || PACKET_remaining(pkt) != 0) {
3053 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3054 goto err;
3055 }
3056 if (skey == NULL) {
3057 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
3058 goto err;
3059 }
3060
3061 ckey = EVP_PKEY_new();
3062 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3063 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
3064 goto err;
3065 }
3066
3067 if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
3068 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
3069 goto err;
3070 }
3071 }
3072
3073 if (ssl_derive(s, skey, ckey, 1) == 0) {
3074 /* SSLfatal() already called */
3075 goto err;
3076 }
3077
3078 ret = 1;
3079 EVP_PKEY_free(s->s3.tmp.pkey);
3080 s->s3.tmp.pkey = NULL;
3081 err:
3082 EVP_PKEY_free(ckey);
3083
3084 return ret;
3085 }
3086
3087 static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt)
3088 {
3089 #ifndef OPENSSL_NO_SRP
3090 unsigned int i;
3091 const unsigned char *data;
3092
3093 if (!PACKET_get_net_2(pkt, &i)
3094 || !PACKET_get_bytes(pkt, &data, i)) {
3095 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
3096 return 0;
3097 }
3098 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3099 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
3100 return 0;
3101 }
3102 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3103 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
3104 return 0;
3105 }
3106 OPENSSL_free(s->session->srp_username);
3107 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3108 if (s->session->srp_username == NULL) {
3109 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3110 return 0;
3111 }
3112
3113 if (!srp_generate_server_master_secret(s)) {
3114 /* SSLfatal() already called */
3115 return 0;
3116 }
3117
3118 return 1;
3119 #else
3120 /* Should never happen */
3121 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3122 return 0;
3123 #endif
3124 }
3125
3126 static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt)
3127 {
3128 #ifndef OPENSSL_NO_GOST
3129 EVP_PKEY_CTX *pkey_ctx;
3130 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3131 unsigned char premaster_secret[32];
3132 const unsigned char *start;
3133 size_t outlen = 32, inlen;
3134 unsigned long alg_a;
3135 GOST_KX_MESSAGE *pKX = NULL;
3136 const unsigned char *ptr;
3137 int ret = 0;
3138 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3139
3140 /* Get our certificate private key */
3141 alg_a = s->s3.tmp.new_cipher->algorithm_auth;
3142 if (alg_a & SSL_aGOST12) {
3143 /*
3144 * New GOST ciphersuites have SSL_aGOST01 bit too
3145 */
3146 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3147 if (pk == NULL) {
3148 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3149 }
3150 if (pk == NULL) {
3151 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3152 }
3153 } else if (alg_a & SSL_aGOST01) {
3154 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3155 }
3156
3157 pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
3158 if (pkey_ctx == NULL) {
3159 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3160 return 0;
3161 }
3162 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3163 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3164 return 0;
3165 }
3166 /*
3167 * If client certificate is present and is of the same type, maybe
3168 * use it for key exchange. Don't mind errors from
3169 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3170 * client certificate for authorization only.
3171 */
3172 client_pub_pkey = X509_get0_pubkey(s->session->peer);
3173 if (client_pub_pkey) {
3174 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3175 ERR_clear_error();
3176 }
3177
3178 ptr = PACKET_data(pkt);
3179 /* Some implementations provide extra data in the opaqueBlob
3180 * We have nothing to do with this blob so we just skip it */
3181 pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
3182 if (pKX == NULL
3183 || pKX->kxBlob == NULL
3184 || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
3185 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3186 goto err;
3187 }
3188
3189 if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
3190 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
3191 goto err;
3192 }
3193
3194 if (PACKET_remaining(pkt) != 0) {
3195 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
3196 goto err;
3197 }
3198
3199 inlen = pKX->kxBlob->value.sequence->length;
3200 start = pKX->kxBlob->value.sequence->data;
3201
3202 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3203 inlen) <= 0) {
3204 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3205 goto err;
3206 }
3207 /* Generate master secret */
3208 if (!ssl_generate_master_secret(s, premaster_secret,
3209 sizeof(premaster_secret), 0)) {
3210 /* SSLfatal() already called */
3211 goto err;
3212 }
3213 /* Check if pubkey from client certificate was used */
3214 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3215 NULL) > 0)
3216 s->statem.no_cert_verify = 1;
3217
3218 ret = 1;
3219 err:
3220 EVP_PKEY_CTX_free(pkey_ctx);
3221 GOST_KX_MESSAGE_free(pKX);
3222 return ret;
3223 #else
3224 /* Should never happen */
3225 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3226 return 0;
3227 #endif
3228 }
3229
3230 static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt)
3231 {
3232 #ifndef OPENSSL_NO_GOST
3233 unsigned char rnd_dgst[32];
3234 EVP_PKEY_CTX *pkey_ctx = NULL;
3235 EVP_PKEY *pk = NULL;
3236 unsigned char premaster_secret[32];
3237 const unsigned char *start = NULL;
3238 size_t outlen = 32, inlen = 0;
3239 int ret = 0;
3240 int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3241 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3242
3243 if (cipher_nid == NID_undef) {
3244 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3245 return 0;
3246 }
3247
3248 if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3249 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3250 goto err;
3251 }
3252
3253 /* Get our certificate private key */
3254 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ?
3255 s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey :
3256 s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3257 if (pk == NULL) {
3258 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
3259 goto err;
3260 }
3261
3262 pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);
3263 if (pkey_ctx == NULL) {
3264 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3265 goto err;
3266 }
3267 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3268 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3269 goto err;
3270 }
3271
3272 /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3273 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
3274 EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
3275 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3276 goto err;
3277 }
3278
3279 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
3280 EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
3281 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3282 goto err;
3283 }
3284 inlen = PACKET_remaining(pkt);
3285 start = PACKET_data(pkt);
3286
3287 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
3288 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3289 goto err;
3290 }
3291 /* Generate master secret */
3292 if (!ssl_generate_master_secret(s, premaster_secret,
3293 sizeof(premaster_secret), 0)) {
3294 /* SSLfatal() already called */
3295 goto err;
3296 }
3297 ret = 1;
3298
3299 err:
3300 EVP_PKEY_CTX_free(pkey_ctx);
3301 return ret;
3302 #else
3303 /* Should never happen */
3304 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3305 return 0;
3306 #endif
3307 }
3308
3309 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s,
3310 PACKET *pkt)
3311 {
3312 unsigned long alg_k;
3313
3314 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3315
3316 /* For PSK parse and retrieve identity, obtain PSK key */
3317 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3318 /* SSLfatal() already called */
3319 goto err;
3320 }
3321
3322 if (alg_k & SSL_kPSK) {
3323 /* Identity extracted earlier: should be nothing left */
3324 if (PACKET_remaining(pkt) != 0) {
3325 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3326 goto err;
3327 }
3328 /* PSK handled by ssl_generate_master_secret */
3329 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3330 /* SSLfatal() already called */
3331 goto err;
3332 }
3333 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3334 if (!tls_process_cke_rsa(s, pkt)) {
3335 /* SSLfatal() already called */
3336 goto err;
3337 }
3338 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3339 if (!tls_process_cke_dhe(s, pkt)) {
3340 /* SSLfatal() already called */
3341 goto err;
3342 }
3343 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3344 if (!tls_process_cke_ecdhe(s, pkt)) {
3345 /* SSLfatal() already called */
3346 goto err;
3347 }
3348 } else if (alg_k & SSL_kSRP) {
3349 if (!tls_process_cke_srp(s, pkt)) {
3350 /* SSLfatal() already called */
3351 goto err;
3352 }
3353 } else if (alg_k & SSL_kGOST) {
3354 if (!tls_process_cke_gost(s, pkt)) {
3355 /* SSLfatal() already called */
3356 goto err;
3357 }
3358 } else if (alg_k & SSL_kGOST18) {
3359 if (!tls_process_cke_gost18(s, pkt)) {
3360 /* SSLfatal() already called */
3361 goto err;
3362 }
3363 } else {
3364 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
3365 goto err;
3366 }
3367
3368 return MSG_PROCESS_CONTINUE_PROCESSING;
3369 err:
3370 #ifndef OPENSSL_NO_PSK
3371 OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
3372 s->s3.tmp.psk = NULL;
3373 s->s3.tmp.psklen = 0;
3374 #endif
3375 return MSG_PROCESS_ERROR;
3376 }
3377
3378 WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s,
3379 WORK_STATE wst)
3380 {
3381 #ifndef OPENSSL_NO_SCTP
3382 if (wst == WORK_MORE_A) {
3383 if (SSL_CONNECTION_IS_DTLS(s)) {
3384 unsigned char sctpauthkey[64];
3385 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3386 size_t labellen;
3387 /*
3388 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3389 * used.
3390 */
3391 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3392 sizeof(DTLS1_SCTP_AUTH_LABEL));
3393
3394 /* Don't include the terminating zero. */
3395 labellen = sizeof(labelbuffer) - 1;
3396 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3397 labellen += 1;
3398
3399 if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s),
3400 sctpauthkey,
3401 sizeof(sctpauthkey), labelbuffer,
3402 labellen, NULL, 0,
3403 0) <= 0) {
3404 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3405 return WORK_ERROR;
3406 }
3407
3408 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3409 sizeof(sctpauthkey), sctpauthkey);
3410 }
3411 }
3412 #endif
3413
3414 if (s->statem.no_cert_verify || !s->session->peer) {
3415 /*
3416 * No certificate verify or no peer certificate so we no longer need
3417 * the handshake_buffer
3418 */
3419 if (!ssl3_digest_cached_records(s, 0)) {
3420 /* SSLfatal() already called */
3421 return WORK_ERROR;
3422 }
3423 return WORK_FINISHED_CONTINUE;
3424 } else {
3425 if (!s->s3.handshake_buffer) {
3426 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3427 return WORK_ERROR;
3428 }
3429 /*
3430 * For sigalgs freeze the handshake buffer. If we support
3431 * extms we've done this already so this is a no-op
3432 */
3433 if (!ssl3_digest_cached_records(s, 1)) {
3434 /* SSLfatal() already called */
3435 return WORK_ERROR;
3436 }
3437 }
3438
3439 return WORK_FINISHED_CONTINUE;
3440 }
3441
3442 MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s,
3443 PACKET *pkt)
3444 {
3445 int i;
3446 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
3447 X509 *x = NULL;
3448 unsigned long l;
3449 const unsigned char *certstart, *certbytes;
3450 STACK_OF(X509) *sk = NULL;
3451 PACKET spkt, context;
3452 size_t chainidx;
3453 SSL_SESSION *new_sess = NULL;
3454 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3455
3456 /*
3457 * To get this far we must have read encrypted data from the client. We no
3458 * longer tolerate unencrypted alerts. This value is ignored if less than
3459 * TLSv1.3
3460 */
3461 s->statem.enc_read_state = ENC_READ_STATE_VALID;
3462
3463 if ((sk = sk_X509_new_null()) == NULL) {
3464 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3465 goto err;
3466 }
3467
3468 if (SSL_CONNECTION_IS_TLS13(s)
3469 && (!PACKET_get_length_prefixed_1(pkt, &context)
3470 || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
3471 || (s->pha_context != NULL
3472 && !PACKET_equal(&context, s->pha_context,
3473 s->pha_context_len)))) {
3474 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
3475 goto err;
3476 }
3477
3478 if (!PACKET_get_length_prefixed_3(pkt, &spkt)
3479 || PACKET_remaining(pkt) != 0) {
3480 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3481 goto err;
3482 }
3483
3484 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3485 if (!PACKET_get_net_3(&spkt, &l)
3486 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3487 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
3488 goto err;
3489 }
3490
3491 certstart = certbytes;
3492 x = X509_new_ex(sctx->libctx, sctx->propq);
3493 if (x == NULL) {
3494 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_MALLOC_FAILURE);
3495 goto err;
3496 }
3497 if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
3498 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
3499 goto err;
3500 }
3501
3502 if (certbytes != (certstart + l)) {
3503 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
3504 goto err;
3505 }
3506
3507 if (SSL_CONNECTION_IS_TLS13(s)) {
3508 RAW_EXTENSION *rawexts = NULL;
3509 PACKET extensions;
3510
3511 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3512 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
3513 goto err;
3514 }
3515 if (!tls_collect_extensions(s, &extensions,
3516 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3517 NULL, chainidx == 0)
3518 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3519 rawexts, x, chainidx,
3520 PACKET_remaining(&spkt) == 0)) {
3521 OPENSSL_free(rawexts);
3522 goto err;
3523 }
3524 OPENSSL_free(rawexts);
3525 }
3526
3527 if (!sk_X509_push(sk, x)) {
3528 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3529 goto err;
3530 }
3531 x = NULL;
3532 }
3533
3534 if (sk_X509_num(sk) <= 0) {
3535 /* TLS does not mind 0 certs returned */
3536 if (s->version == SSL3_VERSION) {
3537 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3538 SSL_R_NO_CERTIFICATES_RETURNED);
3539 goto err;
3540 }
3541 /* Fail for TLS only if we required a certificate */
3542 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3543 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3544 SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
3545 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3546 goto err;
3547 }
3548 /* No client certificate so digest cached records */
3549 if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3550 /* SSLfatal() already called */
3551 goto err;
3552 }
3553 } else {
3554 EVP_PKEY *pkey;
3555 i = ssl_verify_cert_chain(s, sk);
3556 if (i <= 0) {
3557 SSLfatal(s, ssl_x509err2alert(s->verify_result),
3558 SSL_R_CERTIFICATE_VERIFY_FAILED);
3559 goto err;
3560 }
3561 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3562 if (pkey == NULL) {
3563 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3564 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3565 goto err;
3566 }
3567 }
3568
3569 /*
3570 * Sessions must be immutable once they go into the session cache. Otherwise
3571 * we can get multi-thread problems. Therefore we don't "update" sessions,
3572 * we replace them with a duplicate. Here, we need to do this every time
3573 * a new certificate is received via post-handshake authentication, as the
3574 * session may have already gone into the session cache.
3575 */
3576
3577 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3578 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
3579 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3580 goto err;
3581 }
3582
3583 SSL_SESSION_free(s->session);
3584 s->session = new_sess;
3585 }
3586
3587 X509_free(s->session->peer);
3588 s->session->peer = sk_X509_shift(sk);
3589 s->session->verify_result = s->verify_result;
3590
3591 OSSL_STACK_OF_X509_free(s->session->peer_chain);
3592 s->session->peer_chain = sk;
3593 sk = NULL;
3594
3595 /*
3596 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3597 * message
3598 */
3599 if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3600 /* SSLfatal() already called */
3601 goto err;
3602 }
3603
3604 /*
3605 * Inconsistency alert: cert_chain does *not* include the peer's own
3606 * certificate, while we do include it in statem_clnt.c
3607 */
3608
3609 /* Save the current hash state for when we receive the CertificateVerify */
3610 if (SSL_CONNECTION_IS_TLS13(s)) {
3611 if (!ssl_handshake_hash(s, s->cert_verify_hash,
3612 sizeof(s->cert_verify_hash),
3613 &s->cert_verify_hash_len)) {
3614 /* SSLfatal() already called */
3615 goto err;
3616 }
3617
3618 /* Resend session tickets */
3619 s->sent_tickets = 0;
3620 }
3621
3622 ret = MSG_PROCESS_CONTINUE_READING;
3623
3624 err:
3625 X509_free(x);
3626 OSSL_STACK_OF_X509_free(sk);
3627 return ret;
3628 }
3629
3630 int tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt)
3631 {
3632 CERT_PKEY *cpk = s->s3.tmp.cert;
3633
3634 if (cpk == NULL) {
3635 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3636 return 0;
3637 }
3638
3639 /*
3640 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3641 * for the server Certificate message
3642 */
3643 if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
3644 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3645 return 0;
3646 }
3647 if (!ssl3_output_cert_chain(s, pkt, cpk)) {
3648 /* SSLfatal() already called */
3649 return 0;
3650 }
3651
3652 return 1;
3653 }
3654
3655 static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt,
3656 uint32_t age_add, unsigned char *tick_nonce)
3657 {
3658 uint32_t timeout = (uint32_t)s->session->timeout;
3659
3660 /*
3661 * Ticket lifetime hint:
3662 * In TLSv1.3 we reset the "time" field above, and always specify the
3663 * timeout, limited to a 1 week period per RFC8446.
3664 * For TLSv1.2 this is advisory only and we leave this unspecified for
3665 * resumed session (for simplicity).
3666 */
3667 #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
3668
3669 if (SSL_CONNECTION_IS_TLS13(s)) {
3670 if (s->session->timeout > ONE_WEEK_SEC)
3671 timeout = ONE_WEEK_SEC;
3672 } else if (s->hit)
3673 timeout = 0;
3674
3675 if (!WPACKET_put_bytes_u32(pkt, timeout)) {
3676 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3677 return 0;
3678 }
3679
3680 if (SSL_CONNECTION_IS_TLS13(s)) {
3681 if (!WPACKET_put_bytes_u32(pkt, age_add)
3682 || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
3683 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3684 return 0;
3685 }
3686 }
3687
3688 /* Start the sub-packet for the actual ticket data */
3689 if (!WPACKET_start_sub_packet_u16(pkt)) {
3690 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3691 return 0;
3692 }
3693
3694 return 1;
3695 }
3696
3697 static int construct_stateless_ticket(SSL_CONNECTION *s, WPACKET *pkt,
3698 uint32_t age_add,
3699 unsigned char *tick_nonce)
3700 {
3701 unsigned char *senc = NULL;
3702 EVP_CIPHER_CTX *ctx = NULL;
3703 SSL_HMAC *hctx = NULL;
3704 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3705 const unsigned char *const_p;
3706 int len, slen_full, slen, lenfinal;
3707 SSL_SESSION *sess;
3708 size_t hlen;
3709 SSL_CTX *tctx = s->session_ctx;
3710 unsigned char iv[EVP_MAX_IV_LENGTH];
3711 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3712 int iv_len, ok = 0;
3713 size_t macoffset, macendoffset;
3714 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3715 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3716
3717 /* get session encoding length */
3718 slen_full = i2d_SSL_SESSION(s->session, NULL);
3719 /*
3720 * Some length values are 16 bits, so forget it if session is too
3721 * long
3722 */
3723 if (slen_full == 0 || slen_full > 0xFF00) {
3724 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3725 goto err;
3726 }
3727 senc = OPENSSL_malloc(slen_full);
3728 if (senc == NULL) {
3729 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3730 goto err;
3731 }
3732
3733 ctx = EVP_CIPHER_CTX_new();
3734 hctx = ssl_hmac_new(tctx);
3735 if (ctx == NULL || hctx == NULL) {
3736 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3737 goto err;
3738 }
3739
3740 p = senc;
3741 if (!i2d_SSL_SESSION(s->session, &p)) {
3742 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3743 goto err;
3744 }
3745
3746 /*
3747 * create a fresh copy (not shared with other threads) to clean up
3748 */
3749 const_p = senc;
3750 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3751 if (sess == NULL) {
3752 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3753 goto err;
3754 }
3755
3756 slen = i2d_SSL_SESSION(sess, NULL);
3757 if (slen == 0 || slen > slen_full) {
3758 /* shouldn't ever happen */
3759 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3760 SSL_SESSION_free(sess);
3761 goto err;
3762 }
3763 p = senc;
3764 if (!i2d_SSL_SESSION(sess, &p)) {
3765 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3766 SSL_SESSION_free(sess);
3767 goto err;
3768 }
3769 SSL_SESSION_free(sess);
3770
3771 /*
3772 * Initialize HMAC and cipher contexts. If callback present it does
3773 * all the work otherwise use generated values from parent ctx.
3774 */
3775 #ifndef OPENSSL_NO_DEPRECATED_3_0
3776 if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
3777 #else
3778 if (tctx->ext.ticket_key_evp_cb != NULL)
3779 #endif
3780 {
3781 int ret = 0;
3782
3783 if (tctx->ext.ticket_key_evp_cb != NULL)
3784 ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx,
3785 ssl_hmac_get0_EVP_MAC_CTX(hctx),
3786 1);
3787 #ifndef OPENSSL_NO_DEPRECATED_3_0
3788 else if (tctx->ext.ticket_key_cb != NULL)
3789 /* if 0 is returned, write an empty ticket */
3790 ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx,
3791 ssl_hmac_get0_HMAC_CTX(hctx), 1);
3792 #endif
3793
3794 if (ret == 0) {
3795
3796 /* Put timeout and length */
3797 if (!WPACKET_put_bytes_u32(pkt, 0)
3798 || !WPACKET_put_bytes_u16(pkt, 0)) {
3799 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3800 goto err;
3801 }
3802 OPENSSL_free(senc);
3803 EVP_CIPHER_CTX_free(ctx);
3804 ssl_hmac_free(hctx);
3805 return 1;
3806 }
3807 if (ret < 0) {
3808 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
3809 goto err;
3810 }
3811 iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
3812 } else {
3813 EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC",
3814 sctx->propq);
3815
3816 if (cipher == NULL) {
3817 /* Error is already recorded */
3818 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
3819 goto err;
3820 }
3821
3822 iv_len = EVP_CIPHER_get_iv_length(cipher);
3823 if (iv_len < 0
3824 || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0
3825 || !EVP_EncryptInit_ex(ctx, cipher, NULL,
3826 tctx->ext.secure->tick_aes_key, iv)
3827 || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
3828 sizeof(tctx->ext.secure->tick_hmac_key),
3829 "SHA256")) {
3830 EVP_CIPHER_free(cipher);
3831 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3832 goto err;
3833 }
3834 EVP_CIPHER_free(cipher);
3835 memcpy(key_name, tctx->ext.tick_key_name,
3836 sizeof(tctx->ext.tick_key_name));
3837 }
3838
3839 if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3840 /* SSLfatal() already called */
3841 goto err;
3842 }
3843
3844 if (!WPACKET_get_total_written(pkt, &macoffset)
3845 /* Output key name */
3846 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3847 /* output IV */
3848 || !WPACKET_memcpy(pkt, iv, iv_len)
3849 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3850 &encdata1)
3851 /* Encrypt session data */
3852 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3853 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3854 || encdata1 != encdata2
3855 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3856 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3857 || encdata1 + len != encdata2
3858 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3859 || !WPACKET_get_total_written(pkt, &macendoffset)
3860 || !ssl_hmac_update(hctx,
3861 (unsigned char *)s->init_buf->data + macoffset,
3862 macendoffset - macoffset)
3863 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3864 || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
3865 || hlen > EVP_MAX_MD_SIZE
3866 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3867 || macdata1 != macdata2) {
3868 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3869 goto err;
3870 }
3871
3872 /* Close the sub-packet created by create_ticket_prequel() */
3873 if (!WPACKET_close(pkt)) {
3874 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3875 goto err;
3876 }
3877
3878 ok = 1;
3879 err:
3880 OPENSSL_free(senc);
3881 EVP_CIPHER_CTX_free(ctx);
3882 ssl_hmac_free(hctx);
3883 return ok;
3884 }
3885
3886 static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt,
3887 uint32_t age_add,
3888 unsigned char *tick_nonce)
3889 {
3890 if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3891 /* SSLfatal() already called */
3892 return 0;
3893 }
3894
3895 if (!WPACKET_memcpy(pkt, s->session->session_id,
3896 s->session->session_id_length)
3897 || !WPACKET_close(pkt)) {
3898 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3899 return 0;
3900 }
3901
3902 return 1;
3903 }
3904
3905 int tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt)
3906 {
3907 SSL_CTX *tctx = s->session_ctx;
3908 unsigned char tick_nonce[TICKET_NONCE_SIZE];
3909 union {
3910 unsigned char age_add_c[sizeof(uint32_t)];
3911 uint32_t age_add;
3912 } age_add_u;
3913
3914 age_add_u.age_add = 0;
3915
3916 if (SSL_CONNECTION_IS_TLS13(s)) {
3917 size_t i, hashlen;
3918 uint64_t nonce;
3919 static const unsigned char nonce_label[] = "resumption";
3920 const EVP_MD *md = ssl_handshake_md(s);
3921 int hashleni = EVP_MD_get_size(md);
3922
3923 /* Ensure cast to size_t is safe */
3924 if (!ossl_assert(hashleni >= 0)) {
3925 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3926 goto err;
3927 }
3928 hashlen = (size_t)hashleni;
3929
3930 /*
3931 * If we already sent one NewSessionTicket, or we resumed then
3932 * s->session may already be in a cache and so we must not modify it.
3933 * Instead we need to take a copy of it and modify that.
3934 */
3935 if (s->sent_tickets != 0 || s->hit) {
3936 SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
3937
3938 if (new_sess == NULL) {
3939 /* SSLfatal already called */
3940 goto err;
3941 }
3942
3943 SSL_SESSION_free(s->session);
3944 s->session = new_sess;
3945 }
3946
3947 if (!ssl_generate_session_id(s, s->session)) {
3948 /* SSLfatal() already called */
3949 goto err;
3950 }
3951 if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
3952 age_add_u.age_add_c, sizeof(age_add_u), 0) <= 0) {
3953 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3954 goto err;
3955 }
3956 s->session->ext.tick_age_add = age_add_u.age_add;
3957
3958 nonce = s->next_ticket_nonce;
3959 for (i = TICKET_NONCE_SIZE; i > 0; i--) {
3960 tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
3961 nonce >>= 8;
3962 }
3963
3964 if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
3965 nonce_label,
3966 sizeof(nonce_label) - 1,
3967 tick_nonce,
3968 TICKET_NONCE_SIZE,
3969 s->session->master_key,
3970 hashlen, 1)) {
3971 /* SSLfatal() already called */
3972 goto err;
3973 }
3974 s->session->master_key_length = hashlen;
3975
3976 s->session->time = time(NULL);
3977 ssl_session_calculate_timeout(s->session);
3978 if (s->s3.alpn_selected != NULL) {
3979 OPENSSL_free(s->session->ext.alpn_selected);
3980 s->session->ext.alpn_selected =
3981 OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
3982 if (s->session->ext.alpn_selected == NULL) {
3983 s->session->ext.alpn_selected_len = 0;
3984 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3985 goto err;
3986 }
3987 s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
3988 }
3989 s->session->ext.max_early_data = s->max_early_data;
3990 }
3991
3992 if (tctx->generate_ticket_cb != NULL &&
3993 tctx->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s),
3994 tctx->ticket_cb_data) == 0) {
3995 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3996 goto err;
3997 }
3998 /*
3999 * If we are using anti-replay protection then we behave as if
4000 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4001 * is no point in using full stateless tickets.
4002 */
4003 if (SSL_CONNECTION_IS_TLS13(s)
4004 && ((s->options & SSL_OP_NO_TICKET) != 0
4005 || (s->max_early_data > 0
4006 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
4007 if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
4008 /* SSLfatal() already called */
4009 goto err;
4010 }
4011 } else if (!construct_stateless_ticket(s, pkt, age_add_u.age_add,
4012 tick_nonce)) {
4013 /* SSLfatal() already called */
4014 goto err;
4015 }
4016
4017 if (SSL_CONNECTION_IS_TLS13(s)) {
4018 if (!tls_construct_extensions(s, pkt,
4019 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
4020 NULL, 0)) {
4021 /* SSLfatal() already called */
4022 goto err;
4023 }
4024 /*
4025 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4026 * gets reset to 0 if we send more tickets following a post-handshake
4027 * auth, but |next_ticket_nonce| does not. If we're sending extra
4028 * tickets, decrement the count of pending extra tickets.
4029 */
4030 s->sent_tickets++;
4031 s->next_ticket_nonce++;
4032 if (s->ext.extra_tickets_expected > 0)
4033 s->ext.extra_tickets_expected--;
4034 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
4035 }
4036
4037 return 1;
4038 err:
4039 return 0;
4040 }
4041
4042 /*
4043 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4044 * create a separate message. Returns 1 on success or 0 on failure.
4045 */
4046 int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt)
4047 {
4048 if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
4049 || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
4050 s->ext.ocsp.resp_len)) {
4051 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4052 return 0;
4053 }
4054
4055 return 1;
4056 }
4057
4058 int tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt)
4059 {
4060 if (!tls_construct_cert_status_body(s, pkt)) {
4061 /* SSLfatal() already called */
4062 return 0;
4063 }
4064
4065 return 1;
4066 }
4067
4068 #ifndef OPENSSL_NO_NEXTPROTONEG
4069 /*
4070 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4071 * It sets the next_proto member in s if found
4072 */
4073 MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt)
4074 {
4075 PACKET next_proto, padding;
4076 size_t next_proto_len;
4077
4078 /*-
4079 * The payload looks like:
4080 * uint8 proto_len;
4081 * uint8 proto[proto_len];
4082 * uint8 padding_len;
4083 * uint8 padding[padding_len];
4084 */
4085 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
4086 || !PACKET_get_length_prefixed_1(pkt, &padding)
4087 || PACKET_remaining(pkt) > 0) {
4088 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4089 return MSG_PROCESS_ERROR;
4090 }
4091
4092 if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
4093 s->ext.npn_len = 0;
4094 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4095 return MSG_PROCESS_ERROR;
4096 }
4097
4098 s->ext.npn_len = (unsigned char)next_proto_len;
4099
4100 return MSG_PROCESS_CONTINUE_READING;
4101 }
4102 #endif
4103
4104 static int tls_construct_encrypted_extensions(SSL_CONNECTION *s, WPACKET *pkt)
4105 {
4106 if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
4107 NULL, 0)) {
4108 /* SSLfatal() already called */
4109 return 0;
4110 }
4111
4112 return 1;
4113 }
4114
4115 MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt)
4116 {
4117 if (PACKET_remaining(pkt) != 0) {
4118 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4119 return MSG_PROCESS_ERROR;
4120 }
4121
4122 if (s->early_data_state != SSL_EARLY_DATA_READING
4123 && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
4124 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4125 return MSG_PROCESS_ERROR;
4126 }
4127
4128 /*
4129 * EndOfEarlyData signals a key change so the end of the message must be on
4130 * a record boundary.
4131 */
4132 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
4133 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
4134 return MSG_PROCESS_ERROR;
4135 }
4136
4137 s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
4138 if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s,
4139 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
4140 /* SSLfatal() already called */
4141 return MSG_PROCESS_ERROR;
4142 }
4143
4144 return MSG_PROCESS_CONTINUE_READING;
4145 }