2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
152 #include "ssl_locl.h"
153 #include "kssl_lcl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
160 # include <openssl/fips.h>
162 #ifndef OPENSSL_NO_DH
163 # include <openssl/dh.h>
165 #include <openssl/bn.h>
166 #ifndef OPENSSL_NO_ENGINE
167 # include <openssl/engine.h>
170 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
171 #ifndef OPENSSL_NO_TLSEXT
172 static int ssl3_check_finished(SSL *s);
175 #ifndef OPENSSL_NO_SSL3_METHOD
176 static const SSL_METHOD *ssl3_get_client_method(int ver)
178 if (ver == SSL3_VERSION)
179 return (SSLv3_client_method());
184 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
185 ssl_undefined_function,
186 ssl3_connect, ssl3_get_client_method)
188 int ssl3_connect(SSL *s)
191 unsigned long Time = (unsigned long)time(NULL);
192 void (*cb) (const SSL *ssl, int type, int val) = NULL;
194 int new_state, state, skip = 0;
196 RAND_add(&Time, sizeof(Time), 0);
200 if (s->info_callback != NULL)
201 cb = s->info_callback;
202 else if (s->ctx->info_callback != NULL)
203 cb = s->ctx->info_callback;
206 if (!SSL_in_init(s) || SSL_in_before(s))
209 #ifndef OPENSSL_NO_HEARTBEATS
211 * If we're awaiting a HeartbeatResponse, pretend we already got and
212 * don't await it anymore, because Heartbeats don't make sense during
215 if (s->tlsext_hb_pending) {
216 s->tlsext_hb_pending = 0;
225 case SSL_ST_RENEGOTIATE:
227 s->state = SSL_ST_CONNECT;
228 s->ctx->stats.sess_connect_renegotiate++;
232 case SSL_ST_BEFORE | SSL_ST_CONNECT:
233 case SSL_ST_OK | SSL_ST_CONNECT:
237 cb(s, SSL_CB_HANDSHAKE_START, 1);
239 if ((s->version & 0xff00) != 0x0300) {
240 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
241 s->state = SSL_ST_ERR;
246 /* s->version=SSL3_VERSION; */
247 s->type = SSL_ST_CONNECT;
249 if (s->init_buf == NULL) {
250 if ((buf = BUF_MEM_new()) == NULL) {
252 s->state = SSL_ST_ERR;
255 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
257 s->state = SSL_ST_ERR;
264 if (!ssl3_setup_buffers(s)) {
266 s->state = SSL_ST_ERR;
270 /* setup buffing BIO */
271 if (!ssl_init_wbio_buffer(s, 0)) {
273 s->state = SSL_ST_ERR;
277 /* don't push the buffering BIO quite yet */
279 if (!ssl3_init_finished_mac(s)) {
281 s->state = SSL_ST_ERR;
285 s->state = SSL3_ST_CW_CLNT_HELLO_A;
286 s->ctx->stats.sess_connect++;
288 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
290 * Should have been reset by ssl3_get_finished, too.
292 s->s3->change_cipher_spec = 0;
295 case SSL3_ST_CW_CLNT_HELLO_A:
296 case SSL3_ST_CW_CLNT_HELLO_B:
299 ret = ssl3_client_hello(s);
302 s->state = SSL3_ST_CR_SRVR_HELLO_A;
305 /* turn on buffering for the next lot of output */
306 if (s->bbio != s->wbio)
307 s->wbio = BIO_push(s->bbio, s->wbio);
311 case SSL3_ST_CR_SRVR_HELLO_A:
312 case SSL3_ST_CR_SRVR_HELLO_B:
313 ret = ssl3_get_server_hello(s);
318 s->state = SSL3_ST_CR_FINISHED_A;
319 #ifndef OPENSSL_NO_TLSEXT
320 if (s->tlsext_ticket_expected) {
321 /* receive renewed session ticket */
322 s->state = SSL3_ST_CR_SESSION_TICKET_A;
326 s->state = SSL3_ST_CR_CERT_A;
330 case SSL3_ST_CR_CERT_A:
331 case SSL3_ST_CR_CERT_B:
332 #ifndef OPENSSL_NO_TLSEXT
333 /* Noop (ret = 0) for everything but EAP-FAST. */
334 ret = ssl3_check_finished(s);
339 s->state = SSL3_ST_CR_FINISHED_A;
344 /* Check if it is anon DH/ECDH, SRP auth */
348 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
349 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
350 ret = ssl3_get_server_certificate(s);
353 #ifndef OPENSSL_NO_TLSEXT
354 if (s->tlsext_status_expected)
355 s->state = SSL3_ST_CR_CERT_STATUS_A;
357 s->state = SSL3_ST_CR_KEY_EXCH_A;
360 s->state = SSL3_ST_CR_KEY_EXCH_A;
366 s->state = SSL3_ST_CR_KEY_EXCH_A;
371 case SSL3_ST_CR_KEY_EXCH_A:
372 case SSL3_ST_CR_KEY_EXCH_B:
373 ret = ssl3_get_key_exchange(s);
376 s->state = SSL3_ST_CR_CERT_REQ_A;
380 * at this point we check that we have the required stuff from
383 if (!ssl3_check_cert_and_algorithm(s)) {
385 s->state = SSL_ST_ERR;
390 case SSL3_ST_CR_CERT_REQ_A:
391 case SSL3_ST_CR_CERT_REQ_B:
392 ret = ssl3_get_certificate_request(s);
395 s->state = SSL3_ST_CR_SRVR_DONE_A;
399 case SSL3_ST_CR_SRVR_DONE_A:
400 case SSL3_ST_CR_SRVR_DONE_B:
401 ret = ssl3_get_server_done(s);
404 #ifndef OPENSSL_NO_SRP
405 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
406 if ((ret = SRP_Calc_A_param(s)) <= 0) {
407 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
408 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
409 s->state = SSL_ST_ERR;
414 if (s->s3->tmp.cert_req)
415 s->state = SSL3_ST_CW_CERT_A;
417 s->state = SSL3_ST_CW_KEY_EXCH_A;
422 case SSL3_ST_CW_CERT_A:
423 case SSL3_ST_CW_CERT_B:
424 case SSL3_ST_CW_CERT_C:
425 case SSL3_ST_CW_CERT_D:
426 ret = ssl3_send_client_certificate(s);
429 s->state = SSL3_ST_CW_KEY_EXCH_A;
433 case SSL3_ST_CW_KEY_EXCH_A:
434 case SSL3_ST_CW_KEY_EXCH_B:
435 ret = ssl3_send_client_key_exchange(s);
439 * EAY EAY EAY need to check for DH fix cert sent back
442 * For TLS, cert_req is set to 2, so a cert chain of nothing is
443 * sent, but no verify packet is sent
446 * XXX: For now, we do not support client authentication in ECDH
447 * cipher suites with ECDH (rather than ECDSA) certificates. We
448 * need to skip the certificate verify message when client's
449 * ECDH public key is sent inside the client certificate.
451 if (s->s3->tmp.cert_req == 1) {
452 s->state = SSL3_ST_CW_CERT_VRFY_A;
454 s->state = SSL3_ST_CW_CHANGE_A;
456 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
457 s->state = SSL3_ST_CW_CHANGE_A;
463 case SSL3_ST_CW_CERT_VRFY_A:
464 case SSL3_ST_CW_CERT_VRFY_B:
465 ret = ssl3_send_client_verify(s);
468 s->state = SSL3_ST_CW_CHANGE_A;
472 case SSL3_ST_CW_CHANGE_A:
473 case SSL3_ST_CW_CHANGE_B:
474 ret = ssl3_send_change_cipher_spec(s,
476 SSL3_ST_CW_CHANGE_B);
480 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
481 s->state = SSL3_ST_CW_FINISHED_A;
483 if (s->s3->next_proto_neg_seen)
484 s->state = SSL3_ST_CW_NEXT_PROTO_A;
486 s->state = SSL3_ST_CW_FINISHED_A;
490 s->session->cipher = s->s3->tmp.new_cipher;
491 #ifdef OPENSSL_NO_COMP
492 s->session->compress_meth = 0;
494 if (s->s3->tmp.new_compression == NULL)
495 s->session->compress_meth = 0;
497 s->session->compress_meth = s->s3->tmp.new_compression->id;
499 if (!s->method->ssl3_enc->setup_key_block(s)) {
501 s->state = SSL_ST_ERR;
505 if (!s->method->ssl3_enc->change_cipher_state(s,
506 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
509 s->state = SSL_ST_ERR;
515 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
516 case SSL3_ST_CW_NEXT_PROTO_A:
517 case SSL3_ST_CW_NEXT_PROTO_B:
518 ret = ssl3_send_next_proto(s);
521 s->state = SSL3_ST_CW_FINISHED_A;
525 case SSL3_ST_CW_FINISHED_A:
526 case SSL3_ST_CW_FINISHED_B:
527 ret = ssl3_send_finished(s,
528 SSL3_ST_CW_FINISHED_A,
529 SSL3_ST_CW_FINISHED_B,
531 ssl3_enc->client_finished_label,
533 ssl3_enc->client_finished_label_len);
536 s->state = SSL3_ST_CW_FLUSH;
539 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
541 s->s3->tmp.next_state = SSL_ST_OK;
542 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
543 s->state = SSL_ST_OK;
544 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
545 s->s3->delay_buf_pop_ret = 0;
548 #ifndef OPENSSL_NO_TLSEXT
550 * Allow NewSessionTicket if ticket expected
552 if (s->tlsext_ticket_expected)
553 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
557 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
562 #ifndef OPENSSL_NO_TLSEXT
563 case SSL3_ST_CR_SESSION_TICKET_A:
564 case SSL3_ST_CR_SESSION_TICKET_B:
565 ret = ssl3_get_new_session_ticket(s);
568 s->state = SSL3_ST_CR_FINISHED_A;
572 case SSL3_ST_CR_CERT_STATUS_A:
573 case SSL3_ST_CR_CERT_STATUS_B:
574 ret = ssl3_get_cert_status(s);
577 s->state = SSL3_ST_CR_KEY_EXCH_A;
582 case SSL3_ST_CR_FINISHED_A:
583 case SSL3_ST_CR_FINISHED_B:
584 if (!s->s3->change_cipher_spec)
585 s->s3->flags |= SSL3_FLAGS_CCS_OK;
586 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
587 SSL3_ST_CR_FINISHED_B);
592 s->state = SSL3_ST_CW_CHANGE_A;
594 s->state = SSL_ST_OK;
598 case SSL3_ST_CW_FLUSH:
599 s->rwstate = SSL_WRITING;
600 if (BIO_flush(s->wbio) <= 0) {
604 s->rwstate = SSL_NOTHING;
605 s->state = s->s3->tmp.next_state;
609 /* clean a few things up */
610 ssl3_cleanup_key_block(s);
612 if (s->init_buf != NULL) {
613 BUF_MEM_free(s->init_buf);
618 * If we are not 'joining' the last two packets, remove the
621 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
622 ssl_free_wbio_buffer(s);
623 /* else do it later in ssl3_write */
629 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
631 s->ctx->stats.sess_hit++;
635 s->handshake_func = ssl3_connect;
636 s->ctx->stats.sess_connect_good++;
639 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
646 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
652 /* did we do anything */
653 if (!s->s3->tmp.reuse_message && !skip) {
655 if ((ret = BIO_flush(s->wbio)) <= 0)
659 if ((cb != NULL) && (s->state != state)) {
660 new_state = s->state;
662 cb(s, SSL_CB_CONNECT_LOOP, 1);
663 s->state = new_state;
673 cb(s, SSL_CB_CONNECT_EXIT, ret);
677 int ssl3_client_hello(SSL *s)
680 unsigned char *p, *d;
684 #ifndef OPENSSL_NO_COMP
689 buf = (unsigned char *)s->init_buf->data;
690 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
691 SSL_SESSION *sess = s->session;
692 if ((sess == NULL) || (sess->ssl_version != s->version) ||
693 #ifdef OPENSSL_NO_TLSEXT
694 !sess->session_id_length ||
697 * In the case of EAP-FAST, we can have a pre-shared
698 * "ticket" without a session ID.
700 (!sess->session_id_length && !sess->tlsext_tick) ||
702 (sess->not_resumable)) {
703 if (!ssl_get_new_session(s, 0))
706 if (s->method->version == DTLS_ANY_VERSION) {
707 /* Determine which DTLS version to use */
708 int options = s->options;
709 /* If DTLS 1.2 disabled correct the version number */
710 if (options & SSL_OP_NO_DTLSv1_2) {
711 if (tls1_suiteb(s)) {
712 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
713 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
717 * Disabling all versions is silly: return an error.
719 if (options & SSL_OP_NO_DTLSv1) {
720 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
724 * Update method so we don't use any DTLS 1.2 features.
726 s->method = DTLSv1_client_method();
727 s->version = DTLS1_VERSION;
730 * We only support one version: update method
732 if (options & SSL_OP_NO_DTLSv1)
733 s->method = DTLSv1_2_client_method();
734 s->version = DTLS1_2_VERSION;
736 s->client_version = s->version;
738 /* else use the pre-loaded session */
740 p = s->s3->client_random;
743 * for DTLS if client_random is initialized, reuse it, we are
744 * required to use same upon reply to HelloVerify
746 if (SSL_IS_DTLS(s)) {
749 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
758 if (i && ssl_fill_hello_random(s, 0, p,
759 sizeof(s->s3->client_random)) <= 0)
762 /* Do the message type and length last */
763 d = p = ssl_handshake_start(s);
766 * version indicates the negotiated version: for example from
767 * an SSLv2/v3 compatible client hello). The client_version
768 * field is the maximum version we permit and it is also
769 * used in RSA encrypted premaster secrets. Some servers can
770 * choke if we initially report a higher version then
771 * renegotiate to a lower one in the premaster secret. This
772 * didn't happen with TLS 1.0 as most servers supported it
773 * but it can with TLS 1.1 or later if the server only supports
776 * Possible scenario with previous logic:
777 * 1. Client hello indicates TLS 1.2
778 * 2. Server hello says TLS 1.0
779 * 3. RSA encrypted premaster secret uses 1.2.
780 * 4. Handhaked proceeds using TLS 1.0.
781 * 5. Server sends hello request to renegotiate.
782 * 6. Client hello indicates TLS v1.0 as we now
783 * know that is maximum server supports.
784 * 7. Server chokes on RSA encrypted premaster secret
785 * containing version 1.0.
787 * For interoperability it should be OK to always use the
788 * maximum version we support in client hello and then rely
789 * on the checking of version to ensure the servers isn't
790 * being inconsistent: for example initially negotiating with
791 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
792 * client_version in client hello and not resetting it to
793 * the negotiated version.
796 *(p++) = s->version >> 8;
797 *(p++) = s->version & 0xff;
798 s->client_version = s->version;
800 *(p++) = s->client_version >> 8;
801 *(p++) = s->client_version & 0xff;
805 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
806 p += SSL3_RANDOM_SIZE;
812 i = s->session->session_id_length;
815 if (i > (int)sizeof(s->session->session_id)) {
816 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
819 memcpy(p, s->session->session_id, i);
823 /* cookie stuff for DTLS */
824 if (SSL_IS_DTLS(s)) {
825 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
826 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
829 *(p++) = s->d1->cookie_len;
830 memcpy(p, s->d1->cookie, s->d1->cookie_len);
831 p += s->d1->cookie_len;
834 /* Ciphers supported */
835 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
837 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
840 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
842 * Some servers hang if client hello > 256 bytes as hack workaround
843 * chop number of supported ciphers to keep it well below this if we
846 if (TLS1_get_version(s) >= TLS1_2_VERSION
847 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
848 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
854 #ifdef OPENSSL_NO_COMP
858 if ((s->options & SSL_OP_NO_COMPRESSION)
859 || !s->ctx->comp_methods)
862 j = sk_SSL_COMP_num(s->ctx->comp_methods);
864 for (i = 0; i < j; i++) {
865 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
869 *(p++) = 0; /* Add the NULL method */
871 #ifndef OPENSSL_NO_TLSEXT
873 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
874 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
878 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
880 ssl3_send_alert(s, SSL3_AL_FATAL, al);
881 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
887 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
888 s->state = SSL3_ST_CW_CLNT_HELLO_B;
891 /* SSL3_ST_CW_CLNT_HELLO_B */
892 return ssl_do_write(s);
894 s->state = SSL_ST_ERR;
898 int ssl3_get_server_hello(SSL *s)
900 STACK_OF(SSL_CIPHER) *sk;
903 unsigned char *p, *d;
904 int i, al = SSL_AD_INTERNAL_ERROR, ok;
907 #ifndef OPENSSL_NO_COMP
911 * Hello verify request and/or server hello version may not match so set
912 * first packet if we're negotiating version.
917 n = s->method->ssl_get_message(s,
918 SSL3_ST_CR_SRVR_HELLO_A,
919 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
924 if (SSL_IS_DTLS(s)) {
926 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
927 if (s->d1->send_cookie == 0) {
928 s->s3->tmp.reuse_message = 1;
930 } else { /* already sent a cookie */
932 al = SSL_AD_UNEXPECTED_MESSAGE;
933 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
939 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
940 al = SSL_AD_UNEXPECTED_MESSAGE;
941 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
945 d = p = (unsigned char *)s->init_msg;
946 if (s->method->version == DTLS_ANY_VERSION) {
947 /* Work out correct protocol version to use */
948 int hversion = (p[0] << 8) | p[1];
949 int options = s->options;
950 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
951 s->method = DTLSv1_2_client_method();
952 else if (tls1_suiteb(s)) {
953 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
954 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
955 s->version = hversion;
956 al = SSL_AD_PROTOCOL_VERSION;
958 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
959 s->method = DTLSv1_client_method();
961 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
962 s->version = hversion;
963 al = SSL_AD_PROTOCOL_VERSION;
966 s->session->ssl_version = s->version = s->method->version;
969 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
970 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
971 s->version = (s->version & 0xff00) | p[1];
972 al = SSL_AD_PROTOCOL_VERSION;
977 /* load the server hello data */
978 /* load the server random */
979 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
980 p += SSL3_RANDOM_SIZE;
984 /* get the session-id */
987 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
988 al = SSL_AD_ILLEGAL_PARAMETER;
989 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
992 #ifndef OPENSSL_NO_TLSEXT
994 * Check if we can resume the session based on external pre-shared secret.
995 * EAP-FAST (RFC 4851) supports two types of session resumption.
996 * Resumption based on server-side state works with session IDs.
997 * Resumption based on pre-shared Protected Access Credentials (PACs)
998 * works by overriding the SessionTicket extension at the application
999 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1000 * servers would honour the session ID.) Therefore, the session ID alone
1001 * is not a reliable indicator of session resumption, so we first check if
1002 * we can resume, and later peek at the next handshake message to see if the
1003 * server wants to resume.
1005 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1006 s->session->tlsext_tick) {
1007 SSL_CIPHER *pref_cipher = NULL;
1008 s->session->master_key_length = sizeof(s->session->master_key);
1009 if (s->tls_session_secret_cb(s, s->session->master_key,
1010 &s->session->master_key_length,
1012 s->tls_session_secret_cb_arg)) {
1013 s->session->cipher = pref_cipher ?
1014 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1016 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1017 al = SSL_AD_INTERNAL_ERROR;
1021 #endif /* OPENSSL_NO_TLSEXT */
1023 if (j != 0 && j == s->session->session_id_length
1024 && memcmp(p, s->session->session_id, j) == 0) {
1025 if (s->sid_ctx_length != s->session->sid_ctx_length
1026 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1027 /* actually a client application bug */
1028 al = SSL_AD_ILLEGAL_PARAMETER;
1029 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1030 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1036 * If we were trying for session-id reuse but the server
1037 * didn't echo the ID, make a new SSL_SESSION.
1038 * In the case of EAP-FAST and PAC, we do not send a session ID,
1039 * so the PAC-based session secret is always preserved. It'll be
1040 * overwritten if the server refuses resumption.
1042 if (s->session->session_id_length > 0) {
1043 if (!ssl_get_new_session(s, 0)) {
1047 s->session->session_id_length = j;
1048 memcpy(s->session->session_id, p, j); /* j could be 0 */
1051 c = ssl_get_cipher_by_char(s, p);
1053 /* unknown cipher */
1054 al = SSL_AD_ILLEGAL_PARAMETER;
1055 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1058 /* Set version disabled mask now we know version */
1059 if (!SSL_USE_TLS1_2_CIPHERS(s))
1060 ct->mask_ssl = SSL_TLSV1_2;
1064 * If it is a disabled cipher we didn't send it in client hello, so
1067 if (c->algorithm_ssl & ct->mask_ssl ||
1068 c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a) {
1069 al = SSL_AD_ILLEGAL_PARAMETER;
1070 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1073 p += ssl_put_cipher_by_char(s, NULL, NULL);
1075 sk = ssl_get_ciphers_by_id(s);
1076 i = sk_SSL_CIPHER_find(sk, c);
1078 /* we did not say we would use this cipher */
1079 al = SSL_AD_ILLEGAL_PARAMETER;
1080 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1085 * Depending on the session caching (internal/external), the cipher
1086 * and/or cipher_id values may not be set. Make sure that cipher_id is
1087 * set and use it for comparison.
1089 if (s->session->cipher)
1090 s->session->cipher_id = s->session->cipher->id;
1091 if (s->hit && (s->session->cipher_id != c->id)) {
1092 /* Workaround is now obsolete */
1094 if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1097 al = SSL_AD_ILLEGAL_PARAMETER;
1098 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1099 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1103 s->s3->tmp.new_cipher = c;
1105 * Don't digest cached records if no sigalgs: we may need them for client
1108 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1110 /* lets get the compression algorithm */
1112 #ifdef OPENSSL_NO_COMP
1114 al = SSL_AD_ILLEGAL_PARAMETER;
1115 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1116 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1120 * If compression is disabled we'd better not try to resume a session
1121 * using compression.
1123 if (s->session->compress_meth != 0) {
1124 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1129 if (s->hit && j != s->session->compress_meth) {
1130 al = SSL_AD_ILLEGAL_PARAMETER;
1131 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1132 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1137 else if (s->options & SSL_OP_NO_COMPRESSION) {
1138 al = SSL_AD_ILLEGAL_PARAMETER;
1139 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1142 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1144 if ((j != 0) && (comp == NULL)) {
1145 al = SSL_AD_ILLEGAL_PARAMETER;
1146 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1147 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1150 s->s3->tmp.new_compression = comp;
1154 #ifndef OPENSSL_NO_TLSEXT
1155 /* TLS extensions */
1156 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1157 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1163 /* wrong packet length */
1164 al = SSL_AD_DECODE_ERROR;
1165 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1171 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1173 s->state = SSL_ST_ERR;
1177 int ssl3_get_server_certificate(SSL *s)
1179 int al, i, ok, ret = -1;
1180 unsigned long n, nc, llen, l;
1182 const unsigned char *q, *p;
1184 STACK_OF(X509) *sk = NULL;
1186 EVP_PKEY *pkey = NULL;
1187 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1190 n = s->method->ssl_get_message(s,
1193 -1, s->max_cert_list, &ok);
1198 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1199 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1200 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1201 s->s3->tmp.reuse_message = 1;
1205 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1206 al = SSL_AD_UNEXPECTED_MESSAGE;
1207 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1210 p = d = (unsigned char *)s->init_msg;
1212 if ((sk = sk_X509_new_null()) == NULL) {
1213 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1218 if (llen + 3 != n) {
1219 al = SSL_AD_DECODE_ERROR;
1220 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1223 for (nc = 0; nc < llen;) {
1224 if (nc + 3 > llen) {
1225 al = SSL_AD_DECODE_ERROR;
1226 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1227 SSL_R_CERT_LENGTH_MISMATCH);
1231 if ((l + nc + 3) > llen) {
1232 al = SSL_AD_DECODE_ERROR;
1233 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1234 SSL_R_CERT_LENGTH_MISMATCH);
1239 x = d2i_X509(NULL, &q, l);
1241 al = SSL_AD_BAD_CERTIFICATE;
1242 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1246 al = SSL_AD_DECODE_ERROR;
1247 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1248 SSL_R_CERT_LENGTH_MISMATCH);
1251 if (!sk_X509_push(sk, x)) {
1252 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1260 i = ssl_verify_cert_chain(s, sk);
1261 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1262 #ifndef OPENSSL_NO_KRB5
1263 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1264 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1265 #endif /* OPENSSL_NO_KRB5 */
1267 al = ssl_verify_alarm_type(s->verify_result);
1268 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1269 SSL_R_CERTIFICATE_VERIFY_FAILED);
1272 ERR_clear_error(); /* but we keep s->verify_result */
1274 sc = ssl_sess_cert_new();
1278 if (s->session->sess_cert)
1279 ssl_sess_cert_free(s->session->sess_cert);
1280 s->session->sess_cert = sc;
1282 sc->cert_chain = sk;
1284 * Inconsistency alert: cert_chain does include the peer's certificate,
1285 * which we don't include in s3_srvr.c
1287 x = sk_X509_value(sk, 0);
1290 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1293 pkey = X509_get_pubkey(x);
1295 /* VRS: allow null cert if auth == KRB5 */
1296 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1297 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1301 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1302 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1303 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1304 s->s3->tmp.new_cipher->name,
1305 s->s3->tmp.new_cipher->algorithm_mkey,
1306 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1307 #endif /* KSSL_DEBUG */
1309 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1312 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1313 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1317 i = ssl_cert_type(x, pkey);
1318 if (need_cert && i < 0) {
1321 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1322 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1327 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1328 if (exp_idx >= 0 && i != exp_idx) {
1330 al = SSL_AD_ILLEGAL_PARAMETER;
1331 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1332 SSL_R_WRONG_CERTIFICATE_TYPE);
1335 sc->peer_cert_type = i;
1336 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1338 * Why would the following ever happen? We just created sc a couple
1341 if (sc->peer_pkeys[i].x509 != NULL)
1342 X509_free(sc->peer_pkeys[i].x509);
1343 sc->peer_pkeys[i].x509 = x;
1344 sc->peer_key = &(sc->peer_pkeys[i]);
1346 if (s->session->peer != NULL)
1347 X509_free(s->session->peer);
1348 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1349 s->session->peer = x;
1351 sc->peer_cert_type = i;
1352 sc->peer_key = NULL;
1354 if (s->session->peer != NULL)
1355 X509_free(s->session->peer);
1356 s->session->peer = NULL;
1358 s->session->verify_result = s->verify_result;
1364 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1366 s->state = SSL_ST_ERR;
1369 EVP_PKEY_free(pkey);
1371 sk_X509_pop_free(sk, X509_free);
1375 int ssl3_get_key_exchange(SSL *s)
1377 #ifndef OPENSSL_NO_RSA
1378 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1381 unsigned char *param, *p;
1383 long i, param_len, n, alg_k, alg_a;
1384 EVP_PKEY *pkey = NULL;
1385 const EVP_MD *md = NULL;
1386 #ifndef OPENSSL_NO_RSA
1389 #ifndef OPENSSL_NO_DH
1392 #ifndef OPENSSL_NO_ECDH
1393 EC_KEY *ecdh = NULL;
1394 BN_CTX *bn_ctx = NULL;
1395 EC_POINT *srvr_ecpoint = NULL;
1397 int encoded_pt_len = 0;
1400 EVP_MD_CTX_init(&md_ctx);
1403 * use same message size as in ssl3_get_certificate_request() as
1404 * ServerKeyExchange message may be skipped
1406 n = s->method->ssl_get_message(s,
1407 SSL3_ST_CR_KEY_EXCH_A,
1408 SSL3_ST_CR_KEY_EXCH_B,
1409 -1, s->max_cert_list, &ok);
1413 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1415 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1417 * Can't skip server key exchange if this is an ephemeral
1420 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1421 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1422 al = SSL_AD_UNEXPECTED_MESSAGE;
1425 #ifndef OPENSSL_NO_PSK
1427 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1428 * identity hint is sent. Set session->sess_cert anyway to avoid
1431 if (alg_k & SSL_kPSK) {
1432 s->session->sess_cert = ssl_sess_cert_new();
1433 if (s->ctx->psk_identity_hint)
1434 OPENSSL_free(s->ctx->psk_identity_hint);
1435 s->ctx->psk_identity_hint = NULL;
1438 s->s3->tmp.reuse_message = 1;
1442 param = p = (unsigned char *)s->init_msg;
1443 if (s->session->sess_cert != NULL) {
1444 #ifndef OPENSSL_NO_RSA
1445 if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1446 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1447 s->session->sess_cert->peer_rsa_tmp = NULL;
1450 #ifndef OPENSSL_NO_DH
1451 if (s->session->sess_cert->peer_dh_tmp) {
1452 DH_free(s->session->sess_cert->peer_dh_tmp);
1453 s->session->sess_cert->peer_dh_tmp = NULL;
1456 #ifndef OPENSSL_NO_ECDH
1457 if (s->session->sess_cert->peer_ecdh_tmp) {
1458 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1459 s->session->sess_cert->peer_ecdh_tmp = NULL;
1463 s->session->sess_cert = ssl_sess_cert_new();
1466 /* Total length of the parameters including the length prefix */
1469 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1471 al = SSL_AD_DECODE_ERROR;
1473 #ifndef OPENSSL_NO_PSK
1474 if (alg_k & SSL_kPSK) {
1476 if (param_len > n) {
1477 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1483 * Store PSK identity hint for later use, hint is used in
1484 * ssl3_send_client_key_exchange. Assume that the maximum length of
1485 * a PSK identity hint can be as long as the maximum length of a PSK
1488 if (i > PSK_MAX_IDENTITY_LEN) {
1489 al = SSL_AD_HANDSHAKE_FAILURE;
1490 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1493 if (i > n - param_len) {
1494 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1495 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1500 s->session->psk_identity_hint = BUF_strndup((char *)p, i);
1501 if (s->session->psk_identity_hint == NULL) {
1502 al = SSL_AD_HANDSHAKE_FAILURE;
1503 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1510 #endif /* !OPENSSL_NO_PSK */
1511 #ifndef OPENSSL_NO_SRP
1512 if (alg_k & SSL_kSRP) {
1514 if (param_len > n) {
1515 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1520 if (i > n - param_len) {
1521 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1526 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1527 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1532 if (2 > n - param_len) {
1533 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1540 if (i > n - param_len) {
1541 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1546 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1547 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1552 if (1 > n - param_len) {
1553 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1558 i = (unsigned int)(p[0]);
1561 if (i > n - param_len) {
1562 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1567 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1568 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1573 if (2 > n - param_len) {
1574 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_SRP_B_LENGTH);
1587 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1588 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1594 if (!srp_verify_server_param(s, &al)) {
1595 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1599 /* We must check if there is a certificate */
1600 # ifndef OPENSSL_NO_RSA
1601 if (alg_a & SSL_aRSA)
1603 X509_get_pubkey(s->session->
1604 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1608 # ifndef OPENSSL_NO_DSA
1609 else if (alg_a & SSL_aDSS)
1611 X509_get_pubkey(s->session->
1612 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1616 #endif /* !OPENSSL_NO_SRP */
1617 #ifndef OPENSSL_NO_RSA
1618 if (alg_k & SSL_kRSA) {
1619 /* Temporary RSA keys only allowed in export ciphersuites */
1620 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1621 al = SSL_AD_UNEXPECTED_MESSAGE;
1622 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1625 if ((rsa = RSA_new()) == NULL) {
1626 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1631 if (param_len > n) {
1632 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1637 if (i > n - param_len) {
1638 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1643 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1644 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1649 if (2 > n - param_len) {
1650 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1657 if (i > n - param_len) {
1658 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1663 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1664 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1670 /* this should be because we are using an export cipher */
1671 if (alg_a & SSL_aRSA)
1673 X509_get_pubkey(s->session->
1674 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1680 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1681 al = SSL_AD_UNEXPECTED_MESSAGE;
1682 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1686 s->session->sess_cert->peer_rsa_tmp = rsa;
1689 #else /* OPENSSL_NO_RSA */
1692 #ifndef OPENSSL_NO_DH
1693 else if (alg_k & SSL_kEDH) {
1694 if ((dh = DH_new()) == NULL) {
1695 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1700 if (param_len > n) {
1701 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1706 if (i > n - param_len) {
1707 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1712 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1713 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1718 if (2 > n - param_len) {
1719 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1726 if (i > n - param_len) {
1727 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1732 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1733 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1738 if (2 > n - param_len) {
1739 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1746 if (i > n - param_len) {
1747 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1752 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1753 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1759 if (BN_is_zero(dh->pub_key)) {
1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1765 * Check that p and g are suitable enough
1773 if (!BN_is_odd(dh->p)) {
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1777 if (BN_is_negative(dh->g) || BN_is_zero(dh->g)
1778 || BN_is_one(dh->g)) {
1779 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1782 if ((tmp = BN_new()) == NULL
1783 || BN_copy(tmp, dh->p) == NULL
1784 || !BN_sub_word(tmp, 1)) {
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1789 if (BN_cmp(dh->g, tmp) >= 0) {
1791 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1797 # ifndef OPENSSL_NO_RSA
1798 if (alg_a & SSL_aRSA)
1800 X509_get_pubkey(s->session->
1801 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1805 # ifndef OPENSSL_NO_DSA
1806 else if (alg_a & SSL_aDSS)
1808 X509_get_pubkey(s->session->
1809 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1812 /* else anonymous DH, so no certificate or pkey. */
1814 s->session->sess_cert->peer_dh_tmp = dh;
1816 } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1817 al = SSL_AD_ILLEGAL_PARAMETER;
1818 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1819 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1822 #endif /* !OPENSSL_NO_DH */
1824 #ifndef OPENSSL_NO_ECDH
1825 else if (alg_k & SSL_kEECDH) {
1827 const EC_GROUP *group;
1829 if ((ecdh = EC_KEY_new()) == NULL) {
1830 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1835 * Extract elliptic curve parameters and the server's ephemeral ECDH
1836 * public key. Keep accumulating lengths of various components in
1837 * param_len and make sure it never exceeds n.
1841 * XXX: For now we only support named (not generic) curves and the
1842 * ECParameters in this case is just three bytes. We also need one
1843 * byte for the length of the encoded point
1846 if (param_len > n) {
1847 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1851 * Check curve is one of our preferences, if not server has sent an
1852 * invalid curve. ECParameters is 3 bytes.
1854 if (!tls1_check_curve(s, p, 3)) {
1855 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1859 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1860 al = SSL_AD_INTERNAL_ERROR;
1861 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1862 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1866 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1867 if (ngroup == NULL) {
1868 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1871 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1872 EC_GROUP_free(ngroup);
1873 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1876 EC_GROUP_free(ngroup);
1878 group = EC_KEY_get0_group(ecdh);
1880 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1881 (EC_GROUP_get_degree(group) > 163)) {
1882 al = SSL_AD_EXPORT_RESTRICTION;
1883 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1884 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1890 /* Next, get the encoded ECPoint */
1891 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1892 ((bn_ctx = BN_CTX_new()) == NULL)) {
1893 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1897 encoded_pt_len = *p; /* length of encoded point */
1900 if ((encoded_pt_len > n - param_len) ||
1901 (EC_POINT_oct2point(group, srvr_ecpoint,
1902 p, encoded_pt_len, bn_ctx) == 0)) {
1903 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1906 param_len += encoded_pt_len;
1909 p += encoded_pt_len;
1912 * The ECC/TLS specification does not mention the use of DSA to sign
1913 * ECParameters in the server key exchange message. We do support RSA
1917 # ifndef OPENSSL_NO_RSA
1918 else if (alg_a & SSL_aRSA)
1920 X509_get_pubkey(s->session->
1921 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1923 # ifndef OPENSSL_NO_ECDSA
1924 else if (alg_a & SSL_aECDSA)
1926 X509_get_pubkey(s->session->
1927 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1929 /* else anonymous ECDH, so no certificate or pkey. */
1930 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1931 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1933 BN_CTX_free(bn_ctx);
1935 EC_POINT_free(srvr_ecpoint);
1936 srvr_ecpoint = NULL;
1938 al = SSL_AD_UNEXPECTED_MESSAGE;
1939 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1942 #endif /* !OPENSSL_NO_ECDH */
1944 /* p points to the next byte, there are 'n' bytes left */
1946 /* if it was signed, check the signature */
1948 if (SSL_USE_SIGALGS(s)) {
1951 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1954 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1961 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1969 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1974 j = EVP_PKEY_size(pkey);
1977 * Check signature length. If n is 0 then signature is empty
1979 if ((i != n) || (n > j) || (n <= 0)) {
1980 /* wrong packet length */
1981 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1984 #ifndef OPENSSL_NO_RSA
1985 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1991 for (num = 2; num > 0; num--) {
1992 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1993 if (EVP_DigestInit_ex(&md_ctx,
1994 (num == 2) ? s->ctx->md5 : s->ctx->sha1,
1996 || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1997 SSL3_RANDOM_SIZE) <= 0
1998 || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1999 SSL3_RANDOM_SIZE) <= 0
2000 || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0
2001 || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) {
2002 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
2003 ERR_R_INTERNAL_ERROR);
2004 al = SSL_AD_INTERNAL_ERROR;
2010 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
2012 al = SSL_AD_DECRYPT_ERROR;
2013 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
2018 al = SSL_AD_DECRYPT_ERROR;
2019 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2025 if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
2026 || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
2027 SSL3_RANDOM_SIZE) <= 0
2028 || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
2029 SSL3_RANDOM_SIZE) <= 0
2030 || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) {
2031 al = SSL_AD_INTERNAL_ERROR;
2032 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB);
2035 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
2037 al = SSL_AD_DECRYPT_ERROR;
2038 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2043 /* aNULL, aSRP or kPSK do not need public keys */
2044 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
2045 /* Might be wrong key type, check it */
2046 if (ssl3_check_cert_and_algorithm(s))
2047 /* Otherwise this shouldn't happen */
2048 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2051 /* still data left over */
2053 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2057 EVP_PKEY_free(pkey);
2058 EVP_MD_CTX_cleanup(&md_ctx);
2061 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2063 EVP_PKEY_free(pkey);
2064 #ifndef OPENSSL_NO_RSA
2068 #ifndef OPENSSL_NO_DH
2072 #ifndef OPENSSL_NO_ECDH
2073 BN_CTX_free(bn_ctx);
2074 EC_POINT_free(srvr_ecpoint);
2078 EVP_MD_CTX_cleanup(&md_ctx);
2079 s->state = SSL_ST_ERR;
2083 int ssl3_get_certificate_request(SSL *s)
2086 unsigned long n, nc, l;
2087 unsigned int llen, ctype_num, i;
2088 X509_NAME *xn = NULL;
2089 const unsigned char *p, *q;
2091 STACK_OF(X509_NAME) *ca_sk = NULL;
2093 n = s->method->ssl_get_message(s,
2094 SSL3_ST_CR_CERT_REQ_A,
2095 SSL3_ST_CR_CERT_REQ_B,
2096 -1, s->max_cert_list, &ok);
2101 s->s3->tmp.cert_req = 0;
2103 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2104 s->s3->tmp.reuse_message = 1;
2106 * If we get here we don't need any cached handshake records as we
2107 * wont be doing client auth.
2109 if (s->s3->handshake_buffer) {
2110 if (!ssl3_digest_cached_records(s))
2116 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2117 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2118 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2122 /* TLS does not like anon-DH with client cert */
2123 if (s->version > SSL3_VERSION) {
2124 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2125 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2126 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2127 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2132 p = d = (unsigned char *)s->init_msg;
2134 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2135 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2139 /* get the certificate types */
2141 if (s->cert->ctypes) {
2142 OPENSSL_free(s->cert->ctypes);
2143 s->cert->ctypes = NULL;
2145 if (ctype_num > SSL3_CT_NUMBER) {
2146 /* If we exceed static buffer copy all to cert structure */
2147 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2148 if (s->cert->ctypes == NULL) {
2149 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2152 memcpy(s->cert->ctypes, p, ctype_num);
2153 s->cert->ctype_num = (size_t)ctype_num;
2154 ctype_num = SSL3_CT_NUMBER;
2156 for (i = 0; i < ctype_num; i++)
2157 s->s3->tmp.ctype[i] = p[i];
2159 if (SSL_USE_SIGALGS(s)) {
2162 * Check we have enough room for signature algorithms and following
2165 if ((unsigned long)(p - d + llen + 2) > n) {
2166 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2167 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2168 SSL_R_DATA_LENGTH_TOO_LONG);
2171 /* Clear certificate digests and validity flags */
2172 for (i = 0; i < SSL_PKEY_NUM; i++) {
2173 s->cert->pkeys[i].digest = NULL;
2174 s->cert->pkeys[i].valid_flags = 0;
2176 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2177 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2178 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2179 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2182 if (!tls1_process_sigalgs(s)) {
2183 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2184 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2190 /* get the CA RDNs */
2195 out = fopen("/tmp/vsign.der", "w");
2196 fwrite(p, 1, llen, out);
2201 if ((unsigned long)(p - d + llen) != n) {
2202 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2203 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2207 for (nc = 0; nc < llen;) {
2208 if (nc + 2 > llen) {
2209 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2210 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2214 if ((l + nc + 2) > llen) {
2215 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2216 goto cont; /* netscape bugs */
2217 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2218 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2224 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2225 /* If netscape tolerance is on, ignore errors */
2226 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2229 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2230 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2236 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2237 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2238 SSL_R_CA_DN_LENGTH_MISMATCH);
2241 if (!sk_X509_NAME_push(ca_sk, xn)) {
2242 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2256 /* we should setup a certificate to return.... */
2257 s->s3->tmp.cert_req = 1;
2258 s->s3->tmp.ctype_num = ctype_num;
2259 if (s->s3->tmp.ca_names != NULL)
2260 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2261 s->s3->tmp.ca_names = ca_sk;
2267 s->state = SSL_ST_ERR;
2271 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2275 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2277 return (X509_NAME_cmp(*a, *b));
2280 #ifndef OPENSSL_NO_TLSEXT
2281 int ssl3_get_new_session_ticket(SSL *s)
2283 int ok, al, ret = 0, ticklen;
2285 const unsigned char *p;
2287 unsigned long ticket_lifetime_hint;
2289 n = s->method->ssl_get_message(s,
2290 SSL3_ST_CR_SESSION_TICKET_A,
2291 SSL3_ST_CR_SESSION_TICKET_B,
2292 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2298 /* need at least ticket_lifetime_hint + ticket length */
2299 al = SSL_AD_DECODE_ERROR;
2300 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2304 p = d = (unsigned char *)s->init_msg;
2306 n2l(p, ticket_lifetime_hint);
2308 /* ticket_lifetime_hint + ticket_length + ticket */
2309 if (ticklen + 6 != n) {
2310 al = SSL_AD_DECODE_ERROR;
2311 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2315 /* Server is allowed to change its mind and send an empty ticket. */
2319 if (s->session->session_id_length > 0) {
2320 int i = s->session_ctx->session_cache_mode;
2321 SSL_SESSION *new_sess;
2323 * We reused an existing session, so we need to replace it with a new
2326 if (i & SSL_SESS_CACHE_CLIENT) {
2328 * Remove the old session from the cache
2330 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2331 if (s->session_ctx->remove_session_cb != NULL)
2332 s->session_ctx->remove_session_cb(s->session_ctx,
2335 /* We carry on if this fails */
2336 SSL_CTX_remove_session(s->session_ctx, s->session);
2340 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2341 al = SSL_AD_INTERNAL_ERROR;
2342 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2346 SSL_SESSION_free(s->session);
2347 s->session = new_sess;
2350 if (s->session->tlsext_tick) {
2351 OPENSSL_free(s->session->tlsext_tick);
2352 s->session->tlsext_ticklen = 0;
2354 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2355 if (!s->session->tlsext_tick) {
2356 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2359 memcpy(s->session->tlsext_tick, p, ticklen);
2360 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2361 s->session->tlsext_ticklen = ticklen;
2363 * There are two ways to detect a resumed ticket session. One is to set
2364 * an appropriate session ID and then the server must return a match in
2365 * ServerHello. This allows the normal client session ID matching to work
2366 * and we know much earlier that the ticket has been accepted. The
2367 * other way is to set zero length session ID when the ticket is
2368 * presented and rely on the handshake to determine session resumption.
2369 * We choose the former approach because this fits in with assumptions
2370 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2371 * SHA256 is disabled) hash of the ticket.
2373 EVP_Digest(p, ticklen,
2374 s->session->session_id, &s->session->session_id_length,
2375 # ifndef OPENSSL_NO_SHA256
2376 EVP_sha256(), NULL);
2383 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2385 s->state = SSL_ST_ERR;
2389 int ssl3_get_cert_status(SSL *s)
2392 unsigned long resplen, n;
2393 const unsigned char *p;
2395 n = s->method->ssl_get_message(s,
2396 SSL3_ST_CR_CERT_STATUS_A,
2397 SSL3_ST_CR_CERT_STATUS_B,
2403 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
2405 * The CertificateStatus message is optional even if
2406 * tlsext_status_expected is set
2408 s->s3->tmp.reuse_message = 1;
2411 /* need at least status type + length */
2412 al = SSL_AD_DECODE_ERROR;
2413 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2416 p = (unsigned char *)s->init_msg;
2417 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2418 al = SSL_AD_DECODE_ERROR;
2419 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2423 if (resplen + 4 != n) {
2424 al = SSL_AD_DECODE_ERROR;
2425 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2428 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2429 if (s->tlsext_ocsp_resp == NULL) {
2430 al = SSL_AD_INTERNAL_ERROR;
2431 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2434 s->tlsext_ocsp_resplen = resplen;
2436 if (s->ctx->tlsext_status_cb) {
2438 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2440 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2441 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2445 al = SSL_AD_INTERNAL_ERROR;
2446 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2452 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2453 s->state = SSL_ST_ERR;
2458 int ssl3_get_server_done(SSL *s)
2463 /* Second to last param should be very small, like 0 :-) */
2464 n = s->method->ssl_get_message(s,
2465 SSL3_ST_CR_SRVR_DONE_A,
2466 SSL3_ST_CR_SRVR_DONE_B,
2467 SSL3_MT_SERVER_DONE, 30, &ok);
2472 /* should contain no data */
2473 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2474 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2475 s->state = SSL_ST_ERR;
2482 #ifndef OPENSSL_NO_DH
2483 static DH *get_server_static_dh_key(SESS_CERT *scert)
2486 EVP_PKEY *spkey = NULL;
2487 int idx = scert->peer_cert_type;
2490 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2492 dh_srvr = EVP_PKEY_get1_DH(spkey);
2493 EVP_PKEY_free(spkey);
2495 if (dh_srvr == NULL)
2496 SSLerr(SSL_F_GET_SERVER_STATIC_DH_KEY, ERR_R_INTERNAL_ERROR);
2501 int ssl3_send_client_key_exchange(SSL *s)
2505 unsigned long alg_k;
2506 #ifndef OPENSSL_NO_RSA
2508 EVP_PKEY *pkey = NULL;
2510 #ifndef OPENSSL_NO_KRB5
2512 #endif /* OPENSSL_NO_KRB5 */
2513 #ifndef OPENSSL_NO_ECDH
2514 EC_KEY *clnt_ecdh = NULL;
2515 const EC_POINT *srvr_ecpoint = NULL;
2516 EVP_PKEY *srvr_pub_pkey = NULL;
2517 unsigned char *encodedPoint = NULL;
2518 int encoded_pt_len = 0;
2519 BN_CTX *bn_ctx = NULL;
2522 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2523 p = ssl_handshake_start(s);
2525 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2527 /* Fool emacs indentation */
2530 #ifndef OPENSSL_NO_RSA
2531 else if (alg_k & SSL_kRSA) {
2533 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2535 if (s->session->sess_cert == NULL) {
2537 * We should always have a server certificate with SSL_kRSA.
2539 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2540 ERR_R_INTERNAL_ERROR);
2544 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2545 rsa = s->session->sess_cert->peer_rsa_tmp;
2548 X509_get_pubkey(s->session->
2549 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2551 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2552 || (pkey->pkey.rsa == NULL)) {
2553 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2554 ERR_R_INTERNAL_ERROR);
2555 EVP_PKEY_free(pkey);
2558 rsa = pkey->pkey.rsa;
2559 EVP_PKEY_free(pkey);
2562 tmp_buf[0] = s->client_version >> 8;
2563 tmp_buf[1] = s->client_version & 0xff;
2564 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2567 s->session->master_key_length = sizeof tmp_buf;
2570 /* Fix buf for TLS and beyond */
2571 if (s->version > SSL3_VERSION)
2573 n = RSA_public_encrypt(sizeof tmp_buf,
2574 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
2576 if (s->options & SSL_OP_PKCS1_CHECK_1)
2578 if (s->options & SSL_OP_PKCS1_CHECK_2)
2582 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2583 SSL_R_BAD_RSA_ENCRYPT);
2587 /* Fix buf for TLS and beyond */
2588 if (s->version > SSL3_VERSION) {
2593 s->session->master_key_length =
2594 s->method->ssl3_enc->generate_master_secret(s,
2596 session->master_key,
2599 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2602 #ifndef OPENSSL_NO_KRB5
2603 else if (alg_k & SSL_kKRB5) {
2604 krb5_error_code krb5rc;
2605 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2606 /* krb5_data krb5_ap_req; */
2607 krb5_data *enc_ticket;
2608 krb5_data authenticator, *authp = NULL;
2609 EVP_CIPHER_CTX ciph_ctx;
2610 const EVP_CIPHER *enc = NULL;
2611 unsigned char iv[EVP_MAX_IV_LENGTH];
2612 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2613 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2614 int padl, outl = sizeof(epms);
2616 EVP_CIPHER_CTX_init(&ciph_ctx);
2619 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2621 # endif /* KSSL_DEBUG */
2624 # ifdef KRB5SENDAUTH
2626 authp = &authenticator;
2627 # endif /* KRB5SENDAUTH */
2629 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2630 enc = kssl_map_enc(kssl_ctx->enctype);
2635 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2636 if (krb5rc && kssl_err.text)
2637 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2640 # endif /* KSSL_DEBUG */
2643 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2644 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2649 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2650 * in place of RFC 2712 KerberosWrapper, as in:
2652 * Send ticket (copy to *p, set n = length)
2653 * n = krb5_ap_req.length;
2654 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2655 * if (krb5_ap_req.data)
2656 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2658 * Now using real RFC 2712 KerberosWrapper
2659 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2660 * Note: 2712 "opaque" types are here replaced
2661 * with a 2-byte length followed by the value.
2663 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2664 * Where "xx xx" = length bytes. Shown here with
2665 * optional authenticator omitted.
2668 /* KerberosWrapper.Ticket */
2669 s2n(enc_ticket->length, p);
2670 memcpy(p, enc_ticket->data, enc_ticket->length);
2671 p += enc_ticket->length;
2672 n = enc_ticket->length + 2;
2674 /* KerberosWrapper.Authenticator */
2675 if (authp && authp->length) {
2676 s2n(authp->length, p);
2677 memcpy(p, authp->data, authp->length);
2679 n += authp->length + 2;
2685 s2n(0, p); /* null authenticator length */
2689 tmp_buf[0] = s->client_version >> 8;
2690 tmp_buf[1] = s->client_version & 0xff;
2691 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2695 * 20010420 VRS. Tried it this way; failed.
2696 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2697 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2698 * kssl_ctx->length);
2699 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2702 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2703 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2704 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
2706 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2708 if (outl > (int)sizeof epms) {
2709 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2710 ERR_R_INTERNAL_ERROR);
2713 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2715 /* KerberosWrapper.EncryptedPreMasterSecret */
2717 memcpy(p, epms, outl);
2721 s->session->master_key_length =
2722 s->method->ssl3_enc->generate_master_secret(s,
2724 session->master_key,
2728 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2729 OPENSSL_cleanse(epms, outl);
2732 #ifndef OPENSSL_NO_DH
2733 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2734 DH *dh_srvr, *dh_clnt;
2735 SESS_CERT *scert = s->session->sess_cert;
2737 if (scert == NULL) {
2738 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2739 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2740 SSL_R_UNEXPECTED_MESSAGE);
2744 if (scert->peer_dh_tmp != NULL) {
2745 dh_srvr = scert->peer_dh_tmp;
2747 dh_srvr = get_server_static_dh_key(scert);
2748 if (dh_srvr == NULL)
2752 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2753 /* Use client certificate key */
2754 EVP_PKEY *clkey = s->cert->key->privatekey;
2757 dh_clnt = EVP_PKEY_get1_DH(clkey);
2758 if (dh_clnt == NULL) {
2759 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2760 ERR_R_INTERNAL_ERROR);
2764 /* generate a new random key */
2765 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2766 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2769 if (!DH_generate_key(dh_clnt)) {
2770 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2777 * use the 'p' output buffer for the DH key, but make sure to
2778 * clear it out afterwards
2781 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2782 if (scert->peer_dh_tmp == NULL)
2786 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2791 /* generate master key from the result */
2792 s->session->master_key_length =
2793 s->method->ssl3_enc->generate_master_secret(s,
2795 session->master_key,
2800 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2803 /* send off the data */
2804 n = BN_num_bytes(dh_clnt->pub_key);
2806 BN_bn2bin(dh_clnt->pub_key, p);
2814 #ifndef OPENSSL_NO_ECDH
2815 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2816 const EC_GROUP *srvr_group = NULL;
2818 int ecdh_clnt_cert = 0;
2821 if (s->session->sess_cert == NULL) {
2822 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2823 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2824 SSL_R_UNEXPECTED_MESSAGE);
2829 * Did we send out the client's ECDH share for use in premaster
2830 * computation as part of client certificate? If so, set
2831 * ecdh_clnt_cert to 1.
2833 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2835 * XXX: For now, we do not support client
2836 * authentication using ECDH certificates.
2837 * To add such support, one needs to add
2838 * code that checks for appropriate
2839 * conditions and sets ecdh_clnt_cert to 1.
2840 * For example, the cert have an ECC
2841 * key on the same curve as the server's
2842 * and the key should be authorized for
2845 * One also needs to add code in ssl3_connect
2846 * to skip sending the certificate verify
2849 * if ((s->cert->key->privatekey != NULL) &&
2850 * (s->cert->key->privatekey->type ==
2851 * EVP_PKEY_EC) && ...)
2852 * ecdh_clnt_cert = 1;
2856 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2857 tkey = s->session->sess_cert->peer_ecdh_tmp;
2859 /* Get the Server Public Key from Cert */
2861 X509_get_pubkey(s->session->
2862 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2863 if ((srvr_pub_pkey == NULL)
2864 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2865 || (srvr_pub_pkey->pkey.ec == NULL)) {
2866 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2867 ERR_R_INTERNAL_ERROR);
2871 tkey = srvr_pub_pkey->pkey.ec;
2874 srvr_group = EC_KEY_get0_group(tkey);
2875 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2877 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2878 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2879 ERR_R_INTERNAL_ERROR);
2883 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2884 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2885 ERR_R_MALLOC_FAILURE);
2889 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2890 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2893 if (ecdh_clnt_cert) {
2895 * Reuse key info from our certificate We only need our
2896 * private key to perform the ECDH computation.
2898 const BIGNUM *priv_key;
2899 tkey = s->cert->key->privatekey->pkey.ec;
2900 priv_key = EC_KEY_get0_private_key(tkey);
2901 if (priv_key == NULL) {
2902 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2903 ERR_R_MALLOC_FAILURE);
2906 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2907 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2911 /* Generate a new ECDH key pair */
2912 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2913 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2920 * use the 'p' output buffer for the ECDH key, but make sure to
2921 * clear it out afterwards
2924 field_size = EC_GROUP_get_degree(srvr_group);
2925 if (field_size <= 0) {
2926 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2929 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
2932 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2936 /* generate master key from the result */
2937 s->session->master_key_length =
2938 s->method->ssl3_enc->generate_master_secret(s,
2940 session->master_key,
2943 memset(p, 0, n); /* clean up */
2945 if (ecdh_clnt_cert) {
2946 /* Send empty client key exch message */
2950 * First check the size of encoding and allocate memory
2954 EC_POINT_point2oct(srvr_group,
2955 EC_KEY_get0_public_key(clnt_ecdh),
2956 POINT_CONVERSION_UNCOMPRESSED,
2959 encodedPoint = (unsigned char *)
2960 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2961 bn_ctx = BN_CTX_new();
2962 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2963 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2964 ERR_R_MALLOC_FAILURE);
2968 /* Encode the public key */
2969 n = EC_POINT_point2oct(srvr_group,
2970 EC_KEY_get0_public_key(clnt_ecdh),
2971 POINT_CONVERSION_UNCOMPRESSED,
2972 encodedPoint, encoded_pt_len, bn_ctx);
2974 *p = n; /* length of encoded point */
2975 /* Encoded point will be copied here */
2977 /* copy the point */
2978 memcpy((unsigned char *)p, encodedPoint, n);
2979 /* increment n to account for length field */
2983 /* Free allocated memory */
2984 BN_CTX_free(bn_ctx);
2985 if (encodedPoint != NULL)
2986 OPENSSL_free(encodedPoint);
2987 if (clnt_ecdh != NULL)
2988 EC_KEY_free(clnt_ecdh);
2989 EVP_PKEY_free(srvr_pub_pkey);
2991 #endif /* !OPENSSL_NO_ECDH */
2992 else if (alg_k & SSL_kGOST) {
2993 /* GOST key exchange message creation */
2994 EVP_PKEY_CTX *pkey_ctx;
2997 unsigned int md_len;
2999 unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
3000 EVP_MD_CTX *ukm_hash;
3004 * Get server sertificate PKEY and create ctx from it
3008 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
3012 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
3014 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3015 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3019 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
3020 X509_get_pubkey(peer_cert), NULL);
3021 if (pkey_ctx == NULL) {
3022 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3023 ERR_R_MALLOC_FAILURE);
3027 * If we have send a certificate, and certificate key
3029 * * parameters match those of server certificate, use
3030 * certificate key for key exchange
3033 /* Otherwise, generate ephemeral key pair */
3035 if (pkey_ctx == NULL
3036 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3037 /* Generate session key */
3038 || RAND_bytes(premaster_secret, 32) <= 0) {
3039 EVP_PKEY_CTX_free(pkey_ctx);
3040 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3041 ERR_R_INTERNAL_ERROR);
3045 * Compute shared IV and store it in algorithm-specific context
3048 ukm_hash = EVP_MD_CTX_create();
3049 if (EVP_DigestInit(ukm_hash,
3050 EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0
3051 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
3052 SSL3_RANDOM_SIZE) <= 0
3053 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
3054 SSL3_RANDOM_SIZE) <= 0
3055 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3056 EVP_MD_CTX_destroy(ukm_hash);
3057 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3058 ERR_R_INTERNAL_ERROR);
3061 EVP_MD_CTX_destroy(ukm_hash);
3062 if (EVP_PKEY_CTX_ctrl
3063 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
3065 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3069 /* Make GOST keytransport blob message */
3071 * Encapsulate it into sequence
3073 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
3075 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
3077 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3081 if (msglen >= 0x80) {
3083 *(p++) = msglen & 0xff;
3086 *(p++) = msglen & 0xff;
3089 memcpy(p, tmp, msglen);
3090 EVP_PKEY_CTX_free(pkey_ctx);
3091 s->session->master_key_length =
3092 s->method->ssl3_enc->generate_master_secret(s,
3094 session->master_key,
3097 EVP_PKEY_free(pub_key);
3100 #ifndef OPENSSL_NO_SRP
3101 else if (alg_k & SSL_kSRP) {
3102 if (s->srp_ctx.A != NULL) {
3103 /* send off the data */
3104 n = BN_num_bytes(s->srp_ctx.A);
3106 BN_bn2bin(s->srp_ctx.A, p);
3109 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3110 ERR_R_INTERNAL_ERROR);
3113 if (s->session->srp_username != NULL)
3114 OPENSSL_free(s->session->srp_username);
3115 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
3116 if (s->session->srp_username == NULL) {
3117 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3118 ERR_R_MALLOC_FAILURE);
3122 if ((s->session->master_key_length =
3123 SRP_generate_client_master_secret(s,
3124 s->session->master_key)) <
3126 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3127 ERR_R_INTERNAL_ERROR);
3132 #ifndef OPENSSL_NO_PSK
3133 else if (alg_k & SSL_kPSK) {
3135 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
3136 * \0-terminated identity. The last byte is for us for simulating
3139 char identity[PSK_MAX_IDENTITY_LEN + 2];
3140 size_t identity_len;
3141 unsigned char *t = NULL;
3142 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
3143 unsigned int pre_ms_len = 0, psk_len = 0;
3147 if (s->psk_client_callback == NULL) {
3148 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3149 SSL_R_PSK_NO_CLIENT_CB);
3153 memset(identity, 0, sizeof(identity));
3154 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
3155 identity, sizeof(identity) - 1,
3157 sizeof(psk_or_pre_ms));
3158 if (psk_len > PSK_MAX_PSK_LEN) {
3159 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3160 ERR_R_INTERNAL_ERROR);
3162 } else if (psk_len == 0) {
3163 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3164 SSL_R_PSK_IDENTITY_NOT_FOUND);
3167 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
3168 identity_len = strlen(identity);
3169 if (identity_len > PSK_MAX_IDENTITY_LEN) {
3170 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3171 ERR_R_INTERNAL_ERROR);
3174 /* create PSK pre_master_secret */
3175 pre_ms_len = 2 + psk_len + 2 + psk_len;
3177 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
3179 memset(t, 0, psk_len);
3183 if (s->session->psk_identity_hint != NULL)
3184 OPENSSL_free(s->session->psk_identity_hint);
3185 s->session->psk_identity_hint =
3186 BUF_strdup(s->ctx->psk_identity_hint);
3187 if (s->ctx->psk_identity_hint != NULL
3188 && s->session->psk_identity_hint == NULL) {
3189 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3190 ERR_R_MALLOC_FAILURE);
3194 if (s->session->psk_identity != NULL)
3195 OPENSSL_free(s->session->psk_identity);
3196 s->session->psk_identity = BUF_strdup(identity);
3197 if (s->session->psk_identity == NULL) {
3198 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3199 ERR_R_MALLOC_FAILURE);
3203 s->session->master_key_length =
3204 s->method->ssl3_enc->generate_master_secret(s,
3206 session->master_key,
3209 s2n(identity_len, p);
3210 memcpy(p, identity, identity_len);
3211 n = 2 + identity_len;
3214 OPENSSL_cleanse(identity, sizeof(identity));
3215 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
3217 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3223 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3224 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3228 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
3229 s->state = SSL3_ST_CW_KEY_EXCH_B;
3232 /* SSL3_ST_CW_KEY_EXCH_B */
3233 return ssl_do_write(s);
3235 #ifndef OPENSSL_NO_ECDH
3236 BN_CTX_free(bn_ctx);
3237 if (encodedPoint != NULL)
3238 OPENSSL_free(encodedPoint);
3239 if (clnt_ecdh != NULL)
3240 EC_KEY_free(clnt_ecdh);
3241 EVP_PKEY_free(srvr_pub_pkey);
3243 s->state = SSL_ST_ERR;
3247 int ssl3_send_client_verify(SSL *s)
3250 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3252 EVP_PKEY_CTX *pctx = NULL;
3258 EVP_MD_CTX_init(&mctx);
3260 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3261 p = ssl_handshake_start(s);
3262 pkey = s->cert->key->privatekey;
3263 /* Create context from key and test if sha1 is allowed as digest */
3264 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3265 if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) {
3266 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3269 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3270 if (!SSL_USE_SIGALGS(s))
3271 s->method->ssl3_enc->cert_verify_mac(s,
3274 [MD5_DIGEST_LENGTH]));
3279 * For TLS v1.2 send signature algorithm and signature using agreed
3280 * digest and cached handshake records.
3282 if (SSL_USE_SIGALGS(s)) {
3285 const EVP_MD *md = s->cert->key->digest;
3286 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3287 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3288 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3293 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3296 if (!EVP_SignInit_ex(&mctx, md, NULL)
3297 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3298 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3299 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3304 if (!ssl3_digest_cached_records(s))
3307 #ifndef OPENSSL_NO_RSA
3308 if (pkey->type == EVP_PKEY_RSA) {
3309 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3310 if (RSA_sign(NID_md5_sha1, data,
3311 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3312 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3313 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3320 #ifndef OPENSSL_NO_DSA
3321 if (pkey->type == EVP_PKEY_DSA) {
3322 if (!DSA_sign(pkey->save_type,
3323 &(data[MD5_DIGEST_LENGTH]),
3324 SHA_DIGEST_LENGTH, &(p[2]),
3325 (unsigned int *)&j, pkey->pkey.dsa)) {
3326 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3333 #ifndef OPENSSL_NO_ECDSA
3334 if (pkey->type == EVP_PKEY_EC) {
3335 if (!ECDSA_sign(pkey->save_type,
3336 &(data[MD5_DIGEST_LENGTH]),
3337 SHA_DIGEST_LENGTH, &(p[2]),
3338 (unsigned int *)&j, pkey->pkey.ec)) {
3339 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3346 if (pkey->type == NID_id_GostR3410_94
3347 || pkey->type == NID_id_GostR3410_2001) {
3348 unsigned char signbuf[64];
3350 size_t sigsize = 64;
3351 s->method->ssl3_enc->cert_verify_mac(s,
3352 NID_id_GostR3411_94, data);
3353 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3354 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3357 for (i = 63, j = 0; i >= 0; j++, i--) {
3358 p[2 + j] = signbuf[i];
3363 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3366 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
3367 s->state = SSL3_ST_CW_CERT_VRFY_B;
3369 EVP_MD_CTX_cleanup(&mctx);
3370 EVP_PKEY_CTX_free(pctx);
3371 return ssl_do_write(s);
3373 EVP_MD_CTX_cleanup(&mctx);
3374 EVP_PKEY_CTX_free(pctx);
3375 s->state = SSL_ST_ERR;
3380 * Check a certificate can be used for client authentication. Currently check
3381 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3382 * certificates can be used and optionally checks suitability for Suite B.
3384 static int ssl3_check_client_certificate(SSL *s)
3386 unsigned long alg_k;
3387 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3389 /* If no suitable signature algorithm can't use certificate */
3390 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3393 * If strict mode check suitability of chain before using it. This also
3394 * adjusts suite B digest if necessary.
3396 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3397 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3399 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3400 /* See if we can use client certificate for fixed DH */
3401 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3402 SESS_CERT *scert = s->session->sess_cert;
3403 int i = scert->peer_cert_type;
3404 EVP_PKEY *clkey = NULL, *spkey = NULL;
3405 clkey = s->cert->key->privatekey;
3406 /* If client key not DH assume it can be used */
3407 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3410 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3412 /* Compare server and client parameters */
3413 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3414 EVP_PKEY_free(spkey);
3418 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3423 int ssl3_send_client_certificate(SSL *s)
3426 EVP_PKEY *pkey = NULL;
3429 if (s->state == SSL3_ST_CW_CERT_A) {
3430 /* Let cert callback update client certificates if required */
3431 if (s->cert->cert_cb) {
3432 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3434 s->rwstate = SSL_X509_LOOKUP;
3438 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3439 s->state = SSL_ST_ERR;
3442 s->rwstate = SSL_NOTHING;
3444 if (ssl3_check_client_certificate(s))
3445 s->state = SSL3_ST_CW_CERT_C;
3447 s->state = SSL3_ST_CW_CERT_B;
3450 /* We need to get a client cert */
3451 if (s->state == SSL3_ST_CW_CERT_B) {
3453 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3454 * return(-1); We then get retied later
3456 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3458 s->rwstate = SSL_X509_LOOKUP;
3461 s->rwstate = SSL_NOTHING;
3462 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3463 s->state = SSL3_ST_CW_CERT_B;
3464 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3466 } else if (i == 1) {
3468 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3469 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3475 EVP_PKEY_free(pkey);
3476 if (i && !ssl3_check_client_certificate(s))
3479 if (s->version == SSL3_VERSION) {
3480 s->s3->tmp.cert_req = 0;
3481 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3484 s->s3->tmp.cert_req = 2;
3488 /* Ok, we have a cert */
3489 s->state = SSL3_ST_CW_CERT_C;
3492 if (s->state == SSL3_ST_CW_CERT_C) {
3493 s->state = SSL3_ST_CW_CERT_D;
3494 if (!ssl3_output_cert_chain(s,
3495 (s->s3->tmp.cert_req ==
3496 2) ? NULL : s->cert->key)) {
3497 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3498 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3499 s->state = SSL_ST_ERR;
3503 /* SSL3_ST_CW_CERT_D */
3504 return ssl_do_write(s);
3507 #define has_bits(i,m) (((i)&(m)) == (m))
3509 int ssl3_check_cert_and_algorithm(SSL *s)
3513 EVP_PKEY *pkey = NULL;
3516 #ifndef OPENSSL_NO_RSA
3519 #ifndef OPENSSL_NO_DH
3522 int al = SSL_AD_HANDSHAKE_FAILURE;
3524 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3525 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3527 /* we don't have a certificate */
3528 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3531 sc = s->session->sess_cert;
3533 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3536 #ifndef OPENSSL_NO_RSA
3537 rsa = s->session->sess_cert->peer_rsa_tmp;
3539 #ifndef OPENSSL_NO_DH
3540 dh = s->session->sess_cert->peer_dh_tmp;
3543 /* This is the passed certificate */
3545 idx = sc->peer_cert_type;
3546 #ifndef OPENSSL_NO_ECDH
3547 if (idx == SSL_PKEY_ECC) {
3548 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3550 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3555 } else if (alg_a & SSL_aECDSA) {
3556 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3557 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3559 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3560 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3564 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3565 pkey_bits = EVP_PKEY_bits(pkey);
3566 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3567 EVP_PKEY_free(pkey);
3569 /* Check that we have a certificate if we require one */
3570 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3571 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3572 SSL_R_MISSING_RSA_SIGNING_CERT);
3575 #ifndef OPENSSL_NO_DSA
3576 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3577 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3578 SSL_R_MISSING_DSA_SIGNING_CERT);
3582 #ifndef OPENSSL_NO_RSA
3583 if (alg_k & SSL_kRSA) {
3584 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3585 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3586 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3587 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3589 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3590 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3591 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3592 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3593 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3597 /* server key exchange is not allowed. */
3598 al = SSL_AD_INTERNAL_ERROR;
3599 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3606 #ifndef OPENSSL_NO_DH
3607 if ((alg_k & SSL_kEDH) && dh == NULL) {
3608 al = SSL_AD_INTERNAL_ERROR;
3609 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3612 if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3613 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3614 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3615 SSL_R_MISSING_DH_RSA_CERT);
3618 # ifndef OPENSSL_NO_DSA
3619 if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3620 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3621 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3622 SSL_R_MISSING_DH_DSA_CERT);
3627 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3629 if (alg_k & SSL_kDHE) {
3630 dh_size = BN_num_bits(dh->p);
3632 DH *dh_srvr = get_server_static_dh_key(sc);
3633 if (dh_srvr == NULL)
3635 dh_size = BN_num_bits(dh_srvr->p);
3639 if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024)
3640 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
3641 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
3645 #endif /* !OPENSSL_NO_DH */
3647 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3648 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3649 #ifndef OPENSSL_NO_RSA
3650 if (alg_k & SSL_kRSA) {
3652 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3653 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3655 } else if (BN_num_bits(rsa->n) >
3656 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3657 /* We have a temporary RSA key but it's too large. */
3658 al = SSL_AD_EXPORT_RESTRICTION;
3659 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3660 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3665 #ifndef OPENSSL_NO_DH
3666 if (alg_k & SSL_kDHE) {
3667 if (BN_num_bits(dh->p) >
3668 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3669 /* We have a temporary DH key but it's too large. */
3670 al = SSL_AD_EXPORT_RESTRICTION;
3671 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3672 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3675 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3676 /* The cert should have had an export DH key. */
3677 al = SSL_AD_EXPORT_RESTRICTION;
3678 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3679 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3684 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3685 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3691 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3696 #ifndef OPENSSL_NO_TLSEXT
3698 * Normally, we can tell if the server is resuming the session from
3699 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3700 * message after the ServerHello to determine if the server is resuming.
3701 * Therefore, we allow EAP-FAST to peek ahead.
3702 * ssl3_check_finished returns 1 if we are resuming from an external
3703 * pre-shared secret, we have a "ticket" and the next server handshake message
3704 * is Finished; and 0 otherwise. It returns -1 upon an error.
3706 static int ssl3_check_finished(SSL *s)
3710 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3711 !s->session->tlsext_tick)
3714 /* Need to permit this temporarily, in case the next message is Finished. */
3715 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3717 * This function is called when we might get a Certificate message instead,
3718 * so permit appropriate message length.
3719 * We ignore the return value as we're only interested in the message type
3720 * and not its length.
3722 s->method->ssl_get_message(s,
3725 -1, s->max_cert_list, &ok);
3726 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3731 s->s3->tmp.reuse_message = 1;
3733 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3736 /* If we're not done, then the CCS arrived early and we should bail. */
3737 if (s->s3->change_cipher_spec) {
3738 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3739 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3746 # ifndef OPENSSL_NO_NEXTPROTONEG
3747 int ssl3_send_next_proto(SSL *s)
3749 unsigned int len, padding_len;
3752 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3753 len = s->next_proto_negotiated_len;
3754 padding_len = 32 - ((len + 2) % 32);
3755 d = (unsigned char *)s->init_buf->data;
3757 memcpy(d + 5, s->next_proto_negotiated, len);
3758 d[5 + len] = padding_len;
3759 memset(d + 6 + len, 0, padding_len);
3760 *(d++) = SSL3_MT_NEXT_PROTO;
3761 l2n3(2 + len + padding_len, d);
3762 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3763 s->init_num = 4 + 2 + len + padding_len;
3767 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3769 #endif /* !OPENSSL_NO_NEXTPROTONEG */
3770 #endif /* !OPENSSL_NO_TLSEXT */
3772 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3775 #ifndef OPENSSL_NO_ENGINE
3776 if (s->ctx->client_cert_engine) {
3777 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3778 SSL_get_client_CA_list(s),
3779 px509, ppkey, NULL, NULL, NULL);
3784 if (s->ctx->client_cert_cb)
3785 i = s->ctx->client_cert_cb(s, px509, ppkey);