mps_reader.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright The Mbed TLS Contributors
  3. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  4. */
  5. /**
  6. * \file mps_reader.h
  7. *
  8. * \brief This file defines reader objects, which together with their
  9. * sibling writer objects form the basis for the communication
  10. * between the various layers of the Mbed TLS messaging stack,
  11. * as well as the communication between the messaging stack and
  12. * the (D)TLS handshake protocol implementation.
  13. *
  14. * Readers provide a means of transferring incoming data from
  15. * a 'producer' providing it in chunks of arbitrary size, to
  16. * a 'consumer' which fetches and processes it in chunks of
  17. * again arbitrary, and potentially different, size.
  18. *
  19. * Readers can thus be seen as datagram-to-stream converters,
  20. * and they abstract away the following two tasks from the user:
  21. * 1. The pointer arithmetic of stepping through a producer-
  22. * provided chunk in smaller chunks.
  23. * 2. The merging of incoming data chunks in case the
  24. * consumer requests data in larger chunks than what the
  25. * producer provides.
  26. *
  27. * The basic abstract flow of operation is the following:
  28. * - Initially, the reader is in 'producing mode'.
  29. * - The producer hands an incoming data buffer to the reader,
  30. * moving it from 'producing' to 'consuming' mode.
  31. * - The consumer subsequently fetches and processes the buffer
  32. * content. Once that's done -- or partially done and a consumer's
  33. * request can't be fulfilled -- the producer revokes the reader's
  34. * access to the incoming data buffer, putting the reader back to
  35. * producing mode.
  36. * - The producer subsequently gathers more incoming data and hands
  37. * it to the reader until it switches back to consuming mode
  38. * if enough data is available for the last consumer request to
  39. * be satisfiable.
  40. * - Repeat the above.
  41. *
  42. * The abstract states of the reader from the producer's and
  43. * consumer's perspective are as follows:
  44. *
  45. * - From the perspective of the consumer, the state of the
  46. * reader consists of the following:
  47. * - A byte stream representing (concatenation of) the data
  48. * received through calls to mbedtls_mps_reader_get(),
  49. * - A marker within that byte stream indicating which data
  50. * can be considered processed, and hence need not be retained,
  51. * when the reader is passed back to the producer via
  52. * mbedtls_mps_reader_reclaim().
  53. * The marker is set via mbedtls_mps_reader_commit()
  54. * which places it at the end of the current byte stream.
  55. * The consumer need not be aware of the distinction between consumer
  56. * and producer mode, because it only interfaces with the reader
  57. * when the latter is in consuming mode.
  58. *
  59. * - From the perspective of the producer, the reader's state is one of:
  60. * - Attached: The reader is in consuming mode.
  61. * - Unset: No incoming data buffer is currently managed by the reader,
  62. * and all previously handed incoming data buffers have been
  63. * fully processed. More data needs to be fed into the reader
  64. * via mbedtls_mps_reader_feed().
  65. *
  66. * - Accumulating: No incoming data buffer is currently managed by the
  67. * reader, but some data from the previous incoming data
  68. * buffer hasn't been processed yet and is internally
  69. * held back.
  70. * The Attached state belongs to consuming mode, while the Unset and
  71. * Accumulating states belong to producing mode.
  72. *
  73. * Transitioning from the Unset or Accumulating state to Attached is
  74. * done via successful calls to mbedtls_mps_reader_feed(), while
  75. * transitioning from Attached to either Unset or Accumulating (depending
  76. * on what has been processed) is done via mbedtls_mps_reader_reclaim().
  77. *
  78. * The following diagram depicts the producer-state progression:
  79. *
  80. * +------------------+ reclaim
  81. * | Unset +<-------------------------------------+ get
  82. * +--------|---------+ | +------+
  83. * | | | |
  84. * | | | |
  85. * | feed +---------+---+--+ |
  86. * +--------------------------------------> <---+
  87. * | Attached |
  88. * +--------------------------------------> <---+
  89. * | feed, enough data available +---------+---+--+ |
  90. * | to serve previous consumer request | | |
  91. * | | | |
  92. * +--------+---------+ | +------+
  93. * +----> Accumulating |<-------------------------------------+ commit
  94. * | +---+--------------+ reclaim, previous read request
  95. * | | couldn't be fulfilled
  96. * | |
  97. * +--------+
  98. * feed, need more data to serve
  99. * previous consumer request
  100. * |
  101. * |
  102. * producing mode | consuming mode
  103. * |
  104. *
  105. */
  106. #ifndef MBEDTLS_READER_H
  107. #define MBEDTLS_READER_H
  108. #include <stdio.h>
  109. #include "mps_common.h"
  110. #include "mps_error.h"
  111. struct mbedtls_mps_reader;
  112. typedef struct mbedtls_mps_reader mbedtls_mps_reader;
  113. /*
  114. * Structure definitions
  115. */
  116. struct mbedtls_mps_reader {
  117. unsigned char *frag; /*!< The fragment of incoming data managed by
  118. * the reader; it is provided to the reader
  119. * through mbedtls_mps_reader_feed(). The reader
  120. * does not own the fragment and does not
  121. * perform any allocation operations on it,
  122. * but does have read and write access to it.
  123. *
  124. * The reader is in consuming mode if
  125. * and only if \c frag is not \c NULL. */
  126. mbedtls_mps_stored_size_t frag_len;
  127. /*!< The length of the current fragment.
  128. * Must be 0 if \c frag == \c NULL. */
  129. mbedtls_mps_stored_size_t commit;
  130. /*!< The offset of the last commit, relative
  131. * to the first byte in the fragment, if
  132. * no accumulator is present. If an accumulator
  133. * is present, it is viewed as a prefix to the
  134. * current fragment, and this variable contains
  135. * an offset from the beginning of the accumulator.
  136. *
  137. * This is only used when the reader is in
  138. * consuming mode, i.e. \c frag != \c NULL;
  139. * otherwise, its value is \c 0. */
  140. mbedtls_mps_stored_size_t end;
  141. /*!< The offset of the end of the last chunk
  142. * passed to the user through a call to
  143. * mbedtls_mps_reader_get(), relative to the first
  144. * byte in the fragment, if no accumulator is
  145. * present. If an accumulator is present, it is
  146. * viewed as a prefix to the current fragment, and
  147. * this variable contains an offset from the
  148. * beginning of the accumulator.
  149. *
  150. * This is only used when the reader is in
  151. * consuming mode, i.e. \c frag != \c NULL;
  152. * otherwise, its value is \c 0. */
  153. mbedtls_mps_stored_size_t pending;
  154. /*!< The amount of incoming data missing on the
  155. * last call to mbedtls_mps_reader_get().
  156. * In particular, it is \c 0 if the last call
  157. * was successful.
  158. * If a reader is reclaimed after an
  159. * unsuccessful call to mbedtls_mps_reader_get(),
  160. * this variable is used to have the reader
  161. * remember how much data should be accumulated
  162. * so that the call to mbedtls_mps_reader_get()
  163. * succeeds next time.
  164. * This is only used when the reader is in
  165. * consuming mode, i.e. \c frag != \c NULL;
  166. * otherwise, its value is \c 0. */
  167. /* The accumulator is only needed if we need to be able to pause
  168. * the reader. A few bytes could be saved by moving this to a
  169. * separate struct and using a pointer here. */
  170. unsigned char *acc; /*!< The accumulator is used to gather incoming
  171. * data if a read-request via mbedtls_mps_reader_get()
  172. * cannot be served from the current fragment. */
  173. mbedtls_mps_stored_size_t acc_len;
  174. /*!< The total size of the accumulator. */
  175. mbedtls_mps_stored_size_t acc_available;
  176. /*!< The number of bytes currently gathered in
  177. * the accumulator. This is both used in
  178. * producing and in consuming mode:
  179. * While producing, it is increased until
  180. * it reaches the value of \c acc_remaining below.
  181. * While consuming, it is used to judge if a
  182. * get request can be served from the
  183. * accumulator or not.
  184. * Must not be larger than \c acc_len. */
  185. union {
  186. mbedtls_mps_stored_size_t acc_remaining;
  187. /*!< This indicates the amount of data still
  188. * to be gathered in the accumulator. It is
  189. * only used in producing mode.
  190. * Must be at most acc_len - acc_available. */
  191. mbedtls_mps_stored_size_t frag_offset;
  192. /*!< If an accumulator is present and in use, this
  193. * field indicates the offset of the current
  194. * fragment from the beginning of the
  195. * accumulator. If no accumulator is present
  196. * or the accumulator is not in use, this is \c 0.
  197. * It is only used in consuming mode.
  198. * Must not be larger than \c acc_available. */
  199. } acc_share;
  200. };
  201. /*
  202. * API organization:
  203. * A reader object is usually prepared and maintained
  204. * by some lower layer and passed for usage to an upper
  205. * layer, and the API naturally splits according to which
  206. * layer is supposed to use the respective functions.
  207. */
  208. /*
  209. * Maintenance API (Lower layer)
  210. */
  211. /**
  212. * \brief Initialize a reader object
  213. *
  214. * \param reader The reader to be initialized.
  215. * \param acc The buffer to be used as a temporary accumulator
  216. * in case get requests through mbedtls_mps_reader_get()
  217. * exceed the buffer provided by mbedtls_mps_reader_feed().
  218. * This buffer is owned by the caller and exclusive use
  219. * for reading and writing is given to the reader for the
  220. * duration of the reader's lifetime. It is thus the caller's
  221. * responsibility to maintain (and not touch) the buffer for
  222. * the lifetime of the reader, and to properly zeroize and
  223. * free the memory after the reader has been destroyed.
  224. * \param acc_len The size in Bytes of \p acc.
  225. *
  226. * \return \c 0 on success.
  227. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  228. */
  229. int mbedtls_mps_reader_init(mbedtls_mps_reader *reader,
  230. unsigned char *acc,
  231. mbedtls_mps_size_t acc_len);
  232. /**
  233. * \brief Free a reader object
  234. *
  235. * \param reader The reader to be freed.
  236. *
  237. * \return \c 0 on success.
  238. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  239. */
  240. int mbedtls_mps_reader_free(mbedtls_mps_reader *reader);
  241. /**
  242. * \brief Pass chunk of data for the reader to manage.
  243. *
  244. * \param reader The reader context to use. The reader must be
  245. * in producing mode.
  246. * \param buf The buffer to be managed by the reader.
  247. * \param buflen The size in Bytes of \p buffer.
  248. *
  249. * \return \c 0 on success. In this case, the reader will be
  250. * moved to consuming mode and obtains read access
  251. * of \p buf until mbedtls_mps_reader_reclaim()
  252. * is called. It is the responsibility of the caller
  253. * to ensure that the \p buf persists and is not changed
  254. * between successful calls to mbedtls_mps_reader_feed()
  255. * and mbedtls_mps_reader_reclaim().
  256. * \return \c MBEDTLS_ERR_MPS_READER_NEED_MORE if more input data is
  257. * required to fulfill a previous request to mbedtls_mps_reader_get().
  258. * In this case, the reader remains in producing mode and
  259. * takes no ownership of the provided buffer (an internal copy
  260. * is made instead).
  261. * \return Another negative \c MBEDTLS_ERR_READER_XXX error code on
  262. * different kinds of failures.
  263. */
  264. int mbedtls_mps_reader_feed(mbedtls_mps_reader *reader,
  265. unsigned char *buf,
  266. mbedtls_mps_size_t buflen);
  267. /**
  268. * \brief Reclaim reader's access to the current input buffer.
  269. *
  270. * \param reader The reader context to use. The reader must be
  271. * in consuming mode.
  272. * \param paused If not \c NULL, the integer at address \p paused will be
  273. * modified to indicate whether the reader has been paused
  274. * (value \c 1) or not (value \c 0). Pausing happens if there
  275. * is uncommitted data and a previous request to
  276. * mbedtls_mps_reader_get() has exceeded the bounds of the
  277. * input buffer.
  278. *
  279. * \return \c 0 on success.
  280. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  281. */
  282. int mbedtls_mps_reader_reclaim(mbedtls_mps_reader *reader,
  283. int *paused);
  284. /*
  285. * Usage API (Upper layer)
  286. */
  287. /**
  288. * \brief Request data from the reader.
  289. *
  290. * \param reader The reader context to use. The reader must
  291. * be in consuming mode.
  292. * \param desired The desired amount of data to be read, in Bytes.
  293. * \param buffer The address to store the buffer pointer in.
  294. * This must not be \c NULL.
  295. * \param buflen The address to store the actual buffer
  296. * length in, or \c NULL.
  297. *
  298. * \return \c 0 on success. In this case, \c *buf holds the
  299. * address of a buffer of size \c *buflen
  300. * (if \c buflen != \c NULL) or \c desired
  301. * (if \c buflen == \c NULL). The user has read access
  302. * to the buffer and guarantee of stability of the data
  303. * until the next call to mbedtls_mps_reader_reclaim().
  304. * \return #MBEDTLS_ERR_MPS_READER_OUT_OF_DATA if there is not enough
  305. * data available to serve the get request. In this case, the
  306. * reader remains intact and in consuming mode, and the consumer
  307. * should retry the call after a successful cycle of
  308. * mbedtls_mps_reader_reclaim() and mbedtls_mps_reader_feed().
  309. * If, after such a cycle, the consumer requests a different
  310. * amount of data, the result is implementation-defined;
  311. * progress is guaranteed only if the same amount of data
  312. * is requested after a mbedtls_mps_reader_reclaim() and
  313. * mbedtls_mps_reader_feed() cycle.
  314. * \return Another negative \c MBEDTLS_ERR_READER_XXX error
  315. * code for different kinds of failure.
  316. *
  317. * \note Passing \c NULL as \p buflen is a convenient way to
  318. * indicate that fragmentation is not tolerated.
  319. * It's functionally equivalent to passing a valid
  320. * address as buflen and checking \c *buflen == \c desired
  321. * afterwards.
  322. */
  323. int mbedtls_mps_reader_get(mbedtls_mps_reader *reader,
  324. mbedtls_mps_size_t desired,
  325. unsigned char **buffer,
  326. mbedtls_mps_size_t *buflen);
  327. /**
  328. * \brief Mark data obtained from mbedtls_mps_reader_get() as processed.
  329. *
  330. * This call indicates that all data received from prior calls to
  331. * mbedtls_mps_reader_get() has been or will have been
  332. * processed when mbedtls_mps_reader_reclaim() is called,
  333. * and thus need not be backed up.
  334. *
  335. * This function has no user observable effect until
  336. * mbedtls_mps_reader_reclaim() is called. In particular,
  337. * buffers received from mbedtls_mps_reader_get() remain
  338. * valid until mbedtls_mps_reader_reclaim() is called.
  339. *
  340. * \param reader The reader context to use.
  341. *
  342. * \return \c 0 on success.
  343. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  344. *
  345. */
  346. int mbedtls_mps_reader_commit(mbedtls_mps_reader *reader);
  347. #endif /* MBEDTLS_READER_H */