anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

testing_cmd_truth_upload.c (9332B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020 Anastasis SARL
      4 
      5   Anastasis 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   Anastasis 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   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file testing/testing_cmd_truth_upload.c
     18  * @brief command to execute the anastasis secret share service
     19  * @author Christian Grothoff
     20  * @author Dennis Neufeld
     21  * @author Dominik Meister
     22  */
     23 
     24 #include "platform.h"
     25 struct TruthUploadState;
     26 #define ANASTASIS_TRUTH_RESULT_CLOSURE struct TruthUploadState
     27 #include "anastasis_testing_lib.h"
     28 #include <taler/taler_util.h>
     29 #include <taler/taler_testing_lib.h>
     30 
     31 
     32 /**
     33  * State for a "truth upload" CMD.
     34  */
     35 struct TruthUploadState
     36 {
     37   /**
     38    * The interpreter state.
     39    */
     40   struct TALER_TESTING_Interpreter *is;
     41 
     42   /**
     43    * URL of the anastasis backend.
     44    */
     45   const char *anastasis_url;
     46 
     47   /**
     48    * Label of this command.
     49    */
     50   const char *label;
     51 
     52   /**
     53    * The ID data to generate user identifier
     54    */
     55   json_t *id_data;
     56 
     57   /**
     58    * The escrow method
     59    */
     60   const char *method;
     61 
     62   /**
     63    * Instructions to be returned to client/user
     64    * (e.g. "Look at your smartphone. SMS was sent to you")
     65    */
     66   const char *instructions;
     67 
     68   /**
     69    * Mime type of truth_data (eg. jpeg, string etc.)
     70    */
     71   const char *mime_type;
     72 
     73   /**
     74    * The truth_data (e.g. hash of answer to a secure question)
     75    */
     76   void *truth_data;
     77 
     78   /**
     79    * Requested order ID for this upload (if unpaid).
     80    */
     81   struct ANASTASIS_PaymentSecretP payment_secret_response;
     82 
     83   /**
     84    * Size of truth_data
     85    */
     86   size_t truth_data_size;
     87 
     88   /**
     89    * Expected status code.
     90    */
     91   unsigned int http_status;
     92 
     93   /**
     94    * The /truth POST operation handle.
     95    */
     96   struct ANASTASIS_TruthUpload *tuo;
     97 
     98   /**
     99    * closure for the payment callback
    100    */
    101   void *tpc_cls;
    102 
    103   /**
    104    * Reference to salt download.
    105    */
    106   const char *salt_reference;
    107 
    108   /**
    109    * Options for how we are supposed to do the upload.
    110    */
    111   enum ANASTASIS_TESTING_TruthStoreOption tsopt;
    112 
    113   /**
    114    * Truth object
    115    */
    116   struct ANASTASIS_Truth *truth;
    117 };
    118 
    119 
    120 /**
    121  * Upload information
    122  * caller MUST free 't' using ANASTASIS_truth_free()
    123  *
    124  * @param tus closure for callback
    125  * @param t Truth object (contains provider url and truth public key)
    126  * @param ud upload details, useful to continue in case of errors, NULL on success
    127  */
    128 static void
    129 truth_upload_cb (struct TruthUploadState *tus,
    130                  struct ANASTASIS_Truth *t,
    131                  const struct ANASTASIS_UploadDetails *ud)
    132 {
    133   tus->tuo = NULL;
    134   if (NULL == ud)
    135   {
    136     GNUNET_break (0);
    137     TALER_TESTING_interpreter_fail (tus->is);
    138     return;
    139   }
    140   if (ud->http_status != tus->http_status)
    141   {
    142     GNUNET_break (0);
    143     TALER_TESTING_interpreter_fail (tus->is);
    144     return;
    145   }
    146   if (MHD_HTTP_PAYMENT_REQUIRED == ud->http_status)
    147   {
    148     if (ANASTASIS_US_PAYMENT_REQUIRED != ud->us)
    149     {
    150       GNUNET_break (0);
    151       TALER_TESTING_interpreter_fail (tus->is);
    152       return;
    153     }
    154     tus->payment_secret_response = ud->details.payment.ps;
    155     TALER_TESTING_interpreter_next (tus->is);
    156     return;
    157   }
    158   if ( (ANASTASIS_US_SUCCESS == ud->us) &&
    159        (NULL == t) )
    160   {
    161     GNUNET_break (0);
    162     TALER_TESTING_interpreter_fail (tus->is);
    163     return;
    164   }
    165   tus->truth = t;
    166   TALER_TESTING_interpreter_next (tus->is);
    167 }
    168 
    169 
    170 /**
    171  * Run a "truth upload" CMD.
    172  *
    173  * @param cls closure.
    174  * @param cmd command currently being run.
    175  * @param is interpreter state.
    176  */
    177 static void
    178 truth_upload_run (void *cls,
    179                   const struct TALER_TESTING_Command *cmd,
    180                   struct TALER_TESTING_Interpreter *is)
    181 {
    182   struct TruthUploadState *tus = cls;
    183   const struct TALER_TESTING_Command *ref;
    184   const struct ANASTASIS_CRYPTO_ProviderSaltP *provider_salt;
    185   struct ANASTASIS_CRYPTO_UserIdentifierP user_id;
    186 
    187   tus->is = is;
    188   GNUNET_assert (NULL != tus->salt_reference);
    189   ref = TALER_TESTING_interpreter_lookup_command (
    190     is,
    191     tus->salt_reference);
    192   if (NULL == ref)
    193   {
    194     GNUNET_break (0);
    195     TALER_TESTING_interpreter_fail (tus->is);
    196     return;
    197   }
    198   if (GNUNET_OK !=
    199       ANASTASIS_TESTING_get_trait_provider_salt (ref,
    200                                                  &provider_salt))
    201   {
    202     GNUNET_break (0);
    203     TALER_TESTING_interpreter_fail (tus->is);
    204     return;
    205   }
    206   ANASTASIS_CRYPTO_user_identifier_derive (tus->id_data,
    207                                            provider_salt,
    208                                            &user_id);
    209   tus->tuo = ANASTASIS_truth_upload (
    210     TALER_TESTING_interpreter_get_context (is),
    211     &user_id,
    212     tus->anastasis_url,
    213     tus->method,
    214     tus->instructions,
    215     tus->mime_type,
    216     provider_salt,
    217     tus->truth_data,
    218     tus->truth_data_size,
    219     false,                                  /* force payment */
    220     GNUNET_TIME_UNIT_ZERO,
    221     &truth_upload_cb,
    222     tus);
    223   if (NULL == tus->tuo)
    224   {
    225     GNUNET_break (0);
    226     TALER_TESTING_interpreter_fail (tus->is);
    227   }
    228 }
    229 
    230 
    231 /**
    232  * Free the state of a "truth upload" CMD, and possibly
    233  * cancel it if it did not complete.
    234  *
    235  * @param cls closure.
    236  * @param cmd command being freed.
    237  */
    238 static void
    239 truth_upload_cleanup (void *cls,
    240                       const struct TALER_TESTING_Command *cmd)
    241 {
    242   struct TruthUploadState *tus = cls;
    243 
    244   if (NULL != tus->tuo)
    245   {
    246     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    247                 "Command '%s' did not complete\n",
    248                 cmd->label);
    249     ANASTASIS_truth_upload_cancel (tus->tuo);
    250     tus->tuo = NULL;
    251   }
    252   if (NULL != tus->id_data)
    253   {
    254     json_decref (tus->id_data);
    255     tus->id_data = NULL;
    256   }
    257   if (NULL != tus->truth)
    258   {
    259     ANASTASIS_truth_free (tus->truth);
    260     tus->truth = NULL;
    261   }
    262   GNUNET_free (tus->truth_data);
    263   GNUNET_free (tus);
    264 }
    265 
    266 
    267 /**
    268  * Offer internal data to other commands.
    269  *
    270  * @param cls closure
    271  * @param[out] ret result (could be anything)
    272  * @param trait name of the trait
    273  * @param index index number of the object to extract.
    274  * @return #GNUNET_OK on success
    275  */
    276 static int
    277 truth_upload_traits (void *cls,
    278                      const void **ret,
    279                      const char *trait,
    280                      unsigned int index)
    281 {
    282   struct TruthUploadState *tus = cls;
    283   struct TALER_TESTING_Trait traits[] = {
    284     ANASTASIS_TESTING_make_trait_truth (
    285       (const struct ANASTASIS_Truth **) &tus->truth),
    286     ANASTASIS_TESTING_make_trait_payment_secret (&tus->payment_secret_response),
    287     TALER_TESTING_trait_end ()
    288   };
    289 
    290   return TALER_TESTING_get_trait (traits,
    291                                   ret,
    292                                   trait,
    293                                   index);
    294 }
    295 
    296 
    297 json_t *
    298 ANASTASIS_TESTING_make_id_data_example (const char *id_data)
    299 {
    300   return GNUNET_JSON_PACK (
    301     GNUNET_JSON_pack_string ("id_data",
    302                              id_data));
    303 }
    304 
    305 
    306 struct TALER_TESTING_Command
    307 ANASTASIS_TESTING_cmd_truth_upload (
    308   const char *label,
    309   const char *anastasis_url,
    310   const json_t *id_data,
    311   const char *method,
    312   const char *instructions,
    313   const char *mime_type,
    314   const void *truth_data,
    315   size_t truth_data_size,
    316   unsigned int http_status,
    317   enum ANASTASIS_TESTING_TruthStoreOption tso,
    318   const char *salt_ref)
    319 {
    320   struct TruthUploadState *tus;
    321 
    322   tus = GNUNET_new (struct TruthUploadState);
    323   tus->label = label;
    324   tus->http_status = http_status;
    325   tus->tsopt = tso;
    326   tus->anastasis_url = anastasis_url;
    327   tus->salt_reference = salt_ref;
    328   tus->id_data = json_incref ((json_t *) id_data);
    329   tus->method = method;
    330   tus->instructions = instructions;
    331   tus->mime_type = mime_type;
    332   tus->truth_data_size = truth_data_size;
    333   tus->truth_data = GNUNET_memdup (truth_data,
    334                                    truth_data_size);
    335   {
    336     struct TALER_TESTING_Command cmd = {
    337       .cls = tus,
    338       .label = label,
    339       .run = &truth_upload_run,
    340       .cleanup = &truth_upload_cleanup,
    341       .traits = &truth_upload_traits
    342     };
    343 
    344     return cmd;
    345   }
    346 }
    347 
    348 
    349 struct TALER_TESTING_Command
    350 ANASTASIS_TESTING_cmd_truth_upload_question (
    351   const char *label,
    352   const char *anastasis_url,
    353   const json_t *id_data,
    354   const char *instructions,
    355   const char *mime_type,
    356   const void *answer,
    357   unsigned int http_status,
    358   enum ANASTASIS_TESTING_TruthStoreOption tso,
    359   const char *salt_ref)
    360 {
    361   return ANASTASIS_TESTING_cmd_truth_upload (label,
    362                                              anastasis_url,
    363                                              id_data,
    364                                              "question",
    365                                              instructions,
    366                                              mime_type,
    367                                              answer,
    368                                              strlen (answer),
    369                                              http_status,
    370                                              tso,
    371                                              salt_ref);
    372 }
    373 
    374 
    375 /* end of testing_cmd_truth_upload.c */