d1_pkt.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. /* ssl/d1_pkt.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@openssl.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  60. * All rights reserved.
  61. *
  62. * This package is an SSL implementation written
  63. * by Eric Young (eay@cryptsoft.com).
  64. * The implementation was written so as to conform with Netscapes SSL.
  65. *
  66. * This library is free for commercial and non-commercial use as long as
  67. * the following conditions are aheared to. The following conditions
  68. * apply to all code found in this distribution, be it the RC4, RSA,
  69. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  70. * included with this distribution is covered by the same copyright terms
  71. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  72. *
  73. * Copyright remains Eric Young's, and as such any Copyright notices in
  74. * the code are not to be removed.
  75. * If this package is used in a product, Eric Young should be given attribution
  76. * as the author of the parts of the library used.
  77. * This can be in the form of a textual message at program startup or
  78. * in documentation (online or textual) provided with the package.
  79. *
  80. * Redistribution and use in source and binary forms, with or without
  81. * modification, are permitted provided that the following conditions
  82. * are met:
  83. * 1. Redistributions of source code must retain the copyright
  84. * notice, this list of conditions and the following disclaimer.
  85. * 2. Redistributions in binary form must reproduce the above copyright
  86. * notice, this list of conditions and the following disclaimer in the
  87. * documentation and/or other materials provided with the distribution.
  88. * 3. All advertising materials mentioning features or use of this software
  89. * must display the following acknowledgement:
  90. * "This product includes cryptographic software written by
  91. * Eric Young (eay@cryptsoft.com)"
  92. * The word 'cryptographic' can be left out if the rouines from the library
  93. * being used are not cryptographic related :-).
  94. * 4. If you include any Windows specific code (or a derivative thereof) from
  95. * the apps directory (application code) you must include an acknowledgement:
  96. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  97. *
  98. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  99. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  100. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  101. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  102. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  103. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  104. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  105. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  106. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  107. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  108. * SUCH DAMAGE.
  109. *
  110. * The licence and distribution terms for any publically available version or
  111. * derivative of this code cannot be changed. i.e. this code cannot simply be
  112. * copied and put under another distribution licence
  113. * [including the GNU Public Licence.]
  114. */
  115. #include <stdio.h>
  116. #include <errno.h>
  117. #define USE_SOCKETS
  118. #include "ssl_locl.h"
  119. #include <openssl/evp.h>
  120. #include <openssl/buffer.h>
  121. #include <openssl/pqueue.h>
  122. #include <openssl/rand.h>
  123. static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
  124. int len, int peek);
  125. static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap,
  126. PQ_64BIT * seq_num);
  127. static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
  128. static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
  129. unsigned int *is_next_epoch);
  130. #if 0
  131. static int dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr,
  132. unsigned short *priority,
  133. unsigned long *offset);
  134. #endif
  135. static int dtls1_buffer_record(SSL *s, record_pqueue *q, PQ_64BIT * priority);
  136. static int dtls1_process_record(SSL *s);
  137. #if PQ_64BIT_IS_INTEGER
  138. static PQ_64BIT bytes_to_long_long(unsigned char *bytes, PQ_64BIT * num);
  139. #endif
  140. /* copy buffered record into SSL structure */
  141. static int dtls1_copy_record(SSL *s, pitem *item)
  142. {
  143. DTLS1_RECORD_DATA *rdata;
  144. rdata = (DTLS1_RECORD_DATA *)item->data;
  145. if (s->s3->rbuf.buf != NULL)
  146. OPENSSL_free(s->s3->rbuf.buf);
  147. s->packet = rdata->packet;
  148. s->packet_length = rdata->packet_length;
  149. memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER));
  150. memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD));
  151. /* Set proper sequence number for mac calculation */
  152. memcpy(&(s->s3->read_sequence[2]), &(rdata->packet[5]), 6);
  153. return (1);
  154. }
  155. static int
  156. dtls1_buffer_record(SSL *s, record_pqueue *queue, PQ_64BIT * priority)
  157. {
  158. DTLS1_RECORD_DATA *rdata;
  159. pitem *item;
  160. /* Limit the size of the queue to prevent DOS attacks */
  161. if (pqueue_size(queue->q) >= 100)
  162. return 0;
  163. rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA));
  164. item = pitem_new(*priority, rdata);
  165. if (rdata == NULL || item == NULL) {
  166. if (rdata != NULL)
  167. OPENSSL_free(rdata);
  168. if (item != NULL)
  169. pitem_free(item);
  170. SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
  171. return (0);
  172. }
  173. rdata->packet = s->packet;
  174. rdata->packet_length = s->packet_length;
  175. memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER));
  176. memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD));
  177. item->data = rdata;
  178. /* insert should not fail, since duplicates are dropped */
  179. if (pqueue_insert(queue->q, item) == NULL) {
  180. OPENSSL_free(rdata);
  181. pitem_free(item);
  182. return (0);
  183. }
  184. s->packet = NULL;
  185. s->packet_length = 0;
  186. memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER));
  187. memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD));
  188. if (!ssl3_setup_buffers(s)) {
  189. SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
  190. OPENSSL_free(rdata);
  191. pitem_free(item);
  192. return (0);
  193. }
  194. return (1);
  195. }
  196. static int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
  197. {
  198. pitem *item;
  199. item = pqueue_pop(queue->q);
  200. if (item) {
  201. dtls1_copy_record(s, item);
  202. OPENSSL_free(item->data);
  203. pitem_free(item);
  204. return (1);
  205. }
  206. return (0);
  207. }
  208. /*
  209. * retrieve a buffered record that belongs to the new epoch, i.e., not
  210. * processed yet
  211. */
  212. #define dtls1_get_unprocessed_record(s) \
  213. dtls1_retrieve_buffered_record((s), \
  214. &((s)->d1->unprocessed_rcds))
  215. /*
  216. * retrieve a buffered record that belongs to the current epoch, ie,
  217. * processed
  218. */
  219. #define dtls1_get_processed_record(s) \
  220. dtls1_retrieve_buffered_record((s), \
  221. &((s)->d1->processed_rcds))
  222. static int dtls1_process_buffered_records(SSL *s)
  223. {
  224. pitem *item;
  225. item = pqueue_peek(s->d1->unprocessed_rcds.q);
  226. if (item) {
  227. /* Check if epoch is current. */
  228. if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch)
  229. return (1); /* Nothing to do. */
  230. /* Process all the records. */
  231. while (pqueue_peek(s->d1->unprocessed_rcds.q)) {
  232. dtls1_get_unprocessed_record(s);
  233. if (!dtls1_process_record(s))
  234. return (0);
  235. dtls1_buffer_record(s, &(s->d1->processed_rcds),
  236. &s->s3->rrec.seq_num);
  237. }
  238. }
  239. /*
  240. * sync epoch numbers once all the unprocessed records have been
  241. * processed
  242. */
  243. s->d1->processed_rcds.epoch = s->d1->r_epoch;
  244. s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1;
  245. return (1);
  246. }
  247. #if 0
  248. static int dtls1_get_buffered_record(SSL *s)
  249. {
  250. pitem *item;
  251. PQ_64BIT priority =
  252. (((PQ_64BIT) s->d1->handshake_read_seq) << 32) |
  253. ((PQ_64BIT) s->d1->r_msg_hdr.frag_off);
  254. /* if we're not (re)negotiating, nothing buffered */
  255. if (!SSL_in_init(s))
  256. return 0;
  257. item = pqueue_peek(s->d1->rcvd_records);
  258. if (item && item->priority == priority) {
  259. /*
  260. * Check if we've received the record of interest. It must be a
  261. * handshake record, since data records as passed up without
  262. * buffering
  263. */
  264. DTLS1_RECORD_DATA *rdata;
  265. item = pqueue_pop(s->d1->rcvd_records);
  266. rdata = (DTLS1_RECORD_DATA *)item->data;
  267. if (s->s3->rbuf.buf != NULL)
  268. OPENSSL_free(s->s3->rbuf.buf);
  269. s->packet = rdata->packet;
  270. s->packet_length = rdata->packet_length;
  271. memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER));
  272. memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD));
  273. OPENSSL_free(item->data);
  274. pitem_free(item);
  275. /* s->d1->next_expected_seq_num++; */
  276. return (1);
  277. }
  278. return 0;
  279. }
  280. #endif
  281. static int dtls1_process_record(SSL *s)
  282. {
  283. int i, al;
  284. int enc_err;
  285. SSL_SESSION *sess;
  286. SSL3_RECORD *rr;
  287. unsigned int mac_size, orig_len;
  288. unsigned char md[EVP_MAX_MD_SIZE];
  289. rr = &(s->s3->rrec);
  290. sess = s->session;
  291. /*
  292. * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  293. * and we have that many bytes in s->packet
  294. */
  295. rr->input = &(s->packet[DTLS1_RT_HEADER_LENGTH]);
  296. /*
  297. * ok, we can now read from 's->packet' data into 'rr' rr->input points
  298. * at rr->length bytes, which need to be copied into rr->data by either
  299. * the decryption or by the decompression When the data is 'copied' into
  300. * the rr->data buffer, rr->input will be pointed at the new buffer
  301. */
  302. /*
  303. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  304. * bytes of encrypted compressed stuff.
  305. */
  306. /* check is not needed I believe */
  307. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  308. al = SSL_AD_RECORD_OVERFLOW;
  309. SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  310. goto f_err;
  311. }
  312. /* decrypt in place in 'rr->input' */
  313. rr->data = rr->input;
  314. enc_err = s->method->ssl3_enc->enc(s, 0);
  315. /*-
  316. * enc_err is:
  317. * 0: (in non-constant time) if the record is publically invalid.
  318. * 1: if the padding is valid
  319. * -1: if the padding is invalid
  320. */
  321. if (enc_err == 0) {
  322. /* For DTLS we simply ignore bad packets. */
  323. rr->length = 0;
  324. s->packet_length = 0;
  325. goto err;
  326. }
  327. #ifdef TLS_DEBUG
  328. printf("dec %d\n", rr->length);
  329. {
  330. unsigned int z;
  331. for (z = 0; z < rr->length; z++)
  332. printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
  333. }
  334. printf("\n");
  335. #endif
  336. /* r->length is now the compressed data plus mac */
  337. if ((sess != NULL) && (s->enc_read_ctx != NULL) && (s->read_hash != NULL)) {
  338. /* s->read_hash != NULL => mac_size != -1 */
  339. unsigned char *mac = NULL;
  340. unsigned char mac_tmp[EVP_MAX_MD_SIZE];
  341. mac_size = EVP_MD_size(s->read_hash);
  342. OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
  343. /*
  344. * kludge: *_cbc_remove_padding passes padding length in rr->type
  345. */
  346. orig_len = rr->length + ((unsigned int)rr->type >> 8);
  347. /*
  348. * orig_len is the length of the record before any padding was
  349. * removed. This is public information, as is the MAC in use,
  350. * therefore we can safely process the record in a different amount
  351. * of time if it's too short to possibly contain a MAC.
  352. */
  353. if (orig_len < mac_size ||
  354. /* CBC records must have a padding length byte too. */
  355. (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  356. orig_len < mac_size + 1)) {
  357. al = SSL_AD_DECODE_ERROR;
  358. SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
  359. goto f_err;
  360. }
  361. if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
  362. /*
  363. * We update the length so that the TLS header bytes can be
  364. * constructed correctly but we need to extract the MAC in
  365. * constant time from within the record, without leaking the
  366. * contents of the padding bytes.
  367. */
  368. mac = mac_tmp;
  369. ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len);
  370. rr->length -= mac_size;
  371. } else {
  372. /*
  373. * In this case there's no padding, so |orig_len| equals
  374. * |rec->length| and we checked that there's enough bytes for
  375. * |mac_size| above.
  376. */
  377. rr->length -= mac_size;
  378. mac = &rr->data[rr->length];
  379. }
  380. i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
  381. if (i < 0 || mac == NULL
  382. || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
  383. enc_err = -1;
  384. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  385. enc_err = -1;
  386. }
  387. if (enc_err < 0) {
  388. /* decryption failed, silently discard message */
  389. rr->length = 0;
  390. s->packet_length = 0;
  391. goto err;
  392. }
  393. /* r->length is now just compressed */
  394. if (s->expand != NULL) {
  395. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  396. al = SSL_AD_RECORD_OVERFLOW;
  397. SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
  398. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  399. goto f_err;
  400. }
  401. if (!ssl3_do_uncompress(s)) {
  402. al = SSL_AD_DECOMPRESSION_FAILURE;
  403. SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
  404. goto f_err;
  405. }
  406. }
  407. if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
  408. al = SSL_AD_RECORD_OVERFLOW;
  409. SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
  410. goto f_err;
  411. }
  412. rr->off = 0;
  413. /*-
  414. * So at this point the following is true
  415. * ssl->s3->rrec.type is the type of record
  416. * ssl->s3->rrec.length == number of bytes in record
  417. * ssl->s3->rrec.off == offset to first valid byte
  418. * ssl->s3->rrec.data == where to take bytes from, increment
  419. * after use :-).
  420. */
  421. /* we have pulled in a full packet so zero things */
  422. s->packet_length = 0;
  423. dtls1_record_bitmap_update(s, &(s->d1->bitmap)); /* Mark receipt of
  424. * record. */
  425. return (1);
  426. f_err:
  427. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  428. err:
  429. return (0);
  430. }
  431. /*-
  432. * Call this to get a new input record.
  433. * It will return <= 0 if more data is needed, normally due to an error
  434. * or non-blocking IO.
  435. * When it finishes, one packet has been decoded and can be found in
  436. * ssl->s3->rrec.type - is the type of record
  437. * ssl->s3->rrec.data, - data
  438. * ssl->s3->rrec.length, - number of bytes
  439. */
  440. /* used only by dtls1_read_bytes */
  441. int dtls1_get_record(SSL *s)
  442. {
  443. int ssl_major, ssl_minor;
  444. int i, n;
  445. SSL3_RECORD *rr;
  446. unsigned char *p = NULL;
  447. unsigned short version;
  448. DTLS1_BITMAP *bitmap;
  449. unsigned int is_next_epoch;
  450. rr = &(s->s3->rrec);
  451. /*
  452. * The epoch may have changed. If so, process all the pending records.
  453. * This is a non-blocking operation.
  454. */
  455. dtls1_process_buffered_records(s);
  456. /* if we're renegotiating, then there may be buffered records */
  457. if (dtls1_get_processed_record(s))
  458. return 1;
  459. /* get something from the wire */
  460. again:
  461. /* check if we have the header */
  462. if ((s->rstate != SSL_ST_READ_BODY) ||
  463. (s->packet_length < DTLS1_RT_HEADER_LENGTH)) {
  464. n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
  465. /* read timeout is handled by dtls1_read_bytes */
  466. if (n <= 0)
  467. return (n); /* error or non-blocking */
  468. /* this packet contained a partial record, dump it */
  469. if (s->packet_length != DTLS1_RT_HEADER_LENGTH) {
  470. s->packet_length = 0;
  471. goto again;
  472. }
  473. s->rstate = SSL_ST_READ_BODY;
  474. p = s->packet;
  475. /* Pull apart the header into the DTLS1_RECORD */
  476. rr->type = *(p++);
  477. ssl_major = *(p++);
  478. ssl_minor = *(p++);
  479. version = (ssl_major << 8) | ssl_minor;
  480. /* sequence number is 64 bits, with top 2 bytes = epoch */
  481. n2s(p, rr->epoch);
  482. memcpy(&(s->s3->read_sequence[2]), p, 6);
  483. p += 6;
  484. n2s(p, rr->length);
  485. /* Lets check version */
  486. if (!s->first_packet) {
  487. if (version != s->version && version != DTLS1_BAD_VER) {
  488. /* unexpected version, silently discard */
  489. rr->length = 0;
  490. s->packet_length = 0;
  491. goto again;
  492. }
  493. }
  494. if ((version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
  495. (version & 0xff00) != (DTLS1_BAD_VER & 0xff00)) {
  496. /* wrong version, silently discard record */
  497. rr->length = 0;
  498. s->packet_length = 0;
  499. goto again;
  500. }
  501. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  502. /* record too long, silently discard it */
  503. rr->length = 0;
  504. s->packet_length = 0;
  505. goto again;
  506. }
  507. s->client_version = version;
  508. /* now s->rstate == SSL_ST_READ_BODY */
  509. }
  510. /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
  511. if (rr->length > s->packet_length - DTLS1_RT_HEADER_LENGTH) {
  512. /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
  513. i = rr->length;
  514. n = ssl3_read_n(s, i, i, 1);
  515. /* this packet contained a partial record, dump it */
  516. if (n != i) {
  517. rr->length = 0;
  518. s->packet_length = 0;
  519. goto again;
  520. }
  521. /*
  522. * now n == rr->length, and s->packet_length ==
  523. * DTLS1_RT_HEADER_LENGTH + rr->length
  524. */
  525. }
  526. s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
  527. /* match epochs. NULL means the packet is dropped on the floor */
  528. bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
  529. if (bitmap == NULL) {
  530. rr->length = 0;
  531. s->packet_length = 0; /* dump this record */
  532. goto again; /* get another record */
  533. }
  534. /*
  535. * Check whether this is a repeat, or aged record. Don't check if we're
  536. * listening and this message is a ClientHello. They can look as if
  537. * they're replayed, since they arrive from different connections and
  538. * would be dropped unnecessarily.
  539. */
  540. if (!(s->d1->listen && rr->type == SSL3_RT_HANDSHAKE &&
  541. s->packet_length > DTLS1_RT_HEADER_LENGTH &&
  542. s->packet[DTLS1_RT_HEADER_LENGTH] == SSL3_MT_CLIENT_HELLO) &&
  543. !dtls1_record_replay_check(s, bitmap, &(rr->seq_num))) {
  544. rr->length = 0;
  545. s->packet_length = 0; /* dump this record */
  546. goto again; /* get another record */
  547. }
  548. /* just read a 0 length packet */
  549. if (rr->length == 0)
  550. goto again;
  551. /*
  552. * If this record is from the next epoch (either HM or ALERT), and a
  553. * handshake is currently in progress, buffer it since it cannot be
  554. * processed at this time. However, do not buffer anything while
  555. * listening.
  556. */
  557. if (is_next_epoch) {
  558. if ((SSL_in_init(s) || s->in_handshake) && !s->d1->listen) {
  559. dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), &rr->seq_num);
  560. }
  561. rr->length = 0;
  562. s->packet_length = 0;
  563. goto again;
  564. }
  565. if (!dtls1_process_record(s)) {
  566. rr->length = 0;
  567. s->packet_length = 0; /* dump this record */
  568. goto again; /* get another record */
  569. }
  570. return (1);
  571. }
  572. /*-
  573. * Return up to 'len' payload bytes received in 'type' records.
  574. * 'type' is one of the following:
  575. *
  576. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  577. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  578. * - 0 (during a shutdown, no data has to be returned)
  579. *
  580. * If we don't have stored data to work from, read a SSL/TLS record first
  581. * (possibly multiple records if we still don't have anything to return).
  582. *
  583. * This function must handle any surprises the peer may have for us, such as
  584. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  585. * a surprise, but handled as if it were), or renegotiation requests.
  586. * Also if record payloads contain fragments too small to process, we store
  587. * them until there is enough for the respective protocol (the record protocol
  588. * may use arbitrary fragmentation and even interleaving):
  589. * Change cipher spec protocol
  590. * just 1 byte needed, no need for keeping anything stored
  591. * Alert protocol
  592. * 2 bytes needed (AlertLevel, AlertDescription)
  593. * Handshake protocol
  594. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  595. * to detect unexpected Client Hello and Hello Request messages
  596. * here, anything else is handled by higher layers
  597. * Application data protocol
  598. * none of our business
  599. */
  600. int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
  601. {
  602. int al, i, j, ret;
  603. unsigned int n;
  604. SSL3_RECORD *rr;
  605. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  606. if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
  607. if (!ssl3_setup_buffers(s))
  608. return (-1);
  609. /* XXX: check what the second '&& type' is about */
  610. if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
  611. (type != SSL3_RT_HANDSHAKE) && type) ||
  612. (peek && (type != SSL3_RT_APPLICATION_DATA))) {
  613. SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
  614. return -1;
  615. }
  616. /*
  617. * check whether there's a handshake message (client hello?) waiting
  618. */
  619. if ((ret = have_handshake_fragment(s, type, buf, len, peek)))
  620. return ret;
  621. /*
  622. * Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
  623. */
  624. if (!s->in_handshake && SSL_in_init(s)) {
  625. /* type == SSL3_RT_APPLICATION_DATA */
  626. i = s->handshake_func(s);
  627. if (i < 0)
  628. return (i);
  629. if (i == 0) {
  630. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  631. return (-1);
  632. }
  633. }
  634. start:
  635. s->rwstate = SSL_NOTHING;
  636. /*-
  637. * s->s3->rrec.type - is the type of record
  638. * s->s3->rrec.data, - data
  639. * s->s3->rrec.off, - offset into 'data' for next read
  640. * s->s3->rrec.length, - number of bytes.
  641. */
  642. rr = &(s->s3->rrec);
  643. /*
  644. * We are not handshaking and have no data yet, so process data buffered
  645. * during the last handshake in advance, if any.
  646. */
  647. if (s->state == SSL_ST_OK && rr->length == 0) {
  648. pitem *item;
  649. item = pqueue_pop(s->d1->buffered_app_data.q);
  650. if (item) {
  651. dtls1_copy_record(s, item);
  652. OPENSSL_free(item->data);
  653. pitem_free(item);
  654. }
  655. }
  656. /* Check for timeout */
  657. if (dtls1_handle_timeout(s) > 0)
  658. goto start;
  659. /* get new packet if necessary */
  660. if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) {
  661. ret = dtls1_get_record(s);
  662. if (ret <= 0) {
  663. ret = dtls1_read_failed(s, ret);
  664. /* anything other than a timeout is an error */
  665. if (ret <= 0)
  666. return (ret);
  667. else
  668. goto start;
  669. }
  670. }
  671. if (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE) {
  672. rr->length = 0;
  673. goto start;
  674. }
  675. /* we now have a packet which can be read and processed */
  676. if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
  677. * reset by ssl3_get_finished */
  678. && (rr->type != SSL3_RT_HANDSHAKE)) {
  679. /*
  680. * We now have application data between CCS and Finished. Most likely
  681. * the packets were reordered on their way, so buffer the application
  682. * data for later processing rather than dropping the connection.
  683. */
  684. dtls1_buffer_record(s, &(s->d1->buffered_app_data), &rr->seq_num);
  685. rr->length = 0;
  686. goto start;
  687. }
  688. /*
  689. * If the other end has shut down, throw anything we read away (even in
  690. * 'peek' mode)
  691. */
  692. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  693. rr->length = 0;
  694. s->rwstate = SSL_NOTHING;
  695. return (0);
  696. }
  697. if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or
  698. * SSL3_RT_HANDSHAKE */
  699. /*
  700. * make sure that we are not getting application data when we are
  701. * doing a handshake for the first time
  702. */
  703. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  704. (s->enc_read_ctx == NULL)) {
  705. al = SSL_AD_UNEXPECTED_MESSAGE;
  706. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
  707. goto f_err;
  708. }
  709. if (len <= 0)
  710. return (len);
  711. if ((unsigned int)len > rr->length)
  712. n = rr->length;
  713. else
  714. n = (unsigned int)len;
  715. memcpy(buf, &(rr->data[rr->off]), n);
  716. if (!peek) {
  717. rr->length -= n;
  718. rr->off += n;
  719. if (rr->length == 0) {
  720. s->rstate = SSL_ST_READ_HEADER;
  721. rr->off = 0;
  722. }
  723. }
  724. return (n);
  725. }
  726. /*
  727. * If we get here, then type != rr->type; if we have a handshake message,
  728. * then it was unexpected (Hello Request or Client Hello).
  729. */
  730. /*
  731. * In case of record types for which we have 'fragment' storage, fill
  732. * that so that we can process the data at a fixed place.
  733. */
  734. {
  735. unsigned int k, dest_maxlen = 0;
  736. unsigned char *dest = NULL;
  737. unsigned int *dest_len = NULL;
  738. if (rr->type == SSL3_RT_HANDSHAKE) {
  739. dest_maxlen = sizeof s->d1->handshake_fragment;
  740. dest = s->d1->handshake_fragment;
  741. dest_len = &s->d1->handshake_fragment_len;
  742. } else if (rr->type == SSL3_RT_ALERT) {
  743. dest_maxlen = sizeof(s->d1->alert_fragment);
  744. dest = s->d1->alert_fragment;
  745. dest_len = &s->d1->alert_fragment_len;
  746. }
  747. /* else it's a CCS message, or application data or wrong */
  748. else if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
  749. /*
  750. * Application data while renegotiating is allowed. Try again
  751. * reading.
  752. */
  753. if (rr->type == SSL3_RT_APPLICATION_DATA) {
  754. BIO *bio;
  755. s->s3->in_read_app_data = 2;
  756. bio = SSL_get_rbio(s);
  757. s->rwstate = SSL_READING;
  758. BIO_clear_retry_flags(bio);
  759. BIO_set_retry_read(bio);
  760. return (-1);
  761. }
  762. /* Not certain if this is the right error handling */
  763. al = SSL_AD_UNEXPECTED_MESSAGE;
  764. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  765. goto f_err;
  766. }
  767. if (dest_maxlen > 0) {
  768. /*
  769. * XDTLS: In a pathalogical case, the Client Hello may be
  770. * fragmented--don't always expect dest_maxlen bytes
  771. */
  772. if (rr->length < dest_maxlen) {
  773. #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
  774. /*
  775. * for normal alerts rr->length is 2, while
  776. * dest_maxlen is 7 if we were to handle this
  777. * non-existing alert...
  778. */
  779. FIX ME
  780. #endif
  781. s->rstate = SSL_ST_READ_HEADER;
  782. rr->length = 0;
  783. goto start;
  784. }
  785. /* now move 'n' bytes: */
  786. for (k = 0; k < dest_maxlen; k++) {
  787. dest[k] = rr->data[rr->off++];
  788. rr->length--;
  789. }
  790. *dest_len = dest_maxlen;
  791. }
  792. }
  793. /*-
  794. * s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
  795. * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
  796. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
  797. */
  798. /* If we are a client, check for an incoming 'Hello Request': */
  799. if ((!s->server) &&
  800. (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
  801. (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
  802. (s->session != NULL) && (s->session->cipher != NULL)) {
  803. s->d1->handshake_fragment_len = 0;
  804. if ((s->d1->handshake_fragment[1] != 0) ||
  805. (s->d1->handshake_fragment[2] != 0) ||
  806. (s->d1->handshake_fragment[3] != 0)) {
  807. al = SSL_AD_DECODE_ERROR;
  808. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
  809. goto err;
  810. }
  811. /*
  812. * no need to check sequence number on HELLO REQUEST messages
  813. */
  814. if (s->msg_callback)
  815. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  816. s->d1->handshake_fragment, 4, s,
  817. s->msg_callback_arg);
  818. if (SSL_is_init_finished(s) &&
  819. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
  820. !s->s3->renegotiate) {
  821. s->d1->handshake_read_seq++;
  822. ssl3_renegotiate(s);
  823. if (ssl3_renegotiate_check(s)) {
  824. i = s->handshake_func(s);
  825. if (i < 0)
  826. return (i);
  827. if (i == 0) {
  828. SSLerr(SSL_F_DTLS1_READ_BYTES,
  829. SSL_R_SSL_HANDSHAKE_FAILURE);
  830. return (-1);
  831. }
  832. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  833. if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
  834. BIO *bio;
  835. /*
  836. * In the case where we try to read application data,
  837. * but we trigger an SSL handshake, we return -1 with
  838. * the retry option set. Otherwise renegotiation may
  839. * cause nasty problems in the blocking world
  840. */
  841. s->rwstate = SSL_READING;
  842. bio = SSL_get_rbio(s);
  843. BIO_clear_retry_flags(bio);
  844. BIO_set_retry_read(bio);
  845. return (-1);
  846. }
  847. }
  848. }
  849. }
  850. /*
  851. * we either finished a handshake or ignored the request, now try
  852. * again to obtain the (application) data we were asked for
  853. */
  854. goto start;
  855. }
  856. if (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {
  857. int alert_level = s->d1->alert_fragment[0];
  858. int alert_descr = s->d1->alert_fragment[1];
  859. s->d1->alert_fragment_len = 0;
  860. if (s->msg_callback)
  861. s->msg_callback(0, s->version, SSL3_RT_ALERT,
  862. s->d1->alert_fragment, 2, s, s->msg_callback_arg);
  863. if (s->info_callback != NULL)
  864. cb = s->info_callback;
  865. else if (s->ctx->info_callback != NULL)
  866. cb = s->ctx->info_callback;
  867. if (cb != NULL) {
  868. j = (alert_level << 8) | alert_descr;
  869. cb(s, SSL_CB_READ_ALERT, j);
  870. }
  871. if (alert_level == 1) { /* warning */
  872. s->s3->warn_alert = alert_descr;
  873. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  874. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  875. return (0);
  876. }
  877. #if 0
  878. /* XXX: this is a possible improvement in the future */
  879. /* now check if it's a missing record */
  880. if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
  881. unsigned short seq;
  882. unsigned int frag_off;
  883. unsigned char *p = &(s->d1->alert_fragment[2]);
  884. n2s(p, seq);
  885. n2l3(p, frag_off);
  886. dtls1_retransmit_message(s,
  887. dtls1_get_queue_priority
  888. (frag->msg_header.seq, 0), frag_off,
  889. &found);
  890. if (!found && SSL_in_init(s)) {
  891. /*
  892. * fprintf( stderr,"in init = %d\n", SSL_in_init(s));
  893. */
  894. /*
  895. * requested a message not yet sent, send an alert
  896. * ourselves
  897. */
  898. ssl3_send_alert(s, SSL3_AL_WARNING,
  899. DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
  900. }
  901. }
  902. #endif
  903. } else if (alert_level == 2) { /* fatal */
  904. char tmp[16];
  905. s->rwstate = SSL_NOTHING;
  906. s->s3->fatal_alert = alert_descr;
  907. SSLerr(SSL_F_DTLS1_READ_BYTES,
  908. SSL_AD_REASON_OFFSET + alert_descr);
  909. BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
  910. ERR_add_error_data(2, "SSL alert number ", tmp);
  911. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  912. SSL_CTX_remove_session(s->ctx, s->session);
  913. return (0);
  914. } else {
  915. al = SSL_AD_ILLEGAL_PARAMETER;
  916. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
  917. goto f_err;
  918. }
  919. goto start;
  920. }
  921. if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
  922. * shutdown */
  923. s->rwstate = SSL_NOTHING;
  924. rr->length = 0;
  925. return (0);
  926. }
  927. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  928. struct ccs_header_st ccs_hdr;
  929. unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH;
  930. dtls1_get_ccs_header(rr->data, &ccs_hdr);
  931. /*
  932. * 'Change Cipher Spec' is just a single byte, so we know exactly
  933. * what the record payload has to look like
  934. */
  935. /* XDTLS: check that epoch is consistent */
  936. if (s->client_version == DTLS1_BAD_VER || s->version == DTLS1_BAD_VER)
  937. ccs_hdr_len = 3;
  938. if ((rr->length != ccs_hdr_len) || (rr->off != 0)
  939. || (rr->data[0] != SSL3_MT_CCS)) {
  940. i = SSL_AD_ILLEGAL_PARAMETER;
  941. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  942. goto err;
  943. }
  944. rr->length = 0;
  945. if (s->msg_callback)
  946. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
  947. rr->data, 1, s, s->msg_callback_arg);
  948. /*
  949. * We can't process a CCS now, because previous handshake messages
  950. * are still missing, so just drop it.
  951. */
  952. if (!s->d1->change_cipher_spec_ok) {
  953. goto start;
  954. }
  955. s->d1->change_cipher_spec_ok = 0;
  956. s->s3->change_cipher_spec = 1;
  957. if (!ssl3_do_change_cipher_spec(s))
  958. goto err;
  959. /* do this whenever CCS is processed */
  960. dtls1_reset_seq_numbers(s, SSL3_CC_READ);
  961. if (s->client_version == DTLS1_BAD_VER)
  962. s->d1->handshake_read_seq++;
  963. goto start;
  964. }
  965. /*
  966. * Unexpected handshake message (Client Hello, or protocol violation)
  967. */
  968. if ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
  969. !s->in_handshake) {
  970. struct hm_header_st msg_hdr;
  971. /* this may just be a stale retransmit */
  972. dtls1_get_message_header(rr->data, &msg_hdr);
  973. if (rr->epoch != s->d1->r_epoch) {
  974. rr->length = 0;
  975. goto start;
  976. }
  977. /*
  978. * If we are server, we may have a repeated FINISHED of the client
  979. * here, then retransmit our CCS and FINISHED.
  980. */
  981. if (msg_hdr.type == SSL3_MT_FINISHED) {
  982. if (dtls1_check_timeout_num(s) < 0)
  983. return -1;
  984. dtls1_retransmit_buffered_messages(s);
  985. rr->length = 0;
  986. goto start;
  987. }
  988. if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
  989. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
  990. #if 0 /* worked only because C operator preferences
  991. * are not as expected (and because this is
  992. * not really needed for clients except for
  993. * detecting protocol violations): */
  994. s->state = SSL_ST_BEFORE | (s->server)
  995. ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  996. #else
  997. s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  998. #endif
  999. s->new_session = 1;
  1000. }
  1001. i = s->handshake_func(s);
  1002. if (i < 0)
  1003. return (i);
  1004. if (i == 0) {
  1005. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  1006. return (-1);
  1007. }
  1008. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1009. if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
  1010. BIO *bio;
  1011. /*
  1012. * In the case where we try to read application data, but we
  1013. * trigger an SSL handshake, we return -1 with the retry
  1014. * option set. Otherwise renegotiation may cause nasty
  1015. * problems in the blocking world
  1016. */
  1017. s->rwstate = SSL_READING;
  1018. bio = SSL_get_rbio(s);
  1019. BIO_clear_retry_flags(bio);
  1020. BIO_set_retry_read(bio);
  1021. return (-1);
  1022. }
  1023. }
  1024. goto start;
  1025. }
  1026. switch (rr->type) {
  1027. default:
  1028. #ifndef OPENSSL_NO_TLS
  1029. /* TLS just ignores unknown message types */
  1030. if (s->version == TLS1_VERSION) {
  1031. rr->length = 0;
  1032. goto start;
  1033. }
  1034. #endif
  1035. al = SSL_AD_UNEXPECTED_MESSAGE;
  1036. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1037. goto f_err;
  1038. case SSL3_RT_CHANGE_CIPHER_SPEC:
  1039. case SSL3_RT_ALERT:
  1040. case SSL3_RT_HANDSHAKE:
  1041. /*
  1042. * we already handled all of these, with the possible exception of
  1043. * SSL3_RT_HANDSHAKE when s->in_handshake is set, but that should not
  1044. * happen when type != rr->type
  1045. */
  1046. al = SSL_AD_UNEXPECTED_MESSAGE;
  1047. SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1048. goto f_err;
  1049. case SSL3_RT_APPLICATION_DATA:
  1050. /*
  1051. * At this point, we were expecting handshake data, but have
  1052. * application data. If the library was running inside ssl3_read()
  1053. * (i.e. in_read_app_data is set) and it makes sense to read
  1054. * application data at this point (session renegotiation not yet
  1055. * started), we will indulge it.
  1056. */
  1057. if (s->s3->in_read_app_data &&
  1058. (s->s3->total_renegotiations != 0) &&
  1059. (((s->state & SSL_ST_CONNECT) &&
  1060. (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
  1061. (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
  1062. ) || ((s->state & SSL_ST_ACCEPT) &&
  1063. (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
  1064. (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
  1065. )
  1066. )) {
  1067. s->s3->in_read_app_data = 2;
  1068. return (-1);
  1069. } else {
  1070. al = SSL_AD_UNEXPECTED_MESSAGE;
  1071. SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1072. goto f_err;
  1073. }
  1074. }
  1075. /* not reached */
  1076. f_err:
  1077. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1078. err:
  1079. return (-1);
  1080. }
  1081. int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len)
  1082. {
  1083. int i;
  1084. if (SSL_in_init(s) && !s->in_handshake) {
  1085. i = s->handshake_func(s);
  1086. if (i < 0)
  1087. return (i);
  1088. if (i == 0) {
  1089. SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,
  1090. SSL_R_SSL_HANDSHAKE_FAILURE);
  1091. return -1;
  1092. }
  1093. }
  1094. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  1095. SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG);
  1096. return -1;
  1097. }
  1098. i = dtls1_write_bytes(s, type, buf_, len);
  1099. return i;
  1100. }
  1101. /*
  1102. * this only happens when a client hello is received and a handshake
  1103. * is started.
  1104. */
  1105. static int
  1106. have_handshake_fragment(SSL *s, int type, unsigned char *buf,
  1107. int len, int peek)
  1108. {
  1109. if ((type == SSL3_RT_HANDSHAKE) && (s->d1->handshake_fragment_len > 0))
  1110. /* (partially) satisfy request from storage */
  1111. {
  1112. unsigned char *src = s->d1->handshake_fragment;
  1113. unsigned char *dst = buf;
  1114. unsigned int k, n;
  1115. /* peek == 0 */
  1116. n = 0;
  1117. while ((len > 0) && (s->d1->handshake_fragment_len > 0)) {
  1118. *dst++ = *src++;
  1119. len--;
  1120. s->d1->handshake_fragment_len--;
  1121. n++;
  1122. }
  1123. /* move any remaining fragment bytes: */
  1124. for (k = 0; k < s->d1->handshake_fragment_len; k++)
  1125. s->d1->handshake_fragment[k] = *src++;
  1126. return n;
  1127. }
  1128. return 0;
  1129. }
  1130. /*
  1131. * Call this to write data in records of type 'type' It will return <= 0 if
  1132. * not all data has been sent or non-blocking IO.
  1133. */
  1134. int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
  1135. {
  1136. int i;
  1137. OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
  1138. s->rwstate = SSL_NOTHING;
  1139. i = do_dtls1_write(s, type, buf, len, 0);
  1140. return i;
  1141. }
  1142. int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
  1143. unsigned int len, int create_empty_fragment)
  1144. {
  1145. unsigned char *p, *pseq;
  1146. int i, mac_size, clear = 0;
  1147. int prefix_len = 0;
  1148. SSL3_RECORD *wr;
  1149. SSL3_BUFFER *wb;
  1150. SSL_SESSION *sess;
  1151. int bs;
  1152. /*
  1153. * first check if there is a SSL3_BUFFER still being written out. This
  1154. * will happen with non blocking IO
  1155. */
  1156. if (s->s3->wbuf.left != 0) {
  1157. OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
  1158. return (ssl3_write_pending(s, type, buf, len));
  1159. }
  1160. /* If we have an alert to send, lets send it */
  1161. if (s->s3->alert_dispatch) {
  1162. i = s->method->ssl_dispatch_alert(s);
  1163. if (i <= 0)
  1164. return (i);
  1165. /* if it went, fall through and send more stuff */
  1166. }
  1167. if (len == 0 && !create_empty_fragment)
  1168. return 0;
  1169. wr = &(s->s3->wrec);
  1170. wb = &(s->s3->wbuf);
  1171. sess = s->session;
  1172. if ((sess == NULL) ||
  1173. (s->enc_write_ctx == NULL) || (s->write_hash == NULL))
  1174. clear = 1;
  1175. if (clear)
  1176. mac_size = 0;
  1177. else
  1178. mac_size = EVP_MD_size(s->write_hash);
  1179. /* DTLS implements explicit IV, so no need for empty fragments */
  1180. #if 0
  1181. /*
  1182. * 'create_empty_fragment' is true only when this function calls itself
  1183. */
  1184. if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done
  1185. && SSL_version(s) != DTLS1_VERSION && SSL_version(s) != DTLS1_BAD_VER)
  1186. {
  1187. /*
  1188. * countermeasure against known-IV weakness in CBC ciphersuites (see
  1189. * http://www.openssl.org/~bodo/tls-cbc.txt)
  1190. */
  1191. if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
  1192. /*
  1193. * recursive function call with 'create_empty_fragment' set; this
  1194. * prepares and buffers the data for an empty fragment (these
  1195. * 'prefix_len' bytes are sent out later together with the actual
  1196. * payload)
  1197. */
  1198. prefix_len = s->method->do_ssl_write(s, type, buf, 0, 1);
  1199. if (prefix_len <= 0)
  1200. goto err;
  1201. if (s->s3->wbuf.len <
  1202. (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE) {
  1203. /* insufficient space */
  1204. SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);
  1205. goto err;
  1206. }
  1207. }
  1208. s->s3->empty_fragment_done = 1;
  1209. }
  1210. #endif
  1211. p = wb->buf + prefix_len;
  1212. /* write the header */
  1213. *(p++) = type & 0xff;
  1214. wr->type = type;
  1215. if (s->client_version == DTLS1_BAD_VER)
  1216. *(p++) = DTLS1_BAD_VER >> 8, *(p++) = DTLS1_BAD_VER & 0xff;
  1217. else
  1218. *(p++) = (s->version >> 8), *(p++) = s->version & 0xff;
  1219. /* field where we are to write out packet epoch, seq num and len */
  1220. pseq = p;
  1221. p += 10;
  1222. /* lets setup the record stuff. */
  1223. /*
  1224. * Make space for the explicit IV in case of CBC. (this is a bit of a
  1225. * boundary violation, but what the heck).
  1226. */
  1227. if (s->enc_write_ctx &&
  1228. (EVP_CIPHER_mode(s->enc_write_ctx->cipher) & EVP_CIPH_CBC_MODE))
  1229. bs = EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
  1230. else
  1231. bs = 0;
  1232. wr->data = p + bs; /* make room for IV in case of CBC */
  1233. wr->length = (int)len;
  1234. wr->input = (unsigned char *)buf;
  1235. /*
  1236. * we now 'read' from wr->input, wr->length bytes into wr->data
  1237. */
  1238. /* first we compress */
  1239. if (s->compress != NULL) {
  1240. if (!ssl3_do_compress(s)) {
  1241. SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
  1242. goto err;
  1243. }
  1244. } else {
  1245. memcpy(wr->data, wr->input, wr->length);
  1246. wr->input = wr->data;
  1247. }
  1248. /*
  1249. * we should still have the output to wr->data and the input from
  1250. * wr->input. Length should be wr->length. wr->data still points in the
  1251. * wb->buf
  1252. */
  1253. if (mac_size != 0) {
  1254. s->method->ssl3_enc->mac(s, &(p[wr->length + bs]), 1);
  1255. wr->length += mac_size;
  1256. }
  1257. /* this is true regardless of mac size */
  1258. wr->input = p;
  1259. wr->data = p;
  1260. /* ssl3_enc can only have an error on read */
  1261. if (bs) { /* bs != 0 in case of CBC */
  1262. RAND_pseudo_bytes(p, bs);
  1263. /*
  1264. * master IV and last CBC residue stand for the rest of randomness
  1265. */
  1266. wr->length += bs;
  1267. }
  1268. s->method->ssl3_enc->enc(s, 1);
  1269. /* record length after mac and block padding */
  1270. /*
  1271. * if (type == SSL3_RT_APPLICATION_DATA || (type == SSL3_RT_ALERT && !
  1272. * SSL_in_init(s)))
  1273. */
  1274. /* there's only one epoch between handshake and app data */
  1275. s2n(s->d1->w_epoch, pseq);
  1276. /* XDTLS: ?? */
  1277. /*
  1278. * else s2n(s->d1->handshake_epoch, pseq);
  1279. */
  1280. memcpy(pseq, &(s->s3->write_sequence[2]), 6);
  1281. pseq += 6;
  1282. s2n(wr->length, pseq);
  1283. /*
  1284. * we should now have wr->data pointing to the encrypted data, which is
  1285. * wr->length long
  1286. */
  1287. wr->type = type; /* not needed but helps for debugging */
  1288. wr->length += DTLS1_RT_HEADER_LENGTH;
  1289. #if 0 /* this is now done at the message layer */
  1290. /* buffer the record, making it easy to handle retransmits */
  1291. if (type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC)
  1292. dtls1_buffer_record(s, wr->data, wr->length,
  1293. *((PQ_64BIT *) & (s->s3->write_sequence[0])));
  1294. #endif
  1295. ssl3_record_sequence_update(&(s->s3->write_sequence[0]));
  1296. if (create_empty_fragment) {
  1297. /*
  1298. * we are in a recursive call; just return the length, don't write
  1299. * out anything here
  1300. */
  1301. return wr->length;
  1302. }
  1303. /* now let's set up wb */
  1304. wb->left = prefix_len + wr->length;
  1305. wb->offset = 0;
  1306. /*
  1307. * memorize arguments so that ssl3_write_pending can detect bad write
  1308. * retries later
  1309. */
  1310. s->s3->wpend_tot = len;
  1311. s->s3->wpend_buf = buf;
  1312. s->s3->wpend_type = type;
  1313. s->s3->wpend_ret = len;
  1314. /* we now just need to write the buffer */
  1315. return ssl3_write_pending(s, type, buf, len);
  1316. err:
  1317. return -1;
  1318. }
  1319. static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap,
  1320. PQ_64BIT * seq_num)
  1321. {
  1322. #if PQ_64BIT_IS_INTEGER
  1323. PQ_64BIT mask = 0x0000000000000001L;
  1324. #endif
  1325. PQ_64BIT rcd_num, tmp;
  1326. pq_64bit_init(&rcd_num);
  1327. pq_64bit_init(&tmp);
  1328. /* this is the sequence number for the record just read */
  1329. pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8);
  1330. if (pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) ||
  1331. pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num))) {
  1332. pq_64bit_assign(seq_num, &rcd_num);
  1333. pq_64bit_free(&rcd_num);
  1334. pq_64bit_free(&tmp);
  1335. return 1; /* this record is new */
  1336. }
  1337. pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num);
  1338. if (pq_64bit_get_word(&tmp) > bitmap->length) {
  1339. pq_64bit_free(&rcd_num);
  1340. pq_64bit_free(&tmp);
  1341. return 0; /* stale, outside the window */
  1342. }
  1343. #if PQ_64BIT_IS_BIGNUM
  1344. {
  1345. int offset;
  1346. pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num);
  1347. pq_64bit_sub_word(&tmp, 1);
  1348. offset = pq_64bit_get_word(&tmp);
  1349. if (pq_64bit_is_bit_set(&(bitmap->map), offset)) {
  1350. pq_64bit_free(&rcd_num);
  1351. pq_64bit_free(&tmp);
  1352. return 0;
  1353. }
  1354. }
  1355. #else
  1356. mask <<= (bitmap->max_seq_num - rcd_num - 1);
  1357. if (bitmap->map & mask)
  1358. return 0; /* record previously received */
  1359. #endif
  1360. pq_64bit_assign(seq_num, &rcd_num);
  1361. pq_64bit_free(&rcd_num);
  1362. pq_64bit_free(&tmp);
  1363. return 1;
  1364. }
  1365. static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap)
  1366. {
  1367. unsigned int shift;
  1368. PQ_64BIT rcd_num;
  1369. PQ_64BIT tmp;
  1370. PQ_64BIT_CTX *ctx;
  1371. pq_64bit_init(&rcd_num);
  1372. pq_64bit_init(&tmp);
  1373. pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8);
  1374. /*
  1375. * unfortunate code complexity due to 64-bit manipulation support on
  1376. * 32-bit machines
  1377. */
  1378. if (pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) ||
  1379. pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num))) {
  1380. pq_64bit_sub(&tmp, &rcd_num, &(bitmap->max_seq_num));
  1381. pq_64bit_add_word(&tmp, 1);
  1382. shift = (unsigned int)pq_64bit_get_word(&tmp);
  1383. pq_64bit_lshift(&(tmp), &(bitmap->map), shift);
  1384. pq_64bit_assign(&(bitmap->map), &tmp);
  1385. pq_64bit_set_bit(&(bitmap->map), 0);
  1386. pq_64bit_add_word(&rcd_num, 1);
  1387. pq_64bit_assign(&(bitmap->max_seq_num), &rcd_num);
  1388. pq_64bit_assign_word(&tmp, 1);
  1389. pq_64bit_lshift(&tmp, &tmp, bitmap->length);
  1390. ctx = pq_64bit_ctx_new(&ctx);
  1391. pq_64bit_mod(&(bitmap->map), &(bitmap->map), &tmp, ctx);
  1392. pq_64bit_ctx_free(ctx);
  1393. } else {
  1394. pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num);
  1395. pq_64bit_sub_word(&tmp, 1);
  1396. shift = (unsigned int)pq_64bit_get_word(&tmp);
  1397. pq_64bit_set_bit(&(bitmap->map), shift);
  1398. }
  1399. pq_64bit_free(&rcd_num);
  1400. pq_64bit_free(&tmp);
  1401. }
  1402. int dtls1_dispatch_alert(SSL *s)
  1403. {
  1404. int i, j;
  1405. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  1406. unsigned char buf[DTLS1_AL_HEADER_LENGTH];
  1407. unsigned char *ptr = &buf[0];
  1408. s->s3->alert_dispatch = 0;
  1409. memset(buf, 0x00, sizeof(buf));
  1410. *ptr++ = s->s3->send_alert[0];
  1411. *ptr++ = s->s3->send_alert[1];
  1412. #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
  1413. if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
  1414. s2n(s->d1->handshake_read_seq, ptr);
  1415. # if 0
  1416. if (s->d1->r_msg_hdr.frag_off == 0)
  1417. /*
  1418. * waiting for a new msg
  1419. */
  1420. else
  1421. s2n(s->d1->r_msg_hdr.seq, ptr); /* partial msg read */
  1422. # endif
  1423. # if 0
  1424. fprintf(stderr,
  1425. "s->d1->handshake_read_seq = %d, s->d1->r_msg_hdr.seq = %d\n",
  1426. s->d1->handshake_read_seq, s->d1->r_msg_hdr.seq);
  1427. # endif
  1428. l2n3(s->d1->r_msg_hdr.frag_off, ptr);
  1429. }
  1430. #endif
  1431. i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0);
  1432. if (i <= 0) {
  1433. s->s3->alert_dispatch = 1;
  1434. /* fprintf( stderr, "not done with alert\n" ); */
  1435. } else {
  1436. if (s->s3->send_alert[0] == SSL3_AL_FATAL
  1437. #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
  1438. || s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
  1439. #endif
  1440. )
  1441. (void)BIO_flush(s->wbio);
  1442. if (s->msg_callback)
  1443. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
  1444. 2, s, s->msg_callback_arg);
  1445. if (s->info_callback != NULL)
  1446. cb = s->info_callback;
  1447. else if (s->ctx->info_callback != NULL)
  1448. cb = s->ctx->info_callback;
  1449. if (cb != NULL) {
  1450. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  1451. cb(s, SSL_CB_WRITE_ALERT, j);
  1452. }
  1453. }
  1454. return (i);
  1455. }
  1456. static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
  1457. unsigned int *is_next_epoch)
  1458. {
  1459. *is_next_epoch = 0;
  1460. /* In current epoch, accept HM, CCS, DATA, & ALERT */
  1461. if (rr->epoch == s->d1->r_epoch)
  1462. return &s->d1->bitmap;
  1463. /* Only HM and ALERT messages can be from the next epoch */
  1464. else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) &&
  1465. (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
  1466. *is_next_epoch = 1;
  1467. return &s->d1->next_bitmap;
  1468. }
  1469. return NULL;
  1470. }
  1471. #if 0
  1472. static int
  1473. dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr,
  1474. unsigned short *priority, unsigned long *offset)
  1475. {
  1476. /* alerts are passed up immediately */
  1477. if (rr->type == SSL3_RT_APPLICATION_DATA || rr->type == SSL3_RT_ALERT)
  1478. return 0;
  1479. /*
  1480. * Only need to buffer if a handshake is underway. (this implies that
  1481. * Hello Request and Client Hello are passed up immediately)
  1482. */
  1483. if (SSL_in_init(s)) {
  1484. unsigned char *data = rr->data;
  1485. /* need to extract the HM/CCS sequence number here */
  1486. if (rr->type == SSL3_RT_HANDSHAKE ||
  1487. rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  1488. unsigned short seq_num;
  1489. struct hm_header_st msg_hdr;
  1490. struct ccs_header_st ccs_hdr;
  1491. if (rr->type == SSL3_RT_HANDSHAKE) {
  1492. dtls1_get_message_header(data, &msg_hdr);
  1493. seq_num = msg_hdr.seq;
  1494. *offset = msg_hdr.frag_off;
  1495. } else {
  1496. dtls1_get_ccs_header(data, &ccs_hdr);
  1497. seq_num = ccs_hdr.seq;
  1498. *offset = 0;
  1499. }
  1500. /*
  1501. * this is either a record we're waiting for, or a retransmit of
  1502. * something we happened to previously receive (higher layers
  1503. * will drop the repeat silently
  1504. */
  1505. if (seq_num < s->d1->handshake_read_seq)
  1506. return 0;
  1507. if (rr->type == SSL3_RT_HANDSHAKE &&
  1508. seq_num == s->d1->handshake_read_seq &&
  1509. msg_hdr.frag_off < s->d1->r_msg_hdr.frag_off)
  1510. return 0;
  1511. else if (seq_num == s->d1->handshake_read_seq &&
  1512. (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC ||
  1513. msg_hdr.frag_off == s->d1->r_msg_hdr.frag_off))
  1514. return 0;
  1515. else {
  1516. *priority = seq_num;
  1517. return 1;
  1518. }
  1519. } else /* unknown record type */
  1520. return 0;
  1521. }
  1522. return 0;
  1523. }
  1524. #endif
  1525. void dtls1_reset_seq_numbers(SSL *s, int rw)
  1526. {
  1527. unsigned char *seq;
  1528. unsigned int seq_bytes = sizeof(s->s3->read_sequence);
  1529. if (rw & SSL3_CC_READ) {
  1530. seq = s->s3->read_sequence;
  1531. s->d1->r_epoch++;
  1532. pq_64bit_assign(&(s->d1->bitmap.map), &(s->d1->next_bitmap.map));
  1533. s->d1->bitmap.length = s->d1->next_bitmap.length;
  1534. pq_64bit_assign(&(s->d1->bitmap.max_seq_num),
  1535. &(s->d1->next_bitmap.max_seq_num));
  1536. pq_64bit_free(&(s->d1->next_bitmap.map));
  1537. pq_64bit_free(&(s->d1->next_bitmap.max_seq_num));
  1538. memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP));
  1539. pq_64bit_init(&(s->d1->next_bitmap.map));
  1540. pq_64bit_init(&(s->d1->next_bitmap.max_seq_num));
  1541. } else {
  1542. seq = s->s3->write_sequence;
  1543. memcpy(s->d1->last_write_sequence, seq,
  1544. sizeof(s->s3->write_sequence));
  1545. s->d1->w_epoch++;
  1546. }
  1547. memset(seq, 0x00, seq_bytes);
  1548. }
  1549. #if PQ_64BIT_IS_INTEGER
  1550. static PQ_64BIT bytes_to_long_long(unsigned char *bytes, PQ_64BIT * num)
  1551. {
  1552. PQ_64BIT _num;
  1553. _num = (((PQ_64BIT) bytes[0]) << 56) |
  1554. (((PQ_64BIT) bytes[1]) << 48) |
  1555. (((PQ_64BIT) bytes[2]) << 40) |
  1556. (((PQ_64BIT) bytes[3]) << 32) |
  1557. (((PQ_64BIT) bytes[4]) << 24) |
  1558. (((PQ_64BIT) bytes[5]) << 16) |
  1559. (((PQ_64BIT) bytes[6]) << 8) | (((PQ_64BIT) bytes[7]));
  1560. *num = _num;
  1561. return _num;
  1562. }
  1563. #endif