statem_dtls.c 39 KB

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