rec_layer_d1.c 31 KB

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