d1_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* ssl/d1_lib.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #define USE_SOCKETS
  61. #include <openssl/objects.h>
  62. #include "ssl_locl.h"
  63. #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
  64. # include <sys/timeb.h>
  65. #endif
  66. static void get_current_time(struct timeval *t);
  67. const char dtls1_version_str[] = "DTLSv1" OPENSSL_VERSION_PTEXT;
  68. int dtls1_listen(SSL *s, struct sockaddr *client);
  69. SSL3_ENC_METHOD DTLSv1_enc_data = {
  70. dtls1_enc,
  71. tls1_mac,
  72. tls1_setup_key_block,
  73. tls1_generate_master_secret,
  74. tls1_change_cipher_state,
  75. tls1_final_finish_mac,
  76. TLS1_FINISH_MAC_LENGTH,
  77. tls1_cert_verify_mac,
  78. TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
  79. TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
  80. tls1_alert_code,
  81. };
  82. long dtls1_default_timeout(void)
  83. {
  84. /*
  85. * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for
  86. * http, the cache would over fill
  87. */
  88. return (60 * 60 * 2);
  89. }
  90. IMPLEMENT_dtls1_meth_func(dtlsv1_base_method,
  91. ssl_undefined_function,
  92. ssl_undefined_function, ssl_bad_method)
  93. int dtls1_new(SSL *s)
  94. {
  95. DTLS1_STATE *d1;
  96. if (!ssl3_new(s))
  97. return (0);
  98. if ((d1 = OPENSSL_malloc(sizeof *d1)) == NULL)
  99. return (0);
  100. memset(d1, 0, sizeof *d1);
  101. /* d1->handshake_epoch=0; */
  102. #if defined(OPENSSL_SYS_VMS) || defined(VMS_TEST)
  103. d1->bitmap.length = 64;
  104. #else
  105. d1->bitmap.length = sizeof(d1->bitmap.map) * 8;
  106. #endif
  107. pq_64bit_init(&(d1->bitmap.map));
  108. pq_64bit_init(&(d1->bitmap.max_seq_num));
  109. d1->next_bitmap.length = d1->bitmap.length;
  110. pq_64bit_init(&(d1->next_bitmap.map));
  111. pq_64bit_init(&(d1->next_bitmap.max_seq_num));
  112. d1->unprocessed_rcds.q = pqueue_new();
  113. d1->processed_rcds.q = pqueue_new();
  114. d1->buffered_messages = pqueue_new();
  115. d1->sent_messages = pqueue_new();
  116. d1->buffered_app_data.q = pqueue_new();
  117. if (s->server) {
  118. d1->cookie_len = sizeof(s->d1->cookie);
  119. }
  120. if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q
  121. || !d1->buffered_messages || !d1->sent_messages
  122. || !d1->buffered_app_data.q) {
  123. if (d1->unprocessed_rcds.q)
  124. pqueue_free(d1->unprocessed_rcds.q);
  125. if (d1->processed_rcds.q)
  126. pqueue_free(d1->processed_rcds.q);
  127. if (d1->buffered_messages)
  128. pqueue_free(d1->buffered_messages);
  129. if (d1->sent_messages)
  130. pqueue_free(d1->sent_messages);
  131. if (d1->buffered_app_data.q)
  132. pqueue_free(d1->buffered_app_data.q);
  133. OPENSSL_free(d1);
  134. return (0);
  135. }
  136. s->d1 = d1;
  137. s->method->ssl_clear(s);
  138. return (1);
  139. }
  140. static void dtls1_clear_queues(SSL *s)
  141. {
  142. pitem *item = NULL;
  143. hm_fragment *frag = NULL;
  144. DTLS1_RECORD_DATA *rdata;
  145. while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
  146. rdata = (DTLS1_RECORD_DATA *)item->data;
  147. if (rdata->rbuf.buf) {
  148. OPENSSL_free(rdata->rbuf.buf);
  149. }
  150. OPENSSL_free(item->data);
  151. pitem_free(item);
  152. }
  153. while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
  154. rdata = (DTLS1_RECORD_DATA *)item->data;
  155. if (rdata->rbuf.buf) {
  156. OPENSSL_free(rdata->rbuf.buf);
  157. }
  158. OPENSSL_free(item->data);
  159. pitem_free(item);
  160. }
  161. while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
  162. frag = (hm_fragment *)item->data;
  163. OPENSSL_free(frag->fragment);
  164. OPENSSL_free(frag);
  165. pitem_free(item);
  166. }
  167. while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
  168. frag = (hm_fragment *)item->data;
  169. OPENSSL_free(frag->fragment);
  170. OPENSSL_free(frag);
  171. pitem_free(item);
  172. }
  173. while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
  174. rdata = (DTLS1_RECORD_DATA *)item->data;
  175. if (rdata->rbuf.buf) {
  176. OPENSSL_free(rdata->rbuf.buf);
  177. }
  178. OPENSSL_free(item->data);
  179. pitem_free(item);
  180. }
  181. }
  182. void dtls1_free(SSL *s)
  183. {
  184. ssl3_free(s);
  185. dtls1_clear_queues(s);
  186. pqueue_free(s->d1->unprocessed_rcds.q);
  187. pqueue_free(s->d1->processed_rcds.q);
  188. pqueue_free(s->d1->buffered_messages);
  189. pqueue_free(s->d1->sent_messages);
  190. pqueue_free(s->d1->buffered_app_data.q);
  191. pq_64bit_free(&(s->d1->bitmap.map));
  192. pq_64bit_free(&(s->d1->bitmap.max_seq_num));
  193. pq_64bit_free(&(s->d1->next_bitmap.map));
  194. pq_64bit_free(&(s->d1->next_bitmap.max_seq_num));
  195. OPENSSL_free(s->d1);
  196. s->d1 = NULL;
  197. }
  198. void dtls1_clear(SSL *s)
  199. {
  200. pqueue unprocessed_rcds;
  201. pqueue processed_rcds;
  202. pqueue buffered_messages;
  203. pqueue sent_messages;
  204. pqueue buffered_app_data;
  205. unsigned int mtu;
  206. if (s->d1) {
  207. unprocessed_rcds = s->d1->unprocessed_rcds.q;
  208. processed_rcds = s->d1->processed_rcds.q;
  209. buffered_messages = s->d1->buffered_messages;
  210. sent_messages = s->d1->sent_messages;
  211. buffered_app_data = s->d1->buffered_app_data.q;
  212. mtu = s->d1->mtu;
  213. dtls1_clear_queues(s);
  214. pq_64bit_free(&(s->d1->bitmap.map));
  215. pq_64bit_free(&(s->d1->bitmap.max_seq_num));
  216. pq_64bit_free(&(s->d1->next_bitmap.map));
  217. pq_64bit_free(&(s->d1->next_bitmap.max_seq_num));
  218. memset(s->d1, 0, sizeof(*(s->d1)));
  219. if (s->server) {
  220. s->d1->cookie_len = sizeof(s->d1->cookie);
  221. }
  222. if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) {
  223. s->d1->mtu = mtu;
  224. }
  225. s->d1->unprocessed_rcds.q = unprocessed_rcds;
  226. s->d1->processed_rcds.q = processed_rcds;
  227. s->d1->buffered_messages = buffered_messages;
  228. s->d1->sent_messages = sent_messages;
  229. s->d1->buffered_app_data.q = buffered_app_data;
  230. #if defined(OPENSSL_SYS_VMS) || defined(VMS_TEST)
  231. s->d1->bitmap.length = 64;
  232. #else
  233. s->d1->bitmap.length = sizeof(s->d1->bitmap.map) * 8;
  234. #endif
  235. pq_64bit_init(&(s->d1->bitmap.map));
  236. pq_64bit_init(&(s->d1->bitmap.max_seq_num));
  237. s->d1->next_bitmap.length = s->d1->bitmap.length;
  238. pq_64bit_init(&(s->d1->next_bitmap.map));
  239. pq_64bit_init(&(s->d1->next_bitmap.max_seq_num));
  240. }
  241. ssl3_clear(s);
  242. if (s->options & SSL_OP_CISCO_ANYCONNECT)
  243. s->version = DTLS1_BAD_VER;
  244. else
  245. s->version = DTLS1_VERSION;
  246. }
  247. long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg)
  248. {
  249. int ret = 0;
  250. switch (cmd) {
  251. case DTLS_CTRL_GET_TIMEOUT:
  252. if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) {
  253. ret = 1;
  254. }
  255. break;
  256. case DTLS_CTRL_HANDLE_TIMEOUT:
  257. ret = dtls1_handle_timeout(s);
  258. break;
  259. case DTLS_CTRL_LISTEN:
  260. ret = dtls1_listen(s, parg);
  261. break;
  262. case SSL_CTRL_CHECK_PROTO_VERSION:
  263. /*
  264. * For library-internal use; checks that the current protocol is the
  265. * highest enabled version (according to s->ctx->method, as version
  266. * negotiation may have changed s->method).
  267. */
  268. #if DTLS_MAX_VERSION != DTLS1_VERSION
  269. # error Code needs update for DTLS_method() support beyond DTLS1_VERSION.
  270. #endif
  271. /*
  272. * Just one protocol version is supported so far; fail closed if the
  273. * version is not as expected.
  274. */
  275. return s->version == DTLS_MAX_VERSION;
  276. default:
  277. ret = ssl3_ctrl(s, cmd, larg, parg);
  278. break;
  279. }
  280. return (ret);
  281. }
  282. /*
  283. * As it's impossible to use stream ciphers in "datagram" mode, this
  284. * simple filter is designed to disengage them in DTLS. Unfortunately
  285. * there is no universal way to identify stream SSL_CIPHER, so we have
  286. * to explicitly list their SSL_* codes. Currently RC4 is the only one
  287. * available, but if new ones emerge, they will have to be added...
  288. */
  289. SSL_CIPHER *dtls1_get_cipher(unsigned int u)
  290. {
  291. SSL_CIPHER *ciph = ssl3_get_cipher(u);
  292. if (ciph != NULL) {
  293. if ((ciph->algorithms & SSL_ENC_MASK) == SSL_RC4)
  294. return NULL;
  295. }
  296. return ciph;
  297. }
  298. void dtls1_start_timer(SSL *s)
  299. {
  300. /* If timer is not set, initialize duration with 1 second */
  301. if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
  302. s->d1->timeout_duration = 1;
  303. }
  304. /* Set timeout to current time */
  305. get_current_time(&(s->d1->next_timeout));
  306. /* Add duration to current time */
  307. s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
  308. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  309. &(s->d1->next_timeout));
  310. }
  311. struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
  312. {
  313. struct timeval timenow;
  314. /* If no timeout is set, just return NULL */
  315. if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
  316. return NULL;
  317. }
  318. /* Get current time */
  319. get_current_time(&timenow);
  320. /* If timer already expired, set remaining time to 0 */
  321. if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
  322. (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
  323. s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
  324. memset(timeleft, 0, sizeof(struct timeval));
  325. return timeleft;
  326. }
  327. /* Calculate time left until timer expires */
  328. memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval));
  329. timeleft->tv_sec -= timenow.tv_sec;
  330. timeleft->tv_usec -= timenow.tv_usec;
  331. if (timeleft->tv_usec < 0) {
  332. timeleft->tv_sec--;
  333. timeleft->tv_usec += 1000000;
  334. }
  335. /*
  336. * If remaining time is less than 15 ms, set it to 0 to prevent issues
  337. * because of small devergences with socket timeouts.
  338. */
  339. if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
  340. memset(timeleft, 0, sizeof(struct timeval));
  341. }
  342. return timeleft;
  343. }
  344. int dtls1_is_timer_expired(SSL *s)
  345. {
  346. struct timeval timeleft;
  347. /* Get time left until timeout, return false if no timer running */
  348. if (dtls1_get_timeout(s, &timeleft) == NULL) {
  349. return 0;
  350. }
  351. /* Return false if timer is not expired yet */
  352. if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
  353. return 0;
  354. }
  355. /* Timer expired, so return true */
  356. return 1;
  357. }
  358. void dtls1_double_timeout(SSL *s)
  359. {
  360. s->d1->timeout_duration *= 2;
  361. if (s->d1->timeout_duration > 60)
  362. s->d1->timeout_duration = 60;
  363. dtls1_start_timer(s);
  364. }
  365. void dtls1_stop_timer(SSL *s)
  366. {
  367. /* Reset everything */
  368. memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
  369. memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
  370. s->d1->timeout_duration = 1;
  371. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  372. &(s->d1->next_timeout));
  373. /* Clear retransmission buffer */
  374. dtls1_clear_record_buffer(s);
  375. }
  376. int dtls1_check_timeout_num(SSL *s)
  377. {
  378. s->d1->timeout.num_alerts++;
  379. /* Reduce MTU after 2 unsuccessful retransmissions */
  380. if (s->d1->timeout.num_alerts > 2) {
  381. s->d1->mtu =
  382. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
  383. NULL);
  384. }
  385. if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
  386. /* fail the connection, enough alerts have been sent */
  387. SSLerr(SSL_F_DTLS1_CHECK_TIMEOUT_NUM, SSL_R_READ_TIMEOUT_EXPIRED);
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. int dtls1_handle_timeout(SSL *s)
  393. {
  394. /* if no timer is expired, don't do anything */
  395. if (!dtls1_is_timer_expired(s)) {
  396. return 0;
  397. }
  398. dtls1_double_timeout(s);
  399. if (dtls1_check_timeout_num(s) < 0)
  400. return -1;
  401. s->d1->timeout.read_timeouts++;
  402. if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
  403. s->d1->timeout.read_timeouts = 1;
  404. }
  405. dtls1_start_timer(s);
  406. return dtls1_retransmit_buffered_messages(s);
  407. }
  408. static void get_current_time(struct timeval *t)
  409. {
  410. #ifdef OPENSSL_SYS_WIN32
  411. struct _timeb tb;
  412. _ftime(&tb);
  413. t->tv_sec = (long)tb.time;
  414. t->tv_usec = (long)tb.millitm * 1000;
  415. #elif defined(OPENSSL_SYS_VMS)
  416. struct timeb tb;
  417. ftime(&tb);
  418. t->tv_sec = (long)tb.time;
  419. t->tv_usec = (long)tb.millitm * 1000;
  420. #else
  421. gettimeofday(t, NULL);
  422. #endif
  423. }
  424. int dtls1_listen(SSL *s, struct sockaddr *client)
  425. {
  426. int ret;
  427. /* Ensure there is no state left over from a previous invocation */
  428. SSL_clear(s);
  429. SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
  430. s->d1->listen = 1;
  431. ret = SSL_accept(s);
  432. if (ret <= 0)
  433. return ret;
  434. (void)BIO_dgram_get_peer(SSL_get_rbio(s), client);
  435. return 1;
  436. }