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 ssl_set_version(SSL *s);
167 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
168 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
172 * Work out what version we should be using for the initial ClientHello if
173 * the version is currently set to (D)TLS_ANY_VERSION.
174 * Returns 1 on success
177 static int ssl_set_version(SSL *s)
179 unsigned long mask, options = s->options;
181 if (s->method->version == TLS_ANY_VERSION) {
183 * SSL_OP_NO_X disables all protocols above X *if* there are
184 * some protocols below X enabled. This is required in order
185 * to maintain "version capability" vector contiguous. So
186 * that if application wants to disable TLS1.0 in favour of
187 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
188 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
190 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
191 #if !defined(OPENSSL_NO_SSL3)
195 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
196 if (options & SSL_OP_NO_TLSv1_2) {
197 if ((options & mask) != mask) {
198 s->version = TLS1_1_VERSION;
200 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
204 s->version = TLS1_2_VERSION;
207 if ((options & mask) == mask) {
208 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
211 s->version = TLS1_1_VERSION;
214 mask &= ~SSL_OP_NO_TLSv1_1;
215 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
216 s->version = TLS1_VERSION;
217 mask &= ~SSL_OP_NO_TLSv1;
218 #if !defined(OPENSSL_NO_SSL3)
219 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
220 s->version = SSL3_VERSION;
223 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
224 SSLerr(SSL_F_SSL_SET_VERSION,
225 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
229 if (s->version == SSL3_VERSION && FIPS_mode()) {
230 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
234 } else if (s->method->version == DTLS_ANY_VERSION) {
235 /* Determine which DTLS version to use */
236 /* If DTLS 1.2 disabled correct the version number */
237 if (options & SSL_OP_NO_DTLSv1_2) {
238 if (tls1_suiteb(s)) {
239 SSLerr(SSL_F_SSL_SET_VERSION,
240 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
244 * Disabling all versions is silly: return an error.
246 if (options & SSL_OP_NO_DTLSv1) {
247 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
251 * Update method so we don't use any DTLS 1.2 features.
253 s->method = DTLSv1_client_method();
254 s->version = DTLS1_VERSION;
257 * We only support one version: update method
259 if (options & SSL_OP_NO_DTLSv1)
260 s->method = DTLSv1_2_client_method();
261 s->version = DTLS1_2_VERSION;
265 s->client_version = s->version;
270 int ssl3_client_hello(SSL *s)
272 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
273 if(tls_construct_client_hello(s) == 0)
276 s->state = SSL3_ST_CW_CLNT_HELLO_B;
279 /* SSL3_ST_CW_CLNT_HELLO_B */
280 return ssl_do_write(s);
282 s->state = SSL_ST_ERR;
287 int tls_construct_client_hello(SSL *s)
290 unsigned char *p, *d;
294 #ifndef OPENSSL_NO_COMP
298 SSL_SESSION *sess = s->session;
300 buf = (unsigned char *)s->init_buf->data;
302 /* Work out what SSL/TLS/DTLS version to use */
303 if (ssl_set_version(s) == 0)
306 if ((sess == NULL) || (sess->ssl_version != s->version) ||
308 * In the case of EAP-FAST, we can have a pre-shared
309 * "ticket" without a session ID.
311 (!sess->session_id_length && !sess->tlsext_tick) ||
312 (sess->not_resumable)) {
313 if (!ssl_get_new_session(s, 0))
316 /* else use the pre-loaded session */
318 p = s->s3->client_random;
321 * for DTLS if client_random is initialized, reuse it, we are
322 * required to use same upon reply to HelloVerify
324 if (SSL_IS_DTLS(s)) {
327 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
336 if (i && ssl_fill_hello_random(s, 0, p,
337 sizeof(s->s3->client_random)) <= 0)
340 /* Do the message type and length last */
341 d = p = ssl_handshake_start(s);
344 * version indicates the negotiated version: for example from
345 * an SSLv2/v3 compatible client hello). The client_version
346 * field is the maximum version we permit and it is also
347 * used in RSA encrypted premaster secrets. Some servers can
348 * choke if we initially report a higher version then
349 * renegotiate to a lower one in the premaster secret. This
350 * didn't happen with TLS 1.0 as most servers supported it
351 * but it can with TLS 1.1 or later if the server only supports
354 * Possible scenario with previous logic:
355 * 1. Client hello indicates TLS 1.2
356 * 2. Server hello says TLS 1.0
357 * 3. RSA encrypted premaster secret uses 1.2.
358 * 4. Handhaked proceeds using TLS 1.0.
359 * 5. Server sends hello request to renegotiate.
360 * 6. Client hello indicates TLS v1.0 as we now
361 * know that is maximum server supports.
362 * 7. Server chokes on RSA encrypted premaster secret
363 * containing version 1.0.
365 * For interoperability it should be OK to always use the
366 * maximum version we support in client hello and then rely
367 * on the checking of version to ensure the servers isn't
368 * being inconsistent: for example initially negotiating with
369 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
370 * client_version in client hello and not resetting it to
371 * the negotiated version.
373 *(p++) = s->client_version >> 8;
374 *(p++) = s->client_version & 0xff;
377 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
378 p += SSL3_RANDOM_SIZE;
384 i = s->session->session_id_length;
387 if (i > (int)sizeof(s->session->session_id)) {
388 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
391 memcpy(p, s->session->session_id, i);
395 /* cookie stuff for DTLS */
396 if (SSL_IS_DTLS(s)) {
397 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
398 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
401 *(p++) = s->d1->cookie_len;
402 memcpy(p, s->d1->cookie, s->d1->cookie_len);
403 p += s->d1->cookie_len;
406 /* Ciphers supported */
407 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
409 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
412 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
414 * Some servers hang if client hello > 256 bytes as hack workaround
415 * chop number of supported ciphers to keep it well below this if we
418 if (TLS1_get_version(s) >= TLS1_2_VERSION
419 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
420 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
426 #ifdef OPENSSL_NO_COMP
430 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
433 j = sk_SSL_COMP_num(s->ctx->comp_methods);
435 for (i = 0; i < j; i++) {
436 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
440 *(p++) = 0; /* Add the NULL method */
443 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
444 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
448 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
450 ssl3_send_alert(s, SSL3_AL_FATAL, al);
451 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
456 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
457 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
458 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
468 int ssl3_get_server_hello(SSL *s)
473 * Hello verify request and/or server hello version may not match so set
474 * first packet if we're negotiating version.
478 n = s->method->ssl_get_message(s,
479 SSL3_ST_CR_SRVR_HELLO_A,
480 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
486 if (SSL_IS_DTLS(s)) {
487 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
488 if (s->d1->send_cookie == 0) {
489 s->s3->tmp.reuse_message = 1;
491 } else { /* already sent a cookie */
493 al = SSL_AD_UNEXPECTED_MESSAGE;
494 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
500 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
501 al = SSL_AD_UNEXPECTED_MESSAGE;
502 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
506 return tls_process_server_hello(s, (unsigned long)n);
508 ssl3_send_alert(s, SSL3_AL_FATAL, al);
509 s->state = SSL_ST_ERR;
513 enum MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, unsigned long n)
515 STACK_OF(SSL_CIPHER) *sk;
517 PACKET pkt, session_id;
518 size_t session_id_len;
519 unsigned char *cipherchars;
520 int i, al = SSL_AD_INTERNAL_ERROR;
521 unsigned int compression;
522 #ifndef OPENSSL_NO_COMP
526 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
527 al = SSL_AD_INTERNAL_ERROR;
528 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
532 if (s->method->version == TLS_ANY_VERSION) {
533 unsigned int sversion;
535 if (!PACKET_get_net_2(&pkt, &sversion)) {
536 al = SSL_AD_DECODE_ERROR;
537 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
541 #if TLS_MAX_VERSION != TLS1_2_VERSION
542 #error Code needs updating for new TLS version
544 #ifndef OPENSSL_NO_SSL3
545 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
547 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
548 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
549 al = SSL_AD_PROTOCOL_VERSION;
552 s->method = SSLv3_client_method();
555 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
556 s->method = TLSv1_client_method();
557 } else if ((sversion == TLS1_1_VERSION) &&
558 !(s->options & SSL_OP_NO_TLSv1_1)) {
559 s->method = TLSv1_1_client_method();
560 } else if ((sversion == TLS1_2_VERSION) &&
561 !(s->options & SSL_OP_NO_TLSv1_2)) {
562 s->method = TLSv1_2_client_method();
564 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
565 al = SSL_AD_PROTOCOL_VERSION;
568 s->session->ssl_version = s->version = s->method->version;
570 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
571 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
572 al = SSL_AD_PROTOCOL_VERSION;
575 } else if (s->method->version == DTLS_ANY_VERSION) {
576 /* Work out correct protocol version to use */
577 unsigned int hversion;
580 if (!PACKET_get_net_2(&pkt, &hversion)) {
581 al = SSL_AD_DECODE_ERROR;
582 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
586 options = s->options;
587 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
588 s->method = DTLSv1_2_client_method();
589 else if (tls1_suiteb(s)) {
590 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
591 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
592 s->version = hversion;
593 al = SSL_AD_PROTOCOL_VERSION;
595 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
596 s->method = DTLSv1_client_method();
598 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
599 s->version = hversion;
600 al = SSL_AD_PROTOCOL_VERSION;
603 s->session->ssl_version = s->version = s->method->version;
607 if (!PACKET_get_bytes(&pkt, &vers, 2)) {
608 al = SSL_AD_DECODE_ERROR;
609 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
612 if ((vers[0] != (s->version >> 8))
613 || (vers[1] != (s->version & 0xff))) {
614 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
615 s->version = (s->version & 0xff00) | vers[1];
616 al = SSL_AD_PROTOCOL_VERSION;
621 /* load the server hello data */
622 /* load the server random */
623 if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
624 al = SSL_AD_DECODE_ERROR;
625 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
631 /* Get the session-id. */
632 if (!PACKET_get_length_prefixed_1(&pkt, &session_id)) {
633 al = SSL_AD_DECODE_ERROR;
634 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
637 session_id_len = PACKET_remaining(&session_id);
638 if (session_id_len > sizeof s->session->session_id
639 || session_id_len > SSL3_SESSION_ID_SIZE) {
640 al = SSL_AD_ILLEGAL_PARAMETER;
641 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
645 if (!PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {
646 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
647 al = SSL_AD_DECODE_ERROR;
652 * Check if we can resume the session based on external pre-shared secret.
653 * EAP-FAST (RFC 4851) supports two types of session resumption.
654 * Resumption based on server-side state works with session IDs.
655 * Resumption based on pre-shared Protected Access Credentials (PACs)
656 * works by overriding the SessionTicket extension at the application
657 * layer, and does not send a session ID. (We do not know whether EAP-FAST
658 * servers would honour the session ID.) Therefore, the session ID alone
659 * is not a reliable indicator of session resumption, so we first check if
660 * we can resume, and later peek at the next handshake message to see if the
661 * server wants to resume.
663 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
664 s->session->tlsext_tick) {
665 SSL_CIPHER *pref_cipher = NULL;
666 s->session->master_key_length = sizeof(s->session->master_key);
667 if (s->tls_session_secret_cb(s, s->session->master_key,
668 &s->session->master_key_length,
670 s->tls_session_secret_cb_arg)) {
671 s->session->cipher = pref_cipher ?
672 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
674 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
675 al = SSL_AD_INTERNAL_ERROR;
680 if (session_id_len != 0 && session_id_len == s->session->session_id_length
681 && memcmp(PACKET_data(&session_id), s->session->session_id,
682 session_id_len) == 0) {
683 if (s->sid_ctx_length != s->session->sid_ctx_length
684 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
685 /* actually a client application bug */
686 al = SSL_AD_ILLEGAL_PARAMETER;
687 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
688 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
694 * If we were trying for session-id reuse but the server
695 * didn't echo the ID, make a new SSL_SESSION.
696 * In the case of EAP-FAST and PAC, we do not send a session ID,
697 * so the PAC-based session secret is always preserved. It'll be
698 * overwritten if the server refuses resumption.
700 if (s->session->session_id_length > 0) {
701 if (!ssl_get_new_session(s, 0)) {
706 s->session->session_id_length = session_id_len;
707 /* session_id_len could be 0 */
708 memcpy(s->session->session_id, PACKET_data(&session_id),
712 c = ssl_get_cipher_by_char(s, cipherchars);
715 al = SSL_AD_ILLEGAL_PARAMETER;
716 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
719 /* Set version disabled mask now we know version */
720 if (!SSL_USE_TLS1_2_CIPHERS(s))
721 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
723 s->s3->tmp.mask_ssl = 0;
725 * If it is a disabled cipher we didn't send it in client hello, so
728 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
729 al = SSL_AD_ILLEGAL_PARAMETER;
730 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
734 sk = ssl_get_ciphers_by_id(s);
735 i = sk_SSL_CIPHER_find(sk, c);
737 /* we did not say we would use this cipher */
738 al = SSL_AD_ILLEGAL_PARAMETER;
739 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
744 * Depending on the session caching (internal/external), the cipher
745 * and/or cipher_id values may not be set. Make sure that cipher_id is
746 * set and use it for comparison.
748 if (s->session->cipher)
749 s->session->cipher_id = s->session->cipher->id;
750 if (s->hit && (s->session->cipher_id != c->id)) {
751 al = SSL_AD_ILLEGAL_PARAMETER;
752 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
753 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
756 s->s3->tmp.new_cipher = c;
758 * Don't digest cached records if no sigalgs: we may need them for client
761 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
763 /* lets get the compression algorithm */
765 if (!PACKET_get_1(&pkt, &compression)) {
766 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
767 al = SSL_AD_DECODE_ERROR;
770 #ifdef OPENSSL_NO_COMP
771 if (compression != 0) {
772 al = SSL_AD_ILLEGAL_PARAMETER;
773 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
774 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
778 * If compression is disabled we'd better not try to resume a session
781 if (s->session->compress_meth != 0) {
782 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
786 if (s->hit && compression != s->session->compress_meth) {
787 al = SSL_AD_ILLEGAL_PARAMETER;
788 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
789 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
792 if (compression == 0)
794 else if (!ssl_allow_compression(s)) {
795 al = SSL_AD_ILLEGAL_PARAMETER;
796 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
799 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
802 if (compression != 0 && comp == NULL) {
803 al = SSL_AD_ILLEGAL_PARAMETER;
804 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
805 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
808 s->s3->tmp.new_compression = comp;
813 if (!ssl_parse_serverhello_tlsext(s, &pkt)) {
814 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
818 if (PACKET_remaining(&pkt) != 0) {
819 /* wrong packet length */
820 al = SSL_AD_DECODE_ERROR;
821 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
825 #ifndef OPENSSL_NO_SCTP
826 if (SSL_IS_DTLS(s) && s->hit) {
827 unsigned char sctpauthkey[64];
828 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
831 * Add new shared key for SCTP-Auth, will be ignored if
834 snprintf((char *)labelbuffer,
835 sizeof(DTLS1_SCTP_AUTH_LABEL),
836 DTLS1_SCTP_AUTH_LABEL);
838 if (SSL_export_keying_material(s, sctpauthkey,
841 sizeof(labelbuffer), NULL, 0,
845 BIO_ctrl(SSL_get_wbio(s),
846 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
847 sizeof(sctpauthkey), sctpauthkey);
851 return MSG_PROCESS_CONTINUE_READING;
853 ssl3_send_alert(s, SSL3_AL_FATAL, al);
856 return MSG_PROCESS_ERROR;
859 int ssl3_get_server_certificate(SSL *s)
864 n = s->method->ssl_get_message(s,
867 -1, s->max_cert_list, &ok);
872 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
873 s->s3->tmp.reuse_message = 1;
877 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
878 al = SSL_AD_UNEXPECTED_MESSAGE;
879 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
883 return tls_process_server_certificate(s, (unsigned long)n);
885 ssl3_send_alert(s, SSL3_AL_FATAL, al);
886 s->state = SSL_ST_ERR;
890 enum MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, unsigned long n)
892 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
893 unsigned long cert_list_len, cert_len;
895 unsigned char *certstart, *certbytes;
896 STACK_OF(X509) *sk = NULL;
897 EVP_PKEY *pkey = NULL;
900 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
901 al = SSL_AD_INTERNAL_ERROR;
902 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
906 if ((sk = sk_X509_new_null()) == NULL) {
907 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
911 if (!PACKET_get_net_3(&pkt, &cert_list_len)
912 || PACKET_remaining(&pkt) != cert_list_len) {
913 al = SSL_AD_DECODE_ERROR;
914 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
917 while (PACKET_remaining(&pkt)) {
918 if (!PACKET_get_net_3(&pkt, &cert_len)
919 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
920 al = SSL_AD_DECODE_ERROR;
921 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
922 SSL_R_CERT_LENGTH_MISMATCH);
926 certstart = certbytes;
927 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
929 al = SSL_AD_BAD_CERTIFICATE;
930 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
933 if (certbytes != (certstart + cert_len)) {
934 al = SSL_AD_DECODE_ERROR;
935 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
936 SSL_R_CERT_LENGTH_MISMATCH);
939 if (!sk_X509_push(sk, x)) {
940 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
946 i = ssl_verify_cert_chain(s, sk);
947 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
948 al = ssl_verify_alarm_type(s->verify_result);
949 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
950 SSL_R_CERTIFICATE_VERIFY_FAILED);
953 ERR_clear_error(); /* but we keep s->verify_result */
955 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
956 al = SSL_AD_HANDSHAKE_FAILURE;
960 s->session->peer_chain = sk;
962 * Inconsistency alert: cert_chain does include the peer's certificate,
963 * which we don't include in s3_srvr.c
965 x = sk_X509_value(sk, 0);
968 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
971 pkey = X509_get_pubkey(x);
973 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
976 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
977 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
981 i = ssl_cert_type(x, pkey);
985 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
986 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
990 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
991 if (exp_idx >= 0 && i != exp_idx) {
993 al = SSL_AD_ILLEGAL_PARAMETER;
994 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
995 SSL_R_WRONG_CERTIFICATE_TYPE);
998 s->session->peer_type = i;
1000 X509_free(s->session->peer);
1002 s->session->peer = x;
1003 s->session->verify_result = s->verify_result;
1006 ret = MSG_PROCESS_CONTINUE_READING;
1010 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1012 statem_set_error(s);
1014 EVP_PKEY_free(pkey);
1016 sk_X509_pop_free(sk, X509_free);
1020 int ssl3_get_key_exchange(SSL *s)
1027 * use same message size as in ssl3_get_certificate_request() as
1028 * ServerKeyExchange message may be skipped
1030 n = s->method->ssl_get_message(s,
1031 SSL3_ST_CR_KEY_EXCH_A,
1032 SSL3_ST_CR_KEY_EXCH_B,
1033 -1, s->max_cert_list, &ok);
1037 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1039 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1041 * Can't skip server key exchange if this is an ephemeral
1044 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
1045 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1046 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1050 s->s3->tmp.reuse_message = 1;
1054 return tls_process_key_exchange(s, (unsigned long)n);
1057 enum MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, unsigned long n)
1059 #ifndef OPENSSL_NO_RSA
1060 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1063 int al, j, verify_ret;
1065 EVP_PKEY *pkey = NULL;
1066 const EVP_MD *md = NULL;
1067 #ifndef OPENSSL_NO_RSA
1070 #ifndef OPENSSL_NO_DH
1073 #ifndef OPENSSL_NO_EC
1074 EC_KEY *ecdh = NULL;
1075 BN_CTX *bn_ctx = NULL;
1076 EC_POINT *srvr_ecpoint = NULL;
1079 PACKET pkt, save_param_start, signature;
1081 EVP_MD_CTX_init(&md_ctx);
1083 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1085 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1086 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1087 al = SSL_AD_INTERNAL_ERROR;
1090 save_param_start = pkt;
1092 #ifndef OPENSSL_NO_RSA
1093 RSA_free(s->s3->peer_rsa_tmp);
1094 s->s3->peer_rsa_tmp = NULL;
1096 #ifndef OPENSSL_NO_DH
1097 DH_free(s->s3->peer_dh_tmp);
1098 s->s3->peer_dh_tmp = NULL;
1100 #ifndef OPENSSL_NO_EC
1101 EC_KEY_free(s->s3->peer_ecdh_tmp);
1102 s->s3->peer_ecdh_tmp = NULL;
1105 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1107 al = SSL_AD_DECODE_ERROR;
1109 #ifndef OPENSSL_NO_PSK
1110 /* PSK ciphersuites are preceded by an identity hint */
1111 if (alg_k & SSL_PSK) {
1112 PACKET psk_identity_hint;
1113 if (!PACKET_get_length_prefixed_2(&pkt, &psk_identity_hint)) {
1114 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1119 * Store PSK identity hint for later use, hint is used in
1120 * ssl3_send_client_key_exchange. Assume that the maximum length of
1121 * a PSK identity hint can be as long as the maximum length of a PSK
1124 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1125 al = SSL_AD_HANDSHAKE_FAILURE;
1126 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1130 if (!PACKET_strndup(&psk_identity_hint,
1131 &s->session->psk_identity_hint)) {
1132 al = SSL_AD_INTERNAL_ERROR;
1137 /* Nothing else to do for plain PSK or RSAPSK */
1138 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1140 #endif /* !OPENSSL_NO_PSK */
1141 #ifndef OPENSSL_NO_SRP
1142 if (alg_k & SSL_kSRP) {
1143 PACKET prime, generator, salt, server_pub;
1144 if (!PACKET_get_length_prefixed_2(&pkt, &prime)
1145 || !PACKET_get_length_prefixed_2(&pkt, &generator)
1146 || !PACKET_get_length_prefixed_1(&pkt, &salt)
1147 || !PACKET_get_length_prefixed_2(&pkt, &server_pub)) {
1148 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1153 BN_bin2bn(PACKET_data(&prime),
1154 PACKET_remaining(&prime), NULL)) == NULL
1156 BN_bin2bn(PACKET_data(&generator),
1157 PACKET_remaining(&generator), NULL)) == NULL
1159 BN_bin2bn(PACKET_data(&salt),
1160 PACKET_remaining(&salt), NULL)) == NULL
1162 BN_bin2bn(PACKET_data(&server_pub),
1163 PACKET_remaining(&server_pub), NULL)) == NULL) {
1164 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1168 if (!srp_verify_server_param(s, &al)) {
1169 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1173 /* We must check if there is a certificate */
1174 if (alg_a & (SSL_aRSA|SSL_aDSS))
1175 pkey = X509_get_pubkey(s->session->peer);
1177 #endif /* !OPENSSL_NO_SRP */
1178 #ifndef OPENSSL_NO_RSA
1179 if (alg_k & SSL_kRSA) {
1181 /* Temporary RSA keys only allowed in export ciphersuites */
1182 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1183 al = SSL_AD_UNEXPECTED_MESSAGE;
1184 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1188 if (!PACKET_get_length_prefixed_2(&pkt, &mod)
1189 || !PACKET_get_length_prefixed_2(&pkt, &exp)) {
1190 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1194 if ((rsa = RSA_new()) == NULL) {
1195 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1199 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1201 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1203 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1207 /* this should be because we are using an export cipher */
1208 if (alg_a & SSL_aRSA)
1209 pkey = X509_get_pubkey(s->session->peer);
1211 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1215 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1216 al = SSL_AD_UNEXPECTED_MESSAGE;
1217 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1221 s->s3->peer_rsa_tmp = rsa;
1224 #else /* OPENSSL_NO_RSA */
1227 #ifndef OPENSSL_NO_DH
1228 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1229 PACKET prime, generator, pub_key;
1231 if (!PACKET_get_length_prefixed_2(&pkt, &prime)
1232 || !PACKET_get_length_prefixed_2(&pkt, &generator)
1233 || !PACKET_get_length_prefixed_2(&pkt, &pub_key)) {
1234 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1238 if ((dh = DH_new()) == NULL) {
1239 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1243 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1244 PACKET_remaining(&prime), NULL)) == NULL
1245 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1246 PACKET_remaining(&generator), NULL)) == NULL
1248 BN_bin2bn(PACKET_data(&pub_key),
1249 PACKET_remaining(&pub_key), NULL)) == NULL) {
1250 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1254 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1255 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1259 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1260 al = SSL_AD_HANDSHAKE_FAILURE;
1261 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1264 if (alg_a & (SSL_aRSA|SSL_aDSS))
1265 pkey = X509_get_pubkey(s->session->peer);
1266 /* else anonymous DH, so no certificate or pkey. */
1268 s->s3->peer_dh_tmp = dh;
1271 #endif /* !OPENSSL_NO_DH */
1273 #ifndef OPENSSL_NO_EC
1274 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1276 const EC_GROUP *group;
1278 unsigned char *ecparams;
1280 if ((ecdh = EC_KEY_new()) == NULL) {
1281 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1286 * Extract elliptic curve parameters and the server's ephemeral ECDH
1287 * public key. For now we only support named (not generic) curves and
1288 * ECParameters in this case is just three bytes.
1290 if (!PACKET_get_bytes(&pkt, &ecparams, 3)) {
1291 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1295 * Check curve is one of our preferences, if not server has sent an
1296 * invalid curve. ECParameters is 3 bytes.
1298 if (!tls1_check_curve(s, ecparams, 3)) {
1299 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1303 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
1304 al = SSL_AD_INTERNAL_ERROR;
1305 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1306 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1310 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1311 if (ngroup == NULL) {
1312 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1315 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1316 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1319 EC_GROUP_free(ngroup);
1321 group = EC_KEY_get0_group(ecdh);
1323 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1324 (EC_GROUP_get_degree(group) > 163)) {
1325 al = SSL_AD_EXPORT_RESTRICTION;
1326 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1327 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1331 /* Next, get the encoded ECPoint */
1332 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1333 ((bn_ctx = BN_CTX_new()) == NULL)) {
1334 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1338 if (!PACKET_get_length_prefixed_1(&pkt, &encoded_pt)) {
1339 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1343 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1344 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
1345 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1350 * The ECC/TLS specification does not mention the use of DSA to sign
1351 * ECParameters in the server key exchange message. We do support RSA
1355 # ifndef OPENSSL_NO_RSA
1356 else if (alg_a & SSL_aRSA)
1357 pkey = X509_get_pubkey(s->session->peer);
1359 # ifndef OPENSSL_NO_EC
1360 else if (alg_a & SSL_aECDSA)
1361 pkey = X509_get_pubkey(s->session->peer);
1363 /* else anonymous ECDH, so no certificate or pkey. */
1364 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1365 s->s3->peer_ecdh_tmp = ecdh;
1367 BN_CTX_free(bn_ctx);
1369 EC_POINT_free(srvr_ecpoint);
1370 srvr_ecpoint = NULL;
1372 al = SSL_AD_UNEXPECTED_MESSAGE;
1373 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1376 #endif /* !OPENSSL_NO_EC */
1378 /* if it was signed, check the signature */
1382 * |pkt| now points to the beginning of the signature, so the difference
1383 * equals the length of the parameters.
1385 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1386 PACKET_remaining(&save_param_start) -
1387 PACKET_remaining(&pkt))) {
1388 al = SSL_AD_INTERNAL_ERROR;
1389 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1393 if (SSL_USE_SIGALGS(s)) {
1394 unsigned char *sigalgs;
1396 if (!PACKET_get_bytes(&pkt, &sigalgs, 2)) {
1397 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1400 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1407 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1413 if (!PACKET_get_length_prefixed_2(&pkt, &signature)
1414 || PACKET_remaining(&pkt) != 0) {
1415 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1418 j = EVP_PKEY_size(pkey);
1420 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1425 * Check signature length
1427 if (PACKET_remaining(&signature) > (size_t)j) {
1428 /* wrong packet length */
1429 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1432 #ifndef OPENSSL_NO_RSA
1433 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1439 for (num = 2; num > 0; num--) {
1440 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1441 EVP_DigestInit_ex(&md_ctx, (num == 2)
1442 ? s->ctx->md5 : s->ctx->sha1, NULL);
1443 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1445 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1447 EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms),
1448 PACKET_remaining(¶ms));
1449 EVP_DigestFinal_ex(&md_ctx, q, &size);
1454 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1455 PACKET_remaining(&signature), pkey->pkey.rsa);
1456 if (verify_ret < 0) {
1457 al = SSL_AD_DECRYPT_ERROR;
1458 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1461 if (verify_ret == 0) {
1463 al = SSL_AD_DECRYPT_ERROR;
1464 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1470 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1471 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1473 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1475 EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms),
1476 PACKET_remaining(¶ms));
1477 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1478 PACKET_remaining(&signature), pkey) <= 0) {
1480 al = SSL_AD_DECRYPT_ERROR;
1481 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1486 /* aNULL, aSRP or PSK do not need public keys */
1487 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1488 /* Might be wrong key type, check it */
1489 if (ssl3_check_cert_and_algorithm(s))
1490 /* Otherwise this shouldn't happen */
1491 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1494 /* still data left over */
1495 if (PACKET_remaining(&pkt) != 0) {
1496 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1500 EVP_PKEY_free(pkey);
1501 EVP_MD_CTX_cleanup(&md_ctx);
1502 return MSG_PROCESS_CONTINUE_READING;
1504 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1506 EVP_PKEY_free(pkey);
1507 #ifndef OPENSSL_NO_RSA
1510 #ifndef OPENSSL_NO_DH
1513 #ifndef OPENSSL_NO_EC
1514 BN_CTX_free(bn_ctx);
1515 EC_POINT_free(srvr_ecpoint);
1518 EVP_MD_CTX_cleanup(&md_ctx);
1519 statem_set_error(s);
1520 return MSG_PROCESS_ERROR;
1523 int ssl3_get_certificate_request(SSL *s)
1528 n = s->method->ssl_get_message(s,
1529 SSL3_ST_CR_CERT_REQ_A,
1530 SSL3_ST_CR_CERT_REQ_B,
1531 -1, s->max_cert_list, &ok);
1536 s->s3->tmp.cert_req = 0;
1538 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1539 s->s3->tmp.reuse_message = 1;
1541 * If we get here we don't need any cached handshake records as we
1542 * wont be doing client auth.
1544 if (!ssl3_digest_cached_records(s, 0))
1549 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1550 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1551 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
1555 /* TLS does not like anon-DH with client cert */
1556 if (s->version > SSL3_VERSION) {
1557 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1558 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1559 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1560 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1565 return tls_process_certificate_request(s, (unsigned long)n);
1567 s->state = SSL_ST_ERR;
1572 enum MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, unsigned long n)
1574 int ret = MSG_PROCESS_ERROR;
1575 unsigned int list_len, ctype_num, i, name_len;
1576 X509_NAME *xn = NULL;
1577 unsigned char *data;
1578 unsigned char *namestart, *namebytes;
1579 STACK_OF(X509_NAME) *ca_sk = NULL;
1582 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1583 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1584 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
1588 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1589 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1593 /* get the certificate types */
1594 if (!PACKET_get_1(&pkt, &ctype_num)
1595 || !PACKET_get_bytes(&pkt, &data, ctype_num)) {
1596 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1597 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1600 OPENSSL_free(s->cert->ctypes);
1601 s->cert->ctypes = NULL;
1602 if (ctype_num > SSL3_CT_NUMBER) {
1603 /* If we exceed static buffer copy all to cert structure */
1604 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1605 if (s->cert->ctypes == NULL) {
1606 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1609 memcpy(s->cert->ctypes, data, ctype_num);
1610 s->cert->ctype_num = (size_t)ctype_num;
1611 ctype_num = SSL3_CT_NUMBER;
1613 for (i = 0; i < ctype_num; i++)
1614 s->s3->tmp.ctype[i] = data[i];
1616 if (SSL_USE_SIGALGS(s)) {
1617 if (!PACKET_get_net_2(&pkt, &list_len)
1618 || !PACKET_get_bytes(&pkt, &data, list_len)) {
1619 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1620 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1621 SSL_R_LENGTH_MISMATCH);
1625 /* Clear certificate digests and validity flags */
1626 for (i = 0; i < SSL_PKEY_NUM; i++) {
1627 s->s3->tmp.md[i] = NULL;
1628 s->s3->tmp.valid_flags[i] = 0;
1630 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1631 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1632 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1633 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1636 if (!tls1_process_sigalgs(s)) {
1637 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1638 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1643 /* get the CA RDNs */
1644 if (!PACKET_get_net_2(&pkt, &list_len)
1645 || PACKET_remaining(&pkt) != list_len) {
1646 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1647 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1651 while (PACKET_remaining(&pkt)) {
1652 if (!PACKET_get_net_2(&pkt, &name_len)
1653 || !PACKET_get_bytes(&pkt, &namebytes, name_len)) {
1654 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1655 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1656 SSL_R_LENGTH_MISMATCH);
1660 namestart = namebytes;
1662 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1663 name_len)) == NULL) {
1664 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1665 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1669 if (namebytes != (namestart + name_len)) {
1670 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1671 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1672 SSL_R_CA_DN_LENGTH_MISMATCH);
1675 if (!sk_X509_NAME_push(ca_sk, xn)) {
1676 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1681 /* we should setup a certificate to return.... */
1682 s->s3->tmp.cert_req = 1;
1683 s->s3->tmp.ctype_num = ctype_num;
1684 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1685 s->s3->tmp.ca_names = ca_sk;
1688 ret = MSG_PROCESS_CONTINUE_READING;
1691 statem_set_error(s);
1693 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1697 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1699 return (X509_NAME_cmp(*a, *b));
1702 int ssl3_get_new_session_ticket(SSL *s)
1707 n = s->method->ssl_get_message(s,
1708 SSL3_ST_CR_SESSION_TICKET_A,
1709 SSL3_ST_CR_SESSION_TICKET_B,
1710 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
1715 return tls_process_new_session_ticket(s, (unsigned long)n);
1718 enum MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, unsigned long n)
1721 unsigned int ticklen;
1722 unsigned long ticket_lifetime_hint;
1725 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1726 al = SSL_AD_INTERNAL_ERROR;
1727 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
1731 if (!PACKET_get_net_4(&pkt, &ticket_lifetime_hint)
1732 || !PACKET_get_net_2(&pkt, &ticklen)
1733 || PACKET_remaining(&pkt) != ticklen) {
1734 al = SSL_AD_DECODE_ERROR;
1735 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1739 /* Server is allowed to change its mind and send an empty ticket. */
1743 if (s->session->session_id_length > 0) {
1744 int i = s->session_ctx->session_cache_mode;
1745 SSL_SESSION *new_sess;
1747 * We reused an existing session, so we need to replace it with a new
1750 if (i & SSL_SESS_CACHE_CLIENT) {
1752 * Remove the old session from the cache
1754 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
1755 if (s->session_ctx->remove_session_cb != NULL)
1756 s->session_ctx->remove_session_cb(s->session_ctx,
1759 /* We carry on if this fails */
1760 SSL_CTX_remove_session(s->session_ctx, s->session);
1764 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1765 al = SSL_AD_INTERNAL_ERROR;
1766 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1770 SSL_SESSION_free(s->session);
1771 s->session = new_sess;
1774 OPENSSL_free(s->session->tlsext_tick);
1775 s->session->tlsext_ticklen = 0;
1777 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1778 if (!s->session->tlsext_tick) {
1779 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1782 if (!PACKET_copy_bytes(&pkt, s->session->tlsext_tick, ticklen)) {
1783 al = SSL_AD_DECODE_ERROR;
1784 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1788 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1789 s->session->tlsext_ticklen = ticklen;
1791 * There are two ways to detect a resumed ticket session. One is to set
1792 * an appropriate session ID and then the server must return a match in
1793 * ServerHello. This allows the normal client session ID matching to work
1794 * and we know much earlier that the ticket has been accepted. The
1795 * other way is to set zero length session ID when the ticket is
1796 * presented and rely on the handshake to determine session resumption.
1797 * We choose the former approach because this fits in with assumptions
1798 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1799 * SHA256 is disabled) hash of the ticket.
1801 EVP_Digest(s->session->tlsext_tick, ticklen,
1802 s->session->session_id, &s->session->session_id_length,
1803 EVP_sha256(), NULL);
1804 return MSG_PROCESS_CONTINUE_READING;
1806 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1808 statem_set_error(s);
1809 return MSG_PROCESS_ERROR;
1812 int ssl3_get_cert_status(SSL *s)
1817 n = s->method->ssl_get_message(s,
1818 SSL3_ST_CR_CERT_STATUS_A,
1819 SSL3_ST_CR_CERT_STATUS_B,
1820 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
1825 return tls_process_cert_status(s, (unsigned long)n);
1828 enum MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, unsigned long n)
1831 unsigned long resplen;
1835 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1836 al = SSL_AD_INTERNAL_ERROR;
1837 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_INTERNAL_ERROR);
1840 if (!PACKET_get_1(&pkt, &type)
1841 || type != TLSEXT_STATUSTYPE_ocsp) {
1842 al = SSL_AD_DECODE_ERROR;
1843 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1846 if (!PACKET_get_net_3(&pkt, &resplen)
1847 || PACKET_remaining(&pkt) != resplen) {
1848 al = SSL_AD_DECODE_ERROR;
1849 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1852 OPENSSL_free(s->tlsext_ocsp_resp);
1853 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
1854 if (!s->tlsext_ocsp_resp) {
1855 al = SSL_AD_INTERNAL_ERROR;
1856 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1859 if (!PACKET_copy_bytes(&pkt, s->tlsext_ocsp_resp, resplen)) {
1860 al = SSL_AD_DECODE_ERROR;
1861 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1864 s->tlsext_ocsp_resplen = resplen;
1865 if (s->ctx->tlsext_status_cb) {
1867 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1869 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1870 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
1874 al = SSL_AD_INTERNAL_ERROR;
1875 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1879 return MSG_PROCESS_CONTINUE_READING;
1881 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1882 statem_set_error(s);
1883 return MSG_PROCESS_ERROR;
1886 int ssl3_get_server_done(SSL *s)
1891 /* Second to last param should be very small, like 0 :-) */
1892 n = s->method->ssl_get_message(s,
1893 SSL3_ST_CR_SRVR_DONE_A,
1894 SSL3_ST_CR_SRVR_DONE_B,
1895 SSL3_MT_SERVER_DONE, 30, &ok);
1900 return tls_process_server_done(s, (unsigned long)n);
1903 enum MSG_PROCESS_RETURN tls_process_server_done(SSL *s, unsigned long n)
1906 /* should contain no data */
1907 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1908 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1909 statem_set_error(s);
1910 return MSG_PROCESS_ERROR;
1913 #ifndef OPENSSL_NO_SRP
1914 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
1915 if (SRP_Calc_A_param(s) <= 0) {
1916 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
1917 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1918 statem_set_error(s);
1919 return MSG_PROCESS_ERROR;
1924 return MSG_PROCESS_FINISHED_READING;
1927 int ssl3_send_client_key_exchange(SSL *s)
1931 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1932 if(tls_construct_client_key_exchange(s) == 0)
1935 s->state = SSL3_ST_CW_KEY_EXCH_B;
1938 /* SSL3_ST_CW_KEY_EXCH_B */
1939 n = ssl_do_write(s);
1942 if (tls_client_key_exchange_post_work(s) == 0)
1948 s->state = SSL_ST_ERR;
1953 int tls_construct_client_key_exchange(SSL *s)
1957 #ifndef OPENSSL_NO_PSK
1958 size_t pskhdrlen = 0;
1960 unsigned long alg_k;
1961 #ifndef OPENSSL_NO_RSA
1963 EVP_PKEY *pkey = NULL;
1965 #ifndef OPENSSL_NO_EC
1966 EC_KEY *clnt_ecdh = NULL;
1967 const EC_POINT *srvr_ecpoint = NULL;
1968 EVP_PKEY *srvr_pub_pkey = NULL;
1969 unsigned char *encodedPoint = NULL;
1970 int encoded_pt_len = 0;
1971 BN_CTX *bn_ctx = NULL;
1973 unsigned char *pms = NULL;
1975 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1977 p = ssl_handshake_start(s);
1980 #ifndef OPENSSL_NO_PSK
1981 if (alg_k & SSL_PSK) {
1984 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
1985 * \0-terminated identity. The last byte is for us for simulating
1988 char identity[PSK_MAX_IDENTITY_LEN + 1];
1990 unsigned char psk[PSK_MAX_PSK_LEN];
1993 if (s->psk_client_callback == NULL) {
1994 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1995 SSL_R_PSK_NO_CLIENT_CB);
1999 memset(identity, 0, sizeof(identity));
2001 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2002 identity, sizeof(identity) - 1,
2005 if (psklen > PSK_MAX_PSK_LEN) {
2006 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2007 ERR_R_INTERNAL_ERROR);
2009 } else if (psklen == 0) {
2010 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2011 SSL_R_PSK_IDENTITY_NOT_FOUND);
2015 OPENSSL_free(s->s3->tmp.psk);
2016 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2017 OPENSSL_cleanse(psk, psklen);
2019 if (s->s3->tmp.psk == NULL) {
2020 OPENSSL_cleanse(identity, sizeof(identity));
2024 s->s3->tmp.psklen = psklen;
2026 identitylen = strlen(identity);
2027 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2028 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2029 ERR_R_INTERNAL_ERROR);
2032 OPENSSL_free(s->session->psk_identity);
2033 s->session->psk_identity = BUF_strdup(identity);
2034 if (s->session->psk_identity == NULL) {
2035 OPENSSL_cleanse(identity, sizeof(identity));
2039 s2n(identitylen, p);
2040 memcpy(p, identity, identitylen);
2041 pskhdrlen = 2 + identitylen;
2045 OPENSSL_cleanse(identity, sizeof(identity));
2047 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2051 if (alg_k & SSL_kPSK) {
2056 /* Fool emacs indentation */
2059 #ifndef OPENSSL_NO_RSA
2060 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2062 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2063 pms = OPENSSL_malloc(pmslen);
2067 if (s->session->peer == NULL) {
2069 * We should always have a server certificate with SSL_kRSA.
2071 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2072 ERR_R_INTERNAL_ERROR);
2076 if (s->s3->peer_rsa_tmp != NULL)
2077 rsa = s->s3->peer_rsa_tmp;
2079 pkey = X509_get_pubkey(s->session->peer);
2080 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2081 || (pkey->pkey.rsa == NULL)) {
2082 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2083 ERR_R_INTERNAL_ERROR);
2084 EVP_PKEY_free(pkey);
2087 rsa = pkey->pkey.rsa;
2088 EVP_PKEY_free(pkey);
2091 pms[0] = s->client_version >> 8;
2092 pms[1] = s->client_version & 0xff;
2093 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2097 /* Fix buf for TLS and beyond */
2098 if (s->version > SSL3_VERSION)
2100 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2102 if (s->options & SSL_OP_PKCS1_CHECK_1)
2104 if (s->options & SSL_OP_PKCS1_CHECK_2)
2108 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2109 SSL_R_BAD_RSA_ENCRYPT);
2113 /* Fix buf for TLS and beyond */
2114 if (s->version > SSL3_VERSION) {
2120 #ifndef OPENSSL_NO_DH
2121 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2122 DH *dh_srvr, *dh_clnt;
2123 if (s->s3->peer_dh_tmp != NULL)
2124 dh_srvr = s->s3->peer_dh_tmp;
2126 /* we get them from the cert */
2127 EVP_PKEY *spkey = NULL;
2129 spkey = X509_get_pubkey(s->session->peer);
2131 dh_srvr = EVP_PKEY_get1_DH(spkey);
2132 EVP_PKEY_free(spkey);
2134 if (dh_srvr == NULL) {
2135 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2136 ERR_R_INTERNAL_ERROR);
2140 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2141 /* Use client certificate key */
2142 EVP_PKEY *clkey = s->cert->key->privatekey;
2145 dh_clnt = EVP_PKEY_get1_DH(clkey);
2146 if (dh_clnt == NULL) {
2147 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2148 ERR_R_INTERNAL_ERROR);
2152 /* generate a new random key */
2153 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2154 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2157 if (!DH_generate_key(dh_clnt)) {
2158 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2164 pmslen = DH_size(dh_clnt);
2165 pms = OPENSSL_malloc(pmslen);
2170 * use the 'p' output buffer for the DH key, but make sure to
2171 * clear it out afterwards
2174 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2175 if (s->s3->peer_dh_tmp == NULL)
2179 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2185 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2188 /* send off the data */
2189 n = BN_num_bytes(dh_clnt->pub_key);
2191 BN_bn2bin(dh_clnt->pub_key, p);
2199 #ifndef OPENSSL_NO_EC
2200 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2201 const EC_GROUP *srvr_group = NULL;
2203 int ecdh_clnt_cert = 0;
2206 * Did we send out the client's ECDH share for use in premaster
2207 * computation as part of client certificate? If so, set
2208 * ecdh_clnt_cert to 1.
2210 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2212 * XXX: For now, we do not support client
2213 * authentication using ECDH certificates.
2214 * To add such support, one needs to add
2215 * code that checks for appropriate
2216 * conditions and sets ecdh_clnt_cert to 1.
2217 * For example, the cert have an ECC
2218 * key on the same curve as the server's
2219 * and the key should be authorized for
2222 * One also needs to add code in ssl3_connect
2223 * to skip sending the certificate verify
2226 * if ((s->cert->key->privatekey != NULL) &&
2227 * (s->cert->key->privatekey->type ==
2228 * EVP_PKEY_EC) && ...)
2229 * ecdh_clnt_cert = 1;
2233 if (s->s3->peer_ecdh_tmp != NULL) {
2234 tkey = s->s3->peer_ecdh_tmp;
2236 /* Get the Server Public Key from Cert */
2237 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2238 if ((srvr_pub_pkey == NULL)
2239 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2240 || (srvr_pub_pkey->pkey.ec == NULL)) {
2241 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2242 ERR_R_INTERNAL_ERROR);
2246 tkey = srvr_pub_pkey->pkey.ec;
2249 srvr_group = EC_KEY_get0_group(tkey);
2250 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2252 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2253 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2254 ERR_R_INTERNAL_ERROR);
2258 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2259 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2260 ERR_R_MALLOC_FAILURE);
2264 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2265 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2268 if (ecdh_clnt_cert) {
2270 * Reuse key info from our certificate We only need our
2271 * private key to perform the ECDH computation.
2273 const BIGNUM *priv_key;
2274 tkey = s->cert->key->privatekey->pkey.ec;
2275 priv_key = EC_KEY_get0_private_key(tkey);
2276 if (priv_key == NULL) {
2277 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2278 ERR_R_MALLOC_FAILURE);
2281 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2282 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2286 /* Generate a new ECDH key pair */
2287 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2288 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2295 * use the 'p' output buffer for the ECDH key, but make sure to
2296 * clear it out afterwards
2299 field_size = EC_GROUP_get_degree(srvr_group);
2300 if (field_size <= 0) {
2301 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2304 pmslen = (field_size + 7) / 8;
2305 pms = OPENSSL_malloc(pmslen);
2308 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2309 if (n <= 0 || pmslen != (size_t)n) {
2310 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2314 if (ecdh_clnt_cert) {
2315 /* Send empty client key exch message */
2319 * First check the size of encoding and allocate memory
2323 EC_POINT_point2oct(srvr_group,
2324 EC_KEY_get0_public_key(clnt_ecdh),
2325 POINT_CONVERSION_UNCOMPRESSED,
2328 encodedPoint = (unsigned char *)
2329 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2330 bn_ctx = BN_CTX_new();
2331 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2332 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2333 ERR_R_MALLOC_FAILURE);
2337 /* Encode the public key */
2338 n = EC_POINT_point2oct(srvr_group,
2339 EC_KEY_get0_public_key(clnt_ecdh),
2340 POINT_CONVERSION_UNCOMPRESSED,
2341 encodedPoint, encoded_pt_len, bn_ctx);
2343 *p = n; /* length of encoded point */
2344 /* Encoded point will be copied here */
2346 /* copy the point */
2347 memcpy(p, encodedPoint, n);
2348 /* increment n to account for length field */
2352 /* Free allocated memory */
2353 BN_CTX_free(bn_ctx);
2354 OPENSSL_free(encodedPoint);
2355 EC_KEY_free(clnt_ecdh);
2356 EVP_PKEY_free(srvr_pub_pkey);
2358 #endif /* !OPENSSL_NO_EC */
2359 else if (alg_k & SSL_kGOST) {
2360 /* GOST key exchange message creation */
2361 EVP_PKEY_CTX *pkey_ctx;
2364 unsigned int md_len;
2365 unsigned char shared_ukm[32], tmp[256];
2366 EVP_MD_CTX *ukm_hash;
2370 pms = OPENSSL_malloc(pmslen);
2375 * Get server sertificate PKEY and create ctx from it
2377 peer_cert = s->session->peer;
2379 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2380 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2384 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2385 X509_get_pubkey(peer_cert), NULL);
2387 * If we have send a certificate, and certificate key
2389 * * parameters match those of server certificate, use
2390 * certificate key for key exchange
2393 /* Otherwise, generate ephemeral key pair */
2395 EVP_PKEY_encrypt_init(pkey_ctx);
2396 /* Generate session key */
2397 if (RAND_bytes(pms, pmslen) <= 0) {
2398 EVP_PKEY_CTX_free(pkey_ctx);
2399 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2400 ERR_R_INTERNAL_ERROR);
2404 * If we have client certificate, use its secret as peer key
2406 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2407 if (EVP_PKEY_derive_set_peer
2408 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2410 * If there was an error - just ignore it. Ephemeral key
2417 * Compute shared IV and store it in algorithm-specific context
2420 ukm_hash = EVP_MD_CTX_create();
2421 EVP_DigestInit(ukm_hash,
2422 EVP_get_digestbynid(NID_id_GostR3411_94));
2423 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2425 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2427 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2428 EVP_MD_CTX_destroy(ukm_hash);
2429 if (EVP_PKEY_CTX_ctrl
2430 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2432 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2436 /* Make GOST keytransport blob message */
2438 * Encapsulate it into sequence
2440 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2442 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2443 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2447 if (msglen >= 0x80) {
2449 *(p++) = msglen & 0xff;
2452 *(p++) = msglen & 0xff;
2455 memcpy(p, tmp, msglen);
2456 /* Check if pubkey from client certificate was used */
2457 if (EVP_PKEY_CTX_ctrl
2458 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2459 /* Set flag "skip certificate verify" */
2460 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2462 EVP_PKEY_CTX_free(pkey_ctx);
2463 EVP_PKEY_free(pub_key);
2466 #ifndef OPENSSL_NO_SRP
2467 else if (alg_k & SSL_kSRP) {
2468 if (s->srp_ctx.A != NULL) {
2469 /* send off the data */
2470 n = BN_num_bytes(s->srp_ctx.A);
2472 BN_bn2bin(s->srp_ctx.A, p);
2475 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2476 ERR_R_INTERNAL_ERROR);
2479 OPENSSL_free(s->session->srp_username);
2480 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2481 if (s->session->srp_username == NULL) {
2482 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2483 ERR_R_MALLOC_FAILURE);
2489 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2490 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2494 #ifndef OPENSSL_NO_PSK
2498 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2499 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2500 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2504 s->s3->tmp.pms = pms;
2505 s->s3->tmp.pmslen = pmslen;
2509 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2510 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2512 OPENSSL_clear_free(pms, pmslen);
2513 s->s3->tmp.pms = NULL;
2514 #ifndef OPENSSL_NO_EC
2515 BN_CTX_free(bn_ctx);
2516 OPENSSL_free(encodedPoint);
2517 EC_KEY_free(clnt_ecdh);
2518 EVP_PKEY_free(srvr_pub_pkey);
2520 #ifndef OPENSSL_NO_PSK
2521 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2522 s->s3->tmp.psk = NULL;
2524 statem_set_error(s);
2528 int tls_client_key_exchange_post_work(SSL *s)
2530 unsigned char *pms = NULL;
2533 #ifndef OPENSSL_NO_SRP
2535 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2536 if (!srp_generate_client_master_secret(s)) {
2537 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2538 ERR_R_INTERNAL_ERROR);
2544 pms = s->s3->tmp.pms;
2545 pmslen = s->s3->tmp.pmslen;
2547 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2548 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2549 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2552 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2553 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2554 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2559 OPENSSL_clear_free(pms, pmslen);
2560 s->s3->tmp.pms = NULL;
2564 int ssl3_send_client_verify(SSL *s)
2566 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2567 if(tls_construct_client_verify(s) == 0)
2570 s->state = SSL3_ST_CW_CERT_VRFY_B;
2573 /* SSL3_ST_CW_CERT_VRFY_B */
2574 return ssl_do_write(s);
2576 s->state = SSL_ST_ERR;
2581 int tls_construct_client_verify(SSL *s)
2584 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2586 EVP_PKEY_CTX *pctx = NULL;
2592 EVP_MD_CTX_init(&mctx);
2594 p = ssl_handshake_start(s);
2595 pkey = s->cert->key->privatekey;
2596 /* Create context from key and test if sha1 is allowed as digest */
2597 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2598 EVP_PKEY_sign_init(pctx);
2599 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2600 if (!SSL_USE_SIGALGS(s))
2601 s->method->ssl3_enc->cert_verify_mac(s,
2604 [MD5_DIGEST_LENGTH]));
2609 * For TLS v1.2 send signature algorithm and signature using agreed
2610 * digest and cached handshake records.
2612 if (SSL_USE_SIGALGS(s)) {
2615 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2616 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2617 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
2618 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2623 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
2626 if (!EVP_SignInit_ex(&mctx, md, NULL)
2627 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
2628 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2629 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2634 /* Digest cached records and discard handshake buffer */
2635 if (!ssl3_digest_cached_records(s, 0))
2638 #ifndef OPENSSL_NO_RSA
2639 if (pkey->type == EVP_PKEY_RSA) {
2640 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
2641 if (RSA_sign(NID_md5_sha1, data,
2642 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
2643 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2644 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
2651 #ifndef OPENSSL_NO_DSA
2652 if (pkey->type == EVP_PKEY_DSA) {
2653 if (!DSA_sign(pkey->save_type,
2654 &(data[MD5_DIGEST_LENGTH]),
2655 SHA_DIGEST_LENGTH, &(p[2]),
2656 (unsigned int *)&j, pkey->pkey.dsa)) {
2657 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
2664 #ifndef OPENSSL_NO_EC
2665 if (pkey->type == EVP_PKEY_EC) {
2666 if (!ECDSA_sign(pkey->save_type,
2667 &(data[MD5_DIGEST_LENGTH]),
2668 SHA_DIGEST_LENGTH, &(p[2]),
2669 (unsigned int *)&j, pkey->pkey.ec)) {
2670 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
2677 if (pkey->type == NID_id_GostR3410_2001) {
2678 unsigned char signbuf[64];
2680 size_t sigsize = 64;
2681 s->method->ssl3_enc->cert_verify_mac(s,
2682 NID_id_GostR3411_94, data);
2683 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
2684 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2687 for (i = 63, j = 0; i >= 0; j++, i--) {
2688 p[2 + j] = signbuf[i];
2693 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2696 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2697 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2701 EVP_MD_CTX_cleanup(&mctx);
2702 EVP_PKEY_CTX_free(pctx);
2705 EVP_MD_CTX_cleanup(&mctx);
2706 EVP_PKEY_CTX_free(pctx);
2711 * Check a certificate can be used for client authentication. Currently check
2712 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2713 * certificates can be used and optionally checks suitability for Suite B.
2715 static int ssl3_check_client_certificate(SSL *s)
2717 unsigned long alg_k;
2718 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2720 /* If no suitable signature algorithm can't use certificate */
2721 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2724 * If strict mode check suitability of chain before using it. This also
2725 * adjusts suite B digest if necessary.
2727 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2728 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2730 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2731 /* See if we can use client certificate for fixed DH */
2732 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
2733 int i = s->session->peer_type;
2734 EVP_PKEY *clkey = NULL, *spkey = NULL;
2735 clkey = s->cert->key->privatekey;
2736 /* If client key not DH assume it can be used */
2737 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2740 spkey = X509_get_pubkey(s->session->peer);
2742 /* Compare server and client parameters */
2743 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2744 EVP_PKEY_free(spkey);
2748 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2754 int ssl3_send_client_certificate(SSL *s)
2756 enum WORK_STATE wst;
2758 if (s->state == SSL3_ST_CW_CERT_A || s->state == SSL3_ST_CW_CERT_B) {
2759 if (s->state == SSL3_ST_CW_CERT_A)
2760 wst = tls_prepare_client_certificate(s, WORK_MORE_A);
2762 wst = tls_prepare_client_certificate(s, WORK_MORE_B);
2763 if (wst == WORK_ERROR)
2765 if (wst == WORK_MORE_A)
2767 if (wst == WORK_MORE_B) {
2768 s->state = SSL3_ST_CW_CERT_B;
2772 s->state = SSL3_ST_CW_CERT_C;
2775 if (s->state == SSL3_ST_CW_CERT_B) {
2776 wst = tls_prepare_client_certificate(s, WORK_MORE_B);
2777 if (wst == WORK_ERROR)
2779 if (wst == WORK_MORE_A)
2782 /* Skip state C...it was entirely temporary in the original code */
2783 s->state = SSL3_ST_CW_CERT_D;
2786 if (tls_construct_client_certificate(s) == 0)
2789 return ssl_do_write(s);
2791 s->state = SSL_ST_ERR;
2796 enum WORK_STATE tls_prepare_client_certificate(SSL *s, enum WORK_STATE wst)
2799 EVP_PKEY *pkey = NULL;
2802 if (wst == WORK_MORE_A) {
2803 /* Let cert callback update client certificates if required */
2804 if (s->cert->cert_cb) {
2805 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2807 s->rwstate = SSL_X509_LOOKUP;
2811 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2812 statem_set_error(s);
2815 s->rwstate = SSL_NOTHING;
2817 if (ssl3_check_client_certificate(s))
2818 return WORK_FINISHED_CONTINUE;
2820 /* Fall through to WORK_MORE_B */
2824 /* We need to get a client cert */
2825 if (wst == WORK_MORE_B) {
2827 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2828 * return(-1); We then get retied later
2830 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2832 s->rwstate = SSL_X509_LOOKUP;
2835 s->rwstate = SSL_NOTHING;
2836 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2837 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2839 } else if (i == 1) {
2841 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2842 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2846 EVP_PKEY_free(pkey);
2847 if (i && !ssl3_check_client_certificate(s))
2850 if (s->version == SSL3_VERSION) {
2851 s->s3->tmp.cert_req = 0;
2852 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2853 return WORK_FINISHED_CONTINUE;
2855 s->s3->tmp.cert_req = 2;
2856 if (!ssl3_digest_cached_records(s, 0)) {
2857 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2858 statem_set_error(s);
2864 return WORK_FINISHED_CONTINUE;
2867 /* Shouldn't ever get here */
2871 int tls_construct_client_certificate(SSL *s)
2873 if (!ssl3_output_cert_chain(s,
2874 (s->s3->tmp.cert_req ==
2875 2) ? NULL : s->cert->key)) {
2876 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2877 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2878 statem_set_error(s);
2885 #define has_bits(i,m) (((i)&(m)) == (m))
2887 int ssl3_check_cert_and_algorithm(SSL *s)
2891 EVP_PKEY *pkey = NULL;
2893 #ifndef OPENSSL_NO_RSA
2896 #ifndef OPENSSL_NO_DH
2899 int al = SSL_AD_HANDSHAKE_FAILURE;
2901 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2902 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2904 /* we don't have a certificate */
2905 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2907 #ifndef OPENSSL_NO_RSA
2908 rsa = s->s3->peer_rsa_tmp;
2910 #ifndef OPENSSL_NO_DH
2911 dh = s->s3->peer_dh_tmp;
2914 /* This is the passed certificate */
2916 idx = s->session->peer_type;
2917 #ifndef OPENSSL_NO_EC
2918 if (idx == SSL_PKEY_ECC) {
2919 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2921 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2926 } else if (alg_a & SSL_aECDSA) {
2927 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2928 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2930 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2931 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
2935 pkey = X509_get_pubkey(s->session->peer);
2936 pkey_bits = EVP_PKEY_bits(pkey);
2937 i = X509_certificate_type(s->session->peer, pkey);
2938 EVP_PKEY_free(pkey);
2940 /* Check that we have a certificate if we require one */
2941 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2942 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2943 SSL_R_MISSING_RSA_SIGNING_CERT);
2946 #ifndef OPENSSL_NO_DSA
2947 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2948 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2949 SSL_R_MISSING_DSA_SIGNING_CERT);
2953 #ifndef OPENSSL_NO_RSA
2954 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2955 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
2956 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2957 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2958 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2960 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
2961 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
2962 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2963 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2964 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2968 /* server key exchange is not allowed. */
2969 al = SSL_AD_INTERNAL_ERROR;
2970 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2977 #ifndef OPENSSL_NO_DH
2978 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
2979 al = SSL_AD_INTERNAL_ERROR;
2980 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2982 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2983 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
2984 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2985 SSL_R_MISSING_DH_RSA_CERT);
2988 # ifndef OPENSSL_NO_DSA
2989 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2990 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
2991 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2992 SSL_R_MISSING_DH_DSA_CERT);
2998 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
2999 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3000 #ifndef OPENSSL_NO_RSA
3001 if (alg_k & SSL_kRSA) {
3003 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3004 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3006 } else if (RSA_bits(rsa) >
3007 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3008 /* We have a temporary RSA key but it's too large. */
3009 al = SSL_AD_EXPORT_RESTRICTION;
3010 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3011 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3016 #ifndef OPENSSL_NO_DH
3017 if (alg_k & SSL_kDHE) {
3019 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3020 /* We have a temporary DH key but it's too large. */
3021 al = SSL_AD_EXPORT_RESTRICTION;
3022 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3023 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3026 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3027 /* The cert should have had an export DH key. */
3028 al = SSL_AD_EXPORT_RESTRICTION;
3029 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3030 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3035 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3036 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3042 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3046 #ifndef OPENSSL_NO_NEXTPROTONEG
3047 int ssl3_send_next_proto(SSL *s)
3049 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3050 if (tls_construct_next_proto(s) == 0) {
3051 s->state = SSL_ST_ERR;
3054 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3057 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3060 int tls_construct_next_proto(SSL *s)
3062 unsigned int len, padding_len;
3065 len = s->next_proto_negotiated_len;
3066 padding_len = 32 - ((len + 2) % 32);
3067 d = (unsigned char *)s->init_buf->data;
3069 memcpy(d + 5, s->next_proto_negotiated, len);
3070 d[5 + len] = padding_len;
3071 memset(d + 6 + len, 0, padding_len);
3072 *(d++) = SSL3_MT_NEXT_PROTO;
3073 l2n3(2 + len + padding_len, d);
3074 s->init_num = 4 + 2 + len + padding_len;
3081 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3084 #ifndef OPENSSL_NO_ENGINE
3085 if (s->ctx->client_cert_engine) {
3086 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3087 SSL_get_client_CA_list(s),
3088 px509, ppkey, NULL, NULL, NULL);
3093 if (s->ctx->client_cert_cb)
3094 i = s->ctx->client_cert_cb(s, px509, ppkey);
3098 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3104 int empty_reneg_info_scsv = !s->renegotiate;
3105 /* Set disabled masks for this session */
3106 ssl_set_client_disabled(s);
3112 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3113 c = sk_SSL_CIPHER_value(sk, i);
3114 /* Skip disabled ciphers */
3115 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3117 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3118 if (c->id == SSL3_CK_SCSV) {
3119 if (!empty_reneg_info_scsv)
3122 empty_reneg_info_scsv = 0;
3125 j = s->method->put_cipher_by_char(c, p);
3129 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3133 if (empty_reneg_info_scsv) {
3134 static SSL_CIPHER scsv = {
3135 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3137 j = s->method->put_cipher_by_char(&scsv, p);
3139 #ifdef OPENSSL_RI_DEBUG
3141 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3144 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3145 static SSL_CIPHER scsv = {
3146 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3148 j = s->method->put_cipher_by_char(&scsv, p);