d1_pkt.c 51 KB

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