rec_layer_d1.c 22 KB

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