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>
159 #ifndef OPENSSL_NO_DH
160 # include <openssl/dh.h>
162 #include <openssl/bn.h>
163 #ifndef OPENSSL_NO_ENGINE
164 # include <openssl/engine.h>
167 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
169 #ifndef OPENSSL_NO_SSL3_METHOD
170 static const SSL_METHOD *ssl3_get_client_method(int ver)
172 if (ver == SSL3_VERSION)
173 return (SSLv3_client_method());
178 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
179 ssl_undefined_function,
180 ssl3_connect, ssl3_get_client_method)
182 int ssl3_connect(SSL *s)
185 unsigned long Time = (unsigned long)time(NULL);
186 void (*cb) (const SSL *ssl, int type, int val) = NULL;
188 int new_state, state, skip = 0;
190 RAND_add(&Time, sizeof(Time), 0);
194 if (s->info_callback != NULL)
195 cb = s->info_callback;
196 else if (s->ctx->info_callback != NULL)
197 cb = s->ctx->info_callback;
200 if (!SSL_in_init(s) || SSL_in_before(s))
203 #ifndef OPENSSL_NO_HEARTBEATS
205 * If we're awaiting a HeartbeatResponse, pretend we already got and
206 * don't await it anymore, because Heartbeats don't make sense during
209 if (s->tlsext_hb_pending) {
210 s->tlsext_hb_pending = 0;
219 case SSL_ST_RENEGOTIATE:
221 s->state = SSL_ST_CONNECT;
222 s->ctx->stats.sess_connect_renegotiate++;
226 case SSL_ST_BEFORE | SSL_ST_CONNECT:
227 case SSL_ST_OK | SSL_ST_CONNECT:
231 cb(s, SSL_CB_HANDSHAKE_START, 1);
233 if ((s->version & 0xff00) != 0x0300) {
234 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
239 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
240 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
244 /* s->version=SSL3_VERSION; */
245 s->type = SSL_ST_CONNECT;
247 if (s->init_buf == NULL) {
248 if ((buf = BUF_MEM_new()) == NULL) {
252 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
260 if (!ssl3_setup_buffers(s)) {
265 /* setup buffing BIO */
266 if (!ssl_init_wbio_buffer(s, 0)) {
271 /* don't push the buffering BIO quite yet */
273 ssl3_init_finished_mac(s);
275 s->state = SSL3_ST_CW_CLNT_HELLO_A;
276 s->ctx->stats.sess_connect++;
278 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
280 * Should have been reset by ssl3_get_finished, too.
282 s->s3->change_cipher_spec = 0;
285 case SSL3_ST_CW_CLNT_HELLO_A:
286 case SSL3_ST_CW_CLNT_HELLO_B:
289 ret = ssl3_client_hello(s);
292 s->state = SSL3_ST_CR_SRVR_HELLO_A;
295 /* turn on buffering for the next lot of output */
296 if (s->bbio != s->wbio)
297 s->wbio = BIO_push(s->bbio, s->wbio);
301 case SSL3_ST_CR_SRVR_HELLO_A:
302 case SSL3_ST_CR_SRVR_HELLO_B:
303 ret = ssl3_get_server_hello(s);
308 s->state = SSL3_ST_CR_FINISHED_A;
309 #ifndef OPENSSL_NO_TLSEXT
310 if (s->tlsext_ticket_expected) {
311 /* receive renewed session ticket */
312 s->state = SSL3_ST_CR_SESSION_TICKET_A;
316 s->state = SSL3_ST_CR_CERT_A;
320 case SSL3_ST_CR_CERT_A:
321 case SSL3_ST_CR_CERT_B:
322 /* Check if it is anon DH/ECDH, SRP auth */
326 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
327 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
328 ret = ssl3_get_server_certificate(s);
331 #ifndef OPENSSL_NO_TLSEXT
332 if (s->tlsext_status_expected)
333 s->state = SSL3_ST_CR_CERT_STATUS_A;
335 s->state = SSL3_ST_CR_KEY_EXCH_A;
338 s->state = SSL3_ST_CR_KEY_EXCH_A;
344 s->state = SSL3_ST_CR_KEY_EXCH_A;
349 case SSL3_ST_CR_KEY_EXCH_A:
350 case SSL3_ST_CR_KEY_EXCH_B:
351 ret = ssl3_get_key_exchange(s);
354 s->state = SSL3_ST_CR_CERT_REQ_A;
358 * at this point we check that we have the required stuff from
361 if (!ssl3_check_cert_and_algorithm(s)) {
367 case SSL3_ST_CR_CERT_REQ_A:
368 case SSL3_ST_CR_CERT_REQ_B:
369 ret = ssl3_get_certificate_request(s);
372 s->state = SSL3_ST_CR_SRVR_DONE_A;
376 case SSL3_ST_CR_SRVR_DONE_A:
377 case SSL3_ST_CR_SRVR_DONE_B:
378 ret = ssl3_get_server_done(s);
381 #ifndef OPENSSL_NO_SRP
382 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
383 if ((ret = SRP_Calc_A_param(s)) <= 0) {
384 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
385 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
390 if (s->s3->tmp.cert_req)
391 s->state = SSL3_ST_CW_CERT_A;
393 s->state = SSL3_ST_CW_KEY_EXCH_A;
398 case SSL3_ST_CW_CERT_A:
399 case SSL3_ST_CW_CERT_B:
400 case SSL3_ST_CW_CERT_C:
401 case SSL3_ST_CW_CERT_D:
402 ret = ssl3_send_client_certificate(s);
405 s->state = SSL3_ST_CW_KEY_EXCH_A;
409 case SSL3_ST_CW_KEY_EXCH_A:
410 case SSL3_ST_CW_KEY_EXCH_B:
411 ret = ssl3_send_client_key_exchange(s);
415 * EAY EAY EAY need to check for DH fix cert sent back
418 * For TLS, cert_req is set to 2, so a cert chain of nothing is
419 * sent, but no verify packet is sent
422 * XXX: For now, we do not support client authentication in ECDH
423 * cipher suites with ECDH (rather than ECDSA) certificates. We
424 * need to skip the certificate verify message when client's
425 * ECDH public key is sent inside the client certificate.
427 if (s->s3->tmp.cert_req == 1) {
428 s->state = SSL3_ST_CW_CERT_VRFY_A;
430 s->state = SSL3_ST_CW_CHANGE_A;
432 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
433 s->state = SSL3_ST_CW_CHANGE_A;
439 case SSL3_ST_CW_CERT_VRFY_A:
440 case SSL3_ST_CW_CERT_VRFY_B:
441 ret = ssl3_send_client_verify(s);
444 s->state = SSL3_ST_CW_CHANGE_A;
448 case SSL3_ST_CW_CHANGE_A:
449 case SSL3_ST_CW_CHANGE_B:
450 ret = ssl3_send_change_cipher_spec(s,
452 SSL3_ST_CW_CHANGE_B);
456 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
457 s->state = SSL3_ST_CW_FINISHED_A;
459 if (s->s3->next_proto_neg_seen)
460 s->state = SSL3_ST_CW_NEXT_PROTO_A;
462 s->state = SSL3_ST_CW_FINISHED_A;
466 s->session->cipher = s->s3->tmp.new_cipher;
467 #ifdef OPENSSL_NO_COMP
468 s->session->compress_meth = 0;
470 if (s->s3->tmp.new_compression == NULL)
471 s->session->compress_meth = 0;
473 s->session->compress_meth = s->s3->tmp.new_compression->id;
475 if (!s->method->ssl3_enc->setup_key_block(s)) {
480 if (!s->method->ssl3_enc->change_cipher_state(s,
481 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
489 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
490 case SSL3_ST_CW_NEXT_PROTO_A:
491 case SSL3_ST_CW_NEXT_PROTO_B:
492 ret = ssl3_send_next_proto(s);
495 s->state = SSL3_ST_CW_FINISHED_A;
499 case SSL3_ST_CW_FINISHED_A:
500 case SSL3_ST_CW_FINISHED_B:
501 ret = ssl3_send_finished(s,
502 SSL3_ST_CW_FINISHED_A,
503 SSL3_ST_CW_FINISHED_B,
505 ssl3_enc->client_finished_label,
507 ssl3_enc->client_finished_label_len);
510 s->state = SSL3_ST_CW_FLUSH;
513 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
515 s->s3->tmp.next_state = SSL_ST_OK;
516 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
517 s->state = SSL_ST_OK;
518 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
519 s->s3->delay_buf_pop_ret = 0;
522 #ifndef OPENSSL_NO_TLSEXT
524 * Allow NewSessionTicket if ticket expected
526 if (s->tlsext_ticket_expected)
527 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
531 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
536 #ifndef OPENSSL_NO_TLSEXT
537 case SSL3_ST_CR_SESSION_TICKET_A:
538 case SSL3_ST_CR_SESSION_TICKET_B:
539 ret = ssl3_get_new_session_ticket(s);
542 s->state = SSL3_ST_CR_FINISHED_A;
546 case SSL3_ST_CR_CERT_STATUS_A:
547 case SSL3_ST_CR_CERT_STATUS_B:
548 ret = ssl3_get_cert_status(s);
551 s->state = SSL3_ST_CR_KEY_EXCH_A;
556 case SSL3_ST_CR_FINISHED_A:
557 case SSL3_ST_CR_FINISHED_B:
558 s->s3->flags |= SSL3_FLAGS_CCS_OK;
559 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
560 SSL3_ST_CR_FINISHED_B);
565 s->state = SSL3_ST_CW_CHANGE_A;
567 s->state = SSL_ST_OK;
571 case SSL3_ST_CW_FLUSH:
572 s->rwstate = SSL_WRITING;
573 if (BIO_flush(s->wbio) <= 0) {
577 s->rwstate = SSL_NOTHING;
578 s->state = s->s3->tmp.next_state;
582 /* clean a few things up */
583 ssl3_cleanup_key_block(s);
585 if (s->init_buf != NULL) {
586 BUF_MEM_free(s->init_buf);
591 * If we are not 'joining' the last two packets, remove the
594 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
595 ssl_free_wbio_buffer(s);
596 /* else do it later in ssl3_write */
602 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
604 s->ctx->stats.sess_hit++;
608 s->handshake_func = ssl3_connect;
609 s->ctx->stats.sess_connect_good++;
612 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
618 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
624 /* did we do anything */
625 if (!s->s3->tmp.reuse_message && !skip) {
627 if ((ret = BIO_flush(s->wbio)) <= 0)
631 if ((cb != NULL) && (s->state != state)) {
632 new_state = s->state;
634 cb(s, SSL_CB_CONNECT_LOOP, 1);
635 s->state = new_state;
645 cb(s, SSL_CB_CONNECT_EXIT, ret);
649 int ssl3_client_hello(SSL *s)
652 unsigned char *p, *d;
656 #ifndef OPENSSL_NO_COMP
661 buf = (unsigned char *)s->init_buf->data;
662 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
663 SSL_SESSION *sess = s->session;
664 if ((sess == NULL) ||
665 (sess->ssl_version != s->version) ||
666 !sess->session_id_length || (sess->not_resumable)) {
667 if (!ssl_get_new_session(s, 0))
670 if (s->method->version == DTLS_ANY_VERSION) {
671 /* Determine which DTLS version to use */
672 int options = s->options;
673 /* If DTLS 1.2 disabled correct the version number */
674 if (options & SSL_OP_NO_DTLSv1_2) {
675 if (tls1_suiteb(s)) {
676 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
677 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
681 * Disabling all versions is silly: return an error.
683 if (options & SSL_OP_NO_DTLSv1) {
684 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
688 * Update method so we don't use any DTLS 1.2 features.
690 s->method = DTLSv1_client_method();
691 s->version = DTLS1_VERSION;
694 * We only support one version: update method
696 if (options & SSL_OP_NO_DTLSv1)
697 s->method = DTLSv1_2_client_method();
698 s->version = DTLS1_2_VERSION;
700 s->client_version = s->version;
702 /* else use the pre-loaded session */
704 p = s->s3->client_random;
707 * for DTLS if client_random is initialized, reuse it, we are
708 * required to use same upon reply to HelloVerify
710 if (SSL_IS_DTLS(s)) {
713 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
723 ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
725 /* Do the message type and length last */
726 d = p = ssl_handshake_start(s);
729 * version indicates the negotiated version: for example from
730 * an SSLv2/v3 compatible client hello). The client_version
731 * field is the maximum version we permit and it is also
732 * used in RSA encrypted premaster secrets. Some servers can
733 * choke if we initially report a higher version then
734 * renegotiate to a lower one in the premaster secret. This
735 * didn't happen with TLS 1.0 as most servers supported it
736 * but it can with TLS 1.1 or later if the server only supports
739 * Possible scenario with previous logic:
740 * 1. Client hello indicates TLS 1.2
741 * 2. Server hello says TLS 1.0
742 * 3. RSA encrypted premaster secret uses 1.2.
743 * 4. Handhaked proceeds using TLS 1.0.
744 * 5. Server sends hello request to renegotiate.
745 * 6. Client hello indicates TLS v1.0 as we now
746 * know that is maximum server supports.
747 * 7. Server chokes on RSA encrypted premaster secret
748 * containing version 1.0.
750 * For interoperability it should be OK to always use the
751 * maximum version we support in client hello and then rely
752 * on the checking of version to ensure the servers isn't
753 * being inconsistent: for example initially negotiating with
754 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
755 * client_version in client hello and not resetting it to
756 * the negotiated version.
759 *(p++) = s->version >> 8;
760 *(p++) = s->version & 0xff;
761 s->client_version = s->version;
763 *(p++) = s->client_version >> 8;
764 *(p++) = s->client_version & 0xff;
768 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
769 p += SSL3_RANDOM_SIZE;
775 i = s->session->session_id_length;
778 if (i > (int)sizeof(s->session->session_id)) {
779 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
782 memcpy(p, s->session->session_id, i);
786 /* cookie stuff for DTLS */
787 if (SSL_IS_DTLS(s)) {
788 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
789 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
792 *(p++) = s->d1->cookie_len;
793 memcpy(p, s->d1->cookie, s->d1->cookie_len);
794 p += s->d1->cookie_len;
797 /* Ciphers supported */
798 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
800 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
803 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
805 * Some servers hang if client hello > 256 bytes as hack workaround
806 * chop number of supported ciphers to keep it well below this if we
809 if (TLS1_get_version(s) >= TLS1_2_VERSION
810 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
811 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
817 #ifdef OPENSSL_NO_COMP
821 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
824 j = sk_SSL_COMP_num(s->ctx->comp_methods);
826 for (i = 0; i < j; i++) {
827 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
831 *(p++) = 0; /* Add the NULL method */
833 #ifndef OPENSSL_NO_TLSEXT
835 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
836 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
840 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
842 ssl3_send_alert(s, SSL3_AL_FATAL, al);
843 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
849 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
850 s->state = SSL3_ST_CW_CLNT_HELLO_B;
853 /* SSL3_ST_CW_CLNT_HELLO_B */
854 return ssl_do_write(s);
859 int ssl3_get_server_hello(SSL *s)
861 STACK_OF(SSL_CIPHER) *sk;
864 unsigned char *p, *d;
865 int i, al = SSL_AD_INTERNAL_ERROR, ok;
868 #ifndef OPENSSL_NO_COMP
872 * Hello verify request and/or server hello version may not match so set
873 * first packet if we're negotiating version.
878 n = s->method->ssl_get_message(s,
879 SSL3_ST_CR_SRVR_HELLO_A,
880 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
885 if (SSL_IS_DTLS(s)) {
887 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
888 if (s->d1->send_cookie == 0) {
889 s->s3->tmp.reuse_message = 1;
891 } else { /* already sent a cookie */
893 al = SSL_AD_UNEXPECTED_MESSAGE;
894 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
900 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
901 al = SSL_AD_UNEXPECTED_MESSAGE;
902 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
906 d = p = (unsigned char *)s->init_msg;
907 if (s->method->version == DTLS_ANY_VERSION) {
908 /* Work out correct protocol version to use */
909 int hversion = (p[0] << 8) | p[1];
910 int options = s->options;
911 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
912 s->method = DTLSv1_2_client_method();
913 else if (tls1_suiteb(s)) {
914 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
915 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
916 s->version = hversion;
917 al = SSL_AD_PROTOCOL_VERSION;
919 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
920 s->method = DTLSv1_client_method();
922 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
923 s->version = hversion;
924 al = SSL_AD_PROTOCOL_VERSION;
927 s->version = s->method->version;
930 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
931 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
932 s->version = (s->version & 0xff00) | p[1];
933 al = SSL_AD_PROTOCOL_VERSION;
938 /* load the server hello data */
939 /* load the server random */
940 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
941 p += SSL3_RANDOM_SIZE;
945 /* get the session-id */
948 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
949 al = SSL_AD_ILLEGAL_PARAMETER;
950 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
953 #ifndef OPENSSL_NO_TLSEXT
955 * check if we want to resume the session based on external pre-shared
958 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
959 SSL_CIPHER *pref_cipher = NULL;
960 s->session->master_key_length = sizeof(s->session->master_key);
961 if (s->tls_session_secret_cb(s, s->session->master_key,
962 &s->session->master_key_length,
964 s->tls_session_secret_cb_arg)) {
965 s->session->cipher = pref_cipher ?
966 pref_cipher : ssl_get_cipher_by_char(s, p + j);
970 #endif /* OPENSSL_NO_TLSEXT */
972 if (!s->hit && j != 0 && j == s->session->session_id_length
973 && memcmp(p, s->session->session_id, j) == 0) {
974 if (s->sid_ctx_length != s->session->sid_ctx_length
975 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
976 /* actually a client application bug */
977 al = SSL_AD_ILLEGAL_PARAMETER;
978 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
979 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
984 /* a miss or crap from the other end */
987 * If we were trying for session-id reuse, make a new SSL_SESSION so
988 * we don't stuff up other people
990 if (s->session->session_id_length > 0) {
991 if (!ssl_get_new_session(s, 0)) {
995 s->session->session_id_length = j;
996 memcpy(s->session->session_id, p, j); /* j could be 0 */
999 c = ssl_get_cipher_by_char(s, p);
1001 /* unknown cipher */
1002 al = SSL_AD_ILLEGAL_PARAMETER;
1003 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1006 /* Set version disabled mask now we know version */
1007 if (!SSL_USE_TLS1_2_CIPHERS(s))
1008 ct->mask_ssl = SSL_TLSV1_2;
1012 * If it is a disabled cipher we didn't send it in client hello, so
1015 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1016 al = SSL_AD_ILLEGAL_PARAMETER;
1017 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1020 p += ssl_put_cipher_by_char(s, NULL, NULL);
1022 sk = ssl_get_ciphers_by_id(s);
1023 i = sk_SSL_CIPHER_find(sk, c);
1025 /* we did not say we would use this cipher */
1026 al = SSL_AD_ILLEGAL_PARAMETER;
1027 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1032 * Depending on the session caching (internal/external), the cipher
1033 * and/or cipher_id values may not be set. Make sure that cipher_id is
1034 * set and use it for comparison.
1036 if (s->session->cipher)
1037 s->session->cipher_id = s->session->cipher->id;
1038 if (s->hit && (s->session->cipher_id != c->id)) {
1039 /* Workaround is now obsolete */
1041 if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1044 al = SSL_AD_ILLEGAL_PARAMETER;
1045 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1046 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1050 s->s3->tmp.new_cipher = c;
1052 * Don't digest cached records if no sigalgs: we may need them for client
1055 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1057 /* lets get the compression algorithm */
1059 #ifdef OPENSSL_NO_COMP
1061 al = SSL_AD_ILLEGAL_PARAMETER;
1062 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1063 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1067 * If compression is disabled we'd better not try to resume a session
1068 * using compression.
1070 if (s->session->compress_meth != 0) {
1071 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1076 if (s->hit && j != s->session->compress_meth) {
1077 al = SSL_AD_ILLEGAL_PARAMETER;
1078 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1079 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1084 else if (!ssl_allow_compression(s)) {
1085 al = SSL_AD_ILLEGAL_PARAMETER;
1086 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1089 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1091 if ((j != 0) && (comp == NULL)) {
1092 al = SSL_AD_ILLEGAL_PARAMETER;
1093 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1094 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1097 s->s3->tmp.new_compression = comp;
1101 #ifndef OPENSSL_NO_TLSEXT
1102 /* TLS extensions */
1103 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1104 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1110 /* wrong packet length */
1111 al = SSL_AD_DECODE_ERROR;
1112 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1118 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1123 int ssl3_get_server_certificate(SSL *s)
1125 int al, i, ok, ret = -1;
1126 unsigned long n, nc, llen, l;
1128 const unsigned char *q, *p;
1130 STACK_OF(X509) *sk = NULL;
1132 EVP_PKEY *pkey = NULL;
1133 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1136 n = s->method->ssl_get_message(s,
1139 -1, s->max_cert_list, &ok);
1144 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1145 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1146 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1147 s->s3->tmp.reuse_message = 1;
1151 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1152 al = SSL_AD_UNEXPECTED_MESSAGE;
1153 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1156 p = d = (unsigned char *)s->init_msg;
1158 if ((sk = sk_X509_new_null()) == NULL) {
1159 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1164 if (llen + 3 != n) {
1165 al = SSL_AD_DECODE_ERROR;
1166 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1169 for (nc = 0; nc < llen;) {
1171 if ((l + nc + 3) > llen) {
1172 al = SSL_AD_DECODE_ERROR;
1173 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1174 SSL_R_CERT_LENGTH_MISMATCH);
1179 x = d2i_X509(NULL, &q, l);
1181 al = SSL_AD_BAD_CERTIFICATE;
1182 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1186 al = SSL_AD_DECODE_ERROR;
1187 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1188 SSL_R_CERT_LENGTH_MISMATCH);
1191 if (!sk_X509_push(sk, x)) {
1192 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1200 i = ssl_verify_cert_chain(s, sk);
1201 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1202 #ifndef OPENSSL_NO_KRB5
1203 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1204 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1205 #endif /* OPENSSL_NO_KRB5 */
1207 al = ssl_verify_alarm_type(s->verify_result);
1208 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1209 SSL_R_CERTIFICATE_VERIFY_FAILED);
1212 ERR_clear_error(); /* but we keep s->verify_result */
1214 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1215 al = SSL_AD_HANDSHAKE_FAILURE;
1219 sc = ssl_sess_cert_new();
1223 if (s->session->sess_cert)
1224 ssl_sess_cert_free(s->session->sess_cert);
1225 s->session->sess_cert = sc;
1227 sc->cert_chain = sk;
1229 * Inconsistency alert: cert_chain does include the peer's certificate,
1230 * which we don't include in s3_srvr.c
1232 x = sk_X509_value(sk, 0);
1235 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1238 pkey = X509_get_pubkey(x);
1240 /* VRS: allow null cert if auth == KRB5 */
1241 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1242 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1246 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1247 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1248 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1249 s->s3->tmp.new_cipher->name,
1250 s->s3->tmp.new_cipher->algorithm_mkey,
1251 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1252 #endif /* KSSL_DEBUG */
1254 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1257 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1258 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1262 i = ssl_cert_type(x, pkey);
1263 if (need_cert && i < 0) {
1266 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1267 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1272 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1273 if (exp_idx >= 0 && i != exp_idx) {
1275 al = SSL_AD_ILLEGAL_PARAMETER;
1276 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1277 SSL_R_WRONG_CERTIFICATE_TYPE);
1280 sc->peer_cert_type = i;
1281 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1283 * Why would the following ever happen? We just created sc a couple
1286 if (sc->peer_pkeys[i].x509 != NULL)
1287 X509_free(sc->peer_pkeys[i].x509);
1288 sc->peer_pkeys[i].x509 = x;
1289 sc->peer_key = &(sc->peer_pkeys[i]);
1291 if (s->session->peer != NULL)
1292 X509_free(s->session->peer);
1293 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1294 s->session->peer = x;
1296 sc->peer_cert_type = i;
1297 sc->peer_key = NULL;
1299 if (s->session->peer != NULL)
1300 X509_free(s->session->peer);
1301 s->session->peer = NULL;
1303 s->session->verify_result = s->verify_result;
1309 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1312 EVP_PKEY_free(pkey);
1314 sk_X509_pop_free(sk, X509_free);
1318 int ssl3_get_key_exchange(SSL *s)
1320 #ifndef OPENSSL_NO_RSA
1321 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1324 unsigned char *param, *p;
1326 long i, param_len, n, alg_k, alg_a;
1327 EVP_PKEY *pkey = NULL;
1328 const EVP_MD *md = NULL;
1329 #ifndef OPENSSL_NO_RSA
1332 #ifndef OPENSSL_NO_DH
1335 #ifndef OPENSSL_NO_ECDH
1336 EC_KEY *ecdh = NULL;
1337 BN_CTX *bn_ctx = NULL;
1338 EC_POINT *srvr_ecpoint = NULL;
1340 int encoded_pt_len = 0;
1343 EVP_MD_CTX_init(&md_ctx);
1346 * use same message size as in ssl3_get_certificate_request() as
1347 * ServerKeyExchange message may be skipped
1349 n = s->method->ssl_get_message(s,
1350 SSL3_ST_CR_KEY_EXCH_A,
1351 SSL3_ST_CR_KEY_EXCH_B,
1352 -1, s->max_cert_list, &ok);
1356 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1358 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1360 * Can't skip server key exchange if this is an ephemeral
1363 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1364 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1365 al = SSL_AD_UNEXPECTED_MESSAGE;
1368 #ifndef OPENSSL_NO_PSK
1370 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1371 * identity hint is sent. Set session->sess_cert anyway to avoid
1374 if (alg_k & SSL_kPSK) {
1375 s->session->sess_cert = ssl_sess_cert_new();
1376 if (s->ctx->psk_identity_hint)
1377 OPENSSL_free(s->ctx->psk_identity_hint);
1378 s->ctx->psk_identity_hint = NULL;
1381 s->s3->tmp.reuse_message = 1;
1385 param = p = (unsigned char *)s->init_msg;
1386 if (s->session->sess_cert != NULL) {
1387 #ifndef OPENSSL_NO_RSA
1388 if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1389 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1390 s->session->sess_cert->peer_rsa_tmp = NULL;
1393 #ifndef OPENSSL_NO_DH
1394 if (s->session->sess_cert->peer_dh_tmp) {
1395 DH_free(s->session->sess_cert->peer_dh_tmp);
1396 s->session->sess_cert->peer_dh_tmp = NULL;
1399 #ifndef OPENSSL_NO_ECDH
1400 if (s->session->sess_cert->peer_ecdh_tmp) {
1401 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1402 s->session->sess_cert->peer_ecdh_tmp = NULL;
1406 s->session->sess_cert = ssl_sess_cert_new();
1409 /* Total length of the parameters including the length prefix */
1412 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1414 al = SSL_AD_DECODE_ERROR;
1416 #ifndef OPENSSL_NO_PSK
1417 if (alg_k & SSL_kPSK) {
1418 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1421 if (param_len > n) {
1422 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1428 * Store PSK identity hint for later use, hint is used in
1429 * ssl3_send_client_key_exchange. Assume that the maximum length of
1430 * a PSK identity hint can be as long as the maximum length of a PSK
1433 if (i > PSK_MAX_IDENTITY_LEN) {
1434 al = SSL_AD_HANDSHAKE_FAILURE;
1435 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1438 if (i > n - param_len) {
1439 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1440 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1446 * If received PSK identity hint contains NULL characters, the hint
1447 * is truncated from the first NULL. p may not be ending with NULL,
1448 * so create a NULL-terminated string.
1450 memcpy(tmp_id_hint, p, i);
1451 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1452 if (s->ctx->psk_identity_hint != NULL)
1453 OPENSSL_free(s->ctx->psk_identity_hint);
1454 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1455 if (s->ctx->psk_identity_hint == NULL) {
1456 al = SSL_AD_HANDSHAKE_FAILURE;
1457 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1464 #endif /* !OPENSSL_NO_PSK */
1465 #ifndef OPENSSL_NO_SRP
1466 if (alg_k & SSL_kSRP) {
1468 if (param_len > n) {
1469 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1474 if (i > n - param_len) {
1475 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1480 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1481 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1486 if (2 > n - param_len) {
1487 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1494 if (i > n - param_len) {
1495 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1500 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1501 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1506 if (1 > n - param_len) {
1507 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1512 i = (unsigned int)(p[0]);
1515 if (i > n - param_len) {
1516 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1521 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1522 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1527 if (2 > n - param_len) {
1528 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1535 if (i > n - param_len) {
1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1541 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1542 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1548 if (!srp_verify_server_param(s, &al)) {
1549 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1553 /* We must check if there is a certificate */
1554 # ifndef OPENSSL_NO_RSA
1555 if (alg_a & SSL_aRSA)
1557 X509_get_pubkey(s->session->
1558 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1562 # ifndef OPENSSL_NO_DSA
1563 else if (alg_a & SSL_aDSS)
1565 X509_get_pubkey(s->session->
1566 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1570 #endif /* !OPENSSL_NO_SRP */
1571 #ifndef OPENSSL_NO_RSA
1572 if (alg_k & SSL_kRSA) {
1573 /* Temporary RSA keys only allowed in export ciphersuites */
1574 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1575 al = SSL_AD_UNEXPECTED_MESSAGE;
1576 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1579 if ((rsa = RSA_new()) == NULL) {
1580 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1585 if (param_len > n) {
1586 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1591 if (i > n - param_len) {
1592 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1597 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1598 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1603 if (2 > n - param_len) {
1604 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1611 if (i > n - param_len) {
1612 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1617 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1618 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1624 /* this should be because we are using an export cipher */
1625 if (alg_a & SSL_aRSA)
1627 X509_get_pubkey(s->session->
1628 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1630 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1633 s->session->sess_cert->peer_rsa_tmp = rsa;
1636 #else /* OPENSSL_NO_RSA */
1639 #ifndef OPENSSL_NO_DH
1640 else if (alg_k & SSL_kDHE) {
1641 if ((dh = DH_new()) == NULL) {
1642 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1647 if (param_len > n) {
1648 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1653 if (i > n - param_len) {
1654 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1659 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1660 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1665 if (2 > n - param_len) {
1666 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1673 if (i > n - param_len) {
1674 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1679 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1680 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1685 if (2 > n - param_len) {
1686 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1693 if (i > n - param_len) {
1694 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1699 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1700 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1706 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1707 al = SSL_AD_HANDSHAKE_FAILURE;
1708 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1711 # ifndef OPENSSL_NO_RSA
1712 if (alg_a & SSL_aRSA)
1714 X509_get_pubkey(s->session->
1715 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1719 # ifndef OPENSSL_NO_DSA
1720 else if (alg_a & SSL_aDSS)
1722 X509_get_pubkey(s->session->
1723 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1726 /* else anonymous DH, so no certificate or pkey. */
1728 s->session->sess_cert->peer_dh_tmp = dh;
1730 } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1731 al = SSL_AD_ILLEGAL_PARAMETER;
1732 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1733 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1736 #endif /* !OPENSSL_NO_DH */
1738 #ifndef OPENSSL_NO_ECDH
1739 else if (alg_k & SSL_kECDHE) {
1741 const EC_GROUP *group;
1743 if ((ecdh = EC_KEY_new()) == NULL) {
1744 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1749 * Extract elliptic curve parameters and the server's ephemeral ECDH
1750 * public key. Keep accumulating lengths of various components in
1751 * param_len and make sure it never exceeds n.
1755 * XXX: For now we only support named (not generic) curves and the
1756 * ECParameters in this case is just three bytes. We also need one
1757 * byte for the length of the encoded point
1760 if (param_len > n) {
1761 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1765 * Check curve is one of our preferences, if not server has sent an
1766 * invalid curve. ECParameters is 3 bytes.
1768 if (!tls1_check_curve(s, p, 3)) {
1769 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1773 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1774 al = SSL_AD_INTERNAL_ERROR;
1775 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1776 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1780 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1781 if (ngroup == NULL) {
1782 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1785 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1789 EC_GROUP_free(ngroup);
1791 group = EC_KEY_get0_group(ecdh);
1793 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1794 (EC_GROUP_get_degree(group) > 163)) {
1795 al = SSL_AD_EXPORT_RESTRICTION;
1796 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1797 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1803 /* Next, get the encoded ECPoint */
1804 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1805 ((bn_ctx = BN_CTX_new()) == NULL)) {
1806 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1810 encoded_pt_len = *p; /* length of encoded point */
1813 if ((encoded_pt_len > n - param_len) ||
1814 (EC_POINT_oct2point(group, srvr_ecpoint,
1815 p, encoded_pt_len, bn_ctx) == 0)) {
1816 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1819 param_len += encoded_pt_len;
1822 p += encoded_pt_len;
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)
1833 X509_get_pubkey(s->session->
1834 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1836 # ifndef OPENSSL_NO_ECDSA
1837 else if (alg_a & SSL_aECDSA)
1839 X509_get_pubkey(s->session->
1840 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1842 /* else anonymous ECDH, so no certificate or pkey. */
1843 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1844 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1846 BN_CTX_free(bn_ctx);
1848 EC_POINT_free(srvr_ecpoint);
1849 srvr_ecpoint = NULL;
1851 al = SSL_AD_UNEXPECTED_MESSAGE;
1852 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1855 #endif /* !OPENSSL_NO_ECDH */
1857 /* p points to the next byte, there are 'n' bytes left */
1859 /* if it was signed, check the signature */
1861 if (SSL_USE_SIGALGS(s)) {
1864 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1867 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1874 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1882 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1887 j = EVP_PKEY_size(pkey);
1890 * Check signature length. If n is 0 then signature is empty
1892 if ((i != n) || (n > j) || (n <= 0)) {
1893 /* wrong packet length */
1894 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1897 #ifndef OPENSSL_NO_RSA
1898 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1904 for (num = 2; num > 0; num--) {
1905 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1906 EVP_DigestInit_ex(&md_ctx, (num == 2)
1907 ? s->ctx->md5 : s->ctx->sha1, NULL);
1908 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1910 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1912 EVP_DigestUpdate(&md_ctx, param, param_len);
1913 EVP_DigestFinal_ex(&md_ctx, q, &size);
1917 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1919 al = SSL_AD_DECRYPT_ERROR;
1920 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1925 al = SSL_AD_DECRYPT_ERROR;
1926 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1932 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1933 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1935 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1937 EVP_VerifyUpdate(&md_ctx, param, param_len);
1938 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1940 al = SSL_AD_DECRYPT_ERROR;
1941 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1946 /* aNULL, aSRP or kPSK do not need public keys */
1947 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1948 /* Might be wrong key type, check it */
1949 if (ssl3_check_cert_and_algorithm(s))
1950 /* Otherwise this shouldn't happen */
1951 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1954 /* still data left over */
1956 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1960 EVP_PKEY_free(pkey);
1961 EVP_MD_CTX_cleanup(&md_ctx);
1964 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1966 EVP_PKEY_free(pkey);
1967 #ifndef OPENSSL_NO_RSA
1971 #ifndef OPENSSL_NO_DH
1975 #ifndef OPENSSL_NO_ECDH
1976 BN_CTX_free(bn_ctx);
1977 EC_POINT_free(srvr_ecpoint);
1981 EVP_MD_CTX_cleanup(&md_ctx);
1985 int ssl3_get_certificate_request(SSL *s)
1988 unsigned long n, nc, l;
1989 unsigned int llen, ctype_num, i;
1990 X509_NAME *xn = NULL;
1991 const unsigned char *p, *q;
1993 STACK_OF(X509_NAME) *ca_sk = NULL;
1995 n = s->method->ssl_get_message(s,
1996 SSL3_ST_CR_CERT_REQ_A,
1997 SSL3_ST_CR_CERT_REQ_B,
1998 -1, s->max_cert_list, &ok);
2003 s->s3->tmp.cert_req = 0;
2005 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2006 s->s3->tmp.reuse_message = 1;
2008 * If we get here we don't need any cached handshake records as we
2009 * wont be doing client auth.
2011 if (s->s3->handshake_buffer) {
2012 if (!ssl3_digest_cached_records(s))
2018 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2019 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2020 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2024 /* TLS does not like anon-DH with client cert */
2025 if (s->version > SSL3_VERSION) {
2026 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2027 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2028 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2029 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2034 p = d = (unsigned char *)s->init_msg;
2036 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2037 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2041 /* get the certificate types */
2043 if (s->cert->ctypes) {
2044 OPENSSL_free(s->cert->ctypes);
2045 s->cert->ctypes = NULL;
2047 if (ctype_num > SSL3_CT_NUMBER) {
2048 /* If we exceed static buffer copy all to cert structure */
2049 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2050 if (s->cert->ctypes == NULL) {
2051 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2054 memcpy(s->cert->ctypes, p, ctype_num);
2055 s->cert->ctype_num = (size_t)ctype_num;
2056 ctype_num = SSL3_CT_NUMBER;
2058 for (i = 0; i < ctype_num; i++)
2059 s->s3->tmp.ctype[i] = p[i];
2061 if (SSL_USE_SIGALGS(s)) {
2064 * Check we have enough room for signature algorithms and following
2067 if ((unsigned long)(p - d + llen + 2) > n) {
2068 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2069 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2070 SSL_R_DATA_LENGTH_TOO_LONG);
2073 /* Clear certificate digests and validity flags */
2074 for (i = 0; i < SSL_PKEY_NUM; i++) {
2075 s->cert->pkeys[i].digest = NULL;
2076 s->cert->pkeys[i].valid_flags = 0;
2078 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2079 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2080 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2081 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2084 if (!tls1_process_sigalgs(s)) {
2085 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2086 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2092 /* get the CA RDNs */
2097 out = fopen("/tmp/vsign.der", "w");
2098 fwrite(p, 1, llen, out);
2103 if ((unsigned long)(p - d + llen) != n) {
2104 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2105 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2109 for (nc = 0; nc < llen;) {
2111 if ((l + nc + 2) > llen) {
2112 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2113 goto cont; /* netscape bugs */
2114 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2115 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2121 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2122 /* If netscape tolerance is on, ignore errors */
2123 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2126 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2127 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2133 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2134 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2135 SSL_R_CA_DN_LENGTH_MISMATCH);
2138 if (!sk_X509_NAME_push(ca_sk, xn)) {
2139 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2152 /* we should setup a certificate to return.... */
2153 s->s3->tmp.cert_req = 1;
2154 s->s3->tmp.ctype_num = ctype_num;
2155 if (s->s3->tmp.ca_names != NULL)
2156 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2157 s->s3->tmp.ca_names = ca_sk;
2163 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2167 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2169 return (X509_NAME_cmp(*a, *b));
2172 #ifndef OPENSSL_NO_TLSEXT
2173 int ssl3_get_new_session_ticket(SSL *s)
2175 int ok, al, ret = 0, ticklen;
2177 const unsigned char *p;
2180 n = s->method->ssl_get_message(s,
2181 SSL3_ST_CR_SESSION_TICKET_A,
2182 SSL3_ST_CR_SESSION_TICKET_B,
2183 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2189 /* need at least ticket_lifetime_hint + ticket length */
2190 al = SSL_AD_DECODE_ERROR;
2191 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2195 p = d = (unsigned char *)s->init_msg;
2196 n2l(p, s->session->tlsext_tick_lifetime_hint);
2198 /* ticket_lifetime_hint + ticket_length + ticket */
2199 if (ticklen + 6 != n) {
2200 al = SSL_AD_DECODE_ERROR;
2201 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2204 if (s->session->tlsext_tick) {
2205 OPENSSL_free(s->session->tlsext_tick);
2206 s->session->tlsext_ticklen = 0;
2208 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2209 if (!s->session->tlsext_tick) {
2210 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2213 memcpy(s->session->tlsext_tick, p, ticklen);
2214 s->session->tlsext_ticklen = ticklen;
2216 * There are two ways to detect a resumed ticket session. One is to set
2217 * an appropriate session ID and then the server must return a match in
2218 * ServerHello. This allows the normal client session ID matching to work
2219 * and we know much earlier that the ticket has been accepted. The
2220 * other way is to set zero length session ID when the ticket is
2221 * presented and rely on the handshake to determine session resumption.
2222 * We choose the former approach because this fits in with assumptions
2223 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2224 * SHA256 is disabled) hash of the ticket.
2226 EVP_Digest(p, ticklen,
2227 s->session->session_id, &s->session->session_id_length,
2228 EVP_sha256(), NULL);
2232 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2237 int ssl3_get_cert_status(SSL *s)
2240 unsigned long resplen, n;
2241 const unsigned char *p;
2243 n = s->method->ssl_get_message(s,
2244 SSL3_ST_CR_CERT_STATUS_A,
2245 SSL3_ST_CR_CERT_STATUS_B,
2246 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2251 /* need at least status type + length */
2252 al = SSL_AD_DECODE_ERROR;
2253 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2256 p = (unsigned char *)s->init_msg;
2257 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2258 al = SSL_AD_DECODE_ERROR;
2259 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2263 if (resplen + 4 != n) {
2264 al = SSL_AD_DECODE_ERROR;
2265 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2268 if (s->tlsext_ocsp_resp)
2269 OPENSSL_free(s->tlsext_ocsp_resp);
2270 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2271 if (!s->tlsext_ocsp_resp) {
2272 al = SSL_AD_INTERNAL_ERROR;
2273 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2276 s->tlsext_ocsp_resplen = resplen;
2277 if (s->ctx->tlsext_status_cb) {
2279 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2281 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2282 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2286 al = SSL_AD_INTERNAL_ERROR;
2287 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2293 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2298 int ssl3_get_server_done(SSL *s)
2303 /* Second to last param should be very small, like 0 :-) */
2304 n = s->method->ssl_get_message(s,
2305 SSL3_ST_CR_SRVR_DONE_A,
2306 SSL3_ST_CR_SRVR_DONE_B,
2307 SSL3_MT_SERVER_DONE, 30, &ok);
2312 /* should contain no data */
2313 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2314 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2321 int ssl3_send_client_key_exchange(SSL *s)
2325 unsigned long alg_k;
2326 #ifndef OPENSSL_NO_RSA
2328 EVP_PKEY *pkey = NULL;
2330 #ifndef OPENSSL_NO_KRB5
2332 #endif /* OPENSSL_NO_KRB5 */
2333 #ifndef OPENSSL_NO_ECDH
2334 EC_KEY *clnt_ecdh = NULL;
2335 const EC_POINT *srvr_ecpoint = NULL;
2336 EVP_PKEY *srvr_pub_pkey = NULL;
2337 unsigned char *encodedPoint = NULL;
2338 int encoded_pt_len = 0;
2339 BN_CTX *bn_ctx = NULL;
2341 unsigned char *pms = NULL;
2344 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2345 p = ssl_handshake_start(s);
2347 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2349 /* Fool emacs indentation */
2352 #ifndef OPENSSL_NO_RSA
2353 else if (alg_k & SSL_kRSA) {
2355 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2356 pms = OPENSSL_malloc(pmslen);
2360 if (s->session->sess_cert == NULL) {
2362 * We should always have a server certificate with SSL_kRSA.
2364 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2365 ERR_R_INTERNAL_ERROR);
2369 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2370 rsa = s->session->sess_cert->peer_rsa_tmp;
2373 X509_get_pubkey(s->session->
2374 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2376 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2377 || (pkey->pkey.rsa == NULL)) {
2378 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2379 ERR_R_INTERNAL_ERROR);
2382 rsa = pkey->pkey.rsa;
2383 EVP_PKEY_free(pkey);
2386 pms[0] = s->client_version >> 8;
2387 pms[1] = s->client_version & 0xff;
2388 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2392 /* Fix buf for TLS and beyond */
2393 if (s->version > SSL3_VERSION)
2395 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2397 if (s->options & SSL_OP_PKCS1_CHECK_1)
2399 if (s->options & SSL_OP_PKCS1_CHECK_2)
2403 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2404 SSL_R_BAD_RSA_ENCRYPT);
2408 /* Fix buf for TLS and beyond */
2409 if (s->version > SSL3_VERSION) {
2415 #ifndef OPENSSL_NO_KRB5
2416 else if (alg_k & SSL_kKRB5) {
2417 krb5_error_code krb5rc;
2418 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2419 /* krb5_data krb5_ap_req; */
2420 krb5_data *enc_ticket;
2421 krb5_data authenticator, *authp = NULL;
2422 EVP_CIPHER_CTX ciph_ctx;
2423 const EVP_CIPHER *enc = NULL;
2424 unsigned char iv[EVP_MAX_IV_LENGTH];
2425 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2426 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2427 int padl, outl = sizeof(epms);
2429 EVP_CIPHER_CTX_init(&ciph_ctx);
2432 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2434 # endif /* KSSL_DEBUG */
2437 # ifdef KRB5SENDAUTH
2439 authp = &authenticator;
2440 # endif /* KRB5SENDAUTH */
2442 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2443 enc = kssl_map_enc(kssl_ctx->enctype);
2448 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2449 if (krb5rc && kssl_err.text)
2450 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2453 # endif /* KSSL_DEBUG */
2456 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2457 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2462 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2463 * in place of RFC 2712 KerberosWrapper, as in:
2465 * Send ticket (copy to *p, set n = length)
2466 * n = krb5_ap_req.length;
2467 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2468 * if (krb5_ap_req.data)
2469 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2471 * Now using real RFC 2712 KerberosWrapper
2472 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2473 * Note: 2712 "opaque" types are here replaced
2474 * with a 2-byte length followed by the value.
2476 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2477 * Where "xx xx" = length bytes. Shown here with
2478 * optional authenticator omitted.
2481 /* KerberosWrapper.Ticket */
2482 s2n(enc_ticket->length, p);
2483 memcpy(p, enc_ticket->data, enc_ticket->length);
2484 p += enc_ticket->length;
2485 n = enc_ticket->length + 2;
2487 /* KerberosWrapper.Authenticator */
2488 if (authp && authp->length) {
2489 s2n(authp->length, p);
2490 memcpy(p, authp->data, authp->length);
2492 n += authp->length + 2;
2498 s2n(0, p); /* null authenticator length */
2502 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2503 pms = OPENSSL_malloc(pmslen);
2507 pms[0] = s->client_version >> 8;
2508 pms[1] = s->client_version & 0xff;
2509 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2513 * 20010420 VRS. Tried it this way; failed.
2514 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2515 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2516 * kssl_ctx->length);
2517 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2520 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2521 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2522 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen);
2523 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2525 if (outl > (int)sizeof epms) {
2526 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2527 ERR_R_INTERNAL_ERROR);
2530 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2532 /* KerberosWrapper.EncryptedPreMasterSecret */
2534 memcpy(p, epms, outl);
2537 OPENSSL_cleanse(epms, outl);
2540 #ifndef OPENSSL_NO_DH
2541 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2542 DH *dh_srvr, *dh_clnt;
2543 SESS_CERT *scert = s->session->sess_cert;
2545 if (scert == NULL) {
2546 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2547 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2548 SSL_R_UNEXPECTED_MESSAGE);
2552 if (scert->peer_dh_tmp != NULL)
2553 dh_srvr = scert->peer_dh_tmp;
2555 /* we get them from the cert */
2556 int idx = scert->peer_cert_type;
2557 EVP_PKEY *spkey = NULL;
2560 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2562 dh_srvr = EVP_PKEY_get1_DH(spkey);
2563 EVP_PKEY_free(spkey);
2565 if (dh_srvr == NULL) {
2566 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2567 ERR_R_INTERNAL_ERROR);
2571 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2572 /* Use client certificate key */
2573 EVP_PKEY *clkey = s->cert->key->privatekey;
2576 dh_clnt = EVP_PKEY_get1_DH(clkey);
2577 if (dh_clnt == NULL) {
2578 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2579 ERR_R_INTERNAL_ERROR);
2583 /* generate a new random key */
2584 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2588 if (!DH_generate_key(dh_clnt)) {
2589 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2595 pmslen = DH_size(dh_clnt);
2596 pms = OPENSSL_malloc(pmslen);
2601 * use the 'p' output buffer for the DH key, but make sure to
2602 * clear it out afterwards
2605 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2606 if (scert->peer_dh_tmp == NULL)
2610 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2615 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2618 /* send off the data */
2619 n = BN_num_bytes(dh_clnt->pub_key);
2621 BN_bn2bin(dh_clnt->pub_key, p);
2627 /* perhaps clean things up a bit EAY EAY EAY EAY */
2631 #ifndef OPENSSL_NO_ECDH
2632 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2633 const EC_GROUP *srvr_group = NULL;
2635 int ecdh_clnt_cert = 0;
2638 if (s->session->sess_cert == NULL) {
2639 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2640 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2641 SSL_R_UNEXPECTED_MESSAGE);
2646 * Did we send out the client's ECDH share for use in premaster
2647 * computation as part of client certificate? If so, set
2648 * ecdh_clnt_cert to 1.
2650 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2652 * XXX: For now, we do not support client
2653 * authentication using ECDH certificates.
2654 * To add such support, one needs to add
2655 * code that checks for appropriate
2656 * conditions and sets ecdh_clnt_cert to 1.
2657 * For example, the cert have an ECC
2658 * key on the same curve as the server's
2659 * and the key should be authorized for
2662 * One also needs to add code in ssl3_connect
2663 * to skip sending the certificate verify
2666 * if ((s->cert->key->privatekey != NULL) &&
2667 * (s->cert->key->privatekey->type ==
2668 * EVP_PKEY_EC) && ...)
2669 * ecdh_clnt_cert = 1;
2673 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2674 tkey = s->session->sess_cert->peer_ecdh_tmp;
2676 /* Get the Server Public Key from Cert */
2678 X509_get_pubkey(s->session->
2679 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2680 if ((srvr_pub_pkey == NULL)
2681 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2682 || (srvr_pub_pkey->pkey.ec == NULL)) {
2683 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2684 ERR_R_INTERNAL_ERROR);
2688 tkey = srvr_pub_pkey->pkey.ec;
2691 srvr_group = EC_KEY_get0_group(tkey);
2692 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2694 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2695 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2696 ERR_R_INTERNAL_ERROR);
2700 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2701 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2702 ERR_R_MALLOC_FAILURE);
2706 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2707 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2710 if (ecdh_clnt_cert) {
2712 * Reuse key info from our certificate We only need our
2713 * private key to perform the ECDH computation.
2715 const BIGNUM *priv_key;
2716 tkey = s->cert->key->privatekey->pkey.ec;
2717 priv_key = EC_KEY_get0_private_key(tkey);
2718 if (priv_key == NULL) {
2719 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2720 ERR_R_MALLOC_FAILURE);
2723 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2724 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2728 /* Generate a new ECDH key pair */
2729 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2730 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2737 * use the 'p' output buffer for the ECDH key, but make sure to
2738 * clear it out afterwards
2741 field_size = EC_GROUP_get_degree(srvr_group);
2742 if (field_size <= 0) {
2743 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2746 pmslen = (field_size + 7) / 8;
2747 pms = OPENSSL_malloc(pmslen);
2750 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2751 if (n <= 0 || pmslen != (size_t)n) {
2752 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2756 if (ecdh_clnt_cert) {
2757 /* Send empty client key exch message */
2761 * First check the size of encoding and allocate memory
2765 EC_POINT_point2oct(srvr_group,
2766 EC_KEY_get0_public_key(clnt_ecdh),
2767 POINT_CONVERSION_UNCOMPRESSED,
2770 encodedPoint = (unsigned char *)
2771 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2772 bn_ctx = BN_CTX_new();
2773 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2774 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2775 ERR_R_MALLOC_FAILURE);
2779 /* Encode the public key */
2780 n = EC_POINT_point2oct(srvr_group,
2781 EC_KEY_get0_public_key(clnt_ecdh),
2782 POINT_CONVERSION_UNCOMPRESSED,
2783 encodedPoint, encoded_pt_len, bn_ctx);
2785 *p = n; /* length of encoded point */
2786 /* Encoded point will be copied here */
2788 /* copy the point */
2789 memcpy((unsigned char *)p, encodedPoint, n);
2790 /* increment n to account for length field */
2794 /* Free allocated memory */
2795 BN_CTX_free(bn_ctx);
2796 if (encodedPoint != NULL)
2797 OPENSSL_free(encodedPoint);
2798 if (clnt_ecdh != NULL)
2799 EC_KEY_free(clnt_ecdh);
2800 EVP_PKEY_free(srvr_pub_pkey);
2802 #endif /* !OPENSSL_NO_ECDH */
2803 else if (alg_k & SSL_kGOST) {
2804 /* GOST key exchange message creation */
2805 EVP_PKEY_CTX *pkey_ctx;
2808 unsigned int md_len;
2810 unsigned char shared_ukm[32], tmp[256];
2811 EVP_MD_CTX *ukm_hash;
2815 pms = OPENSSL_malloc(pmslen);
2820 * Get server sertificate PKEY and create ctx from it
2824 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2828 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2830 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2831 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2835 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2836 X509_get_pubkey(peer_cert), NULL);
2838 * If we have send a certificate, and certificate key
2840 * * parameters match those of server certificate, use
2841 * certificate key for key exchange
2844 /* Otherwise, generate ephemeral key pair */
2846 EVP_PKEY_encrypt_init(pkey_ctx);
2847 /* Generate session key */
2848 RAND_bytes(pms, pmslen);
2850 * If we have client certificate, use its secret as peer key
2852 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2853 if (EVP_PKEY_derive_set_peer
2854 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2856 * If there was an error - just ignore it. Ephemeral key
2863 * Compute shared IV and store it in algorithm-specific context
2866 ukm_hash = EVP_MD_CTX_create();
2867 EVP_DigestInit(ukm_hash,
2868 EVP_get_digestbynid(NID_id_GostR3411_94));
2869 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2871 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2873 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2874 EVP_MD_CTX_destroy(ukm_hash);
2875 if (EVP_PKEY_CTX_ctrl
2876 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2878 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2882 /* Make GOST keytransport blob message */
2884 * Encapsulate it into sequence
2886 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2888 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2889 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2893 if (msglen >= 0x80) {
2895 *(p++) = msglen & 0xff;
2898 *(p++) = msglen & 0xff;
2901 memcpy(p, tmp, msglen);
2902 /* Check if pubkey from client certificate was used */
2903 if (EVP_PKEY_CTX_ctrl
2904 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2905 /* Set flag "skip certificate verify" */
2906 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2908 EVP_PKEY_CTX_free(pkey_ctx);
2909 EVP_PKEY_free(pub_key);
2912 #ifndef OPENSSL_NO_SRP
2913 else if (alg_k & SSL_kSRP) {
2914 if (s->srp_ctx.A != NULL) {
2915 /* send off the data */
2916 n = BN_num_bytes(s->srp_ctx.A);
2918 BN_bn2bin(s->srp_ctx.A, p);
2921 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2922 ERR_R_INTERNAL_ERROR);
2925 if (s->session->srp_username != NULL)
2926 OPENSSL_free(s->session->srp_username);
2927 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2928 if (s->session->srp_username == NULL) {
2929 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2930 ERR_R_MALLOC_FAILURE);
2935 #ifndef OPENSSL_NO_PSK
2936 else if (alg_k & SSL_kPSK) {
2938 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2939 * \0-terminated identity. The last byte is for us for simulating
2942 char identity[PSK_MAX_IDENTITY_LEN + 2];
2943 size_t identity_len;
2944 unsigned char *t = NULL;
2945 unsigned int psk_len = 0;
2949 if (s->psk_client_callback == NULL) {
2950 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2951 SSL_R_PSK_NO_CLIENT_CB);
2955 memset(identity, 0, sizeof(identity));
2956 /* Allocate maximum size buffer */
2957 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2958 pms = OPENSSL_malloc(pmslen);
2962 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2963 identity, sizeof(identity) - 1,
2965 if (psk_len > PSK_MAX_PSK_LEN) {
2966 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2967 ERR_R_INTERNAL_ERROR);
2969 } else if (psk_len == 0) {
2970 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2971 SSL_R_PSK_IDENTITY_NOT_FOUND);
2974 /* Change pmslen to real length */
2975 pmslen = 2 + psk_len + 2 + psk_len;
2976 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2977 identity_len = strlen(identity);
2978 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2979 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2980 ERR_R_INTERNAL_ERROR);
2983 /* create PSK pre_master_secret */
2985 memmove(pms + psk_len + 4, pms, psk_len);
2987 memset(t, 0, psk_len);
2991 if (s->session->psk_identity_hint != NULL)
2992 OPENSSL_free(s->session->psk_identity_hint);
2993 s->session->psk_identity_hint =
2994 BUF_strdup(s->ctx->psk_identity_hint);
2995 if (s->ctx->psk_identity_hint != NULL
2996 && s->session->psk_identity_hint == NULL) {
2997 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2998 ERR_R_MALLOC_FAILURE);
3002 if (s->session->psk_identity != NULL)
3003 OPENSSL_free(s->session->psk_identity);
3004 s->session->psk_identity = BUF_strdup(identity);
3005 if (s->session->psk_identity == NULL) {
3006 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3007 ERR_R_MALLOC_FAILURE);
3011 s2n(identity_len, p);
3012 memcpy(p, identity, identity_len);
3013 n = 2 + identity_len;
3016 OPENSSL_cleanse(identity, sizeof(identity));
3018 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3024 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3025 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3029 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
3030 s->state = SSL3_ST_CW_KEY_EXCH_B;
3033 /* SSL3_ST_CW_KEY_EXCH_B */
3034 n = ssl_do_write(s);
3035 #ifndef OPENSSL_NO_SRP
3037 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3039 * If everything written generate master key: no need to save PMS as
3040 * SRP_generate_client_master_secret generates it internally.
3043 if ((s->session->master_key_length =
3044 SRP_generate_client_master_secret(s,
3045 s->session->master_key)) <
3047 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3048 ERR_R_INTERNAL_ERROR);
3054 /* If we haven't written everything save PMS */
3057 s->cert->pmslen = pmslen;
3059 /* If we don't have a PMS restore */
3062 pmslen = s->cert->pmslen;
3065 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3066 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3069 s->session->master_key_length =
3070 s->method->ssl3_enc->generate_master_secret(s,
3072 session->master_key,
3074 OPENSSL_cleanse(pms, pmslen);
3076 s->cert->pms = NULL;
3080 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3081 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
3084 OPENSSL_cleanse(pms, pmslen);
3086 s->cert->pms = NULL;
3088 #ifndef OPENSSL_NO_ECDH
3089 BN_CTX_free(bn_ctx);
3090 if (encodedPoint != NULL)
3091 OPENSSL_free(encodedPoint);
3092 if (clnt_ecdh != NULL)
3093 EC_KEY_free(clnt_ecdh);
3094 EVP_PKEY_free(srvr_pub_pkey);
3099 int ssl3_send_client_verify(SSL *s)
3102 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3104 EVP_PKEY_CTX *pctx = NULL;
3110 EVP_MD_CTX_init(&mctx);
3112 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3113 p = ssl_handshake_start(s);
3114 pkey = s->cert->key->privatekey;
3115 /* Create context from key and test if sha1 is allowed as digest */
3116 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3117 EVP_PKEY_sign_init(pctx);
3118 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3119 if (!SSL_USE_SIGALGS(s))
3120 s->method->ssl3_enc->cert_verify_mac(s,
3123 [MD5_DIGEST_LENGTH]));
3128 * For TLS v1.2 send signature algorithm and signature using agreed
3129 * digest and cached handshake records.
3131 if (SSL_USE_SIGALGS(s)) {
3134 const EVP_MD *md = s->cert->key->digest;
3135 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3136 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3137 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3142 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3145 if (!EVP_SignInit_ex(&mctx, md, NULL)
3146 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3147 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3148 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3153 if (!ssl3_digest_cached_records(s))
3156 #ifndef OPENSSL_NO_RSA
3157 if (pkey->type == EVP_PKEY_RSA) {
3158 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3159 if (RSA_sign(NID_md5_sha1, data,
3160 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3161 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3162 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3169 #ifndef OPENSSL_NO_DSA
3170 if (pkey->type == EVP_PKEY_DSA) {
3171 if (!DSA_sign(pkey->save_type,
3172 &(data[MD5_DIGEST_LENGTH]),
3173 SHA_DIGEST_LENGTH, &(p[2]),
3174 (unsigned int *)&j, pkey->pkey.dsa)) {
3175 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3182 #ifndef OPENSSL_NO_ECDSA
3183 if (pkey->type == EVP_PKEY_EC) {
3184 if (!ECDSA_sign(pkey->save_type,
3185 &(data[MD5_DIGEST_LENGTH]),
3186 SHA_DIGEST_LENGTH, &(p[2]),
3187 (unsigned int *)&j, pkey->pkey.ec)) {
3188 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3195 if (pkey->type == NID_id_GostR3410_94
3196 || pkey->type == NID_id_GostR3410_2001) {
3197 unsigned char signbuf[64];
3199 size_t sigsize = 64;
3200 s->method->ssl3_enc->cert_verify_mac(s,
3201 NID_id_GostR3411_94, data);
3202 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3203 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3206 for (i = 63, j = 0; i >= 0; j++, i--) {
3207 p[2 + j] = signbuf[i];
3212 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3215 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
3216 s->state = SSL3_ST_CW_CERT_VRFY_B;
3218 EVP_MD_CTX_cleanup(&mctx);
3219 EVP_PKEY_CTX_free(pctx);
3220 return ssl_do_write(s);
3222 EVP_MD_CTX_cleanup(&mctx);
3223 EVP_PKEY_CTX_free(pctx);
3228 * Check a certificate can be used for client authentication. Currently check
3229 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3230 * certificates can be used and optionally checks suitability for Suite B.
3232 static int ssl3_check_client_certificate(SSL *s)
3234 unsigned long alg_k;
3235 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3237 /* If no suitable signature algorithm can't use certificate */
3238 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3241 * If strict mode check suitability of chain before using it. This also
3242 * adjusts suite B digest if necessary.
3244 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3245 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3247 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3248 /* See if we can use client certificate for fixed DH */
3249 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3250 SESS_CERT *scert = s->session->sess_cert;
3251 int i = scert->peer_cert_type;
3252 EVP_PKEY *clkey = NULL, *spkey = NULL;
3253 clkey = s->cert->key->privatekey;
3254 /* If client key not DH assume it can be used */
3255 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3258 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3260 /* Compare server and client parameters */
3261 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3262 EVP_PKEY_free(spkey);
3266 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3271 int ssl3_send_client_certificate(SSL *s)
3274 EVP_PKEY *pkey = NULL;
3277 if (s->state == SSL3_ST_CW_CERT_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);
3289 s->rwstate = SSL_NOTHING;
3291 if (ssl3_check_client_certificate(s))
3292 s->state = SSL3_ST_CW_CERT_C;
3294 s->state = SSL3_ST_CW_CERT_B;
3297 /* We need to get a client cert */
3298 if (s->state == SSL3_ST_CW_CERT_B) {
3300 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3301 * return(-1); We then get retied later
3304 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3306 s->rwstate = SSL_X509_LOOKUP;
3309 s->rwstate = SSL_NOTHING;
3310 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3311 s->state = SSL3_ST_CW_CERT_B;
3312 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3314 } else if (i == 1) {
3316 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3317 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3323 EVP_PKEY_free(pkey);
3324 if (i && !ssl3_check_client_certificate(s))
3327 if (s->version == SSL3_VERSION) {
3328 s->s3->tmp.cert_req = 0;
3329 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3332 s->s3->tmp.cert_req = 2;
3336 /* Ok, we have a cert */
3337 s->state = SSL3_ST_CW_CERT_C;
3340 if (s->state == SSL3_ST_CW_CERT_C) {
3341 s->state = SSL3_ST_CW_CERT_D;
3342 if (!ssl3_output_cert_chain(s,
3343 (s->s3->tmp.cert_req ==
3344 2) ? NULL : s->cert->key)) {
3345 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3346 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3350 /* SSL3_ST_CW_CERT_D */
3351 return ssl_do_write(s);
3354 #define has_bits(i,m) (((i)&(m)) == (m))
3356 int ssl3_check_cert_and_algorithm(SSL *s)
3360 EVP_PKEY *pkey = NULL;
3362 #ifndef OPENSSL_NO_RSA
3365 #ifndef OPENSSL_NO_DH
3369 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3370 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3372 /* we don't have a certificate */
3373 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3376 sc = s->session->sess_cert;
3378 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3381 #ifndef OPENSSL_NO_RSA
3382 rsa = s->session->sess_cert->peer_rsa_tmp;
3384 #ifndef OPENSSL_NO_DH
3385 dh = s->session->sess_cert->peer_dh_tmp;
3388 /* This is the passed certificate */
3390 idx = sc->peer_cert_type;
3391 #ifndef OPENSSL_NO_ECDH
3392 if (idx == SSL_PKEY_ECC) {
3393 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3395 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3400 } else if (alg_a & SSL_aECDSA) {
3401 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3402 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3404 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3405 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3409 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3410 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3411 EVP_PKEY_free(pkey);
3413 /* Check that we have a certificate if we require one */
3414 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3415 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3416 SSL_R_MISSING_RSA_SIGNING_CERT);
3419 #ifndef OPENSSL_NO_DSA
3420 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3421 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3422 SSL_R_MISSING_DSA_SIGNING_CERT);
3426 #ifndef OPENSSL_NO_RSA
3427 if ((alg_k & SSL_kRSA) &&
3428 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3429 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3430 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3434 #ifndef OPENSSL_NO_DH
3435 if ((alg_k & SSL_kDHE) &&
3436 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3437 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3439 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3440 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3441 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3442 SSL_R_MISSING_DH_RSA_CERT);
3445 # ifndef OPENSSL_NO_DSA
3446 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3447 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3448 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3449 SSL_R_MISSING_DH_DSA_CERT);
3455 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3456 #ifndef OPENSSL_NO_RSA
3457 if (alg_k & SSL_kRSA) {
3459 || RSA_size(rsa) * 8 >
3460 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3461 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3462 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3467 #ifndef OPENSSL_NO_DH
3468 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3470 || DH_size(dh) * 8 >
3471 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3472 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3473 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3479 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3480 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3486 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3492 * Check to see if handshake is full or resumed. Usually this is just a case
3493 * of checking to see if a cache hit has occurred. In the case of session
3494 * tickets we have to check the next message to be sure.
3497 #ifndef OPENSSL_NO_TLSEXT
3498 # ifndef OPENSSL_NO_NEXTPROTONEG
3499 int ssl3_send_next_proto(SSL *s)
3501 unsigned int len, padding_len;
3504 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3505 len = s->next_proto_negotiated_len;
3506 padding_len = 32 - ((len + 2) % 32);
3507 d = (unsigned char *)s->init_buf->data;
3509 memcpy(d + 5, s->next_proto_negotiated, len);
3510 d[5 + len] = padding_len;
3511 memset(d + 6 + len, 0, padding_len);
3512 *(d++) = SSL3_MT_NEXT_PROTO;
3513 l2n3(2 + len + padding_len, d);
3514 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3515 s->init_num = 4 + 2 + len + padding_len;
3519 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3524 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3527 #ifndef OPENSSL_NO_ENGINE
3528 if (s->ctx->client_cert_engine) {
3529 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3530 SSL_get_client_CA_list(s),
3531 px509, ppkey, NULL, NULL, NULL);
3536 if (s->ctx->client_cert_cb)
3537 i = s->ctx->client_cert_cb(s, px509, ppkey);