fix more leaks
[oweals/gnunet.git] / src / fs / fs_search.c
index 2fa14f68dc115b544a9299b290ccc1941ab459c5..11af03646d5c73c905cd785f5a4ded6c7dbde00a 100644 (file)
@@ -27,6 +27,7 @@
 #include "gnunet_fs_service.h"
 #include "gnunet_protocols.h"
 #include "fs_api.h"
+#include "fs_publish_ublock.h"
 
 
 /**
@@ -53,14 +54,15 @@ GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
   pi->value.search.sc = sc;
   pi->value.search.cctx = (NULL != sc) ? sc->client_info : NULL;
   pi->value.search.pctx =
-    ((NULL == sc) || (NULL == sc->psearch_result)) 
-    ? NULL 
+    ((NULL == sc) || (NULL == sc->psearch_result))
+    ? NULL
     : sc->psearch_result->client_info;
   pi->value.search.query = (NULL != sc) ? sc->uri : NULL;
-  pi->value.search.duration = (NULL != sc) 
+  pi->value.search.duration = (NULL != sc)
     ? GNUNET_TIME_absolute_get_duration (sc->start_time)
     : GNUNET_TIME_UNIT_ZERO;
   pi->value.search.anonymity = (NULL != sc) ? sc->anonymity : 0;
+  pi->fsh = h;
   ret = h->upcb (h->upcb_cls, pi);
   return ret;
 }
@@ -123,7 +125,7 @@ notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
                           struct GNUNET_FS_SearchResult *sr)
 {
   struct GNUNET_FS_ProgressInfo pi;
+
   pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
   pi.value.search.specifics.update.cctx = sr->client_info;
   pi.value.search.specifics.update.meta = sr->meta;
@@ -133,7 +135,7 @@ notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
   pi.value.search.specifics.update.availability_certainty =
       sr->availability_trials;
   pi.value.search.specifics.update.applicability_rank = sr->optional_support;
-  pi.value.search.specifics.update.current_probe_time 
+  pi.value.search.specifics.update.current_probe_time
     = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
 }
@@ -192,12 +194,12 @@ signal_probe_result (struct GNUNET_FS_SearchResult *sr)
   pi.value.search.specifics.update.cctx = sr->client_info;
   pi.value.search.specifics.update.meta = sr->meta;
   pi.value.search.specifics.update.uri = sr->uri;
-  pi.value.search.specifics.update.availability_rank 
+  pi.value.search.specifics.update.availability_rank
     = 2 * sr->availability_success - sr->availability_trials;
-  pi.value.search.specifics.update.availability_certainty 
+  pi.value.search.specifics.update.availability_certainty
     = sr->availability_trials;
   pi.value.search.specifics.update.applicability_rank = sr->optional_support;
-  pi.value.search.specifics.update.current_probe_time 
+  pi.value.search.specifics.update.current_probe_time
     = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sr->sc);
   GNUNET_FS_search_start_probe_ (sr);
@@ -340,7 +342,7 @@ GNUNET_FS_search_probe_progress_ (void *cls,
     {
       /* should only happen if the cancel task was already
         created on 'DOWNLOAD_INACTIVE' as we were out of time */
-      GNUNET_break (0 == sr->remaining_probe_time.rel_value);
+      GNUNET_break (0 == sr->remaining_probe_time.rel_value_us);
     }
     break;
   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
@@ -352,7 +354,7 @@ GNUNET_FS_search_probe_progress_ (void *cls,
     dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
     sr->remaining_probe_time =
         GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
-    if (0 == sr->remaining_probe_time.rel_value)
+    if (0 == sr->remaining_probe_time.rel_value_us)
       sr->probe_cancel_task =
         GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
     GNUNET_FS_search_result_sync_ (sr);
@@ -378,7 +380,7 @@ probe_ping_task (void *cls,
   struct GNUNET_FS_SearchResult *sr = cls;
 
   signal_probe_result (sr);
-  sr->probe_ping_task 
+  sr->probe_ping_task
     = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
                                    &probe_ping_task,
                                    sr);
@@ -432,7 +434,7 @@ GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
                                 len, sr->anonymity,
                                 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
                                 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
-  sr->probe_ping_task 
+  sr->probe_ping_task
     = GNUNET_SCHEDULER_add_now (&probe_ping_task,
                                sr);
 }
@@ -457,12 +459,13 @@ GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
 {
   struct GNUNET_FS_SearchResult *sr;
 
+  GNUNET_assert (NULL != h);
   sr = GNUNET_new (struct GNUNET_FS_SearchResult);
   sr->h = h;
   sr->uri = GNUNET_FS_uri_dup (uri);
   sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
   sr->client_info = client_info;
-  sr->anonymity = anonymity; 
+  sr->anonymity = anonymity;
   GNUNET_FS_search_start_probe_ (sr);
   return sr;
 }
@@ -470,8 +473,8 @@ GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
 
 /**
  * Stop probing activity associated with a search result.
- * 
- * @param sr search result 
+ *
+ * @param sr search result
  */
 static void
 GNUNET_FS_search_stop_probe_ (struct GNUNET_FS_SearchResult *sr)
@@ -555,7 +558,7 @@ process_ksk_result (struct GNUNET_FS_SearchContext *sc,
   is_new = (NULL == sr) || (sr->mandatory_missing > 0);
   if (NULL == sr)
   {
-    sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
+    sr = GNUNET_new (struct GNUNET_FS_SearchResult);
     sr->h = sc->h;
     sr->sc = sc;
     sr->anonymity = sc->anonymity;
@@ -636,7 +639,7 @@ process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
                                                   &test_result_present,
                                                   (void *) uri))
     return;                     /* duplicate result */
-  sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
+  sr = GNUNET_new (struct GNUNET_FS_SearchResult);
   sr->h = sc->h;
   sr->sc = sc;
   sr->anonymity = sc->anonymity;
@@ -667,31 +670,29 @@ process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
  * given ciphertext block.
  *
  * @param sc search context with the keywords
- * @param verification_key public key to use to lookup the keyword
+ * @param dpub derived public key used for the search
  * @param edata encrypted data
- * @param edata_size number of bytes in 'edata' (and 'data')
+ * @param edata_size number of bytes in @a edata (and @a data)
  * @param data where to store the plaintext
- * @return keyword index on success, GNUNET_SYSERR on error (no such 
+ * @return keyword index on success, #GNUNET_SYSERR on error (no such
  *         keyword, internal error)
  */
 static int
 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
-                           const struct GNUNET_FS_PseudonymIdentifier *verification_key,
+                           const struct GNUNET_CRYPTO_EccPublicSignKey *dpub,
                            const void *edata,
                            size_t edata_size,
                            char *data)
-{ 
-  struct GNUNET_HashCode q;
-  struct GNUNET_CRYPTO_AesSessionKey skey;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
-  int i;
+{
+  const struct GNUNET_CRYPTO_EccPrivateKey *anon;
+  struct GNUNET_CRYPTO_EccPublicSignKey anon_pub;
+  unsigned int i;
 
-  GNUNET_CRYPTO_hash (verification_key,
-                      sizeof (struct GNUNET_FS_PseudonymIdentifier),
-                      &q);
   /* find key */
   for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
-    if (0 == memcmp (&q, &sc->requests[i].uquery, sizeof (struct GNUNET_HashCode)))
+    if (0 == memcmp (dpub,
+                    &sc->requests[i].dpub,
+                    sizeof (struct GNUNET_CRYPTO_EccPublicSignKey)))
       break;
   if (i == sc->uri->data.ksk.keywordCount)
   {
@@ -700,14 +701,12 @@ decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
     return GNUNET_SYSERR;
   }
   /* decrypt */
-  GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].ukey, &skey, &iv);
-  if (-1 ==
-      GNUNET_CRYPTO_aes_decrypt (edata, edata_size, &skey,
-                                 &iv, data))
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
+  anon = GNUNET_CRYPTO_ecc_key_get_anonymous ();
+  GNUNET_CRYPTO_ecc_key_get_public_for_signature (anon, &anon_pub);
+  GNUNET_FS_ublock_decrypt_ (edata, edata_size,
+                            &anon_pub,
+                            sc->requests[i].keyword,
+                            data);
   return i;
 }
 
@@ -719,10 +718,11 @@ decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
  *
  * @param sc our search context
  * @param ub the ublock with the keyword search result
- * @param size size of nb
+ * @param size size of @a ub
  */
 static void
-process_kblock (struct GNUNET_FS_SearchContext *sc, const struct UBlock *ub,
+process_kblock (struct GNUNET_FS_SearchContext *sc,
+               const struct UBlock *ub,
                 size_t size)
 {
   size_t j;
@@ -749,7 +749,7 @@ process_kblock (struct GNUNET_FS_SearchContext *sc, const struct UBlock *ub,
   if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
   {
     GNUNET_break_op (0);        /* ublock malformed */
-    GNUNET_free_non_null (emsg);   
+    GNUNET_free_non_null (emsg);
     return;
   }
   j = eos - pt + 1;
@@ -781,37 +781,23 @@ process_kblock (struct GNUNET_FS_SearchContext *sc, const struct UBlock *ub,
  * @param size size of sb
  */
 static void
-process_sblock (struct GNUNET_FS_SearchContext *sc, 
+process_sblock (struct GNUNET_FS_SearchContext *sc,
                const struct UBlock *ub,
                 size_t size)
 {
   size_t len = size - sizeof (struct UBlock);
   char pt[len];
-  struct GNUNET_CRYPTO_AesSessionKey skey;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
   struct GNUNET_FS_Uri *uri;
   struct GNUNET_CONTAINER_MetaData *meta;
   const char *id;
   const char *uris;
   size_t off;
   char *emsg;
-  struct GNUNET_HashCode key;
-  struct GNUNET_HashCode id_hash;
-  struct GNUNET_HashCode ns_hash;
-  char *identifier;
 
-  /* decrypt */
-  identifier = sc->uri->data.sks.identifier;
-  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &id_hash);
-  GNUNET_CRYPTO_hash (&sc->uri->data.sks.ns, 
-                     sizeof (sc->uri->data.sks.ns), &ns_hash);
-  GNUNET_CRYPTO_hash_xor (&id_hash, &ns_hash, &key);
-  GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
-  if (-1 == GNUNET_CRYPTO_aes_decrypt (&ub[1], len, &skey, &iv, pt))
-  {
-    GNUNET_break (0);
-    return;
-  }
+  GNUNET_FS_ublock_decrypt_ (&ub[1], len,
+                            &sc->uri->data.sks.ns,
+                            sc->uri->data.sks.identifier,
+                            pt);
   /* parse */
   if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
   {
@@ -825,8 +811,8 @@ process_sblock (struct GNUNET_FS_SearchContext *sc,
   }
   if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-               _("Failed to parse URI `%s': %s\n"), 
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("Failed to parse URI `%s': %s\n"),
                uris, emsg);
     GNUNET_break_op (0);        /* ublock malformed */
     GNUNET_free_non_null (emsg);
@@ -848,15 +834,16 @@ process_sblock (struct GNUNET_FS_SearchContext *sc,
  * @param type type of the result
  * @param expiration when it will expire
  * @param data the (encrypted) response
- * @param size size of data
+ * @param size size of @a data
  */
 static void
-process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
-                struct GNUNET_TIME_Absolute expiration, 
+process_result (struct GNUNET_FS_SearchContext *sc,
+               enum GNUNET_BLOCK_Type type,
+                struct GNUNET_TIME_Absolute expiration,
                const void *data,
                 size_t size)
 {
-  if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
+  if (GNUNET_TIME_absolute_get_duration (expiration).rel_value_us > 0)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Result received has already expired.\n");
@@ -1048,12 +1035,7 @@ transmit_search_request (void *cls, size_t size, void *buf)
   struct MessageBuilderContext mbc;
   size_t msize;
   struct SearchMessage *sm;
-  const char *identifier;
-  struct GNUNET_HashCode key;
-  struct GNUNET_HashCode signing_key;
-  struct GNUNET_HashCode ns_hash;
-  struct GNUNET_HashCode id_hash;
-  struct GNUNET_FS_PseudonymIdentifier verification_key;
+  struct GNUNET_CRYPTO_EccPublicSignKey dpub;
   unsigned int sqms;
   uint32_t options;
 
@@ -1120,18 +1102,12 @@ transmit_search_request (void *cls, size_t size, void *buf)
     sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
     sm->anonymity_level = htonl (sc->anonymity);
     memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
-
-    identifier = sc->uri->data.sks.identifier;
-    GNUNET_CRYPTO_hash (identifier, strlen (identifier), &id_hash);
-    GNUNET_CRYPTO_hash (&sc->uri->data.sks.ns,
-                       sizeof (sc->uri->data.sks.ns), &ns_hash);
-    GNUNET_CRYPTO_hash_xor (&id_hash, &ns_hash, &key);
-    GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &signing_key);
-    GNUNET_FS_pseudonym_derive_verification_key (&sc->uri->data.sks.ns,
-                                                &signing_key,
-                                                &verification_key);
-    GNUNET_CRYPTO_hash (&verification_key,
-                       sizeof (verification_key),
+    GNUNET_CRYPTO_ecc_public_key_derive (&sc->uri->data.sks.ns,
+                                        sc->uri->data.sks.identifier,
+                                        "fs-ublock",
+                                        &dpub);
+    GNUNET_CRYPTO_hash (&dpub,
+                       sizeof (dpub),
                        &sm->query);
     mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
     sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
@@ -1230,7 +1206,7 @@ try_reconnect (struct GNUNET_FS_SearchContext *sc)
   }
   sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
   sc->task =
-      GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff, 
+      GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff,
                                    &do_reconnect,
                                     sc);
 }
@@ -1295,35 +1271,35 @@ GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
 {
   unsigned int i;
   const char *keyword;
-  struct GNUNET_HashCode signing_key;
-  struct GNUNET_FS_PseudonymHandle *ph;
-  struct GNUNET_FS_PseudonymIdentifier anon;
-  struct GNUNET_FS_PseudonymIdentifier verification_key;
+  const struct GNUNET_CRYPTO_EccPrivateKey *anon;
+  struct GNUNET_CRYPTO_EccPublicSignKey anon_pub;
+  struct SearchRequestEntry *sre;
 
   GNUNET_assert (NULL == sc->client);
   if (GNUNET_FS_uri_test_ksk (sc->uri))
   {
     GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
-    ph = GNUNET_FS_pseudonym_get_anonymous_pseudonym_handle ();
-    GNUNET_FS_pseudonym_get_identifier (ph, &anon);
-    GNUNET_FS_pseudonym_destroy (ph);
+    anon = GNUNET_CRYPTO_ecc_key_get_anonymous ();
+    GNUNET_CRYPTO_ecc_key_get_public_for_signature (anon, &anon_pub);
     sc->requests =
         GNUNET_malloc (sizeof (struct SearchRequestEntry) *
                        sc->uri->data.ksk.keywordCount);
     for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
     {
       keyword = &sc->uri->data.ksk.keywords[i][1];
-      GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].ukey);
-      GNUNET_CRYPTO_hash (&sc->requests[i].ukey, sizeof (struct GNUNET_HashCode), &signing_key);
-      GNUNET_FS_pseudonym_derive_verification_key (&anon, 
-                                                  &signing_key,
-                                                  &verification_key);
-      GNUNET_CRYPTO_hash (&verification_key, sizeof (struct GNUNET_FS_PseudonymIdentifier),
-                         &sc->requests[i].uquery);
-      sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
-      if (sc->requests[i].mandatory)
+      sre = &sc->requests[i];
+      sre->keyword = GNUNET_strdup (keyword);
+      GNUNET_CRYPTO_ecc_public_key_derive (&anon_pub,
+                                          keyword,
+                                          "fs-ublock",
+                                          &sre->dpub);
+      GNUNET_CRYPTO_hash (&sre->dpub,
+                         sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
+                         &sre->uquery);
+      sre->mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
+      if (sre->mandatory)
         sc->mandatory_count++;
-      sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
+      sre->results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
     }
   }
   sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
@@ -1473,7 +1449,10 @@ GNUNET_FS_search_signal_suspend_ (void *cls)
   {
     GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
     for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
+    {
       GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
+      GNUNET_free (sc->requests[i].keyword);
+    }
   }
   GNUNET_free_non_null (sc->requests);
   GNUNET_free_non_null (sc->emsg);