statem_dtls.c 44 KB

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