obsolete due to alpine policy
[oweals/gnunet.git] / src / reclaim / json_reclaim.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2009-2018 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 rest-plugins/json_reclaim.c
23  * @brief JSON handling of reclaim data
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27
28 #include "gnunet_util_lib.h"
29
30 #include "gnunet_json_lib.h"
31 #include "gnunet_reclaim_attribute_lib.h"
32 #include "gnunet_reclaim_service.h"
33
34
35 /**
36  * Parse given JSON object to a claim
37  *
38  * @param cls closure, NULL
39  * @param root the json object representing data
40  * @param spec where to write the data
41  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
42  */
43 static int
44 parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
45 {
46   struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
47   const char *name_str = NULL;
48   const char *val_str = NULL;
49   const char *type_str = NULL;
50   const char *id_str = NULL;
51   char *data;
52   int unpack_state;
53   uint32_t type;
54   size_t data_size;
55
56   GNUNET_assert (NULL != root);
57
58   if (! json_is_object (root))
59   {
60     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61                 "Error json is not array nor object!\n");
62     return GNUNET_SYSERR;
63   }
64   // interpret single attribute
65   unpack_state = json_unpack (root,
66                               "{s:s, s?s, s:s, s:s!}",
67                               "name",
68                               &name_str,
69                               "id",
70                               &id_str,
71                               "type",
72                               &type_str,
73                               "value",
74                               &val_str);
75   if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
76       (NULL == type_str))
77   {
78     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
79                 "Error json object has a wrong format!\n");
80     return GNUNET_SYSERR;
81   }
82   type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
83   if (GNUNET_SYSERR ==
84       (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
85                                                  val_str,
86                                                  (void **) &data,
87                                                  &data_size)))
88   {
89     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
90     return GNUNET_SYSERR;
91   }
92   attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, type, data, data_size);
93   if ((NULL == id_str) || (0 == strlen (id_str)))
94     attr->id = 0;
95   else
96     GNUNET_STRINGS_string_to_data (id_str,
97                                    strlen (id_str),
98                                    &attr->id,
99                                    sizeof(uint64_t));
100
101   *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
102   return GNUNET_OK;
103 }
104
105 /**
106  * Cleanup data left from parsing RSA public key.
107  *
108  * @param cls closure, NULL
109  * @param[out] spec where to free the data
110  */
111 static void
112 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
113 {
114   struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
115
116   attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
117   if (NULL != *attr)
118   {
119     GNUNET_free (*attr);
120     *attr = NULL;
121   }
122 }
123
124 /**
125  * JSON Specification for Reclaim claims.
126  *
127  * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
128  * @return JSON Specification
129  */
130 struct GNUNET_JSON_Specification
131 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
132 {
133   struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
134                                            .cleaner = &clean_attr,
135                                            .cls = NULL,
136                                            .field = NULL,
137                                            .ptr = attr,
138                                            .ptr_size = 0,
139                                            .size_ptr = NULL };
140
141   *attr = NULL;
142   return ret;
143 }
144 /**
145  * Parse given JSON object to a ticket
146  *
147  * @param cls closure, NULL
148  * @param root the json object representing data
149  * @param spec where to write the data
150  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
151  */
152 static int
153 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
154 {
155   struct GNUNET_RECLAIM_Ticket *ticket;
156   const char *rnd_str;
157   const char *aud_str;
158   const char *id_str;
159   int unpack_state;
160
161   GNUNET_assert (NULL != root);
162
163   if (! json_is_object (root))
164   {
165     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166                 "Error json is not array nor object!\n");
167     return GNUNET_SYSERR;
168   }
169   // interpret single ticket
170   unpack_state = json_unpack (root,
171                               "{s:s, s:s, s:s!}",
172                               "rnd",
173                               &rnd_str,
174                               "audience",
175                               &aud_str,
176                               "issuer",
177                               &id_str);
178   if (0 != unpack_state)
179   {
180     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181                 "Error json object has a wrong format!\n");
182     return GNUNET_SYSERR;
183   }
184   ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
185   if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
186                                                   strlen (rnd_str),
187                                                   &ticket->rnd,
188                                                   sizeof(uint64_t)))
189   {
190     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
191     GNUNET_free (ticket);
192     return GNUNET_SYSERR;
193   }
194   if (GNUNET_OK !=
195       GNUNET_STRINGS_string_to_data (id_str,
196                                      strlen (id_str),
197                                      &ticket->identity,
198                                      sizeof(
199                                        struct GNUNET_CRYPTO_EcdsaPublicKey)))
200   {
201     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
202     GNUNET_free (ticket);
203     return GNUNET_SYSERR;
204   }
205
206   if (GNUNET_OK !=
207       GNUNET_STRINGS_string_to_data (aud_str,
208                                      strlen (aud_str),
209                                      &ticket->audience,
210                                      sizeof(struct
211                                             GNUNET_CRYPTO_EcdsaPublicKey)))
212   {
213     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
214     GNUNET_free (ticket);
215     return GNUNET_SYSERR;
216   }
217
218   *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
219   return GNUNET_OK;
220 }
221
222 /**
223  * Cleanup data left from parsing RSA public key.
224  *
225  * @param cls closure, NULL
226  * @param[out] spec where to free the data
227  */
228 static void
229 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
230 {
231   struct GNUNET_RECLAIM_Ticket **ticket;
232
233   ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
234   if (NULL != *ticket)
235   {
236     GNUNET_free (*ticket);
237     *ticket = NULL;
238   }
239 }
240
241 /**
242  * JSON Specification for Reclaim tickets.
243  *
244  * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
245  * @return JSON Specification
246  */
247 struct GNUNET_JSON_Specification
248 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
249 {
250   struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
251                                            .cleaner = &clean_ticket,
252                                            .cls = NULL,
253                                            .field = NULL,
254                                            .ptr = ticket,
255                                            .ptr_size = 0,
256                                            .size_ptr = NULL };
257
258   *ticket = NULL;
259   return ret;
260 }