BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
mock_bpa_ctr.c
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#include <BPSecLib_Private.h>
23
24#include "mock_bpa_ctr.h"
25#include "bsl_mock_bpa_decode.h"
26#include "bsl_mock_bpa_encode.h"
27
28static const size_t SMALLBUF_SIZE = 1024;
29
30void mock_bpa_ctr_init(mock_bpa_ctr_t *ctr)
31{
32 CHKVOID(ctr);
33 BSL_Data_Init(&(ctr->encoded));
34 assert(ctr->bundle_ref.data == NULL);
35 ctr->bundle_ref.data = calloc(1, sizeof(MockBPA_Bundle_t));
36 // TODO : Just make a MockBPA_Bundle_Init function.
37 // HostEID_t's are initialized deeper into the decode function.
38}
39
40void mock_bpa_ctr_init_move(mock_bpa_ctr_t *ctr, mock_bpa_ctr_t *src)
41{
42 CHKVOID(ctr);
43 CHKVOID(src);
44 BSL_Data_InitMove(&(ctr->encoded), &(src->encoded));
45 ctr->bundle_ref = src->bundle_ref;
46 src->bundle_ref.data = NULL;
47}
48
49void mock_bpa_ctr_deinit(mock_bpa_ctr_t *ctr)
50{
51 CHKVOID(ctr);
52 BSL_Data_Deinit(&(ctr->encoded));
53
54 if (ctr->bundle_ref.data)
55 {
56 MockBPA_Bundle_Deinit(ctr->bundle_ref.data);
57 free(ctr->bundle_ref.data);
58 }
59}
60
61int mock_bpa_decode(mock_bpa_ctr_t *ctr, BSL_LibCtx_t *bsl)
62{
63 (void)bsl;
64 CHKERR1(ctr);
65 MockBPA_Bundle_t *bundle = ctr->bundle_ref.data;
66
67 if (ctr->bundle_ref.data)
68 {
69 for (size_t i = 0; i < bundle->block_count; i++)
70 {
71 free(bundle->blocks[i].btsd);
72 }
73 }
74
75 QCBORDecodeContext decoder;
76 QCBORDecode_Init(&decoder, (UsefulBufC) { ctr->encoded.ptr, ctr->encoded.len }, QCBOR_DECODE_MODE_NORMAL);
77 if (bsl_mock_decode_bundle(&decoder, bundle))
78 {
79 return 2;
80 }
81 if (QCBORDecode_Finish(&decoder))
82 {
83 return 3;
84 }
85
86 return 0;
87}
88
89int mock_bpa_encode(mock_bpa_ctr_t *ctr)
90{
91 CHKERR1(ctr);
92 MockBPA_Bundle_t *bundle = ctr->bundle_ref.data;
93 CHKERR1(bundle);
94
95 QCBOREncodeContext encoder;
96
97 // assume some small size and expand if necessary
98 uint8_t smallbuf[SMALLBUF_SIZE];
99 QCBOREncode_Init(&encoder, (UsefulBuf) { smallbuf, sizeof(smallbuf) });
100 if (bsl_mock_encode_bundle(&encoder, bundle))
101 {
102 return 2;
103 }
104 size_t needlen;
105 if (QCBOR_SUCCESS != QCBOREncode_FinishGetSize(&encoder, &needlen))
106 {
107 return 3;
108 }
109
110 if (needlen < SMALLBUF_SIZE)
111 {
112 if (BSL_Data_CopyFrom(&(ctr->encoded), needlen, smallbuf))
113 {
114 return 4;
115 }
116 }
117 else
118 {
119 if (BSL_Data_Resize(&(ctr->encoded), needlen))
120 {
121 return 4;
122 }
123 QCBOREncode_Init(&encoder, (UsefulBuf) { ctr->encoded.ptr, ctr->encoded.len });
124 if (bsl_mock_encode_bundle(&encoder, bundle))
125 {
126 return 2;
127 }
128 UsefulBufC final;
129 if (QCBOR_SUCCESS != QCBOREncode_Finish(&encoder, &final))
130 {
131 return 3;
132 }
133 }
134
135 BSL_LOG_INFO("Encoded bundle");
136
137 return 0;
138}
Single entry-point include file for all of the BPSec Lib (BSL) frontend API.
#define CHKVOID(cond)
Return from void functions if condition fails.
#define BSL_LOG_INFO(...)
This is an overloaded member function, provided for convenience. It differs from the above function o...
#define CHKERR1(cond)
Return the error value 1 if condition fails.
int BSL_Data_Resize(BSL_Data_t *data, size_t len)
Resize the data, copying if necessary.
void BSL_Data_InitMove(BSL_Data_t *data, BSL_Data_t *src)
This is an overloaded member function, provided for convenience. It differs from the above function o...
int BSL_Data_Deinit(BSL_Data_t *data)
De-initialize a data struct, freeing if necessary.
int BSL_Data_CopyFrom(BSL_Data_t *data, size_t len, BSL_DataConstPtr_t src)
Set an initialized data struct to a given size.
int BSL_Data_Init(BSL_Data_t *data)
Initialize an empty data struct.
int bsl_mock_decode_bundle(QCBORDecodeContext *dec, MockBPA_Bundle_t *bundle)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Declarations for bundle and block decoding.
int bsl_mock_encode_bundle(QCBOREncodeContext *enc, const MockBPA_Bundle_t *bundle)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Declarations for bundle and block encoding.
void * data
Opaque pointer, not used by the BSL.
size_t len
Size of the data buffer.
BSL_DataPtr_t ptr
Pointer to the front of the buffer.
Concrete definition of library context.
A container for encoded and decoded bundle data.
BSL_Data_t encoded
Encoded PDU.
BSL_BundleRef_t bundle_ref
The decoded bundle.