Skip to content

Commit 621ed42

Browse files
authored
BCB Verifier Fix (#155)
* Init, failing * seqwriter NULL * Clean up unit tests * apply format ubuntu * update docs * apply format ubuntu * Adjust function names and add consistency between bib verif/acc and bcb * apply format ubuntu
1 parent 948238e commit 621ed42

8 files changed

Lines changed: 161 additions & 97 deletions

File tree

mock-bpa-test/test_requirements.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,3 +822,26 @@ def test_BSL_49(self):
822822
input_data_format=DataFormat.BUNDLEARRAY,
823823
expected_output_format=DataFormat.BUNDLEARRAY
824824
))
825+
826+
def test_BCB_verifier(self):
827+
self._single_test(_TestCase(
828+
input_data=[
829+
[7, 0, 0, [2, [1, 2]], [2, [2, 1]], [2, [2, 1]], [0, 40], 1000000],
830+
[12, 2, 1, 0, bytes.fromhex(
831+
'8101020182028202018482014c5477656c76653132313231328202018203581869c411276fecddc4780df42c8a2af89296fabf34d7fae7008204008181820150efa4b5ac0108e3816c5606479801bc04')],
832+
[1, 1, 0, 0, bytes.fromhex(
833+
'3a09c1e63fe23a7f66a59c7303837241e070b02619fc59c5214a22f08cd70795e73e9a')]
834+
],
835+
expected_output=[
836+
[7, 0, 0, [2, [1, 2]], [2, [2, 1]], [2, [2, 1]], [0, 40], 1000000],
837+
[12, 2, 1, 0, bytes.fromhex(
838+
'8101020182028202018482014c5477656c76653132313231328202018203581869c411276fecddc4780df42c8a2af89296fabf34d7fae7008204008181820150efa4b5ac0108e3816c5606479801bc04')],
839+
[1, 1, 0, 0, bytes.fromhex(
840+
'3a09c1e63fe23a7f66a59c7303837241e070b02619fc59c5214a22f08cd70795e73e9a')]
841+
],
842+
policy_config='0x165',
843+
key_set="mock-bpa-test/key_set_1.json",
844+
is_working=True,
845+
input_data_format=DataFormat.BUNDLEARRAY,
846+
expected_output_format=DataFormat.BUNDLEARRAY
847+
))

src/BPSecLib_Private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ size_t BSL_SecOutcome_CountParams(const BSL_SecOutcome_t *self);
11131113
*/
11141114
const BSL_SecParam_t *BSL_SecOutcome_GetParamAt(const BSL_SecOutcome_t *self, size_t index);
11151115

1116-
/// @brief Returns true if this (the parameters and results) is contained within the given ASK
1116+
/// @brief Returns true if this (the parameters and results) is contained within the given ASB
11171117
/// @todo Can move to backend
11181118
/// @param[in] self
11191119
/// @param[in] outcome

src/CryptoInterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ int BSL_Cipher_AddData(BSL_Cipher_t *cipher_ctx, BSL_Data_t plaintext, BSL_Data_
284284
* Add data to encrypt or decrypt to the context sequentially
285285
* @param cipher_ctx pointer to context to add data to
286286
* @param[in] reader pointer to sequential reader - input to crypto operation
287-
* @param[in] writer pointer to sequential writer - output of crypto operation
287+
* @param[in] writer pointer to sequential writer (output of crypto operation), or NULL (crypto output will not be
288+
* written)
288289
* @return 0 if successful
289290
*/
290291
int BSL_Cipher_AddSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqReader_t *reader, BSL_SeqWriter_t *writer);

src/backend/SecurityContext.c

Lines changed: 78 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ static int BSL_ExecBIBSource(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *
101101

102102
CHK_PROPERTY(created_block_num > 1);
103103

104-
const int bib_result = (*sec_context_fn)(lib, bundle, sec_oper, outcome);
104+
sec_oper->sec_block_num = created_block_num;
105+
const int bib_result = (*sec_context_fn)(lib, bundle, sec_oper, outcome);
105106
if (bib_result != 0) // || outcome->is_success == false)
106107
{
107108
BSL_LOG_ERR("BIB Source failed!");
@@ -151,8 +152,8 @@ static int BSL_ExecBIBSource(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *
151152
return res;
152153
}
153154

154-
static int BSL_ExecBIBAccept(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle,
155-
BSL_SecOper_t *sec_oper, BSL_SecOutcome_t *outcome)
155+
static int BSL_ExecBIBVerifierAcceptor(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle,
156+
BSL_SecOper_t *sec_oper, BSL_SecOutcome_t *outcome)
156157
{
157158
CHK_ARG_NONNULL(lib);
158159
CHK_ARG_NONNULL(bundle);
@@ -201,76 +202,70 @@ static int BSL_ExecBIBAccept(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *
201202
const int sec_context_result = (*sec_context_fn)(lib, bundle, sec_oper, outcome);
202203
if (sec_context_result != BSL_SUCCESS) // || outcome->is_success == false)
203204
{
204-
BSL_LOG_ERR("BIB Acceptor failed!");
205+
BSL_LOG_ERR("BIB Sec Ctx processing for verifier/acceptor failed!");
205206
BSL_AbsSecBlock_Deinit(&abs_sec_block);
206207
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
207208
return BSL_ERR_SECURITY_OPERATION_FAILED;
208209
}
209210

210-
bool auth_success = BSL_SecOutcome_IsInAbsSecBlock(outcome, &abs_sec_block);
211-
if (!auth_success)
211+
if (!BSL_SecOutcome_IsInAbsSecBlock(outcome, &abs_sec_block))
212212
{
213-
BSL_LOG_ERR("BIB Accepting failed");
213+
BSL_LOG_ERR("ASB Does not contain expeceted sec params and outcomes");
214+
BSL_AbsSecBlock_Deinit(&abs_sec_block);
214215
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
216+
return BSL_ERR_SECURITY_OPERATION_FAILED;
217+
}
218+
219+
// If secop is to verify, processing is complete
220+
if (BSL_SecOper_IsRoleVerifier(sec_oper))
221+
{
222+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_VERIFIER_COUNT, 1);
223+
BSL_AbsSecBlock_Deinit(&abs_sec_block);
224+
return BSL_SUCCESS;
215225
}
216226

217227
// TODO/FIXME - This logic seems to be correct, but should be refactored and simplified.
218228
// There are too many branches/conditionals each with their own return statement.
219229

220-
if (BSL_SecOper_IsRoleAcceptor(sec_oper))
230+
// If secop is to accept, BIB must be removed from bundle
231+
uint64_t target_block_num = BSL_SecOper_GetTargetBlockNum(sec_oper);
232+
int status = BSL_AbsSecBlock_StripResults(&abs_sec_block, target_block_num);
233+
if (status < 0)
221234
{
222-
uint64_t target_block_num = BSL_SecOper_GetTargetBlockNum(sec_oper);
223-
int status = BSL_AbsSecBlock_StripResults(&abs_sec_block, target_block_num);
224-
if (status < 0)
235+
BSL_LOG_ERR("Failure to strip ASB of results");
236+
BSL_AbsSecBlock_Deinit(&abs_sec_block);
237+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
238+
return BSL_ERR_FAILURE;
239+
}
240+
241+
if (BSL_AbsSecBlock_IsEmpty(&abs_sec_block))
242+
{
243+
if (BSL_BundleCtx_RemoveBlock(bundle, sec_blk.block_num) != BSL_SUCCESS)
225244
{
226-
BSL_LOG_ERR("Failure to strip ASB of results");
245+
BSL_LOG_ERR("Failed to remove block when ASB is empty");
227246
BSL_AbsSecBlock_Deinit(&abs_sec_block);
228247
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
229-
return BSL_ERR_FAILURE;
248+
return BSL_ERR_HOST_CALLBACK_FAILED;
230249
}
231-
232-
if (BSL_AbsSecBlock_IsEmpty(&abs_sec_block))
233-
{
234-
if (BSL_BundleCtx_RemoveBlock(bundle, sec_blk.block_num) != BSL_SUCCESS)
235-
{
236-
BSL_LOG_ERR("Failed to remove block when ASB is empty");
237-
BSL_AbsSecBlock_Deinit(&abs_sec_block);
238-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
239-
return BSL_ERR_HOST_CALLBACK_FAILED;
240-
}
241-
}
242-
else
243-
{
244-
int res = Encode_ASB(lib, bundle, sec_blk.block_num, &abs_sec_block);
245-
if (res != BSL_SUCCESS)
246-
{
247-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
248-
return res;
249-
}
250-
}
251-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_ACCEPTOR_COUNT, 1);
252-
}
253-
254-
BSL_AbsSecBlock_Deinit(&abs_sec_block);
255-
256-
// TODO(bvb) Check postconditions that the block actually was removed
257-
if (auth_success)
258-
{
259-
BSL_LOG_INFO("BIB Accept SUCCESS");
260250
}
261251
else
262252
{
263-
BSL_LOG_ERR("BIB Accept FAIL");
264-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
253+
int res = Encode_ASB(lib, bundle, sec_blk.block_num, &abs_sec_block);
254+
if (res != BSL_SUCCESS)
255+
{
256+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
257+
return res;
258+
}
265259
}
260+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_ACCEPTOR_COUNT, 1);
261+
BSL_AbsSecBlock_Deinit(&abs_sec_block);
266262

267-
return auth_success ? BSL_SUCCESS : BSL_ERR_SECURITY_OPERATION_FAILED;
263+
return BSL_SUCCESS;
268264
}
269265

270-
static int BSL_ExecBCBAcceptor(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle,
271-
BSL_SecOper_t *sec_oper, BSL_SecOutcome_t *outcome)
266+
static int BSL_ExecBCBVerifierAcceptor(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle,
267+
BSL_SecOper_t *sec_oper, BSL_SecOutcome_t *outcome)
272268
{
273-
(void)lib;
274269
CHK_ARG_NONNULL(sec_context_fn);
275270
CHK_ARG_NONNULL(bundle);
276271
CHK_ARG_NONNULL(sec_oper);
@@ -319,7 +314,7 @@ static int BSL_ExecBCBAcceptor(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t
319314
BSL_SecParam_t results_as_params[result_count];
320315
for (size_t i = 0; i < result_count; i++)
321316
{
322-
BSL_SecResult_t *result = BSLB_SecResultList_get(abs_sec_block.results, i);
317+
const BSL_SecResult_t *result = BSLB_SecResultList_get(abs_sec_block.results, i);
323318
if (result->target_block_num == sec_oper->target_block_num)
324319
{
325320
BSL_Data_t as_data;
@@ -332,62 +327,61 @@ static int BSL_ExecBCBAcceptor(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t
332327
}
333328

334329
const int sec_context_result = (*sec_context_fn)(lib, bundle, sec_oper, outcome);
335-
if (sec_context_result != BSL_SUCCESS) // || outcome->is_success == false)
330+
if (sec_context_result != BSL_SUCCESS)
336331
{
337-
BSL_LOG_ERR("BCB Acceptor failed!");
332+
BSL_LOG_ERR("BCB Sec Ctx processing for verifier/acceptor failed!");
338333
BSL_AbsSecBlock_Deinit(&abs_sec_block);
339334
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
340335
return BSL_ERR_SECURITY_OPERATION_FAILED;
341336
}
342337

343-
// TODO/FIXME - This logic seems to be correct, but should be refactored and simplified.
344-
// There are too many branches/conditionals each with their own return statement.
338+
// If secop is to verify, processing is complete
339+
if (BSL_SecOper_IsRoleVerifier(sec_oper))
340+
{
341+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_VERIFIER_COUNT, 1);
342+
BSL_AbsSecBlock_Deinit(&abs_sec_block);
343+
return BSL_SUCCESS;
344+
}
345+
346+
// If secop is to accept, BCB must be removed from bundle
347+
uint64_t target_block_num = BSL_SecOper_GetTargetBlockNum(sec_oper);
348+
int status = BSL_AbsSecBlock_StripResults(&abs_sec_block, target_block_num);
349+
if (status < 0)
350+
{
351+
BSL_LOG_ERR("Failure to strip ASB of results");
352+
BSL_AbsSecBlock_Deinit(&abs_sec_block);
353+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
354+
return BSL_ERR_FAILURE;
355+
}
345356

346-
if (BSL_SecOper_IsRoleAcceptor(sec_oper))
357+
if (BSL_AbsSecBlock_IsEmpty(&abs_sec_block))
347358
{
348-
uint64_t target_block_num = BSL_SecOper_GetTargetBlockNum(sec_oper);
349-
int status = BSL_AbsSecBlock_StripResults(&abs_sec_block, target_block_num);
350-
if (status < 0)
359+
if (BSL_BundleCtx_RemoveBlock(bundle, sec_blk.block_num) != BSL_SUCCESS)
351360
{
352-
BSL_LOG_ERR("Failure to strip ASB of results");
361+
BSL_LOG_ERR("Failed to remove block when ASB is empty");
353362
BSL_AbsSecBlock_Deinit(&abs_sec_block);
354363
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
355-
return BSL_ERR_FAILURE;
364+
return BSL_ERR_HOST_CALLBACK_FAILED;
356365
}
357-
358-
if (BSL_AbsSecBlock_IsEmpty(&abs_sec_block))
359-
{
360-
if (BSL_BundleCtx_RemoveBlock(bundle, sec_blk.block_num) != BSL_SUCCESS)
361-
{
362-
BSL_LOG_ERR("Failed to remove block when ASB is empty");
363-
BSL_AbsSecBlock_Deinit(&abs_sec_block);
364-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
365-
return BSL_ERR_HOST_CALLBACK_FAILED;
366-
}
367-
}
368-
else
366+
}
367+
else
368+
{
369+
int res = Encode_ASB(lib, bundle, sec_blk.block_num, &abs_sec_block);
370+
if (res != BSL_SUCCESS)
369371
{
370-
int res = Encode_ASB(lib, bundle, sec_blk.block_num, &abs_sec_block);
371-
if (res != BSL_SUCCESS)
372-
{
373-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
374-
return res;
375-
}
372+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_FAIL_COUNT, 1);
373+
return res;
376374
}
377-
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_ACCEPTOR_COUNT, 1);
378375
}
379-
376+
BSL_TlmCounters_IncrementCounter(lib, BSL_TLM_SECOP_ACCEPTOR_COUNT, 1);
380377
BSL_AbsSecBlock_Deinit(&abs_sec_block);
381378

382-
// TODO(bvb) Check postconditions that the block actually was removed
383379
return BSL_SUCCESS;
384380
}
385381

386382
static int BSL_ExecBCBSource(BSL_SecCtx_Execute_f sec_context_fn, BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle,
387383
BSL_SecOper_t *sec_oper, BSL_SecOutcome_t *outcome)
388384
{
389-
(void)lib;
390-
391385
CHK_ARG_NONNULL(sec_context_fn);
392386
CHK_ARG_NONNULL(bundle);
393387
CHK_ARG_NONNULL(sec_oper);
@@ -497,7 +491,7 @@ int BSL_SecCtx_ExecutePolicyActionSet(BSL_LibCtx_t *lib, BSL_SecurityResponseSet
497491
{
498492
errcode = BSL_SecOper_IsRoleSource(sec_oper) == true
499493
? BSL_ExecBIBSource(sec_ctx->execute, lib, bundle, sec_oper, outcome)
500-
: BSL_ExecBIBAccept(sec_ctx->execute, lib, bundle, sec_oper, outcome);
494+
: BSL_ExecBIBVerifierAcceptor(sec_ctx->execute, lib, bundle, sec_oper, outcome);
501495
}
502496
else
503497
{
@@ -507,7 +501,7 @@ int BSL_SecCtx_ExecutePolicyActionSet(BSL_LibCtx_t *lib, BSL_SecurityResponseSet
507501
}
508502
else
509503
{
510-
errcode = BSL_ExecBCBAcceptor(sec_ctx->execute, lib, bundle, sec_oper, outcome);
504+
errcode = BSL_ExecBCBVerifierAcceptor(sec_ctx->execute, lib, bundle, sec_oper, outcome);
511505
}
512506
}
513507

src/crypto/CryptoInterface.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,11 @@ int BSL_Cipher_AddSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqReader_t *reader, BSL_Seq
548548

549549
key->stats.stats[BSL_CRYPTO_KEYSTATS_BYTES_PROCESSED] += block_size_int;
550550
block_size = (size_t)block_size_int;
551-
BSL_SeqWriter_Put(writer, write_buf, block_size);
551+
552+
if (NULL != writer)
553+
{
554+
BSL_SeqWriter_Put(writer, write_buf, block_size);
555+
}
552556
}
553557

554558
return 0;

src/security_context/BCB_AES_GCM.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,21 @@ static int BSLX_BCB_Decrypt(BSLX_BCB_t *bcb_context)
175175
if (retval == BSL_SUCCESS)
176176
{
177177
btsd_read = BSL_BundleCtx_ReadBTSD(bcb_context->bundle, bcb_context->target_block.block_num);
178-
// output is same size
179-
btsd_write = BSL_BundleCtx_WriteBTSD(bcb_context->bundle, bcb_context->target_block.block_num,
180-
bcb_context->target_block.btsd_len);
181178
if (!btsd_read)
182179
{
183180
BSL_LOG_ERR("Failed to construct reader");
184181
retval = BSL_ERR_HOST_CALLBACK_FAILED;
185182
}
186-
if (!btsd_write)
183+
184+
if (bcb_context->overwrite_btsd)
187185
{
188-
BSL_LOG_ERR("Failed to construct writer");
189-
retval = BSL_ERR_HOST_CALLBACK_FAILED;
186+
btsd_write = BSL_BundleCtx_WriteBTSD(bcb_context->bundle, bcb_context->target_block.block_num,
187+
bcb_context->target_block.btsd_len);
188+
if (!btsd_write)
189+
{
190+
BSL_LOG_ERR("Failed to construct writer");
191+
retval = BSL_ERR_HOST_CALLBACK_FAILED;
192+
}
190193
}
191194
}
192195

@@ -228,7 +231,10 @@ static int BSLX_BCB_Decrypt(BSLX_BCB_t *bcb_context)
228231

229232
// close write after read
230233
BSL_SeqReader_Destroy(btsd_read);
231-
BSL_SeqWriter_Destroy(btsd_write);
234+
if (NULL != btsd_write)
235+
{
236+
BSL_SeqWriter_Destroy(btsd_write);
237+
}
232238

233239
BSL_Data_Deinit(&bcb_context->authtag);
234240
if (bcb_context->keywrap)
@@ -636,6 +642,9 @@ int BSLX_BCB_Execute(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, const BSL_S
636642
return BSL_ERR_SECURITY_CONTEXT_FAILED;
637643
}
638644

645+
// If secop is accpeting BCB, target btsd should be overwritten with resulting plaintext
646+
bcb_context.overwrite_btsd = BSL_SecOper_IsRoleAcceptor(sec_oper);
647+
639648
// Select whether to call the encrypt or decrypt function
640649
int (*crypto_fn)(BSLX_BCB_t *) = BSL_SecOper_IsRoleSource(sec_oper) ? BSLX_BCB_Encrypt : BSLX_BCB_Decrypt;
641650

src/security_context/DefaultSecContext_Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ typedef struct BSLX_BCB_s
111111
bool skip_aad_sec_block;
112112
bool skip_aad_target_block;
113113
bool skip_aad_prim_block;
114+
bool overwrite_btsd;
114115
} BSLX_BCB_t;
115116

116117
int BSLX_BCB_GetParams(const BSL_BundleRef_t *bundle, BSLX_BCB_t *bcb_context, const BSL_SecOper_t *sec_oper);

0 commit comments

Comments
 (0)