global reindent, now with uncrustify hook enabled
[oweals/gnunet.git] / src / credential / plugin_gnsrecord_credential.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file credential/plugin_gnsrecord_credential.c
23  * @brief gnsrecord plugin to provide the API for CREDENTIAL records
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27
28 #include "gnunet_util_lib.h"
29
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"
36 /**
37  * Convert the 'value' of a record to a string.
38  *
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
44  */
45 static char *
46 credential_value_to_string (void *cls, uint32_t type, const void *data,
47                             size_t data_size)
48 {
49   const char *cdata;
50
51   switch (type)
52   {
53   case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
54       struct GNUNET_CREDENTIAL_DelegationRecord sets;
55       char *attr_str;
56       char *subject_pkey;
57       char *tmp_str;
58       int i;
59       if (data_size < sizeof(struct GNUNET_CREDENTIAL_DelegationRecord))
60         return NULL; /* malformed */
61       GNUNET_memcpy (&sets, data, sizeof(sets));
62       cdata = data;
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))
67         return NULL;
68
69       for (i = 0; i < ntohl (sets.set_count); i++)
70       {
71         subject_pkey =
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)
76         {
77           if (0 == i)
78           {
79             GNUNET_asprintf (&attr_str, "%s", subject_pkey);
80           }
81           else
82           {
83             GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey);
84             GNUNET_free (attr_str);
85             attr_str = tmp_str;
86           }
87         }
88         else
89         {
90           if (0 == i)
91           {
92             GNUNET_asprintf (&attr_str, "%s %s", subject_pkey,
93                              set[i].subject_attribute);
94           }
95           else
96           {
97             GNUNET_asprintf (&tmp_str, "%s,%s %s", attr_str, subject_pkey,
98                              set[i].subject_attribute);
99             GNUNET_free (attr_str);
100             attr_str = tmp_str;
101           }
102         }
103         GNUNET_free (subject_pkey);
104       }
105       return attr_str;
106     }
107
108   case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
109       struct GNUNET_CREDENTIAL_Credential *cred;
110       char *cred_str;
111
112       cred = GNUNET_CREDENTIAL_credential_deserialize (data, data_size);
113       cred_str = GNUNET_CREDENTIAL_credential_to_string (cred);
114       GNUNET_free (cred);
115       return cred_str;
116     }
117
118   case GNUNET_GNSRECORD_TYPE_POLICY: {
119       return GNUNET_strndup (data, data_size);
120     }
121
122   default:
123     return NULL;
124   }
125 }
126
127
128 /**
129  * Convert human-readable version of a 'value' of a record to the binary
130  * representation.
131  *
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
138  */
139 static int
140 credential_string_to_value (void *cls, uint32_t type, const char *s,
141                             void **data, size_t *data_size)
142 {
143   if (NULL == s)
144     return GNUNET_SYSERR;
145   switch (type)
146   {
147   case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
148       struct GNUNET_CREDENTIAL_DelegationRecord *sets;
149       char attr_str[253 + 1];
150       char subject_pkey[52 + 1];
151       char *token;
152       char *tmp_str;
153       int matches = 0;
154       int entries;
155       size_t tmp_data_size;
156       int i;
157
158       tmp_str = GNUNET_strdup (s);
159       token = strtok (tmp_str, ",");
160       entries = 0;
161       tmp_data_size = 0;
162       *data_size = sizeof(struct GNUNET_CREDENTIAL_DelegationRecord);
163       while (NULL != token)
164       {
165         matches = sscanf (token, "%s %s", subject_pkey, attr_str);
166         if (0 == matches)
167         {
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;
172         }
173         if (1 == matches)
174         {
175           tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet);
176         }
177         else if (2 == matches)
178         {
179           tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet)
180                            + strlen (attr_str) + 1;
181         }
182         entries++;
183         token = strtok (NULL, ",");
184       }
185       GNUNET_free (tmp_str);
186       tmp_str = GNUNET_strdup (s);
187       token = strtok (tmp_str, ",");
188       if (NULL == token)
189       {
190         GNUNET_free (tmp_str);
191         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
192         return GNUNET_SYSERR;
193       }
194       struct GNUNET_CREDENTIAL_DelegationSet set[entries];
195       memset (set, 0, sizeof(struct GNUNET_CREDENTIAL_DelegationSet) * entries);
196       for (i = 0; i < entries; i++)
197       {
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);
201         if (2 == matches)
202         {
203           set[i].subject_attribute_len = strlen (attr_str) + 1;
204           set[i].subject_attribute = GNUNET_strdup (attr_str);
205         }
206         token = strtok (NULL, ",");
207       }
208       tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries, set);
209
210       if (-1 == tmp_data_size)
211       {
212         GNUNET_free (tmp_str);
213         return GNUNET_SYSERR;
214       }
215       *data_size += tmp_data_size;
216       *data = sets = GNUNET_malloc (*data_size);
217       GNUNET_CREDENTIAL_delegation_set_serialize (entries, set, tmp_data_size,
218                                                   (char *) &sets[1]);
219       for (i = 0; i < entries; i++)
220       {
221         if (0 != set[i].subject_attribute_len)
222           GNUNET_free ((char *) set[i].subject_attribute);
223       }
224       sets->set_count = htonl (entries);
225       sets->data_size = GNUNET_htonll (tmp_data_size);
226
227       GNUNET_free (tmp_str);
228       return GNUNET_OK;
229     }
230
231   case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
232       struct GNUNET_CREDENTIAL_Credential *cred;
233       cred = GNUNET_CREDENTIAL_credential_from_string (s);
234
235       *data_size = GNUNET_CREDENTIAL_credential_serialize (cred,
236                                                            (char **) data);
237       return GNUNET_OK;
238     }
239
240   case GNUNET_GNSRECORD_TYPE_POLICY: {
241       *data_size = strlen (s);
242       *data = GNUNET_strdup (s);
243       return GNUNET_OK;
244     }
245
246   default:
247     return GNUNET_SYSERR;
248   }
249 }
250
251
252 /**
253  * Mapping of record type numbers to human-readable
254  * record type names.
255  */
256 static struct
257 {
258   const char *name;
259   uint32_t number;
260 } name_map[] = { { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
261                  { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
262                  { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY },
263                  { NULL, UINT32_MAX } };
264
265
266 /**
267  * Convert a type name (i.e. "AAAA") to the corresponding number.
268  *
269  * @param cls closure, unused
270  * @param gns_typename name to convert
271  * @return corresponding number, UINT32_MAX on error
272  */
273 static uint32_t
274 credential_typename_to_number (void *cls, const char *gns_typename)
275 {
276   unsigned int i;
277
278   i = 0;
279   while ((name_map[i].name != NULL) &&
280          (0 != strcasecmp (gns_typename, name_map[i].name)))
281     i++;
282   return name_map[i].number;
283 }
284
285
286 /**
287  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
288  *
289  * @param cls closure, unused
290  * @param type number of a type to convert
291  * @return corresponding typestring, NULL on error
292  */
293 static const char *
294 credential_number_to_typename (void *cls, uint32_t type)
295 {
296   unsigned int i;
297
298   i = 0;
299   while ((name_map[i].name != NULL) && (type != name_map[i].number))
300     i++;
301   return name_map[i].name;
302 }
303
304
305 /**
306  * Entry point for the plugin.
307  *
308  * @param cls NULL
309  * @return the exported block API
310  */
311 void *
312 libgnunet_plugin_gnsrecord_credential_init (void *cls)
313 {
314   struct GNUNET_GNSRECORD_PluginFunctions *api;
315
316   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
317   api->value_to_string = &credential_value_to_string;
318   api->string_to_value = &credential_string_to_value;
319   api->typename_to_number = &credential_typename_to_number;
320   api->number_to_typename = &credential_number_to_typename;
321   return api;
322 }
323
324
325 /**
326  * Exit point from the plugin.
327  *
328  * @param cls the return value from #libgnunet_plugin_block_test_init
329  * @return NULL
330  */
331 void *
332 libgnunet_plugin_gnsrecord_credential_done (void *cls)
333 {
334   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
335
336   GNUNET_free (api);
337   return NULL;
338 }
339
340 /* end of plugin_gnsrecord_credential.c */