frosix_api_delete-key.c (7935B)
1 /* 2 This file is part of Frosix 3 Copyright (C) 2020, 2021 Anastasis SARL 4 5 Frosix 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 Frosix 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 Frosix; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file reducer/frosix_api_delete-key.c 18 * @brief frosix reducer key delete api 19 * @author Christian Grothoff 20 * @author Dominik Meister 21 * @author Dennis Neufeld 22 * @author Joel Urech 23 */ 24 25 #include "platform.h" 26 #include "frost_verify.h" 27 #include "frosix.h" 28 #include "frosix_api.h" 29 #include <taler/taler_merchant_service.h> 30 31 /** 32 * FIXME 33 */ 34 struct FROSIX_KeyDeleteProvider 35 { 36 /** 37 * URL of the provider. 38 */ 39 char *backend_url; 40 41 /** 42 * Encryption key 43 */ 44 struct FROSIX_EncryptionKey enc_key; 45 }; 46 47 /** 48 * FIXME 49 */ 50 struct FROSIX_KeyDeleteData 51 { 52 /** 53 * Pointer to a list of providers, length is @e num_of_providers 54 */ 55 struct FROSIX_KeyDeleteProvider *providers; 56 57 /** 58 * How many providers do we have? 59 */ 60 uint8_t num_of_providers; 61 }; 62 63 64 /** 65 * State of a sign procedure 66 */ 67 struct FROSIX_KeyDeleteState 68 { 69 /** 70 * FIXME 71 */ 72 struct FROSIX_KeyDeleteData *key_del_data; 73 74 /** 75 * Function to call when we are done. 76 */ 77 FROSIX_ActionCallback cb; 78 79 /** 80 * Closure for @e cb. 81 */ 82 void *cb_cls; 83 84 /** 85 * Redux action we returned to our controller. 86 */ 87 struct FROSIX_ReduxAction ra; 88 89 /** 90 * Number of provider /config operations in @e ba_head that 91 * are still awaiting completion. 92 */ 93 uint8_t counter; 94 95 /** 96 * Last error code 97 */ 98 enum TALER_ErrorCode error_code; 99 100 /** 101 * Array of config requests, with length `num_of_participants` 102 */ 103 struct FROSIX_KeyDeleteOperation **kdo; 104 }; 105 106 107 /** 108 * Function to free all initialized memory of a provider from a 109 * `struct FROSIX_KeyDeleteProvider` 110 * 111 * @param fp the struct to clean up 112 */ 113 static void 114 free_provider_struct (struct FROSIX_KeyDeleteProvider *kp) 115 { 116 if (NULL != kp->backend_url) 117 GNUNET_free (kp->backend_url); 118 } 119 120 121 /** 122 * Function to free all initialized memory of a provider from a 123 * `struct FROSIX_KeyDeleteData` 124 * 125 * @param dd the struct to clean up 126 */ 127 static void 128 free_data_struct (struct FROSIX_KeyDeleteData *dd) 129 { 130 if (NULL != dd->providers) 131 { 132 for (unsigned int i = 0; i < dd->num_of_providers; i++) 133 { 134 if (NULL != &dd->providers[i]) 135 free_provider_struct (&dd->providers[i]); 136 } 137 138 GNUNET_free (dd->providers); 139 } 140 141 GNUNET_free (dd); 142 } 143 144 145 /** 146 * Function called when signature process is being aborted. 147 * Frees all initialized memory! 148 * 149 * @param cls a `struct FROSIX_KeyDeleteState` 150 */ 151 static void 152 key_delete_cancel_cb (void *cls) 153 { 154 struct FROSIX_KeyDeleteState *kds = cls; 155 156 if (NULL != kds->key_del_data) 157 free_data_struct (kds->key_del_data); 158 159 /* free operations */ 160 if (NULL != kds->kdo) 161 GNUNET_free (kds->kdo); 162 163 GNUNET_free (kds); 164 } 165 166 167 /** 168 * Callback to process a POST /sig-commitment request 169 * 170 * @param cls closure 171 * @param scd the decoded response body 172 */ 173 static void 174 key_delete_request_cb ( 175 void *cls, 176 const struct FROSIX_KeyDeleteDetails *kdd) 177 { 178 GNUNET_assert (NULL != cls); 179 struct FROSIX_KeyDeleteState *kds = cls; 180 181 /* set error code */ 182 kds->error_code = kdd->ec; 183 184 /* check if this is a valid callback */ 185 GNUNET_assert (0 < kds->counter); 186 GNUNET_assert (0 != kdd->http_status); 187 188 /* call next dkg-commitment request */ 189 kds->counter--; 190 if (0 == kds->counter) 191 { 192 /* we are the last, go ahead with sig-share */ 193 free_data_struct (kds->key_del_data); 194 195 kds->cb (kds->cb_cls, 196 kds->error_code, 197 NULL); 198 } 199 } 200 201 202 /** 203 * FIXME 204 */ 205 static void 206 start_key_delete (struct FROSIX_KeyDeleteState *kds) 207 { 208 kds->kdo = GNUNET_malloc ( 209 kds->key_del_data->num_of_providers * sizeof (char *)); 210 211 kds->counter = 0; 212 for (unsigned int i = 0; i < kds->key_del_data->num_of_providers; i++) 213 { 214 struct FROSIX_KeyDeleteProvider *kp = &kds->key_del_data->providers[i]; 215 216 /* hash encryption key to get request id*/ 217 struct FROSIX_DkgRequestIdP request_id; 218 FROSIX_hash_encryption_key (&request_id.id, 219 &kp->enc_key); 220 221 kds->kdo[i] = FROSIX_key_delete ( 222 FROSIX_REDUX_ctx_, 223 kp->backend_url, 224 &request_id, 225 &key_delete_request_cb, 226 kds); 227 228 kds->counter++; 229 } 230 } 231 232 233 /** 234 * Helper function to parse a keygen input 235 * 236 * @param[in,out] kdd A initialized struct 237 * @param[in] input Input from the cli 238 */ 239 enum GNUNET_GenericReturnValue 240 parse_key_delete_input (struct FROSIX_KeyDeleteData *kdd, 241 const json_t *input) 242 { 243 json_t *providers = NULL; 244 245 struct GNUNET_JSON_Specification spec[] = { 246 GNUNET_JSON_spec_uint8 ("number_of_participants", 247 &kdd->num_of_providers), 248 GNUNET_JSON_spec_json ("providers", 249 &providers), 250 GNUNET_JSON_spec_end () 251 }; 252 253 if (GNUNET_OK != GNUNET_JSON_parse (input, 254 spec, 255 NULL, 256 NULL)) 257 { 258 GNUNET_break (0); 259 GNUNET_JSON_parse_free (spec); 260 return GNUNET_NO; 261 } 262 263 /* get number of providers */ 264 size_t parsed_providers_len = json_array_size (providers); 265 266 /* validate number of providers */ 267 GNUNET_assert (kdd->num_of_providers == parsed_providers_len); 268 GNUNET_assert (254 > kdd->num_of_providers); 269 GNUNET_assert (1 < kdd->num_of_providers); 270 271 /* initialize providers */ 272 kdd->providers = GNUNET_new_array (kdd->num_of_providers, 273 struct FROSIX_KeyDeleteProvider); 274 275 /* parse provider data */ 276 for (unsigned int i = 0; i < kdd->num_of_providers; i++) 277 { 278 struct GNUNET_JSON_Specification prov_spec[] = { 279 GNUNET_JSON_spec_string ("backend_url", 280 (const 281 char**) &kdd->providers[i].backend_url), 282 GNUNET_JSON_spec_fixed_auto ("encryption_key", 283 &kdd->providers[i].enc_key), 284 GNUNET_JSON_spec_end () 285 }; 286 287 if (GNUNET_OK != GNUNET_JSON_parse (json_array_get (providers, 288 i), 289 prov_spec, 290 NULL, 291 NULL)) 292 { 293 GNUNET_break (0); 294 GNUNET_JSON_parse_free (prov_spec); 295 GNUNET_JSON_parse_free (spec); 296 return GNUNET_NO; 297 } 298 299 GNUNET_JSON_parse_free (prov_spec); 300 } 301 302 GNUNET_JSON_parse_free (spec); 303 304 return GNUNET_OK; 305 } 306 307 /** 308 * 309 */ 310 struct FROSIX_ReduxAction * 311 FROSIX_redux_key_delete_start (json_t *input, 312 FROSIX_ActionCallback cb, 313 void *cb_cls) 314 { 315 struct FROSIX_KeyDeleteData *key_del_data = GNUNET_new (struct 316 FROSIX_KeyDeleteData); 317 318 /* parse arguments from cli */ 319 if (GNUNET_OK != parse_key_delete_input (key_del_data, 320 input)) 321 { 322 free_data_struct (key_del_data); 323 324 // FIXME: Return some useful error message 325 return NULL; 326 } 327 328 struct FROSIX_KeyDeleteState *kds = GNUNET_new (struct FROSIX_KeyDeleteState); 329 330 kds->cb = cb; 331 kds->cb_cls = cb_cls; 332 kds->key_del_data = key_del_data; 333 kds->ra.cleanup = &key_delete_cancel_cb; 334 kds->ra.cleanup_cls = kds; 335 336 /* get commitments from all parsed providers */ 337 start_key_delete (kds); 338 339 return &kds->ra; 340 }