rec_layer_d1.c 33 KB

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