281113a3461dd280d07ac3c71b26027cf4f9a325
[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 #include "gnunet_signatures.h"
33
34
35 /**
36  * Convert the 'value' of a record to a string.
37  *
38  * @param cls closure, unused
39  * @param type type of the record
40  * @param data value in binary encoding
41  * @param data_size number of bytes in @a data
42  * @return NULL on error, otherwise human-readable representation of the value
43  */
44 static char *
45 credential_value_to_string (void *cls,
46                               uint32_t type,
47                               const void *data,
48                               size_t data_size)
49 {
50
51   const char *cdata;
52
53   switch (type)
54   {
55    case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
56    {
57     struct GNUNET_CREDENTIAL_AttributeRecordData attr;
58     char *attr_str;
59     char *subject_pkey;
60     
61     if (data_size < sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData))
62       return NULL; /* malformed */
63     memcpy (&attr,
64             data,
65             sizeof (attr));
66     cdata = data;
67     subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&attr.subject_key);
68     if (data_size == sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData))
69     {
70       return subject_pkey;
71     } else {
72       GNUNET_asprintf (&attr_str,
73                        "%s %s",
74                        subject_pkey,
75                        &cdata[sizeof (attr)]);
76     }
77     GNUNET_free (subject_pkey);
78     return attr_str;
79    }
80    case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
81    {
82      struct GNUNET_CREDENTIAL_CredentialRecordData cred;
83      struct GNUNET_TIME_Absolute etime_abs;
84      char *cred_str;
85      char *subject_pkey;
86      char *issuer_pkey;
87      char *signature;
88      const char *expiration;
89
90
91      if (data_size < sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData))
92        return NULL; /* malformed */
93      memcpy (&cred,
94              data,
95              sizeof (cred));
96      cdata = data;  
97      subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.subject_key);
98      issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.issuer_key);
99      etime_abs.abs_value_us = GNUNET_ntohll(cred.expiration);
100      expiration = GNUNET_STRINGS_absolute_time_to_string (etime_abs);
101      GNUNET_STRINGS_base64_encode ((char*)&cred.signature,
102                                    sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
103                                    &signature);
104      GNUNET_asprintf (&cred_str,
105                       "%s.%s -> %s | %s | %s",
106                       issuer_pkey,
107                       &cdata[sizeof (cred)],
108                       subject_pkey,
109                       signature,
110                       expiration);
111      GNUNET_free (subject_pkey);
112      GNUNET_free (issuer_pkey);
113      GNUNET_free (signature);
114      return cred_str;
115    }
116    default:
117    return NULL;
118   }
119 }
120
121
122 /**
123  * Convert human-readable version of a 'value' of a record to the binary
124  * representation.
125  *
126  * @param cls closure, unused
127  * @param type type of the record
128  * @param s human-readable string
129  * @param data set to value in binary encoding (will be allocated)
130  * @param data_size set to number of bytes in @a data
131  * @return #GNUNET_OK on success
132  */
133 static int
134 credential_string_to_value (void *cls,
135                             uint32_t type,
136                             const char *s,
137                             void **data,
138                             size_t *data_size)
139 {
140   if (NULL == s)
141     return GNUNET_SYSERR;
142   switch (type)
143   {
144     case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
145       {
146         struct GNUNET_CREDENTIAL_AttributeRecordData *attr;
147         char attr_str[253 + 1];
148         char subject_pkey[52 + 1];
149         int matches = 0;
150         matches = SSCANF (s,
151                           "%s %s",
152                           subject_pkey,
153                           attr_str);
154         if (0 == matches)
155         {
156           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157                       _("Unable to parse ATTR record string `%s'\n"),
158                       s);
159           return GNUNET_SYSERR;
160
161         }
162         if (1 == matches) {
163           *data_size = sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData);
164         } else if (2 == matches) {
165           *data_size = sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData) + strlen (attr_str) + 1;
166         }
167         *data = attr = GNUNET_malloc (*data_size);
168         GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
169                                                     strlen (subject_pkey),
170                                                     &attr->subject_key);
171         if (NULL != attr_str)
172           GNUNET_memcpy (&attr[1],
173                          attr_str,
174                          strlen (attr_str));
175
176
177         return GNUNET_OK;
178       }
179     case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
180       { 
181         struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
182
183         size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
184         if (enclen % 5 > 0)
185           enclen += 5 - enclen % 5;
186         enclen /= 5; /* 260/5 = 52 */
187         char subject_pkey[enclen + 1];
188         char issuer_pkey[enclen + 1];
189         char name[253 + 1];
190         char signature[128]; //TODO max payload size
191         char expiration[256];
192
193         struct GNUNET_CRYPTO_EcdsaSignature *sig;
194         struct GNUNET_TIME_Absolute etime_abs;
195
196         if (5 != SSCANF (s,
197                          "%52s.%253s -> %52s | %s | %255[0-9a-zA-Z: ]",
198                          issuer_pkey,
199                          name,
200                          subject_pkey,
201                          signature,
202                          expiration))
203         {
204           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205                       _("Unable to parse CRED record string `%s'\n"),
206                       s);
207           return GNUNET_SYSERR;
208         }
209         *data_size = sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (name) + 1;
210         *data = cred = GNUNET_malloc (*data_size);
211         GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
212                                                     strlen (subject_pkey),
213                                                     &cred->subject_key);
214         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
215                                                     strlen (issuer_pkey),
216                                                     &cred->issuer_key);
217         GNUNET_STRINGS_fancy_time_to_absolute (expiration,
218                                                &etime_abs);
219         GNUNET_STRINGS_base64_decode (signature,
220                                       strlen (signature),
221                                       (char**)&sig);
222         cred->signature = *sig;
223         cred->expiration = GNUNET_htonll (etime_abs.abs_value_us);
224         cred->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
225         cred->purpose.size = htonl (strlen (name) + 1 + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
226                                     sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + sizeof (uint64_t));
227         GNUNET_free (sig);
228         GNUNET_memcpy (&cred[1],
229                        name,
230                        strlen (name));
231
232
233         return GNUNET_OK;
234       }
235     default:
236       return GNUNET_SYSERR;
237   }
238 }
239
240
241 /**
242  * Mapping of record type numbers to human-readable
243  * record type names.
244  */
245 static struct {
246   const char *name;
247   uint32_t number;
248 } name_map[] = {
249   { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
250   { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
251   { NULL, UINT32_MAX }
252 };
253
254
255 /**
256  * Convert a type name (i.e. "AAAA") to the corresponding number.
257  *
258  * @param cls closure, unused
259  * @param gns_typename name to convert
260  * @return corresponding number, UINT32_MAX on error
261  */
262 static uint32_t
263 credential_typename_to_number (void *cls,
264                                const char *gns_typename)
265 {
266   unsigned int i;
267
268   i=0;
269   while ( (name_map[i].name != NULL) &&
270           (0 != strcasecmp (gns_typename, name_map[i].name)) )
271     i++;
272   return name_map[i].number;
273 }
274
275
276 /**
277  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
278  *
279  * @param cls closure, unused
280  * @param type number of a type to convert
281  * @return corresponding typestring, NULL on error
282  */
283 static const char *
284 credential_number_to_typename (void *cls,
285                                uint32_t type)
286 {
287   unsigned int i;
288
289   i=0;
290   while ( (name_map[i].name != NULL) &&
291           (type != name_map[i].number) )
292     i++;
293   return name_map[i].name;
294 }
295
296
297 /**
298  * Entry point for the plugin.
299  *
300  * @param cls NULL
301  * @return the exported block API
302  */
303 void *
304 libgnunet_plugin_gnsrecord_credential_init (void *cls)
305 {
306   struct GNUNET_GNSRECORD_PluginFunctions *api;
307
308   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
309   api->value_to_string = &credential_value_to_string;
310   api->string_to_value = &credential_string_to_value;
311   api->typename_to_number = &credential_typename_to_number;
312   api->number_to_typename = &credential_number_to_typename;
313   return api;
314 }
315
316
317 /**
318  * Exit point from the plugin.
319  *
320  * @param cls the return value from #libgnunet_plugin_block_test_init
321  * @return NULL
322  */
323 void *
324 libgnunet_plugin_gnsrecord_credential_done (void *cls)
325 {
326   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
327
328   GNUNET_free (api);
329   return NULL;
330 }
331
332 /* end of plugin_gnsrecord_credential.c */