GNUnet 0.24.1-16-gbc519cf4b
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_json_lib.h"
27
28json_t *
30{
31 json_t *ret;
32
33 if (NULL == spec[0].field_name)
34 {
35 ret = spec[0].object;
36 spec[0].object = NULL;
37 return ret;
38 }
39 ret = json_object ();
40 GNUNET_assert (NULL != ret);
41 for (unsigned int i = 0;
42 ! spec[i].final;
43 i++)
44 {
45 if (NULL == spec[i].object)
46 {
47 if (! spec[i].allow_null)
48 {
50 "NULL not allowed for `%s'\n",
51 spec[i].field_name);
52 GNUNET_assert (0);
53 }
54 }
55 else
56 {
57 if (NULL == spec[i].field_name)
58 GNUNET_assert (0 ==
59 json_object_update_new (ret,
60 spec[i].object));
61 else
62 GNUNET_assert (0 ==
63 json_object_set_new (ret,
64 spec[i].field_name,
65 spec[i].object));
66
67 spec[i].object = NULL;
68 }
69 }
70 return ret;
71}
72
73
76{
77 struct GNUNET_JSON_PackSpec ps = {
78 .final = true
79 };
80
81 return ps;
82}
83
84
87{
88 in.allow_null = true;
89 return in;
90}
91
92
95 bool b)
96{
97 struct GNUNET_JSON_PackSpec ps = {
98 .field_name = name,
99 .object = json_boolean (b)
100 };
101
102 GNUNET_assert (NULL != name);
103 return ps;
104}
105
106
109 double f)
110{
111 struct GNUNET_JSON_PackSpec ps = {
112 .field_name = name,
113 .object = json_real (f)
114 };
115
116 GNUNET_assert (NULL != name);
117 return ps;
118}
119
120
123 const char *s)
124{
125 struct GNUNET_JSON_PackSpec ps = {
126 .field_name = name,
127 .object = json_string (s)
128 };
129
130 GNUNET_assert (NULL != name);
131 return ps;
132}
133
134
137 uint64_t num)
138{
139 struct GNUNET_JSON_PackSpec ps = {
140 .field_name = name,
141 .object = json_integer ((json_int_t) num)
142 };
143
144 GNUNET_assert (NULL != name);
145#if JSON_INTEGER_IS_LONG_LONG
146 GNUNET_assert (num <= LLONG_MAX);
147#else
148 GNUNET_assert (num <= LONG_MAX);
149#endif
150 return ps;
151}
152
153
156 int64_t num)
157{
158 struct GNUNET_JSON_PackSpec ps = {
159 .field_name = name,
160 .object = json_integer ((json_int_t) num)
161 };
162
163 GNUNET_assert (NULL != name);
164#if JSON_INTEGER_IS_LONG_LONG
165 GNUNET_assert (num <= LLONG_MAX);
166 GNUNET_assert (num >= LLONG_MIN);
167#else
168 GNUNET_assert (num <= LONG_MAX);
169 GNUNET_assert (num >= LONG_MIN);
170#endif
171 return ps;
172}
173
174
177 json_t *o)
178{
179 struct GNUNET_JSON_PackSpec ps = {
180 .field_name = name,
181 .object = o
182 };
183
184 if (NULL == o)
185 return ps;
186 if (! json_is_object (o))
187 {
189 "Expected JSON object for field `%s'\n",
190 name);
191 GNUNET_assert (0);
192 }
193 return ps;
194}
195
196
199 json_t *o)
200{
201 struct GNUNET_JSON_PackSpec ps = {
202 .field_name = name,
203 .object = o
204 };
205
206 if (NULL == o)
207 return ps;
208 (void) json_incref (o);
209 if (! json_is_object (o))
210 {
212 "Expected JSON object for field `%s'\n",
213 name);
214 GNUNET_assert (0);
215 }
216 return ps;
217}
218
219
222 json_t *a)
223{
224 struct GNUNET_JSON_PackSpec ps = {
225 .field_name = name,
226 .object = a
227 };
228
229 GNUNET_assert (NULL != name);
230 if (NULL == a)
231 return ps;
232 if (! json_is_array (a))
233 {
235 "Expected JSON array for field `%s'\n",
236 name);
237 GNUNET_assert (0);
238 }
239 return ps;
240}
241
242
245 json_t *a)
246{
247 struct GNUNET_JSON_PackSpec ps = {
248 .field_name = name,
249 .object = a
250 };
251
252 GNUNET_assert (NULL != name);
253 if (NULL == a)
254 return ps;
255 (void) json_incref (a);
256 if (! json_is_array (a))
257 {
259 "Expected JSON array for field `%s'\n",
260 name);
261 GNUNET_assert (0);
262 }
263 return ps;
264}
265
266
269 const void *blob,
270 size_t blob_size)
271{
272 struct GNUNET_JSON_PackSpec ps = {
273 .field_name = name,
274 .object = (NULL != blob)
275 ? GNUNET_JSON_from_data (blob,
276 blob_size)
277 : NULL
278 };
279
280 GNUNET_assert (NULL != name);
281 return ps;
282}
283
284
287 const void *blob,
288 size_t blob_size)
289{
290 struct GNUNET_JSON_PackSpec ps = {
291 .field_name = name,
292 .object = (NULL != blob)
294 blob_size)
295 : NULL
296 };
297
298 GNUNET_assert (NULL != name);
299 return ps;
300}
301
302
306{
307 struct GNUNET_JSON_PackSpec ps = {
308 .field_name = name
309 };
310
311 GNUNET_assert (NULL != name);
312 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
313 {
315 GNUNET_assert (NULL != ps.object);
316 }
317 else
318 {
319 ps.object = NULL;
320 }
321 return ps;
322}
323
324
327 struct GNUNET_TIME_TimestampNBO at)
328{
331}
332
333
336 struct GNUNET_TIME_Relative rt)
337{
338 json_t *json;
339
340 GNUNET_assert (NULL != name);
341 json = GNUNET_JSON_from_time_rel (rt);
342 GNUNET_assert (NULL != json);
344 json);
345}
346
347
350 struct GNUNET_TIME_RelativeNBO rt)
351{
354}
355
356
359 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
360{
361 struct GNUNET_JSON_PackSpec ps = {
362 .field_name = name,
364 };
365
366 return ps;
367}
368
369
372 const struct GNUNET_CRYPTO_RsaSignature *sig)
373{
374 struct GNUNET_JSON_PackSpec ps = {
375 .field_name = name,
376 .object = GNUNET_JSON_from_rsa_signature (sig)
377 };
378
379 return ps;
380}
381
382
385 const struct
387{
388 struct GNUNET_JSON_PackSpec ps = {
389 .field_name = name
390 };
391
392 if (NULL == sig)
393 return ps;
394
395 switch (sig->cipher)
396 {
398 break;
400 ps.object = GNUNET_JSON_PACK (
401 GNUNET_JSON_pack_string ("cipher",
402 "RSA"),
403 GNUNET_JSON_pack_rsa_signature ("rsa_signature",
404 sig->details.rsa_signature));
405 return ps;
407 ps.object = GNUNET_JSON_PACK (
408 GNUNET_JSON_pack_string ("cipher",
409 "CS"),
410 GNUNET_JSON_pack_data_auto ("cs_signature_r",
411 &sig->details.cs_signature.r_point),
412 GNUNET_JSON_pack_data_auto ("cs_signature_s",
413 &sig->details.cs_signature.s_scalar));
414 return ps;
415 }
416 GNUNET_assert (0);
417 return ps;
418}
419
420
423 const char *name,
424 const struct GNUNET_CRYPTO_BlindedMessage *msg)
425{
426 struct GNUNET_JSON_PackSpec ps = {
427 .field_name = name,
428 };
429
430 switch (msg->cipher)
431 {
433 break;
435 ps.object = GNUNET_JSON_PACK (
436 GNUNET_JSON_pack_string ("cipher",
437 "RSA"),
439 "rsa_blinded_planchet",
440 msg->details.rsa_blinded_message.blinded_msg,
441 msg->details.rsa_blinded_message.blinded_msg_size));
442 return ps;
444 ps.object = GNUNET_JSON_PACK (
445 GNUNET_JSON_pack_string ("cipher",
446 "CS"),
448 "cs_nonce",
449 &msg->details.cs_blinded_message.nonce),
451 "cs_blinded_c0",
452 &msg->details.cs_blinded_message.c[0]),
454 "cs_blinded_c1",
455 &msg->details.cs_blinded_message.c[1]));
456 return ps;
457 }
458 GNUNET_assert (0);
459 return ps;
460}
461
462
465 const char *name,
466 const struct GNUNET_CRYPTO_BlindedSignature *sig)
467{
468 struct GNUNET_JSON_PackSpec ps = {
469 .field_name = name,
470 };
471
472 if (NULL == sig)
473 return ps;
474 switch (sig->cipher)
475 {
477 break;
479 ps.object = GNUNET_JSON_PACK (
480 GNUNET_JSON_pack_string ("cipher",
481 "RSA"),
482 GNUNET_JSON_pack_rsa_signature ("blinded_rsa_signature",
483 sig->details.blinded_rsa_signature));
484 return ps;
486 ps.object = GNUNET_JSON_PACK (
487 GNUNET_JSON_pack_string ("cipher",
488 "CS"),
490 sig->details.blinded_cs_answer.b),
492 &sig->details.blinded_cs_answer.s_scalar));
493 return ps;
494 }
495 GNUNET_assert (0);
496 return ps;
497}
498
499
500/* 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_PrivateKey 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
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
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.
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.
@ 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:630
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:848
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:335
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:75
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:221
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:268
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:94
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:304
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:176
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:244
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:86
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:384
json_t * GNUNET_JSON_pack_(struct GNUNET_JSON_PackSpec spec[])
Pack a JSON object from a spec.
Definition: json_pack.c:29
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:108
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:422
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:326
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:464
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:136
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:198
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:371
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:286
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:358
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:155
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:349
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:122
Blinded message ready for blind signing.
Type for blinded signatures.
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
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.