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 tls_construct_client_hello(SSL *s)
273 unsigned char *p, *d;
277 #ifndef OPENSSL_NO_COMP
281 SSL_SESSION *sess = s->session;
283 buf = (unsigned char *)s->init_buf->data;
285 /* Work out what SSL/TLS/DTLS version to use */
286 if (ssl_set_version(s) == 0)
289 if ((sess == NULL) || (sess->ssl_version != s->version) ||
291 * In the case of EAP-FAST, we can have a pre-shared
292 * "ticket" without a session ID.
294 (!sess->session_id_length && !sess->tlsext_tick) ||
295 (sess->not_resumable)) {
296 if (!ssl_get_new_session(s, 0))
299 /* else use the pre-loaded session */
301 p = s->s3->client_random;
304 * for DTLS if client_random is initialized, reuse it, we are
305 * required to use same upon reply to HelloVerify
307 if (SSL_IS_DTLS(s)) {
310 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
319 if (i && ssl_fill_hello_random(s, 0, p,
320 sizeof(s->s3->client_random)) <= 0)
323 /* Do the message type and length last */
324 d = p = ssl_handshake_start(s);
327 * version indicates the negotiated version: for example from
328 * an SSLv2/v3 compatible client hello). The client_version
329 * field is the maximum version we permit and it is also
330 * used in RSA encrypted premaster secrets. Some servers can
331 * choke if we initially report a higher version then
332 * renegotiate to a lower one in the premaster secret. This
333 * didn't happen with TLS 1.0 as most servers supported it
334 * but it can with TLS 1.1 or later if the server only supports
337 * Possible scenario with previous logic:
338 * 1. Client hello indicates TLS 1.2
339 * 2. Server hello says TLS 1.0
340 * 3. RSA encrypted premaster secret uses 1.2.
341 * 4. Handhaked proceeds using TLS 1.0.
342 * 5. Server sends hello request to renegotiate.
343 * 6. Client hello indicates TLS v1.0 as we now
344 * know that is maximum server supports.
345 * 7. Server chokes on RSA encrypted premaster secret
346 * containing version 1.0.
348 * For interoperability it should be OK to always use the
349 * maximum version we support in client hello and then rely
350 * on the checking of version to ensure the servers isn't
351 * being inconsistent: for example initially negotiating with
352 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
353 * client_version in client hello and not resetting it to
354 * the negotiated version.
356 *(p++) = s->client_version >> 8;
357 *(p++) = s->client_version & 0xff;
360 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
361 p += SSL3_RANDOM_SIZE;
367 i = s->session->session_id_length;
370 if (i > (int)sizeof(s->session->session_id)) {
371 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
374 memcpy(p, s->session->session_id, i);
378 /* cookie stuff for DTLS */
379 if (SSL_IS_DTLS(s)) {
380 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
381 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
384 *(p++) = s->d1->cookie_len;
385 memcpy(p, s->d1->cookie, s->d1->cookie_len);
386 p += s->d1->cookie_len;
389 /* Ciphers supported */
390 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
392 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
395 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
397 * Some servers hang if client hello > 256 bytes as hack workaround
398 * chop number of supported ciphers to keep it well below this if we
401 if (TLS1_get_version(s) >= TLS1_2_VERSION
402 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
403 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
409 #ifdef OPENSSL_NO_COMP
413 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
416 j = sk_SSL_COMP_num(s->ctx->comp_methods);
418 for (i = 0; i < j; i++) {
419 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
423 *(p++) = 0; /* Add the NULL method */
426 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
427 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
431 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
433 ssl3_send_alert(s, SSL3_AL_FATAL, al);
434 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
439 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
440 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
441 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
451 enum MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
453 STACK_OF(SSL_CIPHER) *sk;
456 size_t session_id_len;
457 unsigned char *cipherchars;
458 int i, al = SSL_AD_INTERNAL_ERROR;
459 unsigned int compression;
460 #ifndef OPENSSL_NO_COMP
464 if (s->method->version == TLS_ANY_VERSION) {
465 unsigned int sversion;
467 if (!PACKET_get_net_2(pkt, &sversion)) {
468 al = SSL_AD_DECODE_ERROR;
469 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
473 #if TLS_MAX_VERSION != TLS1_2_VERSION
474 #error Code needs updating for new TLS version
476 #ifndef OPENSSL_NO_SSL3
477 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
479 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
480 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
481 al = SSL_AD_PROTOCOL_VERSION;
484 s->method = SSLv3_client_method();
487 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
488 s->method = TLSv1_client_method();
489 } else if ((sversion == TLS1_1_VERSION) &&
490 !(s->options & SSL_OP_NO_TLSv1_1)) {
491 s->method = TLSv1_1_client_method();
492 } else if ((sversion == TLS1_2_VERSION) &&
493 !(s->options & SSL_OP_NO_TLSv1_2)) {
494 s->method = TLSv1_2_client_method();
496 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
497 al = SSL_AD_PROTOCOL_VERSION;
500 s->session->ssl_version = s->version = s->method->version;
502 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
503 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
504 al = SSL_AD_PROTOCOL_VERSION;
507 } else if (s->method->version == DTLS_ANY_VERSION) {
508 /* Work out correct protocol version to use */
509 unsigned int hversion;
512 if (!PACKET_get_net_2(pkt, &hversion)) {
513 al = SSL_AD_DECODE_ERROR;
514 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
518 options = s->options;
519 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
520 s->method = DTLSv1_2_client_method();
521 else if (tls1_suiteb(s)) {
522 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
523 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
524 s->version = hversion;
525 al = SSL_AD_PROTOCOL_VERSION;
527 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
528 s->method = DTLSv1_client_method();
530 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
531 s->version = hversion;
532 al = SSL_AD_PROTOCOL_VERSION;
535 s->session->ssl_version = s->version = s->method->version;
539 if (!PACKET_get_bytes(pkt, &vers, 2)) {
540 al = SSL_AD_DECODE_ERROR;
541 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
544 if ((vers[0] != (s->version >> 8))
545 || (vers[1] != (s->version & 0xff))) {
546 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
547 s->version = (s->version & 0xff00) | vers[1];
548 al = SSL_AD_PROTOCOL_VERSION;
553 /* load the server hello data */
554 /* load the server random */
555 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
556 al = SSL_AD_DECODE_ERROR;
557 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
563 /* Get the session-id. */
564 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
565 al = SSL_AD_DECODE_ERROR;
566 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
569 session_id_len = PACKET_remaining(&session_id);
570 if (session_id_len > sizeof s->session->session_id
571 || session_id_len > SSL3_SESSION_ID_SIZE) {
572 al = SSL_AD_ILLEGAL_PARAMETER;
573 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
577 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
578 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
579 al = SSL_AD_DECODE_ERROR;
584 * Check if we can resume the session based on external pre-shared secret.
585 * EAP-FAST (RFC 4851) supports two types of session resumption.
586 * Resumption based on server-side state works with session IDs.
587 * Resumption based on pre-shared Protected Access Credentials (PACs)
588 * works by overriding the SessionTicket extension at the application
589 * layer, and does not send a session ID. (We do not know whether EAP-FAST
590 * servers would honour the session ID.) Therefore, the session ID alone
591 * is not a reliable indicator of session resumption, so we first check if
592 * we can resume, and later peek at the next handshake message to see if the
593 * server wants to resume.
595 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
596 s->session->tlsext_tick) {
597 SSL_CIPHER *pref_cipher = NULL;
598 s->session->master_key_length = sizeof(s->session->master_key);
599 if (s->tls_session_secret_cb(s, s->session->master_key,
600 &s->session->master_key_length,
602 s->tls_session_secret_cb_arg)) {
603 s->session->cipher = pref_cipher ?
604 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
606 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
607 al = SSL_AD_INTERNAL_ERROR;
612 if (session_id_len != 0 && session_id_len == s->session->session_id_length
613 && memcmp(PACKET_data(&session_id), s->session->session_id,
614 session_id_len) == 0) {
615 if (s->sid_ctx_length != s->session->sid_ctx_length
616 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
617 /* actually a client application bug */
618 al = SSL_AD_ILLEGAL_PARAMETER;
619 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
620 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
626 * If we were trying for session-id reuse but the server
627 * didn't echo the ID, make a new SSL_SESSION.
628 * In the case of EAP-FAST and PAC, we do not send a session ID,
629 * so the PAC-based session secret is always preserved. It'll be
630 * overwritten if the server refuses resumption.
632 if (s->session->session_id_length > 0) {
633 if (!ssl_get_new_session(s, 0)) {
638 s->session->session_id_length = session_id_len;
639 /* session_id_len could be 0 */
640 memcpy(s->session->session_id, PACKET_data(&session_id),
644 c = ssl_get_cipher_by_char(s, cipherchars);
647 al = SSL_AD_ILLEGAL_PARAMETER;
648 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
651 /* Set version disabled mask now we know version */
652 if (!SSL_USE_TLS1_2_CIPHERS(s))
653 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
655 s->s3->tmp.mask_ssl = 0;
657 * If it is a disabled cipher we didn't send it in client hello, so
660 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
661 al = SSL_AD_ILLEGAL_PARAMETER;
662 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
666 sk = ssl_get_ciphers_by_id(s);
667 i = sk_SSL_CIPHER_find(sk, c);
669 /* we did not say we would use this cipher */
670 al = SSL_AD_ILLEGAL_PARAMETER;
671 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
676 * Depending on the session caching (internal/external), the cipher
677 * and/or cipher_id values may not be set. Make sure that cipher_id is
678 * set and use it for comparison.
680 if (s->session->cipher)
681 s->session->cipher_id = s->session->cipher->id;
682 if (s->hit && (s->session->cipher_id != c->id)) {
683 al = SSL_AD_ILLEGAL_PARAMETER;
684 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
685 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
688 s->s3->tmp.new_cipher = c;
690 * Don't digest cached records if no sigalgs: we may need them for client
693 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
695 /* lets get the compression algorithm */
697 if (!PACKET_get_1(pkt, &compression)) {
698 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
699 al = SSL_AD_DECODE_ERROR;
702 #ifdef OPENSSL_NO_COMP
703 if (compression != 0) {
704 al = SSL_AD_ILLEGAL_PARAMETER;
705 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
706 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
710 * If compression is disabled we'd better not try to resume a session
713 if (s->session->compress_meth != 0) {
714 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
718 if (s->hit && compression != s->session->compress_meth) {
719 al = SSL_AD_ILLEGAL_PARAMETER;
720 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
721 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
724 if (compression == 0)
726 else if (!ssl_allow_compression(s)) {
727 al = SSL_AD_ILLEGAL_PARAMETER;
728 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
731 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
734 if (compression != 0 && comp == NULL) {
735 al = SSL_AD_ILLEGAL_PARAMETER;
736 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
737 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
740 s->s3->tmp.new_compression = comp;
745 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
746 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
750 if (PACKET_remaining(pkt) != 0) {
751 /* wrong packet length */
752 al = SSL_AD_DECODE_ERROR;
753 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
757 #ifndef OPENSSL_NO_SCTP
758 if (SSL_IS_DTLS(s) && s->hit) {
759 unsigned char sctpauthkey[64];
760 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
763 * Add new shared key for SCTP-Auth, will be ignored if
766 snprintf((char *)labelbuffer,
767 sizeof(DTLS1_SCTP_AUTH_LABEL),
768 DTLS1_SCTP_AUTH_LABEL);
770 if (SSL_export_keying_material(s, sctpauthkey,
773 sizeof(labelbuffer), NULL, 0,
777 BIO_ctrl(SSL_get_wbio(s),
778 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
779 sizeof(sctpauthkey), sctpauthkey);
783 return MSG_PROCESS_CONTINUE_READING;
785 ssl3_send_alert(s, SSL3_AL_FATAL, al);
788 return MSG_PROCESS_ERROR;
791 enum MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
793 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
794 unsigned long cert_list_len, cert_len;
796 unsigned char *certstart, *certbytes;
797 STACK_OF(X509) *sk = NULL;
798 EVP_PKEY *pkey = NULL;
800 if ((sk = sk_X509_new_null()) == NULL) {
801 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
805 if (!PACKET_get_net_3(pkt, &cert_list_len)
806 || PACKET_remaining(pkt) != cert_list_len) {
807 al = SSL_AD_DECODE_ERROR;
808 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
811 while (PACKET_remaining(pkt)) {
812 if (!PACKET_get_net_3(pkt, &cert_len)
813 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
814 al = SSL_AD_DECODE_ERROR;
815 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
816 SSL_R_CERT_LENGTH_MISMATCH);
820 certstart = certbytes;
821 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
823 al = SSL_AD_BAD_CERTIFICATE;
824 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
827 if (certbytes != (certstart + cert_len)) {
828 al = SSL_AD_DECODE_ERROR;
829 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
830 SSL_R_CERT_LENGTH_MISMATCH);
833 if (!sk_X509_push(sk, x)) {
834 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
840 i = ssl_verify_cert_chain(s, sk);
841 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
842 al = ssl_verify_alarm_type(s->verify_result);
843 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
844 SSL_R_CERTIFICATE_VERIFY_FAILED);
847 ERR_clear_error(); /* but we keep s->verify_result */
849 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
850 al = SSL_AD_HANDSHAKE_FAILURE;
854 s->session->peer_chain = sk;
856 * Inconsistency alert: cert_chain does include the peer's certificate,
857 * which we don't include in s3_srvr.c
859 x = sk_X509_value(sk, 0);
862 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
865 pkey = X509_get_pubkey(x);
867 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
870 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
871 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
875 i = ssl_cert_type(x, pkey);
879 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
880 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
884 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
885 if (exp_idx >= 0 && i != exp_idx) {
887 al = SSL_AD_ILLEGAL_PARAMETER;
888 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
889 SSL_R_WRONG_CERTIFICATE_TYPE);
892 s->session->peer_type = i;
894 X509_free(s->session->peer);
896 s->session->peer = x;
897 s->session->verify_result = s->verify_result;
900 ret = MSG_PROCESS_CONTINUE_READING;
904 ssl3_send_alert(s, SSL3_AL_FATAL, al);
910 sk_X509_pop_free(sk, X509_free);
914 enum MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
916 #ifndef OPENSSL_NO_RSA
917 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
920 int al, j, verify_ret;
922 EVP_PKEY *pkey = NULL;
923 const EVP_MD *md = NULL;
924 #ifndef OPENSSL_NO_RSA
927 #ifndef OPENSSL_NO_DH
930 #ifndef OPENSSL_NO_EC
932 BN_CTX *bn_ctx = NULL;
933 EC_POINT *srvr_ecpoint = NULL;
936 PACKET save_param_start, signature;
938 EVP_MD_CTX_init(&md_ctx);
940 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
942 save_param_start = *pkt;
944 #ifndef OPENSSL_NO_RSA
945 RSA_free(s->s3->peer_rsa_tmp);
946 s->s3->peer_rsa_tmp = NULL;
948 #ifndef OPENSSL_NO_DH
949 DH_free(s->s3->peer_dh_tmp);
950 s->s3->peer_dh_tmp = NULL;
952 #ifndef OPENSSL_NO_EC
953 EC_KEY_free(s->s3->peer_ecdh_tmp);
954 s->s3->peer_ecdh_tmp = NULL;
957 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
959 al = SSL_AD_DECODE_ERROR;
961 #ifndef OPENSSL_NO_PSK
962 /* PSK ciphersuites are preceded by an identity hint */
963 if (alg_k & SSL_PSK) {
964 PACKET psk_identity_hint;
965 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
966 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
971 * Store PSK identity hint for later use, hint is used in
972 * ssl3_send_client_key_exchange. Assume that the maximum length of
973 * a PSK identity hint can be as long as the maximum length of a PSK
976 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
977 al = SSL_AD_HANDSHAKE_FAILURE;
978 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
982 if (!PACKET_strndup(&psk_identity_hint,
983 &s->session->psk_identity_hint)) {
984 al = SSL_AD_INTERNAL_ERROR;
989 /* Nothing else to do for plain PSK or RSAPSK */
990 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
992 #endif /* !OPENSSL_NO_PSK */
993 #ifndef OPENSSL_NO_SRP
994 if (alg_k & SSL_kSRP) {
995 PACKET prime, generator, salt, server_pub;
996 if (!PACKET_get_length_prefixed_2(pkt, &prime)
997 || !PACKET_get_length_prefixed_2(pkt, &generator)
998 || !PACKET_get_length_prefixed_1(pkt, &salt)
999 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1000 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1005 BN_bin2bn(PACKET_data(&prime),
1006 PACKET_remaining(&prime), NULL)) == NULL
1008 BN_bin2bn(PACKET_data(&generator),
1009 PACKET_remaining(&generator), NULL)) == NULL
1011 BN_bin2bn(PACKET_data(&salt),
1012 PACKET_remaining(&salt), NULL)) == NULL
1014 BN_bin2bn(PACKET_data(&server_pub),
1015 PACKET_remaining(&server_pub), NULL)) == NULL) {
1016 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1020 if (!srp_verify_server_param(s, &al)) {
1021 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1025 /* We must check if there is a certificate */
1026 if (alg_a & (SSL_aRSA|SSL_aDSS))
1027 pkey = X509_get_pubkey(s->session->peer);
1029 #endif /* !OPENSSL_NO_SRP */
1030 #ifndef OPENSSL_NO_RSA
1031 if (alg_k & SSL_kRSA) {
1033 /* Temporary RSA keys only allowed in export ciphersuites */
1034 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1035 al = SSL_AD_UNEXPECTED_MESSAGE;
1036 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1040 if (!PACKET_get_length_prefixed_2(pkt, &mod)
1041 || !PACKET_get_length_prefixed_2(pkt, &exp)) {
1042 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1046 if ((rsa = RSA_new()) == NULL) {
1047 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1051 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1053 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1055 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1059 /* this should be because we are using an export cipher */
1060 if (alg_a & SSL_aRSA)
1061 pkey = X509_get_pubkey(s->session->peer);
1063 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1067 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1068 al = SSL_AD_UNEXPECTED_MESSAGE;
1069 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1073 s->s3->peer_rsa_tmp = rsa;
1076 #else /* OPENSSL_NO_RSA */
1079 #ifndef OPENSSL_NO_DH
1080 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1081 PACKET prime, generator, pub_key;
1083 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1084 || !PACKET_get_length_prefixed_2(pkt, &generator)
1085 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1086 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1090 if ((dh = DH_new()) == NULL) {
1091 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1095 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1096 PACKET_remaining(&prime), NULL)) == NULL
1097 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1098 PACKET_remaining(&generator), NULL)) == NULL
1100 BN_bin2bn(PACKET_data(&pub_key),
1101 PACKET_remaining(&pub_key), NULL)) == NULL) {
1102 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1106 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1107 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1111 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1112 al = SSL_AD_HANDSHAKE_FAILURE;
1113 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1116 if (alg_a & (SSL_aRSA|SSL_aDSS))
1117 pkey = X509_get_pubkey(s->session->peer);
1118 /* else anonymous DH, so no certificate or pkey. */
1120 s->s3->peer_dh_tmp = dh;
1123 #endif /* !OPENSSL_NO_DH */
1125 #ifndef OPENSSL_NO_EC
1126 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1128 const EC_GROUP *group;
1130 unsigned char *ecparams;
1132 if ((ecdh = EC_KEY_new()) == NULL) {
1133 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1138 * Extract elliptic curve parameters and the server's ephemeral ECDH
1139 * public key. For now we only support named (not generic) curves and
1140 * ECParameters in this case is just three bytes.
1142 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1143 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1147 * Check curve is one of our preferences, if not server has sent an
1148 * invalid curve. ECParameters is 3 bytes.
1150 if (!tls1_check_curve(s, ecparams, 3)) {
1151 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1155 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
1156 al = SSL_AD_INTERNAL_ERROR;
1157 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1158 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1162 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1163 if (ngroup == NULL) {
1164 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1167 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1168 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1171 EC_GROUP_free(ngroup);
1173 group = EC_KEY_get0_group(ecdh);
1175 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1176 (EC_GROUP_get_degree(group) > 163)) {
1177 al = SSL_AD_EXPORT_RESTRICTION;
1178 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1179 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1183 /* Next, get the encoded ECPoint */
1184 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1185 ((bn_ctx = BN_CTX_new()) == NULL)) {
1186 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1190 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1191 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1195 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1196 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
1197 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1202 * The ECC/TLS specification does not mention the use of DSA to sign
1203 * ECParameters in the server key exchange message. We do support RSA
1207 # ifndef OPENSSL_NO_RSA
1208 else if (alg_a & SSL_aRSA)
1209 pkey = X509_get_pubkey(s->session->peer);
1211 # ifndef OPENSSL_NO_EC
1212 else if (alg_a & SSL_aECDSA)
1213 pkey = X509_get_pubkey(s->session->peer);
1215 /* else anonymous ECDH, so no certificate or pkey. */
1216 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1217 s->s3->peer_ecdh_tmp = ecdh;
1219 BN_CTX_free(bn_ctx);
1221 EC_POINT_free(srvr_ecpoint);
1222 srvr_ecpoint = NULL;
1224 al = SSL_AD_UNEXPECTED_MESSAGE;
1225 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1228 #endif /* !OPENSSL_NO_EC */
1230 /* if it was signed, check the signature */
1234 * |pkt| now points to the beginning of the signature, so the difference
1235 * equals the length of the parameters.
1237 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1238 PACKET_remaining(&save_param_start) -
1239 PACKET_remaining(pkt))) {
1240 al = SSL_AD_INTERNAL_ERROR;
1241 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1245 if (SSL_USE_SIGALGS(s)) {
1246 unsigned char *sigalgs;
1248 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1249 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1252 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1259 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1265 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1266 || PACKET_remaining(pkt) != 0) {
1267 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1270 j = EVP_PKEY_size(pkey);
1272 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1277 * Check signature length
1279 if (PACKET_remaining(&signature) > (size_t)j) {
1280 /* wrong packet length */
1281 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1284 #ifndef OPENSSL_NO_RSA
1285 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1291 for (num = 2; num > 0; num--) {
1292 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1293 EVP_DigestInit_ex(&md_ctx, (num == 2)
1294 ? s->ctx->md5 : s->ctx->sha1, NULL);
1295 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1297 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1299 EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms),
1300 PACKET_remaining(¶ms));
1301 EVP_DigestFinal_ex(&md_ctx, q, &size);
1306 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1307 PACKET_remaining(&signature), pkey->pkey.rsa);
1308 if (verify_ret < 0) {
1309 al = SSL_AD_DECRYPT_ERROR;
1310 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1313 if (verify_ret == 0) {
1315 al = SSL_AD_DECRYPT_ERROR;
1316 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1322 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1323 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1325 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1327 EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms),
1328 PACKET_remaining(¶ms));
1329 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1330 PACKET_remaining(&signature), pkey) <= 0) {
1332 al = SSL_AD_DECRYPT_ERROR;
1333 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1338 /* aNULL, aSRP or PSK do not need public keys */
1339 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1340 /* Might be wrong key type, check it */
1341 if (ssl3_check_cert_and_algorithm(s))
1342 /* Otherwise this shouldn't happen */
1343 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1346 /* still data left over */
1347 if (PACKET_remaining(pkt) != 0) {
1348 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1352 EVP_PKEY_free(pkey);
1353 EVP_MD_CTX_cleanup(&md_ctx);
1354 return MSG_PROCESS_CONTINUE_READING;
1356 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1358 EVP_PKEY_free(pkey);
1359 #ifndef OPENSSL_NO_RSA
1362 #ifndef OPENSSL_NO_DH
1365 #ifndef OPENSSL_NO_EC
1366 BN_CTX_free(bn_ctx);
1367 EC_POINT_free(srvr_ecpoint);
1370 EVP_MD_CTX_cleanup(&md_ctx);
1371 statem_set_error(s);
1372 return MSG_PROCESS_ERROR;
1375 enum MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1377 int ret = MSG_PROCESS_ERROR;
1378 unsigned int list_len, ctype_num, i, name_len;
1379 X509_NAME *xn = NULL;
1380 unsigned char *data;
1381 unsigned char *namestart, *namebytes;
1382 STACK_OF(X509_NAME) *ca_sk = NULL;
1384 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1385 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1389 /* get the certificate types */
1390 if (!PACKET_get_1(pkt, &ctype_num)
1391 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1392 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1393 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1396 OPENSSL_free(s->cert->ctypes);
1397 s->cert->ctypes = NULL;
1398 if (ctype_num > SSL3_CT_NUMBER) {
1399 /* If we exceed static buffer copy all to cert structure */
1400 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1401 if (s->cert->ctypes == NULL) {
1402 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1405 memcpy(s->cert->ctypes, data, ctype_num);
1406 s->cert->ctype_num = (size_t)ctype_num;
1407 ctype_num = SSL3_CT_NUMBER;
1409 for (i = 0; i < ctype_num; i++)
1410 s->s3->tmp.ctype[i] = data[i];
1412 if (SSL_USE_SIGALGS(s)) {
1413 if (!PACKET_get_net_2(pkt, &list_len)
1414 || !PACKET_get_bytes(pkt, &data, list_len)) {
1415 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1416 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1417 SSL_R_LENGTH_MISMATCH);
1421 /* Clear certificate digests and validity flags */
1422 for (i = 0; i < SSL_PKEY_NUM; i++) {
1423 s->s3->tmp.md[i] = NULL;
1424 s->s3->tmp.valid_flags[i] = 0;
1426 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1427 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1428 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1429 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1432 if (!tls1_process_sigalgs(s)) {
1433 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1434 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1439 /* get the CA RDNs */
1440 if (!PACKET_get_net_2(pkt, &list_len)
1441 || PACKET_remaining(pkt) != list_len) {
1442 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1443 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1447 while (PACKET_remaining(pkt)) {
1448 if (!PACKET_get_net_2(pkt, &name_len)
1449 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
1450 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1451 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1452 SSL_R_LENGTH_MISMATCH);
1456 namestart = namebytes;
1458 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1459 name_len)) == NULL) {
1460 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1461 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
1465 if (namebytes != (namestart + name_len)) {
1466 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1467 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1468 SSL_R_CA_DN_LENGTH_MISMATCH);
1471 if (!sk_X509_NAME_push(ca_sk, xn)) {
1472 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1477 /* we should setup a certificate to return.... */
1478 s->s3->tmp.cert_req = 1;
1479 s->s3->tmp.ctype_num = ctype_num;
1480 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1481 s->s3->tmp.ca_names = ca_sk;
1484 ret = MSG_PROCESS_CONTINUE_READING;
1487 statem_set_error(s);
1489 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1493 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1495 return (X509_NAME_cmp(*a, *b));
1498 enum MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
1501 unsigned int ticklen;
1502 unsigned long ticket_lifetime_hint;
1504 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1505 || !PACKET_get_net_2(pkt, &ticklen)
1506 || PACKET_remaining(pkt) != ticklen) {
1507 al = SSL_AD_DECODE_ERROR;
1508 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1512 /* Server is allowed to change its mind and send an empty ticket. */
1516 if (s->session->session_id_length > 0) {
1517 int i = s->session_ctx->session_cache_mode;
1518 SSL_SESSION *new_sess;
1520 * We reused an existing session, so we need to replace it with a new
1523 if (i & SSL_SESS_CACHE_CLIENT) {
1525 * Remove the old session from the cache
1527 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
1528 if (s->session_ctx->remove_session_cb != NULL)
1529 s->session_ctx->remove_session_cb(s->session_ctx,
1532 /* We carry on if this fails */
1533 SSL_CTX_remove_session(s->session_ctx, s->session);
1537 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1538 al = SSL_AD_INTERNAL_ERROR;
1539 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1543 SSL_SESSION_free(s->session);
1544 s->session = new_sess;
1547 OPENSSL_free(s->session->tlsext_tick);
1548 s->session->tlsext_ticklen = 0;
1550 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1551 if (!s->session->tlsext_tick) {
1552 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
1555 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
1556 al = SSL_AD_DECODE_ERROR;
1557 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1561 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1562 s->session->tlsext_ticklen = ticklen;
1564 * There are two ways to detect a resumed ticket session. One is to set
1565 * an appropriate session ID and then the server must return a match in
1566 * ServerHello. This allows the normal client session ID matching to work
1567 * and we know much earlier that the ticket has been accepted. The
1568 * other way is to set zero length session ID when the ticket is
1569 * presented and rely on the handshake to determine session resumption.
1570 * We choose the former approach because this fits in with assumptions
1571 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1572 * SHA256 is disabled) hash of the ticket.
1574 EVP_Digest(s->session->tlsext_tick, ticklen,
1575 s->session->session_id, &s->session->session_id_length,
1576 EVP_sha256(), NULL);
1577 return MSG_PROCESS_CONTINUE_READING;
1579 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1581 statem_set_error(s);
1582 return MSG_PROCESS_ERROR;
1585 enum MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
1588 unsigned long resplen;
1591 if (!PACKET_get_1(pkt, &type)
1592 || type != TLSEXT_STATUSTYPE_ocsp) {
1593 al = SSL_AD_DECODE_ERROR;
1594 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
1597 if (!PACKET_get_net_3(pkt, &resplen)
1598 || PACKET_remaining(pkt) != resplen) {
1599 al = SSL_AD_DECODE_ERROR;
1600 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1603 OPENSSL_free(s->tlsext_ocsp_resp);
1604 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
1605 if (!s->tlsext_ocsp_resp) {
1606 al = SSL_AD_INTERNAL_ERROR;
1607 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1610 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
1611 al = SSL_AD_DECODE_ERROR;
1612 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
1615 s->tlsext_ocsp_resplen = resplen;
1616 if (s->ctx->tlsext_status_cb) {
1618 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1620 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1621 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
1625 al = SSL_AD_INTERNAL_ERROR;
1626 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
1630 return MSG_PROCESS_CONTINUE_READING;
1632 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1633 statem_set_error(s);
1634 return MSG_PROCESS_ERROR;
1637 enum MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
1639 if (PACKET_remaining(pkt) > 0) {
1640 /* should contain no data */
1641 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1642 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1643 statem_set_error(s);
1644 return MSG_PROCESS_ERROR;
1647 #ifndef OPENSSL_NO_SRP
1648 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
1649 if (SRP_Calc_A_param(s) <= 0) {
1650 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
1651 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1652 statem_set_error(s);
1653 return MSG_PROCESS_ERROR;
1658 #ifndef OPENSSL_NO_SCTP
1659 /* Only applies to renegotiation */
1660 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
1661 && s->renegotiate != 0)
1662 return MSG_PROCESS_CONTINUE_PROCESSING;
1665 return MSG_PROCESS_FINISHED_READING;
1668 int tls_construct_client_key_exchange(SSL *s)
1672 #ifndef OPENSSL_NO_PSK
1673 size_t pskhdrlen = 0;
1675 unsigned long alg_k;
1676 #ifndef OPENSSL_NO_RSA
1678 EVP_PKEY *pkey = NULL;
1680 #ifndef OPENSSL_NO_EC
1681 EC_KEY *clnt_ecdh = NULL;
1682 const EC_POINT *srvr_ecpoint = NULL;
1683 EVP_PKEY *srvr_pub_pkey = NULL;
1684 unsigned char *encodedPoint = NULL;
1685 int encoded_pt_len = 0;
1686 BN_CTX *bn_ctx = NULL;
1688 unsigned char *pms = NULL;
1690 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1692 p = ssl_handshake_start(s);
1695 #ifndef OPENSSL_NO_PSK
1696 if (alg_k & SSL_PSK) {
1699 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
1700 * \0-terminated identity. The last byte is for us for simulating
1703 char identity[PSK_MAX_IDENTITY_LEN + 1];
1705 unsigned char psk[PSK_MAX_PSK_LEN];
1708 if (s->psk_client_callback == NULL) {
1709 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1710 SSL_R_PSK_NO_CLIENT_CB);
1714 memset(identity, 0, sizeof(identity));
1716 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
1717 identity, sizeof(identity) - 1,
1720 if (psklen > PSK_MAX_PSK_LEN) {
1721 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1722 ERR_R_INTERNAL_ERROR);
1724 } else if (psklen == 0) {
1725 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1726 SSL_R_PSK_IDENTITY_NOT_FOUND);
1730 OPENSSL_free(s->s3->tmp.psk);
1731 s->s3->tmp.psk = BUF_memdup(psk, psklen);
1732 OPENSSL_cleanse(psk, psklen);
1734 if (s->s3->tmp.psk == NULL) {
1735 OPENSSL_cleanse(identity, sizeof(identity));
1739 s->s3->tmp.psklen = psklen;
1741 identitylen = strlen(identity);
1742 if (identitylen > PSK_MAX_IDENTITY_LEN) {
1743 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1744 ERR_R_INTERNAL_ERROR);
1747 OPENSSL_free(s->session->psk_identity);
1748 s->session->psk_identity = BUF_strdup(identity);
1749 if (s->session->psk_identity == NULL) {
1750 OPENSSL_cleanse(identity, sizeof(identity));
1754 s2n(identitylen, p);
1755 memcpy(p, identity, identitylen);
1756 pskhdrlen = 2 + identitylen;
1760 OPENSSL_cleanse(identity, sizeof(identity));
1762 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1766 if (alg_k & SSL_kPSK) {
1771 /* Fool emacs indentation */
1774 #ifndef OPENSSL_NO_RSA
1775 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
1777 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
1778 pms = OPENSSL_malloc(pmslen);
1782 if (s->session->peer == NULL) {
1784 * We should always have a server certificate with SSL_kRSA.
1786 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1787 ERR_R_INTERNAL_ERROR);
1791 if (s->s3->peer_rsa_tmp != NULL)
1792 rsa = s->s3->peer_rsa_tmp;
1794 pkey = X509_get_pubkey(s->session->peer);
1795 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
1796 || (pkey->pkey.rsa == NULL)) {
1797 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1798 ERR_R_INTERNAL_ERROR);
1799 EVP_PKEY_free(pkey);
1802 rsa = pkey->pkey.rsa;
1803 EVP_PKEY_free(pkey);
1806 pms[0] = s->client_version >> 8;
1807 pms[1] = s->client_version & 0xff;
1808 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
1812 /* Fix buf for TLS and beyond */
1813 if (s->version > SSL3_VERSION)
1815 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
1817 if (s->options & SSL_OP_PKCS1_CHECK_1)
1819 if (s->options & SSL_OP_PKCS1_CHECK_2)
1823 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1824 SSL_R_BAD_RSA_ENCRYPT);
1828 /* Fix buf for TLS and beyond */
1829 if (s->version > SSL3_VERSION) {
1835 #ifndef OPENSSL_NO_DH
1836 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
1837 DH *dh_srvr, *dh_clnt;
1838 if (s->s3->peer_dh_tmp != NULL)
1839 dh_srvr = s->s3->peer_dh_tmp;
1841 /* we get them from the cert */
1842 EVP_PKEY *spkey = NULL;
1844 spkey = X509_get_pubkey(s->session->peer);
1846 dh_srvr = EVP_PKEY_get1_DH(spkey);
1847 EVP_PKEY_free(spkey);
1849 if (dh_srvr == NULL) {
1850 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1851 ERR_R_INTERNAL_ERROR);
1855 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
1856 /* Use client certificate key */
1857 EVP_PKEY *clkey = s->cert->key->privatekey;
1860 dh_clnt = EVP_PKEY_get1_DH(clkey);
1861 if (dh_clnt == NULL) {
1862 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1863 ERR_R_INTERNAL_ERROR);
1867 /* generate a new random key */
1868 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1869 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1872 if (!DH_generate_key(dh_clnt)) {
1873 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1879 pmslen = DH_size(dh_clnt);
1880 pms = OPENSSL_malloc(pmslen);
1885 * use the 'p' output buffer for the DH key, but make sure to
1886 * clear it out afterwards
1889 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1890 if (s->s3->peer_dh_tmp == NULL)
1894 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1900 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
1903 /* send off the data */
1904 n = BN_num_bytes(dh_clnt->pub_key);
1906 BN_bn2bin(dh_clnt->pub_key, p);
1914 #ifndef OPENSSL_NO_EC
1915 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
1916 const EC_GROUP *srvr_group = NULL;
1918 int ecdh_clnt_cert = 0;
1921 * Did we send out the client's ECDH share for use in premaster
1922 * computation as part of client certificate? If so, set
1923 * ecdh_clnt_cert to 1.
1925 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
1927 * XXX: For now, we do not support client
1928 * authentication using ECDH certificates.
1929 * To add such support, one needs to add
1930 * code that checks for appropriate
1931 * conditions and sets ecdh_clnt_cert to 1.
1932 * For example, the cert have an ECC
1933 * key on the same curve as the server's
1934 * and the key should be authorized for
1937 * One also needs to add code in ssl3_connect
1938 * to skip sending the certificate verify
1941 * if ((s->cert->key->privatekey != NULL) &&
1942 * (s->cert->key->privatekey->type ==
1943 * EVP_PKEY_EC) && ...)
1944 * ecdh_clnt_cert = 1;
1948 if (s->s3->peer_ecdh_tmp != NULL) {
1949 tkey = s->s3->peer_ecdh_tmp;
1951 /* Get the Server Public Key from Cert */
1952 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
1953 if ((srvr_pub_pkey == NULL)
1954 || (srvr_pub_pkey->type != EVP_PKEY_EC)
1955 || (srvr_pub_pkey->pkey.ec == NULL)) {
1956 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1957 ERR_R_INTERNAL_ERROR);
1961 tkey = srvr_pub_pkey->pkey.ec;
1964 srvr_group = EC_KEY_get0_group(tkey);
1965 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1967 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
1968 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1969 ERR_R_INTERNAL_ERROR);
1973 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
1974 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1975 ERR_R_MALLOC_FAILURE);
1979 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1980 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
1983 if (ecdh_clnt_cert) {
1985 * Reuse key info from our certificate We only need our
1986 * private key to perform the ECDH computation.
1988 const BIGNUM *priv_key;
1989 tkey = s->cert->key->privatekey->pkey.ec;
1990 priv_key = EC_KEY_get0_private_key(tkey);
1991 if (priv_key == NULL) {
1992 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1993 ERR_R_MALLOC_FAILURE);
1996 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
1997 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2001 /* Generate a new ECDH key pair */
2002 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2003 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2010 * use the 'p' output buffer for the ECDH key, but make sure to
2011 * clear it out afterwards
2014 field_size = EC_GROUP_get_degree(srvr_group);
2015 if (field_size <= 0) {
2016 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2019 pmslen = (field_size + 7) / 8;
2020 pms = OPENSSL_malloc(pmslen);
2023 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2024 if (n <= 0 || pmslen != (size_t)n) {
2025 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2029 if (ecdh_clnt_cert) {
2030 /* Send empty client key exch message */
2034 * First check the size of encoding and allocate memory
2038 EC_POINT_point2oct(srvr_group,
2039 EC_KEY_get0_public_key(clnt_ecdh),
2040 POINT_CONVERSION_UNCOMPRESSED,
2043 encodedPoint = (unsigned char *)
2044 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2045 bn_ctx = BN_CTX_new();
2046 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2047 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2048 ERR_R_MALLOC_FAILURE);
2052 /* Encode the public key */
2053 n = EC_POINT_point2oct(srvr_group,
2054 EC_KEY_get0_public_key(clnt_ecdh),
2055 POINT_CONVERSION_UNCOMPRESSED,
2056 encodedPoint, encoded_pt_len, bn_ctx);
2058 *p = n; /* length of encoded point */
2059 /* Encoded point will be copied here */
2061 /* copy the point */
2062 memcpy(p, encodedPoint, n);
2063 /* increment n to account for length field */
2067 /* Free allocated memory */
2068 BN_CTX_free(bn_ctx);
2069 OPENSSL_free(encodedPoint);
2070 EC_KEY_free(clnt_ecdh);
2071 EVP_PKEY_free(srvr_pub_pkey);
2073 #endif /* !OPENSSL_NO_EC */
2074 else if (alg_k & SSL_kGOST) {
2075 /* GOST key exchange message creation */
2076 EVP_PKEY_CTX *pkey_ctx;
2079 unsigned int md_len;
2080 unsigned char shared_ukm[32], tmp[256];
2081 EVP_MD_CTX *ukm_hash;
2085 pms = OPENSSL_malloc(pmslen);
2090 * Get server sertificate PKEY and create ctx from it
2092 peer_cert = s->session->peer;
2094 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2095 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2099 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2100 X509_get_pubkey(peer_cert), NULL);
2102 * If we have send a certificate, and certificate key
2104 * * parameters match those of server certificate, use
2105 * certificate key for key exchange
2108 /* Otherwise, generate ephemeral key pair */
2110 EVP_PKEY_encrypt_init(pkey_ctx);
2111 /* Generate session key */
2112 if (RAND_bytes(pms, pmslen) <= 0) {
2113 EVP_PKEY_CTX_free(pkey_ctx);
2114 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2115 ERR_R_INTERNAL_ERROR);
2119 * If we have client certificate, use its secret as peer key
2121 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2122 if (EVP_PKEY_derive_set_peer
2123 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2125 * If there was an error - just ignore it. Ephemeral key
2132 * Compute shared IV and store it in algorithm-specific context
2135 ukm_hash = EVP_MD_CTX_create();
2136 EVP_DigestInit(ukm_hash,
2137 EVP_get_digestbynid(NID_id_GostR3411_94));
2138 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2140 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2142 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2143 EVP_MD_CTX_destroy(ukm_hash);
2144 if (EVP_PKEY_CTX_ctrl
2145 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2147 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2151 /* Make GOST keytransport blob message */
2153 * Encapsulate it into sequence
2155 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2157 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2158 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2162 if (msglen >= 0x80) {
2164 *(p++) = msglen & 0xff;
2167 *(p++) = msglen & 0xff;
2170 memcpy(p, tmp, msglen);
2171 /* Check if pubkey from client certificate was used */
2172 if (EVP_PKEY_CTX_ctrl
2173 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2174 /* Set flag "skip certificate verify" */
2175 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2177 EVP_PKEY_CTX_free(pkey_ctx);
2178 EVP_PKEY_free(pub_key);
2181 #ifndef OPENSSL_NO_SRP
2182 else if (alg_k & SSL_kSRP) {
2183 if (s->srp_ctx.A != NULL) {
2184 /* send off the data */
2185 n = BN_num_bytes(s->srp_ctx.A);
2187 BN_bn2bin(s->srp_ctx.A, p);
2190 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2191 ERR_R_INTERNAL_ERROR);
2194 OPENSSL_free(s->session->srp_username);
2195 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2196 if (s->session->srp_username == NULL) {
2197 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2198 ERR_R_MALLOC_FAILURE);
2204 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2205 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2209 #ifndef OPENSSL_NO_PSK
2213 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2214 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2215 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2219 s->s3->tmp.pms = pms;
2220 s->s3->tmp.pmslen = pmslen;
2224 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2225 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2227 OPENSSL_clear_free(pms, pmslen);
2228 s->s3->tmp.pms = NULL;
2229 #ifndef OPENSSL_NO_EC
2230 BN_CTX_free(bn_ctx);
2231 OPENSSL_free(encodedPoint);
2232 EC_KEY_free(clnt_ecdh);
2233 EVP_PKEY_free(srvr_pub_pkey);
2235 #ifndef OPENSSL_NO_PSK
2236 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2237 s->s3->tmp.psk = NULL;
2239 statem_set_error(s);
2243 int tls_client_key_exchange_post_work(SSL *s)
2245 unsigned char *pms = NULL;
2248 #ifndef OPENSSL_NO_SRP
2250 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2251 if (!srp_generate_client_master_secret(s)) {
2252 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2253 ERR_R_INTERNAL_ERROR);
2259 pms = s->s3->tmp.pms;
2260 pmslen = s->s3->tmp.pmslen;
2262 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2263 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2264 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2267 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2268 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2269 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2273 #ifndef OPENSSL_NO_SCTP
2274 if (SSL_IS_DTLS(s)) {
2275 unsigned char sctpauthkey[64];
2276 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2279 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2282 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
2283 DTLS1_SCTP_AUTH_LABEL);
2285 if (SSL_export_keying_material(s, sctpauthkey,
2286 sizeof(sctpauthkey), labelbuffer,
2287 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2290 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2291 sizeof(sctpauthkey), sctpauthkey);
2297 OPENSSL_clear_free(pms, pmslen);
2298 s->s3->tmp.pms = NULL;
2302 int tls_construct_client_verify(SSL *s)
2305 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2307 EVP_PKEY_CTX *pctx = NULL;
2313 EVP_MD_CTX_init(&mctx);
2315 p = ssl_handshake_start(s);
2316 pkey = s->cert->key->privatekey;
2317 /* Create context from key and test if sha1 is allowed as digest */
2318 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2319 EVP_PKEY_sign_init(pctx);
2320 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2321 if (!SSL_USE_SIGALGS(s))
2322 s->method->ssl3_enc->cert_verify_mac(s,
2325 [MD5_DIGEST_LENGTH]));
2330 * For TLS v1.2 send signature algorithm and signature using agreed
2331 * digest and cached handshake records.
2333 if (SSL_USE_SIGALGS(s)) {
2336 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2337 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2338 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
2339 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2344 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
2347 if (!EVP_SignInit_ex(&mctx, md, NULL)
2348 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
2349 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2350 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2355 /* Digest cached records and discard handshake buffer */
2356 if (!ssl3_digest_cached_records(s, 0))
2359 #ifndef OPENSSL_NO_RSA
2360 if (pkey->type == EVP_PKEY_RSA) {
2361 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
2362 if (RSA_sign(NID_md5_sha1, data,
2363 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
2364 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2365 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
2372 #ifndef OPENSSL_NO_DSA
2373 if (pkey->type == EVP_PKEY_DSA) {
2374 if (!DSA_sign(pkey->save_type,
2375 &(data[MD5_DIGEST_LENGTH]),
2376 SHA_DIGEST_LENGTH, &(p[2]),
2377 (unsigned int *)&j, pkey->pkey.dsa)) {
2378 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
2385 #ifndef OPENSSL_NO_EC
2386 if (pkey->type == EVP_PKEY_EC) {
2387 if (!ECDSA_sign(pkey->save_type,
2388 &(data[MD5_DIGEST_LENGTH]),
2389 SHA_DIGEST_LENGTH, &(p[2]),
2390 (unsigned int *)&j, pkey->pkey.ec)) {
2391 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
2398 if (pkey->type == NID_id_GostR3410_2001) {
2399 unsigned char signbuf[64];
2401 size_t sigsize = 64;
2402 s->method->ssl3_enc->cert_verify_mac(s,
2403 NID_id_GostR3411_94, data);
2404 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
2405 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2408 for (i = 63, j = 0; i >= 0; j++, i--) {
2409 p[2 + j] = signbuf[i];
2414 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2417 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2418 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2422 EVP_MD_CTX_cleanup(&mctx);
2423 EVP_PKEY_CTX_free(pctx);
2426 EVP_MD_CTX_cleanup(&mctx);
2427 EVP_PKEY_CTX_free(pctx);
2432 * Check a certificate can be used for client authentication. Currently check
2433 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2434 * certificates can be used and optionally checks suitability for Suite B.
2436 static int ssl3_check_client_certificate(SSL *s)
2438 unsigned long alg_k;
2439 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2441 /* If no suitable signature algorithm can't use certificate */
2442 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2445 * If strict mode check suitability of chain before using it. This also
2446 * adjusts suite B digest if necessary.
2448 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2449 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2451 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2452 /* See if we can use client certificate for fixed DH */
2453 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
2454 int i = s->session->peer_type;
2455 EVP_PKEY *clkey = NULL, *spkey = NULL;
2456 clkey = s->cert->key->privatekey;
2457 /* If client key not DH assume it can be used */
2458 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2461 spkey = X509_get_pubkey(s->session->peer);
2463 /* Compare server and client parameters */
2464 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2465 EVP_PKEY_free(spkey);
2469 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2474 enum WORK_STATE tls_prepare_client_certificate(SSL *s, enum WORK_STATE wst)
2477 EVP_PKEY *pkey = NULL;
2480 if (wst == WORK_MORE_A) {
2481 /* Let cert callback update client certificates if required */
2482 if (s->cert->cert_cb) {
2483 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2485 s->rwstate = SSL_X509_LOOKUP;
2489 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2490 statem_set_error(s);
2493 s->rwstate = SSL_NOTHING;
2495 if (ssl3_check_client_certificate(s))
2496 return WORK_FINISHED_CONTINUE;
2498 /* Fall through to WORK_MORE_B */
2502 /* We need to get a client cert */
2503 if (wst == WORK_MORE_B) {
2505 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2506 * return(-1); We then get retied later
2508 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2510 s->rwstate = SSL_X509_LOOKUP;
2513 s->rwstate = SSL_NOTHING;
2514 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2515 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2517 } else if (i == 1) {
2519 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2520 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2524 EVP_PKEY_free(pkey);
2525 if (i && !ssl3_check_client_certificate(s))
2528 if (s->version == SSL3_VERSION) {
2529 s->s3->tmp.cert_req = 0;
2530 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2531 return WORK_FINISHED_CONTINUE;
2533 s->s3->tmp.cert_req = 2;
2534 if (!ssl3_digest_cached_records(s, 0)) {
2535 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2536 statem_set_error(s);
2542 return WORK_FINISHED_CONTINUE;
2545 /* Shouldn't ever get here */
2549 int tls_construct_client_certificate(SSL *s)
2551 if (!ssl3_output_cert_chain(s,
2552 (s->s3->tmp.cert_req ==
2553 2) ? NULL : s->cert->key)) {
2554 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2555 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2556 statem_set_error(s);
2563 #define has_bits(i,m) (((i)&(m)) == (m))
2565 int ssl3_check_cert_and_algorithm(SSL *s)
2569 EVP_PKEY *pkey = NULL;
2571 #ifndef OPENSSL_NO_RSA
2574 #ifndef OPENSSL_NO_DH
2577 int al = SSL_AD_HANDSHAKE_FAILURE;
2579 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2580 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2582 /* we don't have a certificate */
2583 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2585 #ifndef OPENSSL_NO_RSA
2586 rsa = s->s3->peer_rsa_tmp;
2588 #ifndef OPENSSL_NO_DH
2589 dh = s->s3->peer_dh_tmp;
2592 /* This is the passed certificate */
2594 idx = s->session->peer_type;
2595 #ifndef OPENSSL_NO_EC
2596 if (idx == SSL_PKEY_ECC) {
2597 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2599 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2604 } else if (alg_a & SSL_aECDSA) {
2605 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2606 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2608 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2609 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
2613 pkey = X509_get_pubkey(s->session->peer);
2614 pkey_bits = EVP_PKEY_bits(pkey);
2615 i = X509_certificate_type(s->session->peer, pkey);
2616 EVP_PKEY_free(pkey);
2618 /* Check that we have a certificate if we require one */
2619 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2620 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2621 SSL_R_MISSING_RSA_SIGNING_CERT);
2624 #ifndef OPENSSL_NO_DSA
2625 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2626 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2627 SSL_R_MISSING_DSA_SIGNING_CERT);
2631 #ifndef OPENSSL_NO_RSA
2632 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2633 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
2634 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2635 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2636 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2638 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
2639 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
2640 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2641 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2642 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2646 /* server key exchange is not allowed. */
2647 al = SSL_AD_INTERNAL_ERROR;
2648 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2655 #ifndef OPENSSL_NO_DH
2656 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
2657 al = SSL_AD_INTERNAL_ERROR;
2658 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2660 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2661 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
2662 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2663 SSL_R_MISSING_DH_RSA_CERT);
2666 # ifndef OPENSSL_NO_DSA
2667 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2668 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
2669 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2670 SSL_R_MISSING_DH_DSA_CERT);
2676 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
2677 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
2678 #ifndef OPENSSL_NO_RSA
2679 if (alg_k & SSL_kRSA) {
2681 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2682 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2684 } else if (RSA_bits(rsa) >
2685 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
2686 /* We have a temporary RSA key but it's too large. */
2687 al = SSL_AD_EXPORT_RESTRICTION;
2688 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2689 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2694 #ifndef OPENSSL_NO_DH
2695 if (alg_k & SSL_kDHE) {
2697 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
2698 /* We have a temporary DH key but it's too large. */
2699 al = SSL_AD_EXPORT_RESTRICTION;
2700 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2701 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2704 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
2705 /* The cert should have had an export DH key. */
2706 al = SSL_AD_EXPORT_RESTRICTION;
2707 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2708 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2713 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2714 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2720 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2724 #ifndef OPENSSL_NO_NEXTPROTONEG
2725 int tls_construct_next_proto(SSL *s)
2727 unsigned int len, padding_len;
2730 len = s->next_proto_negotiated_len;
2731 padding_len = 32 - ((len + 2) % 32);
2732 d = (unsigned char *)s->init_buf->data;
2734 memcpy(d + 5, s->next_proto_negotiated, len);
2735 d[5 + len] = padding_len;
2736 memset(d + 6 + len, 0, padding_len);
2737 *(d++) = SSL3_MT_NEXT_PROTO;
2738 l2n3(2 + len + padding_len, d);
2739 s->init_num = 4 + 2 + len + padding_len;
2746 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2749 #ifndef OPENSSL_NO_ENGINE
2750 if (s->ctx->client_cert_engine) {
2751 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2752 SSL_get_client_CA_list(s),
2753 px509, ppkey, NULL, NULL, NULL);
2758 if (s->ctx->client_cert_cb)
2759 i = s->ctx->client_cert_cb(s, px509, ppkey);
2763 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
2769 int empty_reneg_info_scsv = !s->renegotiate;
2770 /* Set disabled masks for this session */
2771 ssl_set_client_disabled(s);
2777 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2778 c = sk_SSL_CIPHER_value(sk, i);
2779 /* Skip disabled ciphers */
2780 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2782 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2783 if (c->id == SSL3_CK_SCSV) {
2784 if (!empty_reneg_info_scsv)
2787 empty_reneg_info_scsv = 0;
2790 j = s->method->put_cipher_by_char(c, p);
2794 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2798 if (empty_reneg_info_scsv) {
2799 static SSL_CIPHER scsv = {
2800 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2802 j = s->method->put_cipher_by_char(&scsv, p);
2804 #ifdef OPENSSL_RI_DEBUG
2806 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
2809 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2810 static SSL_CIPHER scsv = {
2811 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2813 j = s->method->put_cipher_by_char(&scsv, p);