pq_result_helper.c (4455B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 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_result_helper.c 18 * @brief functions to initialize parameter arrays 19 * @author Johannes Casaburi 20 */ 21 #include "donau_config.h" 22 #include <taler/taler_pq_lib.h> 23 #include "donau_util.h" 24 #include "donau_pq_lib.h" 25 26 /** 27 * Extract data from a Postgres database @a result at row @a row. 28 * 29 * @param cls closure 30 * @param result where to extract data from 31 * @param row the row to extract data from 32 * @param fname name (or prefix) of the fields to extract from 33 * @param[in,out] dst_size where to store size of result, may be NULL 34 * @param[out] dst where to store the result 35 * @return 36 * #GNUNET_YES if all results could be extracted 37 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 38 */ 39 static enum GNUNET_GenericReturnValue 40 extract_donation_unit_pub (void *cls, 41 PGresult *result, 42 int row, 43 const char *fname, 44 size_t *dst_size, 45 void *dst) 46 { 47 struct DONAU_DonationUnitPublicKey *pk = dst; 48 struct GNUNET_CRYPTO_BlindSignPublicKey *bpk; 49 size_t len; 50 const char *res; 51 int fnum; 52 uint32_t be[1]; 53 54 (void) cls; 55 (void) dst_size; 56 fnum = PQfnumber (result, 57 fname); 58 if (fnum < 0) 59 { 60 GNUNET_break (0); 61 return GNUNET_SYSERR; 62 } 63 if (PQgetisnull (result, 64 row, 65 fnum)) 66 return GNUNET_NO; 67 68 /* if a field is null, continue but 69 * remember that we now return a different result */ 70 len = PQgetlength (result, 71 row, 72 fnum); 73 res = PQgetvalue (result, 74 row, 75 fnum); 76 if (len < sizeof (be)) 77 { 78 GNUNET_break (0); 79 return GNUNET_SYSERR; 80 } 81 GNUNET_memcpy (be, 82 res, 83 sizeof (be)); 84 res += sizeof (be); 85 len -= sizeof (be); 86 bpk = GNUNET_new (struct GNUNET_CRYPTO_BlindSignPublicKey); 87 bpk->cipher = ntohl (be[0]); 88 bpk->rc = 1; 89 switch (bpk->cipher) 90 { 91 case GNUNET_CRYPTO_BSA_INVALID: 92 break; 93 case GNUNET_CRYPTO_BSA_RSA: 94 bpk->details.rsa_public_key 95 = GNUNET_CRYPTO_rsa_public_key_decode (res, 96 len); 97 if (NULL == bpk->details.rsa_public_key) 98 { 99 GNUNET_break (0); 100 GNUNET_free (bpk); 101 return GNUNET_SYSERR; 102 } 103 GNUNET_CRYPTO_hash (res, 104 len, 105 &bpk->pub_key_hash); 106 pk->bsign_pub_key = bpk; 107 return GNUNET_OK; 108 case GNUNET_CRYPTO_BSA_CS: 109 if (sizeof (bpk->details.cs_public_key) != len) 110 { 111 GNUNET_break (0); 112 GNUNET_free (bpk); 113 return GNUNET_SYSERR; 114 } 115 GNUNET_memcpy (&bpk->details.cs_public_key, 116 res, 117 len); 118 GNUNET_CRYPTO_hash (res, 119 len, 120 &bpk->pub_key_hash); 121 pk->bsign_pub_key = bpk; 122 return GNUNET_OK; 123 } 124 GNUNET_break (0); 125 GNUNET_free (bpk); 126 return GNUNET_SYSERR; 127 } 128 129 130 /** 131 * Function called to clean up memory allocated 132 * by a #GNUNET_PQ_ResultConverter. 133 * 134 * @param cls closure 135 * @param rd result data to clean up 136 */ 137 static void 138 clean_donation_unit_pub (void *cls, 139 void *rd) 140 { 141 struct DONAU_DonationUnitPublicKey *donation_unit_pub = rd; 142 143 (void) cls; 144 DONAU_donation_unit_pub_free (donation_unit_pub); 145 } 146 147 148 struct GNUNET_PQ_ResultSpec 149 DONAU_PQ_result_spec_donation_unit_pub ( 150 const char *name, 151 struct DONAU_DonationUnitPublicKey *donation_unit_pub) 152 { 153 struct GNUNET_PQ_ResultSpec res = { 154 .conv = &extract_donation_unit_pub, 155 .cleaner = &clean_donation_unit_pub, 156 .dst = (void *) donation_unit_pub, 157 .fname = name 158 }; 159 160 return res; 161 } 162 163 164 /* end of pq_result_helper.c */