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.
 
 
 
 
 
 

1325 lines
37 KiB

  1. /*
  2. * DTLS implementation written by Nagendra Modadugu
  3. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-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. *
  57. */
  58. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  59. * All rights reserved.
  60. *
  61. * This package is an SSL implementation written
  62. * by Eric Young (eay@cryptsoft.com).
  63. * The implementation was written so as to conform with Netscapes SSL.
  64. *
  65. * This library is free for commercial and non-commercial use as long as
  66. * the following conditions are aheared to. The following conditions
  67. * apply to all code found in this distribution, be it the RC4, RSA,
  68. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  69. * included with this distribution is covered by the same copyright terms
  70. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  71. *
  72. * Copyright remains Eric Young's, and as such any Copyright notices in
  73. * the code are not to be removed.
  74. * If this package is used in a product, Eric Young should be given attribution
  75. * as the author of the parts of the library used.
  76. * This can be in the form of a textual message at program startup or
  77. * in documentation (online or textual) provided with the package.
  78. *
  79. * Redistribution and use in source and binary forms, with or without
  80. * modification, are permitted provided that the following conditions
  81. * are met:
  82. * 1. Redistributions of source code must retain the copyright
  83. * notice, this list of conditions and the following disclaimer.
  84. * 2. Redistributions in binary form must reproduce the above copyright
  85. * notice, this list of conditions and the following disclaimer in the
  86. * documentation and/or other materials provided with the distribution.
  87. * 3. All advertising materials mentioning features or use of this software
  88. * must display the following acknowledgement:
  89. * "This product includes cryptographic software written by
  90. * Eric Young (eay@cryptsoft.com)"
  91. * The word 'cryptographic' can be left out if the rouines from the library
  92. * being used are not cryptographic related :-).
  93. * 4. If you include any Windows specific code (or a derivative thereof) from
  94. * the apps directory (application code) you must include an acknowledgement:
  95. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  96. *
  97. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  98. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  99. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  100. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  101. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  102. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  103. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  104. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  105. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  106. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  107. * SUCH DAMAGE.
  108. *
  109. * The licence and distribution terms for any publically available version or
  110. * derivative of this code cannot be changed. i.e. this code cannot simply be
  111. * copied and put under another distribution licence
  112. * [including the GNU Public Licence.] */
  113. #include <assert.h>
  114. #include <limits.h>
  115. #include <stdio.h>
  116. #include <string.h>
  117. #include <openssl/buf.h>
  118. #include <openssl/err.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/mem.h>
  121. #include <openssl/obj.h>
  122. #include <openssl/rand.h>
  123. #include <openssl/x509.h>
  124. #include "ssl_locl.h"
  125. #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
  126. #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
  127. if ((end) - (start) <= 8) { \
  128. long ii; \
  129. for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
  130. } else { \
  131. long ii; \
  132. bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
  133. for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
  134. bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
  135. } }
  136. #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
  137. long ii; \
  138. assert((msg_len) > 0); \
  139. is_complete = 1; \
  140. if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
  141. if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
  142. if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
  143. #if 0
  144. #define RSMBLY_BITMASK_PRINT(bitmask, msg_len) { \
  145. long ii; \
  146. printf("bitmask: "); for (ii = 0; ii < (msg_len); ii++) \
  147. printf("%d ", (bitmask[ii >> 3] & (1 << (ii & 7))) >> (ii & 7)); \
  148. printf("\n"); }
  149. #endif
  150. static const uint8_t bitmask_start_values[] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
  151. static const uint8_t bitmask_end_values[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
  152. /* XDTLS: figure out the right values */
  153. static const unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28};
  154. static unsigned int dtls1_guess_mtu(unsigned int curr_mtu);
  155. static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  156. unsigned long frag_len);
  157. static unsigned char *dtls1_write_message_header(SSL *s,
  158. unsigned char *p);
  159. static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
  160. unsigned long len, unsigned short seq_num, unsigned long frag_off,
  161. unsigned long frag_len);
  162. static long dtls1_get_message_fragment(SSL *s, int stn,
  163. long max, int *ok);
  164. static hm_fragment *
  165. dtls1_hm_fragment_new(unsigned long frag_len, int reassembly)
  166. {
  167. hm_fragment *frag = NULL;
  168. unsigned char *buf = NULL;
  169. unsigned char *bitmask = NULL;
  170. frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
  171. if ( frag == NULL)
  172. return NULL;
  173. if (frag_len)
  174. {
  175. buf = (unsigned char *)OPENSSL_malloc(frag_len);
  176. if ( buf == NULL)
  177. {
  178. OPENSSL_free(frag);
  179. return NULL;
  180. }
  181. }
  182. /* zero length fragment gets zero frag->fragment */
  183. frag->fragment = buf;
  184. /* Initialize reassembly bitmask if necessary */
  185. if (reassembly)
  186. {
  187. bitmask = (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
  188. if (bitmask == NULL)
  189. {
  190. if (buf != NULL) OPENSSL_free(buf);
  191. OPENSSL_free(frag);
  192. return NULL;
  193. }
  194. memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
  195. }
  196. frag->reassembly = bitmask;
  197. return frag;
  198. }
  199. static void
  200. dtls1_hm_fragment_free(hm_fragment *frag)
  201. {
  202. if (frag->msg_header.is_ccs)
  203. {
  204. EVP_CIPHER_CTX_free(frag->msg_header.saved_retransmit_state.enc_write_ctx);
  205. EVP_MD_CTX_destroy(frag->msg_header.saved_retransmit_state.write_hash);
  206. }
  207. if (frag->fragment) OPENSSL_free(frag->fragment);
  208. if (frag->reassembly) OPENSSL_free(frag->reassembly);
  209. OPENSSL_free(frag);
  210. }
  211. /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
  212. int dtls1_do_write(SSL *s, int type)
  213. {
  214. int ret;
  215. int curr_mtu;
  216. unsigned int len, frag_off, mac_size, blocksize;
  217. /* AHA! Figure out the MTU, and stick to the right size */
  218. if (s->d1->mtu < dtls1_min_mtu() && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU))
  219. {
  220. s->d1->mtu =
  221. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  222. /* I've seen the kernel return bogus numbers when it doesn't know
  223. * (initial write), so just make sure we have a reasonable number */
  224. if (s->d1->mtu < dtls1_min_mtu())
  225. {
  226. s->d1->mtu = 0;
  227. s->d1->mtu = dtls1_guess_mtu(s->d1->mtu);
  228. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
  229. s->d1->mtu, NULL);
  230. }
  231. }
  232. #if 0
  233. mtu = s->d1->mtu;
  234. fprintf(stderr, "using MTU = %d\n", mtu);
  235. mtu -= (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
  236. curr_mtu = mtu - BIO_wpending(SSL_get_wbio(s));
  237. if ( curr_mtu > 0)
  238. mtu = curr_mtu;
  239. else if ( ( ret = BIO_flush(SSL_get_wbio(s))) <= 0)
  240. return ret;
  241. if ( BIO_wpending(SSL_get_wbio(s)) + s->init_num >= mtu)
  242. {
  243. ret = BIO_flush(SSL_get_wbio(s));
  244. if ( ret <= 0)
  245. return ret;
  246. mtu = s->d1->mtu - (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
  247. }
  248. #endif
  249. assert(s->d1->mtu >= dtls1_min_mtu()); /* should have something reasonable now */
  250. if ( s->init_off == 0 && type == SSL3_RT_HANDSHAKE)
  251. assert(s->init_num ==
  252. (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
  253. if (s->write_hash)
  254. {
  255. if (s->enc_write_ctx && EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_GCM_MODE)
  256. mac_size = 0;
  257. else
  258. mac_size = EVP_MD_CTX_size(s->write_hash);
  259. }
  260. else
  261. mac_size = 0;
  262. if (s->enc_write_ctx &&
  263. (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))
  264. blocksize = 2 * EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
  265. else
  266. blocksize = 0;
  267. frag_off = 0;
  268. while( s->init_num)
  269. {
  270. curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
  271. DTLS1_RT_HEADER_LENGTH - mac_size - blocksize;
  272. if ( curr_mtu <= DTLS1_HM_HEADER_LENGTH)
  273. {
  274. /* grr.. we could get an error if MTU picked was wrong */
  275. ret = BIO_flush(SSL_get_wbio(s));
  276. if ( ret <= 0)
  277. return ret;
  278. curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH -
  279. mac_size - blocksize;
  280. }
  281. if ( s->init_num > curr_mtu)
  282. len = curr_mtu;
  283. else
  284. len = s->init_num;
  285. /* XDTLS: this function is too long. split out the CCS part */
  286. if ( type == SSL3_RT_HANDSHAKE)
  287. {
  288. if ( s->init_off != 0)
  289. {
  290. assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
  291. s->init_off -= DTLS1_HM_HEADER_LENGTH;
  292. s->init_num += DTLS1_HM_HEADER_LENGTH;
  293. if ( s->init_num > curr_mtu)
  294. len = curr_mtu;
  295. else
  296. len = s->init_num;
  297. }
  298. dtls1_fix_message_header(s, frag_off,
  299. len - DTLS1_HM_HEADER_LENGTH);
  300. dtls1_write_message_header(s, (unsigned char *)&s->init_buf->data[s->init_off]);
  301. assert(len >= DTLS1_HM_HEADER_LENGTH);
  302. }
  303. ret=dtls1_write_bytes(s,type,&s->init_buf->data[s->init_off],
  304. len);
  305. if (ret < 0)
  306. {
  307. /* might need to update MTU here, but we don't know
  308. * which previous packet caused the failure -- so can't
  309. * really retransmit anything. continue as if everything
  310. * is fine and wait for an alert to handle the
  311. * retransmit
  312. */
  313. if ( BIO_ctrl(SSL_get_wbio(s),
  314. BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0 )
  315. s->d1->mtu = BIO_ctrl(SSL_get_wbio(s),
  316. BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  317. else
  318. return(-1);
  319. }
  320. else
  321. {
  322. /* bad if this assert fails, only part of the handshake
  323. * message got sent. but why would this happen? */
  324. assert(len == (unsigned int)ret);
  325. if (type == SSL3_RT_HANDSHAKE && ! s->d1->retransmitting)
  326. {
  327. /* should not be done for 'Hello Request's, but in that case
  328. * we'll ignore the result anyway */
  329. unsigned char *p = (unsigned char *)&s->init_buf->data[s->init_off];
  330. const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  331. int xlen;
  332. if (frag_off == 0)
  333. {
  334. /* reconstruct message header is if it
  335. * is being sent in single fragment */
  336. *p++ = msg_hdr->type;
  337. l2n3(msg_hdr->msg_len,p);
  338. s2n (msg_hdr->seq,p);
  339. l2n3(0,p);
  340. l2n3(msg_hdr->msg_len,p);
  341. p -= DTLS1_HM_HEADER_LENGTH;
  342. xlen = ret;
  343. }
  344. else
  345. {
  346. p += DTLS1_HM_HEADER_LENGTH;
  347. xlen = ret - DTLS1_HM_HEADER_LENGTH;
  348. }
  349. ssl3_finish_mac(s, p, xlen);
  350. }
  351. if (ret == s->init_num)
  352. {
  353. if (s->msg_callback)
  354. s->msg_callback(1, s->version, type, s->init_buf->data,
  355. (size_t)(s->init_off + s->init_num), s,
  356. s->msg_callback_arg);
  357. s->init_off = 0; /* done writing this message */
  358. s->init_num = 0;
  359. return(1);
  360. }
  361. s->init_off+=ret;
  362. s->init_num-=ret;
  363. frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
  364. }
  365. }
  366. return(0);
  367. }
  368. /* Obtain handshake message of message type 'mt' (any if mt == -1),
  369. * maximum acceptable body length 'max'.
  370. * Read an entire handshake message. Handshake messages arrive in
  371. * fragments.
  372. */
  373. long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int hash_message, int *ok)
  374. {
  375. int i, al;
  376. struct hm_header_st *msg_hdr;
  377. unsigned char *p;
  378. unsigned long msg_len;
  379. /* s3->tmp is used to store messages that are unexpected, caused
  380. * by the absence of an optional handshake message */
  381. if (s->s3->tmp.reuse_message)
  382. {
  383. /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined
  384. * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE
  385. * would have to have been applied to the previous call. */
  386. assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE);
  387. s->s3->tmp.reuse_message=0;
  388. if ((mt >= 0) && (s->s3->tmp.message_type != mt))
  389. {
  390. al=SSL_AD_UNEXPECTED_MESSAGE;
  391. OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
  392. goto f_err;
  393. }
  394. *ok=1;
  395. s->init_msg = (uint8_t*)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  396. s->init_num = (int)s->s3->tmp.message_size;
  397. return s->init_num;
  398. }
  399. msg_hdr = &s->d1->r_msg_hdr;
  400. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  401. again:
  402. i = dtls1_get_message_fragment(s, stn, max, ok);
  403. if ( i == DTLS1_HM_BAD_FRAGMENT ||
  404. i == DTLS1_HM_FRAGMENT_RETRY) /* bad fragment received */
  405. goto again;
  406. else if ( i <= 0 && !*ok)
  407. return i;
  408. p = (unsigned char *)s->init_buf->data;
  409. msg_len = msg_hdr->msg_len;
  410. /* reconstruct message header */
  411. *(p++) = msg_hdr->type;
  412. l2n3(msg_len,p);
  413. s2n (msg_hdr->seq,p);
  414. l2n3(0,p);
  415. l2n3(msg_len,p);
  416. p -= DTLS1_HM_HEADER_LENGTH;
  417. msg_len += DTLS1_HM_HEADER_LENGTH;
  418. s->init_msg = (uint8_t*)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  419. if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE)
  420. ssl3_hash_current_message(s);
  421. if (s->msg_callback)
  422. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  423. p, msg_len,
  424. s, s->msg_callback_arg);
  425. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  426. /* Don't change sequence numbers while listening */
  427. if (!s->d1->listen)
  428. s->d1->handshake_read_seq++;
  429. return s->init_num;
  430. f_err:
  431. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  432. *ok = 0;
  433. return -1;
  434. }
  435. static int dtls1_preprocess_fragment(SSL *s,struct hm_header_st *msg_hdr,int max)
  436. {
  437. size_t frag_off,frag_len,msg_len;
  438. msg_len = msg_hdr->msg_len;
  439. frag_off = msg_hdr->frag_off;
  440. frag_len = msg_hdr->frag_len;
  441. /* sanity checking */
  442. if ( (frag_off+frag_len) > msg_len)
  443. {
  444. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  445. return SSL_AD_ILLEGAL_PARAMETER;
  446. }
  447. if ( (frag_off+frag_len) > (unsigned long)max)
  448. {
  449. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  450. return SSL_AD_ILLEGAL_PARAMETER;
  451. }
  452. if ( s->d1->r_msg_hdr.frag_off == 0) /* first fragment */
  453. {
  454. /* msg_len is limited to 2^24, but is effectively checked
  455. * against max above */
  456. if (!BUF_MEM_grow_clean(s->init_buf,msg_len+DTLS1_HM_HEADER_LENGTH))
  457. {
  458. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
  459. return SSL_AD_INTERNAL_ERROR;
  460. }
  461. s->s3->tmp.message_size = msg_len;
  462. s->d1->r_msg_hdr.msg_len = msg_len;
  463. s->s3->tmp.message_type = msg_hdr->type;
  464. s->d1->r_msg_hdr.type = msg_hdr->type;
  465. s->d1->r_msg_hdr.seq = msg_hdr->seq;
  466. }
  467. else if (msg_len != s->d1->r_msg_hdr.msg_len)
  468. {
  469. /* They must be playing with us! BTW, failure to enforce
  470. * upper limit would open possibility for buffer overrun. */
  471. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  472. return SSL_AD_ILLEGAL_PARAMETER;
  473. }
  474. return 0; /* no error */
  475. }
  476. static int
  477. dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok)
  478. {
  479. /* (0) check whether the desired fragment is available
  480. * if so:
  481. * (1) copy over the fragment to s->init_buf->data[]
  482. * (2) update s->init_num
  483. */
  484. pitem *item;
  485. hm_fragment *frag;
  486. int al;
  487. *ok = 0;
  488. item = pqueue_peek(s->d1->buffered_messages);
  489. if ( item == NULL)
  490. return 0;
  491. frag = (hm_fragment *)item->data;
  492. /* Don't return if reassembly still in progress */
  493. if (frag->reassembly != NULL)
  494. return 0;
  495. if ( s->d1->handshake_read_seq == frag->msg_header.seq)
  496. {
  497. unsigned long frag_len = frag->msg_header.frag_len;
  498. pqueue_pop(s->d1->buffered_messages);
  499. al=dtls1_preprocess_fragment(s,&frag->msg_header,max);
  500. if (al==0) /* no alert */
  501. {
  502. unsigned char *p = (unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
  503. memcpy(&p[frag->msg_header.frag_off],
  504. frag->fragment,frag->msg_header.frag_len);
  505. }
  506. dtls1_hm_fragment_free(frag);
  507. pitem_free(item);
  508. if (al==0)
  509. {
  510. *ok = 1;
  511. return frag_len;
  512. }
  513. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  514. s->init_num = 0;
  515. *ok = 0;
  516. return -1;
  517. }
  518. else
  519. return 0;
  520. }
  521. /* dtls1_max_handshake_message_len returns the maximum number of bytes
  522. * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
  523. * be greater if the maximum certificate list size requires it. */
  524. static unsigned long dtls1_max_handshake_message_len(const SSL *s)
  525. {
  526. unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  527. if (max_len < (unsigned long)s->max_cert_list)
  528. return s->max_cert_list;
  529. return max_len;
  530. }
  531. static int
  532. dtls1_reassemble_fragment(SSL *s, const struct hm_header_st* msg_hdr, int *ok)
  533. {
  534. hm_fragment *frag = NULL;
  535. pitem *item = NULL;
  536. int i = -1, is_complete;
  537. unsigned char seq64be[8];
  538. unsigned long frag_len = msg_hdr->frag_len;
  539. if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len ||
  540. msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
  541. goto err;
  542. if (frag_len == 0)
  543. return DTLS1_HM_FRAGMENT_RETRY;
  544. /* Try to find item in queue */
  545. memset(seq64be,0,sizeof(seq64be));
  546. seq64be[6] = (unsigned char) (msg_hdr->seq>>8);
  547. seq64be[7] = (unsigned char) msg_hdr->seq;
  548. item = pqueue_find(s->d1->buffered_messages, seq64be);
  549. if (item == NULL)
  550. {
  551. frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
  552. if ( frag == NULL)
  553. goto err;
  554. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  555. frag->msg_header.frag_len = frag->msg_header.msg_len;
  556. frag->msg_header.frag_off = 0;
  557. }
  558. else
  559. {
  560. frag = (hm_fragment*) item->data;
  561. if (frag->msg_header.msg_len != msg_hdr->msg_len)
  562. {
  563. item = NULL;
  564. frag = NULL;
  565. goto err;
  566. }
  567. }
  568. /* If message is already reassembled, this must be a
  569. * retransmit and can be dropped. In this case item != NULL and so frag
  570. * does not need to be freed. */
  571. if (frag->reassembly == NULL)
  572. {
  573. unsigned char devnull [256];
  574. assert(item != NULL);
  575. while (frag_len)
  576. {
  577. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  578. devnull,
  579. frag_len>sizeof(devnull)?sizeof(devnull):frag_len,0);
  580. if (i<=0) goto err;
  581. frag_len -= i;
  582. }
  583. return DTLS1_HM_FRAGMENT_RETRY;
  584. }
  585. /* read the body of the fragment (header has already been read */
  586. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  587. frag->fragment + msg_hdr->frag_off,frag_len,0);
  588. if ((unsigned long)i!=frag_len)
  589. i=-1;
  590. if (i<=0)
  591. goto err;
  592. RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
  593. (long)(msg_hdr->frag_off + frag_len));
  594. RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
  595. is_complete);
  596. if (is_complete)
  597. {
  598. OPENSSL_free(frag->reassembly);
  599. frag->reassembly = NULL;
  600. }
  601. if (item == NULL)
  602. {
  603. item = pitem_new(seq64be, frag);
  604. if (item == NULL)
  605. {
  606. i = -1;
  607. goto err;
  608. }
  609. item = pqueue_insert(s->d1->buffered_messages, item);
  610. /* pqueue_insert fails iff a duplicate item is inserted.
  611. * However, |item| cannot be a duplicate. If it were,
  612. * |pqueue_find|, above, would have returned it and control
  613. * would never have reached this branch. */
  614. assert(item != NULL);
  615. }
  616. return DTLS1_HM_FRAGMENT_RETRY;
  617. err:
  618. if (frag != NULL && item == NULL) dtls1_hm_fragment_free(frag);
  619. *ok = 0;
  620. return i;
  621. }
  622. static int
  623. dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st* msg_hdr, int *ok)
  624. {
  625. int i=-1;
  626. hm_fragment *frag = NULL;
  627. pitem *item = NULL;
  628. unsigned char seq64be[8];
  629. unsigned long frag_len = msg_hdr->frag_len;
  630. if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len)
  631. goto err;
  632. /* Try to find item in queue, to prevent duplicate entries */
  633. memset(seq64be,0,sizeof(seq64be));
  634. seq64be[6] = (unsigned char) (msg_hdr->seq>>8);
  635. seq64be[7] = (unsigned char) msg_hdr->seq;
  636. item = pqueue_find(s->d1->buffered_messages, seq64be);
  637. /* If we already have an entry and this one is a fragment,
  638. * don't discard it and rather try to reassemble it.
  639. */
  640. if (item != NULL && frag_len != msg_hdr->msg_len)
  641. item = NULL;
  642. /* Discard the message if sequence number was already there, is
  643. * too far in the future, already in the queue or if we received
  644. * a FINISHED before the SERVER_HELLO, which then must be a stale
  645. * retransmit.
  646. */
  647. if (msg_hdr->seq <= s->d1->handshake_read_seq ||
  648. msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
  649. (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED))
  650. {
  651. unsigned char devnull [256];
  652. while (frag_len)
  653. {
  654. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  655. devnull,
  656. frag_len>sizeof(devnull)?sizeof(devnull):frag_len,0);
  657. if (i<=0) goto err;
  658. frag_len -= i;
  659. }
  660. }
  661. else
  662. {
  663. if (frag_len != msg_hdr->msg_len)
  664. return dtls1_reassemble_fragment(s, msg_hdr, ok);
  665. if (frag_len > dtls1_max_handshake_message_len(s))
  666. goto err;
  667. frag = dtls1_hm_fragment_new(frag_len, 0);
  668. if ( frag == NULL)
  669. goto err;
  670. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  671. if (frag_len)
  672. {
  673. /* read the body of the fragment (header has already been read */
  674. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  675. frag->fragment,frag_len,0);
  676. if ((unsigned long)i!=frag_len)
  677. i = -1;
  678. if (i<=0)
  679. goto err;
  680. }
  681. item = pitem_new(seq64be, frag);
  682. if ( item == NULL)
  683. goto err;
  684. item = pqueue_insert(s->d1->buffered_messages, item);
  685. /* pqueue_insert fails iff a duplicate item is inserted.
  686. * However, |item| cannot be a duplicate. If it were,
  687. * |pqueue_find|, above, would have returned it. Then, either
  688. * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
  689. * to NULL and it will have been processed with
  690. * |dtls1_reassemble_fragment|, above, or the record will have
  691. * been discarded. */
  692. assert(item != NULL);
  693. }
  694. return DTLS1_HM_FRAGMENT_RETRY;
  695. err:
  696. if (frag != NULL && item == NULL) dtls1_hm_fragment_free(frag);
  697. *ok = 0;
  698. return i;
  699. }
  700. static long
  701. dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok)
  702. {
  703. unsigned char wire[DTLS1_HM_HEADER_LENGTH];
  704. unsigned long len, frag_off, frag_len;
  705. int i,al;
  706. struct hm_header_st msg_hdr;
  707. redo:
  708. /* see if we have the required fragment already */
  709. if ((frag_len = dtls1_retrieve_buffered_fragment(s,max,ok)) || *ok)
  710. {
  711. if (*ok) s->init_num = frag_len;
  712. return frag_len;
  713. }
  714. /* read handshake message header */
  715. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,wire,
  716. DTLS1_HM_HEADER_LENGTH, 0);
  717. if (i <= 0) /* nbio, or an error */
  718. {
  719. s->rwstate=SSL_READING;
  720. *ok = 0;
  721. return i;
  722. }
  723. /* Handshake fails if message header is incomplete */
  724. if (i != DTLS1_HM_HEADER_LENGTH)
  725. {
  726. al=SSL_AD_UNEXPECTED_MESSAGE;
  727. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, SSL_R_UNEXPECTED_MESSAGE);
  728. goto f_err;
  729. }
  730. /* parse the message fragment header */
  731. dtls1_get_message_header(wire, &msg_hdr);
  732. /*
  733. * if this is a future (or stale) message it gets buffered
  734. * (or dropped)--no further processing at this time
  735. * While listening, we accept seq 1 (ClientHello with cookie)
  736. * although we're still expecting seq 0 (ClientHello)
  737. */
  738. if (msg_hdr.seq != s->d1->handshake_read_seq && !(s->d1->listen && msg_hdr.seq == 1))
  739. return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
  740. len = msg_hdr.msg_len;
  741. frag_off = msg_hdr.frag_off;
  742. frag_len = msg_hdr.frag_len;
  743. if (frag_len && frag_len < len)
  744. return dtls1_reassemble_fragment(s, &msg_hdr, ok);
  745. if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
  746. wire[0] == SSL3_MT_HELLO_REQUEST)
  747. {
  748. /* The server may always send 'Hello Request' messages --
  749. * we are doing a handshake anyway now, so ignore them
  750. * if their format is correct. Does not count for
  751. * 'Finished' MAC. */
  752. if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0)
  753. {
  754. if (s->msg_callback)
  755. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  756. wire, DTLS1_HM_HEADER_LENGTH, s,
  757. s->msg_callback_arg);
  758. s->init_num = 0;
  759. goto redo;
  760. }
  761. else /* Incorrectly formated Hello request */
  762. {
  763. al=SSL_AD_UNEXPECTED_MESSAGE;
  764. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, SSL_R_UNEXPECTED_MESSAGE);
  765. goto f_err;
  766. }
  767. }
  768. if ((al=dtls1_preprocess_fragment(s,&msg_hdr,max)))
  769. goto f_err;
  770. /* XDTLS: ressurect this when restart is in place */
  771. s->state=stn;
  772. if ( frag_len > 0)
  773. {
  774. unsigned char *p=(unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
  775. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  776. &p[frag_off],frag_len,0);
  777. /* XDTLS: fix this--message fragments cannot span multiple packets */
  778. if (i <= 0)
  779. {
  780. s->rwstate=SSL_READING;
  781. *ok = 0;
  782. return i;
  783. }
  784. }
  785. else
  786. i = 0;
  787. /* XDTLS: an incorrectly formatted fragment should cause the
  788. * handshake to fail */
  789. if (i != (int)frag_len)
  790. {
  791. al=SSL3_AD_ILLEGAL_PARAMETER;
  792. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, SSL3_AD_ILLEGAL_PARAMETER);
  793. goto f_err;
  794. }
  795. *ok = 1;
  796. /* Note that s->init_num is *not* used as current offset in
  797. * s->init_buf->data, but as a counter summing up fragments'
  798. * lengths: as soon as they sum up to handshake packet
  799. * length, we assume we have got all the fragments. */
  800. s->init_num = frag_len;
  801. return frag_len;
  802. f_err:
  803. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  804. s->init_num = 0;
  805. *ok=0;
  806. return(-1);
  807. }
  808. /* for these 2 messages, we need to
  809. * ssl->enc_read_ctx re-init
  810. * ssl->s3->read_sequence zero
  811. * ssl->s3->read_mac_secret re-init
  812. * ssl->session->read_sym_enc assign
  813. * ssl->session->read_compression assign
  814. * ssl->session->read_hash assign
  815. */
  816. int dtls1_send_change_cipher_spec(SSL *s, int a, int b)
  817. {
  818. unsigned char *p;
  819. if (s->state == a)
  820. {
  821. p=(unsigned char *)s->init_buf->data;
  822. *p++=SSL3_MT_CCS;
  823. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  824. s->init_num=DTLS1_CCS_HEADER_LENGTH;
  825. s->init_off=0;
  826. dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
  827. s->d1->handshake_write_seq, 0, 0);
  828. /* buffer the message to handle re-xmits */
  829. dtls1_buffer_message(s, 1);
  830. s->state=b;
  831. }
  832. /* SSL3_ST_CW_CHANGE_B */
  833. return(dtls1_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC));
  834. }
  835. int dtls1_read_failed(SSL *s, int code)
  836. {
  837. if ( code > 0)
  838. {
  839. fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
  840. return 1;
  841. }
  842. if (!dtls1_is_timer_expired(s))
  843. {
  844. /* not a timeout, none of our business,
  845. let higher layers handle this. in fact it's probably an error */
  846. return code;
  847. }
  848. if (!SSL_in_init(s)) /* done, no need to send a retransmit */
  849. {
  850. BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
  851. return code;
  852. }
  853. #if 0 /* for now, each alert contains only one record number */
  854. item = pqueue_peek(state->rcvd_records);
  855. if ( item )
  856. {
  857. /* send an alert immediately for all the missing records */
  858. }
  859. else
  860. #endif
  861. #if 0 /* no more alert sending, just retransmit the last set of messages */
  862. if ( state->timeout.read_timeouts >= DTLS1_TMO_READ_COUNT)
  863. ssl3_send_alert(s,SSL3_AL_WARNING,
  864. DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
  865. #endif
  866. return dtls1_handle_timeout(s);
  867. }
  868. int
  869. dtls1_get_queue_priority(unsigned short seq, int is_ccs)
  870. {
  871. /* The index of the retransmission queue actually is the message sequence number,
  872. * since the queue only contains messages of a single handshake. However, the
  873. * ChangeCipherSpec has no message sequence number and so using only the sequence
  874. * will result in the CCS and Finished having the same index. To prevent this,
  875. * the sequence number is multiplied by 2. In case of a CCS 1 is subtracted.
  876. * This does not only differ CSS and Finished, it also maintains the order of the
  877. * index (important for priority queues) and fits in the unsigned short variable.
  878. */
  879. return seq * 2 - is_ccs;
  880. }
  881. int
  882. dtls1_retransmit_buffered_messages(SSL *s)
  883. {
  884. pqueue sent = s->d1->sent_messages;
  885. piterator iter;
  886. pitem *item;
  887. hm_fragment *frag;
  888. int found = 0;
  889. iter = pqueue_iterator(sent);
  890. for ( item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter))
  891. {
  892. frag = (hm_fragment *)item->data;
  893. if ( dtls1_retransmit_message(s,
  894. (unsigned short)dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs),
  895. 0, &found) <= 0 && found)
  896. {
  897. fprintf(stderr, "dtls1_retransmit_message() failed\n");
  898. return -1;
  899. }
  900. }
  901. return 1;
  902. }
  903. int
  904. dtls1_buffer_message(SSL *s, int is_ccs)
  905. {
  906. pitem *item;
  907. hm_fragment *frag;
  908. unsigned char seq64be[8];
  909. /* this function is called immediately after a message has
  910. * been serialized */
  911. assert(s->init_off == 0);
  912. frag = dtls1_hm_fragment_new(s->init_num, 0);
  913. if (!frag)
  914. return 0;
  915. memcpy(frag->fragment, s->init_buf->data, s->init_num);
  916. if ( is_ccs)
  917. {
  918. assert(s->d1->w_msg_hdr.msg_len +
  919. DTLS1_CCS_HEADER_LENGTH == (unsigned int)s->init_num);
  920. }
  921. else
  922. {
  923. assert(s->d1->w_msg_hdr.msg_len +
  924. DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
  925. }
  926. frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
  927. frag->msg_header.seq = s->d1->w_msg_hdr.seq;
  928. frag->msg_header.type = s->d1->w_msg_hdr.type;
  929. frag->msg_header.frag_off = 0;
  930. frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
  931. frag->msg_header.is_ccs = is_ccs;
  932. /* save current state*/
  933. frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
  934. frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
  935. frag->msg_header.saved_retransmit_state.session = s->session;
  936. frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
  937. memset(seq64be,0,sizeof(seq64be));
  938. seq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
  939. frag->msg_header.is_ccs)>>8);
  940. seq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
  941. frag->msg_header.is_ccs));
  942. item = pitem_new(seq64be, frag);
  943. if ( item == NULL)
  944. {
  945. dtls1_hm_fragment_free(frag);
  946. return 0;
  947. }
  948. #if 0
  949. fprintf( stderr, "buffered messge: \ttype = %xx\n", msg_buf->type);
  950. fprintf( stderr, "\t\t\t\t\tlen = %d\n", msg_buf->len);
  951. fprintf( stderr, "\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
  952. #endif
  953. pqueue_insert(s->d1->sent_messages, item);
  954. return 1;
  955. }
  956. int
  957. dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
  958. int *found)
  959. {
  960. int ret;
  961. /* XDTLS: for now assuming that read/writes are blocking */
  962. pitem *item;
  963. hm_fragment *frag ;
  964. unsigned long header_length;
  965. unsigned char seq64be[8];
  966. struct dtls1_retransmit_state saved_state;
  967. unsigned char save_write_sequence[8];
  968. /*
  969. assert(s->init_num == 0);
  970. assert(s->init_off == 0);
  971. */
  972. /* XDTLS: the requested message ought to be found, otherwise error */
  973. memset(seq64be,0,sizeof(seq64be));
  974. seq64be[6] = (unsigned char)(seq>>8);
  975. seq64be[7] = (unsigned char)seq;
  976. item = pqueue_find(s->d1->sent_messages, seq64be);
  977. if ( item == NULL)
  978. {
  979. fprintf(stderr, "retransmit: message %d non-existant\n", seq);
  980. *found = 0;
  981. return 0;
  982. }
  983. *found = 1;
  984. frag = (hm_fragment *)item->data;
  985. if ( frag->msg_header.is_ccs)
  986. header_length = DTLS1_CCS_HEADER_LENGTH;
  987. else
  988. header_length = DTLS1_HM_HEADER_LENGTH;
  989. memcpy(s->init_buf->data, frag->fragment,
  990. frag->msg_header.msg_len + header_length);
  991. s->init_num = frag->msg_header.msg_len + header_length;
  992. dtls1_set_message_header_int(s, frag->msg_header.type,
  993. frag->msg_header.msg_len, frag->msg_header.seq, 0,
  994. frag->msg_header.frag_len);
  995. /* save current state */
  996. saved_state.enc_write_ctx = s->enc_write_ctx;
  997. saved_state.write_hash = s->write_hash;
  998. saved_state.session = s->session;
  999. saved_state.epoch = s->d1->w_epoch;
  1000. s->d1->retransmitting = 1;
  1001. /* restore state in which the message was originally sent */
  1002. s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
  1003. s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
  1004. s->session = frag->msg_header.saved_retransmit_state.session;
  1005. s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
  1006. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1)
  1007. {
  1008. memcpy(save_write_sequence, s->s3->write_sequence, sizeof(s->s3->write_sequence));
  1009. memcpy(s->s3->write_sequence, s->d1->last_write_sequence, sizeof(s->s3->write_sequence));
  1010. }
  1011. ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
  1012. SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
  1013. /* restore current state */
  1014. s->enc_write_ctx = saved_state.enc_write_ctx;
  1015. s->write_hash = saved_state.write_hash;
  1016. s->session = saved_state.session;
  1017. s->d1->w_epoch = saved_state.epoch;
  1018. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1)
  1019. {
  1020. memcpy(s->d1->last_write_sequence, s->s3->write_sequence, sizeof(s->s3->write_sequence));
  1021. memcpy(s->s3->write_sequence, save_write_sequence, sizeof(s->s3->write_sequence));
  1022. }
  1023. s->d1->retransmitting = 0;
  1024. (void)BIO_flush(SSL_get_wbio(s));
  1025. return ret;
  1026. }
  1027. /* call this function when the buffered messages are no longer needed */
  1028. void
  1029. dtls1_clear_record_buffer(SSL *s)
  1030. {
  1031. pitem *item;
  1032. for(item = pqueue_pop(s->d1->sent_messages);
  1033. item != NULL; item = pqueue_pop(s->d1->sent_messages))
  1034. {
  1035. dtls1_hm_fragment_free((hm_fragment *)item->data);
  1036. pitem_free(item);
  1037. }
  1038. }
  1039. unsigned char *
  1040. dtls1_set_message_header(SSL *s, unsigned char *p, unsigned char mt,
  1041. unsigned long len, unsigned long frag_off, unsigned long frag_len)
  1042. {
  1043. /* Don't change sequence numbers while listening */
  1044. if (frag_off == 0 && !s->d1->listen)
  1045. {
  1046. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1047. s->d1->next_handshake_write_seq++;
  1048. }
  1049. dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
  1050. frag_off, frag_len);
  1051. return p += DTLS1_HM_HEADER_LENGTH;
  1052. }
  1053. /* don't actually do the writing, wait till the MTU has been retrieved */
  1054. static void
  1055. dtls1_set_message_header_int(SSL *s, unsigned char mt,
  1056. unsigned long len, unsigned short seq_num, unsigned long frag_off,
  1057. unsigned long frag_len)
  1058. {
  1059. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1060. msg_hdr->type = mt;
  1061. msg_hdr->msg_len = len;
  1062. msg_hdr->seq = seq_num;
  1063. msg_hdr->frag_off = frag_off;
  1064. msg_hdr->frag_len = frag_len;
  1065. }
  1066. static void
  1067. dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  1068. unsigned long frag_len)
  1069. {
  1070. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1071. msg_hdr->frag_off = frag_off;
  1072. msg_hdr->frag_len = frag_len;
  1073. }
  1074. static unsigned char *
  1075. dtls1_write_message_header(SSL *s, unsigned char *p)
  1076. {
  1077. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1078. *p++ = msg_hdr->type;
  1079. l2n3(msg_hdr->msg_len, p);
  1080. s2n(msg_hdr->seq, p);
  1081. l2n3(msg_hdr->frag_off, p);
  1082. l2n3(msg_hdr->frag_len, p);
  1083. return p;
  1084. }
  1085. unsigned int
  1086. dtls1_min_mtu(void)
  1087. {
  1088. return (g_probable_mtu[(sizeof(g_probable_mtu) /
  1089. sizeof(g_probable_mtu[0])) - 1]);
  1090. }
  1091. static unsigned int
  1092. dtls1_guess_mtu(unsigned int curr_mtu)
  1093. {
  1094. unsigned int i;
  1095. if ( curr_mtu == 0 )
  1096. return g_probable_mtu[0] ;
  1097. for ( i = 0; i < sizeof(g_probable_mtu)/sizeof(g_probable_mtu[0]); i++)
  1098. if ( curr_mtu > g_probable_mtu[i])
  1099. return g_probable_mtu[i];
  1100. return curr_mtu;
  1101. }
  1102. void
  1103. dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
  1104. {
  1105. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  1106. msg_hdr->type = *(data++);
  1107. n2l3(data, msg_hdr->msg_len);
  1108. n2s(data, msg_hdr->seq);
  1109. n2l3(data, msg_hdr->frag_off);
  1110. n2l3(data, msg_hdr->frag_len);
  1111. }
  1112. void
  1113. dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
  1114. {
  1115. memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
  1116. ccs_hdr->type = *(data++);
  1117. }
  1118. int dtls1_shutdown(SSL *s)
  1119. {
  1120. int ret;
  1121. ret = ssl3_shutdown(s);
  1122. return ret;
  1123. }