rec_layer_d1.c 27 KB

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