Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014, 2015, 2016, 2021, 2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file pq/pq_query_helper.c
18 : * @brief helper functions for Taler-specific libpq (PostGres) interactions
19 : * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20 : * @author Florian Dold
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include <gnunet/gnunet_util_lib.h>
25 : #include <gnunet/gnunet_pq_lib.h>
26 : #include "taler_pq_lib.h"
27 :
28 :
29 : /**
30 : * Function called to convert input argument into SQL parameters.
31 : *
32 : * @param cls closure
33 : * @param data pointer to input argument, here a `struct TALER_AmountNBO`
34 : * @param data_len number of bytes in @a data (if applicable)
35 : * @param[out] param_values SQL data to set
36 : * @param[out] param_lengths SQL length data to set
37 : * @param[out] param_formats SQL format data to set
38 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
39 : * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
40 : * @param scratch_length number of entries left in @a scratch
41 : * @return -1 on error, number of offsets used in @a scratch otherwise
42 : */
43 : static int
44 0 : qconv_amount_nbo (void *cls,
45 : const void *data,
46 : size_t data_len,
47 : void *param_values[],
48 : int param_lengths[],
49 : int param_formats[],
50 : unsigned int param_length,
51 : void *scratch[],
52 : unsigned int scratch_length)
53 : {
54 0 : const struct TALER_AmountNBO *amount = data;
55 0 : unsigned int off = 0;
56 :
57 : (void) cls;
58 : (void) scratch;
59 : (void) scratch_length;
60 0 : GNUNET_assert (sizeof (struct TALER_AmountNBO) == data_len);
61 0 : GNUNET_assert (2 == param_length);
62 0 : param_values[off] = (void *) &amount->value;
63 0 : param_lengths[off] = sizeof (amount->value);
64 0 : param_formats[off] = 1;
65 0 : off++;
66 0 : param_values[off] = (void *) &amount->fraction;
67 0 : param_lengths[off] = sizeof (amount->fraction);
68 0 : param_formats[off] = 1;
69 0 : return 0;
70 : }
71 :
72 :
73 : struct GNUNET_PQ_QueryParam
74 0 : TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
75 : {
76 0 : struct GNUNET_PQ_QueryParam res = {
77 : .conv = &qconv_amount_nbo,
78 : .data = x,
79 : .size = sizeof (*x),
80 : .num_params = 2
81 : };
82 :
83 0 : return res;
84 : }
85 :
86 :
87 : /**
88 : * Function called to convert input argument into SQL parameters.
89 : *
90 : * @param cls closure
91 : * @param data pointer to input argument, here a `struct TALER_Amount`
92 : * @param data_len number of bytes in @a data (if applicable)
93 : * @param[out] param_values SQL data to set
94 : * @param[out] param_lengths SQL length data to set
95 : * @param[out] param_formats SQL format data to set
96 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
97 : * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
98 : * @param scratch_length number of entries left in @a scratch
99 : * @return -1 on error, number of offsets used in @a scratch otherwise
100 : */
101 : static int
102 0 : qconv_amount (void *cls,
103 : const void *data,
104 : size_t data_len,
105 : void *param_values[],
106 : int param_lengths[],
107 : int param_formats[],
108 : unsigned int param_length,
109 : void *scratch[],
110 : unsigned int scratch_length)
111 : {
112 0 : const struct TALER_Amount *amount_hbo = data;
113 : struct TALER_AmountNBO *amount;
114 :
115 : (void) cls;
116 : (void) scratch;
117 : (void) scratch_length;
118 0 : GNUNET_assert (2 == param_length);
119 0 : GNUNET_assert (sizeof (struct TALER_AmountNBO) == data_len);
120 0 : amount = GNUNET_new (struct TALER_AmountNBO);
121 0 : scratch[0] = amount;
122 0 : TALER_amount_hton (amount,
123 : amount_hbo);
124 0 : qconv_amount_nbo (cls,
125 : amount,
126 : sizeof (struct TALER_AmountNBO),
127 : param_values,
128 : param_lengths,
129 : param_formats,
130 : param_length,
131 : &scratch[1],
132 : scratch_length - 1);
133 0 : return 1;
134 : }
135 :
136 :
137 : struct GNUNET_PQ_QueryParam
138 0 : TALER_PQ_query_param_amount (const struct TALER_Amount *x)
139 : {
140 0 : struct GNUNET_PQ_QueryParam res = {
141 : .conv = &qconv_amount,
142 : .data = x,
143 : .size = sizeof (*x),
144 : .num_params = 2
145 : };
146 :
147 0 : return res;
148 : }
149 :
150 :
151 : /**
152 : * Function called to convert input argument into SQL parameters.
153 : *
154 : * @param cls closure
155 : * @param data pointer to input argument
156 : * @param data_len number of bytes in @a data (if applicable)
157 : * @param[out] param_values SQL data to set
158 : * @param[out] param_lengths SQL length data to set
159 : * @param[out] param_formats SQL format data to set
160 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
161 : * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
162 : * @param scratch_length number of entries left in @a scratch
163 : * @return -1 on error, number of offsets used in @a scratch otherwise
164 : */
165 : static int
166 0 : qconv_denom_pub (void *cls,
167 : const void *data,
168 : size_t data_len,
169 : void *param_values[],
170 : int param_lengths[],
171 : int param_formats[],
172 : unsigned int param_length,
173 : void *scratch[],
174 : unsigned int scratch_length)
175 : {
176 0 : const struct TALER_DenominationPublicKey *denom_pub = data;
177 : size_t tlen;
178 : size_t len;
179 : uint32_t be[2];
180 : char *buf;
181 : void *tbuf;
182 :
183 : (void) cls;
184 : (void) data_len;
185 0 : GNUNET_assert (1 == param_length);
186 0 : GNUNET_assert (scratch_length > 0);
187 0 : GNUNET_break (NULL == cls);
188 0 : be[0] = htonl ((uint32_t) denom_pub->cipher);
189 0 : be[1] = htonl (denom_pub->age_mask.bits);
190 0 : switch (denom_pub->cipher)
191 : {
192 0 : case TALER_DENOMINATION_RSA:
193 0 : tlen = GNUNET_CRYPTO_rsa_public_key_encode (
194 0 : denom_pub->details.rsa_public_key,
195 : &tbuf);
196 0 : break;
197 0 : case TALER_DENOMINATION_CS:
198 0 : tlen = sizeof (denom_pub->details.cs_public_key);
199 0 : break;
200 0 : default:
201 0 : GNUNET_assert (0);
202 : }
203 0 : len = tlen + sizeof (be);
204 0 : buf = GNUNET_malloc (len);
205 0 : memcpy (buf,
206 : be,
207 : sizeof (be));
208 0 : switch (denom_pub->cipher)
209 : {
210 0 : case TALER_DENOMINATION_RSA:
211 0 : memcpy (&buf[sizeof (be)],
212 : tbuf,
213 : tlen);
214 0 : GNUNET_free (tbuf);
215 0 : break;
216 0 : case TALER_DENOMINATION_CS:
217 0 : memcpy (&buf[sizeof (be)],
218 0 : &denom_pub->details.cs_public_key,
219 : tlen);
220 0 : break;
221 0 : default:
222 0 : GNUNET_assert (0);
223 : }
224 :
225 0 : scratch[0] = buf;
226 0 : param_values[0] = (void *) buf;
227 0 : param_lengths[0] = len;
228 0 : param_formats[0] = 1;
229 0 : return 1;
230 : }
231 :
232 :
233 : struct GNUNET_PQ_QueryParam
234 0 : TALER_PQ_query_param_denom_pub (
235 : const struct TALER_DenominationPublicKey *denom_pub)
236 : {
237 0 : struct GNUNET_PQ_QueryParam res = {
238 : .conv = &qconv_denom_pub,
239 : .data = denom_pub,
240 : .num_params = 1
241 : };
242 :
243 0 : return res;
244 : }
245 :
246 :
247 : /**
248 : * Function called to convert input argument into SQL parameters.
249 : *
250 : * @param cls closure
251 : * @param data pointer to input argument
252 : * @param data_len number of bytes in @a data (if applicable)
253 : * @param[out] param_values SQL data to set
254 : * @param[out] param_lengths SQL length data to set
255 : * @param[out] param_formats SQL format data to set
256 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
257 : * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
258 : * @param scratch_length number of entries left in @a scratch
259 : * @return -1 on error, number of offsets used in @a scratch otherwise
260 : */
261 : static int
262 0 : qconv_denom_sig (void *cls,
263 : const void *data,
264 : size_t data_len,
265 : void *param_values[],
266 : int param_lengths[],
267 : int param_formats[],
268 : unsigned int param_length,
269 : void *scratch[],
270 : unsigned int scratch_length)
271 : {
272 0 : const struct TALER_DenominationSignature *denom_sig = data;
273 : size_t tlen;
274 : size_t len;
275 : uint32_t be[2];
276 : char *buf;
277 : void *tbuf;
278 :
279 : (void) cls;
280 : (void) data_len;
281 0 : GNUNET_assert (1 == param_length);
282 0 : GNUNET_assert (scratch_length > 0);
283 0 : GNUNET_break (NULL == cls);
284 0 : be[0] = htonl ((uint32_t) denom_sig->cipher);
285 0 : be[1] = htonl (0x00); /* magic marker: unblinded */
286 0 : switch (denom_sig->cipher)
287 : {
288 0 : case TALER_DENOMINATION_RSA:
289 0 : tlen = GNUNET_CRYPTO_rsa_signature_encode (
290 0 : denom_sig->details.rsa_signature,
291 : &tbuf);
292 0 : break;
293 0 : case TALER_DENOMINATION_CS:
294 0 : tlen = sizeof (denom_sig->details.cs_signature);
295 0 : break;
296 0 : default:
297 0 : GNUNET_assert (0);
298 : }
299 0 : len = tlen + sizeof (be);
300 0 : buf = GNUNET_malloc (len);
301 0 : memcpy (buf,
302 : &be,
303 : sizeof (be));
304 0 : switch (denom_sig->cipher)
305 : {
306 0 : case TALER_DENOMINATION_RSA:
307 0 : memcpy (&buf[sizeof (be)],
308 : tbuf,
309 : tlen);
310 0 : GNUNET_free (tbuf);
311 0 : break;
312 0 : case TALER_DENOMINATION_CS:
313 0 : memcpy (&buf[sizeof (be)],
314 0 : &denom_sig->details.cs_signature,
315 : tlen);
316 0 : break;
317 0 : default:
318 0 : GNUNET_assert (0);
319 : }
320 :
321 0 : scratch[0] = buf;
322 0 : param_values[0] = (void *) buf;
323 0 : param_lengths[0] = len;
324 0 : param_formats[0] = 1;
325 0 : return 1;
326 : }
327 :
328 :
329 : struct GNUNET_PQ_QueryParam
330 0 : TALER_PQ_query_param_denom_sig (
331 : const struct TALER_DenominationSignature *denom_sig)
332 : {
333 0 : struct GNUNET_PQ_QueryParam res = {
334 : .conv = &qconv_denom_sig,
335 : .data = denom_sig,
336 : .num_params = 1
337 : };
338 :
339 0 : return res;
340 : }
341 :
342 :
343 : /**
344 : * Function called to convert input argument into SQL parameters.
345 : *
346 : * @param cls closure
347 : * @param data pointer to input argument
348 : * @param data_len number of bytes in @a data (if applicable)
349 : * @param[out] param_values SQL data to set
350 : * @param[out] param_lengths SQL length data to set
351 : * @param[out] param_formats SQL format data to set
352 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
353 : * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
354 : * @param scratch_length number of entries left in @a scratch
355 : * @return -1 on error, number of offsets used in @a scratch otherwise
356 : */
357 : static int
358 0 : qconv_blinded_denom_sig (void *cls,
359 : const void *data,
360 : size_t data_len,
361 : void *param_values[],
362 : int param_lengths[],
363 : int param_formats[],
364 : unsigned int param_length,
365 : void *scratch[],
366 : unsigned int scratch_length)
367 : {
368 0 : const struct TALER_BlindedDenominationSignature *denom_sig = data;
369 : size_t tlen;
370 : size_t len;
371 : uint32_t be[2];
372 : char *buf;
373 : void *tbuf;
374 :
375 : (void) cls;
376 : (void) data_len;
377 0 : GNUNET_assert (1 == param_length);
378 0 : GNUNET_assert (scratch_length > 0);
379 0 : GNUNET_break (NULL == cls);
380 0 : be[0] = htonl ((uint32_t) denom_sig->cipher);
381 0 : be[1] = htonl (0x01); /* magic marker: blinded */
382 0 : switch (denom_sig->cipher)
383 : {
384 0 : case TALER_DENOMINATION_RSA:
385 0 : tlen = GNUNET_CRYPTO_rsa_signature_encode (
386 0 : denom_sig->details.blinded_rsa_signature,
387 : &tbuf);
388 0 : break;
389 0 : case TALER_DENOMINATION_CS:
390 0 : tlen = sizeof (denom_sig->details.blinded_cs_answer);
391 0 : break;
392 0 : default:
393 0 : GNUNET_assert (0);
394 : }
395 0 : len = tlen + sizeof (be);
396 0 : buf = GNUNET_malloc (len);
397 0 : memcpy (buf,
398 : &be,
399 : sizeof (be));
400 0 : switch (denom_sig->cipher)
401 : {
402 0 : case TALER_DENOMINATION_RSA:
403 0 : memcpy (&buf[sizeof (be)],
404 : tbuf,
405 : tlen);
406 0 : GNUNET_free (tbuf);
407 0 : break;
408 0 : case TALER_DENOMINATION_CS:
409 0 : memcpy (&buf[sizeof (be)],
410 0 : &denom_sig->details.blinded_cs_answer,
411 : tlen);
412 0 : break;
413 0 : default:
414 0 : GNUNET_assert (0);
415 : }
416 :
417 0 : scratch[0] = buf;
418 0 : param_values[0] = (void *) buf;
419 0 : param_lengths[0] = len;
420 0 : param_formats[0] = 1;
421 0 : return 1;
422 : }
423 :
424 :
425 : struct GNUNET_PQ_QueryParam
426 0 : TALER_PQ_query_param_blinded_denom_sig (
427 : const struct TALER_BlindedDenominationSignature *denom_sig)
428 : {
429 0 : struct GNUNET_PQ_QueryParam res = {
430 : .conv = &qconv_blinded_denom_sig,
431 : .data = denom_sig,
432 : .num_params = 1
433 : };
434 :
435 0 : return res;
436 : }
437 :
438 :
439 : /**
440 : * Function called to convert input argument into SQL parameters.
441 : *
442 : * @param cls closure
443 : * @param data pointer to input argument
444 : * @param data_len number of bytes in @a data (if applicable)
445 : * @param[out] param_values SQL data to set
446 : * @param[out] param_lengths SQL length data to set
447 : * @param[out] param_formats SQL format data to set
448 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
449 : * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
450 : * @param scratch_length number of entries left in @a scratch
451 : * @return -1 on error, number of offsets used in @a scratch otherwise
452 : */
453 : static int
454 0 : qconv_blinded_planchet (void *cls,
455 : const void *data,
456 : size_t data_len,
457 : void *param_values[],
458 : int param_lengths[],
459 : int param_formats[],
460 : unsigned int param_length,
461 : void *scratch[],
462 : unsigned int scratch_length)
463 : {
464 0 : const struct TALER_BlindedPlanchet *bp = data;
465 : size_t tlen;
466 : size_t len;
467 : uint32_t be[2];
468 : char *buf;
469 :
470 : (void) cls;
471 : (void) data_len;
472 0 : GNUNET_assert (1 == param_length);
473 0 : GNUNET_assert (scratch_length > 0);
474 0 : GNUNET_break (NULL == cls);
475 0 : be[0] = htonl ((uint32_t) bp->cipher);
476 0 : be[1] = htonl (0x0100); /* magic marker: blinded */
477 0 : switch (bp->cipher)
478 : {
479 0 : case TALER_DENOMINATION_RSA:
480 0 : tlen = bp->details.rsa_blinded_planchet.blinded_msg_size;
481 0 : break;
482 0 : case TALER_DENOMINATION_CS:
483 0 : tlen = sizeof (bp->details.cs_blinded_planchet);
484 0 : break;
485 0 : default:
486 0 : GNUNET_assert (0);
487 : }
488 0 : len = tlen + sizeof (be);
489 0 : buf = GNUNET_malloc (len);
490 0 : memcpy (buf,
491 : &be,
492 : sizeof (be));
493 0 : switch (bp->cipher)
494 : {
495 0 : case TALER_DENOMINATION_RSA:
496 0 : memcpy (&buf[sizeof (be)],
497 0 : bp->details.rsa_blinded_planchet.blinded_msg,
498 : tlen);
499 0 : break;
500 0 : case TALER_DENOMINATION_CS:
501 0 : memcpy (&buf[sizeof (be)],
502 0 : &bp->details.cs_blinded_planchet,
503 : tlen);
504 0 : break;
505 0 : default:
506 0 : GNUNET_assert (0);
507 : }
508 0 : scratch[0] = buf;
509 0 : param_values[0] = (void *) buf;
510 0 : param_lengths[0] = len;
511 0 : param_formats[0] = 1;
512 0 : return 1;
513 : }
514 :
515 :
516 : struct GNUNET_PQ_QueryParam
517 0 : TALER_PQ_query_param_blinded_planchet (
518 : const struct TALER_BlindedPlanchet *bp)
519 : {
520 0 : struct GNUNET_PQ_QueryParam res = {
521 : .conv = &qconv_blinded_planchet,
522 : .data = bp,
523 : .num_params = 1
524 : };
525 :
526 0 : return res;
527 : }
528 :
529 :
530 : /**
531 : * Function called to convert input argument into SQL parameters.
532 : *
533 : * @param cls closure
534 : * @param data pointer to input argument
535 : * @param data_len number of bytes in @a data (if applicable)
536 : * @param[out] param_values SQL data to set
537 : * @param[out] param_lengths SQL length data to set
538 : * @param[out] param_formats SQL format data to set
539 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
540 : * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
541 : * @param scratch_length number of entries left in @a scratch
542 : * @return -1 on error, number of offsets used in @a scratch otherwise
543 : */
544 : static int
545 0 : qconv_exchange_withdraw_values (void *cls,
546 : const void *data,
547 : size_t data_len,
548 : void *param_values[],
549 : int param_lengths[],
550 : int param_formats[],
551 : unsigned int param_length,
552 : void *scratch[],
553 : unsigned int scratch_length)
554 : {
555 0 : const struct TALER_ExchangeWithdrawValues *alg_values = data;
556 : size_t tlen;
557 : size_t len;
558 : uint32_t be[2];
559 : char *buf;
560 :
561 : (void) cls;
562 : (void) data_len;
563 0 : GNUNET_assert (1 == param_length);
564 0 : GNUNET_assert (scratch_length > 0);
565 0 : GNUNET_break (NULL == cls);
566 0 : be[0] = htonl ((uint32_t) alg_values->cipher);
567 0 : be[1] = htonl (0x010000); /* magic marker: EWV */
568 0 : switch (alg_values->cipher)
569 : {
570 0 : case TALER_DENOMINATION_RSA:
571 0 : tlen = 0;
572 0 : break;
573 0 : case TALER_DENOMINATION_CS:
574 0 : tlen = sizeof (struct TALER_DenominationCSPublicRPairP);
575 0 : break;
576 0 : default:
577 0 : GNUNET_assert (0);
578 : }
579 0 : len = tlen + sizeof (be);
580 0 : buf = GNUNET_malloc (len);
581 0 : memcpy (buf,
582 : &be,
583 : sizeof (be));
584 0 : switch (alg_values->cipher)
585 : {
586 0 : case TALER_DENOMINATION_RSA:
587 0 : break;
588 0 : case TALER_DENOMINATION_CS:
589 0 : memcpy (&buf[sizeof (be)],
590 0 : &alg_values->details.cs_values,
591 : tlen);
592 0 : break;
593 0 : default:
594 0 : GNUNET_assert (0);
595 : }
596 0 : scratch[0] = buf;
597 0 : param_values[0] = (void *) buf;
598 0 : param_lengths[0] = len;
599 0 : param_formats[0] = 1;
600 0 : return 1;
601 : }
602 :
603 :
604 : struct GNUNET_PQ_QueryParam
605 0 : TALER_PQ_query_param_exchange_withdraw_values (
606 : const struct TALER_ExchangeWithdrawValues *alg_values)
607 : {
608 0 : struct GNUNET_PQ_QueryParam res = {
609 : .conv = &qconv_exchange_withdraw_values,
610 : .data = alg_values,
611 : .num_params = 1
612 : };
613 :
614 0 : return res;
615 : }
616 :
617 :
618 : /**
619 : * Function called to convert input argument into SQL parameters.
620 : *
621 : * @param cls closure
622 : * @param data pointer to input argument, here a `json_t *`
623 : * @param data_len number of bytes in @a data (if applicable)
624 : * @param[out] param_values SQL data to set
625 : * @param[out] param_lengths SQL length data to set
626 : * @param[out] param_formats SQL format data to set
627 : * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
628 : * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
629 : * @param scratch_length number of entries left in @a scratch
630 : * @return -1 on error, number of offsets used in @a scratch otherwise
631 : */
632 : static int
633 0 : qconv_json (void *cls,
634 : const void *data,
635 : size_t data_len,
636 : void *param_values[],
637 : int param_lengths[],
638 : int param_formats[],
639 : unsigned int param_length,
640 : void *scratch[],
641 : unsigned int scratch_length)
642 : {
643 0 : const json_t *json = data;
644 : char *str;
645 :
646 : (void) cls;
647 : (void) data_len;
648 0 : GNUNET_assert (1 == param_length);
649 0 : GNUNET_assert (scratch_length > 0);
650 0 : str = json_dumps (json, JSON_COMPACT);
651 0 : if (NULL == str)
652 0 : return -1;
653 0 : scratch[0] = str;
654 0 : param_values[0] = (void *) str;
655 0 : param_lengths[0] = strlen (str);
656 0 : param_formats[0] = 1;
657 0 : return 1;
658 : }
659 :
660 :
661 : struct GNUNET_PQ_QueryParam
662 0 : TALER_PQ_query_param_json (const json_t *x)
663 : {
664 0 : struct GNUNET_PQ_QueryParam res = {
665 : .conv = &qconv_json,
666 : .data = x,
667 : .num_params = 1
668 : };
669 :
670 0 : return res;
671 : }
672 :
673 :
674 : /* end of pq/pq_query_helper.c */
|