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