rec_layer_d1.c 34 KB

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