dtls_meth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. /* DTLSv1.3 records sets the legacy version field to DTLSv1.2 */
  380. && !(rr->rec_version == DTLS1_2_VERSION
  381. && rl->version == DTLS1_3_VERSION)) {
  382. if (rr->rec_version != rl->version) {
  383. /* unexpected version, silently discard */
  384. rr->length = 0;
  385. rl->packet_length = 0;
  386. goto again;
  387. }
  388. }
  389. if (ssl_major !=
  390. (rl->version == DTLS_ANY_VERSION ? DTLS1_VERSION_MAJOR
  391. : rl->version >> 8)) {
  392. /* wrong version, silently discard record */
  393. rr->length = 0;
  394. rl->packet_length = 0;
  395. goto again;
  396. }
  397. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  398. /* record too long, silently discard it */
  399. rr->length = 0;
  400. rl->packet_length = 0;
  401. goto again;
  402. }
  403. /*
  404. * If received packet overflows maximum possible fragment length then
  405. * silently discard it
  406. */
  407. if (rr->length > rl->max_frag_len + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
  408. /* record too long, silently discard it */
  409. rr->length = 0;
  410. rl->packet_length = 0;
  411. goto again;
  412. }
  413. /* now rl->rstate == SSL_ST_READ_BODY */
  414. }
  415. /* rl->rstate == SSL_ST_READ_BODY, get and decode the data */
  416. if (rr->length > rl->packet_length - DTLS1_RT_HEADER_LENGTH) {
  417. /* now rl->packet_length == DTLS1_RT_HEADER_LENGTH */
  418. more = rr->length;
  419. rret = rl->funcs->read_n(rl, more, more, 1, 1, &n);
  420. /* this packet contained a partial record, dump it */
  421. if (rret < OSSL_RECORD_RETURN_SUCCESS || n != more) {
  422. if (rl->alert != SSL_AD_NO_ALERT) {
  423. /* read_n() called RLAYERfatal() */
  424. return OSSL_RECORD_RETURN_FATAL;
  425. }
  426. rr->length = 0;
  427. rl->packet_length = 0;
  428. goto again;
  429. }
  430. /*
  431. * now n == rr->length,
  432. * and rl->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length
  433. */
  434. }
  435. /* set state for later operations */
  436. rl->rstate = SSL_ST_READ_HEADER;
  437. /* match epochs. NULL means the packet is dropped on the floor */
  438. bitmap = dtls_get_bitmap(rl, rr, &is_next_epoch);
  439. if (bitmap == NULL) {
  440. rr->length = 0;
  441. rl->packet_length = 0; /* dump this record */
  442. goto again; /* get another record */
  443. }
  444. #ifndef OPENSSL_NO_SCTP
  445. /* Only do replay check if no SCTP bio */
  446. if (!BIO_dgram_is_sctp(rl->bio)) {
  447. #endif
  448. /* Check whether this is a repeat, or aged record. */
  449. if (!dtls_record_replay_check(rl, bitmap)) {
  450. rr->length = 0;
  451. rl->packet_length = 0; /* dump this record */
  452. goto again; /* get another record */
  453. }
  454. #ifndef OPENSSL_NO_SCTP
  455. }
  456. #endif
  457. /* just read a 0 length packet */
  458. if (rr->length == 0)
  459. goto again;
  460. /*
  461. * If this record is from the next epoch (either HM or ALERT), and a
  462. * handshake is currently in progress, buffer it since it cannot be
  463. * processed at this time.
  464. */
  465. if (is_next_epoch) {
  466. if (rl->in_init) {
  467. if (dtls_rlayer_buffer_record(rl, rl->unprocessed_rcds,
  468. rr->seq_num) < 0) {
  469. /* RLAYERfatal() already called */
  470. return OSSL_RECORD_RETURN_FATAL;
  471. }
  472. }
  473. rr->length = 0;
  474. rl->packet_length = 0;
  475. goto again;
  476. }
  477. if (!dtls_process_record(rl, bitmap)) {
  478. if (rl->alert != SSL_AD_NO_ALERT) {
  479. /* dtls_process_record() called RLAYERfatal */
  480. return OSSL_RECORD_RETURN_FATAL;
  481. }
  482. rr->length = 0;
  483. rl->packet_length = 0; /* dump this record */
  484. goto again; /* get another record */
  485. }
  486. if (rl->funcs->post_process_record && !rl->funcs->post_process_record(rl, rr)) {
  487. /* RLAYERfatal already called */
  488. return OSSL_RECORD_RETURN_FATAL;
  489. }
  490. rl->num_recs = 1;
  491. return OSSL_RECORD_RETURN_SUCCESS;
  492. }
  493. static int dtls_free(OSSL_RECORD_LAYER *rl)
  494. {
  495. TLS_BUFFER *rbuf;
  496. size_t left, written;
  497. pitem *item;
  498. DTLS_RLAYER_RECORD_DATA *rdata;
  499. int ret = 1;
  500. rbuf = &rl->rbuf;
  501. left = rbuf->left;
  502. if (left > 0) {
  503. /*
  504. * This record layer is closing but we still have data left in our
  505. * buffer. It must be destined for the next epoch - so push it there.
  506. */
  507. ret = BIO_write_ex(rl->next, rbuf->buf + rbuf->offset, left, &written);
  508. rbuf->left = 0;
  509. }
  510. if (rl->unprocessed_rcds != NULL) {
  511. while ((item = pqueue_pop(rl->unprocessed_rcds)) != NULL) {
  512. rdata = (DTLS_RLAYER_RECORD_DATA *)item->data;
  513. /* Push to the next record layer */
  514. ret &= BIO_write_ex(rl->next, rdata->packet, rdata->packet_length,
  515. &written);
  516. OPENSSL_free(rdata->rbuf.buf);
  517. OPENSSL_free(item->data);
  518. pitem_free(item);
  519. }
  520. pqueue_free(rl->unprocessed_rcds);
  521. }
  522. if (rl->processed_rcds!= NULL) {
  523. while ((item = pqueue_pop(rl->processed_rcds)) != NULL) {
  524. rdata = (DTLS_RLAYER_RECORD_DATA *)item->data;
  525. OPENSSL_free(rdata->rbuf.buf);
  526. OPENSSL_free(item->data);
  527. pitem_free(item);
  528. }
  529. pqueue_free(rl->processed_rcds);
  530. }
  531. return tls_free(rl) && ret;
  532. }
  533. static int
  534. dtls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  535. int role, int direction, int level, uint16_t epoch,
  536. unsigned char *secret, size_t secretlen,
  537. unsigned char *key, size_t keylen, unsigned char *iv,
  538. size_t ivlen, unsigned char *mackey, size_t mackeylen,
  539. const EVP_CIPHER *ciph, size_t taglen,
  540. int mactype,
  541. const EVP_MD *md, COMP_METHOD *comp,
  542. const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
  543. BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
  544. const OSSL_PARAM *settings, const OSSL_PARAM *options,
  545. const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
  546. OSSL_RECORD_LAYER **retrl)
  547. {
  548. int ret;
  549. ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
  550. ciph, taglen, md, comp, prev,
  551. transport, next, settings,
  552. options, fns, cbarg, retrl);
  553. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  554. return ret;
  555. (*retrl)->unprocessed_rcds = pqueue_new();
  556. (*retrl)->processed_rcds = pqueue_new();
  557. if ((*retrl)->unprocessed_rcds == NULL
  558. || (*retrl)->processed_rcds == NULL) {
  559. dtls_free(*retrl);
  560. *retrl = NULL;
  561. ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
  562. return OSSL_RECORD_RETURN_FATAL;
  563. }
  564. (*retrl)->isdtls = 1;
  565. (*retrl)->epoch = epoch;
  566. (*retrl)->in_init = 1;
  567. switch (vers) {
  568. case DTLS_ANY_VERSION:
  569. (*retrl)->funcs = &dtls_any_funcs;
  570. break;
  571. case DTLS1_3_VERSION:
  572. (*retrl)->funcs = &dtls_1_3_funcs;
  573. break;
  574. case DTLS1_2_VERSION:
  575. case DTLS1_VERSION:
  576. case DTLS1_BAD_VER:
  577. (*retrl)->funcs = &dtls_1_funcs;
  578. break;
  579. default:
  580. /* Should not happen */
  581. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  582. ret = OSSL_RECORD_RETURN_FATAL;
  583. goto err;
  584. }
  585. ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
  586. ivlen, mackey, mackeylen, ciph,
  587. taglen, mactype, md, comp);
  588. err:
  589. if (ret != OSSL_RECORD_RETURN_SUCCESS) {
  590. dtls_free(*retrl);
  591. *retrl = NULL;
  592. }
  593. return ret;
  594. }
  595. int dtls_prepare_record_header(OSSL_RECORD_LAYER *rl,
  596. WPACKET *thispkt,
  597. OSSL_RECORD_TEMPLATE *templ,
  598. uint8_t rectype,
  599. unsigned char **recdata)
  600. {
  601. size_t maxcomplen;
  602. *recdata = NULL;
  603. maxcomplen = templ->buflen;
  604. if (rl->compctx != NULL)
  605. maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  606. if (!WPACKET_put_bytes_u8(thispkt, rectype)
  607. || !WPACKET_put_bytes_u16(thispkt, templ->version)
  608. || !WPACKET_put_bytes_u16(thispkt, rl->epoch)
  609. || !WPACKET_memcpy(thispkt, &(rl->sequence[2]), 6)
  610. || !WPACKET_start_sub_packet_u16(thispkt)
  611. || (rl->eivlen > 0
  612. && !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
  613. || (maxcomplen > 0
  614. && !WPACKET_reserve_bytes(thispkt, maxcomplen,
  615. recdata))) {
  616. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  617. return 0;
  618. }
  619. return 1;
  620. }
  621. int dtls_post_encryption_processing(OSSL_RECORD_LAYER *rl,
  622. size_t mac_size,
  623. OSSL_RECORD_TEMPLATE *thistempl,
  624. WPACKET *thispkt,
  625. TLS_RL_RECORD *thiswr)
  626. {
  627. if (!tls_post_encryption_processing_default(rl, mac_size, thistempl,
  628. thispkt, thiswr)) {
  629. /* RLAYERfatal() already called */
  630. return 0;
  631. }
  632. return tls_increment_sequence_ctr(rl);
  633. }
  634. static size_t dtls_get_max_record_overhead(OSSL_RECORD_LAYER *rl)
  635. {
  636. size_t blocksize = 0;
  637. if (rl->enc_ctx != NULL &&
  638. (EVP_CIPHER_CTX_get_mode(rl->enc_ctx) == EVP_CIPH_CBC_MODE))
  639. blocksize = EVP_CIPHER_CTX_get_block_size(rl->enc_ctx);
  640. /*
  641. * If we have a cipher in place then the tag is mandatory. If the cipher is
  642. * CBC mode then an explicit IV is also mandatory. If we know the digest,
  643. * then we check it is consistent with the taglen. In the case of stitched
  644. * ciphers or AEAD ciphers we don't now the digest (or there isn't one) so
  645. * we just trust that the taglen is correct.
  646. */
  647. assert(rl->enc_ctx == NULL || ((blocksize == 0 || rl->eivlen > 0)
  648. && rl->taglen > 0));
  649. assert(rl->md == NULL || (int)rl->taglen == EVP_MD_size(rl->md));
  650. /*
  651. * Record overhead consists of the record header, the explicit IV, any
  652. * expansion due to cbc padding, and the mac/tag len. There could be
  653. * further expansion due to compression - but we don't know what this will
  654. * be without knowing the length of the data. However when this function is
  655. * called we don't know what the length will be yet - so this is a catch-22.
  656. * We *could* use SSL_3_RT_MAX_COMPRESSED_OVERHEAD which is an upper limit
  657. * for the maximum record size. But this value is larger than our fallback
  658. * MTU size - so isn't very helpful. We just ignore potential expansion
  659. * due to compression.
  660. */
  661. return DTLS1_RT_HEADER_LENGTH + rl->eivlen + blocksize + rl->taglen;
  662. }
  663. const OSSL_RECORD_METHOD ossl_dtls_record_method = {
  664. dtls_new_record_layer,
  665. dtls_free,
  666. tls_unprocessed_read_pending,
  667. tls_processed_read_pending,
  668. tls_app_data_pending,
  669. tls_get_max_records,
  670. tls_write_records,
  671. tls_retry_write_records,
  672. tls_read_record,
  673. tls_release_record,
  674. tls_get_alert_code,
  675. tls_set1_bio,
  676. tls_set_protocol_version,
  677. tls_set_plain_alerts,
  678. tls_set_first_handshake,
  679. tls_set_max_pipelines,
  680. dtls_set_in_init,
  681. tls_get_state,
  682. tls_set_options,
  683. tls_get_compression,
  684. tls_set_max_frag_len,
  685. dtls_get_max_record_overhead,
  686. tls_increment_sequence_ctr,
  687. tls_alloc_buffers,
  688. tls_free_buffers
  689. };