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