typo in src/identity/gnunet-identity.c
[oweals/gnunet.git] / src / identity / plugin_gnsrecord_identity.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_ID_TOKEN_METADATA:
60         ecdhe_privkey = data;
61         audience_pubkey = data+sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
62         scopes =  (char*) audience_pubkey+(sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
63         ecdhe_str = GNUNET_STRINGS_data_to_string_alloc (ecdhe_privkey,
64                                                         sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
65         aud_str = GNUNET_STRINGS_data_to_string_alloc (audience_pubkey,
66                                                        sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
67         GNUNET_asprintf (&result,
68                          "%s;%s;%s",
69                          ecdhe_str, aud_str, scopes);
70         GNUNET_free (aud_str);
71         GNUNET_free (ecdhe_str);
72         return result;
73
74     default:
75       return NULL;
76   }
77 }
78
79
80 /**
81  * Convert human-readable version of a 'value' of a record to the binary
82  * representation.
83  *
84  * @param cls closure, unused
85  * @param type type of the record
86  * @param s human-readable string
87  * @param data set to value in binary encoding (will be allocated)
88  * @param data_size set to number of bytes in @a data
89  * @return #GNUNET_OK on success
90  */
91 static int
92 string_to_value (void *cls,
93                  uint32_t type,
94                  const char *s,
95                  void **data,
96                  size_t *data_size)
97 {
98   char* ecdhe_str;
99   char* aud_keystr;
100   char* write_ptr;
101   char* tmp_tok;
102   char* str;
103
104   if (NULL == s)
105     return GNUNET_SYSERR;
106   switch (type)
107   {
108     case GNUNET_GNSRECORD_TYPE_ID_ATTR:
109     case GNUNET_GNSRECORD_TYPE_ID_TOKEN:
110       *data = GNUNET_strdup (s);
111       *data_size = strlen (s);
112       return GNUNET_OK;
113     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA:
114             tmp_tok = GNUNET_strdup (s);
115       ecdhe_str = strtok (tmp_tok, ";");
116       if (NULL == ecdhe_str)
117       {
118         GNUNET_free (tmp_tok);
119         return GNUNET_SYSERR;
120       }
121       aud_keystr = strtok (NULL, ";");
122       if (NULL == aud_keystr)
123       {
124         GNUNET_free (tmp_tok);
125         return GNUNET_SYSERR;
126       }
127       str = strtok (NULL, ";");
128       if (NULL == str)
129       {
130         GNUNET_free (tmp_tok);
131         return GNUNET_SYSERR;
132       }
133       *data_size = strlen (str) + 1
134         +sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
135         +sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
136       *data = GNUNET_malloc (*data_size);
137
138       write_ptr = *data;
139       GNUNET_STRINGS_string_to_data (ecdhe_str,
140                                      strlen (ecdhe_str),
141                                      write_ptr,
142                                      sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
143       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
144       GNUNET_STRINGS_string_to_data (aud_keystr,
145                                      strlen (aud_keystr),
146                                      write_ptr,
147                                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
148       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
149       memcpy (write_ptr, str, strlen (str) + 1); //with 0-Terminator
150       GNUNET_free (tmp_tok);
151       return GNUNET_OK;
152
153     default:
154       return GNUNET_SYSERR;
155   }
156 }
157
158
159 /**
160  * Mapping of record type numbers to human-readable
161  * record type names.
162  */
163         static struct {
164           const char *name;
165           uint32_t number;
166         } name_map[] = {
167           { "ID_ATTR", GNUNET_GNSRECORD_TYPE_ID_ATTR },
168           { "ID_TOKEN", GNUNET_GNSRECORD_TYPE_ID_TOKEN },
169           { "ID_TOKEN_METADATA", GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA },
170           { NULL, UINT32_MAX }
171         };
172
173
174 /**
175  * Convert a type name (i.e. "AAAA") to the corresponding number.
176  *
177  * @param cls closure, unused
178  * @param dns_typename name to convert
179  * @return corresponding number, UINT32_MAX on error
180  */
181 static uint32_t
182 typename_to_number (void *cls,
183                     const char *dns_typename)
184 {
185   unsigned int i;
186
187   i=0;
188   while ( (NULL != name_map[i].name) &&
189           (0 != strcasecmp (dns_typename, name_map[i].name)) )
190     i++;
191   return name_map[i].number;
192 }
193
194
195 /**
196  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
197  *
198  * @param cls closure, unused
199  * @param type number of a type to convert
200  * @return corresponding typestring, NULL on error
201  */
202 static const char *
203 number_to_typename (void *cls,
204                     uint32_t type)
205 {
206   unsigned int i;
207
208   i=0;
209   while ( (NULL != name_map[i].name) &&
210           (type != name_map[i].number) )
211     i++;
212   return name_map[i].name;
213 }
214
215
216 /**
217  * Entry point for the plugin.
218  *
219  * @param cls NULL
220  * @return the exported block API
221  */
222 void *
223 libgnunet_plugin_gnsrecord_identity_init (void *cls)
224 {
225   struct GNUNET_GNSRECORD_PluginFunctions *api;
226
227   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
228   api->value_to_string = &value_to_string;
229   api->string_to_value = &string_to_value;
230   api->typename_to_number = &typename_to_number;
231   api->number_to_typename = &number_to_typename;
232   return api;
233 }
234
235
236 /**
237  * Exit point from the plugin.
238  *
239  * @param cls the return value from #libgnunet_plugin_block_test_init
240  * @return NULL
241  */
242 void *
243 libgnunet_plugin_gnsrecord_identity_done (void *cls)
244 {
245   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
246
247   GNUNET_free (api);
248   return NULL;
249 }
250
251 /* end of plugin_gnsrecord_dns.c */