DTLS version usage fixes.
[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 #ifndef OPENSSL_NO_TLSEXT
392                         ret=ssl3_check_finished(s);
393                         if (ret <= 0) goto end;
394                         if (ret == 2)
395                                 {
396                                 s->hit = 1;
397                                 if (s->tlsext_ticket_expected)
398                                         s->state=SSL3_ST_CR_SESSION_TICKET_A;
399                                 else
400                                         s->state=SSL3_ST_CR_FINISHED_A;
401                                 s->init_num=0;
402                                 break;
403                                 }
404 #endif
405                         /* Check if it is anon DH or PSK */
406                         if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
407                             !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
408                                 {
409                                 ret=ssl3_get_server_certificate(s);
410                                 if (ret <= 0) goto end;
411 #ifndef OPENSSL_NO_TLSEXT
412                                 if (s->tlsext_status_expected)
413                                         s->state=SSL3_ST_CR_CERT_STATUS_A;
414                                 else
415                                         s->state=SSL3_ST_CR_KEY_EXCH_A;
416                                 }
417                         else
418                                 {
419                                 skip = 1;
420                                 s->state=SSL3_ST_CR_KEY_EXCH_A;
421                                 }
422 #else
423                                 }
424                         else
425                                 skip=1;
426
427                         s->state=SSL3_ST_CR_KEY_EXCH_A;
428 #endif
429                         s->init_num=0;
430                         break;
431
432                 case SSL3_ST_CR_KEY_EXCH_A:
433                 case SSL3_ST_CR_KEY_EXCH_B:
434                         ret=ssl3_get_key_exchange(s);
435                         if (ret <= 0) goto end;
436                         s->state=SSL3_ST_CR_CERT_REQ_A;
437                         s->init_num=0;
438
439                         /* at this point we check that we have the
440                          * required stuff from the server */
441                         if (!ssl3_check_cert_and_algorithm(s))
442                                 {
443                                 ret= -1;
444                                 goto end;
445                                 }
446                         break;
447
448                 case SSL3_ST_CR_CERT_REQ_A:
449                 case SSL3_ST_CR_CERT_REQ_B:
450                         ret=ssl3_get_certificate_request(s);
451                         if (ret <= 0) goto end;
452                         s->state=SSL3_ST_CR_SRVR_DONE_A;
453                         s->init_num=0;
454                         break;
455
456                 case SSL3_ST_CR_SRVR_DONE_A:
457                 case SSL3_ST_CR_SRVR_DONE_B:
458                         ret=ssl3_get_server_done(s);
459                         if (ret <= 0) goto end;
460                         dtls1_stop_timer(s);
461                         if (s->s3->tmp.cert_req)
462                                 s->s3->tmp.next_state=SSL3_ST_CW_CERT_A;
463                         else
464                                 s->s3->tmp.next_state=SSL3_ST_CW_KEY_EXCH_A;
465                         s->init_num=0;
466
467 #ifndef OPENSSL_NO_SCTP                 
468                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
469                             state == SSL_ST_RENEGOTIATE)
470                                 s->state=DTLS1_SCTP_ST_CR_READ_SOCK;
471                         else
472 #endif                  
473                         s->state=s->s3->tmp.next_state;
474                         break;
475
476                 case SSL3_ST_CW_CERT_A:
477                 case SSL3_ST_CW_CERT_B:
478                 case SSL3_ST_CW_CERT_C:
479                 case SSL3_ST_CW_CERT_D:
480                         dtls1_start_timer(s);
481                         ret=ssl3_send_client_certificate(s);
482                         if (ret <= 0) goto end;
483                         s->state=SSL3_ST_CW_KEY_EXCH_A;
484                         s->init_num=0;
485                         break;
486
487                 case SSL3_ST_CW_KEY_EXCH_A:
488                 case SSL3_ST_CW_KEY_EXCH_B:
489                         dtls1_start_timer(s);
490                         ret=ssl3_send_client_key_exchange(s);
491                         if (ret <= 0) goto end;
492
493 #ifndef OPENSSL_NO_SCTP
494                         /* Add new shared key for SCTP-Auth,
495                          * will be ignored if no SCTP used.
496                          */
497                         snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
498                                  DTLS1_SCTP_AUTH_LABEL);
499
500                         SSL_export_keying_material(s, sctpauthkey,
501                                                    sizeof(sctpauthkey), labelbuffer,
502                                                    sizeof(labelbuffer), NULL, 0, 0);
503
504                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
505                                          sizeof(sctpauthkey), sctpauthkey);
506 #endif
507
508                         /* EAY EAY EAY need to check for DH fix cert
509                          * sent back */
510                         /* For TLS, cert_req is set to 2, so a cert chain
511                          * of nothing is sent, but no verify packet is sent */
512                         if (s->s3->tmp.cert_req == 1)
513                                 {
514                                 s->state=SSL3_ST_CW_CERT_VRFY_A;
515                                 }
516                         else
517                                 {
518 #ifndef OPENSSL_NO_SCTP
519                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
520                                         {
521                                         s->d1->next_state=SSL3_ST_CW_CHANGE_A;
522                                         s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
523                                         }
524                                 else
525 #endif
526                                         s->state=SSL3_ST_CW_CHANGE_A;
527                                 s->s3->change_cipher_spec=0;
528                                 }
529
530                         s->init_num=0;
531                         break;
532
533                 case SSL3_ST_CW_CERT_VRFY_A:
534                 case SSL3_ST_CW_CERT_VRFY_B:
535                         dtls1_start_timer(s);
536                         ret=ssl3_send_client_verify(s);
537                         if (ret <= 0) goto end;
538 #ifndef OPENSSL_NO_SCTP
539                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
540                         {
541                                 s->d1->next_state=SSL3_ST_CW_CHANGE_A;
542                                 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
543                         }
544                         else
545 #endif
546                                 s->state=SSL3_ST_CW_CHANGE_A;
547                         s->init_num=0;
548                         s->s3->change_cipher_spec=0;
549                         break;
550
551                 case SSL3_ST_CW_CHANGE_A:
552                 case SSL3_ST_CW_CHANGE_B:
553                         if (!s->hit)
554                                 dtls1_start_timer(s);
555                         ret=dtls1_send_change_cipher_spec(s,
556                                 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
557                         if (ret <= 0) goto end;
558
559 #ifndef OPENSSL_NO_SCTP
560                         /* Change to new shared key of SCTP-Auth,
561                          * will be ignored if no SCTP used.
562                          */
563                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
564 #endif
565
566                         s->state=SSL3_ST_CW_FINISHED_A;
567                         s->init_num=0;
568
569                         s->session->cipher=s->s3->tmp.new_cipher;
570 #ifdef OPENSSL_NO_COMP
571                         s->session->compress_meth=0;
572 #else
573                         if (s->s3->tmp.new_compression == NULL)
574                                 s->session->compress_meth=0;
575                         else
576                                 s->session->compress_meth=
577                                         s->s3->tmp.new_compression->id;
578 #endif
579                         if (!s->method->ssl3_enc->setup_key_block(s))
580                                 {
581                                 ret= -1;
582                                 goto end;
583                                 }
584
585                         if (!s->method->ssl3_enc->change_cipher_state(s,
586                                 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
587                                 {
588                                 ret= -1;
589                                 goto end;
590                                 }
591                         
592                         dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
593                         break;
594
595                 case SSL3_ST_CW_FINISHED_A:
596                 case SSL3_ST_CW_FINISHED_B:
597                         if (!s->hit)
598                                 dtls1_start_timer(s);
599                         ret=ssl3_send_finished(s,
600                                 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
601                                 s->method->ssl3_enc->client_finished_label,
602                                 s->method->ssl3_enc->client_finished_label_len);
603                         if (ret <= 0) goto end;
604                         s->state=SSL3_ST_CW_FLUSH;
605
606                         /* clear flags */
607                         s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
608                         if (s->hit)
609                                 {
610                                 s->s3->tmp.next_state=SSL_ST_OK;
611 #ifndef OPENSSL_NO_SCTP
612                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
613                                         {
614                                                 s->d1->next_state = s->s3->tmp.next_state;
615                                                 s->s3->tmp.next_state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
616                                         }
617 #endif
618                                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
619                                         {
620                                         s->state=SSL_ST_OK;
621 #ifndef OPENSSL_NO_SCTP
622                                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
623                                                 {
624                                                         s->d1->next_state = SSL_ST_OK;
625                                                         s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
626                                                 }
627 #endif
628                                         s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
629                                         s->s3->delay_buf_pop_ret=0;
630                                         }
631                                 }
632                         else
633                                 {
634 #ifndef OPENSSL_NO_TLSEXT
635                                 /* Allow NewSessionTicket if ticket expected */
636                                 if (s->tlsext_ticket_expected)
637                                         s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
638                                 else
639 #endif
640                                 
641                                 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
642                                 }
643                         s->init_num=0;
644                         break;
645
646 #ifndef OPENSSL_NO_TLSEXT
647                 case SSL3_ST_CR_SESSION_TICKET_A:
648                 case SSL3_ST_CR_SESSION_TICKET_B:
649                         ret=ssl3_get_new_session_ticket(s);
650                         if (ret <= 0) goto end;
651                         s->state=SSL3_ST_CR_FINISHED_A;
652                         s->init_num=0;
653                 break;
654
655                 case SSL3_ST_CR_CERT_STATUS_A:
656                 case SSL3_ST_CR_CERT_STATUS_B:
657                         ret=ssl3_get_cert_status(s);
658                         if (ret <= 0) goto end;
659                         s->state=SSL3_ST_CR_KEY_EXCH_A;
660                         s->init_num=0;
661                 break;
662 #endif
663
664                 case SSL3_ST_CR_FINISHED_A:
665                 case SSL3_ST_CR_FINISHED_B:
666                         s->d1->change_cipher_spec_ok = 1;
667                         ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
668                                 SSL3_ST_CR_FINISHED_B);
669                         if (ret <= 0) goto end;
670                         dtls1_stop_timer(s);
671
672                         if (s->hit)
673                                 s->state=SSL3_ST_CW_CHANGE_A;
674                         else
675                                 s->state=SSL_ST_OK;
676
677 #ifndef OPENSSL_NO_SCTP
678                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
679                                 state == SSL_ST_RENEGOTIATE)
680                                 {
681                                 s->d1->next_state=s->state;
682                                 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
683                                 }
684 #endif
685
686                         s->init_num=0;
687                         break;
688
689                 case SSL3_ST_CW_FLUSH:
690                         s->rwstate=SSL_WRITING;
691                         if (BIO_flush(s->wbio) <= 0)
692                                 {
693                                 /* If the write error was fatal, stop trying */
694                                 if (!BIO_should_retry(s->wbio))
695                                         {
696                                         s->rwstate=SSL_NOTHING;
697                                         s->state=s->s3->tmp.next_state;
698                                         }
699                                 
700                                 ret= -1;
701                                 goto end;
702                                 }
703                         s->rwstate=SSL_NOTHING;
704                         s->state=s->s3->tmp.next_state;
705                         break;
706
707                 case SSL_ST_OK:
708                         /* clean a few things up */
709                         ssl3_cleanup_key_block(s);
710
711 #if 0
712                         if (s->init_buf != NULL)
713                                 {
714                                 BUF_MEM_free(s->init_buf);
715                                 s->init_buf=NULL;
716                                 }
717 #endif
718
719                         /* If we are not 'joining' the last two packets,
720                          * remove the buffering now */
721                         if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
722                                 ssl_free_wbio_buffer(s);
723                         /* else do it later in ssl3_write */
724
725                         s->init_num=0;
726                         s->renegotiate=0;
727                         s->new_session=0;
728
729                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
730                         if (s->hit) s->ctx->stats.sess_hit++;
731
732                         ret=1;
733                         /* s->server=0; */
734                         s->handshake_func=dtls1_connect;
735                         s->ctx->stats.sess_connect_good++;
736
737                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
738
739                         /* done with handshaking */
740                         s->d1->handshake_read_seq  = 0;
741                         s->d1->next_handshake_write_seq = 0;
742                         goto end;
743                         /* break; */
744                         
745                 default:
746                         SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
747                         ret= -1;
748                         goto end;
749                         /* break; */
750                         }
751
752                 /* did we do anything */
753                 if (!s->s3->tmp.reuse_message && !skip)
754                         {
755                         if (s->debug)
756                                 {
757                                 if ((ret=BIO_flush(s->wbio)) <= 0)
758                                         goto end;
759                                 }
760
761                         if ((cb != NULL) && (s->state != state))
762                                 {
763                                 new_state=s->state;
764                                 s->state=state;
765                                 cb(s,SSL_CB_CONNECT_LOOP,1);
766                                 s->state=new_state;
767                                 }
768                         }
769                 skip=0;
770                 }
771 end:
772         s->in_handshake--;
773         
774 #ifndef OPENSSL_NO_SCTP
775         /* Notify SCTP BIO socket to leave handshake
776          * mode and allow stream identifier other
777          * than 0. Will be ignored if no SCTP is used.
778          */
779         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
780 #endif
781
782         if (buf != NULL)
783                 BUF_MEM_free(buf);
784         if (cb != NULL)
785                 cb(s,SSL_CB_CONNECT_EXIT,ret);
786         return(ret);
787         }
788
789 static int dtls1_get_hello_verify(SSL *s)
790         {
791         int n, al, ok = 0;
792         unsigned char *data;
793         unsigned int cookie_len;
794
795         s->first_packet = 1;
796         n=s->method->ssl_get_message(s,
797                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
798                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
799                 -1,
800                 s->max_cert_list,
801                 &ok);
802         s->first_packet = 0;
803
804         if (!ok) return((int)n);
805
806         if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
807                 {
808                 s->d1->send_cookie = 0;
809                 s->s3->tmp.reuse_message=1;
810                 return(1);
811                 }
812
813         data = (unsigned char *)s->init_msg;
814 #if 0
815         if (s->method->version != DTLS_ANY_VERSION &&
816                 ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))))
817                 {
818                 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
819                 s->version=(s->version&0xff00)|data[1];
820                 al = SSL_AD_PROTOCOL_VERSION;
821                 goto f_err;
822                 }
823 #endif
824         data+=2;
825
826         cookie_len = *(data++);
827         if ( cookie_len > sizeof(s->d1->cookie))
828                 {
829                 al=SSL_AD_ILLEGAL_PARAMETER;
830                 goto f_err;
831                 }
832
833         memcpy(s->d1->cookie, data, cookie_len);
834         s->d1->cookie_len = cookie_len;
835
836         s->d1->send_cookie = 1;
837         return 1;
838
839 f_err:
840         ssl3_send_alert(s, SSL3_AL_FATAL, al);
841         return -1;
842         }
843