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++) {
71 GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key);
72 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d len attr\n",
73 set[i].subject_attribute_len);
74 if (0 == set[i].subject_attribute_len) {
76 GNUNET_asprintf (&attr_str, "%s", subject_pkey);
78 GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey);
79 GNUNET_free (attr_str);
84 GNUNET_asprintf (&attr_str, "%s %s", subject_pkey,
85 set[i].subject_attribute);
87 GNUNET_asprintf (&tmp_str, "%s,%s %s", attr_str, subject_pkey,
88 set[i].subject_attribute);
89 GNUNET_free (attr_str);
93 GNUNET_free (subject_pkey);
97 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
98 struct GNUNET_CREDENTIAL_Credential *cred;
101 cred = GNUNET_CREDENTIAL_credential_deserialize (data, data_size);
102 cred_str = GNUNET_CREDENTIAL_credential_to_string (cred);
106 case GNUNET_GNSRECORD_TYPE_POLICY: {
107 return GNUNET_strndup (data, data_size);
116 * Convert human-readable version of a 'value' of a record to the binary
119 * @param cls closure, unused
120 * @param type type of the record
121 * @param s human-readable string
122 * @param data set to value in binary encoding (will be allocated)
123 * @param data_size set to number of bytes in @a data
124 * @return #GNUNET_OK on success
127 credential_string_to_value (void *cls, uint32_t type, const char *s,
128 void **data, size_t *data_size)
131 return GNUNET_SYSERR;
133 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
134 struct GNUNET_CREDENTIAL_DelegationRecord *sets;
135 char attr_str[253 + 1];
136 char subject_pkey[52 + 1];
141 size_t tmp_data_size;
144 tmp_str = GNUNET_strdup (s);
145 token = strtok (tmp_str, ",");
148 *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord);
149 while (NULL != token) {
150 matches = SSCANF (token, "%s %s", subject_pkey, attr_str);
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 _ ("Unable to parse ATTR record string `%s'\n"), s);
154 GNUNET_free (tmp_str);
155 return GNUNET_SYSERR;
158 tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet);
159 } else if (2 == matches) {
160 tmp_data_size += sizeof (struct GNUNET_CREDENTIAL_DelegationRecordSet) +
161 strlen (attr_str) + 1;
164 token = strtok (NULL, ",");
166 GNUNET_free (tmp_str);
167 tmp_str = GNUNET_strdup (s);
168 token = strtok (tmp_str, ",");
170 GNUNET_free (tmp_str);
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
172 return GNUNET_SYSERR;
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);
181 set[i].subject_attribute_len = strlen (attr_str) + 1;
182 set[i].subject_attribute = GNUNET_strdup (attr_str);
184 token = strtok (NULL, ",");
186 tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries, set);
188 if (-1 == tmp_data_size) {
189 GNUNET_free (tmp_str);
190 return GNUNET_SYSERR;
192 *data_size += tmp_data_size;
193 *data = sets = GNUNET_malloc (*data_size);
194 GNUNET_CREDENTIAL_delegation_set_serialize (entries, set, tmp_data_size,
196 for (i = 0; i < entries; i++) {
197 if (0 != set[i].subject_attribute_len)
198 GNUNET_free ((char *)set[i].subject_attribute);
200 sets->set_count = htonl (entries);
201 sets->data_size = GNUNET_htonll (tmp_data_size);
203 GNUNET_free (tmp_str);
206 case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
207 struct GNUNET_CREDENTIAL_Credential *cred;
208 cred = GNUNET_CREDENTIAL_credential_from_string (s);
210 *data_size = GNUNET_CREDENTIAL_credential_serialize (cred, (char **)data);
213 case GNUNET_GNSRECORD_TYPE_POLICY: {
214 *data_size = strlen (s);
215 *data = GNUNET_strdup (s);
219 return GNUNET_SYSERR;
225 * Mapping of record type numbers to human-readable
232 } name_map[] = {{"CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL},
233 {"ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE},
234 {"POLICY", GNUNET_GNSRECORD_TYPE_POLICY},
239 * Convert a type name (i.e. "AAAA") to the corresponding number.
241 * @param cls closure, unused
242 * @param gns_typename name to convert
243 * @return corresponding number, UINT32_MAX on error
246 credential_typename_to_number (void *cls, const char *gns_typename)
251 while ((name_map[i].name != NULL) &&
252 (0 != strcasecmp (gns_typename, name_map[i].name)))
254 return name_map[i].number;
259 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
261 * @param cls closure, unused
262 * @param type number of a type to convert
263 * @return corresponding typestring, NULL on error
266 credential_number_to_typename (void *cls, uint32_t type)
271 while ((name_map[i].name != NULL) && (type != name_map[i].number))
273 return name_map[i].name;
278 * Entry point for the plugin.
281 * @return the exported block API
284 libgnunet_plugin_gnsrecord_credential_init (void *cls)
286 struct GNUNET_GNSRECORD_PluginFunctions *api;
288 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
289 api->value_to_string = &credential_value_to_string;
290 api->string_to_value = &credential_string_to_value;
291 api->typename_to_number = &credential_typename_to_number;
292 api->number_to_typename = &credential_number_to_typename;
298 * Exit point from the plugin.
300 * @param cls the return value from #libgnunet_plugin_block_test_init
304 libgnunet_plugin_gnsrecord_credential_done (void *cls)
306 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
312 /* end of plugin_gnsrecord_credential.c */