rec_layer_d1.c 33 KB

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