CREDENTIAL: Fix build with new APIs
[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
50   const char *cdata;
51
52   switch (type) {
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       subject_pkey =
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) {
75         if (0 == i) {
76           GNUNET_asprintf (&attr_str, "%s", subject_pkey);
77         } else {
78           GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey);
79           GNUNET_free (attr_str);
80           attr_str = tmp_str;
81         }
82       } else {
83         if (0 == i) {
84           GNUNET_asprintf (&attr_str, "%s %s", subject_pkey,
85                            set[i].subject_attribute);
86         } else {
87           GNUNET_asprintf (&tmp_str, "%s,%s %s", attr_str, subject_pkey,
88                            set[i].subject_attribute);
89           GNUNET_free (attr_str);
90           attr_str = tmp_str;
91         }
92       }
93       GNUNET_free (subject_pkey);
94     }
95     return attr_str;
96   }
97   case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
98     struct GNUNET_CREDENTIAL_Credential *cred;
99     char *cred_str;
100
101     cred = GNUNET_CREDENTIAL_credential_deserialize (data, data_size);
102     cred_str = GNUNET_CREDENTIAL_credential_to_string (cred);
103     GNUNET_free (cred);
104     return cred_str;
105   }
106   case GNUNET_GNSRECORD_TYPE_POLICY: {
107     return GNUNET_strndup (data, data_size);
108   }
109   default:
110     return NULL;
111   }
112 }
113
114
115 /**
116  * Convert human-readable version of a 'value' of a record to the binary
117  * representation.
118  *
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
125  */
126 static int
127 credential_string_to_value (void *cls, uint32_t type, const char *s,
128                             void **data, size_t *data_size)
129 {
130   if (NULL == s)
131     return GNUNET_SYSERR;
132   switch (type) {
133   case GNUNET_GNSRECORD_TYPE_ATTRIBUTE: {
134     struct GNUNET_CREDENTIAL_DelegationRecord *sets;
135     char attr_str[253 + 1];
136     char subject_pkey[52 + 1];
137     char *token;
138     char *tmp_str;
139     int matches = 0;
140     int entries;
141     size_t tmp_data_size;
142     int i;
143
144     tmp_str = GNUNET_strdup (s);
145     token = strtok (tmp_str, ",");
146     entries = 0;
147     tmp_data_size = 0;
148     *data_size = sizeof (struct GNUNET_CREDENTIAL_DelegationRecord);
149     while (NULL != token) {
150       matches = SSCANF (token, "%s %s", subject_pkey, attr_str);
151       if (0 == matches) {
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;
156       }
157       if (1 == matches) {
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;
162       }
163       entries++;
164       token = strtok (NULL, ",");
165     }
166     GNUNET_free (tmp_str);
167     tmp_str = GNUNET_strdup (s);
168     token = strtok (tmp_str, ",");
169     if (NULL == token) {
170       GNUNET_free (tmp_str);
171       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
172       return GNUNET_SYSERR;
173     }
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);
180       if (2 == matches) {
181         set[i].subject_attribute_len = strlen (attr_str) + 1;
182         set[i].subject_attribute = GNUNET_strdup (attr_str);
183       }
184       token = strtok (NULL, ",");
185     }
186     tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size (entries, set);
187
188     if (-1 == tmp_data_size) {
189       GNUNET_free (tmp_str);
190       return GNUNET_SYSERR;
191     }
192     *data_size += tmp_data_size;
193     *data = sets = GNUNET_malloc (*data_size);
194     GNUNET_CREDENTIAL_delegation_set_serialize (entries, set, tmp_data_size,
195                                                 (char *)&sets[1]);
196     for (i = 0; i < entries; i++) {
197       if (0 != set[i].subject_attribute_len)
198         GNUNET_free ((char *)set[i].subject_attribute);
199     }
200     sets->set_count = htonl (entries);
201     sets->data_size = GNUNET_htonll (tmp_data_size);
202
203     GNUNET_free (tmp_str);
204     return GNUNET_OK;
205   }
206   case GNUNET_GNSRECORD_TYPE_CREDENTIAL: {
207     struct GNUNET_CREDENTIAL_Credential *cred;
208     cred = GNUNET_CREDENTIAL_credential_from_string (s);
209
210     *data_size = GNUNET_CREDENTIAL_credential_serialize (cred, (char **)data);
211     return GNUNET_OK;
212   }
213   case GNUNET_GNSRECORD_TYPE_POLICY: {
214     *data_size = strlen (s);
215     *data = GNUNET_strdup (s);
216     return GNUNET_OK;
217   }
218   default:
219     return GNUNET_SYSERR;
220   }
221 }
222
223
224 /**
225  * Mapping of record type numbers to human-readable
226  * record type names.
227  */
228 static struct
229 {
230   const char *name;
231   uint32_t number;
232 } name_map[] = {{"CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL},
233                 {"ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE},
234                 {"POLICY", GNUNET_GNSRECORD_TYPE_POLICY},
235                 {NULL, UINT32_MAX}};
236
237
238 /**
239  * Convert a type name (i.e. "AAAA") to the corresponding number.
240  *
241  * @param cls closure, unused
242  * @param gns_typename name to convert
243  * @return corresponding number, UINT32_MAX on error
244  */
245 static uint32_t
246 credential_typename_to_number (void *cls, const char *gns_typename)
247 {
248   unsigned int i;
249
250   i = 0;
251   while ((name_map[i].name != NULL) &&
252          (0 != strcasecmp (gns_typename, name_map[i].name)))
253     i++;
254   return name_map[i].number;
255 }
256
257
258 /**
259  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
260  *
261  * @param cls closure, unused
262  * @param type number of a type to convert
263  * @return corresponding typestring, NULL on error
264  */
265 static const char *
266 credential_number_to_typename (void *cls, uint32_t type)
267 {
268   unsigned int i;
269
270   i = 0;
271   while ((name_map[i].name != NULL) && (type != name_map[i].number))
272     i++;
273   return name_map[i].name;
274 }
275
276
277 /**
278  * Entry point for the plugin.
279  *
280  * @param cls NULL
281  * @return the exported block API
282  */
283 void *
284 libgnunet_plugin_gnsrecord_credential_init (void *cls)
285 {
286   struct GNUNET_GNSRECORD_PluginFunctions *api;
287
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;
293   return api;
294 }
295
296
297 /**
298  * Exit point from the plugin.
299  *
300  * @param cls the return value from #libgnunet_plugin_block_test_init
301  * @return NULL
302  */
303 void *
304 libgnunet_plugin_gnsrecord_credential_done (void *cls)
305 {
306   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
307
308   GNUNET_free (api);
309   return NULL;
310 }
311
312 /* end of plugin_gnsrecord_credential.c */