Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

299 rader
8.9 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 <assert.h>
  58. #include <limits.h>
  59. #include <string.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/nid.h>
  63. #include "internal.h"
  64. #if defined(OPENSSL_WINDOWS)
  65. #include <sys/timeb.h>
  66. #else
  67. #include <sys/socket.h>
  68. #include <sys/time.h>
  69. #endif
  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(const SSL *ssl, struct timeval *out_clock);
  77. int dtls1_new(SSL *ssl) {
  78. DTLS1_STATE *d1;
  79. if (!ssl3_new(ssl)) {
  80. return 0;
  81. }
  82. d1 = OPENSSL_malloc(sizeof *d1);
  83. if (d1 == NULL) {
  84. ssl3_free(ssl);
  85. return 0;
  86. }
  87. memset(d1, 0, sizeof *d1);
  88. ssl->d1 = d1;
  89. /* Set the version to the highest supported version.
  90. *
  91. * TODO(davidben): Move this field into |s3|, have it store the normalized
  92. * protocol version, and implement this pre-negotiation quirk in |SSL_version|
  93. * at the API boundary rather than in internal state. */
  94. ssl->version = DTLS1_2_VERSION;
  95. return 1;
  96. }
  97. void dtls1_free(SSL *ssl) {
  98. ssl3_free(ssl);
  99. if (ssl == NULL || ssl->d1 == NULL) {
  100. return;
  101. }
  102. dtls_clear_incoming_messages(ssl);
  103. dtls_clear_outgoing_messages(ssl);
  104. OPENSSL_free(ssl->d1);
  105. ssl->d1 = NULL;
  106. }
  107. int dtls1_supports_cipher(const SSL_CIPHER *cipher) {
  108. /* DTLS does not support stream ciphers. The NULL cipher is rejected because
  109. * it's not needed. */
  110. return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL;
  111. }
  112. void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) {
  113. ssl->initial_timeout_duration_ms = duration_ms;
  114. }
  115. void dtls1_start_timer(SSL *ssl) {
  116. /* If timer is not set, initialize duration (by default, 1 second) */
  117. if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
  118. ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
  119. }
  120. /* Set timeout to current time */
  121. get_current_time(ssl, &ssl->d1->next_timeout);
  122. /* Add duration to current time */
  123. ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
  124. ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000;
  125. if (ssl->d1->next_timeout.tv_usec >= 1000000) {
  126. ssl->d1->next_timeout.tv_sec++;
  127. ssl->d1->next_timeout.tv_usec -= 1000000;
  128. }
  129. BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  130. &ssl->d1->next_timeout);
  131. }
  132. int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
  133. if (!SSL_IS_DTLS(ssl)) {
  134. return 0;
  135. }
  136. /* If no timeout is set, just return NULL */
  137. if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
  138. return 0;
  139. }
  140. /* Get current time */
  141. struct timeval timenow;
  142. get_current_time(ssl, &timenow);
  143. /* If timer already expired, set remaining time to 0 */
  144. if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
  145. (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
  146. ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
  147. memset(out, 0, sizeof(struct timeval));
  148. return 1;
  149. }
  150. /* Calculate time left until timer expires */
  151. memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval));
  152. out->tv_sec -= timenow.tv_sec;
  153. out->tv_usec -= timenow.tv_usec;
  154. if (out->tv_usec < 0) {
  155. out->tv_sec--;
  156. out->tv_usec += 1000000;
  157. }
  158. /* If remaining time is less than 15 ms, set it to 0 to prevent issues
  159. * because of small devergences with socket timeouts. */
  160. if (out->tv_sec == 0 && out->tv_usec < 15000) {
  161. memset(out, 0, sizeof(struct timeval));
  162. }
  163. return 1;
  164. }
  165. int dtls1_is_timer_expired(SSL *ssl) {
  166. struct timeval timeleft;
  167. /* Get time left until timeout, return false if no timer running */
  168. if (!DTLSv1_get_timeout(ssl, &timeleft)) {
  169. return 0;
  170. }
  171. /* Return false if timer is not expired yet */
  172. if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
  173. return 0;
  174. }
  175. /* Timer expired, so return true */
  176. return 1;
  177. }
  178. void dtls1_double_timeout(SSL *ssl) {
  179. ssl->d1->timeout_duration_ms *= 2;
  180. if (ssl->d1->timeout_duration_ms > 60000) {
  181. ssl->d1->timeout_duration_ms = 60000;
  182. }
  183. dtls1_start_timer(ssl);
  184. }
  185. void dtls1_stop_timer(SSL *ssl) {
  186. /* Reset everything */
  187. ssl->d1->num_timeouts = 0;
  188. memset(&ssl->d1->next_timeout, 0, sizeof(struct timeval));
  189. ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
  190. BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  191. &ssl->d1->next_timeout);
  192. /* Clear retransmission buffer */
  193. dtls_clear_outgoing_messages(ssl);
  194. }
  195. int dtls1_check_timeout_num(SSL *ssl) {
  196. ssl->d1->num_timeouts++;
  197. /* Reduce MTU after 2 unsuccessful retransmissions */
  198. if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
  199. !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  200. long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
  201. if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
  202. ssl->d1->mtu = (unsigned)mtu;
  203. }
  204. }
  205. if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
  206. /* fail the connection, enough alerts have been sent */
  207. OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. int DTLSv1_handle_timeout(SSL *ssl) {
  213. ssl->rwstate = SSL_NOTHING;
  214. /* Functions which use SSL_get_error must clear the error queue on entry. */
  215. ERR_clear_error();
  216. if (!SSL_IS_DTLS(ssl)) {
  217. return -1;
  218. }
  219. /* if no timer is expired, don't do anything */
  220. if (!dtls1_is_timer_expired(ssl)) {
  221. return 0;
  222. }
  223. dtls1_double_timeout(ssl);
  224. if (dtls1_check_timeout_num(ssl) < 0) {
  225. return -1;
  226. }
  227. dtls1_start_timer(ssl);
  228. return dtls1_retransmit_outgoing_messages(ssl);
  229. }
  230. static void get_current_time(const SSL *ssl, struct timeval *out_clock) {
  231. if (ssl->ctx->current_time_cb != NULL) {
  232. ssl->ctx->current_time_cb(ssl, out_clock);
  233. return;
  234. }
  235. #if defined(OPENSSL_WINDOWS)
  236. struct _timeb time;
  237. _ftime(&time);
  238. out_clock->tv_sec = time.time;
  239. out_clock->tv_usec = time.millitm * 1000;
  240. #else
  241. gettimeofday(out_clock, NULL);
  242. #endif
  243. }
  244. void dtls1_expect_flight(SSL *ssl) {
  245. dtls1_start_timer(ssl);
  246. }
  247. void dtls1_received_flight(SSL *ssl) {
  248. dtls1_stop_timer(ssl);
  249. }