mps_reader.c 20 KB

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