tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / crypto_ecc_setup.c
index 67f1dea24e37d4943a70aa3b58adf285878e5412..1031b302ec23ca64ed3c38b5e131fb137d4be7d8 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2012, 2013, 2015 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012, 2013, 2015 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
 */
 
 /**
 #include <gcrypt.h>
 #include "gnunet_util_lib.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
 
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
 
-#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
 
 /**
  * Log an error message at log-level 'level' that indicates
@@ -80,6 +80,7 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
   unsigned int cnt;
   int ec;
   uint64_t fs;
+  ssize_t sret;
 
   if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
     return NULL;
@@ -119,7 +120,8 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
       {
         ec = errno;
         LOG (GNUNET_ERROR_TYPE_ERROR,
-             _("Could not acquire lock on file `%s': %s...\n"), filename,
+             _("Could not acquire lock on file `%s': %s...\n"),
+            filename,
              STRERROR (ec));
       }
     }
@@ -192,7 +194,8 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
       {
         LOG (GNUNET_ERROR_TYPE_ERROR,
              _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
-             filename, (unsigned int) fs,
+             filename,
+            (unsigned int) fs,
              (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
         LOG (GNUNET_ERROR_TYPE_ERROR,
              _("This may be ok if someone is currently generating a key.\n"));
@@ -204,12 +207,29 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
   }
   fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey);
   priv = GNUNET_malloc (fs);
-  GNUNET_assert (fs == GNUNET_DISK_file_read (fd, priv, fs));
+  sret = GNUNET_DISK_file_read (fd,
+                               priv,
+                               fs);
+  GNUNET_assert ( (sret >= 0) &&
+                 (fs == (size_t) sret) );
   if (GNUNET_YES !=
-      GNUNET_DISK_file_unlock (fd, 0,
+      GNUNET_DISK_file_unlock (fd,
+                              0,
                                sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-  GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
+                      "fcntl",
+                      filename);
+  GNUNET_assert (GNUNET_YES ==
+                GNUNET_DISK_file_close (fd));
+#if CRYPTO_BUG
+  if (GNUNET_OK !=
+      check_eddsa_key (priv))
+  {
+    GNUNET_break (0);
+    GNUNET_free (priv);
+    return NULL;
+  }
+#endif
   return priv;
 }
 
@@ -236,8 +256,10 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
   unsigned int cnt;
   int ec;
   uint64_t fs;
+  ssize_t sret;
 
-  if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
+  if (GNUNET_SYSERR ==
+      GNUNET_DISK_directory_create_for_file (filename))
     return NULL;
   while (GNUNET_YES != GNUNET_DISK_file_test (filename))
   {
@@ -254,19 +276,24 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
         {
           /* must exist but not be accessible, fail for good! */
           if (0 != ACCESS (filename, R_OK))
-            LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
+            LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
+                              "access",
+                              filename);
           else
             GNUNET_break (0);   /* what is going on!? */
           return NULL;
         }
         continue;
       }
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
+      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
+                        "open",
+                        filename);
       return NULL;
     }
     cnt = 0;
     while (GNUNET_YES !=
-           GNUNET_DISK_file_lock (fd, 0,
+           GNUNET_DISK_file_lock (fd,
+                                 0,
                                   sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
                                   GNUNET_YES))
     {
@@ -275,7 +302,8 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
       {
         ec = errno;
         LOG (GNUNET_ERROR_TYPE_ERROR,
-             _("Could not acquire lock on file `%s': %s...\n"), filename,
+             _("Could not acquire lock on file `%s': %s...\n"),
+            filename,
              STRERROR (ec));
       }
     }
@@ -284,21 +312,29 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
     priv = GNUNET_CRYPTO_ecdsa_key_create ();
     GNUNET_assert (NULL != priv);
     GNUNET_assert (sizeof (*priv) ==
-                   GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
+                   GNUNET_DISK_file_write (fd,
+                                          priv,
+                                          sizeof (*priv)));
     GNUNET_DISK_file_sync (fd);
     if (GNUNET_YES !=
         GNUNET_DISK_file_unlock (fd, 0,
                                  sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-    GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
+      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
+                        "fcntl",
+                        filename);
+    GNUNET_assert (GNUNET_YES ==
+                  GNUNET_DISK_file_close (fd));
     return priv;
   }
   /* key file exists already, read it! */
-  fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
+  fd = GNUNET_DISK_file_open (filename,
+                             GNUNET_DISK_OPEN_READ,
                               GNUNET_DISK_PERM_NONE);
   if (NULL == fd)
   {
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
+                      "open",
+                      filename);
     return NULL;
   }
   cnt = 0;
@@ -313,28 +349,38 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
       {
         ec = errno;
         LOG (GNUNET_ERROR_TYPE_ERROR,
-             _("Could not acquire lock on file `%s': %s...\n"), filename,
+             _("Could not acquire lock on file `%s': %s...\n"),
+            filename,
              STRERROR (ec));
         LOG (GNUNET_ERROR_TYPE_ERROR,
-             _
-             ("This may be ok if someone is currently generating a private key.\n"));
+             _("This may be ok if someone is currently generating a private key.\n"));
       }
       short_wait ();
       continue;
     }
-    if (GNUNET_YES != GNUNET_DISK_file_test (filename))
+    if (GNUNET_YES !=
+       GNUNET_DISK_file_test (filename))
     {
       /* eh, what!? File we opened is now gone!? */
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
+      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
+                        "stat",
+                        filename);
       if (GNUNET_YES !=
           GNUNET_DISK_file_unlock (fd, 0,
                                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-      GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
+        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
+                          "fcntl",
+                          filename);
+      GNUNET_assert (GNUNET_OK ==
+                    GNUNET_DISK_file_close (fd));
 
       return NULL;
     }
-    if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
+    if (GNUNET_OK !=
+       GNUNET_DISK_file_size (filename,
+                              &fs,
+                              GNUNET_YES,
+                              GNUNET_YES))
       fs = 0;
     if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
     {
@@ -343,7 +389,9 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
       if (GNUNET_YES !=
           GNUNET_DISK_file_unlock (fd, 0,
                                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
+                          "fcntl",
+                          filename);
       if (0 == ++cnt % 10)
       {
         LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -360,12 +408,19 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
   }
   fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
   priv = GNUNET_malloc (fs);
-  GNUNET_assert (fs == GNUNET_DISK_file_read (fd, priv, fs));
+  sret = GNUNET_DISK_file_read (fd,
+                               priv,
+                               fs);
+  GNUNET_assert ( (sret >= 0) &&
+                 (fs == (size_t) sret) );
   if (GNUNET_YES !=
       GNUNET_DISK_file_unlock (fd, 0,
                                sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-  GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
+                      "fcntl",
+                      filename);
+  GNUNET_assert (GNUNET_YES ==
+                GNUNET_DISK_file_close (fd));
   return priv;
 }