Skip to content

Commit 369baa7

Browse files
authored
Merge branch 'main' into mockbpa-decoder-edges
2 parents e02f237 + 87c3532 commit 369baa7

33 files changed

Lines changed: 492 additions & 450 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ add_compile_options(
8080
$<$<COMPILE_LANGUAGE:C>:-Wextra>
8181
$<$<COMPILE_LANGUAGE:C>:-Wpedantic>
8282
$<$<COMPILE_LANGUAGE:C>:-Werror>
83+
$<$<COMPILE_LANGUAGE:C>:-Wformat>
8384
-Wshadow -Wpointer-arith -Wstrict-prototypes
8485
-Wmissing-prototypes -Wredundant-decls -Wcast-align
8586
-Wformat=2

mock-bpa-test/_generate_simple_bundles.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,13 @@ def add_bcb_to_bundle_over_x(bundle, x):
152152

153153
b = [
154154
[7, 0, 0, [2, [1, 2]], [2, [2, 1]], [2, [2, 1]], [0, 40], 1000000],
155-
[1, 1, 0, 0, '526561647920746F2067656E657261746520612033322D62797465207061796C6F6164']
155+
[1, 1, 0, 0, '526561647920746F2067656E657261746520612033322D62797465207061796C6F6164'],
156+
[192, 2, 0, 0, '676f20647261676f6e666c7921']
156157
]
157158

158159

159160
print (f"ORIGINAL BUNDLE: {b}")
160-
b = add_bib_to_bundle_over_x(b, 0)
161+
b = add_bib_to_bundle_over_x(b, 2)
161162
print(f'BUNDLE AFTER BIB: {b}')
162163
#b = add_bcb_to_bundle_over_x(b, 1)
163164
print(f'FINAL BUNDLE: {b}')

mock-bpa-test/_test_util.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,21 @@ class DataFormat(Enum):
3030
# "structure" to hold a simple test case
3131
class _TestCase:
3232
'''
33-
@param input_data list representation of bundle | TODO hex option / fully hex?
34-
@param expected_output either list representation of expected output bundle OR tuple for outcome (FAILURE_CODE, N), (NO_OUTPUT, N), etc.
35-
@param policy_config decimal digit representing uint32 for policy configuration | TODO switch to a hex string?
36-
@param impl - boolean, true if test is implemented, false if not (placeholder for empty test fixtures)
37-
@param success - boolean, true if input bundle is expected to have an output bundle, false if error/no output
33+
@param input_data: list representation of bundle
34+
@param expected_output: either list representation of expected output bundle OR a string to search log output for match
35+
@param policy_config: decimal digit representing uint32 for policy configuration
36+
@param is_working: True if test working
37+
@param input/output_data_format: data format of input/output
3838
'''
39-
def __init__(self, input_data, expected_output, policy_config,
40-
is_implemented : bool, is_working: bool, expect_success: bool,
39+
def __init__(self, input_data, expected_output, policy_config, is_working: bool,
4140
input_data_format : DataFormat, expected_output_format : DataFormat):
4241
self.input_data = input_data
4342
self.expected_output = expected_output
4443
self.policy_config = policy_config
4544

46-
# can be removed once all tests are implemeneted
47-
self.is_implemented = is_implemented
48-
4945
# can be removed once all tests are wworking
5046
self.is_working = is_working
5147

52-
# true if test expected to succeed (return output bundle with no errors)
53-
self.expect_success = expect_success
54-
5548
self.input_data_format = input_data_format
5649
self.expected_output_format = expected_output_format
5750

mock-bpa-test/requirements_tests.py

Lines changed: 58 additions & 91 deletions
Large diffs are not rendered by default.

mock-bpa-test/test_bpa.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ def _single_test(self, testcase: _TestCase):
173173

174174
LOGGER.warning('Check log output to validate reason for no data!!')
175175

176-
# Currently hard-coded for test case 19 but no other instances of DataFormat.NONE
177-
case_19_str = r".*Delete bundle due to failed security operation"
176+
output_str = testcase.expected_output
178177

179-
LOGGER.debug("Searching test runner logger for failure string: %s", case_19_str)
180-
found = self._agent.wait_for_text(case_19_str)
178+
LOGGER.debug("Searching test runner logger for failure string: %s", output_str)
179+
found = self._agent.wait_for_text(output_str)
181180
LOGGER.debug("\nFOUND OCCURENCE: %s", found)
182181
self.assertTrue(found != "")
183182

@@ -192,8 +191,7 @@ def _single_test(self, testcase: _TestCase):
192191

193192
LOGGER.warning('Check log output to validate expected error')
194193

195-
# TBD - this logic is not used yet
196-
err_case_str = r"tbd"
194+
err_case_str = testcase.expected_output
197195

198196
LOGGER.debug("Searching test runner logger for error string: %s", err_case_str)
199197
found = self._agent.wait_for_text(err_case_str)
@@ -208,7 +206,7 @@ def _add_tests(new_tests: _TestSet):
208206

209207
def decorator(cls):
210208
for id, tc in new_tests.cases.items():
211-
if tc.is_implemented and tc.is_working:
209+
if tc.is_working:
212210

213211
def _test(cls, id=id):
214212
cls._single_test(new_tests.cases[id])

src/BPSecLib_Private.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ typedef enum
7777
/// Security Context errors start at 200
7878
BSL_ERR_SECURITY_CONTEXT_FAILED = -200, ///< General error code for errors arising from a Security Context.
7979
BSL_ERR_SECURITY_CONTEXT_PARTIAL_FAIL = -201, ///< General code where at least some security operations failed.
80-
BSL_ERR_SECURITY_CONTEXT_VALIDATION_FAILED = -202 ///< Indicates an HMAC signature did not match
80+
BSL_ERR_SECURITY_CONTEXT_VALIDATION_FAILED = -202, ///< Indicates security context validate failed
81+
BSL_ERR_SECURITY_CONTEXT_AUTH_FAILED = -203, ///< Indicates an HMAC Auth failed
82+
BSL_ERR_SECURITY_CONTEXT_CRYPTO_FAILED = -204 ///< Indicates a cryptographic operation failed (encrypt/decrypt)
8183
} BSL_ErrCodes_e;
8284

8385
/** Mark an unused parameter Within a function definition.
@@ -313,6 +315,11 @@ typedef struct BSL_Data_s
313315
.owned = false, .ptr = NULL, .len = 0 \
314316
}
315317

318+
/**
319+
* Return size of library context
320+
*/
321+
size_t BSL_LibCtx_Sizeof(void);
322+
316323
/** Initialize an empty data struct.
317324
*
318325
* @param[in,out] data The data to initialize, which must not be NULL.
@@ -935,7 +942,7 @@ size_t BSL_AbsSecBlock_Sizeof(void);
935942
* @param[in] sec_context_id Security Context ID
936943
* @param[in] source_eid Source EID in format native to host BPA.
937944
*/
938-
void BSL_AbsSecBlock_Init(BSL_AbsSecBlock_t *self, uint64_t sec_context_id, BSL_HostEID_t source_eid);
945+
void BSL_AbsSecBlock_Init(BSL_AbsSecBlock_t *self, int64_t sec_context_id, BSL_HostEID_t source_eid);
939946

940947
/** Checks internal consistency and sanity of this structure.
941948
* @param[in] self This ASB

src/backend/AbsSecBlock.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool BSL_AbsSecBlock_IsConsistent(const BSL_AbsSecBlock_t *self)
3939
{
4040
// NOLINTBEGIN
4141
CHK_AS_BOOL(self != NULL);
42-
CHK_AS_BOOL(self->sec_context_id > 0);
42+
// CHK_AS_BOOL(self->sec_context_id > 0);
4343
CHK_AS_BOOL(self->source_eid.handle != NULL);
4444
CHK_AS_BOOL(BSLB_SecParamList_size(self->params) < 10000);
4545

@@ -55,24 +55,24 @@ bool BSL_AbsSecBlock_IsConsistent(const BSL_AbsSecBlock_t *self)
5555
void BSL_AbsSecBlock_Print(const BSL_AbsSecBlock_t *self)
5656
{
5757
BSL_StaticString_t str;
58-
BSL_LOG_INFO("ASB context id: %lu", self->sec_context_id);
58+
BSL_LOG_INFO("ASB context id: %" PRId64, self->sec_context_id);
5959
for (size_t index = 0; index < uint64_list_size(self->targets); index++)
6060
{
61-
BSL_LOG_INFO("ASB target[%lu]: %lu", index, *uint64_list_cget(self->targets, index));
61+
BSL_LOG_INFO("ASB target[%zu]: %" PRIu64, index, *uint64_list_cget(self->targets, index));
6262
}
6363

6464
for (size_t index = 0; index < BSLB_SecParamList_size(self->params); index++)
6565
{
6666
BSL_SecParam_t *param = BSLB_SecParamList_get(self->params, index);
67-
BSL_LOG_INFO("ASB Param[%lu]: id=%lu val=%lu", index, param->param_id, param->_uint_value);
67+
BSL_LOG_INFO("ASB Param[%zu]: id=%" PRIu64 " val=%" PRIu64, index, param->param_id, param->_uint_value);
6868
}
6969

7070
for (size_t index = 0; index < BSLB_SecResultList_size(self->results); index++)
7171
{
7272
BSL_SecResult_t *sec_result = BSLB_SecResultList_get(self->results, index);
7373
BSL_Log_DumpAsHexString((uint8_t *)str, sizeof(str), sec_result->_bytes, sec_result->_bytelen);
74-
BSL_LOG_INFO("ASB Result[%lu]: tgt=%lu, id=%lu %s", index, sec_result->target_block_num, sec_result->result_id,
75-
str);
74+
BSL_LOG_INFO("ASB Result[%zu]: tgt=%" PRIu64 ", id=%" PRIu64 " %s", index, sec_result->target_block_num,
75+
sec_result->result_id, str);
7676
}
7777
}
7878

@@ -86,7 +86,7 @@ void BSL_AbsSecBlock_InitEmpty(BSL_AbsSecBlock_t *self)
8686
uint64_list_init(self->targets);
8787
}
8888

89-
void BSL_AbsSecBlock_Init(BSL_AbsSecBlock_t *self, uint64_t sec_context_id, BSL_HostEID_t source_eid)
89+
void BSL_AbsSecBlock_Init(BSL_AbsSecBlock_t *self, int64_t sec_context_id, BSL_HostEID_t source_eid)
9090
{
9191
ASSERT_ARG_NONNULL(self);
9292
memset(self, 0, sizeof(*self));
@@ -249,7 +249,7 @@ ssize_t BSL_AbsSecBlock_EncodeToCBOR(const BSL_AbsSecBlock_t *self, UsefulBuf bu
249249
}
250250

251251
{
252-
QCBOREncode_AddUInt64(&encoder, self->sec_context_id);
252+
QCBOREncode_AddInt64(&encoder, self->sec_context_id);
253253
}
254254

255255
{
@@ -347,7 +347,8 @@ int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, BSL_Data_t encoded_c
347347
QCBORDecode_GetUInt64(&asbdec, &tgt_num);
348348
BSL_LOG_DEBUG("got tgt %" PRIu64 "", tgt_num);
349349
uint64_list_push_back(self->targets, tgt_num);
350-
assert(quit++ < 20);
350+
// TODO better error handling
351+
ASSERT_PROPERTY(quit++ < 20);
351352
}
352353
QCBORDecode_ExitArray(&asbdec);
353354

@@ -393,7 +394,7 @@ int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, BSL_Data_t encoded_c
393394
{
394395
uint64_t param_u64_value = 0;
395396
QCBORDecode_GetUInt64(&asbdec, &param_u64_value);
396-
BSL_LOG_DEBUG("ASB: Parsed Param[%lu] = %lu", item_id, param_u64_value);
397+
BSL_LOG_DEBUG("ASB: Parsed Param[%" PRIu64 "] = %" PRIu64, item_id, param_u64_value);
397398
BSL_SecParam_t param;
398399
BSL_SecParam_InitInt64(&param, item_id, param_u64_value);
399400
BSLB_SecParamList_push_back(self->params, param);
@@ -402,7 +403,7 @@ int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, BSL_Data_t encoded_c
402403
{
403404
UsefulBufC target_buf;
404405
QCBORDecode_GetByteString(&asbdec, &target_buf);
405-
BSL_LOG_DEBUG("ASB: Parsed Param[%lu] (ByteStr) = %lu bytes", item_id, target_buf.len);
406+
BSL_LOG_DEBUG("ASB: Parsed Param[%" PRIu64 "] (ByteStr) = %zu bytes", item_id, target_buf.len);
406407
BSL_SecParam_t param;
407408
BSL_Data_t data_view = { .owned = 0, .ptr = (uint8_t *)target_buf.ptr, .len = target_buf.len };
408409
BSL_SecParam_InitBytestr(&param, item_id, data_view);
@@ -440,7 +441,7 @@ int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, BSL_Data_t encoded_c
440441
}
441442
result_index++;
442443

443-
BSL_LOG_DEBUG("Parsing ASB results for target[index=%lu, block#=%lu]", result_index, target_id);
444+
BSL_LOG_DEBUG("Parsing ASB results for target[index=%zu, block#=%zu]", result_index, target_id);
444445

445446
// variable length array of results
446447
QCBORDecode_EnterArray(&asbdec, NULL);
@@ -465,7 +466,7 @@ int BSL_AbsSecBlock_DecodeFromCBOR(BSL_AbsSecBlock_t *self, BSL_Data_t encoded_c
465466
BSL_SecResult_t result;
466467
int result_code = BSL_SecResult_Init(&result, item_id, self->sec_context_id, target_id, bufdata);
467468
ASSERT_PROPERTY(result_code == 0);
468-
BSL_LOG_DEBUG("ASB: Parsed Result (target_block=%lu, len=%lu)", result.target_block_num,
469+
BSL_LOG_DEBUG("ASB: Parsed Result (target_block=%" PRIu64 ", len=%zu)", result.target_block_num,
469470
result._bytelen);
470471
BSLB_SecResultList_push_back(self->results, result);
471472
}

src/backend/AbsSecBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct BSL_AbsSecBlock_s
6060
uint64_list_t targets;
6161

6262
/// @brief Security context id
63-
uint64_t sec_context_id;
63+
int64_t sec_context_id;
6464

6565
/// @brief Source EID native representation, BSL host must take care of encoding/decoding.
6666
BSL_HostEID_t source_eid;

src/backend/PublicInterfaceImpl.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
#include "SecurityActionSet.h"
3535
#include "SecurityResultSet.h"
3636

37+
size_t BSL_LibCtx_Sizeof(void)
38+
{
39+
return sizeof(BSL_LibCtx_t);
40+
}
41+
3742
int BSL_API_InitLib(BSL_LibCtx_t *lib)
3843
{
3944
CHK_ARG_NONNULL(lib);
@@ -52,7 +57,7 @@ int BSL_API_DeinitLib(BSL_LibCtx_t *lib)
5257
(lib->policy_registry.deinit_fn)(lib->policy_registry.user_data);
5358

5459
// TODO - We should not assume this is dynamically allocated.
55-
free(lib->policy_registry.user_data);
60+
BSL_FREE(lib->policy_registry.user_data);
5661
}
5762
else
5863
{
@@ -126,7 +131,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp
126131
BSL_CanonicalBlock_t block = { 0 };
127132
if (BSL_SUCCESS != BSL_BundleCtx_GetBlockMetadata(bundle, blocks_array[i], &block))
128133
{
129-
BSL_LOG_WARNING("Failed to get block number %lu", blocks_array[i]);
134+
BSL_LOG_WARNING("Failed to get block number %" PRIu64, blocks_array[i]);
130135
continue;
131136
}
132137
BSL_SecActionList_it_t act_it;
@@ -142,7 +147,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp
142147
continue;
143148
}
144149
// Now set it's sec_block
145-
BSL_AbsSecBlock_t *abs_sec_block = calloc(1, BSL_AbsSecBlock_Sizeof());
150+
BSL_AbsSecBlock_t *abs_sec_block = BSL_CALLOC(1, BSL_AbsSecBlock_Sizeof());
146151
BSL_Data_t block_btsd = { 0 };
147152
BSL_Data_InitView(&block_btsd, block.btsd_len, block.btsd);
148153
if (BSL_AbsSecBlock_DecodeFromCBOR(abs_sec_block, block_btsd) == 0)
@@ -157,7 +162,7 @@ int BSL_API_QuerySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityActionSet_t *outp
157162
BSL_LOG_WARNING("Failed to parse ASB from BTSD");
158163
}
159164
BSL_AbsSecBlock_Deinit(abs_sec_block);
160-
free(abs_sec_block);
165+
BSL_FREE(abs_sec_block);
161166
}
162167
}
163168
}
@@ -197,8 +202,6 @@ int BSL_API_ApplySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityResponseSet_t *re
197202
int finalize_status = BSL_PolicyRegistry_FinalizeActions(bsl, policy_actions, bundle, response_output);
198203
BSL_LOG_INFO("Completed finalize: status=%d", finalize_status);
199204

200-
bool must_drop = false;
201-
202205
BSL_SecActionList_it_t act_it;
203206
for (BSL_SecActionList_it(act_it, policy_actions->actions); !BSL_SecActionList_end_p(act_it);
204207
BSL_SecActionList_next(act_it))
@@ -213,7 +216,7 @@ int BSL_API_ApplySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityResponseSet_t *re
213216
// When the operation was a success, there's nothing further to do.
214217
if (conclusion == BSL_SECOP_CONCLUSION_SUCCESS)
215218
{
216-
BSL_LOG_DEBUG("Security operation success, target block num = %lu", sec_oper->target_block_num);
219+
BSL_LOG_DEBUG("Security operation success, target block num = %" PRIu64, sec_oper->target_block_num);
217220
continue;
218221
}
219222

@@ -237,32 +240,22 @@ int BSL_API_ApplySecurity(const BSL_LibCtx_t *bsl, BSL_SecurityResponseSet_t *re
237240
}
238241
case BSL_POLICYACTION_DROP_BUNDLE:
239242
{
240-
BSL_LOG_WARNING("Deleting bundle due to block target num %lu security failure",
243+
BSL_LOG_WARNING("Deleting bundle due to block target num %" PRIu64 " security failure",
241244
sec_oper->target_block_num);
242-
must_drop = true;
245+
// Drop the bundle and return operation error
246+
BSL_LOG_WARNING("***** Delete bundle due to failed security operation *******");
247+
BSL_BundleCtx_DeleteBundle(bundle);
243248
break;
244249
}
245250
case BSL_POLICYACTION_UNDEFINED:
246251
default:
247252
{
248-
BSL_LOG_ERR("Unhandled policy action: %lu", err_action_code);
253+
BSL_LOG_ERR("Unhandled policy action: %" PRIu64, err_action_code);
249254
}
250255
}
251-
252-
if (must_drop)
253-
{
254-
break;
255-
}
256256
}
257257
}
258258

259-
if (must_drop)
260-
{
261-
// Drop the bundle and return operation error
262-
BSL_LOG_WARNING("***** Delete bundle due to failed security operation *******");
263-
BSL_BundleCtx_DeleteBundle(bundle);
264-
}
265-
266259
// TODO CHK_POSTCONDITION
267-
return (must_drop) ? BSL_ERR_SECURITY_OPERATION_FAILED : BSL_SUCCESS;
260+
return BSL_SUCCESS;
268261
}

src/backend/SecOutcome.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ bool BSL_SecOutcome_IsInAbsSecBlock(const BSL_SecOutcome_t *self, const BSL_AbsS
184184
}
185185
else
186186
{
187-
BSL_LOG_ERR("Security operation mismatch - ASB does NOT contain block %lu", actual_res->target_block_num);
187+
BSL_LOG_ERR("Security operation mismatch - ASB does NOT contain block %" PRIu64,
188+
actual_res->target_block_num);
188189
}
189190
}
190-
BSL_LOG_DEBUG("Checking results: %lu expected, %lu found", expected_matches, found_matches);
191+
BSL_LOG_DEBUG("Checking results: %zu expected, %zu found", expected_matches, found_matches);
191192
return (expected_matches == found_matches) && (found_matches > 0);
192193
}

0 commit comments

Comments
 (0)