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
152 #include "ssl_locl.h"
153 #include <openssl/buffer.h>
154 #include <openssl/rand.h>
155 #include <openssl/objects.h>
156 #include <openssl/evp.h>
157 #include <openssl/md5.h>
158 #ifndef OPENSSL_NO_DH
159 # include <openssl/dh.h>
161 #include <openssl/bn.h>
162 #ifndef OPENSSL_NO_ENGINE
163 # include <openssl/engine.h>
166 static int ssl_set_version(SSL *s);
167 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
168 static int ssl3_check_finished(SSL *s);
169 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
171 int (*put_cb) (const SSL_CIPHER *,
175 int ssl3_connect(SSL *s)
178 unsigned long Time = (unsigned long)time(NULL);
179 void (*cb) (const SSL *ssl, int type, int val) = NULL;
181 int new_state, state, skip = 0;
183 RAND_add(&Time, sizeof(Time), 0);
187 if (s->info_callback != NULL)
188 cb = s->info_callback;
189 else if (s->ctx->info_callback != NULL)
190 cb = s->ctx->info_callback;
193 if (!SSL_in_init(s) || SSL_in_before(s)) {
198 #ifndef OPENSSL_NO_HEARTBEATS
200 * If we're awaiting a HeartbeatResponse, pretend we already got and
201 * don't await it anymore, because Heartbeats don't make sense during
204 if (s->tlsext_hb_pending) {
205 s->tlsext_hb_pending = 0;
214 case SSL_ST_RENEGOTIATE:
216 s->state = SSL_ST_CONNECT;
217 s->ctx->stats.sess_connect_renegotiate++;
221 case SSL_ST_BEFORE | SSL_ST_CONNECT:
222 case SSL_ST_OK | SSL_ST_CONNECT:
226 cb(s, SSL_CB_HANDSHAKE_START, 1);
228 if ((s->version >> 8) != SSL3_VERSION_MAJOR
229 && s->version != TLS_ANY_VERSION) {
230 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
231 s->state = SSL_ST_ERR;
236 if (s->version != TLS_ANY_VERSION &&
237 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
238 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
242 /* s->version=SSL3_VERSION; */
243 s->type = SSL_ST_CONNECT;
245 if (s->init_buf == NULL) {
246 if ((buf = BUF_MEM_new()) == NULL) {
248 s->state = SSL_ST_ERR;
251 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
253 s->state = SSL_ST_ERR;
260 if (!ssl3_setup_buffers(s)) {
265 /* setup buffing BIO */
266 if (!ssl_init_wbio_buffer(s, 0)) {
268 s->state = SSL_ST_ERR;
272 /* don't push the buffering BIO quite yet */
274 ssl3_init_finished_mac(s);
276 s->state = SSL3_ST_CW_CLNT_HELLO_A;
277 s->ctx->stats.sess_connect++;
279 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
281 * Should have been reset by ssl3_get_finished, too.
283 s->s3->change_cipher_spec = 0;
286 case SSL3_ST_CW_CLNT_HELLO_A:
287 case SSL3_ST_CW_CLNT_HELLO_B:
290 ret = ssl3_client_hello(s);
293 s->state = SSL3_ST_CR_SRVR_HELLO_A;
296 /* turn on buffering for the next lot of output */
297 if (s->bbio != s->wbio)
298 s->wbio = BIO_push(s->bbio, s->wbio);
302 case SSL3_ST_CR_SRVR_HELLO_A:
303 case SSL3_ST_CR_SRVR_HELLO_B:
304 ret = ssl3_get_server_hello(s);
309 s->state = SSL3_ST_CR_FINISHED_A;
310 if (s->tlsext_ticket_expected) {
311 /* receive renewed session ticket */
312 s->state = SSL3_ST_CR_SESSION_TICKET_A;
315 s->state = SSL3_ST_CR_CERT_A;
319 case SSL3_ST_CR_CERT_A:
320 case SSL3_ST_CR_CERT_B:
321 /* Noop (ret = 0) for everything but EAP-FAST. */
322 ret = ssl3_check_finished(s);
327 s->state = SSL3_ST_CR_FINISHED_A;
332 /* Check if it is anon DH/ECDH, SRP auth */
334 if (!(s->s3->tmp.new_cipher->algorithm_auth &
335 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
336 ret = ssl3_get_server_certificate(s);
340 if (s->tlsext_status_expected)
341 s->state = SSL3_ST_CR_CERT_STATUS_A;
343 s->state = SSL3_ST_CR_KEY_EXCH_A;
346 s->state = SSL3_ST_CR_KEY_EXCH_A;
352 case SSL3_ST_CR_KEY_EXCH_A:
353 case SSL3_ST_CR_KEY_EXCH_B:
354 ret = ssl3_get_key_exchange(s);
357 s->state = SSL3_ST_CR_CERT_REQ_A;
361 * at this point we check that we have the required stuff from
364 if (!ssl3_check_cert_and_algorithm(s)) {
366 s->state = SSL_ST_ERR;
371 case SSL3_ST_CR_CERT_REQ_A:
372 case SSL3_ST_CR_CERT_REQ_B:
373 ret = ssl3_get_certificate_request(s);
376 s->state = SSL3_ST_CR_SRVR_DONE_A;
380 case SSL3_ST_CR_SRVR_DONE_A:
381 case SSL3_ST_CR_SRVR_DONE_B:
382 ret = ssl3_get_server_done(s);
385 #ifndef OPENSSL_NO_SRP
386 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
387 if ((ret = SRP_Calc_A_param(s)) <= 0) {
388 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
389 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
390 s->state = SSL_ST_ERR;
395 if (s->s3->tmp.cert_req)
396 s->state = SSL3_ST_CW_CERT_A;
398 s->state = SSL3_ST_CW_KEY_EXCH_A;
403 case SSL3_ST_CW_CERT_A:
404 case SSL3_ST_CW_CERT_B:
405 case SSL3_ST_CW_CERT_C:
406 case SSL3_ST_CW_CERT_D:
407 ret = ssl3_send_client_certificate(s);
410 s->state = SSL3_ST_CW_KEY_EXCH_A;
414 case SSL3_ST_CW_KEY_EXCH_A:
415 case SSL3_ST_CW_KEY_EXCH_B:
416 ret = ssl3_send_client_key_exchange(s);
420 * EAY EAY EAY need to check for DH fix cert sent back
423 * For TLS, cert_req is set to 2, so a cert chain of nothing is
424 * sent, but no verify packet is sent
427 * XXX: For now, we do not support client authentication in ECDH
428 * cipher suites with ECDH (rather than ECDSA) certificates. We
429 * need to skip the certificate verify message when client's
430 * ECDH public key is sent inside the client certificate.
432 if (s->s3->tmp.cert_req == 1) {
433 s->state = SSL3_ST_CW_CERT_VRFY_A;
435 s->state = SSL3_ST_CW_CHANGE_A;
437 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
438 s->state = SSL3_ST_CW_CHANGE_A;
444 case SSL3_ST_CW_CERT_VRFY_A:
445 case SSL3_ST_CW_CERT_VRFY_B:
446 ret = ssl3_send_client_verify(s);
449 s->state = SSL3_ST_CW_CHANGE_A;
453 case SSL3_ST_CW_CHANGE_A:
454 case SSL3_ST_CW_CHANGE_B:
455 ret = ssl3_send_change_cipher_spec(s,
457 SSL3_ST_CW_CHANGE_B);
461 #if defined(OPENSSL_NO_NEXTPROTONEG)
462 s->state = SSL3_ST_CW_FINISHED_A;
464 if (s->s3->next_proto_neg_seen)
465 s->state = SSL3_ST_CW_NEXT_PROTO_A;
467 s->state = SSL3_ST_CW_FINISHED_A;
471 s->session->cipher = s->s3->tmp.new_cipher;
472 #ifdef OPENSSL_NO_COMP
473 s->session->compress_meth = 0;
475 if (s->s3->tmp.new_compression == NULL)
476 s->session->compress_meth = 0;
478 s->session->compress_meth = s->s3->tmp.new_compression->id;
480 if (!s->method->ssl3_enc->setup_key_block(s)) {
482 s->state = SSL_ST_ERR;
486 if (!s->method->ssl3_enc->change_cipher_state(s,
487 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
490 s->state = SSL_ST_ERR;
496 #if !defined(OPENSSL_NO_NEXTPROTONEG)
497 case SSL3_ST_CW_NEXT_PROTO_A:
498 case SSL3_ST_CW_NEXT_PROTO_B:
499 ret = ssl3_send_next_proto(s);
502 s->state = SSL3_ST_CW_FINISHED_A;
506 case SSL3_ST_CW_FINISHED_A:
507 case SSL3_ST_CW_FINISHED_B:
508 ret = ssl3_send_finished(s,
509 SSL3_ST_CW_FINISHED_A,
510 SSL3_ST_CW_FINISHED_B,
512 ssl3_enc->client_finished_label,
514 ssl3_enc->client_finished_label_len);
517 s->state = SSL3_ST_CW_FLUSH;
520 s->s3->tmp.next_state = SSL_ST_OK;
523 * Allow NewSessionTicket if ticket expected
525 if (s->tlsext_ticket_expected)
526 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
528 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
533 case SSL3_ST_CR_SESSION_TICKET_A:
534 case SSL3_ST_CR_SESSION_TICKET_B:
535 ret = ssl3_get_new_session_ticket(s);
538 s->state = SSL3_ST_CR_FINISHED_A;
542 case SSL3_ST_CR_CERT_STATUS_A:
543 case SSL3_ST_CR_CERT_STATUS_B:
544 ret = ssl3_get_cert_status(s);
547 s->state = SSL3_ST_CR_KEY_EXCH_A;
551 case SSL3_ST_CR_FINISHED_A:
552 case SSL3_ST_CR_FINISHED_B:
553 if (!s->s3->change_cipher_spec)
554 s->s3->flags |= SSL3_FLAGS_CCS_OK;
555 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
556 SSL3_ST_CR_FINISHED_B);
561 s->state = SSL3_ST_CW_CHANGE_A;
563 s->state = SSL_ST_OK;
567 case SSL3_ST_CW_FLUSH:
568 s->rwstate = SSL_WRITING;
569 if (BIO_flush(s->wbio) <= 0) {
573 s->rwstate = SSL_NOTHING;
574 s->state = s->s3->tmp.next_state;
578 /* clean a few things up */
579 ssl3_cleanup_key_block(s);
580 BUF_MEM_free(s->init_buf);
583 /* remove the buffering */
584 ssl_free_wbio_buffer(s);
590 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
592 s->ctx->stats.sess_hit++;
596 s->handshake_func = ssl3_connect;
597 s->ctx->stats.sess_connect_good++;
600 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
607 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
613 /* did we do anything */
614 if (!s->s3->tmp.reuse_message && !skip) {
616 if ((ret = BIO_flush(s->wbio)) <= 0)
620 if ((cb != NULL) && (s->state != state)) {
621 new_state = s->state;
623 cb(s, SSL_CB_CONNECT_LOOP, 1);
624 s->state = new_state;
633 cb(s, SSL_CB_CONNECT_EXIT, ret);
638 * Work out what version we should be using for the initial ClientHello if
639 * the version is currently set to (D)TLS_ANY_VERSION.
640 * Returns 1 on success
643 static int ssl_set_version(SSL *s)
645 unsigned long mask, options = s->options;
647 if (s->method->version == TLS_ANY_VERSION) {
649 * SSL_OP_NO_X disables all protocols above X *if* there are
650 * some protocols below X enabled. This is required in order
651 * to maintain "version capability" vector contiguous. So
652 * that if application wants to disable TLS1.0 in favour of
653 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
654 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
656 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
657 #if !defined(OPENSSL_NO_SSL3)
661 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
662 if (options & SSL_OP_NO_TLSv1_2) {
663 if ((options & mask) != mask) {
664 s->version = TLS1_1_VERSION;
666 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
670 s->version = TLS1_2_VERSION;
673 if ((options & mask) == mask) {
674 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
677 s->version = TLS1_1_VERSION;
680 mask &= ~SSL_OP_NO_TLSv1_1;
681 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
682 s->version = TLS1_VERSION;
683 mask &= ~SSL_OP_NO_TLSv1;
684 #if !defined(OPENSSL_NO_SSL3)
685 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
686 s->version = SSL3_VERSION;
689 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
690 SSLerr(SSL_F_SSL_SET_VERSION,
691 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
695 if (s->version == SSL3_VERSION && FIPS_mode()) {
696 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
700 } else if (s->method->version == DTLS_ANY_VERSION) {
701 /* Determine which DTLS version to use */
702 /* If DTLS 1.2 disabled correct the version number */
703 if (options & SSL_OP_NO_DTLSv1_2) {
704 if (tls1_suiteb(s)) {
705 SSLerr(SSL_F_SSL_SET_VERSION,
706 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
710 * Disabling all versions is silly: return an error.
712 if (options & SSL_OP_NO_DTLSv1) {
713 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
717 * Update method so we don't use any DTLS 1.2 features.
719 s->method = DTLSv1_client_method();
720 s->version = DTLS1_VERSION;
723 * We only support one version: update method
725 if (options & SSL_OP_NO_DTLSv1)
726 s->method = DTLSv1_2_client_method();
727 s->version = DTLS1_2_VERSION;
731 s->client_version = s->version;
736 int ssl3_client_hello(SSL *s)
739 unsigned char *p, *d;
743 #ifndef OPENSSL_NO_COMP
748 buf = (unsigned char *)s->init_buf->data;
749 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
750 SSL_SESSION *sess = s->session;
752 /* Work out what SSL/TLS/DTLS version to use */
753 if (ssl_set_version(s) == 0)
756 if ((sess == NULL) || (sess->ssl_version != s->version) ||
758 * In the case of EAP-FAST, we can have a pre-shared
759 * "ticket" without a session ID.
761 (!sess->session_id_length && !sess->tlsext_tick) ||
762 (sess->not_resumable)) {
763 if (!ssl_get_new_session(s, 0))
766 /* else use the pre-loaded session */
768 p = s->s3->client_random;
771 * for DTLS if client_random is initialized, reuse it, we are
772 * required to use same upon reply to HelloVerify
774 if (SSL_IS_DTLS(s)) {
777 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
786 if (i && ssl_fill_hello_random(s, 0, p,
787 sizeof(s->s3->client_random)) <= 0)
790 /* Do the message type and length last */
791 d = p = ssl_handshake_start(s);
794 * version indicates the negotiated version: for example from
795 * an SSLv2/v3 compatible client hello). The client_version
796 * field is the maximum version we permit and it is also
797 * used in RSA encrypted premaster secrets. Some servers can
798 * choke if we initially report a higher version then
799 * renegotiate to a lower one in the premaster secret. This
800 * didn't happen with TLS 1.0 as most servers supported it
801 * but it can with TLS 1.1 or later if the server only supports
804 * Possible scenario with previous logic:
805 * 1. Client hello indicates TLS 1.2
806 * 2. Server hello says TLS 1.0
807 * 3. RSA encrypted premaster secret uses 1.2.
808 * 4. Handhaked proceeds using TLS 1.0.
809 * 5. Server sends hello request to renegotiate.
810 * 6. Client hello indicates TLS v1.0 as we now
811 * know that is maximum server supports.
812 * 7. Server chokes on RSA encrypted premaster secret
813 * containing version 1.0.
815 * For interoperability it should be OK to always use the
816 * maximum version we support in client hello and then rely
817 * on the checking of version to ensure the servers isn't
818 * being inconsistent: for example initially negotiating with
819 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
820 * client_version in client hello and not resetting it to
821 * the negotiated version.
823 *(p++) = s->client_version >> 8;
824 *(p++) = s->client_version & 0xff;
827 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
828 p += SSL3_RANDOM_SIZE;
834 i = s->session->session_id_length;
837 if (i > (int)sizeof(s->session->session_id)) {
838 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
841 memcpy(p, s->session->session_id, i);
845 /* cookie stuff for DTLS */
846 if (SSL_IS_DTLS(s)) {
847 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
848 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
851 *(p++) = s->d1->cookie_len;
852 memcpy(p, s->d1->cookie, s->d1->cookie_len);
853 p += s->d1->cookie_len;
856 /* Ciphers supported */
857 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
859 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
862 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
864 * Some servers hang if client hello > 256 bytes as hack workaround
865 * chop number of supported ciphers to keep it well below this if we
868 if (TLS1_get_version(s) >= TLS1_2_VERSION
869 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
870 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
876 #ifdef OPENSSL_NO_COMP
880 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
883 j = sk_SSL_COMP_num(s->ctx->comp_methods);
885 for (i = 0; i < j; i++) {
886 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
890 *(p++) = 0; /* Add the NULL method */
893 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
894 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
898 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
900 ssl3_send_alert(s, SSL3_AL_FATAL, al);
901 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
906 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
907 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
908 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
911 s->state = SSL3_ST_CW_CLNT_HELLO_B;
914 /* SSL3_ST_CW_CLNT_HELLO_B */
915 return ssl_do_write(s);
917 s->state = SSL_ST_ERR;
921 int ssl3_get_server_hello(SSL *s)
923 STACK_OF(SSL_CIPHER) *sk;
925 unsigned char *p, *d;
926 int i, al = SSL_AD_INTERNAL_ERROR, ok;
929 #ifndef OPENSSL_NO_COMP
933 * Hello verify request and/or server hello version may not match so set
934 * first packet if we're negotiating version.
938 n = s->method->ssl_get_message(s,
939 SSL3_ST_CR_SRVR_HELLO_A,
940 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
946 if (SSL_IS_DTLS(s)) {
947 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
948 if (s->d1->send_cookie == 0) {
949 s->s3->tmp.reuse_message = 1;
951 } else { /* already sent a cookie */
953 al = SSL_AD_UNEXPECTED_MESSAGE;
954 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
960 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
961 al = SSL_AD_UNEXPECTED_MESSAGE;
962 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
966 d = p = (unsigned char *)s->init_msg;
968 if (s->method->version == TLS_ANY_VERSION) {
969 int sversion = (p[0] << 8) | p[1];
971 #if TLS_MAX_VERSION != TLS1_2_VERSION
972 #error Code needs updating for new TLS version
974 #ifndef OPENSSL_NO_SSL3
975 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
977 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
978 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
979 al = SSL_AD_PROTOCOL_VERSION;
982 s->method = SSLv3_client_method();
985 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
986 s->method = TLSv1_client_method();
987 } else if ((sversion == TLS1_1_VERSION) &&
988 !(s->options & SSL_OP_NO_TLSv1_1)) {
989 s->method = TLSv1_1_client_method();
990 } else if ((sversion == TLS1_2_VERSION) &&
991 !(s->options & SSL_OP_NO_TLSv1_2)) {
992 s->method = TLSv1_2_client_method();
994 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
995 al = SSL_AD_PROTOCOL_VERSION;
998 s->session->ssl_version = s->version = s->method->version;
1000 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1001 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1002 al = SSL_AD_PROTOCOL_VERSION;
1005 } else if (s->method->version == DTLS_ANY_VERSION) {
1006 /* Work out correct protocol version to use */
1007 int hversion = (p[0] << 8) | p[1];
1008 int options = s->options;
1009 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1010 s->method = DTLSv1_2_client_method();
1011 else if (tls1_suiteb(s)) {
1012 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1013 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1014 s->version = hversion;
1015 al = SSL_AD_PROTOCOL_VERSION;
1017 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1018 s->method = DTLSv1_client_method();
1020 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1021 s->version = hversion;
1022 al = SSL_AD_PROTOCOL_VERSION;
1025 s->session->ssl_version = s->version = s->method->version;
1026 } else if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
1027 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1028 s->version = (s->version & 0xff00) | p[1];
1029 al = SSL_AD_PROTOCOL_VERSION;
1034 /* load the server hello data */
1035 /* load the server random */
1036 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
1037 p += SSL3_RANDOM_SIZE;
1041 /* get the session-id */
1044 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
1045 al = SSL_AD_ILLEGAL_PARAMETER;
1046 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1051 * Check if we can resume the session based on external pre-shared secret.
1052 * EAP-FAST (RFC 4851) supports two types of session resumption.
1053 * Resumption based on server-side state works with session IDs.
1054 * Resumption based on pre-shared Protected Access Credentials (PACs)
1055 * works by overriding the SessionTicket extension at the application
1056 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1057 * servers would honour the session ID.) Therefore, the session ID alone
1058 * is not a reliable indicator of session resumption, so we first check if
1059 * we can resume, and later peek at the next handshake message to see if the
1060 * server wants to resume.
1062 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1063 s->session->tlsext_tick) {
1064 SSL_CIPHER *pref_cipher = NULL;
1065 s->session->master_key_length = sizeof(s->session->master_key);
1066 if (s->tls_session_secret_cb(s, s->session->master_key,
1067 &s->session->master_key_length,
1069 s->tls_session_secret_cb_arg)) {
1070 s->session->cipher = pref_cipher ?
1071 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1073 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1074 al = SSL_AD_INTERNAL_ERROR;
1079 if (j != 0 && j == s->session->session_id_length
1080 && memcmp(p, s->session->session_id, j) == 0) {
1081 if (s->sid_ctx_length != s->session->sid_ctx_length
1082 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1083 /* actually a client application bug */
1084 al = SSL_AD_ILLEGAL_PARAMETER;
1085 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1086 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1092 * If we were trying for session-id reuse but the server
1093 * didn't echo the ID, make a new SSL_SESSION.
1094 * In the case of EAP-FAST and PAC, we do not send a session ID,
1095 * so the PAC-based session secret is always preserved. It'll be
1096 * overwritten if the server refuses resumption.
1098 if (s->session->session_id_length > 0) {
1099 if (!ssl_get_new_session(s, 0)) {
1103 s->session->session_id_length = j;
1104 memcpy(s->session->session_id, p, j); /* j could be 0 */
1107 c = ssl_get_cipher_by_char(s, p);
1109 /* unknown cipher */
1110 al = SSL_AD_ILLEGAL_PARAMETER;
1111 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1114 /* Set version disabled mask now we know version */
1115 if (!SSL_USE_TLS1_2_CIPHERS(s))
1116 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1118 s->s3->tmp.mask_ssl = 0;
1120 * If it is a disabled cipher we didn't send it in client hello, so
1123 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1124 al = SSL_AD_ILLEGAL_PARAMETER;
1125 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1128 p += ssl_put_cipher_by_char(s, NULL, NULL);
1130 sk = ssl_get_ciphers_by_id(s);
1131 i = sk_SSL_CIPHER_find(sk, c);
1133 /* we did not say we would use this cipher */
1134 al = SSL_AD_ILLEGAL_PARAMETER;
1135 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1140 * Depending on the session caching (internal/external), the cipher
1141 * and/or cipher_id values may not be set. Make sure that cipher_id is
1142 * set and use it for comparison.
1144 if (s->session->cipher)
1145 s->session->cipher_id = s->session->cipher->id;
1146 if (s->hit && (s->session->cipher_id != c->id)) {
1147 al = SSL_AD_ILLEGAL_PARAMETER;
1148 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1149 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1152 s->s3->tmp.new_cipher = c;
1154 * Don't digest cached records if no sigalgs: we may need them for client
1157 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1159 /* lets get the compression algorithm */
1161 #ifdef OPENSSL_NO_COMP
1163 al = SSL_AD_ILLEGAL_PARAMETER;
1164 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1165 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1169 * If compression is disabled we'd better not try to resume a session
1170 * using compression.
1172 if (s->session->compress_meth != 0) {
1173 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1178 if (s->hit && j != s->session->compress_meth) {
1179 al = SSL_AD_ILLEGAL_PARAMETER;
1180 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1181 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1186 else if (!ssl_allow_compression(s)) {
1187 al = SSL_AD_ILLEGAL_PARAMETER;
1188 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1191 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1193 if ((j != 0) && (comp == NULL)) {
1194 al = SSL_AD_ILLEGAL_PARAMETER;
1195 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1196 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1199 s->s3->tmp.new_compression = comp;
1203 /* TLS extensions */
1204 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1205 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1210 /* wrong packet length */
1211 al = SSL_AD_DECODE_ERROR;
1212 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1218 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1220 s->state = SSL_ST_ERR;
1224 int ssl3_get_server_certificate(SSL *s)
1226 int al, i, ok, ret = -1, exp_idx;
1227 unsigned long n, nc, llen, l;
1229 const unsigned char *q, *p;
1231 STACK_OF(X509) *sk = NULL;
1232 EVP_PKEY *pkey = NULL;
1234 n = s->method->ssl_get_message(s,
1237 -1, s->max_cert_list, &ok);
1242 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1243 s->s3->tmp.reuse_message = 1;
1247 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1248 al = SSL_AD_UNEXPECTED_MESSAGE;
1249 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1252 p = d = (unsigned char *)s->init_msg;
1254 if ((sk = sk_X509_new_null()) == NULL) {
1255 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1260 if (llen + 3 != n) {
1261 al = SSL_AD_DECODE_ERROR;
1262 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1265 for (nc = 0; nc < llen;) {
1267 if ((l + nc + 3) > llen) {
1268 al = SSL_AD_DECODE_ERROR;
1269 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1270 SSL_R_CERT_LENGTH_MISMATCH);
1275 x = d2i_X509(NULL, &q, l);
1277 al = SSL_AD_BAD_CERTIFICATE;
1278 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1282 al = SSL_AD_DECODE_ERROR;
1283 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1284 SSL_R_CERT_LENGTH_MISMATCH);
1287 if (!sk_X509_push(sk, x)) {
1288 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1296 i = ssl_verify_cert_chain(s, sk);
1297 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1298 al = ssl_verify_alarm_type(s->verify_result);
1299 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1300 SSL_R_CERTIFICATE_VERIFY_FAILED);
1303 ERR_clear_error(); /* but we keep s->verify_result */
1305 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1306 al = SSL_AD_HANDSHAKE_FAILURE;
1310 s->session->peer_chain = sk;
1312 * Inconsistency alert: cert_chain does include the peer's certificate,
1313 * which we don't include in s3_srvr.c
1315 x = sk_X509_value(sk, 0);
1318 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1321 pkey = X509_get_pubkey(x);
1323 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1326 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1327 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1331 i = ssl_cert_type(x, pkey);
1335 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1336 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1340 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1341 if (exp_idx >= 0 && i != exp_idx) {
1343 al = SSL_AD_ILLEGAL_PARAMETER;
1344 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1345 SSL_R_WRONG_CERTIFICATE_TYPE);
1348 s->session->peer_type = i;
1350 X509_free(s->session->peer);
1351 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1352 s->session->peer = x;
1353 s->session->verify_result = s->verify_result;
1360 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1362 s->state = SSL_ST_ERR;
1364 EVP_PKEY_free(pkey);
1366 sk_X509_pop_free(sk, X509_free);
1370 int ssl3_get_key_exchange(SSL *s)
1372 #ifndef OPENSSL_NO_RSA
1373 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1376 unsigned char *param, *p;
1378 long i, param_len, n, alg_k, alg_a;
1379 EVP_PKEY *pkey = NULL;
1380 const EVP_MD *md = NULL;
1381 #ifndef OPENSSL_NO_RSA
1384 #ifndef OPENSSL_NO_DH
1387 #ifndef OPENSSL_NO_EC
1388 EC_KEY *ecdh = NULL;
1389 BN_CTX *bn_ctx = NULL;
1390 EC_POINT *srvr_ecpoint = NULL;
1392 int encoded_pt_len = 0;
1395 EVP_MD_CTX_init(&md_ctx);
1398 * use same message size as in ssl3_get_certificate_request() as
1399 * ServerKeyExchange message may be skipped
1401 n = s->method->ssl_get_message(s,
1402 SSL3_ST_CR_KEY_EXCH_A,
1403 SSL3_ST_CR_KEY_EXCH_B,
1404 -1, s->max_cert_list, &ok);
1408 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1410 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1412 * Can't skip server key exchange if this is an ephemeral
1415 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
1416 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1417 al = SSL_AD_UNEXPECTED_MESSAGE;
1421 s->s3->tmp.reuse_message = 1;
1425 param = p = (unsigned char *)s->init_msg;
1427 #ifndef OPENSSL_NO_RSA
1428 RSA_free(s->s3->peer_rsa_tmp);
1429 s->s3->peer_rsa_tmp = NULL;
1431 #ifndef OPENSSL_NO_DH
1432 DH_free(s->s3->peer_dh_tmp);
1433 s->s3->peer_dh_tmp = NULL;
1435 #ifndef OPENSSL_NO_EC
1436 EC_KEY_free(s->s3->peer_ecdh_tmp);
1437 s->s3->peer_ecdh_tmp = NULL;
1440 /* Total length of the parameters including the length prefix */
1443 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1445 al = SSL_AD_DECODE_ERROR;
1447 #ifndef OPENSSL_NO_PSK
1448 /* PSK ciphersuites are preceded by an identity hint */
1449 if (alg_k & SSL_PSK) {
1452 if (param_len > n) {
1453 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1459 * Store PSK identity hint for later use, hint is used in
1460 * ssl3_send_client_key_exchange. Assume that the maximum length of
1461 * a PSK identity hint can be as long as the maximum length of a PSK
1464 if (i > PSK_MAX_IDENTITY_LEN) {
1465 al = SSL_AD_HANDSHAKE_FAILURE;
1466 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1469 if (i > n - param_len) {
1470 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1471 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1476 OPENSSL_free(s->session->psk_identity_hint);
1478 s->session->psk_identity_hint = BUF_strndup((char *)p, i);
1479 if (s->session->psk_identity_hint == NULL) {
1480 al = SSL_AD_HANDSHAKE_FAILURE;
1481 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1485 s->session->psk_identity_hint = NULL;
1492 /* Nothing else to do for plain PSK or RSAPSK */
1493 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1495 #endif /* !OPENSSL_NO_PSK */
1496 #ifndef OPENSSL_NO_SRP
1497 if (alg_k & SSL_kSRP) {
1499 if (param_len > n) {
1500 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1505 if (i > n - param_len) {
1506 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1511 if ((s->srp_ctx.N = BN_bin2bn(p, i, NULL)) == NULL) {
1512 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1517 if (2 > n - param_len) {
1518 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1525 if (i > n - param_len) {
1526 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1531 if ((s->srp_ctx.g = BN_bin2bn(p, i, NULL)) == NULL) {
1532 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1537 if (1 > n - param_len) {
1538 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1543 i = (unsigned int)(p[0]);
1546 if (i > n - param_len) {
1547 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1552 if ((s->srp_ctx.s = BN_bin2bn(p, i, NULL)) == NULL) {
1553 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1558 if (2 > n - param_len) {
1559 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1566 if (i > n - param_len) {
1567 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1572 if ((s->srp_ctx.B = BN_bin2bn(p, i, NULL)) == NULL) {
1573 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1579 if (!srp_verify_server_param(s, &al)) {
1580 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1584 /* We must check if there is a certificate */
1585 if (alg_a & (SSL_aRSA|SSL_aDSS))
1586 pkey = X509_get_pubkey(s->session->peer);
1588 #endif /* !OPENSSL_NO_SRP */
1589 #ifndef OPENSSL_NO_RSA
1590 if (alg_k & SSL_kRSA) {
1591 /* Temporary RSA keys only allowed in export ciphersuites */
1592 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1593 al = SSL_AD_UNEXPECTED_MESSAGE;
1594 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1597 if ((rsa = RSA_new()) == NULL) {
1598 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1603 if (param_len > n) {
1604 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1609 if (i > n - param_len) {
1610 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1615 if ((rsa->n = BN_bin2bn(p, i, rsa->n)) == NULL) {
1616 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1621 if (2 > n - param_len) {
1622 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1629 if (i > n - param_len) {
1630 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1635 if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == NULL) {
1636 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1642 /* this should be because we are using an export cipher */
1643 if (alg_a & SSL_aRSA)
1644 pkey = X509_get_pubkey(s->session->peer);
1646 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1650 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1651 al = SSL_AD_UNEXPECTED_MESSAGE;
1652 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1656 s->s3->peer_rsa_tmp = rsa;
1659 #else /* OPENSSL_NO_RSA */
1662 #ifndef OPENSSL_NO_DH
1663 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1664 if ((dh = DH_new()) == NULL) {
1665 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1670 if (param_len > n) {
1671 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1676 if (i > n - param_len) {
1677 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1682 if ((dh->p = BN_bin2bn(p, i, NULL)) == NULL) {
1683 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1688 if (2 > n - param_len) {
1689 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1696 if (i > n - param_len) {
1697 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1702 if ((dh->g = BN_bin2bn(p, i, NULL)) == NULL) {
1703 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1708 if (2 > n - param_len) {
1709 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1716 if (i > n - param_len) {
1717 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1722 if ((dh->pub_key = BN_bin2bn(p, i, NULL)) == NULL) {
1723 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1729 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1730 al = SSL_AD_HANDSHAKE_FAILURE;
1731 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1734 if (alg_a & (SSL_aRSA|SSL_aDSS))
1735 pkey = X509_get_pubkey(s->session->peer);
1736 /* else anonymous DH, so no certificate or pkey. */
1738 s->s3->peer_dh_tmp = dh;
1741 #endif /* !OPENSSL_NO_DH */
1743 #ifndef OPENSSL_NO_EC
1744 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1746 const EC_GROUP *group;
1748 if ((ecdh = EC_KEY_new()) == NULL) {
1749 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1754 * Extract elliptic curve parameters and the server's ephemeral ECDH
1755 * public key. Keep accumulating lengths of various components in
1756 * param_len and make sure it never exceeds n.
1760 * XXX: For now we only support named (not generic) curves and the
1761 * ECParameters in this case is just three bytes. We also need one
1762 * byte for the length of the encoded point
1765 if (param_len > n) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1770 * Check curve is one of our preferences, if not server has sent an
1771 * invalid curve. ECParameters is 3 bytes.
1773 if (!tls1_check_curve(s, p, 3)) {
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1778 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1779 al = SSL_AD_INTERNAL_ERROR;
1780 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1781 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1785 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1786 if (ngroup == NULL) {
1787 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1790 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1791 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1794 EC_GROUP_free(ngroup);
1796 group = EC_KEY_get0_group(ecdh);
1798 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1799 (EC_GROUP_get_degree(group) > 163)) {
1800 al = SSL_AD_EXPORT_RESTRICTION;
1801 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1802 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1808 /* Next, get the encoded ECPoint */
1809 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1810 ((bn_ctx = BN_CTX_new()) == NULL)) {
1811 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1815 encoded_pt_len = *p; /* length of encoded point */
1818 if ((encoded_pt_len > n - param_len) ||
1819 (EC_POINT_oct2point(group, srvr_ecpoint,
1820 p, encoded_pt_len, bn_ctx) == 0)) {
1821 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1824 param_len += encoded_pt_len;
1827 p += encoded_pt_len;
1830 * The ECC/TLS specification does not mention the use of DSA to sign
1831 * ECParameters in the server key exchange message. We do support RSA
1835 # ifndef OPENSSL_NO_RSA
1836 else if (alg_a & SSL_aRSA)
1837 pkey = X509_get_pubkey(s->session->peer);
1839 # ifndef OPENSSL_NO_EC
1840 else if (alg_a & SSL_aECDSA)
1841 pkey = X509_get_pubkey(s->session->peer);
1843 /* else anonymous ECDH, so no certificate or pkey. */
1844 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1845 s->s3->peer_ecdh_tmp = ecdh;
1847 BN_CTX_free(bn_ctx);
1849 EC_POINT_free(srvr_ecpoint);
1850 srvr_ecpoint = NULL;
1852 al = SSL_AD_UNEXPECTED_MESSAGE;
1853 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1856 #endif /* !OPENSSL_NO_EC */
1858 /* p points to the next byte, there are 'n' bytes left */
1860 /* if it was signed, check the signature */
1862 if (SSL_USE_SIGALGS(s)) {
1865 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1868 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1875 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1883 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1888 j = EVP_PKEY_size(pkey);
1891 * Check signature length. If n is 0 then signature is empty
1893 if ((i != n) || (n > j) || (n <= 0)) {
1894 /* wrong packet length */
1895 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1898 #ifndef OPENSSL_NO_RSA
1899 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1905 for (num = 2; num > 0; num--) {
1906 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1907 EVP_DigestInit_ex(&md_ctx, (num == 2)
1908 ? s->ctx->md5 : s->ctx->sha1, NULL);
1909 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1911 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1913 EVP_DigestUpdate(&md_ctx, param, param_len);
1914 EVP_DigestFinal_ex(&md_ctx, q, &size);
1918 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1920 al = SSL_AD_DECRYPT_ERROR;
1921 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1926 al = SSL_AD_DECRYPT_ERROR;
1927 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1933 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1934 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1936 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1938 EVP_VerifyUpdate(&md_ctx, param, param_len);
1939 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1941 al = SSL_AD_DECRYPT_ERROR;
1942 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1947 /* aNULL, aSRP or PSK do not need public keys */
1948 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1949 /* Might be wrong key type, check it */
1950 if (ssl3_check_cert_and_algorithm(s))
1951 /* Otherwise this shouldn't happen */
1952 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1955 /* still data left over */
1957 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1961 EVP_PKEY_free(pkey);
1962 EVP_MD_CTX_cleanup(&md_ctx);
1965 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1967 EVP_PKEY_free(pkey);
1968 #ifndef OPENSSL_NO_RSA
1971 #ifndef OPENSSL_NO_DH
1974 #ifndef OPENSSL_NO_EC
1975 BN_CTX_free(bn_ctx);
1976 EC_POINT_free(srvr_ecpoint);
1979 EVP_MD_CTX_cleanup(&md_ctx);
1980 s->state = SSL_ST_ERR;
1984 int ssl3_get_certificate_request(SSL *s)
1987 unsigned long n, nc, l;
1988 unsigned int llen, ctype_num, i;
1989 X509_NAME *xn = NULL;
1990 const unsigned char *p, *q;
1992 STACK_OF(X509_NAME) *ca_sk = NULL;
1994 n = s->method->ssl_get_message(s,
1995 SSL3_ST_CR_CERT_REQ_A,
1996 SSL3_ST_CR_CERT_REQ_B,
1997 -1, s->max_cert_list, &ok);
2002 s->s3->tmp.cert_req = 0;
2004 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2005 s->s3->tmp.reuse_message = 1;
2007 * If we get here we don't need any cached handshake records as we
2008 * wont be doing client auth.
2010 if (!ssl3_digest_cached_records(s, 0))
2015 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2016 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2017 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2021 /* TLS does not like anon-DH with client cert */
2022 if (s->version > SSL3_VERSION) {
2023 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2024 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2025 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2026 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2031 p = d = (unsigned char *)s->init_msg;
2033 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2034 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2038 /* get the certificate types */
2040 OPENSSL_free(s->cert->ctypes);
2041 s->cert->ctypes = NULL;
2042 if (ctype_num > SSL3_CT_NUMBER) {
2043 /* If we exceed static buffer copy all to cert structure */
2044 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2045 if (s->cert->ctypes == NULL) {
2046 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2049 memcpy(s->cert->ctypes, p, ctype_num);
2050 s->cert->ctype_num = (size_t)ctype_num;
2051 ctype_num = SSL3_CT_NUMBER;
2053 for (i = 0; i < ctype_num; i++)
2054 s->s3->tmp.ctype[i] = p[i];
2056 if (SSL_USE_SIGALGS(s)) {
2059 * Check we have enough room for signature algorithms and following
2062 if ((unsigned long)(p - d + llen + 2) > n) {
2063 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2064 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2065 SSL_R_DATA_LENGTH_TOO_LONG);
2068 /* Clear certificate digests and validity flags */
2069 for (i = 0; i < SSL_PKEY_NUM; i++) {
2070 s->s3->tmp.md[i] = NULL;
2071 s->s3->tmp.valid_flags[i] = 0;
2073 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2074 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2075 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2076 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2079 if (!tls1_process_sigalgs(s)) {
2080 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2081 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2087 /* get the CA RDNs */
2090 if ((unsigned long)(p - d + llen) != n) {
2091 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2092 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2096 for (nc = 0; nc < llen;) {
2098 if ((l + nc + 2) > llen) {
2099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2100 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2106 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2108 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2113 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2114 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2115 SSL_R_CA_DN_LENGTH_MISMATCH);
2118 if (!sk_X509_NAME_push(ca_sk, xn)) {
2119 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2127 /* we should setup a certificate to return.... */
2128 s->s3->tmp.cert_req = 1;
2129 s->s3->tmp.ctype_num = ctype_num;
2130 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2131 s->s3->tmp.ca_names = ca_sk;
2137 s->state = SSL_ST_ERR;
2139 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2143 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2145 return (X509_NAME_cmp(*a, *b));
2148 int ssl3_get_new_session_ticket(SSL *s)
2150 int ok, al, ret = 0, ticklen;
2152 const unsigned char *p;
2155 n = s->method->ssl_get_message(s,
2156 SSL3_ST_CR_SESSION_TICKET_A,
2157 SSL3_ST_CR_SESSION_TICKET_B,
2158 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2164 /* need at least ticket_lifetime_hint + ticket length */
2165 al = SSL_AD_DECODE_ERROR;
2166 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2170 p = d = (unsigned char *)s->init_msg;
2172 if (s->session->session_id_length > 0) {
2173 int i = s->session_ctx->session_cache_mode;
2174 SSL_SESSION *new_sess;
2176 * We reused an existing session, so we need to replace it with a new
2179 if (i & SSL_SESS_CACHE_CLIENT) {
2181 * Remove the old session from the cache
2183 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2184 if (s->session_ctx->remove_session_cb != NULL)
2185 s->session_ctx->remove_session_cb(s->session_ctx,
2188 /* We carry on if this fails */
2189 SSL_CTX_remove_session(s->session_ctx, s->session);
2193 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2194 al = SSL_AD_INTERNAL_ERROR;
2195 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2199 SSL_SESSION_free(s->session);
2200 s->session = new_sess;
2203 n2l(p, s->session->tlsext_tick_lifetime_hint);
2205 /* ticket_lifetime_hint + ticket_length + ticket */
2206 if (ticklen + 6 != n) {
2207 al = SSL_AD_DECODE_ERROR;
2208 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2211 OPENSSL_free(s->session->tlsext_tick);
2212 s->session->tlsext_ticklen = 0;
2213 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2214 if (!s->session->tlsext_tick) {
2215 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2218 memcpy(s->session->tlsext_tick, p, ticklen);
2219 s->session->tlsext_ticklen = ticklen;
2221 * There are two ways to detect a resumed ticket session. One is to set
2222 * an appropriate session ID and then the server must return a match in
2223 * ServerHello. This allows the normal client session ID matching to work
2224 * and we know much earlier that the ticket has been accepted. The
2225 * other way is to set zero length session ID when the ticket is
2226 * presented and rely on the handshake to determine session resumption.
2227 * We choose the former approach because this fits in with assumptions
2228 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2229 * SHA256 is disabled) hash of the ticket.
2231 EVP_Digest(p, ticklen,
2232 s->session->session_id, &s->session->session_id_length,
2233 EVP_sha256(), NULL);
2237 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2239 s->state = SSL_ST_ERR;
2243 int ssl3_get_cert_status(SSL *s)
2246 unsigned long resplen, n;
2247 const unsigned char *p;
2249 n = s->method->ssl_get_message(s,
2250 SSL3_ST_CR_CERT_STATUS_A,
2251 SSL3_ST_CR_CERT_STATUS_B,
2252 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2257 /* need at least status type + length */
2258 al = SSL_AD_DECODE_ERROR;
2259 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2262 p = (unsigned char *)s->init_msg;
2263 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2264 al = SSL_AD_DECODE_ERROR;
2265 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2269 if (resplen + 4 != n) {
2270 al = SSL_AD_DECODE_ERROR;
2271 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2274 OPENSSL_free(s->tlsext_ocsp_resp);
2275 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2276 if (!s->tlsext_ocsp_resp) {
2277 al = SSL_AD_INTERNAL_ERROR;
2278 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2281 s->tlsext_ocsp_resplen = resplen;
2282 if (s->ctx->tlsext_status_cb) {
2284 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2286 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2287 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2291 al = SSL_AD_INTERNAL_ERROR;
2292 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2298 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2299 s->state = SSL_ST_ERR;
2303 int ssl3_get_server_done(SSL *s)
2308 /* Second to last param should be very small, like 0 :-) */
2309 n = s->method->ssl_get_message(s,
2310 SSL3_ST_CR_SRVR_DONE_A,
2311 SSL3_ST_CR_SRVR_DONE_B,
2312 SSL3_MT_SERVER_DONE, 30, &ok);
2317 /* should contain no data */
2318 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2319 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2320 s->state = SSL_ST_ERR;
2327 int ssl3_send_client_key_exchange(SSL *s)
2331 #ifndef OPENSSL_NO_PSK
2332 size_t pskhdrlen = 0;
2334 unsigned long alg_k;
2335 #ifndef OPENSSL_NO_RSA
2337 EVP_PKEY *pkey = NULL;
2339 #ifndef OPENSSL_NO_EC
2340 EC_KEY *clnt_ecdh = NULL;
2341 const EC_POINT *srvr_ecpoint = NULL;
2342 EVP_PKEY *srvr_pub_pkey = NULL;
2343 unsigned char *encodedPoint = NULL;
2344 int encoded_pt_len = 0;
2345 BN_CTX *bn_ctx = NULL;
2347 unsigned char *pms = NULL;
2349 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2351 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2352 p = ssl_handshake_start(s);
2355 #ifndef OPENSSL_NO_PSK
2356 if (alg_k & SSL_PSK) {
2359 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2360 * \0-terminated identity. The last byte is for us for simulating
2363 char identity[PSK_MAX_IDENTITY_LEN + 1];
2365 unsigned char psk[PSK_MAX_PSK_LEN];
2368 if (s->psk_client_callback == NULL) {
2369 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2370 SSL_R_PSK_NO_CLIENT_CB);
2374 memset(identity, 0, sizeof(identity));
2376 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2377 identity, sizeof(identity) - 1,
2380 if (psklen > PSK_MAX_PSK_LEN) {
2381 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2382 ERR_R_INTERNAL_ERROR);
2384 } else if (psklen == 0) {
2385 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2386 SSL_R_PSK_IDENTITY_NOT_FOUND);
2390 OPENSSL_free(s->s3->tmp.psk);
2391 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2392 OPENSSL_cleanse(psk, psklen);
2394 if (s->s3->tmp.psk == NULL)
2397 s->s3->tmp.psklen = psklen;
2399 identitylen = strlen(identity);
2400 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2401 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2402 ERR_R_INTERNAL_ERROR);
2405 OPENSSL_free(s->session->psk_identity);
2406 s->session->psk_identity = BUF_strdup(identity);
2407 if (s->session->psk_identity == NULL)
2410 s2n(identitylen, p);
2411 memcpy(p, identity, identitylen);
2412 pskhdrlen = 2 + identitylen;
2416 OPENSSL_cleanse(identity, sizeof(identity));
2418 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2422 if (alg_k & SSL_kPSK) {
2427 /* Fool emacs indentation */
2430 #ifndef OPENSSL_NO_RSA
2431 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2433 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2434 pms = OPENSSL_malloc(pmslen);
2438 if (s->session->peer == NULL) {
2440 * We should always have a server certificate with SSL_kRSA.
2442 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2443 ERR_R_INTERNAL_ERROR);
2447 if (s->s3->peer_rsa_tmp != NULL)
2448 rsa = s->s3->peer_rsa_tmp;
2450 pkey = X509_get_pubkey(s->session->peer);
2451 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2452 || (pkey->pkey.rsa == NULL)) {
2453 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2454 ERR_R_INTERNAL_ERROR);
2457 rsa = pkey->pkey.rsa;
2458 EVP_PKEY_free(pkey);
2461 pms[0] = s->client_version >> 8;
2462 pms[1] = s->client_version & 0xff;
2463 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2467 /* Fix buf for TLS and beyond */
2468 if (s->version > SSL3_VERSION)
2470 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2472 if (s->options & SSL_OP_PKCS1_CHECK_1)
2474 if (s->options & SSL_OP_PKCS1_CHECK_2)
2478 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2479 SSL_R_BAD_RSA_ENCRYPT);
2483 /* Fix buf for TLS and beyond */
2484 if (s->version > SSL3_VERSION) {
2490 #ifndef OPENSSL_NO_DH
2491 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2492 DH *dh_srvr, *dh_clnt;
2493 if (s->s3->peer_dh_tmp != NULL)
2494 dh_srvr = s->s3->peer_dh_tmp;
2496 /* we get them from the cert */
2497 EVP_PKEY *spkey = NULL;
2499 spkey = X509_get_pubkey(s->session->peer);
2501 dh_srvr = EVP_PKEY_get1_DH(spkey);
2502 EVP_PKEY_free(spkey);
2504 if (dh_srvr == NULL) {
2505 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2506 ERR_R_INTERNAL_ERROR);
2510 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2511 /* Use client certificate key */
2512 EVP_PKEY *clkey = s->cert->key->privatekey;
2515 dh_clnt = EVP_PKEY_get1_DH(clkey);
2516 if (dh_clnt == NULL) {
2517 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2518 ERR_R_INTERNAL_ERROR);
2522 /* generate a new random key */
2523 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2524 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2527 if (!DH_generate_key(dh_clnt)) {
2528 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2534 pmslen = DH_size(dh_clnt);
2535 pms = OPENSSL_malloc(pmslen);
2540 * use the 'p' output buffer for the DH key, but make sure to
2541 * clear it out afterwards
2544 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2545 if (s->s3->peer_dh_tmp == NULL)
2549 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2555 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2558 /* send off the data */
2559 n = BN_num_bytes(dh_clnt->pub_key);
2561 BN_bn2bin(dh_clnt->pub_key, p);
2569 #ifndef OPENSSL_NO_EC
2570 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2571 const EC_GROUP *srvr_group = NULL;
2573 int ecdh_clnt_cert = 0;
2576 * Did we send out the client's ECDH share for use in premaster
2577 * computation as part of client certificate? If so, set
2578 * ecdh_clnt_cert to 1.
2580 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2582 * XXX: For now, we do not support client
2583 * authentication using ECDH certificates.
2584 * To add such support, one needs to add
2585 * code that checks for appropriate
2586 * conditions and sets ecdh_clnt_cert to 1.
2587 * For example, the cert have an ECC
2588 * key on the same curve as the server's
2589 * and the key should be authorized for
2592 * One also needs to add code in ssl3_connect
2593 * to skip sending the certificate verify
2596 * if ((s->cert->key->privatekey != NULL) &&
2597 * (s->cert->key->privatekey->type ==
2598 * EVP_PKEY_EC) && ...)
2599 * ecdh_clnt_cert = 1;
2603 if (s->s3->peer_ecdh_tmp != NULL) {
2604 tkey = s->s3->peer_ecdh_tmp;
2606 /* Get the Server Public Key from Cert */
2607 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2608 if ((srvr_pub_pkey == NULL)
2609 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2610 || (srvr_pub_pkey->pkey.ec == NULL)) {
2611 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2612 ERR_R_INTERNAL_ERROR);
2616 tkey = srvr_pub_pkey->pkey.ec;
2619 srvr_group = EC_KEY_get0_group(tkey);
2620 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2622 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2623 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2624 ERR_R_INTERNAL_ERROR);
2628 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2629 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2630 ERR_R_MALLOC_FAILURE);
2634 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2635 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2638 if (ecdh_clnt_cert) {
2640 * Reuse key info from our certificate We only need our
2641 * private key to perform the ECDH computation.
2643 const BIGNUM *priv_key;
2644 tkey = s->cert->key->privatekey->pkey.ec;
2645 priv_key = EC_KEY_get0_private_key(tkey);
2646 if (priv_key == NULL) {
2647 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2648 ERR_R_MALLOC_FAILURE);
2651 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2652 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2656 /* Generate a new ECDH key pair */
2657 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2658 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2665 * use the 'p' output buffer for the ECDH key, but make sure to
2666 * clear it out afterwards
2669 field_size = EC_GROUP_get_degree(srvr_group);
2670 if (field_size <= 0) {
2671 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2674 pmslen = (field_size + 7) / 8;
2675 pms = OPENSSL_malloc(pmslen);
2678 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2679 if (n <= 0 || pmslen != (size_t)n) {
2680 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2684 if (ecdh_clnt_cert) {
2685 /* Send empty client key exch message */
2689 * First check the size of encoding and allocate memory
2693 EC_POINT_point2oct(srvr_group,
2694 EC_KEY_get0_public_key(clnt_ecdh),
2695 POINT_CONVERSION_UNCOMPRESSED,
2698 encodedPoint = (unsigned char *)
2699 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2700 bn_ctx = BN_CTX_new();
2701 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2702 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2703 ERR_R_MALLOC_FAILURE);
2707 /* Encode the public key */
2708 n = EC_POINT_point2oct(srvr_group,
2709 EC_KEY_get0_public_key(clnt_ecdh),
2710 POINT_CONVERSION_UNCOMPRESSED,
2711 encodedPoint, encoded_pt_len, bn_ctx);
2713 *p = n; /* length of encoded point */
2714 /* Encoded point will be copied here */
2716 /* copy the point */
2717 memcpy(p, encodedPoint, n);
2718 /* increment n to account for length field */
2722 /* Free allocated memory */
2723 BN_CTX_free(bn_ctx);
2724 OPENSSL_free(encodedPoint);
2725 EC_KEY_free(clnt_ecdh);
2726 EVP_PKEY_free(srvr_pub_pkey);
2728 #endif /* !OPENSSL_NO_EC */
2729 else if (alg_k & SSL_kGOST) {
2730 /* GOST key exchange message creation */
2731 EVP_PKEY_CTX *pkey_ctx;
2734 unsigned int md_len;
2735 unsigned char shared_ukm[32], tmp[256];
2736 EVP_MD_CTX *ukm_hash;
2740 pms = OPENSSL_malloc(pmslen);
2745 * Get server sertificate PKEY and create ctx from it
2747 peer_cert = s->session->peer;
2749 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2750 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2754 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2755 X509_get_pubkey(peer_cert), NULL);
2757 * If we have send a certificate, and certificate key
2759 * * parameters match those of server certificate, use
2760 * certificate key for key exchange
2763 /* Otherwise, generate ephemeral key pair */
2765 EVP_PKEY_encrypt_init(pkey_ctx);
2766 /* Generate session key */
2767 if (RAND_bytes(pms, pmslen) <= 0) {
2768 EVP_PKEY_CTX_free(pkey_ctx);
2769 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2770 ERR_R_INTERNAL_ERROR);
2774 * If we have client certificate, use its secret as peer key
2776 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2777 if (EVP_PKEY_derive_set_peer
2778 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2780 * If there was an error - just ignore it. Ephemeral key
2787 * Compute shared IV and store it in algorithm-specific context
2790 ukm_hash = EVP_MD_CTX_create();
2791 EVP_DigestInit(ukm_hash,
2792 EVP_get_digestbynid(NID_id_GostR3411_94));
2793 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2795 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2797 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2798 EVP_MD_CTX_destroy(ukm_hash);
2799 if (EVP_PKEY_CTX_ctrl
2800 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2802 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2806 /* Make GOST keytransport blob message */
2808 * Encapsulate it into sequence
2810 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2812 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2813 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2817 if (msglen >= 0x80) {
2819 *(p++) = msglen & 0xff;
2822 *(p++) = msglen & 0xff;
2825 memcpy(p, tmp, msglen);
2826 /* Check if pubkey from client certificate was used */
2827 if (EVP_PKEY_CTX_ctrl
2828 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2829 /* Set flag "skip certificate verify" */
2830 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2832 EVP_PKEY_CTX_free(pkey_ctx);
2833 EVP_PKEY_free(pub_key);
2836 #ifndef OPENSSL_NO_SRP
2837 else if (alg_k & SSL_kSRP) {
2838 if (s->srp_ctx.A != NULL) {
2839 /* send off the data */
2840 n = BN_num_bytes(s->srp_ctx.A);
2842 BN_bn2bin(s->srp_ctx.A, p);
2845 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2846 ERR_R_INTERNAL_ERROR);
2849 OPENSSL_free(s->session->srp_username);
2850 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2851 if (s->session->srp_username == NULL) {
2852 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2853 ERR_R_MALLOC_FAILURE);
2859 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2860 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2864 #ifndef OPENSSL_NO_PSK
2868 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2869 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2870 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2874 s->state = SSL3_ST_CW_KEY_EXCH_B;
2877 /* SSL3_ST_CW_KEY_EXCH_B */
2878 n = ssl_do_write(s);
2879 #ifndef OPENSSL_NO_SRP
2881 if (alg_k & SSL_kSRP) {
2883 * If everything written generate master key: no need to save PMS as
2884 * srp_generate_client_master_secret generates it internally.
2887 if (!srp_generate_client_master_secret(s)) {
2888 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2889 ERR_R_INTERNAL_ERROR);
2895 /* If we haven't written everything save PMS */
2897 s->s3->tmp.pms = pms;
2898 s->s3->tmp.pmslen = pmslen;
2900 /* If we don't have a PMS restore */
2902 pms = s->s3->tmp.pms;
2903 pmslen = s->s3->tmp.pmslen;
2905 if (pms == NULL && !(alg_k & SSL_kPSK)) {
2906 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2907 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2910 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2911 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2912 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2918 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2919 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2921 OPENSSL_clear_free(pms, pmslen);
2922 s->s3->tmp.pms = NULL;
2923 #ifndef OPENSSL_NO_EC
2924 BN_CTX_free(bn_ctx);
2925 OPENSSL_free(encodedPoint);
2926 EC_KEY_free(clnt_ecdh);
2927 EVP_PKEY_free(srvr_pub_pkey);
2929 #ifndef OPENSSL_NO_PSK
2930 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2931 s->s3->tmp.psk = NULL;
2933 s->state = SSL_ST_ERR;
2937 int ssl3_send_client_verify(SSL *s)
2940 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2942 EVP_PKEY_CTX *pctx = NULL;
2948 EVP_MD_CTX_init(&mctx);
2950 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2951 p = ssl_handshake_start(s);
2952 pkey = s->cert->key->privatekey;
2953 /* Create context from key and test if sha1 is allowed as digest */
2954 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2955 EVP_PKEY_sign_init(pctx);
2956 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2957 if (!SSL_USE_SIGALGS(s))
2958 s->method->ssl3_enc->cert_verify_mac(s,
2961 [MD5_DIGEST_LENGTH]));
2966 * For TLS v1.2 send signature algorithm and signature using agreed
2967 * digest and cached handshake records.
2969 if (SSL_USE_SIGALGS(s)) {
2972 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2973 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2974 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
2975 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2980 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
2983 if (!EVP_SignInit_ex(&mctx, md, NULL)
2984 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
2985 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2986 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
2991 /* Digest cached records and discard handshake buffer */
2992 if (!ssl3_digest_cached_records(s, 0))
2995 #ifndef OPENSSL_NO_RSA
2996 if (pkey->type == EVP_PKEY_RSA) {
2997 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
2998 if (RSA_sign(NID_md5_sha1, data,
2999 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3000 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3001 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3008 #ifndef OPENSSL_NO_DSA
3009 if (pkey->type == EVP_PKEY_DSA) {
3010 if (!DSA_sign(pkey->save_type,
3011 &(data[MD5_DIGEST_LENGTH]),
3012 SHA_DIGEST_LENGTH, &(p[2]),
3013 (unsigned int *)&j, pkey->pkey.dsa)) {
3014 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3021 #ifndef OPENSSL_NO_EC
3022 if (pkey->type == EVP_PKEY_EC) {
3023 if (!ECDSA_sign(pkey->save_type,
3024 &(data[MD5_DIGEST_LENGTH]),
3025 SHA_DIGEST_LENGTH, &(p[2]),
3026 (unsigned int *)&j, pkey->pkey.ec)) {
3027 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3034 if (pkey->type == NID_id_GostR3410_94
3035 || pkey->type == NID_id_GostR3410_2001) {
3036 unsigned char signbuf[64];
3038 size_t sigsize = 64;
3039 s->method->ssl3_enc->cert_verify_mac(s,
3040 NID_id_GostR3411_94, data);
3041 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3042 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3045 for (i = 63, j = 0; i >= 0; j++, i--) {
3046 p[2 + j] = signbuf[i];
3051 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3054 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3055 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3058 s->state = SSL3_ST_CW_CERT_VRFY_B;
3060 EVP_MD_CTX_cleanup(&mctx);
3061 EVP_PKEY_CTX_free(pctx);
3062 return ssl_do_write(s);
3064 EVP_MD_CTX_cleanup(&mctx);
3065 EVP_PKEY_CTX_free(pctx);
3066 s->state = SSL_ST_ERR;
3071 * Check a certificate can be used for client authentication. Currently check
3072 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3073 * certificates can be used and optionally checks suitability for Suite B.
3075 static int ssl3_check_client_certificate(SSL *s)
3077 unsigned long alg_k;
3078 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3080 /* If no suitable signature algorithm can't use certificate */
3081 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3084 * If strict mode check suitability of chain before using it. This also
3085 * adjusts suite B digest if necessary.
3087 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3088 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3090 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3091 /* See if we can use client certificate for fixed DH */
3092 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3093 int i = s->session->peer_type;
3094 EVP_PKEY *clkey = NULL, *spkey = NULL;
3095 clkey = s->cert->key->privatekey;
3096 /* If client key not DH assume it can be used */
3097 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3100 spkey = X509_get_pubkey(s->session->peer);
3102 /* Compare server and client parameters */
3103 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3104 EVP_PKEY_free(spkey);
3108 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3113 int ssl3_send_client_certificate(SSL *s)
3116 EVP_PKEY *pkey = NULL;
3119 if (s->state == SSL3_ST_CW_CERT_A) {
3120 /* Let cert callback update client certificates if required */
3121 if (s->cert->cert_cb) {
3122 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3124 s->rwstate = SSL_X509_LOOKUP;
3128 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3129 s->state = SSL_ST_ERR;
3132 s->rwstate = SSL_NOTHING;
3134 if (ssl3_check_client_certificate(s))
3135 s->state = SSL3_ST_CW_CERT_C;
3137 s->state = SSL3_ST_CW_CERT_B;
3140 /* We need to get a client cert */
3141 if (s->state == SSL3_ST_CW_CERT_B) {
3143 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3144 * return(-1); We then get retied later
3147 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3149 s->rwstate = SSL_X509_LOOKUP;
3152 s->rwstate = SSL_NOTHING;
3153 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3154 s->state = SSL3_ST_CW_CERT_B;
3155 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3157 } else if (i == 1) {
3159 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3160 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3164 EVP_PKEY_free(pkey);
3165 if (i && !ssl3_check_client_certificate(s))
3168 if (s->version == SSL3_VERSION) {
3169 s->s3->tmp.cert_req = 0;
3170 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3173 s->s3->tmp.cert_req = 2;
3174 if (!ssl3_digest_cached_records(s, 0)) {
3175 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3176 s->state = SSL_ST_ERR;
3182 /* Ok, we have a cert */
3183 s->state = SSL3_ST_CW_CERT_C;
3186 if (s->state == SSL3_ST_CW_CERT_C) {
3187 s->state = SSL3_ST_CW_CERT_D;
3188 if (!ssl3_output_cert_chain(s,
3189 (s->s3->tmp.cert_req ==
3190 2) ? NULL : s->cert->key)) {
3191 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3192 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3193 s->state = SSL_ST_ERR;
3197 /* SSL3_ST_CW_CERT_D */
3198 return ssl_do_write(s);
3201 #define has_bits(i,m) (((i)&(m)) == (m))
3203 int ssl3_check_cert_and_algorithm(SSL *s)
3207 EVP_PKEY *pkey = NULL;
3209 #ifndef OPENSSL_NO_RSA
3212 #ifndef OPENSSL_NO_DH
3215 int al = SSL_AD_HANDSHAKE_FAILURE;
3217 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3218 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3220 /* we don't have a certificate */
3221 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3223 #ifndef OPENSSL_NO_RSA
3224 rsa = s->s3->peer_rsa_tmp;
3226 #ifndef OPENSSL_NO_DH
3227 dh = s->s3->peer_dh_tmp;
3230 /* This is the passed certificate */
3232 idx = s->session->peer_type;
3233 #ifndef OPENSSL_NO_EC
3234 if (idx == SSL_PKEY_ECC) {
3235 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3237 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3242 } else if (alg_a & SSL_aECDSA) {
3243 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3244 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3246 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3247 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3251 pkey = X509_get_pubkey(s->session->peer);
3252 pkey_bits = EVP_PKEY_bits(pkey);
3253 i = X509_certificate_type(s->session->peer, pkey);
3254 EVP_PKEY_free(pkey);
3256 /* Check that we have a certificate if we require one */
3257 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3258 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3259 SSL_R_MISSING_RSA_SIGNING_CERT);
3262 #ifndef OPENSSL_NO_DSA
3263 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3264 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3265 SSL_R_MISSING_DSA_SIGNING_CERT);
3269 #ifndef OPENSSL_NO_RSA
3270 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3271 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3272 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3273 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3274 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3276 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3277 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3278 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3279 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3280 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3284 /* server key exchange is not allowed. */
3285 al = SSL_AD_INTERNAL_ERROR;
3286 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3293 #ifndef OPENSSL_NO_DH
3294 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3295 al = SSL_AD_INTERNAL_ERROR;
3296 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3298 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3299 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3300 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3301 SSL_R_MISSING_DH_RSA_CERT);
3304 # ifndef OPENSSL_NO_DSA
3305 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3306 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3307 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3308 SSL_R_MISSING_DH_DSA_CERT);
3314 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3315 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3316 #ifndef OPENSSL_NO_RSA
3317 if (alg_k & SSL_kRSA) {
3319 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3320 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3322 } else if (RSA_bits(rsa) >
3323 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3324 /* We have a temporary RSA key but it's too large. */
3325 al = SSL_AD_EXPORT_RESTRICTION;
3326 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3327 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3332 #ifndef OPENSSL_NO_DH
3333 if (alg_k & SSL_kDHE) {
3335 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3336 /* We have a temporary DH key but it's too large. */
3337 al = SSL_AD_EXPORT_RESTRICTION;
3338 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3339 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3342 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3343 /* The cert should have had an export DH key. */
3344 al = SSL_AD_EXPORT_RESTRICTION;
3345 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3346 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3351 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3352 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3358 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3363 * Normally, we can tell if the server is resuming the session from
3364 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3365 * message after the ServerHello to determine if the server is resuming.
3366 * Therefore, we allow EAP-FAST to peek ahead.
3367 * ssl3_check_finished returns 1 if we are resuming from an external
3368 * pre-shared secret, we have a "ticket" and the next server handshake message
3369 * is Finished; and 0 otherwise. It returns -1 upon an error.
3371 static int ssl3_check_finished(SSL *s)
3375 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3376 !s->session->tlsext_tick)
3379 /* Need to permit this temporarily, in case the next message is Finished. */
3380 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3382 * This function is called when we might get a Certificate message instead,
3383 * so permit appropriate message length.
3384 * We ignore the return value as we're only interested in the message type
3385 * and not its length.
3387 s->method->ssl_get_message(s,
3390 -1, s->max_cert_list, &ok);
3391 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3396 s->s3->tmp.reuse_message = 1;
3398 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3401 /* If we're not done, then the CCS arrived early and we should bail. */
3402 if (s->s3->change_cipher_spec) {
3403 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3404 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3411 #ifndef OPENSSL_NO_NEXTPROTONEG
3412 int ssl3_send_next_proto(SSL *s)
3414 unsigned int len, padding_len;
3417 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3418 len = s->next_proto_negotiated_len;
3419 padding_len = 32 - ((len + 2) % 32);
3420 d = (unsigned char *)s->init_buf->data;
3422 memcpy(d + 5, s->next_proto_negotiated, len);
3423 d[5 + len] = padding_len;
3424 memset(d + 6 + len, 0, padding_len);
3425 *(d++) = SSL3_MT_NEXT_PROTO;
3426 l2n3(2 + len + padding_len, d);
3427 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3428 s->init_num = 4 + 2 + len + padding_len;
3432 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3436 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3439 #ifndef OPENSSL_NO_ENGINE
3440 if (s->ctx->client_cert_engine) {
3441 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3442 SSL_get_client_CA_list(s),
3443 px509, ppkey, NULL, NULL, NULL);
3448 if (s->ctx->client_cert_cb)
3449 i = s->ctx->client_cert_cb(s, px509, ppkey);
3453 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3455 int (*put_cb) (const SSL_CIPHER *,
3461 int empty_reneg_info_scsv = !s->renegotiate;
3462 /* Set disabled masks for this session */
3463 ssl_set_client_disabled(s);
3469 put_cb = s->method->put_cipher_by_char;
3471 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3472 c = sk_SSL_CIPHER_value(sk, i);
3473 /* Skip disabled ciphers */
3474 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3476 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3477 if (c->id == SSL3_CK_SCSV) {
3478 if (!empty_reneg_info_scsv)
3481 empty_reneg_info_scsv = 0;
3488 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3492 if (empty_reneg_info_scsv) {
3493 static SSL_CIPHER scsv = {
3494 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3496 j = put_cb(&scsv, p);
3498 #ifdef OPENSSL_RI_DEBUG
3500 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3503 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3504 static SSL_CIPHER scsv = {
3505 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3507 j = put_cb(&scsv, p);