Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1306 rader
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, enum should_add_to_finished_hash should_add_to_finished_hash)
  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. should_add_to_finished_hash == add_to_finished_hash)
  327. {
  328. /* should not be done for 'Hello Request's, but in that case
  329. * we'll ignore the result anyway */
  330. unsigned char *p = (unsigned char *)&s->init_buf->data[s->init_off];
  331. const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  332. int xlen;
  333. if (frag_off == 0)
  334. {
  335. /* reconstruct message header is if it
  336. * is being sent in single fragment */
  337. *p++ = msg_hdr->type;
  338. l2n3(msg_hdr->msg_len,p);
  339. s2n (msg_hdr->seq,p);
  340. l2n3(0,p);
  341. l2n3(msg_hdr->msg_len,p);
  342. p -= DTLS1_HM_HEADER_LENGTH;
  343. xlen = ret;
  344. }
  345. else
  346. {
  347. p += DTLS1_HM_HEADER_LENGTH;
  348. xlen = ret - DTLS1_HM_HEADER_LENGTH;
  349. }
  350. ssl3_finish_mac(s, p, xlen);
  351. }
  352. if (ret == s->init_num)
  353. {
  354. if (s->msg_callback)
  355. s->msg_callback(1, s->version, type, s->init_buf->data,
  356. (size_t)(s->init_off + s->init_num), s,
  357. s->msg_callback_arg);
  358. s->init_off = 0; /* done writing this message */
  359. s->init_num = 0;
  360. return(1);
  361. }
  362. s->init_off+=ret;
  363. s->init_num-=ret;
  364. frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
  365. }
  366. }
  367. return(0);
  368. }
  369. /* Obtain handshake message of message type 'mt' (any if mt == -1),
  370. * maximum acceptable body length 'max'.
  371. * Read an entire handshake message. Handshake messages arrive in
  372. * fragments.
  373. */
  374. long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int hash_message, int *ok)
  375. {
  376. int i, al;
  377. struct hm_header_st *msg_hdr;
  378. unsigned char *p;
  379. unsigned long msg_len;
  380. /* s3->tmp is used to store messages that are unexpected, caused
  381. * by the absence of an optional handshake message */
  382. if (s->s3->tmp.reuse_message)
  383. {
  384. /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined
  385. * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE
  386. * would have to have been applied to the previous call. */
  387. assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE);
  388. s->s3->tmp.reuse_message=0;
  389. if ((mt >= 0) && (s->s3->tmp.message_type != mt))
  390. {
  391. al=SSL_AD_UNEXPECTED_MESSAGE;
  392. OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
  393. goto f_err;
  394. }
  395. *ok=1;
  396. s->init_msg = (uint8_t*)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  397. s->init_num = (int)s->s3->tmp.message_size;
  398. return s->init_num;
  399. }
  400. msg_hdr = &s->d1->r_msg_hdr;
  401. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  402. again:
  403. i = dtls1_get_message_fragment(s, stn, max, ok);
  404. if ( i == DTLS1_HM_BAD_FRAGMENT ||
  405. i == DTLS1_HM_FRAGMENT_RETRY) /* bad fragment received */
  406. goto again;
  407. else if ( i <= 0 && !*ok)
  408. return i;
  409. p = (unsigned char *)s->init_buf->data;
  410. msg_len = msg_hdr->msg_len;
  411. /* reconstruct message header */
  412. *(p++) = msg_hdr->type;
  413. l2n3(msg_len,p);
  414. s2n (msg_hdr->seq,p);
  415. l2n3(0,p);
  416. l2n3(msg_len,p);
  417. p -= DTLS1_HM_HEADER_LENGTH;
  418. msg_len += DTLS1_HM_HEADER_LENGTH;
  419. s->init_msg = (uint8_t*)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  420. if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE)
  421. ssl3_hash_current_message(s);
  422. if (s->msg_callback)
  423. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  424. p, msg_len,
  425. s, s->msg_callback_arg);
  426. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  427. s->d1->handshake_read_seq++;
  428. return s->init_num;
  429. f_err:
  430. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  431. *ok = 0;
  432. return -1;
  433. }
  434. static int dtls1_preprocess_fragment(SSL *s,struct hm_header_st *msg_hdr,int max)
  435. {
  436. size_t frag_off,frag_len,msg_len;
  437. msg_len = msg_hdr->msg_len;
  438. frag_off = msg_hdr->frag_off;
  439. frag_len = msg_hdr->frag_len;
  440. /* sanity checking */
  441. if ( (frag_off+frag_len) > msg_len)
  442. {
  443. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  444. return SSL_AD_ILLEGAL_PARAMETER;
  445. }
  446. if ( (frag_off+frag_len) > (unsigned long)max)
  447. {
  448. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  449. return SSL_AD_ILLEGAL_PARAMETER;
  450. }
  451. if ( s->d1->r_msg_hdr.frag_off == 0) /* first fragment */
  452. {
  453. /* msg_len is limited to 2^24, but is effectively checked
  454. * against max above */
  455. if (!BUF_MEM_grow_clean(s->init_buf,msg_len+DTLS1_HM_HEADER_LENGTH))
  456. {
  457. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
  458. return SSL_AD_INTERNAL_ERROR;
  459. }
  460. s->s3->tmp.message_size = msg_len;
  461. s->d1->r_msg_hdr.msg_len = msg_len;
  462. s->s3->tmp.message_type = msg_hdr->type;
  463. s->d1->r_msg_hdr.type = msg_hdr->type;
  464. s->d1->r_msg_hdr.seq = msg_hdr->seq;
  465. }
  466. else if (msg_len != s->d1->r_msg_hdr.msg_len)
  467. {
  468. /* They must be playing with us! BTW, failure to enforce
  469. * upper limit would open possibility for buffer overrun. */
  470. OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  471. return SSL_AD_ILLEGAL_PARAMETER;
  472. }
  473. return 0; /* no error */
  474. }
  475. static int
  476. dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok)
  477. {
  478. /* (0) check whether the desired fragment is available
  479. * if so:
  480. * (1) copy over the fragment to s->init_buf->data[]
  481. * (2) update s->init_num
  482. */
  483. pitem *item;
  484. hm_fragment *frag;
  485. int al;
  486. *ok = 0;
  487. item = pqueue_peek(s->d1->buffered_messages);
  488. if ( item == NULL)
  489. return 0;
  490. frag = (hm_fragment *)item->data;
  491. /* Don't return if reassembly still in progress */
  492. if (frag->reassembly != NULL)
  493. return 0;
  494. if ( s->d1->handshake_read_seq == frag->msg_header.seq)
  495. {
  496. unsigned long frag_len = frag->msg_header.frag_len;
  497. pqueue_pop(s->d1->buffered_messages);
  498. al=dtls1_preprocess_fragment(s,&frag->msg_header,max);
  499. if (al==0) /* no alert */
  500. {
  501. unsigned char *p = (unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
  502. memcpy(&p[frag->msg_header.frag_off],
  503. frag->fragment,frag->msg_header.frag_len);
  504. }
  505. dtls1_hm_fragment_free(frag);
  506. pitem_free(item);
  507. if (al==0)
  508. {
  509. *ok = 1;
  510. return frag_len;
  511. }
  512. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  513. s->init_num = 0;
  514. *ok = 0;
  515. return -1;
  516. }
  517. else
  518. return 0;
  519. }
  520. /* dtls1_max_handshake_message_len returns the maximum number of bytes
  521. * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
  522. * be greater if the maximum certificate list size requires it. */
  523. static unsigned long dtls1_max_handshake_message_len(const SSL *s)
  524. {
  525. unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  526. if (max_len < (unsigned long)s->max_cert_list)
  527. return s->max_cert_list;
  528. return max_len;
  529. }
  530. static int
  531. dtls1_reassemble_fragment(SSL *s, const struct hm_header_st* msg_hdr, int *ok)
  532. {
  533. hm_fragment *frag = NULL;
  534. pitem *item = NULL;
  535. int i = -1, is_complete;
  536. unsigned char seq64be[8];
  537. unsigned long frag_len = msg_hdr->frag_len;
  538. if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len ||
  539. msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
  540. goto err;
  541. if (frag_len == 0)
  542. return DTLS1_HM_FRAGMENT_RETRY;
  543. /* Try to find item in queue */
  544. memset(seq64be,0,sizeof(seq64be));
  545. seq64be[6] = (unsigned char) (msg_hdr->seq>>8);
  546. seq64be[7] = (unsigned char) msg_hdr->seq;
  547. item = pqueue_find(s->d1->buffered_messages, seq64be);
  548. if (item == NULL)
  549. {
  550. frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
  551. if ( frag == NULL)
  552. goto err;
  553. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  554. frag->msg_header.frag_len = frag->msg_header.msg_len;
  555. frag->msg_header.frag_off = 0;
  556. }
  557. else
  558. {
  559. frag = (hm_fragment*) item->data;
  560. if (frag->msg_header.msg_len != msg_hdr->msg_len)
  561. {
  562. item = NULL;
  563. frag = NULL;
  564. goto err;
  565. }
  566. }
  567. /* If message is already reassembled, this must be a
  568. * retransmit and can be dropped. In this case item != NULL and so frag
  569. * does not need to be freed. */
  570. if (frag->reassembly == NULL)
  571. {
  572. unsigned char devnull [256];
  573. assert(item != NULL);
  574. while (frag_len)
  575. {
  576. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  577. devnull,
  578. frag_len>sizeof(devnull)?sizeof(devnull):frag_len,0);
  579. if (i<=0) goto err;
  580. frag_len -= i;
  581. }
  582. return DTLS1_HM_FRAGMENT_RETRY;
  583. }
  584. /* read the body of the fragment (header has already been read */
  585. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  586. frag->fragment + msg_hdr->frag_off,frag_len,0);
  587. if ((unsigned long)i!=frag_len)
  588. i=-1;
  589. if (i<=0)
  590. goto err;
  591. RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
  592. (long)(msg_hdr->frag_off + frag_len));
  593. RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
  594. is_complete);
  595. if (is_complete)
  596. {
  597. OPENSSL_free(frag->reassembly);
  598. frag->reassembly = NULL;
  599. }
  600. if (item == NULL)
  601. {
  602. item = pitem_new(seq64be, frag);
  603. if (item == NULL)
  604. {
  605. i = -1;
  606. goto err;
  607. }
  608. item = pqueue_insert(s->d1->buffered_messages, item);
  609. /* pqueue_insert fails iff a duplicate item is inserted.
  610. * However, |item| cannot be a duplicate. If it were,
  611. * |pqueue_find|, above, would have returned it and control
  612. * would never have reached this branch. */
  613. assert(item != NULL);
  614. }
  615. return DTLS1_HM_FRAGMENT_RETRY;
  616. err:
  617. if (frag != NULL && item == NULL) dtls1_hm_fragment_free(frag);
  618. *ok = 0;
  619. return i;
  620. }
  621. static int
  622. dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st* msg_hdr, int *ok)
  623. {
  624. int i=-1;
  625. hm_fragment *frag = NULL;
  626. pitem *item = NULL;
  627. unsigned char seq64be[8];
  628. unsigned long frag_len = msg_hdr->frag_len;
  629. if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len)
  630. goto err;
  631. /* Try to find item in queue, to prevent duplicate entries */
  632. memset(seq64be,0,sizeof(seq64be));
  633. seq64be[6] = (unsigned char) (msg_hdr->seq>>8);
  634. seq64be[7] = (unsigned char) msg_hdr->seq;
  635. item = pqueue_find(s->d1->buffered_messages, seq64be);
  636. /* If we already have an entry and this one is a fragment,
  637. * don't discard it and rather try to reassemble it.
  638. */
  639. if (item != NULL && frag_len != msg_hdr->msg_len)
  640. item = NULL;
  641. /* Discard the message if sequence number was already there, is
  642. * too far in the future, already in the queue or if we received
  643. * a FINISHED before the SERVER_HELLO, which then must be a stale
  644. * retransmit.
  645. */
  646. if (msg_hdr->seq <= s->d1->handshake_read_seq ||
  647. msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
  648. (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED))
  649. {
  650. unsigned char devnull [256];
  651. while (frag_len)
  652. {
  653. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  654. devnull,
  655. frag_len>sizeof(devnull)?sizeof(devnull):frag_len,0);
  656. if (i<=0) goto err;
  657. frag_len -= i;
  658. }
  659. }
  660. else
  661. {
  662. if (frag_len != msg_hdr->msg_len)
  663. return dtls1_reassemble_fragment(s, msg_hdr, ok);
  664. if (frag_len > dtls1_max_handshake_message_len(s))
  665. goto err;
  666. frag = dtls1_hm_fragment_new(frag_len, 0);
  667. if ( frag == NULL)
  668. goto err;
  669. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  670. if (frag_len)
  671. {
  672. /* read the body of the fragment (header has already been read */
  673. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  674. frag->fragment,frag_len,0);
  675. if ((unsigned long)i!=frag_len)
  676. i = -1;
  677. if (i<=0)
  678. goto err;
  679. }
  680. item = pitem_new(seq64be, frag);
  681. if ( item == NULL)
  682. goto err;
  683. item = pqueue_insert(s->d1->buffered_messages, item);
  684. /* pqueue_insert fails iff a duplicate item is inserted.
  685. * However, |item| cannot be a duplicate. If it were,
  686. * |pqueue_find|, above, would have returned it. Then, either
  687. * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
  688. * to NULL and it will have been processed with
  689. * |dtls1_reassemble_fragment|, above, or the record will have
  690. * been discarded. */
  691. assert(item != NULL);
  692. }
  693. return DTLS1_HM_FRAGMENT_RETRY;
  694. err:
  695. if (frag != NULL && item == NULL) dtls1_hm_fragment_free(frag);
  696. *ok = 0;
  697. return i;
  698. }
  699. static long
  700. dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok)
  701. {
  702. unsigned char wire[DTLS1_HM_HEADER_LENGTH];
  703. unsigned long len, frag_off, frag_len;
  704. int i,al;
  705. struct hm_header_st msg_hdr;
  706. redo:
  707. /* see if we have the required fragment already */
  708. if ((frag_len = dtls1_retrieve_buffered_fragment(s,max,ok)) || *ok)
  709. {
  710. if (*ok) s->init_num = frag_len;
  711. return frag_len;
  712. }
  713. /* read handshake message header */
  714. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,wire,
  715. DTLS1_HM_HEADER_LENGTH, 0);
  716. if (i <= 0) /* nbio, or an error */
  717. {
  718. s->rwstate=SSL_READING;
  719. *ok = 0;
  720. return i;
  721. }
  722. /* Handshake fails if message header is incomplete */
  723. if (i != DTLS1_HM_HEADER_LENGTH)
  724. {
  725. al=SSL_AD_UNEXPECTED_MESSAGE;
  726. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, SSL_R_UNEXPECTED_MESSAGE);
  727. goto f_err;
  728. }
  729. /* parse the message fragment header */
  730. dtls1_get_message_header(wire, &msg_hdr);
  731. /*
  732. * if this is a future (or stale) message it gets buffered
  733. * (or dropped)--no further processing at this time
  734. */
  735. if (msg_hdr.seq != s->d1->handshake_read_seq)
  736. return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
  737. len = msg_hdr.msg_len;
  738. frag_off = msg_hdr.frag_off;
  739. frag_len = msg_hdr.frag_len;
  740. if (frag_len && frag_len < len)
  741. return dtls1_reassemble_fragment(s, &msg_hdr, ok);
  742. if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
  743. wire[0] == SSL3_MT_HELLO_REQUEST)
  744. {
  745. /* The server may always send 'Hello Request' messages --
  746. * we are doing a handshake anyway now, so ignore them
  747. * if their format is correct. Does not count for
  748. * 'Finished' MAC. */
  749. if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0)
  750. {
  751. if (s->msg_callback)
  752. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  753. wire, DTLS1_HM_HEADER_LENGTH, s,
  754. s->msg_callback_arg);
  755. s->init_num = 0;
  756. goto redo;
  757. }
  758. else /* Incorrectly formated Hello request */
  759. {
  760. al=SSL_AD_UNEXPECTED_MESSAGE;
  761. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, SSL_R_UNEXPECTED_MESSAGE);
  762. goto f_err;
  763. }
  764. }
  765. if ((al=dtls1_preprocess_fragment(s,&msg_hdr,max)))
  766. goto f_err;
  767. /* XDTLS: ressurect this when restart is in place */
  768. s->state=stn;
  769. if ( frag_len > 0)
  770. {
  771. unsigned char *p=(unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
  772. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  773. &p[frag_off],frag_len,0);
  774. /* XDTLS: fix this--message fragments cannot span multiple packets */
  775. if (i <= 0)
  776. {
  777. s->rwstate=SSL_READING;
  778. *ok = 0;
  779. return i;
  780. }
  781. }
  782. else
  783. i = 0;
  784. /* XDTLS: an incorrectly formatted fragment should cause the
  785. * handshake to fail */
  786. if (i != (int)frag_len)
  787. {
  788. al=SSL3_AD_ILLEGAL_PARAMETER;
  789. OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, SSL3_AD_ILLEGAL_PARAMETER);
  790. goto f_err;
  791. }
  792. *ok = 1;
  793. /* Note that s->init_num is *not* used as current offset in
  794. * s->init_buf->data, but as a counter summing up fragments'
  795. * lengths: as soon as they sum up to handshake packet
  796. * length, we assume we have got all the fragments. */
  797. s->init_num = frag_len;
  798. return frag_len;
  799. f_err:
  800. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  801. s->init_num = 0;
  802. *ok=0;
  803. return(-1);
  804. }
  805. /* for these 2 messages, we need to
  806. * ssl->enc_read_ctx re-init
  807. * ssl->s3->read_sequence zero
  808. * ssl->s3->read_mac_secret re-init
  809. * ssl->session->read_sym_enc assign
  810. * ssl->session->read_compression assign
  811. * ssl->session->read_hash assign
  812. */
  813. int dtls1_send_change_cipher_spec(SSL *s, int a, int b)
  814. {
  815. unsigned char *p;
  816. if (s->state == a)
  817. {
  818. p=(unsigned char *)s->init_buf->data;
  819. *p++=SSL3_MT_CCS;
  820. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  821. s->init_num=DTLS1_CCS_HEADER_LENGTH;
  822. s->init_off=0;
  823. dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
  824. s->d1->handshake_write_seq, 0, 0);
  825. /* buffer the message to handle re-xmits */
  826. dtls1_buffer_message(s, 1);
  827. s->state=b;
  828. }
  829. /* SSL3_ST_CW_CHANGE_B */
  830. return(dtls1_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC, dont_add_to_finished_hash));
  831. }
  832. int dtls1_read_failed(SSL *s, int code)
  833. {
  834. if ( code > 0)
  835. {
  836. fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
  837. return 1;
  838. }
  839. if (!dtls1_is_timer_expired(s))
  840. {
  841. /* not a timeout, none of our business,
  842. let higher layers handle this. in fact it's probably an error */
  843. return code;
  844. }
  845. if (!SSL_in_init(s)) /* done, no need to send a retransmit */
  846. {
  847. BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
  848. return code;
  849. }
  850. return dtls1_handle_timeout(s);
  851. }
  852. int
  853. dtls1_get_queue_priority(unsigned short seq, int is_ccs)
  854. {
  855. /* The index of the retransmission queue actually is the message sequence number,
  856. * since the queue only contains messages of a single handshake. However, the
  857. * ChangeCipherSpec has no message sequence number and so using only the sequence
  858. * will result in the CCS and Finished having the same index. To prevent this,
  859. * the sequence number is multiplied by 2. In case of a CCS 1 is subtracted.
  860. * This does not only differ CSS and Finished, it also maintains the order of the
  861. * index (important for priority queues) and fits in the unsigned short variable.
  862. */
  863. return seq * 2 - is_ccs;
  864. }
  865. int
  866. dtls1_retransmit_buffered_messages(SSL *s)
  867. {
  868. pqueue sent = s->d1->sent_messages;
  869. piterator iter;
  870. pitem *item;
  871. hm_fragment *frag;
  872. int found = 0;
  873. iter = pqueue_iterator(sent);
  874. for ( item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter))
  875. {
  876. frag = (hm_fragment *)item->data;
  877. if ( dtls1_retransmit_message(s,
  878. (unsigned short)dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs),
  879. 0, &found) <= 0 && found)
  880. {
  881. fprintf(stderr, "dtls1_retransmit_message() failed\n");
  882. return -1;
  883. }
  884. }
  885. return 1;
  886. }
  887. int
  888. dtls1_buffer_message(SSL *s, int is_ccs)
  889. {
  890. pitem *item;
  891. hm_fragment *frag;
  892. unsigned char seq64be[8];
  893. /* this function is called immediately after a message has
  894. * been serialized */
  895. assert(s->init_off == 0);
  896. frag = dtls1_hm_fragment_new(s->init_num, 0);
  897. if (!frag)
  898. return 0;
  899. memcpy(frag->fragment, s->init_buf->data, s->init_num);
  900. if ( is_ccs)
  901. {
  902. assert(s->d1->w_msg_hdr.msg_len +
  903. DTLS1_CCS_HEADER_LENGTH == (unsigned int)s->init_num);
  904. }
  905. else
  906. {
  907. assert(s->d1->w_msg_hdr.msg_len +
  908. DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
  909. }
  910. frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
  911. frag->msg_header.seq = s->d1->w_msg_hdr.seq;
  912. frag->msg_header.type = s->d1->w_msg_hdr.type;
  913. frag->msg_header.frag_off = 0;
  914. frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
  915. frag->msg_header.is_ccs = is_ccs;
  916. /* save current state*/
  917. frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
  918. frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
  919. frag->msg_header.saved_retransmit_state.session = s->session;
  920. frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
  921. memset(seq64be,0,sizeof(seq64be));
  922. seq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
  923. frag->msg_header.is_ccs)>>8);
  924. seq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
  925. frag->msg_header.is_ccs));
  926. item = pitem_new(seq64be, frag);
  927. if ( item == NULL)
  928. {
  929. dtls1_hm_fragment_free(frag);
  930. return 0;
  931. }
  932. #if 0
  933. fprintf( stderr, "buffered messge: \ttype = %xx\n", msg_buf->type);
  934. fprintf( stderr, "\t\t\t\t\tlen = %d\n", msg_buf->len);
  935. fprintf( stderr, "\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
  936. #endif
  937. pqueue_insert(s->d1->sent_messages, item);
  938. return 1;
  939. }
  940. int
  941. dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
  942. int *found)
  943. {
  944. int ret;
  945. /* XDTLS: for now assuming that read/writes are blocking */
  946. pitem *item;
  947. hm_fragment *frag ;
  948. unsigned long header_length;
  949. unsigned char seq64be[8];
  950. struct dtls1_retransmit_state saved_state;
  951. unsigned char save_write_sequence[8];
  952. /*
  953. assert(s->init_num == 0);
  954. assert(s->init_off == 0);
  955. */
  956. /* XDTLS: the requested message ought to be found, otherwise error */
  957. memset(seq64be,0,sizeof(seq64be));
  958. seq64be[6] = (unsigned char)(seq>>8);
  959. seq64be[7] = (unsigned char)seq;
  960. item = pqueue_find(s->d1->sent_messages, seq64be);
  961. if ( item == NULL)
  962. {
  963. fprintf(stderr, "retransmit: message %d non-existant\n", seq);
  964. *found = 0;
  965. return 0;
  966. }
  967. *found = 1;
  968. frag = (hm_fragment *)item->data;
  969. if ( frag->msg_header.is_ccs)
  970. header_length = DTLS1_CCS_HEADER_LENGTH;
  971. else
  972. header_length = DTLS1_HM_HEADER_LENGTH;
  973. memcpy(s->init_buf->data, frag->fragment,
  974. frag->msg_header.msg_len + header_length);
  975. s->init_num = frag->msg_header.msg_len + header_length;
  976. dtls1_set_message_header_int(s, frag->msg_header.type,
  977. frag->msg_header.msg_len, frag->msg_header.seq, 0,
  978. frag->msg_header.frag_len);
  979. /* save current state */
  980. saved_state.enc_write_ctx = s->enc_write_ctx;
  981. saved_state.write_hash = s->write_hash;
  982. saved_state.session = s->session;
  983. saved_state.epoch = s->d1->w_epoch;
  984. s->d1->retransmitting = 1;
  985. /* restore state in which the message was originally sent */
  986. s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
  987. s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
  988. s->session = frag->msg_header.saved_retransmit_state.session;
  989. s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
  990. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1)
  991. {
  992. memcpy(save_write_sequence, s->s3->write_sequence, sizeof(s->s3->write_sequence));
  993. memcpy(s->s3->write_sequence, s->d1->last_write_sequence, sizeof(s->s3->write_sequence));
  994. }
  995. ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
  996. SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE, add_to_finished_hash);
  997. /* restore current state */
  998. s->enc_write_ctx = saved_state.enc_write_ctx;
  999. s->write_hash = saved_state.write_hash;
  1000. s->session = saved_state.session;
  1001. s->d1->w_epoch = saved_state.epoch;
  1002. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1)
  1003. {
  1004. memcpy(s->d1->last_write_sequence, s->s3->write_sequence, sizeof(s->s3->write_sequence));
  1005. memcpy(s->s3->write_sequence, save_write_sequence, sizeof(s->s3->write_sequence));
  1006. }
  1007. s->d1->retransmitting = 0;
  1008. (void)BIO_flush(SSL_get_wbio(s));
  1009. return ret;
  1010. }
  1011. /* call this function when the buffered messages are no longer needed */
  1012. void
  1013. dtls1_clear_record_buffer(SSL *s)
  1014. {
  1015. pitem *item;
  1016. for(item = pqueue_pop(s->d1->sent_messages);
  1017. item != NULL; item = pqueue_pop(s->d1->sent_messages))
  1018. {
  1019. dtls1_hm_fragment_free((hm_fragment *)item->data);
  1020. pitem_free(item);
  1021. }
  1022. }
  1023. unsigned char *
  1024. dtls1_set_message_header(SSL *s, unsigned char *p, unsigned char mt,
  1025. unsigned long len, unsigned long frag_off, unsigned long frag_len)
  1026. {
  1027. if (frag_off == 0)
  1028. {
  1029. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1030. s->d1->next_handshake_write_seq++;
  1031. }
  1032. dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
  1033. frag_off, frag_len);
  1034. return p += DTLS1_HM_HEADER_LENGTH;
  1035. }
  1036. /* don't actually do the writing, wait till the MTU has been retrieved */
  1037. static void
  1038. dtls1_set_message_header_int(SSL *s, unsigned char mt,
  1039. unsigned long len, unsigned short seq_num, unsigned long frag_off,
  1040. unsigned long frag_len)
  1041. {
  1042. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1043. msg_hdr->type = mt;
  1044. msg_hdr->msg_len = len;
  1045. msg_hdr->seq = seq_num;
  1046. msg_hdr->frag_off = frag_off;
  1047. msg_hdr->frag_len = frag_len;
  1048. }
  1049. static void
  1050. dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  1051. unsigned long frag_len)
  1052. {
  1053. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1054. msg_hdr->frag_off = frag_off;
  1055. msg_hdr->frag_len = frag_len;
  1056. }
  1057. static unsigned char *
  1058. dtls1_write_message_header(SSL *s, unsigned char *p)
  1059. {
  1060. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1061. *p++ = msg_hdr->type;
  1062. l2n3(msg_hdr->msg_len, p);
  1063. s2n(msg_hdr->seq, p);
  1064. l2n3(msg_hdr->frag_off, p);
  1065. l2n3(msg_hdr->frag_len, p);
  1066. return p;
  1067. }
  1068. unsigned int
  1069. dtls1_min_mtu(void)
  1070. {
  1071. return (g_probable_mtu[(sizeof(g_probable_mtu) /
  1072. sizeof(g_probable_mtu[0])) - 1]);
  1073. }
  1074. static unsigned int
  1075. dtls1_guess_mtu(unsigned int curr_mtu)
  1076. {
  1077. unsigned int i;
  1078. if ( curr_mtu == 0 )
  1079. return g_probable_mtu[0] ;
  1080. for ( i = 0; i < sizeof(g_probable_mtu)/sizeof(g_probable_mtu[0]); i++)
  1081. if ( curr_mtu > g_probable_mtu[i])
  1082. return g_probable_mtu[i];
  1083. return curr_mtu;
  1084. }
  1085. void
  1086. dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
  1087. {
  1088. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  1089. msg_hdr->type = *(data++);
  1090. n2l3(data, msg_hdr->msg_len);
  1091. n2s(data, msg_hdr->seq);
  1092. n2l3(data, msg_hdr->frag_off);
  1093. n2l3(data, msg_hdr->frag_len);
  1094. }
  1095. void
  1096. dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
  1097. {
  1098. memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
  1099. ccs_hdr->type = *(data++);
  1100. }
  1101. int dtls1_shutdown(SSL *s)
  1102. {
  1103. int ret;
  1104. ret = ssl3_shutdown(s);
  1105. return ret;
  1106. }