- fix coverity
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct_bob.c
index 2c6d607e58d45164a1f4fd20f547c4c2237bbefe..d0a622a36a3d9567675793fe217a50b49b99f335 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013, 2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013, 2014 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
@@ -14,8 +14,8 @@
 
      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.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
  */
 /**
  * @file scalarproduct/gnunet-service-scalarproduct_bob.c
@@ -139,11 +139,6 @@ struct BobServiceSession
    */
   struct CadetIncomingSession *cadet;
 
-  /**
-   * The computed scalar
-   */
-  gcry_mpi_t product;
-
   /**
    * How many elements will be supplied in total from the client.
    */
@@ -408,11 +403,6 @@ destroy_service_session (struct BobServiceSession *s)
     GNUNET_free (s->r_prime);
     s->r_prime = NULL;
   }
-  if (NULL != s->product)
-  {
-    gcry_mpi_release (s->product);
-    s->product = NULL;
-  }
   GNUNET_free (s);
 }
 
@@ -470,6 +460,8 @@ prepare_client_end_notification (struct BobServiceSession *session)
   struct ClientResponseMessage *msg;
   struct GNUNET_MQ_Envelope *e;
 
+  if (NULL == session->client_mq)
+    return; /* no client left to be notified */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sending session-end notification with status %d to client for session %s\n",
               session->status,
@@ -507,12 +499,6 @@ cb_channel_destruction (void *cls,
               "Peer disconnected, terminating session %s with peer %s\n",
               GNUNET_h2s (&in->session_id),
               GNUNET_i2s (&in->peer));
-  if (NULL != in->cadet_mq)
-  {
-    GNUNET_MQ_destroy (in->cadet_mq);
-    in->cadet_mq = NULL;
-  }
-  in->channel = NULL;
   if (NULL != (s = in->s))
   {
     if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
@@ -521,6 +507,12 @@ cb_channel_destruction (void *cls,
       prepare_client_end_notification (s);
     }
   }
+  if (NULL != in->cadet_mq)
+  {
+    GNUNET_MQ_destroy (in->cadet_mq);
+    in->cadet_mq = NULL;
+  }
+  in->channel = NULL;
   destroy_cadet_session (in);
 }
 
@@ -542,7 +534,7 @@ bob_cadet_done_cb (void *cls)
 /**
  * Maximum count of elements we can put into a multipart message
  */
-#define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - sizeof (struct BobCryptodataMultipartMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
+#define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMultipartMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
 
 
 /**
@@ -593,6 +585,8 @@ transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
     GNUNET_MQ_send (s->cadet->cadet_mq,
                     e);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "All values queued for Alice, Bob is done\n");
 }
 
 
@@ -615,8 +609,9 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s)
   struct GNUNET_CRYPTO_PaillierCiphertext *payload;
   unsigned int i;
 
-  s->cadet_transmitted_element_count = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMessage)) /
-    (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * 2) - 2;
+  s->cadet_transmitted_element_count
+    = ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMessage))
+       / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) / 2) - 1;
   if (s->cadet_transmitted_element_count > s->used_element_count)
     s->cadet_transmitted_element_count = s->used_element_count;
 
@@ -659,6 +654,7 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s)
                   e);
   transmit_bobs_cryptodata_message_multipart (s);
 }
+#undef ELEMENT_CAPACITY
 
 
 /**
@@ -697,8 +693,9 @@ compute_square_sum (const gcry_mpi_t *vector,
  *     S': $S' := E_A(sum r_i^2)$
  *
  * @param request the requesting session + bob's requesting peer
+ * @return #GNUNET_OK on success
  */
-static void
+static int
 compute_service_response (struct BobServiceSession *session)
 {
   uint32_t i;
@@ -751,16 +748,22 @@ compute_service_response (struct BobServiceSession *session)
     // E(S - r_pi - b_pi)
     gcry_mpi_sub (tmp, my_offset, rand[p[i]]);
     gcry_mpi_sub (tmp, tmp, b[p[i]].value);
-    GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                    tmp,
-                                    2,
-                                    &r[i]);
+    GNUNET_assert (2 ==
+                   GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                   tmp,
+                                                   2,
+                                                   &r[i]));
 
     // E(S - r_pi - b_pi) * E(S + a_pi) ==  E(2*S + a - r - b)
-    GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
-                                    &r[i],
-                                    &a[p[i]],
-                                    &r[i]);
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
+                                        &r[i],
+                                        &a[p[i]],
+                                        &r[i]))
+    {
+      GNUNET_break_op (0);
+      goto error_cleanup;
+    }
   }
 
   // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
@@ -768,41 +771,48 @@ compute_service_response (struct BobServiceSession *session)
   {
     // E(S - r_qi)
     gcry_mpi_sub (tmp, my_offset, rand[q[i]]);
-    GNUNET_assert (2 == GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                                        tmp,
-                                                        2,
-                                                        &r_prime[i]));
+    GNUNET_assert (2 ==
+                   GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                   tmp,
+                                                   2,
+                                                   &r_prime[i]));
 
     // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
-    GNUNET_assert (1 == GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
-                                                        &r_prime[i],
-                                                        &a[q[i]],
-                                                        &r_prime[i]));
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
+                                        &r_prime[i],
+                                        &a[q[i]],
+                                        &r_prime[i]))
+    {
+      GNUNET_break_op (0);
+      goto error_cleanup;
+    }
   }
   gcry_mpi_release (tmp);
 
   // Calculate S' =  E(SUM( r_i^2 ))
   tmp = compute_square_sum (rand, count);
-  GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                  tmp,
-                                  1,
-                                  &session->s_prime);
+  GNUNET_assert (1 ==
+                 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                 tmp,
+                                                 1,
+                                                 &session->s_prime));
   gcry_mpi_release (tmp);
 
   // Calculate S = E(SUM( (r_i + b_i)^2 ))
   for (i = 0; i < count; i++)
     gcry_mpi_add (rand[i], rand[i], b[i].value);
   tmp = compute_square_sum (rand, count);
-  GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                  tmp,
-                                  1,
-                                  &session->s);
+  GNUNET_assert (1 ==
+                 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                 tmp,
+                                                 1,
+                                                 &session->s));
   gcry_mpi_release (tmp);
 
   session->r = r;
   session->r_prime = r_prime;
 
-  // release rand, b and a
   for (i = 0; i < count; i++)
     gcry_mpi_release (rand[i]);
   GNUNET_free (session->e_a);
@@ -810,8 +820,18 @@ compute_service_response (struct BobServiceSession *session)
   GNUNET_free (p);
   GNUNET_free (q);
   GNUNET_free (rand);
+  return GNUNET_OK;
 
-  // copy the r[], r_prime[], S and Stick into a new message, prepare_service_response frees these
+ error_cleanup:
+  GNUNET_free (r);
+  GNUNET_free (r_prime);
+  gcry_mpi_release (tmp);
+  GNUNET_free (p);
+  GNUNET_free (q);
+  for (i = 0; i < count; i++)
+    gcry_mpi_release (rand[i]);
+  GNUNET_free (rand);
+  return GNUNET_SYSERR;
 }
 
 
@@ -877,6 +897,8 @@ element_cmp (const void *a,
 static void
 transmit_cryptographic_reply (struct BobServiceSession *s)
 {
+  struct GNUNET_CADET_Channel *channel;
+
   /* TODO: code duplication with Alice! */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Received everything, building reply for Alice\n");
@@ -891,7 +913,14 @@ transmit_cryptographic_reply (struct BobServiceSession *s)
          s->used_element_count,
          sizeof (struct MpiElement),
          &element_cmp);
-  compute_service_response (s);
+  if (GNUNET_OK !=
+      compute_service_response (s))
+  {
+    channel = s->cadet->channel;
+    s->cadet->channel = NULL;
+    GNUNET_CADET_channel_destroy (channel);
+    return;
+  }
   transmit_bobs_cryptodata_message (s);
 }
 
@@ -1107,11 +1136,6 @@ handle_alices_computation_request (void *cls,
   struct BobServiceSession *s;
   const struct ServiceRequestMessage *msg;
 
-  if (ntohs (message->size) != sizeof (struct ServiceRequestMessage))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
   msg = (const struct ServiceRequestMessage *) message;
   if (GNUNET_YES == in->in_map)
   {
@@ -1237,8 +1261,6 @@ GSS_handle_bob_client_message_multipart (void *cls,
   elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
   for (i = 0; i < contained_count; i++)
   {
-    if (0 == GNUNET_ntohll (elements[i].value))
-      continue;
     elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
     memcpy (elem,
             &elements[i],
@@ -1413,15 +1435,13 @@ GSS_handle_bob_client_message (void *cls,
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Shutting down, initiating cleanup.\n");
-  // FIXME: do we have to cut our connections to CADET first?
+  // FIXME: we have to cut our connections to CADET first!
   if (NULL != my_cadet)
   {
     GNUNET_CADET_disconnect (my_cadet);
@@ -1452,8 +1472,7 @@ handle_client_disconnect (void *cls,
   if (NULL == client)
     return;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Client disconnected from us.\n",
-              client);
+              "Client disconnected from us.\n");
   s = GNUNET_SERVER_client_get_user_context (client,
                                              struct BobServiceSession);
   if (NULL == s)
@@ -1530,9 +1549,8 @@ run (void *cls,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &shutdown_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
 }