rec_layer_d1.c 34 KB

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