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);
383 if (s->s3->tmp.cert_req)
384 s->state = SSL3_ST_CW_CERT_A;
386 s->state = SSL3_ST_CW_KEY_EXCH_A;
391 case SSL3_ST_CW_CERT_A:
392 case SSL3_ST_CW_CERT_B:
393 case SSL3_ST_CW_CERT_C:
394 case SSL3_ST_CW_CERT_D:
395 ret = ssl3_send_client_certificate(s);
398 s->state = SSL3_ST_CW_KEY_EXCH_A;
402 case SSL3_ST_CW_KEY_EXCH_A:
403 case SSL3_ST_CW_KEY_EXCH_B:
404 ret = ssl3_send_client_key_exchange(s);
408 * EAY EAY EAY need to check for DH fix cert sent back
411 * For TLS, cert_req is set to 2, so a cert chain of nothing is
412 * sent, but no verify packet is sent
415 * XXX: For now, we do not support client authentication in ECDH
416 * cipher suites with ECDH (rather than ECDSA) certificates. We
417 * need to skip the certificate verify message when client's
418 * ECDH public key is sent inside the client certificate.
420 if (s->s3->tmp.cert_req == 1) {
421 s->state = SSL3_ST_CW_CERT_VRFY_A;
423 s->state = SSL3_ST_CW_CHANGE_A;
425 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
426 s->state = SSL3_ST_CW_CHANGE_A;
432 case SSL3_ST_CW_CERT_VRFY_A:
433 case SSL3_ST_CW_CERT_VRFY_B:
434 ret = ssl3_send_client_verify(s);
437 s->state = SSL3_ST_CW_CHANGE_A;
441 case SSL3_ST_CW_CHANGE_A:
442 case SSL3_ST_CW_CHANGE_B:
443 ret = ssl3_send_change_cipher_spec(s,
445 SSL3_ST_CW_CHANGE_B);
449 #if defined(OPENSSL_NO_NEXTPROTONEG)
450 s->state = SSL3_ST_CW_FINISHED_A;
452 if (s->s3->next_proto_neg_seen)
453 s->state = SSL3_ST_CW_NEXT_PROTO_A;
455 s->state = SSL3_ST_CW_FINISHED_A;
459 s->session->cipher = s->s3->tmp.new_cipher;
460 #ifdef OPENSSL_NO_COMP
461 s->session->compress_meth = 0;
463 if (s->s3->tmp.new_compression == NULL)
464 s->session->compress_meth = 0;
466 s->session->compress_meth = s->s3->tmp.new_compression->id;
468 if (!s->method->ssl3_enc->setup_key_block(s)) {
470 s->state = SSL_ST_ERR;
474 if (!s->method->ssl3_enc->change_cipher_state(s,
475 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
478 s->state = SSL_ST_ERR;
484 #if !defined(OPENSSL_NO_NEXTPROTONEG)
485 case SSL3_ST_CW_NEXT_PROTO_A:
486 case SSL3_ST_CW_NEXT_PROTO_B:
487 ret = ssl3_send_next_proto(s);
490 s->state = SSL3_ST_CW_FINISHED_A;
494 case SSL3_ST_CW_FINISHED_A:
495 case SSL3_ST_CW_FINISHED_B:
496 ret = ssl3_send_finished(s,
497 SSL3_ST_CW_FINISHED_A,
498 SSL3_ST_CW_FINISHED_B,
500 ssl3_enc->client_finished_label,
502 ssl3_enc->client_finished_label_len);
505 s->state = SSL3_ST_CW_FLUSH;
508 s->s3->tmp.next_state = SSL_ST_OK;
511 * Allow NewSessionTicket if ticket expected
513 if (s->tlsext_ticket_expected)
514 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
516 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE_A;
521 case SSL3_ST_CR_SESSION_TICKET_A:
522 case SSL3_ST_CR_SESSION_TICKET_B:
523 ret = ssl3_get_new_session_ticket(s);
526 s->state = SSL3_ST_CR_CHANGE_A;
530 case SSL3_ST_CR_CERT_STATUS_A:
531 case SSL3_ST_CR_CERT_STATUS_B:
532 ret = ssl3_get_cert_status(s);
535 s->state = SSL3_ST_CR_KEY_EXCH_A;
539 case SSL3_ST_CR_CHANGE_A:
540 case SSL3_ST_CR_CHANGE_B:
541 ret = ssl3_get_change_cipher_spec(s, SSL3_ST_CR_CHANGE_A,
542 SSL3_ST_CR_CHANGE_B);
546 s->state = SSL3_ST_CR_FINISHED_A;
550 case SSL3_ST_CR_FINISHED_A:
551 case SSL3_ST_CR_FINISHED_B:
552 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
553 SSL3_ST_CR_FINISHED_B);
558 s->state = SSL3_ST_CW_CHANGE_A;
560 s->state = SSL_ST_OK;
564 case SSL3_ST_CW_FLUSH:
565 s->rwstate = SSL_WRITING;
566 if (BIO_flush(s->wbio) <= 0) {
570 s->rwstate = SSL_NOTHING;
571 s->state = s->s3->tmp.next_state;
575 /* clean a few things up */
576 ssl3_cleanup_key_block(s);
577 BUF_MEM_free(s->init_buf);
580 /* remove the buffering */
581 ssl_free_wbio_buffer(s);
587 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
589 s->ctx->stats.sess_hit++;
593 s->handshake_func = ssl3_connect;
594 s->ctx->stats.sess_connect_good++;
597 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
604 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
610 /* did we do anything */
611 if (!s->s3->tmp.reuse_message && !skip) {
613 if ((ret = BIO_flush(s->wbio)) <= 0)
617 if ((cb != NULL) && (s->state != state)) {
618 new_state = s->state;
620 cb(s, SSL_CB_CONNECT_LOOP, 1);
621 s->state = new_state;
630 cb(s, SSL_CB_CONNECT_EXIT, ret);
635 * Work out what version we should be using for the initial ClientHello if
636 * the version is currently set to (D)TLS_ANY_VERSION.
637 * Returns 1 on success
640 static int ssl_set_version(SSL *s)
642 unsigned long mask, options = s->options;
644 if (s->method->version == TLS_ANY_VERSION) {
646 * SSL_OP_NO_X disables all protocols above X *if* there are
647 * some protocols below X enabled. This is required in order
648 * to maintain "version capability" vector contiguous. So
649 * that if application wants to disable TLS1.0 in favour of
650 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
651 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
653 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
654 #if !defined(OPENSSL_NO_SSL3)
658 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
659 if (options & SSL_OP_NO_TLSv1_2) {
660 if ((options & mask) != mask) {
661 s->version = TLS1_1_VERSION;
663 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
667 s->version = TLS1_2_VERSION;
670 if ((options & mask) == mask) {
671 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
674 s->version = TLS1_1_VERSION;
677 mask &= ~SSL_OP_NO_TLSv1_1;
678 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
679 s->version = TLS1_VERSION;
680 mask &= ~SSL_OP_NO_TLSv1;
681 #if !defined(OPENSSL_NO_SSL3)
682 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
683 s->version = SSL3_VERSION;
686 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
687 SSLerr(SSL_F_SSL_SET_VERSION,
688 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
692 if (s->version == SSL3_VERSION && FIPS_mode()) {
693 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
697 } else if (s->method->version == DTLS_ANY_VERSION) {
698 /* Determine which DTLS version to use */
699 /* If DTLS 1.2 disabled correct the version number */
700 if (options & SSL_OP_NO_DTLSv1_2) {
701 if (tls1_suiteb(s)) {
702 SSLerr(SSL_F_SSL_SET_VERSION,
703 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
707 * Disabling all versions is silly: return an error.
709 if (options & SSL_OP_NO_DTLSv1) {
710 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
714 * Update method so we don't use any DTLS 1.2 features.
716 s->method = DTLSv1_client_method();
717 s->version = DTLS1_VERSION;
720 * We only support one version: update method
722 if (options & SSL_OP_NO_DTLSv1)
723 s->method = DTLSv1_2_client_method();
724 s->version = DTLS1_2_VERSION;
728 s->client_version = s->version;
733 int ssl3_client_hello(SSL *s)
735 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
736 if(tls_construct_client_hello(s) == 0)
739 s->state = SSL3_ST_CW_CLNT_HELLO_B;
742 /* SSL3_ST_CW_CLNT_HELLO_B */
743 return ssl_do_write(s);
745 s->state = SSL_ST_ERR;
750 int tls_construct_client_hello(SSL *s)
753 unsigned char *p, *d;
757 #ifndef OPENSSL_NO_COMP
761 SSL_SESSION *sess = s->session;
763 buf = (unsigned char *)s->init_buf->data;
765 /* Work out what SSL/TLS/DTLS version to use */
766 if (ssl_set_version(s) == 0)
769 if ((sess == NULL) || (sess->ssl_version != s->version) ||
771 * In the case of EAP-FAST, we can have a pre-shared
772 * "ticket" without a session ID.
774 (!sess->session_id_length && !sess->tlsext_tick) ||
775 (sess->not_resumable)) {
776 if (!ssl_get_new_session(s, 0))
779 /* else use the pre-loaded session */
781 p = s->s3->client_random;
784 * for DTLS if client_random is initialized, reuse it, we are
785 * required to use same upon reply to HelloVerify
787 if (SSL_IS_DTLS(s)) {
790 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
799 if (i && ssl_fill_hello_random(s, 0, p,
800 sizeof(s->s3->client_random)) <= 0)
803 /* Do the message type and length last */
804 d = p = ssl_handshake_start(s);
807 * version indicates the negotiated version: for example from
808 * an SSLv2/v3 compatible client hello). The client_version
809 * field is the maximum version we permit and it is also
810 * used in RSA encrypted premaster secrets. Some servers can
811 * choke if we initially report a higher version then
812 * renegotiate to a lower one in the premaster secret. This
813 * didn't happen with TLS 1.0 as most servers supported it
814 * but it can with TLS 1.1 or later if the server only supports
817 * Possible scenario with previous logic:
818 * 1. Client hello indicates TLS 1.2
819 * 2. Server hello says TLS 1.0
820 * 3. RSA encrypted premaster secret uses 1.2.
821 * 4. Handhaked proceeds using TLS 1.0.
822 * 5. Server sends hello request to renegotiate.
823 * 6. Client hello indicates TLS v1.0 as we now
824 * know that is maximum server supports.
825 * 7. Server chokes on RSA encrypted premaster secret
826 * containing version 1.0.
828 * For interoperability it should be OK to always use the
829 * maximum version we support in client hello and then rely
830 * on the checking of version to ensure the servers isn't
831 * being inconsistent: for example initially negotiating with
832 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
833 * client_version in client hello and not resetting it to
834 * the negotiated version.
836 *(p++) = s->client_version >> 8;
837 *(p++) = s->client_version & 0xff;
840 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
841 p += SSL3_RANDOM_SIZE;
847 i = s->session->session_id_length;
850 if (i > (int)sizeof(s->session->session_id)) {
851 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
854 memcpy(p, s->session->session_id, i);
858 /* cookie stuff for DTLS */
859 if (SSL_IS_DTLS(s)) {
860 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
861 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
864 *(p++) = s->d1->cookie_len;
865 memcpy(p, s->d1->cookie, s->d1->cookie_len);
866 p += s->d1->cookie_len;
869 /* Ciphers supported */
870 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
872 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
875 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
877 * Some servers hang if client hello > 256 bytes as hack workaround
878 * chop number of supported ciphers to keep it well below this if we
881 if (TLS1_get_version(s) >= TLS1_2_VERSION
882 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
883 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
889 #ifdef OPENSSL_NO_COMP
893 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
896 j = sk_SSL_COMP_num(s->ctx->comp_methods);
898 for (i = 0; i < j; i++) {
899 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
903 *(p++) = 0; /* Add the NULL method */
906 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
907 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
911 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
913 ssl3_send_alert(s, SSL3_AL_FATAL, al);
914 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
919 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
920 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
921 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
931 int ssl3_get_server_hello(SSL *s)
936 * Hello verify request and/or server hello version may not match so set
937 * first packet if we're negotiating version.
941 n = s->method->ssl_get_message(s,
942 SSL3_ST_CR_SRVR_HELLO_A,
943 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
949 if (SSL_IS_DTLS(s)) {
950 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
951 if (s->d1->send_cookie == 0) {
952 s->s3->tmp.reuse_message = 1;
954 } else { /* already sent a cookie */
956 al = SSL_AD_UNEXPECTED_MESSAGE;
957 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
963 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
964 al = SSL_AD_UNEXPECTED_MESSAGE;
965 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
969 return tls_process_server_hello(s, (unsigned long)n);
971 ssl3_send_alert(s, SSL3_AL_FATAL, al);
972 s->state = SSL_ST_ERR;
976 enum MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, unsigned long n)
978 STACK_OF(SSL_CIPHER) *sk;
980 PACKET pkt, session_id;
981 size_t session_id_len;
982 unsigned char *cipherchars;
983 int i, al = SSL_AD_INTERNAL_ERROR;
984 unsigned int compression;
985 #ifndef OPENSSL_NO_COMP
989 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
990 al = SSL_AD_INTERNAL_ERROR;
991 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
995 if (s->method->version == TLS_ANY_VERSION) {
996 unsigned int sversion;
998 if (!PACKET_get_net_2(&pkt, &sversion)) {
999 al = SSL_AD_DECODE_ERROR;
1000 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1004 #if TLS_MAX_VERSION != TLS1_2_VERSION
1005 #error Code needs updating for new TLS version
1007 #ifndef OPENSSL_NO_SSL3
1008 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1010 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1011 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1012 al = SSL_AD_PROTOCOL_VERSION;
1015 s->method = SSLv3_client_method();
1018 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1019 s->method = TLSv1_client_method();
1020 } else if ((sversion == TLS1_1_VERSION) &&
1021 !(s->options & SSL_OP_NO_TLSv1_1)) {
1022 s->method = TLSv1_1_client_method();
1023 } else if ((sversion == TLS1_2_VERSION) &&
1024 !(s->options & SSL_OP_NO_TLSv1_2)) {
1025 s->method = TLSv1_2_client_method();
1027 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1028 al = SSL_AD_PROTOCOL_VERSION;
1031 s->session->ssl_version = s->version = s->method->version;
1033 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1034 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1035 al = SSL_AD_PROTOCOL_VERSION;
1038 } else if (s->method->version == DTLS_ANY_VERSION) {
1039 /* Work out correct protocol version to use */
1040 unsigned int hversion;
1043 if (!PACKET_get_net_2(&pkt, &hversion)) {
1044 al = SSL_AD_DECODE_ERROR;
1045 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1049 options = s->options;
1050 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1051 s->method = DTLSv1_2_client_method();
1052 else if (tls1_suiteb(s)) {
1053 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1054 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1055 s->version = hversion;
1056 al = SSL_AD_PROTOCOL_VERSION;
1058 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1059 s->method = DTLSv1_client_method();
1061 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1062 s->version = hversion;
1063 al = SSL_AD_PROTOCOL_VERSION;
1066 s->session->ssl_version = s->version = s->method->version;
1068 unsigned char *vers;
1070 if (!PACKET_get_bytes(&pkt, &vers, 2)) {
1071 al = SSL_AD_DECODE_ERROR;
1072 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1075 if ((vers[0] != (s->version >> 8))
1076 || (vers[1] != (s->version & 0xff))) {
1077 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1078 s->version = (s->version & 0xff00) | vers[1];
1079 al = SSL_AD_PROTOCOL_VERSION;
1084 /* load the server hello data */
1085 /* load the server random */
1086 if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1087 al = SSL_AD_DECODE_ERROR;
1088 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1094 /* Get the session-id. */
1095 if (!PACKET_get_length_prefixed_1(&pkt, &session_id)) {
1096 al = SSL_AD_DECODE_ERROR;
1097 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1100 session_id_len = PACKET_remaining(&session_id);
1101 if (session_id_len > sizeof s->session->session_id
1102 || session_id_len > SSL3_SESSION_ID_SIZE) {
1103 al = SSL_AD_ILLEGAL_PARAMETER;
1104 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1108 if (!PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {
1109 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1110 al = SSL_AD_DECODE_ERROR;
1115 * Check if we can resume the session based on external pre-shared secret.
1116 * EAP-FAST (RFC 4851) supports two types of session resumption.
1117 * Resumption based on server-side state works with session IDs.
1118 * Resumption based on pre-shared Protected Access Credentials (PACs)
1119 * works by overriding the SessionTicket extension at the application
1120 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1121 * servers would honour the session ID.) Therefore, the session ID alone
1122 * is not a reliable indicator of session resumption, so we first check if
1123 * we can resume, and later peek at the next handshake message to see if the
1124 * server wants to resume.
1126 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1127 s->session->tlsext_tick) {
1128 SSL_CIPHER *pref_cipher = NULL;
1129 s->session->master_key_length = sizeof(s->session->master_key);
1130 if (s->tls_session_secret_cb(s, s->session->master_key,
1131 &s->session->master_key_length,
1133 s->tls_session_secret_cb_arg)) {
1134 s->session->cipher = pref_cipher ?
1135 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1137 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1138 al = SSL_AD_INTERNAL_ERROR;
1143 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1144 && memcmp(PACKET_data(&session_id), s->session->session_id,
1145 session_id_len) == 0) {
1146 if (s->sid_ctx_length != s->session->sid_ctx_length
1147 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1148 /* actually a client application bug */
1149 al = SSL_AD_ILLEGAL_PARAMETER;
1150 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1151 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1157 * If we were trying for session-id reuse but the server
1158 * didn't echo the ID, make a new SSL_SESSION.
1159 * In the case of EAP-FAST and PAC, we do not send a session ID,
1160 * so the PAC-based session secret is always preserved. It'll be
1161 * overwritten if the server refuses resumption.
1163 if (s->session->session_id_length > 0) {
1164 if (!ssl_get_new_session(s, 0)) {
1169 s->session->session_id_length = session_id_len;
1170 /* session_id_len could be 0 */
1171 memcpy(s->session->session_id, PACKET_data(&session_id),
1175 c = ssl_get_cipher_by_char(s, cipherchars);
1177 /* unknown cipher */
1178 al = SSL_AD_ILLEGAL_PARAMETER;
1179 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1182 /* Set version disabled mask now we know version */
1183 if (!SSL_USE_TLS1_2_CIPHERS(s))
1184 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1186 s->s3->tmp.mask_ssl = 0;
1188 * If it is a disabled cipher we didn't send it in client hello, so
1191 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1192 al = SSL_AD_ILLEGAL_PARAMETER;
1193 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1197 sk = ssl_get_ciphers_by_id(s);
1198 i = sk_SSL_CIPHER_find(sk, c);
1200 /* we did not say we would use this cipher */
1201 al = SSL_AD_ILLEGAL_PARAMETER;
1202 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1207 * Depending on the session caching (internal/external), the cipher
1208 * and/or cipher_id values may not be set. Make sure that cipher_id is
1209 * set and use it for comparison.
1211 if (s->session->cipher)
1212 s->session->cipher_id = s->session->cipher->id;
1213 if (s->hit && (s->session->cipher_id != c->id)) {
1214 al = SSL_AD_ILLEGAL_PARAMETER;
1215 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1216 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1219 s->s3->tmp.new_cipher = c;
1221 * Don't digest cached records if no sigalgs: we may need them for client
1224 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1226 /* lets get the compression algorithm */
1228 if (!PACKET_get_1(&pkt, &compression)) {
1229 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1230 al = SSL_AD_DECODE_ERROR;
1233 #ifdef OPENSSL_NO_COMP
1234 if (compression != 0) {
1235 al = SSL_AD_ILLEGAL_PARAMETER;
1236 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1237 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1241 * If compression is disabled we'd better not try to resume a session
1242 * using compression.
1244 if (s->session->compress_meth != 0) {
1245 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1249 if (s->hit && compression != s->session->compress_meth) {
1250 al = SSL_AD_ILLEGAL_PARAMETER;
1251 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1252 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1255 if (compression == 0)
1257 else if (!ssl_allow_compression(s)) {
1258 al = SSL_AD_ILLEGAL_PARAMETER;
1259 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1262 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1265 if (compression != 0 && comp == NULL) {
1266 al = SSL_AD_ILLEGAL_PARAMETER;
1267 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1268 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1271 s->s3->tmp.new_compression = comp;
1275 /* TLS extensions */
1276 if (!ssl_parse_serverhello_tlsext(s, &pkt)) {
1277 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1281 if (PACKET_remaining(&pkt) != 0) {
1282 /* wrong packet length */
1283 al = SSL_AD_DECODE_ERROR;
1284 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1288 return MSG_PROCESS_CONTINUE_READING;
1290 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1292 statem_set_error(s);
1293 return MSG_PROCESS_ERROR;
1296 int ssl3_get_server_certificate(SSL *s)
1301 n = s->method->ssl_get_message(s,
1304 -1, s->max_cert_list, &ok);
1309 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1310 s->s3->tmp.reuse_message = 1;
1314 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1315 al = SSL_AD_UNEXPECTED_MESSAGE;
1316 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1320 return tls_process_server_certificate(s, (unsigned long)n);
1322 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1323 s->state = SSL_ST_ERR;
1327 enum MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, unsigned long n)
1329 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1330 unsigned long cert_list_len, cert_len;
1332 unsigned char *certstart, *certbytes;
1333 STACK_OF(X509) *sk = NULL;
1334 EVP_PKEY *pkey = NULL;
1337 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1338 al = SSL_AD_INTERNAL_ERROR;
1339 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1343 if ((sk = sk_X509_new_null()) == NULL) {
1344 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1348 if (!PACKET_get_net_3(&pkt, &cert_list_len)
1349 || PACKET_remaining(&pkt) != cert_list_len) {
1350 al = SSL_AD_DECODE_ERROR;
1351 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1354 while (PACKET_remaining(&pkt)) {
1355 if (!PACKET_get_net_3(&pkt, &cert_len)
1356 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
1357 al = SSL_AD_DECODE_ERROR;
1358 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1359 SSL_R_CERT_LENGTH_MISMATCH);
1363 certstart = certbytes;
1364 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1366 al = SSL_AD_BAD_CERTIFICATE;
1367 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1370 if (certbytes != (certstart + cert_len)) {
1371 al = SSL_AD_DECODE_ERROR;
1372 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1373 SSL_R_CERT_LENGTH_MISMATCH);
1376 if (!sk_X509_push(sk, x)) {
1377 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1383 i = ssl_verify_cert_chain(s, sk);
1384 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1385 al = ssl_verify_alarm_type(s->verify_result);
1386 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1387 SSL_R_CERTIFICATE_VERIFY_FAILED);
1390 ERR_clear_error(); /* but we keep s->verify_result */
1392 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1393 al = SSL_AD_HANDSHAKE_FAILURE;
1397 s->session->peer_chain = sk;
1399 * Inconsistency alert: cert_chain does include the peer's certificate,
1400 * which we don't include in s3_srvr.c
1402 x = sk_X509_value(sk, 0);
1405 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1408 pkey = X509_get_pubkey(x);
1410 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1413 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1414 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1418 i = ssl_cert_type(x, pkey);
1422 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1423 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1427 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1428 if (exp_idx >= 0 && i != exp_idx) {
1430 al = SSL_AD_ILLEGAL_PARAMETER;
1431 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1432 SSL_R_WRONG_CERTIFICATE_TYPE);
1435 s->session->peer_type = i;
1437 X509_free(s->session->peer);
1439 s->session->peer = x;
1440 s->session->verify_result = s->verify_result;
1443 ret = MSG_PROCESS_CONTINUE_READING;
1447 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1449 statem_set_error(s);
1451 EVP_PKEY_free(pkey);
1453 sk_X509_pop_free(sk, X509_free);
1457 int ssl3_get_key_exchange(SSL *s)
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 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1487 s->s3->tmp.reuse_message = 1;
1491 return tls_process_key_exchange(s, (unsigned long)n);
1494 enum MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, unsigned long n)
1496 #ifndef OPENSSL_NO_RSA
1497 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1500 int al, j, verify_ret;
1502 EVP_PKEY *pkey = NULL;
1503 const EVP_MD *md = NULL;
1504 #ifndef OPENSSL_NO_RSA
1507 #ifndef OPENSSL_NO_DH
1510 #ifndef OPENSSL_NO_EC
1511 EC_KEY *ecdh = NULL;
1512 BN_CTX *bn_ctx = NULL;
1513 EC_POINT *srvr_ecpoint = NULL;
1516 PACKET pkt, save_param_start, signature;
1518 EVP_MD_CTX_init(&md_ctx);
1520 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1522 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1524 al = SSL_AD_INTERNAL_ERROR;
1527 save_param_start = pkt;
1529 #ifndef OPENSSL_NO_RSA
1530 RSA_free(s->s3->peer_rsa_tmp);
1531 s->s3->peer_rsa_tmp = NULL;
1533 #ifndef OPENSSL_NO_DH
1534 DH_free(s->s3->peer_dh_tmp);
1535 s->s3->peer_dh_tmp = NULL;
1537 #ifndef OPENSSL_NO_EC
1538 EC_KEY_free(s->s3->peer_ecdh_tmp);
1539 s->s3->peer_ecdh_tmp = NULL;
1542 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1544 al = SSL_AD_DECODE_ERROR;
1546 #ifndef OPENSSL_NO_PSK
1547 /* PSK ciphersuites are preceded by an identity hint */
1548 if (alg_k & SSL_PSK) {
1549 PACKET psk_identity_hint;
1550 if (!PACKET_get_length_prefixed_2(&pkt, &psk_identity_hint)) {
1551 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1556 * Store PSK identity hint for later use, hint is used in
1557 * ssl3_send_client_key_exchange. Assume that the maximum length of
1558 * a PSK identity hint can be as long as the maximum length of a PSK
1561 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1562 al = SSL_AD_HANDSHAKE_FAILURE;
1563 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1567 if (!PACKET_strndup(&psk_identity_hint,
1568 &s->session->psk_identity_hint)) {
1569 al = SSL_AD_INTERNAL_ERROR;
1574 /* Nothing else to do for plain PSK or RSAPSK */
1575 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1577 #endif /* !OPENSSL_NO_PSK */
1578 #ifndef OPENSSL_NO_SRP
1579 if (alg_k & SSL_kSRP) {
1580 PACKET prime, generator, salt, server_pub;
1581 if (!PACKET_get_length_prefixed_2(&pkt, &prime)
1582 || !PACKET_get_length_prefixed_2(&pkt, &generator)
1583 || !PACKET_get_length_prefixed_1(&pkt, &salt)
1584 || !PACKET_get_length_prefixed_2(&pkt, &server_pub)) {
1585 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1590 BN_bin2bn(PACKET_data(&prime),
1591 PACKET_remaining(&prime), NULL)) == NULL
1593 BN_bin2bn(PACKET_data(&generator),
1594 PACKET_remaining(&generator), NULL)) == NULL
1596 BN_bin2bn(PACKET_data(&salt),
1597 PACKET_remaining(&salt), NULL)) == NULL
1599 BN_bin2bn(PACKET_data(&server_pub),
1600 PACKET_remaining(&server_pub), NULL)) == NULL) {
1601 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1605 if (!srp_verify_server_param(s, &al)) {
1606 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1610 /* We must check if there is a certificate */
1611 if (alg_a & (SSL_aRSA|SSL_aDSS))
1612 pkey = X509_get_pubkey(s->session->peer);
1614 #endif /* !OPENSSL_NO_SRP */
1615 #ifndef OPENSSL_NO_RSA
1616 if (alg_k & SSL_kRSA) {
1618 /* Temporary RSA keys only allowed in export ciphersuites */
1619 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1620 al = SSL_AD_UNEXPECTED_MESSAGE;
1621 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1625 if (!PACKET_get_length_prefixed_2(&pkt, &mod)
1626 || !PACKET_get_length_prefixed_2(&pkt, &exp)) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1631 if ((rsa = RSA_new()) == NULL) {
1632 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1636 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1638 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1640 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1644 /* this should be because we are using an export cipher */
1645 if (alg_a & SSL_aRSA)
1646 pkey = X509_get_pubkey(s->session->peer);
1648 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1652 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1653 al = SSL_AD_UNEXPECTED_MESSAGE;
1654 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1658 s->s3->peer_rsa_tmp = rsa;
1661 #else /* OPENSSL_NO_RSA */
1664 #ifndef OPENSSL_NO_DH
1665 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1666 PACKET prime, generator, pub_key;
1668 if (!PACKET_get_length_prefixed_2(&pkt, &prime)
1669 || !PACKET_get_length_prefixed_2(&pkt, &generator)
1670 || !PACKET_get_length_prefixed_2(&pkt, &pub_key)) {
1671 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1675 if ((dh = DH_new()) == NULL) {
1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1680 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1681 PACKET_remaining(&prime), NULL)) == NULL
1682 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1683 PACKET_remaining(&generator), NULL)) == NULL
1685 BN_bin2bn(PACKET_data(&pub_key),
1686 PACKET_remaining(&pub_key), NULL)) == NULL) {
1687 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1691 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1696 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1697 al = SSL_AD_HANDSHAKE_FAILURE;
1698 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1701 if (alg_a & (SSL_aRSA|SSL_aDSS))
1702 pkey = X509_get_pubkey(s->session->peer);
1703 /* else anonymous DH, so no certificate or pkey. */
1705 s->s3->peer_dh_tmp = dh;
1708 #endif /* !OPENSSL_NO_DH */
1710 #ifndef OPENSSL_NO_EC
1711 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1713 const EC_GROUP *group;
1715 unsigned char *ecparams;
1717 if ((ecdh = EC_KEY_new()) == NULL) {
1718 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1723 * Extract elliptic curve parameters and the server's ephemeral ECDH
1724 * public key. For now we only support named (not generic) curves and
1725 * ECParameters in this case is just three bytes.
1727 if (!PACKET_get_bytes(&pkt, &ecparams, 3)) {
1728 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1732 * Check curve is one of our preferences, if not server has sent an
1733 * invalid curve. ECParameters is 3 bytes.
1735 if (!tls1_check_curve(s, ecparams, 3)) {
1736 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1740 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
1741 al = SSL_AD_INTERNAL_ERROR;
1742 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1743 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1747 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1748 if (ngroup == NULL) {
1749 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1752 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1753 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1756 EC_GROUP_free(ngroup);
1758 group = EC_KEY_get0_group(ecdh);
1760 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1761 (EC_GROUP_get_degree(group) > 163)) {
1762 al = SSL_AD_EXPORT_RESTRICTION;
1763 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1764 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1768 /* Next, get the encoded ECPoint */
1769 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1770 ((bn_ctx = BN_CTX_new()) == NULL)) {
1771 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1775 if (!PACKET_get_length_prefixed_1(&pkt, &encoded_pt)) {
1776 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1780 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1781 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
1782 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1787 * The ECC/TLS specification does not mention the use of DSA to sign
1788 * ECParameters in the server key exchange message. We do support RSA
1792 # ifndef OPENSSL_NO_RSA
1793 else if (alg_a & SSL_aRSA)
1794 pkey = X509_get_pubkey(s->session->peer);
1796 # ifndef OPENSSL_NO_EC
1797 else if (alg_a & SSL_aECDSA)
1798 pkey = X509_get_pubkey(s->session->peer);
1800 /* else anonymous ECDH, so no certificate or pkey. */
1801 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1802 s->s3->peer_ecdh_tmp = ecdh;
1804 BN_CTX_free(bn_ctx);
1806 EC_POINT_free(srvr_ecpoint);
1807 srvr_ecpoint = NULL;
1809 al = SSL_AD_UNEXPECTED_MESSAGE;
1810 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1813 #endif /* !OPENSSL_NO_EC */
1815 /* if it was signed, check the signature */
1819 * |pkt| now points to the beginning of the signature, so the difference
1820 * equals the length of the parameters.
1822 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1823 PACKET_remaining(&save_param_start) -
1824 PACKET_remaining(&pkt))) {
1825 al = SSL_AD_INTERNAL_ERROR;
1826 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1830 if (SSL_USE_SIGALGS(s)) {
1831 unsigned char *sigalgs;
1833 if (!PACKET_get_bytes(&pkt, &sigalgs, 2)) {
1834 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1837 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1844 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1850 if (!PACKET_get_length_prefixed_2(&pkt, &signature)
1851 || PACKET_remaining(&pkt) != 0) {
1852 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1855 j = EVP_PKEY_size(pkey);
1857 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1862 * Check signature length
1864 if (PACKET_remaining(&signature) > (size_t)j) {
1865 /* wrong packet length */
1866 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1869 #ifndef OPENSSL_NO_RSA
1870 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1876 for (num = 2; num > 0; num--) {
1877 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1878 EVP_DigestInit_ex(&md_ctx, (num == 2)
1879 ? s->ctx->md5 : s->ctx->sha1, NULL);
1880 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1882 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1884 EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms),
1885 PACKET_remaining(¶ms));
1886 EVP_DigestFinal_ex(&md_ctx, q, &size);
1891 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1892 PACKET_remaining(&signature), pkey->pkey.rsa);
1893 if (verify_ret < 0) {
1894 al = SSL_AD_DECRYPT_ERROR;
1895 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1898 if (verify_ret == 0) {
1900 al = SSL_AD_DECRYPT_ERROR;
1901 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1907 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1908 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1910 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1912 EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms),
1913 PACKET_remaining(¶ms));
1914 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1915 PACKET_remaining(&signature), pkey) <= 0) {
1917 al = SSL_AD_DECRYPT_ERROR;
1918 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1923 /* aNULL, aSRP or PSK do not need public keys */
1924 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1925 /* Might be wrong key type, check it */
1926 if (ssl3_check_cert_and_algorithm(s))
1927 /* Otherwise this shouldn't happen */
1928 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1931 /* still data left over */
1932 if (PACKET_remaining(&pkt) != 0) {
1933 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1937 EVP_PKEY_free(pkey);
1938 EVP_MD_CTX_cleanup(&md_ctx);
1939 return MSG_PROCESS_CONTINUE_READING;
1941 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1943 EVP_PKEY_free(pkey);
1944 #ifndef OPENSSL_NO_RSA
1947 #ifndef OPENSSL_NO_DH
1950 #ifndef OPENSSL_NO_EC
1951 BN_CTX_free(bn_ctx);
1952 EC_POINT_free(srvr_ecpoint);
1955 EVP_MD_CTX_cleanup(&md_ctx);
1956 statem_set_error(s);
1957 return MSG_PROCESS_ERROR;
1960 int ssl3_get_certificate_request(SSL *s)
1965 n = s->method->ssl_get_message(s,
1966 SSL3_ST_CR_CERT_REQ_A,
1967 SSL3_ST_CR_CERT_REQ_B,
1968 -1, s->max_cert_list, &ok);
1973 s->s3->tmp.cert_req = 0;
1975 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1976 s->s3->tmp.reuse_message = 1;
1978 * If we get here we don't need any cached handshake records as we
1979 * wont be doing client auth.
1981 if (!ssl3_digest_cached_records(s, 0))
1986 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1987 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1988 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
1992 /* TLS does not like anon-DH with client cert */
1993 if (s->version > SSL3_VERSION) {
1994 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1995 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1996 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1997 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2002 return tls_process_certificate_request(s, (unsigned long)n);
2004 s->state = SSL_ST_ERR;
2009 enum MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, unsigned long n)
2011 int ret = MSG_PROCESS_ERROR;
2012 unsigned int list_len, ctype_num, i, name_len;
2013 X509_NAME *xn = NULL;
2014 unsigned char *data;
2015 unsigned char *namestart, *namebytes;
2016 STACK_OF(X509_NAME) *ca_sk = NULL;
2019 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2020 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2021 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2025 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2026 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2030 /* get the certificate types */
2031 if (!PACKET_get_1(&pkt, &ctype_num)
2032 || !PACKET_get_bytes(&pkt, &data, ctype_num)) {
2033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2034 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2037 OPENSSL_free(s->cert->ctypes);
2038 s->cert->ctypes = NULL;
2039 if (ctype_num > SSL3_CT_NUMBER) {
2040 /* If we exceed static buffer copy all to cert structure */
2041 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2042 if (s->cert->ctypes == NULL) {
2043 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2046 memcpy(s->cert->ctypes, data, ctype_num);
2047 s->cert->ctype_num = (size_t)ctype_num;
2048 ctype_num = SSL3_CT_NUMBER;
2050 for (i = 0; i < ctype_num; i++)
2051 s->s3->tmp.ctype[i] = data[i];
2053 if (SSL_USE_SIGALGS(s)) {
2054 if (!PACKET_get_net_2(&pkt, &list_len)
2055 || !PACKET_get_bytes(&pkt, &data, list_len)) {
2056 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2057 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2058 SSL_R_LENGTH_MISMATCH);
2062 /* Clear certificate digests and validity flags */
2063 for (i = 0; i < SSL_PKEY_NUM; i++) {
2064 s->s3->tmp.md[i] = NULL;
2065 s->s3->tmp.valid_flags[i] = 0;
2067 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2068 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2069 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2070 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2073 if (!tls1_process_sigalgs(s)) {
2074 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2075 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2080 /* get the CA RDNs */
2081 if (!PACKET_get_net_2(&pkt, &list_len)
2082 || PACKET_remaining(&pkt) != list_len) {
2083 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2084 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2088 while (PACKET_remaining(&pkt)) {
2089 if (!PACKET_get_net_2(&pkt, &name_len)
2090 || !PACKET_get_bytes(&pkt, &namebytes, name_len)) {
2091 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2092 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2093 SSL_R_LENGTH_MISMATCH);
2097 namestart = namebytes;
2099 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2100 name_len)) == NULL) {
2101 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2102 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2106 if (namebytes != (namestart + name_len)) {
2107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2108 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2109 SSL_R_CA_DN_LENGTH_MISMATCH);
2112 if (!sk_X509_NAME_push(ca_sk, xn)) {
2113 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2118 /* we should setup a certificate to return.... */
2119 s->s3->tmp.cert_req = 1;
2120 s->s3->tmp.ctype_num = ctype_num;
2121 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2122 s->s3->tmp.ca_names = ca_sk;
2125 ret = MSG_PROCESS_CONTINUE_READING;
2128 statem_set_error(s);
2130 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2134 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2136 return (X509_NAME_cmp(*a, *b));
2139 int ssl3_get_new_session_ticket(SSL *s)
2144 n = s->method->ssl_get_message(s,
2145 SSL3_ST_CR_SESSION_TICKET_A,
2146 SSL3_ST_CR_SESSION_TICKET_B,
2147 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2152 return tls_process_new_session_ticket(s, (unsigned long)n);
2155 enum MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, unsigned long n)
2158 unsigned int ticklen;
2159 unsigned long ticket_lifetime_hint;
2162 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2163 al = SSL_AD_INTERNAL_ERROR;
2164 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
2168 if (!PACKET_get_net_4(&pkt, &ticket_lifetime_hint)
2169 || !PACKET_get_net_2(&pkt, &ticklen)
2170 || PACKET_remaining(&pkt) != ticklen) {
2171 al = SSL_AD_DECODE_ERROR;
2172 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2176 /* Server is allowed to change its mind and send an empty ticket. */
2180 if (s->session->session_id_length > 0) {
2181 int i = s->session_ctx->session_cache_mode;
2182 SSL_SESSION *new_sess;
2184 * We reused an existing session, so we need to replace it with a new
2187 if (i & SSL_SESS_CACHE_CLIENT) {
2189 * Remove the old session from the cache
2191 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2192 if (s->session_ctx->remove_session_cb != NULL)
2193 s->session_ctx->remove_session_cb(s->session_ctx,
2196 /* We carry on if this fails */
2197 SSL_CTX_remove_session(s->session_ctx, s->session);
2201 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2202 al = SSL_AD_INTERNAL_ERROR;
2203 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2207 SSL_SESSION_free(s->session);
2208 s->session = new_sess;
2211 OPENSSL_free(s->session->tlsext_tick);
2212 s->session->tlsext_ticklen = 0;
2214 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2215 if (!s->session->tlsext_tick) {
2216 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2219 if (!PACKET_copy_bytes(&pkt, s->session->tlsext_tick, ticklen)) {
2220 al = SSL_AD_DECODE_ERROR;
2221 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2225 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2226 s->session->tlsext_ticklen = ticklen;
2228 * There are two ways to detect a resumed ticket session. One is to set
2229 * an appropriate session ID and then the server must return a match in
2230 * ServerHello. This allows the normal client session ID matching to work
2231 * and we know much earlier that the ticket has been accepted. The
2232 * other way is to set zero length session ID when the ticket is
2233 * presented and rely on the handshake to determine session resumption.
2234 * We choose the former approach because this fits in with assumptions
2235 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2236 * SHA256 is disabled) hash of the ticket.
2238 EVP_Digest(s->session->tlsext_tick, ticklen,
2239 s->session->session_id, &s->session->session_id_length,
2240 EVP_sha256(), NULL);
2241 return MSG_PROCESS_CONTINUE_READING;
2243 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2245 statem_set_error(s);
2246 return MSG_PROCESS_ERROR;
2249 int ssl3_get_cert_status(SSL *s)
2254 n = s->method->ssl_get_message(s,
2255 SSL3_ST_CR_CERT_STATUS_A,
2256 SSL3_ST_CR_CERT_STATUS_B,
2257 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2262 return tls_process_cert_status(s, (unsigned long)n);
2265 enum MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, unsigned long n)
2268 unsigned long resplen;
2272 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2273 al = SSL_AD_INTERNAL_ERROR;
2274 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_INTERNAL_ERROR);
2277 if (!PACKET_get_1(&pkt, &type)
2278 || type != TLSEXT_STATUSTYPE_ocsp) {
2279 al = SSL_AD_DECODE_ERROR;
2280 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2283 if (!PACKET_get_net_3(&pkt, &resplen)
2284 || PACKET_remaining(&pkt) != resplen) {
2285 al = SSL_AD_DECODE_ERROR;
2286 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2289 OPENSSL_free(s->tlsext_ocsp_resp);
2290 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2291 if (!s->tlsext_ocsp_resp) {
2292 al = SSL_AD_INTERNAL_ERROR;
2293 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2296 if (!PACKET_copy_bytes(&pkt, s->tlsext_ocsp_resp, resplen)) {
2297 al = SSL_AD_DECODE_ERROR;
2298 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2301 s->tlsext_ocsp_resplen = resplen;
2302 if (s->ctx->tlsext_status_cb) {
2304 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2306 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2307 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2311 al = SSL_AD_INTERNAL_ERROR;
2312 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2316 return MSG_PROCESS_CONTINUE_READING;
2318 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2319 statem_set_error(s);
2320 return MSG_PROCESS_ERROR;
2323 int ssl3_get_server_done(SSL *s)
2328 /* Second to last param should be very small, like 0 :-) */
2329 n = s->method->ssl_get_message(s,
2330 SSL3_ST_CR_SRVR_DONE_A,
2331 SSL3_ST_CR_SRVR_DONE_B,
2332 SSL3_MT_SERVER_DONE, 30, &ok);
2337 return tls_process_server_done(s, (unsigned long)n);
2340 enum MSG_PROCESS_RETURN tls_process_server_done(SSL *s, unsigned long n)
2343 /* should contain no data */
2344 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2345 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2346 statem_set_error(s);
2347 return MSG_PROCESS_ERROR;
2350 #ifndef OPENSSL_NO_SRP
2351 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2352 if (SRP_Calc_A_param(s) <= 0) {
2353 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2354 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2355 statem_set_error(s);
2356 return MSG_PROCESS_ERROR;
2361 return MSG_PROCESS_FINISHED_READING;
2364 int ssl3_send_client_key_exchange(SSL *s)
2368 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2369 if(tls_construct_client_key_exchange(s) == 0)
2372 s->state = SSL3_ST_CW_KEY_EXCH_B;
2375 /* SSL3_ST_CW_KEY_EXCH_B */
2376 n = ssl_do_write(s);
2379 if (tls_client_key_exchange_post_work(s) == 0)
2385 s->state = SSL_ST_ERR;
2390 int tls_construct_client_key_exchange(SSL *s)
2394 #ifndef OPENSSL_NO_PSK
2395 size_t pskhdrlen = 0;
2397 unsigned long alg_k;
2398 #ifndef OPENSSL_NO_RSA
2400 EVP_PKEY *pkey = NULL;
2402 #ifndef OPENSSL_NO_EC
2403 EC_KEY *clnt_ecdh = NULL;
2404 const EC_POINT *srvr_ecpoint = NULL;
2405 EVP_PKEY *srvr_pub_pkey = NULL;
2406 unsigned char *encodedPoint = NULL;
2407 int encoded_pt_len = 0;
2408 BN_CTX *bn_ctx = NULL;
2410 unsigned char *pms = NULL;
2412 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2414 p = ssl_handshake_start(s);
2417 #ifndef OPENSSL_NO_PSK
2418 if (alg_k & SSL_PSK) {
2421 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2422 * \0-terminated identity. The last byte is for us for simulating
2425 char identity[PSK_MAX_IDENTITY_LEN + 1];
2427 unsigned char psk[PSK_MAX_PSK_LEN];
2430 if (s->psk_client_callback == NULL) {
2431 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2432 SSL_R_PSK_NO_CLIENT_CB);
2436 memset(identity, 0, sizeof(identity));
2438 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2439 identity, sizeof(identity) - 1,
2442 if (psklen > PSK_MAX_PSK_LEN) {
2443 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2444 ERR_R_INTERNAL_ERROR);
2446 } else if (psklen == 0) {
2447 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2448 SSL_R_PSK_IDENTITY_NOT_FOUND);
2452 OPENSSL_free(s->s3->tmp.psk);
2453 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2454 OPENSSL_cleanse(psk, psklen);
2456 if (s->s3->tmp.psk == NULL) {
2457 OPENSSL_cleanse(identity, sizeof(identity));
2461 s->s3->tmp.psklen = psklen;
2463 identitylen = strlen(identity);
2464 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2465 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2466 ERR_R_INTERNAL_ERROR);
2469 OPENSSL_free(s->session->psk_identity);
2470 s->session->psk_identity = BUF_strdup(identity);
2471 if (s->session->psk_identity == NULL) {
2472 OPENSSL_cleanse(identity, sizeof(identity));
2476 s2n(identitylen, p);
2477 memcpy(p, identity, identitylen);
2478 pskhdrlen = 2 + identitylen;
2482 OPENSSL_cleanse(identity, sizeof(identity));
2484 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2488 if (alg_k & SSL_kPSK) {
2493 /* Fool emacs indentation */
2496 #ifndef OPENSSL_NO_RSA
2497 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2499 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2500 pms = OPENSSL_malloc(pmslen);
2504 if (s->session->peer == NULL) {
2506 * We should always have a server certificate with SSL_kRSA.
2508 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2509 ERR_R_INTERNAL_ERROR);
2513 if (s->s3->peer_rsa_tmp != NULL)
2514 rsa = s->s3->peer_rsa_tmp;
2516 pkey = X509_get_pubkey(s->session->peer);
2517 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2518 || (pkey->pkey.rsa == NULL)) {
2519 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2520 ERR_R_INTERNAL_ERROR);
2521 EVP_PKEY_free(pkey);
2524 rsa = pkey->pkey.rsa;
2525 EVP_PKEY_free(pkey);
2528 pms[0] = s->client_version >> 8;
2529 pms[1] = s->client_version & 0xff;
2530 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2534 /* Fix buf for TLS and beyond */
2535 if (s->version > SSL3_VERSION)
2537 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2539 if (s->options & SSL_OP_PKCS1_CHECK_1)
2541 if (s->options & SSL_OP_PKCS1_CHECK_2)
2545 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2546 SSL_R_BAD_RSA_ENCRYPT);
2550 /* Fix buf for TLS and beyond */
2551 if (s->version > SSL3_VERSION) {
2557 #ifndef OPENSSL_NO_DH
2558 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2559 DH *dh_srvr, *dh_clnt;
2560 if (s->s3->peer_dh_tmp != NULL)
2561 dh_srvr = s->s3->peer_dh_tmp;
2563 /* we get them from the cert */
2564 EVP_PKEY *spkey = NULL;
2566 spkey = X509_get_pubkey(s->session->peer);
2568 dh_srvr = EVP_PKEY_get1_DH(spkey);
2569 EVP_PKEY_free(spkey);
2571 if (dh_srvr == NULL) {
2572 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2573 ERR_R_INTERNAL_ERROR);
2577 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2578 /* Use client certificate key */
2579 EVP_PKEY *clkey = s->cert->key->privatekey;
2582 dh_clnt = EVP_PKEY_get1_DH(clkey);
2583 if (dh_clnt == NULL) {
2584 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2585 ERR_R_INTERNAL_ERROR);
2589 /* generate a new random key */
2590 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2591 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2594 if (!DH_generate_key(dh_clnt)) {
2595 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2601 pmslen = DH_size(dh_clnt);
2602 pms = OPENSSL_malloc(pmslen);
2607 * use the 'p' output buffer for the DH key, but make sure to
2608 * clear it out afterwards
2611 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2612 if (s->s3->peer_dh_tmp == NULL)
2616 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2622 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2625 /* send off the data */
2626 n = BN_num_bytes(dh_clnt->pub_key);
2628 BN_bn2bin(dh_clnt->pub_key, p);
2636 #ifndef OPENSSL_NO_EC
2637 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2638 const EC_GROUP *srvr_group = NULL;
2640 int ecdh_clnt_cert = 0;
2643 * Did we send out the client's ECDH share for use in premaster
2644 * computation as part of client certificate? If so, set
2645 * ecdh_clnt_cert to 1.
2647 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2649 * XXX: For now, we do not support client
2650 * authentication using ECDH certificates.
2651 * To add such support, one needs to add
2652 * code that checks for appropriate
2653 * conditions and sets ecdh_clnt_cert to 1.
2654 * For example, the cert have an ECC
2655 * key on the same curve as the server's
2656 * and the key should be authorized for
2659 * One also needs to add code in ssl3_connect
2660 * to skip sending the certificate verify
2663 * if ((s->cert->key->privatekey != NULL) &&
2664 * (s->cert->key->privatekey->type ==
2665 * EVP_PKEY_EC) && ...)
2666 * ecdh_clnt_cert = 1;
2670 if (s->s3->peer_ecdh_tmp != NULL) {
2671 tkey = s->s3->peer_ecdh_tmp;
2673 /* Get the Server Public Key from Cert */
2674 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2675 if ((srvr_pub_pkey == NULL)
2676 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2677 || (srvr_pub_pkey->pkey.ec == NULL)) {
2678 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2679 ERR_R_INTERNAL_ERROR);
2683 tkey = srvr_pub_pkey->pkey.ec;
2686 srvr_group = EC_KEY_get0_group(tkey);
2687 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2689 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2690 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2691 ERR_R_INTERNAL_ERROR);
2695 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2696 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2697 ERR_R_MALLOC_FAILURE);
2701 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2702 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2705 if (ecdh_clnt_cert) {
2707 * Reuse key info from our certificate We only need our
2708 * private key to perform the ECDH computation.
2710 const BIGNUM *priv_key;
2711 tkey = s->cert->key->privatekey->pkey.ec;
2712 priv_key = EC_KEY_get0_private_key(tkey);
2713 if (priv_key == NULL) {
2714 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2715 ERR_R_MALLOC_FAILURE);
2718 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2719 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2723 /* Generate a new ECDH key pair */
2724 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2725 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2732 * use the 'p' output buffer for the ECDH key, but make sure to
2733 * clear it out afterwards
2736 field_size = EC_GROUP_get_degree(srvr_group);
2737 if (field_size <= 0) {
2738 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2741 pmslen = (field_size + 7) / 8;
2742 pms = OPENSSL_malloc(pmslen);
2745 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2746 if (n <= 0 || pmslen != (size_t)n) {
2747 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2751 if (ecdh_clnt_cert) {
2752 /* Send empty client key exch message */
2756 * First check the size of encoding and allocate memory
2760 EC_POINT_point2oct(srvr_group,
2761 EC_KEY_get0_public_key(clnt_ecdh),
2762 POINT_CONVERSION_UNCOMPRESSED,
2765 encodedPoint = (unsigned char *)
2766 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2767 bn_ctx = BN_CTX_new();
2768 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2769 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2770 ERR_R_MALLOC_FAILURE);
2774 /* Encode the public key */
2775 n = EC_POINT_point2oct(srvr_group,
2776 EC_KEY_get0_public_key(clnt_ecdh),
2777 POINT_CONVERSION_UNCOMPRESSED,
2778 encodedPoint, encoded_pt_len, bn_ctx);
2780 *p = n; /* length of encoded point */
2781 /* Encoded point will be copied here */
2783 /* copy the point */
2784 memcpy(p, encodedPoint, n);
2785 /* increment n to account for length field */
2789 /* Free allocated memory */
2790 BN_CTX_free(bn_ctx);
2791 OPENSSL_free(encodedPoint);
2792 EC_KEY_free(clnt_ecdh);
2793 EVP_PKEY_free(srvr_pub_pkey);
2795 #endif /* !OPENSSL_NO_EC */
2796 else if (alg_k & SSL_kGOST) {
2797 /* GOST key exchange message creation */
2798 EVP_PKEY_CTX *pkey_ctx;
2801 unsigned int md_len;
2802 unsigned char shared_ukm[32], tmp[256];
2803 EVP_MD_CTX *ukm_hash;
2807 pms = OPENSSL_malloc(pmslen);
2812 * Get server sertificate PKEY and create ctx from it
2814 peer_cert = s->session->peer;
2816 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2817 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2821 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2822 X509_get_pubkey(peer_cert), NULL);
2824 * If we have send a certificate, and certificate key
2826 * * parameters match those of server certificate, use
2827 * certificate key for key exchange
2830 /* Otherwise, generate ephemeral key pair */
2832 EVP_PKEY_encrypt_init(pkey_ctx);
2833 /* Generate session key */
2834 if (RAND_bytes(pms, pmslen) <= 0) {
2835 EVP_PKEY_CTX_free(pkey_ctx);
2836 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2837 ERR_R_INTERNAL_ERROR);
2841 * If we have client certificate, use its secret as peer key
2843 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2844 if (EVP_PKEY_derive_set_peer
2845 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2847 * If there was an error - just ignore it. Ephemeral key
2854 * Compute shared IV and store it in algorithm-specific context
2857 ukm_hash = EVP_MD_CTX_create();
2858 EVP_DigestInit(ukm_hash,
2859 EVP_get_digestbynid(NID_id_GostR3411_94));
2860 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2862 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2864 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2865 EVP_MD_CTX_destroy(ukm_hash);
2866 if (EVP_PKEY_CTX_ctrl
2867 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2869 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2873 /* Make GOST keytransport blob message */
2875 * Encapsulate it into sequence
2877 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2879 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2880 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2884 if (msglen >= 0x80) {
2886 *(p++) = msglen & 0xff;
2889 *(p++) = msglen & 0xff;
2892 memcpy(p, tmp, msglen);
2893 /* Check if pubkey from client certificate was used */
2894 if (EVP_PKEY_CTX_ctrl
2895 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2896 /* Set flag "skip certificate verify" */
2897 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2899 EVP_PKEY_CTX_free(pkey_ctx);
2900 EVP_PKEY_free(pub_key);
2903 #ifndef OPENSSL_NO_SRP
2904 else if (alg_k & SSL_kSRP) {
2905 if (s->srp_ctx.A != NULL) {
2906 /* send off the data */
2907 n = BN_num_bytes(s->srp_ctx.A);
2909 BN_bn2bin(s->srp_ctx.A, p);
2912 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2913 ERR_R_INTERNAL_ERROR);
2916 OPENSSL_free(s->session->srp_username);
2917 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2918 if (s->session->srp_username == NULL) {
2919 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2920 ERR_R_MALLOC_FAILURE);
2926 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2927 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2931 #ifndef OPENSSL_NO_PSK
2935 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2936 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2937 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2941 s->s3->tmp.pms = pms;
2942 s->s3->tmp.pmslen = pmslen;
2946 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2947 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2949 OPENSSL_clear_free(pms, pmslen);
2950 s->s3->tmp.pms = NULL;
2951 #ifndef OPENSSL_NO_EC
2952 BN_CTX_free(bn_ctx);
2953 OPENSSL_free(encodedPoint);
2954 EC_KEY_free(clnt_ecdh);
2955 EVP_PKEY_free(srvr_pub_pkey);
2957 #ifndef OPENSSL_NO_PSK
2958 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2959 s->s3->tmp.psk = NULL;
2961 statem_set_error(s);
2965 int tls_client_key_exchange_post_work(SSL *s)
2967 unsigned char *pms = NULL;
2970 #ifndef OPENSSL_NO_SRP
2972 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2973 if (!srp_generate_client_master_secret(s)) {
2974 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2975 ERR_R_INTERNAL_ERROR);
2981 pms = s->s3->tmp.pms;
2982 pmslen = s->s3->tmp.pmslen;
2984 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2985 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2986 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2989 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2990 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2991 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2996 OPENSSL_clear_free(pms, pmslen);
2997 s->s3->tmp.pms = NULL;
3001 int ssl3_send_client_verify(SSL *s)
3003 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3004 if(tls_construct_client_verify(s) == 0)
3007 s->state = SSL3_ST_CW_CERT_VRFY_B;
3010 /* SSL3_ST_CW_CERT_VRFY_B */
3011 return ssl_do_write(s);
3013 s->state = SSL_ST_ERR;
3018 int tls_construct_client_verify(SSL *s)
3021 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3023 EVP_PKEY_CTX *pctx = NULL;
3029 EVP_MD_CTX_init(&mctx);
3031 p = ssl_handshake_start(s);
3032 pkey = s->cert->key->privatekey;
3033 /* Create context from key and test if sha1 is allowed as digest */
3034 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3035 EVP_PKEY_sign_init(pctx);
3036 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3037 if (!SSL_USE_SIGALGS(s))
3038 s->method->ssl3_enc->cert_verify_mac(s,
3041 [MD5_DIGEST_LENGTH]));
3046 * For TLS v1.2 send signature algorithm and signature using agreed
3047 * digest and cached handshake records.
3049 if (SSL_USE_SIGALGS(s)) {
3052 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3053 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3054 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3055 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3060 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3063 if (!EVP_SignInit_ex(&mctx, md, NULL)
3064 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3065 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3066 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
3071 /* Digest cached records and discard handshake buffer */
3072 if (!ssl3_digest_cached_records(s, 0))
3075 #ifndef OPENSSL_NO_RSA
3076 if (pkey->type == EVP_PKEY_RSA) {
3077 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3078 if (RSA_sign(NID_md5_sha1, data,
3079 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3080 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3081 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
3088 #ifndef OPENSSL_NO_DSA
3089 if (pkey->type == EVP_PKEY_DSA) {
3090 if (!DSA_sign(pkey->save_type,
3091 &(data[MD5_DIGEST_LENGTH]),
3092 SHA_DIGEST_LENGTH, &(p[2]),
3093 (unsigned int *)&j, pkey->pkey.dsa)) {
3094 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
3101 #ifndef OPENSSL_NO_EC
3102 if (pkey->type == EVP_PKEY_EC) {
3103 if (!ECDSA_sign(pkey->save_type,
3104 &(data[MD5_DIGEST_LENGTH]),
3105 SHA_DIGEST_LENGTH, &(p[2]),
3106 (unsigned int *)&j, pkey->pkey.ec)) {
3107 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3114 if (pkey->type == NID_id_GostR3410_2001) {
3115 unsigned char signbuf[64];
3117 size_t sigsize = 64;
3118 s->method->ssl3_enc->cert_verify_mac(s,
3119 NID_id_GostR3411_94, data);
3120 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3121 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3124 for (i = 63, j = 0; i >= 0; j++, i--) {
3125 p[2 + j] = signbuf[i];
3130 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3133 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3134 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3138 EVP_MD_CTX_cleanup(&mctx);
3139 EVP_PKEY_CTX_free(pctx);
3142 EVP_MD_CTX_cleanup(&mctx);
3143 EVP_PKEY_CTX_free(pctx);
3148 * Check a certificate can be used for client authentication. Currently check
3149 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3150 * certificates can be used and optionally checks suitability for Suite B.
3152 static int ssl3_check_client_certificate(SSL *s)
3154 unsigned long alg_k;
3155 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3157 /* If no suitable signature algorithm can't use certificate */
3158 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3161 * If strict mode check suitability of chain before using it. This also
3162 * adjusts suite B digest if necessary.
3164 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3165 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3167 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3168 /* See if we can use client certificate for fixed DH */
3169 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3170 int i = s->session->peer_type;
3171 EVP_PKEY *clkey = NULL, *spkey = NULL;
3172 clkey = s->cert->key->privatekey;
3173 /* If client key not DH assume it can be used */
3174 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3177 spkey = X509_get_pubkey(s->session->peer);
3179 /* Compare server and client parameters */
3180 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3181 EVP_PKEY_free(spkey);
3185 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3191 int ssl3_send_client_certificate(SSL *s)
3193 enum WORK_STATE wst;
3195 if (s->state == SSL3_ST_CW_CERT_A || s->state == SSL3_ST_CW_CERT_B) {
3196 if (s->state == SSL3_ST_CW_CERT_A)
3197 wst = tls_prepare_client_certificate(s, WORK_MORE_A);
3199 wst = tls_prepare_client_certificate(s, WORK_MORE_B);
3200 if (wst == WORK_ERROR)
3202 if (wst == WORK_MORE_A)
3204 if (wst == WORK_MORE_B) {
3205 s->state = SSL3_ST_CW_CERT_B;
3209 s->state = SSL3_ST_CW_CERT_C;
3212 if (s->state == SSL3_ST_CW_CERT_B) {
3213 wst = tls_prepare_client_certificate(s, WORK_MORE_B);
3214 if (wst == WORK_ERROR)
3216 if (wst == WORK_MORE_A)
3219 /* Skip state C...it was entirely temporary in the original code */
3220 s->state = SSL3_ST_CW_CERT_D;
3223 if (tls_construct_client_certificate(s) == 0)
3226 return ssl_do_write(s);
3228 s->state = SSL_ST_ERR;
3233 enum WORK_STATE tls_prepare_client_certificate(SSL *s, enum WORK_STATE wst)
3236 EVP_PKEY *pkey = NULL;
3239 if (wst == WORK_MORE_A) {
3240 /* Let cert callback update client certificates if required */
3241 if (s->cert->cert_cb) {
3242 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3244 s->rwstate = SSL_X509_LOOKUP;
3248 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3249 statem_set_error(s);
3252 s->rwstate = SSL_NOTHING;
3254 if (ssl3_check_client_certificate(s))
3255 return WORK_FINISHED_CONTINUE;
3257 /* Fall through to WORK_MORE_B */
3261 /* We need to get a client cert */
3262 if (wst == WORK_MORE_B) {
3264 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3265 * return(-1); We then get retied later
3267 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3269 s->rwstate = SSL_X509_LOOKUP;
3272 s->rwstate = SSL_NOTHING;
3273 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3274 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3276 } else if (i == 1) {
3278 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
3279 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3283 EVP_PKEY_free(pkey);
3284 if (i && !ssl3_check_client_certificate(s))
3287 if (s->version == SSL3_VERSION) {
3288 s->s3->tmp.cert_req = 0;
3289 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3290 return WORK_FINISHED_CONTINUE;
3292 s->s3->tmp.cert_req = 2;
3293 if (!ssl3_digest_cached_records(s, 0)) {
3294 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3295 statem_set_error(s);
3301 return WORK_FINISHED_CONTINUE;
3304 /* Shouldn't ever get here */
3308 int tls_construct_client_certificate(SSL *s)
3310 if (!ssl3_output_cert_chain(s,
3311 (s->s3->tmp.cert_req ==
3312 2) ? NULL : s->cert->key)) {
3313 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3314 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3315 statem_set_error(s);
3322 #define has_bits(i,m) (((i)&(m)) == (m))
3324 int ssl3_check_cert_and_algorithm(SSL *s)
3328 EVP_PKEY *pkey = NULL;
3330 #ifndef OPENSSL_NO_RSA
3333 #ifndef OPENSSL_NO_DH
3336 int al = SSL_AD_HANDSHAKE_FAILURE;
3338 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3339 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3341 /* we don't have a certificate */
3342 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3344 #ifndef OPENSSL_NO_RSA
3345 rsa = s->s3->peer_rsa_tmp;
3347 #ifndef OPENSSL_NO_DH
3348 dh = s->s3->peer_dh_tmp;
3351 /* This is the passed certificate */
3353 idx = s->session->peer_type;
3354 #ifndef OPENSSL_NO_EC
3355 if (idx == SSL_PKEY_ECC) {
3356 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3358 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3363 } else if (alg_a & SSL_aECDSA) {
3364 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3365 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3367 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3368 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3372 pkey = X509_get_pubkey(s->session->peer);
3373 pkey_bits = EVP_PKEY_bits(pkey);
3374 i = X509_certificate_type(s->session->peer, pkey);
3375 EVP_PKEY_free(pkey);
3377 /* Check that we have a certificate if we require one */
3378 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3379 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3380 SSL_R_MISSING_RSA_SIGNING_CERT);
3383 #ifndef OPENSSL_NO_DSA
3384 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3385 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3386 SSL_R_MISSING_DSA_SIGNING_CERT);
3390 #ifndef OPENSSL_NO_RSA
3391 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3392 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3393 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3394 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3395 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3397 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3398 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3399 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3400 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3401 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3405 /* server key exchange is not allowed. */
3406 al = SSL_AD_INTERNAL_ERROR;
3407 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3414 #ifndef OPENSSL_NO_DH
3415 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3416 al = SSL_AD_INTERNAL_ERROR;
3417 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3419 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3420 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3421 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3422 SSL_R_MISSING_DH_RSA_CERT);
3425 # ifndef OPENSSL_NO_DSA
3426 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3427 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3428 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3429 SSL_R_MISSING_DH_DSA_CERT);
3435 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3436 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3437 #ifndef OPENSSL_NO_RSA
3438 if (alg_k & SSL_kRSA) {
3440 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3441 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3443 } else if (RSA_bits(rsa) >
3444 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3445 /* We have a temporary RSA key but it's too large. */
3446 al = SSL_AD_EXPORT_RESTRICTION;
3447 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3448 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3453 #ifndef OPENSSL_NO_DH
3454 if (alg_k & SSL_kDHE) {
3456 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3457 /* We have a temporary DH key but it's too large. */
3458 al = SSL_AD_EXPORT_RESTRICTION;
3459 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3460 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3463 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3464 /* The cert should have had an export DH key. */
3465 al = SSL_AD_EXPORT_RESTRICTION;
3466 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3467 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3472 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3473 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3479 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3484 * Normally, we can tell if the server is resuming the session from
3485 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3486 * message after the ServerHello to determine if the server is resuming.
3487 * Therefore, we allow EAP-FAST to peek ahead.
3488 * ssl3_check_change returns 1 if we are resuming from an external
3489 * pre-shared secret, we have a "ticket" and the next server message
3490 * is CCS; and 0 otherwise. It returns -1 upon an error.
3492 static int ssl3_check_change(SSL *s)
3496 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3497 !s->session->tlsext_tick)
3501 * This function is called when we might get a Certificate message instead,
3502 * so permit appropriate message length.
3503 * We ignore the return value as we're only interested in the message type
3504 * and not its length.
3506 s->method->ssl_get_message(s,
3509 -1, s->max_cert_list, &ok);
3514 s->s3->tmp.reuse_message = 1;
3516 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC)
3522 #ifndef OPENSSL_NO_NEXTPROTONEG
3523 int ssl3_send_next_proto(SSL *s)
3525 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3526 if (tls_construct_next_proto(s) == 0) {
3527 s->state = SSL_ST_ERR;
3530 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3533 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3536 int tls_construct_next_proto(SSL *s)
3538 unsigned int len, padding_len;
3541 len = s->next_proto_negotiated_len;
3542 padding_len = 32 - ((len + 2) % 32);
3543 d = (unsigned char *)s->init_buf->data;
3545 memcpy(d + 5, s->next_proto_negotiated, len);
3546 d[5 + len] = padding_len;
3547 memset(d + 6 + len, 0, padding_len);
3548 *(d++) = SSL3_MT_NEXT_PROTO;
3549 l2n3(2 + len + padding_len, d);
3550 s->init_num = 4 + 2 + len + padding_len;
3557 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3560 #ifndef OPENSSL_NO_ENGINE
3561 if (s->ctx->client_cert_engine) {
3562 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3563 SSL_get_client_CA_list(s),
3564 px509, ppkey, NULL, NULL, NULL);
3569 if (s->ctx->client_cert_cb)
3570 i = s->ctx->client_cert_cb(s, px509, ppkey);
3574 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3580 int empty_reneg_info_scsv = !s->renegotiate;
3581 /* Set disabled masks for this session */
3582 ssl_set_client_disabled(s);
3588 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3589 c = sk_SSL_CIPHER_value(sk, i);
3590 /* Skip disabled ciphers */
3591 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3593 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3594 if (c->id == SSL3_CK_SCSV) {
3595 if (!empty_reneg_info_scsv)
3598 empty_reneg_info_scsv = 0;
3601 j = s->method->put_cipher_by_char(c, p);
3605 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3609 if (empty_reneg_info_scsv) {
3610 static SSL_CIPHER scsv = {
3611 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3613 j = s->method->put_cipher_by_char(&scsv, p);
3615 #ifdef OPENSSL_RI_DEBUG
3617 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3620 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3621 static SSL_CIPHER scsv = {
3622 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3624 j = s->method->put_cipher_by_char(&scsv, p);