Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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