diff options
author | Markus Voggenreiter <Markus.Voggenreiter@tum.de> | 2019-10-09 21:10:14 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2020-01-13 13:31:01 +0100 |
commit | e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c (patch) | |
tree | 746c6b899bc5fdc491ad477463dda3b56295304e | |
parent | 60d2660de243053bc0f41657ad9d67537723276c (diff) | |
download | gnunet-e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c.tar.gz gnunet-e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c.zip |
Adapted JSON Conversion and Serialization
-rw-r--r-- | src/include/gnunet_reclaim_attribute_lib.h | 144 | ||||
-rw-r--r-- | src/include/gnunet_reclaim_attribute_plugin.h | 21 | ||||
-rw-r--r-- | src/reclaim-attribute/plugin_reclaim_attribute_gnuid.c | 110 | ||||
-rw-r--r-- | src/reclaim-attribute/reclaim_attribute.c | 242 | ||||
-rw-r--r-- | src/reclaim-attribute/reclaim_attribute.h | 33 | ||||
-rw-r--r-- | src/reclaim/json_reclaim.c | 110 | ||||
-rw-r--r-- | src/reclaim/json_reclaim.h | 10 |
7 files changed, 670 insertions, 0 deletions
diff --git a/src/include/gnunet_reclaim_attribute_lib.h b/src/include/gnunet_reclaim_attribute_lib.h index 4563a5f67..c23b39383 100644 --- a/src/include/gnunet_reclaim_attribute_lib.h +++ b/src/include/gnunet_reclaim_attribute_lib.h | |||
@@ -50,6 +50,15 @@ extern "C" { | |||
50 | */ | 50 | */ |
51 | #define GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING 1 | 51 | #define GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING 1 |
52 | 52 | ||
53 | /** | ||
54 | * No value attestation. | ||
55 | */ | ||
56 | #define GNUNET_RECLAIM_ATTESTATION_TYPE_NONE 0 | ||
57 | |||
58 | /** | ||
59 | * A JSON Web Token attestation. | ||
60 | */ | ||
61 | #define GNUNET_RECLAIM_ATTESTATION_TYPE_JWT 1 | ||
53 | 62 | ||
54 | /** | 63 | /** |
55 | * An attribute. | 64 | * An attribute. |
@@ -89,6 +98,44 @@ struct GNUNET_RECLAIM_ATTRIBUTE_Claim | |||
89 | const void *data; | 98 | const void *data; |
90 | }; | 99 | }; |
91 | 100 | ||
101 | /** | ||
102 | * An attestation. | ||
103 | */ | ||
104 | struct GNUNET_RECLAIM_ATTESTATION_Claim | ||
105 | { | ||
106 | /** | ||
107 | * ID | ||
108 | */ | ||
109 | uint64_t id; | ||
110 | |||
111 | /** | ||
112 | * Type/Format of Claim | ||
113 | */ | ||
114 | uint32_t type; | ||
115 | |||
116 | /** | ||
117 | * Version | ||
118 | */ | ||
119 | uint32_t version; | ||
120 | |||
121 | /** | ||
122 | * The name of the attribute. Note "name" must never be individually | ||
123 | * free'd | ||
124 | */ | ||
125 | const char *name; | ||
126 | |||
127 | /** | ||
128 | * Number of bytes in @e data. | ||
129 | */ | ||
130 | size_t data_size; | ||
131 | |||
132 | /** | ||
133 | * Binary value stored as attribute value. Note: "data" must never | ||
134 | * be individually 'malloc'ed, but instead always points into some | ||
135 | * existing data area. | ||
136 | */ | ||
137 | const void *data; | ||
138 | }; | ||
92 | 139 | ||
93 | /** | 140 | /** |
94 | * A list of GNUNET_RECLAIM_ATTRIBUTE_Claim structures. | 141 | * A list of GNUNET_RECLAIM_ATTRIBUTE_Claim structures. |
@@ -299,6 +346,103 @@ GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, | |||
299 | const char * | 346 | const char * |
300 | GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type); | 347 | GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type); |
301 | 348 | ||
349 | /** | ||
350 | * Get required size for serialization buffer | ||
351 | * | ||
352 | * @param attr the attestation to serialize | ||
353 | * @return the required buffer size | ||
354 | */ | ||
355 | size_t | ||
356 | GNUNET_RECLAIM_ATTESTATION_serialize_get_size ( | ||
357 | const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr); | ||
358 | |||
359 | |||
360 | /** | ||
361 | * Serialize an attestation | ||
362 | * | ||
363 | * @param attr the attestation to serialize | ||
364 | * @param result the serialized attestation | ||
365 | * @return length of serialized data | ||
366 | */ | ||
367 | size_t | ||
368 | GNUNET_RECLAIM_ATTESTATION_serialize ( | ||
369 | const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr, | ||
370 | char *result); | ||
371 | |||
372 | |||
373 | /** | ||
374 | * Deserialize an attestation | ||
375 | * | ||
376 | * @param data the serialized attestation | ||
377 | * @param data_size the length of the serialized data | ||
378 | * | ||
379 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
380 | */ | ||
381 | struct GNUNET_RECLAIM_ATTESTATION_Claim * | ||
382 | GNUNET_RECLAIM_ATTESTATION_deserialize (const char *data, size_t data_size); | ||
383 | |||
384 | |||
385 | /** | ||
386 | * Create a new attestation. | ||
387 | * | ||
388 | * @param attr_name the attestation name | ||
389 | * @param type the attestation type | ||
390 | * @param data the attestation value | ||
391 | * @param data_size the attestation value size | ||
392 | * @return the new attestation | ||
393 | */ | ||
394 | struct GNUNET_RECLAIM_ATTESTATION_Claim * | ||
395 | GNUNET_RECLAIM_ATTESTATION_claim_new (const char *attr_name, | ||
396 | uint32_t type, | ||
397 | const void *data, | ||
398 | size_t data_size); | ||
399 | |||
400 | /** | ||
401 | * Convert the 'claim' of an attestation to a string | ||
402 | * | ||
403 | * @param type the type of attestation | ||
404 | * @param data claim in binary encoding | ||
405 | * @param data_size number of bytes in @a data | ||
406 | * @return NULL on error, otherwise human-readable representation of the claim | ||
407 | */ | ||
408 | char * | ||
409 | GNUNET_RECLAIM_ATTESTATION_value_to_string (uint32_t type, | ||
410 | const void *data, | ||
411 | size_t data_size); | ||
412 | |||
413 | /** | ||
414 | * Convert human-readable version of a 'claim' of an attestation to the binary | ||
415 | * representation | ||
416 | * | ||
417 | * @param type type of the claim | ||
418 | * @param s human-readable string | ||
419 | * @param data set to value in binary encoding (will be allocated) | ||
420 | * @param data_size set to number of bytes in @a data | ||
421 | * @return #GNUNET_OK on success | ||
422 | */ | ||
423 | int | ||
424 | GNUNET_RECLAIM_ATTESTATION_string_to_value (uint32_t type, | ||
425 | const char *s, | ||
426 | void **data, | ||
427 | size_t *data_size); | ||
428 | |||
429 | /** | ||
430 | * Convert an attestation type number to the corresponding attestation type string | ||
431 | * | ||
432 | * @param type number of a type | ||
433 | * @return corresponding typestring, NULL on error | ||
434 | */ | ||
435 | const char * | ||
436 | GNUNET_RECLAIM_ATTESTATION_number_to_typename (uint32_t type); | ||
437 | |||
438 | /** | ||
439 | * Convert an attestation type name to the corresponding number | ||
440 | * | ||
441 | * @param typename name to convert | ||
442 | * @return corresponding number, UINT32_MAX on error | ||
443 | */ | ||
444 | uint32_t | ||
445 | GNUNET_RECLAIM_ATTESTATION_typename_to_number (const char *typename); | ||
302 | 446 | ||
303 | #if 0 /* keep Emacsens' auto-indent happy */ | 447 | #if 0 /* keep Emacsens' auto-indent happy */ |
304 | { | 448 | { |
diff --git a/src/include/gnunet_reclaim_attribute_plugin.h b/src/include/gnunet_reclaim_attribute_plugin.h index 26a4bb4f2..bdd815dcf 100644 --- a/src/include/gnunet_reclaim_attribute_plugin.h +++ b/src/include/gnunet_reclaim_attribute_plugin.h | |||
@@ -134,6 +134,27 @@ struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions | |||
134 | * Number to typename. | 134 | * Number to typename. |
135 | */ | 135 | */ |
136 | GNUNET_RECLAIM_ATTRIBUTE_NumberToTypenameFunction number_to_typename; | 136 | GNUNET_RECLAIM_ATTRIBUTE_NumberToTypenameFunction number_to_typename; |
137 | |||
138 | /** | ||
139 | * Attestation Conversion to string. | ||
140 | */ | ||
141 | GNUNET_RECLAIM_ATTRIBUTE_ValueToStringFunction value_to_string_attest; | ||
142 | |||
143 | /** | ||
144 | * Attestation Conversion to binary. | ||
145 | */ | ||
146 | GNUNET_RECLAIM_ATTRIBUTE_StringToValueFunction string_to_value_attest; | ||
147 | |||
148 | /** | ||
149 | * Attestation Typename to number. | ||
150 | */ | ||
151 | GNUNET_RECLAIM_ATTRIBUTE_TypenameToNumberFunction typename_to_number_attest; | ||
152 | |||
153 | /** | ||
154 | * Attestation Number to typename. | ||
155 | */ | ||
156 | GNUNET_RECLAIM_ATTRIBUTE_NumberToTypenameFunction number_to_typename_attest; | ||
157 | |||
137 | }; | 158 | }; |
138 | 159 | ||
139 | 160 | ||
diff --git a/src/reclaim-attribute/plugin_reclaim_attribute_gnuid.c b/src/reclaim-attribute/plugin_reclaim_attribute_gnuid.c index bb60909d9..37610e2fb 100644 --- a/src/reclaim-attribute/plugin_reclaim_attribute_gnuid.c +++ b/src/reclaim-attribute/plugin_reclaim_attribute_gnuid.c | |||
@@ -90,6 +90,63 @@ gnuid_string_to_value (void *cls, | |||
90 | } | 90 | } |
91 | } | 91 | } |
92 | 92 | ||
93 | /** | ||
94 | * Convert the 'value' of an attestation to a string. | ||
95 | * | ||
96 | * @param cls closure, unused | ||
97 | * @param type type of the attestation | ||
98 | * @param data value in binary encoding | ||
99 | * @param data_size number of bytes in @a data | ||
100 | * @return NULL on error, otherwise human-readable representation of the value | ||
101 | */ | ||
102 | static char * | ||
103 | gnuid_value_to_string_attest (void *cls, | ||
104 | uint32_t type, | ||
105 | const void *data, | ||
106 | size_t data_size) | ||
107 | { | ||
108 | switch (type) | ||
109 | { | ||
110 | case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT: | ||
111 | return GNUNET_strndup (data, data_size); | ||
112 | |||
113 | default: | ||
114 | return NULL; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Convert human-readable version of a 'value' of an attestation to the binary | ||
121 | * representation. | ||
122 | * | ||
123 | * @param cls closure, unused | ||
124 | * @param type type of the attestation | ||
125 | * @param s human-readable string | ||
126 | * @param data set to value in binary encoding (will be allocated) | ||
127 | * @param data_size set to number of bytes in @a data | ||
128 | * @return #GNUNET_OK on success | ||
129 | */ | ||
130 | static int | ||
131 | gnuid_string_to_value_attest (void *cls, | ||
132 | uint32_t type, | ||
133 | const char *s, | ||
134 | void **data, | ||
135 | size_t *data_size) | ||
136 | { | ||
137 | if (NULL == s) | ||
138 | return GNUNET_SYSERR; | ||
139 | switch (type) | ||
140 | { | ||
141 | case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT: | ||
142 | *data = GNUNET_strdup (s); | ||
143 | *data_size = strlen (s); | ||
144 | return GNUNET_OK; | ||
145 | |||
146 | default: | ||
147 | return GNUNET_SYSERR; | ||
148 | } | ||
149 | } | ||
93 | 150 | ||
94 | /** | 151 | /** |
95 | * Mapping of attribute type numbers to human-readable | 152 | * Mapping of attribute type numbers to human-readable |
@@ -102,6 +159,17 @@ static struct | |||
102 | } gnuid_name_map[] = { { "STRING", GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING }, | 159 | } gnuid_name_map[] = { { "STRING", GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING }, |
103 | { NULL, UINT32_MAX } }; | 160 | { NULL, UINT32_MAX } }; |
104 | 161 | ||
162 | /** | ||
163 | * Mapping of attribute type numbers to human-readable | ||
164 | * attribute type names. | ||
165 | */ | ||
166 | static struct | ||
167 | { | ||
168 | const char *name; | ||
169 | uint32_t number; | ||
170 | } gnuid_attest_name_map[] = { { "STRING", | ||
171 | GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING }, | ||
172 | { NULL, UINT32_MAX } }; | ||
105 | 173 | ||
106 | /** | 174 | /** |
107 | * Convert a type name to the corresponding number. | 175 | * Convert a type name to the corresponding number. |
@@ -141,6 +209,44 @@ gnuid_number_to_typename (void *cls, uint32_t type) | |||
141 | return gnuid_name_map[i].name; | 209 | return gnuid_name_map[i].name; |
142 | } | 210 | } |
143 | 211 | ||
212 | /** | ||
213 | * Convert a type name to the corresponding number. | ||
214 | * | ||
215 | * @param cls closure, unused | ||
216 | * @param gnuid_typename name to convert | ||
217 | * @return corresponding number, UINT32_MAX on error | ||
218 | */ | ||
219 | static uint32_t | ||
220 | gnuid_typename_to_number_attest (void *cls, const char *gnuid_typename) | ||
221 | { | ||
222 | unsigned int i; | ||
223 | |||
224 | i = 0; | ||
225 | while ((NULL != gnuid_attest_name_map[i].name) && | ||
226 | (0 != strcasecmp (gnuid_typename, gnuid_attest_name_map[i].name))) | ||
227 | i++; | ||
228 | return gnuid_attest_name_map[i].number; | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * Convert a type number (i.e. 1) to the corresponding type string | ||
233 | * | ||
234 | * @param cls closure, unused | ||
235 | * @param type number of a type to convert | ||
236 | * @return corresponding typestring, NULL on error | ||
237 | */ | ||
238 | static const char * | ||
239 | gnuid_number_to_typename (void *cls, uint32_t type) | ||
240 | { | ||
241 | unsigned int i; | ||
242 | |||
243 | i = 0; | ||
244 | while ((NULL != gnuid_attest_name_map[i].name) && (type != | ||
245 | gnuid_attest_name_map[i]. | ||
246 | number)) | ||
247 | i++; | ||
248 | return gnuid_attest_name_map[i].name; | ||
249 | } | ||
144 | 250 | ||
145 | /** | 251 | /** |
146 | * Entry point for the plugin. | 252 | * Entry point for the plugin. |
@@ -158,6 +264,10 @@ libgnunet_plugin_reclaim_attribute_gnuid_init (void *cls) | |||
158 | api->string_to_value = &gnuid_string_to_value; | 264 | api->string_to_value = &gnuid_string_to_value; |
159 | api->typename_to_number = &gnuid_typename_to_number; | 265 | api->typename_to_number = &gnuid_typename_to_number; |
160 | api->number_to_typename = &gnuid_number_to_typename; | 266 | api->number_to_typename = &gnuid_number_to_typename; |
267 | api->value_to_string_attest = &gnuid_value_to_string_attest; | ||
268 | api->string_to_value_attest = &gnuid_string_to_value_attest; | ||
269 | api->typename_to_number_attest = &gnuid_typename_to_number_attest; | ||
270 | api->number_to_typename_attest = &gnuid_number_to_typename_attest; | ||
161 | return api; | 271 | return api; |
162 | } | 272 | } |
163 | 273 | ||
diff --git a/src/reclaim-attribute/reclaim_attribute.c b/src/reclaim-attribute/reclaim_attribute.c index b81394ad2..6dd1632fd 100644 --- a/src/reclaim-attribute/reclaim_attribute.c +++ b/src/reclaim-attribute/reclaim_attribute.c | |||
@@ -217,6 +217,117 @@ GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, | |||
217 | return NULL; | 217 | return NULL; |
218 | } | 218 | } |
219 | 219 | ||
220 | /** | ||
221 | * Convert an attestation type name to the corresponding number | ||
222 | * | ||
223 | * @param typename name to convert | ||
224 | * @return corresponding number, UINT32_MAX on error | ||
225 | */ | ||
226 | uint32_t | ||
227 | GNUNET_RECLAIM_ATTESTATION_typename_to_number (const char *typename) | ||
228 | { | ||
229 | unsigned int i; | ||
230 | struct Plugin *plugin; | ||
231 | uint32_t ret; | ||
232 | |||
233 | init (); | ||
234 | for (i = 0; i < num_plugins; i++) | ||
235 | { | ||
236 | plugin = attr_plugins[i]; | ||
237 | if (UINT32_MAX != | ||
238 | (ret = plugin->api->typename_to_number_attest (plugin->api->cls, | ||
239 | typename))) | ||
240 | return ret; | ||
241 | } | ||
242 | return UINT32_MAX; | ||
243 | } | ||
244 | |||
245 | /** | ||
246 | * Convert an attestation type number to the corresponding attestation type string | ||
247 | * | ||
248 | * @param type number of a type | ||
249 | * @return corresponding typestring, NULL on error | ||
250 | */ | ||
251 | const char * | ||
252 | GNUNET_RECLAIM_ATTESTATION_number_to_typename (uint32_t type) | ||
253 | { | ||
254 | unsigned int i; | ||
255 | struct Plugin *plugin; | ||
256 | const char *ret; | ||
257 | |||
258 | init (); | ||
259 | for (i = 0; i < num_plugins; i++) | ||
260 | { | ||
261 | plugin = attr_plugins[i]; | ||
262 | if (NULL != | ||
263 | (ret = plugin->api->number_to_typename_attest (plugin->api->cls, type))) | ||
264 | return ret; | ||
265 | } | ||
266 | return NULL; | ||
267 | } | ||
268 | /** | ||
269 | * Convert human-readable version of a 'claim' of an attestation to the binary | ||
270 | * representation | ||
271 | * | ||
272 | * @param type type of the claim | ||
273 | * @param s human-readable string | ||
274 | * @param data set to value in binary encoding (will be allocated) | ||
275 | * @param data_size set to number of bytes in @a data | ||
276 | * @return #GNUNET_OK on success | ||
277 | */ | ||
278 | int | ||
279 | GNUNET_RECLAIM_ATTESTATION_string_to_value (uint32_t type, | ||
280 | const char *s, | ||
281 | void **data, | ||
282 | size_t *data_size) | ||
283 | { | ||
284 | unsigned int i; | ||
285 | struct Plugin *plugin; | ||
286 | |||
287 | init (); | ||
288 | for (i = 0; i < num_plugins; i++) | ||
289 | { | ||
290 | plugin = attr_plugins[i]; | ||
291 | if (GNUNET_OK == plugin->api->string_to_value_attest (plugin->api->cls, | ||
292 | type, | ||
293 | s, | ||
294 | data, | ||
295 | data_size)) | ||
296 | return GNUNET_OK; | ||
297 | } | ||
298 | return GNUNET_SYSERR; | ||
299 | } | ||
300 | |||
301 | |||
302 | /** | ||
303 | * Convert the 'claim' of an attestation to a string | ||
304 | * | ||
305 | * @param type the type of attestation | ||
306 | * @param data claim in binary encoding | ||
307 | * @param data_size number of bytes in @a data | ||
308 | * @return NULL on error, otherwise human-readable representation of the claim | ||
309 | */ | ||
310 | char * | ||
311 | GNUNET_RECLAIM_ATTESTATION_value_to_string (uint32_t type, | ||
312 | const void *data, | ||
313 | size_t data_size) | ||
314 | { | ||
315 | unsigned int i; | ||
316 | struct Plugin *plugin; | ||
317 | char *ret; | ||
318 | |||
319 | init (); | ||
320 | for (i = 0; i < num_plugins; i++) | ||
321 | { | ||
322 | plugin = attr_plugins[i]; | ||
323 | if (NULL != (ret = plugin->api->value_to_string_attest (plugin->api->cls, | ||
324 | type, | ||
325 | data, | ||
326 | data_size))) | ||
327 | return ret; | ||
328 | } | ||
329 | return NULL; | ||
330 | } | ||
220 | 331 | ||
221 | /** | 332 | /** |
222 | * Create a new attribute. | 333 | * Create a new attribute. |
@@ -254,6 +365,41 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name, | |||
254 | return attr; | 365 | return attr; |
255 | } | 366 | } |
256 | 367 | ||
368 | /** | ||
369 | * Create a new attestation. | ||
370 | * | ||
371 | * @param attr_name the attestation name | ||
372 | * @param type the attestation type | ||
373 | * @param data the attestation value | ||
374 | * @param data_size the attestation value size | ||
375 | * @return the new attestation | ||
376 | */ | ||
377 | struct GNUNET_RECLAIM_ATTESTATION_Claim * | ||
378 | GNUNET_RECLAIM_ATTESTATION_claim_new (const char *attr_name, | ||
379 | uint32_t type, | ||
380 | const void *data, | ||
381 | size_t data_size) | ||
382 | { | ||
383 | struct GNUNET_RECLAIM_ATTESTATION_Claim *attr; | ||
384 | char *write_ptr; | ||
385 | char *attr_name_tmp = GNUNET_strdup (attr_name); | ||
386 | |||
387 | GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); | ||
388 | |||
389 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim) | ||
390 | + strlen (attr_name_tmp) + 1 + data_size); | ||
391 | attr->type = type; | ||
392 | attr->data_size = data_size; | ||
393 | attr->version = 0; | ||
394 | write_ptr = (char *) &attr[1]; | ||
395 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | ||
396 | attr->name = write_ptr; | ||
397 | write_ptr += strlen (attr->name) + 1; | ||
398 | GNUNET_memcpy (write_ptr, data, data_size); | ||
399 | attr->data = write_ptr; | ||
400 | GNUNET_free (attr_name_tmp); | ||
401 | return attr; | ||
402 | } | ||
257 | 403 | ||
258 | /** | 404 | /** |
259 | * Add a new attribute to a claim list | 405 | * Add a new attribute to a claim list |
@@ -521,4 +667,100 @@ GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size) | |||
521 | } | 667 | } |
522 | 668 | ||
523 | 669 | ||
670 | /** | ||
671 | * Get required size for serialization buffer | ||
672 | * | ||
673 | * @param attr the attestation to serialize | ||
674 | * @return the required buffer size | ||
675 | */ | ||
676 | size_t | ||
677 | GNUNET_RECLAIM_ATTESTATION_serialize_get_size ( | ||
678 | const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr) | ||
679 | { | ||
680 | return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size; | ||
681 | } | ||
682 | |||
683 | /** | ||
684 | * Serialize an attestation | ||
685 | * | ||
686 | * @param attr the attestation to serialize | ||
687 | * @param result the serialized attestation | ||
688 | * @return length of serialized data | ||
689 | */ | ||
690 | size_t | ||
691 | GNUNET_RECLAIM_ATTESTATION_serialize ( | ||
692 | const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr, | ||
693 | char *result) | ||
694 | { | ||
695 | size_t data_len_ser; | ||
696 | size_t name_len; | ||
697 | struct Attestation *attr_ser; | ||
698 | char *write_ptr; | ||
699 | |||
700 | attr_ser = (struct Attestation *) result; | ||
701 | attr_ser->attestation_type = htons (attr->type); | ||
702 | attr_ser->attestation_type = htonl (attr->version); | ||
703 | attr_ser->attestation_type = GNUNET_htonll (attr->id); | ||
704 | name_len = strlen (attr->name); | ||
705 | attr_ser->name_len = htons (name_len); | ||
706 | write_ptr = (char *) &attr_ser[1]; | ||
707 | GNUNET_memcpy (write_ptr, attr->name, name_len); | ||
708 | write_ptr += name_len; | ||
709 | // TODO plugin-ize | ||
710 | // data_len_ser = plugin->serialize_attribute_value (attr, | ||
711 | // &attr_ser[1]); | ||
712 | data_len_ser = attr->data_size; | ||
713 | GNUNET_memcpy (write_ptr, attr->data, attr->data_size); | ||
714 | attr_ser->data_size = htons (data_len_ser); | ||
715 | |||
716 | return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size; | ||
717 | } | ||
718 | |||
719 | /** | ||
720 | * Deserialize an attestation | ||
721 | * | ||
722 | * @param data the serialized attestation | ||
723 | * @param data_size the length of the serialized data | ||
724 | * | ||
725 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
726 | */ | ||
727 | struct GNUNET_RECLAIM_ATTESTATION_Claim * | ||
728 | GNUNET_RECLAIM_ATTESTATION_deserialize (const char *data, size_t data_size) | ||
729 | { | ||
730 | struct GNUNET_RECLAIM_ATTESTATION_Claim *attr; | ||
731 | struct Attestation *attr_ser; | ||
732 | size_t data_len; | ||
733 | size_t name_len; | ||
734 | char *write_ptr; | ||
735 | |||
736 | if (data_size < sizeof(struct Attestation)) | ||
737 | return NULL; | ||
738 | |||
739 | attr_ser = (struct Attestation *) data; | ||
740 | data_len = ntohs (attr_ser->data_size); | ||
741 | name_len = ntohs (attr_ser->name_len); | ||
742 | if (data_size < sizeof(struct Attestation) + data_len + name_len) | ||
743 | { | ||
744 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
745 | "Buffer too small to deserialize\n"); | ||
746 | return NULL; | ||
747 | } | ||
748 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim) | ||
749 | + data_len + name_len + 1); | ||
750 | attr->type = ntohs (attr_ser->attestation_type); | ||
751 | attr->version = ntohl (attr_ser->attestation_version); | ||
752 | attr->id = GNUNET_ntohll (attr_ser->attestation_id); | ||
753 | attr->data_size = data_len; | ||
754 | |||
755 | write_ptr = (char *) &attr[1]; | ||
756 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); | ||
757 | write_ptr[name_len] = '\0'; | ||
758 | attr->name = write_ptr; | ||
759 | |||
760 | write_ptr += name_len + 1; | ||
761 | GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size); | ||
762 | attr->data = write_ptr; | ||
763 | return attr; | ||
764 | } | ||
765 | |||
524 | /* end of reclaim_attribute.c */ | 766 | /* end of reclaim_attribute.c */ |
diff --git a/src/reclaim-attribute/reclaim_attribute.h b/src/reclaim-attribute/reclaim_attribute.h index d7358847e..750afc479 100644 --- a/src/reclaim-attribute/reclaim_attribute.h +++ b/src/reclaim-attribute/reclaim_attribute.h | |||
@@ -61,4 +61,37 @@ struct Attribute | |||
61 | // followed by data_size Attribute value data | 61 | // followed by data_size Attribute value data |
62 | }; | 62 | }; |
63 | 63 | ||
64 | /** | ||
65 | * Serialized attestation claim | ||
66 | */ | ||
67 | struct Attestation | ||
68 | { | ||
69 | /** | ||
70 | * Attestation type | ||
71 | */ | ||
72 | uint32_t attestation_type; | ||
73 | |||
74 | /** | ||
75 | * Attestation version | ||
76 | */ | ||
77 | uint32_t attestation_version; | ||
78 | |||
79 | /** | ||
80 | * Attestation ID | ||
81 | */ | ||
82 | uint64_t attestation_id; | ||
83 | |||
84 | /** | ||
85 | * Name length | ||
86 | */ | ||
87 | uint32_t name_len; | ||
88 | |||
89 | /** | ||
90 | * Data size | ||
91 | */ | ||
92 | uint32_t data_size; | ||
93 | |||
94 | // followed by data_size Attestation value data | ||
95 | }; | ||
96 | |||
64 | #endif | 97 | #endif |
diff --git a/src/reclaim/json_reclaim.c b/src/reclaim/json_reclaim.c index e029fdfb6..a0016bac8 100644 --- a/src/reclaim/json_reclaim.c +++ b/src/reclaim/json_reclaim.c | |||
@@ -264,3 +264,113 @@ GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket) | |||
264 | *ticket = NULL; | 264 | *ticket = NULL; |
265 | return ret; | 265 | return ret; |
266 | } | 266 | } |
267 | |||
268 | /** | ||
269 | * Parse given JSON object to an attestation claim | ||
270 | * | ||
271 | * @param cls closure, NULL | ||
272 | * @param root the json object representing data | ||
273 | * @param spec where to write the data | ||
274 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
275 | */ | ||
276 | static int | ||
277 | parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) | ||
278 | { | ||
279 | struct GNUNET_RECLAIM_ATTESTATION_Claim *attr; | ||
280 | const char *name_str = NULL; | ||
281 | const char *val_str = NULL; | ||
282 | const char *type_str = NULL; | ||
283 | const char *id_str = NULL; | ||
284 | char *data; | ||
285 | int unpack_state; | ||
286 | uint32_t type; | ||
287 | size_t data_size; | ||
288 | |||
289 | GNUNET_assert (NULL != root); | ||
290 | |||
291 | if (! json_is_object (root)) | ||
292 | { | ||
293 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
294 | "Error json is not array nor object!\n"); | ||
295 | return GNUNET_SYSERR; | ||
296 | } | ||
297 | // interpret single attribute | ||
298 | unpack_state = json_unpack (root, | ||
299 | "{s:s, s?s, s:s, s:s!}", | ||
300 | "name", | ||
301 | &name_str, | ||
302 | "id", | ||
303 | &id_str, | ||
304 | "type", | ||
305 | &type_str, | ||
306 | "value", | ||
307 | &val_str); | ||
308 | if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) || | ||
309 | (NULL == type_str)) | ||
310 | { | ||
311 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
312 | "Error json object has a wrong format!\n"); | ||
313 | return GNUNET_SYSERR; | ||
314 | } | ||
315 | type = GNUNET_RECLAIM_ATTESTATION_typename_to_number (type_str); | ||
316 | if (GNUNET_SYSERR == | ||
317 | (GNUNET_RECLAIM_ATTESTATION_string_to_value (type, | ||
318 | val_str, | ||
319 | (void **) &data, | ||
320 | &data_size))) | ||
321 | { | ||
322 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n"); | ||
323 | return GNUNET_SYSERR; | ||
324 | } | ||
325 | attr = GNUNET_RECLAIM_ATTESTATION_claim_new (name_str, type, data, data_size); | ||
326 | if ((NULL == id_str) || (0 == strlen (id_str))) | ||
327 | attr->id = 0; | ||
328 | else | ||
329 | GNUNET_STRINGS_string_to_data (id_str, | ||
330 | strlen (id_str), | ||
331 | &attr->id, | ||
332 | sizeof(uint64_t)); | ||
333 | |||
334 | *(struct GNUNET_RECLAIM_ATTESTATION_Claim **) spec->ptr = attr; | ||
335 | return GNUNET_OK; | ||
336 | } | ||
337 | |||
338 | /** | ||
339 | * Cleanup data left from parsing RSA public key. | ||
340 | * | ||
341 | * @param cls closure, NULL | ||
342 | * @param[out] spec where to free the data | ||
343 | */ | ||
344 | static void | ||
345 | clean_attest (void *cls, struct GNUNET_JSON_Specification *spec) | ||
346 | { | ||
347 | struct GNUNET_RECLAIM_ATTESTATION_Claim **attr; | ||
348 | |||
349 | attr = (struct GNUNET_RECLAIM_ATTESTATION_Claim **) spec->ptr; | ||
350 | if (NULL != *attr) | ||
351 | { | ||
352 | GNUNET_free (*attr); | ||
353 | *attr = NULL; | ||
354 | } | ||
355 | } | ||
356 | /** | ||
357 | * JSON Specification for Reclaim attestation claims. | ||
358 | * | ||
359 | * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_Claim to fill | ||
360 | * @return JSON Specification | ||
361 | */ | ||
362 | struct GNUNET_JSON_Specification | ||
363 | GNUNET_RECLAIM_JSON_spec_claim_attest (struct | ||
364 | GNUNET_RECLAIM_ATTESTATION_Claim **attr) | ||
365 | { | ||
366 | struct GNUNET_JSON_Specification ret = { .parser = &parse_attest, | ||
367 | .cleaner = &clean_attest, | ||
368 | .cls = NULL, | ||
369 | .field = NULL, | ||
370 | .ptr = attr, | ||
371 | .ptr_size = 0, | ||
372 | .size_ptr = NULL }; | ||
373 | |||
374 | *attr = NULL; | ||
375 | return ret; | ||
376 | } | ||
diff --git a/src/reclaim/json_reclaim.h b/src/reclaim/json_reclaim.h index 3fd26167f..4280cce48 100644 --- a/src/reclaim/json_reclaim.h +++ b/src/reclaim/json_reclaim.h | |||
@@ -46,3 +46,13 @@ GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr); | |||
46 | */ | 46 | */ |
47 | struct GNUNET_JSON_Specification | 47 | struct GNUNET_JSON_Specification |
48 | GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket); | 48 | GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket); |
49 | |||
50 | /** | ||
51 | * JSON Specification for Reclaim attestation claims. | ||
52 | * | ||
53 | * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_Claim to fill | ||
54 | * @return JSON Specification | ||
55 | */ | ||
56 | struct GNUNET_JSON_Specification | ||
57 | GNUNET_RECLAIM_JSON_spec_claim_attest (struct | ||
58 | GNUNET_RECLAIM_ATTESTATION_Claim **attr); | ||