Remove ssl3_check_finished.
[openssl.git] / ssl / d1_clnt.c
1 /* ssl/d1_clnt.c */
2 /* 
3  * DTLS implementation written by Nagendra Modadugu
4  * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.  
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2007 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer. 
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    openssl-core@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60  * All rights reserved.
61  *
62  * This package is an SSL implementation written
63  * by Eric Young (eay@cryptsoft.com).
64  * The implementation was written so as to conform with Netscapes SSL.
65  * 
66  * This library is free for commercial and non-commercial use as long as
67  * the following conditions are aheared to.  The following conditions
68  * apply to all code found in this distribution, be it the RC4, RSA,
69  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
70  * included with this distribution is covered by the same copyright terms
71  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72  * 
73  * Copyright remains Eric Young's, and as such any Copyright notices in
74  * the code are not to be removed.
75  * If this package is used in a product, Eric Young should be given attribution
76  * as the author of the parts of the library used.
77  * This can be in the form of a textual message at program startup or
78  * in documentation (online or textual) provided with the package.
79  * 
80  * Redistribution and use in source and binary forms, with or without
81  * modification, are permitted provided that the following conditions
82  * are met:
83  * 1. Redistributions of source code must retain the copyright
84  *    notice, this list of conditions and the following disclaimer.
85  * 2. Redistributions in binary form must reproduce the above copyright
86  *    notice, this list of conditions and the following disclaimer in the
87  *    documentation and/or other materials provided with the distribution.
88  * 3. All advertising materials mentioning features or use of this software
89  *    must display the following acknowledgement:
90  *    "This product includes cryptographic software written by
91  *     Eric Young (eay@cryptsoft.com)"
92  *    The word 'cryptographic' can be left out if the rouines from the library
93  *    being used are not cryptographic related :-).
94  * 4. If you include any Windows specific code (or a derivative thereof) from 
95  *    the apps directory (application code) you must include an acknowledgement:
96  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97  * 
98  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108  * SUCH DAMAGE.
109  * 
110  * The licence and distribution terms for any publically available version or
111  * derivative of this code cannot be changed.  i.e. this code cannot simply be
112  * copied and put under another distribution licence
113  * [including the GNU Public Licence.]
114  */
115
116 #include <stdio.h>
117 #include "ssl_locl.h"
118 #ifndef OPENSSL_NO_KRB5
119 #include "kssl_lcl.h"
120 #endif
121 #include <openssl/buffer.h>
122 #include <openssl/rand.h>
123 #include <openssl/objects.h>
124 #include <openssl/evp.h>
125 #include <openssl/md5.h>
126 #include <openssl/bn.h>
127 #ifndef OPENSSL_NO_DH
128 #include <openssl/dh.h>
129 #endif
130
131 static const SSL_METHOD *dtls1_get_client_method(int ver);
132 static int dtls1_get_hello_verify(SSL *s);
133
134 static const SSL_METHOD *dtls1_get_client_method(int ver)
135         {
136         if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
137                 return(DTLSv1_client_method());
138         else if (ver == DTLS1_2_VERSION)
139                 return(DTLSv1_2_client_method());
140         else
141                 return(NULL);
142         }
143
144 IMPLEMENT_dtls1_meth_func(DTLS1_VERSION,
145                         DTLSv1_client_method,
146                         ssl_undefined_function,
147                         dtls1_connect,
148                         dtls1_get_client_method,
149                         DTLSv1_enc_data)
150
151 IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION,
152                         DTLSv1_2_client_method,
153                         ssl_undefined_function,
154                         dtls1_connect,
155                         dtls1_get_client_method,
156                         DTLSv1_2_enc_data)
157
158 IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION,
159                         DTLS_client_method,
160                         ssl_undefined_function,
161                         dtls1_connect,
162                         dtls1_get_client_method,
163                         DTLSv1_2_enc_data)
164
165 int dtls1_connect(SSL *s)
166         {
167         BUF_MEM *buf=NULL;
168         unsigned long Time=(unsigned long)time(NULL);
169         void (*cb)(const SSL *ssl,int type,int val)=NULL;
170         int ret= -1;
171         int new_state,state,skip=0;
172 #ifndef OPENSSL_NO_SCTP
173         unsigned char sctpauthkey[64];
174         char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
175 #endif
176
177         RAND_add(&Time,sizeof(Time),0);
178         ERR_clear_error();
179         clear_sys_error();
180
181         if (s->info_callback != NULL)
182                 cb=s->info_callback;
183         else if (s->ctx->info_callback != NULL)
184                 cb=s->ctx->info_callback;
185         
186         s->in_handshake++;
187         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
188
189 #ifndef OPENSSL_NO_SCTP
190         /* Notify SCTP BIO socket to enter handshake
191          * mode and prevent stream identifier other
192          * than 0. Will be ignored if no SCTP is used.
193          */
194         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
195 #endif
196
197 #ifndef OPENSSL_NO_HEARTBEATS
198         /* If we're awaiting a HeartbeatResponse, pretend we
199          * already got and don't await it anymore, because
200          * Heartbeats don't make sense during handshakes anyway.
201          */
202         if (s->tlsext_hb_pending)
203                 {
204                 dtls1_stop_timer(s);
205                 s->tlsext_hb_pending = 0;
206                 s->tlsext_hb_seq++;
207                 }
208 #endif
209
210         for (;;)
211                 {
212                 state=s->state;
213
214                 switch(s->state)
215                         {
216                 case SSL_ST_RENEGOTIATE:
217                         s->renegotiate=1;
218                         s->state=SSL_ST_CONNECT;
219                         s->ctx->stats.sess_connect_renegotiate++;
220                         /* break */
221                 case SSL_ST_BEFORE:
222                 case SSL_ST_CONNECT:
223                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
224                 case SSL_ST_OK|SSL_ST_CONNECT:
225
226                         s->server=0;
227                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
228
229                         if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
230                             (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
231                                 {
232                                 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
233                                 ret = -1;
234                                 goto end;
235                                 }
236                                 
237                         /* s->version=SSL3_VERSION; */
238                         s->type=SSL_ST_CONNECT;
239
240                         if (s->init_buf == NULL)
241                                 {
242                                 if ((buf=BUF_MEM_new()) == NULL)
243                                         {
244                                         ret= -1;
245                                         goto end;
246                                         }
247                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
248                                         {
249                                         ret= -1;
250                                         goto end;
251                                         }
252                                 s->init_buf=buf;
253                                 buf=NULL;
254                                 }
255
256                         if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
257
258                         /* setup buffing BIO */
259                         if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
260
261                         /* don't push the buffering BIO quite yet */
262
263                         s->state=SSL3_ST_CW_CLNT_HELLO_A;
264                         s->ctx->stats.sess_connect++;
265                         s->init_num=0;
266                         /* mark client_random uninitialized */
267                         memset(s->s3->client_random,0,sizeof(s->s3->client_random));
268                         s->d1->send_cookie = 0;
269                         s->hit = 0;
270                         break;
271
272 #ifndef OPENSSL_NO_SCTP
273                 case DTLS1_SCTP_ST_CR_READ_SOCK:
274
275                         if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)))
276                         {
277                                 s->s3->in_read_app_data=2;
278                                 s->rwstate=SSL_READING;
279                                 BIO_clear_retry_flags(SSL_get_rbio(s));
280                                 BIO_set_retry_read(SSL_get_rbio(s));
281                                 ret = -1;
282                                 goto end;
283                         }
284
285                         s->state=s->s3->tmp.next_state;
286                         break;
287
288                 case DTLS1_SCTP_ST_CW_WRITE_SOCK:
289                         /* read app data until dry event */
290
291                         ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
292                         if (ret < 0) goto end;
293
294                         if (ret == 0)
295                         {
296                                 s->s3->in_read_app_data=2;
297                                 s->rwstate=SSL_READING;
298                                 BIO_clear_retry_flags(SSL_get_rbio(s));
299                                 BIO_set_retry_read(SSL_get_rbio(s));
300                                 ret = -1;
301                                 goto end;
302                         }
303
304                         s->state=s->d1->next_state;
305                         break;
306 #endif
307
308                 case SSL3_ST_CW_CLNT_HELLO_A:
309                 case SSL3_ST_CW_CLNT_HELLO_B:
310
311                         s->shutdown=0;
312
313                         /* every DTLS ClientHello resets Finished MAC */
314                         ssl3_init_finished_mac(s);
315
316                         dtls1_start_timer(s);
317                         ret=ssl3_client_hello(s);
318                         if (ret <= 0) goto end;
319
320                         if ( s->d1->send_cookie)
321                                 {
322                                 s->state=SSL3_ST_CW_FLUSH;
323                                 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
324                                 }
325                         else
326                                 s->state=SSL3_ST_CR_SRVR_HELLO_A;
327
328                         s->init_num=0;
329
330 #ifndef OPENSSL_NO_SCTP
331                         /* Disable buffering for SCTP */
332                         if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
333                                 {
334 #endif
335                                 /* turn on buffering for the next lot of output */
336                                 if (s->bbio != s->wbio)
337                                         s->wbio=BIO_push(s->bbio,s->wbio);
338 #ifndef OPENSSL_NO_SCTP
339                                 }
340 #endif
341
342                         break;
343
344                 case SSL3_ST_CR_SRVR_HELLO_A:
345                 case SSL3_ST_CR_SRVR_HELLO_B:
346                         ret=ssl3_get_server_hello(s);
347                         if (ret <= 0) goto end;
348                         else
349                                 {
350                                 if (s->hit)
351                                         {
352 #ifndef OPENSSL_NO_SCTP
353                                         /* Add new shared key for SCTP-Auth,
354                                          * will be ignored if no SCTP used.
355                                          */
356                                         snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
357                                                  DTLS1_SCTP_AUTH_LABEL);
358
359                                         SSL_export_keying_material(s, sctpauthkey,
360                                                                    sizeof(sctpauthkey), labelbuffer,
361                                                                    sizeof(labelbuffer), NULL, 0, 0);
362
363                                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
364                                                          sizeof(sctpauthkey), sctpauthkey);
365 #endif
366
367                                         s->state=SSL3_ST_CR_FINISHED_A;
368                                         }
369                                 else
370                                         s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
371                                 }
372                         s->init_num=0;
373                         break;
374
375                 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
376                 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
377
378                         ret = dtls1_get_hello_verify(s);
379                         if ( ret <= 0)
380                                 goto end;
381                         dtls1_stop_timer(s);
382                         if ( s->d1->send_cookie) /* start again, with a cookie */
383                                 s->state=SSL3_ST_CW_CLNT_HELLO_A;
384                         else
385                                 s->state = SSL3_ST_CR_CERT_A;
386                         s->init_num = 0;
387                         break;
388
389                 case SSL3_ST_CR_CERT_A:
390                 case SSL3_ST_CR_CERT_B:
391                         /* Check if it is anon DH or PSK */
392                         if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
393                             !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
394                                 {
395                                 ret=ssl3_get_server_certificate(s);
396                                 if (ret <= 0) goto end;
397 #ifndef OPENSSL_NO_TLSEXT
398                                 if (s->tlsext_status_expected)
399                                         s->state=SSL3_ST_CR_CERT_STATUS_A;
400                                 else
401                                         s->state=SSL3_ST_CR_KEY_EXCH_A;
402                                 }
403                         else
404                                 {
405                                 skip = 1;
406                                 s->state=SSL3_ST_CR_KEY_EXCH_A;
407                                 }
408 #else
409                                 }
410                         else
411                                 skip=1;
412
413                         s->state=SSL3_ST_CR_KEY_EXCH_A;
414 #endif
415                         s->init_num=0;
416                         break;
417
418                 case SSL3_ST_CR_KEY_EXCH_A:
419                 case SSL3_ST_CR_KEY_EXCH_B:
420                         ret=ssl3_get_key_exchange(s);
421                         if (ret <= 0) goto end;
422                         s->state=SSL3_ST_CR_CERT_REQ_A;
423                         s->init_num=0;
424
425                         /* at this point we check that we have the
426                          * required stuff from the server */
427                         if (!ssl3_check_cert_and_algorithm(s))
428                                 {
429                                 ret= -1;
430                                 goto end;
431                                 }
432                         break;
433
434                 case SSL3_ST_CR_CERT_REQ_A:
435                 case SSL3_ST_CR_CERT_REQ_B:
436                         ret=ssl3_get_certificate_request(s);
437                         if (ret <= 0) goto end;
438                         s->state=SSL3_ST_CR_SRVR_DONE_A;
439                         s->init_num=0;
440                         break;
441
442                 case SSL3_ST_CR_SRVR_DONE_A:
443                 case SSL3_ST_CR_SRVR_DONE_B:
444                         ret=ssl3_get_server_done(s);
445                         if (ret <= 0) goto end;
446                         dtls1_stop_timer(s);
447                         if (s->s3->tmp.cert_req)
448                                 s->s3->tmp.next_state=SSL3_ST_CW_CERT_A;
449                         else
450                                 s->s3->tmp.next_state=SSL3_ST_CW_KEY_EXCH_A;
451                         s->init_num=0;
452
453 #ifndef OPENSSL_NO_SCTP                 
454                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
455                             state == SSL_ST_RENEGOTIATE)
456                                 s->state=DTLS1_SCTP_ST_CR_READ_SOCK;
457                         else
458 #endif                  
459                         s->state=s->s3->tmp.next_state;
460                         break;
461
462                 case SSL3_ST_CW_CERT_A:
463                 case SSL3_ST_CW_CERT_B:
464                 case SSL3_ST_CW_CERT_C:
465                 case SSL3_ST_CW_CERT_D:
466                         dtls1_start_timer(s);
467                         ret=ssl3_send_client_certificate(s);
468                         if (ret <= 0) goto end;
469                         s->state=SSL3_ST_CW_KEY_EXCH_A;
470                         s->init_num=0;
471                         break;
472
473                 case SSL3_ST_CW_KEY_EXCH_A:
474                 case SSL3_ST_CW_KEY_EXCH_B:
475                         dtls1_start_timer(s);
476                         ret=ssl3_send_client_key_exchange(s);
477                         if (ret <= 0) goto end;
478
479 #ifndef OPENSSL_NO_SCTP
480                         /* Add new shared key for SCTP-Auth,
481                          * will be ignored if no SCTP used.
482                          */
483                         snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
484                                  DTLS1_SCTP_AUTH_LABEL);
485
486                         SSL_export_keying_material(s, sctpauthkey,
487                                                    sizeof(sctpauthkey), labelbuffer,
488                                                    sizeof(labelbuffer), NULL, 0, 0);
489
490                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
491                                          sizeof(sctpauthkey), sctpauthkey);
492 #endif
493
494                         /* EAY EAY EAY need to check for DH fix cert
495                          * sent back */
496                         /* For TLS, cert_req is set to 2, so a cert chain
497                          * of nothing is sent, but no verify packet is sent */
498                         if (s->s3->tmp.cert_req == 1)
499                                 {
500                                 s->state=SSL3_ST_CW_CERT_VRFY_A;
501                                 }
502                         else
503                                 {
504 #ifndef OPENSSL_NO_SCTP
505                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
506                                         {
507                                         s->d1->next_state=SSL3_ST_CW_CHANGE_A;
508                                         s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
509                                         }
510                                 else
511 #endif
512                                         s->state=SSL3_ST_CW_CHANGE_A;
513                                 s->s3->change_cipher_spec=0;
514                                 }
515
516                         s->init_num=0;
517                         break;
518
519                 case SSL3_ST_CW_CERT_VRFY_A:
520                 case SSL3_ST_CW_CERT_VRFY_B:
521                         dtls1_start_timer(s);
522                         ret=ssl3_send_client_verify(s);
523                         if (ret <= 0) goto end;
524 #ifndef OPENSSL_NO_SCTP
525                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
526                         {
527                                 s->d1->next_state=SSL3_ST_CW_CHANGE_A;
528                                 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
529                         }
530                         else
531 #endif
532                                 s->state=SSL3_ST_CW_CHANGE_A;
533                         s->init_num=0;
534                         s->s3->change_cipher_spec=0;
535                         break;
536
537                 case SSL3_ST_CW_CHANGE_A:
538                 case SSL3_ST_CW_CHANGE_B:
539                         if (!s->hit)
540                                 dtls1_start_timer(s);
541                         ret=dtls1_send_change_cipher_spec(s,
542                                 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
543                         if (ret <= 0) goto end;
544
545                         s->state=SSL3_ST_CW_FINISHED_A;
546                         s->init_num=0;
547
548                         s->session->cipher=s->s3->tmp.new_cipher;
549 #ifdef OPENSSL_NO_COMP
550                         s->session->compress_meth=0;
551 #else
552                         if (s->s3->tmp.new_compression == NULL)
553                                 s->session->compress_meth=0;
554                         else
555                                 s->session->compress_meth=
556                                         s->s3->tmp.new_compression->id;
557 #endif
558                         if (!s->method->ssl3_enc->setup_key_block(s))
559                                 {
560                                 ret= -1;
561                                 goto end;
562                                 }
563
564                         if (!s->method->ssl3_enc->change_cipher_state(s,
565                                 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
566                                 {
567                                 ret= -1;
568                                 goto end;
569                                 }
570                         
571 #ifndef OPENSSL_NO_SCTP
572                                 if (s->hit)
573                                         {
574                                         /* Change to new shared key of SCTP-Auth,
575                                          * will be ignored if no SCTP used.
576                                          */
577                                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
578                                         }
579 #endif
580
581                         dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
582                         break;
583
584                 case SSL3_ST_CW_FINISHED_A:
585                 case SSL3_ST_CW_FINISHED_B:
586                         if (!s->hit)
587                                 dtls1_start_timer(s);
588                         ret=ssl3_send_finished(s,
589                                 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
590                                 s->method->ssl3_enc->client_finished_label,
591                                 s->method->ssl3_enc->client_finished_label_len);
592                         if (ret <= 0) goto end;
593                         s->state=SSL3_ST_CW_FLUSH;
594
595                         /* clear flags */
596                         s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
597                         if (s->hit)
598                                 {
599                                 s->s3->tmp.next_state=SSL_ST_OK;
600 #ifndef OPENSSL_NO_SCTP
601                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
602                                         {
603                                                 s->d1->next_state = s->s3->tmp.next_state;
604                                                 s->s3->tmp.next_state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
605                                         }
606 #endif
607                                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
608                                         {
609                                         s->state=SSL_ST_OK;
610 #ifndef OPENSSL_NO_SCTP
611                                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
612                                                 {
613                                                         s->d1->next_state = SSL_ST_OK;
614                                                         s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
615                                                 }
616 #endif
617                                         s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
618                                         s->s3->delay_buf_pop_ret=0;
619                                         }
620                                 }
621                         else
622                                 {
623 #ifndef OPENSSL_NO_SCTP
624                                 /* Change to new shared key of SCTP-Auth,
625                                  * will be ignored if no SCTP used.
626                                  */
627                                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
628 #endif
629
630 #ifndef OPENSSL_NO_TLSEXT
631                                 /* Allow NewSessionTicket if ticket expected */
632                                 if (s->tlsext_ticket_expected)
633                                         s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
634                                 else
635 #endif
636                                 
637                                 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
638                                 }
639                         s->init_num=0;
640                         break;
641
642 #ifndef OPENSSL_NO_TLSEXT
643                 case SSL3_ST_CR_SESSION_TICKET_A:
644                 case SSL3_ST_CR_SESSION_TICKET_B:
645                         ret=ssl3_get_new_session_ticket(s);
646                         if (ret <= 0) goto end;
647                         s->state=SSL3_ST_CR_FINISHED_A;
648                         s->init_num=0;
649                 break;
650
651                 case SSL3_ST_CR_CERT_STATUS_A:
652                 case SSL3_ST_CR_CERT_STATUS_B:
653                         ret=ssl3_get_cert_status(s);
654                         if (ret <= 0) goto end;
655                         s->state=SSL3_ST_CR_KEY_EXCH_A;
656                         s->init_num=0;
657                 break;
658 #endif
659
660                 case SSL3_ST_CR_FINISHED_A:
661                 case SSL3_ST_CR_FINISHED_B:
662                         s->d1->change_cipher_spec_ok = 1;
663                         ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
664                                 SSL3_ST_CR_FINISHED_B);
665                         if (ret <= 0) goto end;
666                         dtls1_stop_timer(s);
667
668                         if (s->hit)
669                                 s->state=SSL3_ST_CW_CHANGE_A;
670                         else
671                                 s->state=SSL_ST_OK;
672
673 #ifndef OPENSSL_NO_SCTP
674                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
675                                 state == SSL_ST_RENEGOTIATE)
676                                 {
677                                 s->d1->next_state=s->state;
678                                 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
679                                 }
680 #endif
681
682                         s->init_num=0;
683                         break;
684
685                 case SSL3_ST_CW_FLUSH:
686                         s->rwstate=SSL_WRITING;
687                         if (BIO_flush(s->wbio) <= 0)
688                                 {
689                                 /* If the write error was fatal, stop trying */
690                                 if (!BIO_should_retry(s->wbio))
691                                         {
692                                         s->rwstate=SSL_NOTHING;
693                                         s->state=s->s3->tmp.next_state;
694                                         }
695                                 
696                                 ret= -1;
697                                 goto end;
698                                 }
699                         s->rwstate=SSL_NOTHING;
700                         s->state=s->s3->tmp.next_state;
701                         break;
702
703                 case SSL_ST_OK:
704                         /* clean a few things up */
705                         ssl3_cleanup_key_block(s);
706
707 #if 0
708                         if (s->init_buf != NULL)
709                                 {
710                                 BUF_MEM_free(s->init_buf);
711                                 s->init_buf=NULL;
712                                 }
713 #endif
714
715                         /* If we are not 'joining' the last two packets,
716                          * remove the buffering now */
717                         if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
718                                 ssl_free_wbio_buffer(s);
719                         /* else do it later in ssl3_write */
720
721                         s->init_num=0;
722                         s->renegotiate=0;
723                         s->new_session=0;
724
725                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
726                         if (s->hit) s->ctx->stats.sess_hit++;
727
728                         ret=1;
729                         /* s->server=0; */
730                         s->handshake_func=dtls1_connect;
731                         s->ctx->stats.sess_connect_good++;
732
733                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
734
735                         /* done with handshaking */
736                         s->d1->handshake_read_seq  = 0;
737                         s->d1->next_handshake_write_seq = 0;
738                         goto end;
739                         /* break; */
740                         
741                 default:
742                         SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
743                         ret= -1;
744                         goto end;
745                         /* break; */
746                         }
747
748                 /* did we do anything */
749                 if (!s->s3->tmp.reuse_message && !skip)
750                         {
751                         if (s->debug)
752                                 {
753                                 if ((ret=BIO_flush(s->wbio)) <= 0)
754                                         goto end;
755                                 }
756
757                         if ((cb != NULL) && (s->state != state))
758                                 {
759                                 new_state=s->state;
760                                 s->state=state;
761                                 cb(s,SSL_CB_CONNECT_LOOP,1);
762                                 s->state=new_state;
763                                 }
764                         }
765                 skip=0;
766                 }
767 end:
768         s->in_handshake--;
769         
770 #ifndef OPENSSL_NO_SCTP
771         /* Notify SCTP BIO socket to leave handshake
772          * mode and allow stream identifier other
773          * than 0. Will be ignored if no SCTP is used.
774          */
775         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
776 #endif
777
778         if (buf != NULL)
779                 BUF_MEM_free(buf);
780         if (cb != NULL)
781                 cb(s,SSL_CB_CONNECT_EXIT,ret);
782         return(ret);
783         }
784
785 static int dtls1_get_hello_verify(SSL *s)
786         {
787         int n, al, ok = 0;
788         unsigned char *data;
789         unsigned int cookie_len;
790
791         s->first_packet = 1;
792         n=s->method->ssl_get_message(s,
793                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
794                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
795                 -1,
796                 s->max_cert_list,
797                 &ok);
798         s->first_packet = 0;
799
800         if (!ok) return((int)n);
801
802         if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
803                 {
804                 s->d1->send_cookie = 0;
805                 s->s3->tmp.reuse_message=1;
806                 return(1);
807                 }
808
809         data = (unsigned char *)s->init_msg;
810 #if 0
811         if (s->method->version != DTLS_ANY_VERSION &&
812                 ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))))
813                 {
814                 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
815                 s->version=(s->version&0xff00)|data[1];
816                 al = SSL_AD_PROTOCOL_VERSION;
817                 goto f_err;
818                 }
819 #endif
820         data+=2;
821
822         cookie_len = *(data++);
823         if ( cookie_len > sizeof(s->d1->cookie))
824                 {
825                 al=SSL_AD_ILLEGAL_PARAMETER;
826                 goto f_err;
827                 }
828
829         memcpy(s->d1->cookie, data, cookie_len);
830         s->d1->cookie_len = cookie_len;
831
832         s->d1->send_cookie = 1;
833         return 1;
834
835 f_err:
836         ssl3_send_alert(s, SSL3_AL_FATAL, al);
837         return -1;
838         }