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