mps_reader.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Message Processing Stack, Reader implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  9. #include "mps_reader.h"
  10. #include "mps_common.h"
  11. #include "mps_trace.h"
  12. #include <string.h>
  13. #if defined(MBEDTLS_MPS_ENABLE_TRACE)
  14. static int mbedtls_mps_trace_id = MBEDTLS_MPS_TRACE_BIT_READER;
  15. #endif /* MBEDTLS_MPS_ENABLE_TRACE */
  16. /*
  17. * GENERAL NOTE ON CODING STYLE
  18. *
  19. * The following code intentionally separates memory loads
  20. * and stores from other operations (arithmetic or branches).
  21. * This leads to the introduction of many local variables
  22. * and significantly increases the C-code line count, but
  23. * should not increase the size of generated assembly.
  24. *
  25. * The reason for this is twofold:
  26. * (1) It will ease verification efforts using the VST
  27. * (Verified Software Toolchain)
  28. * whose program logic cannot directly reason
  29. * about instructions containing a load or store in
  30. * addition to other operations (e.g. *p = *q or
  31. * tmp = *p + 42).
  32. * (2) Operating on local variables and writing the results
  33. * back to the target contexts on success only
  34. * allows to maintain structure invariants even
  35. * on failure - this in turn has two benefits:
  36. * (2.a) If for some reason an error code is not caught
  37. * and operation continues, functions are nonetheless
  38. * called with sane contexts, reducing the risk
  39. * of dangerous behavior.
  40. * (2.b) Randomized testing is easier if structures
  41. * remain intact even in the face of failing
  42. * and/or non-sensical calls.
  43. * Moreover, it might even reduce code-size because
  44. * the compiler need not write back temporary results
  45. * to memory in case of failure.
  46. *
  47. */
  48. static inline int mps_reader_is_accumulating(
  49. mbedtls_mps_reader const *rd)
  50. {
  51. mbedtls_mps_size_t acc_remaining;
  52. if (rd->acc == NULL) {
  53. return 0;
  54. }
  55. acc_remaining = rd->acc_share.acc_remaining;
  56. return acc_remaining > 0;
  57. }
  58. static inline int mps_reader_is_producing(
  59. mbedtls_mps_reader const *rd)
  60. {
  61. unsigned char *frag = rd->frag;
  62. return frag == NULL;
  63. }
  64. static inline int mps_reader_is_consuming(
  65. mbedtls_mps_reader const *rd)
  66. {
  67. return !mps_reader_is_producing(rd);
  68. }
  69. static inline mbedtls_mps_size_t mps_reader_get_fragment_offset(
  70. mbedtls_mps_reader const *rd)
  71. {
  72. unsigned char *acc = rd->acc;
  73. mbedtls_mps_size_t frag_offset;
  74. if (acc == NULL) {
  75. return 0;
  76. }
  77. frag_offset = rd->acc_share.frag_offset;
  78. return frag_offset;
  79. }
  80. static inline mbedtls_mps_size_t mps_reader_serving_from_accumulator(
  81. mbedtls_mps_reader const *rd)
  82. {
  83. mbedtls_mps_size_t frag_offset, end;
  84. frag_offset = mps_reader_get_fragment_offset(rd);
  85. end = rd->end;
  86. return end < frag_offset;
  87. }
  88. static inline void mps_reader_zero(mbedtls_mps_reader *rd)
  89. {
  90. /* A plain memset() would likely be more efficient,
  91. * but the current way of zeroing makes it harder
  92. * to overlook fields which should not be zero-initialized.
  93. * It's also more suitable for FV efforts since it
  94. * doesn't require reasoning about structs being
  95. * interpreted as unstructured binary blobs. */
  96. static mbedtls_mps_reader const zero =
  97. { .frag = NULL,
  98. .frag_len = 0,
  99. .commit = 0,
  100. .end = 0,
  101. .pending = 0,
  102. .acc = NULL,
  103. .acc_len = 0,
  104. .acc_available = 0,
  105. .acc_share = { .acc_remaining = 0 } };
  106. *rd = zero;
  107. }
  108. int mbedtls_mps_reader_init(mbedtls_mps_reader *rd,
  109. unsigned char *acc,
  110. mbedtls_mps_size_t acc_len)
  111. {
  112. MBEDTLS_MPS_TRACE_INIT("mbedtls_mps_reader_init");
  113. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  114. "* Accumulator size: %u bytes", (unsigned) acc_len);
  115. mps_reader_zero(rd);
  116. rd->acc = acc;
  117. rd->acc_len = acc_len;
  118. MBEDTLS_MPS_TRACE_RETURN(0);
  119. }
  120. int mbedtls_mps_reader_free(mbedtls_mps_reader *rd)
  121. {
  122. MBEDTLS_MPS_TRACE_INIT("mbedtls_mps_reader_free");
  123. mps_reader_zero(rd);
  124. MBEDTLS_MPS_TRACE_RETURN(0);
  125. }
  126. int mbedtls_mps_reader_feed(mbedtls_mps_reader *rd,
  127. unsigned char *new_frag,
  128. mbedtls_mps_size_t new_frag_len)
  129. {
  130. mbedtls_mps_size_t copy_to_acc;
  131. MBEDTLS_MPS_TRACE_INIT("mbedtls_mps_reader_feed");
  132. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  133. "* Fragment length: %u bytes", (unsigned) new_frag_len);
  134. if (new_frag == NULL) {
  135. MBEDTLS_MPS_TRACE_RETURN(MBEDTLS_ERR_MPS_READER_INVALID_ARG);
  136. }
  137. MBEDTLS_MPS_STATE_VALIDATE_RAW(mps_reader_is_producing(
  138. rd),
  139. "mbedtls_mps_reader_feed() requires reader to be in producing mode");
  140. if (mps_reader_is_accumulating(rd)) {
  141. unsigned char *acc = rd->acc;
  142. mbedtls_mps_size_t acc_remaining = rd->acc_share.acc_remaining;
  143. mbedtls_mps_size_t acc_available = rd->acc_available;
  144. /* Skip over parts of the accumulator that have already been filled. */
  145. acc += acc_available;
  146. copy_to_acc = acc_remaining;
  147. if (copy_to_acc > new_frag_len) {
  148. copy_to_acc = new_frag_len;
  149. }
  150. /* Copy new contents to accumulator. */
  151. memcpy(acc, new_frag, copy_to_acc);
  152. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  153. "Copy new data of size %u of %u into accumulator at offset %u",
  154. (unsigned) copy_to_acc, (unsigned) new_frag_len,
  155. (unsigned) acc_available);
  156. /* Check if, with the new fragment, we have enough data. */
  157. acc_remaining -= copy_to_acc;
  158. if (acc_remaining > 0) {
  159. /* We need to accumulate more data. Stay in producing mode. */
  160. acc_available += copy_to_acc;
  161. rd->acc_share.acc_remaining = acc_remaining;
  162. rd->acc_available = acc_available;
  163. MBEDTLS_MPS_TRACE_RETURN(MBEDTLS_ERR_MPS_READER_NEED_MORE);
  164. }
  165. /* We have filled the accumulator: Move to consuming mode. */
  166. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  167. "Enough data available to serve user request");
  168. /* Remember overlap of accumulator and fragment. */
  169. rd->acc_share.frag_offset = acc_available;
  170. acc_available += copy_to_acc;
  171. rd->acc_available = acc_available;
  172. } else { /* Not accumulating */
  173. rd->acc_share.frag_offset = 0;
  174. }
  175. rd->frag = new_frag;
  176. rd->frag_len = new_frag_len;
  177. rd->commit = 0;
  178. rd->end = 0;
  179. MBEDTLS_MPS_TRACE_RETURN(0);
  180. }
  181. int mbedtls_mps_reader_get(mbedtls_mps_reader *rd,
  182. mbedtls_mps_size_t desired,
  183. unsigned char **buffer,
  184. mbedtls_mps_size_t *buflen)
  185. {
  186. unsigned char *frag;
  187. mbedtls_mps_size_t frag_len, frag_offset, end, frag_fetched, frag_remaining;
  188. MBEDTLS_MPS_TRACE_INIT("mbedtls_mps_reader_get");
  189. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  190. "* Bytes requested: %u", (unsigned) desired);
  191. MBEDTLS_MPS_STATE_VALIDATE_RAW(mps_reader_is_consuming(
  192. rd),
  193. "mbedtls_mps_reader_get() requires reader to be in consuming mode");
  194. end = rd->end;
  195. frag_offset = mps_reader_get_fragment_offset(rd);
  196. /* Check if we're still serving from the accumulator. */
  197. if (mps_reader_serving_from_accumulator(rd)) {
  198. /* Illustration of supported and unsupported cases:
  199. *
  200. * - Allowed #1
  201. *
  202. * +-----------------------------------+
  203. * | frag |
  204. * +-----------------------------------+
  205. *
  206. * end end+desired
  207. * | |
  208. * +-----v-------v-------------+
  209. * | acc |
  210. * +---------------------------+
  211. * | |
  212. * frag_offset acc_available
  213. *
  214. * - Allowed #2
  215. *
  216. * +-----------------------------------+
  217. * | frag |
  218. * +-----------------------------------+
  219. *
  220. * end end+desired
  221. * | |
  222. * +----------v----------------v
  223. * | acc |
  224. * +---------------------------+
  225. * | |
  226. * frag_offset acc_available
  227. *
  228. * - Not allowed #1 (could be served, but we don't actually use it):
  229. *
  230. * +-----------------------------------+
  231. * | frag |
  232. * +-----------------------------------+
  233. *
  234. * end end+desired
  235. * | |
  236. * +------v-------------v------+
  237. * | acc |
  238. * +---------------------------+
  239. * | |
  240. * frag_offset acc_available
  241. *
  242. *
  243. * - Not allowed #2 (can't be served with a contiguous buffer):
  244. *
  245. * +-----------------------------------+
  246. * | frag |
  247. * +-----------------------------------+
  248. *
  249. * end end + desired
  250. * | |
  251. * +------v--------------------+ v
  252. * | acc |
  253. * +---------------------------+
  254. * | |
  255. * frag_offset acc_available
  256. *
  257. * In case of Allowed #2 we're switching to serve from
  258. * `frag` starting from the next call to mbedtls_mps_reader_get().
  259. */
  260. unsigned char *acc;
  261. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  262. "Serve the request from the accumulator");
  263. if (frag_offset - end < desired) {
  264. mbedtls_mps_size_t acc_available;
  265. acc_available = rd->acc_available;
  266. if (acc_available - end != desired) {
  267. /* It might be possible to serve some of these situations by
  268. * making additional space in the accumulator, removing those
  269. * parts that have already been committed.
  270. * On the other hand, this brings additional complexity and
  271. * enlarges the code size, while there doesn't seem to be a use
  272. * case where we don't attempt exactly the same `get` calls when
  273. * resuming on a reader than what we tried before pausing it.
  274. * If we believe we adhere to this restricted usage throughout
  275. * the library, this check is a good opportunity to
  276. * validate this. */
  277. MBEDTLS_MPS_TRACE_RETURN(
  278. MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS);
  279. }
  280. }
  281. acc = rd->acc;
  282. acc += end;
  283. *buffer = acc;
  284. if (buflen != NULL) {
  285. *buflen = desired;
  286. }
  287. end += desired;
  288. rd->end = end;
  289. rd->pending = 0;
  290. MBEDTLS_MPS_TRACE_RETURN(0);
  291. }
  292. /* Attempt to serve the request from the current fragment */
  293. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  294. "Serve the request from the current fragment.");
  295. frag_len = rd->frag_len;
  296. frag_fetched = end - frag_offset; /* The amount of data from the current
  297. * fragment that has already been passed
  298. * to the user. */
  299. frag_remaining = frag_len - frag_fetched; /* Remaining data in fragment */
  300. /* Check if we can serve the read request from the fragment. */
  301. if (frag_remaining < desired) {
  302. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  303. "There's not enough data in the current fragment "
  304. "to serve the request.");
  305. /* There's not enough data in the current fragment,
  306. * so either just RETURN what we have or fail. */
  307. if (buflen == NULL) {
  308. if (frag_remaining > 0) {
  309. rd->pending = desired - frag_remaining;
  310. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  311. "Remember to collect %u bytes before re-opening",
  312. (unsigned) rd->pending);
  313. }
  314. MBEDTLS_MPS_TRACE_RETURN(MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
  315. }
  316. desired = frag_remaining;
  317. }
  318. /* There's enough data in the current fragment to serve the
  319. * (potentially modified) read request. */
  320. frag = rd->frag;
  321. frag += frag_fetched;
  322. *buffer = frag;
  323. if (buflen != NULL) {
  324. *buflen = desired;
  325. }
  326. end += desired;
  327. rd->end = end;
  328. rd->pending = 0;
  329. MBEDTLS_MPS_TRACE_RETURN(0);
  330. }
  331. int mbedtls_mps_reader_commit(mbedtls_mps_reader *rd)
  332. {
  333. mbedtls_mps_size_t end;
  334. MBEDTLS_MPS_TRACE_INIT("mbedtls_mps_reader_commit");
  335. MBEDTLS_MPS_STATE_VALIDATE_RAW(mps_reader_is_consuming(
  336. rd),
  337. "mbedtls_mps_reader_commit() requires reader to be in consuming mode");
  338. end = rd->end;
  339. rd->commit = end;
  340. MBEDTLS_MPS_TRACE_RETURN(0);
  341. }
  342. int mbedtls_mps_reader_reclaim(mbedtls_mps_reader *rd,
  343. int *paused)
  344. {
  345. unsigned char *frag, *acc;
  346. mbedtls_mps_size_t pending, commit;
  347. mbedtls_mps_size_t acc_len, frag_offset, frag_len;
  348. MBEDTLS_MPS_TRACE_INIT("mbedtls_mps_reader_reclaim");
  349. if (paused != NULL) {
  350. *paused = 0;
  351. }
  352. MBEDTLS_MPS_STATE_VALIDATE_RAW(mps_reader_is_consuming(
  353. rd),
  354. "mbedtls_mps_reader_reclaim() requires reader to be in consuming mode");
  355. frag = rd->frag;
  356. acc = rd->acc;
  357. pending = rd->pending;
  358. commit = rd->commit;
  359. frag_len = rd->frag_len;
  360. frag_offset = mps_reader_get_fragment_offset(rd);
  361. if (pending == 0) {
  362. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  363. "No unsatisfied read-request has been logged.");
  364. /* Check if there's data left to be consumed. */
  365. if (commit < frag_offset || commit - frag_offset < frag_len) {
  366. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  367. "There is data left to be consumed.");
  368. rd->end = commit;
  369. MBEDTLS_MPS_TRACE_RETURN(MBEDTLS_ERR_MPS_READER_DATA_LEFT);
  370. }
  371. rd->acc_available = 0;
  372. rd->acc_share.acc_remaining = 0;
  373. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  374. "Fragment has been fully processed and committed.");
  375. } else {
  376. int overflow;
  377. mbedtls_mps_size_t acc_backup_offset;
  378. mbedtls_mps_size_t acc_backup_len;
  379. mbedtls_mps_size_t frag_backup_offset;
  380. mbedtls_mps_size_t frag_backup_len;
  381. mbedtls_mps_size_t backup_len;
  382. mbedtls_mps_size_t acc_len_needed;
  383. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  384. "There has been an unsatisfied read with %u bytes overhead.",
  385. (unsigned) pending);
  386. if (acc == NULL) {
  387. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  388. "No accumulator present");
  389. MBEDTLS_MPS_TRACE_RETURN(
  390. MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR);
  391. }
  392. acc_len = rd->acc_len;
  393. /* Check if the upper layer has already fetched
  394. * and committed the contents of the accumulator. */
  395. if (commit < frag_offset) {
  396. /* No, accumulator is still being processed. */
  397. frag_backup_offset = 0;
  398. frag_backup_len = frag_len;
  399. acc_backup_offset = commit;
  400. acc_backup_len = frag_offset - commit;
  401. } else {
  402. /* Yes, the accumulator is already processed. */
  403. frag_backup_offset = commit - frag_offset;
  404. frag_backup_len = frag_len - frag_backup_offset;
  405. acc_backup_offset = 0;
  406. acc_backup_len = 0;
  407. }
  408. backup_len = acc_backup_len + frag_backup_len;
  409. acc_len_needed = backup_len + pending;
  410. overflow = 0;
  411. overflow |= (backup_len < acc_backup_len);
  412. overflow |= (acc_len_needed < backup_len);
  413. if (overflow || acc_len < acc_len_needed) {
  414. /* Except for the different return code, we behave as if
  415. * there hadn't been a call to mbedtls_mps_reader_get()
  416. * since the last commit. */
  417. rd->end = commit;
  418. rd->pending = 0;
  419. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_ERROR,
  420. "The accumulator is too small to handle the backup.");
  421. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_ERROR,
  422. "* Size: %u", (unsigned) acc_len);
  423. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_ERROR,
  424. "* Needed: %u (%u + %u)",
  425. (unsigned) acc_len_needed,
  426. (unsigned) backup_len, (unsigned) pending);
  427. MBEDTLS_MPS_TRACE_RETURN(
  428. MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL);
  429. }
  430. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  431. "Fragment backup: %u", (unsigned) frag_backup_len);
  432. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  433. "Accumulator backup: %u", (unsigned) acc_backup_len);
  434. /* Move uncommitted parts from the accumulator to the front
  435. * of the accumulator. */
  436. memmove(acc, acc + acc_backup_offset, acc_backup_len);
  437. /* Copy uncommitted parts of the current fragment to the
  438. * accumulator. */
  439. memcpy(acc + acc_backup_len,
  440. frag + frag_backup_offset, frag_backup_len);
  441. rd->acc_available = backup_len;
  442. rd->acc_share.acc_remaining = pending;
  443. if (paused != NULL) {
  444. *paused = 1;
  445. }
  446. }
  447. rd->frag = NULL;
  448. rd->frag_len = 0;
  449. rd->commit = 0;
  450. rd->end = 0;
  451. rd->pending = 0;
  452. MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  453. "Final state: aa %u, al %u, ar %u",
  454. (unsigned) rd->acc_available, (unsigned) rd->acc_len,
  455. (unsigned) rd->acc_share.acc_remaining);
  456. MBEDTLS_MPS_TRACE_RETURN(0);
  457. }
  458. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */