-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_BackendSecurityContext.c
More file actions
539 lines (451 loc) · 25.6 KB
/
Copy pathtest_BackendSecurityContext.c
File metadata and controls
539 lines (451 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright (c) 2025 The Johns Hopkins University Applied Physics
* Laboratory LLC.
*
* This file is part of the Bundle Protocol Security Library (BSL).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This work was performed for the Jet Propulsion Laboratory, California
* Institute of Technology, sponsored by the United States Government under
* the prime contract 80NM0018D0004 between the Caltech and NASA under
* subcontract 1700763.
*/
/** @file
*
* @brief Exercises the Security Context front-end interface.
*
* Notes:
* - These tests exercise the security context front-end interface.
* - They are mostly concerned with given bundles, blocks, and PolicyActionSets
* - They test correctness mostly by verifying that operations modify the bundle as intended
* - They are checked against test vectors in Appendex A of RFC9173.
*
* @ingroup unit-tests
*/
#include <stdlib.h>
#include <stdio.h>
#include <unity.h>
#include <BPSecLib_Private.h>
#include <BPSecLib_MockBPA.h>
#include <CryptoInterface.h>
#include <security_context/rfc9173.h>
#include "bsl_test_utils.h"
static BSL_TestContext_t LocalTestCtx;
void suiteSetUp(void)
{
BSL_openlog();
assert(0 == bsl_mock_bpa_init());
}
int suiteTearDown(int failures)
{
bsl_mock_bpa_deinit();
BSL_closelog();
return failures;
}
void setUp(void)
{
setenv("BSL_TEST_LOCAL_IPN_EID", "ipn:2.1", 1);
memset(&LocalTestCtx, 0, sizeof(LocalTestCtx));
TEST_ASSERT_EQUAL(0, BSL_API_InitLib(&LocalTestCtx.bsl));
mock_bpa_ctr_init(&LocalTestCtx.mock_bpa_ctr);
BSL_TestUtils_SetupDefaultSecurityContext(&LocalTestCtx.bsl);
}
void tearDown(void)
{
mock_bpa_ctr_deinit(&LocalTestCtx.mock_bpa_ctr);
BSL_CryptoDeinit();
TEST_ASSERT_EQUAL(0, BSL_API_DeinitLib(&LocalTestCtx.bsl));
}
/**
* @brief Purpose: Creates a BIB block and adds it to the bundle, confirms it matches the test vector in RFC9173
*
* Steps:
* - Get an unsecured bundle with a primary and payload block (From RFC9173)
* - Create a BIB security operation with hard-coded arguments (From RFC9173 A1 ASB)
* - Use the high-level security context interface to apply the security operation
* - Confirm the bundle has the BIB block applied by comparing its encoding to expect in RFC9173.
*
* Notes:
* - Common repeated patterns are in the process of being factored out
* - All values are drawn from RFC9173 Appendix A.
*/
void test_SecurityContext_BIB_Source(void)
{
TEST_ASSERT_EQUAL(
0, BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_original));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
BIBTestContext bib_test_context;
BSL_TestUtils_InitBIB_AppendixA1(&bib_test_context, BSL_SECROLE_SOURCE, RFC9173_EXAMPLE_A1_KEY);
BSL_SecurityActionSet_t *malloced_actionset = BSL_TestUtils_InitMallocBIBActionSet(&bib_test_context);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
TEST_ASSERT_EQUAL(0, BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset));
BSL_CanonicalBlock_t block;
BSL_BundleCtx_GetBlockMetadata(&mock_bpa_ctr->bundle_ref, 2, &block);
bool x = BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bib_abs_sec_block,
(BSL_Data_t) { .len = block.btsd_len, .ptr = block.btsd });
TEST_ASSERT_TRUE(x);
TEST_ASSERT_EQUAL(0, mock_bpa_encode(mock_bpa_ctr));
bool is_expected =
(BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_bib, mock_bpa_ctr->encoded));
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_responseset);
free(malloced_actionset);
TEST_ASSERT_TRUE(is_expected);
}
/**
* @brief Purpose: Tests that running as role VERIFIER passes correctly when the cryptographic material matches.
*
* Steps:
* - Get a BIB secured bundle from RFC9173 Appendix A1.4.
* - Create a BIB-Verify security operation with hard-coded arguments (From RFC9173 A1 ASB)
* - Use the high-level security context interface to create a security outcome.
* - Confirm the bundle's BIB HMAC matches the outcome's HMAC.
*
* Notes:
* - Common repeated patterns are in the process of being factored out
* - All values are drawn from RFC9173 Appendix A.
*/
void test_SecurityContext_BIB_Verifier(void)
{
TEST_ASSERT_EQUAL(0,
BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_bib));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
BIBTestContext bib_test_context;
BSL_TestUtils_InitBIB_AppendixA1(&bib_test_context, BSL_SECROLE_VERIFIER, RFC9173_EXAMPLE_A1_KEY);
BSL_SecurityActionSet_t *malloced_actionset = BSL_TestUtils_InitMallocBIBActionSet(&bib_test_context);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
TEST_ASSERT_EQUAL(0, BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset));
TEST_ASSERT_EQUAL(0, mock_bpa_encode(mock_bpa_ctr));
bool is_match =
(BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_bib, mock_bpa_ctr->encoded));
BSL_SecurityActionSet_Deinit(malloced_actionset);
BSL_SecurityResponseSet_Deinit(malloced_responseset);
free(malloced_actionset);
free(malloced_responseset);
TEST_ASSERT_TRUE(is_match);
}
/**
* @brief Purpose: Test that a BIB verification operation does not pass when the cryptographic material does not match.
*
* Steps:
* - Get a BIB secured bundle from RFC9173 Appendix A1.4.
* - Create a BIB-Verify security operation with hard-coded arguments (From RFC9173 A1 ASB)
* - Manipulate the arguments so they use a different key
* - Use the high-level security context interface to create a security outcome.
* - Confirm that the execution failed (return code != 0)
*
* Notes:
* - Check more than return code, look deeper into outcome.
*/
void test_SecurityContext_BIB_Verifier_Failure(void)
{
// TODO(bvb) Note that this is basically identical to above except different key, they should be consolidated
TEST_ASSERT_EQUAL(0,
BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_bib));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
BIBTestContext bib_test_context;
BSL_TestUtils_InitBIB_AppendixA1(&bib_test_context, BSL_SECROLE_VERIFIER, RFC9173_EXAMPLE_A2_KEY);
// Note - switch to use the WRONG KEY
memcpy(bib_test_context.param_test_key._bytes, RFC9173_EXAMPLE_A2_KEY, strlen(RFC9173_EXAMPLE_A2_KEY));
bib_test_context.param_test_key._bytelen = strlen(RFC9173_EXAMPLE_A2_KEY);
BSL_SecurityActionSet_t *malloced_actionset = BSL_TestUtils_InitMallocBIBActionSet(&bib_test_context);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
TEST_ASSERT_NOT_EQUAL(BSL_SUCCESS,
BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset));
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_actionset);
free(malloced_responseset);
}
/**
* @brief Tests that an acceptor will strip off the result and security block when the security operation validates
* correctly.
*
* Steps:
* - Get a BIB secured bundle from RFC9173 Appendix A1.4.
* - Create a BIB-Acceptor security operation with hard-coded arguments (From RFC9173 A1 ASB)
* - Use the high-level security context interface to create a security outcome.
* - Confirm that the execution succeeds.
* - Check that the BIB result was removed from the bundle (by making sure the encoding matches bundle in A1.1)
*
*/
void test_SecurityContext_BIB_Acceptor(void)
{
TEST_ASSERT_EQUAL(0,
BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, RFC9173_TestVectors_AppendixA1.cbor_bundle_bib));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
BIBTestContext bib_test_context;
BSL_TestUtils_InitBIB_AppendixA1(&bib_test_context, BSL_SECROLE_ACCEPTOR, RFC9173_EXAMPLE_A1_KEY);
BSL_SecurityActionSet_t *malloced_actionset = BSL_TestUtils_InitMallocBIBActionSet(&bib_test_context);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
int encode_result = -1;
bool is_equal_test_vec = false;
int sec_context_result = BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset);
// Note, we use the goto statements to better cleanup if failure happens
if (sec_context_result != 0)
goto cleanup;
encode_result = mock_bpa_encode(mock_bpa_ctr);
if (encode_result != 0)
goto cleanup;
is_equal_test_vec =
BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_original, mock_bpa_ctr->encoded);
if (!is_equal_test_vec)
goto cleanup;
cleanup:
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_actionset);
free(malloced_responseset);
TEST_ASSERT_EQUAL(0, sec_context_result);
TEST_ASSERT_EQUAL(0, encode_result);
TEST_ASSERT_TRUE(is_equal_test_vec);
}
// See RFC: https://www.rfc-editor.org/rfc/rfc9173.html#name-example-3-security-blocks-f
void test_RFC9173_AppendixA_Example3_Acceptor(void)
{
BSL_Crypto_SetRngGenerator(rfc9173_byte_gen_fn_a4);
const char *final_bundle = ("9f88070000820282010282028202018202820201820018281a000f4240850b0300"
"00585c8200020101820282030082820105820300828182015820cac6ce8e4c5dae57"
"988b757e49a6dd1431dc04763541b2845098265bc817241b81820158203ed614c0d9"
"7f49b3633627779aa18a338d212bf3c92b97759d9739cd50725596850c0401005834"
"8101020182028202018382014c5477656c7665313231323132820201820400818182"
"0150efa4b5ac0108e3816c5606479801bc0485070200004319012c85010100005823"
"3a09c1e63fe23a7f66a59c7303837241e070b02619fc59c5214a22f08cd70795e73e"
"9aff");
TEST_ASSERT_EQUAL(0, BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, final_bundle));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
BSL_PrimaryBlock_t primary_block = { 0 };
BSL_BundleCtx_GetBundleMetadata(&mock_bpa_ctr->bundle_ref, &primary_block);
TEST_ASSERT_EQUAL(4, primary_block.block_count);
BSL_SecParam_t param_key = { 0 };
BSL_SecParam_InitStr(¶m_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A1_KEY);
BSL_SecOper_t bib_oper_primary = { 0 };
BSL_SecOper_Init(&bib_oper_primary, 1, 0, 3, BSL_SECBLOCKTYPE_BIB, BSL_SECROLE_ACCEPTOR,
BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bib_oper_primary, ¶m_key);
BSL_SecOper_t bib_oper_ext_block = { 0 };
BSL_SecOper_Init(&bib_oper_ext_block, 1, 2, 3, BSL_SECBLOCKTYPE_BIB, BSL_SECROLE_ACCEPTOR,
BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bib_oper_ext_block, ¶m_key);
BSL_SecParam_t bcb_param_key = { 0 };
BSL_SecParam_InitStr(&bcb_param_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A3_KEY);
BSL_SecOper_t bcb_oper = { 0 };
BSL_SecOper_Init(&bcb_oper, 2, 1, 4, BSL_SECBLOCKTYPE_BCB, BSL_SECROLE_ACCEPTOR, BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bcb_oper, &bcb_param_key);
BSL_SecurityActionSet_t *malloced_actionset = calloc(1, BSL_SecurityActionSet_Sizeof());
BSL_SecurityActionSet_Init(malloced_actionset);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bib_oper_primary);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bib_oper_ext_block);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bcb_oper);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
const int exec_result = BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset);
TEST_ASSERT_EQUAL(BSL_SUCCESS, exec_result);
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_actionset);
free(malloced_responseset);
}
void test_RFC9173_AppendixA_Example3_Source(void)
{
// See: https://www.rfc-editor.org/rfc/rfc9173.html#appendix-A.3.1
const char *plain_bundle = ("9f88070000820282010282028202018202820201820018281a000f424085070200"
"004319012c85010100005823526561647920746f2067656e65726174652061203332"
"2d62797465207061796c6f6164ff");
TEST_ASSERT_EQUAL(0, BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, plain_bundle));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
// Confirm the bundle has two canonical blocks, the payload and bundle age block
BSL_PrimaryBlock_t primary_block = { 0 };
BSL_BundleCtx_GetBundleMetadata(&mock_bpa_ctr->bundle_ref, &primary_block);
TEST_ASSERT_EQUAL(2, primary_block.block_count);
BSL_SecParam_t param_key = { 0 };
BSL_SecParam_InitStr(¶m_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A1_KEY);
BSL_SecParam_t param_sha_var = { 0 };
BSL_SecParam_InitInt64(¶m_sha_var, RFC9173_BIB_PARAMID_SHA_VARIANT, RFC9173_BIB_SHA_HMAC256);
BSL_SecParam_t param_integ_scope = { 0 };
BSL_SecParam_InitInt64(¶m_integ_scope, RFC9173_BIB_PARAMID_INTEG_SCOPE_FLAG, 0);
BSL_SecOper_t bib_oper_primary = { 0 };
BSL_SecOper_Init(&bib_oper_primary, 1, 0, 3, BSL_SECBLOCKTYPE_BIB, BSL_SECROLE_SOURCE, BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bib_oper_primary, ¶m_key);
BSL_SecOper_AppendParam(&bib_oper_primary, ¶m_sha_var);
BSL_SecOper_AppendParam(&bib_oper_primary, ¶m_integ_scope);
BSL_SecOper_t bib_oper_ext_block = { 0 };
BSL_SecOper_Init(&bib_oper_ext_block, 1, 2, 3, BSL_SECBLOCKTYPE_BIB, BSL_SECROLE_SOURCE,
BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bib_oper_ext_block, ¶m_key);
BSL_SecOper_AppendParam(&bib_oper_ext_block, ¶m_sha_var);
BSL_SecOper_AppendParam(&bib_oper_ext_block, ¶m_integ_scope);
BSL_SecParam_t bcb_param_key = { 0 };
BSL_SecParam_InitStr(&bcb_param_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A3_KEY);
BSL_SecParam_t bcb_scope = { 0 };
BSL_SecParam_InitInt64(&bcb_scope, RFC9173_BCB_SECPARAM_AADSCOPE, 0);
BSL_SecParam_t aes_variant = { 0 };
BSL_SecParam_InitInt64(&aes_variant, RFC9173_BCB_SECPARAM_AESVARIANT, 1);
BSL_SecOper_t bcb_oper = { 0 };
BSL_SecOper_Init(&bcb_oper, 2, 1, 4, BSL_SECBLOCKTYPE_BCB, BSL_SECROLE_SOURCE, BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bcb_oper, &bcb_param_key);
BSL_SecOper_AppendParam(&bcb_oper, &bcb_scope);
BSL_SecOper_AppendParam(&bcb_oper, &aes_variant);
BSL_SecurityActionSet_t *malloced_actionset = calloc(1, BSL_SecurityActionSet_Sizeof());
BSL_SecurityActionSet_Init(malloced_actionset);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bib_oper_primary);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bib_oper_ext_block);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bcb_oper);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
const int exec_result = BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset);
TEST_ASSERT_EQUAL(BSL_SUCCESS, exec_result);
// Should have created a new security block
BSL_BundleCtx_GetBundleMetadata(&mock_bpa_ctr->bundle_ref, &primary_block);
TEST_ASSERT_TRUE(primary_block.block_count >= 4);
TEST_ASSERT_TRUE(primary_block.block_count <= 5);
const size_t response_count = BSL_SecurityResponseSet_CountResponses(malloced_responseset);
TEST_ASSERT_EQUAL(3, response_count);
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_actionset);
free(malloced_responseset);
}
void test_RFC9173_AppendixA_Example4_Acceptor(void)
{
BSL_Crypto_SetRngGenerator(rfc9173_byte_gen_fn_a4);
// See: https://www.rfc-editor.org/rfc/rfc9173.html#appendix-A.4.5
const char *final_bundle = ("9f88070000820282010282028202018202820201820018281a000f4240850b0300"
"005846438ed6208eb1c1ffb94d952175167df0902902064a2983910c4fb2340790bf"
"420a7d1921d5bf7c4721e02ab87a93ab1e0b75cf62e4948727c8b5dae46ed2af0543"
"9b88029191850c0201005849820301020182028202018382014c5477656c76653132"
"313231328202038204078281820150220ffc45c8a901999ecc60991dd78b29818201"
"50d2c51cb2481792dae8b21d848cede99b8501010000582390eab6457593379298a8"
"724e16e61f837488e127212b59ac91f8a86287b7d07630a122ff");
TEST_ASSERT_EQUAL(0, BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, final_bundle));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
// Confirm the bundle has 3 canonical blocks: payload, BIB, and BCB
BSL_PrimaryBlock_t primary_block = { 0 };
BSL_BundleCtx_GetBundleMetadata(&mock_bpa_ctr->bundle_ref, &primary_block);
TEST_ASSERT_EQUAL(3, primary_block.block_count);
BSL_CanonicalBlock_t bcb_block = { 0 };
BSL_BundleCtx_GetBlockMetadata(&mock_bpa_ctr->bundle_ref, 2, &bcb_block);
TEST_ASSERT_EQUAL(12, bcb_block.type_code);
TEST_ASSERT_EQUAL(2, bcb_block.block_num);
TEST_ASSERT_EQUAL(1, bcb_block.flags);
// FIRST we must decrypt the BCB targets.
BSL_SecParam_t bcb_param_key = { 0 };
BSL_SecParam_InitStr(&bcb_param_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A4_BCB_KEY);
BSL_SecParam_t bcb_scope = { 0 };
BSL_SecParam_InitInt64(&bcb_scope, RFC9173_BCB_SECPARAM_AADSCOPE, 0x07);
BSL_SecParam_t aes_variant = { 0 };
BSL_SecParam_InitInt64(&aes_variant, RFC9173_BCB_SECPARAM_AESVARIANT, RFC9173_BCB_AES_VARIANT_A256GCM);
BSL_SecOper_t bcb_op_tgt_payload = { 0 };
BSL_SecOper_Init(&bcb_op_tgt_payload, 2, 1, 2, BSL_SECBLOCKTYPE_BCB, BSL_SECROLE_ACCEPTOR,
BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bcb_op_tgt_payload, &bcb_param_key);
BSL_SecOper_AppendParam(&bcb_op_tgt_payload, &aes_variant);
BSL_SecOper_AppendParam(&bcb_op_tgt_payload, &bcb_scope);
BSL_SecOper_t bcb_op_tgt_bib = { 0 };
BSL_SecOper_Init(&bcb_op_tgt_bib, 2, 3, 2, BSL_SECBLOCKTYPE_BCB, BSL_SECROLE_ACCEPTOR, BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &bcb_param_key);
BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &aes_variant);
BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &bcb_scope);
BSL_SecParam_t param_key = { 0 };
BSL_SecParam_InitStr(¶m_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A1_KEY);
BSL_SecParam_t sha_variant = { 0 };
BSL_SecParam_InitInt64(&sha_variant, RFC9173_BIB_PARAMID_SHA_VARIANT, RFC9173_BIB_SHA_HMAC384);
BSL_SecParam_t scope_flag = { 0 };
BSL_SecParam_InitInt64(&scope_flag, RFC9173_BIB_PARAMID_INTEG_SCOPE_FLAG, 0x07);
BSL_SecOper_t bib_oper_payload = { 0 };
BSL_SecOper_Init(&bib_oper_payload, 1, 1, 3, BSL_SECBLOCKTYPE_BIB, BSL_SECROLE_ACCEPTOR,
BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bib_oper_payload, ¶m_key);
BSL_SecOper_AppendParam(&bib_oper_payload, &sha_variant);
BSL_SecOper_AppendParam(&bib_oper_payload, &scope_flag);
BSL_SecurityActionSet_t *malloced_actionset = calloc(1, BSL_SecurityActionSet_Sizeof());
BSL_SecurityActionSet_Init(malloced_actionset);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bcb_op_tgt_payload);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bcb_op_tgt_bib);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bib_oper_payload);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
const int exec_result = BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset);
TEST_ASSERT_EQUAL(BSL_SUCCESS, exec_result);
// After all the security results have been stripped, this is the bundle's result.
// See: https://www.rfc-editor.org/rfc/rfc9173.html#appendix-A.4.1
const char *expected_processed_bundle = ("9f88070000820282010282028202018202820201820018281a000f424085010100"
"005823526561647920746f2067656e657261746520612033322d6279746520706179"
"6c6f6164ff");
TEST_ASSERT_EQUAL(0, mock_bpa_encode(mock_bpa_ctr));
TEST_ASSERT_TRUE(BSL_TestUtils_IsB16StrEqualTo(expected_processed_bundle, mock_bpa_ctr->encoded));
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_actionset);
free(malloced_responseset);
}
void test_RFC9173_AppendixA_Example4_Source(void)
{
BSL_Crypto_SetRngGenerator(rfc9173_byte_gen_fn_a4);
const char *original_bundle = ("9f88070000820282010282028202018202820201820018281a000f424085010100"
"005823526561647920746f2067656e657261746520612033322d6279746520706179"
"6c6f6164ff");
TEST_ASSERT_EQUAL(0, BSL_TestUtils_LoadBundleFromCBOR(&LocalTestCtx, original_bundle));
mock_bpa_ctr_t *mock_bpa_ctr = &LocalTestCtx.mock_bpa_ctr;
BSL_PrimaryBlock_t primary_block = { 0 };
BSL_BundleCtx_GetBundleMetadata(&mock_bpa_ctr->bundle_ref, &primary_block);
TEST_ASSERT_EQUAL(1, primary_block.block_count);
BSL_SecParam_t param_key = { 0 };
BSL_SecParam_InitStr(¶m_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A1_KEY);
BSL_SecParam_t sha_variant = { 0 };
BSL_SecParam_InitInt64(&sha_variant, RFC9173_BIB_PARAMID_SHA_VARIANT, RFC9173_BIB_SHA_HMAC384);
BSL_SecParam_t scope_flag = { 0 };
BSL_SecParam_InitInt64(&scope_flag, RFC9173_BIB_PARAMID_INTEG_SCOPE_FLAG, 0x07);
BSL_SecOper_t bib_oper_payload = { 0 };
BSL_SecOper_Init(&bib_oper_payload, 1, 1, 2, BSL_SECBLOCKTYPE_BIB, BSL_SECROLE_SOURCE, BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bib_oper_payload, ¶m_key);
BSL_SecOper_AppendParam(&bib_oper_payload, &sha_variant);
BSL_SecOper_AppendParam(&bib_oper_payload, &scope_flag);
BSL_SecParam_t bcb_param_key = { 0 };
BSL_SecParam_InitStr(&bcb_param_key, BSL_SECPARAM_TYPE_KEY_ID, RFC9173_EXAMPLE_A4_BCB_KEY);
BSL_SecParam_t bcb_scope = { 0 };
BSL_SecParam_InitInt64(&bcb_scope, RFC9173_BCB_SECPARAM_AADSCOPE, 0x07);
BSL_SecParam_t aes_variant = { 0 };
BSL_SecParam_InitInt64(&aes_variant, RFC9173_BCB_SECPARAM_AESVARIANT, RFC9173_BCB_AES_VARIANT_A256GCM);
BSL_SecOper_t bcb_op_tgt_payload = { 0 };
BSL_SecOper_Init(&bcb_op_tgt_payload, 2, 1, 3, BSL_SECBLOCKTYPE_BCB, BSL_SECROLE_SOURCE,
BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bcb_op_tgt_payload, &bcb_param_key);
BSL_SecOper_AppendParam(&bcb_op_tgt_payload, &aes_variant);
BSL_SecOper_AppendParam(&bcb_op_tgt_payload, &bcb_scope);
BSL_SecOper_t bcb_op_tgt_bib = { 0 };
BSL_SecOper_Init(&bcb_op_tgt_bib, 2, 2, 3, BSL_SECBLOCKTYPE_BCB, BSL_SECROLE_SOURCE, BSL_POLICYACTION_DROP_BLOCK);
BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &bcb_param_key);
BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &aes_variant);
BSL_SecOper_AppendParam(&bcb_op_tgt_bib, &bcb_scope);
BSL_SecurityActionSet_t *malloced_actionset = calloc(1, BSL_SecurityActionSet_Sizeof());
BSL_SecurityActionSet_Init(malloced_actionset);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bcb_op_tgt_payload);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bcb_op_tgt_bib);
BSL_SecurityActionSet_AppendSecOper(malloced_actionset, &bib_oper_payload);
BSL_SecurityResponseSet_t *malloced_responseset = BSL_TestUtils_MallocEmptyPolicyResponse();
const int exec_result = BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
&mock_bpa_ctr->bundle_ref, malloced_actionset);
TEST_ASSERT_EQUAL(BSL_SUCCESS, exec_result);
BSL_PrimaryBlock_t prim_blk;
BSL_BundleCtx_GetBundleMetadata(&mock_bpa_ctr->bundle_ref, &prim_blk);
TEST_ASSERT_TRUE(prim_blk.block_count >= 3 && prim_blk.block_count <= 4);
BSL_SecurityResponseSet_Deinit(malloced_responseset);
BSL_SecurityActionSet_Deinit(malloced_actionset);
free(malloced_actionset);
free(malloced_responseset);
}