BSL v1.0.0 - 29.g99f1b76
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
HostInterface.c File Reference

Implementation of the host BPA and its callback functions. More...

#include <stdarg.h>
#include <pthread.h>
#include <sys/time.h>
#include <BPSecLib_Private.h>
#include "UtilDefs_SeqReadWrite.h"
+ Include dependency graph for HostInterface.c:

Functions

int BSL_HostDescriptors_Set (BSL_HostDescriptors_t desc)
 Set the BPA descriptor (callbacks) for this process.
 
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.
 
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.
 
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_BundleCtx_RemoveBlock (BSL_BundleRef_t *bundle, uint64_t block_num)
 Requests the removal of a block from a bundle.
 
int BSL_BundleCtx_DeleteBundle (BSL_BundleRef_t *bundle, BSL_ReasonCode_t reason_code)
 Requests dropping of bundle.
 
int BSL_BundleCtx_ReallocBTSD (BSL_BundleRef_t *bundle, uint64_t block_num, size_t btsd_size)
 Requests the re-allocation of a block's BTSD, useful for BCB.
 
BSL_SeqReader_t * BSL_BundleCtx_ReadBTSD (const BSL_BundleRef_t *bundle, uint64_t block_num)
 Construct a new sequential reader for BTSD content.
 
BSL_SeqWriter_t * BSL_BundleCtx_WriteBTSD (BSL_BundleRef_t *bundle, uint64_t block_num, size_t btsd_size)
 Construct a new sequential writer for BTSD content.
 
void BSL_HostDescriptors_Get (BSL_HostDescriptors_t *desc)
 Copy the BPA descriptor for this process.
 
void BSL_HostDescriptors_Clear (void)
 Reset the host descriptors to their default, unusable state.
 
void BSL_HostEID_Init (BSL_HostEID_t *eid)
 Initialize an abstract EID.
 
void BSL_HostEID_Deinit (BSL_HostEID_t *eid)
 De-initialize an abstract EID.
 
int BSL_Host_GetSecSrcEID (BSL_HostEID_t *eid)
 Get the local EID used when this node is a security source.
 
int BSL_HostEID_EncodeToCBOR (const BSL_HostEID_t *eid, BSL_Data_t *encoded_bytes)
 Encode a EID into CBOR.
 
int BSL_HostEID_DecodeFromCBOR (const BSL_Data_t *encoded_bytes, BSL_HostEID_t *eid)
 Decode an EID from CBOR.
 
int BSL_HostEID_DecodeFromText (BSL_HostEID_t *eid, const char *text)
 Decode an EID from its text form.
 
int BSL_HostEIDPattern_Init (BSL_HostEIDPattern_t *pat)
 Initialize an abstract EID Pattern.
 
void BSL_HostEIDPattern_Deinit (BSL_HostEIDPattern_t *pat)
 De-initialize an abstract EID Pattern.
 
int BSL_HostEIDPattern_DecodeFromText (BSL_HostEIDPattern_t *pat, const char *text)
 Decode an EID Pattern from its text form.
 
bool BSL_HostEIDPattern_IsMatch (const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid)
 Determine if an EID Pattern matches a specific EID.
 
bool BSL_LogIsEnabledFor (int severity)
 Determine if a particular severity is being logged.
 
void BSL_LogEvent (int severity, const char *filename, int lineno, const char *funcname, const char *format,...)
 Log an event.
 
void * BSL_malloc (size_t size)
 Dynamic memory allocation.
 
void * BSL_realloc (void *ptr, size_t size)
 Dynamic memory reallocation.
 
void * BSL_calloc (size_t nmemb, size_t size)
 Contiguous dynamic memory allocation.
 
void BSL_free (void *ptr)
 Free dynamically allocated memory.
 

Variables

static BSL_HostDescriptors_t HostDescriptorTable = { .dyn_mem_desc = BSL_DynMemHostDescriptors_DEFAULT }
 Initialized to library default.
 
static const char * log_sev_names []
 

Detailed Description

Implementation of the host BPA and its callback functions.

Function Documentation

◆ BSL_BundleCtx_CreateBlock()

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.

Parameters
[in]bundleContext bundle
[in]block_type_codeThe type of block to be created (e.g, 1 means payload)
[out]block_numPointer to integer containing the number of the block just created.abort
Returns
0 on success, negative on error

References BSL_HostDescriptors_t::block_create_fn, BSL_ERR_HOST_CALLBACK_FAILED, BSL_SUCCESS, CHK_ARG_EXPR, CHK_ARG_NONNULL, CHK_PRECONDITION, and HostDescriptorTable.

Referenced by BSL_ExecBCBSource(), and BSL_ExecBIBSource().

◆ BSL_BundleCtx_DeleteBundle()

int BSL_BundleCtx_DeleteBundle ( BSL_BundleRef_t bundle,
BSL_ReasonCode_t  reason_code 
)

Requests dropping of bundle.

Parameters
[in]bundleContext bundle
[in]reason_codereason code indicating reason for bundle deletion
Returns
0 on success, negative on failure.

References BSL_ERR_HOST_CALLBACK_FAILED, BSL_SUCCESS, BSL_HostDescriptors_t::bundle_delete_fn, CHK_ARG_NONNULL, CHK_PRECONDITION, and HostDescriptorTable.

Referenced by BSLP_PolicyProvider_HandleFailures().

◆ BSL_BundleCtx_GetBlockMetadata()

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.

Parameters
[in]bundleContext bundle
[in]block_numThe number of the bundle canonical block we seek information on
[out]result_blockPointer to allocated memory which contains the results of the query.
Returns
0 on success, negative on error

References BSL_HostDescriptors_t::block_metadata_fn, BSL_ERR_HOST_CALLBACK_FAILED, BSL_SUCCESS, CHK_ARG_EXPR, CHK_ARG_NONNULL, CHK_PRECONDITION, and HostDescriptorTable.

Referenced by BSL_API_QuerySecurity(), BSL_ExecBCBSource(), BSL_ExecBCBVerifierAcceptor(), BSL_ExecBIBSource(), BSL_ExecBIBVerifierAcceptor(), BSLX_BCB_Execute(), BSLX_BCB_GetParams(), BSLX_BCB_Init(), BSLX_BIB_Execute(), get_target_block_id(), n_test_BSL_6(), test_BSL_32(), TEST_CASE(), test_dyn_mem_cbs_BSL_32(), and test_RFC9173_AppendixA_Example4_Acceptor().

◆ BSL_BundleCtx_GetBundleMetadata()

int BSL_BundleCtx_GetBundleMetadata ( const BSL_BundleRef_t bundle,
BSL_PrimaryBlock_t result_primary_block 
)

◆ BSL_BundleCtx_ReadBTSD()

BSL_SeqReader_t * BSL_BundleCtx_ReadBTSD ( const BSL_BundleRef_t bundle,
uint64_t  block_num 
)

Construct a new sequential reader for BTSD content.

Parameters
[in]bundleContext bundle
[in]block_numThe unique block number for which BTSD will be read from.
Returns
Pointer to the new reader or NULL if some failure occurs.

References BSL_HostDescriptors_t::block_read_btsd_fn, and HostDescriptorTable.

Referenced by BSL_API_QuerySecurity(), BSL_ExecBCBVerifierAcceptor(), BSL_ExecBIBVerifierAcceptor(), BSLX_BCB_Decrypt(), BSLX_BCB_Encrypt(), BSLX_BIB_GenIPPT(), and TEST_CASE().

◆ BSL_BundleCtx_ReallocBTSD()

int BSL_BundleCtx_ReallocBTSD ( BSL_BundleRef_t bundle,
uint64_t  block_num,
size_t  btsd_size 
)

Requests the re-allocation of a block's BTSD, useful for BCB.

Note
Uses semantics similar to memcpy().
Parameters
[in]bundleContext bundle
[in]block_numThe unique block number for which BTSD will be resized.
[in]btsd_sizeSize of new BTSD content.
Returns
0 on success, negative on failure.

References BSL_HostDescriptors_t::block_realloc_btsd_fn, BSL_HostDescriptors_t::block_remove_fn, BSL_ERR_HOST_CALLBACK_FAILED, BSL_SUCCESS, CHK_ARG_EXPR, CHK_ARG_NONNULL, CHK_PRECONDITION, and HostDescriptorTable.

◆ BSL_BundleCtx_RemoveBlock()

int BSL_BundleCtx_RemoveBlock ( BSL_BundleRef_t bundle,
uint64_t  block_num 
)

Requests the removal of a block from a bundle.

Parameters
[in]bundleContext bundle
[in]block_numBlock number to be removed
Returns
0 on success, negative on failure.

References BSL_HostDescriptors_t::block_remove_fn, BSL_ERR_HOST_CALLBACK_FAILED, BSL_SUCCESS, CHK_ARG_EXPR, CHK_ARG_NONNULL, CHK_PRECONDITION, and HostDescriptorTable.

Referenced by BSL_ExecBCBVerifierAcceptor(), BSL_ExecBIBVerifierAcceptor(), and BSLP_PolicyProvider_HandleFailures().

◆ BSL_BundleCtx_WriteBTSD()

BSL_SeqWriter_t * BSL_BundleCtx_WriteBTSD ( BSL_BundleRef_t bundle,
uint64_t  block_num,
size_t  btsd_size 
)

Construct a new sequential writer for BTSD content.

Parameters
[in]bundleContext bundle
[in]block_numThe unique block number for which BTSD will be overwritten.
btsd_sizeThe total total size of BTSD content that will be written. The actual sequence of writes must not exceed this total size or it will be considered an error. If the actual sequence of writes does not reach this size it should be zero-padded and logged as an anomaly.
Returns
Pointer to the new writer or NULL if some failure occurs.

References BSL_HostDescriptors_t::block_write_btsd_fn, and HostDescriptorTable.

Referenced by BSLX_BCB_Decrypt(), BSLX_BCB_Encrypt(), and Encode_ASB().

◆ BSL_calloc()

◆ BSL_free()

void BSL_free ( void *  ptr)

Free dynamically allocated memory.

Parameters
ptrpointer to memory to free

References BSL_HostDescriptors_t::dyn_mem_desc, BSL_DynMemHostDescriptors_t::free_cb, and HostDescriptorTable.

Referenced by BSL_API_QuerySecurity(), BSL_Crypto_ClearGeneratedKeyHandle(), BSL_Crypto_UnwrapKey(), bsl_data_int_free(), bsl_mock_decode_canonical(), BSL_PrimaryBlock_deinit(), BSL_SecCtx_ExecutePolicyActionSet(), BSL_SeqReader_Destroy(), BSL_SeqWriter_Destroy(), BSL_TestUtils_InitMallocBIBActionSet(), BSL_TestUtils_ReadBTSD_Deinit(), BSL_TestUtils_WriteBTSD_Deinit(), BSLP_InitParams_Deinit(), BSLP_PolicyProvider_Deinit(), BSLP_QueryPolicy(), BSLX_BCB_Execute(), BSLX_BIB_Execute(), LLVMFuzzerTestOneInput(), mock_bpa_ctr_deinit(), mock_bpa_eidpat_deinit(), MockBPA_Agent_Deinit(), MockBPA_Agent_process(), MockBPA_Bundle_Deinit(), MockBPA_EID_Deinit(), MockBPA_ReadBTSD(), MockBPA_ReadBTSD_Deinit(), MockBPA_RemoveBlock(), MockBPA_WriteBTSD(), MockBPA_WriteBTSD_Deinit(), tearDown(), test_bsl_mock_encode_canonical(), TEST_CASE(), test_decrypt(), test_encrypt(), test_MultiplePolicyProviders(), test_RFC9173_AppendixA_Example1_BIB_Source(), test_RFC9173_AppendixA_Example2_BCB_Acceptor(), test_RFC9173_AppendixA_Example2_BCB_Source(), test_RFC9173_AppendixA_Example3_Acceptor(), test_RFC9173_AppendixA_Example3_Source(), test_RFC9173_AppendixA_Example4_Acceptor(), test_RFC9173_AppendixA_Example4_Source(), test_sec_accept_keyunwrap(), test_sec_source_keywrap(), test_SecurityContext_BIB_Acceptor(), test_SecurityContext_BIB_Source(), test_SecurityContext_BIB_Verifier(), test_SecurityContext_BIB_Verifier_Failure(), test_SeqWriter_flat(), and TestASBDecodeEncodeClosure().

◆ BSL_Host_GetSecSrcEID()

int BSL_Host_GetSecSrcEID ( BSL_HostEID_t eid)

Get the local EID used when this node is a security source.

Parameters
[out]eidThe EID to write into. This must already be initialized.
Returns
Zero if successful.
See also
BSL_ROLE_SOURCE

References CHK_ARG_NONNULL, CHK_PRECONDITION, BSL_HostDescriptors_t::get_sec_src_eid_fn, HostDescriptorTable, and BSL_HostDescriptors_t::user_data.

Referenced by BSL_ExecBCBSource(), and BSL_ExecBIBSource().

◆ BSL_HostDescriptors_Clear()

void BSL_HostDescriptors_Clear ( void  )

Reset the host descriptors to their default, unusable state.

Warning
This function is not thread safe and should be used after any ::BSL_LibCtx_t is deinitialized.

References HostDescriptorTable.

Referenced by main(), and suiteTearDown().

◆ BSL_HostDescriptors_Get()

void BSL_HostDescriptors_Get ( BSL_HostDescriptors_t desc)

Copy the BPA descriptor for this process.

Note
This function is not thread safe.
Parameters
[out]descThe descriptor to copy into.

References ASSERT_ARG_NONNULL, and HostDescriptorTable.

◆ BSL_HostDescriptors_Set()

◆ BSL_HostEID_DecodeFromCBOR()

int BSL_HostEID_DecodeFromCBOR ( const BSL_Data_t encoded_bytes,
BSL_HostEID_t eid 
)

Decode an EID from CBOR.

Parameters
[in]encoded_bytesCBOR encoded bytes
[in,out]eidThe value to decode into
Returns
0 on success

References CHK_ARG_NONNULL, CHK_PRECONDITION, BSL_HostDescriptors_t::eid_from_cbor, BSL_HostEID_t::handle, and HostDescriptorTable.

Referenced by BSL_AbsSecBlock_DecodeFromCBOR(), and LLVMFuzzerTestOneInput().

◆ BSL_HostEID_DecodeFromText()

int BSL_HostEID_DecodeFromText ( BSL_HostEID_t eid,
const char *  text 
)

Decode an EID from its text form.

Parameters
[out]eidThe EID to write into. This must already be initialized.
[in]textThe text to read from, which must be non-null.
Returns
Zero if successful.

References CHK_ARG_NONNULL, CHK_PRECONDITION, BSL_HostDescriptors_t::eid_from_text, BSL_HostEID_t::handle, HostDescriptorTable, and BSL_HostDescriptors_t::user_data.

Referenced by LLVMFuzzerTestOneInput(), main(), test_BSL_HostEID_DecodeFromText_invalid(), test_BSL_HostEID_DecodeFromText_ipn(), test_BSL_HostEID_DecodeFromText_valid(), test_BSL_HostEIDPattern_IsMatch(), test_bsl_mock_encode_bundle(), and TEST_CASE().

◆ BSL_HostEID_Deinit()

◆ BSL_HostEID_EncodeToCBOR()

int BSL_HostEID_EncodeToCBOR ( const BSL_HostEID_t eid,
BSL_Data_t encoded_bytes 
)

Encode a EID into CBOR.

Parameters
[in]eidThe value to encode
[in,out]encoded_bytesCBOR encoded bytes
Returns
Zero if successful.

References CHK_ARG_NONNULL, BSL_HostDescriptors_t::eid_to_cbor, and HostDescriptorTable.

Referenced by BSL_AbsSecBlock_EncodeToCBOR(), bsl_mock_encode_eid_from_ctx(), and LLVMFuzzerTestOneInput().

◆ BSL_HostEID_Init()

◆ BSL_HostEIDPattern_DecodeFromText()

int BSL_HostEIDPattern_DecodeFromText ( BSL_HostEIDPattern_t pat,
const char *  text 
)

Decode an EID Pattern from its text form.

Parameters
[out]patThe pattern to write into. This must already be initialized.
[in]textThe text to read from, which must be non-null.
Returns
Zero if successful.

References CHK_ARG_NONNULL, CHK_PRECONDITION, BSL_HostDescriptors_t::eidpat_from_text, HostDescriptorTable, and BSL_HostDescriptors_t::user_data.

Referenced by BSL_TestUtils_GetEidPatternFromText(), BSLP_PolicyPredicate_InitFrom(), LLVMFuzzerTestOneInput(), test_BSL_HostEIDPattern_DecodeFromText_invalid(), test_BSL_HostEIDPattern_DecodeFromText_valid(), test_BSL_HostEIDPattern_IsMatch(), and TEST_CASE().

◆ BSL_HostEIDPattern_Deinit()

◆ BSL_HostEIDPattern_Init()

◆ BSL_HostEIDPattern_IsMatch()

bool BSL_HostEIDPattern_IsMatch ( const BSL_HostEIDPattern_t pat,
const BSL_HostEID_t eid 
)

Determine if an EID Pattern matches a specific EID.

Parameters
[in]patThe pattern to compare.
[in]eidThe EID to compare.
Returns
True if the EID is a match to the pattern.

References ASSERT_ARG_NONNULL, ASSERT_PRECONDITION, BSL_HostDescriptors_t::eidpat_match, HostDescriptorTable, and BSL_HostDescriptors_t::user_data.

Referenced by BSLP_PolicyPredicate_IsMatch(), and test_BSL_HostEIDPattern_IsMatch().

◆ BSL_LogEvent()

void BSL_LogEvent ( int  severity,
const char *  filename,
int  lineno,
const char *  funcname,
const char *  format,
  ... 
)

Log an event.

Parameters
severityThe severity from a subset of the POSIX syslog values.
[in]filenameThe originating file name, which may include directory parts.
[in]linenoThe originating file line number.
[in]funcnameThe originating function name.
[in]formatThe log message format string.
...Values for the format string.

References BSL_LogIsEnabledFor(), HostDescriptorTable, BSL_HostDescriptors_t::log_event, and log_sev_names.

◆ BSL_LogIsEnabledFor()

bool BSL_LogIsEnabledFor ( int  severity)

Determine if a particular severity is being logged.

This function is multi-thread safe.

Parameters
severityThe severity from a subset of the POSIX syslog values.
Returns
True if the severity level will be logged.
See also
BSL_log_set_least_severity()

References HostDescriptorTable, and BSL_HostDescriptors_t::log_is_enabled_for.

Referenced by BSL_LogEvent().

◆ BSL_malloc()

◆ BSL_realloc()

void * BSL_realloc ( void *  ptr,
size_t  size 
)

Dynamic memory reallocation.

Parameters
ptrexisting dynamic memory pointer
sizenew allocation size
Returns
valid heap pointer

References BSL_HostDescriptors_t::dyn_mem_desc, HostDescriptorTable, and BSL_DynMemHostDescriptors_t::realloc_cb.

Referenced by BSL_Data_Resize(), and MockBPA_ReallocBTSD().

Variable Documentation

◆ HostDescriptorTable

◆ log_sev_names

const char* log_sev_names[]
static
Initial value:
= {
NULL,
NULL,
"CRIT",
"ERROR",
"WARNING",
NULL,
"INFO",
"DEBUG",
}

Referenced by BSL_LogEvent().