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