statem_dtls.c 44 KB

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