2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
20 * @file credential/plugin_gnsrecord_credential.c
21 * @brief gnsrecord plugin to provide the API for CREDENTIAL records
22 * @author Martin Schanzenbach
26 #include "gnunet_util_lib.h"
27 #include "gnunet_gnsrecord_lib.h"
28 #include "gnunet_credential_service.h"
29 #include "gnunet_gnsrecord_plugin.h"
30 #include "gnunet_signatures.h"
31 #include "credential_serialization.h"
32 #include "credential_misc.h"
35 * Convert the 'value' of a record to a string.
37 * @param cls closure, unused
38 * @param type type of the record
39 * @param data value in binary encoding
40 * @param data_size number of bytes in @a data
41 * @return NULL on error, otherwise human-readable representation of the value
44 credential_value_to_string (void *cls,
54 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
56 struct GNUNET_CREDENTIAL_DelegationRecord sets;
61 if (data_size < sizeof (struct GNUNET_CREDENTIAL_DelegationRecord))
62 return NULL; /* malformed */
67 struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets.set_count)];
68 if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize (GNUNET_ntohll (sets.data_size),
69 &cdata[sizeof (sets)],
70 ntohl (sets.set_count),
74 for (i=0;i<ntohl(sets.set_count);i++)
76 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key);
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78 "%d len attr\n", set[i].subject_attribute_len);
79 if (0 == set[i].subject_attribute_len)
83 GNUNET_asprintf (&attr_str,
87 GNUNET_asprintf (&tmp_str,
91 GNUNET_free (attr_str);
97 GNUNET_asprintf (&attr_str,
100 set[i].subject_attribute);
102 GNUNET_asprintf (&tmp_str,
106 set[i].subject_attribute);
107 GNUNET_free (attr_str);
111 GNUNET_free (subject_pkey);
115 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
117 struct GNUNET_CREDENTIAL_Credential *cred;
120 cred = GNUNET_CREDENTIAL_credential_deserialize (data,
122 cred_str = GNUNET_CREDENTIAL_credential_to_string (cred);
126 case GNUNET_GNSRECORD_TYPE_POLICY:
128 return GNUNET_strndup (data,data_size);
137 * Convert human-readable version of a 'value' of a record to the binary
140 * @param cls closure, unused
141 * @param type type of the record
142 * @param s human-readable string
143 * @param data set to value in binary encoding (will be allocated)
144 * @param data_size set to number of bytes in @a data
145 * @return #GNUNET_OK on success
148 credential_string_to_value (void *cls,
155 return GNUNET_SYSERR;
158 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
160 struct GNUNET_CREDENTIAL_DelegationRecord *sets;
161 char attr_str[253 + 1];
162 char subject_pkey[52 + 1];
167 size_t tmp_data_size;
170 tmp_str = GNUNET_strdup (s);
171 token = strtok (tmp_str, ",");
174 *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord);
175 while (NULL != token)
177 matches = SSCANF (token,
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 _("Unable to parse ATTR record string `%s'\n"),
186 GNUNET_free (tmp_str);
187 return GNUNET_SYSERR;
190 tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet);
191 } else if (2 == matches) {
192 tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet) + strlen (attr_str) + 1;
195 token = strtok (NULL, ",");
197 GNUNET_free (tmp_str);
198 tmp_str = GNUNET_strdup (s);
199 token = strtok (tmp_str, ",");
202 GNUNET_free (tmp_str);
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 "Malformed string %s\n", s);
205 return GNUNET_SYSERR;
207 struct GNUNET_CREDENTIAL_DelegationSet set[entries];
208 for (i=0;i<entries;i++)
210 matches = SSCANF (token,
214 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
215 strlen (subject_pkey),
216 &set[i].subject_key);
218 set[i].subject_attribute_len = strlen (attr_str) + 1;
219 set[i].subject_attribute = GNUNET_strdup (attr_str);
221 token = strtok (NULL , ",");
223 tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries,
226 if (-1 == tmp_data_size)
228 GNUNET_free (tmp_str);
229 return GNUNET_SYSERR;
231 *data_size += tmp_data_size;
232 *data = sets = GNUNET_malloc (*data_size);
233 GNUNET_CREDENTIAL_delegation_set_serialize (entries,
237 for (i=0;i<entries;i++)
239 if (0 != set[i].subject_attribute_len)
240 GNUNET_free ((char*)set[i].subject_attribute);
242 sets->set_count = htonl (entries);
243 sets->data_size = GNUNET_htonll (tmp_data_size);
245 GNUNET_free (tmp_str);
248 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
250 struct GNUNET_CREDENTIAL_Credential *cred;
251 cred = GNUNET_CREDENTIAL_credential_from_string (s);
253 *data_size = GNUNET_CREDENTIAL_credential_serialize (cred,
257 case GNUNET_GNSRECORD_TYPE_POLICY:
259 *data_size = strlen (s);
260 *data = GNUNET_strdup (s);
264 return GNUNET_SYSERR;
270 * Mapping of record type numbers to human-readable
277 { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
278 { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
279 { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY },
285 * Convert a type name (i.e. "AAAA") to the corresponding number.
287 * @param cls closure, unused
288 * @param gns_typename name to convert
289 * @return corresponding number, UINT32_MAX on error
292 credential_typename_to_number (void *cls,
293 const char *gns_typename)
298 while ( (name_map[i].name != NULL) &&
299 (0 != strcasecmp (gns_typename, name_map[i].name)) )
301 return name_map[i].number;
306 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
308 * @param cls closure, unused
309 * @param type number of a type to convert
310 * @return corresponding typestring, NULL on error
313 credential_number_to_typename (void *cls,
319 while ( (name_map[i].name != NULL) &&
320 (type != name_map[i].number) )
322 return name_map[i].name;
327 * Entry point for the plugin.
330 * @return the exported block API
333 libgnunet_plugin_gnsrecord_credential_init (void *cls)
335 struct GNUNET_GNSRECORD_PluginFunctions *api;
337 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
338 api->value_to_string = &credential_value_to_string;
339 api->string_to_value = &credential_string_to_value;
340 api->typename_to_number = &credential_typename_to_number;
341 api->number_to_typename = &credential_number_to_typename;
347 * Exit point from the plugin.
349 * @param cls the return value from #libgnunet_plugin_block_test_init
353 libgnunet_plugin_gnsrecord_credential_done (void *cls)
355 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
361 /* end of plugin_gnsrecord_credential.c */