Import OpenSSL-0.9.8m.
[dragonfly.git] / crypto / openssl / 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-2005 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 "kssl_lcl.h"
119 #include <openssl/buffer.h>
120 #include <openssl/rand.h>
121 #include <openssl/objects.h>
122 #include <openssl/evp.h>
123 #include <openssl/md5.h>
124 #ifndef OPENSSL_NO_DH
125 #include <openssl/dh.h>
126 #endif
127
128 static SSL_METHOD *dtls1_get_client_method(int ver);
129 static int dtls1_get_hello_verify(SSL *s);
130
131 static SSL_METHOD *dtls1_get_client_method(int ver)
132         {
133         if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
134                 return(DTLSv1_client_method());
135         else
136                 return(NULL);
137         }
138
139 IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,
140                         ssl_undefined_function,
141                         dtls1_connect,
142                         dtls1_get_client_method)
143
144 int dtls1_connect(SSL *s)
145         {
146         BUF_MEM *buf=NULL;
147         unsigned long Time=(unsigned long)time(NULL),l;
148         void (*cb)(const SSL *ssl,int type,int val)=NULL;
149         int ret= -1;
150         int new_state,state,skip=0;;
151
152         RAND_add(&Time,sizeof(Time),0);
153         ERR_clear_error();
154         clear_sys_error();
155
156         if (s->info_callback != NULL)
157                 cb=s->info_callback;
158         else if (s->ctx->info_callback != NULL)
159                 cb=s->ctx->info_callback;
160         
161         s->in_handshake++;
162         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
163
164         for (;;)
165                 {
166                 state=s->state;
167
168                 switch(s->state)
169                         {
170                 case SSL_ST_RENEGOTIATE:
171                         s->new_session=1;
172                         s->state=SSL_ST_CONNECT;
173                         s->ctx->stats.sess_connect_renegotiate++;
174                         /* break */
175                 case SSL_ST_BEFORE:
176                 case SSL_ST_CONNECT:
177                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
178                 case SSL_ST_OK|SSL_ST_CONNECT:
179
180                         s->server=0;
181                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
182
183                         if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
184                             (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
185                                 {
186                                 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
187                                 ret = -1;
188                                 goto end;
189                                 }
190                                 
191                         /* s->version=SSL3_VERSION; */
192                         s->type=SSL_ST_CONNECT;
193
194                         if (s->init_buf == NULL)
195                                 {
196                                 if ((buf=BUF_MEM_new()) == NULL)
197                                         {
198                                         ret= -1;
199                                         goto end;
200                                         }
201                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
202                                         {
203                                         ret= -1;
204                                         goto end;
205                                         }
206                                 s->init_buf=buf;
207                                 buf=NULL;
208                                 }
209
210                         if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
211
212                         /* setup buffing BIO */
213                         if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
214
215                         /* don't push the buffering BIO quite yet */
216
217                         s->state=SSL3_ST_CW_CLNT_HELLO_A;
218                         s->ctx->stats.sess_connect++;
219                         s->init_num=0;
220                         /* mark client_random uninitialized */
221                         memset(s->s3->client_random,0,sizeof(s->s3->client_random));
222                         s->d1->send_cookie = 0;
223                         s->hit = 0;
224                         break;
225
226                 case SSL3_ST_CW_CLNT_HELLO_A:
227                 case SSL3_ST_CW_CLNT_HELLO_B:
228
229                         s->shutdown=0;
230
231                         /* every DTLS ClientHello resets Finished MAC */
232                         ssl3_init_finished_mac(s);
233
234                         dtls1_start_timer(s);
235                         ret=dtls1_client_hello(s);
236                         if (ret <= 0) goto end;
237
238                         if ( s->d1->send_cookie)
239                                 {
240                                 s->state=SSL3_ST_CW_FLUSH;
241                                 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
242                                 }
243                         else
244                                 s->state=SSL3_ST_CR_SRVR_HELLO_A;
245
246                         s->init_num=0;
247
248                         /* turn on buffering for the next lot of output */
249                         if (s->bbio != s->wbio)
250                                 s->wbio=BIO_push(s->bbio,s->wbio);
251
252                         break;
253
254                 case SSL3_ST_CR_SRVR_HELLO_A:
255                 case SSL3_ST_CR_SRVR_HELLO_B:
256                         ret=ssl3_get_server_hello(s);
257                         if (ret <= 0) goto end;
258                         else
259                                 {
260                                 dtls1_stop_timer(s);
261                                 if (s->hit)
262                                         s->state=SSL3_ST_CR_FINISHED_A;
263                                 else
264                                         s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
265                                 }
266                         s->init_num=0;
267                         break;
268
269                 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
270                 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
271
272                         ret = dtls1_get_hello_verify(s);
273                         if ( ret <= 0)
274                                 goto end;
275                         dtls1_stop_timer(s);
276                         if ( s->d1->send_cookie) /* start again, with a cookie */
277                                 s->state=SSL3_ST_CW_CLNT_HELLO_A;
278                         else
279                                 s->state = SSL3_ST_CR_CERT_A;
280                         s->init_num = 0;
281                         break;
282
283                 case SSL3_ST_CR_CERT_A:
284                 case SSL3_ST_CR_CERT_B:
285 #ifndef OPENSSL_NO_TLSEXT
286                         ret=ssl3_check_finished(s);
287                         if (ret <= 0) goto end;
288                         if (ret == 2)
289                                 {
290                                 s->hit = 1;
291                                 if (s->tlsext_ticket_expected)
292                                         s->state=SSL3_ST_CR_SESSION_TICKET_A;
293                                 else
294                                         s->state=SSL3_ST_CR_FINISHED_A;
295                                 s->init_num=0;
296                                 break;
297                                 }
298 #endif
299                         /* Check if it is anon DH */
300                         if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
301                                 {
302                                 ret=ssl3_get_server_certificate(s);
303                                 if (ret <= 0) goto end;
304 #ifndef OPENSSL_NO_TLSEXT
305                                 if (s->tlsext_status_expected)
306                                         s->state=SSL3_ST_CR_CERT_STATUS_A;
307                                 else
308                                         s->state=SSL3_ST_CR_KEY_EXCH_A;
309                                 }
310                         else
311                                 {
312                                 skip = 1;
313                                 s->state=SSL3_ST_CR_KEY_EXCH_A;
314                                 }
315 #else
316                                 }
317                         else
318                                 skip=1;
319
320                         s->state=SSL3_ST_CR_KEY_EXCH_A;
321 #endif
322                         s->init_num=0;
323                         break;
324
325                 case SSL3_ST_CR_KEY_EXCH_A:
326                 case SSL3_ST_CR_KEY_EXCH_B:
327                         ret=ssl3_get_key_exchange(s);
328                         if (ret <= 0) goto end;
329                         s->state=SSL3_ST_CR_CERT_REQ_A;
330                         s->init_num=0;
331
332                         /* at this point we check that we have the
333                          * required stuff from the server */
334                         if (!ssl3_check_cert_and_algorithm(s))
335                                 {
336                                 ret= -1;
337                                 goto end;
338                                 }
339                         break;
340
341                 case SSL3_ST_CR_CERT_REQ_A:
342                 case SSL3_ST_CR_CERT_REQ_B:
343                         ret=ssl3_get_certificate_request(s);
344                         if (ret <= 0) goto end;
345                         s->state=SSL3_ST_CR_SRVR_DONE_A;
346                         s->init_num=0;
347                         break;
348
349                 case SSL3_ST_CR_SRVR_DONE_A:
350                 case SSL3_ST_CR_SRVR_DONE_B:
351                         ret=ssl3_get_server_done(s);
352                         if (ret <= 0) goto end;
353                         if (s->s3->tmp.cert_req)
354                                 s->state=SSL3_ST_CW_CERT_A;
355                         else
356                                 s->state=SSL3_ST_CW_KEY_EXCH_A;
357                         s->init_num=0;
358
359                         break;
360
361                 case SSL3_ST_CW_CERT_A:
362                 case SSL3_ST_CW_CERT_B:
363                 case SSL3_ST_CW_CERT_C:
364                 case SSL3_ST_CW_CERT_D:
365                         dtls1_start_timer(s);
366                         ret=dtls1_send_client_certificate(s);
367                         if (ret <= 0) goto end;
368                         s->state=SSL3_ST_CW_KEY_EXCH_A;
369                         s->init_num=0;
370                         break;
371
372                 case SSL3_ST_CW_KEY_EXCH_A:
373                 case SSL3_ST_CW_KEY_EXCH_B:
374                         dtls1_start_timer(s);
375                         ret=dtls1_send_client_key_exchange(s);
376                         if (ret <= 0) goto end;
377                         l=s->s3->tmp.new_cipher->algorithms;
378                         /* EAY EAY EAY need to check for DH fix cert
379                          * sent back */
380                         /* For TLS, cert_req is set to 2, so a cert chain
381                          * of nothing is sent, but no verify packet is sent */
382                         if (s->s3->tmp.cert_req == 1)
383                                 {
384                                 s->state=SSL3_ST_CW_CERT_VRFY_A;
385                                 }
386                         else
387                                 {
388                                 s->state=SSL3_ST_CW_CHANGE_A;
389                                 s->s3->change_cipher_spec=0;
390                                 }
391
392                         s->init_num=0;
393                         break;
394
395                 case SSL3_ST_CW_CERT_VRFY_A:
396                 case SSL3_ST_CW_CERT_VRFY_B:
397                         dtls1_start_timer(s);
398                         ret=dtls1_send_client_verify(s);
399                         if (ret <= 0) goto end;
400                         s->state=SSL3_ST_CW_CHANGE_A;
401                         s->init_num=0;
402                         s->s3->change_cipher_spec=0;
403                         break;
404
405                 case SSL3_ST_CW_CHANGE_A:
406                 case SSL3_ST_CW_CHANGE_B:
407                         dtls1_start_timer(s);
408                         ret=dtls1_send_change_cipher_spec(s,
409                                 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
410                         if (ret <= 0) goto end;
411                         s->state=SSL3_ST_CW_FINISHED_A;
412                         s->init_num=0;
413
414                         s->session->cipher=s->s3->tmp.new_cipher;
415 #ifdef OPENSSL_NO_COMP
416                         s->session->compress_meth=0;
417 #else
418                         if (s->s3->tmp.new_compression == NULL)
419                                 s->session->compress_meth=0;
420                         else
421                                 s->session->compress_meth=
422                                         s->s3->tmp.new_compression->id;
423 #endif
424                         if (!s->method->ssl3_enc->setup_key_block(s))
425                                 {
426                                 ret= -1;
427                                 goto end;
428                                 }
429
430                         if (!s->method->ssl3_enc->change_cipher_state(s,
431                                 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
432                                 {
433                                 ret= -1;
434                                 goto end;
435                                 }
436                         
437                         dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
438                         break;
439
440                 case SSL3_ST_CW_FINISHED_A:
441                 case SSL3_ST_CW_FINISHED_B:
442                         dtls1_start_timer(s);
443                         ret=dtls1_send_finished(s,
444                                 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
445                                 s->method->ssl3_enc->client_finished_label,
446                                 s->method->ssl3_enc->client_finished_label_len);
447                         if (ret <= 0) goto end;
448                         s->state=SSL3_ST_CW_FLUSH;
449
450                         /* clear flags */
451                         s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
452                         if (s->hit)
453                                 {
454                                 s->s3->tmp.next_state=SSL_ST_OK;
455                                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
456                                         {
457                                         s->state=SSL_ST_OK;
458                                         s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
459                                         s->s3->delay_buf_pop_ret=0;
460                                         }
461                                 }
462                         else
463                                 {
464 #ifndef OPENSSL_NO_TLSEXT
465                                 /* Allow NewSessionTicket if ticket expected */
466                                 if (s->tlsext_ticket_expected)
467                                         s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
468                                 else
469 #endif
470                                 
471                                 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
472                                 }
473                         s->init_num=0;
474
475                         break;
476
477 #ifndef OPENSSL_NO_TLSEXT
478                 case SSL3_ST_CR_SESSION_TICKET_A:
479                 case SSL3_ST_CR_SESSION_TICKET_B:
480                         ret=ssl3_get_new_session_ticket(s);
481                         if (ret <= 0) goto end;
482                         s->state=SSL3_ST_CR_FINISHED_A;
483                         s->init_num=0;
484                 break;
485
486                 case SSL3_ST_CR_CERT_STATUS_A:
487                 case SSL3_ST_CR_CERT_STATUS_B:
488                         ret=ssl3_get_cert_status(s);
489                         if (ret <= 0) goto end;
490                         s->state=SSL3_ST_CR_KEY_EXCH_A;
491                         s->init_num=0;
492                 break;
493 #endif
494
495                 case SSL3_ST_CR_FINISHED_A:
496                 case SSL3_ST_CR_FINISHED_B:
497                         s->d1->change_cipher_spec_ok = 1;
498                         ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
499                                 SSL3_ST_CR_FINISHED_B);
500                         if (ret <= 0) goto end;
501                         dtls1_stop_timer(s);
502
503                         if (s->hit)
504                                 s->state=SSL3_ST_CW_CHANGE_A;
505                         else
506                                 s->state=SSL_ST_OK;
507                         s->init_num=0;
508                         break;
509
510                 case SSL3_ST_CW_FLUSH:
511                         s->rwstate=SSL_WRITING;
512                         if (BIO_flush(s->wbio) <= 0)
513                                 {
514                                 ret= -1;
515                                 goto end;
516                                 }
517                         s->rwstate=SSL_NOTHING;
518                         s->state=s->s3->tmp.next_state;
519                         break;
520
521                 case SSL_ST_OK:
522                         /* clean a few things up */
523                         ssl3_cleanup_key_block(s);
524
525 #if 0
526                         if (s->init_buf != NULL)
527                                 {
528                                 BUF_MEM_free(s->init_buf);
529                                 s->init_buf=NULL;
530                                 }
531 #endif
532
533                         /* If we are not 'joining' the last two packets,
534                          * remove the buffering now */
535                         if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
536                                 ssl_free_wbio_buffer(s);
537                         /* else do it later in ssl3_write */
538
539                         s->init_num=0;
540                         s->new_session=0;
541
542                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
543                         if (s->hit) s->ctx->stats.sess_hit++;
544
545                         ret=1;
546                         /* s->server=0; */
547                         s->handshake_func=dtls1_connect;
548                         s->ctx->stats.sess_connect_good++;
549
550                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
551
552                         /* done with handshaking */
553                         s->d1->handshake_read_seq  = 0;
554                         s->d1->next_handshake_write_seq = 0;
555                         goto end;
556                         /* break; */
557                         
558                 default:
559                         SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
560                         ret= -1;
561                         goto end;
562                         /* break; */
563                         }
564
565                 /* did we do anything */
566                 if (!s->s3->tmp.reuse_message && !skip)
567                         {
568                         if (s->debug)
569                                 {
570                                 if ((ret=BIO_flush(s->wbio)) <= 0)
571                                         goto end;
572                                 }
573
574                         if ((cb != NULL) && (s->state != state))
575                                 {
576                                 new_state=s->state;
577                                 s->state=state;
578                                 cb(s,SSL_CB_CONNECT_LOOP,1);
579                                 s->state=new_state;
580                                 }
581                         }
582                 skip=0;
583                 }
584 end:
585         s->in_handshake--;
586         if (buf != NULL)
587                 BUF_MEM_free(buf);
588         if (cb != NULL)
589                 cb(s,SSL_CB_CONNECT_EXIT,ret);
590         return(ret);
591         }
592
593 int dtls1_client_hello(SSL *s)
594         {
595         unsigned char *buf;
596         unsigned char *p,*d;
597         unsigned int i,j;
598         unsigned long Time,l;
599         SSL_COMP *comp;
600
601         buf=(unsigned char *)s->init_buf->data;
602         if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
603                 {
604                 SSL_SESSION *sess = s->session;
605                 if ((s->session == NULL) ||
606                         (s->session->ssl_version != s->version) ||
607 #ifdef OPENSSL_NO_TLSEXT
608                         !sess->session_id_length ||
609 #else
610                         (!sess->session_id_length && !sess->tlsext_tick) ||
611 #endif
612                         (s->session->not_resumable))
613                         {
614                         if (!ssl_get_new_session(s,0))
615                                 goto err;
616                         }
617                 /* else use the pre-loaded session */
618
619                 p=s->s3->client_random;
620                 /* if client_random is initialized, reuse it, we are
621                  * required to use same upon reply to HelloVerify */
622                 for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ;
623                 if (i==sizeof(s->s3->client_random))
624                         {
625                         Time=(unsigned long)time(NULL); /* Time */
626                         l2n(Time,p);
627                         RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4);
628                         }
629
630                 /* Do the message type and length last */
631                 d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
632
633                 *(p++)=s->version>>8;
634                 *(p++)=s->version&0xff;
635                 s->client_version=s->version;
636
637                 /* Random stuff */
638                 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
639                 p+=SSL3_RANDOM_SIZE;
640
641                 /* Session ID */
642                 if (s->new_session)
643                         i=0;
644                 else
645                         i=s->session->session_id_length;
646                 *(p++)=i;
647                 if (i != 0)
648                         {
649                         if (i > sizeof s->session->session_id)
650                                 {
651                                 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
652                                 goto err;
653                                 }
654                         memcpy(p,s->session->session_id,i);
655                         p+=i;
656                         }
657                 
658                 /* cookie stuff */
659                 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
660                         {
661                         SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
662                         goto err;
663                         }
664                 *(p++) = s->d1->cookie_len;
665                 memcpy(p, s->d1->cookie, s->d1->cookie_len);
666                 p += s->d1->cookie_len;
667
668                 /* Ciphers supported */
669                 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
670                 if (i == 0)
671                         {
672                         SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
673                         goto err;
674                         }
675                 s2n(i,p);
676                 p+=i;
677
678                 /* COMPRESSION */
679                 if (s->ctx->comp_methods == NULL)
680                         j=0;
681                 else
682                         j=sk_SSL_COMP_num(s->ctx->comp_methods);
683                 *(p++)=1+j;
684                 for (i=0; i<j; i++)
685                         {
686                         comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
687                         *(p++)=comp->id;
688                         }
689                 *(p++)=0; /* Add the NULL method */
690
691 #ifndef OPENSSL_NO_TLSEXT
692                 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
693                         {
694                         SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
695                         goto err;
696                         }
697 #endif          
698
699                 l=(p-d);
700                 d=buf;
701
702                 d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
703
704                 s->state=SSL3_ST_CW_CLNT_HELLO_B;
705                 /* number of bytes to write */
706                 s->init_num=p-buf;
707                 s->init_off=0;
708
709                 /* buffer the message to handle re-xmits */
710                 dtls1_buffer_message(s, 0);
711                 }
712
713         /* SSL3_ST_CW_CLNT_HELLO_B */
714         return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
715 err:
716         return(-1);
717         }
718
719 static int dtls1_get_hello_verify(SSL *s)
720         {
721         int n, al, ok = 0;
722         unsigned char *data;
723         unsigned int cookie_len;
724
725         n=s->method->ssl_get_message(s,
726                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
727                 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
728                 -1,
729                 s->max_cert_list,
730                 &ok);
731
732         if (!ok) return((int)n);
733
734         if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
735                 {
736                 s->d1->send_cookie = 0;
737                 s->s3->tmp.reuse_message=1;
738                 return(1);
739                 }
740
741         data = (unsigned char *)s->init_msg;
742
743         if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff)))
744                 {
745                 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
746                 s->version=(s->version&0xff00)|data[1];
747                 al = SSL_AD_PROTOCOL_VERSION;
748                 goto f_err;
749                 }
750         data+=2;
751
752         cookie_len = *(data++);
753         if ( cookie_len > sizeof(s->d1->cookie))
754                 {
755                 al=SSL_AD_ILLEGAL_PARAMETER;
756                 goto f_err;
757                 }
758
759         memcpy(s->d1->cookie, data, cookie_len);
760         s->d1->cookie_len = cookie_len;
761
762         s->d1->send_cookie = 1;
763         return 1;
764
765 f_err:
766         ssl3_send_alert(s, SSL3_AL_FATAL, al);
767         return -1;
768         }
769
770 int dtls1_send_client_key_exchange(SSL *s)
771         {
772         unsigned char *p,*d;
773         int n;
774         unsigned long l;
775 #ifndef OPENSSL_NO_RSA
776         unsigned char *q;
777         EVP_PKEY *pkey=NULL;
778 #endif
779 #ifndef OPENSSL_NO_KRB5
780         KSSL_ERR kssl_err;
781 #endif /* OPENSSL_NO_KRB5 */
782
783         if (s->state == SSL3_ST_CW_KEY_EXCH_A)
784                 {
785                 d=(unsigned char *)s->init_buf->data;
786                 p= &(d[DTLS1_HM_HEADER_LENGTH]);
787
788                 l=s->s3->tmp.new_cipher->algorithms;
789
790                 /* Fool emacs indentation */
791                 if (0) {}
792 #ifndef OPENSSL_NO_RSA
793                 else if (l & SSL_kRSA)
794                         {
795                         RSA *rsa;
796                         unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
797
798                         if (s->session->sess_cert->peer_rsa_tmp != NULL)
799                                 rsa=s->session->sess_cert->peer_rsa_tmp;
800                         else
801                                 {
802                                 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
803                                 if ((pkey == NULL) ||
804                                         (pkey->type != EVP_PKEY_RSA) ||
805                                         (pkey->pkey.rsa == NULL))
806                                         {
807                                         SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
808                                         goto err;
809                                         }
810                                 rsa=pkey->pkey.rsa;
811                                 EVP_PKEY_free(pkey);
812                                 }
813                                 
814                         tmp_buf[0]=s->client_version>>8;
815                         tmp_buf[1]=s->client_version&0xff;
816                         if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
817                                         goto err;
818
819                         s->session->master_key_length=sizeof tmp_buf;
820
821                         q=p;
822                         /* Fix buf for TLS and [incidentally] DTLS */
823                         if (s->version > SSL3_VERSION)
824                                 p+=2;
825                         n=RSA_public_encrypt(sizeof tmp_buf,
826                                 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
827 #ifdef PKCS1_CHECK
828                         if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
829                         if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
830 #endif
831                         if (n <= 0)
832                                 {
833                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
834                                 goto err;
835                                 }
836
837                         /* Fix buf for TLS and [incidentally] DTLS */
838                         if (s->version > SSL3_VERSION)
839                                 {
840                                 s2n(n,q);
841                                 n+=2;
842                                 }
843
844                         s->session->master_key_length=
845                                 s->method->ssl3_enc->generate_master_secret(s,
846                                         s->session->master_key,
847                                         tmp_buf,sizeof tmp_buf);
848                         OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
849                         }
850 #endif
851 #ifndef OPENSSL_NO_KRB5
852                 else if (l & SSL_kKRB5)
853                         {
854                         krb5_error_code krb5rc;
855                         KSSL_CTX        *kssl_ctx = s->kssl_ctx;
856                         /*  krb5_data   krb5_ap_req;  */
857                         krb5_data       *enc_ticket;
858                         krb5_data       authenticator, *authp = NULL;
859                         EVP_CIPHER_CTX  ciph_ctx;
860                         EVP_CIPHER      *enc = NULL;
861                         unsigned char   iv[EVP_MAX_IV_LENGTH];
862                         unsigned char   tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
863                         unsigned char   epms[SSL_MAX_MASTER_KEY_LENGTH 
864                                                 + EVP_MAX_IV_LENGTH];
865                         int             padl, outl = sizeof(epms);
866
867                         EVP_CIPHER_CTX_init(&ciph_ctx);
868
869 #ifdef KSSL_DEBUG
870                         printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
871                                 l, SSL_kKRB5);
872 #endif  /* KSSL_DEBUG */
873
874                         authp = NULL;
875 #ifdef KRB5SENDAUTH
876                         if (KRB5SENDAUTH)  authp = &authenticator;
877 #endif  /* KRB5SENDAUTH */
878
879                         krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
880                                 &kssl_err);
881                         enc = kssl_map_enc(kssl_ctx->enctype);
882                         if (enc == NULL)
883                             goto err;
884 #ifdef KSSL_DEBUG
885                         {
886                         printf("kssl_cget_tkt rtn %d\n", krb5rc);
887                         if (krb5rc && kssl_err.text)
888                           printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
889                         }
890 #endif  /* KSSL_DEBUG */
891
892                         if (krb5rc)
893                                 {
894                                 ssl3_send_alert(s,SSL3_AL_FATAL,
895                                                 SSL_AD_HANDSHAKE_FAILURE);
896                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
897                                                 kssl_err.reason);
898                                 goto err;
899                                 }
900
901                         /*  20010406 VRS - Earlier versions used KRB5 AP_REQ
902                         **  in place of RFC 2712 KerberosWrapper, as in:
903                         **
904                         **  Send ticket (copy to *p, set n = length)
905                         **  n = krb5_ap_req.length;
906                         **  memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
907                         **  if (krb5_ap_req.data)  
908                         **    kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
909                         **
910                         **  Now using real RFC 2712 KerberosWrapper
911                         **  (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
912                         **  Note: 2712 "opaque" types are here replaced
913                         **  with a 2-byte length followed by the value.
914                         **  Example:
915                         **  KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
916                         **  Where "xx xx" = length bytes.  Shown here with
917                         **  optional authenticator omitted.
918                         */
919
920                         /*  KerberosWrapper.Ticket              */
921                         s2n(enc_ticket->length,p);
922                         memcpy(p, enc_ticket->data, enc_ticket->length);
923                         p+= enc_ticket->length;
924                         n = enc_ticket->length + 2;
925
926                         /*  KerberosWrapper.Authenticator       */
927                         if (authp  &&  authp->length)  
928                                 {
929                                 s2n(authp->length,p);
930                                 memcpy(p, authp->data, authp->length);
931                                 p+= authp->length;
932                                 n+= authp->length + 2;
933                                 
934                                 free(authp->data);
935                                 authp->data = NULL;
936                                 authp->length = 0;
937                                 }
938                         else
939                                 {
940                                 s2n(0,p);/*  null authenticator length  */
941                                 n+=2;
942                                 }
943  
944                         if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
945                             goto err;
946
947                         /*  20010420 VRS.  Tried it this way; failed.
948                         **      EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
949                         **      EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
950                         **                              kssl_ctx->length);
951                         **      EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
952                         */
953
954                         memset(iv, 0, sizeof iv);  /* per RFC 1510 */
955                         EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
956                                 kssl_ctx->key,iv);
957                         EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
958                                 sizeof tmp_buf);
959                         EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
960                         outl += padl;
961                         if (outl > sizeof epms)
962                                 {
963                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
964                                 goto err;
965                                 }
966                         EVP_CIPHER_CTX_cleanup(&ciph_ctx);
967
968                         /*  KerberosWrapper.EncryptedPreMasterSecret    */
969                         s2n(outl,p);
970                         memcpy(p, epms, outl);
971                         p+=outl;
972                         n+=outl + 2;
973
974                         s->session->master_key_length=
975                                 s->method->ssl3_enc->generate_master_secret(s,
976                                         s->session->master_key,
977                                         tmp_buf, sizeof tmp_buf);
978
979                         OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
980                         OPENSSL_cleanse(epms, outl);
981                         }
982 #endif
983 #ifndef OPENSSL_NO_DH
984                 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
985                         {
986                         DH *dh_srvr,*dh_clnt;
987
988                         if (s->session->sess_cert->peer_dh_tmp != NULL)
989                                 dh_srvr=s->session->sess_cert->peer_dh_tmp;
990                         else
991                                 {
992                                 /* we get them from the cert */
993                                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
994                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
995                                 goto err;
996                                 }
997                         
998                         /* generate a new random key */
999                         if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1000                                 {
1001                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1002                                 goto err;
1003                                 }
1004                         if (!DH_generate_key(dh_clnt))
1005                                 {
1006                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1007                                 goto err;
1008                                 }
1009
1010                         /* use the 'p' output buffer for the DH key, but
1011                          * make sure to clear it out afterwards */
1012
1013                         n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1014
1015                         if (n <= 0)
1016                                 {
1017                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1018                                 goto err;
1019                                 }
1020
1021                         /* generate master key from the result */
1022                         s->session->master_key_length=
1023                                 s->method->ssl3_enc->generate_master_secret(s,
1024                                         s->session->master_key,p,n);
1025                         /* clean up */
1026                         memset(p,0,n);
1027
1028                         /* send off the data */
1029                         n=BN_num_bytes(dh_clnt->pub_key);
1030                         s2n(n,p);
1031                         BN_bn2bin(dh_clnt->pub_key,p);
1032                         n+=2;
1033
1034                         DH_free(dh_clnt);
1035
1036                         /* perhaps clean things up a bit EAY EAY EAY EAY*/
1037                         }
1038 #endif
1039                 else
1040                         {
1041                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1042                         SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1043                         goto err;
1044                         }
1045                 
1046                 d = dtls1_set_message_header(s, d,
1047                 SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
1048                 /*
1049                  *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1050                  l2n3(n,d);
1051                  l2n(s->d1->handshake_write_seq,d);
1052                  s->d1->handshake_write_seq++;
1053                 */
1054                 
1055                 s->state=SSL3_ST_CW_KEY_EXCH_B;
1056                 /* number of bytes to write */
1057                 s->init_num=n+DTLS1_HM_HEADER_LENGTH;
1058                 s->init_off=0;
1059
1060                 /* buffer the message to handle re-xmits */
1061                 dtls1_buffer_message(s, 0);
1062                 }
1063         
1064         /* SSL3_ST_CW_KEY_EXCH_B */
1065         return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1066 err:
1067         return(-1);
1068         }
1069
1070 int dtls1_send_client_verify(SSL *s)
1071         {
1072         unsigned char *p,*d;
1073         unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1074         EVP_PKEY *pkey;
1075 #ifndef OPENSSL_NO_RSA
1076         unsigned u=0;
1077 #endif
1078         unsigned long n;
1079 #ifndef OPENSSL_NO_DSA
1080         int j;
1081 #endif
1082
1083         if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1084                 {
1085                 d=(unsigned char *)s->init_buf->data;
1086                 p= &(d[DTLS1_HM_HEADER_LENGTH]);
1087                 pkey=s->cert->key->privatekey;
1088
1089                 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1090                         &(data[MD5_DIGEST_LENGTH]));
1091
1092 #ifndef OPENSSL_NO_RSA
1093                 if (pkey->type == EVP_PKEY_RSA)
1094                         {
1095                         s->method->ssl3_enc->cert_verify_mac(s,
1096                                 &(s->s3->finish_dgst1),&(data[0]));
1097                         if (RSA_sign(NID_md5_sha1, data,
1098                                          MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1099                                         &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1100                                 {
1101                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1102                                 goto err;
1103                                 }
1104                         s2n(u,p);
1105                         n=u+2;
1106                         }
1107                 else
1108 #endif
1109 #ifndef OPENSSL_NO_DSA
1110                         if (pkey->type == EVP_PKEY_DSA)
1111                         {
1112                         if (!DSA_sign(pkey->save_type,
1113                                 &(data[MD5_DIGEST_LENGTH]),
1114                                 SHA_DIGEST_LENGTH,&(p[2]),
1115                                 (unsigned int *)&j,pkey->pkey.dsa))
1116                                 {
1117                                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1118                                 goto err;
1119                                 }
1120                         s2n(j,p);
1121                         n=j+2;
1122                         }
1123                 else
1124 #endif
1125                         {
1126                         SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1127                         goto err;
1128                         }
1129
1130                 d = dtls1_set_message_header(s, d,
1131                         SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ;
1132
1133                 s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH;
1134                 s->init_off=0;
1135
1136                 /* buffer the message to handle re-xmits */
1137                 dtls1_buffer_message(s, 0);
1138
1139                 s->state = SSL3_ST_CW_CERT_VRFY_B;
1140                 }
1141
1142         /* s->state = SSL3_ST_CW_CERT_VRFY_B */
1143         return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1144 err:
1145         return(-1);
1146         }
1147
1148 int dtls1_send_client_certificate(SSL *s)
1149         {
1150         X509 *x509=NULL;
1151         EVP_PKEY *pkey=NULL;
1152         int i;
1153         unsigned long l;
1154
1155         if (s->state == SSL3_ST_CW_CERT_A)
1156                 {
1157                 if ((s->cert == NULL) ||
1158                         (s->cert->key->x509 == NULL) ||
1159                         (s->cert->key->privatekey == NULL))
1160                         s->state=SSL3_ST_CW_CERT_B;
1161                 else
1162                         s->state=SSL3_ST_CW_CERT_C;
1163                 }
1164
1165         /* We need to get a client cert */
1166         if (s->state == SSL3_ST_CW_CERT_B)
1167                 {
1168                 /* If we get an error, we need to
1169                  * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1170                  * We then get retied later */
1171                 i=0;
1172                 i = ssl_do_client_cert_cb(s, &x509, &pkey);
1173                 if (i < 0)
1174                         {
1175                         s->rwstate=SSL_X509_LOOKUP;
1176                         return(-1);
1177                         }
1178                 s->rwstate=SSL_NOTHING;
1179                 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1180                         {
1181                         s->state=SSL3_ST_CW_CERT_B;
1182                         if (    !SSL_use_certificate(s,x509) ||
1183                                 !SSL_use_PrivateKey(s,pkey))
1184                                 i=0;
1185                         }
1186                 else if (i == 1)
1187                         {
1188                         i=0;
1189                         SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1190                         }
1191
1192                 if (x509 != NULL) X509_free(x509);
1193                 if (pkey != NULL) EVP_PKEY_free(pkey);
1194                 if (i == 0)
1195                         {
1196                         if (s->version == SSL3_VERSION)
1197                                 {
1198                                 s->s3->tmp.cert_req=0;
1199                                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1200                                 return(1);
1201                                 }
1202                         else
1203                                 {
1204                                 s->s3->tmp.cert_req=2;
1205                                 }
1206                         }
1207
1208                 /* Ok, we have a cert */
1209                 s->state=SSL3_ST_CW_CERT_C;
1210                 }
1211
1212         if (s->state == SSL3_ST_CW_CERT_C)
1213                 {
1214                 s->state=SSL3_ST_CW_CERT_D;
1215                 l=dtls1_output_cert_chain(s,
1216                         (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1217                 s->init_num=(int)l;
1218                 s->init_off=0;
1219
1220                 /* set header called by dtls1_output_cert_chain() */
1221
1222                 /* buffer the message to handle re-xmits */
1223                 dtls1_buffer_message(s, 0);
1224                 }
1225         /* SSL3_ST_CW_CERT_D */
1226         return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1227         }
1228
1229