2 * Copyright 1995-2018 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.
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
13 #include "../ssl_locl.h"
14 #include "statem_locl.h"
15 #include "internal/constant_time_locl.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/hmac.h>
22 #include <openssl/x509.h>
23 #include <openssl/dh.h>
24 #include <openssl/bn.h>
25 #include <openssl/md5.h>
26 #include <openssl/trace.h>
28 #define TICKET_NONCE_SIZE 8
30 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
33 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
34 * handshake state transitions when a TLSv1.3 server is reading messages from
35 * the client. The message type that the client has sent is provided in |mt|.
36 * The current state is in |s->statem.hand_state|.
38 * Return values are 1 for success (transition allowed) and 0 on error
39 * (transition not allowed)
41 static int ossl_statem_server13_read_transition(SSL *s, int mt)
43 OSSL_STATEM *st = &s->statem;
46 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
47 * not negotiated TLSv1.3 yet, so that case is handled by
48 * ossl_statem_server_read_transition()
50 switch (st->hand_state) {
54 case TLS_ST_EARLY_DATA:
55 if (s->hello_retry_request == SSL_HRR_PENDING) {
56 if (mt == SSL3_MT_CLIENT_HELLO) {
57 st->hand_state = TLS_ST_SR_CLNT_HELLO;
61 } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
62 if (mt == SSL3_MT_END_OF_EARLY_DATA) {
63 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
70 case TLS_ST_SR_END_OF_EARLY_DATA:
71 case TLS_ST_SW_FINISHED:
72 if (s->s3->tmp.cert_request) {
73 if (mt == SSL3_MT_CERTIFICATE) {
74 st->hand_state = TLS_ST_SR_CERT;
78 if (mt == SSL3_MT_FINISHED) {
79 st->hand_state = TLS_ST_SR_FINISHED;
86 if (s->session->peer == NULL) {
87 if (mt == SSL3_MT_FINISHED) {
88 st->hand_state = TLS_ST_SR_FINISHED;
92 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
93 st->hand_state = TLS_ST_SR_CERT_VRFY;
99 case TLS_ST_SR_CERT_VRFY:
100 if (mt == SSL3_MT_FINISHED) {
101 st->hand_state = TLS_ST_SR_FINISHED;
108 * Its never ok to start processing handshake messages in the middle of
109 * early data (i.e. before we've received the end of early data alert)
111 if (s->early_data_state == SSL_EARLY_DATA_READING)
114 if (mt == SSL3_MT_CERTIFICATE
115 && s->post_handshake_auth == SSL_PHA_REQUESTED) {
116 st->hand_state = TLS_ST_SR_CERT;
120 if (mt == SSL3_MT_KEY_UPDATE) {
121 st->hand_state = TLS_ST_SR_KEY_UPDATE;
127 /* No valid transition found */
132 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
133 * handshake state transitions when the server is reading messages from the
134 * client. The message type that the client has sent is provided in |mt|. The
135 * current state is in |s->statem.hand_state|.
137 * Return values are 1 for success (transition allowed) and 0 on error
138 * (transition not allowed)
140 int ossl_statem_server_read_transition(SSL *s, int mt)
142 OSSL_STATEM *st = &s->statem;
144 if (SSL_IS_TLS13(s)) {
145 if (!ossl_statem_server13_read_transition(s, mt))
150 switch (st->hand_state) {
156 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
157 if (mt == SSL3_MT_CLIENT_HELLO) {
158 st->hand_state = TLS_ST_SR_CLNT_HELLO;
163 case TLS_ST_SW_SRVR_DONE:
165 * If we get a CKE message after a ServerDone then either
166 * 1) We didn't request a Certificate
168 * 2) If we did request one then
169 * a) We allow no Certificate to be returned
171 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
172 * list if we requested a certificate)
174 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
175 if (s->s3->tmp.cert_request) {
176 if (s->version == SSL3_VERSION) {
177 if ((s->verify_mode & SSL_VERIFY_PEER)
178 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
180 * This isn't an unexpected message as such - we're just
181 * not going to accept it because we require a client
184 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
185 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
186 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
189 st->hand_state = TLS_ST_SR_KEY_EXCH;
193 st->hand_state = TLS_ST_SR_KEY_EXCH;
196 } else if (s->s3->tmp.cert_request) {
197 if (mt == SSL3_MT_CERTIFICATE) {
198 st->hand_state = TLS_ST_SR_CERT;
205 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
206 st->hand_state = TLS_ST_SR_KEY_EXCH;
211 case TLS_ST_SR_KEY_EXCH:
213 * We should only process a CertificateVerify message if we have
214 * received a Certificate from the client. If so then |s->session->peer|
215 * will be non NULL. In some instances a CertificateVerify message is
216 * not required even if the peer has sent a Certificate (e.g. such as in
217 * the case of static DH). In that case |st->no_cert_verify| should be
220 if (s->session->peer == NULL || st->no_cert_verify) {
221 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
223 * For the ECDH ciphersuites when the client sends its ECDH
224 * pub key in a certificate, the CertificateVerify message is
225 * not sent. Also for GOST ciphersuites when the client uses
226 * its key from the certificate for key exchange.
228 st->hand_state = TLS_ST_SR_CHANGE;
232 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
233 st->hand_state = TLS_ST_SR_CERT_VRFY;
239 case TLS_ST_SR_CERT_VRFY:
240 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
241 st->hand_state = TLS_ST_SR_CHANGE;
246 case TLS_ST_SR_CHANGE:
247 #ifndef OPENSSL_NO_NEXTPROTONEG
248 if (s->s3->npn_seen) {
249 if (mt == SSL3_MT_NEXT_PROTO) {
250 st->hand_state = TLS_ST_SR_NEXT_PROTO;
255 if (mt == SSL3_MT_FINISHED) {
256 st->hand_state = TLS_ST_SR_FINISHED;
259 #ifndef OPENSSL_NO_NEXTPROTONEG
264 #ifndef OPENSSL_NO_NEXTPROTONEG
265 case TLS_ST_SR_NEXT_PROTO:
266 if (mt == SSL3_MT_FINISHED) {
267 st->hand_state = TLS_ST_SR_FINISHED;
273 case TLS_ST_SW_FINISHED:
274 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
275 st->hand_state = TLS_ST_SR_CHANGE;
282 /* No valid transition found */
283 if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
287 * CCS messages don't have a message sequence number so this is probably
288 * because of an out-of-order CCS. We'll just drop it.
291 s->rwstate = SSL_READING;
292 rbio = SSL_get_rbio(s);
293 BIO_clear_retry_flags(rbio);
294 BIO_set_retry_read(rbio);
297 SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
298 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
299 SSL_R_UNEXPECTED_MESSAGE);
304 * Should we send a ServerKeyExchange message?
306 * Valid return values are:
310 static int send_server_key_exchange(SSL *s)
312 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
315 * only send a ServerKeyExchange if DH or fortezza but we have a
316 * sign only certificate PSK: may send PSK identity hints For
317 * ECC ciphersuites, we send a serverKeyExchange message only if
318 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
319 * the server certificate contains the server's public key for
322 if (alg_k & (SSL_kDHE | SSL_kECDHE)
324 * PSK: send ServerKeyExchange if PSK identity hint if
327 #ifndef OPENSSL_NO_PSK
328 /* Only send SKE if we have identity hint for plain PSK */
329 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
330 && s->cert->psk_identity_hint)
331 /* For other PSK always send SKE */
332 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
334 #ifndef OPENSSL_NO_SRP
335 /* SRP: send ServerKeyExchange */
336 || (alg_k & SSL_kSRP)
346 * Should we send a CertificateRequest message?
348 * Valid return values are:
352 int send_certificate_request(SSL *s)
355 /* don't request cert unless asked for it: */
356 s->verify_mode & SSL_VERIFY_PEER
358 * don't request if post-handshake-only unless doing
359 * post-handshake in TLSv1.3:
361 && (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
362 || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
364 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
367 && (s->certreqs_sent < 1 ||
368 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
370 * never request cert in anonymous ciphersuites (see
371 * section "Certificate request" in SSL 3 drafts and in
374 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
376 * ... except when the application insists on
377 * verification (against the specs, but statem_clnt.c accepts
380 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
381 /* don't request certificate for SRP auth */
382 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
384 * With normal PSK Certificates and Certificate Requests
387 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
395 * ossl_statem_server13_write_transition() works out what handshake state to
396 * move to next when a TLSv1.3 server is writing messages to be sent to the
399 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
401 OSSL_STATEM *st = &s->statem;
404 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
405 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
408 switch (st->hand_state) {
410 /* Shouldn't happen */
411 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
412 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION,
413 ERR_R_INTERNAL_ERROR);
414 return WRITE_TRAN_ERROR;
417 if (s->key_update != SSL_KEY_UPDATE_NONE) {
418 st->hand_state = TLS_ST_SW_KEY_UPDATE;
419 return WRITE_TRAN_CONTINUE;
421 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
422 st->hand_state = TLS_ST_SW_CERT_REQ;
423 return WRITE_TRAN_CONTINUE;
425 /* Try to read from the client instead */
426 return WRITE_TRAN_FINISHED;
428 case TLS_ST_SR_CLNT_HELLO:
429 st->hand_state = TLS_ST_SW_SRVR_HELLO;
430 return WRITE_TRAN_CONTINUE;
432 case TLS_ST_SW_SRVR_HELLO:
433 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
434 && s->hello_retry_request != SSL_HRR_COMPLETE)
435 st->hand_state = TLS_ST_SW_CHANGE;
436 else if (s->hello_retry_request == SSL_HRR_PENDING)
437 st->hand_state = TLS_ST_EARLY_DATA;
439 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
440 return WRITE_TRAN_CONTINUE;
442 case TLS_ST_SW_CHANGE:
443 if (s->hello_retry_request == SSL_HRR_PENDING)
444 st->hand_state = TLS_ST_EARLY_DATA;
446 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
447 return WRITE_TRAN_CONTINUE;
449 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
451 st->hand_state = TLS_ST_SW_FINISHED;
452 else if (send_certificate_request(s))
453 st->hand_state = TLS_ST_SW_CERT_REQ;
455 st->hand_state = TLS_ST_SW_CERT;
457 return WRITE_TRAN_CONTINUE;
459 case TLS_ST_SW_CERT_REQ:
460 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
461 s->post_handshake_auth = SSL_PHA_REQUESTED;
462 st->hand_state = TLS_ST_OK;
464 st->hand_state = TLS_ST_SW_CERT;
466 return WRITE_TRAN_CONTINUE;
469 st->hand_state = TLS_ST_SW_CERT_VRFY;
470 return WRITE_TRAN_CONTINUE;
472 case TLS_ST_SW_CERT_VRFY:
473 st->hand_state = TLS_ST_SW_FINISHED;
474 return WRITE_TRAN_CONTINUE;
476 case TLS_ST_SW_FINISHED:
477 st->hand_state = TLS_ST_EARLY_DATA;
478 return WRITE_TRAN_CONTINUE;
480 case TLS_ST_EARLY_DATA:
481 return WRITE_TRAN_FINISHED;
483 case TLS_ST_SR_FINISHED:
485 * Technically we have finished the handshake at this point, but we're
486 * going to remain "in_init" for now and write out any session tickets
489 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
490 s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
491 } else if (!s->ext.ticket_expected) {
493 * If we're not going to renew the ticket then we just finish the
494 * handshake at this point.
496 st->hand_state = TLS_ST_OK;
497 return WRITE_TRAN_CONTINUE;
499 if (s->num_tickets > s->sent_tickets)
500 st->hand_state = TLS_ST_SW_SESSION_TICKET;
502 st->hand_state = TLS_ST_OK;
503 return WRITE_TRAN_CONTINUE;
505 case TLS_ST_SR_KEY_UPDATE:
506 if (s->key_update != SSL_KEY_UPDATE_NONE) {
507 st->hand_state = TLS_ST_SW_KEY_UPDATE;
508 return WRITE_TRAN_CONTINUE;
512 case TLS_ST_SW_KEY_UPDATE:
513 st->hand_state = TLS_ST_OK;
514 return WRITE_TRAN_CONTINUE;
516 case TLS_ST_SW_SESSION_TICKET:
517 /* In a resumption we only ever send a maximum of one new ticket.
518 * Following an initial handshake we send the number of tickets we have
519 * been configured for.
521 if (s->hit || s->num_tickets <= s->sent_tickets) {
522 /* We've written enough tickets out. */
523 st->hand_state = TLS_ST_OK;
525 return WRITE_TRAN_CONTINUE;
530 * ossl_statem_server_write_transition() works out what handshake state to move
531 * to next when the server is writing messages to be sent to the client.
533 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
535 OSSL_STATEM *st = &s->statem;
538 * Note that before the ClientHello we don't know what version we are going
539 * to negotiate yet, so we don't take this branch until later
543 return ossl_statem_server13_write_transition(s);
545 switch (st->hand_state) {
547 /* Shouldn't happen */
548 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
549 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION,
550 ERR_R_INTERNAL_ERROR);
551 return WRITE_TRAN_ERROR;
554 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
555 /* We must be trying to renegotiate */
556 st->hand_state = TLS_ST_SW_HELLO_REQ;
557 st->request_state = TLS_ST_BEFORE;
558 return WRITE_TRAN_CONTINUE;
560 /* Must be an incoming ClientHello */
561 if (!tls_setup_handshake(s)) {
562 /* SSLfatal() already called */
563 return WRITE_TRAN_ERROR;
568 /* Just go straight to trying to read from the client */
569 return WRITE_TRAN_FINISHED;
571 case TLS_ST_SW_HELLO_REQ:
572 st->hand_state = TLS_ST_OK;
573 return WRITE_TRAN_CONTINUE;
575 case TLS_ST_SR_CLNT_HELLO:
576 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
577 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
578 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
579 } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
580 /* We must have rejected the renegotiation */
581 st->hand_state = TLS_ST_OK;
582 return WRITE_TRAN_CONTINUE;
584 st->hand_state = TLS_ST_SW_SRVR_HELLO;
586 return WRITE_TRAN_CONTINUE;
588 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
589 return WRITE_TRAN_FINISHED;
591 case TLS_ST_SW_SRVR_HELLO:
593 if (s->ext.ticket_expected)
594 st->hand_state = TLS_ST_SW_SESSION_TICKET;
596 st->hand_state = TLS_ST_SW_CHANGE;
598 /* Check if it is anon DH or anon ECDH, */
599 /* normal PSK or SRP */
600 if (!(s->s3->tmp.new_cipher->algorithm_auth &
601 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
602 st->hand_state = TLS_ST_SW_CERT;
603 } else if (send_server_key_exchange(s)) {
604 st->hand_state = TLS_ST_SW_KEY_EXCH;
605 } else if (send_certificate_request(s)) {
606 st->hand_state = TLS_ST_SW_CERT_REQ;
608 st->hand_state = TLS_ST_SW_SRVR_DONE;
611 return WRITE_TRAN_CONTINUE;
614 if (s->ext.status_expected) {
615 st->hand_state = TLS_ST_SW_CERT_STATUS;
616 return WRITE_TRAN_CONTINUE;
620 case TLS_ST_SW_CERT_STATUS:
621 if (send_server_key_exchange(s)) {
622 st->hand_state = TLS_ST_SW_KEY_EXCH;
623 return WRITE_TRAN_CONTINUE;
627 case TLS_ST_SW_KEY_EXCH:
628 if (send_certificate_request(s)) {
629 st->hand_state = TLS_ST_SW_CERT_REQ;
630 return WRITE_TRAN_CONTINUE;
634 case TLS_ST_SW_CERT_REQ:
635 st->hand_state = TLS_ST_SW_SRVR_DONE;
636 return WRITE_TRAN_CONTINUE;
638 case TLS_ST_SW_SRVR_DONE:
639 return WRITE_TRAN_FINISHED;
641 case TLS_ST_SR_FINISHED:
643 st->hand_state = TLS_ST_OK;
644 return WRITE_TRAN_CONTINUE;
645 } else if (s->ext.ticket_expected) {
646 st->hand_state = TLS_ST_SW_SESSION_TICKET;
648 st->hand_state = TLS_ST_SW_CHANGE;
650 return WRITE_TRAN_CONTINUE;
652 case TLS_ST_SW_SESSION_TICKET:
653 st->hand_state = TLS_ST_SW_CHANGE;
654 return WRITE_TRAN_CONTINUE;
656 case TLS_ST_SW_CHANGE:
657 st->hand_state = TLS_ST_SW_FINISHED;
658 return WRITE_TRAN_CONTINUE;
660 case TLS_ST_SW_FINISHED:
662 return WRITE_TRAN_FINISHED;
664 st->hand_state = TLS_ST_OK;
665 return WRITE_TRAN_CONTINUE;
670 * Perform any pre work that needs to be done prior to sending a message from
671 * the server to the client.
673 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
675 OSSL_STATEM *st = &s->statem;
677 switch (st->hand_state) {
679 /* No pre work to be done */
682 case TLS_ST_SW_HELLO_REQ:
685 dtls1_clear_sent_buffer(s);
688 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
690 if (SSL_IS_DTLS(s)) {
691 dtls1_clear_sent_buffer(s);
692 /* We don't buffer this message so don't use the timer */
697 case TLS_ST_SW_SRVR_HELLO:
698 if (SSL_IS_DTLS(s)) {
700 * Messages we write from now on should be buffered and
701 * retransmitted if necessary, so we need to use the timer now
707 case TLS_ST_SW_SRVR_DONE:
708 #ifndef OPENSSL_NO_SCTP
709 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
710 /* Calls SSLfatal() as required */
711 return dtls_wait_for_dry(s);
714 return WORK_FINISHED_CONTINUE;
716 case TLS_ST_SW_SESSION_TICKET:
717 if (SSL_IS_TLS13(s) && s->sent_tickets == 0) {
719 * Actually this is the end of the handshake, but we're going
720 * straight into writing the session ticket out. So we finish off
721 * the handshake, but keep the various buffers active.
723 * Calls SSLfatal as required.
725 return tls_finish_handshake(s, wst, 0, 0);
726 } if (SSL_IS_DTLS(s)) {
728 * We're into the last flight. We don't retransmit the last flight
729 * unless we need to, so we don't use the timer
735 case TLS_ST_SW_CHANGE:
738 s->session->cipher = s->s3->tmp.new_cipher;
739 if (!s->method->ssl3_enc->setup_key_block(s)) {
740 /* SSLfatal() already called */
743 if (SSL_IS_DTLS(s)) {
745 * We're into the last flight. We don't retransmit the last flight
746 * unless we need to, so we don't use the timer. This might have
747 * already been set to 0 if we sent a NewSessionTicket message,
748 * but we'll set it again here in case we didn't.
752 return WORK_FINISHED_CONTINUE;
754 case TLS_ST_EARLY_DATA:
755 if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
756 && (s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
757 return WORK_FINISHED_CONTINUE;
761 /* Calls SSLfatal() as required */
762 return tls_finish_handshake(s, wst, 1, 1);
765 return WORK_FINISHED_CONTINUE;
768 static ossl_inline int conn_is_closed(void)
770 switch (get_last_sys_error()) {
775 #if defined(ECONNRESET)
785 * Perform any work that needs to be done after sending a message from the
786 * server to the client.
788 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
790 OSSL_STATEM *st = &s->statem;
794 switch (st->hand_state) {
796 /* No post work to be done */
799 case TLS_ST_SW_HELLO_REQ:
800 if (statem_flush(s) != 1)
802 if (!ssl3_init_finished_mac(s)) {
803 /* SSLfatal() already called */
808 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
809 if (statem_flush(s) != 1)
811 /* HelloVerifyRequest resets Finished MAC */
812 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
813 /* SSLfatal() already called */
817 * The next message should be another ClientHello which we need to
818 * treat like it was the first packet
823 case TLS_ST_SW_SRVR_HELLO:
824 if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
825 if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
826 && statem_flush(s) != 1)
830 #ifndef OPENSSL_NO_SCTP
831 if (SSL_IS_DTLS(s) && s->hit) {
832 unsigned char sctpauthkey[64];
833 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
837 * Add new shared key for SCTP-Auth, will be ignored if no
840 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
841 sizeof(DTLS1_SCTP_AUTH_LABEL));
843 /* Don't include the terminating zero. */
844 labellen = sizeof(labelbuffer) - 1;
845 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
848 if (SSL_export_keying_material(s, sctpauthkey,
849 sizeof(sctpauthkey), labelbuffer,
852 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
853 SSL_F_OSSL_STATEM_SERVER_POST_WORK,
854 ERR_R_INTERNAL_ERROR);
858 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
859 sizeof(sctpauthkey), sctpauthkey);
863 || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
864 && s->hello_retry_request != SSL_HRR_COMPLETE))
868 case TLS_ST_SW_CHANGE:
869 if (s->hello_retry_request == SSL_HRR_PENDING) {
870 if (!statem_flush(s))
875 if (SSL_IS_TLS13(s)) {
876 if (!s->method->ssl3_enc->setup_key_block(s)
877 || !s->method->ssl3_enc->change_cipher_state(s,
878 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
879 /* SSLfatal() already called */
883 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
884 && !s->method->ssl3_enc->change_cipher_state(s,
885 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
886 /* SSLfatal() already called */
890 * We don't yet know whether the next record we are going to receive
891 * is an unencrypted alert, an encrypted alert, or an encrypted
892 * handshake message. We temporarily tolerate unencrypted alerts.
894 s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
898 #ifndef OPENSSL_NO_SCTP
899 if (SSL_IS_DTLS(s) && !s->hit) {
901 * Change to new shared key of SCTP-Auth, will be ignored if
904 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
908 if (!s->method->ssl3_enc->change_cipher_state(s,
909 SSL3_CHANGE_CIPHER_SERVER_WRITE))
911 /* SSLfatal() already called */
916 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
919 case TLS_ST_SW_SRVR_DONE:
920 if (statem_flush(s) != 1)
924 case TLS_ST_SW_FINISHED:
925 if (statem_flush(s) != 1)
927 #ifndef OPENSSL_NO_SCTP
928 if (SSL_IS_DTLS(s) && s->hit) {
930 * Change to new shared key of SCTP-Auth, will be ignored if
933 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
937 if (SSL_IS_TLS13(s)) {
938 if (!s->method->ssl3_enc->generate_master_secret(s,
939 s->master_secret, s->handshake_secret, 0,
940 &s->session->master_key_length)
941 || !s->method->ssl3_enc->change_cipher_state(s,
942 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
943 /* SSLfatal() already called */
948 case TLS_ST_SW_CERT_REQ:
949 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
950 if (statem_flush(s) != 1)
955 case TLS_ST_SW_KEY_UPDATE:
956 if (statem_flush(s) != 1)
958 if (!tls13_update_key(s, 1)) {
959 /* SSLfatal() already called */
964 case TLS_ST_SW_SESSION_TICKET:
966 if (SSL_IS_TLS13(s) && statem_flush(s) != 1) {
967 if (SSL_get_error(s, 0) == SSL_ERROR_SYSCALL
968 && conn_is_closed()) {
970 * We ignore connection closed errors in TLSv1.3 when sending a
971 * NewSessionTicket and behave as if we were successful. This is
972 * so that we are still able to read data sent to us by a client
973 * that closes soon after the end of the handshake without
974 * waiting to read our post-handshake NewSessionTickets.
976 s->rwstate = SSL_NOTHING;
985 return WORK_FINISHED_CONTINUE;
989 * Get the message construction function and message type for sending from the
992 * Valid return values are:
996 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
997 confunc_f *confunc, int *mt)
999 OSSL_STATEM *st = &s->statem;
1001 switch (st->hand_state) {
1003 /* Shouldn't happen */
1004 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1005 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE,
1006 SSL_R_BAD_HANDSHAKE_STATE);
1009 case TLS_ST_SW_CHANGE:
1011 *confunc = dtls_construct_change_cipher_spec;
1013 *confunc = tls_construct_change_cipher_spec;
1014 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1017 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1018 *confunc = dtls_construct_hello_verify_request;
1019 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
1022 case TLS_ST_SW_HELLO_REQ:
1023 /* No construction function needed */
1025 *mt = SSL3_MT_HELLO_REQUEST;
1028 case TLS_ST_SW_SRVR_HELLO:
1029 *confunc = tls_construct_server_hello;
1030 *mt = SSL3_MT_SERVER_HELLO;
1033 case TLS_ST_SW_CERT:
1034 *confunc = tls_construct_server_certificate;
1035 *mt = SSL3_MT_CERTIFICATE;
1038 case TLS_ST_SW_CERT_VRFY:
1039 *confunc = tls_construct_cert_verify;
1040 *mt = SSL3_MT_CERTIFICATE_VERIFY;
1044 case TLS_ST_SW_KEY_EXCH:
1045 *confunc = tls_construct_server_key_exchange;
1046 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
1049 case TLS_ST_SW_CERT_REQ:
1050 *confunc = tls_construct_certificate_request;
1051 *mt = SSL3_MT_CERTIFICATE_REQUEST;
1054 case TLS_ST_SW_SRVR_DONE:
1055 *confunc = tls_construct_server_done;
1056 *mt = SSL3_MT_SERVER_DONE;
1059 case TLS_ST_SW_SESSION_TICKET:
1060 *confunc = tls_construct_new_session_ticket;
1061 *mt = SSL3_MT_NEWSESSION_TICKET;
1064 case TLS_ST_SW_CERT_STATUS:
1065 *confunc = tls_construct_cert_status;
1066 *mt = SSL3_MT_CERTIFICATE_STATUS;
1069 case TLS_ST_SW_FINISHED:
1070 *confunc = tls_construct_finished;
1071 *mt = SSL3_MT_FINISHED;
1074 case TLS_ST_EARLY_DATA:
1076 *mt = SSL3_MT_DUMMY;
1079 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
1080 *confunc = tls_construct_encrypted_extensions;
1081 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
1084 case TLS_ST_SW_KEY_UPDATE:
1085 *confunc = tls_construct_key_update;
1086 *mt = SSL3_MT_KEY_UPDATE;
1094 * Maximum size (excluding the Handshake header) of a ClientHello message,
1095 * calculated as follows:
1097 * 2 + # client_version
1098 * 32 + # only valid length for random
1099 * 1 + # length of session_id
1100 * 32 + # maximum size for session_id
1101 * 2 + # length of cipher suites
1102 * 2^16-2 + # maximum length of cipher suites array
1103 * 1 + # length of compression_methods
1104 * 2^8-1 + # maximum length of compression methods
1105 * 2 + # length of extensions
1106 * 2^16-1 # maximum length of extensions
1108 #define CLIENT_HELLO_MAX_LENGTH 131396
1110 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1111 #define NEXT_PROTO_MAX_LENGTH 514
1114 * Returns the maximum allowed length for the current message that we are
1115 * reading. Excludes the message header.
1117 size_t ossl_statem_server_max_message_size(SSL *s)
1119 OSSL_STATEM *st = &s->statem;
1121 switch (st->hand_state) {
1123 /* Shouldn't happen */
1126 case TLS_ST_SR_CLNT_HELLO:
1127 return CLIENT_HELLO_MAX_LENGTH;
1129 case TLS_ST_SR_END_OF_EARLY_DATA:
1130 return END_OF_EARLY_DATA_MAX_LENGTH;
1132 case TLS_ST_SR_CERT:
1133 return s->max_cert_list;
1135 case TLS_ST_SR_KEY_EXCH:
1136 return CLIENT_KEY_EXCH_MAX_LENGTH;
1138 case TLS_ST_SR_CERT_VRFY:
1139 return SSL3_RT_MAX_PLAIN_LENGTH;
1141 #ifndef OPENSSL_NO_NEXTPROTONEG
1142 case TLS_ST_SR_NEXT_PROTO:
1143 return NEXT_PROTO_MAX_LENGTH;
1146 case TLS_ST_SR_CHANGE:
1147 return CCS_MAX_LENGTH;
1149 case TLS_ST_SR_FINISHED:
1150 return FINISHED_MAX_LENGTH;
1152 case TLS_ST_SR_KEY_UPDATE:
1153 return KEY_UPDATE_MAX_LENGTH;
1158 * Process a message that the server has received from the client.
1160 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1162 OSSL_STATEM *st = &s->statem;
1164 switch (st->hand_state) {
1166 /* Shouldn't happen */
1167 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1168 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE,
1169 ERR_R_INTERNAL_ERROR);
1170 return MSG_PROCESS_ERROR;
1172 case TLS_ST_SR_CLNT_HELLO:
1173 return tls_process_client_hello(s, pkt);
1175 case TLS_ST_SR_END_OF_EARLY_DATA:
1176 return tls_process_end_of_early_data(s, pkt);
1178 case TLS_ST_SR_CERT:
1179 return tls_process_client_certificate(s, pkt);
1181 case TLS_ST_SR_KEY_EXCH:
1182 return tls_process_client_key_exchange(s, pkt);
1184 case TLS_ST_SR_CERT_VRFY:
1185 return tls_process_cert_verify(s, pkt);
1187 #ifndef OPENSSL_NO_NEXTPROTONEG
1188 case TLS_ST_SR_NEXT_PROTO:
1189 return tls_process_next_proto(s, pkt);
1192 case TLS_ST_SR_CHANGE:
1193 return tls_process_change_cipher_spec(s, pkt);
1195 case TLS_ST_SR_FINISHED:
1196 return tls_process_finished(s, pkt);
1198 case TLS_ST_SR_KEY_UPDATE:
1199 return tls_process_key_update(s, pkt);
1205 * Perform any further processing required following the receipt of a message
1208 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1210 OSSL_STATEM *st = &s->statem;
1212 switch (st->hand_state) {
1214 /* Shouldn't happen */
1215 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1216 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE,
1217 ERR_R_INTERNAL_ERROR);
1220 case TLS_ST_SR_CLNT_HELLO:
1221 return tls_post_process_client_hello(s, wst);
1223 case TLS_ST_SR_KEY_EXCH:
1224 return tls_post_process_client_key_exchange(s, wst);
1228 #ifndef OPENSSL_NO_SRP
1229 /* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1230 static int ssl_check_srp_ext_ClientHello(SSL *s)
1233 int al = SSL_AD_UNRECOGNIZED_NAME;
1235 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1236 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1237 if (s->srp_ctx.login == NULL) {
1239 * RFC 5054 says SHOULD reject, we do so if There is no srp
1242 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1243 SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1244 SSL_R_PSK_IDENTITY_NOT_FOUND);
1247 ret = SSL_srp_server_param_with_username(s, &al);
1250 if (ret == SSL3_AL_FATAL) {
1251 SSLfatal(s, al, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1252 al == SSL_AD_UNKNOWN_PSK_IDENTITY
1253 ? SSL_R_PSK_IDENTITY_NOT_FOUND
1254 : SSL_R_CLIENTHELLO_TLSEXT);
1263 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1266 /* Always use DTLS 1.0 version: see RFC 6347 */
1267 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1268 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1274 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1276 unsigned int cookie_leni;
1277 if (s->ctx->app_gen_cookie_cb == NULL ||
1278 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1279 &cookie_leni) == 0 ||
1280 cookie_leni > 255) {
1281 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1282 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1285 s->d1->cookie_len = cookie_leni;
1287 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1288 s->d1->cookie_len)) {
1289 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1290 ERR_R_INTERNAL_ERROR);
1297 #ifndef OPENSSL_NO_EC
1299 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1300 * SecureTransport using the TLS extension block in |hello|.
1301 * Safari, since 10.6, sends exactly these extensions, in this order:
1305 * signature_algorithms (for TLSv1.2 only)
1307 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1308 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1309 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1310 * 10.8..10.8.3 (which don't work).
1312 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1314 static const unsigned char kSafariExtensionsBlock[] = {
1315 0x00, 0x0a, /* elliptic_curves extension */
1316 0x00, 0x08, /* 8 bytes */
1317 0x00, 0x06, /* 6 bytes of curve ids */
1318 0x00, 0x17, /* P-256 */
1319 0x00, 0x18, /* P-384 */
1320 0x00, 0x19, /* P-521 */
1322 0x00, 0x0b, /* ec_point_formats */
1323 0x00, 0x02, /* 2 bytes */
1324 0x01, /* 1 point format */
1325 0x00, /* uncompressed */
1326 /* The following is only present in TLS 1.2 */
1327 0x00, 0x0d, /* signature_algorithms */
1328 0x00, 0x0c, /* 12 bytes */
1329 0x00, 0x0a, /* 10 bytes */
1330 0x05, 0x01, /* SHA-384/RSA */
1331 0x04, 0x01, /* SHA-256/RSA */
1332 0x02, 0x01, /* SHA-1/RSA */
1333 0x04, 0x03, /* SHA-256/ECDSA */
1334 0x02, 0x03, /* SHA-1/ECDSA */
1336 /* Length of the common prefix (first two extensions). */
1337 static const size_t kSafariCommonExtensionsLength = 18;
1342 tmppkt = hello->extensions;
1344 if (!PACKET_forward(&tmppkt, 2)
1345 || !PACKET_get_net_2(&tmppkt, &type)
1346 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1350 if (type != TLSEXT_TYPE_server_name)
1353 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1354 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1356 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1359 #endif /* !OPENSSL_NO_EC */
1361 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1363 /* |cookie| will only be initialized for DTLS. */
1364 PACKET session_id, compression, extensions, cookie;
1365 static const unsigned char null_compression = 0;
1366 CLIENTHELLO_MSG *clienthello = NULL;
1368 /* Check if this is actually an unexpected renegotiation ClientHello */
1369 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1370 if (!ossl_assert(!SSL_IS_TLS13(s))) {
1371 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1372 ERR_R_INTERNAL_ERROR);
1375 if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0
1376 || (!s->s3->send_connection_binding
1378 & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
1379 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1380 return MSG_PROCESS_FINISHED_READING;
1386 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1387 if (clienthello == NULL) {
1388 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1389 ERR_R_INTERNAL_ERROR);
1394 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1396 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1397 PACKET_null_init(&cookie);
1399 if (clienthello->isv2) {
1402 if (!SSL_IS_FIRST_HANDSHAKE(s)
1403 || s->hello_retry_request != SSL_HRR_NONE) {
1404 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1405 SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1410 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1411 * header is sent directly on the wire, not wrapped as a TLS
1412 * record. Our record layer just processes the message length and passes
1413 * the rest right through. Its format is:
1415 * 0-1 msg_length - decoded by the record layer
1416 * 2 msg_type - s->init_msg points here
1418 * 5-6 cipher_spec_length
1419 * 7-8 session_id_length
1420 * 9-10 challenge_length
1424 if (!PACKET_get_1(pkt, &mt)
1425 || mt != SSL2_MT_CLIENT_HELLO) {
1427 * Should never happen. We should have tested this in the record
1428 * layer in order to have determined that this is a SSLv2 record
1429 * in the first place
1431 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1432 ERR_R_INTERNAL_ERROR);
1437 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1438 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1439 SSL_R_LENGTH_TOO_SHORT);
1443 /* Parse the message and load client random. */
1444 if (clienthello->isv2) {
1446 * Handle an SSLv2 backwards compatible ClientHello
1447 * Note, this is only for SSLv3+ using the backward compatible format.
1448 * Real SSLv2 is not supported, and is rejected below.
1450 unsigned int ciphersuite_len, session_id_len, challenge_len;
1453 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1454 || !PACKET_get_net_2(pkt, &session_id_len)
1455 || !PACKET_get_net_2(pkt, &challenge_len)) {
1456 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1457 SSL_R_RECORD_LENGTH_MISMATCH);
1461 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1462 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1463 SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1467 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1469 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1470 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1471 /* No extensions. */
1472 || PACKET_remaining(pkt) != 0) {
1473 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1474 SSL_R_RECORD_LENGTH_MISMATCH);
1477 clienthello->session_id_len = session_id_len;
1479 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1480 * here rather than sizeof(clienthello->random) because that is the limit
1481 * for SSLv3 and it is fixed. It won't change even if
1482 * sizeof(clienthello->random) does.
1484 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1485 ? SSL3_RANDOM_SIZE : challenge_len;
1486 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1487 if (!PACKET_copy_bytes(&challenge,
1488 clienthello->random + SSL3_RANDOM_SIZE -
1489 challenge_len, challenge_len)
1490 /* Advertise only null compression. */
1491 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1492 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1493 ERR_R_INTERNAL_ERROR);
1497 PACKET_null_init(&clienthello->extensions);
1499 /* Regular ClientHello. */
1500 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1501 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1502 || !PACKET_copy_all(&session_id, clienthello->session_id,
1503 SSL_MAX_SSL_SESSION_ID_LENGTH,
1504 &clienthello->session_id_len)) {
1505 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1506 SSL_R_LENGTH_MISMATCH);
1510 if (SSL_IS_DTLS(s)) {
1511 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1512 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1513 SSL_R_LENGTH_MISMATCH);
1516 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1517 DTLS1_COOKIE_LENGTH,
1518 &clienthello->dtls_cookie_len)) {
1519 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1520 SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1524 * If we require cookies and this ClientHello doesn't contain one,
1525 * just return since we do not want to allocate any memory yet.
1526 * So check cookie length...
1528 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1529 if (clienthello->dtls_cookie_len == 0) {
1530 OPENSSL_free(clienthello);
1531 return MSG_PROCESS_FINISHED_READING;
1536 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1537 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1538 SSL_R_LENGTH_MISMATCH);
1542 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1543 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1544 SSL_R_LENGTH_MISMATCH);
1548 /* Could be empty. */
1549 if (PACKET_remaining(pkt) == 0) {
1550 PACKET_null_init(&clienthello->extensions);
1552 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1553 || PACKET_remaining(pkt) != 0) {
1554 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1555 SSL_R_LENGTH_MISMATCH);
1561 if (!PACKET_copy_all(&compression, clienthello->compressions,
1562 MAX_COMPRESSIONS_SIZE,
1563 &clienthello->compressions_len)) {
1564 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1565 ERR_R_INTERNAL_ERROR);
1569 /* Preserve the raw extensions PACKET for later use */
1570 extensions = clienthello->extensions;
1571 if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1572 &clienthello->pre_proc_exts,
1573 &clienthello->pre_proc_exts_len, 1)) {
1574 /* SSLfatal already been called */
1577 s->clienthello = clienthello;
1579 return MSG_PROCESS_CONTINUE_PROCESSING;
1582 if (clienthello != NULL)
1583 OPENSSL_free(clienthello->pre_proc_exts);
1584 OPENSSL_free(clienthello);
1586 return MSG_PROCESS_ERROR;
1589 static int tls_early_post_process_client_hello(SSL *s)
1592 int i, al = SSL_AD_INTERNAL_ERROR;
1596 #ifndef OPENSSL_NO_COMP
1597 SSL_COMP *comp = NULL;
1599 const SSL_CIPHER *c;
1600 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1601 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1602 CLIENTHELLO_MSG *clienthello = s->clienthello;
1603 DOWNGRADE dgrd = DOWNGRADE_NONE;
1605 /* Finished parsing the ClientHello, now we can start processing it */
1606 /* Give the ClientHello callback a crack at things */
1607 if (s->ctx->client_hello_cb != NULL) {
1608 /* A failure in the ClientHello callback terminates the connection. */
1609 switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
1610 case SSL_CLIENT_HELLO_SUCCESS:
1612 case SSL_CLIENT_HELLO_RETRY:
1613 s->rwstate = SSL_CLIENT_HELLO_CB;
1615 case SSL_CLIENT_HELLO_ERROR:
1618 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1619 SSL_R_CALLBACK_FAILED);
1624 /* Set up the client_random */
1625 memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1627 /* Choose the version */
1629 if (clienthello->isv2) {
1630 if (clienthello->legacy_version == SSL2_VERSION
1631 || (clienthello->legacy_version & 0xff00)
1632 != (SSL3_VERSION_MAJOR << 8)) {
1634 * This is real SSLv2 or something completely unknown. We don't
1637 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1638 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1639 SSL_R_UNKNOWN_PROTOCOL);
1643 s->client_version = clienthello->legacy_version;
1646 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1647 * versions are potentially compatible. Version negotiation comes later.
1649 if (!SSL_IS_DTLS(s)) {
1650 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1651 } else if (s->method->version != DTLS_ANY_VERSION &&
1652 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1653 protverr = SSL_R_VERSION_TOO_LOW;
1659 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1660 /* like ssl3_get_record, send alert using remote version number */
1661 s->version = s->client_version = clienthello->legacy_version;
1663 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1664 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1668 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1669 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1670 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1671 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1672 SSL_R_NOT_ON_RECORD_BOUNDARY);
1676 if (SSL_IS_DTLS(s)) {
1677 /* Empty cookie was already handled above by returning early. */
1678 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1679 if (s->ctx->app_verify_cookie_cb != NULL) {
1680 if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1681 clienthello->dtls_cookie_len) == 0) {
1682 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1683 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1684 SSL_R_COOKIE_MISMATCH);
1686 /* else cookie verification succeeded */
1688 /* default verification */
1689 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1690 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1691 s->d1->cookie_len) != 0) {
1692 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1693 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1694 SSL_R_COOKIE_MISMATCH);
1697 s->d1->cookie_verified = 1;
1699 if (s->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,
1704 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1712 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1713 clienthello->isv2) ||
1714 !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1715 clienthello->isv2, 1)) {
1716 /* SSLfatal() already called */
1720 s->s3->send_connection_binding = 0;
1721 /* Check what signalling cipher-suite values were received. */
1722 if (scsvs != NULL) {
1723 for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1724 c = sk_SSL_CIPHER_value(scsvs, i);
1725 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1726 if (s->renegotiate) {
1727 /* SCSV is fatal if renegotiating */
1728 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1729 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1730 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1733 s->s3->send_connection_binding = 1;
1734 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1735 !ssl_check_version_downgrade(s)) {
1737 * This SCSV indicates that the client previously tried
1738 * a higher version. We should fail if the current version
1739 * is an unexpected downgrade, as that indicates that the first
1740 * connection may have been tampered with in order to trigger
1741 * an insecure downgrade.
1743 SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
1744 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1745 SSL_R_INAPPROPRIATE_FALLBACK);
1751 /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1752 if (SSL_IS_TLS13(s)) {
1753 const SSL_CIPHER *cipher =
1754 ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
1756 if (cipher == NULL) {
1757 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1758 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1759 SSL_R_NO_SHARED_CIPHER);
1762 if (s->hello_retry_request == SSL_HRR_PENDING
1763 && (s->s3->tmp.new_cipher == NULL
1764 || s->s3->tmp.new_cipher->id != cipher->id)) {
1766 * A previous HRR picked a different ciphersuite to the one we
1767 * just selected. Something must have changed.
1769 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1770 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1774 s->s3->tmp.new_cipher = cipher;
1777 /* We need to do this before getting the session */
1778 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1779 SSL_EXT_CLIENT_HELLO,
1780 clienthello->pre_proc_exts, NULL, 0)) {
1781 /* SSLfatal() already called */
1786 * We don't allow resumption in a backwards compatible ClientHello.
1787 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1789 * Versions before 0.9.7 always allow clients to resume sessions in
1790 * renegotiation. 0.9.7 and later allow this by default, but optionally
1791 * ignore resumption requests with flag
1792 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1793 * than a change to default behavior so that applications relying on
1794 * this for security won't even compile against older library versions).
1795 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1796 * request renegotiation but not a new session (s->new_session remains
1797 * unset): for servers, this essentially just means that the
1798 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1801 if (clienthello->isv2 ||
1803 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1804 if (!ssl_get_new_session(s, 1)) {
1805 /* SSLfatal() already called */
1809 i = ssl_get_prev_session(s, clienthello);
1811 /* previous session */
1813 } else if (i == -1) {
1814 /* SSLfatal() already called */
1818 if (!ssl_get_new_session(s, 1)) {
1819 /* SSLfatal() already called */
1825 if (SSL_IS_TLS13(s)) {
1826 memcpy(s->tmp_session_id, s->clienthello->session_id,
1827 s->clienthello->session_id_len);
1828 s->tmp_session_id_len = s->clienthello->session_id_len;
1832 * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1833 * ciphersuite compatibility with the session as part of resumption.
1835 if (!SSL_IS_TLS13(s) && s->hit) {
1837 id = s->session->cipher->id;
1839 OSSL_TRACE_BEGIN(TLS_CIPHER) {
1840 BIO_printf(trc_out, "client sent %d ciphers\n",
1841 sk_SSL_CIPHER_num(ciphers));
1843 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1844 c = sk_SSL_CIPHER_value(ciphers, i);
1845 if (trc_out != NULL)
1846 BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
1847 sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1855 * we need to have the cipher in the cipher list if we are asked
1858 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1859 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1860 SSL_R_REQUIRED_CIPHER_MISSING);
1861 OSSL_TRACE_CANCEL(TLS_CIPHER);
1864 OSSL_TRACE_END(TLS_CIPHER);
1867 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1868 if (clienthello->compressions[loop] == 0)
1872 if (loop >= clienthello->compressions_len) {
1874 SSLfatal(s, SSL_AD_DECODE_ERROR,
1875 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1876 SSL_R_NO_COMPRESSION_SPECIFIED);
1880 #ifndef OPENSSL_NO_EC
1881 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1882 ssl_check_for_safari(s, clienthello);
1883 #endif /* !OPENSSL_NO_EC */
1885 /* TLS extensions */
1886 if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1887 clienthello->pre_proc_exts, NULL, 0, 1)) {
1888 /* SSLfatal() already called */
1893 * Check if we want to use external pre-shared secret for this handshake
1894 * for not reused session only. We need to generate server_random before
1895 * calling tls_session_secret_cb in order to allow SessionTicket
1896 * processing to use it in key derivation.
1900 pos = s->s3->server_random;
1901 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1902 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1903 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1904 ERR_R_INTERNAL_ERROR);
1910 && s->version >= TLS1_VERSION
1913 && s->ext.session_secret_cb) {
1914 const SSL_CIPHER *pref_cipher = NULL;
1916 * s->session->master_key_length is a size_t, but this is an int for
1917 * backwards compat reasons
1919 int master_key_length;
1921 master_key_length = sizeof(s->session->master_key);
1922 if (s->ext.session_secret_cb(s, s->session->master_key,
1923 &master_key_length, ciphers,
1925 s->ext.session_secret_cb_arg)
1926 && master_key_length > 0) {
1927 s->session->master_key_length = master_key_length;
1929 s->session->ciphers = ciphers;
1930 s->session->verify_result = X509_V_OK;
1934 /* check if some cipher was preferred by call back */
1935 if (pref_cipher == NULL)
1936 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1937 SSL_get_ciphers(s));
1938 if (pref_cipher == NULL) {
1939 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1940 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1941 SSL_R_NO_SHARED_CIPHER);
1945 s->session->cipher = pref_cipher;
1946 sk_SSL_CIPHER_free(s->cipher_list);
1947 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1948 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1949 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1954 * Worst case, we will use the NULL compression, but if we have other
1955 * options, we will now look for them. We have complen-1 compression
1956 * algorithms from the client, starting at q.
1958 s->s3->tmp.new_compression = NULL;
1959 if (SSL_IS_TLS13(s)) {
1961 * We already checked above that the NULL compression method appears in
1962 * the list. Now we check there aren't any others (which is illegal in
1963 * a TLSv1.3 ClientHello.
1965 if (clienthello->compressions_len != 1) {
1966 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1967 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1968 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1972 #ifndef OPENSSL_NO_COMP
1973 /* This only happens if we have a cache hit */
1974 else if (s->session->compress_meth != 0) {
1975 int m, comp_id = s->session->compress_meth;
1977 /* Perform sanity checks on resumed compression algorithm */
1978 /* Can't disable compression */
1979 if (!ssl_allow_compression(s)) {
1980 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1981 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1982 SSL_R_INCONSISTENT_COMPRESSION);
1985 /* Look for resumed compression method */
1986 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1987 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1988 if (comp_id == comp->id) {
1989 s->s3->tmp.new_compression = comp;
1993 if (s->s3->tmp.new_compression == NULL) {
1994 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1995 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1996 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1999 /* Look for resumed method in compression list */
2000 for (k = 0; k < clienthello->compressions_len; k++) {
2001 if (clienthello->compressions[k] == comp_id)
2004 if (k >= clienthello->compressions_len) {
2005 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2006 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
2007 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
2010 } else if (s->hit) {
2012 } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
2013 /* See if we have a match */
2014 int m, nn, v, done = 0;
2017 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
2018 for (m = 0; m < nn; m++) {
2019 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
2021 for (o = 0; o < clienthello->compressions_len; o++) {
2022 if (v == clienthello->compressions[o]) {
2031 s->s3->tmp.new_compression = comp;
2037 * If compression is disabled we'd better not try to resume a session
2038 * using compression.
2040 if (s->session->compress_meth != 0) {
2041 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2042 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
2043 SSL_R_INCONSISTENT_COMPRESSION);
2049 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
2052 if (!s->hit || SSL_IS_TLS13(s)) {
2053 sk_SSL_CIPHER_free(s->session->ciphers);
2054 s->session->ciphers = ciphers;
2055 if (ciphers == NULL) {
2056 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2057 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
2058 ERR_R_INTERNAL_ERROR);
2065 #ifdef OPENSSL_NO_COMP
2066 s->session->compress_meth = 0;
2068 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
2072 sk_SSL_CIPHER_free(ciphers);
2073 sk_SSL_CIPHER_free(scsvs);
2074 OPENSSL_free(clienthello->pre_proc_exts);
2075 OPENSSL_free(s->clienthello);
2076 s->clienthello = NULL;
2079 sk_SSL_CIPHER_free(ciphers);
2080 sk_SSL_CIPHER_free(scsvs);
2081 OPENSSL_free(clienthello->pre_proc_exts);
2082 OPENSSL_free(s->clienthello);
2083 s->clienthello = NULL;
2089 * Call the status request callback if needed. Upon success, returns 1.
2090 * Upon failure, returns 0.
2092 static int tls_handle_status_request(SSL *s)
2094 s->ext.status_expected = 0;
2097 * If status request then ask callback what to do. Note: this must be
2098 * called after servername callbacks in case the certificate has changed,
2099 * and must be called after the cipher has been chosen because this may
2100 * influence which certificate is sent
2102 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
2103 && s->ctx->ext.status_cb != NULL) {
2106 /* If no certificate can't return certificate status */
2107 if (s->s3->tmp.cert != NULL) {
2109 * Set current certificate to one we will use so SSL_get_certificate
2110 * et al can pick it up.
2112 s->cert->key = s->s3->tmp.cert;
2113 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
2115 /* We don't want to send a status request response */
2116 case SSL_TLSEXT_ERR_NOACK:
2117 s->ext.status_expected = 0;
2119 /* status request response should be sent */
2120 case SSL_TLSEXT_ERR_OK:
2121 if (s->ext.ocsp.resp)
2122 s->ext.status_expected = 1;
2124 /* something bad happened */
2125 case SSL_TLSEXT_ERR_ALERT_FATAL:
2127 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2128 SSL_F_TLS_HANDLE_STATUS_REQUEST,
2129 SSL_R_CLIENTHELLO_TLSEXT);
2139 * Call the alpn_select callback if needed. Upon success, returns 1.
2140 * Upon failure, returns 0.
2142 int tls_handle_alpn(SSL *s)
2144 const unsigned char *selected = NULL;
2145 unsigned char selected_len = 0;
2147 if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
2148 int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
2149 s->s3->alpn_proposed,
2150 (unsigned int)s->s3->alpn_proposed_len,
2151 s->ctx->ext.alpn_select_cb_arg);
2153 if (r == SSL_TLSEXT_ERR_OK) {
2154 OPENSSL_free(s->s3->alpn_selected);
2155 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
2156 if (s->s3->alpn_selected == NULL) {
2157 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_HANDLE_ALPN,
2158 ERR_R_INTERNAL_ERROR);
2161 s->s3->alpn_selected_len = selected_len;
2162 #ifndef OPENSSL_NO_NEXTPROTONEG
2163 /* ALPN takes precedence over NPN. */
2164 s->s3->npn_seen = 0;
2167 /* Check ALPN is consistent with session */
2168 if (s->session->ext.alpn_selected == NULL
2169 || selected_len != s->session->ext.alpn_selected_len
2170 || memcmp(selected, s->session->ext.alpn_selected,
2171 selected_len) != 0) {
2172 /* Not consistent so can't be used for early_data */
2173 s->ext.early_data_ok = 0;
2177 * This is a new session and so alpn_selected should have
2178 * been initialised to NULL. We should update it with the
2181 if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2182 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2183 SSL_F_TLS_HANDLE_ALPN,
2184 ERR_R_INTERNAL_ERROR);
2187 s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2189 if (s->session->ext.alpn_selected == NULL) {
2190 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2191 SSL_F_TLS_HANDLE_ALPN,
2192 ERR_R_INTERNAL_ERROR);
2195 s->session->ext.alpn_selected_len = selected_len;
2200 } else if (r != SSL_TLSEXT_ERR_NOACK) {
2201 SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, SSL_F_TLS_HANDLE_ALPN,
2202 SSL_R_NO_APPLICATION_PROTOCOL);
2206 * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2211 /* Check ALPN is consistent with session */
2212 if (s->session->ext.alpn_selected != NULL) {
2213 /* Not consistent so can't be used for early_data */
2214 s->ext.early_data_ok = 0;
2220 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
2222 const SSL_CIPHER *cipher;
2224 if (wst == WORK_MORE_A) {
2225 int rv = tls_early_post_process_client_hello(s);
2227 /* SSLfatal() was already called */
2234 if (wst == WORK_MORE_B) {
2235 if (!s->hit || SSL_IS_TLS13(s)) {
2236 /* Let cert callback update server certificates if required */
2238 if (s->cert->cert_cb != NULL) {
2239 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2241 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2242 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2243 SSL_R_CERT_CB_ERROR);
2247 s->rwstate = SSL_X509_LOOKUP;
2250 s->rwstate = SSL_NOTHING;
2252 if (!tls1_set_server_sigalgs(s)) {
2253 /* SSLfatal already called */
2258 /* In TLSv1.3 we selected the ciphersuite before resumption */
2259 if (!SSL_IS_TLS13(s)) {
2261 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
2263 if (cipher == NULL) {
2264 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2265 SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2266 SSL_R_NO_SHARED_CIPHER);
2269 s->s3->tmp.new_cipher = cipher;
2272 if (!tls_choose_sigalg(s, 1)) {
2273 /* SSLfatal already called */
2276 /* check whether we should disable session resumption */
2277 if (s->not_resumable_session_cb != NULL)
2278 s->session->not_resumable =
2279 s->not_resumable_session_cb(s,
2280 ((s->s3->tmp.new_cipher->algorithm_mkey
2281 & (SSL_kDHE | SSL_kECDHE)) != 0));
2282 if (s->session->not_resumable)
2283 /* do not send a session ticket */
2284 s->ext.ticket_expected = 0;
2287 /* Session-id reuse */
2288 s->s3->tmp.new_cipher = s->session->cipher;
2292 * we now have the following setup.
2294 * cipher_list - our preferred list of ciphers
2295 * ciphers - the clients preferred list of ciphers
2296 * compression - basically ignored right now
2297 * ssl version is set - sslv3
2298 * s->session - The ssl session has been setup.
2299 * s->hit - session reuse flag
2300 * s->s3->tmp.new_cipher- the new cipher to use.
2304 * Call status_request callback if needed. Has to be done after the
2305 * certificate callbacks etc above.
2307 if (!tls_handle_status_request(s)) {
2308 /* SSLfatal() already called */
2312 * Call alpn_select callback if needed. Has to be done after SNI and
2313 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2314 * we already did this because cipher negotiation happens earlier, and
2315 * we must handle ALPN before we decide whether to accept early_data.
2317 if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
2318 /* SSLfatal() already called */
2324 #ifndef OPENSSL_NO_SRP
2325 if (wst == WORK_MORE_C) {
2327 if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2329 * callback indicates further work to be done
2331 s->rwstate = SSL_X509_LOOKUP;
2335 /* SSLfatal() already called */
2341 return WORK_FINISHED_STOP;
2346 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2351 unsigned char *session_id;
2352 int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
2354 version = usetls13 ? TLS1_2_VERSION : s->version;
2355 if (!WPACKET_put_bytes_u16(pkt, version)
2357 * Random stuff. Filling of the server_random takes place in
2358 * tls_process_client_hello()
2360 || !WPACKET_memcpy(pkt,
2361 s->hello_retry_request == SSL_HRR_PENDING
2362 ? hrrrandom : s->s3->server_random,
2363 SSL3_RANDOM_SIZE)) {
2364 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2365 ERR_R_INTERNAL_ERROR);
2370 * There are several cases for the session ID to send
2371 * back in the server hello:
2372 * - For session reuse from the session cache,
2373 * we send back the old session ID.
2374 * - If stateless session reuse (using a session ticket)
2375 * is successful, we send back the client's "session ID"
2376 * (which doesn't actually identify the session).
2377 * - If it is a new session, we send back the new
2379 * - However, if we want the new session to be single-use,
2380 * we send back a 0-length session ID.
2381 * - In TLSv1.3 we echo back the session id sent to us by the client
2383 * s->hit is non-zero in either case of session reuse,
2384 * so the following won't overwrite an ID that we're supposed
2387 if (s->session->not_resumable ||
2388 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2390 s->session->session_id_length = 0;
2393 sl = s->tmp_session_id_len;
2394 session_id = s->tmp_session_id;
2396 sl = s->session->session_id_length;
2397 session_id = s->session->session_id;
2400 if (sl > sizeof(s->session->session_id)) {
2401 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2402 ERR_R_INTERNAL_ERROR);
2406 /* set up the compression method */
2407 #ifdef OPENSSL_NO_COMP
2410 if (usetls13 || s->s3->tmp.new_compression == NULL)
2413 compm = s->s3->tmp.new_compression->id;
2416 if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2417 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
2418 || !WPACKET_put_bytes_u8(pkt, compm)) {
2419 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2420 ERR_R_INTERNAL_ERROR);
2424 if (!tls_construct_extensions(s, pkt,
2425 s->hello_retry_request == SSL_HRR_PENDING
2426 ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2428 ? SSL_EXT_TLS1_3_SERVER_HELLO
2429 : SSL_EXT_TLS1_2_SERVER_HELLO),
2431 /* SSLfatal() already called */
2435 if (s->hello_retry_request == SSL_HRR_PENDING) {
2436 /* Ditch the session. We'll create a new one next time around */
2437 SSL_SESSION_free(s->session);
2442 * Re-initialise the Transcript Hash. We're going to prepopulate it with
2443 * a synthetic message_hash in place of ClientHello1.
2445 if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
2446 /* SSLfatal() already called */
2449 } else if (!(s->verify_mode & SSL_VERIFY_PEER)
2450 && !ssl3_digest_cached_records(s, 0)) {
2451 /* SSLfatal() already called */;
2458 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2460 if (!s->s3->tmp.cert_request) {
2461 if (!ssl3_digest_cached_records(s, 0)) {
2462 /* SSLfatal() already called */
2469 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2471 #ifndef OPENSSL_NO_DH
2472 EVP_PKEY *pkdh = NULL;
2474 #ifndef OPENSSL_NO_EC
2475 unsigned char *encodedPoint = NULL;
2476 size_t encodedlen = 0;
2479 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2483 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2484 EVP_PKEY_CTX *pctx = NULL;
2485 size_t paramlen, paramoffset;
2487 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
2488 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2489 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2493 if (md_ctx == NULL) {
2494 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2495 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2499 type = s->s3->tmp.new_cipher->algorithm_mkey;
2501 r[0] = r[1] = r[2] = r[3] = NULL;
2502 #ifndef OPENSSL_NO_PSK
2503 /* Plain PSK or RSAPSK nothing to do */
2504 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2506 #endif /* !OPENSSL_NO_PSK */
2507 #ifndef OPENSSL_NO_DH
2508 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2509 CERT *cert = s->cert;
2511 EVP_PKEY *pkdhp = NULL;
2514 if (s->cert->dh_tmp_auto) {
2515 DH *dhp = ssl_get_auto_dh(s);
2516 pkdh = EVP_PKEY_new();
2517 if (pkdh == NULL || dhp == NULL) {
2519 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2520 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2521 ERR_R_INTERNAL_ERROR);
2524 EVP_PKEY_assign_DH(pkdh, dhp);
2527 pkdhp = cert->dh_tmp;
2529 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2530 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2531 pkdh = ssl_dh_to_pkey(dhp);
2533 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2534 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2535 ERR_R_INTERNAL_ERROR);
2540 if (pkdhp == NULL) {
2541 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2542 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2543 SSL_R_MISSING_TMP_DH_KEY);
2546 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2547 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2548 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2549 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2550 SSL_R_DH_KEY_TOO_SMALL);
2553 if (s->s3->tmp.pkey != NULL) {
2554 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2555 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2556 ERR_R_INTERNAL_ERROR);
2560 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2561 if (s->s3->tmp.pkey == NULL) {
2562 /* SSLfatal() already called */
2566 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2568 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2569 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2570 ERR_R_INTERNAL_ERROR);
2574 EVP_PKEY_free(pkdh);
2577 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2578 DH_get0_key(dh, &r[2], NULL);
2581 #ifndef OPENSSL_NO_EC
2582 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2584 if (s->s3->tmp.pkey != NULL) {
2585 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2586 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2587 ERR_R_INTERNAL_ERROR);
2591 /* Get NID of appropriate shared curve */
2592 curve_id = tls1_shared_group(s, -2);
2593 if (curve_id == 0) {
2594 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2595 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2596 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2599 s->s3->tmp.pkey = ssl_generate_pkey_group(s, curve_id);
2600 /* Generate a new key for this curve */
2601 if (s->s3->tmp.pkey == NULL) {
2602 /* SSLfatal() already called */
2606 /* Encode the public key. */
2607 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2609 if (encodedlen == 0) {
2610 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2611 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2616 * We'll generate the serverKeyExchange message explicitly so we
2617 * can set these to NULLs
2624 #endif /* !OPENSSL_NO_EC */
2625 #ifndef OPENSSL_NO_SRP
2626 if (type & SSL_kSRP) {
2627 if ((s->srp_ctx.N == NULL) ||
2628 (s->srp_ctx.g == NULL) ||
2629 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2630 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2631 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2632 SSL_R_MISSING_SRP_PARAM);
2635 r[0] = s->srp_ctx.N;
2636 r[1] = s->srp_ctx.g;
2637 r[2] = s->srp_ctx.s;
2638 r[3] = s->srp_ctx.B;
2642 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2643 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2644 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2648 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2649 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2651 } else if (lu == NULL) {
2652 SSLfatal(s, SSL_AD_DECODE_ERROR,
2653 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2657 #ifndef OPENSSL_NO_PSK
2658 if (type & SSL_PSK) {
2659 size_t len = (s->cert->psk_identity_hint == NULL)
2660 ? 0 : strlen(s->cert->psk_identity_hint);
2663 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2664 * checked this when we set the identity hint - but just in case
2666 if (len > PSK_MAX_IDENTITY_LEN
2667 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2669 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2670 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2671 ERR_R_INTERNAL_ERROR);
2677 for (i = 0; i < 4 && r[i] != NULL; i++) {
2678 unsigned char *binval;
2681 #ifndef OPENSSL_NO_SRP
2682 if ((i == 2) && (type & SSL_kSRP)) {
2683 res = WPACKET_start_sub_packet_u8(pkt);
2686 res = WPACKET_start_sub_packet_u16(pkt);
2689 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2690 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2691 ERR_R_INTERNAL_ERROR);
2695 #ifndef OPENSSL_NO_DH
2697 * for interoperability with some versions of the Microsoft TLS
2698 * stack, we need to zero pad the DHE pub key to the same length
2701 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2702 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2705 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2706 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2707 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2708 ERR_R_INTERNAL_ERROR);
2711 memset(binval, 0, len);
2715 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2716 || !WPACKET_close(pkt)) {
2717 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2718 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2719 ERR_R_INTERNAL_ERROR);
2723 BN_bn2bin(r[i], binval);
2726 #ifndef OPENSSL_NO_EC
2727 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2729 * We only support named (not generic) curves. In this situation, the
2730 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2731 * [1 byte length of encoded point], followed by the actual encoded
2734 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2735 || !WPACKET_put_bytes_u8(pkt, 0)
2736 || !WPACKET_put_bytes_u8(pkt, curve_id)
2737 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2738 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2739 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2740 ERR_R_INTERNAL_ERROR);
2743 OPENSSL_free(encodedPoint);
2744 encodedPoint = NULL;
2750 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2752 unsigned char *sigbytes1, *sigbytes2, *tbs;
2753 size_t siglen, tbslen;
2756 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
2757 /* Should never happen */
2758 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2759 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2760 ERR_R_INTERNAL_ERROR);
2763 /* Get length of the parameters we have written above */
2764 if (!WPACKET_get_length(pkt, ¶mlen)) {
2765 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2766 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2767 ERR_R_INTERNAL_ERROR);
2770 /* send signature algorithm */
2771 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
2772 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2773 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2774 ERR_R_INTERNAL_ERROR);
2778 * Create the signature. We don't know the actual length of the sig
2779 * until after we've created it, so we reserve enough bytes for it
2780 * up front, and then properly allocate them in the WPACKET
2783 siglen = EVP_PKEY_size(pkey);
2784 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2785 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2786 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2787 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2788 ERR_R_INTERNAL_ERROR);
2791 if (lu->sig == EVP_PKEY_RSA_PSS) {
2792 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2793 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2794 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2795 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2800 tbslen = construct_key_exchange_tbs(s, &tbs,
2801 s->init_buf->data + paramoffset,
2804 /* SSLfatal() already called */
2807 rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);
2809 if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2810 || sigbytes1 != sigbytes2) {
2811 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2812 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2813 ERR_R_INTERNAL_ERROR);
2818 EVP_MD_CTX_free(md_ctx);
2821 #ifndef OPENSSL_NO_DH
2822 EVP_PKEY_free(pkdh);
2824 #ifndef OPENSSL_NO_EC
2825 OPENSSL_free(encodedPoint);
2827 EVP_MD_CTX_free(md_ctx);
2831 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2833 if (SSL_IS_TLS13(s)) {
2834 /* Send random context when doing post-handshake auth */
2835 if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
2836 OPENSSL_free(s->pha_context);
2837 s->pha_context_len = 32;
2838 if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL
2839 || RAND_bytes(s->pha_context, s->pha_context_len) <= 0
2840 || !WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
2841 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2842 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2843 ERR_R_INTERNAL_ERROR);
2846 /* reset the handshake hash back to just after the ClientFinished */
2847 if (!tls13_restore_handshake_digest_for_pha(s)) {
2848 /* SSLfatal() already called */
2852 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2853 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2854 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2855 ERR_R_INTERNAL_ERROR);
2860 if (!tls_construct_extensions(s, pkt,
2861 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2863 /* SSLfatal() already called */
2869 /* get the list of acceptable cert types */
2870 if (!WPACKET_start_sub_packet_u8(pkt)
2871 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2872 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2873 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2877 if (SSL_USE_SIGALGS(s)) {
2878 const uint16_t *psigs;
2879 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2881 if (!WPACKET_start_sub_packet_u16(pkt)
2882 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2883 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2884 || !WPACKET_close(pkt)) {
2885 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2886 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2887 ERR_R_INTERNAL_ERROR);
2892 if (!construct_ca_names(s, get_ca_names(s), pkt)) {
2893 /* SSLfatal() already called */
2899 s->s3->tmp.cert_request = 1;
2903 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
2905 #ifndef OPENSSL_NO_PSK
2906 unsigned char psk[PSK_MAX_PSK_LEN];
2908 PACKET psk_identity;
2910 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2911 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2912 SSL_R_LENGTH_MISMATCH);
2915 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2916 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2917 SSL_R_DATA_LENGTH_TOO_LONG);
2920 if (s->psk_server_callback == NULL) {
2921 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2922 SSL_R_PSK_NO_SERVER_CB);
2926 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2927 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2928 ERR_R_INTERNAL_ERROR);
2932 psklen = s->psk_server_callback(s, s->session->psk_identity,
2935 if (psklen > PSK_MAX_PSK_LEN) {
2936 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2937 ERR_R_INTERNAL_ERROR);
2939 } else if (psklen == 0) {
2941 * PSK related to the given identity not found
2943 SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
2944 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2945 SSL_R_PSK_IDENTITY_NOT_FOUND);
2949 OPENSSL_free(s->s3->tmp.psk);
2950 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2951 OPENSSL_cleanse(psk, psklen);
2953 if (s->s3->tmp.psk == NULL) {
2954 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2955 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2959 s->s3->tmp.psklen = psklen;
2963 /* Should never happen */
2964 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2965 ERR_R_INTERNAL_ERROR);
2970 static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
2972 #ifndef OPENSSL_NO_RSA
2973 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2975 unsigned char decrypt_good, version_good;
2976 size_t j, padding_len;
2977 PACKET enc_premaster;
2979 unsigned char *rsa_decrypt = NULL;
2982 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2984 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2985 SSL_R_MISSING_RSA_CERTIFICATE);
2989 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2990 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2991 enc_premaster = *pkt;
2993 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2994 || PACKET_remaining(pkt) != 0) {
2995 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2996 SSL_R_LENGTH_MISMATCH);
3002 * We want to be sure that the plaintext buffer size makes it safe to
3003 * iterate over the entire size of a premaster secret
3004 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
3005 * their ciphertext cannot accommodate a premaster secret anyway.
3007 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
3008 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3009 RSA_R_KEY_SIZE_TOO_SMALL);
3013 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
3014 if (rsa_decrypt == NULL) {
3015 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3016 ERR_R_MALLOC_FAILURE);
3021 * We must not leak whether a decryption failure occurs because of
3022 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
3023 * section 7.4.7.1). The code follows that advice of the TLS RFC and
3024 * generates a random premaster secret for the case that the decrypt
3025 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
3028 if (RAND_priv_bytes(rand_premaster_secret,
3029 sizeof(rand_premaster_secret)) <= 0) {
3030 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3031 ERR_R_INTERNAL_ERROR);
3036 * Decrypt with no padding. PKCS#1 padding will be removed as part of
3037 * the timing-sensitive code below.
3039 /* TODO(size_t): Convert this function */
3040 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
3041 PACKET_data(&enc_premaster),
3042 rsa_decrypt, rsa, RSA_NO_PADDING);
3043 if (decrypt_len < 0) {
3044 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3045 ERR_R_INTERNAL_ERROR);
3049 /* Check the padding. See RFC 3447, section 7.2.2. */
3052 * The smallest padded premaster is 11 bytes of overhead. Small keys
3053 * are publicly invalid, so this may return immediately. This ensures
3054 * PS is at least 8 bytes.
3056 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
3057 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3058 SSL_R_DECRYPTION_FAILED);
3062 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
3063 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
3064 constant_time_eq_int_8(rsa_decrypt[1], 2);
3065 for (j = 2; j < padding_len - 1; j++) {
3066 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
3068 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
3071 * If the version in the decrypted pre-master secret is correct then
3072 * version_good will be 0xff, otherwise it'll be zero. The
3073 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
3074 * (http://eprint.iacr.org/2003/052/) exploits the version number
3075 * check as a "bad version oracle". Thus version checks are done in
3076 * constant time and are treated like any other decryption error.
3079 constant_time_eq_8(rsa_decrypt[padding_len],
3080 (unsigned)(s->client_version >> 8));
3082 constant_time_eq_8(rsa_decrypt[padding_len + 1],
3083 (unsigned)(s->client_version & 0xff));
3086 * The premaster secret must contain the same version number as the
3087 * ClientHello to detect version rollback attacks (strangely, the
3088 * protocol does not offer such protection for DH ciphersuites).
3089 * However, buggy clients exist that send the negotiated protocol
3090 * version instead if the server does not support the requested
3091 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
3094 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
3095 unsigned char workaround_good;
3096 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
3097 (unsigned)(s->version >> 8));
3099 constant_time_eq_8(rsa_decrypt[padding_len + 1],
3100 (unsigned)(s->version & 0xff));
3101 version_good |= workaround_good;
3105 * Both decryption and version must be good for decrypt_good to
3106 * remain non-zero (0xff).
3108 decrypt_good &= version_good;
3111 * Now copy rand_premaster_secret over from p using
3112 * decrypt_good_mask. If decryption failed, then p does not
3113 * contain valid plaintext, however, a check above guarantees
3114 * it is still sufficiently large to read from.
3116 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
3117 rsa_decrypt[padding_len + j] =
3118 constant_time_select_8(decrypt_good,
3119 rsa_decrypt[padding_len + j],
3120 rand_premaster_secret[j]);
3123 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
3124 sizeof(rand_premaster_secret), 0)) {
3125 /* SSLfatal() already called */
3131 OPENSSL_free(rsa_decrypt);
3134 /* Should never happen */
3135 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3136 ERR_R_INTERNAL_ERROR);
3141 static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
3143 #ifndef OPENSSL_NO_DH
3144 EVP_PKEY *skey = NULL;
3148 const unsigned char *data;
3149 EVP_PKEY *ckey = NULL;
3152 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
3153 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3154 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
3157 skey = s->s3->tmp.pkey;
3159 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3160 SSL_R_MISSING_TMP_DH_KEY);
3164 if (PACKET_remaining(pkt) == 0L) {
3165 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3166 SSL_R_MISSING_TMP_DH_KEY);
3169 if (!PACKET_get_bytes(pkt, &data, i)) {
3170 /* We already checked we have enough data */
3171 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3172 ERR_R_INTERNAL_ERROR);
3175 ckey = EVP_PKEY_new();
3176 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
3177 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3182 cdh = EVP_PKEY_get0_DH(ckey);
3183 pub_key = BN_bin2bn(data, i, NULL);
3184 if (pub_key == NULL || cdh == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
3185 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3186 ERR_R_INTERNAL_ERROR);
3191 if (ssl_derive(s, skey, ckey, 1) == 0) {
3192 /* SSLfatal() already called */
3197 EVP_PKEY_free(s->s3->tmp.pkey);
3198 s->s3->tmp.pkey = NULL;
3200 EVP_PKEY_free(ckey);
3203 /* Should never happen */
3204 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3205 ERR_R_INTERNAL_ERROR);
3210 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
3212 #ifndef OPENSSL_NO_EC
3213 EVP_PKEY *skey = s->s3->tmp.pkey;
3214 EVP_PKEY *ckey = NULL;
3217 if (PACKET_remaining(pkt) == 0L) {
3218 /* We don't support ECDH client auth */
3219 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_CKE_ECDHE,
3220 SSL_R_MISSING_TMP_ECDH_KEY);
3224 const unsigned char *data;
3227 * Get client's public key from encoded point in the
3228 * ClientKeyExchange message.
3231 /* Get encoded point length */
3232 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
3233 || PACKET_remaining(pkt) != 0) {
3234 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3235 SSL_R_LENGTH_MISMATCH);
3239 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3240 SSL_R_MISSING_TMP_ECDH_KEY);
3244 ckey = EVP_PKEY_new();
3245 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3246 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3250 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
3251 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3257 if (ssl_derive(s, skey, ckey, 1) == 0) {
3258 /* SSLfatal() already called */
3263 EVP_PKEY_free(s->s3->tmp.pkey);
3264 s->s3->tmp.pkey = NULL;
3266 EVP_PKEY_free(ckey);
3270 /* Should never happen */
3271 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3272 ERR_R_INTERNAL_ERROR);
3277 static int tls_process_cke_srp(SSL *s, PACKET *pkt)
3279 #ifndef OPENSSL_NO_SRP
3281 const unsigned char *data;
3283 if (!PACKET_get_net_2(pkt, &i)
3284 || !PACKET_get_bytes(pkt, &data, i)) {
3285 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3286 SSL_R_BAD_SRP_A_LENGTH);
3289 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3290 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3294 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3295 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CKE_SRP,
3296 SSL_R_BAD_SRP_PARAMETERS);
3299 OPENSSL_free(s->session->srp_username);
3300 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3301 if (s->session->srp_username == NULL) {
3302 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3303 ERR_R_MALLOC_FAILURE);
3307 if (!srp_generate_server_master_secret(s)) {
3308 /* SSLfatal() already called */
3314 /* Should never happen */
3315 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3316 ERR_R_INTERNAL_ERROR);
3321 static int tls_process_cke_gost(SSL *s, PACKET *pkt)
3323 #ifndef OPENSSL_NO_GOST
3324 EVP_PKEY_CTX *pkey_ctx;
3325 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3326 unsigned char premaster_secret[32];
3327 const unsigned char *start;
3328 size_t outlen = 32, inlen;
3329 unsigned long alg_a;
3330 unsigned int asn1id, asn1len;
3334 /* Get our certificate private key */
3335 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3336 if (alg_a & SSL_aGOST12) {
3338 * New GOST ciphersuites have SSL_aGOST01 bit too
3340 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3342 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3345 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3347 } else if (alg_a & SSL_aGOST01) {
3348 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3351 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
3352 if (pkey_ctx == NULL) {
3353 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3354 ERR_R_MALLOC_FAILURE);
3357 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3358 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3359 ERR_R_INTERNAL_ERROR);
3363 * If client certificate is present and is of the same type, maybe
3364 * use it for key exchange. Don't mind errors from
3365 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3366 * client certificate for authorization only.
3368 client_pub_pkey = X509_get0_pubkey(s->session->peer);
3369 if (client_pub_pkey) {
3370 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3373 /* Decrypt session key */
3374 if (!PACKET_get_1(pkt, &asn1id)
3375 || asn1id != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
3376 || !PACKET_peek_1(pkt, &asn1len)) {
3377 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3378 SSL_R_DECRYPTION_FAILED);
3381 if (asn1len == 0x81) {
3383 * Long form length. Should only be one byte of length. Anything else
3385 * We did a successful peek before so this shouldn't fail
3387 if (!PACKET_forward(pkt, 1)) {
3388 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3389 SSL_R_DECRYPTION_FAILED);
3392 } else if (asn1len >= 0x80) {
3394 * Indefinite length, or more than one long form length bytes. We don't
3397 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3398 SSL_R_DECRYPTION_FAILED);
3400 } /* else short form length */
3402 if (!PACKET_as_length_prefixed_1(pkt, &encdata)) {
3403 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3404 SSL_R_DECRYPTION_FAILED);
3407 inlen = PACKET_remaining(&encdata);
3408 start = PACKET_data(&encdata);
3410 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3412 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3413 SSL_R_DECRYPTION_FAILED);
3416 /* Generate master secret */
3417 if (!ssl_generate_master_secret(s, premaster_secret,
3418 sizeof(premaster_secret), 0)) {
3419 /* SSLfatal() already called */
3422 /* Check if pubkey from client certificate was used */
3423 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3425 s->statem.no_cert_verify = 1;
3429 EVP_PKEY_CTX_free(pkey_ctx);
3432 /* Should never happen */
3433 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3434 ERR_R_INTERNAL_ERROR);
3439 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3441 unsigned long alg_k;
3443 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3445 /* For PSK parse and retrieve identity, obtain PSK key */
3446 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3447 /* SSLfatal() already called */
3451 if (alg_k & SSL_kPSK) {
3452 /* Identity extracted earlier: should be nothing left */
3453 if (PACKET_remaining(pkt) != 0) {
3454 SSLfatal(s, SSL_AD_DECODE_ERROR,
3455 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3456 SSL_R_LENGTH_MISMATCH);
3459 /* PSK handled by ssl_generate_master_secret */
3460 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3461 /* SSLfatal() already called */
3464 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3465 if (!tls_process_cke_rsa(s, pkt)) {
3466 /* SSLfatal() already called */
3469 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3470 if (!tls_process_cke_dhe(s, pkt)) {
3471 /* SSLfatal() already called */
3474 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3475 if (!tls_process_cke_ecdhe(s, pkt)) {
3476 /* SSLfatal() already called */
3479 } else if (alg_k & SSL_kSRP) {
3480 if (!tls_process_cke_srp(s, pkt)) {
3481 /* SSLfatal() already called */
3484 } else if (alg_k & SSL_kGOST) {
3485 if (!tls_process_cke_gost(s, pkt)) {
3486 /* SSLfatal() already called */
3490 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3491 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3492 SSL_R_UNKNOWN_CIPHER_TYPE);
3496 return MSG_PROCESS_CONTINUE_PROCESSING;
3498 #ifndef OPENSSL_NO_PSK
3499 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3500 s->s3->tmp.psk = NULL;
3502 return MSG_PROCESS_ERROR;
3505 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3507 #ifndef OPENSSL_NO_SCTP
3508 if (wst == WORK_MORE_A) {
3509 if (SSL_IS_DTLS(s)) {
3510 unsigned char sctpauthkey[64];
3511 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3514 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3517 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3518 sizeof(DTLS1_SCTP_AUTH_LABEL));
3520 /* Don't include the terminating zero. */
3521 labellen = sizeof(labelbuffer) - 1;
3522 if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3525 if (SSL_export_keying_material(s, sctpauthkey,
3526 sizeof(sctpauthkey), labelbuffer,
3529 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3530 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3531 ERR_R_INTERNAL_ERROR);
3535 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3536 sizeof(sctpauthkey), sctpauthkey);
3541 if (s->statem.no_cert_verify || !s->session->peer) {
3543 * No certificate verify or no peer certificate so we no longer need
3544 * the handshake_buffer
3546 if (!ssl3_digest_cached_records(s, 0)) {
3547 /* SSLfatal() already called */
3550 return WORK_FINISHED_CONTINUE;
3552 if (!s->s3->handshake_buffer) {
3553 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3554 SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3555 ERR_R_INTERNAL_ERROR);
3559 * For sigalgs freeze the handshake buffer. If we support
3560 * extms we've done this already so this is a no-op
3562 if (!ssl3_digest_cached_records(s, 1)) {
3563 /* SSLfatal() already called */
3568 return WORK_FINISHED_CONTINUE;
3571 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3574 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
3577 const unsigned char *certstart, *certbytes;
3578 STACK_OF(X509) *sk = NULL;
3579 PACKET spkt, context;
3581 SSL_SESSION *new_sess = NULL;
3584 * To get this far we must have read encrypted data from the client. We no
3585 * longer tolerate unencrypted alerts. This value is ignored if less than
3588 s->statem.enc_read_state = ENC_READ_STATE_VALID;
3590 if ((sk = sk_X509_new_null()) == NULL) {
3591 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3592 ERR_R_MALLOC_FAILURE);
3596 if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context)
3597 || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
3598 || (s->pha_context != NULL &&
3599 !PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
3600 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3601 SSL_R_INVALID_CONTEXT);
3605 if (!PACKET_get_length_prefixed_3(pkt, &spkt)
3606 || PACKET_remaining(pkt) != 0) {
3607 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3608 SSL_R_LENGTH_MISMATCH);
3612 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3613 if (!PACKET_get_net_3(&spkt, &l)
3614 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3615 SSLfatal(s, SSL_AD_DECODE_ERROR,
3616 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3617 SSL_R_CERT_LENGTH_MISMATCH);
3621 certstart = certbytes;
3622 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3624 SSLfatal(s, SSL_AD_DECODE_ERROR,
3625 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3628 if (certbytes != (certstart + l)) {
3629 SSLfatal(s, SSL_AD_DECODE_ERROR,
3630 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3631 SSL_R_CERT_LENGTH_MISMATCH);
3635 if (SSL_IS_TLS13(s)) {
3636 RAW_EXTENSION *rawexts = NULL;
3639 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3640 SSLfatal(s, SSL_AD_DECODE_ERROR,
3641 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3645 if (!tls_collect_extensions(s, &extensions,
3646 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3647 NULL, chainidx == 0)
3648 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3649 rawexts, x, chainidx,
3650 PACKET_remaining(&spkt) == 0)) {
3651 OPENSSL_free(rawexts);
3654 OPENSSL_free(rawexts);
3657 if (!sk_X509_push(sk, x)) {
3658 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3659 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3660 ERR_R_MALLOC_FAILURE);
3666 if (sk_X509_num(sk) <= 0) {
3667 /* TLS does not mind 0 certs returned */
3668 if (s->version == SSL3_VERSION) {
3669 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3670 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3671 SSL_R_NO_CERTIFICATES_RETURNED);
3674 /* Fail for TLS only if we required a certificate */
3675 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3676 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3677 SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
3678 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3679 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3682 /* No client certificate so digest cached records */
3683 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3684 /* SSLfatal() already called */
3689 i = ssl_verify_cert_chain(s, sk);
3691 SSLfatal(s, ssl_x509err2alert(s->verify_result),
3692 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3693 SSL_R_CERTIFICATE_VERIFY_FAILED);
3697 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3698 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3701 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3703 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3704 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3705 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3711 * Sessions must be immutable once they go into the session cache. Otherwise
3712 * we can get multi-thread problems. Therefore we don't "update" sessions,
3713 * we replace them with a duplicate. Here, we need to do this every time
3714 * a new certificate is received via post-handshake authentication, as the
3715 * session may have already gone into the session cache.
3718 if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3719 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
3720 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3721 SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3722 ERR_R_MALLOC_FAILURE);
3726 SSL_SESSION_free(s->session);
3727 s->session = new_sess;
3730 X509_free(s->session->peer);
3731 s->session->peer = sk_X509_shift(sk);
3732 s->session->verify_result = s->verify_result;
3734 sk_X509_pop_free(s->session->peer_chain, X509_free);
3735 s->session->peer_chain = sk;
3738 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3741 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3742 /* SSLfatal() already called */
3747 * Inconsistency alert: cert_chain does *not* include the peer's own
3748 * certificate, while we do include it in statem_clnt.c
3752 /* Save the current hash state for when we receive the CertificateVerify */
3753 if (SSL_IS_TLS13(s)) {
3754 if (!ssl_handshake_hash(s, s->cert_verify_hash,
3755 sizeof(s->cert_verify_hash),
3756 &s->cert_verify_hash_len)) {
3757 /* SSLfatal() already called */
3761 /* Resend session tickets */
3762 s->sent_tickets = 0;
3765 ret = MSG_PROCESS_CONTINUE_READING;
3769 sk_X509_pop_free(sk, X509_free);
3773 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3775 CERT_PKEY *cpk = s->s3->tmp.cert;
3778 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3779 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3784 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3785 * for the server Certificate message
3787 if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
3788 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3789 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3792 if (!ssl3_output_cert_chain(s, pkt, cpk)) {
3793 /* SSLfatal() already called */
3800 static int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
3801 unsigned char *tick_nonce)
3804 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3805 * unspecified for resumed session (for simplicity).
3806 * In TLSv1.3 we reset the "time" field above, and always specify the
3809 if (!WPACKET_put_bytes_u32(pkt,
3810 (s->hit && !SSL_IS_TLS13(s))
3811 ? 0 : s->session->timeout)) {
3812 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
3813 ERR_R_INTERNAL_ERROR);
3817 if (SSL_IS_TLS13(s)) {
3818 if (!WPACKET_put_bytes_u32(pkt, age_add)
3819 || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
3820 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
3821 ERR_R_INTERNAL_ERROR);
3826 /* Start the sub-packet for the actual ticket data */
3827 if (!WPACKET_start_sub_packet_u16(pkt)) {
3828 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
3829 ERR_R_INTERNAL_ERROR);
3836 static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
3837 unsigned char *tick_nonce)
3839 unsigned char *senc = NULL;
3840 EVP_CIPHER_CTX *ctx = NULL;
3841 HMAC_CTX *hctx = NULL;
3842 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3843 const unsigned char *const_p;
3844 int len, slen_full, slen, lenfinal;
3847 SSL_CTX *tctx = s->session_ctx;
3848 unsigned char iv[EVP_MAX_IV_LENGTH];
3849 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3851 size_t macoffset, macendoffset;
3853 /* get session encoding length */
3854 slen_full = i2d_SSL_SESSION(s->session, NULL);
3856 * Some length values are 16 bits, so forget it if session is too
3859 if (slen_full == 0 || slen_full > 0xFF00) {
3860 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3861 ERR_R_INTERNAL_ERROR);
3864 senc = OPENSSL_malloc(slen_full);
3866 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3867 SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_MALLOC_FAILURE);
3871 ctx = EVP_CIPHER_CTX_new();
3872 hctx = HMAC_CTX_new();
3873 if (ctx == NULL || hctx == NULL) {
3874 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3875 ERR_R_MALLOC_FAILURE);
3880 if (!i2d_SSL_SESSION(s->session, &p)) {
3881 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3882 ERR_R_INTERNAL_ERROR);
3887 * create a fresh copy (not shared with other threads) to clean up
3890 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3892 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3893 ERR_R_INTERNAL_ERROR);
3897 slen = i2d_SSL_SESSION(sess, NULL);
3898 if (slen == 0 || slen > slen_full) {
3899 /* shouldn't ever happen */
3900 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3901 ERR_R_INTERNAL_ERROR);
3902 SSL_SESSION_free(sess);
3906 if (!i2d_SSL_SESSION(sess, &p)) {
3907 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3908 ERR_R_INTERNAL_ERROR);
3909 SSL_SESSION_free(sess);
3912 SSL_SESSION_free(sess);
3915 * Initialize HMAC and cipher contexts. If callback present it does
3916 * all the work otherwise use generated values from parent ctx.
3918 if (tctx->ext.ticket_key_cb) {
3919 /* if 0 is returned, write an empty ticket */
3920 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3925 /* Put timeout and length */
3926 if (!WPACKET_put_bytes_u32(pkt, 0)
3927 || !WPACKET_put_bytes_u16(pkt, 0)) {
3928 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3929 SSL_F_CONSTRUCT_STATELESS_TICKET,
3930 ERR_R_INTERNAL_ERROR);
3934 EVP_CIPHER_CTX_free(ctx);
3935 HMAC_CTX_free(hctx);
3939 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3940 SSL_R_CALLBACK_FAILED);
3943 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3945 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3947 iv_len = EVP_CIPHER_iv_length(cipher);
3948 if (RAND_bytes(iv, iv_len) <= 0
3949 || !EVP_EncryptInit_ex(ctx, cipher, NULL,
3950 tctx->ext.secure->tick_aes_key, iv)
3951 || !HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
3952 sizeof(tctx->ext.secure->tick_hmac_key),
3953 EVP_sha256(), NULL)) {
3954 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3955 ERR_R_INTERNAL_ERROR);
3958 memcpy(key_name, tctx->ext.tick_key_name,
3959 sizeof(tctx->ext.tick_key_name));
3962 if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3963 /* SSLfatal() already called */
3967 if (!WPACKET_get_total_written(pkt, &macoffset)
3968 /* Output key name */
3969 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3971 || !WPACKET_memcpy(pkt, iv, iv_len)
3972 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3974 /* Encrypt session data */
3975 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3976 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3977 || encdata1 != encdata2
3978 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3979 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3980 || encdata1 + len != encdata2
3981 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3982 || !WPACKET_get_total_written(pkt, &macendoffset)
3983 || !HMAC_Update(hctx,
3984 (unsigned char *)s->init_buf->data + macoffset,
3985 macendoffset - macoffset)
3986 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3987 || !HMAC_Final(hctx, macdata1, &hlen)
3988 || hlen > EVP_MAX_MD_SIZE
3989 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3990 || macdata1 != macdata2) {
3991 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3992 SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_INTERNAL_ERROR);
3996 /* Close the sub-packet created by create_ticket_prequel() */
3997 if (!WPACKET_close(pkt)) {
3998 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3999 ERR_R_INTERNAL_ERROR);
4006 EVP_CIPHER_CTX_free(ctx);
4007 HMAC_CTX_free(hctx);
4011 static int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
4012 unsigned char *tick_nonce)
4014 if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
4015 /* SSLfatal() already called */
4019 if (!WPACKET_memcpy(pkt, s->session->session_id,
4020 s->session->session_id_length)
4021 || !WPACKET_close(pkt)) {
4022 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATEFUL_TICKET,
4023 ERR_R_INTERNAL_ERROR);
4030 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
4032 SSL_CTX *tctx = s->session_ctx;
4033 unsigned char tick_nonce[TICKET_NONCE_SIZE];
4035 unsigned char age_add_c[sizeof(uint32_t)];
4039 age_add_u.age_add = 0;
4041 if (SSL_IS_TLS13(s)) {
4044 static const unsigned char nonce_label[] = "resumption";
4045 const EVP_MD *md = ssl_handshake_md(s);
4046 int hashleni = EVP_MD_size(md);
4048 /* Ensure cast to size_t is safe */
4049 if (!ossl_assert(hashleni >= 0)) {
4050 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
4051 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
4052 ERR_R_INTERNAL_ERROR);
4055 hashlen = (size_t)hashleni;
4058 * If we already sent one NewSessionTicket, or we resumed then
4059 * s->session may already be in a cache and so we must not modify it.
4060 * Instead we need to take a copy of it and modify that.
4062 if (s->sent_tickets != 0 || s->hit) {
4063 SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
4065 if (new_sess == NULL) {
4066 /* SSLfatal already called */
4070 SSL_SESSION_free(s->session);
4071 s->session = new_sess;
4074 if (!ssl_generate_session_id(s, s->session)) {
4075 /* SSLfatal() already called */
4078 if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0) {
4079 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
4080 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
4081 ERR_R_INTERNAL_ERROR);
4084 s->session->ext.tick_age_add = age_add_u.age_add;
4086 nonce = s->next_ticket_nonce;
4087 for (i = TICKET_NONCE_SIZE; i > 0; i--) {
4088 tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
4092 if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
4094 sizeof(nonce_label) - 1,
4097 s->session->master_key,
4099 /* SSLfatal() already called */
4102 s->session->master_key_length = hashlen;
4104 s->session->time = (long)time(NULL);
4105 if (s->s3->alpn_selected != NULL) {
4106 OPENSSL_free(s->session->ext.alpn_selected);
4107 s->session->ext.alpn_selected =
4108 OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
4109 if (s->session->ext.alpn_selected == NULL) {
4110 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
4111 SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
4112 ERR_R_MALLOC_FAILURE);
4115 s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
4117 s->session->ext.max_early_data = s->max_early_data;
4120 if (tctx->generate_ticket_cb != NULL &&
4121 tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0)
4125 * If we are using anti-replay protection then we behave as if
4126 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4127 * is no point in using full stateless tickets.
4130 && ((s->options & SSL_OP_NO_TICKET) != 0
4131 || (s->max_early_data > 0
4132 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
4133 if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
4134 /* SSLfatal() already called */
4137 } else if (!construct_stateless_ticket(s, pkt, age_add_u.age_add,
4139 /* SSLfatal() already called */
4143 if (SSL_IS_TLS13(s)) {
4144 if (!tls_construct_extensions(s, pkt,
4145 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
4147 /* SSLfatal() already called */
4151 * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4152 * gets reset to 0 if we send more tickets following a post-handshake
4153 * auth, but |next_ticket_nonce| does not.
4156 s->next_ticket_nonce++;
4157 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
4166 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4167 * create a separate message. Returns 1 on success or 0 on failure.
4169 int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
4171 if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
4172 || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
4173 s->ext.ocsp.resp_len)) {
4174 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY,
4175 ERR_R_INTERNAL_ERROR);
4182 int tls_construct_cert_status(SSL *s, WPACKET *pkt)
4184 if (!tls_construct_cert_status_body(s, pkt)) {
4185 /* SSLfatal() already called */
4192 #ifndef OPENSSL_NO_NEXTPROTONEG
4194 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4195 * It sets the next_proto member in s if found
4197 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
4199 PACKET next_proto, padding;
4200 size_t next_proto_len;
4203 * The payload looks like:
4205 * uint8 proto[proto_len];
4206 * uint8 padding_len;
4207 * uint8 padding[padding_len];
4209 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
4210 || !PACKET_get_length_prefixed_1(pkt, &padding)
4211 || PACKET_remaining(pkt) > 0) {
4212 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
4213 SSL_R_LENGTH_MISMATCH);
4214 return MSG_PROCESS_ERROR;
4217 if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
4219 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
4220 ERR_R_INTERNAL_ERROR);
4221 return MSG_PROCESS_ERROR;
4224 s->ext.npn_len = (unsigned char)next_proto_len;
4226 return MSG_PROCESS_CONTINUE_READING;
4230 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
4232 if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
4234 /* SSLfatal() already called */
4241 MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
4243 if (PACKET_remaining(pkt) != 0) {
4244 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
4245 SSL_R_LENGTH_MISMATCH);
4246 return MSG_PROCESS_ERROR;
4249 if (s->early_data_state != SSL_EARLY_DATA_READING
4250 && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
4251 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
4252 ERR_R_INTERNAL_ERROR);
4253 return MSG_PROCESS_ERROR;
4257 * EndOfEarlyData signals a key change so the end of the message must be on
4258 * a record boundary.
4260 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
4261 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
4262 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
4263 SSL_R_NOT_ON_RECORD_BOUNDARY);
4264 return MSG_PROCESS_ERROR;
4267 s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
4268 if (!s->method->ssl3_enc->change_cipher_state(s,
4269 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
4270 /* SSLfatal() already called */
4271 return MSG_PROCESS_ERROR;
4274 return MSG_PROCESS_CONTINUE_READING;