BSL v1.1.1 - 64.gc92ac12
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
PolicyParser.c File Reference

Implementation of the JSON parser for this policy. More...

#include "PolicyParser.h"
#include "bsl/front/TextUtil.h"
#include "bsl/cose_sc/CoseContext.h"
#include "bsl/default_sc/DefaultSecContext.h"
#include <jansson.h>
#include <errno.h>
#include <fcntl.h>
#include <strings.h>
+ Include dependency graph for PolicyParser.c:

Typedefs

typedef int(* BSLP_OptionHandler_f) (BSLB_VariantPtrMap_t options, const char *id_str, json_t *value)
 Type for individual option handling according to each SC.
 

Functions

static int BSLP_GetTextAsInt (int64_t *as_int, const char *ptr, size_t len)
 Read a text value as long integer.
 
static int BSLP_GetNumberInt (const json_t *value, int64_t *as_int)
 Read a JSON value as long integer, either directly or from text.
 
static int BSLP_GetBytesHex (const json_t *value, BSL_Data_t *as_bytes)
 Read a JSON value as hexadecimal bytes from text.
 
static int BSLP_GetBoolean (const json_t *value, bool *as_bool)
 Read a JSON value as a boolean, either directly or from text.
 
static BSL_Variant_t * BSLP_OptionAddOrErase (BSLB_VariantPtrMap_t options, int64_t key, const json_t *value)
 Add an option variant if a value is non-null or erase it if the value is null.
 
static int BSLP_PolicyOptions_SC1 (BSLB_VariantPtrMap_t options, const char *id_str, json_t *value)
 Handle options for Security context ID 1.
 
static int BSLP_PolicyOptions_SC2 (BSLB_VariantPtrMap_t options, const char *id_str, json_t *value)
 Handle options for Security context ID 2.
 
static int BSLP_PolicyOptions_SC3 (BSLB_VariantPtrMap_t options, const char *id_str, json_t *value)
 Handle options for Security context ID 3.
 
static int BSLP_PolicyParser_GetLoc (BSL_PolicyLocation_e *loc, const char *text)
 
static int BSLP_PolicyParser_ReadOneRule (BSLP_PolicyProvider_t *policy, const json_t *policy_rule_elm)
 
int BSLP_PolicyParser_LoadFile (const char *file_path, BSLP_PolicyProvider_t *policy)
 Initialize local policy provider from JSON file.
 
int BSLP_PolicyParser_LoadFd (int infd, BSLP_PolicyProvider_t *policy)
 Read policy from a file descriptor.
 
static void BSLP_RegisterPolicyFromBitstring (const BSLP_PolicyParser_BitstringConfig_t policy_bits, BSLP_PolicyProvider_t *policy, BSLB_VariantPtrMap_t options)
 
int BSLP_PolicyParser_FromBitstringList (const char *policies, BSLP_PolicyProvider_t *policy)
 Initialize local policy provider from list of bit strings.
 

Detailed Description

Implementation of the JSON parser for this policy.

Typedef Documentation

◆ BSLP_OptionHandler_f

typedef int(* BSLP_OptionHandler_f) (BSLB_VariantPtrMap_t options, const char *id_str, json_t *value)

Type for individual option handling according to each SC.

Function Documentation

◆ BSLP_GetBoolean()

static int BSLP_GetBoolean ( const json_t *  value,
bool *  as_bool 
)
static

Read a JSON value as a boolean, either directly or from text.

Parameters
[in]valueThe value to interpret.
[out]as_boolThe output value.

References BSL_ERR_POLICY_CONFIG, BSL_LOG_ERR, and BSL_SUCCESS.

Referenced by BSLP_PolicyOptions_SC1(), and BSLP_PolicyOptions_SC2().

◆ BSLP_GetBytesHex()

static int BSLP_GetBytesHex ( const json_t *  value,
BSL_Data_t as_bytes 
)
static

Read a JSON value as hexadecimal bytes from text.

Parameters
[in]valueThe value to interpret.
[out]as_bytesThe output value.

References BSL_ERR_POLICY_CONFIG, BSL_LOG_ERR, and BSL_TextUtil_Base16_Decode().

Referenced by BSLP_PolicyOptions_SC3().

◆ BSLP_GetNumberInt()

static int BSLP_GetNumberInt ( const json_t *  value,
int64_t *  as_int 
)
static

Read a JSON value as long integer, either directly or from text.

Parameters
[in]valueThe value to interpret.
[out]as_intThe output value.

References BSL_ERR_POLICY_CONFIG, BSL_LOG_ERR, BSL_SUCCESS, and BSLP_GetTextAsInt().

Referenced by BSLP_PolicyOptions_SC1(), BSLP_PolicyOptions_SC2(), BSLP_PolicyOptions_SC3(), and BSLP_PolicyParser_ReadOneRule().

◆ BSLP_GetTextAsInt()

static int BSLP_GetTextAsInt ( int64_t *  as_int,
const char *  ptr,
size_t  len 
)
static

Read a text value as long integer.

The entire text must be consumed to be valid.

Parameters
[out]as_intThe output value.
[in]ptrThe text pointer.
lenThe text length (excluding null terminator).

References BSL_ERR_POLICY_CONFIG, BSL_LOG_ERR, and BSL_SUCCESS.

Referenced by BSLP_GetNumberInt(), and BSLP_PolicyOptions_SC3().

◆ BSLP_OptionAddOrErase()

static BSL_Variant_t * BSLP_OptionAddOrErase ( BSLB_VariantPtrMap_t  options,
int64_t  key,
const json_t *  value 
)
static

Add an option variant if a value is non-null or erase it if the value is null.

Parameters
[in]optionsThe map to modify.
keyThe option ID.
valueThe config value.
Returns
Non-NULL if value is to be set or NULL.

References BSLB_VariantPtrMap_add().

Referenced by BSLP_PolicyOptions_SC1(), BSLP_PolicyOptions_SC2(), and BSLP_PolicyOptions_SC3().

◆ BSLP_PolicyOptions_SC1()

static int BSLP_PolicyOptions_SC1 ( BSLB_VariantPtrMap_t  options,
const char *  id_str,
json_t *  value 
)
static

◆ BSLP_PolicyOptions_SC2()

static int BSLP_PolicyOptions_SC2 ( BSLB_VariantPtrMap_t  options,
const char *  id_str,
json_t *  value 
)
static

◆ BSLP_PolicyOptions_SC3()

◆ BSLP_PolicyParser_FromBitstringList()

int BSLP_PolicyParser_FromBitstringList ( const char *  policies,
BSLP_PolicyProvider_t policy 
)

Initialize local policy provider from list of bit strings.

Parameters
[in]policiescomma separated policy bit strings as described by BSLP_PolicyParser_BitstringConfig_t
[in,out]policypolicy provider to configure. Must be initialize/allocated

References BSL_LOG_ERR, BSL_SUCCESS, BSLP_RegisterPolicyFromBitstring(), CHK_ARG_NONNULL, and policy.

Referenced by main().

◆ BSLP_PolicyParser_GetLoc()

◆ BSLP_PolicyParser_LoadFd()

int BSLP_PolicyParser_LoadFd ( int  infd,
BSLP_PolicyProvider_t policy 
)

Read policy from a file descriptor.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References BSL_ERR_POLICY_CONFIG, BSL_LOG_DEBUG, BSL_LOG_ERR, BSL_SUCCESS, BSLP_PolicyParser_ReadOneRule(), CHK_ARG_EXPR, CHK_ARG_NONNULL, and policy.

Referenced by BSLP_PolicyParser_LoadFile(), LLVMFuzzerTestOneInput(), test_PolicyParser_ReadConfigEmpty(), test_PolicyParser_ReadConfigInvalid(), and test_PolicyParser_ReadConfigValid().

◆ BSLP_PolicyParser_LoadFile()

int BSLP_PolicyParser_LoadFile ( const char *  file_path,
BSLP_PolicyProvider_t policy 
)

Initialize local policy provider from JSON file.

Parameters
[in]file_pathFile path to open and read JSON containing policy configuration.
[in,out]policyPolicy provider to configure. Must be initialize/allocated before calling.
Returns
BSL_SUCCESS if successful.

References BSL_ERR_DECODING, BSL_LOG_ERR, BSLP_PolicyParser_LoadFd(), CHK_ARG_NONNULL, and policy.

Referenced by main().

◆ BSLP_PolicyParser_ReadOneRule()

◆ BSLP_RegisterPolicyFromBitstring()