Extend DTLS method macros.
[openssl.git] / ssl / d1_srvr.c
1 /* ssl/d1_srvr.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 #include <openssl/buffer.h>
119 #include <openssl/rand.h>
120 #include <openssl/objects.h>
121 #include <openssl/evp.h>
122 #include <openssl/x509.h>
123 #include <openssl/md5.h>
124 #include <openssl/bn.h>
125 #ifndef OPENSSL_NO_DH
126 #include <openssl/dh.h>
127 #endif
128
129 static const SSL_METHOD *dtls1_get_server_method(int ver);
130 static int dtls1_send_hello_verify_request(SSL *s);
131
132 static const SSL_METHOD *dtls1_get_server_method(int ver)
133         {
134         if (ver == DTLS1_VERSION)
135                 return(DTLSv1_server_method());
136         else
137                 return(NULL);
138         }
139
140 IMPLEMENT_dtls1_meth_func(DTLS1_VERSION,
141                         DTLSv1_server_method,
142                         dtls1_accept,
143                         ssl_undefined_function,
144                         dtls1_get_server_method,
145                         DTLSv1_enc_data)
146
147 int dtls1_accept(SSL *s)
148         {
149         BUF_MEM *buf;
150         unsigned long Time=(unsigned long)time(NULL);
151         void (*cb)(const SSL *ssl,int type,int val)=NULL;
152         unsigned long alg_k;
153         int ret= -1;
154         int new_state,state,skip=0;
155         int listen;
156 #ifndef OPENSSL_NO_SCTP
157         unsigned char sctpauthkey[64];
158         char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
159 #endif
160
161         RAND_add(&Time,sizeof(Time),0);
162         ERR_clear_error();
163         clear_sys_error();
164
165         if (s->info_callback != NULL)
166                 cb=s->info_callback;
167         else if (s->ctx->info_callback != NULL)
168                 cb=s->ctx->info_callback;
169         
170         listen = s->d1->listen;
171
172         /* init things to blank */
173         s->in_handshake++;
174         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
175
176         s->d1->listen = listen;
177 #ifndef OPENSSL_NO_SCTP
178         /* Notify SCTP BIO socket to enter handshake
179          * mode and prevent stream identifier other
180          * than 0. Will be ignored if no SCTP is used.
181          */
182         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
183 #endif
184
185         if (s->cert == NULL)
186                 {
187                 SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
188                 return(-1);
189                 }
190
191 #ifndef OPENSSL_NO_HEARTBEATS
192         /* If we're awaiting a HeartbeatResponse, pretend we
193          * already got and don't await it anymore, because
194          * Heartbeats don't make sense during handshakes anyway.
195          */
196         if (s->tlsext_hb_pending)
197                 {
198                 dtls1_stop_timer(s);
199                 s->tlsext_hb_pending = 0;
200                 s->tlsext_hb_seq++;
201                 }
202 #endif
203
204         for (;;)
205                 {
206                 state=s->state;
207
208                 switch (s->state)
209                         {
210                 case SSL_ST_RENEGOTIATE:
211                         s->renegotiate=1;
212                         /* s->state=SSL_ST_ACCEPT; */
213
214                 case SSL_ST_BEFORE:
215                 case SSL_ST_ACCEPT:
216                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
217                 case SSL_ST_OK|SSL_ST_ACCEPT:
218
219                         s->server=1;
220                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
221
222                         if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))
223                                 {
224                                 SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
225                                 return -1;
226                                 }
227                         s->type=SSL_ST_ACCEPT;
228
229                         if (s->init_buf == NULL)
230                                 {
231                                 if ((buf=BUF_MEM_new()) == NULL)
232                                         {
233                                         ret= -1;
234                                         goto end;
235                                         }
236                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
237                                         {
238                                         ret= -1;
239                                         goto end;
240                                         }
241                                 s->init_buf=buf;
242                                 }
243
244                         if (!ssl3_setup_buffers(s))
245                                 {
246                                 ret= -1;
247                                 goto end;
248                                 }
249
250                         s->init_num=0;
251
252                         if (s->state != SSL_ST_RENEGOTIATE)
253                                 {
254                                 /* Ok, we now need to push on a buffering BIO so that
255                                  * the output is sent in a way that TCP likes :-)
256                                  * ...but not with SCTP :-)
257                                  */
258 #ifndef OPENSSL_NO_SCTP
259                                 if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
260 #endif
261                                         if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
262
263                                 ssl3_init_finished_mac(s);
264                                 s->state=SSL3_ST_SR_CLNT_HELLO_A;
265                                 s->ctx->stats.sess_accept++;
266                                 }
267                         else
268                                 {
269                                 /* s->state == SSL_ST_RENEGOTIATE,
270                                  * we will just send a HelloRequest */
271                                 s->ctx->stats.sess_accept_renegotiate++;
272                                 s->state=SSL3_ST_SW_HELLO_REQ_A;
273                                 }
274
275                         break;
276
277                 case SSL3_ST_SW_HELLO_REQ_A:
278                 case SSL3_ST_SW_HELLO_REQ_B:
279
280                         s->shutdown=0;
281                         dtls1_clear_record_buffer(s);
282                         dtls1_start_timer(s);
283                         ret=ssl3_send_hello_request(s);
284                         if (ret <= 0) goto end;
285                         s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
286                         s->state=SSL3_ST_SW_FLUSH;
287                         s->init_num=0;
288
289                         ssl3_init_finished_mac(s);
290                         break;
291
292                 case SSL3_ST_SW_HELLO_REQ_C:
293                         s->state=SSL_ST_OK;
294                         break;
295
296                 case SSL3_ST_SR_CLNT_HELLO_A:
297                 case SSL3_ST_SR_CLNT_HELLO_B:
298                 case SSL3_ST_SR_CLNT_HELLO_C:
299
300                         s->shutdown=0;
301                         ret=ssl3_get_client_hello(s);
302                         if (ret <= 0) goto end;
303                         dtls1_stop_timer(s);
304
305                         if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
306                                 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
307                         else
308                                 s->state = SSL3_ST_SW_SRVR_HELLO_A;
309
310                         s->init_num=0;
311
312                         /* Reflect ClientHello sequence to remain stateless while listening */
313                         if (listen)
314                                 {
315                                 memcpy(s->s3->write_sequence, s->s3->read_sequence, sizeof(s->s3->write_sequence));
316                                 }
317
318                         /* If we're just listening, stop here */
319                         if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A)
320                                 {
321                                 ret = 2;
322                                 s->d1->listen = 0;
323                                 /* Set expected sequence numbers
324                                  * to continue the handshake.
325                                  */
326                                 s->d1->handshake_read_seq = 2;
327                                 s->d1->handshake_write_seq = 1;
328                                 s->d1->next_handshake_write_seq = 1;
329                                 goto end;
330                                 }
331                         
332                         break;
333                         
334                 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
335                 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
336
337                         ret = dtls1_send_hello_verify_request(s);
338                         if ( ret <= 0) goto end;
339                         s->state=SSL3_ST_SW_FLUSH;
340                         s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
341
342                         /* HelloVerifyRequest resets Finished MAC */
343                         if (s->version != DTLS1_BAD_VER)
344                                 ssl3_init_finished_mac(s);
345                         break;
346                         
347 #ifndef OPENSSL_NO_SCTP
348                 case DTLS1_SCTP_ST_SR_READ_SOCK:
349                         
350                         if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)))                
351                                 {
352                                 s->s3->in_read_app_data=2;
353                                 s->rwstate=SSL_READING;
354                                 BIO_clear_retry_flags(SSL_get_rbio(s));
355                                 BIO_set_retry_read(SSL_get_rbio(s));
356                                 ret = -1;
357                                 goto end;
358                                 }
359                         
360                         s->state=SSL3_ST_SR_FINISHED_A;
361                         break;
362                         
363                 case DTLS1_SCTP_ST_SW_WRITE_SOCK:
364                         ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
365                         if (ret < 0) goto end;
366                         
367                         if (ret == 0)
368                                 {
369                                 if (s->d1->next_state != SSL_ST_OK)
370                                         {
371                                         s->s3->in_read_app_data=2;
372                                         s->rwstate=SSL_READING;
373                                         BIO_clear_retry_flags(SSL_get_rbio(s));
374                                         BIO_set_retry_read(SSL_get_rbio(s));
375                                         ret = -1;
376                                         goto end;
377                                         }
378                                 }
379
380                         s->state=s->d1->next_state;
381                         break;
382 #endif
383
384                 case SSL3_ST_SW_SRVR_HELLO_A:
385                 case SSL3_ST_SW_SRVR_HELLO_B:
386                         s->renegotiate = 2;
387                         dtls1_start_timer(s);
388                         ret=ssl3_send_server_hello(s);
389                         if (ret <= 0) goto end;
390
391                         if (s->hit)
392                                 {
393 #ifndef OPENSSL_NO_SCTP
394                                 /* Add new shared key for SCTP-Auth,
395                                  * will be ignored if no SCTP used.
396                                  */
397                                 snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
398                                          DTLS1_SCTP_AUTH_LABEL);
399
400                                 SSL_export_keying_material(s, sctpauthkey,
401                                                            sizeof(sctpauthkey), labelbuffer,
402                                                            sizeof(labelbuffer), NULL, 0, 0);
403                                 
404                                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
405                          sizeof(sctpauthkey), sctpauthkey);
406 #endif
407 #ifndef OPENSSL_NO_TLSEXT
408                                 if (s->tlsext_ticket_expected)
409                                         s->state=SSL3_ST_SW_SESSION_TICKET_A;
410                                 else
411                                         s->state=SSL3_ST_SW_CHANGE_A;
412 #else
413                                 s->state=SSL3_ST_SW_CHANGE_A;
414 #endif
415                                 }
416                         else
417                                 s->state=SSL3_ST_SW_CERT_A;
418                         s->init_num=0;
419                         break;
420
421                 case SSL3_ST_SW_CERT_A:
422                 case SSL3_ST_SW_CERT_B:
423                         /* Check if it is anon DH or normal PSK */
424                         if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
425                                 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
426                                 {
427                                 dtls1_start_timer(s);
428                                 ret=ssl3_send_server_certificate(s);
429                                 if (ret <= 0) goto end;
430 #ifndef OPENSSL_NO_TLSEXT
431                                 if (s->tlsext_status_expected)
432                                         s->state=SSL3_ST_SW_CERT_STATUS_A;
433                                 else
434                                         s->state=SSL3_ST_SW_KEY_EXCH_A;
435                                 }
436                         else
437                                 {
438                                 skip = 1;
439                                 s->state=SSL3_ST_SW_KEY_EXCH_A;
440                                 }
441 #else
442                                 }
443                         else
444                                 skip=1;
445
446                         s->state=SSL3_ST_SW_KEY_EXCH_A;
447 #endif
448                         s->init_num=0;
449                         break;
450
451                 case SSL3_ST_SW_KEY_EXCH_A:
452                 case SSL3_ST_SW_KEY_EXCH_B:
453                         alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
454
455                         /* clear this, it may get reset by
456                          * send_server_key_exchange */
457                         if ((s->options & SSL_OP_EPHEMERAL_RSA)
458 #ifndef OPENSSL_NO_KRB5
459                                 && !(alg_k & SSL_kKRB5)
460 #endif /* OPENSSL_NO_KRB5 */
461                                 )
462                                 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
463                                  * even when forbidden by protocol specs
464                                  * (handshake may fail as clients are not required to
465                                  * be able to handle this) */
466                                 s->s3->tmp.use_rsa_tmp=1;
467                         else
468                                 s->s3->tmp.use_rsa_tmp=0;
469
470                         /* only send if a DH key exchange or
471                          * RSA but we have a sign only certificate */
472                         if (s->s3->tmp.use_rsa_tmp
473                         /* PSK: send ServerKeyExchange if PSK identity
474                          * hint if provided */
475 #ifndef OPENSSL_NO_PSK
476                             || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
477 #endif
478                             || (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
479                             || (alg_k & SSL_kEECDH)
480                             || ((alg_k & SSL_kRSA)
481                                 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
482                                     || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
483                                         && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
484                                         )
485                                     )
486                                 )
487                             )
488                                 {
489                                 dtls1_start_timer(s);
490                                 ret=ssl3_send_server_key_exchange(s);
491                                 if (ret <= 0) goto end;
492                                 }
493                         else
494                                 skip=1;
495
496                         s->state=SSL3_ST_SW_CERT_REQ_A;
497                         s->init_num=0;
498                         break;
499
500                 case SSL3_ST_SW_CERT_REQ_A:
501                 case SSL3_ST_SW_CERT_REQ_B:
502                         if (/* don't request cert unless asked for it: */
503                                 !(s->verify_mode & SSL_VERIFY_PEER) ||
504                                 /* if SSL_VERIFY_CLIENT_ONCE is set,
505                                  * don't request cert during re-negotiation: */
506                                 ((s->session->peer != NULL) &&
507                                  (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
508                                 /* never request cert in anonymous ciphersuites
509                                  * (see section "Certificate request" in SSL 3 drafts
510                                  * and in RFC 2246): */
511                                 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
512                                  /* ... except when the application insists on verification
513                                   * (against the specs, but s3_clnt.c accepts this for SSL 3) */
514                                  !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
515                                  /* never request cert in Kerberos ciphersuites */
516                                 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
517                                 /* With normal PSK Certificates and
518                                  * Certificate Requests are omitted */
519                                 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
520                                 {
521                                 /* no cert request */
522                                 skip=1;
523                                 s->s3->tmp.cert_request=0;
524                                 s->state=SSL3_ST_SW_SRVR_DONE_A;
525 #ifndef OPENSSL_NO_SCTP
526                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
527                                         {
528                                         s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
529                                         s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
530                                         }
531 #endif
532                                 }
533                         else
534                                 {
535                                 s->s3->tmp.cert_request=1;
536                                 dtls1_start_timer(s);
537                                 ret=ssl3_send_certificate_request(s);
538                                 if (ret <= 0) goto end;
539 #ifndef NETSCAPE_HANG_BUG
540                                 s->state=SSL3_ST_SW_SRVR_DONE_A;
541 #ifndef OPENSSL_NO_SCTP
542                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
543                                         {
544                                         s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
545                                         s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
546                                         }
547 #endif
548 #else
549                                 s->state=SSL3_ST_SW_FLUSH;
550                                 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
551 #ifndef OPENSSL_NO_SCTP
552                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
553                                         {
554                                         s->d1->next_state = s->s3->tmp.next_state;
555                                         s->s3->tmp.next_state=DTLS1_SCTP_ST_SW_WRITE_SOCK;
556                                         }
557 #endif
558 #endif
559                                 s->init_num=0;
560                                 }
561                         break;
562
563                 case SSL3_ST_SW_SRVR_DONE_A:
564                 case SSL3_ST_SW_SRVR_DONE_B:
565                         dtls1_start_timer(s);
566                         ret=ssl3_send_server_done(s);
567                         if (ret <= 0) goto end;
568                         s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
569                         s->state=SSL3_ST_SW_FLUSH;
570                         s->init_num=0;
571                         break;
572                 
573                 case SSL3_ST_SW_FLUSH:
574                         s->rwstate=SSL_WRITING;
575                         if (BIO_flush(s->wbio) <= 0)
576                                 {
577                                 /* If the write error was fatal, stop trying */
578                                 if (!BIO_should_retry(s->wbio))
579                                         {
580                                         s->rwstate=SSL_NOTHING;
581                                         s->state=s->s3->tmp.next_state;
582                                         }
583                                 
584                                 ret= -1;
585                                 goto end;
586                                 }
587                         s->rwstate=SSL_NOTHING;
588                         s->state=s->s3->tmp.next_state;
589                         break;
590
591                 case SSL3_ST_SR_CERT_A:
592                 case SSL3_ST_SR_CERT_B:
593                         /* Check for second client hello (MS SGC) */
594                         ret = ssl3_check_client_hello(s);
595                         if (ret <= 0)
596                                 goto end;
597                         if (ret == 2)
598                                 {
599                                 dtls1_stop_timer(s);
600                                 s->state = SSL3_ST_SR_CLNT_HELLO_C;
601                                 }
602                         else {
603                                 /* could be sent for a DH cert, even if we
604                                  * have not asked for it :-) */
605                                 ret=ssl3_get_client_certificate(s);
606                                 if (ret <= 0) goto end;
607                                 s->init_num=0;
608                                 s->state=SSL3_ST_SR_KEY_EXCH_A;
609                         }
610                         break;
611
612                 case SSL3_ST_SR_KEY_EXCH_A:
613                 case SSL3_ST_SR_KEY_EXCH_B:
614                         ret=ssl3_get_client_key_exchange(s);
615                         if (ret <= 0) goto end;
616 #ifndef OPENSSL_NO_SCTP
617                         /* Add new shared key for SCTP-Auth,
618                          * will be ignored if no SCTP used.
619                          */
620                         snprintf((char *) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
621                                  DTLS1_SCTP_AUTH_LABEL);
622
623                         SSL_export_keying_material(s, sctpauthkey,
624                                                    sizeof(sctpauthkey), labelbuffer,
625                                                    sizeof(labelbuffer), NULL, 0, 0);
626
627                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
628                                  sizeof(sctpauthkey), sctpauthkey);
629 #endif
630
631                         s->state=SSL3_ST_SR_CERT_VRFY_A;
632                         s->init_num=0;
633
634                         if (ret == 2)
635                                 {
636                                 /* For the ECDH ciphersuites when
637                                  * the client sends its ECDH pub key in
638                                  * a certificate, the CertificateVerify
639                                  * message is not sent.
640                                  */
641                                 s->state=SSL3_ST_SR_FINISHED_A;
642                                 s->init_num = 0;
643                                 }
644                         else
645                                 {
646                                 s->state=SSL3_ST_SR_CERT_VRFY_A;
647                                 s->init_num=0;
648
649                                 /* We need to get hashes here so if there is
650                                  * a client cert, it can be verified */ 
651                                 s->method->ssl3_enc->cert_verify_mac(s,
652                                         NID_md5,
653                                         &(s->s3->tmp.cert_verify_md[0]));
654                                 s->method->ssl3_enc->cert_verify_mac(s,
655                                         NID_sha1,
656                                         &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
657                                 }
658                         break;
659
660                 case SSL3_ST_SR_CERT_VRFY_A:
661                 case SSL3_ST_SR_CERT_VRFY_B:
662
663                         s->d1->change_cipher_spec_ok = 1;
664                         /* we should decide if we expected this one */
665                         ret=ssl3_get_cert_verify(s);
666                         if (ret <= 0) goto end;
667 #ifndef OPENSSL_NO_SCTP
668                         if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
669                             state == SSL_ST_RENEGOTIATE)
670                                 s->state=DTLS1_SCTP_ST_SR_READ_SOCK;
671                         else
672 #endif                  
673                                 s->state=SSL3_ST_SR_FINISHED_A;
674                         s->init_num=0;
675                         break;
676
677                 case SSL3_ST_SR_FINISHED_A:
678                 case SSL3_ST_SR_FINISHED_B:
679                         s->d1->change_cipher_spec_ok = 1;
680                         ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
681                                 SSL3_ST_SR_FINISHED_B);
682                         if (ret <= 0) goto end;
683                         dtls1_stop_timer(s);
684                         if (s->hit)
685                                 s->state=SSL_ST_OK;
686 #ifndef OPENSSL_NO_TLSEXT
687                         else if (s->tlsext_ticket_expected)
688                                 s->state=SSL3_ST_SW_SESSION_TICKET_A;
689 #endif
690                         else
691                                 s->state=SSL3_ST_SW_CHANGE_A;
692                         s->init_num=0;
693                         break;
694
695 #ifndef OPENSSL_NO_TLSEXT
696                 case SSL3_ST_SW_SESSION_TICKET_A:
697                 case SSL3_ST_SW_SESSION_TICKET_B:
698                         ret=ssl3_send_newsession_ticket(s);
699                         if (ret <= 0) goto end;
700                         s->state=SSL3_ST_SW_CHANGE_A;
701                         s->init_num=0;
702                         break;
703
704                 case SSL3_ST_SW_CERT_STATUS_A:
705                 case SSL3_ST_SW_CERT_STATUS_B:
706                         ret=ssl3_send_cert_status(s);
707                         if (ret <= 0) goto end;
708                         s->state=SSL3_ST_SW_KEY_EXCH_A;
709                         s->init_num=0;
710                         break;
711
712 #endif
713
714                 case SSL3_ST_SW_CHANGE_A:
715                 case SSL3_ST_SW_CHANGE_B:
716
717                         s->session->cipher=s->s3->tmp.new_cipher;
718                         if (!s->method->ssl3_enc->setup_key_block(s))
719                                 { ret= -1; goto end; }
720
721                         ret=dtls1_send_change_cipher_spec(s,
722                                 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
723
724                         if (ret <= 0) goto end;
725
726 #ifndef OPENSSL_NO_SCTP
727                         /* Change to new shared key of SCTP-Auth,
728                          * will be ignored if no SCTP used.
729                          */
730                         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
731 #endif
732
733                         s->state=SSL3_ST_SW_FINISHED_A;
734                         s->init_num=0;
735
736                         if (!s->method->ssl3_enc->change_cipher_state(s,
737                                 SSL3_CHANGE_CIPHER_SERVER_WRITE))
738                                 {
739                                 ret= -1;
740                                 goto end;
741                                 }
742
743                         dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
744                         break;
745
746                 case SSL3_ST_SW_FINISHED_A:
747                 case SSL3_ST_SW_FINISHED_B:
748                         ret=ssl3_send_finished(s,
749                                 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
750                                 s->method->ssl3_enc->server_finished_label,
751                                 s->method->ssl3_enc->server_finished_label_len);
752                         if (ret <= 0) goto end;
753                         s->state=SSL3_ST_SW_FLUSH;
754                         if (s->hit)
755                                 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
756                         else
757                                 {
758                                 s->s3->tmp.next_state=SSL_ST_OK;
759 #ifndef OPENSSL_NO_SCTP
760                                 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
761                                         {
762                                         s->d1->next_state = s->s3->tmp.next_state;
763                                         s->s3->tmp.next_state=DTLS1_SCTP_ST_SW_WRITE_SOCK;
764                                         }
765 #endif
766                                 }
767                         s->init_num=0;
768                         break;
769
770                 case SSL_ST_OK:
771                         /* clean a few things up */
772                         ssl3_cleanup_key_block(s);
773
774 #if 0
775                         BUF_MEM_free(s->init_buf);
776                         s->init_buf=NULL;
777 #endif
778
779                         /* remove buffering on output */
780                         ssl_free_wbio_buffer(s);
781
782                         s->init_num=0;
783
784                         if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
785                                 {
786                                 s->renegotiate=0;
787                                 s->new_session=0;
788                                 
789                                 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
790                                 
791                                 s->ctx->stats.sess_accept_good++;
792                                 /* s->server=1; */
793                                 s->handshake_func=dtls1_accept;
794
795                                 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
796                                 }
797                         
798                         ret = 1;
799
800                         /* done handshaking, next message is client hello */
801                         s->d1->handshake_read_seq = 0;
802                         /* next message is server hello */
803                         s->d1->handshake_write_seq = 0;
804                         s->d1->next_handshake_write_seq = 0;
805                         goto end;
806                         /* break; */
807
808                 default:
809                         SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);
810                         ret= -1;
811                         goto end;
812                         /* break; */
813                         }
814                 
815                 if (!s->s3->tmp.reuse_message && !skip)
816                         {
817                         if (s->debug)
818                                 {
819                                 if ((ret=BIO_flush(s->wbio)) <= 0)
820                                         goto end;
821                                 }
822
823
824                         if ((cb != NULL) && (s->state != state))
825                                 {
826                                 new_state=s->state;
827                                 s->state=state;
828                                 cb(s,SSL_CB_ACCEPT_LOOP,1);
829                                 s->state=new_state;
830                                 }
831                         }
832                 skip=0;
833                 }
834 end:
835         /* BIO_flush(s->wbio); */
836
837         s->in_handshake--;
838 #ifndef OPENSSL_NO_SCTP
839                 /* Notify SCTP BIO socket to leave handshake
840                  * mode and prevent stream identifier other
841                  * than 0. Will be ignored if no SCTP is used.
842                  */
843                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
844 #endif
845
846         if (cb != NULL)
847                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
848         return(ret);
849         }
850
851 int dtls1_send_hello_verify_request(SSL *s)
852         {
853         unsigned int msg_len;
854         unsigned char *msg, *buf, *p;
855
856         if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
857                 {
858                 buf = (unsigned char *)s->init_buf->data;
859
860                 msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
861                 *(p++) = s->version >> 8;
862                 *(p++) = s->version & 0xFF;
863
864                 if (s->ctx->app_gen_cookie_cb == NULL ||
865                      s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
866                          &(s->d1->cookie_len)) == 0)
867                         {
868                         SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR);
869                         return 0;
870                         }
871
872                 *(p++) = (unsigned char) s->d1->cookie_len;
873                 memcpy(p, s->d1->cookie, s->d1->cookie_len);
874                 p += s->d1->cookie_len;
875                 msg_len = p - msg;
876
877                 dtls1_set_message_header(s, buf,
878                         DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
879
880                 s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
881                 /* number of bytes to write */
882                 s->init_num=p-buf;
883                 s->init_off=0;
884                 }
885
886         /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
887         return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
888         }