rec_layer_d1.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include "../ssl_locl.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/buffer.h>
  14. #include "record_locl.h"
  15. #include "../packet_locl.h"
  16. #include "internal/cryptlib.h"
  17. int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
  18. {
  19. DTLS_RECORD_LAYER *d;
  20. if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) {
  21. SSLerr(SSL_F_DTLS_RECORD_LAYER_NEW, ERR_R_MALLOC_FAILURE);
  22. return 0;
  23. }
  24. rl->d = d;
  25. d->unprocessed_rcds.q = pqueue_new();
  26. d->processed_rcds.q = pqueue_new();
  27. d->buffered_app_data.q = pqueue_new();
  28. if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL
  29. || d->buffered_app_data.q == NULL) {
  30. pqueue_free(d->unprocessed_rcds.q);
  31. pqueue_free(d->processed_rcds.q);
  32. pqueue_free(d->buffered_app_data.q);
  33. OPENSSL_free(d);
  34. rl->d = NULL;
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
  40. {
  41. DTLS_RECORD_LAYER_clear(rl);
  42. pqueue_free(rl->d->unprocessed_rcds.q);
  43. pqueue_free(rl->d->processed_rcds.q);
  44. pqueue_free(rl->d->buffered_app_data.q);
  45. OPENSSL_free(rl->d);
  46. rl->d = NULL;
  47. }
  48. void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
  49. {
  50. DTLS_RECORD_LAYER *d;
  51. pitem *item = NULL;
  52. DTLS1_RECORD_DATA *rdata;
  53. pqueue *unprocessed_rcds;
  54. pqueue *processed_rcds;
  55. pqueue *buffered_app_data;
  56. d = rl->d;
  57. while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
  58. rdata = (DTLS1_RECORD_DATA *)item->data;
  59. OPENSSL_free(rdata->rbuf.buf);
  60. OPENSSL_free(item->data);
  61. pitem_free(item);
  62. }
  63. while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
  64. rdata = (DTLS1_RECORD_DATA *)item->data;
  65. OPENSSL_free(rdata->rbuf.buf);
  66. OPENSSL_free(item->data);
  67. pitem_free(item);
  68. }
  69. while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
  70. rdata = (DTLS1_RECORD_DATA *)item->data;
  71. OPENSSL_free(rdata->rbuf.buf);
  72. OPENSSL_free(item->data);
  73. pitem_free(item);
  74. }
  75. unprocessed_rcds = d->unprocessed_rcds.q;
  76. processed_rcds = d->processed_rcds.q;
  77. buffered_app_data = d->buffered_app_data.q;
  78. memset(d, 0, sizeof(*d));
  79. d->unprocessed_rcds.q = unprocessed_rcds;
  80. d->processed_rcds.q = processed_rcds;
  81. d->buffered_app_data.q = buffered_app_data;
  82. }
  83. void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
  84. {
  85. if (e == rl->d->w_epoch - 1) {
  86. memcpy(rl->d->curr_write_sequence,
  87. rl->write_sequence, sizeof(rl->write_sequence));
  88. memcpy(rl->write_sequence,
  89. rl->d->last_write_sequence, sizeof(rl->write_sequence));
  90. } else if (e == rl->d->w_epoch + 1) {
  91. memcpy(rl->d->last_write_sequence,
  92. rl->write_sequence, sizeof(unsigned char[8]));
  93. memcpy(rl->write_sequence,
  94. rl->d->curr_write_sequence, sizeof(rl->write_sequence));
  95. }
  96. rl->d->w_epoch = e;
  97. }
  98. void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
  99. {
  100. memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
  101. }
  102. /* copy buffered record into SSL structure */
  103. static int dtls1_copy_record(SSL *s, pitem *item)
  104. {
  105. DTLS1_RECORD_DATA *rdata;
  106. rdata = (DTLS1_RECORD_DATA *)item->data;
  107. SSL3_BUFFER_release(&s->rlayer.rbuf);
  108. s->rlayer.packet = rdata->packet;
  109. s->rlayer.packet_length = rdata->packet_length;
  110. memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
  111. memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
  112. /* Set proper sequence number for mac calculation */
  113. memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
  114. return 1;
  115. }
  116. int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
  117. {
  118. DTLS1_RECORD_DATA *rdata;
  119. pitem *item;
  120. /* Limit the size of the queue to prevent DOS attacks */
  121. if (pqueue_size(queue->q) >= 100)
  122. return 0;
  123. rdata = OPENSSL_malloc(sizeof(*rdata));
  124. item = pitem_new(priority, rdata);
  125. if (rdata == NULL || item == NULL) {
  126. OPENSSL_free(rdata);
  127. pitem_free(item);
  128. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
  129. ERR_R_INTERNAL_ERROR);
  130. return -1;
  131. }
  132. rdata->packet = s->rlayer.packet;
  133. rdata->packet_length = s->rlayer.packet_length;
  134. memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
  135. memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
  136. item->data = rdata;
  137. #ifndef OPENSSL_NO_SCTP
  138. /* Store bio_dgram_sctp_rcvinfo struct */
  139. if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
  140. (SSL_get_state(s) == TLS_ST_SR_FINISHED
  141. || SSL_get_state(s) == TLS_ST_CR_FINISHED)) {
  142. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
  143. sizeof(rdata->recordinfo), &rdata->recordinfo);
  144. }
  145. #endif
  146. s->rlayer.packet = NULL;
  147. s->rlayer.packet_length = 0;
  148. memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
  149. memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
  150. if (!ssl3_setup_buffers(s)) {
  151. /* SSLfatal() already called */
  152. OPENSSL_free(rdata->rbuf.buf);
  153. OPENSSL_free(rdata);
  154. pitem_free(item);
  155. return -1;
  156. }
  157. /* insert should not fail, since duplicates are dropped */
  158. if (pqueue_insert(queue->q, item) == NULL) {
  159. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
  160. ERR_R_INTERNAL_ERROR);
  161. OPENSSL_free(rdata->rbuf.buf);
  162. OPENSSL_free(rdata);
  163. pitem_free(item);
  164. return -1;
  165. }
  166. return 1;
  167. }
  168. int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
  169. {
  170. pitem *item;
  171. item = pqueue_pop(queue->q);
  172. if (item) {
  173. dtls1_copy_record(s, item);
  174. OPENSSL_free(item->data);
  175. pitem_free(item);
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. /*
  181. * retrieve a buffered record that belongs to the new epoch, i.e., not
  182. * processed yet
  183. */
  184. #define dtls1_get_unprocessed_record(s) \
  185. dtls1_retrieve_buffered_record((s), \
  186. &((s)->rlayer.d->unprocessed_rcds))
  187. int dtls1_process_buffered_records(SSL *s)
  188. {
  189. pitem *item;
  190. SSL3_BUFFER *rb;
  191. SSL3_RECORD *rr;
  192. DTLS1_BITMAP *bitmap;
  193. unsigned int is_next_epoch;
  194. int replayok = 1;
  195. item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
  196. if (item) {
  197. /* Check if epoch is current. */
  198. if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
  199. return 1; /* Nothing to do. */
  200. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  201. rb = RECORD_LAYER_get_rbuf(&s->rlayer);
  202. if (SSL3_BUFFER_get_left(rb) > 0) {
  203. /*
  204. * We've still got data from the current packet to read. There could
  205. * be a record from the new epoch in it - so don't overwrite it
  206. * with the unprocessed records yet (we'll do it when we've
  207. * finished reading the current packet).
  208. */
  209. return 1;
  210. }
  211. /* Process all the records. */
  212. while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
  213. dtls1_get_unprocessed_record(s);
  214. bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
  215. if (bitmap == NULL) {
  216. /*
  217. * Should not happen. This will only ever be NULL when the
  218. * current record is from a different epoch. But that cannot
  219. * be the case because we already checked the epoch above
  220. */
  221. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  222. SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
  223. ERR_R_INTERNAL_ERROR);
  224. return 0;
  225. }
  226. #ifndef OPENSSL_NO_SCTP
  227. /* Only do replay check if no SCTP bio */
  228. if (!BIO_dgram_is_sctp(SSL_get_rbio(s)))
  229. #endif
  230. {
  231. /*
  232. * Check whether this is a repeat, or aged record. We did this
  233. * check once already when we first received the record - but
  234. * we might have updated the window since then due to
  235. * records we subsequently processed.
  236. */
  237. replayok = dtls1_record_replay_check(s, bitmap);
  238. }
  239. if (!replayok || !dtls1_process_record(s, bitmap)) {
  240. if (ossl_statem_in_error(s)) {
  241. /* dtls1_process_record called SSLfatal() */
  242. return -1;
  243. }
  244. /* dump this record */
  245. rr->length = 0;
  246. RECORD_LAYER_reset_packet_length(&s->rlayer);
  247. continue;
  248. }
  249. if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
  250. SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0) {
  251. /* SSLfatal() already called */
  252. return 0;
  253. }
  254. }
  255. }
  256. /*
  257. * sync epoch numbers once all the unprocessed records have been
  258. * processed
  259. */
  260. s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;
  261. s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;
  262. return 1;
  263. }
  264. /*-
  265. * Return up to 'len' payload bytes received in 'type' records.
  266. * 'type' is one of the following:
  267. *
  268. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  269. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  270. * - 0 (during a shutdown, no data has to be returned)
  271. *
  272. * If we don't have stored data to work from, read a SSL/TLS record first
  273. * (possibly multiple records if we still don't have anything to return).
  274. *
  275. * This function must handle any surprises the peer may have for us, such as
  276. * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
  277. * messages are treated as if they were handshake messages *if* the |recd_type|
  278. * argument is non NULL.
  279. * Also if record payloads contain fragments too small to process, we store
  280. * them until there is enough for the respective protocol (the record protocol
  281. * may use arbitrary fragmentation and even interleaving):
  282. * Change cipher spec protocol
  283. * just 1 byte needed, no need for keeping anything stored
  284. * Alert protocol
  285. * 2 bytes needed (AlertLevel, AlertDescription)
  286. * Handshake protocol
  287. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  288. * to detect unexpected Client Hello and Hello Request messages
  289. * here, anything else is handled by higher layers
  290. * Application data protocol
  291. * none of our business
  292. */
  293. int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
  294. size_t len, int peek, size_t *readbytes)
  295. {
  296. int i, j, iret;
  297. size_t n;
  298. SSL3_RECORD *rr;
  299. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  300. if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
  301. /* Not initialized yet */
  302. if (!ssl3_setup_buffers(s)) {
  303. /* SSLfatal() already called */
  304. return -1;
  305. }
  306. }
  307. if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
  308. (type != SSL3_RT_HANDSHAKE)) ||
  309. (peek && (type != SSL3_RT_APPLICATION_DATA))) {
  310. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
  311. ERR_R_INTERNAL_ERROR);
  312. return -1;
  313. }
  314. if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {
  315. /* type == SSL3_RT_APPLICATION_DATA */
  316. i = s->handshake_func(s);
  317. /* SSLfatal() already called if appropriate */
  318. if (i < 0)
  319. return i;
  320. if (i == 0)
  321. return -1;
  322. }
  323. start:
  324. s->rwstate = SSL_NOTHING;
  325. /*-
  326. * s->s3->rrec.type - is the type of record
  327. * s->s3->rrec.data, - data
  328. * s->s3->rrec.off, - offset into 'data' for next read
  329. * s->s3->rrec.length, - number of bytes.
  330. */
  331. rr = s->rlayer.rrec;
  332. /*
  333. * We are not handshaking and have no data yet, so process data buffered
  334. * during the last handshake in advance, if any.
  335. */
  336. if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {
  337. pitem *item;
  338. item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
  339. if (item) {
  340. #ifndef OPENSSL_NO_SCTP
  341. /* Restore bio_dgram_sctp_rcvinfo struct */
  342. if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
  343. DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
  344. BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
  345. sizeof(rdata->recordinfo), &rdata->recordinfo);
  346. }
  347. #endif
  348. dtls1_copy_record(s, item);
  349. OPENSSL_free(item->data);
  350. pitem_free(item);
  351. }
  352. }
  353. /* Check for timeout */
  354. if (dtls1_handle_timeout(s) > 0) {
  355. goto start;
  356. } else if (ossl_statem_in_error(s)) {
  357. /* dtls1_handle_timeout() has failed with a fatal error */
  358. return -1;
  359. }
  360. /* get new packet if necessary */
  361. if ((SSL3_RECORD_get_length(rr) == 0)
  362. || (s->rlayer.rstate == SSL_ST_READ_BODY)) {
  363. RECORD_LAYER_set_numrpipes(&s->rlayer, 0);
  364. iret = dtls1_get_record(s);
  365. if (iret <= 0) {
  366. iret = dtls1_read_failed(s, iret);
  367. /*
  368. * Anything other than a timeout is an error. SSLfatal() already
  369. * called if appropriate.
  370. */
  371. if (iret <= 0)
  372. return iret;
  373. else
  374. goto start;
  375. }
  376. RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
  377. }
  378. /*
  379. * Reset the count of consecutive warning alerts if we've got a non-empty
  380. * record that isn't an alert.
  381. */
  382. if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
  383. && SSL3_RECORD_get_length(rr) != 0)
  384. s->rlayer.alert_count = 0;
  385. if (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE
  386. && SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC
  387. && !SSL_in_init(s)
  388. && (s->d1->next_timeout.tv_sec != 0
  389. || s->d1->next_timeout.tv_usec != 0)) {
  390. /*
  391. * The timer is still running but we've received something that isn't
  392. * handshake data - so the peer must have finished processing our
  393. * last handshake flight. Stop the timer.
  394. */
  395. dtls1_stop_timer(s);
  396. }
  397. /* we now have a packet which can be read and processed */
  398. if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
  399. * reset by ssl3_get_finished */
  400. && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
  401. /*
  402. * We now have application data between CCS and Finished. Most likely
  403. * the packets were reordered on their way, so buffer the application
  404. * data for later processing rather than dropping the connection.
  405. */
  406. if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
  407. SSL3_RECORD_get_seq_num(rr)) < 0) {
  408. /* SSLfatal() already called */
  409. return -1;
  410. }
  411. SSL3_RECORD_set_length(rr, 0);
  412. SSL3_RECORD_set_read(rr);
  413. goto start;
  414. }
  415. /*
  416. * If the other end has shut down, throw anything we read away (even in
  417. * 'peek' mode)
  418. */
  419. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  420. SSL3_RECORD_set_length(rr, 0);
  421. SSL3_RECORD_set_read(rr);
  422. s->rwstate = SSL_NOTHING;
  423. return 0;
  424. }
  425. if (type == SSL3_RECORD_get_type(rr)
  426. || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
  427. && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
  428. /*
  429. * SSL3_RT_APPLICATION_DATA or
  430. * SSL3_RT_HANDSHAKE or
  431. * SSL3_RT_CHANGE_CIPHER_SPEC
  432. */
  433. /*
  434. * make sure that we are not getting application data when we are
  435. * doing a handshake for the first time
  436. */
  437. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  438. (s->enc_read_ctx == NULL)) {
  439. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
  440. SSL_R_APP_DATA_IN_HANDSHAKE);
  441. return -1;
  442. }
  443. if (recvd_type != NULL)
  444. *recvd_type = SSL3_RECORD_get_type(rr);
  445. if (len == 0) {
  446. /*
  447. * Mark a zero length record as read. This ensures multiple calls to
  448. * SSL_read() with a zero length buffer will eventually cause
  449. * SSL_pending() to report data as being available.
  450. */
  451. if (SSL3_RECORD_get_length(rr) == 0)
  452. SSL3_RECORD_set_read(rr);
  453. return 0;
  454. }
  455. if (len > SSL3_RECORD_get_length(rr))
  456. n = SSL3_RECORD_get_length(rr);
  457. else
  458. n = len;
  459. memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
  460. if (peek) {
  461. if (SSL3_RECORD_get_length(rr) == 0)
  462. SSL3_RECORD_set_read(rr);
  463. } else {
  464. SSL3_RECORD_sub_length(rr, n);
  465. SSL3_RECORD_add_off(rr, n);
  466. if (SSL3_RECORD_get_length(rr) == 0) {
  467. s->rlayer.rstate = SSL_ST_READ_HEADER;
  468. SSL3_RECORD_set_off(rr, 0);
  469. SSL3_RECORD_set_read(rr);
  470. }
  471. }
  472. #ifndef OPENSSL_NO_SCTP
  473. /*
  474. * We might had to delay a close_notify alert because of reordered
  475. * app data. If there was an alert and there is no message to read
  476. * anymore, finally set shutdown.
  477. */
  478. if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
  479. s->d1->shutdown_received
  480. && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  481. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  482. return 0;
  483. }
  484. #endif
  485. *readbytes = n;
  486. return 1;
  487. }
  488. /*
  489. * If we get here, then type != rr->type; if we have a handshake message,
  490. * then it was unexpected (Hello Request or Client Hello).
  491. */
  492. if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
  493. unsigned int alert_level, alert_descr;
  494. unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)
  495. + SSL3_RECORD_get_off(rr);
  496. PACKET alert;
  497. if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))
  498. || !PACKET_get_1(&alert, &alert_level)
  499. || !PACKET_get_1(&alert, &alert_descr)
  500. || PACKET_remaining(&alert) != 0) {
  501. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
  502. SSL_R_INVALID_ALERT);
  503. return -1;
  504. }
  505. if (s->msg_callback)
  506. s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,
  507. s->msg_callback_arg);
  508. if (s->info_callback != NULL)
  509. cb = s->info_callback;
  510. else if (s->ctx->info_callback != NULL)
  511. cb = s->ctx->info_callback;
  512. if (cb != NULL) {
  513. j = (alert_level << 8) | alert_descr;
  514. cb(s, SSL_CB_READ_ALERT, j);
  515. }
  516. if (alert_level == SSL3_AL_WARNING) {
  517. s->s3->warn_alert = alert_descr;
  518. SSL3_RECORD_set_read(rr);
  519. s->rlayer.alert_count++;
  520. if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
  521. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
  522. SSL_R_TOO_MANY_WARN_ALERTS);
  523. return -1;
  524. }
  525. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  526. #ifndef OPENSSL_NO_SCTP
  527. /*
  528. * With SCTP and streams the socket may deliver app data
  529. * after a close_notify alert. We have to check this first so
  530. * that nothing gets discarded.
  531. */
  532. if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
  533. BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  534. s->d1->shutdown_received = 1;
  535. s->rwstate = SSL_READING;
  536. BIO_clear_retry_flags(SSL_get_rbio(s));
  537. BIO_set_retry_read(SSL_get_rbio(s));
  538. return -1;
  539. }
  540. #endif
  541. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  542. return 0;
  543. }
  544. } else if (alert_level == SSL3_AL_FATAL) {
  545. char tmp[16];
  546. s->rwstate = SSL_NOTHING;
  547. s->s3->fatal_alert = alert_descr;
  548. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS1_READ_BYTES,
  549. SSL_AD_REASON_OFFSET + alert_descr);
  550. BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
  551. ERR_add_error_data(2, "SSL alert number ", tmp);
  552. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  553. SSL3_RECORD_set_read(rr);
  554. SSL_CTX_remove_session(s->session_ctx, s->session);
  555. return 0;
  556. } else {
  557. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_READ_BYTES,
  558. SSL_R_UNKNOWN_ALERT_TYPE);
  559. return -1;
  560. }
  561. goto start;
  562. }
  563. if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
  564. * shutdown */
  565. s->rwstate = SSL_NOTHING;
  566. SSL3_RECORD_set_length(rr, 0);
  567. SSL3_RECORD_set_read(rr);
  568. return 0;
  569. }
  570. if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
  571. /*
  572. * We can't process a CCS now, because previous handshake messages
  573. * are still missing, so just drop it.
  574. */
  575. SSL3_RECORD_set_length(rr, 0);
  576. SSL3_RECORD_set_read(rr);
  577. goto start;
  578. }
  579. /*
  580. * Unexpected handshake message (Client Hello, or protocol violation)
  581. */
  582. if ((SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) &&
  583. !ossl_statem_get_in_handshake(s)) {
  584. struct hm_header_st msg_hdr;
  585. /*
  586. * This may just be a stale retransmit. Also sanity check that we have
  587. * at least enough record bytes for a message header
  588. */
  589. if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch
  590. || SSL3_RECORD_get_length(rr) < DTLS1_HM_HEADER_LENGTH) {
  591. SSL3_RECORD_set_length(rr, 0);
  592. SSL3_RECORD_set_read(rr);
  593. goto start;
  594. }
  595. dtls1_get_message_header(rr->data, &msg_hdr);
  596. /*
  597. * If we are server, we may have a repeated FINISHED of the client
  598. * here, then retransmit our CCS and FINISHED.
  599. */
  600. if (msg_hdr.type == SSL3_MT_FINISHED) {
  601. if (dtls1_check_timeout_num(s) < 0) {
  602. /* SSLfatal) already called */
  603. return -1;
  604. }
  605. if (dtls1_retransmit_buffered_messages(s) <= 0) {
  606. /* Fail if we encountered a fatal error */
  607. if (ossl_statem_in_error(s))
  608. return -1;
  609. }
  610. SSL3_RECORD_set_length(rr, 0);
  611. SSL3_RECORD_set_read(rr);
  612. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  613. if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
  614. /* no read-ahead left? */
  615. BIO *bio;
  616. s->rwstate = SSL_READING;
  617. bio = SSL_get_rbio(s);
  618. BIO_clear_retry_flags(bio);
  619. BIO_set_retry_read(bio);
  620. return -1;
  621. }
  622. }
  623. goto start;
  624. }
  625. /*
  626. * To get here we must be trying to read app data but found handshake
  627. * data. But if we're trying to read app data, and we're not in init
  628. * (which is tested for at the top of this function) then init must be
  629. * finished
  630. */
  631. if (!ossl_assert(SSL_is_init_finished(s))) {
  632. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
  633. ERR_R_INTERNAL_ERROR);
  634. return -1;
  635. }
  636. /* We found handshake data, so we're going back into init */
  637. ossl_statem_set_in_init(s, 1);
  638. i = s->handshake_func(s);
  639. /* SSLfatal() called if appropriate */
  640. if (i < 0)
  641. return i;
  642. if (i == 0)
  643. return -1;
  644. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  645. if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
  646. /* no read-ahead left? */
  647. BIO *bio;
  648. /*
  649. * In the case where we try to read application data, but we
  650. * trigger an SSL handshake, we return -1 with the retry
  651. * option set. Otherwise renegotiation may cause nasty
  652. * problems in the blocking world
  653. */
  654. s->rwstate = SSL_READING;
  655. bio = SSL_get_rbio(s);
  656. BIO_clear_retry_flags(bio);
  657. BIO_set_retry_read(bio);
  658. return -1;
  659. }
  660. }
  661. goto start;
  662. }
  663. switch (SSL3_RECORD_get_type(rr)) {
  664. default:
  665. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
  666. SSL_R_UNEXPECTED_RECORD);
  667. return -1;
  668. case SSL3_RT_CHANGE_CIPHER_SPEC:
  669. case SSL3_RT_ALERT:
  670. case SSL3_RT_HANDSHAKE:
  671. /*
  672. * we already handled all of these, with the possible exception of
  673. * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
  674. * that should not happen when type != rr->type
  675. */
  676. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
  677. ERR_R_INTERNAL_ERROR);
  678. return -1;
  679. case SSL3_RT_APPLICATION_DATA:
  680. /*
  681. * At this point, we were expecting handshake data, but have
  682. * application data. If the library was running inside ssl3_read()
  683. * (i.e. in_read_app_data is set) and it makes sense to read
  684. * application data at this point (session renegotiation not yet
  685. * started), we will indulge it.
  686. */
  687. if (s->s3->in_read_app_data &&
  688. (s->s3->total_renegotiations != 0) &&
  689. ossl_statem_app_data_allowed(s)) {
  690. s->s3->in_read_app_data = 2;
  691. return -1;
  692. } else {
  693. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
  694. SSL_R_UNEXPECTED_RECORD);
  695. return -1;
  696. }
  697. }
  698. /* not reached */
  699. }
  700. /*
  701. * Call this to write data in records of type 'type' It will return <= 0 if
  702. * not all data has been sent or non-blocking IO.
  703. */
  704. int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
  705. size_t *written)
  706. {
  707. int i;
  708. if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
  709. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_WRITE_BYTES,
  710. ERR_R_INTERNAL_ERROR);
  711. return -1;
  712. }
  713. s->rwstate = SSL_NOTHING;
  714. i = do_dtls1_write(s, type, buf, len, 0, written);
  715. return i;
  716. }
  717. int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
  718. size_t len, int create_empty_fragment, size_t *written)
  719. {
  720. unsigned char *p, *pseq;
  721. int i, mac_size, clear = 0;
  722. size_t prefix_len = 0;
  723. int eivlen;
  724. SSL3_RECORD wr;
  725. SSL3_BUFFER *wb;
  726. SSL_SESSION *sess;
  727. wb = &s->rlayer.wbuf[0];
  728. /*
  729. * first check if there is a SSL3_BUFFER still being written out. This
  730. * will happen with non blocking IO
  731. */
  732. if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
  733. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  734. ERR_R_INTERNAL_ERROR);
  735. return 0;
  736. }
  737. /* If we have an alert to send, lets send it */
  738. if (s->s3->alert_dispatch) {
  739. i = s->method->ssl_dispatch_alert(s);
  740. if (i <= 0)
  741. return i;
  742. /* if it went, fall through and send more stuff */
  743. }
  744. if (len == 0 && !create_empty_fragment)
  745. return 0;
  746. if (len > ssl_get_max_send_fragment(s)) {
  747. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  748. SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
  749. return 0;
  750. }
  751. sess = s->session;
  752. if ((sess == NULL) ||
  753. (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
  754. clear = 1;
  755. if (clear)
  756. mac_size = 0;
  757. else {
  758. mac_size = EVP_MD_CTX_size(s->write_hash);
  759. if (mac_size < 0) {
  760. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  761. SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
  762. return -1;
  763. }
  764. }
  765. p = SSL3_BUFFER_get_buf(wb) + prefix_len;
  766. /* write the header */
  767. *(p++) = type & 0xff;
  768. SSL3_RECORD_set_type(&wr, type);
  769. /*
  770. * Special case: for hello verify request, client version 1.0 and we
  771. * haven't decided which version to use yet send back using version 1.0
  772. * header: otherwise some clients will ignore it.
  773. */
  774. if (s->method->version == DTLS_ANY_VERSION &&
  775. s->max_proto_version != DTLS1_BAD_VER) {
  776. *(p++) = DTLS1_VERSION >> 8;
  777. *(p++) = DTLS1_VERSION & 0xff;
  778. } else {
  779. *(p++) = s->version >> 8;
  780. *(p++) = s->version & 0xff;
  781. }
  782. /* field where we are to write out packet epoch, seq num and len */
  783. pseq = p;
  784. p += 10;
  785. /* Explicit IV length, block ciphers appropriate version flag */
  786. if (s->enc_write_ctx) {
  787. int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
  788. if (mode == EVP_CIPH_CBC_MODE) {
  789. eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
  790. if (eivlen <= 1)
  791. eivlen = 0;
  792. }
  793. /* Need explicit part of IV for GCM mode */
  794. else if (mode == EVP_CIPH_GCM_MODE)
  795. eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  796. else if (mode == EVP_CIPH_CCM_MODE)
  797. eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
  798. else
  799. eivlen = 0;
  800. } else
  801. eivlen = 0;
  802. /* lets setup the record stuff. */
  803. SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
  804. SSL3_RECORD_set_length(&wr, len);
  805. SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
  806. /*
  807. * we now 'read' from wr.input, wr.length bytes into wr.data
  808. */
  809. /* first we compress */
  810. if (s->compress != NULL) {
  811. if (!ssl3_do_compress(s, &wr)) {
  812. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  813. SSL_R_COMPRESSION_FAILURE);
  814. return -1;
  815. }
  816. } else {
  817. memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
  818. SSL3_RECORD_get_length(&wr));
  819. SSL3_RECORD_reset_input(&wr);
  820. }
  821. /*
  822. * we should still have the output to wr.data and the input from
  823. * wr.input. Length should be wr.length. wr.data still points in the
  824. * wb->buf
  825. */
  826. if (!SSL_WRITE_ETM(s) && mac_size != 0) {
  827. if (!s->method->ssl3_enc->mac(s, &wr,
  828. &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
  829. 1)) {
  830. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  831. ERR_R_INTERNAL_ERROR);
  832. return -1;
  833. }
  834. SSL3_RECORD_add_length(&wr, mac_size);
  835. }
  836. /* this is true regardless of mac size */
  837. SSL3_RECORD_set_data(&wr, p);
  838. SSL3_RECORD_reset_input(&wr);
  839. if (eivlen)
  840. SSL3_RECORD_add_length(&wr, eivlen);
  841. if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1) {
  842. if (!ossl_statem_in_error(s)) {
  843. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  844. ERR_R_INTERNAL_ERROR);
  845. }
  846. return -1;
  847. }
  848. if (SSL_WRITE_ETM(s) && mac_size != 0) {
  849. if (!s->method->ssl3_enc->mac(s, &wr,
  850. &(p[SSL3_RECORD_get_length(&wr)]), 1)) {
  851. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
  852. ERR_R_INTERNAL_ERROR);
  853. return -1;
  854. }
  855. SSL3_RECORD_add_length(&wr, mac_size);
  856. }
  857. /* record length after mac and block padding */
  858. /* there's only one epoch between handshake and app data */
  859. s2n(s->rlayer.d->w_epoch, pseq);
  860. memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
  861. pseq += 6;
  862. s2n(SSL3_RECORD_get_length(&wr), pseq);
  863. if (s->msg_callback)
  864. s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
  865. DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
  866. /*
  867. * we should now have wr.data pointing to the encrypted data, which is
  868. * wr->length long
  869. */
  870. SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
  871. SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
  872. ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
  873. if (create_empty_fragment) {
  874. /*
  875. * we are in a recursive call; just return the length, don't write
  876. * out anything here
  877. */
  878. *written = wr.length;
  879. return 1;
  880. }
  881. /* now let's set up wb */
  882. SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
  883. SSL3_BUFFER_set_offset(wb, 0);
  884. /*
  885. * memorize arguments so that ssl3_write_pending can detect bad write
  886. * retries later
  887. */
  888. s->rlayer.wpend_tot = len;
  889. s->rlayer.wpend_buf = buf;
  890. s->rlayer.wpend_type = type;
  891. s->rlayer.wpend_ret = len;
  892. /* we now just need to write the buffer. Calls SSLfatal() as required. */
  893. return ssl3_write_pending(s, type, buf, len, written);
  894. }
  895. DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
  896. unsigned int *is_next_epoch)
  897. {
  898. *is_next_epoch = 0;
  899. /* In current epoch, accept HM, CCS, DATA, & ALERT */
  900. if (rr->epoch == s->rlayer.d->r_epoch)
  901. return &s->rlayer.d->bitmap;
  902. /*
  903. * Only HM and ALERT messages can be from the next epoch and only if we
  904. * have already processed all of the unprocessed records from the last
  905. * epoch
  906. */
  907. else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
  908. s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch &&
  909. (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
  910. *is_next_epoch = 1;
  911. return &s->rlayer.d->next_bitmap;
  912. }
  913. return NULL;
  914. }
  915. void dtls1_reset_seq_numbers(SSL *s, int rw)
  916. {
  917. unsigned char *seq;
  918. unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
  919. if (rw & SSL3_CC_READ) {
  920. seq = s->rlayer.read_sequence;
  921. s->rlayer.d->r_epoch++;
  922. memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
  923. sizeof(s->rlayer.d->bitmap));
  924. memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap));
  925. /*
  926. * We must not use any buffered messages received from the previous
  927. * epoch
  928. */
  929. dtls1_clear_received_buffer(s);
  930. } else {
  931. seq = s->rlayer.write_sequence;
  932. memcpy(s->rlayer.d->last_write_sequence, seq,
  933. sizeof(s->rlayer.write_sequence));
  934. s->rlayer.d->w_epoch++;
  935. }
  936. memset(seq, 0, seq_bytes);
  937. }