LCOV - code coverage report
Current view: top level - json - json_wire.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 39 57 68.4 %
Date: 2021-08-30 06:43:37 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018 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             : /**
      28             :  * Compute the hash of the given wire details.   The resulting
      29             :  * hash is what is put into the contract.
      30             :  *
      31             :  * @param wire_s wire details to hash
      32             :  * @param[out] hc set to the hash
      33             :  * @return #GNUNET_OK on success, #GNUNET_SYSERR if @a wire_s is malformed
      34             :  */
      35             : int
      36         481 : TALER_JSON_merchant_wire_signature_hash (const json_t *wire_s,
      37             :                                          struct GNUNET_HashCode *hc)
      38             : {
      39             :   const char *payto_uri;
      40             :   const char *salt; /* Current merchant backend will always make the salt
      41             :                        a `struct GNUNET_HashCode`, but *we* do not insist
      42             :                        on that. */
      43             :   struct GNUNET_JSON_Specification spec[] = {
      44         481 :     GNUNET_JSON_spec_string ("payto_uri", &payto_uri),
      45         481 :     GNUNET_JSON_spec_string ("salt", &salt),
      46         481 :     GNUNET_JSON_spec_end ()
      47             :   };
      48             : 
      49         481 :   if (GNUNET_OK !=
      50         481 :       GNUNET_JSON_parse (wire_s,
      51             :                          spec,
      52             :                          NULL, NULL))
      53             :   {
      54           1 :     GNUNET_break_op (0);
      55           1 :     return GNUNET_SYSERR;
      56             :   }
      57         480 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      58             :               "Validating `%s'\n",
      59             :               payto_uri);
      60             :   {
      61             :     char *err;
      62             : 
      63         480 :     err = TALER_payto_validate (payto_uri);
      64         480 :     if (NULL != err)
      65             :     {
      66           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      67             :                   "URI `%s' ill-formed: %s\n",
      68             :                   payto_uri,
      69             :                   err);
      70           0 :       GNUNET_free (err);
      71           0 :       return GNUNET_SYSERR;
      72             :     }
      73             :   }
      74         480 :   TALER_merchant_wire_signature_hash (payto_uri,
      75             :                                       salt,
      76             :                                       hc);
      77         480 :   return GNUNET_OK;
      78             : }
      79             : 
      80             : 
      81             : /**
      82             :  * Check the signature in @a wire_s.  Also performs rudimentary
      83             :  * checks on the account data *if* supported.
      84             :  *
      85             :  * @param wire_s signed wire information of an exchange
      86             :  * @param master_pub master public key of the exchange
      87             :  * @return #GNUNET_OK if signature is valid
      88             :  */
      89             : int
      90           4 : TALER_JSON_exchange_wire_signature_check (
      91             :   const json_t *wire_s,
      92             :   const struct TALER_MasterPublicKeyP *master_pub)
      93             : {
      94             :   const char *payto_uri;
      95             :   struct TALER_MasterSignatureP master_sig;
      96             :   struct GNUNET_JSON_Specification spec[] = {
      97           4 :     GNUNET_JSON_spec_string ("payto_uri", &payto_uri),
      98           4 :     GNUNET_JSON_spec_fixed_auto ("master_sig", &master_sig),
      99           4 :     GNUNET_JSON_spec_end ()
     100             :   };
     101             : 
     102           4 :   if (GNUNET_OK !=
     103           4 :       GNUNET_JSON_parse (wire_s,
     104             :                          spec,
     105             :                          NULL, NULL))
     106             :   {
     107           0 :     GNUNET_break_op (0);
     108           0 :     return GNUNET_SYSERR;
     109             :   }
     110             : 
     111             :   {
     112             :     char *err;
     113             : 
     114           4 :     err = TALER_payto_validate (payto_uri);
     115           4 :     if (NULL != err)
     116             :     {
     117           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     118             :                   "URI `%s' ill-formed: %s\n",
     119             :                   payto_uri,
     120             :                   err);
     121           0 :       GNUNET_free (err);
     122           0 :       return GNUNET_SYSERR;
     123             :     }
     124             :   }
     125             : 
     126           4 :   return TALER_exchange_wire_signature_check (payto_uri,
     127             :                                               master_pub,
     128             :                                               &master_sig);
     129             : }
     130             : 
     131             : 
     132             : /**
     133             :  * Create a signed wire statement for the given account.
     134             :  *
     135             :  * @param payto_uri account specification
     136             :  * @param master_priv private key to sign with
     137             :  * @return NULL if @a payto_uri is malformed
     138             :  */
     139             : json_t *
     140           2 : TALER_JSON_exchange_wire_signature_make (
     141             :   const char *payto_uri,
     142             :   const struct TALER_MasterPrivateKeyP *master_priv)
     143             : {
     144             :   struct TALER_MasterSignatureP master_sig;
     145             :   char *err;
     146             : 
     147           2 :   if (NULL !=
     148           2 :       (err = TALER_payto_validate (payto_uri)))
     149             :   {
     150           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     151             :                 "Invalid payto URI `%s': %s\n",
     152             :                 payto_uri,
     153             :                 err);
     154           0 :     GNUNET_free (err);
     155           0 :     return NULL;
     156             :   }
     157           2 :   TALER_exchange_wire_signature_make (payto_uri,
     158             :                                       master_priv,
     159             :                                       &master_sig);
     160           2 :   return GNUNET_JSON_PACK (
     161             :     GNUNET_JSON_pack_string ("payto_uri",
     162             :                              payto_uri),
     163             :     GNUNET_JSON_pack_data_auto ("master_sig",
     164             :                                 &master_sig));
     165             : }
     166             : 
     167             : 
     168             : /**
     169             :  * Obtain the wire method associated with the given
     170             :  * wire account details.  @a wire_s must contain a payto://-URL
     171             :  * under 'payto_uri'.
     172             :  *
     173             :  * @return NULL on error
     174             :  */
     175             : char *
     176         168 : TALER_JSON_wire_to_payto (const json_t *wire_s)
     177             : {
     178             :   json_t *payto_o;
     179             :   const char *payto_str;
     180             :   char *err;
     181             : 
     182         168 :   payto_o = json_object_get (wire_s,
     183             :                              "payto_uri");
     184         336 :   if ( (NULL == payto_o) ||
     185         168 :        (NULL == (payto_str = json_string_value (payto_o))) )
     186             :   {
     187           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     188             :                 "Malformed wire record encountered: lacks payto://-url\n");
     189           0 :     return NULL;
     190             :   }
     191         168 :   if (NULL !=
     192         168 :       (err = TALER_payto_validate (payto_str)))
     193             :   {
     194           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     195             :                 "Malformed wire record encountered: payto URI `%s' invalid: %s\n",
     196             :                 payto_str,
     197             :                 err);
     198           0 :     GNUNET_free (err);
     199           0 :     return NULL;
     200             :   }
     201         168 :   return GNUNET_strdup (payto_str);
     202             : }
     203             : 
     204             : 
     205             : /**
     206             :  * Obtain the wire method associated with the given
     207             :  * wire account details.  @a wire_s must contain a payto://-URL
     208             :  * under 'url'.
     209             :  *
     210             :  * @return NULL on error
     211             :  */
     212             : char *
     213          32 : TALER_JSON_wire_to_method (const json_t *wire_s)
     214             : {
     215             :   json_t *payto_o;
     216             :   const char *payto_str;
     217             : 
     218          32 :   payto_o = json_object_get (wire_s,
     219             :                              "payto_uri");
     220          64 :   if ( (NULL == payto_o) ||
     221          32 :        (NULL == (payto_str = json_string_value (payto_o))) )
     222             :   {
     223           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     224             :                 "Fatally malformed wire record encountered: lacks payto://-url\n");
     225           0 :     return NULL;
     226             :   }
     227          32 :   return TALER_payto_get_method (payto_str);
     228             : }
     229             : 
     230             : 
     231             : /* end of json_wire.c */

Generated by: LCOV version 1.14