Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2020 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your option) any later version.
9 :
10 : TALER 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
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing/testing_api_cmd_revoke_denom_key.c
21 : * @brief Implement the revoke test command.
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include "taler_json_lib.h"
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler_signatures.h"
28 : #include "taler_testing_lib.h"
29 :
30 :
31 : /**
32 : * State for a "revoke" CMD.
33 : */
34 : struct RevokeState
35 : {
36 : /**
37 : * Expected HTTP status code.
38 : */
39 : unsigned int expected_response_code;
40 :
41 : /**
42 : * Command that offers a denomination to revoke.
43 : */
44 : const char *coin_reference;
45 :
46 : /**
47 : * The interpreter state.
48 : */
49 : struct TALER_TESTING_Interpreter *is;
50 :
51 : /**
52 : * Handle for the operation.
53 : */
54 : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *kh;
55 :
56 : /**
57 : * Should we use a bogus signature?
58 : */
59 : bool bad_sig;
60 :
61 : };
62 :
63 :
64 : /**
65 : * Function called with information about the post revocation operation result.
66 : *
67 : * @param cls closure with a `struct RevokeState *`
68 : * @param rdr response data
69 : */
70 : static void
71 0 : success_cb (
72 : void *cls,
73 : const struct TALER_EXCHANGE_ManagementRevokeDenominationResponse *rdr)
74 : {
75 0 : struct RevokeState *rs = cls;
76 0 : const struct TALER_EXCHANGE_HttpResponse *hr = &rdr->hr;
77 :
78 0 : rs->kh = NULL;
79 0 : if (rs->expected_response_code != hr->http_status)
80 : {
81 0 : TALER_TESTING_unexpected_status (rs->is,
82 : hr->http_status,
83 : rs->expected_response_code);
84 0 : return;
85 : }
86 0 : TALER_TESTING_interpreter_next (rs->is);
87 : }
88 :
89 :
90 : /**
91 : * Cleanup the state.
92 : *
93 : * @param cls closure, must be a `struct RevokeState`.
94 : * @param cmd the command which is being cleaned up.
95 : */
96 : static void
97 0 : revoke_cleanup (void *cls,
98 : const struct TALER_TESTING_Command *cmd)
99 : {
100 0 : struct RevokeState *rs = cls;
101 :
102 0 : if (NULL != rs->kh)
103 : {
104 0 : TALER_EXCHANGE_management_revoke_denomination_key_cancel (rs->kh);
105 0 : rs->kh = NULL;
106 : }
107 0 : GNUNET_free (rs);
108 0 : }
109 :
110 :
111 : /**
112 : * Offer internal data from a "revoke" CMD to other CMDs.
113 : *
114 : * @param cls closure
115 : * @param[out] ret result (could be anything)
116 : * @param trait name of the trait
117 : * @param index index number of the object to offer.
118 : * @return #GNUNET_OK on success
119 : */
120 : static int
121 0 : revoke_traits (void *cls,
122 : const void **ret,
123 : const char *trait,
124 : unsigned int index)
125 : {
126 0 : struct RevokeState *rs = cls;
127 : struct TALER_TESTING_Trait traits[] = {
128 0 : TALER_TESTING_trait_end ()
129 : };
130 :
131 : (void) rs;
132 0 : return TALER_TESTING_get_trait (traits,
133 : ret,
134 : trait,
135 : index);
136 : }
137 :
138 :
139 : /**
140 : * Run the "revoke" command for a denomination key.
141 : *
142 : * @param cls closure.
143 : * @param cmd the command to execute.
144 : * @param is the interpreter state.
145 : */
146 : static void
147 0 : revoke_run (void *cls,
148 : const struct TALER_TESTING_Command *cmd,
149 : struct TALER_TESTING_Interpreter *is)
150 : {
151 0 : struct RevokeState *rs = cls;
152 : const struct TALER_TESTING_Command *coin_cmd;
153 : const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
154 : struct TALER_MasterSignatureP master_sig;
155 : const char *exchange_url;
156 :
157 : {
158 : const struct TALER_TESTING_Command *exchange_cmd;
159 :
160 0 : exchange_cmd = TALER_TESTING_interpreter_get_command (is,
161 : "exchange");
162 0 : if (NULL == exchange_cmd)
163 : {
164 0 : GNUNET_break (0);
165 0 : TALER_TESTING_interpreter_fail (is);
166 0 : return;
167 : }
168 0 : GNUNET_assert (GNUNET_OK ==
169 : TALER_TESTING_get_trait_exchange_url (exchange_cmd,
170 : &exchange_url));
171 : }
172 0 : rs->is = is;
173 : /* Get denom pub from trait */
174 0 : coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
175 : rs->coin_reference);
176 :
177 0 : if (NULL == coin_cmd)
178 : {
179 0 : GNUNET_break (0);
180 0 : TALER_TESTING_interpreter_fail (is);
181 0 : return;
182 : }
183 0 : GNUNET_assert (GNUNET_OK ==
184 : TALER_TESTING_get_trait_denom_pub (coin_cmd,
185 : 0,
186 : &denom_pub));
187 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188 : "Trying to revoke denom '%s..'\n",
189 : TALER_B2S (&denom_pub->h_key));
190 0 : if (rs->bad_sig)
191 : {
192 0 : memset (&master_sig,
193 : 42,
194 : sizeof (master_sig));
195 : }
196 : else
197 : {
198 : const struct TALER_TESTING_Command *exchange_cmd;
199 : const struct TALER_MasterPrivateKeyP *master_priv;
200 :
201 0 : exchange_cmd = TALER_TESTING_interpreter_get_command (is,
202 : "exchange");
203 0 : if (NULL == exchange_cmd)
204 : {
205 0 : GNUNET_break (0);
206 0 : TALER_TESTING_interpreter_fail (is);
207 0 : return;
208 : }
209 0 : GNUNET_assert (GNUNET_OK ==
210 : TALER_TESTING_get_trait_master_priv (exchange_cmd,
211 : &master_priv));
212 0 : TALER_exchange_offline_denomination_revoke_sign (&denom_pub->h_key,
213 : master_priv,
214 : &master_sig);
215 : }
216 0 : rs->kh = TALER_EXCHANGE_management_revoke_denomination_key (
217 : TALER_TESTING_interpreter_get_context (is),
218 : exchange_url,
219 0 : &denom_pub->h_key,
220 : &master_sig,
221 : &success_cb,
222 : rs);
223 0 : if (NULL == rs->kh)
224 : {
225 0 : GNUNET_break (0);
226 0 : TALER_TESTING_interpreter_fail (is);
227 0 : return;
228 : }
229 : }
230 :
231 :
232 : struct TALER_TESTING_Command
233 0 : TALER_TESTING_cmd_revoke_denom_key (
234 : const char *label,
235 : unsigned int expected_response_code,
236 : bool bad_sig,
237 : const char *denom_ref)
238 : {
239 : struct RevokeState *rs;
240 :
241 0 : rs = GNUNET_new (struct RevokeState);
242 0 : rs->expected_response_code = expected_response_code;
243 0 : rs->coin_reference = denom_ref;
244 0 : rs->bad_sig = bad_sig;
245 : {
246 0 : struct TALER_TESTING_Command cmd = {
247 : .cls = rs,
248 : .label = label,
249 : .run = &revoke_run,
250 : .cleanup = &revoke_cleanup,
251 : .traits = &revoke_traits
252 : };
253 :
254 0 : return cmd;
255 : }
256 : }
|