- fix
[oweals/gnunet.git] / src / util / crypto_ecc.c
index 0b3ef0ad04a6644533784c66a53313a01ca9433f..a3c92a335865ac559c8fd810b63b9c385b4124ea 100644 (file)
@@ -28,9 +28,9 @@
 #include "gnunet_common.h"
 #include "gnunet_util_lib.h"
 
-#define EXTRA_CHECKS ALLOW_EXTRA_CHECKS || 1
+#define EXTRA_CHECKS ALLOW_EXTRA_CHECKS 
 
-#define CURVE "NIST P-521"
+#define CURVE "NIST P-256"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
@@ -325,11 +325,15 @@ GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *key)
  *
  * @param buf the buffer where the private key data is stored
  * @param len the length of the data in 'buffer'
+ * @param validate GNUNET_YES to validate that the key is well-formed,
+ *                 GNUNET_NO if the key comes from a totally trusted source 
+ *                 and validation is considered too expensive
  * @return NULL on error
  */
 struct GNUNET_CRYPTO_EccPrivateKey *
 GNUNET_CRYPTO_ecc_decode_key (const char *buf, 
-                             size_t len)
+                             size_t len,
+                             int validate)
 {
   struct GNUNET_CRYPTO_EccPrivateKey *ret;
   uint16_t be;
@@ -350,8 +354,9 @@ GNUNET_CRYPTO_ecc_decode_key (const char *buf,
   {
     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_scan", rc);
     return NULL;
-  }
-  if (0 != (rc = gcry_pk_testkey (sexp)))
+  }  
+  if ( (GNUNET_YES == validate) &&
+       (0 != (rc = gcry_pk_testkey (sexp))) )
   {
     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
     return NULL;
@@ -451,7 +456,7 @@ try_read_key (const char *filename)
     char enc[fs];
 
     GNUNET_break (fs == GNUNET_DISK_file_read (fd, enc, fs));
-    if (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, fs)))
+    if (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, fs, GNUNET_YES)))
     {
       LOG (GNUNET_ERROR_TYPE_ERROR,
           _("File `%s' does not contain a valid private key (failed decode, %llu bytes).  Deleting it.\n"),
@@ -646,7 +651,7 @@ GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename)
   len = ntohs (enc->size);
   ret = NULL;
   if ((len > fs) ||
-      (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, len))))
+      (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, len, GNUNET_YES))))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
          _("File `%s' does not contain a valid private key.  Deleting it.\n"),
@@ -972,6 +977,8 @@ GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
     LOG (GNUNET_ERROR_TYPE_WARNING,
          _("ECC signing failed at %s:%d: %s\n"), __FILE__,
          __LINE__, gcry_strerror (rc));
+    gcry_sexp_release (data);
+    return GNUNET_SYSERR;
   }
   gcry_sexp_release (data);
   ssize = gcry_sexp_sprint (result,