dtls_meth.c 25 KB

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