RECLAIM/OIDC: code cleanup
[oweals/gnunet.git] / src / revocation / gnunet-revocation.c
index 10259fc995c8c250ac3b830f4bf14fdadcc5640b..4b8fa15bb73c3d1186dd3eff8e4bafc3eaadeffd 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -61,7 +61,7 @@ static struct GNUNET_REVOCATION_Query *q;
 
 /**
  * Handle for revocation.
- */ 
+ */
 static struct GNUNET_REVOCATION_Handle *h;
 
 /**
@@ -71,25 +71,33 @@ static struct GNUNET_IDENTITY_EgoLookup *el;
 
 /**
  * Our configuration.
- */ 
+ */
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
  * Number of matching bits required for revocation.
- */ 
+ */
 static unsigned long long matching_bits;
 
+/**
+ * Task used for proof-of-work calculation.
+ */
+static struct GNUNET_SCHEDULER_Task *pow_task;
+
 
 /**
  * Function run if the user aborts with CTRL-C.
  *
  * @param cls closure
- * @param tc scheduler context
  */
 static void
-do_shutdown (void *cls,
-             const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
 {
+  if (NULL != el)
+  {
+    GNUNET_IDENTITY_ego_lookup_cancel (el);
+    el = NULL;
+  }
   if (NULL != q)
   {
     GNUNET_REVOCATION_query_cancel (q);
@@ -153,14 +161,24 @@ print_revocation_result (void *cls,
   switch (is_valid)
   {
   case GNUNET_YES:
-    FPRINTF (stdout,
-             _("Key for ego `%s' is still valid, revocation failed (!)\n"),
-             test_ego);
+    if (NULL != revoke_ego)
+      FPRINTF (stdout,
+               _("Key for ego `%s' is still valid, revocation failed (!)\n"),
+               revoke_ego);
+    else
+      FPRINTF (stdout,
+               "%s",
+               _("Revocation failed (!)\n"));
     break;
   case GNUNET_NO:
-    FPRINTF (stdout,
-             _("Key for ego `%s' has been successfully revoked\n"),
-             test_ego);
+    if (NULL != revoke_ego)
+      FPRINTF (stdout,
+               _("Key for ego `%s' has been successfully revoked\n"),
+               revoke_ego);
+    else
+      FPRINTF (stdout,
+               "%s",
+               _("Revocation successful.\n"));
     break;
   case GNUNET_SYSERR:
     FPRINTF (stdout,
@@ -178,17 +196,17 @@ print_revocation_result (void *cls,
 /**
  * Data needed to perform a revocation.
  */
-struct RevocationData 
+struct RevocationData
 {
   /**
    * Public key.
    */
-  struct GNUNET_CRYPTO_EccPublicSignKey key;
+  struct GNUNET_CRYPTO_EcdsaPublicKey key;
 
   /**
    * Revocation signature data.
    */
-  struct GNUNET_CRYPTO_EccSignature sig;
+  struct GNUNET_CRYPTO_EcdsaSignature sig;
 
   /**
    * Proof of work (in NBO).
@@ -212,43 +230,72 @@ perform_revocation (const struct RevocationData *rd)
 }
 
 
+/**
+ * Write the current state of the revocation data
+ * to disk.
+ *
+ * @param rd data to sync
+ */
+static void
+sync_rd (const struct RevocationData *rd)
+{
+  if ( (NULL != filename) &&
+       (sizeof (struct RevocationData) ==
+       GNUNET_DISK_fn_write (filename,
+                             &rd,
+                             sizeof (rd),
+                             GNUNET_DISK_PERM_USER_READ |
+                             GNUNET_DISK_PERM_USER_WRITE)) )
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                             "write",
+                             filename);
+}
+
+
 /**
  * Perform the proof-of-work calculation.
  *
  * @param cls the `struct RevocationData`
- * @param tc scheduler context
  */
 static void
-calculate_pow (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+calculate_pow_shutdown (void *cls)
 {
   struct RevocationData *rd = cls;
 
-  if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) ||
-       (0 == (rd->pow % 128) ) )
-  {    
-    if (0 == (rd->pow % 128 * 1024))
-    {
-      if (0 == (rd->pow % (1024 * 128 * 80)))
-        fprintf (stderr, "\n");
-      fprintf (stderr, ".");
-    }
-    if ( (NULL != filename) &&
-         (sizeof (struct RevocationData) ==
-          GNUNET_DISK_fn_write (filename,
-                                &rd,
-                                sizeof (rd),
-                                GNUNET_DISK_PERM_USER_READ |
-                                GNUNET_DISK_PERM_USER_WRITE)) )
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
-                                "write",
-                                filename);
-  }
-  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
+  if (NULL != pow_task)
   {
-    GNUNET_free (rd);
-    return;
+    GNUNET_SCHEDULER_cancel (pow_task);
+    pow_task = NULL;
   }
+  sync_rd (rd);
+  GNUNET_free (rd);
+}
+
+
+/**
+ * Perform the proof-of-work calculation.
+ *
+ * @param cls the `struct RevocationData`
+ */
+static void
+calculate_pow (void *cls)
+{
+  struct RevocationData *rd = cls;
+
+  /* store temporary results */
+  pow_task = NULL;
+  if (0 == (rd->pow % 128))
+    sync_rd (rd);
+  /* display progress estimate */
+  if ( (0 == ((1 << matching_bits) / 100 / 50)) ||
+       (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) )
+    FPRINTF (stderr, "%s", ".");
+  if ( (0 != rd->pow) &&
+       ( (0 == ((1 << matching_bits) / 100)) ||
+         (0 == (rd->pow % ((1 << matching_bits) / 100))) ) )
+    FPRINTF (stderr, " - @ %3u%% (estimate)\n",
+             (unsigned int) (rd->pow * 100) / (1 << matching_bits));
+  /* actually do POW calculation */
   rd->pow++;
   if (GNUNET_OK ==
       GNUNET_REVOCATION_check_pow (&rd->key,
@@ -256,29 +303,32 @@ calculate_pow (void *cls,
                                    (unsigned int) matching_bits))
   {
     if ( (NULL != filename) &&
-         (sizeof (struct RevocationData) ==
+         (sizeof (struct RevocationData) !=
           GNUNET_DISK_fn_write (filename,
-                                &rd,
-                                sizeof (rd),
+                                rd,
+                                sizeof (struct RevocationData),
                                 GNUNET_DISK_PERM_USER_READ |
                                 GNUNET_DISK_PERM_USER_WRITE)) )
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                                 "write",
                                 filename);
     if (perform)
+    {
       perform_revocation (rd);
+    }
     else
     {
+      FPRINTF (stderr, "%s", "\n");
       FPRINTF (stderr,
                _("Revocation certificate for `%s' stored in `%s'\n"),
                revoke_ego,
                filename);
       GNUNET_SCHEDULER_shutdown ();
     }
-    GNUNET_free (rd);
+    return;
   }
-  GNUNET_SCHEDULER_add_now (&calculate_pow,
-                            rd);
+  pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
+                                      rd);
 }
 
 
@@ -293,14 +343,14 @@ ego_callback (void *cls,
               const struct GNUNET_IDENTITY_Ego *ego)
 {
   struct RevocationData *rd;
-  struct GNUNET_CRYPTO_EccPublicSignKey key;
+  struct GNUNET_CRYPTO_EcdsaPublicKey key;
 
   el = NULL;
   if (NULL == ego)
   {
     FPRINTF (stdout,
              _("Ego `%s' not found.\n"),
-             test_ego);
+             revoke_ego);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -308,16 +358,15 @@ ego_callback (void *cls,
                                       &key);
   rd = GNUNET_new (struct RevocationData);
   if ( (NULL != filename) &&
-       (GNUNET_YES == 
+       (GNUNET_YES ==
         GNUNET_DISK_file_test (filename)) &&
        (sizeof (struct RevocationData) ==
         GNUNET_DISK_fn_read (filename,
-                             &rd,
-                             sizeof (rd))) )
+                             rd,
+                             sizeof (struct RevocationData))) )
   {
-    if (0 != memcmp (&rd->key,
-                     &key,
-                     sizeof (struct GNUNET_CRYPTO_EccPublicSignKey)))
+    if (0 != GNUNET_memcmp (&rd->key,
+                     &key))
     {
       fprintf (stderr,
                _("Error: revocation certificate in `%s' is not for `%s'\n"),
@@ -325,10 +374,10 @@ ego_callback (void *cls,
                revoke_ego);
       GNUNET_free (rd);
       return;
-    }    
+    }
   }
   else
-  {    
+  {
     GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (ego),
                                        &rd->sig);
     rd->key = key;
@@ -340,16 +389,21 @@ ego_callback (void *cls,
   {
     FPRINTF (stderr,
              "%s",
-             _("Revocation certificate ready, initiating revocation\n"));
-    perform_revocation (rd);
+             _("Revocation certificate ready\n"));
+    if (perform)
+      perform_revocation (rd);
+    else
+      GNUNET_SCHEDULER_shutdown ();
     GNUNET_free (rd);
     return;
   }
   FPRINTF (stderr,
            "%s",
            _("Revocation certificate not ready, calculating proof of work\n"));
-  GNUNET_SCHEDULER_add_now (&calculate_pow,
-                            rd);
+  pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
+                                      rd);
+  GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
+                                rd);
 }
 
 
@@ -362,19 +416,19 @@ ego_callback (void *cls,
  * @param c configuration
  */
 static void
-run (void *cls, 
-     char *const *args, 
+run (void *cls,
+     char *const *args,
      const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  struct GNUNET_CRYPTO_EccPublicSignKey pk;
+  struct GNUNET_CRYPTO_EcdsaPublicKey pk;
   struct RevocationData rd;
 
   cfg = c;
   if (NULL != test_ego)
   {
     if (GNUNET_OK !=
-        GNUNET_CRYPTO_ecc_public_sign_key_from_string (test_ego,
+        GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
                                                        strlen (test_ego),
                                                        &pk))
     {
@@ -383,9 +437,8 @@ run (void *cls,
                test_ego);
       return;
     }
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                  &do_shutdown,
-                                  NULL);
+    GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                  NULL);
     q = GNUNET_REVOCATION_query (cfg,
                                  &pk,
                                  &print_query_result,
@@ -409,14 +462,20 @@ run (void *cls,
   }
   if (NULL != revoke_ego)
   {
+    if ( !perform && (NULL == filename) )
+    {
+        FPRINTF (stderr,
+                 "%s",
+                 _("No filename to store revocation certificate given.\n"));
+        return;
+    }
     /* main code here */
     el = GNUNET_IDENTITY_ego_lookup (cfg,
                                      revoke_ego,
                                      &ego_callback,
                                      NULL);
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                  &do_shutdown,
-                                  NULL);
+    GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                  NULL);
     return;
   }
   if ( (NULL != filename) &&
@@ -432,10 +491,23 @@ run (void *cls,
                filename);
       return;
     }
+    GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                  NULL);
+    if (GNUNET_YES !=
+        GNUNET_REVOCATION_check_pow (&rd.key,
+                                     rd.pow,
+                                     (unsigned int) matching_bits))
+    {
+      struct RevocationData *cp = GNUNET_new (struct RevocationData);
+
+      *cp = rd;
+      pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
+                                          cp);
+      GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
+                                    cp);
+      return;
+    }
     perform_revocation (&rd);
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                  &do_shutdown,
-                                  NULL);
     return;
   }
   FPRINTF (stderr,
@@ -454,19 +526,31 @@ run (void *cls,
 int
 main (int argc, char *const *argv)
 {
-  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
-    {'f', "filename", "NAME",
-     gettext_noop ("use NAME for the name of the revocation file"),
-     1, &GNUNET_GETOPT_set_string, &filename},
-    {'R', "revoke", "NAME",
-     gettext_noop ("revoke the private key associated with the ego NAME "),
-     1, &GNUNET_GETOPT_set_string, &revoke_ego},
-    {'p', "perform", NULL,
-     gettext_noop ("actually perform the revocation revocation file, otherwise we just do the precomputation"),
-     0, &GNUNET_GETOPT_set_one, &perform},
-    {'t', "test", "KEY",
-     gettext_noop ("test if the public key KEY has been revoked"),
-     1, &GNUNET_GETOPT_set_string, &test_ego},
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+
+    GNUNET_GETOPT_option_string ('f',
+                                 "filename",
+                                 "NAME",
+                                 gettext_noop ("use NAME for the name of the revocation file"),
+                                 &filename),
+
+    GNUNET_GETOPT_option_string ('R',
+                                 "revoke",
+                                 "NAME",
+                                 gettext_noop ("revoke the private key associated for the the private key associated with the ego NAME "),
+                                 &revoke_ego), 
+
+    GNUNET_GETOPT_option_flag ('p',
+                                  "perform",
+                                  gettext_noop ("actually perform revocation, otherwise we just do the precomputation"),
+                                  &perform),
+
+    GNUNET_GETOPT_option_string ('t',
+                                 "test",
+                                 "KEY",
+                                 gettext_noop ("test if the public key KEY has been revoked"),
+                                 &test_ego), 
+
     GNUNET_GETOPT_OPTION_END
   };
   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))