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