Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / ssl / d1_clnt.c
1 /* $OpenBSD: d1_clnt.c,v 1.55 2015/09/12 16:10:07 doug Exp $ */
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 <limits.h>
117 #include <stdio.h>
118
119 #include "ssl_locl.h"
120
121 #include <openssl/bn.h>
122 #include <openssl/buffer.h>
123 #include <openssl/dh.h>
124 #include <openssl/evp.h>
125 #include <openssl/md5.h>
126 #include <openssl/objects.h>
127
128 #include "bytestring.h"
129
130 static const SSL_METHOD *dtls1_get_client_method(int ver);
131 static int dtls1_get_hello_verify(SSL *s);
132
133 const SSL_METHOD DTLSv1_client_method_data = {
134         .version = DTLS1_VERSION,
135         .ssl_new = dtls1_new,
136         .ssl_clear = dtls1_clear,
137         .ssl_free = dtls1_free,
138         .ssl_accept = ssl_undefined_function,
139         .ssl_connect = dtls1_connect,
140         .ssl_read = ssl3_read,
141         .ssl_peek = ssl3_peek,
142         .ssl_write = ssl3_write,
143         .ssl_shutdown = dtls1_shutdown,
144         .ssl_renegotiate = ssl3_renegotiate,
145         .ssl_renegotiate_check = ssl3_renegotiate_check,
146         .ssl_get_message = dtls1_get_message,
147         .ssl_read_bytes = dtls1_read_bytes,
148         .ssl_write_bytes = dtls1_write_app_data_bytes,
149         .ssl_dispatch_alert = dtls1_dispatch_alert,
150         .ssl_ctrl = dtls1_ctrl,
151         .ssl_ctx_ctrl = ssl3_ctx_ctrl,
152         .get_cipher_by_char = ssl3_get_cipher_by_char,
153         .put_cipher_by_char = ssl3_put_cipher_by_char,
154         .ssl_pending = ssl3_pending,
155         .num_ciphers = ssl3_num_ciphers,
156         .get_cipher = dtls1_get_cipher,
157         .get_ssl_method = dtls1_get_client_method,
158         .get_timeout = dtls1_default_timeout,
159         .ssl3_enc = &DTLSv1_enc_data,
160         .ssl_version = ssl_undefined_void_function,
161         .ssl_callback_ctrl = ssl3_callback_ctrl,
162         .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl,
163 };
164
165 const SSL_METHOD *
166 DTLSv1_client_method(void)
167 {
168         return &DTLSv1_client_method_data;
169 }
170
171 static const SSL_METHOD *
172 dtls1_get_client_method(int ver)
173 {
174         if (ver == DTLS1_VERSION)
175                 return (DTLSv1_client_method());
176         return (NULL);
177 }
178
179 int
180 dtls1_connect(SSL *s)
181 {
182         void (*cb)(const SSL *ssl, int type, int val) = NULL;
183         int ret = -1;
184         int new_state, state, skip = 0;
185
186         ERR_clear_error();
187         errno = 0;
188
189         if (s->info_callback != NULL)
190                 cb = s->info_callback;
191         else if (s->ctx->info_callback != NULL)
192                 cb = s->ctx->info_callback;
193
194         s->in_handshake++;
195         if (!SSL_in_init(s) || SSL_in_before(s))
196                 SSL_clear(s);
197
198
199         for (;;) {
200                 state = s->state;
201
202                 switch (s->state) {
203                 case SSL_ST_RENEGOTIATE:
204                         s->renegotiate = 1;
205                         s->state = SSL_ST_CONNECT;
206                         s->ctx->stats.sess_connect_renegotiate++;
207                         /* break */
208                 case SSL_ST_BEFORE:
209                 case SSL_ST_CONNECT:
210                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
211                 case SSL_ST_OK|SSL_ST_CONNECT:
212
213                         s->server = 0;
214                         if (cb != NULL)
215                                 cb(s, SSL_CB_HANDSHAKE_START, 1);
216
217                         if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00)) {
218                                 SSLerr(SSL_F_DTLS1_CONNECT,
219                                     ERR_R_INTERNAL_ERROR);
220                                 ret = -1;
221                                 goto end;
222                         }
223
224                         /* s->version=SSL3_VERSION; */
225                         s->type = SSL_ST_CONNECT;
226
227                         if (!ssl3_setup_init_buffer(s)) {
228                                 ret = -1;
229                                 goto end;
230                         }
231                         if (!ssl3_setup_buffers(s)) {
232                                 ret = -1;
233                                 goto end;
234                         }
235                         if (!ssl_init_wbio_buffer(s, 0)) {
236                                 ret = -1;
237                                 goto end;
238                         }
239
240                         /* don't push the buffering BIO quite yet */
241
242                         s->state = SSL3_ST_CW_CLNT_HELLO_A;
243                         s->ctx->stats.sess_connect++;
244                         s->init_num = 0;
245                         /* mark client_random uninitialized */
246                         memset(s->s3->client_random, 0,
247                             sizeof(s->s3->client_random));
248                         s->d1->send_cookie = 0;
249                         s->hit = 0;
250                         break;
251
252
253                 case SSL3_ST_CW_CLNT_HELLO_A:
254                 case SSL3_ST_CW_CLNT_HELLO_B:
255
256                         s->shutdown = 0;
257
258                         /* every DTLS ClientHello resets Finished MAC */
259                         if (!tls1_init_finished_mac(s)) {
260                                 ret = -1;
261                                 goto end;
262                         }
263
264                         dtls1_start_timer(s);
265                         ret = ssl3_client_hello(s);
266                         if (ret <= 0)
267                                 goto end;
268
269                         if (s->d1->send_cookie) {
270                                 s->state = SSL3_ST_CW_FLUSH;
271                                 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
272                         } else
273                                 s->state = SSL3_ST_CR_SRVR_HELLO_A;
274
275                         s->init_num = 0;
276
277                         /* turn on buffering for the next lot of output */
278                         if (s->bbio != s->wbio)
279                                 s->wbio = BIO_push(s->bbio, s->wbio);
280
281                         break;
282
283                 case SSL3_ST_CR_SRVR_HELLO_A:
284                 case SSL3_ST_CR_SRVR_HELLO_B:
285                         ret = ssl3_get_server_hello(s);
286                         if (ret <= 0)
287                                 goto end;
288                         else {
289                                 if (s->hit) {
290
291                                         s->state = SSL3_ST_CR_FINISHED_A;
292                                 } else
293                                         s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
294                         }
295                         s->init_num = 0;
296                         break;
297
298                 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
299                 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
300
301                         ret = dtls1_get_hello_verify(s);
302                         if (ret <= 0)
303                                 goto end;
304                         dtls1_stop_timer(s);
305                         if ( s->d1->send_cookie) /* start again, with a cookie */
306                                 s->state = SSL3_ST_CW_CLNT_HELLO_A;
307                         else
308                                 s->state = SSL3_ST_CR_CERT_A;
309                         s->init_num = 0;
310                         break;
311
312                 case SSL3_ST_CR_CERT_A:
313                 case SSL3_ST_CR_CERT_B:
314                         ret = ssl3_check_finished(s);
315                         if (ret <= 0)
316                                 goto end;
317                         if (ret == 2) {
318                                 s->hit = 1;
319                                 if (s->tlsext_ticket_expected)
320                                         s->state = SSL3_ST_CR_SESSION_TICKET_A;
321                                 else
322                                         s->state = SSL3_ST_CR_FINISHED_A;
323                                 s->init_num = 0;
324                                 break;
325                         }
326                         /* Check if it is anon DH. */
327                         if (!(s->s3->tmp.new_cipher->algorithm_auth &
328                             SSL_aNULL)) {
329                                 ret = ssl3_get_server_certificate(s);
330                                 if (ret <= 0)
331                                         goto end;
332                                 if (s->tlsext_status_expected)
333                                         s->state = SSL3_ST_CR_CERT_STATUS_A;
334                                 else
335                                         s->state = SSL3_ST_CR_KEY_EXCH_A;
336                         } else {
337                                 skip = 1;
338                                 s->state = SSL3_ST_CR_KEY_EXCH_A;
339                         }
340                         s->init_num = 0;
341                         break;
342
343                 case SSL3_ST_CR_KEY_EXCH_A:
344                 case SSL3_ST_CR_KEY_EXCH_B:
345                         ret = ssl3_get_key_exchange(s);
346                         if (ret <= 0)
347                                 goto end;
348                         s->state = SSL3_ST_CR_CERT_REQ_A;
349                         s->init_num = 0;
350
351                         /* at this point we check that we have the
352                          * required stuff from the server */
353                         if (!ssl3_check_cert_and_algorithm(s)) {
354                                 ret = -1;
355                                 goto end;
356                         }
357                         break;
358
359                 case SSL3_ST_CR_CERT_REQ_A:
360                 case SSL3_ST_CR_CERT_REQ_B:
361                         ret = ssl3_get_certificate_request(s);
362                         if (ret <= 0)
363                                 goto end;
364                         s->state = SSL3_ST_CR_SRVR_DONE_A;
365                         s->init_num = 0;
366                         break;
367
368                 case SSL3_ST_CR_SRVR_DONE_A:
369                 case SSL3_ST_CR_SRVR_DONE_B:
370                         ret = ssl3_get_server_done(s);
371                         if (ret <= 0)
372                                 goto end;
373                         dtls1_stop_timer(s);
374                         if (s->s3->tmp.cert_req)
375                                 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
376                         else
377                                 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
378                         s->init_num = 0;
379                         s->state = s->s3->tmp.next_state;
380                         break;
381
382                 case SSL3_ST_CW_CERT_A:
383                 case SSL3_ST_CW_CERT_B:
384                 case SSL3_ST_CW_CERT_C:
385                 case SSL3_ST_CW_CERT_D:
386                         dtls1_start_timer(s);
387                         ret = dtls1_send_client_certificate(s);
388                         if (ret <= 0)
389                                 goto end;
390                         s->state = SSL3_ST_CW_KEY_EXCH_A;
391                         s->init_num = 0;
392                         break;
393
394                 case SSL3_ST_CW_KEY_EXCH_A:
395                 case SSL3_ST_CW_KEY_EXCH_B:
396                         dtls1_start_timer(s);
397                         ret = ssl3_send_client_key_exchange(s);
398                         if (ret <= 0)
399                                 goto end;
400
401                         /* EAY EAY EAY need to check for DH fix cert
402                          * sent back */
403                         /* For TLS, cert_req is set to 2, so a cert chain
404                          * of nothing is sent, but no verify packet is sent */
405                         if (s->s3->tmp.cert_req == 1) {
406                                 s->state = SSL3_ST_CW_CERT_VRFY_A;
407                         } else {
408                                 s->state = SSL3_ST_CW_CHANGE_A;
409                                 s->s3->change_cipher_spec = 0;
410                         }
411
412                         s->init_num = 0;
413                         break;
414
415                 case SSL3_ST_CW_CERT_VRFY_A:
416                 case SSL3_ST_CW_CERT_VRFY_B:
417                         dtls1_start_timer(s);
418                         ret = ssl3_send_client_verify(s);
419                         if (ret <= 0)
420                                 goto end;
421                         s->state = SSL3_ST_CW_CHANGE_A;
422                         s->init_num = 0;
423                         s->s3->change_cipher_spec = 0;
424                         break;
425
426                 case SSL3_ST_CW_CHANGE_A:
427                 case SSL3_ST_CW_CHANGE_B:
428                         if (!s->hit)
429                                 dtls1_start_timer(s);
430                         ret = dtls1_send_change_cipher_spec(s,
431                             SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B);
432                         if (ret <= 0)
433                                 goto end;
434
435                         s->state = SSL3_ST_CW_FINISHED_A;
436                         s->init_num = 0;
437
438                         s->session->cipher = s->s3->tmp.new_cipher;
439                         if (!s->method->ssl3_enc->setup_key_block(s)) {
440                                 ret = -1;
441                                 goto end;
442                         }
443
444                         if (!s->method->ssl3_enc->change_cipher_state(s,
445                             SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
446                                 ret = -1;
447                                 goto end;
448                         }
449
450
451                         dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
452                         break;
453
454                 case SSL3_ST_CW_FINISHED_A:
455                 case SSL3_ST_CW_FINISHED_B:
456                         if (!s->hit)
457                                 dtls1_start_timer(s);
458                         ret = ssl3_send_finished(s,
459                             SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
460                             s->method->ssl3_enc->client_finished_label,
461                             s->method->ssl3_enc->client_finished_label_len);
462                         if (ret <= 0)
463                                 goto end;
464                         s->state = SSL3_ST_CW_FLUSH;
465
466                         /* clear flags */
467                         s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
468                         if (s->hit) {
469                                 s->s3->tmp.next_state = SSL_ST_OK;
470                                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
471                                         s->state = SSL_ST_OK;
472                                         s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
473                                         s->s3->delay_buf_pop_ret = 0;
474                                 }
475                         } else {
476
477                                 /* Allow NewSessionTicket if ticket expected */
478                                 if (s->tlsext_ticket_expected)
479                                         s->s3->tmp.next_state =
480                                             SSL3_ST_CR_SESSION_TICKET_A;
481                                 else
482                                         s->s3->tmp.next_state =
483                                             SSL3_ST_CR_FINISHED_A;
484                         }
485                         s->init_num = 0;
486                         break;
487
488                 case SSL3_ST_CR_SESSION_TICKET_A:
489                 case SSL3_ST_CR_SESSION_TICKET_B:
490                         ret = ssl3_get_new_session_ticket(s);
491                         if (ret <= 0)
492                                 goto end;
493                         s->state = SSL3_ST_CR_FINISHED_A;
494                         s->init_num = 0;
495                         break;
496
497                 case SSL3_ST_CR_CERT_STATUS_A:
498                 case SSL3_ST_CR_CERT_STATUS_B:
499                         ret = ssl3_get_cert_status(s);
500                         if (ret <= 0)
501                                 goto end;
502                         s->state = SSL3_ST_CR_KEY_EXCH_A;
503                         s->init_num = 0;
504                         break;
505
506                 case SSL3_ST_CR_FINISHED_A:
507                 case SSL3_ST_CR_FINISHED_B:
508                         s->d1->change_cipher_spec_ok = 1;
509                         ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
510                             SSL3_ST_CR_FINISHED_B);
511                         if (ret <= 0)
512                                 goto end;
513                         dtls1_stop_timer(s);
514
515                         if (s->hit)
516                                 s->state = SSL3_ST_CW_CHANGE_A;
517                         else
518                                 s->state = SSL_ST_OK;
519
520
521                         s->init_num = 0;
522                         break;
523
524                 case SSL3_ST_CW_FLUSH:
525                         s->rwstate = SSL_WRITING;
526                         if (BIO_flush(s->wbio) <= 0) {
527                                 /* If the write error was fatal, stop trying */
528                                 if (!BIO_should_retry(s->wbio)) {
529                                         s->rwstate = SSL_NOTHING;
530                                         s->state = s->s3->tmp.next_state;
531                                 }
532
533                                 ret = -1;
534                                 goto end;
535                         }
536                         s->rwstate = SSL_NOTHING;
537                         s->state = s->s3->tmp.next_state;
538                         break;
539
540                 case SSL_ST_OK:
541                         /* clean a few things up */
542                         tls1_cleanup_key_block(s);
543
544                         /* If we are not 'joining' the last two packets,
545                          * remove the buffering now */
546                         if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
547                                 ssl_free_wbio_buffer(s);
548                         /* else do it later in ssl3_write */
549
550                         s->init_num = 0;
551                         s->renegotiate = 0;
552                         s->new_session = 0;
553
554                         ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
555                         if (s->hit)
556                                 s->ctx->stats.sess_hit++;
557
558                         ret = 1;
559                         /* s->server=0; */
560                         s->handshake_func = dtls1_connect;
561                         s->ctx->stats.sess_connect_good++;
562
563                         if (cb != NULL)
564                                 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
565
566                         /* done with handshaking */
567                         s->d1->handshake_read_seq = 0;
568                         s->d1->next_handshake_write_seq = 0;
569                         goto end;
570                         /* break; */
571
572                 default:
573                         SSLerr(SSL_F_DTLS1_CONNECT, SSL_R_UNKNOWN_STATE);
574                         ret = -1;
575                         goto end;
576                         /* break; */
577                 }
578
579                 /* did we do anything */
580                 if (!s->s3->tmp.reuse_message && !skip) {
581                         if (s->debug) {
582                                 if ((ret = BIO_flush(s->wbio)) <= 0)
583                                         goto end;
584                         }
585
586                         if ((cb != NULL) && (s->state != state)) {
587                                 new_state = s->state;
588                                 s->state = state;
589                                 cb(s, SSL_CB_CONNECT_LOOP, 1);
590                                 s->state = new_state;
591                         }
592                 }
593                 skip = 0;
594         }
595
596 end:
597         s->in_handshake--;
598         if (cb != NULL)
599                 cb(s, SSL_CB_CONNECT_EXIT, ret);
600
601         return (ret);
602 }
603
604 static int
605 dtls1_get_hello_verify(SSL *s)
606 {
607         long n;
608         int al, ok = 0;
609         size_t cookie_len;
610         uint16_t ssl_version;
611         CBS hello_verify_request, cookie;
612
613         n = s->method->ssl_get_message(s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
614             DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, -1, s->max_cert_list, &ok);
615
616         if (!ok)
617                 return ((int)n);
618
619         if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
620                 s->d1->send_cookie = 0;
621                 s->s3->tmp.reuse_message = 1;
622                 return (1);
623         }
624
625         if (n < 0)
626                 goto truncated;
627
628         CBS_init(&hello_verify_request, s->init_msg, n);
629
630         if (!CBS_get_u16(&hello_verify_request, &ssl_version))
631                 goto truncated;
632
633         if (ssl_version != s->version) {
634                 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY, SSL_R_WRONG_SSL_VERSION);
635                 s->version = (s->version & 0xff00) | (ssl_version & 0xff);
636                 al = SSL_AD_PROTOCOL_VERSION;
637                 goto f_err;
638         }
639
640         if (!CBS_get_u8_length_prefixed(&hello_verify_request, &cookie))
641                 goto truncated;
642
643         if (!CBS_write_bytes(&cookie, s->d1->cookie,
644             sizeof(s->d1->cookie), &cookie_len)) {
645                 s->d1->cookie_len = 0;
646                 al = SSL_AD_ILLEGAL_PARAMETER;
647                 goto f_err;
648         }
649         s->d1->cookie_len = cookie_len;
650         s->d1->send_cookie = 1;
651
652         return 1;
653
654 truncated:
655         al = SSL_AD_DECODE_ERROR;
656 f_err:
657         ssl3_send_alert(s, SSL3_AL_FATAL, al);
658         return -1;
659 }
660
661 int
662 dtls1_send_client_certificate(SSL *s)
663 {
664         X509 *x509 = NULL;
665         EVP_PKEY *pkey = NULL;
666         int i;
667         unsigned long l;
668
669         if (s->state == SSL3_ST_CW_CERT_A) {
670                 if ((s->cert == NULL) || (s->cert->key->x509 == NULL) ||
671                     (s->cert->key->privatekey == NULL))
672                         s->state = SSL3_ST_CW_CERT_B;
673                 else
674                         s->state = SSL3_ST_CW_CERT_C;
675         }
676
677         /* We need to get a client cert */
678         if (s->state == SSL3_ST_CW_CERT_B) {
679                 /* If we get an error, we need to
680                  * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
681                  * We then get retied later */
682                 i = 0;
683                 i = ssl_do_client_cert_cb(s, &x509, &pkey);
684                 if (i < 0) {
685                         s->rwstate = SSL_X509_LOOKUP;
686                         return (-1);
687                 }
688                 s->rwstate = SSL_NOTHING;
689                 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
690                         s->state = SSL3_ST_CW_CERT_B;
691                         if (!SSL_use_certificate(s, x509) ||
692                             !SSL_use_PrivateKey(s, pkey))
693                                 i = 0;
694                 } else if (i == 1) {
695                         i = 0;
696                         SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,
697                             SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
698                 }
699
700                 X509_free(x509);
701                 EVP_PKEY_free(pkey);
702                 if (i == 0)
703                         s->s3->tmp.cert_req = 2;
704
705                 /* Ok, we have a cert */
706                 s->state = SSL3_ST_CW_CERT_C;
707         }
708
709         if (s->state == SSL3_ST_CW_CERT_C) {
710                 s->state = SSL3_ST_CW_CERT_D;
711                 l = dtls1_output_cert_chain(s,
712                     (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key->x509);
713                 s->init_num = (int)l;
714                 s->init_off = 0;
715
716                 /* set header called by dtls1_output_cert_chain() */
717
718                 /* buffer the message to handle re-xmits */
719                 dtls1_buffer_message(s, 0);
720         }
721
722         /* SSL3_ST_CW_CERT_D */
723         return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
724 }