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