rec_layer_d1.c 34 KB

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