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 "kssl_lcl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
160 # include <openssl/fips.h>
162 #ifndef OPENSSL_NO_DH
163 # include <openssl/dh.h>
165 #include <openssl/bn.h>
166 #ifndef OPENSSL_NO_ENGINE
167 # include <openssl/engine.h>
170 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
171 #ifndef OPENSSL_NO_TLSEXT
172 static int ssl3_check_finished(SSL *s);
175 #ifndef OPENSSL_NO_SSL3_METHOD
176 static const SSL_METHOD *ssl3_get_client_method(int ver)
178 if (ver == SSL3_VERSION)
179 return (SSLv3_client_method());
184 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
185 ssl_undefined_function,
186 ssl3_connect, ssl3_get_client_method)
188 int ssl3_connect(SSL *s)
191 unsigned long Time = (unsigned long)time(NULL);
192 void (*cb) (const SSL *ssl, int type, int val) = NULL;
194 int new_state, state, skip = 0;
196 RAND_add(&Time, sizeof(Time), 0);
200 if (s->info_callback != NULL)
201 cb = s->info_callback;
202 else if (s->ctx->info_callback != NULL)
203 cb = s->ctx->info_callback;
206 if (!SSL_in_init(s) || SSL_in_before(s))
209 #ifndef OPENSSL_NO_HEARTBEATS
211 * If we're awaiting a HeartbeatResponse, pretend we already got and
212 * don't await it anymore, because Heartbeats don't make sense during
215 if (s->tlsext_hb_pending) {
216 s->tlsext_hb_pending = 0;
225 case SSL_ST_RENEGOTIATE:
227 s->state = SSL_ST_CONNECT;
228 s->ctx->stats.sess_connect_renegotiate++;
232 case SSL_ST_BEFORE | SSL_ST_CONNECT:
233 case SSL_ST_OK | SSL_ST_CONNECT:
237 cb(s, SSL_CB_HANDSHAKE_START, 1);
239 if ((s->version & 0xff00) != 0x0300) {
240 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
245 /* s->version=SSL3_VERSION; */
246 s->type = SSL_ST_CONNECT;
248 if (s->init_buf == NULL) {
249 if ((buf = BUF_MEM_new()) == NULL) {
253 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
261 if (!ssl3_setup_buffers(s)) {
266 /* setup buffing BIO */
267 if (!ssl_init_wbio_buffer(s, 0)) {
272 /* don't push the buffering BIO quite yet */
274 ssl3_init_finished_mac(s);
276 s->state = SSL3_ST_CW_CLNT_HELLO_A;
277 s->ctx->stats.sess_connect++;
279 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
281 * Should have been reset by ssl3_get_finished, too.
283 s->s3->change_cipher_spec = 0;
286 case SSL3_ST_CW_CLNT_HELLO_A:
287 case SSL3_ST_CW_CLNT_HELLO_B:
290 ret = ssl3_client_hello(s);
293 s->state = SSL3_ST_CR_SRVR_HELLO_A;
296 /* turn on buffering for the next lot of output */
297 if (s->bbio != s->wbio)
298 s->wbio = BIO_push(s->bbio, s->wbio);
302 case SSL3_ST_CR_SRVR_HELLO_A:
303 case SSL3_ST_CR_SRVR_HELLO_B:
304 ret = ssl3_get_server_hello(s);
309 s->state = SSL3_ST_CR_FINISHED_A;
310 #ifndef OPENSSL_NO_TLSEXT
311 if (s->tlsext_ticket_expected) {
312 /* receive renewed session ticket */
313 s->state = SSL3_ST_CR_SESSION_TICKET_A;
317 s->state = SSL3_ST_CR_CERT_A;
321 case SSL3_ST_CR_CERT_A:
322 case SSL3_ST_CR_CERT_B:
323 #ifndef OPENSSL_NO_TLSEXT
324 /* Noop (ret = 0) for everything but EAP-FAST. */
325 ret = ssl3_check_finished(s);
330 s->state = SSL3_ST_CR_FINISHED_A;
335 /* Check if it is anon DH/ECDH, SRP auth */
339 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
340 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
341 ret = ssl3_get_server_certificate(s);
344 #ifndef OPENSSL_NO_TLSEXT
345 if (s->tlsext_status_expected)
346 s->state = SSL3_ST_CR_CERT_STATUS_A;
348 s->state = SSL3_ST_CR_KEY_EXCH_A;
351 s->state = SSL3_ST_CR_KEY_EXCH_A;
357 s->state = SSL3_ST_CR_KEY_EXCH_A;
362 case SSL3_ST_CR_KEY_EXCH_A:
363 case SSL3_ST_CR_KEY_EXCH_B:
364 ret = ssl3_get_key_exchange(s);
367 s->state = SSL3_ST_CR_CERT_REQ_A;
371 * at this point we check that we have the required stuff from
374 if (!ssl3_check_cert_and_algorithm(s)) {
380 case SSL3_ST_CR_CERT_REQ_A:
381 case SSL3_ST_CR_CERT_REQ_B:
382 ret = ssl3_get_certificate_request(s);
385 s->state = SSL3_ST_CR_SRVR_DONE_A;
389 case SSL3_ST_CR_SRVR_DONE_A:
390 case SSL3_ST_CR_SRVR_DONE_B:
391 ret = ssl3_get_server_done(s);
394 #ifndef OPENSSL_NO_SRP
395 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
396 if ((ret = SRP_Calc_A_param(s)) <= 0) {
397 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
398 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
403 if (s->s3->tmp.cert_req)
404 s->state = SSL3_ST_CW_CERT_A;
406 s->state = SSL3_ST_CW_KEY_EXCH_A;
411 case SSL3_ST_CW_CERT_A:
412 case SSL3_ST_CW_CERT_B:
413 case SSL3_ST_CW_CERT_C:
414 case SSL3_ST_CW_CERT_D:
415 ret = ssl3_send_client_certificate(s);
418 s->state = SSL3_ST_CW_KEY_EXCH_A;
422 case SSL3_ST_CW_KEY_EXCH_A:
423 case SSL3_ST_CW_KEY_EXCH_B:
424 ret = ssl3_send_client_key_exchange(s);
428 * EAY EAY EAY need to check for DH fix cert sent back
431 * For TLS, cert_req is set to 2, so a cert chain of nothing is
432 * sent, but no verify packet is sent
435 * XXX: For now, we do not support client authentication in ECDH
436 * cipher suites with ECDH (rather than ECDSA) certificates. We
437 * need to skip the certificate verify message when client's
438 * ECDH public key is sent inside the client certificate.
440 if (s->s3->tmp.cert_req == 1) {
441 s->state = SSL3_ST_CW_CERT_VRFY_A;
443 s->state = SSL3_ST_CW_CHANGE_A;
445 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
446 s->state = SSL3_ST_CW_CHANGE_A;
452 case SSL3_ST_CW_CERT_VRFY_A:
453 case SSL3_ST_CW_CERT_VRFY_B:
454 ret = ssl3_send_client_verify(s);
457 s->state = SSL3_ST_CW_CHANGE_A;
461 case SSL3_ST_CW_CHANGE_A:
462 case SSL3_ST_CW_CHANGE_B:
463 ret = ssl3_send_change_cipher_spec(s,
465 SSL3_ST_CW_CHANGE_B);
469 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
470 s->state = SSL3_ST_CW_FINISHED_A;
472 if (s->s3->next_proto_neg_seen)
473 s->state = SSL3_ST_CW_NEXT_PROTO_A;
475 s->state = SSL3_ST_CW_FINISHED_A;
479 s->session->cipher = s->s3->tmp.new_cipher;
480 #ifdef OPENSSL_NO_COMP
481 s->session->compress_meth = 0;
483 if (s->s3->tmp.new_compression == NULL)
484 s->session->compress_meth = 0;
486 s->session->compress_meth = s->s3->tmp.new_compression->id;
488 if (!s->method->ssl3_enc->setup_key_block(s)) {
493 if (!s->method->ssl3_enc->change_cipher_state(s,
494 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
502 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
503 case SSL3_ST_CW_NEXT_PROTO_A:
504 case SSL3_ST_CW_NEXT_PROTO_B:
505 ret = ssl3_send_next_proto(s);
508 s->state = SSL3_ST_CW_FINISHED_A;
512 case SSL3_ST_CW_FINISHED_A:
513 case SSL3_ST_CW_FINISHED_B:
514 ret = ssl3_send_finished(s,
515 SSL3_ST_CW_FINISHED_A,
516 SSL3_ST_CW_FINISHED_B,
518 ssl3_enc->client_finished_label,
520 ssl3_enc->client_finished_label_len);
523 s->state = SSL3_ST_CW_FLUSH;
526 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
528 s->s3->tmp.next_state = SSL_ST_OK;
529 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
530 s->state = SSL_ST_OK;
531 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
532 s->s3->delay_buf_pop_ret = 0;
535 #ifndef OPENSSL_NO_TLSEXT
537 * Allow NewSessionTicket if ticket expected
539 if (s->tlsext_ticket_expected)
540 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
544 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
549 #ifndef OPENSSL_NO_TLSEXT
550 case SSL3_ST_CR_SESSION_TICKET_A:
551 case SSL3_ST_CR_SESSION_TICKET_B:
552 ret = ssl3_get_new_session_ticket(s);
555 s->state = SSL3_ST_CR_FINISHED_A;
559 case SSL3_ST_CR_CERT_STATUS_A:
560 case SSL3_ST_CR_CERT_STATUS_B:
561 ret = ssl3_get_cert_status(s);
564 s->state = SSL3_ST_CR_KEY_EXCH_A;
569 case SSL3_ST_CR_FINISHED_A:
570 case SSL3_ST_CR_FINISHED_B:
571 if (!s->s3->change_cipher_spec)
572 s->s3->flags |= SSL3_FLAGS_CCS_OK;
573 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
574 SSL3_ST_CR_FINISHED_B);
579 s->state = SSL3_ST_CW_CHANGE_A;
581 s->state = SSL_ST_OK;
585 case SSL3_ST_CW_FLUSH:
586 s->rwstate = SSL_WRITING;
587 if (BIO_flush(s->wbio) <= 0) {
591 s->rwstate = SSL_NOTHING;
592 s->state = s->s3->tmp.next_state;
596 /* clean a few things up */
597 ssl3_cleanup_key_block(s);
599 if (s->init_buf != NULL) {
600 BUF_MEM_free(s->init_buf);
605 * If we are not 'joining' the last two packets, remove the
608 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
609 ssl_free_wbio_buffer(s);
610 /* else do it later in ssl3_write */
616 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
618 s->ctx->stats.sess_hit++;
622 s->handshake_func = ssl3_connect;
623 s->ctx->stats.sess_connect_good++;
626 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
632 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
638 /* did we do anything */
639 if (!s->s3->tmp.reuse_message && !skip) {
641 if ((ret = BIO_flush(s->wbio)) <= 0)
645 if ((cb != NULL) && (s->state != state)) {
646 new_state = s->state;
648 cb(s, SSL_CB_CONNECT_LOOP, 1);
649 s->state = new_state;
659 cb(s, SSL_CB_CONNECT_EXIT, ret);
663 int ssl3_client_hello(SSL *s)
666 unsigned char *p, *d;
669 #ifndef OPENSSL_NO_COMP
674 buf = (unsigned char *)s->init_buf->data;
675 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
676 SSL_SESSION *sess = s->session;
677 if ((sess == NULL) || (sess->ssl_version != s->version) ||
678 #ifdef OPENSSL_NO_TLSEXT
679 !sess->session_id_length ||
682 * In the case of EAP-FAST, we can have a pre-shared
683 * "ticket" without a session ID.
685 (!sess->session_id_length && !sess->tlsext_tick) ||
687 (sess->not_resumable)) {
688 if (!ssl_get_new_session(s, 0))
691 /* else use the pre-loaded session */
693 p = s->s3->client_random;
695 if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
698 /* Do the message type and length last */
702 * version indicates the negotiated version: for example from
703 * an SSLv2/v3 compatible client hello). The client_version
704 * field is the maximum version we permit and it is also
705 * used in RSA encrypted premaster secrets. Some servers can
706 * choke if we initially report a higher version then
707 * renegotiate to a lower one in the premaster secret. This
708 * didn't happen with TLS 1.0 as most servers supported it
709 * but it can with TLS 1.1 or later if the server only supports
712 * Possible scenario with previous logic:
713 * 1. Client hello indicates TLS 1.2
714 * 2. Server hello says TLS 1.0
715 * 3. RSA encrypted premaster secret uses 1.2.
716 * 4. Handhaked proceeds using TLS 1.0.
717 * 5. Server sends hello request to renegotiate.
718 * 6. Client hello indicates TLS v1.0 as we now
719 * know that is maximum server supports.
720 * 7. Server chokes on RSA encrypted premaster secret
721 * containing version 1.0.
723 * For interoperability it should be OK to always use the
724 * maximum version we support in client hello and then rely
725 * on the checking of version to ensure the servers isn't
726 * being inconsistent: for example initially negotiating with
727 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
728 * client_version in client hello and not resetting it to
729 * the negotiated version.
732 *(p++) = s->version >> 8;
733 *(p++) = s->version & 0xff;
734 s->client_version = s->version;
736 *(p++) = s->client_version >> 8;
737 *(p++) = s->client_version & 0xff;
741 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
742 p += SSL3_RANDOM_SIZE;
748 i = s->session->session_id_length;
751 if (i > (int)sizeof(s->session->session_id)) {
752 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
755 memcpy(p, s->session->session_id, i);
759 /* Ciphers supported */
760 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
762 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
765 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
767 * Some servers hang if client hello > 256 bytes as hack workaround
768 * chop number of supported ciphers to keep it well below this if we
771 if (TLS1_get_version(s) >= TLS1_2_VERSION
772 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
773 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
779 #ifdef OPENSSL_NO_COMP
783 if ((s->options & SSL_OP_NO_COMPRESSION)
784 || !s->ctx->comp_methods)
787 j = sk_SSL_COMP_num(s->ctx->comp_methods);
789 for (i = 0; i < j; i++) {
790 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
794 *(p++) = 0; /* Add the NULL method */
796 #ifndef OPENSSL_NO_TLSEXT
798 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
799 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
803 ssl_add_clienthello_tlsext(s, p,
804 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
806 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
813 *(d++) = SSL3_MT_CLIENT_HELLO;
816 s->state = SSL3_ST_CW_CLNT_HELLO_B;
817 /* number of bytes to write */
818 s->init_num = p - buf;
822 /* SSL3_ST_CW_CLNT_HELLO_B */
823 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
828 int ssl3_get_server_hello(SSL *s)
830 STACK_OF(SSL_CIPHER) *sk;
832 unsigned char *p, *d;
836 #ifndef OPENSSL_NO_COMP
840 n = s->method->ssl_get_message(s,
841 SSL3_ST_CR_SRVR_HELLO_A,
842 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
847 if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) {
848 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
849 if (s->d1->send_cookie == 0) {
850 s->s3->tmp.reuse_message = 1;
852 } else { /* already sent a cookie */
854 al = SSL_AD_UNEXPECTED_MESSAGE;
855 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
861 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
862 al = SSL_AD_UNEXPECTED_MESSAGE;
863 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
867 d = p = (unsigned char *)s->init_msg;
869 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
870 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
871 s->version = (s->version & 0xff00) | p[1];
872 al = SSL_AD_PROTOCOL_VERSION;
877 /* load the server hello data */
878 /* load the server random */
879 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
880 p += SSL3_RANDOM_SIZE;
884 /* get the session-id */
887 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
888 al = SSL_AD_ILLEGAL_PARAMETER;
889 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
892 #ifndef OPENSSL_NO_TLSEXT
894 * Check if we can resume the session based on external pre-shared secret.
895 * EAP-FAST (RFC 4851) supports two types of session resumption.
896 * Resumption based on server-side state works with session IDs.
897 * Resumption based on pre-shared Protected Access Credentials (PACs)
898 * works by overriding the SessionTicket extension at the application
899 * layer, and does not send a session ID. (We do not know whether EAP-FAST
900 * servers would honour the session ID.) Therefore, the session ID alone
901 * is not a reliable indicator of session resumption, so we first check if
902 * we can resume, and later peek at the next handshake message to see if the
903 * server wants to resume.
905 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
906 s->session->tlsext_tick) {
907 SSL_CIPHER *pref_cipher = NULL;
908 s->session->master_key_length = sizeof(s->session->master_key);
909 if (s->tls_session_secret_cb(s, s->session->master_key,
910 &s->session->master_key_length,
912 s->tls_session_secret_cb_arg)) {
913 s->session->cipher = pref_cipher ?
914 pref_cipher : ssl_get_cipher_by_char(s, p + j);
916 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
917 al = SSL_AD_INTERNAL_ERROR;
921 #endif /* OPENSSL_NO_TLSEXT */
923 if (j != 0 && j == s->session->session_id_length
924 && memcmp(p, s->session->session_id, j) == 0) {
925 if (s->sid_ctx_length != s->session->sid_ctx_length
926 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
927 /* actually a client application bug */
928 al = SSL_AD_ILLEGAL_PARAMETER;
929 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
930 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
936 * If we were trying for session-id reuse but the server
937 * didn't echo the ID, make a new SSL_SESSION.
938 * In the case of EAP-FAST and PAC, we do not send a session ID,
939 * so the PAC-based session secret is always preserved. It'll be
940 * overwritten if the server refuses resumption.
942 if (s->session->session_id_length > 0) {
943 if (!ssl_get_new_session(s, 0)) {
944 al = SSL_AD_INTERNAL_ERROR;
948 s->session->session_id_length = j;
949 memcpy(s->session->session_id, p, j); /* j could be 0 */
952 c = ssl_get_cipher_by_char(s, p);
955 al = SSL_AD_ILLEGAL_PARAMETER;
956 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
959 /* TLS v1.2 only ciphersuites require v1.2 or later */
960 if ((c->algorithm_ssl & SSL_TLSV1_2) &&
961 (TLS1_get_version(s) < TLS1_2_VERSION)) {
962 al = SSL_AD_ILLEGAL_PARAMETER;
963 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
966 #ifndef OPENSSL_NO_SRP
967 if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) &&
968 !(s->srp_ctx.srp_Mask & SSL_kSRP)) {
969 al = SSL_AD_ILLEGAL_PARAMETER;
970 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
973 #endif /* OPENSSL_NO_SRP */
974 p += ssl_put_cipher_by_char(s, NULL, NULL);
976 sk = ssl_get_ciphers_by_id(s);
977 i = sk_SSL_CIPHER_find(sk, c);
979 /* we did not say we would use this cipher */
980 al = SSL_AD_ILLEGAL_PARAMETER;
981 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
986 * Depending on the session caching (internal/external), the cipher
987 * and/or cipher_id values may not be set. Make sure that cipher_id is
988 * set and use it for comparison.
990 if (s->session->cipher)
991 s->session->cipher_id = s->session->cipher->id;
992 if (s->hit && (s->session->cipher_id != c->id)) {
993 /* Workaround is now obsolete */
995 if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
998 al = SSL_AD_ILLEGAL_PARAMETER;
999 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1000 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1004 s->s3->tmp.new_cipher = c;
1006 * Don't digest cached records if TLS v1.2: we may need them for client
1009 if (TLS1_get_version(s) < TLS1_2_VERSION
1010 && !ssl3_digest_cached_records(s)) {
1011 al = SSL_AD_INTERNAL_ERROR;
1014 /* lets get the compression algorithm */
1016 #ifdef OPENSSL_NO_COMP
1018 al = SSL_AD_ILLEGAL_PARAMETER;
1019 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1020 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1024 * If compression is disabled we'd better not try to resume a session
1025 * using compression.
1027 if (s->session->compress_meth != 0) {
1028 al = SSL_AD_INTERNAL_ERROR;
1029 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1034 if (s->hit && j != s->session->compress_meth) {
1035 al = SSL_AD_ILLEGAL_PARAMETER;
1036 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1037 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1042 else if (s->options & SSL_OP_NO_COMPRESSION) {
1043 al = SSL_AD_ILLEGAL_PARAMETER;
1044 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1047 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1049 if ((j != 0) && (comp == NULL)) {
1050 al = SSL_AD_ILLEGAL_PARAMETER;
1051 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1052 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1055 s->s3->tmp.new_compression = comp;
1059 #ifndef OPENSSL_NO_TLSEXT
1060 /* TLS extensions */
1061 if (s->version >= SSL3_VERSION) {
1062 if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) {
1063 /* 'al' set by ssl_parse_serverhello_tlsext */
1064 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1067 if (ssl_check_serverhello_tlsext(s) <= 0) {
1068 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1075 /* wrong packet length */
1076 al = SSL_AD_DECODE_ERROR;
1077 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1083 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1088 int ssl3_get_server_certificate(SSL *s)
1090 int al, i, ok, ret = -1;
1091 unsigned long n, nc, llen, l;
1093 const unsigned char *q, *p;
1095 STACK_OF(X509) *sk = NULL;
1097 EVP_PKEY *pkey = NULL;
1098 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1101 n = s->method->ssl_get_message(s,
1104 -1, s->max_cert_list, &ok);
1109 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1110 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1111 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1112 s->s3->tmp.reuse_message = 1;
1116 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1117 al = SSL_AD_UNEXPECTED_MESSAGE;
1118 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1121 p = d = (unsigned char *)s->init_msg;
1123 if ((sk = sk_X509_new_null()) == NULL) {
1124 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1129 if (llen + 3 != n) {
1130 al = SSL_AD_DECODE_ERROR;
1131 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1134 for (nc = 0; nc < llen;) {
1136 if ((l + nc + 3) > llen) {
1137 al = SSL_AD_DECODE_ERROR;
1138 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1139 SSL_R_CERT_LENGTH_MISMATCH);
1144 x = d2i_X509(NULL, &q, l);
1146 al = SSL_AD_BAD_CERTIFICATE;
1147 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1151 al = SSL_AD_DECODE_ERROR;
1152 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1153 SSL_R_CERT_LENGTH_MISMATCH);
1156 if (!sk_X509_push(sk, x)) {
1157 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1165 i = ssl_verify_cert_chain(s, sk);
1166 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1167 #ifndef OPENSSL_NO_KRB5
1168 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1169 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1170 #endif /* OPENSSL_NO_KRB5 */
1172 al = ssl_verify_alarm_type(s->verify_result);
1173 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1174 SSL_R_CERTIFICATE_VERIFY_FAILED);
1177 ERR_clear_error(); /* but we keep s->verify_result */
1179 sc = ssl_sess_cert_new();
1183 if (s->session->sess_cert)
1184 ssl_sess_cert_free(s->session->sess_cert);
1185 s->session->sess_cert = sc;
1187 sc->cert_chain = sk;
1189 * Inconsistency alert: cert_chain does include the peer's certificate,
1190 * which we don't include in s3_srvr.c
1192 x = sk_X509_value(sk, 0);
1195 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1198 pkey = X509_get_pubkey(x);
1200 /* VRS: allow null cert if auth == KRB5 */
1201 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1202 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1206 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1207 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1208 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1209 s->s3->tmp.new_cipher->name,
1210 s->s3->tmp.new_cipher->algorithm_mkey,
1211 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1212 #endif /* KSSL_DEBUG */
1214 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1217 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1218 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1222 i = ssl_cert_type(x, pkey);
1223 if (need_cert && i < 0) {
1226 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1227 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1232 sc->peer_cert_type = i;
1233 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1235 * Why would the following ever happen? We just created sc a couple
1238 if (sc->peer_pkeys[i].x509 != NULL)
1239 X509_free(sc->peer_pkeys[i].x509);
1240 sc->peer_pkeys[i].x509 = x;
1241 sc->peer_key = &(sc->peer_pkeys[i]);
1243 if (s->session->peer != NULL)
1244 X509_free(s->session->peer);
1245 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1246 s->session->peer = x;
1248 sc->peer_cert_type = i;
1249 sc->peer_key = NULL;
1251 if (s->session->peer != NULL)
1252 X509_free(s->session->peer);
1253 s->session->peer = NULL;
1255 s->session->verify_result = s->verify_result;
1262 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1265 EVP_PKEY_free(pkey);
1267 sk_X509_pop_free(sk, X509_free);
1271 int ssl3_get_key_exchange(SSL *s)
1273 #ifndef OPENSSL_NO_RSA
1274 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1277 unsigned char *param, *p;
1279 long i, param_len, n, alg_k, alg_a;
1280 EVP_PKEY *pkey = NULL;
1281 const EVP_MD *md = NULL;
1282 #ifndef OPENSSL_NO_RSA
1285 #ifndef OPENSSL_NO_DH
1288 #ifndef OPENSSL_NO_ECDH
1289 EC_KEY *ecdh = NULL;
1290 BN_CTX *bn_ctx = NULL;
1291 EC_POINT *srvr_ecpoint = NULL;
1293 int encoded_pt_len = 0;
1296 EVP_MD_CTX_init(&md_ctx);
1299 * use same message size as in ssl3_get_certificate_request() as
1300 * ServerKeyExchange message may be skipped
1302 n = s->method->ssl_get_message(s,
1303 SSL3_ST_CR_KEY_EXCH_A,
1304 SSL3_ST_CR_KEY_EXCH_B,
1305 -1, s->max_cert_list, &ok);
1309 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1311 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1313 * Can't skip server key exchange if this is an ephemeral
1316 if (alg_k & (SSL_kEDH | SSL_kEECDH)) {
1317 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1318 al = SSL_AD_UNEXPECTED_MESSAGE;
1321 #ifndef OPENSSL_NO_PSK
1323 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1324 * identity hint is sent. Set session->sess_cert anyway to avoid
1327 if (alg_k & SSL_kPSK) {
1328 s->session->sess_cert = ssl_sess_cert_new();
1329 if (s->ctx->psk_identity_hint)
1330 OPENSSL_free(s->ctx->psk_identity_hint);
1331 s->ctx->psk_identity_hint = NULL;
1334 s->s3->tmp.reuse_message = 1;
1338 param = p = (unsigned char *)s->init_msg;
1339 if (s->session->sess_cert != NULL) {
1340 #ifndef OPENSSL_NO_RSA
1341 if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1342 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1343 s->session->sess_cert->peer_rsa_tmp = NULL;
1346 #ifndef OPENSSL_NO_DH
1347 if (s->session->sess_cert->peer_dh_tmp) {
1348 DH_free(s->session->sess_cert->peer_dh_tmp);
1349 s->session->sess_cert->peer_dh_tmp = NULL;
1352 #ifndef OPENSSL_NO_ECDH
1353 if (s->session->sess_cert->peer_ecdh_tmp) {
1354 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1355 s->session->sess_cert->peer_ecdh_tmp = NULL;
1359 s->session->sess_cert = ssl_sess_cert_new();
1362 /* Total length of the parameters including the length prefix */
1365 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1367 al = SSL_AD_DECODE_ERROR;
1369 #ifndef OPENSSL_NO_PSK
1370 if (alg_k & SSL_kPSK) {
1371 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1374 if (param_len > n) {
1375 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1381 * Store PSK identity hint for later use, hint is used in
1382 * ssl3_send_client_key_exchange. Assume that the maximum length of
1383 * a PSK identity hint can be as long as the maximum length of a PSK
1386 if (i > PSK_MAX_IDENTITY_LEN) {
1387 al = SSL_AD_HANDSHAKE_FAILURE;
1388 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1391 if (i > n - param_len) {
1392 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1393 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1399 * If received PSK identity hint contains NULL characters, the hint
1400 * is truncated from the first NULL. p may not be ending with NULL,
1401 * so create a NULL-terminated string.
1403 memcpy(tmp_id_hint, p, i);
1404 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1405 if (s->ctx->psk_identity_hint != NULL)
1406 OPENSSL_free(s->ctx->psk_identity_hint);
1407 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1408 if (s->ctx->psk_identity_hint == NULL) {
1409 al = SSL_AD_HANDSHAKE_FAILURE;
1410 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1417 #endif /* !OPENSSL_NO_PSK */
1418 #ifndef OPENSSL_NO_SRP
1419 if (alg_k & SSL_kSRP) {
1421 if (param_len > n) {
1422 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1427 if (i > n - param_len) {
1428 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1433 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1434 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1439 if (2 > n - param_len) {
1440 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1447 if (i > n - param_len) {
1448 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1453 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1454 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1459 if (1 > n - param_len) {
1460 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1465 i = (unsigned int)(p[0]);
1468 if (i > n - param_len) {
1469 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1474 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1475 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1480 if (2 > n - param_len) {
1481 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1488 if (i > n - param_len) {
1489 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1494 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1495 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1501 if (!srp_verify_server_param(s, &al)) {
1502 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1506 /* We must check if there is a certificate */
1507 # ifndef OPENSSL_NO_RSA
1508 if (alg_a & SSL_aRSA)
1510 X509_get_pubkey(s->session->
1511 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1515 # ifndef OPENSSL_NO_DSA
1516 else if (alg_a & SSL_aDSS)
1518 X509_get_pubkey(s->session->
1519 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1523 #endif /* !OPENSSL_NO_SRP */
1524 #ifndef OPENSSL_NO_RSA
1525 if (alg_k & SSL_kRSA) {
1526 /* Temporary RSA keys only allowed in export ciphersuites */
1527 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1528 al = SSL_AD_UNEXPECTED_MESSAGE;
1529 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1532 if ((rsa = RSA_new()) == NULL) {
1533 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1538 if (param_len > n) {
1539 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1544 if (i > n - param_len) {
1545 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1550 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1551 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1556 if (2 > n - param_len) {
1557 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1564 if (i > n - param_len) {
1565 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1570 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1577 /* this should be because we are using an export cipher */
1578 if (alg_a & SSL_aRSA)
1580 X509_get_pubkey(s->session->
1581 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1583 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1586 s->session->sess_cert->peer_rsa_tmp = rsa;
1589 #else /* OPENSSL_NO_RSA */
1592 #ifndef OPENSSL_NO_DH
1593 else if (alg_k & SSL_kEDH) {
1594 if ((dh = DH_new()) == NULL) {
1595 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1600 if (param_len > n) {
1601 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1606 if (i > n - param_len) {
1607 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1612 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1613 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1618 if (2 > n - param_len) {
1619 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1626 if (i > n - param_len) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1632 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1633 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1638 if (2 > n - param_len) {
1639 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1646 if (i > n - param_len) {
1647 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1652 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1653 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1659 # ifndef OPENSSL_NO_RSA
1660 if (alg_a & SSL_aRSA)
1662 X509_get_pubkey(s->session->
1663 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1667 # ifndef OPENSSL_NO_DSA
1668 else if (alg_a & SSL_aDSS)
1670 X509_get_pubkey(s->session->
1671 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1674 /* else anonymous DH, so no certificate or pkey. */
1676 s->session->sess_cert->peer_dh_tmp = dh;
1678 } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1679 al = SSL_AD_ILLEGAL_PARAMETER;
1680 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1681 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1684 #endif /* !OPENSSL_NO_DH */
1686 #ifndef OPENSSL_NO_ECDH
1687 else if (alg_k & SSL_kEECDH) {
1689 const EC_GROUP *group;
1691 if ((ecdh = EC_KEY_new()) == NULL) {
1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1697 * Extract elliptic curve parameters and the server's ephemeral ECDH
1698 * public key. Keep accumulating lengths of various components in
1699 * param_len and make sure it never exceeds n.
1703 * XXX: For now we only support named (not generic) curves and the
1704 * ECParameters in this case is just three bytes. We also need one
1705 * byte for the length of the encoded point
1708 if (param_len > n) {
1709 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1713 if ((*p != NAMED_CURVE_TYPE) ||
1714 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) {
1715 al = SSL_AD_INTERNAL_ERROR;
1716 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1717 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1721 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1722 if (ngroup == NULL) {
1723 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1726 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1727 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1730 EC_GROUP_free(ngroup);
1732 group = EC_KEY_get0_group(ecdh);
1734 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1735 (EC_GROUP_get_degree(group) > 163)) {
1736 al = SSL_AD_EXPORT_RESTRICTION;
1737 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1738 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1744 /* Next, get the encoded ECPoint */
1745 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1746 ((bn_ctx = BN_CTX_new()) == NULL)) {
1747 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1751 encoded_pt_len = *p; /* length of encoded point */
1754 if ((encoded_pt_len > n - param_len) ||
1755 (EC_POINT_oct2point(group, srvr_ecpoint,
1756 p, encoded_pt_len, bn_ctx) == 0)) {
1757 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1760 param_len += encoded_pt_len;
1763 p += encoded_pt_len;
1766 * The ECC/TLS specification does not mention the use of DSA to sign
1767 * ECParameters in the server key exchange message. We do support RSA
1771 # ifndef OPENSSL_NO_RSA
1772 else if (alg_a & SSL_aRSA)
1774 X509_get_pubkey(s->session->
1775 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1777 # ifndef OPENSSL_NO_ECDSA
1778 else if (alg_a & SSL_aECDSA)
1780 X509_get_pubkey(s->session->
1781 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1783 /* else anonymous ECDH, so no certificate or pkey. */
1784 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1785 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1787 BN_CTX_free(bn_ctx);
1789 EC_POINT_free(srvr_ecpoint);
1790 srvr_ecpoint = NULL;
1792 al = SSL_AD_UNEXPECTED_MESSAGE;
1793 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1796 #endif /* !OPENSSL_NO_ECDH */
1798 /* p points to the next byte, there are 'n' bytes left */
1800 /* if it was signed, check the signature */
1802 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
1805 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1809 sigalg = tls12_get_sigid(pkey);
1810 /* Should never happen */
1812 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1815 /* Check key type is consistent with signature */
1816 if (sigalg != (int)p[1]) {
1817 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1818 SSL_R_WRONG_SIGNATURE_TYPE);
1819 al = SSL_AD_DECODE_ERROR;
1822 md = tls12_get_hash(p[0]);
1824 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNKNOWN_DIGEST);
1828 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1836 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1841 j = EVP_PKEY_size(pkey);
1844 * Check signature length. If n is 0 then signature is empty
1846 if ((i != n) || (n > j) || (n <= 0)) {
1847 /* wrong packet length */
1848 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1851 #ifndef OPENSSL_NO_RSA
1852 if (pkey->type == EVP_PKEY_RSA
1853 && TLS1_get_version(s) < TLS1_2_VERSION) {
1859 for (num = 2; num > 0; num--) {
1860 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1861 EVP_DigestInit_ex(&md_ctx, (num == 2)
1862 ? s->ctx->md5 : s->ctx->sha1, NULL);
1863 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1865 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1867 EVP_DigestUpdate(&md_ctx, param, param_len);
1868 EVP_DigestFinal_ex(&md_ctx, q, &size);
1872 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1874 al = SSL_AD_DECRYPT_ERROR;
1875 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1880 al = SSL_AD_DECRYPT_ERROR;
1881 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1887 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1888 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1890 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1892 EVP_VerifyUpdate(&md_ctx, param, param_len);
1893 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1895 al = SSL_AD_DECRYPT_ERROR;
1896 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1901 /* aNULL, aSRP or kPSK do not need public keys */
1902 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1903 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1906 /* still data left over */
1908 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1912 EVP_PKEY_free(pkey);
1913 EVP_MD_CTX_cleanup(&md_ctx);
1916 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1918 EVP_PKEY_free(pkey);
1919 #ifndef OPENSSL_NO_RSA
1923 #ifndef OPENSSL_NO_DH
1927 #ifndef OPENSSL_NO_ECDH
1928 BN_CTX_free(bn_ctx);
1929 EC_POINT_free(srvr_ecpoint);
1933 EVP_MD_CTX_cleanup(&md_ctx);
1937 int ssl3_get_certificate_request(SSL *s)
1940 unsigned long n, nc, l;
1941 unsigned int llen, ctype_num, i;
1942 X509_NAME *xn = NULL;
1943 const unsigned char *p, *q;
1945 STACK_OF(X509_NAME) *ca_sk = NULL;
1947 n = s->method->ssl_get_message(s,
1948 SSL3_ST_CR_CERT_REQ_A,
1949 SSL3_ST_CR_CERT_REQ_B,
1950 -1, s->max_cert_list, &ok);
1955 s->s3->tmp.cert_req = 0;
1957 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1958 s->s3->tmp.reuse_message = 1;
1960 * If we get here we don't need any cached handshake records as we
1961 * wont be doing client auth.
1963 if (s->s3->handshake_buffer) {
1964 if (!ssl3_digest_cached_records(s))
1970 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1971 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1972 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
1976 /* TLS does not like anon-DH with client cert */
1977 if (s->version > SSL3_VERSION) {
1978 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1979 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1980 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1981 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1986 p = d = (unsigned char *)s->init_msg;
1988 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1989 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1993 /* get the certificate types */
1995 if (ctype_num > SSL3_CT_NUMBER)
1996 ctype_num = SSL3_CT_NUMBER;
1997 for (i = 0; i < ctype_num; i++)
1998 s->s3->tmp.ctype[i] = p[i];
2000 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
2003 * Check we have enough room for signature algorithms and following
2006 if ((unsigned long)(p - d + llen + 2) > n) {
2007 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2008 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2009 SSL_R_DATA_LENGTH_TOO_LONG);
2012 if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) {
2013 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2014 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2015 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2021 /* get the CA RDNs */
2026 out = fopen("/tmp/vsign.der", "w");
2027 fwrite(p, 1, llen, out);
2032 if ((unsigned long)(p - d + llen) != n) {
2033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2034 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2038 for (nc = 0; nc < llen;) {
2040 if ((l + nc + 2) > llen) {
2041 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2042 goto cont; /* netscape bugs */
2043 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2044 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2050 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2051 /* If netscape tolerance is on, ignore errors */
2052 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2055 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2056 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2062 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2063 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2064 SSL_R_CA_DN_LENGTH_MISMATCH);
2067 if (!sk_X509_NAME_push(ca_sk, xn)) {
2068 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2081 /* we should setup a certificate to return.... */
2082 s->s3->tmp.cert_req = 1;
2083 s->s3->tmp.ctype_num = ctype_num;
2084 if (s->s3->tmp.ca_names != NULL)
2085 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2086 s->s3->tmp.ca_names = ca_sk;
2092 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2096 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2098 return (X509_NAME_cmp(*a, *b));
2101 #ifndef OPENSSL_NO_TLSEXT
2102 int ssl3_get_new_session_ticket(SSL *s)
2104 int ok, al, ret = 0, ticklen;
2106 const unsigned char *p;
2109 n = s->method->ssl_get_message(s,
2110 SSL3_ST_CR_SESSION_TICKET_A,
2111 SSL3_ST_CR_SESSION_TICKET_B,
2112 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2118 /* need at least ticket_lifetime_hint + ticket length */
2119 al = SSL_AD_DECODE_ERROR;
2120 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2124 p = d = (unsigned char *)s->init_msg;
2125 n2l(p, s->session->tlsext_tick_lifetime_hint);
2127 /* ticket_lifetime_hint + ticket_length + ticket */
2128 if (ticklen + 6 != n) {
2129 al = SSL_AD_DECODE_ERROR;
2130 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2133 if (s->session->tlsext_tick) {
2134 OPENSSL_free(s->session->tlsext_tick);
2135 s->session->tlsext_ticklen = 0;
2137 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2138 if (!s->session->tlsext_tick) {
2139 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2142 memcpy(s->session->tlsext_tick, p, ticklen);
2143 s->session->tlsext_ticklen = ticklen;
2145 * There are two ways to detect a resumed ticket session. One is to set
2146 * an appropriate session ID and then the server must return a match in
2147 * ServerHello. This allows the normal client session ID matching to work
2148 * and we know much earlier that the ticket has been accepted. The
2149 * other way is to set zero length session ID when the ticket is
2150 * presented and rely on the handshake to determine session resumption.
2151 * We choose the former approach because this fits in with assumptions
2152 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2153 * SHA256 is disabled) hash of the ticket.
2155 EVP_Digest(p, ticklen,
2156 s->session->session_id, &s->session->session_id_length,
2157 # ifndef OPENSSL_NO_SHA256
2158 EVP_sha256(), NULL);
2165 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2170 int ssl3_get_cert_status(SSL *s)
2173 unsigned long resplen, n;
2174 const unsigned char *p;
2176 n = s->method->ssl_get_message(s,
2177 SSL3_ST_CR_CERT_STATUS_A,
2178 SSL3_ST_CR_CERT_STATUS_B,
2179 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2184 /* need at least status type + length */
2185 al = SSL_AD_DECODE_ERROR;
2186 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2189 p = (unsigned char *)s->init_msg;
2190 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2191 al = SSL_AD_DECODE_ERROR;
2192 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2196 if (resplen + 4 != n) {
2197 al = SSL_AD_DECODE_ERROR;
2198 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2201 if (s->tlsext_ocsp_resp)
2202 OPENSSL_free(s->tlsext_ocsp_resp);
2203 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2204 if (!s->tlsext_ocsp_resp) {
2205 al = SSL_AD_INTERNAL_ERROR;
2206 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2209 s->tlsext_ocsp_resplen = resplen;
2210 if (s->ctx->tlsext_status_cb) {
2212 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2214 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2215 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2219 al = SSL_AD_INTERNAL_ERROR;
2220 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2226 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2231 int ssl3_get_server_done(SSL *s)
2236 /* Second to last param should be very small, like 0 :-) */
2237 n = s->method->ssl_get_message(s,
2238 SSL3_ST_CR_SRVR_DONE_A,
2239 SSL3_ST_CR_SRVR_DONE_B,
2240 SSL3_MT_SERVER_DONE, 30, &ok);
2245 /* should contain no data */
2246 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2247 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2254 int ssl3_send_client_key_exchange(SSL *s)
2256 unsigned char *p, *d;
2258 unsigned long alg_k;
2259 #ifndef OPENSSL_NO_RSA
2261 EVP_PKEY *pkey = NULL;
2263 #ifndef OPENSSL_NO_KRB5
2265 #endif /* OPENSSL_NO_KRB5 */
2266 #ifndef OPENSSL_NO_ECDH
2267 EC_KEY *clnt_ecdh = NULL;
2268 const EC_POINT *srvr_ecpoint = NULL;
2269 EVP_PKEY *srvr_pub_pkey = NULL;
2270 unsigned char *encodedPoint = NULL;
2271 int encoded_pt_len = 0;
2272 BN_CTX *bn_ctx = NULL;
2275 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2276 d = (unsigned char *)s->init_buf->data;
2279 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2281 /* Fool emacs indentation */
2284 #ifndef OPENSSL_NO_RSA
2285 else if (alg_k & SSL_kRSA) {
2287 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2289 if (s->session->sess_cert == NULL) {
2291 * We should always have a server certificate with SSL_kRSA.
2293 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2294 ERR_R_INTERNAL_ERROR);
2298 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2299 rsa = s->session->sess_cert->peer_rsa_tmp;
2302 X509_get_pubkey(s->session->
2303 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2305 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2306 || (pkey->pkey.rsa == NULL)) {
2307 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2308 ERR_R_INTERNAL_ERROR);
2311 rsa = pkey->pkey.rsa;
2312 EVP_PKEY_free(pkey);
2315 tmp_buf[0] = s->client_version >> 8;
2316 tmp_buf[1] = s->client_version & 0xff;
2317 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2320 s->session->master_key_length = sizeof tmp_buf;
2323 /* Fix buf for TLS and beyond */
2324 if (s->version > SSL3_VERSION)
2326 n = RSA_public_encrypt(sizeof tmp_buf,
2327 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
2329 if (s->options & SSL_OP_PKCS1_CHECK_1)
2331 if (s->options & SSL_OP_PKCS1_CHECK_2)
2335 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2336 SSL_R_BAD_RSA_ENCRYPT);
2340 /* Fix buf for TLS and beyond */
2341 if (s->version > SSL3_VERSION) {
2346 s->session->master_key_length =
2347 s->method->ssl3_enc->generate_master_secret(s,
2349 session->master_key,
2352 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2355 #ifndef OPENSSL_NO_KRB5
2356 else if (alg_k & SSL_kKRB5) {
2357 krb5_error_code krb5rc;
2358 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2359 /* krb5_data krb5_ap_req; */
2360 krb5_data *enc_ticket;
2361 krb5_data authenticator, *authp = NULL;
2362 EVP_CIPHER_CTX ciph_ctx;
2363 const EVP_CIPHER *enc = NULL;
2364 unsigned char iv[EVP_MAX_IV_LENGTH];
2365 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2366 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2367 int padl, outl = sizeof(epms);
2369 EVP_CIPHER_CTX_init(&ciph_ctx);
2372 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2374 # endif /* KSSL_DEBUG */
2377 # ifdef KRB5SENDAUTH
2379 authp = &authenticator;
2380 # endif /* KRB5SENDAUTH */
2382 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2383 enc = kssl_map_enc(kssl_ctx->enctype);
2388 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2389 if (krb5rc && kssl_err.text)
2390 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2393 # endif /* KSSL_DEBUG */
2396 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2397 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2402 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2403 * in place of RFC 2712 KerberosWrapper, as in:
2405 * Send ticket (copy to *p, set n = length)
2406 * n = krb5_ap_req.length;
2407 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2408 * if (krb5_ap_req.data)
2409 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2411 * Now using real RFC 2712 KerberosWrapper
2412 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2413 * Note: 2712 "opaque" types are here replaced
2414 * with a 2-byte length followed by the value.
2416 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2417 * Where "xx xx" = length bytes. Shown here with
2418 * optional authenticator omitted.
2421 /* KerberosWrapper.Ticket */
2422 s2n(enc_ticket->length, p);
2423 memcpy(p, enc_ticket->data, enc_ticket->length);
2424 p += enc_ticket->length;
2425 n = enc_ticket->length + 2;
2427 /* KerberosWrapper.Authenticator */
2428 if (authp && authp->length) {
2429 s2n(authp->length, p);
2430 memcpy(p, authp->data, authp->length);
2432 n += authp->length + 2;
2438 s2n(0, p); /* null authenticator length */
2442 tmp_buf[0] = s->client_version >> 8;
2443 tmp_buf[1] = s->client_version & 0xff;
2444 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2448 * 20010420 VRS. Tried it this way; failed.
2449 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2450 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2451 * kssl_ctx->length);
2452 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2455 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2456 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2457 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
2459 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2461 if (outl > (int)sizeof epms) {
2462 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2463 ERR_R_INTERNAL_ERROR);
2466 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2468 /* KerberosWrapper.EncryptedPreMasterSecret */
2470 memcpy(p, epms, outl);
2474 s->session->master_key_length =
2475 s->method->ssl3_enc->generate_master_secret(s,
2477 session->master_key,
2481 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2482 OPENSSL_cleanse(epms, outl);
2485 #ifndef OPENSSL_NO_DH
2486 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2487 DH *dh_srvr, *dh_clnt;
2489 if (s->session->sess_cert == NULL) {
2490 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2491 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2492 SSL_R_UNEXPECTED_MESSAGE);
2496 if (s->session->sess_cert->peer_dh_tmp != NULL)
2497 dh_srvr = s->session->sess_cert->peer_dh_tmp;
2499 /* we get them from the cert */
2500 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2501 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2502 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
2506 /* generate a new random key */
2507 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2508 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2511 if (!DH_generate_key(dh_clnt)) {
2512 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2518 * use the 'p' output buffer for the DH key, but make sure to
2519 * clear it out afterwards
2522 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2525 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2530 /* generate master key from the result */
2531 s->session->master_key_length =
2532 s->method->ssl3_enc->generate_master_secret(s,
2534 session->master_key,
2539 /* send off the data */
2540 n = BN_num_bytes(dh_clnt->pub_key);
2542 BN_bn2bin(dh_clnt->pub_key, p);
2547 /* perhaps clean things up a bit EAY EAY EAY EAY */
2551 #ifndef OPENSSL_NO_ECDH
2552 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2553 const EC_GROUP *srvr_group = NULL;
2555 int ecdh_clnt_cert = 0;
2558 if (s->session->sess_cert == NULL) {
2559 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2560 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2561 SSL_R_UNEXPECTED_MESSAGE);
2566 * Did we send out the client's ECDH share for use in premaster
2567 * computation as part of client certificate? If so, set
2568 * ecdh_clnt_cert to 1.
2570 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2572 * XXX: For now, we do not support client
2573 * authentication using ECDH certificates.
2574 * To add such support, one needs to add
2575 * code that checks for appropriate
2576 * conditions and sets ecdh_clnt_cert to 1.
2577 * For example, the cert have an ECC
2578 * key on the same curve as the server's
2579 * and the key should be authorized for
2582 * One also needs to add code in ssl3_connect
2583 * to skip sending the certificate verify
2586 * if ((s->cert->key->privatekey != NULL) &&
2587 * (s->cert->key->privatekey->type ==
2588 * EVP_PKEY_EC) && ...)
2589 * ecdh_clnt_cert = 1;
2593 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2594 tkey = s->session->sess_cert->peer_ecdh_tmp;
2596 /* Get the Server Public Key from Cert */
2598 X509_get_pubkey(s->session->
2599 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2600 if ((srvr_pub_pkey == NULL)
2601 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2602 || (srvr_pub_pkey->pkey.ec == NULL)) {
2603 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2604 ERR_R_INTERNAL_ERROR);
2608 tkey = srvr_pub_pkey->pkey.ec;
2611 srvr_group = EC_KEY_get0_group(tkey);
2612 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2614 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2615 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2616 ERR_R_INTERNAL_ERROR);
2620 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2621 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2622 ERR_R_MALLOC_FAILURE);
2626 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2627 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2630 if (ecdh_clnt_cert) {
2632 * Reuse key info from our certificate We only need our
2633 * private key to perform the ECDH computation.
2635 const BIGNUM *priv_key;
2636 tkey = s->cert->key->privatekey->pkey.ec;
2637 priv_key = EC_KEY_get0_private_key(tkey);
2638 if (priv_key == NULL) {
2639 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2640 ERR_R_MALLOC_FAILURE);
2643 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2644 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2648 /* Generate a new ECDH key pair */
2649 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2650 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2657 * use the 'p' output buffer for the ECDH key, but make sure to
2658 * clear it out afterwards
2661 field_size = EC_GROUP_get_degree(srvr_group);
2662 if (field_size <= 0) {
2663 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2666 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
2669 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2673 /* generate master key from the result */
2674 s->session->master_key_length =
2675 s->method->ssl3_enc->generate_master_secret(s,
2677 session->master_key,
2680 memset(p, 0, n); /* clean up */
2682 if (ecdh_clnt_cert) {
2683 /* Send empty client key exch message */
2687 * First check the size of encoding and allocate memory
2691 EC_POINT_point2oct(srvr_group,
2692 EC_KEY_get0_public_key(clnt_ecdh),
2693 POINT_CONVERSION_UNCOMPRESSED,
2696 encodedPoint = (unsigned char *)
2697 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2698 bn_ctx = BN_CTX_new();
2699 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2700 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2701 ERR_R_MALLOC_FAILURE);
2705 /* Encode the public key */
2706 n = EC_POINT_point2oct(srvr_group,
2707 EC_KEY_get0_public_key(clnt_ecdh),
2708 POINT_CONVERSION_UNCOMPRESSED,
2709 encodedPoint, encoded_pt_len, bn_ctx);
2711 *p = n; /* length of encoded point */
2712 /* Encoded point will be copied here */
2714 /* copy the point */
2715 memcpy((unsigned char *)p, encodedPoint, n);
2716 /* increment n to account for length field */
2720 /* Free allocated memory */
2721 BN_CTX_free(bn_ctx);
2722 if (encodedPoint != NULL)
2723 OPENSSL_free(encodedPoint);
2724 if (clnt_ecdh != NULL)
2725 EC_KEY_free(clnt_ecdh);
2726 EVP_PKEY_free(srvr_pub_pkey);
2728 #endif /* !OPENSSL_NO_ECDH */
2729 else if (alg_k & SSL_kGOST) {
2730 /* GOST key exchange message creation */
2731 EVP_PKEY_CTX *pkey_ctx;
2734 unsigned int md_len;
2736 unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
2737 EVP_MD_CTX *ukm_hash;
2741 * Get server sertificate PKEY and create ctx from it
2745 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2749 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2751 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2752 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2756 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2757 X509_get_pubkey(peer_cert), NULL);
2759 * If we have send a certificate, and certificate key
2761 * * parameters match those of server certificate, use
2762 * certificate key for key exchange
2765 /* Otherwise, generate ephemeral key pair */
2767 EVP_PKEY_encrypt_init(pkey_ctx);
2768 /* Generate session key */
2769 if (RAND_bytes(premaster_secret, 32) <= 0) {
2770 EVP_PKEY_CTX_free(pkey_ctx);
2774 * If we have client certificate, use its secret as peer key
2776 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2777 if (EVP_PKEY_derive_set_peer
2778 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2780 * If there was an error - just ignore it. Ephemeral key
2787 * Compute shared IV and store it in algorithm-specific context
2790 ukm_hash = EVP_MD_CTX_create();
2791 EVP_DigestInit(ukm_hash,
2792 EVP_get_digestbynid(NID_id_GostR3411_94));
2793 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2795 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2797 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2798 EVP_MD_CTX_destroy(ukm_hash);
2799 if (EVP_PKEY_CTX_ctrl
2800 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2802 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2806 /* Make GOST keytransport blob message */
2808 * Encapsulate it into sequence
2810 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2812 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
2814 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2818 if (msglen >= 0x80) {
2820 *(p++) = msglen & 0xff;
2823 *(p++) = msglen & 0xff;
2826 memcpy(p, tmp, msglen);
2827 /* Check if pubkey from client certificate was used */
2828 if (EVP_PKEY_CTX_ctrl
2829 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2830 /* Set flag "skip certificate verify" */
2831 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2833 EVP_PKEY_CTX_free(pkey_ctx);
2834 s->session->master_key_length =
2835 s->method->ssl3_enc->generate_master_secret(s,
2837 session->master_key,
2840 EVP_PKEY_free(pub_key);
2843 #ifndef OPENSSL_NO_SRP
2844 else if (alg_k & SSL_kSRP) {
2845 if (s->srp_ctx.A != NULL) {
2846 /* send off the data */
2847 n = BN_num_bytes(s->srp_ctx.A);
2849 BN_bn2bin(s->srp_ctx.A, p);
2852 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2853 ERR_R_INTERNAL_ERROR);
2856 if (s->session->srp_username != NULL)
2857 OPENSSL_free(s->session->srp_username);
2858 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2859 if (s->session->srp_username == NULL) {
2860 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2861 ERR_R_MALLOC_FAILURE);
2865 if ((s->session->master_key_length =
2866 SRP_generate_client_master_secret(s,
2867 s->session->master_key)) <
2869 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2870 ERR_R_INTERNAL_ERROR);
2875 #ifndef OPENSSL_NO_PSK
2876 else if (alg_k & SSL_kPSK) {
2878 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2879 * \0-terminated identity. The last byte is for us for simulating
2882 char identity[PSK_MAX_IDENTITY_LEN + 2];
2883 size_t identity_len;
2884 unsigned char *t = NULL;
2885 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2886 unsigned int pre_ms_len = 0, psk_len = 0;
2890 if (s->psk_client_callback == NULL) {
2891 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2892 SSL_R_PSK_NO_CLIENT_CB);
2896 memset(identity, 0, sizeof(identity));
2897 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2898 identity, sizeof(identity) - 1,
2900 sizeof(psk_or_pre_ms));
2901 if (psk_len > PSK_MAX_PSK_LEN) {
2902 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2903 ERR_R_INTERNAL_ERROR);
2905 } else if (psk_len == 0) {
2906 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2907 SSL_R_PSK_IDENTITY_NOT_FOUND);
2910 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2911 identity_len = strlen(identity);
2912 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2913 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2914 ERR_R_INTERNAL_ERROR);
2917 /* create PSK pre_master_secret */
2918 pre_ms_len = 2 + psk_len + 2 + psk_len;
2920 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2922 memset(t, 0, psk_len);
2926 if (s->session->psk_identity_hint != NULL)
2927 OPENSSL_free(s->session->psk_identity_hint);
2928 s->session->psk_identity_hint =
2929 BUF_strdup(s->ctx->psk_identity_hint);
2930 if (s->ctx->psk_identity_hint != NULL
2931 && s->session->psk_identity_hint == NULL) {
2932 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2933 ERR_R_MALLOC_FAILURE);
2937 if (s->session->psk_identity != NULL)
2938 OPENSSL_free(s->session->psk_identity);
2939 s->session->psk_identity = BUF_strdup(identity);
2940 if (s->session->psk_identity == NULL) {
2941 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2942 ERR_R_MALLOC_FAILURE);
2946 s->session->master_key_length =
2947 s->method->ssl3_enc->generate_master_secret(s,
2949 session->master_key,
2952 s2n(identity_len, p);
2953 memcpy(p, identity, identity_len);
2954 n = 2 + identity_len;
2957 OPENSSL_cleanse(identity, sizeof(identity));
2958 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2960 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2966 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2967 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2971 *(d++) = SSL3_MT_CLIENT_KEY_EXCHANGE;
2974 s->state = SSL3_ST_CW_KEY_EXCH_B;
2975 /* number of bytes to write */
2976 s->init_num = n + 4;
2980 /* SSL3_ST_CW_KEY_EXCH_B */
2981 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2983 #ifndef OPENSSL_NO_ECDH
2984 BN_CTX_free(bn_ctx);
2985 if (encodedPoint != NULL)
2986 OPENSSL_free(encodedPoint);
2987 if (clnt_ecdh != NULL)
2988 EC_KEY_free(clnt_ecdh);
2989 EVP_PKEY_free(srvr_pub_pkey);
2994 int ssl3_send_client_verify(SSL *s)
2996 unsigned char *p, *d;
2997 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2999 EVP_PKEY_CTX *pctx = NULL;
3005 EVP_MD_CTX_init(&mctx);
3007 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3008 d = (unsigned char *)s->init_buf->data;
3010 pkey = s->cert->key->privatekey;
3011 /* Create context from key and test if sha1 is allowed as digest */
3012 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3013 EVP_PKEY_sign_init(pctx);
3014 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3015 if (TLS1_get_version(s) < TLS1_2_VERSION)
3016 s->method->ssl3_enc->cert_verify_mac(s,
3019 [MD5_DIGEST_LENGTH]));
3024 * For TLS v1.2 send signature algorithm and signature using agreed
3025 * digest and cached handshake records.
3027 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3030 const EVP_MD *md = s->cert->key->digest;
3031 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3032 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3033 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3038 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3041 if (!EVP_SignInit_ex(&mctx, md, NULL)
3042 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3043 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3044 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3049 if (!ssl3_digest_cached_records(s))
3052 #ifndef OPENSSL_NO_RSA
3053 if (pkey->type == EVP_PKEY_RSA) {
3054 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3055 if (RSA_sign(NID_md5_sha1, data,
3056 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3057 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3058 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3065 #ifndef OPENSSL_NO_DSA
3066 if (pkey->type == EVP_PKEY_DSA) {
3067 if (!DSA_sign(pkey->save_type,
3068 &(data[MD5_DIGEST_LENGTH]),
3069 SHA_DIGEST_LENGTH, &(p[2]),
3070 (unsigned int *)&j, pkey->pkey.dsa)) {
3071 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3078 #ifndef OPENSSL_NO_ECDSA
3079 if (pkey->type == EVP_PKEY_EC) {
3080 if (!ECDSA_sign(pkey->save_type,
3081 &(data[MD5_DIGEST_LENGTH]),
3082 SHA_DIGEST_LENGTH, &(p[2]),
3083 (unsigned int *)&j, pkey->pkey.ec)) {
3084 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3091 if (pkey->type == NID_id_GostR3410_94
3092 || pkey->type == NID_id_GostR3410_2001) {
3093 unsigned char signbuf[64];
3095 size_t sigsize = 64;
3096 s->method->ssl3_enc->cert_verify_mac(s,
3097 NID_id_GostR3411_94, data);
3098 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3099 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3102 for (i = 63, j = 0; i >= 0; j++, i--) {
3103 p[2 + j] = signbuf[i];
3108 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3111 *(d++) = SSL3_MT_CERTIFICATE_VERIFY;
3114 s->state = SSL3_ST_CW_CERT_VRFY_B;
3115 s->init_num = (int)n + 4;
3118 EVP_MD_CTX_cleanup(&mctx);
3119 EVP_PKEY_CTX_free(pctx);
3120 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3122 EVP_MD_CTX_cleanup(&mctx);
3123 EVP_PKEY_CTX_free(pctx);
3127 int ssl3_send_client_certificate(SSL *s)
3130 EVP_PKEY *pkey = NULL;
3134 if (s->state == SSL3_ST_CW_CERT_A) {
3135 if ((s->cert == NULL) ||
3136 (s->cert->key->x509 == NULL) ||
3137 (s->cert->key->privatekey == NULL))
3138 s->state = SSL3_ST_CW_CERT_B;
3140 s->state = SSL3_ST_CW_CERT_C;
3143 /* We need to get a client cert */
3144 if (s->state == SSL3_ST_CW_CERT_B) {
3146 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3147 * return(-1); We then get retied later
3150 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3152 s->rwstate = SSL_X509_LOOKUP;
3155 s->rwstate = SSL_NOTHING;
3156 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3157 s->state = SSL3_ST_CW_CERT_B;
3158 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3160 } else if (i == 1) {
3162 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3163 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3169 EVP_PKEY_free(pkey);
3171 if (s->version == SSL3_VERSION) {
3172 s->s3->tmp.cert_req = 0;
3173 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3176 s->s3->tmp.cert_req = 2;
3180 /* Ok, we have a cert */
3181 s->state = SSL3_ST_CW_CERT_C;
3184 if (s->state == SSL3_ST_CW_CERT_C) {
3185 s->state = SSL3_ST_CW_CERT_D;
3186 l = ssl3_output_cert_chain(s,
3187 (s->s3->tmp.cert_req ==
3188 2) ? NULL : s->cert->key->x509);
3190 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3191 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3194 s->init_num = (int)l;
3197 /* SSL3_ST_CW_CERT_D */
3198 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3201 #define has_bits(i,m) (((i)&(m)) == (m))
3203 int ssl3_check_cert_and_algorithm(SSL *s)
3207 EVP_PKEY *pkey = NULL;
3209 #ifndef OPENSSL_NO_RSA
3212 #ifndef OPENSSL_NO_DH
3216 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3217 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3219 /* we don't have a certificate */
3220 if ((alg_a & (SSL_aDH | SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3223 sc = s->session->sess_cert;
3225 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3228 #ifndef OPENSSL_NO_RSA
3229 rsa = s->session->sess_cert->peer_rsa_tmp;
3231 #ifndef OPENSSL_NO_DH
3232 dh = s->session->sess_cert->peer_dh_tmp;
3235 /* This is the passed certificate */
3237 idx = sc->peer_cert_type;
3238 #ifndef OPENSSL_NO_ECDH
3239 if (idx == SSL_PKEY_ECC) {
3240 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3242 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3249 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3250 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3251 EVP_PKEY_free(pkey);
3253 /* Check that we have a certificate if we require one */
3254 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3255 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3256 SSL_R_MISSING_RSA_SIGNING_CERT);
3259 #ifndef OPENSSL_NO_DSA
3260 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3261 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3262 SSL_R_MISSING_DSA_SIGNING_CERT);
3266 #ifndef OPENSSL_NO_RSA
3267 if ((alg_k & SSL_kRSA) &&
3268 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3269 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3270 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3274 #ifndef OPENSSL_NO_DH
3275 if ((alg_k & SSL_kEDH) &&
3276 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3277 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3279 } else if ((alg_k & SSL_kDHr) && !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3280 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3281 SSL_R_MISSING_DH_RSA_CERT);
3284 # ifndef OPENSSL_NO_DSA
3285 else if ((alg_k & SSL_kDHd) && !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3286 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3287 SSL_R_MISSING_DH_DSA_CERT);
3293 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3294 #ifndef OPENSSL_NO_RSA
3295 if (alg_k & SSL_kRSA) {
3297 || RSA_size(rsa) * 8 >
3298 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3299 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3300 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3305 #ifndef OPENSSL_NO_DH
3306 if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
3308 || DH_size(dh) * 8 >
3309 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3310 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3311 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3317 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3318 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3324 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3329 #ifndef OPENSSL_NO_TLSEXT
3331 * Normally, we can tell if the server is resuming the session from
3332 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3333 * message after the ServerHello to determine if the server is resuming.
3334 * Therefore, we allow EAP-FAST to peek ahead.
3335 * ssl3_check_finished returns 1 if we are resuming from an external
3336 * pre-shared secret, we have a "ticket" and the next server handshake message
3337 * is Finished; and 0 otherwise. It returns -1 upon an error.
3339 static int ssl3_check_finished(SSL *s)
3343 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3344 !s->session->tlsext_tick)
3347 /* Need to permit this temporarily, in case the next message is Finished. */
3348 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3350 * This function is called when we might get a Certificate message instead,
3351 * so permit appropriate message length.
3352 * We ignore the return value as we're only interested in the message type
3353 * and not its length.
3355 s->method->ssl_get_message(s,
3358 -1, s->max_cert_list, &ok);
3359 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3364 s->s3->tmp.reuse_message = 1;
3366 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3369 /* If we're not done, then the CCS arrived early and we should bail. */
3370 if (s->s3->change_cipher_spec) {
3371 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3372 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3379 # ifndef OPENSSL_NO_NEXTPROTONEG
3380 int ssl3_send_next_proto(SSL *s)
3382 unsigned int len, padding_len;
3385 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3386 len = s->next_proto_negotiated_len;
3387 padding_len = 32 - ((len + 2) % 32);
3388 d = (unsigned char *)s->init_buf->data;
3390 memcpy(d + 5, s->next_proto_negotiated, len);
3391 d[5 + len] = padding_len;
3392 memset(d + 6 + len, 0, padding_len);
3393 *(d++) = SSL3_MT_NEXT_PROTO;
3394 l2n3(2 + len + padding_len, d);
3395 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3396 s->init_num = 4 + 2 + len + padding_len;
3400 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3402 #endif /* !OPENSSL_NO_NEXTPROTONEG */
3403 #endif /* !OPENSSL_NO_TLSEXT */
3405 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3408 #ifndef OPENSSL_NO_ENGINE
3409 if (s->ctx->client_cert_engine) {
3410 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3411 SSL_get_client_CA_list(s),
3412 px509, ppkey, NULL, NULL, NULL);
3417 if (s->ctx->client_cert_cb)
3418 i = s->ctx->client_cert_cb(s, px509, ppkey);