Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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 exchangedb/get_refresh.c
18 : * @brief Implementation of the get_refresh function for Postgres
19 : * @author get_refresh
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/get_refresh.h"
23 : #include "helper.h"
24 :
25 :
26 : enum GNUNET_DB_QueryStatus
27 68 : TALER_EXCHANGEDB_get_refresh (
28 : struct TALER_EXCHANGEDB_PostgresContext *pg,
29 : const struct TALER_RefreshCommitmentP *rc,
30 : struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *refresh)
31 : {
32 68 : struct GNUNET_PQ_QueryParam params[] = {
33 68 : GNUNET_PQ_query_param_auto_from_type (rc),
34 : GNUNET_PQ_query_param_end
35 : };
36 : bool no_cs_r_values;
37 : bool no_cs_r_choices;
38 : bool no_transfer_pubs;
39 : size_t num_denom_sigs;
40 : size_t num_transfer_pubs;
41 68 : struct TALER_BlindedDenominationSignature *denom_sigs = NULL;
42 68 : struct GNUNET_CRYPTO_CSPublicRPairP *cs_r_values = NULL;
43 68 : struct TALER_TransferPublicKeyP *transfer_pubs = NULL;
44 68 : uint64_t *denom_serials = NULL;
45 : uint32_t rc_version;
46 68 : struct GNUNET_PQ_ResultSpec rs[] = {
47 68 : GNUNET_PQ_result_spec_uint64 ("refresh_id",
48 : &refresh->refresh_id),
49 68 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
50 : &refresh->amount_with_fee),
51 68 : GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub",
52 : &refresh->coin.coin_pub),
53 68 : GNUNET_PQ_result_spec_allow_null (
54 68 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
55 : &refresh->coin.h_age_commitment),
56 : &refresh->coin.no_age_commitment),
57 68 : GNUNET_PQ_result_spec_auto_from_type ("old_coin_sig",
58 : &refresh->coin_sig),
59 68 : GNUNET_PQ_result_spec_auto_from_type ("refresh_seed",
60 : &refresh->refresh_seed),
61 68 : GNUNET_PQ_result_spec_uint32 ("noreveal_index",
62 : &refresh->noreveal_index),
63 68 : GNUNET_PQ_result_spec_allow_null (
64 68 : GNUNET_PQ_result_spec_auto_from_type ("blinding_seed",
65 : &refresh->blinding_seed),
66 : &refresh->no_blinding_seed),
67 68 : GNUNET_PQ_result_spec_allow_null (
68 : TALER_PQ_result_spec_array_cs_r_pub (pg->conn,
69 : "cs_r_values",
70 : &refresh->num_cs_r_values,
71 : &cs_r_values),
72 : &no_cs_r_values),
73 68 : GNUNET_PQ_result_spec_allow_null (
74 : GNUNET_PQ_result_spec_uint64 ("cs_r_choices",
75 : &refresh->cs_r_choices),
76 : &no_cs_r_choices),
77 68 : GNUNET_PQ_result_spec_auto_from_type ("planchets_h",
78 : &refresh->planchets_h),
79 68 : GNUNET_PQ_result_spec_auto_from_type ("selected_h",
80 : &refresh->selected_h),
81 68 : GNUNET_PQ_result_spec_allow_null (
82 : GNUNET_PQ_result_spec_array_fixed_size (pg->conn,
83 : "transfer_pubs",
84 : sizeof(*transfer_pubs),
85 : &num_transfer_pubs,
86 : (void **) &transfer_pubs),
87 : &no_transfer_pubs),
88 68 : GNUNET_PQ_result_spec_array_uint64 (pg->conn,
89 : "denom_serials",
90 : &refresh->num_coins,
91 : &denom_serials),
92 68 : TALER_PQ_result_spec_array_blinded_denom_sig (pg->conn,
93 : "denom_sigs",
94 : &num_denom_sigs,
95 : &denom_sigs),
96 68 : GNUNET_PQ_result_spec_bool ("revealed",
97 : &refresh->revealed),
98 68 : GNUNET_PQ_result_spec_uint32 ("rc_version",
99 : &rc_version),
100 : GNUNET_PQ_result_spec_end
101 : };
102 : enum GNUNET_DB_QueryStatus qs;
103 :
104 68 : memset (&refresh->coin.denom_sig,
105 : 0,
106 : sizeof (refresh->coin.denom_sig));
107 68 : PREPARE (pg,
108 : "get_refresh",
109 : "SELECT"
110 : " refresh_id"
111 : ",amount_with_fee"
112 : ",old_coin_pub"
113 : ",kc.age_commitment_hash AS age_commitment_hash"
114 : ",old_coin_sig"
115 : ",refresh_seed"
116 : ",noreveal_index"
117 : ",blinding_seed"
118 : ",cs_r_values"
119 : ",cs_r_choices"
120 : ",planchets_h"
121 : ",transfer_pubs"
122 : ",selected_h"
123 : ",denom_serials"
124 : ",denom_sigs"
125 : ",revealed"
126 : ",rc_version"
127 : " FROM refresh"
128 : " JOIN known_coins kc"
129 : " ON (old_coin_pub = kc.coin_pub)"
130 : " WHERE rc = $1;"
131 : );
132 68 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
133 : "get_refresh",
134 : params,
135 : rs);
136 68 : GNUNET_PQ_cleanup_query_params_closures (params);
137 68 : if (0 > qs)
138 : {
139 0 : GNUNET_break (0);
140 0 : GNUNET_PQ_cleanup_result (rs);
141 0 : return qs;
142 : }
143 68 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
144 : {
145 16 : GNUNET_PQ_cleanup_result (rs);
146 16 : return qs;
147 : }
148 52 : if (refresh->num_coins != num_denom_sigs)
149 : {
150 0 : GNUNET_break (0);
151 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 : "got inconsistent number of entries in refresh from DB: "
153 : "num_coins=%ld, num_denom_sigs=%ld\n",
154 : refresh->num_coins,
155 : num_denom_sigs);
156 0 : GNUNET_PQ_cleanup_result (rs);
157 0 : return GNUNET_DB_STATUS_HARD_ERROR;
158 : }
159 52 : switch (rc_version)
160 : {
161 52 : case TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27:
162 : case TALER_REFRESH_COMMITMENT_VERSION_LEGACY_VDOLDPLUS_BROKEN:
163 : case TALER_REFRESH_COMMITMENT_VERSION_CURRENT:
164 52 : break;
165 0 : default:
166 0 : GNUNET_break (0);
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 : "got unknown rc_version %u in refresh from DB\n",
169 : (unsigned int) rc_version);
170 0 : GNUNET_PQ_cleanup_result (rs);
171 0 : return GNUNET_DB_STATUS_HARD_ERROR;
172 : }
173 52 : refresh->rc_version = (enum TALER_RefreshCommitmentVersion) rc_version;
174 52 : if (no_transfer_pubs !=
175 52 : (TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27 == refresh->rc_version))
176 : {
177 2 : GNUNET_break (0);
178 2 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 : "rc_version %u inconsistent with transfer_pubs in refresh from DB\n",
180 : (unsigned int) rc_version);
181 2 : GNUNET_PQ_cleanup_result (rs);
182 2 : return GNUNET_DB_STATUS_HARD_ERROR;
183 : }
184 50 : if (no_transfer_pubs)
185 : {
186 2 : refresh->transfer_pubs = NULL;
187 : }
188 : else
189 : {
190 48 : if (num_transfer_pubs != refresh->num_coins)
191 : {
192 0 : GNUNET_break (0);
193 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 : "got inconsistent number of transfer_pubs in refresh from DB: "
195 : "num_coins=%ld, num_transfer_pubs=%ld\n",
196 : refresh->num_coins,
197 : num_transfer_pubs);
198 0 : GNUNET_PQ_cleanup_result (rs);
199 0 : return GNUNET_DB_STATUS_HARD_ERROR;
200 : }
201 48 : refresh->transfer_pubs = transfer_pubs;
202 : }
203 : /* A blinding seed may be present without any CS denomination
204 : (the client is free to send one), but CS values require it. */
205 50 : if (refresh->no_blinding_seed && ! no_cs_r_values)
206 : {
207 0 : GNUNET_break (0);
208 0 : GNUNET_PQ_cleanup_result (rs);
209 0 : return GNUNET_DB_STATUS_HARD_ERROR;
210 : }
211 50 : if (no_cs_r_choices != no_cs_r_values)
212 : {
213 0 : GNUNET_break (0);
214 0 : GNUNET_PQ_cleanup_result (rs);
215 0 : return GNUNET_DB_STATUS_HARD_ERROR;
216 : }
217 50 : if (no_cs_r_values)
218 : {
219 25 : refresh->cs_r_values = NULL;
220 25 : refresh->num_cs_r_values = 0;
221 : }
222 50 : if (refresh->coin.no_age_commitment)
223 42 : memset (&refresh->coin.h_age_commitment,
224 : 0,
225 : sizeof(refresh->coin.h_age_commitment));
226 50 : refresh->rc = *rc;
227 : /* move the result arrays */
228 50 : refresh->denom_sigs = denom_sigs;
229 50 : refresh->denom_serials = denom_serials;
230 50 : refresh->cs_r_values = cs_r_values;
231 50 : transfer_pubs = NULL;
232 50 : denom_sigs = NULL;
233 50 : denom_serials = NULL;
234 50 : cs_r_values = NULL;
235 50 : GNUNET_PQ_cleanup_result (rs);
236 50 : return qs;
237 : }
|