您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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