dtls_meth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright 2018-2024 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 <assert.h>
  10. #include "../../ssl_local.h"
  11. #include "../record_local.h"
  12. #include "recmethod_local.h"
  13. /* mod 128 saturating subtract of two 64-bit values in big-endian order */
  14. static int satsub64be(const unsigned char *v1, const unsigned char *v2)
  15. {
  16. int64_t ret;
  17. uint64_t l1, l2;
  18. n2l8(v1, l1);
  19. n2l8(v2, l2);
  20. ret = l1 - l2;
  21. /* We do not permit wrap-around */
  22. if (l1 > l2 && ret < 0)
  23. return 128;
  24. else if (l2 > l1 && ret > 0)
  25. return -128;
  26. if (ret > 128)
  27. return 128;
  28. else if (ret < -128)
  29. return -128;
  30. else
  31. return (int)ret;
  32. }
  33. static int dtls_record_replay_check(OSSL_RECORD_LAYER *rl, DTLS_BITMAP *bitmap)
  34. {
  35. int cmp;
  36. unsigned int shift;
  37. const unsigned char *seq = rl->sequence;
  38. cmp = satsub64be(seq, bitmap->max_seq_num);
  39. if (cmp > 0) {
  40. ossl_tls_rl_record_set_seq_num(&rl->rrec[0], seq);
  41. return 1; /* this record in new */
  42. }
  43. shift = -cmp;
  44. if (shift >= sizeof(bitmap->map) * 8)
  45. return 0; /* stale, outside the window */
  46. else if (bitmap->map & ((uint64_t)1 << shift))
  47. return 0; /* record previously received */
  48. ossl_tls_rl_record_set_seq_num(&rl->rrec[0], seq);
  49. return 1;
  50. }
  51. static void dtls_record_bitmap_update(OSSL_RECORD_LAYER *rl,
  52. DTLS_BITMAP *bitmap)
  53. {
  54. int cmp;
  55. unsigned int shift;
  56. const unsigned char *seq = rl->sequence;
  57. cmp = satsub64be(seq, bitmap->max_seq_num);
  58. if (cmp > 0) {
  59. shift = cmp;
  60. if (shift < sizeof(bitmap->map) * 8)
  61. bitmap->map <<= shift, bitmap->map |= 1UL;
  62. else
  63. bitmap->map = 1UL;
  64. memcpy(bitmap->max_seq_num, seq, SEQ_NUM_SIZE);
  65. } else {
  66. shift = -cmp;
  67. if (shift < sizeof(bitmap->map) * 8)
  68. bitmap->map |= (uint64_t)1 << shift;
  69. }
  70. }
  71. static DTLS_BITMAP *dtls_get_bitmap(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rr,
  72. unsigned int *is_next_epoch)
  73. {
  74. *is_next_epoch = 0;
  75. /* In current epoch, accept HM, CCS, DATA, & ALERT */
  76. if (rr->epoch == rl->epoch)
  77. return &rl->bitmap;
  78. /*
  79. * Check if the message is from the next epoch
  80. */
  81. else if (rr->epoch == rl->epoch + 1) {
  82. *is_next_epoch = 1;
  83. return &rl->next_bitmap;
  84. }
  85. return NULL;
  86. }
  87. static void dtls_set_in_init(OSSL_RECORD_LAYER *rl, int in_init)
  88. {
  89. rl->in_init = in_init;
  90. }
  91. static int dtls_process_record(OSSL_RECORD_LAYER *rl, DTLS_BITMAP *bitmap)
  92. {
  93. int i;
  94. int enc_err;
  95. TLS_RL_RECORD *rr;
  96. int imac_size;
  97. size_t mac_size = 0;
  98. unsigned char md[EVP_MAX_MD_SIZE];
  99. SSL_MAC_BUF macbuf = { NULL, 0 };
  100. int ret = 0;
  101. rr = &rl->rrec[0];
  102. /*
  103. * At this point, rl->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length,
  104. * and we have that many bytes in rl->packet
  105. */
  106. rr->input = &(rl->packet[DTLS1_RT_HEADER_LENGTH]);
  107. /*
  108. * ok, we can now read from 'rl->packet' data into 'rr'. rr->input
  109. * points at rr->length bytes, which need to be copied into rr->data by
  110. * either the decryption or by the decompression. When the data is 'copied'
  111. * into the rr->data buffer, rr->input will be pointed at the new buffer
  112. */
  113. /*
  114. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  115. * bytes of encrypted compressed stuff.
  116. */
  117. /* check is not needed I believe */
  118. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  119. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  120. return 0;
  121. }
  122. /* decrypt in place in 'rr->input' */
  123. rr->data = rr->input;
  124. rr->orig_len = rr->length;
  125. if (rl->md_ctx != NULL) {
  126. const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(rl->md_ctx);
  127. if (tmpmd != NULL) {
  128. imac_size = EVP_MD_get_size(tmpmd);
  129. if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
  130. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  131. return 0;
  132. }
  133. mac_size = (size_t)imac_size;
  134. }
  135. }
  136. if (rl->use_etm && rl->md_ctx != NULL) {
  137. unsigned char *mac;
  138. if (rr->orig_len < mac_size) {
  139. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  140. return 0;
  141. }
  142. rr->length -= mac_size;
  143. mac = rr->data + rr->length;
  144. i = rl->funcs->mac(rl, rr, md, 0 /* not send */);
  145. if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
  146. RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
  147. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  148. return 0;
  149. }
  150. /*
  151. * We've handled the mac now - there is no MAC inside the encrypted
  152. * record
  153. */
  154. mac_size = 0;
  155. }
  156. /*
  157. * Set a mark around the packet decryption attempt. This is DTLS, so
  158. * bad packets are just ignored, and we don't want to leave stray
  159. * errors in the queue from processing bogus junk that we ignored.
  160. */
  161. ERR_set_mark();
  162. enc_err = rl->funcs->cipher(rl, rr, 1, 0, &macbuf, mac_size);
  163. /*-
  164. * enc_err is:
  165. * 0: if the record is publicly invalid, or an internal error, or AEAD
  166. * decryption failed, or ETM decryption failed.
  167. * 1: Success or MTE decryption failed (MAC will be randomised)
  168. */
  169. if (enc_err == 0) {
  170. ERR_pop_to_mark();
  171. if (rl->alert != SSL_AD_NO_ALERT) {
  172. /* RLAYERfatal() already called */
  173. goto end;
  174. }
  175. /* For DTLS we simply ignore bad packets. */
  176. rr->length = 0;
  177. rl->packet_length = 0;
  178. goto end;
  179. }
  180. ERR_clear_last_mark();
  181. OSSL_TRACE_BEGIN(TLS) {
  182. BIO_printf(trc_out, "dec %zd\n", rr->length);
  183. BIO_dump_indent(trc_out, rr->data, rr->length, 4);
  184. } OSSL_TRACE_END(TLS);
  185. /* r->length is now the compressed data plus mac */
  186. if (!rl->use_etm
  187. && (rl->enc_ctx != NULL)
  188. && (EVP_MD_CTX_get0_md(rl->md_ctx) != NULL)) {
  189. /* rl->md_ctx != NULL => mac_size != -1 */
  190. i = rl->funcs->mac(rl, rr, md, 0 /* not send */);
  191. if (i == 0 || macbuf.mac == NULL
  192. || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
  193. enc_err = 0;
  194. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  195. enc_err = 0;
  196. }
  197. if (enc_err == 0) {
  198. /* decryption failed, silently discard message */
  199. rr->length = 0;
  200. rl->packet_length = 0;
  201. goto end;
  202. }
  203. /* r->length is now just compressed */
  204. if (rl->compctx != NULL) {
  205. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  206. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  207. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  208. goto end;
  209. }
  210. if (!tls_do_uncompress(rl, rr)) {
  211. RLAYERfatal(rl, SSL_AD_DECOMPRESSION_FAILURE, SSL_R_BAD_DECOMPRESSION);
  212. goto end;
  213. }
  214. }
  215. /*
  216. * Check if the received packet overflows the current Max Fragment
  217. * Length setting.
  218. */
  219. if (rr->length > rl->max_frag_len) {
  220. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  221. goto end;
  222. }
  223. rr->off = 0;
  224. /*-
  225. * So at this point the following is true
  226. * ssl->s3.rrec.type is the type of record
  227. * ssl->s3.rrec.length == number of bytes in record
  228. * ssl->s3.rrec.off == offset to first valid byte
  229. * ssl->s3.rrec.data == where to take bytes from, increment
  230. * after use :-).
  231. */
  232. /* we have pulled in a full packet so zero things */
  233. rl->packet_length = 0;
  234. /* Mark receipt of record. */
  235. dtls_record_bitmap_update(rl, bitmap);
  236. ret = 1;
  237. end:
  238. if (macbuf.alloced)
  239. OPENSSL_free(macbuf.mac);
  240. return ret;
  241. }
  242. static int dtls_rlayer_buffer_record(OSSL_RECORD_LAYER *rl, struct pqueue_st *queue,
  243. unsigned char *priority)
  244. {
  245. DTLS_RLAYER_RECORD_DATA *rdata;
  246. pitem *item;
  247. /* Limit the size of the queue to prevent DOS attacks */
  248. if (pqueue_size(queue) >= 100)
  249. return 0;
  250. rdata = OPENSSL_malloc(sizeof(*rdata));
  251. item = pitem_new(priority, rdata);
  252. if (rdata == NULL || item == NULL) {
  253. OPENSSL_free(rdata);
  254. pitem_free(item);
  255. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  256. return -1;
  257. }
  258. rdata->packet = rl->packet;
  259. rdata->packet_length = rl->packet_length;
  260. memcpy(&(rdata->rbuf), &rl->rbuf, sizeof(TLS_BUFFER));
  261. memcpy(&(rdata->rrec), &rl->rrec[0], sizeof(TLS_RL_RECORD));
  262. item->data = rdata;
  263. rl->packet = NULL;
  264. rl->packet_length = 0;
  265. memset(&rl->rbuf, 0, sizeof(TLS_BUFFER));
  266. memset(&rl->rrec[0], 0, sizeof(rl->rrec[0]));
  267. if (!tls_setup_read_buffer(rl)) {
  268. /* RLAYERfatal() already called */
  269. OPENSSL_free(rdata->rbuf.buf);
  270. OPENSSL_free(rdata);
  271. pitem_free(item);
  272. return -1;
  273. }
  274. if (pqueue_insert(queue, item) == NULL) {
  275. /* Must be a duplicate so ignore it */
  276. OPENSSL_free(rdata->rbuf.buf);
  277. OPENSSL_free(rdata);
  278. pitem_free(item);
  279. }
  280. return 1;
  281. }
  282. /* copy buffered record into OSSL_RECORD_LAYER structure */
  283. static int dtls_copy_rlayer_record(OSSL_RECORD_LAYER *rl, pitem *item)
  284. {
  285. DTLS_RLAYER_RECORD_DATA *rdata;
  286. rdata = (DTLS_RLAYER_RECORD_DATA *)item->data;
  287. ossl_tls_buffer_release(&rl->rbuf);
  288. rl->packet = rdata->packet;
  289. rl->packet_length = rdata->packet_length;
  290. memcpy(&rl->rbuf, &(rdata->rbuf), sizeof(TLS_BUFFER));
  291. memcpy(&rl->rrec[0], &(rdata->rrec), sizeof(TLS_RL_RECORD));
  292. /* Set proper sequence number for mac calculation */
  293. memcpy(&(rl->sequence[2]), &(rdata->packet[5]), 6);
  294. return 1;
  295. }
  296. static int dtls_retrieve_rlayer_buffered_record(OSSL_RECORD_LAYER *rl,
  297. struct pqueue_st *queue)
  298. {
  299. pitem *item;
  300. item = pqueue_pop(queue);
  301. if (item) {
  302. dtls_copy_rlayer_record(rl, item);
  303. OPENSSL_free(item->data);
  304. pitem_free(item);
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. /*-
  310. * Call this to get a new input record.
  311. * It will return <= 0 if more data is needed, normally due to an error
  312. * or non-blocking IO.
  313. * When it finishes, one packet has been decoded and can be found in
  314. * ssl->s3.rrec.type - is the type of record
  315. * ssl->s3.rrec.data - data
  316. * ssl->s3.rrec.length - number of bytes
  317. */
  318. int dtls_get_more_records(OSSL_RECORD_LAYER *rl)
  319. {
  320. int ssl_major, ssl_minor;
  321. int rret;
  322. size_t more, n;
  323. TLS_RL_RECORD *rr;
  324. unsigned char *p = NULL;
  325. DTLS_BITMAP *bitmap;
  326. unsigned int is_next_epoch;
  327. rl->num_recs = 0;
  328. rl->curr_rec = 0;
  329. rl->num_released = 0;
  330. rr = rl->rrec;
  331. if (rl->rbuf.buf == NULL) {
  332. if (!tls_setup_read_buffer(rl)) {
  333. /* RLAYERfatal() already called */
  334. return OSSL_RECORD_RETURN_FATAL;
  335. }
  336. }
  337. again:
  338. /* if we're renegotiating, then there may be buffered records */
  339. if (dtls_retrieve_rlayer_buffered_record(rl, rl->processed_rcds)) {
  340. rl->num_recs = 1;
  341. return OSSL_RECORD_RETURN_SUCCESS;
  342. }
  343. /* get something from the wire */
  344. /* check if we have the header */
  345. if ((rl->rstate != SSL_ST_READ_BODY) ||
  346. (rl->packet_length < DTLS1_RT_HEADER_LENGTH)) {
  347. rret = rl->funcs->read_n(rl, DTLS1_RT_HEADER_LENGTH,
  348. TLS_BUFFER_get_len(&rl->rbuf), 0, 1, &n);
  349. /* read timeout is handled by dtls1_read_bytes */
  350. if (rret < OSSL_RECORD_RETURN_SUCCESS) {
  351. /* RLAYERfatal() already called if appropriate */
  352. return rret; /* error or non-blocking */
  353. }
  354. /* this packet contained a partial record, dump it */
  355. if (rl->packet_length != DTLS1_RT_HEADER_LENGTH) {
  356. rl->packet_length = 0;
  357. goto again;
  358. }
  359. rl->rstate = SSL_ST_READ_BODY;
  360. p = rl->packet;
  361. /* Pull apart the header into the DTLS1_RECORD */
  362. rr->type = *(p++);
  363. ssl_major = *(p++);
  364. ssl_minor = *(p++);
  365. rr->rec_version = (ssl_major << 8) | ssl_minor;
  366. /* sequence number is 64 bits, with top 2 bytes = epoch */
  367. n2s(p, rr->epoch);
  368. memcpy(&(rl->sequence[2]), p, 6);
  369. p += 6;
  370. n2s(p, rr->length);
  371. if (rl->msg_callback != NULL)
  372. rl->msg_callback(0, rr->rec_version, SSL3_RT_HEADER, rl->packet, DTLS1_RT_HEADER_LENGTH,
  373. rl->cbarg);
  374. /*
  375. * Lets check the version. We tolerate alerts that don't have the exact
  376. * version number (e.g. because of protocol version errors)
  377. */
  378. if (!rl->is_first_record && rr->type != SSL3_RT_ALERT) {
  379. if (rr->rec_version != rl->version) {
  380. /* unexpected version, silently discard */
  381. rr->length = 0;
  382. rl->packet_length = 0;
  383. goto again;
  384. }
  385. }
  386. if (ssl_major !=
  387. (rl->version == DTLS_ANY_VERSION ? DTLS1_VERSION_MAJOR
  388. : rl->version >> 8)) {
  389. /* wrong version, silently discard record */
  390. rr->length = 0;
  391. rl->packet_length = 0;
  392. goto again;
  393. }
  394. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  395. /* record too long, silently discard it */
  396. rr->length = 0;
  397. rl->packet_length = 0;
  398. goto again;
  399. }
  400. /*
  401. * If received packet overflows maximum possible fragment length then
  402. * silently discard it
  403. */
  404. if (rr->length > rl->max_frag_len + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
  405. /* record too long, silently discard it */
  406. rr->length = 0;
  407. rl->packet_length = 0;
  408. goto again;
  409. }
  410. /* now rl->rstate == SSL_ST_READ_BODY */
  411. }
  412. /* rl->rstate == SSL_ST_READ_BODY, get and decode the data */
  413. if (rr->length > rl->packet_length - DTLS1_RT_HEADER_LENGTH) {
  414. /* now rl->packet_length == DTLS1_RT_HEADER_LENGTH */
  415. more = rr->length;
  416. rret = rl->funcs->read_n(rl, more, more, 1, 1, &n);
  417. /* this packet contained a partial record, dump it */
  418. if (rret < OSSL_RECORD_RETURN_SUCCESS || n != more) {
  419. if (rl->alert != SSL_AD_NO_ALERT) {
  420. /* read_n() called RLAYERfatal() */
  421. return OSSL_RECORD_RETURN_FATAL;
  422. }
  423. rr->length = 0;
  424. rl->packet_length = 0;
  425. goto again;
  426. }
  427. /*
  428. * now n == rr->length,
  429. * and rl->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length
  430. */
  431. }
  432. /* set state for later operations */
  433. rl->rstate = SSL_ST_READ_HEADER;
  434. /* match epochs. NULL means the packet is dropped on the floor */
  435. bitmap = dtls_get_bitmap(rl, rr, &is_next_epoch);
  436. if (bitmap == NULL) {
  437. rr->length = 0;
  438. rl->packet_length = 0; /* dump this record */
  439. goto again; /* get another record */
  440. }
  441. #ifndef OPENSSL_NO_SCTP
  442. /* Only do replay check if no SCTP bio */
  443. if (!BIO_dgram_is_sctp(rl->bio)) {
  444. #endif
  445. /* Check whether this is a repeat, or aged record. */
  446. if (!dtls_record_replay_check(rl, bitmap)) {
  447. rr->length = 0;
  448. rl->packet_length = 0; /* dump this record */
  449. goto again; /* get another record */
  450. }
  451. #ifndef OPENSSL_NO_SCTP
  452. }
  453. #endif
  454. /* just read a 0 length packet */
  455. if (rr->length == 0)
  456. goto again;
  457. /*
  458. * If this record is from the next epoch (either HM or ALERT), and a
  459. * handshake is currently in progress, buffer it since it cannot be
  460. * processed at this time.
  461. */
  462. if (is_next_epoch) {
  463. if (rl->in_init) {
  464. if (dtls_rlayer_buffer_record(rl, rl->unprocessed_rcds,
  465. rr->seq_num) < 0) {
  466. /* RLAYERfatal() already called */
  467. return OSSL_RECORD_RETURN_FATAL;
  468. }
  469. }
  470. rr->length = 0;
  471. rl->packet_length = 0;
  472. goto again;
  473. }
  474. if (!dtls_process_record(rl, bitmap)) {
  475. if (rl->alert != SSL_AD_NO_ALERT) {
  476. /* dtls_process_record() called RLAYERfatal */
  477. return OSSL_RECORD_RETURN_FATAL;
  478. }
  479. rr->length = 0;
  480. rl->packet_length = 0; /* dump this record */
  481. goto again; /* get another record */
  482. }
  483. if (rl->funcs->post_process_record && !rl->funcs->post_process_record(rl, rr)) {
  484. /* RLAYERfatal already called */
  485. return OSSL_RECORD_RETURN_FATAL;
  486. }
  487. rl->num_recs = 1;
  488. return OSSL_RECORD_RETURN_SUCCESS;
  489. }
  490. static int dtls_free(OSSL_RECORD_LAYER *rl)
  491. {
  492. TLS_BUFFER *rbuf;
  493. size_t left, written;
  494. pitem *item;
  495. DTLS_RLAYER_RECORD_DATA *rdata;
  496. int ret = 1;
  497. rbuf = &rl->rbuf;
  498. left = rbuf->left;
  499. if (left > 0) {
  500. /*
  501. * This record layer is closing but we still have data left in our
  502. * buffer. It must be destined for the next epoch - so push it there.
  503. */
  504. ret = BIO_write_ex(rl->next, rbuf->buf + rbuf->offset, left, &written);
  505. rbuf->left = 0;
  506. }
  507. if (rl->unprocessed_rcds != NULL) {
  508. while ((item = pqueue_pop(rl->unprocessed_rcds)) != NULL) {
  509. rdata = (DTLS_RLAYER_RECORD_DATA *)item->data;
  510. /* Push to the next record layer */
  511. ret &= BIO_write_ex(rl->next, rdata->packet, rdata->packet_length,
  512. &written);
  513. OPENSSL_free(rdata->rbuf.buf);
  514. OPENSSL_free(item->data);
  515. pitem_free(item);
  516. }
  517. pqueue_free(rl->unprocessed_rcds);
  518. }
  519. if (rl->processed_rcds!= NULL) {
  520. while ((item = pqueue_pop(rl->processed_rcds)) != NULL) {
  521. rdata = (DTLS_RLAYER_RECORD_DATA *)item->data;
  522. OPENSSL_free(rdata->rbuf.buf);
  523. OPENSSL_free(item->data);
  524. pitem_free(item);
  525. }
  526. pqueue_free(rl->processed_rcds);
  527. }
  528. return tls_free(rl) && ret;
  529. }
  530. static int
  531. dtls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  532. int role, int direction, int level, uint16_t epoch,
  533. unsigned char *secret, size_t secretlen,
  534. unsigned char *key, size_t keylen, unsigned char *iv,
  535. size_t ivlen, unsigned char *mackey, size_t mackeylen,
  536. const EVP_CIPHER *ciph, size_t taglen,
  537. int mactype,
  538. const EVP_MD *md, COMP_METHOD *comp,
  539. const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
  540. BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
  541. const OSSL_PARAM *settings, const OSSL_PARAM *options,
  542. const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
  543. OSSL_RECORD_LAYER **retrl)
  544. {
  545. int ret;
  546. ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
  547. ciph, taglen, md, comp, prev,
  548. transport, next, settings,
  549. options, fns, cbarg, retrl);
  550. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  551. return ret;
  552. (*retrl)->unprocessed_rcds = pqueue_new();
  553. (*retrl)->processed_rcds = pqueue_new();
  554. if ((*retrl)->unprocessed_rcds == NULL
  555. || (*retrl)->processed_rcds == NULL) {
  556. dtls_free(*retrl);
  557. *retrl = NULL;
  558. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  559. return OSSL_RECORD_RETURN_FATAL;
  560. }
  561. (*retrl)->isdtls = 1;
  562. (*retrl)->epoch = epoch;
  563. (*retrl)->in_init = 1;
  564. switch (vers) {
  565. case DTLS_ANY_VERSION:
  566. (*retrl)->funcs = &dtls_any_funcs;
  567. break;
  568. case DTLS1_2_VERSION:
  569. case DTLS1_VERSION:
  570. case DTLS1_BAD_VER:
  571. (*retrl)->funcs = &dtls_1_funcs;
  572. break;
  573. default:
  574. /* Should not happen */
  575. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  576. ret = OSSL_RECORD_RETURN_FATAL;
  577. goto err;
  578. }
  579. ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
  580. ivlen, mackey, mackeylen, ciph,
  581. taglen, mactype, md, comp);
  582. err:
  583. if (ret != OSSL_RECORD_RETURN_SUCCESS) {
  584. dtls_free(*retrl);
  585. *retrl = NULL;
  586. }
  587. return ret;
  588. }
  589. int dtls_prepare_record_header(OSSL_RECORD_LAYER *rl,
  590. WPACKET *thispkt,
  591. OSSL_RECORD_TEMPLATE *templ,
  592. uint8_t rectype,
  593. unsigned char **recdata)
  594. {
  595. size_t maxcomplen;
  596. *recdata = NULL;
  597. maxcomplen = templ->buflen;
  598. if (rl->compctx != NULL)
  599. maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  600. if (!WPACKET_put_bytes_u8(thispkt, rectype)
  601. || !WPACKET_put_bytes_u16(thispkt, templ->version)
  602. || !WPACKET_put_bytes_u16(thispkt, rl->epoch)
  603. || !WPACKET_memcpy(thispkt, &(rl->sequence[2]), 6)
  604. || !WPACKET_start_sub_packet_u16(thispkt)
  605. || (rl->eivlen > 0
  606. && !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
  607. || (maxcomplen > 0
  608. && !WPACKET_reserve_bytes(thispkt, maxcomplen,
  609. recdata))) {
  610. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  611. return 0;
  612. }
  613. return 1;
  614. }
  615. int dtls_post_encryption_processing(OSSL_RECORD_LAYER *rl,
  616. size_t mac_size,
  617. OSSL_RECORD_TEMPLATE *thistempl,
  618. WPACKET *thispkt,
  619. TLS_RL_RECORD *thiswr)
  620. {
  621. if (!tls_post_encryption_processing_default(rl, mac_size, thistempl,
  622. thispkt, thiswr)) {
  623. /* RLAYERfatal() already called */
  624. return 0;
  625. }
  626. return tls_increment_sequence_ctr(rl);
  627. }
  628. static size_t dtls_get_max_record_overhead(OSSL_RECORD_LAYER *rl)
  629. {
  630. size_t blocksize = 0;
  631. if (rl->enc_ctx != NULL &&
  632. (EVP_CIPHER_CTX_get_mode(rl->enc_ctx) == EVP_CIPH_CBC_MODE))
  633. blocksize = EVP_CIPHER_CTX_get_block_size(rl->enc_ctx);
  634. /*
  635. * If we have a cipher in place then the tag is mandatory. If the cipher is
  636. * CBC mode then an explicit IV is also mandatory. If we know the digest,
  637. * then we check it is consistent with the taglen. In the case of stitched
  638. * ciphers or AEAD ciphers we don't now the digest (or there isn't one) so
  639. * we just trust that the taglen is correct.
  640. */
  641. assert(rl->enc_ctx == NULL || ((blocksize == 0 || rl->eivlen > 0)
  642. && rl->taglen > 0));
  643. assert(rl->md == NULL || (int)rl->taglen == EVP_MD_size(rl->md));
  644. /*
  645. * Record overhead consists of the record header, the explicit IV, any
  646. * expansion due to cbc padding, and the mac/tag len. There could be
  647. * further expansion due to compression - but we don't know what this will
  648. * be without knowing the length of the data. However when this function is
  649. * called we don't know what the length will be yet - so this is a catch-22.
  650. * We *could* use SSL_3_RT_MAX_COMPRESSED_OVERHEAD which is an upper limit
  651. * for the maximum record size. But this value is larger than our fallback
  652. * MTU size - so isn't very helpful. We just ignore potential expansion
  653. * due to compression.
  654. */
  655. return DTLS1_RT_HEADER_LENGTH + rl->eivlen + blocksize + rl->taglen;
  656. }
  657. const OSSL_RECORD_METHOD ossl_dtls_record_method = {
  658. dtls_new_record_layer,
  659. dtls_free,
  660. tls_unprocessed_read_pending,
  661. tls_processed_read_pending,
  662. tls_app_data_pending,
  663. tls_get_max_records,
  664. tls_write_records,
  665. tls_retry_write_records,
  666. tls_read_record,
  667. tls_release_record,
  668. tls_get_alert_code,
  669. tls_set1_bio,
  670. tls_set_protocol_version,
  671. NULL,
  672. tls_set_first_handshake,
  673. tls_set_max_pipelines,
  674. dtls_set_in_init,
  675. tls_get_state,
  676. tls_set_options,
  677. tls_get_compression,
  678. tls_set_max_frag_len,
  679. dtls_get_max_record_overhead,
  680. tls_increment_sequence_ctr,
  681. tls_alloc_buffers,
  682. tls_free_buffers
  683. };