GNUnet 0.28.0-dev.1-15-g082b3bb92
 
Loading...
Searching...
No Matches
json_pack.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 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 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_json_lib.h"
28
29json_t *
31{
32 json_t *ret;
33
34 ret = json_object ();
35 GNUNET_assert (NULL != ret);
36 for (unsigned int i = 0;
37 ! spec[i].final;
38 i++)
39 {
40 if (NULL == spec[i].object)
41 {
42 if (! spec[i].allow_null)
43 {
45 "NULL not allowed for `%s'\n",
46 spec[i].field_name);
47 GNUNET_assert (0);
48 }
49 }
50 else
51 {
52 if (NULL == spec[i].field_name)
53 GNUNET_assert (0 ==
54 json_object_update_new (ret,
55 spec[i].object));
56 else
57 GNUNET_assert (0 ==
58 json_object_set_new (ret,
59 spec[i].field_name,
60 spec[i].object));
61
62 spec[i].object = NULL;
63 }
64 }
65 return ret;
66}
67
68
71{
72 struct GNUNET_JSON_PackSpec ps = {
73 .final = true
74 };
75
76 return ps;
77}
78
79
82{
83 in.allow_null = true;
84 return in;
85}
86
87
90 bool b)
91{
92 struct GNUNET_JSON_PackSpec ps = {
93 .field_name = name,
94 .object = json_boolean (b)
95 };
96
97 GNUNET_assert (NULL != name);
98 return ps;
99}
100
101
104 double f)
105{
106 struct GNUNET_JSON_PackSpec ps = {
107 .field_name = name,
108 .object = json_real (f)
109 };
110
111 GNUNET_assert (NULL != name);
112 return ps;
113}
114
115
118 const char *s)
119{
120 struct GNUNET_JSON_PackSpec ps = {
121 .field_name = name,
122 .object = json_string (s)
123 };
124
125 GNUNET_assert (NULL != name);
126 return ps;
127}
128
129
132 uint64_t num)
133{
134 struct GNUNET_JSON_PackSpec ps = {
135 .field_name = name,
136 .object = json_integer ((json_int_t) num)
137 };
138
139 GNUNET_assert (NULL != name);
140#if JSON_INTEGER_IS_LONG_LONG
141 GNUNET_assert (num <= LLONG_MAX);
142#else
143 GNUNET_assert (num <= LONG_MAX);
144#endif
145 return ps;
146}
147
148
151 int64_t num)
152{
153 struct GNUNET_JSON_PackSpec ps = {
154 .field_name = name,
155 .object = json_integer ((json_int_t) num)
156 };
157
158 GNUNET_assert (NULL != name);
159#if JSON_INTEGER_IS_LONG_LONG
160 GNUNET_assert (num <= LLONG_MAX);
161 GNUNET_assert (num >= LLONG_MIN);
162#else
163 GNUNET_assert (num <= LONG_MAX);
164 GNUNET_assert (num >= LONG_MIN);
165#endif
166 return ps;
167}
168
169
172 json_t *o)
173{
174 struct GNUNET_JSON_PackSpec ps = {
175 .field_name = name,
176 .object = o
177 };
178
179 if (NULL == o)
180 return ps;
181 if (! json_is_object (o))
182 {
184 "Expected JSON object for field `%s'\n",
185 name);
186 GNUNET_assert (0);
187 }
188 return ps;
189}
190
191
194 json_t *o)
195{
196 struct GNUNET_JSON_PackSpec ps = {
197 .field_name = name,
198 .object = o
199 };
200
201 if (NULL == o)
202 return ps;
203 (void) json_incref (o);
204 if (! json_is_object (o))
205 {
207 "Expected JSON object for field `%s'\n",
208 name);
209 GNUNET_assert (0);
210 }
211 return ps;
212}
213
214
217 json_t *a)
218{
219 struct GNUNET_JSON_PackSpec ps = {
220 .field_name = name,
221 .object = a
222 };
223
224 GNUNET_assert (NULL != name);
225 if (NULL == a)
226 return ps;
227 if (! json_is_array (a))
228 {
230 "Expected JSON array for field `%s'\n",
231 name);
232 GNUNET_assert (0);
233 }
234 return ps;
235}
236
237
240 json_t *a)
241{
242 struct GNUNET_JSON_PackSpec ps = {
243 .field_name = name,
244 .object = a
245 };
246
247 GNUNET_assert (NULL != name);
248 if (NULL == a)
249 return ps;
250 (void) json_incref (a);
251 if (! json_is_array (a))
252 {
254 "Expected JSON array for field `%s'\n",
255 name);
256 GNUNET_assert (0);
257 }
258 return ps;
259}
260
261
264 const void *blob,
265 size_t blob_size)
266{
267 struct GNUNET_JSON_PackSpec ps = {
268 .field_name = name,
269 .object = (NULL != blob)
270 ? GNUNET_JSON_from_data (blob,
271 blob_size)
272 : NULL
273 };
274
275 GNUNET_assert (NULL != name);
276 return ps;
277}
278
279
282 const void *blob,
283 size_t blob_size)
284{
285 struct GNUNET_JSON_PackSpec ps = {
286 .field_name = name,
287 .object = (NULL != blob)
289 blob_size)
290 : NULL
291 };
292
293 GNUNET_assert (NULL != name);
294 return ps;
295}
296
297
301{
302 struct GNUNET_JSON_PackSpec ps = {
303 .field_name = name
304 };
305
306 GNUNET_assert (NULL != name);
307 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
308 {
310 GNUNET_assert (NULL != ps.object);
311 }
312 else
313 {
314 ps.object = NULL;
315 }
316 return ps;
317}
318
319
327
328
331 struct GNUNET_TIME_Relative rt)
332{
333 json_t *json;
334
335 GNUNET_assert (NULL != name);
336 json = GNUNET_JSON_from_time_rel (rt);
337 GNUNET_assert (NULL != json);
339 json);
340}
341
342
350
351
354 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
355{
356 struct GNUNET_JSON_PackSpec ps = {
357 .field_name = name,
359 };
360
361 return ps;
362}
363
364
367 const struct GNUNET_CRYPTO_RsaSignature *sig)
368{
369 struct GNUNET_JSON_PackSpec ps = {
370 .field_name = name,
371 .object = GNUNET_JSON_from_rsa_signature (sig)
372 };
373
374 return ps;
375}
376
377
380 const struct
382{
383 struct GNUNET_JSON_PackSpec ps = {
384 .field_name = name
385 };
386
387 if (NULL == sig)
388 return ps;
389
390 switch (sig->cipher)
391 {
393 break;
395 ps.object = GNUNET_JSON_PACK (
396 GNUNET_JSON_pack_string ("cipher",
397 "RSA"),
398 GNUNET_JSON_pack_rsa_signature ("rsa_signature",
399 sig->details.rsa_signature));
400 return ps;
402 ps.object = GNUNET_JSON_PACK (
403 GNUNET_JSON_pack_string ("cipher",
404 "CS"),
405 GNUNET_JSON_pack_data_auto ("cs_signature_r",
406 &sig->details.cs_signature.r_point),
407 GNUNET_JSON_pack_data_auto ("cs_signature_s",
408 &sig->details.cs_signature.s_scalar));
409 return ps;
410 }
411 GNUNET_assert (0);
412 return ps;
413}
414
415
418 const char *name,
419 const struct GNUNET_CRYPTO_BlindedMessage *msg)
420{
421 struct GNUNET_JSON_PackSpec ps = {
422 .field_name = name,
423 };
424
425 switch (msg->cipher)
426 {
428 break;
430 ps.object = GNUNET_JSON_PACK (
431 GNUNET_JSON_pack_string ("cipher",
432 "RSA"),
434 "rsa_blinded_planchet",
435 msg->details.rsa_blinded_message.blinded_msg,
436 msg->details.rsa_blinded_message.blinded_msg_size));
437 return ps;
439 ps.object = GNUNET_JSON_PACK (
440 GNUNET_JSON_pack_string ("cipher",
441 "CS"),
443 "cs_nonce",
444 &msg->details.cs_blinded_message.nonce),
446 "cs_blinded_c0",
447 &msg->details.cs_blinded_message.c[0]),
449 "cs_blinded_c1",
450 &msg->details.cs_blinded_message.c[1]));
451 return ps;
452 }
453 GNUNET_assert (0);
454 return ps;
455}
456
457
460 const char *name,
461 const struct GNUNET_CRYPTO_BlindedSignature *sig)
462{
463 struct GNUNET_JSON_PackSpec ps = {
464 .field_name = name,
465 };
466
467 if (NULL == sig)
468 return ps;
469 switch (sig->cipher)
470 {
472 break;
474 ps.object = GNUNET_JSON_PACK (
475 GNUNET_JSON_pack_string ("cipher",
476 "RSA"),
477 GNUNET_JSON_pack_rsa_signature ("blinded_rsa_signature",
478 sig->details.blinded_rsa_signature));
479 return ps;
481 ps.object = GNUNET_JSON_PACK (
482 GNUNET_JSON_pack_string ("cipher",
483 "CS"),
485 sig->details.blinded_cs_answer.b),
487 &sig->details.blinded_cs_answer.s_scalar));
488 return ps;
489 }
490 GNUNET_assert (0);
491 return ps;
492}
493
494
498{
499 const char *str = "INVALID";
500
502 if (NULL == str)
503 {
504 GNUNET_break (0);
505 str = "INVALID";
506 }
508 str);
509}
510
511
512/* end of json_pack.c */
struct GNUNET_MessageHeader * msg
Definition 005.c:2
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
struct GNUNET_CRYPTO_BlindablePrivateKey pk
Private key from command line option, or NULL.
static char * name
Name (label) of the records to list.
static struct GNUNET_SCHEDULER_Task * t
Main task.
functions to parse JSON objects into GNUnet objects
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel(const char *name, struct GNUNET_TIME_Relative rt)
Generate packer instruction for a JSON field of type relative time.
Definition json_pack.c:330
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp at)
Generate packer instruction for a JSON field of type timestamp.
Definition json_pack.c:299
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference is taken over by...
Definition json_pack.c:171
json_t * GNUNET_JSON_from_data64(const void *data, size_t size)
Convert binary data to a JSON string with base64 encoding.
#define GNUNET_JSON_PACK(...)
Pack a JSON object from a spec.
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
#define GNUNET_JSON_pack_data_auto(name, blob)
Generate packer instruction for a JSON field where the size is automatically determined from the argu...
#define GNUNET_log(kind,...)
@ GNUNET_CRYPTO_BSA_INVALID
Invalid type of signature.
@ GNUNET_CRYPTO_BSA_CS
Clause Blind Schnorr signature.
@ GNUNET_CRYPTO_BSA_RSA
RSA blind signature.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition time.c:626
GNUNET_TIME_RounderInterval
Quantities by which we support round up absolute time values.
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition time.c:844
const char * GNUNET_TIME_round_interval2s(enum GNUNET_TIME_RounderInterval ri)
Convert rounding interval to string.
Definition time.c:1293
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition time.c:101
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel(const char *name, struct GNUNET_TIME_Relative rt)
Generate packer instruction for a JSON field of type relative time.
Definition json_pack.c:330
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition json_pack.c:70
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_steal(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference is taken over by ...
Definition json_pack.c:216
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition json_pack.c:263
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool(const char *name, bool b)
Generate packer instruction for a JSON field of type bool.
Definition json_pack.c:89
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp t)
Generate packer instruction for a JSON field of type timestamp.
Definition json_pack.c:299
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference is taken over by...
Definition json_pack.c:171
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_incref(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference counter is increm...
Definition json_pack.c:239
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null(struct GNUNET_JSON_PackSpec in)
Modify packer instruction to allow NULL as a value.
Definition json_pack.c:81
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_unblinded_signature(const char *name, const struct GNUNET_CRYPTO_UnblindedSignature *sig)
Generate packer instruction for a JSON field of type unblinded signature.
Definition json_pack.c:379
json_t * GNUNET_JSON_pack_(struct GNUNET_JSON_PackSpec spec[])
Pack a JSON object from a spec.
Definition json_pack.c:30
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_double(const char *name, double f)
Generate packer instruction for a JSON field of type double.
Definition json_pack.c:103
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_blinded_message(const char *name, const struct GNUNET_CRYPTO_BlindedMessage *msg)
Generate packer instruction for a JSON field of type blinded message.
Definition json_pack.c:417
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp_nbo(const char *name, struct GNUNET_TIME_TimestampNBO at)
Generate packer instruction for a JSON field of type timestamp in network byte order.
Definition json_pack.c:321
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_blinded_sig(const char *name, const struct GNUNET_CRYPTO_BlindedSignature *sig)
Generate packer instruction for a JSON field of type blinded signature.
Definition json_pack.c:459
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_uint64(const char *name, uint64_t num)
Generate packer instruction for a JSON field of type unsigned integer.
Definition json_pack.c:131
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rounder_interval(const char *name, enum GNUNET_TIME_RounderInterval ri)
Generate packer instruction of a time rounder interval.
Definition json_pack.c:496
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_incref(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference counter is incre...
Definition json_pack.c:193
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_signature(const char *name, const struct GNUNET_CRYPTO_RsaSignature *sig)
Generate packer instruction for a JSON field of type RSA signature.
Definition json_pack.c:366
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data64_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition json_pack.c:281
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_public_key(const char *name, const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Generate packer instruction for a JSON field of type RSA public key.
Definition json_pack.c:353
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_int64(const char *name, int64_t num)
Generate packer instruction for a JSON field of type signed integer.
Definition json_pack.c:150
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel_nbo(const char *name, struct GNUNET_TIME_RelativeNBO rt)
Generate packer instruction for a JSON field of type relative time in network byte order.
Definition json_pack.c:344
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string(const char *name, const char *s)
Generate packer instruction for a JSON field of type string.
Definition json_pack.c:117
Blinded message ready for blind signing.
Type for blinded signatures.
The public information of an RSA key pair.
Definition crypto_rsa.c:53
Type of (unblinded) signatures.
Element in the array to give to the packer.
bool allow_null
True if a NULL (or 0) argument is allowed.
json_t * object
Object to pack.
bool final
True if last element in the spec array.
Time for relative time used by GNUnet, in microseconds and in network byte order.
Time for relative time used by GNUnet, in microseconds.
Time for timestamps used by GNUnet, in seconds and in network byte order.
Time for timestamps used by GNUnet, in microseconds rounded to seconds.
enum GNUNET_TIME_RounderInterval ri
Definition time.c:1251
const char * str
Definition time.c:1252