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);
172 #ifndef OPENSSL_NO_SSL3_METHOD
173 static const SSL_METHOD *ssl3_get_client_method(int ver)
175 if (ver == SSL3_VERSION)
176 return (SSLv3_client_method());
181 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
182 ssl_undefined_function,
183 ssl3_connect, ssl3_get_client_method)
185 int ssl3_connect(SSL *s)
188 unsigned long Time = (unsigned long)time(NULL);
189 void (*cb) (const SSL *ssl, int type, int val) = NULL;
191 int new_state, state, skip = 0;
193 RAND_add(&Time, sizeof(Time), 0);
197 if (s->info_callback != NULL)
198 cb = s->info_callback;
199 else if (s->ctx->info_callback != NULL)
200 cb = s->ctx->info_callback;
203 if (!SSL_in_init(s) || SSL_in_before(s))
206 #ifndef OPENSSL_NO_HEARTBEATS
208 * If we're awaiting a HeartbeatResponse, pretend we already got and
209 * don't await it anymore, because Heartbeats don't make sense during
212 if (s->tlsext_hb_pending) {
213 s->tlsext_hb_pending = 0;
222 case SSL_ST_RENEGOTIATE:
224 s->state = SSL_ST_CONNECT;
225 s->ctx->stats.sess_connect_renegotiate++;
229 case SSL_ST_BEFORE | SSL_ST_CONNECT:
230 case SSL_ST_OK | SSL_ST_CONNECT:
234 cb(s, SSL_CB_HANDSHAKE_START, 1);
236 if ((s->version & 0xff00) != 0x0300) {
237 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
242 /* s->version=SSL3_VERSION; */
243 s->type = SSL_ST_CONNECT;
245 if (s->init_buf == NULL) {
246 if ((buf = BUF_MEM_new()) == NULL) {
250 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
258 if (!ssl3_setup_buffers(s)) {
263 /* setup buffing BIO */
264 if (!ssl_init_wbio_buffer(s, 0)) {
269 /* don't push the buffering BIO quite yet */
271 ssl3_init_finished_mac(s);
273 s->state = SSL3_ST_CW_CLNT_HELLO_A;
274 s->ctx->stats.sess_connect++;
276 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
278 * Should have been reset by ssl3_get_finished, too.
280 s->s3->change_cipher_spec = 0;
283 case SSL3_ST_CW_CLNT_HELLO_A:
284 case SSL3_ST_CW_CLNT_HELLO_B:
287 ret = ssl3_client_hello(s);
290 s->state = SSL3_ST_CR_SRVR_HELLO_A;
293 /* turn on buffering for the next lot of output */
294 if (s->bbio != s->wbio)
295 s->wbio = BIO_push(s->bbio, s->wbio);
299 case SSL3_ST_CR_SRVR_HELLO_A:
300 case SSL3_ST_CR_SRVR_HELLO_B:
301 ret = ssl3_get_server_hello(s);
306 s->state = SSL3_ST_CR_FINISHED_A;
307 #ifndef OPENSSL_NO_TLSEXT
308 if (s->tlsext_ticket_expected) {
309 /* receive renewed session ticket */
310 s->state = SSL3_ST_CR_SESSION_TICKET_A;
314 s->state = SSL3_ST_CR_CERT_A;
318 case SSL3_ST_CR_CERT_A:
319 case SSL3_ST_CR_CERT_B:
320 /* Check if it is anon DH/ECDH, SRP auth */
324 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
325 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
326 ret = ssl3_get_server_certificate(s);
329 #ifndef OPENSSL_NO_TLSEXT
330 if (s->tlsext_status_expected)
331 s->state = SSL3_ST_CR_CERT_STATUS_A;
333 s->state = SSL3_ST_CR_KEY_EXCH_A;
336 s->state = SSL3_ST_CR_KEY_EXCH_A;
342 s->state = SSL3_ST_CR_KEY_EXCH_A;
347 case SSL3_ST_CR_KEY_EXCH_A:
348 case SSL3_ST_CR_KEY_EXCH_B:
349 ret = ssl3_get_key_exchange(s);
352 s->state = SSL3_ST_CR_CERT_REQ_A;
356 * at this point we check that we have the required stuff from
359 if (!ssl3_check_cert_and_algorithm(s)) {
365 case SSL3_ST_CR_CERT_REQ_A:
366 case SSL3_ST_CR_CERT_REQ_B:
367 ret = ssl3_get_certificate_request(s);
370 s->state = SSL3_ST_CR_SRVR_DONE_A;
374 case SSL3_ST_CR_SRVR_DONE_A:
375 case SSL3_ST_CR_SRVR_DONE_B:
376 ret = ssl3_get_server_done(s);
379 #ifndef OPENSSL_NO_SRP
380 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
381 if ((ret = SRP_Calc_A_param(s)) <= 0) {
382 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
383 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
388 if (s->s3->tmp.cert_req)
389 s->state = SSL3_ST_CW_CERT_A;
391 s->state = SSL3_ST_CW_KEY_EXCH_A;
396 case SSL3_ST_CW_CERT_A:
397 case SSL3_ST_CW_CERT_B:
398 case SSL3_ST_CW_CERT_C:
399 case SSL3_ST_CW_CERT_D:
400 ret = ssl3_send_client_certificate(s);
403 s->state = SSL3_ST_CW_KEY_EXCH_A;
407 case SSL3_ST_CW_KEY_EXCH_A:
408 case SSL3_ST_CW_KEY_EXCH_B:
409 ret = ssl3_send_client_key_exchange(s);
413 * EAY EAY EAY need to check for DH fix cert sent back
416 * For TLS, cert_req is set to 2, so a cert chain of nothing is
417 * sent, but no verify packet is sent
420 * XXX: For now, we do not support client authentication in ECDH
421 * cipher suites with ECDH (rather than ECDSA) certificates. We
422 * need to skip the certificate verify message when client's
423 * ECDH public key is sent inside the client certificate.
425 if (s->s3->tmp.cert_req == 1) {
426 s->state = SSL3_ST_CW_CERT_VRFY_A;
428 s->state = SSL3_ST_CW_CHANGE_A;
430 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
431 s->state = SSL3_ST_CW_CHANGE_A;
437 case SSL3_ST_CW_CERT_VRFY_A:
438 case SSL3_ST_CW_CERT_VRFY_B:
439 ret = ssl3_send_client_verify(s);
442 s->state = SSL3_ST_CW_CHANGE_A;
446 case SSL3_ST_CW_CHANGE_A:
447 case SSL3_ST_CW_CHANGE_B:
448 ret = ssl3_send_change_cipher_spec(s,
450 SSL3_ST_CW_CHANGE_B);
454 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
455 s->state = SSL3_ST_CW_FINISHED_A;
457 if (s->s3->next_proto_neg_seen)
458 s->state = SSL3_ST_CW_NEXT_PROTO_A;
460 s->state = SSL3_ST_CW_FINISHED_A;
464 s->session->cipher = s->s3->tmp.new_cipher;
465 #ifdef OPENSSL_NO_COMP
466 s->session->compress_meth = 0;
468 if (s->s3->tmp.new_compression == NULL)
469 s->session->compress_meth = 0;
471 s->session->compress_meth = s->s3->tmp.new_compression->id;
473 if (!s->method->ssl3_enc->setup_key_block(s)) {
478 if (!s->method->ssl3_enc->change_cipher_state(s,
479 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
487 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
488 case SSL3_ST_CW_NEXT_PROTO_A:
489 case SSL3_ST_CW_NEXT_PROTO_B:
490 ret = ssl3_send_next_proto(s);
493 s->state = SSL3_ST_CW_FINISHED_A;
497 case SSL3_ST_CW_FINISHED_A:
498 case SSL3_ST_CW_FINISHED_B:
499 ret = ssl3_send_finished(s,
500 SSL3_ST_CW_FINISHED_A,
501 SSL3_ST_CW_FINISHED_B,
503 ssl3_enc->client_finished_label,
505 ssl3_enc->client_finished_label_len);
508 s->state = SSL3_ST_CW_FLUSH;
511 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
513 s->s3->tmp.next_state = SSL_ST_OK;
514 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
515 s->state = SSL_ST_OK;
516 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
517 s->s3->delay_buf_pop_ret = 0;
520 #ifndef OPENSSL_NO_TLSEXT
522 * Allow NewSessionTicket if ticket expected
524 if (s->tlsext_ticket_expected)
525 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
529 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
534 #ifndef OPENSSL_NO_TLSEXT
535 case SSL3_ST_CR_SESSION_TICKET_A:
536 case SSL3_ST_CR_SESSION_TICKET_B:
537 ret = ssl3_get_new_session_ticket(s);
540 s->state = SSL3_ST_CR_FINISHED_A;
544 case SSL3_ST_CR_CERT_STATUS_A:
545 case SSL3_ST_CR_CERT_STATUS_B:
546 ret = ssl3_get_cert_status(s);
549 s->state = SSL3_ST_CR_KEY_EXCH_A;
554 case SSL3_ST_CR_FINISHED_A:
555 case SSL3_ST_CR_FINISHED_B:
556 s->s3->flags |= SSL3_FLAGS_CCS_OK;
557 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
558 SSL3_ST_CR_FINISHED_B);
563 s->state = SSL3_ST_CW_CHANGE_A;
565 s->state = SSL_ST_OK;
569 case SSL3_ST_CW_FLUSH:
570 s->rwstate = SSL_WRITING;
571 if (BIO_flush(s->wbio) <= 0) {
575 s->rwstate = SSL_NOTHING;
576 s->state = s->s3->tmp.next_state;
580 /* clean a few things up */
581 ssl3_cleanup_key_block(s);
583 if (s->init_buf != NULL) {
584 BUF_MEM_free(s->init_buf);
589 * If we are not 'joining' the last two packets, remove the
592 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
593 ssl_free_wbio_buffer(s);
594 /* else do it later in ssl3_write */
600 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
602 s->ctx->stats.sess_hit++;
606 s->handshake_func = ssl3_connect;
607 s->ctx->stats.sess_connect_good++;
610 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
616 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
622 /* did we do anything */
623 if (!s->s3->tmp.reuse_message && !skip) {
625 if ((ret = BIO_flush(s->wbio)) <= 0)
629 if ((cb != NULL) && (s->state != state)) {
630 new_state = s->state;
632 cb(s, SSL_CB_CONNECT_LOOP, 1);
633 s->state = new_state;
643 cb(s, SSL_CB_CONNECT_EXIT, ret);
647 int ssl3_client_hello(SSL *s)
650 unsigned char *p, *d;
654 #ifndef OPENSSL_NO_COMP
659 buf = (unsigned char *)s->init_buf->data;
660 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
661 SSL_SESSION *sess = s->session;
662 if ((sess == NULL) ||
663 (sess->ssl_version != s->version) ||
664 !sess->session_id_length || (sess->not_resumable)) {
665 if (!ssl_get_new_session(s, 0))
668 if (s->method->version == DTLS_ANY_VERSION) {
669 /* Determine which DTLS version to use */
670 int options = s->options;
671 /* If DTLS 1.2 disabled correct the version number */
672 if (options & SSL_OP_NO_DTLSv1_2) {
673 if (tls1_suiteb(s)) {
674 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
675 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
679 * Disabling all versions is silly: return an error.
681 if (options & SSL_OP_NO_DTLSv1) {
682 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
686 * Update method so we don't use any DTLS 1.2 features.
688 s->method = DTLSv1_client_method();
689 s->version = DTLS1_VERSION;
692 * We only support one version: update method
694 if (options & SSL_OP_NO_DTLSv1)
695 s->method = DTLSv1_2_client_method();
696 s->version = DTLS1_2_VERSION;
698 s->client_version = s->version;
700 /* else use the pre-loaded session */
702 p = s->s3->client_random;
705 * for DTLS if client_random is initialized, reuse it, we are
706 * required to use same upon reply to HelloVerify
708 if (SSL_IS_DTLS(s)) {
711 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
721 ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
723 /* Do the message type and length last */
724 d = p = ssl_handshake_start(s);
727 * version indicates the negotiated version: for example from
728 * an SSLv2/v3 compatible client hello). The client_version
729 * field is the maximum version we permit and it is also
730 * used in RSA encrypted premaster secrets. Some servers can
731 * choke if we initially report a higher version then
732 * renegotiate to a lower one in the premaster secret. This
733 * didn't happen with TLS 1.0 as most servers supported it
734 * but it can with TLS 1.1 or later if the server only supports
737 * Possible scenario with previous logic:
738 * 1. Client hello indicates TLS 1.2
739 * 2. Server hello says TLS 1.0
740 * 3. RSA encrypted premaster secret uses 1.2.
741 * 4. Handhaked proceeds using TLS 1.0.
742 * 5. Server sends hello request to renegotiate.
743 * 6. Client hello indicates TLS v1.0 as we now
744 * know that is maximum server supports.
745 * 7. Server chokes on RSA encrypted premaster secret
746 * containing version 1.0.
748 * For interoperability it should be OK to always use the
749 * maximum version we support in client hello and then rely
750 * on the checking of version to ensure the servers isn't
751 * being inconsistent: for example initially negotiating with
752 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
753 * client_version in client hello and not resetting it to
754 * the negotiated version.
757 *(p++) = s->version >> 8;
758 *(p++) = s->version & 0xff;
759 s->client_version = s->version;
761 *(p++) = s->client_version >> 8;
762 *(p++) = s->client_version & 0xff;
766 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
767 p += SSL3_RANDOM_SIZE;
773 i = s->session->session_id_length;
776 if (i > (int)sizeof(s->session->session_id)) {
777 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
780 memcpy(p, s->session->session_id, i);
784 /* cookie stuff for DTLS */
785 if (SSL_IS_DTLS(s)) {
786 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
787 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
790 *(p++) = s->d1->cookie_len;
791 memcpy(p, s->d1->cookie, s->d1->cookie_len);
792 p += s->d1->cookie_len;
795 /* Ciphers supported */
796 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
798 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
801 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
803 * Some servers hang if client hello > 256 bytes as hack workaround
804 * chop number of supported ciphers to keep it well below this if we
807 if (TLS1_get_version(s) >= TLS1_2_VERSION
808 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
809 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
815 #ifdef OPENSSL_NO_COMP
819 if ((s->options & SSL_OP_NO_COMPRESSION)
820 || !s->ctx->comp_methods)
823 j = sk_SSL_COMP_num(s->ctx->comp_methods);
825 for (i = 0; i < j; i++) {
826 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
830 *(p++) = 0; /* Add the NULL method */
832 #ifndef OPENSSL_NO_TLSEXT
834 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
835 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
839 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
841 ssl3_send_alert(s, SSL3_AL_FATAL, al);
842 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
848 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
849 s->state = SSL3_ST_CW_CLNT_HELLO_B;
852 /* SSL3_ST_CW_CLNT_HELLO_B */
853 return ssl_do_write(s);
858 int ssl3_get_server_hello(SSL *s)
860 STACK_OF(SSL_CIPHER) *sk;
863 unsigned char *p, *d;
864 int i, al = SSL_AD_INTERNAL_ERROR, ok;
867 #ifndef OPENSSL_NO_COMP
871 * Hello verify request and/or server hello version may not match so set
872 * first packet if we're negotiating version.
877 n = s->method->ssl_get_message(s,
878 SSL3_ST_CR_SRVR_HELLO_A,
879 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
884 if (SSL_IS_DTLS(s)) {
886 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
887 if (s->d1->send_cookie == 0) {
888 s->s3->tmp.reuse_message = 1;
890 } else { /* already sent a cookie */
892 al = SSL_AD_UNEXPECTED_MESSAGE;
893 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
899 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
900 al = SSL_AD_UNEXPECTED_MESSAGE;
901 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
905 d = p = (unsigned char *)s->init_msg;
906 if (s->method->version == DTLS_ANY_VERSION) {
907 /* Work out correct protocol version to use */
908 int hversion = (p[0] << 8) | p[1];
909 int options = s->options;
910 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
911 s->method = DTLSv1_2_client_method();
912 else if (tls1_suiteb(s)) {
913 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
914 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
915 s->version = hversion;
916 al = SSL_AD_PROTOCOL_VERSION;
918 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
919 s->method = DTLSv1_client_method();
921 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
922 s->version = hversion;
923 al = SSL_AD_PROTOCOL_VERSION;
926 s->version = s->method->version;
929 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
930 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
931 s->version = (s->version & 0xff00) | p[1];
932 al = SSL_AD_PROTOCOL_VERSION;
937 /* load the server hello data */
938 /* load the server random */
939 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
940 p += SSL3_RANDOM_SIZE;
944 /* get the session-id */
947 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
948 al = SSL_AD_ILLEGAL_PARAMETER;
949 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
952 #ifndef OPENSSL_NO_TLSEXT
954 * check if we want to resume the session based on external pre-shared
957 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
958 SSL_CIPHER *pref_cipher = NULL;
959 s->session->master_key_length = sizeof(s->session->master_key);
960 if (s->tls_session_secret_cb(s, s->session->master_key,
961 &s->session->master_key_length,
963 s->tls_session_secret_cb_arg)) {
964 s->session->cipher = pref_cipher ?
965 pref_cipher : ssl_get_cipher_by_char(s, p + j);
969 #endif /* OPENSSL_NO_TLSEXT */
971 if (!s->hit && j != 0 && j == s->session->session_id_length
972 && memcmp(p, s->session->session_id, j) == 0) {
973 if (s->sid_ctx_length != s->session->sid_ctx_length
974 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
975 /* actually a client application bug */
976 al = SSL_AD_ILLEGAL_PARAMETER;
977 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
978 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
983 /* a miss or crap from the other end */
986 * If we were trying for session-id reuse, make a new SSL_SESSION so
987 * we don't stuff up other people
989 if (s->session->session_id_length > 0) {
990 if (!ssl_get_new_session(s, 0)) {
994 s->session->session_id_length = j;
995 memcpy(s->session->session_id, p, j); /* j could be 0 */
998 c = ssl_get_cipher_by_char(s, p);
1000 /* unknown cipher */
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1006 * If it is a disabled cipher we didn't send it in client hello, so
1009 if (c->algorithm_ssl & ct->mask_ssl ||
1010 c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a) {
1011 al = SSL_AD_ILLEGAL_PARAMETER;
1012 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1015 p += ssl_put_cipher_by_char(s, NULL, NULL);
1017 sk = ssl_get_ciphers_by_id(s);
1018 i = sk_SSL_CIPHER_find(sk, c);
1020 /* we did not say we would use this cipher */
1021 al = SSL_AD_ILLEGAL_PARAMETER;
1022 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1027 * Depending on the session caching (internal/external), the cipher
1028 * and/or cipher_id values may not be set. Make sure that cipher_id is
1029 * set and use it for comparison.
1031 if (s->session->cipher)
1032 s->session->cipher_id = s->session->cipher->id;
1033 if (s->hit && (s->session->cipher_id != c->id)) {
1034 /* Workaround is now obsolete */
1036 if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1039 al = SSL_AD_ILLEGAL_PARAMETER;
1040 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1041 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1045 s->s3->tmp.new_cipher = c;
1047 * Don't digest cached records if no sigalgs: we may need them for client
1050 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1052 /* lets get the compression algorithm */
1054 #ifdef OPENSSL_NO_COMP
1056 al = SSL_AD_ILLEGAL_PARAMETER;
1057 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1058 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1062 * If compression is disabled we'd better not try to resume a session
1063 * using compression.
1065 if (s->session->compress_meth != 0) {
1066 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1071 if (s->hit && j != s->session->compress_meth) {
1072 al = SSL_AD_ILLEGAL_PARAMETER;
1073 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1074 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1079 else if (s->options & SSL_OP_NO_COMPRESSION) {
1080 al = SSL_AD_ILLEGAL_PARAMETER;
1081 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1084 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1086 if ((j != 0) && (comp == NULL)) {
1087 al = SSL_AD_ILLEGAL_PARAMETER;
1088 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1089 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1092 s->s3->tmp.new_compression = comp;
1096 #ifndef OPENSSL_NO_TLSEXT
1097 /* TLS extensions */
1098 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1099 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1105 /* wrong packet length */
1106 al = SSL_AD_DECODE_ERROR;
1107 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1113 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1118 int ssl3_get_server_certificate(SSL *s)
1120 int al, i, ok, ret = -1;
1121 unsigned long n, nc, llen, l;
1123 const unsigned char *q, *p;
1125 STACK_OF(X509) *sk = NULL;
1127 EVP_PKEY *pkey = NULL;
1128 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1131 n = s->method->ssl_get_message(s,
1134 -1, s->max_cert_list, &ok);
1139 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1140 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1141 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1142 s->s3->tmp.reuse_message = 1;
1146 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1147 al = SSL_AD_UNEXPECTED_MESSAGE;
1148 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1151 p = d = (unsigned char *)s->init_msg;
1153 if ((sk = sk_X509_new_null()) == NULL) {
1154 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1159 if (llen + 3 != n) {
1160 al = SSL_AD_DECODE_ERROR;
1161 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1164 for (nc = 0; nc < llen;) {
1166 if ((l + nc + 3) > llen) {
1167 al = SSL_AD_DECODE_ERROR;
1168 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1169 SSL_R_CERT_LENGTH_MISMATCH);
1174 x = d2i_X509(NULL, &q, l);
1176 al = SSL_AD_BAD_CERTIFICATE;
1177 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1181 al = SSL_AD_DECODE_ERROR;
1182 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1183 SSL_R_CERT_LENGTH_MISMATCH);
1186 if (!sk_X509_push(sk, x)) {
1187 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1195 i = ssl_verify_cert_chain(s, sk);
1196 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1197 #ifndef OPENSSL_NO_KRB5
1198 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1199 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1200 #endif /* OPENSSL_NO_KRB5 */
1202 al = ssl_verify_alarm_type(s->verify_result);
1203 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1204 SSL_R_CERTIFICATE_VERIFY_FAILED);
1207 ERR_clear_error(); /* but we keep s->verify_result */
1209 sc = ssl_sess_cert_new();
1213 if (s->session->sess_cert)
1214 ssl_sess_cert_free(s->session->sess_cert);
1215 s->session->sess_cert = sc;
1217 sc->cert_chain = sk;
1219 * Inconsistency alert: cert_chain does include the peer's certificate,
1220 * which we don't include in s3_srvr.c
1222 x = sk_X509_value(sk, 0);
1225 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1228 pkey = X509_get_pubkey(x);
1230 /* VRS: allow null cert if auth == KRB5 */
1231 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1232 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1236 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1237 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1238 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1239 s->s3->tmp.new_cipher->name,
1240 s->s3->tmp.new_cipher->algorithm_mkey,
1241 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1242 #endif /* KSSL_DEBUG */
1244 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1247 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1248 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1252 i = ssl_cert_type(x, pkey);
1253 if (need_cert && i < 0) {
1256 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1257 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1262 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1263 if (exp_idx >= 0 && i != exp_idx) {
1265 al = SSL_AD_ILLEGAL_PARAMETER;
1266 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1267 SSL_R_WRONG_CERTIFICATE_TYPE);
1270 sc->peer_cert_type = i;
1271 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1273 * Why would the following ever happen? We just created sc a couple
1276 if (sc->peer_pkeys[i].x509 != NULL)
1277 X509_free(sc->peer_pkeys[i].x509);
1278 sc->peer_pkeys[i].x509 = x;
1279 sc->peer_key = &(sc->peer_pkeys[i]);
1281 if (s->session->peer != NULL)
1282 X509_free(s->session->peer);
1283 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1284 s->session->peer = x;
1286 sc->peer_cert_type = i;
1287 sc->peer_key = NULL;
1289 if (s->session->peer != NULL)
1290 X509_free(s->session->peer);
1291 s->session->peer = NULL;
1293 s->session->verify_result = s->verify_result;
1299 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1302 EVP_PKEY_free(pkey);
1304 sk_X509_pop_free(sk, X509_free);
1308 int ssl3_get_key_exchange(SSL *s)
1310 #ifndef OPENSSL_NO_RSA
1311 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1314 unsigned char *param, *p;
1316 long i, param_len, n, alg_k, alg_a;
1317 EVP_PKEY *pkey = NULL;
1318 const EVP_MD *md = NULL;
1319 #ifndef OPENSSL_NO_RSA
1322 #ifndef OPENSSL_NO_DH
1325 #ifndef OPENSSL_NO_ECDH
1326 EC_KEY *ecdh = NULL;
1327 BN_CTX *bn_ctx = NULL;
1328 EC_POINT *srvr_ecpoint = NULL;
1330 int encoded_pt_len = 0;
1333 EVP_MD_CTX_init(&md_ctx);
1336 * use same message size as in ssl3_get_certificate_request() as
1337 * ServerKeyExchange message may be skipped
1339 n = s->method->ssl_get_message(s,
1340 SSL3_ST_CR_KEY_EXCH_A,
1341 SSL3_ST_CR_KEY_EXCH_B,
1342 -1, s->max_cert_list, &ok);
1346 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1348 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1350 * Can't skip server key exchange if this is an ephemeral
1353 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1354 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1355 al = SSL_AD_UNEXPECTED_MESSAGE;
1358 #ifndef OPENSSL_NO_PSK
1360 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1361 * identity hint is sent. Set session->sess_cert anyway to avoid
1364 if (alg_k & SSL_kPSK) {
1365 s->session->sess_cert = ssl_sess_cert_new();
1366 if (s->ctx->psk_identity_hint)
1367 OPENSSL_free(s->ctx->psk_identity_hint);
1368 s->ctx->psk_identity_hint = NULL;
1371 s->s3->tmp.reuse_message = 1;
1375 param = p = (unsigned char *)s->init_msg;
1376 if (s->session->sess_cert != NULL) {
1377 #ifndef OPENSSL_NO_RSA
1378 if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1379 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1380 s->session->sess_cert->peer_rsa_tmp = NULL;
1383 #ifndef OPENSSL_NO_DH
1384 if (s->session->sess_cert->peer_dh_tmp) {
1385 DH_free(s->session->sess_cert->peer_dh_tmp);
1386 s->session->sess_cert->peer_dh_tmp = NULL;
1389 #ifndef OPENSSL_NO_ECDH
1390 if (s->session->sess_cert->peer_ecdh_tmp) {
1391 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1392 s->session->sess_cert->peer_ecdh_tmp = NULL;
1396 s->session->sess_cert = ssl_sess_cert_new();
1399 /* Total length of the parameters including the length prefix */
1402 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1404 al = SSL_AD_DECODE_ERROR;
1406 #ifndef OPENSSL_NO_PSK
1407 if (alg_k & SSL_kPSK) {
1408 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1411 if (param_len > n) {
1412 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1418 * Store PSK identity hint for later use, hint is used in
1419 * ssl3_send_client_key_exchange. Assume that the maximum length of
1420 * a PSK identity hint can be as long as the maximum length of a PSK
1423 if (i > PSK_MAX_IDENTITY_LEN) {
1424 al = SSL_AD_HANDSHAKE_FAILURE;
1425 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1428 if (i > n - param_len) {
1429 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1430 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1436 * If received PSK identity hint contains NULL characters, the hint
1437 * is truncated from the first NULL. p may not be ending with NULL,
1438 * so create a NULL-terminated string.
1440 memcpy(tmp_id_hint, p, i);
1441 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1442 if (s->ctx->psk_identity_hint != NULL)
1443 OPENSSL_free(s->ctx->psk_identity_hint);
1444 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1445 if (s->ctx->psk_identity_hint == NULL) {
1446 al = SSL_AD_HANDSHAKE_FAILURE;
1447 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1454 #endif /* !OPENSSL_NO_PSK */
1455 #ifndef OPENSSL_NO_SRP
1456 if (alg_k & SSL_kSRP) {
1458 if (param_len > n) {
1459 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1464 if (i > n - param_len) {
1465 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1470 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1471 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1476 if (2 > n - param_len) {
1477 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1484 if (i > n - param_len) {
1485 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1490 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1491 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1496 if (1 > n - param_len) {
1497 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1502 i = (unsigned int)(p[0]);
1505 if (i > n - param_len) {
1506 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1511 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1512 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1517 if (2 > n - param_len) {
1518 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1525 if (i > n - param_len) {
1526 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1531 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1532 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1538 if (!srp_verify_server_param(s, &al)) {
1539 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1543 /* We must check if there is a certificate */
1544 # ifndef OPENSSL_NO_RSA
1545 if (alg_a & SSL_aRSA)
1547 X509_get_pubkey(s->session->
1548 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1552 # ifndef OPENSSL_NO_DSA
1553 else if (alg_a & SSL_aDSS)
1555 X509_get_pubkey(s->session->
1556 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1560 #endif /* !OPENSSL_NO_SRP */
1561 #ifndef OPENSSL_NO_RSA
1562 if (alg_k & SSL_kRSA) {
1563 /* Temporary RSA keys only allowed in export ciphersuites */
1564 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1565 al = SSL_AD_UNEXPECTED_MESSAGE;
1566 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1569 if ((rsa = RSA_new()) == NULL) {
1570 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1575 if (param_len > n) {
1576 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1581 if (i > n - param_len) {
1582 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1587 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1588 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1593 if (2 > n - param_len) {
1594 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1601 if (i > n - param_len) {
1602 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1607 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1608 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1614 /* this should be because we are using an export cipher */
1615 if (alg_a & SSL_aRSA)
1617 X509_get_pubkey(s->session->
1618 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1620 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1623 s->session->sess_cert->peer_rsa_tmp = rsa;
1626 #else /* OPENSSL_NO_RSA */
1629 #ifndef OPENSSL_NO_DH
1630 else if (alg_k & SSL_kEDH) {
1631 if ((dh = DH_new()) == NULL) {
1632 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1637 if (param_len > n) {
1638 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1643 if (i > n - param_len) {
1644 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1649 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1650 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1655 if (2 > n - param_len) {
1656 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1663 if (i > n - param_len) {
1664 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1669 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1670 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1675 if (2 > n - param_len) {
1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1683 if (i > n - param_len) {
1684 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1689 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1690 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1696 # ifndef OPENSSL_NO_RSA
1697 if (alg_a & SSL_aRSA)
1699 X509_get_pubkey(s->session->
1700 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1704 # ifndef OPENSSL_NO_DSA
1705 else if (alg_a & SSL_aDSS)
1707 X509_get_pubkey(s->session->
1708 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1711 /* else anonymous DH, so no certificate or pkey. */
1713 s->session->sess_cert->peer_dh_tmp = dh;
1715 } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1716 al = SSL_AD_ILLEGAL_PARAMETER;
1717 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1718 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1721 #endif /* !OPENSSL_NO_DH */
1723 #ifndef OPENSSL_NO_ECDH
1724 else if (alg_k & SSL_kEECDH) {
1726 const EC_GROUP *group;
1728 if ((ecdh = EC_KEY_new()) == NULL) {
1729 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1734 * Extract elliptic curve parameters and the server's ephemeral ECDH
1735 * public key. Keep accumulating lengths of various components in
1736 * param_len and make sure it never exceeds n.
1740 * XXX: For now we only support named (not generic) curves and the
1741 * ECParameters in this case is just three bytes. We also need one
1742 * byte for the length of the encoded point
1745 if (param_len > n) {
1746 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1750 * Check curve is one of our preferences, if not server has sent an
1751 * invalid curve. ECParameters is 3 bytes.
1753 if (!tls1_check_curve(s, p, 3)) {
1754 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1758 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1759 al = SSL_AD_INTERNAL_ERROR;
1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1761 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1765 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1766 if (ngroup == NULL) {
1767 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1770 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1771 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1774 EC_GROUP_free(ngroup);
1776 group = EC_KEY_get0_group(ecdh);
1778 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1779 (EC_GROUP_get_degree(group) > 163)) {
1780 al = SSL_AD_EXPORT_RESTRICTION;
1781 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1782 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1788 /* Next, get the encoded ECPoint */
1789 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1790 ((bn_ctx = BN_CTX_new()) == NULL)) {
1791 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1795 encoded_pt_len = *p; /* length of encoded point */
1798 if ((encoded_pt_len > n - param_len) ||
1799 (EC_POINT_oct2point(group, srvr_ecpoint,
1800 p, encoded_pt_len, bn_ctx) == 0)) {
1801 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1804 param_len += encoded_pt_len;
1807 p += encoded_pt_len;
1810 * The ECC/TLS specification does not mention the use of DSA to sign
1811 * ECParameters in the server key exchange message. We do support RSA
1815 # ifndef OPENSSL_NO_RSA
1816 else if (alg_a & SSL_aRSA)
1818 X509_get_pubkey(s->session->
1819 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1821 # ifndef OPENSSL_NO_ECDSA
1822 else if (alg_a & SSL_aECDSA)
1824 X509_get_pubkey(s->session->
1825 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1827 /* else anonymous ECDH, so no certificate or pkey. */
1828 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1829 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1831 BN_CTX_free(bn_ctx);
1833 EC_POINT_free(srvr_ecpoint);
1834 srvr_ecpoint = NULL;
1836 al = SSL_AD_UNEXPECTED_MESSAGE;
1837 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1840 #endif /* !OPENSSL_NO_ECDH */
1842 /* p points to the next byte, there are 'n' bytes left */
1844 /* if it was signed, check the signature */
1846 if (SSL_USE_SIGALGS(s)) {
1849 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1852 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1859 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1867 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1872 j = EVP_PKEY_size(pkey);
1875 * Check signature length. If n is 0 then signature is empty
1877 if ((i != n) || (n > j) || (n <= 0)) {
1878 /* wrong packet length */
1879 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1882 #ifndef OPENSSL_NO_RSA
1883 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1889 for (num = 2; num > 0; num--) {
1890 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1891 EVP_DigestInit_ex(&md_ctx, (num == 2)
1892 ? s->ctx->md5 : s->ctx->sha1, NULL);
1893 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1895 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1897 EVP_DigestUpdate(&md_ctx, param, param_len);
1898 EVP_DigestFinal_ex(&md_ctx, q, &size);
1902 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1904 al = SSL_AD_DECRYPT_ERROR;
1905 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1910 al = SSL_AD_DECRYPT_ERROR;
1911 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1917 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1918 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1920 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1922 EVP_VerifyUpdate(&md_ctx, param, param_len);
1923 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1925 al = SSL_AD_DECRYPT_ERROR;
1926 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1931 /* aNULL, aSRP or kPSK do not need public keys */
1932 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1933 /* Might be wrong key type, check it */
1934 if (ssl3_check_cert_and_algorithm(s))
1935 /* Otherwise this shouldn't happen */
1936 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1939 /* still data left over */
1941 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1945 EVP_PKEY_free(pkey);
1946 EVP_MD_CTX_cleanup(&md_ctx);
1949 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1951 EVP_PKEY_free(pkey);
1952 #ifndef OPENSSL_NO_RSA
1956 #ifndef OPENSSL_NO_DH
1960 #ifndef OPENSSL_NO_ECDH
1961 BN_CTX_free(bn_ctx);
1962 EC_POINT_free(srvr_ecpoint);
1966 EVP_MD_CTX_cleanup(&md_ctx);
1970 int ssl3_get_certificate_request(SSL *s)
1973 unsigned long n, nc, l;
1974 unsigned int llen, ctype_num, i;
1975 X509_NAME *xn = NULL;
1976 const unsigned char *p, *q;
1978 STACK_OF(X509_NAME) *ca_sk = NULL;
1980 n = s->method->ssl_get_message(s,
1981 SSL3_ST_CR_CERT_REQ_A,
1982 SSL3_ST_CR_CERT_REQ_B,
1983 -1, s->max_cert_list, &ok);
1988 s->s3->tmp.cert_req = 0;
1990 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1991 s->s3->tmp.reuse_message = 1;
1993 * If we get here we don't need any cached handshake records as we
1994 * wont be doing client auth.
1996 if (s->s3->handshake_buffer) {
1997 if (!ssl3_digest_cached_records(s))
2003 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2004 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2005 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2009 /* TLS does not like anon-DH with client cert */
2010 if (s->version > SSL3_VERSION) {
2011 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2012 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2013 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2014 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2019 p = d = (unsigned char *)s->init_msg;
2021 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2022 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2026 /* get the certificate types */
2028 if (s->cert->ctypes) {
2029 OPENSSL_free(s->cert->ctypes);
2030 s->cert->ctypes = NULL;
2032 if (ctype_num > SSL3_CT_NUMBER) {
2033 /* If we exceed static buffer copy all to cert structure */
2034 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2035 memcpy(s->cert->ctypes, p, ctype_num);
2036 s->cert->ctype_num = (size_t)ctype_num;
2037 ctype_num = SSL3_CT_NUMBER;
2039 for (i = 0; i < ctype_num; i++)
2040 s->s3->tmp.ctype[i] = p[i];
2042 if (SSL_USE_SIGALGS(s)) {
2045 * Check we have enough room for signature algorithms and following
2048 if ((unsigned long)(p - d + llen + 2) > n) {
2049 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2050 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2051 SSL_R_DATA_LENGTH_TOO_LONG);
2054 /* Clear certificate digests and validity flags */
2055 for (i = 0; i < SSL_PKEY_NUM; i++) {
2056 s->cert->pkeys[i].digest = NULL;
2057 s->cert->pkeys[i].valid_flags = 0;
2059 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2060 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2061 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2062 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2065 if (!tls1_process_sigalgs(s)) {
2066 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2067 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2073 /* get the CA RDNs */
2078 out = fopen("/tmp/vsign.der", "w");
2079 fwrite(p, 1, llen, out);
2084 if ((unsigned long)(p - d + llen) != n) {
2085 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2086 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2090 for (nc = 0; nc < llen;) {
2092 if ((l + nc + 2) > llen) {
2093 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2094 goto cont; /* netscape bugs */
2095 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2096 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2102 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2103 /* If netscape tolerance is on, ignore errors */
2104 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2108 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2114 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2115 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2116 SSL_R_CA_DN_LENGTH_MISMATCH);
2119 if (!sk_X509_NAME_push(ca_sk, xn)) {
2120 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2133 /* we should setup a certificate to return.... */
2134 s->s3->tmp.cert_req = 1;
2135 s->s3->tmp.ctype_num = ctype_num;
2136 if (s->s3->tmp.ca_names != NULL)
2137 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2138 s->s3->tmp.ca_names = ca_sk;
2144 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2148 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2150 return (X509_NAME_cmp(*a, *b));
2153 #ifndef OPENSSL_NO_TLSEXT
2154 int ssl3_get_new_session_ticket(SSL *s)
2156 int ok, al, ret = 0, ticklen;
2158 const unsigned char *p;
2161 n = s->method->ssl_get_message(s,
2162 SSL3_ST_CR_SESSION_TICKET_A,
2163 SSL3_ST_CR_SESSION_TICKET_B,
2164 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2170 /* need at least ticket_lifetime_hint + ticket length */
2171 al = SSL_AD_DECODE_ERROR;
2172 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2176 p = d = (unsigned char *)s->init_msg;
2177 n2l(p, s->session->tlsext_tick_lifetime_hint);
2179 /* ticket_lifetime_hint + ticket_length + ticket */
2180 if (ticklen + 6 != n) {
2181 al = SSL_AD_DECODE_ERROR;
2182 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2185 if (s->session->tlsext_tick) {
2186 OPENSSL_free(s->session->tlsext_tick);
2187 s->session->tlsext_ticklen = 0;
2189 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2190 if (!s->session->tlsext_tick) {
2191 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2194 memcpy(s->session->tlsext_tick, p, ticklen);
2195 s->session->tlsext_ticklen = ticklen;
2197 * There are two ways to detect a resumed ticket session. One is to set
2198 * an appropriate session ID and then the server must return a match in
2199 * ServerHello. This allows the normal client session ID matching to work
2200 * and we know much earlier that the ticket has been accepted. The
2201 * other way is to set zero length session ID when the ticket is
2202 * presented and rely on the handshake to determine session resumption.
2203 * We choose the former approach because this fits in with assumptions
2204 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2205 * SHA256 is disabled) hash of the ticket.
2207 EVP_Digest(p, ticklen,
2208 s->session->session_id, &s->session->session_id_length,
2209 # ifndef OPENSSL_NO_SHA256
2210 EVP_sha256(), NULL);
2217 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2222 int ssl3_get_cert_status(SSL *s)
2225 unsigned long resplen, n;
2226 const unsigned char *p;
2228 n = s->method->ssl_get_message(s,
2229 SSL3_ST_CR_CERT_STATUS_A,
2230 SSL3_ST_CR_CERT_STATUS_B,
2231 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2236 /* need at least status type + length */
2237 al = SSL_AD_DECODE_ERROR;
2238 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2241 p = (unsigned char *)s->init_msg;
2242 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2243 al = SSL_AD_DECODE_ERROR;
2244 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2248 if (resplen + 4 != n) {
2249 al = SSL_AD_DECODE_ERROR;
2250 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2253 if (s->tlsext_ocsp_resp)
2254 OPENSSL_free(s->tlsext_ocsp_resp);
2255 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2256 if (!s->tlsext_ocsp_resp) {
2257 al = SSL_AD_INTERNAL_ERROR;
2258 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2261 s->tlsext_ocsp_resplen = resplen;
2262 if (s->ctx->tlsext_status_cb) {
2264 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2266 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2267 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2271 al = SSL_AD_INTERNAL_ERROR;
2272 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2278 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2283 int ssl3_get_server_done(SSL *s)
2288 /* Second to last param should be very small, like 0 :-) */
2289 n = s->method->ssl_get_message(s,
2290 SSL3_ST_CR_SRVR_DONE_A,
2291 SSL3_ST_CR_SRVR_DONE_B,
2292 SSL3_MT_SERVER_DONE, 30, &ok);
2297 /* should contain no data */
2298 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2299 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2306 int ssl3_send_client_key_exchange(SSL *s)
2310 unsigned long alg_k;
2311 #ifndef OPENSSL_NO_RSA
2313 EVP_PKEY *pkey = NULL;
2315 #ifndef OPENSSL_NO_KRB5
2317 #endif /* OPENSSL_NO_KRB5 */
2318 #ifndef OPENSSL_NO_ECDH
2319 EC_KEY *clnt_ecdh = NULL;
2320 const EC_POINT *srvr_ecpoint = NULL;
2321 EVP_PKEY *srvr_pub_pkey = NULL;
2322 unsigned char *encodedPoint = NULL;
2323 int encoded_pt_len = 0;
2324 BN_CTX *bn_ctx = NULL;
2327 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2328 p = ssl_handshake_start(s);
2330 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2332 /* Fool emacs indentation */
2335 #ifndef OPENSSL_NO_RSA
2336 else if (alg_k & SSL_kRSA) {
2338 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2340 if (s->session->sess_cert == NULL) {
2342 * We should always have a server certificate with SSL_kRSA.
2344 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2345 ERR_R_INTERNAL_ERROR);
2349 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2350 rsa = s->session->sess_cert->peer_rsa_tmp;
2353 X509_get_pubkey(s->session->
2354 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2356 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2357 || (pkey->pkey.rsa == NULL)) {
2358 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2359 ERR_R_INTERNAL_ERROR);
2362 rsa = pkey->pkey.rsa;
2363 EVP_PKEY_free(pkey);
2366 tmp_buf[0] = s->client_version >> 8;
2367 tmp_buf[1] = s->client_version & 0xff;
2368 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2371 s->session->master_key_length = sizeof tmp_buf;
2374 /* Fix buf for TLS and beyond */
2375 if (s->version > SSL3_VERSION)
2377 n = RSA_public_encrypt(sizeof tmp_buf,
2378 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
2380 if (s->options & SSL_OP_PKCS1_CHECK_1)
2382 if (s->options & SSL_OP_PKCS1_CHECK_2)
2386 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2387 SSL_R_BAD_RSA_ENCRYPT);
2391 /* Fix buf for TLS and beyond */
2392 if (s->version > SSL3_VERSION) {
2397 s->session->master_key_length =
2398 s->method->ssl3_enc->generate_master_secret(s,
2400 session->master_key,
2403 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2406 #ifndef OPENSSL_NO_KRB5
2407 else if (alg_k & SSL_kKRB5) {
2408 krb5_error_code krb5rc;
2409 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2410 /* krb5_data krb5_ap_req; */
2411 krb5_data *enc_ticket;
2412 krb5_data authenticator, *authp = NULL;
2413 EVP_CIPHER_CTX ciph_ctx;
2414 const EVP_CIPHER *enc = NULL;
2415 unsigned char iv[EVP_MAX_IV_LENGTH];
2416 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2417 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2418 int padl, outl = sizeof(epms);
2420 EVP_CIPHER_CTX_init(&ciph_ctx);
2423 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2425 # endif /* KSSL_DEBUG */
2428 # ifdef KRB5SENDAUTH
2430 authp = &authenticator;
2431 # endif /* KRB5SENDAUTH */
2433 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2434 enc = kssl_map_enc(kssl_ctx->enctype);
2439 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2440 if (krb5rc && kssl_err.text)
2441 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2444 # endif /* KSSL_DEBUG */
2447 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2448 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2453 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2454 * in place of RFC 2712 KerberosWrapper, as in:
2456 * Send ticket (copy to *p, set n = length)
2457 * n = krb5_ap_req.length;
2458 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2459 * if (krb5_ap_req.data)
2460 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2462 * Now using real RFC 2712 KerberosWrapper
2463 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2464 * Note: 2712 "opaque" types are here replaced
2465 * with a 2-byte length followed by the value.
2467 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2468 * Where "xx xx" = length bytes. Shown here with
2469 * optional authenticator omitted.
2472 /* KerberosWrapper.Ticket */
2473 s2n(enc_ticket->length, p);
2474 memcpy(p, enc_ticket->data, enc_ticket->length);
2475 p += enc_ticket->length;
2476 n = enc_ticket->length + 2;
2478 /* KerberosWrapper.Authenticator */
2479 if (authp && authp->length) {
2480 s2n(authp->length, p);
2481 memcpy(p, authp->data, authp->length);
2483 n += authp->length + 2;
2489 s2n(0, p); /* null authenticator length */
2493 tmp_buf[0] = s->client_version >> 8;
2494 tmp_buf[1] = s->client_version & 0xff;
2495 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2499 * 20010420 VRS. Tried it this way; failed.
2500 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2501 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2502 * kssl_ctx->length);
2503 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2506 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2507 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2508 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
2510 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2512 if (outl > (int)sizeof epms) {
2513 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2514 ERR_R_INTERNAL_ERROR);
2517 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2519 /* KerberosWrapper.EncryptedPreMasterSecret */
2521 memcpy(p, epms, outl);
2525 s->session->master_key_length =
2526 s->method->ssl3_enc->generate_master_secret(s,
2528 session->master_key,
2532 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2533 OPENSSL_cleanse(epms, outl);
2536 #ifndef OPENSSL_NO_DH
2537 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2538 DH *dh_srvr, *dh_clnt;
2539 SESS_CERT *scert = s->session->sess_cert;
2541 if (scert == NULL) {
2542 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2543 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2544 SSL_R_UNEXPECTED_MESSAGE);
2548 if (scert->peer_dh_tmp != NULL)
2549 dh_srvr = scert->peer_dh_tmp;
2551 /* we get them from the cert */
2552 int idx = scert->peer_cert_type;
2553 EVP_PKEY *spkey = NULL;
2556 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2558 dh_srvr = EVP_PKEY_get1_DH(spkey);
2559 EVP_PKEY_free(spkey);
2561 if (dh_srvr == NULL) {
2562 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2563 ERR_R_INTERNAL_ERROR);
2567 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2568 /* Use client certificate key */
2569 EVP_PKEY *clkey = s->cert->key->privatekey;
2572 dh_clnt = EVP_PKEY_get1_DH(clkey);
2573 if (dh_clnt == NULL) {
2574 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2575 ERR_R_INTERNAL_ERROR);
2579 /* generate a new random key */
2580 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2581 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2584 if (!DH_generate_key(dh_clnt)) {
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2592 * use the 'p' output buffer for the DH key, but make sure to
2593 * clear it out afterwards
2596 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2597 if (scert->peer_dh_tmp == NULL)
2601 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2606 /* generate master key from the result */
2607 s->session->master_key_length =
2608 s->method->ssl3_enc->generate_master_secret(s,
2610 session->master_key,
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_kEECDH | 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 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
2749 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2753 /* generate master key from the result */
2754 s->session->master_key_length =
2755 s->method->ssl3_enc->generate_master_secret(s,
2757 session->master_key,
2760 memset(p, 0, n); /* clean up */
2762 if (ecdh_clnt_cert) {
2763 /* Send empty client key exch message */
2767 * First check the size of encoding and allocate memory
2771 EC_POINT_point2oct(srvr_group,
2772 EC_KEY_get0_public_key(clnt_ecdh),
2773 POINT_CONVERSION_UNCOMPRESSED,
2776 encodedPoint = (unsigned char *)
2777 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2778 bn_ctx = BN_CTX_new();
2779 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2780 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2781 ERR_R_MALLOC_FAILURE);
2785 /* Encode the public key */
2786 n = EC_POINT_point2oct(srvr_group,
2787 EC_KEY_get0_public_key(clnt_ecdh),
2788 POINT_CONVERSION_UNCOMPRESSED,
2789 encodedPoint, encoded_pt_len, bn_ctx);
2791 *p = n; /* length of encoded point */
2792 /* Encoded point will be copied here */
2794 /* copy the point */
2795 memcpy((unsigned char *)p, encodedPoint, n);
2796 /* increment n to account for length field */
2800 /* Free allocated memory */
2801 BN_CTX_free(bn_ctx);
2802 if (encodedPoint != NULL)
2803 OPENSSL_free(encodedPoint);
2804 if (clnt_ecdh != NULL)
2805 EC_KEY_free(clnt_ecdh);
2806 EVP_PKEY_free(srvr_pub_pkey);
2808 #endif /* !OPENSSL_NO_ECDH */
2809 else if (alg_k & SSL_kGOST) {
2810 /* GOST key exchange message creation */
2811 EVP_PKEY_CTX *pkey_ctx;
2814 unsigned int md_len;
2816 unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
2817 EVP_MD_CTX *ukm_hash;
2821 * Get server sertificate PKEY and create ctx from it
2825 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2829 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2831 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2832 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2836 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2837 X509_get_pubkey(peer_cert), NULL);
2839 * If we have send a certificate, and certificate key
2841 * * parameters match those of server certificate, use
2842 * certificate key for key exchange
2845 /* Otherwise, generate ephemeral key pair */
2847 EVP_PKEY_encrypt_init(pkey_ctx);
2848 /* Generate session key */
2849 RAND_bytes(premaster_secret, 32);
2851 * If we have client certificate, use its secret as peer key
2853 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2854 if (EVP_PKEY_derive_set_peer
2855 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2857 * If there was an error - just ignore it. Ephemeral key
2864 * Compute shared IV and store it in algorithm-specific context
2867 ukm_hash = EVP_MD_CTX_create();
2868 EVP_DigestInit(ukm_hash,
2869 EVP_get_digestbynid(NID_id_GostR3411_94));
2870 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2872 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2874 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2875 EVP_MD_CTX_destroy(ukm_hash);
2876 if (EVP_PKEY_CTX_ctrl
2877 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2879 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2883 /* Make GOST keytransport blob message */
2885 * Encapsulate it into sequence
2887 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2889 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
2891 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2895 if (msglen >= 0x80) {
2897 *(p++) = msglen & 0xff;
2900 *(p++) = msglen & 0xff;
2903 memcpy(p, tmp, msglen);
2904 /* Check if pubkey from client certificate was used */
2905 if (EVP_PKEY_CTX_ctrl
2906 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2907 /* Set flag "skip certificate verify" */
2908 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2910 EVP_PKEY_CTX_free(pkey_ctx);
2911 s->session->master_key_length =
2912 s->method->ssl3_enc->generate_master_secret(s,
2914 session->master_key,
2917 EVP_PKEY_free(pub_key);
2920 #ifndef OPENSSL_NO_SRP
2921 else if (alg_k & SSL_kSRP) {
2922 if (s->srp_ctx.A != NULL) {
2923 /* send off the data */
2924 n = BN_num_bytes(s->srp_ctx.A);
2926 BN_bn2bin(s->srp_ctx.A, p);
2929 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2930 ERR_R_INTERNAL_ERROR);
2933 if (s->session->srp_username != NULL)
2934 OPENSSL_free(s->session->srp_username);
2935 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2936 if (s->session->srp_username == NULL) {
2937 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2938 ERR_R_MALLOC_FAILURE);
2942 if ((s->session->master_key_length =
2943 SRP_generate_client_master_secret(s,
2944 s->session->master_key)) <
2946 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2947 ERR_R_INTERNAL_ERROR);
2952 #ifndef OPENSSL_NO_PSK
2953 else if (alg_k & SSL_kPSK) {
2955 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2956 * \0-terminated identity. The last byte is for us for simulating
2959 char identity[PSK_MAX_IDENTITY_LEN + 2];
2960 size_t identity_len;
2961 unsigned char *t = NULL;
2962 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2963 unsigned int pre_ms_len = 0, psk_len = 0;
2967 if (s->psk_client_callback == NULL) {
2968 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2969 SSL_R_PSK_NO_CLIENT_CB);
2973 memset(identity, 0, sizeof(identity));
2974 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2975 identity, sizeof(identity) - 1,
2977 sizeof(psk_or_pre_ms));
2978 if (psk_len > PSK_MAX_PSK_LEN) {
2979 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2980 ERR_R_INTERNAL_ERROR);
2982 } else if (psk_len == 0) {
2983 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2984 SSL_R_PSK_IDENTITY_NOT_FOUND);
2987 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2988 identity_len = strlen(identity);
2989 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2990 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2991 ERR_R_INTERNAL_ERROR);
2994 /* create PSK pre_master_secret */
2995 pre_ms_len = 2 + psk_len + 2 + psk_len;
2997 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2999 memset(t, 0, psk_len);
3003 if (s->session->psk_identity_hint != NULL)
3004 OPENSSL_free(s->session->psk_identity_hint);
3005 s->session->psk_identity_hint =
3006 BUF_strdup(s->ctx->psk_identity_hint);
3007 if (s->ctx->psk_identity_hint != NULL
3008 && s->session->psk_identity_hint == NULL) {
3009 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3010 ERR_R_MALLOC_FAILURE);
3014 if (s->session->psk_identity != NULL)
3015 OPENSSL_free(s->session->psk_identity);
3016 s->session->psk_identity = BUF_strdup(identity);
3017 if (s->session->psk_identity == NULL) {
3018 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3019 ERR_R_MALLOC_FAILURE);
3023 s->session->master_key_length =
3024 s->method->ssl3_enc->generate_master_secret(s,
3026 session->master_key,
3029 s2n(identity_len, p);
3030 memcpy(p, identity, identity_len);
3031 n = 2 + identity_len;
3034 OPENSSL_cleanse(identity, sizeof(identity));
3035 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
3037 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3043 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3044 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3048 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
3049 s->state = SSL3_ST_CW_KEY_EXCH_B;
3052 /* SSL3_ST_CW_KEY_EXCH_B */
3053 return ssl_do_write(s);
3055 #ifndef OPENSSL_NO_ECDH
3056 BN_CTX_free(bn_ctx);
3057 if (encodedPoint != NULL)
3058 OPENSSL_free(encodedPoint);
3059 if (clnt_ecdh != NULL)
3060 EC_KEY_free(clnt_ecdh);
3061 EVP_PKEY_free(srvr_pub_pkey);
3066 int ssl3_send_client_verify(SSL *s)
3069 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3071 EVP_PKEY_CTX *pctx = NULL;
3077 EVP_MD_CTX_init(&mctx);
3079 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3080 p = ssl_handshake_start(s);
3081 pkey = s->cert->key->privatekey;
3082 /* Create context from key and test if sha1 is allowed as digest */
3083 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3084 EVP_PKEY_sign_init(pctx);
3085 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3086 if (!SSL_USE_SIGALGS(s))
3087 s->method->ssl3_enc->cert_verify_mac(s,
3090 [MD5_DIGEST_LENGTH]));
3095 * For TLS v1.2 send signature algorithm and signature using agreed
3096 * digest and cached handshake records.
3098 if (SSL_USE_SIGALGS(s)) {
3101 const EVP_MD *md = s->cert->key->digest;
3102 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3103 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3104 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3109 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3112 if (!EVP_SignInit_ex(&mctx, md, NULL)
3113 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3114 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3115 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3120 if (!ssl3_digest_cached_records(s))
3123 #ifndef OPENSSL_NO_RSA
3124 if (pkey->type == EVP_PKEY_RSA) {
3125 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3126 if (RSA_sign(NID_md5_sha1, data,
3127 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3128 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3129 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3136 #ifndef OPENSSL_NO_DSA
3137 if (pkey->type == EVP_PKEY_DSA) {
3138 if (!DSA_sign(pkey->save_type,
3139 &(data[MD5_DIGEST_LENGTH]),
3140 SHA_DIGEST_LENGTH, &(p[2]),
3141 (unsigned int *)&j, pkey->pkey.dsa)) {
3142 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3149 #ifndef OPENSSL_NO_ECDSA
3150 if (pkey->type == EVP_PKEY_EC) {
3151 if (!ECDSA_sign(pkey->save_type,
3152 &(data[MD5_DIGEST_LENGTH]),
3153 SHA_DIGEST_LENGTH, &(p[2]),
3154 (unsigned int *)&j, pkey->pkey.ec)) {
3155 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3162 if (pkey->type == NID_id_GostR3410_94
3163 || pkey->type == NID_id_GostR3410_2001) {
3164 unsigned char signbuf[64];
3166 size_t sigsize = 64;
3167 s->method->ssl3_enc->cert_verify_mac(s,
3168 NID_id_GostR3411_94, data);
3169 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3170 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3173 for (i = 63, j = 0; i >= 0; j++, i--) {
3174 p[2 + j] = signbuf[i];
3179 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3182 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
3183 s->state = SSL3_ST_CW_CERT_VRFY_B;
3185 EVP_MD_CTX_cleanup(&mctx);
3186 EVP_PKEY_CTX_free(pctx);
3187 return ssl_do_write(s);
3189 EVP_MD_CTX_cleanup(&mctx);
3190 EVP_PKEY_CTX_free(pctx);
3195 * Check a certificate can be used for client authentication. Currently check
3196 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3197 * certificates can be used and optionally checks suitability for Suite B.
3199 static int ssl3_check_client_certificate(SSL *s)
3201 unsigned long alg_k;
3202 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3204 /* If no suitable signature algorithm can't use certificate */
3205 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3208 * If strict mode check suitability of chain before using it. This also
3209 * adjusts suite B digest if necessary.
3211 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3212 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3214 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3215 /* See if we can use client certificate for fixed DH */
3216 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3217 SESS_CERT *scert = s->session->sess_cert;
3218 int i = scert->peer_cert_type;
3219 EVP_PKEY *clkey = NULL, *spkey = NULL;
3220 clkey = s->cert->key->privatekey;
3221 /* If client key not DH assume it can be used */
3222 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3225 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3227 /* Compare server and client parameters */
3228 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3229 EVP_PKEY_free(spkey);
3233 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3238 int ssl3_send_client_certificate(SSL *s)
3241 EVP_PKEY *pkey = NULL;
3244 if (s->state == SSL3_ST_CW_CERT_A) {
3245 /* Let cert callback update client certificates if required */
3246 if (s->cert->cert_cb) {
3247 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3249 s->rwstate = SSL_X509_LOOKUP;
3253 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3256 s->rwstate = SSL_NOTHING;
3258 if (ssl3_check_client_certificate(s))
3259 s->state = SSL3_ST_CW_CERT_C;
3261 s->state = SSL3_ST_CW_CERT_B;
3264 /* We need to get a client cert */
3265 if (s->state == SSL3_ST_CW_CERT_B) {
3267 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3268 * return(-1); We then get retied later
3271 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3273 s->rwstate = SSL_X509_LOOKUP;
3276 s->rwstate = SSL_NOTHING;
3277 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3278 s->state = SSL3_ST_CW_CERT_B;
3279 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3281 } else if (i == 1) {
3283 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3284 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3290 EVP_PKEY_free(pkey);
3291 if (i && !ssl3_check_client_certificate(s))
3294 if (s->version == SSL3_VERSION) {
3295 s->s3->tmp.cert_req = 0;
3296 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3299 s->s3->tmp.cert_req = 2;
3303 /* Ok, we have a cert */
3304 s->state = SSL3_ST_CW_CERT_C;
3307 if (s->state == SSL3_ST_CW_CERT_C) {
3308 s->state = SSL3_ST_CW_CERT_D;
3309 if (!ssl3_output_cert_chain(s,
3310 (s->s3->tmp.cert_req ==
3311 2) ? NULL : s->cert->key)) {
3312 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3313 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3317 /* SSL3_ST_CW_CERT_D */
3318 return ssl_do_write(s);
3321 #define has_bits(i,m) (((i)&(m)) == (m))
3323 int ssl3_check_cert_and_algorithm(SSL *s)
3327 EVP_PKEY *pkey = NULL;
3329 #ifndef OPENSSL_NO_RSA
3332 #ifndef OPENSSL_NO_DH
3336 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3337 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3339 /* we don't have a certificate */
3340 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3343 sc = s->session->sess_cert;
3345 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3348 #ifndef OPENSSL_NO_RSA
3349 rsa = s->session->sess_cert->peer_rsa_tmp;
3351 #ifndef OPENSSL_NO_DH
3352 dh = s->session->sess_cert->peer_dh_tmp;
3355 /* This is the passed certificate */
3357 idx = sc->peer_cert_type;
3358 #ifndef OPENSSL_NO_ECDH
3359 if (idx == SSL_PKEY_ECC) {
3360 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3362 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3367 } else if (alg_a & SSL_aECDSA) {
3368 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3369 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3371 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3372 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3376 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3377 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3378 EVP_PKEY_free(pkey);
3380 /* Check that we have a certificate if we require one */
3381 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3382 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3383 SSL_R_MISSING_RSA_SIGNING_CERT);
3386 #ifndef OPENSSL_NO_DSA
3387 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3388 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3389 SSL_R_MISSING_DSA_SIGNING_CERT);
3393 #ifndef OPENSSL_NO_RSA
3394 if ((alg_k & SSL_kRSA) &&
3395 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3396 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3397 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3401 #ifndef OPENSSL_NO_DH
3402 if ((alg_k & SSL_kEDH) &&
3403 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3404 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3406 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3407 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3408 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3409 SSL_R_MISSING_DH_RSA_CERT);
3412 # ifndef OPENSSL_NO_DSA
3413 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3414 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3415 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3416 SSL_R_MISSING_DH_DSA_CERT);
3422 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3423 #ifndef OPENSSL_NO_RSA
3424 if (alg_k & SSL_kRSA) {
3426 || RSA_size(rsa) * 8 >
3427 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3428 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3429 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3434 #ifndef OPENSSL_NO_DH
3435 if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
3437 || DH_size(dh) * 8 >
3438 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3439 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3440 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3446 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3447 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3453 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3458 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
3459 int ssl3_send_next_proto(SSL *s)
3461 unsigned int len, padding_len;
3464 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3465 len = s->next_proto_negotiated_len;
3466 padding_len = 32 - ((len + 2) % 32);
3467 d = (unsigned char *)s->init_buf->data;
3469 memcpy(d + 5, s->next_proto_negotiated, len);
3470 d[5 + len] = padding_len;
3471 memset(d + 6 + len, 0, padding_len);
3472 *(d++) = SSL3_MT_NEXT_PROTO;
3473 l2n3(2 + len + padding_len, d);
3474 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3475 s->init_num = 4 + 2 + len + padding_len;
3479 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3481 #endif /* !OPENSSL_NO_TLSEXT &&
3482 * !OPENSSL_NO_NEXTPROTONEG */
3484 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3487 #ifndef OPENSSL_NO_ENGINE
3488 if (s->ctx->client_cert_engine) {
3489 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3490 SSL_get_client_CA_list(s),
3491 px509, ppkey, NULL, NULL, NULL);
3496 if (s->ctx->client_cert_cb)
3497 i = s->ctx->client_cert_cb(s, px509, ppkey);