You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

363 lines
10 KiB

  1. /*
  2. * DTLS implementation written by Nagendra Modadugu
  3. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  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
  18. * distribution.
  19. *
  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/)"
  24. *
  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.
  29. *
  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.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  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. * ====================================================================
  52. *
  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). */
  56. #include <openssl/base.h>
  57. #include <limits.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #if defined(OPENSSL_WINDOWS)
  61. #include <sys/timeb.h>
  62. #else
  63. #include <sys/socket.h>
  64. #include <sys/time.h>
  65. #endif
  66. #include <openssl/err.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/obj.h>
  69. #include "internal.h"
  70. /* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
  71. * before starting to decrease the MTU. */
  72. #define DTLS1_MTU_TIMEOUTS 2
  73. /* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
  74. * before failing the DTLS handshake. */
  75. #define DTLS1_MAX_TIMEOUTS 12
  76. static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock);
  77. static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft);
  78. int dtls1_new(SSL *s) {
  79. DTLS1_STATE *d1;
  80. if (!ssl3_new(s)) {
  81. return 0;
  82. }
  83. d1 = OPENSSL_malloc(sizeof *d1);
  84. if (d1 == NULL) {
  85. ssl3_free(s);
  86. return 0;
  87. }
  88. memset(d1, 0, sizeof *d1);
  89. d1->buffered_messages = pqueue_new();
  90. d1->sent_messages = pqueue_new();
  91. if (!d1->buffered_messages || !d1->sent_messages) {
  92. if (d1->buffered_messages) {
  93. pqueue_free(d1->buffered_messages);
  94. }
  95. if (d1->sent_messages) {
  96. pqueue_free(d1->sent_messages);
  97. }
  98. OPENSSL_free(d1);
  99. ssl3_free(s);
  100. return 0;
  101. }
  102. s->d1 = d1;
  103. /* Set the version to the highest version for DTLS. This controls the initial
  104. * state of |s->enc_method| and what the API reports as the version prior to
  105. * negotiation.
  106. *
  107. * TODO(davidben): This is fragile and confusing. */
  108. s->version = DTLS1_2_VERSION;
  109. return 1;
  110. }
  111. static void dtls1_clear_queues(SSL *s) {
  112. pitem *item = NULL;
  113. hm_fragment *frag = NULL;
  114. while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
  115. frag = (hm_fragment *)item->data;
  116. dtls1_hm_fragment_free(frag);
  117. pitem_free(item);
  118. }
  119. while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
  120. frag = (hm_fragment *)item->data;
  121. dtls1_hm_fragment_free(frag);
  122. pitem_free(item);
  123. }
  124. }
  125. void dtls1_free(SSL *s) {
  126. ssl3_free(s);
  127. if (s == NULL || s->d1 == NULL) {
  128. return;
  129. }
  130. dtls1_clear_queues(s);
  131. pqueue_free(s->d1->buffered_messages);
  132. pqueue_free(s->d1->sent_messages);
  133. OPENSSL_free(s->d1);
  134. s->d1 = NULL;
  135. }
  136. long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
  137. int ret = 0;
  138. switch (cmd) {
  139. case DTLS_CTRL_GET_TIMEOUT:
  140. if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
  141. ret = 1;
  142. }
  143. break;
  144. case DTLS_CTRL_HANDLE_TIMEOUT:
  145. ret = dtls1_handle_timeout(s);
  146. break;
  147. default:
  148. ret = ssl3_ctrl(s, cmd, larg, parg);
  149. break;
  150. }
  151. return ret;
  152. }
  153. const SSL_CIPHER *dtls1_get_cipher(size_t i) {
  154. const SSL_CIPHER *ciph = ssl3_get_cipher(i);
  155. /* DTLS does not support stream ciphers. */
  156. if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) {
  157. return NULL;
  158. }
  159. return ciph;
  160. }
  161. void dtls1_start_timer(SSL *s) {
  162. /* If timer is not set, initialize duration with 1 second */
  163. if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
  164. s->d1->timeout_duration = 1;
  165. }
  166. /* Set timeout to current time */
  167. get_current_time(s, &s->d1->next_timeout);
  168. /* Add duration to current time */
  169. s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
  170. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  171. &s->d1->next_timeout);
  172. }
  173. static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
  174. OPENSSL_timeval timenow;
  175. /* If no timeout is set, just return NULL */
  176. if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
  177. return NULL;
  178. }
  179. /* Get current time */
  180. get_current_time(s, &timenow);
  181. /* If timer already expired, set remaining time to 0 */
  182. if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
  183. (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
  184. s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
  185. memset(timeleft, 0, sizeof(OPENSSL_timeval));
  186. return timeleft;
  187. }
  188. /* Calculate time left until timer expires */
  189. memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
  190. timeleft->tv_sec -= timenow.tv_sec;
  191. timeleft->tv_usec -= timenow.tv_usec;
  192. if (timeleft->tv_usec < 0) {
  193. timeleft->tv_sec--;
  194. timeleft->tv_usec += 1000000;
  195. }
  196. /* If remaining time is less than 15 ms, set it to 0 to prevent issues
  197. * because of small devergences with socket timeouts. */
  198. if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
  199. memset(timeleft, 0, sizeof(OPENSSL_timeval));
  200. }
  201. return timeleft;
  202. }
  203. int dtls1_is_timer_expired(SSL *s) {
  204. OPENSSL_timeval timeleft;
  205. /* Get time left until timeout, return false if no timer running */
  206. if (dtls1_get_timeout(s, &timeleft) == NULL) {
  207. return 0;
  208. }
  209. /* Return false if timer is not expired yet */
  210. if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
  211. return 0;
  212. }
  213. /* Timer expired, so return true */
  214. return 1;
  215. }
  216. void dtls1_double_timeout(SSL *s) {
  217. s->d1->timeout_duration *= 2;
  218. if (s->d1->timeout_duration > 60) {
  219. s->d1->timeout_duration = 60;
  220. }
  221. dtls1_start_timer(s);
  222. }
  223. void dtls1_stop_timer(SSL *s) {
  224. /* Reset everything */
  225. s->d1->num_timeouts = 0;
  226. memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
  227. s->d1->timeout_duration = 1;
  228. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  229. &s->d1->next_timeout);
  230. /* Clear retransmission buffer */
  231. dtls1_clear_record_buffer(s);
  232. }
  233. int dtls1_check_timeout_num(SSL *s) {
  234. s->d1->num_timeouts++;
  235. /* Reduce MTU after 2 unsuccessful retransmissions */
  236. if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
  237. !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
  238. long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
  239. NULL);
  240. if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
  241. s->d1->mtu = (unsigned)mtu;
  242. }
  243. }
  244. if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
  245. /* fail the connection, enough alerts have been sent */
  246. OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. int dtls1_handle_timeout(SSL *s) {
  252. /* if no timer is expired, don't do anything */
  253. if (!dtls1_is_timer_expired(s)) {
  254. return 0;
  255. }
  256. dtls1_double_timeout(s);
  257. if (dtls1_check_timeout_num(s) < 0) {
  258. return -1;
  259. }
  260. dtls1_start_timer(s);
  261. return dtls1_retransmit_buffered_messages(s);
  262. }
  263. static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) {
  264. if (ssl->ctx->current_time_cb != NULL) {
  265. ssl->ctx->current_time_cb(ssl, out_clock);
  266. return;
  267. }
  268. #if defined(OPENSSL_WINDOWS)
  269. struct _timeb time;
  270. _ftime(&time);
  271. out_clock->tv_sec = time.time;
  272. out_clock->tv_usec = time.millitm * 1000;
  273. #else
  274. gettimeofday(out_clock, NULL);
  275. #endif
  276. }
  277. int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
  278. uint8_t *message = (uint8_t *)s->init_buf->data;
  279. const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  280. uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
  281. uint8_t *p = serialised_header;
  282. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  283. s->d1->next_handshake_write_seq++;
  284. dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
  285. s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
  286. s->init_off = 0;
  287. /* Buffer the message to handle re-xmits */
  288. dtls1_buffer_message(s, 0);
  289. /* Add the new message to the handshake hash. Serialize the message
  290. * header as if it were a single fragment. */
  291. *p++ = msg_hdr->type;
  292. l2n3(msg_hdr->msg_len, p);
  293. s2n(msg_hdr->seq, p);
  294. l2n3(0, p);
  295. l2n3(msg_hdr->msg_len, p);
  296. return ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)) &&
  297. ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
  298. }
  299. int dtls1_handshake_write(SSL *s) {
  300. return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
  301. }