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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file credential/plugin_gnsrecord_credential.c
23 * @brief gnsrecord plugin to provide the API for CREDENTIAL records
24 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_credential_service.h"
31 #include "gnunet_gnsrecord_plugin.h"
32 #include "gnunet_signatures.h"
33 #include "credential_serialization.h"
34 #include "credential_misc.h"
37 * Convert the 'value' of a record to a string.
39 * @param cls closure, unused
40 * @param type type of the record
41 * @param data value in binary encoding
42 * @param data_size number of bytes in @a data
43 * @return NULL on error, otherwise human-readable representation of the value
46 credential_value_to_string (void *cls,
56 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
58 struct GNUNET_CREDENTIAL_DelegationRecord sets;
63 if (data_size < sizeof (struct GNUNET_CREDENTIAL_DelegationRecord))
64 return NULL; /* malformed */
69 struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets.set_count)];
70 if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize (GNUNET_ntohll (sets.data_size),
71 &cdata[sizeof (sets)],
72 ntohl (sets.set_count),
76 for (i=0;i<ntohl(sets.set_count);i++)
78 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key);
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "%d len attr\n", set[i].subject_attribute_len);
81 if (0 == set[i].subject_attribute_len)
85 GNUNET_asprintf (&attr_str,
89 GNUNET_asprintf (&tmp_str,
93 GNUNET_free (attr_str);
99 GNUNET_asprintf (&attr_str,
102 set[i].subject_attribute);
104 GNUNET_asprintf (&tmp_str,
108 set[i].subject_attribute);
109 GNUNET_free (attr_str);
113 GNUNET_free (subject_pkey);
117 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
119 struct GNUNET_CREDENTIAL_Credential *cred;
122 cred = GNUNET_CREDENTIAL_credential_deserialize (data,
124 cred_str = GNUNET_CREDENTIAL_credential_to_string (cred);
128 case GNUNET_GNSRECORD_TYPE_POLICY:
130 return GNUNET_strndup (data,data_size);
139 * Convert human-readable version of a 'value' of a record to the binary
142 * @param cls closure, unused
143 * @param type type of the record
144 * @param s human-readable string
145 * @param data set to value in binary encoding (will be allocated)
146 * @param data_size set to number of bytes in @a data
147 * @return #GNUNET_OK on success
150 credential_string_to_value (void *cls,
157 return GNUNET_SYSERR;
160 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
162 struct GNUNET_CREDENTIAL_DelegationRecord *sets;
163 char attr_str[253 + 1];
164 char subject_pkey[52 + 1];
169 size_t tmp_data_size;
172 tmp_str = GNUNET_strdup (s);
173 token = strtok (tmp_str, ",");
176 *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord);
177 while (NULL != token)
179 matches = SSCANF (token,
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 _("Unable to parse ATTR record string `%s'\n"),
188 GNUNET_free (tmp_str);
189 return GNUNET_SYSERR;
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;
197 token = strtok (NULL, ",");
199 GNUNET_free (tmp_str);
200 tmp_str = GNUNET_strdup (s);
201 token = strtok (tmp_str, ",");
204 GNUNET_free (tmp_str);
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 "Malformed string %s\n", s);
207 return GNUNET_SYSERR;
209 struct GNUNET_CREDENTIAL_DelegationSet set[entries];
210 for (i=0;i<entries;i++)
212 matches = SSCANF (token,
216 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
217 strlen (subject_pkey),
218 &set[i].subject_key);
220 set[i].subject_attribute_len = strlen (attr_str) + 1;
221 set[i].subject_attribute = GNUNET_strdup (attr_str);
223 token = strtok (NULL , ",");
225 tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries,
228 if (-1 == tmp_data_size)
230 GNUNET_free (tmp_str);
231 return GNUNET_SYSERR;
233 *data_size += tmp_data_size;
234 *data = sets = GNUNET_malloc (*data_size);
235 GNUNET_CREDENTIAL_delegation_set_serialize (entries,
239 for (i=0;i<entries;i++)
241 if (0 != set[i].subject_attribute_len)
242 GNUNET_free ((char*)set[i].subject_attribute);
244 sets->set_count = htonl (entries);
245 sets->data_size = GNUNET_htonll (tmp_data_size);
247 GNUNET_free (tmp_str);
250 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
252 struct GNUNET_CREDENTIAL_Credential *cred;
253 cred = GNUNET_CREDENTIAL_credential_from_string (s);
255 *data_size = GNUNET_CREDENTIAL_credential_serialize (cred,
259 case GNUNET_GNSRECORD_TYPE_POLICY:
261 *data_size = strlen (s);
262 *data = GNUNET_strdup (s);
266 return GNUNET_SYSERR;
272 * Mapping of record type numbers to human-readable
279 { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
280 { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
281 { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY },
287 * Convert a type name (i.e. "AAAA") to the corresponding number.
289 * @param cls closure, unused
290 * @param gns_typename name to convert
291 * @return corresponding number, UINT32_MAX on error
294 credential_typename_to_number (void *cls,
295 const char *gns_typename)
300 while ( (name_map[i].name != NULL) &&
301 (0 != strcasecmp (gns_typename, name_map[i].name)) )
303 return name_map[i].number;
308 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
310 * @param cls closure, unused
311 * @param type number of a type to convert
312 * @return corresponding typestring, NULL on error
315 credential_number_to_typename (void *cls,
321 while ( (name_map[i].name != NULL) &&
322 (type != name_map[i].number) )
324 return name_map[i].name;
329 * Entry point for the plugin.
332 * @return the exported block API
335 libgnunet_plugin_gnsrecord_credential_init (void *cls)
337 struct GNUNET_GNSRECORD_PluginFunctions *api;
339 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
340 api->value_to_string = &credential_value_to_string;
341 api->string_to_value = &credential_string_to_value;
342 api->typename_to_number = &credential_typename_to_number;
343 api->number_to_typename = &credential_number_to_typename;
349 * Exit point from the plugin.
351 * @param cls the return value from #libgnunet_plugin_block_test_init
355 libgnunet_plugin_gnsrecord_credential_done (void *cls)
357 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
363 /* end of plugin_gnsrecord_credential.c */