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 (6605B)


      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/platform.h"
     25 #include "taler/taler_json_lib.h"
     26 #include <gnunet/gnunet_curl_lib.h>
     27 #include "taler/taler_signatures.h"
     28 #include "taler/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 signination 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_ManagementRevokeSigningKeyHandle *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 rsr response data
     69  */
     70 static void
     71 success_cb (
     72   void *cls,
     73   const struct TALER_EXCHANGE_ManagementRevokeSigningKeyResponse *rsr)
     74 {
     75   struct RevokeState *rs = cls;
     76   const struct TALER_EXCHANGE_HttpResponse *hr = &rsr->hr;
     77 
     78   rs->kh = NULL;
     79   if (rs->expected_response_code != hr->http_status)
     80   {
     81     TALER_TESTING_unexpected_status (rs->is,
     82                                      hr->http_status,
     83                                      rs->expected_response_code);
     84     return;
     85   }
     86   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 revoke_cleanup (void *cls,
     98                 const struct TALER_TESTING_Command *cmd)
     99 {
    100   struct RevokeState *rs = cls;
    101 
    102   if (NULL != rs->kh)
    103   {
    104     TALER_EXCHANGE_management_revoke_signing_key_cancel (rs->kh);
    105     rs->kh = NULL;
    106   }
    107   GNUNET_free (rs);
    108 }
    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 revoke_traits (void *cls,
    122                const void **ret,
    123                const char *trait,
    124                unsigned int index)
    125 {
    126   struct RevokeState *rs = cls;
    127   struct TALER_TESTING_Trait traits[] = {
    128     TALER_TESTING_trait_end ()
    129   };
    130 
    131   (void) rs;
    132   return TALER_TESTING_get_trait (traits,
    133                                   ret,
    134                                   trait,
    135                                   index);
    136 }
    137 
    138 
    139 /**
    140  * Run the "revoke" command for a signing key.
    141  *
    142  * @param cls closure.
    143  * @param cmd the command to execute.
    144  * @param is the interpreter state.
    145  */
    146 static void
    147 revoke_run (void *cls,
    148             const struct TALER_TESTING_Command *cmd,
    149             struct TALER_TESTING_Interpreter *is)
    150 {
    151   struct RevokeState *rs = cls;
    152   const struct TALER_TESTING_Command *coin_cmd;
    153   const struct TALER_ExchangePublicKeyP *exchange_pub;
    154   struct TALER_MasterSignatureP master_sig;
    155   const char *exchange_url;
    156 
    157   {
    158     const struct TALER_TESTING_Command *exchange_cmd;
    159 
    160     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
    161                                                           "exchange");
    162     if (NULL == exchange_cmd)
    163     {
    164       GNUNET_break (0);
    165       TALER_TESTING_interpreter_fail (is);
    166       return;
    167     }
    168     GNUNET_assert (GNUNET_OK ==
    169                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
    170                                                          &exchange_url));
    171   }
    172   rs->is = is;
    173   /* Get sign pub from trait */
    174   coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
    175                                                        rs->coin_reference);
    176 
    177   if (NULL == coin_cmd)
    178   {
    179     GNUNET_break (0);
    180     TALER_TESTING_interpreter_fail (is);
    181     return;
    182   }
    183   GNUNET_assert (GNUNET_OK ==
    184                  TALER_TESTING_get_trait_exchange_pub (coin_cmd,
    185                                                        0,
    186                                                        &exchange_pub));
    187   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    188               "Trying to revoke sign '%s..'\n",
    189               TALER_B2S (exchange_pub));
    190   if (rs->bad_sig)
    191   {
    192     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     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
    202                                                           "exchange");
    203     if (NULL == exchange_cmd)
    204     {
    205       GNUNET_break (0);
    206       TALER_TESTING_interpreter_fail (is);
    207       return;
    208     }
    209     GNUNET_assert (GNUNET_OK ==
    210                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
    211                                                         &master_priv));
    212     TALER_exchange_offline_signkey_revoke_sign (exchange_pub,
    213                                                 master_priv,
    214                                                 &master_sig);
    215   }
    216   rs->kh = TALER_EXCHANGE_management_revoke_signing_key (
    217     TALER_TESTING_interpreter_get_context (is),
    218     exchange_url,
    219     exchange_pub,
    220     &master_sig,
    221     &success_cb,
    222     rs);
    223   if (NULL == rs->kh)
    224   {
    225     GNUNET_break (0);
    226     TALER_TESTING_interpreter_fail (is);
    227     return;
    228   }
    229 }
    230 
    231 
    232 struct TALER_TESTING_Command
    233 TALER_TESTING_cmd_revoke_sign_key (
    234   const char *label,
    235   unsigned int expected_response_code,
    236   bool bad_sig,
    237   const char *sign_ref)
    238 {
    239   struct RevokeState *rs;
    240 
    241   rs = GNUNET_new (struct RevokeState);
    242   rs->expected_response_code = expected_response_code;
    243   rs->coin_reference = sign_ref;
    244   rs->bad_sig = bad_sig;
    245   {
    246     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     return cmd;
    255   }
    256 }