merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

insert_token_family_key.c (5191B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 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 src/backenddb/insert_token_family_key.c
     18  * @brief Implementation of the insert_token_family_key function for Postgres
     19  * @author Christian Blättler
     20  */
     21 #include "platform.h"
     22 #include <gnunet/gnunet_common.h>
     23 #include <gnunet/gnunet_pq_lib.h>
     24 #include <taler/taler_error_codes.h>
     25 #include <taler/taler_dbevents.h>
     26 #include <taler/taler_pq_lib.h>
     27 #include "merchant-database/insert_token_family_key.h"
     28 #include "helper.h"
     29 
     30 
     31 enum GNUNET_DB_QueryStatus
     32 TALER_MERCHANTDB_insert_token_family_key (struct TALER_MERCHANTDB_PostgresContext *pg,
     33                                           const char *merchant_id,
     34                                           const char *token_family_slug,
     35                                           const struct TALER_TokenIssuePublicKey *pub,
     36                                           const struct TALER_TokenIssuePrivateKey *priv,
     37                                           struct GNUNET_TIME_Timestamp key_expires,
     38                                           struct GNUNET_TIME_Timestamp valid_after,
     39                                           struct GNUNET_TIME_Timestamp valid_before)
     40 {
     41   struct GNUNET_TIME_Timestamp now
     42     = GNUNET_TIME_timestamp_get ();
     43   const char *cipher = NULL;
     44 
     45 #if DEBUG
     46   struct GNUNET_HashCode pub_hash;
     47 
     48   switch (pub->public_key->cipher)
     49   {
     50   case GNUNET_CRYPTO_BSA_RSA:
     51     cipher = "rsa";
     52     GNUNET_CRYPTO_rsa_public_key_hash (
     53       pub->public_key->details.rsa_public_key,
     54       &pub_hash);
     55     break;
     56   case GNUNET_CRYPTO_BSA_CS:
     57     cipher = "cs";
     58     GNUNET_CRYPTO_hash (
     59       &pub->public_key->details.cs_public_key,
     60       sizeof (pub->public_key->details.cs_public_key),
     61       &pub_hash);
     62     break;
     63   case GNUNET_CRYPTO_BSA_INVALID:
     64     GNUNET_break (0);
     65     return GNUNET_DB_STATUS_HARD_ERROR;
     66   }
     67   GNUNET_assert (0 ==
     68                  GNUNET_memcmp (&pub_hash,
     69                                 &pub->public_key->pub_key_hash));
     70 #endif
     71   switch (pub->public_key->cipher)
     72   {
     73   case GNUNET_CRYPTO_BSA_RSA:
     74     cipher = "rsa";
     75     break;
     76   case GNUNET_CRYPTO_BSA_CS:
     77     cipher = "cs";
     78     break;
     79   case GNUNET_CRYPTO_BSA_INVALID:
     80     GNUNET_break (0);
     81     return GNUNET_DB_STATUS_HARD_ERROR;
     82   }
     83   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     84               "Storing token public key with hash %s\n",
     85               GNUNET_h2s (&pub->public_key->pub_key_hash));
     86   GNUNET_assert (pub->public_key->cipher ==
     87                  priv->private_key->cipher);
     88   GNUNET_assert (! GNUNET_TIME_absolute_is_zero (
     89                    valid_after.abs_time));
     90   GNUNET_assert (! GNUNET_TIME_absolute_is_zero (
     91                    valid_before.abs_time));
     92   PREPARE (pg,
     93            "token_family_key_insert",
     94            "INSERT INTO merchant_token_family_keys "
     95            "(token_family_serial"
     96            ",pub"
     97            ",h_pub"
     98            ",priv"
     99            ",private_key_created_at"
    100            ",private_key_deleted_at"
    101            ",signature_validity_start"
    102            ",signature_validity_end"
    103            ",cipher)"
    104            " SELECT token_family_serial, $2, $3, $4, $5, $6, $7, $8, $9"
    105            " FROM merchant_token_families"
    106            " WHERE (slug = $1)"
    107            "   AND merchant_serial="
    108            "   (SELECT merchant_serial"
    109            "      FROM merchant_instances"
    110            "     WHERE merchant_id=$10)");
    111   {
    112     struct GNUNET_PQ_QueryParam params[] = {
    113       GNUNET_PQ_query_param_string (token_family_slug),
    114       GNUNET_PQ_query_param_blind_sign_pub (pub->public_key),
    115       GNUNET_PQ_query_param_auto_from_type (&pub->public_key->pub_key_hash),
    116       GNUNET_PQ_query_param_blind_sign_priv (priv->private_key),
    117       GNUNET_PQ_query_param_timestamp (&now),
    118       GNUNET_PQ_query_param_timestamp (&key_expires),
    119       GNUNET_PQ_query_param_timestamp (&valid_after),
    120       GNUNET_PQ_query_param_timestamp (&valid_before),
    121       GNUNET_PQ_query_param_string (cipher),
    122       GNUNET_PQ_query_param_string (merchant_id),
    123       GNUNET_PQ_query_param_end
    124     };
    125     enum GNUNET_DB_QueryStatus qs;
    126 
    127     qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
    128                                              "token_family_key_insert",
    129                                              params);
    130     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    131                 "Insert into MTFK %s with valid [%llu,%llu] got %d\n",
    132                 token_family_slug,
    133                 (unsigned long long) valid_after.abs_time.abs_value_us,
    134                 (unsigned long long) valid_before.abs_time.abs_value_us,
    135                 (int) qs);
    136     return qs;
    137   }
    138 }