remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / reclaim / plugin_reclaim_attestation_jwt.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file reclaim-attribute/plugin_reclaim_attestation_gnuid.c
23  * @brief reclaim-attribute-plugin-gnuid attribute plugin to provide the API for
24  *                                       fundamental
25  *                                       attribute types.
26  *
27  * @author Martin Schanzenbach
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_reclaim_plugin.h"
32 #include <inttypes.h>
33 #include <jansson.h>
34
35 /**
36    * Convert the 'value' of an attestation to a string.
37    *
38    * @param cls closure, unused
39    * @param type type of the attestation
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 jwt_value_to_string (void *cls,
46                      uint32_t type,
47                      const void *data,
48                      size_t data_size)
49 {
50   switch (type)
51   {
52   case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT:
53     return GNUNET_strndup (data, data_size);
54
55   default:
56     return NULL;
57   }
58 }
59
60
61 /**
62  * Convert human-readable version of a 'value' of an attestation to the binary
63  * representation.
64  *
65  * @param cls closure, unused
66  * @param type type of the attestation
67  * @param s human-readable string
68  * @param data set to value in binary encoding (will be allocated)
69  * @param data_size set to number of bytes in @a data
70  * @return #GNUNET_OK on success
71  */
72 static int
73 jwt_string_to_value (void *cls,
74                      uint32_t type,
75                      const char *s,
76                      void **data,
77                      size_t *data_size)
78 {
79   if (NULL == s)
80     return GNUNET_SYSERR;
81   switch (type)
82   {
83   case GNUNET_RECLAIM_ATTESTATION_TYPE_JWT:
84     *data = GNUNET_strdup (s);
85     *data_size = strlen (s);
86     return GNUNET_OK;
87
88   default:
89     return GNUNET_SYSERR;
90   }
91 }
92
93
94 /**
95  * Mapping of attestation type numbers to human-readable
96  * attestation type names.
97  */
98 static struct
99 {
100   const char *name;
101   uint32_t number;
102 } jwt_attest_name_map[] = { { "JWT", GNUNET_RECLAIM_ATTESTATION_TYPE_JWT },
103                             { NULL, UINT32_MAX } };
104
105 /**
106    * Convert a type name to the corresponding number.
107    *
108    * @param cls closure, unused
109    * @param jwt_typename name to convert
110    * @return corresponding number, UINT32_MAX on error
111    */
112 static uint32_t
113 jwt_typename_to_number (void *cls, const char *jwt_typename)
114 {
115   unsigned int i;
116
117   i = 0;
118   while ((NULL != jwt_attest_name_map[i].name) &&
119          (0 != strcasecmp (jwt_typename, jwt_attest_name_map[i].name)))
120     i++;
121   return jwt_attest_name_map[i].number;
122 }
123
124
125 /**
126  * Convert a type number (i.e. 1) to the corresponding type string
127  *
128  * @param cls closure, unused
129  * @param type number of a type to convert
130  * @return corresponding typestring, NULL on error
131  */
132 static const char *
133 jwt_number_to_typename (void *cls, uint32_t type)
134 {
135   unsigned int i;
136
137   i = 0;
138   while ((NULL != jwt_attest_name_map[i].name) && (type !=
139                                                    jwt_attest_name_map[i].
140                                                    number))
141     i++;
142   return jwt_attest_name_map[i].name;
143 }
144
145
146 /**
147  * Parse a JWT and return the respective claim value as Attribute
148  *
149  * @param cls the plugin
150  * @param attest the jwt attestation
151  * @return a GNUNET_RECLAIM_Attribute, containing the new value
152  */
153 struct GNUNET_RECLAIM_AttributeList *
154 jwt_parse_attributes (void *cls,
155                       const struct GNUNET_RECLAIM_Attestation *attest)
156 {
157   char *jwt_string;
158   struct GNUNET_RECLAIM_AttributeList *attrs;
159   char delim[] = ".";
160   char *val_str = NULL;
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n");
162   char *decoded_jwt;
163   json_t *json_val;
164   json_error_t *json_err = NULL;
165
166   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n", attest->data);
167   if (GNUNET_RECLAIM_ATTESTATION_TYPE_JWT != attest->type)
168     return NULL;
169   attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
170
171   jwt_string = GNUNET_strdup (attest->data);
172   const char *jwt_body = strtok (jwt_string, delim);
173   jwt_body = strtok (NULL, delim);
174   GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body),
175                                    (void **) &decoded_jwt);
176   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", decoded_jwt);
177   GNUNET_assert (NULL != decoded_jwt);
178   json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err);
179   const char *key;
180   json_t *value;
181   json_object_foreach (json_val, key, value) {
182     if (0 == strcmp ("iss", key))
183       continue;
184     if (0 == strcmp ("exp", key))
185       continue;
186     if (0 == strcmp ("iat", key))
187       continue;
188     if (0 == strcmp ("nbf", key))
189       continue;
190     if (0 == strcmp ("aud", key))
191       continue;
192     val_str = json_dumps (value, JSON_ENCODE_ANY);
193     GNUNET_RECLAIM_attribute_list_add (attrs,
194                                        key,
195                                        NULL,
196                                        GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,// FIXME
197                                        val_str,
198                                        strlen (val_str));
199     GNUNET_free (val_str);
200   }
201   GNUNET_free (jwt_string);
202   return attrs;
203 }
204
205
206 /**
207  * Parse a JWT and return the issuer
208  *
209  * @param cls the plugin
210  * @param attest the jwt attestation
211  * @return a string, containing the isser
212  */
213 char *
214 jwt_get_issuer (void *cls,
215                 const struct GNUNET_RECLAIM_Attestation *attest)
216 {
217   const char *jwt_body;
218   char *jwt_string;
219   char delim[] = ".";
220   char *issuer = NULL;
221   char *decoded_jwt;
222   json_t *issuer_json;
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n");
224   json_t *json_val;
225   json_error_t *json_err = NULL;
226
227   if (GNUNET_RECLAIM_ATTESTATION_TYPE_JWT != attest->type)
228     return NULL;
229   jwt_string = GNUNET_strdup (attest->data);
230   jwt_body = strtok (jwt_string, delim);
231   jwt_body = strtok (NULL, delim);
232   GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body),
233                                    (void **) &decoded_jwt);
234   json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err);
235   issuer_json = json_object_get (json_val, "iss");
236   if ((NULL == issuer_json) || (! json_is_string (issuer_json)))
237     return NULL;
238   issuer = GNUNET_strdup (json_string_value (issuer_json));
239   GNUNET_free (jwt_string);
240   return issuer;
241 }
242
243
244 /**
245  * Parse a JWT and return the expiration
246  *
247  * @param cls the plugin
248  * @param attest the jwt attestation
249  * @return a string, containing the isser
250  */
251 int
252 jwt_get_expiration (void *cls,
253                     const struct GNUNET_RECLAIM_Attestation *attest,
254                     struct GNUNET_TIME_Absolute *exp)
255 {
256   const char *jwt_body;
257   char *jwt_string;
258   char delim[] = ".";
259   char *decoded_jwt;
260   json_t *exp_json;
261   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n");
262   json_t *json_val;
263   json_error_t *json_err = NULL;
264
265   if (GNUNET_RECLAIM_ATTESTATION_TYPE_JWT != attest->type)
266     return GNUNET_NO;
267   jwt_string = GNUNET_strdup (attest->data);
268   jwt_body = strtok (jwt_string, delim);
269   jwt_body = strtok (NULL, delim);
270   GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body),
271                                    (void **) &decoded_jwt);
272   json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err);
273   exp_json = json_object_get (json_val, "exp");
274   if ((NULL == exp_json) || (! json_is_integer (exp_json)))
275     return GNUNET_SYSERR;
276   exp->abs_value_us = json_integer_value (exp_json) * 1000 * 1000;
277   GNUNET_free (jwt_string);
278   return GNUNET_OK;
279 }
280
281
282 /**
283  * Entry point for the plugin.
284  *
285  * @param cls NULL
286  * @return the exported block API
287  */
288 void *
289 libgnunet_plugin_reclaim_attestation_jwt_init (void *cls)
290 {
291   struct GNUNET_RECLAIM_AttestationPluginFunctions *api;
292
293   api = GNUNET_new (struct GNUNET_RECLAIM_AttestationPluginFunctions);
294   api->value_to_string = &jwt_value_to_string;
295   api->string_to_value = &jwt_string_to_value;
296   api->typename_to_number = &jwt_typename_to_number;
297   api->number_to_typename = &jwt_number_to_typename;
298   api->get_attributes = &jwt_parse_attributes;
299   api->get_issuer = &jwt_get_issuer;
300   api->get_expiration = &jwt_get_expiration;
301   return api;
302 }
303
304
305 /**
306  * Exit point from the plugin.
307  *
308  * @param cls the return value from #libgnunet_plugin_block_test_init()
309  * @return NULL
310  */
311 void *
312 libgnunet_plugin_reclaim_attestation_jwt_done (void *cls)
313 {
314   struct GNUNET_RECLAIM_AttestationPluginFunctions *api = cls;
315
316   GNUNET_free (api);
317   return NULL;
318 }
319
320
321 /* end of plugin_reclaim_attestation_type_gnuid.c */