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->ctx->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, cipherlen, complen;
842 unsigned int cookie_len = 0;
846 #ifndef OPENSSL_NO_COMP
847 unsigned char *q = NULL;
848 SSL_COMP *comp = NULL;
850 STACK_OF(SSL_CIPHER) *ciphers = NULL;
853 unsigned char *sess, *cdata;
855 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
859 * We do this so that we will respond with our native type. If we are
860 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
861 * switching should be handled by a different method. If we are SSLv3, we
862 * will respond with SSLv3, even if prompted with TLSv1.
864 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
865 s->state = SSL3_ST_SR_CLNT_HELLO_B;
868 n = s->method->ssl_get_message(s,
869 SSL3_ST_SR_CLNT_HELLO_B,
870 SSL3_ST_SR_CLNT_HELLO_C,
871 SSL3_MT_CLIENT_HELLO,
872 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
877 PACKET_buf_init(&pkt, s->init_msg, n);
879 /* First lets get s->client_version set correctly */
880 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
881 unsigned int version;
884 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
885 * header is sent directly on the wire, not wrapped as a TLS
886 * record. Our record layer just processes the message length and passes
887 * the rest right through. Its format is:
889 * 0-1 msg_length - decoded by the record layer
890 * 2 msg_type - s->init_msg points here
892 * 5-6 cipher_spec_length
893 * 7-8 session_id_length
894 * 9-10 challenge_length
898 if (!PACKET_get_1(&pkt, &mt)
899 || mt != SSL2_MT_CLIENT_HELLO) {
901 * Should never happen. We should have tested this in the record
902 * layer in order to have determined that this is a SSLv2 record
905 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
909 if (!PACKET_get_net_2(&pkt, &version)) {
910 /* No protocol version supplied! */
911 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
914 if (version == 0x0002) {
915 /* This is real SSLv2. We don't support it. */
916 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
918 } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {
920 s->client_version = version;
922 /* No idea what protocol this is */
923 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
928 * use version from inside client hello, not from record header (may
929 * differ: see RFC 2246, Appendix E, second paragraph)
931 if(!PACKET_get_net_2(&pkt, (unsigned int *)&s->client_version)) {
932 al = SSL_AD_DECODE_ERROR;
933 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
938 /* Do SSL/TLS version negotiation if applicable */
939 if (!SSL_IS_DTLS(s)) {
940 if (s->version != TLS_ANY_VERSION) {
941 if (s->client_version >= s->version) {
944 } else if (s->client_version >= SSL3_VERSION) {
945 switch(s->client_version) {
948 if(!(s->options & SSL_OP_NO_TLSv1_2)) {
949 s->version = TLS1_2_VERSION;
950 s->method = TLSv1_2_server_method();
954 /* Deliberately fall through */
956 if(!(s->options & SSL_OP_NO_TLSv1_1)) {
957 s->version = TLS1_1_VERSION;
958 s->method = TLSv1_1_server_method();
962 /* Deliberately fall through */
964 if(!(s->options & SSL_OP_NO_TLSv1)) {
965 s->version = TLS1_VERSION;
966 s->method = TLSv1_server_method();
970 /* Deliberately fall through */
972 #ifndef OPENSSL_NO_SSL3
973 if(!(s->options & SSL_OP_NO_SSLv3)) {
974 s->version = SSL3_VERSION;
975 s->method = SSLv3_server_method();
984 } else if (s->client_version <= s->version
985 || s->method->version == DTLS_ANY_VERSION) {
987 * For DTLS we just check versions are potentially compatible. Version
988 * negotiation comes later.
994 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
995 if ((!s->enc_write_ctx && !s->write_hash)) {
997 * similar to ssl3_get_record, send alert using remote version
1000 s->version = s->client_version;
1002 al = SSL_AD_PROTOCOL_VERSION;
1006 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1008 * Handle an SSLv2 backwards compatible ClientHello
1009 * Note, this is only for SSLv3+ using the backward compatible format.
1010 * Real SSLv2 is not supported, and is rejected above.
1012 unsigned int csl, sil, cl;
1014 if (!PACKET_get_net_2(&pkt, &csl)
1015 || !PACKET_get_net_2(&pkt, &sil)
1016 || !PACKET_get_net_2(&pkt, &cl)) {
1020 /* we need at least one cipher */
1021 al = SSL_AD_ILLEGAL_PARAMETER;
1022 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1026 if (!PACKET_get_bytes(&pkt, &cdata, csl)) {
1027 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1028 al = SSL_AD_DECODE_ERROR;
1032 if (ssl_bytes_to_cipher_list(s, cdata, csl, &(ciphers), 1) == NULL) {
1037 * Ignore any session id. We don't allow resumption in a backwards
1038 * compatible ClientHello
1040 if (!PACKET_forward(&pkt, sil)) {
1041 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1042 al = SSL_AD_DECODE_ERROR;
1047 if (!ssl_get_new_session(s, 1))
1050 /* Load the client random */
1051 i = (cl > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : cl;
1052 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1053 if (!PACKET_peek_copy_bytes(&pkt, s->s3->client_random, i)
1054 || !PACKET_forward(&pkt, cl)
1055 || !PACKET_remaining(&pkt) == 0) {
1056 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1057 al = SSL_AD_DECODE_ERROR;
1061 /* No compression, so set complen to 0 */
1064 /* If we get here we've got SSLv3+ in an SSLv3+ record */
1066 /* load the client random and get the session-id */
1067 if (!PACKET_copy_bytes(&pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
1068 || !PACKET_get_1(&pkt, &j)
1069 || !PACKET_get_bytes(&pkt, &sess, j)) {
1070 al = SSL_AD_DECODE_ERROR;
1071 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1076 * If we require cookies and this ClientHello doesn't contain one, just
1077 * return since we do not want to allocate any memory yet. So check
1080 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1082 if (!PACKET_peek_1(&pkt, &cookie_len)) {
1083 al = SSL_AD_DECODE_ERROR;
1084 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1088 if (cookie_len == 0)
1094 * Versions before 0.9.7 always allow clients to resume sessions in
1095 * renegotiation. 0.9.7 and later allow this by default, but optionally
1096 * ignore resumption requests with flag
1097 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1098 * than a change to default behavior so that applications relying on
1099 * this for security won't even compile against older library versions).
1100 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1101 * request renegotiation but not a new session (s->new_session remains
1102 * unset): for servers, this essentially just means that the
1103 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1107 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1108 if (!ssl_get_new_session(s, 1))
1111 i = ssl_get_prev_session(s, &pkt, sess, j);
1113 * Only resume if the session's version matches the negotiated
1115 * RFC 5246 does not provide much useful advice on resumption
1116 * with a different protocol version. It doesn't forbid it but
1117 * the sanity of such behaviour would be questionable.
1118 * In practice, clients do not accept a version mismatch and
1119 * will abort the handshake with an error.
1121 if (i == 1 && s->version == s->session->ssl_version) {
1122 /* previous session */
1128 if (!ssl_get_new_session(s, 1))
1133 if (SSL_IS_DTLS(s)) {
1134 if (!PACKET_get_1(&pkt, &cookie_len)) {
1135 al = SSL_AD_DECODE_ERROR;
1136 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1140 * The ClientHello may contain a cookie even if the
1141 * HelloVerify message has not been sent--make sure that it
1142 * does not cause an overflow.
1144 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1146 al = SSL_AD_DECODE_ERROR;
1147 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1151 /* verify the cookie if appropriate option is set. */
1152 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
1153 && cookie_len > 0) {
1155 if (!PACKET_copy_bytes(&pkt, s->d1->rcvd_cookie,
1157 al = SSL_AD_DECODE_ERROR;
1158 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1162 if (s->ctx->app_verify_cookie_cb != NULL) {
1163 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1165 al = SSL_AD_HANDSHAKE_FAILURE;
1166 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1167 SSL_R_COOKIE_MISMATCH);
1170 /* else cookie verification succeeded */
1172 /* default verification */
1173 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1174 s->d1->cookie_len) != 0) {
1175 al = SSL_AD_HANDSHAKE_FAILURE;
1176 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1179 /* Set to -2 so if successful we return 2 */
1182 /* Skip over cookie */
1183 if (!PACKET_forward(&pkt, cookie_len)) {
1184 al = SSL_AD_DECODE_ERROR;
1185 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1190 if (s->method->version == DTLS_ANY_VERSION) {
1191 /* Select version to use */
1192 if (s->client_version <= DTLS1_2_VERSION &&
1193 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1194 s->version = DTLS1_2_VERSION;
1195 s->method = DTLSv1_2_server_method();
1196 } else if (tls1_suiteb(s)) {
1197 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1198 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1199 s->version = s->client_version;
1200 al = SSL_AD_PROTOCOL_VERSION;
1202 } else if (s->client_version <= DTLS1_VERSION &&
1203 !(s->options & SSL_OP_NO_DTLSv1)) {
1204 s->version = DTLS1_VERSION;
1205 s->method = DTLSv1_server_method();
1207 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1208 SSL_R_WRONG_VERSION_NUMBER);
1209 s->version = s->client_version;
1210 al = SSL_AD_PROTOCOL_VERSION;
1213 s->session->ssl_version = s->version;
1217 if (!PACKET_get_net_2(&pkt, &cipherlen)) {
1218 al = SSL_AD_DECODE_ERROR;
1219 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1223 if (cipherlen == 0) {
1224 al = SSL_AD_ILLEGAL_PARAMETER;
1225 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1229 if (!PACKET_get_bytes(&pkt, &cdata, cipherlen)) {
1230 /* not enough data */
1231 al = SSL_AD_DECODE_ERROR;
1232 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1236 if (ssl_bytes_to_cipher_list(s, cdata, cipherlen, &(ciphers), 0) == NULL) {
1240 /* If it is a hit, check that the cipher is in the list */
1243 id = s->session->cipher->id;
1246 fprintf(stderr, "client sent %d ciphers\n",
1247 sk_SSL_CIPHER_num(ciphers));
1249 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1250 c = sk_SSL_CIPHER_value(ciphers, i);
1252 fprintf(stderr, "client [%2d of %2d]:%s\n",
1253 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1261 * Disabled because it can be used in a ciphersuite downgrade
1266 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1267 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1269 * Special case as client bug workaround: the previously used
1270 * cipher may not be in the current list, the client instead
1271 * might be trying to continue using a cipher that before wasn't
1272 * chosen due to server preferences. We'll have to reject the
1273 * connection if the cipher is not enabled, though.
1275 c = sk_SSL_CIPHER_value(ciphers, 0);
1276 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1277 s->session->cipher = c;
1284 * we need to have the cipher in the cipher list if we are asked
1287 al = SSL_AD_ILLEGAL_PARAMETER;
1288 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1289 SSL_R_REQUIRED_CIPHER_MISSING);
1295 if (!PACKET_get_1(&pkt, &complen)
1296 || !PACKET_get_bytes(&pkt, &cdata, complen)) {
1297 /* not enough data */
1298 al = SSL_AD_DECODE_ERROR;
1299 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1303 #ifndef OPENSSL_NO_COMP
1306 for (j = 0; j < complen; j++) {
1313 al = SSL_AD_DECODE_ERROR;
1314 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1319 /* TLS extensions */
1320 if (s->version >= SSL3_VERSION) {
1321 if (!ssl_parse_clienthello_tlsext(s, &pkt)) {
1322 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1328 * Check if we want to use external pre-shared secret for this handshake
1329 * for not reused session only. We need to generate server_random before
1330 * calling tls_session_secret_cb in order to allow SessionTicket
1331 * processing to use it in key derivation.
1335 pos = s->s3->server_random;
1336 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1341 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1342 SSL_CIPHER *pref_cipher = NULL;
1344 s->session->master_key_length = sizeof(s->session->master_key);
1345 if (s->tls_session_secret_cb(s, s->session->master_key,
1346 &s->session->master_key_length, ciphers,
1348 s->tls_session_secret_cb_arg)) {
1350 s->session->ciphers = ciphers;
1351 s->session->verify_result = X509_V_OK;
1355 /* check if some cipher was preferred by call back */
1357 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1362 if (pref_cipher == NULL) {
1363 al = SSL_AD_HANDSHAKE_FAILURE;
1364 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1368 s->session->cipher = pref_cipher;
1369 sk_SSL_CIPHER_free(s->cipher_list);
1370 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1371 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1372 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1377 * Worst case, we will use the NULL compression, but if we have other
1378 * options, we will now look for them. We have complen-1 compression
1379 * algorithms from the client, starting at q.
1381 s->s3->tmp.new_compression = NULL;
1382 #ifndef OPENSSL_NO_COMP
1383 /* This only happens if we have a cache hit */
1384 if (s->session->compress_meth != 0) {
1385 int m, comp_id = s->session->compress_meth;
1387 /* Perform sanity checks on resumed compression algorithm */
1388 /* Can't disable compression */
1389 if (!ssl_allow_compression(s)) {
1390 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1391 SSL_R_INCONSISTENT_COMPRESSION);
1394 /* Look for resumed compression method */
1395 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1396 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1397 if (comp_id == comp->id) {
1398 s->s3->tmp.new_compression = comp;
1402 if (s->s3->tmp.new_compression == NULL) {
1403 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1404 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1407 /* Look for resumed method in compression list */
1408 for (k = 0; k < complen; k++) {
1409 if (q[k] == comp_id)
1413 al = SSL_AD_ILLEGAL_PARAMETER;
1414 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1415 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1420 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1421 /* See if we have a match */
1422 int m, nn, v, done = 0;
1425 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1426 for (m = 0; m < nn; m++) {
1427 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1429 for (o = 0; o < complen; o++) {
1439 s->s3->tmp.new_compression = comp;
1445 * If compression is disabled we'd better not try to resume a session
1446 * using compression.
1448 if (s->session->compress_meth != 0) {
1449 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1455 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1459 #ifdef OPENSSL_NO_COMP
1460 s->session->compress_meth = 0;
1462 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1464 sk_SSL_CIPHER_free(s->session->ciphers);
1465 s->session->ciphers = ciphers;
1466 if (ciphers == NULL) {
1467 al = SSL_AD_INTERNAL_ERROR;
1468 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1472 if (!tls1_set_server_sigalgs(s)) {
1473 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1476 /* Let cert callback update server certificates if required */
1478 if (s->cert->cert_cb) {
1479 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1481 al = SSL_AD_INTERNAL_ERROR;
1482 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1486 s->rwstate = SSL_X509_LOOKUP;
1489 s->rwstate = SSL_NOTHING;
1491 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1494 al = SSL_AD_HANDSHAKE_FAILURE;
1495 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1498 s->s3->tmp.new_cipher = c;
1499 /* check whether we should disable session resumption */
1500 if (s->not_resumable_session_cb != NULL)
1501 s->session->not_resumable = s->not_resumable_session_cb(s,
1502 ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE))
1504 if (s->session->not_resumable)
1505 /* do not send a session ticket */
1506 s->tlsext_ticket_expected = 0;
1508 /* Session-id reuse */
1509 s->s3->tmp.new_cipher = s->session->cipher;
1512 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1513 if (!ssl3_digest_cached_records(s, 0))
1518 * we now have the following setup.
1520 * cipher_list - our prefered list of ciphers
1521 * ciphers - the clients prefered list of ciphers
1522 * compression - basically ignored right now
1523 * ssl version is set - sslv3
1524 * s->session - The ssl session has been setup.
1525 * s->hit - session reuse flag
1526 * s->s3->tmp.new_cipher- the new cipher to use.
1529 /* Handles TLS extensions that we couldn't check earlier */
1530 if (s->version >= SSL3_VERSION) {
1531 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1532 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1541 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1543 s->state = SSL_ST_ERR;
1546 sk_SSL_CIPHER_free(ciphers);
1547 return ret < 0 ? -1 : ret;
1550 int ssl3_send_server_hello(SSL *s)
1553 unsigned char *p, *d;
1558 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1559 buf = (unsigned char *)s->init_buf->data;
1561 /* Do the message type and length last */
1562 d = p = ssl_handshake_start(s);
1564 *(p++) = s->version >> 8;
1565 *(p++) = s->version & 0xff;
1568 * Random stuff. Filling of the server_random takes place in
1569 * ssl3_get_client_hello()
1571 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1572 p += SSL3_RANDOM_SIZE;
1575 * There are several cases for the session ID to send
1576 * back in the server hello:
1577 * - For session reuse from the session cache,
1578 * we send back the old session ID.
1579 * - If stateless session reuse (using a session ticket)
1580 * is successful, we send back the client's "session ID"
1581 * (which doesn't actually identify the session).
1582 * - If it is a new session, we send back the new
1584 * - However, if we want the new session to be single-use,
1585 * we send back a 0-length session ID.
1586 * s->hit is non-zero in either case of session reuse,
1587 * so the following won't overwrite an ID that we're supposed
1590 if (s->session->not_resumable ||
1591 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1593 s->session->session_id_length = 0;
1595 sl = s->session->session_id_length;
1596 if (sl > (int)sizeof(s->session->session_id)) {
1597 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1598 s->state = SSL_ST_ERR;
1602 memcpy(p, s->session->session_id, sl);
1605 /* put the cipher */
1606 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1609 /* put the compression method */
1610 #ifdef OPENSSL_NO_COMP
1613 if (s->s3->tmp.new_compression == NULL)
1616 *(p++) = s->s3->tmp.new_compression->id;
1619 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1620 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1621 s->state = SSL_ST_ERR;
1625 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1627 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1628 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1629 s->state = SSL_ST_ERR;
1635 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1636 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1639 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1642 /* SSL3_ST_SW_SRVR_HELLO_B */
1643 return ssl_do_write(s);
1646 int ssl3_send_server_done(SSL *s)
1649 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1650 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1651 SSLerr(SSL_F_SSL3_SEND_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1654 s->state = SSL3_ST_SW_SRVR_DONE_B;
1657 /* SSL3_ST_SW_SRVR_DONE_B */
1658 return ssl_do_write(s);
1661 int ssl3_send_server_key_exchange(SSL *s)
1663 #ifndef OPENSSL_NO_RSA
1667 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1670 #ifndef OPENSSL_NO_DH
1671 DH *dh = NULL, *dhp;
1673 #ifndef OPENSSL_NO_EC
1674 EC_KEY *ecdh = NULL, *ecdhp;
1675 unsigned char *encodedPoint = NULL;
1678 BN_CTX *bn_ctx = NULL;
1681 const EVP_MD *md = NULL;
1682 unsigned char *p, *d;
1692 EVP_MD_CTX_init(&md_ctx);
1693 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1694 type = s->s3->tmp.new_cipher->algorithm_mkey;
1699 r[0] = r[1] = r[2] = r[3] = NULL;
1701 #ifndef OPENSSL_NO_PSK
1702 if (type & SSL_PSK) {
1704 * reserve size for record length and PSK identity hint
1707 if (s->ctx->psk_identity_hint)
1708 n += strlen(s->ctx->psk_identity_hint);
1710 /* Plain PSK or RSAPSK nothing to do */
1711 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
1713 #endif /* !OPENSSL_NO_PSK */
1714 #ifndef OPENSSL_NO_RSA
1715 if (type & SSL_kRSA) {
1716 rsa = cert->rsa_tmp;
1717 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1718 rsa = s->cert->rsa_tmp_cb(s,
1719 SSL_C_IS_EXPORT(s->s3->
1721 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1724 al = SSL_AD_HANDSHAKE_FAILURE;
1725 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1726 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1730 cert->rsa_tmp = rsa;
1733 al = SSL_AD_HANDSHAKE_FAILURE;
1734 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1735 SSL_R_MISSING_TMP_RSA_KEY);
1740 s->s3->tmp.use_rsa_tmp = 1;
1743 #ifndef OPENSSL_NO_DH
1744 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
1745 if (s->cert->dh_tmp_auto) {
1746 dhp = ssl_get_auto_dh(s);
1748 al = SSL_AD_INTERNAL_ERROR;
1749 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1750 ERR_R_INTERNAL_ERROR);
1755 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1756 dhp = s->cert->dh_tmp_cb(s,
1757 SSL_C_IS_EXPORT(s->s3->
1759 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1762 al = SSL_AD_HANDSHAKE_FAILURE;
1763 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1764 SSL_R_MISSING_TMP_DH_KEY);
1767 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1768 DH_security_bits(dhp), 0, dhp)) {
1769 al = SSL_AD_HANDSHAKE_FAILURE;
1770 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1771 SSL_R_DH_KEY_TOO_SMALL);
1774 if (s->s3->tmp.dh != NULL) {
1775 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1776 ERR_R_INTERNAL_ERROR);
1780 if (s->cert->dh_tmp_auto)
1782 else if ((dh = DHparams_dup(dhp)) == NULL) {
1783 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1788 if ((dhp->pub_key == NULL ||
1789 dhp->priv_key == NULL ||
1790 (s->options & SSL_OP_SINGLE_DH_USE))) {
1791 if (!DH_generate_key(dh)) {
1792 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1796 dh->pub_key = BN_dup(dhp->pub_key);
1797 dh->priv_key = BN_dup(dhp->priv_key);
1798 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1799 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1808 #ifndef OPENSSL_NO_EC
1809 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1810 const EC_GROUP *group;
1812 ecdhp = cert->ecdh_tmp;
1813 if (s->cert->ecdh_tmp_auto) {
1814 /* Get NID of appropriate shared curve */
1815 int nid = tls1_shared_curve(s, -2);
1816 if (nid != NID_undef)
1817 ecdhp = EC_KEY_new_by_curve_name(nid);
1818 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1819 ecdhp = s->cert->ecdh_tmp_cb(s,
1820 SSL_C_IS_EXPORT(s->s3->
1822 SSL_C_EXPORT_PKEYLENGTH(s->
1823 s3->tmp.new_cipher));
1825 if (ecdhp == NULL) {
1826 al = SSL_AD_HANDSHAKE_FAILURE;
1827 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1828 SSL_R_MISSING_TMP_ECDH_KEY);
1832 if (s->s3->tmp.ecdh != NULL) {
1833 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1834 ERR_R_INTERNAL_ERROR);
1838 /* Duplicate the ECDH structure. */
1839 if (ecdhp == NULL) {
1840 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1843 if (s->cert->ecdh_tmp_auto)
1845 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1846 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1850 s->s3->tmp.ecdh = ecdh;
1851 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1852 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1853 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1854 if (!EC_KEY_generate_key(ecdh)) {
1855 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1861 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1862 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1863 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1864 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1868 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1869 (EC_GROUP_get_degree(group) > 163)) {
1870 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1871 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1876 * XXX: For now, we only support ephemeral ECDH keys over named
1877 * (not generic) curves. For supported named curves, curve_id is
1881 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1883 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1884 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1889 * Encode the public key. First check the size of encoding and
1890 * allocate memory accordingly.
1892 encodedlen = EC_POINT_point2oct(group,
1893 EC_KEY_get0_public_key(ecdh),
1894 POINT_CONVERSION_UNCOMPRESSED,
1897 encodedPoint = (unsigned char *)
1898 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1899 bn_ctx = BN_CTX_new();
1900 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1901 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1902 ERR_R_MALLOC_FAILURE);
1906 encodedlen = EC_POINT_point2oct(group,
1907 EC_KEY_get0_public_key(ecdh),
1908 POINT_CONVERSION_UNCOMPRESSED,
1909 encodedPoint, encodedlen, bn_ctx);
1911 if (encodedlen == 0) {
1912 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1916 BN_CTX_free(bn_ctx);
1920 * XXX: For now, we only support named (not generic) curves in
1921 * ECDH ephemeral key exchanges. In this situation, we need four
1922 * additional bytes to encode the entire ServerECDHParams
1925 n += 4 + encodedlen;
1928 * We'll generate the serverKeyExchange message explicitly so we
1929 * can set these to NULLs
1936 #endif /* !OPENSSL_NO_EC */
1937 #ifndef OPENSSL_NO_SRP
1938 if (type & SSL_kSRP) {
1939 if ((s->srp_ctx.N == NULL) ||
1940 (s->srp_ctx.g == NULL) ||
1941 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1942 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1943 SSL_R_MISSING_SRP_PARAM);
1946 r[0] = s->srp_ctx.N;
1947 r[1] = s->srp_ctx.g;
1948 r[2] = s->srp_ctx.s;
1949 r[3] = s->srp_ctx.B;
1953 al = SSL_AD_HANDSHAKE_FAILURE;
1954 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1955 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1958 for (i = 0; i < 4 && r[i] != NULL; i++) {
1959 nr[i] = BN_num_bytes(r[i]);
1960 #ifndef OPENSSL_NO_SRP
1961 if ((i == 2) && (type & SSL_kSRP))
1968 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP))
1969 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
1970 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1972 al = SSL_AD_DECODE_ERROR;
1975 kn = EVP_PKEY_size(pkey);
1981 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1982 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1985 d = p = ssl_handshake_start(s);
1987 #ifndef OPENSSL_NO_PSK
1988 if (type & SSL_PSK) {
1989 /* copy PSK identity hint */
1990 if (s->ctx->psk_identity_hint) {
1991 s2n(strlen(s->ctx->psk_identity_hint), p);
1992 strncpy((char *)p, s->ctx->psk_identity_hint,
1993 strlen(s->ctx->psk_identity_hint));
1994 p += strlen(s->ctx->psk_identity_hint);
2001 for (i = 0; i < 4 && r[i] != NULL; i++) {
2002 #ifndef OPENSSL_NO_SRP
2003 if ((i == 2) && (type & SSL_kSRP)) {
2013 #ifndef OPENSSL_NO_EC
2014 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2016 * XXX: For now, we only support named (not generic) curves. In
2017 * this situation, the serverKeyExchange message has: [1 byte
2018 * CurveType], [2 byte CurveName] [1 byte length of encoded
2019 * point], followed by the actual encoded point itself
2021 *p = NAMED_CURVE_TYPE;
2029 memcpy(p, encodedPoint, encodedlen);
2030 OPENSSL_free(encodedPoint);
2031 encodedPoint = NULL;
2039 * n is the length of the params, they start at &(d[4]) and p
2040 * points to the space at the end.
2042 #ifndef OPENSSL_NO_RSA
2043 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
2046 for (num = 2; num > 0; num--) {
2047 EVP_MD_CTX_set_flags(&md_ctx,
2048 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2049 EVP_DigestInit_ex(&md_ctx, (num == 2)
2050 ? s->ctx->md5 : s->ctx->sha1, NULL);
2051 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2053 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2055 EVP_DigestUpdate(&md_ctx, d, n);
2056 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
2060 if (RSA_sign(NID_md5_sha1, md_buf, j,
2061 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2062 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2070 /* send signature algorithm */
2071 if (SSL_USE_SIGALGS(s)) {
2072 if (!tls12_get_sigandhash(p, pkey, md)) {
2073 /* Should never happen */
2074 al = SSL_AD_INTERNAL_ERROR;
2075 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2076 ERR_R_INTERNAL_ERROR);
2082 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2084 EVP_SignInit_ex(&md_ctx, md, NULL);
2085 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2087 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2089 EVP_SignUpdate(&md_ctx, d, n);
2090 if (!EVP_SignFinal(&md_ctx, &(p[2]),
2091 (unsigned int *)&i, pkey)) {
2092 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2097 if (SSL_USE_SIGALGS(s))
2100 /* Is this error check actually needed? */
2101 al = SSL_AD_HANDSHAKE_FAILURE;
2102 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2103 SSL_R_UNKNOWN_PKEY_TYPE);
2108 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
2109 al = SSL_AD_HANDSHAKE_FAILURE;
2110 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2115 s->state = SSL3_ST_SW_KEY_EXCH_B;
2116 EVP_MD_CTX_cleanup(&md_ctx);
2117 return ssl_do_write(s);
2119 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2121 #ifndef OPENSSL_NO_EC
2122 OPENSSL_free(encodedPoint);
2123 BN_CTX_free(bn_ctx);
2125 EVP_MD_CTX_cleanup(&md_ctx);
2126 s->state = SSL_ST_ERR;
2130 int ssl3_send_certificate_request(SSL *s)
2132 unsigned char *p, *d;
2133 int i, j, nl, off, n;
2134 STACK_OF(X509_NAME) *sk = NULL;
2138 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2141 d = p = ssl_handshake_start(s);
2143 /* get the list of acceptable cert types */
2145 n = ssl3_get_req_cert_type(s, p);
2150 if (SSL_USE_SIGALGS(s)) {
2151 const unsigned char *psigs;
2152 unsigned char *etmp = p;
2153 nl = tls12_get_psigalgs(s, &psigs);
2154 /* Skip over length for now */
2156 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2157 /* Now fill in length */
2167 sk = SSL_get_client_CA_list(s);
2170 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2171 name = sk_X509_NAME_value(sk, i);
2172 j = i2d_X509_NAME(name, NULL);
2173 if (!BUF_MEM_grow_clean
2174 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2175 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2179 p = ssl_handshake_start(s) + n;
2181 i2d_X509_NAME(name, &p);
2186 /* else no CA names */
2187 p = ssl_handshake_start(s) + off;
2190 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2191 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2195 s->state = SSL3_ST_SW_CERT_REQ_B;
2198 /* SSL3_ST_SW_CERT_REQ_B */
2199 return ssl_do_write(s);
2201 s->state = SSL_ST_ERR;
2205 int ssl3_get_client_key_exchange(SSL *s)
2209 unsigned long alg_k;
2211 #ifndef OPENSSL_NO_RSA
2213 EVP_PKEY *pkey = NULL;
2215 #ifndef OPENSSL_NO_DH
2217 DH *dh_srvr, *dh_clnt = NULL;
2219 #ifndef OPENSSL_NO_EC
2220 EC_KEY *srvr_ecdh = NULL;
2221 EVP_PKEY *clnt_pub_pkey = NULL;
2222 EC_POINT *clnt_ecpoint = NULL;
2223 BN_CTX *bn_ctx = NULL;
2226 n = s->method->ssl_get_message(s,
2227 SSL3_ST_SR_KEY_EXCH_A,
2228 SSL3_ST_SR_KEY_EXCH_B,
2229 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2233 p = (unsigned char *)s->init_msg;
2235 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2237 #ifndef OPENSSL_NO_PSK
2238 /* For PSK parse and retrieve identity, obtain PSK key */
2239 if (alg_k & SSL_PSK) {
2240 unsigned char psk[PSK_MAX_PSK_LEN];
2243 al = SSL_AD_DECODE_ERROR;
2244 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2249 al = SSL_AD_DECODE_ERROR;
2250 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2253 if (i > PSK_MAX_IDENTITY_LEN) {
2254 al = SSL_AD_DECODE_ERROR;
2255 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2256 SSL_R_DATA_LENGTH_TOO_LONG);
2259 if (s->psk_server_callback == NULL) {
2260 al = SSL_AD_INTERNAL_ERROR;
2261 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2262 SSL_R_PSK_NO_SERVER_CB);
2266 OPENSSL_free(s->session->psk_identity);
2267 s->session->psk_identity = BUF_strndup((char *)p, i);
2269 if (s->session->psk_identity == NULL) {
2270 al = SSL_AD_INTERNAL_ERROR;
2271 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2272 ERR_R_MALLOC_FAILURE);
2276 psklen = s->psk_server_callback(s, s->session->psk_identity,
2279 if (psklen > PSK_MAX_PSK_LEN) {
2280 al = SSL_AD_INTERNAL_ERROR;
2281 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2283 } else if (psklen == 0) {
2285 * PSK related to the given identity not found
2287 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2288 SSL_R_PSK_IDENTITY_NOT_FOUND);
2289 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2293 OPENSSL_free(s->s3->tmp.psk);
2294 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2295 OPENSSL_cleanse(psk, psklen);
2297 if (s->s3->tmp.psk == NULL) {
2298 al = SSL_AD_INTERNAL_ERROR;
2299 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2303 s->s3->tmp.psklen = psklen;
2308 if (alg_k & SSL_kPSK) {
2309 /* Identity extracted earlier: should be nothing left */
2311 al = SSL_AD_HANDSHAKE_FAILURE;
2312 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2315 /* PSK handled by ssl_generate_master_secret */
2316 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2317 al = SSL_AD_INTERNAL_ERROR;
2318 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2323 #ifndef OPENSSL_NO_RSA
2324 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2325 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2327 unsigned char decrypt_good, version_good;
2330 /* FIX THIS UP EAY EAY EAY EAY */
2331 if (s->s3->tmp.use_rsa_tmp) {
2332 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2333 rsa = s->cert->rsa_tmp;
2335 * Don't do a callback because rsa_tmp should be sent already
2338 al = SSL_AD_HANDSHAKE_FAILURE;
2339 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2340 SSL_R_MISSING_TMP_RSA_PKEY);
2345 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2346 if ((pkey == NULL) ||
2347 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2348 al = SSL_AD_HANDSHAKE_FAILURE;
2349 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2350 SSL_R_MISSING_RSA_CERTIFICATE);
2353 rsa = pkey->pkey.rsa;
2356 /* TLS and [incidentally] DTLS{0xFEFF} */
2357 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2360 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2361 al = SSL_AD_DECODE_ERROR;
2362 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2363 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2372 * Reject overly short RSA ciphertext because we want to be sure
2373 * that the buffer size makes it safe to iterate over the entire
2374 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2375 * actual expected size is larger due to RSA padding, but the
2376 * bound is sufficient to be safe.
2378 if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2379 al = SSL_AD_DECRYPT_ERROR;
2380 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2381 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2386 * We must not leak whether a decryption failure occurs because of
2387 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2388 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2389 * generates a random premaster secret for the case that the decrypt
2390 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2393 if (RAND_bytes(rand_premaster_secret,
2394 sizeof(rand_premaster_secret)) <= 0)
2397 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2401 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2402 * be 0xff if so and zero otherwise.
2405 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2408 * If the version in the decrypted pre-master secret is correct then
2409 * version_good will be 0xff, otherwise it'll be zero. The
2410 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2411 * (http://eprint.iacr.org/2003/052/) exploits the version number
2412 * check as a "bad version oracle". Thus version checks are done in
2413 * constant time and are treated like any other decryption error.
2416 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2418 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2421 * The premaster secret must contain the same version number as the
2422 * ClientHello to detect version rollback attacks (strangely, the
2423 * protocol does not offer such protection for DH ciphersuites).
2424 * However, buggy clients exist that send the negotiated protocol
2425 * version instead if the server does not support the requested
2426 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2429 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2430 unsigned char workaround_good;
2432 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2434 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2435 version_good |= workaround_good;
2439 * Both decryption and version must be good for decrypt_good to
2440 * remain non-zero (0xff).
2442 decrypt_good &= version_good;
2445 * Now copy rand_premaster_secret over from p using
2446 * decrypt_good_mask. If decryption failed, then p does not
2447 * contain valid plaintext, however, a check above guarantees
2448 * it is still sufficiently large to read from.
2450 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2451 p[j] = constant_time_select_8(decrypt_good, p[j],
2452 rand_premaster_secret[j]);
2455 if (!ssl_generate_master_secret(s, p, sizeof(rand_premaster_secret), 0)) {
2456 al = SSL_AD_INTERNAL_ERROR;
2457 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2462 #ifndef OPENSSL_NO_DH
2463 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2465 EVP_PKEY *skey = NULL;
2469 if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2470 al = SSL_AD_HANDSHAKE_FAILURE;
2471 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2472 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2477 if (n && n != i + 2) {
2478 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2479 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2480 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2487 if (alg_k & SSL_kDHr)
2488 idx = SSL_PKEY_DH_RSA;
2489 else if (alg_k & SSL_kDHd)
2490 idx = SSL_PKEY_DH_DSA;
2492 skey = s->cert->pkeys[idx].privatekey;
2493 if ((skey == NULL) ||
2494 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2495 al = SSL_AD_HANDSHAKE_FAILURE;
2496 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2497 SSL_R_MISSING_RSA_CERTIFICATE);
2500 dh_srvr = skey->pkey.dh;
2501 } else if (s->s3->tmp.dh == NULL) {
2502 al = SSL_AD_HANDSHAKE_FAILURE;
2503 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2504 SSL_R_MISSING_TMP_DH_KEY);
2507 dh_srvr = s->s3->tmp.dh;
2510 /* Get pubkey from cert */
2511 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2513 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2514 dh_clnt = EVP_PKEY_get1_DH(clkey);
2516 if (dh_clnt == NULL) {
2517 al = SSL_AD_HANDSHAKE_FAILURE;
2518 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2519 SSL_R_MISSING_TMP_DH_KEY);
2522 EVP_PKEY_free(clkey);
2523 pub = dh_clnt->pub_key;
2525 pub = BN_bin2bn(p, i, NULL);
2527 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2531 i = DH_compute_key(p, pub, dh_srvr);
2534 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2539 DH_free(s->s3->tmp.dh);
2540 s->s3->tmp.dh = NULL;
2546 if (!ssl_generate_master_secret(s, p, i, 0)) {
2547 al = SSL_AD_INTERNAL_ERROR;
2548 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2556 #ifndef OPENSSL_NO_EC
2557 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2561 const EC_GROUP *group;
2562 const BIGNUM *priv_key;
2564 /* initialize structures for server's ECDH key pair */
2565 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2566 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2570 /* Let's get server private key and group information */
2571 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2572 /* use the certificate */
2573 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2576 * use the ephermeral values we saved when generating the
2577 * ServerKeyExchange msg.
2579 tkey = s->s3->tmp.ecdh;
2582 group = EC_KEY_get0_group(tkey);
2583 priv_key = EC_KEY_get0_private_key(tkey);
2585 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2586 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2587 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2591 /* Let's get client's public key */
2592 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2593 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2598 /* Client Publickey was in Client Certificate */
2600 if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2601 al = SSL_AD_HANDSHAKE_FAILURE;
2602 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2603 SSL_R_MISSING_TMP_ECDH_KEY);
2606 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2607 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2609 * XXX: For now, we do not support client authentication
2610 * using ECDH certificates so this branch (n == 0L) of the
2611 * code is never executed. When that support is added, we
2612 * ought to ensure the key received in the certificate is
2613 * authorized for key agreement. ECDH_compute_key implicitly
2614 * checks that the two ECDH shares are for the same group.
2616 al = SSL_AD_HANDSHAKE_FAILURE;
2617 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2618 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2622 if (EC_POINT_copy(clnt_ecpoint,
2623 EC_KEY_get0_public_key(clnt_pub_pkey->
2625 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2628 ret = 2; /* Skip certificate verify processing */
2631 * Get client's public key from encoded point in the
2632 * ClientKeyExchange message.
2634 if ((bn_ctx = BN_CTX_new()) == NULL) {
2635 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2636 ERR_R_MALLOC_FAILURE);
2640 /* Get encoded point length */
2644 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2647 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2648 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2652 * p is pointing to somewhere in the buffer currently, so set it
2655 p = (unsigned char *)s->init_buf->data;
2658 /* Compute the shared pre-master secret */
2659 field_size = EC_GROUP_get_degree(group);
2660 if (field_size <= 0) {
2661 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2664 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2667 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2671 EVP_PKEY_free(clnt_pub_pkey);
2672 EC_POINT_free(clnt_ecpoint);
2673 EC_KEY_free(srvr_ecdh);
2674 BN_CTX_free(bn_ctx);
2675 EC_KEY_free(s->s3->tmp.ecdh);
2676 s->s3->tmp.ecdh = NULL;
2678 if (!ssl_generate_master_secret(s, p, i, 0)) {
2679 al = SSL_AD_INTERNAL_ERROR;
2680 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2686 #ifndef OPENSSL_NO_SRP
2687 if (alg_k & SSL_kSRP) {
2692 if (param_len > n) {
2693 al = SSL_AD_DECODE_ERROR;
2694 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2695 SSL_R_BAD_SRP_A_LENGTH);
2698 if ((s->srp_ctx.A = BN_bin2bn(p, i, NULL)) == NULL) {
2699 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2702 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2703 || BN_is_zero(s->srp_ctx.A)) {
2704 al = SSL_AD_ILLEGAL_PARAMETER;
2705 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2706 SSL_R_BAD_SRP_PARAMETERS);
2709 OPENSSL_free(s->session->srp_username);
2710 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2711 if (s->session->srp_username == NULL) {
2712 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2716 if (!srp_generate_server_master_secret(s)) {
2717 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2723 #endif /* OPENSSL_NO_SRP */
2724 if (alg_k & SSL_kGOST) {
2726 EVP_PKEY_CTX *pkey_ctx;
2727 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2728 unsigned char premaster_secret[32], *start;
2729 size_t outlen = 32, inlen;
2730 unsigned long alg_a;
2734 /* Get our certificate private key */
2735 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2736 if (alg_a & SSL_aGOST94)
2737 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2738 else if (alg_a & SSL_aGOST01)
2739 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2741 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2742 EVP_PKEY_decrypt_init(pkey_ctx);
2744 * If client certificate is present and is of the same type, maybe
2745 * use it for key exchange. Don't mind errors from
2746 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2747 * client certificate for authorization only.
2749 client_pub_pkey = X509_get_pubkey(s->session->peer);
2750 if (client_pub_pkey) {
2751 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2754 /* Decrypt session key */
2756 ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2757 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2758 || Tclass != V_ASN1_UNIVERSAL) {
2759 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2760 SSL_R_DECRYPTION_FAILED);
2765 if (EVP_PKEY_decrypt
2766 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2767 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2768 SSL_R_DECRYPTION_FAILED);
2771 /* Generate master secret */
2772 if (!ssl_generate_master_secret(s, premaster_secret,
2773 sizeof(premaster_secret), 0)) {
2774 al = SSL_AD_INTERNAL_ERROR;
2775 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2778 /* Check if pubkey from client certificate was used */
2779 if (EVP_PKEY_CTX_ctrl
2780 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2785 EVP_PKEY_free(client_pub_pkey);
2786 EVP_PKEY_CTX_free(pkey_ctx);
2791 al = SSL_AD_HANDSHAKE_FAILURE;
2792 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2798 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2799 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2802 #ifndef OPENSSL_NO_EC
2803 EVP_PKEY_free(clnt_pub_pkey);
2804 EC_POINT_free(clnt_ecpoint);
2805 EC_KEY_free(srvr_ecdh);
2806 BN_CTX_free(bn_ctx);
2808 #ifndef OPENSSL_NO_PSK
2809 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2810 s->s3->tmp.psk = NULL;
2812 s->state = SSL_ST_ERR;
2816 int ssl3_get_cert_verify(SSL *s)
2818 EVP_PKEY *pkey = NULL;
2820 int al, ok, ret = 0;
2824 const EVP_MD *md = NULL;
2826 EVP_MD_CTX_init(&mctx);
2829 * We should only process a CertificateVerify message if we have received
2830 * a Certificate from the client. If so then |s->session->peer| will be non
2831 * NULL. In some instances a CertificateVerify message is not required even
2832 * if the peer has sent a Certificate (e.g. such as in the case of static
2833 * DH). In that case the ClientKeyExchange processing will skip the
2834 * CertificateVerify state so we should not arrive here.
2836 if (s->session->peer == NULL) {
2841 n = s->method->ssl_get_message(s,
2842 SSL3_ST_SR_CERT_VRFY_A,
2843 SSL3_ST_SR_CERT_VRFY_B,
2844 SSL3_MT_CERTIFICATE_VERIFY,
2845 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2850 peer = s->session->peer;
2851 pkey = X509_get_pubkey(peer);
2852 type = X509_certificate_type(peer, pkey);
2854 if (!(type & EVP_PKT_SIGN)) {
2855 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2856 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2857 al = SSL_AD_ILLEGAL_PARAMETER;
2861 /* we now have a signature that we need to verify */
2862 p = (unsigned char *)s->init_msg;
2863 /* Check for broken implementations of GOST ciphersuites */
2865 * If key is GOST and n is exactly 64, it is bare signature without
2868 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2869 pkey->type == NID_id_GostR3410_2001)) {
2872 if (SSL_USE_SIGALGS(s)) {
2873 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
2875 al = SSL_AD_INTERNAL_ERROR;
2877 } else if (rv == 0) {
2878 al = SSL_AD_DECODE_ERROR;
2882 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2890 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2891 al = SSL_AD_DECODE_ERROR;
2895 j = EVP_PKEY_size(pkey);
2896 if ((i > j) || (n > j) || (n <= 0)) {
2897 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2898 al = SSL_AD_DECODE_ERROR;
2902 if (SSL_USE_SIGALGS(s)) {
2905 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2906 if (hdatalen <= 0) {
2907 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2908 al = SSL_AD_INTERNAL_ERROR;
2912 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
2915 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
2916 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
2917 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
2918 al = SSL_AD_INTERNAL_ERROR;
2922 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
2923 al = SSL_AD_DECRYPT_ERROR;
2924 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
2928 #ifndef OPENSSL_NO_RSA
2929 if (pkey->type == EVP_PKEY_RSA) {
2930 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2931 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2934 al = SSL_AD_DECRYPT_ERROR;
2935 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
2939 al = SSL_AD_DECRYPT_ERROR;
2940 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
2945 #ifndef OPENSSL_NO_DSA
2946 if (pkey->type == EVP_PKEY_DSA) {
2947 j = DSA_verify(pkey->save_type,
2948 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2949 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2952 al = SSL_AD_DECRYPT_ERROR;
2953 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
2958 #ifndef OPENSSL_NO_EC
2959 if (pkey->type == EVP_PKEY_EC) {
2960 j = ECDSA_verify(pkey->save_type,
2961 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2962 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2965 al = SSL_AD_DECRYPT_ERROR;
2966 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2971 if (pkey->type == NID_id_GostR3410_94
2972 || pkey->type == NID_id_GostR3410_2001) {
2973 unsigned char signature[64];
2975 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
2976 EVP_PKEY_verify_init(pctx);
2978 fprintf(stderr, "GOST signature length is %d", i);
2980 for (idx = 0; idx < 64; idx++) {
2981 signature[63 - idx] = p[idx];
2983 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
2985 EVP_PKEY_CTX_free(pctx);
2987 al = SSL_AD_DECRYPT_ERROR;
2988 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2992 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2993 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3000 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3001 s->state = SSL_ST_ERR;
3004 BIO_free(s->s3->handshake_buffer);
3005 s->s3->handshake_buffer = NULL;
3006 EVP_MD_CTX_cleanup(&mctx);
3007 EVP_PKEY_free(pkey);
3011 int ssl3_get_client_certificate(SSL *s)
3013 int i, ok, al, ret = -1;
3015 unsigned long l, nc, llen, n;
3016 const unsigned char *p, *q;
3018 STACK_OF(X509) *sk = NULL;
3020 n = s->method->ssl_get_message(s,
3023 -1, s->max_cert_list, &ok);
3028 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3029 if ((s->verify_mode & SSL_VERIFY_PEER) &&
3030 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3031 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3032 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3033 al = SSL_AD_HANDSHAKE_FAILURE;
3037 * If tls asked for a client cert, the client must return a 0 list
3039 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3040 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3041 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3042 al = SSL_AD_UNEXPECTED_MESSAGE;
3045 s->s3->tmp.reuse_message = 1;
3049 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3050 al = SSL_AD_UNEXPECTED_MESSAGE;
3051 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3054 p = d = (unsigned char *)s->init_msg;
3056 if ((sk = sk_X509_new_null()) == NULL) {
3057 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3062 if (llen + 3 != n) {
3063 al = SSL_AD_DECODE_ERROR;
3064 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3067 for (nc = 0; nc < llen;) {
3069 if ((l + nc + 3) > llen) {
3070 al = SSL_AD_DECODE_ERROR;
3071 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3072 SSL_R_CERT_LENGTH_MISMATCH);
3077 x = d2i_X509(NULL, &p, l);
3079 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3083 al = SSL_AD_DECODE_ERROR;
3084 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3085 SSL_R_CERT_LENGTH_MISMATCH);
3088 if (!sk_X509_push(sk, x)) {
3089 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3096 if (sk_X509_num(sk) <= 0) {
3097 /* TLS does not mind 0 certs returned */
3098 if (s->version == SSL3_VERSION) {
3099 al = SSL_AD_HANDSHAKE_FAILURE;
3100 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3101 SSL_R_NO_CERTIFICATES_RETURNED);
3104 /* Fail for TLS only if we required a certificate */
3105 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3106 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3107 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3108 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3109 al = SSL_AD_HANDSHAKE_FAILURE;
3112 /* No client certificate so digest cached records */
3113 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3114 al = SSL_AD_INTERNAL_ERROR;
3119 i = ssl_verify_cert_chain(s, sk);
3121 al = ssl_verify_alarm_type(s->verify_result);
3122 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3123 SSL_R_CERTIFICATE_VERIFY_FAILED);
3127 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, i);
3128 al = SSL_AD_HANDSHAKE_FAILURE;
3131 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3133 al = SSL3_AD_HANDSHAKE_FAILURE;
3134 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3135 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3138 EVP_PKEY_free(pkey);
3141 X509_free(s->session->peer);
3142 s->session->peer = sk_X509_shift(sk);
3143 s->session->verify_result = s->verify_result;
3145 sk_X509_pop_free(s->session->peer_chain, X509_free);
3146 s->session->peer_chain = sk;
3148 * Inconsistency alert: cert_chain does *not* include the peer's own
3149 * certificate, while we do include it in s3_clnt.c
3156 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3158 s->state = SSL_ST_ERR;
3160 sk_X509_pop_free(sk, X509_free);
3164 int ssl3_send_server_certificate(SSL *s)
3168 if (s->state == SSL3_ST_SW_CERT_A) {
3169 cpk = ssl_get_server_send_pkey(s);
3171 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3172 s->state = SSL_ST_ERR;
3176 if (!ssl3_output_cert_chain(s, cpk)) {
3177 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3178 s->state = SSL_ST_ERR;
3181 s->state = SSL3_ST_SW_CERT_B;
3184 /* SSL3_ST_SW_CERT_B */
3185 return ssl_do_write(s);
3188 /* send a new session ticket (not necessarily for a new session) */
3189 int ssl3_send_newsession_ticket(SSL *s)
3191 unsigned char *senc = NULL;
3195 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3196 unsigned char *p, *macstart;
3197 const unsigned char *const_p;
3198 int len, slen_full, slen;
3201 SSL_CTX *tctx = s->initial_ctx;
3202 unsigned char iv[EVP_MAX_IV_LENGTH];
3203 unsigned char key_name[16];
3205 /* get session encoding length */
3206 slen_full = i2d_SSL_SESSION(s->session, NULL);
3208 * Some length values are 16 bits, so forget it if session is too
3211 if (slen_full == 0 || slen_full > 0xFF00) {
3212 s->state = SSL_ST_ERR;
3215 senc = OPENSSL_malloc(slen_full);
3217 s->state = SSL_ST_ERR;
3221 EVP_CIPHER_CTX_init(&ctx);
3222 HMAC_CTX_init(&hctx);
3225 if (!i2d_SSL_SESSION(s->session, &p))
3229 * create a fresh copy (not shared with other threads) to clean up
3232 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3235 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3237 slen = i2d_SSL_SESSION(sess, NULL);
3238 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3239 SSL_SESSION_free(sess);
3243 if (!i2d_SSL_SESSION(sess, &p)) {
3244 SSL_SESSION_free(sess);
3247 SSL_SESSION_free(sess);
3250 * Grow buffer if need be: the length calculation is as
3251 * follows handshake_header_length +
3252 * 4 (ticket lifetime hint) + 2 (ticket length) +
3253 * 16 (key name) + max_iv_len (iv length) +
3254 * session_length + max_enc_block_size (max encrypted session
3255 * length) + max_md_size (HMAC).
3257 if (!BUF_MEM_grow(s->init_buf,
3258 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3259 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3262 p = ssl_handshake_start(s);
3264 * Initialize HMAC and cipher contexts. If callback present it does
3265 * all the work otherwise use generated values from parent ctx.
3267 if (tctx->tlsext_ticket_key_cb) {
3268 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3272 if (RAND_bytes(iv, 16) <= 0)
3274 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3275 tctx->tlsext_tick_aes_key, iv))
3277 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3278 EVP_sha256(), NULL))
3280 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3284 * Ticket lifetime hint (advisory only): We leave this unspecified
3285 * for resumed session (for simplicity), and guess that tickets for
3286 * new sessions will live as long as their sessions.
3288 l2n(s->hit ? 0 : s->session->timeout, p);
3290 /* Skip ticket length for now */
3292 /* Output key name */
3294 memcpy(p, key_name, 16);
3297 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3298 p += EVP_CIPHER_CTX_iv_length(&ctx);
3299 /* Encrypt session data */
3300 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3303 if (!EVP_EncryptFinal(&ctx, p, &len))
3307 if (!HMAC_Update(&hctx, macstart, p - macstart))
3309 if (!HMAC_Final(&hctx, p, &hlen))
3312 EVP_CIPHER_CTX_cleanup(&ctx);
3313 HMAC_CTX_cleanup(&hctx);
3316 /* Now write out lengths: p points to end of data written */
3318 len = p - ssl_handshake_start(s);
3319 /* Skip ticket lifetime hint */
3320 p = ssl_handshake_start(s) + 4;
3322 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3324 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3328 /* SSL3_ST_SW_SESSION_TICKET_B */
3329 return ssl_do_write(s);
3332 EVP_CIPHER_CTX_cleanup(&ctx);
3333 HMAC_CTX_cleanup(&hctx);
3334 s->state = SSL_ST_ERR;
3338 int ssl3_send_cert_status(SSL *s)
3340 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3343 * Grow buffer if need be: the length calculation is as
3344 * follows 1 (message type) + 3 (message length) +
3345 * 1 (ocsp response type) + 3 (ocsp response length)
3348 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3349 s->state = SSL_ST_ERR;
3353 p = (unsigned char *)s->init_buf->data;
3356 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3357 /* message length */
3358 l2n3(s->tlsext_ocsp_resplen + 4, p);
3360 *(p++) = s->tlsext_status_type;
3361 /* length of OCSP response */
3362 l2n3(s->tlsext_ocsp_resplen, p);
3363 /* actual response */
3364 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3365 /* number of bytes to write */
3366 s->init_num = 8 + s->tlsext_ocsp_resplen;
3367 s->state = SSL3_ST_SW_CERT_STATUS_B;
3371 /* SSL3_ST_SW_CERT_STATUS_B */
3372 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3375 #ifndef OPENSSL_NO_NEXTPROTONEG
3377 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3378 * It sets the next_proto member in s if found
3380 int ssl3_get_next_proto(SSL *s)
3383 int proto_len, padding_len;
3385 const unsigned char *p;
3388 * Clients cannot send a NextProtocol message if we didn't see the
3389 * extension in their ClientHello
3391 if (!s->s3->next_proto_neg_seen) {
3392 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3393 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3394 s->state = SSL_ST_ERR;
3398 /* See the payload format below */
3399 n = s->method->ssl_get_message(s,
3400 SSL3_ST_SR_NEXT_PROTO_A,
3401 SSL3_ST_SR_NEXT_PROTO_B,
3402 SSL3_MT_NEXT_PROTO, 514, &ok);
3408 * s->state doesn't reflect whether ChangeCipherSpec has been received in
3409 * this handshake, but s->s3->change_cipher_spec does (will be reset by
3410 * ssl3_get_finished).
3412 if (!s->s3->change_cipher_spec) {
3413 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3414 s->state = SSL_ST_ERR;
3419 s->state = SSL_ST_ERR;
3420 return 0; /* The body must be > 1 bytes long */
3423 p = (unsigned char *)s->init_msg;
3426 * The payload looks like:
3428 * uint8 proto[proto_len];
3429 * uint8 padding_len;
3430 * uint8 padding[padding_len];
3433 if (proto_len + 2 > s->init_num) {
3434 s->state = SSL_ST_ERR;
3437 padding_len = p[proto_len + 1];
3438 if (proto_len + padding_len + 2 != s->init_num) {
3439 s->state = SSL_ST_ERR;
3443 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3444 if (!s->next_proto_negotiated) {
3445 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3446 s->state = SSL_ST_ERR;
3449 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3450 s->next_proto_negotiated_len = proto_len;
3456 #define SSLV2_CIPHER_LEN 3
3458 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
3460 STACK_OF(SSL_CIPHER) **skp,
3463 const SSL_CIPHER *c;
3464 STACK_OF(SSL_CIPHER) *sk;
3468 s->s3->send_connection_binding = 0;
3471 n = SSLV2_CIPHER_LEN;
3473 n = ssl_put_cipher_by_char(s, NULL, NULL);
3475 if (n == 0 || (num % n) != 0) {
3476 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3477 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3480 if ((skp == NULL) || (*skp == NULL)) {
3481 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3483 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3488 sk_SSL_CIPHER_zero(sk);
3491 OPENSSL_free(s->s3->tmp.ciphers_raw);
3492 s->s3->tmp.ciphers_raw = BUF_memdup(p, num);
3493 if (s->s3->tmp.ciphers_raw == NULL) {
3494 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3497 s->s3->tmp.ciphers_rawlen = (size_t)num;
3499 for (i = 0; i < num; i += n) {
3500 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3501 if (s->s3 && (n != 3 || !p[0]) &&
3502 (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3503 (p[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3504 /* SCSV fatal if renegotiating */
3505 if (s->renegotiate) {
3506 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3507 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3508 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3511 s->s3->send_connection_binding = 1;
3513 #ifdef OPENSSL_RI_DEBUG
3514 fprintf(stderr, "SCSV received by server\n");
3519 /* Check for TLS_FALLBACK_SCSV */
3520 if ((n != 3 || !p[0]) &&
3521 (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3522 (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3524 * The SCSV indicates that the client previously tried a higher
3525 * version. Fail if the current version is an unexpected
3528 if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
3529 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3530 SSL_R_INAPPROPRIATE_FALLBACK);
3532 ssl3_send_alert(s, SSL3_AL_FATAL,
3533 SSL_AD_INAPPROPRIATE_FALLBACK);
3542 * We only support SSLv2 format ciphers in SSLv3+ using a
3543 * SSLv2 backward compatible ClientHello. In this case the first
3544 * byte is always 0 for SSLv3 compatible ciphers. Anything else
3545 * is an SSLv2 cipher and we ignore it
3548 c = ssl_get_cipher_by_char(s, &p[1]);
3552 c = ssl_get_cipher_by_char(s, p);
3556 if (!sk_SSL_CIPHER_push(sk, c)) {
3557 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3567 if ((skp == NULL) || (*skp == NULL))
3568 sk_SSL_CIPHER_free(sk);