statem_dtls.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * Copyright 2005-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "../ssl_local.h"
  14. #include "statem_local.h"
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/objects.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
  21. #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
  22. if ((end) - (start) <= 8) { \
  23. long ii; \
  24. for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
  25. } else { \
  26. long ii; \
  27. bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
  28. for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
  29. bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
  30. } }
  31. #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
  32. long ii; \
  33. is_complete = 1; \
  34. if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
  35. if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
  36. if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
  37. static unsigned char bitmask_start_values[] =
  38. { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
  39. static unsigned char bitmask_end_values[] =
  40. { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
  41. static void dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off,
  42. size_t frag_len);
  43. static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,
  44. unsigned char *p);
  45. static void dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,
  46. size_t len,
  47. unsigned short seq_num,
  48. size_t frag_off,
  49. size_t frag_len);
  50. static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,
  51. size_t *len);
  52. static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly)
  53. {
  54. hm_fragment *frag = NULL;
  55. unsigned char *buf = NULL;
  56. unsigned char *bitmask = NULL;
  57. if ((frag = OPENSSL_malloc(sizeof(*frag))) == NULL)
  58. return NULL;
  59. if (frag_len) {
  60. if ((buf = OPENSSL_malloc(frag_len)) == NULL) {
  61. OPENSSL_free(frag);
  62. return NULL;
  63. }
  64. }
  65. /* zero length fragment gets zero frag->fragment */
  66. frag->fragment = buf;
  67. /* Initialize reassembly bitmask if necessary */
  68. if (reassembly) {
  69. bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
  70. if (bitmask == NULL) {
  71. OPENSSL_free(buf);
  72. OPENSSL_free(frag);
  73. return NULL;
  74. }
  75. }
  76. frag->reassembly = bitmask;
  77. return frag;
  78. }
  79. void dtls1_hm_fragment_free(hm_fragment *frag)
  80. {
  81. if (!frag)
  82. return;
  83. if (frag->msg_header.is_ccs) {
  84. /*
  85. * If we're freeing the CCS then we're done with the old wrl and it
  86. * can bee freed
  87. */
  88. if (frag->msg_header.saved_retransmit_state.wrlmethod != NULL)
  89. frag->msg_header.saved_retransmit_state.wrlmethod->free(frag->msg_header.saved_retransmit_state.wrl);
  90. }
  91. OPENSSL_free(frag->fragment);
  92. OPENSSL_free(frag->reassembly);
  93. OPENSSL_free(frag);
  94. }
  95. /*
  96. * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
  97. * SSL3_RT_CHANGE_CIPHER_SPEC)
  98. */
  99. int dtls1_do_write(SSL_CONNECTION *s, int type)
  100. {
  101. int ret;
  102. size_t written;
  103. size_t curr_mtu;
  104. int retry = 1;
  105. size_t len, frag_off, overhead, used_len;
  106. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  107. if (!dtls1_query_mtu(s))
  108. return -1;
  109. if (s->d1->mtu < dtls1_min_mtu(s))
  110. /* should have something reasonable now */
  111. return -1;
  112. if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
  113. if (!ossl_assert(s->init_num ==
  114. s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))
  115. return -1;
  116. }
  117. overhead = s->rlayer.wrlmethod->get_max_record_overhead(s->rlayer.wrl);
  118. frag_off = 0;
  119. s->rwstate = SSL_NOTHING;
  120. /* s->init_num shouldn't ever be < 0...but just in case */
  121. while (s->init_num > 0) {
  122. if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {
  123. /* We must be writing a fragment other than the first one */
  124. if (frag_off > 0) {
  125. /* This is the first attempt at writing out this fragment */
  126. if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {
  127. /*
  128. * Each fragment that was already sent must at least have
  129. * contained the message header plus one other byte.
  130. * Therefore |init_off| must have progressed by at least
  131. * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
  132. * wrong.
  133. */
  134. return -1;
  135. }
  136. /*
  137. * Adjust |init_off| and |init_num| to allow room for a new
  138. * message header for this fragment.
  139. */
  140. s->init_off -= DTLS1_HM_HEADER_LENGTH;
  141. s->init_num += DTLS1_HM_HEADER_LENGTH;
  142. } else {
  143. /*
  144. * We must have been called again after a retry so use the
  145. * fragment offset from our last attempt. We do not need
  146. * to adjust |init_off| and |init_num| as above, because
  147. * that should already have been done before the retry.
  148. */
  149. frag_off = s->d1->w_msg_hdr.frag_off;
  150. }
  151. }
  152. used_len = BIO_wpending(s->wbio) + overhead;
  153. if (s->d1->mtu > used_len)
  154. curr_mtu = s->d1->mtu - used_len;
  155. else
  156. curr_mtu = 0;
  157. if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
  158. /*
  159. * grr.. we could get an error if MTU picked was wrong
  160. */
  161. ret = BIO_flush(s->wbio);
  162. if (ret <= 0) {
  163. s->rwstate = SSL_WRITING;
  164. return ret;
  165. }
  166. if (s->d1->mtu > overhead + DTLS1_HM_HEADER_LENGTH) {
  167. curr_mtu = s->d1->mtu - overhead;
  168. } else {
  169. /* Shouldn't happen */
  170. return -1;
  171. }
  172. }
  173. /*
  174. * We just checked that s->init_num > 0 so this cast should be safe
  175. */
  176. if (((unsigned int)s->init_num) > curr_mtu)
  177. len = curr_mtu;
  178. else
  179. len = s->init_num;
  180. if (len > ssl_get_max_send_fragment(s))
  181. len = ssl_get_max_send_fragment(s);
  182. /*
  183. * XDTLS: this function is too long. split out the CCS part
  184. */
  185. if (type == SSL3_RT_HANDSHAKE) {
  186. if (len < DTLS1_HM_HEADER_LENGTH) {
  187. /*
  188. * len is so small that we really can't do anything sensible
  189. * so fail
  190. */
  191. return -1;
  192. }
  193. dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
  194. dtls1_write_message_header(s,
  195. (unsigned char *)&s->init_buf->
  196. data[s->init_off]);
  197. }
  198. ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,
  199. &written);
  200. if (ret <= 0) {
  201. /*
  202. * might need to update MTU here, but we don't know which
  203. * previous packet caused the failure -- so can't really
  204. * retransmit anything. continue as if everything is fine and
  205. * wait for an alert to handle the retransmit
  206. */
  207. if (retry && BIO_ctrl(SSL_get_wbio(ssl),
  208. BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
  209. if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  210. if (!dtls1_query_mtu(s))
  211. return -1;
  212. /* Have one more go */
  213. retry = 0;
  214. } else
  215. return -1;
  216. } else {
  217. return -1;
  218. }
  219. } else {
  220. /*
  221. * bad if this assert fails, only part of the handshake message
  222. * got sent. but why would this happen?
  223. */
  224. if (!ossl_assert(len == written))
  225. return -1;
  226. /*
  227. * We should not exceed the MTU size. If compression is in use
  228. * then the max record overhead calculation is unreliable so we do
  229. * not check in that case. We use assert rather than ossl_assert
  230. * because in a production build, if this assert were ever to fail,
  231. * then the best thing to do is probably carry on regardless.
  232. */
  233. assert(s->s3.tmp.new_compression != NULL
  234. || BIO_wpending(s->wbio) <= (int)s->d1->mtu);
  235. if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
  236. /*
  237. * should not be done for 'Hello Request's, but in that case
  238. * we'll ignore the result anyway
  239. */
  240. unsigned char *p =
  241. (unsigned char *)&s->init_buf->data[s->init_off];
  242. const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  243. size_t xlen;
  244. if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
  245. /*
  246. * reconstruct message header is if it is being sent in
  247. * single fragment
  248. */
  249. *p++ = msg_hdr->type;
  250. l2n3(msg_hdr->msg_len, p);
  251. s2n(msg_hdr->seq, p);
  252. l2n3(0, p);
  253. l2n3(msg_hdr->msg_len, p);
  254. p -= DTLS1_HM_HEADER_LENGTH;
  255. xlen = written;
  256. } else {
  257. p += DTLS1_HM_HEADER_LENGTH;
  258. xlen = written - DTLS1_HM_HEADER_LENGTH;
  259. }
  260. if (!ssl3_finish_mac(s, p, xlen))
  261. return -1;
  262. }
  263. if (written == s->init_num) {
  264. if (s->msg_callback)
  265. s->msg_callback(1, s->version, type, s->init_buf->data,
  266. (size_t)(s->init_off + s->init_num), ssl,
  267. s->msg_callback_arg);
  268. s->init_off = 0; /* done writing this message */
  269. s->init_num = 0;
  270. return 1;
  271. }
  272. s->init_off += written;
  273. s->init_num -= written;
  274. written -= DTLS1_HM_HEADER_LENGTH;
  275. frag_off += written;
  276. /*
  277. * We save the fragment offset for the next fragment so we have it
  278. * available in case of an IO retry. We don't know the length of the
  279. * next fragment yet so just set that to 0 for now. It will be
  280. * updated again later.
  281. */
  282. dtls1_fix_message_header(s, frag_off, 0);
  283. }
  284. }
  285. return 0;
  286. }
  287. int dtls_get_message(SSL_CONNECTION *s, int *mt)
  288. {
  289. struct hm_header_st *msg_hdr;
  290. unsigned char *p;
  291. size_t msg_len;
  292. size_t tmplen;
  293. int errtype;
  294. msg_hdr = &s->d1->r_msg_hdr;
  295. memset(msg_hdr, 0, sizeof(*msg_hdr));
  296. again:
  297. if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) {
  298. if (errtype == DTLS1_HM_BAD_FRAGMENT
  299. || errtype == DTLS1_HM_FRAGMENT_RETRY) {
  300. /* bad fragment received */
  301. goto again;
  302. }
  303. return 0;
  304. }
  305. *mt = s->s3.tmp.message_type;
  306. p = (unsigned char *)s->init_buf->data;
  307. if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  308. if (s->msg_callback) {
  309. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
  310. p, 1, SSL_CONNECTION_GET_SSL(s),
  311. s->msg_callback_arg);
  312. }
  313. /*
  314. * This isn't a real handshake message so skip the processing below.
  315. */
  316. return 1;
  317. }
  318. msg_len = msg_hdr->msg_len;
  319. /* reconstruct message header */
  320. *(p++) = msg_hdr->type;
  321. l2n3(msg_len, p);
  322. s2n(msg_hdr->seq, p);
  323. l2n3(0, p);
  324. l2n3(msg_len, p);
  325. memset(msg_hdr, 0, sizeof(*msg_hdr));
  326. s->d1->handshake_read_seq++;
  327. s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  328. return 1;
  329. }
  330. /*
  331. * Actually we already have the message body - but this is an opportunity for
  332. * DTLS to do any further processing it wants at the same point that TLS would
  333. * be asked for the message body.
  334. */
  335. int dtls_get_message_body(SSL_CONNECTION *s, size_t *len)
  336. {
  337. unsigned char *msg = (unsigned char *)s->init_buf->data;
  338. size_t msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH;
  339. if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
  340. /* Nothing to be done */
  341. goto end;
  342. }
  343. /*
  344. * If receiving Finished, record MAC of prior handshake messages for
  345. * Finished verification.
  346. */
  347. if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
  348. /* SSLfatal() already called */
  349. return 0;
  350. }
  351. if (s->version == DTLS1_BAD_VER) {
  352. msg += DTLS1_HM_HEADER_LENGTH;
  353. msg_len -= DTLS1_HM_HEADER_LENGTH;
  354. }
  355. if (!ssl3_finish_mac(s, msg, msg_len))
  356. return 0;
  357. if (s->msg_callback)
  358. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  359. s->init_buf->data, s->init_num + DTLS1_HM_HEADER_LENGTH,
  360. SSL_CONNECTION_GET_SSL(s), s->msg_callback_arg);
  361. end:
  362. *len = s->init_num;
  363. return 1;
  364. }
  365. /*
  366. * dtls1_max_handshake_message_len returns the maximum number of bytes
  367. * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
  368. * may be greater if the maximum certificate list size requires it.
  369. */
  370. static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION *s)
  371. {
  372. size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  373. if (max_len < s->max_cert_list)
  374. return s->max_cert_list;
  375. return max_len;
  376. }
  377. static int dtls1_preprocess_fragment(SSL_CONNECTION *s,
  378. struct hm_header_st *msg_hdr)
  379. {
  380. size_t frag_off, frag_len, msg_len;
  381. msg_len = msg_hdr->msg_len;
  382. frag_off = msg_hdr->frag_off;
  383. frag_len = msg_hdr->frag_len;
  384. /* sanity checking */
  385. if ((frag_off + frag_len) > msg_len
  386. || msg_len > dtls1_max_handshake_message_len(s)) {
  387. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  388. return 0;
  389. }
  390. if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
  391. /*
  392. * msg_len is limited to 2^24, but is effectively checked against
  393. * dtls_max_handshake_message_len(s) above
  394. */
  395. if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
  396. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
  397. return 0;
  398. }
  399. s->s3.tmp.message_size = msg_len;
  400. s->d1->r_msg_hdr.msg_len = msg_len;
  401. s->s3.tmp.message_type = msg_hdr->type;
  402. s->d1->r_msg_hdr.type = msg_hdr->type;
  403. s->d1->r_msg_hdr.seq = msg_hdr->seq;
  404. } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
  405. /*
  406. * They must be playing with us! BTW, failure to enforce upper limit
  407. * would open possibility for buffer overrun.
  408. */
  409. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  410. return 0;
  411. }
  412. return 1;
  413. }
  414. /*
  415. * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
  416. * fatal error.
  417. */
  418. static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION *s, size_t *len)
  419. {
  420. /*-
  421. * (0) check whether the desired fragment is available
  422. * if so:
  423. * (1) copy over the fragment to s->init_buf->data[]
  424. * (2) update s->init_num
  425. */
  426. pitem *item;
  427. piterator iter;
  428. hm_fragment *frag;
  429. int ret;
  430. int chretran = 0;
  431. iter = pqueue_iterator(s->d1->buffered_messages);
  432. do {
  433. item = pqueue_next(&iter);
  434. if (item == NULL)
  435. return 0;
  436. frag = (hm_fragment *)item->data;
  437. if (frag->msg_header.seq < s->d1->handshake_read_seq) {
  438. pitem *next;
  439. hm_fragment *nextfrag;
  440. if (!s->server
  441. || frag->msg_header.seq != 0
  442. || s->d1->handshake_read_seq != 1
  443. || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
  444. /*
  445. * This is a stale message that has been buffered so clear it.
  446. * It is safe to pop this message from the queue even though
  447. * we have an active iterator
  448. */
  449. pqueue_pop(s->d1->buffered_messages);
  450. dtls1_hm_fragment_free(frag);
  451. pitem_free(item);
  452. item = NULL;
  453. frag = NULL;
  454. } else {
  455. /*
  456. * We have fragments for a ClientHello without a cookie,
  457. * even though we have sent a HelloVerifyRequest. It is possible
  458. * that the HelloVerifyRequest got lost and this is a
  459. * retransmission of the original ClientHello
  460. */
  461. next = pqueue_next(&iter);
  462. if (next != NULL) {
  463. nextfrag = (hm_fragment *)next->data;
  464. if (nextfrag->msg_header.seq == s->d1->handshake_read_seq) {
  465. /*
  466. * We have fragments for both a ClientHello without
  467. * cookie and one with. Ditch the one without.
  468. */
  469. pqueue_pop(s->d1->buffered_messages);
  470. dtls1_hm_fragment_free(frag);
  471. pitem_free(item);
  472. item = next;
  473. frag = nextfrag;
  474. } else {
  475. chretran = 1;
  476. }
  477. } else {
  478. chretran = 1;
  479. }
  480. }
  481. }
  482. } while (item == NULL);
  483. /* Don't return if reassembly still in progress */
  484. if (frag->reassembly != NULL)
  485. return 0;
  486. if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) {
  487. size_t frag_len = frag->msg_header.frag_len;
  488. pqueue_pop(s->d1->buffered_messages);
  489. /* Calls SSLfatal() as required */
  490. ret = dtls1_preprocess_fragment(s, &frag->msg_header);
  491. if (ret && frag->msg_header.frag_len > 0) {
  492. unsigned char *p =
  493. (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  494. memcpy(&p[frag->msg_header.frag_off], frag->fragment,
  495. frag->msg_header.frag_len);
  496. }
  497. dtls1_hm_fragment_free(frag);
  498. pitem_free(item);
  499. if (ret) {
  500. if (chretran) {
  501. /*
  502. * We got a new ClientHello with a message sequence of 0.
  503. * Reset the read/write sequences back to the beginning.
  504. * We process it like this is the first time we've seen a
  505. * ClientHello from the client.
  506. */
  507. s->d1->handshake_read_seq = 0;
  508. s->d1->next_handshake_write_seq = 0;
  509. }
  510. *len = frag_len;
  511. return 1;
  512. }
  513. /* Fatal error */
  514. s->init_num = 0;
  515. return -1;
  516. } else {
  517. return 0;
  518. }
  519. }
  520. static int dtls1_reassemble_fragment(SSL_CONNECTION *s,
  521. const struct hm_header_st *msg_hdr)
  522. {
  523. hm_fragment *frag = NULL;
  524. pitem *item = NULL;
  525. int i = -1, is_complete;
  526. unsigned char seq64be[8];
  527. size_t frag_len = msg_hdr->frag_len;
  528. size_t readbytes;
  529. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  530. if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
  531. msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
  532. goto err;
  533. if (frag_len == 0) {
  534. return DTLS1_HM_FRAGMENT_RETRY;
  535. }
  536. /* Try to find item in queue */
  537. memset(seq64be, 0, sizeof(seq64be));
  538. seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
  539. seq64be[7] = (unsigned char)msg_hdr->seq;
  540. item = pqueue_find(s->d1->buffered_messages, seq64be);
  541. if (item == NULL) {
  542. frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
  543. if (frag == NULL)
  544. goto err;
  545. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  546. frag->msg_header.frag_len = frag->msg_header.msg_len;
  547. frag->msg_header.frag_off = 0;
  548. } else {
  549. frag = (hm_fragment *)item->data;
  550. if (frag->msg_header.msg_len != msg_hdr->msg_len) {
  551. item = NULL;
  552. frag = NULL;
  553. goto err;
  554. }
  555. }
  556. /*
  557. * If message is already reassembled, this must be a retransmit and can
  558. * be dropped. In this case item != NULL and so frag does not need to be
  559. * freed.
  560. */
  561. if (frag->reassembly == NULL) {
  562. unsigned char devnull[256];
  563. while (frag_len) {
  564. i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
  565. devnull,
  566. frag_len >
  567. sizeof(devnull) ? sizeof(devnull) :
  568. frag_len, 0, &readbytes);
  569. if (i <= 0)
  570. goto err;
  571. frag_len -= readbytes;
  572. }
  573. return DTLS1_HM_FRAGMENT_RETRY;
  574. }
  575. /* read the body of the fragment (header has already been read */
  576. i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
  577. frag->fragment + msg_hdr->frag_off,
  578. frag_len, 0, &readbytes);
  579. if (i <= 0 || readbytes != frag_len)
  580. i = -1;
  581. if (i <= 0)
  582. goto err;
  583. RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
  584. (long)(msg_hdr->frag_off + frag_len));
  585. if (!ossl_assert(msg_hdr->msg_len > 0))
  586. goto err;
  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. item = pitem_new(seq64be, frag);
  595. if (item == NULL) {
  596. i = -1;
  597. goto err;
  598. }
  599. item = pqueue_insert(s->d1->buffered_messages, item);
  600. /*
  601. * pqueue_insert fails iff a duplicate item is inserted. However,
  602. * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
  603. * would have returned it and control would never have reached this
  604. * branch.
  605. */
  606. if (!ossl_assert(item != NULL))
  607. goto err;
  608. }
  609. return DTLS1_HM_FRAGMENT_RETRY;
  610. err:
  611. if (item == NULL)
  612. dtls1_hm_fragment_free(frag);
  613. return -1;
  614. }
  615. static int dtls1_process_out_of_seq_message(SSL_CONNECTION *s,
  616. const struct hm_header_st *msg_hdr)
  617. {
  618. int i = -1;
  619. hm_fragment *frag = NULL;
  620. pitem *item = NULL;
  621. unsigned char seq64be[8];
  622. size_t frag_len = msg_hdr->frag_len;
  623. size_t readbytes;
  624. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  625. if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
  626. goto err;
  627. /* Try to find item in queue, to prevent duplicate entries */
  628. memset(seq64be, 0, sizeof(seq64be));
  629. seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
  630. seq64be[7] = (unsigned char)msg_hdr->seq;
  631. item = pqueue_find(s->d1->buffered_messages, seq64be);
  632. /*
  633. * If we already have an entry and this one is a fragment, don't discard
  634. * it and rather try to reassemble it.
  635. */
  636. if (item != NULL && frag_len != msg_hdr->msg_len)
  637. item = NULL;
  638. /*
  639. * Discard the message if sequence number was already there, is too far
  640. * in the future, already in the queue or if we received a FINISHED
  641. * before the SERVER_HELLO, which then must be a stale retransmit.
  642. */
  643. if (msg_hdr->seq <= s->d1->handshake_read_seq ||
  644. msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
  645. (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
  646. unsigned char devnull[256];
  647. while (frag_len) {
  648. i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
  649. devnull,
  650. frag_len >
  651. sizeof(devnull) ? sizeof(devnull) :
  652. frag_len, 0, &readbytes);
  653. if (i <= 0)
  654. goto err;
  655. frag_len -= readbytes;
  656. }
  657. } else {
  658. if (frag_len != msg_hdr->msg_len) {
  659. return dtls1_reassemble_fragment(s, msg_hdr);
  660. }
  661. if (frag_len > dtls1_max_handshake_message_len(s))
  662. goto err;
  663. frag = dtls1_hm_fragment_new(frag_len, 0);
  664. if (frag == NULL)
  665. goto err;
  666. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  667. if (frag_len) {
  668. /*
  669. * read the body of the fragment (header has already been read
  670. */
  671. i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
  672. frag->fragment, frag_len, 0,
  673. &readbytes);
  674. if (i<=0 || readbytes != frag_len)
  675. i = -1;
  676. if (i <= 0)
  677. goto err;
  678. }
  679. item = pitem_new(seq64be, frag);
  680. if (item == NULL)
  681. goto err;
  682. item = pqueue_insert(s->d1->buffered_messages, item);
  683. /*
  684. * pqueue_insert fails iff a duplicate item is inserted. However,
  685. * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
  686. * would have returned it. Then, either |frag_len| !=
  687. * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
  688. * have been processed with |dtls1_reassemble_fragment|, above, or
  689. * the record will have been discarded.
  690. */
  691. if (!ossl_assert(item != NULL))
  692. goto err;
  693. }
  694. return DTLS1_HM_FRAGMENT_RETRY;
  695. err:
  696. if (item == NULL)
  697. dtls1_hm_fragment_free(frag);
  698. return 0;
  699. }
  700. static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,
  701. size_t *len)
  702. {
  703. unsigned char wire[DTLS1_HM_HEADER_LENGTH];
  704. size_t mlen, frag_off, frag_len;
  705. int i, ret, recvd_type;
  706. struct hm_header_st msg_hdr;
  707. size_t readbytes;
  708. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  709. int chretran = 0;
  710. *errtype = 0;
  711. redo:
  712. /* see if we have the required fragment already */
  713. ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
  714. if (ret < 0) {
  715. /* SSLfatal() already called */
  716. return 0;
  717. }
  718. if (ret > 0) {
  719. s->init_num = frag_len;
  720. *len = frag_len;
  721. return 1;
  722. }
  723. /* read handshake message header */
  724. i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, wire,
  725. DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
  726. if (i <= 0) { /* nbio, or an error */
  727. s->rwstate = SSL_READING;
  728. *len = 0;
  729. return 0;
  730. }
  731. if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  732. if (wire[0] != SSL3_MT_CCS) {
  733. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  734. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  735. goto f_err;
  736. }
  737. memcpy(s->init_buf->data, wire, readbytes);
  738. s->init_num = readbytes - 1;
  739. s->init_msg = s->init_buf->data + 1;
  740. s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
  741. s->s3.tmp.message_size = readbytes - 1;
  742. *len = readbytes - 1;
  743. return 1;
  744. }
  745. /* Handshake fails if message header is incomplete */
  746. if (readbytes != DTLS1_HM_HEADER_LENGTH) {
  747. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  748. goto f_err;
  749. }
  750. /* parse the message fragment header */
  751. dtls1_get_message_header(wire, &msg_hdr);
  752. mlen = msg_hdr.msg_len;
  753. frag_off = msg_hdr.frag_off;
  754. frag_len = msg_hdr.frag_len;
  755. /*
  756. * We must have at least frag_len bytes left in the record to be read.
  757. * Fragments must not span records.
  758. */
  759. if (frag_len > s->rlayer.tlsrecs[s->rlayer.curr_rec].length) {
  760. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
  761. goto f_err;
  762. }
  763. /*
  764. * if this is a future (or stale) message it gets buffered
  765. * (or dropped)--no further processing at this time
  766. * While listening, we accept seq 1 (ClientHello with cookie)
  767. * although we're still expecting seq 0 (ClientHello)
  768. */
  769. if (msg_hdr.seq != s->d1->handshake_read_seq) {
  770. if (!s->server
  771. || msg_hdr.seq != 0
  772. || s->d1->handshake_read_seq != 1
  773. || wire[0] != SSL3_MT_CLIENT_HELLO
  774. || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
  775. *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
  776. return 0;
  777. }
  778. /*
  779. * We received a ClientHello and sent back a HelloVerifyRequest. We
  780. * now seem to have received a retransmitted initial ClientHello. That
  781. * is allowed (possibly our HelloVerifyRequest got lost).
  782. */
  783. chretran = 1;
  784. }
  785. if (frag_len && frag_len < mlen) {
  786. *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
  787. return 0;
  788. }
  789. if (!s->server
  790. && s->d1->r_msg_hdr.frag_off == 0
  791. && s->statem.hand_state != TLS_ST_OK
  792. && wire[0] == SSL3_MT_HELLO_REQUEST) {
  793. /*
  794. * The server may always send 'Hello Request' messages -- we are
  795. * doing a handshake anyway now, so ignore them if their format is
  796. * correct. Does not count for 'Finished' MAC.
  797. */
  798. if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
  799. if (s->msg_callback)
  800. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  801. wire, DTLS1_HM_HEADER_LENGTH, ssl,
  802. s->msg_callback_arg);
  803. s->init_num = 0;
  804. goto redo;
  805. } else { /* Incorrectly formatted Hello request */
  806. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  807. goto f_err;
  808. }
  809. }
  810. if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
  811. /* SSLfatal() already called */
  812. goto f_err;
  813. }
  814. if (frag_len > 0) {
  815. unsigned char *p =
  816. (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  817. i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
  818. &p[frag_off], frag_len, 0, &readbytes);
  819. /*
  820. * This shouldn't ever fail due to NBIO because we already checked
  821. * that we have enough data in the record
  822. */
  823. if (i <= 0) {
  824. s->rwstate = SSL_READING;
  825. *len = 0;
  826. return 0;
  827. }
  828. } else {
  829. readbytes = 0;
  830. }
  831. /*
  832. * XDTLS: an incorrectly formatted fragment should cause the handshake
  833. * to fail
  834. */
  835. if (readbytes != frag_len) {
  836. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
  837. goto f_err;
  838. }
  839. if (chretran) {
  840. /*
  841. * We got a new ClientHello with a message sequence of 0.
  842. * Reset the read/write sequences back to the beginning.
  843. * We process it like this is the first time we've seen a ClientHello
  844. * from the client.
  845. */
  846. s->d1->handshake_read_seq = 0;
  847. s->d1->next_handshake_write_seq = 0;
  848. }
  849. /*
  850. * Note that s->init_num is *not* used as current offset in
  851. * s->init_buf->data, but as a counter summing up fragments' lengths: as
  852. * soon as they sum up to handshake packet length, we assume we have got
  853. * all the fragments.
  854. */
  855. *len = s->init_num = frag_len;
  856. return 1;
  857. f_err:
  858. s->init_num = 0;
  859. *len = 0;
  860. return 0;
  861. }
  862. /*-
  863. * for these 2 messages, we need to
  864. * ssl->session->read_sym_enc assign
  865. * ssl->session->read_compression assign
  866. * ssl->session->read_hash assign
  867. */
  868. CON_FUNC_RETURN dtls_construct_change_cipher_spec(SSL_CONNECTION *s,
  869. WPACKET *pkt)
  870. {
  871. if (s->version == DTLS1_BAD_VER) {
  872. s->d1->next_handshake_write_seq++;
  873. if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) {
  874. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  875. return CON_FUNC_ERROR;
  876. }
  877. }
  878. return CON_FUNC_SUCCESS;
  879. }
  880. #ifndef OPENSSL_NO_SCTP
  881. /*
  882. * Wait for a dry event. Should only be called at a point in the handshake
  883. * where we are not expecting any data from the peer except an alert.
  884. */
  885. WORK_STATE dtls_wait_for_dry(SSL_CONNECTION *s)
  886. {
  887. int ret, errtype;
  888. size_t len;
  889. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  890. /* read app data until dry event */
  891. ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl));
  892. if (ret < 0) {
  893. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  894. return WORK_ERROR;
  895. }
  896. if (ret == 0) {
  897. /*
  898. * We're not expecting any more messages from the peer at this point -
  899. * but we could get an alert. If an alert is waiting then we will never
  900. * return successfully. Therefore we attempt to read a message. This
  901. * should never succeed but will process any waiting alerts.
  902. */
  903. if (dtls_get_reassembled_message(s, &errtype, &len)) {
  904. /* The call succeeded! This should never happen */
  905. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  906. return WORK_ERROR;
  907. }
  908. s->s3.in_read_app_data = 2;
  909. s->rwstate = SSL_READING;
  910. BIO_clear_retry_flags(SSL_get_rbio(ssl));
  911. BIO_set_retry_read(SSL_get_rbio(ssl));
  912. return WORK_MORE_A;
  913. }
  914. return WORK_FINISHED_CONTINUE;
  915. }
  916. #endif
  917. int dtls1_read_failed(SSL_CONNECTION *s, int code)
  918. {
  919. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  920. if (code > 0) {
  921. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  922. return 0;
  923. }
  924. if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) {
  925. /*
  926. * not a timeout, none of our business, let higher layers handle
  927. * this. in fact it's probably an error
  928. */
  929. return code;
  930. }
  931. /* done, no need to send a retransmit */
  932. if (!SSL_in_init(ssl))
  933. {
  934. BIO_set_flags(SSL_get_rbio(ssl), BIO_FLAGS_READ);
  935. return code;
  936. }
  937. return dtls1_handle_timeout(s);
  938. }
  939. int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
  940. {
  941. /*
  942. * The index of the retransmission queue actually is the message sequence
  943. * number, since the queue only contains messages of a single handshake.
  944. * However, the ChangeCipherSpec has no message sequence number and so
  945. * using only the sequence will result in the CCS and Finished having the
  946. * same index. To prevent this, the sequence number is multiplied by 2.
  947. * In case of a CCS 1 is subtracted. This does not only differ CSS and
  948. * Finished, it also maintains the order of the index (important for
  949. * priority queues) and fits in the unsigned short variable.
  950. */
  951. return seq * 2 - is_ccs;
  952. }
  953. int dtls1_retransmit_buffered_messages(SSL_CONNECTION *s)
  954. {
  955. pqueue *sent = s->d1->sent_messages;
  956. piterator iter;
  957. pitem *item;
  958. hm_fragment *frag;
  959. int found = 0;
  960. iter = pqueue_iterator(sent);
  961. for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
  962. frag = (hm_fragment *)item->data;
  963. if (dtls1_retransmit_message(s, (unsigned short)
  964. dtls1_get_queue_priority
  965. (frag->msg_header.seq,
  966. frag->msg_header.is_ccs), &found) <= 0)
  967. return -1;
  968. }
  969. return 1;
  970. }
  971. int dtls1_buffer_message(SSL_CONNECTION *s, int is_ccs)
  972. {
  973. pitem *item;
  974. hm_fragment *frag;
  975. unsigned char seq64be[8];
  976. /*
  977. * this function is called immediately after a message has been
  978. * serialized
  979. */
  980. if (!ossl_assert(s->init_off == 0))
  981. return 0;
  982. frag = dtls1_hm_fragment_new(s->init_num, 0);
  983. if (frag == NULL)
  984. return 0;
  985. memcpy(frag->fragment, s->init_buf->data, s->init_num);
  986. if (is_ccs) {
  987. /* For DTLS1_BAD_VER the header length is non-standard */
  988. if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
  989. ((s->version ==
  990. DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
  991. == (unsigned int)s->init_num)) {
  992. dtls1_hm_fragment_free(frag);
  993. return 0;
  994. }
  995. } else {
  996. if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
  997. DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num)) {
  998. dtls1_hm_fragment_free(frag);
  999. return 0;
  1000. }
  1001. }
  1002. frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
  1003. frag->msg_header.seq = s->d1->w_msg_hdr.seq;
  1004. frag->msg_header.type = s->d1->w_msg_hdr.type;
  1005. frag->msg_header.frag_off = 0;
  1006. frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
  1007. frag->msg_header.is_ccs = is_ccs;
  1008. /* save current state */
  1009. frag->msg_header.saved_retransmit_state.wrlmethod = s->rlayer.wrlmethod;
  1010. frag->msg_header.saved_retransmit_state.wrl = s->rlayer.wrl;
  1011. memset(seq64be, 0, sizeof(seq64be));
  1012. seq64be[6] =
  1013. (unsigned
  1014. char)(dtls1_get_queue_priority(frag->msg_header.seq,
  1015. frag->msg_header.is_ccs) >> 8);
  1016. seq64be[7] =
  1017. (unsigned
  1018. char)(dtls1_get_queue_priority(frag->msg_header.seq,
  1019. frag->msg_header.is_ccs));
  1020. item = pitem_new(seq64be, frag);
  1021. if (item == NULL) {
  1022. dtls1_hm_fragment_free(frag);
  1023. return 0;
  1024. }
  1025. pqueue_insert(s->d1->sent_messages, item);
  1026. return 1;
  1027. }
  1028. int dtls1_retransmit_message(SSL_CONNECTION *s, unsigned short seq, int *found)
  1029. {
  1030. int ret;
  1031. /* XDTLS: for now assuming that read/writes are blocking */
  1032. pitem *item;
  1033. hm_fragment *frag;
  1034. unsigned long header_length;
  1035. unsigned char seq64be[8];
  1036. struct dtls1_retransmit_state saved_state;
  1037. /* XDTLS: the requested message ought to be found, otherwise error */
  1038. memset(seq64be, 0, sizeof(seq64be));
  1039. seq64be[6] = (unsigned char)(seq >> 8);
  1040. seq64be[7] = (unsigned char)seq;
  1041. item = pqueue_find(s->d1->sent_messages, seq64be);
  1042. if (item == NULL) {
  1043. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1044. *found = 0;
  1045. return 0;
  1046. }
  1047. *found = 1;
  1048. frag = (hm_fragment *)item->data;
  1049. if (frag->msg_header.is_ccs)
  1050. header_length = DTLS1_CCS_HEADER_LENGTH;
  1051. else
  1052. header_length = DTLS1_HM_HEADER_LENGTH;
  1053. memcpy(s->init_buf->data, frag->fragment,
  1054. frag->msg_header.msg_len + header_length);
  1055. s->init_num = frag->msg_header.msg_len + header_length;
  1056. dtls1_set_message_header_int(s, frag->msg_header.type,
  1057. frag->msg_header.msg_len,
  1058. frag->msg_header.seq, 0,
  1059. frag->msg_header.frag_len);
  1060. /* save current state */
  1061. saved_state.wrlmethod = s->rlayer.wrlmethod;
  1062. saved_state.wrl = s->rlayer.wrl;
  1063. s->d1->retransmitting = 1;
  1064. /* restore state in which the message was originally sent */
  1065. s->rlayer.wrlmethod = frag->msg_header.saved_retransmit_state.wrlmethod;
  1066. s->rlayer.wrl = frag->msg_header.saved_retransmit_state.wrl;
  1067. /*
  1068. * The old wrl may be still pointing at an old BIO. Update it to what we're
  1069. * using now.
  1070. */
  1071. s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
  1072. ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
  1073. SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
  1074. /* restore current state */
  1075. s->rlayer.wrlmethod = saved_state.wrlmethod;
  1076. s->rlayer.wrl = saved_state.wrl;
  1077. s->d1->retransmitting = 0;
  1078. (void)BIO_flush(s->wbio);
  1079. return ret;
  1080. }
  1081. void dtls1_set_message_header(SSL_CONNECTION *s,
  1082. unsigned char mt, size_t len,
  1083. size_t frag_off, size_t frag_len)
  1084. {
  1085. if (frag_off == 0) {
  1086. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1087. s->d1->next_handshake_write_seq++;
  1088. }
  1089. dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
  1090. frag_off, frag_len);
  1091. }
  1092. /* don't actually do the writing, wait till the MTU has been retrieved */
  1093. static void
  1094. dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,
  1095. size_t len, unsigned short seq_num,
  1096. size_t frag_off, size_t frag_len)
  1097. {
  1098. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1099. msg_hdr->type = mt;
  1100. msg_hdr->msg_len = len;
  1101. msg_hdr->seq = seq_num;
  1102. msg_hdr->frag_off = frag_off;
  1103. msg_hdr->frag_len = frag_len;
  1104. }
  1105. static void
  1106. dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off, size_t frag_len)
  1107. {
  1108. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1109. msg_hdr->frag_off = frag_off;
  1110. msg_hdr->frag_len = frag_len;
  1111. }
  1112. static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,
  1113. unsigned char *p)
  1114. {
  1115. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1116. *p++ = msg_hdr->type;
  1117. l2n3(msg_hdr->msg_len, p);
  1118. s2n(msg_hdr->seq, p);
  1119. l2n3(msg_hdr->frag_off, p);
  1120. l2n3(msg_hdr->frag_len, p);
  1121. return p;
  1122. }
  1123. void dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
  1124. {
  1125. memset(msg_hdr, 0, sizeof(*msg_hdr));
  1126. msg_hdr->type = *(data++);
  1127. n2l3(data, msg_hdr->msg_len);
  1128. n2s(data, msg_hdr->seq);
  1129. n2l3(data, msg_hdr->frag_off);
  1130. n2l3(data, msg_hdr->frag_len);
  1131. }
  1132. int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype)
  1133. {
  1134. unsigned char *header;
  1135. if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
  1136. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1137. dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
  1138. s->d1->handshake_write_seq, 0, 0);
  1139. if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
  1140. return 0;
  1141. } else {
  1142. dtls1_set_message_header(s, htype, 0, 0, 0);
  1143. /*
  1144. * We allocate space at the start for the message header. This gets
  1145. * filled in later
  1146. */
  1147. if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
  1148. || !WPACKET_start_sub_packet(pkt))
  1149. return 0;
  1150. }
  1151. return 1;
  1152. }
  1153. int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype)
  1154. {
  1155. size_t msglen;
  1156. if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
  1157. || !WPACKET_get_length(pkt, &msglen)
  1158. || msglen > INT_MAX)
  1159. return 0;
  1160. if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
  1161. s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
  1162. s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
  1163. }
  1164. s->init_num = (int)msglen;
  1165. s->init_off = 0;
  1166. if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  1167. /* Buffer the message to handle re-xmits */
  1168. if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC
  1169. ? 1 : 0))
  1170. return 0;
  1171. }
  1172. return 1;
  1173. }