statem_dtls.c 41 KB

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