1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
153 #include "ssl_locl.h"
154 #include "internal/constant_time_locl.h"
155 #include <openssl/buffer.h>
156 #include <openssl/rand.h>
157 #include <openssl/objects.h>
158 #include <openssl/evp.h>
159 #include <openssl/hmac.h>
160 #include <openssl/x509.h>
161 #ifndef OPENSSL_NO_DH
162 # include <openssl/dh.h>
164 #include <openssl/bn.h>
165 #include <openssl/md5.h>
167 static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
168 int num, STACK_OF(SSL_CIPHER) **skp, int sslv2format);
171 #ifndef OPENSSL_NO_SRP
172 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
174 int ret = SSL_ERROR_NONE;
176 *al = SSL_AD_UNRECOGNIZED_NAME;
178 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
179 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
180 if (s->srp_ctx.login == NULL) {
182 * RFC 5054 says SHOULD reject, we do so if There is no srp
186 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
188 ret = SSL_srp_server_param_with_username(s, al);
195 int ssl3_accept(SSL *s)
198 unsigned long alg_k, Time = (unsigned long)time(NULL);
199 void (*cb) (const SSL *ssl, int type, int val) = NULL;
201 int new_state, state, skip = 0;
203 RAND_add(&Time, sizeof(Time), 0);
207 if (s->info_callback != NULL)
208 cb = s->info_callback;
209 else if (s->ctx->info_callback != NULL)
210 cb = s->ctx->info_callback;
212 /* init things to blank */
214 if (!SSL_in_init(s) || SSL_in_before(s)) {
219 #ifndef OPENSSL_NO_HEARTBEATS
221 * If we're awaiting a HeartbeatResponse, pretend we already got and
222 * don't await it anymore, because Heartbeats don't make sense during
225 if (s->tlsext_hb_pending) {
226 s->tlsext_hb_pending = 0;
235 case SSL_ST_RENEGOTIATE:
237 /* s->state=SSL_ST_ACCEPT; */
241 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
242 case SSL_ST_OK | SSL_ST_ACCEPT:
246 cb(s, SSL_CB_HANDSHAKE_START, 1);
248 if ((s->version >> 8 != 3) && s->version != TLS_ANY_VERSION) {
249 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
250 s->state = SSL_ST_ERR;
254 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
255 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_VERSION_TOO_LOW);
259 s->type = SSL_ST_ACCEPT;
261 if (s->init_buf == NULL) {
262 if ((buf = BUF_MEM_new()) == NULL) {
264 s->state = SSL_ST_ERR;
267 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
270 s->state = SSL_ST_ERR;
276 if (!ssl3_setup_buffers(s)) {
278 s->state = SSL_ST_ERR;
283 s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
285 * Should have been reset by ssl3_get_finished, too.
287 s->s3->change_cipher_spec = 0;
289 if (s->state != SSL_ST_RENEGOTIATE) {
291 * Ok, we now need to push on a buffering BIO so that the
292 * output is sent in a way that TCP likes :-)
294 if (!ssl_init_wbio_buffer(s, 1)) {
296 s->state = SSL_ST_ERR;
300 ssl3_init_finished_mac(s);
301 s->state = SSL3_ST_SR_CLNT_HELLO_A;
302 s->ctx->stats.sess_accept++;
303 } else if (!s->s3->send_connection_binding &&
305 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
307 * Server attempting to renegotiate with client that doesn't
308 * support secure renegotiation.
310 SSLerr(SSL_F_SSL3_ACCEPT,
311 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
312 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
314 s->state = SSL_ST_ERR;
318 * s->state == SSL_ST_RENEGOTIATE, we will just send a
321 s->ctx->stats.sess_accept_renegotiate++;
322 s->state = SSL3_ST_SW_HELLO_REQ_A;
326 case SSL3_ST_SW_HELLO_REQ_A:
327 case SSL3_ST_SW_HELLO_REQ_B:
330 ret = ssl3_send_hello_request(s);
333 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
334 s->state = SSL3_ST_SW_FLUSH;
337 ssl3_init_finished_mac(s);
340 case SSL3_ST_SW_HELLO_REQ_C:
341 s->state = SSL_ST_OK;
344 case SSL3_ST_SR_CLNT_HELLO_A:
345 case SSL3_ST_SR_CLNT_HELLO_B:
346 case SSL3_ST_SR_CLNT_HELLO_C:
348 ret = ssl3_get_client_hello(s);
351 #ifndef OPENSSL_NO_SRP
352 s->state = SSL3_ST_SR_CLNT_HELLO_D;
353 case SSL3_ST_SR_CLNT_HELLO_D:
356 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
358 * callback indicates firther work to be done
360 s->rwstate = SSL_X509_LOOKUP;
363 if (ret != SSL_ERROR_NONE) {
364 ssl3_send_alert(s, SSL3_AL_FATAL, al);
366 * This is not really an error but the only means to for
367 * a client to detect whether srp is supported.
369 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
370 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
371 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
373 s->state = SSL_ST_ERR;
380 s->state = SSL3_ST_SW_SRVR_HELLO_A;
384 case SSL3_ST_SW_SRVR_HELLO_A:
385 case SSL3_ST_SW_SRVR_HELLO_B:
386 ret = ssl3_send_server_hello(s);
391 if (s->tlsext_ticket_expected)
392 s->state = SSL3_ST_SW_SESSION_TICKET_A;
394 s->state = SSL3_ST_SW_CHANGE_A;
396 s->state = SSL3_ST_SW_CERT_A;
401 case SSL3_ST_SW_CERT_A:
402 case SSL3_ST_SW_CERT_B:
403 /* Check if it is anon DH or anon ECDH, */
404 /* normal PSK or SRP */
405 if (!(s->s3->tmp.new_cipher->algorithm_auth &
406 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
407 ret = ssl3_send_server_certificate(s);
411 if (s->tlsext_status_expected)
412 s->state = SSL3_ST_SW_CERT_STATUS_A;
414 s->state = SSL3_ST_SW_KEY_EXCH_A;
417 s->state = SSL3_ST_SW_KEY_EXCH_A;
422 case SSL3_ST_SW_KEY_EXCH_A:
423 case SSL3_ST_SW_KEY_EXCH_B:
424 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
427 * clear this, it may get reset by
428 * send_server_key_exchange
430 s->s3->tmp.use_rsa_tmp = 0;
433 * only send if a DH key exchange, fortezza or RSA but we have a
434 * sign only certificate PSK: may send PSK identity hints For
435 * ECC ciphersuites, we send a serverKeyExchange message only if
436 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
437 * the server certificate contains the server's public key for
442 * PSK: send ServerKeyExchange if PSK identity hint if
445 #ifndef OPENSSL_NO_PSK
446 /* Only send SKE if we have identity hint for plain PSK */
447 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK)) && s->cert->psk_identity_hint)
448 /* For other PSK always send SKE */
449 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
451 #ifndef OPENSSL_NO_SRP
452 /* SRP: send ServerKeyExchange */
453 || (alg_k & SSL_kSRP)
455 || (alg_k & SSL_kDHE)
456 || (alg_k & SSL_kECDHE)
457 || ((alg_k & SSL_kRSA)
458 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
459 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
460 && EVP_PKEY_size(s->cert->pkeys
461 [SSL_PKEY_RSA_ENC].privatekey) *
462 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
467 ret = ssl3_send_server_key_exchange(s);
473 s->state = SSL3_ST_SW_CERT_REQ_A;
477 case SSL3_ST_SW_CERT_REQ_A:
478 case SSL3_ST_SW_CERT_REQ_B:
479 if ( /* don't request cert unless asked for it: */
480 !(s->verify_mode & SSL_VERIFY_PEER) ||
482 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
483 * during re-negotiation:
485 ((s->session->peer != NULL) &&
486 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
488 * never request cert in anonymous ciphersuites (see
489 * section "Certificate request" in SSL 3 drafts and in
492 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
494 * ... except when the application insists on
495 * verification (against the specs, but s3_clnt.c accepts
498 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
499 /* don't request certificate for SRP auth */
500 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
502 * With normal PSK Certificates and Certificate Requests
505 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
506 /* no cert request */
508 s->s3->tmp.cert_request = 0;
509 s->state = SSL3_ST_SW_SRVR_DONE_A;
510 if (!ssl3_digest_cached_records(s, 0)) {
511 s->state = SSL_ST_ERR;
515 s->s3->tmp.cert_request = 1;
516 ret = ssl3_send_certificate_request(s);
519 s->state = SSL3_ST_SW_SRVR_DONE_A;
524 case SSL3_ST_SW_SRVR_DONE_A:
525 case SSL3_ST_SW_SRVR_DONE_B:
526 ret = ssl3_send_server_done(s);
529 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
530 s->state = SSL3_ST_SW_FLUSH;
534 case SSL3_ST_SW_FLUSH:
537 * This code originally checked to see if any data was pending
538 * using BIO_CTRL_INFO and then flushed. This caused problems as
539 * documented in PR#1939. The proposed fix doesn't completely
540 * resolve this issue as buggy implementations of
541 * BIO_CTRL_PENDING still exist. So instead we just flush
545 s->rwstate = SSL_WRITING;
546 if (BIO_flush(s->wbio) <= 0) {
550 s->rwstate = SSL_NOTHING;
552 s->state = s->s3->tmp.next_state;
555 case SSL3_ST_SR_CERT_A:
556 case SSL3_ST_SR_CERT_B:
557 if (s->s3->tmp.cert_request) {
558 ret = ssl3_get_client_certificate(s);
563 s->state = SSL3_ST_SR_KEY_EXCH_A;
566 case SSL3_ST_SR_KEY_EXCH_A:
567 case SSL3_ST_SR_KEY_EXCH_B:
568 ret = ssl3_get_client_key_exchange(s);
573 * For the ECDH ciphersuites when the client sends its ECDH
574 * pub key in a certificate, the CertificateVerify message is
575 * not sent. Also for GOST ciphersuites when the client uses
576 * its key from the certificate for key exchange.
578 s->state = SSL3_ST_SR_CHANGE_A;
580 } else if (SSL_USE_SIGALGS(s)) {
581 s->state = SSL3_ST_SR_CERT_VRFY_A;
583 if (!s->session->peer)
585 if (!s->s3->handshake_buffer) {
586 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
587 s->state = SSL_ST_ERR;
591 * For sigalgs freeze the handshake buffer. If we support
592 * extms we've done this already so this is a no-op
594 if (!ssl3_digest_cached_records(s, 1)) {
595 s->state = SSL_ST_ERR;
602 s->state = SSL3_ST_SR_CERT_VRFY_A;
606 * We need to get hashes here so if there is a client cert,
607 * it can be verified FIXME - digest processing for
608 * CertificateVerify should be generalized. But it is next
611 if (!ssl3_digest_cached_records(s, 0)) {
612 s->state = SSL_ST_ERR;
615 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
616 if (s->s3->handshake_dgst[dgst_num]) {
619 s->method->ssl3_enc->cert_verify_mac(s,
628 EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
630 s->state = SSL_ST_ERR;
639 case SSL3_ST_SR_CERT_VRFY_A:
640 case SSL3_ST_SR_CERT_VRFY_B:
641 ret = ssl3_get_cert_verify(s);
645 s->state = SSL3_ST_SR_CHANGE_A;
649 #if !defined(OPENSSL_NO_NEXTPROTONEG)
650 case SSL3_ST_SR_NEXT_PROTO_A:
651 case SSL3_ST_SR_NEXT_PROTO_B:
652 ret = ssl3_get_next_proto(s);
656 s->state = SSL3_ST_SR_FINISHED_A;
661 case SSL3_ST_SR_CHANGE_A:
662 case SSL3_ST_SR_CHANGE_B:
663 ret = ssl3_get_change_cipher_spec(s, SSL3_ST_SR_CHANGE_A,
664 SSL3_ST_SR_CHANGE_B);
668 #if defined(OPENSSL_NO_NEXTPROTONEG)
669 s->state = SSL3_ST_SR_FINISHED_A;
671 if (s->s3->next_proto_neg_seen)
672 s->state = SSL3_ST_SR_NEXT_PROTO_A;
674 s->state = SSL3_ST_SR_FINISHED_A;
679 case SSL3_ST_SR_FINISHED_A:
680 case SSL3_ST_SR_FINISHED_B:
681 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
682 SSL3_ST_SR_FINISHED_B);
686 s->state = SSL_ST_OK;
687 else if (s->tlsext_ticket_expected)
688 s->state = SSL3_ST_SW_SESSION_TICKET_A;
690 s->state = SSL3_ST_SW_CHANGE_A;
694 case SSL3_ST_SW_SESSION_TICKET_A:
695 case SSL3_ST_SW_SESSION_TICKET_B:
696 ret = ssl3_send_newsession_ticket(s);
699 s->state = SSL3_ST_SW_CHANGE_A;
703 case SSL3_ST_SW_CERT_STATUS_A:
704 case SSL3_ST_SW_CERT_STATUS_B:
705 ret = ssl3_send_cert_status(s);
708 s->state = SSL3_ST_SW_KEY_EXCH_A;
712 case SSL3_ST_SW_CHANGE_A:
713 case SSL3_ST_SW_CHANGE_B:
715 s->session->cipher = s->s3->tmp.new_cipher;
716 if (!s->method->ssl3_enc->setup_key_block(s)) {
718 s->state = SSL_ST_ERR;
722 ret = ssl3_send_change_cipher_spec(s,
724 SSL3_ST_SW_CHANGE_B);
728 s->state = SSL3_ST_SW_FINISHED_A;
731 if (!s->method->ssl3_enc->change_cipher_state(s,
732 SSL3_CHANGE_CIPHER_SERVER_WRITE))
735 s->state = SSL_ST_ERR;
741 case SSL3_ST_SW_FINISHED_A:
742 case SSL3_ST_SW_FINISHED_B:
743 ret = ssl3_send_finished(s,
744 SSL3_ST_SW_FINISHED_A,
745 SSL3_ST_SW_FINISHED_B,
747 ssl3_enc->server_finished_label,
749 ssl3_enc->server_finished_label_len);
752 s->state = SSL3_ST_SW_FLUSH;
754 s->s3->tmp.next_state = SSL3_ST_SR_CHANGE_A;
756 s->s3->tmp.next_state = SSL_ST_OK;
761 /* clean a few things up */
762 ssl3_cleanup_key_block(s);
764 BUF_MEM_free(s->init_buf);
767 /* remove buffering on output */
768 ssl_free_wbio_buffer(s);
772 if (s->renegotiate == 2) { /* skipped if we just sent a
777 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
779 s->ctx->stats.sess_accept_good++;
781 s->handshake_func = ssl3_accept;
784 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
793 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
799 if (!s->s3->tmp.reuse_message && !skip) {
801 if ((ret = BIO_flush(s->wbio)) <= 0)
805 if ((cb != NULL) && (s->state != state)) {
806 new_state = s->state;
808 cb(s, SSL_CB_ACCEPT_LOOP, 1);
809 s->state = new_state;
815 /* BIO_flush(s->wbio); */
819 cb(s, SSL_CB_ACCEPT_EXIT, ret);
823 int ssl3_send_hello_request(SSL *s)
826 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
827 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
828 SSLerr(SSL_F_SSL3_SEND_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
831 s->state = SSL3_ST_SW_HELLO_REQ_B;
834 /* SSL3_ST_SW_HELLO_REQ_B */
835 return ssl_do_write(s);
838 int ssl3_get_client_hello(SSL *s)
840 int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
841 unsigned int j, complen = 0;
845 #ifndef OPENSSL_NO_COMP
846 SSL_COMP *comp = NULL;
848 STACK_OF(SSL_CIPHER) *ciphers = NULL;
850 PACKET pkt, cipher_suite, compression;
852 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
856 * We do this so that we will respond with our native type. If we are
857 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
858 * switching should be handled by a different method. If we are SSLv3, we
859 * will respond with SSLv3, even if prompted with TLSv1.
861 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
862 s->state = SSL3_ST_SR_CLNT_HELLO_B;
865 n = s->method->ssl_get_message(s,
866 SSL3_ST_SR_CLNT_HELLO_B,
867 SSL3_ST_SR_CLNT_HELLO_C,
868 SSL3_MT_CLIENT_HELLO,
869 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
874 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
875 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
876 al = SSL_AD_INTERNAL_ERROR;
880 /* First lets get s->client_version set correctly */
881 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
882 unsigned int version;
885 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
886 * header is sent directly on the wire, not wrapped as a TLS
887 * record. Our record layer just processes the message length and passes
888 * the rest right through. Its format is:
890 * 0-1 msg_length - decoded by the record layer
891 * 2 msg_type - s->init_msg points here
893 * 5-6 cipher_spec_length
894 * 7-8 session_id_length
895 * 9-10 challenge_length
899 if (!PACKET_get_1(&pkt, &mt)
900 || mt != SSL2_MT_CLIENT_HELLO) {
902 * Should never happen. We should have tested this in the record
903 * layer in order to have determined that this is a SSLv2 record
906 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
910 if (!PACKET_get_net_2(&pkt, &version)) {
911 /* No protocol version supplied! */
912 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
915 if (version == 0x0002) {
916 /* This is real SSLv2. We don't support it. */
917 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
919 } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {
921 s->client_version = version;
923 /* No idea what protocol this is */
924 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
929 * use version from inside client hello, not from record header (may
930 * differ: see RFC 2246, Appendix E, second paragraph)
932 if(!PACKET_get_net_2(&pkt, (unsigned int *)&s->client_version)) {
933 al = SSL_AD_DECODE_ERROR;
934 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
939 /* Do SSL/TLS version negotiation if applicable */
940 if (!SSL_IS_DTLS(s)) {
941 if (s->version != TLS_ANY_VERSION) {
942 if (s->client_version >= s->version) {
945 } else if (s->client_version >= SSL3_VERSION) {
946 switch(s->client_version) {
949 if(!(s->options & SSL_OP_NO_TLSv1_2)) {
950 s->version = TLS1_2_VERSION;
951 s->method = TLSv1_2_server_method();
955 /* Deliberately fall through */
957 if(!(s->options & SSL_OP_NO_TLSv1_1)) {
958 s->version = TLS1_1_VERSION;
959 s->method = TLSv1_1_server_method();
963 /* Deliberately fall through */
965 if(!(s->options & SSL_OP_NO_TLSv1)) {
966 s->version = TLS1_VERSION;
967 s->method = TLSv1_server_method();
971 /* Deliberately fall through */
973 #ifndef OPENSSL_NO_SSL3
974 if(!(s->options & SSL_OP_NO_SSLv3)) {
975 s->version = SSL3_VERSION;
976 s->method = SSLv3_server_method();
985 } else if (s->client_version <= s->version
986 || s->method->version == DTLS_ANY_VERSION) {
988 * For DTLS we just check versions are potentially compatible. Version
989 * negotiation comes later.
995 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
996 if ((!s->enc_write_ctx && !s->write_hash)) {
998 * similar to ssl3_get_record, send alert using remote version
1001 s->version = s->client_version;
1003 al = SSL_AD_PROTOCOL_VERSION;
1007 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1009 * Handle an SSLv2 backwards compatible ClientHello
1010 * Note, this is only for SSLv3+ using the backward compatible format.
1011 * Real SSLv2 is not supported, and is rejected above.
1013 unsigned int cipher_len, session_id_len, challenge_len;
1015 if (!PACKET_get_net_2(&pkt, &cipher_len)
1016 || !PACKET_get_net_2(&pkt, &session_id_len)
1017 || !PACKET_get_net_2(&pkt, &challenge_len)) {
1018 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1019 al = SSL_AD_DECODE_ERROR;
1023 if (cipher_len == 0) {
1024 /* we need at least one cipher */
1025 al = SSL_AD_ILLEGAL_PARAMETER;
1026 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1030 if (!PACKET_get_sub_packet(&pkt, &cipher_suite, cipher_len)) {
1031 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1032 al = SSL_AD_DECODE_ERROR;
1036 if (ssl_bytes_to_cipher_list(s, PACKET_data(&cipher_suite),
1037 cipher_len, &(ciphers), 1) == NULL) {
1042 * Ignore any session id. We don't allow resumption in a backwards
1043 * compatible ClientHello
1045 if (!PACKET_forward(&pkt, session_id_len)) {
1046 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1047 al = SSL_AD_DECODE_ERROR;
1052 if (!ssl_get_new_session(s, 1))
1055 /* Load the client random */
1056 i = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE : challenge_len;
1057 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1058 if (!PACKET_peek_copy_bytes(&pkt,
1059 s->s3->client_random + SSL3_RANDOM_SIZE - i,
1061 || !PACKET_forward(&pkt, challenge_len)
1062 || PACKET_remaining(&pkt) != 0) {
1063 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1064 al = SSL_AD_DECODE_ERROR;
1068 /* If we get here we've got SSLv3+ in an SSLv3+ record */
1070 unsigned int cookie_len;
1071 /* load the client random and get the session-id */
1072 if (!PACKET_copy_bytes(&pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
1073 || !PACKET_get_length_prefixed_1(&pkt, &session_id)) {
1074 al = SSL_AD_DECODE_ERROR;
1075 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1080 * If we require cookies and this ClientHello doesn't contain one, just
1081 * return since we do not want to allocate any memory yet. So check
1084 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1086 if (!PACKET_peek_1(&pkt, &cookie_len)) {
1087 al = SSL_AD_DECODE_ERROR;
1088 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1092 if (cookie_len == 0)
1098 * Versions before 0.9.7 always allow clients to resume sessions in
1099 * renegotiation. 0.9.7 and later allow this by default, but optionally
1100 * ignore resumption requests with flag
1101 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1102 * than a change to default behavior so that applications relying on
1103 * this for security won't even compile against older library versions).
1104 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1105 * request renegotiation but not a new session (s->new_session remains
1106 * unset): for servers, this essentially just means that the
1107 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1111 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1112 if (!ssl_get_new_session(s, 1))
1116 * TODO(openssl-team): ssl_get_prev_session passes a non-const
1117 * 'unsigned char*' session id to a user callback. Grab a copy of
1120 i = ssl_get_prev_session(s, &pkt, PACKET_data(&session_id),
1121 PACKET_remaining(&session_id));
1123 * Only resume if the session's version matches the negotiated
1125 * RFC 5246 does not provide much useful advice on resumption
1126 * with a different protocol version. It doesn't forbid it but
1127 * the sanity of such behaviour would be questionable.
1128 * In practice, clients do not accept a version mismatch and
1129 * will abort the handshake with an error.
1131 if (i == 1 && s->version == s->session->ssl_version) {
1132 /* previous session */
1138 if (!ssl_get_new_session(s, 1))
1143 if (SSL_IS_DTLS(s)) {
1145 if (!PACKET_get_length_prefixed_1(&pkt, &cookie)) {
1146 al = SSL_AD_DECODE_ERROR;
1147 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1150 cookie_len = PACKET_remaining(&cookie);
1152 * The ClientHello may contain a cookie even if the
1153 * HelloVerify message has not been sent--make sure that it
1154 * does not cause an overflow.
1156 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1158 al = SSL_AD_DECODE_ERROR;
1159 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1163 /* verify the cookie if appropriate option is set. */
1164 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
1165 && cookie_len > 0) {
1168 * TODO(openssl-team): rcvd_cookie appears unused outside this
1169 * function. Remove the field?
1171 if (!PACKET_copy_bytes(&cookie, s->d1->rcvd_cookie, cookie_len)) {
1172 al = SSL_AD_INTERNAL_ERROR;
1173 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1177 if (s->ctx->app_verify_cookie_cb != NULL) {
1178 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1180 al = SSL_AD_HANDSHAKE_FAILURE;
1181 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1182 SSL_R_COOKIE_MISMATCH);
1185 /* else cookie verification succeeded */
1187 /* default verification */
1188 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1189 s->d1->cookie_len) != 0) {
1190 al = SSL_AD_HANDSHAKE_FAILURE;
1191 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1194 /* Set to -2 so if successful we return 2 */
1197 if (s->method->version == DTLS_ANY_VERSION) {
1198 /* Select version to use */
1199 if (s->client_version <= DTLS1_2_VERSION &&
1200 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1201 s->version = DTLS1_2_VERSION;
1202 s->method = DTLSv1_2_server_method();
1203 } else if (tls1_suiteb(s)) {
1204 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1205 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1206 s->version = s->client_version;
1207 al = SSL_AD_PROTOCOL_VERSION;
1209 } else if (s->client_version <= DTLS1_VERSION &&
1210 !(s->options & SSL_OP_NO_DTLSv1)) {
1211 s->version = DTLS1_VERSION;
1212 s->method = DTLSv1_server_method();
1214 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1215 SSL_R_WRONG_VERSION_NUMBER);
1216 s->version = s->client_version;
1217 al = SSL_AD_PROTOCOL_VERSION;
1220 s->session->ssl_version = s->version;
1224 if (!PACKET_get_length_prefixed_2(&pkt, &cipher_suite)) {
1225 al = SSL_AD_DECODE_ERROR;
1226 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1230 if (PACKET_remaining(&cipher_suite) == 0) {
1231 al = SSL_AD_ILLEGAL_PARAMETER;
1232 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1236 if (ssl_bytes_to_cipher_list(s, PACKET_data(&cipher_suite),
1237 PACKET_remaining(&cipher_suite),
1238 &(ciphers), 0) == NULL) {
1242 /* If it is a hit, check that the cipher is in the list */
1245 id = s->session->cipher->id;
1248 fprintf(stderr, "client sent %d ciphers\n",
1249 sk_SSL_CIPHER_num(ciphers));
1251 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1252 c = sk_SSL_CIPHER_value(ciphers, i);
1254 fprintf(stderr, "client [%2d of %2d]:%s\n",
1255 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1263 * Disabled because it can be used in a ciphersuite downgrade
1268 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1269 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1271 * Special case as client bug workaround: the previously used
1272 * cipher may not be in the current list, the client instead
1273 * might be trying to continue using a cipher that before wasn't
1274 * chosen due to server preferences. We'll have to reject the
1275 * connection if the cipher is not enabled, though.
1277 c = sk_SSL_CIPHER_value(ciphers, 0);
1278 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1279 s->session->cipher = c;
1286 * we need to have the cipher in the cipher list if we are asked
1289 al = SSL_AD_ILLEGAL_PARAMETER;
1290 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1291 SSL_R_REQUIRED_CIPHER_MISSING);
1297 if (!PACKET_get_length_prefixed_1(&pkt, &compression)) {
1298 /* not enough data */
1299 al = SSL_AD_DECODE_ERROR;
1301 * TODO(openssl-team):
1302 * SSL_R_LENGTH_TOO_SHORT and SSL_R_LENGTH_MISMATCH are used
1303 * interchangeably. Pick one.
1305 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1309 complen = PACKET_remaining(&compression);
1310 for (j = 0; j < complen; j++) {
1311 if (PACKET_data(&compression)[j] == 0)
1317 al = SSL_AD_DECODE_ERROR;
1318 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1323 /* TLS extensions */
1324 if (s->version >= SSL3_VERSION) {
1325 if (!ssl_parse_clienthello_tlsext(s, &pkt)) {
1326 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1332 * Check if we want to use external pre-shared secret for this handshake
1333 * for not reused session only. We need to generate server_random before
1334 * calling tls_session_secret_cb in order to allow SessionTicket
1335 * processing to use it in key derivation.
1339 pos = s->s3->server_random;
1340 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1345 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1346 SSL_CIPHER *pref_cipher = NULL;
1348 s->session->master_key_length = sizeof(s->session->master_key);
1349 if (s->tls_session_secret_cb(s, s->session->master_key,
1350 &s->session->master_key_length, ciphers,
1352 s->tls_session_secret_cb_arg)) {
1354 s->session->ciphers = ciphers;
1355 s->session->verify_result = X509_V_OK;
1359 /* check if some cipher was preferred by call back */
1361 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1366 if (pref_cipher == NULL) {
1367 al = SSL_AD_HANDSHAKE_FAILURE;
1368 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1372 s->session->cipher = pref_cipher;
1373 sk_SSL_CIPHER_free(s->cipher_list);
1374 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1375 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1376 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1381 * Worst case, we will use the NULL compression, but if we have other
1382 * options, we will now look for them. We have complen-1 compression
1383 * algorithms from the client, starting at q.
1385 s->s3->tmp.new_compression = NULL;
1386 #ifndef OPENSSL_NO_COMP
1387 /* This only happens if we have a cache hit */
1388 if (s->session->compress_meth != 0) {
1389 int m, comp_id = s->session->compress_meth;
1391 /* Perform sanity checks on resumed compression algorithm */
1392 /* Can't disable compression */
1393 if (!ssl_allow_compression(s)) {
1394 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1395 SSL_R_INCONSISTENT_COMPRESSION);
1398 /* Look for resumed compression method */
1399 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1400 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1401 if (comp_id == comp->id) {
1402 s->s3->tmp.new_compression = comp;
1406 if (s->s3->tmp.new_compression == NULL) {
1407 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1408 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1411 /* Look for resumed method in compression list */
1412 for (k = 0; k < complen; k++) {
1413 if (PACKET_data(&compression)[k] == comp_id)
1417 al = SSL_AD_ILLEGAL_PARAMETER;
1418 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1419 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1424 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1425 /* See if we have a match */
1426 int m, nn, v, done = 0;
1429 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1430 for (m = 0; m < nn; m++) {
1431 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1433 for (o = 0; o < complen; o++) {
1434 if (v == PACKET_data(&compression)[o]) {
1443 s->s3->tmp.new_compression = comp;
1449 * If compression is disabled we'd better not try to resume a session
1450 * using compression.
1452 if (s->session->compress_meth != 0) {
1453 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1459 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1463 #ifdef OPENSSL_NO_COMP
1464 s->session->compress_meth = 0;
1466 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1468 sk_SSL_CIPHER_free(s->session->ciphers);
1469 s->session->ciphers = ciphers;
1470 if (ciphers == NULL) {
1471 al = SSL_AD_INTERNAL_ERROR;
1472 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1476 if (!tls1_set_server_sigalgs(s)) {
1477 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1480 /* Let cert callback update server certificates if required */
1482 if (s->cert->cert_cb) {
1483 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1485 al = SSL_AD_INTERNAL_ERROR;
1486 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1490 s->rwstate = SSL_X509_LOOKUP;
1493 s->rwstate = SSL_NOTHING;
1495 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1498 al = SSL_AD_HANDSHAKE_FAILURE;
1499 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1502 s->s3->tmp.new_cipher = c;
1503 /* check whether we should disable session resumption */
1504 if (s->not_resumable_session_cb != NULL)
1505 s->session->not_resumable = s->not_resumable_session_cb(s,
1506 ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE))
1508 if (s->session->not_resumable)
1509 /* do not send a session ticket */
1510 s->tlsext_ticket_expected = 0;
1512 /* Session-id reuse */
1513 s->s3->tmp.new_cipher = s->session->cipher;
1516 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1517 if (!ssl3_digest_cached_records(s, 0))
1522 * we now have the following setup.
1524 * cipher_list - our prefered list of ciphers
1525 * ciphers - the clients prefered list of ciphers
1526 * compression - basically ignored right now
1527 * ssl version is set - sslv3
1528 * s->session - The ssl session has been setup.
1529 * s->hit - session reuse flag
1530 * s->s3->tmp.new_cipher- the new cipher to use.
1533 /* Handles TLS extensions that we couldn't check earlier */
1534 if (s->version >= SSL3_VERSION) {
1535 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1536 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1545 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1547 s->state = SSL_ST_ERR;
1550 sk_SSL_CIPHER_free(ciphers);
1551 return ret < 0 ? -1 : ret;
1554 int ssl3_send_server_hello(SSL *s)
1557 unsigned char *p, *d;
1562 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1563 buf = (unsigned char *)s->init_buf->data;
1565 /* Do the message type and length last */
1566 d = p = ssl_handshake_start(s);
1568 *(p++) = s->version >> 8;
1569 *(p++) = s->version & 0xff;
1572 * Random stuff. Filling of the server_random takes place in
1573 * ssl3_get_client_hello()
1575 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1576 p += SSL3_RANDOM_SIZE;
1579 * There are several cases for the session ID to send
1580 * back in the server hello:
1581 * - For session reuse from the session cache,
1582 * we send back the old session ID.
1583 * - If stateless session reuse (using a session ticket)
1584 * is successful, we send back the client's "session ID"
1585 * (which doesn't actually identify the session).
1586 * - If it is a new session, we send back the new
1588 * - However, if we want the new session to be single-use,
1589 * we send back a 0-length session ID.
1590 * s->hit is non-zero in either case of session reuse,
1591 * so the following won't overwrite an ID that we're supposed
1594 if (s->session->not_resumable ||
1595 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1597 s->session->session_id_length = 0;
1599 sl = s->session->session_id_length;
1600 if (sl > (int)sizeof(s->session->session_id)) {
1601 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1602 s->state = SSL_ST_ERR;
1606 memcpy(p, s->session->session_id, sl);
1609 /* put the cipher */
1610 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1613 /* put the compression method */
1614 #ifdef OPENSSL_NO_COMP
1617 if (s->s3->tmp.new_compression == NULL)
1620 *(p++) = s->s3->tmp.new_compression->id;
1623 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1624 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1625 s->state = SSL_ST_ERR;
1629 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1631 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1632 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1633 s->state = SSL_ST_ERR;
1639 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1640 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1643 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1646 /* SSL3_ST_SW_SRVR_HELLO_B */
1647 return ssl_do_write(s);
1650 int ssl3_send_server_done(SSL *s)
1653 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1654 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1655 SSLerr(SSL_F_SSL3_SEND_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1658 s->state = SSL3_ST_SW_SRVR_DONE_B;
1661 /* SSL3_ST_SW_SRVR_DONE_B */
1662 return ssl_do_write(s);
1665 int ssl3_send_server_key_exchange(SSL *s)
1667 #ifndef OPENSSL_NO_RSA
1671 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1674 #ifndef OPENSSL_NO_DH
1675 DH *dh = NULL, *dhp;
1677 #ifndef OPENSSL_NO_EC
1678 EC_KEY *ecdh = NULL, *ecdhp;
1679 unsigned char *encodedPoint = NULL;
1682 BN_CTX *bn_ctx = NULL;
1685 const EVP_MD *md = NULL;
1686 unsigned char *p, *d;
1696 EVP_MD_CTX_init(&md_ctx);
1697 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1698 type = s->s3->tmp.new_cipher->algorithm_mkey;
1703 r[0] = r[1] = r[2] = r[3] = NULL;
1705 #ifndef OPENSSL_NO_PSK
1706 if (type & SSL_PSK) {
1708 * reserve size for record length and PSK identity hint
1711 if (s->cert->psk_identity_hint)
1712 n += strlen(s->cert->psk_identity_hint);
1714 /* Plain PSK or RSAPSK nothing to do */
1715 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
1717 #endif /* !OPENSSL_NO_PSK */
1718 #ifndef OPENSSL_NO_RSA
1719 if (type & SSL_kRSA) {
1720 rsa = cert->rsa_tmp;
1721 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1722 rsa = s->cert->rsa_tmp_cb(s,
1723 SSL_C_IS_EXPORT(s->s3->
1725 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1728 al = SSL_AD_HANDSHAKE_FAILURE;
1729 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1730 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1734 cert->rsa_tmp = rsa;
1737 al = SSL_AD_HANDSHAKE_FAILURE;
1738 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1739 SSL_R_MISSING_TMP_RSA_KEY);
1744 s->s3->tmp.use_rsa_tmp = 1;
1747 #ifndef OPENSSL_NO_DH
1748 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
1749 if (s->cert->dh_tmp_auto) {
1750 dhp = ssl_get_auto_dh(s);
1752 al = SSL_AD_INTERNAL_ERROR;
1753 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1754 ERR_R_INTERNAL_ERROR);
1759 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1760 dhp = s->cert->dh_tmp_cb(s,
1761 SSL_C_IS_EXPORT(s->s3->
1763 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1766 al = SSL_AD_HANDSHAKE_FAILURE;
1767 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1768 SSL_R_MISSING_TMP_DH_KEY);
1771 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1772 DH_security_bits(dhp), 0, dhp)) {
1773 al = SSL_AD_HANDSHAKE_FAILURE;
1774 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1775 SSL_R_DH_KEY_TOO_SMALL);
1778 if (s->s3->tmp.dh != NULL) {
1779 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1780 ERR_R_INTERNAL_ERROR);
1784 if (s->cert->dh_tmp_auto)
1786 else if ((dh = DHparams_dup(dhp)) == NULL) {
1787 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1792 if ((dhp->pub_key == NULL ||
1793 dhp->priv_key == NULL ||
1794 (s->options & SSL_OP_SINGLE_DH_USE))) {
1795 if (!DH_generate_key(dh)) {
1796 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1800 dh->pub_key = BN_dup(dhp->pub_key);
1801 dh->priv_key = BN_dup(dhp->priv_key);
1802 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1803 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1812 #ifndef OPENSSL_NO_EC
1813 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1814 const EC_GROUP *group;
1816 ecdhp = cert->ecdh_tmp;
1817 if (s->cert->ecdh_tmp_auto) {
1818 /* Get NID of appropriate shared curve */
1819 int nid = tls1_shared_curve(s, -2);
1820 if (nid != NID_undef)
1821 ecdhp = EC_KEY_new_by_curve_name(nid);
1822 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1823 ecdhp = s->cert->ecdh_tmp_cb(s,
1824 SSL_C_IS_EXPORT(s->s3->
1826 SSL_C_EXPORT_PKEYLENGTH(s->
1827 s3->tmp.new_cipher));
1829 if (ecdhp == NULL) {
1830 al = SSL_AD_HANDSHAKE_FAILURE;
1831 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1832 SSL_R_MISSING_TMP_ECDH_KEY);
1836 if (s->s3->tmp.ecdh != NULL) {
1837 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1838 ERR_R_INTERNAL_ERROR);
1842 /* Duplicate the ECDH structure. */
1843 if (ecdhp == NULL) {
1844 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1847 if (s->cert->ecdh_tmp_auto)
1849 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1850 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1854 s->s3->tmp.ecdh = ecdh;
1855 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1856 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1857 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1858 if (!EC_KEY_generate_key(ecdh)) {
1859 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1865 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1866 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1867 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1868 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1872 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1873 (EC_GROUP_get_degree(group) > 163)) {
1874 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1875 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1880 * XXX: For now, we only support ephemeral ECDH keys over named
1881 * (not generic) curves. For supported named curves, curve_id is
1885 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1887 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1888 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1893 * Encode the public key. First check the size of encoding and
1894 * allocate memory accordingly.
1896 encodedlen = EC_POINT_point2oct(group,
1897 EC_KEY_get0_public_key(ecdh),
1898 POINT_CONVERSION_UNCOMPRESSED,
1901 encodedPoint = (unsigned char *)
1902 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1903 bn_ctx = BN_CTX_new();
1904 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1905 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1906 ERR_R_MALLOC_FAILURE);
1910 encodedlen = EC_POINT_point2oct(group,
1911 EC_KEY_get0_public_key(ecdh),
1912 POINT_CONVERSION_UNCOMPRESSED,
1913 encodedPoint, encodedlen, bn_ctx);
1915 if (encodedlen == 0) {
1916 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1920 BN_CTX_free(bn_ctx);
1924 * XXX: For now, we only support named (not generic) curves in
1925 * ECDH ephemeral key exchanges. In this situation, we need four
1926 * additional bytes to encode the entire ServerECDHParams
1929 n += 4 + encodedlen;
1932 * We'll generate the serverKeyExchange message explicitly so we
1933 * can set these to NULLs
1940 #endif /* !OPENSSL_NO_EC */
1941 #ifndef OPENSSL_NO_SRP
1942 if (type & SSL_kSRP) {
1943 if ((s->srp_ctx.N == NULL) ||
1944 (s->srp_ctx.g == NULL) ||
1945 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1946 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1947 SSL_R_MISSING_SRP_PARAM);
1950 r[0] = s->srp_ctx.N;
1951 r[1] = s->srp_ctx.g;
1952 r[2] = s->srp_ctx.s;
1953 r[3] = s->srp_ctx.B;
1957 al = SSL_AD_HANDSHAKE_FAILURE;
1958 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1959 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1962 for (i = 0; i < 4 && r[i] != NULL; i++) {
1963 nr[i] = BN_num_bytes(r[i]);
1964 #ifndef OPENSSL_NO_SRP
1965 if ((i == 2) && (type & SSL_kSRP))
1972 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP))
1973 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
1974 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1976 al = SSL_AD_DECODE_ERROR;
1979 kn = EVP_PKEY_size(pkey);
1985 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1986 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1989 d = p = ssl_handshake_start(s);
1991 #ifndef OPENSSL_NO_PSK
1992 if (type & SSL_PSK) {
1993 /* copy PSK identity hint */
1994 if (s->cert->psk_identity_hint) {
1995 s2n(strlen(s->cert->psk_identity_hint), p);
1996 strncpy((char *)p, s->cert->psk_identity_hint,
1997 strlen(s->cert->psk_identity_hint));
1998 p += strlen(s->cert->psk_identity_hint);
2005 for (i = 0; i < 4 && r[i] != NULL; i++) {
2006 #ifndef OPENSSL_NO_SRP
2007 if ((i == 2) && (type & SSL_kSRP)) {
2017 #ifndef OPENSSL_NO_EC
2018 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2020 * XXX: For now, we only support named (not generic) curves. In
2021 * this situation, the serverKeyExchange message has: [1 byte
2022 * CurveType], [2 byte CurveName] [1 byte length of encoded
2023 * point], followed by the actual encoded point itself
2025 *p = NAMED_CURVE_TYPE;
2033 memcpy(p, encodedPoint, encodedlen);
2034 OPENSSL_free(encodedPoint);
2035 encodedPoint = NULL;
2043 * n is the length of the params, they start at &(d[4]) and p
2044 * points to the space at the end.
2046 #ifndef OPENSSL_NO_RSA
2047 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
2050 for (num = 2; num > 0; num--) {
2051 EVP_MD_CTX_set_flags(&md_ctx,
2052 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2053 EVP_DigestInit_ex(&md_ctx, (num == 2)
2054 ? s->ctx->md5 : s->ctx->sha1, NULL);
2055 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2057 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2059 EVP_DigestUpdate(&md_ctx, d, n);
2060 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
2064 if (RSA_sign(NID_md5_sha1, md_buf, j,
2065 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2066 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2074 /* send signature algorithm */
2075 if (SSL_USE_SIGALGS(s)) {
2076 if (!tls12_get_sigandhash(p, pkey, md)) {
2077 /* Should never happen */
2078 al = SSL_AD_INTERNAL_ERROR;
2079 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2080 ERR_R_INTERNAL_ERROR);
2086 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2088 EVP_SignInit_ex(&md_ctx, md, NULL);
2089 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2091 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2093 EVP_SignUpdate(&md_ctx, d, n);
2094 if (!EVP_SignFinal(&md_ctx, &(p[2]),
2095 (unsigned int *)&i, pkey)) {
2096 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2101 if (SSL_USE_SIGALGS(s))
2104 /* Is this error check actually needed? */
2105 al = SSL_AD_HANDSHAKE_FAILURE;
2106 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2107 SSL_R_UNKNOWN_PKEY_TYPE);
2112 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
2113 al = SSL_AD_HANDSHAKE_FAILURE;
2114 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2119 s->state = SSL3_ST_SW_KEY_EXCH_B;
2120 EVP_MD_CTX_cleanup(&md_ctx);
2121 return ssl_do_write(s);
2123 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2125 #ifndef OPENSSL_NO_EC
2126 OPENSSL_free(encodedPoint);
2127 BN_CTX_free(bn_ctx);
2129 EVP_MD_CTX_cleanup(&md_ctx);
2130 s->state = SSL_ST_ERR;
2134 int ssl3_send_certificate_request(SSL *s)
2136 unsigned char *p, *d;
2137 int i, j, nl, off, n;
2138 STACK_OF(X509_NAME) *sk = NULL;
2142 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2145 d = p = ssl_handshake_start(s);
2147 /* get the list of acceptable cert types */
2149 n = ssl3_get_req_cert_type(s, p);
2154 if (SSL_USE_SIGALGS(s)) {
2155 const unsigned char *psigs;
2156 unsigned char *etmp = p;
2157 nl = tls12_get_psigalgs(s, &psigs);
2158 /* Skip over length for now */
2160 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2161 /* Now fill in length */
2171 sk = SSL_get_client_CA_list(s);
2174 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2175 name = sk_X509_NAME_value(sk, i);
2176 j = i2d_X509_NAME(name, NULL);
2177 if (!BUF_MEM_grow_clean
2178 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2179 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2183 p = ssl_handshake_start(s) + n;
2185 i2d_X509_NAME(name, &p);
2190 /* else no CA names */
2191 p = ssl_handshake_start(s) + off;
2194 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2195 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2199 s->state = SSL3_ST_SW_CERT_REQ_B;
2202 /* SSL3_ST_SW_CERT_REQ_B */
2203 return ssl_do_write(s);
2205 s->state = SSL_ST_ERR;
2209 int ssl3_get_client_key_exchange(SSL *s)
2214 unsigned long alg_k;
2215 #ifndef OPENSSL_NO_RSA
2217 EVP_PKEY *pkey = NULL;
2219 #ifndef OPENSSL_NO_DH
2221 DH *dh_srvr, *dh_clnt = NULL;
2223 #ifndef OPENSSL_NO_EC
2224 EC_KEY *srvr_ecdh = NULL;
2225 EVP_PKEY *clnt_pub_pkey = NULL;
2226 EC_POINT *clnt_ecpoint = NULL;
2227 BN_CTX *bn_ctx = NULL;
2229 PACKET pkt, enc_premaster;
2230 unsigned char *data, *rsa_decrypt = NULL;
2232 n = s->method->ssl_get_message(s,
2233 SSL3_ST_SR_KEY_EXCH_A,
2234 SSL3_ST_SR_KEY_EXCH_B,
2235 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2239 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2240 al = SSL_AD_INTERNAL_ERROR;
2241 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2245 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2247 #ifndef OPENSSL_NO_PSK
2248 /* For PSK parse and retrieve identity, obtain PSK key */
2249 if (alg_k & SSL_PSK) {
2250 unsigned char psk[PSK_MAX_PSK_LEN];
2252 PACKET psk_identity;
2254 if (!PACKET_get_length_prefixed_2(&pkt, &psk_identity)) {
2255 al = SSL_AD_DECODE_ERROR;
2256 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2259 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2260 al = SSL_AD_DECODE_ERROR;
2261 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2262 SSL_R_DATA_LENGTH_TOO_LONG);
2265 if (s->psk_server_callback == NULL) {
2266 al = SSL_AD_INTERNAL_ERROR;
2267 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2268 SSL_R_PSK_NO_SERVER_CB);
2272 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2273 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2274 al = SSL_AD_INTERNAL_ERROR;
2278 psklen = s->psk_server_callback(s, s->session->psk_identity,
2281 if (psklen > PSK_MAX_PSK_LEN) {
2282 al = SSL_AD_INTERNAL_ERROR;
2283 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2285 } else if (psklen == 0) {
2287 * PSK related to the given identity not found
2289 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2290 SSL_R_PSK_IDENTITY_NOT_FOUND);
2291 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2295 OPENSSL_free(s->s3->tmp.psk);
2296 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2297 OPENSSL_cleanse(psk, psklen);
2299 if (s->s3->tmp.psk == NULL) {
2300 al = SSL_AD_INTERNAL_ERROR;
2301 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2305 s->s3->tmp.psklen = psklen;
2307 if (alg_k & SSL_kPSK) {
2308 /* Identity extracted earlier: should be nothing left */
2309 if (PACKET_remaining(&pkt) != 0) {
2310 al = SSL_AD_HANDSHAKE_FAILURE;
2311 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2314 /* PSK handled by ssl_generate_master_secret */
2315 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2316 al = SSL_AD_INTERNAL_ERROR;
2317 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2322 #ifndef OPENSSL_NO_RSA
2323 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2324 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2326 unsigned char decrypt_good, version_good;
2329 /* FIX THIS UP EAY EAY EAY EAY */
2330 if (s->s3->tmp.use_rsa_tmp) {
2331 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2332 rsa = s->cert->rsa_tmp;
2334 * Don't do a callback because rsa_tmp should be sent already
2337 al = SSL_AD_HANDSHAKE_FAILURE;
2338 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2339 SSL_R_MISSING_TMP_RSA_PKEY);
2344 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2345 if ((pkey == NULL) ||
2346 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2347 al = SSL_AD_HANDSHAKE_FAILURE;
2348 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2349 SSL_R_MISSING_RSA_CERTIFICATE);
2352 rsa = pkey->pkey.rsa;
2355 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2356 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2357 enc_premaster = pkt;
2360 if (!PACKET_get_length_prefixed_2(&pkt, &enc_premaster)
2361 || PACKET_remaining(&pkt) != 0) {
2362 /* Try SSLv3 behaviour for TLS. */
2363 if (s->options & SSL_OP_TLS_D5_BUG) {
2364 enc_premaster = orig;
2366 al = SSL_AD_DECODE_ERROR;
2367 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2374 * We want to be sure that the plaintext buffer size makes it safe to
2375 * iterate over the entire size of a premaster secret
2376 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2377 * their ciphertext cannot accommodate a premaster secret anyway.
2379 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2380 al = SSL_AD_INTERNAL_ERROR;
2381 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2382 RSA_R_KEY_SIZE_TOO_SMALL);
2386 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2387 if (rsa_decrypt == NULL) {
2388 al = SSL_AD_INTERNAL_ERROR;
2389 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2394 * We must not leak whether a decryption failure occurs because of
2395 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2396 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2397 * generates a random premaster secret for the case that the decrypt
2398 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2401 if (RAND_bytes(rand_premaster_secret,
2402 sizeof(rand_premaster_secret)) <= 0) {
2406 decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),
2407 PACKET_data(&enc_premaster),
2408 rsa_decrypt, rsa, RSA_PKCS1_PADDING);
2412 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2413 * be 0xff if so and zero otherwise.
2416 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2419 * If the version in the decrypted pre-master secret is correct then
2420 * version_good will be 0xff, otherwise it'll be zero. The
2421 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2422 * (http://eprint.iacr.org/2003/052/) exploits the version number
2423 * check as a "bad version oracle". Thus version checks are done in
2424 * constant time and are treated like any other decryption error.
2427 constant_time_eq_8(rsa_decrypt[0],
2428 (unsigned)(s->client_version >> 8));
2430 constant_time_eq_8(rsa_decrypt[1],
2431 (unsigned)(s->client_version & 0xff));
2434 * The premaster secret must contain the same version number as the
2435 * ClientHello to detect version rollback attacks (strangely, the
2436 * protocol does not offer such protection for DH ciphersuites).
2437 * However, buggy clients exist that send the negotiated protocol
2438 * version instead if the server does not support the requested
2439 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2442 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2443 unsigned char workaround_good;
2445 constant_time_eq_8(rsa_decrypt[0], (unsigned)(s->version >> 8));
2447 constant_time_eq_8(rsa_decrypt[1],
2448 (unsigned)(s->version & 0xff));
2449 version_good |= workaround_good;
2453 * Both decryption and version must be good for decrypt_good to
2454 * remain non-zero (0xff).
2456 decrypt_good &= version_good;
2459 * Now copy rand_premaster_secret over from p using
2460 * decrypt_good_mask. If decryption failed, then p does not
2461 * contain valid plaintext, however, a check above guarantees
2462 * it is still sufficiently large to read from.
2464 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2466 constant_time_select_8(decrypt_good, rsa_decrypt[j],
2467 rand_premaster_secret[j]);
2470 if (!ssl_generate_master_secret(s, rsa_decrypt,
2471 sizeof(rand_premaster_secret), 0)) {
2472 al = SSL_AD_INTERNAL_ERROR;
2473 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2476 OPENSSL_free(rsa_decrypt);
2480 #ifndef OPENSSL_NO_DH
2481 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2483 EVP_PKEY *skey = NULL;
2485 unsigned char shared[(OPENSSL_DH_MAX_MODULUS_BITS + 7) / 8];
2487 if (!PACKET_get_bookmark(&pkt, &bookm)) {
2488 al = SSL_AD_INTERNAL_ERROR;
2489 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2492 if (!PACKET_get_net_2(&pkt, &i)) {
2493 if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2494 al = SSL_AD_HANDSHAKE_FAILURE;
2495 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2496 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2501 if (PACKET_remaining(&pkt) != i) {
2502 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2503 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2504 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2507 if (!PACKET_goto_bookmark(&pkt, bookm)) {
2508 al = SSL_AD_INTERNAL_ERROR;
2509 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2510 ERR_R_INTERNAL_ERROR);
2513 i = PACKET_remaining(&pkt);
2516 if (alg_k & SSL_kDHr)
2517 idx = SSL_PKEY_DH_RSA;
2518 else if (alg_k & SSL_kDHd)
2519 idx = SSL_PKEY_DH_DSA;
2521 skey = s->cert->pkeys[idx].privatekey;
2522 if ((skey == NULL) ||
2523 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2524 al = SSL_AD_HANDSHAKE_FAILURE;
2525 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2526 SSL_R_MISSING_RSA_CERTIFICATE);
2529 dh_srvr = skey->pkey.dh;
2530 } else if (s->s3->tmp.dh == NULL) {
2531 al = SSL_AD_HANDSHAKE_FAILURE;
2532 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2533 SSL_R_MISSING_TMP_DH_KEY);
2536 dh_srvr = s->s3->tmp.dh;
2539 /* Get pubkey from cert */
2540 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2542 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2543 dh_clnt = EVP_PKEY_get1_DH(clkey);
2545 if (dh_clnt == NULL) {
2546 al = SSL_AD_HANDSHAKE_FAILURE;
2547 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2548 SSL_R_MISSING_TMP_DH_KEY);
2551 EVP_PKEY_free(clkey);
2552 pub = dh_clnt->pub_key;
2554 if (!PACKET_get_bytes(&pkt, &data, i)) {
2555 /* We already checked we have enough data */
2556 al = SSL_AD_INTERNAL_ERROR;
2557 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2558 ERR_R_INTERNAL_ERROR);
2561 pub = BN_bin2bn(data, i, NULL);
2564 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2568 i = DH_compute_key(shared, pub, dh_srvr);
2571 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2576 DH_free(s->s3->tmp.dh);
2577 s->s3->tmp.dh = NULL;
2583 if (!ssl_generate_master_secret(s, shared, i, 0)) {
2584 al = SSL_AD_INTERNAL_ERROR;
2585 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2593 #ifndef OPENSSL_NO_EC
2594 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2598 const EC_GROUP *group;
2599 const BIGNUM *priv_key;
2600 unsigned char *shared;
2602 /* initialize structures for server's ECDH key pair */
2603 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2604 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2608 /* Let's get server private key and group information */
2609 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2610 /* use the certificate */
2611 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2614 * use the ephermeral values we saved when generating the
2615 * ServerKeyExchange msg.
2617 tkey = s->s3->tmp.ecdh;
2620 group = EC_KEY_get0_group(tkey);
2621 priv_key = EC_KEY_get0_private_key(tkey);
2623 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2624 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2625 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2629 /* Let's get client's public key */
2630 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2631 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2636 /* Client Publickey was in Client Certificate */
2638 if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2639 al = SSL_AD_HANDSHAKE_FAILURE;
2640 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2641 SSL_R_MISSING_TMP_ECDH_KEY);
2644 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2645 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2647 * XXX: For now, we do not support client authentication
2648 * using ECDH certificates so this branch (n == 0L) of the
2649 * code is never executed. When that support is added, we
2650 * ought to ensure the key received in the certificate is
2651 * authorized for key agreement. ECDH_compute_key implicitly
2652 * checks that the two ECDH shares are for the same group.
2654 al = SSL_AD_HANDSHAKE_FAILURE;
2655 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2656 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2660 if (EC_POINT_copy(clnt_ecpoint,
2661 EC_KEY_get0_public_key(clnt_pub_pkey->
2663 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2666 ret = 2; /* Skip certificate verify processing */
2669 * Get client's public key from encoded point in the
2670 * ClientKeyExchange message.
2672 if ((bn_ctx = BN_CTX_new()) == NULL) {
2673 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2674 ERR_R_MALLOC_FAILURE);
2678 /* Get encoded point length */
2679 if (!PACKET_get_1(&pkt, &i)) {
2680 al = SSL_AD_DECODE_ERROR;
2681 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2682 SSL_R_LENGTH_MISMATCH);
2685 if (!PACKET_get_bytes(&pkt, &data, i)
2686 || PACKET_remaining(&pkt) != 0) {
2687 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2690 if (EC_POINT_oct2point(group, clnt_ecpoint, data, i, bn_ctx) == 0) {
2691 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2696 /* Compute the shared pre-master secret */
2697 field_size = EC_GROUP_get_degree(group);
2698 if (field_size <= 0) {
2699 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2702 shared = OPENSSL_malloc((field_size + 7) / 8);
2703 if (shared == NULL) {
2704 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2707 i = ECDH_compute_key(shared, (field_size + 7) / 8, clnt_ecpoint,
2710 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2711 OPENSSL_free(shared);
2715 EVP_PKEY_free(clnt_pub_pkey);
2716 EC_POINT_free(clnt_ecpoint);
2717 EC_KEY_free(srvr_ecdh);
2718 BN_CTX_free(bn_ctx);
2719 EC_KEY_free(s->s3->tmp.ecdh);
2720 s->s3->tmp.ecdh = NULL;
2722 if (!ssl_generate_master_secret(s, shared, i, 1)) {
2723 al = SSL_AD_INTERNAL_ERROR;
2724 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2730 #ifndef OPENSSL_NO_SRP
2731 if (alg_k & SSL_kSRP) {
2732 if (!PACKET_get_net_2(&pkt, &i)
2733 || !PACKET_get_bytes(&pkt, &data, i)) {
2734 al = SSL_AD_DECODE_ERROR;
2735 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BAD_SRP_A_LENGTH);
2738 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2739 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2742 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2743 || BN_is_zero(s->srp_ctx.A)) {
2744 al = SSL_AD_ILLEGAL_PARAMETER;
2745 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2746 SSL_R_BAD_SRP_PARAMETERS);
2749 OPENSSL_free(s->session->srp_username);
2750 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2751 if (s->session->srp_username == NULL) {
2752 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2756 if (!srp_generate_server_master_secret(s)) {
2757 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2761 #endif /* OPENSSL_NO_SRP */
2762 if (alg_k & SSL_kGOST) {
2764 EVP_PKEY_CTX *pkey_ctx;
2765 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2766 unsigned char premaster_secret[32], *start;
2767 size_t outlen = 32, inlen;
2768 unsigned long alg_a;
2772 /* Get our certificate private key */
2773 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2774 if (alg_a & SSL_aGOST01)
2775 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2777 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2778 EVP_PKEY_decrypt_init(pkey_ctx);
2780 * If client certificate is present and is of the same type, maybe
2781 * use it for key exchange. Don't mind errors from
2782 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2783 * client certificate for authorization only.
2785 client_pub_pkey = X509_get_pubkey(s->session->peer);
2786 if (client_pub_pkey) {
2787 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2790 /* Decrypt session key */
2791 if (!PACKET_get_bytes(&pkt, &data, n)) {
2792 al = SSL_AD_INTERNAL_ERROR;
2793 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2797 ((const unsigned char **)&data, &Tlen, &Ttag, &Tclass,
2798 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2799 || Tclass != V_ASN1_UNIVERSAL) {
2800 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2801 SSL_R_DECRYPTION_FAILED);
2806 if (EVP_PKEY_decrypt
2807 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2808 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2809 SSL_R_DECRYPTION_FAILED);
2812 /* Generate master secret */
2813 if (!ssl_generate_master_secret(s, premaster_secret,
2814 sizeof(premaster_secret), 0)) {
2815 al = SSL_AD_INTERNAL_ERROR;
2816 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2819 /* Check if pubkey from client certificate was used */
2820 if (EVP_PKEY_CTX_ctrl
2821 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2826 EVP_PKEY_free(client_pub_pkey);
2827 EVP_PKEY_CTX_free(pkey_ctx);
2832 al = SSL_AD_HANDSHAKE_FAILURE;
2833 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2839 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2840 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2843 #ifndef OPENSSL_NO_EC
2844 EVP_PKEY_free(clnt_pub_pkey);
2845 EC_POINT_free(clnt_ecpoint);
2846 EC_KEY_free(srvr_ecdh);
2847 BN_CTX_free(bn_ctx);
2848 OPENSSL_free(rsa_decrypt);
2850 #ifndef OPENSSL_NO_PSK
2851 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2852 s->s3->tmp.psk = NULL;
2854 s->state = SSL_ST_ERR;
2858 int ssl3_get_cert_verify(SSL *s)
2860 EVP_PKEY *pkey = NULL;
2861 unsigned char *sig, *data;
2862 int al, ok, ret = 0;
2867 const EVP_MD *md = NULL;
2870 EVP_MD_CTX_init(&mctx);
2873 * We should only process a CertificateVerify message if we have received
2874 * a Certificate from the client. If so then |s->session->peer| will be non
2875 * NULL. In some instances a CertificateVerify message is not required even
2876 * if the peer has sent a Certificate (e.g. such as in the case of static
2877 * DH). In that case the ClientKeyExchange processing will skip the
2878 * CertificateVerify state so we should not arrive here.
2880 if (s->session->peer == NULL) {
2885 n = s->method->ssl_get_message(s,
2886 SSL3_ST_SR_CERT_VRFY_A,
2887 SSL3_ST_SR_CERT_VRFY_B,
2888 SSL3_MT_CERTIFICATE_VERIFY,
2889 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2894 peer = s->session->peer;
2895 pkey = X509_get_pubkey(peer);
2896 type = X509_certificate_type(peer, pkey);
2898 if (!(type & EVP_PKT_SIGN)) {
2899 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2900 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2901 al = SSL_AD_ILLEGAL_PARAMETER;
2905 /* we now have a signature that we need to verify */
2906 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2907 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2908 al = SSL_AD_INTERNAL_ERROR;
2911 /* Check for broken implementations of GOST ciphersuites */
2913 * If key is GOST and n is exactly 64, it is bare signature without
2916 if (n == 64 && pkey->type == NID_id_GostR3410_2001) {
2919 if (SSL_USE_SIGALGS(s)) {
2922 if (!PACKET_get_bytes(&pkt, &sig, 2)) {
2923 al = SSL_AD_DECODE_ERROR;
2926 rv = tls12_check_peer_sigalg(&md, s, sig, pkey);
2928 al = SSL_AD_INTERNAL_ERROR;
2930 } else if (rv == 0) {
2931 al = SSL_AD_DECODE_ERROR;
2935 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2938 if (!PACKET_get_net_2(&pkt, &len)) {
2939 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2940 al = SSL_AD_DECODE_ERROR;
2944 j = EVP_PKEY_size(pkey);
2945 if (((int)len > j) || ((int)PACKET_remaining(&pkt) > j) || (n <= 0)) {
2946 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2947 al = SSL_AD_DECODE_ERROR;
2950 if (!PACKET_get_bytes(&pkt, &data, len)) {
2951 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2952 al = SSL_AD_DECODE_ERROR;
2956 if (SSL_USE_SIGALGS(s)) {
2959 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2960 if (hdatalen <= 0) {
2961 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2962 al = SSL_AD_INTERNAL_ERROR;
2966 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
2969 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
2970 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
2971 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
2972 al = SSL_AD_INTERNAL_ERROR;
2976 if (EVP_VerifyFinal(&mctx, data, len, pkey) <= 0) {
2977 al = SSL_AD_DECRYPT_ERROR;
2978 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
2982 #ifndef OPENSSL_NO_RSA
2983 if (pkey->type == EVP_PKEY_RSA) {
2984 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2985 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, data, len,
2988 al = SSL_AD_DECRYPT_ERROR;
2989 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
2993 al = SSL_AD_DECRYPT_ERROR;
2994 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
2999 #ifndef OPENSSL_NO_DSA
3000 if (pkey->type == EVP_PKEY_DSA) {
3001 j = DSA_verify(pkey->save_type,
3002 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3003 SHA_DIGEST_LENGTH, data, len, pkey->pkey.dsa);
3006 al = SSL_AD_DECRYPT_ERROR;
3007 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3012 #ifndef OPENSSL_NO_EC
3013 if (pkey->type == EVP_PKEY_EC) {
3014 j = ECDSA_verify(pkey->save_type,
3015 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3016 SHA_DIGEST_LENGTH, data, len, pkey->pkey.ec);
3019 al = SSL_AD_DECRYPT_ERROR;
3020 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3025 if (pkey->type == NID_id_GostR3410_2001) {
3026 unsigned char signature[64];
3028 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3029 EVP_PKEY_verify_init(pctx);
3031 fprintf(stderr, "GOST signature length is %d", len);
3033 for (idx = 0; idx < 64; idx++) {
3034 signature[63 - idx] = data[idx];
3036 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3038 EVP_PKEY_CTX_free(pctx);
3040 al = SSL_AD_DECRYPT_ERROR;
3041 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3045 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3046 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3053 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3054 s->state = SSL_ST_ERR;
3057 BIO_free(s->s3->handshake_buffer);
3058 s->s3->handshake_buffer = NULL;
3059 EVP_MD_CTX_cleanup(&mctx);
3060 EVP_PKEY_free(pkey);
3064 int ssl3_get_client_certificate(SSL *s)
3066 int i, ok, al, ret = -1;
3068 unsigned long l, llen, n;
3069 const unsigned char *certstart;
3070 unsigned char *certbytes;
3071 STACK_OF(X509) *sk = NULL;
3074 n = s->method->ssl_get_message(s,
3077 -1, s->max_cert_list, &ok);
3082 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3083 if ((s->verify_mode & SSL_VERIFY_PEER) &&
3084 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3085 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3086 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3087 al = SSL_AD_HANDSHAKE_FAILURE;
3091 * If tls asked for a client cert, the client must return a 0 list
3093 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3094 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3095 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3096 al = SSL_AD_UNEXPECTED_MESSAGE;
3099 s->s3->tmp.reuse_message = 1;
3103 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3104 al = SSL_AD_UNEXPECTED_MESSAGE;
3105 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3109 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
3110 al = SSL_AD_INTERNAL_ERROR;
3111 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3115 if ((sk = sk_X509_new_null()) == NULL) {
3116 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3120 if (!PACKET_get_net_3(&pkt, &llen)
3121 || !PACKET_get_sub_packet(&pkt, &spkt, llen)
3122 || PACKET_remaining(&pkt) != 0) {
3123 al = SSL_AD_DECODE_ERROR;
3124 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3128 while (PACKET_remaining(&spkt) > 0) {
3129 if (!PACKET_get_net_3(&spkt, &l)
3130 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3131 al = SSL_AD_DECODE_ERROR;
3132 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3133 SSL_R_CERT_LENGTH_MISMATCH);
3137 certstart = certbytes;
3138 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3140 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3143 if (certbytes != (certstart + l)) {
3144 al = SSL_AD_DECODE_ERROR;
3145 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3146 SSL_R_CERT_LENGTH_MISMATCH);
3149 if (!sk_X509_push(sk, x)) {
3150 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3156 if (sk_X509_num(sk) <= 0) {
3157 /* TLS does not mind 0 certs returned */
3158 if (s->version == SSL3_VERSION) {
3159 al = SSL_AD_HANDSHAKE_FAILURE;
3160 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3161 SSL_R_NO_CERTIFICATES_RETURNED);
3164 /* Fail for TLS only if we required a certificate */
3165 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3166 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3167 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3168 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3169 al = SSL_AD_HANDSHAKE_FAILURE;
3172 /* No client certificate so digest cached records */
3173 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3174 al = SSL_AD_INTERNAL_ERROR;
3179 i = ssl_verify_cert_chain(s, sk);
3181 al = ssl_verify_alarm_type(s->verify_result);
3182 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3183 SSL_R_CERTIFICATE_VERIFY_FAILED);
3187 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, i);
3188 al = SSL_AD_HANDSHAKE_FAILURE;
3191 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3193 al = SSL3_AD_HANDSHAKE_FAILURE;
3194 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3195 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3198 EVP_PKEY_free(pkey);
3201 X509_free(s->session->peer);
3202 s->session->peer = sk_X509_shift(sk);
3203 s->session->verify_result = s->verify_result;
3205 sk_X509_pop_free(s->session->peer_chain, X509_free);
3206 s->session->peer_chain = sk;
3208 * Inconsistency alert: cert_chain does *not* include the peer's own
3209 * certificate, while we do include it in s3_clnt.c
3216 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3218 s->state = SSL_ST_ERR;
3220 sk_X509_pop_free(sk, X509_free);
3224 int ssl3_send_server_certificate(SSL *s)
3228 if (s->state == SSL3_ST_SW_CERT_A) {
3229 cpk = ssl_get_server_send_pkey(s);
3231 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3232 s->state = SSL_ST_ERR;
3236 if (!ssl3_output_cert_chain(s, cpk)) {
3237 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3238 s->state = SSL_ST_ERR;
3241 s->state = SSL3_ST_SW_CERT_B;
3244 /* SSL3_ST_SW_CERT_B */
3245 return ssl_do_write(s);
3248 /* send a new session ticket (not necessarily for a new session) */
3249 int ssl3_send_newsession_ticket(SSL *s)
3251 unsigned char *senc = NULL;
3255 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3256 unsigned char *p, *macstart;
3257 const unsigned char *const_p;
3258 int len, slen_full, slen;
3261 SSL_CTX *tctx = s->initial_ctx;
3262 unsigned char iv[EVP_MAX_IV_LENGTH];
3263 unsigned char key_name[16];
3265 /* get session encoding length */
3266 slen_full = i2d_SSL_SESSION(s->session, NULL);
3268 * Some length values are 16 bits, so forget it if session is too
3271 if (slen_full == 0 || slen_full > 0xFF00) {
3272 s->state = SSL_ST_ERR;
3275 senc = OPENSSL_malloc(slen_full);
3277 s->state = SSL_ST_ERR;
3281 EVP_CIPHER_CTX_init(&ctx);
3282 HMAC_CTX_init(&hctx);
3285 if (!i2d_SSL_SESSION(s->session, &p))
3289 * create a fresh copy (not shared with other threads) to clean up
3292 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3295 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3297 slen = i2d_SSL_SESSION(sess, NULL);
3298 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3299 SSL_SESSION_free(sess);
3303 if (!i2d_SSL_SESSION(sess, &p)) {
3304 SSL_SESSION_free(sess);
3307 SSL_SESSION_free(sess);
3310 * Grow buffer if need be: the length calculation is as
3311 * follows handshake_header_length +
3312 * 4 (ticket lifetime hint) + 2 (ticket length) +
3313 * 16 (key name) + max_iv_len (iv length) +
3314 * session_length + max_enc_block_size (max encrypted session
3315 * length) + max_md_size (HMAC).
3317 if (!BUF_MEM_grow(s->init_buf,
3318 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3319 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3322 p = ssl_handshake_start(s);
3324 * Initialize HMAC and cipher contexts. If callback present it does
3325 * all the work otherwise use generated values from parent ctx.
3327 if (tctx->tlsext_ticket_key_cb) {
3328 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3332 if (RAND_bytes(iv, 16) <= 0)
3334 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3335 tctx->tlsext_tick_aes_key, iv))
3337 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3338 EVP_sha256(), NULL))
3340 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3344 * Ticket lifetime hint (advisory only): We leave this unspecified
3345 * for resumed session (for simplicity), and guess that tickets for
3346 * new sessions will live as long as their sessions.
3348 l2n(s->hit ? 0 : s->session->timeout, p);
3350 /* Skip ticket length for now */
3352 /* Output key name */
3354 memcpy(p, key_name, 16);
3357 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3358 p += EVP_CIPHER_CTX_iv_length(&ctx);
3359 /* Encrypt session data */
3360 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3363 if (!EVP_EncryptFinal(&ctx, p, &len))
3367 if (!HMAC_Update(&hctx, macstart, p - macstart))
3369 if (!HMAC_Final(&hctx, p, &hlen))
3372 EVP_CIPHER_CTX_cleanup(&ctx);
3373 HMAC_CTX_cleanup(&hctx);
3376 /* Now write out lengths: p points to end of data written */
3378 len = p - ssl_handshake_start(s);
3379 /* Skip ticket lifetime hint */
3380 p = ssl_handshake_start(s) + 4;
3382 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3384 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3388 /* SSL3_ST_SW_SESSION_TICKET_B */
3389 return ssl_do_write(s);
3392 EVP_CIPHER_CTX_cleanup(&ctx);
3393 HMAC_CTX_cleanup(&hctx);
3394 s->state = SSL_ST_ERR;
3398 int ssl3_send_cert_status(SSL *s)
3400 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3403 * Grow buffer if need be: the length calculation is as
3404 * follows 1 (message type) + 3 (message length) +
3405 * 1 (ocsp response type) + 3 (ocsp response length)
3408 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3409 s->state = SSL_ST_ERR;
3413 p = (unsigned char *)s->init_buf->data;
3416 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3417 /* message length */
3418 l2n3(s->tlsext_ocsp_resplen + 4, p);
3420 *(p++) = s->tlsext_status_type;
3421 /* length of OCSP response */
3422 l2n3(s->tlsext_ocsp_resplen, p);
3423 /* actual response */
3424 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3425 /* number of bytes to write */
3426 s->init_num = 8 + s->tlsext_ocsp_resplen;
3427 s->state = SSL3_ST_SW_CERT_STATUS_B;
3431 /* SSL3_ST_SW_CERT_STATUS_B */
3432 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3435 #ifndef OPENSSL_NO_NEXTPROTONEG
3437 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3438 * It sets the next_proto member in s if found
3440 int ssl3_get_next_proto(SSL *s)
3444 PACKET pkt, next_proto, padding;
3445 size_t next_proto_len;
3448 * Clients cannot send a NextProtocol message if we didn't see the
3449 * extension in their ClientHello
3451 if (!s->s3->next_proto_neg_seen) {
3452 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3453 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3454 s->state = SSL_ST_ERR;
3458 /* See the payload format below */
3459 n = s->method->ssl_get_message(s,
3460 SSL3_ST_SR_NEXT_PROTO_A,
3461 SSL3_ST_SR_NEXT_PROTO_B,
3462 SSL3_MT_NEXT_PROTO, 514, &ok);
3468 * s->state doesn't reflect whether ChangeCipherSpec has been received in
3469 * this handshake, but s->s3->change_cipher_spec does (will be reset by
3470 * ssl3_get_finished).
3472 if (!s->s3->change_cipher_spec) {
3473 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3474 s->state = SSL_ST_ERR;
3479 goto err; /* The body must be > 1 bytes long */
3482 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
3483 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_INTERNAL_ERROR);
3488 * The payload looks like:
3490 * uint8 proto[proto_len];
3491 * uint8 padding_len;
3492 * uint8 padding[padding_len];
3494 if (!PACKET_get_length_prefixed_1(&pkt, &next_proto)
3495 || !PACKET_get_length_prefixed_1(&pkt, &padding)
3496 || PACKET_remaining(&pkt) > 0) {
3497 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3501 if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated,
3503 s->next_proto_negotiated_len = 0;
3507 s->next_proto_negotiated_len = (unsigned char)next_proto_len;
3511 s->state = SSL_ST_ERR;
3516 #define SSLV2_CIPHER_LEN 3
3518 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
3520 STACK_OF(SSL_CIPHER) **skp,
3523 const SSL_CIPHER *c;
3524 STACK_OF(SSL_CIPHER) *sk;
3528 s->s3->send_connection_binding = 0;
3531 n = SSLV2_CIPHER_LEN;
3533 n = ssl_put_cipher_by_char(s, NULL, NULL);
3535 if (n == 0 || (num % n) != 0) {
3536 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3537 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3540 if ((skp == NULL) || (*skp == NULL)) {
3541 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3543 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3548 sk_SSL_CIPHER_zero(sk);
3551 OPENSSL_free(s->s3->tmp.ciphers_raw);
3552 s->s3->tmp.ciphers_raw = BUF_memdup(p, num);
3553 if (s->s3->tmp.ciphers_raw == NULL) {
3554 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3557 s->s3->tmp.ciphers_rawlen = (size_t)num;
3559 for (i = 0; i < num; i += n) {
3560 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3561 if (s->s3 && (n != 3 || !p[0]) &&
3562 (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3563 (p[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3564 /* SCSV fatal if renegotiating */
3565 if (s->renegotiate) {
3566 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3567 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3568 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3571 s->s3->send_connection_binding = 1;
3573 #ifdef OPENSSL_RI_DEBUG
3574 fprintf(stderr, "SCSV received by server\n");
3579 /* Check for TLS_FALLBACK_SCSV */
3580 if ((n != 3 || !p[0]) &&
3581 (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3582 (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3584 * The SCSV indicates that the client previously tried a higher
3585 * version. Fail if the current version is an unexpected
3588 if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
3589 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3590 SSL_R_INAPPROPRIATE_FALLBACK);
3592 ssl3_send_alert(s, SSL3_AL_FATAL,
3593 SSL_AD_INAPPROPRIATE_FALLBACK);
3602 * We only support SSLv2 format ciphers in SSLv3+ using a
3603 * SSLv2 backward compatible ClientHello. In this case the first
3604 * byte is always 0 for SSLv3 compatible ciphers. Anything else
3605 * is an SSLv2 cipher and we ignore it
3608 c = ssl_get_cipher_by_char(s, &p[1]);
3612 c = ssl_get_cipher_by_char(s, p);
3616 if (!sk_SSL_CIPHER_push(sk, c)) {
3617 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3627 if ((skp == NULL) || (*skp == NULL))
3628 sk_SSL_CIPHER_free(sk);