rec_layer_d1.c 33 KB

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