d1_both.c 35 KB

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