diff options
Diffstat (limited to 'src/credential/plugin_gnsrecord_credential.c')
-rw-r--r-- | src/credential/plugin_gnsrecord_credential.c | 329 |
1 files changed, 139 insertions, 190 deletions
diff --git a/src/credential/plugin_gnsrecord_credential.c b/src/credential/plugin_gnsrecord_credential.c index 1e1c0ff92..c03f2b3e4 100644 --- a/src/credential/plugin_gnsrecord_credential.c +++ b/src/credential/plugin_gnsrecord_credential.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -23,16 +23,16 @@ | |||
23 | * @brief gnsrecord plugin to provide the API for CREDENTIAL records | 23 | * @brief gnsrecord plugin to provide the API for CREDENTIAL records |
24 | * @author Martin Schanzenbach | 24 | * @author Martin Schanzenbach |
25 | */ | 25 | */ |
26 | |||
27 | #include "platform.h" | 26 | #include "platform.h" |
27 | |||
28 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
29 | #include "gnunet_gnsrecord_lib.h" | 29 | |
30 | #include "credential_misc.h" | ||
31 | #include "credential_serialization.h" | ||
30 | #include "gnunet_credential_service.h" | 32 | #include "gnunet_credential_service.h" |
33 | #include "gnunet_gnsrecord_lib.h" | ||
31 | #include "gnunet_gnsrecord_plugin.h" | 34 | #include "gnunet_gnsrecord_plugin.h" |
32 | #include "gnunet_signatures.h" | 35 | #include "gnunet_signatures.h" |
33 | #include "credential_serialization.h" | ||
34 | #include "credential_misc.h" | ||
35 | |||
36 | /** | 36 | /** |
37 | * Convert the 'value' of a record to a string. | 37 | * Convert the 'value' of a record to a string. |
38 | * | 38 | * |
@@ -43,18 +43,14 @@ | |||
43 | * @return NULL on error, otherwise human-readable representation of the value | 43 | * @return NULL on error, otherwise human-readable representation of the value |
44 | */ | 44 | */ |
45 | static char * | 45 | static char * |
46 | credential_value_to_string (void *cls, | 46 | credential_value_to_string (void *cls, uint32_t type, const void *data, |
47 | uint32_t type, | 47 | size_t data_size) |
48 | const void *data, | ||
49 | size_t data_size) | ||
50 | { | 48 | { |
51 | 49 | ||
52 | const char *cdata; | 50 | const char *cdata; |
53 | 51 | ||
54 | switch (type) | 52 | switch (type) { |
55 | { | 53 | case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: { |
56 | case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: | ||
57 | { | ||
58 | struct GNUNET_CREDENTIAL_DelegationRecord sets; | 54 | struct GNUNET_CREDENTIAL_DelegationRecord sets; |
59 | char *attr_str; | 55 | char *attr_str; |
60 | char *subject_pkey; | 56 | char *subject_pkey; |
@@ -62,49 +58,33 @@ credential_value_to_string (void *cls, | |||
62 | int i; | 58 | int i; |
63 | if (data_size < sizeof (struct GNUNET_CREDENTIAL_DelegationRecord)) | 59 | if (data_size < sizeof (struct GNUNET_CREDENTIAL_DelegationRecord)) |
64 | return NULL; /* malformed */ | 60 | return NULL; /* malformed */ |
65 | GNUNET_memcpy (&sets, | 61 | GNUNET_memcpy (&sets, data, sizeof (sets)); |
66 | data, | ||
67 | sizeof (sets)); | ||
68 | cdata = data; | 62 | cdata = data; |
69 | struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets.set_count)]; | 63 | struct GNUNET_CREDENTIAL_DelegationSet set[ntohl (sets.set_count)]; |
70 | if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize (GNUNET_ntohll (sets.data_size), | 64 | if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize ( |
71 | &cdata[sizeof (sets)], | 65 | GNUNET_ntohll (sets.data_size), &cdata[sizeof (sets)], |
72 | ntohl (sets.set_count), | 66 | ntohl (sets.set_count), set)) |
73 | set)) | ||
74 | return NULL; | 67 | return NULL; |
75 | 68 | ||
76 | for (i=0;i<ntohl(sets.set_count);i++) | 69 | for (i = 0; i < ntohl (sets.set_count); i++) { |
77 | { | 70 | subject_pkey = |
78 | subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key); | 71 | GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key); |
79 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 72 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d len attr\n", |
80 | "%d len attr\n", set[i].subject_attribute_len); | 73 | set[i].subject_attribute_len); |
81 | if (0 == set[i].subject_attribute_len) | 74 | if (0 == set[i].subject_attribute_len) { |
82 | { | 75 | if (0 == i) { |
83 | if (0 == i) | 76 | GNUNET_asprintf (&attr_str, "%s", subject_pkey); |
84 | { | ||
85 | GNUNET_asprintf (&attr_str, | ||
86 | "%s", | ||
87 | subject_pkey); | ||
88 | } else { | 77 | } else { |
89 | GNUNET_asprintf (&tmp_str, | 78 | GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey); |
90 | "%s,%s", | ||
91 | attr_str, | ||
92 | subject_pkey); | ||
93 | GNUNET_free (attr_str); | 79 | GNUNET_free (attr_str); |
94 | attr_str = tmp_str; | 80 | attr_str = tmp_str; |
95 | } | 81 | } |
96 | } else { | 82 | } else { |
97 | if (0 == i) | 83 | if (0 == i) { |
98 | { | 84 | GNUNET_asprintf (&attr_str, "%s %s", subject_pkey, |
99 | GNUNET_asprintf (&attr_str, | ||
100 | "%s %s", | ||
101 | subject_pkey, | ||
102 | set[i].subject_attribute); | 85 | set[i].subject_attribute); |
103 | } else { | 86 | } else { |
104 | GNUNET_asprintf (&tmp_str, | 87 | GNUNET_asprintf (&tmp_str, "%s,%s %s", attr_str, subject_pkey, |
105 | "%s,%s %s", | ||
106 | attr_str, | ||
107 | subject_pkey, | ||
108 | set[i].subject_attribute); | 88 | set[i].subject_attribute); |
109 | GNUNET_free (attr_str); | 89 | GNUNET_free (attr_str); |
110 | attr_str = tmp_str; | 90 | attr_str = tmp_str; |
@@ -113,24 +93,21 @@ credential_value_to_string (void *cls, | |||
113 | GNUNET_free (subject_pkey); | 93 | GNUNET_free (subject_pkey); |
114 | } | 94 | } |
115 | return attr_str; | 95 | return attr_str; |
116 | } | 96 | } |
117 | case GNUNET_GNSRECORD_TYPE_CREDENTIAL: | 97 | case GNUNET_GNSRECORD_TYPE_CREDENTIAL: { |
118 | { | 98 | struct GNUNET_CREDENTIAL_Credential *cred; |
119 | struct GNUNET_CREDENTIAL_Credential *cred; | 99 | char *cred_str; |
120 | char *cred_str; | 100 | |
121 | 101 | cred = GNUNET_CREDENTIAL_credential_deserialize (data, data_size); | |
122 | cred = GNUNET_CREDENTIAL_credential_deserialize (data, | 102 | cred_str = GNUNET_CREDENTIAL_credential_to_string (cred); |
123 | data_size); | 103 | GNUNET_free (cred); |
124 | cred_str = GNUNET_CREDENTIAL_credential_to_string (cred); | 104 | return cred_str; |
125 | GNUNET_free (cred); | 105 | } |
126 | return cred_str; | 106 | case GNUNET_GNSRECORD_TYPE_POLICY: { |
127 | } | 107 | return GNUNET_strndup (data, data_size); |
128 | case GNUNET_GNSRECORD_TYPE_POLICY: | 108 | } |
129 | { | 109 | default: |
130 | return GNUNET_strndup (data,data_size); | 110 | return NULL; |
131 | } | ||
132 | default: | ||
133 | return NULL; | ||
134 | } | 111 | } |
135 | } | 112 | } |
136 | 113 | ||
@@ -147,123 +124,99 @@ credential_value_to_string (void *cls, | |||
147 | * @return #GNUNET_OK on success | 124 | * @return #GNUNET_OK on success |
148 | */ | 125 | */ |
149 | static int | 126 | static int |
150 | credential_string_to_value (void *cls, | 127 | credential_string_to_value (void *cls, uint32_t type, const char *s, |
151 | uint32_t type, | 128 | void **data, size_t *data_size) |
152 | const char *s, | ||
153 | void **data, | ||
154 | size_t *data_size) | ||
155 | { | 129 | { |
156 | if (NULL == s) | 130 | if (NULL == s) |
157 | return GNUNET_SYSERR; | 131 | return GNUNET_SYSERR; |
158 | switch (type) | 132 | switch (type) { |
159 | { | 133 | case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: { |
160 | case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: | 134 | struct GNUNET_CREDENTIAL_DelegationRecord *sets; |
161 | { | 135 | char attr_str[253 + 1]; |
162 | struct GNUNET_CREDENTIAL_DelegationRecord *sets; | 136 | char subject_pkey[52 + 1]; |
163 | char attr_str[253 + 1]; | 137 | char *token; |
164 | char subject_pkey[52 + 1]; | 138 | char *tmp_str; |
165 | char *token; | 139 | int matches = 0; |
166 | char *tmp_str; | 140 | int entries; |
167 | int matches = 0; | 141 | size_t tmp_data_size; |
168 | int entries; | 142 | int i; |
169 | size_t tmp_data_size; | ||
170 | int i; | ||
171 | |||
172 | tmp_str = GNUNET_strdup (s); | ||
173 | token = strtok (tmp_str, ","); | ||
174 | entries = 0; | ||
175 | tmp_data_size = 0; | ||
176 | *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord); | ||
177 | while (NULL != token) | ||
178 | { | ||
179 | matches = SSCANF (token, | ||
180 | "%s %s", | ||
181 | subject_pkey, | ||
182 | attr_str); | ||
183 | if (0 == matches) | ||
184 | { | ||
185 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
186 | _("Unable to parse ATTR record string `%s'\n"), | ||
187 | s); | ||
188 | GNUNET_free (tmp_str); | ||
189 | return GNUNET_SYSERR; | ||
190 | } | ||
191 | if (1 == matches) { | ||
192 | tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet); | ||
193 | } else if (2 == matches) { | ||
194 | tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet) + strlen (attr_str) + 1; | ||
195 | } | ||
196 | entries++; | ||
197 | token = strtok (NULL, ","); | ||
198 | } | ||
199 | GNUNET_free (tmp_str); | ||
200 | tmp_str = GNUNET_strdup (s); | ||
201 | token = strtok (tmp_str, ","); | ||
202 | if (NULL == token) | ||
203 | { | ||
204 | GNUNET_free (tmp_str); | ||
205 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
206 | "Malformed string %s\n", s); | ||
207 | return GNUNET_SYSERR; | ||
208 | } | ||
209 | struct GNUNET_CREDENTIAL_DelegationSet set[entries]; | ||
210 | for (i=0;i<entries;i++) | ||
211 | { | ||
212 | matches = SSCANF (token, | ||
213 | "%s %s", | ||
214 | subject_pkey, | ||
215 | attr_str); | ||
216 | GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey, | ||
217 | strlen (subject_pkey), | ||
218 | &set[i].subject_key); | ||
219 | if (2 == matches) { | ||
220 | set[i].subject_attribute_len = strlen (attr_str) + 1; | ||
221 | set[i].subject_attribute = GNUNET_strdup (attr_str); | ||
222 | } | ||
223 | token = strtok (NULL , ","); | ||
224 | } | ||
225 | tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries, | ||
226 | set); | ||
227 | |||
228 | if (-1 == tmp_data_size) | ||
229 | { | ||
230 | GNUNET_free (tmp_str); | ||
231 | return GNUNET_SYSERR; | ||
232 | } | ||
233 | *data_size += tmp_data_size; | ||
234 | *data = sets = GNUNET_malloc (*data_size); | ||
235 | GNUNET_CREDENTIAL_delegation_set_serialize (entries, | ||
236 | set, | ||
237 | tmp_data_size, | ||
238 | (char*)&sets[1]); | ||
239 | for (i=0;i<entries;i++) | ||
240 | { | ||
241 | if (0 != set[i].subject_attribute_len) | ||
242 | GNUNET_free ((char*)set[i].subject_attribute); | ||
243 | } | ||
244 | sets->set_count = htonl (entries); | ||
245 | sets->data_size = GNUNET_htonll (tmp_data_size); | ||
246 | 143 | ||
144 | tmp_str = GNUNET_strdup (s); | ||
145 | token = strtok (tmp_str, ","); | ||
146 | entries = 0; | ||
147 | tmp_data_size = 0; | ||
148 | *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord); | ||
149 | while (NULL != token) { | ||
150 | matches = SSCANF (token, "%s %s", subject_pkey, attr_str); | ||
151 | if (0 == matches) { | ||
152 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
153 | _ ("Unable to parse ATTR record string `%s'\n"), s); | ||
247 | GNUNET_free (tmp_str); | 154 | GNUNET_free (tmp_str); |
248 | return GNUNET_OK; | 155 | return GNUNET_SYSERR; |
249 | } | 156 | } |
250 | case GNUNET_GNSRECORD_TYPE_CREDENTIAL: | 157 | if (1 == matches) { |
251 | { | 158 | tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet); |
252 | struct GNUNET_CREDENTIAL_Credential *cred; | 159 | } else if (2 == matches) { |
253 | cred = GNUNET_CREDENTIAL_credential_from_string (s); | 160 | tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet) + |
254 | 161 | strlen (attr_str) + 1; | |
255 | *data_size = GNUNET_CREDENTIAL_credential_serialize (cred, | ||
256 | (char**)data); | ||
257 | return GNUNET_OK; | ||
258 | } | 162 | } |
259 | case GNUNET_GNSRECORD_TYPE_POLICY: | 163 | entries++; |
260 | { | 164 | token = strtok (NULL, ","); |
261 | *data_size = strlen (s); | 165 | } |
262 | *data = GNUNET_strdup (s); | 166 | GNUNET_free (tmp_str); |
263 | return GNUNET_OK; | 167 | tmp_str = GNUNET_strdup (s); |
168 | token = strtok (tmp_str, ","); | ||
169 | if (NULL == token) { | ||
170 | GNUNET_free (tmp_str); | ||
171 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s); | ||
172 | return GNUNET_SYSERR; | ||
173 | } | ||
174 | struct GNUNET_CREDENTIAL_DelegationSet set[entries]; | ||
175 | memset (set, 0, sizeof (struct GNUNET_CREDENTIAL_DelegationSet) * entries); | ||
176 | for (i = 0; i < entries; i++) { | ||
177 | matches = SSCANF (token, "%s %s", subject_pkey, attr_str); | ||
178 | GNUNET_CRYPTO_ecdsa_public_key_from_string ( | ||
179 | subject_pkey, strlen (subject_pkey), &set[i].subject_key); | ||
180 | if (2 == matches) { | ||
181 | set[i].subject_attribute_len = strlen (attr_str) + 1; | ||
182 | set[i].subject_attribute = GNUNET_strdup (attr_str); | ||
264 | } | 183 | } |
265 | default: | 184 | token = strtok (NULL, ","); |
185 | } | ||
186 | tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries, set); | ||
187 | |||
188 | if (-1 == tmp_data_size) { | ||
189 | GNUNET_free (tmp_str); | ||
266 | return GNUNET_SYSERR; | 190 | return GNUNET_SYSERR; |
191 | } | ||
192 | *data_size += tmp_data_size; | ||
193 | *data = sets = GNUNET_malloc (*data_size); | ||
194 | GNUNET_CREDENTIAL_delegation_set_serialize (entries, set, tmp_data_size, | ||
195 | (char *)&sets[1]); | ||
196 | for (i = 0; i < entries; i++) { | ||
197 | if (0 != set[i].subject_attribute_len) | ||
198 | GNUNET_free ((char *)set[i].subject_attribute); | ||
199 | } | ||
200 | sets->set_count = htonl (entries); | ||
201 | sets->data_size = GNUNET_htonll (tmp_data_size); | ||
202 | |||
203 | GNUNET_free (tmp_str); | ||
204 | return GNUNET_OK; | ||
205 | } | ||
206 | case GNUNET_GNSRECORD_TYPE_CREDENTIAL: { | ||
207 | struct GNUNET_CREDENTIAL_Credential *cred; | ||
208 | cred = GNUNET_CREDENTIAL_credential_from_string (s); | ||
209 | |||
210 | *data_size = GNUNET_CREDENTIAL_credential_serialize (cred, (char **)data); | ||
211 | return GNUNET_OK; | ||
212 | } | ||
213 | case GNUNET_GNSRECORD_TYPE_POLICY: { | ||
214 | *data_size = strlen (s); | ||
215 | *data = GNUNET_strdup (s); | ||
216 | return GNUNET_OK; | ||
217 | } | ||
218 | default: | ||
219 | return GNUNET_SYSERR; | ||
267 | } | 220 | } |
268 | } | 221 | } |
269 | 222 | ||
@@ -272,15 +225,14 @@ credential_string_to_value (void *cls, | |||
272 | * Mapping of record type numbers to human-readable | 225 | * Mapping of record type numbers to human-readable |
273 | * record type names. | 226 | * record type names. |
274 | */ | 227 | */ |
275 | static struct { | 228 | static struct |
229 | { | ||
276 | const char *name; | 230 | const char *name; |
277 | uint32_t number; | 231 | uint32_t number; |
278 | } name_map[] = { | 232 | } name_map[] = {{"CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL}, |
279 | { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL }, | 233 | {"ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE}, |
280 | { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE }, | 234 | {"POLICY", GNUNET_GNSRECORD_TYPE_POLICY}, |
281 | { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY }, | 235 | {NULL, UINT32_MAX}}; |
282 | { NULL, UINT32_MAX } | ||
283 | }; | ||
284 | 236 | ||
285 | 237 | ||
286 | /** | 238 | /** |
@@ -291,14 +243,13 @@ static struct { | |||
291 | * @return corresponding number, UINT32_MAX on error | 243 | * @return corresponding number, UINT32_MAX on error |
292 | */ | 244 | */ |
293 | static uint32_t | 245 | static uint32_t |
294 | credential_typename_to_number (void *cls, | 246 | credential_typename_to_number (void *cls, const char *gns_typename) |
295 | const char *gns_typename) | ||
296 | { | 247 | { |
297 | unsigned int i; | 248 | unsigned int i; |
298 | 249 | ||
299 | i=0; | 250 | i = 0; |
300 | while ( (name_map[i].name != NULL) && | 251 | while ((name_map[i].name != NULL) && |
301 | (0 != strcasecmp (gns_typename, name_map[i].name)) ) | 252 | (0 != strcasecmp (gns_typename, name_map[i].name))) |
302 | i++; | 253 | i++; |
303 | return name_map[i].number; | 254 | return name_map[i].number; |
304 | } | 255 | } |
@@ -312,14 +263,12 @@ credential_typename_to_number (void *cls, | |||
312 | * @return corresponding typestring, NULL on error | 263 | * @return corresponding typestring, NULL on error |
313 | */ | 264 | */ |
314 | static const char * | 265 | static const char * |
315 | credential_number_to_typename (void *cls, | 266 | credential_number_to_typename (void *cls, uint32_t type) |
316 | uint32_t type) | ||
317 | { | 267 | { |
318 | unsigned int i; | 268 | unsigned int i; |
319 | 269 | ||
320 | i=0; | 270 | i = 0; |
321 | while ( (name_map[i].name != NULL) && | 271 | while ((name_map[i].name != NULL) && (type != name_map[i].number)) |
322 | (type != name_map[i].number) ) | ||
323 | i++; | 272 | i++; |
324 | return name_map[i].name; | 273 | return name_map[i].name; |
325 | } | 274 | } |