LCOV - code coverage report
Current view: top level - exchangedb - exchangedb_auditorkeys.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 60 88 68.2 %
Date: 2017-11-25 11:31:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016, 2017 Inria & GNUnet e.V.
       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/exchangedb_auditorkeys.c
      18             :  * @brief I/O operations for the Exchange's auditor data
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Sree Harsha Totakura
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_exchangedb_lib.h"
      26             : 
      27             : 
      28             : /**
      29             :  * Closure for #auditor_iter() and
      30             :  */
      31             : struct AuditorIterateContext
      32             : {
      33             : 
      34             :   /**
      35             :    * Function to call with the information for each auditor.
      36             :    */
      37             :   TALER_EXCHANGEDB_AuditorIterator it;
      38             : 
      39             :   /**
      40             :    * Closure for @e it.
      41             :    */
      42             :   void *it_cls;
      43             : 
      44             :   /**
      45             :    * Status of the iteration.
      46             :    */
      47             :   int status;
      48             : };
      49             : 
      50             : 
      51             : GNUNET_NETWORK_STRUCT_BEGIN
      52             : 
      53             : /**
      54             :  * Header of a file with auditing information.
      55             :  */
      56             : struct AuditorFileHeaderP
      57             : {
      58             : 
      59             :   /**
      60             :    * Public key of the auditor.
      61             :    */
      62             :   struct TALER_AuditorPublicKeyP apub;
      63             : 
      64             :   /**
      65             :    * Master public key of the exchange the auditor is signing
      66             :    * information for.
      67             :    */
      68             :   struct TALER_MasterPublicKeyP mpub;
      69             : 
      70             :   /**
      71             :    * Number of signatures and DKI entries in this file.
      72             :    */
      73             :   uint32_t dki_len;
      74             : 
      75             : };
      76             : GNUNET_NETWORK_STRUCT_END
      77             : 
      78             : 
      79             : /**
      80             :  * Load the auditor signature and the information signed by the
      81             :  * auditor and call the callback in @a cls with the information.
      82             :  *
      83             :  * @param cls the `struct AuditorIterateContext *`
      84             :  * @param filename name of a file that should contain
      85             :  *                 a denomination key
      86             :  * @return #GNUNET_OK to continue to iterate
      87             :  *         #GNUNET_NO to abort iteration with success
      88             :  *         #GNUNET_SYSERR to abort iteration with failure
      89             :  */
      90             : static int
      91           7 : auditor_iter (void *cls,
      92             :               const char *filename)
      93             : {
      94           7 :   struct AuditorIterateContext *aic = cls;
      95             :   uint64_t size;
      96             :   struct AuditorFileHeaderP *af;
      97             :   const struct TALER_AuditorSignatureP *sigs;
      98             :   const struct TALER_DenominationKeyValidityPS *dki;
      99             :   const char *auditor_url;
     100             :   unsigned int dki_len;
     101             :   size_t url_len;
     102             :   int iret;
     103             : 
     104           7 :   if (GNUNET_OK != GNUNET_DISK_file_size (filename,
     105             :                                           &size,
     106             :                                           GNUNET_YES,
     107             :                                           GNUNET_YES))
     108             :   {
     109           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     110             :                 "Skipping inaccessable auditor information file `%s'\n",
     111             :                 filename);
     112           0 :     return GNUNET_OK;
     113             :   }
     114           7 :   if (size < sizeof (struct AuditorFileHeaderP))
     115             :   {
     116           0 :     GNUNET_break (0);
     117           0 :     return GNUNET_OK;
     118             :   }
     119           7 :   af = GNUNET_malloc (size);
     120           7 :   if (size !=
     121           7 :       GNUNET_DISK_fn_read (filename,
     122             :                            af,
     123             :                            size))
     124             :   {
     125           0 :     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
     126             :                               "read",
     127             :                               filename);
     128           0 :     GNUNET_free (af);
     129           0 :     return GNUNET_OK;
     130             :   }
     131           7 :   dki_len = ntohl (af->dki_len);
     132           7 :   if (0 == dki_len)
     133             :   {
     134           0 :     GNUNET_break_op (0);
     135           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     136             :                 "No signed keys in %s\n",
     137             :                 filename);
     138           0 :     GNUNET_free (af);
     139           0 :     return GNUNET_OK;
     140             :   }
     141           7 :   if ( (size - sizeof (struct AuditorFileHeaderP)) / dki_len <
     142             :        (sizeof (struct TALER_DenominationKeyValidityPS) +
     143             :         sizeof (struct TALER_AuditorSignatureP)) )
     144             :   {
     145           0 :     GNUNET_break_op (0);
     146           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     147             :                 "Malformed key file %s\n",
     148             :                 filename);
     149           0 :     GNUNET_free (af);
     150           0 :     return GNUNET_OK;
     151             :   }
     152           7 :   url_len = size
     153             :     - sizeof (struct AuditorFileHeaderP)
     154           7 :     - dki_len * (sizeof (struct TALER_DenominationKeyValidityPS) +
     155             :                  sizeof (struct TALER_AuditorSignatureP));
     156           7 :   sigs = (const struct TALER_AuditorSignatureP *) &af[1];
     157           7 :   dki = (const struct TALER_DenominationKeyValidityPS *) &sigs[dki_len];
     158           7 :   auditor_url = (const char *) &dki[dki_len];
     159          14 :   if ( (0 == url_len) ||
     160           7 :        ('\0' != auditor_url[url_len - 1]) )
     161             :   {
     162           0 :     GNUNET_break_op (0);
     163           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     164             :                 "Malformed key file %s\n",
     165             :                 filename);
     166           0 :     GNUNET_free (af);
     167           0 :     return GNUNET_OK;
     168             :   }
     169             :   /* Ignoring return value to not interrupt the iteration */
     170           7 :   if (GNUNET_OK !=
     171          14 :       (iret = aic->it (aic->it_cls,
     172           7 :                        &af->apub,
     173             :                        auditor_url,
     174           7 :                        &af->mpub,
     175             :                        dki_len,
     176             :                        sigs,
     177             :                        dki)))
     178             :   {
     179           0 :     GNUNET_free (af);
     180           0 :     if (GNUNET_SYSERR == iret)
     181           0 :       aic->status = GNUNET_SYSERR;
     182           0 :     return GNUNET_SYSERR;
     183             :   }
     184           7 :   aic->status++;
     185           7 :   GNUNET_free (af);
     186           7 :   return GNUNET_OK;
     187             : }
     188             : 
     189             : 
     190             : /**
     191             :  * Call @a it with information for each auditor found in the @a exchange_base_dir.
     192             :  *
     193             :  * @param cfg configuration to use
     194             :  * @param it function to call with auditor information
     195             :  * @param it_cls closure for @a it
     196             :  * @return -1 on error, 0 if no files were found, otherwise
     197             :  *         a positive number (however, even with a positive
     198             :  *         number it is possible that @a it was never called
     199             :  *         as maybe none of the files were well-formed)
     200             :  */
     201             : int
     202           7 : TALER_EXCHANGEDB_auditor_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
     203             :                                   TALER_EXCHANGEDB_AuditorIterator it,
     204             :                                   void *it_cls)
     205             : {
     206             :   struct AuditorIterateContext aic;
     207             :   int ret;
     208             :   char *auditor_base_dir;
     209             : 
     210           7 :   if (GNUNET_OK !=
     211           7 :       GNUNET_CONFIGURATION_get_value_filename (cfg,
     212             :                                                "exchangedb",
     213             :                                                "AUDITOR_BASE_DIR",
     214             :                                                &auditor_base_dir))
     215           0 :     return -1;
     216           7 :   aic.it = it;
     217           7 :   aic.it_cls = it_cls;
     218           7 :   aic.status = 0;
     219           7 :   ret = GNUNET_DISK_directory_scan (auditor_base_dir,
     220             :                                     &auditor_iter,
     221             :                                     &aic);
     222           7 :   GNUNET_free (auditor_base_dir);
     223           7 :   if ( (0 != aic.status) ||
     224             :        (GNUNET_OK == ret) )
     225           7 :     return aic.status;
     226           0 :   return ret;
     227             : }
     228             : 
     229             : 
     230             : /**
     231             :  * Write auditor information to the given file.
     232             :  *
     233             :  * @param filename the file where to write the auditor information to
     234             :  * @param apub the auditor's public key
     235             :  * @param auditor_url the URL of the auditor
     236             :  * @param asigs the auditor's signatures, array of length @a dki_len
     237             :  * @param mpub the exchange's public key (as expected by the auditor)
     238             :  * @param dki_len length of @a dki
     239             :  * @param dki array of denomination coin data signed by the auditor
     240             :  * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure.
     241             :  */
     242             : int
     243           4 : TALER_EXCHANGEDB_auditor_write (const char *filename,
     244             :                                 const struct TALER_AuditorPublicKeyP *apub,
     245             :                                 const char *auditor_url,
     246             :                                 const struct TALER_AuditorSignatureP *asigs,
     247             :                                 const struct TALER_MasterPublicKeyP *mpub,
     248             :                                 unsigned int dki_len,
     249             :                                 const struct TALER_DenominationKeyValidityPS *dki)
     250             : {
     251             :   struct AuditorFileHeaderP af;
     252             :   struct GNUNET_DISK_FileHandle *fh;
     253             :   ssize_t wrote;
     254             :   size_t wsize;
     255             :   int ret;
     256             :   int eno;
     257             : 
     258           4 :   af.apub = *apub;
     259           4 :   af.mpub = *mpub;
     260           4 :   af.dki_len = htonl ((uint32_t) dki_len);
     261           4 :   ret = GNUNET_SYSERR;
     262           4 :   if (NULL == (fh = GNUNET_DISK_file_open
     263             :                (filename,
     264             :                 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
     265             :                 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE)))
     266           0 :     goto cleanup;
     267           4 :   wsize = sizeof (struct AuditorFileHeaderP);
     268           4 :   if (GNUNET_SYSERR == (wrote = GNUNET_DISK_file_write (fh,
     269             :                                                         &af,
     270             :                                                         wsize)))
     271           0 :     goto cleanup;
     272           4 :   if (wrote != wsize)
     273           0 :     goto cleanup;
     274           4 :   wsize = dki_len * sizeof (struct TALER_AuditorSignatureP);
     275           4 :   if (wsize ==
     276           4 :       GNUNET_DISK_file_write (fh,
     277             :                               asigs,
     278             :                               wsize))
     279           4 :     ret = GNUNET_OK;
     280           4 :   wsize = dki_len * sizeof (struct TALER_DenominationKeyValidityPS);
     281           4 :   if (wsize ==
     282           4 :       GNUNET_DISK_file_write (fh,
     283             :                               dki,
     284             :                               wsize))
     285           4 :     ret = GNUNET_OK;
     286           4 :   wsize = strlen (auditor_url) + 1;
     287           4 :   if (wsize ==
     288           4 :       GNUNET_DISK_file_write (fh,
     289             :                               auditor_url,
     290             :                               wsize))
     291           4 :     ret = GNUNET_OK;
     292             :  cleanup:
     293           4 :   eno = errno;
     294           4 :   if (NULL != fh)
     295           4 :     (void) GNUNET_DISK_file_close (fh);
     296           4 :   errno = eno;
     297           4 :   return ret;
     298             : }
     299             : 
     300             : 
     301             : /* end of exchangedb_auditorkeys.c */

Generated by: LCOV version 1.13