2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
23 /* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
51 #include "../ssl_locl.h"
52 #include "statem_locl.h"
53 #include "internal/constant_time_locl.h"
54 #include <openssl/buffer.h>
55 #include <openssl/rand.h>
56 #include <openssl/objects.h>
57 #include <openssl/evp.h>
58 #include <openssl/hmac.h>
59 #include <openssl/x509.h>
60 #include <openssl/dh.h>
61 #include <openssl/bn.h>
62 #include <openssl/md5.h>
64 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
65 static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt);
66 static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
67 PACKET *cipher_suites,
69 **skp, int sslv2format,
73 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
74 * handshake state transitions when a TLSv1.3 server is reading messages from
75 * the client. The message type that the client has sent is provided in |mt|.
76 * The current state is in |s->statem.hand_state|.
78 * Return values are 1 for success (transition allowed) and 0 on error
79 * (transition not allowed)
81 static int ossl_statem_server13_read_transition(SSL *s, int mt)
83 OSSL_STATEM *st = &s->statem;
86 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
87 * not negotiated TLSv1.3 yet, so that case is handled by
88 * ossl_statem_server_read_transition()
90 switch (st->hand_state) {
94 case TLS_ST_SW_HELLO_RETRY_REQUEST:
95 if (mt == SSL3_MT_CLIENT_HELLO) {
96 st->hand_state = TLS_ST_SR_CLNT_HELLO;
101 case TLS_ST_SW_FINISHED:
102 if (s->s3->tmp.cert_request) {
103 if (mt == SSL3_MT_CERTIFICATE) {
104 st->hand_state = TLS_ST_SR_CERT;
108 if (mt == SSL3_MT_FINISHED) {
109 st->hand_state = TLS_ST_SR_FINISHED;
116 if (s->session->peer == NULL) {
117 if (mt == SSL3_MT_FINISHED) {
118 st->hand_state = TLS_ST_SR_FINISHED;
122 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
123 st->hand_state = TLS_ST_SR_CERT_VRFY;
129 case TLS_ST_SR_CERT_VRFY:
130 if (mt == SSL3_MT_FINISHED) {
131 st->hand_state = TLS_ST_SR_FINISHED;
137 if (mt == SSL3_MT_KEY_UPDATE) {
138 st->hand_state = TLS_ST_SR_KEY_UPDATE;
144 /* No valid transition found */
145 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
146 SSLerr(SSL_F_OSSL_STATEM_SERVER13_READ_TRANSITION,
147 SSL_R_UNEXPECTED_MESSAGE);
152 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
153 * handshake state transitions when the server is reading messages from the
154 * client. The message type that the client has sent is provided in |mt|. The
155 * current state is in |s->statem.hand_state|.
157 * Return values are 1 for success (transition allowed) and 0 on error
158 * (transition not allowed)
160 int ossl_statem_server_read_transition(SSL *s, int mt)
162 OSSL_STATEM *st = &s->statem;
164 if (SSL_IS_TLS13(s)) {
165 if (!ossl_statem_server13_read_transition(s, mt))
170 switch (st->hand_state) {
176 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
177 if (mt == SSL3_MT_CLIENT_HELLO) {
178 st->hand_state = TLS_ST_SR_CLNT_HELLO;
183 case TLS_ST_SW_SRVR_DONE:
185 * If we get a CKE message after a ServerDone then either
186 * 1) We didn't request a Certificate
188 * 2) If we did request one then
189 * a) We allow no Certificate to be returned
191 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
192 * list if we requested a certificate)
194 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
195 if (s->s3->tmp.cert_request) {
196 if (s->version == SSL3_VERSION) {
197 if ((s->verify_mode & SSL_VERIFY_PEER)
198 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
200 * This isn't an unexpected message as such - we're just
201 * not going to accept it because we require a client
204 ssl3_send_alert(s, SSL3_AL_FATAL,
205 SSL3_AD_HANDSHAKE_FAILURE);
206 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
207 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
210 st->hand_state = TLS_ST_SR_KEY_EXCH;
214 st->hand_state = TLS_ST_SR_KEY_EXCH;
217 } else if (s->s3->tmp.cert_request) {
218 if (mt == SSL3_MT_CERTIFICATE) {
219 st->hand_state = TLS_ST_SR_CERT;
226 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
227 st->hand_state = TLS_ST_SR_KEY_EXCH;
232 case TLS_ST_SR_KEY_EXCH:
234 * We should only process a CertificateVerify message if we have
235 * received a Certificate from the client. If so then |s->session->peer|
236 * will be non NULL. In some instances a CertificateVerify message is
237 * not required even if the peer has sent a Certificate (e.g. such as in
238 * the case of static DH). In that case |st->no_cert_verify| should be
241 if (s->session->peer == NULL || st->no_cert_verify) {
242 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
244 * For the ECDH ciphersuites when the client sends its ECDH
245 * pub key in a certificate, the CertificateVerify message is
246 * not sent. Also for GOST ciphersuites when the client uses
247 * its key from the certificate for key exchange.
249 st->hand_state = TLS_ST_SR_CHANGE;
253 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
254 st->hand_state = TLS_ST_SR_CERT_VRFY;
260 case TLS_ST_SR_CERT_VRFY:
261 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
262 st->hand_state = TLS_ST_SR_CHANGE;
267 case TLS_ST_SR_CHANGE:
268 #ifndef OPENSSL_NO_NEXTPROTONEG
269 if (s->s3->npn_seen) {
270 if (mt == SSL3_MT_NEXT_PROTO) {
271 st->hand_state = TLS_ST_SR_NEXT_PROTO;
276 if (mt == SSL3_MT_FINISHED) {
277 st->hand_state = TLS_ST_SR_FINISHED;
280 #ifndef OPENSSL_NO_NEXTPROTONEG
285 #ifndef OPENSSL_NO_NEXTPROTONEG
286 case TLS_ST_SR_NEXT_PROTO:
287 if (mt == SSL3_MT_FINISHED) {
288 st->hand_state = TLS_ST_SR_FINISHED;
294 case TLS_ST_SW_FINISHED:
295 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
296 st->hand_state = TLS_ST_SR_CHANGE;
303 /* No valid transition found */
304 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
305 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
310 * Should we send a ServerKeyExchange message?
312 * Valid return values are:
316 static int send_server_key_exchange(SSL *s)
318 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
321 * only send a ServerKeyExchange if DH or fortezza but we have a
322 * sign only certificate PSK: may send PSK identity hints For
323 * ECC ciphersuites, we send a serverKeyExchange message only if
324 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
325 * the server certificate contains the server's public key for
328 if (alg_k & (SSL_kDHE | SSL_kECDHE)
330 * PSK: send ServerKeyExchange if PSK identity hint if
333 #ifndef OPENSSL_NO_PSK
334 /* Only send SKE if we have identity hint for plain PSK */
335 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
336 && s->cert->psk_identity_hint)
337 /* For other PSK always send SKE */
338 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
340 #ifndef OPENSSL_NO_SRP
341 /* SRP: send ServerKeyExchange */
342 || (alg_k & SSL_kSRP)
352 * Should we send a CertificateRequest message?
354 * Valid return values are:
358 static int send_certificate_request(SSL *s)
361 /* don't request cert unless asked for it: */
362 s->verify_mode & SSL_VERIFY_PEER
364 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
365 * during re-negotiation:
367 && (s->s3->tmp.finish_md_len == 0 ||
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 return WRITE_TRAN_ERROR;
414 if (s->key_update != SSL_KEY_UPDATE_NONE) {
415 st->hand_state = TLS_ST_SW_KEY_UPDATE;
416 return WRITE_TRAN_CONTINUE;
418 /* Try to read from the client instead */
419 return WRITE_TRAN_FINISHED;
421 case TLS_ST_SR_CLNT_HELLO:
422 if (s->hello_retry_request)
423 st->hand_state = TLS_ST_SW_HELLO_RETRY_REQUEST;
425 st->hand_state = TLS_ST_SW_SRVR_HELLO;
426 return WRITE_TRAN_CONTINUE;
428 case TLS_ST_SW_HELLO_RETRY_REQUEST:
429 return WRITE_TRAN_FINISHED;
431 case TLS_ST_SW_SRVR_HELLO:
432 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
433 return WRITE_TRAN_CONTINUE;
435 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
437 st->hand_state = TLS_ST_SW_FINISHED;
438 else if (send_certificate_request(s))
439 st->hand_state = TLS_ST_SW_CERT_REQ;
441 st->hand_state = TLS_ST_SW_CERT;
443 return WRITE_TRAN_CONTINUE;
445 case TLS_ST_SW_CERT_REQ:
446 st->hand_state = TLS_ST_SW_CERT;
447 return WRITE_TRAN_CONTINUE;
450 st->hand_state = TLS_ST_SW_CERT_VRFY;
451 return WRITE_TRAN_CONTINUE;
453 case TLS_ST_SW_CERT_VRFY:
454 st->hand_state = TLS_ST_SW_FINISHED;
455 return WRITE_TRAN_CONTINUE;
457 case TLS_ST_SW_FINISHED:
458 return WRITE_TRAN_FINISHED;
460 case TLS_ST_SR_FINISHED:
462 * Technically we have finished the handshake at this point, but we're
463 * going to remain "in_init" for now and write out the session ticket
465 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
466 * and give the application the opportunity to delay sending the
469 st->hand_state = TLS_ST_SW_SESSION_TICKET;
470 return WRITE_TRAN_CONTINUE;
472 case TLS_ST_SR_KEY_UPDATE:
473 if (s->key_update != SSL_KEY_UPDATE_NONE) {
474 st->hand_state = TLS_ST_SW_KEY_UPDATE;
475 return WRITE_TRAN_CONTINUE;
479 case TLS_ST_SW_KEY_UPDATE:
480 case TLS_ST_SW_SESSION_TICKET:
481 st->hand_state = TLS_ST_OK;
482 ossl_statem_set_in_init(s, 0);
483 return WRITE_TRAN_CONTINUE;
488 * ossl_statem_server_write_transition() works out what handshake state to move
489 * to next when the server is writing messages to be sent to the client.
491 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
493 OSSL_STATEM *st = &s->statem;
496 * Note that before the ClientHello we don't know what version we are going
497 * to negotiate yet, so we don't take this branch until later
501 return ossl_statem_server13_write_transition(s);
503 switch (st->hand_state) {
505 /* Shouldn't happen */
506 return WRITE_TRAN_ERROR;
509 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
510 /* We must be trying to renegotiate */
511 st->hand_state = TLS_ST_SW_HELLO_REQ;
512 st->request_state = TLS_ST_BEFORE;
513 return WRITE_TRAN_CONTINUE;
515 /* Must be an incoming ClientHello */
516 if (!tls_setup_handshake(s)) {
517 ossl_statem_set_error(s);
518 return WRITE_TRAN_ERROR;
523 /* Just go straight to trying to read from the client */
524 return WRITE_TRAN_FINISHED;
526 case TLS_ST_SW_HELLO_REQ:
527 st->hand_state = TLS_ST_OK;
528 ossl_statem_set_in_init(s, 0);
529 return WRITE_TRAN_CONTINUE;
531 case TLS_ST_SR_CLNT_HELLO:
532 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
533 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
534 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
536 st->hand_state = TLS_ST_SW_SRVR_HELLO;
537 return WRITE_TRAN_CONTINUE;
539 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
540 return WRITE_TRAN_FINISHED;
542 case TLS_ST_SW_SRVR_HELLO:
544 if (s->ext.ticket_expected)
545 st->hand_state = TLS_ST_SW_SESSION_TICKET;
547 st->hand_state = TLS_ST_SW_CHANGE;
549 /* Check if it is anon DH or anon ECDH, */
550 /* normal PSK or SRP */
551 if (!(s->s3->tmp.new_cipher->algorithm_auth &
552 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
553 st->hand_state = TLS_ST_SW_CERT;
554 } else if (send_server_key_exchange(s)) {
555 st->hand_state = TLS_ST_SW_KEY_EXCH;
556 } else if (send_certificate_request(s)) {
557 st->hand_state = TLS_ST_SW_CERT_REQ;
559 st->hand_state = TLS_ST_SW_SRVR_DONE;
562 return WRITE_TRAN_CONTINUE;
565 if (s->ext.status_expected) {
566 st->hand_state = TLS_ST_SW_CERT_STATUS;
567 return WRITE_TRAN_CONTINUE;
571 case TLS_ST_SW_CERT_STATUS:
572 if (send_server_key_exchange(s)) {
573 st->hand_state = TLS_ST_SW_KEY_EXCH;
574 return WRITE_TRAN_CONTINUE;
578 case TLS_ST_SW_KEY_EXCH:
579 if (send_certificate_request(s)) {
580 st->hand_state = TLS_ST_SW_CERT_REQ;
581 return WRITE_TRAN_CONTINUE;
585 case TLS_ST_SW_CERT_REQ:
586 st->hand_state = TLS_ST_SW_SRVR_DONE;
587 return WRITE_TRAN_CONTINUE;
589 case TLS_ST_SW_SRVR_DONE:
590 return WRITE_TRAN_FINISHED;
592 case TLS_ST_SR_FINISHED:
594 st->hand_state = TLS_ST_OK;
595 ossl_statem_set_in_init(s, 0);
596 return WRITE_TRAN_CONTINUE;
597 } else if (s->ext.ticket_expected) {
598 st->hand_state = TLS_ST_SW_SESSION_TICKET;
600 st->hand_state = TLS_ST_SW_CHANGE;
602 return WRITE_TRAN_CONTINUE;
604 case TLS_ST_SW_SESSION_TICKET:
605 st->hand_state = TLS_ST_SW_CHANGE;
606 return WRITE_TRAN_CONTINUE;
608 case TLS_ST_SW_CHANGE:
609 st->hand_state = TLS_ST_SW_FINISHED;
610 return WRITE_TRAN_CONTINUE;
612 case TLS_ST_SW_FINISHED:
614 return WRITE_TRAN_FINISHED;
616 st->hand_state = TLS_ST_OK;
617 ossl_statem_set_in_init(s, 0);
618 return WRITE_TRAN_CONTINUE;
623 * Perform any pre work that needs to be done prior to sending a message from
624 * the server to the client.
626 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
628 OSSL_STATEM *st = &s->statem;
630 switch (st->hand_state) {
632 /* No pre work to be done */
635 case TLS_ST_SW_HELLO_REQ:
638 dtls1_clear_sent_buffer(s);
641 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
643 if (SSL_IS_DTLS(s)) {
644 dtls1_clear_sent_buffer(s);
645 /* We don't buffer this message so don't use the timer */
650 case TLS_ST_SW_SRVR_HELLO:
651 if (SSL_IS_DTLS(s)) {
653 * Messages we write from now on should be bufferred and
654 * retransmitted if necessary, so we need to use the timer now
660 case TLS_ST_SW_SRVR_DONE:
661 #ifndef OPENSSL_NO_SCTP
662 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
663 return dtls_wait_for_dry(s);
665 return WORK_FINISHED_CONTINUE;
667 case TLS_ST_SW_SESSION_TICKET:
668 if (SSL_IS_TLS13(s)) {
670 * Actually this is the end of the handshake, but we're going
671 * straight into writing the session ticket out. So we finish off
672 * the handshake, but keep the various buffers active.
674 return tls_finish_handshake(s, wst, 0);
675 } if (SSL_IS_DTLS(s)) {
677 * We're into the last flight. We don't retransmit the last flight
678 * unless we need to, so we don't use the timer
684 case TLS_ST_SW_CHANGE:
685 s->session->cipher = s->s3->tmp.new_cipher;
686 if (!s->method->ssl3_enc->setup_key_block(s)) {
687 ossl_statem_set_error(s);
690 if (SSL_IS_DTLS(s)) {
692 * We're into the last flight. We don't retransmit the last flight
693 * unless we need to, so we don't use the timer. This might have
694 * already been set to 0 if we sent a NewSessionTicket message,
695 * but we'll set it again here in case we didn't.
699 return WORK_FINISHED_CONTINUE;
702 return tls_finish_handshake(s, wst, 1);
705 return WORK_FINISHED_CONTINUE;
709 * Perform any work that needs to be done after sending a message from the
710 * server to the client.
712 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
714 OSSL_STATEM *st = &s->statem;
718 switch (st->hand_state) {
720 /* No post work to be done */
723 case TLS_ST_SW_HELLO_RETRY_REQUEST:
724 if (statem_flush(s) != 1)
728 case TLS_ST_SW_HELLO_REQ:
729 if (statem_flush(s) != 1)
731 if (!ssl3_init_finished_mac(s)) {
732 ossl_statem_set_error(s);
737 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
738 if (statem_flush(s) != 1)
740 /* HelloVerifyRequest resets Finished MAC */
741 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
742 ossl_statem_set_error(s);
746 * The next message should be another ClientHello which we need to
747 * treat like it was the first packet
752 case TLS_ST_SW_SRVR_HELLO:
753 #ifndef OPENSSL_NO_SCTP
754 if (SSL_IS_DTLS(s) && s->hit) {
755 unsigned char sctpauthkey[64];
756 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
759 * Add new shared key for SCTP-Auth, will be ignored if no
762 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
763 sizeof(DTLS1_SCTP_AUTH_LABEL));
765 if (SSL_export_keying_material(s, sctpauthkey,
766 sizeof(sctpauthkey), labelbuffer,
767 sizeof(labelbuffer), NULL, 0,
769 ossl_statem_set_error(s);
773 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
774 sizeof(sctpauthkey), sctpauthkey);
778 * TODO(TLS1.3): This actually causes a problem. We don't yet know
779 * whether the next record we are going to receive is an unencrypted
780 * alert, or an encrypted handshake message. We're going to need
781 * something clever in the record layer for this.
783 if (SSL_IS_TLS13(s)) {
784 if (!s->method->ssl3_enc->setup_key_block(s)
785 || !s->method->ssl3_enc->change_cipher_state(s,
786 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)
787 || !s->method->ssl3_enc->change_cipher_state(s,
788 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ))
793 case TLS_ST_SW_CHANGE:
794 #ifndef OPENSSL_NO_SCTP
795 if (SSL_IS_DTLS(s) && !s->hit) {
797 * Change to new shared key of SCTP-Auth, will be ignored if
800 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
804 if (!s->method->ssl3_enc->change_cipher_state(s,
805 SSL3_CHANGE_CIPHER_SERVER_WRITE))
807 ossl_statem_set_error(s);
812 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
815 case TLS_ST_SW_SRVR_DONE:
816 if (statem_flush(s) != 1)
820 case TLS_ST_SW_FINISHED:
821 if (statem_flush(s) != 1)
823 #ifndef OPENSSL_NO_SCTP
824 if (SSL_IS_DTLS(s) && s->hit) {
826 * Change to new shared key of SCTP-Auth, will be ignored if
829 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
833 if (SSL_IS_TLS13(s)) {
834 if (!s->method->ssl3_enc->generate_master_secret(s,
835 s->master_secret, s->handshake_secret, 0,
836 &s->session->master_key_length)
837 || !s->method->ssl3_enc->change_cipher_state(s,
838 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
843 case TLS_ST_SW_KEY_UPDATE:
844 if (statem_flush(s) != 1)
846 if (!tls13_update_key(s, 1))
850 case TLS_ST_SW_SESSION_TICKET:
851 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
856 return WORK_FINISHED_CONTINUE;
860 * Get the message construction function and message type for sending from the
863 * Valid return values are:
867 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
868 confunc_f *confunc, int *mt)
870 OSSL_STATEM *st = &s->statem;
872 switch (st->hand_state) {
874 /* Shouldn't happen */
877 case TLS_ST_SW_CHANGE:
879 *confunc = dtls_construct_change_cipher_spec;
881 *confunc = tls_construct_change_cipher_spec;
882 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
885 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
886 *confunc = dtls_construct_hello_verify_request;
887 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
890 case TLS_ST_SW_HELLO_REQ:
891 /* No construction function needed */
893 *mt = SSL3_MT_HELLO_REQUEST;
896 case TLS_ST_SW_SRVR_HELLO:
897 *confunc = tls_construct_server_hello;
898 *mt = SSL3_MT_SERVER_HELLO;
902 *confunc = tls_construct_server_certificate;
903 *mt = SSL3_MT_CERTIFICATE;
906 case TLS_ST_SW_CERT_VRFY:
907 *confunc = tls_construct_cert_verify;
908 *mt = SSL3_MT_CERTIFICATE_VERIFY;
912 case TLS_ST_SW_KEY_EXCH:
913 *confunc = tls_construct_server_key_exchange;
914 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
917 case TLS_ST_SW_CERT_REQ:
918 *confunc = tls_construct_certificate_request;
919 *mt = SSL3_MT_CERTIFICATE_REQUEST;
922 case TLS_ST_SW_SRVR_DONE:
923 *confunc = tls_construct_server_done;
924 *mt = SSL3_MT_SERVER_DONE;
927 case TLS_ST_SW_SESSION_TICKET:
928 *confunc = tls_construct_new_session_ticket;
929 *mt = SSL3_MT_NEWSESSION_TICKET;
932 case TLS_ST_SW_CERT_STATUS:
933 *confunc = tls_construct_cert_status;
934 *mt = SSL3_MT_CERTIFICATE_STATUS;
937 case TLS_ST_SW_FINISHED:
938 *confunc = tls_construct_finished;
939 *mt = SSL3_MT_FINISHED;
942 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
943 *confunc = tls_construct_encrypted_extensions;
944 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
947 case TLS_ST_SW_HELLO_RETRY_REQUEST:
948 *confunc = tls_construct_hello_retry_request;
949 *mt = SSL3_MT_HELLO_RETRY_REQUEST;
952 case TLS_ST_SW_KEY_UPDATE:
953 *confunc = tls_construct_key_update;
954 *mt = SSL3_MT_KEY_UPDATE;
962 * Maximum size (excluding the Handshake header) of a ClientHello message,
963 * calculated as follows:
965 * 2 + # client_version
966 * 32 + # only valid length for random
967 * 1 + # length of session_id
968 * 32 + # maximum size for session_id
969 * 2 + # length of cipher suites
970 * 2^16-2 + # maximum length of cipher suites array
971 * 1 + # length of compression_methods
972 * 2^8-1 + # maximum length of compression methods
973 * 2 + # length of extensions
974 * 2^16-1 # maximum length of extensions
976 #define CLIENT_HELLO_MAX_LENGTH 131396
978 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
979 #define NEXT_PROTO_MAX_LENGTH 514
982 * Returns the maximum allowed length for the current message that we are
983 * reading. Excludes the message header.
985 size_t ossl_statem_server_max_message_size(SSL *s)
987 OSSL_STATEM *st = &s->statem;
989 switch (st->hand_state) {
991 /* Shouldn't happen */
994 case TLS_ST_SR_CLNT_HELLO:
995 return CLIENT_HELLO_MAX_LENGTH;
998 return s->max_cert_list;
1000 case TLS_ST_SR_KEY_EXCH:
1001 return CLIENT_KEY_EXCH_MAX_LENGTH;
1003 case TLS_ST_SR_CERT_VRFY:
1004 return SSL3_RT_MAX_PLAIN_LENGTH;
1006 #ifndef OPENSSL_NO_NEXTPROTONEG
1007 case TLS_ST_SR_NEXT_PROTO:
1008 return NEXT_PROTO_MAX_LENGTH;
1011 case TLS_ST_SR_CHANGE:
1012 return CCS_MAX_LENGTH;
1014 case TLS_ST_SR_FINISHED:
1015 return FINISHED_MAX_LENGTH;
1017 case TLS_ST_SR_KEY_UPDATE:
1018 return KEY_UPDATE_MAX_LENGTH;
1023 * Process a message that the server has received from the client.
1025 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1027 OSSL_STATEM *st = &s->statem;
1029 switch (st->hand_state) {
1031 /* Shouldn't happen */
1032 return MSG_PROCESS_ERROR;
1034 case TLS_ST_SR_CLNT_HELLO:
1035 return tls_process_client_hello(s, pkt);
1037 case TLS_ST_SR_CERT:
1038 return tls_process_client_certificate(s, pkt);
1040 case TLS_ST_SR_KEY_EXCH:
1041 return tls_process_client_key_exchange(s, pkt);
1043 case TLS_ST_SR_CERT_VRFY:
1044 return tls_process_cert_verify(s, pkt);
1046 #ifndef OPENSSL_NO_NEXTPROTONEG
1047 case TLS_ST_SR_NEXT_PROTO:
1048 return tls_process_next_proto(s, pkt);
1051 case TLS_ST_SR_CHANGE:
1052 return tls_process_change_cipher_spec(s, pkt);
1054 case TLS_ST_SR_FINISHED:
1055 return tls_process_finished(s, pkt);
1057 case TLS_ST_SR_KEY_UPDATE:
1058 return tls_process_key_update(s, pkt);
1064 * Perform any further processing required following the receipt of a message
1067 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1069 OSSL_STATEM *st = &s->statem;
1071 switch (st->hand_state) {
1073 /* Shouldn't happen */
1076 case TLS_ST_SR_CLNT_HELLO:
1077 return tls_post_process_client_hello(s, wst);
1079 case TLS_ST_SR_KEY_EXCH:
1080 return tls_post_process_client_key_exchange(s, wst);
1082 case TLS_ST_SR_CERT_VRFY:
1083 #ifndef OPENSSL_NO_SCTP
1084 if ( /* Is this SCTP? */
1085 BIO_dgram_is_sctp(SSL_get_wbio(s))
1086 /* Are we renegotiating? */
1087 && s->renegotiate && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
1088 s->s3->in_read_app_data = 2;
1089 s->rwstate = SSL_READING;
1090 BIO_clear_retry_flags(SSL_get_rbio(s));
1091 BIO_set_retry_read(SSL_get_rbio(s));
1092 ossl_statem_set_sctp_read_sock(s, 1);
1095 ossl_statem_set_sctp_read_sock(s, 0);
1098 return WORK_FINISHED_CONTINUE;
1100 return WORK_FINISHED_CONTINUE;
1103 #ifndef OPENSSL_NO_SRP
1104 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
1106 int ret = SSL_ERROR_NONE;
1108 *al = SSL_AD_UNRECOGNIZED_NAME;
1110 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1111 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1112 if (s->srp_ctx.login == NULL) {
1114 * RFC 5054 says SHOULD reject, we do so if There is no srp
1117 ret = SSL3_AL_FATAL;
1118 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1120 ret = SSL_srp_server_param_with_username(s, al);
1127 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1130 /* Always use DTLS 1.0 version: see RFC 6347 */
1131 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1132 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1138 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1140 unsigned int cookie_leni;
1141 if (s->ctx->app_gen_cookie_cb == NULL ||
1142 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1143 &cookie_leni) == 0 ||
1144 cookie_leni > 255) {
1145 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1146 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1149 s->d1->cookie_len = cookie_leni;
1151 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1152 s->d1->cookie_len)) {
1153 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, ERR_R_INTERNAL_ERROR);
1160 #ifndef OPENSSL_NO_EC
1162 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1163 * SecureTransport using the TLS extension block in |hello|.
1164 * Safari, since 10.6, sends exactly these extensions, in this order:
1169 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1170 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1171 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1172 * 10.8..10.8.3 (which don't work).
1174 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1176 static const unsigned char kSafariExtensionsBlock[] = {
1177 0x00, 0x0a, /* elliptic_curves extension */
1178 0x00, 0x08, /* 8 bytes */
1179 0x00, 0x06, /* 6 bytes of curve ids */
1180 0x00, 0x17, /* P-256 */
1181 0x00, 0x18, /* P-384 */
1182 0x00, 0x19, /* P-521 */
1184 0x00, 0x0b, /* ec_point_formats */
1185 0x00, 0x02, /* 2 bytes */
1186 0x01, /* 1 point format */
1187 0x00, /* uncompressed */
1188 /* The following is only present in TLS 1.2 */
1189 0x00, 0x0d, /* signature_algorithms */
1190 0x00, 0x0c, /* 12 bytes */
1191 0x00, 0x0a, /* 10 bytes */
1192 0x05, 0x01, /* SHA-384/RSA */
1193 0x04, 0x01, /* SHA-256/RSA */
1194 0x02, 0x01, /* SHA-1/RSA */
1195 0x04, 0x03, /* SHA-256/ECDSA */
1196 0x02, 0x03, /* SHA-1/ECDSA */
1198 /* Length of the common prefix (first two extensions). */
1199 static const size_t kSafariCommonExtensionsLength = 18;
1204 tmppkt = hello->extensions;
1206 if (!PACKET_forward(&tmppkt, 2)
1207 || !PACKET_get_net_2(&tmppkt, &type)
1208 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1212 if (type != TLSEXT_TYPE_server_name)
1215 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1216 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1218 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1221 #endif /* !OPENSSL_NO_EC */
1223 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1225 int i, al = SSL_AD_INTERNAL_ERROR;
1229 const SSL_CIPHER *c;
1230 #ifndef OPENSSL_NO_COMP
1231 SSL_COMP *comp = NULL;
1233 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1235 /* |cookie| will only be initialized for DTLS. */
1236 PACKET session_id, compression, extensions, cookie;
1237 static const unsigned char null_compression = 0;
1238 CLIENTHELLO_MSG clienthello;
1240 /* Check if this is actually an unexpected renegotiation ClientHello */
1241 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1246 /* This is a real handshake so make sure we clean it up at the end */
1247 s->statem.cleanuphand = 1;
1250 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1252 memset(&clienthello, 0, sizeof(clienthello));
1253 clienthello.isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1254 PACKET_null_init(&cookie);
1256 if (clienthello.isv2) {
1259 if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
1260 al = SSL_AD_HANDSHAKE_FAILURE;
1261 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1266 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1267 * header is sent directly on the wire, not wrapped as a TLS
1268 * record. Our record layer just processes the message length and passes
1269 * the rest right through. Its format is:
1271 * 0-1 msg_length - decoded by the record layer
1272 * 2 msg_type - s->init_msg points here
1274 * 5-6 cipher_spec_length
1275 * 7-8 session_id_length
1276 * 9-10 challenge_length
1280 if (!PACKET_get_1(pkt, &mt)
1281 || mt != SSL2_MT_CLIENT_HELLO) {
1283 * Should never happen. We should have tested this in the record
1284 * layer in order to have determined that this is a SSLv2 record
1285 * in the first place
1287 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1292 if (!PACKET_get_net_2(pkt, &clienthello.legacy_version)) {
1293 al = SSL_AD_DECODE_ERROR;
1294 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1298 /* Parse the message and load client random. */
1299 if (clienthello.isv2) {
1301 * Handle an SSLv2 backwards compatible ClientHello
1302 * Note, this is only for SSLv3+ using the backward compatible format.
1303 * Real SSLv2 is not supported, and is rejected below.
1305 unsigned int ciphersuite_len, session_id_len, challenge_len;
1308 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1309 || !PACKET_get_net_2(pkt, &session_id_len)
1310 || !PACKET_get_net_2(pkt, &challenge_len)) {
1311 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1312 SSL_R_RECORD_LENGTH_MISMATCH);
1313 al = SSL_AD_DECODE_ERROR;
1317 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1318 al = SSL_AD_DECODE_ERROR;
1319 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1323 if (!PACKET_get_sub_packet(pkt, &clienthello.ciphersuites,
1325 || !PACKET_copy_bytes(pkt, clienthello.session_id, session_id_len)
1326 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1327 /* No extensions. */
1328 || PACKET_remaining(pkt) != 0) {
1329 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1330 SSL_R_RECORD_LENGTH_MISMATCH);
1331 al = SSL_AD_DECODE_ERROR;
1334 clienthello.session_id_len = session_id_len;
1336 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1337 * here rather than sizeof(clienthello.random) because that is the limit
1338 * for SSLv3 and it is fixed. It won't change even if
1339 * sizeof(clienthello.random) does.
1341 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1342 ? SSL3_RANDOM_SIZE : challenge_len;
1343 memset(clienthello.random, 0, SSL3_RANDOM_SIZE);
1344 if (!PACKET_copy_bytes(&challenge,
1345 clienthello.random + SSL3_RANDOM_SIZE -
1346 challenge_len, challenge_len)
1347 /* Advertise only null compression. */
1348 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1349 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1350 al = SSL_AD_INTERNAL_ERROR;
1354 PACKET_null_init(&clienthello.extensions);
1356 /* Regular ClientHello. */
1357 if (!PACKET_copy_bytes(pkt, clienthello.random, SSL3_RANDOM_SIZE)
1358 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1359 || !PACKET_copy_all(&session_id, clienthello.session_id,
1360 SSL_MAX_SSL_SESSION_ID_LENGTH,
1361 &clienthello.session_id_len)) {
1362 al = SSL_AD_DECODE_ERROR;
1363 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1367 if (SSL_IS_DTLS(s)) {
1368 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1369 al = SSL_AD_DECODE_ERROR;
1370 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1373 if (!PACKET_copy_all(&cookie, clienthello.dtls_cookie,
1374 DTLS1_COOKIE_LENGTH,
1375 &clienthello.dtls_cookie_len)) {
1376 al = SSL_AD_DECODE_ERROR;
1377 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1381 * If we require cookies and this ClientHello doesn't contain one,
1382 * just return since we do not want to allocate any memory yet.
1383 * So check cookie length...
1385 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1386 if (clienthello.dtls_cookie_len == 0)
1391 if (!PACKET_get_length_prefixed_2(pkt, &clienthello.ciphersuites)) {
1392 al = SSL_AD_DECODE_ERROR;
1393 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1397 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1398 al = SSL_AD_DECODE_ERROR;
1399 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1403 /* Could be empty. */
1404 if (PACKET_remaining(pkt) == 0) {
1405 PACKET_null_init(&clienthello.extensions);
1407 if (!PACKET_get_length_prefixed_2(pkt, &clienthello.extensions)) {
1408 al = SSL_AD_DECODE_ERROR;
1409 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1415 if (!PACKET_copy_all(&compression, clienthello.compressions,
1416 MAX_COMPRESSIONS_SIZE,
1417 &clienthello.compressions_len)) {
1418 al = SSL_AD_DECODE_ERROR;
1419 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1423 /* Preserve the raw extensions PACKET for later use */
1424 extensions = clienthello.extensions;
1425 if (!tls_collect_extensions(s, &extensions, EXT_CLIENT_HELLO,
1426 &clienthello.pre_proc_exts, &al, NULL)) {
1427 /* SSLerr already been called */
1431 /* Finished parsing the ClientHello, now we can start processing it */
1433 /* Set up the client_random */
1434 memcpy(s->s3->client_random, clienthello.random, SSL3_RANDOM_SIZE);
1436 /* Choose the version */
1438 if (clienthello.isv2) {
1439 if (clienthello.legacy_version == SSL2_VERSION
1440 || (clienthello.legacy_version & 0xff00)
1441 != (SSL3_VERSION_MAJOR << 8)) {
1443 * This is real SSLv2 or something complete unknown. We don't
1446 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1450 s->client_version = clienthello.legacy_version;
1453 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1454 * versions are potentially compatible. Version negotiation comes later.
1456 if (!SSL_IS_DTLS(s)) {
1457 protverr = ssl_choose_server_version(s, &clienthello);
1458 } else if (s->method->version != DTLS_ANY_VERSION &&
1459 DTLS_VERSION_LT((int)clienthello.legacy_version, s->version)) {
1460 protverr = SSL_R_VERSION_TOO_LOW;
1466 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1467 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1468 /* like ssl3_get_record, send alert using remote version number */
1469 s->version = s->client_version = clienthello.legacy_version;
1471 al = SSL_AD_PROTOCOL_VERSION;
1475 if (SSL_IS_DTLS(s)) {
1476 /* Empty cookie was already handled above by returning early. */
1477 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1478 if (s->ctx->app_verify_cookie_cb != NULL) {
1479 if (s->ctx->app_verify_cookie_cb(s, clienthello.dtls_cookie,
1480 clienthello.dtls_cookie_len) == 0) {
1481 al = SSL_AD_HANDSHAKE_FAILURE;
1482 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1483 SSL_R_COOKIE_MISMATCH);
1485 /* else cookie verification succeeded */
1487 /* default verification */
1488 } else if (s->d1->cookie_len != clienthello.dtls_cookie_len
1489 || memcmp(clienthello.dtls_cookie, s->d1->cookie,
1490 s->d1->cookie_len) != 0) {
1491 al = SSL_AD_HANDSHAKE_FAILURE;
1492 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1495 s->d1->cookie_verified = 1;
1497 if (s->method->version == DTLS_ANY_VERSION) {
1498 protverr = ssl_choose_server_version(s, &clienthello);
1499 if (protverr != 0) {
1500 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1501 s->version = s->client_version;
1502 al = SSL_AD_PROTOCOL_VERSION;
1510 /* We need to do this before getting the session */
1511 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1513 clienthello.pre_proc_exts, NULL, 0, &al)) {
1514 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1519 * We don't allow resumption in a backwards compatible ClientHello.
1520 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1522 * Versions before 0.9.7 always allow clients to resume sessions in
1523 * renegotiation. 0.9.7 and later allow this by default, but optionally
1524 * ignore resumption requests with flag
1525 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1526 * than a change to default behavior so that applications relying on
1527 * this for security won't even compile against older library versions).
1528 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1529 * request renegotiation but not a new session (s->new_session remains
1530 * unset): for servers, this essentially just means that the
1531 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1534 if (clienthello.isv2 ||
1536 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1537 if (!ssl_get_new_session(s, 1))
1540 i = ssl_get_prev_session(s, &clienthello, &al);
1542 /* previous session */
1544 } else if (i == -1) {
1548 if (!ssl_get_new_session(s, 1))
1553 if (ssl_bytes_to_cipher_list(s, &clienthello.ciphersuites, &ciphers,
1554 clienthello.isv2, &al) == NULL) {
1558 /* If it is a hit, check that the cipher is in the list */
1561 id = s->session->cipher->id;
1564 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1566 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1567 c = sk_SSL_CIPHER_value(ciphers, i);
1569 fprintf(stderr, "client [%2d of %2d]:%s\n",
1570 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1579 * we need to have the cipher in the cipher list if we are asked
1582 al = SSL_AD_ILLEGAL_PARAMETER;
1583 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1584 SSL_R_REQUIRED_CIPHER_MISSING);
1589 for (loop = 0; loop < clienthello.compressions_len; loop++) {
1590 if (clienthello.compressions[loop] == 0)
1594 if (loop >= clienthello.compressions_len) {
1596 al = SSL_AD_DECODE_ERROR;
1597 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1601 #ifndef OPENSSL_NO_EC
1602 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1603 ssl_check_for_safari(s, &clienthello);
1604 #endif /* !OPENSSL_NO_EC */
1606 /* TLS extensions */
1607 if (!tls_parse_all_extensions(s, EXT_CLIENT_HELLO,
1608 clienthello.pre_proc_exts, NULL, 0, &al)) {
1609 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1614 * Check if we want to use external pre-shared secret for this handshake
1615 * for not reused session only. We need to generate server_random before
1616 * calling tls_session_secret_cb in order to allow SessionTicket
1617 * processing to use it in key derivation.
1621 pos = s->s3->server_random;
1622 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1627 if (!s->hit && s->version >= TLS1_VERSION && s->ext.session_secret_cb) {
1628 const SSL_CIPHER *pref_cipher = NULL;
1630 * s->session->master_key_length is a size_t, but this is an int for
1631 * backwards compat reasons
1633 int master_key_length;
1635 master_key_length = sizeof(s->session->master_key);
1636 if (s->ext.session_secret_cb(s, s->session->master_key,
1637 &master_key_length, ciphers,
1639 s->ext.session_secret_cb_arg)
1640 && master_key_length > 0) {
1641 s->session->master_key_length = master_key_length;
1643 s->session->ciphers = ciphers;
1644 s->session->verify_result = X509_V_OK;
1648 /* check if some cipher was preferred by call back */
1649 if (pref_cipher == NULL)
1650 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1651 SSL_get_ciphers(s));
1652 if (pref_cipher == NULL) {
1653 al = SSL_AD_HANDSHAKE_FAILURE;
1654 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1658 s->session->cipher = pref_cipher;
1659 sk_SSL_CIPHER_free(s->cipher_list);
1660 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1661 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1662 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1667 * Worst case, we will use the NULL compression, but if we have other
1668 * options, we will now look for them. We have complen-1 compression
1669 * algorithms from the client, starting at q.
1671 s->s3->tmp.new_compression = NULL;
1672 #ifndef OPENSSL_NO_COMP
1673 /* This only happens if we have a cache hit */
1674 if (s->session->compress_meth != 0) {
1675 int m, comp_id = s->session->compress_meth;
1677 /* Perform sanity checks on resumed compression algorithm */
1678 /* Can't disable compression */
1679 if (!ssl_allow_compression(s)) {
1680 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1681 SSL_R_INCONSISTENT_COMPRESSION);
1684 /* Look for resumed compression method */
1685 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1686 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1687 if (comp_id == comp->id) {
1688 s->s3->tmp.new_compression = comp;
1692 if (s->s3->tmp.new_compression == NULL) {
1693 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1694 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1697 /* Look for resumed method in compression list */
1698 for (k = 0; k < clienthello.compressions_len; k++) {
1699 if (clienthello.compressions[k] == comp_id)
1702 if (k >= clienthello.compressions_len) {
1703 al = SSL_AD_ILLEGAL_PARAMETER;
1704 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1705 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1710 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1711 /* See if we have a match */
1712 int m, nn, v, done = 0;
1715 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1716 for (m = 0; m < nn; m++) {
1717 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1719 for (o = 0; o < clienthello.compressions_len; o++) {
1720 if (v == clienthello.compressions[o]) {
1729 s->s3->tmp.new_compression = comp;
1735 * If compression is disabled we'd better not try to resume a session
1736 * using compression.
1738 if (s->session->compress_meth != 0) {
1739 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1745 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1749 #ifdef OPENSSL_NO_COMP
1750 s->session->compress_meth = 0;
1752 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1754 sk_SSL_CIPHER_free(s->session->ciphers);
1755 s->session->ciphers = ciphers;
1756 if (ciphers == NULL) {
1757 al = SSL_AD_INTERNAL_ERROR;
1758 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1762 if (!tls1_set_server_sigalgs(s)) {
1763 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1768 sk_SSL_CIPHER_free(ciphers);
1769 OPENSSL_free(clienthello.pre_proc_exts);
1770 return MSG_PROCESS_CONTINUE_PROCESSING;
1772 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1774 ossl_statem_set_error(s);
1776 sk_SSL_CIPHER_free(ciphers);
1777 OPENSSL_free(clienthello.pre_proc_exts);
1779 return MSG_PROCESS_ERROR;
1783 * Call the status request callback if needed. Upon success, returns 1.
1784 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
1786 static int tls_handle_status_request(SSL *s, int *al)
1788 s->ext.status_expected = 0;
1791 * If status request then ask callback what to do. Note: this must be
1792 * called after servername callbacks in case the certificate has changed,
1793 * and must be called after the cipher has been chosen because this may
1794 * influence which certificate is sent
1796 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1797 && s->ctx->ext.status_cb != NULL) {
1800 /* If no certificate can't return certificate status */
1801 if (s->s3->tmp.cert != NULL) {
1803 * Set current certificate to one we will use so SSL_get_certificate
1804 * et al can pick it up.
1806 s->cert->key = s->s3->tmp.cert;
1807 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
1809 /* We don't want to send a status request response */
1810 case SSL_TLSEXT_ERR_NOACK:
1811 s->ext.status_expected = 0;
1813 /* status request response should be sent */
1814 case SSL_TLSEXT_ERR_OK:
1815 if (s->ext.ocsp.resp)
1816 s->ext.status_expected = 1;
1818 /* something bad happened */
1819 case SSL_TLSEXT_ERR_ALERT_FATAL:
1821 *al = SSL_AD_INTERNAL_ERROR;
1830 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1832 int al = SSL_AD_HANDSHAKE_FAILURE;
1833 const SSL_CIPHER *cipher;
1835 if (wst == WORK_MORE_A) {
1837 /* Let cert callback update server certificates if required */
1838 if (s->cert->cert_cb) {
1839 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1841 al = SSL_AD_INTERNAL_ERROR;
1842 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1843 SSL_R_CERT_CB_ERROR);
1847 s->rwstate = SSL_X509_LOOKUP;
1850 s->rwstate = SSL_NOTHING;
1853 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1855 if (cipher == NULL) {
1856 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1857 SSL_R_NO_SHARED_CIPHER);
1860 s->s3->tmp.new_cipher = cipher;
1861 if (!tls_choose_sigalg(s, &al))
1863 /* check whether we should disable session resumption */
1864 if (s->not_resumable_session_cb != NULL)
1865 s->session->not_resumable =
1866 s->not_resumable_session_cb(s, ((cipher->algorithm_mkey
1867 & (SSL_kDHE | SSL_kECDHE))
1869 if (s->session->not_resumable)
1870 /* do not send a session ticket */
1871 s->ext.ticket_expected = 0;
1873 /* Session-id reuse */
1874 s->s3->tmp.new_cipher = s->session->cipher;
1878 * we now have the following setup.
1880 * cipher_list - our preferred list of ciphers
1881 * ciphers - the clients preferred list of ciphers
1882 * compression - basically ignored right now
1883 * ssl version is set - sslv3
1884 * s->session - The ssl session has been setup.
1885 * s->hit - session reuse flag
1886 * s->s3->tmp.new_cipher- the new cipher to use.
1890 * Call status_request callback if needed. Has to be done after the
1891 * certificate callbacks etc above.
1893 if (!tls_handle_status_request(s, &al)) {
1894 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1895 SSL_R_CLIENTHELLO_TLSEXT);
1901 #ifndef OPENSSL_NO_SRP
1902 if (wst == WORK_MORE_B) {
1904 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1906 * callback indicates further work to be done
1908 s->rwstate = SSL_X509_LOOKUP;
1911 if (ret != SSL_ERROR_NONE) {
1913 * This is not really an error but the only means to for
1914 * a client to detect whether srp is supported.
1916 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1917 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1918 SSL_R_CLIENTHELLO_TLSEXT);
1920 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1921 SSL_R_PSK_IDENTITY_NOT_FOUND);
1927 return WORK_FINISHED_STOP;
1929 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1930 ossl_statem_set_error(s);
1934 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
1936 int compm, al = SSL_AD_INTERNAL_ERROR;
1940 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
1941 version = SSL_IS_TLS13(s) ? TLS1_3_VERSION_DRAFT : s->version;
1942 if (!WPACKET_put_bytes_u16(pkt, version)
1944 * Random stuff. Filling of the server_random takes place in
1945 * tls_process_client_hello()
1947 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1948 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1953 * There are several cases for the session ID to send
1954 * back in the server hello:
1955 * - For session reuse from the session cache,
1956 * we send back the old session ID.
1957 * - If stateless session reuse (using a session ticket)
1958 * is successful, we send back the client's "session ID"
1959 * (which doesn't actually identify the session).
1960 * - If it is a new session, we send back the new
1962 * - However, if we want the new session to be single-use,
1963 * we send back a 0-length session ID.
1964 * s->hit is non-zero in either case of session reuse,
1965 * so the following won't overwrite an ID that we're supposed
1968 if (s->session->not_resumable ||
1969 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1971 s->session->session_id_length = 0;
1973 sl = s->session->session_id_length;
1974 if (sl > sizeof(s->session->session_id)) {
1975 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1979 /* set up the compression method */
1980 #ifdef OPENSSL_NO_COMP
1983 if (s->s3->tmp.new_compression == NULL)
1986 compm = s->s3->tmp.new_compression->id;
1989 if ((!SSL_IS_TLS13(s)
1990 && !WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl))
1991 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
1992 || (!SSL_IS_TLS13(s)
1993 && !WPACKET_put_bytes_u8(pkt, compm))
1994 || !tls_construct_extensions(s, pkt,
1996 ? EXT_TLS1_3_SERVER_HELLO
1997 : EXT_TLS1_2_SERVER_HELLO,
1999 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2003 if (!(s->verify_mode & SSL_VERIFY_PEER)
2004 && !ssl3_digest_cached_records(s, 0)) {
2005 al = SSL_AD_INTERNAL_ERROR;
2011 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2015 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2017 if (!s->s3->tmp.cert_request) {
2018 if (!ssl3_digest_cached_records(s, 0)) {
2019 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2026 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2028 #ifndef OPENSSL_NO_DH
2029 EVP_PKEY *pkdh = NULL;
2031 #ifndef OPENSSL_NO_EC
2032 unsigned char *encodedPoint = NULL;
2033 size_t encodedlen = 0;
2036 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2037 int al = SSL_AD_INTERNAL_ERROR, i;
2040 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2041 EVP_PKEY_CTX *pctx = NULL;
2042 size_t paramlen, paramoffset;
2044 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
2045 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2049 if (md_ctx == NULL) {
2050 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2054 type = s->s3->tmp.new_cipher->algorithm_mkey;
2056 r[0] = r[1] = r[2] = r[3] = NULL;
2057 #ifndef OPENSSL_NO_PSK
2058 /* Plain PSK or RSAPSK nothing to do */
2059 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2061 #endif /* !OPENSSL_NO_PSK */
2062 #ifndef OPENSSL_NO_DH
2063 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2064 CERT *cert = s->cert;
2066 EVP_PKEY *pkdhp = NULL;
2069 if (s->cert->dh_tmp_auto) {
2070 DH *dhp = ssl_get_auto_dh(s);
2071 pkdh = EVP_PKEY_new();
2072 if (pkdh == NULL || dhp == NULL) {
2074 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2075 ERR_R_INTERNAL_ERROR);
2078 EVP_PKEY_assign_DH(pkdh, dhp);
2081 pkdhp = cert->dh_tmp;
2083 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2084 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2085 pkdh = ssl_dh_to_pkey(dhp);
2087 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2088 ERR_R_INTERNAL_ERROR);
2093 if (pkdhp == NULL) {
2094 al = SSL_AD_HANDSHAKE_FAILURE;
2095 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2096 SSL_R_MISSING_TMP_DH_KEY);
2099 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2100 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2101 al = SSL_AD_HANDSHAKE_FAILURE;
2102 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2103 SSL_R_DH_KEY_TOO_SMALL);
2106 if (s->s3->tmp.pkey != NULL) {
2107 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2108 ERR_R_INTERNAL_ERROR);
2112 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2114 if (s->s3->tmp.pkey == NULL) {
2115 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2119 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2121 EVP_PKEY_free(pkdh);
2124 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2125 DH_get0_key(dh, &r[2], NULL);
2128 #ifndef OPENSSL_NO_EC
2129 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2132 if (s->s3->tmp.pkey != NULL) {
2133 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2134 ERR_R_INTERNAL_ERROR);
2138 /* Get NID of appropriate shared curve */
2139 nid = tls1_shared_group(s, -2);
2140 curve_id = tls1_ec_nid2curve_id(nid);
2141 if (curve_id == 0) {
2142 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2143 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2146 s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
2147 /* Generate a new key for this curve */
2148 if (s->s3->tmp.pkey == NULL) {
2149 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2153 /* Encode the public key. */
2154 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2156 if (encodedlen == 0) {
2157 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2162 * We'll generate the serverKeyExchange message explicitly so we
2163 * can set these to NULLs
2170 #endif /* !OPENSSL_NO_EC */
2171 #ifndef OPENSSL_NO_SRP
2172 if (type & SSL_kSRP) {
2173 if ((s->srp_ctx.N == NULL) ||
2174 (s->srp_ctx.g == NULL) ||
2175 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2176 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2177 SSL_R_MISSING_SRP_PARAM);
2180 r[0] = s->srp_ctx.N;
2181 r[1] = s->srp_ctx.g;
2182 r[2] = s->srp_ctx.s;
2183 r[3] = s->srp_ctx.B;
2187 al = SSL_AD_HANDSHAKE_FAILURE;
2188 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2189 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2193 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2194 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2196 } else if (lu == NULL) {
2197 al = SSL_AD_DECODE_ERROR;
2201 #ifndef OPENSSL_NO_PSK
2202 if (type & SSL_PSK) {
2203 size_t len = (s->cert->psk_identity_hint == NULL)
2204 ? 0 : strlen(s->cert->psk_identity_hint);
2207 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2208 * checked this when we set the identity hint - but just in case
2210 if (len > PSK_MAX_IDENTITY_LEN
2211 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2213 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2214 ERR_R_INTERNAL_ERROR);
2220 for (i = 0; i < 4 && r[i] != NULL; i++) {
2221 unsigned char *binval;
2224 #ifndef OPENSSL_NO_SRP
2225 if ((i == 2) && (type & SSL_kSRP)) {
2226 res = WPACKET_start_sub_packet_u8(pkt);
2229 res = WPACKET_start_sub_packet_u16(pkt);
2232 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2233 ERR_R_INTERNAL_ERROR);
2237 #ifndef OPENSSL_NO_DH
2239 * for interoperability with some versions of the Microsoft TLS
2240 * stack, we need to zero pad the DHE pub key to the same length
2243 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2244 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2247 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2248 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2249 ERR_R_INTERNAL_ERROR);
2252 memset(binval, 0, len);
2256 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2257 || !WPACKET_close(pkt)) {
2258 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2259 ERR_R_INTERNAL_ERROR);
2263 BN_bn2bin(r[i], binval);
2266 #ifndef OPENSSL_NO_EC
2267 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2269 * We only support named (not generic) curves. In this situation, the
2270 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2271 * [1 byte length of encoded point], followed by the actual encoded
2274 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2275 || !WPACKET_put_bytes_u8(pkt, 0)
2276 || !WPACKET_put_bytes_u8(pkt, curve_id)
2277 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2278 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2279 ERR_R_INTERNAL_ERROR);
2282 OPENSSL_free(encodedPoint);
2283 encodedPoint = NULL;
2289 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2290 const EVP_MD *md = ssl_md(lu->hash_idx);
2291 unsigned char *sigbytes1, *sigbytes2;
2294 if (pkey == NULL || md == NULL) {
2295 /* Should never happen */
2296 al = SSL_AD_INTERNAL_ERROR;
2297 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2298 ERR_R_INTERNAL_ERROR);
2302 * n is the length of the params, they start at &(d[4]) and p
2303 * points to the space at the end.
2306 /* Get length of the parameters we have written above */
2307 if (!WPACKET_get_length(pkt, ¶mlen)) {
2308 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2309 ERR_R_INTERNAL_ERROR);
2312 /* send signature algorithm */
2313 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg))
2316 * Create the signature. We don't know the actual length of the sig
2317 * until after we've created it, so we reserve enough bytes for it
2318 * up front, and then properly allocate them in the WPACKET
2321 siglen = EVP_PKEY_size(pkey);
2322 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2323 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2324 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2325 ERR_R_INTERNAL_ERROR);
2328 if (lu->sig == EVP_PKEY_RSA_PSS) {
2329 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2330 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2331 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2336 if (EVP_DigestSignUpdate(md_ctx, &(s->s3->client_random[0]),
2337 SSL3_RANDOM_SIZE) <= 0
2338 || EVP_DigestSignUpdate(md_ctx, &(s->s3->server_random[0]),
2339 SSL3_RANDOM_SIZE) <= 0
2340 || EVP_DigestSignUpdate(md_ctx,
2341 s->init_buf->data + paramoffset,
2343 || EVP_DigestSignFinal(md_ctx, sigbytes1, &siglen) <= 0
2344 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2345 || sigbytes1 != sigbytes2) {
2346 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2347 ERR_R_INTERNAL_ERROR);
2352 EVP_MD_CTX_free(md_ctx);
2355 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2357 #ifndef OPENSSL_NO_DH
2358 EVP_PKEY_free(pkdh);
2360 #ifndef OPENSSL_NO_EC
2361 OPENSSL_free(encodedPoint);
2363 EVP_MD_CTX_free(md_ctx);
2367 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2370 STACK_OF(X509_NAME) *sk = NULL;
2372 /* get the list of acceptable cert types */
2373 if (!WPACKET_start_sub_packet_u8(pkt)
2374 || !ssl3_get_req_cert_type(s, pkt)
2375 || !WPACKET_close(pkt)) {
2376 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2380 if (SSL_USE_SIGALGS(s)) {
2381 const uint16_t *psigs;
2382 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2384 if (!WPACKET_start_sub_packet_u16(pkt)
2385 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2386 || !WPACKET_close(pkt)) {
2387 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2388 ERR_R_INTERNAL_ERROR);
2393 /* Start sub-packet for client CA list */
2394 if (!WPACKET_start_sub_packet_u16(pkt)) {
2395 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2399 sk = SSL_get_client_CA_list(s);
2401 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2402 unsigned char *namebytes;
2403 X509_NAME *name = sk_X509_NAME_value(sk, i);
2407 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2408 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2410 || i2d_X509_NAME(name, &namebytes) != namelen) {
2411 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2412 ERR_R_INTERNAL_ERROR);
2417 /* else no CA names */
2419 if (!WPACKET_close(pkt)) {
2420 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2424 s->s3->tmp.cert_request = 1;
2428 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2432 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
2434 #ifndef OPENSSL_NO_PSK
2435 unsigned char psk[PSK_MAX_PSK_LEN];
2437 PACKET psk_identity;
2439 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2440 *al = SSL_AD_DECODE_ERROR;
2441 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
2444 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2445 *al = SSL_AD_DECODE_ERROR;
2446 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
2449 if (s->psk_server_callback == NULL) {
2450 *al = SSL_AD_INTERNAL_ERROR;
2451 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
2455 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2456 *al = SSL_AD_INTERNAL_ERROR;
2457 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2461 psklen = s->psk_server_callback(s, s->session->psk_identity,
2464 if (psklen > PSK_MAX_PSK_LEN) {
2465 *al = SSL_AD_INTERNAL_ERROR;
2466 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2468 } else if (psklen == 0) {
2470 * PSK related to the given identity not found
2472 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2473 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2474 SSL_R_PSK_IDENTITY_NOT_FOUND);
2478 OPENSSL_free(s->s3->tmp.psk);
2479 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2480 OPENSSL_cleanse(psk, psklen);
2482 if (s->s3->tmp.psk == NULL) {
2483 *al = SSL_AD_INTERNAL_ERROR;
2484 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2488 s->s3->tmp.psklen = psklen;
2492 /* Should never happen */
2493 *al = SSL_AD_INTERNAL_ERROR;
2494 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2499 static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
2501 #ifndef OPENSSL_NO_RSA
2502 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2504 unsigned char decrypt_good, version_good;
2505 size_t j, padding_len;
2506 PACKET enc_premaster;
2508 unsigned char *rsa_decrypt = NULL;
2511 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2513 *al = SSL_AD_HANDSHAKE_FAILURE;
2514 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
2518 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2519 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2520 enc_premaster = *pkt;
2522 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2523 || PACKET_remaining(pkt) != 0) {
2524 *al = SSL_AD_DECODE_ERROR;
2525 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
2531 * We want to be sure that the plaintext buffer size makes it safe to
2532 * iterate over the entire size of a premaster secret
2533 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2534 * their ciphertext cannot accommodate a premaster secret anyway.
2536 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2537 *al = SSL_AD_INTERNAL_ERROR;
2538 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
2542 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2543 if (rsa_decrypt == NULL) {
2544 *al = SSL_AD_INTERNAL_ERROR;
2545 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
2550 * We must not leak whether a decryption failure occurs because of
2551 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2552 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2553 * generates a random premaster secret for the case that the decrypt
2554 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2557 if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
2561 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2562 * the timing-sensitive code below.
2564 /* TODO(size_t): Convert this function */
2565 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2566 PACKET_data(&enc_premaster),
2567 rsa_decrypt, rsa, RSA_NO_PADDING);
2568 if (decrypt_len < 0)
2571 /* Check the padding. See RFC 3447, section 7.2.2. */
2574 * The smallest padded premaster is 11 bytes of overhead. Small keys
2575 * are publicly invalid, so this may return immediately. This ensures
2576 * PS is at least 8 bytes.
2578 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2579 *al = SSL_AD_DECRYPT_ERROR;
2580 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
2584 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2585 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2586 constant_time_eq_int_8(rsa_decrypt[1], 2);
2587 for (j = 2; j < padding_len - 1; j++) {
2588 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2590 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2593 * If the version in the decrypted pre-master secret is correct then
2594 * version_good will be 0xff, otherwise it'll be zero. The
2595 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2596 * (http://eprint.iacr.org/2003/052/) exploits the version number
2597 * check as a "bad version oracle". Thus version checks are done in
2598 * constant time and are treated like any other decryption error.
2601 constant_time_eq_8(rsa_decrypt[padding_len],
2602 (unsigned)(s->client_version >> 8));
2604 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2605 (unsigned)(s->client_version & 0xff));
2608 * The premaster secret must contain the same version number as the
2609 * ClientHello to detect version rollback attacks (strangely, the
2610 * protocol does not offer such protection for DH ciphersuites).
2611 * However, buggy clients exist that send the negotiated protocol
2612 * version instead if the server does not support the requested
2613 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2616 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2617 unsigned char workaround_good;
2618 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2619 (unsigned)(s->version >> 8));
2621 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2622 (unsigned)(s->version & 0xff));
2623 version_good |= workaround_good;
2627 * Both decryption and version must be good for decrypt_good to
2628 * remain non-zero (0xff).
2630 decrypt_good &= version_good;
2633 * Now copy rand_premaster_secret over from p using
2634 * decrypt_good_mask. If decryption failed, then p does not
2635 * contain valid plaintext, however, a check above guarantees
2636 * it is still sufficiently large to read from.
2638 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2639 rsa_decrypt[padding_len + j] =
2640 constant_time_select_8(decrypt_good,
2641 rsa_decrypt[padding_len + j],
2642 rand_premaster_secret[j]);
2645 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2646 sizeof(rand_premaster_secret), 0)) {
2647 *al = SSL_AD_INTERNAL_ERROR;
2648 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2654 OPENSSL_free(rsa_decrypt);
2657 /* Should never happen */
2658 *al = SSL_AD_INTERNAL_ERROR;
2659 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2664 static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
2666 #ifndef OPENSSL_NO_DH
2667 EVP_PKEY *skey = NULL;
2671 const unsigned char *data;
2672 EVP_PKEY *ckey = NULL;
2675 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2676 *al = SSL_AD_HANDSHAKE_FAILURE;
2677 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
2678 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2681 skey = s->s3->tmp.pkey;
2683 *al = SSL_AD_HANDSHAKE_FAILURE;
2684 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2688 if (PACKET_remaining(pkt) == 0L) {
2689 *al = SSL_AD_HANDSHAKE_FAILURE;
2690 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2693 if (!PACKET_get_bytes(pkt, &data, i)) {
2694 /* We already checked we have enough data */
2695 *al = SSL_AD_INTERNAL_ERROR;
2696 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2699 ckey = EVP_PKEY_new();
2700 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2701 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
2704 cdh = EVP_PKEY_get0_DH(ckey);
2705 pub_key = BN_bin2bn(data, i, NULL);
2707 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2708 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2709 if (pub_key != NULL)
2714 if (ssl_derive(s, skey, ckey, 1) == 0) {
2715 *al = SSL_AD_INTERNAL_ERROR;
2716 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2721 EVP_PKEY_free(s->s3->tmp.pkey);
2722 s->s3->tmp.pkey = NULL;
2724 EVP_PKEY_free(ckey);
2727 /* Should never happen */
2728 *al = SSL_AD_INTERNAL_ERROR;
2729 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2734 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
2736 #ifndef OPENSSL_NO_EC
2737 EVP_PKEY *skey = s->s3->tmp.pkey;
2738 EVP_PKEY *ckey = NULL;
2741 if (PACKET_remaining(pkt) == 0L) {
2742 /* We don't support ECDH client auth */
2743 *al = SSL_AD_HANDSHAKE_FAILURE;
2744 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
2748 const unsigned char *data;
2751 * Get client's public key from encoded point in the
2752 * ClientKeyExchange message.
2755 /* Get encoded point length */
2756 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
2757 || PACKET_remaining(pkt) != 0) {
2758 *al = SSL_AD_DECODE_ERROR;
2759 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
2762 ckey = EVP_PKEY_new();
2763 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
2764 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
2767 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
2768 *al = SSL_AD_HANDSHAKE_FAILURE;
2769 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
2774 if (ssl_derive(s, skey, ckey, 1) == 0) {
2775 *al = SSL_AD_INTERNAL_ERROR;
2776 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2781 EVP_PKEY_free(s->s3->tmp.pkey);
2782 s->s3->tmp.pkey = NULL;
2784 EVP_PKEY_free(ckey);
2788 /* Should never happen */
2789 *al = SSL_AD_INTERNAL_ERROR;
2790 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2795 static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
2797 #ifndef OPENSSL_NO_SRP
2799 const unsigned char *data;
2801 if (!PACKET_get_net_2(pkt, &i)
2802 || !PACKET_get_bytes(pkt, &data, i)) {
2803 *al = SSL_AD_DECODE_ERROR;
2804 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
2807 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2808 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
2811 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
2812 *al = SSL_AD_ILLEGAL_PARAMETER;
2813 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
2816 OPENSSL_free(s->session->srp_username);
2817 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2818 if (s->session->srp_username == NULL) {
2819 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
2823 if (!srp_generate_server_master_secret(s)) {
2824 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2830 /* Should never happen */
2831 *al = SSL_AD_INTERNAL_ERROR;
2832 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2837 static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
2839 #ifndef OPENSSL_NO_GOST
2840 EVP_PKEY_CTX *pkey_ctx;
2841 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2842 unsigned char premaster_secret[32];
2843 const unsigned char *start;
2844 size_t outlen = 32, inlen;
2845 unsigned long alg_a;
2848 size_t sess_key_len;
2849 const unsigned char *data;
2852 /* Get our certificate private key */
2853 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2854 if (alg_a & SSL_aGOST12) {
2856 * New GOST ciphersuites have SSL_aGOST01 bit too
2858 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2860 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2863 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2865 } else if (alg_a & SSL_aGOST01) {
2866 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2869 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2870 if (pkey_ctx == NULL) {
2871 *al = SSL_AD_INTERNAL_ERROR;
2872 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
2875 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2876 *al = SSL_AD_INTERNAL_ERROR;
2877 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2881 * If client certificate is present and is of the same type, maybe
2882 * use it for key exchange. Don't mind errors from
2883 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2884 * client certificate for authorization only.
2886 client_pub_pkey = X509_get0_pubkey(s->session->peer);
2887 if (client_pub_pkey) {
2888 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2891 /* Decrypt session key */
2892 sess_key_len = PACKET_remaining(pkt);
2893 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2894 *al = SSL_AD_INTERNAL_ERROR;
2895 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2898 /* TODO(size_t): Convert this function */
2899 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
2900 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
2901 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
2902 *al = SSL_AD_DECODE_ERROR;
2903 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
2908 if (EVP_PKEY_decrypt
2909 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2910 *al = SSL_AD_DECODE_ERROR;
2911 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
2914 /* Generate master secret */
2915 if (!ssl_generate_master_secret(s, premaster_secret,
2916 sizeof(premaster_secret), 0)) {
2917 *al = SSL_AD_INTERNAL_ERROR;
2918 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2921 /* Check if pubkey from client certificate was used */
2922 if (EVP_PKEY_CTX_ctrl
2923 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2924 s->statem.no_cert_verify = 1;
2928 EVP_PKEY_CTX_free(pkey_ctx);
2931 /* Should never happen */
2932 *al = SSL_AD_INTERNAL_ERROR;
2933 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2938 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
2941 unsigned long alg_k;
2943 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2945 /* For PSK parse and retrieve identity, obtain PSK key */
2946 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
2949 if (alg_k & SSL_kPSK) {
2950 /* Identity extracted earlier: should be nothing left */
2951 if (PACKET_remaining(pkt) != 0) {
2952 al = SSL_AD_HANDSHAKE_FAILURE;
2953 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2954 SSL_R_LENGTH_MISMATCH);
2957 /* PSK handled by ssl_generate_master_secret */
2958 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2959 al = SSL_AD_INTERNAL_ERROR;
2960 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2963 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2964 if (!tls_process_cke_rsa(s, pkt, &al))
2966 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2967 if (!tls_process_cke_dhe(s, pkt, &al))
2969 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2970 if (!tls_process_cke_ecdhe(s, pkt, &al))
2972 } else if (alg_k & SSL_kSRP) {
2973 if (!tls_process_cke_srp(s, pkt, &al))
2975 } else if (alg_k & SSL_kGOST) {
2976 if (!tls_process_cke_gost(s, pkt, &al))
2979 al = SSL_AD_HANDSHAKE_FAILURE;
2980 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2981 SSL_R_UNKNOWN_CIPHER_TYPE);
2985 return MSG_PROCESS_CONTINUE_PROCESSING;
2988 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2989 #ifndef OPENSSL_NO_PSK
2990 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2991 s->s3->tmp.psk = NULL;
2993 ossl_statem_set_error(s);
2994 return MSG_PROCESS_ERROR;
2997 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
2999 #ifndef OPENSSL_NO_SCTP
3000 if (wst == WORK_MORE_A) {
3001 if (SSL_IS_DTLS(s)) {
3002 unsigned char sctpauthkey[64];
3003 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3005 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3008 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3009 sizeof(DTLS1_SCTP_AUTH_LABEL));
3011 if (SSL_export_keying_material(s, sctpauthkey,
3012 sizeof(sctpauthkey), labelbuffer,
3013 sizeof(labelbuffer), NULL, 0,
3015 ossl_statem_set_error(s);
3019 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3020 sizeof(sctpauthkey), sctpauthkey);
3025 if ((wst == WORK_MORE_B)
3027 && BIO_dgram_is_sctp(SSL_get_wbio(s))
3028 /* Are we renegotiating? */
3030 /* Are we going to skip the CertificateVerify? */
3031 && (s->session->peer == NULL || s->statem.no_cert_verify)
3032 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
3033 s->s3->in_read_app_data = 2;
3034 s->rwstate = SSL_READING;
3035 BIO_clear_retry_flags(SSL_get_rbio(s));
3036 BIO_set_retry_read(SSL_get_rbio(s));
3037 ossl_statem_set_sctp_read_sock(s, 1);
3040 ossl_statem_set_sctp_read_sock(s, 0);
3044 if (s->statem.no_cert_verify || !s->session->peer) {
3046 * No certificate verify or no peer certificate so we no longer need
3047 * the handshake_buffer
3049 if (!ssl3_digest_cached_records(s, 0)) {
3050 ossl_statem_set_error(s);
3053 return WORK_FINISHED_CONTINUE;
3055 if (!s->s3->handshake_buffer) {
3056 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3057 ERR_R_INTERNAL_ERROR);
3058 ossl_statem_set_error(s);
3062 * For sigalgs freeze the handshake buffer. If we support
3063 * extms we've done this already so this is a no-op
3065 if (!ssl3_digest_cached_records(s, 1)) {
3066 ossl_statem_set_error(s);
3071 return WORK_FINISHED_CONTINUE;
3074 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3076 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3078 unsigned long l, llen;
3079 const unsigned char *certstart, *certbytes;
3080 STACK_OF(X509) *sk = NULL;
3081 PACKET spkt, context;
3084 if ((sk = sk_X509_new_null()) == NULL) {
3085 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3089 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3090 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3091 || !PACKET_get_net_3(pkt, &llen)
3092 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3093 || PACKET_remaining(pkt) != 0) {
3094 al = SSL_AD_DECODE_ERROR;
3095 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3099 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3100 if (!PACKET_get_net_3(&spkt, &l)
3101 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3102 al = SSL_AD_DECODE_ERROR;
3103 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3104 SSL_R_CERT_LENGTH_MISMATCH);
3108 certstart = certbytes;
3109 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3111 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3114 if (certbytes != (certstart + l)) {
3115 al = SSL_AD_DECODE_ERROR;
3116 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3117 SSL_R_CERT_LENGTH_MISMATCH);
3121 if (SSL_IS_TLS13(s)) {
3122 RAW_EXTENSION *rawexts = NULL;
3125 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3126 al = SSL_AD_DECODE_ERROR;
3127 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_BAD_LENGTH);
3130 if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_CERTIFICATE,
3131 &rawexts, &al, NULL)
3132 || !tls_parse_all_extensions(s, EXT_TLS1_3_CERTIFICATE,
3133 rawexts, x, chainidx, &al)) {
3134 OPENSSL_free(rawexts);
3137 OPENSSL_free(rawexts);
3140 if (!sk_X509_push(sk, x)) {
3141 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3147 if (sk_X509_num(sk) <= 0) {
3148 /* TLS does not mind 0 certs returned */
3149 if (s->version == SSL3_VERSION) {
3150 al = SSL_AD_HANDSHAKE_FAILURE;
3151 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3152 SSL_R_NO_CERTIFICATES_RETURNED);
3155 /* Fail for TLS only if we required a certificate */
3156 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3157 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3158 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3159 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3160 al = SSL_AD_HANDSHAKE_FAILURE;
3163 /* No client certificate so digest cached records */
3164 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3169 i = ssl_verify_cert_chain(s, sk);
3171 al = ssl_verify_alarm_type(s->verify_result);
3172 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3173 SSL_R_CERTIFICATE_VERIFY_FAILED);
3177 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3178 al = SSL_AD_HANDSHAKE_FAILURE;
3181 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3183 al = SSL3_AD_HANDSHAKE_FAILURE;
3184 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3185 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3190 X509_free(s->session->peer);
3191 s->session->peer = sk_X509_shift(sk);
3192 s->session->verify_result = s->verify_result;
3194 sk_X509_pop_free(s->session->peer_chain, X509_free);
3195 s->session->peer_chain = sk;
3198 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3201 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3202 al = SSL_AD_INTERNAL_ERROR;
3203 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3208 * Inconsistency alert: cert_chain does *not* include the peer's own
3209 * certificate, while we do include it in statem_clnt.c
3213 /* Save the current hash state for when we receive the CertificateVerify */
3215 && !ssl_handshake_hash(s, s->cert_verify_hash,
3216 sizeof(s->cert_verify_hash),
3217 &s->cert_verify_hash_len)) {
3218 al = SSL_AD_INTERNAL_ERROR;
3219 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3223 ret = MSG_PROCESS_CONTINUE_READING;
3227 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3228 ossl_statem_set_error(s);
3231 sk_X509_pop_free(sk, X509_free);
3235 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3237 CERT_PKEY *cpk = s->s3->tmp.cert;
3238 int al = SSL_AD_INTERNAL_ERROR;
3241 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3246 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3247 * for the server Certificate message
3249 if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0))
3250 || !ssl3_output_cert_chain(s, pkt, cpk, &al)) {
3251 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3252 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3259 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3261 unsigned char *senc = NULL;
3262 EVP_CIPHER_CTX *ctx = NULL;
3263 HMAC_CTX *hctx = NULL;
3264 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3265 const unsigned char *const_p;
3266 int len, slen_full, slen, lenfinal;
3269 SSL_CTX *tctx = s->session_ctx;
3270 unsigned char iv[EVP_MAX_IV_LENGTH];
3271 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3272 int iv_len, al = SSL_AD_INTERNAL_ERROR;
3273 size_t macoffset, macendoffset;
3275 unsigned char age_add_c[sizeof(uint32_t)];
3279 if (SSL_IS_TLS13(s)) {
3280 if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0)
3282 s->session->ext.tick_age_add = age_add_u.age_add;
3285 /* get session encoding length */
3286 slen_full = i2d_SSL_SESSION(s->session, NULL);
3288 * Some length values are 16 bits, so forget it if session is too
3291 if (slen_full == 0 || slen_full > 0xFF00) {
3292 ossl_statem_set_error(s);
3295 senc = OPENSSL_malloc(slen_full);
3297 ossl_statem_set_error(s);
3301 ctx = EVP_CIPHER_CTX_new();
3302 hctx = HMAC_CTX_new();
3303 if (ctx == NULL || hctx == NULL) {
3304 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3309 if (!i2d_SSL_SESSION(s->session, &p))
3313 * create a fresh copy (not shared with other threads) to clean up
3316 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3319 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3321 slen = i2d_SSL_SESSION(sess, NULL);
3322 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3323 SSL_SESSION_free(sess);
3327 if (!i2d_SSL_SESSION(sess, &p)) {
3328 SSL_SESSION_free(sess);
3331 SSL_SESSION_free(sess);
3334 * Initialize HMAC and cipher contexts. If callback present it does
3335 * all the work otherwise use generated values from parent ctx.
3337 if (tctx->ext.ticket_key_cb) {
3338 /* if 0 is returned, write an empty ticket */
3339 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3344 /* Put timeout and length */
3345 if (!WPACKET_put_bytes_u32(pkt, 0)
3346 || !WPACKET_put_bytes_u16(pkt, 0)) {
3347 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3348 ERR_R_INTERNAL_ERROR);
3352 EVP_CIPHER_CTX_free(ctx);
3353 HMAC_CTX_free(hctx);
3358 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3360 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3362 iv_len = EVP_CIPHER_iv_length(cipher);
3363 if (RAND_bytes(iv, iv_len) <= 0)
3365 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
3366 tctx->ext.tick_aes_key, iv))
3368 if (!HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
3369 sizeof(tctx->ext.tick_hmac_key),
3370 EVP_sha256(), NULL))
3372 memcpy(key_name, tctx->ext.tick_key_name,
3373 sizeof(tctx->ext.tick_key_name));
3377 * Ticket lifetime hint (advisory only): We leave this unspecified
3378 * for resumed session (for simplicity), and guess that tickets for
3379 * new sessions will live as long as their sessions.
3381 if (!WPACKET_put_bytes_u32(pkt, s->hit ? 0 : s->session->timeout)
3383 && !WPACKET_put_bytes_u32(pkt, age_add_u.age_add))
3384 /* Now the actual ticket data */
3385 || !WPACKET_start_sub_packet_u16(pkt)
3386 || !WPACKET_get_total_written(pkt, &macoffset)
3387 /* Output key name */
3388 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3390 || !WPACKET_memcpy(pkt, iv, iv_len)
3391 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3393 /* Encrypt session data */
3394 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3395 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3396 || encdata1 != encdata2
3397 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3398 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3399 || encdata1 + len != encdata2
3400 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3401 || !WPACKET_get_total_written(pkt, &macendoffset)
3402 || !HMAC_Update(hctx,
3403 (unsigned char *)s->init_buf->data + macoffset,
3404 macendoffset - macoffset)
3405 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3406 || !HMAC_Final(hctx, macdata1, &hlen)
3407 || hlen > EVP_MAX_MD_SIZE
3408 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3409 || macdata1 != macdata2
3410 || !WPACKET_close(pkt)
3412 && !tls_construct_extensions(s, pkt,
3413 EXT_TLS1_3_NEW_SESSION_TICKET,
3415 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3418 EVP_CIPHER_CTX_free(ctx);
3419 HMAC_CTX_free(hctx);
3425 EVP_CIPHER_CTX_free(ctx);
3426 HMAC_CTX_free(hctx);
3427 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3432 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3433 * create a separate message. Returns 1 on success or 0 on failure.
3435 int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
3437 if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
3438 || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
3439 s->ext.ocsp.resp_len)) {
3440 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY, ERR_R_INTERNAL_ERROR);
3447 int tls_construct_cert_status(SSL *s, WPACKET *pkt)
3449 if (!tls_construct_cert_status_body(s, pkt)) {
3450 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3457 #ifndef OPENSSL_NO_NEXTPROTONEG
3459 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3460 * It sets the next_proto member in s if found
3462 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3464 PACKET next_proto, padding;
3465 size_t next_proto_len;
3468 * The payload looks like:
3470 * uint8 proto[proto_len];
3471 * uint8 padding_len;
3472 * uint8 padding[padding_len];
3474 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3475 || !PACKET_get_length_prefixed_1(pkt, &padding)
3476 || PACKET_remaining(pkt) > 0) {
3477 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3481 if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
3486 s->ext.npn_len = (unsigned char)next_proto_len;
3488 return MSG_PROCESS_CONTINUE_READING;
3490 ossl_statem_set_error(s);
3491 return MSG_PROCESS_ERROR;
3495 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
3499 if (!tls_construct_extensions(s, pkt, EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
3501 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3502 SSLerr(SSL_F_TLS_CONSTRUCT_ENCRYPTED_EXTENSIONS, ERR_R_INTERNAL_ERROR);
3503 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3510 #define SSLV2_CIPHER_LEN 3
3512 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
3513 PACKET *cipher_suites,
3514 STACK_OF(SSL_CIPHER) **skp,
3515 int sslv2format, int *al)
3517 const SSL_CIPHER *c;
3518 STACK_OF(SSL_CIPHER) *sk;
3520 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
3521 unsigned char cipher[SSLV2_CIPHER_LEN];
3523 s->s3->send_connection_binding = 0;
3525 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
3527 if (PACKET_remaining(cipher_suites) == 0) {
3528 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
3529 *al = SSL_AD_ILLEGAL_PARAMETER;
3533 if (PACKET_remaining(cipher_suites) % n != 0) {
3534 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3535 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3536 *al = SSL_AD_DECODE_ERROR;
3540 sk = sk_SSL_CIPHER_new_null();
3542 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3543 *al = SSL_AD_INTERNAL_ERROR;
3547 OPENSSL_free(s->s3->tmp.ciphers_raw);
3548 s->s3->tmp.ciphers_raw = NULL;
3549 s->s3->tmp.ciphers_rawlen = 0;
3552 size_t numciphers = PACKET_remaining(cipher_suites) / n;
3553 PACKET sslv2ciphers = *cipher_suites;
3554 unsigned int leadbyte;
3558 * We store the raw ciphers list in SSLv3+ format so we need to do some
3559 * preprocessing to convert the list first. If there are any SSLv2 only
3560 * ciphersuites with a non-zero leading byte then we are going to
3561 * slightly over allocate because we won't store those. But that isn't a
3564 raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
3565 s->s3->tmp.ciphers_raw = raw;
3567 *al = SSL_AD_INTERNAL_ERROR;
3570 for (s->s3->tmp.ciphers_rawlen = 0;
3571 PACKET_remaining(&sslv2ciphers) > 0;
3572 raw += TLS_CIPHER_LEN) {
3573 if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
3575 && !PACKET_copy_bytes(&sslv2ciphers, raw,
3578 && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
3579 *al = SSL_AD_INTERNAL_ERROR;
3580 OPENSSL_free(s->s3->tmp.ciphers_raw);
3581 s->s3->tmp.ciphers_raw = NULL;
3582 s->s3->tmp.ciphers_rawlen = 0;
3586 s->s3->tmp.ciphers_rawlen += TLS_CIPHER_LEN;
3588 } else if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
3589 &s->s3->tmp.ciphers_rawlen)) {
3590 *al = SSL_AD_INTERNAL_ERROR;
3594 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
3596 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
3597 * first byte set to zero, while true SSLv2 ciphers have a non-zero
3598 * first byte. We don't support any true SSLv2 ciphers, so skip them.
3600 if (sslv2format && cipher[0] != '\0')
3603 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3604 if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3605 (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3606 /* SCSV fatal if renegotiating */
3607 if (s->renegotiate) {
3608 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3609 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3610 *al = SSL_AD_HANDSHAKE_FAILURE;
3613 s->s3->send_connection_binding = 1;
3617 /* Check for TLS_FALLBACK_SCSV */
3618 if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3619 (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3621 * The SCSV indicates that the client previously tried a higher
3622 * version. Fail if the current version is an unexpected
3625 if (!ssl_check_version_downgrade(s)) {
3626 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3627 SSL_R_INAPPROPRIATE_FALLBACK);
3628 *al = SSL_AD_INAPPROPRIATE_FALLBACK;
3634 /* For SSLv2-compat, ignore leading 0-byte. */
3635 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher);
3637 if (!sk_SSL_CIPHER_push(sk, c)) {
3638 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3639 *al = SSL_AD_INTERNAL_ERROR;
3644 if (PACKET_remaining(cipher_suites) > 0) {
3645 *al = SSL_AD_INTERNAL_ERROR;
3646 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);
3653 sk_SSL_CIPHER_free(sk);
3657 static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt)
3659 int al = SSL_AD_INTERNAL_ERROR;
3662 * TODO(TLS1.3): Remove the DRAFT version before release
3663 * (should be s->version)
3665 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)
3666 || !tls_construct_extensions(s, pkt, EXT_TLS1_3_HELLO_RETRY_REQUEST,
3668 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR);
3669 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3673 /* Ditch the session. We'll create a new one next time around */
3674 SSL_SESSION_free(s->session);