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_SW_FINISHED:
97 if (s->s3->tmp.cert_request) {
98 if (mt == SSL3_MT_CERTIFICATE) {
99 st->hand_state = TLS_ST_SR_CERT;
103 if (mt == SSL3_MT_FINISHED) {
104 st->hand_state = TLS_ST_SR_FINISHED;
111 if (s->session->peer == NULL) {
112 if (mt == SSL3_MT_FINISHED) {
113 st->hand_state = TLS_ST_SR_FINISHED;
117 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
118 st->hand_state = TLS_ST_SR_CERT_VRFY;
124 case TLS_ST_SR_CERT_VRFY:
125 if (mt == SSL3_MT_FINISHED) {
126 st->hand_state = TLS_ST_SR_FINISHED;
132 if (mt == SSL3_MT_KEY_UPDATE) {
133 st->hand_state = TLS_ST_SR_KEY_UPDATE;
139 /* No valid transition found */
140 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
141 SSLerr(SSL_F_OSSL_STATEM_SERVER13_READ_TRANSITION,
142 SSL_R_UNEXPECTED_MESSAGE);
147 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
148 * handshake state transitions when the server is reading messages from the
149 * client. The message type that the client has sent is provided in |mt|. The
150 * current state is in |s->statem.hand_state|.
152 * Return values are 1 for success (transition allowed) and 0 on error
153 * (transition not allowed)
155 int ossl_statem_server_read_transition(SSL *s, int mt)
157 OSSL_STATEM *st = &s->statem;
159 if (SSL_IS_TLS13(s)) {
160 if (!ossl_statem_server13_read_transition(s, mt))
165 switch (st->hand_state) {
171 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
172 if (mt == SSL3_MT_CLIENT_HELLO) {
173 st->hand_state = TLS_ST_SR_CLNT_HELLO;
178 case TLS_ST_SW_SRVR_DONE:
180 * If we get a CKE message after a ServerDone then either
181 * 1) We didn't request a Certificate
183 * 2) If we did request one then
184 * a) We allow no Certificate to be returned
186 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
187 * list if we requested a certificate)
189 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
190 if (s->s3->tmp.cert_request) {
191 if (s->version == SSL3_VERSION) {
192 if ((s->verify_mode & SSL_VERIFY_PEER)
193 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
195 * This isn't an unexpected message as such - we're just
196 * not going to accept it because we require a client
199 ssl3_send_alert(s, SSL3_AL_FATAL,
200 SSL3_AD_HANDSHAKE_FAILURE);
201 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
202 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
205 st->hand_state = TLS_ST_SR_KEY_EXCH;
209 st->hand_state = TLS_ST_SR_KEY_EXCH;
212 } else if (s->s3->tmp.cert_request) {
213 if (mt == SSL3_MT_CERTIFICATE) {
214 st->hand_state = TLS_ST_SR_CERT;
221 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
222 st->hand_state = TLS_ST_SR_KEY_EXCH;
227 case TLS_ST_SR_KEY_EXCH:
229 * We should only process a CertificateVerify message if we have
230 * received a Certificate from the client. If so then |s->session->peer|
231 * will be non NULL. In some instances a CertificateVerify message is
232 * not required even if the peer has sent a Certificate (e.g. such as in
233 * the case of static DH). In that case |st->no_cert_verify| should be
236 if (s->session->peer == NULL || st->no_cert_verify) {
237 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
239 * For the ECDH ciphersuites when the client sends its ECDH
240 * pub key in a certificate, the CertificateVerify message is
241 * not sent. Also for GOST ciphersuites when the client uses
242 * its key from the certificate for key exchange.
244 st->hand_state = TLS_ST_SR_CHANGE;
248 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
249 st->hand_state = TLS_ST_SR_CERT_VRFY;
255 case TLS_ST_SR_CERT_VRFY:
256 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
257 st->hand_state = TLS_ST_SR_CHANGE;
262 case TLS_ST_SR_CHANGE:
263 #ifndef OPENSSL_NO_NEXTPROTONEG
264 if (s->s3->npn_seen) {
265 if (mt == SSL3_MT_NEXT_PROTO) {
266 st->hand_state = TLS_ST_SR_NEXT_PROTO;
271 if (mt == SSL3_MT_FINISHED) {
272 st->hand_state = TLS_ST_SR_FINISHED;
275 #ifndef OPENSSL_NO_NEXTPROTONEG
280 #ifndef OPENSSL_NO_NEXTPROTONEG
281 case TLS_ST_SR_NEXT_PROTO:
282 if (mt == SSL3_MT_FINISHED) {
283 st->hand_state = TLS_ST_SR_FINISHED;
289 case TLS_ST_SW_FINISHED:
290 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
291 st->hand_state = TLS_ST_SR_CHANGE;
298 /* No valid transition found */
299 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
300 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
305 * Should we send a ServerKeyExchange message?
307 * Valid return values are:
311 static int send_server_key_exchange(SSL *s)
313 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
316 * only send a ServerKeyExchange if DH or fortezza but we have a
317 * sign only certificate PSK: may send PSK identity hints For
318 * ECC ciphersuites, we send a serverKeyExchange message only if
319 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
320 * the server certificate contains the server's public key for
323 if (alg_k & (SSL_kDHE | SSL_kECDHE)
325 * PSK: send ServerKeyExchange if PSK identity hint if
328 #ifndef OPENSSL_NO_PSK
329 /* Only send SKE if we have identity hint for plain PSK */
330 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
331 && s->cert->psk_identity_hint)
332 /* For other PSK always send SKE */
333 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
335 #ifndef OPENSSL_NO_SRP
336 /* SRP: send ServerKeyExchange */
337 || (alg_k & SSL_kSRP)
347 * Should we send a CertificateRequest message?
349 * Valid return values are:
353 static int send_certificate_request(SSL *s)
356 /* don't request cert unless asked for it: */
357 s->verify_mode & SSL_VERIFY_PEER
359 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
360 * during re-negotiation:
362 && (s->s3->tmp.finish_md_len == 0 ||
363 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
365 * never request cert in anonymous ciphersuites (see
366 * section "Certificate request" in SSL 3 drafts and in
369 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
371 * ... except when the application insists on
372 * verification (against the specs, but statem_clnt.c accepts
375 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
376 /* don't request certificate for SRP auth */
377 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
379 * With normal PSK Certificates and Certificate Requests
382 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
390 * ossl_statem_server13_write_transition() works out what handshake state to
391 * move to next when a TLSv1.3 server is writing messages to be sent to the
394 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
396 OSSL_STATEM *st = &s->statem;
399 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
400 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
403 switch (st->hand_state) {
405 /* Shouldn't happen */
406 return WRITE_TRAN_ERROR;
409 if (s->key_update != SSL_KEY_UPDATE_NONE) {
410 st->hand_state = TLS_ST_SW_KEY_UPDATE;
411 return WRITE_TRAN_CONTINUE;
413 /* Try to read from the client instead */
414 return WRITE_TRAN_FINISHED;
416 case TLS_ST_SR_CLNT_HELLO:
417 if (s->hello_retry_request)
418 st->hand_state = TLS_ST_SW_HELLO_RETRY_REQUEST;
420 st->hand_state = TLS_ST_SW_SRVR_HELLO;
421 return WRITE_TRAN_CONTINUE;
423 case TLS_ST_SW_HELLO_RETRY_REQUEST:
424 return WRITE_TRAN_FINISHED;
426 case TLS_ST_SW_SRVR_HELLO:
427 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
428 return WRITE_TRAN_CONTINUE;
430 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
432 st->hand_state = TLS_ST_SW_FINISHED;
433 else if (send_certificate_request(s))
434 st->hand_state = TLS_ST_SW_CERT_REQ;
436 st->hand_state = TLS_ST_SW_CERT;
438 return WRITE_TRAN_CONTINUE;
440 case TLS_ST_SW_CERT_REQ:
441 st->hand_state = TLS_ST_SW_CERT;
442 return WRITE_TRAN_CONTINUE;
445 st->hand_state = TLS_ST_SW_CERT_VRFY;
446 return WRITE_TRAN_CONTINUE;
448 case TLS_ST_SW_CERT_VRFY:
449 st->hand_state = TLS_ST_SW_FINISHED;
450 return WRITE_TRAN_CONTINUE;
452 case TLS_ST_SW_FINISHED:
453 return WRITE_TRAN_FINISHED;
455 case TLS_ST_SR_FINISHED:
457 * Technically we have finished the handshake at this point, but we're
458 * going to remain "in_init" for now and write out the session ticket
460 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
461 * and give the application the opportunity to delay sending the
464 st->hand_state = TLS_ST_SW_SESSION_TICKET;
465 return WRITE_TRAN_CONTINUE;
467 case TLS_ST_SR_KEY_UPDATE:
468 if (s->key_update != SSL_KEY_UPDATE_NONE) {
469 st->hand_state = TLS_ST_SW_KEY_UPDATE;
470 return WRITE_TRAN_CONTINUE;
474 case TLS_ST_SW_KEY_UPDATE:
475 case TLS_ST_SW_SESSION_TICKET:
476 st->hand_state = TLS_ST_OK;
477 ossl_statem_set_in_init(s, 0);
478 return WRITE_TRAN_CONTINUE;
483 * ossl_statem_server_write_transition() works out what handshake state to move
484 * to next when the server is writing messages to be sent to the client.
486 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
488 OSSL_STATEM *st = &s->statem;
491 * Note that before the ClientHello we don't know what version we are going
492 * to negotiate yet, so we don't take this branch until later
496 return ossl_statem_server13_write_transition(s);
498 switch (st->hand_state) {
500 /* Shouldn't happen */
501 return WRITE_TRAN_ERROR;
504 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
505 /* We must be trying to renegotiate */
506 st->hand_state = TLS_ST_SW_HELLO_REQ;
507 st->request_state = TLS_ST_BEFORE;
508 return WRITE_TRAN_CONTINUE;
510 /* Must be an incoming ClientHello */
511 if (!tls_setup_handshake(s)) {
512 ossl_statem_set_error(s);
513 return WRITE_TRAN_ERROR;
518 /* Just go straight to trying to read from the client */
519 return WRITE_TRAN_FINISHED;
521 case TLS_ST_SW_HELLO_REQ:
522 st->hand_state = TLS_ST_OK;
523 ossl_statem_set_in_init(s, 0);
524 return WRITE_TRAN_CONTINUE;
526 case TLS_ST_SR_CLNT_HELLO:
527 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
528 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
529 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
531 st->hand_state = TLS_ST_SW_SRVR_HELLO;
532 return WRITE_TRAN_CONTINUE;
534 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
535 return WRITE_TRAN_FINISHED;
537 case TLS_ST_SW_SRVR_HELLO:
539 if (s->ext.ticket_expected)
540 st->hand_state = TLS_ST_SW_SESSION_TICKET;
542 st->hand_state = TLS_ST_SW_CHANGE;
544 /* Check if it is anon DH or anon ECDH, */
545 /* normal PSK or SRP */
546 if (!(s->s3->tmp.new_cipher->algorithm_auth &
547 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
548 st->hand_state = TLS_ST_SW_CERT;
549 } else if (send_server_key_exchange(s)) {
550 st->hand_state = TLS_ST_SW_KEY_EXCH;
551 } else if (send_certificate_request(s)) {
552 st->hand_state = TLS_ST_SW_CERT_REQ;
554 st->hand_state = TLS_ST_SW_SRVR_DONE;
557 return WRITE_TRAN_CONTINUE;
560 if (s->ext.status_expected) {
561 st->hand_state = TLS_ST_SW_CERT_STATUS;
562 return WRITE_TRAN_CONTINUE;
566 case TLS_ST_SW_CERT_STATUS:
567 if (send_server_key_exchange(s)) {
568 st->hand_state = TLS_ST_SW_KEY_EXCH;
569 return WRITE_TRAN_CONTINUE;
573 case TLS_ST_SW_KEY_EXCH:
574 if (send_certificate_request(s)) {
575 st->hand_state = TLS_ST_SW_CERT_REQ;
576 return WRITE_TRAN_CONTINUE;
580 case TLS_ST_SW_CERT_REQ:
581 st->hand_state = TLS_ST_SW_SRVR_DONE;
582 return WRITE_TRAN_CONTINUE;
584 case TLS_ST_SW_SRVR_DONE:
585 return WRITE_TRAN_FINISHED;
587 case TLS_ST_SR_FINISHED:
589 st->hand_state = TLS_ST_OK;
590 ossl_statem_set_in_init(s, 0);
591 return WRITE_TRAN_CONTINUE;
592 } else if (s->ext.ticket_expected) {
593 st->hand_state = TLS_ST_SW_SESSION_TICKET;
595 st->hand_state = TLS_ST_SW_CHANGE;
597 return WRITE_TRAN_CONTINUE;
599 case TLS_ST_SW_SESSION_TICKET:
600 st->hand_state = TLS_ST_SW_CHANGE;
601 return WRITE_TRAN_CONTINUE;
603 case TLS_ST_SW_CHANGE:
604 st->hand_state = TLS_ST_SW_FINISHED;
605 return WRITE_TRAN_CONTINUE;
607 case TLS_ST_SW_FINISHED:
609 return WRITE_TRAN_FINISHED;
611 st->hand_state = TLS_ST_OK;
612 ossl_statem_set_in_init(s, 0);
613 return WRITE_TRAN_CONTINUE;
618 * Perform any pre work that needs to be done prior to sending a message from
619 * the server to the client.
621 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
623 OSSL_STATEM *st = &s->statem;
625 switch (st->hand_state) {
627 /* No pre work to be done */
630 case TLS_ST_SW_HELLO_REQ:
633 dtls1_clear_sent_buffer(s);
636 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
638 if (SSL_IS_DTLS(s)) {
639 dtls1_clear_sent_buffer(s);
640 /* We don't buffer this message so don't use the timer */
645 case TLS_ST_SW_SRVR_HELLO:
646 if (SSL_IS_DTLS(s)) {
648 * Messages we write from now on should be bufferred and
649 * retransmitted if necessary, so we need to use the timer now
655 case TLS_ST_SW_SRVR_DONE:
656 #ifndef OPENSSL_NO_SCTP
657 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
658 return dtls_wait_for_dry(s);
660 return WORK_FINISHED_CONTINUE;
662 case TLS_ST_SW_SESSION_TICKET:
663 if (SSL_IS_TLS13(s)) {
665 * Actually this is the end of the handshake, but we're going
666 * straight into writing the session ticket out. So we finish off
667 * the handshake, but keep the various buffers active.
669 return tls_finish_handshake(s, wst, 0);
670 } if (SSL_IS_DTLS(s)) {
672 * We're into the last flight. We don't retransmit the last flight
673 * unless we need to, so we don't use the timer
679 case TLS_ST_SW_CHANGE:
680 s->session->cipher = s->s3->tmp.new_cipher;
681 if (!s->method->ssl3_enc->setup_key_block(s)) {
682 ossl_statem_set_error(s);
685 if (SSL_IS_DTLS(s)) {
687 * We're into the last flight. We don't retransmit the last flight
688 * unless we need to, so we don't use the timer. This might have
689 * already been set to 0 if we sent a NewSessionTicket message,
690 * but we'll set it again here in case we didn't.
694 return WORK_FINISHED_CONTINUE;
697 return tls_finish_handshake(s, wst, 1);
700 return WORK_FINISHED_CONTINUE;
704 * Perform any work that needs to be done after sending a message from the
705 * server to the client.
707 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
709 OSSL_STATEM *st = &s->statem;
713 switch (st->hand_state) {
715 /* No post work to be done */
718 case TLS_ST_SW_HELLO_RETRY_REQUEST:
719 if (statem_flush(s) != 1)
723 case TLS_ST_SW_HELLO_REQ:
724 if (statem_flush(s) != 1)
726 if (!ssl3_init_finished_mac(s)) {
727 ossl_statem_set_error(s);
732 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
733 if (statem_flush(s) != 1)
735 /* HelloVerifyRequest resets Finished MAC */
736 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
737 ossl_statem_set_error(s);
741 * The next message should be another ClientHello which we need to
742 * treat like it was the first packet
747 case TLS_ST_SW_SRVR_HELLO:
748 #ifndef OPENSSL_NO_SCTP
749 if (SSL_IS_DTLS(s) && s->hit) {
750 unsigned char sctpauthkey[64];
751 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
754 * Add new shared key for SCTP-Auth, will be ignored if no
757 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
758 sizeof(DTLS1_SCTP_AUTH_LABEL));
760 if (SSL_export_keying_material(s, sctpauthkey,
761 sizeof(sctpauthkey), labelbuffer,
762 sizeof(labelbuffer), NULL, 0,
764 ossl_statem_set_error(s);
768 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
769 sizeof(sctpauthkey), sctpauthkey);
773 * TODO(TLS1.3): This actually causes a problem. We don't yet know
774 * whether the next record we are going to receive is an unencrypted
775 * alert, or an encrypted handshake message. We're going to need
776 * something clever in the record layer for this.
778 if (SSL_IS_TLS13(s)) {
779 if (!s->method->ssl3_enc->setup_key_block(s)
780 || !s->method->ssl3_enc->change_cipher_state(s,
781 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)
782 || !s->method->ssl3_enc->change_cipher_state(s,
783 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ))
788 case TLS_ST_SW_CHANGE:
789 #ifndef OPENSSL_NO_SCTP
790 if (SSL_IS_DTLS(s) && !s->hit) {
792 * Change to new shared key of SCTP-Auth, will be ignored if
795 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
799 if (!s->method->ssl3_enc->change_cipher_state(s,
800 SSL3_CHANGE_CIPHER_SERVER_WRITE))
802 ossl_statem_set_error(s);
807 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
810 case TLS_ST_SW_SRVR_DONE:
811 if (statem_flush(s) != 1)
815 case TLS_ST_SW_FINISHED:
816 if (statem_flush(s) != 1)
818 #ifndef OPENSSL_NO_SCTP
819 if (SSL_IS_DTLS(s) && s->hit) {
821 * Change to new shared key of SCTP-Auth, will be ignored if
824 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
828 if (SSL_IS_TLS13(s)) {
829 if (!s->method->ssl3_enc->generate_master_secret(s,
830 s->master_secret, s->handshake_secret, 0,
831 &s->session->master_key_length)
832 || !s->method->ssl3_enc->change_cipher_state(s,
833 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
838 case TLS_ST_SW_KEY_UPDATE:
839 if (statem_flush(s) != 1)
841 if (!tls13_update_key(s, 1))
845 case TLS_ST_SW_SESSION_TICKET:
846 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
851 return WORK_FINISHED_CONTINUE;
855 * Get the message construction function and message type for sending from the
858 * Valid return values are:
862 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
863 confunc_f *confunc, int *mt)
865 OSSL_STATEM *st = &s->statem;
867 switch (st->hand_state) {
869 /* Shouldn't happen */
872 case TLS_ST_SW_CHANGE:
874 *confunc = dtls_construct_change_cipher_spec;
876 *confunc = tls_construct_change_cipher_spec;
877 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
880 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
881 *confunc = dtls_construct_hello_verify_request;
882 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
885 case TLS_ST_SW_HELLO_REQ:
886 /* No construction function needed */
888 *mt = SSL3_MT_HELLO_REQUEST;
891 case TLS_ST_SW_SRVR_HELLO:
892 *confunc = tls_construct_server_hello;
893 *mt = SSL3_MT_SERVER_HELLO;
897 *confunc = tls_construct_server_certificate;
898 *mt = SSL3_MT_CERTIFICATE;
901 case TLS_ST_SW_CERT_VRFY:
902 *confunc = tls_construct_cert_verify;
903 *mt = SSL3_MT_CERTIFICATE_VERIFY;
907 case TLS_ST_SW_KEY_EXCH:
908 *confunc = tls_construct_server_key_exchange;
909 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
912 case TLS_ST_SW_CERT_REQ:
913 *confunc = tls_construct_certificate_request;
914 *mt = SSL3_MT_CERTIFICATE_REQUEST;
917 case TLS_ST_SW_SRVR_DONE:
918 *confunc = tls_construct_server_done;
919 *mt = SSL3_MT_SERVER_DONE;
922 case TLS_ST_SW_SESSION_TICKET:
923 *confunc = tls_construct_new_session_ticket;
924 *mt = SSL3_MT_NEWSESSION_TICKET;
927 case TLS_ST_SW_CERT_STATUS:
928 *confunc = tls_construct_cert_status;
929 *mt = SSL3_MT_CERTIFICATE_STATUS;
932 case TLS_ST_SW_FINISHED:
933 *confunc = tls_construct_finished;
934 *mt = SSL3_MT_FINISHED;
937 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
938 *confunc = tls_construct_encrypted_extensions;
939 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
942 case TLS_ST_SW_HELLO_RETRY_REQUEST:
943 *confunc = tls_construct_hello_retry_request;
944 *mt = SSL3_MT_HELLO_RETRY_REQUEST;
947 case TLS_ST_SW_KEY_UPDATE:
948 *confunc = tls_construct_key_update;
949 *mt = SSL3_MT_KEY_UPDATE;
957 * Maximum size (excluding the Handshake header) of a ClientHello message,
958 * calculated as follows:
960 * 2 + # client_version
961 * 32 + # only valid length for random
962 * 1 + # length of session_id
963 * 32 + # maximum size for session_id
964 * 2 + # length of cipher suites
965 * 2^16-2 + # maximum length of cipher suites array
966 * 1 + # length of compression_methods
967 * 2^8-1 + # maximum length of compression methods
968 * 2 + # length of extensions
969 * 2^16-1 # maximum length of extensions
971 #define CLIENT_HELLO_MAX_LENGTH 131396
973 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
974 #define NEXT_PROTO_MAX_LENGTH 514
977 * Returns the maximum allowed length for the current message that we are
978 * reading. Excludes the message header.
980 size_t ossl_statem_server_max_message_size(SSL *s)
982 OSSL_STATEM *st = &s->statem;
984 switch (st->hand_state) {
986 /* Shouldn't happen */
989 case TLS_ST_SR_CLNT_HELLO:
990 return CLIENT_HELLO_MAX_LENGTH;
993 return s->max_cert_list;
995 case TLS_ST_SR_KEY_EXCH:
996 return CLIENT_KEY_EXCH_MAX_LENGTH;
998 case TLS_ST_SR_CERT_VRFY:
999 return SSL3_RT_MAX_PLAIN_LENGTH;
1001 #ifndef OPENSSL_NO_NEXTPROTONEG
1002 case TLS_ST_SR_NEXT_PROTO:
1003 return NEXT_PROTO_MAX_LENGTH;
1006 case TLS_ST_SR_CHANGE:
1007 return CCS_MAX_LENGTH;
1009 case TLS_ST_SR_FINISHED:
1010 return FINISHED_MAX_LENGTH;
1012 case TLS_ST_SR_KEY_UPDATE:
1013 return KEY_UPDATE_MAX_LENGTH;
1018 * Process a message that the server has received from the client.
1020 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1022 OSSL_STATEM *st = &s->statem;
1024 switch (st->hand_state) {
1026 /* Shouldn't happen */
1027 return MSG_PROCESS_ERROR;
1029 case TLS_ST_SR_CLNT_HELLO:
1030 return tls_process_client_hello(s, pkt);
1032 case TLS_ST_SR_CERT:
1033 return tls_process_client_certificate(s, pkt);
1035 case TLS_ST_SR_KEY_EXCH:
1036 return tls_process_client_key_exchange(s, pkt);
1038 case TLS_ST_SR_CERT_VRFY:
1039 return tls_process_cert_verify(s, pkt);
1041 #ifndef OPENSSL_NO_NEXTPROTONEG
1042 case TLS_ST_SR_NEXT_PROTO:
1043 return tls_process_next_proto(s, pkt);
1046 case TLS_ST_SR_CHANGE:
1047 return tls_process_change_cipher_spec(s, pkt);
1049 case TLS_ST_SR_FINISHED:
1050 return tls_process_finished(s, pkt);
1052 case TLS_ST_SR_KEY_UPDATE:
1053 return tls_process_key_update(s, pkt);
1059 * Perform any further processing required following the receipt of a message
1062 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1064 OSSL_STATEM *st = &s->statem;
1066 switch (st->hand_state) {
1068 /* Shouldn't happen */
1071 case TLS_ST_SR_CLNT_HELLO:
1072 return tls_post_process_client_hello(s, wst);
1074 case TLS_ST_SR_KEY_EXCH:
1075 return tls_post_process_client_key_exchange(s, wst);
1077 case TLS_ST_SR_CERT_VRFY:
1078 #ifndef OPENSSL_NO_SCTP
1079 if ( /* Is this SCTP? */
1080 BIO_dgram_is_sctp(SSL_get_wbio(s))
1081 /* Are we renegotiating? */
1082 && s->renegotiate && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
1083 s->s3->in_read_app_data = 2;
1084 s->rwstate = SSL_READING;
1085 BIO_clear_retry_flags(SSL_get_rbio(s));
1086 BIO_set_retry_read(SSL_get_rbio(s));
1087 ossl_statem_set_sctp_read_sock(s, 1);
1090 ossl_statem_set_sctp_read_sock(s, 0);
1093 return WORK_FINISHED_CONTINUE;
1095 return WORK_FINISHED_CONTINUE;
1098 #ifndef OPENSSL_NO_SRP
1099 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
1101 int ret = SSL_ERROR_NONE;
1103 *al = SSL_AD_UNRECOGNIZED_NAME;
1105 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1106 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1107 if (s->srp_ctx.login == NULL) {
1109 * RFC 5054 says SHOULD reject, we do so if There is no srp
1112 ret = SSL3_AL_FATAL;
1113 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1115 ret = SSL_srp_server_param_with_username(s, al);
1122 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1125 /* Always use DTLS 1.0 version: see RFC 6347 */
1126 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1127 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1133 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1135 unsigned int cookie_leni;
1136 if (s->ctx->app_gen_cookie_cb == NULL ||
1137 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1138 &cookie_leni) == 0 ||
1139 cookie_leni > 255) {
1140 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1141 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1144 s->d1->cookie_len = cookie_leni;
1146 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1147 s->d1->cookie_len)) {
1148 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, ERR_R_INTERNAL_ERROR);
1155 #ifndef OPENSSL_NO_EC
1157 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1158 * SecureTransport using the TLS extension block in |hello|.
1159 * Safari, since 10.6, sends exactly these extensions, in this order:
1164 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1165 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1166 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1167 * 10.8..10.8.3 (which don't work).
1169 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1171 static const unsigned char kSafariExtensionsBlock[] = {
1172 0x00, 0x0a, /* elliptic_curves extension */
1173 0x00, 0x08, /* 8 bytes */
1174 0x00, 0x06, /* 6 bytes of curve ids */
1175 0x00, 0x17, /* P-256 */
1176 0x00, 0x18, /* P-384 */
1177 0x00, 0x19, /* P-521 */
1179 0x00, 0x0b, /* ec_point_formats */
1180 0x00, 0x02, /* 2 bytes */
1181 0x01, /* 1 point format */
1182 0x00, /* uncompressed */
1183 /* The following is only present in TLS 1.2 */
1184 0x00, 0x0d, /* signature_algorithms */
1185 0x00, 0x0c, /* 12 bytes */
1186 0x00, 0x0a, /* 10 bytes */
1187 0x05, 0x01, /* SHA-384/RSA */
1188 0x04, 0x01, /* SHA-256/RSA */
1189 0x02, 0x01, /* SHA-1/RSA */
1190 0x04, 0x03, /* SHA-256/ECDSA */
1191 0x02, 0x03, /* SHA-1/ECDSA */
1193 /* Length of the common prefix (first two extensions). */
1194 static const size_t kSafariCommonExtensionsLength = 18;
1199 tmppkt = hello->extensions;
1201 if (!PACKET_forward(&tmppkt, 2)
1202 || !PACKET_get_net_2(&tmppkt, &type)
1203 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1207 if (type != TLSEXT_TYPE_server_name)
1210 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1211 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1213 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1216 #endif /* !OPENSSL_NO_EC */
1218 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1220 int al = SSL_AD_INTERNAL_ERROR;
1221 /* |cookie| will only be initialized for DTLS. */
1222 PACKET session_id, compression, extensions, cookie;
1223 static const unsigned char null_compression = 0;
1224 CLIENTHELLO_MSG *clienthello;
1226 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1227 if (clienthello == NULL) {
1228 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1231 /* Check if this is actually an unexpected renegotiation ClientHello */
1232 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1237 /* This is a real handshake so make sure we clean it up at the end */
1238 s->statem.cleanuphand = 1;
1241 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1243 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1244 PACKET_null_init(&cookie);
1246 if (clienthello->isv2) {
1249 if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
1250 al = SSL_AD_HANDSHAKE_FAILURE;
1251 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1256 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1257 * header is sent directly on the wire, not wrapped as a TLS
1258 * record. Our record layer just processes the message length and passes
1259 * the rest right through. Its format is:
1261 * 0-1 msg_length - decoded by the record layer
1262 * 2 msg_type - s->init_msg points here
1264 * 5-6 cipher_spec_length
1265 * 7-8 session_id_length
1266 * 9-10 challenge_length
1270 if (!PACKET_get_1(pkt, &mt)
1271 || mt != SSL2_MT_CLIENT_HELLO) {
1273 * Should never happen. We should have tested this in the record
1274 * layer in order to have determined that this is a SSLv2 record
1275 * in the first place
1277 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1282 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1283 al = SSL_AD_DECODE_ERROR;
1284 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1288 /* Parse the message and load client random. */
1289 if (clienthello->isv2) {
1291 * Handle an SSLv2 backwards compatible ClientHello
1292 * Note, this is only for SSLv3+ using the backward compatible format.
1293 * Real SSLv2 is not supported, and is rejected below.
1295 unsigned int ciphersuite_len, session_id_len, challenge_len;
1298 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1299 || !PACKET_get_net_2(pkt, &session_id_len)
1300 || !PACKET_get_net_2(pkt, &challenge_len)) {
1301 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1302 SSL_R_RECORD_LENGTH_MISMATCH);
1303 al = SSL_AD_DECODE_ERROR;
1307 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1308 al = SSL_AD_DECODE_ERROR;
1309 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1313 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1315 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1316 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1317 /* No extensions. */
1318 || PACKET_remaining(pkt) != 0) {
1319 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1320 SSL_R_RECORD_LENGTH_MISMATCH);
1321 al = SSL_AD_DECODE_ERROR;
1324 clienthello->session_id_len = session_id_len;
1326 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1327 * here rather than sizeof(clienthello->random) because that is the limit
1328 * for SSLv3 and it is fixed. It won't change even if
1329 * sizeof(clienthello->random) does.
1331 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1332 ? SSL3_RANDOM_SIZE : challenge_len;
1333 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1334 if (!PACKET_copy_bytes(&challenge,
1335 clienthello->random + SSL3_RANDOM_SIZE -
1336 challenge_len, challenge_len)
1337 /* Advertise only null compression. */
1338 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1339 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1340 al = SSL_AD_INTERNAL_ERROR;
1344 PACKET_null_init(&clienthello->extensions);
1346 /* Regular ClientHello. */
1347 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1348 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1349 || !PACKET_copy_all(&session_id, clienthello->session_id,
1350 SSL_MAX_SSL_SESSION_ID_LENGTH,
1351 &clienthello->session_id_len)) {
1352 al = SSL_AD_DECODE_ERROR;
1353 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1357 if (SSL_IS_DTLS(s)) {
1358 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1359 al = SSL_AD_DECODE_ERROR;
1360 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1363 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1364 DTLS1_COOKIE_LENGTH,
1365 &clienthello->dtls_cookie_len)) {
1366 al = SSL_AD_DECODE_ERROR;
1367 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1371 * If we require cookies and this ClientHello doesn't contain one,
1372 * just return since we do not want to allocate any memory yet.
1373 * So check cookie length...
1375 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1376 if (clienthello->dtls_cookie_len == 0)
1381 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1382 al = SSL_AD_DECODE_ERROR;
1383 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1387 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1388 al = SSL_AD_DECODE_ERROR;
1389 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1393 /* Could be empty. */
1394 if (PACKET_remaining(pkt) == 0) {
1395 PACKET_null_init(&clienthello->extensions);
1397 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)) {
1398 al = SSL_AD_DECODE_ERROR;
1399 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1405 if (!PACKET_copy_all(&compression, clienthello->compressions,
1406 MAX_COMPRESSIONS_SIZE,
1407 &clienthello->compressions_len)) {
1408 al = SSL_AD_DECODE_ERROR;
1409 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1413 /* Preserve the raw extensions PACKET for later use */
1414 extensions = clienthello->extensions;
1415 if (!tls_collect_extensions(s, &extensions, EXT_CLIENT_HELLO,
1416 &clienthello->pre_proc_exts, &al,
1417 &clienthello->pre_proc_exts_len)) {
1418 /* SSLerr already been called */
1421 s->clienthello = clienthello;
1423 return MSG_PROCESS_CONTINUE_PROCESSING;
1425 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1427 ossl_statem_set_error(s);
1429 OPENSSL_free(clienthello->pre_proc_exts);
1430 OPENSSL_free(clienthello);
1432 return MSG_PROCESS_ERROR;
1435 static int tls_early_post_process_client_hello(SSL *s, int *al)
1442 #ifndef OPENSSL_NO_COMP
1443 SSL_COMP *comp = NULL;
1445 const SSL_CIPHER *c;
1446 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1447 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1448 CLIENTHELLO_MSG *clienthello = s->clienthello;
1450 *al = SSL_AD_INTERNAL_ERROR;
1451 /* Finished parsing the ClientHello, now we can start processing it */
1452 /* Give the early callback a crack at things */
1453 if (s->ctx->early_cb != NULL) {
1455 /* A failure in the early callback terminates the connection. */
1456 code = s->ctx->early_cb(s, al, s->ctx->early_cb_arg);
1460 s->rwstate = SSL_EARLY_WORK;
1465 /* Set up the client_random */
1466 memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1468 /* Choose the version */
1470 if (clienthello->isv2) {
1471 if (clienthello->legacy_version == SSL2_VERSION
1472 || (clienthello->legacy_version & 0xff00)
1473 != (SSL3_VERSION_MAJOR << 8)) {
1475 * This is real SSLv2 or something complete unknown. We don't
1478 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1482 s->client_version = clienthello->legacy_version;
1485 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1486 * versions are potentially compatible. Version negotiation comes later.
1488 if (!SSL_IS_DTLS(s)) {
1489 protverr = ssl_choose_server_version(s, clienthello);
1490 } else if (s->method->version != DTLS_ANY_VERSION &&
1491 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1492 protverr = SSL_R_VERSION_TOO_LOW;
1498 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1499 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1500 /* like ssl3_get_record, send alert using remote version number */
1501 s->version = s->client_version = clienthello->legacy_version;
1503 *al = SSL_AD_PROTOCOL_VERSION;
1507 if (SSL_IS_DTLS(s)) {
1508 /* Empty cookie was already handled above by returning early. */
1509 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1510 if (s->ctx->app_verify_cookie_cb != NULL) {
1511 if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1512 clienthello->dtls_cookie_len) == 0) {
1513 *al = SSL_AD_HANDSHAKE_FAILURE;
1514 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1515 SSL_R_COOKIE_MISMATCH);
1517 /* else cookie verification succeeded */
1519 /* default verification */
1520 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1521 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1522 s->d1->cookie_len) != 0) {
1523 *al = SSL_AD_HANDSHAKE_FAILURE;
1524 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1527 s->d1->cookie_verified = 1;
1529 if (s->method->version == DTLS_ANY_VERSION) {
1530 protverr = ssl_choose_server_version(s, clienthello);
1531 if (protverr != 0) {
1532 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1533 s->version = s->client_version;
1534 *al = SSL_AD_PROTOCOL_VERSION;
1542 /* We need to do this before getting the session */
1543 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1545 clienthello->pre_proc_exts, NULL, 0, al)) {
1546 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1551 * We don't allow resumption in a backwards compatible ClientHello.
1552 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1554 * Versions before 0.9.7 always allow clients to resume sessions in
1555 * renegotiation. 0.9.7 and later allow this by default, but optionally
1556 * ignore resumption requests with flag
1557 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1558 * than a change to default behavior so that applications relying on
1559 * this for security won't even compile against older library versions).
1560 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1561 * request renegotiation but not a new session (s->new_session remains
1562 * unset): for servers, this essentially just means that the
1563 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1566 if (clienthello->isv2 ||
1568 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1569 if (!ssl_get_new_session(s, 1))
1572 i = ssl_get_prev_session(s, clienthello, al);
1574 /* previous session */
1576 } else if (i == -1) {
1580 if (!ssl_get_new_session(s, 1))
1585 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1586 clienthello->isv2, al) ||
1587 !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1588 clienthello->isv2, al)) {
1592 s->s3->send_connection_binding = 0;
1593 /* Check what signalling cipher-suite values were received. */
1594 if (scsvs != NULL) {
1595 for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1596 c = sk_SSL_CIPHER_value(scsvs, i);
1597 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1598 if (s->renegotiate) {
1599 /* SCSV is fatal if renegotiating */
1600 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1601 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1602 *al = SSL_AD_HANDSHAKE_FAILURE;
1605 s->s3->send_connection_binding = 1;
1606 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1607 !ssl_check_version_downgrade(s)) {
1609 * This SCSV indicates that the client previously tried
1610 * a higher version. We should fail if the current version
1611 * is an unexpected downgrade, as that indicates that the first
1612 * connection may have been tampered with in order to trigger
1613 * an insecure downgrade.
1615 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1616 SSL_R_INAPPROPRIATE_FALLBACK);
1617 *al = SSL_AD_INAPPROPRIATE_FALLBACK;
1623 /* If it is a hit, check that the cipher is in the list */
1626 id = s->session->cipher->id;
1629 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1631 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1632 c = sk_SSL_CIPHER_value(ciphers, i);
1634 fprintf(stderr, "client [%2d of %2d]:%s\n",
1635 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1644 * we need to have the cipher in the cipher list if we are asked
1647 *al = SSL_AD_ILLEGAL_PARAMETER;
1648 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1649 SSL_R_REQUIRED_CIPHER_MISSING);
1654 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1655 if (clienthello->compressions[loop] == 0)
1659 if (loop >= clienthello->compressions_len) {
1661 *al = SSL_AD_DECODE_ERROR;
1662 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1666 #ifndef OPENSSL_NO_EC
1667 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1668 ssl_check_for_safari(s, clienthello);
1669 #endif /* !OPENSSL_NO_EC */
1671 /* TLS extensions */
1672 if (!tls_parse_all_extensions(s, EXT_CLIENT_HELLO,
1673 clienthello->pre_proc_exts, NULL, 0, al)) {
1674 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1679 * Check if we want to use external pre-shared secret for this handshake
1680 * for not reused session only. We need to generate server_random before
1681 * calling tls_session_secret_cb in order to allow SessionTicket
1682 * processing to use it in key derivation.
1686 pos = s->s3->server_random;
1687 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1692 if (!s->hit && s->version >= TLS1_VERSION && s->ext.session_secret_cb) {
1693 const SSL_CIPHER *pref_cipher = NULL;
1695 * s->session->master_key_length is a size_t, but this is an int for
1696 * backwards compat reasons
1698 int master_key_length;
1700 master_key_length = sizeof(s->session->master_key);
1701 if (s->ext.session_secret_cb(s, s->session->master_key,
1702 &master_key_length, ciphers,
1704 s->ext.session_secret_cb_arg)
1705 && master_key_length > 0) {
1706 s->session->master_key_length = master_key_length;
1708 s->session->ciphers = ciphers;
1709 s->session->verify_result = X509_V_OK;
1713 /* check if some cipher was preferred by call back */
1714 if (pref_cipher == NULL)
1715 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1716 SSL_get_ciphers(s));
1717 if (pref_cipher == NULL) {
1718 *al = SSL_AD_HANDSHAKE_FAILURE;
1719 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1723 s->session->cipher = pref_cipher;
1724 sk_SSL_CIPHER_free(s->cipher_list);
1725 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1726 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1727 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1732 * Worst case, we will use the NULL compression, but if we have other
1733 * options, we will now look for them. We have complen-1 compression
1734 * algorithms from the client, starting at q.
1736 s->s3->tmp.new_compression = NULL;
1737 #ifndef OPENSSL_NO_COMP
1738 /* This only happens if we have a cache hit */
1739 if (s->session->compress_meth != 0 && !SSL_IS_TLS13(s)) {
1740 int m, comp_id = s->session->compress_meth;
1742 /* Perform sanity checks on resumed compression algorithm */
1743 /* Can't disable compression */
1744 if (!ssl_allow_compression(s)) {
1745 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1746 SSL_R_INCONSISTENT_COMPRESSION);
1749 /* Look for resumed compression method */
1750 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1751 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1752 if (comp_id == comp->id) {
1753 s->s3->tmp.new_compression = comp;
1757 if (s->s3->tmp.new_compression == NULL) {
1758 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1759 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1762 /* Look for resumed method in compression list */
1763 for (k = 0; k < clienthello->compressions_len; k++) {
1764 if (clienthello->compressions[k] == comp_id)
1767 if (k >= clienthello->compressions_len) {
1768 *al = SSL_AD_ILLEGAL_PARAMETER;
1769 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1770 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1773 } else if (s->hit) {
1775 } else if (ssl_allow_compression(s) && s->ctx->comp_methods
1776 && !SSL_IS_TLS13(s)) {
1777 /* See if we have a match */
1778 int m, nn, v, done = 0;
1781 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1782 for (m = 0; m < nn; m++) {
1783 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1785 for (o = 0; o < clienthello->compressions_len; o++) {
1786 if (v == clienthello->compressions[o]) {
1795 s->s3->tmp.new_compression = comp;
1801 * If compression is disabled we'd better not try to resume a session
1802 * using compression.
1804 if (s->session->compress_meth != 0) {
1805 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1811 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1815 #ifdef OPENSSL_NO_COMP
1816 s->session->compress_meth = 0;
1818 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1820 sk_SSL_CIPHER_free(s->session->ciphers);
1821 s->session->ciphers = ciphers;
1822 if (ciphers == NULL) {
1823 *al = SSL_AD_INTERNAL_ERROR;
1824 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1828 if (!tls1_set_server_sigalgs(s)) {
1829 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1834 sk_SSL_CIPHER_free(ciphers);
1835 sk_SSL_CIPHER_free(scsvs);
1836 OPENSSL_free(clienthello->pre_proc_exts);
1837 OPENSSL_free(s->clienthello);
1838 s->clienthello = NULL;
1841 ossl_statem_set_error(s);
1843 sk_SSL_CIPHER_free(ciphers);
1844 sk_SSL_CIPHER_free(scsvs);
1845 OPENSSL_free(clienthello->pre_proc_exts);
1846 OPENSSL_free(s->clienthello);
1847 s->clienthello = NULL;
1853 * Call the status request callback if needed. Upon success, returns 1.
1854 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
1856 static int tls_handle_status_request(SSL *s, int *al)
1858 s->ext.status_expected = 0;
1861 * If status request then ask callback what to do. Note: this must be
1862 * called after servername callbacks in case the certificate has changed,
1863 * and must be called after the cipher has been chosen because this may
1864 * influence which certificate is sent
1866 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1867 && s->ctx->ext.status_cb != NULL) {
1870 /* If no certificate can't return certificate status */
1871 if (s->s3->tmp.cert != NULL) {
1873 * Set current certificate to one we will use so SSL_get_certificate
1874 * et al can pick it up.
1876 s->cert->key = s->s3->tmp.cert;
1877 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
1879 /* We don't want to send a status request response */
1880 case SSL_TLSEXT_ERR_NOACK:
1881 s->ext.status_expected = 0;
1883 /* status request response should be sent */
1884 case SSL_TLSEXT_ERR_OK:
1885 if (s->ext.ocsp.resp)
1886 s->ext.status_expected = 1;
1888 /* something bad happened */
1889 case SSL_TLSEXT_ERR_ALERT_FATAL:
1891 *al = SSL_AD_INTERNAL_ERROR;
1900 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1902 int al = SSL_AD_HANDSHAKE_FAILURE;
1903 const SSL_CIPHER *cipher;
1905 if (wst == WORK_MORE_A) {
1906 int rv = tls_early_post_process_client_hello(s, &al);
1908 /* SSLErr() was already called */
1915 if (wst == WORK_MORE_B) {
1917 /* Let cert callback update server certificates if required */
1918 if (s->cert->cert_cb) {
1919 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1921 al = SSL_AD_INTERNAL_ERROR;
1922 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1923 SSL_R_CERT_CB_ERROR);
1927 s->rwstate = SSL_X509_LOOKUP;
1930 s->rwstate = SSL_NOTHING;
1933 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1935 if (cipher == NULL) {
1936 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1937 SSL_R_NO_SHARED_CIPHER);
1940 s->s3->tmp.new_cipher = cipher;
1941 if (!tls_choose_sigalg(s, &al))
1943 /* check whether we should disable session resumption */
1944 if (s->not_resumable_session_cb != NULL)
1945 s->session->not_resumable =
1946 s->not_resumable_session_cb(s, ((cipher->algorithm_mkey
1947 & (SSL_kDHE | SSL_kECDHE))
1949 if (s->session->not_resumable)
1950 /* do not send a session ticket */
1951 s->ext.ticket_expected = 0;
1953 /* Session-id reuse */
1954 s->s3->tmp.new_cipher = s->session->cipher;
1958 * we now have the following setup.
1960 * cipher_list - our preferred list of ciphers
1961 * ciphers - the clients preferred list of ciphers
1962 * compression - basically ignored right now
1963 * ssl version is set - sslv3
1964 * s->session - The ssl session has been setup.
1965 * s->hit - session reuse flag
1966 * s->s3->tmp.new_cipher- the new cipher to use.
1970 * Call status_request callback if needed. Has to be done after the
1971 * certificate callbacks etc above.
1973 if (!tls_handle_status_request(s, &al)) {
1974 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1975 SSL_R_CLIENTHELLO_TLSEXT);
1981 #ifndef OPENSSL_NO_SRP
1982 if (wst == WORK_MORE_C) {
1984 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1986 * callback indicates further work to be done
1988 s->rwstate = SSL_X509_LOOKUP;
1991 if (ret != SSL_ERROR_NONE) {
1993 * This is not really an error but the only means to for
1994 * a client to detect whether srp is supported.
1996 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1997 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1998 SSL_R_CLIENTHELLO_TLSEXT);
2000 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2001 SSL_R_PSK_IDENTITY_NOT_FOUND);
2007 return WORK_FINISHED_STOP;
2009 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2010 ossl_statem_set_error(s);
2014 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2016 int compm, al = SSL_AD_INTERNAL_ERROR;
2020 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
2021 version = SSL_IS_TLS13(s) ? TLS1_3_VERSION_DRAFT : s->version;
2022 if (!WPACKET_put_bytes_u16(pkt, version)
2024 * Random stuff. Filling of the server_random takes place in
2025 * tls_process_client_hello()
2027 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
2028 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2033 * There are several cases for the session ID to send
2034 * back in the server hello:
2035 * - For session reuse from the session cache,
2036 * we send back the old session ID.
2037 * - If stateless session reuse (using a session ticket)
2038 * is successful, we send back the client's "session ID"
2039 * (which doesn't actually identify the session).
2040 * - If it is a new session, we send back the new
2042 * - However, if we want the new session to be single-use,
2043 * we send back a 0-length session ID.
2044 * s->hit is non-zero in either case of session reuse,
2045 * so the following won't overwrite an ID that we're supposed
2048 if (s->session->not_resumable ||
2049 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2051 s->session->session_id_length = 0;
2053 sl = s->session->session_id_length;
2054 if (sl > sizeof(s->session->session_id)) {
2055 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2059 /* set up the compression method */
2060 #ifdef OPENSSL_NO_COMP
2063 if (s->s3->tmp.new_compression == NULL)
2066 compm = s->s3->tmp.new_compression->id;
2069 if ((!SSL_IS_TLS13(s)
2070 && !WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl))
2071 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
2072 || (!SSL_IS_TLS13(s)
2073 && !WPACKET_put_bytes_u8(pkt, compm))
2074 || !tls_construct_extensions(s, pkt,
2076 ? EXT_TLS1_3_SERVER_HELLO
2077 : EXT_TLS1_2_SERVER_HELLO,
2079 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2083 if (!(s->verify_mode & SSL_VERIFY_PEER)
2084 && !ssl3_digest_cached_records(s, 0)) {
2085 al = SSL_AD_INTERNAL_ERROR;
2091 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2095 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2097 if (!s->s3->tmp.cert_request) {
2098 if (!ssl3_digest_cached_records(s, 0)) {
2099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2106 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2108 #ifndef OPENSSL_NO_DH
2109 EVP_PKEY *pkdh = NULL;
2111 #ifndef OPENSSL_NO_EC
2112 unsigned char *encodedPoint = NULL;
2113 size_t encodedlen = 0;
2116 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2117 int al = SSL_AD_INTERNAL_ERROR, i;
2120 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2121 EVP_PKEY_CTX *pctx = NULL;
2122 size_t paramlen, paramoffset;
2124 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
2125 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2129 if (md_ctx == NULL) {
2130 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2134 type = s->s3->tmp.new_cipher->algorithm_mkey;
2136 r[0] = r[1] = r[2] = r[3] = NULL;
2137 #ifndef OPENSSL_NO_PSK
2138 /* Plain PSK or RSAPSK nothing to do */
2139 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2141 #endif /* !OPENSSL_NO_PSK */
2142 #ifndef OPENSSL_NO_DH
2143 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2144 CERT *cert = s->cert;
2146 EVP_PKEY *pkdhp = NULL;
2149 if (s->cert->dh_tmp_auto) {
2150 DH *dhp = ssl_get_auto_dh(s);
2151 pkdh = EVP_PKEY_new();
2152 if (pkdh == NULL || dhp == NULL) {
2154 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2155 ERR_R_INTERNAL_ERROR);
2158 EVP_PKEY_assign_DH(pkdh, dhp);
2161 pkdhp = cert->dh_tmp;
2163 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2164 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2165 pkdh = ssl_dh_to_pkey(dhp);
2167 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2168 ERR_R_INTERNAL_ERROR);
2173 if (pkdhp == NULL) {
2174 al = SSL_AD_HANDSHAKE_FAILURE;
2175 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2176 SSL_R_MISSING_TMP_DH_KEY);
2179 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2180 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2181 al = SSL_AD_HANDSHAKE_FAILURE;
2182 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2183 SSL_R_DH_KEY_TOO_SMALL);
2186 if (s->s3->tmp.pkey != NULL) {
2187 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2188 ERR_R_INTERNAL_ERROR);
2192 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2194 if (s->s3->tmp.pkey == NULL) {
2195 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2199 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2201 EVP_PKEY_free(pkdh);
2204 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2205 DH_get0_key(dh, &r[2], NULL);
2208 #ifndef OPENSSL_NO_EC
2209 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2212 if (s->s3->tmp.pkey != NULL) {
2213 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2214 ERR_R_INTERNAL_ERROR);
2218 /* Get NID of appropriate shared curve */
2219 nid = tls1_shared_group(s, -2);
2220 curve_id = tls1_ec_nid2curve_id(nid);
2221 if (curve_id == 0) {
2222 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2223 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2226 s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
2227 /* Generate a new key for this curve */
2228 if (s->s3->tmp.pkey == NULL) {
2229 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2233 /* Encode the public key. */
2234 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2236 if (encodedlen == 0) {
2237 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2242 * We'll generate the serverKeyExchange message explicitly so we
2243 * can set these to NULLs
2250 #endif /* !OPENSSL_NO_EC */
2251 #ifndef OPENSSL_NO_SRP
2252 if (type & SSL_kSRP) {
2253 if ((s->srp_ctx.N == NULL) ||
2254 (s->srp_ctx.g == NULL) ||
2255 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2256 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2257 SSL_R_MISSING_SRP_PARAM);
2260 r[0] = s->srp_ctx.N;
2261 r[1] = s->srp_ctx.g;
2262 r[2] = s->srp_ctx.s;
2263 r[3] = s->srp_ctx.B;
2267 al = SSL_AD_HANDSHAKE_FAILURE;
2268 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2269 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2273 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2274 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2276 } else if (lu == NULL) {
2277 al = SSL_AD_DECODE_ERROR;
2281 #ifndef OPENSSL_NO_PSK
2282 if (type & SSL_PSK) {
2283 size_t len = (s->cert->psk_identity_hint == NULL)
2284 ? 0 : strlen(s->cert->psk_identity_hint);
2287 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2288 * checked this when we set the identity hint - but just in case
2290 if (len > PSK_MAX_IDENTITY_LEN
2291 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2293 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2294 ERR_R_INTERNAL_ERROR);
2300 for (i = 0; i < 4 && r[i] != NULL; i++) {
2301 unsigned char *binval;
2304 #ifndef OPENSSL_NO_SRP
2305 if ((i == 2) && (type & SSL_kSRP)) {
2306 res = WPACKET_start_sub_packet_u8(pkt);
2309 res = WPACKET_start_sub_packet_u16(pkt);
2312 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2313 ERR_R_INTERNAL_ERROR);
2317 #ifndef OPENSSL_NO_DH
2319 * for interoperability with some versions of the Microsoft TLS
2320 * stack, we need to zero pad the DHE pub key to the same length
2323 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2324 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2327 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2328 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2329 ERR_R_INTERNAL_ERROR);
2332 memset(binval, 0, len);
2336 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2337 || !WPACKET_close(pkt)) {
2338 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2339 ERR_R_INTERNAL_ERROR);
2343 BN_bn2bin(r[i], binval);
2346 #ifndef OPENSSL_NO_EC
2347 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2349 * We only support named (not generic) curves. In this situation, the
2350 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2351 * [1 byte length of encoded point], followed by the actual encoded
2354 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2355 || !WPACKET_put_bytes_u8(pkt, 0)
2356 || !WPACKET_put_bytes_u8(pkt, curve_id)
2357 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2358 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2359 ERR_R_INTERNAL_ERROR);
2362 OPENSSL_free(encodedPoint);
2363 encodedPoint = NULL;
2369 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2370 const EVP_MD *md = ssl_md(lu->hash_idx);
2371 unsigned char *sigbytes1, *sigbytes2;
2374 if (pkey == NULL || md == NULL) {
2375 /* Should never happen */
2376 al = SSL_AD_INTERNAL_ERROR;
2377 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2378 ERR_R_INTERNAL_ERROR);
2382 * n is the length of the params, they start at &(d[4]) and p
2383 * points to the space at the end.
2386 /* Get length of the parameters we have written above */
2387 if (!WPACKET_get_length(pkt, ¶mlen)) {
2388 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2389 ERR_R_INTERNAL_ERROR);
2392 /* send signature algorithm */
2393 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg))
2396 * Create the signature. We don't know the actual length of the sig
2397 * until after we've created it, so we reserve enough bytes for it
2398 * up front, and then properly allocate them in the WPACKET
2401 siglen = EVP_PKEY_size(pkey);
2402 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2403 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2404 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2405 ERR_R_INTERNAL_ERROR);
2408 if (lu->sig == EVP_PKEY_RSA_PSS) {
2409 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2410 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2411 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2416 if (EVP_DigestSignUpdate(md_ctx, &(s->s3->client_random[0]),
2417 SSL3_RANDOM_SIZE) <= 0
2418 || EVP_DigestSignUpdate(md_ctx, &(s->s3->server_random[0]),
2419 SSL3_RANDOM_SIZE) <= 0
2420 || EVP_DigestSignUpdate(md_ctx,
2421 s->init_buf->data + paramoffset,
2423 || EVP_DigestSignFinal(md_ctx, sigbytes1, &siglen) <= 0
2424 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2425 || sigbytes1 != sigbytes2) {
2426 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2427 ERR_R_INTERNAL_ERROR);
2432 EVP_MD_CTX_free(md_ctx);
2435 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2437 #ifndef OPENSSL_NO_DH
2438 EVP_PKEY_free(pkdh);
2440 #ifndef OPENSSL_NO_EC
2441 OPENSSL_free(encodedPoint);
2443 EVP_MD_CTX_free(md_ctx);
2447 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2450 STACK_OF(X509_NAME) *sk = NULL;
2452 if (SSL_IS_TLS13(s)) {
2453 /* TODO(TLS1.3) for now send empty request context */
2454 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2455 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2456 ERR_R_INTERNAL_ERROR);
2460 /* get the list of acceptable cert types */
2461 if (!WPACKET_start_sub_packet_u8(pkt)
2462 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2463 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2464 ERR_R_INTERNAL_ERROR);
2469 if (SSL_USE_SIGALGS(s)) {
2470 const uint16_t *psigs;
2471 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2473 if (!WPACKET_start_sub_packet_u16(pkt)
2474 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2475 || !WPACKET_close(pkt)) {
2476 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2477 ERR_R_INTERNAL_ERROR);
2482 /* Start sub-packet for client CA list */
2483 if (!WPACKET_start_sub_packet_u16(pkt)) {
2484 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2488 sk = SSL_get_client_CA_list(s);
2490 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2491 unsigned char *namebytes;
2492 X509_NAME *name = sk_X509_NAME_value(sk, i);
2496 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2497 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2499 || i2d_X509_NAME(name, &namebytes) != namelen) {
2500 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2501 ERR_R_INTERNAL_ERROR);
2506 /* else no CA names */
2507 if (!WPACKET_close(pkt)) {
2508 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2512 * TODO(TLS1.3) implement configurable certificate_extensions
2513 * For now just send zero length extensions.
2515 if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u16(pkt, 0)) {
2516 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2520 s->s3->tmp.cert_request = 1;
2524 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2528 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
2530 #ifndef OPENSSL_NO_PSK
2531 unsigned char psk[PSK_MAX_PSK_LEN];
2533 PACKET psk_identity;
2535 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2536 *al = SSL_AD_DECODE_ERROR;
2537 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
2540 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2541 *al = SSL_AD_DECODE_ERROR;
2542 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
2545 if (s->psk_server_callback == NULL) {
2546 *al = SSL_AD_INTERNAL_ERROR;
2547 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
2551 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2552 *al = SSL_AD_INTERNAL_ERROR;
2553 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2557 psklen = s->psk_server_callback(s, s->session->psk_identity,
2560 if (psklen > PSK_MAX_PSK_LEN) {
2561 *al = SSL_AD_INTERNAL_ERROR;
2562 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2564 } else if (psklen == 0) {
2566 * PSK related to the given identity not found
2568 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2569 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2570 SSL_R_PSK_IDENTITY_NOT_FOUND);
2574 OPENSSL_free(s->s3->tmp.psk);
2575 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2576 OPENSSL_cleanse(psk, psklen);
2578 if (s->s3->tmp.psk == NULL) {
2579 *al = SSL_AD_INTERNAL_ERROR;
2580 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2584 s->s3->tmp.psklen = psklen;
2588 /* Should never happen */
2589 *al = SSL_AD_INTERNAL_ERROR;
2590 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2595 static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
2597 #ifndef OPENSSL_NO_RSA
2598 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2600 unsigned char decrypt_good, version_good;
2601 size_t j, padding_len;
2602 PACKET enc_premaster;
2604 unsigned char *rsa_decrypt = NULL;
2607 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2609 *al = SSL_AD_HANDSHAKE_FAILURE;
2610 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
2614 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2615 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2616 enc_premaster = *pkt;
2618 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2619 || PACKET_remaining(pkt) != 0) {
2620 *al = SSL_AD_DECODE_ERROR;
2621 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
2627 * We want to be sure that the plaintext buffer size makes it safe to
2628 * iterate over the entire size of a premaster secret
2629 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2630 * their ciphertext cannot accommodate a premaster secret anyway.
2632 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2633 *al = SSL_AD_INTERNAL_ERROR;
2634 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
2638 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2639 if (rsa_decrypt == NULL) {
2640 *al = SSL_AD_INTERNAL_ERROR;
2641 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
2646 * We must not leak whether a decryption failure occurs because of
2647 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2648 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2649 * generates a random premaster secret for the case that the decrypt
2650 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2653 if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
2657 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2658 * the timing-sensitive code below.
2660 /* TODO(size_t): Convert this function */
2661 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2662 PACKET_data(&enc_premaster),
2663 rsa_decrypt, rsa, RSA_NO_PADDING);
2664 if (decrypt_len < 0)
2667 /* Check the padding. See RFC 3447, section 7.2.2. */
2670 * The smallest padded premaster is 11 bytes of overhead. Small keys
2671 * are publicly invalid, so this may return immediately. This ensures
2672 * PS is at least 8 bytes.
2674 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2675 *al = SSL_AD_DECRYPT_ERROR;
2676 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
2680 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2681 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2682 constant_time_eq_int_8(rsa_decrypt[1], 2);
2683 for (j = 2; j < padding_len - 1; j++) {
2684 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2686 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2689 * If the version in the decrypted pre-master secret is correct then
2690 * version_good will be 0xff, otherwise it'll be zero. The
2691 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2692 * (http://eprint.iacr.org/2003/052/) exploits the version number
2693 * check as a "bad version oracle". Thus version checks are done in
2694 * constant time and are treated like any other decryption error.
2697 constant_time_eq_8(rsa_decrypt[padding_len],
2698 (unsigned)(s->client_version >> 8));
2700 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2701 (unsigned)(s->client_version & 0xff));
2704 * The premaster secret must contain the same version number as the
2705 * ClientHello to detect version rollback attacks (strangely, the
2706 * protocol does not offer such protection for DH ciphersuites).
2707 * However, buggy clients exist that send the negotiated protocol
2708 * version instead if the server does not support the requested
2709 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2712 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2713 unsigned char workaround_good;
2714 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2715 (unsigned)(s->version >> 8));
2717 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2718 (unsigned)(s->version & 0xff));
2719 version_good |= workaround_good;
2723 * Both decryption and version must be good for decrypt_good to
2724 * remain non-zero (0xff).
2726 decrypt_good &= version_good;
2729 * Now copy rand_premaster_secret over from p using
2730 * decrypt_good_mask. If decryption failed, then p does not
2731 * contain valid plaintext, however, a check above guarantees
2732 * it is still sufficiently large to read from.
2734 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2735 rsa_decrypt[padding_len + j] =
2736 constant_time_select_8(decrypt_good,
2737 rsa_decrypt[padding_len + j],
2738 rand_premaster_secret[j]);
2741 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2742 sizeof(rand_premaster_secret), 0)) {
2743 *al = SSL_AD_INTERNAL_ERROR;
2744 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2750 OPENSSL_free(rsa_decrypt);
2753 /* Should never happen */
2754 *al = SSL_AD_INTERNAL_ERROR;
2755 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2760 static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
2762 #ifndef OPENSSL_NO_DH
2763 EVP_PKEY *skey = NULL;
2767 const unsigned char *data;
2768 EVP_PKEY *ckey = NULL;
2771 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2772 *al = SSL_AD_HANDSHAKE_FAILURE;
2773 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
2774 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2777 skey = s->s3->tmp.pkey;
2779 *al = SSL_AD_HANDSHAKE_FAILURE;
2780 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2784 if (PACKET_remaining(pkt) == 0L) {
2785 *al = SSL_AD_HANDSHAKE_FAILURE;
2786 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2789 if (!PACKET_get_bytes(pkt, &data, i)) {
2790 /* We already checked we have enough data */
2791 *al = SSL_AD_INTERNAL_ERROR;
2792 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2795 ckey = EVP_PKEY_new();
2796 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2797 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
2800 cdh = EVP_PKEY_get0_DH(ckey);
2801 pub_key = BN_bin2bn(data, i, NULL);
2803 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2804 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2805 if (pub_key != NULL)
2810 if (ssl_derive(s, skey, ckey, 1) == 0) {
2811 *al = SSL_AD_INTERNAL_ERROR;
2812 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2817 EVP_PKEY_free(s->s3->tmp.pkey);
2818 s->s3->tmp.pkey = NULL;
2820 EVP_PKEY_free(ckey);
2823 /* Should never happen */
2824 *al = SSL_AD_INTERNAL_ERROR;
2825 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2830 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
2832 #ifndef OPENSSL_NO_EC
2833 EVP_PKEY *skey = s->s3->tmp.pkey;
2834 EVP_PKEY *ckey = NULL;
2837 if (PACKET_remaining(pkt) == 0L) {
2838 /* We don't support ECDH client auth */
2839 *al = SSL_AD_HANDSHAKE_FAILURE;
2840 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
2844 const unsigned char *data;
2847 * Get client's public key from encoded point in the
2848 * ClientKeyExchange message.
2851 /* Get encoded point length */
2852 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
2853 || PACKET_remaining(pkt) != 0) {
2854 *al = SSL_AD_DECODE_ERROR;
2855 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
2858 ckey = EVP_PKEY_new();
2859 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
2860 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
2863 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
2864 *al = SSL_AD_HANDSHAKE_FAILURE;
2865 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
2870 if (ssl_derive(s, skey, ckey, 1) == 0) {
2871 *al = SSL_AD_INTERNAL_ERROR;
2872 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2877 EVP_PKEY_free(s->s3->tmp.pkey);
2878 s->s3->tmp.pkey = NULL;
2880 EVP_PKEY_free(ckey);
2884 /* Should never happen */
2885 *al = SSL_AD_INTERNAL_ERROR;
2886 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2891 static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
2893 #ifndef OPENSSL_NO_SRP
2895 const unsigned char *data;
2897 if (!PACKET_get_net_2(pkt, &i)
2898 || !PACKET_get_bytes(pkt, &data, i)) {
2899 *al = SSL_AD_DECODE_ERROR;
2900 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
2903 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2904 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
2907 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
2908 *al = SSL_AD_ILLEGAL_PARAMETER;
2909 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
2912 OPENSSL_free(s->session->srp_username);
2913 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2914 if (s->session->srp_username == NULL) {
2915 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
2919 if (!srp_generate_server_master_secret(s)) {
2920 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2926 /* Should never happen */
2927 *al = SSL_AD_INTERNAL_ERROR;
2928 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2933 static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
2935 #ifndef OPENSSL_NO_GOST
2936 EVP_PKEY_CTX *pkey_ctx;
2937 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2938 unsigned char premaster_secret[32];
2939 const unsigned char *start;
2940 size_t outlen = 32, inlen;
2941 unsigned long alg_a;
2944 size_t sess_key_len;
2945 const unsigned char *data;
2948 /* Get our certificate private key */
2949 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2950 if (alg_a & SSL_aGOST12) {
2952 * New GOST ciphersuites have SSL_aGOST01 bit too
2954 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2956 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2959 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2961 } else if (alg_a & SSL_aGOST01) {
2962 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2965 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2966 if (pkey_ctx == NULL) {
2967 *al = SSL_AD_INTERNAL_ERROR;
2968 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
2971 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2972 *al = SSL_AD_INTERNAL_ERROR;
2973 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2977 * If client certificate is present and is of the same type, maybe
2978 * use it for key exchange. Don't mind errors from
2979 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2980 * client certificate for authorization only.
2982 client_pub_pkey = X509_get0_pubkey(s->session->peer);
2983 if (client_pub_pkey) {
2984 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2987 /* Decrypt session key */
2988 sess_key_len = PACKET_remaining(pkt);
2989 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2990 *al = SSL_AD_INTERNAL_ERROR;
2991 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2994 /* TODO(size_t): Convert this function */
2995 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
2996 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
2997 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
2998 *al = SSL_AD_DECODE_ERROR;
2999 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
3004 if (EVP_PKEY_decrypt
3005 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
3006 *al = SSL_AD_DECODE_ERROR;
3007 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
3010 /* Generate master secret */
3011 if (!ssl_generate_master_secret(s, premaster_secret,
3012 sizeof(premaster_secret), 0)) {
3013 *al = SSL_AD_INTERNAL_ERROR;
3014 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
3017 /* Check if pubkey from client certificate was used */
3018 if (EVP_PKEY_CTX_ctrl
3019 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
3020 s->statem.no_cert_verify = 1;
3024 EVP_PKEY_CTX_free(pkey_ctx);
3027 /* Should never happen */
3028 *al = SSL_AD_INTERNAL_ERROR;
3029 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
3034 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3037 unsigned long alg_k;
3039 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3041 /* For PSK parse and retrieve identity, obtain PSK key */
3042 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
3045 if (alg_k & SSL_kPSK) {
3046 /* Identity extracted earlier: should be nothing left */
3047 if (PACKET_remaining(pkt) != 0) {
3048 al = SSL_AD_HANDSHAKE_FAILURE;
3049 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3050 SSL_R_LENGTH_MISMATCH);
3053 /* PSK handled by ssl_generate_master_secret */
3054 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3055 al = SSL_AD_INTERNAL_ERROR;
3056 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3059 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3060 if (!tls_process_cke_rsa(s, pkt, &al))
3062 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3063 if (!tls_process_cke_dhe(s, pkt, &al))
3065 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3066 if (!tls_process_cke_ecdhe(s, pkt, &al))
3068 } else if (alg_k & SSL_kSRP) {
3069 if (!tls_process_cke_srp(s, pkt, &al))
3071 } else if (alg_k & SSL_kGOST) {
3072 if (!tls_process_cke_gost(s, pkt, &al))
3075 al = SSL_AD_HANDSHAKE_FAILURE;
3076 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3077 SSL_R_UNKNOWN_CIPHER_TYPE);
3081 return MSG_PROCESS_CONTINUE_PROCESSING;
3084 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3085 #ifndef OPENSSL_NO_PSK
3086 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3087 s->s3->tmp.psk = NULL;
3089 ossl_statem_set_error(s);
3090 return MSG_PROCESS_ERROR;
3093 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3095 #ifndef OPENSSL_NO_SCTP
3096 if (wst == WORK_MORE_A) {
3097 if (SSL_IS_DTLS(s)) {
3098 unsigned char sctpauthkey[64];
3099 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3101 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3104 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3105 sizeof(DTLS1_SCTP_AUTH_LABEL));
3107 if (SSL_export_keying_material(s, sctpauthkey,
3108 sizeof(sctpauthkey), labelbuffer,
3109 sizeof(labelbuffer), NULL, 0,
3111 ossl_statem_set_error(s);
3115 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3116 sizeof(sctpauthkey), sctpauthkey);
3121 if ((wst == WORK_MORE_B)
3123 && BIO_dgram_is_sctp(SSL_get_wbio(s))
3124 /* Are we renegotiating? */
3126 /* Are we going to skip the CertificateVerify? */
3127 && (s->session->peer == NULL || s->statem.no_cert_verify)
3128 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
3129 s->s3->in_read_app_data = 2;
3130 s->rwstate = SSL_READING;
3131 BIO_clear_retry_flags(SSL_get_rbio(s));
3132 BIO_set_retry_read(SSL_get_rbio(s));
3133 ossl_statem_set_sctp_read_sock(s, 1);
3136 ossl_statem_set_sctp_read_sock(s, 0);
3140 if (s->statem.no_cert_verify || !s->session->peer) {
3142 * No certificate verify or no peer certificate so we no longer need
3143 * the handshake_buffer
3145 if (!ssl3_digest_cached_records(s, 0)) {
3146 ossl_statem_set_error(s);
3149 return WORK_FINISHED_CONTINUE;
3151 if (!s->s3->handshake_buffer) {
3152 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3153 ERR_R_INTERNAL_ERROR);
3154 ossl_statem_set_error(s);
3158 * For sigalgs freeze the handshake buffer. If we support
3159 * extms we've done this already so this is a no-op
3161 if (!ssl3_digest_cached_records(s, 1)) {
3162 ossl_statem_set_error(s);
3167 return WORK_FINISHED_CONTINUE;
3170 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3172 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3174 unsigned long l, llen;
3175 const unsigned char *certstart, *certbytes;
3176 STACK_OF(X509) *sk = NULL;
3177 PACKET spkt, context;
3180 if ((sk = sk_X509_new_null()) == NULL) {
3181 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3185 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3186 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3187 || !PACKET_get_net_3(pkt, &llen)
3188 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3189 || PACKET_remaining(pkt) != 0) {
3190 al = SSL_AD_DECODE_ERROR;
3191 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3195 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3196 if (!PACKET_get_net_3(&spkt, &l)
3197 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3198 al = SSL_AD_DECODE_ERROR;
3199 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3200 SSL_R_CERT_LENGTH_MISMATCH);
3204 certstart = certbytes;
3205 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3207 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3210 if (certbytes != (certstart + l)) {
3211 al = SSL_AD_DECODE_ERROR;
3212 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3213 SSL_R_CERT_LENGTH_MISMATCH);
3217 if (SSL_IS_TLS13(s)) {
3218 RAW_EXTENSION *rawexts = NULL;
3221 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3222 al = SSL_AD_DECODE_ERROR;
3223 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_BAD_LENGTH);
3226 if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_CERTIFICATE,
3227 &rawexts, &al, NULL)
3228 || !tls_parse_all_extensions(s, EXT_TLS1_3_CERTIFICATE,
3229 rawexts, x, chainidx, &al)) {
3230 OPENSSL_free(rawexts);
3233 OPENSSL_free(rawexts);
3236 if (!sk_X509_push(sk, x)) {
3237 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3243 if (sk_X509_num(sk) <= 0) {
3244 /* TLS does not mind 0 certs returned */
3245 if (s->version == SSL3_VERSION) {
3246 al = SSL_AD_HANDSHAKE_FAILURE;
3247 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3248 SSL_R_NO_CERTIFICATES_RETURNED);
3251 /* Fail for TLS only if we required a certificate */
3252 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3253 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3254 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3255 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3256 al = SSL_AD_HANDSHAKE_FAILURE;
3259 /* No client certificate so digest cached records */
3260 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3265 i = ssl_verify_cert_chain(s, sk);
3267 al = ssl_verify_alarm_type(s->verify_result);
3268 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3269 SSL_R_CERTIFICATE_VERIFY_FAILED);
3273 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3274 al = SSL_AD_HANDSHAKE_FAILURE;
3277 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3279 al = SSL3_AD_HANDSHAKE_FAILURE;
3280 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3281 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3286 X509_free(s->session->peer);
3287 s->session->peer = sk_X509_shift(sk);
3288 s->session->verify_result = s->verify_result;
3290 sk_X509_pop_free(s->session->peer_chain, X509_free);
3291 s->session->peer_chain = sk;
3294 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3297 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3298 al = SSL_AD_INTERNAL_ERROR;
3299 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3304 * Inconsistency alert: cert_chain does *not* include the peer's own
3305 * certificate, while we do include it in statem_clnt.c
3309 /* Save the current hash state for when we receive the CertificateVerify */
3311 && !ssl_handshake_hash(s, s->cert_verify_hash,
3312 sizeof(s->cert_verify_hash),
3313 &s->cert_verify_hash_len)) {
3314 al = SSL_AD_INTERNAL_ERROR;
3315 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3319 ret = MSG_PROCESS_CONTINUE_READING;
3323 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3324 ossl_statem_set_error(s);
3327 sk_X509_pop_free(sk, X509_free);
3331 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3333 CERT_PKEY *cpk = s->s3->tmp.cert;
3334 int al = SSL_AD_INTERNAL_ERROR;
3337 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3342 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3343 * for the server Certificate message
3345 if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0))
3346 || !ssl3_output_cert_chain(s, pkt, cpk, &al)) {
3347 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3348 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3355 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3357 unsigned char *senc = NULL;
3358 EVP_CIPHER_CTX *ctx = NULL;
3359 HMAC_CTX *hctx = NULL;
3360 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3361 const unsigned char *const_p;
3362 int len, slen_full, slen, lenfinal;
3365 SSL_CTX *tctx = s->session_ctx;
3366 unsigned char iv[EVP_MAX_IV_LENGTH];
3367 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3368 int iv_len, al = SSL_AD_INTERNAL_ERROR;
3369 size_t macoffset, macendoffset;
3371 unsigned char age_add_c[sizeof(uint32_t)];
3375 if (SSL_IS_TLS13(s)) {
3376 if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0)
3378 s->session->ext.tick_age_add = age_add_u.age_add;
3381 /* get session encoding length */
3382 slen_full = i2d_SSL_SESSION(s->session, NULL);
3384 * Some length values are 16 bits, so forget it if session is too
3387 if (slen_full == 0 || slen_full > 0xFF00) {
3388 ossl_statem_set_error(s);
3391 senc = OPENSSL_malloc(slen_full);
3393 ossl_statem_set_error(s);
3397 ctx = EVP_CIPHER_CTX_new();
3398 hctx = HMAC_CTX_new();
3399 if (ctx == NULL || hctx == NULL) {
3400 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3405 if (!i2d_SSL_SESSION(s->session, &p))
3409 * create a fresh copy (not shared with other threads) to clean up
3412 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3415 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3417 slen = i2d_SSL_SESSION(sess, NULL);
3418 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3419 SSL_SESSION_free(sess);
3423 if (!i2d_SSL_SESSION(sess, &p)) {
3424 SSL_SESSION_free(sess);
3427 SSL_SESSION_free(sess);
3430 * Initialize HMAC and cipher contexts. If callback present it does
3431 * all the work otherwise use generated values from parent ctx.
3433 if (tctx->ext.ticket_key_cb) {
3434 /* if 0 is returned, write an empty ticket */
3435 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3440 /* Put timeout and length */
3441 if (!WPACKET_put_bytes_u32(pkt, 0)
3442 || !WPACKET_put_bytes_u16(pkt, 0)) {
3443 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3444 ERR_R_INTERNAL_ERROR);
3448 EVP_CIPHER_CTX_free(ctx);
3449 HMAC_CTX_free(hctx);
3454 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3456 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3458 iv_len = EVP_CIPHER_iv_length(cipher);
3459 if (RAND_bytes(iv, iv_len) <= 0)
3461 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
3462 tctx->ext.tick_aes_key, iv))
3464 if (!HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
3465 sizeof(tctx->ext.tick_hmac_key),
3466 EVP_sha256(), NULL))
3468 memcpy(key_name, tctx->ext.tick_key_name,
3469 sizeof(tctx->ext.tick_key_name));
3473 * Ticket lifetime hint (advisory only): We leave this unspecified
3474 * for resumed session (for simplicity), and guess that tickets for
3475 * new sessions will live as long as their sessions.
3477 if (!WPACKET_put_bytes_u32(pkt, s->hit ? 0 : s->session->timeout)
3479 && !WPACKET_put_bytes_u32(pkt, age_add_u.age_add))
3480 /* Now the actual ticket data */
3481 || !WPACKET_start_sub_packet_u16(pkt)