LCOV - code coverage report
Current view: top level - exchangedb - exchangedb_auditorkeys.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 56 79 70.9 %
Date: 2017-09-17 17:24:28 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.13