statem_dtls.c 42 KB

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