d1_both.c 47 KB

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