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.

d1_lib.c 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #if defined(OPENSSL_WINDOWS)
  60. #include <sys/timeb.h>
  61. #else
  62. #include <sys/socket.h>
  63. #include <sys/time.h>
  64. #endif
  65. #include <openssl/err.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/obj.h>
  68. #include "ssl_locl.h"
  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(SSL *ssl, OPENSSL_timeval *out_clock);
  76. static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft);
  77. static void dtls1_set_handshake_header(SSL *s, int type, unsigned long len);
  78. static int dtls1_handshake_write(SSL *s);
  79. const SSL3_ENC_METHOD DTLSv1_enc_data = {
  80. tls1_enc,
  81. tls1_prf,
  82. tls1_setup_key_block,
  83. tls1_generate_master_secret,
  84. tls1_change_cipher_state,
  85. tls1_final_finish_mac,
  86. TLS1_FINISH_MAC_LENGTH,
  87. tls1_cert_verify_mac,
  88. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  89. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  90. tls1_alert_code,
  91. tls1_export_keying_material,
  92. SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV,
  93. DTLS1_HM_HEADER_LENGTH,
  94. dtls1_set_handshake_header,
  95. dtls1_handshake_write,
  96. };
  97. const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
  98. tls1_enc,
  99. tls1_prf,
  100. tls1_setup_key_block,
  101. tls1_generate_master_secret,
  102. tls1_change_cipher_state,
  103. tls1_final_finish_mac,
  104. TLS1_FINISH_MAC_LENGTH,
  105. tls1_cert_verify_mac,
  106. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  107. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  108. tls1_alert_code,
  109. tls1_export_keying_material,
  110. SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS |
  111. SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
  112. DTLS1_HM_HEADER_LENGTH,
  113. dtls1_set_handshake_header,
  114. dtls1_handshake_write,
  115. };
  116. int dtls1_new(SSL *s) {
  117. DTLS1_STATE *d1;
  118. if (!ssl3_new(s)) {
  119. return 0;
  120. }
  121. d1 = OPENSSL_malloc(sizeof *d1);
  122. if (d1 == NULL) {
  123. ssl3_free(s);
  124. return 0;
  125. }
  126. memset(d1, 0, sizeof *d1);
  127. d1->unprocessed_rcds.q = pqueue_new();
  128. d1->processed_rcds.q = pqueue_new();
  129. d1->buffered_messages = pqueue_new();
  130. d1->sent_messages = pqueue_new();
  131. d1->buffered_app_data.q = pqueue_new();
  132. if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q ||
  133. !d1->buffered_messages || !d1->sent_messages ||
  134. !d1->buffered_app_data.q) {
  135. if (d1->unprocessed_rcds.q) {
  136. pqueue_free(d1->unprocessed_rcds.q);
  137. }
  138. if (d1->processed_rcds.q) {
  139. pqueue_free(d1->processed_rcds.q);
  140. }
  141. if (d1->buffered_messages) {
  142. pqueue_free(d1->buffered_messages);
  143. }
  144. if (d1->sent_messages) {
  145. pqueue_free(d1->sent_messages);
  146. }
  147. if (d1->buffered_app_data.q) {
  148. pqueue_free(d1->buffered_app_data.q);
  149. }
  150. OPENSSL_free(d1);
  151. ssl3_free(s);
  152. return 0;
  153. }
  154. s->d1 = d1;
  155. /* Set the version to the highest version for DTLS. This controls the initial
  156. * state of |s->enc_method| and what the API reports as the version prior to
  157. * negotiation.
  158. *
  159. * TODO(davidben): This is fragile and confusing. */
  160. s->version = DTLS1_2_VERSION;
  161. return 1;
  162. }
  163. static void dtls1_clear_queues(SSL *s) {
  164. pitem *item = NULL;
  165. hm_fragment *frag = NULL;
  166. DTLS1_RECORD_DATA *rdata;
  167. while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
  168. rdata = (DTLS1_RECORD_DATA *)item->data;
  169. if (rdata->rbuf.buf) {
  170. OPENSSL_free(rdata->rbuf.buf);
  171. }
  172. OPENSSL_free(item->data);
  173. pitem_free(item);
  174. }
  175. while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
  176. rdata = (DTLS1_RECORD_DATA *)item->data;
  177. if (rdata->rbuf.buf) {
  178. OPENSSL_free(rdata->rbuf.buf);
  179. }
  180. OPENSSL_free(item->data);
  181. pitem_free(item);
  182. }
  183. while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
  184. frag = (hm_fragment *)item->data;
  185. dtls1_hm_fragment_free(frag);
  186. pitem_free(item);
  187. }
  188. while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
  189. frag = (hm_fragment *)item->data;
  190. dtls1_hm_fragment_free(frag);
  191. pitem_free(item);
  192. }
  193. while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
  194. rdata = (DTLS1_RECORD_DATA *)item->data;
  195. if (rdata->rbuf.buf) {
  196. OPENSSL_free(rdata->rbuf.buf);
  197. }
  198. OPENSSL_free(item->data);
  199. pitem_free(item);
  200. }
  201. }
  202. void dtls1_free(SSL *s) {
  203. ssl3_free(s);
  204. if (s == NULL || s->d1 == NULL) {
  205. return;
  206. }
  207. dtls1_clear_queues(s);
  208. pqueue_free(s->d1->unprocessed_rcds.q);
  209. pqueue_free(s->d1->processed_rcds.q);
  210. pqueue_free(s->d1->buffered_messages);
  211. pqueue_free(s->d1->sent_messages);
  212. pqueue_free(s->d1->buffered_app_data.q);
  213. OPENSSL_free(s->d1);
  214. s->d1 = NULL;
  215. }
  216. long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
  217. int ret = 0;
  218. switch (cmd) {
  219. case DTLS_CTRL_GET_TIMEOUT:
  220. if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
  221. ret = 1;
  222. }
  223. break;
  224. case DTLS_CTRL_HANDLE_TIMEOUT:
  225. ret = dtls1_handle_timeout(s);
  226. break;
  227. default:
  228. ret = ssl3_ctrl(s, cmd, larg, parg);
  229. break;
  230. }
  231. return ret;
  232. }
  233. const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
  234. const SSL_CIPHER *ciph = ssl3_get_cipher(u);
  235. /* DTLS does not support stream ciphers. */
  236. if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) {
  237. return NULL;
  238. }
  239. return ciph;
  240. }
  241. void dtls1_start_timer(SSL *s) {
  242. /* If timer is not set, initialize duration with 1 second */
  243. if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
  244. s->d1->timeout_duration = 1;
  245. }
  246. /* Set timeout to current time */
  247. get_current_time(s, &s->d1->next_timeout);
  248. /* Add duration to current time */
  249. s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
  250. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  251. &s->d1->next_timeout);
  252. }
  253. static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
  254. OPENSSL_timeval timenow;
  255. /* If no timeout is set, just return NULL */
  256. if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
  257. return NULL;
  258. }
  259. /* Get current time */
  260. get_current_time(s, &timenow);
  261. /* If timer already expired, set remaining time to 0 */
  262. if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
  263. (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
  264. s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
  265. memset(timeleft, 0, sizeof(OPENSSL_timeval));
  266. return timeleft;
  267. }
  268. /* Calculate time left until timer expires */
  269. memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
  270. timeleft->tv_sec -= timenow.tv_sec;
  271. timeleft->tv_usec -= timenow.tv_usec;
  272. if (timeleft->tv_usec < 0) {
  273. timeleft->tv_sec--;
  274. timeleft->tv_usec += 1000000;
  275. }
  276. /* If remaining time is less than 15 ms, set it to 0 to prevent issues
  277. * because of small devergences with socket timeouts. */
  278. if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
  279. memset(timeleft, 0, sizeof(OPENSSL_timeval));
  280. }
  281. return timeleft;
  282. }
  283. int dtls1_is_timer_expired(SSL *s) {
  284. OPENSSL_timeval timeleft;
  285. /* Get time left until timeout, return false if no timer running */
  286. if (dtls1_get_timeout(s, &timeleft) == NULL) {
  287. return 0;
  288. }
  289. /* Return false if timer is not expired yet */
  290. if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
  291. return 0;
  292. }
  293. /* Timer expired, so return true */
  294. return 1;
  295. }
  296. void dtls1_double_timeout(SSL *s) {
  297. s->d1->timeout_duration *= 2;
  298. if (s->d1->timeout_duration > 60) {
  299. s->d1->timeout_duration = 60;
  300. }
  301. dtls1_start_timer(s);
  302. }
  303. void dtls1_stop_timer(SSL *s) {
  304. /* Reset everything */
  305. s->d1->num_timeouts = 0;
  306. memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
  307. s->d1->timeout_duration = 1;
  308. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  309. &s->d1->next_timeout);
  310. /* Clear retransmission buffer */
  311. dtls1_clear_record_buffer(s);
  312. }
  313. int dtls1_check_timeout_num(SSL *s) {
  314. s->d1->num_timeouts++;
  315. /* Reduce MTU after 2 unsuccessful retransmissions */
  316. if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
  317. !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
  318. long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
  319. NULL);
  320. if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
  321. s->d1->mtu = (unsigned)mtu;
  322. }
  323. }
  324. if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
  325. /* fail the connection, enough alerts have been sent */
  326. OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
  327. return -1;
  328. }
  329. return 0;
  330. }
  331. int dtls1_handle_timeout(SSL *s) {
  332. /* if no timer is expired, don't do anything */
  333. if (!dtls1_is_timer_expired(s)) {
  334. return 0;
  335. }
  336. dtls1_double_timeout(s);
  337. if (dtls1_check_timeout_num(s) < 0) {
  338. return -1;
  339. }
  340. dtls1_start_timer(s);
  341. return dtls1_retransmit_buffered_messages(s);
  342. }
  343. static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) {
  344. if (ssl->ctx->current_time_cb != NULL) {
  345. ssl->ctx->current_time_cb(ssl, out_clock);
  346. return;
  347. }
  348. #if defined(OPENSSL_WINDOWS)
  349. struct _timeb time;
  350. _ftime(&time);
  351. out_clock->tv_sec = time.time;
  352. out_clock->tv_usec = time.millitm * 1000;
  353. #else
  354. gettimeofday(out_clock, NULL);
  355. #endif
  356. }
  357. static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
  358. uint8_t *message = (uint8_t *)s->init_buf->data;
  359. const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  360. uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
  361. uint8_t *p = serialised_header;
  362. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  363. s->d1->next_handshake_write_seq++;
  364. dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
  365. s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
  366. s->init_off = 0;
  367. /* Buffer the message to handle re-xmits */
  368. dtls1_buffer_message(s, 0);
  369. /* Add the new message to the handshake hash. Serialize the message
  370. * header as if it were a single fragment. */
  371. *p++ = msg_hdr->type;
  372. l2n3(msg_hdr->msg_len, p);
  373. s2n(msg_hdr->seq, p);
  374. l2n3(0, p);
  375. l2n3(msg_hdr->msg_len, p);
  376. ssl3_finish_mac(s, serialised_header, sizeof(serialised_header));
  377. ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
  378. }
  379. static int dtls1_handshake_write(SSL *s) {
  380. return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
  381. }