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.
 
 
 
 
 
 

361 lines
11 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/ssl.h>
  57. #include <limits.h>
  58. #include <string.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/nid.h>
  62. #include "internal.h"
  63. #if defined(OPENSSL_WINDOWS)
  64. #include <sys/timeb.h>
  65. #else
  66. #include <sys/socket.h>
  67. #include <sys/time.h>
  68. #endif
  69. /* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
  70. * before starting to decrease the MTU. */
  71. #define DTLS1_MTU_TIMEOUTS 2
  72. /* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
  73. * before failing the DTLS handshake. */
  74. #define DTLS1_MAX_TIMEOUTS 12
  75. static void get_current_time(const SSL *ssl, struct timeval *out_clock);
  76. int dtls1_new(SSL *ssl) {
  77. DTLS1_STATE *d1;
  78. if (!ssl3_new(ssl)) {
  79. return 0;
  80. }
  81. d1 = OPENSSL_malloc(sizeof *d1);
  82. if (d1 == NULL) {
  83. ssl3_free(ssl);
  84. return 0;
  85. }
  86. memset(d1, 0, sizeof *d1);
  87. d1->buffered_messages = pqueue_new();
  88. d1->sent_messages = pqueue_new();
  89. if (!d1->buffered_messages || !d1->sent_messages) {
  90. pqueue_free(d1->buffered_messages);
  91. pqueue_free(d1->sent_messages);
  92. OPENSSL_free(d1);
  93. ssl3_free(ssl);
  94. return 0;
  95. }
  96. ssl->d1 = d1;
  97. /* Set the version to the highest supported version.
  98. *
  99. * TODO(davidben): Move this field into |s3|, have it store the normalized
  100. * protocol version, and implement this pre-negotiation quirk in |SSL_version|
  101. * at the API boundary rather than in internal state. */
  102. ssl->version = DTLS1_2_VERSION;
  103. return 1;
  104. }
  105. static void dtls1_clear_queues(SSL *ssl) {
  106. pitem *item = NULL;
  107. hm_fragment *frag = NULL;
  108. while ((item = pqueue_pop(ssl->d1->buffered_messages)) != NULL) {
  109. frag = (hm_fragment *)item->data;
  110. dtls1_hm_fragment_free(frag);
  111. pitem_free(item);
  112. }
  113. while ((item = pqueue_pop(ssl->d1->sent_messages)) != NULL) {
  114. frag = (hm_fragment *)item->data;
  115. dtls1_hm_fragment_free(frag);
  116. pitem_free(item);
  117. }
  118. }
  119. void dtls1_free(SSL *ssl) {
  120. ssl3_free(ssl);
  121. if (ssl == NULL || ssl->d1 == NULL) {
  122. return;
  123. }
  124. dtls1_clear_queues(ssl);
  125. pqueue_free(ssl->d1->buffered_messages);
  126. pqueue_free(ssl->d1->sent_messages);
  127. OPENSSL_free(ssl->d1);
  128. ssl->d1 = NULL;
  129. }
  130. int dtls1_supports_cipher(const SSL_CIPHER *cipher) {
  131. /* DTLS does not support stream ciphers. The NULL cipher is rejected because
  132. * it's not needed. */
  133. return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL;
  134. }
  135. void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) {
  136. ssl->initial_timeout_duration_ms = duration_ms;
  137. }
  138. void dtls1_start_timer(SSL *ssl) {
  139. /* If timer is not set, initialize duration (by default, 1 second) */
  140. if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
  141. ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
  142. }
  143. /* Set timeout to current time */
  144. get_current_time(ssl, &ssl->d1->next_timeout);
  145. /* Add duration to current time */
  146. ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
  147. ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000;
  148. if (ssl->d1->next_timeout.tv_usec >= 1000000) {
  149. ssl->d1->next_timeout.tv_sec++;
  150. ssl->d1->next_timeout.tv_usec -= 1000000;
  151. }
  152. BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  153. &ssl->d1->next_timeout);
  154. }
  155. int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
  156. if (!SSL_IS_DTLS(ssl)) {
  157. return 0;
  158. }
  159. /* If no timeout is set, just return NULL */
  160. if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
  161. return 0;
  162. }
  163. /* Get current time */
  164. struct timeval timenow;
  165. get_current_time(ssl, &timenow);
  166. /* If timer already expired, set remaining time to 0 */
  167. if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
  168. (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
  169. ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
  170. memset(out, 0, sizeof(struct timeval));
  171. return 1;
  172. }
  173. /* Calculate time left until timer expires */
  174. memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval));
  175. out->tv_sec -= timenow.tv_sec;
  176. out->tv_usec -= timenow.tv_usec;
  177. if (out->tv_usec < 0) {
  178. out->tv_sec--;
  179. out->tv_usec += 1000000;
  180. }
  181. /* If remaining time is less than 15 ms, set it to 0 to prevent issues
  182. * because of small devergences with socket timeouts. */
  183. if (out->tv_sec == 0 && out->tv_usec < 15000) {
  184. memset(out, 0, sizeof(struct timeval));
  185. }
  186. return 1;
  187. }
  188. int dtls1_is_timer_expired(SSL *ssl) {
  189. struct timeval timeleft;
  190. /* Get time left until timeout, return false if no timer running */
  191. if (!DTLSv1_get_timeout(ssl, &timeleft)) {
  192. return 0;
  193. }
  194. /* Return false if timer is not expired yet */
  195. if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
  196. return 0;
  197. }
  198. /* Timer expired, so return true */
  199. return 1;
  200. }
  201. void dtls1_double_timeout(SSL *ssl) {
  202. ssl->d1->timeout_duration_ms *= 2;
  203. if (ssl->d1->timeout_duration_ms > 60000) {
  204. ssl->d1->timeout_duration_ms = 60000;
  205. }
  206. dtls1_start_timer(ssl);
  207. }
  208. void dtls1_stop_timer(SSL *ssl) {
  209. /* Reset everything */
  210. ssl->d1->num_timeouts = 0;
  211. memset(&ssl->d1->next_timeout, 0, sizeof(struct timeval));
  212. ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
  213. BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  214. &ssl->d1->next_timeout);
  215. /* Clear retransmission buffer */
  216. dtls1_clear_record_buffer(ssl);
  217. }
  218. int dtls1_check_timeout_num(SSL *ssl) {
  219. ssl->d1->num_timeouts++;
  220. /* Reduce MTU after 2 unsuccessful retransmissions */
  221. if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
  222. !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  223. long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
  224. if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
  225. ssl->d1->mtu = (unsigned)mtu;
  226. }
  227. }
  228. if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
  229. /* fail the connection, enough alerts have been sent */
  230. OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. int DTLSv1_handle_timeout(SSL *ssl) {
  236. ssl->rwstate = SSL_NOTHING;
  237. /* Functions which use SSL_get_error must clear the error queue on entry. */
  238. ERR_clear_error();
  239. if (!SSL_IS_DTLS(ssl)) {
  240. return -1;
  241. }
  242. /* if no timer is expired, don't do anything */
  243. if (!dtls1_is_timer_expired(ssl)) {
  244. return 0;
  245. }
  246. dtls1_double_timeout(ssl);
  247. if (dtls1_check_timeout_num(ssl) < 0) {
  248. return -1;
  249. }
  250. dtls1_start_timer(ssl);
  251. return dtls1_retransmit_buffered_messages(ssl);
  252. }
  253. static void get_current_time(const SSL *ssl, struct timeval *out_clock) {
  254. if (ssl->ctx->current_time_cb != NULL) {
  255. ssl->ctx->current_time_cb(ssl, out_clock);
  256. return;
  257. }
  258. #if defined(OPENSSL_WINDOWS)
  259. struct _timeb time;
  260. _ftime(&time);
  261. out_clock->tv_sec = time.time;
  262. out_clock->tv_usec = time.millitm * 1000;
  263. #else
  264. gettimeofday(out_clock, NULL);
  265. #endif
  266. }
  267. int dtls1_set_handshake_header(SSL *ssl, int htype, unsigned long len) {
  268. uint8_t *message = (uint8_t *)ssl->init_buf->data;
  269. const struct hm_header_st *msg_hdr = &ssl->d1->w_msg_hdr;
  270. uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
  271. uint8_t *p = serialised_header;
  272. ssl->d1->handshake_write_seq = ssl->d1->next_handshake_write_seq;
  273. ssl->d1->next_handshake_write_seq++;
  274. dtls1_set_message_header(ssl, htype, len, ssl->d1->handshake_write_seq, 0,
  275. len);
  276. ssl->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
  277. ssl->init_off = 0;
  278. /* Buffer the message to handle re-xmits */
  279. dtls1_buffer_message(ssl);
  280. /* Add the new message to the handshake hash. Serialize the message
  281. * header as if it were a single fragment. */
  282. *p++ = msg_hdr->type;
  283. l2n3(msg_hdr->msg_len, p);
  284. s2n(msg_hdr->seq, p);
  285. l2n3(0, p);
  286. l2n3(msg_hdr->msg_len, p);
  287. return ssl3_update_handshake_hash(ssl, serialised_header,
  288. sizeof(serialised_header)) &&
  289. ssl3_update_handshake_hash(ssl, message + DTLS1_HM_HEADER_LENGTH, len);
  290. }
  291. int dtls1_handshake_write(SSL *ssl) {
  292. return dtls1_do_handshake_write(ssl, dtls1_use_current_epoch);
  293. }
  294. void dtls1_expect_flight(SSL *ssl) {
  295. dtls1_start_timer(ssl);
  296. }
  297. void dtls1_received_flight(SSL *ssl) {
  298. dtls1_stop_timer(ssl);
  299. }