BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
bsl_mock_bpa_eid.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 "bsl_mock_bpa_eid.h"
28#include <BSLConfig.h>
29#include <BPSecLib_Private.h>
30
31#include <string.h>
32#include <strings.h>
33#include <stdio.h>
34#include <sys/types.h>
35
36int MockBPA_GetEid(const void *user_data, BSL_HostEID_t *result_eid)
37{
38 const char *local_ipn = getenv("BSL_TEST_LOCAL_IPN_EID");
39 return (0 == mock_bpa_eid_from_text(result_eid, local_ipn, (void *)user_data)) ? 0 : -1;
40}
41
43{
44 CHKVOID(eid);
45 memset(eid, 0, sizeof(bsl_mock_eid_t));
46}
47
49{
50 CHKVOID(eid);
51 switch (eid->scheme)
52 {
54 break;
55 default:
56 BSL_Data_Deinit(&(eid->ssp.as_raw));
57 break;
58 }
59 memset(eid, 0, sizeof(bsl_mock_eid_t));
60}
61
62int MockBPA_EID_Init(void *user_data _U_, BSL_HostEID_t *eid)
63{
64 CHKERR1(eid);
65 assert(eid != NULL);
66 memset(eid, 0, sizeof(BSL_HostEID_t));
67 eid->handle = BSL_MALLOC(sizeof(bsl_mock_eid_t));
68 if (!(eid->handle))
69 {
70 return -2;
71 }
73 return 0;
74}
75
76void MockBPA_EID_Deinit(void *user_data _U_, BSL_HostEID_t *eid)
77{
78 CHKVOID(eid);
79 if (eid->handle)
80 {
82 BSL_FREE(eid->handle);
83 }
84 memset(eid, 0, sizeof(BSL_HostEID_t));
85}
86
87int mock_bpa_get_secsrc(BSL_HostEID_t *eid, void *user_data)
88{
89 const char *local_ipn = getenv("BSL_TEST_LOCAL_IPN_EID");
90 return mock_bpa_eid_from_text(eid, local_ipn, user_data);
91}
92
93int mock_bpa_eid_from_text(BSL_HostEID_t *eid, const char *text, void *user_data _U_)
94{
95 CHKERR1(eid);
96 CHKERR1(text);
97
98 // clean up if necessary
99 // bsl_mock_eid_deinit(eid->handle);
100
101 const char *curs = text;
102 const char *end = curs + strlen(text);
103 char *pend = strchr(text, ':');
104 if (pend == NULL)
105 {
106 return 2;
107 }
108 size_t scheme_len = pend - text;
109
110 if (strncasecmp(text, "ipn", scheme_len) == 0)
111 {
112 curs = pend + 1;
113
114 uint64_t p1, p2, p3;
115 int len1, len2;
116 // use scanf to handle two or three component case
117 int res = sscanf(curs, "%" PRIu64 ".%" PRIu64 "%n.%" PRIu64 "%n", &p1, &p2, &len1, &p3, &len2);
118
119 bsl_eid_ipn_ssp_t ipn_ssp;
120 if (res == 2)
121 {
122 // two components
123 ipn_ssp.ncomp = 2;
124 ipn_ssp.auth_num = p1 >> 32;
125 ipn_ssp.node_num = p1 & 0xFFFFFFFF;
126 ipn_ssp.svc_num = p2;
127 curs += len1;
128 }
129 else if (res == 3)
130 {
131 // three components
132 ipn_ssp.ncomp = 3;
133 ipn_ssp.auth_num = p1;
134 ipn_ssp.node_num = p2;
135 ipn_ssp.svc_num = p3;
136 curs += len2;
137
138 if ((ipn_ssp.auth_num > UINT32_MAX) || (ipn_ssp.node_num > UINT32_MAX))
139 {
140 // parts larger than allowed
141 return 4;
142 }
143 }
144 else
145 {
146 return 4;
147 }
148
149 if (curs < end)
150 {
151 // extra text
152 return 5;
153 }
154
155 bsl_mock_eid_t *obj = (bsl_mock_eid_t *)eid->handle;
156 assert(eid->handle != NULL);
158 obj->ssp.as_ipn = ipn_ssp;
159 }
160 else
161 {
162 // unhandled scheme
163 return 3;
164 }
165
166 return 0;
167}
168
169// int mock_bpa_eid_to_text(string_t out, const BSL_HostEID_t *eid, void *user_data _U_)
170// {
171// CHKERR1(eid);
172// CHKERR1(eid->handle);
173// bsl_mock_eid_t *obj = (bsl_mock_eid_t *)eid->handle;
174
175// switch (obj->scheme)
176// {
177// case BSL_MOCK_EID_IPN:
178// {
179// const bsl_eid_ipn_ssp_t *ipn = &(obj->ssp.as_ipn);
180// switch (ipn->ncomp)
181// {
182// case 2:
183// string_printf(out, "ipn:%" PRIu64 ".%" PRIu64, (ipn->auth_num << 32) | ipn->node_num,
184// ipn->svc_num); break;
185// case 3:
186// string_printf(out, "ipn:%" PRIu64 ".%" PRIu64 ".%" PRIu64, ipn->auth_num, ipn->node_num,
187// ipn->svc_num);
188// break;
189// default:
190// // not valid
191// break;
192// }
193// break;
194// }
195// default:
196// string_printf(out, "<unknown EID scheme: %d>", obj->scheme);
197// break;
198// }
199// return 0;
200// }
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 _U_
Mark an unused parameter Within a function definition.
#define CHKERR1(cond)
Return the error value 1 if condition fails.
int BSL_Data_Deinit(BSL_Data_t *data)
De-initialize a data struct, freeing if necessary.
void bsl_mock_eid_deinit(bsl_mock_eid_t *eid)
Internal struct de-initializer.
int mock_bpa_get_secsrc(BSL_HostEID_t *eid, void *user_data)
Interface for BSL_HostDescriptors_t::get_secsrc.
void MockBPA_EID_Deinit(void *user_data, BSL_HostEID_t *eid)
Interface for BSL_HostDescriptors_t::eid_deinit.
int MockBPA_EID_Init(void *user_data, BSL_HostEID_t *eid)
Interface for BSL_HostDescriptors_t::eid_init.
int mock_bpa_eid_from_text(BSL_HostEID_t *eid, const char *text, void *user_data)
Interface for BSL_HostDescriptors_t::eid_from_text.
void bsl_mock_eid_init(bsl_mock_eid_t *eid)
Internal struct initializer.
Declarations for EID handling.
@ BSL_MOCK_EID_IPN
The "ipn" scheme.
Opaque pointer to BPA-specific Endpoint ID storage.
void * handle
Opaque pointer for BPA backend to use.
Scheme-specific part for IPN scheme.
int ncomp
The number of components when encoded, either 2 or 3.
uint64_t svc_num
The service number component.
uint64_t node_num
The node number component.
uint64_t auth_num
The authority number component.
Struct to be used as a BSL_HostEID_t::handle.
union bsl_mock_eid_t::@0 ssp
Interpreted according to scheme code.
bsl_eid_ipn_ssp_t as_ipn
Used when scheme is BSL_MOCK_EID_IPN.
uint64_t scheme
Code point for EID schemes from .
BSL_Data_t as_raw
Used in all other cases, copied from source.