diff options
author | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2020-02-07 15:51:56 +0100 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2020-02-09 20:38:11 +0100 |
commit | 5f9f9cae1714eb33e0ee9c824f3d88e6aab8cf63 (patch) | |
tree | a83fc7724ee2efbfee7d7a353b54930fb1d511b8 /src/reclaim | |
parent | 141a279fb55bc9b11aaa765ca3ad54689003e291 (diff) | |
download | gnunet-5f9f9cae1714eb33e0ee9c824f3d88e6aab8cf63.tar.gz gnunet-5f9f9cae1714eb33e0ee9c824f3d88e6aab8cf63.zip |
consolidate reclaim attribute lib
Diffstat (limited to 'src/reclaim')
-rw-r--r-- | src/reclaim/Makefile.am | 32 | ||||
-rw-r--r-- | src/reclaim/gnunet-service-reclaim.c | 2 | ||||
-rw-r--r-- | src/reclaim/gnunet-service-reclaim_tickets.h | 2 | ||||
-rw-r--r-- | src/reclaim/json_reclaim.c | 2 | ||||
-rw-r--r-- | src/reclaim/json_reclaim.h | 2 | ||||
-rw-r--r-- | src/reclaim/oidc_helper.c | 2 | ||||
-rw-r--r-- | src/reclaim/plugin_reclaim_attestation_jwt.c | 233 | ||||
-rw-r--r-- | src/reclaim/plugin_reclaim_attribute_basic.c | 180 | ||||
-rw-r--r-- | src/reclaim/plugin_rest_openid_connect.c | 2 | ||||
-rw-r--r-- | src/reclaim/plugin_rest_reclaim.c | 2 | ||||
-rw-r--r-- | src/reclaim/reclaim_api.c | 2 | ||||
-rw-r--r-- | src/reclaim/reclaim_attestation.c | 520 | ||||
-rw-r--r-- | src/reclaim/reclaim_attestation.h | 64 | ||||
-rw-r--r-- | src/reclaim/reclaim_attribute.c | 539 | ||||
-rw-r--r-- | src/reclaim/reclaim_attribute.h | 102 |
15 files changed, 1672 insertions, 14 deletions
diff --git a/src/reclaim/Makefile.am b/src/reclaim/Makefile.am index 51b9b9c5b..9242d6d79 100644 --- a/src/reclaim/Makefile.am +++ b/src/reclaim/Makefile.am | |||
@@ -34,6 +34,8 @@ lib_LTLIBRARIES = \ | |||
34 | libgnunetreclaim.la | 34 | libgnunetreclaim.la |
35 | plugin_LTLIBRARIES = \ | 35 | plugin_LTLIBRARIES = \ |
36 | libgnunet_plugin_gnsrecord_reclaim.la \ | 36 | libgnunet_plugin_gnsrecord_reclaim.la \ |
37 | libgnunet_plugin_reclaim_attribute_basic.la \ | ||
38 | libgnunet_plugin_reclaim_attestation_jwt.la \ | ||
37 | $(REST_PLUGIN) | 39 | $(REST_PLUGIN) |
38 | 40 | ||
39 | bin_PROGRAMS = \ | 41 | bin_PROGRAMS = \ |
@@ -51,7 +53,6 @@ libgnunet_plugin_rest_reclaim_la_LIBADD = \ | |||
51 | libgnunetreclaim.la \ | 53 | libgnunetreclaim.la \ |
52 | $(top_builddir)/src/json/libgnunetjson.la \ | 54 | $(top_builddir)/src/json/libgnunetjson.la \ |
53 | $(top_builddir)/src/rest/libgnunetrest.la \ | 55 | $(top_builddir)/src/rest/libgnunetrest.la \ |
54 | $(top_builddir)/src/reclaim-attribute/libgnunetreclaimattribute.la \ | ||
55 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | 56 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ |
56 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | 57 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ |
57 | $(LTLIBINTL) -ljansson $(MHD_LIBS) | 58 | $(LTLIBINTL) -ljansson $(MHD_LIBS) |
@@ -68,7 +69,6 @@ libgnunet_plugin_rest_openid_connect_la_LIBADD = \ | |||
68 | $(top_builddir)/src/identity/libgnunetidentity.la \ | 69 | $(top_builddir)/src/identity/libgnunetidentity.la \ |
69 | libgnunetreclaim.la \ | 70 | libgnunetreclaim.la \ |
70 | $(top_builddir)/src/rest/libgnunetrest.la \ | 71 | $(top_builddir)/src/rest/libgnunetrest.la \ |
71 | $(top_builddir)/src/reclaim-attribute/libgnunetreclaimattribute.la \ | ||
72 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | 72 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ |
73 | $(top_builddir)/src/gns/libgnunetgns.la \ | 73 | $(top_builddir)/src/gns/libgnunetgns.la \ |
74 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ | 74 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ |
@@ -98,22 +98,43 @@ gnunet_service_reclaim_LDADD = \ | |||
98 | $(top_builddir)/src/util/libgnunetutil.la \ | 98 | $(top_builddir)/src/util/libgnunetutil.la \ |
99 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | 99 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ |
100 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | 100 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ |
101 | $(top_builddir)/src/reclaim-attribute/libgnunetreclaimattribute.la \ | ||
102 | libgnunetreclaim.la \ | 101 | libgnunetreclaim.la \ |
103 | $(top_builddir)/src/gns/libgnunetgns.la \ | 102 | $(top_builddir)/src/gns/libgnunetgns.la \ |
104 | $(GN_LIBINTL) | 103 | $(GN_LIBINTL) |
105 | 104 | ||
106 | libgnunetreclaim_la_SOURCES = \ | 105 | libgnunetreclaim_la_SOURCES = \ |
107 | reclaim_api.c \ | 106 | reclaim_api.c \ |
108 | reclaim.h | 107 | reclaim.h \ |
108 | reclaim_attribute.c \ | ||
109 | reclaim_attribute.h \ | ||
110 | reclaim_attestation.c \ | ||
111 | reclaim_attestation.h | ||
109 | libgnunetreclaim_la_LIBADD = \ | 112 | libgnunetreclaim_la_LIBADD = \ |
110 | $(top_builddir)/src/util/libgnunetutil.la \ | 113 | $(top_builddir)/src/util/libgnunetutil.la \ |
111 | $(top_builddir)/src/reclaim-attribute/libgnunetreclaimattribute.la \ | ||
112 | $(GN_LIBINTL) $(XLIB) | 114 | $(GN_LIBINTL) $(XLIB) |
113 | libgnunetreclaim_la_LDFLAGS = \ | 115 | libgnunetreclaim_la_LDFLAGS = \ |
114 | $(GN_LIB_LDFLAGS) \ | 116 | $(GN_LIB_LDFLAGS) \ |
115 | -version-info 0:0:0 | 117 | -version-info 0:0:0 |
116 | 118 | ||
119 | |||
120 | libgnunet_plugin_reclaim_attribute_basic_la_SOURCES = \ | ||
121 | plugin_reclaim_attribute_basic.c | ||
122 | libgnunet_plugin_reclaim_attribute_basic_la_LIBADD = \ | ||
123 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
124 | $(LTLIBINTL) | ||
125 | libgnunet_plugin_reclaim_attribute_basic_la_LDFLAGS = \ | ||
126 | $(GN_PLUGIN_LDFLAGS) | ||
127 | |||
128 | libgnunet_plugin_reclaim_attestation_jwt_la_SOURCES = \ | ||
129 | plugin_reclaim_attestation_jwt.c | ||
130 | libgnunet_plugin_reclaim_attestation_jwt_la_LIBADD = \ | ||
131 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
132 | libgnunetreclaim.la \ | ||
133 | -ljansson\ | ||
134 | $(LTLIBINTL) | ||
135 | libgnunet_plugin_reclaim_attestation_jwt_la_LDFLAGS = \ | ||
136 | $(GN_PLUGIN_LDFLAGS) | ||
137 | |||
117 | gnunet_reclaim_SOURCES = \ | 138 | gnunet_reclaim_SOURCES = \ |
118 | gnunet-reclaim.c | 139 | gnunet-reclaim.c |
119 | gnunet_reclaim_LDADD = \ | 140 | gnunet_reclaim_LDADD = \ |
@@ -121,7 +142,6 @@ gnunet_reclaim_LDADD = \ | |||
121 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ | 142 | $(top_builddir)/src/namestore/libgnunetnamestore.la \ |
122 | libgnunetreclaim.la \ | 143 | libgnunetreclaim.la \ |
123 | $(top_builddir)/src/identity/libgnunetidentity.la \ | 144 | $(top_builddir)/src/identity/libgnunetidentity.la \ |
124 | $(top_builddir)/src/reclaim-attribute/libgnunetreclaimattribute.la \ | ||
125 | $(GN_LIBINTL) | 145 | $(GN_LIBINTL) |
126 | 146 | ||
127 | check_SCRIPTS = \ | 147 | check_SCRIPTS = \ |
diff --git a/src/reclaim/gnunet-service-reclaim.c b/src/reclaim/gnunet-service-reclaim.c index 4521e5c81..ddfe05556 100644 --- a/src/reclaim/gnunet-service-reclaim.c +++ b/src/reclaim/gnunet-service-reclaim.c | |||
@@ -29,7 +29,7 @@ | |||
29 | #include "gnunet_constants.h" | 29 | #include "gnunet_constants.h" |
30 | #include "gnunet_gnsrecord_lib.h" | 30 | #include "gnunet_gnsrecord_lib.h" |
31 | #include "gnunet_protocols.h" | 31 | #include "gnunet_protocols.h" |
32 | #include "gnunet_reclaim_attribute_lib.h" | 32 | #include "gnunet_reclaim_lib.h" |
33 | #include "gnunet_reclaim_service.h" | 33 | #include "gnunet_reclaim_service.h" |
34 | #include "gnunet_signatures.h" | 34 | #include "gnunet_signatures.h" |
35 | #include "reclaim.h" | 35 | #include "reclaim.h" |
diff --git a/src/reclaim/gnunet-service-reclaim_tickets.h b/src/reclaim/gnunet-service-reclaim_tickets.h index d6bc4a70a..1c7214d42 100644 --- a/src/reclaim/gnunet-service-reclaim_tickets.h +++ b/src/reclaim/gnunet-service-reclaim_tickets.h | |||
@@ -36,7 +36,7 @@ | |||
36 | #include "gnunet_gns_service.h" | 36 | #include "gnunet_gns_service.h" |
37 | #include "gnunet_gnsrecord_lib.h" | 37 | #include "gnunet_gnsrecord_lib.h" |
38 | #include "gnunet_protocols.h" | 38 | #include "gnunet_protocols.h" |
39 | #include "gnunet_reclaim_attribute_lib.h" | 39 | #include "gnunet_reclaim_lib.h" |
40 | #include "gnunet_reclaim_service.h" | 40 | #include "gnunet_reclaim_service.h" |
41 | #include "gnunet_signatures.h" | 41 | #include "gnunet_signatures.h" |
42 | #include "gnunet_statistics_service.h" | 42 | #include "gnunet_statistics_service.h" |
diff --git a/src/reclaim/json_reclaim.c b/src/reclaim/json_reclaim.c index 4c08430e7..353303187 100644 --- a/src/reclaim/json_reclaim.c +++ b/src/reclaim/json_reclaim.c | |||
@@ -28,7 +28,7 @@ | |||
28 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
29 | 29 | ||
30 | #include "gnunet_json_lib.h" | 30 | #include "gnunet_json_lib.h" |
31 | #include "gnunet_reclaim_attribute_lib.h" | 31 | #include "gnunet_reclaim_lib.h" |
32 | #include "gnunet_reclaim_service.h" | 32 | #include "gnunet_reclaim_service.h" |
33 | 33 | ||
34 | 34 | ||
diff --git a/src/reclaim/json_reclaim.h b/src/reclaim/json_reclaim.h index 5aaf81b93..61ddb4378 100644 --- a/src/reclaim/json_reclaim.h +++ b/src/reclaim/json_reclaim.h | |||
@@ -27,7 +27,7 @@ | |||
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | #include "gnunet_json_lib.h" | 28 | #include "gnunet_json_lib.h" |
29 | #include "gnunet_reclaim_service.h" | 29 | #include "gnunet_reclaim_service.h" |
30 | #include "gnunet_reclaim_attribute_lib.h" | 30 | #include "gnunet_reclaim_lib.h" |
31 | 31 | ||
32 | /** | 32 | /** |
33 | * JSON Specification for Reclaim claims. | 33 | * JSON Specification for Reclaim claims. |
diff --git a/src/reclaim/oidc_helper.c b/src/reclaim/oidc_helper.c index 92b4b69cc..469b8a796 100644 --- a/src/reclaim/oidc_helper.c +++ b/src/reclaim/oidc_helper.c | |||
@@ -27,7 +27,7 @@ | |||
27 | #include <inttypes.h> | 27 | #include <inttypes.h> |
28 | #include <jansson.h> | 28 | #include <jansson.h> |
29 | #include "gnunet_util_lib.h" | 29 | #include "gnunet_util_lib.h" |
30 | #include "gnunet_reclaim_attribute_lib.h" | 30 | #include "gnunet_reclaim_lib.h" |
31 | #include "gnunet_reclaim_service.h" | 31 | #include "gnunet_reclaim_service.h" |
32 | #include "gnunet_signatures.h" | 32 | #include "gnunet_signatures.h" |
33 | #include "oidc_helper.h" | 33 | #include "oidc_helper.h" |
diff --git a/src/reclaim/plugin_reclaim_attestation_jwt.c b/src/reclaim/plugin_reclaim_attestation_jwt.c new file mode 100644 index 000000000..8a67b18cd --- /dev/null +++ b/src/reclaim/plugin_reclaim_attestation_jwt.c | |||
@@ -0,0 +1,233 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013, 2014, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file reclaim-attribute/plugin_reclaim_attestation_gnuid.c | ||
23 | * @brief reclaim-attribute-plugin-gnuid attribute plugin to provide the API for | ||
24 | * fundamental | ||
25 | * attribute types. | ||
26 | * | ||
27 | * @author Martin Schanzenbach | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_reclaim_plugin.h" | ||
32 | #include <inttypes.h> | ||
33 | #include <jansson.h> | ||
34 | |||
35 | /** | ||
36 | * Convert the 'value' of an attestation to a string. | ||
37 | * | ||
38 | * @param cls closure, unused | ||
39 | * @param type type of the attestation | ||
40 | * @param data value in binary encoding | ||
41 | * @param data_size number of bytes in @a data | ||
42 | * @return NULL on error, otherwise human-readable representation of the value | ||
43 | */ | ||
44 | static char * | ||
45 | jwt_value_to_string (void *cls, | ||
46 | uint32_t type, | ||
47 | const void *data, | ||
48 | size_t data_size) | ||
49 | { | ||
50 | switch (type) | ||
51 | { | ||
52 | case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT: | ||
53 | return GNUNET_strndup (data, data_size); | ||
54 | |||
55 | default: | ||
56 | return NULL; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Convert human-readable version of a 'value' of an attestation to the binary | ||
63 | * representation. | ||
64 | * | ||
65 | * @param cls closure, unused | ||
66 | * @param type type of the attestation | ||
67 | * @param s human-readable string | ||
68 | * @param data set to value in binary encoding (will be allocated) | ||
69 | * @param data_size set to number of bytes in @a data | ||
70 | * @return #GNUNET_OK on success | ||
71 | */ | ||
72 | static int | ||
73 | jwt_string_to_value (void *cls, | ||
74 | uint32_t type, | ||
75 | const char *s, | ||
76 | void **data, | ||
77 | size_t *data_size) | ||
78 | { | ||
79 | if (NULL == s) | ||
80 | return GNUNET_SYSERR; | ||
81 | switch (type) | ||
82 | { | ||
83 | case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT: | ||
84 | *data = GNUNET_strdup (s); | ||
85 | *data_size = strlen (s); | ||
86 | return GNUNET_OK; | ||
87 | |||
88 | default: | ||
89 | return GNUNET_SYSERR; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Mapping of attestation type numbers to human-readable | ||
96 | * attestation type names. | ||
97 | */ | ||
98 | static struct | ||
99 | { | ||
100 | const char *name; | ||
101 | uint32_t number; | ||
102 | } jwt_attest_name_map[] = { { "JWT", GNUNET_RECLAIM_ATTESTATION_TYPE_JWT }, | ||
103 | { NULL, UINT32_MAX } }; | ||
104 | |||
105 | /** | ||
106 | * Convert a type name to the corresponding number. | ||
107 | * | ||
108 | * @param cls closure, unused | ||
109 | * @param jwt_typename name to convert | ||
110 | * @return corresponding number, UINT32_MAX on error | ||
111 | */ | ||
112 | static uint32_t | ||
113 | jwt_typename_to_number (void *cls, const char *jwt_typename) | ||
114 | { | ||
115 | unsigned int i; | ||
116 | |||
117 | i = 0; | ||
118 | while ((NULL != jwt_attest_name_map[i].name) && | ||
119 | (0 != strcasecmp (jwt_typename, jwt_attest_name_map[i].name))) | ||
120 | i++; | ||
121 | return jwt_attest_name_map[i].number; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Convert a type number (i.e. 1) to the corresponding type string | ||
127 | * | ||
128 | * @param cls closure, unused | ||
129 | * @param type number of a type to convert | ||
130 | * @return corresponding typestring, NULL on error | ||
131 | */ | ||
132 | static const char * | ||
133 | jwt_number_to_typename (void *cls, uint32_t type) | ||
134 | { | ||
135 | unsigned int i; | ||
136 | |||
137 | i = 0; | ||
138 | while ((NULL != jwt_attest_name_map[i].name) && (type != | ||
139 | jwt_attest_name_map[i]. | ||
140 | number)) | ||
141 | i++; | ||
142 | return jwt_attest_name_map[i].name; | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * Parse a JWT and return the respective claim value as Attribute | ||
147 | * | ||
148 | * @param attest the jwt attestation | ||
149 | * @param claim the name of the claim in the JWT | ||
150 | * | ||
151 | * @return a GNUNET_RECLAIM_Attribute, containing the new value | ||
152 | */ | ||
153 | struct GNUNET_RECLAIM_AttributeList * | ||
154 | jwt_parse_attributes (void *cls, | ||
155 | const struct GNUNET_RECLAIM_Attestation *attest) | ||
156 | { | ||
157 | char *jwt_string; | ||
158 | struct GNUNET_RECLAIM_AttributeList *attrs; | ||
159 | char delim[] = "."; | ||
160 | char *val_str = NULL; | ||
161 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n"); | ||
162 | char *decoded_jwt; | ||
163 | json_t *json_val; | ||
164 | json_error_t *json_err = NULL; | ||
165 | |||
166 | if (GNUNET_RECLAIM_ATTESTATION_TYPE_JWT != attest->type) | ||
167 | return NULL; | ||
168 | attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
169 | |||
170 | jwt_string = GNUNET_strdup (attest->data); | ||
171 | const char *jwt_body = strtok (jwt_string, delim); | ||
172 | jwt_body = strtok (NULL, delim); | ||
173 | GNUNET_STRINGS_base64_decode (jwt_body, strlen (jwt_body), | ||
174 | (void **) &decoded_jwt); | ||
175 | json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err); | ||
176 | const char *key; | ||
177 | json_t *value; | ||
178 | json_object_foreach (json_val, key, value) { | ||
179 | val_str = json_dumps (value, JSON_ENCODE_ANY); | ||
180 | GNUNET_RECLAIM_attribute_list_add (attrs, | ||
181 | key, | ||
182 | NULL, | ||
183 | GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,//FIXME | ||
184 | val_str, | ||
185 | strlen (val_str)); | ||
186 | GNUNET_free (val_str); | ||
187 | } | ||
188 | GNUNET_free (jwt_string); | ||
189 | //FIXME needed?? | ||
190 | return attrs; | ||
191 | } | ||
192 | |||
193 | |||
194 | |||
195 | |||
196 | /** | ||
197 | * Entry point for the plugin. | ||
198 | * | ||
199 | * @param cls NULL | ||
200 | * @return the exported block API | ||
201 | */ | ||
202 | void * | ||
203 | libgnunet_plugin_reclaim_attestation_jwt_init (void *cls) | ||
204 | { | ||
205 | struct GNUNET_RECLAIM_AttestationPluginFunctions *api; | ||
206 | |||
207 | api = GNUNET_new (struct GNUNET_RECLAIM_AttestationPluginFunctions); | ||
208 | api->value_to_string = &jwt_value_to_string; | ||
209 | api->string_to_value = &jwt_string_to_value; | ||
210 | api->typename_to_number = &jwt_typename_to_number; | ||
211 | api->number_to_typename = &jwt_number_to_typename; | ||
212 | api->get_attributes = &jwt_parse_attributes; | ||
213 | return api; | ||
214 | } | ||
215 | |||
216 | |||
217 | /** | ||
218 | * Exit point from the plugin. | ||
219 | * | ||
220 | * @param cls the return value from #libgnunet_plugin_block_test_init() | ||
221 | * @return NULL | ||
222 | */ | ||
223 | void * | ||
224 | libgnunet_plugin_reclaim_attestation_jwt_done (void *cls) | ||
225 | { | ||
226 | struct GNUNET_RECLAIM_AttestationPluginFunctions *api = cls; | ||
227 | |||
228 | GNUNET_free (api); | ||
229 | return NULL; | ||
230 | } | ||
231 | |||
232 | |||
233 | /* end of plugin_reclaim_attestation_type_gnuid.c */ | ||
diff --git a/src/reclaim/plugin_reclaim_attribute_basic.c b/src/reclaim/plugin_reclaim_attribute_basic.c new file mode 100644 index 000000000..47fdd5f11 --- /dev/null +++ b/src/reclaim/plugin_reclaim_attribute_basic.c | |||
@@ -0,0 +1,180 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013, 2014, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file reclaim-attribute/plugin_reclaim_attribute_gnuid.c | ||
23 | * @brief reclaim-attribute-plugin-gnuid attribute plugin to provide the API for | ||
24 | * fundamental | ||
25 | * attribute types. | ||
26 | * | ||
27 | * @author Martin Schanzenbach | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_reclaim_plugin.h" | ||
32 | #include <inttypes.h> | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Convert the 'value' of an attribute to a string. | ||
37 | * | ||
38 | * @param cls closure, unused | ||
39 | * @param type type of the attribute | ||
40 | * @param data value in binary encoding | ||
41 | * @param data_size number of bytes in @a data | ||
42 | * @return NULL on error, otherwise human-readable representation of the value | ||
43 | */ | ||
44 | static char * | ||
45 | basic_value_to_string (void *cls, | ||
46 | uint32_t type, | ||
47 | const void *data, | ||
48 | size_t data_size) | ||
49 | { | ||
50 | switch (type) | ||
51 | { | ||
52 | case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING: | ||
53 | return GNUNET_strndup (data, data_size); | ||
54 | |||
55 | default: | ||
56 | return NULL; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Convert human-readable version of a 'value' of an attribute to the binary | ||
63 | * representation. | ||
64 | * | ||
65 | * @param cls closure, unused | ||
66 | * @param type type of the attribute | ||
67 | * @param s human-readable string | ||
68 | * @param data set to value in binary encoding (will be allocated) | ||
69 | * @param data_size set to number of bytes in @a data | ||
70 | * @return #GNUNET_OK on success | ||
71 | */ | ||
72 | static int | ||
73 | basic_string_to_value (void *cls, | ||
74 | uint32_t type, | ||
75 | const char *s, | ||
76 | void **data, | ||
77 | size_t *data_size) | ||
78 | { | ||
79 | if (NULL == s) | ||
80 | return GNUNET_SYSERR; | ||
81 | switch (type) | ||
82 | { | ||
83 | case GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING: | ||
84 | *data = GNUNET_strdup (s); | ||
85 | *data_size = strlen (s); | ||
86 | return GNUNET_OK; | ||
87 | |||
88 | default: | ||
89 | return GNUNET_SYSERR; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Mapping of attribute type numbers to human-readable | ||
95 | * attribute type names. | ||
96 | */ | ||
97 | static struct | ||
98 | { | ||
99 | const char *name; | ||
100 | uint32_t number; | ||
101 | } basic_name_map[] = { { "STRING", GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING }, | ||
102 | { NULL, UINT32_MAX } }; | ||
103 | |||
104 | |||
105 | /** | ||
106 | * Convert a type name to the corresponding number. | ||
107 | * | ||
108 | * @param cls closure, unused | ||
109 | * @param basic_typename name to convert | ||
110 | * @return corresponding number, UINT32_MAX on error | ||
111 | */ | ||
112 | static uint32_t | ||
113 | basic_typename_to_number (void *cls, const char *basic_typename) | ||
114 | { | ||
115 | unsigned int i; | ||
116 | |||
117 | i = 0; | ||
118 | while ((NULL != basic_name_map[i].name) && | ||
119 | (0 != strcasecmp (basic_typename, basic_name_map[i].name))) | ||
120 | i++; | ||
121 | return basic_name_map[i].number; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Convert a type number (i.e. 1) to the corresponding type string | ||
127 | * | ||
128 | * @param cls closure, unused | ||
129 | * @param type number of a type to convert | ||
130 | * @return corresponding typestring, NULL on error | ||
131 | */ | ||
132 | static const char * | ||
133 | basic_number_to_typename (void *cls, uint32_t type) | ||
134 | { | ||
135 | unsigned int i; | ||
136 | |||
137 | i = 0; | ||
138 | while ((NULL != basic_name_map[i].name) && (type != basic_name_map[i].number)) | ||
139 | i++; | ||
140 | return basic_name_map[i].name; | ||
141 | } | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Entry point for the plugin. | ||
146 | * | ||
147 | * @param cls NULL | ||
148 | * @return the exported block API | ||
149 | */ | ||
150 | void * | ||
151 | libgnunet_plugin_reclaim_attribute_basic_init (void *cls) | ||
152 | { | ||
153 | struct GNUNET_RECLAIM_AttributePluginFunctions *api; | ||
154 | |||
155 | api = GNUNET_new (struct GNUNET_RECLAIM_AttributePluginFunctions); | ||
156 | api->value_to_string = &basic_value_to_string; | ||
157 | api->string_to_value = &basic_string_to_value; | ||
158 | api->typename_to_number = &basic_typename_to_number; | ||
159 | api->number_to_typename = &basic_number_to_typename; | ||
160 | return api; | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * Exit point from the plugin. | ||
166 | * | ||
167 | * @param cls the return value from #libgnunet_plugin_block_test_init() | ||
168 | * @return NULL | ||
169 | */ | ||
170 | void * | ||
171 | libgnunet_plugin_reclaim_attribute_basic_done (void *cls) | ||
172 | { | ||
173 | struct GNUNET_RECLAIM_AttributePluginFunctions *api = cls; | ||
174 | |||
175 | GNUNET_free (api); | ||
176 | return NULL; | ||
177 | } | ||
178 | |||
179 | |||
180 | /* end of plugin_reclaim_attribute_type_gnuid.c */ | ||
diff --git a/src/reclaim/plugin_rest_openid_connect.c b/src/reclaim/plugin_rest_openid_connect.c index b296f6d15..3e138f259 100644 --- a/src/reclaim/plugin_rest_openid_connect.c +++ b/src/reclaim/plugin_rest_openid_connect.c | |||
@@ -32,7 +32,7 @@ | |||
32 | #include "gnunet_gnsrecord_lib.h" | 32 | #include "gnunet_gnsrecord_lib.h" |
33 | #include "gnunet_identity_service.h" | 33 | #include "gnunet_identity_service.h" |
34 | #include "gnunet_namestore_service.h" | 34 | #include "gnunet_namestore_service.h" |
35 | #include "gnunet_reclaim_attribute_lib.h" | 35 | #include "gnunet_reclaim_lib.h" |
36 | #include "gnunet_reclaim_service.h" | 36 | #include "gnunet_reclaim_service.h" |
37 | #include "gnunet_rest_lib.h" | 37 | #include "gnunet_rest_lib.h" |
38 | #include "gnunet_rest_plugin.h" | 38 | #include "gnunet_rest_plugin.h" |
diff --git a/src/reclaim/plugin_rest_reclaim.c b/src/reclaim/plugin_rest_reclaim.c index 417e594fe..8b3aee8ba 100644 --- a/src/reclaim/plugin_rest_reclaim.c +++ b/src/reclaim/plugin_rest_reclaim.c | |||
@@ -31,7 +31,7 @@ | |||
31 | #include "gnunet_gns_service.h" | 31 | #include "gnunet_gns_service.h" |
32 | #include "gnunet_gnsrecord_lib.h" | 32 | #include "gnunet_gnsrecord_lib.h" |
33 | #include "gnunet_identity_service.h" | 33 | #include "gnunet_identity_service.h" |
34 | #include "gnunet_reclaim_attribute_lib.h" | 34 | #include "gnunet_reclaim_lib.h" |
35 | #include "gnunet_reclaim_service.h" | 35 | #include "gnunet_reclaim_service.h" |
36 | #include "gnunet_rest_lib.h" | 36 | #include "gnunet_rest_lib.h" |
37 | #include "gnunet_rest_plugin.h" | 37 | #include "gnunet_rest_plugin.h" |
diff --git a/src/reclaim/reclaim_api.c b/src/reclaim/reclaim_api.c index 54288866d..8558b19df 100644 --- a/src/reclaim/reclaim_api.c +++ b/src/reclaim/reclaim_api.c | |||
@@ -28,7 +28,7 @@ | |||
28 | #include "gnunet_constants.h" | 28 | #include "gnunet_constants.h" |
29 | #include "gnunet_mq_lib.h" | 29 | #include "gnunet_mq_lib.h" |
30 | #include "gnunet_protocols.h" | 30 | #include "gnunet_protocols.h" |
31 | #include "gnunet_reclaim_attribute_lib.h" | 31 | #include "gnunet_reclaim_lib.h" |
32 | #include "gnunet_reclaim_service.h" | 32 | #include "gnunet_reclaim_service.h" |
33 | #include "reclaim.h" | 33 | #include "reclaim.h" |
34 | 34 | ||
diff --git a/src/reclaim/reclaim_attestation.c b/src/reclaim/reclaim_attestation.c new file mode 100644 index 000000000..961c6ede4 --- /dev/null +++ b/src/reclaim/reclaim_attestation.c | |||
@@ -0,0 +1,520 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file reclaim-attribute/reclaim_attestation.c | ||
23 | * @brief helper library to manage identity attribute attestations | ||
24 | * @author Martin Schanzenbach | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_reclaim_plugin.h" | ||
29 | #include "reclaim_attestation.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Handle for a plugin | ||
34 | */ | ||
35 | struct Plugin | ||
36 | { | ||
37 | /** | ||
38 | * Name of the plugin | ||
39 | */ | ||
40 | char *library_name; | ||
41 | |||
42 | /** | ||
43 | * Plugin API | ||
44 | */ | ||
45 | struct GNUNET_RECLAIM_AttestationPluginFunctions *api; | ||
46 | }; | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Plugins | ||
51 | */ | ||
52 | static struct Plugin **attest_plugins; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Number of plugins | ||
57 | */ | ||
58 | static unsigned int num_plugins; | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Init canary | ||
63 | */ | ||
64 | static int initialized; | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Add a plugin | ||
69 | * | ||
70 | * @param cls closure | ||
71 | * @param library_name name of the API library | ||
72 | * @param lib_ret the plugin API pointer | ||
73 | */ | ||
74 | static void | ||
75 | add_plugin (void *cls, const char *library_name, void *lib_ret) | ||
76 | { | ||
77 | struct GNUNET_RECLAIM_AttestationPluginFunctions *api = lib_ret; | ||
78 | struct Plugin *plugin; | ||
79 | |||
80 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
81 | "Loading attestation plugin `%s'\n", | ||
82 | library_name); | ||
83 | plugin = GNUNET_new (struct Plugin); | ||
84 | plugin->api = api; | ||
85 | plugin->library_name = GNUNET_strdup (library_name); | ||
86 | GNUNET_array_append (attest_plugins, num_plugins, plugin); | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Load plugins | ||
92 | */ | ||
93 | static void | ||
94 | init () | ||
95 | { | ||
96 | if (GNUNET_YES == initialized) | ||
97 | return; | ||
98 | initialized = GNUNET_YES; | ||
99 | GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attestation_", | ||
100 | NULL, | ||
101 | &add_plugin, | ||
102 | NULL); | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Convert an attestation type name to the corresponding number | ||
108 | * | ||
109 | * @param typename name to convert | ||
110 | * @return corresponding number, UINT32_MAX on error | ||
111 | */ | ||
112 | uint32_t | ||
113 | GNUNET_RECLAIM_attestation_typename_to_number (const char *typename) | ||
114 | { | ||
115 | unsigned int i; | ||
116 | struct Plugin *plugin; | ||
117 | uint32_t ret; | ||
118 | init (); | ||
119 | for (i = 0; i < num_plugins; i++) | ||
120 | { | ||
121 | plugin = attest_plugins[i]; | ||
122 | if (UINT32_MAX != | ||
123 | (ret = plugin->api->typename_to_number (plugin->api->cls, | ||
124 | typename))) | ||
125 | return ret; | ||
126 | } | ||
127 | return UINT32_MAX; | ||
128 | } | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Convert an attestation type number to the corresponding attestation type string | ||
133 | * | ||
134 | * @param type number of a type | ||
135 | * @return corresponding typestring, NULL on error | ||
136 | */ | ||
137 | const char * | ||
138 | GNUNET_RECLAIM_attestation_number_to_typename (uint32_t type) | ||
139 | { | ||
140 | unsigned int i; | ||
141 | struct Plugin *plugin; | ||
142 | const char *ret; | ||
143 | |||
144 | init (); | ||
145 | for (i = 0; i < num_plugins; i++) | ||
146 | { | ||
147 | plugin = attest_plugins[i]; | ||
148 | if (NULL != | ||
149 | (ret = plugin->api->number_to_typename (plugin->api->cls, type))) | ||
150 | return ret; | ||
151 | } | ||
152 | return NULL; | ||
153 | } | ||
154 | |||
155 | |||
156 | /** | ||
157 | * Convert human-readable version of a 'claim' of an attestation to the binary | ||
158 | * representation | ||
159 | * | ||
160 | * @param type type of the claim | ||
161 | * @param s human-readable string | ||
162 | * @param data set to value in binary encoding (will be allocated) | ||
163 | * @param data_size set to number of bytes in @a data | ||
164 | * @return #GNUNET_OK on success | ||
165 | */ | ||
166 | int | ||
167 | GNUNET_RECLAIM_attestation_string_to_value (uint32_t type, | ||
168 | const char *s, | ||
169 | void **data, | ||
170 | size_t *data_size) | ||
171 | { | ||
172 | unsigned int i; | ||
173 | struct Plugin *plugin; | ||
174 | |||
175 | init (); | ||
176 | for (i = 0; i < num_plugins; i++) | ||
177 | { | ||
178 | plugin = attest_plugins[i]; | ||
179 | if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, | ||
180 | type, | ||
181 | s, | ||
182 | data, | ||
183 | data_size)) | ||
184 | return GNUNET_OK; | ||
185 | } | ||
186 | return GNUNET_SYSERR; | ||
187 | } | ||
188 | |||
189 | |||
190 | /** | ||
191 | * Convert the 'claim' of an attestation to a string | ||
192 | * | ||
193 | * @param type the type of attestation | ||
194 | * @param data claim in binary encoding | ||
195 | * @param data_size number of bytes in @a data | ||
196 | * @return NULL on error, otherwise human-readable representation of the claim | ||
197 | */ | ||
198 | char * | ||
199 | GNUNET_RECLAIM_attestation_value_to_string (uint32_t type, | ||
200 | const void *data, | ||
201 | size_t data_size) | ||
202 | { | ||
203 | unsigned int i; | ||
204 | struct Plugin *plugin; | ||
205 | char *ret; | ||
206 | |||
207 | init (); | ||
208 | for (i = 0; i < num_plugins; i++) | ||
209 | { | ||
210 | plugin = attest_plugins[i]; | ||
211 | if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, | ||
212 | type, | ||
213 | data, | ||
214 | data_size))) | ||
215 | return ret; | ||
216 | } | ||
217 | return NULL; | ||
218 | } | ||
219 | |||
220 | |||
221 | /** | ||
222 | * Create a new attestation. | ||
223 | * | ||
224 | * @param attr_name the attestation name | ||
225 | * @param type the attestation type | ||
226 | * @param data the attestation value | ||
227 | * @param data_size the attestation value size | ||
228 | * @return the new attestation | ||
229 | */ | ||
230 | struct GNUNET_RECLAIM_Attestation * | ||
231 | GNUNET_RECLAIM_attestation_new (const char *attr_name, | ||
232 | uint32_t type, | ||
233 | const void *data, | ||
234 | size_t data_size) | ||
235 | { | ||
236 | struct GNUNET_RECLAIM_Attestation *attr; | ||
237 | char *write_ptr; | ||
238 | char *attr_name_tmp = GNUNET_strdup (attr_name); | ||
239 | |||
240 | GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); | ||
241 | |||
242 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attestation) | ||
243 | + strlen (attr_name_tmp) + 1 + data_size); | ||
244 | attr->type = type; | ||
245 | attr->data_size = data_size; | ||
246 | attr->flag = 0; | ||
247 | write_ptr = (char *) &attr[1]; | ||
248 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | ||
249 | attr->name = write_ptr; | ||
250 | write_ptr += strlen (attr->name) + 1; | ||
251 | GNUNET_memcpy (write_ptr, data, data_size); | ||
252 | attr->data = write_ptr; | ||
253 | GNUNET_free (attr_name_tmp); | ||
254 | return attr; | ||
255 | } | ||
256 | |||
257 | |||
258 | /** | ||
259 | * Get required size for serialization buffer | ||
260 | * | ||
261 | * @param attrs the attribute list to serialize | ||
262 | * @return the required buffer size | ||
263 | */ | ||
264 | size_t | ||
265 | GNUNET_RECLAIM_attestation_list_serialize_get_size ( | ||
266 | const struct GNUNET_RECLAIM_AttestationList *attestations) | ||
267 | { | ||
268 | struct GNUNET_RECLAIM_AttestationListEntry *le; | ||
269 | size_t len = 0; | ||
270 | |||
271 | for (le = attestations->list_head; NULL != le; le = le->next) | ||
272 | { | ||
273 | GNUNET_assert (NULL != le->attestation); | ||
274 | len += GNUNET_RECLAIM_attestation_serialize_get_size (le->attestation); | ||
275 | len += sizeof(struct GNUNET_RECLAIM_AttestationListEntry); | ||
276 | } | ||
277 | return len; | ||
278 | } | ||
279 | |||
280 | |||
281 | /** | ||
282 | * Serialize an attribute list | ||
283 | * | ||
284 | * @param attrs the attribute list to serialize | ||
285 | * @param result the serialized attribute | ||
286 | * @return length of serialized data | ||
287 | */ | ||
288 | size_t | ||
289 | GNUNET_RECLAIM_attestation_list_serialize ( | ||
290 | const struct GNUNET_RECLAIM_AttestationList *attestations, | ||
291 | char *result) | ||
292 | { | ||
293 | struct GNUNET_RECLAIM_AttestationListEntry *le; | ||
294 | size_t len; | ||
295 | size_t total_len; | ||
296 | char *write_ptr; | ||
297 | write_ptr = result; | ||
298 | total_len = 0; | ||
299 | for (le = attestations->list_head; NULL != le; le = le->next) | ||
300 | { | ||
301 | GNUNET_assert (NULL != le->attestation); | ||
302 | len = GNUNET_RECLAIM_attestation_serialize (le->attestation, write_ptr); | ||
303 | total_len += len; | ||
304 | write_ptr += len; | ||
305 | } | ||
306 | return total_len; | ||
307 | } | ||
308 | |||
309 | |||
310 | /** | ||
311 | * Deserialize an attestation list | ||
312 | * | ||
313 | * @param data the serialized attribute list | ||
314 | * @param data_size the length of the serialized data | ||
315 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
316 | */ | ||
317 | struct GNUNET_RECLAIM_AttestationList * | ||
318 | GNUNET_RECLAIM_attestation_list_deserialize (const char *data, size_t data_size) | ||
319 | { | ||
320 | struct GNUNET_RECLAIM_AttestationList *al; | ||
321 | struct GNUNET_RECLAIM_AttestationListEntry *ale; | ||
322 | size_t att_len; | ||
323 | const char *read_ptr; | ||
324 | |||
325 | al = GNUNET_new (struct GNUNET_RECLAIM_AttestationList); | ||
326 | |||
327 | if ((data_size < sizeof(struct | ||
328 | Attestation) | ||
329 | + sizeof(struct GNUNET_RECLAIM_AttestationListEntry))) | ||
330 | return al; | ||
331 | |||
332 | read_ptr = data; | ||
333 | while (((data + data_size) - read_ptr) >= sizeof(struct Attestation)) | ||
334 | { | ||
335 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttestationListEntry); | ||
336 | ale->attestation = | ||
337 | GNUNET_RECLAIM_attestation_deserialize (read_ptr, | ||
338 | data_size - (read_ptr - data)); | ||
339 | GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale); | ||
340 | att_len = GNUNET_RECLAIM_attestation_serialize_get_size (ale->attestation); | ||
341 | read_ptr += att_len; | ||
342 | } | ||
343 | return al; | ||
344 | } | ||
345 | |||
346 | |||
347 | /** | ||
348 | * Make a (deep) copy of the attestation list | ||
349 | * @param attrs claim list to copy | ||
350 | * @return copied claim list | ||
351 | */ | ||
352 | struct GNUNET_RECLAIM_AttestationList * | ||
353 | GNUNET_RECLAIM_attestation_list_dup ( | ||
354 | const struct GNUNET_RECLAIM_AttestationList *al) | ||
355 | { | ||
356 | struct GNUNET_RECLAIM_AttestationListEntry *ale; | ||
357 | struct GNUNET_RECLAIM_AttestationListEntry *result_ale; | ||
358 | struct GNUNET_RECLAIM_AttestationList *result; | ||
359 | |||
360 | result = GNUNET_new (struct GNUNET_RECLAIM_AttestationList); | ||
361 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
362 | { | ||
363 | result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttestationListEntry); | ||
364 | GNUNET_assert (NULL != ale->attestation); | ||
365 | result_ale->attestation = | ||
366 | GNUNET_RECLAIM_attestation_new (ale->attestation->name, | ||
367 | ale->attestation->type, | ||
368 | ale->attestation->data, | ||
369 | ale->attestation->data_size); | ||
370 | result_ale->attestation->id = ale->attestation->id; | ||
371 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
372 | result->list_tail, | ||
373 | result_ale); | ||
374 | } | ||
375 | return result; | ||
376 | } | ||
377 | |||
378 | |||
379 | /** | ||
380 | * Destroy attestation list | ||
381 | * | ||
382 | * @param attrs list to destroy | ||
383 | */ | ||
384 | void | ||
385 | GNUNET_RECLAIM_attestation_list_destroy ( | ||
386 | struct GNUNET_RECLAIM_AttestationList *al) | ||
387 | { | ||
388 | struct GNUNET_RECLAIM_AttestationListEntry *ale; | ||
389 | struct GNUNET_RECLAIM_AttestationListEntry *tmp_ale; | ||
390 | |||
391 | for (ale = al->list_head; NULL != ale;) | ||
392 | { | ||
393 | if (NULL != ale->attestation) | ||
394 | GNUNET_free (ale->attestation); | ||
395 | tmp_ale = ale; | ||
396 | ale = ale->next; | ||
397 | GNUNET_free (tmp_ale); | ||
398 | } | ||
399 | GNUNET_free (al); | ||
400 | } | ||
401 | |||
402 | |||
403 | /** | ||
404 | * Get required size for serialization buffer | ||
405 | * | ||
406 | * @param attr the attestation to serialize | ||
407 | * @return the required buffer size | ||
408 | */ | ||
409 | size_t | ||
410 | GNUNET_RECLAIM_attestation_serialize_get_size ( | ||
411 | const struct GNUNET_RECLAIM_Attestation *attestation) | ||
412 | { | ||
413 | return sizeof(struct Attestation) + strlen (attestation->name) | ||
414 | + attestation->data_size; | ||
415 | } | ||
416 | |||
417 | |||
418 | /** | ||
419 | * Serialize an attestation | ||
420 | * | ||
421 | * @param attr the attestation to serialize | ||
422 | * @param result the serialized attestation | ||
423 | * @return length of serialized data | ||
424 | */ | ||
425 | size_t | ||
426 | GNUNET_RECLAIM_attestation_serialize ( | ||
427 | const struct GNUNET_RECLAIM_Attestation *attestation, | ||
428 | char *result) | ||
429 | { | ||
430 | size_t data_len_ser; | ||
431 | size_t name_len; | ||
432 | struct Attestation *atts; | ||
433 | char *write_ptr; | ||
434 | |||
435 | atts = (struct Attestation *) result; | ||
436 | atts->attestation_type = htons (attestation->type); | ||
437 | atts->attestation_flag = htonl (attestation->flag); | ||
438 | atts->attestation_id = attestation->id; | ||
439 | name_len = strlen (attestation->name); | ||
440 | atts->name_len = htons (name_len); | ||
441 | write_ptr = (char *) &atts[1]; | ||
442 | GNUNET_memcpy (write_ptr, attestation->name, name_len); | ||
443 | write_ptr += name_len; | ||
444 | // TODO plugin-ize | ||
445 | // data_len_ser = plugin->serialize_attribute_value (attr, | ||
446 | // &attr_ser[1]); | ||
447 | data_len_ser = attestation->data_size; | ||
448 | GNUNET_memcpy (write_ptr, attestation->data, attestation->data_size); | ||
449 | atts->data_size = htons (data_len_ser); | ||
450 | |||
451 | return sizeof(struct Attestation) + strlen (attestation->name) | ||
452 | + attestation->data_size; | ||
453 | } | ||
454 | |||
455 | |||
456 | /** | ||
457 | * Deserialize an attestation | ||
458 | * | ||
459 | * @param data the serialized attestation | ||
460 | * @param data_size the length of the serialized data | ||
461 | * | ||
462 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
463 | */ | ||
464 | struct GNUNET_RECLAIM_Attestation * | ||
465 | GNUNET_RECLAIM_attestation_deserialize (const char *data, size_t data_size) | ||
466 | { | ||
467 | struct GNUNET_RECLAIM_Attestation *attestation; | ||
468 | struct Attestation *atts; | ||
469 | size_t data_len; | ||
470 | size_t name_len; | ||
471 | char *write_ptr; | ||
472 | |||
473 | if (data_size < sizeof(struct Attestation)) | ||
474 | return NULL; | ||
475 | |||
476 | atts = (struct Attestation *) data; | ||
477 | data_len = ntohs (atts->data_size); | ||
478 | name_len = ntohs (atts->name_len); | ||
479 | if (data_size < sizeof(struct Attestation) + data_len + name_len) | ||
480 | { | ||
481 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
482 | "Buffer too small to deserialize\n"); | ||
483 | return NULL; | ||
484 | } | ||
485 | attestation = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attestation) | ||
486 | + data_len + name_len + 1); | ||
487 | attestation->type = ntohs (atts->attestation_type); | ||
488 | attestation->flag = ntohl (atts->attestation_flag); | ||
489 | attestation->id = atts->attestation_id; | ||
490 | attestation->data_size = data_len; | ||
491 | |||
492 | write_ptr = (char *) &attestation[1]; | ||
493 | GNUNET_memcpy (write_ptr, &atts[1], name_len); | ||
494 | write_ptr[name_len] = '\0'; | ||
495 | attestation->name = write_ptr; | ||
496 | |||
497 | write_ptr += name_len + 1; | ||
498 | GNUNET_memcpy (write_ptr, (char *) &atts[1] + name_len, | ||
499 | attestation->data_size); | ||
500 | attestation->data = write_ptr; | ||
501 | return attestation; | ||
502 | } | ||
503 | |||
504 | struct GNUNET_RECLAIM_AttributeList* | ||
505 | GNUNET_RECLAIM_attestation_get_attributes (const struct GNUNET_RECLAIM_Attestation *attest) | ||
506 | { | ||
507 | unsigned int i; | ||
508 | struct Plugin *plugin; | ||
509 | struct GNUNET_RECLAIM_AttributeList *ret; | ||
510 | init (); | ||
511 | for (i = 0; i < num_plugins; i++) | ||
512 | { | ||
513 | plugin = attest_plugins[i]; | ||
514 | if (NULL != | ||
515 | (ret = plugin->api->get_attributes (plugin->api->cls, | ||
516 | attest))) | ||
517 | return ret; | ||
518 | } | ||
519 | return NULL; | ||
520 | } | ||
diff --git a/src/reclaim/reclaim_attestation.h b/src/reclaim/reclaim_attestation.h new file mode 100644 index 000000000..5747d8896 --- /dev/null +++ b/src/reclaim/reclaim_attestation.h | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @author Martin Schanzenbach | ||
22 | * @file reclaim-attribute/reclaim_attestation.h | ||
23 | * @brief GNUnet reclaim identity attribute attestations | ||
24 | * | ||
25 | */ | ||
26 | #ifndef RECLAIM_ATTESTATION_H | ||
27 | #define RECLAIM_ATTESTATION_H | ||
28 | |||
29 | #include "gnunet_reclaim_service.h" | ||
30 | |||
31 | /** | ||
32 | * Serialized attestation claim | ||
33 | */ | ||
34 | struct Attestation | ||
35 | { | ||
36 | /** | ||
37 | * Attestation type | ||
38 | */ | ||
39 | uint32_t attestation_type; | ||
40 | |||
41 | /** | ||
42 | * Attestation flag | ||
43 | */ | ||
44 | uint32_t attestation_flag; | ||
45 | |||
46 | /** | ||
47 | * Attestation ID | ||
48 | */ | ||
49 | struct GNUNET_RECLAIM_Identifier attestation_id; | ||
50 | |||
51 | /** | ||
52 | * Name length | ||
53 | */ | ||
54 | uint32_t name_len; | ||
55 | |||
56 | /** | ||
57 | * Data size | ||
58 | */ | ||
59 | uint32_t data_size; | ||
60 | |||
61 | // followed by data_size Attestation value data | ||
62 | }; | ||
63 | |||
64 | #endif | ||
diff --git a/src/reclaim/reclaim_attribute.c b/src/reclaim/reclaim_attribute.c new file mode 100644 index 000000000..12f124c15 --- /dev/null +++ b/src/reclaim/reclaim_attribute.c | |||
@@ -0,0 +1,539 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file reclaim-attribute/reclaim_attribute.c | ||
23 | * @brief helper library to manage identity attributes | ||
24 | * @author Martin Schanzenbach | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_reclaim_plugin.h" | ||
29 | #include "reclaim_attribute.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Handle for a plugin | ||
34 | */ | ||
35 | struct Plugin | ||
36 | { | ||
37 | /** | ||
38 | * Name of the plugin | ||
39 | */ | ||
40 | char *library_name; | ||
41 | |||
42 | /** | ||
43 | * Plugin API | ||
44 | */ | ||
45 | struct GNUNET_RECLAIM_AttributePluginFunctions *api; | ||
46 | }; | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Plugins | ||
51 | */ | ||
52 | static struct Plugin **attr_plugins; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Number of plugins | ||
57 | */ | ||
58 | static unsigned int num_plugins; | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Init canary | ||
63 | */ | ||
64 | static int initialized; | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Add a plugin | ||
69 | * | ||
70 | * @param cls closure | ||
71 | * @param library_name name of the API library | ||
72 | * @param lib_ret the plugin API pointer | ||
73 | */ | ||
74 | static void | ||
75 | add_plugin (void *cls, const char *library_name, void *lib_ret) | ||
76 | { | ||
77 | struct GNUNET_RECLAIM_AttributePluginFunctions *api = lib_ret; | ||
78 | struct Plugin *plugin; | ||
79 | |||
80 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
81 | "Loading attribute plugin `%s'\n", | ||
82 | library_name); | ||
83 | plugin = GNUNET_new (struct Plugin); | ||
84 | plugin->api = api; | ||
85 | plugin->library_name = GNUNET_strdup (library_name); | ||
86 | GNUNET_array_append (attr_plugins, num_plugins, plugin); | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Load plugins | ||
92 | */ | ||
93 | static void | ||
94 | init () | ||
95 | { | ||
96 | if (GNUNET_YES == initialized) | ||
97 | return; | ||
98 | initialized = GNUNET_YES; | ||
99 | GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_", | ||
100 | NULL, | ||
101 | &add_plugin, | ||
102 | NULL); | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Convert a type name to the corresponding number | ||
108 | * | ||
109 | * @param typename name to convert | ||
110 | * @return corresponding number, UINT32_MAX on error | ||
111 | */ | ||
112 | uint32_t | ||
113 | GNUNET_RECLAIM_attribute_typename_to_number (const char *typename) | ||
114 | { | ||
115 | unsigned int i; | ||
116 | struct Plugin *plugin; | ||
117 | uint32_t ret; | ||
118 | |||
119 | init (); | ||
120 | for (i = 0; i < num_plugins; i++) | ||
121 | { | ||
122 | plugin = attr_plugins[i]; | ||
123 | if (UINT32_MAX != | ||
124 | (ret = plugin->api->typename_to_number (plugin->api->cls, typename))) | ||
125 | return ret; | ||
126 | } | ||
127 | return UINT32_MAX; | ||
128 | } | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Convert a type number to the corresponding type string | ||
133 | * | ||
134 | * @param type number of a type | ||
135 | * @return corresponding typestring, NULL on error | ||
136 | */ | ||
137 | const char * | ||
138 | GNUNET_RECLAIM_attribute_number_to_typename (uint32_t type) | ||
139 | { | ||
140 | unsigned int i; | ||
141 | struct Plugin *plugin; | ||
142 | const char *ret; | ||
143 | |||
144 | init (); | ||
145 | for (i = 0; i < num_plugins; i++) | ||
146 | { | ||
147 | plugin = attr_plugins[i]; | ||
148 | if (NULL != | ||
149 | (ret = plugin->api->number_to_typename (plugin->api->cls, type))) | ||
150 | return ret; | ||
151 | } | ||
152 | return NULL; | ||
153 | } | ||
154 | |||
155 | |||
156 | /** | ||
157 | * Convert human-readable version of a 'claim' of an attribute to the binary | ||
158 | * representation | ||
159 | * | ||
160 | * @param type type of the claim | ||
161 | * @param s human-readable string | ||
162 | * @param data set to value in binary encoding (will be allocated) | ||
163 | * @param data_size set to number of bytes in @a data | ||
164 | * @return #GNUNET_OK on success | ||
165 | */ | ||
166 | int | ||
167 | GNUNET_RECLAIM_attribute_string_to_value (uint32_t type, | ||
168 | const char *s, | ||
169 | void **data, | ||
170 | size_t *data_size) | ||
171 | { | ||
172 | unsigned int i; | ||
173 | struct Plugin *plugin; | ||
174 | |||
175 | init (); | ||
176 | for (i = 0; i < num_plugins; i++) | ||
177 | { | ||
178 | plugin = attr_plugins[i]; | ||
179 | if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, | ||
180 | type, | ||
181 | s, | ||
182 | data, | ||
183 | data_size)) | ||
184 | return GNUNET_OK; | ||
185 | } | ||
186 | return GNUNET_SYSERR; | ||
187 | } | ||
188 | |||
189 | |||
190 | /** | ||
191 | * Convert the 'claim' of an attribute to a string | ||
192 | * | ||
193 | * @param type the type of attribute | ||
194 | * @param data claim in binary encoding | ||
195 | * @param data_size number of bytes in @a data | ||
196 | * @return NULL on error, otherwise human-readable representation of the claim | ||
197 | */ | ||
198 | char * | ||
199 | GNUNET_RECLAIM_attribute_value_to_string (uint32_t type, | ||
200 | const void *data, | ||
201 | size_t data_size) | ||
202 | { | ||
203 | unsigned int i; | ||
204 | struct Plugin *plugin; | ||
205 | char *ret; | ||
206 | |||
207 | init (); | ||
208 | for (i = 0; i < num_plugins; i++) | ||
209 | { | ||
210 | plugin = attr_plugins[i]; | ||
211 | if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, | ||
212 | type, | ||
213 | data, | ||
214 | data_size))) | ||
215 | return ret; | ||
216 | } | ||
217 | return NULL; | ||
218 | } | ||
219 | |||
220 | |||
221 | /** | ||
222 | * Create a new attribute. | ||
223 | * | ||
224 | * @param attr_name the attribute name | ||
225 | * @param attestation attestation ID of the attribute (maybe NULL) | ||
226 | * @param type the attribute type | ||
227 | * @param data the attribute value | ||
228 | * @param data_size the attribute value size | ||
229 | * @return the new attribute | ||
230 | */ | ||
231 | struct GNUNET_RECLAIM_Attribute * | ||
232 | GNUNET_RECLAIM_attribute_new (const char *attr_name, | ||
233 | const struct GNUNET_RECLAIM_Identifier *attestation, | ||
234 | uint32_t type, | ||
235 | const void *data, | ||
236 | size_t data_size) | ||
237 | { | ||
238 | struct GNUNET_RECLAIM_Attribute *attr; | ||
239 | char *write_ptr; | ||
240 | char *attr_name_tmp = GNUNET_strdup (attr_name); | ||
241 | |||
242 | GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); | ||
243 | |||
244 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute) | ||
245 | + strlen (attr_name_tmp) + 1 + data_size); | ||
246 | if (NULL != attestation) | ||
247 | attr->attestation = *attestation; | ||
248 | attr->type = type; | ||
249 | attr->data_size = data_size; | ||
250 | attr->flag = 0; | ||
251 | write_ptr = (char *) &attr[1]; | ||
252 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | ||
253 | attr->name = write_ptr; | ||
254 | write_ptr += strlen (attr->name) + 1; | ||
255 | GNUNET_memcpy (write_ptr, data, data_size); | ||
256 | attr->data = write_ptr; | ||
257 | GNUNET_free (attr_name_tmp); | ||
258 | return attr; | ||
259 | } | ||
260 | |||
261 | |||
262 | /** | ||
263 | * Add a new attribute to a claim list | ||
264 | * | ||
265 | * @param attr_name the name of the new attribute claim | ||
266 | * @param type the type of the claim | ||
267 | * @param data claim payload | ||
268 | * @param data_size claim payload size | ||
269 | */ | ||
270 | void | ||
271 | GNUNET_RECLAIM_attribute_list_add ( | ||
272 | struct GNUNET_RECLAIM_AttributeList *al, | ||
273 | const char *attr_name, | ||
274 | const struct GNUNET_RECLAIM_Identifier *attestation, | ||
275 | uint32_t type, | ||
276 | const void *data, | ||
277 | size_t data_size) | ||
278 | { | ||
279 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
280 | |||
281 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
282 | ale->attribute = | ||
283 | GNUNET_RECLAIM_attribute_new (attr_name, attestation, | ||
284 | type, data, data_size); | ||
285 | GNUNET_CONTAINER_DLL_insert (al->list_head, | ||
286 | al->list_tail, | ||
287 | ale); | ||
288 | } | ||
289 | |||
290 | |||
291 | /** | ||
292 | * Get required size for serialization buffer | ||
293 | * | ||
294 | * @param attrs the attribute list to serialize | ||
295 | * @return the required buffer size | ||
296 | */ | ||
297 | size_t | ||
298 | GNUNET_RECLAIM_attribute_list_serialize_get_size ( | ||
299 | const struct GNUNET_RECLAIM_AttributeList *al) | ||
300 | { | ||
301 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
302 | size_t len = 0; | ||
303 | |||
304 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
305 | { | ||
306 | GNUNET_assert (NULL != ale->attribute); | ||
307 | len += GNUNET_RECLAIM_attribute_serialize_get_size (ale->attribute); | ||
308 | len += sizeof(struct GNUNET_RECLAIM_AttributeListEntry); | ||
309 | } | ||
310 | return len; | ||
311 | } | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Serialize an attribute list | ||
316 | * | ||
317 | * @param attrs the attribute list to serialize | ||
318 | * @param result the serialized attribute | ||
319 | * @return length of serialized data | ||
320 | */ | ||
321 | size_t | ||
322 | GNUNET_RECLAIM_attribute_list_serialize ( | ||
323 | const struct GNUNET_RECLAIM_AttributeList *al, | ||
324 | char *result) | ||
325 | { | ||
326 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
327 | size_t len; | ||
328 | size_t total_len; | ||
329 | char *write_ptr; | ||
330 | write_ptr = result; | ||
331 | total_len = 0; | ||
332 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
333 | { | ||
334 | GNUNET_assert (NULL != ale->attribute); | ||
335 | len = GNUNET_RECLAIM_attribute_serialize (ale->attribute, write_ptr); | ||
336 | total_len += len; | ||
337 | write_ptr += len; | ||
338 | } | ||
339 | return total_len; | ||
340 | } | ||
341 | |||
342 | |||
343 | /** | ||
344 | * Deserialize an attribute list | ||
345 | * | ||
346 | * @param data the serialized attribute list | ||
347 | * @param data_size the length of the serialized data | ||
348 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
349 | */ | ||
350 | struct GNUNET_RECLAIM_AttributeList * | ||
351 | GNUNET_RECLAIM_attribute_list_deserialize (const char *data, size_t data_size) | ||
352 | { | ||
353 | struct GNUNET_RECLAIM_AttributeList *al; | ||
354 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
355 | size_t attr_len; | ||
356 | const char *read_ptr; | ||
357 | |||
358 | al = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
359 | if (data_size < sizeof(struct Attribute) + sizeof(struct | ||
360 | GNUNET_RECLAIM_AttributeListEntry)) | ||
361 | return al; | ||
362 | read_ptr = data; | ||
363 | while (((data + data_size) - read_ptr) >= sizeof(struct Attribute)) | ||
364 | { | ||
365 | ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
366 | ale->attribute = | ||
367 | GNUNET_RECLAIM_attribute_deserialize (read_ptr, | ||
368 | data_size - (read_ptr - data)); | ||
369 | GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale); | ||
370 | attr_len = GNUNET_RECLAIM_attribute_serialize_get_size (ale->attribute); | ||
371 | read_ptr += attr_len; | ||
372 | } | ||
373 | return al; | ||
374 | } | ||
375 | |||
376 | |||
377 | /** | ||
378 | * Make a (deep) copy of a claim list | ||
379 | * @param attrs claim list to copy | ||
380 | * @return copied claim list | ||
381 | */ | ||
382 | struct GNUNET_RECLAIM_AttributeList * | ||
383 | GNUNET_RECLAIM_attribute_list_dup ( | ||
384 | const struct GNUNET_RECLAIM_AttributeList *al) | ||
385 | { | ||
386 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
387 | struct GNUNET_RECLAIM_AttributeListEntry *result_ale; | ||
388 | struct GNUNET_RECLAIM_AttributeList *result; | ||
389 | |||
390 | result = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | ||
391 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
392 | { | ||
393 | result_ale = GNUNET_new (struct GNUNET_RECLAIM_AttributeListEntry); | ||
394 | GNUNET_assert (NULL != ale->attribute); | ||
395 | { | ||
396 | result_ale->attribute = | ||
397 | GNUNET_RECLAIM_attribute_new (ale->attribute->name, | ||
398 | &ale->attribute->attestation, | ||
399 | ale->attribute->type, | ||
400 | ale->attribute->data, | ||
401 | ale->attribute->data_size); | ||
402 | |||
403 | result_ale->attribute->id = ale->attribute->id; | ||
404 | result_ale->attribute->flag = ale->attribute->flag; | ||
405 | } | ||
406 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
407 | result->list_tail, | ||
408 | result_ale); | ||
409 | } | ||
410 | return result; | ||
411 | } | ||
412 | |||
413 | |||
414 | /** | ||
415 | * Destroy claim list | ||
416 | * | ||
417 | * @param attrs list to destroy | ||
418 | */ | ||
419 | void | ||
420 | GNUNET_RECLAIM_attribute_list_destroy ( | ||
421 | struct GNUNET_RECLAIM_AttributeList *al) | ||
422 | { | ||
423 | struct GNUNET_RECLAIM_AttributeListEntry *ale; | ||
424 | struct GNUNET_RECLAIM_AttributeListEntry *tmp_ale; | ||
425 | |||
426 | for (ale = al->list_head; NULL != ale;) | ||
427 | { | ||
428 | if (NULL != ale->attribute) | ||
429 | GNUNET_free (ale->attribute); | ||
430 | tmp_ale = ale; | ||
431 | ale = ale->next; | ||
432 | GNUNET_free (tmp_ale); | ||
433 | } | ||
434 | GNUNET_free (al); | ||
435 | } | ||
436 | |||
437 | |||
438 | /** | ||
439 | * Get required size for serialization buffer | ||
440 | * | ||
441 | * @param attr the attribute to serialize | ||
442 | * @return the required buffer size | ||
443 | */ | ||
444 | size_t | ||
445 | GNUNET_RECLAIM_attribute_serialize_get_size ( | ||
446 | const struct GNUNET_RECLAIM_Attribute *attr) | ||
447 | { | ||
448 | return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size; | ||
449 | } | ||
450 | |||
451 | |||
452 | /** | ||
453 | * Serialize an attribute | ||
454 | * | ||
455 | * @param attr the attribute to serialize | ||
456 | * @param result the serialized attribute | ||
457 | * @return length of serialized data | ||
458 | */ | ||
459 | size_t | ||
460 | GNUNET_RECLAIM_attribute_serialize ( | ||
461 | const struct GNUNET_RECLAIM_Attribute *attr, | ||
462 | char *result) | ||
463 | { | ||
464 | size_t data_len_ser; | ||
465 | size_t name_len; | ||
466 | struct Attribute *attr_ser; | ||
467 | char *write_ptr; | ||
468 | |||
469 | attr_ser = (struct Attribute *) result; | ||
470 | attr_ser->attribute_type = htons (attr->type); | ||
471 | attr_ser->attribute_flag = htonl (attr->flag); | ||
472 | attr_ser->attribute_id = attr->id; | ||
473 | attr_ser->attestation_id = attr->attestation; | ||
474 | name_len = strlen (attr->name); | ||
475 | attr_ser->name_len = htons (name_len); | ||
476 | write_ptr = (char *) &attr_ser[1]; | ||
477 | GNUNET_memcpy (write_ptr, attr->name, name_len); | ||
478 | write_ptr += name_len; | ||
479 | // TODO plugin-ize | ||
480 | // data_len_ser = plugin->serialize_attribute_value (attr, | ||
481 | // &attr_ser[1]); | ||
482 | data_len_ser = attr->data_size; | ||
483 | GNUNET_memcpy (write_ptr, attr->data, attr->data_size); | ||
484 | attr_ser->data_size = htons (data_len_ser); | ||
485 | |||
486 | return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size; | ||
487 | } | ||
488 | |||
489 | |||
490 | /** | ||
491 | * Deserialize an attribute | ||
492 | * | ||
493 | * @param data the serialized attribute | ||
494 | * @param data_size the length of the serialized data | ||
495 | * | ||
496 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
497 | */ | ||
498 | struct GNUNET_RECLAIM_Attribute * | ||
499 | GNUNET_RECLAIM_attribute_deserialize (const char *data, size_t data_size) | ||
500 | { | ||
501 | struct GNUNET_RECLAIM_Attribute *attr; | ||
502 | struct Attribute *attr_ser; | ||
503 | size_t data_len; | ||
504 | size_t name_len; | ||
505 | char *write_ptr; | ||
506 | |||
507 | if (data_size < sizeof(struct Attribute)) | ||
508 | return NULL; | ||
509 | |||
510 | attr_ser = (struct Attribute *) data; | ||
511 | data_len = ntohs (attr_ser->data_size); | ||
512 | name_len = ntohs (attr_ser->name_len); | ||
513 | if (data_size < sizeof(struct Attribute) + data_len + name_len) | ||
514 | { | ||
515 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
516 | "Buffer too small to deserialize\n"); | ||
517 | return NULL; | ||
518 | } | ||
519 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Attribute) | ||
520 | + data_len + name_len + 1); | ||
521 | attr->type = ntohs (attr_ser->attribute_type); | ||
522 | attr->flag = ntohl (attr_ser->attribute_flag); | ||
523 | attr->id = attr_ser->attribute_id; | ||
524 | attr->attestation = attr_ser->attestation_id; | ||
525 | attr->data_size = data_len; | ||
526 | |||
527 | write_ptr = (char *) &attr[1]; | ||
528 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); | ||
529 | write_ptr[name_len] = '\0'; | ||
530 | attr->name = write_ptr; | ||
531 | |||
532 | write_ptr += name_len + 1; | ||
533 | GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size); | ||
534 | attr->data = write_ptr; | ||
535 | return attr; | ||
536 | } | ||
537 | |||
538 | |||
539 | /* end of reclaim_attribute.c */ | ||
diff --git a/src/reclaim/reclaim_attribute.h b/src/reclaim/reclaim_attribute.h new file mode 100644 index 000000000..e54b210b9 --- /dev/null +++ b/src/reclaim/reclaim_attribute.h | |||
@@ -0,0 +1,102 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @author Martin Schanzenbach | ||
22 | * @file reclaim-attribute/reclaim_attribute.h | ||
23 | * @brief GNUnet reclaim identity attributes | ||
24 | * | ||
25 | */ | ||
26 | #ifndef RECLAIM_ATTRIBUTE_H | ||
27 | #define RECLAIM_ATTRIBUTE_H | ||
28 | |||
29 | #include "gnunet_reclaim_service.h" | ||
30 | |||
31 | /** | ||
32 | * Serialized claim | ||
33 | */ | ||
34 | struct Attribute | ||
35 | { | ||
36 | /** | ||
37 | * Attribute type | ||
38 | */ | ||
39 | uint32_t attribute_type; | ||
40 | |||
41 | /** | ||
42 | * Attribute flag | ||
43 | */ | ||
44 | uint32_t attribute_flag; | ||
45 | |||
46 | /** | ||
47 | * Attribute ID | ||
48 | */ | ||
49 | struct GNUNET_RECLAIM_Identifier attribute_id; | ||
50 | |||
51 | /** | ||
52 | * Attestation ID | ||
53 | */ | ||
54 | struct GNUNET_RECLAIM_Identifier attestation_id; | ||
55 | |||
56 | /** | ||
57 | * Name length | ||
58 | */ | ||
59 | uint32_t name_len; | ||
60 | |||
61 | /** | ||
62 | * Data size | ||
63 | */ | ||
64 | uint32_t data_size; | ||
65 | |||
66 | // followed by data_size Attribute value data | ||
67 | }; | ||
68 | |||
69 | /** | ||
70 | * Serialized attestation claim | ||
71 | */ | ||
72 | struct Attestation | ||
73 | { | ||
74 | /** | ||
75 | * Attestation type | ||
76 | */ | ||
77 | uint32_t attestation_type; | ||
78 | |||
79 | /** | ||
80 | * Attestation flag | ||
81 | */ | ||
82 | uint32_t attestation_flag; | ||
83 | |||
84 | /** | ||
85 | * Attestation ID | ||
86 | */ | ||
87 | struct GNUNET_RECLAIM_Identifier attestation_id; | ||
88 | |||
89 | /** | ||
90 | * Name length | ||
91 | */ | ||
92 | uint32_t name_len; | ||
93 | |||
94 | /** | ||
95 | * Data size | ||
96 | */ | ||
97 | uint32_t data_size; | ||
98 | |||
99 | // followed by data_size Attestation value data | ||
100 | }; | ||
101 | |||
102 | #endif | ||