aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim-attribute
diff options
context:
space:
mode:
authorMarkus Voggenreiter <Markus.Voggenreiter@tum.de>2019-10-09 21:10:14 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2020-01-13 13:31:01 +0100
commite6eb523e12729ef1d6a6ee3f140671e5e51d8d1c (patch)
tree746c6b899bc5fdc491ad477463dda3b56295304e /src/reclaim-attribute
parent60d2660de243053bc0f41657ad9d67537723276c (diff)
downloadgnunet-e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c.tar.gz
gnunet-e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c.zip
Adapted JSON Conversion and Serialization
Diffstat (limited to 'src/reclaim-attribute')
-rw-r--r--src/reclaim-attribute/plugin_reclaim_attribute_gnuid.c110
-rw-r--r--src/reclaim-attribute/reclaim_attribute.c242
-rw-r--r--src/reclaim-attribute/reclaim_attribute.h33
3 files changed, 385 insertions, 0 deletions
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 */
102static char *
103gnuid_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 */
130static int
131gnuid_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 */
166static 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 */
219static uint32_t
220gnuid_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 */
238static const char *
239gnuid_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 */
226uint32_t
227GNUNET_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 */
251const char *
252GNUNET_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 */
278int
279GNUNET_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 */
310char *
311GNUNET_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 */
377struct GNUNET_RECLAIM_ATTESTATION_Claim *
378GNUNET_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 */
676size_t
677GNUNET_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 */
690size_t
691GNUNET_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 */
727struct GNUNET_RECLAIM_ATTESTATION_Claim *
728GNUNET_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 */
67struct 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