60b677a4e11044d465bbbd31e690ca28e1be41a9
[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      struct GNUNET_TIME_Absolute etime_abs;
78      char *cred_str;
79      char *subject_pkey;
80      char *issuer_pkey;
81      char *signature;
82      const char *expiration;
83
84      
85      if (data_size < sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData))
86        return NULL; /* malformed */
87      memcpy (&cred,
88              data,
89              sizeof (cred));
90      cdata = data;  
91      subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.subject_key);
92      issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.issuer_key);
93      etime_abs.abs_value_us = GNUNET_ntohll(cred.expiration);
94      expiration = GNUNET_STRINGS_absolute_time_to_string (etime_abs);
95      GNUNET_STRINGS_base64_encode ((char*)&cred.sig,
96                                    sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
97                                    &signature);
98      GNUNET_asprintf (&cred_str,
99                       "%s.%s -> %s exp:%s sig:%s",
100                       issuer_pkey,
101                       &cdata[sizeof (cred)],
102                       subject_pkey,
103                       expiration,
104                       signature);
105      GNUNET_free (subject_pkey);
106      GNUNET_free (issuer_pkey);
107      GNUNET_free (signature);
108      return cred_str;
109    }
110    default:
111    return NULL;
112   }
113 }
114
115
116 /**
117  * Convert human-readable version of a 'value' of a record to the binary
118  * representation.
119  *
120  * @param cls closure, unused
121  * @param type type of the record
122  * @param s human-readable string
123  * @param data set to value in binary encoding (will be allocated)
124  * @param data_size set to number of bytes in @a data
125  * @return #GNUNET_OK on success
126  */
127 static int
128 credential_string_to_value (void *cls,
129                             uint32_t type,
130                             const char *s,
131                             void **data,
132                             size_t *data_size)
133 {
134   if (NULL == s)
135     return GNUNET_SYSERR;
136   switch (type)
137   {
138     case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
139       { 
140         struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
141
142         size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
143         if (enclen % 5 > 0)
144           enclen += 5 - enclen % 5;
145         enclen /= 5; /* 260/5 = 52 */
146         char subject_pkey[enclen + 1];
147         char issuer_pkey[enclen + 1];
148         char name[253 + 1];
149         char signature[128]; //TODO max payload size
150         char expiration[256];
151
152         struct GNUNET_CRYPTO_EcdsaSignature *sig;
153         struct GNUNET_TIME_Absolute etime_abs;
154
155         if (5 != SSCANF (s,
156                          "%52s.%253s -> %52s exp:%255s sig:%127s",
157                          issuer_pkey,
158                          name,
159                          subject_pkey,
160                          expiration,
161                          signature))
162         {
163           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164                       _("Unable to parse CRED record string `%s'\n"),
165                       s);
166           return GNUNET_SYSERR;
167         }
168         *data_size = sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (name) + 1;
169         *data = cred = GNUNET_malloc (*data_size);
170         GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
171                                                     strlen (subject_pkey),
172                                                     &cred->subject_key);
173         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
174                                                     strlen (issuer_pkey),
175                                                     &cred->issuer_key);
176         GNUNET_STRINGS_fancy_time_to_absolute (expiration,
177                                                &etime_abs);
178         GNUNET_STRINGS_base64_decode (signature,
179                                       strlen (signature),
180                                       (char**)&sig);
181         cred->sig = *sig;
182         cred->expiration = htonl (etime_abs.abs_value_us);
183         GNUNET_free (sig);
184         GNUNET_memcpy (&cred[1],
185                        name,
186                        strlen (name));
187
188
189         *data = GNUNET_strdup (s);
190         *data_size = strlen (s);
191         return GNUNET_OK;
192       }
193     default:
194       return GNUNET_SYSERR;
195   }
196 }
197
198
199 /**
200  * Mapping of record type numbers to human-readable
201  * record type names.
202  */
203 static struct {
204   const char *name;
205   uint32_t number;
206 } name_map[] = {
207   { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
208   { NULL, UINT32_MAX }
209 };
210
211
212 /**
213  * Convert a type name (i.e. "AAAA") to the corresponding number.
214  *
215  * @param cls closure, unused
216  * @param gns_typename name to convert
217  * @return corresponding number, UINT32_MAX on error
218  */
219 static uint32_t
220 credential_typename_to_number (void *cls,
221                                const char *gns_typename)
222 {
223   unsigned int i;
224
225   i=0;
226   while ( (name_map[i].name != NULL) &&
227           (0 != strcasecmp (gns_typename, name_map[i].name)) )
228     i++;
229   return name_map[i].number;
230 }
231
232
233 /**
234  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
235  *
236  * @param cls closure, unused
237  * @param type number of a type to convert
238  * @return corresponding typestring, NULL on error
239  */
240 static const char *
241 credential_number_to_typename (void *cls,
242                                uint32_t type)
243 {
244   unsigned int i;
245
246   i=0;
247   while ( (name_map[i].name != NULL) &&
248           (type != name_map[i].number) )
249     i++;
250   return name_map[i].name;
251 }
252
253
254 /**
255  * Entry point for the plugin.
256  *
257  * @param cls NULL
258  * @return the exported block API
259  */
260 void *
261 libgnunet_plugin_gnsrecord_credential_init (void *cls)
262 {
263   struct GNUNET_GNSRECORD_PluginFunctions *api;
264
265   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
266   api->value_to_string = &credential_value_to_string;
267   api->string_to_value = &credential_string_to_value;
268   api->typename_to_number = &credential_typename_to_number;
269   api->number_to_typename = &credential_number_to_typename;
270   return api;
271 }
272
273
274 /**
275  * Exit point from the plugin.
276  *
277  * @param cls the return value from #libgnunet_plugin_block_test_init
278  * @return NULL
279  */
280 void *
281 libgnunet_plugin_gnsrecord_credential_done (void *cls)
282 {
283   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
284
285   GNUNET_free (api);
286   return NULL;
287 }
288
289 /* end of plugin_gnsrecord_credential.c */