mps_reader.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. {
  132. unsigned char *frag; /*!< The fragment of incoming data managed by
  133. * the reader; it is provided to the reader
  134. * through mbedtls_mps_reader_feed(). The reader
  135. * does not own the fragment and does not
  136. * perform any allocation operations on it,
  137. * but does have read and write access to it.
  138. *
  139. * The reader is in consuming mode if
  140. * and only if \c frag is not \c NULL. */
  141. mbedtls_mps_stored_size_t frag_len;
  142. /*!< The length of the current fragment.
  143. * Must be 0 if \c frag == \c NULL. */
  144. mbedtls_mps_stored_size_t commit;
  145. /*!< The offset of the last commit, relative
  146. * to the first byte in the fragment, if
  147. * no accumulator is present. If an accumulator
  148. * is present, it is viewed as a prefix to the
  149. * current fragment, and this variable contains
  150. * an offset from the beginning of the accumulator.
  151. *
  152. * This is only used when the reader is in
  153. * consuming mode, i.e. \c frag != \c NULL;
  154. * otherwise, its value is \c 0. */
  155. mbedtls_mps_stored_size_t end;
  156. /*!< The offset of the end of the last chunk
  157. * passed to the user through a call to
  158. * mbedtls_mps_reader_get(), relative to the first
  159. * byte in the fragment, if no accumulator is
  160. * present. If an accumulator is present, it is
  161. * viewed as a prefix to the current fragment, and
  162. * this variable contains an offset from the
  163. * beginning of the accumulator.
  164. *
  165. * This is only used when the reader is in
  166. * consuming mode, i.e. \c frag != \c NULL;
  167. * otherwise, its value is \c 0. */
  168. mbedtls_mps_stored_size_t pending;
  169. /*!< The amount of incoming data missing on the
  170. * last call to mbedtls_mps_reader_get().
  171. * In particular, it is \c 0 if the last call
  172. * was successful.
  173. * If a reader is reclaimed after an
  174. * unsuccessful call to mbedtls_mps_reader_get(),
  175. * this variable is used to have the reader
  176. * remember how much data should be accumulated
  177. * so that the call to mbedtls_mps_reader_get()
  178. * succeeds next time.
  179. * This is only used when the reader is in
  180. * consuming mode, i.e. \c frag != \c NULL;
  181. * otherwise, its value is \c 0. */
  182. /* The accumulator is only needed if we need to be able to pause
  183. * the reader. A few bytes could be saved by moving this to a
  184. * separate struct and using a pointer here. */
  185. unsigned char *acc; /*!< The accumulator is used to gather incoming
  186. * data if a read-request via mbedtls_mps_reader_get()
  187. * cannot be served from the current fragment. */
  188. mbedtls_mps_stored_size_t acc_len;
  189. /*!< The total size of the accumulator. */
  190. mbedtls_mps_stored_size_t acc_available;
  191. /*!< The number of bytes currently gathered in
  192. * the accumulator. This is both used in
  193. * producing and in consuming mode:
  194. * While producing, it is increased until
  195. * it reaches the value of \c acc_remaining below.
  196. * While consuming, it is used to judge if a
  197. * get request can be served from the
  198. * accumulator or not.
  199. * Must not be larger than \c acc_len. */
  200. union
  201. {
  202. mbedtls_mps_stored_size_t acc_remaining;
  203. /*!< This indicates the amount of data still
  204. * to be gathered in the accumulator. It is
  205. * only used in producing mode.
  206. * Must be at most acc_len - acc_available. */
  207. mbedtls_mps_stored_size_t frag_offset;
  208. /*!< If an accumulator is present and in use, this
  209. * field indicates the offset of the current
  210. * fragment from the beginning of the
  211. * accumulator. If no accumulator is present
  212. * or the accumulator is not in use, this is \c 0.
  213. * It is only used in consuming mode.
  214. * Must not be larger than \c acc_available. */
  215. } acc_share;
  216. };
  217. /*
  218. * API organization:
  219. * A reader object is usually prepared and maintained
  220. * by some lower layer and passed for usage to an upper
  221. * layer, and the API naturally splits according to which
  222. * layer is supposed to use the respective functions.
  223. */
  224. /*
  225. * Maintenance API (Lower layer)
  226. */
  227. /**
  228. * \brief Initialize a reader object
  229. *
  230. * \param reader The reader to be initialized.
  231. * \param acc The buffer to be used as a temporary accumulator
  232. * in case get requests through mbedtls_mps_reader_get()
  233. * exceed the buffer provided by mbedtls_mps_reader_feed().
  234. * This buffer is owned by the caller and exclusive use
  235. * for reading and writing is given to the reader for the
  236. * duration of the reader's lifetime. It is thus the caller's
  237. * responsibility to maintain (and not touch) the buffer for
  238. * the lifetime of the reader, and to properly zeroize and
  239. * free the memory after the reader has been destroyed.
  240. * \param acc_len The size in Bytes of \p acc.
  241. *
  242. * \return \c 0 on success.
  243. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  244. */
  245. int mbedtls_mps_reader_init( mbedtls_mps_reader *reader,
  246. unsigned char *acc,
  247. mbedtls_mps_size_t acc_len );
  248. /**
  249. * \brief Free a reader object
  250. *
  251. * \param reader The reader to be freed.
  252. *
  253. * \return \c 0 on success.
  254. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  255. */
  256. int mbedtls_mps_reader_free( mbedtls_mps_reader *reader );
  257. /**
  258. * \brief Pass chunk of data for the reader to manage.
  259. *
  260. * \param reader The reader context to use. The reader must be
  261. * in producing mode.
  262. * \param buf The buffer to be managed by the reader.
  263. * \param buflen The size in Bytes of \p buffer.
  264. *
  265. * \return \c 0 on success. In this case, the reader will be
  266. * moved to consuming mode and obtains read access
  267. * of \p buf until mbedtls_mps_reader_reclaim()
  268. * is called. It is the responsibility of the caller
  269. * to ensure that the \p buf persists and is not changed
  270. * between successful calls to mbedtls_mps_reader_feed()
  271. * and mbedtls_mps_reader_reclaim().
  272. * \return \c MBEDTLS_ERR_MPS_READER_NEED_MORE if more input data is
  273. * required to fulfill a previous request to mbedtls_mps_reader_get().
  274. * In this case, the reader remains in producing mode and
  275. * takes no ownership of the provided buffer (an internal copy
  276. * is made instead).
  277. * \return Another negative \c MBEDTLS_ERR_READER_XXX error code on
  278. * different kinds of failures.
  279. */
  280. int mbedtls_mps_reader_feed( mbedtls_mps_reader *reader,
  281. unsigned char *buf,
  282. mbedtls_mps_size_t buflen );
  283. /**
  284. * \brief Reclaim reader's access to the current input buffer.
  285. *
  286. * \param reader The reader context to use. The reader must be
  287. * in consuming mode.
  288. * \param paused If not \c NULL, the integer at address \p paused will be
  289. * modified to indicate whether the reader has been paused
  290. * (value \c 1) or not (value \c 0). Pausing happens if there
  291. * is uncommitted data and a previous request to
  292. * mbedtls_mps_reader_get() has exceeded the bounds of the
  293. * input buffer.
  294. *
  295. * \return \c 0 on success.
  296. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  297. */
  298. int mbedtls_mps_reader_reclaim( mbedtls_mps_reader *reader,
  299. int *paused );
  300. /*
  301. * Usage API (Upper layer)
  302. */
  303. /**
  304. * \brief Request data from the reader.
  305. *
  306. * \param reader The reader context to use. The reader must
  307. * be in consuming mode.
  308. * \param desired The desired amount of data to be read, in Bytes.
  309. * \param buffer The address to store the buffer pointer in.
  310. * This must not be \c NULL.
  311. * \param buflen The address to store the actual buffer
  312. * length in, or \c NULL.
  313. *
  314. * \return \c 0 on success. In this case, \c *buf holds the
  315. * address of a buffer of size \c *buflen
  316. * (if \c buflen != \c NULL) or \c desired
  317. * (if \c buflen == \c NULL). The user has read access
  318. * to the buffer and guarantee of stability of the data
  319. * until the next call to mbedtls_mps_reader_reclaim().
  320. * \return #MBEDTLS_ERR_MPS_READER_OUT_OF_DATA if there is not enough
  321. * data available to serve the get request. In this case, the
  322. * reader remains intact and in consuming mode, and the consumer
  323. * should retry the call after a successful cycle of
  324. * mbedtls_mps_reader_reclaim() and mbedtls_mps_reader_feed().
  325. * If, after such a cycle, the consumer requests a different
  326. * amount of data, the result is implementation-defined;
  327. * progress is guaranteed only if the same amount of data
  328. * is requested after a mbedtls_mps_reader_reclaim() and
  329. * mbedtls_mps_reader_feed() cycle.
  330. * \return Another negative \c MBEDTLS_ERR_READER_XXX error
  331. * code for different kinds of failure.
  332. *
  333. * \note Passing \c NULL as \p buflen is a convenient way to
  334. * indicate that fragmentation is not tolerated.
  335. * It's functionally equivalent to passing a valid
  336. * address as buflen and checking \c *buflen == \c desired
  337. * afterwards.
  338. */
  339. int mbedtls_mps_reader_get( mbedtls_mps_reader *reader,
  340. mbedtls_mps_size_t desired,
  341. unsigned char **buffer,
  342. mbedtls_mps_size_t *buflen );
  343. /**
  344. * \brief Mark data obtained from mbedtls_mps_reader_get() as processed.
  345. *
  346. * This call indicates that all data received from prior calls to
  347. * mbedtls_mps_reader_get() has been or will have been
  348. * processed when mbedtls_mps_reader_reclaim() is called,
  349. * and thus need not be backed up.
  350. *
  351. * This function has no user observable effect until
  352. * mbedtls_mps_reader_reclaim() is called. In particular,
  353. * buffers received from mbedtls_mps_reader_get() remain
  354. * valid until mbedtls_mps_reader_reclaim() is called.
  355. *
  356. * \param reader The reader context to use.
  357. *
  358. * \return \c 0 on success.
  359. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
  360. *
  361. */
  362. int mbedtls_mps_reader_commit( mbedtls_mps_reader *reader );
  363. #endif /* MBEDTLS_READER_H */