2
0

statem_dtls.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Copyright 2005-2018 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_locl.h"
  13. #include "statem_locl.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. SSLerr(SSL_F_DTLS1_HM_FRAGMENT_NEW, ERR_R_MALLOC_FAILURE);
  56. return NULL;
  57. }
  58. if (frag_len) {
  59. if ((buf = OPENSSL_malloc(frag_len)) == NULL) {
  60. SSLerr(SSL_F_DTLS1_HM_FRAGMENT_NEW, 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. SSLerr(SSL_F_DTLS1_HM_FRAGMENT_NEW, 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_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &
  117. EVP_CIPH_FLAG_AEAD_CIPHER) != 0)
  118. mac_size = 0;
  119. else
  120. mac_size = EVP_MD_CTX_size(s->write_hash);
  121. } else
  122. mac_size = 0;
  123. if (s->enc_write_ctx &&
  124. (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))
  125. blocksize = 2 * EVP_CIPHER_CTX_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 > s->max_send_fragment)
  193. len = s->max_send_fragment;
  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, size_t *len)
  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. *len = s->init_num;
  311. if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  312. if (s->msg_callback) {
  313. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
  314. p, 1, s, s->msg_callback_arg);
  315. }
  316. /*
  317. * This isn't a real handshake message so skip the processing below.
  318. */
  319. return 1;
  320. }
  321. msg_len = msg_hdr->msg_len;
  322. /* reconstruct message header */
  323. *(p++) = msg_hdr->type;
  324. l2n3(msg_len, p);
  325. s2n(msg_hdr->seq, p);
  326. l2n3(0, p);
  327. l2n3(msg_len, p);
  328. if (s->version != DTLS1_BAD_VER) {
  329. p -= DTLS1_HM_HEADER_LENGTH;
  330. msg_len += DTLS1_HM_HEADER_LENGTH;
  331. }
  332. /*
  333. * If receiving Finished, record MAC of prior handshake messages for
  334. * Finished verification.
  335. */
  336. if (*mt == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
  337. /* SSLfatal() already called */
  338. return 0;
  339. }
  340. if (!ssl3_finish_mac(s, p, msg_len))
  341. return 0;
  342. if (s->msg_callback)
  343. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  344. p, msg_len, s, s->msg_callback_arg);
  345. memset(msg_hdr, 0, sizeof(*msg_hdr));
  346. s->d1->handshake_read_seq++;
  347. s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  348. return 1;
  349. }
  350. /*
  351. * dtls1_max_handshake_message_len returns the maximum number of bytes
  352. * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
  353. * may be greater if the maximum certificate list size requires it.
  354. */
  355. static size_t dtls1_max_handshake_message_len(const SSL *s)
  356. {
  357. size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
  358. if (max_len < s->max_cert_list)
  359. return s->max_cert_list;
  360. return max_len;
  361. }
  362. static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr)
  363. {
  364. size_t frag_off, frag_len, msg_len;
  365. msg_len = msg_hdr->msg_len;
  366. frag_off = msg_hdr->frag_off;
  367. frag_len = msg_hdr->frag_len;
  368. /* sanity checking */
  369. if ((frag_off + frag_len) > msg_len
  370. || msg_len > dtls1_max_handshake_message_len(s)) {
  371. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_PREPROCESS_FRAGMENT,
  372. SSL_R_EXCESSIVE_MESSAGE_SIZE);
  373. return 0;
  374. }
  375. if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
  376. /*
  377. * msg_len is limited to 2^24, but is effectively checked against
  378. * dtls_max_handshake_message_len(s) above
  379. */
  380. if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
  381. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PREPROCESS_FRAGMENT,
  382. ERR_R_BUF_LIB);
  383. return 0;
  384. }
  385. s->s3.tmp.message_size = msg_len;
  386. s->d1->r_msg_hdr.msg_len = msg_len;
  387. s->s3.tmp.message_type = msg_hdr->type;
  388. s->d1->r_msg_hdr.type = msg_hdr->type;
  389. s->d1->r_msg_hdr.seq = msg_hdr->seq;
  390. } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
  391. /*
  392. * They must be playing with us! BTW, failure to enforce upper limit
  393. * would open possibility for buffer overrun.
  394. */
  395. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_PREPROCESS_FRAGMENT,
  396. SSL_R_EXCESSIVE_MESSAGE_SIZE);
  397. return 0;
  398. }
  399. return 1;
  400. }
  401. /*
  402. * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
  403. * fatal error.
  404. */
  405. static int dtls1_retrieve_buffered_fragment(SSL *s, size_t *len)
  406. {
  407. /*-
  408. * (0) check whether the desired fragment is available
  409. * if so:
  410. * (1) copy over the fragment to s->init_buf->data[]
  411. * (2) update s->init_num
  412. */
  413. pitem *item;
  414. hm_fragment *frag;
  415. int ret;
  416. do {
  417. item = pqueue_peek(s->d1->buffered_messages);
  418. if (item == NULL)
  419. return 0;
  420. frag = (hm_fragment *)item->data;
  421. if (frag->msg_header.seq < s->d1->handshake_read_seq) {
  422. /* This is a stale message that has been buffered so clear it */
  423. pqueue_pop(s->d1->buffered_messages);
  424. dtls1_hm_fragment_free(frag);
  425. pitem_free(item);
  426. item = NULL;
  427. frag = NULL;
  428. }
  429. } while (item == NULL);
  430. /* Don't return if reassembly still in progress */
  431. if (frag->reassembly != NULL)
  432. return 0;
  433. if (s->d1->handshake_read_seq == frag->msg_header.seq) {
  434. size_t frag_len = frag->msg_header.frag_len;
  435. pqueue_pop(s->d1->buffered_messages);
  436. /* Calls SSLfatal() as required */
  437. ret = dtls1_preprocess_fragment(s, &frag->msg_header);
  438. if (ret && frag->msg_header.frag_len > 0) {
  439. unsigned char *p =
  440. (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  441. memcpy(&p[frag->msg_header.frag_off], frag->fragment,
  442. frag->msg_header.frag_len);
  443. }
  444. dtls1_hm_fragment_free(frag);
  445. pitem_free(item);
  446. if (ret) {
  447. *len = frag_len;
  448. return 1;
  449. }
  450. /* Fatal error */
  451. s->init_num = 0;
  452. return -1;
  453. } else {
  454. return 0;
  455. }
  456. }
  457. static int
  458. dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr)
  459. {
  460. hm_fragment *frag = NULL;
  461. pitem *item = NULL;
  462. int i = -1, is_complete;
  463. unsigned char seq64be[8];
  464. size_t frag_len = msg_hdr->frag_len;
  465. size_t readbytes;
  466. if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
  467. msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
  468. goto err;
  469. if (frag_len == 0) {
  470. return DTLS1_HM_FRAGMENT_RETRY;
  471. }
  472. /* Try to find item in queue */
  473. memset(seq64be, 0, sizeof(seq64be));
  474. seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
  475. seq64be[7] = (unsigned char)msg_hdr->seq;
  476. item = pqueue_find(s->d1->buffered_messages, seq64be);
  477. if (item == NULL) {
  478. frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
  479. if (frag == NULL)
  480. goto err;
  481. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  482. frag->msg_header.frag_len = frag->msg_header.msg_len;
  483. frag->msg_header.frag_off = 0;
  484. } else {
  485. frag = (hm_fragment *)item->data;
  486. if (frag->msg_header.msg_len != msg_hdr->msg_len) {
  487. item = NULL;
  488. frag = NULL;
  489. goto err;
  490. }
  491. }
  492. /*
  493. * If message is already reassembled, this must be a retransmit and can
  494. * be dropped. In this case item != NULL and so frag does not need to be
  495. * freed.
  496. */
  497. if (frag->reassembly == NULL) {
  498. unsigned char devnull[256];
  499. while (frag_len) {
  500. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  501. devnull,
  502. frag_len >
  503. sizeof(devnull) ? sizeof(devnull) :
  504. frag_len, 0, &readbytes);
  505. if (i <= 0)
  506. goto err;
  507. frag_len -= readbytes;
  508. }
  509. return DTLS1_HM_FRAGMENT_RETRY;
  510. }
  511. /* read the body of the fragment (header has already been read */
  512. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  513. frag->fragment + msg_hdr->frag_off,
  514. frag_len, 0, &readbytes);
  515. if (i <= 0 || readbytes != frag_len)
  516. i = -1;
  517. if (i <= 0)
  518. goto err;
  519. RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
  520. (long)(msg_hdr->frag_off + frag_len));
  521. if (!ossl_assert(msg_hdr->msg_len > 0))
  522. goto err;
  523. RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
  524. is_complete);
  525. if (is_complete) {
  526. OPENSSL_free(frag->reassembly);
  527. frag->reassembly = NULL;
  528. }
  529. if (item == NULL) {
  530. item = pitem_new(seq64be, frag);
  531. if (item == NULL) {
  532. i = -1;
  533. goto err;
  534. }
  535. item = pqueue_insert(s->d1->buffered_messages, item);
  536. /*
  537. * pqueue_insert fails iff a duplicate item is inserted. However,
  538. * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
  539. * would have returned it and control would never have reached this
  540. * branch.
  541. */
  542. if (!ossl_assert(item != NULL))
  543. goto err;
  544. }
  545. return DTLS1_HM_FRAGMENT_RETRY;
  546. err:
  547. if (item == NULL)
  548. dtls1_hm_fragment_free(frag);
  549. return -1;
  550. }
  551. static int
  552. dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st *msg_hdr)
  553. {
  554. int i = -1;
  555. hm_fragment *frag = NULL;
  556. pitem *item = NULL;
  557. unsigned char seq64be[8];
  558. size_t frag_len = msg_hdr->frag_len;
  559. size_t readbytes;
  560. if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
  561. goto err;
  562. /* Try to find item in queue, to prevent duplicate entries */
  563. memset(seq64be, 0, sizeof(seq64be));
  564. seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
  565. seq64be[7] = (unsigned char)msg_hdr->seq;
  566. item = pqueue_find(s->d1->buffered_messages, seq64be);
  567. /*
  568. * If we already have an entry and this one is a fragment, don't discard
  569. * it and rather try to reassemble it.
  570. */
  571. if (item != NULL && frag_len != msg_hdr->msg_len)
  572. item = NULL;
  573. /*
  574. * Discard the message if sequence number was already there, is too far
  575. * in the future, already in the queue or if we received a FINISHED
  576. * before the SERVER_HELLO, which then must be a stale retransmit.
  577. */
  578. if (msg_hdr->seq <= s->d1->handshake_read_seq ||
  579. msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
  580. (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
  581. unsigned char devnull[256];
  582. while (frag_len) {
  583. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  584. devnull,
  585. frag_len >
  586. sizeof(devnull) ? sizeof(devnull) :
  587. frag_len, 0, &readbytes);
  588. if (i <= 0)
  589. goto err;
  590. frag_len -= readbytes;
  591. }
  592. } else {
  593. if (frag_len != msg_hdr->msg_len) {
  594. return dtls1_reassemble_fragment(s, msg_hdr);
  595. }
  596. if (frag_len > dtls1_max_handshake_message_len(s))
  597. goto err;
  598. frag = dtls1_hm_fragment_new(frag_len, 0);
  599. if (frag == NULL)
  600. goto err;
  601. memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
  602. if (frag_len) {
  603. /*
  604. * read the body of the fragment (header has already been read
  605. */
  606. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  607. frag->fragment, frag_len, 0,
  608. &readbytes);
  609. if (i<=0 || readbytes != frag_len)
  610. i = -1;
  611. if (i <= 0)
  612. goto err;
  613. }
  614. item = pitem_new(seq64be, frag);
  615. if (item == NULL)
  616. goto err;
  617. item = pqueue_insert(s->d1->buffered_messages, item);
  618. /*
  619. * pqueue_insert fails iff a duplicate item is inserted. However,
  620. * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
  621. * would have returned it. Then, either |frag_len| !=
  622. * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
  623. * have been processed with |dtls1_reassemble_fragment|, above, or
  624. * the record will have been discarded.
  625. */
  626. if (!ossl_assert(item != NULL))
  627. goto err;
  628. }
  629. return DTLS1_HM_FRAGMENT_RETRY;
  630. err:
  631. if (item == NULL)
  632. dtls1_hm_fragment_free(frag);
  633. return 0;
  634. }
  635. static int dtls_get_reassembled_message(SSL *s, int *errtype, size_t *len)
  636. {
  637. unsigned char wire[DTLS1_HM_HEADER_LENGTH];
  638. size_t mlen, frag_off, frag_len;
  639. int i, ret, recvd_type;
  640. struct hm_header_st msg_hdr;
  641. size_t readbytes;
  642. *errtype = 0;
  643. redo:
  644. /* see if we have the required fragment already */
  645. ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
  646. if (ret < 0) {
  647. /* SSLfatal() already called */
  648. return 0;
  649. }
  650. if (ret > 0) {
  651. s->init_num = frag_len;
  652. *len = frag_len;
  653. return 1;
  654. }
  655. /* read handshake message header */
  656. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,
  657. DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
  658. if (i <= 0) { /* nbio, or an error */
  659. s->rwstate = SSL_READING;
  660. *len = 0;
  661. return 0;
  662. }
  663. if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  664. if (wire[0] != SSL3_MT_CCS) {
  665. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  666. SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
  667. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  668. goto f_err;
  669. }
  670. memcpy(s->init_buf->data, wire, readbytes);
  671. s->init_num = readbytes - 1;
  672. s->init_msg = s->init_buf->data + 1;
  673. s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
  674. s->s3.tmp.message_size = readbytes - 1;
  675. *len = readbytes - 1;
  676. return 1;
  677. }
  678. /* Handshake fails if message header is incomplete */
  679. if (readbytes != DTLS1_HM_HEADER_LENGTH) {
  680. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  681. SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  682. goto f_err;
  683. }
  684. /* parse the message fragment header */
  685. dtls1_get_message_header(wire, &msg_hdr);
  686. mlen = msg_hdr.msg_len;
  687. frag_off = msg_hdr.frag_off;
  688. frag_len = msg_hdr.frag_len;
  689. /*
  690. * We must have at least frag_len bytes left in the record to be read.
  691. * Fragments must not span records.
  692. */
  693. if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {
  694. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  695. SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);
  696. goto f_err;
  697. }
  698. /*
  699. * if this is a future (or stale) message it gets buffered
  700. * (or dropped)--no further processing at this time
  701. * While listening, we accept seq 1 (ClientHello with cookie)
  702. * although we're still expecting seq 0 (ClientHello)
  703. */
  704. if (msg_hdr.seq != s->d1->handshake_read_seq) {
  705. *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
  706. return 0;
  707. }
  708. if (frag_len && frag_len < mlen) {
  709. *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
  710. return 0;
  711. }
  712. if (!s->server
  713. && s->d1->r_msg_hdr.frag_off == 0
  714. && s->statem.hand_state != TLS_ST_OK
  715. && wire[0] == SSL3_MT_HELLO_REQUEST) {
  716. /*
  717. * The server may always send 'Hello Request' messages -- we are
  718. * doing a handshake anyway now, so ignore them if their format is
  719. * correct. Does not count for 'Finished' MAC.
  720. */
  721. if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
  722. if (s->msg_callback)
  723. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  724. wire, DTLS1_HM_HEADER_LENGTH, s,
  725. s->msg_callback_arg);
  726. s->init_num = 0;
  727. goto redo;
  728. } else { /* Incorrectly formatted Hello request */
  729. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  730. SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
  731. SSL_R_UNEXPECTED_MESSAGE);
  732. goto f_err;
  733. }
  734. }
  735. if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
  736. /* SSLfatal() already called */
  737. goto f_err;
  738. }
  739. if (frag_len > 0) {
  740. unsigned char *p =
  741. (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
  742. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  743. &p[frag_off], frag_len, 0, &readbytes);
  744. /*
  745. * This shouldn't ever fail due to NBIO because we already checked
  746. * that we have enough data in the record
  747. */
  748. if (i <= 0) {
  749. s->rwstate = SSL_READING;
  750. *len = 0;
  751. return 0;
  752. }
  753. } else {
  754. readbytes = 0;
  755. }
  756. /*
  757. * XDTLS: an incorrectly formatted fragment should cause the handshake
  758. * to fail
  759. */
  760. if (readbytes != frag_len) {
  761. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  762. SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);
  763. goto f_err;
  764. }
  765. /*
  766. * Note that s->init_num is *not* used as current offset in
  767. * s->init_buf->data, but as a counter summing up fragments' lengths: as
  768. * soon as they sum up to handshake packet length, we assume we have got
  769. * all the fragments.
  770. */
  771. *len = s->init_num = frag_len;
  772. return 1;
  773. f_err:
  774. s->init_num = 0;
  775. *len = 0;
  776. return 0;
  777. }
  778. /*-
  779. * for these 2 messages, we need to
  780. * ssl->enc_read_ctx re-init
  781. * ssl->rlayer.read_sequence zero
  782. * ssl->s3.read_mac_secret re-init
  783. * ssl->session->read_sym_enc assign
  784. * ssl->session->read_compression assign
  785. * ssl->session->read_hash assign
  786. */
  787. int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
  788. {
  789. if (s->version == DTLS1_BAD_VER) {
  790. s->d1->next_handshake_write_seq++;
  791. if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) {
  792. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  793. SSL_F_DTLS_CONSTRUCT_CHANGE_CIPHER_SPEC,
  794. ERR_R_INTERNAL_ERROR);
  795. return 0;
  796. }
  797. }
  798. return 1;
  799. }
  800. #ifndef OPENSSL_NO_SCTP
  801. /*
  802. * Wait for a dry event. Should only be called at a point in the handshake
  803. * where we are not expecting any data from the peer except an alert.
  804. */
  805. WORK_STATE dtls_wait_for_dry(SSL *s)
  806. {
  807. int ret, errtype;
  808. size_t len;
  809. /* read app data until dry event */
  810. ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
  811. if (ret < 0) {
  812. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS_WAIT_FOR_DRY,
  813. ERR_R_INTERNAL_ERROR);
  814. return WORK_ERROR;
  815. }
  816. if (ret == 0) {
  817. /*
  818. * We're not expecting any more messages from the peer at this point -
  819. * but we could get an alert. If an alert is waiting then we will never
  820. * return successfully. Therefore we attempt to read a message. This
  821. * should never succeed but will process any waiting alerts.
  822. */
  823. if (dtls_get_reassembled_message(s, &errtype, &len)) {
  824. /* The call succeeded! This should never happen */
  825. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS_WAIT_FOR_DRY,
  826. SSL_R_UNEXPECTED_MESSAGE);
  827. return WORK_ERROR;
  828. }
  829. s->s3.in_read_app_data = 2;
  830. s->rwstate = SSL_READING;
  831. BIO_clear_retry_flags(SSL_get_rbio(s));
  832. BIO_set_retry_read(SSL_get_rbio(s));
  833. return WORK_MORE_A;
  834. }
  835. return WORK_FINISHED_CONTINUE;
  836. }
  837. #endif
  838. int dtls1_read_failed(SSL *s, int code)
  839. {
  840. if (code > 0) {
  841. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  842. SSL_F_DTLS1_READ_FAILED, ERR_R_INTERNAL_ERROR);
  843. return 0;
  844. }
  845. if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) {
  846. /*
  847. * not a timeout, none of our business, let higher layers handle
  848. * this. in fact it's probably an error
  849. */
  850. return code;
  851. }
  852. /* done, no need to send a retransmit */
  853. if (!SSL_in_init(s))
  854. {
  855. BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
  856. return code;
  857. }
  858. return dtls1_handle_timeout(s);
  859. }
  860. int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
  861. {
  862. /*
  863. * The index of the retransmission queue actually is the message sequence
  864. * number, since the queue only contains messages of a single handshake.
  865. * However, the ChangeCipherSpec has no message sequence number and so
  866. * using only the sequence will result in the CCS and Finished having the
  867. * same index. To prevent this, the sequence number is multiplied by 2.
  868. * In case of a CCS 1 is subtracted. This does not only differ CSS and
  869. * Finished, it also maintains the order of the index (important for
  870. * priority queues) and fits in the unsigned short variable.
  871. */
  872. return seq * 2 - is_ccs;
  873. }
  874. int dtls1_retransmit_buffered_messages(SSL *s)
  875. {
  876. pqueue *sent = s->d1->sent_messages;
  877. piterator iter;
  878. pitem *item;
  879. hm_fragment *frag;
  880. int found = 0;
  881. iter = pqueue_iterator(sent);
  882. for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
  883. frag = (hm_fragment *)item->data;
  884. if (dtls1_retransmit_message(s, (unsigned short)
  885. dtls1_get_queue_priority
  886. (frag->msg_header.seq,
  887. frag->msg_header.is_ccs), &found) <= 0)
  888. return -1;
  889. }
  890. return 1;
  891. }
  892. int dtls1_buffer_message(SSL *s, int is_ccs)
  893. {
  894. pitem *item;
  895. hm_fragment *frag;
  896. unsigned char seq64be[8];
  897. /*
  898. * this function is called immediately after a message has been
  899. * serialized
  900. */
  901. if (!ossl_assert(s->init_off == 0))
  902. return 0;
  903. frag = dtls1_hm_fragment_new(s->init_num, 0);
  904. if (frag == NULL)
  905. return 0;
  906. memcpy(frag->fragment, s->init_buf->data, s->init_num);
  907. if (is_ccs) {
  908. /* For DTLS1_BAD_VER the header length is non-standard */
  909. if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
  910. ((s->version ==
  911. DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
  912. == (unsigned int)s->init_num))
  913. return 0;
  914. } else {
  915. if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
  916. DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num))
  917. return 0;
  918. }
  919. frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
  920. frag->msg_header.seq = s->d1->w_msg_hdr.seq;
  921. frag->msg_header.type = s->d1->w_msg_hdr.type;
  922. frag->msg_header.frag_off = 0;
  923. frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
  924. frag->msg_header.is_ccs = is_ccs;
  925. /* save current state */
  926. frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
  927. frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
  928. frag->msg_header.saved_retransmit_state.compress = s->compress;
  929. frag->msg_header.saved_retransmit_state.session = s->session;
  930. frag->msg_header.saved_retransmit_state.epoch =
  931. DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
  932. memset(seq64be, 0, sizeof(seq64be));
  933. seq64be[6] =
  934. (unsigned
  935. char)(dtls1_get_queue_priority(frag->msg_header.seq,
  936. frag->msg_header.is_ccs) >> 8);
  937. seq64be[7] =
  938. (unsigned
  939. char)(dtls1_get_queue_priority(frag->msg_header.seq,
  940. frag->msg_header.is_ccs));
  941. item = pitem_new(seq64be, frag);
  942. if (item == NULL) {
  943. dtls1_hm_fragment_free(frag);
  944. return 0;
  945. }
  946. pqueue_insert(s->d1->sent_messages, item);
  947. return 1;
  948. }
  949. int dtls1_retransmit_message(SSL *s, unsigned short seq, int *found)
  950. {
  951. int ret;
  952. /* XDTLS: for now assuming that read/writes are blocking */
  953. pitem *item;
  954. hm_fragment *frag;
  955. unsigned long header_length;
  956. unsigned char seq64be[8];
  957. struct dtls1_retransmit_state saved_state;
  958. /* XDTLS: the requested message ought to be found, otherwise error */
  959. memset(seq64be, 0, sizeof(seq64be));
  960. seq64be[6] = (unsigned char)(seq >> 8);
  961. seq64be[7] = (unsigned char)seq;
  962. item = pqueue_find(s->d1->sent_messages, seq64be);
  963. if (item == NULL) {
  964. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_RETRANSMIT_MESSAGE,
  965. ERR_R_INTERNAL_ERROR);
  966. *found = 0;
  967. return 0;
  968. }
  969. *found = 1;
  970. frag = (hm_fragment *)item->data;
  971. if (frag->msg_header.is_ccs)
  972. header_length = DTLS1_CCS_HEADER_LENGTH;
  973. else
  974. header_length = DTLS1_HM_HEADER_LENGTH;
  975. memcpy(s->init_buf->data, frag->fragment,
  976. frag->msg_header.msg_len + header_length);
  977. s->init_num = frag->msg_header.msg_len + header_length;
  978. dtls1_set_message_header_int(s, frag->msg_header.type,
  979. frag->msg_header.msg_len,
  980. frag->msg_header.seq, 0,
  981. frag->msg_header.frag_len);
  982. /* save current state */
  983. saved_state.enc_write_ctx = s->enc_write_ctx;
  984. saved_state.write_hash = s->write_hash;
  985. saved_state.compress = s->compress;
  986. saved_state.session = s->session;
  987. saved_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
  988. s->d1->retransmitting = 1;
  989. /* restore state in which the message was originally sent */
  990. s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
  991. s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
  992. s->compress = frag->msg_header.saved_retransmit_state.compress;
  993. s->session = frag->msg_header.saved_retransmit_state.session;
  994. DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer,
  995. frag->msg_header.
  996. saved_retransmit_state.epoch);
  997. ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
  998. SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
  999. /* restore current state */
  1000. s->enc_write_ctx = saved_state.enc_write_ctx;
  1001. s->write_hash = saved_state.write_hash;
  1002. s->compress = saved_state.compress;
  1003. s->session = saved_state.session;
  1004. DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer, saved_state.epoch);
  1005. s->d1->retransmitting = 0;
  1006. (void)BIO_flush(s->wbio);
  1007. return ret;
  1008. }
  1009. void dtls1_set_message_header(SSL *s,
  1010. unsigned char mt, size_t len,
  1011. size_t frag_off, size_t frag_len)
  1012. {
  1013. if (frag_off == 0) {
  1014. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1015. s->d1->next_handshake_write_seq++;
  1016. }
  1017. dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
  1018. frag_off, frag_len);
  1019. }
  1020. /* don't actually do the writing, wait till the MTU has been retrieved */
  1021. static void
  1022. dtls1_set_message_header_int(SSL *s, unsigned char mt,
  1023. size_t len, unsigned short seq_num,
  1024. size_t frag_off, size_t frag_len)
  1025. {
  1026. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1027. msg_hdr->type = mt;
  1028. msg_hdr->msg_len = len;
  1029. msg_hdr->seq = seq_num;
  1030. msg_hdr->frag_off = frag_off;
  1031. msg_hdr->frag_len = frag_len;
  1032. }
  1033. static void
  1034. dtls1_fix_message_header(SSL *s, size_t frag_off, size_t frag_len)
  1035. {
  1036. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1037. msg_hdr->frag_off = frag_off;
  1038. msg_hdr->frag_len = frag_len;
  1039. }
  1040. static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p)
  1041. {
  1042. struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
  1043. *p++ = msg_hdr->type;
  1044. l2n3(msg_hdr->msg_len, p);
  1045. s2n(msg_hdr->seq, p);
  1046. l2n3(msg_hdr->frag_off, p);
  1047. l2n3(msg_hdr->frag_len, p);
  1048. return p;
  1049. }
  1050. void dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
  1051. {
  1052. memset(msg_hdr, 0, sizeof(*msg_hdr));
  1053. msg_hdr->type = *(data++);
  1054. n2l3(data, msg_hdr->msg_len);
  1055. n2s(data, msg_hdr->seq);
  1056. n2l3(data, msg_hdr->frag_off);
  1057. n2l3(data, msg_hdr->frag_len);
  1058. }
  1059. int dtls1_set_handshake_header(SSL *s, WPACKET *pkt, int htype)
  1060. {
  1061. unsigned char *header;
  1062. if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
  1063. s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
  1064. dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
  1065. s->d1->handshake_write_seq, 0, 0);
  1066. if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
  1067. return 0;
  1068. } else {
  1069. dtls1_set_message_header(s, htype, 0, 0, 0);
  1070. /*
  1071. * We allocate space at the start for the message header. This gets
  1072. * filled in later
  1073. */
  1074. if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
  1075. || !WPACKET_start_sub_packet(pkt))
  1076. return 0;
  1077. }
  1078. return 1;
  1079. }
  1080. int dtls1_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
  1081. {
  1082. size_t msglen;
  1083. if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
  1084. || !WPACKET_get_length(pkt, &msglen)
  1085. || msglen > INT_MAX)
  1086. return 0;
  1087. if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
  1088. s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
  1089. s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
  1090. }
  1091. s->init_num = (int)msglen;
  1092. s->init_off = 0;
  1093. if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  1094. /* Buffer the message to handle re-xmits */
  1095. if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC
  1096. ? 1 : 0))
  1097. return 0;
  1098. }
  1099. return 1;
  1100. }