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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
30 #include "credential_misc.h"
31 #include "credential_serialization.h"
32 #include "gnunet_credential_service.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_gnsrecord_plugin.h"
35 #include "gnunet_signatures.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, uint32_t type, const void *data,
53 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
54 struct GNUNET_CREDENTIAL_DelegationRecord sets;
59 if (data_size < sizeof(struct GNUNET_CREDENTIAL_DelegationRecord))
60 return NULL; /* malformed */
61 GNUNET_memcpy(&sets, data, sizeof(sets));
63 struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets.set_count)];
64 if (GNUNET_OK != GNUNET_CREDENTIAL_delegation_set_deserialize(
65 GNUNET_ntohll(sets.data_size), &cdata[sizeof(sets)],
66 ntohl(sets.set_count), set))
69 for (i = 0; i < ntohl(sets.set_count); i++)
72 GNUNET_CRYPTO_ecdsa_public_key_to_string(&set[i].subject_key);
73 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%d len attr\n",
74 set[i].subject_attribute_len);
75 if (0 == set[i].subject_attribute_len)
79 GNUNET_asprintf(&attr_str, "%s", subject_pkey);
83 GNUNET_asprintf(&tmp_str, "%s,%s", attr_str, subject_pkey);
84 GNUNET_free(attr_str);
92 GNUNET_asprintf(&attr_str, "%s %s", subject_pkey,
93 set[i].subject_attribute);
97 GNUNET_asprintf(&tmp_str, "%s,%s %s", attr_str, subject_pkey,
98 set[i].subject_attribute);
99 GNUNET_free(attr_str);
103 GNUNET_free(subject_pkey);
108 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
109 struct GNUNET_CREDENTIAL_Credential *cred;
112 cred = GNUNET_CREDENTIAL_credential_deserialize(data, data_size);
113 cred_str = GNUNET_CREDENTIAL_credential_to_string(cred);
118 case GNUNET_GNSRECORD_TYPE_POLICY: {
119 return GNUNET_strndup(data, data_size);
129 * Convert human-readable version of a 'value' of a record to the binary
132 * @param cls closure, unused
133 * @param type type of the record
134 * @param s human-readable string
135 * @param data set to value in binary encoding (will be allocated)
136 * @param data_size set to number of bytes in @a data
137 * @return #GNUNET_OK on success
140 credential_string_to_value(void *cls, uint32_t type, const char *s,
141 void **data, size_t *data_size)
144 return GNUNET_SYSERR;
147 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
148 struct GNUNET_CREDENTIAL_DelegationRecord *sets;
149 char attr_str[253 + 1];
150 char subject_pkey[52 + 1];
155 size_t tmp_data_size;
158 tmp_str = GNUNET_strdup(s);
159 token = strtok(tmp_str, ",");
162 *data_size = sizeof(struct GNUNET_CREDENTIAL_DelegationRecord);
163 while (NULL != token)
165 matches = sscanf(token, "%s %s", subject_pkey, attr_str);
168 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
169 _("Unable to parse ATTR record string `%s'\n"), s);
170 GNUNET_free(tmp_str);
171 return GNUNET_SYSERR;
175 tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet);
177 else if (2 == matches)
179 tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet) +
180 strlen(attr_str) + 1;
183 token = strtok(NULL, ",");
185 GNUNET_free(tmp_str);
186 tmp_str = GNUNET_strdup(s);
187 token = strtok(tmp_str, ",");
190 GNUNET_free(tmp_str);
191 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
192 return GNUNET_SYSERR;
194 struct GNUNET_CREDENTIAL_DelegationSet set[entries];
195 memset(set, 0, sizeof(struct GNUNET_CREDENTIAL_DelegationSet) * entries);
196 for (i = 0; i < entries; i++)
198 matches = sscanf(token, "%s %s", subject_pkey, attr_str);
199 GNUNET_CRYPTO_ecdsa_public_key_from_string(
200 subject_pkey, strlen(subject_pkey), &set[i].subject_key);
203 set[i].subject_attribute_len = strlen(attr_str) + 1;
204 set[i].subject_attribute = GNUNET_strdup(attr_str);
206 token = strtok(NULL, ",");
208 tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size(entries, set);
210 if (-1 == tmp_data_size)
212 GNUNET_free(tmp_str);
213 return GNUNET_SYSERR;
215 *data_size += tmp_data_size;
216 *data = sets = GNUNET_malloc(*data_size);
217 GNUNET_CREDENTIAL_delegation_set_serialize(entries, set, tmp_data_size,
219 for (i = 0; i < entries; i++)
221 if (0 != set[i].subject_attribute_len)
222 GNUNET_free((char *)set[i].subject_attribute);
224 sets->set_count = htonl(entries);
225 sets->data_size = GNUNET_htonll(tmp_data_size);
227 GNUNET_free(tmp_str);
231 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
232 struct GNUNET_CREDENTIAL_Credential *cred;
233 cred = GNUNET_CREDENTIAL_credential_from_string(s);
235 *data_size = GNUNET_CREDENTIAL_credential_serialize(cred, (char **)data);
239 case GNUNET_GNSRECORD_TYPE_POLICY: {
240 *data_size = strlen(s);
241 *data = GNUNET_strdup(s);
246 return GNUNET_SYSERR;
252 * Mapping of record type numbers to human-readable
258 } name_map[] = { { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
259 { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
260 { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY },
261 { NULL, UINT32_MAX } };
265 * Convert a type name (i.e. "AAAA") to the corresponding number.
267 * @param cls closure, unused
268 * @param gns_typename name to convert
269 * @return corresponding number, UINT32_MAX on error
272 credential_typename_to_number(void *cls, const char *gns_typename)
277 while ((name_map[i].name != NULL) &&
278 (0 != strcasecmp(gns_typename, name_map[i].name)))
280 return name_map[i].number;
285 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
287 * @param cls closure, unused
288 * @param type number of a type to convert
289 * @return corresponding typestring, NULL on error
292 credential_number_to_typename(void *cls, uint32_t type)
297 while ((name_map[i].name != NULL) && (type != name_map[i].number))
299 return name_map[i].name;
304 * Entry point for the plugin.
307 * @return the exported block API
310 libgnunet_plugin_gnsrecord_credential_init(void *cls)
312 struct GNUNET_GNSRECORD_PluginFunctions *api;
314 api = GNUNET_new(struct GNUNET_GNSRECORD_PluginFunctions);
315 api->value_to_string = &credential_value_to_string;
316 api->string_to_value = &credential_string_to_value;
317 api->typename_to_number = &credential_typename_to_number;
318 api->number_to_typename = &credential_number_to_typename;
324 * Exit point from the plugin.
326 * @param cls the return value from #libgnunet_plugin_block_test_init
330 libgnunet_plugin_gnsrecord_credential_done(void *cls)
332 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
338 /* end of plugin_gnsrecord_credential.c */