exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_revoke_sign_key.c (6616B)


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