c7cbb8bdd15e7d3e2affb361f866f8eaad398634
[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
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 credential/plugin_gnsrecord_credential.c
23  * @brief gnsrecord plugin to provide the API for CREDENTIAL records
24  * @author Adnan Husain
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_credential_service.h"
31 #include "gnunet_gnsrecord_plugin.h"
32
33
34 /**
35  * Convert the 'value' of a record to a string.
36  *
37  * @param cls closure, unused
38  * @param type type of the record
39  * @param data value in binary encoding
40  * @param data_size number of bytes in @a data
41  * @return NULL on error, otherwise human-readable representation of the value
42  */
43 static char *
44 credential_value_to_string (void *cls,
45                               uint32_t type,
46                               const void *data,
47                               size_t data_size)
48 {
49
50   const char *cdata;
51
52   switch (type)
53   {
54    case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
55    {
56     struct GNUNET_CREDENTIAL_AttributeRecordData attr;
57     char *attr_str;
58     char *subject_pkey;
59     
60     if (data_size < sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData))
61       return NULL; /* malformed */
62     memcpy (&attr,
63             data,
64             sizeof (attr));
65     cdata = data;
66     subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&attr.subject_key);
67     GNUNET_asprintf (&attr_str,
68                      "%s.%s",
69                      subject_pkey,
70                      &cdata[sizeof (attr)]);
71     GNUNET_free (subject_pkey);
72     return attr_str;
73    }
74    case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
75    {
76     struct GNUNET_CREDENTIAL_CredentialRecordData cred;
77     char *cred_str;
78     char *subject_pkey;
79     char *issuer_pkey;
80     if (data_size < sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData))
81         return NULL; /* malformed */
82     memcpy (&cred,
83               data,
84               sizeof (cred));
85     cdata = data;  
86     subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.subject_key);
87     issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.issuer_key);
88
89      GNUNET_asprintf (&cred_str,
90                      "%s %s %s",
91                      subject_pkey,
92                      issuer_pkey,
93                      &cdata[sizeof (cred)]);
94       GNUNET_free (subject_pkey);
95       GNUNET_free (issuer_pkey);
96
97
98
99     return cred_str;
100     }
101   default:
102     return NULL;
103   }
104 }
105
106
107 /**
108  * Convert human-readable version of a 'value' of a record to the binary
109  * representation.
110  *
111  * @param cls closure, unused
112  * @param type type of the record
113  * @param s human-readable string
114  * @param data set to value in binary encoding (will be allocated)
115  * @param data_size set to number of bytes in @a data
116  * @return #GNUNET_OK on success
117  */
118 static int
119 credential_string_to_value (void *cls,
120                      uint32_t type,
121                      const char *s,
122                      void **data,
123                      size_t *data_size)
124 {
125   if (NULL == s)
126     return GNUNET_SYSERR;
127   switch (type)
128   {
129     case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
130       { 
131         struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
132
133         size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
134         if (enclen % 5 > 0)
135           enclen += 5 - enclen % 5;
136         enclen /= 5; /* 260/5 = 52 */
137         char subject_pkey[enclen + 1];
138         char issuer_pkey[enclen + 1];
139         char name[253 + 1];
140
141         if (5 != SSCANF (s,
142                          "%52s %52s %253s",
143                          subject_pkey,
144                          issuer_pkey,
145                          name))
146         {
147           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148                       _("Unable to parse CRED record string `%s'\n"),
149                       s);
150           return GNUNET_SYSERR;
151         }
152         *data_size = sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (name) + 1;
153         *data = cred = GNUNET_malloc (*data_size);
154         GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
155                                                     strlen (subject_pkey),
156                                                     &cred->subject_key);
157         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
158                                                     strlen (issuer_pkey),
159                                                     &cred->issuer_key);
160         GNUNET_memcpy (&cred[1],
161                        name,
162                        strlen (name));
163
164
165         *data = GNUNET_strdup (s);
166         *data_size = strlen (s);
167         return GNUNET_OK;
168       }
169     default:
170       return GNUNET_SYSERR;
171   }
172 }
173
174
175 /**
176  * Mapping of record type numbers to human-readable
177  * record type names.
178  */
179 static struct {
180   const char *name;
181   uint32_t number;
182 } name_map[] = {
183   { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
184   { NULL, UINT32_MAX }
185 };
186
187
188 /**
189  * Convert a type name (i.e. "AAAA") to the corresponding number.
190  *
191  * @param cls closure, unused
192  * @param gns_typename name to convert
193  * @return corresponding number, UINT32_MAX on error
194  */
195 static uint32_t
196 credential_typename_to_number (void *cls,
197                                const char *gns_typename)
198 {
199   unsigned int i;
200
201   i=0;
202   while ( (name_map[i].name != NULL) &&
203           (0 != strcasecmp (gns_typename, name_map[i].name)) )
204     i++;
205   return name_map[i].number;
206 }
207
208
209 /**
210  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
211  *
212  * @param cls closure, unused
213  * @param type number of a type to convert
214  * @return corresponding typestring, NULL on error
215  */
216 static const char *
217 credential_number_to_typename (void *cls,
218                                uint32_t type)
219 {
220   unsigned int i;
221
222   i=0;
223   while ( (name_map[i].name != NULL) &&
224           (type != name_map[i].number) )
225     i++;
226   return name_map[i].name;
227 }
228
229
230 /**
231  * Entry point for the plugin.
232  *
233  * @param cls NULL
234  * @return the exported block API
235  */
236 void *
237 libgnunet_plugin_gnsrecord_credential_init (void *cls)
238 {
239   struct GNUNET_GNSRECORD_PluginFunctions *api;
240
241   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
242   api->value_to_string = &credential_value_to_string;
243   api->string_to_value = &credential_string_to_value;
244   api->typename_to_number = &credential_typename_to_number;
245   api->number_to_typename = &credential_number_to_typename;
246   return api;
247 }
248
249
250 /**
251  * Exit point from the plugin.
252  *
253  * @param cls the return value from #libgnunet_plugin_block_test_init
254  * @return NULL
255  */
256 void *
257 libgnunet_plugin_gnsrecord_credential_done (void *cls)
258 {
259   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
260
261   GNUNET_free (api);
262   return NULL;
263 }
264
265 /* end of plugin_gnsrecord_credential.c */