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);
170 * Temporarily disabled during development of new state machine code.
173 static int ssl3_check_change(SSL *s);
175 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
181 * Temporarily disabled during development of new state machine code.
184 int ssl3_connect(SSL *s)
187 unsigned long Time = (unsigned long)time(NULL);
188 void (*cb) (const SSL *ssl, int type, int val) = NULL;
190 int new_state, state, skip = 0;
192 RAND_add(&Time, sizeof(Time), 0);
196 if (s->info_callback != NULL)
197 cb = s->info_callback;
198 else if (s->ctx->info_callback != NULL)
199 cb = s->ctx->info_callback;
202 if (!SSL_in_init(s) || SSL_in_before(s)) {
207 #ifndef OPENSSL_NO_HEARTBEATS
209 * If we're awaiting a HeartbeatResponse, pretend we already got and
210 * don't await it anymore, because Heartbeats don't make sense during
213 if (s->tlsext_hb_pending) {
214 s->tlsext_hb_pending = 0;
223 case SSL_ST_RENEGOTIATE:
225 s->state = SSL_ST_CONNECT;
226 s->ctx->stats.sess_connect_renegotiate++;
230 case SSL_ST_BEFORE | SSL_ST_CONNECT:
231 case SSL_ST_OK | SSL_ST_CONNECT:
235 cb(s, SSL_CB_HANDSHAKE_START, 1);
237 if ((s->version >> 8) != SSL3_VERSION_MAJOR
238 && s->version != TLS_ANY_VERSION) {
239 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
240 s->state = SSL_ST_ERR;
245 if (s->version != TLS_ANY_VERSION &&
246 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
247 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
251 /* s->version=SSL3_VERSION; */
252 s->type = SSL_ST_CONNECT;
254 if (s->init_buf == NULL) {
255 if ((buf = BUF_MEM_new()) == NULL) {
257 s->state = SSL_ST_ERR;
260 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
262 s->state = SSL_ST_ERR;
269 if (!ssl3_setup_buffers(s)) {
274 /* setup buffing BIO */
275 if (!ssl_init_wbio_buffer(s, 0)) {
277 s->state = SSL_ST_ERR;
281 /* don't push the buffering BIO quite yet */
283 ssl3_init_finished_mac(s);
285 s->state = SSL3_ST_CW_CLNT_HELLO_A;
286 s->ctx->stats.sess_connect++;
289 * Should have been reset by ssl3_get_finished, too.
291 s->s3->change_cipher_spec = 0;
294 case SSL3_ST_CW_CLNT_HELLO_A:
295 case SSL3_ST_CW_CLNT_HELLO_B:
298 ret = ssl3_client_hello(s);
301 s->state = SSL3_ST_CR_SRVR_HELLO_A;
304 /* turn on buffering for the next lot of output */
305 if (s->bbio != s->wbio)
306 s->wbio = BIO_push(s->bbio, s->wbio);
310 case SSL3_ST_CR_SRVR_HELLO_A:
311 case SSL3_ST_CR_SRVR_HELLO_B:
312 ret = ssl3_get_server_hello(s);
317 s->state = SSL3_ST_CR_CHANGE_A;
318 if (s->tlsext_ticket_expected) {
319 /* receive renewed session ticket */
320 s->state = SSL3_ST_CR_SESSION_TICKET_A;
323 s->state = SSL3_ST_CR_CERT_A;
327 case SSL3_ST_CR_CERT_A:
328 case SSL3_ST_CR_CERT_B:
329 /* Noop (ret = 0) for everything but EAP-FAST. */
330 ret = ssl3_check_change(s);
335 s->state = SSL3_ST_CR_CHANGE_A;
340 /* Check if it is anon DH/ECDH, SRP auth */
342 if (!(s->s3->tmp.new_cipher->algorithm_auth &
343 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
344 ret = ssl3_get_server_certificate(s);
348 if (s->tlsext_status_expected)
349 s->state = SSL3_ST_CR_CERT_STATUS_A;
351 s->state = SSL3_ST_CR_KEY_EXCH_A;
354 s->state = SSL3_ST_CR_KEY_EXCH_A;
360 case SSL3_ST_CR_KEY_EXCH_A:
361 case SSL3_ST_CR_KEY_EXCH_B:
362 ret = ssl3_get_key_exchange(s);
365 s->state = SSL3_ST_CR_CERT_REQ_A;
369 * at this point we check that we have the required stuff from
372 if (!ssl3_check_cert_and_algorithm(s)) {
374 s->state = SSL_ST_ERR;
379 case SSL3_ST_CR_CERT_REQ_A:
380 case SSL3_ST_CR_CERT_REQ_B:
381 ret = ssl3_get_certificate_request(s);
384 s->state = SSL3_ST_CR_SRVR_DONE_A;
388 case SSL3_ST_CR_SRVR_DONE_A:
389 case SSL3_ST_CR_SRVR_DONE_B:
390 ret = ssl3_get_server_done(s);
394 if (s->s3->tmp.cert_req)
395 s->state = SSL3_ST_CW_CERT_A;
397 s->state = SSL3_ST_CW_KEY_EXCH_A;
402 case SSL3_ST_CW_CERT_A:
403 case SSL3_ST_CW_CERT_B:
404 case SSL3_ST_CW_CERT_C:
405 case SSL3_ST_CW_CERT_D:
406 ret = ssl3_send_client_certificate(s);
409 s->state = SSL3_ST_CW_KEY_EXCH_A;
413 case SSL3_ST_CW_KEY_EXCH_A:
414 case SSL3_ST_CW_KEY_EXCH_B:
415 ret = ssl3_send_client_key_exchange(s);
419 * EAY EAY EAY need to check for DH fix cert sent back
422 * For TLS, cert_req is set to 2, so a cert chain of nothing is
423 * sent, but no verify packet is sent
426 * XXX: For now, we do not support client authentication in ECDH
427 * cipher suites with ECDH (rather than ECDSA) certificates. We
428 * need to skip the certificate verify message when client's
429 * ECDH public key is sent inside the client certificate.
431 if (s->s3->tmp.cert_req == 1) {
432 s->state = SSL3_ST_CW_CERT_VRFY_A;
434 s->state = SSL3_ST_CW_CHANGE_A;
436 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
437 s->state = SSL3_ST_CW_CHANGE_A;
443 case SSL3_ST_CW_CERT_VRFY_A:
444 case SSL3_ST_CW_CERT_VRFY_B:
445 ret = ssl3_send_client_verify(s);
448 s->state = SSL3_ST_CW_CHANGE_A;
452 case SSL3_ST_CW_CHANGE_A:
453 case SSL3_ST_CW_CHANGE_B:
454 ret = ssl3_send_change_cipher_spec(s,
456 SSL3_ST_CW_CHANGE_B);
460 #if defined(OPENSSL_NO_NEXTPROTONEG)
461 s->state = SSL3_ST_CW_FINISHED_A;
463 if (s->s3->next_proto_neg_seen)
464 s->state = SSL3_ST_CW_NEXT_PROTO_A;
466 s->state = SSL3_ST_CW_FINISHED_A;
470 s->session->cipher = s->s3->tmp.new_cipher;
471 #ifdef OPENSSL_NO_COMP
472 s->session->compress_meth = 0;
474 if (s->s3->tmp.new_compression == NULL)
475 s->session->compress_meth = 0;
477 s->session->compress_meth = s->s3->tmp.new_compression->id;
479 if (!s->method->ssl3_enc->setup_key_block(s)) {
481 s->state = SSL_ST_ERR;
485 if (!s->method->ssl3_enc->change_cipher_state(s,
486 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
489 s->state = SSL_ST_ERR;
495 #if !defined(OPENSSL_NO_NEXTPROTONEG)
496 case SSL3_ST_CW_NEXT_PROTO_A:
497 case SSL3_ST_CW_NEXT_PROTO_B:
498 ret = ssl3_send_next_proto(s);
501 s->state = SSL3_ST_CW_FINISHED_A;
505 case SSL3_ST_CW_FINISHED_A:
506 case SSL3_ST_CW_FINISHED_B:
507 ret = ssl3_send_finished(s,
508 SSL3_ST_CW_FINISHED_A,
509 SSL3_ST_CW_FINISHED_B,
511 ssl3_enc->client_finished_label,
513 ssl3_enc->client_finished_label_len);
516 s->state = SSL3_ST_CW_FLUSH;
519 s->s3->tmp.next_state = SSL_ST_OK;
522 * Allow NewSessionTicket if ticket expected
524 if (s->tlsext_ticket_expected)
525 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
527 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE_A;
532 case SSL3_ST_CR_SESSION_TICKET_A:
533 case SSL3_ST_CR_SESSION_TICKET_B:
534 ret = ssl3_get_new_session_ticket(s);
537 s->state = SSL3_ST_CR_CHANGE_A;
541 case SSL3_ST_CR_CERT_STATUS_A:
542 case SSL3_ST_CR_CERT_STATUS_B:
543 ret = ssl3_get_cert_status(s);
546 s->state = SSL3_ST_CR_KEY_EXCH_A;
550 case SSL3_ST_CR_CHANGE_A:
551 case SSL3_ST_CR_CHANGE_B:
552 ret = ssl3_get_change_cipher_spec(s, SSL3_ST_CR_CHANGE_A,
553 SSL3_ST_CR_CHANGE_B);
557 s->state = SSL3_ST_CR_FINISHED_A;
561 case SSL3_ST_CR_FINISHED_A:
562 case SSL3_ST_CR_FINISHED_B:
563 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
564 SSL3_ST_CR_FINISHED_B);
569 s->state = SSL3_ST_CW_CHANGE_A;
571 s->state = SSL_ST_OK;
575 case SSL3_ST_CW_FLUSH:
576 s->rwstate = SSL_WRITING;
577 if (BIO_flush(s->wbio) <= 0) {
581 s->rwstate = SSL_NOTHING;
582 s->state = s->s3->tmp.next_state;
586 /* clean a few things up */
587 ssl3_cleanup_key_block(s);
588 BUF_MEM_free(s->init_buf);
591 /* remove the buffering */
592 ssl_free_wbio_buffer(s);
598 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
600 s->ctx->stats.sess_hit++;
604 s->handshake_func = ssl3_connect;
605 s->ctx->stats.sess_connect_good++;
608 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
615 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
621 /* did we do anything */
622 if (!s->s3->tmp.reuse_message && !skip) {
624 if ((ret = BIO_flush(s->wbio)) <= 0)
628 if ((cb != NULL) && (s->state != state)) {
629 new_state = s->state;
631 cb(s, SSL_CB_CONNECT_LOOP, 1);
632 s->state = new_state;
641 cb(s, SSL_CB_CONNECT_EXIT, ret);
645 #endif /* End temp disabled ssl3_connect */
647 * Work out what version we should be using for the initial ClientHello if
648 * the version is currently set to (D)TLS_ANY_VERSION.
649 * Returns 1 on success
652 static int ssl_set_version(SSL *s)
654 unsigned long mask, options = s->options;
656 if (s->method->version == TLS_ANY_VERSION) {
658 * SSL_OP_NO_X disables all protocols above X *if* there are
659 * some protocols below X enabled. This is required in order
660 * to maintain "version capability" vector contiguous. So
661 * that if application wants to disable TLS1.0 in favour of
662 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
663 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
665 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
666 #if !defined(OPENSSL_NO_SSL3)
670 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
671 if (options & SSL_OP_NO_TLSv1_2) {
672 if ((options & mask) != mask) {
673 s->version = TLS1_1_VERSION;
675 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
679 s->version = TLS1_2_VERSION;
682 if ((options & mask) == mask) {
683 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
686 s->version = TLS1_1_VERSION;
689 mask &= ~SSL_OP_NO_TLSv1_1;
690 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
691 s->version = TLS1_VERSION;
692 mask &= ~SSL_OP_NO_TLSv1;
693 #if !defined(OPENSSL_NO_SSL3)
694 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
695 s->version = SSL3_VERSION;
698 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
699 SSLerr(SSL_F_SSL_SET_VERSION,
700 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
704 if (s->version == SSL3_VERSION && FIPS_mode()) {
705 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
709 } else if (s->method->version == DTLS_ANY_VERSION) {
710 /* Determine which DTLS version to use */
711 /* If DTLS 1.2 disabled correct the version number */
712 if (options & SSL_OP_NO_DTLSv1_2) {
713 if (tls1_suiteb(s)) {
714 SSLerr(SSL_F_SSL_SET_VERSION,
715 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
719 * Disabling all versions is silly: return an error.
721 if (options & SSL_OP_NO_DTLSv1) {
722 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
726 * Update method so we don't use any DTLS 1.2 features.
728 s->method = DTLSv1_client_method();
729 s->version = DTLS1_VERSION;
732 * We only support one version: update method
734 if (options & SSL_OP_NO_DTLSv1)
735 s->method = DTLSv1_2_client_method();
736 s->version = DTLS1_2_VERSION;
740 s->client_version = s->version;
745 int ssl3_client_hello(SSL *s)
747 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
748 if(tls_construct_client_hello(s) == 0)
751 s->state = SSL3_ST_CW_CLNT_HELLO_B;
754 /* SSL3_ST_CW_CLNT_HELLO_B */
755 return ssl_do_write(s);
757 s->state = SSL_ST_ERR;
762 int tls_construct_client_hello(SSL *s)
765 unsigned char *p, *d;
769 #ifndef OPENSSL_NO_COMP
773 SSL_SESSION *sess = s->session;
775 buf = (unsigned char *)s->init_buf->data;
777 /* Work out what SSL/TLS/DTLS version to use */
778 if (ssl_set_version(s) == 0)
781 if ((sess == NULL) || (sess->ssl_version != s->version) ||
783 * In the case of EAP-FAST, we can have a pre-shared
784 * "ticket" without a session ID.
786 (!sess->session_id_length && !sess->tlsext_tick) ||
787 (sess->not_resumable)) {
788 if (!ssl_get_new_session(s, 0))
791 /* else use the pre-loaded session */
793 p = s->s3->client_random;
796 * for DTLS if client_random is initialized, reuse it, we are
797 * required to use same upon reply to HelloVerify
799 if (SSL_IS_DTLS(s)) {
802 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
811 if (i && ssl_fill_hello_random(s, 0, p,
812 sizeof(s->s3->client_random)) <= 0)
815 /* Do the message type and length last */
816 d = p = ssl_handshake_start(s);
819 * version indicates the negotiated version: for example from
820 * an SSLv2/v3 compatible client hello). The client_version
821 * field is the maximum version we permit and it is also
822 * used in RSA encrypted premaster secrets. Some servers can
823 * choke if we initially report a higher version then
824 * renegotiate to a lower one in the premaster secret. This
825 * didn't happen with TLS 1.0 as most servers supported it
826 * but it can with TLS 1.1 or later if the server only supports
829 * Possible scenario with previous logic:
830 * 1. Client hello indicates TLS 1.2
831 * 2. Server hello says TLS 1.0
832 * 3. RSA encrypted premaster secret uses 1.2.
833 * 4. Handhaked proceeds using TLS 1.0.
834 * 5. Server sends hello request to renegotiate.
835 * 6. Client hello indicates TLS v1.0 as we now
836 * know that is maximum server supports.
837 * 7. Server chokes on RSA encrypted premaster secret
838 * containing version 1.0.
840 * For interoperability it should be OK to always use the
841 * maximum version we support in client hello and then rely
842 * on the checking of version to ensure the servers isn't
843 * being inconsistent: for example initially negotiating with
844 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
845 * client_version in client hello and not resetting it to
846 * the negotiated version.
848 *(p++) = s->client_version >> 8;
849 *(p++) = s->client_version & 0xff;
852 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
853 p += SSL3_RANDOM_SIZE;
859 i = s->session->session_id_length;
862 if (i > (int)sizeof(s->session->session_id)) {
863 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
866 memcpy(p, s->session->session_id, i);
870 /* cookie stuff for DTLS */
871 if (SSL_IS_DTLS(s)) {
872 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
873 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
876 *(p++) = s->d1->cookie_len;
877 memcpy(p, s->d1->cookie, s->d1->cookie_len);
878 p += s->d1->cookie_len;
881 /* Ciphers supported */
882 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
884 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
887 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
889 * Some servers hang if client hello > 256 bytes as hack workaround
890 * chop number of supported ciphers to keep it well below this if we
893 if (TLS1_get_version(s) >= TLS1_2_VERSION
894 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
895 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
901 #ifdef OPENSSL_NO_COMP
905 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
908 j = sk_SSL_COMP_num(s->ctx->comp_methods);
910 for (i = 0; i < j; i++) {
911 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
915 *(p++) = 0; /* Add the NULL method */
918 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
919 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
923 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
925 ssl3_send_alert(s, SSL3_AL_FATAL, al);
926 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
931 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
932 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
933 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
943 int ssl3_get_server_hello(SSL *s)
948 * Hello verify request and/or server hello version may not match so set
949 * first packet if we're negotiating version.
953 n = s->method->ssl_get_message(s,
954 SSL3_ST_CR_SRVR_HELLO_A,
955 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
961 if (SSL_IS_DTLS(s)) {
962 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
963 if (s->d1->send_cookie == 0) {
964 s->s3->tmp.reuse_message = 1;
966 } else { /* already sent a cookie */
968 al = SSL_AD_UNEXPECTED_MESSAGE;
969 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
975 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
976 al = SSL_AD_UNEXPECTED_MESSAGE;
977 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
981 return tls_process_server_hello(s, (unsigned long)n);
983 ssl3_send_alert(s, SSL3_AL_FATAL, al);
984 s->state = SSL_ST_ERR;
988 enum MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, unsigned long n)
990 STACK_OF(SSL_CIPHER) *sk;
992 PACKET pkt, session_id;
993 size_t session_id_len;
994 unsigned char *cipherchars;
995 int i, al = SSL_AD_INTERNAL_ERROR;
996 unsigned int compression;
997 #ifndef OPENSSL_NO_COMP
1001 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1002 al = SSL_AD_INTERNAL_ERROR;
1003 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1007 if (s->method->version == TLS_ANY_VERSION) {
1008 unsigned int sversion;
1010 if (!PACKET_get_net_2(&pkt, &sversion)) {
1011 al = SSL_AD_DECODE_ERROR;
1012 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1016 #if TLS_MAX_VERSION != TLS1_2_VERSION
1017 #error Code needs updating for new TLS version
1019 #ifndef OPENSSL_NO_SSL3
1020 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1022 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1023 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1024 al = SSL_AD_PROTOCOL_VERSION;
1027 s->method = SSLv3_client_method();
1030 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1031 s->method = TLSv1_client_method();
1032 } else if ((sversion == TLS1_1_VERSION) &&
1033 !(s->options & SSL_OP_NO_TLSv1_1)) {
1034 s->method = TLSv1_1_client_method();
1035 } else if ((sversion == TLS1_2_VERSION) &&
1036 !(s->options & SSL_OP_NO_TLSv1_2)) {
1037 s->method = TLSv1_2_client_method();
1039 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1040 al = SSL_AD_PROTOCOL_VERSION;
1043 s->session->ssl_version = s->version = s->method->version;
1045 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1046 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1047 al = SSL_AD_PROTOCOL_VERSION;
1050 } else if (s->method->version == DTLS_ANY_VERSION) {
1051 /* Work out correct protocol version to use */
1052 unsigned int hversion;
1055 if (!PACKET_get_net_2(&pkt, &hversion)) {
1056 al = SSL_AD_DECODE_ERROR;
1057 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1061 options = s->options;
1062 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1063 s->method = DTLSv1_2_client_method();
1064 else if (tls1_suiteb(s)) {
1065 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1066 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1067 s->version = hversion;
1068 al = SSL_AD_PROTOCOL_VERSION;
1070 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1071 s->method = DTLSv1_client_method();
1073 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1074 s->version = hversion;
1075 al = SSL_AD_PROTOCOL_VERSION;
1078 s->session->ssl_version = s->version = s->method->version;
1080 unsigned char *vers;
1082 if (!PACKET_get_bytes(&pkt, &vers, 2)) {
1083 al = SSL_AD_DECODE_ERROR;
1084 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1087 if ((vers[0] != (s->version >> 8))
1088 || (vers[1] != (s->version & 0xff))) {
1089 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1090 s->version = (s->version & 0xff00) | vers[1];
1091 al = SSL_AD_PROTOCOL_VERSION;
1096 /* load the server hello data */
1097 /* load the server random */
1098 if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1099 al = SSL_AD_DECODE_ERROR;
1100 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1106 /* Get the session-id. */
1107 if (!PACKET_get_length_prefixed_1(&pkt, &session_id)) {
1108 al = SSL_AD_DECODE_ERROR;
1109 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1112 session_id_len = PACKET_remaining(&session_id);
1113 if (session_id_len > sizeof s->session->session_id
1114 || session_id_len > SSL3_SESSION_ID_SIZE) {
1115 al = SSL_AD_ILLEGAL_PARAMETER;
1116 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1120 if (!PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {
1121 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1122 al = SSL_AD_DECODE_ERROR;
1127 * Check if we can resume the session based on external pre-shared secret.
1128 * EAP-FAST (RFC 4851) supports two types of session resumption.
1129 * Resumption based on server-side state works with session IDs.
1130 * Resumption based on pre-shared Protected Access Credentials (PACs)
1131 * works by overriding the SessionTicket extension at the application
1132 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1133 * servers would honour the session ID.) Therefore, the session ID alone
1134 * is not a reliable indicator of session resumption, so we first check if
1135 * we can resume, and later peek at the next handshake message to see if the
1136 * server wants to resume.
1138 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1139 s->session->tlsext_tick) {
1140 SSL_CIPHER *pref_cipher = NULL;
1141 s->session->master_key_length = sizeof(s->session->master_key);
1142 if (s->tls_session_secret_cb(s, s->session->master_key,
1143 &s->session->master_key_length,
1145 s->tls_session_secret_cb_arg)) {
1146 s->session->cipher = pref_cipher ?
1147 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1149 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1150 al = SSL_AD_INTERNAL_ERROR;
1155 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1156 && memcmp(PACKET_data(&session_id), s->session->session_id,
1157 session_id_len) == 0) {
1158 if (s->sid_ctx_length != s->session->sid_ctx_length
1159 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1160 /* actually a client application bug */
1161 al = SSL_AD_ILLEGAL_PARAMETER;
1162 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1163 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1169 * If we were trying for session-id reuse but the server
1170 * didn't echo the ID, make a new SSL_SESSION.
1171 * In the case of EAP-FAST and PAC, we do not send a session ID,
1172 * so the PAC-based session secret is always preserved. It'll be
1173 * overwritten if the server refuses resumption.
1175 if (s->session->session_id_length > 0) {
1176 if (!ssl_get_new_session(s, 0)) {
1181 s->session->session_id_length = session_id_len;
1182 /* session_id_len could be 0 */
1183 memcpy(s->session->session_id, PACKET_data(&session_id),
1187 c = ssl_get_cipher_by_char(s, cipherchars);
1189 /* unknown cipher */
1190 al = SSL_AD_ILLEGAL_PARAMETER;
1191 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1194 /* Set version disabled mask now we know version */
1195 if (!SSL_USE_TLS1_2_CIPHERS(s))
1196 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1198 s->s3->tmp.mask_ssl = 0;
1200 * If it is a disabled cipher we didn't send it in client hello, so
1203 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1204 al = SSL_AD_ILLEGAL_PARAMETER;
1205 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1209 sk = ssl_get_ciphers_by_id(s);
1210 i = sk_SSL_CIPHER_find(sk, c);
1212 /* we did not say we would use this cipher */
1213 al = SSL_AD_ILLEGAL_PARAMETER;
1214 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1219 * Depending on the session caching (internal/external), the cipher
1220 * and/or cipher_id values may not be set. Make sure that cipher_id is
1221 * set and use it for comparison.
1223 if (s->session->cipher)
1224 s->session->cipher_id = s->session->cipher->id;
1225 if (s->hit && (s->session->cipher_id != c->id)) {
1226 al = SSL_AD_ILLEGAL_PARAMETER;
1227 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1228 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1231 s->s3->tmp.new_cipher = c;
1233 * Don't digest cached records if no sigalgs: we may need them for client
1236 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1238 /* lets get the compression algorithm */
1240 if (!PACKET_get_1(&pkt, &compression)) {
1241 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1242 al = SSL_AD_DECODE_ERROR;
1245 #ifdef OPENSSL_NO_COMP
1246 if (compression != 0) {
1247 al = SSL_AD_ILLEGAL_PARAMETER;
1248 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1249 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1253 * If compression is disabled we'd better not try to resume a session
1254 * using compression.
1256 if (s->session->compress_meth != 0) {
1257 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1261 if (s->hit && compression != s->session->compress_meth) {
1262 al = SSL_AD_ILLEGAL_PARAMETER;
1263 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1264 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1267 if (compression == 0)
1269 else if (!ssl_allow_compression(s)) {
1270 al = SSL_AD_ILLEGAL_PARAMETER;
1271 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1274 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1277 if (compression != 0 && comp == NULL) {
1278 al = SSL_AD_ILLEGAL_PARAMETER;
1279 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1280 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1283 s->s3->tmp.new_compression = comp;
1287 /* TLS extensions */
1288 if (!ssl_parse_serverhello_tlsext(s, &pkt)) {
1289 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1293 if (PACKET_remaining(&pkt) != 0) {
1294 /* wrong packet length */
1295 al = SSL_AD_DECODE_ERROR;
1296 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1300 #ifndef OPENSSL_NO_SCTP
1301 if (SSL_IS_DTLS(s) && s->hit) {
1302 unsigned char sctpauthkey[64];
1303 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1306 * Add new shared key for SCTP-Auth, will be ignored if
1309 snprintf((char *)labelbuffer,
1310 sizeof(DTLS1_SCTP_AUTH_LABEL),
1311 DTLS1_SCTP_AUTH_LABEL);
1313 if (SSL_export_keying_material(s, sctpauthkey,
1314 sizeof(sctpauthkey),
1316 sizeof(labelbuffer), NULL, 0,
1320 BIO_ctrl(SSL_get_wbio(s),
1321 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1322 sizeof(sctpauthkey), sctpauthkey);
1326 return MSG_PROCESS_CONTINUE_READING;
1328 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1330 statem_set_error(s);
1331 return MSG_PROCESS_ERROR;
1334 int ssl3_get_server_certificate(SSL *s)
1339 n = s->method->ssl_get_message(s,
1342 -1, s->max_cert_list, &ok);
1347 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1348 s->s3->tmp.reuse_message = 1;
1352 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1353 al = SSL_AD_UNEXPECTED_MESSAGE;
1354 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1358 return tls_process_server_certificate(s, (unsigned long)n);
1360 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1361 s->state = SSL_ST_ERR;
1365 enum MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, unsigned long n)
1367 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1368 unsigned long cert_list_len, cert_len;
1370 unsigned char *certstart, *certbytes;
1371 STACK_OF(X509) *sk = NULL;
1372 EVP_PKEY *pkey = NULL;
1375 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1376 al = SSL_AD_INTERNAL_ERROR;
1377 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1381 if ((sk = sk_X509_new_null()) == NULL) {
1382 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1386 if (!PACKET_get_net_3(&pkt, &cert_list_len)
1387 || PACKET_remaining(&pkt) != cert_list_len) {
1388 al = SSL_AD_DECODE_ERROR;
1389 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1392 while (PACKET_remaining(&pkt)) {
1393 if (!PACKET_get_net_3(&pkt, &cert_len)
1394 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
1395 al = SSL_AD_DECODE_ERROR;
1396 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1397 SSL_R_CERT_LENGTH_MISMATCH);
1401 certstart = certbytes;
1402 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1404 al = SSL_AD_BAD_CERTIFICATE;
1405 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1408 if (certbytes != (certstart + cert_len)) {
1409 al = SSL_AD_DECODE_ERROR;
1410 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1411 SSL_R_CERT_LENGTH_MISMATCH);
1414 if (!sk_X509_push(sk, x)) {
1415 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1421 i = ssl_verify_cert_chain(s, sk);
1422 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1423 al = ssl_verify_alarm_type(s->verify_result);
1424 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1425 SSL_R_CERTIFICATE_VERIFY_FAILED);
1428 ERR_clear_error(); /* but we keep s->verify_result */
1430 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1431 al = SSL_AD_HANDSHAKE_FAILURE;
1435 s->session->peer_chain = sk;
1437 * Inconsistency alert: cert_chain does include the peer's certificate,
1438 * which we don't include in s3_srvr.c
1440 x = sk_X509_value(sk, 0);
1443 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1446 pkey = X509_get_pubkey(x);
1448 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1451 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1452 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1456 i = ssl_cert_type(x, pkey);
1460 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1461 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1465 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1466 if (exp_idx >= 0 && i != exp_idx) {
1468 al = SSL_AD_ILLEGAL_PARAMETER;
1469 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1470 SSL_R_WRONG_CERTIFICATE_TYPE);
1473 s->session->peer_type = i;
1475 X509_free(s->session->peer);
1477 s->session->peer = x;
1478 s->session->verify_result = s->verify_result;
1481 ret = MSG_PROCESS_CONTINUE_READING;
1485 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1487 statem_set_error(s);
1489 EVP_PKEY_free(pkey);
1491 sk_X509_pop_free(sk, X509_free);
1495 int ssl3_get_key_exchange(SSL *s)
1502 * use same message size as in ssl3_get_certificate_request() as
1503 * ServerKeyExchange message may be skipped
1505 n = s->method->ssl_get_message(s,
1506 SSL3_ST_CR_KEY_EXCH_A,
1507 SSL3_ST_CR_KEY_EXCH_B,
1508 -1, s->max_cert_list, &ok);
1512 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1514 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1516 * Can't skip server key exchange if this is an ephemeral
1519 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
1520 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1521 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1525 s->s3->tmp.reuse_message = 1;
1529 return tls_process_key_exchange(s, (unsigned long)n);
1532 enum MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, unsigned long n)
1534 #ifndef OPENSSL_NO_RSA
1535 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1538 int al, j, verify_ret;
1540 EVP_PKEY *pkey = NULL;
1541 const EVP_MD *md = NULL;
1542 #ifndef OPENSSL_NO_RSA
1545 #ifndef OPENSSL_NO_DH
1548 #ifndef OPENSSL_NO_EC
1549 EC_KEY *ecdh = NULL;
1550 BN_CTX *bn_ctx = NULL;
1551 EC_POINT *srvr_ecpoint = NULL;
1554 PACKET pkt, save_param_start, signature;
1556 EVP_MD_CTX_init(&md_ctx);
1558 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1560 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1561 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1562 al = SSL_AD_INTERNAL_ERROR;
1565 save_param_start = pkt;
1567 #ifndef OPENSSL_NO_RSA
1568 RSA_free(s->s3->peer_rsa_tmp);
1569 s->s3->peer_rsa_tmp = NULL;
1571 #ifndef OPENSSL_NO_DH
1572 DH_free(s->s3->peer_dh_tmp);
1573 s->s3->peer_dh_tmp = NULL;
1575 #ifndef OPENSSL_NO_EC
1576 EC_KEY_free(s->s3->peer_ecdh_tmp);
1577 s->s3->peer_ecdh_tmp = NULL;
1580 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1582 al = SSL_AD_DECODE_ERROR;
1584 #ifndef OPENSSL_NO_PSK
1585 /* PSK ciphersuites are preceded by an identity hint */
1586 if (alg_k & SSL_PSK) {
1587 PACKET psk_identity_hint;
1588 if (!PACKET_get_length_prefixed_2(&pkt, &psk_identity_hint)) {
1589 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1594 * Store PSK identity hint for later use, hint is used in
1595 * ssl3_send_client_key_exchange. Assume that the maximum length of
1596 * a PSK identity hint can be as long as the maximum length of a PSK
1599 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1600 al = SSL_AD_HANDSHAKE_FAILURE;
1601 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1605 if (!PACKET_strndup(&psk_identity_hint,
1606 &s->session->psk_identity_hint)) {
1607 al = SSL_AD_INTERNAL_ERROR;
1612 /* Nothing else to do for plain PSK or RSAPSK */
1613 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1615 #endif /* !OPENSSL_NO_PSK */
1616 #ifndef OPENSSL_NO_SRP
1617 if (alg_k & SSL_kSRP) {
1618 PACKET prime, generator, salt, server_pub;
1619 if (!PACKET_get_length_prefixed_2(&pkt, &prime)
1620 || !PACKET_get_length_prefixed_2(&pkt, &generator)
1621 || !PACKET_get_length_prefixed_1(&pkt, &salt)
1622 || !PACKET_get_length_prefixed_2(&pkt, &server_pub)) {
1623 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1628 BN_bin2bn(PACKET_data(&prime),
1629 PACKET_remaining(&prime), NULL)) == NULL
1631 BN_bin2bn(PACKET_data(&generator),
1632 PACKET_remaining(&generator), NULL)) == NULL
1634 BN_bin2bn(PACKET_data(&salt),
1635 PACKET_remaining(&salt), NULL)) == NULL
1637 BN_bin2bn(PACKET_data(&server_pub),
1638 PACKET_remaining(&server_pub), NULL)) == NULL) {
1639 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1643 if (!srp_verify_server_param(s, &al)) {
1644 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1648 /* We must check if there is a certificate */
1649 if (alg_a & (SSL_aRSA|SSL_aDSS))
1650 pkey = X509_get_pubkey(s->session->peer);
1652 #endif /* !OPENSSL_NO_SRP */
1653 #ifndef OPENSSL_NO_RSA
1654 if (alg_k & SSL_kRSA) {
1656 /* Temporary RSA keys only allowed in export ciphersuites */
1657 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1658 al = SSL_AD_UNEXPECTED_MESSAGE;
1659 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1663 if (!PACKET_get_length_prefixed_2(&pkt, &mod)
1664 || !PACKET_get_length_prefixed_2(&pkt, &exp)) {
1665 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1669 if ((rsa = RSA_new()) == NULL) {
1670 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1674 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1676 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1678 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1682 /* this should be because we are using an export cipher */
1683 if (alg_a & SSL_aRSA)
1684 pkey = X509_get_pubkey(s->session->peer);
1686 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1690 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1691 al = SSL_AD_UNEXPECTED_MESSAGE;
1692 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1696 s->s3->peer_rsa_tmp = rsa;
1699 #else /* OPENSSL_NO_RSA */
1702 #ifndef OPENSSL_NO_DH
1703 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1704 PACKET prime, generator, pub_key;
1706 if (!PACKET_get_length_prefixed_2(&pkt, &prime)
1707 || !PACKET_get_length_prefixed_2(&pkt, &generator)
1708 || !PACKET_get_length_prefixed_2(&pkt, &pub_key)) {
1709 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1713 if ((dh = DH_new()) == NULL) {
1714 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1718 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1719 PACKET_remaining(&prime), NULL)) == NULL
1720 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1721 PACKET_remaining(&generator), NULL)) == NULL
1723 BN_bin2bn(PACKET_data(&pub_key),
1724 PACKET_remaining(&pub_key), NULL)) == NULL) {
1725 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1729 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1730 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1734 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1735 al = SSL_AD_HANDSHAKE_FAILURE;
1736 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1739 if (alg_a & (SSL_aRSA|SSL_aDSS))
1740 pkey = X509_get_pubkey(s->session->peer);
1741 /* else anonymous DH, so no certificate or pkey. */
1743 s->s3->peer_dh_tmp = dh;
1746 #endif /* !OPENSSL_NO_DH */
1748 #ifndef OPENSSL_NO_EC
1749 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1751 const EC_GROUP *group;
1753 unsigned char *ecparams;
1755 if ((ecdh = EC_KEY_new()) == NULL) {
1756 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1761 * Extract elliptic curve parameters and the server's ephemeral ECDH
1762 * public key. For now we only support named (not generic) curves and
1763 * ECParameters in this case is just three bytes.
1765 if (!PACKET_get_bytes(&pkt, &ecparams, 3)) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1770 * Check curve is one of our preferences, if not server has sent an
1771 * invalid curve. ECParameters is 3 bytes.
1773 if (!tls1_check_curve(s, ecparams, 3)) {
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1778 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
1779 al = SSL_AD_INTERNAL_ERROR;
1780 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1781 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1785 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1786 if (ngroup == NULL) {
1787 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1790 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1791 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1794 EC_GROUP_free(ngroup);
1796 group = EC_KEY_get0_group(ecdh);
1798 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1799 (EC_GROUP_get_degree(group) > 163)) {
1800 al = SSL_AD_EXPORT_RESTRICTION;
1801 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1802 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1806 /* Next, get the encoded ECPoint */
1807 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1808 ((bn_ctx = BN_CTX_new()) == NULL)) {
1809 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1813 if (!PACKET_get_length_prefixed_1(&pkt, &encoded_pt)) {
1814 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1818 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1819 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
1820 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1825 * The ECC/TLS specification does not mention the use of DSA to sign
1826 * ECParameters in the server key exchange message. We do support RSA
1830 # ifndef OPENSSL_NO_RSA
1831 else if (alg_a & SSL_aRSA)
1832 pkey = X509_get_pubkey(s->session->peer);
1834 # ifndef OPENSSL_NO_EC
1835 else if (alg_a & SSL_aECDSA)
1836 pkey = X509_get_pubkey(s->session->peer);
1838 /* else anonymous ECDH, so no certificate or pkey. */
1839 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1840 s->s3->peer_ecdh_tmp = ecdh;
1842 BN_CTX_free(bn_ctx);
1844 EC_POINT_free(srvr_ecpoint);
1845 srvr_ecpoint = NULL;
1847 al = SSL_AD_UNEXPECTED_MESSAGE;
1848 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1851 #endif /* !OPENSSL_NO_EC */
1853 /* if it was signed, check the signature */
1857 * |pkt| now points to the beginning of the signature, so the difference
1858 * equals the length of the parameters.
1860 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1861 PACKET_remaining(&save_param_start) -
1862 PACKET_remaining(&pkt))) {
1863 al = SSL_AD_INTERNAL_ERROR;
1864 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1868 if (SSL_USE_SIGALGS(s)) {
1869 unsigned char *sigalgs;
1871 if (!PACKET_get_bytes(&pkt, &sigalgs, 2)) {
1872 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1875 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1882 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1888 if (!PACKET_get_length_prefixed_2(&pkt, &signature)
1889 || PACKET_remaining(&pkt) != 0) {
1890 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1893 j = EVP_PKEY_size(pkey);
1895 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1900 * Check signature length
1902 if (PACKET_remaining(&signature) > (size_t)j) {
1903 /* wrong packet length */
1904 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1907 #ifndef OPENSSL_NO_RSA
1908 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1914 for (num = 2; num > 0; num--) {
1915 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1916 EVP_DigestInit_ex(&md_ctx, (num == 2)
1917 ? s->ctx->md5 : s->ctx->sha1, NULL);
1918 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1920 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1922 EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms),
1923 PACKET_remaining(¶ms));
1924 EVP_DigestFinal_ex(&md_ctx, q, &size);
1929 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1930 PACKET_remaining(&signature), pkey->pkey.rsa);
1931 if (verify_ret < 0) {
1932 al = SSL_AD_DECRYPT_ERROR;
1933 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1936 if (verify_ret == 0) {
1938 al = SSL_AD_DECRYPT_ERROR;
1939 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1945 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1946 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1948 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1950 EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms),
1951 PACKET_remaining(¶ms));
1952 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1953 PACKET_remaining(&signature), pkey) <= 0) {
1955 al = SSL_AD_DECRYPT_ERROR;
1956 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1961 /* aNULL, aSRP or PSK do not need public keys */
1962 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1963 /* Might be wrong key type, check it */
1964 if (ssl3_check_cert_and_algorithm(s))
1965 /* Otherwise this shouldn't happen */
1966 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1969 /* still data left over */
1970 if (PACKET_remaining(&pkt) != 0) {
1971 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1975 EVP_PKEY_free(pkey);
1976 EVP_MD_CTX_cleanup(&md_ctx);
1977 return MSG_PROCESS_CONTINUE_READING;
1979 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1981 EVP_PKEY_free(pkey);
1982 #ifndef OPENSSL_NO_RSA
1985 #ifndef OPENSSL_NO_DH
1988 #ifndef OPENSSL_NO_EC
1989 BN_CTX_free(bn_ctx);
1990 EC_POINT_free(srvr_ecpoint);
1993 EVP_MD_CTX_cleanup(&md_ctx);
1994 statem_set_error(s);
1995 return MSG_PROCESS_ERROR;
1998 int ssl3_get_certificate_request(SSL *s)
2003 n = s->method->ssl_get_message(s,
2004 SSL3_ST_CR_CERT_REQ_A,
2005 SSL3_ST_CR_CERT_REQ_B,
2006 -1, s->max_cert_list, &ok);
2011 s->s3->tmp.cert_req = 0;
2013 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2014 s->s3->tmp.reuse_message = 1;
2016 * If we get here we don't need any cached handshake records as we
2017 * wont be doing client auth.
2019 if (!ssl3_digest_cached_records(s, 0))
2024 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2025 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2026 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2030 /* TLS does not like anon-DH with client cert */
2031 if (s->version > SSL3_VERSION) {
2032 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2034 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2035 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2040 return tls_process_certificate_request(s, (unsigned long)n);
2042 s->state = SSL_ST_ERR;
2047 enum MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, unsigned long n)
2049 int ret = MSG_PROCESS_ERROR;
2050 unsigned int list_len, ctype_num, i, name_len;
2051 X509_NAME *xn = NULL;
2052 unsigned char *data;
2053 unsigned char *namestart, *namebytes;
2054 STACK_OF(X509_NAME) *ca_sk = NULL;
2057 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2058 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2059 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2063 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2064 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2068 /* get the certificate types */
2069 if (!PACKET_get_1(&pkt, &ctype_num)
2070 || !PACKET_get_bytes(&pkt, &data, ctype_num)) {
2071 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2072 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2075 OPENSSL_free(s->cert->ctypes);
2076 s->cert->ctypes = NULL;
2077 if (ctype_num > SSL3_CT_NUMBER) {
2078 /* If we exceed static buffer copy all to cert structure */
2079 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2080 if (s->cert->ctypes == NULL) {
2081 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2084 memcpy(s->cert->ctypes, data, ctype_num);
2085 s->cert->ctype_num = (size_t)ctype_num;
2086 ctype_num = SSL3_CT_NUMBER;
2088 for (i = 0; i < ctype_num; i++)
2089 s->s3->tmp.ctype[i] = data[i];
2091 if (SSL_USE_SIGALGS(s)) {
2092 if (!PACKET_get_net_2(&pkt, &list_len)
2093 || !PACKET_get_bytes(&pkt, &data, list_len)) {
2094 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2095 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2096 SSL_R_LENGTH_MISMATCH);
2100 /* Clear certificate digests and validity flags */
2101 for (i = 0; i < SSL_PKEY_NUM; i++) {
2102 s->s3->tmp.md[i] = NULL;
2103 s->s3->tmp.valid_flags[i] = 0;
2105 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2106 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2107 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2108 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2111 if (!tls1_process_sigalgs(s)) {
2112 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2113 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2118 /* get the CA RDNs */
2119 if (!PACKET_get_net_2(&pkt, &list_len)
2120 || PACKET_remaining(&pkt) != list_len) {
2121 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2122 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2126 while (PACKET_remaining(&pkt)) {
2127 if (!PACKET_get_net_2(&pkt, &name_len)
2128 || !PACKET_get_bytes(&pkt, &namebytes, name_len)) {
2129 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2130 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2131 SSL_R_LENGTH_MISMATCH);
2135 namestart = namebytes;
2137 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2138 name_len)) == NULL) {
2139 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2140 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2144 if (namebytes != (namestart + name_len)) {
2145 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2146 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2147 SSL_R_CA_DN_LENGTH_MISMATCH);
2150 if (!sk_X509_NAME_push(ca_sk, xn)) {
2151 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2156 /* we should setup a certificate to return.... */
2157 s->s3->tmp.cert_req = 1;
2158 s->s3->tmp.ctype_num = ctype_num;
2159 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2160 s->s3->tmp.ca_names = ca_sk;
2163 ret = MSG_PROCESS_CONTINUE_READING;
2166 statem_set_error(s);
2168 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2172 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2174 return (X509_NAME_cmp(*a, *b));
2177 int ssl3_get_new_session_ticket(SSL *s)
2182 n = s->method->ssl_get_message(s,
2183 SSL3_ST_CR_SESSION_TICKET_A,
2184 SSL3_ST_CR_SESSION_TICKET_B,
2185 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2190 return tls_process_new_session_ticket(s, (unsigned long)n);
2193 enum MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, unsigned long n)
2196 unsigned int ticklen;
2197 unsigned long ticket_lifetime_hint;
2200 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2201 al = SSL_AD_INTERNAL_ERROR;
2202 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
2206 if (!PACKET_get_net_4(&pkt, &ticket_lifetime_hint)
2207 || !PACKET_get_net_2(&pkt, &ticklen)
2208 || PACKET_remaining(&pkt) != ticklen) {
2209 al = SSL_AD_DECODE_ERROR;
2210 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2214 /* Server is allowed to change its mind and send an empty ticket. */
2218 if (s->session->session_id_length > 0) {
2219 int i = s->session_ctx->session_cache_mode;
2220 SSL_SESSION *new_sess;
2222 * We reused an existing session, so we need to replace it with a new
2225 if (i & SSL_SESS_CACHE_CLIENT) {
2227 * Remove the old session from the cache
2229 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2230 if (s->session_ctx->remove_session_cb != NULL)
2231 s->session_ctx->remove_session_cb(s->session_ctx,
2234 /* We carry on if this fails */
2235 SSL_CTX_remove_session(s->session_ctx, s->session);
2239 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2240 al = SSL_AD_INTERNAL_ERROR;
2241 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2245 SSL_SESSION_free(s->session);
2246 s->session = new_sess;
2249 OPENSSL_free(s->session->tlsext_tick);
2250 s->session->tlsext_ticklen = 0;
2252 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2253 if (!s->session->tlsext_tick) {
2254 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2257 if (!PACKET_copy_bytes(&pkt, s->session->tlsext_tick, ticklen)) {
2258 al = SSL_AD_DECODE_ERROR;
2259 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2263 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2264 s->session->tlsext_ticklen = ticklen;
2266 * There are two ways to detect a resumed ticket session. One is to set
2267 * an appropriate session ID and then the server must return a match in
2268 * ServerHello. This allows the normal client session ID matching to work
2269 * and we know much earlier that the ticket has been accepted. The
2270 * other way is to set zero length session ID when the ticket is
2271 * presented and rely on the handshake to determine session resumption.
2272 * We choose the former approach because this fits in with assumptions
2273 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2274 * SHA256 is disabled) hash of the ticket.
2276 EVP_Digest(s->session->tlsext_tick, ticklen,
2277 s->session->session_id, &s->session->session_id_length,
2278 EVP_sha256(), NULL);
2279 return MSG_PROCESS_CONTINUE_READING;
2281 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2283 statem_set_error(s);
2284 return MSG_PROCESS_ERROR;
2287 int ssl3_get_cert_status(SSL *s)
2292 n = s->method->ssl_get_message(s,
2293 SSL3_ST_CR_CERT_STATUS_A,
2294 SSL3_ST_CR_CERT_STATUS_B,
2295 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2300 return tls_process_cert_status(s, (unsigned long)n);
2303 enum MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, unsigned long n)
2306 unsigned long resplen;
2310 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2311 al = SSL_AD_INTERNAL_ERROR;
2312 SSLerr(SSL_F_TLS_PROCESS_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_TLS_PROCESS_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_TLS_PROCESS_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_TLS_PROCESS_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_TLS_PROCESS_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_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2349 al = SSL_AD_INTERNAL_ERROR;
2350 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2354 return MSG_PROCESS_CONTINUE_READING;
2356 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2357 statem_set_error(s);
2358 return MSG_PROCESS_ERROR;
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 return tls_process_server_done(s, (unsigned long)n);
2378 enum MSG_PROCESS_RETURN tls_process_server_done(SSL *s, unsigned long n)
2381 /* should contain no data */
2382 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2383 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2384 statem_set_error(s);
2385 return MSG_PROCESS_ERROR;
2388 #ifndef OPENSSL_NO_SRP
2389 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2390 if (SRP_Calc_A_param(s) <= 0) {
2391 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2392 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2393 statem_set_error(s);
2394 return MSG_PROCESS_ERROR;
2399 return MSG_PROCESS_FINISHED_READING;
2402 int ssl3_send_client_key_exchange(SSL *s)
2406 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2407 if(tls_construct_client_key_exchange(s) == 0)
2410 s->state = SSL3_ST_CW_KEY_EXCH_B;
2413 /* SSL3_ST_CW_KEY_EXCH_B */
2414 n = ssl_do_write(s);
2417 if (tls_client_key_exchange_post_work(s) == 0)
2423 s->state = SSL_ST_ERR;
2428 int tls_construct_client_key_exchange(SSL *s)
2432 #ifndef OPENSSL_NO_PSK
2433 size_t pskhdrlen = 0;
2435 unsigned long alg_k;
2436 #ifndef OPENSSL_NO_RSA
2438 EVP_PKEY *pkey = NULL;
2440 #ifndef OPENSSL_NO_EC
2441 EC_KEY *clnt_ecdh = NULL;
2442 const EC_POINT *srvr_ecpoint = NULL;
2443 EVP_PKEY *srvr_pub_pkey = NULL;
2444 unsigned char *encodedPoint = NULL;
2445 int encoded_pt_len = 0;
2446 BN_CTX *bn_ctx = NULL;
2448 unsigned char *pms = NULL;
2450 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2452 p = ssl_handshake_start(s);
2455 #ifndef OPENSSL_NO_PSK
2456 if (alg_k & SSL_PSK) {
2459 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2460 * \0-terminated identity. The last byte is for us for simulating
2463 char identity[PSK_MAX_IDENTITY_LEN + 1];
2465 unsigned char psk[PSK_MAX_PSK_LEN];
2468 if (s->psk_client_callback == NULL) {
2469 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2470 SSL_R_PSK_NO_CLIENT_CB);
2474 memset(identity, 0, sizeof(identity));
2476 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2477 identity, sizeof(identity) - 1,
2480 if (psklen > PSK_MAX_PSK_LEN) {
2481 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2482 ERR_R_INTERNAL_ERROR);
2484 } else if (psklen == 0) {
2485 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2486 SSL_R_PSK_IDENTITY_NOT_FOUND);
2490 OPENSSL_free(s->s3->tmp.psk);
2491 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2492 OPENSSL_cleanse(psk, psklen);
2494 if (s->s3->tmp.psk == NULL) {
2495 OPENSSL_cleanse(identity, sizeof(identity));
2499 s->s3->tmp.psklen = psklen;
2501 identitylen = strlen(identity);
2502 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2503 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2504 ERR_R_INTERNAL_ERROR);
2507 OPENSSL_free(s->session->psk_identity);
2508 s->session->psk_identity = BUF_strdup(identity);
2509 if (s->session->psk_identity == NULL) {
2510 OPENSSL_cleanse(identity, sizeof(identity));
2514 s2n(identitylen, p);
2515 memcpy(p, identity, identitylen);
2516 pskhdrlen = 2 + identitylen;
2520 OPENSSL_cleanse(identity, sizeof(identity));
2522 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2526 if (alg_k & SSL_kPSK) {
2531 /* Fool emacs indentation */
2534 #ifndef OPENSSL_NO_RSA
2535 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2537 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2538 pms = OPENSSL_malloc(pmslen);
2542 if (s->session->peer == NULL) {
2544 * We should always have a server certificate with SSL_kRSA.
2546 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2547 ERR_R_INTERNAL_ERROR);
2551 if (s->s3->peer_rsa_tmp != NULL)
2552 rsa = s->s3->peer_rsa_tmp;
2554 pkey = X509_get_pubkey(s->session->peer);
2555 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2556 || (pkey->pkey.rsa == NULL)) {
2557 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2558 ERR_R_INTERNAL_ERROR);
2559 EVP_PKEY_free(pkey);
2562 rsa = pkey->pkey.rsa;
2563 EVP_PKEY_free(pkey);
2566 pms[0] = s->client_version >> 8;
2567 pms[1] = s->client_version & 0xff;
2568 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2572 /* Fix buf for TLS and beyond */
2573 if (s->version > SSL3_VERSION)
2575 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2577 if (s->options & SSL_OP_PKCS1_CHECK_1)
2579 if (s->options & SSL_OP_PKCS1_CHECK_2)
2583 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2584 SSL_R_BAD_RSA_ENCRYPT);
2588 /* Fix buf for TLS and beyond */
2589 if (s->version > SSL3_VERSION) {
2595 #ifndef OPENSSL_NO_DH
2596 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2597 DH *dh_srvr, *dh_clnt;
2598 if (s->s3->peer_dh_tmp != NULL)
2599 dh_srvr = s->s3->peer_dh_tmp;
2601 /* we get them from the cert */
2602 EVP_PKEY *spkey = NULL;
2604 spkey = X509_get_pubkey(s->session->peer);
2606 dh_srvr = EVP_PKEY_get1_DH(spkey);
2607 EVP_PKEY_free(spkey);
2609 if (dh_srvr == NULL) {
2610 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2611 ERR_R_INTERNAL_ERROR);
2615 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2616 /* Use client certificate key */
2617 EVP_PKEY *clkey = s->cert->key->privatekey;
2620 dh_clnt = EVP_PKEY_get1_DH(clkey);
2621 if (dh_clnt == NULL) {
2622 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2623 ERR_R_INTERNAL_ERROR);
2627 /* generate a new random key */
2628 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2629 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2632 if (!DH_generate_key(dh_clnt)) {
2633 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2639 pmslen = DH_size(dh_clnt);
2640 pms = OPENSSL_malloc(pmslen);
2645 * use the 'p' output buffer for the DH key, but make sure to
2646 * clear it out afterwards
2649 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2650 if (s->s3->peer_dh_tmp == NULL)
2654 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2660 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2663 /* send off the data */
2664 n = BN_num_bytes(dh_clnt->pub_key);
2666 BN_bn2bin(dh_clnt->pub_key, p);
2674 #ifndef OPENSSL_NO_EC
2675 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2676 const EC_GROUP *srvr_group = NULL;
2678 int ecdh_clnt_cert = 0;
2681 * Did we send out the client's ECDH share for use in premaster
2682 * computation as part of client certificate? If so, set
2683 * ecdh_clnt_cert to 1.
2685 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2687 * XXX: For now, we do not support client
2688 * authentication using ECDH certificates.
2689 * To add such support, one needs to add
2690 * code that checks for appropriate
2691 * conditions and sets ecdh_clnt_cert to 1.
2692 * For example, the cert have an ECC
2693 * key on the same curve as the server's
2694 * and the key should be authorized for
2697 * One also needs to add code in ssl3_connect
2698 * to skip sending the certificate verify
2701 * if ((s->cert->key->privatekey != NULL) &&
2702 * (s->cert->key->privatekey->type ==
2703 * EVP_PKEY_EC) && ...)
2704 * ecdh_clnt_cert = 1;
2708 if (s->s3->peer_ecdh_tmp != NULL) {
2709 tkey = s->s3->peer_ecdh_tmp;
2711 /* Get the Server Public Key from Cert */
2712 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2713 if ((srvr_pub_pkey == NULL)
2714 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2715 || (srvr_pub_pkey->pkey.ec == NULL)) {
2716 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2717 ERR_R_INTERNAL_ERROR);
2721 tkey = srvr_pub_pkey->pkey.ec;
2724 srvr_group = EC_KEY_get0_group(tkey);
2725 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2727 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2728 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2729 ERR_R_INTERNAL_ERROR);
2733 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2734 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2735 ERR_R_MALLOC_FAILURE);
2739 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2740 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2743 if (ecdh_clnt_cert) {
2745 * Reuse key info from our certificate We only need our
2746 * private key to perform the ECDH computation.
2748 const BIGNUM *priv_key;
2749 tkey = s->cert->key->privatekey->pkey.ec;
2750 priv_key = EC_KEY_get0_private_key(tkey);
2751 if (priv_key == NULL) {
2752 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2753 ERR_R_MALLOC_FAILURE);
2756 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2757 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2761 /* Generate a new ECDH key pair */
2762 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2763 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2770 * use the 'p' output buffer for the ECDH key, but make sure to
2771 * clear it out afterwards
2774 field_size = EC_GROUP_get_degree(srvr_group);
2775 if (field_size <= 0) {
2776 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2779 pmslen = (field_size + 7) / 8;
2780 pms = OPENSSL_malloc(pmslen);
2783 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2784 if (n <= 0 || pmslen != (size_t)n) {
2785 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2789 if (ecdh_clnt_cert) {
2790 /* Send empty client key exch message */
2794 * First check the size of encoding and allocate memory
2798 EC_POINT_point2oct(srvr_group,
2799 EC_KEY_get0_public_key(clnt_ecdh),
2800 POINT_CONVERSION_UNCOMPRESSED,
2803 encodedPoint = (unsigned char *)
2804 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2805 bn_ctx = BN_CTX_new();
2806 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2807 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2808 ERR_R_MALLOC_FAILURE);
2812 /* Encode the public key */
2813 n = EC_POINT_point2oct(srvr_group,
2814 EC_KEY_get0_public_key(clnt_ecdh),
2815 POINT_CONVERSION_UNCOMPRESSED,
2816 encodedPoint, encoded_pt_len, bn_ctx);
2818 *p = n; /* length of encoded point */
2819 /* Encoded point will be copied here */
2821 /* copy the point */
2822 memcpy(p, encodedPoint, n);
2823 /* increment n to account for length field */
2827 /* Free allocated memory */
2828 BN_CTX_free(bn_ctx);
2829 OPENSSL_free(encodedPoint);
2830 EC_KEY_free(clnt_ecdh);
2831 EVP_PKEY_free(srvr_pub_pkey);
2833 #endif /* !OPENSSL_NO_EC */
2834 else if (alg_k & SSL_kGOST) {
2835 /* GOST key exchange message creation */
2836 EVP_PKEY_CTX *pkey_ctx;
2839 unsigned int md_len;
2840 unsigned char shared_ukm[32], tmp[256];
2841 EVP_MD_CTX *ukm_hash;
2845 pms = OPENSSL_malloc(pmslen);
2850 * Get server sertificate PKEY and create ctx from it
2852 peer_cert = s->session->peer;
2854 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2855 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2859 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2860 X509_get_pubkey(peer_cert), NULL);
2862 * If we have send a certificate, and certificate key
2864 * * parameters match those of server certificate, use
2865 * certificate key for key exchange
2868 /* Otherwise, generate ephemeral key pair */
2870 EVP_PKEY_encrypt_init(pkey_ctx);
2871 /* Generate session key */
2872 if (RAND_bytes(pms, pmslen) <= 0) {
2873 EVP_PKEY_CTX_free(pkey_ctx);
2874 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2875 ERR_R_INTERNAL_ERROR);
2879 * If we have client certificate, use its secret as peer key
2881 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2882 if (EVP_PKEY_derive_set_peer
2883 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2885 * If there was an error - just ignore it. Ephemeral key
2892 * Compute shared IV and store it in algorithm-specific context
2895 ukm_hash = EVP_MD_CTX_create();
2896 EVP_DigestInit(ukm_hash,
2897 EVP_get_digestbynid(NID_id_GostR3411_94));
2898 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2900 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2902 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2903 EVP_MD_CTX_destroy(ukm_hash);
2904 if (EVP_PKEY_CTX_ctrl
2905 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2907 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2911 /* Make GOST keytransport blob message */
2913 * Encapsulate it into sequence
2915 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2917 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2918 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2922 if (msglen >= 0x80) {
2924 *(p++) = msglen & 0xff;
2927 *(p++) = msglen & 0xff;
2930 memcpy(p, tmp, msglen);
2931 /* Check if pubkey from client certificate was used */
2932 if (EVP_PKEY_CTX_ctrl
2933 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2934 /* Set flag "skip certificate verify" */
2935 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2937 EVP_PKEY_CTX_free(pkey_ctx);
2938 EVP_PKEY_free(pub_key);
2941 #ifndef OPENSSL_NO_SRP
2942 else if (alg_k & SSL_kSRP) {
2943 if (s->srp_ctx.A != NULL) {
2944 /* send off the data */
2945 n = BN_num_bytes(s->srp_ctx.A);
2947 BN_bn2bin(s->srp_ctx.A, p);
2950 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2951 ERR_R_INTERNAL_ERROR);
2954 OPENSSL_free(s->session->srp_username);
2955 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2956 if (s->session->srp_username == NULL) {
2957 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2958 ERR_R_MALLOC_FAILURE);
2964 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2965 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2969 #ifndef OPENSSL_NO_PSK
2973 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2974 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2975 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2979 s->s3->tmp.pms = pms;
2980 s->s3->tmp.pmslen = pmslen;
2984 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2985 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2987 OPENSSL_clear_free(pms, pmslen);
2988 s->s3->tmp.pms = NULL;
2989 #ifndef OPENSSL_NO_EC
2990 BN_CTX_free(bn_ctx);
2991 OPENSSL_free(encodedPoint);
2992 EC_KEY_free(clnt_ecdh);
2993 EVP_PKEY_free(srvr_pub_pkey);
2995 #ifndef OPENSSL_NO_PSK
2996 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2997 s->s3->tmp.psk = NULL;
2999 statem_set_error(s);
3003 int tls_client_key_exchange_post_work(SSL *s)
3005 unsigned char *pms = NULL;
3008 #ifndef OPENSSL_NO_SRP
3010 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3011 if (!srp_generate_client_master_secret(s)) {
3012 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
3013 ERR_R_INTERNAL_ERROR);
3019 pms = s->s3->tmp.pms;
3020 pmslen = s->s3->tmp.pmslen;
3022 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
3023 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3024 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
3027 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
3028 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3029 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
3034 OPENSSL_clear_free(pms, pmslen);
3035 s->s3->tmp.pms = NULL;
3039 int ssl3_send_client_verify(SSL *s)
3041 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3042 if(tls_construct_client_verify(s) == 0)
3045 s->state = SSL3_ST_CW_CERT_VRFY_B;
3048 /* SSL3_ST_CW_CERT_VRFY_B */
3049 return ssl_do_write(s);
3051 s->state = SSL_ST_ERR;
3056 int tls_construct_client_verify(SSL *s)
3059 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3061 EVP_PKEY_CTX *pctx = NULL;
3067 EVP_MD_CTX_init(&mctx);
3069 p = ssl_handshake_start(s);
3070 pkey = s->cert->key->privatekey;
3071 /* Create context from key and test if sha1 is allowed as digest */
3072 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3073 EVP_PKEY_sign_init(pctx);
3074 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3075 if (!SSL_USE_SIGALGS(s))
3076 s->method->ssl3_enc->cert_verify_mac(s,
3079 [MD5_DIGEST_LENGTH]));
3084 * For TLS v1.2 send signature algorithm and signature using agreed
3085 * digest and cached handshake records.
3087 if (SSL_USE_SIGALGS(s)) {
3090 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3091 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3092 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3093 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3098 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3101 if (!EVP_SignInit_ex(&mctx, md, NULL)
3102 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3103 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3104 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
3109 /* Digest cached records and discard handshake buffer */
3110 if (!ssl3_digest_cached_records(s, 0))
3113 #ifndef OPENSSL_NO_RSA
3114 if (pkey->type == EVP_PKEY_RSA) {
3115 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3116 if (RSA_sign(NID_md5_sha1, data,
3117 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3118 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3119 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
3126 #ifndef OPENSSL_NO_DSA
3127 if (pkey->type == EVP_PKEY_DSA) {
3128 if (!DSA_sign(pkey->save_type,
3129 &(data[MD5_DIGEST_LENGTH]),
3130 SHA_DIGEST_LENGTH, &(p[2]),
3131 (unsigned int *)&j, pkey->pkey.dsa)) {
3132 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
3139 #ifndef OPENSSL_NO_EC
3140 if (pkey->type == EVP_PKEY_EC) {
3141 if (!ECDSA_sign(pkey->save_type,
3142 &(data[MD5_DIGEST_LENGTH]),
3143 SHA_DIGEST_LENGTH, &(p[2]),
3144 (unsigned int *)&j, pkey->pkey.ec)) {
3145 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3152 if (pkey->type == NID_id_GostR3410_2001) {
3153 unsigned char signbuf[64];
3155 size_t sigsize = 64;
3156 s->method->ssl3_enc->cert_verify_mac(s,
3157 NID_id_GostR3411_94, data);
3158 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3159 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3162 for (i = 63, j = 0; i >= 0; j++, i--) {
3163 p[2 + j] = signbuf[i];
3168 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3171 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3172 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3176 EVP_MD_CTX_cleanup(&mctx);
3177 EVP_PKEY_CTX_free(pctx);
3180 EVP_MD_CTX_cleanup(&mctx);
3181 EVP_PKEY_CTX_free(pctx);
3186 * Check a certificate can be used for client authentication. Currently check
3187 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3188 * certificates can be used and optionally checks suitability for Suite B.
3190 static int ssl3_check_client_certificate(SSL *s)
3192 unsigned long alg_k;
3193 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3195 /* If no suitable signature algorithm can't use certificate */
3196 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3199 * If strict mode check suitability of chain before using it. This also
3200 * adjusts suite B digest if necessary.
3202 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3203 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3205 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3206 /* See if we can use client certificate for fixed DH */
3207 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3208 int i = s->session->peer_type;
3209 EVP_PKEY *clkey = NULL, *spkey = NULL;
3210 clkey = s->cert->key->privatekey;
3211 /* If client key not DH assume it can be used */
3212 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3215 spkey = X509_get_pubkey(s->session->peer);
3217 /* Compare server and client parameters */
3218 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3219 EVP_PKEY_free(spkey);
3223 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3229 int ssl3_send_client_certificate(SSL *s)
3231 enum WORK_STATE wst;
3233 if (s->state == SSL3_ST_CW_CERT_A || s->state == SSL3_ST_CW_CERT_B) {
3234 if (s->state == SSL3_ST_CW_CERT_A)
3235 wst = tls_prepare_client_certificate(s, WORK_MORE_A);
3237 wst = tls_prepare_client_certificate(s, WORK_MORE_B);
3238 if (wst == WORK_ERROR)
3240 if (wst == WORK_MORE_A)
3242 if (wst == WORK_MORE_B) {
3243 s->state = SSL3_ST_CW_CERT_B;
3247 s->state = SSL3_ST_CW_CERT_C;
3250 if (s->state == SSL3_ST_CW_CERT_B) {
3251 wst = tls_prepare_client_certificate(s, WORK_MORE_B);
3252 if (wst == WORK_ERROR)
3254 if (wst == WORK_MORE_A)
3257 /* Skip state C...it was entirely temporary in the original code */
3258 s->state = SSL3_ST_CW_CERT_D;
3261 if (tls_construct_client_certificate(s) == 0)
3264 return ssl_do_write(s);
3266 s->state = SSL_ST_ERR;
3271 enum WORK_STATE tls_prepare_client_certificate(SSL *s, enum WORK_STATE wst)
3274 EVP_PKEY *pkey = NULL;
3277 if (wst == WORK_MORE_A) {
3278 /* Let cert callback update client certificates if required */
3279 if (s->cert->cert_cb) {
3280 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3282 s->rwstate = SSL_X509_LOOKUP;
3286 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3287 statem_set_error(s);
3290 s->rwstate = SSL_NOTHING;
3292 if (ssl3_check_client_certificate(s))
3293 return WORK_FINISHED_CONTINUE;
3295 /* Fall through to WORK_MORE_B */
3299 /* We need to get a client cert */
3300 if (wst == WORK_MORE_B) {
3302 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3303 * return(-1); We then get retied later
3305 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3307 s->rwstate = SSL_X509_LOOKUP;
3310 s->rwstate = SSL_NOTHING;
3311 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3312 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3314 } else if (i == 1) {
3316 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
3317 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3321 EVP_PKEY_free(pkey);
3322 if (i && !ssl3_check_client_certificate(s))
3325 if (s->version == SSL3_VERSION) {
3326 s->s3->tmp.cert_req = 0;
3327 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3328 return WORK_FINISHED_CONTINUE;
3330 s->s3->tmp.cert_req = 2;
3331 if (!ssl3_digest_cached_records(s, 0)) {
3332 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3333 statem_set_error(s);
3339 return WORK_FINISHED_CONTINUE;
3342 /* Shouldn't ever get here */
3346 int tls_construct_client_certificate(SSL *s)
3348 if (!ssl3_output_cert_chain(s,
3349 (s->s3->tmp.cert_req ==
3350 2) ? NULL : s->cert->key)) {
3351 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3352 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3353 statem_set_error(s);
3360 #define has_bits(i,m) (((i)&(m)) == (m))
3362 int ssl3_check_cert_and_algorithm(SSL *s)
3366 EVP_PKEY *pkey = NULL;
3368 #ifndef OPENSSL_NO_RSA
3371 #ifndef OPENSSL_NO_DH
3374 int al = SSL_AD_HANDSHAKE_FAILURE;
3376 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3377 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3379 /* we don't have a certificate */
3380 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3382 #ifndef OPENSSL_NO_RSA
3383 rsa = s->s3->peer_rsa_tmp;
3385 #ifndef OPENSSL_NO_DH
3386 dh = s->s3->peer_dh_tmp;
3389 /* This is the passed certificate */
3391 idx = s->session->peer_type;
3392 #ifndef OPENSSL_NO_EC
3393 if (idx == SSL_PKEY_ECC) {
3394 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3396 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3401 } else if (alg_a & SSL_aECDSA) {
3402 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3403 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3405 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3406 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3410 pkey = X509_get_pubkey(s->session->peer);
3411 pkey_bits = EVP_PKEY_bits(pkey);
3412 i = X509_certificate_type(s->session->peer, pkey);
3413 EVP_PKEY_free(pkey);
3415 /* Check that we have a certificate if we require one */
3416 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3417 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3418 SSL_R_MISSING_RSA_SIGNING_CERT);
3421 #ifndef OPENSSL_NO_DSA
3422 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3423 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3424 SSL_R_MISSING_DSA_SIGNING_CERT);
3428 #ifndef OPENSSL_NO_RSA
3429 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3430 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3431 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3432 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3433 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3435 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3436 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3437 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3438 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3439 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3443 /* server key exchange is not allowed. */
3444 al = SSL_AD_INTERNAL_ERROR;
3445 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3452 #ifndef OPENSSL_NO_DH
3453 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3454 al = SSL_AD_INTERNAL_ERROR;
3455 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3457 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3458 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3459 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3460 SSL_R_MISSING_DH_RSA_CERT);
3463 # ifndef OPENSSL_NO_DSA
3464 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3465 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3466 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3467 SSL_R_MISSING_DH_DSA_CERT);
3473 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3474 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3475 #ifndef OPENSSL_NO_RSA
3476 if (alg_k & SSL_kRSA) {
3478 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3479 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3481 } else if (RSA_bits(rsa) >
3482 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3483 /* We have a temporary RSA key but it's too large. */
3484 al = SSL_AD_EXPORT_RESTRICTION;
3485 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3486 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3491 #ifndef OPENSSL_NO_DH
3492 if (alg_k & SSL_kDHE) {
3494 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3495 /* We have a temporary DH key but it's too large. */
3496 al = SSL_AD_EXPORT_RESTRICTION;
3497 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3498 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3501 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3502 /* The cert should have had an export DH key. */
3503 al = SSL_AD_EXPORT_RESTRICTION;
3504 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3505 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3510 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3511 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3517 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3522 * Normally, we can tell if the server is resuming the session from
3523 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3524 * message after the ServerHello to determine if the server is resuming.
3525 * Therefore, we allow EAP-FAST to peek ahead.
3526 * ssl3_check_change returns 1 if we are resuming from an external
3527 * pre-shared secret, we have a "ticket" and the next server message
3528 * is CCS; and 0 otherwise. It returns -1 upon an error.
3532 * TODO: No longer required. Temporarily kept during state machine development
3533 * To be deleted by later commits
3535 static int ssl3_check_change(SSL *s)
3539 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3540 !s->session->tlsext_tick)
3544 * This function is called when we might get a Certificate message instead,
3545 * so permit appropriate message length.
3546 * We ignore the return value as we're only interested in the message type
3547 * and not its length.
3549 s->method->ssl_get_message(s,
3552 -1, s->max_cert_list, &ok);
3557 s->s3->tmp.reuse_message = 1;
3559 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC)
3566 #ifndef OPENSSL_NO_NEXTPROTONEG
3567 int ssl3_send_next_proto(SSL *s)
3569 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3570 if (tls_construct_next_proto(s) == 0) {
3571 s->state = SSL_ST_ERR;
3574 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3577 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3580 int tls_construct_next_proto(SSL *s)
3582 unsigned int len, padding_len;
3585 len = s->next_proto_negotiated_len;
3586 padding_len = 32 - ((len + 2) % 32);
3587 d = (unsigned char *)s->init_buf->data;
3589 memcpy(d + 5, s->next_proto_negotiated, len);
3590 d[5 + len] = padding_len;
3591 memset(d + 6 + len, 0, padding_len);
3592 *(d++) = SSL3_MT_NEXT_PROTO;
3593 l2n3(2 + len + padding_len, d);
3594 s->init_num = 4 + 2 + len + padding_len;
3601 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3604 #ifndef OPENSSL_NO_ENGINE
3605 if (s->ctx->client_cert_engine) {
3606 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3607 SSL_get_client_CA_list(s),
3608 px509, ppkey, NULL, NULL, NULL);
3613 if (s->ctx->client_cert_cb)
3614 i = s->ctx->client_cert_cb(s, px509, ppkey);
3618 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3624 int empty_reneg_info_scsv = !s->renegotiate;
3625 /* Set disabled masks for this session */
3626 ssl_set_client_disabled(s);
3632 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3633 c = sk_SSL_CIPHER_value(sk, i);
3634 /* Skip disabled ciphers */
3635 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3637 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3638 if (c->id == SSL3_CK_SCSV) {
3639 if (!empty_reneg_info_scsv)
3642 empty_reneg_info_scsv = 0;
3645 j = s->method->put_cipher_by_char(c, p);
3649 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3653 if (empty_reneg_info_scsv) {
3654 static SSL_CIPHER scsv = {
3655 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3657 j = s->method->put_cipher_by_char(&scsv, p);
3659 #ifdef OPENSSL_RI_DEBUG
3661 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3664 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3665 static SSL_CIPHER scsv = {
3666 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3668 j = s->method->put_cipher_by_char(&scsv, p);