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;
931 PACKET pkt, session_id;
932 size_t session_id_len;
933 unsigned char *cipherchars;
934 int i, al = SSL_AD_INTERNAL_ERROR, ok;
935 unsigned int compression;
937 #ifndef OPENSSL_NO_COMP
941 * Hello verify request and/or server hello version may not match so set
942 * first packet if we're negotiating version.
946 n = s->method->ssl_get_message(s,
947 SSL3_ST_CR_SRVR_HELLO_A,
948 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
954 if (SSL_IS_DTLS(s)) {
955 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
956 if (s->d1->send_cookie == 0) {
957 s->s3->tmp.reuse_message = 1;
959 } else { /* already sent a cookie */
961 al = SSL_AD_UNEXPECTED_MESSAGE;
962 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
968 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
969 al = SSL_AD_UNEXPECTED_MESSAGE;
970 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
974 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
975 al = SSL_AD_INTERNAL_ERROR;
976 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
980 if (s->method->version == TLS_ANY_VERSION) {
981 unsigned int sversion;
983 if (!PACKET_get_net_2(&pkt, &sversion)) {
984 al = SSL_AD_DECODE_ERROR;
985 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
989 #if TLS_MAX_VERSION != TLS1_2_VERSION
990 #error Code needs updating for new TLS version
992 #ifndef OPENSSL_NO_SSL3
993 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
995 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
996 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
997 al = SSL_AD_PROTOCOL_VERSION;
1000 s->method = SSLv3_client_method();
1003 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1004 s->method = TLSv1_client_method();
1005 } else if ((sversion == TLS1_1_VERSION) &&
1006 !(s->options & SSL_OP_NO_TLSv1_1)) {
1007 s->method = TLSv1_1_client_method();
1008 } else if ((sversion == TLS1_2_VERSION) &&
1009 !(s->options & SSL_OP_NO_TLSv1_2)) {
1010 s->method = TLSv1_2_client_method();
1012 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1013 al = SSL_AD_PROTOCOL_VERSION;
1016 s->session->ssl_version = s->version = s->method->version;
1018 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1019 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1020 al = SSL_AD_PROTOCOL_VERSION;
1023 } else if (s->method->version == DTLS_ANY_VERSION) {
1024 /* Work out correct protocol version to use */
1025 unsigned int hversion;
1028 if (!PACKET_get_net_2(&pkt, &hversion)) {
1029 al = SSL_AD_DECODE_ERROR;
1030 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1034 options = s->options;
1035 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1036 s->method = DTLSv1_2_client_method();
1037 else if (tls1_suiteb(s)) {
1038 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1039 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1040 s->version = hversion;
1041 al = SSL_AD_PROTOCOL_VERSION;
1043 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1044 s->method = DTLSv1_client_method();
1046 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1047 s->version = hversion;
1048 al = SSL_AD_PROTOCOL_VERSION;
1051 s->session->ssl_version = s->version = s->method->version;
1053 unsigned char *vers;
1055 if (!PACKET_get_bytes(&pkt, &vers, 2)) {
1056 al = SSL_AD_DECODE_ERROR;
1057 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1060 if ((vers[0] != (s->version >> 8))
1061 || (vers[1] != (s->version & 0xff))) {
1062 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1063 s->version = (s->version & 0xff00) | vers[1];
1064 al = SSL_AD_PROTOCOL_VERSION;
1069 /* load the server hello data */
1070 /* load the server random */
1071 if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1072 al = SSL_AD_DECODE_ERROR;
1073 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1079 /* Get the session-id. */
1080 if (!PACKET_get_length_prefixed_1(&pkt, &session_id)) {
1081 al = SSL_AD_DECODE_ERROR;
1082 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1085 session_id_len = PACKET_remaining(&session_id);
1086 if (session_id_len > sizeof s->session->session_id
1087 || session_id_len > SSL3_SESSION_ID_SIZE) {
1088 al = SSL_AD_ILLEGAL_PARAMETER;
1089 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1093 if (!PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {
1094 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1095 al = SSL_AD_DECODE_ERROR;
1100 * Check if we can resume the session based on external pre-shared secret.
1101 * EAP-FAST (RFC 4851) supports two types of session resumption.
1102 * Resumption based on server-side state works with session IDs.
1103 * Resumption based on pre-shared Protected Access Credentials (PACs)
1104 * works by overriding the SessionTicket extension at the application
1105 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1106 * servers would honour the session ID.) Therefore, the session ID alone
1107 * is not a reliable indicator of session resumption, so we first check if
1108 * we can resume, and later peek at the next handshake message to see if the
1109 * server wants to resume.
1111 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1112 s->session->tlsext_tick) {
1113 SSL_CIPHER *pref_cipher = NULL;
1114 s->session->master_key_length = sizeof(s->session->master_key);
1115 if (s->tls_session_secret_cb(s, s->session->master_key,
1116 &s->session->master_key_length,
1118 s->tls_session_secret_cb_arg)) {
1119 s->session->cipher = pref_cipher ?
1120 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1122 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1123 al = SSL_AD_INTERNAL_ERROR;
1128 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1129 && memcmp(PACKET_data(&session_id), s->session->session_id,
1130 session_id_len) == 0) {
1131 if (s->sid_ctx_length != s->session->sid_ctx_length
1132 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1133 /* actually a client application bug */
1134 al = SSL_AD_ILLEGAL_PARAMETER;
1135 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1136 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1142 * If we were trying for session-id reuse but the server
1143 * didn't echo the ID, make a new SSL_SESSION.
1144 * In the case of EAP-FAST and PAC, we do not send a session ID,
1145 * so the PAC-based session secret is always preserved. It'll be
1146 * overwritten if the server refuses resumption.
1148 if (s->session->session_id_length > 0) {
1149 if (!ssl_get_new_session(s, 0)) {
1154 s->session->session_id_length = session_id_len;
1155 /* session_id_len could be 0 */
1156 memcpy(s->session->session_id, PACKET_data(&session_id),
1160 c = ssl_get_cipher_by_char(s, cipherchars);
1162 /* unknown cipher */
1163 al = SSL_AD_ILLEGAL_PARAMETER;
1164 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1167 /* Set version disabled mask now we know version */
1168 if (!SSL_USE_TLS1_2_CIPHERS(s))
1169 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1171 s->s3->tmp.mask_ssl = 0;
1173 * If it is a disabled cipher we didn't send it in client hello, so
1176 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1177 al = SSL_AD_ILLEGAL_PARAMETER;
1178 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1182 sk = ssl_get_ciphers_by_id(s);
1183 i = sk_SSL_CIPHER_find(sk, c);
1185 /* we did not say we would use this cipher */
1186 al = SSL_AD_ILLEGAL_PARAMETER;
1187 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1192 * Depending on the session caching (internal/external), the cipher
1193 * and/or cipher_id values may not be set. Make sure that cipher_id is
1194 * set and use it for comparison.
1196 if (s->session->cipher)
1197 s->session->cipher_id = s->session->cipher->id;
1198 if (s->hit && (s->session->cipher_id != c->id)) {
1199 al = SSL_AD_ILLEGAL_PARAMETER;
1200 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1201 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1204 s->s3->tmp.new_cipher = c;
1206 * Don't digest cached records if no sigalgs: we may need them for client
1209 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1211 /* lets get the compression algorithm */
1213 if (!PACKET_get_1(&pkt, &compression)) {
1214 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1215 al = SSL_AD_DECODE_ERROR;
1218 #ifdef OPENSSL_NO_COMP
1219 if (compression != 0) {
1220 al = SSL_AD_ILLEGAL_PARAMETER;
1221 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1222 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1226 * If compression is disabled we'd better not try to resume a session
1227 * using compression.
1229 if (s->session->compress_meth != 0) {
1230 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1234 if (s->hit && compression != s->session->compress_meth) {
1235 al = SSL_AD_ILLEGAL_PARAMETER;
1236 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1237 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1240 if (compression == 0)
1242 else if (!ssl_allow_compression(s)) {
1243 al = SSL_AD_ILLEGAL_PARAMETER;
1244 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1247 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1250 if (compression != 0 && comp == NULL) {
1251 al = SSL_AD_ILLEGAL_PARAMETER;
1252 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1253 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1256 s->s3->tmp.new_compression = comp;
1260 /* TLS extensions */
1261 if (!ssl_parse_serverhello_tlsext(s, &pkt)) {
1262 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1266 if (PACKET_remaining(&pkt) != 0) {
1267 /* wrong packet length */
1268 al = SSL_AD_DECODE_ERROR;
1269 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1275 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1277 s->state = SSL_ST_ERR;
1281 int ssl3_get_server_certificate(SSL *s)
1283 int al, i, ok, ret = -1, exp_idx;
1284 unsigned long n, cert_list_len, cert_len;
1286 unsigned char *certstart, *certbytes;
1287 STACK_OF(X509) *sk = NULL;
1288 EVP_PKEY *pkey = NULL;
1291 n = s->method->ssl_get_message(s,
1294 -1, s->max_cert_list, &ok);
1299 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1300 s->s3->tmp.reuse_message = 1;
1304 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1305 al = SSL_AD_UNEXPECTED_MESSAGE;
1306 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1310 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1311 al = SSL_AD_INTERNAL_ERROR;
1312 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1316 if ((sk = sk_X509_new_null()) == NULL) {
1317 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1321 if (!PACKET_get_net_3(&pkt, &cert_list_len)
1322 || PACKET_remaining(&pkt) != cert_list_len) {
1323 al = SSL_AD_DECODE_ERROR;
1324 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1327 while (PACKET_remaining(&pkt)) {
1328 if (!PACKET_get_net_3(&pkt, &cert_len)
1329 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
1330 al = SSL_AD_DECODE_ERROR;
1331 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1332 SSL_R_CERT_LENGTH_MISMATCH);
1336 certstart = certbytes;
1337 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1339 al = SSL_AD_BAD_CERTIFICATE;
1340 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1343 if (certbytes != (certstart + cert_len)) {
1344 al = SSL_AD_DECODE_ERROR;
1345 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1346 SSL_R_CERT_LENGTH_MISMATCH);
1349 if (!sk_X509_push(sk, x)) {
1350 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1356 i = ssl_verify_cert_chain(s, sk);
1357 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1358 al = ssl_verify_alarm_type(s->verify_result);
1359 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1360 SSL_R_CERTIFICATE_VERIFY_FAILED);
1363 ERR_clear_error(); /* but we keep s->verify_result */
1365 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1366 al = SSL_AD_HANDSHAKE_FAILURE;
1370 s->session->peer_chain = sk;
1372 * Inconsistency alert: cert_chain does include the peer's certificate,
1373 * which we don't include in s3_srvr.c
1375 x = sk_X509_value(sk, 0);
1378 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1381 pkey = X509_get_pubkey(x);
1383 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1386 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1387 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1391 i = ssl_cert_type(x, pkey);
1395 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1396 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1400 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1401 if (exp_idx >= 0 && i != exp_idx) {
1403 al = SSL_AD_ILLEGAL_PARAMETER;
1404 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1405 SSL_R_WRONG_CERTIFICATE_TYPE);
1408 s->session->peer_type = i;
1410 X509_free(s->session->peer);
1412 s->session->peer = x;
1413 s->session->verify_result = s->verify_result;
1420 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1422 s->state = SSL_ST_ERR;
1424 EVP_PKEY_free(pkey);
1426 sk_X509_pop_free(sk, X509_free);
1430 int ssl3_get_key_exchange(SSL *s)
1432 #ifndef OPENSSL_NO_RSA
1433 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1436 int al, j, verify_ret, ok;
1438 long n, alg_k, alg_a;
1439 EVP_PKEY *pkey = NULL;
1440 const EVP_MD *md = NULL;
1441 #ifndef OPENSSL_NO_RSA
1444 #ifndef OPENSSL_NO_DH
1447 #ifndef OPENSSL_NO_EC
1448 EC_KEY *ecdh = NULL;
1449 BN_CTX *bn_ctx = NULL;
1450 EC_POINT *srvr_ecpoint = NULL;
1452 unsigned int encoded_pt_len = 0;
1454 PACKET pkt, save_param_start;
1455 unsigned char *data, *param;
1458 EVP_MD_CTX_init(&md_ctx);
1461 * use same message size as in ssl3_get_certificate_request() as
1462 * ServerKeyExchange message may be skipped
1464 n = s->method->ssl_get_message(s,
1465 SSL3_ST_CR_KEY_EXCH_A,
1466 SSL3_ST_CR_KEY_EXCH_B,
1467 -1, s->max_cert_list, &ok);
1471 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1473 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1475 * Can't skip server key exchange if this is an ephemeral
1478 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
1479 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1480 al = SSL_AD_UNEXPECTED_MESSAGE;
1484 s->s3->tmp.reuse_message = 1;
1488 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1489 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1490 al = SSL_AD_INTERNAL_ERROR;
1493 save_param_start = pkt;
1495 #ifndef OPENSSL_NO_RSA
1496 RSA_free(s->s3->peer_rsa_tmp);
1497 s->s3->peer_rsa_tmp = NULL;
1499 #ifndef OPENSSL_NO_DH
1500 DH_free(s->s3->peer_dh_tmp);
1501 s->s3->peer_dh_tmp = NULL;
1503 #ifndef OPENSSL_NO_EC
1504 EC_KEY_free(s->s3->peer_ecdh_tmp);
1505 s->s3->peer_ecdh_tmp = NULL;
1508 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1510 al = SSL_AD_DECODE_ERROR;
1512 #ifndef OPENSSL_NO_PSK
1513 /* PSK ciphersuites are preceded by an identity hint */
1514 if (alg_k & SSL_PSK) {
1516 if (!PACKET_get_net_2(&pkt, &i)) {
1517 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1522 * Store PSK identity hint for later use, hint is used in
1523 * ssl3_send_client_key_exchange. Assume that the maximum length of
1524 * a PSK identity hint can be as long as the maximum length of a PSK
1527 if (i > PSK_MAX_IDENTITY_LEN) {
1528 al = SSL_AD_HANDSHAKE_FAILURE;
1529 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1532 if (PACKET_remaining(&pkt) < i) {
1533 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1534 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1538 OPENSSL_free(s->session->psk_identity_hint);
1540 unsigned char *hint = NULL;
1542 if (!PACKET_get_bytes(&pkt, &hint, i)) {
1543 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1546 s->session->psk_identity_hint = BUF_strndup((char *)hint, i);
1547 if (s->session->psk_identity_hint == NULL) {
1548 al = SSL_AD_HANDSHAKE_FAILURE;
1549 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1553 s->session->psk_identity_hint = NULL;
1557 /* Nothing else to do for plain PSK or RSAPSK */
1558 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1560 #endif /* !OPENSSL_NO_PSK */
1561 #ifndef OPENSSL_NO_SRP
1562 if (alg_k & SSL_kSRP) {
1563 if (!PACKET_get_net_2(&pkt, &i)) {
1564 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1568 if (!PACKET_get_bytes(&pkt, &data, i)) {
1569 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1573 if ((s->srp_ctx.N = BN_bin2bn(data, i, NULL)) == NULL) {
1574 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1578 if (!PACKET_get_net_2(&pkt, &i)) {
1579 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1583 if (!PACKET_get_bytes(&pkt, &data, i)) {
1584 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1588 if ((s->srp_ctx.g = BN_bin2bn(data, i, NULL)) == NULL) {
1589 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1593 if (!PACKET_get_1(&pkt, &i)) {
1594 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1598 if (!PACKET_get_bytes(&pkt, &data, i)) {
1599 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1603 if ((s->srp_ctx.s = BN_bin2bn(data, i, NULL)) == NULL) {
1604 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1608 if (!PACKET_get_net_2(&pkt, &i)) {
1609 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1613 if (!PACKET_get_bytes(&pkt, &data, i)) {
1614 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1618 if ((s->srp_ctx.B = BN_bin2bn(data, i, NULL)) == NULL) {
1619 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1623 if (!srp_verify_server_param(s, &al)) {
1624 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1628 /* We must check if there is a certificate */
1629 if (alg_a & (SSL_aRSA|SSL_aDSS))
1630 pkey = X509_get_pubkey(s->session->peer);
1632 #endif /* !OPENSSL_NO_SRP */
1633 #ifndef OPENSSL_NO_RSA
1634 if (alg_k & SSL_kRSA) {
1635 /* Temporary RSA keys only allowed in export ciphersuites */
1636 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1637 al = SSL_AD_UNEXPECTED_MESSAGE;
1638 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1641 if ((rsa = RSA_new()) == NULL) {
1642 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1646 if (!PACKET_get_net_2(&pkt, &i)) {
1647 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1651 if (!PACKET_get_bytes(&pkt, &data, i)) {
1652 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1656 if ((rsa->n = BN_bin2bn(data, i, rsa->n)) == NULL) {
1657 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1661 if (!PACKET_get_net_2(&pkt, &i)) {
1662 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1666 if (!PACKET_get_bytes(&pkt, &data, i)) {
1667 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1671 if ((rsa->e = BN_bin2bn(data, i, rsa->e)) == NULL) {
1672 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1676 /* this should be because we are using an export cipher */
1677 if (alg_a & SSL_aRSA)
1678 pkey = X509_get_pubkey(s->session->peer);
1680 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1684 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1685 al = SSL_AD_UNEXPECTED_MESSAGE;
1686 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1690 s->s3->peer_rsa_tmp = rsa;
1693 #else /* OPENSSL_NO_RSA */
1696 #ifndef OPENSSL_NO_DH
1697 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1698 if ((dh = DH_new()) == NULL) {
1699 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1703 if (!PACKET_get_net_2(&pkt, &i)) {
1704 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1708 if (!PACKET_get_bytes(&pkt, &data, i)) {
1709 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1713 if ((dh->p = BN_bin2bn(data, i, NULL)) == NULL) {
1714 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1718 if (BN_is_zero(dh->p)) {
1719 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1723 if (!PACKET_get_net_2(&pkt, &i)) {
1724 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1728 if (!PACKET_get_bytes(&pkt, &data, i)) {
1729 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1733 if ((dh->g = BN_bin2bn(data, i, NULL)) == NULL) {
1734 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1738 if (BN_is_zero(dh->g)) {
1739 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1743 if (!PACKET_get_net_2(&pkt, &i)) {
1744 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1748 if (!PACKET_get_bytes(&pkt, &data, i)) {
1749 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1753 if ((dh->pub_key = BN_bin2bn(data, i, NULL)) == NULL) {
1754 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1758 if (BN_is_zero(dh->pub_key)) {
1759 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1763 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1764 al = SSL_AD_HANDSHAKE_FAILURE;
1765 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1768 if (alg_a & (SSL_aRSA|SSL_aDSS))
1769 pkey = X509_get_pubkey(s->session->peer);
1770 /* else anonymous DH, so no certificate or pkey. */
1772 s->s3->peer_dh_tmp = dh;
1775 #endif /* !OPENSSL_NO_DH */
1777 #ifndef OPENSSL_NO_EC
1778 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1780 const EC_GROUP *group;
1782 if ((ecdh = EC_KEY_new()) == NULL) {
1783 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1788 * Extract elliptic curve parameters and the server's ephemeral ECDH
1789 * public key. Keep accumulating lengths of various components in
1790 * param_len and make sure it never exceeds n.
1794 * XXX: For now we only support named (not generic) curves and the
1795 * ECParameters in this case is just three bytes.
1797 if (!PACKET_get_bytes(&pkt, &data, 3)) {
1798 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1802 * Check curve is one of our preferences, if not server has sent an
1803 * invalid curve. ECParameters is 3 bytes.
1805 if (!tls1_check_curve(s, data, 3)) {
1806 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1810 if ((curve_nid = tls1_ec_curve_id2nid(*(data + 2))) == 0) {
1811 al = SSL_AD_INTERNAL_ERROR;
1812 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1813 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1817 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1818 if (ngroup == NULL) {
1819 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1822 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1823 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1826 EC_GROUP_free(ngroup);
1828 group = EC_KEY_get0_group(ecdh);
1830 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1831 (EC_GROUP_get_degree(group) > 163)) {
1832 al = SSL_AD_EXPORT_RESTRICTION;
1833 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1834 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1838 /* Next, get the encoded ECPoint */
1839 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1840 ((bn_ctx = BN_CTX_new()) == NULL)) {
1841 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1845 if (!PACKET_get_1(&pkt, &encoded_pt_len)) {
1846 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1850 if (!PACKET_get_bytes(&pkt, &data, encoded_pt_len) ||
1851 (EC_POINT_oct2point(group, srvr_ecpoint,
1852 data, encoded_pt_len, bn_ctx) == 0)) {
1853 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1858 * The ECC/TLS specification does not mention the use of DSA to sign
1859 * ECParameters in the server key exchange message. We do support RSA
1863 # ifndef OPENSSL_NO_RSA
1864 else if (alg_a & SSL_aRSA)
1865 pkey = X509_get_pubkey(s->session->peer);
1867 # ifndef OPENSSL_NO_EC
1868 else if (alg_a & SSL_aECDSA)
1869 pkey = X509_get_pubkey(s->session->peer);
1871 /* else anonymous ECDH, so no certificate or pkey. */
1872 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1873 s->s3->peer_ecdh_tmp = ecdh;
1875 BN_CTX_free(bn_ctx);
1877 EC_POINT_free(srvr_ecpoint);
1878 srvr_ecpoint = NULL;
1880 al = SSL_AD_UNEXPECTED_MESSAGE;
1881 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1884 #endif /* !OPENSSL_NO_EC */
1887 * |pkt| now points to the beginning of the signature, so the difference
1888 * equals the length of the parameters.
1890 param_len = PACKET_remaining(&save_param_start) - PACKET_remaining(&pkt);
1892 /* if it was signed, check the signature */
1894 if (SSL_USE_SIGALGS(s)) {
1896 if (!PACKET_get_bytes(&pkt, &data, 2)) {
1897 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1900 rv = tls12_check_peer_sigalg(&md, s, data, pkey);
1907 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1912 if (!PACKET_get_net_2(&pkt, &i)) {
1913 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1916 j = EVP_PKEY_size(pkey);
1918 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1923 * Check signature length
1925 if (i > (unsigned int)j
1926 || !PACKET_get_bytes(&pkt, &data, i)
1927 || PACKET_remaining(&pkt) != 0) {
1928 /* wrong packet length */
1929 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1932 pkt = save_param_start;
1933 if (!PACKET_get_bytes(&pkt, ¶m, param_len)) {
1934 al = SSL_AD_INTERNAL_ERROR;
1935 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1938 #ifndef OPENSSL_NO_RSA
1939 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1945 for (num = 2; num > 0; num--) {
1946 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1947 EVP_DigestInit_ex(&md_ctx, (num == 2)
1948 ? s->ctx->md5 : s->ctx->sha1, NULL);
1949 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1951 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1953 EVP_DigestUpdate(&md_ctx, param, param_len);
1954 EVP_DigestFinal_ex(&md_ctx, q, &size);
1959 RSA_verify(NID_md5_sha1, md_buf, j, data, i, pkey->pkey.rsa);
1960 if (verify_ret < 0) {
1961 al = SSL_AD_DECRYPT_ERROR;
1962 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1965 if (verify_ret == 0) {
1967 al = SSL_AD_DECRYPT_ERROR;
1968 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1974 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1975 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1977 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1979 EVP_VerifyUpdate(&md_ctx, param, param_len);
1980 if (EVP_VerifyFinal(&md_ctx, data, (int)i, pkey) <= 0) {
1982 al = SSL_AD_DECRYPT_ERROR;
1983 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1988 /* aNULL, aSRP or PSK do not need public keys */
1989 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1990 /* Might be wrong key type, check it */
1991 if (ssl3_check_cert_and_algorithm(s))
1992 /* Otherwise this shouldn't happen */
1993 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1996 /* still data left over */
1997 if (PACKET_remaining(&pkt) != 0) {
1998 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2002 EVP_PKEY_free(pkey);
2003 EVP_MD_CTX_cleanup(&md_ctx);
2006 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2008 EVP_PKEY_free(pkey);
2009 #ifndef OPENSSL_NO_RSA
2012 #ifndef OPENSSL_NO_DH
2015 #ifndef OPENSSL_NO_EC
2016 BN_CTX_free(bn_ctx);
2017 EC_POINT_free(srvr_ecpoint);
2020 EVP_MD_CTX_cleanup(&md_ctx);
2021 s->state = SSL_ST_ERR;
2025 int ssl3_get_certificate_request(SSL *s)
2029 unsigned int list_len, ctype_num, i, name_len;
2030 X509_NAME *xn = NULL;
2031 unsigned char *data;
2032 unsigned char *namestart, *namebytes;
2033 STACK_OF(X509_NAME) *ca_sk = NULL;
2036 n = s->method->ssl_get_message(s,
2037 SSL3_ST_CR_CERT_REQ_A,
2038 SSL3_ST_CR_CERT_REQ_B,
2039 -1, s->max_cert_list, &ok);
2044 s->s3->tmp.cert_req = 0;
2046 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2047 s->s3->tmp.reuse_message = 1;
2049 * If we get here we don't need any cached handshake records as we
2050 * wont be doing client auth.
2052 if (!ssl3_digest_cached_records(s, 0))
2057 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2058 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2059 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2063 /* TLS does not like anon-DH with client cert */
2064 if (s->version > SSL3_VERSION) {
2065 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2066 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2067 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2068 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2073 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2074 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2075 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2079 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2080 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2084 /* get the certificate types */
2085 if (!PACKET_get_1(&pkt, &ctype_num)
2086 || !PACKET_get_bytes(&pkt, &data, ctype_num)) {
2087 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2088 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2091 OPENSSL_free(s->cert->ctypes);
2092 s->cert->ctypes = NULL;
2093 if (ctype_num > SSL3_CT_NUMBER) {
2094 /* If we exceed static buffer copy all to cert structure */
2095 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2096 if (s->cert->ctypes == NULL) {
2097 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2100 memcpy(s->cert->ctypes, data, ctype_num);
2101 s->cert->ctype_num = (size_t)ctype_num;
2102 ctype_num = SSL3_CT_NUMBER;
2104 for (i = 0; i < ctype_num; i++)
2105 s->s3->tmp.ctype[i] = data[i];
2107 if (SSL_USE_SIGALGS(s)) {
2108 if (!PACKET_get_net_2(&pkt, &list_len)
2109 || !PACKET_get_bytes(&pkt, &data, list_len)) {
2110 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2111 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2115 /* Clear certificate digests and validity flags */
2116 for (i = 0; i < SSL_PKEY_NUM; i++) {
2117 s->s3->tmp.md[i] = NULL;
2118 s->s3->tmp.valid_flags[i] = 0;
2120 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2121 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2122 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2123 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2126 if (!tls1_process_sigalgs(s)) {
2127 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2128 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2133 /* get the CA RDNs */
2134 if (!PACKET_get_net_2(&pkt, &list_len)
2135 || PACKET_remaining(&pkt) != list_len) {
2136 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2137 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2141 while (PACKET_remaining(&pkt)) {
2142 if (!PACKET_get_net_2(&pkt, &name_len)
2143 || !PACKET_get_bytes(&pkt, &namebytes, name_len)) {
2144 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2145 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2149 namestart = namebytes;
2151 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2152 name_len)) == NULL) {
2153 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2154 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2158 if (namebytes != (namestart + name_len)) {
2159 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2160 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2161 SSL_R_CA_DN_LENGTH_MISMATCH);
2164 if (!sk_X509_NAME_push(ca_sk, xn)) {
2165 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2170 /* we should setup a certificate to return.... */
2171 s->s3->tmp.cert_req = 1;
2172 s->s3->tmp.ctype_num = ctype_num;
2173 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2174 s->s3->tmp.ca_names = ca_sk;
2180 s->state = SSL_ST_ERR;
2182 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2186 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2188 return (X509_NAME_cmp(*a, *b));
2191 int ssl3_get_new_session_ticket(SSL *s)
2193 int ok, al, ret = 0;
2194 unsigned int ticklen;
2195 unsigned long ticket_lifetime_hint;
2199 n = s->method->ssl_get_message(s,
2200 SSL3_ST_CR_SESSION_TICKET_A,
2201 SSL3_ST_CR_SESSION_TICKET_B,
2202 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2207 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2208 al = SSL_AD_INTERNAL_ERROR;
2209 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
2213 if (!PACKET_get_net_4(&pkt, &ticket_lifetime_hint)
2214 || !PACKET_get_net_2(&pkt, &ticklen)
2215 || PACKET_remaining(&pkt) != ticklen) {
2216 al = SSL_AD_DECODE_ERROR;
2217 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2221 /* Server is allowed to change its mind and send an empty ticket. */
2225 if (s->session->session_id_length > 0) {
2226 int i = s->session_ctx->session_cache_mode;
2227 SSL_SESSION *new_sess;
2229 * We reused an existing session, so we need to replace it with a new
2232 if (i & SSL_SESS_CACHE_CLIENT) {
2234 * Remove the old session from the cache
2236 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2237 if (s->session_ctx->remove_session_cb != NULL)
2238 s->session_ctx->remove_session_cb(s->session_ctx,
2241 /* We carry on if this fails */
2242 SSL_CTX_remove_session(s->session_ctx, s->session);
2246 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2247 al = SSL_AD_INTERNAL_ERROR;
2248 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2252 SSL_SESSION_free(s->session);
2253 s->session = new_sess;
2256 OPENSSL_free(s->session->tlsext_tick);
2257 s->session->tlsext_ticklen = 0;
2259 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2260 if (!s->session->tlsext_tick) {
2261 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2264 if (!PACKET_copy_bytes(&pkt, s->session->tlsext_tick, ticklen)) {
2265 al = SSL_AD_DECODE_ERROR;
2266 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2270 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2271 s->session->tlsext_ticklen = ticklen;
2273 * There are two ways to detect a resumed ticket session. One is to set
2274 * an appropriate session ID and then the server must return a match in
2275 * ServerHello. This allows the normal client session ID matching to work
2276 * and we know much earlier that the ticket has been accepted. The
2277 * other way is to set zero length session ID when the ticket is
2278 * presented and rely on the handshake to determine session resumption.
2279 * We choose the former approach because this fits in with assumptions
2280 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2281 * SHA256 is disabled) hash of the ticket.
2283 EVP_Digest(s->session->tlsext_tick, ticklen,
2284 s->session->session_id, &s->session->session_id_length,
2285 EVP_sha256(), NULL);
2289 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2291 s->state = SSL_ST_ERR;
2295 int ssl3_get_cert_status(SSL *s)
2298 unsigned long resplen, n;
2302 n = s->method->ssl_get_message(s,
2303 SSL3_ST_CR_CERT_STATUS_A,
2304 SSL3_ST_CR_CERT_STATUS_B,
2305 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2310 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2311 al = SSL_AD_INTERNAL_ERROR;
2312 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_INTERNAL_ERROR);
2315 if (!PACKET_get_1(&pkt, &type)
2316 || type != TLSEXT_STATUSTYPE_ocsp) {
2317 al = SSL_AD_DECODE_ERROR;
2318 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2321 if (!PACKET_get_net_3(&pkt, &resplen)
2322 || PACKET_remaining(&pkt) != resplen) {
2323 al = SSL_AD_DECODE_ERROR;
2324 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2327 OPENSSL_free(s->tlsext_ocsp_resp);
2328 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2329 if (!s->tlsext_ocsp_resp) {
2330 al = SSL_AD_INTERNAL_ERROR;
2331 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2334 if (!PACKET_copy_bytes(&pkt, s->tlsext_ocsp_resp, resplen)) {
2335 al = SSL_AD_DECODE_ERROR;
2336 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2339 s->tlsext_ocsp_resplen = resplen;
2340 if (s->ctx->tlsext_status_cb) {
2342 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2344 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2345 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2349 al = SSL_AD_INTERNAL_ERROR;
2350 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2356 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2357 s->state = SSL_ST_ERR;
2361 int ssl3_get_server_done(SSL *s)
2366 /* Second to last param should be very small, like 0 :-) */
2367 n = s->method->ssl_get_message(s,
2368 SSL3_ST_CR_SRVR_DONE_A,
2369 SSL3_ST_CR_SRVR_DONE_B,
2370 SSL3_MT_SERVER_DONE, 30, &ok);
2375 /* should contain no data */
2376 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2377 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2378 s->state = SSL_ST_ERR;
2385 int ssl3_send_client_key_exchange(SSL *s)
2389 #ifndef OPENSSL_NO_PSK
2390 size_t pskhdrlen = 0;
2392 unsigned long alg_k;
2393 #ifndef OPENSSL_NO_RSA
2395 EVP_PKEY *pkey = NULL;
2397 #ifndef OPENSSL_NO_EC
2398 EC_KEY *clnt_ecdh = NULL;
2399 const EC_POINT *srvr_ecpoint = NULL;
2400 EVP_PKEY *srvr_pub_pkey = NULL;
2401 unsigned char *encodedPoint = NULL;
2402 int encoded_pt_len = 0;
2403 BN_CTX *bn_ctx = NULL;
2405 unsigned char *pms = NULL;
2407 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2409 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2410 p = ssl_handshake_start(s);
2413 #ifndef OPENSSL_NO_PSK
2414 if (alg_k & SSL_PSK) {
2417 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2418 * \0-terminated identity. The last byte is for us for simulating
2421 char identity[PSK_MAX_IDENTITY_LEN + 1];
2423 unsigned char psk[PSK_MAX_PSK_LEN];
2426 if (s->psk_client_callback == NULL) {
2427 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2428 SSL_R_PSK_NO_CLIENT_CB);
2432 memset(identity, 0, sizeof(identity));
2434 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2435 identity, sizeof(identity) - 1,
2438 if (psklen > PSK_MAX_PSK_LEN) {
2439 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2440 ERR_R_INTERNAL_ERROR);
2442 } else if (psklen == 0) {
2443 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2444 SSL_R_PSK_IDENTITY_NOT_FOUND);
2448 OPENSSL_free(s->s3->tmp.psk);
2449 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2450 OPENSSL_cleanse(psk, psklen);
2452 if (s->s3->tmp.psk == NULL) {
2453 OPENSSL_cleanse(identity, sizeof(identity));
2457 s->s3->tmp.psklen = psklen;
2459 identitylen = strlen(identity);
2460 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2461 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2462 ERR_R_INTERNAL_ERROR);
2465 OPENSSL_free(s->session->psk_identity);
2466 s->session->psk_identity = BUF_strdup(identity);
2467 if (s->session->psk_identity == NULL) {
2468 OPENSSL_cleanse(identity, sizeof(identity));
2472 s2n(identitylen, p);
2473 memcpy(p, identity, identitylen);
2474 pskhdrlen = 2 + identitylen;
2478 OPENSSL_cleanse(identity, sizeof(identity));
2480 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2484 if (alg_k & SSL_kPSK) {
2489 /* Fool emacs indentation */
2492 #ifndef OPENSSL_NO_RSA
2493 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2495 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2496 pms = OPENSSL_malloc(pmslen);
2500 if (s->session->peer == NULL) {
2502 * We should always have a server certificate with SSL_kRSA.
2504 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2505 ERR_R_INTERNAL_ERROR);
2509 if (s->s3->peer_rsa_tmp != NULL)
2510 rsa = s->s3->peer_rsa_tmp;
2512 pkey = X509_get_pubkey(s->session->peer);
2513 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2514 || (pkey->pkey.rsa == NULL)) {
2515 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2516 ERR_R_INTERNAL_ERROR);
2519 rsa = pkey->pkey.rsa;
2520 EVP_PKEY_free(pkey);
2523 pms[0] = s->client_version >> 8;
2524 pms[1] = s->client_version & 0xff;
2525 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2529 /* Fix buf for TLS and beyond */
2530 if (s->version > SSL3_VERSION)
2532 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2534 if (s->options & SSL_OP_PKCS1_CHECK_1)
2536 if (s->options & SSL_OP_PKCS1_CHECK_2)
2540 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2541 SSL_R_BAD_RSA_ENCRYPT);
2545 /* Fix buf for TLS and beyond */
2546 if (s->version > SSL3_VERSION) {
2552 #ifndef OPENSSL_NO_DH
2553 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2554 DH *dh_srvr, *dh_clnt;
2555 if (s->s3->peer_dh_tmp != NULL)
2556 dh_srvr = s->s3->peer_dh_tmp;
2558 /* we get them from the cert */
2559 EVP_PKEY *spkey = NULL;
2561 spkey = X509_get_pubkey(s->session->peer);
2563 dh_srvr = EVP_PKEY_get1_DH(spkey);
2564 EVP_PKEY_free(spkey);
2566 if (dh_srvr == NULL) {
2567 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2568 ERR_R_INTERNAL_ERROR);
2572 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2573 /* Use client certificate key */
2574 EVP_PKEY *clkey = s->cert->key->privatekey;
2577 dh_clnt = EVP_PKEY_get1_DH(clkey);
2578 if (dh_clnt == NULL) {
2579 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2580 ERR_R_INTERNAL_ERROR);
2584 /* generate a new random key */
2585 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2586 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2589 if (!DH_generate_key(dh_clnt)) {
2590 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2596 pmslen = DH_size(dh_clnt);
2597 pms = OPENSSL_malloc(pmslen);
2602 * use the 'p' output buffer for the DH key, but make sure to
2603 * clear it out afterwards
2606 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2607 if (s->s3->peer_dh_tmp == NULL)
2611 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2617 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2620 /* send off the data */
2621 n = BN_num_bytes(dh_clnt->pub_key);
2623 BN_bn2bin(dh_clnt->pub_key, p);
2631 #ifndef OPENSSL_NO_EC
2632 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2633 const EC_GROUP *srvr_group = NULL;
2635 int ecdh_clnt_cert = 0;
2638 * Did we send out the client's ECDH share for use in premaster
2639 * computation as part of client certificate? If so, set
2640 * ecdh_clnt_cert to 1.
2642 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2644 * XXX: For now, we do not support client
2645 * authentication using ECDH certificates.
2646 * To add such support, one needs to add
2647 * code that checks for appropriate
2648 * conditions and sets ecdh_clnt_cert to 1.
2649 * For example, the cert have an ECC
2650 * key on the same curve as the server's
2651 * and the key should be authorized for
2654 * One also needs to add code in ssl3_connect
2655 * to skip sending the certificate verify
2658 * if ((s->cert->key->privatekey != NULL) &&
2659 * (s->cert->key->privatekey->type ==
2660 * EVP_PKEY_EC) && ...)
2661 * ecdh_clnt_cert = 1;
2665 if (s->s3->peer_ecdh_tmp != NULL) {
2666 tkey = s->s3->peer_ecdh_tmp;
2668 /* Get the Server Public Key from Cert */
2669 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2670 if ((srvr_pub_pkey == NULL)
2671 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2672 || (srvr_pub_pkey->pkey.ec == NULL)) {
2673 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2674 ERR_R_INTERNAL_ERROR);
2678 tkey = srvr_pub_pkey->pkey.ec;
2681 srvr_group = EC_KEY_get0_group(tkey);
2682 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2684 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2685 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2686 ERR_R_INTERNAL_ERROR);
2690 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2691 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2692 ERR_R_MALLOC_FAILURE);
2696 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2697 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2700 if (ecdh_clnt_cert) {
2702 * Reuse key info from our certificate We only need our
2703 * private key to perform the ECDH computation.
2705 const BIGNUM *priv_key;
2706 tkey = s->cert->key->privatekey->pkey.ec;
2707 priv_key = EC_KEY_get0_private_key(tkey);
2708 if (priv_key == NULL) {
2709 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2710 ERR_R_MALLOC_FAILURE);
2713 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2714 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2718 /* Generate a new ECDH key pair */
2719 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2720 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2727 * use the 'p' output buffer for the ECDH key, but make sure to
2728 * clear it out afterwards
2731 field_size = EC_GROUP_get_degree(srvr_group);
2732 if (field_size <= 0) {
2733 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2736 pmslen = (field_size + 7) / 8;
2737 pms = OPENSSL_malloc(pmslen);
2740 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2741 if (n <= 0 || pmslen != (size_t)n) {
2742 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2746 if (ecdh_clnt_cert) {
2747 /* Send empty client key exch message */
2751 * First check the size of encoding and allocate memory
2755 EC_POINT_point2oct(srvr_group,
2756 EC_KEY_get0_public_key(clnt_ecdh),
2757 POINT_CONVERSION_UNCOMPRESSED,
2760 encodedPoint = (unsigned char *)
2761 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2762 bn_ctx = BN_CTX_new();
2763 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2764 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2765 ERR_R_MALLOC_FAILURE);
2769 /* Encode the public key */
2770 n = EC_POINT_point2oct(srvr_group,
2771 EC_KEY_get0_public_key(clnt_ecdh),
2772 POINT_CONVERSION_UNCOMPRESSED,
2773 encodedPoint, encoded_pt_len, bn_ctx);
2775 *p = n; /* length of encoded point */
2776 /* Encoded point will be copied here */
2778 /* copy the point */
2779 memcpy(p, encodedPoint, n);
2780 /* increment n to account for length field */
2784 /* Free allocated memory */
2785 BN_CTX_free(bn_ctx);
2786 OPENSSL_free(encodedPoint);
2787 EC_KEY_free(clnt_ecdh);
2788 EVP_PKEY_free(srvr_pub_pkey);
2790 #endif /* !OPENSSL_NO_EC */
2791 else if (alg_k & SSL_kGOST) {
2792 /* GOST key exchange message creation */
2793 EVP_PKEY_CTX *pkey_ctx;
2796 unsigned int md_len;
2797 unsigned char shared_ukm[32], tmp[256];
2798 EVP_MD_CTX *ukm_hash;
2802 pms = OPENSSL_malloc(pmslen);
2807 * Get server sertificate PKEY and create ctx from it
2809 peer_cert = s->session->peer;
2811 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2812 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2816 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2817 X509_get_pubkey(peer_cert), NULL);
2819 * If we have send a certificate, and certificate key
2821 * * parameters match those of server certificate, use
2822 * certificate key for key exchange
2825 /* Otherwise, generate ephemeral key pair */
2827 EVP_PKEY_encrypt_init(pkey_ctx);
2828 /* Generate session key */
2829 if (RAND_bytes(pms, pmslen) <= 0) {
2830 EVP_PKEY_CTX_free(pkey_ctx);
2831 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2832 ERR_R_INTERNAL_ERROR);
2836 * If we have client certificate, use its secret as peer key
2838 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2839 if (EVP_PKEY_derive_set_peer
2840 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2842 * If there was an error - just ignore it. Ephemeral key
2849 * Compute shared IV and store it in algorithm-specific context
2852 ukm_hash = EVP_MD_CTX_create();
2853 EVP_DigestInit(ukm_hash,
2854 EVP_get_digestbynid(NID_id_GostR3411_94));
2855 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2857 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2859 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2860 EVP_MD_CTX_destroy(ukm_hash);
2861 if (EVP_PKEY_CTX_ctrl
2862 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2864 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2868 /* Make GOST keytransport blob message */
2870 * Encapsulate it into sequence
2872 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2874 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2875 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2879 if (msglen >= 0x80) {
2881 *(p++) = msglen & 0xff;
2884 *(p++) = msglen & 0xff;
2887 memcpy(p, tmp, msglen);
2888 /* Check if pubkey from client certificate was used */
2889 if (EVP_PKEY_CTX_ctrl
2890 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2891 /* Set flag "skip certificate verify" */
2892 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2894 EVP_PKEY_CTX_free(pkey_ctx);
2895 EVP_PKEY_free(pub_key);
2898 #ifndef OPENSSL_NO_SRP
2899 else if (alg_k & SSL_kSRP) {
2900 if (s->srp_ctx.A != NULL) {
2901 /* send off the data */
2902 n = BN_num_bytes(s->srp_ctx.A);
2904 BN_bn2bin(s->srp_ctx.A, p);
2907 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2908 ERR_R_INTERNAL_ERROR);
2911 OPENSSL_free(s->session->srp_username);
2912 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2913 if (s->session->srp_username == NULL) {
2914 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2915 ERR_R_MALLOC_FAILURE);
2921 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2922 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2926 #ifndef OPENSSL_NO_PSK
2930 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2931 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2932 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2936 s->state = SSL3_ST_CW_KEY_EXCH_B;
2939 /* SSL3_ST_CW_KEY_EXCH_B */
2940 n = ssl_do_write(s);
2941 #ifndef OPENSSL_NO_SRP
2943 if (alg_k & SSL_kSRP) {
2945 * If everything written generate master key: no need to save PMS as
2946 * srp_generate_client_master_secret generates it internally.
2949 if (!srp_generate_client_master_secret(s)) {
2950 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2951 ERR_R_INTERNAL_ERROR);
2957 /* If we haven't written everything save PMS */
2959 s->s3->tmp.pms = pms;
2960 s->s3->tmp.pmslen = pmslen;
2962 /* If we don't have a PMS restore */
2964 pms = s->s3->tmp.pms;
2965 pmslen = s->s3->tmp.pmslen;
2967 if (pms == NULL && !(alg_k & SSL_kPSK)) {
2968 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2969 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2972 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2973 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2974 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2980 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2981 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2983 OPENSSL_clear_free(pms, pmslen);
2984 s->s3->tmp.pms = NULL;
2985 #ifndef OPENSSL_NO_EC
2986 BN_CTX_free(bn_ctx);
2987 OPENSSL_free(encodedPoint);
2988 EC_KEY_free(clnt_ecdh);
2989 EVP_PKEY_free(srvr_pub_pkey);
2991 #ifndef OPENSSL_NO_PSK
2992 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2993 s->s3->tmp.psk = NULL;
2995 s->state = SSL_ST_ERR;
2999 int ssl3_send_client_verify(SSL *s)
3002 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3004 EVP_PKEY_CTX *pctx = NULL;
3010 EVP_MD_CTX_init(&mctx);
3012 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3013 p = ssl_handshake_start(s);
3014 pkey = s->cert->key->privatekey;
3015 /* Create context from key and test if sha1 is allowed as digest */
3016 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3017 EVP_PKEY_sign_init(pctx);
3018 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3019 if (!SSL_USE_SIGALGS(s))
3020 s->method->ssl3_enc->cert_verify_mac(s,
3023 [MD5_DIGEST_LENGTH]));
3028 * For TLS v1.2 send signature algorithm and signature using agreed
3029 * digest and cached handshake records.
3031 if (SSL_USE_SIGALGS(s)) {
3034 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3035 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3036 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3037 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3042 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3045 if (!EVP_SignInit_ex(&mctx, md, NULL)
3046 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3047 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3048 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3053 /* Digest cached records and discard handshake buffer */
3054 if (!ssl3_digest_cached_records(s, 0))
3057 #ifndef OPENSSL_NO_RSA
3058 if (pkey->type == EVP_PKEY_RSA) {
3059 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3060 if (RSA_sign(NID_md5_sha1, data,
3061 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3062 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3063 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3070 #ifndef OPENSSL_NO_DSA
3071 if (pkey->type == EVP_PKEY_DSA) {
3072 if (!DSA_sign(pkey->save_type,
3073 &(data[MD5_DIGEST_LENGTH]),
3074 SHA_DIGEST_LENGTH, &(p[2]),
3075 (unsigned int *)&j, pkey->pkey.dsa)) {
3076 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3083 #ifndef OPENSSL_NO_EC
3084 if (pkey->type == EVP_PKEY_EC) {
3085 if (!ECDSA_sign(pkey->save_type,
3086 &(data[MD5_DIGEST_LENGTH]),
3087 SHA_DIGEST_LENGTH, &(p[2]),
3088 (unsigned int *)&j, pkey->pkey.ec)) {
3089 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3096 if (pkey->type == NID_id_GostR3410_2001) {
3097 unsigned char signbuf[64];
3099 size_t sigsize = 64;
3100 s->method->ssl3_enc->cert_verify_mac(s,
3101 NID_id_GostR3411_94, data);
3102 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3103 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3106 for (i = 63, j = 0; i >= 0; j++, i--) {
3107 p[2 + j] = signbuf[i];
3112 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3115 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3116 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3119 s->state = SSL3_ST_CW_CERT_VRFY_B;
3121 EVP_MD_CTX_cleanup(&mctx);
3122 EVP_PKEY_CTX_free(pctx);
3123 return ssl_do_write(s);
3125 EVP_MD_CTX_cleanup(&mctx);
3126 EVP_PKEY_CTX_free(pctx);
3127 s->state = SSL_ST_ERR;
3132 * Check a certificate can be used for client authentication. Currently check
3133 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3134 * certificates can be used and optionally checks suitability for Suite B.
3136 static int ssl3_check_client_certificate(SSL *s)
3138 unsigned long alg_k;
3139 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3141 /* If no suitable signature algorithm can't use certificate */
3142 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3145 * If strict mode check suitability of chain before using it. This also
3146 * adjusts suite B digest if necessary.
3148 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3149 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3151 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3152 /* See if we can use client certificate for fixed DH */
3153 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3154 int i = s->session->peer_type;
3155 EVP_PKEY *clkey = NULL, *spkey = NULL;
3156 clkey = s->cert->key->privatekey;
3157 /* If client key not DH assume it can be used */
3158 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3161 spkey = X509_get_pubkey(s->session->peer);
3163 /* Compare server and client parameters */
3164 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3165 EVP_PKEY_free(spkey);
3169 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3174 int ssl3_send_client_certificate(SSL *s)
3177 EVP_PKEY *pkey = NULL;
3180 if (s->state == SSL3_ST_CW_CERT_A) {
3181 /* Let cert callback update client certificates if required */
3182 if (s->cert->cert_cb) {
3183 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3185 s->rwstate = SSL_X509_LOOKUP;
3189 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3190 s->state = SSL_ST_ERR;
3193 s->rwstate = SSL_NOTHING;
3195 if (ssl3_check_client_certificate(s))
3196 s->state = SSL3_ST_CW_CERT_C;
3198 s->state = SSL3_ST_CW_CERT_B;
3201 /* We need to get a client cert */
3202 if (s->state == SSL3_ST_CW_CERT_B) {
3204 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3205 * return(-1); We then get retied later
3208 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3210 s->rwstate = SSL_X509_LOOKUP;
3213 s->rwstate = SSL_NOTHING;
3214 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3215 s->state = SSL3_ST_CW_CERT_B;
3216 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3218 } else if (i == 1) {
3220 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3221 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3225 EVP_PKEY_free(pkey);
3226 if (i && !ssl3_check_client_certificate(s))
3229 if (s->version == SSL3_VERSION) {
3230 s->s3->tmp.cert_req = 0;
3231 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3234 s->s3->tmp.cert_req = 2;
3235 if (!ssl3_digest_cached_records(s, 0)) {
3236 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3237 s->state = SSL_ST_ERR;
3243 /* Ok, we have a cert */
3244 s->state = SSL3_ST_CW_CERT_C;
3247 if (s->state == SSL3_ST_CW_CERT_C) {
3248 s->state = SSL3_ST_CW_CERT_D;
3249 if (!ssl3_output_cert_chain(s,
3250 (s->s3->tmp.cert_req ==
3251 2) ? NULL : s->cert->key)) {
3252 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3253 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3254 s->state = SSL_ST_ERR;
3258 /* SSL3_ST_CW_CERT_D */
3259 return ssl_do_write(s);
3262 #define has_bits(i,m) (((i)&(m)) == (m))
3264 int ssl3_check_cert_and_algorithm(SSL *s)
3268 EVP_PKEY *pkey = NULL;
3270 #ifndef OPENSSL_NO_RSA
3273 #ifndef OPENSSL_NO_DH
3276 int al = SSL_AD_HANDSHAKE_FAILURE;
3278 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3279 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3281 /* we don't have a certificate */
3282 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3284 #ifndef OPENSSL_NO_RSA
3285 rsa = s->s3->peer_rsa_tmp;
3287 #ifndef OPENSSL_NO_DH
3288 dh = s->s3->peer_dh_tmp;
3291 /* This is the passed certificate */
3293 idx = s->session->peer_type;
3294 #ifndef OPENSSL_NO_EC
3295 if (idx == SSL_PKEY_ECC) {
3296 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3298 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3303 } else if (alg_a & SSL_aECDSA) {
3304 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3305 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3307 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3308 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3312 pkey = X509_get_pubkey(s->session->peer);
3313 pkey_bits = EVP_PKEY_bits(pkey);
3314 i = X509_certificate_type(s->session->peer, pkey);
3315 EVP_PKEY_free(pkey);
3317 /* Check that we have a certificate if we require one */
3318 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3319 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3320 SSL_R_MISSING_RSA_SIGNING_CERT);
3323 #ifndef OPENSSL_NO_DSA
3324 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3325 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3326 SSL_R_MISSING_DSA_SIGNING_CERT);
3330 #ifndef OPENSSL_NO_RSA
3331 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3332 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3333 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3334 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3335 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3337 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3338 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3339 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3340 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3341 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3345 /* server key exchange is not allowed. */
3346 al = SSL_AD_INTERNAL_ERROR;
3347 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3354 #ifndef OPENSSL_NO_DH
3355 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3356 al = SSL_AD_INTERNAL_ERROR;
3357 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3359 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3360 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3361 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3362 SSL_R_MISSING_DH_RSA_CERT);
3365 # ifndef OPENSSL_NO_DSA
3366 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3367 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3368 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3369 SSL_R_MISSING_DH_DSA_CERT);
3375 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3376 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3377 #ifndef OPENSSL_NO_RSA
3378 if (alg_k & SSL_kRSA) {
3380 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3381 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3383 } else if (RSA_bits(rsa) >
3384 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3385 /* We have a temporary RSA key but it's too large. */
3386 al = SSL_AD_EXPORT_RESTRICTION;
3387 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3388 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3393 #ifndef OPENSSL_NO_DH
3394 if (alg_k & SSL_kDHE) {
3396 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3397 /* We have a temporary DH key but it's too large. */
3398 al = SSL_AD_EXPORT_RESTRICTION;
3399 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3400 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3403 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3404 /* The cert should have had an export DH key. */
3405 al = SSL_AD_EXPORT_RESTRICTION;
3406 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3407 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3412 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3413 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3419 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3424 * Normally, we can tell if the server is resuming the session from
3425 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3426 * message after the ServerHello to determine if the server is resuming.
3427 * Therefore, we allow EAP-FAST to peek ahead.
3428 * ssl3_check_change returns 1 if we are resuming from an external
3429 * pre-shared secret, we have a "ticket" and the next server message
3430 * is CCS; and 0 otherwise. It returns -1 upon an error.
3432 static int ssl3_check_change(SSL *s)
3436 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3437 !s->session->tlsext_tick)
3441 * This function is called when we might get a Certificate message instead,
3442 * so permit appropriate message length.
3443 * We ignore the return value as we're only interested in the message type
3444 * and not its length.
3446 s->method->ssl_get_message(s,
3449 -1, s->max_cert_list, &ok);
3454 s->s3->tmp.reuse_message = 1;
3456 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC)
3462 #ifndef OPENSSL_NO_NEXTPROTONEG
3463 int ssl3_send_next_proto(SSL *s)
3465 unsigned int len, padding_len;
3468 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3469 len = s->next_proto_negotiated_len;
3470 padding_len = 32 - ((len + 2) % 32);
3471 d = (unsigned char *)s->init_buf->data;
3473 memcpy(d + 5, s->next_proto_negotiated, len);
3474 d[5 + len] = padding_len;
3475 memset(d + 6 + len, 0, padding_len);
3476 *(d++) = SSL3_MT_NEXT_PROTO;
3477 l2n3(2 + len + padding_len, d);
3478 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3479 s->init_num = 4 + 2 + len + padding_len;
3483 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3487 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3490 #ifndef OPENSSL_NO_ENGINE
3491 if (s->ctx->client_cert_engine) {
3492 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3493 SSL_get_client_CA_list(s),
3494 px509, ppkey, NULL, NULL, NULL);
3499 if (s->ctx->client_cert_cb)
3500 i = s->ctx->client_cert_cb(s, px509, ppkey);
3504 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3510 int empty_reneg_info_scsv = !s->renegotiate;
3511 /* Set disabled masks for this session */
3512 ssl_set_client_disabled(s);
3518 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3519 c = sk_SSL_CIPHER_value(sk, i);
3520 /* Skip disabled ciphers */
3521 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3523 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3524 if (c->id == SSL3_CK_SCSV) {
3525 if (!empty_reneg_info_scsv)
3528 empty_reneg_info_scsv = 0;
3531 j = s->method->put_cipher_by_char(c, p);
3535 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3539 if (empty_reneg_info_scsv) {
3540 static SSL_CIPHER scsv = {
3541 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3543 j = s->method->put_cipher_by_char(&scsv, p);
3545 #ifdef OPENSSL_RI_DEBUG
3547 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3550 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3551 static SSL_CIPHER scsv = {
3552 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3554 j = s->method->put_cipher_by_char(&scsv, p);