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 <openssl/buffer.h>
154 #include <openssl/rand.h>
155 #include <openssl/objects.h>
156 #include <openssl/evp.h>
157 #include <openssl/md5.h>
158 #ifndef OPENSSL_NO_DH
159 # include <openssl/dh.h>
161 #include <openssl/bn.h>
162 #ifndef OPENSSL_NO_ENGINE
163 # include <openssl/engine.h>
166 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
167 #ifndef OPENSSL_NO_TLSEXT
168 static int ssl3_check_finished(SSL *s);
171 #ifndef OPENSSL_NO_SSL3_METHOD
172 static const SSL_METHOD *ssl3_get_client_method(int ver)
174 if (ver == SSL3_VERSION)
175 return (SSLv3_client_method());
180 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
181 ssl_undefined_function,
182 ssl3_connect, ssl3_get_client_method)
184 int ssl3_connect(SSL *s)
187 unsigned long Time = (unsigned long)time(NULL);
188 void (*cb) (const SSL *ssl, int type, int val) = NULL;
190 int new_state, state, skip = 0;
192 RAND_add(&Time, sizeof(Time), 0);
196 if (s->info_callback != NULL)
197 cb = s->info_callback;
198 else if (s->ctx->info_callback != NULL)
199 cb = s->ctx->info_callback;
202 if (!SSL_in_init(s) || SSL_in_before(s)) {
207 #ifndef OPENSSL_NO_HEARTBEATS
209 * If we're awaiting a HeartbeatResponse, pretend we already got and
210 * don't await it anymore, because Heartbeats don't make sense during
213 if (s->tlsext_hb_pending) {
214 s->tlsext_hb_pending = 0;
223 case SSL_ST_RENEGOTIATE:
225 s->state = SSL_ST_CONNECT;
226 s->ctx->stats.sess_connect_renegotiate++;
230 case SSL_ST_BEFORE | SSL_ST_CONNECT:
231 case SSL_ST_OK | SSL_ST_CONNECT:
235 cb(s, SSL_CB_HANDSHAKE_START, 1);
237 if ((s->version & 0xff00) != 0x0300) {
238 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
239 s->state = SSL_ST_ERR;
244 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
245 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
249 /* s->version=SSL3_VERSION; */
250 s->type = SSL_ST_CONNECT;
252 if (s->init_buf == NULL) {
253 if ((buf = BUF_MEM_new()) == NULL) {
255 s->state = SSL_ST_ERR;
258 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
260 s->state = SSL_ST_ERR;
267 if (!ssl3_setup_buffers(s)) {
272 /* setup buffing BIO */
273 if (!ssl_init_wbio_buffer(s, 0)) {
275 s->state = SSL_ST_ERR;
279 /* don't push the buffering BIO quite yet */
281 ssl3_init_finished_mac(s);
283 s->state = SSL3_ST_CW_CLNT_HELLO_A;
284 s->ctx->stats.sess_connect++;
286 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
288 * Should have been reset by ssl3_get_finished, too.
290 s->s3->change_cipher_spec = 0;
293 case SSL3_ST_CW_CLNT_HELLO_A:
294 case SSL3_ST_CW_CLNT_HELLO_B:
297 ret = ssl3_client_hello(s);
300 s->state = SSL3_ST_CR_SRVR_HELLO_A;
303 /* turn on buffering for the next lot of output */
304 if (s->bbio != s->wbio)
305 s->wbio = BIO_push(s->bbio, s->wbio);
309 case SSL3_ST_CR_SRVR_HELLO_A:
310 case SSL3_ST_CR_SRVR_HELLO_B:
311 ret = ssl3_get_server_hello(s);
316 s->state = SSL3_ST_CR_FINISHED_A;
317 #ifndef OPENSSL_NO_TLSEXT
318 if (s->tlsext_ticket_expected) {
319 /* receive renewed session ticket */
320 s->state = SSL3_ST_CR_SESSION_TICKET_A;
324 s->state = SSL3_ST_CR_CERT_A;
328 case SSL3_ST_CR_CERT_A:
329 case SSL3_ST_CR_CERT_B:
330 #ifndef OPENSSL_NO_TLSEXT
331 /* Noop (ret = 0) for everything but EAP-FAST. */
332 ret = ssl3_check_finished(s);
337 s->state = SSL3_ST_CR_FINISHED_A;
342 /* Check if it is anon DH/ECDH, SRP auth */
346 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
347 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
348 ret = ssl3_get_server_certificate(s);
351 #ifndef OPENSSL_NO_TLSEXT
352 if (s->tlsext_status_expected)
353 s->state = SSL3_ST_CR_CERT_STATUS_A;
355 s->state = SSL3_ST_CR_KEY_EXCH_A;
358 s->state = SSL3_ST_CR_KEY_EXCH_A;
364 s->state = SSL3_ST_CR_KEY_EXCH_A;
369 case SSL3_ST_CR_KEY_EXCH_A:
370 case SSL3_ST_CR_KEY_EXCH_B:
371 ret = ssl3_get_key_exchange(s);
374 s->state = SSL3_ST_CR_CERT_REQ_A;
378 * at this point we check that we have the required stuff from
381 if (!ssl3_check_cert_and_algorithm(s)) {
383 s->state = SSL_ST_ERR;
388 case SSL3_ST_CR_CERT_REQ_A:
389 case SSL3_ST_CR_CERT_REQ_B:
390 ret = ssl3_get_certificate_request(s);
393 s->state = SSL3_ST_CR_SRVR_DONE_A;
397 case SSL3_ST_CR_SRVR_DONE_A:
398 case SSL3_ST_CR_SRVR_DONE_B:
399 ret = ssl3_get_server_done(s);
402 #ifndef OPENSSL_NO_SRP
403 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
404 if ((ret = SRP_Calc_A_param(s)) <= 0) {
405 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
406 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
407 s->state = SSL_ST_ERR;
412 if (s->s3->tmp.cert_req)
413 s->state = SSL3_ST_CW_CERT_A;
415 s->state = SSL3_ST_CW_KEY_EXCH_A;
420 case SSL3_ST_CW_CERT_A:
421 case SSL3_ST_CW_CERT_B:
422 case SSL3_ST_CW_CERT_C:
423 case SSL3_ST_CW_CERT_D:
424 ret = ssl3_send_client_certificate(s);
427 s->state = SSL3_ST_CW_KEY_EXCH_A;
431 case SSL3_ST_CW_KEY_EXCH_A:
432 case SSL3_ST_CW_KEY_EXCH_B:
433 ret = ssl3_send_client_key_exchange(s);
437 * EAY EAY EAY need to check for DH fix cert sent back
440 * For TLS, cert_req is set to 2, so a cert chain of nothing is
441 * sent, but no verify packet is sent
444 * XXX: For now, we do not support client authentication in ECDH
445 * cipher suites with ECDH (rather than ECDSA) certificates. We
446 * need to skip the certificate verify message when client's
447 * ECDH public key is sent inside the client certificate.
449 if (s->s3->tmp.cert_req == 1) {
450 s->state = SSL3_ST_CW_CERT_VRFY_A;
452 s->state = SSL3_ST_CW_CHANGE_A;
454 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
455 s->state = SSL3_ST_CW_CHANGE_A;
461 case SSL3_ST_CW_CERT_VRFY_A:
462 case SSL3_ST_CW_CERT_VRFY_B:
463 ret = ssl3_send_client_verify(s);
466 s->state = SSL3_ST_CW_CHANGE_A;
470 case SSL3_ST_CW_CHANGE_A:
471 case SSL3_ST_CW_CHANGE_B:
472 ret = ssl3_send_change_cipher_spec(s,
474 SSL3_ST_CW_CHANGE_B);
478 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
479 s->state = SSL3_ST_CW_FINISHED_A;
481 if (s->s3->next_proto_neg_seen)
482 s->state = SSL3_ST_CW_NEXT_PROTO_A;
484 s->state = SSL3_ST_CW_FINISHED_A;
488 s->session->cipher = s->s3->tmp.new_cipher;
489 #ifdef OPENSSL_NO_COMP
490 s->session->compress_meth = 0;
492 if (s->s3->tmp.new_compression == NULL)
493 s->session->compress_meth = 0;
495 s->session->compress_meth = s->s3->tmp.new_compression->id;
497 if (!s->method->ssl3_enc->setup_key_block(s)) {
499 s->state = SSL_ST_ERR;
503 if (!s->method->ssl3_enc->change_cipher_state(s,
504 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
507 s->state = SSL_ST_ERR;
513 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
514 case SSL3_ST_CW_NEXT_PROTO_A:
515 case SSL3_ST_CW_NEXT_PROTO_B:
516 ret = ssl3_send_next_proto(s);
519 s->state = SSL3_ST_CW_FINISHED_A;
523 case SSL3_ST_CW_FINISHED_A:
524 case SSL3_ST_CW_FINISHED_B:
525 ret = ssl3_send_finished(s,
526 SSL3_ST_CW_FINISHED_A,
527 SSL3_ST_CW_FINISHED_B,
529 ssl3_enc->client_finished_label,
531 ssl3_enc->client_finished_label_len);
534 s->state = SSL3_ST_CW_FLUSH;
537 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
539 s->s3->tmp.next_state = SSL_ST_OK;
540 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
541 s->state = SSL_ST_OK;
542 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
543 s->s3->delay_buf_pop_ret = 0;
546 #ifndef OPENSSL_NO_TLSEXT
548 * Allow NewSessionTicket if ticket expected
550 if (s->tlsext_ticket_expected)
551 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
555 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
560 #ifndef OPENSSL_NO_TLSEXT
561 case SSL3_ST_CR_SESSION_TICKET_A:
562 case SSL3_ST_CR_SESSION_TICKET_B:
563 ret = ssl3_get_new_session_ticket(s);
566 s->state = SSL3_ST_CR_FINISHED_A;
570 case SSL3_ST_CR_CERT_STATUS_A:
571 case SSL3_ST_CR_CERT_STATUS_B:
572 ret = ssl3_get_cert_status(s);
575 s->state = SSL3_ST_CR_KEY_EXCH_A;
580 case SSL3_ST_CR_FINISHED_A:
581 case SSL3_ST_CR_FINISHED_B:
582 if (!s->s3->change_cipher_spec)
583 s->s3->flags |= SSL3_FLAGS_CCS_OK;
584 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
585 SSL3_ST_CR_FINISHED_B);
590 s->state = SSL3_ST_CW_CHANGE_A;
592 s->state = SSL_ST_OK;
596 case SSL3_ST_CW_FLUSH:
597 s->rwstate = SSL_WRITING;
598 if (BIO_flush(s->wbio) <= 0) {
602 s->rwstate = SSL_NOTHING;
603 s->state = s->s3->tmp.next_state;
607 /* clean a few things up */
608 ssl3_cleanup_key_block(s);
609 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;
667 cb(s, SSL_CB_CONNECT_EXIT, ret);
671 int ssl3_client_hello(SSL *s)
674 unsigned char *p, *d;
678 #ifndef OPENSSL_NO_COMP
683 buf = (unsigned char *)s->init_buf->data;
684 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
685 SSL_SESSION *sess = s->session;
686 if ((sess == NULL) || (sess->ssl_version != s->version) ||
687 #ifdef OPENSSL_NO_TLSEXT
688 !sess->session_id_length ||
691 * In the case of EAP-FAST, we can have a pre-shared
692 * "ticket" without a session ID.
694 (!sess->session_id_length && !sess->tlsext_tick) ||
696 (sess->not_resumable)) {
697 if (!ssl_get_new_session(s, 0))
700 if (s->method->version == DTLS_ANY_VERSION) {
701 /* Determine which DTLS version to use */
702 int options = s->options;
703 /* If DTLS 1.2 disabled correct the version number */
704 if (options & SSL_OP_NO_DTLSv1_2) {
705 if (tls1_suiteb(s)) {
706 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
707 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
711 * Disabling all versions is silly: return an error.
713 if (options & SSL_OP_NO_DTLSv1) {
714 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
718 * Update method so we don't use any DTLS 1.2 features.
720 s->method = DTLSv1_client_method();
721 s->version = DTLS1_VERSION;
724 * We only support one version: update method
726 if (options & SSL_OP_NO_DTLSv1)
727 s->method = DTLSv1_2_client_method();
728 s->version = DTLS1_2_VERSION;
730 s->client_version = s->version;
732 /* else use the pre-loaded session */
734 p = s->s3->client_random;
737 * for DTLS if client_random is initialized, reuse it, we are
738 * required to use same upon reply to HelloVerify
740 if (SSL_IS_DTLS(s)) {
743 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
752 if (i && ssl_fill_hello_random(s, 0, p,
753 sizeof(s->s3->client_random)) <= 0)
756 /* Do the message type and length last */
757 d = p = ssl_handshake_start(s);
760 * version indicates the negotiated version: for example from
761 * an SSLv2/v3 compatible client hello). The client_version
762 * field is the maximum version we permit and it is also
763 * used in RSA encrypted premaster secrets. Some servers can
764 * choke if we initially report a higher version then
765 * renegotiate to a lower one in the premaster secret. This
766 * didn't happen with TLS 1.0 as most servers supported it
767 * but it can with TLS 1.1 or later if the server only supports
770 * Possible scenario with previous logic:
771 * 1. Client hello indicates TLS 1.2
772 * 2. Server hello says TLS 1.0
773 * 3. RSA encrypted premaster secret uses 1.2.
774 * 4. Handhaked proceeds using TLS 1.0.
775 * 5. Server sends hello request to renegotiate.
776 * 6. Client hello indicates TLS v1.0 as we now
777 * know that is maximum server supports.
778 * 7. Server chokes on RSA encrypted premaster secret
779 * containing version 1.0.
781 * For interoperability it should be OK to always use the
782 * maximum version we support in client hello and then rely
783 * on the checking of version to ensure the servers isn't
784 * being inconsistent: for example initially negotiating with
785 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
786 * client_version in client hello and not resetting it to
787 * the negotiated version.
789 *(p++) = s->client_version >> 8;
790 *(p++) = s->client_version & 0xff;
793 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
794 p += SSL3_RANDOM_SIZE;
800 i = s->session->session_id_length;
803 if (i > (int)sizeof(s->session->session_id)) {
804 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
807 memcpy(p, s->session->session_id, i);
811 /* cookie stuff for DTLS */
812 if (SSL_IS_DTLS(s)) {
813 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
814 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
817 *(p++) = s->d1->cookie_len;
818 memcpy(p, s->d1->cookie, s->d1->cookie_len);
819 p += s->d1->cookie_len;
822 /* Ciphers supported */
823 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
825 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
828 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
830 * Some servers hang if client hello > 256 bytes as hack workaround
831 * chop number of supported ciphers to keep it well below this if we
834 if (TLS1_get_version(s) >= TLS1_2_VERSION
835 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
836 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
842 #ifdef OPENSSL_NO_COMP
846 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
849 j = sk_SSL_COMP_num(s->ctx->comp_methods);
851 for (i = 0; i < j; i++) {
852 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
856 *(p++) = 0; /* Add the NULL method */
858 #ifndef OPENSSL_NO_TLSEXT
860 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
861 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
865 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
867 ssl3_send_alert(s, SSL3_AL_FATAL, al);
868 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
874 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
875 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
876 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
879 s->state = SSL3_ST_CW_CLNT_HELLO_B;
882 /* SSL3_ST_CW_CLNT_HELLO_B */
883 return ssl_do_write(s);
885 s->state = SSL_ST_ERR;
889 int ssl3_get_server_hello(SSL *s)
891 STACK_OF(SSL_CIPHER) *sk;
894 unsigned char *p, *d;
895 int i, al = SSL_AD_INTERNAL_ERROR, ok;
898 #ifndef OPENSSL_NO_COMP
902 * Hello verify request and/or server hello version may not match so set
903 * first packet if we're negotiating version.
908 n = s->method->ssl_get_message(s,
909 SSL3_ST_CR_SRVR_HELLO_A,
910 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
915 if (SSL_IS_DTLS(s)) {
917 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
918 if (s->d1->send_cookie == 0) {
919 s->s3->tmp.reuse_message = 1;
921 } else { /* already sent a cookie */
923 al = SSL_AD_UNEXPECTED_MESSAGE;
924 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
930 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
931 al = SSL_AD_UNEXPECTED_MESSAGE;
932 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
936 d = p = (unsigned char *)s->init_msg;
937 if (s->method->version == DTLS_ANY_VERSION) {
938 /* Work out correct protocol version to use */
939 int hversion = (p[0] << 8) | p[1];
940 int options = s->options;
941 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
942 s->method = DTLSv1_2_client_method();
943 else if (tls1_suiteb(s)) {
944 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
945 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
946 s->version = hversion;
947 al = SSL_AD_PROTOCOL_VERSION;
949 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
950 s->method = DTLSv1_client_method();
952 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
953 s->version = hversion;
954 al = SSL_AD_PROTOCOL_VERSION;
957 s->version = s->method->version;
960 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
961 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
962 s->version = (s->version & 0xff00) | p[1];
963 al = SSL_AD_PROTOCOL_VERSION;
968 /* load the server hello data */
969 /* load the server random */
970 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
971 p += SSL3_RANDOM_SIZE;
975 /* get the session-id */
978 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
979 al = SSL_AD_ILLEGAL_PARAMETER;
980 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
983 #ifndef OPENSSL_NO_TLSEXT
985 * Check if we can resume the session based on external pre-shared secret.
986 * EAP-FAST (RFC 4851) supports two types of session resumption.
987 * Resumption based on server-side state works with session IDs.
988 * Resumption based on pre-shared Protected Access Credentials (PACs)
989 * works by overriding the SessionTicket extension at the application
990 * layer, and does not send a session ID. (We do not know whether EAP-FAST
991 * servers would honour the session ID.) Therefore, the session ID alone
992 * is not a reliable indicator of session resumption, so we first check if
993 * we can resume, and later peek at the next handshake message to see if the
994 * server wants to resume.
996 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
997 s->session->tlsext_tick) {
998 SSL_CIPHER *pref_cipher = NULL;
999 s->session->master_key_length = sizeof(s->session->master_key);
1000 if (s->tls_session_secret_cb(s, s->session->master_key,
1001 &s->session->master_key_length,
1003 s->tls_session_secret_cb_arg)) {
1004 s->session->cipher = pref_cipher ?
1005 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1007 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1008 al = SSL_AD_INTERNAL_ERROR;
1012 #endif /* OPENSSL_NO_TLSEXT */
1014 if (j != 0 && j == s->session->session_id_length
1015 && memcmp(p, s->session->session_id, j) == 0) {
1016 if (s->sid_ctx_length != s->session->sid_ctx_length
1017 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1018 /* actually a client application bug */
1019 al = SSL_AD_ILLEGAL_PARAMETER;
1020 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1021 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1027 * If we were trying for session-id reuse but the server
1028 * didn't echo the ID, make a new SSL_SESSION.
1029 * In the case of EAP-FAST and PAC, we do not send a session ID,
1030 * so the PAC-based session secret is always preserved. It'll be
1031 * overwritten if the server refuses resumption.
1033 if (s->session->session_id_length > 0) {
1034 if (!ssl_get_new_session(s, 0)) {
1038 s->session->session_id_length = j;
1039 memcpy(s->session->session_id, p, j); /* j could be 0 */
1042 c = ssl_get_cipher_by_char(s, p);
1044 /* unknown cipher */
1045 al = SSL_AD_ILLEGAL_PARAMETER;
1046 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1049 /* Set version disabled mask now we know version */
1050 if (!SSL_USE_TLS1_2_CIPHERS(s))
1051 ct->mask_ssl = SSL_TLSV1_2;
1055 * If it is a disabled cipher we didn't send it in client hello, so
1058 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1059 al = SSL_AD_ILLEGAL_PARAMETER;
1060 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1063 p += ssl_put_cipher_by_char(s, NULL, NULL);
1065 sk = ssl_get_ciphers_by_id(s);
1066 i = sk_SSL_CIPHER_find(sk, c);
1068 /* we did not say we would use this cipher */
1069 al = SSL_AD_ILLEGAL_PARAMETER;
1070 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1075 * Depending on the session caching (internal/external), the cipher
1076 * and/or cipher_id values may not be set. Make sure that cipher_id is
1077 * set and use it for comparison.
1079 if (s->session->cipher)
1080 s->session->cipher_id = s->session->cipher->id;
1081 if (s->hit && (s->session->cipher_id != c->id)) {
1082 al = SSL_AD_ILLEGAL_PARAMETER;
1083 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1084 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1087 s->s3->tmp.new_cipher = c;
1089 * Don't digest cached records if no sigalgs: we may need them for client
1092 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1094 /* lets get the compression algorithm */
1096 #ifdef OPENSSL_NO_COMP
1098 al = SSL_AD_ILLEGAL_PARAMETER;
1099 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1100 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1104 * If compression is disabled we'd better not try to resume a session
1105 * using compression.
1107 if (s->session->compress_meth != 0) {
1108 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1113 if (s->hit && j != s->session->compress_meth) {
1114 al = SSL_AD_ILLEGAL_PARAMETER;
1115 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1116 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1121 else if (!ssl_allow_compression(s)) {
1122 al = SSL_AD_ILLEGAL_PARAMETER;
1123 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1126 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1128 if ((j != 0) && (comp == NULL)) {
1129 al = SSL_AD_ILLEGAL_PARAMETER;
1130 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1131 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1134 s->s3->tmp.new_compression = comp;
1138 #ifndef OPENSSL_NO_TLSEXT
1139 /* TLS extensions */
1140 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1141 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1147 /* wrong packet length */
1148 al = SSL_AD_DECODE_ERROR;
1149 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1155 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1157 s->state = SSL_ST_ERR;
1161 int ssl3_get_server_certificate(SSL *s)
1163 int al, i, ok, ret = -1, exp_idx;
1164 unsigned long n, nc, llen, l;
1166 const unsigned char *q, *p;
1168 STACK_OF(X509) *sk = NULL;
1170 EVP_PKEY *pkey = NULL;
1172 n = s->method->ssl_get_message(s,
1175 -1, s->max_cert_list, &ok);
1180 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1181 s->s3->tmp.reuse_message = 1;
1185 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1186 al = SSL_AD_UNEXPECTED_MESSAGE;
1187 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1190 p = d = (unsigned char *)s->init_msg;
1192 if ((sk = sk_X509_new_null()) == NULL) {
1193 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1198 if (llen + 3 != n) {
1199 al = SSL_AD_DECODE_ERROR;
1200 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1203 for (nc = 0; nc < llen;) {
1205 if ((l + nc + 3) > llen) {
1206 al = SSL_AD_DECODE_ERROR;
1207 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1208 SSL_R_CERT_LENGTH_MISMATCH);
1213 x = d2i_X509(NULL, &q, l);
1215 al = SSL_AD_BAD_CERTIFICATE;
1216 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1220 al = SSL_AD_DECODE_ERROR;
1221 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1222 SSL_R_CERT_LENGTH_MISMATCH);
1225 if (!sk_X509_push(sk, x)) {
1226 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1234 i = ssl_verify_cert_chain(s, sk);
1235 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1236 al = ssl_verify_alarm_type(s->verify_result);
1237 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1238 SSL_R_CERTIFICATE_VERIFY_FAILED);
1241 ERR_clear_error(); /* but we keep s->verify_result */
1243 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1244 al = SSL_AD_HANDSHAKE_FAILURE;
1248 sc = ssl_sess_cert_new();
1252 ssl_sess_cert_free(s->session->sess_cert);
1253 s->session->sess_cert = sc;
1255 sc->cert_chain = sk;
1257 * Inconsistency alert: cert_chain does include the peer's certificate,
1258 * which we don't include in s3_srvr.c
1260 x = sk_X509_value(sk, 0);
1263 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1266 pkey = X509_get_pubkey(x);
1268 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1271 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1272 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1276 i = ssl_cert_type(x, pkey);
1280 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1281 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1285 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1286 if (exp_idx >= 0 && i != exp_idx) {
1288 al = SSL_AD_ILLEGAL_PARAMETER;
1289 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1290 SSL_R_WRONG_CERTIFICATE_TYPE);
1293 sc->peer_cert_type = i;
1294 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1296 * Why would the following ever happen? We just created sc a couple
1299 X509_free(sc->peer_pkeys[i].x509);
1300 sc->peer_pkeys[i].x509 = x;
1301 sc->peer_key = &(sc->peer_pkeys[i]);
1303 X509_free(s->session->peer);
1304 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1305 s->session->peer = x;
1306 s->session->verify_result = s->verify_result;
1313 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1315 s->state = SSL_ST_ERR;
1317 EVP_PKEY_free(pkey);
1319 sk_X509_pop_free(sk, X509_free);
1323 int ssl3_get_key_exchange(SSL *s)
1325 #ifndef OPENSSL_NO_RSA
1326 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1329 unsigned char *param, *p;
1331 long i, param_len, n, alg_k, alg_a;
1332 EVP_PKEY *pkey = NULL;
1333 const EVP_MD *md = NULL;
1334 #ifndef OPENSSL_NO_RSA
1337 #ifndef OPENSSL_NO_DH
1340 #ifndef OPENSSL_NO_EC
1341 EC_KEY *ecdh = NULL;
1342 BN_CTX *bn_ctx = NULL;
1343 EC_POINT *srvr_ecpoint = NULL;
1345 int encoded_pt_len = 0;
1348 EVP_MD_CTX_init(&md_ctx);
1351 * use same message size as in ssl3_get_certificate_request() as
1352 * ServerKeyExchange message may be skipped
1354 n = s->method->ssl_get_message(s,
1355 SSL3_ST_CR_KEY_EXCH_A,
1356 SSL3_ST_CR_KEY_EXCH_B,
1357 -1, s->max_cert_list, &ok);
1361 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1363 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1365 * Can't skip server key exchange if this is an ephemeral
1368 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1369 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1370 al = SSL_AD_UNEXPECTED_MESSAGE;
1373 #ifndef OPENSSL_NO_PSK
1375 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1376 * identity hint is sent. Set session->sess_cert anyway to avoid
1379 if (alg_k & SSL_kPSK) {
1380 s->session->sess_cert = ssl_sess_cert_new();
1381 OPENSSL_free(s->ctx->psk_identity_hint);
1382 s->ctx->psk_identity_hint = NULL;
1385 s->s3->tmp.reuse_message = 1;
1389 param = p = (unsigned char *)s->init_msg;
1390 if (s->session->sess_cert != NULL) {
1391 #ifndef OPENSSL_NO_RSA
1392 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1393 s->session->sess_cert->peer_rsa_tmp = NULL;
1395 #ifndef OPENSSL_NO_DH
1396 DH_free(s->session->sess_cert->peer_dh_tmp);
1397 s->session->sess_cert->peer_dh_tmp = NULL;
1399 #ifndef OPENSSL_NO_EC
1400 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1401 s->session->sess_cert->peer_ecdh_tmp = NULL;
1404 s->session->sess_cert = ssl_sess_cert_new();
1407 /* Total length of the parameters including the length prefix */
1410 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1412 al = SSL_AD_DECODE_ERROR;
1414 #ifndef OPENSSL_NO_PSK
1415 if (alg_k & SSL_kPSK) {
1416 char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];
1419 if (param_len > n) {
1420 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1426 * Store PSK identity hint for later use, hint is used in
1427 * ssl3_send_client_key_exchange. Assume that the maximum length of
1428 * a PSK identity hint can be as long as the maximum length of a PSK
1431 if (i > PSK_MAX_IDENTITY_LEN) {
1432 al = SSL_AD_HANDSHAKE_FAILURE;
1433 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1436 if (i > n - param_len) {
1437 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1438 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1444 * If received PSK identity hint contains NULL characters, the hint
1445 * is truncated from the first NULL. p may not be ending with NULL,
1446 * so create a NULL-terminated string.
1448 memcpy(tmp_id_hint, p, i);
1449 memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
1450 OPENSSL_free(s->ctx->psk_identity_hint);
1451 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1452 if (s->ctx->psk_identity_hint == NULL) {
1453 al = SSL_AD_HANDSHAKE_FAILURE;
1454 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1461 #endif /* !OPENSSL_NO_PSK */
1462 #ifndef OPENSSL_NO_SRP
1463 if (alg_k & SSL_kSRP) {
1465 if (param_len > n) {
1466 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1471 if (i > n - param_len) {
1472 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1477 if ((s->srp_ctx.N = BN_bin2bn(p, i, NULL)) == NULL) {
1478 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1483 if (2 > n - param_len) {
1484 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1491 if (i > n - param_len) {
1492 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1497 if ((s->srp_ctx.g = BN_bin2bn(p, i, NULL)) == NULL) {
1498 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1503 if (1 > n - param_len) {
1504 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1509 i = (unsigned int)(p[0]);
1512 if (i > n - param_len) {
1513 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1518 if ((s->srp_ctx.s = BN_bin2bn(p, i, NULL)) == NULL) {
1519 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1524 if (2 > n - param_len) {
1525 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1532 if (i > n - param_len) {
1533 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1538 if ((s->srp_ctx.B = BN_bin2bn(p, i, NULL)) == NULL) {
1539 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1545 if (!srp_verify_server_param(s, &al)) {
1546 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1550 /* We must check if there is a certificate */
1551 # ifndef OPENSSL_NO_RSA
1552 if (alg_a & SSL_aRSA)
1554 X509_get_pubkey(s->session->
1555 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1559 # ifndef OPENSSL_NO_DSA
1560 else if (alg_a & SSL_aDSS)
1562 X509_get_pubkey(s->session->
1563 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1567 #endif /* !OPENSSL_NO_SRP */
1568 #ifndef OPENSSL_NO_RSA
1569 if (alg_k & SSL_kRSA) {
1570 /* Temporary RSA keys only allowed in export ciphersuites */
1571 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1572 al = SSL_AD_UNEXPECTED_MESSAGE;
1573 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1576 if ((rsa = RSA_new()) == NULL) {
1577 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1582 if (param_len > n) {
1583 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1588 if (i > n - param_len) {
1589 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1594 if ((rsa->n = BN_bin2bn(p, i, rsa->n)) == NULL) {
1595 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1600 if (2 > n - param_len) {
1601 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1608 if (i > n - param_len) {
1609 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1614 if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == NULL) {
1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1621 /* this should be because we are using an export cipher */
1622 if (alg_a & SSL_aRSA)
1624 X509_get_pubkey(s->session->
1625 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1630 s->session->sess_cert->peer_rsa_tmp = rsa;
1633 #else /* OPENSSL_NO_RSA */
1636 #ifndef OPENSSL_NO_DH
1637 else if (alg_k & SSL_kDHE) {
1638 if ((dh = DH_new()) == NULL) {
1639 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1644 if (param_len > n) {
1645 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1650 if (i > n - param_len) {
1651 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1656 if ((dh->p = BN_bin2bn(p, i, NULL)) == NULL) {
1657 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1662 if (2 > n - param_len) {
1663 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1670 if (i > n - param_len) {
1671 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1676 if ((dh->g = BN_bin2bn(p, i, NULL)) == NULL) {
1677 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1682 if (2 > n - param_len) {
1683 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1690 if (i > n - param_len) {
1691 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1696 if ((dh->pub_key = BN_bin2bn(p, i, NULL)) == NULL) {
1697 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1703 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1704 al = SSL_AD_HANDSHAKE_FAILURE;
1705 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1708 # ifndef OPENSSL_NO_RSA
1709 if (alg_a & SSL_aRSA)
1711 X509_get_pubkey(s->session->
1712 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1716 # ifndef OPENSSL_NO_DSA
1717 else if (alg_a & SSL_aDSS)
1719 X509_get_pubkey(s->session->
1720 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1723 /* else anonymous DH, so no certificate or pkey. */
1725 s->session->sess_cert->peer_dh_tmp = dh;
1728 #endif /* !OPENSSL_NO_DH */
1730 #ifndef OPENSSL_NO_EC
1731 else if (alg_k & SSL_kECDHE) {
1733 const EC_GROUP *group;
1735 if ((ecdh = EC_KEY_new()) == NULL) {
1736 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1741 * Extract elliptic curve parameters and the server's ephemeral ECDH
1742 * public key. Keep accumulating lengths of various components in
1743 * param_len and make sure it never exceeds n.
1747 * XXX: For now we only support named (not generic) curves and the
1748 * ECParameters in this case is just three bytes. We also need one
1749 * byte for the length of the encoded point
1752 if (param_len > n) {
1753 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1757 * Check curve is one of our preferences, if not server has sent an
1758 * invalid curve. ECParameters is 3 bytes.
1760 if (!tls1_check_curve(s, p, 3)) {
1761 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1765 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1766 al = SSL_AD_INTERNAL_ERROR;
1767 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1768 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1772 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1773 if (ngroup == NULL) {
1774 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1777 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1778 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1781 EC_GROUP_free(ngroup);
1783 group = EC_KEY_get0_group(ecdh);
1785 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1786 (EC_GROUP_get_degree(group) > 163)) {
1787 al = SSL_AD_EXPORT_RESTRICTION;
1788 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1789 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1795 /* Next, get the encoded ECPoint */
1796 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1797 ((bn_ctx = BN_CTX_new()) == NULL)) {
1798 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1802 encoded_pt_len = *p; /* length of encoded point */
1805 if ((encoded_pt_len > n - param_len) ||
1806 (EC_POINT_oct2point(group, srvr_ecpoint,
1807 p, encoded_pt_len, bn_ctx) == 0)) {
1808 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1811 param_len += encoded_pt_len;
1814 p += encoded_pt_len;
1817 * The ECC/TLS specification does not mention the use of DSA to sign
1818 * ECParameters in the server key exchange message. We do support RSA
1822 # ifndef OPENSSL_NO_RSA
1823 else if (alg_a & SSL_aRSA)
1825 X509_get_pubkey(s->session->
1826 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1828 # ifndef OPENSSL_NO_EC
1829 else if (alg_a & SSL_aECDSA)
1831 X509_get_pubkey(s->session->
1832 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1834 /* else anonymous ECDH, so no certificate or pkey. */
1835 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1836 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1838 BN_CTX_free(bn_ctx);
1840 EC_POINT_free(srvr_ecpoint);
1841 srvr_ecpoint = NULL;
1843 al = SSL_AD_UNEXPECTED_MESSAGE;
1844 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1847 #endif /* !OPENSSL_NO_EC */
1849 /* p points to the next byte, there are 'n' bytes left */
1851 /* if it was signed, check the signature */
1853 if (SSL_USE_SIGALGS(s)) {
1856 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1859 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1866 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1874 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1879 j = EVP_PKEY_size(pkey);
1882 * Check signature length. If n is 0 then signature is empty
1884 if ((i != n) || (n > j) || (n <= 0)) {
1885 /* wrong packet length */
1886 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1889 #ifndef OPENSSL_NO_RSA
1890 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1896 for (num = 2; num > 0; num--) {
1897 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1898 EVP_DigestInit_ex(&md_ctx, (num == 2)
1899 ? s->ctx->md5 : s->ctx->sha1, NULL);
1900 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1902 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1904 EVP_DigestUpdate(&md_ctx, param, param_len);
1905 EVP_DigestFinal_ex(&md_ctx, q, &size);
1909 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1911 al = SSL_AD_DECRYPT_ERROR;
1912 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1917 al = SSL_AD_DECRYPT_ERROR;
1918 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1924 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1925 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1927 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1929 EVP_VerifyUpdate(&md_ctx, param, param_len);
1930 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1932 al = SSL_AD_DECRYPT_ERROR;
1933 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1938 /* aNULL, aSRP or kPSK do not need public keys */
1939 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1940 /* Might be wrong key type, check it */
1941 if (ssl3_check_cert_and_algorithm(s))
1942 /* Otherwise this shouldn't happen */
1943 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1946 /* still data left over */
1948 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1952 EVP_PKEY_free(pkey);
1953 EVP_MD_CTX_cleanup(&md_ctx);
1956 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1958 EVP_PKEY_free(pkey);
1959 #ifndef OPENSSL_NO_RSA
1962 #ifndef OPENSSL_NO_DH
1965 #ifndef OPENSSL_NO_EC
1966 BN_CTX_free(bn_ctx);
1967 EC_POINT_free(srvr_ecpoint);
1970 EVP_MD_CTX_cleanup(&md_ctx);
1971 s->state = SSL_ST_ERR;
1975 int ssl3_get_certificate_request(SSL *s)
1978 unsigned long n, nc, l;
1979 unsigned int llen, ctype_num, i;
1980 X509_NAME *xn = NULL;
1981 const unsigned char *p, *q;
1983 STACK_OF(X509_NAME) *ca_sk = NULL;
1985 n = s->method->ssl_get_message(s,
1986 SSL3_ST_CR_CERT_REQ_A,
1987 SSL3_ST_CR_CERT_REQ_B,
1988 -1, s->max_cert_list, &ok);
1993 s->s3->tmp.cert_req = 0;
1995 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1996 s->s3->tmp.reuse_message = 1;
1998 * If we get here we don't need any cached handshake records as we
1999 * wont be doing client auth.
2001 if (s->s3->handshake_buffer) {
2002 if (!ssl3_digest_cached_records(s))
2008 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2009 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2010 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2014 /* TLS does not like anon-DH with client cert */
2015 if (s->version > SSL3_VERSION) {
2016 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2017 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2018 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2019 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2024 p = d = (unsigned char *)s->init_msg;
2026 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2027 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2031 /* get the certificate types */
2033 OPENSSL_free(s->cert->ctypes);
2034 s->cert->ctypes = NULL;
2035 if (ctype_num > SSL3_CT_NUMBER) {
2036 /* If we exceed static buffer copy all to cert structure */
2037 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2038 if (s->cert->ctypes == NULL) {
2039 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2042 memcpy(s->cert->ctypes, p, ctype_num);
2043 s->cert->ctype_num = (size_t)ctype_num;
2044 ctype_num = SSL3_CT_NUMBER;
2046 for (i = 0; i < ctype_num; i++)
2047 s->s3->tmp.ctype[i] = p[i];
2049 if (SSL_USE_SIGALGS(s)) {
2052 * Check we have enough room for signature algorithms and following
2055 if ((unsigned long)(p - d + llen + 2) > n) {
2056 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2057 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2058 SSL_R_DATA_LENGTH_TOO_LONG);
2061 /* Clear certificate digests and validity flags */
2062 for (i = 0; i < SSL_PKEY_NUM; i++) {
2063 s->cert->pkeys[i].digest = NULL;
2064 s->cert->pkeys[i].valid_flags = 0;
2066 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2067 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2068 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2069 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2072 if (!tls1_process_sigalgs(s)) {
2073 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2074 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2080 /* get the CA RDNs */
2083 if ((unsigned long)(p - d + llen) != n) {
2084 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2085 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2089 for (nc = 0; nc < llen;) {
2091 if ((l + nc + 2) > llen) {
2092 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2093 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2099 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2100 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2101 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2106 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2107 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2108 SSL_R_CA_DN_LENGTH_MISMATCH);
2111 if (!sk_X509_NAME_push(ca_sk, xn)) {
2112 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2120 /* we should setup a certificate to return.... */
2121 s->s3->tmp.cert_req = 1;
2122 s->s3->tmp.ctype_num = ctype_num;
2123 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2124 s->s3->tmp.ca_names = ca_sk;
2130 s->state = SSL_ST_ERR;
2132 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2136 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2138 return (X509_NAME_cmp(*a, *b));
2141 #ifndef OPENSSL_NO_TLSEXT
2142 int ssl3_get_new_session_ticket(SSL *s)
2144 int ok, al, ret = 0, ticklen;
2146 const unsigned char *p;
2149 n = s->method->ssl_get_message(s,
2150 SSL3_ST_CR_SESSION_TICKET_A,
2151 SSL3_ST_CR_SESSION_TICKET_B,
2152 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2158 /* need at least ticket_lifetime_hint + ticket length */
2159 al = SSL_AD_DECODE_ERROR;
2160 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2164 p = d = (unsigned char *)s->init_msg;
2165 n2l(p, s->session->tlsext_tick_lifetime_hint);
2167 /* ticket_lifetime_hint + ticket_length + ticket */
2168 if (ticklen + 6 != n) {
2169 al = SSL_AD_DECODE_ERROR;
2170 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2173 OPENSSL_free(s->session->tlsext_tick);
2174 s->session->tlsext_ticklen = 0;
2175 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2176 if (!s->session->tlsext_tick) {
2177 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2180 memcpy(s->session->tlsext_tick, p, ticklen);
2181 s->session->tlsext_ticklen = ticklen;
2183 * There are two ways to detect a resumed ticket session. One is to set
2184 * an appropriate session ID and then the server must return a match in
2185 * ServerHello. This allows the normal client session ID matching to work
2186 * and we know much earlier that the ticket has been accepted. The
2187 * other way is to set zero length session ID when the ticket is
2188 * presented and rely on the handshake to determine session resumption.
2189 * We choose the former approach because this fits in with assumptions
2190 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2191 * SHA256 is disabled) hash of the ticket.
2193 EVP_Digest(p, ticklen,
2194 s->session->session_id, &s->session->session_id_length,
2195 EVP_sha256(), NULL);
2199 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2201 s->state = SSL_ST_ERR;
2205 int ssl3_get_cert_status(SSL *s)
2208 unsigned long resplen, n;
2209 const unsigned char *p;
2211 n = s->method->ssl_get_message(s,
2212 SSL3_ST_CR_CERT_STATUS_A,
2213 SSL3_ST_CR_CERT_STATUS_B,
2214 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2219 /* need at least status type + length */
2220 al = SSL_AD_DECODE_ERROR;
2221 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2224 p = (unsigned char *)s->init_msg;
2225 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2226 al = SSL_AD_DECODE_ERROR;
2227 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2231 if (resplen + 4 != n) {
2232 al = SSL_AD_DECODE_ERROR;
2233 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2236 OPENSSL_free(s->tlsext_ocsp_resp);
2237 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2238 if (!s->tlsext_ocsp_resp) {
2239 al = SSL_AD_INTERNAL_ERROR;
2240 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2243 s->tlsext_ocsp_resplen = resplen;
2244 if (s->ctx->tlsext_status_cb) {
2246 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2248 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2249 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2253 al = SSL_AD_INTERNAL_ERROR;
2254 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2260 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2261 s->state = SSL_ST_ERR;
2266 int ssl3_get_server_done(SSL *s)
2271 /* Second to last param should be very small, like 0 :-) */
2272 n = s->method->ssl_get_message(s,
2273 SSL3_ST_CR_SRVR_DONE_A,
2274 SSL3_ST_CR_SRVR_DONE_B,
2275 SSL3_MT_SERVER_DONE, 30, &ok);
2280 /* should contain no data */
2281 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2282 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2283 s->state = SSL_ST_ERR;
2290 int ssl3_send_client_key_exchange(SSL *s)
2294 unsigned long alg_k;
2295 #ifndef OPENSSL_NO_RSA
2297 EVP_PKEY *pkey = NULL;
2299 #ifndef OPENSSL_NO_EC
2300 EC_KEY *clnt_ecdh = NULL;
2301 const EC_POINT *srvr_ecpoint = NULL;
2302 EVP_PKEY *srvr_pub_pkey = NULL;
2303 unsigned char *encodedPoint = NULL;
2304 int encoded_pt_len = 0;
2305 BN_CTX *bn_ctx = NULL;
2307 unsigned char *pms = NULL;
2310 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2311 p = ssl_handshake_start(s);
2313 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2315 /* Fool emacs indentation */
2318 #ifndef OPENSSL_NO_RSA
2319 else if (alg_k & SSL_kRSA) {
2321 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2322 pms = OPENSSL_malloc(pmslen);
2326 if (s->session->sess_cert == NULL) {
2328 * We should always have a server certificate with SSL_kRSA.
2330 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2331 ERR_R_INTERNAL_ERROR);
2335 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2336 rsa = s->session->sess_cert->peer_rsa_tmp;
2339 X509_get_pubkey(s->session->
2340 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2342 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2343 || (pkey->pkey.rsa == NULL)) {
2344 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2345 ERR_R_INTERNAL_ERROR);
2348 rsa = pkey->pkey.rsa;
2349 EVP_PKEY_free(pkey);
2352 pms[0] = s->client_version >> 8;
2353 pms[1] = s->client_version & 0xff;
2354 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2358 /* Fix buf for TLS and beyond */
2359 if (s->version > SSL3_VERSION)
2361 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2363 if (s->options & SSL_OP_PKCS1_CHECK_1)
2365 if (s->options & SSL_OP_PKCS1_CHECK_2)
2369 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2370 SSL_R_BAD_RSA_ENCRYPT);
2374 /* Fix buf for TLS and beyond */
2375 if (s->version > SSL3_VERSION) {
2381 #ifndef OPENSSL_NO_DH
2382 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2383 DH *dh_srvr, *dh_clnt;
2384 SESS_CERT *scert = s->session->sess_cert;
2386 if (scert == NULL) {
2387 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2388 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2389 SSL_R_UNEXPECTED_MESSAGE);
2393 if (scert->peer_dh_tmp != NULL)
2394 dh_srvr = scert->peer_dh_tmp;
2396 /* we get them from the cert */
2397 int idx = scert->peer_cert_type;
2398 EVP_PKEY *spkey = NULL;
2401 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2403 dh_srvr = EVP_PKEY_get1_DH(spkey);
2404 EVP_PKEY_free(spkey);
2406 if (dh_srvr == NULL) {
2407 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2408 ERR_R_INTERNAL_ERROR);
2412 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2413 /* Use client certificate key */
2414 EVP_PKEY *clkey = s->cert->key->privatekey;
2417 dh_clnt = EVP_PKEY_get1_DH(clkey);
2418 if (dh_clnt == NULL) {
2419 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2420 ERR_R_INTERNAL_ERROR);
2424 /* generate a new random key */
2425 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2426 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2429 if (!DH_generate_key(dh_clnt)) {
2430 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2436 pmslen = DH_size(dh_clnt);
2437 pms = OPENSSL_malloc(pmslen);
2442 * use the 'p' output buffer for the DH key, but make sure to
2443 * clear it out afterwards
2446 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2447 if (scert->peer_dh_tmp == NULL)
2451 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2457 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2460 /* send off the data */
2461 n = BN_num_bytes(dh_clnt->pub_key);
2463 BN_bn2bin(dh_clnt->pub_key, p);
2469 /* perhaps clean things up a bit EAY EAY EAY EAY */
2473 #ifndef OPENSSL_NO_EC
2474 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2475 const EC_GROUP *srvr_group = NULL;
2477 int ecdh_clnt_cert = 0;
2480 if (s->session->sess_cert == NULL) {
2481 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2482 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2483 SSL_R_UNEXPECTED_MESSAGE);
2488 * Did we send out the client's ECDH share for use in premaster
2489 * computation as part of client certificate? If so, set
2490 * ecdh_clnt_cert to 1.
2492 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2494 * XXX: For now, we do not support client
2495 * authentication using ECDH certificates.
2496 * To add such support, one needs to add
2497 * code that checks for appropriate
2498 * conditions and sets ecdh_clnt_cert to 1.
2499 * For example, the cert have an ECC
2500 * key on the same curve as the server's
2501 * and the key should be authorized for
2504 * One also needs to add code in ssl3_connect
2505 * to skip sending the certificate verify
2508 * if ((s->cert->key->privatekey != NULL) &&
2509 * (s->cert->key->privatekey->type ==
2510 * EVP_PKEY_EC) && ...)
2511 * ecdh_clnt_cert = 1;
2515 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2516 tkey = s->session->sess_cert->peer_ecdh_tmp;
2518 /* Get the Server Public Key from Cert */
2520 X509_get_pubkey(s->session->
2521 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2522 if ((srvr_pub_pkey == NULL)
2523 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2524 || (srvr_pub_pkey->pkey.ec == NULL)) {
2525 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2526 ERR_R_INTERNAL_ERROR);
2530 tkey = srvr_pub_pkey->pkey.ec;
2533 srvr_group = EC_KEY_get0_group(tkey);
2534 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2536 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2537 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2538 ERR_R_INTERNAL_ERROR);
2542 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2543 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2544 ERR_R_MALLOC_FAILURE);
2548 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2549 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2552 if (ecdh_clnt_cert) {
2554 * Reuse key info from our certificate We only need our
2555 * private key to perform the ECDH computation.
2557 const BIGNUM *priv_key;
2558 tkey = s->cert->key->privatekey->pkey.ec;
2559 priv_key = EC_KEY_get0_private_key(tkey);
2560 if (priv_key == NULL) {
2561 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2562 ERR_R_MALLOC_FAILURE);
2565 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2566 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2570 /* Generate a new ECDH key pair */
2571 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2572 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2579 * use the 'p' output buffer for the ECDH key, but make sure to
2580 * clear it out afterwards
2583 field_size = EC_GROUP_get_degree(srvr_group);
2584 if (field_size <= 0) {
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2588 pmslen = (field_size + 7) / 8;
2589 pms = OPENSSL_malloc(pmslen);
2592 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2593 if (n <= 0 || pmslen != (size_t)n) {
2594 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2598 if (ecdh_clnt_cert) {
2599 /* Send empty client key exch message */
2603 * First check the size of encoding and allocate memory
2607 EC_POINT_point2oct(srvr_group,
2608 EC_KEY_get0_public_key(clnt_ecdh),
2609 POINT_CONVERSION_UNCOMPRESSED,
2612 encodedPoint = (unsigned char *)
2613 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2614 bn_ctx = BN_CTX_new();
2615 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2616 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2617 ERR_R_MALLOC_FAILURE);
2621 /* Encode the public key */
2622 n = EC_POINT_point2oct(srvr_group,
2623 EC_KEY_get0_public_key(clnt_ecdh),
2624 POINT_CONVERSION_UNCOMPRESSED,
2625 encodedPoint, encoded_pt_len, bn_ctx);
2627 *p = n; /* length of encoded point */
2628 /* Encoded point will be copied here */
2630 /* copy the point */
2631 memcpy(p, encodedPoint, n);
2632 /* increment n to account for length field */
2636 /* Free allocated memory */
2637 BN_CTX_free(bn_ctx);
2638 OPENSSL_free(encodedPoint);
2639 EC_KEY_free(clnt_ecdh);
2640 EVP_PKEY_free(srvr_pub_pkey);
2642 #endif /* !OPENSSL_NO_EC */
2643 else if (alg_k & SSL_kGOST) {
2644 /* GOST key exchange message creation */
2645 EVP_PKEY_CTX *pkey_ctx;
2648 unsigned int md_len;
2650 unsigned char shared_ukm[32], tmp[256];
2651 EVP_MD_CTX *ukm_hash;
2655 pms = OPENSSL_malloc(pmslen);
2660 * Get server sertificate PKEY and create ctx from it
2664 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2668 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2670 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2671 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2675 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2676 X509_get_pubkey(peer_cert), NULL);
2678 * If we have send a certificate, and certificate key
2680 * * parameters match those of server certificate, use
2681 * certificate key for key exchange
2684 /* Otherwise, generate ephemeral key pair */
2686 EVP_PKEY_encrypt_init(pkey_ctx);
2687 /* Generate session key */
2688 if (RAND_bytes(pms, pmslen) <= 0) {
2689 EVP_PKEY_CTX_free(pkey_ctx);
2690 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2691 ERR_R_INTERNAL_ERROR);
2695 * If we have client certificate, use its secret as peer key
2697 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2698 if (EVP_PKEY_derive_set_peer
2699 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2701 * If there was an error - just ignore it. Ephemeral key
2708 * Compute shared IV and store it in algorithm-specific context
2711 ukm_hash = EVP_MD_CTX_create();
2712 EVP_DigestInit(ukm_hash,
2713 EVP_get_digestbynid(NID_id_GostR3411_94));
2714 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2716 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2718 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2719 EVP_MD_CTX_destroy(ukm_hash);
2720 if (EVP_PKEY_CTX_ctrl
2721 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2723 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2727 /* Make GOST keytransport blob message */
2729 * Encapsulate it into sequence
2731 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2733 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2734 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2738 if (msglen >= 0x80) {
2740 *(p++) = msglen & 0xff;
2743 *(p++) = msglen & 0xff;
2746 memcpy(p, tmp, msglen);
2747 /* Check if pubkey from client certificate was used */
2748 if (EVP_PKEY_CTX_ctrl
2749 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2750 /* Set flag "skip certificate verify" */
2751 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2753 EVP_PKEY_CTX_free(pkey_ctx);
2754 EVP_PKEY_free(pub_key);
2757 #ifndef OPENSSL_NO_SRP
2758 else if (alg_k & SSL_kSRP) {
2759 if (s->srp_ctx.A != NULL) {
2760 /* send off the data */
2761 n = BN_num_bytes(s->srp_ctx.A);
2763 BN_bn2bin(s->srp_ctx.A, p);
2766 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2767 ERR_R_INTERNAL_ERROR);
2770 OPENSSL_free(s->session->srp_username);
2771 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2772 if (s->session->srp_username == NULL) {
2773 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2774 ERR_R_MALLOC_FAILURE);
2779 #ifndef OPENSSL_NO_PSK
2780 else if (alg_k & SSL_kPSK) {
2782 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2783 * \0-terminated identity. The last byte is for us for simulating
2786 char identity[PSK_MAX_IDENTITY_LEN + 2];
2787 size_t identity_len;
2788 unsigned char *t = NULL;
2789 unsigned int psk_len = 0;
2793 if (s->psk_client_callback == NULL) {
2794 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2795 SSL_R_PSK_NO_CLIENT_CB);
2799 memset(identity, 0, sizeof(identity));
2800 /* Allocate maximum size buffer */
2801 pmslen = PSK_MAX_PSK_LEN * 2 + 4;
2802 pms = OPENSSL_malloc(pmslen);
2806 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2807 identity, sizeof(identity) - 1,
2809 if (psk_len > PSK_MAX_PSK_LEN) {
2810 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2811 ERR_R_INTERNAL_ERROR);
2813 } else if (psk_len == 0) {
2814 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2815 SSL_R_PSK_IDENTITY_NOT_FOUND);
2818 /* Change pmslen to real length */
2819 pmslen = 2 + psk_len + 2 + psk_len;
2820 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2821 identity_len = strlen(identity);
2822 if (identity_len > PSK_MAX_IDENTITY_LEN) {
2823 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2824 ERR_R_INTERNAL_ERROR);
2827 /* create PSK pre_master_secret */
2829 memmove(pms + psk_len + 4, pms, psk_len);
2831 memset(t, 0, psk_len);
2835 OPENSSL_free(s->session->psk_identity_hint);
2836 s->session->psk_identity_hint =
2837 BUF_strdup(s->ctx->psk_identity_hint);
2838 if (s->ctx->psk_identity_hint != NULL
2839 && s->session->psk_identity_hint == NULL) {
2840 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2841 ERR_R_MALLOC_FAILURE);
2845 OPENSSL_free(s->session->psk_identity);
2846 s->session->psk_identity = BUF_strdup(identity);
2847 if (s->session->psk_identity == NULL) {
2848 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2849 ERR_R_MALLOC_FAILURE);
2853 s2n(identity_len, p);
2854 memcpy(p, identity, identity_len);
2855 n = 2 + identity_len;
2858 OPENSSL_cleanse(identity, sizeof(identity));
2860 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2866 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2867 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2871 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2872 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2873 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2877 s->state = SSL3_ST_CW_KEY_EXCH_B;
2880 /* SSL3_ST_CW_KEY_EXCH_B */
2881 n = ssl_do_write(s);
2882 #ifndef OPENSSL_NO_SRP
2884 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2886 * If everything written generate master key: no need to save PMS as
2887 * SRP_generate_client_master_secret generates it internally.
2890 if ((s->session->master_key_length =
2891 SRP_generate_client_master_secret(s,
2892 s->session->master_key)) <
2894 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2895 ERR_R_INTERNAL_ERROR);
2901 /* If we haven't written everything save PMS */
2904 s->cert->pmslen = pmslen;
2906 /* If we don't have a PMS restore */
2909 pmslen = s->cert->pmslen;
2912 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2913 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2916 s->session->master_key_length =
2917 s->method->ssl3_enc->generate_master_secret(s,
2919 session->master_key,
2921 OPENSSL_clear_free(pms, pmslen);
2922 s->cert->pms = NULL;
2923 if (s->session->master_key_length < 0) {
2924 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2925 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2931 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2932 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2934 OPENSSL_clear_free(pms, pmslen);
2935 s->cert->pms = NULL;
2936 #ifndef OPENSSL_NO_EC
2937 BN_CTX_free(bn_ctx);
2938 OPENSSL_free(encodedPoint);
2939 EC_KEY_free(clnt_ecdh);
2940 EVP_PKEY_free(srvr_pub_pkey);
2942 s->state = SSL_ST_ERR;
2946 int ssl3_send_client_verify(SSL *s)
2949 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2951 EVP_PKEY_CTX *pctx = NULL;
2957 EVP_MD_CTX_init(&mctx);
2959 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2960 p = ssl_handshake_start(s);
2961 pkey = s->cert->key->privatekey;
2962 /* Create context from key and test if sha1 is allowed as digest */
2963 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2964 EVP_PKEY_sign_init(pctx);
2965 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2966 if (!SSL_USE_SIGALGS(s))
2967 s->method->ssl3_enc->cert_verify_mac(s,
2970 [MD5_DIGEST_LENGTH]));
2975 * For TLS v1.2 send signature algorithm and signature using agreed
2976 * digest and cached handshake records.
2978 if (SSL_USE_SIGALGS(s)) {
2981 const EVP_MD *md = s->cert->key->digest;
2982 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2983 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
2984 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2989 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
2992 if (!EVP_SignInit_ex(&mctx, md, NULL)
2993 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
2994 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2995 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3001 * For extended master secret we've already digested cached
3004 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
3005 BIO_free(s->s3->handshake_buffer);
3006 s->s3->handshake_buffer = NULL;
3007 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3008 } else if (!ssl3_digest_cached_records(s))
3011 #ifndef OPENSSL_NO_RSA
3012 if (pkey->type == EVP_PKEY_RSA) {
3013 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3014 if (RSA_sign(NID_md5_sha1, data,
3015 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3016 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3017 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3024 #ifndef OPENSSL_NO_DSA
3025 if (pkey->type == EVP_PKEY_DSA) {
3026 if (!DSA_sign(pkey->save_type,
3027 &(data[MD5_DIGEST_LENGTH]),
3028 SHA_DIGEST_LENGTH, &(p[2]),
3029 (unsigned int *)&j, pkey->pkey.dsa)) {
3030 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3037 #ifndef OPENSSL_NO_EC
3038 if (pkey->type == EVP_PKEY_EC) {
3039 if (!ECDSA_sign(pkey->save_type,
3040 &(data[MD5_DIGEST_LENGTH]),
3041 SHA_DIGEST_LENGTH, &(p[2]),
3042 (unsigned int *)&j, pkey->pkey.ec)) {
3043 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3050 if (pkey->type == NID_id_GostR3410_94
3051 || pkey->type == NID_id_GostR3410_2001) {
3052 unsigned char signbuf[64];
3054 size_t sigsize = 64;
3055 s->method->ssl3_enc->cert_verify_mac(s,
3056 NID_id_GostR3411_94, data);
3057 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3058 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3061 for (i = 63, j = 0; i >= 0; j++, i--) {
3062 p[2 + j] = signbuf[i];
3067 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3070 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3071 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3074 s->state = SSL3_ST_CW_CERT_VRFY_B;
3076 EVP_MD_CTX_cleanup(&mctx);
3077 EVP_PKEY_CTX_free(pctx);
3078 return ssl_do_write(s);
3080 EVP_MD_CTX_cleanup(&mctx);
3081 EVP_PKEY_CTX_free(pctx);
3082 s->state = SSL_ST_ERR;
3087 * Check a certificate can be used for client authentication. Currently check
3088 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3089 * certificates can be used and optionally checks suitability for Suite B.
3091 static int ssl3_check_client_certificate(SSL *s)
3093 unsigned long alg_k;
3094 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3096 /* If no suitable signature algorithm can't use certificate */
3097 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3100 * If strict mode check suitability of chain before using it. This also
3101 * adjusts suite B digest if necessary.
3103 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3104 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3106 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3107 /* See if we can use client certificate for fixed DH */
3108 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3109 SESS_CERT *scert = s->session->sess_cert;
3110 int i = scert->peer_cert_type;
3111 EVP_PKEY *clkey = NULL, *spkey = NULL;
3112 clkey = s->cert->key->privatekey;
3113 /* If client key not DH assume it can be used */
3114 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3117 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3119 /* Compare server and client parameters */
3120 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3121 EVP_PKEY_free(spkey);
3125 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3130 int ssl3_send_client_certificate(SSL *s)
3133 EVP_PKEY *pkey = NULL;
3136 if (s->state == SSL3_ST_CW_CERT_A) {
3137 /* Let cert callback update client certificates if required */
3138 if (s->cert->cert_cb) {
3139 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3141 s->rwstate = SSL_X509_LOOKUP;
3145 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3146 s->state = SSL_ST_ERR;
3149 s->rwstate = SSL_NOTHING;
3151 if (ssl3_check_client_certificate(s))
3152 s->state = SSL3_ST_CW_CERT_C;
3154 s->state = SSL3_ST_CW_CERT_B;
3157 /* We need to get a client cert */
3158 if (s->state == SSL3_ST_CW_CERT_B) {
3160 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3161 * return(-1); We then get retied later
3164 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3166 s->rwstate = SSL_X509_LOOKUP;
3169 s->rwstate = SSL_NOTHING;
3170 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3171 s->state = SSL3_ST_CW_CERT_B;
3172 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3174 } else if (i == 1) {
3176 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3177 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3181 EVP_PKEY_free(pkey);
3182 if (i && !ssl3_check_client_certificate(s))
3185 if (s->version == SSL3_VERSION) {
3186 s->s3->tmp.cert_req = 0;
3187 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3190 s->s3->tmp.cert_req = 2;
3191 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3192 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3193 s->state = SSL_ST_ERR;
3199 /* Ok, we have a cert */
3200 s->state = SSL3_ST_CW_CERT_C;
3203 if (s->state == SSL3_ST_CW_CERT_C) {
3204 s->state = SSL3_ST_CW_CERT_D;
3205 if (!ssl3_output_cert_chain(s,
3206 (s->s3->tmp.cert_req ==
3207 2) ? NULL : s->cert->key)) {
3208 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3209 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3210 s->state = SSL_ST_ERR;
3214 /* SSL3_ST_CW_CERT_D */
3215 return ssl_do_write(s);
3218 #define has_bits(i,m) (((i)&(m)) == (m))
3220 int ssl3_check_cert_and_algorithm(SSL *s)
3224 EVP_PKEY *pkey = NULL;
3226 #ifndef OPENSSL_NO_RSA
3229 #ifndef OPENSSL_NO_DH
3233 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3234 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3236 /* we don't have a certificate */
3237 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3240 sc = s->session->sess_cert;
3242 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3245 #ifndef OPENSSL_NO_RSA
3246 rsa = s->session->sess_cert->peer_rsa_tmp;
3248 #ifndef OPENSSL_NO_DH
3249 dh = s->session->sess_cert->peer_dh_tmp;
3252 /* This is the passed certificate */
3254 idx = sc->peer_cert_type;
3255 #ifndef OPENSSL_NO_EC
3256 if (idx == SSL_PKEY_ECC) {
3257 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3259 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3264 } else if (alg_a & SSL_aECDSA) {
3265 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3266 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3268 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3269 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3273 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3274 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3275 EVP_PKEY_free(pkey);
3277 /* Check that we have a certificate if we require one */
3278 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3279 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3280 SSL_R_MISSING_RSA_SIGNING_CERT);
3283 #ifndef OPENSSL_NO_DSA
3284 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3285 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3286 SSL_R_MISSING_DSA_SIGNING_CERT);
3290 #ifndef OPENSSL_NO_RSA
3291 if ((alg_k & SSL_kRSA) &&
3292 !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) {
3293 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3294 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3298 #ifndef OPENSSL_NO_DH
3299 if ((alg_k & SSL_kDHE) &&
3300 !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) {
3301 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY);
3303 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3304 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3305 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3306 SSL_R_MISSING_DH_RSA_CERT);
3309 # ifndef OPENSSL_NO_DSA
3310 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3311 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3312 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3313 SSL_R_MISSING_DH_DSA_CERT);
3319 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) {
3320 #ifndef OPENSSL_NO_RSA
3321 if (alg_k & SSL_kRSA) {
3323 || RSA_size(rsa) * 8 >
3324 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3325 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3326 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3331 #ifndef OPENSSL_NO_DH
3332 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3334 || DH_size(dh) * 8 >
3335 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3336 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3337 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3343 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3344 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3350 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3355 #ifndef OPENSSL_NO_TLSEXT
3357 * Normally, we can tell if the server is resuming the session from
3358 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3359 * message after the ServerHello to determine if the server is resuming.
3360 * Therefore, we allow EAP-FAST to peek ahead.
3361 * ssl3_check_finished returns 1 if we are resuming from an external
3362 * pre-shared secret, we have a "ticket" and the next server handshake message
3363 * is Finished; and 0 otherwise. It returns -1 upon an error.
3365 static int ssl3_check_finished(SSL *s)
3369 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3370 !s->session->tlsext_tick)
3373 /* Need to permit this temporarily, in case the next message is Finished. */
3374 s->s3->flags |= SSL3_FLAGS_CCS_OK;
3376 * This function is called when we might get a Certificate message instead,
3377 * so permit appropriate message length.
3378 * We ignore the return value as we're only interested in the message type
3379 * and not its length.
3381 s->method->ssl_get_message(s,
3384 -1, s->max_cert_list, &ok);
3385 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3390 s->s3->tmp.reuse_message = 1;
3392 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3395 /* If we're not done, then the CCS arrived early and we should bail. */
3396 if (s->s3->change_cipher_spec) {
3397 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3398 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3405 # ifndef OPENSSL_NO_NEXTPROTONEG
3406 int ssl3_send_next_proto(SSL *s)
3408 unsigned int len, padding_len;
3411 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3412 len = s->next_proto_negotiated_len;
3413 padding_len = 32 - ((len + 2) % 32);
3414 d = (unsigned char *)s->init_buf->data;
3416 memcpy(d + 5, s->next_proto_negotiated, len);
3417 d[5 + len] = padding_len;
3418 memset(d + 6 + len, 0, padding_len);
3419 *(d++) = SSL3_MT_NEXT_PROTO;
3420 l2n3(2 + len + padding_len, d);
3421 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3422 s->init_num = 4 + 2 + len + padding_len;
3426 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3431 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3434 #ifndef OPENSSL_NO_ENGINE
3435 if (s->ctx->client_cert_engine) {
3436 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3437 SSL_get_client_CA_list(s),
3438 px509, ppkey, NULL, NULL, NULL);
3443 if (s->ctx->client_cert_cb)
3444 i = s->ctx->client_cert_cb(s, px509, ppkey);