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)) {
269 /* setup buffing BIO */
270 if (!ssl_init_wbio_buffer(s, 0)) {
272 s->state = SSL_ST_ERR;
276 /* don't push the buffering BIO quite yet */
278 ssl3_init_finished_mac(s);
280 s->state = SSL3_ST_CW_CLNT_HELLO_A;
281 s->ctx->stats.sess_connect++;
283 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
285 * Should have been reset by ssl3_get_finished, too.
287 s->s3->change_cipher_spec = 0;
290 case SSL3_ST_CW_CLNT_HELLO_A:
291 case SSL3_ST_CW_CLNT_HELLO_B:
294 ret = ssl3_client_hello(s);
297 s->state = SSL3_ST_CR_SRVR_HELLO_A;
300 /* turn on buffering for the next lot of output */
301 if (s->bbio != s->wbio)
302 s->wbio = BIO_push(s->bbio, s->wbio);
306 case SSL3_ST_CR_SRVR_HELLO_A:
307 case SSL3_ST_CR_SRVR_HELLO_B:
308 ret = ssl3_get_server_hello(s);
313 s->state = SSL3_ST_CR_FINISHED_A;
314 #ifndef OPENSSL_NO_TLSEXT
315 if (s->tlsext_ticket_expected) {
316 /* receive renewed session ticket */
317 s->state = SSL3_ST_CR_SESSION_TICKET_A;
321 s->state = SSL3_ST_CR_CERT_A;
325 case SSL3_ST_CR_CERT_A:
326 case SSL3_ST_CR_CERT_B:
327 #ifndef OPENSSL_NO_TLSEXT
328 /* Noop (ret = 0) for everything but EAP-FAST. */
329 ret = ssl3_check_finished(s);
334 s->state = SSL3_ST_CR_FINISHED_A;
339 /* Check if it is anon DH/ECDH, SRP auth */
343 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
344 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
345 ret = ssl3_get_server_certificate(s);
348 #ifndef OPENSSL_NO_TLSEXT
349 if (s->tlsext_status_expected)
350 s->state = SSL3_ST_CR_CERT_STATUS_A;
352 s->state = SSL3_ST_CR_KEY_EXCH_A;
355 s->state = SSL3_ST_CR_KEY_EXCH_A;
361 s->state = SSL3_ST_CR_KEY_EXCH_A;
366 case SSL3_ST_CR_KEY_EXCH_A:
367 case SSL3_ST_CR_KEY_EXCH_B:
368 ret = ssl3_get_key_exchange(s);
371 s->state = SSL3_ST_CR_CERT_REQ_A;
375 * at this point we check that we have the required stuff from
378 if (!ssl3_check_cert_and_algorithm(s)) {
380 s->state = SSL_ST_ERR;
385 case SSL3_ST_CR_CERT_REQ_A:
386 case SSL3_ST_CR_CERT_REQ_B:
387 ret = ssl3_get_certificate_request(s);
390 s->state = SSL3_ST_CR_SRVR_DONE_A;
394 case SSL3_ST_CR_SRVR_DONE_A:
395 case SSL3_ST_CR_SRVR_DONE_B:
396 ret = ssl3_get_server_done(s);
399 #ifndef OPENSSL_NO_SRP
400 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
401 if ((ret = SRP_Calc_A_param(s)) <= 0) {
402 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
403 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
404 s->state = SSL_ST_ERR;
409 if (s->s3->tmp.cert_req)
410 s->state = SSL3_ST_CW_CERT_A;
412 s->state = SSL3_ST_CW_KEY_EXCH_A;
417 case SSL3_ST_CW_CERT_A:
418 case SSL3_ST_CW_CERT_B:
419 case SSL3_ST_CW_CERT_C:
420 case SSL3_ST_CW_CERT_D:
421 ret = ssl3_send_client_certificate(s);
424 s->state = SSL3_ST_CW_KEY_EXCH_A;
428 case SSL3_ST_CW_KEY_EXCH_A:
429 case SSL3_ST_CW_KEY_EXCH_B:
430 ret = ssl3_send_client_key_exchange(s);
434 * EAY EAY EAY need to check for DH fix cert sent back
437 * For TLS, cert_req is set to 2, so a cert chain of nothing is
438 * sent, but no verify packet is sent
441 * XXX: For now, we do not support client authentication in ECDH
442 * cipher suites with ECDH (rather than ECDSA) certificates. We
443 * need to skip the certificate verify message when client's
444 * ECDH public key is sent inside the client certificate.
446 if (s->s3->tmp.cert_req == 1) {
447 s->state = SSL3_ST_CW_CERT_VRFY_A;
449 s->state = SSL3_ST_CW_CHANGE_A;
451 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
452 s->state = SSL3_ST_CW_CHANGE_A;
458 case SSL3_ST_CW_CERT_VRFY_A:
459 case SSL3_ST_CW_CERT_VRFY_B:
460 ret = ssl3_send_client_verify(s);
463 s->state = SSL3_ST_CW_CHANGE_A;
467 case SSL3_ST_CW_CHANGE_A:
468 case SSL3_ST_CW_CHANGE_B:
469 ret = ssl3_send_change_cipher_spec(s,
471 SSL3_ST_CW_CHANGE_B);
475 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
476 s->state = SSL3_ST_CW_FINISHED_A;
478 if (s->s3->next_proto_neg_seen)
479 s->state = SSL3_ST_CW_NEXT_PROTO_A;
481 s->state = SSL3_ST_CW_FINISHED_A;
485 s->session->cipher = s->s3->tmp.new_cipher;
486 #ifdef OPENSSL_NO_COMP
487 s->session->compress_meth = 0;
489 if (s->s3->tmp.new_compression == NULL)
490 s->session->compress_meth = 0;
492 s->session->compress_meth = s->s3->tmp.new_compression->id;
494 if (!s->method->ssl3_enc->setup_key_block(s)) {
496 s->state = SSL_ST_ERR;
500 if (!s->method->ssl3_enc->change_cipher_state(s,
501 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
504 s->state = SSL_ST_ERR;
510 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
511 case SSL3_ST_CW_NEXT_PROTO_A:
512 case SSL3_ST_CW_NEXT_PROTO_B:
513 ret = ssl3_send_next_proto(s);
516 s->state = SSL3_ST_CW_FINISHED_A;
520 case SSL3_ST_CW_FINISHED_A:
521 case SSL3_ST_CW_FINISHED_B:
522 ret = ssl3_send_finished(s,
523 SSL3_ST_CW_FINISHED_A,
524 SSL3_ST_CW_FINISHED_B,
526 ssl3_enc->client_finished_label,
528 ssl3_enc->client_finished_label_len);
531 s->state = SSL3_ST_CW_FLUSH;
534 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
536 s->s3->tmp.next_state = SSL_ST_OK;
537 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
538 s->state = SSL_ST_OK;
539 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
540 s->s3->delay_buf_pop_ret = 0;
543 #ifndef OPENSSL_NO_TLSEXT
545 * Allow NewSessionTicket if ticket expected
547 if (s->tlsext_ticket_expected)
548 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
552 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
557 #ifndef OPENSSL_NO_TLSEXT
558 case SSL3_ST_CR_SESSION_TICKET_A:
559 case SSL3_ST_CR_SESSION_TICKET_B:
560 ret = ssl3_get_new_session_ticket(s);
563 s->state = SSL3_ST_CR_FINISHED_A;
567 case SSL3_ST_CR_CERT_STATUS_A:
568 case SSL3_ST_CR_CERT_STATUS_B:
569 ret = ssl3_get_cert_status(s);
572 s->state = SSL3_ST_CR_KEY_EXCH_A;
577 case SSL3_ST_CR_FINISHED_A:
578 case SSL3_ST_CR_FINISHED_B:
579 if (!s->s3->change_cipher_spec)
580 s->s3->flags |= SSL3_FLAGS_CCS_OK;
581 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
582 SSL3_ST_CR_FINISHED_B);
587 s->state = SSL3_ST_CW_CHANGE_A;
589 s->state = SSL_ST_OK;
593 case SSL3_ST_CW_FLUSH:
594 s->rwstate = SSL_WRITING;
595 if (BIO_flush(s->wbio) <= 0) {
599 s->rwstate = SSL_NOTHING;
600 s->state = s->s3->tmp.next_state;
604 /* clean a few things up */
605 ssl3_cleanup_key_block(s);
607 if (s->init_buf != NULL) {
608 BUF_MEM_free(s->init_buf);
613 * If we are not 'joining' the last two packets, remove the
616 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
617 ssl_free_wbio_buffer(s);
618 /* else do it later in ssl3_write */
624 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
626 s->ctx->stats.sess_hit++;
630 s->handshake_func = ssl3_connect;
631 s->ctx->stats.sess_connect_good++;
634 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
641 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
647 /* did we do anything */
648 if (!s->s3->tmp.reuse_message && !skip) {
650 if ((ret = BIO_flush(s->wbio)) <= 0)
654 if ((cb != NULL) && (s->state != state)) {
655 new_state = s->state;
657 cb(s, SSL_CB_CONNECT_LOOP, 1);
658 s->state = new_state;
668 cb(s, SSL_CB_CONNECT_EXIT, ret);
672 int ssl3_client_hello(SSL *s)
675 unsigned char *p, *d;
679 #ifndef OPENSSL_NO_COMP
684 buf = (unsigned char *)s->init_buf->data;
685 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
686 SSL_SESSION *sess = s->session;
687 if ((sess == NULL) || (sess->ssl_version != s->version) ||
688 #ifdef OPENSSL_NO_TLSEXT
689 !sess->session_id_length ||
692 * In the case of EAP-FAST, we can have a pre-shared
693 * "ticket" without a session ID.
695 (!sess->session_id_length && !sess->tlsext_tick) ||
697 (sess->not_resumable)) {
698 if (!ssl_get_new_session(s, 0))
701 if (s->method->version == DTLS_ANY_VERSION) {
702 /* Determine which DTLS version to use */
703 int options = s->options;
704 /* If DTLS 1.2 disabled correct the version number */
705 if (options & SSL_OP_NO_DTLSv1_2) {
706 if (tls1_suiteb(s)) {
707 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
708 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
712 * Disabling all versions is silly: return an error.
714 if (options & SSL_OP_NO_DTLSv1) {
715 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
719 * Update method so we don't use any DTLS 1.2 features.
721 s->method = DTLSv1_client_method();
722 s->version = DTLS1_VERSION;
725 * We only support one version: update method
727 if (options & SSL_OP_NO_DTLSv1)
728 s->method = DTLSv1_2_client_method();
729 s->version = DTLS1_2_VERSION;
731 s->client_version = s->version;
733 /* else use the pre-loaded session */
735 p = s->s3->client_random;
738 * for DTLS if client_random is initialized, reuse it, we are
739 * required to use same upon reply to HelloVerify
741 if (SSL_IS_DTLS(s)) {
744 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
753 if (i && ssl_fill_hello_random(s, 0, p,
754 sizeof(s->s3->client_random)) <= 0)
757 /* Do the message type and length last */
758 d = p = ssl_handshake_start(s);
761 * version indicates the negotiated version: for example from
762 * an SSLv2/v3 compatible client hello). The client_version
763 * field is the maximum version we permit and it is also
764 * used in RSA encrypted premaster secrets. Some servers can
765 * choke if we initially report a higher version then
766 * renegotiate to a lower one in the premaster secret. This
767 * didn't happen with TLS 1.0 as most servers supported it
768 * but it can with TLS 1.1 or later if the server only supports
771 * Possible scenario with previous logic:
772 * 1. Client hello indicates TLS 1.2
773 * 2. Server hello says TLS 1.0
774 * 3. RSA encrypted premaster secret uses 1.2.
775 * 4. Handhaked proceeds using TLS 1.0.
776 * 5. Server sends hello request to renegotiate.
777 * 6. Client hello indicates TLS v1.0 as we now
778 * know that is maximum server supports.
779 * 7. Server chokes on RSA encrypted premaster secret
780 * containing version 1.0.
782 * For interoperability it should be OK to always use the
783 * maximum version we support in client hello and then rely
784 * on the checking of version to ensure the servers isn't
785 * being inconsistent: for example initially negotiating with
786 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
787 * client_version in client hello and not resetting it to
788 * the negotiated version.
791 *(p++) = s->version >> 8;
792 *(p++) = s->version & 0xff;
793 s->client_version = s->version;
795 *(p++) = s->client_version >> 8;
796 *(p++) = s->client_version & 0xff;
800 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
801 p += SSL3_RANDOM_SIZE;
807 i = s->session->session_id_length;
810 if (i > (int)sizeof(s->session->session_id)) {
811 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
814 memcpy(p, s->session->session_id, i);
818 /* cookie stuff for DTLS */
819 if (SSL_IS_DTLS(s)) {
820 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
821 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
824 *(p++) = s->d1->cookie_len;
825 memcpy(p, s->d1->cookie, s->d1->cookie_len);
826 p += s->d1->cookie_len;
829 /* Ciphers supported */
830 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
832 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
835 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
837 * Some servers hang if client hello > 256 bytes as hack workaround
838 * chop number of supported ciphers to keep it well below this if we
841 if (TLS1_get_version(s) >= TLS1_2_VERSION
842 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
843 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
849 #ifdef OPENSSL_NO_COMP
853 if ((s->options & SSL_OP_NO_COMPRESSION)
854 || !s->ctx->comp_methods)
857 j = sk_SSL_COMP_num(s->ctx->comp_methods);
859 for (i = 0; i < j; i++) {
860 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
864 *(p++) = 0; /* Add the NULL method */
866 #ifndef OPENSSL_NO_TLSEXT
868 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
869 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
873 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
875 ssl3_send_alert(s, SSL3_AL_FATAL, al);
876 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
882 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
883 s->state = SSL3_ST_CW_CLNT_HELLO_B;
886 /* SSL3_ST_CW_CLNT_HELLO_B */
887 return ssl_do_write(s);
889 s->state = SSL_ST_ERR;
893 int ssl3_get_server_hello(SSL *s)
895 STACK_OF(SSL_CIPHER) *sk;
898 unsigned char *p, *d;
899 int i, al = SSL_AD_INTERNAL_ERROR, ok;
902 #ifndef OPENSSL_NO_COMP
906 * Hello verify request and/or server hello version may not match so set
907 * first packet if we're negotiating version.
912 n = s->method->ssl_get_message(s,
913 SSL3_ST_CR_SRVR_HELLO_A,
914 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
919 if (SSL_IS_DTLS(s)) {
921 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
922 if (s->d1->send_cookie == 0) {
923 s->s3->tmp.reuse_message = 1;
925 } else { /* already sent a cookie */
927 al = SSL_AD_UNEXPECTED_MESSAGE;
928 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
934 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
935 al = SSL_AD_UNEXPECTED_MESSAGE;
936 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
940 d = p = (unsigned char *)s->init_msg;
941 if (s->method->version == DTLS_ANY_VERSION) {
942 /* Work out correct protocol version to use */
943 int hversion = (p[0] << 8) | p[1];
944 int options = s->options;
945 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
946 s->method = DTLSv1_2_client_method();
947 else if (tls1_suiteb(s)) {
948 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
949 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
950 s->version = hversion;
951 al = SSL_AD_PROTOCOL_VERSION;
953 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
954 s->method = DTLSv1_client_method();
956 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
957 s->version = hversion;
958 al = SSL_AD_PROTOCOL_VERSION;
961 s->session->ssl_version = s->version = s->method->version;
964 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
965 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
966 s->version = (s->version & 0xff00) | p[1];
967 al = SSL_AD_PROTOCOL_VERSION;
972 /* load the server hello data */
973 /* load the server random */
974 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
975 p += SSL3_RANDOM_SIZE;
979 /* get the session-id */
982 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
983 al = SSL_AD_ILLEGAL_PARAMETER;
984 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
987 #ifndef OPENSSL_NO_TLSEXT
989 * Check if we can resume the session based on external pre-shared secret.
990 * EAP-FAST (RFC 4851) supports two types of session resumption.
991 * Resumption based on server-side state works with session IDs.
992 * Resumption based on pre-shared Protected Access Credentials (PACs)
993 * works by overriding the SessionTicket extension at the application
994 * layer, and does not send a session ID. (We do not know whether EAP-FAST
995 * servers would honour the session ID.) Therefore, the session ID alone
996 * is not a reliable indicator of session resumption, so we first check if
997 * we can resume, and later peek at the next handshake message to see if the
998 * server wants to resume.
1000 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1001 s->session->tlsext_tick) {
1002 SSL_CIPHER *pref_cipher = NULL;
1003 s->session->master_key_length = sizeof(s->session->master_key);
1004 if (s->tls_session_secret_cb(s, s->session->master_key,
1005 &s->session->master_key_length,
1007 s->tls_session_secret_cb_arg)) {
1008 s->session->cipher = pref_cipher ?
1009 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1011 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1012 al = SSL_AD_INTERNAL_ERROR;
1016 #endif /* OPENSSL_NO_TLSEXT */
1018 if (j != 0 && j == s->session->session_id_length
1019 && memcmp(p, s->session->session_id, j) == 0) {
1020 if (s->sid_ctx_length != s->session->sid_ctx_length
1021 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1022 /* actually a client application bug */
1023 al = SSL_AD_ILLEGAL_PARAMETER;
1024 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1025 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1031 * If we were trying for session-id reuse but the server
1032 * didn't echo the ID, make a new SSL_SESSION.
1033 * In the case of EAP-FAST and PAC, we do not send a session ID,
1034 * so the PAC-based session secret is always preserved. It'll be
1035 * overwritten if the server refuses resumption.
1037 if (s->session->session_id_length > 0) {
1038 if (!ssl_get_new_session(s, 0)) {
1042 s->session->session_id_length = j;
1043 memcpy(s->session->session_id, p, j); /* j could be 0 */
1046 c = ssl_get_cipher_by_char(s, p);
1048 /* unknown cipher */
1049 al = SSL_AD_ILLEGAL_PARAMETER;
1050 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1053 /* Set version disabled mask now we know version */
1054 if (!SSL_USE_TLS1_2_CIPHERS(s))
1055 ct->mask_ssl = SSL_TLSV1_2;
1059 * If it is a disabled cipher we didn't send it in client hello, so
1062 if (c->algorithm_ssl & ct->mask_ssl ||
1063 c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a) {
1064 al = SSL_AD_ILLEGAL_PARAMETER;
1065 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1068 p += ssl_put_cipher_by_char(s, NULL, NULL);
1070 sk = ssl_get_ciphers_by_id(s);
1071 i = sk_SSL_CIPHER_find(sk, c);
1073 /* we did not say we would use this cipher */
1074 al = SSL_AD_ILLEGAL_PARAMETER;
1075 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1080 * Depending on the session caching (internal/external), the cipher
1081 * and/or cipher_id values may not be set. Make sure that cipher_id is
1082 * set and use it for comparison.
1084 if (s->session->cipher)
1085 s->session->cipher_id = s->session->cipher->id;
1086 if (s->hit && (s->session->cipher_id != c->id)) {
1087 /* Workaround is now obsolete */
1089 if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1092 al = SSL_AD_ILLEGAL_PARAMETER;
1093 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1094 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1098 s->s3->tmp.new_cipher = c;
1100 * Don't digest cached records if no sigalgs: we may need them for client
1103 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1105 /* lets get the compression algorithm */
1107 #ifdef OPENSSL_NO_COMP
1109 al = SSL_AD_ILLEGAL_PARAMETER;
1110 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1111 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1115 * If compression is disabled we'd better not try to resume a session
1116 * using compression.
1118 if (s->session->compress_meth != 0) {
1119 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1124 if (s->hit && j != s->session->compress_meth) {
1125 al = SSL_AD_ILLEGAL_PARAMETER;
1126 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1127 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1132 else if (s->options & SSL_OP_NO_COMPRESSION) {
1133 al = SSL_AD_ILLEGAL_PARAMETER;
1134 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1137 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1139 if ((j != 0) && (comp == NULL)) {
1140 al = SSL_AD_ILLEGAL_PARAMETER;
1141 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1142 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1145 s->s3->tmp.new_compression = comp;
1149 #ifndef OPENSSL_NO_TLSEXT
1150 /* TLS extensions */
1151 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1152 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1158 /* wrong packet length */
1159 al = SSL_AD_DECODE_ERROR;
1160 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1166 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1168 s->state = SSL_ST_ERR;
1172 int ssl3_get_server_certificate(SSL *s)
1174 int al, i, ok, ret = -1;
1175 unsigned long n, nc, llen, l;
1177 const unsigned char *q, *p;
1179 STACK_OF(X509) *sk = NULL;
1181 EVP_PKEY *pkey = NULL;
1182 int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1185 n = s->method->ssl_get_message(s,
1188 -1, s->max_cert_list, &ok);
1193 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1194 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1195 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1196 s->s3->tmp.reuse_message = 1;
1200 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1201 al = SSL_AD_UNEXPECTED_MESSAGE;
1202 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1205 p = d = (unsigned char *)s->init_msg;
1207 if ((sk = sk_X509_new_null()) == NULL) {
1208 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1213 if (llen + 3 != n) {
1214 al = SSL_AD_DECODE_ERROR;
1215 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1218 for (nc = 0; nc < llen;) {
1219 if (nc + 3 > llen) {
1220 al = SSL_AD_DECODE_ERROR;
1221 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1222 SSL_R_CERT_LENGTH_MISMATCH);
1226 if ((l + nc + 3) > llen) {
1227 al = SSL_AD_DECODE_ERROR;
1228 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1229 SSL_R_CERT_LENGTH_MISMATCH);
1234 x = d2i_X509(NULL, &q, l);
1236 al = SSL_AD_BAD_CERTIFICATE;
1237 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1241 al = SSL_AD_DECODE_ERROR;
1242 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1243 SSL_R_CERT_LENGTH_MISMATCH);
1246 if (!sk_X509_push(sk, x)) {
1247 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1255 i = ssl_verify_cert_chain(s, sk);
1256 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1257 #ifndef OPENSSL_NO_KRB5
1258 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1259 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1260 #endif /* OPENSSL_NO_KRB5 */
1262 al = ssl_verify_alarm_type(s->verify_result);
1263 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1264 SSL_R_CERTIFICATE_VERIFY_FAILED);
1267 ERR_clear_error(); /* but we keep s->verify_result */
1269 sc = ssl_sess_cert_new();
1273 if (s->session->sess_cert)
1274 ssl_sess_cert_free(s->session->sess_cert);
1275 s->session->sess_cert = sc;
1277 sc->cert_chain = sk;
1279 * Inconsistency alert: cert_chain does include the peer's certificate,
1280 * which we don't include in s3_srvr.c
1282 x = sk_X509_value(sk, 0);
1285 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1288 pkey = X509_get_pubkey(x);
1290 /* VRS: allow null cert if auth == KRB5 */
1291 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1292 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1296 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1297 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1298 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1299 s->s3->tmp.new_cipher->name,
1300 s->s3->tmp.new_cipher->algorithm_mkey,
1301 s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1302 #endif /* KSSL_DEBUG */
1304 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1307 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1308 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1312 i = ssl_cert_type(x, pkey);
1313 if (need_cert && i < 0) {
1316 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1317 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1322 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1323 if (exp_idx >= 0 && i != exp_idx) {
1325 al = SSL_AD_ILLEGAL_PARAMETER;
1326 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1327 SSL_R_WRONG_CERTIFICATE_TYPE);
1330 sc->peer_cert_type = i;
1331 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1333 * Why would the following ever happen? We just created sc a couple
1336 if (sc->peer_pkeys[i].x509 != NULL)
1337 X509_free(sc->peer_pkeys[i].x509);
1338 sc->peer_pkeys[i].x509 = x;
1339 sc->peer_key = &(sc->peer_pkeys[i]);
1341 if (s->session->peer != NULL)
1342 X509_free(s->session->peer);
1343 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1344 s->session->peer = x;
1346 sc->peer_cert_type = i;
1347 sc->peer_key = NULL;
1349 if (s->session->peer != NULL)
1350 X509_free(s->session->peer);
1351 s->session->peer = NULL;
1353 s->session->verify_result = s->verify_result;
1359 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1361 s->state = SSL_ST_ERR;
1364 EVP_PKEY_free(pkey);
1366 sk_X509_pop_free(sk, X509_free);
1370 int ssl3_get_key_exchange(SSL *s)
1372 #ifndef OPENSSL_NO_RSA
1373 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1376 unsigned char *param, *p;
1378 long i, param_len, n, alg_k, alg_a;
1379 EVP_PKEY *pkey = NULL;
1380 const EVP_MD *md = NULL;
1381 #ifndef OPENSSL_NO_RSA
1384 #ifndef OPENSSL_NO_DH
1387 #ifndef OPENSSL_NO_ECDH
1388 EC_KEY *ecdh = NULL;
1389 BN_CTX *bn_ctx = NULL;
1390 EC_POINT *srvr_ecpoint = NULL;
1392 int encoded_pt_len = 0;
1395 EVP_MD_CTX_init(&md_ctx);
1398 * use same message size as in ssl3_get_certificate_request() as
1399 * ServerKeyExchange message may be skipped
1401 n = s->method->ssl_get_message(s,
1402 SSL3_ST_CR_KEY_EXCH_A,
1403 SSL3_ST_CR_KEY_EXCH_B,
1404 -1, s->max_cert_list, &ok);
1408 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1410 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1412 * Can't skip server key exchange if this is an ephemeral
1415 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1416 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1417 al = SSL_AD_UNEXPECTED_MESSAGE;
1420 #ifndef OPENSSL_NO_PSK
1422 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1423 * identity hint is sent. Set session->sess_cert anyway to avoid
1426 if (alg_k & SSL_kPSK) {
1427 s->session->sess_cert = ssl_sess_cert_new();
1428 if (s->ctx->psk_identity_hint)
1429 OPENSSL_free(s->ctx->psk_identity_hint);
1430 s->ctx->psk_identity_hint = NULL;
1433 s->s3->tmp.reuse_message = 1;
1437 param = p = (unsigned char *)s->init_msg;
1438 if (s->session->sess_cert != NULL) {
1439 #ifndef OPENSSL_NO_RSA
1440 if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1441 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1442 s->session->sess_cert->peer_rsa_tmp = NULL;
1445 #ifndef OPENSSL_NO_DH
1446 if (s->session->sess_cert->peer_dh_tmp) {
1447 DH_free(s->session->sess_cert->peer_dh_tmp);
1448 s->session->sess_cert->peer_dh_tmp = NULL;
1451 #ifndef OPENSSL_NO_ECDH
1452 if (s->session->sess_cert->peer_ecdh_tmp) {
1453 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1454 s->session->sess_cert->peer_ecdh_tmp = NULL;
1458 s->session->sess_cert = ssl_sess_cert_new();
1461 /* Total length of the parameters including the length prefix */
1464 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1466 al = SSL_AD_DECODE_ERROR;
1468 #ifndef OPENSSL_NO_PSK
1469 if (alg_k & SSL_kPSK) {
1471 if (param_len > n) {
1472 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1478 * Store PSK identity hint for later use, hint is used in
1479 * ssl3_send_client_key_exchange. Assume that the maximum length of
1480 * a PSK identity hint can be as long as the maximum length of a PSK
1483 if (i > PSK_MAX_IDENTITY_LEN) {
1484 al = SSL_AD_HANDSHAKE_FAILURE;
1485 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1488 if (i > n - param_len) {
1489 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1490 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1495 s->session->psk_identity_hint = BUF_strndup((char *)p, i);
1496 if (s->session->psk_identity_hint == NULL) {
1497 al = SSL_AD_HANDSHAKE_FAILURE;
1498 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1505 #endif /* !OPENSSL_NO_PSK */
1506 #ifndef OPENSSL_NO_SRP
1507 if (alg_k & SSL_kSRP) {
1509 if (param_len > n) {
1510 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1515 if (i > n - param_len) {
1516 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1521 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1522 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1527 if (2 > n - param_len) {
1528 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1535 if (i > n - param_len) {
1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1541 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1542 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1547 if (1 > n - param_len) {
1548 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1553 i = (unsigned int)(p[0]);
1556 if (i > n - param_len) {
1557 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1562 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1563 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1568 if (2 > n - param_len) {
1569 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1576 if (i > n - param_len) {
1577 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1582 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1583 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1589 if (!srp_verify_server_param(s, &al)) {
1590 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1594 /* We must check if there is a certificate */
1595 # ifndef OPENSSL_NO_RSA
1596 if (alg_a & SSL_aRSA)
1598 X509_get_pubkey(s->session->
1599 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1603 # ifndef OPENSSL_NO_DSA
1604 else if (alg_a & SSL_aDSS)
1606 X509_get_pubkey(s->session->
1607 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1611 #endif /* !OPENSSL_NO_SRP */
1612 #ifndef OPENSSL_NO_RSA
1613 if (alg_k & SSL_kRSA) {
1614 /* Temporary RSA keys only allowed in export ciphersuites */
1615 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1616 al = SSL_AD_UNEXPECTED_MESSAGE;
1617 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1620 if ((rsa = RSA_new()) == NULL) {
1621 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1626 if (param_len > n) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1632 if (i > n - param_len) {
1633 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1638 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1639 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1644 if (2 > n - param_len) {
1645 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1652 if (i > n - param_len) {
1653 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1658 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1659 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1665 /* this should be because we are using an export cipher */
1666 if (alg_a & SSL_aRSA)
1668 X509_get_pubkey(s->session->
1669 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1671 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1675 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1676 al = SSL_AD_UNEXPECTED_MESSAGE;
1677 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1681 s->session->sess_cert->peer_rsa_tmp = rsa;
1684 #else /* OPENSSL_NO_RSA */
1687 #ifndef OPENSSL_NO_DH
1688 else if (alg_k & SSL_kEDH) {
1689 if ((dh = DH_new()) == NULL) {
1690 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1695 if (param_len > n) {
1696 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1701 if (i > n - param_len) {
1702 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1707 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1708 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1713 if (BN_is_zero(dh->p)) {
1714 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1719 if (2 > n - param_len) {
1720 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1727 if (i > n - param_len) {
1728 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1733 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1734 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1739 if (BN_is_zero(dh->g)) {
1740 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1744 if (2 > n - param_len) {
1745 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1752 if (i > n - param_len) {
1753 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1758 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1759 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1765 if (BN_is_zero(dh->pub_key)) {
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1770 # ifndef OPENSSL_NO_RSA
1771 if (alg_a & SSL_aRSA)
1773 X509_get_pubkey(s->session->
1774 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1778 # ifndef OPENSSL_NO_DSA
1779 else if (alg_a & SSL_aDSS)
1781 X509_get_pubkey(s->session->
1782 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1785 /* else anonymous DH, so no certificate or pkey. */
1787 s->session->sess_cert->peer_dh_tmp = dh;
1789 } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1790 al = SSL_AD_ILLEGAL_PARAMETER;
1791 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1792 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1795 #endif /* !OPENSSL_NO_DH */
1797 #ifndef OPENSSL_NO_ECDH
1798 else if (alg_k & SSL_kEECDH) {
1800 const EC_GROUP *group;
1802 if ((ecdh = EC_KEY_new()) == NULL) {
1803 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1808 * Extract elliptic curve parameters and the server's ephemeral ECDH
1809 * public key. Keep accumulating lengths of various components in
1810 * param_len and make sure it never exceeds n.
1814 * XXX: For now we only support named (not generic) curves and the
1815 * ECParameters in this case is just three bytes. We also need one
1816 * byte for the length of the encoded point
1819 if (param_len > n) {
1820 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1824 * Check curve is one of our preferences, if not server has sent an
1825 * invalid curve. ECParameters is 3 bytes.
1827 if (!tls1_check_curve(s, p, 3)) {
1828 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1832 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1833 al = SSL_AD_INTERNAL_ERROR;
1834 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1835 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1839 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1840 if (ngroup == NULL) {
1841 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1844 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1845 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1848 EC_GROUP_free(ngroup);
1850 group = EC_KEY_get0_group(ecdh);
1852 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1853 (EC_GROUP_get_degree(group) > 163)) {
1854 al = SSL_AD_EXPORT_RESTRICTION;
1855 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1856 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1862 /* Next, get the encoded ECPoint */
1863 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1864 ((bn_ctx = BN_CTX_new()) == NULL)) {
1865 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1869 encoded_pt_len = *p; /* length of encoded point */
1872 if ((encoded_pt_len > n - param_len) ||
1873 (EC_POINT_oct2point(group, srvr_ecpoint,
1874 p, encoded_pt_len, bn_ctx) == 0)) {
1875 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1878 param_len += encoded_pt_len;
1881 p += encoded_pt_len;
1884 * The ECC/TLS specification does not mention the use of DSA to sign
1885 * ECParameters in the server key exchange message. We do support RSA
1889 # ifndef OPENSSL_NO_RSA
1890 else if (alg_a & SSL_aRSA)
1892 X509_get_pubkey(s->session->
1893 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1895 # ifndef OPENSSL_NO_ECDSA
1896 else if (alg_a & SSL_aECDSA)
1898 X509_get_pubkey(s->session->
1899 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1901 /* else anonymous ECDH, so no certificate or pkey. */
1902 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1903 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1905 BN_CTX_free(bn_ctx);
1907 EC_POINT_free(srvr_ecpoint);
1908 srvr_ecpoint = NULL;
1910 al = SSL_AD_UNEXPECTED_MESSAGE;
1911 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1914 #endif /* !OPENSSL_NO_ECDH */
1916 /* p points to the next byte, there are 'n' bytes left */
1918 /* if it was signed, check the signature */
1920 if (SSL_USE_SIGALGS(s)) {
1923 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1926 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1933 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1941 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1946 j = EVP_PKEY_size(pkey);
1949 * Check signature length. If n is 0 then signature is empty
1951 if ((i != n) || (n > j) || (n <= 0)) {
1952 /* wrong packet length */
1953 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1956 #ifndef OPENSSL_NO_RSA
1957 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1963 for (num = 2; num > 0; num--) {
1964 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1965 if (EVP_DigestInit_ex(&md_ctx,
1966 (num == 2) ? s->ctx->md5 : s->ctx->sha1,
1968 || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1969 SSL3_RANDOM_SIZE) <= 0
1970 || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1971 SSL3_RANDOM_SIZE) <= 0
1972 || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0
1973 || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) {
1974 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1975 ERR_R_INTERNAL_ERROR);
1976 al = SSL_AD_INTERNAL_ERROR;
1982 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1984 al = SSL_AD_DECRYPT_ERROR;
1985 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1990 al = SSL_AD_DECRYPT_ERROR;
1991 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1997 if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
1998 || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1999 SSL3_RANDOM_SIZE) <= 0
2000 || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
2001 SSL3_RANDOM_SIZE) <= 0
2002 || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) {
2003 al = SSL_AD_INTERNAL_ERROR;
2004 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB);
2007 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
2009 al = SSL_AD_DECRYPT_ERROR;
2010 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2015 /* aNULL, aSRP or kPSK do not need public keys */
2016 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
2017 /* Might be wrong key type, check it */
2018 if (ssl3_check_cert_and_algorithm(s))
2019 /* Otherwise this shouldn't happen */
2020 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2023 /* still data left over */
2025 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2029 EVP_PKEY_free(pkey);
2030 EVP_MD_CTX_cleanup(&md_ctx);
2033 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2035 EVP_PKEY_free(pkey);
2036 #ifndef OPENSSL_NO_RSA
2040 #ifndef OPENSSL_NO_DH
2044 #ifndef OPENSSL_NO_ECDH
2045 BN_CTX_free(bn_ctx);
2046 EC_POINT_free(srvr_ecpoint);
2050 EVP_MD_CTX_cleanup(&md_ctx);
2051 s->state = SSL_ST_ERR;
2055 int ssl3_get_certificate_request(SSL *s)
2058 unsigned long n, nc, l;
2059 unsigned int llen, ctype_num, i;
2060 X509_NAME *xn = NULL;
2061 const unsigned char *p, *q;
2063 STACK_OF(X509_NAME) *ca_sk = NULL;
2065 n = s->method->ssl_get_message(s,
2066 SSL3_ST_CR_CERT_REQ_A,
2067 SSL3_ST_CR_CERT_REQ_B,
2068 -1, s->max_cert_list, &ok);
2073 s->s3->tmp.cert_req = 0;
2075 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2076 s->s3->tmp.reuse_message = 1;
2078 * If we get here we don't need any cached handshake records as we
2079 * wont be doing client auth.
2081 if (s->s3->handshake_buffer) {
2082 if (!ssl3_digest_cached_records(s))
2088 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2089 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2090 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2094 /* TLS does not like anon-DH with client cert */
2095 if (s->version > SSL3_VERSION) {
2096 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2097 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2098 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2099 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2104 p = d = (unsigned char *)s->init_msg;
2106 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2107 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2111 /* get the certificate types */
2113 if (s->cert->ctypes) {
2114 OPENSSL_free(s->cert->ctypes);
2115 s->cert->ctypes = NULL;
2117 if (ctype_num > SSL3_CT_NUMBER) {
2118 /* If we exceed static buffer copy all to cert structure */
2119 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2120 if (s->cert->ctypes == NULL) {
2121 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2124 memcpy(s->cert->ctypes, p, ctype_num);
2125 s->cert->ctype_num = (size_t)ctype_num;
2126 ctype_num = SSL3_CT_NUMBER;
2128 for (i = 0; i < ctype_num; i++)
2129 s->s3->tmp.ctype[i] = p[i];
2131 if (SSL_USE_SIGALGS(s)) {
2134 * Check we have enough room for signature algorithms and following
2137 if ((unsigned long)(p - d + llen + 2) > n) {
2138 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2139 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2140 SSL_R_DATA_LENGTH_TOO_LONG);
2143 /* Clear certificate digests and validity flags */
2144 for (i = 0; i < SSL_PKEY_NUM; i++) {
2145 s->cert->pkeys[i].digest = NULL;
2146 s->cert->pkeys[i].valid_flags = 0;
2148 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2149 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2150 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2151 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2154 if (!tls1_process_sigalgs(s)) {
2155 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2156 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2162 /* get the CA RDNs */
2167 out = fopen("/tmp/vsign.der", "w");
2168 fwrite(p, 1, llen, out);
2173 if ((unsigned long)(p - d + llen) != n) {
2174 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2175 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2179 for (nc = 0; nc < llen;) {
2180 if (nc + 2 > llen) {
2181 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2182 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2186 if ((l + nc + 2) > llen) {
2187 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2188 goto cont; /* netscape bugs */
2189 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2190 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2196 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2197 /* If netscape tolerance is on, ignore errors */
2198 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2201 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2202 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2208 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2209 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2210 SSL_R_CA_DN_LENGTH_MISMATCH);
2213 if (!sk_X509_NAME_push(ca_sk, xn)) {
2214 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2228 /* we should setup a certificate to return.... */
2229 s->s3->tmp.cert_req = 1;
2230 s->s3->tmp.ctype_num = ctype_num;
2231 if (s->s3->tmp.ca_names != NULL)
2232 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2233 s->s3->tmp.ca_names = ca_sk;
2239 s->state = SSL_ST_ERR;
2243 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2247 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2249 return (X509_NAME_cmp(*a, *b));
2252 #ifndef OPENSSL_NO_TLSEXT
2253 int ssl3_get_new_session_ticket(SSL *s)
2255 int ok, al, ret = 0, ticklen;
2257 const unsigned char *p;
2259 unsigned long ticket_lifetime_hint;
2261 n = s->method->ssl_get_message(s,
2262 SSL3_ST_CR_SESSION_TICKET_A,
2263 SSL3_ST_CR_SESSION_TICKET_B,
2264 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2270 /* need at least ticket_lifetime_hint + ticket length */
2271 al = SSL_AD_DECODE_ERROR;
2272 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2276 p = d = (unsigned char *)s->init_msg;
2278 n2l(p, ticket_lifetime_hint);
2280 /* ticket_lifetime_hint + ticket_length + ticket */
2281 if (ticklen + 6 != n) {
2282 al = SSL_AD_DECODE_ERROR;
2283 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2287 /* Server is allowed to change its mind and send an empty ticket. */
2291 if (s->session->session_id_length > 0) {
2292 int i = s->session_ctx->session_cache_mode;
2293 SSL_SESSION *new_sess;
2295 * We reused an existing session, so we need to replace it with a new
2298 if (i & SSL_SESS_CACHE_CLIENT) {
2300 * Remove the old session from the cache
2302 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2303 if (s->session_ctx->remove_session_cb != NULL)
2304 s->session_ctx->remove_session_cb(s->session_ctx,
2307 /* We carry on if this fails */
2308 SSL_CTX_remove_session(s->session_ctx, s->session);
2312 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2313 al = SSL_AD_INTERNAL_ERROR;
2314 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2318 SSL_SESSION_free(s->session);
2319 s->session = new_sess;
2322 if (s->session->tlsext_tick) {
2323 OPENSSL_free(s->session->tlsext_tick);
2324 s->session->tlsext_ticklen = 0;
2326 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2327 if (!s->session->tlsext_tick) {
2328 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2331 memcpy(s->session->tlsext_tick, p, ticklen);
2332 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2333 s->session->tlsext_ticklen = ticklen;
2335 * There are two ways to detect a resumed ticket session. One is to set
2336 * an appropriate session ID and then the server must return a match in
2337 * ServerHello. This allows the normal client session ID matching to work
2338 * and we know much earlier that the ticket has been accepted. The
2339 * other way is to set zero length session ID when the ticket is
2340 * presented and rely on the handshake to determine session resumption.
2341 * We choose the former approach because this fits in with assumptions
2342 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2343 * SHA256 is disabled) hash of the ticket.
2345 EVP_Digest(p, ticklen,
2346 s->session->session_id, &s->session->session_id_length,
2347 # ifndef OPENSSL_NO_SHA256
2348 EVP_sha256(), NULL);
2355 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2357 s->state = SSL_ST_ERR;
2361 int ssl3_get_cert_status(SSL *s)
2364 unsigned long resplen, n;
2365 const unsigned char *p;
2367 n = s->method->ssl_get_message(s,
2368 SSL3_ST_CR_CERT_STATUS_A,
2369 SSL3_ST_CR_CERT_STATUS_B,
2375 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
2377 * The CertificateStatus message is optional even if
2378 * tlsext_status_expected is set
2380 s->s3->tmp.reuse_message = 1;
2383 /* need at least status type + length */
2384 al = SSL_AD_DECODE_ERROR;
2385 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2388 p = (unsigned char *)s->init_msg;
2389 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2390 al = SSL_AD_DECODE_ERROR;
2391 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2395 if (resplen + 4 != n) {
2396 al = SSL_AD_DECODE_ERROR;
2397 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2400 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2401 if (s->tlsext_ocsp_resp == NULL) {
2402 al = SSL_AD_INTERNAL_ERROR;
2403 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2406 s->tlsext_ocsp_resplen = resplen;
2408 if (s->ctx->tlsext_status_cb) {
2410 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2412 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2413 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2417 al = SSL_AD_INTERNAL_ERROR;
2418 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2424 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2425 s->state = SSL_ST_ERR;
2430 int ssl3_get_server_done(SSL *s)
2435 /* Second to last param should be very small, like 0 :-) */
2436 n = s->method->ssl_get_message(s,
2437 SSL3_ST_CR_SRVR_DONE_A,
2438 SSL3_ST_CR_SRVR_DONE_B,
2439 SSL3_MT_SERVER_DONE, 30, &ok);
2444 /* should contain no data */
2445 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2446 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2447 s->state = SSL_ST_ERR;
2454 #ifndef OPENSSL_NO_DH
2455 static DH *get_server_static_dh_key(SESS_CERT *scert)
2458 EVP_PKEY *spkey = NULL;
2459 int idx = scert->peer_cert_type;
2462 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2464 dh_srvr = EVP_PKEY_get1_DH(spkey);
2465 EVP_PKEY_free(spkey);
2467 if (dh_srvr == NULL)
2468 SSLerr(SSL_F_GET_SERVER_STATIC_DH_KEY, ERR_R_INTERNAL_ERROR);
2473 int ssl3_send_client_key_exchange(SSL *s)
2477 unsigned long alg_k;
2478 #ifndef OPENSSL_NO_RSA
2480 EVP_PKEY *pkey = NULL;
2482 #ifndef OPENSSL_NO_KRB5
2484 #endif /* OPENSSL_NO_KRB5 */
2485 #ifndef OPENSSL_NO_ECDH
2486 EC_KEY *clnt_ecdh = NULL;
2487 const EC_POINT *srvr_ecpoint = NULL;
2488 EVP_PKEY *srvr_pub_pkey = NULL;
2489 unsigned char *encodedPoint = NULL;
2490 int encoded_pt_len = 0;
2491 BN_CTX *bn_ctx = NULL;
2494 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2495 p = ssl_handshake_start(s);
2497 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2499 /* Fool emacs indentation */
2502 #ifndef OPENSSL_NO_RSA
2503 else if (alg_k & SSL_kRSA) {
2505 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2507 if (s->session->sess_cert == NULL) {
2509 * We should always have a server certificate with SSL_kRSA.
2511 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2512 ERR_R_INTERNAL_ERROR);
2516 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2517 rsa = s->session->sess_cert->peer_rsa_tmp;
2520 X509_get_pubkey(s->session->
2521 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2523 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2524 || (pkey->pkey.rsa == NULL)) {
2525 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2526 ERR_R_INTERNAL_ERROR);
2527 EVP_PKEY_free(pkey);
2530 rsa = pkey->pkey.rsa;
2531 EVP_PKEY_free(pkey);
2534 tmp_buf[0] = s->client_version >> 8;
2535 tmp_buf[1] = s->client_version & 0xff;
2536 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2539 s->session->master_key_length = sizeof tmp_buf;
2542 /* Fix buf for TLS and beyond */
2543 if (s->version > SSL3_VERSION)
2545 n = RSA_public_encrypt(sizeof tmp_buf,
2546 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
2548 if (s->options & SSL_OP_PKCS1_CHECK_1)
2550 if (s->options & SSL_OP_PKCS1_CHECK_2)
2554 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2555 SSL_R_BAD_RSA_ENCRYPT);
2559 /* Fix buf for TLS and beyond */
2560 if (s->version > SSL3_VERSION) {
2565 s->session->master_key_length =
2566 s->method->ssl3_enc->generate_master_secret(s,
2568 session->master_key,
2571 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2574 #ifndef OPENSSL_NO_KRB5
2575 else if (alg_k & SSL_kKRB5) {
2576 krb5_error_code krb5rc;
2577 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2578 /* krb5_data krb5_ap_req; */
2579 krb5_data *enc_ticket;
2580 krb5_data authenticator, *authp = NULL;
2581 EVP_CIPHER_CTX ciph_ctx;
2582 const EVP_CIPHER *enc = NULL;
2583 unsigned char iv[EVP_MAX_IV_LENGTH];
2584 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2585 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2586 int padl, outl = sizeof(epms);
2588 EVP_CIPHER_CTX_init(&ciph_ctx);
2591 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2593 # endif /* KSSL_DEBUG */
2596 # ifdef KRB5SENDAUTH
2598 authp = &authenticator;
2599 # endif /* KRB5SENDAUTH */
2601 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2602 enc = kssl_map_enc(kssl_ctx->enctype);
2607 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2608 if (krb5rc && kssl_err.text)
2609 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2612 # endif /* KSSL_DEBUG */
2615 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2616 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2621 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2622 * in place of RFC 2712 KerberosWrapper, as in:
2624 * Send ticket (copy to *p, set n = length)
2625 * n = krb5_ap_req.length;
2626 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2627 * if (krb5_ap_req.data)
2628 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2630 * Now using real RFC 2712 KerberosWrapper
2631 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2632 * Note: 2712 "opaque" types are here replaced
2633 * with a 2-byte length followed by the value.
2635 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2636 * Where "xx xx" = length bytes. Shown here with
2637 * optional authenticator omitted.
2640 /* KerberosWrapper.Ticket */
2641 s2n(enc_ticket->length, p);
2642 memcpy(p, enc_ticket->data, enc_ticket->length);
2643 p += enc_ticket->length;
2644 n = enc_ticket->length + 2;
2646 /* KerberosWrapper.Authenticator */
2647 if (authp && authp->length) {
2648 s2n(authp->length, p);
2649 memcpy(p, authp->data, authp->length);
2651 n += authp->length + 2;
2657 s2n(0, p); /* null authenticator length */
2661 tmp_buf[0] = s->client_version >> 8;
2662 tmp_buf[1] = s->client_version & 0xff;
2663 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2667 * 20010420 VRS. Tried it this way; failed.
2668 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2669 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2670 * kssl_ctx->length);
2671 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2674 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2675 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2676 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
2678 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2680 if (outl > (int)sizeof epms) {
2681 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2682 ERR_R_INTERNAL_ERROR);
2685 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2687 /* KerberosWrapper.EncryptedPreMasterSecret */
2689 memcpy(p, epms, outl);
2693 s->session->master_key_length =
2694 s->method->ssl3_enc->generate_master_secret(s,
2696 session->master_key,
2700 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2701 OPENSSL_cleanse(epms, outl);
2704 #ifndef OPENSSL_NO_DH
2705 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2706 DH *dh_srvr, *dh_clnt;
2707 SESS_CERT *scert = s->session->sess_cert;
2709 if (scert == NULL) {
2710 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2711 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2712 SSL_R_UNEXPECTED_MESSAGE);
2716 if (scert->peer_dh_tmp != NULL) {
2717 dh_srvr = scert->peer_dh_tmp;
2719 dh_srvr = get_server_static_dh_key(scert);
2720 if (dh_srvr == NULL)
2724 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2725 /* Use client certificate key */
2726 EVP_PKEY *clkey = s->cert->key->privatekey;
2729 dh_clnt = EVP_PKEY_get1_DH(clkey);
2730 if (dh_clnt == NULL) {
2731 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2732 ERR_R_INTERNAL_ERROR);
2736 /* generate a new random key */
2737 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2738 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2741 if (!DH_generate_key(dh_clnt)) {
2742 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2749 * use the 'p' output buffer for the DH key, but make sure to
2750 * clear it out afterwards
2753 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2754 if (scert->peer_dh_tmp == NULL)
2758 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2763 /* generate master key from the result */
2764 s->session->master_key_length =
2765 s->method->ssl3_enc->generate_master_secret(s,
2767 session->master_key,
2772 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2775 /* send off the data */
2776 n = BN_num_bytes(dh_clnt->pub_key);
2778 BN_bn2bin(dh_clnt->pub_key, p);
2786 #ifndef OPENSSL_NO_ECDH
2787 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2788 const EC_GROUP *srvr_group = NULL;
2790 int ecdh_clnt_cert = 0;
2793 if (s->session->sess_cert == NULL) {
2794 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2795 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2796 SSL_R_UNEXPECTED_MESSAGE);
2801 * Did we send out the client's ECDH share for use in premaster
2802 * computation as part of client certificate? If so, set
2803 * ecdh_clnt_cert to 1.
2805 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2807 * XXX: For now, we do not support client
2808 * authentication using ECDH certificates.
2809 * To add such support, one needs to add
2810 * code that checks for appropriate
2811 * conditions and sets ecdh_clnt_cert to 1.
2812 * For example, the cert have an ECC
2813 * key on the same curve as the server's
2814 * and the key should be authorized for
2817 * One also needs to add code in ssl3_connect
2818 * to skip sending the certificate verify
2821 * if ((s->cert->key->privatekey != NULL) &&
2822 * (s->cert->key->privatekey->type ==
2823 * EVP_PKEY_EC) && ...)
2824 * ecdh_clnt_cert = 1;
2828 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2829 tkey = s->session->sess_cert->peer_ecdh_tmp;
2831 /* Get the Server Public Key from Cert */
2833 X509_get_pubkey(s->session->
2834 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2835 if ((srvr_pub_pkey == NULL)
2836 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2837 || (srvr_pub_pkey->pkey.ec == NULL)) {
2838 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2839 ERR_R_INTERNAL_ERROR);
2843 tkey = srvr_pub_pkey->pkey.ec;
2846 srvr_group = EC_KEY_get0_group(tkey);
2847 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2849 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2850 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2851 ERR_R_INTERNAL_ERROR);
2855 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2856 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2857 ERR_R_MALLOC_FAILURE);
2861 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2862 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2865 if (ecdh_clnt_cert) {
2867 * Reuse key info from our certificate We only need our
2868 * private key to perform the ECDH computation.
2870 const BIGNUM *priv_key;
2871 tkey = s->cert->key->privatekey->pkey.ec;
2872 priv_key = EC_KEY_get0_private_key(tkey);
2873 if (priv_key == NULL) {
2874 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2875 ERR_R_MALLOC_FAILURE);
2878 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2879 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2883 /* Generate a new ECDH key pair */
2884 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2885 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2892 * use the 'p' output buffer for the ECDH key, but make sure to
2893 * clear it out afterwards
2896 field_size = EC_GROUP_get_degree(srvr_group);
2897 if (field_size <= 0) {
2898 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2901 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
2904 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2908 /* generate master key from the result */
2909 s->session->master_key_length =
2910 s->method->ssl3_enc->generate_master_secret(s,
2912 session->master_key,
2915 memset(p, 0, n); /* clean up */
2917 if (ecdh_clnt_cert) {
2918 /* Send empty client key exch message */
2922 * First check the size of encoding and allocate memory
2926 EC_POINT_point2oct(srvr_group,
2927 EC_KEY_get0_public_key(clnt_ecdh),
2928 POINT_CONVERSION_UNCOMPRESSED,
2931 encodedPoint = (unsigned char *)
2932 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2933 bn_ctx = BN_CTX_new();
2934 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2935 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2936 ERR_R_MALLOC_FAILURE);
2940 /* Encode the public key */
2941 n = EC_POINT_point2oct(srvr_group,
2942 EC_KEY_get0_public_key(clnt_ecdh),
2943 POINT_CONVERSION_UNCOMPRESSED,
2944 encodedPoint, encoded_pt_len, bn_ctx);
2946 *p = n; /* length of encoded point */
2947 /* Encoded point will be copied here */
2949 /* copy the point */
2950 memcpy((unsigned char *)p, encodedPoint, n);
2951 /* increment n to account for length field */
2955 /* Free allocated memory */
2956 BN_CTX_free(bn_ctx);
2957 if (encodedPoint != NULL)
2958 OPENSSL_free(encodedPoint);
2959 if (clnt_ecdh != NULL)
2960 EC_KEY_free(clnt_ecdh);
2961 EVP_PKEY_free(srvr_pub_pkey);
2963 #endif /* !OPENSSL_NO_ECDH */
2964 else if (alg_k & SSL_kGOST) {
2965 /* GOST key exchange message creation */
2966 EVP_PKEY_CTX *pkey_ctx;
2969 unsigned int md_len;
2971 unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
2972 EVP_MD_CTX *ukm_hash;
2976 * Get server sertificate PKEY and create ctx from it
2980 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2984 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2986 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2987 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2991 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2992 X509_get_pubkey(peer_cert), NULL);
2993 if (pkey_ctx == NULL) {
2994 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2995 ERR_R_MALLOC_FAILURE);
2999 * If we have send a certificate, and certificate key
3001 * * parameters match those of server certificate, use
3002 * certificate key for key exchange
3005 /* Otherwise, generate ephemeral key pair */
3007 if (pkey_ctx == NULL
3008 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3009 /* Generate session key */
3010 || RAND_bytes(premaster_secret, 32) <= 0) {
3011 EVP_PKEY_CTX_free(pkey_ctx);
3012 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3013 ERR_R_INTERNAL_ERROR);
3017 * Compute shared IV and store it in algorithm-specific context
3020 ukm_hash = EVP_MD_CTX_create();
3021 if (EVP_DigestInit(ukm_hash,
3022 EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0
3023 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
3024 SSL3_RANDOM_SIZE) <= 0
3025 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
3026 SSL3_RANDOM_SIZE) <= 0
3027 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3028 EVP_MD_CTX_destroy(ukm_hash);
3029 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3030 ERR_R_INTERNAL_ERROR);
3033 EVP_MD_CTX_destroy(ukm_hash);
3034 if (EVP_PKEY_CTX_ctrl
3035 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
3037 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3041 /* Make GOST keytransport blob message */
3043 * Encapsulate it into sequence
3045 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
3047 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
3049 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3053 if (msglen >= 0x80) {
3055 *(p++) = msglen & 0xff;
3058 *(p++) = msglen & 0xff;
3061 memcpy(p, tmp, msglen);
3062 EVP_PKEY_CTX_free(pkey_ctx);
3063 s->session->master_key_length =
3064 s->method->ssl3_enc->generate_master_secret(s,
3066 session->master_key,
3069 EVP_PKEY_free(pub_key);
3072 #ifndef OPENSSL_NO_SRP
3073 else if (alg_k & SSL_kSRP) {
3074 if (s->srp_ctx.A != NULL) {
3075 /* send off the data */
3076 n = BN_num_bytes(s->srp_ctx.A);
3078 BN_bn2bin(s->srp_ctx.A, p);
3081 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3082 ERR_R_INTERNAL_ERROR);
3085 if (s->session->srp_username != NULL)
3086 OPENSSL_free(s->session->srp_username);
3087 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
3088 if (s->session->srp_username == NULL) {
3089 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3090 ERR_R_MALLOC_FAILURE);
3094 if ((s->session->master_key_length =
3095 SRP_generate_client_master_secret(s,
3096 s->session->master_key)) <
3098 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3099 ERR_R_INTERNAL_ERROR);
3104 #ifndef OPENSSL_NO_PSK
3105 else if (alg_k & SSL_kPSK) {
3107 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
3108 * \0-terminated identity. The last byte is for us for simulating
3111 char identity[PSK_MAX_IDENTITY_LEN + 2];
3112 size_t identity_len;
3113 unsigned char *t = NULL;
3114 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
3115 unsigned int pre_ms_len = 0, psk_len = 0;
3119 if (s->psk_client_callback == NULL) {
3120 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3121 SSL_R_PSK_NO_CLIENT_CB);
3125 memset(identity, 0, sizeof(identity));
3126 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
3127 identity, sizeof(identity) - 1,
3129 sizeof(psk_or_pre_ms));
3130 if (psk_len > PSK_MAX_PSK_LEN) {
3131 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3132 ERR_R_INTERNAL_ERROR);
3134 } else if (psk_len == 0) {
3135 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3136 SSL_R_PSK_IDENTITY_NOT_FOUND);
3139 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
3140 identity_len = strlen(identity);
3141 if (identity_len > PSK_MAX_IDENTITY_LEN) {
3142 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3143 ERR_R_INTERNAL_ERROR);
3146 /* create PSK pre_master_secret */
3147 pre_ms_len = 2 + psk_len + 2 + psk_len;
3149 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
3151 memset(t, 0, psk_len);
3155 if (s->session->psk_identity_hint != NULL)
3156 OPENSSL_free(s->session->psk_identity_hint);
3157 s->session->psk_identity_hint =
3158 BUF_strdup(s->ctx->psk_identity_hint);
3159 if (s->ctx->psk_identity_hint != NULL
3160 && s->session->psk_identity_hint == NULL) {
3161 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3162 ERR_R_MALLOC_FAILURE);
3166 if (s->session->psk_identity != NULL)
3167 OPENSSL_free(s->session->psk_identity);
3168 s->session->psk_identity = BUF_strdup(identity);
3169 if (s->session->psk_identity == NULL) {
3170 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3171 ERR_R_MALLOC_FAILURE);
3175 s->session->master_key_length =
3176 s->method->ssl3_enc->generate_master_secret(s,
3178 session->master_key,
3181 s2n(identity_len, p);
3182 memcpy(p, identity, identity_len);
3183 n = 2 + identity_len;
3186 OPENSSL_cleanse(identity, sizeof(identity));
3187 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
3189 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3195 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3196 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3200 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
3201 s->state = SSL3_ST_CW_KEY_EXCH_B;
3204 /* SSL3_ST_CW_KEY_EXCH_B */
3205 return ssl_do_write(s);
3207 #ifndef OPENSSL_NO_ECDH
3208 BN_CTX_free(bn_ctx);
3209 if (encodedPoint != NULL)
3210 OPENSSL_free(encodedPoint);
3211 if (clnt_ecdh != NULL)
3212 EC_KEY_free(clnt_ecdh);
3213 EVP_PKEY_free(srvr_pub_pkey);
3215 s->state = SSL_ST_ERR;
3219 int ssl3_send_client_verify(SSL *s)
3222 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3224 EVP_PKEY_CTX *pctx = NULL;
3230 EVP_MD_CTX_init(&mctx);
3232 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3233 p = ssl_handshake_start(s);
3234 pkey = s->cert->key->privatekey;
3235 /* Create context from key and test if sha1 is allowed as digest */
3236 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3237 if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) {
3238 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3241 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3242 if (!SSL_USE_SIGALGS(s))
3243 s->method->ssl3_enc->cert_verify_mac(s,
3246 [MD5_DIGEST_LENGTH]));
3251 * For TLS v1.2 send signature algorithm and signature using agreed
3252 * digest and cached handshake records.
3254 if (SSL_USE_SIGALGS(s)) {
3257 const EVP_MD *md = s->cert->key->digest;
3258 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3259 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3260 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3265 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3268 if (!EVP_SignInit_ex(&mctx, md, NULL)
3269 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3270 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3271 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3276 if (!ssl3_digest_cached_records(s))
3279 #ifndef OPENSSL_NO_RSA
3280 if (pkey->type == EVP_PKEY_RSA) {
3281 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3282 if (RSA_sign(NID_md5_sha1, data,
3283 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3284 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3285 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3292 #ifndef OPENSSL_NO_DSA
3293 if (pkey->type == EVP_PKEY_DSA) {
3294 if (!DSA_sign(pkey->save_type,
3295 &(data[MD5_DIGEST_LENGTH]),
3296 SHA_DIGEST_LENGTH, &(p[2]),
3297 (unsigned int *)&j, pkey->pkey.dsa)) {
3298 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3305 #ifndef OPENSSL_NO_ECDSA
3306 if (pkey->type == EVP_PKEY_EC) {
3307 if (!ECDSA_sign(pkey->save_type,
3308 &(data[MD5_DIGEST_LENGTH]),
3309 SHA_DIGEST_LENGTH, &(p[2]),
3310 (unsigned int *)&j, pkey->pkey.ec)) {
3311 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3318 if (pkey->type == NID_id_GostR3410_94
3319 || pkey->type == NID_id_GostR3410_2001) {
3320 unsigned char signbuf[64];
3322 size_t sigsize = 64;
3323 s->method->ssl3_enc->cert_verify_mac(s,
3324 NID_id_GostR3411_94, data);
3325 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3326 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3329 for (i = 63, j = 0; i >= 0; j++, i--) {
3330 p[2 + j] = signbuf[i];
3335 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3338 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
3339 s->state = SSL3_ST_CW_CERT_VRFY_B;
3341 EVP_MD_CTX_cleanup(&mctx);
3342 EVP_PKEY_CTX_free(pctx);
3343 return ssl_do_write(s);
3345 EVP_MD_CTX_cleanup(&mctx);
3346 EVP_PKEY_CTX_free(pctx);
3347 s->state = SSL_ST_ERR;
3352 * Check a certificate can be used for client authentication. Currently check
3353 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3354 * certificates can be used and optionally checks suitability for Suite B.
3356 static int ssl3_check_client_certificate(SSL *s)
3358 unsigned long alg_k;
3359 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3361 /* If no suitable signature algorithm can't use certificate */
3362 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3365 * If strict mode check suitability of chain before using it. This also
3366 * adjusts suite B digest if necessary.
3368 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3369 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3371 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3372 /* See if we can use client certificate for fixed DH */
3373 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3374 SESS_CERT *scert = s->session->sess_cert;
3375 int i = scert->peer_cert_type;
3376 EVP_PKEY *clkey = NULL, *spkey = NULL;
3377 clkey = s->cert->key->privatekey;
3378 /* If client key not DH assume it can be used */
3379 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3382 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3384 /* Compare server and client parameters */
3385 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3386 EVP_PKEY_free(spkey);
3390 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3395 int ssl3_send_client_certificate(SSL *s)
3398 EVP_PKEY *pkey = NULL;
3401 if (s->state == SSL3_ST_CW_CERT_A) {
3402 /* Let cert callback update client certificates if required */
3403 if (s->cert->cert_cb) {
3404 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3406 s->rwstate = SSL_X509_LOOKUP;
3410 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3411 s->state = SSL_ST_ERR;
3414 s->rwstate = SSL_NOTHING;
3416 if (ssl3_check_client_certificate(s))
3417 s->state = SSL3_ST_CW_CERT_C;
3419 s->state = SSL3_ST_CW_CERT_B;
3422 /* We need to get a client cert */
3423 if (s->state == SSL3_ST_CW_CERT_B) {
3425 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3426 * return(-1); We then get retied later
3428 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3430 s->rwstate = SSL_X509_LOOKUP;
3433 s->rwstate = SSL_NOTHING;
3434 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3435 s->state = SSL3_ST_CW_CERT_B;
3436 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3438 } else if (i == 1) {
3440 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3441 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3447 EVP_PKEY_free(pkey);
3448 if (i && !ssl3_check_client_certificate(s))
3451 if (s->version == SSL3_VERSION) {
3452 s->s3->tmp.cert_req = 0;
3453 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3456 s->s3->tmp.cert_req = 2;
3460 /* Ok, we have a cert */
3461 s->state = SSL3_ST_CW_CERT_C;
3464 if (s->state == SSL3_ST_CW_CERT_C) {
3465 s->state = SSL3_ST_CW_CERT_D;
3466 if (!ssl3_output_cert_chain(s,
3467 (s->s3->tmp.cert_req ==
3468 2) ? NULL : s->cert->key)) {
3469 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3470 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3471 s->state = SSL_ST_ERR;
3475 /* SSL3_ST_CW_CERT_D */
3476 return ssl_do_write(s);
3479 #define has_bits(i,m) (((i)&(m)) == (m))
3481 int ssl3_check_cert_and_algorithm(SSL *s)
3485 EVP_PKEY *pkey = NULL;
3488 #ifndef OPENSSL_NO_RSA
3491 #ifndef OPENSSL_NO_DH
3494 int al = SSL_AD_HANDSHAKE_FAILURE;
3496 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3497 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3499 /* we don't have a certificate */
3500 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3503 sc = s->session->sess_cert;
3505 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3508 #ifndef OPENSSL_NO_RSA
3509 rsa = s->session->sess_cert->peer_rsa_tmp;
3511 #ifndef OPENSSL_NO_DH
3512 dh = s->session->sess_cert->peer_dh_tmp;
3515 /* This is the passed certificate */
3517 idx = sc->peer_cert_type;
3518 #ifndef OPENSSL_NO_ECDH
3519 if (idx == SSL_PKEY_ECC) {
3520 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3522 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3527 } else if (alg_a & SSL_aECDSA) {
3528 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3529 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3531 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3532 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3536 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3537 pkey_bits = EVP_PKEY_bits(pkey);
3538 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3539 EVP_PKEY_free(pkey);
3541 /* Check that we have a certificate if we require one */
3542 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3543 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3544 SSL_R_MISSING_RSA_SIGNING_CERT);
3547 #ifndef OPENSSL_NO_DSA
3548 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3549 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3550 SSL_R_MISSING_DSA_SIGNING_CERT);
3554 #ifndef OPENSSL_NO_RSA
3555 if (alg_k & SSL_kRSA) {
3556 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3557 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3558 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3559 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3561 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3562 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3563 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3564 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3565 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3569 /* server key exchange is not allowed. */
3570 al = SSL_AD_INTERNAL_ERROR;
3571 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3578 #ifndef OPENSSL_NO_DH
3579 if ((alg_k & SSL_kEDH) && dh == NULL) {
3580 al = SSL_AD_INTERNAL_ERROR;
3581 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3584 if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3585 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3586 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3587 SSL_R_MISSING_DH_RSA_CERT);
3590 # ifndef OPENSSL_NO_DSA
3591 if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3592 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3593 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3594 SSL_R_MISSING_DH_DSA_CERT);
3599 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3601 if (alg_k & SSL_kDHE) {
3602 dh_size = BN_num_bits(dh->p);
3604 DH *dh_srvr = get_server_static_dh_key(sc);
3605 if (dh_srvr == NULL)
3607 dh_size = BN_num_bits(dh_srvr->p);
3611 if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024)
3612 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
3613 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
3617 #endif /* !OPENSSL_NO_DH */
3619 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3620 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3621 #ifndef OPENSSL_NO_RSA
3622 if (alg_k & SSL_kRSA) {
3624 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3625 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3627 } else if (BN_num_bits(rsa->n) >
3628 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3629 /* We have a temporary RSA key but it's too large. */
3630 al = SSL_AD_EXPORT_RESTRICTION;
3631 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3632 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3637 #ifndef OPENSSL_NO_DH
3638 if (alg_k & SSL_kDHE) {
3639 if (BN_num_bits(dh->p) >
3640 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3641 /* We have a temporary DH key but it's too large. */
3642 al = SSL_AD_EXPORT_RESTRICTION;
3643 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3644 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3647 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3648 /* The cert should have had an export DH key. */
3649 al = SSL_AD_EXPORT_RESTRICTION;
3650 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3651 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3656 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3657 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3663 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3668 #ifndef OPENSSL_NO_TLSEXT
3670 * Normally, we can tell if the server is resuming the session from
3671 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3672 * message after the ServerHello to determine if the server is resuming.
3673 * Therefore, we allow EAP-FAST to peek ahead.
3674 * ssl3_check_finished returns 1 if we are resuming from an external
3675 * pre-shared secret, we have a "ticket" and the next server handshake message
3676 * is Finished; and 0 otherwise. It returns -1 upon an error.
3678 static int ssl3_check_finished(SSL *s)
3682 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3683 !s->session->tlsext_tick)
3686 /* Need to permit this temporarily, in case the next message is Finished. */
3687 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3689 * This function is called when we might get a Certificate message instead,
3690 * so permit appropriate message length.
3691 * We ignore the return value as we're only interested in the message type
3692 * and not its length.
3694 s->method->ssl_get_message(s,
3697 -1, s->max_cert_list, &ok);
3698 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3703 s->s3->tmp.reuse_message = 1;
3705 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3708 /* If we're not done, then the CCS arrived early and we should bail. */
3709 if (s->s3->change_cipher_spec) {
3710 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3711 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3718 # ifndef OPENSSL_NO_NEXTPROTONEG
3719 int ssl3_send_next_proto(SSL *s)
3721 unsigned int len, padding_len;
3724 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3725 len = s->next_proto_negotiated_len;
3726 padding_len = 32 - ((len + 2) % 32);
3727 d = (unsigned char *)s->init_buf->data;
3729 memcpy(d + 5, s->next_proto_negotiated, len);
3730 d[5 + len] = padding_len;
3731 memset(d + 6 + len, 0, padding_len);
3732 *(d++) = SSL3_MT_NEXT_PROTO;
3733 l2n3(2 + len + padding_len, d);
3734 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3735 s->init_num = 4 + 2 + len + padding_len;
3739 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3741 #endif /* !OPENSSL_NO_NEXTPROTONEG */
3742 #endif /* !OPENSSL_NO_TLSEXT */
3744 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3747 #ifndef OPENSSL_NO_ENGINE
3748 if (s->ctx->client_cert_engine) {
3749 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3750 SSL_get_client_CA_list(s),
3751 px509, ppkey, NULL, NULL, NULL);
3756 if (s->ctx->client_cert_cb)
3757 i = s->ctx->client_cert_cb(s, px509, ppkey);