BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
SecParam.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 */
22
27#include "SecParam.h"
28
30{
31 return sizeof(BSL_SecParam_t);
32}
33
34int BSL_SecParam_InitBytestr(BSL_SecParam_t *self, uint64_t param_id, BSL_Data_t value)
35{
36 CHK_ARG_NONNULL(self);
37
38 CHK_ARG_EXPR(value.ptr != NULL);
39 CHK_ARG_EXPR(value.len > 0);
40 CHK_ARG_EXPR(value.len < sizeof(self->_bytes) - 1);
41
42 memset(self, 0, sizeof(*self));
43 self->param_id = param_id;
45 self->_bytelen = value.len;
46 memcpy(self->_bytes, value.ptr, self->_bytelen);
47
48 return BSL_SUCCESS;
49}
50
51int BSL_SecParam_InitInt64(BSL_SecParam_t *self, uint64_t param_id, uint64_t value)
52{
53 CHK_ARG_NONNULL(self);
54
55 memset(self, 0, sizeof(*self));
56 self->param_id = param_id;
58 self->_uint_value = value;
59
60 return BSL_SUCCESS;
61}
62
64{
65 CHK_ARG_NONNULL(self);
66 return (self->_type == BSL_SECPARAM_TYPE_INT64);
67}
68
70{
71 ASSERT_ARG_NONNULL(self);
72 ASSERT_PRECONDITION(self->_type == BSL_SECPARAM_TYPE_INT64);
73
74 return self->_uint_value;
75}
76
78{
79 CHK_ARG_NONNULL(result);
80 CHK_PRECONDITION(BSL_SecParam_IsConsistent(self));
81
82 return BSL_Data_InitView(result, self->_bytelen, (uint8_t *)self->_bytes);
83}
84
86{
87 ASSERT_PRECONDITION(BSL_SecParam_IsConsistent(self));
88
89 return self->param_id;
90}
91
93{
94 CHK_AS_BOOL(self != NULL);
95 CHK_AS_BOOL(self->param_id > 0);
96 CHK_AS_BOOL(self->_type > BSL_SECPARAM_TYPE_UNKNOWN && self->_type <= BSL_SECPARAM_TYPE_BYTESTR);
97
98 if (self->_type == BSL_SECPARAM_TYPE_INT64)
99 {
100 CHK_AS_BOOL(self->_bytelen == 0);
101 }
102 else
103 {
104 CHK_AS_BOOL(self->_bytelen > 0);
105 CHK_AS_BOOL(self->_bytelen <= sizeof(self->_bytes));
106 CHK_AS_BOOL(self->_uint_value == 0);
107 }
108 return true;
109}
110
111bool BSL_SecParam_IsParamIDOutput(uint64_t param_id)
112{
113 // If this index is less than the start index for numbering
114 // internal param ids, then it's probably a param_id from the spec.
115 return param_id < BSL_SECPARAM_TYPE_INT_STARTINDEX;
116}
@ BSL_SECPARAM_TYPE_INT_STARTINDEX
Do not use. Indicates start index of internal param ids.
@ BSL_SECPARAM_TYPE_BYTESTR
Indicates the value type is a byte string.
@ BSL_SECPARAM_TYPE_INT64
Indicates value type is an unsigned integer.
@ BSL_SECPARAM_TYPE_UNKNOWN
Inidcates parsed value not of expected type.
struct BSL_SecParam_s BSL_SecParam_t
Represents a security parameter in an ASB as defined in RFC9172.
@ BSL_SUCCESS
Placeholder for non-error code.
int BSL_SecParam_IsInt64(const BSL_SecParam_t *self)
Returns true when the value type is an integer.
Definition SecParam.c:63
int BSL_SecParam_GetAsBytestr(const BSL_SecParam_t *self, BSL_Data_t *result)
Retrieve bytestring value of result when security parameter type is bytestring.
Definition SecParam.c:77
int BSL_SecParam_InitInt64(BSL_SecParam_t *self, uint64_t param_id, uint64_t value)
Initialize as a parameter containing an integer as a value.
Definition SecParam.c:51
int BSL_SecParam_InitBytestr(BSL_SecParam_t *self, uint64_t param_id, BSL_Data_t value)
Initialize as a parameter containing a bytestring.
Definition SecParam.c:34
uint64_t BSL_SecParam_GetId(const BSL_SecParam_t *self)
Get parameter ID of this param.
Definition SecParam.c:85
size_t BSL_SecParam_Sizeof(void)
Return size of BSL_SecParam_t struct type.
Definition SecParam.c:29
bool BSL_SecParam_IsParamIDOutput(uint64_t param_id)
Indicates true when this parameter is NOT an implementation-specific security paramter.
Definition SecParam.c:111
uint64_t BSL_SecParam_GetAsUInt64(const BSL_SecParam_t *self)
Retrieve integer value of result when this result type is integer.
Definition SecParam.c:69
bool BSL_SecParam_IsConsistent(const BSL_SecParam_t *self)
Return true if invariant conditions pass.
Definition SecParam.c:92
Defines the RFC 9172 Security Parameter of the Abstract Security Block.
int BSL_Data_InitView(BSL_Data_t *data, size_t len, const BSL_DataPtr_t src)
Initialize a data struct as an overlay on optional external data.
Heap data storage and views.
size_t len
Size of the data buffer.
BSL_DataPtr_t ptr
Pointer to the front of the buffer.
uint8_t _bytes[BSL_DEFAULT_BYTESTR_LEN+1]
Private. When a bytestring, this field is set, with the _bytelen set accordingly.
Definition SecParam.h:79
enum BSL_SecParam_Types_e _type
Private. Indicates whether this is an integer or bytestring.
Definition SecParam.h:73
uint64_t param_id
Parameter ID.
Definition SecParam.h:70
uint64_t _uint_value
Private. When an integer, this field is populated with the correct value.
Definition SecParam.h:76
size_t _bytelen
Private.
Definition SecParam.h:83