diff options
author | Maulik Patel <Maulik.Patel@arm.com> | 2021-07-21 16:05:42 +0100 |
---|---|---|
committer | Maulik Patel <Maulik.Patel@arm.com> | 2021-09-06 13:26:49 +0100 |
commit | e1c191360c709a5de6f148966a5db5b501e9e6de (patch) | |
tree | a8108d327b814e916f61be02650652d6319b578d /lib | |
parent | 12f2587d39cd87c07d49cb77dfd9ec0bb3bdcf38 (diff) | |
download | trusted-firmware-m-e1c191360c709a5de6f148966a5db5b501e9e6de.tar.gz |
QCBOR: Moving QCBOR util to attest test suites
QCBOR util files in tf-m/lib/ext/qcbor/util are not used in tf-m and
are only used by the attestation test suite. Hence moving them to
appropriate location.
Also, updating the tf-m-tests to a new commit.
Change-Id: I1f658aaf053b6820c62268304d082f1880603e36
Signed-off-by: Maulik Patel <maulik.patel@arm.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ext/qcbor/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/ext/qcbor/util/README.md | 43 | ||||
-rw-r--r-- | lib/ext/qcbor/util/qcbor_util.c | 266 | ||||
-rw-r--r-- | lib/ext/qcbor/util/qcbor_util.h | 235 |
4 files changed, 0 insertions, 546 deletions
diff --git a/lib/ext/qcbor/CMakeLists.txt b/lib/ext/qcbor/CMakeLists.txt index 10c891d522..b32cdea3ac 100644 --- a/lib/ext/qcbor/CMakeLists.txt +++ b/lib/ext/qcbor/CMakeLists.txt @@ -15,13 +15,11 @@ target_sources(tfm_qcbor src/qcbor_encode.c src/qcbor_decode.c src/UsefulBuf.c - util/qcbor_util.c ) target_include_directories(tfm_qcbor PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/inc> - $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/util> ) target_link_libraries(tfm_qcbor diff --git a/lib/ext/qcbor/util/README.md b/lib/ext/qcbor/util/README.md deleted file mode 100644 index 7e41dd8b83..0000000000 --- a/lib/ext/qcbor/util/README.md +++ /dev/null @@ -1,43 +0,0 @@ -# Comments on qcbor_util - -These utilities are shared by t_cose and attest_token. They are -are just part of test code for attestation. This is logically -part of neither t_cose or attest_token so it doesn't -belog in either. - -Eventually it is likely to be an official part of QCBOR, but -it is not complete enough for that yet. It works fine -for what it is, but it is only enough for the needs -of t_cose and attest_token. It needs to be more to be -part of QCBOR. - -So the copy here is part of Arm TF-M software and -not part of QCBOR even though it is in QCBOR -directory. This is a convenient place for it. - -# Copyright for this README - -Copyright 2019, Laurence Lundblade - -* Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -* Neither the name of the copyright holder nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/lib/ext/qcbor/util/qcbor_util.c b/lib/ext/qcbor/util/qcbor_util.c deleted file mode 100644 index 8d0930ec75..0000000000 --- a/lib/ext/qcbor/util/qcbor_util.c +++ /dev/null @@ -1,266 +0,0 @@ -/* - * qcbor_util.c - * - * Copyright (c) 2019, Laurence Lundblade. - * - * SPDX-License-Identifier: BSD-3-Clause - * - * See BSD-3-Clause license in README.md - */ - -#include "qcbor_util.h" - - -/* - * Public function. See qcbor_util.h - */ -QCBORError -qcbor_util_consume_item(QCBORDecodeContext *decode_context, - const QCBORItem *item_to_consume, - uint_fast8_t *next_nest_level) -{ - QCBORError return_value; - QCBORItem item; - - if(item_to_consume->uDataType == QCBOR_TYPE_MAP || - item_to_consume->uDataType == QCBOR_TYPE_ARRAY) { - /* There is only real work to do for maps and arrays */ - - /* This works for definite and indefinite length - * maps and arrays by using the nesting level - */ - do { - return_value = QCBORDecode_GetNext(decode_context, &item); - if(return_value != QCBOR_SUCCESS) { - goto Done; - } - } while(item.uNextNestLevel >= item_to_consume->uNextNestLevel); - - if(next_nest_level != NULL) { - *next_nest_level = item.uNextNestLevel; - } - return_value = QCBOR_SUCCESS; - - } else { - /* item_to_consume is not a map or array */ - if(next_nest_level != NULL) { - /* Just pass the nesting level through */ - *next_nest_level = item_to_consume->uNextNestLevel; - } - return_value = QCBOR_SUCCESS; - } - -Done: - return return_value; -} - - -/* - * Public function. qcbor_util.h - */ -enum attest_token_err_t -qcbor_util_get_items_in_map(QCBORDecodeContext *decode_context, - struct qcbor_util_items_to_get_t *items_found) -{ - QCBORItem item; - struct qcbor_util_items_to_get_t *iterator; - enum attest_token_err_t return_value; - uint_fast8_t map_nest_level; - uint_fast8_t next_nest_level; - - /* Clear structure holding the items found */ - for(iterator = items_found; iterator->label != 0; iterator++) { - iterator->item.uDataType = QCBOR_TYPE_NONE; - } - - /* Get the data item that is the map that is being searched */ - QCBORDecode_GetNext(decode_context, &item); - if(item.uDataType != QCBOR_TYPE_MAP) { - return_value = ATTEST_TOKEN_ERR_CBOR_STRUCTURE; - goto Done; - } - - /* Loop over all the items in the map. The map may contain further - * maps and arrays. This also needs to handle definite and - * indefinite length maps and array. - * - * map_nest_level is the nesting level of the data item opening - * the map that is being scanned. All data items inside this map - * have a nesting level greater than it. The data item following - * the map being scanned has a nesting level that is equal to or - * higher than map_nest_level. - */ - map_nest_level = item.uNestingLevel; - next_nest_level = item.uNextNestLevel; - - while(next_nest_level > map_nest_level) { - if(QCBORDecode_GetNext(decode_context, &item) != QCBOR_SUCCESS) { - /* Got non-well-formed CBOR */ - return_value = ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED; - goto Done; - } - - /* Only look at labels that are integers */ - if(item.uLabelType == QCBOR_TYPE_INT64) { - /* See if it is one we are looking for */ - for(iterator = items_found; iterator->label != 0; iterator++) { - if(item.label.int64 == iterator->label) { - /* It is one we are looking for. Record it. - * This was the point of the whole loop! */ - iterator->item = item; - } - } - } - - /* Only looking at top-level data items, so just consume any - * map or array encountered.*/ - if(qcbor_util_consume_item(decode_context, &item, &next_nest_level)) { - return_value = ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED; - goto Done; - } - } - return_value = ATTEST_TOKEN_ERR_SUCCESS; - -Done: - return return_value; -} - - -/* - * Public function. See qcbor_util.h - */ -enum attest_token_err_t -qcbor_util_decode_to_labeled_item(QCBORDecodeContext *decode_context, - int32_t label, - QCBORItem *item) -{ - QCBORItem map_item; - enum attest_token_err_t return_value; - - return_value = ATTEST_TOKEN_ERR_SUCCESS; - - QCBORDecode_GetNext(decode_context, &map_item); - if(map_item.uDataType != QCBOR_TYPE_MAP) { - /* Isn't a map */ - return_value = ATTEST_TOKEN_ERR_CBOR_STRUCTURE; - goto Done; - } - - /* Loop over all the items in the map */ - while(1) { - if(QCBORDecode_GetNext(decode_context, item) != QCBOR_SUCCESS) { - /* Got non-well-formed CBOR */ - return_value = ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED; - goto Done; - } - - /* Only look at labels that are integers */ - if(item->uLabelType == QCBOR_TYPE_INT64) { - /* See if it is one we are looking for */ - if(item->label.int64 == label) { - /* This is successful exit from the loop */ - return_value = ATTEST_TOKEN_ERR_SUCCESS; - goto Done; - } - } - - /* Only looking at top-level data items, so just consume any - * map or array encountered */ - if(qcbor_util_consume_item(decode_context, item, NULL)) { - return_value = ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED; - goto Done; - } - - if(item->uNextNestLevel < map_item.uNextNestLevel) { - /* Fetched last item in the map without - * finding what was requested */ - return_value = ATTEST_TOKEN_ERR_NOT_FOUND; - goto Done; - } - } - -Done: - return return_value; -} - - -/* - * Public function. See qcbor_util.h - */ -enum attest_token_err_t -qcbor_util_get_item_in_map(QCBORDecodeContext *decode_context, - int32_t label, - QCBORItem *item) -{ - struct qcbor_util_items_to_get_t one_item[2]; - enum attest_token_err_t return_value; - - one_item[0].label = label; - one_item[1].label = 0; /* Terminator for search list */ - - return_value = qcbor_util_get_items_in_map(decode_context, one_item); - if(return_value) { - goto Done; - } - - if(one_item[0].item.uDataType == QCBOR_TYPE_NONE) { - return_value = ATTEST_TOKEN_ERR_NOT_FOUND; - goto Done; - } - - *item = one_item[0].item; - -Done: - return return_value; -} - - -/* - * Public function. See qcbor_util.h - */ -enum attest_token_err_t -qcbor_util_get_top_level_item_in_map(struct q_useful_buf_c payload, - int32_t label, - uint_fast8_t qcbor_type, - QCBORItem *item) -{ - enum attest_token_err_t return_value; - QCBORItem found_item; - QCBORDecodeContext decode_context; - QCBORError cbor_error; - - if(q_useful_buf_c_is_null(payload)) { - return_value = ATTEST_TOKEN_ERR_COSE_VALIDATION; - goto Done; - } - - QCBORDecode_Init(&decode_context, payload, QCBOR_DECODE_MODE_NORMAL); - - return_value = qcbor_util_get_item_in_map(&decode_context, - label, - &found_item); - if(return_value != ATTEST_TOKEN_ERR_SUCCESS) { - goto Done; - } - - cbor_error = QCBORDecode_Finish(&decode_context); - if(cbor_error != QCBOR_SUCCESS) { - if(cbor_error == QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) { - return_value = ATTEST_TOKEN_ERR_CBOR_STRUCTURE; - } else { - /* This is usually due to extra bytes at the end */ - return_value = ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED; - } - goto Done; - } - - if(found_item.uDataType != qcbor_type) { - return_value = ATTEST_TOKEN_ERR_CBOR_TYPE; - goto Done; - } - *item = found_item; - -Done: - return return_value; -} - diff --git a/lib/ext/qcbor/util/qcbor_util.h b/lib/ext/qcbor/util/qcbor_util.h deleted file mode 100644 index 1ea05b4c80..0000000000 --- a/lib/ext/qcbor/util/qcbor_util.h +++ /dev/null @@ -1,235 +0,0 @@ -/* - * qcbor_util.h - * - * Copyright (c) 2019, Laurence Lundblade. - * - * SPDX-License-Identifier: BSD-3-Clause - * - * See BSD-3-Clause license in README.md - */ - -#ifndef __QCBOR_UTILS_H__ -#define __QCBOR_UTILS_H__ - - -#include "qcbor.h" -#include "q_useful_buf.h" -#include "attest_token.h" /* For error codes */ - -#ifdef __cplusplus -extern "C" { -#endif - -/** - *\file qcbor_util.h - * - * \brief Some utility functions for decoding CBOR with QCBOR. - * - * All functions search only for integer labeled data items. If data - * items labeled otherwise are present, they will be skipped over. - * - * These functions may eventually expand in to a more general and - * useful set of decoding utilities. - * - * \c uint_fast8_t is used for type and nest levels. They are - * 8-bit quantities, but making using uint8_t variables - * and parameters can result in bigger, slower code. - * \c uint_fast8_t is part of \c <stdint.h>. It is not - * used in structures where it is more important to keep - * the size smaller. - */ - - -/** - *\brief Consume a data item, particularly a map or array. - * - * \param[in] decode_context The CBOR context from which to - * consume the map or array. - * \param[in] item_to_consume The item to consume. - * \param[out] next_nest_level The nesting level of the item - * that would be consumed next. - * - * \return A \c QCBORError when there is something wrong with the - * encoded CBOR. - * - * If the \c item_to_consume is not a map or array this does nothing - * but return the \c next_nest_level (which is just copied from \c - * item_to_consume). If it is a map or array all subordinate items - * will be consumed from the \c decode_context. - */ -QCBORError -qcbor_util_consume_item(QCBORDecodeContext *decode_context, - const QCBORItem *item_to_consume, - uint_fast8_t *next_nest_level); - - -/** - * Descriptor for a single labeled item to be retrieved by - * qcbor_util_get_items_in_map(). An array of these is passed to - * qcbor_util_get_items_in_map() terminated by one of these with label - * 0. - */ -struct qcbor_util_items_to_get_t { - /** - * The integer label to search for. List terminated by label 0. - */ - int64_t label; - /** - * Where the retrieved item is returned. Item.uDataType is - * QCBOR_TYPE_NONE if not found - */ - QCBORItem item; -}; - - -/** - * \brief Search a CBOR map for multiple integer-labeled items. - * - * \param[in,out] decode_context The QCBOR decode context to - * consume and look through. - * \param[in,out] items The array of labels to search for - * and the places to return what was - * found. See \ref - * qcbor_util_items_to_get_t. - * - * \retval ATTEST_TOKEN_ERR_CBOR_STRUCTURE - * The next item in the decode context is not a map. - * \retval ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED - * The CBOR is not well-formed. - * \retval ATTEST_TOKEN_ERR_SUCCESS - * Success. This just means the map was searched, not that - * anything was found. The contents of \c items must be - * checked to see what was found. - * - * The next item from \c decode_context must be a map. This is the map - * that will be searched. Only items at the immediate subordinate - * level in the map will be checked for label matches. This will - * consume the all the data items in the map. - * - * Note that this cannot be used for finding maps and arrays in at map - * and decoding them since it consumes them and does not return their - * contents. qcbor_util_decode_to_labeled_item() is more useful for - * this. - * - * This will ignore any data items that do not have integer labels. - */ -enum attest_token_err_t -qcbor_util_get_items_in_map(QCBORDecodeContext *decode_context, - struct qcbor_util_items_to_get_t *items); - - -/** - * \brief Decode a map up to a particular label and stop. - * - * \param[in,out] decode_context The QCBOR decode context to - * consume and look through. - * \param[in] label The label of the item being sought. - * \param[out] item The item that is filled in with the - * when the match is found. - * - * \retval ATTEST_TOKEN_ERR_CBOR_STRUCTURE - * The next item in the decode context is not a map. - * \retval ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED - * The CBOR is not well-formed. - * \retval ATTEST_TOKEN_ERR_SUCCESS - * The labeled item was found. - * \retval ATTEST_TOKEN_ERR_NOT_FOUND - The entire map was consumed without finding \c label. - * - * The next item from \c decode_context must be a map. This is the map - * that will be searched. - - * This will decode consuming data items from \c decode_context until - * the labeled item is found. It will consume all items in any map or - * array encountered before finding \c label. - * - * Typically this is used to decode to the start of a map or array. - * The next data item from \c decode_context will be the first item in - * the map or the array. - * - * This works for any CBOR data type, not just maps or arrays. - * - * This will ignore any data items that do not have integer labels. - **/ -enum attest_token_err_t -qcbor_util_decode_to_labeled_item(QCBORDecodeContext *decode_context, - int32_t label, - QCBORItem *item); - - -/** - * \brief Search a map for one particular integer labeled item. - * - * \param[in,out] decode_context The QCBOR decode context to - * consume and look through. - * \param[in] label The label of the item being sought. - * \param[out] item The item that is filled in when - * the match is found. - * - * \retval ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED - * CBOR was not well-formed - * \retval ATTEST_TOKEN_ERR_CBOR_STRUCTURE - * Starting item on \c decode context was not a map. - * \retval ATTEST_TOKEN_ERR_SUCCESS - * The labeled item was found and returned. - * \retval ATTEST_TOKEN_ERR_NOT_FOUND - * The entire map was consumed without finding \c label. - * - * This is qcbor_util_get_items_in_map() for just one item. - * - * This will consume the whole map. It is most useful for getting - * unstructured data items like integers and strings, but not good for - * arrays and maps. See qcbor_util_decode_to_labeled_item() for arrays - * and maps. - */ -enum attest_token_err_t -qcbor_util_get_item_in_map(QCBORDecodeContext *decode_context, - int32_t label, - QCBORItem *item); - - -/** - * \brief Search encoded CBOR from beginning to end for labeled item. - * - * \param[in] payload Encoded chunk of CBOR to decode. - * \param[in] label Integer label of item to look for. - * \param[in] qcbor_type One of \c QCBOR_TYPE_xxx indicating the - * type of the data item expected. - * \param[out] item Place to copy the \c QCBORItem to that - * describes what was found. - * - * \retval ATTEST_TOKEN_ERR_CBOR_TYPE - * The labeled item was found, but it didn't match \c qcbor_type. - * \retval ATTEST_TOKEN_ERR_SUCCESS - * The labeled item was found and returned. - * \retval ATTEST_TOKEN_ERR_CBOR_NOT_WELL_FORMED - * CBOR was not well-formed - * \retval ATTEST_TOKEN_ERR_CBOR_STRUCTURE - * A map was expected. - * \retval ATTEST_TOKEN_ERR_NOT_FOUND - * The entire map was consumed without finding \c label. - * - * This will decode the \c payload from beginning to end. If there are - * extra bytes at the end of it or all the maps and arrays in it are - * not closed this will return an error. - * - * Since this decodes the payload from start to finish to find one - * item, calling this multiple times to get multiple items will cause - * the payload to be completely decoded multiple times. This is not as - * efficient as qcbor_util_get_items_in_map(), but not that costly - * either. - * - * This uses qcbor_util_get_item_in_map() to do its work. - */ -enum attest_token_err_t -qcbor_util_get_top_level_item_in_map(struct q_useful_buf_c payload, - int32_t label, - uint_fast8_t qcbor_type, - QCBORItem *item); - - -#ifdef __cplusplus -} -#endif - -#endif /* __QCBOR_UTILS_H__ */ |