LCOV - code coverage report
Current view: top level - json - json_wire.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 21 57 36.8 %
Date: 2022-08-25 06:15:09 Functions: 3 5 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018, 2021 Taler Systems SA
       4             : 
       5             :   TALER 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             :   TALER 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             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file json/json_wire.c
      18             :  * @brief helper functions to generate or check /wire replies
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include "taler_util.h"
      24             : #include "taler_json_lib.h"
      25             : 
      26             : 
      27             : enum GNUNET_GenericReturnValue
      28           0 : TALER_JSON_merchant_wire_signature_hash (const json_t *wire_s,
      29             :                                          struct TALER_MerchantWireHashP *hc)
      30             : {
      31             :   const char *payto_uri;
      32             :   struct TALER_WireSaltP salt;
      33             :   struct GNUNET_JSON_Specification spec[] = {
      34           0 :     GNUNET_JSON_spec_string ("payto_uri",
      35             :                              &payto_uri),
      36           0 :     GNUNET_JSON_spec_fixed_auto ("salt",
      37             :                                  &salt),
      38           0 :     GNUNET_JSON_spec_end ()
      39             :   };
      40             : 
      41           0 :   if (GNUNET_OK !=
      42           0 :       GNUNET_JSON_parse (wire_s,
      43             :                          spec,
      44             :                          NULL, NULL))
      45             :   {
      46           0 :     GNUNET_break_op (0);
      47           0 :     return GNUNET_SYSERR;
      48             :   }
      49           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      50             :               "Validating `%s'\n",
      51             :               payto_uri);
      52             :   {
      53             :     char *err;
      54             : 
      55           0 :     err = TALER_payto_validate (payto_uri);
      56           0 :     if (NULL != err)
      57             :     {
      58           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      59             :                   "URI `%s' ill-formed: %s\n",
      60             :                   payto_uri,
      61             :                   err);
      62           0 :       GNUNET_free (err);
      63           0 :       return GNUNET_SYSERR;
      64             :     }
      65             :   }
      66           0 :   TALER_merchant_wire_signature_hash (payto_uri,
      67             :                                       &salt,
      68             :                                       hc);
      69           0 :   return GNUNET_OK;
      70             : }
      71             : 
      72             : 
      73             : enum GNUNET_GenericReturnValue
      74           2 : TALER_JSON_exchange_wire_signature_check (
      75             :   const json_t *wire_s,
      76             :   const struct TALER_MasterPublicKeyP *master_pub)
      77             : {
      78             :   const char *payto_uri;
      79             :   struct TALER_MasterSignatureP master_sig;
      80             :   struct GNUNET_JSON_Specification spec[] = {
      81           2 :     GNUNET_JSON_spec_string ("payto_uri",
      82             :                              &payto_uri),
      83           2 :     GNUNET_JSON_spec_fixed_auto ("master_sig",
      84             :                                  &master_sig),
      85           2 :     GNUNET_JSON_spec_end ()
      86             :   };
      87             : 
      88           2 :   if (GNUNET_OK !=
      89           2 :       GNUNET_JSON_parse (wire_s,
      90             :                          spec,
      91             :                          NULL, NULL))
      92             :   {
      93           0 :     GNUNET_break_op (0);
      94           0 :     return GNUNET_SYSERR;
      95             :   }
      96             : 
      97             :   {
      98             :     char *err;
      99             : 
     100           2 :     err = TALER_payto_validate (payto_uri);
     101           2 :     if (NULL != err)
     102             :     {
     103           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     104             :                   "URI `%s' ill-formed: %s\n",
     105             :                   payto_uri,
     106             :                   err);
     107           0 :       GNUNET_free (err);
     108           0 :       return GNUNET_SYSERR;
     109             :     }
     110             :   }
     111             : 
     112           2 :   return TALER_exchange_wire_signature_check (payto_uri,
     113             :                                               master_pub,
     114             :                                               &master_sig);
     115             : }
     116             : 
     117             : 
     118             : json_t *
     119           2 : TALER_JSON_exchange_wire_signature_make (
     120             :   const char *payto_uri,
     121             :   const struct TALER_MasterPrivateKeyP *master_priv)
     122             : {
     123             :   struct TALER_MasterSignatureP master_sig;
     124             :   char *err;
     125             : 
     126           2 :   if (NULL !=
     127           2 :       (err = TALER_payto_validate (payto_uri)))
     128             :   {
     129           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                 "Invalid payto URI `%s': %s\n",
     131             :                 payto_uri,
     132             :                 err);
     133           0 :     GNUNET_free (err);
     134           0 :     return NULL;
     135             :   }
     136           2 :   TALER_exchange_wire_signature_make (payto_uri,
     137             :                                       master_priv,
     138             :                                       &master_sig);
     139           2 :   return GNUNET_JSON_PACK (
     140             :     GNUNET_JSON_pack_string ("payto_uri",
     141             :                              payto_uri),
     142             :     GNUNET_JSON_pack_data_auto ("master_sig",
     143             :                                 &master_sig));
     144             : }
     145             : 
     146             : 
     147             : char *
     148           2 : TALER_JSON_wire_to_payto (const json_t *wire_s)
     149             : {
     150             :   json_t *payto_o;
     151             :   const char *payto_str;
     152             :   char *err;
     153             : 
     154           2 :   payto_o = json_object_get (wire_s,
     155             :                              "payto_uri");
     156           4 :   if ( (NULL == payto_o) ||
     157           2 :        (NULL == (payto_str = json_string_value (payto_o))) )
     158             :   {
     159           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     160             :                 "Malformed wire record encountered: lacks payto://-url\n");
     161           0 :     return NULL;
     162             :   }
     163           2 :   if (NULL !=
     164           2 :       (err = TALER_payto_validate (payto_str)))
     165             :   {
     166           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167             :                 "Malformed wire record encountered: payto URI `%s' invalid: %s\n",
     168             :                 payto_str,
     169             :                 err);
     170           0 :     GNUNET_free (err);
     171           0 :     return NULL;
     172             :   }
     173           2 :   return GNUNET_strdup (payto_str);
     174             : }
     175             : 
     176             : 
     177             : char *
     178           0 : TALER_JSON_wire_to_method (const json_t *wire_s)
     179             : {
     180             :   json_t *payto_o;
     181             :   const char *payto_str;
     182             : 
     183           0 :   payto_o = json_object_get (wire_s,
     184             :                              "payto_uri");
     185           0 :   if ( (NULL == payto_o) ||
     186           0 :        (NULL == (payto_str = json_string_value (payto_o))) )
     187             :   {
     188           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     189             :                 "Fatally malformed wire record encountered: lacks payto://-url\n");
     190           0 :     return NULL;
     191             :   }
     192           0 :   return TALER_payto_get_method (payto_str);
     193             : }
     194             : 
     195             : 
     196             : /* end of json_wire.c */

Generated by: LCOV version 1.14