Fix compilation with no-nextprotoneg.
[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                         s->state=SSL3_ST_CW_FINISHED_A;
560                         s->init_num=0;
561
562                         s->session->cipher=s->s3->tmp.new_cipher;
563 #ifdef OPENSSL_NO_COMP
564                         s->session->compress_meth=0;
565 #else
566                         if (s->s3->tmp.new_compression == NULL)
567                                 s->session->compress_meth=0;
568                         else
569                                 s->session->compress_meth=
570                                         s->s3->tmp.new_compression->id;
571 #endif
572                         if (!s->method->ssl3_enc->setup_key_block(s))
573                                 {
574                                 ret= -1;
575                                 goto end;
576                                 }
577
578                         if (!s->method->ssl3_enc->change_cipher_state(s,
579                                 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
580                                 {
581                                 ret= -1;
582                                 goto end;
583                                 }
584                         
585 #ifndef OPENSSL_NO_SCTP
586                                 if (s->hit)
587                                         {
588                                         /* Change to new shared key of SCTP-Auth,
589                                          * will be ignored if no SCTP used.
590                                          */
591                                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
592                                         }
593 #endif
594
595                         dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
596                         break;
597
598                 case SSL3_ST_CW_FINISHED_A:
599                 case SSL3_ST_CW_FINISHED_B:
600                         if (!s->hit)
601                                 dtls1_start_timer(s);
602                         ret=ssl3_send_finished(s,
603                                 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
604                                 s->method->ssl3_enc->client_finished_label,
605                                 s->method->ssl3_enc->client_finished_label_len);
606                         if (ret <= 0) goto end;
607                         s->state=SSL3_ST_CW_FLUSH;
608
609                         /* clear flags */
610                         s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
611                         if (s->hit)
612                                 {
613                                 s->s3->tmp.next_state=SSL_ST_OK;
614 #ifndef OPENSSL_NO_SCTP
615                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
616                                         {
617                                                 s->d1->next_state = s->s3->tmp.next_state;
618                                                 s->s3->tmp.next_state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
619                                         }
620 #endif
621                                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
622                                         {
623                                         s->state=SSL_ST_OK;
624 #ifndef OPENSSL_NO_SCTP
625                                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
626                                                 {
627                                                         s->d1->next_state = SSL_ST_OK;
628                                                         s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
629                                                 }
630 #endif
631                                         s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
632                                         s->s3->delay_buf_pop_ret=0;
633                                         }
634                                 }
635                         else
636                                 {
637 #ifndef OPENSSL_NO_SCTP
638                                 /* Change to new shared key of SCTP-Auth,
639                                  * will be ignored if no SCTP used.
640                                  */
641                                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
642 #endif
643
644 #ifndef OPENSSL_NO_TLSEXT
645                                 /* Allow NewSessionTicket if ticket expected */
646                                 if (s->tlsext_ticket_expected)
647                                         s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
648                                 else
649 #endif
650                                 
651                                 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
652                                 }
653                         s->init_num=0;
654                         break;
655
656 #ifndef OPENSSL_NO_TLSEXT
657                 case SSL3_ST_CR_SESSION_TICKET_A:
658                 case SSL3_ST_CR_SESSION_TICKET_B:
659                         ret=ssl3_get_new_session_ticket(s);
660                         if (ret <= 0) goto end;
661                         s->state=SSL3_ST_CR_FINISHED_A;
662                         s->init_num=0;
663                 break;
664
665                 case SSL3_ST_CR_CERT_STATUS_A:
666                 case SSL3_ST_CR_CERT_STATUS_B:
667                         ret=ssl3_get_cert_status(s);
668                         if (ret <= 0) goto end;
669                         s->state=SSL3_ST_CR_KEY_EXCH_A;
670                         s->init_num=0;
671                 break;
672 #endif
673
674                 case SSL3_ST_CR_FINISHED_A:
675                 case SSL3_ST_CR_FINISHED_B:
676                         s->d1->change_cipher_spec_ok = 1;
677                         ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
678                                 SSL3_ST_CR_FINISHED_B);
679                         if (ret <= 0) goto end;
680                         dtls1_stop_timer(s);
681
682                         if (s->hit)
683                                 s->state=SSL3_ST_CW_CHANGE_A;
684                         else
685                                 s->state=SSL_ST_OK;
686
687 #ifndef OPENSSL_NO_SCTP
688                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
689                                 state == SSL_ST_RENEGOTIATE)
690                                 {
691                                 s->d1->next_state=s->state;
692                                 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
693                                 }
694 #endif
695
696                         s->init_num=0;
697                         break;
698
699                 case SSL3_ST_CW_FLUSH:
700                         s->rwstate=SSL_WRITING;
701                         if (BIO_flush(s->wbio) <= 0)
702                                 {
703                                 /* If the write error was fatal, stop trying */
704                                 if (!BIO_should_retry(s->wbio))
705                                         {
706                                         s->rwstate=SSL_NOTHING;
707                                         s->state=s->s3->tmp.next_state;
708                                         }
709                                 
710                                 ret= -1;
711                                 goto end;
712                                 }
713                         s->rwstate=SSL_NOTHING;
714                         s->state=s->s3->tmp.next_state;
715                         break;
716
717                 case SSL_ST_OK:
718                         /* clean a few things up */
719                         ssl3_cleanup_key_block(s);
720
721 #if 0
722                         if (s->init_buf != NULL)
723                                 {
724                                 BUF_MEM_free(s->init_buf);
725                                 s->init_buf=NULL;
726                                 }
727 #endif
728
729                         /* If we are not 'joining' the last two packets,
730                          * remove the buffering now */
731                         if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
732                                 ssl_free_wbio_buffer(s);
733                         /* else do it later in ssl3_write */
734
735                         s->init_num=0;
736                         s->renegotiate=0;
737                         s->new_session=0;
738
739                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
740                         if (s->hit) s->ctx->stats.sess_hit++;
741
742                         ret=1;
743                         /* s->server=0; */
744                         s->handshake_func=dtls1_connect;
745                         s->ctx->stats.sess_connect_good++;
746
747                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
748
749                         /* done with handshaking */
750                         s->d1->handshake_read_seq  = 0;
751                         s->d1->next_handshake_write_seq = 0;
752                         goto end;
753                         /* break; */
754                         
755                 default:
756                         SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
757                         ret= -1;
758                         goto end;
759                         /* break; */
760                         }
761
762                 /* did we do anything */
763                 if (!s->s3->tmp.reuse_message && !skip)
764                         {
765                         if (s->debug)
766                                 {
767                                 if ((ret=BIO_flush(s->wbio)) <= 0)
768                                         goto end;
769                                 }
770
771                         if ((cb != NULL) && (s->state != state))
772                                 {
773                                 new_state=s->state;
774                                 s->state=state;
775                                 cb(s,SSL_CB_CONNECT_LOOP,1);
776                                 s->state=new_state;
777                                 }
778                         }
779                 skip=0;
780                 }
781 end:
782         s->in_handshake--;
783         
784 #ifndef OPENSSL_NO_SCTP
785         /* Notify SCTP BIO socket to leave handshake
786          * mode and allow stream identifier other
787          * than 0. Will be ignored if no SCTP is used.
788          */
789         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
790 #endif
791
792         if (buf != NULL)
793                 BUF_MEM_free(buf);
794         if (cb != NULL)
795                 cb(s,SSL_CB_CONNECT_EXIT,ret);
796         return(ret);
797         }
798
799 static int dtls1_get_hello_verify(SSL *s)
800         {
801         int n, al, ok = 0;
802         unsigned char *data;
803         unsigned int cookie_len;
804
805         s->first_packet = 1;
806         n=s->method->ssl_get_message(s,
807                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
808                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
809                 -1,
810                 s->max_cert_list,
811                 &ok);
812         s->first_packet = 0;
813
814         if (!ok) return((int)n);
815
816         if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
817                 {
818                 s->d1->send_cookie = 0;
819                 s->s3->tmp.reuse_message=1;
820                 return(1);
821                 }
822
823         data = (unsigned char *)s->init_msg;
824 #if 0
825         if (s->method->version != DTLS_ANY_VERSION &&
826                 ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))))
827                 {
828                 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
829                 s->version=(s->version&0xff00)|data[1];
830                 al = SSL_AD_PROTOCOL_VERSION;
831                 goto f_err;
832                 }
833 #endif
834         data+=2;
835
836         cookie_len = *(data++);
837         if ( cookie_len > sizeof(s->d1->cookie))
838                 {
839                 al=SSL_AD_ILLEGAL_PARAMETER;
840                 goto f_err;
841                 }
842
843         memcpy(s->d1->cookie, data, cookie_len);
844         s->d1->cookie_len = cookie_len;
845
846         s->d1->send_cookie = 1;
847         return 1;
848
849 f_err:
850         ssl3_send_alert(s, SSL3_AL_FATAL, al);
851         return -1;
852         }
853