Merge branch 'license/spdx'
[oweals/gnunet.git] / src / reclaim / plugin_gnsrecord_reclaim.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014 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 reclaim/plugin_gnsrecord_reclaim.c
23  * @brief gnsrecord plugin to provide the API for identity records
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_gnsrecord_lib.h"
29 #include "gnunet_gnsrecord_plugin.h"
30
31
32 /**
33  * Convert the 'value' of a record to a string.
34  *
35  * @param cls closure, unused
36  * @param type type of the record
37  * @param data value in binary encoding
38  * @param data_size number of bytes in @a data
39  * @return NULL on error, otherwise human-readable representation of the value
40  */
41 static char *
42 value_to_string (void *cls,
43                  uint32_t type,
44                  const void *data,
45                  size_t data_size)
46 {
47   const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
48   const struct GNUNET_CRYPTO_EcdsaPublicKey *audience_pubkey;
49   const char *scopes;
50   char *ecdhe_str;
51   char *aud_str;
52   char *result;
53
54   switch (type)
55   {
56     case GNUNET_GNSRECORD_TYPE_ID_ATTR:
57       return GNUNET_STRINGS_data_to_string_alloc (data, data_size);
58     case GNUNET_GNSRECORD_TYPE_ID_TOKEN: //DEPRECATED
59     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT:
60     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT:
61       return GNUNET_strndup (data, data_size);
62     case GNUNET_GNSRECORD_TYPE_ABE_KEY:
63     case GNUNET_GNSRECORD_TYPE_ABE_MASTER:
64       return GNUNET_STRINGS_data_to_string_alloc (data, data_size); 
65     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA: //DEPRECATED
66         ecdhe_privkey = data;
67         audience_pubkey = data+sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
68         scopes =  (char*) audience_pubkey+(sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
69         ecdhe_str = GNUNET_STRINGS_data_to_string_alloc (ecdhe_privkey,
70                                                         sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
71         aud_str = GNUNET_STRINGS_data_to_string_alloc (audience_pubkey,
72                                                        sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
73         GNUNET_asprintf (&result,
74                          "%s;%s;%s",
75                          ecdhe_str, aud_str, scopes);
76         GNUNET_free (aud_str);
77         GNUNET_free (ecdhe_str);
78         return result;
79
80     default:
81       return NULL;
82   }
83 }
84
85
86 /**
87  * Convert human-readable version of a 'value' of a record to the binary
88  * representation.
89  *
90  * @param cls closure, unused
91  * @param type type of the record
92  * @param s human-readable string
93  * @param data set to value in binary encoding (will be allocated)
94  * @param data_size set to number of bytes in @a data
95  * @return #GNUNET_OK on success
96  */
97 static int
98 string_to_value (void *cls,
99                  uint32_t type,
100                  const char *s,
101                  void **data,
102                  size_t *data_size)
103 {
104   char* ecdhe_str;
105   char* aud_keystr;
106   char* write_ptr;
107   char* tmp_tok;
108   char* str;
109
110   if (NULL == s)
111     return GNUNET_SYSERR;
112   switch (type)
113   {
114     case GNUNET_GNSRECORD_TYPE_ID_ATTR:
115       return GNUNET_STRINGS_string_to_data (s,
116                                             strlen (s),
117                                             *data,
118                                             *data_size);
119     case GNUNET_GNSRECORD_TYPE_ID_TOKEN:
120     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT:
121     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT:
122       *data = GNUNET_strdup (s);
123       *data_size = strlen (s);
124       return GNUNET_OK;
125     case GNUNET_GNSRECORD_TYPE_ABE_KEY:
126     case GNUNET_GNSRECORD_TYPE_ABE_MASTER:
127       return GNUNET_STRINGS_string_to_data (s,
128                                             strlen (s),
129                                             *data,
130                                             *data_size);
131     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA:
132       tmp_tok = GNUNET_strdup (s);
133       ecdhe_str = strtok (tmp_tok, ";");
134       if (NULL == ecdhe_str)
135       {
136         GNUNET_free (tmp_tok);
137         return GNUNET_SYSERR;
138       }
139       aud_keystr = strtok (NULL, ";");
140       if (NULL == aud_keystr)
141       {
142         GNUNET_free (tmp_tok);
143         return GNUNET_SYSERR;
144       }
145       str = strtok (NULL, ";");
146       if (NULL == str)
147       {
148         GNUNET_free (tmp_tok);
149         return GNUNET_SYSERR;
150       }
151       *data_size = strlen (str) + 1
152         +sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
153         +sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
154       *data = GNUNET_malloc (*data_size);
155
156       write_ptr = *data;
157       GNUNET_STRINGS_string_to_data (ecdhe_str,
158                                      strlen (ecdhe_str),
159                                      write_ptr,
160                                      sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
161       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
162       GNUNET_STRINGS_string_to_data (aud_keystr,
163                                      strlen (aud_keystr),
164                                      write_ptr,
165                                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
166       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
167       GNUNET_memcpy (write_ptr, str, strlen (str) + 1); //with 0-Terminator
168       GNUNET_free (tmp_tok);
169       return GNUNET_OK;
170
171     default:
172       return GNUNET_SYSERR;
173   }
174 }
175
176
177 /**
178  * Mapping of record type numbers to human-readable
179  * record type names.
180  */
181 static struct {
182   const char *name;
183   uint32_t number;
184 } name_map[] = {
185   { "ID_ATTR", GNUNET_GNSRECORD_TYPE_ID_ATTR },
186   { "ID_TOKEN", GNUNET_GNSRECORD_TYPE_ID_TOKEN },
187   { "ABE_KEY", GNUNET_GNSRECORD_TYPE_ABE_KEY },
188   { "ABE_MASTER", GNUNET_GNSRECORD_TYPE_ABE_MASTER },
189   { "ID_TOKEN_METADATA", GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA },
190   { "RECLAIM_OIDC_CLIENT", GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT },
191   { "RECLAIM_OIDC_REDIRECT", GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT },
192   { NULL, UINT32_MAX }
193 };
194
195
196 /**
197  * Convert a type name (i.e. "AAAA") to the corresponding number.
198  *
199  * @param cls closure, unused
200  * @param dns_typename name to convert
201  * @return corresponding number, UINT32_MAX on error
202  */
203 static uint32_t
204 typename_to_number (void *cls,
205                     const char *dns_typename)
206 {
207   unsigned int i;
208
209   i=0;
210   while ( (NULL != name_map[i].name) &&
211           (0 != strcasecmp (dns_typename, name_map[i].name)) )
212     i++;
213   return name_map[i].number;
214 }
215
216
217 /**
218  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
219  *
220  * @param cls closure, unused
221  * @param type number of a type to convert
222  * @return corresponding typestring, NULL on error
223  */
224 static const char *
225 number_to_typename (void *cls,
226                     uint32_t type)
227 {
228   unsigned int i;
229
230   i=0;
231   while ( (NULL != name_map[i].name) &&
232           (type != name_map[i].number) )
233     i++;
234   return name_map[i].name;
235 }
236
237
238 /**
239  * Entry point for the plugin.
240  *
241  * @param cls NULL
242  * @return the exported block API
243  */
244 void *
245 libgnunet_plugin_gnsrecord_reclaim_init (void *cls)
246 {
247   struct GNUNET_GNSRECORD_PluginFunctions *api;
248
249   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
250   api->value_to_string = &value_to_string;
251   api->string_to_value = &string_to_value;
252   api->typename_to_number = &typename_to_number;
253   api->number_to_typename = &number_to_typename;
254   return api;
255 }
256
257
258 /**
259  * Exit point from the plugin.
260  *
261  * @param cls the return value from #libgnunet_plugin_block_test_init
262  * @return NULL
263  */
264 void *
265 libgnunet_plugin_gnsrecord_reclaim_done (void *cls)
266 {
267   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
268
269   GNUNET_free (api);
270   return NULL;
271 }
272
273 /* end of plugin_gnsrecord_dns.c */