BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
HostInterface.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 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 */
26#include <BPSecLib_Private.h>
27
28// NOLINTNEXTLINE
29static BSL_HostDescriptors_t HostDescriptorTable = { 0 };
30
32{
33 // Note, these used to assert non-NULL
34 // However, this was causing problems in Ubuntu environments
35 (void)(desc.eid_init);
36 (void)(desc.get_host_eid_fn);
37 (void)(desc.eid_deinit);
38 (void)(desc.bundle_metadata_fn);
39 (void)(desc.bundle_get_block_ids);
40 (void)(desc.block_metadata_fn);
41 (void)(desc.block_create_fn);
42 (void)(desc.block_remove_fn);
43 (void)(desc.block_realloc_btsd_fn);
44
45 // Old-style callbacks
46 (void)(desc.eid_from_cbor);
47 (void)(desc.eid_from_text);
48 (void)(desc.eidpat_init);
49 (void)(desc.eidpat_deinit);
50 (void)(desc.eidpat_from_text);
51 (void)(desc.eidpat_match);
52
53 HostDescriptorTable = desc;
54 return BSL_SUCCESS;
55}
56
58{
59 CHK_ARG_NONNULL(bundle);
60 CHK_ARG_NONNULL(result_primary_block);
61
62 CHK_PRECONDITION(HostDescriptorTable.bundle_metadata_fn != NULL);
63
64 memset(result_primary_block, 0, sizeof(*result_primary_block));
65 int result = HostDescriptorTable.bundle_metadata_fn(bundle, result_primary_block);
66 return (result == 0) ? BSL_SUCCESS : BSL_ERR_HOST_CALLBACK_FAILED;
67}
68
69int BSL_BundleCtx_GetBlockMetadata(const BSL_BundleRef_t *bundle, uint64_t block_num,
70 BSL_CanonicalBlock_t *result_block)
71{
72 CHK_ARG_NONNULL(bundle);
73 CHK_ARG_NONNULL(result_block);
74 CHK_ARG_EXPR(block_num > 0);
75
76 CHK_PRECONDITION(HostDescriptorTable.block_metadata_fn != NULL);
77 memset(result_block, 0, sizeof(*result_block));
78 int result = HostDescriptorTable.block_metadata_fn(bundle, block_num, result_block);
79 return (result == 0) ? BSL_SUCCESS : BSL_ERR_HOST_CALLBACK_FAILED;
80}
81
82int BSL_BundleCtx_GetBlockIds(const BSL_BundleRef_t *bundle, size_t array_count, uint64_t block_ids_array[array_count],
83 size_t *result_count)
84{
85 CHK_ARG_NONNULL(bundle);
86 CHK_ARG_EXPR(array_count > 0);
87 CHK_ARG_NONNULL(block_ids_array);
88 CHK_ARG_NONNULL(result_count);
89
90 *result_count = 0;
91 CHK_PRECONDITION(HostDescriptorTable.bundle_get_block_ids != NULL);
92 int returncode = HostDescriptorTable.bundle_get_block_ids(bundle, array_count, block_ids_array, result_count);
93 return (returncode == BSL_SUCCESS) ? 0 : BSL_ERR_HOST_CALLBACK_FAILED;
94}
95
96int BSL_BundleCtx_CreateBlock(BSL_BundleRef_t *bundle, uint64_t block_type_code, uint64_t *block_num)
97{
98 CHK_ARG_NONNULL(bundle);
99 CHK_ARG_EXPR(block_type_code > 0);
100 CHK_ARG_NONNULL(block_num);
101
102 *block_num = 0;
103 CHK_PRECONDITION(HostDescriptorTable.block_create_fn != NULL);
104 int result = HostDescriptorTable.block_create_fn(bundle, block_type_code, block_num);
105 return (result == 0) ? BSL_SUCCESS : BSL_ERR_HOST_CALLBACK_FAILED;
106}
107
108int BSL_BundleCtx_RemoveBlock(BSL_BundleRef_t *bundle, uint64_t block_num)
109{
110 CHK_ARG_NONNULL(bundle);
111 CHK_ARG_EXPR(block_num > 0);
112 CHK_PRECONDITION(HostDescriptorTable.block_remove_fn != NULL);
113 int result = HostDescriptorTable.block_remove_fn(bundle, block_num);
114 return (result == 0) ? BSL_SUCCESS : BSL_ERR_HOST_CALLBACK_FAILED;
115}
116
117int BSL_BundleCtx_ReallocBTSD(BSL_BundleRef_t *bundle, uint64_t block_num, size_t bytesize)
118{
119 CHK_ARG_NONNULL(bundle);
120 CHK_ARG_EXPR(block_num > 0);
121 CHK_ARG_EXPR(bytesize > 0);
122 CHK_PRECONDITION(HostDescriptorTable.block_remove_fn != NULL);
123 int result = HostDescriptorTable.block_realloc_btsd_fn(bundle, block_num, bytesize);
124 return (result == 0) ? BSL_SUCCESS : BSL_ERR_HOST_CALLBACK_FAILED;
125}
126
128{
129 ASSERT_ARG_NONNULL(desc);
130 *desc = HostDescriptorTable;
131}
132
134{
135 CHK_ARG_NONNULL(eid);
136 CHK_PRECONDITION(HostDescriptorTable.eid_init != NULL);
137 return HostDescriptorTable.eid_init(HostDescriptorTable.user_data, eid);
138}
139
141{
142 ASSERT_ARG_NONNULL(eid);
143 ASSERT_PRECONDITION(HostDescriptorTable.eid_deinit != NULL);
144 HostDescriptorTable.eid_deinit(HostDescriptorTable.user_data, eid);
145}
146
148{
149 CHK_ARG_NONNULL(eid);
150 CHK_PRECONDITION(HostDescriptorTable.get_host_eid_fn != NULL);
151 return HostDescriptorTable.get_host_eid_fn(HostDescriptorTable.user_data, eid);
152}
153
154int BSL_HostEID_EncodeToCBOR(const BSL_HostEID_t *eid, void *user_data)
155{
156 CHK_ARG_NONNULL(eid);
157 CHK_ARG_NONNULL(user_data);
158 return HostDescriptorTable.eid_to_cbor(user_data, eid);
159}
160
162{
163 CHK_ARG_NONNULL(eid);
164 CHK_ARG_NONNULL(decoder);
165
166 CHK_PRECONDITION(eid->handle != NULL);
167 int ecode = HostDescriptorTable.eid_from_cbor(decoder, eid);
168 return ecode;
169}
170
171int BSL_HostEID_DecodeFromText(BSL_HostEID_t *eid, const char *text)
172{
173 CHK_ARG_NONNULL(eid);
174 CHK_ARG_NONNULL(text);
175
176 // Basic sanity check, may need to remove.
177 CHK_PRECONDITION(strlen(text) < 100);
178 CHK_PRECONDITION(HostDescriptorTable.eid_from_text != NULL);
179
180 return HostDescriptorTable.eid_from_text(eid, text, HostDescriptorTable.user_data);
181}
182
184{
185 CHK_ARG_NONNULL(pat);
186 CHK_PRECONDITION(HostDescriptorTable.eidpat_init);
187 return HostDescriptorTable.eidpat_init(pat, HostDescriptorTable.user_data);
188}
189
191{
192 ASSERT_ARG_NONNULL(pat);
193 HostDescriptorTable.eidpat_deinit(pat, HostDescriptorTable.user_data);
194}
195
197{
198 CHK_ARG_NONNULL(pat);
199 CHK_ARG_NONNULL(text);
200 CHK_ARG_EXPR(strlen(text) < 100);
201 CHK_PRECONDITION(HostDescriptorTable.eidpat_from_text != NULL);
202 return HostDescriptorTable.eidpat_from_text(pat, text, HostDescriptorTable.user_data);
203}
204
206{
207 ASSERT_ARG_NONNULL(pat);
208 ASSERT_ARG_NONNULL(eid);
209 ASSERT_PRECONDITION(HostDescriptorTable.eidpat_match);
210 return HostDescriptorTable.eidpat_match(pat, eid, HostDescriptorTable.user_data);
211}
Single entry-point include file for all of the BPSec Lib (BSL) frontend API.
@ BSL_SUCCESS
Placeholder for non-error code.
@ BSL_ERR_HOST_CALLBACK_FAILED
Callback to the host BPA returned a non-zero code.
int BSL_BundleCtx_CreateBlock(BSL_BundleRef_t *bundle, uint64_t block_type_code, uint64_t *block_num)
Request the creation of a new block of a given type in the bundle.
int BSL_Host_GetSecSrcEID(BSL_HostEID_t *eid)
Get the local EID used when this node is a security source.
int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc)
Set the BPA descriptor (callbacks) for this process.
int BSL_HostEID_DecodeFromCBOR(BSL_HostEID_t *eid, void *decoder)
Load an EID from CBOR.
int BSL_BundleCtx_GetBlockIds(const BSL_BundleRef_t *bundle, size_t array_count, uint64_t block_ids_array[array_count], size_t *result_count)
Returns an array in which each element contains the id of the corresponding block....
int BSL_HostEID_EncodeToCBOR(const BSL_HostEID_t *eid, void *user_data)
Encode a EID into a CBOR sequence.
int BSL_BundleCtx_GetBlockMetadata(const BSL_BundleRef_t *bundle, uint64_t block_num, BSL_CanonicalBlock_t *result_block)
Returns information about the bundle Canonical block.
void BSL_HostDescriptors_Get(BSL_HostDescriptors_t *desc)
Copy the BPA descriptor for this process.
bool BSL_HostEIDPattern_IsMatch(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid)
Determine if an EID Pattern matches a specific EID.
void BSL_HostEIDPattern_Deinit(BSL_HostEIDPattern_t *pat)
De-initialize an abstract EID Pattern.
int BSL_HostEID_Init(BSL_HostEID_t *eid)
Initialize an abstract EID.
int BSL_HostEIDPattern_Init(BSL_HostEIDPattern_t *pat)
Initialize an abstract EID Pattern.
int BSL_HostEID_DecodeFromText(BSL_HostEID_t *eid, const char *text)
Decode an EID from its text form.
int BSL_HostEIDPattern_DecodeFromText(BSL_HostEIDPattern_t *pat, const char *text)
Decode an EID Pattern from its text form.
int BSL_BundleCtx_ReallocBTSD(BSL_BundleRef_t *bundle, uint64_t block_num, size_t bytesize)
Requests the re-allocation of a block's BTSD, useful for BCB.
int BSL_BundleCtx_RemoveBlock(BSL_BundleRef_t *bundle, uint64_t block_num)
Requests the removal of a block from a bundle.
int BSL_BundleCtx_GetBundleMetadata(const BSL_BundleRef_t *bundle, BSL_PrimaryBlock_t *result_primary_block)
Calls the host interface to get a bundle primary block information.abort.
void BSL_HostEID_Deinit(BSL_HostEID_t *eid)
De-initialize an abstract EID.
Reference to a Bundle owned and stored in the host BPA.
Structure containing parsed Canonical Block fields.
Dynamic BPA descriptor.
int(* block_remove_fn)(BSL_BundleRef_t *bundle_ref, uint64_t block_num)
Host BPA function to remove a given canonical block from the bundle.
void(* eidpat_deinit)(BSL_HostEIDPattern_t *pat, void *user_data)
Host BPA function to deinit an EID pattern type.
int(* block_create_fn)(BSL_BundleRef_t *bundle_ref, uint64_t block_type_code, uint64_t *result_block_num)
Host BPA function to create a new canonical block with the given type, returning result in the output...
int(* block_metadata_fn)(const BSL_BundleRef_t *bundle_ref, uint64_t block_num, BSL_CanonicalBlock_t *result_block)
Host BPA function to populate a Canonical Block struct for a given block number.
int(* eid_to_cbor)(void *encoder, const BSL_HostEID_t *eid)
Host BPA function to encode an EID to CBOR.
int(* block_realloc_btsd_fn)(BSL_BundleRef_t *bundle_ref, uint64_t block_num, size_t bytesize)
Host BPA function to reallocate a canonical block's BTSD, keeping existing data in-place.
int(* eid_init)(void *user_data, BSL_HostEID_t *result)
Host BPA function to initialize/allocate an EID type.
int(* get_host_eid_fn)(const void *user_data, BSL_HostEID_t *result)
Host BPA function to get its current EID.
int(* eidpat_from_text)(BSL_HostEIDPattern_t *pat, const char *text, void *user_data)
Host BPA function to parse an EID pattern from a C-string.
int(* bundle_get_block_ids)(const BSL_BundleRef_t *bundle_ref, size_t array_count, uint64_t array_block_ids[array_count], size_t *result_count)
Host BPA function to populate a pre-allocated array with canonical block IDs.
int(* eid_from_cbor)(void *encoder, BSL_HostEID_t *eid)
Host BPA function to decode an EID from a CBOR context.
int(* eid_from_text)(BSL_HostEID_t *eid, const char *text, void *user_data)
Host BPA function to parse an EID from a C-string.
void(* eid_deinit)(void *user_data, BSL_HostEID_t *eid)
Host BPA function to deinit/free an EID type.
int(* bundle_metadata_fn)(const BSL_BundleRef_t *bundle_ref, BSL_PrimaryBlock_t *result_primary_block)
Host BPA function to populate a Primary Block struct.
bool(* eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data)
Host BPA function that returns true if the given EID matched an EID pattern.
void * user_data
User data pointer for callbacks.
int(* eidpat_init)(BSL_HostEIDPattern_t *pat, void *user_data)
Host BPA function to initialize an EID pattern type.
Reference to a EID pattern owned and stored in the BPA.
Opaque pointer to BPA-specific Endpoint ID storage.
void * handle
Opaque pointer for BPA backend to use.
Contains Bundle Primary Block fields and metadata.