]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_clnt.c
Reorganise state machine files
[thirdparty/openssl.git] / ssl / statem / statem_clnt.c
CommitLineData
8ba708e5 1/* ssl/statem/statem_clnt.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
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.
0f113f3e 8 *
d02b48c6
RE
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).
0f113f3e 15 *
d02b48c6
RE
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.
0f113f3e 22 *
d02b48c6
RE
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
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 :-).
0f113f3e 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 40 *
d02b48c6
RE
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
51 * SUCH DAMAGE.
0f113f3e 52 *
d02b48c6
RE
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.]
57 */
8c74b5e5 58/* ====================================================================
52b8dad8 59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
8c74b5e5
BM
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
0f113f3e 66 * notice, this list of conditions and the following disclaimer.
8c74b5e5
BM
67 *
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
71 * distribution.
72 *
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/)"
77 *
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.
82 *
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.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
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 * ====================================================================
105 *
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).
109 *
110 */
ea262260
BM
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
0f113f3e 114 * Portions of the attached software ("Contribution") are developed by
ea262260
BM
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
ea262260
BM
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
ddac1974
NL
124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
130 *
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.
134 *
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.
138 *
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.
143 *
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
148 * OTHERWISE.
149 */
d02b48c6
RE
150
151#include <stdio.h>
8ba708e5 152#include "../ssl_locl.h"
ec577822
BM
153#include <openssl/buffer.h>
154#include <openssl/rand.h>
155#include <openssl/objects.h>
156#include <openssl/evp.h>
dbad1690 157#include <openssl/md5.h>
3eeaab4b 158#ifndef OPENSSL_NO_DH
0f113f3e 159# include <openssl/dh.h>
3eeaab4b 160#endif
d095b68d 161#include <openssl/bn.h>
368888bc 162#ifndef OPENSSL_NO_ENGINE
0f113f3e 163# include <openssl/engine.h>
368888bc 164#endif
f9b3bff6 165
d45ba43d 166static int ssl_set_version(SSL *s);
0f113f3e 167static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
d45ba43d 168static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
e9fa092e 169 unsigned char *p);
ea262260 170
d45ba43d
MC
171/*
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
175 * Returns 0 on error
176 */
177static int ssl_set_version(SSL *s)
178{
179 unsigned long mask, options = s->options;
180
181 if (s->method->version == TLS_ANY_VERSION) {
182 /*
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.
189 */
190 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
191#if !defined(OPENSSL_NO_SSL3)
192 | SSL_OP_NO_SSLv3
193#endif
194 ;
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;
199 } else {
200 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
201 return 0;
202 }
203 } else {
204 s->version = TLS1_2_VERSION;
205 }
206#else
207 if ((options & mask) == mask) {
208 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
209 return 0;
210 }
211 s->version = TLS1_1_VERSION;
212#endif
213
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;
221#endif
222
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);
226 return 0;
227 }
228
229 if (s->version == SSL3_VERSION && FIPS_mode()) {
230 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
231 return 0;
232 }
233
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);
241 return 0;
242 }
243 /*
244 * Disabling all versions is silly: return an error.
245 */
246 if (options & SSL_OP_NO_DTLSv1) {
247 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
248 return 0;
249 }
250 /*
251 * Update method so we don't use any DTLS 1.2 features.
252 */
253 s->method = DTLSv1_client_method();
254 s->version = DTLS1_VERSION;
255 } else {
256 /*
257 * We only support one version: update method
258 */
259 if (options & SSL_OP_NO_DTLSv1)
260 s->method = DTLSv1_2_client_method();
261 s->version = DTLS1_2_VERSION;
262 }
263 }
264
265 s->client_version = s->version;
266
267 return 1;
268}
269
b9908bf9 270int tls_construct_client_hello(SSL *s)
0f113f3e
MC
271{
272 unsigned char *buf;
273 unsigned char *p, *d;
274 int i;
275 unsigned long l;
276 int al = 0;
09b6c2ef 277#ifndef OPENSSL_NO_COMP
0f113f3e
MC
278 int j;
279 SSL_COMP *comp;
280#endif
b9908bf9 281 SSL_SESSION *sess = s->session;
0f113f3e
MC
282
283 buf = (unsigned char *)s->init_buf->data;
0f113f3e 284
b9908bf9
MC
285 /* Work out what SSL/TLS/DTLS version to use */
286 if (ssl_set_version(s) == 0)
287 goto err;
0f113f3e 288
b9908bf9 289 if ((sess == NULL) || (sess->ssl_version != s->version) ||
0f113f3e 290 /*
b9908bf9
MC
291 * In the case of EAP-FAST, we can have a pre-shared
292 * "ticket" without a session ID.
0f113f3e 293 */
b9908bf9
MC
294 (!sess->session_id_length && !sess->tlsext_tick) ||
295 (sess->not_resumable)) {
296 if (!ssl_get_new_session(s, 0))
e1b568dd 297 goto err;
b9908bf9
MC
298 }
299 /* else use the pre-loaded session */
0f113f3e 300
b9908bf9 301 p = s->s3->client_random;
0f113f3e 302
b9908bf9
MC
303 /*
304 * for DTLS if client_random is initialized, reuse it, we are
305 * required to use same upon reply to HelloVerify
306 */
307 if (SSL_IS_DTLS(s)) {
308 size_t idx;
309 i = 1;
310 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
311 if (p[idx]) {
312 i = 0;
313 break;
0f113f3e 314 }
0f113f3e 315 }
b9908bf9
MC
316 } else
317 i = 1;
0f113f3e 318
b9908bf9
MC
319 if (i && ssl_fill_hello_random(s, 0, p,
320 sizeof(s->s3->client_random)) <= 0)
321 goto err;
322
323 /* Do the message type and length last */
324 d = p = ssl_handshake_start(s);
325
326 /*-
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
335 * 1.0.
336 *
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.
347 *
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.
355 */
356 *(p++) = s->client_version >> 8;
357 *(p++) = s->client_version & 0xff;
358
359 /* Random stuff */
360 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
361 p += SSL3_RANDOM_SIZE;
362
363 /* Session ID */
364 if (s->new_session)
365 i = 0;
366 else
367 i = s->session->session_id_length;
368 *(p++) = i;
369 if (i != 0) {
370 if (i > (int)sizeof(s->session->session_id)) {
371 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
372 goto err;
0f113f3e 373 }
b9908bf9
MC
374 memcpy(p, s->session->session_id, i);
375 p += i;
376 }
0f113f3e 377
b9908bf9
MC
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);
0f113f3e
MC
382 goto err;
383 }
b9908bf9
MC
384 *(p++) = s->d1->cookie_len;
385 memcpy(p, s->d1->cookie, s->d1->cookie_len);
386 p += s->d1->cookie_len;
387 }
388
389 /* Ciphers supported */
390 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
391 if (i == 0) {
392 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
393 goto err;
394 }
800e1cd9 395#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
b9908bf9
MC
396 /*
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
399 * use TLS v1.2
400 */
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;
0f113f3e 404#endif
b9908bf9
MC
405 s2n(i, p);
406 p += i;
0f113f3e 407
b9908bf9 408 /* COMPRESSION */
09b6c2ef 409#ifdef OPENSSL_NO_COMP
b9908bf9 410 *(p++) = 1;
09b6c2ef 411#else
566dda07 412
b9908bf9
MC
413 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
414 j = 0;
415 else
416 j = sk_SSL_COMP_num(s->ctx->comp_methods);
417 *(p++) = 1 + j;
418 for (i = 0; i < j; i++) {
419 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
420 *(p++) = comp->id;
421 }
09b6c2ef 422#endif
b9908bf9 423 *(p++) = 0; /* Add the NULL method */
761772d7 424
b9908bf9
MC
425 /* TLS extensions */
426 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
427 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
428 goto err;
429 }
430 if ((p =
431 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
432 &al)) == NULL) {
433 ssl3_send_alert(s, SSL3_AL_FATAL, al);
434 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
435 goto err;
436 }
0f113f3e 437
b9908bf9
MC
438 l = p - d;
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);
442 goto err;
0f113f3e
MC
443 }
444
b9908bf9 445 return 1;
0f113f3e 446 err:
b9908bf9
MC
447 statem_set_error(s);
448 return 0;
0f113f3e 449}
d02b48c6 450
8ba708e5
MC
451enum MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
452{
453 int al;
454 unsigned int cookie_len;
455 PACKET cookiepkt;
456
457 if (!PACKET_forward(pkt, 2)
458 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
459 al = SSL_AD_DECODE_ERROR;
460 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
461 goto f_err;
462 }
463
464 cookie_len = PACKET_remaining(&cookiepkt);
465 if (cookie_len > sizeof(s->d1->cookie)) {
466 al = SSL_AD_ILLEGAL_PARAMETER;
467 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
468 goto f_err;
469 }
470
471 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
472 al = SSL_AD_DECODE_ERROR;
473 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
474 goto f_err;
475 }
476 s->d1->cookie_len = cookie_len;
477
478 return MSG_PROCESS_FINISHED_READING;
479 f_err:
480 ssl3_send_alert(s, SSL3_AL_FATAL, al);
481 statem_set_error(s);
482 return MSG_PROCESS_ERROR;
483}
484
73999b62 485enum MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
b9908bf9
MC
486{
487 STACK_OF(SSL_CIPHER) *sk;
488 const SSL_CIPHER *c;
73999b62 489 PACKET session_id;
b9908bf9
MC
490 size_t session_id_len;
491 unsigned char *cipherchars;
492 int i, al = SSL_AD_INTERNAL_ERROR;
493 unsigned int compression;
494#ifndef OPENSSL_NO_COMP
495 SSL_COMP *comp;
496#endif
497
13c9bb3e 498 if (s->method->version == TLS_ANY_VERSION) {
50932c4a
MC
499 unsigned int sversion;
500
73999b62 501 if (!PACKET_get_net_2(pkt, &sversion)) {
50932c4a 502 al = SSL_AD_DECODE_ERROR;
b9908bf9 503 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
504 goto f_err;
505 }
13c9bb3e
MC
506
507#if TLS_MAX_VERSION != TLS1_2_VERSION
508#error Code needs updating for new TLS version
509#endif
510#ifndef OPENSSL_NO_SSL3
511 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
512 if (FIPS_mode()) {
b9908bf9 513 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
13c9bb3e 514 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
d45ba43d
MC
515 al = SSL_AD_PROTOCOL_VERSION;
516 goto f_err;
13c9bb3e
MC
517 }
518 s->method = SSLv3_client_method();
519 } else
520#endif
521 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
522 s->method = TLSv1_client_method();
523 } else if ((sversion == TLS1_1_VERSION) &&
524 !(s->options & SSL_OP_NO_TLSv1_1)) {
525 s->method = TLSv1_1_client_method();
526 } else if ((sversion == TLS1_2_VERSION) &&
527 !(s->options & SSL_OP_NO_TLSv1_2)) {
528 s->method = TLSv1_2_client_method();
529 } else {
b9908bf9 530 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
d45ba43d
MC
531 al = SSL_AD_PROTOCOL_VERSION;
532 goto f_err;
13c9bb3e
MC
533 }
534 s->session->ssl_version = s->version = s->method->version;
535
536 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
b9908bf9 537 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
d45ba43d
MC
538 al = SSL_AD_PROTOCOL_VERSION;
539 goto f_err;
13c9bb3e
MC
540 }
541 } else if (s->method->version == DTLS_ANY_VERSION) {
0f113f3e 542 /* Work out correct protocol version to use */
50932c4a
MC
543 unsigned int hversion;
544 int options;
545
73999b62 546 if (!PACKET_get_net_2(pkt, &hversion)) {
50932c4a 547 al = SSL_AD_DECODE_ERROR;
b9908bf9 548 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
549 goto f_err;
550 }
551
552 options = s->options;
0f113f3e
MC
553 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
554 s->method = DTLSv1_2_client_method();
555 else if (tls1_suiteb(s)) {
b9908bf9 556 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
557 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
558 s->version = hversion;
559 al = SSL_AD_PROTOCOL_VERSION;
560 goto f_err;
561 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
562 s->method = DTLSv1_client_method();
563 else {
b9908bf9 564 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
0f113f3e
MC
565 s->version = hversion;
566 al = SSL_AD_PROTOCOL_VERSION;
567 goto f_err;
568 }
7322abf5 569 s->session->ssl_version = s->version = s->method->version;
50932c4a
MC
570 } else {
571 unsigned char *vers;
572
73999b62 573 if (!PACKET_get_bytes(pkt, &vers, 2)) {
50932c4a 574 al = SSL_AD_DECODE_ERROR;
b9908bf9 575 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
576 goto f_err;
577 }
578 if ((vers[0] != (s->version >> 8))
579 || (vers[1] != (s->version & 0xff))) {
b9908bf9 580 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
50932c4a
MC
581 s->version = (s->version & 0xff00) | vers[1];
582 al = SSL_AD_PROTOCOL_VERSION;
583 goto f_err;
584 }
0f113f3e 585 }
0f113f3e
MC
586
587 /* load the server hello data */
588 /* load the server random */
73999b62 589 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
50932c4a 590 al = SSL_AD_DECODE_ERROR;
b9908bf9 591 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
592 goto f_err;
593 }
0f113f3e
MC
594
595 s->hit = 0;
596
fc5ce51d 597 /* Get the session-id. */
73999b62 598 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
fc5ce51d
EK
599 al = SSL_AD_DECODE_ERROR;
600 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
601 goto f_err;
602 }
603 session_id_len = PACKET_remaining(&session_id);
604 if (session_id_len > sizeof s->session->session_id
605 || session_id_len > SSL3_SESSION_ID_SIZE) {
0f113f3e 606 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 607 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
0f113f3e
MC
608 goto f_err;
609 }
e481f9b9 610
73999b62 611 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
fc5ce51d
EK
612 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
613 al = SSL_AD_DECODE_ERROR;
614 goto f_err;
615 }
616
0f113f3e 617 /*
6e3d0153
EK
618 * Check if we can resume the session based on external pre-shared secret.
619 * EAP-FAST (RFC 4851) supports two types of session resumption.
620 * Resumption based on server-side state works with session IDs.
621 * Resumption based on pre-shared Protected Access Credentials (PACs)
622 * works by overriding the SessionTicket extension at the application
623 * layer, and does not send a session ID. (We do not know whether EAP-FAST
624 * servers would honour the session ID.) Therefore, the session ID alone
625 * is not a reliable indicator of session resumption, so we first check if
626 * we can resume, and later peek at the next handshake message to see if the
627 * server wants to resume.
0f113f3e 628 */
6e3d0153
EK
629 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
630 s->session->tlsext_tick) {
0f113f3e
MC
631 SSL_CIPHER *pref_cipher = NULL;
632 s->session->master_key_length = sizeof(s->session->master_key);
633 if (s->tls_session_secret_cb(s, s->session->master_key,
634 &s->session->master_key_length,
635 NULL, &pref_cipher,
636 s->tls_session_secret_cb_arg)) {
637 s->session->cipher = pref_cipher ?
50932c4a 638 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
6e3d0153 639 } else {
b9908bf9 640 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
6e3d0153
EK
641 al = SSL_AD_INTERNAL_ERROR;
642 goto f_err;
0f113f3e 643 }
50932c4a
MC
644 }
645
fc5ce51d
EK
646 if (session_id_len != 0 && session_id_len == s->session->session_id_length
647 && memcmp(PACKET_data(&session_id), s->session->session_id,
648 session_id_len) == 0) {
0f113f3e
MC
649 if (s->sid_ctx_length != s->session->sid_ctx_length
650 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
651 /* actually a client application bug */
652 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 653 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
654 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
655 goto f_err;
656 }
657 s->hit = 1;
6e3d0153 658 } else {
0f113f3e 659 /*
6e3d0153
EK
660 * If we were trying for session-id reuse but the server
661 * didn't echo the ID, make a new SSL_SESSION.
662 * In the case of EAP-FAST and PAC, we do not send a session ID,
663 * so the PAC-based session secret is always preserved. It'll be
664 * overwritten if the server refuses resumption.
0f113f3e
MC
665 */
666 if (s->session->session_id_length > 0) {
667 if (!ssl_get_new_session(s, 0)) {
668 goto f_err;
669 }
670 }
50932c4a 671
fc5ce51d
EK
672 s->session->session_id_length = session_id_len;
673 /* session_id_len could be 0 */
674 memcpy(s->session->session_id, PACKET_data(&session_id),
675 session_id_len);
0f113f3e 676 }
fc5ce51d 677
50932c4a 678 c = ssl_get_cipher_by_char(s, cipherchars);
0f113f3e
MC
679 if (c == NULL) {
680 /* unknown cipher */
681 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 682 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
0f113f3e
MC
683 goto f_err;
684 }
685 /* Set version disabled mask now we know version */
686 if (!SSL_USE_TLS1_2_CIPHERS(s))
4d69f9e6 687 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
0f113f3e 688 else
4d69f9e6 689 s->s3->tmp.mask_ssl = 0;
0f113f3e
MC
690 /*
691 * If it is a disabled cipher we didn't send it in client hello, so
692 * return an error.
693 */
694 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
695 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 696 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
697 goto f_err;
698 }
0f113f3e
MC
699
700 sk = ssl_get_ciphers_by_id(s);
701 i = sk_SSL_CIPHER_find(sk, c);
702 if (i < 0) {
703 /* we did not say we would use this cipher */
704 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 705 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
706 goto f_err;
707 }
708
709 /*
710 * Depending on the session caching (internal/external), the cipher
711 * and/or cipher_id values may not be set. Make sure that cipher_id is
712 * set and use it for comparison.
713 */
714 if (s->session->cipher)
715 s->session->cipher_id = s->session->cipher->id;
716 if (s->hit && (s->session->cipher_id != c->id)) {
9e9858d1 717 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 718 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
9e9858d1
RS
719 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
720 goto f_err;
0f113f3e
MC
721 }
722 s->s3->tmp.new_cipher = c;
723 /*
724 * Don't digest cached records if no sigalgs: we may need them for client
725 * authentication.
726 */
124037fd 727 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
0f113f3e
MC
728 goto f_err;
729 /* lets get the compression algorithm */
730 /* COMPRESSION */
73999b62 731 if (!PACKET_get_1(pkt, &compression)) {
50932c4a
MC
732 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
733 al = SSL_AD_DECODE_ERROR;
734 goto f_err;
735 }
09b6c2ef 736#ifdef OPENSSL_NO_COMP
fc5ce51d 737 if (compression != 0) {
0f113f3e 738 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 739 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
740 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
741 goto f_err;
742 }
743 /*
744 * If compression is disabled we'd better not try to resume a session
745 * using compression.
746 */
747 if (s->session->compress_meth != 0) {
b9908bf9 748 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
0f113f3e
MC
749 goto f_err;
750 }
09b6c2ef 751#else
fc5ce51d 752 if (s->hit && compression != s->session->compress_meth) {
0f113f3e 753 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 754 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
755 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
756 goto f_err;
757 }
fc5ce51d 758 if (compression == 0)
0f113f3e
MC
759 comp = NULL;
760 else if (!ssl_allow_compression(s)) {
761 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 762 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
0f113f3e 763 goto f_err;
fc5ce51d
EK
764 } else {
765 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
766 }
0f113f3e 767
fc5ce51d 768 if (compression != 0 && comp == NULL) {
0f113f3e 769 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 770 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
771 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
772 goto f_err;
773 } else {
774 s->s3->tmp.new_compression = comp;
775 }
09b6c2ef 776#endif
761772d7 777
0f113f3e 778 /* TLS extensions */
73999b62 779 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
b9908bf9 780 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
0f113f3e
MC
781 goto err;
782 }
0f113f3e 783
73999b62 784 if (PACKET_remaining(pkt) != 0) {
0f113f3e
MC
785 /* wrong packet length */
786 al = SSL_AD_DECODE_ERROR;
b9908bf9 787 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
0f113f3e
MC
788 goto f_err;
789 }
790
8723588e
MC
791#ifndef OPENSSL_NO_SCTP
792 if (SSL_IS_DTLS(s) && s->hit) {
793 unsigned char sctpauthkey[64];
794 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
795
796 /*
797 * Add new shared key for SCTP-Auth, will be ignored if
798 * no SCTP used.
799 */
800 snprintf((char *)labelbuffer,
801 sizeof(DTLS1_SCTP_AUTH_LABEL),
802 DTLS1_SCTP_AUTH_LABEL);
803
804 if (SSL_export_keying_material(s, sctpauthkey,
805 sizeof(sctpauthkey),
806 labelbuffer,
807 sizeof(labelbuffer), NULL, 0,
808 0) <= 0)
809 goto err;
810
811 BIO_ctrl(SSL_get_wbio(s),
812 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
813 sizeof(sctpauthkey), sctpauthkey);
814 }
815#endif
816
b9908bf9 817 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
818 f_err:
819 ssl3_send_alert(s, SSL3_AL_FATAL, al);
820 err:
b9908bf9
MC
821 statem_set_error(s);
822 return MSG_PROCESS_ERROR;
0f113f3e 823}
d02b48c6 824
73999b62 825enum MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
b9908bf9
MC
826{
827 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
828 unsigned long cert_list_len, cert_len;
829 X509 *x = NULL;
830 unsigned char *certstart, *certbytes;
831 STACK_OF(X509) *sk = NULL;
832 EVP_PKEY *pkey = NULL;
0f113f3e
MC
833
834 if ((sk = sk_X509_new_null()) == NULL) {
b9908bf9 835 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 836 goto err;
0f113f3e
MC
837 }
838
73999b62
MC
839 if (!PACKET_get_net_3(pkt, &cert_list_len)
840 || PACKET_remaining(pkt) != cert_list_len) {
0f113f3e 841 al = SSL_AD_DECODE_ERROR;
b9908bf9 842 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
843 goto f_err;
844 }
73999b62
MC
845 while (PACKET_remaining(pkt)) {
846 if (!PACKET_get_net_3(pkt, &cert_len)
847 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
0f113f3e 848 al = SSL_AD_DECODE_ERROR;
b9908bf9 849 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
850 SSL_R_CERT_LENGTH_MISMATCH);
851 goto f_err;
852 }
853
df758a85
MC
854 certstart = certbytes;
855 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
0f113f3e
MC
856 if (x == NULL) {
857 al = SSL_AD_BAD_CERTIFICATE;
b9908bf9 858 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
0f113f3e
MC
859 goto f_err;
860 }
df758a85 861 if (certbytes != (certstart + cert_len)) {
0f113f3e 862 al = SSL_AD_DECODE_ERROR;
b9908bf9 863 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
864 SSL_R_CERT_LENGTH_MISMATCH);
865 goto f_err;
866 }
867 if (!sk_X509_push(sk, x)) {
b9908bf9 868 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 869 goto err;
0f113f3e
MC
870 }
871 x = NULL;
0f113f3e
MC
872 }
873
874 i = ssl_verify_cert_chain(s, sk);
55a9a16f 875 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
0f113f3e 876 al = ssl_verify_alarm_type(s->verify_result);
b9908bf9 877 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
878 SSL_R_CERTIFICATE_VERIFY_FAILED);
879 goto f_err;
880 }
881 ERR_clear_error(); /* but we keep s->verify_result */
882 if (i > 1) {
b9908bf9 883 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
0f113f3e
MC
884 al = SSL_AD_HANDSHAKE_FAILURE;
885 goto f_err;
886 }
887
c34b0f99 888 s->session->peer_chain = sk;
0f113f3e
MC
889 /*
890 * Inconsistency alert: cert_chain does include the peer's certificate,
891 * which we don't include in s3_srvr.c
892 */
893 x = sk_X509_value(sk, 0);
894 sk = NULL;
895 /*
896 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
897 */
898
899 pkey = X509_get_pubkey(x);
900
55a9a16f 901 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
0f113f3e
MC
902 x = NULL;
903 al = SSL3_AL_FATAL;
b9908bf9 904 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
905 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
906 goto f_err;
907 }
908
909 i = ssl_cert_type(x, pkey);
55a9a16f 910 if (i < 0) {
0f113f3e
MC
911 x = NULL;
912 al = SSL3_AL_FATAL;
b9908bf9 913 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
914 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
915 goto f_err;
916 }
917
55a9a16f
MC
918 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
919 if (exp_idx >= 0 && i != exp_idx) {
920 x = NULL;
921 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 922 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
55a9a16f
MC
923 SSL_R_WRONG_CERTIFICATE_TYPE);
924 goto f_err;
0f113f3e 925 }
a273c6ee 926 s->session->peer_type = i;
55a9a16f
MC
927
928 X509_free(s->session->peer);
05f0fb9f 929 X509_up_ref(x);
55a9a16f 930 s->session->peer = x;
0f113f3e
MC
931 s->session->verify_result = s->verify_result;
932
933 x = NULL;
b9908bf9 934 ret = MSG_PROCESS_CONTINUE_READING;
66696478
RS
935 goto done;
936
0f113f3e 937 f_err:
66696478 938 ssl3_send_alert(s, SSL3_AL_FATAL, al);
cc273a93 939 err:
b9908bf9 940 statem_set_error(s);
66696478 941 done:
0f113f3e
MC
942 EVP_PKEY_free(pkey);
943 X509_free(x);
944 sk_X509_pop_free(sk, X509_free);
b9908bf9 945 return ret;
0f113f3e 946}
d02b48c6 947
73999b62 948enum MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
b9908bf9
MC
949{
950#ifndef OPENSSL_NO_RSA
951 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
952#endif
953 EVP_MD_CTX md_ctx;
954 int al, j, verify_ret;
955 long alg_k, alg_a;
956 EVP_PKEY *pkey = NULL;
957 const EVP_MD *md = NULL;
958#ifndef OPENSSL_NO_RSA
959 RSA *rsa = NULL;
960#endif
961#ifndef OPENSSL_NO_DH
962 DH *dh = NULL;
963#endif
964#ifndef OPENSSL_NO_EC
965 EC_KEY *ecdh = NULL;
966 BN_CTX *bn_ctx = NULL;
967 EC_POINT *srvr_ecpoint = NULL;
968 int curve_nid = 0;
969#endif
73999b62 970 PACKET save_param_start, signature;
b9908bf9
MC
971
972 EVP_MD_CTX_init(&md_ctx);
973
974 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
975
73999b62 976 save_param_start = *pkt;
8d92c1f8 977
bc36ee62 978#ifndef OPENSSL_NO_RSA
8d92c1f8
DSH
979 RSA_free(s->s3->peer_rsa_tmp);
980 s->s3->peer_rsa_tmp = NULL;
d02b48c6 981#endif
bc36ee62 982#ifndef OPENSSL_NO_DH
8d92c1f8
DSH
983 DH_free(s->s3->peer_dh_tmp);
984 s->s3->peer_dh_tmp = NULL;
ea262260 985#endif
10bf4fc2 986#ifndef OPENSSL_NO_EC
8d92c1f8
DSH
987 EC_KEY_free(s->s3->peer_ecdh_tmp);
988 s->s3->peer_ecdh_tmp = NULL;
d02b48c6 989#endif
8d92c1f8 990
0f113f3e 991 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 992
0f113f3e 993 al = SSL_AD_DECODE_ERROR;
f2be92b9 994
ddac1974 995#ifndef OPENSSL_NO_PSK
7689082b
DSH
996 /* PSK ciphersuites are preceded by an identity hint */
997 if (alg_k & SSL_PSK) {
32942870 998 PACKET psk_identity_hint;
73999b62 999 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
32942870 1000 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1001 goto f_err;
1002 }
0f113f3e
MC
1003
1004 /*
1005 * Store PSK identity hint for later use, hint is used in
1006 * ssl3_send_client_key_exchange. Assume that the maximum length of
1007 * a PSK identity hint can be as long as the maximum length of a PSK
1008 * identity.
1009 */
32942870 1010 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
0f113f3e 1011 al = SSL_AD_HANDSHAKE_FAILURE;
b9908bf9 1012 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
0f113f3e
MC
1013 goto f_err;
1014 }
8098fc56 1015
32942870
EK
1016 if (!PACKET_strndup(&psk_identity_hint,
1017 &s->session->psk_identity_hint)) {
1018 al = SSL_AD_INTERNAL_ERROR;
1019 goto f_err;
0f113f3e 1020 }
7689082b
DSH
1021 }
1022
1023 /* Nothing else to do for plain PSK or RSAPSK */
1024 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
0f113f3e
MC
1025 } else
1026#endif /* !OPENSSL_NO_PSK */
edc032b5 1027#ifndef OPENSSL_NO_SRP
0f113f3e 1028 if (alg_k & SSL_kSRP) {
32942870 1029 PACKET prime, generator, salt, server_pub;
73999b62
MC
1030 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1031 || !PACKET_get_length_prefixed_2(pkt, &generator)
1032 || !PACKET_get_length_prefixed_1(pkt, &salt)
1033 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
32942870 1034 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1035 goto f_err;
1036 }
0f113f3e 1037
32942870
EK
1038 if ((s->srp_ctx.N =
1039 BN_bin2bn(PACKET_data(&prime),
1040 PACKET_remaining(&prime), NULL)) == NULL
1041 || (s->srp_ctx.g =
1042 BN_bin2bn(PACKET_data(&generator),
1043 PACKET_remaining(&generator), NULL)) == NULL
1044 || (s->srp_ctx.s =
1045 BN_bin2bn(PACKET_data(&salt),
1046 PACKET_remaining(&salt), NULL)) == NULL
1047 || (s->srp_ctx.B =
1048 BN_bin2bn(PACKET_data(&server_pub),
1049 PACKET_remaining(&server_pub), NULL)) == NULL) {
0f113f3e
MC
1050 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1051 goto err;
1052 }
0f113f3e
MC
1053
1054 if (!srp_verify_server_param(s, &al)) {
b9908bf9 1055 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
0f113f3e
MC
1056 goto f_err;
1057 }
0989790b 1058
edc032b5 1059/* We must check if there is a certificate */
8df53b7a 1060 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1061 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1062 } else
1063#endif /* !OPENSSL_NO_SRP */
edc032b5 1064#ifndef OPENSSL_NO_RSA
0f113f3e 1065 if (alg_k & SSL_kRSA) {
32942870 1066 PACKET mod, exp;
0f113f3e
MC
1067 /* Temporary RSA keys only allowed in export ciphersuites */
1068 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1069 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1070 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
1071 goto f_err;
1072 }
0f113f3e 1073
73999b62
MC
1074 if (!PACKET_get_length_prefixed_2(pkt, &mod)
1075 || !PACKET_get_length_prefixed_2(pkt, &exp)) {
32942870 1076 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1077 goto f_err;
1078 }
0f113f3e 1079
32942870
EK
1080 if ((rsa = RSA_new()) == NULL) {
1081 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1082 goto err;
1083 }
0f113f3e 1084
32942870
EK
1085 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1086 rsa->n)) == NULL
1087 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1088 rsa->e)) == NULL) {
0f113f3e
MC
1089 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1090 goto err;
1091 }
0f113f3e
MC
1092
1093 /* this should be because we are using an export cipher */
1094 if (alg_a & SSL_aRSA)
a273c6ee 1095 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1096 else {
b9908bf9 1097 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1098 goto err;
1099 }
1dece951
KR
1100
1101 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1102 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1103 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1dece951
KR
1104 goto f_err;
1105 }
1106
8d92c1f8 1107 s->s3->peer_rsa_tmp = rsa;
0f113f3e
MC
1108 rsa = NULL;
1109 }
1110#else /* OPENSSL_NO_RSA */
1111 if (0) ;
d02b48c6 1112#endif
bc36ee62 1113#ifndef OPENSSL_NO_DH
7689082b 1114 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
32942870 1115 PACKET prime, generator, pub_key;
0f113f3e 1116
73999b62
MC
1117 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1118 || !PACKET_get_length_prefixed_2(pkt, &generator)
1119 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
32942870 1120 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1121 goto f_err;
1122 }
0f113f3e 1123
32942870
EK
1124 if ((dh = DH_new()) == NULL) {
1125 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
0f113f3e
MC
1126 goto err;
1127 }
0f113f3e 1128
32942870
EK
1129 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1130 PACKET_remaining(&prime), NULL)) == NULL
1131 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1132 PACKET_remaining(&generator), NULL)) == NULL
1133 || (dh->pub_key =
1134 BN_bin2bn(PACKET_data(&pub_key),
1135 PACKET_remaining(&pub_key), NULL)) == NULL) {
0f113f3e
MC
1136 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1137 goto err;
1138 }
0f113f3e 1139
32942870
EK
1140 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1141 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
61e72d76
GL
1142 goto f_err;
1143 }
1144
0f113f3e
MC
1145 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1146 al = SSL_AD_HANDSHAKE_FAILURE;
b9908bf9 1147 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
0f113f3e
MC
1148 goto f_err;
1149 }
8df53b7a 1150 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1151 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1152 /* else anonymous DH, so no certificate or pkey. */
1153
8d92c1f8 1154 s->s3->peer_dh_tmp = dh;
0f113f3e 1155 dh = NULL;
0f113f3e
MC
1156 }
1157#endif /* !OPENSSL_NO_DH */
ea262260 1158
10bf4fc2 1159#ifndef OPENSSL_NO_EC
7689082b 1160 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
0f113f3e
MC
1161 EC_GROUP *ngroup;
1162 const EC_GROUP *group;
32942870
EK
1163 PACKET encoded_pt;
1164 unsigned char *ecparams;
0f113f3e
MC
1165
1166 if ((ecdh = EC_KEY_new()) == NULL) {
b9908bf9 1167 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1168 goto err;
1169 }
1170
1171 /*
1172 * Extract elliptic curve parameters and the server's ephemeral ECDH
32942870 1173 * public key. For now we only support named (not generic) curves and
8098fc56 1174 * ECParameters in this case is just three bytes.
0f113f3e 1175 */
73999b62 1176 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
0f113f3e
MC
1177 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1178 goto f_err;
1179 }
1180 /*
1181 * Check curve is one of our preferences, if not server has sent an
1182 * invalid curve. ECParameters is 3 bytes.
1183 */
32942870 1184 if (!tls1_check_curve(s, ecparams, 3)) {
0f113f3e
MC
1185 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1186 goto f_err;
1187 }
1188
32942870 1189 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
0f113f3e 1190 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1191 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
0f113f3e
MC
1192 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1193 goto f_err;
1194 }
1195
1196 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1197 if (ngroup == NULL) {
b9908bf9 1198 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
0f113f3e
MC
1199 goto err;
1200 }
1201 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
b9908bf9 1202 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
0f113f3e
MC
1203 goto err;
1204 }
1205 EC_GROUP_free(ngroup);
1206
1207 group = EC_KEY_get0_group(ecdh);
1208
1209 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1210 (EC_GROUP_get_degree(group) > 163)) {
1211 al = SSL_AD_EXPORT_RESTRICTION;
b9908bf9 1212 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
0f113f3e
MC
1213 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1214 goto f_err;
1215 }
1216
0f113f3e
MC
1217 /* Next, get the encoded ECPoint */
1218 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1219 ((bn_ctx = BN_CTX_new()) == NULL)) {
b9908bf9 1220 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1221 goto err;
1222 }
1223
73999b62 1224 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
32942870 1225 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
8098fc56
MC
1226 goto f_err;
1227 }
0f113f3e 1228
32942870
EK
1229 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1230 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
0f113f3e
MC
1231 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1232 goto f_err;
1233 }
0f113f3e
MC
1234
1235 /*
1236 * The ECC/TLS specification does not mention the use of DSA to sign
1237 * ECParameters in the server key exchange message. We do support RSA
1238 * and ECDSA.
1239 */
1240 if (0) ;
1241# ifndef OPENSSL_NO_RSA
1242 else if (alg_a & SSL_aRSA)
a273c6ee 1243 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1244# endif
10bf4fc2 1245# ifndef OPENSSL_NO_EC
0f113f3e 1246 else if (alg_a & SSL_aECDSA)
a273c6ee 1247 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1248# endif
1249 /* else anonymous ECDH, so no certificate or pkey. */
1250 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
8d92c1f8 1251 s->s3->peer_ecdh_tmp = ecdh;
0f113f3e
MC
1252 ecdh = NULL;
1253 BN_CTX_free(bn_ctx);
1254 bn_ctx = NULL;
1255 EC_POINT_free(srvr_ecpoint);
1256 srvr_ecpoint = NULL;
1257 } else if (alg_k) {
1258 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1259 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
1260 goto f_err;
1261 }
10bf4fc2 1262#endif /* !OPENSSL_NO_EC */
0f113f3e 1263
0f113f3e
MC
1264 /* if it was signed, check the signature */
1265 if (pkey != NULL) {
32942870
EK
1266 PACKET params;
1267 /*
1268 * |pkt| now points to the beginning of the signature, so the difference
1269 * equals the length of the parameters.
1270 */
1271 if (!PACKET_get_sub_packet(&save_param_start, &params,
1272 PACKET_remaining(&save_param_start) -
73999b62 1273 PACKET_remaining(pkt))) {
32942870
EK
1274 al = SSL_AD_INTERNAL_ERROR;
1275 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1276 goto f_err;
1277 }
1278
0f113f3e 1279 if (SSL_USE_SIGALGS(s)) {
32942870 1280 unsigned char *sigalgs;
0f113f3e 1281 int rv;
73999b62 1282 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
0f113f3e
MC
1283 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1284 goto f_err;
1285 }
32942870 1286 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
0f113f3e
MC
1287 if (rv == -1)
1288 goto err;
1289 else if (rv == 0) {
1290 goto f_err;
1291 }
a2f9200f 1292#ifdef SSL_DEBUG
0f113f3e
MC
1293 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1294#endif
32942870 1295 } else {
0f113f3e 1296 md = EVP_sha1();
32942870 1297 }
0f113f3e 1298
73999b62
MC
1299 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1300 || PACKET_remaining(pkt) != 0) {
32942870 1301 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1302 goto f_err;
1303 }
0f113f3e 1304 j = EVP_PKEY_size(pkey);
8098fc56 1305 if (j < 0) {
b9908bf9 1306 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
8098fc56
MC
1307 goto f_err;
1308 }
0f113f3e
MC
1309
1310 /*
8098fc56 1311 * Check signature length
0f113f3e 1312 */
32942870 1313 if (PACKET_remaining(&signature) > (size_t)j) {
0f113f3e 1314 /* wrong packet length */
b9908bf9 1315 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
0f113f3e
MC
1316 goto f_err;
1317 }
bc36ee62 1318#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1319 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1320 int num;
1321 unsigned int size;
1322
1323 j = 0;
1324 q = md_buf;
1325 for (num = 2; num > 0; num--) {
1326 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1327 EVP_DigestInit_ex(&md_ctx, (num == 2)
1328 ? s->ctx->md5 : s->ctx->sha1, NULL);
1329 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1330 SSL3_RANDOM_SIZE);
1331 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1332 SSL3_RANDOM_SIZE);
32942870
EK
1333 EVP_DigestUpdate(&md_ctx, PACKET_data(&params),
1334 PACKET_remaining(&params));
0f113f3e
MC
1335 EVP_DigestFinal_ex(&md_ctx, q, &size);
1336 q += size;
1337 j += size;
1338 }
8098fc56 1339 verify_ret =
32942870
EK
1340 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1341 PACKET_remaining(&signature), pkey->pkey.rsa);
8098fc56 1342 if (verify_ret < 0) {
0f113f3e 1343 al = SSL_AD_DECRYPT_ERROR;
b9908bf9 1344 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
0f113f3e
MC
1345 goto f_err;
1346 }
8098fc56 1347 if (verify_ret == 0) {
0f113f3e
MC
1348 /* bad signature */
1349 al = SSL_AD_DECRYPT_ERROR;
b9908bf9 1350 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
0f113f3e
MC
1351 goto f_err;
1352 }
1353 } else
1354#endif
1355 {
1356 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1357 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1358 SSL3_RANDOM_SIZE);
1359 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1360 SSL3_RANDOM_SIZE);
32942870
EK
1361 EVP_VerifyUpdate(&md_ctx, PACKET_data(&params),
1362 PACKET_remaining(&params));
1363 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1364 PACKET_remaining(&signature), pkey) <= 0) {
0f113f3e
MC
1365 /* bad signature */
1366 al = SSL_AD_DECRYPT_ERROR;
b9908bf9 1367 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
0f113f3e
MC
1368 goto f_err;
1369 }
1370 }
1371 } else {
7689082b
DSH
1372 /* aNULL, aSRP or PSK do not need public keys */
1373 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
0f113f3e
MC
1374 /* Might be wrong key type, check it */
1375 if (ssl3_check_cert_and_algorithm(s))
1376 /* Otherwise this shouldn't happen */
b9908bf9 1377 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1378 goto err;
1379 }
1380 /* still data left over */
73999b62 1381 if (PACKET_remaining(pkt) != 0) {
b9908bf9 1382 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
0f113f3e
MC
1383 goto f_err;
1384 }
1385 }
1386 EVP_PKEY_free(pkey);
1387 EVP_MD_CTX_cleanup(&md_ctx);
b9908bf9 1388 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
1389 f_err:
1390 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1391 err:
1392 EVP_PKEY_free(pkey);
bc36ee62 1393#ifndef OPENSSL_NO_RSA
d6407083 1394 RSA_free(rsa);
6b521df3 1395#endif
bc36ee62 1396#ifndef OPENSSL_NO_DH
d6407083 1397 DH_free(dh);
ea262260 1398#endif
10bf4fc2 1399#ifndef OPENSSL_NO_EC
0f113f3e
MC
1400 BN_CTX_free(bn_ctx);
1401 EC_POINT_free(srvr_ecpoint);
8fdc3734 1402 EC_KEY_free(ecdh);
6b521df3 1403#endif
0f113f3e 1404 EVP_MD_CTX_cleanup(&md_ctx);
b9908bf9
MC
1405 statem_set_error(s);
1406 return MSG_PROCESS_ERROR;
0f113f3e 1407}
d02b48c6 1408
73999b62 1409enum MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
b9908bf9
MC
1410{
1411 int ret = MSG_PROCESS_ERROR;
1412 unsigned int list_len, ctype_num, i, name_len;
1413 X509_NAME *xn = NULL;
1414 unsigned char *data;
1415 unsigned char *namestart, *namebytes;
1416 STACK_OF(X509_NAME) *ca_sk = NULL;
0f113f3e
MC
1417
1418 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
b9908bf9 1419 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1420 goto err;
1421 }
1422
1423 /* get the certificate types */
73999b62
MC
1424 if (!PACKET_get_1(pkt, &ctype_num)
1425 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
ac112332 1426 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1427 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
ac112332
MC
1428 goto err;
1429 }
b548a1f1
RS
1430 OPENSSL_free(s->cert->ctypes);
1431 s->cert->ctypes = NULL;
0f113f3e
MC
1432 if (ctype_num > SSL3_CT_NUMBER) {
1433 /* If we exceed static buffer copy all to cert structure */
1434 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1435 if (s->cert->ctypes == NULL) {
b9908bf9 1436 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1437 goto err;
1438 }
ac112332 1439 memcpy(s->cert->ctypes, data, ctype_num);
0f113f3e
MC
1440 s->cert->ctype_num = (size_t)ctype_num;
1441 ctype_num = SSL3_CT_NUMBER;
1442 }
1443 for (i = 0; i < ctype_num; i++)
ac112332
MC
1444 s->s3->tmp.ctype[i] = data[i];
1445
0f113f3e 1446 if (SSL_USE_SIGALGS(s)) {
73999b62
MC
1447 if (!PACKET_get_net_2(pkt, &list_len)
1448 || !PACKET_get_bytes(pkt, &data, list_len)) {
0f113f3e 1449 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
1450 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1451 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1452 goto err;
1453 }
ac112332 1454
0f113f3e
MC
1455 /* Clear certificate digests and validity flags */
1456 for (i = 0; i < SSL_PKEY_NUM; i++) {
d376e57d 1457 s->s3->tmp.md[i] = NULL;
6383d316 1458 s->s3->tmp.valid_flags[i] = 0;
0f113f3e 1459 }
ac112332 1460 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
0f113f3e 1461 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1462 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
1463 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1464 goto err;
1465 }
1466 if (!tls1_process_sigalgs(s)) {
1467 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 1468 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1469 goto err;
1470 }
0f113f3e
MC
1471 }
1472
1473 /* get the CA RDNs */
73999b62
MC
1474 if (!PACKET_get_net_2(pkt, &list_len)
1475 || PACKET_remaining(pkt) != list_len) {
0f113f3e 1476 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1477 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1478 goto err;
1479 }
1480
73999b62
MC
1481 while (PACKET_remaining(pkt)) {
1482 if (!PACKET_get_net_2(pkt, &name_len)
1483 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
0f113f3e 1484 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
1485 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1486 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1487 goto err;
1488 }
1489
ac112332 1490 namestart = namebytes;
0f113f3e 1491
ac112332
MC
1492 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
1493 name_len)) == NULL) {
3c33c6f6 1494 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1495 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
3c33c6f6 1496 goto err;
0f113f3e
MC
1497 }
1498
ac112332 1499 if (namebytes != (namestart + name_len)) {
0f113f3e 1500 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1501 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
1502 SSL_R_CA_DN_LENGTH_MISMATCH);
1503 goto err;
1504 }
1505 if (!sk_X509_NAME_push(ca_sk, xn)) {
b9908bf9 1506 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1507 goto err;
1508 }
0f113f3e
MC
1509 }
1510
0f113f3e
MC
1511 /* we should setup a certificate to return.... */
1512 s->s3->tmp.cert_req = 1;
1513 s->s3->tmp.ctype_num = ctype_num;
222561fe 1514 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
0f113f3e
MC
1515 s->s3->tmp.ca_names = ca_sk;
1516 ca_sk = NULL;
1517
b9908bf9 1518 ret = MSG_PROCESS_CONTINUE_READING;
cc273a93 1519 goto done;
0f113f3e 1520 err:
b9908bf9 1521 statem_set_error(s);
cc273a93 1522 done:
222561fe 1523 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
b9908bf9 1524 return ret;
0f113f3e
MC
1525}
1526
1527static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
dfeab068 1528{
0f113f3e 1529 return (X509_NAME_cmp(*a, *b));
dfeab068 1530}
dfeab068 1531
73999b62 1532enum MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
b9908bf9
MC
1533{
1534 int al;
1535 unsigned int ticklen;
1536 unsigned long ticket_lifetime_hint;
b9908bf9 1537
73999b62
MC
1538 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
1539 || !PACKET_get_net_2(pkt, &ticklen)
1540 || PACKET_remaining(pkt) != ticklen) {
e711da71
EK
1541 al = SSL_AD_DECODE_ERROR;
1542 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
1543 goto f_err;
1544 }
1545
1546 /* Server is allowed to change its mind and send an empty ticket. */
1547 if (ticklen == 0)
1548 return 1;
1549
98ece4ee
MC
1550 if (s->session->session_id_length > 0) {
1551 int i = s->session_ctx->session_cache_mode;
1552 SSL_SESSION *new_sess;
1553 /*
1554 * We reused an existing session, so we need to replace it with a new
1555 * one
1556 */
1557 if (i & SSL_SESS_CACHE_CLIENT) {
1558 /*
1559 * Remove the old session from the cache
1560 */
1561 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
1562 if (s->session_ctx->remove_session_cb != NULL)
1563 s->session_ctx->remove_session_cb(s->session_ctx,
1564 s->session);
1565 } else {
1566 /* We carry on if this fails */
1567 SSL_CTX_remove_session(s->session_ctx, s->session);
1568 }
1569 }
1570
1571 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
1572 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1573 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
98ece4ee
MC
1574 goto f_err;
1575 }
1576
1577 SSL_SESSION_free(s->session);
1578 s->session = new_sess;
1579 }
1580
b548a1f1
RS
1581 OPENSSL_free(s->session->tlsext_tick);
1582 s->session->tlsext_ticklen = 0;
e711da71 1583
0f113f3e
MC
1584 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1585 if (!s->session->tlsext_tick) {
b9908bf9 1586 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1587 goto err;
1588 }
73999b62 1589 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
561e12bb 1590 al = SSL_AD_DECODE_ERROR;
b9908bf9 1591 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
561e12bb
MC
1592 goto f_err;
1593 }
e711da71
EK
1594
1595 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
0f113f3e
MC
1596 s->session->tlsext_ticklen = ticklen;
1597 /*
1598 * There are two ways to detect a resumed ticket session. One is to set
1599 * an appropriate session ID and then the server must return a match in
1600 * ServerHello. This allows the normal client session ID matching to work
1601 * and we know much earlier that the ticket has been accepted. The
1602 * other way is to set zero length session ID when the ticket is
1603 * presented and rely on the handshake to determine session resumption.
1604 * We choose the former approach because this fits in with assumptions
1605 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1606 * SHA256 is disabled) hash of the ticket.
1607 */
561e12bb 1608 EVP_Digest(s->session->tlsext_tick, ticklen,
0f113f3e 1609 s->session->session_id, &s->session->session_id_length,
0f113f3e 1610 EVP_sha256(), NULL);
b9908bf9 1611 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
1612 f_err:
1613 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1614 err:
b9908bf9
MC
1615 statem_set_error(s);
1616 return MSG_PROCESS_ERROR;
0f113f3e 1617}
67c8e7f4 1618
73999b62 1619enum MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
b9908bf9
MC
1620{
1621 int al;
1622 unsigned long resplen;
1623 unsigned int type;
b9908bf9 1624
73999b62 1625 if (!PACKET_get_1(pkt, &type)
ac63710a 1626 || type != TLSEXT_STATUSTYPE_ocsp) {
0f113f3e 1627 al = SSL_AD_DECODE_ERROR;
b9908bf9 1628 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
0f113f3e
MC
1629 goto f_err;
1630 }
73999b62
MC
1631 if (!PACKET_get_net_3(pkt, &resplen)
1632 || PACKET_remaining(pkt) != resplen) {
0f113f3e 1633 al = SSL_AD_DECODE_ERROR;
b9908bf9 1634 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1635 goto f_err;
1636 }
b548a1f1 1637 OPENSSL_free(s->tlsext_ocsp_resp);
ac63710a 1638 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
0f113f3e
MC
1639 if (!s->tlsext_ocsp_resp) {
1640 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1641 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1642 goto f_err;
1643 }
73999b62 1644 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
ac63710a 1645 al = SSL_AD_DECODE_ERROR;
b9908bf9 1646 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
ac63710a
MC
1647 goto f_err;
1648 }
0f113f3e
MC
1649 s->tlsext_ocsp_resplen = resplen;
1650 if (s->ctx->tlsext_status_cb) {
1651 int ret;
1652 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1653 if (ret == 0) {
1654 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
b9908bf9 1655 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
0f113f3e
MC
1656 goto f_err;
1657 }
1658 if (ret < 0) {
1659 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1660 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1661 goto f_err;
1662 }
1663 }
b9908bf9 1664 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
1665 f_err:
1666 ssl3_send_alert(s, SSL3_AL_FATAL, al);
b9908bf9
MC
1667 statem_set_error(s);
1668 return MSG_PROCESS_ERROR;
0f113f3e 1669}
d02b48c6 1670
73999b62 1671enum MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
b9908bf9 1672{
73999b62 1673 if (PACKET_remaining(pkt) > 0) {
0f113f3e
MC
1674 /* should contain no data */
1675 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
1676 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1677 statem_set_error(s);
1678 return MSG_PROCESS_ERROR;
0f113f3e 1679 }
b9908bf9
MC
1680
1681#ifndef OPENSSL_NO_SRP
1682 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
1683 if (SRP_Calc_A_param(s) <= 0) {
1684 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
1685 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1686 statem_set_error(s);
1687 return MSG_PROCESS_ERROR;
1688 }
1689 }
1690#endif
1691
473483d4
MC
1692#ifndef OPENSSL_NO_SCTP
1693 /* Only applies to renegotiation */
1694 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
1695 && s->renegotiate != 0)
1696 return MSG_PROCESS_CONTINUE_PROCESSING;
1697 else
1698#endif
1699 return MSG_PROCESS_FINISHED_READING;
0f113f3e 1700}
176f31dd 1701
b9908bf9 1702int tls_construct_client_key_exchange(SSL *s)
0f113f3e
MC
1703{
1704 unsigned char *p;
1705 int n;
7689082b
DSH
1706#ifndef OPENSSL_NO_PSK
1707 size_t pskhdrlen = 0;
1708#endif
0f113f3e 1709 unsigned long alg_k;
bc36ee62 1710#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1711 unsigned char *q;
1712 EVP_PKEY *pkey = NULL;
79df9d62 1713#endif
10bf4fc2 1714#ifndef OPENSSL_NO_EC
0f113f3e
MC
1715 EC_KEY *clnt_ecdh = NULL;
1716 const EC_POINT *srvr_ecpoint = NULL;
1717 EVP_PKEY *srvr_pub_pkey = NULL;
1718 unsigned char *encodedPoint = NULL;
1719 int encoded_pt_len = 0;
1720 BN_CTX *bn_ctx = NULL;
ea262260 1721#endif
c660ec63
DSH
1722 unsigned char *pms = NULL;
1723 size_t pmslen = 0;
7689082b 1724 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
d02b48c6 1725
b9908bf9 1726 p = ssl_handshake_start(s);
d02b48c6 1727
7689082b
DSH
1728
1729#ifndef OPENSSL_NO_PSK
b9908bf9
MC
1730 if (alg_k & SSL_PSK) {
1731 int psk_err = 1;
1732 /*
1733 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
1734 * \0-terminated identity. The last byte is for us for simulating
1735 * strnlen.
1736 */
1737 char identity[PSK_MAX_IDENTITY_LEN + 1];
1738 size_t identitylen;
1739 unsigned char psk[PSK_MAX_PSK_LEN];
1740 size_t psklen;
1741
1742 if (s->psk_client_callback == NULL) {
1743 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1744 SSL_R_PSK_NO_CLIENT_CB);
1745 goto err;
1746 }
7689082b 1747
b9908bf9 1748 memset(identity, 0, sizeof(identity));
7689082b 1749
b9908bf9
MC
1750 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
1751 identity, sizeof(identity) - 1,
1752 psk, sizeof(psk));
7689082b 1753
b9908bf9
MC
1754 if (psklen > PSK_MAX_PSK_LEN) {
1755 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1756 ERR_R_INTERNAL_ERROR);
1757 goto psk_err;
1758 } else if (psklen == 0) {
1759 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1760 SSL_R_PSK_IDENTITY_NOT_FOUND);
1761 goto psk_err;
1762 }
7689082b 1763
b9908bf9
MC
1764 OPENSSL_free(s->s3->tmp.psk);
1765 s->s3->tmp.psk = BUF_memdup(psk, psklen);
1766 OPENSSL_cleanse(psk, psklen);
7689082b 1767
b9908bf9
MC
1768 if (s->s3->tmp.psk == NULL) {
1769 OPENSSL_cleanse(identity, sizeof(identity));
1770 goto memerr;
1771 }
7689082b 1772
b9908bf9 1773 s->s3->tmp.psklen = psklen;
7689082b 1774
b9908bf9
MC
1775 identitylen = strlen(identity);
1776 if (identitylen > PSK_MAX_IDENTITY_LEN) {
1777 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1778 ERR_R_INTERNAL_ERROR);
1779 goto psk_err;
1780 }
1781 OPENSSL_free(s->session->psk_identity);
1782 s->session->psk_identity = BUF_strdup(identity);
1783 if (s->session->psk_identity == NULL) {
7689082b 1784 OPENSSL_cleanse(identity, sizeof(identity));
b9908bf9 1785 goto memerr;
7689082b 1786 }
d02b48c6 1787
b9908bf9
MC
1788 s2n(identitylen, p);
1789 memcpy(p, identity, identitylen);
1790 pskhdrlen = 2 + identitylen;
1791 p += identitylen;
1792 psk_err = 0;
1793psk_err:
1794 OPENSSL_cleanse(identity, sizeof(identity));
1795 if (psk_err != 0) {
1796 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1797 goto err;
0f113f3e 1798 }
b9908bf9
MC
1799 }
1800 if (alg_k & SSL_kPSK) {
1801 n = 0;
1802 } else
1803#endif
1804
1805 /* Fool emacs indentation */
1806 if (0) {
1807 }
bc36ee62 1808#ifndef OPENSSL_NO_RSA
b9908bf9
MC
1809 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
1810 RSA *rsa;
1811 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
1812 pms = OPENSSL_malloc(pmslen);
1813 if (!pms)
1814 goto memerr;
1815
1816 if (s->session->peer == NULL) {
1817 /*
1818 * We should always have a server certificate with SSL_kRSA.
1819 */
1820 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1821 ERR_R_INTERNAL_ERROR);
1822 goto err;
1823 }
0f113f3e 1824
b9908bf9
MC
1825 if (s->s3->peer_rsa_tmp != NULL)
1826 rsa = s->s3->peer_rsa_tmp;
1827 else {
1828 pkey = X509_get_pubkey(s->session->peer);
1829 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
1830 || (pkey->pkey.rsa == NULL)) {
1831 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1832 ERR_R_INTERNAL_ERROR);
0f113f3e 1833 EVP_PKEY_free(pkey);
b9908bf9 1834 goto err;
0f113f3e 1835 }
b9908bf9
MC
1836 rsa = pkey->pkey.rsa;
1837 EVP_PKEY_free(pkey);
1838 }
0f113f3e 1839
b9908bf9
MC
1840 pms[0] = s->client_version >> 8;
1841 pms[1] = s->client_version & 0xff;
1842 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
1843 goto err;
0f113f3e 1844
b9908bf9
MC
1845 q = p;
1846 /* Fix buf for TLS and beyond */
1847 if (s->version > SSL3_VERSION)
1848 p += 2;
1849 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
0f113f3e 1850# ifdef PKCS1_CHECK
b9908bf9
MC
1851 if (s->options & SSL_OP_PKCS1_CHECK_1)
1852 p[1]++;
1853 if (s->options & SSL_OP_PKCS1_CHECK_2)
1854 tmp_buf[0] = 0x70;
0f113f3e 1855# endif
b9908bf9
MC
1856 if (n <= 0) {
1857 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1858 SSL_R_BAD_RSA_ENCRYPT);
1859 goto err;
1860 }
0f113f3e 1861
b9908bf9
MC
1862 /* Fix buf for TLS and beyond */
1863 if (s->version > SSL3_VERSION) {
1864 s2n(n, q);
1865 n += 2;
0f113f3e 1866 }
b9908bf9 1867 }
f9b3bff6 1868#endif
bc36ee62 1869#ifndef OPENSSL_NO_DH
b9908bf9
MC
1870 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
1871 DH *dh_srvr, *dh_clnt;
1872 if (s->s3->peer_dh_tmp != NULL)
1873 dh_srvr = s->s3->peer_dh_tmp;
1874 else {
1875 /* we get them from the cert */
1876 EVP_PKEY *spkey = NULL;
1877 dh_srvr = NULL;
1878 spkey = X509_get_pubkey(s->session->peer);
1879 if (spkey) {
1880 dh_srvr = EVP_PKEY_get1_DH(spkey);
1881 EVP_PKEY_free(spkey);
0f113f3e 1882 }
b9908bf9
MC
1883 if (dh_srvr == NULL) {
1884 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
1885 ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1886 goto err;
1887 }
0f113f3e 1888 }
b9908bf9
MC
1889 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
1890 /* Use client certificate key */
1891 EVP_PKEY *clkey = s->cert->key->privatekey;
1892 dh_clnt = NULL;
1893 if (clkey)
1894 dh_clnt = EVP_PKEY_get1_DH(clkey);
1895 if (dh_clnt == NULL) {
1896 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
0f113f3e
MC
1897 ERR_R_INTERNAL_ERROR);
1898 goto err;
1899 }
b9908bf9
MC
1900 } else {
1901 /* generate a new random key */
1902 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1903 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
0f113f3e
MC
1904 goto err;
1905 }
b9908bf9
MC
1906 if (!DH_generate_key(dh_clnt)) {
1907 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1908 DH_free(dh_clnt);
0f113f3e
MC
1909 goto err;
1910 }
b9908bf9 1911 }
0f113f3e 1912
b9908bf9
MC
1913 pmslen = DH_size(dh_clnt);
1914 pms = OPENSSL_malloc(pmslen);
1915 if (!pms)
1916 goto memerr;
0f113f3e 1917
b9908bf9
MC
1918 /*
1919 * use the 'p' output buffer for the DH key, but make sure to
1920 * clear it out afterwards
1921 */
0f113f3e 1922
b9908bf9
MC
1923 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
1924 if (s->s3->peer_dh_tmp == NULL)
1925 DH_free(dh_srvr);
0f113f3e 1926
b9908bf9
MC
1927 if (n <= 0) {
1928 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1929 DH_free(dh_clnt);
1930 goto err;
1931 }
1932 pmslen = n;
0f113f3e 1933
b9908bf9
MC
1934 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
1935 n = 0;
1936 else {
1937 /* send off the data */
1938 n = BN_num_bytes(dh_clnt->pub_key);
1939 s2n(n, p);
1940 BN_bn2bin(dh_clnt->pub_key, p);
1941 n += 2;
0f113f3e 1942 }
c660ec63 1943
b9908bf9
MC
1944 DH_free(dh_clnt);
1945 }
1946#endif
0f113f3e 1947
b9908bf9
MC
1948#ifndef OPENSSL_NO_EC
1949 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
1950 const EC_GROUP *srvr_group = NULL;
1951 EC_KEY *tkey;
1952 int ecdh_clnt_cert = 0;
1953 int field_size = 0;
1954 /*
1955 * Did we send out the client's ECDH share for use in premaster
1956 * computation as part of client certificate? If so, set
1957 * ecdh_clnt_cert to 1.
1958 */
1959 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
1960 /*-
1961 * XXX: For now, we do not support client
1962 * authentication using ECDH certificates.
1963 * To add such support, one needs to add
1964 * code that checks for appropriate
1965 * conditions and sets ecdh_clnt_cert to 1.
1966 * For example, the cert have an ECC
1967 * key on the same curve as the server's
1968 * and the key should be authorized for
1969 * key agreement.
0f113f3e 1970 *
b9908bf9
MC
1971 * One also needs to add code in ssl3_connect
1972 * to skip sending the certificate verify
1973 * message.
1974 *
1975 * if ((s->cert->key->privatekey != NULL) &&
1976 * (s->cert->key->privatekey->type ==
1977 * EVP_PKEY_EC) && ...)
1978 * ecdh_clnt_cert = 1;
0f113f3e 1979 */
b9908bf9 1980 }
0f113f3e 1981
b9908bf9
MC
1982 if (s->s3->peer_ecdh_tmp != NULL) {
1983 tkey = s->s3->peer_ecdh_tmp;
1984 } else {
1985 /* Get the Server Public Key from Cert */
1986 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
1987 if ((srvr_pub_pkey == NULL)
1988 || (srvr_pub_pkey->type != EVP_PKEY_EC)
1989 || (srvr_pub_pkey->pkey.ec == NULL)) {
1990 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
266483d2
MC
1991 ERR_R_INTERNAL_ERROR);
1992 goto err;
0f113f3e 1993 }
b9908bf9
MC
1994
1995 tkey = srvr_pub_pkey->pkey.ec;
1996 }
1997
1998 srvr_group = EC_KEY_get0_group(tkey);
1999 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2000
2001 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2002 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2003 ERR_R_INTERNAL_ERROR);
2004 goto err;
2005 }
2006
2007 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2008 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2009 ERR_R_MALLOC_FAILURE);
2010 goto err;
2011 }
2012
2013 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2014 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2015 goto err;
2016 }
2017 if (ecdh_clnt_cert) {
0f113f3e 2018 /*
b9908bf9
MC
2019 * Reuse key info from our certificate We only need our
2020 * private key to perform the ECDH computation.
0f113f3e 2021 */
b9908bf9
MC
2022 const BIGNUM *priv_key;
2023 tkey = s->cert->key->privatekey->pkey.ec;
2024 priv_key = EC_KEY_get0_private_key(tkey);
2025 if (priv_key == NULL) {
2026 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2027 ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2028 goto err;
2029 }
b9908bf9
MC
2030 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2031 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
0f113f3e
MC
2032 goto err;
2033 }
b9908bf9
MC
2034 } else {
2035 /* Generate a new ECDH key pair */
2036 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2037 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2038 ERR_R_ECDH_LIB);
2039 goto err;
0f113f3e 2040 }
b9908bf9
MC
2041 }
2042
2043 /*
2044 * use the 'p' output buffer for the ECDH key, but make sure to
2045 * clear it out afterwards
2046 */
0f113f3e 2047
b9908bf9
MC
2048 field_size = EC_GROUP_get_degree(srvr_group);
2049 if (field_size <= 0) {
2050 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2051 goto err;
0f113f3e 2052 }
b9908bf9
MC
2053 pmslen = (field_size + 7) / 8;
2054 pms = OPENSSL_malloc(pmslen);
2055 if (!pms)
2056 goto memerr;
2057 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2058 if (n <= 0 || pmslen != (size_t)n) {
2059 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2060 goto err;
2061 }
2062
2063 if (ecdh_clnt_cert) {
2064 /* Send empty client key exch message */
2065 n = 0;
2066 } else {
2067 /*
2068 * First check the size of encoding and allocate memory
2069 * accordingly.
2070 */
2071 encoded_pt_len =
2072 EC_POINT_point2oct(srvr_group,
2073 EC_KEY_get0_public_key(clnt_ecdh),
2074 POINT_CONVERSION_UNCOMPRESSED,
2075 NULL, 0, NULL);
2076
2077 encodedPoint = (unsigned char *)
2078 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2079 bn_ctx = BN_CTX_new();
2080 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2081 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
0f113f3e
MC
2082 ERR_R_MALLOC_FAILURE);
2083 goto err;
2084 }
b9908bf9
MC
2085
2086 /* Encode the public key */
2087 n = EC_POINT_point2oct(srvr_group,
2088 EC_KEY_get0_public_key(clnt_ecdh),
2089 POINT_CONVERSION_UNCOMPRESSED,
2090 encodedPoint, encoded_pt_len, bn_ctx);
2091
2092 *p = n; /* length of encoded point */
2093 /* Encoded point will be copied here */
2094 p += 1;
2095 /* copy the point */
2096 memcpy(p, encodedPoint, n);
2097 /* increment n to account for length field */
2098 n += 1;
0f113f3e
MC
2099 }
2100
b9908bf9
MC
2101 /* Free allocated memory */
2102 BN_CTX_free(bn_ctx);
2103 OPENSSL_free(encodedPoint);
2104 EC_KEY_free(clnt_ecdh);
2105 EVP_PKEY_free(srvr_pub_pkey);
2106 }
2107#endif /* !OPENSSL_NO_EC */
2108 else if (alg_k & SSL_kGOST) {
2109 /* GOST key exchange message creation */
2110 EVP_PKEY_CTX *pkey_ctx;
2111 X509 *peer_cert;
2112 size_t msglen;
2113 unsigned int md_len;
2114 unsigned char shared_ukm[32], tmp[256];
2115 EVP_MD_CTX *ukm_hash;
2116 EVP_PKEY *pub_key;
2117
2118 pmslen = 32;
2119 pms = OPENSSL_malloc(pmslen);
2120 if (!pms)
2121 goto memerr;
7689082b 2122
b9908bf9
MC
2123 /*
2124 * Get server sertificate PKEY and create ctx from it
2125 */
2126 peer_cert = s->session->peer;
2127 if (!peer_cert) {
2128 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2129 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
77d514c5
MC
2130 goto err;
2131 }
2132
b9908bf9
MC
2133 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2134 X509_get_pubkey(peer_cert), NULL);
2135 /*
2136 * If we have send a certificate, and certificate key
2137 *
2138 * * parameters match those of server certificate, use
2139 * certificate key for key exchange
2140 */
0f113f3e 2141
b9908bf9
MC
2142 /* Otherwise, generate ephemeral key pair */
2143
2144 EVP_PKEY_encrypt_init(pkey_ctx);
2145 /* Generate session key */
2146 if (RAND_bytes(pms, pmslen) <= 0) {
2147 EVP_PKEY_CTX_free(pkey_ctx);
2148 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2149 ERR_R_INTERNAL_ERROR);
2150 goto err;
2151 };
c660ec63 2152 /*
b9908bf9 2153 * If we have client certificate, use its secret as peer key
c660ec63 2154 */
b9908bf9
MC
2155 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2156 if (EVP_PKEY_derive_set_peer
2157 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2158 /*
2159 * If there was an error - just ignore it. Ephemeral key
2160 * * would be used
2161 */
2162 ERR_clear_error();
c660ec63
DSH
2163 }
2164 }
b9908bf9
MC
2165 /*
2166 * Compute shared IV and store it in algorithm-specific context
2167 * data
2168 */
2169 ukm_hash = EVP_MD_CTX_create();
2170 EVP_DigestInit(ukm_hash,
2171 EVP_get_digestbynid(NID_id_GostR3411_94));
2172 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2173 SSL3_RANDOM_SIZE);
2174 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2175 SSL3_RANDOM_SIZE);
2176 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2177 EVP_MD_CTX_destroy(ukm_hash);
2178 if (EVP_PKEY_CTX_ctrl
2179 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2180 shared_ukm) < 0) {
2181 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2182 SSL_R_LIBRARY_BUG);
2183 goto err;
c660ec63 2184 }
b9908bf9
MC
2185 /* Make GOST keytransport blob message */
2186 /*
2187 * Encapsulate it into sequence
2188 */
2189 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2190 msglen = 255;
2191 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2192 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2193 SSL_R_LIBRARY_BUG);
c660ec63
DSH
2194 goto err;
2195 }
b9908bf9
MC
2196 if (msglen >= 0x80) {
2197 *(p++) = 0x81;
2198 *(p++) = msglen & 0xff;
2199 n = msglen + 3;
2200 } else {
2201 *(p++) = msglen & 0xff;
2202 n = msglen + 2;
2203 }
2204 memcpy(p, tmp, msglen);
2205 /* Check if pubkey from client certificate was used */
2206 if (EVP_PKEY_CTX_ctrl
2207 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2208 /* Set flag "skip certificate verify" */
2209 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2210 }
2211 EVP_PKEY_CTX_free(pkey_ctx);
2212 EVP_PKEY_free(pub_key);
2213
2214 }
2215#ifndef OPENSSL_NO_SRP
2216 else if (alg_k & SSL_kSRP) {
2217 if (s->srp_ctx.A != NULL) {
2218 /* send off the data */
2219 n = BN_num_bytes(s->srp_ctx.A);
2220 s2n(n, p);
2221 BN_bn2bin(s->srp_ctx.A, p);
2222 n += 2;
2223 } else {
2224 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2225 ERR_R_INTERNAL_ERROR);
2226 goto err;
2227 }
2228 OPENSSL_free(s->session->srp_username);
2229 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2230 if (s->session->srp_username == NULL) {
2231 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2232 ERR_R_MALLOC_FAILURE);
69f68237
MC
2233 goto err;
2234 }
c660ec63 2235 }
b9908bf9
MC
2236#endif
2237 else {
2238 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2239 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2240 goto err;
2241 }
2242
2243#ifndef OPENSSL_NO_PSK
2244 n += pskhdrlen;
2245#endif
2246
2247 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2248 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2249 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2250 goto err;
2251 }
2252
2253 s->s3->tmp.pms = pms;
2254 s->s3->tmp.pmslen = pmslen;
2255
2256 return 1;
c660ec63
DSH
2257 memerr:
2258 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2259 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e 2260 err:
4b45c6e5 2261 OPENSSL_clear_free(pms, pmslen);
76106e60 2262 s->s3->tmp.pms = NULL;
10bf4fc2 2263#ifndef OPENSSL_NO_EC
0f113f3e 2264 BN_CTX_free(bn_ctx);
b548a1f1 2265 OPENSSL_free(encodedPoint);
8fdc3734 2266 EC_KEY_free(clnt_ecdh);
0f113f3e 2267 EVP_PKEY_free(srvr_pub_pkey);
7689082b
DSH
2268#endif
2269#ifndef OPENSSL_NO_PSK
2270 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2271 s->s3->tmp.psk = NULL;
0f113f3e 2272#endif
b9908bf9
MC
2273 statem_set_error(s);
2274 return 0;
2275}
2276
2277int tls_client_key_exchange_post_work(SSL *s)
2278{
2279 unsigned char *pms = NULL;
2280 size_t pmslen = 0;
2281
2282#ifndef OPENSSL_NO_SRP
2283 /* Check for SRP */
2284 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2285 if (!srp_generate_client_master_secret(s)) {
2286 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2287 ERR_R_INTERNAL_ERROR);
2288 goto err;
2289 }
2290 return 1;
2291 }
2292#endif
2293 pms = s->s3->tmp.pms;
2294 pmslen = s->s3->tmp.pmslen;
2295
2296 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2297 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2298 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2299 goto err;
2300 }
2301 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2302 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2303 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2304 goto err;
2305 }
473483d4
MC
2306
2307#ifndef OPENSSL_NO_SCTP
2308 if (SSL_IS_DTLS(s)) {
2309 unsigned char sctpauthkey[64];
2310 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2311
2312 /*
2313 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2314 * used.
2315 */
2316 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
2317 DTLS1_SCTP_AUTH_LABEL);
2318
2319 if (SSL_export_keying_material(s, sctpauthkey,
2320 sizeof(sctpauthkey), labelbuffer,
2321 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2322 goto err;
2323
2324 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2325 sizeof(sctpauthkey), sctpauthkey);
2326 }
2327#endif
2328
b9908bf9
MC
2329 return 1;
2330 err:
2331 OPENSSL_clear_free(pms, pmslen);
2332 s->s3->tmp.pms = NULL;
2333 return 0;
0f113f3e 2334}
d02b48c6 2335
b9908bf9 2336int tls_construct_client_verify(SSL *s)
0f113f3e
MC
2337{
2338 unsigned char *p;
2339 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2340 EVP_PKEY *pkey;
2341 EVP_PKEY_CTX *pctx = NULL;
2342 EVP_MD_CTX mctx;
2343 unsigned u = 0;
2344 unsigned long n;
2345 int j;
2346
2347 EVP_MD_CTX_init(&mctx);
2348
b9908bf9
MC
2349 p = ssl_handshake_start(s);
2350 pkey = s->cert->key->privatekey;
0e1dba93 2351/* Create context from key and test if sha1 is allowed as digest */
b9908bf9
MC
2352 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2353 EVP_PKEY_sign_init(pctx);
2354 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2355 if (!SSL_USE_SIGALGS(s))
2356 s->method->ssl3_enc->cert_verify_mac(s,
2357 NID_sha1,
2358 &(data
2359 [MD5_DIGEST_LENGTH]));
2360 } else {
2361 ERR_clear_error();
2362 }
2363 /*
2364 * For TLS v1.2 send signature algorithm and signature using agreed
2365 * digest and cached handshake records.
2366 */
2367 if (SSL_USE_SIGALGS(s)) {
2368 long hdatalen = 0;
2369 void *hdata;
2370 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2371 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2372 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
2373 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2374 goto err;
0f113f3e 2375 }
b9908bf9 2376 p += 2;
855a54a9 2377#ifdef SSL_DEBUG
b9908bf9
MC
2378 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
2379 EVP_MD_name(md));
0f113f3e 2380#endif
b9908bf9
MC
2381 if (!EVP_SignInit_ex(&mctx, md, NULL)
2382 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
2383 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2384 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2385 goto err;
2386 }
2387 s2n(u, p);
2388 n = u + 4;
2389 /* Digest cached records and discard handshake buffer */
2390 if (!ssl3_digest_cached_records(s, 0))
2391 goto err;
2392 } else
bc36ee62 2393#ifndef OPENSSL_NO_RSA
b9908bf9
MC
2394 if (pkey->type == EVP_PKEY_RSA) {
2395 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
2396 if (RSA_sign(NID_md5_sha1, data,
2397 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
2398 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2399 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
2400 goto err;
2401 }
2402 s2n(u, p);
2403 n = u + 2;
2404 } else
d02b48c6 2405#endif
bc36ee62 2406#ifndef OPENSSL_NO_DSA
b9908bf9
MC
2407 if (pkey->type == EVP_PKEY_DSA) {
2408 if (!DSA_sign(pkey->save_type,
2409 &(data[MD5_DIGEST_LENGTH]),
2410 SHA_DIGEST_LENGTH, &(p[2]),
2411 (unsigned int *)&j, pkey->pkey.dsa)) {
2412 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
2413 goto err;
2414 }
2415 s2n(j, p);
2416 n = j + 2;
2417 } else
ea262260 2418#endif
10bf4fc2 2419#ifndef OPENSSL_NO_EC
b9908bf9
MC
2420 if (pkey->type == EVP_PKEY_EC) {
2421 if (!ECDSA_sign(pkey->save_type,
2422 &(data[MD5_DIGEST_LENGTH]),
2423 SHA_DIGEST_LENGTH, &(p[2]),
2424 (unsigned int *)&j, pkey->pkey.ec)) {
2425 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
0f113f3e
MC
2426 goto err;
2427 }
b9908bf9
MC
2428 s2n(j, p);
2429 n = j + 2;
2430 } else
2431#endif
2432 if (pkey->type == NID_id_GostR3410_2001) {
2433 unsigned char signbuf[64];
2434 int i;
2435 size_t sigsize = 64;
2436 s->method->ssl3_enc->cert_verify_mac(s,
2437 NID_id_GostR3411_94, data);
2438 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
2439 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
77d514c5
MC
2440 goto err;
2441 }
b9908bf9
MC
2442 for (i = 63, j = 0; i >= 0; j++, i--) {
2443 p[2 + j] = signbuf[i];
2444 }
2445 s2n(j, p);
2446 n = j + 2;
2447 } else {
2448 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2449 goto err;
2450 }
2451 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2452 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2453 goto err;
0f113f3e 2454 }
b9908bf9 2455
0f113f3e
MC
2456 EVP_MD_CTX_cleanup(&mctx);
2457 EVP_PKEY_CTX_free(pctx);
b9908bf9 2458 return 1;
0f113f3e
MC
2459 err:
2460 EVP_MD_CTX_cleanup(&mctx);
2461 EVP_PKEY_CTX_free(pctx);
b9908bf9 2462 return 0;
0f113f3e
MC
2463}
2464
2465/*
2466 * Check a certificate can be used for client authentication. Currently check
2467 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2468 * certificates can be used and optionally checks suitability for Suite B.
0d609395
DSH
2469 */
2470static int ssl3_check_client_certificate(SSL *s)
0f113f3e
MC
2471{
2472 unsigned long alg_k;
2473 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2474 return 0;
2475 /* If no suitable signature algorithm can't use certificate */
d376e57d 2476 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
0f113f3e
MC
2477 return 0;
2478 /*
2479 * If strict mode check suitability of chain before using it. This also
2480 * adjusts suite B digest if necessary.
2481 */
2482 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2483 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2484 return 0;
2485 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2486 /* See if we can use client certificate for fixed DH */
2487 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
a273c6ee 2488 int i = s->session->peer_type;
0f113f3e
MC
2489 EVP_PKEY *clkey = NULL, *spkey = NULL;
2490 clkey = s->cert->key->privatekey;
2491 /* If client key not DH assume it can be used */
2492 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
2493 return 1;
2494 if (i >= 0)
a273c6ee 2495 spkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
2496 if (spkey) {
2497 /* Compare server and client parameters */
2498 i = EVP_PKEY_cmp_parameters(clkey, spkey);
2499 EVP_PKEY_free(spkey);
2500 if (i != 1)
2501 return 0;
2502 }
2503 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2504 }
2505 return 1;
2506}
0d609395 2507
b9908bf9 2508enum WORK_STATE tls_prepare_client_certificate(SSL *s, enum WORK_STATE wst)
0f113f3e
MC
2509{
2510 X509 *x509 = NULL;
2511 EVP_PKEY *pkey = NULL;
2512 int i;
2513
b9908bf9 2514 if (wst == WORK_MORE_A) {
0f113f3e
MC
2515 /* Let cert callback update client certificates if required */
2516 if (s->cert->cert_cb) {
2517 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2518 if (i < 0) {
2519 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 2520 return WORK_MORE_A;
0f113f3e
MC
2521 }
2522 if (i == 0) {
2523 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2524 statem_set_error(s);
0f113f3e
MC
2525 return 0;
2526 }
2527 s->rwstate = SSL_NOTHING;
2528 }
2529 if (ssl3_check_client_certificate(s))
b9908bf9
MC
2530 return WORK_FINISHED_CONTINUE;
2531
2532 /* Fall through to WORK_MORE_B */
2533 wst = WORK_MORE_B;
0f113f3e
MC
2534 }
2535
2536 /* We need to get a client cert */
b9908bf9 2537 if (wst == WORK_MORE_B) {
0f113f3e
MC
2538 /*
2539 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2540 * return(-1); We then get retied later
2541 */
0f113f3e
MC
2542 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2543 if (i < 0) {
2544 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 2545 return WORK_MORE_B;
0f113f3e
MC
2546 }
2547 s->rwstate = SSL_NOTHING;
2548 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
0f113f3e
MC
2549 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2550 i = 0;
2551 } else if (i == 1) {
2552 i = 0;
b9908bf9 2553 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
0f113f3e
MC
2554 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2555 }
2556
222561fe 2557 X509_free(x509);
25aaa98a 2558 EVP_PKEY_free(pkey);
0f113f3e
MC
2559 if (i && !ssl3_check_client_certificate(s))
2560 i = 0;
2561 if (i == 0) {
2562 if (s->version == SSL3_VERSION) {
2563 s->s3->tmp.cert_req = 0;
2564 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
b9908bf9 2565 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
2566 } else {
2567 s->s3->tmp.cert_req = 2;
124037fd 2568 if (!ssl3_digest_cached_records(s, 0)) {
dab18ab5 2569 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2570 statem_set_error(s);
dab18ab5
DSH
2571 return 0;
2572 }
0f113f3e
MC
2573 }
2574 }
2575
b9908bf9 2576 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
2577 }
2578
b9908bf9
MC
2579 /* Shouldn't ever get here */
2580 return WORK_ERROR;
2581}
2582
2583int tls_construct_client_certificate(SSL *s)
2584{
2585 if (!ssl3_output_cert_chain(s,
2586 (s->s3->tmp.cert_req ==
2587 2) ? NULL : s->cert->key)) {
2588 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2589 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2590 statem_set_error(s);
2591 return 0;
0f113f3e 2592 }
b9908bf9
MC
2593
2594 return 1;
0f113f3e
MC
2595}
2596
2597#define has_bits(i,m) (((i)&(m)) == (m))
d02b48c6 2598
36d16f8e 2599int ssl3_check_cert_and_algorithm(SSL *s)
0f113f3e
MC
2600{
2601 int i, idx;
2602 long alg_k, alg_a;
2603 EVP_PKEY *pkey = NULL;
ac38115c 2604 int pkey_bits;
bc36ee62 2605#ifndef OPENSSL_NO_RSA
0f113f3e 2606 RSA *rsa;
79df9d62 2607#endif
bc36ee62 2608#ifndef OPENSSL_NO_DH
0f113f3e 2609 DH *dh;
79df9d62 2610#endif
26c79d56 2611 int al = SSL_AD_HANDSHAKE_FAILURE;
d02b48c6 2612
0f113f3e
MC
2613 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2614 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 2615
0f113f3e 2616 /* we don't have a certificate */
55a9a16f 2617 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
0f113f3e 2618 return (1);
bc36ee62 2619#ifndef OPENSSL_NO_RSA
8d92c1f8 2620 rsa = s->s3->peer_rsa_tmp;
79df9d62 2621#endif
bc36ee62 2622#ifndef OPENSSL_NO_DH
8d92c1f8 2623 dh = s->s3->peer_dh_tmp;
79df9d62 2624#endif
d02b48c6 2625
0f113f3e 2626 /* This is the passed certificate */
d02b48c6 2627
a273c6ee 2628 idx = s->session->peer_type;
10bf4fc2 2629#ifndef OPENSSL_NO_EC
0f113f3e 2630 if (idx == SSL_PKEY_ECC) {
a273c6ee 2631 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
0f113f3e
MC
2632 /* check failed */
2633 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2634 goto f_err;
2635 } else {
2636 return 1;
2637 }
2638 } else if (alg_a & SSL_aECDSA) {
2639 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2640 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2641 goto f_err;
2642 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2643 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
2644 goto f_err;
2645 }
2646#endif
a273c6ee 2647 pkey = X509_get_pubkey(s->session->peer);
ac38115c 2648 pkey_bits = EVP_PKEY_bits(pkey);
a273c6ee 2649 i = X509_certificate_type(s->session->peer, pkey);
0f113f3e
MC
2650 EVP_PKEY_free(pkey);
2651
2652 /* Check that we have a certificate if we require one */
2653 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2654 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2655 SSL_R_MISSING_RSA_SIGNING_CERT);
2656 goto f_err;
2657 }
bc36ee62 2658#ifndef OPENSSL_NO_DSA
0f113f3e
MC
2659 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2660 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2661 SSL_R_MISSING_DSA_SIGNING_CERT);
2662 goto f_err;
2663 }
d02b48c6 2664#endif
bc36ee62 2665#ifndef OPENSSL_NO_RSA
7689082b 2666 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
26c79d56
KR
2667 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
2668 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2669 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2670 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2671 goto f_err;
2672 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
2673 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
2674 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2675 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2676 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2677 goto f_err;
2678 }
2679 if (rsa != NULL) {
2680 /* server key exchange is not allowed. */
2681 al = SSL_AD_INTERNAL_ERROR;
2682 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
2683 goto f_err;
2684 }
2685 }
2686 }
0f113f3e 2687 }
79df9d62 2688#endif
bc36ee62 2689#ifndef OPENSSL_NO_DH
26c79d56
KR
2690 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
2691 al = SSL_AD_INTERNAL_ERROR;
2692 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
2693 goto f_err;
2694 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
2695 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
2696 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2697 SSL_R_MISSING_DH_RSA_CERT);
2698 goto f_err;
2699 }
2700# ifndef OPENSSL_NO_DSA
2701 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
2702 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
2703 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2704 SSL_R_MISSING_DH_DSA_CERT);
2705 goto f_err;
2706 }
2707# endif
d02b48c6
RE
2708#endif
2709
ac38115c
KR
2710 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
2711 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
bc36ee62 2712#ifndef OPENSSL_NO_RSA
0f113f3e 2713 if (alg_k & SSL_kRSA) {
26c79d56
KR
2714 if (rsa == NULL) {
2715 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2716 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2717 goto f_err;
2718 } else if (RSA_bits(rsa) >
0f113f3e 2719 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
26c79d56
KR
2720 /* We have a temporary RSA key but it's too large. */
2721 al = SSL_AD_EXPORT_RESTRICTION;
0f113f3e
MC
2722 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2723 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2724 goto f_err;
2725 }
2726 } else
d02b48c6 2727#endif
bc36ee62 2728#ifndef OPENSSL_NO_DH
26c79d56
KR
2729 if (alg_k & SSL_kDHE) {
2730 if (DH_bits(dh) >
0f113f3e 2731 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
26c79d56
KR
2732 /* We have a temporary DH key but it's too large. */
2733 al = SSL_AD_EXPORT_RESTRICTION;
0f113f3e
MC
2734 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2735 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2736 goto f_err;
2737 }
26c79d56
KR
2738 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
2739 /* The cert should have had an export DH key. */
2740 al = SSL_AD_EXPORT_RESTRICTION;
2741 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2742 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2743 goto f_err;
0f113f3e
MC
2744 } else
2745#endif
2746 {
2747 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2748 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2749 goto f_err;
2750 }
2751 }
2752 return (1);
2753 f_err:
26c79d56 2754 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0f113f3e
MC
2755 return (0);
2756}
2757
e481f9b9 2758#ifndef OPENSSL_NO_NEXTPROTONEG
b9908bf9
MC
2759int tls_construct_next_proto(SSL *s)
2760{
2761 unsigned int len, padding_len;
2762 unsigned char *d;
2763
2764 len = s->next_proto_negotiated_len;
2765 padding_len = 32 - ((len + 2) % 32);
2766 d = (unsigned char *)s->init_buf->data;
2767 d[4] = len;
2768 memcpy(d + 5, s->next_proto_negotiated, len);
2769 d[5 + len] = padding_len;
2770 memset(d + 6 + len, 0, padding_len);
2771 *(d++) = SSL3_MT_NEXT_PROTO;
2772 l2n3(2 + len + padding_len, d);
2773 s->init_num = 4 + 2 + len + padding_len;
2774 s->init_off = 0;
2775
2776 return 1;
2777}
6434abbf 2778#endif
368888bc
DSH
2779
2780int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
0f113f3e
MC
2781{
2782 int i = 0;
368888bc 2783#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
2784 if (s->ctx->client_cert_engine) {
2785 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2786 SSL_get_client_CA_list(s),
2787 px509, ppkey, NULL, NULL, NULL);
2788 if (i != 0)
2789 return i;
2790 }
2791#endif
2792 if (s->ctx->client_cert_cb)
2793 i = s->ctx->client_cert_cb(s, px509, ppkey);
2794 return i;
2795}
d45ba43d
MC
2796
2797int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
e9fa092e 2798 unsigned char *p)
d45ba43d
MC
2799{
2800 int i, j = 0;
2801 SSL_CIPHER *c;
2802 unsigned char *q;
2803 int empty_reneg_info_scsv = !s->renegotiate;
2804 /* Set disabled masks for this session */
2805 ssl_set_client_disabled(s);
2806
2807 if (sk == NULL)
2808 return (0);
2809 q = p;
d45ba43d
MC
2810
2811 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
2812 c = sk_SSL_CIPHER_value(sk, i);
2813 /* Skip disabled ciphers */
2814 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
2815 continue;
2816#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2817 if (c->id == SSL3_CK_SCSV) {
2818 if (!empty_reneg_info_scsv)
2819 continue;
2820 else
2821 empty_reneg_info_scsv = 0;
2822 }
2823#endif
e9fa092e 2824 j = s->method->put_cipher_by_char(c, p);
d45ba43d
MC
2825 p += j;
2826 }
2827 /*
2828 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2829 * applicable SCSVs.
2830 */
2831 if (p != q) {
2832 if (empty_reneg_info_scsv) {
2833 static SSL_CIPHER scsv = {
2834 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2835 };
e9fa092e 2836 j = s->method->put_cipher_by_char(&scsv, p);
d45ba43d
MC
2837 p += j;
2838#ifdef OPENSSL_RI_DEBUG
2839 fprintf(stderr,
2840 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
2841#endif
2842 }
2843 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
2844 static SSL_CIPHER scsv = {
2845 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
2846 };
e9fa092e 2847 j = s->method->put_cipher_by_char(&scsv, p);
d45ba43d
MC
2848 p += j;
2849 }
2850 }
2851
2852 return (p - q);
2853}