mps_reader.h 17 KB

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