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
);
68 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
69 * handshake state transitions when a TLSv1.3 server is reading messages from
70 * the client. The message type that the client has sent is provided in |mt|.
71 * The current state is in |s->statem.hand_state|.
73 * Return values are 1 for success (transition allowed) and 0 on error
74 * (transition not allowed)
76 static int ossl_statem_server13_read_transition(SSL
*s
, int mt
)
78 OSSL_STATEM
*st
= &s
->statem
;
81 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
82 * not negotiated TLSv1.3 yet, so that case is handled by
83 * ossl_statem_server_read_transition()
85 switch (st
->hand_state
) {
89 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
90 if (mt
== SSL3_MT_CLIENT_HELLO
) {
91 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
96 case TLS_ST_EARLY_DATA
:
97 case TLS_ST_SW_FINISHED
:
98 if (s
->s3
->tmp
.cert_request
) {
99 if (mt
== SSL3_MT_CERTIFICATE
) {
100 st
->hand_state
= TLS_ST_SR_CERT
;
104 if (mt
== SSL3_MT_FINISHED
) {
105 st
->hand_state
= TLS_ST_SR_FINISHED
;
112 if (s
->session
->peer
== NULL
) {
113 if (mt
== SSL3_MT_FINISHED
) {
114 st
->hand_state
= TLS_ST_SR_FINISHED
;
118 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
119 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
125 case TLS_ST_SR_CERT_VRFY
:
126 if (mt
== SSL3_MT_FINISHED
) {
127 st
->hand_state
= TLS_ST_SR_FINISHED
;
134 * Its never ok to start processing handshake messages in the middle of
135 * early data (i.e. before we've received the end of early data alert)
137 if (s
->early_data_state
== SSL_EARLY_DATA_READING
)
139 if (mt
== SSL3_MT_KEY_UPDATE
) {
140 st
->hand_state
= TLS_ST_SR_KEY_UPDATE
;
146 /* No valid transition found */
147 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL3_AD_UNEXPECTED_MESSAGE
);
148 SSLerr(SSL_F_OSSL_STATEM_SERVER13_READ_TRANSITION
,
149 SSL_R_UNEXPECTED_MESSAGE
);
154 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
155 * handshake state transitions when the server is reading messages from the
156 * client. The message type that the client has sent is provided in |mt|. The
157 * current state is in |s->statem.hand_state|.
159 * Return values are 1 for success (transition allowed) and 0 on error
160 * (transition not allowed)
162 int ossl_statem_server_read_transition(SSL
*s
, int mt
)
164 OSSL_STATEM
*st
= &s
->statem
;
166 if (SSL_IS_TLS13(s
)) {
167 if (!ossl_statem_server13_read_transition(s
, mt
))
172 switch (st
->hand_state
) {
178 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
179 if (mt
== SSL3_MT_CLIENT_HELLO
) {
180 st
->hand_state
= TLS_ST_SR_CLNT_HELLO
;
185 case TLS_ST_SW_SRVR_DONE
:
187 * If we get a CKE message after a ServerDone then either
188 * 1) We didn't request a Certificate
190 * 2) If we did request one then
191 * a) We allow no Certificate to be returned
193 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
194 * list if we requested a certificate)
196 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
197 if (s
->s3
->tmp
.cert_request
) {
198 if (s
->version
== SSL3_VERSION
) {
199 if ((s
->verify_mode
& SSL_VERIFY_PEER
)
200 && (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
202 * This isn't an unexpected message as such - we're just
203 * not going to accept it because we require a client
206 ssl3_send_alert(s
, SSL3_AL_FATAL
,
207 SSL3_AD_HANDSHAKE_FAILURE
);
208 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
,
209 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
212 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
216 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
219 } else if (s
->s3
->tmp
.cert_request
) {
220 if (mt
== SSL3_MT_CERTIFICATE
) {
221 st
->hand_state
= TLS_ST_SR_CERT
;
228 if (mt
== SSL3_MT_CLIENT_KEY_EXCHANGE
) {
229 st
->hand_state
= TLS_ST_SR_KEY_EXCH
;
234 case TLS_ST_SR_KEY_EXCH
:
236 * We should only process a CertificateVerify message if we have
237 * received a Certificate from the client. If so then |s->session->peer|
238 * will be non NULL. In some instances a CertificateVerify message is
239 * not required even if the peer has sent a Certificate (e.g. such as in
240 * the case of static DH). In that case |st->no_cert_verify| should be
243 if (s
->session
->peer
== NULL
|| st
->no_cert_verify
) {
244 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
246 * For the ECDH ciphersuites when the client sends its ECDH
247 * pub key in a certificate, the CertificateVerify message is
248 * not sent. Also for GOST ciphersuites when the client uses
249 * its key from the certificate for key exchange.
251 st
->hand_state
= TLS_ST_SR_CHANGE
;
255 if (mt
== SSL3_MT_CERTIFICATE_VERIFY
) {
256 st
->hand_state
= TLS_ST_SR_CERT_VRFY
;
262 case TLS_ST_SR_CERT_VRFY
:
263 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
264 st
->hand_state
= TLS_ST_SR_CHANGE
;
269 case TLS_ST_SR_CHANGE
:
270 #ifndef OPENSSL_NO_NEXTPROTONEG
271 if (s
->s3
->npn_seen
) {
272 if (mt
== SSL3_MT_NEXT_PROTO
) {
273 st
->hand_state
= TLS_ST_SR_NEXT_PROTO
;
278 if (mt
== SSL3_MT_FINISHED
) {
279 st
->hand_state
= TLS_ST_SR_FINISHED
;
282 #ifndef OPENSSL_NO_NEXTPROTONEG
287 #ifndef OPENSSL_NO_NEXTPROTONEG
288 case TLS_ST_SR_NEXT_PROTO
:
289 if (mt
== SSL3_MT_FINISHED
) {
290 st
->hand_state
= TLS_ST_SR_FINISHED
;
296 case TLS_ST_SW_FINISHED
:
297 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
298 st
->hand_state
= TLS_ST_SR_CHANGE
;
305 /* No valid transition found */
306 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL3_AD_UNEXPECTED_MESSAGE
);
307 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION
, SSL_R_UNEXPECTED_MESSAGE
);
312 * Should we send a ServerKeyExchange message?
314 * Valid return values are:
318 static int send_server_key_exchange(SSL
*s
)
320 unsigned long alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
323 * only send a ServerKeyExchange if DH or fortezza but we have a
324 * sign only certificate PSK: may send PSK identity hints For
325 * ECC ciphersuites, we send a serverKeyExchange message only if
326 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
327 * the server certificate contains the server's public key for
330 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
)
332 * PSK: send ServerKeyExchange if PSK identity hint if
335 #ifndef OPENSSL_NO_PSK
336 /* Only send SKE if we have identity hint for plain PSK */
337 || ((alg_k
& (SSL_kPSK
| SSL_kRSAPSK
))
338 && s
->cert
->psk_identity_hint
)
339 /* For other PSK always send SKE */
340 || (alg_k
& (SSL_PSK
& (SSL_kDHEPSK
| SSL_kECDHEPSK
)))
342 #ifndef OPENSSL_NO_SRP
343 /* SRP: send ServerKeyExchange */
344 || (alg_k
& SSL_kSRP
)
354 * Should we send a CertificateRequest message?
356 * Valid return values are:
360 static int send_certificate_request(SSL
*s
)
363 /* don't request cert unless asked for it: */
364 s
->verify_mode
& SSL_VERIFY_PEER
366 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
367 * during re-negotiation:
369 && (s
->s3
->tmp
.finish_md_len
== 0 ||
370 !(s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
))
372 * never request cert in anonymous ciphersuites (see
373 * section "Certificate request" in SSL 3 drafts and in
376 && (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
)
378 * ... except when the application insists on
379 * verification (against the specs, but statem_clnt.c accepts
382 || (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
383 /* don't request certificate for SRP auth */
384 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
386 * With normal PSK Certificates and Certificate Requests
389 && !(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aPSK
)) {
397 * ossl_statem_server13_write_transition() works out what handshake state to
398 * move to next when a TLSv1.3 server is writing messages to be sent to the
401 static WRITE_TRAN
ossl_statem_server13_write_transition(SSL
*s
)
403 OSSL_STATEM
*st
= &s
->statem
;
406 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
407 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
410 switch (st
->hand_state
) {
412 /* Shouldn't happen */
413 return WRITE_TRAN_ERROR
;
416 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
417 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
418 return WRITE_TRAN_CONTINUE
;
420 /* Try to read from the client instead */
421 return WRITE_TRAN_FINISHED
;
423 case TLS_ST_SR_CLNT_HELLO
:
424 if (s
->hello_retry_request
)
425 st
->hand_state
= TLS_ST_SW_HELLO_RETRY_REQUEST
;
427 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
428 return WRITE_TRAN_CONTINUE
;
430 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
431 return WRITE_TRAN_FINISHED
;
433 case TLS_ST_SW_SRVR_HELLO
:
434 st
->hand_state
= TLS_ST_SW_ENCRYPTED_EXTENSIONS
;
435 return WRITE_TRAN_CONTINUE
;
437 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
439 st
->hand_state
= TLS_ST_SW_FINISHED
;
440 else if (send_certificate_request(s
))
441 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
443 st
->hand_state
= TLS_ST_SW_CERT
;
445 return WRITE_TRAN_CONTINUE
;
447 case TLS_ST_SW_CERT_REQ
:
448 st
->hand_state
= TLS_ST_SW_CERT
;
449 return WRITE_TRAN_CONTINUE
;
452 st
->hand_state
= TLS_ST_SW_CERT_VRFY
;
453 return WRITE_TRAN_CONTINUE
;
455 case TLS_ST_SW_CERT_VRFY
:
456 st
->hand_state
= TLS_ST_SW_FINISHED
;
457 return WRITE_TRAN_CONTINUE
;
459 case TLS_ST_SW_FINISHED
:
460 st
->hand_state
= TLS_ST_EARLY_DATA
;
461 return WRITE_TRAN_CONTINUE
;
463 case TLS_ST_EARLY_DATA
:
464 return WRITE_TRAN_FINISHED
;
466 case TLS_ST_SR_FINISHED
:
468 * Technically we have finished the handshake at this point, but we're
469 * going to remain "in_init" for now and write out the session ticket
471 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
472 * and give the application the opportunity to delay sending the
475 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
476 return WRITE_TRAN_CONTINUE
;
478 case TLS_ST_SR_KEY_UPDATE
:
479 if (s
->key_update
!= SSL_KEY_UPDATE_NONE
) {
480 st
->hand_state
= TLS_ST_SW_KEY_UPDATE
;
481 return WRITE_TRAN_CONTINUE
;
485 case TLS_ST_SW_KEY_UPDATE
:
486 case TLS_ST_SW_SESSION_TICKET
:
487 st
->hand_state
= TLS_ST_OK
;
488 return WRITE_TRAN_CONTINUE
;
493 * ossl_statem_server_write_transition() works out what handshake state to move
494 * to next when the server is writing messages to be sent to the client.
496 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
)
498 OSSL_STATEM
*st
= &s
->statem
;
501 * Note that before the ClientHello we don't know what version we are going
502 * to negotiate yet, so we don't take this branch until later
506 return ossl_statem_server13_write_transition(s
);
508 switch (st
->hand_state
) {
510 /* Shouldn't happen */
511 return WRITE_TRAN_ERROR
;
514 if (st
->request_state
== TLS_ST_SW_HELLO_REQ
) {
515 /* We must be trying to renegotiate */
516 st
->hand_state
= TLS_ST_SW_HELLO_REQ
;
517 st
->request_state
= TLS_ST_BEFORE
;
518 return WRITE_TRAN_CONTINUE
;
520 /* Must be an incoming ClientHello */
521 if (!tls_setup_handshake(s
)) {
522 ossl_statem_set_error(s
);
523 return WRITE_TRAN_ERROR
;
528 /* Just go straight to trying to read from the client */
529 return WRITE_TRAN_FINISHED
;
531 case TLS_ST_SW_HELLO_REQ
:
532 st
->hand_state
= TLS_ST_OK
;
533 return WRITE_TRAN_CONTINUE
;
535 case TLS_ST_SR_CLNT_HELLO
:
536 if (SSL_IS_DTLS(s
) && !s
->d1
->cookie_verified
537 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
))
538 st
->hand_state
= DTLS_ST_SW_HELLO_VERIFY_REQUEST
;
540 st
->hand_state
= TLS_ST_SW_SRVR_HELLO
;
541 return WRITE_TRAN_CONTINUE
;
543 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
544 return WRITE_TRAN_FINISHED
;
546 case TLS_ST_SW_SRVR_HELLO
:
548 if (s
->ext
.ticket_expected
)
549 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
551 st
->hand_state
= TLS_ST_SW_CHANGE
;
553 /* Check if it is anon DH or anon ECDH, */
554 /* normal PSK or SRP */
555 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
&
556 (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
557 st
->hand_state
= TLS_ST_SW_CERT
;
558 } else if (send_server_key_exchange(s
)) {
559 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
560 } else if (send_certificate_request(s
)) {
561 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
563 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
566 return WRITE_TRAN_CONTINUE
;
569 if (s
->ext
.status_expected
) {
570 st
->hand_state
= TLS_ST_SW_CERT_STATUS
;
571 return WRITE_TRAN_CONTINUE
;
575 case TLS_ST_SW_CERT_STATUS
:
576 if (send_server_key_exchange(s
)) {
577 st
->hand_state
= TLS_ST_SW_KEY_EXCH
;
578 return WRITE_TRAN_CONTINUE
;
582 case TLS_ST_SW_KEY_EXCH
:
583 if (send_certificate_request(s
)) {
584 st
->hand_state
= TLS_ST_SW_CERT_REQ
;
585 return WRITE_TRAN_CONTINUE
;
589 case TLS_ST_SW_CERT_REQ
:
590 st
->hand_state
= TLS_ST_SW_SRVR_DONE
;
591 return WRITE_TRAN_CONTINUE
;
593 case TLS_ST_SW_SRVR_DONE
:
594 return WRITE_TRAN_FINISHED
;
596 case TLS_ST_SR_FINISHED
:
598 st
->hand_state
= TLS_ST_OK
;
599 return WRITE_TRAN_CONTINUE
;
600 } else if (s
->ext
.ticket_expected
) {
601 st
->hand_state
= TLS_ST_SW_SESSION_TICKET
;
603 st
->hand_state
= TLS_ST_SW_CHANGE
;
605 return WRITE_TRAN_CONTINUE
;
607 case TLS_ST_SW_SESSION_TICKET
:
608 st
->hand_state
= TLS_ST_SW_CHANGE
;
609 return WRITE_TRAN_CONTINUE
;
611 case TLS_ST_SW_CHANGE
:
612 st
->hand_state
= TLS_ST_SW_FINISHED
;
613 return WRITE_TRAN_CONTINUE
;
615 case TLS_ST_SW_FINISHED
:
617 return WRITE_TRAN_FINISHED
;
619 st
->hand_state
= TLS_ST_OK
;
620 return WRITE_TRAN_CONTINUE
;
625 * Perform any pre work that needs to be done prior to sending a message from
626 * the server to the client.
628 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
)
630 OSSL_STATEM
*st
= &s
->statem
;
632 switch (st
->hand_state
) {
634 /* No pre work to be done */
637 case TLS_ST_SW_HELLO_REQ
:
640 dtls1_clear_sent_buffer(s
);
643 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
645 if (SSL_IS_DTLS(s
)) {
646 dtls1_clear_sent_buffer(s
);
647 /* We don't buffer this message so don't use the timer */
652 case TLS_ST_SW_SRVR_HELLO
:
653 if (SSL_IS_DTLS(s
)) {
655 * Messages we write from now on should be bufferred and
656 * retransmitted if necessary, so we need to use the timer now
662 case TLS_ST_SW_SRVR_DONE
:
663 #ifndef OPENSSL_NO_SCTP
664 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
)))
665 return dtls_wait_for_dry(s
);
667 return WORK_FINISHED_CONTINUE
;
669 case TLS_ST_SW_SESSION_TICKET
:
670 if (SSL_IS_TLS13(s
)) {
672 * Actually this is the end of the handshake, but we're going
673 * straight into writing the session ticket out. So we finish off
674 * the handshake, but keep the various buffers active.
676 return tls_finish_handshake(s
, wst
, 0);
677 } if (SSL_IS_DTLS(s
)) {
679 * We're into the last flight. We don't retransmit the last flight
680 * unless we need to, so we don't use the timer
686 case TLS_ST_SW_CHANGE
:
687 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
688 if (!s
->method
->ssl3_enc
->setup_key_block(s
)) {
689 ossl_statem_set_error(s
);
692 if (SSL_IS_DTLS(s
)) {
694 * We're into the last flight. We don't retransmit the last flight
695 * unless we need to, so we don't use the timer. This might have
696 * already been set to 0 if we sent a NewSessionTicket message,
697 * but we'll set it again here in case we didn't.
701 return WORK_FINISHED_CONTINUE
;
703 case TLS_ST_EARLY_DATA
:
704 if (s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
)
705 return WORK_FINISHED_CONTINUE
;
709 return tls_finish_handshake(s
, wst
, 1);
712 return WORK_FINISHED_CONTINUE
;
716 * Perform any work that needs to be done after sending a message from the
717 * server to the client.
719 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
)
721 OSSL_STATEM
*st
= &s
->statem
;
725 switch (st
->hand_state
) {
727 /* No post work to be done */
730 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
731 if (statem_flush(s
) != 1)
735 case TLS_ST_SW_HELLO_REQ
:
736 if (statem_flush(s
) != 1)
738 if (!ssl3_init_finished_mac(s
)) {
739 ossl_statem_set_error(s
);
744 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
745 if (statem_flush(s
) != 1)
747 /* HelloVerifyRequest resets Finished MAC */
748 if (s
->version
!= DTLS1_BAD_VER
&& !ssl3_init_finished_mac(s
)) {
749 ossl_statem_set_error(s
);
753 * The next message should be another ClientHello which we need to
754 * treat like it was the first packet
759 case TLS_ST_SW_SRVR_HELLO
:
760 #ifndef OPENSSL_NO_SCTP
761 if (SSL_IS_DTLS(s
) && s
->hit
) {
762 unsigned char sctpauthkey
[64];
763 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
766 * Add new shared key for SCTP-Auth, will be ignored if no
769 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
770 sizeof(DTLS1_SCTP_AUTH_LABEL
));
772 if (SSL_export_keying_material(s
, sctpauthkey
,
773 sizeof(sctpauthkey
), labelbuffer
,
774 sizeof(labelbuffer
), NULL
, 0,
776 ossl_statem_set_error(s
);
780 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
781 sizeof(sctpauthkey
), sctpauthkey
);
785 * TODO(TLS1.3): This actually causes a problem. We don't yet know
786 * whether the next record we are going to receive is an unencrypted
787 * alert, or an encrypted handshake message. We're going to need
788 * something clever in the record layer for this.
790 if (SSL_IS_TLS13(s
)) {
791 if (!s
->method
->ssl3_enc
->setup_key_block(s
)
792 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
793 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
796 if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
797 && !s
->method
->ssl3_enc
->change_cipher_state(s
,
798 SSL3_CC_HANDSHAKE
|SSL3_CHANGE_CIPHER_SERVER_READ
))
803 case TLS_ST_SW_CHANGE
:
804 #ifndef OPENSSL_NO_SCTP
805 if (SSL_IS_DTLS(s
) && !s
->hit
) {
807 * Change to new shared key of SCTP-Auth, will be ignored if
810 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
814 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
815 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
817 ossl_statem_set_error(s
);
822 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
825 case TLS_ST_SW_SRVR_DONE
:
826 if (statem_flush(s
) != 1)
830 case TLS_ST_SW_FINISHED
:
831 if (statem_flush(s
) != 1)
833 #ifndef OPENSSL_NO_SCTP
834 if (SSL_IS_DTLS(s
) && s
->hit
) {
836 * Change to new shared key of SCTP-Auth, will be ignored if
839 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
843 if (SSL_IS_TLS13(s
)) {
844 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
845 s
->master_secret
, s
->handshake_secret
, 0,
846 &s
->session
->master_key_length
)
847 || !s
->method
->ssl3_enc
->change_cipher_state(s
,
848 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_WRITE
))
853 case TLS_ST_SW_KEY_UPDATE
:
854 if (statem_flush(s
) != 1)
856 if (!tls13_update_key(s
, 1))
860 case TLS_ST_SW_SESSION_TICKET
:
861 if (SSL_IS_TLS13(s
) && statem_flush(s
) != 1)
866 return WORK_FINISHED_CONTINUE
;
870 * Get the message construction function and message type for sending from the
873 * Valid return values are:
877 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
878 confunc_f
*confunc
, int *mt
)
880 OSSL_STATEM
*st
= &s
->statem
;
882 switch (st
->hand_state
) {
884 /* Shouldn't happen */
887 case TLS_ST_SW_CHANGE
:
889 *confunc
= dtls_construct_change_cipher_spec
;
891 *confunc
= tls_construct_change_cipher_spec
;
892 *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
895 case DTLS_ST_SW_HELLO_VERIFY_REQUEST
:
896 *confunc
= dtls_construct_hello_verify_request
;
897 *mt
= DTLS1_MT_HELLO_VERIFY_REQUEST
;
900 case TLS_ST_SW_HELLO_REQ
:
901 /* No construction function needed */
903 *mt
= SSL3_MT_HELLO_REQUEST
;
906 case TLS_ST_SW_SRVR_HELLO
:
907 *confunc
= tls_construct_server_hello
;
908 *mt
= SSL3_MT_SERVER_HELLO
;
912 *confunc
= tls_construct_server_certificate
;
913 *mt
= SSL3_MT_CERTIFICATE
;
916 case TLS_ST_SW_CERT_VRFY
:
917 *confunc
= tls_construct_cert_verify
;
918 *mt
= SSL3_MT_CERTIFICATE_VERIFY
;
922 case TLS_ST_SW_KEY_EXCH
:
923 *confunc
= tls_construct_server_key_exchange
;
924 *mt
= SSL3_MT_SERVER_KEY_EXCHANGE
;
927 case TLS_ST_SW_CERT_REQ
:
928 *confunc
= tls_construct_certificate_request
;
929 *mt
= SSL3_MT_CERTIFICATE_REQUEST
;
932 case TLS_ST_SW_SRVR_DONE
:
933 *confunc
= tls_construct_server_done
;
934 *mt
= SSL3_MT_SERVER_DONE
;
937 case TLS_ST_SW_SESSION_TICKET
:
938 *confunc
= tls_construct_new_session_ticket
;
939 *mt
= SSL3_MT_NEWSESSION_TICKET
;
942 case TLS_ST_SW_CERT_STATUS
:
943 *confunc
= tls_construct_cert_status
;
944 *mt
= SSL3_MT_CERTIFICATE_STATUS
;
947 case TLS_ST_SW_FINISHED
:
948 *confunc
= tls_construct_finished
;
949 *mt
= SSL3_MT_FINISHED
;
952 case TLS_ST_EARLY_DATA
:
957 case TLS_ST_SW_ENCRYPTED_EXTENSIONS
:
958 *confunc
= tls_construct_encrypted_extensions
;
959 *mt
= SSL3_MT_ENCRYPTED_EXTENSIONS
;
962 case TLS_ST_SW_HELLO_RETRY_REQUEST
:
963 *confunc
= tls_construct_hello_retry_request
;
964 *mt
= SSL3_MT_HELLO_RETRY_REQUEST
;
967 case TLS_ST_SW_KEY_UPDATE
:
968 *confunc
= tls_construct_key_update
;
969 *mt
= SSL3_MT_KEY_UPDATE
;
977 * Maximum size (excluding the Handshake header) of a ClientHello message,
978 * calculated as follows:
980 * 2 + # client_version
981 * 32 + # only valid length for random
982 * 1 + # length of session_id
983 * 32 + # maximum size for session_id
984 * 2 + # length of cipher suites
985 * 2^16-2 + # maximum length of cipher suites array
986 * 1 + # length of compression_methods
987 * 2^8-1 + # maximum length of compression methods
988 * 2 + # length of extensions
989 * 2^16-1 # maximum length of extensions
991 #define CLIENT_HELLO_MAX_LENGTH 131396
993 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
994 #define NEXT_PROTO_MAX_LENGTH 514
997 * Returns the maximum allowed length for the current message that we are
998 * reading. Excludes the message header.
1000 size_t ossl_statem_server_max_message_size(SSL
*s
)
1002 OSSL_STATEM
*st
= &s
->statem
;
1004 switch (st
->hand_state
) {
1006 /* Shouldn't happen */
1009 case TLS_ST_SR_CLNT_HELLO
:
1010 return CLIENT_HELLO_MAX_LENGTH
;
1012 case TLS_ST_SR_CERT
:
1013 return s
->max_cert_list
;
1015 case TLS_ST_SR_KEY_EXCH
:
1016 return CLIENT_KEY_EXCH_MAX_LENGTH
;
1018 case TLS_ST_SR_CERT_VRFY
:
1019 return SSL3_RT_MAX_PLAIN_LENGTH
;
1021 #ifndef OPENSSL_NO_NEXTPROTONEG
1022 case TLS_ST_SR_NEXT_PROTO
:
1023 return NEXT_PROTO_MAX_LENGTH
;
1026 case TLS_ST_SR_CHANGE
:
1027 return CCS_MAX_LENGTH
;
1029 case TLS_ST_SR_FINISHED
:
1030 return FINISHED_MAX_LENGTH
;
1032 case TLS_ST_SR_KEY_UPDATE
:
1033 return KEY_UPDATE_MAX_LENGTH
;
1038 * Process a message that the server has received from the client.
1040 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
)
1042 OSSL_STATEM
*st
= &s
->statem
;
1044 switch (st
->hand_state
) {
1046 /* Shouldn't happen */
1047 return MSG_PROCESS_ERROR
;
1049 case TLS_ST_SR_CLNT_HELLO
:
1050 return tls_process_client_hello(s
, pkt
);
1052 case TLS_ST_SR_CERT
:
1053 return tls_process_client_certificate(s
, pkt
);
1055 case TLS_ST_SR_KEY_EXCH
:
1056 return tls_process_client_key_exchange(s
, pkt
);
1058 case TLS_ST_SR_CERT_VRFY
:
1059 return tls_process_cert_verify(s
, pkt
);
1061 #ifndef OPENSSL_NO_NEXTPROTONEG
1062 case TLS_ST_SR_NEXT_PROTO
:
1063 return tls_process_next_proto(s
, pkt
);
1066 case TLS_ST_SR_CHANGE
:
1067 return tls_process_change_cipher_spec(s
, pkt
);
1069 case TLS_ST_SR_FINISHED
:
1070 return tls_process_finished(s
, pkt
);
1072 case TLS_ST_SR_KEY_UPDATE
:
1073 return tls_process_key_update(s
, pkt
);
1079 * Perform any further processing required following the receipt of a message
1082 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
)
1084 OSSL_STATEM
*st
= &s
->statem
;
1086 switch (st
->hand_state
) {
1088 /* Shouldn't happen */
1091 case TLS_ST_SR_CLNT_HELLO
:
1092 return tls_post_process_client_hello(s
, wst
);
1094 case TLS_ST_SR_KEY_EXCH
:
1095 return tls_post_process_client_key_exchange(s
, wst
);
1097 case TLS_ST_SR_CERT_VRFY
:
1098 #ifndef OPENSSL_NO_SCTP
1099 if ( /* Is this SCTP? */
1100 BIO_dgram_is_sctp(SSL_get_wbio(s
))
1101 /* Are we renegotiating? */
1102 && s
->renegotiate
&& BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s
))) {
1103 s
->s3
->in_read_app_data
= 2;
1104 s
->rwstate
= SSL_READING
;
1105 BIO_clear_retry_flags(SSL_get_rbio(s
));
1106 BIO_set_retry_read(SSL_get_rbio(s
));
1107 ossl_statem_set_sctp_read_sock(s
, 1);
1110 ossl_statem_set_sctp_read_sock(s
, 0);
1113 return WORK_FINISHED_CONTINUE
;
1115 return WORK_FINISHED_CONTINUE
;
1118 int ossl_statem_finish_early_data(SSL
*s
)
1120 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
1121 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_SERVER_READ
))
1127 #ifndef OPENSSL_NO_SRP
1128 static int ssl_check_srp_ext_ClientHello(SSL
*s
, int *al
)
1130 int ret
= SSL_ERROR_NONE
;
1132 *al
= SSL_AD_UNRECOGNIZED_NAME
;
1134 if ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) &&
1135 (s
->srp_ctx
.TLS_ext_srp_username_callback
!= NULL
)) {
1136 if (s
->srp_ctx
.login
== NULL
) {
1138 * RFC 5054 says SHOULD reject, we do so if There is no srp
1141 ret
= SSL3_AL_FATAL
;
1142 *al
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
1144 ret
= SSL_srp_server_param_with_username(s
, al
);
1151 int dtls_raw_hello_verify_request(WPACKET
*pkt
, unsigned char *cookie
,
1154 /* Always use DTLS 1.0 version: see RFC 6347 */
1155 if (!WPACKET_put_bytes_u16(pkt
, DTLS1_VERSION
)
1156 || !WPACKET_sub_memcpy_u8(pkt
, cookie
, cookie_len
))
1162 int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
)
1164 unsigned int cookie_leni
;
1165 if (s
->ctx
->app_gen_cookie_cb
== NULL
||
1166 s
->ctx
->app_gen_cookie_cb(s
, s
->d1
->cookie
,
1167 &cookie_leni
) == 0 ||
1168 cookie_leni
> 255) {
1169 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
,
1170 SSL_R_COOKIE_GEN_CALLBACK_FAILURE
);
1173 s
->d1
->cookie_len
= cookie_leni
;
1175 if (!dtls_raw_hello_verify_request(pkt
, s
->d1
->cookie
,
1176 s
->d1
->cookie_len
)) {
1177 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST
, ERR_R_INTERNAL_ERROR
);
1184 #ifndef OPENSSL_NO_EC
1186 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1187 * SecureTransport using the TLS extension block in |hello|.
1188 * Safari, since 10.6, sends exactly these extensions, in this order:
1193 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1194 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1195 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1196 * 10.8..10.8.3 (which don't work).
1198 static void ssl_check_for_safari(SSL
*s
, const CLIENTHELLO_MSG
*hello
)
1200 static const unsigned char kSafariExtensionsBlock
[] = {
1201 0x00, 0x0a, /* elliptic_curves extension */
1202 0x00, 0x08, /* 8 bytes */
1203 0x00, 0x06, /* 6 bytes of curve ids */
1204 0x00, 0x17, /* P-256 */
1205 0x00, 0x18, /* P-384 */
1206 0x00, 0x19, /* P-521 */
1208 0x00, 0x0b, /* ec_point_formats */
1209 0x00, 0x02, /* 2 bytes */
1210 0x01, /* 1 point format */
1211 0x00, /* uncompressed */
1212 /* The following is only present in TLS 1.2 */
1213 0x00, 0x0d, /* signature_algorithms */
1214 0x00, 0x0c, /* 12 bytes */
1215 0x00, 0x0a, /* 10 bytes */
1216 0x05, 0x01, /* SHA-384/RSA */
1217 0x04, 0x01, /* SHA-256/RSA */
1218 0x02, 0x01, /* SHA-1/RSA */
1219 0x04, 0x03, /* SHA-256/ECDSA */
1220 0x02, 0x03, /* SHA-1/ECDSA */
1222 /* Length of the common prefix (first two extensions). */
1223 static const size_t kSafariCommonExtensionsLength
= 18;
1228 tmppkt
= hello
->extensions
;
1230 if (!PACKET_forward(&tmppkt
, 2)
1231 || !PACKET_get_net_2(&tmppkt
, &type
)
1232 || !PACKET_get_length_prefixed_2(&tmppkt
, &sni
)) {
1236 if (type
!= TLSEXT_TYPE_server_name
)
1239 ext_len
= TLS1_get_client_version(s
) >= TLS1_2_VERSION
?
1240 sizeof(kSafariExtensionsBlock
) : kSafariCommonExtensionsLength
;
1242 s
->s3
->is_probably_safari
= PACKET_equal(&tmppkt
, kSafariExtensionsBlock
,
1245 #endif /* !OPENSSL_NO_EC */
1247 MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
)
1249 int al
= SSL_AD_INTERNAL_ERROR
;
1250 /* |cookie| will only be initialized for DTLS. */
1251 PACKET session_id
, compression
, extensions
, cookie
;
1252 static const unsigned char null_compression
= 0;
1253 CLIENTHELLO_MSG
*clienthello
;
1255 clienthello
= OPENSSL_zalloc(sizeof(*clienthello
));
1256 if (clienthello
== NULL
) {
1257 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1260 /* Check if this is actually an unexpected renegotiation ClientHello */
1261 if (s
->renegotiate
== 0 && !SSL_IS_FIRST_HANDSHAKE(s
)) {
1267 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1269 clienthello
->isv2
= RECORD_LAYER_is_sslv2_record(&s
->rlayer
);
1270 PACKET_null_init(&cookie
);
1272 if (clienthello
->isv2
) {
1275 if (!SSL_IS_FIRST_HANDSHAKE(s
) || s
->hello_retry_request
) {
1276 al
= SSL_AD_HANDSHAKE_FAILURE
;
1277 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_UNEXPECTED_MESSAGE
);
1282 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1283 * header is sent directly on the wire, not wrapped as a TLS
1284 * record. Our record layer just processes the message length and passes
1285 * the rest right through. Its format is:
1287 * 0-1 msg_length - decoded by the record layer
1288 * 2 msg_type - s->init_msg points here
1290 * 5-6 cipher_spec_length
1291 * 7-8 session_id_length
1292 * 9-10 challenge_length
1296 if (!PACKET_get_1(pkt
, &mt
)
1297 || mt
!= SSL2_MT_CLIENT_HELLO
) {
1299 * Should never happen. We should have tested this in the record
1300 * layer in order to have determined that this is a SSLv2 record
1301 * in the first place
1303 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1308 if (!PACKET_get_net_2(pkt
, &clienthello
->legacy_version
)) {
1309 al
= SSL_AD_DECODE_ERROR
;
1310 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_TOO_SHORT
);
1314 /* Parse the message and load client random. */
1315 if (clienthello
->isv2
) {
1317 * Handle an SSLv2 backwards compatible ClientHello
1318 * Note, this is only for SSLv3+ using the backward compatible format.
1319 * Real SSLv2 is not supported, and is rejected below.
1321 unsigned int ciphersuite_len
, session_id_len
, challenge_len
;
1324 if (!PACKET_get_net_2(pkt
, &ciphersuite_len
)
1325 || !PACKET_get_net_2(pkt
, &session_id_len
)
1326 || !PACKET_get_net_2(pkt
, &challenge_len
)) {
1327 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1328 SSL_R_RECORD_LENGTH_MISMATCH
);
1329 al
= SSL_AD_DECODE_ERROR
;
1333 if (session_id_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
1334 al
= SSL_AD_DECODE_ERROR
;
1335 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1339 if (!PACKET_get_sub_packet(pkt
, &clienthello
->ciphersuites
,
1341 || !PACKET_copy_bytes(pkt
, clienthello
->session_id
, session_id_len
)
1342 || !PACKET_get_sub_packet(pkt
, &challenge
, challenge_len
)
1343 /* No extensions. */
1344 || PACKET_remaining(pkt
) != 0) {
1345 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
,
1346 SSL_R_RECORD_LENGTH_MISMATCH
);
1347 al
= SSL_AD_DECODE_ERROR
;
1350 clienthello
->session_id_len
= session_id_len
;
1352 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1353 * here rather than sizeof(clienthello->random) because that is the limit
1354 * for SSLv3 and it is fixed. It won't change even if
1355 * sizeof(clienthello->random) does.
1357 challenge_len
= challenge_len
> SSL3_RANDOM_SIZE
1358 ? SSL3_RANDOM_SIZE
: challenge_len
;
1359 memset(clienthello
->random
, 0, SSL3_RANDOM_SIZE
);
1360 if (!PACKET_copy_bytes(&challenge
,
1361 clienthello
->random
+ SSL3_RANDOM_SIZE
-
1362 challenge_len
, challenge_len
)
1363 /* Advertise only null compression. */
1364 || !PACKET_buf_init(&compression
, &null_compression
, 1)) {
1365 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1366 al
= SSL_AD_INTERNAL_ERROR
;
1370 PACKET_null_init(&clienthello
->extensions
);
1372 /* Regular ClientHello. */
1373 if (!PACKET_copy_bytes(pkt
, clienthello
->random
, SSL3_RANDOM_SIZE
)
1374 || !PACKET_get_length_prefixed_1(pkt
, &session_id
)
1375 || !PACKET_copy_all(&session_id
, clienthello
->session_id
,
1376 SSL_MAX_SSL_SESSION_ID_LENGTH
,
1377 &clienthello
->session_id_len
)) {
1378 al
= SSL_AD_DECODE_ERROR
;
1379 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1383 if (SSL_IS_DTLS(s
)) {
1384 if (!PACKET_get_length_prefixed_1(pkt
, &cookie
)) {
1385 al
= SSL_AD_DECODE_ERROR
;
1386 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1389 if (!PACKET_copy_all(&cookie
, clienthello
->dtls_cookie
,
1390 DTLS1_COOKIE_LENGTH
,
1391 &clienthello
->dtls_cookie_len
)) {
1392 al
= SSL_AD_DECODE_ERROR
;
1393 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1397 * If we require cookies and this ClientHello doesn't contain one,
1398 * just return since we do not want to allocate any memory yet.
1399 * So check cookie length...
1401 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1402 if (clienthello
->dtls_cookie_len
== 0)
1407 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->ciphersuites
)) {
1408 al
= SSL_AD_DECODE_ERROR
;
1409 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1413 if (!PACKET_get_length_prefixed_1(pkt
, &compression
)) {
1414 al
= SSL_AD_DECODE_ERROR
;
1415 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1419 /* Could be empty. */
1420 if (PACKET_remaining(pkt
) == 0) {
1421 PACKET_null_init(&clienthello
->extensions
);
1423 if (!PACKET_get_length_prefixed_2(pkt
, &clienthello
->extensions
)) {
1424 al
= SSL_AD_DECODE_ERROR
;
1425 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1431 if (!PACKET_copy_all(&compression
, clienthello
->compressions
,
1432 MAX_COMPRESSIONS_SIZE
,
1433 &clienthello
->compressions_len
)) {
1434 al
= SSL_AD_DECODE_ERROR
;
1435 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO
, SSL_R_LENGTH_MISMATCH
);
1439 /* Preserve the raw extensions PACKET for later use */
1440 extensions
= clienthello
->extensions
;
1441 if (!tls_collect_extensions(s
, &extensions
, EXT_CLIENT_HELLO
,
1442 &clienthello
->pre_proc_exts
, &al
,
1443 &clienthello
->pre_proc_exts_len
)) {
1444 /* SSLerr already been called */
1447 s
->clienthello
= clienthello
;
1449 return MSG_PROCESS_CONTINUE_PROCESSING
;
1451 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1453 ossl_statem_set_error(s
);
1455 OPENSSL_free(clienthello
->pre_proc_exts
);
1456 OPENSSL_free(clienthello
);
1458 return MSG_PROCESS_ERROR
;
1461 static int tls_early_post_process_client_hello(SSL
*s
, int *al
)
1468 #ifndef OPENSSL_NO_COMP
1469 SSL_COMP
*comp
= NULL
;
1471 const SSL_CIPHER
*c
;
1472 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
1473 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
1474 CLIENTHELLO_MSG
*clienthello
= s
->clienthello
;
1476 *al
= SSL_AD_INTERNAL_ERROR
;
1477 /* Finished parsing the ClientHello, now we can start processing it */
1478 /* Give the early callback a crack at things */
1479 if (s
->ctx
->early_cb
!= NULL
) {
1481 /* A failure in the early callback terminates the connection. */
1482 code
= s
->ctx
->early_cb(s
, al
, s
->ctx
->early_cb_arg
);
1486 s
->rwstate
= SSL_EARLY_WORK
;
1491 /* Set up the client_random */
1492 memcpy(s
->s3
->client_random
, clienthello
->random
, SSL3_RANDOM_SIZE
);
1494 /* Choose the version */
1496 if (clienthello
->isv2
) {
1497 if (clienthello
->legacy_version
== SSL2_VERSION
1498 || (clienthello
->legacy_version
& 0xff00)
1499 != (SSL3_VERSION_MAJOR
<< 8)) {
1501 * This is real SSLv2 or something complete unknown. We don't
1504 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_UNKNOWN_PROTOCOL
);
1508 s
->client_version
= clienthello
->legacy_version
;
1511 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1512 * versions are potentially compatible. Version negotiation comes later.
1514 if (!SSL_IS_DTLS(s
)) {
1515 protverr
= ssl_choose_server_version(s
, clienthello
);
1516 } else if (s
->method
->version
!= DTLS_ANY_VERSION
&&
1517 DTLS_VERSION_LT((int)clienthello
->legacy_version
, s
->version
)) {
1518 protverr
= SSL_R_VERSION_TOO_LOW
;
1524 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1525 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
1526 /* like ssl3_get_record, send alert using remote version number */
1527 s
->version
= s
->client_version
= clienthello
->legacy_version
;
1529 *al
= SSL_AD_PROTOCOL_VERSION
;
1533 if (SSL_IS_DTLS(s
)) {
1534 /* Empty cookie was already handled above by returning early. */
1535 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) {
1536 if (s
->ctx
->app_verify_cookie_cb
!= NULL
) {
1537 if (s
->ctx
->app_verify_cookie_cb(s
, clienthello
->dtls_cookie
,
1538 clienthello
->dtls_cookie_len
) == 0) {
1539 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1540 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1541 SSL_R_COOKIE_MISMATCH
);
1543 /* else cookie verification succeeded */
1545 /* default verification */
1546 } else if (s
->d1
->cookie_len
!= clienthello
->dtls_cookie_len
1547 || memcmp(clienthello
->dtls_cookie
, s
->d1
->cookie
,
1548 s
->d1
->cookie_len
) != 0) {
1549 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1550 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_COOKIE_MISMATCH
);
1553 s
->d1
->cookie_verified
= 1;
1555 if (s
->method
->version
== DTLS_ANY_VERSION
) {
1556 protverr
= ssl_choose_server_version(s
, clienthello
);
1557 if (protverr
!= 0) {
1558 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, protverr
);
1559 s
->version
= s
->client_version
;
1560 *al
= SSL_AD_PROTOCOL_VERSION
;
1568 /* We need to do this before getting the session */
1569 if (!tls_parse_extension(s
, TLSEXT_IDX_extended_master_secret
,
1571 clienthello
->pre_proc_exts
, NULL
, 0, al
)) {
1572 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1577 * We don't allow resumption in a backwards compatible ClientHello.
1578 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1580 * Versions before 0.9.7 always allow clients to resume sessions in
1581 * renegotiation. 0.9.7 and later allow this by default, but optionally
1582 * ignore resumption requests with flag
1583 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1584 * than a change to default behavior so that applications relying on
1585 * this for security won't even compile against older library versions).
1586 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1587 * request renegotiation but not a new session (s->new_session remains
1588 * unset): for servers, this essentially just means that the
1589 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1592 if (clienthello
->isv2
||
1594 (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
))) {
1595 if (!ssl_get_new_session(s
, 1))
1598 i
= ssl_get_prev_session(s
, clienthello
, al
);
1600 /* previous session */
1602 } else if (i
== -1) {
1606 if (!ssl_get_new_session(s
, 1))
1611 if (!ssl_cache_cipherlist(s
, &clienthello
->ciphersuites
,
1612 clienthello
->isv2
, al
) ||
1613 !bytes_to_cipher_list(s
, &clienthello
->ciphersuites
, &ciphers
, &scsvs
,
1614 clienthello
->isv2
, al
)) {
1618 s
->s3
->send_connection_binding
= 0;
1619 /* Check what signalling cipher-suite values were received. */
1620 if (scsvs
!= NULL
) {
1621 for(i
= 0; i
< sk_SSL_CIPHER_num(scsvs
); i
++) {
1622 c
= sk_SSL_CIPHER_value(scsvs
, i
);
1623 if (SSL_CIPHER_get_id(c
) == SSL3_CK_SCSV
) {
1624 if (s
->renegotiate
) {
1625 /* SCSV is fatal if renegotiating */
1626 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1627 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
);
1628 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1631 s
->s3
->send_connection_binding
= 1;
1632 } else if (SSL_CIPHER_get_id(c
) == SSL3_CK_FALLBACK_SCSV
&&
1633 !ssl_check_version_downgrade(s
)) {
1635 * This SCSV indicates that the client previously tried
1636 * a higher version. We should fail if the current version
1637 * is an unexpected downgrade, as that indicates that the first
1638 * connection may have been tampered with in order to trigger
1639 * an insecure downgrade.
1641 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1642 SSL_R_INAPPROPRIATE_FALLBACK
);
1643 *al
= SSL_AD_INAPPROPRIATE_FALLBACK
;
1649 /* If it is a hit, check that the cipher is in the list */
1652 id
= s
->session
->cipher
->id
;
1655 fprintf(stderr
, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers
));
1657 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
1658 c
= sk_SSL_CIPHER_value(ciphers
, i
);
1660 fprintf(stderr
, "client [%2d of %2d]:%s\n",
1661 i
, sk_SSL_CIPHER_num(ciphers
), SSL_CIPHER_get_name(c
));
1670 * we need to have the cipher in the cipher list if we are asked
1673 *al
= SSL_AD_ILLEGAL_PARAMETER
;
1674 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1675 SSL_R_REQUIRED_CIPHER_MISSING
);
1680 for (loop
= 0; loop
< clienthello
->compressions_len
; loop
++) {
1681 if (clienthello
->compressions
[loop
] == 0)
1685 if (loop
>= clienthello
->compressions_len
) {
1687 *al
= SSL_AD_DECODE_ERROR
;
1688 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_NO_COMPRESSION_SPECIFIED
);
1692 #ifndef OPENSSL_NO_EC
1693 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1694 ssl_check_for_safari(s
, clienthello
);
1695 #endif /* !OPENSSL_NO_EC */
1697 /* TLS extensions */
1698 if (!tls_parse_all_extensions(s
, EXT_CLIENT_HELLO
,
1699 clienthello
->pre_proc_exts
, NULL
, 0, al
)) {
1700 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_PARSE_TLSEXT
);
1705 * Check if we want to use external pre-shared secret for this handshake
1706 * for not reused session only. We need to generate server_random before
1707 * calling tls_session_secret_cb in order to allow SessionTicket
1708 * processing to use it in key derivation.
1712 pos
= s
->s3
->server_random
;
1713 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
) <= 0) {
1718 if (!s
->hit
&& s
->version
>= TLS1_VERSION
&& s
->ext
.session_secret_cb
) {
1719 const SSL_CIPHER
*pref_cipher
= NULL
;
1721 * s->session->master_key_length is a size_t, but this is an int for
1722 * backwards compat reasons
1724 int master_key_length
;
1726 master_key_length
= sizeof(s
->session
->master_key
);
1727 if (s
->ext
.session_secret_cb(s
, s
->session
->master_key
,
1728 &master_key_length
, ciphers
,
1730 s
->ext
.session_secret_cb_arg
)
1731 && master_key_length
> 0) {
1732 s
->session
->master_key_length
= master_key_length
;
1734 s
->session
->ciphers
= ciphers
;
1735 s
->session
->verify_result
= X509_V_OK
;
1739 /* check if some cipher was preferred by call back */
1740 if (pref_cipher
== NULL
)
1741 pref_cipher
= ssl3_choose_cipher(s
, s
->session
->ciphers
,
1742 SSL_get_ciphers(s
));
1743 if (pref_cipher
== NULL
) {
1744 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1745 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_NO_SHARED_CIPHER
);
1749 s
->session
->cipher
= pref_cipher
;
1750 sk_SSL_CIPHER_free(s
->cipher_list
);
1751 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1752 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1753 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1758 * Worst case, we will use the NULL compression, but if we have other
1759 * options, we will now look for them. We have complen-1 compression
1760 * algorithms from the client, starting at q.
1762 s
->s3
->tmp
.new_compression
= NULL
;
1763 #ifndef OPENSSL_NO_COMP
1764 /* This only happens if we have a cache hit */
1765 if (s
->session
->compress_meth
!= 0 && !SSL_IS_TLS13(s
)) {
1766 int m
, comp_id
= s
->session
->compress_meth
;
1768 /* Perform sanity checks on resumed compression algorithm */
1769 /* Can't disable compression */
1770 if (!ssl_allow_compression(s
)) {
1771 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1772 SSL_R_INCONSISTENT_COMPRESSION
);
1775 /* Look for resumed compression method */
1776 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++) {
1777 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1778 if (comp_id
== comp
->id
) {
1779 s
->s3
->tmp
.new_compression
= comp
;
1783 if (s
->s3
->tmp
.new_compression
== NULL
) {
1784 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1785 SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1788 /* Look for resumed method in compression list */
1789 for (k
= 0; k
< clienthello
->compressions_len
; k
++) {
1790 if (clienthello
->compressions
[k
] == comp_id
)
1793 if (k
>= clienthello
->compressions_len
) {
1794 *al
= SSL_AD_ILLEGAL_PARAMETER
;
1795 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
,
1796 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING
);
1799 } else if (s
->hit
) {
1801 } else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
1802 && !SSL_IS_TLS13(s
)) {
1803 /* See if we have a match */
1804 int m
, nn
, v
, done
= 0;
1807 nn
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1808 for (m
= 0; m
< nn
; m
++) {
1809 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, m
);
1811 for (o
= 0; o
< clienthello
->compressions_len
; o
++) {
1812 if (v
== clienthello
->compressions
[o
]) {
1821 s
->s3
->tmp
.new_compression
= comp
;
1827 * If compression is disabled we'd better not try to resume a session
1828 * using compression.
1830 if (s
->session
->compress_meth
!= 0) {
1831 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1837 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1841 #ifdef OPENSSL_NO_COMP
1842 s
->session
->compress_meth
= 0;
1844 s
->session
->compress_meth
= (comp
== NULL
) ? 0 : comp
->id
;
1846 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1847 s
->session
->ciphers
= ciphers
;
1848 if (ciphers
== NULL
) {
1849 *al
= SSL_AD_INTERNAL_ERROR
;
1850 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
1854 if (!tls1_set_server_sigalgs(s
)) {
1855 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1860 sk_SSL_CIPHER_free(ciphers
);
1861 sk_SSL_CIPHER_free(scsvs
);
1862 OPENSSL_free(clienthello
->pre_proc_exts
);
1863 OPENSSL_free(s
->clienthello
);
1864 s
->clienthello
= NULL
;
1867 ossl_statem_set_error(s
);
1869 sk_SSL_CIPHER_free(ciphers
);
1870 sk_SSL_CIPHER_free(scsvs
);
1871 OPENSSL_free(clienthello
->pre_proc_exts
);
1872 OPENSSL_free(s
->clienthello
);
1873 s
->clienthello
= NULL
;
1879 * Call the status request callback if needed. Upon success, returns 1.
1880 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
1882 static int tls_handle_status_request(SSL
*s
, int *al
)
1884 s
->ext
.status_expected
= 0;
1887 * If status request then ask callback what to do. Note: this must be
1888 * called after servername callbacks in case the certificate has changed,
1889 * and must be called after the cipher has been chosen because this may
1890 * influence which certificate is sent
1892 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_nothing
&& s
->ctx
!= NULL
1893 && s
->ctx
->ext
.status_cb
!= NULL
) {
1896 /* If no certificate can't return certificate status */
1897 if (s
->s3
->tmp
.cert
!= NULL
) {
1899 * Set current certificate to one we will use so SSL_get_certificate
1900 * et al can pick it up.
1902 s
->cert
->key
= s
->s3
->tmp
.cert
;
1903 ret
= s
->ctx
->ext
.status_cb(s
, s
->ctx
->ext
.status_arg
);
1905 /* We don't want to send a status request response */
1906 case SSL_TLSEXT_ERR_NOACK
:
1907 s
->ext
.status_expected
= 0;
1909 /* status request response should be sent */
1910 case SSL_TLSEXT_ERR_OK
:
1911 if (s
->ext
.ocsp
.resp
)
1912 s
->ext
.status_expected
= 1;
1914 /* something bad happened */
1915 case SSL_TLSEXT_ERR_ALERT_FATAL
:
1917 *al
= SSL_AD_INTERNAL_ERROR
;
1926 WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
)
1928 int al
= SSL_AD_HANDSHAKE_FAILURE
;
1929 const SSL_CIPHER
*cipher
;
1931 if (wst
== WORK_MORE_A
) {
1932 int rv
= tls_early_post_process_client_hello(s
, &al
);
1934 /* SSLErr() was already called */
1941 if (wst
== WORK_MORE_B
) {
1943 /* Let cert callback update server certificates if required */
1944 if (s
->cert
->cert_cb
) {
1945 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
1947 al
= SSL_AD_INTERNAL_ERROR
;
1948 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
1949 SSL_R_CERT_CB_ERROR
);
1953 s
->rwstate
= SSL_X509_LOOKUP
;
1956 s
->rwstate
= SSL_NOTHING
;
1959 ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
1961 if (cipher
== NULL
) {
1962 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
1963 SSL_R_NO_SHARED_CIPHER
);
1966 s
->s3
->tmp
.new_cipher
= cipher
;
1967 if (!tls_choose_sigalg(s
, &al
))
1969 /* check whether we should disable session resumption */
1970 if (s
->not_resumable_session_cb
!= NULL
)
1971 s
->session
->not_resumable
=
1972 s
->not_resumable_session_cb(s
, ((cipher
->algorithm_mkey
1973 & (SSL_kDHE
| SSL_kECDHE
))
1975 if (s
->session
->not_resumable
)
1976 /* do not send a session ticket */
1977 s
->ext
.ticket_expected
= 0;
1979 /* Session-id reuse */
1980 s
->s3
->tmp
.new_cipher
= s
->session
->cipher
;
1984 * we now have the following setup.
1986 * cipher_list - our preferred list of ciphers
1987 * ciphers - the clients preferred list of ciphers
1988 * compression - basically ignored right now
1989 * ssl version is set - sslv3
1990 * s->session - The ssl session has been setup.
1991 * s->hit - session reuse flag
1992 * s->s3->tmp.new_cipher- the new cipher to use.
1996 * Call status_request callback if needed. Has to be done after the
1997 * certificate callbacks etc above.
1999 if (!tls_handle_status_request(s
, &al
)) {
2000 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2001 SSL_R_CLIENTHELLO_TLSEXT
);
2007 #ifndef OPENSSL_NO_SRP
2008 if (wst
== WORK_MORE_C
) {
2010 if ((ret
= ssl_check_srp_ext_ClientHello(s
, &al
)) < 0) {
2012 * callback indicates further work to be done
2014 s
->rwstate
= SSL_X509_LOOKUP
;
2017 if (ret
!= SSL_ERROR_NONE
) {
2019 * This is not really an error but the only means to for
2020 * a client to detect whether srp is supported.
2022 if (al
!= TLS1_AD_UNKNOWN_PSK_IDENTITY
)
2023 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2024 SSL_R_CLIENTHELLO_TLSEXT
);
2026 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
,
2027 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2033 return WORK_FINISHED_STOP
;
2035 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2036 ossl_statem_set_error(s
);
2040 int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
)
2042 int compm
, al
= SSL_AD_INTERNAL_ERROR
;
2046 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
2047 version
= SSL_IS_TLS13(s
) ? TLS1_3_VERSION_DRAFT
: s
->version
;
2048 if (!WPACKET_put_bytes_u16(pkt
, version
)
2050 * Random stuff. Filling of the server_random takes place in
2051 * tls_process_client_hello()
2053 || !WPACKET_memcpy(pkt
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
)) {
2054 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
2059 * There are several cases for the session ID to send
2060 * back in the server hello:
2061 * - For session reuse from the session cache,
2062 * we send back the old session ID.
2063 * - If stateless session reuse (using a session ticket)
2064 * is successful, we send back the client's "session ID"
2065 * (which doesn't actually identify the session).
2066 * - If it is a new session, we send back the new
2068 * - However, if we want the new session to be single-use,
2069 * we send back a 0-length session ID.
2070 * s->hit is non-zero in either case of session reuse,
2071 * so the following won't overwrite an ID that we're supposed
2074 if (s
->session
->not_resumable
||
2075 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
2077 s
->session
->session_id_length
= 0;
2079 sl
= s
->session
->session_id_length
;
2080 if (sl
> sizeof(s
->session
->session_id
)) {
2081 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
2085 /* set up the compression method */
2086 #ifdef OPENSSL_NO_COMP
2089 if (s
->s3
->tmp
.new_compression
== NULL
)
2092 compm
= s
->s3
->tmp
.new_compression
->id
;
2095 if ((!SSL_IS_TLS13(s
)
2096 && !WPACKET_sub_memcpy_u8(pkt
, s
->session
->session_id
, sl
))
2097 || !s
->method
->put_cipher_by_char(s
->s3
->tmp
.new_cipher
, pkt
, &len
)
2098 || (!SSL_IS_TLS13(s
)
2099 && !WPACKET_put_bytes_u8(pkt
, compm
))
2100 || !tls_construct_extensions(s
, pkt
,
2102 ? EXT_TLS1_3_SERVER_HELLO
2103 : EXT_TLS1_2_SERVER_HELLO
,
2105 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
2109 if (!(s
->verify_mode
& SSL_VERIFY_PEER
)
2110 && !ssl3_digest_cached_records(s
, 0)) {
2111 al
= SSL_AD_INTERNAL_ERROR
;
2117 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2121 int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
)
2123 if (!s
->s3
->tmp
.cert_request
) {
2124 if (!ssl3_digest_cached_records(s
, 0)) {
2125 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2132 int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
)
2134 #ifndef OPENSSL_NO_DH
2135 EVP_PKEY
*pkdh
= NULL
;
2137 #ifndef OPENSSL_NO_EC
2138 unsigned char *encodedPoint
= NULL
;
2139 size_t encodedlen
= 0;
2142 const SIGALG_LOOKUP
*lu
= s
->s3
->tmp
.sigalg
;
2143 int al
= SSL_AD_INTERNAL_ERROR
, i
;
2146 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
2147 EVP_PKEY_CTX
*pctx
= NULL
;
2148 size_t paramlen
, paramoffset
;
2150 if (!WPACKET_get_total_written(pkt
, ¶moffset
)) {
2151 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2155 if (md_ctx
== NULL
) {
2156 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2160 type
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2162 r
[0] = r
[1] = r
[2] = r
[3] = NULL
;
2163 #ifndef OPENSSL_NO_PSK
2164 /* Plain PSK or RSAPSK nothing to do */
2165 if (type
& (SSL_kPSK
| SSL_kRSAPSK
)) {
2167 #endif /* !OPENSSL_NO_PSK */
2168 #ifndef OPENSSL_NO_DH
2169 if (type
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2170 CERT
*cert
= s
->cert
;
2172 EVP_PKEY
*pkdhp
= NULL
;
2175 if (s
->cert
->dh_tmp_auto
) {
2176 DH
*dhp
= ssl_get_auto_dh(s
);
2177 pkdh
= EVP_PKEY_new();
2178 if (pkdh
== NULL
|| dhp
== NULL
) {
2180 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2181 ERR_R_INTERNAL_ERROR
);
2184 EVP_PKEY_assign_DH(pkdh
, dhp
);
2187 pkdhp
= cert
->dh_tmp
;
2189 if ((pkdhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
)) {
2190 DH
*dhp
= s
->cert
->dh_tmp_cb(s
, 0, 1024);
2191 pkdh
= ssl_dh_to_pkey(dhp
);
2193 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2194 ERR_R_INTERNAL_ERROR
);
2199 if (pkdhp
== NULL
) {
2200 al
= SSL_AD_HANDSHAKE_FAILURE
;
2201 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2202 SSL_R_MISSING_TMP_DH_KEY
);
2205 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
2206 EVP_PKEY_security_bits(pkdhp
), 0, pkdhp
)) {
2207 al
= SSL_AD_HANDSHAKE_FAILURE
;
2208 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2209 SSL_R_DH_KEY_TOO_SMALL
);
2212 if (s
->s3
->tmp
.pkey
!= NULL
) {
2213 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2214 ERR_R_INTERNAL_ERROR
);
2218 s
->s3
->tmp
.pkey
= ssl_generate_pkey(pkdhp
);
2220 if (s
->s3
->tmp
.pkey
== NULL
) {
2221 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
2225 dh
= EVP_PKEY_get0_DH(s
->s3
->tmp
.pkey
);
2227 EVP_PKEY_free(pkdh
);
2230 DH_get0_pqg(dh
, &r
[0], NULL
, &r
[1]);
2231 DH_get0_key(dh
, &r
[2], NULL
);
2234 #ifndef OPENSSL_NO_EC
2235 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2238 if (s
->s3
->tmp
.pkey
!= NULL
) {
2239 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2240 ERR_R_INTERNAL_ERROR
);
2244 /* Get NID of appropriate shared curve */
2245 nid
= tls1_shared_group(s
, -2);
2246 curve_id
= tls1_ec_nid2curve_id(nid
);
2247 if (curve_id
== 0) {
2248 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2249 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
2252 s
->s3
->tmp
.pkey
= ssl_generate_pkey_curve(curve_id
);
2253 /* Generate a new key for this curve */
2254 if (s
->s3
->tmp
.pkey
== NULL
) {
2255 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
2259 /* Encode the public key. */
2260 encodedlen
= EVP_PKEY_get1_tls_encodedpoint(s
->s3
->tmp
.pkey
,
2262 if (encodedlen
== 0) {
2263 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2268 * We'll generate the serverKeyExchange message explicitly so we
2269 * can set these to NULLs
2276 #endif /* !OPENSSL_NO_EC */
2277 #ifndef OPENSSL_NO_SRP
2278 if (type
& SSL_kSRP
) {
2279 if ((s
->srp_ctx
.N
== NULL
) ||
2280 (s
->srp_ctx
.g
== NULL
) ||
2281 (s
->srp_ctx
.s
== NULL
) || (s
->srp_ctx
.B
== NULL
)) {
2282 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2283 SSL_R_MISSING_SRP_PARAM
);
2286 r
[0] = s
->srp_ctx
.N
;
2287 r
[1] = s
->srp_ctx
.g
;
2288 r
[2] = s
->srp_ctx
.s
;
2289 r
[3] = s
->srp_ctx
.B
;
2293 al
= SSL_AD_HANDSHAKE_FAILURE
;
2294 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2295 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
2299 if (((s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aSRP
)) != 0)
2300 || ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)) != 0) {
2302 } else if (lu
== NULL
) {
2303 al
= SSL_AD_DECODE_ERROR
;
2307 #ifndef OPENSSL_NO_PSK
2308 if (type
& SSL_PSK
) {
2309 size_t len
= (s
->cert
->psk_identity_hint
== NULL
)
2310 ? 0 : strlen(s
->cert
->psk_identity_hint
);
2313 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2314 * checked this when we set the identity hint - but just in case
2316 if (len
> PSK_MAX_IDENTITY_LEN
2317 || !WPACKET_sub_memcpy_u16(pkt
, s
->cert
->psk_identity_hint
,
2319 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2320 ERR_R_INTERNAL_ERROR
);
2326 for (i
= 0; i
< 4 && r
[i
] != NULL
; i
++) {
2327 unsigned char *binval
;
2330 #ifndef OPENSSL_NO_SRP
2331 if ((i
== 2) && (type
& SSL_kSRP
)) {
2332 res
= WPACKET_start_sub_packet_u8(pkt
);
2335 res
= WPACKET_start_sub_packet_u16(pkt
);
2338 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2339 ERR_R_INTERNAL_ERROR
);
2343 #ifndef OPENSSL_NO_DH
2345 * for interoperability with some versions of the Microsoft TLS
2346 * stack, we need to zero pad the DHE pub key to the same length
2349 if ((i
== 2) && (type
& (SSL_kDHE
| SSL_kDHEPSK
))) {
2350 size_t len
= BN_num_bytes(r
[0]) - BN_num_bytes(r
[2]);
2353 if (!WPACKET_allocate_bytes(pkt
, len
, &binval
)) {
2354 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2355 ERR_R_INTERNAL_ERROR
);
2358 memset(binval
, 0, len
);
2362 if (!WPACKET_allocate_bytes(pkt
, BN_num_bytes(r
[i
]), &binval
)
2363 || !WPACKET_close(pkt
)) {
2364 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2365 ERR_R_INTERNAL_ERROR
);
2369 BN_bn2bin(r
[i
], binval
);
2372 #ifndef OPENSSL_NO_EC
2373 if (type
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
2375 * We only support named (not generic) curves. In this situation, the
2376 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2377 * [1 byte length of encoded point], followed by the actual encoded
2380 if (!WPACKET_put_bytes_u8(pkt
, NAMED_CURVE_TYPE
)
2381 || !WPACKET_put_bytes_u8(pkt
, 0)
2382 || !WPACKET_put_bytes_u8(pkt
, curve_id
)
2383 || !WPACKET_sub_memcpy_u8(pkt
, encodedPoint
, encodedlen
)) {
2384 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2385 ERR_R_INTERNAL_ERROR
);
2388 OPENSSL_free(encodedPoint
);
2389 encodedPoint
= NULL
;
2395 EVP_PKEY
*pkey
= s
->s3
->tmp
.cert
->privatekey
;
2396 const EVP_MD
*md
= ssl_md(lu
->hash_idx
);
2397 unsigned char *sigbytes1
, *sigbytes2
;
2400 if (pkey
== NULL
|| md
== NULL
) {
2401 /* Should never happen */
2402 al
= SSL_AD_INTERNAL_ERROR
;
2403 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2404 ERR_R_INTERNAL_ERROR
);
2408 * n is the length of the params, they start at &(d[4]) and p
2409 * points to the space at the end.
2412 /* Get length of the parameters we have written above */
2413 if (!WPACKET_get_length(pkt
, ¶mlen
)) {
2414 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2415 ERR_R_INTERNAL_ERROR
);
2418 /* send signature algorithm */
2419 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
))
2422 * Create the signature. We don't know the actual length of the sig
2423 * until after we've created it, so we reserve enough bytes for it
2424 * up front, and then properly allocate them in the WPACKET
2427 siglen
= EVP_PKEY_size(pkey
);
2428 if (!WPACKET_sub_reserve_bytes_u16(pkt
, siglen
, &sigbytes1
)
2429 || EVP_DigestSignInit(md_ctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
2430 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2431 ERR_R_INTERNAL_ERROR
);
2434 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2435 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
2436 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
, RSA_PSS_SALTLEN_DIGEST
) <= 0) {
2437 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2442 if (EVP_DigestSignUpdate(md_ctx
, &(s
->s3
->client_random
[0]),
2443 SSL3_RANDOM_SIZE
) <= 0
2444 || EVP_DigestSignUpdate(md_ctx
, &(s
->s3
->server_random
[0]),
2445 SSL3_RANDOM_SIZE
) <= 0
2446 || EVP_DigestSignUpdate(md_ctx
,
2447 s
->init_buf
->data
+ paramoffset
,
2449 || EVP_DigestSignFinal(md_ctx
, sigbytes1
, &siglen
) <= 0
2450 || !WPACKET_sub_allocate_bytes_u16(pkt
, siglen
, &sigbytes2
)
2451 || sigbytes1
!= sigbytes2
) {
2452 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE
,
2453 ERR_R_INTERNAL_ERROR
);
2458 EVP_MD_CTX_free(md_ctx
);
2461 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2463 #ifndef OPENSSL_NO_DH
2464 EVP_PKEY_free(pkdh
);
2466 #ifndef OPENSSL_NO_EC
2467 OPENSSL_free(encodedPoint
);
2469 EVP_MD_CTX_free(md_ctx
);
2473 int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
)
2476 STACK_OF(X509_NAME
) *sk
= NULL
;
2478 if (SSL_IS_TLS13(s
)) {
2479 /* TODO(TLS1.3) for now send empty request context */
2480 if (!WPACKET_put_bytes_u8(pkt
, 0)) {
2481 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2482 ERR_R_INTERNAL_ERROR
);
2486 /* get the list of acceptable cert types */
2487 if (!WPACKET_start_sub_packet_u8(pkt
)
2488 || !ssl3_get_req_cert_type(s
, pkt
) || !WPACKET_close(pkt
)) {
2489 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2490 ERR_R_INTERNAL_ERROR
);
2495 if (SSL_USE_SIGALGS(s
)) {
2496 const uint16_t *psigs
;
2497 size_t nl
= tls12_get_psigalgs(s
, 1, &psigs
);
2499 if (!WPACKET_start_sub_packet_u16(pkt
)
2500 || !tls12_copy_sigalgs(s
, pkt
, psigs
, nl
)
2501 || !WPACKET_close(pkt
)) {
2502 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2503 ERR_R_INTERNAL_ERROR
);
2508 /* Start sub-packet for client CA list */
2509 if (!WPACKET_start_sub_packet_u16(pkt
)) {
2510 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2514 sk
= SSL_get_client_CA_list(s
);
2516 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
2517 unsigned char *namebytes
;
2518 X509_NAME
*name
= sk_X509_NAME_value(sk
, i
);
2522 || (namelen
= i2d_X509_NAME(name
, NULL
)) < 0
2523 || !WPACKET_sub_allocate_bytes_u16(pkt
, namelen
,
2525 || i2d_X509_NAME(name
, &namebytes
) != namelen
) {
2526 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
,
2527 ERR_R_INTERNAL_ERROR
);
2532 /* else no CA names */
2533 if (!WPACKET_close(pkt
)) {
2534 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2538 * TODO(TLS1.3) implement configurable certificate_extensions
2539 * For now just send zero length extensions.
2541 if (SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u16(pkt
, 0)) {
2542 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST
, ERR_R_INTERNAL_ERROR
);
2546 s
->s3
->tmp
.cert_request
= 1;
2550 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2554 static int tls_process_cke_psk_preamble(SSL
*s
, PACKET
*pkt
, int *al
)
2556 #ifndef OPENSSL_NO_PSK
2557 unsigned char psk
[PSK_MAX_PSK_LEN
];
2559 PACKET psk_identity
;
2561 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity
)) {
2562 *al
= SSL_AD_DECODE_ERROR
;
2563 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, SSL_R_LENGTH_MISMATCH
);
2566 if (PACKET_remaining(&psk_identity
) > PSK_MAX_IDENTITY_LEN
) {
2567 *al
= SSL_AD_DECODE_ERROR
;
2568 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, SSL_R_DATA_LENGTH_TOO_LONG
);
2571 if (s
->psk_server_callback
== NULL
) {
2572 *al
= SSL_AD_INTERNAL_ERROR
;
2573 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, SSL_R_PSK_NO_SERVER_CB
);
2577 if (!PACKET_strndup(&psk_identity
, &s
->session
->psk_identity
)) {
2578 *al
= SSL_AD_INTERNAL_ERROR
;
2579 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_INTERNAL_ERROR
);
2583 psklen
= s
->psk_server_callback(s
, s
->session
->psk_identity
,
2586 if (psklen
> PSK_MAX_PSK_LEN
) {
2587 *al
= SSL_AD_INTERNAL_ERROR
;
2588 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_INTERNAL_ERROR
);
2590 } else if (psklen
== 0) {
2592 * PSK related to the given identity not found
2594 *al
= SSL_AD_UNKNOWN_PSK_IDENTITY
;
2595 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
,
2596 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2600 OPENSSL_free(s
->s3
->tmp
.psk
);
2601 s
->s3
->tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2602 OPENSSL_cleanse(psk
, psklen
);
2604 if (s
->s3
->tmp
.psk
== NULL
) {
2605 *al
= SSL_AD_INTERNAL_ERROR
;
2606 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_MALLOC_FAILURE
);
2610 s
->s3
->tmp
.psklen
= psklen
;
2614 /* Should never happen */
2615 *al
= SSL_AD_INTERNAL_ERROR
;
2616 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE
, ERR_R_INTERNAL_ERROR
);
2621 static int tls_process_cke_rsa(SSL
*s
, PACKET
*pkt
, int *al
)
2623 #ifndef OPENSSL_NO_RSA
2624 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
2626 unsigned char decrypt_good
, version_good
;
2627 size_t j
, padding_len
;
2628 PACKET enc_premaster
;
2630 unsigned char *rsa_decrypt
= NULL
;
2633 rsa
= EVP_PKEY_get0_RSA(s
->cert
->pkeys
[SSL_PKEY_RSA
].privatekey
);
2635 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2636 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, SSL_R_MISSING_RSA_CERTIFICATE
);
2640 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2641 if (s
->version
== SSL3_VERSION
|| s
->version
== DTLS1_BAD_VER
) {
2642 enc_premaster
= *pkt
;
2644 if (!PACKET_get_length_prefixed_2(pkt
, &enc_premaster
)
2645 || PACKET_remaining(pkt
) != 0) {
2646 *al
= SSL_AD_DECODE_ERROR
;
2647 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, SSL_R_LENGTH_MISMATCH
);
2653 * We want to be sure that the plaintext buffer size makes it safe to
2654 * iterate over the entire size of a premaster secret
2655 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2656 * their ciphertext cannot accommodate a premaster secret anyway.
2658 if (RSA_size(rsa
) < SSL_MAX_MASTER_KEY_LENGTH
) {
2659 *al
= SSL_AD_INTERNAL_ERROR
;
2660 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, RSA_R_KEY_SIZE_TOO_SMALL
);
2664 rsa_decrypt
= OPENSSL_malloc(RSA_size(rsa
));
2665 if (rsa_decrypt
== NULL
) {
2666 *al
= SSL_AD_INTERNAL_ERROR
;
2667 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, ERR_R_MALLOC_FAILURE
);
2672 * We must not leak whether a decryption failure occurs because of
2673 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2674 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2675 * generates a random premaster secret for the case that the decrypt
2676 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2679 if (RAND_bytes(rand_premaster_secret
, sizeof(rand_premaster_secret
)) <= 0)
2683 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2684 * the timing-sensitive code below.
2686 /* TODO(size_t): Convert this function */
2687 decrypt_len
= (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster
),
2688 PACKET_data(&enc_premaster
),
2689 rsa_decrypt
, rsa
, RSA_NO_PADDING
);
2690 if (decrypt_len
< 0)
2693 /* Check the padding. See RFC 3447, section 7.2.2. */
2696 * The smallest padded premaster is 11 bytes of overhead. Small keys
2697 * are publicly invalid, so this may return immediately. This ensures
2698 * PS is at least 8 bytes.
2700 if (decrypt_len
< 11 + SSL_MAX_MASTER_KEY_LENGTH
) {
2701 *al
= SSL_AD_DECRYPT_ERROR
;
2702 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, SSL_R_DECRYPTION_FAILED
);
2706 padding_len
= decrypt_len
- SSL_MAX_MASTER_KEY_LENGTH
;
2707 decrypt_good
= constant_time_eq_int_8(rsa_decrypt
[0], 0) &
2708 constant_time_eq_int_8(rsa_decrypt
[1], 2);
2709 for (j
= 2; j
< padding_len
- 1; j
++) {
2710 decrypt_good
&= ~constant_time_is_zero_8(rsa_decrypt
[j
]);
2712 decrypt_good
&= constant_time_is_zero_8(rsa_decrypt
[padding_len
- 1]);
2715 * If the version in the decrypted pre-master secret is correct then
2716 * version_good will be 0xff, otherwise it'll be zero. The
2717 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2718 * (http://eprint.iacr.org/2003/052/) exploits the version number
2719 * check as a "bad version oracle". Thus version checks are done in
2720 * constant time and are treated like any other decryption error.
2723 constant_time_eq_8(rsa_decrypt
[padding_len
],
2724 (unsigned)(s
->client_version
>> 8));
2726 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
2727 (unsigned)(s
->client_version
& 0xff));
2730 * The premaster secret must contain the same version number as the
2731 * ClientHello to detect version rollback attacks (strangely, the
2732 * protocol does not offer such protection for DH ciphersuites).
2733 * However, buggy clients exist that send the negotiated protocol
2734 * version instead if the server does not support the requested
2735 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2738 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) {
2739 unsigned char workaround_good
;
2740 workaround_good
= constant_time_eq_8(rsa_decrypt
[padding_len
],
2741 (unsigned)(s
->version
>> 8));
2743 constant_time_eq_8(rsa_decrypt
[padding_len
+ 1],
2744 (unsigned)(s
->version
& 0xff));
2745 version_good
|= workaround_good
;
2749 * Both decryption and version must be good for decrypt_good to
2750 * remain non-zero (0xff).
2752 decrypt_good
&= version_good
;
2755 * Now copy rand_premaster_secret over from p using
2756 * decrypt_good_mask. If decryption failed, then p does not
2757 * contain valid plaintext, however, a check above guarantees
2758 * it is still sufficiently large to read from.
2760 for (j
= 0; j
< sizeof(rand_premaster_secret
); j
++) {
2761 rsa_decrypt
[padding_len
+ j
] =
2762 constant_time_select_8(decrypt_good
,
2763 rsa_decrypt
[padding_len
+ j
],
2764 rand_premaster_secret
[j
]);
2767 if (!ssl_generate_master_secret(s
, rsa_decrypt
+ padding_len
,
2768 sizeof(rand_premaster_secret
), 0)) {
2769 *al
= SSL_AD_INTERNAL_ERROR
;
2770 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, ERR_R_INTERNAL_ERROR
);
2776 OPENSSL_free(rsa_decrypt
);
2779 /* Should never happen */
2780 *al
= SSL_AD_INTERNAL_ERROR
;
2781 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA
, ERR_R_INTERNAL_ERROR
);
2786 static int tls_process_cke_dhe(SSL
*s
, PACKET
*pkt
, int *al
)
2788 #ifndef OPENSSL_NO_DH
2789 EVP_PKEY
*skey
= NULL
;
2793 const unsigned char *data
;
2794 EVP_PKEY
*ckey
= NULL
;
2797 if (!PACKET_get_net_2(pkt
, &i
) || PACKET_remaining(pkt
) != i
) {
2798 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2799 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
,
2800 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2803 skey
= s
->s3
->tmp
.pkey
;
2805 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2806 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, SSL_R_MISSING_TMP_DH_KEY
);
2810 if (PACKET_remaining(pkt
) == 0L) {
2811 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2812 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, SSL_R_MISSING_TMP_DH_KEY
);
2815 if (!PACKET_get_bytes(pkt
, &data
, i
)) {
2816 /* We already checked we have enough data */
2817 *al
= SSL_AD_INTERNAL_ERROR
;
2818 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2821 ckey
= EVP_PKEY_new();
2822 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) == 0) {
2823 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, SSL_R_BN_LIB
);
2826 cdh
= EVP_PKEY_get0_DH(ckey
);
2827 pub_key
= BN_bin2bn(data
, i
, NULL
);
2829 if (pub_key
== NULL
|| !DH_set0_key(cdh
, pub_key
, NULL
)) {
2830 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2831 if (pub_key
!= NULL
)
2836 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
2837 *al
= SSL_AD_INTERNAL_ERROR
;
2838 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2843 EVP_PKEY_free(s
->s3
->tmp
.pkey
);
2844 s
->s3
->tmp
.pkey
= NULL
;
2846 EVP_PKEY_free(ckey
);
2849 /* Should never happen */
2850 *al
= SSL_AD_INTERNAL_ERROR
;
2851 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE
, ERR_R_INTERNAL_ERROR
);
2856 static int tls_process_cke_ecdhe(SSL
*s
, PACKET
*pkt
, int *al
)
2858 #ifndef OPENSSL_NO_EC
2859 EVP_PKEY
*skey
= s
->s3
->tmp
.pkey
;
2860 EVP_PKEY
*ckey
= NULL
;
2863 if (PACKET_remaining(pkt
) == 0L) {
2864 /* We don't support ECDH client auth */
2865 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2866 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, SSL_R_MISSING_TMP_ECDH_KEY
);
2870 const unsigned char *data
;
2873 * Get client's public key from encoded point in the
2874 * ClientKeyExchange message.
2877 /* Get encoded point length */
2878 if (!PACKET_get_1(pkt
, &i
) || !PACKET_get_bytes(pkt
, &data
, i
)
2879 || PACKET_remaining(pkt
) != 0) {
2880 *al
= SSL_AD_DECODE_ERROR
;
2881 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, SSL_R_LENGTH_MISMATCH
);
2884 ckey
= EVP_PKEY_new();
2885 if (ckey
== NULL
|| EVP_PKEY_copy_parameters(ckey
, skey
) <= 0) {
2886 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_EVP_LIB
);
2889 if (EVP_PKEY_set1_tls_encodedpoint(ckey
, data
, i
) == 0) {
2890 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2891 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_EC_LIB
);
2896 if (ssl_derive(s
, skey
, ckey
, 1) == 0) {
2897 *al
= SSL_AD_INTERNAL_ERROR
;
2898 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_INTERNAL_ERROR
);
2903 EVP_PKEY_free(s
->s3
->tmp
.pkey
);
2904 s
->s3
->tmp
.pkey
= NULL
;
2906 EVP_PKEY_free(ckey
);
2910 /* Should never happen */
2911 *al
= SSL_AD_INTERNAL_ERROR
;
2912 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE
, ERR_R_INTERNAL_ERROR
);
2917 static int tls_process_cke_srp(SSL
*s
, PACKET
*pkt
, int *al
)
2919 #ifndef OPENSSL_NO_SRP
2921 const unsigned char *data
;
2923 if (!PACKET_get_net_2(pkt
, &i
)
2924 || !PACKET_get_bytes(pkt
, &data
, i
)) {
2925 *al
= SSL_AD_DECODE_ERROR
;
2926 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, SSL_R_BAD_SRP_A_LENGTH
);
2929 if ((s
->srp_ctx
.A
= BN_bin2bn(data
, i
, NULL
)) == NULL
) {
2930 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_BN_LIB
);
2933 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0 || BN_is_zero(s
->srp_ctx
.A
)) {
2934 *al
= SSL_AD_ILLEGAL_PARAMETER
;
2935 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, SSL_R_BAD_SRP_PARAMETERS
);
2938 OPENSSL_free(s
->session
->srp_username
);
2939 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
2940 if (s
->session
->srp_username
== NULL
) {
2941 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_MALLOC_FAILURE
);
2945 if (!srp_generate_server_master_secret(s
)) {
2946 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_INTERNAL_ERROR
);
2952 /* Should never happen */
2953 *al
= SSL_AD_INTERNAL_ERROR
;
2954 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP
, ERR_R_INTERNAL_ERROR
);
2959 static int tls_process_cke_gost(SSL
*s
, PACKET
*pkt
, int *al
)
2961 #ifndef OPENSSL_NO_GOST
2962 EVP_PKEY_CTX
*pkey_ctx
;
2963 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
2964 unsigned char premaster_secret
[32];
2965 const unsigned char *start
;
2966 size_t outlen
= 32, inlen
;
2967 unsigned long alg_a
;
2970 size_t sess_key_len
;
2971 const unsigned char *data
;
2974 /* Get our certificate private key */
2975 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2976 if (alg_a
& SSL_aGOST12
) {
2978 * New GOST ciphersuites have SSL_aGOST01 bit too
2980 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_512
].privatekey
;
2982 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST12_256
].privatekey
;
2985 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2987 } else if (alg_a
& SSL_aGOST01
) {
2988 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2991 pkey_ctx
= EVP_PKEY_CTX_new(pk
, NULL
);
2992 if (pkey_ctx
== NULL
) {
2993 *al
= SSL_AD_INTERNAL_ERROR
;
2994 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_MALLOC_FAILURE
);
2997 if (EVP_PKEY_decrypt_init(pkey_ctx
) <= 0) {
2998 *al
= SSL_AD_INTERNAL_ERROR
;
2999 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3003 * If client certificate is present and is of the same type, maybe
3004 * use it for key exchange. Don't mind errors from
3005 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3006 * client certificate for authorization only.
3008 client_pub_pkey
= X509_get0_pubkey(s
->session
->peer
);
3009 if (client_pub_pkey
) {
3010 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3013 /* Decrypt session key */
3014 sess_key_len
= PACKET_remaining(pkt
);
3015 if (!PACKET_get_bytes(pkt
, &data
, sess_key_len
)) {
3016 *al
= SSL_AD_INTERNAL_ERROR
;
3017 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3020 /* TODO(size_t): Convert this function */
3021 if (ASN1_get_object((const unsigned char **)&data
, &Tlen
, &Ttag
,
3022 &Tclass
, (long)sess_key_len
) != V_ASN1_CONSTRUCTED
3023 || Ttag
!= V_ASN1_SEQUENCE
|| Tclass
!= V_ASN1_UNIVERSAL
) {
3024 *al
= SSL_AD_DECODE_ERROR
;
3025 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, SSL_R_DECRYPTION_FAILED
);
3030 if (EVP_PKEY_decrypt
3031 (pkey_ctx
, premaster_secret
, &outlen
, start
, inlen
) <= 0) {
3032 *al
= SSL_AD_DECODE_ERROR
;
3033 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, SSL_R_DECRYPTION_FAILED
);
3036 /* Generate master secret */
3037 if (!ssl_generate_master_secret(s
, premaster_secret
,
3038 sizeof(premaster_secret
), 0)) {
3039 *al
= SSL_AD_INTERNAL_ERROR
;
3040 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3043 /* Check if pubkey from client certificate was used */
3044 if (EVP_PKEY_CTX_ctrl
3045 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
3046 s
->statem
.no_cert_verify
= 1;
3050 EVP_PKEY_CTX_free(pkey_ctx
);
3053 /* Should never happen */
3054 *al
= SSL_AD_INTERNAL_ERROR
;
3055 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST
, ERR_R_INTERNAL_ERROR
);
3060 MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
)
3063 unsigned long alg_k
;
3065 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
3067 /* For PSK parse and retrieve identity, obtain PSK key */
3068 if ((alg_k
& SSL_PSK
) && !tls_process_cke_psk_preamble(s
, pkt
, &al
))
3071 if (alg_k
& SSL_kPSK
) {
3072 /* Identity extracted earlier: should be nothing left */
3073 if (PACKET_remaining(pkt
) != 0) {
3074 al
= SSL_AD_HANDSHAKE_FAILURE
;
3075 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3076 SSL_R_LENGTH_MISMATCH
);
3079 /* PSK handled by ssl_generate_master_secret */
3080 if (!ssl_generate_master_secret(s
, NULL
, 0, 0)) {
3081 al
= SSL_AD_INTERNAL_ERROR
;
3082 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
3085 } else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
3086 if (!tls_process_cke_rsa(s
, pkt
, &al
))
3088 } else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
3089 if (!tls_process_cke_dhe(s
, pkt
, &al
))
3091 } else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
3092 if (!tls_process_cke_ecdhe(s
, pkt
, &al
))
3094 } else if (alg_k
& SSL_kSRP
) {
3095 if (!tls_process_cke_srp(s
, pkt
, &al
))
3097 } else if (alg_k
& SSL_kGOST
) {
3098 if (!tls_process_cke_gost(s
, pkt
, &al
))
3101 al
= SSL_AD_HANDSHAKE_FAILURE
;
3102 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE
,
3103 SSL_R_UNKNOWN_CIPHER_TYPE
);
3107 return MSG_PROCESS_CONTINUE_PROCESSING
;
3110 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3111 #ifndef OPENSSL_NO_PSK
3112 OPENSSL_clear_free(s
->s3
->tmp
.psk
, s
->s3
->tmp
.psklen
);
3113 s
->s3
->tmp
.psk
= NULL
;
3115 ossl_statem_set_error(s
);
3116 return MSG_PROCESS_ERROR
;
3119 WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
)
3121 #ifndef OPENSSL_NO_SCTP
3122 if (wst
== WORK_MORE_A
) {
3123 if (SSL_IS_DTLS(s
)) {
3124 unsigned char sctpauthkey
[64];
3125 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
3127 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3130 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
3131 sizeof(DTLS1_SCTP_AUTH_LABEL
));
3133 if (SSL_export_keying_material(s
, sctpauthkey
,
3134 sizeof(sctpauthkey
), labelbuffer
,
3135 sizeof(labelbuffer
), NULL
, 0,
3137 ossl_statem_set_error(s
);
3141 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
3142 sizeof(sctpauthkey
), sctpauthkey
);
3147 if ((wst
== WORK_MORE_B
)
3149 && BIO_dgram_is_sctp(SSL_get_wbio(s
))
3150 /* Are we renegotiating? */
3152 /* Are we going to skip the CertificateVerify? */
3153 && (s
->session
->peer
== NULL
|| s
->statem
.no_cert_verify
)
3154 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s
))) {
3155 s
->s3
->in_read_app_data
= 2;
3156 s
->rwstate
= SSL_READING
;
3157 BIO_clear_retry_flags(SSL_get_rbio(s
));
3158 BIO_set_retry_read(SSL_get_rbio(s
));
3159 ossl_statem_set_sctp_read_sock(s
, 1);
3162 ossl_statem_set_sctp_read_sock(s
, 0);
3166 if (s
->statem
.no_cert_verify
|| !s
->session
->peer
) {
3168 * No certificate verify or no peer certificate so we no longer need
3169 * the handshake_buffer
3171 if (!ssl3_digest_cached_records(s
, 0)) {
3172 ossl_statem_set_error(s
);
3175 return WORK_FINISHED_CONTINUE
;
3177 if (!s
->s3
->handshake_buffer
) {
3178 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE
,
3179 ERR_R_INTERNAL_ERROR
);
3180 ossl_statem_set_error(s
);
3184 * For sigalgs freeze the handshake buffer. If we support
3185 * extms we've done this already so this is a no-op
3187 if (!ssl3_digest_cached_records(s
, 1)) {
3188 ossl_statem_set_error(s
);
3193 return WORK_FINISHED_CONTINUE
;
3196 MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
)
3198 int i
, al
= SSL_AD_INTERNAL_ERROR
, ret
= MSG_PROCESS_ERROR
;
3200 unsigned long l
, llen
;
3201 const unsigned char *certstart
, *certbytes
;
3202 STACK_OF(X509
) *sk
= NULL
;
3203 PACKET spkt
, context
;
3206 if ((sk
= sk_X509_new_null()) == NULL
) {
3207 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3211 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3212 if ((SSL_IS_TLS13(s
) && !PACKET_get_length_prefixed_1(pkt
, &context
))
3213 || !PACKET_get_net_3(pkt
, &llen
)
3214 || !PACKET_get_sub_packet(pkt
, &spkt
, llen
)
3215 || PACKET_remaining(pkt
) != 0) {
3216 al
= SSL_AD_DECODE_ERROR
;
3217 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
3221 for (chainidx
= 0; PACKET_remaining(&spkt
) > 0; chainidx
++) {
3222 if (!PACKET_get_net_3(&spkt
, &l
)
3223 || !PACKET_get_bytes(&spkt
, &certbytes
, l
)) {
3224 al
= SSL_AD_DECODE_ERROR
;
3225 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3226 SSL_R_CERT_LENGTH_MISMATCH
);
3230 certstart
= certbytes
;
3231 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, l
);
3233 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_ASN1_LIB
);
3236 if (certbytes
!= (certstart
+ l
)) {
3237 al
= SSL_AD_DECODE_ERROR
;
3238 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3239 SSL_R_CERT_LENGTH_MISMATCH
);
3243 if (SSL_IS_TLS13(s
)) {
3244 RAW_EXTENSION
*rawexts
= NULL
;
3247 if (!PACKET_get_length_prefixed_2(&spkt
, &extensions
)) {
3248 al
= SSL_AD_DECODE_ERROR
;
3249 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, SSL_R_BAD_LENGTH
);
3252 if (!tls_collect_extensions(s
, &extensions
, EXT_TLS1_3_CERTIFICATE
,
3253 &rawexts
, &al
, NULL
)
3254 || !tls_parse_all_extensions(s
, EXT_TLS1_3_CERTIFICATE
,
3255 rawexts
, x
, chainidx
, &al
)) {
3256 OPENSSL_free(rawexts
);
3259 OPENSSL_free(rawexts
);
3262 if (!sk_X509_push(sk
, x
)) {
3263 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3269 if (sk_X509_num(sk
) <= 0) {
3270 /* TLS does not mind 0 certs returned */
3271 if (s
->version
== SSL3_VERSION
) {
3272 al
= SSL_AD_HANDSHAKE_FAILURE
;
3273 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3274 SSL_R_NO_CERTIFICATES_RETURNED
);
3277 /* Fail for TLS only if we required a certificate */
3278 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3279 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) {
3280 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3281 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3282 al
= SSL_AD_HANDSHAKE_FAILURE
;
3285 /* No client certificate so digest cached records */
3286 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
, 0)) {
3291 i
= ssl_verify_cert_chain(s
, sk
);
3293 al
= ssl_verify_alarm_type(s
->verify_result
);
3294 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3295 SSL_R_CERTIFICATE_VERIFY_FAILED
);
3299 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, i
);
3300 al
= SSL_AD_HANDSHAKE_FAILURE
;
3303 pkey
= X509_get0_pubkey(sk_X509_value(sk
, 0));
3305 al
= SSL3_AD_HANDSHAKE_FAILURE
;
3306 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
,
3307 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3312 X509_free(s
->session
->peer
);
3313 s
->session
->peer
= sk_X509_shift(sk
);
3314 s
->session
->verify_result
= s
->verify_result
;
3316 sk_X509_pop_free(s
->session
->peer_chain
, X509_free
);
3317 s
->session
->peer_chain
= sk
;
3320 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3323 if (SSL_IS_TLS13(s
) && !ssl3_digest_cached_records(s
, 1)) {
3324 al
= SSL_AD_INTERNAL_ERROR
;
3325 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3330 * Inconsistency alert: cert_chain does *not* include the peer's own
3331 * certificate, while we do include it in statem_clnt.c
3335 /* Save the current hash state for when we receive the CertificateVerify */
3337 && !ssl_handshake_hash(s
, s
->cert_verify_hash
,
3338 sizeof(s
->cert_verify_hash
),
3339 &s
->cert_verify_hash_len
)) {
3340 al
= SSL_AD_INTERNAL_ERROR
;
3341 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3345 ret
= MSG_PROCESS_CONTINUE_READING
;
3349 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3350 ossl_statem_set_error(s
);
3353 sk_X509_pop_free(sk
, X509_free
);
3357 int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
)
3359 CERT_PKEY
*cpk
= s
->s3
->tmp
.cert
;
3360 int al
= SSL_AD_INTERNAL_ERROR
;
3363 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3368 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3369 * for the server Certificate message
3371 if ((SSL_IS_TLS13(s
) && !WPACKET_put_bytes_u8(pkt
, 0))
3372 || !ssl3_output_cert_chain(s
, pkt
, cpk
, &al
)) {
3373 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
3374 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3381 int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
)
3383 unsigned char *senc
= NULL
;
3384 EVP_CIPHER_CTX
*ctx
= NULL
;
3385 HMAC_CTX
*hctx
= NULL
;
3386 unsigned char *p
, *encdata1
, *encdata2
, *macdata1
, *macdata2
;
3387 const unsigned char *const_p
;
3388 int len
, slen_full
, slen
, lenfinal
;
3391 SSL_CTX
*tctx
= s
->session_ctx
;
3392 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3393 unsigned char key_name
[TLSEXT_KEYNAME_LENGTH
];
3394 int iv_len
, al
= SSL_AD_INTERNAL_ERROR
;
3395 size_t macoffset
, macendoffset
;
3397 unsigned char age_add_c
[sizeof(uint32_t)];
3401 if (SSL_IS_TLS13(s
)) {
3402 if (RAND_bytes(age_add_u
.age_add_c
, sizeof(age_add_u
)) <= 0)
3404 s
->session
->ext
.tick_age_add
= age_add_u
.age_add
;
3405 s
->session
->time
= (long)time(NULL
);
3406 if (s
->s3
->alpn_selected
!= NULL
) {
3407 OPENSSL_free(s
->session
->ext
.alpn_selected
);
3408 s
->session
->ext
.alpn_selected
=
3409 OPENSSL_memdup(s
->s3
->alpn_selected
, s
->s3
->alpn_selected_len
);
3410 if (s
->session
->ext
.alpn_selected
== NULL
) {
3411 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3412 ERR_R_MALLOC_FAILURE
);
3415 s
->session
->ext
.alpn_selected_len
= s
->s3
->alpn_selected_len
;
3417 s
->session
->ext
.max_early_data
= s
->max_early_data
;
3420 /* get session encoding length */
3421 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3423 * Some length values are 16 bits, so forget it if session is too
3426 if (slen_full
== 0 || slen_full
> 0xFF00) {
3427 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3430 senc
= OPENSSL_malloc(slen_full
);
3432 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
3436 ctx
= EVP_CIPHER_CTX_new();
3437 hctx
= HMAC_CTX_new();
3438 if (ctx
== NULL
|| hctx
== NULL
) {
3439 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
3444 if (!i2d_SSL_SESSION(s
->session
, &p
))
3448 * create a fresh copy (not shared with other threads) to clean up
3451 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3454 sess
->session_id_length
= 0; /* ID is irrelevant for the ticket */
3456 slen
= i2d_SSL_SESSION(sess
, NULL
);
3457 if (slen
== 0 || slen
> slen_full
) { /* shouldn't ever happen */
3458 SSL_SESSION_free(sess
);
3462 if (!i2d_SSL_SESSION(sess
, &p
)) {
3463 SSL_SESSION_free(sess
);
3466 SSL_SESSION_free(sess
);
3469 * Initialize HMAC and cipher contexts. If callback present it does
3470 * all the work otherwise use generated values from parent ctx.
3472 if (tctx
->ext
.ticket_key_cb
) {
3473 /* if 0 is returned, write an empty ticket */
3474 int ret
= tctx
->ext
.ticket_key_cb(s
, key_name
, iv
, ctx
,
3479 /* Put timeout and length */
3480 if (!WPACKET_put_bytes_u32(pkt
, 0)
3481 || !WPACKET_put_bytes_u16(pkt
, 0)) {
3482 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
,
3483 ERR_R_INTERNAL_ERROR
);
3487 EVP_CIPHER_CTX_free(ctx
);
3488 HMAC_CTX_free(hctx
);
3493 iv_len
= EVP_CIPHER_CTX_iv_length(ctx
);
3495 const EVP_CIPHER
*cipher
= EVP_aes_256_cbc();
3497 iv_len
= EVP_CIPHER_iv_length(cipher
);
3498 if (RAND_bytes(iv
, iv_len
) <= 0)
3500 if (!EVP_EncryptInit_ex(ctx
, cipher
, NULL
,
3501 tctx
->ext
.tick_aes_key
, iv
))
3503 if (!HMAC_Init_ex(hctx
, tctx
->ext
.tick_hmac_key
,
3504 sizeof(tctx
->ext
.tick_hmac_key
),
3505 EVP_sha256(), NULL
))
3507 memcpy(key_name
, tctx
->ext
.tick_key_name
,
3508 sizeof(tctx
->ext
.tick_key_name
));
3512 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3513 * unspecified for resumed session (for simplicity).
3514 * In TLSv1.3 we reset the "time" field above, and always specify the
3517 if (!WPACKET_put_bytes_u32(pkt
,
3518 (s
->hit
&& !SSL_IS_TLS13(s
))
3519 ? 0 : s
->session
->timeout
)
3521 && !WPACKET_put_bytes_u32(pkt
, age_add_u
.age_add
))
3522 /* Now the actual ticket data */
3523 || !WPACKET_start_sub_packet_u16(pkt
)
3524 || !WPACKET_get_total_written(pkt
, &macoffset
)
3525 /* Output key name */
3526 || !WPACKET_memcpy(pkt
, key_name
, sizeof(key_name
))
3528 || !WPACKET_memcpy(pkt
, iv
, iv_len
)
3529 || !WPACKET_reserve_bytes(pkt
, slen
+ EVP_MAX_BLOCK_LENGTH
,
3531 /* Encrypt session data */
3532 || !EVP_EncryptUpdate(ctx
, encdata1
, &len
, senc
, slen
)
3533 || !WPACKET_allocate_bytes(pkt
, len
, &encdata2
)
3534 || encdata1
!= encdata2
3535 || !EVP_EncryptFinal(ctx
, encdata1
+ len
, &lenfinal
)
3536 || !WPACKET_allocate_bytes(pkt
, lenfinal
, &encdata2
)
3537 || encdata1
+ len
!= encdata2
3538 || len
+ lenfinal
> slen
+ EVP_MAX_BLOCK_LENGTH
3539 || !WPACKET_get_total_written(pkt
, &macendoffset
)
3540 || !HMAC_Update(hctx
,
3541 (unsigned char *)s
->init_buf
->data
+ macoffset
,
3542 macendoffset
- macoffset
)
3543 || !WPACKET_reserve_bytes(pkt
, EVP_MAX_MD_SIZE
, &macdata1
)
3544 || !HMAC_Final(hctx
, macdata1
, &hlen
)
3545 || hlen
> EVP_MAX_MD_SIZE
3546 || !WPACKET_allocate_bytes(pkt
, hlen
, &macdata2
)
3547 || macdata1
!= macdata2
3548 || !WPACKET_close(pkt
)
3550 && !tls_construct_extensions(s
, pkt
,
3551 EXT_TLS1_3_NEW_SESSION_TICKET
,
3553 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET
, ERR_R_INTERNAL_ERROR
);
3556 EVP_CIPHER_CTX_free(ctx
);
3557 HMAC_CTX_free(hctx
);
3562 ossl_statem_set_error(s
);
3564 EVP_CIPHER_CTX_free(ctx
);
3565 HMAC_CTX_free(hctx
);
3566 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3571 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3572 * create a separate message. Returns 1 on success or 0 on failure.
3574 int tls_construct_cert_status_body(SSL
*s
, WPACKET
*pkt
)
3576 if (!WPACKET_put_bytes_u8(pkt
, s
->ext
.status_type
)
3577 || !WPACKET_sub_memcpy_u24(pkt
, s
->ext
.ocsp
.resp
,
3578 s
->ext
.ocsp
.resp_len
)) {
3579 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY
, ERR_R_INTERNAL_ERROR
);
3586 int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
)
3588 if (!tls_construct_cert_status_body(s
, pkt
)) {
3589 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
3596 #ifndef OPENSSL_NO_NEXTPROTONEG
3598 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3599 * It sets the next_proto member in s if found
3601 MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
)
3603 PACKET next_proto
, padding
;
3604 size_t next_proto_len
;
3607 * The payload looks like:
3609 * uint8 proto[proto_len];
3610 * uint8 padding_len;
3611 * uint8 padding[padding_len];
3613 if (!PACKET_get_length_prefixed_1(pkt
, &next_proto
)
3614 || !PACKET_get_length_prefixed_1(pkt
, &padding
)
3615 || PACKET_remaining(pkt
) > 0) {
3616 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO
, SSL_R_LENGTH_MISMATCH
);
3620 if (!PACKET_memdup(&next_proto
, &s
->ext
.npn
, &next_proto_len
)) {
3625 s
->ext
.npn_len
= (unsigned char)next_proto_len
;
3627 return MSG_PROCESS_CONTINUE_READING
;
3629 ossl_statem_set_error(s
);
3630 return MSG_PROCESS_ERROR
;
3634 static int tls_construct_encrypted_extensions(SSL
*s
, WPACKET
*pkt
)
3638 if (!tls_construct_extensions(s
, pkt
, EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
3640 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3641 SSLerr(SSL_F_TLS_CONSTRUCT_ENCRYPTED_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
3642 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3649 static int tls_construct_hello_retry_request(SSL
*s
, WPACKET
*pkt
)
3651 int al
= SSL_AD_INTERNAL_ERROR
;
3654 * TODO(TLS1.3): Remove the DRAFT version before release
3655 * (should be s->version)
3657 if (!WPACKET_put_bytes_u16(pkt
, TLS1_3_VERSION_DRAFT
)
3658 || !tls_construct_extensions(s
, pkt
, EXT_TLS1_3_HELLO_RETRY_REQUEST
,
3660 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST
, ERR_R_INTERNAL_ERROR
);
3661 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
3665 /* Ditch the session. We'll create a new one next time around */
3666 SSL_SESSION_free(s
->session
);