GNUnet 0.21.1
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 NULL != spec[i].field_name;
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 GNUNET_assert (0 ==
58 json_object_set_new (ret,
59 spec[i].field_name,
60 spec[i].object));
61 spec[i].object = NULL;
62 }
63 }
64 return ret;
65}
66
67
70{
71 struct GNUNET_JSON_PackSpec ps = {
72 .field_name = NULL
73 };
74
75 return ps;
76}
77
78
81{
82 in.allow_null = true;
83 return in;
84}
85
86
89 bool b)
90{
91 struct GNUNET_JSON_PackSpec ps = {
92 .field_name = name,
93 .object = json_boolean (b)
94 };
95
96 return ps;
97}
98
99
102 double f)
103{
104 struct GNUNET_JSON_PackSpec ps = {
105 .field_name = name,
106 .object = json_real (f)
107 };
108
109 return ps;
110}
111
112
115 const char *s)
116{
117 struct GNUNET_JSON_PackSpec ps = {
118 .field_name = name,
119 .object = json_string (s)
120 };
121
122 return ps;
123}
124
125
128 uint64_t num)
129{
130 struct GNUNET_JSON_PackSpec ps = {
131 .field_name = name,
132 .object = json_integer ((json_int_t) num)
133 };
134
135#if JSON_INTEGER_IS_LONG_LONG
136 GNUNET_assert (num <= LLONG_MAX);
137#else
138 GNUNET_assert (num <= LONG_MAX);
139#endif
140 return ps;
141}
142
143
146 int64_t num)
147{
148 struct GNUNET_JSON_PackSpec ps = {
149 .field_name = name,
150 .object = json_integer ((json_int_t) num)
151 };
152
153#if JSON_INTEGER_IS_LONG_LONG
154 GNUNET_assert (num <= LLONG_MAX);
155 GNUNET_assert (num >= LLONG_MIN);
156#else
157 GNUNET_assert (num <= LONG_MAX);
158 GNUNET_assert (num >= LONG_MIN);
159#endif
160 return ps;
161}
162
163
166 json_t *o)
167{
168 struct GNUNET_JSON_PackSpec ps = {
169 .field_name = name,
170 .object = o
171 };
172
173 if (NULL == o)
174 return ps;
175 if (! json_is_object (o))
176 {
178 "Expected JSON object for field `%s'\n",
179 name);
180 GNUNET_assert (0);
181 }
182 return ps;
183}
184
185
188 json_t *o)
189{
190 struct GNUNET_JSON_PackSpec ps = {
191 .field_name = name,
192 .object = o
193 };
194
195 if (NULL == o)
196 return ps;
197 (void) json_incref (o);
198 if (! json_is_object (o))
199 {
201 "Expected JSON object for field `%s'\n",
202 name);
203 GNUNET_assert (0);
204 }
205 return ps;
206}
207
208
211 json_t *a)
212{
213 struct GNUNET_JSON_PackSpec ps = {
214 .field_name = name,
215 .object = a
216 };
217
218 if (NULL == a)
219 return ps;
220 if (! json_is_array (a))
221 {
223 "Expected JSON array for field `%s'\n",
224 name);
225 GNUNET_assert (0);
226 }
227 return ps;
228}
229
230
233 json_t *a)
234{
235 struct GNUNET_JSON_PackSpec ps = {
236 .field_name = name,
237 .object = a
238 };
239
240 if (NULL == a)
241 return ps;
242 (void) json_incref (a);
243 if (! json_is_array (a))
244 {
246 "Expected JSON array for field `%s'\n",
247 name);
248 GNUNET_assert (0);
249 }
250 return ps;
251}
252
253
256 const void *blob,
257 size_t blob_size)
258{
259 struct GNUNET_JSON_PackSpec ps = {
260 .field_name = name,
261 .object = (NULL != blob)
262 ? GNUNET_JSON_from_data (blob,
263 blob_size)
264 : NULL
265 };
266
267 return ps;
268}
269
270
273 const void *blob,
274 size_t blob_size)
275{
276 struct GNUNET_JSON_PackSpec ps = {
277 .field_name = name,
278 .object = (NULL != blob)
280 blob_size)
281 : NULL
282 };
283
284 return ps;
285}
286
287
291{
292 struct GNUNET_JSON_PackSpec ps = {
293 .field_name = name
294 };
295
296 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
297 {
299 GNUNET_assert (NULL != ps.object);
300 }
301 else
302 {
303 ps.object = NULL;
304 }
305 return ps;
306}
307
308
311 struct GNUNET_TIME_TimestampNBO at)
312{
315}
316
317
320 struct GNUNET_TIME_Relative rt)
321{
322 json_t *json;
323
324 json = GNUNET_JSON_from_time_rel (rt);
325 GNUNET_assert (NULL != json);
327 json);
328}
329
330
333 struct GNUNET_TIME_RelativeNBO rt)
334{
337}
338
339
342 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
343{
344 struct GNUNET_JSON_PackSpec ps = {
345 .field_name = name,
347 };
348
349 return ps;
350}
351
352
355 const struct GNUNET_CRYPTO_RsaSignature *sig)
356{
357 struct GNUNET_JSON_PackSpec ps = {
358 .field_name = name,
359 .object = GNUNET_JSON_from_rsa_signature (sig)
360 };
361
362 return ps;
363}
364
365
368 const struct GNUNET_CRYPTO_UnblindedSignature *sig)
369{
370 struct GNUNET_JSON_PackSpec ps = {
371 .field_name = name
372 };
373
374 if (NULL == sig)
375 return ps;
376
377 switch (sig->cipher)
378 {
380 break;
382 ps.object = GNUNET_JSON_PACK (
383 GNUNET_JSON_pack_string ("cipher",
384 "RSA"),
385 GNUNET_JSON_pack_rsa_signature ("rsa_signature",
386 sig->details.rsa_signature));
387 return ps;
389 ps.object = GNUNET_JSON_PACK (
390 GNUNET_JSON_pack_string ("cipher",
391 "CS"),
392 GNUNET_JSON_pack_data_auto ("cs_signature_r",
393 &sig->details.cs_signature.r_point),
394 GNUNET_JSON_pack_data_auto ("cs_signature_s",
395 &sig->details.cs_signature.s_scalar));
396 return ps;
397 }
398 GNUNET_assert (0);
399 return ps;
400}
401
402
405 const struct GNUNET_CRYPTO_BlindedMessage *msg)
406{
407 struct GNUNET_JSON_PackSpec ps = {
408 .field_name = name,
409 };
410
411 switch (msg->cipher)
412 {
414 break;
416 ps.object = GNUNET_JSON_PACK (
417 GNUNET_JSON_pack_string ("cipher",
418 "RSA"),
420 "rsa_blinded_planchet",
421 msg->details.rsa_blinded_message.blinded_msg,
422 msg->details.rsa_blinded_message.blinded_msg_size));
423 return ps;
425 ps.object = GNUNET_JSON_PACK (
426 GNUNET_JSON_pack_string ("cipher",
427 "CS"),
429 "cs_nonce",
430 &msg->details.cs_blinded_message.nonce),
432 "cs_blinded_c0",
433 &msg->details.cs_blinded_message.c[0]),
435 "cs_blinded_c1",
436 &msg->details.cs_blinded_message.c[1]));
437 return ps;
438 }
439 GNUNET_assert (0);
440 return ps;
441}
442
443
446 const char *name,
447 const struct GNUNET_CRYPTO_BlindedSignature *sig)
448{
449 struct GNUNET_JSON_PackSpec ps = {
450 .field_name = name,
451 };
452
453 if (NULL == sig)
454 return ps;
455 switch (sig->cipher)
456 {
458 break;
460 ps.object = GNUNET_JSON_PACK (
461 GNUNET_JSON_pack_string ("cipher",
462 "RSA"),
463 GNUNET_JSON_pack_rsa_signature ("blinded_rsa_signature",
464 sig->details.blinded_rsa_signature));
465 return ps;
467 ps.object = GNUNET_JSON_PACK (
468 GNUNET_JSON_pack_string ("cipher",
469 "CS"),
471 sig->details.blinded_cs_answer.b),
473 &sig->details.blinded_cs_answer.s_scalar));
474 return ps;
475 }
476 GNUNET_assert (0);
477 return ps;
478}
479
480/* end of json_pack.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static int ret
Final status code.
Definition: gnunet-arm.c:94
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:628
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:844
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:319
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:69
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:210
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:255
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:88
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:289
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:165
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:232
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:80
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:367
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:101
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:404
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:310
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:445
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:127
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:187
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:354
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:272
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:341
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:145
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:332
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:114
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.
const char * field_name
Name of the field to pack.
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.
Rounded time for timestamps used by GNUnet, in seconds.