]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/s3_clnt.c
Document BUF_strnlen
[thirdparty/openssl.git] / ssl / s3_clnt.c
CommitLineData
d02b48c6 1/* ssl/s3_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>
7b63c0fa 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);
657da85e 168static int ssl3_check_change(SSL *s);
d45ba43d
MC
169static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
170 unsigned char *p,
171 int (*put_cb) (const SSL_CIPHER *,
172 unsigned char *));
ea262260 173
d02b48c6 174
6b691a5c 175int ssl3_connect(SSL *s)
0f113f3e
MC
176{
177 BUF_MEM *buf = NULL;
178 unsigned long Time = (unsigned long)time(NULL);
179 void (*cb) (const SSL *ssl, int type, int val) = NULL;
180 int ret = -1;
181 int new_state, state, skip = 0;
d02b48c6 182
0f113f3e
MC
183 RAND_add(&Time, sizeof(Time), 0);
184 ERR_clear_error();
185 clear_sys_error();
4817504d 186
0f113f3e
MC
187 if (s->info_callback != NULL)
188 cb = s->info_callback;
189 else if (s->ctx->info_callback != NULL)
190 cb = s->ctx->info_callback;
191
192 s->in_handshake++;
69f68237 193 if (!SSL_in_init(s) || SSL_in_before(s)) {
61986d32 194 if (!SSL_clear(s))
69f68237
MC
195 return -1;
196 }
0f113f3e
MC
197
198#ifndef OPENSSL_NO_HEARTBEATS
199 /*
200 * If we're awaiting a HeartbeatResponse, pretend we already got and
201 * don't await it anymore, because Heartbeats don't make sense during
202 * handshakes anyway.
203 */
204 if (s->tlsext_hb_pending) {
205 s->tlsext_hb_pending = 0;
206 s->tlsext_hb_seq++;
207 }
208#endif
209
210 for (;;) {
211 state = s->state;
212
213 switch (s->state) {
214 case SSL_ST_RENEGOTIATE:
215 s->renegotiate = 1;
216 s->state = SSL_ST_CONNECT;
217 s->ctx->stats.sess_connect_renegotiate++;
218 /* break */
219 case SSL_ST_BEFORE:
220 case SSL_ST_CONNECT:
221 case SSL_ST_BEFORE | SSL_ST_CONNECT:
222 case SSL_ST_OK | SSL_ST_CONNECT:
223
224 s->server = 0;
225 if (cb != NULL)
226 cb(s, SSL_CB_HANDSHAKE_START, 1);
227
13c9bb3e
MC
228 if ((s->version >> 8) != SSL3_VERSION_MAJOR
229 && s->version != TLS_ANY_VERSION) {
0f113f3e 230 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
cc273a93 231 s->state = SSL_ST_ERR;
0f113f3e
MC
232 ret = -1;
233 goto end;
234 }
235
d45ba43d
MC
236 if (s->version != TLS_ANY_VERSION &&
237 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
0f113f3e
MC
238 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
239 return -1;
240 }
241
242 /* s->version=SSL3_VERSION; */
243 s->type = SSL_ST_CONNECT;
244
245 if (s->init_buf == NULL) {
246 if ((buf = BUF_MEM_new()) == NULL) {
247 ret = -1;
cc273a93 248 s->state = SSL_ST_ERR;
0f113f3e
MC
249 goto end;
250 }
251 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
252 ret = -1;
cc273a93 253 s->state = SSL_ST_ERR;
0f113f3e
MC
254 goto end;
255 }
256 s->init_buf = buf;
257 buf = NULL;
258 }
259
260 if (!ssl3_setup_buffers(s)) {
261 ret = -1;
262 goto end;
263 }
264
265 /* setup buffing BIO */
266 if (!ssl_init_wbio_buffer(s, 0)) {
267 ret = -1;
cc273a93 268 s->state = SSL_ST_ERR;
0f113f3e
MC
269 goto end;
270 }
271
272 /* don't push the buffering BIO quite yet */
273
274 ssl3_init_finished_mac(s);
275
276 s->state = SSL3_ST_CW_CLNT_HELLO_A;
277 s->ctx->stats.sess_connect++;
278 s->init_num = 0;
0f113f3e
MC
279 /*
280 * Should have been reset by ssl3_get_finished, too.
281 */
282 s->s3->change_cipher_spec = 0;
283 break;
284
285 case SSL3_ST_CW_CLNT_HELLO_A:
286 case SSL3_ST_CW_CLNT_HELLO_B:
287
288 s->shutdown = 0;
289 ret = ssl3_client_hello(s);
290 if (ret <= 0)
291 goto end;
292 s->state = SSL3_ST_CR_SRVR_HELLO_A;
293 s->init_num = 0;
294
295 /* turn on buffering for the next lot of output */
296 if (s->bbio != s->wbio)
297 s->wbio = BIO_push(s->bbio, s->wbio);
298
299 break;
300
301 case SSL3_ST_CR_SRVR_HELLO_A:
302 case SSL3_ST_CR_SRVR_HELLO_B:
303 ret = ssl3_get_server_hello(s);
304 if (ret <= 0)
305 goto end;
306
307 if (s->hit) {
657da85e 308 s->state = SSL3_ST_CR_CHANGE_A;
0f113f3e
MC
309 if (s->tlsext_ticket_expected) {
310 /* receive renewed session ticket */
311 s->state = SSL3_ST_CR_SESSION_TICKET_A;
312 }
0f113f3e
MC
313 } else {
314 s->state = SSL3_ST_CR_CERT_A;
315 }
316 s->init_num = 0;
317 break;
318 case SSL3_ST_CR_CERT_A:
319 case SSL3_ST_CR_CERT_B:
6e3d0153 320 /* Noop (ret = 0) for everything but EAP-FAST. */
657da85e 321 ret = ssl3_check_change(s);
6e3d0153
EK
322 if (ret < 0)
323 goto end;
324 if (ret == 1) {
325 s->hit = 1;
657da85e 326 s->state = SSL3_ST_CR_CHANGE_A;
6e3d0153
EK
327 s->init_num = 0;
328 break;
329 }
e481f9b9 330
0f113f3e
MC
331 /* Check if it is anon DH/ECDH, SRP auth */
332 /* or PSK */
7689082b
DSH
333 if (!(s->s3->tmp.new_cipher->algorithm_auth &
334 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
0f113f3e
MC
335 ret = ssl3_get_server_certificate(s);
336 if (ret <= 0)
337 goto end;
e481f9b9 338
0f113f3e
MC
339 if (s->tlsext_status_expected)
340 s->state = SSL3_ST_CR_CERT_STATUS_A;
341 else
342 s->state = SSL3_ST_CR_KEY_EXCH_A;
343 } else {
344 skip = 1;
345 s->state = SSL3_ST_CR_KEY_EXCH_A;
346 }
0f113f3e 347
0f113f3e
MC
348 s->init_num = 0;
349 break;
350
351 case SSL3_ST_CR_KEY_EXCH_A:
352 case SSL3_ST_CR_KEY_EXCH_B:
353 ret = ssl3_get_key_exchange(s);
354 if (ret <= 0)
355 goto end;
356 s->state = SSL3_ST_CR_CERT_REQ_A;
357 s->init_num = 0;
358
359 /*
360 * at this point we check that we have the required stuff from
361 * the server
362 */
363 if (!ssl3_check_cert_and_algorithm(s)) {
364 ret = -1;
cc273a93 365 s->state = SSL_ST_ERR;
0f113f3e
MC
366 goto end;
367 }
368 break;
369
370 case SSL3_ST_CR_CERT_REQ_A:
371 case SSL3_ST_CR_CERT_REQ_B:
372 ret = ssl3_get_certificate_request(s);
373 if (ret <= 0)
374 goto end;
375 s->state = SSL3_ST_CR_SRVR_DONE_A;
376 s->init_num = 0;
377 break;
378
379 case SSL3_ST_CR_SRVR_DONE_A:
380 case SSL3_ST_CR_SRVR_DONE_B:
381 ret = ssl3_get_server_done(s);
382 if (ret <= 0)
383 goto end;
edc032b5 384#ifndef OPENSSL_NO_SRP
0f113f3e
MC
385 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
386 if ((ret = SRP_Calc_A_param(s)) <= 0) {
387 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
388 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
cc273a93 389 s->state = SSL_ST_ERR;
0f113f3e
MC
390 goto end;
391 }
392 }
393#endif
394 if (s->s3->tmp.cert_req)
395 s->state = SSL3_ST_CW_CERT_A;
396 else
397 s->state = SSL3_ST_CW_KEY_EXCH_A;
398 s->init_num = 0;
399
400 break;
401
402 case SSL3_ST_CW_CERT_A:
403 case SSL3_ST_CW_CERT_B:
404 case SSL3_ST_CW_CERT_C:
405 case SSL3_ST_CW_CERT_D:
406 ret = ssl3_send_client_certificate(s);
407 if (ret <= 0)
408 goto end;
409 s->state = SSL3_ST_CW_KEY_EXCH_A;
410 s->init_num = 0;
411 break;
412
413 case SSL3_ST_CW_KEY_EXCH_A:
414 case SSL3_ST_CW_KEY_EXCH_B:
415 ret = ssl3_send_client_key_exchange(s);
416 if (ret <= 0)
417 goto end;
418 /*
419 * EAY EAY EAY need to check for DH fix cert sent back
420 */
421 /*
422 * For TLS, cert_req is set to 2, so a cert chain of nothing is
423 * sent, but no verify packet is sent
424 */
425 /*
426 * XXX: For now, we do not support client authentication in ECDH
427 * cipher suites with ECDH (rather than ECDSA) certificates. We
428 * need to skip the certificate verify message when client's
429 * ECDH public key is sent inside the client certificate.
430 */
431 if (s->s3->tmp.cert_req == 1) {
432 s->state = SSL3_ST_CW_CERT_VRFY_A;
433 } else {
434 s->state = SSL3_ST_CW_CHANGE_A;
435 }
436 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
437 s->state = SSL3_ST_CW_CHANGE_A;
438 }
439
440 s->init_num = 0;
441 break;
442
443 case SSL3_ST_CW_CERT_VRFY_A:
444 case SSL3_ST_CW_CERT_VRFY_B:
445 ret = ssl3_send_client_verify(s);
446 if (ret <= 0)
447 goto end;
448 s->state = SSL3_ST_CW_CHANGE_A;
449 s->init_num = 0;
450 break;
451
452 case SSL3_ST_CW_CHANGE_A:
453 case SSL3_ST_CW_CHANGE_B:
454 ret = ssl3_send_change_cipher_spec(s,
455 SSL3_ST_CW_CHANGE_A,
456 SSL3_ST_CW_CHANGE_B);
457 if (ret <= 0)
458 goto end;
ee2ffc27 459
e481f9b9 460#if defined(OPENSSL_NO_NEXTPROTONEG)
0f113f3e 461 s->state = SSL3_ST_CW_FINISHED_A;
ee2ffc27 462#else
0f113f3e
MC
463 if (s->s3->next_proto_neg_seen)
464 s->state = SSL3_ST_CW_NEXT_PROTO_A;
465 else
466 s->state = SSL3_ST_CW_FINISHED_A;
ee2ffc27 467#endif
0f113f3e 468 s->init_num = 0;
d02b48c6 469
0f113f3e 470 s->session->cipher = s->s3->tmp.new_cipher;
09b6c2ef 471#ifdef OPENSSL_NO_COMP
0f113f3e 472 s->session->compress_meth = 0;
09b6c2ef 473#else
0f113f3e
MC
474 if (s->s3->tmp.new_compression == NULL)
475 s->session->compress_meth = 0;
476 else
477 s->session->compress_meth = s->s3->tmp.new_compression->id;
09b6c2ef 478#endif
0f113f3e
MC
479 if (!s->method->ssl3_enc->setup_key_block(s)) {
480 ret = -1;
cc273a93 481 s->state = SSL_ST_ERR;
0f113f3e
MC
482 goto end;
483 }
d02b48c6 484
0f113f3e
MC
485 if (!s->method->ssl3_enc->change_cipher_state(s,
486 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
487 {
488 ret = -1;
cc273a93 489 s->state = SSL_ST_ERR;
0f113f3e
MC
490 goto end;
491 }
d02b48c6 492
0f113f3e 493 break;
d02b48c6 494
e481f9b9 495#if !defined(OPENSSL_NO_NEXTPROTONEG)
0f113f3e
MC
496 case SSL3_ST_CW_NEXT_PROTO_A:
497 case SSL3_ST_CW_NEXT_PROTO_B:
498 ret = ssl3_send_next_proto(s);
499 if (ret <= 0)
500 goto end;
501 s->state = SSL3_ST_CW_FINISHED_A;
502 break;
503#endif
504
505 case SSL3_ST_CW_FINISHED_A:
506 case SSL3_ST_CW_FINISHED_B:
507 ret = ssl3_send_finished(s,
508 SSL3_ST_CW_FINISHED_A,
509 SSL3_ST_CW_FINISHED_B,
510 s->method->
511 ssl3_enc->client_finished_label,
512 s->method->
513 ssl3_enc->client_finished_label_len);
514 if (ret <= 0)
515 goto end;
516 s->state = SSL3_ST_CW_FLUSH;
517
0f113f3e
MC
518 if (s->hit) {
519 s->s3->tmp.next_state = SSL_ST_OK;
0f113f3e 520 } else {
0f113f3e
MC
521 /*
522 * Allow NewSessionTicket if ticket expected
523 */
524 if (s->tlsext_ticket_expected)
525 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
526 else
657da85e 527 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE_A;
0f113f3e
MC
528 }
529 s->init_num = 0;
530 break;
d02b48c6 531
0f113f3e
MC
532 case SSL3_ST_CR_SESSION_TICKET_A:
533 case SSL3_ST_CR_SESSION_TICKET_B:
534 ret = ssl3_get_new_session_ticket(s);
535 if (ret <= 0)
536 goto end;
657da85e 537 s->state = SSL3_ST_CR_CHANGE_A;
0f113f3e
MC
538 s->init_num = 0;
539 break;
540
541 case SSL3_ST_CR_CERT_STATUS_A:
542 case SSL3_ST_CR_CERT_STATUS_B:
543 ret = ssl3_get_cert_status(s);
544 if (ret <= 0)
545 goto end;
546 s->state = SSL3_ST_CR_KEY_EXCH_A;
547 s->init_num = 0;
548 break;
0f113f3e 549
657da85e
MC
550 case SSL3_ST_CR_CHANGE_A:
551 case SSL3_ST_CR_CHANGE_B:
552 ret = ssl3_get_change_cipher_spec(s, SSL3_ST_CR_CHANGE_A,
553 SSL3_ST_CR_CHANGE_B);
554 if (ret <= 0)
555 goto end;
556
557 s->state = SSL3_ST_CR_FINISHED_A;
558 s->init_num = 0;
559 break;
560
0f113f3e
MC
561 case SSL3_ST_CR_FINISHED_A:
562 case SSL3_ST_CR_FINISHED_B:
0f113f3e
MC
563 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
564 SSL3_ST_CR_FINISHED_B);
565 if (ret <= 0)
566 goto end;
567
568 if (s->hit)
569 s->state = SSL3_ST_CW_CHANGE_A;
570 else
571 s->state = SSL_ST_OK;
572 s->init_num = 0;
573 break;
574
575 case SSL3_ST_CW_FLUSH:
576 s->rwstate = SSL_WRITING;
577 if (BIO_flush(s->wbio) <= 0) {
578 ret = -1;
579 goto end;
580 }
581 s->rwstate = SSL_NOTHING;
582 s->state = s->s3->tmp.next_state;
583 break;
584
585 case SSL_ST_OK:
586 /* clean a few things up */
587 ssl3_cleanup_key_block(s);
25aaa98a
RS
588 BUF_MEM_free(s->init_buf);
589 s->init_buf = NULL;
0f113f3e 590
57787ac8
MC
591 /* remove the buffering */
592 ssl_free_wbio_buffer(s);
0f113f3e
MC
593
594 s->init_num = 0;
595 s->renegotiate = 0;
596 s->new_session = 0;
597
598 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
599 if (s->hit)
600 s->ctx->stats.sess_hit++;
601
602 ret = 1;
603 /* s->server=0; */
604 s->handshake_func = ssl3_connect;
605 s->ctx->stats.sess_connect_good++;
606
607 if (cb != NULL)
608 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
609
610 goto end;
611 /* break; */
612
cc273a93 613 case SSL_ST_ERR:
0f113f3e
MC
614 default:
615 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
616 ret = -1;
617 goto end;
618 /* break; */
619 }
620
621 /* did we do anything */
622 if (!s->s3->tmp.reuse_message && !skip) {
623 if (s->debug) {
624 if ((ret = BIO_flush(s->wbio)) <= 0)
625 goto end;
626 }
627
628 if ((cb != NULL) && (s->state != state)) {
629 new_state = s->state;
630 s->state = state;
631 cb(s, SSL_CB_CONNECT_LOOP, 1);
632 s->state = new_state;
633 }
634 }
635 skip = 0;
636 }
637 end:
638 s->in_handshake--;
25aaa98a 639 BUF_MEM_free(buf);
0f113f3e
MC
640 if (cb != NULL)
641 cb(s, SSL_CB_CONNECT_EXIT, ret);
642 return (ret);
643}
d02b48c6 644
d45ba43d
MC
645/*
646 * Work out what version we should be using for the initial ClientHello if
647 * the version is currently set to (D)TLS_ANY_VERSION.
648 * Returns 1 on success
649 * Returns 0 on error
650 */
651static int ssl_set_version(SSL *s)
652{
653 unsigned long mask, options = s->options;
654
655 if (s->method->version == TLS_ANY_VERSION) {
656 /*
657 * SSL_OP_NO_X disables all protocols above X *if* there are
658 * some protocols below X enabled. This is required in order
659 * to maintain "version capability" vector contiguous. So
660 * that if application wants to disable TLS1.0 in favour of
661 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
662 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
663 */
664 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
665#if !defined(OPENSSL_NO_SSL3)
666 | SSL_OP_NO_SSLv3
667#endif
668 ;
669#if !defined(OPENSSL_NO_TLS1_2_CLIENT)
670 if (options & SSL_OP_NO_TLSv1_2) {
671 if ((options & mask) != mask) {
672 s->version = TLS1_1_VERSION;
673 } else {
674 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
675 return 0;
676 }
677 } else {
678 s->version = TLS1_2_VERSION;
679 }
680#else
681 if ((options & mask) == mask) {
682 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
683 return 0;
684 }
685 s->version = TLS1_1_VERSION;
686#endif
687
688 mask &= ~SSL_OP_NO_TLSv1_1;
689 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
690 s->version = TLS1_VERSION;
691 mask &= ~SSL_OP_NO_TLSv1;
692#if !defined(OPENSSL_NO_SSL3)
693 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
694 s->version = SSL3_VERSION;
695#endif
696
697 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
698 SSLerr(SSL_F_SSL_SET_VERSION,
699 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
700 return 0;
701 }
702
703 if (s->version == SSL3_VERSION && FIPS_mode()) {
704 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
705 return 0;
706 }
707
708 } else if (s->method->version == DTLS_ANY_VERSION) {
709 /* Determine which DTLS version to use */
710 /* If DTLS 1.2 disabled correct the version number */
711 if (options & SSL_OP_NO_DTLSv1_2) {
712 if (tls1_suiteb(s)) {
713 SSLerr(SSL_F_SSL_SET_VERSION,
714 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
715 return 0;
716 }
717 /*
718 * Disabling all versions is silly: return an error.
719 */
720 if (options & SSL_OP_NO_DTLSv1) {
721 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
722 return 0;
723 }
724 /*
725 * Update method so we don't use any DTLS 1.2 features.
726 */
727 s->method = DTLSv1_client_method();
728 s->version = DTLS1_VERSION;
729 } else {
730 /*
731 * We only support one version: update method
732 */
733 if (options & SSL_OP_NO_DTLSv1)
734 s->method = DTLSv1_2_client_method();
735 s->version = DTLS1_2_VERSION;
736 }
737 }
738
739 s->client_version = s->version;
740
741 return 1;
742}
743
36d16f8e 744int ssl3_client_hello(SSL *s)
0f113f3e
MC
745{
746 unsigned char *buf;
747 unsigned char *p, *d;
748 int i;
749 unsigned long l;
750 int al = 0;
09b6c2ef 751#ifndef OPENSSL_NO_COMP
0f113f3e
MC
752 int j;
753 SSL_COMP *comp;
754#endif
755
756 buf = (unsigned char *)s->init_buf->data;
757 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
758 SSL_SESSION *sess = s->session;
13c9bb3e 759
d45ba43d
MC
760 /* Work out what SSL/TLS/DTLS version to use */
761 if (ssl_set_version(s) == 0)
762 goto err;
13c9bb3e
MC
763
764 if ((sess == NULL) || (sess->ssl_version != s->version) ||
13c9bb3e
MC
765 /*
766 * In the case of EAP-FAST, we can have a pre-shared
767 * "ticket" without a session ID.
768 */
769 (!sess->session_id_length && !sess->tlsext_tick) ||
13c9bb3e
MC
770 (sess->not_resumable)) {
771 if (!ssl_get_new_session(s, 0))
772 goto err;
773 }
0f113f3e
MC
774 /* else use the pre-loaded session */
775
776 p = s->s3->client_random;
777
778 /*
779 * for DTLS if client_random is initialized, reuse it, we are
780 * required to use same upon reply to HelloVerify
781 */
782 if (SSL_IS_DTLS(s)) {
783 size_t idx;
784 i = 1;
785 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
786 if (p[idx]) {
787 i = 0;
788 break;
789 }
790 }
791 } else
792 i = 1;
793
e1b568dd
MC
794 if (i && ssl_fill_hello_random(s, 0, p,
795 sizeof(s->s3->client_random)) <= 0)
796 goto err;
0f113f3e
MC
797
798 /* Do the message type and length last */
799 d = p = ssl_handshake_start(s);
800
35a1cc90
MC
801 /*-
802 * version indicates the negotiated version: for example from
803 * an SSLv2/v3 compatible client hello). The client_version
804 * field is the maximum version we permit and it is also
805 * used in RSA encrypted premaster secrets. Some servers can
806 * choke if we initially report a higher version then
807 * renegotiate to a lower one in the premaster secret. This
808 * didn't happen with TLS 1.0 as most servers supported it
809 * but it can with TLS 1.1 or later if the server only supports
810 * 1.0.
811 *
812 * Possible scenario with previous logic:
813 * 1. Client hello indicates TLS 1.2
814 * 2. Server hello says TLS 1.0
815 * 3. RSA encrypted premaster secret uses 1.2.
816 * 4. Handhaked proceeds using TLS 1.0.
817 * 5. Server sends hello request to renegotiate.
818 * 6. Client hello indicates TLS v1.0 as we now
819 * know that is maximum server supports.
820 * 7. Server chokes on RSA encrypted premaster secret
821 * containing version 1.0.
822 *
823 * For interoperability it should be OK to always use the
824 * maximum version we support in client hello and then rely
825 * on the checking of version to ensure the servers isn't
826 * being inconsistent: for example initially negotiating with
827 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
828 * client_version in client hello and not resetting it to
829 * the negotiated version.
830 */
0f113f3e
MC
831 *(p++) = s->client_version >> 8;
832 *(p++) = s->client_version & 0xff;
0f113f3e
MC
833
834 /* Random stuff */
835 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
836 p += SSL3_RANDOM_SIZE;
837
838 /* Session ID */
839 if (s->new_session)
840 i = 0;
841 else
842 i = s->session->session_id_length;
843 *(p++) = i;
844 if (i != 0) {
845 if (i > (int)sizeof(s->session->session_id)) {
846 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
847 goto err;
848 }
849 memcpy(p, s->session->session_id, i);
850 p += i;
851 }
852
853 /* cookie stuff for DTLS */
854 if (SSL_IS_DTLS(s)) {
855 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
856 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
857 goto err;
858 }
859 *(p++) = s->d1->cookie_len;
860 memcpy(p, s->d1->cookie, s->d1->cookie_len);
861 p += s->d1->cookie_len;
862 }
863
864 /* Ciphers supported */
865 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
866 if (i == 0) {
867 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
868 goto err;
869 }
800e1cd9 870#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
0f113f3e
MC
871 /*
872 * Some servers hang if client hello > 256 bytes as hack workaround
873 * chop number of supported ciphers to keep it well below this if we
874 * use TLS v1.2
875 */
876 if (TLS1_get_version(s) >= TLS1_2_VERSION
877 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
878 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
879#endif
880 s2n(i, p);
881 p += i;
882
883 /* COMPRESSION */
09b6c2ef 884#ifdef OPENSSL_NO_COMP
0f113f3e 885 *(p++) = 1;
09b6c2ef 886#else
566dda07 887
0f113f3e
MC
888 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
889 j = 0;
890 else
891 j = sk_SSL_COMP_num(s->ctx->comp_methods);
892 *(p++) = 1 + j;
893 for (i = 0; i < j; i++) {
894 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
895 *(p++) = comp->id;
896 }
09b6c2ef 897#endif
0f113f3e 898 *(p++) = 0; /* Add the NULL method */
761772d7 899
0f113f3e
MC
900 /* TLS extensions */
901 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
902 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
903 goto err;
904 }
905 if ((p =
906 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
907 &al)) == NULL) {
908 ssl3_send_alert(s, SSL3_AL_FATAL, al);
909 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
910 goto err;
911 }
0f113f3e
MC
912
913 l = p - d;
61986d32 914 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
77d514c5
MC
915 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
916 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
917 goto err;
918 }
0f113f3e
MC
919 s->state = SSL3_ST_CW_CLNT_HELLO_B;
920 }
921
922 /* SSL3_ST_CW_CLNT_HELLO_B */
923 return ssl_do_write(s);
924 err:
cc273a93 925 s->state = SSL_ST_ERR;
0f113f3e
MC
926 return (-1);
927}
d02b48c6 928
36d16f8e 929int ssl3_get_server_hello(SSL *s)
0f113f3e
MC
930{
931 STACK_OF(SSL_CIPHER) *sk;
932 const SSL_CIPHER *c;
50932c4a
MC
933 PACKET pkt;
934 unsigned char *session_id, *cipherchars;
0f113f3e 935 int i, al = SSL_AD_INTERNAL_ERROR, ok;
50932c4a 936 unsigned int j, ciphercharlen;
0f113f3e 937 long n;
09b6c2ef 938#ifndef OPENSSL_NO_COMP
0f113f3e
MC
939 SSL_COMP *comp;
940#endif
941 /*
942 * Hello verify request and/or server hello version may not match so set
943 * first packet if we're negotiating version.
944 */
81fc390f 945 s->first_packet = 1;
0f113f3e
MC
946
947 n = s->method->ssl_get_message(s,
948 SSL3_ST_CR_SRVR_HELLO_A,
949 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
950
951 if (!ok)
952 return ((int)n);
953
81fc390f 954 s->first_packet = 0;
0f113f3e 955 if (SSL_IS_DTLS(s)) {
0f113f3e
MC
956 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
957 if (s->d1->send_cookie == 0) {
958 s->s3->tmp.reuse_message = 1;
959 return 1;
960 } else { /* already sent a cookie */
961
962 al = SSL_AD_UNEXPECTED_MESSAGE;
963 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
964 goto f_err;
965 }
966 }
967 }
968
969 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
970 al = SSL_AD_UNEXPECTED_MESSAGE;
971 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
972 goto f_err;
973 }
974
50932c4a
MC
975 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
976 al = SSL_AD_INTERNAL_ERROR;
977 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
978 goto f_err;
979 }
13c9bb3e
MC
980
981 if (s->method->version == TLS_ANY_VERSION) {
50932c4a
MC
982 unsigned int sversion;
983
984 if (!PACKET_get_net_2(&pkt, &sversion)) {
985 al = SSL_AD_DECODE_ERROR;
986 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
987 goto f_err;
988 }
13c9bb3e
MC
989
990#if TLS_MAX_VERSION != TLS1_2_VERSION
991#error Code needs updating for new TLS version
992#endif
993#ifndef OPENSSL_NO_SSL3
994 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
995 if (FIPS_mode()) {
996 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
997 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
d45ba43d
MC
998 al = SSL_AD_PROTOCOL_VERSION;
999 goto f_err;
13c9bb3e
MC
1000 }
1001 s->method = SSLv3_client_method();
1002 } else
1003#endif
1004 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1005 s->method = TLSv1_client_method();
1006 } else if ((sversion == TLS1_1_VERSION) &&
1007 !(s->options & SSL_OP_NO_TLSv1_1)) {
1008 s->method = TLSv1_1_client_method();
1009 } else if ((sversion == TLS1_2_VERSION) &&
1010 !(s->options & SSL_OP_NO_TLSv1_2)) {
1011 s->method = TLSv1_2_client_method();
1012 } else {
1013 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
d45ba43d
MC
1014 al = SSL_AD_PROTOCOL_VERSION;
1015 goto f_err;
13c9bb3e
MC
1016 }
1017 s->session->ssl_version = s->version = s->method->version;
1018
1019 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1020 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
d45ba43d
MC
1021 al = SSL_AD_PROTOCOL_VERSION;
1022 goto f_err;
13c9bb3e
MC
1023 }
1024 } else if (s->method->version == DTLS_ANY_VERSION) {
0f113f3e 1025 /* Work out correct protocol version to use */
50932c4a
MC
1026 unsigned int hversion;
1027 int options;
1028
1029 if (!PACKET_get_net_2(&pkt, &hversion)) {
1030 al = SSL_AD_DECODE_ERROR;
1031 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1032 goto f_err;
1033 }
1034
1035 options = s->options;
0f113f3e
MC
1036 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1037 s->method = DTLSv1_2_client_method();
1038 else if (tls1_suiteb(s)) {
1039 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1040 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1041 s->version = hversion;
1042 al = SSL_AD_PROTOCOL_VERSION;
1043 goto f_err;
1044 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1045 s->method = DTLSv1_client_method();
1046 else {
1047 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1048 s->version = hversion;
1049 al = SSL_AD_PROTOCOL_VERSION;
1050 goto f_err;
1051 }
7322abf5 1052 s->session->ssl_version = s->version = s->method->version;
50932c4a
MC
1053 } else {
1054 unsigned char *vers;
1055
1056 if (!PACKET_get_bytes(&pkt, &vers, 2)) {
1057 al = SSL_AD_DECODE_ERROR;
1058 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1059 goto f_err;
1060 }
1061 if ((vers[0] != (s->version >> 8))
1062 || (vers[1] != (s->version & 0xff))) {
1063 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1064 s->version = (s->version & 0xff00) | vers[1];
1065 al = SSL_AD_PROTOCOL_VERSION;
1066 goto f_err;
1067 }
0f113f3e 1068 }
0f113f3e
MC
1069
1070 /* load the server hello data */
1071 /* load the server random */
50932c4a
MC
1072 if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1073 al = SSL_AD_DECODE_ERROR;
1074 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1075 goto f_err;
1076 }
0f113f3e
MC
1077
1078 s->hit = 0;
1079
50932c4a
MC
1080 /* get the session-id length */
1081 if (!PACKET_get_1(&pkt, &j)
1082 || (j > sizeof s->session->session_id)
1083 || (j > SSL3_SESSION_ID_SIZE)) {
0f113f3e
MC
1084 al = SSL_AD_ILLEGAL_PARAMETER;
1085 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1086 goto f_err;
1087 }
e481f9b9 1088
50932c4a 1089 ciphercharlen = ssl_put_cipher_by_char(s, NULL, NULL);
0f113f3e 1090 /*
6e3d0153
EK
1091 * Check if we can resume the session based on external pre-shared secret.
1092 * EAP-FAST (RFC 4851) supports two types of session resumption.
1093 * Resumption based on server-side state works with session IDs.
1094 * Resumption based on pre-shared Protected Access Credentials (PACs)
1095 * works by overriding the SessionTicket extension at the application
1096 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1097 * servers would honour the session ID.) Therefore, the session ID alone
1098 * is not a reliable indicator of session resumption, so we first check if
1099 * we can resume, and later peek at the next handshake message to see if the
1100 * server wants to resume.
0f113f3e 1101 */
6e3d0153
EK
1102 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1103 s->session->tlsext_tick) {
0f113f3e 1104 SSL_CIPHER *pref_cipher = NULL;
4bd16463
EK
1105 PACKET bookmark = pkt;
1106 if (!PACKET_forward(&pkt, j)
1107 || !PACKET_get_bytes(&pkt, &cipherchars, ciphercharlen)) {
50932c4a
MC
1108 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1109 al = SSL_AD_DECODE_ERROR;
1110 goto f_err;
1111 }
0f113f3e
MC
1112 s->session->master_key_length = sizeof(s->session->master_key);
1113 if (s->tls_session_secret_cb(s, s->session->master_key,
1114 &s->session->master_key_length,
1115 NULL, &pref_cipher,
1116 s->tls_session_secret_cb_arg)) {
1117 s->session->cipher = pref_cipher ?
50932c4a 1118 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
6e3d0153
EK
1119 } else {
1120 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1121 al = SSL_AD_INTERNAL_ERROR;
1122 goto f_err;
0f113f3e 1123 }
4bd16463 1124 pkt = bookmark;
50932c4a
MC
1125 }
1126
1127 /* Get the session id */
1128 if (!PACKET_get_bytes(&pkt, &session_id, j)) {
1129 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1130 al = SSL_AD_DECODE_ERROR;
1131 goto f_err;
0f113f3e 1132 }
0f113f3e 1133
6e3d0153 1134 if (j != 0 && j == s->session->session_id_length
50932c4a 1135 && memcmp(session_id, s->session->session_id, j) == 0) {
0f113f3e
MC
1136 if (s->sid_ctx_length != s->session->sid_ctx_length
1137 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1138 /* actually a client application bug */
1139 al = SSL_AD_ILLEGAL_PARAMETER;
1140 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1141 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1142 goto f_err;
1143 }
1144 s->hit = 1;
6e3d0153 1145 } else {
0f113f3e 1146 /*
6e3d0153
EK
1147 * If we were trying for session-id reuse but the server
1148 * didn't echo the ID, make a new SSL_SESSION.
1149 * In the case of EAP-FAST and PAC, we do not send a session ID,
1150 * so the PAC-based session secret is always preserved. It'll be
1151 * overwritten if the server refuses resumption.
0f113f3e
MC
1152 */
1153 if (s->session->session_id_length > 0) {
1154 if (!ssl_get_new_session(s, 0)) {
1155 goto f_err;
1156 }
1157 }
1158 s->session->session_id_length = j;
50932c4a
MC
1159 memcpy(s->session->session_id, session_id, j); /* j could be 0 */
1160 }
1161
1162 if (!PACKET_get_bytes(&pkt, &cipherchars, ciphercharlen)) {
1163 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1164 al = SSL_AD_DECODE_ERROR;
1165 goto f_err;
0f113f3e 1166 }
50932c4a 1167 c = ssl_get_cipher_by_char(s, cipherchars);
0f113f3e
MC
1168 if (c == NULL) {
1169 /* unknown cipher */
1170 al = SSL_AD_ILLEGAL_PARAMETER;
1171 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1172 goto f_err;
1173 }
1174 /* Set version disabled mask now we know version */
1175 if (!SSL_USE_TLS1_2_CIPHERS(s))
4d69f9e6 1176 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
0f113f3e 1177 else
4d69f9e6 1178 s->s3->tmp.mask_ssl = 0;
0f113f3e
MC
1179 /*
1180 * If it is a disabled cipher we didn't send it in client hello, so
1181 * return an error.
1182 */
1183 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1184 al = SSL_AD_ILLEGAL_PARAMETER;
1185 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1186 goto f_err;
1187 }
0f113f3e
MC
1188
1189 sk = ssl_get_ciphers_by_id(s);
1190 i = sk_SSL_CIPHER_find(sk, c);
1191 if (i < 0) {
1192 /* we did not say we would use this cipher */
1193 al = SSL_AD_ILLEGAL_PARAMETER;
1194 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1195 goto f_err;
1196 }
1197
1198 /*
1199 * Depending on the session caching (internal/external), the cipher
1200 * and/or cipher_id values may not be set. Make sure that cipher_id is
1201 * set and use it for comparison.
1202 */
1203 if (s->session->cipher)
1204 s->session->cipher_id = s->session->cipher->id;
1205 if (s->hit && (s->session->cipher_id != c->id)) {
9e9858d1
RS
1206 al = SSL_AD_ILLEGAL_PARAMETER;
1207 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1208 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1209 goto f_err;
0f113f3e
MC
1210 }
1211 s->s3->tmp.new_cipher = c;
1212 /*
1213 * Don't digest cached records if no sigalgs: we may need them for client
1214 * authentication.
1215 */
124037fd 1216 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
0f113f3e
MC
1217 goto f_err;
1218 /* lets get the compression algorithm */
1219 /* COMPRESSION */
50932c4a
MC
1220 if (!PACKET_get_1(&pkt, &j)) {
1221 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1222 al = SSL_AD_DECODE_ERROR;
1223 goto f_err;
1224 }
09b6c2ef 1225#ifdef OPENSSL_NO_COMP
50932c4a 1226 if (j != 0) {
0f113f3e
MC
1227 al = SSL_AD_ILLEGAL_PARAMETER;
1228 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1229 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1230 goto f_err;
1231 }
1232 /*
1233 * If compression is disabled we'd better not try to resume a session
1234 * using compression.
1235 */
1236 if (s->session->compress_meth != 0) {
1237 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1238 goto f_err;
1239 }
09b6c2ef 1240#else
0f113f3e
MC
1241 if (s->hit && j != s->session->compress_meth) {
1242 al = SSL_AD_ILLEGAL_PARAMETER;
1243 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1244 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1245 goto f_err;
1246 }
1247 if (j == 0)
1248 comp = NULL;
1249 else if (!ssl_allow_compression(s)) {
1250 al = SSL_AD_ILLEGAL_PARAMETER;
1251 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1252 goto f_err;
1253 } else
1254 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1255
1256 if ((j != 0) && (comp == NULL)) {
1257 al = SSL_AD_ILLEGAL_PARAMETER;
1258 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1259 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1260 goto f_err;
1261 } else {
1262 s->s3->tmp.new_compression = comp;
1263 }
09b6c2ef 1264#endif
761772d7 1265
0f113f3e 1266 /* TLS extensions */
50932c4a 1267 if (!ssl_parse_serverhello_tlsext(s, &pkt)) {
0f113f3e
MC
1268 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1269 goto err;
1270 }
0f113f3e 1271
50932c4a 1272 if (PACKET_remaining(&pkt) != 0) {
0f113f3e
MC
1273 /* wrong packet length */
1274 al = SSL_AD_DECODE_ERROR;
1275 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1276 goto f_err;
1277 }
1278
1279 return (1);
1280 f_err:
1281 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1282 err:
cc273a93 1283 s->state = SSL_ST_ERR;
0f113f3e
MC
1284 return (-1);
1285}
d02b48c6 1286
36d16f8e 1287int ssl3_get_server_certificate(SSL *s)
0f113f3e 1288{
55a9a16f 1289 int al, i, ok, ret = -1, exp_idx;
df758a85 1290 unsigned long n, cert_list_len, cert_len;
0f113f3e 1291 X509 *x = NULL;
df758a85 1292 unsigned char *certstart, *certbytes;
0f113f3e 1293 STACK_OF(X509) *sk = NULL;
0f113f3e 1294 EVP_PKEY *pkey = NULL;
df758a85 1295 PACKET pkt;
0f113f3e
MC
1296
1297 n = s->method->ssl_get_message(s,
1298 SSL3_ST_CR_CERT_A,
1299 SSL3_ST_CR_CERT_B,
1300 -1, s->max_cert_list, &ok);
1301
1302 if (!ok)
1303 return ((int)n);
1304
55a9a16f 1305 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
0f113f3e
MC
1306 s->s3->tmp.reuse_message = 1;
1307 return (1);
1308 }
1309
1310 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1311 al = SSL_AD_UNEXPECTED_MESSAGE;
1312 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1313 goto f_err;
1314 }
df758a85
MC
1315
1316 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1317 al = SSL_AD_INTERNAL_ERROR;
1318 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1319 goto f_err;
1320 }
0f113f3e
MC
1321
1322 if ((sk = sk_X509_new_null()) == NULL) {
1323 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 1324 goto err;
0f113f3e
MC
1325 }
1326
df758a85
MC
1327 if (!PACKET_get_net_3(&pkt, &cert_list_len)
1328 || PACKET_remaining(&pkt) != cert_list_len) {
0f113f3e
MC
1329 al = SSL_AD_DECODE_ERROR;
1330 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1331 goto f_err;
1332 }
df758a85
MC
1333 while (PACKET_remaining(&pkt)) {
1334 if (!PACKET_get_net_3(&pkt, &cert_len)
1335 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
0f113f3e
MC
1336 al = SSL_AD_DECODE_ERROR;
1337 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1338 SSL_R_CERT_LENGTH_MISMATCH);
1339 goto f_err;
1340 }
1341
df758a85
MC
1342 certstart = certbytes;
1343 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
0f113f3e
MC
1344 if (x == NULL) {
1345 al = SSL_AD_BAD_CERTIFICATE;
1346 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1347 goto f_err;
1348 }
df758a85 1349 if (certbytes != (certstart + cert_len)) {
0f113f3e
MC
1350 al = SSL_AD_DECODE_ERROR;
1351 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1352 SSL_R_CERT_LENGTH_MISMATCH);
1353 goto f_err;
1354 }
1355 if (!sk_X509_push(sk, x)) {
1356 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 1357 goto err;
0f113f3e
MC
1358 }
1359 x = NULL;
0f113f3e
MC
1360 }
1361
1362 i = ssl_verify_cert_chain(s, sk);
55a9a16f 1363 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
0f113f3e
MC
1364 al = ssl_verify_alarm_type(s->verify_result);
1365 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1366 SSL_R_CERTIFICATE_VERIFY_FAILED);
1367 goto f_err;
1368 }
1369 ERR_clear_error(); /* but we keep s->verify_result */
1370 if (i > 1) {
1371 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1372 al = SSL_AD_HANDSHAKE_FAILURE;
1373 goto f_err;
1374 }
1375
c34b0f99 1376 s->session->peer_chain = sk;
0f113f3e
MC
1377 /*
1378 * Inconsistency alert: cert_chain does include the peer's certificate,
1379 * which we don't include in s3_srvr.c
1380 */
1381 x = sk_X509_value(sk, 0);
1382 sk = NULL;
1383 /*
1384 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1385 */
1386
1387 pkey = X509_get_pubkey(x);
1388
55a9a16f 1389 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
0f113f3e
MC
1390 x = NULL;
1391 al = SSL3_AL_FATAL;
1392 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1393 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1394 goto f_err;
1395 }
1396
1397 i = ssl_cert_type(x, pkey);
55a9a16f 1398 if (i < 0) {
0f113f3e
MC
1399 x = NULL;
1400 al = SSL3_AL_FATAL;
1401 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1402 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1403 goto f_err;
1404 }
1405
55a9a16f
MC
1406 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1407 if (exp_idx >= 0 && i != exp_idx) {
1408 x = NULL;
1409 al = SSL_AD_ILLEGAL_PARAMETER;
1410 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1411 SSL_R_WRONG_CERTIFICATE_TYPE);
1412 goto f_err;
0f113f3e 1413 }
a273c6ee 1414 s->session->peer_type = i;
55a9a16f
MC
1415
1416 X509_free(s->session->peer);
05f0fb9f 1417 X509_up_ref(x);
55a9a16f 1418 s->session->peer = x;
0f113f3e
MC
1419 s->session->verify_result = s->verify_result;
1420
1421 x = NULL;
1422 ret = 1;
66696478
RS
1423 goto done;
1424
0f113f3e 1425 f_err:
66696478 1426 ssl3_send_alert(s, SSL3_AL_FATAL, al);
cc273a93
MC
1427 err:
1428 s->state = SSL_ST_ERR;
66696478 1429 done:
0f113f3e
MC
1430 EVP_PKEY_free(pkey);
1431 X509_free(x);
1432 sk_X509_pop_free(sk, X509_free);
1433 return (ret);
1434}
d02b48c6 1435
36d16f8e 1436int ssl3_get_key_exchange(SSL *s)
0f113f3e 1437{
bc36ee62 1438#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1439 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1440#endif
1441 EVP_MD_CTX md_ctx;
8098fc56
MC
1442 int al, j, verify_ret, ok;
1443 unsigned int i;
1444 long n, alg_k, alg_a;
0f113f3e
MC
1445 EVP_PKEY *pkey = NULL;
1446 const EVP_MD *md = NULL;
bc36ee62 1447#ifndef OPENSSL_NO_RSA
0f113f3e 1448 RSA *rsa = NULL;
79df9d62 1449#endif
bc36ee62 1450#ifndef OPENSSL_NO_DH
0f113f3e 1451 DH *dh = NULL;
58964a49 1452#endif
10bf4fc2 1453#ifndef OPENSSL_NO_EC
0f113f3e
MC
1454 EC_KEY *ecdh = NULL;
1455 BN_CTX *bn_ctx = NULL;
1456 EC_POINT *srvr_ecpoint = NULL;
1457 int curve_nid = 0;
8098fc56 1458 unsigned int encoded_pt_len = 0;
0f113f3e 1459#endif
4bd16463 1460 PACKET pkt, save_param_start;
8098fc56 1461 unsigned char *data, *param;
4bd16463 1462 size_t param_len;
0f113f3e
MC
1463
1464 EVP_MD_CTX_init(&md_ctx);
1465
1466 /*
1467 * use same message size as in ssl3_get_certificate_request() as
1468 * ServerKeyExchange message may be skipped
1469 */
1470 n = s->method->ssl_get_message(s,
1471 SSL3_ST_CR_KEY_EXCH_A,
1472 SSL3_ST_CR_KEY_EXCH_B,
1473 -1, s->max_cert_list, &ok);
1474 if (!ok)
1475 return ((int)n);
1476
1477 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1478
1479 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1480 /*
1481 * Can't skip server key exchange if this is an ephemeral
1482 * ciphersuite.
1483 */
7689082b 1484 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
0f113f3e
MC
1485 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1486 al = SSL_AD_UNEXPECTED_MESSAGE;
1487 goto f_err;
1488 }
a16ca4e8 1489
0f113f3e
MC
1490 s->s3->tmp.reuse_message = 1;
1491 return (1);
1492 }
1493
4bd16463 1494 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
8098fc56
MC
1495 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1496 al = SSL_AD_INTERNAL_ERROR;
1497 goto f_err;
1498 }
4bd16463 1499 save_param_start = pkt;
8d92c1f8 1500
bc36ee62 1501#ifndef OPENSSL_NO_RSA
8d92c1f8
DSH
1502 RSA_free(s->s3->peer_rsa_tmp);
1503 s->s3->peer_rsa_tmp = NULL;
d02b48c6 1504#endif
bc36ee62 1505#ifndef OPENSSL_NO_DH
8d92c1f8
DSH
1506 DH_free(s->s3->peer_dh_tmp);
1507 s->s3->peer_dh_tmp = NULL;
ea262260 1508#endif
10bf4fc2 1509#ifndef OPENSSL_NO_EC
8d92c1f8
DSH
1510 EC_KEY_free(s->s3->peer_ecdh_tmp);
1511 s->s3->peer_ecdh_tmp = NULL;
d02b48c6 1512#endif
8d92c1f8 1513
0f113f3e 1514 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 1515
0f113f3e 1516 al = SSL_AD_DECODE_ERROR;
f2be92b9 1517
ddac1974 1518#ifndef OPENSSL_NO_PSK
7689082b
DSH
1519 /* PSK ciphersuites are preceded by an identity hint */
1520 if (alg_k & SSL_PSK) {
0f113f3e 1521
8098fc56 1522 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1524 goto f_err;
1525 }
0f113f3e
MC
1526
1527 /*
1528 * Store PSK identity hint for later use, hint is used in
1529 * ssl3_send_client_key_exchange. Assume that the maximum length of
1530 * a PSK identity hint can be as long as the maximum length of a PSK
1531 * identity.
1532 */
1533 if (i > PSK_MAX_IDENTITY_LEN) {
1534 al = SSL_AD_HANDSHAKE_FAILURE;
1535 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1536 goto f_err;
1537 }
8098fc56 1538 if (PACKET_remaining(&pkt) < i) {
0f113f3e
MC
1539 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1540 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1541 goto f_err;
1542 }
0f113f3e 1543
a16ca4e8 1544 OPENSSL_free(s->session->psk_identity_hint);
7689082b 1545 if (i != 0) {
8098fc56
MC
1546 unsigned char *hint = NULL;
1547
1548 if (!PACKET_get_bytes(&pkt, &hint, i)) {
1549 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1550 goto f_err;
1551 }
1552 s->session->psk_identity_hint = BUF_strndup((char *)hint, i);
7689082b
DSH
1553 if (s->session->psk_identity_hint == NULL) {
1554 al = SSL_AD_HANDSHAKE_FAILURE;
1555 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1556 goto f_err;
1557 }
1558 } else {
1559 s->session->psk_identity_hint = NULL;
0f113f3e 1560 }
7689082b
DSH
1561 }
1562
1563 /* Nothing else to do for plain PSK or RSAPSK */
1564 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
0f113f3e
MC
1565 } else
1566#endif /* !OPENSSL_NO_PSK */
edc032b5 1567#ifndef OPENSSL_NO_SRP
0f113f3e 1568 if (alg_k & SSL_kSRP) {
8098fc56 1569 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1570 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1571 goto f_err;
1572 }
0f113f3e 1573
8098fc56 1574 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1575 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1576 goto f_err;
1577 }
0f113f3e 1578
8098fc56 1579 if ((s->srp_ctx.N = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1580 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1581 goto err;
1582 }
0f113f3e 1583
8098fc56 1584 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1585 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1586 goto f_err;
1587 }
0f113f3e 1588
8098fc56 1589 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1590 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1591 goto f_err;
1592 }
0f113f3e 1593
8098fc56 1594 if ((s->srp_ctx.g = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1595 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1596 goto err;
1597 }
0f113f3e 1598
8098fc56 1599 if (!PACKET_get_1(&pkt, &i)) {
0f113f3e
MC
1600 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1601 goto f_err;
1602 }
0f113f3e 1603
8098fc56 1604 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1605 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1606 goto f_err;
1607 }
0f113f3e 1608
8098fc56 1609 if ((s->srp_ctx.s = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1610 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1611 goto err;
1612 }
0f113f3e 1613
8098fc56 1614 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1616 goto f_err;
1617 }
0f113f3e 1618
8098fc56 1619 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1620 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1621 goto f_err;
1622 }
0f113f3e 1623
8098fc56 1624 if ((s->srp_ctx.B = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1625 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1626 goto err;
1627 }
0f113f3e
MC
1628
1629 if (!srp_verify_server_param(s, &al)) {
1630 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1631 goto f_err;
1632 }
0989790b 1633
edc032b5 1634/* We must check if there is a certificate */
8df53b7a 1635 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1636 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1637 } else
1638#endif /* !OPENSSL_NO_SRP */
edc032b5 1639#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1640 if (alg_k & SSL_kRSA) {
1641 /* Temporary RSA keys only allowed in export ciphersuites */
1642 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1643 al = SSL_AD_UNEXPECTED_MESSAGE;
1644 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1645 goto f_err;
1646 }
1647 if ((rsa = RSA_new()) == NULL) {
1648 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1649 goto err;
1650 }
1651
8098fc56 1652 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1653 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1654 goto f_err;
1655 }
0f113f3e 1656
8098fc56 1657 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1658 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1659 goto f_err;
1660 }
0f113f3e 1661
8098fc56 1662 if ((rsa->n = BN_bin2bn(data, i, rsa->n)) == NULL) {
0f113f3e
MC
1663 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1664 goto err;
1665 }
0f113f3e 1666
8098fc56 1667 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1668 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1669 goto f_err;
1670 }
0f113f3e 1671
8098fc56 1672 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1673 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1674 goto f_err;
1675 }
0f113f3e 1676
8098fc56 1677 if ((rsa->e = BN_bin2bn(data, i, rsa->e)) == NULL) {
0f113f3e
MC
1678 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1679 goto err;
1680 }
0f113f3e
MC
1681
1682 /* this should be because we are using an export cipher */
1683 if (alg_a & SSL_aRSA)
a273c6ee 1684 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1685 else {
1686 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1687 goto err;
1688 }
1dece951
KR
1689
1690 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1691 al = SSL_AD_UNEXPECTED_MESSAGE;
1692 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1693 goto f_err;
1694 }
1695
8d92c1f8 1696 s->s3->peer_rsa_tmp = rsa;
0f113f3e
MC
1697 rsa = NULL;
1698 }
1699#else /* OPENSSL_NO_RSA */
1700 if (0) ;
d02b48c6 1701#endif
bc36ee62 1702#ifndef OPENSSL_NO_DH
7689082b 1703 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
0f113f3e
MC
1704 if ((dh = DH_new()) == NULL) {
1705 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1706 goto err;
1707 }
1708
8098fc56 1709 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1710 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1711 goto f_err;
1712 }
0f113f3e 1713
8098fc56 1714 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1715 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1716 goto f_err;
1717 }
0f113f3e 1718
8098fc56 1719 if ((dh->p = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1720 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1721 goto err;
1722 }
0f113f3e 1723
61e72d76
GL
1724 if (BN_is_zero(dh->p)) {
1725 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1726 goto f_err;
1727 }
1728
8098fc56 1729 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1730 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1731 goto f_err;
1732 }
0f113f3e 1733
8098fc56 1734 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1735 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1736 goto f_err;
1737 }
0f113f3e 1738
8098fc56 1739 if ((dh->g = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1740 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1741 goto err;
1742 }
0f113f3e 1743
61e72d76
GL
1744 if (BN_is_zero(dh->g)) {
1745 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1746 goto f_err;
1747 }
1748
8098fc56 1749 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1750 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1751 goto f_err;
1752 }
0f113f3e 1753
8098fc56 1754 if (!PACKET_get_bytes(&pkt, &data, i)) {
0f113f3e
MC
1755 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1756 goto f_err;
1757 }
0f113f3e 1758
8098fc56 1759 if ((dh->pub_key = BN_bin2bn(data, i, NULL)) == NULL) {
0f113f3e
MC
1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1761 goto err;
1762 }
0f113f3e 1763
61e72d76
GL
1764 if (BN_is_zero(dh->pub_key)) {
1765 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1766 goto f_err;
1767 }
1768
0f113f3e
MC
1769 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1770 al = SSL_AD_HANDSHAKE_FAILURE;
1771 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1772 goto f_err;
1773 }
8df53b7a 1774 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1775 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1776 /* else anonymous DH, so no certificate or pkey. */
1777
8d92c1f8 1778 s->s3->peer_dh_tmp = dh;
0f113f3e 1779 dh = NULL;
0f113f3e
MC
1780 }
1781#endif /* !OPENSSL_NO_DH */
ea262260 1782
10bf4fc2 1783#ifndef OPENSSL_NO_EC
7689082b 1784 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
0f113f3e
MC
1785 EC_GROUP *ngroup;
1786 const EC_GROUP *group;
1787
1788 if ((ecdh = EC_KEY_new()) == NULL) {
1789 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1790 goto err;
1791 }
1792
1793 /*
1794 * Extract elliptic curve parameters and the server's ephemeral ECDH
1795 * public key. Keep accumulating lengths of various components in
1796 * param_len and make sure it never exceeds n.
1797 */
1798
1799 /*
1800 * XXX: For now we only support named (not generic) curves and the
8098fc56 1801 * ECParameters in this case is just three bytes.
0f113f3e 1802 */
8098fc56 1803 if (!PACKET_get_bytes(&pkt, &data, 3)) {
0f113f3e
MC
1804 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1805 goto f_err;
1806 }
1807 /*
1808 * Check curve is one of our preferences, if not server has sent an
1809 * invalid curve. ECParameters is 3 bytes.
1810 */
8098fc56 1811 if (!tls1_check_curve(s, data, 3)) {
0f113f3e
MC
1812 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1813 goto f_err;
1814 }
1815
8098fc56 1816 if ((curve_nid = tls1_ec_curve_id2nid(*(data + 2))) == 0) {
0f113f3e
MC
1817 al = SSL_AD_INTERNAL_ERROR;
1818 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1819 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1820 goto f_err;
1821 }
1822
1823 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1824 if (ngroup == NULL) {
1825 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1826 goto err;
1827 }
1828 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1829 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1830 goto err;
1831 }
1832 EC_GROUP_free(ngroup);
1833
1834 group = EC_KEY_get0_group(ecdh);
1835
1836 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1837 (EC_GROUP_get_degree(group) > 163)) {
1838 al = SSL_AD_EXPORT_RESTRICTION;
1839 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1840 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1841 goto f_err;
1842 }
1843
0f113f3e
MC
1844 /* Next, get the encoded ECPoint */
1845 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1846 ((bn_ctx = BN_CTX_new()) == NULL)) {
1847 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1848 goto err;
1849 }
1850
8098fc56
MC
1851 if (!PACKET_get_1(&pkt, &encoded_pt_len)) {
1852 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1853 goto f_err;
1854 }
0f113f3e 1855
8098fc56 1856 if (!PACKET_get_bytes(&pkt, &data, encoded_pt_len) ||
0f113f3e 1857 (EC_POINT_oct2point(group, srvr_ecpoint,
8098fc56 1858 data, encoded_pt_len, bn_ctx) == 0)) {
0f113f3e
MC
1859 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1860 goto f_err;
1861 }
0f113f3e
MC
1862
1863 /*
1864 * The ECC/TLS specification does not mention the use of DSA to sign
1865 * ECParameters in the server key exchange message. We do support RSA
1866 * and ECDSA.
1867 */
1868 if (0) ;
1869# ifndef OPENSSL_NO_RSA
1870 else if (alg_a & SSL_aRSA)
a273c6ee 1871 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1872# endif
10bf4fc2 1873# ifndef OPENSSL_NO_EC
0f113f3e 1874 else if (alg_a & SSL_aECDSA)
a273c6ee 1875 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1876# endif
1877 /* else anonymous ECDH, so no certificate or pkey. */
1878 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
8d92c1f8 1879 s->s3->peer_ecdh_tmp = ecdh;
0f113f3e
MC
1880 ecdh = NULL;
1881 BN_CTX_free(bn_ctx);
1882 bn_ctx = NULL;
1883 EC_POINT_free(srvr_ecpoint);
1884 srvr_ecpoint = NULL;
1885 } else if (alg_k) {
1886 al = SSL_AD_UNEXPECTED_MESSAGE;
1887 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1888 goto f_err;
1889 }
10bf4fc2 1890#endif /* !OPENSSL_NO_EC */
0f113f3e 1891
4bd16463
EK
1892 /*
1893 * |pkt| now points to the beginning of the signature, so the difference
1894 * equals the length of the parameters.
1895 */
1896 param_len = PACKET_remaining(&save_param_start) - PACKET_remaining(&pkt);
0f113f3e
MC
1897
1898 /* if it was signed, check the signature */
1899 if (pkey != NULL) {
1900 if (SSL_USE_SIGALGS(s)) {
1901 int rv;
8098fc56 1902 if (!PACKET_get_bytes(&pkt, &data, 2)) {
0f113f3e
MC
1903 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1904 goto f_err;
1905 }
8098fc56 1906 rv = tls12_check_peer_sigalg(&md, s, data, pkey);
0f113f3e
MC
1907 if (rv == -1)
1908 goto err;
1909 else if (rv == 0) {
1910 goto f_err;
1911 }
a2f9200f 1912#ifdef SSL_DEBUG
0f113f3e
MC
1913 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1914#endif
0f113f3e
MC
1915 } else
1916 md = EVP_sha1();
1917
8098fc56 1918 if (!PACKET_get_net_2(&pkt, &i)) {
0f113f3e
MC
1919 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1920 goto f_err;
1921 }
0f113f3e 1922 j = EVP_PKEY_size(pkey);
8098fc56
MC
1923 if (j < 0) {
1924 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1925 goto f_err;
1926 }
0f113f3e
MC
1927
1928 /*
8098fc56 1929 * Check signature length
0f113f3e 1930 */
8098fc56
MC
1931 if (i > (unsigned int)j
1932 || !PACKET_get_bytes(&pkt, &data, i)
1933 || PACKET_remaining(&pkt) != 0) {
0f113f3e
MC
1934 /* wrong packet length */
1935 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1936 goto f_err;
1937 }
4bd16463
EK
1938 pkt = save_param_start;
1939 if (!PACKET_get_bytes(&pkt, &param, param_len)) {
8098fc56
MC
1940 al = SSL_AD_INTERNAL_ERROR;
1941 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1942 goto f_err;
1943 }
bc36ee62 1944#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1945 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1946 int num;
1947 unsigned int size;
1948
1949 j = 0;
1950 q = md_buf;
1951 for (num = 2; num > 0; num--) {
1952 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1953 EVP_DigestInit_ex(&md_ctx, (num == 2)
1954 ? s->ctx->md5 : s->ctx->sha1, NULL);
1955 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1956 SSL3_RANDOM_SIZE);
1957 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1958 SSL3_RANDOM_SIZE);
4bd16463 1959 EVP_DigestUpdate(&md_ctx, param, param_len);
0f113f3e
MC
1960 EVP_DigestFinal_ex(&md_ctx, q, &size);
1961 q += size;
1962 j += size;
1963 }
8098fc56
MC
1964 verify_ret =
1965 RSA_verify(NID_md5_sha1, md_buf, j, data, i, pkey->pkey.rsa);
1966 if (verify_ret < 0) {
0f113f3e
MC
1967 al = SSL_AD_DECRYPT_ERROR;
1968 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1969 goto f_err;
1970 }
8098fc56 1971 if (verify_ret == 0) {
0f113f3e
MC
1972 /* bad signature */
1973 al = SSL_AD_DECRYPT_ERROR;
1974 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1975 goto f_err;
1976 }
1977 } else
1978#endif
1979 {
1980 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1981 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1982 SSL3_RANDOM_SIZE);
1983 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1984 SSL3_RANDOM_SIZE);
4bd16463 1985 EVP_VerifyUpdate(&md_ctx, param, param_len);
8098fc56 1986 if (EVP_VerifyFinal(&md_ctx, data, (int)i, pkey) <= 0) {
0f113f3e
MC
1987 /* bad signature */
1988 al = SSL_AD_DECRYPT_ERROR;
1989 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1990 goto f_err;
1991 }
1992 }
1993 } else {
7689082b
DSH
1994 /* aNULL, aSRP or PSK do not need public keys */
1995 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
0f113f3e
MC
1996 /* Might be wrong key type, check it */
1997 if (ssl3_check_cert_and_algorithm(s))
1998 /* Otherwise this shouldn't happen */
1999 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2000 goto err;
2001 }
2002 /* still data left over */
8098fc56 2003 if (PACKET_remaining(&pkt) != 0) {
0f113f3e
MC
2004 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2005 goto f_err;
2006 }
2007 }
2008 EVP_PKEY_free(pkey);
2009 EVP_MD_CTX_cleanup(&md_ctx);
2010 return (1);
2011 f_err:
2012 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2013 err:
2014 EVP_PKEY_free(pkey);
bc36ee62 2015#ifndef OPENSSL_NO_RSA
d6407083 2016 RSA_free(rsa);
6b521df3 2017#endif
bc36ee62 2018#ifndef OPENSSL_NO_DH
d6407083 2019 DH_free(dh);
ea262260 2020#endif
10bf4fc2 2021#ifndef OPENSSL_NO_EC
0f113f3e
MC
2022 BN_CTX_free(bn_ctx);
2023 EC_POINT_free(srvr_ecpoint);
8fdc3734 2024 EC_KEY_free(ecdh);
6b521df3 2025#endif
0f113f3e 2026 EVP_MD_CTX_cleanup(&md_ctx);
cc273a93 2027 s->state = SSL_ST_ERR;
0f113f3e
MC
2028 return (-1);
2029}
d02b48c6 2030
36d16f8e 2031int ssl3_get_certificate_request(SSL *s)
0f113f3e
MC
2032{
2033 int ok, ret = 0;
ac112332
MC
2034 unsigned long n;
2035 unsigned int list_len, ctype_num, i, name_len;
0f113f3e 2036 X509_NAME *xn = NULL;
ac112332
MC
2037 unsigned char *data;
2038 unsigned char *namestart, *namebytes;
0f113f3e 2039 STACK_OF(X509_NAME) *ca_sk = NULL;
ac112332 2040 PACKET pkt;
0f113f3e
MC
2041
2042 n = s->method->ssl_get_message(s,
2043 SSL3_ST_CR_CERT_REQ_A,
2044 SSL3_ST_CR_CERT_REQ_B,
2045 -1, s->max_cert_list, &ok);
2046
2047 if (!ok)
2048 return ((int)n);
2049
2050 s->s3->tmp.cert_req = 0;
2051
2052 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2053 s->s3->tmp.reuse_message = 1;
2054 /*
2055 * If we get here we don't need any cached handshake records as we
2056 * wont be doing client auth.
2057 */
124037fd
DSH
2058 if (!ssl3_digest_cached_records(s, 0))
2059 goto err;
0f113f3e
MC
2060 return (1);
2061 }
2062
2063 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2064 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2065 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2066 goto err;
2067 }
2068
2069 /* TLS does not like anon-DH with client cert */
2070 if (s->version > SSL3_VERSION) {
2071 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2072 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2073 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2074 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2075 goto err;
2076 }
2077 }
2078
ac112332
MC
2079 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2080 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2081 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2082 goto err;
2083 }
0f113f3e
MC
2084
2085 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2086 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2087 goto err;
2088 }
2089
2090 /* get the certificate types */
ac112332
MC
2091 if (!PACKET_get_1(&pkt, &ctype_num)
2092 || !PACKET_get_bytes(&pkt, &data, ctype_num)) {
2093 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2094 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2095 goto err;
2096 }
b548a1f1
RS
2097 OPENSSL_free(s->cert->ctypes);
2098 s->cert->ctypes = NULL;
0f113f3e
MC
2099 if (ctype_num > SSL3_CT_NUMBER) {
2100 /* If we exceed static buffer copy all to cert structure */
2101 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2102 if (s->cert->ctypes == NULL) {
2103 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2104 goto err;
2105 }
ac112332 2106 memcpy(s->cert->ctypes, data, ctype_num);
0f113f3e
MC
2107 s->cert->ctype_num = (size_t)ctype_num;
2108 ctype_num = SSL3_CT_NUMBER;
2109 }
2110 for (i = 0; i < ctype_num; i++)
ac112332
MC
2111 s->s3->tmp.ctype[i] = data[i];
2112
0f113f3e 2113 if (SSL_USE_SIGALGS(s)) {
ac112332
MC
2114 if (!PACKET_get_net_2(&pkt, &list_len)
2115 || !PACKET_get_bytes(&pkt, &data, list_len)) {
0f113f3e 2116 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
ac112332 2117 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2118 goto err;
2119 }
ac112332 2120
0f113f3e
MC
2121 /* Clear certificate digests and validity flags */
2122 for (i = 0; i < SSL_PKEY_NUM; i++) {
d376e57d 2123 s->s3->tmp.md[i] = NULL;
6383d316 2124 s->s3->tmp.valid_flags[i] = 0;
0f113f3e 2125 }
ac112332 2126 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
0f113f3e
MC
2127 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2128 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2129 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2130 goto err;
2131 }
2132 if (!tls1_process_sigalgs(s)) {
2133 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2134 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2135 goto err;
2136 }
0f113f3e
MC
2137 }
2138
2139 /* get the CA RDNs */
ac112332
MC
2140 if (!PACKET_get_net_2(&pkt, &list_len)
2141 || PACKET_remaining(&pkt) != list_len) {
0f113f3e
MC
2142 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2143 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2144 goto err;
2145 }
2146
ac112332
MC
2147 while (PACKET_remaining(&pkt)) {
2148 if (!PACKET_get_net_2(&pkt, &name_len)
2149 || !PACKET_get_bytes(&pkt, &namebytes, name_len)) {
0f113f3e 2150 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
ac112332 2151 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2152 goto err;
2153 }
2154
ac112332 2155 namestart = namebytes;
0f113f3e 2156
ac112332
MC
2157 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2158 name_len)) == NULL) {
3c33c6f6
MC
2159 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2160 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2161 goto err;
0f113f3e
MC
2162 }
2163
ac112332 2164 if (namebytes != (namestart + name_len)) {
0f113f3e
MC
2165 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2166 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2167 SSL_R_CA_DN_LENGTH_MISMATCH);
2168 goto err;
2169 }
2170 if (!sk_X509_NAME_push(ca_sk, xn)) {
2171 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2172 goto err;
2173 }
0f113f3e
MC
2174 }
2175
0f113f3e
MC
2176 /* we should setup a certificate to return.... */
2177 s->s3->tmp.cert_req = 1;
2178 s->s3->tmp.ctype_num = ctype_num;
222561fe 2179 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
0f113f3e
MC
2180 s->s3->tmp.ca_names = ca_sk;
2181 ca_sk = NULL;
2182
2183 ret = 1;
cc273a93 2184 goto done;
0f113f3e 2185 err:
cc273a93
MC
2186 s->state = SSL_ST_ERR;
2187 done:
222561fe 2188 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
0f113f3e
MC
2189 return (ret);
2190}
2191
2192static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
dfeab068 2193{
0f113f3e 2194 return (X509_NAME_cmp(*a, *b));
dfeab068 2195}
dfeab068 2196
6434abbf 2197int ssl3_get_new_session_ticket(SSL *s)
0f113f3e 2198{
561e12bb
MC
2199 int ok, al, ret = 0;
2200 unsigned int ticklen;
0f113f3e 2201 long n;
561e12bb 2202 PACKET pkt;
0f113f3e
MC
2203
2204 n = s->method->ssl_get_message(s,
2205 SSL3_ST_CR_SESSION_TICKET_A,
2206 SSL3_ST_CR_SESSION_TICKET_B,
2207 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2208
2209 if (!ok)
2210 return ((int)n);
2211
561e12bb
MC
2212 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2213 al = SSL_AD_INTERNAL_ERROR;
2214 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
2215 goto f_err;
2216 }
2217
98ece4ee
MC
2218 if (s->session->session_id_length > 0) {
2219 int i = s->session_ctx->session_cache_mode;
2220 SSL_SESSION *new_sess;
2221 /*
2222 * We reused an existing session, so we need to replace it with a new
2223 * one
2224 */
2225 if (i & SSL_SESS_CACHE_CLIENT) {
2226 /*
2227 * Remove the old session from the cache
2228 */
2229 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2230 if (s->session_ctx->remove_session_cb != NULL)
2231 s->session_ctx->remove_session_cb(s->session_ctx,
2232 s->session);
2233 } else {
2234 /* We carry on if this fails */
2235 SSL_CTX_remove_session(s->session_ctx, s->session);
2236 }
2237 }
2238
2239 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2240 al = SSL_AD_INTERNAL_ERROR;
2241 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2242 goto f_err;
2243 }
2244
2245 SSL_SESSION_free(s->session);
2246 s->session = new_sess;
2247 }
2248
561e12bb
MC
2249 if (!PACKET_get_net_4(&pkt, &s->session->tlsext_tick_lifetime_hint)
2250 || !PACKET_get_net_2(&pkt, &ticklen)
2251 || PACKET_remaining(&pkt) != ticklen) {
0f113f3e
MC
2252 al = SSL_AD_DECODE_ERROR;
2253 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2254 goto f_err;
2255 }
b548a1f1
RS
2256 OPENSSL_free(s->session->tlsext_tick);
2257 s->session->tlsext_ticklen = 0;
0f113f3e
MC
2258 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2259 if (!s->session->tlsext_tick) {
2260 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2261 goto err;
2262 }
561e12bb
MC
2263 if (!PACKET_copy_bytes(&pkt, s->session->tlsext_tick, ticklen)) {
2264 al = SSL_AD_DECODE_ERROR;
2265 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2266 goto f_err;
2267 }
0f113f3e
MC
2268 s->session->tlsext_ticklen = ticklen;
2269 /*
2270 * There are two ways to detect a resumed ticket session. One is to set
2271 * an appropriate session ID and then the server must return a match in
2272 * ServerHello. This allows the normal client session ID matching to work
2273 * and we know much earlier that the ticket has been accepted. The
2274 * other way is to set zero length session ID when the ticket is
2275 * presented and rely on the handshake to determine session resumption.
2276 * We choose the former approach because this fits in with assumptions
2277 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2278 * SHA256 is disabled) hash of the ticket.
2279 */
561e12bb 2280 EVP_Digest(s->session->tlsext_tick, ticklen,
0f113f3e 2281 s->session->session_id, &s->session->session_id_length,
0f113f3e 2282 EVP_sha256(), NULL);
0f113f3e
MC
2283 ret = 1;
2284 return (ret);
2285 f_err:
2286 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2287 err:
cc273a93 2288 s->state = SSL_ST_ERR;
0f113f3e
MC
2289 return (-1);
2290}
67c8e7f4
DSH
2291
2292int ssl3_get_cert_status(SSL *s)
0f113f3e
MC
2293{
2294 int ok, al;
2295 unsigned long resplen, n;
ac63710a
MC
2296 unsigned int type;
2297 PACKET pkt;
0f113f3e
MC
2298
2299 n = s->method->ssl_get_message(s,
2300 SSL3_ST_CR_CERT_STATUS_A,
2301 SSL3_ST_CR_CERT_STATUS_B,
2302 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2303
2304 if (!ok)
2305 return ((int)n);
ac63710a
MC
2306
2307 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
2308 al = SSL_AD_INTERNAL_ERROR;
2309 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
2310 goto f_err;
2311 }
ac63710a
MC
2312 if (!PACKET_get_1(&pkt, &type)
2313 || type != TLSEXT_STATUSTYPE_ocsp) {
0f113f3e
MC
2314 al = SSL_AD_DECODE_ERROR;
2315 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2316 goto f_err;
2317 }
ac63710a
MC
2318 if (!PACKET_get_net_3(&pkt, &resplen)
2319 || PACKET_remaining(&pkt) != resplen) {
0f113f3e
MC
2320 al = SSL_AD_DECODE_ERROR;
2321 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2322 goto f_err;
2323 }
b548a1f1 2324 OPENSSL_free(s->tlsext_ocsp_resp);
ac63710a 2325 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
0f113f3e
MC
2326 if (!s->tlsext_ocsp_resp) {
2327 al = SSL_AD_INTERNAL_ERROR;
2328 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2329 goto f_err;
2330 }
ac63710a
MC
2331 if (!PACKET_copy_bytes(&pkt, s->tlsext_ocsp_resp, resplen)) {
2332 al = SSL_AD_DECODE_ERROR;
2333 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2334 goto f_err;
2335 }
0f113f3e
MC
2336 s->tlsext_ocsp_resplen = resplen;
2337 if (s->ctx->tlsext_status_cb) {
2338 int ret;
2339 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2340 if (ret == 0) {
2341 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2342 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2343 goto f_err;
2344 }
2345 if (ret < 0) {
2346 al = SSL_AD_INTERNAL_ERROR;
2347 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2348 goto f_err;
2349 }
2350 }
2351 return 1;
2352 f_err:
2353 ssl3_send_alert(s, SSL3_AL_FATAL, al);
cc273a93 2354 s->state = SSL_ST_ERR;
0f113f3e
MC
2355 return (-1);
2356}
d02b48c6 2357
36d16f8e 2358int ssl3_get_server_done(SSL *s)
0f113f3e
MC
2359{
2360 int ok, ret = 0;
2361 long n;
2362
2363 /* Second to last param should be very small, like 0 :-) */
2364 n = s->method->ssl_get_message(s,
2365 SSL3_ST_CR_SRVR_DONE_A,
2366 SSL3_ST_CR_SRVR_DONE_B,
2367 SSL3_MT_SERVER_DONE, 30, &ok);
2368
2369 if (!ok)
2370 return ((int)n);
2371 if (n > 0) {
2372 /* should contain no data */
2373 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2374 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
cc273a93 2375 s->state = SSL_ST_ERR;
0f113f3e
MC
2376 return -1;
2377 }
2378 ret = 1;
2379 return (ret);
2380}
176f31dd 2381
36d16f8e 2382int ssl3_send_client_key_exchange(SSL *s)
0f113f3e
MC
2383{
2384 unsigned char *p;
2385 int n;
7689082b
DSH
2386#ifndef OPENSSL_NO_PSK
2387 size_t pskhdrlen = 0;
2388#endif
0f113f3e 2389 unsigned long alg_k;
bc36ee62 2390#ifndef OPENSSL_NO_RSA
0f113f3e
MC
2391 unsigned char *q;
2392 EVP_PKEY *pkey = NULL;
79df9d62 2393#endif
10bf4fc2 2394#ifndef OPENSSL_NO_EC
0f113f3e
MC
2395 EC_KEY *clnt_ecdh = NULL;
2396 const EC_POINT *srvr_ecpoint = NULL;
2397 EVP_PKEY *srvr_pub_pkey = NULL;
2398 unsigned char *encodedPoint = NULL;
2399 int encoded_pt_len = 0;
2400 BN_CTX *bn_ctx = NULL;
ea262260 2401#endif
c660ec63
DSH
2402 unsigned char *pms = NULL;
2403 size_t pmslen = 0;
7689082b 2404 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
d02b48c6 2405
0f113f3e
MC
2406 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2407 p = ssl_handshake_start(s);
d02b48c6 2408
7689082b
DSH
2409
2410#ifndef OPENSSL_NO_PSK
2411 if (alg_k & SSL_PSK) {
2412 int psk_err = 1;
2413 /*
2414 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2415 * \0-terminated identity. The last byte is for us for simulating
2416 * strnlen.
2417 */
2418 char identity[PSK_MAX_IDENTITY_LEN + 1];
2419 size_t identitylen;
2420 unsigned char psk[PSK_MAX_PSK_LEN];
2421 size_t psklen;
2422
2423 if (s->psk_client_callback == NULL) {
2424 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2425 SSL_R_PSK_NO_CLIENT_CB);
2426 goto err;
2427 }
2428
2429 memset(identity, 0, sizeof(identity));
2430
2431 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2432 identity, sizeof(identity) - 1,
2433 psk, sizeof(psk));
2434
2435 if (psklen > PSK_MAX_PSK_LEN) {
2436 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2437 ERR_R_INTERNAL_ERROR);
2438 goto psk_err;
2439 } else if (psklen == 0) {
2440 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2441 SSL_R_PSK_IDENTITY_NOT_FOUND);
2442 goto psk_err;
2443 }
2444
2445 OPENSSL_free(s->s3->tmp.psk);
2446 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2447 OPENSSL_cleanse(psk, psklen);
2448
3df16cc2
DSH
2449 if (s->s3->tmp.psk == NULL) {
2450 OPENSSL_cleanse(identity, sizeof(identity));
7689082b 2451 goto memerr;
3df16cc2 2452 }
7689082b
DSH
2453
2454 s->s3->tmp.psklen = psklen;
2455
2456 identitylen = strlen(identity);
2457 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2458 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2459 ERR_R_INTERNAL_ERROR);
2460 goto psk_err;
2461 }
2462 OPENSSL_free(s->session->psk_identity);
2463 s->session->psk_identity = BUF_strdup(identity);
3df16cc2
DSH
2464 if (s->session->psk_identity == NULL) {
2465 OPENSSL_cleanse(identity, sizeof(identity));
7689082b 2466 goto memerr;
3df16cc2 2467 }
7689082b
DSH
2468
2469 s2n(identitylen, p);
2470 memcpy(p, identity, identitylen);
2471 pskhdrlen = 2 + identitylen;
2472 p += identitylen;
2473 psk_err = 0;
2474 psk_err:
2475 OPENSSL_cleanse(identity, sizeof(identity));
2476 if (psk_err != 0) {
2477 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2478 goto err;
2479 }
2480 }
2481 if (alg_k & SSL_kPSK) {
2482 n = 0;
2483 } else
2484#endif
d02b48c6 2485
0f113f3e
MC
2486 /* Fool emacs indentation */
2487 if (0) {
2488 }
bc36ee62 2489#ifndef OPENSSL_NO_RSA
7689082b 2490 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
0f113f3e 2491 RSA *rsa;
c660ec63
DSH
2492 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2493 pms = OPENSSL_malloc(pmslen);
2494 if (!pms)
2495 goto memerr;
0f113f3e 2496
389ebcec 2497 if (s->session->peer == NULL) {
0f113f3e
MC
2498 /*
2499 * We should always have a server certificate with SSL_kRSA.
2500 */
2501 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2502 ERR_R_INTERNAL_ERROR);
2503 goto err;
2504 }
2505
8d92c1f8
DSH
2506 if (s->s3->peer_rsa_tmp != NULL)
2507 rsa = s->s3->peer_rsa_tmp;
0f113f3e 2508 else {
a273c6ee 2509 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
2510 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2511 || (pkey->pkey.rsa == NULL)) {
2512 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2513 ERR_R_INTERNAL_ERROR);
2514 goto err;
2515 }
2516 rsa = pkey->pkey.rsa;
2517 EVP_PKEY_free(pkey);
2518 }
2519
c660ec63
DSH
2520 pms[0] = s->client_version >> 8;
2521 pms[1] = s->client_version & 0xff;
2522 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
0f113f3e
MC
2523 goto err;
2524
0f113f3e
MC
2525 q = p;
2526 /* Fix buf for TLS and beyond */
2527 if (s->version > SSL3_VERSION)
2528 p += 2;
c660ec63 2529 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
0f113f3e
MC
2530# ifdef PKCS1_CHECK
2531 if (s->options & SSL_OP_PKCS1_CHECK_1)
2532 p[1]++;
2533 if (s->options & SSL_OP_PKCS1_CHECK_2)
2534 tmp_buf[0] = 0x70;
2535# endif
2536 if (n <= 0) {
2537 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2538 SSL_R_BAD_RSA_ENCRYPT);
2539 goto err;
2540 }
2541
2542 /* Fix buf for TLS and beyond */
2543 if (s->version > SSL3_VERSION) {
2544 s2n(n, q);
2545 n += 2;
2546 }
0f113f3e 2547 }
f9b3bff6 2548#endif
bc36ee62 2549#ifndef OPENSSL_NO_DH
7689082b 2550 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
0f113f3e 2551 DH *dh_srvr, *dh_clnt;
8d92c1f8
DSH
2552 if (s->s3->peer_dh_tmp != NULL)
2553 dh_srvr = s->s3->peer_dh_tmp;
0f113f3e
MC
2554 else {
2555 /* we get them from the cert */
0f113f3e
MC
2556 EVP_PKEY *spkey = NULL;
2557 dh_srvr = NULL;
a273c6ee 2558 spkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
2559 if (spkey) {
2560 dh_srvr = EVP_PKEY_get1_DH(spkey);
2561 EVP_PKEY_free(spkey);
2562 }
2563 if (dh_srvr == NULL) {
2564 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2565 ERR_R_INTERNAL_ERROR);
2566 goto err;
2567 }
2568 }
2569 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2570 /* Use client certificate key */
2571 EVP_PKEY *clkey = s->cert->key->privatekey;
2572 dh_clnt = NULL;
2573 if (clkey)
2574 dh_clnt = EVP_PKEY_get1_DH(clkey);
2575 if (dh_clnt == NULL) {
2576 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2577 ERR_R_INTERNAL_ERROR);
2578 goto err;
2579 }
2580 } else {
2581 /* generate a new random key */
2582 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2583 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2584 goto err;
2585 }
2586 if (!DH_generate_key(dh_clnt)) {
2587 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2588 DH_free(dh_clnt);
2589 goto err;
2590 }
2591 }
2592
c660ec63
DSH
2593 pmslen = DH_size(dh_clnt);
2594 pms = OPENSSL_malloc(pmslen);
2595 if (!pms)
2596 goto memerr;
2597
0f113f3e
MC
2598 /*
2599 * use the 'p' output buffer for the DH key, but make sure to
2600 * clear it out afterwards
2601 */
2602
c660ec63 2603 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
8d92c1f8 2604 if (s->s3->peer_dh_tmp == NULL)
0f113f3e
MC
2605 DH_free(dh_srvr);
2606
2607 if (n <= 0) {
2608 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2609 DH_free(dh_clnt);
2610 goto err;
2611 }
6b937f8b 2612 pmslen = n;
0f113f3e 2613
0f113f3e
MC
2614 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2615 n = 0;
2616 else {
2617 /* send off the data */
2618 n = BN_num_bytes(dh_clnt->pub_key);
2619 s2n(n, p);
2620 BN_bn2bin(dh_clnt->pub_key, p);
2621 n += 2;
2622 }
2623
2624 DH_free(dh_clnt);
0f113f3e 2625 }
d02b48c6 2626#endif
ea262260 2627
10bf4fc2 2628#ifndef OPENSSL_NO_EC
7689082b 2629 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
0f113f3e
MC
2630 const EC_GROUP *srvr_group = NULL;
2631 EC_KEY *tkey;
2632 int ecdh_clnt_cert = 0;
2633 int field_size = 0;
0f113f3e
MC
2634 /*
2635 * Did we send out the client's ECDH share for use in premaster
2636 * computation as part of client certificate? If so, set
2637 * ecdh_clnt_cert to 1.
2638 */
2639 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
50e735f9
MC
2640 /*-
2641 * XXX: For now, we do not support client
2642 * authentication using ECDH certificates.
2643 * To add such support, one needs to add
2644 * code that checks for appropriate
2645 * conditions and sets ecdh_clnt_cert to 1.
2646 * For example, the cert have an ECC
2647 * key on the same curve as the server's
2648 * and the key should be authorized for
2649 * key agreement.
2650 *
2651 * One also needs to add code in ssl3_connect
2652 * to skip sending the certificate verify
2653 * message.
2654 *
2655 * if ((s->cert->key->privatekey != NULL) &&
2656 * (s->cert->key->privatekey->type ==
2657 * EVP_PKEY_EC) && ...)
2658 * ecdh_clnt_cert = 1;
2659 */
0f113f3e
MC
2660 }
2661
8d92c1f8
DSH
2662 if (s->s3->peer_ecdh_tmp != NULL) {
2663 tkey = s->s3->peer_ecdh_tmp;
0f113f3e
MC
2664 } else {
2665 /* Get the Server Public Key from Cert */
a273c6ee 2666 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
2667 if ((srvr_pub_pkey == NULL)
2668 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2669 || (srvr_pub_pkey->pkey.ec == NULL)) {
2670 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2671 ERR_R_INTERNAL_ERROR);
2672 goto err;
2673 }
2674
2675 tkey = srvr_pub_pkey->pkey.ec;
2676 }
2677
2678 srvr_group = EC_KEY_get0_group(tkey);
2679 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2680
2681 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2682 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2683 ERR_R_INTERNAL_ERROR);
2684 goto err;
2685 }
2686
2687 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2688 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2689 ERR_R_MALLOC_FAILURE);
2690 goto err;
2691 }
2692
2693 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2694 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2695 goto err;
2696 }
2697 if (ecdh_clnt_cert) {
2698 /*
2699 * Reuse key info from our certificate We only need our
2700 * private key to perform the ECDH computation.
2701 */
2702 const BIGNUM *priv_key;
2703 tkey = s->cert->key->privatekey->pkey.ec;
2704 priv_key = EC_KEY_get0_private_key(tkey);
2705 if (priv_key == NULL) {
2706 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2707 ERR_R_MALLOC_FAILURE);
2708 goto err;
2709 }
2710 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2711 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2712 goto err;
2713 }
2714 } else {
2715 /* Generate a new ECDH key pair */
2716 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2717 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2718 ERR_R_ECDH_LIB);
2719 goto err;
2720 }
2721 }
2722
2723 /*
2724 * use the 'p' output buffer for the ECDH key, but make sure to
2725 * clear it out afterwards
2726 */
2727
2728 field_size = EC_GROUP_get_degree(srvr_group);
2729 if (field_size <= 0) {
2730 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2731 goto err;
2732 }
c660ec63
DSH
2733 pmslen = (field_size + 7) / 8;
2734 pms = OPENSSL_malloc(pmslen);
2735 if (!pms)
2736 goto memerr;
2737 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2738 if (n <= 0 || pmslen != (size_t)n) {
0f113f3e
MC
2739 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2740 goto err;
2741 }
2742
0f113f3e
MC
2743 if (ecdh_clnt_cert) {
2744 /* Send empty client key exch message */
2745 n = 0;
2746 } else {
2747 /*
2748 * First check the size of encoding and allocate memory
2749 * accordingly.
2750 */
2751 encoded_pt_len =
2752 EC_POINT_point2oct(srvr_group,
2753 EC_KEY_get0_public_key(clnt_ecdh),
2754 POINT_CONVERSION_UNCOMPRESSED,
2755 NULL, 0, NULL);
2756
2757 encodedPoint = (unsigned char *)
2758 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2759 bn_ctx = BN_CTX_new();
2760 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2761 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2762 ERR_R_MALLOC_FAILURE);
2763 goto err;
2764 }
2765
2766 /* Encode the public key */
2767 n = EC_POINT_point2oct(srvr_group,
2768 EC_KEY_get0_public_key(clnt_ecdh),
2769 POINT_CONVERSION_UNCOMPRESSED,
2770 encodedPoint, encoded_pt_len, bn_ctx);
2771
2772 *p = n; /* length of encoded point */
2773 /* Encoded point will be copied here */
2774 p += 1;
2775 /* copy the point */
16f8d4eb 2776 memcpy(p, encodedPoint, n);
0f113f3e
MC
2777 /* increment n to account for length field */
2778 n += 1;
2779 }
2780
2781 /* Free allocated memory */
2782 BN_CTX_free(bn_ctx);
b548a1f1 2783 OPENSSL_free(encodedPoint);
8fdc3734 2784 EC_KEY_free(clnt_ecdh);
0f113f3e
MC
2785 EVP_PKEY_free(srvr_pub_pkey);
2786 }
10bf4fc2 2787#endif /* !OPENSSL_NO_EC */
0f113f3e
MC
2788 else if (alg_k & SSL_kGOST) {
2789 /* GOST key exchange message creation */
2790 EVP_PKEY_CTX *pkey_ctx;
2791 X509 *peer_cert;
2792 size_t msglen;
2793 unsigned int md_len;
c660ec63 2794 unsigned char shared_ukm[32], tmp[256];
0f113f3e
MC
2795 EVP_MD_CTX *ukm_hash;
2796 EVP_PKEY *pub_key;
2797
c660ec63
DSH
2798 pmslen = 32;
2799 pms = OPENSSL_malloc(pmslen);
2800 if (!pms)
2801 goto memerr;
2802
0f113f3e
MC
2803 /*
2804 * Get server sertificate PKEY and create ctx from it
2805 */
a273c6ee 2806 peer_cert = s->session->peer;
0f113f3e
MC
2807 if (!peer_cert) {
2808 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2809 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2810 goto err;
2811 }
2812
2813 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2814 X509_get_pubkey(peer_cert), NULL);
2815 /*
2816 * If we have send a certificate, and certificate key
2817 *
2818 * * parameters match those of server certificate, use
2819 * certificate key for key exchange
2820 */
2821
2822 /* Otherwise, generate ephemeral key pair */
2823
2824 EVP_PKEY_encrypt_init(pkey_ctx);
2825 /* Generate session key */
266483d2
MC
2826 if (RAND_bytes(pms, pmslen) <= 0) {
2827 EVP_PKEY_CTX_free(pkey_ctx);
2828 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2829 ERR_R_INTERNAL_ERROR);
2830 goto err;
2831 };
0f113f3e
MC
2832 /*
2833 * If we have client certificate, use its secret as peer key
2834 */
2835 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2836 if (EVP_PKEY_derive_set_peer
2837 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2838 /*
2839 * If there was an error - just ignore it. Ephemeral key
2840 * * would be used
2841 */
2842 ERR_clear_error();
2843 }
2844 }
2845 /*
2846 * Compute shared IV and store it in algorithm-specific context
2847 * data
2848 */
2849 ukm_hash = EVP_MD_CTX_create();
2850 EVP_DigestInit(ukm_hash,
2851 EVP_get_digestbynid(NID_id_GostR3411_94));
2852 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2853 SSL3_RANDOM_SIZE);
2854 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2855 SSL3_RANDOM_SIZE);
2856 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2857 EVP_MD_CTX_destroy(ukm_hash);
2858 if (EVP_PKEY_CTX_ctrl
2859 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2860 shared_ukm) < 0) {
2861 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2862 SSL_R_LIBRARY_BUG);
2863 goto err;
2864 }
2865 /* Make GOST keytransport blob message */
2866 /*
2867 * Encapsulate it into sequence
2868 */
2869 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2870 msglen = 255;
c660ec63 2871 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
0f113f3e
MC
2872 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2873 SSL_R_LIBRARY_BUG);
2874 goto err;
2875 }
2876 if (msglen >= 0x80) {
2877 *(p++) = 0x81;
2878 *(p++) = msglen & 0xff;
2879 n = msglen + 3;
2880 } else {
2881 *(p++) = msglen & 0xff;
2882 n = msglen + 2;
2883 }
2884 memcpy(p, tmp, msglen);
2885 /* Check if pubkey from client certificate was used */
2886 if (EVP_PKEY_CTX_ctrl
2887 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2888 /* Set flag "skip certificate verify" */
2889 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2890 }
2891 EVP_PKEY_CTX_free(pkey_ctx);
0f113f3e
MC
2892 EVP_PKEY_free(pub_key);
2893
2894 }
edc032b5 2895#ifndef OPENSSL_NO_SRP
0f113f3e
MC
2896 else if (alg_k & SSL_kSRP) {
2897 if (s->srp_ctx.A != NULL) {
2898 /* send off the data */
2899 n = BN_num_bytes(s->srp_ctx.A);
2900 s2n(n, p);
2901 BN_bn2bin(s->srp_ctx.A, p);
2902 n += 2;
2903 } else {
2904 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2905 ERR_R_INTERNAL_ERROR);
2906 goto err;
2907 }
b548a1f1 2908 OPENSSL_free(s->session->srp_username);
0f113f3e
MC
2909 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2910 if (s->session->srp_username == NULL) {
2911 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2912 ERR_R_MALLOC_FAILURE);
2913 goto err;
2914 }
0f113f3e 2915 }
0f113f3e
MC
2916#endif
2917 else {
2918 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2919 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2920 goto err;
2921 }
2922
7689082b
DSH
2923#ifndef OPENSSL_NO_PSK
2924 n += pskhdrlen;
2925#endif
2926
61986d32 2927 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
77d514c5
MC
2928 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2929 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2930 goto err;
2931 }
2932
0f113f3e
MC
2933 s->state = SSL3_ST_CW_KEY_EXCH_B;
2934 }
2935
2936 /* SSL3_ST_CW_KEY_EXCH_B */
c660ec63
DSH
2937 n = ssl_do_write(s);
2938#ifndef OPENSSL_NO_SRP
2939 /* Check for SRP */
7689082b 2940 if (alg_k & SSL_kSRP) {
c660ec63
DSH
2941 /*
2942 * If everything written generate master key: no need to save PMS as
57b272b0 2943 * srp_generate_client_master_secret generates it internally.
c660ec63
DSH
2944 */
2945 if (n > 0) {
57b272b0 2946 if (!srp_generate_client_master_secret(s)) {
c660ec63
DSH
2947 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2948 ERR_R_INTERNAL_ERROR);
2949 goto err;
2950 }
2951 }
2952 } else
2953#endif
2954 /* If we haven't written everything save PMS */
2955 if (n <= 0) {
76106e60
DSH
2956 s->s3->tmp.pms = pms;
2957 s->s3->tmp.pmslen = pmslen;
c660ec63
DSH
2958 } else {
2959 /* If we don't have a PMS restore */
2960 if (pms == NULL) {
76106e60
DSH
2961 pms = s->s3->tmp.pms;
2962 pmslen = s->s3->tmp.pmslen;
c660ec63 2963 }
7689082b 2964 if (pms == NULL && !(alg_k & SSL_kPSK)) {
c660ec63
DSH
2965 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2966 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2967 goto err;
2968 }
57b272b0 2969 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
69f68237
MC
2970 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2971 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2972 goto err;
2973 }
c660ec63
DSH
2974 }
2975 return n;
2976 memerr:
2977 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2978 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e 2979 err:
4b45c6e5 2980 OPENSSL_clear_free(pms, pmslen);
76106e60 2981 s->s3->tmp.pms = NULL;
10bf4fc2 2982#ifndef OPENSSL_NO_EC
0f113f3e 2983 BN_CTX_free(bn_ctx);
b548a1f1 2984 OPENSSL_free(encodedPoint);
8fdc3734 2985 EC_KEY_free(clnt_ecdh);
0f113f3e 2986 EVP_PKEY_free(srvr_pub_pkey);
7689082b
DSH
2987#endif
2988#ifndef OPENSSL_NO_PSK
2989 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2990 s->s3->tmp.psk = NULL;
0f113f3e 2991#endif
cc273a93 2992 s->state = SSL_ST_ERR;
0f113f3e
MC
2993 return (-1);
2994}
d02b48c6 2995
36d16f8e 2996int ssl3_send_client_verify(SSL *s)
0f113f3e
MC
2997{
2998 unsigned char *p;
2999 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3000 EVP_PKEY *pkey;
3001 EVP_PKEY_CTX *pctx = NULL;
3002 EVP_MD_CTX mctx;
3003 unsigned u = 0;
3004 unsigned long n;
3005 int j;
3006
3007 EVP_MD_CTX_init(&mctx);
3008
3009 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3010 p = ssl_handshake_start(s);
3011 pkey = s->cert->key->privatekey;
0e1dba93 3012/* Create context from key and test if sha1 is allowed as digest */
0f113f3e
MC
3013 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3014 EVP_PKEY_sign_init(pctx);
3015 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3016 if (!SSL_USE_SIGALGS(s))
3017 s->method->ssl3_enc->cert_verify_mac(s,
3018 NID_sha1,
3019 &(data
3020 [MD5_DIGEST_LENGTH]));
3021 } else {
3022 ERR_clear_error();
3023 }
3024 /*
3025 * For TLS v1.2 send signature algorithm and signature using agreed
3026 * digest and cached handshake records.
3027 */
3028 if (SSL_USE_SIGALGS(s)) {
3029 long hdatalen = 0;
3030 void *hdata;
d376e57d 3031 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
0f113f3e
MC
3032 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3033 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3034 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3035 goto err;
3036 }
3037 p += 2;
855a54a9 3038#ifdef SSL_DEBUG
0f113f3e
MC
3039 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3040 EVP_MD_name(md));
3041#endif
3042 if (!EVP_SignInit_ex(&mctx, md, NULL)
3043 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3044 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3045 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3046 goto err;
3047 }
3048 s2n(u, p);
3049 n = u + 4;
124037fd
DSH
3050 /* Digest cached records and discard handshake buffer */
3051 if (!ssl3_digest_cached_records(s, 0))
0f113f3e
MC
3052 goto err;
3053 } else
bc36ee62 3054#ifndef OPENSSL_NO_RSA
0f113f3e
MC
3055 if (pkey->type == EVP_PKEY_RSA) {
3056 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3057 if (RSA_sign(NID_md5_sha1, data,
3058 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3059 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3060 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3061 goto err;
3062 }
3063 s2n(u, p);
3064 n = u + 2;
3065 } else
d02b48c6 3066#endif
bc36ee62 3067#ifndef OPENSSL_NO_DSA
0f113f3e
MC
3068 if (pkey->type == EVP_PKEY_DSA) {
3069 if (!DSA_sign(pkey->save_type,
3070 &(data[MD5_DIGEST_LENGTH]),
3071 SHA_DIGEST_LENGTH, &(p[2]),
3072 (unsigned int *)&j, pkey->pkey.dsa)) {
3073 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3074 goto err;
3075 }
3076 s2n(j, p);
3077 n = j + 2;
3078 } else
ea262260 3079#endif
10bf4fc2 3080#ifndef OPENSSL_NO_EC
0f113f3e
MC
3081 if (pkey->type == EVP_PKEY_EC) {
3082 if (!ECDSA_sign(pkey->save_type,
3083 &(data[MD5_DIGEST_LENGTH]),
3084 SHA_DIGEST_LENGTH, &(p[2]),
3085 (unsigned int *)&j, pkey->pkey.ec)) {
3086 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3087 goto err;
3088 }
3089 s2n(j, p);
3090 n = j + 2;
3091 } else
3092#endif
ade44dcb 3093 if (pkey->type == NID_id_GostR3410_2001) {
0f113f3e
MC
3094 unsigned char signbuf[64];
3095 int i;
3096 size_t sigsize = 64;
3097 s->method->ssl3_enc->cert_verify_mac(s,
3098 NID_id_GostR3411_94, data);
3099 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3100 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3101 goto err;
3102 }
3103 for (i = 63, j = 0; i >= 0; j++, i--) {
3104 p[2 + j] = signbuf[i];
3105 }
3106 s2n(j, p);
3107 n = j + 2;
3108 } else {
3109 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3110 goto err;
3111 }
61986d32 3112 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
77d514c5
MC
3113 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3114 goto err;
3115 }
0f113f3e
MC
3116 s->state = SSL3_ST_CW_CERT_VRFY_B;
3117 }
3118 EVP_MD_CTX_cleanup(&mctx);
3119 EVP_PKEY_CTX_free(pctx);
3120 return ssl_do_write(s);
3121 err:
3122 EVP_MD_CTX_cleanup(&mctx);
3123 EVP_PKEY_CTX_free(pctx);
cc273a93 3124 s->state = SSL_ST_ERR;
0f113f3e
MC
3125 return (-1);
3126}
3127
3128/*
3129 * Check a certificate can be used for client authentication. Currently check
3130 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3131 * certificates can be used and optionally checks suitability for Suite B.
0d609395
DSH
3132 */
3133static int ssl3_check_client_certificate(SSL *s)
0f113f3e
MC
3134{
3135 unsigned long alg_k;
3136 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3137 return 0;
3138 /* If no suitable signature algorithm can't use certificate */
d376e57d 3139 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
0f113f3e
MC
3140 return 0;
3141 /*
3142 * If strict mode check suitability of chain before using it. This also
3143 * adjusts suite B digest if necessary.
3144 */
3145 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3146 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3147 return 0;
3148 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3149 /* See if we can use client certificate for fixed DH */
3150 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
a273c6ee 3151 int i = s->session->peer_type;
0f113f3e
MC
3152 EVP_PKEY *clkey = NULL, *spkey = NULL;
3153 clkey = s->cert->key->privatekey;
3154 /* If client key not DH assume it can be used */
3155 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3156 return 1;
3157 if (i >= 0)
a273c6ee 3158 spkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
3159 if (spkey) {
3160 /* Compare server and client parameters */
3161 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3162 EVP_PKEY_free(spkey);
3163 if (i != 1)
3164 return 0;
3165 }
3166 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3167 }
3168 return 1;
3169}
0d609395 3170
36d16f8e 3171int ssl3_send_client_certificate(SSL *s)
0f113f3e
MC
3172{
3173 X509 *x509 = NULL;
3174 EVP_PKEY *pkey = NULL;
3175 int i;
3176
3177 if (s->state == SSL3_ST_CW_CERT_A) {
3178 /* Let cert callback update client certificates if required */
3179 if (s->cert->cert_cb) {
3180 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3181 if (i < 0) {
3182 s->rwstate = SSL_X509_LOOKUP;
3183 return -1;
3184 }
3185 if (i == 0) {
3186 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
cc273a93 3187 s->state = SSL_ST_ERR;
0f113f3e
MC
3188 return 0;
3189 }
3190 s->rwstate = SSL_NOTHING;
3191 }
3192 if (ssl3_check_client_certificate(s))
3193 s->state = SSL3_ST_CW_CERT_C;
3194 else
3195 s->state = SSL3_ST_CW_CERT_B;
3196 }
3197
3198 /* We need to get a client cert */
3199 if (s->state == SSL3_ST_CW_CERT_B) {
3200 /*
3201 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3202 * return(-1); We then get retied later
3203 */
3204 i = 0;
3205 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3206 if (i < 0) {
3207 s->rwstate = SSL_X509_LOOKUP;
3208 return (-1);
3209 }
3210 s->rwstate = SSL_NOTHING;
3211 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3212 s->state = SSL3_ST_CW_CERT_B;
3213 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3214 i = 0;
3215 } else if (i == 1) {
3216 i = 0;
3217 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3218 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3219 }
3220
222561fe 3221 X509_free(x509);
25aaa98a 3222 EVP_PKEY_free(pkey);
0f113f3e
MC
3223 if (i && !ssl3_check_client_certificate(s))
3224 i = 0;
3225 if (i == 0) {
3226 if (s->version == SSL3_VERSION) {
3227 s->s3->tmp.cert_req = 0;
3228 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3229 return (1);
3230 } else {
3231 s->s3->tmp.cert_req = 2;
124037fd 3232 if (!ssl3_digest_cached_records(s, 0)) {
dab18ab5
DSH
3233 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3234 s->state = SSL_ST_ERR;
3235 return 0;
3236 }
0f113f3e
MC
3237 }
3238 }
3239
3240 /* Ok, we have a cert */
3241 s->state = SSL3_ST_CW_CERT_C;
3242 }
3243
3244 if (s->state == SSL3_ST_CW_CERT_C) {
3245 s->state = SSL3_ST_CW_CERT_D;
3246 if (!ssl3_output_cert_chain(s,
3247 (s->s3->tmp.cert_req ==
3248 2) ? NULL : s->cert->key)) {
3249 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3250 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
cc273a93 3251 s->state = SSL_ST_ERR;
0f113f3e
MC
3252 return 0;
3253 }
3254 }
3255 /* SSL3_ST_CW_CERT_D */
3256 return ssl_do_write(s);
3257}
3258
3259#define has_bits(i,m) (((i)&(m)) == (m))
d02b48c6 3260
36d16f8e 3261int ssl3_check_cert_and_algorithm(SSL *s)
0f113f3e
MC
3262{
3263 int i, idx;
3264 long alg_k, alg_a;
3265 EVP_PKEY *pkey = NULL;
ac38115c 3266 int pkey_bits;
bc36ee62 3267#ifndef OPENSSL_NO_RSA
0f113f3e 3268 RSA *rsa;
79df9d62 3269#endif
bc36ee62 3270#ifndef OPENSSL_NO_DH
0f113f3e 3271 DH *dh;
79df9d62 3272#endif
26c79d56 3273 int al = SSL_AD_HANDSHAKE_FAILURE;
d02b48c6 3274
0f113f3e
MC
3275 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3276 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 3277
0f113f3e 3278 /* we don't have a certificate */
55a9a16f 3279 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
0f113f3e 3280 return (1);
bc36ee62 3281#ifndef OPENSSL_NO_RSA
8d92c1f8 3282 rsa = s->s3->peer_rsa_tmp;
79df9d62 3283#endif
bc36ee62 3284#ifndef OPENSSL_NO_DH
8d92c1f8 3285 dh = s->s3->peer_dh_tmp;
79df9d62 3286#endif
d02b48c6 3287
0f113f3e 3288 /* This is the passed certificate */
d02b48c6 3289
a273c6ee 3290 idx = s->session->peer_type;
10bf4fc2 3291#ifndef OPENSSL_NO_EC
0f113f3e 3292 if (idx == SSL_PKEY_ECC) {
a273c6ee 3293 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
0f113f3e
MC
3294 /* check failed */
3295 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3296 goto f_err;
3297 } else {
3298 return 1;
3299 }
3300 } else if (alg_a & SSL_aECDSA) {
3301 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3302 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3303 goto f_err;
3304 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3305 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3306 goto f_err;
3307 }
3308#endif
a273c6ee 3309 pkey = X509_get_pubkey(s->session->peer);
ac38115c 3310 pkey_bits = EVP_PKEY_bits(pkey);
a273c6ee 3311 i = X509_certificate_type(s->session->peer, pkey);
0f113f3e
MC
3312 EVP_PKEY_free(pkey);
3313
3314 /* Check that we have a certificate if we require one */
3315 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3316 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3317 SSL_R_MISSING_RSA_SIGNING_CERT);
3318 goto f_err;
3319 }
bc36ee62 3320#ifndef OPENSSL_NO_DSA
0f113f3e
MC
3321 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3322 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3323 SSL_R_MISSING_DSA_SIGNING_CERT);
3324 goto f_err;
3325 }
d02b48c6 3326#endif
bc36ee62 3327#ifndef OPENSSL_NO_RSA
7689082b 3328 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
26c79d56
KR
3329 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3330 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3331 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3332 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3333 goto f_err;
3334 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3335 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3336 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3337 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3338 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3339 goto f_err;
3340 }
3341 if (rsa != NULL) {
3342 /* server key exchange is not allowed. */
3343 al = SSL_AD_INTERNAL_ERROR;
3344 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3345 goto f_err;
3346 }
3347 }
3348 }
0f113f3e 3349 }
79df9d62 3350#endif
bc36ee62 3351#ifndef OPENSSL_NO_DH
26c79d56
KR
3352 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3353 al = SSL_AD_INTERNAL_ERROR;
3354 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
3355 goto f_err;
3356 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3357 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3358 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3359 SSL_R_MISSING_DH_RSA_CERT);
3360 goto f_err;
3361 }
3362# ifndef OPENSSL_NO_DSA
3363 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3364 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3365 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3366 SSL_R_MISSING_DH_DSA_CERT);
3367 goto f_err;
3368 }
3369# endif
d02b48c6
RE
3370#endif
3371
ac38115c
KR
3372 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3373 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
bc36ee62 3374#ifndef OPENSSL_NO_RSA
0f113f3e 3375 if (alg_k & SSL_kRSA) {
26c79d56
KR
3376 if (rsa == NULL) {
3377 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3378 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3379 goto f_err;
3380 } else if (RSA_bits(rsa) >
0f113f3e 3381 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
26c79d56
KR
3382 /* We have a temporary RSA key but it's too large. */
3383 al = SSL_AD_EXPORT_RESTRICTION;
0f113f3e
MC
3384 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3385 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3386 goto f_err;
3387 }
3388 } else
d02b48c6 3389#endif
bc36ee62 3390#ifndef OPENSSL_NO_DH
26c79d56
KR
3391 if (alg_k & SSL_kDHE) {
3392 if (DH_bits(dh) >
0f113f3e 3393 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
26c79d56
KR
3394 /* We have a temporary DH key but it's too large. */
3395 al = SSL_AD_EXPORT_RESTRICTION;
0f113f3e
MC
3396 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3397 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3398 goto f_err;
3399 }
26c79d56
KR
3400 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3401 /* The cert should have had an export DH key. */
3402 al = SSL_AD_EXPORT_RESTRICTION;
3403 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3404 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3405 goto f_err;
0f113f3e
MC
3406 } else
3407#endif
3408 {
3409 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3410 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3411 goto f_err;
3412 }
3413 }
3414 return (1);
3415 f_err:
26c79d56 3416 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0f113f3e
MC
3417 return (0);
3418}
3419
3420/*
6e3d0153
EK
3421 * Normally, we can tell if the server is resuming the session from
3422 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3423 * message after the ServerHello to determine if the server is resuming.
3424 * Therefore, we allow EAP-FAST to peek ahead.
657da85e
MC
3425 * ssl3_check_change returns 1 if we are resuming from an external
3426 * pre-shared secret, we have a "ticket" and the next server message
3427 * is CCS; and 0 otherwise. It returns -1 upon an error.
6434abbf 3428 */
657da85e 3429static int ssl3_check_change(SSL *s)
6e3d0153
EK
3430{
3431 int ok = 0;
3432
3433 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3434 !s->session->tlsext_tick)
3435 return 0;
3436
6e3d0153
EK
3437 /*
3438 * This function is called when we might get a Certificate message instead,
3439 * so permit appropriate message length.
3440 * We ignore the return value as we're only interested in the message type
3441 * and not its length.
3442 */
3443 s->method->ssl_get_message(s,
3444 SSL3_ST_CR_CERT_A,
3445 SSL3_ST_CR_CERT_B,
3446 -1, s->max_cert_list, &ok);
6e3d0153
EK
3447
3448 if (!ok)
3449 return -1;
3450
3451 s->s3->tmp.reuse_message = 1;
3452
657da85e 3453 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC)
6e3d0153
EK
3454 return 1;
3455
6e3d0153
EK
3456 return 0;
3457}
6434abbf 3458
e481f9b9 3459#ifndef OPENSSL_NO_NEXTPROTONEG
ee2ffc27 3460int ssl3_send_next_proto(SSL *s)
0f113f3e
MC
3461{
3462 unsigned int len, padding_len;
3463 unsigned char *d;
3464
3465 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3466 len = s->next_proto_negotiated_len;
3467 padding_len = 32 - ((len + 2) % 32);
3468 d = (unsigned char *)s->init_buf->data;
3469 d[4] = len;
3470 memcpy(d + 5, s->next_proto_negotiated, len);
3471 d[5 + len] = padding_len;
3472 memset(d + 6 + len, 0, padding_len);
3473 *(d++) = SSL3_MT_NEXT_PROTO;
3474 l2n3(2 + len + padding_len, d);
3475 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3476 s->init_num = 4 + 2 + len + padding_len;
3477 s->init_off = 0;
3478 }
3479
3480 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3481}
6434abbf 3482#endif
368888bc
DSH
3483
3484int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
0f113f3e
MC
3485{
3486 int i = 0;
368888bc 3487#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
3488 if (s->ctx->client_cert_engine) {
3489 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3490 SSL_get_client_CA_list(s),
3491 px509, ppkey, NULL, NULL, NULL);
3492 if (i != 0)
3493 return i;
3494 }
3495#endif
3496 if (s->ctx->client_cert_cb)
3497 i = s->ctx->client_cert_cb(s, px509, ppkey);
3498 return i;
3499}
d45ba43d
MC
3500
3501int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3502 unsigned char *p,
3503 int (*put_cb) (const SSL_CIPHER *,
3504 unsigned char *))
3505{
3506 int i, j = 0;
3507 SSL_CIPHER *c;
3508 unsigned char *q;
3509 int empty_reneg_info_scsv = !s->renegotiate;
3510 /* Set disabled masks for this session */
3511 ssl_set_client_disabled(s);
3512
3513 if (sk == NULL)
3514 return (0);
3515 q = p;
3516 if (put_cb == NULL)
3517 put_cb = s->method->put_cipher_by_char;
3518
3519 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3520 c = sk_SSL_CIPHER_value(sk, i);
3521 /* Skip disabled ciphers */
3522 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3523 continue;
3524#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3525 if (c->id == SSL3_CK_SCSV) {
3526 if (!empty_reneg_info_scsv)
3527 continue;
3528 else
3529 empty_reneg_info_scsv = 0;
3530 }
3531#endif
3532 j = put_cb(c, p);
3533 p += j;
3534 }
3535 /*
3536 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3537 * applicable SCSVs.
3538 */
3539 if (p != q) {
3540 if (empty_reneg_info_scsv) {
3541 static SSL_CIPHER scsv = {
3542 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3543 };
3544 j = put_cb(&scsv, p);
3545 p += j;
3546#ifdef OPENSSL_RI_DEBUG
3547 fprintf(stderr,
3548 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3549#endif
3550 }
3551 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3552 static SSL_CIPHER scsv = {
3553 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3554 };
3555 j = put_cb(&scsv, p);
3556 p += j;
3557 }
3558 }
3559
3560 return (p - q);
3561}