d1_both.c 39 KB

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