1
0

statem_dtls.c 44 KB

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