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
52 #include "../ssl_locl.h"
53 #include "statem_locl.h"
54 #include "internal/constant_time_locl.h"
55 #include <openssl/buffer.h>
56 #include <openssl/rand.h>
57 #include <openssl/objects.h>
58 #include <openssl/evp.h>
59 #include <openssl/hmac.h>
60 #include <openssl/x509.h>
61 #include <openssl/dh.h>
62 #include <openssl/bn.h>
63 #include <openssl/md5.h>
65 static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
66 PACKET *cipher_suites,
67 STACK_OF(SSL_CIPHER) **skp,
68 int sslv2format, int *al);
71 * server_read_transition() encapsulates the logic for the allowed handshake
72 * state transitions when the server is reading messages from the client. The
73 * message type that the client has sent is provided in |mt|. The current state
74 * is in |s->statem.hand_state|.
76 * Valid return values are:
77 * 1: Success (transition allowed)
78 * 0: Error (transition not allowed)
80 int ossl_statem_server_read_transition(SSL *s, int mt)
82 OSSL_STATEM *st = &s->statem;
84 switch(st->hand_state) {
86 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
87 if (mt == SSL3_MT_CLIENT_HELLO) {
88 st->hand_state = TLS_ST_SR_CLNT_HELLO;
93 case TLS_ST_SW_SRVR_DONE:
95 * If we get a CKE message after a ServerDone then either
96 * 1) We didn't request a Certificate
98 * 2) If we did request one then
99 * a) We allow no Certificate to be returned
101 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
102 * list if we requested a certificate)
104 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE
105 && (!s->s3->tmp.cert_request
106 || (!((s->verify_mode & SSL_VERIFY_PEER) &&
107 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
108 && (s->version == SSL3_VERSION)))) {
109 st->hand_state = TLS_ST_SR_KEY_EXCH;
111 } else if (s->s3->tmp.cert_request) {
112 if (mt == SSL3_MT_CERTIFICATE) {
113 st->hand_state = TLS_ST_SR_CERT;
117 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE && s->s3->tmp.cert_request
118 && s->version == SSL3_VERSION) {
120 * This isn't an unexpected message as such - we're just not going
123 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_HANDSHAKE_FAILURE);
124 SSLerr(SSL_F_READ_STATE_MACHINE,
125 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
131 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
132 st->hand_state = TLS_ST_SR_KEY_EXCH;
137 case TLS_ST_SR_KEY_EXCH:
139 * We should only process a CertificateVerify message if we have
140 * received a Certificate from the client. If so then |s->session->peer|
141 * will be non NULL. In some instances a CertificateVerify message is
142 * not required even if the peer has sent a Certificate (e.g. such as in
143 * the case of static DH). In that case |st->no_cert_verify| should be
146 if (s->session->peer == NULL || st->no_cert_verify) {
147 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
149 * For the ECDH ciphersuites when the client sends its ECDH
150 * pub key in a certificate, the CertificateVerify message is
151 * not sent. Also for GOST ciphersuites when the client uses
152 * its key from the certificate for key exchange.
154 st->hand_state = TLS_ST_SR_CHANGE;
158 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
159 st->hand_state = TLS_ST_SR_CERT_VRFY;
165 case TLS_ST_SR_CERT_VRFY:
166 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
167 st->hand_state = TLS_ST_SR_CHANGE;
172 case TLS_ST_SR_CHANGE:
173 #ifndef OPENSSL_NO_NEXTPROTONEG
174 if (s->s3->next_proto_neg_seen) {
175 if (mt == SSL3_MT_NEXT_PROTO) {
176 st->hand_state = TLS_ST_SR_NEXT_PROTO;
181 if (mt == SSL3_MT_FINISHED) {
182 st->hand_state = TLS_ST_SR_FINISHED;
185 #ifndef OPENSSL_NO_NEXTPROTONEG
190 #ifndef OPENSSL_NO_NEXTPROTONEG
191 case TLS_ST_SR_NEXT_PROTO:
192 if (mt == SSL3_MT_FINISHED) {
193 st->hand_state = TLS_ST_SR_FINISHED;
199 case TLS_ST_SW_FINISHED:
200 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
201 st->hand_state = TLS_ST_SR_CHANGE;
210 /* No valid transition found */
211 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
212 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_UNEXPECTED_MESSAGE);
217 * Should we send a ServerKeyExchange message?
219 * Valid return values are:
223 static int send_server_key_exchange(SSL *s)
225 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
228 * only send a ServerKeyExchange if DH or fortezza but we have a
229 * sign only certificate PSK: may send PSK identity hints For
230 * ECC ciphersuites, we send a serverKeyExchange message only if
231 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
232 * the server certificate contains the server's public key for
235 if (alg_k & (SSL_kDHE|SSL_kECDHE)
237 * PSK: send ServerKeyExchange if PSK identity hint if
240 #ifndef OPENSSL_NO_PSK
241 /* Only send SKE if we have identity hint for plain PSK */
242 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
243 && s->cert->psk_identity_hint)
244 /* For other PSK always send SKE */
245 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
247 #ifndef OPENSSL_NO_SRP
248 /* SRP: send ServerKeyExchange */
249 || (alg_k & SSL_kSRP)
259 * Should we send a CertificateRequest message?
261 * Valid return values are:
265 static int send_certificate_request(SSL *s)
268 /* don't request cert unless asked for it: */
269 s->verify_mode & SSL_VERIFY_PEER
271 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
272 * during re-negotiation:
274 && ((s->session->peer == NULL) ||
275 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
277 * never request cert in anonymous ciphersuites (see
278 * section "Certificate request" in SSL 3 drafts and in
281 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
283 * ... except when the application insists on
284 * verification (against the specs, but statem_clnt.c accepts
287 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
288 /* don't request certificate for SRP auth */
289 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
291 * With normal PSK Certificates and Certificate Requests
294 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
302 * server_write_transition() works out what handshake state to move to next
303 * when the server is writing messages to be sent to the client.
305 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
307 OSSL_STATEM *st = &s->statem;
309 switch(st->hand_state) {
311 /* Just go straight to trying to read from the client */;
312 return WRITE_TRAN_FINISHED;
315 /* We must be trying to renegotiate */
316 st->hand_state = TLS_ST_SW_HELLO_REQ;
317 return WRITE_TRAN_CONTINUE;
319 case TLS_ST_SW_HELLO_REQ:
320 st->hand_state = TLS_ST_OK;
321 ossl_statem_set_in_init(s, 0);
322 return WRITE_TRAN_CONTINUE;
324 case TLS_ST_SR_CLNT_HELLO:
325 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
326 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
327 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
329 st->hand_state = TLS_ST_SW_SRVR_HELLO;
330 return WRITE_TRAN_CONTINUE;
332 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
333 return WRITE_TRAN_FINISHED;
335 case TLS_ST_SW_SRVR_HELLO:
337 if (s->tlsext_ticket_expected)
338 st->hand_state = TLS_ST_SW_SESSION_TICKET;
340 st->hand_state = TLS_ST_SW_CHANGE;
342 /* Check if it is anon DH or anon ECDH, */
343 /* normal PSK or SRP */
344 if (!(s->s3->tmp.new_cipher->algorithm_auth &
345 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
346 st->hand_state = TLS_ST_SW_CERT;
347 } else if (send_server_key_exchange(s)) {
348 st->hand_state = TLS_ST_SW_KEY_EXCH;
349 } else if (send_certificate_request(s)) {
350 st->hand_state = TLS_ST_SW_CERT_REQ;
352 st->hand_state = TLS_ST_SW_SRVR_DONE;
355 return WRITE_TRAN_CONTINUE;
358 if (s->tlsext_status_expected) {
359 st->hand_state = TLS_ST_SW_CERT_STATUS;
360 return WRITE_TRAN_CONTINUE;
364 case TLS_ST_SW_CERT_STATUS:
365 if (send_server_key_exchange(s)) {
366 st->hand_state = TLS_ST_SW_KEY_EXCH;
367 return WRITE_TRAN_CONTINUE;
371 case TLS_ST_SW_KEY_EXCH:
372 if (send_certificate_request(s)) {
373 st->hand_state = TLS_ST_SW_CERT_REQ;
374 return WRITE_TRAN_CONTINUE;
378 case TLS_ST_SW_CERT_REQ:
379 st->hand_state = TLS_ST_SW_SRVR_DONE;
380 return WRITE_TRAN_CONTINUE;
382 case TLS_ST_SW_SRVR_DONE:
383 return WRITE_TRAN_FINISHED;
385 case TLS_ST_SR_FINISHED:
387 st->hand_state = TLS_ST_OK;
388 ossl_statem_set_in_init(s, 0);
389 return WRITE_TRAN_CONTINUE;
390 } else if (s->tlsext_ticket_expected) {
391 st->hand_state = TLS_ST_SW_SESSION_TICKET;
393 st->hand_state = TLS_ST_SW_CHANGE;
395 return WRITE_TRAN_CONTINUE;
397 case TLS_ST_SW_SESSION_TICKET:
398 st->hand_state = TLS_ST_SW_CHANGE;
399 return WRITE_TRAN_CONTINUE;
401 case TLS_ST_SW_CHANGE:
402 st->hand_state = TLS_ST_SW_FINISHED;
403 return WRITE_TRAN_CONTINUE;
405 case TLS_ST_SW_FINISHED:
407 return WRITE_TRAN_FINISHED;
409 st->hand_state = TLS_ST_OK;
410 ossl_statem_set_in_init(s, 0);
411 return WRITE_TRAN_CONTINUE;
414 /* Shouldn't happen */
415 return WRITE_TRAN_ERROR;
420 * Perform any pre work that needs to be done prior to sending a message from
421 * the server to the client.
423 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
425 OSSL_STATEM *st = &s->statem;
427 switch(st->hand_state) {
428 case TLS_ST_SW_HELLO_REQ:
431 dtls1_clear_record_buffer(s);
434 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
436 if (SSL_IS_DTLS(s)) {
437 dtls1_clear_record_buffer(s);
438 /* We don't buffer this message so don't use the timer */
443 case TLS_ST_SW_SRVR_HELLO:
444 if (SSL_IS_DTLS(s)) {
446 * Messages we write from now on should be bufferred and
447 * retransmitted if necessary, so we need to use the timer now
453 case TLS_ST_SW_SRVR_DONE:
454 #ifndef OPENSSL_NO_SCTP
455 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
456 return dtls_wait_for_dry(s);
458 return WORK_FINISHED_CONTINUE;
460 case TLS_ST_SW_SESSION_TICKET:
461 if (SSL_IS_DTLS(s)) {
463 * We're into the last flight. We don't retransmit the last flight
464 * unless we need to, so we don't use the timer
470 case TLS_ST_SW_CHANGE:
471 s->session->cipher = s->s3->tmp.new_cipher;
472 if (!s->method->ssl3_enc->setup_key_block(s)) {
473 ossl_statem_set_error(s);
476 if (SSL_IS_DTLS(s)) {
478 * We're into the last flight. We don't retransmit the last flight
479 * unless we need to, so we don't use the timer. This might have
480 * already been set to 0 if we sent a NewSessionTicket message,
481 * but we'll set it again here in case we didn't.
485 return WORK_FINISHED_CONTINUE;
488 return tls_finish_handshake(s, wst);
491 /* No pre work to be done */
495 return WORK_FINISHED_CONTINUE;
499 * Perform any work that needs to be done after sending a message from the
500 * server to the client.
502 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
504 OSSL_STATEM *st = &s->statem;
508 switch(st->hand_state) {
509 case TLS_ST_SW_HELLO_REQ:
510 if (statem_flush(s) != 1)
512 if (!ssl3_init_finished_mac(s)) {
513 ossl_statem_set_error(s);
518 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
519 if (statem_flush(s) != 1)
521 /* HelloVerifyRequest resets Finished MAC */
522 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
523 ossl_statem_set_error(s);
527 * The next message should be another ClientHello which we need to
528 * treat like it was the first packet
533 case TLS_ST_SW_SRVR_HELLO:
534 #ifndef OPENSSL_NO_SCTP
535 if (SSL_IS_DTLS(s) && s->hit) {
536 unsigned char sctpauthkey[64];
537 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
540 * Add new shared key for SCTP-Auth, will be ignored if no
543 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
544 sizeof(DTLS1_SCTP_AUTH_LABEL));
546 if (SSL_export_keying_material(s, sctpauthkey,
547 sizeof(sctpauthkey), labelbuffer,
548 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
549 ossl_statem_set_error(s);
553 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
554 sizeof(sctpauthkey), sctpauthkey);
559 case TLS_ST_SW_CHANGE:
560 #ifndef OPENSSL_NO_SCTP
561 if (SSL_IS_DTLS(s) && !s->hit) {
563 * Change to new shared key of SCTP-Auth, will be ignored if
566 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
570 if (!s->method->ssl3_enc->change_cipher_state(s,
571 SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
572 ossl_statem_set_error(s);
577 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
580 case TLS_ST_SW_SRVR_DONE:
581 if (statem_flush(s) != 1)
585 case TLS_ST_SW_FINISHED:
586 if (statem_flush(s) != 1)
588 #ifndef OPENSSL_NO_SCTP
589 if (SSL_IS_DTLS(s) && s->hit) {
591 * Change to new shared key of SCTP-Auth, will be ignored if
594 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
601 /* No post work to be done */
605 return WORK_FINISHED_CONTINUE;
609 * Construct a message to be sent from the server to the client.
611 * Valid return values are:
615 int ossl_statem_server_construct_message(SSL *s)
617 OSSL_STATEM *st = &s->statem;
619 switch(st->hand_state) {
620 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
621 return dtls_construct_hello_verify_request(s);
623 case TLS_ST_SW_HELLO_REQ:
624 return tls_construct_hello_request(s);
626 case TLS_ST_SW_SRVR_HELLO:
627 return tls_construct_server_hello(s);
630 return tls_construct_server_certificate(s);
632 case TLS_ST_SW_KEY_EXCH:
633 return tls_construct_server_key_exchange(s);
635 case TLS_ST_SW_CERT_REQ:
636 return tls_construct_certificate_request(s);
638 case TLS_ST_SW_SRVR_DONE:
639 return tls_construct_server_done(s);
641 case TLS_ST_SW_SESSION_TICKET:
642 return tls_construct_new_session_ticket(s);
644 case TLS_ST_SW_CERT_STATUS:
645 return tls_construct_cert_status(s);
647 case TLS_ST_SW_CHANGE:
649 return dtls_construct_change_cipher_spec(s);
651 return tls_construct_change_cipher_spec(s);
653 case TLS_ST_SW_FINISHED:
654 return tls_construct_finished(s,
656 ssl3_enc->server_finished_label,
658 ssl3_enc->server_finished_label_len);
661 /* Shouldn't happen */
669 * Maximum size (excluding the Handshake header) of a ClientHello message,
670 * calculated as follows:
672 * 2 + # client_version
673 * 32 + # only valid length for random
674 * 1 + # length of session_id
675 * 32 + # maximum size for session_id
676 * 2 + # length of cipher suites
677 * 2^16-2 + # maximum length of cipher suites array
678 * 1 + # length of compression_methods
679 * 2^8-1 + # maximum length of compression methods
680 * 2 + # length of extensions
681 * 2^16-1 # maximum length of extensions
683 #define CLIENT_HELLO_MAX_LENGTH 131396
685 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
686 #define NEXT_PROTO_MAX_LENGTH 514
689 * Returns the maximum allowed length for the current message that we are
690 * reading. Excludes the message header.
692 unsigned long ossl_statem_server_max_message_size(SSL *s)
694 OSSL_STATEM *st = &s->statem;
696 switch(st->hand_state) {
697 case TLS_ST_SR_CLNT_HELLO:
698 return CLIENT_HELLO_MAX_LENGTH;
701 return s->max_cert_list;
703 case TLS_ST_SR_KEY_EXCH:
704 return CLIENT_KEY_EXCH_MAX_LENGTH;
706 case TLS_ST_SR_CERT_VRFY:
707 return SSL3_RT_MAX_PLAIN_LENGTH;
709 #ifndef OPENSSL_NO_NEXTPROTONEG
710 case TLS_ST_SR_NEXT_PROTO:
711 return NEXT_PROTO_MAX_LENGTH;
714 case TLS_ST_SR_CHANGE:
715 return CCS_MAX_LENGTH;
717 case TLS_ST_SR_FINISHED:
718 return FINISHED_MAX_LENGTH;
721 /* Shouldn't happen */
729 * Process a message that the server has received from the client.
731 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
733 OSSL_STATEM *st = &s->statem;
735 switch(st->hand_state) {
736 case TLS_ST_SR_CLNT_HELLO:
737 return tls_process_client_hello(s, pkt);
740 return tls_process_client_certificate(s, pkt);
742 case TLS_ST_SR_KEY_EXCH:
743 return tls_process_client_key_exchange(s, pkt);
745 case TLS_ST_SR_CERT_VRFY:
746 return tls_process_cert_verify(s, pkt);
748 #ifndef OPENSSL_NO_NEXTPROTONEG
749 case TLS_ST_SR_NEXT_PROTO:
750 return tls_process_next_proto(s, pkt);
753 case TLS_ST_SR_CHANGE:
754 return tls_process_change_cipher_spec(s, pkt);
756 case TLS_ST_SR_FINISHED:
757 return tls_process_finished(s, pkt);
760 /* Shouldn't happen */
764 return MSG_PROCESS_ERROR;
768 * Perform any further processing required following the receipt of a message
771 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
773 OSSL_STATEM *st = &s->statem;
775 switch(st->hand_state) {
776 case TLS_ST_SR_CLNT_HELLO:
777 return tls_post_process_client_hello(s, wst);
779 case TLS_ST_SR_KEY_EXCH:
780 return tls_post_process_client_key_exchange(s, wst);
782 case TLS_ST_SR_CERT_VRFY:
783 #ifndef OPENSSL_NO_SCTP
784 if ( /* Is this SCTP? */
785 BIO_dgram_is_sctp(SSL_get_wbio(s))
786 /* Are we renegotiating? */
788 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
789 s->s3->in_read_app_data = 2;
790 s->rwstate = SSL_READING;
791 BIO_clear_retry_flags(SSL_get_rbio(s));
792 BIO_set_retry_read(SSL_get_rbio(s));
793 ossl_statem_set_sctp_read_sock(s, 1);
796 ossl_statem_set_sctp_read_sock(s, 0);
799 return WORK_FINISHED_CONTINUE;
805 /* Shouldn't happen */
809 #ifndef OPENSSL_NO_SRP
810 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
812 int ret = SSL_ERROR_NONE;
814 *al = SSL_AD_UNRECOGNIZED_NAME;
816 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
817 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
818 if (s->srp_ctx.login == NULL) {
820 * RFC 5054 says SHOULD reject, we do so if There is no srp
824 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
826 ret = SSL_srp_server_param_with_username(s, al);
833 int tls_construct_hello_request(SSL *s)
835 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
836 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
837 ossl_statem_set_error(s);
844 unsigned int dtls_raw_hello_verify_request(unsigned char *buf,
845 unsigned char *cookie,
846 unsigned char cookie_len)
848 unsigned int msg_len;
852 /* Always use DTLS 1.0 version: see RFC 6347 */
853 *(p++) = DTLS1_VERSION >> 8;
854 *(p++) = DTLS1_VERSION & 0xFF;
856 *(p++) = (unsigned char)cookie_len;
857 memcpy(p, cookie, cookie_len);
864 int dtls_construct_hello_verify_request(SSL *s)
869 buf = (unsigned char *)s->init_buf->data;
871 if (s->ctx->app_gen_cookie_cb == NULL ||
872 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
873 &(s->d1->cookie_len)) == 0 ||
874 s->d1->cookie_len > 255) {
875 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
876 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
877 ossl_statem_set_error(s);
881 len = dtls_raw_hello_verify_request(&buf[DTLS1_HM_HEADER_LENGTH],
882 s->d1->cookie, s->d1->cookie_len);
884 dtls1_set_message_header(s, DTLS1_MT_HELLO_VERIFY_REQUEST, len, 0,
886 len += DTLS1_HM_HEADER_LENGTH;
888 /* number of bytes to write */
895 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
897 int i, al = SSL_AD_INTERNAL_ERROR;
898 unsigned int j, complen = 0;
901 #ifndef OPENSSL_NO_COMP
902 SSL_COMP *comp = NULL;
904 STACK_OF(SSL_CIPHER) *ciphers = NULL;
906 /* |cookie| will only be initialized for DTLS. */
907 PACKET session_id, cipher_suites, compression, extensions, cookie;
909 static const unsigned char null_compression = 0;
911 is_v2_record = RECORD_LAYER_is_sslv2_record(&s->rlayer);
913 PACKET_null_init(&cookie);
914 /* First lets get s->client_version set correctly */
916 unsigned int version;
919 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
920 * header is sent directly on the wire, not wrapped as a TLS
921 * record. Our record layer just processes the message length and passes
922 * the rest right through. Its format is:
924 * 0-1 msg_length - decoded by the record layer
925 * 2 msg_type - s->init_msg points here
927 * 5-6 cipher_spec_length
928 * 7-8 session_id_length
929 * 9-10 challenge_length
933 if (!PACKET_get_1(pkt, &mt)
934 || mt != SSL2_MT_CLIENT_HELLO) {
936 * Should never happen. We should have tested this in the record
937 * layer in order to have determined that this is a SSLv2 record
940 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
944 if (!PACKET_get_net_2(pkt, &version)) {
945 /* No protocol version supplied! */
946 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
949 if (version == 0x0002) {
950 /* This is real SSLv2. We don't support it. */
951 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
953 } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {
955 s->client_version = version;
957 /* No idea what protocol this is */
958 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
963 * use version from inside client hello, not from record header (may
964 * differ: see RFC 2246, Appendix E, second paragraph)
966 if(!PACKET_get_net_2(pkt, (unsigned int *)&s->client_version)) {
967 al = SSL_AD_DECODE_ERROR;
968 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
974 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
975 * versions are potentially compatible. Version negotiation comes later.
977 if (!SSL_IS_DTLS(s)) {
978 protverr = ssl_choose_server_version(s);
979 } else if (s->method->version != DTLS_ANY_VERSION &&
980 DTLS_VERSION_LT(s->client_version, s->version)) {
981 protverr = SSL_R_VERSION_TOO_LOW;
987 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
988 if ((!s->enc_write_ctx && !s->write_hash)) {
990 * similar to ssl3_get_record, send alert using remote version
993 s->version = s->client_version;
995 al = SSL_AD_PROTOCOL_VERSION;
999 /* Parse the message and load client random. */
1002 * Handle an SSLv2 backwards compatible ClientHello
1003 * Note, this is only for SSLv3+ using the backward compatible format.
1004 * Real SSLv2 is not supported, and is rejected above.
1006 unsigned int cipher_len, session_id_len, challenge_len;
1009 if (!PACKET_get_net_2(pkt, &cipher_len)
1010 || !PACKET_get_net_2(pkt, &session_id_len)
1011 || !PACKET_get_net_2(pkt, &challenge_len)) {
1012 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1013 SSL_R_RECORD_LENGTH_MISMATCH);
1014 al = SSL_AD_DECODE_ERROR;
1018 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1019 al = SSL_AD_DECODE_ERROR;
1020 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1024 if (!PACKET_get_sub_packet(pkt, &cipher_suites, cipher_len)
1025 || !PACKET_get_sub_packet(pkt, &session_id, session_id_len)
1026 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1027 /* No extensions. */
1028 || PACKET_remaining(pkt) != 0) {
1029 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1030 SSL_R_RECORD_LENGTH_MISMATCH);
1031 al = SSL_AD_DECODE_ERROR;
1035 /* Load the client random and compression list. */
1036 challenge_len = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE :
1038 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1039 if (!PACKET_copy_bytes(&challenge,
1040 s->s3->client_random + SSL3_RANDOM_SIZE -
1041 challenge_len, challenge_len)
1042 /* Advertise only null compression. */
1043 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1044 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1045 al = SSL_AD_INTERNAL_ERROR;
1049 PACKET_null_init(&extensions);
1051 /* Regular ClientHello. */
1052 if (!PACKET_copy_bytes(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
1053 || !PACKET_get_length_prefixed_1(pkt, &session_id)) {
1054 al = SSL_AD_DECODE_ERROR;
1055 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1059 if (PACKET_remaining(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1060 al = SSL_AD_DECODE_ERROR;
1061 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1065 if (SSL_IS_DTLS(s)) {
1066 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1067 al = SSL_AD_DECODE_ERROR;
1068 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1072 * If we require cookies and this ClientHello doesn't contain one,
1073 * just return since we do not want to allocate any memory yet.
1074 * So check cookie length...
1076 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1077 if (PACKET_remaining(&cookie) == 0)
1082 if (!PACKET_get_length_prefixed_2(pkt, &cipher_suites)
1083 || !PACKET_get_length_prefixed_1(pkt, &compression)) {
1084 al = SSL_AD_DECODE_ERROR;
1085 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1088 /* Could be empty. */
1092 if (SSL_IS_DTLS(s)) {
1093 /* Empty cookie was already handled above by returning early. */
1094 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1095 if (s->ctx->app_verify_cookie_cb != NULL) {
1096 if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookie),
1097 PACKET_remaining(&cookie)) == 0) {
1098 al = SSL_AD_HANDSHAKE_FAILURE;
1099 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1100 SSL_R_COOKIE_MISMATCH);
1102 /* else cookie verification succeeded */
1104 /* default verification */
1105 } else if (!PACKET_equal(&cookie, s->d1->cookie,
1106 s->d1->cookie_len)) {
1107 al = SSL_AD_HANDSHAKE_FAILURE;
1108 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1111 s->d1->cookie_verified = 1;
1113 if (s->method->version == DTLS_ANY_VERSION) {
1114 protverr = ssl_choose_server_version(s);
1115 if (protverr != 0) {
1116 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1117 s->version = s->client_version;
1118 al = SSL_AD_PROTOCOL_VERSION;
1127 * We don't allow resumption in a backwards compatible ClientHello.
1128 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1130 * Versions before 0.9.7 always allow clients to resume sessions in
1131 * renegotiation. 0.9.7 and later allow this by default, but optionally
1132 * ignore resumption requests with flag
1133 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1134 * than a change to default behavior so that applications relying on
1135 * this for security won't even compile against older library versions).
1136 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1137 * request renegotiation but not a new session (s->new_session remains
1138 * unset): for servers, this essentially just means that the
1139 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1144 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1145 if (!ssl_get_new_session(s, 1))
1148 i = ssl_get_prev_session(s, &extensions, &session_id);
1150 * Only resume if the session's version matches the negotiated
1152 * RFC 5246 does not provide much useful advice on resumption
1153 * with a different protocol version. It doesn't forbid it but
1154 * the sanity of such behaviour would be questionable.
1155 * In practice, clients do not accept a version mismatch and
1156 * will abort the handshake with an error.
1158 if (i == 1 && s->version == s->session->ssl_version) {
1159 /* previous session */
1161 } else if (i == -1) {
1165 if (!ssl_get_new_session(s, 1))
1170 if (ssl_bytes_to_cipher_list(s, &cipher_suites, &(ciphers),
1171 is_v2_record, &al) == NULL) {
1175 /* If it is a hit, check that the cipher is in the list */
1178 id = s->session->cipher->id;
1181 fprintf(stderr, "client sent %d ciphers\n",
1182 sk_SSL_CIPHER_num(ciphers));
1184 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1185 c = sk_SSL_CIPHER_value(ciphers, i);
1187 fprintf(stderr, "client [%2d of %2d]:%s\n",
1188 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1197 * we need to have the cipher in the cipher list if we are asked
1200 al = SSL_AD_ILLEGAL_PARAMETER;
1201 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1202 SSL_R_REQUIRED_CIPHER_MISSING);
1207 complen = PACKET_remaining(&compression);
1208 for (j = 0; j < complen; j++) {
1209 if (PACKET_data(&compression)[j] == 0)
1215 al = SSL_AD_DECODE_ERROR;
1216 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1220 /* TLS extensions */
1221 if (s->version >= SSL3_VERSION) {
1222 if (!ssl_parse_clienthello_tlsext(s, &extensions)) {
1223 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1229 * Check if we want to use external pre-shared secret for this handshake
1230 * for not reused session only. We need to generate server_random before
1231 * calling tls_session_secret_cb in order to allow SessionTicket
1232 * processing to use it in key derivation.
1236 pos = s->s3->server_random;
1237 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1242 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1243 const SSL_CIPHER *pref_cipher = NULL;
1245 s->session->master_key_length = sizeof(s->session->master_key);
1246 if (s->tls_session_secret_cb(s, s->session->master_key,
1247 &s->session->master_key_length, ciphers,
1249 s->tls_session_secret_cb_arg)) {
1251 s->session->ciphers = ciphers;
1252 s->session->verify_result = X509_V_OK;
1256 /* check if some cipher was preferred by call back */
1258 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1263 if (pref_cipher == NULL) {
1264 al = SSL_AD_HANDSHAKE_FAILURE;
1265 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1269 s->session->cipher = pref_cipher;
1270 sk_SSL_CIPHER_free(s->cipher_list);
1271 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1272 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1273 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1278 * Worst case, we will use the NULL compression, but if we have other
1279 * options, we will now look for them. We have complen-1 compression
1280 * algorithms from the client, starting at q.
1282 s->s3->tmp.new_compression = NULL;
1283 #ifndef OPENSSL_NO_COMP
1284 /* This only happens if we have a cache hit */
1285 if (s->session->compress_meth != 0) {
1286 int m, comp_id = s->session->compress_meth;
1288 /* Perform sanity checks on resumed compression algorithm */
1289 /* Can't disable compression */
1290 if (!ssl_allow_compression(s)) {
1291 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1292 SSL_R_INCONSISTENT_COMPRESSION);
1295 /* Look for resumed compression method */
1296 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1297 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1298 if (comp_id == comp->id) {
1299 s->s3->tmp.new_compression = comp;
1303 if (s->s3->tmp.new_compression == NULL) {
1304 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1305 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1308 /* Look for resumed method in compression list */
1309 for (k = 0; k < complen; k++) {
1310 if (PACKET_data(&compression)[k] == comp_id)
1314 al = SSL_AD_ILLEGAL_PARAMETER;
1315 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1316 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1321 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1322 /* See if we have a match */
1323 int m, nn, v, done = 0;
1326 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1327 for (m = 0; m < nn; m++) {
1328 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1330 for (o = 0; o < complen; o++) {
1331 if (v == PACKET_data(&compression)[o]) {
1340 s->s3->tmp.new_compression = comp;
1346 * If compression is disabled we'd better not try to resume a session
1347 * using compression.
1349 if (s->session->compress_meth != 0) {
1350 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1356 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1360 #ifdef OPENSSL_NO_COMP
1361 s->session->compress_meth = 0;
1363 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1365 sk_SSL_CIPHER_free(s->session->ciphers);
1366 s->session->ciphers = ciphers;
1367 if (ciphers == NULL) {
1368 al = SSL_AD_INTERNAL_ERROR;
1369 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1373 if (!tls1_set_server_sigalgs(s)) {
1374 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1379 sk_SSL_CIPHER_free(ciphers);
1380 return MSG_PROCESS_CONTINUE_PROCESSING;
1382 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1384 ossl_statem_set_error(s);
1386 sk_SSL_CIPHER_free(ciphers);
1387 return MSG_PROCESS_ERROR;
1391 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1393 int al = SSL_AD_HANDSHAKE_FAILURE;
1394 const SSL_CIPHER *cipher;
1396 if (wst == WORK_MORE_A) {
1398 /* Let cert callback update server certificates if required */
1399 if (s->cert->cert_cb) {
1400 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1402 al = SSL_AD_INTERNAL_ERROR;
1403 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1407 s->rwstate = SSL_X509_LOOKUP;
1410 s->rwstate = SSL_NOTHING;
1412 cipher = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1414 if (cipher == NULL) {
1415 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1418 s->s3->tmp.new_cipher = cipher;
1419 /* check whether we should disable session resumption */
1420 if (s->not_resumable_session_cb != NULL)
1421 s->session->not_resumable = s->not_resumable_session_cb(s,
1422 ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0));
1423 if (s->session->not_resumable)
1424 /* do not send a session ticket */
1425 s->tlsext_ticket_expected = 0;
1427 /* Session-id reuse */
1428 s->s3->tmp.new_cipher = s->session->cipher;
1431 if (!(s->verify_mode & SSL_VERIFY_PEER)) {
1432 if (!ssl3_digest_cached_records(s, 0)) {
1433 al = SSL_AD_INTERNAL_ERROR;
1439 * we now have the following setup.
1441 * cipher_list - our prefered list of ciphers
1442 * ciphers - the clients prefered list of ciphers
1443 * compression - basically ignored right now
1444 * ssl version is set - sslv3
1445 * s->session - The ssl session has been setup.
1446 * s->hit - session reuse flag
1447 * s->s3->tmp.new_cipher- the new cipher to use.
1450 /* Handles TLS extensions that we couldn't check earlier */
1451 if (s->version >= SSL3_VERSION) {
1452 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1453 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1454 SSL_R_CLIENTHELLO_TLSEXT);
1461 #ifndef OPENSSL_NO_SRP
1462 if (wst == WORK_MORE_B) {
1464 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1466 * callback indicates further work to be done
1468 s->rwstate = SSL_X509_LOOKUP;
1471 if (ret != SSL_ERROR_NONE) {
1473 * This is not really an error but the only means to for
1474 * a client to detect whether srp is supported.
1476 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1477 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1478 SSL_R_CLIENTHELLO_TLSEXT);
1485 return WORK_FINISHED_STOP;
1487 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1488 ossl_statem_set_error(s);
1492 int tls_construct_server_hello(SSL *s)
1495 unsigned char *p, *d;
1500 buf = (unsigned char *)s->init_buf->data;
1502 /* Do the message type and length last */
1503 d = p = ssl_handshake_start(s);
1505 *(p++) = s->version >> 8;
1506 *(p++) = s->version & 0xff;
1509 * Random stuff. Filling of the server_random takes place in
1510 * tls_process_client_hello()
1512 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1513 p += SSL3_RANDOM_SIZE;
1516 * There are several cases for the session ID to send
1517 * back in the server hello:
1518 * - For session reuse from the session cache,
1519 * we send back the old session ID.
1520 * - If stateless session reuse (using a session ticket)
1521 * is successful, we send back the client's "session ID"
1522 * (which doesn't actually identify the session).
1523 * - If it is a new session, we send back the new
1525 * - However, if we want the new session to be single-use,
1526 * we send back a 0-length session ID.
1527 * s->hit is non-zero in either case of session reuse,
1528 * so the following won't overwrite an ID that we're supposed
1531 if (s->session->not_resumable ||
1532 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1534 s->session->session_id_length = 0;
1536 sl = s->session->session_id_length;
1537 if (sl > (int)sizeof(s->session->session_id)) {
1538 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1539 ossl_statem_set_error(s);
1543 memcpy(p, s->session->session_id, sl);
1546 /* put the cipher */
1547 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1550 /* put the compression method */
1551 #ifdef OPENSSL_NO_COMP
1554 if (s->s3->tmp.new_compression == NULL)
1557 *(p++) = s->s3->tmp.new_compression->id;
1560 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1561 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1562 ossl_statem_set_error(s);
1566 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1568 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1569 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1570 ossl_statem_set_error(s);
1576 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1577 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1578 ossl_statem_set_error(s);
1585 int tls_construct_server_done(SSL *s)
1587 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1588 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1589 ossl_statem_set_error(s);
1593 if (!s->s3->tmp.cert_request) {
1594 if (!ssl3_digest_cached_records(s, 0)) {
1595 ossl_statem_set_error(s);
1602 int tls_construct_server_key_exchange(SSL *s)
1604 #ifndef OPENSSL_NO_DH
1605 EVP_PKEY *pkdh = NULL;
1607 #ifndef OPENSSL_NO_EC
1608 unsigned char *encodedPoint = NULL;
1613 const EVP_MD *md = NULL;
1614 unsigned char *p, *d;
1621 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
1623 if (md_ctx == NULL) {
1624 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1625 al = SSL_AD_INTERNAL_ERROR;
1629 type = s->s3->tmp.new_cipher->algorithm_mkey;
1633 r[0] = r[1] = r[2] = r[3] = NULL;
1635 #ifndef OPENSSL_NO_PSK
1636 if (type & SSL_PSK) {
1638 * reserve size for record length and PSK identity hint
1641 if (s->cert->psk_identity_hint)
1642 n += strlen(s->cert->psk_identity_hint);
1644 /* Plain PSK or RSAPSK nothing to do */
1645 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
1647 #endif /* !OPENSSL_NO_PSK */
1648 #ifndef OPENSSL_NO_DH
1649 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
1650 CERT *cert = s->cert;
1652 EVP_PKEY *pkdhp = NULL;
1655 if (s->cert->dh_tmp_auto) {
1656 DH *dhp = ssl_get_auto_dh(s);
1657 pkdh = EVP_PKEY_new();
1658 if (pkdh == NULL || dhp == NULL) {
1660 al = SSL_AD_INTERNAL_ERROR;
1661 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1662 ERR_R_INTERNAL_ERROR);
1665 EVP_PKEY_assign_DH(pkdh, dhp);
1668 pkdhp = cert->dh_tmp;
1670 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
1671 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1672 pkdh = ssl_dh_to_pkey(dhp);
1674 al = SSL_AD_INTERNAL_ERROR;
1675 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1676 ERR_R_INTERNAL_ERROR);
1681 if (pkdhp == NULL) {
1682 al = SSL_AD_HANDSHAKE_FAILURE;
1683 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1684 SSL_R_MISSING_TMP_DH_KEY);
1687 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1688 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
1689 al = SSL_AD_HANDSHAKE_FAILURE;
1690 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1691 SSL_R_DH_KEY_TOO_SMALL);
1694 if (s->s3->tmp.pkey != NULL) {
1695 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1696 ERR_R_INTERNAL_ERROR);
1700 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp, NID_undef);
1702 if (s->s3->tmp.pkey == NULL) {
1703 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
1707 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
1709 EVP_PKEY_free(pkdh);
1712 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
1713 DH_get0_key(dh, &r[2], NULL);
1716 #ifndef OPENSSL_NO_EC
1717 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1720 if (s->s3->tmp.pkey != NULL) {
1721 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1722 ERR_R_INTERNAL_ERROR);
1726 /* Get NID of appropriate shared curve */
1727 nid = tls1_shared_curve(s, -2);
1728 curve_id = tls1_ec_nid2curve_id(nid);
1729 if (curve_id == 0) {
1730 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1731 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1734 s->s3->tmp.pkey = ssl_generate_pkey(NULL, nid);
1735 /* Generate a new key for this curve */
1736 if (s->s3->tmp.pkey == NULL) {
1737 al = SSL_AD_INTERNAL_ERROR;
1738 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
1742 /* Encode the public key. */
1743 encodedlen = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(s->s3->tmp.pkey),
1744 POINT_CONVERSION_UNCOMPRESSED,
1745 &encodedPoint, NULL);
1747 if (encodedlen == 0) {
1748 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
1753 * We only support named (not generic) curves in ECDH ephemeral key
1754 * exchanges. In this situation, we need four additional bytes to
1755 * encode the entire ServerECDHParams structure.
1757 n += 4 + encodedlen;
1760 * We'll generate the serverKeyExchange message explicitly so we
1761 * can set these to NULLs
1768 #endif /* !OPENSSL_NO_EC */
1769 #ifndef OPENSSL_NO_SRP
1770 if (type & SSL_kSRP) {
1771 if ((s->srp_ctx.N == NULL) ||
1772 (s->srp_ctx.g == NULL) ||
1773 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1774 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1775 SSL_R_MISSING_SRP_PARAM);
1778 r[0] = s->srp_ctx.N;
1779 r[1] = s->srp_ctx.g;
1780 r[2] = s->srp_ctx.s;
1781 r[3] = s->srp_ctx.B;
1785 al = SSL_AD_HANDSHAKE_FAILURE;
1786 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1787 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1790 for (i = 0; i < 4 && r[i] != NULL; i++) {
1791 nr[i] = BN_num_bytes(r[i]);
1792 #ifndef OPENSSL_NO_SRP
1793 if ((i == 2) && (type & SSL_kSRP))
1800 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP))
1801 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
1802 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1804 al = SSL_AD_DECODE_ERROR;
1807 kn = EVP_PKEY_size(pkey);
1808 /* Allow space for signature algorithm */
1809 if (SSL_USE_SIGALGS(s))
1811 /* Allow space for signature length */
1818 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1819 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1822 d = p = ssl_handshake_start(s);
1824 #ifndef OPENSSL_NO_PSK
1825 if (type & SSL_PSK) {
1826 /* copy PSK identity hint */
1827 if (s->cert->psk_identity_hint) {
1828 s2n(strlen(s->cert->psk_identity_hint), p);
1829 strncpy((char *)p, s->cert->psk_identity_hint,
1830 strlen(s->cert->psk_identity_hint));
1831 p += strlen(s->cert->psk_identity_hint);
1838 for (i = 0; i < 4 && r[i] != NULL; i++) {
1839 #ifndef OPENSSL_NO_SRP
1840 if ((i == 2) && (type & SSL_kSRP)) {
1850 #ifndef OPENSSL_NO_EC
1851 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1853 * XXX: For now, we only support named (not generic) curves. In
1854 * this situation, the serverKeyExchange message has: [1 byte
1855 * CurveType], [2 byte CurveName] [1 byte length of encoded
1856 * point], followed by the actual encoded point itself
1858 *p = NAMED_CURVE_TYPE;
1866 memcpy(p, encodedPoint, encodedlen);
1867 OPENSSL_free(encodedPoint);
1868 encodedPoint = NULL;
1876 * n is the length of the params, they start at &(d[4]) and p
1877 * points to the space at the end.
1880 /* send signature algorithm */
1881 if (SSL_USE_SIGALGS(s)) {
1882 if (!tls12_get_sigandhash(p, pkey, md)) {
1883 /* Should never happen */
1884 al = SSL_AD_INTERNAL_ERROR;
1885 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1886 ERR_R_INTERNAL_ERROR);
1892 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
1894 if (EVP_SignInit_ex(md_ctx, md, NULL) <= 0
1895 || EVP_SignUpdate(md_ctx, &(s->s3->client_random[0]),
1896 SSL3_RANDOM_SIZE) <= 0
1897 || EVP_SignUpdate(md_ctx, &(s->s3->server_random[0]),
1898 SSL3_RANDOM_SIZE) <= 0
1899 || EVP_SignUpdate(md_ctx, d, n) <= 0
1900 || EVP_SignFinal(md_ctx, &(p[2]),
1901 (unsigned int *)&i, pkey) <= 0) {
1902 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
1903 al = SSL_AD_INTERNAL_ERROR;
1908 if (SSL_USE_SIGALGS(s))
1911 /* Is this error check actually needed? */
1912 al = SSL_AD_HANDSHAKE_FAILURE;
1913 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1914 SSL_R_UNKNOWN_PKEY_TYPE);
1919 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
1920 al = SSL_AD_HANDSHAKE_FAILURE;
1921 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1925 EVP_MD_CTX_free(md_ctx);
1928 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1930 #ifndef OPENSSL_NO_DH
1931 EVP_PKEY_free(pkdh);
1933 #ifndef OPENSSL_NO_EC
1934 OPENSSL_free(encodedPoint);
1936 EVP_MD_CTX_free(md_ctx);
1937 ossl_statem_set_error(s);
1941 int tls_construct_certificate_request(SSL *s)
1943 unsigned char *p, *d;
1944 int i, j, nl, off, n;
1945 STACK_OF(X509_NAME) *sk = NULL;
1951 d = p = ssl_handshake_start(s);
1953 /* get the list of acceptable cert types */
1955 n = ssl3_get_req_cert_type(s, p);
1960 if (SSL_USE_SIGALGS(s)) {
1961 const unsigned char *psigs;
1962 unsigned char *etmp = p;
1963 nl = tls12_get_psigalgs(s, &psigs);
1964 /* Skip over length for now */
1966 nl = tls12_copy_sigalgs(s, p, psigs, nl);
1967 /* Now fill in length */
1977 sk = SSL_get_client_CA_list(s);
1980 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1981 name = sk_X509_NAME_value(sk, i);
1982 j = i2d_X509_NAME(name, NULL);
1983 if (!BUF_MEM_grow_clean
1984 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
1985 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
1989 p = ssl_handshake_start(s) + n;
1991 i2d_X509_NAME(name, &p);
1996 /* else no CA names */
1997 p = ssl_handshake_start(s) + off;
2000 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2001 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2005 s->s3->tmp.cert_request = 1;
2009 ossl_statem_set_error(s);
2013 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
2016 unsigned long alg_k;
2017 #ifndef OPENSSL_NO_RSA
2020 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2021 EVP_PKEY *ckey = NULL;
2023 PACKET enc_premaster;
2024 unsigned char *rsa_decrypt = NULL;
2026 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2028 #ifndef OPENSSL_NO_PSK
2029 /* For PSK parse and retrieve identity, obtain PSK key */
2030 if (alg_k & SSL_PSK) {
2031 unsigned char psk[PSK_MAX_PSK_LEN];
2033 PACKET psk_identity;
2035 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2036 al = SSL_AD_DECODE_ERROR;
2037 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2040 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2041 al = SSL_AD_DECODE_ERROR;
2042 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2043 SSL_R_DATA_LENGTH_TOO_LONG);
2046 if (s->psk_server_callback == NULL) {
2047 al = SSL_AD_INTERNAL_ERROR;
2048 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2049 SSL_R_PSK_NO_SERVER_CB);
2053 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2054 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2055 al = SSL_AD_INTERNAL_ERROR;
2059 psklen = s->psk_server_callback(s, s->session->psk_identity,
2062 if (psklen > PSK_MAX_PSK_LEN) {
2063 al = SSL_AD_INTERNAL_ERROR;
2064 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2066 } else if (psklen == 0) {
2068 * PSK related to the given identity not found
2070 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2071 SSL_R_PSK_IDENTITY_NOT_FOUND);
2072 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2076 OPENSSL_free(s->s3->tmp.psk);
2077 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2078 OPENSSL_cleanse(psk, psklen);
2080 if (s->s3->tmp.psk == NULL) {
2081 al = SSL_AD_INTERNAL_ERROR;
2082 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2086 s->s3->tmp.psklen = psklen;
2088 if (alg_k & SSL_kPSK) {
2089 /* Identity extracted earlier: should be nothing left */
2090 if (PACKET_remaining(pkt) != 0) {
2091 al = SSL_AD_HANDSHAKE_FAILURE;
2092 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2095 /* PSK handled by ssl_generate_master_secret */
2096 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2097 al = SSL_AD_INTERNAL_ERROR;
2098 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2103 #ifndef OPENSSL_NO_RSA
2104 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2105 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2107 unsigned char decrypt_good, version_good;
2108 size_t j, padding_len;
2110 /* FIX THIS UP EAY EAY EAY EAY */
2111 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey);
2113 al = SSL_AD_HANDSHAKE_FAILURE;
2114 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2115 SSL_R_MISSING_RSA_CERTIFICATE);
2119 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2120 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2121 enc_premaster = *pkt;
2123 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2124 || PACKET_remaining(pkt) != 0) {
2125 al = SSL_AD_DECODE_ERROR;
2126 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2127 SSL_R_LENGTH_MISMATCH);
2133 * We want to be sure that the plaintext buffer size makes it safe to
2134 * iterate over the entire size of a premaster secret
2135 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2136 * their ciphertext cannot accommodate a premaster secret anyway.
2138 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2139 al = SSL_AD_INTERNAL_ERROR;
2140 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2141 RSA_R_KEY_SIZE_TOO_SMALL);
2145 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2146 if (rsa_decrypt == NULL) {
2147 al = SSL_AD_INTERNAL_ERROR;
2148 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2153 * We must not leak whether a decryption failure occurs because of
2154 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2155 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2156 * generates a random premaster secret for the case that the decrypt
2157 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2160 if (RAND_bytes(rand_premaster_secret,
2161 sizeof(rand_premaster_secret)) <= 0) {
2166 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2167 * the timing-sensitive code below.
2169 decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),
2170 PACKET_data(&enc_premaster),
2171 rsa_decrypt, rsa, RSA_NO_PADDING);
2172 if (decrypt_len < 0) {
2176 /* Check the padding. See RFC 3447, section 7.2.2. */
2179 * The smallest padded premaster is 11 bytes of overhead. Small keys
2180 * are publicly invalid, so this may return immediately. This ensures
2181 * PS is at least 8 bytes.
2183 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2184 al = SSL_AD_DECRYPT_ERROR;
2185 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_DECRYPTION_FAILED);
2189 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2190 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2191 constant_time_eq_int_8(rsa_decrypt[1], 2);
2192 for (j = 2; j < padding_len - 1; j++) {
2193 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2195 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2198 * If the version in the decrypted pre-master secret is correct then
2199 * version_good will be 0xff, otherwise it'll be zero. The
2200 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2201 * (http://eprint.iacr.org/2003/052/) exploits the version number
2202 * check as a "bad version oracle". Thus version checks are done in
2203 * constant time and are treated like any other decryption error.
2206 constant_time_eq_8(rsa_decrypt[padding_len],
2207 (unsigned)(s->client_version >> 8));
2209 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2210 (unsigned)(s->client_version & 0xff));
2213 * The premaster secret must contain the same version number as the
2214 * ClientHello to detect version rollback attacks (strangely, the
2215 * protocol does not offer such protection for DH ciphersuites).
2216 * However, buggy clients exist that send the negotiated protocol
2217 * version instead if the server does not support the requested
2218 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2221 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2222 unsigned char workaround_good;
2223 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2224 (unsigned)(s->version >> 8));
2226 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2227 (unsigned)(s->version & 0xff));
2228 version_good |= workaround_good;
2232 * Both decryption and version must be good for decrypt_good to
2233 * remain non-zero (0xff).
2235 decrypt_good &= version_good;
2238 * Now copy rand_premaster_secret over from p using
2239 * decrypt_good_mask. If decryption failed, then p does not
2240 * contain valid plaintext, however, a check above guarantees
2241 * it is still sufficiently large to read from.
2243 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2244 rsa_decrypt[padding_len + j] =
2245 constant_time_select_8(decrypt_good,
2246 rsa_decrypt[padding_len + j],
2247 rand_premaster_secret[j]);
2250 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2251 sizeof(rand_premaster_secret), 0)) {
2252 al = SSL_AD_INTERNAL_ERROR;
2253 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2256 OPENSSL_free(rsa_decrypt);
2260 #ifndef OPENSSL_NO_DH
2261 if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2262 EVP_PKEY *skey = NULL;
2266 const unsigned char *data;
2268 if (!PACKET_get_net_2(pkt, &i)) {
2269 if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2270 al = SSL_AD_HANDSHAKE_FAILURE;
2271 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2272 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2277 if (PACKET_remaining(pkt) != i) {
2278 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2279 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2282 skey = s->s3->tmp.pkey;
2284 al = SSL_AD_HANDSHAKE_FAILURE;
2285 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2286 SSL_R_MISSING_TMP_DH_KEY);
2290 if (PACKET_remaining(pkt) == 0L) {
2291 al = SSL_AD_HANDSHAKE_FAILURE;
2292 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2293 SSL_R_MISSING_TMP_DH_KEY);
2296 if (!PACKET_get_bytes(pkt, &data, i)) {
2297 /* We already checked we have enough data */
2298 al = SSL_AD_INTERNAL_ERROR;
2299 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2300 ERR_R_INTERNAL_ERROR);
2303 ckey = EVP_PKEY_new();
2304 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2305 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2308 cdh = EVP_PKEY_get0_DH(ckey);
2309 pub_key = BN_bin2bn(data, i, NULL);
2311 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2312 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2313 if (pub_key != NULL)
2318 if (ssl_derive(s, skey, ckey) == 0) {
2319 al = SSL_AD_INTERNAL_ERROR;
2320 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2324 EVP_PKEY_free(ckey);
2326 EVP_PKEY_free(s->s3->tmp.pkey);
2327 s->s3->tmp.pkey = NULL;
2332 #ifndef OPENSSL_NO_EC
2333 if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2334 EVP_PKEY *skey = s->s3->tmp.pkey;
2336 if (PACKET_remaining(pkt) == 0L) {
2337 /* We don't support ECDH client auth */
2338 al = SSL_AD_HANDSHAKE_FAILURE;
2339 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2340 SSL_R_MISSING_TMP_ECDH_KEY);
2344 const unsigned char *data;
2347 * Get client's public key from encoded point in the
2348 * ClientKeyExchange message.
2351 /* Get encoded point length */
2352 if (!PACKET_get_1(pkt, &i)) {
2353 al = SSL_AD_DECODE_ERROR;
2354 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2355 SSL_R_LENGTH_MISMATCH);
2358 if (!PACKET_get_bytes(pkt, &data, i)
2359 || PACKET_remaining(pkt) != 0) {
2360 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2363 ckey = EVP_PKEY_new();
2364 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
2365 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);
2368 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(ckey), data, i,
2370 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2375 if (ssl_derive(s, skey, ckey) == 0) {
2376 al = SSL_AD_INTERNAL_ERROR;
2377 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2381 EVP_PKEY_free(ckey);
2383 EVP_PKEY_free(s->s3->tmp.pkey);
2384 s->s3->tmp.pkey = NULL;
2386 return MSG_PROCESS_CONTINUE_PROCESSING;
2389 #ifndef OPENSSL_NO_SRP
2390 if (alg_k & SSL_kSRP) {
2392 const unsigned char *data;
2394 if (!PACKET_get_net_2(pkt, &i)
2395 || !PACKET_get_bytes(pkt, &data, i)) {
2396 al = SSL_AD_DECODE_ERROR;
2397 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BAD_SRP_A_LENGTH);
2400 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2401 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2404 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2405 || BN_is_zero(s->srp_ctx.A)) {
2406 al = SSL_AD_ILLEGAL_PARAMETER;
2407 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2408 SSL_R_BAD_SRP_PARAMETERS);
2411 OPENSSL_free(s->session->srp_username);
2412 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2413 if (s->session->srp_username == NULL) {
2414 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2418 if (!srp_generate_server_master_secret(s)) {
2419 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2423 #endif /* OPENSSL_NO_SRP */
2424 #ifndef OPENSSL_NO_GOST
2425 if (alg_k & SSL_kGOST) {
2426 EVP_PKEY_CTX *pkey_ctx;
2427 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2428 unsigned char premaster_secret[32];
2429 const unsigned char *start;
2430 size_t outlen = 32, inlen;
2431 unsigned long alg_a;
2435 const unsigned char *data;
2437 /* Get our certificate private key */
2438 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2439 if (alg_a & SSL_aGOST12) {
2441 * New GOST ciphersuites have SSL_aGOST01 bit too
2443 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2445 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2448 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2450 } else if (alg_a & SSL_aGOST01) {
2451 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2454 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2455 if (pkey_ctx == NULL) {
2456 al = SSL_AD_INTERNAL_ERROR;
2457 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2460 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2461 al = SSL_AD_INTERNAL_ERROR;
2462 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2466 * If client certificate is present and is of the same type, maybe
2467 * use it for key exchange. Don't mind errors from
2468 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2469 * client certificate for authorization only.
2471 client_pub_pkey = X509_get0_pubkey(s->session->peer);
2472 if (client_pub_pkey) {
2473 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2476 /* Decrypt session key */
2477 sess_key_len = PACKET_remaining(pkt);
2478 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2479 al = SSL_AD_INTERNAL_ERROR;
2480 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2483 if (ASN1_get_object ((const unsigned char **)&data, &Tlen, &Ttag,
2484 &Tclass, sess_key_len) != V_ASN1_CONSTRUCTED
2485 || Ttag != V_ASN1_SEQUENCE
2486 || Tclass != V_ASN1_UNIVERSAL) {
2487 al = SSL_AD_DECODE_ERROR;
2488 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2489 SSL_R_DECRYPTION_FAILED);
2494 if (EVP_PKEY_decrypt
2495 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2496 al = SSL_AD_DECODE_ERROR;
2497 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2498 SSL_R_DECRYPTION_FAILED);
2501 /* Generate master secret */
2502 if (!ssl_generate_master_secret(s, premaster_secret,
2503 sizeof(premaster_secret), 0)) {
2504 al = SSL_AD_INTERNAL_ERROR;
2505 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2508 /* Check if pubkey from client certificate was used */
2509 if (EVP_PKEY_CTX_ctrl
2510 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2511 s->statem.no_cert_verify = 1;
2513 EVP_PKEY_CTX_free(pkey_ctx);
2514 return MSG_PROCESS_CONTINUE_PROCESSING;
2516 EVP_PKEY_CTX_free(pkey_ctx);
2521 al = SSL_AD_HANDSHAKE_FAILURE;
2522 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2526 return MSG_PROCESS_CONTINUE_PROCESSING;
2528 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2529 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2532 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2533 EVP_PKEY_free(ckey);
2535 OPENSSL_free(rsa_decrypt);
2536 #ifndef OPENSSL_NO_PSK
2537 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2538 s->s3->tmp.psk = NULL;
2540 ossl_statem_set_error(s);
2541 return MSG_PROCESS_ERROR;
2544 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
2546 #ifndef OPENSSL_NO_SCTP
2547 if (wst == WORK_MORE_A) {
2548 if (SSL_IS_DTLS(s)) {
2549 unsigned char sctpauthkey[64];
2550 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2552 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2555 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2556 sizeof(DTLS1_SCTP_AUTH_LABEL));
2558 if (SSL_export_keying_material(s, sctpauthkey,
2559 sizeof(sctpauthkey), labelbuffer,
2560 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
2561 ossl_statem_set_error(s);
2565 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2566 sizeof(sctpauthkey), sctpauthkey);
2571 if ((wst == WORK_MORE_B)
2573 && BIO_dgram_is_sctp(SSL_get_wbio(s))
2574 /* Are we renegotiating? */
2576 /* Are we going to skip the CertificateVerify? */
2577 && (s->session->peer == NULL || s->statem.no_cert_verify)
2578 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
2579 s->s3->in_read_app_data = 2;
2580 s->rwstate = SSL_READING;
2581 BIO_clear_retry_flags(SSL_get_rbio(s));
2582 BIO_set_retry_read(SSL_get_rbio(s));
2583 ossl_statem_set_sctp_read_sock(s, 1);
2586 ossl_statem_set_sctp_read_sock(s, 0);
2590 if (s->statem.no_cert_verify || !s->session->peer) {
2591 /* No certificate verify or no peer certificate so we no longer need the
2594 if (!ssl3_digest_cached_records(s, 0)) {
2595 ossl_statem_set_error(s);
2598 return WORK_FINISHED_CONTINUE;
2600 if (!s->s3->handshake_buffer) {
2601 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
2602 ERR_R_INTERNAL_ERROR);
2603 ossl_statem_set_error(s);
2607 * For sigalgs freeze the handshake buffer. If we support
2608 * extms we've done this already so this is a no-op
2610 if (!ssl3_digest_cached_records(s, 1)) {
2611 ossl_statem_set_error(s);
2616 return WORK_FINISHED_CONTINUE;
2619 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
2621 EVP_PKEY *pkey = NULL;
2622 const unsigned char *sig, *data;
2623 #ifndef OPENSSL_NO_GOST
2624 unsigned char *gost_data = NULL;
2626 int al, ret = MSG_PROCESS_ERROR;
2630 const EVP_MD *md = NULL;
2634 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
2637 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
2638 al = SSL_AD_INTERNAL_ERROR;
2642 peer = s->session->peer;
2643 pkey = X509_get0_pubkey(peer);
2644 type = X509_certificate_type(peer, pkey);
2646 if (!(type & EVP_PKT_SIGN)) {
2647 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
2648 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2649 al = SSL_AD_ILLEGAL_PARAMETER;
2653 /* Check for broken implementations of GOST ciphersuites */
2655 * If key is GOST and n is exactly 64, it is bare signature without
2656 * length field (CryptoPro implementations at least till CSP 4.0)
2658 #ifndef OPENSSL_NO_GOST
2659 if (PACKET_remaining(pkt) == 64
2660 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) {
2665 if (SSL_USE_SIGALGS(s)) {
2668 if (!PACKET_get_bytes(pkt, &sig, 2)) {
2669 al = SSL_AD_DECODE_ERROR;
2672 rv = tls12_check_peer_sigalg(&md, s, sig, pkey);
2674 al = SSL_AD_INTERNAL_ERROR;
2676 } else if (rv == 0) {
2677 al = SSL_AD_DECODE_ERROR;
2681 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2684 /* Use default digest for this key type */
2685 int idx = ssl_cert_type(NULL, pkey);
2687 md = s->s3->tmp.md[idx];
2689 al = SSL_AD_INTERNAL_ERROR;
2694 if (!PACKET_get_net_2(pkt, &len)) {
2695 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2696 al = SSL_AD_DECODE_ERROR;
2700 j = EVP_PKEY_size(pkey);
2701 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
2702 || (PACKET_remaining(pkt) == 0)) {
2703 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2704 al = SSL_AD_DECODE_ERROR;
2707 if (!PACKET_get_bytes(pkt, &data, len)) {
2708 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2709 al = SSL_AD_DECODE_ERROR;
2713 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2714 if (hdatalen <= 0) {
2715 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2716 al = SSL_AD_INTERNAL_ERROR;
2720 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
2722 if (!EVP_VerifyInit_ex(mctx, md, NULL)
2723 || !EVP_VerifyUpdate(mctx, hdata, hdatalen)) {
2724 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
2725 al = SSL_AD_INTERNAL_ERROR;
2729 #ifndef OPENSSL_NO_GOST
2731 int pktype = EVP_PKEY_id(pkey);
2732 if (pktype == NID_id_GostR3410_2001
2733 || pktype == NID_id_GostR3410_2012_256
2734 || pktype == NID_id_GostR3410_2012_512) {
2735 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
2736 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
2737 al = SSL_AD_INTERNAL_ERROR;
2740 BUF_reverse(gost_data, data, len);
2746 if (s->version == SSL3_VERSION
2747 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2748 s->session->master_key_length,
2749 s->session->master_key)) {
2750 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
2751 al = SSL_AD_INTERNAL_ERROR;
2755 if (EVP_VerifyFinal(mctx, data, len, pkey) <= 0) {
2756 al = SSL_AD_DECRYPT_ERROR;
2757 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
2761 ret = MSG_PROCESS_CONTINUE_PROCESSING;
2764 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2765 ossl_statem_set_error(s);
2767 BIO_free(s->s3->handshake_buffer);
2768 s->s3->handshake_buffer = NULL;
2769 EVP_MD_CTX_free(mctx);
2770 #ifndef OPENSSL_NO_GOST
2771 OPENSSL_free(gost_data);
2776 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
2778 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
2780 unsigned long l, llen;
2781 const unsigned char *certstart, *certbytes;
2782 STACK_OF(X509) *sk = NULL;
2785 if ((sk = sk_X509_new_null()) == NULL) {
2786 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2790 if (!PACKET_get_net_3(pkt, &llen)
2791 || !PACKET_get_sub_packet(pkt, &spkt, llen)
2792 || PACKET_remaining(pkt) != 0) {
2793 al = SSL_AD_DECODE_ERROR;
2794 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
2798 while (PACKET_remaining(&spkt) > 0) {
2799 if (!PACKET_get_net_3(&spkt, &l)
2800 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
2801 al = SSL_AD_DECODE_ERROR;
2802 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
2803 SSL_R_CERT_LENGTH_MISMATCH);
2807 certstart = certbytes;
2808 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
2810 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
2813 if (certbytes != (certstart + l)) {
2814 al = SSL_AD_DECODE_ERROR;
2815 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
2816 SSL_R_CERT_LENGTH_MISMATCH);
2819 if (!sk_X509_push(sk, x)) {
2820 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2826 if (sk_X509_num(sk) <= 0) {
2827 /* TLS does not mind 0 certs returned */
2828 if (s->version == SSL3_VERSION) {
2829 al = SSL_AD_HANDSHAKE_FAILURE;
2830 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
2831 SSL_R_NO_CERTIFICATES_RETURNED);
2834 /* Fail for TLS only if we required a certificate */
2835 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2836 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2837 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
2838 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2839 al = SSL_AD_HANDSHAKE_FAILURE;
2842 /* No client certificate so digest cached records */
2843 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
2848 i = ssl_verify_cert_chain(s, sk);
2850 al = ssl_verify_alarm_type(s->verify_result);
2851 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
2852 SSL_R_CERTIFICATE_VERIFY_FAILED);
2856 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
2857 al = SSL_AD_HANDSHAKE_FAILURE;
2860 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
2862 al = SSL3_AD_HANDSHAKE_FAILURE;
2863 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
2864 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
2869 X509_free(s->session->peer);
2870 s->session->peer = sk_X509_shift(sk);
2871 s->session->verify_result = s->verify_result;
2873 sk_X509_pop_free(s->session->peer_chain, X509_free);
2874 s->session->peer_chain = sk;
2876 * Inconsistency alert: cert_chain does *not* include the peer's own
2877 * certificate, while we do include it in statem_clnt.c
2880 ret = MSG_PROCESS_CONTINUE_READING;
2884 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2885 ossl_statem_set_error(s);
2888 sk_X509_pop_free(sk, X509_free);
2892 int tls_construct_server_certificate(SSL *s)
2896 cpk = ssl_get_server_send_pkey(s);
2898 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2899 ossl_statem_set_error(s);
2903 if (!ssl3_output_cert_chain(s, cpk)) {
2904 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2905 ossl_statem_set_error(s);
2912 int tls_construct_new_session_ticket(SSL *s)
2914 unsigned char *senc = NULL;
2915 EVP_CIPHER_CTX *ctx;
2916 HMAC_CTX *hctx = NULL;
2917 unsigned char *p, *macstart;
2918 const unsigned char *const_p;
2919 int len, slen_full, slen;
2922 SSL_CTX *tctx = s->initial_ctx;
2923 unsigned char iv[EVP_MAX_IV_LENGTH];
2924 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
2927 /* get session encoding length */
2928 slen_full = i2d_SSL_SESSION(s->session, NULL);
2930 * Some length values are 16 bits, so forget it if session is too
2933 if (slen_full == 0 || slen_full > 0xFF00) {
2934 ossl_statem_set_error(s);
2937 senc = OPENSSL_malloc(slen_full);
2939 ossl_statem_set_error(s);
2943 ctx = EVP_CIPHER_CTX_new();
2944 hctx = HMAC_CTX_new();
2947 if (!i2d_SSL_SESSION(s->session, &p))
2951 * create a fresh copy (not shared with other threads) to clean up
2954 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
2957 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
2959 slen = i2d_SSL_SESSION(sess, NULL);
2960 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
2961 SSL_SESSION_free(sess);
2965 if (!i2d_SSL_SESSION(sess, &p)) {
2966 SSL_SESSION_free(sess);
2969 SSL_SESSION_free(sess);
2972 * Grow buffer if need be: the length calculation is as
2973 * follows handshake_header_length +
2974 * 4 (ticket lifetime hint) + 2 (ticket length) +
2975 * sizeof(keyname) + max_iv_len (iv length) +
2976 * max_enc_block_size (max encrypted session * length) +
2977 * max_md_size (HMAC) + session_length.
2979 if (!BUF_MEM_grow(s->init_buf,
2980 SSL_HM_HEADER_LENGTH(s) + 6 + sizeof(key_name) +
2981 EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2982 EVP_MAX_MD_SIZE + slen))
2985 p = ssl_handshake_start(s);
2987 * Initialize HMAC and cipher contexts. If callback present it does
2988 * all the work otherwise use generated values from parent ctx.
2990 if (tctx->tlsext_ticket_key_cb) {
2991 /* if 0 is returned, write an empty ticket */
2992 int ret = tctx->tlsext_ticket_key_cb(s, key_name, iv, ctx,
2996 l2n(0, p); /* timeout */
2997 s2n(0, p); /* length */
2998 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, p - ssl_handshake_start(s)))
3001 EVP_CIPHER_CTX_free(ctx);
3002 HMAC_CTX_free(hctx);
3007 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3009 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3011 iv_len = EVP_CIPHER_iv_length(cipher);
3012 if (RAND_bytes(iv, iv_len) <= 0)
3014 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
3015 tctx->tlsext_tick_aes_key, iv))
3017 if (!HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3018 sizeof(tctx->tlsext_tick_hmac_key),
3019 EVP_sha256(), NULL))
3021 memcpy(key_name, tctx->tlsext_tick_key_name,
3022 sizeof(tctx->tlsext_tick_key_name));
3026 * Ticket lifetime hint (advisory only): We leave this unspecified
3027 * for resumed session (for simplicity), and guess that tickets for
3028 * new sessions will live as long as their sessions.
3030 l2n(s->hit ? 0 : s->session->timeout, p);
3032 /* Skip ticket length for now */
3034 /* Output key name */
3036 memcpy(p, key_name, sizeof(key_name));
3037 p += sizeof(key_name);
3039 memcpy(p, iv, iv_len);
3041 /* Encrypt session data */
3042 if (!EVP_EncryptUpdate(ctx, p, &len, senc, slen))
3045 if (!EVP_EncryptFinal(ctx, p, &len))
3049 if (!HMAC_Update(hctx, macstart, p - macstart))
3051 if (!HMAC_Final(hctx, p, &hlen))
3054 EVP_CIPHER_CTX_free(ctx);
3055 HMAC_CTX_free(hctx);
3060 /* Now write out lengths: p points to end of data written */
3062 len = p - ssl_handshake_start(s);
3063 /* Skip ticket lifetime hint */
3064 p = ssl_handshake_start(s) + 4;
3066 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3073 EVP_CIPHER_CTX_free(ctx);
3074 HMAC_CTX_free(hctx);
3075 ossl_statem_set_error(s);
3079 int tls_construct_cert_status(SSL *s)
3083 * Grow buffer if need be: the length calculation is as
3084 * follows 1 (message type) + 3 (message length) +
3085 * 1 (ocsp response type) + 3 (ocsp response length)
3088 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3089 ossl_statem_set_error(s);
3093 p = (unsigned char *)s->init_buf->data;
3096 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3097 /* message length */
3098 l2n3(s->tlsext_ocsp_resplen + 4, p);
3100 *(p++) = s->tlsext_status_type;
3101 /* length of OCSP response */
3102 l2n3(s->tlsext_ocsp_resplen, p);
3103 /* actual response */
3104 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3105 /* number of bytes to write */
3106 s->init_num = 8 + s->tlsext_ocsp_resplen;
3112 #ifndef OPENSSL_NO_NEXTPROTONEG
3114 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3115 * It sets the next_proto member in s if found
3117 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3119 PACKET next_proto, padding;
3120 size_t next_proto_len;
3123 * The payload looks like:
3125 * uint8 proto[proto_len];
3126 * uint8 padding_len;
3127 * uint8 padding[padding_len];
3129 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3130 || !PACKET_get_length_prefixed_1(pkt, &padding)
3131 || PACKET_remaining(pkt) > 0) {
3132 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3136 if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated,
3138 s->next_proto_negotiated_len = 0;
3142 s->next_proto_negotiated_len = (unsigned char)next_proto_len;
3144 return MSG_PROCESS_CONTINUE_READING;
3146 ossl_statem_set_error(s);
3147 return MSG_PROCESS_ERROR;
3151 #define SSLV2_CIPHER_LEN 3
3153 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
3154 PACKET *cipher_suites,
3155 STACK_OF(SSL_CIPHER) **skp,
3156 int sslv2format, int *al
3159 const SSL_CIPHER *c;
3160 STACK_OF(SSL_CIPHER) *sk;
3162 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
3163 unsigned char cipher[SSLV2_CIPHER_LEN];
3165 s->s3->send_connection_binding = 0;
3167 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
3169 if (PACKET_remaining(cipher_suites) == 0) {
3170 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
3171 *al = SSL_AD_ILLEGAL_PARAMETER;
3175 if (PACKET_remaining(cipher_suites) % n != 0) {
3176 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3177 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3178 *al = SSL_AD_DECODE_ERROR;
3182 if ((skp == NULL) || (*skp == NULL)) {
3183 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3185 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3186 *al = SSL_AD_INTERNAL_ERROR;
3191 sk_SSL_CIPHER_zero(sk);
3194 if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
3195 &s->s3->tmp.ciphers_rawlen)) {
3196 *al = SSL_AD_INTERNAL_ERROR;
3200 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
3202 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
3203 * first byte set to zero, while true SSLv2 ciphers have a non-zero
3204 * first byte. We don't support any true SSLv2 ciphers, so skip them.
3206 if (sslv2format && cipher[0] != '\0')
3209 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3210 if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3211 (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3212 /* SCSV fatal if renegotiating */
3213 if (s->renegotiate) {
3214 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3215 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3216 *al = SSL_AD_HANDSHAKE_FAILURE;
3219 s->s3->send_connection_binding = 1;
3223 /* Check for TLS_FALLBACK_SCSV */
3224 if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3225 (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3227 * The SCSV indicates that the client previously tried a higher
3228 * version. Fail if the current version is an unexpected
3231 if (!ssl_check_version_downgrade(s)) {
3232 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3233 SSL_R_INAPPROPRIATE_FALLBACK);
3234 *al = SSL_AD_INAPPROPRIATE_FALLBACK;
3240 /* For SSLv2-compat, ignore leading 0-byte. */
3241 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher);
3243 if (!sk_SSL_CIPHER_push(sk, c)) {
3244 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3245 *al = SSL_AD_INTERNAL_ERROR;
3250 if (PACKET_remaining(cipher_suites) > 0) {
3251 *al = SSL_AD_INTERNAL_ERROR;
3252 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);
3260 if ((skp == NULL) || (*skp == NULL))
3261 sk_SSL_CIPHER_free(sk);