2 * DTLS implementation written by Nagendra Modadugu
3 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 /* ====================================================================
6 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
59 * All rights reserved.
61 * This package is an SSL implementation written
62 * by Eric Young (eay@cryptsoft.com).
63 * The implementation was written so as to conform with Netscapes SSL.
65 * This library is free for commercial and non-commercial use as long as
66 * the following conditions are aheared to. The following conditions
67 * apply to all code found in this distribution, be it the RC4, RSA,
68 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
69 * included with this distribution is covered by the same copyright terms
70 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72 * Copyright remains Eric Young's, and as such any Copyright notices in
73 * the code are not to be removed.
74 * If this package is used in a product, Eric Young should be given attribution
75 * as the author of the parts of the library used.
76 * This can be in the form of a textual message at program startup or
77 * in documentation (online or textual) provided with the package.
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
82 * 1. Redistributions of source code must retain the copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * "This product includes cryptographic software written by
90 * Eric Young (eay@cryptsoft.com)"
91 * The word 'cryptographic' can be left out if the rouines from the library
92 * being used are not cryptographic related :-).
93 * 4. If you include any Windows specific code (or a derivative thereof) from
94 * the apps directory (application code) you must include an acknowledgement:
95 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109 * The licence and distribution terms for any publically available version or
110 * derivative of this code cannot be changed. i.e. this code cannot simply be
111 * copied and put under another distribution licence
112 * [including the GNU Public Licence.]
117 #include <openssl/bn.h>
118 #include <openssl/buf.h>
119 #include <openssl/dh.h>
120 #include <openssl/evp.h>
121 #include <openssl/md5.h>
122 #include <openssl/mem.h>
123 #include <openssl/obj.h>
124 #include <openssl/rand.h>
126 #include "ssl_locl.h"
128 static const SSL_METHOD *dtls1_get_client_method(int ver);
129 static int dtls1_get_hello_verify(SSL *s);
131 static const SSL_METHOD *dtls1_get_client_method(int ver)
133 if (ver == DTLS1_VERSION)
134 return(DTLSv1_client_method());
135 else if (ver == DTLS1_2_VERSION)
136 return(DTLSv1_2_client_method());
141 IMPLEMENT_dtls1_meth_func(DTLS1_VERSION,
142 DTLSv1_client_method,
143 ssl_undefined_function,
145 dtls1_get_client_method,
148 IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION,
149 DTLSv1_2_client_method,
150 ssl_undefined_function,
152 dtls1_get_client_method,
155 IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION,
157 ssl_undefined_function,
159 dtls1_get_client_method,
162 int dtls1_connect(SSL *s)
165 void (*cb)(const SSL *ssl,int type,int val)=NULL;
167 int new_state,state,skip=0;
170 ERR_clear_system_error();
172 if (s->info_callback != NULL)
174 else if (s->ctx->info_callback != NULL)
175 cb=s->ctx->info_callback;
178 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
186 case SSL_ST_RENEGOTIATE:
188 s->state=SSL_ST_CONNECT;
189 s->ctx->stats.sess_connect_renegotiate++;
193 case SSL_ST_BEFORE|SSL_ST_CONNECT:
194 case SSL_ST_OK|SSL_ST_CONNECT:
197 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
199 if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00))
201 OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR);
206 /* s->version=SSL3_VERSION; */
207 s->type=SSL_ST_CONNECT;
209 if (s->init_buf == NULL)
211 if ((buf=BUF_MEM_new()) == NULL)
216 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
225 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
227 /* setup buffing BIO */
228 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
230 /* don't push the buffering BIO quite yet */
232 s->state=SSL3_ST_CW_CLNT_HELLO_A;
233 s->ctx->stats.sess_connect++;
235 s->d1->send_cookie = 0;
239 case SSL3_ST_CW_CLNT_HELLO_A:
240 case SSL3_ST_CW_CLNT_HELLO_B:
244 /* every DTLS ClientHello resets Finished MAC */
245 ssl3_init_finished_mac(s);
247 dtls1_start_timer(s);
248 ret=ssl3_send_client_hello(s);
249 if (ret <= 0) goto end;
251 if ( s->d1->send_cookie)
253 s->state=SSL3_ST_CW_FLUSH;
254 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
257 s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
260 /* turn on buffering for the next lot of output */
261 if (s->bbio != s->wbio)
262 s->wbio=BIO_push(s->bbio,s->wbio);
266 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
267 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
269 ret = dtls1_get_hello_verify(s);
272 if ( s->d1->send_cookie)
274 /* start again, with a cookie */
276 s->state = SSL3_ST_CW_CLNT_HELLO_A;
280 s->state = SSL3_ST_CR_SRVR_HELLO_A;
285 case SSL3_ST_CR_SRVR_HELLO_A:
286 case SSL3_ST_CR_SRVR_HELLO_B:
287 ret=ssl3_get_server_hello(s);
288 if (ret <= 0) goto end;
292 s->state=SSL3_ST_CR_FINISHED_A;
293 if (s->tlsext_ticket_expected)
295 /* receive renewed session ticket */
296 s->state=SSL3_ST_CR_SESSION_TICKET_A;
301 s->state=SSL3_ST_CR_CERT_A;
306 case SSL3_ST_CR_CERT_A:
307 case SSL3_ST_CR_CERT_B:
308 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
310 ret=ssl3_get_server_certificate(s);
311 if (ret <= 0) goto end;
312 if (s->s3->tmp.certificate_status_expected)
313 s->state=SSL3_ST_CR_CERT_STATUS_A;
315 s->state=SSL3_ST_CR_KEY_EXCH_A;
320 s->state=SSL3_ST_CR_KEY_EXCH_A;
325 case SSL3_ST_CR_KEY_EXCH_A:
326 case SSL3_ST_CR_KEY_EXCH_B:
327 ret=ssl3_get_server_key_exchange(s);
328 if (ret <= 0) goto end;
329 s->state=SSL3_ST_CR_CERT_REQ_A;
332 /* at this point we check that we have the
333 * required stuff from the server */
334 if (!ssl3_check_cert_and_algorithm(s))
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;
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;
354 if (s->s3->tmp.cert_req)
355 s->s3->tmp.next_state=SSL3_ST_CW_CERT_A;
357 s->s3->tmp.next_state=SSL3_ST_CW_KEY_EXCH_A;
359 s->state=s->s3->tmp.next_state;
362 case SSL3_ST_CW_CERT_A:
363 case SSL3_ST_CW_CERT_B:
364 case SSL3_ST_CW_CERT_C:
365 case SSL3_ST_CW_CERT_D:
366 dtls1_start_timer(s);
367 ret=ssl3_send_client_certificate(s);
368 if (ret <= 0) goto end;
369 s->state=SSL3_ST_CW_KEY_EXCH_A;
373 case SSL3_ST_CW_KEY_EXCH_A:
374 case SSL3_ST_CW_KEY_EXCH_B:
375 dtls1_start_timer(s);
376 ret=ssl3_send_client_key_exchange(s);
377 if (ret <= 0) goto end;
378 /* For TLS, cert_req is set to 2, so a cert chain
379 * of nothing is sent, but no verify packet is sent */
380 if (s->s3->tmp.cert_req == 1)
382 s->state=SSL3_ST_CW_CERT_VRFY_A;
386 s->state=SSL3_ST_CW_CHANGE_A;
387 s->s3->change_cipher_spec=0;
393 case SSL3_ST_CW_CERT_VRFY_A:
394 case SSL3_ST_CW_CERT_VRFY_B:
395 dtls1_start_timer(s);
396 ret=ssl3_send_cert_verify(s);
397 if (ret <= 0) goto end;
398 s->state=SSL3_ST_CW_CHANGE_A;
400 s->s3->change_cipher_spec=0;
403 case SSL3_ST_CW_CHANGE_A:
404 case SSL3_ST_CW_CHANGE_B:
406 dtls1_start_timer(s);
407 ret=dtls1_send_change_cipher_spec(s,
408 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
409 if (ret <= 0) goto end;
411 s->state=SSL3_ST_CW_FINISHED_A;
414 s->session->cipher=s->s3->tmp.new_cipher;
415 if (!s->method->ssl3_enc->setup_key_block(s))
421 if (!s->method->ssl3_enc->change_cipher_state(s,
422 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
428 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
431 case SSL3_ST_CW_FINISHED_A:
432 case SSL3_ST_CW_FINISHED_B:
434 dtls1_start_timer(s);
435 ret=ssl3_send_finished(s,
436 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
437 s->method->ssl3_enc->client_finished_label,
438 s->method->ssl3_enc->client_finished_label_len);
439 if (ret <= 0) goto end;
440 s->state=SSL3_ST_CW_FLUSH;
443 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
446 s->s3->tmp.next_state=SSL_ST_OK;
451 /* Allow NewSessionTicket if ticket expected */
452 if (s->tlsext_ticket_expected)
453 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
456 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
461 case SSL3_ST_CR_SESSION_TICKET_A:
462 case SSL3_ST_CR_SESSION_TICKET_B:
463 ret=ssl3_get_new_session_ticket(s);
464 if (ret <= 0) goto end;
465 s->state=SSL3_ST_CR_FINISHED_A;
469 case SSL3_ST_CR_CERT_STATUS_A:
470 case SSL3_ST_CR_CERT_STATUS_B:
471 ret=ssl3_get_cert_status(s);
472 if (ret <= 0) goto end;
473 s->state=SSL3_ST_CR_KEY_EXCH_A;
477 case SSL3_ST_CR_FINISHED_A:
478 case SSL3_ST_CR_FINISHED_B:
479 s->d1->change_cipher_spec_ok = 1;
480 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
481 SSL3_ST_CR_FINISHED_B);
482 if (ret <= 0) goto end;
486 s->state=SSL3_ST_CW_CHANGE_A;
493 case SSL3_ST_CW_FLUSH:
494 s->rwstate=SSL_WRITING;
495 if (BIO_flush(s->wbio) <= 0)
497 /* If the write error was fatal, stop trying */
498 if (!BIO_should_retry(s->wbio))
500 s->rwstate=SSL_NOTHING;
501 s->state=s->s3->tmp.next_state;
507 s->rwstate=SSL_NOTHING;
508 s->state=s->s3->tmp.next_state;
512 /* clean a few things up */
513 ssl3_cleanup_key_block(s);
516 if (s->init_buf != NULL)
518 BUF_MEM_free(s->init_buf);
523 /* If we are not 'joining' the last two packets,
524 * remove the buffering now */
525 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
526 ssl_free_wbio_buffer(s);
527 /* else do it later in ssl3_write */
533 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
534 if (s->hit) s->ctx->stats.sess_hit++;
538 s->handshake_func=dtls1_connect;
539 s->ctx->stats.sess_connect_good++;
541 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
543 /* done with handshaking */
544 s->d1->handshake_read_seq = 0;
545 s->d1->next_handshake_write_seq = 0;
550 OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE);
556 /* did we do anything */
557 if (!s->s3->tmp.reuse_message && !skip)
561 if ((ret=BIO_flush(s->wbio)) <= 0)
565 if ((cb != NULL) && (s->state != state))
569 cb(s,SSL_CB_CONNECT_LOOP,1);
581 cb(s,SSL_CB_CONNECT_EXIT,ret);
585 static int dtls1_get_hello_verify(SSL *s)
589 CBS hello_verify_request, cookie;
590 uint16_t server_version;
593 n=s->method->ssl_get_message(s,
594 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
595 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
597 /* Use the same maximum size as ssl3_get_server_hello. */
599 SSL_GET_MESSAGE_HASH_MESSAGE,
603 if (!ok) return((int)n);
605 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
607 s->d1->send_cookie = 0;
608 s->s3->tmp.reuse_message = 1;
612 CBS_init(&hello_verify_request, s->init_msg, n);
614 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
615 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
616 CBS_len(&hello_verify_request) != 0)
618 al = SSL_AD_DECODE_ERROR;
619 OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
623 if (CBS_len(&cookie) > sizeof(s->d1->cookie))
625 al=SSL_AD_ILLEGAL_PARAMETER;
629 memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
630 s->d1->cookie_len = CBS_len(&cookie);
632 s->d1->send_cookie = 1;
636 ssl3_send_alert(s, SSL3_AL_FATAL, al);