rec_layer_d1.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright 2005-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_local.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/buffer.h>
  14. #include "record_local.h"
  15. #include "internal/packet.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->buffered_app_data = pqueue_new();
  24. if (d->buffered_app_data == NULL) {
  25. OPENSSL_free(d);
  26. rl->d = NULL;
  27. return 0;
  28. }
  29. return 1;
  30. }
  31. void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
  32. {
  33. if (rl->d == NULL)
  34. return;
  35. DTLS_RECORD_LAYER_clear(rl);
  36. pqueue_free(rl->d->buffered_app_data);
  37. OPENSSL_free(rl->d);
  38. rl->d = NULL;
  39. }
  40. void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
  41. {
  42. DTLS_RECORD_LAYER *d;
  43. pitem *item = NULL;
  44. TLS_RECORD *rec;
  45. pqueue *buffered_app_data;
  46. d = rl->d;
  47. while ((item = pqueue_pop(d->buffered_app_data)) != NULL) {
  48. rec = (TLS_RECORD *)item->data;
  49. if (rl->s->options & SSL_OP_CLEANSE_PLAINTEXT)
  50. OPENSSL_cleanse(rec->allocdata, rec->length);
  51. OPENSSL_free(rec->allocdata);
  52. OPENSSL_free(item->data);
  53. pitem_free(item);
  54. }
  55. buffered_app_data = d->buffered_app_data;
  56. memset(d, 0, sizeof(*d));
  57. d->buffered_app_data = buffered_app_data;
  58. }
  59. static int dtls_buffer_record(SSL_CONNECTION *s, TLS_RECORD *rec)
  60. {
  61. TLS_RECORD *rdata;
  62. pitem *item;
  63. struct pqueue_st *queue = s->rlayer.d->buffered_app_data;
  64. /* Limit the size of the queue to prevent DOS attacks */
  65. if (pqueue_size(queue) >= 100)
  66. return 0;
  67. /* We don't buffer partially read records */
  68. if (!ossl_assert(rec->off == 0))
  69. return -1;
  70. rdata = OPENSSL_malloc(sizeof(*rdata));
  71. item = pitem_new(rec->seq_num, rdata);
  72. if (rdata == NULL || item == NULL) {
  73. OPENSSL_free(rdata);
  74. pitem_free(item);
  75. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  76. return -1;
  77. }
  78. *rdata = *rec;
  79. /*
  80. * We will release the record from the record layer soon, so we take a copy
  81. * now. Copying data isn't good - but this should be infrequent so we
  82. * accept it here.
  83. */
  84. rdata->data = rdata->allocdata = OPENSSL_memdup(rec->data, rec->length);
  85. if (rdata->data == NULL) {
  86. OPENSSL_free(rdata);
  87. pitem_free(item);
  88. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  89. return -1;
  90. }
  91. /*
  92. * We use a NULL rechandle to indicate that the data field has been
  93. * allocated by us.
  94. */
  95. rdata->rechandle = NULL;
  96. item->data = rdata;
  97. #ifndef OPENSSL_NO_SCTP
  98. /* Store bio_dgram_sctp_rcvinfo struct */
  99. if (BIO_dgram_is_sctp(s->rbio) &&
  100. (ossl_statem_get_state(s) == TLS_ST_SR_FINISHED
  101. || ossl_statem_get_state(s) == TLS_ST_CR_FINISHED)) {
  102. BIO_ctrl(s->rbio, BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
  103. sizeof(rdata->recordinfo), &rdata->recordinfo);
  104. }
  105. #endif
  106. if (pqueue_insert(queue, item) == NULL) {
  107. /* Must be a duplicate so ignore it */
  108. OPENSSL_free(rdata->allocdata);
  109. OPENSSL_free(rdata);
  110. pitem_free(item);
  111. }
  112. return 1;
  113. }
  114. /* Unbuffer a previously buffered TLS_RECORD structure if any */
  115. static void dtls_unbuffer_record(SSL_CONNECTION *s)
  116. {
  117. TLS_RECORD *rdata;
  118. pitem *item;
  119. /* If we already have records to handle then do nothing */
  120. if (s->rlayer.curr_rec < s->rlayer.num_recs)
  121. return;
  122. item = pqueue_pop(s->rlayer.d->buffered_app_data);
  123. if (item != NULL) {
  124. rdata = (TLS_RECORD *)item->data;
  125. s->rlayer.tlsrecs[0] = *rdata;
  126. s->rlayer.num_recs = 1;
  127. s->rlayer.curr_rec = 0;
  128. #ifndef OPENSSL_NO_SCTP
  129. /* Restore bio_dgram_sctp_rcvinfo struct */
  130. if (BIO_dgram_is_sctp(s->rbio)) {
  131. BIO_ctrl(s->rbio, BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
  132. sizeof(rdata->recordinfo), &rdata->recordinfo);
  133. }
  134. #endif
  135. OPENSSL_free(item->data);
  136. pitem_free(item);
  137. }
  138. }
  139. /*-
  140. * Return up to 'len' payload bytes received in 'type' records.
  141. * 'type' is one of the following:
  142. *
  143. * - SSL3_RT_HANDSHAKE
  144. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  145. * - 0 (during a shutdown, no data has to be returned)
  146. *
  147. * If we don't have stored data to work from, read a SSL/TLS record first
  148. * (possibly multiple records if we still don't have anything to return).
  149. *
  150. * This function must handle any surprises the peer may have for us, such as
  151. * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
  152. * messages are treated as if they were handshake messages *if* the |recd_type|
  153. * argument is non NULL.
  154. * Also if record payloads contain fragments too small to process, we store
  155. * them until there is enough for the respective protocol (the record protocol
  156. * may use arbitrary fragmentation and even interleaving):
  157. * Change cipher spec protocol
  158. * just 1 byte needed, no need for keeping anything stored
  159. * Alert protocol
  160. * 2 bytes needed (AlertLevel, AlertDescription)
  161. * Handshake protocol
  162. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  163. * to detect unexpected Client Hello and Hello Request messages
  164. * here, anything else is handled by higher layers
  165. * Application data protocol
  166. * none of our business
  167. */
  168. int dtls1_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,
  169. unsigned char *buf, size_t len,
  170. int peek, size_t *readbytes)
  171. {
  172. int i, j, ret;
  173. size_t n;
  174. TLS_RECORD *rr;
  175. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  176. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  177. if (sc == NULL)
  178. return -1;
  179. if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
  180. (type != SSL3_RT_HANDSHAKE)) ||
  181. (peek && (type != SSL3_RT_APPLICATION_DATA))) {
  182. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  183. return -1;
  184. }
  185. if (!ossl_statem_get_in_handshake(sc) && SSL_in_init(s)) {
  186. /* type == SSL3_RT_APPLICATION_DATA */
  187. i = sc->handshake_func(s);
  188. /* SSLfatal() already called if appropriate */
  189. if (i < 0)
  190. return i;
  191. if (i == 0)
  192. return -1;
  193. }
  194. start:
  195. sc->rwstate = SSL_NOTHING;
  196. /*
  197. * We are not handshaking and have no data yet, so process data buffered
  198. * during the last handshake in advance, if any.
  199. */
  200. if (SSL_is_init_finished(s))
  201. dtls_unbuffer_record(sc);
  202. /* Check for timeout */
  203. if (dtls1_handle_timeout(sc) > 0) {
  204. goto start;
  205. } else if (ossl_statem_in_error(sc)) {
  206. /* dtls1_handle_timeout() has failed with a fatal error */
  207. return -1;
  208. }
  209. /* get new packet if necessary */
  210. if (sc->rlayer.curr_rec >= sc->rlayer.num_recs) {
  211. sc->rlayer.curr_rec = sc->rlayer.num_recs = 0;
  212. do {
  213. rr = &sc->rlayer.tlsrecs[sc->rlayer.num_recs];
  214. ret = HANDLE_RLAYER_READ_RETURN(sc,
  215. sc->rlayer.rrlmethod->read_record(sc->rlayer.rrl,
  216. &rr->rechandle,
  217. &rr->version, &rr->type,
  218. &rr->data, &rr->length,
  219. &rr->epoch, rr->seq_num));
  220. if (ret <= 0) {
  221. ret = dtls1_read_failed(sc, ret);
  222. /*
  223. * Anything other than a timeout is an error. SSLfatal() already
  224. * called if appropriate.
  225. */
  226. if (ret <= 0)
  227. return ret;
  228. else
  229. goto start;
  230. }
  231. rr->off = 0;
  232. sc->rlayer.num_recs++;
  233. } while (sc->rlayer.rrlmethod->processed_read_pending(sc->rlayer.rrl)
  234. && sc->rlayer.num_recs < SSL_MAX_PIPELINES);
  235. }
  236. rr = &sc->rlayer.tlsrecs[sc->rlayer.curr_rec];
  237. /*
  238. * Reset the count of consecutive warning alerts if we've got a non-empty
  239. * record that isn't an alert.
  240. */
  241. if (rr->type != SSL3_RT_ALERT && rr->length != 0)
  242. sc->rlayer.alert_count = 0;
  243. /* we now have a packet which can be read and processed */
  244. if (sc->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
  245. * reset by ssl3_get_finished */
  246. && (rr->type != SSL3_RT_HANDSHAKE)) {
  247. /*
  248. * We now have application data between CCS and Finished. Most likely
  249. * the packets were reordered on their way, so buffer the application
  250. * data for later processing rather than dropping the connection.
  251. */
  252. if (dtls_buffer_record(sc, rr) < 0) {
  253. /* SSLfatal() already called */
  254. return -1;
  255. }
  256. if (!ssl_release_record(sc, rr, 0))
  257. return -1;
  258. goto start;
  259. }
  260. /*
  261. * If the other end has shut down, throw anything we read away (even in
  262. * 'peek' mode)
  263. */
  264. if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
  265. if (!ssl_release_record(sc, rr, 0))
  266. return -1;
  267. sc->rwstate = SSL_NOTHING;
  268. return 0;
  269. }
  270. if (type == rr->type
  271. || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
  272. && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
  273. /*
  274. * SSL3_RT_APPLICATION_DATA or
  275. * SSL3_RT_HANDSHAKE or
  276. * SSL3_RT_CHANGE_CIPHER_SPEC
  277. */
  278. /*
  279. * make sure that we are not getting application data when we are
  280. * doing a handshake for the first time
  281. */
  282. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA)
  283. && (SSL_IS_FIRST_HANDSHAKE(sc))) {
  284. SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE,
  285. SSL_R_APP_DATA_IN_HANDSHAKE);
  286. return -1;
  287. }
  288. if (recvd_type != NULL)
  289. *recvd_type = rr->type;
  290. if (len == 0) {
  291. /*
  292. * Release a zero length record. This ensures multiple calls to
  293. * SSL_read() with a zero length buffer will eventually cause
  294. * SSL_pending() to report data as being available.
  295. */
  296. if (rr->length == 0 && !ssl_release_record(sc, rr, 0))
  297. return -1;
  298. return 0;
  299. }
  300. if (len > rr->length)
  301. n = rr->length;
  302. else
  303. n = len;
  304. memcpy(buf, &(rr->data[rr->off]), n);
  305. if (peek) {
  306. if (rr->length == 0 && !ssl_release_record(sc, rr, 0))
  307. return -1;
  308. } else {
  309. if (!ssl_release_record(sc, rr, n))
  310. return -1;
  311. }
  312. #ifndef OPENSSL_NO_SCTP
  313. /*
  314. * We might had to delay a close_notify alert because of reordered
  315. * app data. If there was an alert and there is no message to read
  316. * anymore, finally set shutdown.
  317. */
  318. if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
  319. sc->d1->shutdown_received
  320. && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) <= 0) {
  321. sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
  322. return 0;
  323. }
  324. #endif
  325. *readbytes = n;
  326. return 1;
  327. }
  328. /*
  329. * If we get here, then type != rr->type; if we have a handshake message,
  330. * then it was unexpected (Hello Request or Client Hello).
  331. */
  332. if (rr->type == SSL3_RT_ALERT) {
  333. unsigned int alert_level, alert_descr;
  334. const unsigned char *alert_bytes = rr->data + rr->off;
  335. PACKET alert;
  336. if (!PACKET_buf_init(&alert, alert_bytes, rr->length)
  337. || !PACKET_get_1(&alert, &alert_level)
  338. || !PACKET_get_1(&alert, &alert_descr)
  339. || PACKET_remaining(&alert) != 0) {
  340. SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
  341. return -1;
  342. }
  343. if (sc->msg_callback)
  344. sc->msg_callback(0, sc->version, SSL3_RT_ALERT, alert_bytes, 2, s,
  345. sc->msg_callback_arg);
  346. if (sc->info_callback != NULL)
  347. cb = sc->info_callback;
  348. else if (s->ctx->info_callback != NULL)
  349. cb = s->ctx->info_callback;
  350. if (cb != NULL) {
  351. j = (alert_level << 8) | alert_descr;
  352. cb(s, SSL_CB_READ_ALERT, j);
  353. }
  354. if (alert_level == SSL3_AL_WARNING) {
  355. sc->s3.warn_alert = alert_descr;
  356. if (!ssl_release_record(sc, rr, 0))
  357. return -1;
  358. sc->rlayer.alert_count++;
  359. if (sc->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
  360. SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE,
  361. SSL_R_TOO_MANY_WARN_ALERTS);
  362. return -1;
  363. }
  364. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  365. #ifndef OPENSSL_NO_SCTP
  366. /*
  367. * With SCTP and streams the socket may deliver app data
  368. * after a close_notify alert. We have to check this first so
  369. * that nothing gets discarded.
  370. */
  371. if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
  372. BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) > 0) {
  373. sc->d1->shutdown_received = 1;
  374. sc->rwstate = SSL_READING;
  375. BIO_clear_retry_flags(SSL_get_rbio(s));
  376. BIO_set_retry_read(SSL_get_rbio(s));
  377. return -1;
  378. }
  379. #endif
  380. sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
  381. return 0;
  382. } else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  383. /*
  384. * This is a warning but we receive it if we requested
  385. * renegotiation and the peer denied it. Terminate with a fatal
  386. * alert because if the application tried to renegotiate it
  387. * presumably had a good reason and expects it to succeed. In
  388. * the future we might have a renegotiation where we don't care
  389. * if the peer refused it where we carry on.
  390. */
  391. SSLfatal(sc, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION);
  392. return -1;
  393. }
  394. } else if (alert_level == SSL3_AL_FATAL) {
  395. sc->rwstate = SSL_NOTHING;
  396. sc->s3.fatal_alert = alert_descr;
  397. SSLfatal_data(sc, SSL_AD_NO_ALERT,
  398. SSL_AD_REASON_OFFSET + alert_descr,
  399. "SSL alert number %d", alert_descr);
  400. sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
  401. if (!ssl_release_record(sc, rr, 0))
  402. return -1;
  403. SSL_CTX_remove_session(sc->session_ctx, sc->session);
  404. return 0;
  405. } else {
  406. SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
  407. return -1;
  408. }
  409. goto start;
  410. }
  411. if (sc->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
  412. * shutdown */
  413. sc->rwstate = SSL_NOTHING;
  414. if (!ssl_release_record(sc, rr, 0))
  415. return -1;
  416. return 0;
  417. }
  418. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  419. /*
  420. * We can't process a CCS now, because previous handshake messages
  421. * are still missing, so just drop it.
  422. */
  423. if (!ssl_release_record(sc, rr, 0))
  424. return -1;
  425. goto start;
  426. }
  427. /*
  428. * Unexpected handshake message (Client Hello, or protocol violation)
  429. */
  430. if (rr->type == SSL3_RT_HANDSHAKE && !ossl_statem_get_in_handshake(sc)) {
  431. struct hm_header_st msg_hdr;
  432. /*
  433. * This may just be a stale retransmit. Also sanity check that we have
  434. * at least enough record bytes for a message header
  435. */
  436. if (rr->epoch != sc->rlayer.d->r_epoch
  437. || rr->length < DTLS1_HM_HEADER_LENGTH) {
  438. if (!ssl_release_record(sc, rr, 0))
  439. return -1;
  440. goto start;
  441. }
  442. dtls1_get_message_header(rr->data, &msg_hdr);
  443. /*
  444. * If we are server, we may have a repeated FINISHED of the client
  445. * here, then retransmit our CCS and FINISHED.
  446. */
  447. if (msg_hdr.type == SSL3_MT_FINISHED) {
  448. if (dtls1_check_timeout_num(sc) < 0) {
  449. /* SSLfatal) already called */
  450. return -1;
  451. }
  452. if (dtls1_retransmit_buffered_messages(sc) <= 0) {
  453. /* Fail if we encountered a fatal error */
  454. if (ossl_statem_in_error(sc))
  455. return -1;
  456. }
  457. if (!ssl_release_record(sc, rr, 0))
  458. return -1;
  459. if (!(sc->mode & SSL_MODE_AUTO_RETRY)) {
  460. if (!sc->rlayer.rrlmethod->unprocessed_read_pending(sc->rlayer.rrl)) {
  461. /* no read-ahead left? */
  462. BIO *bio;
  463. sc->rwstate = SSL_READING;
  464. bio = SSL_get_rbio(s);
  465. BIO_clear_retry_flags(bio);
  466. BIO_set_retry_read(bio);
  467. return -1;
  468. }
  469. }
  470. goto start;
  471. }
  472. /*
  473. * To get here we must be trying to read app data but found handshake
  474. * data. But if we're trying to read app data, and we're not in init
  475. * (which is tested for at the top of this function) then init must be
  476. * finished
  477. */
  478. if (!ossl_assert(SSL_is_init_finished(s))) {
  479. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  480. return -1;
  481. }
  482. /* We found handshake data, so we're going back into init */
  483. ossl_statem_set_in_init(sc, 1);
  484. i = sc->handshake_func(s);
  485. /* SSLfatal() called if appropriate */
  486. if (i < 0)
  487. return i;
  488. if (i == 0)
  489. return -1;
  490. if (!(sc->mode & SSL_MODE_AUTO_RETRY)) {
  491. if (!sc->rlayer.rrlmethod->unprocessed_read_pending(sc->rlayer.rrl)) {
  492. /* no read-ahead left? */
  493. BIO *bio;
  494. /*
  495. * In the case where we try to read application data, but we
  496. * trigger an SSL handshake, we return -1 with the retry
  497. * option set. Otherwise renegotiation may cause nasty
  498. * problems in the blocking world
  499. */
  500. sc->rwstate = SSL_READING;
  501. bio = SSL_get_rbio(s);
  502. BIO_clear_retry_flags(bio);
  503. BIO_set_retry_read(bio);
  504. return -1;
  505. }
  506. }
  507. goto start;
  508. }
  509. switch (rr->type) {
  510. default:
  511. SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
  512. return -1;
  513. case SSL3_RT_CHANGE_CIPHER_SPEC:
  514. case SSL3_RT_ALERT:
  515. case SSL3_RT_HANDSHAKE:
  516. /*
  517. * we already handled all of these, with the possible exception of
  518. * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
  519. * that should not happen when type != rr->type
  520. */
  521. SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
  522. return -1;
  523. case SSL3_RT_APPLICATION_DATA:
  524. /*
  525. * At this point, we were expecting handshake data, but have
  526. * application data. If the library was running inside ssl3_read()
  527. * (i.e. in_read_app_data is set) and it makes sense to read
  528. * application data at this point (session renegotiation not yet
  529. * started), we will indulge it.
  530. */
  531. if (sc->s3.in_read_app_data &&
  532. (sc->s3.total_renegotiations != 0) &&
  533. ossl_statem_app_data_allowed(sc)) {
  534. sc->s3.in_read_app_data = 2;
  535. return -1;
  536. } else {
  537. SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
  538. return -1;
  539. }
  540. }
  541. /* not reached */
  542. }
  543. /*
  544. * Call this to write data in records of type 'type' It will return <= 0 if
  545. * not all data has been sent or non-blocking IO.
  546. */
  547. int dtls1_write_bytes(SSL_CONNECTION *s, uint8_t type, const void *buf,
  548. size_t len, size_t *written)
  549. {
  550. int i;
  551. if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
  552. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  553. return -1;
  554. }
  555. s->rwstate = SSL_NOTHING;
  556. i = do_dtls1_write(s, type, buf, len, written);
  557. return i;
  558. }
  559. int do_dtls1_write(SSL_CONNECTION *sc, uint8_t type, const unsigned char *buf,
  560. size_t len, size_t *written)
  561. {
  562. int i;
  563. OSSL_RECORD_TEMPLATE tmpl;
  564. SSL *s = SSL_CONNECTION_GET_SSL(sc);
  565. int ret;
  566. /* If we have an alert to send, lets send it */
  567. if (sc->s3.alert_dispatch > 0) {
  568. i = s->method->ssl_dispatch_alert(s);
  569. if (i <= 0)
  570. return i;
  571. /* if it went, fall through and send more stuff */
  572. }
  573. if (len == 0)
  574. return 0;
  575. if (len > ssl_get_max_send_fragment(sc)) {
  576. SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
  577. return 0;
  578. }
  579. tmpl.type = type;
  580. /*
  581. * Special case: for hello verify request, client version 1.0 and we
  582. * haven't decided which version to use yet send back using version 1.0
  583. * header: otherwise some clients will ignore it.
  584. */
  585. if (s->method->version == DTLS_ANY_VERSION
  586. && sc->max_proto_version != DTLS1_BAD_VER)
  587. tmpl.version = DTLS1_VERSION;
  588. else
  589. tmpl.version = sc->version;
  590. tmpl.buf = buf;
  591. tmpl.buflen = len;
  592. ret = HANDLE_RLAYER_WRITE_RETURN(sc,
  593. sc->rlayer.wrlmethod->write_records(sc->rlayer.wrl, &tmpl, 1));
  594. if (ret > 0)
  595. *written = (int)len;
  596. return ret;
  597. }
  598. void dtls1_increment_epoch(SSL_CONNECTION *s, int rw)
  599. {
  600. if (rw & SSL3_CC_READ) {
  601. s->rlayer.d->r_epoch++;
  602. /*
  603. * We must not use any buffered messages received from the previous
  604. * epoch
  605. */
  606. dtls1_clear_received_buffer(s);
  607. } else {
  608. s->rlayer.d->w_epoch++;
  609. }
  610. }
  611. uint16_t dtls1_get_epoch(SSL_CONNECTION *s, int rw) {
  612. uint16_t epoch;
  613. if (rw & SSL3_CC_READ)
  614. epoch = s->rlayer.d->r_epoch;
  615. else
  616. epoch = s->rlayer.d->w_epoch;
  617. return epoch;
  618. }