d1_pkt.c 48 KB

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