Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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