-minor fixed, start consume
[oweals/gnunet.git] / src / identity-provider / plugin_gnsrecord_identity_provider.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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file identity/plugin_gnsrecord_identity.c
23  * @brief gnsrecord plugin to provide the API for identity records
24  * @author Christian Grothoff
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     case GNUNET_GNSRECORD_TYPE_ID_TOKEN:
58       return GNUNET_strndup (data, data_size);
59     case GNUNET_GNSRECORD_TYPE_ABE_KEY:
60     case GNUNET_GNSRECORD_TYPE_ABE_MASTER:
61       return GNUNET_STRINGS_data_to_string_alloc (data, data_size); 
62     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA:
63         ecdhe_privkey = data;
64         audience_pubkey = data+sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
65         scopes =  (char*) audience_pubkey+(sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
66         ecdhe_str = GNUNET_STRINGS_data_to_string_alloc (ecdhe_privkey,
67                                                         sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
68         aud_str = GNUNET_STRINGS_data_to_string_alloc (audience_pubkey,
69                                                        sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
70         GNUNET_asprintf (&result,
71                          "%s;%s;%s",
72                          ecdhe_str, aud_str, scopes);
73         GNUNET_free (aud_str);
74         GNUNET_free (ecdhe_str);
75         return result;
76
77     default:
78       return NULL;
79   }
80 }
81
82
83 /**
84  * Convert human-readable version of a 'value' of a record to the binary
85  * representation.
86  *
87  * @param cls closure, unused
88  * @param type type of the record
89  * @param s human-readable string
90  * @param data set to value in binary encoding (will be allocated)
91  * @param data_size set to number of bytes in @a data
92  * @return #GNUNET_OK on success
93  */
94 static int
95 string_to_value (void *cls,
96                  uint32_t type,
97                  const char *s,
98                  void **data,
99                  size_t *data_size)
100 {
101   char* ecdhe_str;
102   char* aud_keystr;
103   char* write_ptr;
104   char* tmp_tok;
105   char* str;
106
107   if (NULL == s)
108     return GNUNET_SYSERR;
109   switch (type)
110   {
111     case GNUNET_GNSRECORD_TYPE_ID_ATTR:
112     case GNUNET_GNSRECORD_TYPE_ID_TOKEN:
113       *data = GNUNET_strdup (s);
114       *data_size = strlen (s);
115       return GNUNET_OK;
116     case GNUNET_GNSRECORD_TYPE_ABE_KEY:
117     case GNUNET_GNSRECORD_TYPE_ABE_MASTER:
118       return GNUNET_STRINGS_string_to_data (s,
119                                             strlen (s),
120                                             *data,
121                                             *data_size);
122     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA:
123       tmp_tok = GNUNET_strdup (s);
124       ecdhe_str = strtok (tmp_tok, ";");
125       if (NULL == ecdhe_str)
126       {
127         GNUNET_free (tmp_tok);
128         return GNUNET_SYSERR;
129       }
130       aud_keystr = strtok (NULL, ";");
131       if (NULL == aud_keystr)
132       {
133         GNUNET_free (tmp_tok);
134         return GNUNET_SYSERR;
135       }
136       str = strtok (NULL, ";");
137       if (NULL == str)
138       {
139         GNUNET_free (tmp_tok);
140         return GNUNET_SYSERR;
141       }
142       *data_size = strlen (str) + 1
143         +sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
144         +sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
145       *data = GNUNET_malloc (*data_size);
146
147       write_ptr = *data;
148       GNUNET_STRINGS_string_to_data (ecdhe_str,
149                                      strlen (ecdhe_str),
150                                      write_ptr,
151                                      sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
152       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
153       GNUNET_STRINGS_string_to_data (aud_keystr,
154                                      strlen (aud_keystr),
155                                      write_ptr,
156                                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
157       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
158       GNUNET_memcpy (write_ptr, str, strlen (str) + 1); //with 0-Terminator
159       GNUNET_free (tmp_tok);
160       return GNUNET_OK;
161
162     default:
163       return GNUNET_SYSERR;
164   }
165 }
166
167
168 /**
169  * Mapping of record type numbers to human-readable
170  * record type names.
171  */
172 static struct {
173   const char *name;
174   uint32_t number;
175 } name_map[] = {
176   { "ID_ATTR", GNUNET_GNSRECORD_TYPE_ID_ATTR },
177   { "ID_TOKEN", GNUNET_GNSRECORD_TYPE_ID_TOKEN },
178   { "ABE_KEY", GNUNET_GNSRECORD_TYPE_ABE_KEY },
179   { "ABE_MASTER", GNUNET_GNSRECORD_TYPE_ABE_MASTER },
180   { "ID_TOKEN_METADATA", GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA },
181   { NULL, UINT32_MAX }
182 };
183
184
185 /**
186  * Convert a type name (i.e. "AAAA") to the corresponding number.
187  *
188  * @param cls closure, unused
189  * @param dns_typename name to convert
190  * @return corresponding number, UINT32_MAX on error
191  */
192 static uint32_t
193 typename_to_number (void *cls,
194                     const char *dns_typename)
195 {
196   unsigned int i;
197
198   i=0;
199   while ( (NULL != name_map[i].name) &&
200           (0 != strcasecmp (dns_typename, name_map[i].name)) )
201     i++;
202   return name_map[i].number;
203 }
204
205
206 /**
207  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
208  *
209  * @param cls closure, unused
210  * @param type number of a type to convert
211  * @return corresponding typestring, NULL on error
212  */
213 static const char *
214 number_to_typename (void *cls,
215                     uint32_t type)
216 {
217   unsigned int i;
218
219   i=0;
220   while ( (NULL != name_map[i].name) &&
221           (type != name_map[i].number) )
222     i++;
223   return name_map[i].name;
224 }
225
226
227 /**
228  * Entry point for the plugin.
229  *
230  * @param cls NULL
231  * @return the exported block API
232  */
233 void *
234 libgnunet_plugin_gnsrecord_identity_provider_init (void *cls)
235 {
236   struct GNUNET_GNSRECORD_PluginFunctions *api;
237
238   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
239   api->value_to_string = &value_to_string;
240   api->string_to_value = &string_to_value;
241   api->typename_to_number = &typename_to_number;
242   api->number_to_typename = &number_to_typename;
243   return api;
244 }
245
246
247 /**
248  * Exit point from the plugin.
249  *
250  * @param cls the return value from #libgnunet_plugin_block_test_init
251  * @return NULL
252  */
253 void *
254 libgnunet_plugin_gnsrecord_identity_provider_done (void *cls)
255 {
256   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
257
258   GNUNET_free (api);
259   return NULL;
260 }
261
262 /* end of plugin_gnsrecord_dns.c */