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 #ifndef OPENSSL_NO_SSL3_METHOD
168 static const SSL_METHOD *ssl3_get_server_method(int ver);
170 static const SSL_METHOD *ssl3_get_server_method(int ver)
172 if (ver == SSL3_VERSION)
173 return (SSLv3_server_method());
178 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
180 ssl_undefined_function, ssl3_get_server_method)
182 #ifndef OPENSSL_NO_SRP
183 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
185 int ret = SSL_ERROR_NONE;
187 *al = SSL_AD_UNRECOGNIZED_NAME;
189 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
190 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
191 if (s->srp_ctx.login == NULL) {
193 * RFC 5054 says SHOULD reject, we do so if There is no srp
197 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
199 ret = SSL_srp_server_param_with_username(s, al);
206 int ssl3_accept(SSL *s)
209 unsigned long alg_k, Time = (unsigned long)time(NULL);
210 void (*cb) (const SSL *ssl, int type, int val) = NULL;
212 int new_state, state, skip = 0;
214 RAND_add(&Time, sizeof(Time), 0);
218 if (s->info_callback != NULL)
219 cb = s->info_callback;
220 else if (s->ctx->info_callback != NULL)
221 cb = s->ctx->info_callback;
223 /* init things to blank */
225 if (!SSL_in_init(s) || SSL_in_before(s)) {
230 #ifndef OPENSSL_NO_HEARTBEATS
232 * If we're awaiting a HeartbeatResponse, pretend we already got and
233 * don't await it anymore, because Heartbeats don't make sense during
236 if (s->tlsext_hb_pending) {
237 s->tlsext_hb_pending = 0;
246 case SSL_ST_RENEGOTIATE:
248 /* s->state=SSL_ST_ACCEPT; */
252 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
253 case SSL_ST_OK | SSL_ST_ACCEPT:
257 cb(s, SSL_CB_HANDSHAKE_START, 1);
259 if ((s->version >> 8 != 3) && s->version != TLS_ANY_VERSION) {
260 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
261 s->state = SSL_ST_ERR;
265 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
266 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_VERSION_TOO_LOW);
270 s->type = SSL_ST_ACCEPT;
272 if (s->init_buf == NULL) {
273 if ((buf = BUF_MEM_new()) == NULL) {
275 s->state = SSL_ST_ERR;
278 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
281 s->state = SSL_ST_ERR;
287 if (!ssl3_setup_buffers(s)) {
289 s->state = SSL_ST_ERR;
294 s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
295 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
297 * Should have been reset by ssl3_get_finished, too.
299 s->s3->change_cipher_spec = 0;
301 if (s->state != SSL_ST_RENEGOTIATE) {
303 * Ok, we now need to push on a buffering BIO so that the
304 * output is sent in a way that TCP likes :-)
306 if (!ssl_init_wbio_buffer(s, 1)) {
308 s->state = SSL_ST_ERR;
312 ssl3_init_finished_mac(s);
313 s->state = SSL3_ST_SR_CLNT_HELLO_A;
314 s->ctx->stats.sess_accept++;
315 } else if (!s->s3->send_connection_binding &&
317 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
319 * Server attempting to renegotiate with client that doesn't
320 * support secure renegotiation.
322 SSLerr(SSL_F_SSL3_ACCEPT,
323 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
324 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
326 s->state = SSL_ST_ERR;
330 * s->state == SSL_ST_RENEGOTIATE, we will just send a
333 s->ctx->stats.sess_accept_renegotiate++;
334 s->state = SSL3_ST_SW_HELLO_REQ_A;
338 case SSL3_ST_SW_HELLO_REQ_A:
339 case SSL3_ST_SW_HELLO_REQ_B:
342 ret = ssl3_send_hello_request(s);
345 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
346 s->state = SSL3_ST_SW_FLUSH;
349 ssl3_init_finished_mac(s);
352 case SSL3_ST_SW_HELLO_REQ_C:
353 s->state = SSL_ST_OK;
356 case SSL3_ST_SR_CLNT_HELLO_A:
357 case SSL3_ST_SR_CLNT_HELLO_B:
358 case SSL3_ST_SR_CLNT_HELLO_C:
360 ret = ssl3_get_client_hello(s);
363 #ifndef OPENSSL_NO_SRP
364 s->state = SSL3_ST_SR_CLNT_HELLO_D;
365 case SSL3_ST_SR_CLNT_HELLO_D:
368 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
370 * callback indicates firther work to be done
372 s->rwstate = SSL_X509_LOOKUP;
375 if (ret != SSL_ERROR_NONE) {
376 ssl3_send_alert(s, SSL3_AL_FATAL, al);
378 * This is not really an error but the only means to for
379 * a client to detect whether srp is supported.
381 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
382 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
383 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
385 s->state = SSL_ST_ERR;
392 s->state = SSL3_ST_SW_SRVR_HELLO_A;
396 case SSL3_ST_SW_SRVR_HELLO_A:
397 case SSL3_ST_SW_SRVR_HELLO_B:
398 ret = ssl3_send_server_hello(s);
401 #ifndef OPENSSL_NO_TLSEXT
403 if (s->tlsext_ticket_expected)
404 s->state = SSL3_ST_SW_SESSION_TICKET_A;
406 s->state = SSL3_ST_SW_CHANGE_A;
410 s->state = SSL3_ST_SW_CHANGE_A;
413 s->state = SSL3_ST_SW_CERT_A;
417 case SSL3_ST_SW_CERT_A:
418 case SSL3_ST_SW_CERT_B:
419 /* Check if it is anon DH or anon ECDH, */
420 /* normal PSK or SRP */
423 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
424 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
425 ret = ssl3_send_server_certificate(s);
428 #ifndef OPENSSL_NO_TLSEXT
429 if (s->tlsext_status_expected)
430 s->state = SSL3_ST_SW_CERT_STATUS_A;
432 s->state = SSL3_ST_SW_KEY_EXCH_A;
435 s->state = SSL3_ST_SW_KEY_EXCH_A;
441 s->state = SSL3_ST_SW_KEY_EXCH_A;
446 case SSL3_ST_SW_KEY_EXCH_A:
447 case SSL3_ST_SW_KEY_EXCH_B:
448 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
451 * clear this, it may get reset by
452 * send_server_key_exchange
454 s->s3->tmp.use_rsa_tmp = 0;
457 * only send if a DH key exchange, fortezza or RSA but we have a
458 * sign only certificate PSK: may send PSK identity hints For
459 * ECC ciphersuites, we send a serverKeyExchange message only if
460 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
461 * the server certificate contains the server's public key for
466 * PSK: send ServerKeyExchange if PSK identity hint if
469 #ifndef OPENSSL_NO_PSK
470 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
472 #ifndef OPENSSL_NO_SRP
473 /* SRP: send ServerKeyExchange */
474 || (alg_k & SSL_kSRP)
476 || (alg_k & SSL_kDHE)
477 || (alg_k & SSL_kECDHE)
478 || ((alg_k & SSL_kRSA)
479 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
480 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
481 && EVP_PKEY_size(s->cert->pkeys
482 [SSL_PKEY_RSA_ENC].privatekey) *
483 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
488 ret = ssl3_send_server_key_exchange(s);
494 s->state = SSL3_ST_SW_CERT_REQ_A;
498 case SSL3_ST_SW_CERT_REQ_A:
499 case SSL3_ST_SW_CERT_REQ_B:
500 if ( /* don't request cert unless asked for it: */
501 !(s->verify_mode & SSL_VERIFY_PEER) ||
503 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
504 * during re-negotiation:
506 ((s->session->peer != NULL) &&
507 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
509 * never request cert in anonymous ciphersuites (see
510 * section "Certificate request" in SSL 3 drafts and in
513 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
515 * ... except when the application insists on
516 * verification (against the specs, but s3_clnt.c accepts
519 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
520 /* don't request certificate for SRP auth */
521 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
523 * With normal PSK Certificates and Certificate Requests
526 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
527 /* no cert request */
529 s->s3->tmp.cert_request = 0;
530 s->state = SSL3_ST_SW_SRVR_DONE_A;
531 if (s->s3->handshake_buffer) {
532 if (!ssl3_digest_cached_records(s)) {
533 s->state = SSL_ST_ERR;
538 s->s3->tmp.cert_request = 1;
539 ret = ssl3_send_certificate_request(s);
542 s->state = SSL3_ST_SW_SRVR_DONE_A;
547 case SSL3_ST_SW_SRVR_DONE_A:
548 case SSL3_ST_SW_SRVR_DONE_B:
549 ret = ssl3_send_server_done(s);
552 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
553 s->state = SSL3_ST_SW_FLUSH;
557 case SSL3_ST_SW_FLUSH:
560 * This code originally checked to see if any data was pending
561 * using BIO_CTRL_INFO and then flushed. This caused problems as
562 * documented in PR#1939. The proposed fix doesn't completely
563 * resolve this issue as buggy implementations of
564 * BIO_CTRL_PENDING still exist. So instead we just flush
568 s->rwstate = SSL_WRITING;
569 if (BIO_flush(s->wbio) <= 0) {
573 s->rwstate = SSL_NOTHING;
575 s->state = s->s3->tmp.next_state;
578 case SSL3_ST_SR_CERT_A:
579 case SSL3_ST_SR_CERT_B:
580 if (s->s3->tmp.cert_request) {
581 ret = ssl3_get_client_certificate(s);
586 s->state = SSL3_ST_SR_KEY_EXCH_A;
589 case SSL3_ST_SR_KEY_EXCH_A:
590 case SSL3_ST_SR_KEY_EXCH_B:
591 ret = ssl3_get_client_key_exchange(s);
596 * For the ECDH ciphersuites when the client sends its ECDH
597 * pub key in a certificate, the CertificateVerify message is
598 * not sent. Also for GOST ciphersuites when the client uses
599 * its key from the certificate for key exchange.
601 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
602 s->state = SSL3_ST_SR_FINISHED_A;
604 if (s->s3->next_proto_neg_seen)
605 s->state = SSL3_ST_SR_NEXT_PROTO_A;
607 s->state = SSL3_ST_SR_FINISHED_A;
610 } else if (SSL_USE_SIGALGS(s)) {
611 s->state = SSL3_ST_SR_CERT_VRFY_A;
613 if (!s->session->peer)
615 if (!s->s3->handshake_buffer) {
616 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
617 s->state = SSL_ST_ERR;
621 * For sigalgs freeze the handshake buffer. If we support
622 * extms we've done this already.
624 if (!(s->s3->flags & SSL_SESS_FLAG_EXTMS)) {
625 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
626 if (!ssl3_digest_cached_records(s)) {
627 s->state = SSL_ST_ERR;
635 s->state = SSL3_ST_SR_CERT_VRFY_A;
639 * We need to get hashes here so if there is a client cert,
640 * it can be verified FIXME - digest processing for
641 * CertificateVerify should be generalized. But it is next
644 if (s->s3->handshake_buffer) {
645 if (!ssl3_digest_cached_records(s)) {
646 s->state = SSL_ST_ERR;
650 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
651 if (s->s3->handshake_dgst[dgst_num]) {
654 s->method->ssl3_enc->cert_verify_mac(s,
663 EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
665 s->state = SSL_ST_ERR;
674 case SSL3_ST_SR_CERT_VRFY_A:
675 case SSL3_ST_SR_CERT_VRFY_B:
676 ret = ssl3_get_cert_verify(s);
680 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
681 s->state = SSL3_ST_SR_FINISHED_A;
683 if (s->s3->next_proto_neg_seen)
684 s->state = SSL3_ST_SR_NEXT_PROTO_A;
686 s->state = SSL3_ST_SR_FINISHED_A;
691 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
692 case SSL3_ST_SR_NEXT_PROTO_A:
693 case SSL3_ST_SR_NEXT_PROTO_B:
695 * Enable CCS for NPN. Receiving a CCS clears the flag, so make
696 * sure not to re-enable it to ban duplicates. This *should* be the
697 * first time we have received one - but we check anyway to be
699 * s->s3->change_cipher_spec is set when a CCS is
700 * processed in s3_pkt.c, and remains set until
701 * the client's Finished message is read.
703 if (!s->s3->change_cipher_spec)
704 s->s3->flags |= SSL3_FLAGS_CCS_OK;
706 ret = ssl3_get_next_proto(s);
710 s->state = SSL3_ST_SR_FINISHED_A;
714 case SSL3_ST_SR_FINISHED_A:
715 case SSL3_ST_SR_FINISHED_B:
717 * Enable CCS for handshakes without NPN. In NPN the CCS flag has
718 * already been set. Receiving a CCS clears the flag, so make
719 * sure not to re-enable it to ban duplicates.
720 * s->s3->change_cipher_spec is set when a CCS is
721 * processed in s3_pkt.c, and remains set until
722 * the client's Finished message is read.
724 if (!s->s3->change_cipher_spec)
725 s->s3->flags |= SSL3_FLAGS_CCS_OK;
726 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
727 SSL3_ST_SR_FINISHED_B);
731 s->state = SSL_ST_OK;
732 #ifndef OPENSSL_NO_TLSEXT
733 else if (s->tlsext_ticket_expected)
734 s->state = SSL3_ST_SW_SESSION_TICKET_A;
737 s->state = SSL3_ST_SW_CHANGE_A;
741 #ifndef OPENSSL_NO_TLSEXT
742 case SSL3_ST_SW_SESSION_TICKET_A:
743 case SSL3_ST_SW_SESSION_TICKET_B:
744 ret = ssl3_send_newsession_ticket(s);
747 s->state = SSL3_ST_SW_CHANGE_A;
751 case SSL3_ST_SW_CERT_STATUS_A:
752 case SSL3_ST_SW_CERT_STATUS_B:
753 ret = ssl3_send_cert_status(s);
756 s->state = SSL3_ST_SW_KEY_EXCH_A;
762 case SSL3_ST_SW_CHANGE_A:
763 case SSL3_ST_SW_CHANGE_B:
765 s->session->cipher = s->s3->tmp.new_cipher;
766 if (!s->method->ssl3_enc->setup_key_block(s)) {
768 s->state = SSL_ST_ERR;
772 ret = ssl3_send_change_cipher_spec(s,
774 SSL3_ST_SW_CHANGE_B);
778 s->state = SSL3_ST_SW_FINISHED_A;
781 if (!s->method->ssl3_enc->change_cipher_state(s,
782 SSL3_CHANGE_CIPHER_SERVER_WRITE))
785 s->state = SSL_ST_ERR;
791 case SSL3_ST_SW_FINISHED_A:
792 case SSL3_ST_SW_FINISHED_B:
793 ret = ssl3_send_finished(s,
794 SSL3_ST_SW_FINISHED_A,
795 SSL3_ST_SW_FINISHED_B,
797 ssl3_enc->server_finished_label,
799 ssl3_enc->server_finished_label_len);
802 s->state = SSL3_ST_SW_FLUSH;
804 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
805 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
807 if (s->s3->next_proto_neg_seen) {
808 s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A;
810 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
813 s->s3->tmp.next_state = SSL_ST_OK;
818 /* clean a few things up */
819 ssl3_cleanup_key_block(s);
821 BUF_MEM_free(s->init_buf);
824 /* remove buffering on output */
825 ssl_free_wbio_buffer(s);
829 if (s->renegotiate == 2) { /* skipped if we just sent a
834 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
836 s->ctx->stats.sess_accept_good++;
838 s->handshake_func = ssl3_accept;
841 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
850 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
856 if (!s->s3->tmp.reuse_message && !skip) {
858 if ((ret = BIO_flush(s->wbio)) <= 0)
862 if ((cb != NULL) && (s->state != state)) {
863 new_state = s->state;
865 cb(s, SSL_CB_ACCEPT_LOOP, 1);
866 s->state = new_state;
872 /* BIO_flush(s->wbio); */
876 cb(s, SSL_CB_ACCEPT_EXIT, ret);
880 int ssl3_send_hello_request(SSL *s)
883 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
884 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
885 SSLerr(SSL_F_SSL3_SEND_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
888 s->state = SSL3_ST_SW_HELLO_REQ_B;
891 /* SSL3_ST_SW_HELLO_REQ_B */
892 return ssl_do_write(s);
895 int ssl3_get_client_hello(SSL *s)
897 int i, j, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
898 unsigned int cookie_len;
901 unsigned char *p, *d;
903 #ifndef OPENSSL_NO_COMP
905 SSL_COMP *comp = NULL;
907 STACK_OF(SSL_CIPHER) *ciphers = NULL;
910 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
914 * We do this so that we will respond with our native type. If we are
915 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
916 * switching should be handled by a different method. If we are SSLv3, we
917 * will respond with SSLv3, even if prompted with TLSv1.
919 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
920 s->state = SSL3_ST_SR_CLNT_HELLO_B;
923 n = s->method->ssl_get_message(s,
924 SSL3_ST_SR_CLNT_HELLO_B,
925 SSL3_ST_SR_CLNT_HELLO_C,
926 SSL3_MT_CLIENT_HELLO,
927 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
932 d = p = (unsigned char *)s->init_msg;
934 /* First lets get s->client_version set correctly */
935 if (!s->read_hash && !s->enc_read_ctx
936 && RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
937 if (n < MIN_SSL2_RECORD_LEN) {
938 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
939 al = SSL_AD_DECODE_ERROR;
943 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
944 * header is sent directly on the wire, not wrapped as a TLS
945 * record. Our record layer just processes the message length and passes
946 * the rest right through. Its format is:
948 * 0-1 msg_length - decoded by the record layer
949 * 2 msg_type - s->init_msg points here
951 * 5-6 cipher_spec_length
952 * 7-8 session_id_length
953 * 9-10 challenge_length
957 if (p[0] != SSL2_MT_CLIENT_HELLO) {
959 * Should never happen. We should have tested this in the record
960 * layer in order to have determined that this is a SSLv2 record
963 al = SSL_AD_HANDSHAKE_FAILURE;
964 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
968 if ((p[1] == 0x00) && (p[2] == 0x02)) {
969 /* This is real SSLv2. We don't support it. */
970 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
972 } else if (p[1] == SSL3_VERSION_MAJOR) {
974 s->client_version = (((int)p[1]) << 8) | (int)p[2];
976 /* No idea what protocol this is */
977 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
982 * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
983 * for session id length
985 if (n < 2 + SSL3_RANDOM_SIZE + 1) {
986 al = SSL_AD_DECODE_ERROR;
987 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
992 * use version from inside client hello, not from record header (may
993 * differ: see RFC 2246, Appendix E, second paragraph)
995 s->client_version = (((int)p[0]) << 8) | (int)p[1];
998 /* Do SSL/TLS version negotiation if applicable */
999 if (!SSL_IS_DTLS(s)) {
1000 if (s->version != TLS_ANY_VERSION) {
1001 if (s->client_version >= s->version
1002 && (((s->client_version >> 8) & 0xff) == SSL3_VERSION_MAJOR)) {
1007 * We already know that this is an SSL3_VERSION_MAJOR protocol,
1008 * so we're just testing the minor versions here
1010 switch(s->client_version) {
1012 case TLS1_2_VERSION:
1013 if(!(s->options & SSL_OP_NO_TLSv1_2)) {
1014 s->version = TLS1_2_VERSION;
1015 s->method = TLSv1_2_server_method();
1019 /* Deliberately fall through */
1020 case TLS1_1_VERSION:
1021 if(!(s->options & SSL_OP_NO_TLSv1_1)) {
1022 s->version = TLS1_1_VERSION;
1023 s->method = TLSv1_1_server_method();
1027 /* Deliberately fall through */
1029 if(!(s->options & SSL_OP_NO_TLSv1)) {
1030 s->version = TLS1_VERSION;
1031 s->method = TLSv1_server_method();
1035 /* Deliberately fall through */
1037 if(!(s->options & SSL_OP_NO_SSLv3)) {
1038 s->version = SSL3_VERSION;
1039 s->method = SSLv3_server_method();
1045 } else if (((s->client_version >> 8) & 0xff) == DTLS1_VERSION_MAJOR &&
1046 (s->client_version <= s->version
1047 || s->method->version == DTLS_ANY_VERSION)) {
1049 * For DTLS we just check versions are potentially compatible. Version
1050 * negotiation comes later.
1056 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1057 if ((!s->enc_write_ctx && !s->write_hash)) {
1059 * similar to ssl3_get_record, send alert using remote version
1062 s->version = s->client_version;
1064 al = SSL_AD_PROTOCOL_VERSION;
1068 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1070 * Handle an SSLv2 backwards compatible ClientHello
1071 * Note, this is only for SSLv3+ using the backward compatible format.
1072 * Real SSLv2 is not supported, and is rejected above.
1074 unsigned int csl, sil, cl;
1081 if (csl + sil + cl + MIN_SSL2_RECORD_LEN != (unsigned int) n) {
1082 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1083 al = SSL_AD_DECODE_ERROR;
1088 /* we need at least one cipher */
1089 al = SSL_AD_ILLEGAL_PARAMETER;
1090 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1094 if (ssl_bytes_to_cipher_list(s, p, csl, &(ciphers), 1) == NULL) {
1099 * Ignore any session id. We don't allow resumption in a backwards
1100 * compatible ClientHello
1104 if (!ssl_get_new_session(s, 1))
1107 /* Load the client random */
1108 i = (cl > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : cl;
1109 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1110 memcpy(s->s3->client_random, &(p[csl + sil]), i);
1112 /* Set p to end of packet to ensure we don't look for extensions */
1115 /* No compression, so set i to 0 */
1118 /* If we get here we've got SSLv3+ in an SSLv3+ record */
1123 * If we require cookies and this ClientHello doesn't contain one, just
1124 * return since we do not want to allocate any memory yet. So check
1127 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1128 unsigned int session_length, cookie_length;
1130 session_length = *(p + SSL3_RANDOM_SIZE);
1132 if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
1133 al = SSL_AD_DECODE_ERROR;
1134 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1137 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
1139 if (cookie_length == 0)
1143 /* load the client random */
1144 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
1145 p += SSL3_RANDOM_SIZE;
1147 /* get the session-id */
1150 if (p + j > d + n) {
1151 al = SSL_AD_DECODE_ERROR;
1152 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1158 * Versions before 0.9.7 always allow clients to resume sessions in
1159 * renegotiation. 0.9.7 and later allow this by default, but optionally
1160 * ignore resumption requests with flag
1161 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1162 * than a change to default behavior so that applications relying on
1163 * this for security won't even compile against older library versions).
1164 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1165 * request renegotiation but not a new session (s->new_session remains
1166 * unset): for servers, this essentially just means that the
1167 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1171 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1172 if (!ssl_get_new_session(s, 1))
1175 i = ssl_get_prev_session(s, p, j, d + n);
1177 * Only resume if the session's version matches the negotiated
1179 * RFC 5246 does not provide much useful advice on resumption
1180 * with a different protocol version. It doesn't forbid it but
1181 * the sanity of such behaviour would be questionable.
1182 * In practice, clients do not accept a version mismatch and
1183 * will abort the handshake with an error.
1185 if (i == 1 && s->version == s->session->ssl_version) {
1186 /* previous session */
1192 if (!ssl_get_new_session(s, 1))
1199 if (SSL_IS_DTLS(s)) {
1201 if (p + 1 > d + n) {
1202 al = SSL_AD_DECODE_ERROR;
1203 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1206 cookie_len = *(p++);
1208 if (p + cookie_len > d + n) {
1209 al = SSL_AD_DECODE_ERROR;
1210 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1215 * The ClientHello may contain a cookie even if the
1216 * HelloVerify message has not been sent--make sure that it
1217 * does not cause an overflow.
1219 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1221 al = SSL_AD_DECODE_ERROR;
1222 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1226 /* verify the cookie if appropriate option is set. */
1227 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
1228 && cookie_len > 0) {
1229 memcpy(s->d1->rcvd_cookie, p, cookie_len);
1231 if (s->ctx->app_verify_cookie_cb != NULL) {
1232 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1234 al = SSL_AD_HANDSHAKE_FAILURE;
1235 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1236 SSL_R_COOKIE_MISMATCH);
1239 /* else cookie verification succeeded */
1241 /* default verification */
1242 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1243 s->d1->cookie_len) != 0) {
1244 al = SSL_AD_HANDSHAKE_FAILURE;
1245 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1248 /* Set to -2 so if successful we return 2 */
1253 if (s->method->version == DTLS_ANY_VERSION) {
1254 /* Select version to use */
1255 if (s->client_version <= DTLS1_2_VERSION &&
1256 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1257 s->version = DTLS1_2_VERSION;
1258 s->method = DTLSv1_2_server_method();
1259 } else if (tls1_suiteb(s)) {
1260 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1261 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1262 s->version = s->client_version;
1263 al = SSL_AD_PROTOCOL_VERSION;
1265 } else if (s->client_version <= DTLS1_VERSION &&
1266 !(s->options & SSL_OP_NO_DTLSv1)) {
1267 s->version = DTLS1_VERSION;
1268 s->method = DTLSv1_server_method();
1270 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1271 SSL_R_WRONG_VERSION_NUMBER);
1272 s->version = s->client_version;
1273 al = SSL_AD_PROTOCOL_VERSION;
1276 s->session->ssl_version = s->version;
1280 if (p + 2 > d + n) {
1281 al = SSL_AD_DECODE_ERROR;
1282 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1288 al = SSL_AD_ILLEGAL_PARAMETER;
1289 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1293 /* i bytes of cipher data + 1 byte for compression length later */
1294 if ((p + i + 1) > (d + n)) {
1295 /* not enough data */
1296 al = SSL_AD_DECODE_ERROR;
1297 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1300 if (ssl_bytes_to_cipher_list(s, p, i, &(ciphers), 0) == NULL) {
1305 /* If it is a hit, check that the cipher is in the list */
1308 id = s->session->cipher->id;
1311 fprintf(stderr, "client sent %d ciphers\n",
1312 sk_SSL_CIPHER_num(ciphers));
1314 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1315 c = sk_SSL_CIPHER_value(ciphers, i);
1317 fprintf(stderr, "client [%2d of %2d]:%s\n",
1318 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1326 * Disabled because it can be used in a ciphersuite downgrade
1331 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1332 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1334 * Special case as client bug workaround: the previously used
1335 * cipher may not be in the current list, the client instead
1336 * might be trying to continue using a cipher that before wasn't
1337 * chosen due to server preferences. We'll have to reject the
1338 * connection if the cipher is not enabled, though.
1340 c = sk_SSL_CIPHER_value(ciphers, 0);
1341 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1342 s->session->cipher = c;
1349 * we need to have the cipher in the cipher list if we are asked
1352 al = SSL_AD_ILLEGAL_PARAMETER;
1353 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1354 SSL_R_REQUIRED_CIPHER_MISSING);
1361 if ((p + i) > (d + n)) {
1362 /* not enough data */
1363 al = SSL_AD_DECODE_ERROR;
1364 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1367 #ifndef OPENSSL_NO_COMP
1370 for (j = 0; j < i; j++) {
1378 al = SSL_AD_DECODE_ERROR;
1379 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1384 #ifndef OPENSSL_NO_TLSEXT
1385 /* TLS extensions */
1386 if (s->version >= SSL3_VERSION) {
1387 if (!ssl_parse_clienthello_tlsext(s, &p, d, n)) {
1388 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1394 * Check if we want to use external pre-shared secret for this handshake
1395 * for not reused session only. We need to generate server_random before
1396 * calling tls_session_secret_cb in order to allow SessionTicket
1397 * processing to use it in key derivation.
1401 pos = s->s3->server_random;
1402 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1407 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1408 SSL_CIPHER *pref_cipher = NULL;
1410 s->session->master_key_length = sizeof(s->session->master_key);
1411 if (s->tls_session_secret_cb(s, s->session->master_key,
1412 &s->session->master_key_length, ciphers,
1414 s->tls_session_secret_cb_arg)) {
1416 s->session->ciphers = ciphers;
1417 s->session->verify_result = X509_V_OK;
1421 /* check if some cipher was preferred by call back */
1423 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1428 if (pref_cipher == NULL) {
1429 al = SSL_AD_HANDSHAKE_FAILURE;
1430 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1434 s->session->cipher = pref_cipher;
1435 sk_SSL_CIPHER_free(s->cipher_list);
1436 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1437 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1438 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1444 * Worst case, we will use the NULL compression, but if we have other
1445 * options, we will now look for them. We have i-1 compression
1446 * algorithms from the client, starting at q.
1448 s->s3->tmp.new_compression = NULL;
1449 #ifndef OPENSSL_NO_COMP
1450 /* This only happens if we have a cache hit */
1451 if (s->session->compress_meth != 0) {
1452 int m, comp_id = s->session->compress_meth;
1453 /* Perform sanity checks on resumed compression algorithm */
1454 /* Can't disable compression */
1455 if (!ssl_allow_compression(s)) {
1456 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1457 SSL_R_INCONSISTENT_COMPRESSION);
1460 /* Look for resumed compression method */
1461 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1462 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1463 if (comp_id == comp->id) {
1464 s->s3->tmp.new_compression = comp;
1468 if (s->s3->tmp.new_compression == NULL) {
1469 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1470 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1473 /* Look for resumed method in compression list */
1474 for (m = 0; m < i; m++) {
1475 if (q[m] == comp_id)
1479 al = SSL_AD_ILLEGAL_PARAMETER;
1480 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1481 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1486 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1487 /* See if we have a match */
1488 int m, nn, o, v, done = 0;
1490 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1491 for (m = 0; m < nn; m++) {
1492 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1494 for (o = 0; o < i; o++) {
1504 s->s3->tmp.new_compression = comp;
1510 * If compression is disabled we'd better not try to resume a session
1511 * using compression.
1513 if (s->session->compress_meth != 0) {
1514 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1520 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1524 #ifdef OPENSSL_NO_COMP
1525 s->session->compress_meth = 0;
1527 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1529 sk_SSL_CIPHER_free(s->session->ciphers);
1530 s->session->ciphers = ciphers;
1531 if (ciphers == NULL) {
1532 al = SSL_AD_INTERNAL_ERROR;
1533 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1537 if (!tls1_set_server_sigalgs(s)) {
1538 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1541 /* Let cert callback update server certificates if required */
1543 if (s->cert->cert_cb) {
1544 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1546 al = SSL_AD_INTERNAL_ERROR;
1547 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1551 s->rwstate = SSL_X509_LOOKUP;
1554 s->rwstate = SSL_NOTHING;
1556 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1559 al = SSL_AD_HANDSHAKE_FAILURE;
1560 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1563 s->s3->tmp.new_cipher = c;
1564 /* check whether we should disable session resumption */
1565 if (s->not_resumable_session_cb != NULL)
1566 s->session->not_resumable = s->not_resumable_session_cb(s,
1567 ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE))
1569 if (s->session->not_resumable)
1570 /* do not send a session ticket */
1571 s->tlsext_ticket_expected = 0;
1573 /* Session-id reuse */
1574 s->s3->tmp.new_cipher = s->session->cipher;
1577 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1578 if (!ssl3_digest_cached_records(s))
1583 * we now have the following setup.
1585 * cipher_list - our prefered list of ciphers
1586 * ciphers - the clients prefered list of ciphers
1587 * compression - basically ignored right now
1588 * ssl version is set - sslv3
1589 * s->session - The ssl session has been setup.
1590 * s->hit - session reuse flag
1591 * s->s3->tmp.new_cipher- the new cipher to use.
1594 /* Handles TLS extensions that we couldn't check earlier */
1595 if (s->version >= SSL3_VERSION) {
1596 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1597 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1606 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1608 s->state = SSL_ST_ERR;
1611 sk_SSL_CIPHER_free(ciphers);
1612 return ret < 0 ? -1 : ret;
1615 int ssl3_send_server_hello(SSL *s)
1618 unsigned char *p, *d;
1623 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1624 buf = (unsigned char *)s->init_buf->data;
1625 #ifdef OPENSSL_NO_TLSEXT
1626 p = s->s3->server_random;
1627 if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0) {
1628 s->state = SSL_ST_ERR;
1632 /* Do the message type and length last */
1633 d = p = ssl_handshake_start(s);
1635 *(p++) = s->version >> 8;
1636 *(p++) = s->version & 0xff;
1639 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1640 p += SSL3_RANDOM_SIZE;
1643 * There are several cases for the session ID to send
1644 * back in the server hello:
1645 * - For session reuse from the session cache,
1646 * we send back the old session ID.
1647 * - If stateless session reuse (using a session ticket)
1648 * is successful, we send back the client's "session ID"
1649 * (which doesn't actually identify the session).
1650 * - If it is a new session, we send back the new
1652 * - However, if we want the new session to be single-use,
1653 * we send back a 0-length session ID.
1654 * s->hit is non-zero in either case of session reuse,
1655 * so the following won't overwrite an ID that we're supposed
1658 if (s->session->not_resumable ||
1659 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1661 s->session->session_id_length = 0;
1663 sl = s->session->session_id_length;
1664 if (sl > (int)sizeof(s->session->session_id)) {
1665 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1666 s->state = SSL_ST_ERR;
1670 memcpy(p, s->session->session_id, sl);
1673 /* put the cipher */
1674 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1677 /* put the compression method */
1678 #ifdef OPENSSL_NO_COMP
1681 if (s->s3->tmp.new_compression == NULL)
1684 *(p++) = s->s3->tmp.new_compression->id;
1686 #ifndef OPENSSL_NO_TLSEXT
1687 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1688 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1689 s->state = SSL_ST_ERR;
1693 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1695 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1696 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1697 s->state = SSL_ST_ERR;
1703 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1704 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1707 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1710 /* SSL3_ST_SW_SRVR_HELLO_B */
1711 return ssl_do_write(s);
1714 int ssl3_send_server_done(SSL *s)
1717 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1718 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1719 SSLerr(SSL_F_SSL3_SEND_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1722 s->state = SSL3_ST_SW_SRVR_DONE_B;
1725 /* SSL3_ST_SW_SRVR_DONE_B */
1726 return ssl_do_write(s);
1729 int ssl3_send_server_key_exchange(SSL *s)
1731 #ifndef OPENSSL_NO_RSA
1735 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1738 #ifndef OPENSSL_NO_DH
1739 DH *dh = NULL, *dhp;
1741 #ifndef OPENSSL_NO_EC
1742 EC_KEY *ecdh = NULL, *ecdhp;
1743 unsigned char *encodedPoint = NULL;
1746 BN_CTX *bn_ctx = NULL;
1749 const EVP_MD *md = NULL;
1750 unsigned char *p, *d;
1760 EVP_MD_CTX_init(&md_ctx);
1761 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1762 type = s->s3->tmp.new_cipher->algorithm_mkey;
1767 r[0] = r[1] = r[2] = r[3] = NULL;
1769 #ifndef OPENSSL_NO_RSA
1770 if (type & SSL_kRSA) {
1771 rsa = cert->rsa_tmp;
1772 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1773 rsa = s->cert->rsa_tmp_cb(s,
1774 SSL_C_IS_EXPORT(s->s3->
1776 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1779 al = SSL_AD_HANDSHAKE_FAILURE;
1780 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1781 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1785 cert->rsa_tmp = rsa;
1788 al = SSL_AD_HANDSHAKE_FAILURE;
1789 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1790 SSL_R_MISSING_TMP_RSA_KEY);
1795 s->s3->tmp.use_rsa_tmp = 1;
1798 #ifndef OPENSSL_NO_DH
1799 if (type & SSL_kDHE) {
1800 if (s->cert->dh_tmp_auto) {
1801 dhp = ssl_get_auto_dh(s);
1803 al = SSL_AD_INTERNAL_ERROR;
1804 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1805 ERR_R_INTERNAL_ERROR);
1810 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1811 dhp = s->cert->dh_tmp_cb(s,
1812 SSL_C_IS_EXPORT(s->s3->
1814 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1817 al = SSL_AD_HANDSHAKE_FAILURE;
1818 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1819 SSL_R_MISSING_TMP_DH_KEY);
1822 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1823 DH_security_bits(dhp), 0, dhp)) {
1824 al = SSL_AD_HANDSHAKE_FAILURE;
1825 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1826 SSL_R_DH_KEY_TOO_SMALL);
1829 if (s->s3->tmp.dh != NULL) {
1830 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1831 ERR_R_INTERNAL_ERROR);
1835 if (s->cert->dh_tmp_auto)
1837 else if ((dh = DHparams_dup(dhp)) == NULL) {
1838 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1843 if ((dhp->pub_key == NULL ||
1844 dhp->priv_key == NULL ||
1845 (s->options & SSL_OP_SINGLE_DH_USE))) {
1846 if (!DH_generate_key(dh)) {
1847 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1851 dh->pub_key = BN_dup(dhp->pub_key);
1852 dh->priv_key = BN_dup(dhp->priv_key);
1853 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1854 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1863 #ifndef OPENSSL_NO_EC
1864 if (type & SSL_kECDHE) {
1865 const EC_GROUP *group;
1867 ecdhp = cert->ecdh_tmp;
1868 if (s->cert->ecdh_tmp_auto) {
1869 /* Get NID of appropriate shared curve */
1870 int nid = tls1_shared_curve(s, -2);
1871 if (nid != NID_undef)
1872 ecdhp = EC_KEY_new_by_curve_name(nid);
1873 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1874 ecdhp = s->cert->ecdh_tmp_cb(s,
1875 SSL_C_IS_EXPORT(s->s3->
1877 SSL_C_EXPORT_PKEYLENGTH(s->
1878 s3->tmp.new_cipher));
1880 if (ecdhp == NULL) {
1881 al = SSL_AD_HANDSHAKE_FAILURE;
1882 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1883 SSL_R_MISSING_TMP_ECDH_KEY);
1887 if (s->s3->tmp.ecdh != NULL) {
1888 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1889 ERR_R_INTERNAL_ERROR);
1893 /* Duplicate the ECDH structure. */
1894 if (ecdhp == NULL) {
1895 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1898 if (s->cert->ecdh_tmp_auto)
1900 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1901 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1905 s->s3->tmp.ecdh = ecdh;
1906 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1907 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1908 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1909 if (!EC_KEY_generate_key(ecdh)) {
1910 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1916 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1917 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1918 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1919 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1923 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1924 (EC_GROUP_get_degree(group) > 163)) {
1925 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1926 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1931 * XXX: For now, we only support ephemeral ECDH keys over named
1932 * (not generic) curves. For supported named curves, curve_id is
1936 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1938 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1939 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1944 * Encode the public key. First check the size of encoding and
1945 * allocate memory accordingly.
1947 encodedlen = EC_POINT_point2oct(group,
1948 EC_KEY_get0_public_key(ecdh),
1949 POINT_CONVERSION_UNCOMPRESSED,
1952 encodedPoint = (unsigned char *)
1953 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1954 bn_ctx = BN_CTX_new();
1955 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1956 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1957 ERR_R_MALLOC_FAILURE);
1961 encodedlen = EC_POINT_point2oct(group,
1962 EC_KEY_get0_public_key(ecdh),
1963 POINT_CONVERSION_UNCOMPRESSED,
1964 encodedPoint, encodedlen, bn_ctx);
1966 if (encodedlen == 0) {
1967 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1971 BN_CTX_free(bn_ctx);
1975 * XXX: For now, we only support named (not generic) curves in
1976 * ECDH ephemeral key exchanges. In this situation, we need four
1977 * additional bytes to encode the entire ServerECDHParams
1983 * We'll generate the serverKeyExchange message explicitly so we
1984 * can set these to NULLs
1991 #endif /* !OPENSSL_NO_EC */
1992 #ifndef OPENSSL_NO_PSK
1993 if (type & SSL_kPSK) {
1995 * reserve size for record length and PSK identity hint
1997 n += 2 + strlen(s->ctx->psk_identity_hint);
1999 #endif /* !OPENSSL_NO_PSK */
2000 #ifndef OPENSSL_NO_SRP
2001 if (type & SSL_kSRP) {
2002 if ((s->srp_ctx.N == NULL) ||
2003 (s->srp_ctx.g == NULL) ||
2004 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2005 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2006 SSL_R_MISSING_SRP_PARAM);
2009 r[0] = s->srp_ctx.N;
2010 r[1] = s->srp_ctx.g;
2011 r[2] = s->srp_ctx.s;
2012 r[3] = s->srp_ctx.B;
2016 al = SSL_AD_HANDSHAKE_FAILURE;
2017 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2018 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2021 for (i = 0; i < 4 && r[i] != NULL; i++) {
2022 nr[i] = BN_num_bytes(r[i]);
2023 #ifndef OPENSSL_NO_SRP
2024 if ((i == 2) && (type & SSL_kSRP))
2031 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
2032 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2033 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
2035 al = SSL_AD_DECODE_ERROR;
2038 kn = EVP_PKEY_size(pkey);
2044 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
2045 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
2048 d = p = ssl_handshake_start(s);
2050 for (i = 0; i < 4 && r[i] != NULL; i++) {
2051 #ifndef OPENSSL_NO_SRP
2052 if ((i == 2) && (type & SSL_kSRP)) {
2062 #ifndef OPENSSL_NO_EC
2063 if (type & SSL_kECDHE) {
2065 * XXX: For now, we only support named (not generic) curves. In
2066 * this situation, the serverKeyExchange message has: [1 byte
2067 * CurveType], [2 byte CurveName] [1 byte length of encoded
2068 * point], followed by the actual encoded point itself
2070 *p = NAMED_CURVE_TYPE;
2078 memcpy(p, encodedPoint, encodedlen);
2079 OPENSSL_free(encodedPoint);
2080 encodedPoint = NULL;
2085 #ifndef OPENSSL_NO_PSK
2086 if (type & SSL_kPSK) {
2087 /* copy PSK identity hint */
2088 s2n(strlen(s->ctx->psk_identity_hint), p);
2089 strncpy((char *)p, s->ctx->psk_identity_hint,
2090 strlen(s->ctx->psk_identity_hint));
2091 p += strlen(s->ctx->psk_identity_hint);
2098 * n is the length of the params, they start at &(d[4]) and p
2099 * points to the space at the end.
2101 #ifndef OPENSSL_NO_RSA
2102 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
2105 for (num = 2; num > 0; num--) {
2106 EVP_MD_CTX_set_flags(&md_ctx,
2107 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2108 EVP_DigestInit_ex(&md_ctx, (num == 2)
2109 ? s->ctx->md5 : s->ctx->sha1, NULL);
2110 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2112 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2114 EVP_DigestUpdate(&md_ctx, d, n);
2115 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
2119 if (RSA_sign(NID_md5_sha1, md_buf, j,
2120 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2121 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2129 /* send signature algorithm */
2130 if (SSL_USE_SIGALGS(s)) {
2131 if (!tls12_get_sigandhash(p, pkey, md)) {
2132 /* Should never happen */
2133 al = SSL_AD_INTERNAL_ERROR;
2134 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2135 ERR_R_INTERNAL_ERROR);
2141 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2143 EVP_SignInit_ex(&md_ctx, md, NULL);
2144 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2146 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2148 EVP_SignUpdate(&md_ctx, d, n);
2149 if (!EVP_SignFinal(&md_ctx, &(p[2]),
2150 (unsigned int *)&i, pkey)) {
2151 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2156 if (SSL_USE_SIGALGS(s))
2159 /* Is this error check actually needed? */
2160 al = SSL_AD_HANDSHAKE_FAILURE;
2161 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2162 SSL_R_UNKNOWN_PKEY_TYPE);
2167 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
2168 al = SSL_AD_HANDSHAKE_FAILURE;
2169 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2174 s->state = SSL3_ST_SW_KEY_EXCH_B;
2175 EVP_MD_CTX_cleanup(&md_ctx);
2176 return ssl_do_write(s);
2178 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2180 #ifndef OPENSSL_NO_EC
2181 OPENSSL_free(encodedPoint);
2182 BN_CTX_free(bn_ctx);
2184 EVP_MD_CTX_cleanup(&md_ctx);
2185 s->state = SSL_ST_ERR;
2189 int ssl3_send_certificate_request(SSL *s)
2191 unsigned char *p, *d;
2192 int i, j, nl, off, n;
2193 STACK_OF(X509_NAME) *sk = NULL;
2197 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2200 d = p = ssl_handshake_start(s);
2202 /* get the list of acceptable cert types */
2204 n = ssl3_get_req_cert_type(s, p);
2209 if (SSL_USE_SIGALGS(s)) {
2210 const unsigned char *psigs;
2211 unsigned char *etmp = p;
2212 nl = tls12_get_psigalgs(s, &psigs);
2213 /* Skip over length for now */
2215 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2216 /* Now fill in length */
2226 sk = SSL_get_client_CA_list(s);
2229 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2230 name = sk_X509_NAME_value(sk, i);
2231 j = i2d_X509_NAME(name, NULL);
2232 if (!BUF_MEM_grow_clean
2233 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2234 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2238 p = ssl_handshake_start(s) + n;
2240 i2d_X509_NAME(name, &p);
2245 /* else no CA names */
2246 p = ssl_handshake_start(s) + off;
2249 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2250 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2254 s->state = SSL3_ST_SW_CERT_REQ_B;
2257 /* SSL3_ST_SW_CERT_REQ_B */
2258 return ssl_do_write(s);
2260 s->state = SSL_ST_ERR;
2264 int ssl3_get_client_key_exchange(SSL *s)
2268 unsigned long alg_k;
2270 #ifndef OPENSSL_NO_RSA
2272 EVP_PKEY *pkey = NULL;
2274 #ifndef OPENSSL_NO_DH
2276 DH *dh_srvr, *dh_clnt = NULL;
2279 #ifndef OPENSSL_NO_EC
2280 EC_KEY *srvr_ecdh = NULL;
2281 EVP_PKEY *clnt_pub_pkey = NULL;
2282 EC_POINT *clnt_ecpoint = NULL;
2283 BN_CTX *bn_ctx = NULL;
2286 n = s->method->ssl_get_message(s,
2287 SSL3_ST_SR_KEY_EXCH_A,
2288 SSL3_ST_SR_KEY_EXCH_B,
2289 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2293 p = (unsigned char *)s->init_msg;
2295 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2297 #ifndef OPENSSL_NO_RSA
2298 if (alg_k & SSL_kRSA) {
2299 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2301 unsigned char decrypt_good, version_good;
2304 /* FIX THIS UP EAY EAY EAY EAY */
2305 if (s->s3->tmp.use_rsa_tmp) {
2306 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2307 rsa = s->cert->rsa_tmp;
2309 * Don't do a callback because rsa_tmp should be sent already
2312 al = SSL_AD_HANDSHAKE_FAILURE;
2313 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2314 SSL_R_MISSING_TMP_RSA_PKEY);
2319 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2320 if ((pkey == NULL) ||
2321 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2322 al = SSL_AD_HANDSHAKE_FAILURE;
2323 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2324 SSL_R_MISSING_RSA_CERTIFICATE);
2327 rsa = pkey->pkey.rsa;
2330 /* TLS and [incidentally] DTLS{0xFEFF} */
2331 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2334 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2335 al = SSL_AD_DECODE_ERROR;
2336 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2337 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2346 * Reject overly short RSA ciphertext because we want to be sure
2347 * that the buffer size makes it safe to iterate over the entire
2348 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2349 * actual expected size is larger due to RSA padding, but the
2350 * bound is sufficient to be safe.
2352 if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2353 al = SSL_AD_DECRYPT_ERROR;
2354 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2355 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2360 * We must not leak whether a decryption failure occurs because of
2361 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2362 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2363 * generates a random premaster secret for the case that the decrypt
2364 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2367 if (RAND_bytes(rand_premaster_secret,
2368 sizeof(rand_premaster_secret)) <= 0)
2371 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2375 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2376 * be 0xff if so and zero otherwise.
2379 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2382 * If the version in the decrypted pre-master secret is correct then
2383 * version_good will be 0xff, otherwise it'll be zero. The
2384 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2385 * (http://eprint.iacr.org/2003/052/) exploits the version number
2386 * check as a "bad version oracle". Thus version checks are done in
2387 * constant time and are treated like any other decryption error.
2390 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2392 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2395 * The premaster secret must contain the same version number as the
2396 * ClientHello to detect version rollback attacks (strangely, the
2397 * protocol does not offer such protection for DH ciphersuites).
2398 * However, buggy clients exist that send the negotiated protocol
2399 * version instead if the server does not support the requested
2400 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2403 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2404 unsigned char workaround_good;
2406 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2408 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2409 version_good |= workaround_good;
2413 * Both decryption and version must be good for decrypt_good to
2414 * remain non-zero (0xff).
2416 decrypt_good &= version_good;
2419 * Now copy rand_premaster_secret over from p using
2420 * decrypt_good_mask. If decryption failed, then p does not
2421 * contain valid plaintext, however, a check above guarantees
2422 * it is still sufficiently large to read from.
2424 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2425 p[j] = constant_time_select_8(decrypt_good, p[j],
2426 rand_premaster_secret[j]);
2429 s->session->master_key_length =
2430 s->method->ssl3_enc->generate_master_secret(s,
2432 session->master_key,
2435 (rand_premaster_secret));
2436 OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
2437 if (s->session->master_key_length < 0) {
2438 al = SSL_AD_INTERNAL_ERROR;
2439 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2444 #ifndef OPENSSL_NO_DH
2445 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2447 EVP_PKEY *skey = NULL;
2451 if (alg_k & SSL_kDHE) {
2452 al = SSL_AD_HANDSHAKE_FAILURE;
2453 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2454 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2459 if (n && n != i + 2) {
2460 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2461 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2462 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2469 if (alg_k & SSL_kDHr)
2470 idx = SSL_PKEY_DH_RSA;
2471 else if (alg_k & SSL_kDHd)
2472 idx = SSL_PKEY_DH_DSA;
2474 skey = s->cert->pkeys[idx].privatekey;
2475 if ((skey == NULL) ||
2476 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2477 al = SSL_AD_HANDSHAKE_FAILURE;
2478 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2479 SSL_R_MISSING_RSA_CERTIFICATE);
2482 dh_srvr = skey->pkey.dh;
2483 } else if (s->s3->tmp.dh == NULL) {
2484 al = SSL_AD_HANDSHAKE_FAILURE;
2485 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2486 SSL_R_MISSING_TMP_DH_KEY);
2489 dh_srvr = s->s3->tmp.dh;
2492 /* Get pubkey from cert */
2493 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2495 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2496 dh_clnt = EVP_PKEY_get1_DH(clkey);
2498 if (dh_clnt == NULL) {
2499 al = SSL_AD_HANDSHAKE_FAILURE;
2500 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2501 SSL_R_MISSING_TMP_DH_KEY);
2504 EVP_PKEY_free(clkey);
2505 pub = dh_clnt->pub_key;
2507 pub = BN_bin2bn(p, i, NULL);
2509 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2513 i = DH_compute_key(p, pub, dh_srvr);
2516 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2521 DH_free(s->s3->tmp.dh);
2522 s->s3->tmp.dh = NULL;
2528 s->session->master_key_length =
2529 s->method->ssl3_enc->generate_master_secret(s,
2531 session->master_key,
2533 OPENSSL_cleanse(p, i);
2534 if (s->session->master_key_length < 0) {
2535 al = SSL_AD_INTERNAL_ERROR;
2536 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2544 #ifndef OPENSSL_NO_EC
2545 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2549 const EC_GROUP *group;
2550 const BIGNUM *priv_key;
2552 /* initialize structures for server's ECDH key pair */
2553 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2554 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2558 /* Let's get server private key and group information */
2559 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2560 /* use the certificate */
2561 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2564 * use the ephermeral values we saved when generating the
2565 * ServerKeyExchange msg.
2567 tkey = s->s3->tmp.ecdh;
2570 group = EC_KEY_get0_group(tkey);
2571 priv_key = EC_KEY_get0_private_key(tkey);
2573 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2574 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2575 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2579 /* Let's get client's public key */
2580 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2581 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2586 /* Client Publickey was in Client Certificate */
2588 if (alg_k & SSL_kECDHE) {
2589 al = SSL_AD_HANDSHAKE_FAILURE;
2590 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2591 SSL_R_MISSING_TMP_ECDH_KEY);
2594 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2595 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2597 * XXX: For now, we do not support client authentication
2598 * using ECDH certificates so this branch (n == 0L) of the
2599 * code is never executed. When that support is added, we
2600 * ought to ensure the key received in the certificate is
2601 * authorized for key agreement. ECDH_compute_key implicitly
2602 * checks that the two ECDH shares are for the same group.
2604 al = SSL_AD_HANDSHAKE_FAILURE;
2605 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2606 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2610 if (EC_POINT_copy(clnt_ecpoint,
2611 EC_KEY_get0_public_key(clnt_pub_pkey->
2613 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2616 ret = 2; /* Skip certificate verify processing */
2619 * Get client's public key from encoded point in the
2620 * ClientKeyExchange message.
2622 if ((bn_ctx = BN_CTX_new()) == NULL) {
2623 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2624 ERR_R_MALLOC_FAILURE);
2628 /* Get encoded point length */
2632 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2635 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2636 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2640 * p is pointing to somewhere in the buffer currently, so set it
2643 p = (unsigned char *)s->init_buf->data;
2646 /* Compute the shared pre-master secret */
2647 field_size = EC_GROUP_get_degree(group);
2648 if (field_size <= 0) {
2649 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2652 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2655 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2659 EVP_PKEY_free(clnt_pub_pkey);
2660 EC_POINT_free(clnt_ecpoint);
2661 EC_KEY_free(srvr_ecdh);
2662 BN_CTX_free(bn_ctx);
2663 EC_KEY_free(s->s3->tmp.ecdh);
2664 s->s3->tmp.ecdh = NULL;
2666 /* Compute the master secret */
2667 s->session->master_key_length =
2668 s->method->ssl3_enc->generate_master_secret(s,
2670 session->master_key,
2673 OPENSSL_cleanse(p, i);
2674 if (s->session->master_key_length < 0) {
2675 al = SSL_AD_INTERNAL_ERROR;
2676 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2682 #ifndef OPENSSL_NO_PSK
2683 if (alg_k & SSL_kPSK) {
2684 unsigned char *t = NULL;
2685 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2686 unsigned int pre_ms_len = 0, psk_len = 0;
2688 char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2690 al = SSL_AD_HANDSHAKE_FAILURE;
2694 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2697 if (i > PSK_MAX_IDENTITY_LEN) {
2698 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2699 SSL_R_DATA_LENGTH_TOO_LONG);
2702 if (s->psk_server_callback == NULL) {
2703 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2704 SSL_R_PSK_NO_SERVER_CB);
2709 * Create guaranteed NULL-terminated identity string for the callback
2711 memcpy(tmp_id, p, i);
2712 memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2713 psk_len = s->psk_server_callback(s, tmp_id,
2715 sizeof(psk_or_pre_ms));
2716 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2718 if (psk_len > PSK_MAX_PSK_LEN) {
2719 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2721 } else if (psk_len == 0) {
2723 * PSK related to the given identity not found
2725 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2726 SSL_R_PSK_IDENTITY_NOT_FOUND);
2727 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2731 /* create PSK pre_master_secret */
2732 pre_ms_len = 2 + psk_len + 2 + psk_len;
2734 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2736 memset(t, 0, psk_len);
2740 OPENSSL_free(s->session->psk_identity);
2741 s->session->psk_identity = BUF_strdup((char *)p);
2742 if (s->session->psk_identity == NULL) {
2743 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2747 OPENSSL_free(s->session->psk_identity_hint);
2748 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2749 if (s->ctx->psk_identity_hint != NULL &&
2750 s->session->psk_identity_hint == NULL) {
2751 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2755 s->session->master_key_length =
2756 s->method->ssl3_enc->generate_master_secret(s,
2758 session->master_key,
2761 if (s->session->master_key_length < 0) {
2762 al = SSL_AD_INTERNAL_ERROR;
2763 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2768 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2773 #ifndef OPENSSL_NO_SRP
2774 if (alg_k & SSL_kSRP) {
2779 if (param_len > n) {
2780 al = SSL_AD_DECODE_ERROR;
2781 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2782 SSL_R_BAD_SRP_A_LENGTH);
2785 if ((s->srp_ctx.A = BN_bin2bn(p, i, NULL)) == NULL) {
2786 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2789 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2790 || BN_is_zero(s->srp_ctx.A)) {
2791 al = SSL_AD_ILLEGAL_PARAMETER;
2792 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2793 SSL_R_BAD_SRP_PARAMETERS);
2796 OPENSSL_free(s->session->srp_username);
2797 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2798 if (s->session->srp_username == NULL) {
2799 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2803 if ((s->session->master_key_length =
2804 SRP_generate_server_master_secret(s,
2805 s->session->master_key)) < 0) {
2806 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2812 #endif /* OPENSSL_NO_SRP */
2813 if (alg_k & SSL_kGOST) {
2815 EVP_PKEY_CTX *pkey_ctx;
2816 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2817 unsigned char premaster_secret[32], *start;
2818 size_t outlen = 32, inlen;
2819 unsigned long alg_a;
2823 /* Get our certificate private key */
2824 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2825 if (alg_a & SSL_aGOST94)
2826 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2827 else if (alg_a & SSL_aGOST01)
2828 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2830 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2831 EVP_PKEY_decrypt_init(pkey_ctx);
2833 * If client certificate is present and is of the same type, maybe
2834 * use it for key exchange. Don't mind errors from
2835 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2836 * client certificate for authorization only.
2838 client_pub_pkey = X509_get_pubkey(s->session->peer);
2839 if (client_pub_pkey) {
2840 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2843 /* Decrypt session key */
2845 ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2846 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2847 || Tclass != V_ASN1_UNIVERSAL) {
2848 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2849 SSL_R_DECRYPTION_FAILED);
2854 if (EVP_PKEY_decrypt
2855 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2856 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2857 SSL_R_DECRYPTION_FAILED);
2860 /* Generate master secret */
2861 s->session->master_key_length =
2862 s->method->ssl3_enc->generate_master_secret(s,
2864 session->master_key,
2865 premaster_secret, 32);
2866 if (s->session->master_key_length < 0) {
2867 al = SSL_AD_INTERNAL_ERROR;
2868 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2871 /* Check if pubkey from client certificate was used */
2872 if (EVP_PKEY_CTX_ctrl
2873 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2878 EVP_PKEY_free(client_pub_pkey);
2879 EVP_PKEY_CTX_free(pkey_ctx);
2884 al = SSL_AD_HANDSHAKE_FAILURE;
2885 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2891 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2892 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2895 #ifndef OPENSSL_NO_EC
2896 EVP_PKEY_free(clnt_pub_pkey);
2897 EC_POINT_free(clnt_ecpoint);
2898 EC_KEY_free(srvr_ecdh);
2899 BN_CTX_free(bn_ctx);
2901 s->state = SSL_ST_ERR;
2905 int ssl3_get_cert_verify(SSL *s)
2907 EVP_PKEY *pkey = NULL;
2909 int al, ok, ret = 0;
2913 const EVP_MD *md = NULL;
2915 EVP_MD_CTX_init(&mctx);
2918 * We should only process a CertificateVerify message if we have received
2919 * a Certificate from the client. If so then |s->session->peer| will be non
2920 * NULL. In some instances a CertificateVerify message is not required even
2921 * if the peer has sent a Certificate (e.g. such as in the case of static
2922 * DH). In that case the ClientKeyExchange processing will skip the
2923 * CertificateVerify state so we should not arrive here.
2925 if (s->session->peer == NULL) {
2930 n = s->method->ssl_get_message(s,
2931 SSL3_ST_SR_CERT_VRFY_A,
2932 SSL3_ST_SR_CERT_VRFY_B,
2933 SSL3_MT_CERTIFICATE_VERIFY,
2934 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2939 peer = s->session->peer;
2940 pkey = X509_get_pubkey(peer);
2941 type = X509_certificate_type(peer, pkey);
2943 if (!(type & EVP_PKT_SIGN)) {
2944 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2945 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2946 al = SSL_AD_ILLEGAL_PARAMETER;
2950 /* we now have a signature that we need to verify */
2951 p = (unsigned char *)s->init_msg;
2952 /* Check for broken implementations of GOST ciphersuites */
2954 * If key is GOST and n is exactly 64, it is bare signature without
2957 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2958 pkey->type == NID_id_GostR3410_2001)) {
2961 if (SSL_USE_SIGALGS(s)) {
2962 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
2964 al = SSL_AD_INTERNAL_ERROR;
2966 } else if (rv == 0) {
2967 al = SSL_AD_DECODE_ERROR;
2971 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2979 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2980 al = SSL_AD_DECODE_ERROR;
2984 j = EVP_PKEY_size(pkey);
2985 if ((i > j) || (n > j) || (n <= 0)) {
2986 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2987 al = SSL_AD_DECODE_ERROR;
2991 if (SSL_USE_SIGALGS(s)) {
2994 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2995 if (hdatalen <= 0) {
2996 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2997 al = SSL_AD_INTERNAL_ERROR;
3001 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3004 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3005 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3006 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3007 al = SSL_AD_INTERNAL_ERROR;
3011 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
3012 al = SSL_AD_DECRYPT_ERROR;
3013 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3017 #ifndef OPENSSL_NO_RSA
3018 if (pkey->type == EVP_PKEY_RSA) {
3019 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3020 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
3023 al = SSL_AD_DECRYPT_ERROR;
3024 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3028 al = SSL_AD_DECRYPT_ERROR;
3029 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3034 #ifndef OPENSSL_NO_DSA
3035 if (pkey->type == EVP_PKEY_DSA) {
3036 j = DSA_verify(pkey->save_type,
3037 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3038 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
3041 al = SSL_AD_DECRYPT_ERROR;
3042 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3047 #ifndef OPENSSL_NO_EC
3048 if (pkey->type == EVP_PKEY_EC) {
3049 j = ECDSA_verify(pkey->save_type,
3050 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3051 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
3054 al = SSL_AD_DECRYPT_ERROR;
3055 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3060 if (pkey->type == NID_id_GostR3410_94
3061 || pkey->type == NID_id_GostR3410_2001) {
3062 unsigned char signature[64];
3064 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3065 EVP_PKEY_verify_init(pctx);
3067 fprintf(stderr, "GOST signature length is %d", i);
3069 for (idx = 0; idx < 64; idx++) {
3070 signature[63 - idx] = p[idx];
3072 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3074 EVP_PKEY_CTX_free(pctx);
3076 al = SSL_AD_DECRYPT_ERROR;
3077 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3081 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3082 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3089 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3090 s->state = SSL_ST_ERR;
3093 BIO_free(s->s3->handshake_buffer);
3094 s->s3->handshake_buffer = NULL;
3095 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3096 EVP_MD_CTX_cleanup(&mctx);
3097 EVP_PKEY_free(pkey);
3101 int ssl3_get_client_certificate(SSL *s)
3103 int i, ok, al, ret = -1;
3105 unsigned long l, nc, llen, n;
3106 const unsigned char *p, *q;
3108 STACK_OF(X509) *sk = NULL;
3110 n = s->method->ssl_get_message(s,
3113 -1, s->max_cert_list, &ok);
3118 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3119 if ((s->verify_mode & SSL_VERIFY_PEER) &&
3120 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3121 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3122 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3123 al = SSL_AD_HANDSHAKE_FAILURE;
3127 * If tls asked for a client cert, the client must return a 0 list
3129 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3130 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3131 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3132 al = SSL_AD_UNEXPECTED_MESSAGE;
3135 s->s3->tmp.reuse_message = 1;
3139 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3140 al = SSL_AD_UNEXPECTED_MESSAGE;
3141 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3144 p = d = (unsigned char *)s->init_msg;
3146 if ((sk = sk_X509_new_null()) == NULL) {
3147 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3152 if (llen + 3 != n) {
3153 al = SSL_AD_DECODE_ERROR;
3154 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3157 for (nc = 0; nc < llen;) {
3159 if ((l + nc + 3) > llen) {
3160 al = SSL_AD_DECODE_ERROR;
3161 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3162 SSL_R_CERT_LENGTH_MISMATCH);
3167 x = d2i_X509(NULL, &p, l);
3169 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3173 al = SSL_AD_DECODE_ERROR;
3174 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3175 SSL_R_CERT_LENGTH_MISMATCH);
3178 if (!sk_X509_push(sk, x)) {
3179 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3186 if (sk_X509_num(sk) <= 0) {
3187 /* TLS does not mind 0 certs returned */
3188 if (s->version == SSL3_VERSION) {
3189 al = SSL_AD_HANDSHAKE_FAILURE;
3190 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3191 SSL_R_NO_CERTIFICATES_RETURNED);
3194 /* Fail for TLS only if we required a certificate */
3195 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3196 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3197 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3198 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3199 al = SSL_AD_HANDSHAKE_FAILURE;
3202 /* No client certificate so digest cached records */
3203 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3204 al = SSL_AD_INTERNAL_ERROR;
3209 i = ssl_verify_cert_chain(s, sk);
3211 al = ssl_verify_alarm_type(s->verify_result);
3212 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3213 SSL_R_CERTIFICATE_VERIFY_FAILED);
3217 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, i);
3218 al = SSL_AD_HANDSHAKE_FAILURE;
3221 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3223 al = SSL3_AD_HANDSHAKE_FAILURE;
3224 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3225 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3228 EVP_PKEY_free(pkey);
3231 X509_free(s->session->peer);
3232 s->session->peer = sk_X509_shift(sk);
3233 s->session->verify_result = s->verify_result;
3236 * With the current implementation, sess_cert will always be NULL when we
3239 if (s->session->sess_cert == NULL) {
3240 s->session->sess_cert = ssl_sess_cert_new();
3241 if (s->session->sess_cert == NULL) {
3242 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3246 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3247 s->session->sess_cert->cert_chain = sk;
3249 * Inconsistency alert: cert_chain does *not* include the peer's own
3250 * certificate, while we do include it in s3_clnt.c
3257 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3259 s->state = SSL_ST_ERR;
3261 sk_X509_pop_free(sk, X509_free);
3265 int ssl3_send_server_certificate(SSL *s)
3269 if (s->state == SSL3_ST_SW_CERT_A) {
3270 cpk = ssl_get_server_send_pkey(s);
3272 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3273 s->state = SSL_ST_ERR;
3277 if (!ssl3_output_cert_chain(s, cpk)) {
3278 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3279 s->state = SSL_ST_ERR;
3282 s->state = SSL3_ST_SW_CERT_B;
3285 /* SSL3_ST_SW_CERT_B */
3286 return ssl_do_write(s);
3289 #ifndef OPENSSL_NO_TLSEXT
3290 /* send a new session ticket (not necessarily for a new session) */
3291 int ssl3_send_newsession_ticket(SSL *s)
3293 unsigned char *senc = NULL;
3297 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3298 unsigned char *p, *macstart;
3299 const unsigned char *const_p;
3300 int len, slen_full, slen;
3303 SSL_CTX *tctx = s->initial_ctx;
3304 unsigned char iv[EVP_MAX_IV_LENGTH];
3305 unsigned char key_name[16];
3307 /* get session encoding length */
3308 slen_full = i2d_SSL_SESSION(s->session, NULL);
3310 * Some length values are 16 bits, so forget it if session is too
3313 if (slen_full == 0 || slen_full > 0xFF00) {
3314 s->state = SSL_ST_ERR;
3317 senc = OPENSSL_malloc(slen_full);
3319 s->state = SSL_ST_ERR;
3323 EVP_CIPHER_CTX_init(&ctx);
3324 HMAC_CTX_init(&hctx);
3327 if (!i2d_SSL_SESSION(s->session, &p))
3331 * create a fresh copy (not shared with other threads) to clean up
3334 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3337 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3339 slen = i2d_SSL_SESSION(sess, NULL);
3340 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3341 SSL_SESSION_free(sess);
3345 if (!i2d_SSL_SESSION(sess, &p)) {
3346 SSL_SESSION_free(sess);
3349 SSL_SESSION_free(sess);
3352 * Grow buffer if need be: the length calculation is as
3353 * follows handshake_header_length +
3354 * 4 (ticket lifetime hint) + 2 (ticket length) +
3355 * 16 (key name) + max_iv_len (iv length) +
3356 * session_length + max_enc_block_size (max encrypted session
3357 * length) + max_md_size (HMAC).
3359 if (!BUF_MEM_grow(s->init_buf,
3360 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3361 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3364 p = ssl_handshake_start(s);
3366 * Initialize HMAC and cipher contexts. If callback present it does
3367 * all the work otherwise use generated values from parent ctx.
3369 if (tctx->tlsext_ticket_key_cb) {
3370 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3374 if (RAND_bytes(iv, 16) <= 0)
3376 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3377 tctx->tlsext_tick_aes_key, iv))
3379 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3380 EVP_sha256(), NULL))
3382 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3386 * Ticket lifetime hint (advisory only): We leave this unspecified
3387 * for resumed session (for simplicity), and guess that tickets for
3388 * new sessions will live as long as their sessions.
3390 l2n(s->hit ? 0 : s->session->timeout, p);
3392 /* Skip ticket length for now */
3394 /* Output key name */
3396 memcpy(p, key_name, 16);
3399 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3400 p += EVP_CIPHER_CTX_iv_length(&ctx);
3401 /* Encrypt session data */
3402 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3405 if (!EVP_EncryptFinal(&ctx, p, &len))
3409 if (!HMAC_Update(&hctx, macstart, p - macstart))
3411 if (!HMAC_Final(&hctx, p, &hlen))
3414 EVP_CIPHER_CTX_cleanup(&ctx);
3415 HMAC_CTX_cleanup(&hctx);
3418 /* Now write out lengths: p points to end of data written */
3420 len = p - ssl_handshake_start(s);
3421 /* Skip ticket lifetime hint */
3422 p = ssl_handshake_start(s) + 4;
3424 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3426 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3430 /* SSL3_ST_SW_SESSION_TICKET_B */
3431 return ssl_do_write(s);
3434 EVP_CIPHER_CTX_cleanup(&ctx);
3435 HMAC_CTX_cleanup(&hctx);
3436 s->state = SSL_ST_ERR;
3440 int ssl3_send_cert_status(SSL *s)
3442 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3445 * Grow buffer if need be: the length calculation is as
3446 * follows 1 (message type) + 3 (message length) +
3447 * 1 (ocsp response type) + 3 (ocsp response length)
3450 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3451 s->state = SSL_ST_ERR;
3455 p = (unsigned char *)s->init_buf->data;
3458 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3459 /* message length */
3460 l2n3(s->tlsext_ocsp_resplen + 4, p);
3462 *(p++) = s->tlsext_status_type;
3463 /* length of OCSP response */
3464 l2n3(s->tlsext_ocsp_resplen, p);
3465 /* actual response */
3466 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3467 /* number of bytes to write */
3468 s->init_num = 8 + s->tlsext_ocsp_resplen;
3469 s->state = SSL3_ST_SW_CERT_STATUS_B;
3473 /* SSL3_ST_SW_CERT_STATUS_B */
3474 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3477 # ifndef OPENSSL_NO_NEXTPROTONEG
3479 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3480 * It sets the next_proto member in s if found
3482 int ssl3_get_next_proto(SSL *s)
3485 int proto_len, padding_len;
3487 const unsigned char *p;
3490 * Clients cannot send a NextProtocol message if we didn't see the
3491 * extension in their ClientHello
3493 if (!s->s3->next_proto_neg_seen) {
3494 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3495 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3496 s->state = SSL_ST_ERR;
3500 /* See the payload format below */
3501 n = s->method->ssl_get_message(s,
3502 SSL3_ST_SR_NEXT_PROTO_A,
3503 SSL3_ST_SR_NEXT_PROTO_B,
3504 SSL3_MT_NEXT_PROTO, 514, &ok);
3510 * s->state doesn't reflect whether ChangeCipherSpec has been received in
3511 * this handshake, but s->s3->change_cipher_spec does (will be reset by
3512 * ssl3_get_finished).
3514 if (!s->s3->change_cipher_spec) {
3515 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3516 s->state = SSL_ST_ERR;
3521 s->state = SSL_ST_ERR;
3522 return 0; /* The body must be > 1 bytes long */
3525 p = (unsigned char *)s->init_msg;
3528 * The payload looks like:
3530 * uint8 proto[proto_len];
3531 * uint8 padding_len;
3532 * uint8 padding[padding_len];
3535 if (proto_len + 2 > s->init_num) {
3536 s->state = SSL_ST_ERR;
3539 padding_len = p[proto_len + 1];
3540 if (proto_len + padding_len + 2 != s->init_num) {
3541 s->state = SSL_ST_ERR;
3545 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3546 if (!s->next_proto_negotiated) {
3547 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3548 s->state = SSL_ST_ERR;
3551 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3552 s->next_proto_negotiated_len = proto_len;