d1_lib.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "../crypto/internal.h"
  64. #include "internal.h"
  65. BSSL_NAMESPACE_BEGIN
  66. // DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
  67. // before starting to decrease the MTU.
  68. #define DTLS1_MTU_TIMEOUTS 2
  69. // DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
  70. // before failing the DTLS handshake.
  71. #define DTLS1_MAX_TIMEOUTS 12
  72. DTLS1_STATE::DTLS1_STATE()
  73. : has_change_cipher_spec(false),
  74. outgoing_messages_complete(false),
  75. flight_has_reply(false) {}
  76. DTLS1_STATE::~DTLS1_STATE() {}
  77. bool dtls1_new(SSL *ssl) {
  78. if (!ssl3_new(ssl)) {
  79. return false;
  80. }
  81. UniquePtr<DTLS1_STATE> d1 = MakeUnique<DTLS1_STATE>();
  82. if (!d1) {
  83. ssl3_free(ssl);
  84. return false;
  85. }
  86. ssl->d1 = d1.release();
  87. // Set the version to the highest supported version.
  88. //
  89. // TODO(davidben): Move this field into |s3|, have it store the normalized
  90. // protocol version, and implement this pre-negotiation quirk in |SSL_version|
  91. // at the API boundary rather than in internal state.
  92. ssl->version = DTLS1_2_VERSION;
  93. return true;
  94. }
  95. void dtls1_free(SSL *ssl) {
  96. ssl3_free(ssl);
  97. if (ssl == NULL) {
  98. return;
  99. }
  100. Delete(ssl->d1);
  101. ssl->d1 = NULL;
  102. }
  103. void dtls1_start_timer(SSL *ssl) {
  104. // If timer is not set, initialize duration (by default, 1 second)
  105. if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
  106. ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
  107. }
  108. // Set timeout to current time
  109. ssl_get_current_time(ssl, &ssl->d1->next_timeout);
  110. // Add duration to current time
  111. ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
  112. ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000;
  113. if (ssl->d1->next_timeout.tv_usec >= 1000000) {
  114. ssl->d1->next_timeout.tv_sec++;
  115. ssl->d1->next_timeout.tv_usec -= 1000000;
  116. }
  117. }
  118. bool dtls1_is_timer_expired(SSL *ssl) {
  119. struct timeval timeleft;
  120. // Get time left until timeout, return false if no timer running
  121. if (!DTLSv1_get_timeout(ssl, &timeleft)) {
  122. return false;
  123. }
  124. // Return false if timer is not expired yet
  125. if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
  126. return false;
  127. }
  128. // Timer expired, so return true
  129. return true;
  130. }
  131. static void dtls1_double_timeout(SSL *ssl) {
  132. ssl->d1->timeout_duration_ms *= 2;
  133. if (ssl->d1->timeout_duration_ms > 60000) {
  134. ssl->d1->timeout_duration_ms = 60000;
  135. }
  136. }
  137. void dtls1_stop_timer(SSL *ssl) {
  138. ssl->d1->num_timeouts = 0;
  139. OPENSSL_memset(&ssl->d1->next_timeout, 0, sizeof(ssl->d1->next_timeout));
  140. ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
  141. }
  142. bool dtls1_check_timeout_num(SSL *ssl) {
  143. ssl->d1->num_timeouts++;
  144. // Reduce MTU after 2 unsuccessful retransmissions
  145. if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
  146. !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  147. long mtu =
  148. BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, nullptr);
  149. if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
  150. ssl->d1->mtu = (unsigned)mtu;
  151. }
  152. }
  153. if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
  154. // fail the connection, enough alerts have been sent
  155. OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
  156. return false;
  157. }
  158. return true;
  159. }
  160. BSSL_NAMESPACE_END
  161. using namespace bssl;
  162. void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) {
  163. ssl->initial_timeout_duration_ms = duration_ms;
  164. }
  165. int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
  166. if (!SSL_is_dtls(ssl)) {
  167. return 0;
  168. }
  169. // If no timeout is set, just return 0.
  170. if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
  171. return 0;
  172. }
  173. struct OPENSSL_timeval timenow;
  174. ssl_get_current_time(ssl, &timenow);
  175. // If timer already expired, set remaining time to 0.
  176. if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
  177. (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
  178. ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
  179. OPENSSL_memset(out, 0, sizeof(*out));
  180. return 1;
  181. }
  182. // Calculate time left until timer expires.
  183. struct OPENSSL_timeval ret;
  184. OPENSSL_memcpy(&ret, &ssl->d1->next_timeout, sizeof(ret));
  185. ret.tv_sec -= timenow.tv_sec;
  186. if (ret.tv_usec >= timenow.tv_usec) {
  187. ret.tv_usec -= timenow.tv_usec;
  188. } else {
  189. ret.tv_usec = 1000000 + ret.tv_usec - timenow.tv_usec;
  190. ret.tv_sec--;
  191. }
  192. // If remaining time is less than 15 ms, set it to 0 to prevent issues
  193. // because of small divergences with socket timeouts.
  194. if (ret.tv_sec == 0 && ret.tv_usec < 15000) {
  195. OPENSSL_memset(&ret, 0, sizeof(ret));
  196. }
  197. // Clamp the result in case of overflow.
  198. if (ret.tv_sec > INT_MAX) {
  199. assert(0);
  200. out->tv_sec = INT_MAX;
  201. } else {
  202. out->tv_sec = ret.tv_sec;
  203. }
  204. out->tv_usec = ret.tv_usec;
  205. return 1;
  206. }
  207. int DTLSv1_handle_timeout(SSL *ssl) {
  208. ssl_reset_error_state(ssl);
  209. if (!SSL_is_dtls(ssl)) {
  210. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  211. return -1;
  212. }
  213. // If no timer is expired, don't do anything.
  214. if (!dtls1_is_timer_expired(ssl)) {
  215. return 0;
  216. }
  217. if (!dtls1_check_timeout_num(ssl)) {
  218. return -1;
  219. }
  220. dtls1_double_timeout(ssl);
  221. dtls1_start_timer(ssl);
  222. return dtls1_retransmit_outgoing_messages(ssl);
  223. }