exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

pg_iterate_active_signkeys.c (4559B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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 exchangedb/pg_iterate_active_signkeys.c
     18  * @brief Implementation of the iterate_active_signkeys function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_iterate_active_signkeys.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #signkeys_cb_helper()
     31  */
     32 struct SignkeysIteratorContext
     33 {
     34   /**
     35    * Function to call with the results.
     36    */
     37   TALER_EXCHANGEDB_ActiveSignkeysCallback cb;
     38 
     39   /**
     40    * Closure to pass to @e cb
     41    */
     42   void *cb_cls;
     43 
     44 };
     45 
     46 
     47 /**
     48  * Helper function for #TEH_PG_iterate_active_signkeys().
     49  * Calls the callback with each signkey.
     50  *
     51  * @param cls a `struct SignkeysIteratorContext`
     52  * @param result db results
     53  * @param num_results number of results in @a result
     54  */
     55 static void
     56 signkeys_cb_helper (void *cls,
     57                     PGresult *result,
     58                     unsigned int num_results)
     59 {
     60   struct SignkeysIteratorContext *dic = cls;
     61 
     62   for (unsigned int i = 0; i<num_results; i++)
     63   {
     64     struct TALER_EXCHANGEDB_SignkeyMetaData meta;
     65     struct TALER_ExchangePublicKeyP exchange_pub;
     66     struct TALER_MasterSignatureP master_sig;
     67     struct GNUNET_PQ_ResultSpec rs[] = {
     68       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     69                                             &master_sig),
     70       GNUNET_PQ_result_spec_auto_from_type ("exchange_pub",
     71                                             &exchange_pub),
     72       GNUNET_PQ_result_spec_timestamp ("valid_from",
     73                                        &meta.start),
     74       GNUNET_PQ_result_spec_timestamp ("expire_sign",
     75                                        &meta.expire_sign),
     76       GNUNET_PQ_result_spec_timestamp ("expire_legal",
     77                                        &meta.expire_legal),
     78       GNUNET_PQ_result_spec_end
     79     };
     80 
     81     if (GNUNET_OK !=
     82         GNUNET_PQ_extract_result (result,
     83                                   rs,
     84                                   i))
     85     {
     86       GNUNET_break (0);
     87       return;
     88     }
     89     dic->cb (dic->cb_cls,
     90              &exchange_pub,
     91              &meta,
     92              &master_sig);
     93   }
     94 }
     95 
     96 
     97 /**
     98  * Function called to invoke @a cb on every non-revoked exchange signing key
     99  * that has been signed by the master key.  Revoked and (for signing!)
    100  * expired keys are skipped. Runs in its own read-only transaction.
    101  *
    102  * @param cls the @e cls of this struct with the plugin-specific state
    103  * @param cb function to call on each signing key
    104  * @param cb_cls closure for @a cb
    105  * @return transaction status code
    106  */
    107 enum GNUNET_DB_QueryStatus
    108 TEH_PG_iterate_active_signkeys (void *cls,
    109                                 TALER_EXCHANGEDB_ActiveSignkeysCallback cb,
    110                                 void *cb_cls)
    111 {
    112   struct PostgresClosure *pg = cls;
    113   struct GNUNET_TIME_Absolute now = {0};
    114   struct GNUNET_PQ_QueryParam params[] = {
    115     GNUNET_PQ_query_param_absolute_time (&now),
    116     GNUNET_PQ_query_param_end
    117   };
    118   struct SignkeysIteratorContext dic = {
    119     .cb = cb,
    120     .cb_cls = cb_cls,
    121   };
    122 
    123   PREPARE (pg,
    124            "select_signkeys",
    125            "SELECT"
    126            " master_sig"
    127            ",exchange_pub"
    128            ",valid_from"
    129            ",expire_sign"
    130            ",expire_legal"
    131            " FROM exchange_sign_keys esk"
    132            " WHERE"
    133            "   expire_sign > $1"
    134            " AND NOT EXISTS "
    135            "  (SELECT esk_serial "
    136            "     FROM signkey_revocations skr"
    137            "    WHERE esk.esk_serial = skr.esk_serial);");
    138   now = GNUNET_TIME_absolute_get ();
    139   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    140                                                "select_signkeys",
    141                                                params,
    142                                                &signkeys_cb_helper,
    143                                                &dic);
    144 }