Skip to content

Commit 341b9c3

Browse files
authored
Removed M*LIB use from bsl_cose_sc public headers (#258)
1 parent 9f43410 commit 341b9c3

8 files changed

Lines changed: 281 additions & 216 deletions

File tree

pkg/bsl-cose-sc.pc.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
55
Name: bsl-cose-sc
66
Description: BSL COSE Context provider
77
Version: @PROJECT_VERSION@
8-
Requires: bsl, m-lib
8+
Requires: bsl
9+
Requires.private: m-lib
910
Cflags: -I${includedir}
1011
Libs: -L${libdir} -lbsl_cose_sc

src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ target_sources(bsl_cose_sc
124124
FILE_SET private_headers
125125
TYPE HEADERS
126126
FILES
127+
bsl/cose_sc/AadScope.h
127128
bsl/cose_sc/CoseMsg.h
128129
PRIVATE
130+
bsl/cose_sc/AadScope.c
129131
bsl/cose_sc/CoseContext.c
130132
bsl/cose_sc/CoseMsg.c
131133
)
@@ -137,7 +139,7 @@ set_target_properties(bsl_cose_sc
137139
target_link_libraries(bsl_cose_sc PUBLIC bsl_front)
138140
target_link_libraries(bsl_cose_sc PUBLIC bsl_dynamic) # for CBOR API
139141
target_link_libraries(bsl_cose_sc PRIVATE bsl_crypto)
140-
target_link_libraries(bsl_cose_sc PUBLIC MLIB::mlib)
142+
target_link_libraries(bsl_cose_sc PRIVATE MLIB::mlib)
141143
target_link_libraries(bsl_cose_sc PRIVATE qcbor::qcbor)
142144

143145
# Example Policy Provider library

src/bsl/cose_sc/AadScope.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "AadScope.h"
2+
3+
int BSLX_CoseSc_AadScope_Encode(QCBOREncodeContext *enc, const BSLX_CoseSc_AadScope_t *scope)
4+
{
5+
// aad-scope map
6+
QCBOREncode_OpenMap(enc);
7+
8+
BSLX_CoseSc_AadScope_it_t aads_it;
9+
for (BSLX_CoseSc_AadScope_it(aads_it, *scope); !BSLX_CoseSc_AadScope_end_p(aads_it);
10+
BSLX_CoseSc_AadScope_next(aads_it))
11+
{
12+
const BSLX_CoseSc_AadScope_subtype_ct *aads_pair = BSLX_CoseSc_AadScope_cref(aads_it);
13+
QCBOREncode_AddInt64(enc, *(aads_pair->key_ptr));
14+
QCBOREncode_AddUInt64(enc, *(aads_pair->value_ptr));
15+
}
16+
17+
QCBOREncode_CloseMap(enc);
18+
return BSL_SUCCESS;
19+
}
20+
21+
int BSLX_CoseSc_AadScope_Decode(QCBORDecodeContext *dec, BSLX_CoseSc_AadScope_t *scope)
22+
{
23+
BSLX_CoseSc_AadScope_reset(*scope);
24+
25+
QCBORItem item;
26+
QCBORDecode_EnterArray(dec, &item); // using QCBOR_DECODE_MODE_MAP_AS_ARRAY
27+
28+
while (QCBOR_SUCCESS == QCBORDecode_PeekNext(dec, &item))
29+
{
30+
int64_t blk_num;
31+
QCBORDecode_GetInt64(dec, &blk_num);
32+
if (QCBOR_SUCCESS != QCBORDecode_GetError(dec))
33+
{
34+
BSL_LOG_ERR("Invalid AAD Scope map key");
35+
break;
36+
}
37+
38+
uint64_t aad_flags;
39+
QCBORDecode_GetUInt64(dec, &aad_flags);
40+
if (QCBOR_SUCCESS != QCBORDecode_GetError(dec))
41+
{
42+
BSL_LOG_ERR("Invalid AAD Scope map value");
43+
break;
44+
}
45+
46+
BSLX_CoseSc_AadScope_set_at(*scope, blk_num, aad_flags);
47+
}
48+
49+
QCBORDecode_ExitArray(dec);
50+
return BSL_SUCCESS;
51+
}

src/bsl/cose_sc/AadScope.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Johns Hopkins University Applied Physics
3+
* Laboratory LLC.
4+
*
5+
* This file is part of the Bundle Protocol Security Library (BSL).
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* This work was performed for the Jet Propulsion Laboratory, California
18+
* Institute of Technology, sponsored by the United States Government under
19+
* the prime contract 80NM0018D0004 between the Caltech and NASA under
20+
* subcontract 1700763.
21+
*/
22+
23+
/** @file
24+
* @ingroup cose_sc
25+
* Header for the implementation of the COSE context @cite draft-ietf-dtn-bpsec-cose.
26+
*/
27+
28+
#ifndef BSLX_COSESC_AADSCOPE_H_
29+
#define BSLX_COSESC_AADSCOPE_H_
30+
31+
#include "CoseContext.h"
32+
33+
#include "bsl/dynamic/CBOR.h"
34+
35+
#include <m-bptree.h>
36+
37+
#ifdef __cplusplus
38+
extern "C" {
39+
#endif
40+
41+
/** @struct BSLX_CoseSc_AadScope_t
42+
* An internal representation of AAD Scope map, with keys sorted in
43+
* CBOR deterministic order and values as a bit mask of
44+
* ::BSLX_CoseSc_AadScope_Flag_e flags.
45+
*/
46+
// NOLINTBEGIN
47+
/// @cond Doxygen_Suppress
48+
// GCOV_EXCL_START
49+
M_BPTREE_DEF2(BSLX_CoseSc_AadScope, 4, int64_t, M_OPEXTEND(M_BASIC_OPLIST, CMP(API_6(BSL_CBOR_Compare_Int64))),
50+
uint64_t, M_BASIC_OPLIST)
51+
// GCOV_EXCL_STOP
52+
/// @endcond
53+
// NOLINTEND
54+
55+
/// Matches ::BSL_CBOR_Encode_f signature.
56+
int BSLX_CoseSc_AadScope_Encode(QCBOREncodeContext *enc, const BSLX_CoseSc_AadScope_t *scope);
57+
58+
/// Matches ::BSL_CBOR_Decode_f signature.
59+
int BSLX_CoseSc_AadScope_Decode(QCBORDecodeContext *dec, BSLX_CoseSc_AadScope_t *scope);
60+
61+
#ifdef __cplusplus
62+
} // extern C
63+
#endif
64+
65+
#endif /* BSLX_COSESC_AADSCOPE_H_ */

src/bsl/cose_sc/CoseContext.c

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
#include "CoseContext.h"
2828

29-
#include "CoseContext_Private.h"
29+
#include "AadScope.h"
3030
#include "CoseMsg.h"
3131

3232
#include "bsl/BPSecLib_Private.h"
@@ -42,6 +42,37 @@
4242
#include <stdlib.h>
4343
#include <time.h>
4444

45+
int BSLX_CoseSc_SetAadScope(BSL_IdValPair_t *option, const BSLX_CoseSc_AadScope_Item_t *list, size_t count)
46+
{
47+
ASSERT_ARG_NONNULL(option);
48+
ASSERT_ARG_NONNULL(list);
49+
50+
BSLX_CoseSc_AadScope_t obj;
51+
BSLX_CoseSc_AadScope_init(obj);
52+
53+
for (size_t item_ix = 0; item_ix < count; ++item_ix)
54+
{
55+
const BSLX_CoseSc_AadScope_Item_t *item = &list[item_ix];
56+
BSLX_CoseSc_AadScope_set_at(obj, item->key, item->flags);
57+
}
58+
59+
BSL_Data_t aad_scope_enc;
60+
BSL_Data_Init(&aad_scope_enc);
61+
int res = BSL_CBOR_Encode_Twopass(&aad_scope_enc, (BSL_CBOR_Encode_f)&BSLX_CoseSc_AadScope_Encode, &obj);
62+
// GCOV_EXCL_START
63+
if (BSL_SUCCESS != res)
64+
{
65+
BSL_LOG_ERR("Failed to encode AAD Scope");
66+
}
67+
// GCOV_EXCL_STOP
68+
69+
BSL_IdValPair_SetRaw(option, BSLX_COSESC_OPTION_AAD_SCOPE, aad_scope_enc.ptr, aad_scope_enc.len);
70+
BSL_Data_Deinit(&aad_scope_enc);
71+
72+
BSLX_CoseSc_AadScope_clear(obj);
73+
return res;
74+
}
75+
4576
/** Acceptable target algorithms for MAC.
4677
* @note These must be sorted for @c bsearch() to work.
4778
*/
@@ -462,56 +493,6 @@ bool BSLX_CoseSc_Validate(BSL_LibCtx_t *lib _U_, BSL_BundleRef_t *bundle, BSL_Se
462493
return valid;
463494
}
464495

465-
int BSLX_CoseSc_AadScope_Encode(QCBOREncodeContext *enc, const BSLX_CoseSc_AadScope_t *scope)
466-
{
467-
// aad-scope map
468-
QCBOREncode_OpenMap(enc);
469-
470-
BSLX_CoseSc_AadScope_it_t aads_it;
471-
for (BSLX_CoseSc_AadScope_it(aads_it, *scope); !BSLX_CoseSc_AadScope_end_p(aads_it);
472-
BSLX_CoseSc_AadScope_next(aads_it))
473-
{
474-
const BSLX_CoseSc_AadScope_subtype_ct *aads_pair = BSLX_CoseSc_AadScope_cref(aads_it);
475-
QCBOREncode_AddInt64(enc, *(aads_pair->key_ptr));
476-
QCBOREncode_AddUInt64(enc, *(aads_pair->value_ptr));
477-
}
478-
479-
QCBOREncode_CloseMap(enc);
480-
return BSL_SUCCESS;
481-
}
482-
483-
int BSLX_CoseSc_AadScope_Decode(QCBORDecodeContext *dec, BSLX_CoseSc_AadScope_t *scope)
484-
{
485-
BSLX_CoseSc_AadScope_reset(*scope);
486-
487-
QCBORItem item;
488-
QCBORDecode_EnterArray(dec, &item); // using QCBOR_DECODE_MODE_MAP_AS_ARRAY
489-
490-
while (QCBOR_SUCCESS == QCBORDecode_PeekNext(dec, &item))
491-
{
492-
int64_t blk_num;
493-
QCBORDecode_GetInt64(dec, &blk_num);
494-
if (QCBOR_SUCCESS != QCBORDecode_GetError(dec))
495-
{
496-
BSL_LOG_ERR("Invalid AAD Scope map key");
497-
break;
498-
}
499-
500-
uint64_t aad_flags;
501-
QCBORDecode_GetUInt64(dec, &aad_flags);
502-
if (QCBOR_SUCCESS != QCBORDecode_GetError(dec))
503-
{
504-
BSL_LOG_ERR("Invalid AAD Scope map value");
505-
break;
506-
}
507-
508-
BSLX_CoseSc_AadScope_set_at(*scope, blk_num, aad_flags);
509-
}
510-
511-
QCBORDecode_ExitArray(dec);
512-
return BSL_SUCCESS;
513-
}
514-
515496
/** @struct BSLX_CoseSc_ChunkItem_t
516497
* A variant which can be either:
517498
* - @c data An instance of @c m_bstring_t
@@ -665,7 +646,7 @@ static int BSLX_CoseSc_ExternalAad_Chunked(const BSLX_CoseSc_t *ctx, BSLX_CoseSc
665646
if (blk_num == 0)
666647
{
667648
// primary block
668-
if (aad_flags & BSLX_COSESC_AAD_FLAG_METADATA)
649+
if (aad_flags & BSLX_COSESC_AADSCOPE_FLAG_METADATA)
669650
{
670651
m_bstring_t *data = BSLX_CoseSc_ChunkList_GetBstring(chunklist);
671652

@@ -691,7 +672,7 @@ static int BSLX_CoseSc_ExternalAad_Chunked(const BSLX_CoseSc_t *ctx, BSLX_CoseSc
691672
}
692673
}
693674

694-
if (aad_flags & BSLX_COSESC_AAD_FLAG_METADATA)
675+
if (aad_flags & BSLX_COSESC_AADSCOPE_FLAG_METADATA)
695676
{
696677
m_bstring_t *data = BSLX_CoseSc_ChunkList_GetBstring(chunklist);
697678

@@ -700,7 +681,7 @@ static int BSLX_CoseSc_ExternalAad_Chunked(const BSLX_CoseSc_t *ctx, BSLX_CoseSc
700681
*total += BSLX_CoseSc_bstring_AppendHead(*data, CBOR_MAJOR_TYPE_POSITIVE_INT, aad_block.block_num);
701682
*total += BSLX_CoseSc_bstring_AppendHead(*data, CBOR_MAJOR_TYPE_POSITIVE_INT, aad_block.flags);
702683
}
703-
if (aad_flags & BSLX_COSESC_AAD_FLAG_BTSD)
684+
if (aad_flags & BSLX_COSESC_AADSCOPE_FLAG_BTSD)
704685
{
705686
// CBOR head and seq stream
706687
{

src/bsl/cose_sc/CoseContext.h

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
#include "bsl/BPSecLib_Private.h"
3232
#include "bsl/BPSecLib_Public.h"
33-
#include "bsl/dynamic/CBOR.h"
34-
35-
#include <m-bptree.h>
3633

3734
#ifdef __cplusplus
3835
extern "C" {
@@ -42,7 +39,7 @@ extern "C" {
4239
#define BSLX_COSESC_CTX_ID 3
4340

4441
/// Internal option enumerations
45-
enum BSLX_CoseSC_Option_e
42+
enum BSLX_CoseSc_Option_e
4643
{
4744
/** Key ID as a byte string.
4845
* The value is a byte string (which may contain encoded UTF8 text).
@@ -64,7 +61,8 @@ enum BSLX_CoseSC_Option_e
6461
*/
6562
BSLX_COSESC_OPTION_TGT_ALG,
6663
/** AAD Scope as raw encoded data.
67-
* The value is encoded CBOR interpreted as ::BSLX_CoseSc_AadScope_t.
64+
* The value is encoded CBOR interpreted as ::BSLX_CoseSc_AadScope_t,
65+
* and can use the BSLX_CoseSc_SetAadScope() helper in policy providers.
6866
* Optional for source, optional exact-match for verifier/acceptor.
6967
*/
7068
BSLX_COSESC_OPTION_AAD_SCOPE,
@@ -111,7 +109,7 @@ enum BSLX_CoseSC_Option_e
111109
};
112110

113111
/// @brief From https://www.ietf.org/archive/id/draft-ietf-dtn-bpsec-cose-16.html#section-2.2
114-
enum BSLX_CoseSC_Param_e
112+
enum BSLX_CoseSc_Param_e
115113
{
116114
/// Additional Protected headers
117115
BSLX_COSESC_PARAM_ADDL_PHDR = 3,
@@ -122,7 +120,7 @@ enum BSLX_CoseSC_Param_e
122120
};
123121

124122
/// @brief From https://www.ietf.org/archive/id/draft-ietf-dtn-bpsec-cose-16.html#section-2.3
125-
enum BSLX_CoseSC_Result_e
123+
enum BSLX_CoseSc_Result_e
126124
{
127125
BSLX_COSESC_RESULT_COSE_ENCRYPT0 = 16,
128126
BSLX_COSESC_RESULT_COSE_MAC0 = 17,
@@ -132,30 +130,47 @@ enum BSLX_CoseSC_Result_e
132130
BSLX_COSESC_RESULT_COSE_SIGN = 98,
133131
};
134132

135-
/** @struct BSLX_CoseSc_AadScope_t
136-
* An internal representation of AAD Scope map, with keys sorted in
137-
* CBOR deterministic order and values as a bit mask of
138-
* ::BSLX_CoseSC_AAD_Flag_e flags.
139-
*/
140-
// NOLINTBEGIN
141-
/// @cond Doxygen_Suppress
142-
// GCOV_EXCL_START
143-
M_BPTREE_DEF2(BSLX_CoseSc_AadScope, 4, int64_t, M_OPEXTEND(M_BASIC_OPLIST, CMP(API_6(BSL_CBOR_Compare_Int64))),
144-
uint64_t, M_BASIC_OPLIST)
145-
// GCOV_EXCL_STOP
146-
/// @endcond
147-
// NOLINTEND
133+
/// Special keys for AAD Scope parameter
134+
enum BSLX_CoseSc_AadScope_Special_e
135+
{
136+
/// Reference the security target block
137+
BSLX_COSESC_AADSCOPE_SPECIAL_TARGET = -1,
138+
/// Reference the parent security block
139+
BSLX_COSESC_AADSCOPE_SPECIAL_SECURITY = -2,
140+
};
148141

149142
/// Flags for AAD Scope parameter
150-
enum BSLX_CoseSC_AAD_Flag_e
143+
enum BSLX_CoseSc_AadScope_Flag_e
151144
{
152-
BSLX_COSESC_AAD_FLAG_METADATA = 0x1,
153-
BSLX_COSESC_AAD_FLAG_BTSD = 0x2,
145+
/// Include block header items in AAD
146+
BSLX_COSESC_AADSCOPE_FLAG_METADATA = 0x1,
147+
/// Include BTSD in AAD
148+
BSLX_COSESC_AADSCOPE_FLAG_BTSD = 0x2,
154149
};
155150

156-
int BSLX_CoseSc_AadScope_Encode(QCBOREncodeContext *enc, const BSLX_CoseSc_AadScope_t *scope);
151+
/** Native C structure for each item of COSE Context AAD Scope.
152+
*/
153+
typedef struct
154+
{
155+
/// Block number or special key from ::BSLX_CoseSc_AadScope_Special_e
156+
int64_t key;
157+
/** Choice of flags from ::BSLX_CoseSc_AadScope_Flag_e.
158+
* This type is compatible with ::BSL_IdValPair_s storage.
159+
*/
160+
int64_t flags;
161+
} BSLX_CoseSc_AadScope_Item_t;
157162

158-
int BSLX_CoseSc_AadScope_Decode(QCBORDecodeContext *dec, BSLX_CoseSc_AadScope_t *scope);
163+
/** Utility to set the ::BSLX_COSESC_OPTION_AAD_SCOPE option without exposing
164+
* the encoding internals.
165+
*
166+
* @param[in,out] option Pointer to the option to set the AAD Scope on.
167+
* @param[in] list Pointer to an array of integer values, each
168+
* subsequent pair of values is interpreted as a (key, value) in the scope map.
169+
* The order of keys in this form is not significant.
170+
* @param count The number of @b pairs of values in the @c list array.
171+
* @return BSL_SUCCESS if successful.
172+
*/
173+
int BSLX_CoseSc_SetAadScope(BSL_IdValPair_t *option, const BSLX_CoseSc_AadScope_Item_t *list, size_t count);
159174

160175
/// Match signature ::BSL_SecCtx_Validate_f
161176
bool BSLX_CoseSc_Validate(BSL_LibCtx_t *lib, BSL_BundleRef_t *bundle, BSL_SecOper_t *sec_oper);

0 commit comments

Comments
 (0)