d1_both.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. /* XDTLS: figure out the right values */
  125. static unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28};
  126. static unsigned int dtls1_min_mtu(void);
  127. static unsigned int dtls1_guess_mtu(unsigned int curr_mtu);
  128. static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  129. unsigned long frag_len);
  130. static unsigned char *dtls1_write_message_header(SSL *s,
  131. unsigned char *p);
  132. static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
  133. unsigned long len, unsigned short seq_num, unsigned long frag_off,
  134. unsigned long frag_len);
  135. static long dtls1_get_message_fragment(SSL *s, int st1, int stn,
  136. long max, int *ok);
  137. static hm_fragment *
  138. dtls1_hm_fragment_new(unsigned long frag_len)
  139. {
  140. hm_fragment *frag = NULL;
  141. unsigned char *buf = NULL;
  142. frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
  143. if ( frag == NULL)
  144. return NULL;
  145. if (frag_len)
  146. {
  147. buf = (unsigned char *)OPENSSL_malloc(frag_len);
  148. if ( buf == NULL)
  149. {
  150. OPENSSL_free(frag);
  151. return NULL;
  152. }
  153. }
  154. /* zero length fragment gets zero frag->fragment */
  155. frag->fragment = buf;
  156. return frag;
  157. }
  158. static void
  159. dtls1_hm_fragment_free(hm_fragment *frag)
  160. {
  161. if (frag->fragment) OPENSSL_free(frag->fragment);
  162. OPENSSL_free(frag);
  163. }
  164. /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
  165. int dtls1_do_write(SSL *s, int type)
  166. {
  167. int ret;
  168. int curr_mtu;
  169. unsigned int len, frag_off, mac_size, blocksize;
  170. /* AHA! Figure out the MTU, and stick to the right size */
  171. if ( ! (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU))
  172. {
  173. s->d1->mtu =
  174. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  175. /* I've seen the kernel return bogus numbers when it doesn't know
  176. * (initial write), so just make sure we have a reasonable number */
  177. if ( s->d1->mtu < dtls1_min_mtu())
  178. {
  179. s->d1->mtu = 0;
  180. s->d1->mtu = dtls1_guess_mtu(s->d1->mtu);
  181. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
  182. s->d1->mtu, NULL);
  183. }
  184. }
  185. #if 0
  186. mtu = s->d1->mtu;
  187. fprintf(stderr, "using MTU = %d\n", mtu);
  188. mtu -= (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
  189. curr_mtu = mtu - BIO_wpending(SSL_get_wbio(s));
  190. if ( curr_mtu > 0)
  191. mtu = curr_mtu;
  192. else if ( ( ret = BIO_flush(SSL_get_wbio(s))) <= 0)
  193. return ret;
  194. if ( BIO_wpending(SSL_get_wbio(s)) + s->init_num >= mtu)
  195. {
  196. ret = BIO_flush(SSL_get_wbio(s));
  197. if ( ret <= 0)
  198. return ret;
  199. mtu = s->d1->mtu - (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
  200. }
  201. OPENSSL_assert(mtu > 0); /* should have something reasonable now */
  202. #endif
  203. if ( s->init_off == 0 && type == SSL3_RT_HANDSHAKE)
  204. OPENSSL_assert(s->init_num ==
  205. (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
  206. if (s->write_hash)
  207. mac_size = EVP_MD_CTX_size(s->write_hash);
  208. else
  209. mac_size = 0;
  210. if (s->enc_write_ctx &&
  211. (EVP_CIPHER_mode( s->enc_write_ctx->cipher) & EVP_CIPH_CBC_MODE))
  212. blocksize = 2 * EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
  213. else
  214. blocksize = 0;
  215. frag_off = 0;
  216. while( s->init_num)
  217. {
  218. curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
  219. DTLS1_RT_HEADER_LENGTH - mac_size - blocksize;
  220. if ( curr_mtu <= DTLS1_HM_HEADER_LENGTH)
  221. {
  222. /* grr.. we could get an error if MTU picked was wrong */
  223. ret = BIO_flush(SSL_get_wbio(s));
  224. if ( ret <= 0)
  225. return ret;
  226. curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH -
  227. mac_size - blocksize;
  228. }
  229. if ( s->init_num > curr_mtu)
  230. len = curr_mtu;
  231. else
  232. len = s->init_num;
  233. /* XDTLS: this function is too long. split out the CCS part */
  234. if ( type == SSL3_RT_HANDSHAKE)
  235. {
  236. if ( s->init_off != 0)
  237. {
  238. OPENSSL_assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
  239. s->init_off -= DTLS1_HM_HEADER_LENGTH;
  240. s->init_num += DTLS1_HM_HEADER_LENGTH;
  241. /* write atleast DTLS1_HM_HEADER_LENGTH bytes */
  242. if ( len <= DTLS1_HM_HEADER_LENGTH)
  243. len += DTLS1_HM_HEADER_LENGTH;
  244. }
  245. dtls1_fix_message_header(s, frag_off,
  246. len - DTLS1_HM_HEADER_LENGTH);
  247. dtls1_write_message_header(s, (unsigned char *)&s->init_buf->data[s->init_off]);
  248. OPENSSL_assert(len >= DTLS1_HM_HEADER_LENGTH);
  249. }
  250. ret=dtls1_write_bytes(s,type,&s->init_buf->data[s->init_off],
  251. len);
  252. if (ret < 0)
  253. {
  254. /* might need to update MTU here, but we don't know
  255. * which previous packet caused the failure -- so can't
  256. * really retransmit anything. continue as if everything
  257. * is fine and wait for an alert to handle the
  258. * retransmit
  259. */
  260. if ( BIO_ctrl(SSL_get_wbio(s),
  261. BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0 )
  262. s->d1->mtu = BIO_ctrl(SSL_get_wbio(s),
  263. BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  264. else
  265. return(-1);
  266. }
  267. else
  268. {
  269. /* bad if this assert fails, only part of the handshake
  270. * message got sent. but why would this happen? */
  271. OPENSSL_assert(len == (unsigned int)ret);
  272. if (type == SSL3_RT_HANDSHAKE && ! s->d1->retransmitting)
  273. {
  274. /* should not be done for 'Hello Request's, but in that case
  275. * we'll ignore the result anyway */
  276. unsigned char *p = (unsigned char *)&s->init_buf->data[s->init_off];
  277. const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  278. int xlen;
  279. if (frag_off == 0 && s->version != DTLS1_BAD_VER)
  280. {
  281. /* reconstruct message header is if it
  282. * is being sent in single fragment */
  283. *p++ = msg_hdr->type;
  284. l2n3(msg_hdr->msg_len,p);
  285. s2n (msg_hdr->seq,p);
  286. l2n3(0,p);
  287. l2n3(msg_hdr->msg_len,p);
  288. p -= DTLS1_HM_HEADER_LENGTH;
  289. xlen = ret;
  290. }
  291. else
  292. {
  293. p += DTLS1_HM_HEADER_LENGTH;
  294. xlen = ret - DTLS1_HM_HEADER_LENGTH;
  295. }
  296. ssl3_finish_mac(s, p, xlen);
  297. }
  298. if (ret == s->init_num)
  299. {
  300. if (s->msg_callback)
  301. s->msg_callback(1, s->version, type, s->init_buf->data,
  302. (size_t)(s->init_off + s->init_num), s,
  303. s->msg_callback_arg);
  304. s->init_off = 0; /* done writing this message */
  305. s->init_num = 0;
  306. return(1);
  307. }
  308. s->init_off+=ret;
  309. s->init_num-=ret;
  310. frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
  311. }
  312. }
  313. return(0);
  314. }
  315. /* Obtain handshake message of message type 'mt' (any if mt == -1),
  316. * maximum acceptable body length 'max'.
  317. * Read an entire handshake message. Handshake messages arrive in
  318. * fragments.
  319. */
  320. long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
  321. {
  322. int i, al;
  323. struct hm_header_st *msg_hdr;
  324. /* s3->tmp is used to store messages that are unexpected, caused
  325. * by the absence of an optional handshake message */
  326. if (s->s3->tmp.reuse_message)
  327. {
  328. s->s3->tmp.reuse_message=0;
  329. if ((mt >= 0) && (s->s3->tmp.message_type != mt))
  330. {
  331. al=SSL_AD_UNEXPECTED_MESSAGE;
  332. SSLerr(SSL_F_DTLS1_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);
  333. goto f_err;
  334. }
  335. *ok=1;
  336. s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  337. s->init_num = (int)s->s3->tmp.message_size;
  338. return s->init_num;
  339. }
  340. msg_hdr = &s->d1->r_msg_hdr;
  341. do
  342. {
  343. if ( msg_hdr->frag_off == 0)
  344. {
  345. /* s->d1->r_message_header.msg_len = 0; */
  346. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  347. }
  348. i = dtls1_get_message_fragment(s, st1, stn, max, ok);
  349. if ( i == DTLS1_HM_BAD_FRAGMENT ||
  350. i == DTLS1_HM_FRAGMENT_RETRY) /* bad fragment received */
  351. continue;
  352. else if ( i <= 0 && !*ok)
  353. return i;
  354. /* Note that s->init_sum is used as a counter summing
  355. * up fragments' lengths: as soon as they sum up to
  356. * handshake packet length, we assume we have got all
  357. * the fragments. Overlapping fragments would cause
  358. * premature termination, so we don't expect overlaps.
  359. * Well, handling overlaps would require something more
  360. * drastic. Indeed, as it is now there is no way to
  361. * tell if out-of-order fragment from the middle was
  362. * the last. '>=' is the best/least we can do to control
  363. * the potential damage caused by malformed overlaps. */
  364. if ((unsigned int)s->init_num >= msg_hdr->msg_len)
  365. {
  366. unsigned char *p = (unsigned char *)s->init_buf->data;
  367. unsigned long msg_len = msg_hdr->msg_len;
  368. /* reconstruct message header as if it was
  369. * sent in single fragment */
  370. *(p++) = msg_hdr->type;
  371. l2n3(msg_len,p);
  372. s2n (msg_hdr->seq,p);
  373. l2n3(0,p);
  374. l2n3(msg_len,p);
  375. if (s->version != DTLS1_BAD_VER) {
  376. p -= DTLS1_HM_HEADER_LENGTH;
  377. msg_len += DTLS1_HM_HEADER_LENGTH;
  378. }
  379. ssl3_finish_mac(s, p, msg_len);
  380. if (s->msg_callback)
  381. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  382. p, msg_len,
  383. s, s->msg_callback_arg);
  384. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  385. s->d1->handshake_read_seq++;
  386. /* we just read a handshake message from the other side:
  387. * this means that we don't need to retransmit of the
  388. * buffered messages.
  389. * XDTLS: may be able clear out this
  390. * buffer a little sooner (i.e if an out-of-order
  391. * handshake message/record is received at the record
  392. * layer.
  393. * XDTLS: exception is that the server needs to
  394. * know that change cipher spec and finished messages
  395. * have been received by the client before clearing this
  396. * buffer. this can simply be done by waiting for the
  397. * first data segment, but is there a better way? */
  398. dtls1_clear_record_buffer(s);
  399. s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  400. return s->init_num;
  401. }
  402. else
  403. msg_hdr->frag_off = i;
  404. } while(1) ;
  405. f_err:
  406. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  407. *ok = 0;
  408. return -1;
  409. }
  410. static int dtls1_preprocess_fragment(SSL *s,struct hm_header_st *msg_hdr,int max)
  411. {
  412. size_t frag_off,frag_len,msg_len;
  413. msg_len = msg_hdr->msg_len;
  414. frag_off = msg_hdr->frag_off;
  415. frag_len = msg_hdr->frag_len;
  416. /* sanity checking */
  417. if ( (frag_off+frag_len) > msg_len)
  418. {
  419. SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,SSL_R_EXCESSIVE_MESSAGE_SIZE);
  420. return SSL_AD_ILLEGAL_PARAMETER;
  421. }
  422. if ( (frag_off+frag_len) > (unsigned long)max)
  423. {
  424. SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,SSL_R_EXCESSIVE_MESSAGE_SIZE);
  425. return SSL_AD_ILLEGAL_PARAMETER;
  426. }
  427. if ( s->d1->r_msg_hdr.frag_off == 0) /* first fragment */
  428. {
  429. /* msg_len is limited to 2^24, but is effectively checked
  430. * against max above */
  431. if (!BUF_MEM_grow_clean(s->init_buf,msg_len+DTLS1_HM_HEADER_LENGTH))
  432. {
  433. SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,ERR_R_BUF_LIB);
  434. return SSL_AD_INTERNAL_ERROR;
  435. }
  436. s->s3->tmp.message_size = msg_len;
  437. s->d1->r_msg_hdr.msg_len = msg_len;
  438. s->s3->tmp.message_type = msg_hdr->type;
  439. s->d1->r_msg_hdr.type = msg_hdr->type;
  440. s->d1->r_msg_hdr.seq = msg_hdr->seq;
  441. }
  442. else if (msg_len != s->d1->r_msg_hdr.msg_len)
  443. {
  444. /* They must be playing with us! BTW, failure to enforce
  445. * upper limit would open possibility for buffer overrun. */
  446. SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,SSL_R_EXCESSIVE_MESSAGE_SIZE);
  447. return SSL_AD_ILLEGAL_PARAMETER;
  448. }
  449. return 0; /* no error */
  450. }
  451. static int
  452. dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok)
  453. {
  454. /* (0) check whether the desired fragment is available
  455. * if so:
  456. * (1) copy over the fragment to s->init_buf->data[]
  457. * (2) update s->init_num
  458. */
  459. pitem *item;
  460. hm_fragment *frag;
  461. int al;
  462. *ok = 0;
  463. item = pqueue_peek(s->d1->buffered_messages);
  464. if ( item == NULL)
  465. return 0;
  466. frag = (hm_fragment *)item->data;
  467. if ( s->d1->handshake_read_seq == frag->msg_header.seq)
  468. {
  469. unsigned long frag_len = frag->msg_header.frag_len;
  470. pqueue_pop(s->d1->buffered_messages);
  471. al=dtls1_preprocess_fragment(s,&frag->msg_header,max);
  472. if (al==0) /* no alert */
  473. {
  474. unsigned char *p = (unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
  475. memcpy(&p[frag->msg_header.frag_off],
  476. frag->fragment,frag->msg_header.frag_len);
  477. }
  478. dtls1_hm_fragment_free(frag);
  479. pitem_free(item);
  480. if (al==0)
  481. {
  482. *ok = 1;
  483. return frag_len;
  484. }
  485. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  486. s->init_num = 0;
  487. *ok = 0;
  488. return -1;
  489. }
  490. else
  491. return 0;
  492. }
  493. static int
  494. dtls1_process_out_of_seq_message(SSL *s, struct hm_header_st* msg_hdr, int *ok)
  495. {
  496. int i=-1;
  497. hm_fragment *frag = NULL;
  498. pitem *item = NULL;
  499. unsigned char seq64be[8];
  500. unsigned long frag_len = msg_hdr->frag_len;
  501. if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len)
  502. goto err;
  503. /* Try to find item in queue, to prevent duplicate entries */
  504. memset(seq64be,0,sizeof(seq64be));
  505. seq64be[6] = (unsigned char) (msg_hdr->seq>>8);
  506. seq64be[7] = (unsigned char) msg_hdr->seq;
  507. item = pqueue_find(s->d1->buffered_messages, seq64be);
  508. /* Discard the message if sequence number was already there, is
  509. * too far in the future, already in the queue or if we received
  510. * a FINISHED before the SERVER_HELLO, which then must be a stale
  511. * retransmit.
  512. */
  513. if (msg_hdr->seq <= s->d1->handshake_read_seq ||
  514. msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
  515. (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED))
  516. {
  517. unsigned char devnull [256];
  518. while (frag_len)
  519. {
  520. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  521. devnull,
  522. frag_len>sizeof(devnull)?sizeof(devnull):frag_len,0);
  523. if (i<=0) goto err;
  524. frag_len -= i;
  525. }
  526. }
  527. if (frag_len)
  528. {
  529. frag = dtls1_hm_fragment_new(frag_len);
  530. if ( frag == NULL)
  531. goto err;
  532. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  533. /* read the body of the fragment (header has already been read */
  534. i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  535. frag->fragment,frag_len,0);
  536. if (i<=0 || (unsigned long)i!=frag_len)
  537. goto err;
  538. memset(seq64be,0,sizeof(seq64be));
  539. seq64be[6] = (unsigned char)(msg_hdr->seq>>8);
  540. seq64be[7] = (unsigned char)(msg_hdr->seq);
  541. item = pitem_new(seq64be, frag);
  542. if ( item == NULL)
  543. goto err;
  544. pqueue_insert(s->d1->buffered_messages, item);
  545. }
  546. return DTLS1_HM_FRAGMENT_RETRY;
  547. err:
  548. if ( frag != NULL) dtls1_hm_fragment_free(frag);
  549. if ( item != NULL) OPENSSL_free(item);
  550. *ok = 0;
  551. return i;
  552. }
  553. static long
  554. dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok)
  555. {
  556. unsigned char wire[DTLS1_HM_HEADER_LENGTH];
  557. unsigned long l, frag_off, frag_len;
  558. int i,al;
  559. struct hm_header_st msg_hdr;
  560. /* see if we have the required fragment already */
  561. if ((frag_len = dtls1_retrieve_buffered_fragment(s,max,ok)) || *ok)
  562. {
  563. if (*ok) s->init_num += frag_len;
  564. return frag_len;
  565. }
  566. /* read handshake message header */
  567. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,wire,
  568. DTLS1_HM_HEADER_LENGTH, 0);
  569. if (i <= 0) /* nbio, or an error */
  570. {
  571. s->rwstate=SSL_READING;
  572. *ok = 0;
  573. return i;
  574. }
  575. OPENSSL_assert(i == DTLS1_HM_HEADER_LENGTH);
  576. /* parse the message fragment header */
  577. dtls1_get_message_header(wire, &msg_hdr);
  578. /*
  579. * if this is a future (or stale) message it gets buffered
  580. * (or dropped)--no further processing at this time
  581. */
  582. if ( msg_hdr.seq != s->d1->handshake_read_seq)
  583. return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
  584. l = msg_hdr.msg_len;
  585. frag_off = msg_hdr.frag_off;
  586. frag_len = msg_hdr.frag_len;
  587. if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
  588. wire[0] == SSL3_MT_HELLO_REQUEST)
  589. {
  590. /* The server may always send 'Hello Request' messages --
  591. * we are doing a handshake anyway now, so ignore them
  592. * if their format is correct. Does not count for
  593. * 'Finished' MAC. */
  594. if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0)
  595. {
  596. if (s->msg_callback)
  597. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  598. wire, DTLS1_HM_HEADER_LENGTH, s,
  599. s->msg_callback_arg);
  600. s->init_num = 0;
  601. return dtls1_get_message_fragment(s, st1, stn,
  602. max, ok);
  603. }
  604. else /* Incorrectly formated Hello request */
  605. {
  606. al=SSL_AD_UNEXPECTED_MESSAGE;
  607. SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,SSL_R_UNEXPECTED_MESSAGE);
  608. goto f_err;
  609. }
  610. }
  611. if ((al=dtls1_preprocess_fragment(s,&msg_hdr,max)))
  612. goto f_err;
  613. /* XDTLS: ressurect this when restart is in place */
  614. s->state=stn;
  615. if ( frag_len > 0)
  616. {
  617. unsigned char *p=(unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
  618. i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
  619. &p[frag_off],frag_len,0);
  620. /* XDTLS: fix this--message fragments cannot span multiple packets */
  621. if (i <= 0)
  622. {
  623. s->rwstate=SSL_READING;
  624. *ok = 0;
  625. return i;
  626. }
  627. }
  628. else
  629. i = 0;
  630. /* XDTLS: an incorrectly formatted fragment should cause the
  631. * handshake to fail */
  632. OPENSSL_assert(i == (int)frag_len);
  633. *ok = 1;
  634. /* Note that s->init_num is *not* used as current offset in
  635. * s->init_buf->data, but as a counter summing up fragments'
  636. * lengths: as soon as they sum up to handshake packet
  637. * length, we assume we have got all the fragments. */
  638. s->init_num += frag_len;
  639. return frag_len;
  640. f_err:
  641. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  642. s->init_num = 0;
  643. *ok=0;
  644. return(-1);
  645. }
  646. int dtls1_send_finished(SSL *s, int a, int b, const char *sender, int slen)
  647. {
  648. unsigned char *p,*d;
  649. int i;
  650. unsigned long l;
  651. if (s->state == a)
  652. {
  653. d=(unsigned char *)s->init_buf->data;
  654. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  655. i=s->method->ssl3_enc->final_finish_mac(s,
  656. sender,slen,s->s3->tmp.finish_md);
  657. s->s3->tmp.finish_md_len = i;
  658. memcpy(p, s->s3->tmp.finish_md, i);
  659. p+=i;
  660. l=i;
  661. /* Copy the finished so we can use it for
  662. * renegotiation checks
  663. */
  664. if(s->type == SSL_ST_CONNECT)
  665. {
  666. OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
  667. memcpy(s->s3->previous_client_finished,
  668. s->s3->tmp.finish_md, i);
  669. s->s3->previous_client_finished_len=i;
  670. }
  671. else
  672. {
  673. OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
  674. memcpy(s->s3->previous_server_finished,
  675. s->s3->tmp.finish_md, i);
  676. s->s3->previous_server_finished_len=i;
  677. }
  678. #ifdef OPENSSL_SYS_WIN16
  679. /* MSVC 1.5 does not clear the top bytes of the word unless
  680. * I do this.
  681. */
  682. l&=0xffff;
  683. #endif
  684. d = dtls1_set_message_header(s, d, SSL3_MT_FINISHED, l, 0, l);
  685. s->init_num=(int)l+DTLS1_HM_HEADER_LENGTH;
  686. s->init_off=0;
  687. /* buffer the message to handle re-xmits */
  688. dtls1_buffer_message(s, 0);
  689. s->state=b;
  690. }
  691. /* SSL3_ST_SEND_xxxxxx_HELLO_B */
  692. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  693. }
  694. /* for these 2 messages, we need to
  695. * ssl->enc_read_ctx re-init
  696. * ssl->s3->read_sequence zero
  697. * ssl->s3->read_mac_secret re-init
  698. * ssl->session->read_sym_enc assign
  699. * ssl->session->read_compression assign
  700. * ssl->session->read_hash assign
  701. */
  702. int dtls1_send_change_cipher_spec(SSL *s, int a, int b)
  703. {
  704. unsigned char *p;
  705. if (s->state == a)
  706. {
  707. p=(unsigned char *)s->init_buf->data;
  708. *p++=SSL3_MT_CCS;
  709. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  710. s->init_num=DTLS1_CCS_HEADER_LENGTH;
  711. if (s->version == DTLS1_BAD_VER) {
  712. s->d1->next_handshake_write_seq++;
  713. s2n(s->d1->handshake_write_seq,p);
  714. s->init_num+=2;
  715. }
  716. s->init_off=0;
  717. dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
  718. s->d1->handshake_write_seq, 0, 0);
  719. /* buffer the message to handle re-xmits */
  720. dtls1_buffer_message(s, 1);
  721. s->state=b;
  722. }
  723. /* SSL3_ST_CW_CHANGE_B */
  724. return(dtls1_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC));
  725. }
  726. static int dtls1_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
  727. {
  728. int n;
  729. unsigned char *p;
  730. n=i2d_X509(x,NULL);
  731. if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3)))
  732. {
  733. SSLerr(SSL_F_DTLS1_ADD_CERT_TO_BUF,ERR_R_BUF_LIB);
  734. return 0;
  735. }
  736. p=(unsigned char *)&(buf->data[*l]);
  737. l2n3(n,p);
  738. i2d_X509(x,&p);
  739. *l+=n+3;
  740. return 1;
  741. }
  742. unsigned long dtls1_output_cert_chain(SSL *s, X509 *x)
  743. {
  744. unsigned char *p;
  745. int i;
  746. unsigned long l= 3 + DTLS1_HM_HEADER_LENGTH;
  747. BUF_MEM *buf;
  748. /* TLSv1 sends a chain with nothing in it, instead of an alert */
  749. buf=s->init_buf;
  750. if (!BUF_MEM_grow_clean(buf,10))
  751. {
  752. SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
  753. return(0);
  754. }
  755. if (x != NULL)
  756. {
  757. X509_STORE_CTX xs_ctx;
  758. if (!X509_STORE_CTX_init(&xs_ctx,s->ctx->cert_store,x,NULL))
  759. {
  760. SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_X509_LIB);
  761. return(0);
  762. }
  763. X509_verify_cert(&xs_ctx);
  764. for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
  765. {
  766. x = sk_X509_value(xs_ctx.chain, i);
  767. if (!dtls1_add_cert_to_buf(buf, &l, x))
  768. {
  769. X509_STORE_CTX_cleanup(&xs_ctx);
  770. return 0;
  771. }
  772. }
  773. X509_STORE_CTX_cleanup(&xs_ctx);
  774. }
  775. /* Thawte special :-) */
  776. for (i=0; i<sk_X509_num(s->ctx->extra_certs); i++)
  777. {
  778. x=sk_X509_value(s->ctx->extra_certs,i);
  779. if (!dtls1_add_cert_to_buf(buf, &l, x))
  780. return 0;
  781. }
  782. l-= (3 + DTLS1_HM_HEADER_LENGTH);
  783. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
  784. l2n3(l,p);
  785. l+=3;
  786. p=(unsigned char *)&(buf->data[0]);
  787. p = dtls1_set_message_header(s, p, SSL3_MT_CERTIFICATE, l, 0, l);
  788. l+=DTLS1_HM_HEADER_LENGTH;
  789. return(l);
  790. }
  791. int dtls1_read_failed(SSL *s, int code)
  792. {
  793. if ( code > 0)
  794. {
  795. fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
  796. return 1;
  797. }
  798. if (!dtls1_is_timer_expired(s))
  799. {
  800. /* not a timeout, none of our business,
  801. let higher layers handle this. in fact it's probably an error */
  802. return code;
  803. }
  804. if ( ! SSL_in_init(s)) /* done, no need to send a retransmit */
  805. {
  806. BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
  807. return code;
  808. }
  809. #if 0 /* for now, each alert contains only one record number */
  810. item = pqueue_peek(state->rcvd_records);
  811. if ( item )
  812. {
  813. /* send an alert immediately for all the missing records */
  814. }
  815. else
  816. #endif
  817. #if 0 /* no more alert sending, just retransmit the last set of messages */
  818. if ( state->timeout.read_timeouts >= DTLS1_TMO_READ_COUNT)
  819. ssl3_send_alert(s,SSL3_AL_WARNING,
  820. DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
  821. #endif
  822. return dtls1_handle_timeout(s);
  823. }
  824. int
  825. dtls1_get_queue_priority(unsigned short seq, int is_ccs)
  826. {
  827. /* The index of the retransmission queue actually is the message sequence number,
  828. * since the queue only contains messages of a single handshake. However, the
  829. * ChangeCipherSpec has no message sequence number and so using only the sequence
  830. * will result in the CCS and Finished having the same index. To prevent this,
  831. * the sequence number is multiplied by 2. In case of a CCS 1 is subtracted.
  832. * This does not only differ CSS and Finished, it also maintains the order of the
  833. * index (important for priority queues) and fits in the unsigned short variable.
  834. */
  835. return seq * 2 - is_ccs;
  836. }
  837. int
  838. dtls1_retransmit_buffered_messages(SSL *s)
  839. {
  840. pqueue sent = s->d1->sent_messages;
  841. piterator iter;
  842. pitem *item;
  843. hm_fragment *frag;
  844. int found = 0;
  845. iter = pqueue_iterator(sent);
  846. for ( item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter))
  847. {
  848. frag = (hm_fragment *)item->data;
  849. if ( dtls1_retransmit_message(s,
  850. (unsigned short)dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs),
  851. 0, &found) <= 0 && found)
  852. {
  853. fprintf(stderr, "dtls1_retransmit_message() failed\n");
  854. return -1;
  855. }
  856. }
  857. return 1;
  858. }
  859. int
  860. dtls1_buffer_message(SSL *s, int is_ccs)
  861. {
  862. pitem *item;
  863. hm_fragment *frag;
  864. unsigned char seq64be[8];
  865. /* this function is called immediately after a message has
  866. * been serialized */
  867. OPENSSL_assert(s->init_off == 0);
  868. frag = dtls1_hm_fragment_new(s->init_num);
  869. memcpy(frag->fragment, s->init_buf->data, s->init_num);
  870. if ( is_ccs)
  871. {
  872. OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
  873. ((s->version==DTLS1_VERSION)?DTLS1_CCS_HEADER_LENGTH:3) == (unsigned int)s->init_num);
  874. }
  875. else
  876. {
  877. OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
  878. DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
  879. }
  880. frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
  881. frag->msg_header.seq = s->d1->w_msg_hdr.seq;
  882. frag->msg_header.type = s->d1->w_msg_hdr.type;
  883. frag->msg_header.frag_off = 0;
  884. frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
  885. frag->msg_header.is_ccs = is_ccs;
  886. /* save current state*/
  887. frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
  888. frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
  889. frag->msg_header.saved_retransmit_state.compress = s->compress;
  890. frag->msg_header.saved_retransmit_state.session = s->session;
  891. frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
  892. memset(seq64be,0,sizeof(seq64be));
  893. seq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
  894. frag->msg_header.is_ccs)>>8);
  895. seq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
  896. frag->msg_header.is_ccs));
  897. item = pitem_new(seq64be, frag);
  898. if ( item == NULL)
  899. {
  900. dtls1_hm_fragment_free(frag);
  901. return 0;
  902. }
  903. #if 0
  904. fprintf( stderr, "buffered messge: \ttype = %xx\n", msg_buf->type);
  905. fprintf( stderr, "\t\t\t\t\tlen = %d\n", msg_buf->len);
  906. fprintf( stderr, "\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
  907. #endif
  908. pqueue_insert(s->d1->sent_messages, item);
  909. return 1;
  910. }
  911. int
  912. dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
  913. int *found)
  914. {
  915. int ret;
  916. /* XDTLS: for now assuming that read/writes are blocking */
  917. pitem *item;
  918. hm_fragment *frag ;
  919. unsigned long header_length;
  920. unsigned char seq64be[8];
  921. struct dtls1_retransmit_state saved_state;
  922. unsigned char save_write_sequence[8];
  923. /*
  924. OPENSSL_assert(s->init_num == 0);
  925. OPENSSL_assert(s->init_off == 0);
  926. */
  927. /* XDTLS: the requested message ought to be found, otherwise error */
  928. memset(seq64be,0,sizeof(seq64be));
  929. seq64be[6] = (unsigned char)(seq>>8);
  930. seq64be[7] = (unsigned char)seq;
  931. item = pqueue_find(s->d1->sent_messages, seq64be);
  932. if ( item == NULL)
  933. {
  934. fprintf(stderr, "retransmit: message %d non-existant\n", seq);
  935. *found = 0;
  936. return 0;
  937. }
  938. *found = 1;
  939. frag = (hm_fragment *)item->data;
  940. if ( frag->msg_header.is_ccs)
  941. header_length = DTLS1_CCS_HEADER_LENGTH;
  942. else
  943. header_length = DTLS1_HM_HEADER_LENGTH;
  944. memcpy(s->init_buf->data, frag->fragment,
  945. frag->msg_header.msg_len + header_length);
  946. s->init_num = frag->msg_header.msg_len + header_length;
  947. dtls1_set_message_header_int(s, frag->msg_header.type,
  948. frag->msg_header.msg_len, frag->msg_header.seq, 0,
  949. frag->msg_header.frag_len);
  950. /* save current state */
  951. saved_state.enc_write_ctx = s->enc_write_ctx;
  952. saved_state.write_hash = s->write_hash;
  953. saved_state.compress = s->compress;
  954. saved_state.session = s->session;
  955. saved_state.epoch = s->d1->w_epoch;
  956. saved_state.epoch = s->d1->w_epoch;
  957. s->d1->retransmitting = 1;
  958. /* restore state in which the message was originally sent */
  959. s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
  960. s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
  961. s->compress = frag->msg_header.saved_retransmit_state.compress;
  962. s->session = frag->msg_header.saved_retransmit_state.session;
  963. s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
  964. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1)
  965. {
  966. memcpy(save_write_sequence, s->s3->write_sequence, sizeof(s->s3->write_sequence));
  967. memcpy(s->s3->write_sequence, s->d1->last_write_sequence, sizeof(s->s3->write_sequence));
  968. }
  969. ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
  970. SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
  971. /* restore current state */
  972. s->enc_write_ctx = saved_state.enc_write_ctx;
  973. s->write_hash = saved_state.write_hash;
  974. s->compress = saved_state.compress;
  975. s->session = saved_state.session;
  976. s->d1->w_epoch = saved_state.epoch;
  977. if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1)
  978. {
  979. memcpy(s->d1->last_write_sequence, s->s3->write_sequence, sizeof(s->s3->write_sequence));
  980. memcpy(s->s3->write_sequence, save_write_sequence, sizeof(s->s3->write_sequence));
  981. }
  982. s->d1->retransmitting = 0;
  983. (void)BIO_flush(SSL_get_wbio(s));
  984. return ret;
  985. }
  986. /* call this function when the buffered messages are no longer needed */
  987. void
  988. dtls1_clear_record_buffer(SSL *s)
  989. {
  990. pitem *item;
  991. for(item = pqueue_pop(s->d1->sent_messages);
  992. item != NULL; item = pqueue_pop(s->d1->sent_messages))
  993. {
  994. dtls1_hm_fragment_free((hm_fragment *)item->data);
  995. pitem_free(item);
  996. }
  997. }
  998. unsigned char *
  999. dtls1_set_message_header(SSL *s, unsigned char *p, unsigned char mt,
  1000. unsigned long len, unsigned long frag_off, unsigned long frag_len)
  1001. {
  1002. if ( frag_off == 0)
  1003. {
  1004. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1005. s->d1->next_handshake_write_seq++;
  1006. }
  1007. dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
  1008. frag_off, frag_len);
  1009. return p += DTLS1_HM_HEADER_LENGTH;
  1010. }
  1011. /* don't actually do the writing, wait till the MTU has been retrieved */
  1012. static void
  1013. dtls1_set_message_header_int(SSL *s, unsigned char mt,
  1014. unsigned long len, unsigned short seq_num, unsigned long frag_off,
  1015. unsigned long frag_len)
  1016. {
  1017. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1018. msg_hdr->type = mt;
  1019. msg_hdr->msg_len = len;
  1020. msg_hdr->seq = seq_num;
  1021. msg_hdr->frag_off = frag_off;
  1022. msg_hdr->frag_len = frag_len;
  1023. }
  1024. static void
  1025. dtls1_fix_message_header(SSL *s, unsigned long frag_off,
  1026. unsigned long frag_len)
  1027. {
  1028. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1029. msg_hdr->frag_off = frag_off;
  1030. msg_hdr->frag_len = frag_len;
  1031. }
  1032. static unsigned char *
  1033. dtls1_write_message_header(SSL *s, unsigned char *p)
  1034. {
  1035. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1036. *p++ = msg_hdr->type;
  1037. l2n3(msg_hdr->msg_len, p);
  1038. s2n(msg_hdr->seq, p);
  1039. l2n3(msg_hdr->frag_off, p);
  1040. l2n3(msg_hdr->frag_len, p);
  1041. return p;
  1042. }
  1043. static unsigned int
  1044. dtls1_min_mtu(void)
  1045. {
  1046. return (g_probable_mtu[(sizeof(g_probable_mtu) /
  1047. sizeof(g_probable_mtu[0])) - 1]);
  1048. }
  1049. static unsigned int
  1050. dtls1_guess_mtu(unsigned int curr_mtu)
  1051. {
  1052. unsigned int i;
  1053. if ( curr_mtu == 0 )
  1054. return g_probable_mtu[0] ;
  1055. for ( i = 0; i < sizeof(g_probable_mtu)/sizeof(g_probable_mtu[0]); i++)
  1056. if ( curr_mtu > g_probable_mtu[i])
  1057. return g_probable_mtu[i];
  1058. return curr_mtu;
  1059. }
  1060. void
  1061. dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
  1062. {
  1063. memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
  1064. msg_hdr->type = *(data++);
  1065. n2l3(data, msg_hdr->msg_len);
  1066. n2s(data, msg_hdr->seq);
  1067. n2l3(data, msg_hdr->frag_off);
  1068. n2l3(data, msg_hdr->frag_len);
  1069. }
  1070. void
  1071. dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
  1072. {
  1073. memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
  1074. ccs_hdr->type = *(data++);
  1075. }