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_change(SSL *s);
169 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
173 int ssl3_connect(SSL *s)
176 unsigned long Time = (unsigned long)time(NULL);
177 void (*cb) (const SSL *ssl, int type, int val) = NULL;
179 int new_state, state, skip = 0;
181 RAND_add(&Time, sizeof(Time), 0);
185 if (s->info_callback != NULL)
186 cb = s->info_callback;
187 else if (s->ctx->info_callback != NULL)
188 cb = s->ctx->info_callback;
191 if (!SSL_in_init(s) || SSL_in_before(s)) {
196 #ifndef OPENSSL_NO_HEARTBEATS
198 * If we're awaiting a HeartbeatResponse, pretend we already got and
199 * don't await it anymore, because Heartbeats don't make sense during
202 if (s->tlsext_hb_pending) {
203 s->tlsext_hb_pending = 0;
212 case SSL_ST_RENEGOTIATE:
214 s->state = SSL_ST_CONNECT;
215 s->ctx->stats.sess_connect_renegotiate++;
219 case SSL_ST_BEFORE | SSL_ST_CONNECT:
220 case SSL_ST_OK | SSL_ST_CONNECT:
224 cb(s, SSL_CB_HANDSHAKE_START, 1);
226 if ((s->version >> 8) != SSL3_VERSION_MAJOR
227 && s->version != TLS_ANY_VERSION) {
228 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
229 s->state = SSL_ST_ERR;
234 if (s->version != TLS_ANY_VERSION &&
235 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
236 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
240 /* s->version=SSL3_VERSION; */
241 s->type = SSL_ST_CONNECT;
243 if (s->init_buf == NULL) {
244 if ((buf = BUF_MEM_new()) == NULL) {
246 s->state = SSL_ST_ERR;
249 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
251 s->state = SSL_ST_ERR;
258 if (!ssl3_setup_buffers(s)) {
263 /* setup buffing BIO */
264 if (!ssl_init_wbio_buffer(s, 0)) {
266 s->state = SSL_ST_ERR;
270 /* don't push the buffering BIO quite yet */
272 ssl3_init_finished_mac(s);
274 s->state = SSL3_ST_CW_CLNT_HELLO_A;
275 s->ctx->stats.sess_connect++;
278 * Should have been reset by ssl3_get_finished, too.
280 s->s3->change_cipher_spec = 0;
283 case SSL3_ST_CW_CLNT_HELLO_A:
284 case SSL3_ST_CW_CLNT_HELLO_B:
287 ret = ssl3_client_hello(s);
290 s->state = SSL3_ST_CR_SRVR_HELLO_A;
293 /* turn on buffering for the next lot of output */
294 if (s->bbio != s->wbio)
295 s->wbio = BIO_push(s->bbio, s->wbio);
299 case SSL3_ST_CR_SRVR_HELLO_A:
300 case SSL3_ST_CR_SRVR_HELLO_B:
301 ret = ssl3_get_server_hello(s);
306 s->state = SSL3_ST_CR_CHANGE_A;
307 if (s->tlsext_ticket_expected) {
308 /* receive renewed session ticket */
309 s->state = SSL3_ST_CR_SESSION_TICKET_A;
312 s->state = SSL3_ST_CR_CERT_A;
316 case SSL3_ST_CR_CERT_A:
317 case SSL3_ST_CR_CERT_B:
318 /* Noop (ret = 0) for everything but EAP-FAST. */
319 ret = ssl3_check_change(s);
324 s->state = SSL3_ST_CR_CHANGE_A;
329 /* Check if it is anon DH/ECDH, SRP auth */
331 if (!(s->s3->tmp.new_cipher->algorithm_auth &
332 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
333 ret = ssl3_get_server_certificate(s);
337 if (s->tlsext_status_expected)
338 s->state = SSL3_ST_CR_CERT_STATUS_A;
340 s->state = SSL3_ST_CR_KEY_EXCH_A;
343 s->state = SSL3_ST_CR_KEY_EXCH_A;
349 case SSL3_ST_CR_KEY_EXCH_A:
350 case SSL3_ST_CR_KEY_EXCH_B:
351 ret = ssl3_get_key_exchange(s);
354 s->state = SSL3_ST_CR_CERT_REQ_A;
358 * at this point we check that we have the required stuff from
361 if (!ssl3_check_cert_and_algorithm(s)) {
363 s->state = SSL_ST_ERR;
368 case SSL3_ST_CR_CERT_REQ_A:
369 case SSL3_ST_CR_CERT_REQ_B:
370 ret = ssl3_get_certificate_request(s);
373 s->state = SSL3_ST_CR_SRVR_DONE_A;
377 case SSL3_ST_CR_SRVR_DONE_A:
378 case SSL3_ST_CR_SRVR_DONE_B:
379 ret = ssl3_get_server_done(s);
382 #ifndef OPENSSL_NO_SRP
383 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
384 if ((ret = SRP_Calc_A_param(s)) <= 0) {
385 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
386 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
387 s->state = SSL_ST_ERR;
392 if (s->s3->tmp.cert_req)
393 s->state = SSL3_ST_CW_CERT_A;
395 s->state = SSL3_ST_CW_KEY_EXCH_A;
400 case SSL3_ST_CW_CERT_A:
401 case SSL3_ST_CW_CERT_B:
402 case SSL3_ST_CW_CERT_C:
403 case SSL3_ST_CW_CERT_D:
404 ret = ssl3_send_client_certificate(s);
407 s->state = SSL3_ST_CW_KEY_EXCH_A;
411 case SSL3_ST_CW_KEY_EXCH_A:
412 case SSL3_ST_CW_KEY_EXCH_B:
413 ret = ssl3_send_client_key_exchange(s);
417 * EAY EAY EAY need to check for DH fix cert sent back
420 * For TLS, cert_req is set to 2, so a cert chain of nothing is
421 * sent, but no verify packet is sent
424 * XXX: For now, we do not support client authentication in ECDH
425 * cipher suites with ECDH (rather than ECDSA) certificates. We
426 * need to skip the certificate verify message when client's
427 * ECDH public key is sent inside the client certificate.
429 if (s->s3->tmp.cert_req == 1) {
430 s->state = SSL3_ST_CW_CERT_VRFY_A;
432 s->state = SSL3_ST_CW_CHANGE_A;
434 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
435 s->state = SSL3_ST_CW_CHANGE_A;
441 case SSL3_ST_CW_CERT_VRFY_A:
442 case SSL3_ST_CW_CERT_VRFY_B:
443 ret = ssl3_send_client_verify(s);
446 s->state = SSL3_ST_CW_CHANGE_A;
450 case SSL3_ST_CW_CHANGE_A:
451 case SSL3_ST_CW_CHANGE_B:
452 ret = ssl3_send_change_cipher_spec(s,
454 SSL3_ST_CW_CHANGE_B);
458 #if defined(OPENSSL_NO_NEXTPROTONEG)
459 s->state = SSL3_ST_CW_FINISHED_A;
461 if (s->s3->next_proto_neg_seen)
462 s->state = SSL3_ST_CW_NEXT_PROTO_A;
464 s->state = SSL3_ST_CW_FINISHED_A;
468 s->session->cipher = s->s3->tmp.new_cipher;
469 #ifdef OPENSSL_NO_COMP
470 s->session->compress_meth = 0;
472 if (s->s3->tmp.new_compression == NULL)
473 s->session->compress_meth = 0;
475 s->session->compress_meth = s->s3->tmp.new_compression->id;
477 if (!s->method->ssl3_enc->setup_key_block(s)) {
479 s->state = SSL_ST_ERR;
483 if (!s->method->ssl3_enc->change_cipher_state(s,
484 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
487 s->state = SSL_ST_ERR;
493 #if !defined(OPENSSL_NO_NEXTPROTONEG)
494 case SSL3_ST_CW_NEXT_PROTO_A:
495 case SSL3_ST_CW_NEXT_PROTO_B:
496 ret = ssl3_send_next_proto(s);
499 s->state = SSL3_ST_CW_FINISHED_A;
503 case SSL3_ST_CW_FINISHED_A:
504 case SSL3_ST_CW_FINISHED_B:
505 ret = ssl3_send_finished(s,
506 SSL3_ST_CW_FINISHED_A,
507 SSL3_ST_CW_FINISHED_B,
509 ssl3_enc->client_finished_label,
511 ssl3_enc->client_finished_label_len);
514 s->state = SSL3_ST_CW_FLUSH;
517 s->s3->tmp.next_state = SSL_ST_OK;
520 * Allow NewSessionTicket if ticket expected
522 if (s->tlsext_ticket_expected)
523 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
525 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE_A;
530 case SSL3_ST_CR_SESSION_TICKET_A:
531 case SSL3_ST_CR_SESSION_TICKET_B:
532 ret = ssl3_get_new_session_ticket(s);
535 s->state = SSL3_ST_CR_CHANGE_A;
539 case SSL3_ST_CR_CERT_STATUS_A:
540 case SSL3_ST_CR_CERT_STATUS_B:
541 ret = ssl3_get_cert_status(s);
544 s->state = SSL3_ST_CR_KEY_EXCH_A;
548 case SSL3_ST_CR_CHANGE_A:
549 case SSL3_ST_CR_CHANGE_B:
550 ret = ssl3_get_change_cipher_spec(s, SSL3_ST_CR_CHANGE_A,
551 SSL3_ST_CR_CHANGE_B);
555 s->state = SSL3_ST_CR_FINISHED_A;
559 case SSL3_ST_CR_FINISHED_A:
560 case SSL3_ST_CR_FINISHED_B:
561 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
562 SSL3_ST_CR_FINISHED_B);
567 s->state = SSL3_ST_CW_CHANGE_A;
569 s->state = SSL_ST_OK;
573 case SSL3_ST_CW_FLUSH:
574 s->rwstate = SSL_WRITING;
575 if (BIO_flush(s->wbio) <= 0) {
579 s->rwstate = SSL_NOTHING;
580 s->state = s->s3->tmp.next_state;
584 /* clean a few things up */
585 ssl3_cleanup_key_block(s);
586 BUF_MEM_free(s->init_buf);
589 /* remove the buffering */
590 ssl_free_wbio_buffer(s);
596 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
598 s->ctx->stats.sess_hit++;
602 s->handshake_func = ssl3_connect;
603 s->ctx->stats.sess_connect_good++;
606 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
613 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
619 /* did we do anything */
620 if (!s->s3->tmp.reuse_message && !skip) {
622 if ((ret = BIO_flush(s->wbio)) <= 0)
626 if ((cb != NULL) && (s->state != state)) {
627 new_state = s->state;
629 cb(s, SSL_CB_CONNECT_LOOP, 1);
630 s->state = new_state;
639 cb(s, SSL_CB_CONNECT_EXIT, ret);
644 * Work out what version we should be using for the initial ClientHello if
645 * the version is currently set to (D)TLS_ANY_VERSION.
646 * Returns 1 on success
649 static int ssl_set_version(SSL *s)
651 unsigned long mask, options = s->options;
653 if (s->method->version == TLS_ANY_VERSION) {
655 * SSL_OP_NO_X disables all protocols above X *if* there are
656 * some protocols below X enabled. This is required in order
657 * to maintain "version capability" vector contiguous. So
658 * that if application wants to disable TLS1.0 in favour of
659 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
660 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
662 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
663 #if !defined(OPENSSL_NO_SSL3)
667 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
668 if (options & SSL_OP_NO_TLSv1_2) {
669 if ((options & mask) != mask) {
670 s->version = TLS1_1_VERSION;
672 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
676 s->version = TLS1_2_VERSION;
679 if ((options & mask) == mask) {
680 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
683 s->version = TLS1_1_VERSION;
686 mask &= ~SSL_OP_NO_TLSv1_1;
687 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
688 s->version = TLS1_VERSION;
689 mask &= ~SSL_OP_NO_TLSv1;
690 #if !defined(OPENSSL_NO_SSL3)
691 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
692 s->version = SSL3_VERSION;
695 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
696 SSLerr(SSL_F_SSL_SET_VERSION,
697 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
701 if (s->version == SSL3_VERSION && FIPS_mode()) {
702 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
706 } else if (s->method->version == DTLS_ANY_VERSION) {
707 /* Determine which DTLS version to use */
708 /* If DTLS 1.2 disabled correct the version number */
709 if (options & SSL_OP_NO_DTLSv1_2) {
710 if (tls1_suiteb(s)) {
711 SSLerr(SSL_F_SSL_SET_VERSION,
712 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
716 * Disabling all versions is silly: return an error.
718 if (options & SSL_OP_NO_DTLSv1) {
719 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
723 * Update method so we don't use any DTLS 1.2 features.
725 s->method = DTLSv1_client_method();
726 s->version = DTLS1_VERSION;
729 * We only support one version: update method
731 if (options & SSL_OP_NO_DTLSv1)
732 s->method = DTLSv1_2_client_method();
733 s->version = DTLS1_2_VERSION;
737 s->client_version = s->version;
742 int ssl3_client_hello(SSL *s)
745 unsigned char *p, *d;
749 #ifndef OPENSSL_NO_COMP
754 buf = (unsigned char *)s->init_buf->data;
755 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
756 SSL_SESSION *sess = s->session;
758 /* Work out what SSL/TLS/DTLS version to use */
759 if (ssl_set_version(s) == 0)
762 if ((sess == NULL) || (sess->ssl_version != s->version) ||
764 * In the case of EAP-FAST, we can have a pre-shared
765 * "ticket" without a session ID.
767 (!sess->session_id_length && !sess->tlsext_tick) ||
768 (sess->not_resumable)) {
769 if (!ssl_get_new_session(s, 0))
772 /* else use the pre-loaded session */
774 p = s->s3->client_random;
777 * for DTLS if client_random is initialized, reuse it, we are
778 * required to use same upon reply to HelloVerify
780 if (SSL_IS_DTLS(s)) {
783 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
792 if (i && ssl_fill_hello_random(s, 0, p,
793 sizeof(s->s3->client_random)) <= 0)
796 /* Do the message type and length last */
797 d = p = ssl_handshake_start(s);
800 * version indicates the negotiated version: for example from
801 * an SSLv2/v3 compatible client hello). The client_version
802 * field is the maximum version we permit and it is also
803 * used in RSA encrypted premaster secrets. Some servers can
804 * choke if we initially report a higher version then
805 * renegotiate to a lower one in the premaster secret. This
806 * didn't happen with TLS 1.0 as most servers supported it
807 * but it can with TLS 1.1 or later if the server only supports
810 * Possible scenario with previous logic:
811 * 1. Client hello indicates TLS 1.2
812 * 2. Server hello says TLS 1.0
813 * 3. RSA encrypted premaster secret uses 1.2.
814 * 4. Handhaked proceeds using TLS 1.0.
815 * 5. Server sends hello request to renegotiate.
816 * 6. Client hello indicates TLS v1.0 as we now
817 * know that is maximum server supports.
818 * 7. Server chokes on RSA encrypted premaster secret
819 * containing version 1.0.
821 * For interoperability it should be OK to always use the
822 * maximum version we support in client hello and then rely
823 * on the checking of version to ensure the servers isn't
824 * being inconsistent: for example initially negotiating with
825 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
826 * client_version in client hello and not resetting it to
827 * the negotiated version.
829 *(p++) = s->client_version >> 8;
830 *(p++) = s->client_version & 0xff;
833 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
834 p += SSL3_RANDOM_SIZE;
840 i = s->session->session_id_length;
843 if (i > (int)sizeof(s->session->session_id)) {
844 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
847 memcpy(p, s->session->session_id, i);
851 /* cookie stuff for DTLS */
852 if (SSL_IS_DTLS(s)) {
853 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
854 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
857 *(p++) = s->d1->cookie_len;
858 memcpy(p, s->d1->cookie, s->d1->cookie_len);
859 p += s->d1->cookie_len;
862 /* Ciphers supported */
863 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
865 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
868 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
870 * Some servers hang if client hello > 256 bytes as hack workaround
871 * chop number of supported ciphers to keep it well below this if we
874 if (TLS1_get_version(s) >= TLS1_2_VERSION
875 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
876 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
882 #ifdef OPENSSL_NO_COMP
886 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
889 j = sk_SSL_COMP_num(s->ctx->comp_methods);
891 for (i = 0; i < j; i++) {
892 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
896 *(p++) = 0; /* Add the NULL method */
899 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
900 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
904 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
906 ssl3_send_alert(s, SSL3_AL_FATAL, al);
907 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
912 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
913 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
914 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
917 s->state = SSL3_ST_CW_CLNT_HELLO_B;
920 /* SSL3_ST_CW_CLNT_HELLO_B */
921 return ssl_do_write(s);
923 s->state = SSL_ST_ERR;
927 int ssl3_get_server_hello(SSL *s)
929 STACK_OF(SSL_CIPHER) *sk;
932 unsigned char *session_id, *cipherchars;
933 int i, al = SSL_AD_INTERNAL_ERROR, ok;
936 #ifndef OPENSSL_NO_COMP
940 * Hello verify request and/or server hello version may not match so set
941 * first packet if we're negotiating version.
945 n = s->method->ssl_get_message(s,
946 SSL3_ST_CR_SRVR_HELLO_A,
947 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
953 if (SSL_IS_DTLS(s)) {
954 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
955 if (s->d1->send_cookie == 0) {
956 s->s3->tmp.reuse_message = 1;
958 } else { /* already sent a cookie */
960 al = SSL_AD_UNEXPECTED_MESSAGE;
961 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
967 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
968 al = SSL_AD_UNEXPECTED_MESSAGE;
969 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
973 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
974 al = SSL_AD_INTERNAL_ERROR;
975 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
979 if (s->method->version == TLS_ANY_VERSION) {
980 unsigned int sversion;
982 if (!PACKET_get_net_2(&pkt, &sversion)) {
983 al = SSL_AD_DECODE_ERROR;
984 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
988 #if TLS_MAX_VERSION != TLS1_2_VERSION
989 #error Code needs updating for new TLS version
991 #ifndef OPENSSL_NO_SSL3
992 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
994 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
995 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
996 al = SSL_AD_PROTOCOL_VERSION;
999 s->method = SSLv3_client_method();
1002 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1003 s->method = TLSv1_client_method();
1004 } else if ((sversion == TLS1_1_VERSION) &&
1005 !(s->options & SSL_OP_NO_TLSv1_1)) {
1006 s->method = TLSv1_1_client_method();
1007 } else if ((sversion == TLS1_2_VERSION) &&
1008 !(s->options & SSL_OP_NO_TLSv1_2)) {
1009 s->method = TLSv1_2_client_method();
1011 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1012 al = SSL_AD_PROTOCOL_VERSION;
1015 s->session->ssl_version = s->version = s->method->version;
1017 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1018 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1019 al = SSL_AD_PROTOCOL_VERSION;
1022 } else if (s->method->version == DTLS_ANY_VERSION) {
1023 /* Work out correct protocol version to use */
1024 unsigned int hversion;
1027 if (!PACKET_get_net_2(&pkt, &hversion)) {
1028 al = SSL_AD_DECODE_ERROR;
1029 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1033 options = s->options;
1034 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1035 s->method = DTLSv1_2_client_method();
1036 else if (tls1_suiteb(s)) {
1037 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1038 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1039 s->version = hversion;
1040 al = SSL_AD_PROTOCOL_VERSION;
1042 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1043 s->method = DTLSv1_client_method();
1045 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1046 s->version = hversion;
1047 al = SSL_AD_PROTOCOL_VERSION;
1050 s->session->ssl_version = s->version = s->method->version;
1052 unsigned char *vers;
1054 if (!PACKET_get_bytes(&pkt, &vers, 2)) {
1055 al = SSL_AD_DECODE_ERROR;
1056 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1059 if ((vers[0] != (s->version >> 8))
1060 || (vers[1] != (s->version & 0xff))) {
1061 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1062 s->version = (s->version & 0xff00) | vers[1];
1063 al = SSL_AD_PROTOCOL_VERSION;
1068 /* load the server hello data */
1069 /* load the server random */
1070 if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1071 al = SSL_AD_DECODE_ERROR;
1072 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1078 /* get the session-id length */
1079 if (!PACKET_get_1(&pkt, &j)
1080 || (j > sizeof s->session->session_id)
1081 || (j > SSL3_SESSION_ID_SIZE)) {
1082 al = SSL_AD_ILLEGAL_PARAMETER;
1083 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1088 * Check if we can resume the session based on external pre-shared secret.
1089 * EAP-FAST (RFC 4851) supports two types of session resumption.
1090 * Resumption based on server-side state works with session IDs.
1091 * Resumption based on pre-shared Protected Access Credentials (PACs)
1092 * works by overriding the SessionTicket extension at the application
1093 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1094 * servers would honour the session ID.) Therefore, the session ID alone
1095 * is not a reliable indicator of session resumption, so we first check if
1096 * we can resume, and later peek at the next handshake message to see if the
1097 * server wants to resume.
1099 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1100 s->session->tlsext_tick) {
1101 SSL_CIPHER *pref_cipher = NULL;
1102 PACKET bookmark = pkt;
1103 if (!PACKET_forward(&pkt, j)
1104 || !PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {
1105 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1106 al = SSL_AD_DECODE_ERROR;
1109 s->session->master_key_length = sizeof(s->session->master_key);
1110 if (s->tls_session_secret_cb(s, s->session->master_key,
1111 &s->session->master_key_length,
1113 s->tls_session_secret_cb_arg)) {
1114 s->session->cipher = pref_cipher ?
1115 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1117 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1118 al = SSL_AD_INTERNAL_ERROR;
1124 /* Get the session id */
1125 if (!PACKET_get_bytes(&pkt, &session_id, j)) {
1126 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1127 al = SSL_AD_DECODE_ERROR;
1131 if (j != 0 && j == s->session->session_id_length
1132 && memcmp(session_id, s->session->session_id, j) == 0) {
1133 if (s->sid_ctx_length != s->session->sid_ctx_length
1134 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1135 /* actually a client application bug */
1136 al = SSL_AD_ILLEGAL_PARAMETER;
1137 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1138 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1144 * If we were trying for session-id reuse but the server
1145 * didn't echo the ID, make a new SSL_SESSION.
1146 * In the case of EAP-FAST and PAC, we do not send a session ID,
1147 * so the PAC-based session secret is always preserved. It'll be
1148 * overwritten if the server refuses resumption.
1150 if (s->session->session_id_length > 0) {
1151 if (!ssl_get_new_session(s, 0)) {
1155 s->session->session_id_length = j;
1156 memcpy(s->session->session_id, session_id, j); /* j could be 0 */
1159 if (!PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {
1160 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1161 al = SSL_AD_DECODE_ERROR;
1164 c = ssl_get_cipher_by_char(s, cipherchars);
1166 /* unknown cipher */
1167 al = SSL_AD_ILLEGAL_PARAMETER;
1168 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1171 /* Set version disabled mask now we know version */
1172 if (!SSL_USE_TLS1_2_CIPHERS(s))
1173 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1175 s->s3->tmp.mask_ssl = 0;
1177 * If it is a disabled cipher we didn't send it in client hello, so
1180 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1181 al = SSL_AD_ILLEGAL_PARAMETER;
1182 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1186 sk = ssl_get_ciphers_by_id(s);
1187 i = sk_SSL_CIPHER_find(sk, c);
1189 /* we did not say we would use this cipher */
1190 al = SSL_AD_ILLEGAL_PARAMETER;
1191 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1196 * Depending on the session caching (internal/external), the cipher
1197 * and/or cipher_id values may not be set. Make sure that cipher_id is
1198 * set and use it for comparison.
1200 if (s->session->cipher)
1201 s->session->cipher_id = s->session->cipher->id;
1202 if (s->hit && (s->session->cipher_id != c->id)) {
1203 al = SSL_AD_ILLEGAL_PARAMETER;
1204 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1205 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1208 s->s3->tmp.new_cipher = c;
1210 * Don't digest cached records if no sigalgs: we may need them for client
1213 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1215 /* lets get the compression algorithm */
1217 if (!PACKET_get_1(&pkt, &j)) {
1218 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1219 al = SSL_AD_DECODE_ERROR;
1222 #ifdef OPENSSL_NO_COMP
1224 al = SSL_AD_ILLEGAL_PARAMETER;
1225 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1226 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1230 * If compression is disabled we'd better not try to resume a session
1231 * using compression.
1233 if (s->session->compress_meth != 0) {
1234 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1238 if (s->hit && j != s->session->compress_meth) {
1239 al = SSL_AD_ILLEGAL_PARAMETER;
1240 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1241 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1246 else if (!ssl_allow_compression(s)) {
1247 al = SSL_AD_ILLEGAL_PARAMETER;
1248 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1251 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1253 if ((j != 0) && (comp == NULL)) {
1254 al = SSL_AD_ILLEGAL_PARAMETER;
1255 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1256 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1259 s->s3->tmp.new_compression = comp;
1263 /* TLS extensions */
1264 if (!ssl_parse_serverhello_tlsext(s, &pkt)) {
1265 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1269 if (PACKET_remaining(&pkt) != 0) {
1270 /* wrong packet length */
1271 al = SSL_AD_DECODE_ERROR;
1272 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1278 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1280 s->state = SSL_ST_ERR;
1284 int ssl3_get_server_certificate(SSL *s)
1286 int al, i, ok, ret = -1, exp_idx;
1287 unsigned long n, cert_list_len, cert_len;
1289 unsigned char *certstart, *certbytes;
1290 STACK_OF(X509) *sk = NULL;
1291 EVP_PKEY *pkey = NULL;
1294 n = s->method->ssl_get_message(s,
1297 -1, s->max_cert_list, &ok);
1302 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1303 s->s3->tmp.reuse_message = 1;
1307 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1308 al = SSL_AD_UNEXPECTED_MESSAGE;
1309 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1313 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1314 al = SSL_AD_INTERNAL_ERROR;
1315 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1319 if ((sk = sk_X509_new_null()) == NULL) {
1320 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1324 if (!PACKET_get_net_3(&pkt, &cert_list_len)
1325 || PACKET_remaining(&pkt) != cert_list_len) {
1326 al = SSL_AD_DECODE_ERROR;
1327 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1330 while (PACKET_remaining(&pkt)) {
1331 if (!PACKET_get_net_3(&pkt, &cert_len)
1332 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
1333 al = SSL_AD_DECODE_ERROR;
1334 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1335 SSL_R_CERT_LENGTH_MISMATCH);
1339 certstart = certbytes;
1340 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1342 al = SSL_AD_BAD_CERTIFICATE;
1343 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1346 if (certbytes != (certstart + cert_len)) {
1347 al = SSL_AD_DECODE_ERROR;
1348 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1349 SSL_R_CERT_LENGTH_MISMATCH);
1352 if (!sk_X509_push(sk, x)) {
1353 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1359 i = ssl_verify_cert_chain(s, sk);
1360 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1361 al = ssl_verify_alarm_type(s->verify_result);
1362 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1363 SSL_R_CERTIFICATE_VERIFY_FAILED);
1366 ERR_clear_error(); /* but we keep s->verify_result */
1368 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1369 al = SSL_AD_HANDSHAKE_FAILURE;
1373 s->session->peer_chain = sk;
1375 * Inconsistency alert: cert_chain does include the peer's certificate,
1376 * which we don't include in s3_srvr.c
1378 x = sk_X509_value(sk, 0);
1381 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1384 pkey = X509_get_pubkey(x);
1386 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1389 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1390 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1394 i = ssl_cert_type(x, pkey);
1398 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1399 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1403 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1404 if (exp_idx >= 0 && i != exp_idx) {
1406 al = SSL_AD_ILLEGAL_PARAMETER;
1407 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1408 SSL_R_WRONG_CERTIFICATE_TYPE);
1411 s->session->peer_type = i;
1413 X509_free(s->session->peer);
1415 s->session->peer = x;
1416 s->session->verify_result = s->verify_result;
1423 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1425 s->state = SSL_ST_ERR;
1427 EVP_PKEY_free(pkey);
1429 sk_X509_pop_free(sk, X509_free);
1433 int ssl3_get_key_exchange(SSL *s)
1435 #ifndef OPENSSL_NO_RSA
1436 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1439 int al, j, verify_ret, ok;
1441 long n, alg_k, alg_a;
1442 EVP_PKEY *pkey = NULL;
1443 const EVP_MD *md = NULL;
1444 #ifndef OPENSSL_NO_RSA
1447 #ifndef OPENSSL_NO_DH
1450 #ifndef OPENSSL_NO_EC
1451 EC_KEY *ecdh = NULL;
1452 BN_CTX *bn_ctx = NULL;
1453 EC_POINT *srvr_ecpoint = NULL;
1455 unsigned int encoded_pt_len = 0;
1457 PACKET pkt, save_param_start;
1458 unsigned char *data, *param;
1461 EVP_MD_CTX_init(&md_ctx);
1464 * use same message size as in ssl3_get_certificate_request() as
1465 * ServerKeyExchange message may be skipped
1467 n = s->method->ssl_get_message(s,
1468 SSL3_ST_CR_KEY_EXCH_A,
1469 SSL3_ST_CR_KEY_EXCH_B,
1470 -1, s->max_cert_list, &ok);
1474 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1476 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1478 * Can't skip server key exchange if this is an ephemeral
1481 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
1482 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1483 al = SSL_AD_UNEXPECTED_MESSAGE;
1487 s->s3->tmp.reuse_message = 1;
1491 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1492 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1493 al = SSL_AD_INTERNAL_ERROR;
1496 save_param_start = pkt;
1498 #ifndef OPENSSL_NO_RSA
1499 RSA_free(s->s3->peer_rsa_tmp);
1500 s->s3->peer_rsa_tmp = NULL;
1502 #ifndef OPENSSL_NO_DH
1503 DH_free(s->s3->peer_dh_tmp);
1504 s->s3->peer_dh_tmp = NULL;
1506 #ifndef OPENSSL_NO_EC
1507 EC_KEY_free(s->s3->peer_ecdh_tmp);
1508 s->s3->peer_ecdh_tmp = NULL;
1511 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1513 al = SSL_AD_DECODE_ERROR;
1515 #ifndef OPENSSL_NO_PSK
1516 /* PSK ciphersuites are preceded by an identity hint */
1517 if (alg_k & SSL_PSK) {
1519 if (!PACKET_get_net_2(&pkt, &i)) {
1520 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1525 * Store PSK identity hint for later use, hint is used in
1526 * ssl3_send_client_key_exchange. Assume that the maximum length of
1527 * a PSK identity hint can be as long as the maximum length of a PSK
1530 if (i > PSK_MAX_IDENTITY_LEN) {
1531 al = SSL_AD_HANDSHAKE_FAILURE;
1532 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1535 if (PACKET_remaining(&pkt) < i) {
1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1537 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1541 OPENSSL_free(s->session->psk_identity_hint);
1543 unsigned char *hint = NULL;
1545 if (!PACKET_get_bytes(&pkt, &hint, i)) {
1546 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1549 s->session->psk_identity_hint = BUF_strndup((char *)hint, i);
1550 if (s->session->psk_identity_hint == NULL) {
1551 al = SSL_AD_HANDSHAKE_FAILURE;
1552 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1556 s->session->psk_identity_hint = NULL;
1560 /* Nothing else to do for plain PSK or RSAPSK */
1561 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1563 #endif /* !OPENSSL_NO_PSK */
1564 #ifndef OPENSSL_NO_SRP
1565 if (alg_k & SSL_kSRP) {
1566 if (!PACKET_get_net_2(&pkt, &i)) {
1567 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1571 if (!PACKET_get_bytes(&pkt, &data, i)) {
1572 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1576 if ((s->srp_ctx.N = BN_bin2bn(data, i, NULL)) == NULL) {
1577 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1581 if (!PACKET_get_net_2(&pkt, &i)) {
1582 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1586 if (!PACKET_get_bytes(&pkt, &data, i)) {
1587 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1591 if ((s->srp_ctx.g = BN_bin2bn(data, i, NULL)) == NULL) {
1592 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1596 if (!PACKET_get_1(&pkt, &i)) {
1597 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1601 if (!PACKET_get_bytes(&pkt, &data, i)) {
1602 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1606 if ((s->srp_ctx.s = BN_bin2bn(data, i, NULL)) == NULL) {
1607 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1611 if (!PACKET_get_net_2(&pkt, &i)) {
1612 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1616 if (!PACKET_get_bytes(&pkt, &data, i)) {
1617 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1621 if ((s->srp_ctx.B = BN_bin2bn(data, i, NULL)) == NULL) {
1622 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1626 if (!srp_verify_server_param(s, &al)) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1631 /* We must check if there is a certificate */
1632 if (alg_a & (SSL_aRSA|SSL_aDSS))
1633 pkey = X509_get_pubkey(s->session->peer);
1635 #endif /* !OPENSSL_NO_SRP */
1636 #ifndef OPENSSL_NO_RSA
1637 if (alg_k & SSL_kRSA) {
1638 /* Temporary RSA keys only allowed in export ciphersuites */
1639 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1640 al = SSL_AD_UNEXPECTED_MESSAGE;
1641 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1644 if ((rsa = RSA_new()) == NULL) {
1645 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1649 if (!PACKET_get_net_2(&pkt, &i)) {
1650 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1654 if (!PACKET_get_bytes(&pkt, &data, i)) {
1655 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1659 if ((rsa->n = BN_bin2bn(data, i, rsa->n)) == NULL) {
1660 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1664 if (!PACKET_get_net_2(&pkt, &i)) {
1665 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1669 if (!PACKET_get_bytes(&pkt, &data, i)) {
1670 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1674 if ((rsa->e = BN_bin2bn(data, i, rsa->e)) == NULL) {
1675 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1679 /* this should be because we are using an export cipher */
1680 if (alg_a & SSL_aRSA)
1681 pkey = X509_get_pubkey(s->session->peer);
1683 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1687 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1688 al = SSL_AD_UNEXPECTED_MESSAGE;
1689 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1693 s->s3->peer_rsa_tmp = rsa;
1696 #else /* OPENSSL_NO_RSA */
1699 #ifndef OPENSSL_NO_DH
1700 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1701 if ((dh = DH_new()) == NULL) {
1702 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1706 if (!PACKET_get_net_2(&pkt, &i)) {
1707 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1711 if (!PACKET_get_bytes(&pkt, &data, i)) {
1712 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1716 if ((dh->p = BN_bin2bn(data, i, NULL)) == NULL) {
1717 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1721 if (BN_is_zero(dh->p)) {
1722 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1726 if (!PACKET_get_net_2(&pkt, &i)) {
1727 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1731 if (!PACKET_get_bytes(&pkt, &data, i)) {
1732 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1736 if ((dh->g = BN_bin2bn(data, i, NULL)) == NULL) {
1737 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1741 if (BN_is_zero(dh->g)) {
1742 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1746 if (!PACKET_get_net_2(&pkt, &i)) {
1747 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1751 if (!PACKET_get_bytes(&pkt, &data, i)) {
1752 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1756 if ((dh->pub_key = BN_bin2bn(data, i, NULL)) == NULL) {
1757 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1761 if (BN_is_zero(dh->pub_key)) {
1762 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1766 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1767 al = SSL_AD_HANDSHAKE_FAILURE;
1768 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1771 if (alg_a & (SSL_aRSA|SSL_aDSS))
1772 pkey = X509_get_pubkey(s->session->peer);
1773 /* else anonymous DH, so no certificate or pkey. */
1775 s->s3->peer_dh_tmp = dh;
1778 #endif /* !OPENSSL_NO_DH */
1780 #ifndef OPENSSL_NO_EC
1781 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1783 const EC_GROUP *group;
1785 if ((ecdh = EC_KEY_new()) == NULL) {
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1791 * Extract elliptic curve parameters and the server's ephemeral ECDH
1792 * public key. Keep accumulating lengths of various components in
1793 * param_len and make sure it never exceeds n.
1797 * XXX: For now we only support named (not generic) curves and the
1798 * ECParameters in this case is just three bytes.
1800 if (!PACKET_get_bytes(&pkt, &data, 3)) {
1801 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1805 * Check curve is one of our preferences, if not server has sent an
1806 * invalid curve. ECParameters is 3 bytes.
1808 if (!tls1_check_curve(s, data, 3)) {
1809 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1813 if ((curve_nid = tls1_ec_curve_id2nid(*(data + 2))) == 0) {
1814 al = SSL_AD_INTERNAL_ERROR;
1815 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1816 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1820 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1821 if (ngroup == NULL) {
1822 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1825 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1826 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1829 EC_GROUP_free(ngroup);
1831 group = EC_KEY_get0_group(ecdh);
1833 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1834 (EC_GROUP_get_degree(group) > 163)) {
1835 al = SSL_AD_EXPORT_RESTRICTION;
1836 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1837 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1841 /* Next, get the encoded ECPoint */
1842 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1843 ((bn_ctx = BN_CTX_new()) == NULL)) {
1844 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1848 if (!PACKET_get_1(&pkt, &encoded_pt_len)) {
1849 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1853 if (!PACKET_get_bytes(&pkt, &data, encoded_pt_len) ||
1854 (EC_POINT_oct2point(group, srvr_ecpoint,
1855 data, encoded_pt_len, bn_ctx) == 0)) {
1856 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1861 * The ECC/TLS specification does not mention the use of DSA to sign
1862 * ECParameters in the server key exchange message. We do support RSA
1866 # ifndef OPENSSL_NO_RSA
1867 else if (alg_a & SSL_aRSA)
1868 pkey = X509_get_pubkey(s->session->peer);
1870 # ifndef OPENSSL_NO_EC
1871 else if (alg_a & SSL_aECDSA)
1872 pkey = X509_get_pubkey(s->session->peer);
1874 /* else anonymous ECDH, so no certificate or pkey. */
1875 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1876 s->s3->peer_ecdh_tmp = ecdh;
1878 BN_CTX_free(bn_ctx);
1880 EC_POINT_free(srvr_ecpoint);
1881 srvr_ecpoint = NULL;
1883 al = SSL_AD_UNEXPECTED_MESSAGE;
1884 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1887 #endif /* !OPENSSL_NO_EC */
1890 * |pkt| now points to the beginning of the signature, so the difference
1891 * equals the length of the parameters.
1893 param_len = PACKET_remaining(&save_param_start) - PACKET_remaining(&pkt);
1895 /* if it was signed, check the signature */
1897 if (SSL_USE_SIGALGS(s)) {
1899 if (!PACKET_get_bytes(&pkt, &data, 2)) {
1900 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1903 rv = tls12_check_peer_sigalg(&md, s, data, pkey);
1910 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1915 if (!PACKET_get_net_2(&pkt, &i)) {
1916 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1919 j = EVP_PKEY_size(pkey);
1921 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1926 * Check signature length
1928 if (i > (unsigned int)j
1929 || !PACKET_get_bytes(&pkt, &data, i)
1930 || PACKET_remaining(&pkt) != 0) {
1931 /* wrong packet length */
1932 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1935 pkt = save_param_start;
1936 if (!PACKET_get_bytes(&pkt, ¶m, param_len)) {
1937 al = SSL_AD_INTERNAL_ERROR;
1938 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1941 #ifndef OPENSSL_NO_RSA
1942 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1948 for (num = 2; num > 0; num--) {
1949 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1950 EVP_DigestInit_ex(&md_ctx, (num == 2)
1951 ? s->ctx->md5 : s->ctx->sha1, NULL);
1952 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1954 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1956 EVP_DigestUpdate(&md_ctx, param, param_len);
1957 EVP_DigestFinal_ex(&md_ctx, q, &size);
1962 RSA_verify(NID_md5_sha1, md_buf, j, data, i, pkey->pkey.rsa);
1963 if (verify_ret < 0) {
1964 al = SSL_AD_DECRYPT_ERROR;
1965 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1968 if (verify_ret == 0) {
1970 al = SSL_AD_DECRYPT_ERROR;
1971 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1977 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1978 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1980 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1982 EVP_VerifyUpdate(&md_ctx, param, param_len);
1983 if (EVP_VerifyFinal(&md_ctx, data, (int)i, pkey) <= 0) {
1985 al = SSL_AD_DECRYPT_ERROR;
1986 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1991 /* aNULL, aSRP or PSK do not need public keys */
1992 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1993 /* Might be wrong key type, check it */
1994 if (ssl3_check_cert_and_algorithm(s))
1995 /* Otherwise this shouldn't happen */
1996 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1999 /* still data left over */
2000 if (PACKET_remaining(&pkt) != 0) {
2001 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2005 EVP_PKEY_free(pkey);
2006 EVP_MD_CTX_cleanup(&md_ctx);
2009 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2011 EVP_PKEY_free(pkey);
2012 #ifndef OPENSSL_NO_RSA
2015 #ifndef OPENSSL_NO_DH
2018 #ifndef OPENSSL_NO_EC
2019 BN_CTX_free(bn_ctx);
2020 EC_POINT_free(srvr_ecpoint);
2023 EVP_MD_CTX_cleanup(&md_ctx);
2024 s->state = SSL_ST_ERR;
2028 int ssl3_get_certificate_request(SSL *s)
2032 unsigned int list_len, ctype_num, i, name_len;
2033 X509_NAME *xn = NULL;
2034 unsigned char *data;
2035 unsigned char *namestart, *namebytes;
2036 STACK_OF(X509_NAME) *ca_sk = NULL;
2039 n = s->method->ssl_get_message(s,
2040 SSL3_ST_CR_CERT_REQ_A,
2041 SSL3_ST_CR_CERT_REQ_B,
2042 -1, s->max_cert_list, &ok);
2047 s->s3->tmp.cert_req = 0;
2049 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2050 s->s3->tmp.reuse_message = 1;
2052 * If we get here we don't need any cached handshake records as we
2053 * wont be doing client auth.
2055 if (!ssl3_digest_cached_records(s, 0))
2060 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2061 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2062 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2066 /* TLS does not like anon-DH with client cert */
2067 if (s->version > SSL3_VERSION) {
2068 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2069 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2070 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2071 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2076 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2077 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2078 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2082 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2083 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2087 /* get the certificate types */
2088 if (!PACKET_get_1(&pkt, &ctype_num)
2089 || !PACKET_get_bytes(&pkt, &data, ctype_num)) {
2090 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2091 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2094 OPENSSL_free(s->cert->ctypes);
2095 s->cert->ctypes = NULL;
2096 if (ctype_num > SSL3_CT_NUMBER) {
2097 /* If we exceed static buffer copy all to cert structure */
2098 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2099 if (s->cert->ctypes == NULL) {
2100 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2103 memcpy(s->cert->ctypes, data, ctype_num);
2104 s->cert->ctype_num = (size_t)ctype_num;
2105 ctype_num = SSL3_CT_NUMBER;
2107 for (i = 0; i < ctype_num; i++)
2108 s->s3->tmp.ctype[i] = data[i];
2110 if (SSL_USE_SIGALGS(s)) {
2111 if (!PACKET_get_net_2(&pkt, &list_len)
2112 || !PACKET_get_bytes(&pkt, &data, list_len)) {
2113 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2114 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2118 /* Clear certificate digests and validity flags */
2119 for (i = 0; i < SSL_PKEY_NUM; i++) {
2120 s->s3->tmp.md[i] = NULL;
2121 s->s3->tmp.valid_flags[i] = 0;
2123 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2124 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2125 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2126 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2129 if (!tls1_process_sigalgs(s)) {
2130 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2131 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2136 /* get the CA RDNs */
2137 if (!PACKET_get_net_2(&pkt, &list_len)
2138 || PACKET_remaining(&pkt) != list_len) {
2139 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2140 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2144 while (PACKET_remaining(&pkt)) {
2145 if (!PACKET_get_net_2(&pkt, &name_len)
2146 || !PACKET_get_bytes(&pkt, &namebytes, name_len)) {
2147 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2148 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2152 namestart = namebytes;
2154 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2155 name_len)) == NULL) {
2156 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2157 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2161 if (namebytes != (namestart + name_len)) {
2162 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2163 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2164 SSL_R_CA_DN_LENGTH_MISMATCH);
2167 if (!sk_X509_NAME_push(ca_sk, xn)) {
2168 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2173 /* we should setup a certificate to return.... */
2174 s->s3->tmp.cert_req = 1;
2175 s->s3->tmp.ctype_num = ctype_num;
2176 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2177 s->s3->tmp.ca_names = ca_sk;
2183 s->state = SSL_ST_ERR;
2185 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2189 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2191 return (X509_NAME_cmp(*a, *b));
2194 int ssl3_get_new_session_ticket(SSL *s)
2196 int ok, al, ret = 0;
2197 unsigned int ticklen;
2198 unsigned long ticket_lifetime_hint;
2202 n = s->method->ssl_get_message(s,
2203 SSL3_ST_CR_SESSION_TICKET_A,
2204 SSL3_ST_CR_SESSION_TICKET_B,
2205 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2210 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2211 al = SSL_AD_INTERNAL_ERROR;
2212 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
2216 if (!PACKET_get_net_4(&pkt, &ticket_lifetime_hint)
2217 || !PACKET_get_net_2(&pkt, &ticklen)
2218 || PACKET_remaining(&pkt) != ticklen) {
2219 al = SSL_AD_DECODE_ERROR;
2220 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2224 /* Server is allowed to change its mind and send an empty ticket. */
2228 if (s->session->session_id_length > 0) {
2229 int i = s->session_ctx->session_cache_mode;
2230 SSL_SESSION *new_sess;
2232 * We reused an existing session, so we need to replace it with a new
2235 if (i & SSL_SESS_CACHE_CLIENT) {
2237 * Remove the old session from the cache
2239 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2240 if (s->session_ctx->remove_session_cb != NULL)
2241 s->session_ctx->remove_session_cb(s->session_ctx,
2244 /* We carry on if this fails */
2245 SSL_CTX_remove_session(s->session_ctx, s->session);
2249 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2250 al = SSL_AD_INTERNAL_ERROR;
2251 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2255 SSL_SESSION_free(s->session);
2256 s->session = new_sess;
2259 OPENSSL_free(s->session->tlsext_tick);
2260 s->session->tlsext_ticklen = 0;
2262 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2263 if (!s->session->tlsext_tick) {
2264 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2267 if (!PACKET_copy_bytes(&pkt, s->session->tlsext_tick, ticklen)) {
2268 al = SSL_AD_DECODE_ERROR;
2269 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2273 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2274 s->session->tlsext_ticklen = ticklen;
2276 * There are two ways to detect a resumed ticket session. One is to set
2277 * an appropriate session ID and then the server must return a match in
2278 * ServerHello. This allows the normal client session ID matching to work
2279 * and we know much earlier that the ticket has been accepted. The
2280 * other way is to set zero length session ID when the ticket is
2281 * presented and rely on the handshake to determine session resumption.
2282 * We choose the former approach because this fits in with assumptions
2283 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2284 * SHA256 is disabled) hash of the ticket.
2286 EVP_Digest(s->session->tlsext_tick, ticklen,
2287 s->session->session_id, &s->session->session_id_length,
2288 EVP_sha256(), NULL);
2292 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2294 s->state = SSL_ST_ERR;
2298 int ssl3_get_cert_status(SSL *s)
2301 unsigned long resplen, n;
2305 n = s->method->ssl_get_message(s,
2306 SSL3_ST_CR_CERT_STATUS_A,
2307 SSL3_ST_CR_CERT_STATUS_B,
2308 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2313 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2314 al = SSL_AD_INTERNAL_ERROR;
2315 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_INTERNAL_ERROR);
2318 if (!PACKET_get_1(&pkt, &type)
2319 || type != TLSEXT_STATUSTYPE_ocsp) {
2320 al = SSL_AD_DECODE_ERROR;
2321 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2324 if (!PACKET_get_net_3(&pkt, &resplen)
2325 || PACKET_remaining(&pkt) != resplen) {
2326 al = SSL_AD_DECODE_ERROR;
2327 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2330 OPENSSL_free(s->tlsext_ocsp_resp);
2331 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2332 if (!s->tlsext_ocsp_resp) {
2333 al = SSL_AD_INTERNAL_ERROR;
2334 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2337 if (!PACKET_copy_bytes(&pkt, s->tlsext_ocsp_resp, resplen)) {
2338 al = SSL_AD_DECODE_ERROR;
2339 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2342 s->tlsext_ocsp_resplen = resplen;
2343 if (s->ctx->tlsext_status_cb) {
2345 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2347 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2348 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2352 al = SSL_AD_INTERNAL_ERROR;
2353 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2359 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2360 s->state = SSL_ST_ERR;
2364 int ssl3_get_server_done(SSL *s)
2369 /* Second to last param should be very small, like 0 :-) */
2370 n = s->method->ssl_get_message(s,
2371 SSL3_ST_CR_SRVR_DONE_A,
2372 SSL3_ST_CR_SRVR_DONE_B,
2373 SSL3_MT_SERVER_DONE, 30, &ok);
2378 /* should contain no data */
2379 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2380 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2381 s->state = SSL_ST_ERR;
2388 int ssl3_send_client_key_exchange(SSL *s)
2392 #ifndef OPENSSL_NO_PSK
2393 size_t pskhdrlen = 0;
2395 unsigned long alg_k;
2396 #ifndef OPENSSL_NO_RSA
2398 EVP_PKEY *pkey = NULL;
2400 #ifndef OPENSSL_NO_EC
2401 EC_KEY *clnt_ecdh = NULL;
2402 const EC_POINT *srvr_ecpoint = NULL;
2403 EVP_PKEY *srvr_pub_pkey = NULL;
2404 unsigned char *encodedPoint = NULL;
2405 int encoded_pt_len = 0;
2406 BN_CTX *bn_ctx = NULL;
2408 unsigned char *pms = NULL;
2410 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2412 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2413 p = ssl_handshake_start(s);
2416 #ifndef OPENSSL_NO_PSK
2417 if (alg_k & SSL_PSK) {
2420 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2421 * \0-terminated identity. The last byte is for us for simulating
2424 char identity[PSK_MAX_IDENTITY_LEN + 1];
2426 unsigned char psk[PSK_MAX_PSK_LEN];
2429 if (s->psk_client_callback == NULL) {
2430 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2431 SSL_R_PSK_NO_CLIENT_CB);
2435 memset(identity, 0, sizeof(identity));
2437 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2438 identity, sizeof(identity) - 1,
2441 if (psklen > PSK_MAX_PSK_LEN) {
2442 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2443 ERR_R_INTERNAL_ERROR);
2445 } else if (psklen == 0) {
2446 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2447 SSL_R_PSK_IDENTITY_NOT_FOUND);
2451 OPENSSL_free(s->s3->tmp.psk);
2452 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2453 OPENSSL_cleanse(psk, psklen);
2455 if (s->s3->tmp.psk == NULL) {
2456 OPENSSL_cleanse(identity, sizeof(identity));
2460 s->s3->tmp.psklen = psklen;
2462 identitylen = strlen(identity);
2463 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2464 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2465 ERR_R_INTERNAL_ERROR);
2468 OPENSSL_free(s->session->psk_identity);
2469 s->session->psk_identity = BUF_strdup(identity);
2470 if (s->session->psk_identity == NULL) {
2471 OPENSSL_cleanse(identity, sizeof(identity));
2475 s2n(identitylen, p);
2476 memcpy(p, identity, identitylen);
2477 pskhdrlen = 2 + identitylen;
2481 OPENSSL_cleanse(identity, sizeof(identity));
2483 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2487 if (alg_k & SSL_kPSK) {
2492 /* Fool emacs indentation */
2495 #ifndef OPENSSL_NO_RSA
2496 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2498 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2499 pms = OPENSSL_malloc(pmslen);
2503 if (s->session->peer == NULL) {
2505 * We should always have a server certificate with SSL_kRSA.
2507 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2508 ERR_R_INTERNAL_ERROR);
2512 if (s->s3->peer_rsa_tmp != NULL)
2513 rsa = s->s3->peer_rsa_tmp;
2515 pkey = X509_get_pubkey(s->session->peer);
2516 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2517 || (pkey->pkey.rsa == NULL)) {
2518 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2519 ERR_R_INTERNAL_ERROR);
2522 rsa = pkey->pkey.rsa;
2523 EVP_PKEY_free(pkey);
2526 pms[0] = s->client_version >> 8;
2527 pms[1] = s->client_version & 0xff;
2528 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2532 /* Fix buf for TLS and beyond */
2533 if (s->version > SSL3_VERSION)
2535 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2537 if (s->options & SSL_OP_PKCS1_CHECK_1)
2539 if (s->options & SSL_OP_PKCS1_CHECK_2)
2543 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2544 SSL_R_BAD_RSA_ENCRYPT);
2548 /* Fix buf for TLS and beyond */
2549 if (s->version > SSL3_VERSION) {
2555 #ifndef OPENSSL_NO_DH
2556 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2557 DH *dh_srvr, *dh_clnt;
2558 if (s->s3->peer_dh_tmp != NULL)
2559 dh_srvr = s->s3->peer_dh_tmp;
2561 /* we get them from the cert */
2562 EVP_PKEY *spkey = NULL;
2564 spkey = X509_get_pubkey(s->session->peer);
2566 dh_srvr = EVP_PKEY_get1_DH(spkey);
2567 EVP_PKEY_free(spkey);
2569 if (dh_srvr == NULL) {
2570 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2571 ERR_R_INTERNAL_ERROR);
2575 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2576 /* Use client certificate key */
2577 EVP_PKEY *clkey = s->cert->key->privatekey;
2580 dh_clnt = EVP_PKEY_get1_DH(clkey);
2581 if (dh_clnt == NULL) {
2582 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2583 ERR_R_INTERNAL_ERROR);
2587 /* generate a new random key */
2588 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2589 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2592 if (!DH_generate_key(dh_clnt)) {
2593 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2599 pmslen = DH_size(dh_clnt);
2600 pms = OPENSSL_malloc(pmslen);
2605 * use the 'p' output buffer for the DH key, but make sure to
2606 * clear it out afterwards
2609 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2610 if (s->s3->peer_dh_tmp == NULL)
2614 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2620 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2623 /* send off the data */
2624 n = BN_num_bytes(dh_clnt->pub_key);
2626 BN_bn2bin(dh_clnt->pub_key, p);
2634 #ifndef OPENSSL_NO_EC
2635 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2636 const EC_GROUP *srvr_group = NULL;
2638 int ecdh_clnt_cert = 0;
2641 * Did we send out the client's ECDH share for use in premaster
2642 * computation as part of client certificate? If so, set
2643 * ecdh_clnt_cert to 1.
2645 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2647 * XXX: For now, we do not support client
2648 * authentication using ECDH certificates.
2649 * To add such support, one needs to add
2650 * code that checks for appropriate
2651 * conditions and sets ecdh_clnt_cert to 1.
2652 * For example, the cert have an ECC
2653 * key on the same curve as the server's
2654 * and the key should be authorized for
2657 * One also needs to add code in ssl3_connect
2658 * to skip sending the certificate verify
2661 * if ((s->cert->key->privatekey != NULL) &&
2662 * (s->cert->key->privatekey->type ==
2663 * EVP_PKEY_EC) && ...)
2664 * ecdh_clnt_cert = 1;
2668 if (s->s3->peer_ecdh_tmp != NULL) {
2669 tkey = s->s3->peer_ecdh_tmp;
2671 /* Get the Server Public Key from Cert */
2672 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2673 if ((srvr_pub_pkey == NULL)
2674 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2675 || (srvr_pub_pkey->pkey.ec == NULL)) {
2676 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2677 ERR_R_INTERNAL_ERROR);
2681 tkey = srvr_pub_pkey->pkey.ec;
2684 srvr_group = EC_KEY_get0_group(tkey);
2685 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2687 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2688 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2689 ERR_R_INTERNAL_ERROR);
2693 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2694 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2695 ERR_R_MALLOC_FAILURE);
2699 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2700 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2703 if (ecdh_clnt_cert) {
2705 * Reuse key info from our certificate We only need our
2706 * private key to perform the ECDH computation.
2708 const BIGNUM *priv_key;
2709 tkey = s->cert->key->privatekey->pkey.ec;
2710 priv_key = EC_KEY_get0_private_key(tkey);
2711 if (priv_key == NULL) {
2712 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2713 ERR_R_MALLOC_FAILURE);
2716 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2717 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2721 /* Generate a new ECDH key pair */
2722 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2723 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2730 * use the 'p' output buffer for the ECDH key, but make sure to
2731 * clear it out afterwards
2734 field_size = EC_GROUP_get_degree(srvr_group);
2735 if (field_size <= 0) {
2736 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2739 pmslen = (field_size + 7) / 8;
2740 pms = OPENSSL_malloc(pmslen);
2743 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2744 if (n <= 0 || pmslen != (size_t)n) {
2745 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2749 if (ecdh_clnt_cert) {
2750 /* Send empty client key exch message */
2754 * First check the size of encoding and allocate memory
2758 EC_POINT_point2oct(srvr_group,
2759 EC_KEY_get0_public_key(clnt_ecdh),
2760 POINT_CONVERSION_UNCOMPRESSED,
2763 encodedPoint = (unsigned char *)
2764 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2765 bn_ctx = BN_CTX_new();
2766 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2767 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2768 ERR_R_MALLOC_FAILURE);
2772 /* Encode the public key */
2773 n = EC_POINT_point2oct(srvr_group,
2774 EC_KEY_get0_public_key(clnt_ecdh),
2775 POINT_CONVERSION_UNCOMPRESSED,
2776 encodedPoint, encoded_pt_len, bn_ctx);
2778 *p = n; /* length of encoded point */
2779 /* Encoded point will be copied here */
2781 /* copy the point */
2782 memcpy(p, encodedPoint, n);
2783 /* increment n to account for length field */
2787 /* Free allocated memory */
2788 BN_CTX_free(bn_ctx);
2789 OPENSSL_free(encodedPoint);
2790 EC_KEY_free(clnt_ecdh);
2791 EVP_PKEY_free(srvr_pub_pkey);
2793 #endif /* !OPENSSL_NO_EC */
2794 else if (alg_k & SSL_kGOST) {
2795 /* GOST key exchange message creation */
2796 EVP_PKEY_CTX *pkey_ctx;
2799 unsigned int md_len;
2800 unsigned char shared_ukm[32], tmp[256];
2801 EVP_MD_CTX *ukm_hash;
2805 pms = OPENSSL_malloc(pmslen);
2810 * Get server sertificate PKEY and create ctx from it
2812 peer_cert = s->session->peer;
2814 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2815 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2819 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2820 X509_get_pubkey(peer_cert), NULL);
2822 * If we have send a certificate, and certificate key
2824 * * parameters match those of server certificate, use
2825 * certificate key for key exchange
2828 /* Otherwise, generate ephemeral key pair */
2830 EVP_PKEY_encrypt_init(pkey_ctx);
2831 /* Generate session key */
2832 if (RAND_bytes(pms, pmslen) <= 0) {
2833 EVP_PKEY_CTX_free(pkey_ctx);
2834 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2835 ERR_R_INTERNAL_ERROR);
2839 * If we have client certificate, use its secret as peer key
2841 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2842 if (EVP_PKEY_derive_set_peer
2843 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2845 * If there was an error - just ignore it. Ephemeral key
2852 * Compute shared IV and store it in algorithm-specific context
2855 ukm_hash = EVP_MD_CTX_create();
2856 EVP_DigestInit(ukm_hash,
2857 EVP_get_digestbynid(NID_id_GostR3411_94));
2858 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2860 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2862 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2863 EVP_MD_CTX_destroy(ukm_hash);
2864 if (EVP_PKEY_CTX_ctrl
2865 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2867 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2871 /* Make GOST keytransport blob message */
2873 * Encapsulate it into sequence
2875 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2877 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2878 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2882 if (msglen >= 0x80) {
2884 *(p++) = msglen & 0xff;
2887 *(p++) = msglen & 0xff;
2890 memcpy(p, tmp, msglen);
2891 /* Check if pubkey from client certificate was used */
2892 if (EVP_PKEY_CTX_ctrl
2893 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2894 /* Set flag "skip certificate verify" */
2895 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2897 EVP_PKEY_CTX_free(pkey_ctx);
2898 EVP_PKEY_free(pub_key);
2901 #ifndef OPENSSL_NO_SRP
2902 else if (alg_k & SSL_kSRP) {
2903 if (s->srp_ctx.A != NULL) {
2904 /* send off the data */
2905 n = BN_num_bytes(s->srp_ctx.A);
2907 BN_bn2bin(s->srp_ctx.A, p);
2910 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2911 ERR_R_INTERNAL_ERROR);
2914 OPENSSL_free(s->session->srp_username);
2915 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2916 if (s->session->srp_username == NULL) {
2917 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2918 ERR_R_MALLOC_FAILURE);
2924 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2925 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2929 #ifndef OPENSSL_NO_PSK
2933 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2934 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2935 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2939 s->state = SSL3_ST_CW_KEY_EXCH_B;
2942 /* SSL3_ST_CW_KEY_EXCH_B */
2943 n = ssl_do_write(s);
2944 #ifndef OPENSSL_NO_SRP
2946 if (alg_k & SSL_kSRP) {
2948 * If everything written generate master key: no need to save PMS as
2949 * srp_generate_client_master_secret generates it internally.
2952 if (!srp_generate_client_master_secret(s)) {
2953 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2954 ERR_R_INTERNAL_ERROR);
2960 /* If we haven't written everything save PMS */
2962 s->s3->tmp.pms = pms;
2963 s->s3->tmp.pmslen = pmslen;
2965 /* If we don't have a PMS restore */
2967 pms = s->s3->tmp.pms;
2968 pmslen = s->s3->tmp.pmslen;
2970 if (pms == NULL && !(alg_k & SSL_kPSK)) {
2971 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2972 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2975 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2976 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2977 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2983 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2984 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2986 OPENSSL_clear_free(pms, pmslen);
2987 s->s3->tmp.pms = NULL;
2988 #ifndef OPENSSL_NO_EC
2989 BN_CTX_free(bn_ctx);
2990 OPENSSL_free(encodedPoint);
2991 EC_KEY_free(clnt_ecdh);
2992 EVP_PKEY_free(srvr_pub_pkey);
2994 #ifndef OPENSSL_NO_PSK
2995 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2996 s->s3->tmp.psk = NULL;
2998 s->state = SSL_ST_ERR;
3002 int ssl3_send_client_verify(SSL *s)
3005 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3007 EVP_PKEY_CTX *pctx = NULL;
3013 EVP_MD_CTX_init(&mctx);
3015 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3016 p = ssl_handshake_start(s);
3017 pkey = s->cert->key->privatekey;
3018 /* Create context from key and test if sha1 is allowed as digest */
3019 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3020 EVP_PKEY_sign_init(pctx);
3021 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3022 if (!SSL_USE_SIGALGS(s))
3023 s->method->ssl3_enc->cert_verify_mac(s,
3026 [MD5_DIGEST_LENGTH]));
3031 * For TLS v1.2 send signature algorithm and signature using agreed
3032 * digest and cached handshake records.
3034 if (SSL_USE_SIGALGS(s)) {
3037 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3038 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3039 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3040 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3045 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3048 if (!EVP_SignInit_ex(&mctx, md, NULL)
3049 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3050 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3051 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3056 /* Digest cached records and discard handshake buffer */
3057 if (!ssl3_digest_cached_records(s, 0))
3060 #ifndef OPENSSL_NO_RSA
3061 if (pkey->type == EVP_PKEY_RSA) {
3062 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3063 if (RSA_sign(NID_md5_sha1, data,
3064 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3065 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3066 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3073 #ifndef OPENSSL_NO_DSA
3074 if (pkey->type == EVP_PKEY_DSA) {
3075 if (!DSA_sign(pkey->save_type,
3076 &(data[MD5_DIGEST_LENGTH]),
3077 SHA_DIGEST_LENGTH, &(p[2]),
3078 (unsigned int *)&j, pkey->pkey.dsa)) {
3079 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3086 #ifndef OPENSSL_NO_EC
3087 if (pkey->type == EVP_PKEY_EC) {
3088 if (!ECDSA_sign(pkey->save_type,
3089 &(data[MD5_DIGEST_LENGTH]),
3090 SHA_DIGEST_LENGTH, &(p[2]),
3091 (unsigned int *)&j, pkey->pkey.ec)) {
3092 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3099 if (pkey->type == NID_id_GostR3410_2001) {
3100 unsigned char signbuf[64];
3102 size_t sigsize = 64;
3103 s->method->ssl3_enc->cert_verify_mac(s,
3104 NID_id_GostR3411_94, data);
3105 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3106 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3109 for (i = 63, j = 0; i >= 0; j++, i--) {
3110 p[2 + j] = signbuf[i];
3115 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3118 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3119 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3122 s->state = SSL3_ST_CW_CERT_VRFY_B;
3124 EVP_MD_CTX_cleanup(&mctx);
3125 EVP_PKEY_CTX_free(pctx);
3126 return ssl_do_write(s);
3128 EVP_MD_CTX_cleanup(&mctx);
3129 EVP_PKEY_CTX_free(pctx);
3130 s->state = SSL_ST_ERR;
3135 * Check a certificate can be used for client authentication. Currently check
3136 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3137 * certificates can be used and optionally checks suitability for Suite B.
3139 static int ssl3_check_client_certificate(SSL *s)
3141 unsigned long alg_k;
3142 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3144 /* If no suitable signature algorithm can't use certificate */
3145 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3148 * If strict mode check suitability of chain before using it. This also
3149 * adjusts suite B digest if necessary.
3151 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3152 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3154 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3155 /* See if we can use client certificate for fixed DH */
3156 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3157 int i = s->session->peer_type;
3158 EVP_PKEY *clkey = NULL, *spkey = NULL;
3159 clkey = s->cert->key->privatekey;
3160 /* If client key not DH assume it can be used */
3161 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3164 spkey = X509_get_pubkey(s->session->peer);
3166 /* Compare server and client parameters */
3167 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3168 EVP_PKEY_free(spkey);
3172 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3177 int ssl3_send_client_certificate(SSL *s)
3180 EVP_PKEY *pkey = NULL;
3183 if (s->state == SSL3_ST_CW_CERT_A) {
3184 /* Let cert callback update client certificates if required */
3185 if (s->cert->cert_cb) {
3186 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3188 s->rwstate = SSL_X509_LOOKUP;
3192 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3193 s->state = SSL_ST_ERR;
3196 s->rwstate = SSL_NOTHING;
3198 if (ssl3_check_client_certificate(s))
3199 s->state = SSL3_ST_CW_CERT_C;
3201 s->state = SSL3_ST_CW_CERT_B;
3204 /* We need to get a client cert */
3205 if (s->state == SSL3_ST_CW_CERT_B) {
3207 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3208 * return(-1); We then get retied later
3211 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3213 s->rwstate = SSL_X509_LOOKUP;
3216 s->rwstate = SSL_NOTHING;
3217 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3218 s->state = SSL3_ST_CW_CERT_B;
3219 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3221 } else if (i == 1) {
3223 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3224 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3228 EVP_PKEY_free(pkey);
3229 if (i && !ssl3_check_client_certificate(s))
3232 if (s->version == SSL3_VERSION) {
3233 s->s3->tmp.cert_req = 0;
3234 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3237 s->s3->tmp.cert_req = 2;
3238 if (!ssl3_digest_cached_records(s, 0)) {
3239 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3240 s->state = SSL_ST_ERR;
3246 /* Ok, we have a cert */
3247 s->state = SSL3_ST_CW_CERT_C;
3250 if (s->state == SSL3_ST_CW_CERT_C) {
3251 s->state = SSL3_ST_CW_CERT_D;
3252 if (!ssl3_output_cert_chain(s,
3253 (s->s3->tmp.cert_req ==
3254 2) ? NULL : s->cert->key)) {
3255 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3256 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3257 s->state = SSL_ST_ERR;
3261 /* SSL3_ST_CW_CERT_D */
3262 return ssl_do_write(s);
3265 #define has_bits(i,m) (((i)&(m)) == (m))
3267 int ssl3_check_cert_and_algorithm(SSL *s)
3271 EVP_PKEY *pkey = NULL;
3273 #ifndef OPENSSL_NO_RSA
3276 #ifndef OPENSSL_NO_DH
3279 int al = SSL_AD_HANDSHAKE_FAILURE;
3281 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3282 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3284 /* we don't have a certificate */
3285 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3287 #ifndef OPENSSL_NO_RSA
3288 rsa = s->s3->peer_rsa_tmp;
3290 #ifndef OPENSSL_NO_DH
3291 dh = s->s3->peer_dh_tmp;
3294 /* This is the passed certificate */
3296 idx = s->session->peer_type;
3297 #ifndef OPENSSL_NO_EC
3298 if (idx == SSL_PKEY_ECC) {
3299 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3301 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3306 } else if (alg_a & SSL_aECDSA) {
3307 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3308 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3310 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3311 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3315 pkey = X509_get_pubkey(s->session->peer);
3316 pkey_bits = EVP_PKEY_bits(pkey);
3317 i = X509_certificate_type(s->session->peer, pkey);
3318 EVP_PKEY_free(pkey);
3320 /* Check that we have a certificate if we require one */
3321 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3322 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3323 SSL_R_MISSING_RSA_SIGNING_CERT);
3326 #ifndef OPENSSL_NO_DSA
3327 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3328 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3329 SSL_R_MISSING_DSA_SIGNING_CERT);
3333 #ifndef OPENSSL_NO_RSA
3334 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3335 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3336 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3337 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3338 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3340 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3341 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3342 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3343 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3344 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3348 /* server key exchange is not allowed. */
3349 al = SSL_AD_INTERNAL_ERROR;
3350 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3357 #ifndef OPENSSL_NO_DH
3358 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3359 al = SSL_AD_INTERNAL_ERROR;
3360 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3362 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3363 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3364 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3365 SSL_R_MISSING_DH_RSA_CERT);
3368 # ifndef OPENSSL_NO_DSA
3369 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3370 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3371 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3372 SSL_R_MISSING_DH_DSA_CERT);
3378 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3379 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3380 #ifndef OPENSSL_NO_RSA
3381 if (alg_k & SSL_kRSA) {
3383 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3384 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3386 } else if (RSA_bits(rsa) >
3387 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3388 /* We have a temporary RSA key but it's too large. */
3389 al = SSL_AD_EXPORT_RESTRICTION;
3390 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3391 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3396 #ifndef OPENSSL_NO_DH
3397 if (alg_k & SSL_kDHE) {
3399 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3400 /* We have a temporary DH key but it's too large. */
3401 al = SSL_AD_EXPORT_RESTRICTION;
3402 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3403 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3406 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3407 /* The cert should have had an export DH key. */
3408 al = SSL_AD_EXPORT_RESTRICTION;
3409 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3410 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3415 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3416 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3422 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3427 * Normally, we can tell if the server is resuming the session from
3428 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3429 * message after the ServerHello to determine if the server is resuming.
3430 * Therefore, we allow EAP-FAST to peek ahead.
3431 * ssl3_check_change returns 1 if we are resuming from an external
3432 * pre-shared secret, we have a "ticket" and the next server message
3433 * is CCS; and 0 otherwise. It returns -1 upon an error.
3435 static int ssl3_check_change(SSL *s)
3439 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3440 !s->session->tlsext_tick)
3444 * This function is called when we might get a Certificate message instead,
3445 * so permit appropriate message length.
3446 * We ignore the return value as we're only interested in the message type
3447 * and not its length.
3449 s->method->ssl_get_message(s,
3452 -1, s->max_cert_list, &ok);
3457 s->s3->tmp.reuse_message = 1;
3459 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC)
3465 #ifndef OPENSSL_NO_NEXTPROTONEG
3466 int ssl3_send_next_proto(SSL *s)
3468 unsigned int len, padding_len;
3471 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3472 len = s->next_proto_negotiated_len;
3473 padding_len = 32 - ((len + 2) % 32);
3474 d = (unsigned char *)s->init_buf->data;
3476 memcpy(d + 5, s->next_proto_negotiated, len);
3477 d[5 + len] = padding_len;
3478 memset(d + 6 + len, 0, padding_len);
3479 *(d++) = SSL3_MT_NEXT_PROTO;
3480 l2n3(2 + len + padding_len, d);
3481 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3482 s->init_num = 4 + 2 + len + padding_len;
3486 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3490 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3493 #ifndef OPENSSL_NO_ENGINE
3494 if (s->ctx->client_cert_engine) {
3495 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3496 SSL_get_client_CA_list(s),
3497 px509, ppkey, NULL, NULL, NULL);
3502 if (s->ctx->client_cert_cb)
3503 i = s->ctx->client_cert_cb(s, px509, ppkey);
3507 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3513 int empty_reneg_info_scsv = !s->renegotiate;
3514 /* Set disabled masks for this session */
3515 ssl_set_client_disabled(s);
3521 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3522 c = sk_SSL_CIPHER_value(sk, i);
3523 /* Skip disabled ciphers */
3524 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3526 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3527 if (c->id == SSL3_CK_SCSV) {
3528 if (!empty_reneg_info_scsv)
3531 empty_reneg_info_scsv = 0;
3534 j = s->method->put_cipher_by_char(c, p);
3538 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3542 if (empty_reneg_info_scsv) {
3543 static SSL_CIPHER scsv = {
3544 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3546 j = s->method->put_cipher_by_char(&scsv, p);
3548 #ifdef OPENSSL_RI_DEBUG
3550 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3553 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3554 static SSL_CIPHER scsv = {
3555 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3557 j = s->method->put_cipher_by_char(&scsv, p);