d1_lib.c 10 KB

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