aboutsummaryrefslogtreecommitdiff
path: root/src/credential/plugin_gnsrecord_credential.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/credential/plugin_gnsrecord_credential.c')
-rw-r--r--src/credential/plugin_gnsrecord_credential.c346
1 files changed, 186 insertions, 160 deletions
diff --git a/src/credential/plugin_gnsrecord_credential.c b/src/credential/plugin_gnsrecord_credential.c
index c03f2b3e4..9207aa7ad 100644
--- a/src/credential/plugin_gnsrecord_credential.c
+++ b/src/credential/plugin_gnsrecord_credential.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file credential/plugin_gnsrecord_credential.c 22 * @file credential/plugin_gnsrecord_credential.c
@@ -43,72 +43,85 @@
43 * @return NULL on error, otherwise human-readable representation of the value 43 * @return NULL on error, otherwise human-readable representation of the value
44 */ 44 */
45static char * 45static char *
46credential_value_to_string (void *cls, uint32_t type, const void *data, 46credential_value_to_string(void *cls, uint32_t type, const void *data,
47 size_t data_size) 47 size_t data_size)
48{ 48{
49
50 const char *cdata; 49 const char *cdata;
51 50
52 switch (type) { 51 switch (type)
53 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: { 52 {
54 struct GNUNET_CREDENTIAL_DelegationRecord sets; 53 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
55 char *attr_str; 54 struct GNUNET_CREDENTIAL_DelegationRecord sets;
56 char *subject_pkey; 55 char *attr_str;
57 char *tmp_str; 56 char *subject_pkey;
58 int i; 57 char *tmp_str;
59 if (data_size < sizeof (struct GNUNET_CREDENTIAL_DelegationRecord)) 58 int i;
60 return NULL; /* malformed */ 59 if (data_size < sizeof(struct GNUNET_CREDENTIAL_DelegationRecord))
61 GNUNET_memcpy (&sets, data, sizeof (sets)); 60 return NULL; /* malformed */
62 cdata = data; 61 GNUNET_memcpy(&sets, data, sizeof(sets));
63 struct GNUNET_CREDENTIAL_DelegationSet set[ntohl (sets.set_count)]; 62 cdata = data;
64 if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize ( 63 struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets.set_count)];
65 GNUNET_ntohll (sets.data_size), &cdata[sizeof (sets)], 64 if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize(
66 ntohl (sets.set_count), set)) 65 GNUNET_ntohll(sets.data_size), &cdata[sizeof(sets)],
67 return NULL; 66 ntohl(sets.set_count), set))
68 67 return NULL;
69 for (i = 0; i < ntohl (sets.set_count); i++) { 68
70 subject_pkey = 69 for (i = 0; i < ntohl(sets.set_count); i++)
71 GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key); 70 {
72 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d len attr\n", 71 subject_pkey =
73 set[i].subject_attribute_len); 72 GNUNET_CRYPTO_ecdsa_public_key_to_string(&set[i].subject_key);
74 if (0 == set[i].subject_attribute_len) { 73 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%d len attr\n",
75 if (0 == i) { 74 set[i].subject_attribute_len);
76 GNUNET_asprintf (&attr_str, "%s", subject_pkey); 75 if (0 == set[i].subject_attribute_len)
77 } else { 76 {
78 GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey); 77 if (0 == i)
79 GNUNET_free (attr_str); 78 {
80 attr_str = tmp_str; 79 GNUNET_asprintf(&attr_str, "%s", subject_pkey);
80 }
81 else
82 {
83 GNUNET_asprintf(&tmp_str, "%s,%s", attr_str, subject_pkey);
84 GNUNET_free(attr_str);
85 attr_str = tmp_str;
86 }
87 }
88 else
89 {
90 if (0 == i)
91 {
92 GNUNET_asprintf(&attr_str, "%s %s", subject_pkey,
93 set[i].subject_attribute);
94 }
95 else
96 {
97 GNUNET_asprintf(&tmp_str, "%s,%s %s", attr_str, subject_pkey,
98 set[i].subject_attribute);
99 GNUNET_free(attr_str);
100 attr_str = tmp_str;
101 }
102 }
103 GNUNET_free(subject_pkey);
81 } 104 }
82 } else { 105 return attr_str;
83 if (0 == i) { 106 }
84 GNUNET_asprintf (&attr_str, "%s %s", subject_pkey, 107
85 set[i].subject_attribute); 108 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
86 } else { 109 struct GNUNET_CREDENTIAL_Credential *cred;
87 GNUNET_asprintf (&tmp_str, "%s,%s %s", attr_str, subject_pkey, 110 char *cred_str;
88 set[i].subject_attribute); 111
89 GNUNET_free (attr_str); 112 cred = GNUNET_CREDENTIAL_credential_deserialize(data, data_size);
90 attr_str = tmp_str; 113 cred_str = GNUNET_CREDENTIAL_credential_to_string(cred);
91 } 114 GNUNET_free(cred);
92 } 115 return cred_str;
93 GNUNET_free (subject_pkey); 116 }
117
118 case GNUNET_GNSRECORD_TYPE_POLICY: {
119 return GNUNET_strndup(data, data_size);
120 }
121
122 default:
123 return NULL;
94 } 124 }
95 return attr_str;
96 }
97 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
98 struct GNUNET_CREDENTIAL_Credential *cred;
99 char *cred_str;
100
101 cred = GNUNET_CREDENTIAL_credential_deserialize (data, data_size);
102 cred_str = GNUNET_CREDENTIAL_credential_to_string (cred);
103 GNUNET_free (cred);
104 return cred_str;
105 }
106 case GNUNET_GNSRECORD_TYPE_POLICY: {
107 return GNUNET_strndup (data, data_size);
108 }
109 default:
110 return NULL;
111 }
112} 125}
113 126
114 127
@@ -124,100 +137,114 @@ credential_value_to_string (void *cls, uint32_t type, const void *data,
124 * @return #GNUNET_OK on success 137 * @return #GNUNET_OK on success
125 */ 138 */
126static int 139static int
127credential_string_to_value (void *cls, uint32_t type, const char *s, 140credential_string_to_value(void *cls, uint32_t type, const char *s,
128 void **data, size_t *data_size) 141 void **data, size_t *data_size)
129{ 142{
130 if (NULL == s) 143 if (NULL == s)
131 return GNUNET_SYSERR; 144 return GNUNET_SYSERR;
132 switch (type) { 145 switch (type)
133 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: { 146 {
134 struct GNUNET_CREDENTIAL_DelegationRecord *sets; 147 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
135 char attr_str[253 + 1]; 148 struct GNUNET_CREDENTIAL_DelegationRecord *sets;
136 char subject_pkey[52 + 1]; 149 char attr_str[253 + 1];
137 char *token; 150 char subject_pkey[52 + 1];
138 char *tmp_str; 151 char *token;
139 int matches = 0; 152 char *tmp_str;
140 int entries; 153 int matches = 0;
141 size_t tmp_data_size; 154 int entries;
142 int i; 155 size_t tmp_data_size;
143 156 int i;
144 tmp_str = GNUNET_strdup (s); 157
145 token = strtok (tmp_str, ","); 158 tmp_str = GNUNET_strdup(s);
146 entries = 0; 159 token = strtok(tmp_str, ",");
147 tmp_data_size = 0; 160 entries = 0;
148 *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord); 161 tmp_data_size = 0;
149 while (NULL != token) { 162 *data_size = sizeof(struct GNUNET_CREDENTIAL_DelegationRecord);
150 matches = SSCANF (token, "%s %s", subject_pkey, attr_str); 163 while (NULL != token)
151 if (0 == matches) { 164 {
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 165 matches = SSCANF(token, "%s %s", subject_pkey, attr_str);
153 _ ("Unable to parse ATTR record string `%s'\n"), s); 166 if (0 == matches)
154 GNUNET_free (tmp_str); 167 {
155 return GNUNET_SYSERR; 168 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
156 } 169 _("Unable to parse ATTR record string `%s'\n"), s);
157 if (1 == matches) { 170 GNUNET_free(tmp_str);
158 tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet); 171 return GNUNET_SYSERR;
159 } else if (2 == matches) { 172 }
160 tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet) + 173 if (1 == matches)
161 strlen (attr_str) + 1; 174 {
162 } 175 tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet);
163 entries++; 176 }
164 token = strtok (NULL, ","); 177 else if (2 == matches)
178 {
179 tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet) +
180 strlen(attr_str) + 1;
181 }
182 entries++;
183 token = strtok(NULL, ",");
184 }
185 GNUNET_free(tmp_str);
186 tmp_str = GNUNET_strdup(s);
187 token = strtok(tmp_str, ",");
188 if (NULL == token)
189 {
190 GNUNET_free(tmp_str);
191 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
192 return GNUNET_SYSERR;
193 }
194 struct GNUNET_CREDENTIAL_DelegationSet set[entries];
195 memset(set, 0, sizeof(struct GNUNET_CREDENTIAL_DelegationSet) * entries);
196 for (i = 0; i < entries; i++)
197 {
198 matches = SSCANF(token, "%s %s", subject_pkey, attr_str);
199 GNUNET_CRYPTO_ecdsa_public_key_from_string(
200 subject_pkey, strlen(subject_pkey), &set[i].subject_key);
201 if (2 == matches)
202 {
203 set[i].subject_attribute_len = strlen(attr_str) + 1;
204 set[i].subject_attribute = GNUNET_strdup(attr_str);
205 }
206 token = strtok(NULL, ",");
207 }
208 tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size(entries, set);
209
210 if (-1 == tmp_data_size)
211 {
212 GNUNET_free(tmp_str);
213 return GNUNET_SYSERR;
214 }
215 *data_size += tmp_data_size;
216 *data = sets = GNUNET_malloc(*data_size);
217 GNUNET_CREDENTIAL_delegation_set_serialize(entries, set, tmp_data_size,
218 (char *)&sets[1]);
219 for (i = 0; i < entries; i++)
220 {
221 if (0 != set[i].subject_attribute_len)
222 GNUNET_free((char *)set[i].subject_attribute);
223 }
224 sets->set_count = htonl(entries);
225 sets->data_size = GNUNET_htonll(tmp_data_size);
226
227 GNUNET_free(tmp_str);
228 return GNUNET_OK;
165 } 229 }
166 GNUNET_free (tmp_str); 230
167 tmp_str = GNUNET_strdup (s); 231 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
168 token = strtok (tmp_str, ","); 232 struct GNUNET_CREDENTIAL_Credential *cred;
169 if (NULL == token) { 233 cred = GNUNET_CREDENTIAL_credential_from_string(s);
170 GNUNET_free (tmp_str); 234
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s); 235 *data_size = GNUNET_CREDENTIAL_credential_serialize(cred, (char **)data);
172 return GNUNET_SYSERR; 236 return GNUNET_OK;
173 } 237 }
174 struct GNUNET_CREDENTIAL_DelegationSet set[entries]; 238
175 memset (set, 0, sizeof (struct GNUNET_CREDENTIAL_DelegationSet) * entries); 239 case GNUNET_GNSRECORD_TYPE_POLICY: {
176 for (i = 0; i < entries; i++) { 240 *data_size = strlen(s);
177 matches = SSCANF (token, "%s %s", subject_pkey, attr_str); 241 *data = GNUNET_strdup(s);
178 GNUNET_CRYPTO_ecdsa_public_key_from_string ( 242 return GNUNET_OK;
179 subject_pkey, strlen (subject_pkey), &set[i].subject_key);
180 if (2 == matches) {
181 set[i].subject_attribute_len = strlen (attr_str) + 1;
182 set[i].subject_attribute = GNUNET_strdup (attr_str);
183 }
184 token = strtok (NULL, ",");
185 } 243 }
186 tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries, set);
187 244
188 if (-1 == tmp_data_size) { 245 default:
189 GNUNET_free (tmp_str);
190 return GNUNET_SYSERR; 246 return GNUNET_SYSERR;
191 } 247 }
192 *data_size += tmp_data_size;
193 *data = sets = GNUNET_malloc (*data_size);
194 GNUNET_CREDENTIAL_delegation_set_serialize (entries, set, tmp_data_size,
195 (char *)&sets[1]);
196 for (i = 0; i < entries; i++) {
197 if (0 != set[i].subject_attribute_len)
198 GNUNET_free ((char *)set[i].subject_attribute);
199 }
200 sets->set_count = htonl (entries);
201 sets->data_size = GNUNET_htonll (tmp_data_size);
202
203 GNUNET_free (tmp_str);
204 return GNUNET_OK;
205 }
206 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
207 struct GNUNET_CREDENTIAL_Credential *cred;
208 cred = GNUNET_CREDENTIAL_credential_from_string (s);
209
210 *data_size = GNUNET_CREDENTIAL_credential_serialize (cred, (char **)data);
211 return GNUNET_OK;
212 }
213 case GNUNET_GNSRECORD_TYPE_POLICY: {
214 *data_size = strlen (s);
215 *data = GNUNET_strdup (s);
216 return GNUNET_OK;
217 }
218 default:
219 return GNUNET_SYSERR;
220 }
221} 248}
222 249
223 250
@@ -225,14 +252,13 @@ credential_string_to_value (void *cls, uint32_t type, const char *s,
225 * Mapping of record type numbers to human-readable 252 * Mapping of record type numbers to human-readable
226 * record type names. 253 * record type names.
227 */ 254 */
228static struct 255static struct {
229{
230 const char *name; 256 const char *name;
231 uint32_t number; 257 uint32_t number;
232} name_map[] = {{"CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL}, 258} name_map[] = { { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
233 {"ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE}, 259 { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
234 {"POLICY", GNUNET_GNSRECORD_TYPE_POLICY}, 260 { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY },
235 {NULL, UINT32_MAX}}; 261 { NULL, UINT32_MAX } };
236 262
237 263
238/** 264/**
@@ -243,13 +269,13 @@ static struct
243 * @return corresponding number, UINT32_MAX on error 269 * @return corresponding number, UINT32_MAX on error
244 */ 270 */
245static uint32_t 271static uint32_t
246credential_typename_to_number (void *cls, const char *gns_typename) 272credential_typename_to_number(void *cls, const char *gns_typename)
247{ 273{
248 unsigned int i; 274 unsigned int i;
249 275
250 i = 0; 276 i = 0;
251 while ((name_map[i].name != NULL) && 277 while ((name_map[i].name != NULL) &&
252 (0 != strcasecmp (gns_typename, name_map[i].name))) 278 (0 != strcasecmp(gns_typename, name_map[i].name)))
253 i++; 279 i++;
254 return name_map[i].number; 280 return name_map[i].number;
255} 281}
@@ -263,7 +289,7 @@ credential_typename_to_number (void *cls, const char *gns_typename)
263 * @return corresponding typestring, NULL on error 289 * @return corresponding typestring, NULL on error
264 */ 290 */
265static const char * 291static const char *
266credential_number_to_typename (void *cls, uint32_t type) 292credential_number_to_typename(void *cls, uint32_t type)
267{ 293{
268 unsigned int i; 294 unsigned int i;
269 295
@@ -281,11 +307,11 @@ credential_number_to_typename (void *cls, uint32_t type)
281 * @return the exported block API 307 * @return the exported block API
282 */ 308 */
283void * 309void *
284libgnunet_plugin_gnsrecord_credential_init (void *cls) 310libgnunet_plugin_gnsrecord_credential_init(void *cls)
285{ 311{
286 struct GNUNET_GNSRECORD_PluginFunctions *api; 312 struct GNUNET_GNSRECORD_PluginFunctions *api;
287 313
288 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions); 314 api = GNUNET_new(struct GNUNET_GNSRECORD_PluginFunctions);
289 api->value_to_string = &credential_value_to_string; 315 api->value_to_string = &credential_value_to_string;
290 api->string_to_value = &credential_string_to_value; 316 api->string_to_value = &credential_string_to_value;
291 api->typename_to_number = &credential_typename_to_number; 317 api->typename_to_number = &credential_typename_to_number;
@@ -301,11 +327,11 @@ libgnunet_plugin_gnsrecord_credential_init (void *cls)
301 * @return NULL 327 * @return NULL
302 */ 328 */
303void * 329void *
304libgnunet_plugin_gnsrecord_credential_done (void *cls) 330libgnunet_plugin_gnsrecord_credential_done(void *cls)
305{ 331{
306 struct GNUNET_GNSRECORD_PluginFunctions *api = cls; 332 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
307 333
308 GNUNET_free (api); 334 GNUNET_free(api);
309 return NULL; 335 return NULL;
310} 336}
311 337