d1_both.c 39 KB

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