Skip to content

Commit 4610fac

Browse files
authored
Initial work to allow but not require dynamic sized data (#84)
* Better control of primary block lifecycle * Using BSL_Data more consistently to avoid frontend QCBOR dependency
1 parent 40957f9 commit 4610fac

24 files changed

Lines changed: 349 additions & 283 deletions

docs/api/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ decrypt
7878
DECRYPT
7979
decrypted
8080
DefaultSecuritContext's
81+
deallocated
8182
deinit
8283
Deinit
8384
deinitialize
8485
Deinitializes
86+
dereference
8587
dir
8688
DocID
8789
doxygen

src/BPSecLib_Private.h

Lines changed: 11 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <syslog.h>
4444
#include <time.h>
4545
#include <sys/types.h>
46-
#include <qcbor/UsefulBuf.h>
4746

4847
#include "BPSecLib_Public.h"
4948

@@ -289,105 +288,8 @@ void BSL_LogEvent(int severity, const char *filename, int lineno, const char *fu
289288

290289
#define ASSERT_POSTCONDITION(expr) ASSERT_TEMPL(expr, "Panic: Precondition failed to satisfy")
291290

292-
// TODO(Bvb): These can be moved to backend, or removed.
293-
/// Data pointer for BSL_Data_t
294-
typedef uint8_t *BSL_DataPtr_t;
295-
/// Pointer to constant data for BSL_Data_t
296-
typedef const uint8_t *BSL_DataConstPtr_t;
297-
298-
/** Heap data storage and views.
299-
*/
300-
typedef struct BSL_Data_s
301-
{
302-
/// @brief True if this data is a copy
303-
bool owned;
304-
/// @brief Pointer to the front of the buffer
305-
BSL_DataPtr_t ptr;
306-
/// @brief Size of the data buffer
307-
size_t len;
308-
} BSL_Data_t;
309-
310-
/** Static initializer for a data store.
311-
* @sa BSL_Data_Init()
312-
*/
313-
#define BSL_DATA_INIT_NULL \
314-
{ \
315-
.owned = false, .ptr = NULL, .len = 0 \
316-
}
317-
318-
/**
319-
* Return size of library context
320-
*/
321-
size_t BSL_LibCtx_Sizeof(void);
322-
323-
/** Initialize an empty data struct.
324-
*
325-
* @param[in,out] data The data to initialize, which must not be NULL.
326-
* @return Zero upon success.
327-
* @sa BSL_DATA_INIT_NULL
328-
*/
329-
int BSL_Data_Init(BSL_Data_t *data);
330-
331-
/** Initialize with an owned buffer of size bytelen
332-
*
333-
* @todo Clarify to indicate this calls MALLOC.
334-
*
335-
* @param[in,out] data The data to initialize.
336-
* @param[in] bytelen Length of buffer to allocate.
337-
* @return Zero upon success.
338-
*/
339-
int BSL_Data_InitBuffer(BSL_Data_t *data, size_t bytelen);
340-
341-
/** Initialize a data struct as an overlay on optional external data.
342-
*
343-
* @param[in,out] data The data to initialize, which must not be NULL.
344-
* @param[in] len The total length to allocate, which may be zero.
345-
* @param[in] src An optional source buffer to point to.
346-
* @return Zero upon success.
347-
*/
348-
int BSL_Data_InitView(BSL_Data_t *data, size_t len, BSL_DataPtr_t src);
349-
350-
/// @overload
351-
void BSL_Data_InitMove(BSL_Data_t *data, BSL_Data_t *src);
352-
353-
/** De-initialize a data struct, freeing if necessary.
354-
*
355-
* @param[in,out] data The data to de-initialize, which must not be NULL.
356-
* @return Zero upon success.
357-
* @post The struct must be initialized before using again.
358-
*/
359-
int BSL_Data_Deinit(BSL_Data_t *data);
360-
361-
/** Resize the data, copying if necessary.
362-
*
363-
* @param[in,out] data The data to resize, which must not be NULL.
364-
* @param[in] len The new total size.
365-
* @return Zero upon success.
366-
*/
367-
int BSL_Data_Resize(BSL_Data_t *data, size_t len);
368-
369-
/** Set an initialized data struct to a given size.
370-
*
371-
* @param[in,out] data The data to copy into, which must not be NULL.
372-
* @param[in] len The total length to allocate, which may be non-zero.
373-
* @param[in] src An optional source buffer to copy from, from which @c len
374-
* bytes will be copied.
375-
* @return Zero upon success.
376-
*/
377-
int BSL_Data_CopyFrom(BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src);
378-
379-
/** Append an initialized data struct with a given size.
380-
*
381-
* @param[in,out] data The data to copy into, which must not be NULL.
382-
* @param[in] len The total length to allocate, which may be non-zero.
383-
* @param[in] src An optional source buffer to copy from, from which @c len
384-
* bytes will be copied.
385-
* @return Zero upon success.
386-
*/
387-
int BSL_Data_AppendFrom(BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src);
388-
389291
/// @brief Forward declaration for file-like sequential reader.
390-
typedef struct BSL_SeqReader BSL_SeqReader_t;
292+
typedef struct BSL_SeqReader_s BSL_SeqReader_t;
391293

392294
/** Release resources from a sequential reader.
393295
*
@@ -407,7 +309,7 @@ int BSL_SeqReader_Deinit(BSL_SeqReader_t *obj);
407309
int BSL_SeqReader_Get(BSL_SeqReader_t *obj, uint8_t *buf, size_t *bufsize);
408310

409311
/// @brief Forward-declaration for file-like interface for a sequential writer.
410-
typedef struct BSL_SeqWriter BSL_SeqWriter_t;
312+
typedef struct BSL_SeqWriter_s BSL_SeqWriter_t;
411313

412314
/** Release resources from a sequential writer.
413315
*
@@ -582,17 +484,6 @@ typedef enum
582484
*/
583485
int BSL_BundleCtx_GetBundleMetadata(const BSL_BundleRef_t *bundle, BSL_PrimaryBlock_t *result_primary_block);
584486

585-
/** @brief Returns an array in which each element contains the id of the corresponding block.abort
586-
*
587-
* @param[in] bundle Bundle context
588-
* @param[in] array_count Number of elements in `block_id_index_array`
589-
* @param[out] block_id_index_array Array of `array_count` elements for results
590-
* @param[out] result_count Contains the number of elements put into the array
591-
* @return 0 on success, negative on error
592-
*/
593-
int BSL_BundleCtx_GetBlockIds(const BSL_BundleRef_t *bundle, size_t array_count, uint64_t *block_ids_array,
594-
size_t *result_count);
595-
596487
/** @brief Returns information about the bundle Canonical block
597488
*
598489
* @param[in] bundle Context bundle
@@ -638,6 +529,10 @@ int BSL_BundleCtx_DeleteBundle(BSL_BundleRef_t *bundle);
638529
*/
639530
int BSL_BundleCtx_ReallocBTSD(BSL_BundleRef_t *bundle, uint64_t block_num, size_t bytesize);
640531

532+
BSL_SeqReader_t *BSL_BundleCtx_ReadBTSD(BSL_BundleRef_t *bundle, uint64_t block_num);
533+
534+
BSL_SeqWriter_t *BSL_BundleCtx_WriteBTSD(BSL_BundleRef_t *bundle, uint64_t block_num);
535+
641536
#define BSL_DEFAULT_BYTESTR_LEN (128)
642537

643538
/** @brief Security role of an operation
@@ -678,7 +573,7 @@ typedef struct BSL_SecResult_s BSL_SecResult_t;
678573
* @return 0 on success, negative on error
679574
*/
680575
int BSL_SecResult_Init(BSL_SecResult_t *self, uint64_t result_id, uint64_t context_id, uint64_t target_block_num,
681-
BSL_Data_t content);
576+
const BSL_Data_t *content);
682577

683578
/** Return true when internal invariant checks pass
684579
*
@@ -1022,19 +917,19 @@ int BSL_AbsSecBlock_StripResults(BSL_AbsSecBlock_t *self, uint64_t target_block_
1022917
*
1023918
* @param[in] self This ASB.
1024919
* @param[in] buf A buffer with allocated space for the encoded CBOR
1025-
* or the @c SizeCalculateUsefulBuf value to get the real size.
920+
* or a zero-length buffer to calculate the needed size.
1026921
* @return Integer contains number of bytes written to buffer, negative indicates error.
1027922
*
1028923
*/
1029-
ssize_t BSL_AbsSecBlock_EncodeToCBOR(const BSL_AbsSecBlock_t *self, UsefulBuf buf);
924+
ssize_t BSL_AbsSecBlock_EncodeToCBOR(const BSL_AbsSecBlock_t *self, BSL_Data_t *buf);
1030925

1031926
/** Decodes and populates this ASB from a CBOR string.
1032927
*
1033928
* @param[in,out] self This allocated, but uninitialized ASB to populate.
1034-
* @param[in] encoded_cbor A buffer containing a CBOR string representing the ASB
929+
* @param[in] buf A buffer containing a CBOR string representing the ASB
1035930
* @return Negative on error
1036931
*/
1037-
int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, const BSL_Data_t *encoded_cbor);
932+
int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, const BSL_Data_t *buf);
1038933

1039934
/** @brief Represents the output following execution of a security operation.
1040935
*/

src/BPSecLib_Public.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <stdint.h>
3737

3838
#include "BSLConfig.h"
39+
#include "Data.h"
3940

4041
#ifdef __cplusplus
4142
extern "C" {
@@ -51,6 +52,11 @@ extern "C" {
5152
/// Forward declaration for BSL library context.
5253
typedef struct BSL_LibCtx_s BSL_LibCtx_t;
5354

55+
/**
56+
* Return size of library context
57+
*/
58+
size_t BSL_LibCtx_Sizeof(void);
59+
5460
/// @brief Forward declaration of ::BSL_SecurityResponseSet_s, which contains information for BSL and the host BPA to
5561
/// process the Bundle.
5662
typedef struct BSL_SecurityResponseSet_s BSL_SecurityResponseSet_t;
@@ -134,7 +140,7 @@ typedef struct BSL_HostEIDPattern_s
134140

135141
/** @brief Reference to a Bundle owned and stored in the host BPA
136142
*
137-
* @note The BSL internally never attempts to parse the opaque pointer contained here.
143+
* @note The BSL internally never attempts to dereference the opaque pointer contained here.
138144
*/
139145
typedef struct BSL_BundleRef_s
140146
{
@@ -144,6 +150,9 @@ typedef struct BSL_BundleRef_s
144150
/** @brief Contains Bundle Primary Block fields and metadata.
145151
*
146152
* @note This contains a *snapshot* of the fields at the time it was queried. It is not a pointer.
153+
*
154+
* Instances are initialized as part of BSL_BundleCtx_GetBundleMetadata().
155+
* Instances are de-initialized with BSL_PrimaryBlock_deinit().
147156
*/
148157
typedef struct BSL_PrimaryBlock_s
149158
{
@@ -158,11 +167,25 @@ typedef struct BSL_PrimaryBlock_s
158167
uint64_t field_lifetime; ///< CBOR-decoded lifetime
159168
uint64_t field_frag_offset; ///< CBOR-decoded fragment offset (warning, may not be implemented yet).
160169
uint64_t field_adu_length; ///< CBOR-decoded field of ADU length
161-
size_t block_count; ///< Helpful count of total canonical blocks in bundle, not a field of the header.
162-
uint8_t *cbor;
163-
size_t cbor_len;
170+
171+
/// Helpful count of total canonical blocks in bundle, not a field of the header.
172+
size_t block_count;
173+
/** Array of size #block_count containing canonical block numbers in
174+
* the same order in which they appear in the bundle.
175+
*/
176+
uint64_t *block_numbers;
177+
178+
/** The encoded form of the primary block as contiguous data.
179+
*/
180+
BSL_Data_t encoded;
164181
} BSL_PrimaryBlock_t;
165182

183+
/** Deinitialize the use of a primary block metadata.
184+
*
185+
* @param[in,out] obj The instance to deinit.
186+
*/
187+
void BSL_PrimaryBlock_deinit(BSL_PrimaryBlock_t *obj);
188+
166189
/** @brief Structure containing parsed Canonical Block fields.
167190
*
168191
* @note This contains a *snapshot* of the fields at the time it was queried. It is not a pointer.
@@ -172,7 +195,7 @@ typedef struct BSL_CanonicalBlock_s
172195
uint64_t block_num; ///< CBOR-decoded block number (should always be > 0)
173196
uint64_t type_code; ///< CBOR-decoded block type code (should be > 0)
174197
uint64_t flags; ///< CBOR-decoded flags field
175-
uint64_t crc; ///< CBOR-decoded block CRC
198+
uint64_t crc_type; ///< CBOR-decoded block CRC Type
176199
void *btsd; ///< Pointer to BTSD owned by the host BPA
177200
size_t btsd_len; ///< Length in bytes of the BTSD pointer.
178201
} BSL_CanonicalBlock_t;
@@ -196,10 +219,6 @@ typedef struct
196219
/// @brief Host BPA function to populate a Primary Block struct.
197220
int (*bundle_metadata_fn)(const BSL_BundleRef_t *bundle_ref, BSL_PrimaryBlock_t *result_primary_block);
198221

199-
/// @brief Host BPA function to populate a pre-allocated array with canonical block IDs
200-
int (*bundle_get_block_ids)(const BSL_BundleRef_t *bundle_ref, size_t array_count, uint64_t *array_block_ids,
201-
size_t *result_count);
202-
203222
/// @brief Host BPA function to populate a Canonical Block struct for a given block number.
204223
int (*block_metadata_fn)(const BSL_BundleRef_t *bundle_ref, uint64_t block_num, BSL_CanonicalBlock_t *result_block);
205224

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ configure_file(BSLConfig.c.in BSLConfig.c @ONLY)
2525

2626
# Frontend library
2727
set(BSL_FRONT_H
28+
${CMAKE_CURRENT_BINARY_DIR}/BSLConfig.h
29+
${CMAKE_CURRENT_SOURCE_DIR}/Data.h
2830
${CMAKE_CURRENT_SOURCE_DIR}/BPSecLib_Private.h
2931
${CMAKE_CURRENT_SOURCE_DIR}/BPSecLib_Public.h
30-
${CMAKE_CURRENT_BINARY_DIR}/BSLConfig.h
3132
${CMAKE_CURRENT_SOURCE_DIR}/CryptoInterface.h
3233
)
3334

3435
set(BSL_FRONT_C
3536
${CMAKE_CURRENT_BINARY_DIR}/BSLConfig.c
37+
${CMAKE_CURRENT_SOURCE_DIR}/Data.c
3638
)
3739

3840
add_library(bsl_front)
3941
target_sources(bsl_front PUBLIC ${BSL_FRONT_H})
4042
target_sources(bsl_front PRIVATE ${BSL_FRONT_C})
41-
target_link_libraries(bsl_front PUBLIC QCBOR::qcbor)
4243

4344
set_target_properties(bsl_front
4445
PROPERTIES
@@ -120,7 +121,6 @@ set(BSL_DYNAMIC_H
120121

121122
set(BSL_DYNAMIC_C
122123
${CMAKE_CURRENT_SOURCE_DIR}/backend/AbsSecBlock.c
123-
${CMAKE_CURRENT_SOURCE_DIR}/backend/UtilDefs_Data.c
124124
${CMAKE_CURRENT_SOURCE_DIR}/backend/HostInterface.c
125125
${CMAKE_CURRENT_SOURCE_DIR}/backend/PublicInterfaceImpl.c
126126
${CMAKE_CURRENT_SOURCE_DIR}/backend/LoggingStderr.c
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@
2020
* subcontract 1700763.
2121
*/
2222
/** @file
23-
* @ingroup backend_dyn
23+
* @ingroup frontend
2424
* Implementation of the data containers for handling variable-sized buffers and ownership.
2525
*/
26+
#include "Data.h"
27+
#include "BPSecLib_Private.h"
2628
#include <string.h>
2729

28-
#include <BPSecLib_Private.h>
29-
3030
static void bsl_data_int_reset(BSL_Data_t *data)
3131
{
3232
ASSERT_ARG_NONNULL(data);
3333

3434
data->owned = false;
3535
data->ptr = NULL;
3636
data->len = 0;
37-
memset(data, 0, sizeof(*data));
3837
}
3938

4039
static void bsl_data_int_free(BSL_Data_t *data)

0 commit comments

Comments
 (0)