gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] branch master updated: fix truth serialization/deseria


From: gnunet
Subject: [taler-anastasis] branch master updated: fix truth serialization/deserialization
Date: Mon, 21 Dec 2020 14:42:24 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new 0b79edb  fix truth serialization/deserialization
0b79edb is described below

commit 0b79edba02752a7842e32974e0c297e24d614521
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Mon Dec 21 14:42:22 2020 +0100

    fix truth serialization/deserialization
---
 src/cli/anastasis-cli-redux.c        |   5 +-
 src/include/anastasis.h              |  26 +++---
 src/lib/anastasis.c                  | 149 ++++++++++++++++++++++-------------
 src/lib/anastasis_api_backup_redux.c | 141 +++++++++++++++++----------------
 4 files changed, 188 insertions(+), 133 deletions(-)

diff --git a/src/cli/anastasis-cli-redux.c b/src/cli/anastasis-cli-redux.c
index 8600fca..acc7f55 100644
--- a/src/cli/anastasis-cli-redux.c
+++ b/src/cli/anastasis-cli-redux.c
@@ -96,7 +96,7 @@ persist_new_state (json_t *state,
     if (0 !=
         json_dump_file (state,
                         filename,
-                        JSON_COMPACT))
+                        JSON_INDENT (2))) /* fixme: maybe use JSON_COMPACT for 
production? */
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   "Could not dump state to `%s'\n",
@@ -105,7 +105,8 @@ persist_new_state (json_t *state,
     }
     return;
   }
-  char *state_str = json_dumps (state, JSON_COMPACT);
+  char *state_str = json_dumps (state,
+                                JSON_INDENT (2)); /* fixme: maybe use 
JSON_COMPACT for production? */
   if (-1 >=
       fprintf (stdout,
                "%s",
diff --git a/src/include/anastasis.h b/src/include/anastasis.h
index 028f9fd..7bdb7c7 100644
--- a/src/include/anastasis.h
+++ b/src/include/anastasis.h
@@ -376,19 +376,27 @@ ANASTASIS_truth_upload_cancel (struct 
ANASTASIS_TruthUpload *tu);
 void
 ANASTASIS_truth_free (struct ANASTASIS_Truth *t);
 
+
 /**
-* Converts a truth object from string format.
-* @param t object to return size of
-*/
+ * Extracts truth data from JSON.
+ *
+ * @param json JSON encoding to decode; truth returned ONLY valid as long
+ *             as the JSON remains valid (do not decref until the truth
+ *             is truly finished)
+ * @return decoded truth object, NULL on error
+ */
 struct ANASTASIS_Truth *
-ANASTASIS_truth_from_string (const char*t_str);
+ANASTASIS_truth_from_json (const json_t *json);
+
 
 /**
-* Returns the string format of a truth object.
-* @param t object to return string of
-*/
-char *
-ANASTASIS_truth_to_string (struct ANASTASIS_Truth *t);
+ * Returns the size of a truth object.
+ *
+ * @param t object to return JSON encoding for
+ * @return JSON encoding of @a t
+ */
+json_t *
+ANASTASIS_truth_to_json (const struct ANASTASIS_Truth *t);
 
 /**
 * Policy object to upload
diff --git a/src/lib/anastasis.c b/src/lib/anastasis.c
index 2458aa1..05a9e07 100644
--- a/src/lib/anastasis.c
+++ b/src/lib/anastasis.c
@@ -1025,7 +1025,7 @@ struct ANASTASIS_Truth
   /**
    * server salt used to derive user identifier
    */
-  const struct ANASTASIS_CRYPTO_PowSalt *salt;
+  struct ANASTASIS_CRYPTO_PowSalt salt;
 };
 
 /**
@@ -1195,7 +1195,7 @@ ANASTASIS_truth_upload (struct GNUNET_CURL_Context *ctx,
   t->method = method;
   t->instructions = instructions;
   t->mime_type = mime_type;
-  t->salt = salt;
+  t->salt = *salt;
 
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                               &t->nonce,
@@ -1320,32 +1320,67 @@ ANASTASIS_truth_free (struct ANASTASIS_Truth *t)
 }
 
 
-/**
-* Returns the size of a truth object.
-* @param t object to return size of
-*/
-char *
-ANASTASIS_truth_to_string (struct ANASTASIS_Truth *t)
+json_t *
+ANASTASIS_truth_to_json (const struct ANASTASIS_Truth *t)
 {
-  return GNUNET_STRINGS_data_to_string_alloc (t,
-                                              sizeof (*t));
+  return json_pack ("{s:s,s:o,s:s,s:s,s:s,s:o,s:o,s:o}",
+                    "url",
+                    t->url,
+                    "nonce",
+                    GNUNET_JSON_from_data_auto (&t->nonce),
+                    "method",
+                    t->method,
+                    "instructions",
+                    t->instructions,
+                    "mime-type",
+                    t->mime_type,
+                    "key-share",
+                    GNUNET_JSON_from_data_auto (&t->key_share),
+                    "truth-key",
+                    GNUNET_JSON_from_data_auto (&t->truth_key),
+                    "salt",
+                    GNUNET_JSON_from_data_auto (&t->salt));
 }
 
 
-/**
-* Converts a truth object from string format.
-* @param t object to return size of
-*/
 struct ANASTASIS_Truth *
-ANASTASIS_truth_from_string (const char*t_str)
+ANASTASIS_truth_from_json (const json_t *json)
 {
   struct ANASTASIS_Truth *t = GNUNET_new (struct ANASTASIS_Truth);
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_STRINGS_string_to_data (
-                   t_str,
-                   strlen (t_str),
-                   t,
-                   sizeof (struct ANASTASIS_Truth)));
+  struct GNUNET_JSON_Specification spec[] = {
+    GNUNET_JSON_spec_string ("url",
+                             &t->url),
+    GNUNET_JSON_spec_fixed_auto ("nonce",
+                                 &t->nonce),
+    GNUNET_JSON_spec_string ("method",
+                             &t->method),
+    GNUNET_JSON_spec_string ("instructions",
+                             &t->instructions),
+    GNUNET_JSON_spec_string ("mime-type",
+                             &t->mime_type),
+    GNUNET_JSON_spec_fixed_auto ("key-share",
+                                 &t->key_share),
+    GNUNET_JSON_spec_fixed_auto ("truth-key",
+                                 &t->truth_key),
+    GNUNET_JSON_spec_fixed_auto ("salt",
+                                 &t->salt),
+    GNUNET_JSON_spec_end ()
+  };
+  const char *err_name;
+  unsigned int err_line;
+
+  if (GNUNET_OK !=
+      GNUNET_JSON_parse (json,
+                         spec,
+                         &err_name,
+                         &err_line))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to parse truth in line %u (%s)\n",
+                err_line,
+                err_name);
+    return NULL;
+  }
   return t;
 }
 
@@ -1431,8 +1466,6 @@ ANASTASIS_policy_create (const struct ANASTASIS_Truth 
*truths[],
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                               &p->salt,
                               sizeof (struct ANASTASIS_CRYPTO_SaltP));
-  GNUNET_assert (NULL != &p->salt);
-
   ANASTASIS_CRYPTO_policy_key_derive (key_shares,
                                       truths_len,
                                       &p->salt,
@@ -1929,18 +1962,23 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
   int index_pss = 0;
   for (unsigned int k = 0; k < policies_len; k++)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "At %s:%d policy is %s-%llu b\n", __FILE__, __LINE__,
-                TALER_B2S (policies[k]),
-                (unsigned long long) sizeof (struct ANASTASIS_Policy));
+    const struct ANASTASIS_Policy *policy = policies[k];
 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "At %s:%d policy is %s\n",
+                __FILE__,
+                __LINE__,
+                TALER_B2S (&policy->policy_key));
+    // FIXME: create 'nonces = json_array()' and
+    // simply append  GNUNET_JSON_from_data_auto(&policy->nonces[b]) to
+    // that array, keep the JSON structured!
 
-    char nonces[52 * policies[k]->nonces_length + 1];
-    for (unsigned int b = 0; b < policies[k]->nonces_length; b++)
+    char nonces[52 * policy->nonces_length + 1];
+    for (unsigned int b = 0; b < policy->nonces_length; b++)
     {
       char *nonce_str;
-      nonce_str = GNUNET_STRINGS_data_to_string_alloc (&policies[k]->nonces[b],
-                                                       sizeof (policies[k]->
+      nonce_str = GNUNET_STRINGS_data_to_string_alloc (&policy->nonces[b],
+                                                       sizeof (policy->
                                                                nonces[b]));
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "nonce is:  %s\n",
@@ -1951,7 +1989,7 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
       GNUNET_free (nonce_str);
     }
 
-    nonces[52 * policies[k]->nonces_length] = '\0';
+    nonces[52 * policy->nonces_length] = '\0';
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "At %s:%d nonces are %s b\n", __FILE__, __LINE__,
                 nonces);
@@ -1966,12 +2004,11 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
                      GNUNET_JSON_from_data_auto (
                        &encrypted_master_keys[k]),
                      "nonces",
-                     nonces,
+                     nonces, // FIXME: turn into JSON array!
                      "salt",
-                     GNUNET_JSON_from_data_auto (
-                       &policies[k]->salt),
+                     GNUNET_JSON_from_data_auto (&policy->salt),
                      "nonces_length",
-                     (int) policies[k]->nonces_length)))
+                     (int) policy->nonces_length)))
     {
       GNUNET_break (0);
       json_decref (dec_policies);
@@ -1979,34 +2016,34 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
     }
 
     // FIXME CHALLENGE
-    for (unsigned int l = 0; l < policies[k]->nonces_length; l++)
+    for (unsigned int l = 0; l < policy->nonces_length; l++)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "At %s:%d truth is %s-%llu b\n", __FILE__, __LINE__,
-                  TALER_B2S (policies[k]->truths[l]),
+                  TALER_B2S (policy->truths[l]),
                   (unsigned long long) sizeof (struct ANASTASIS_Truth));
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "At %s:%d keyshare is %s-%llu b\n", __FILE__, __LINE__,
-                  TALER_B2S (&policies[k]->truths[l]->key_share),
+                  TALER_B2S (&policy->truths[l]->key_share),
                   (unsigned long
-                   long) sizeof (policies[k]->truths[l]->key_share));
+                   long) sizeof (policy->truths[l]->key_share));
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "At %s:%d truthkey is %s-%llu b\n", __FILE__, __LINE__,
-                  TALER_B2S (&policies[k]->truths[l]->truth_key),
+                  TALER_B2S (&policy->truths[l]->truth_key),
                   (unsigned long
-                   long) sizeof (policies[k]->truths[l]->truth_key));
+                   long) sizeof (policy->truths[l]->truth_key));
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "At %s:%d nonce is %s-%llu b\n", __FILE__, __LINE__,
-                  TALER_B2S (&policies[k]->truths[l]->nonce),
-                  (unsigned long long) sizeof (policies[k]->truths[l]->nonce));
+                  TALER_B2S (&policy->truths[l]->nonce),
+                  (unsigned long long) sizeof (policy->truths[l]->nonce));
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "At %s:%d server salt is %s-%llu b\n", __FILE__, __LINE__,
-                  TALER_B2S (policies[k]->truths[l]->salt),
-                  (unsigned long long) sizeof (*policies[k]->truths[l]->salt));
+                  TALER_B2S (&policy->truths[l]->salt),
+                  (unsigned long long) sizeof (policy->truths[l]->salt));
 
       // FIXME: JUST APPEND UNIQUE NONCES!!!
       // creates a json array for saving
@@ -2021,17 +2058,17 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
                        " s:s}", /* escrow method */
                        "nonce",
                        GNUNET_JSON_from_data_auto (
-                         &policies[k]->truths[l]->nonce),
+                         &policy->truths[l]->nonce),
                        "url",
-                       policies[k]->truths[l]->url,
+                       policy->truths[l]->url,
                        "instructions",
-                       policies[k]->truths[l]->instructions,
+                       policy->truths[l]->instructions,
                        "truth_key", GNUNET_JSON_from_data_auto (
-                         &policies[k]->truths[l]->truth_key),
+                         &policy->truths[l]->truth_key),
                        "salt", GNUNET_JSON_from_data_auto (
-                         policies[k]->truths[l]->salt),
+                         &policy->truths[l]->salt),
                        "escrow_method",
-                       policies[k]->truths[l]->method)))
+                       policy->truths[l]->method)))
       {
         GNUNET_break (0);
         json_decref (esc_methods);
@@ -2045,7 +2082,7 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
         if (NULL != ss->pss[j]->anastasis_url)
         {
           if (0 == strcmp (ss->pss[j]->anastasis_url,
-                           policies[k]->truths[l]->url))
+                           policy->truths[l]->url))
           {
             contains_url = true;
             break;
@@ -2054,13 +2091,13 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
       }
       if (! contains_url)
       {
-        ss->pss[index_pss]->anastasis_url = policies[k]->truths[l]->url;
+        ss->pss[index_pss]->anastasis_url = policy->truths[l]->url;
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                     "At %s:%d anastasis url is %s\n", __FILE__, __LINE__,
                     ss->pss[index_pss]->anastasis_url);
         if (NULL != last_etag)
           ss->pss[index_pss]->prev_hash = *last_etag;
-        ss->pss[index_pss]->server_salt = policies[k]->truths[l]->salt;
+        ss->pss[index_pss]->server_salt = &policy->truths[l]->salt;
         if (pds_len > 0)
         {
           for (unsigned int m = 0; 0 < pds_len; m++)
@@ -2068,7 +2105,7 @@ ANASTASIS_secret_share (struct GNUNET_CURL_Context *ctx,
             if (NULL == pds[m].provider_url)
               continue;
             if (0 == strcmp (pds[m].provider_url,
-                             policies[k]->truths[l]->url))
+                             policy->truths[l]->url))
             {
               ss->pss[index_pss]->payment_secret
                 = pds[m].payment_secret;
diff --git a/src/lib/anastasis_api_backup_redux.c 
b/src/lib/anastasis_api_backup_redux.c
index a3e1bd9..88e1168 100644
--- a/src/lib/anastasis_api_backup_redux.c
+++ b/src/lib/anastasis_api_backup_redux.c
@@ -1205,7 +1205,7 @@ check_uploads (json_t *uploads,
                                                       "status"));
     if (pass && (status != 204))
       return false;
-    if ((status != 204)&&
+    if ((status != 204) &&
         (status != 402) )
     {
       return false;
@@ -1278,6 +1278,7 @@ initialize_payment_details (json_t *state,
   return pds_len;
 }
 
+
 /**
  * Initialize policies for recovery document.
  *
@@ -1328,10 +1329,7 @@ initialize_policies (json_t *state)
               json_object_get (json_truth, "auth_method_index"))
             == auth_method_index)
         {
-          const char *t_str = json_string_value (
-            json_object_get (tu,
-                             "truth_string"));
-          truths[truth_index] = ANASTASIS_truth_from_string (t_str);
+          truths[truth_index] = ANASTASIS_truth_from_json (tu);
           truth_index++;
           break;
         }
@@ -1689,8 +1687,11 @@ truth_upload_cb (void *cls,
                  struct ANASTASIS_Truth *t)
 {
   struct TruthUploadState *tus = cls;
-  tus->tuo = NULL;
+  json_t *p;
+  json_t *j;
+  json_t *truth_uploads;
 
+  tus->tuo = NULL;
   if (NULL == t)
   {
     ANASTASIS_redux_fail (tus->cb,
@@ -1699,22 +1700,28 @@ truth_upload_cb (void *cls,
                           "enter_secret or pay");
     return;
   }
-  json_t *truth_uploads = json_object_get (tus->state,
-                                           "truth_uploads");
-  json_t *truth_upload = json_array_get (truth_uploads,
-                                         tus->index);
-  GNUNET_assert (NULL != truth_upload);
-  GNUNET_assert (0 ==
-                 json_object_set (truth_upload,
-                                  "status",
-                                  json_integer ((json_int_t) 204)));
-  tus->truth = t;
-  char *truth_string = ANASTASIS_truth_to_string (tus->truth);
-  GNUNET_assert (0 ==
-                 json_object_set (truth_upload,
-                                  "truth_string",
-                                  json_string (truth_string)));
-  GNUNET_free (truth_string);
+  truth_uploads = json_object_get (tus->state,
+                                   "truth_uploads");
+  j = ANASTASIS_truth_to_json (tus->truth);
+  GNUNET_assert (NULL != j);
+  p = json_pack ("{s:I,s:o}",
+                 "status",
+                 (json_int_t) MHD_HTTP_NO_CONTENT,
+                 "truth",
+                 j);
+  GNUNET_assert (NULL != p);
+  {
+    char buf[13];
+
+    GNUNET_snprintf (buf,
+                     sizeof (buf),
+                     "%u",
+                     tus->index);
+    GNUNET_assert (0 ==
+                   json_object_set (truth_uploads,
+                                    buf,
+                                    p));
+  }
 
   if (check_uploads (truth_uploads, true) &&
       (json_array_size (
@@ -1747,19 +1754,29 @@ truth_payment_cb (void *cls,
 
   if (! ec)
   {
-    json_t *truth_upload = json_array_get (
-      json_object_get (tus->state,
-                       "truth_uploads"),
-      tus->index);
-    GNUNET_assert (NULL != truth_upload);
-    GNUNET_assert (0 ==
-                   json_object_set (truth_upload,
-                                    "status",
-                                    json_integer ((json_int_t) 402)));
-    GNUNET_assert (0 ==
-                   json_object_set (truth_upload,
-                                    "pay_url",
-                                    json_string (taler_pay_url)));
+    char buf[13];
+    json_t *truth_uploads;
+    json_t *p;
+
+    truth_uploads = json_object_get (tus->state,
+                                     "truth_uploads");
+    p = json_pack ("{s:I,s:s}",
+                   "status",
+                   (json_int_t) MHD_HTTP_PAYMENT_REQUIRED,
+                   "pay_url",
+                   taler_pay_url);
+    GNUNET_assert (NULL != p);
+    {
+      GNUNET_snprintf (buf,
+                       sizeof (buf),
+                       "%u",
+                       tus->index);
+      GNUNET_assert (0 ==
+                     json_object_set (truth_uploads,
+                                      buf,
+                                      p));
+
+    }
     set_state (tus->state,
                ANASTASIS_backup_state_to_string (
                  ANASTASIS_BACKUP_STATE_POLICIES_PAYING));
@@ -1802,9 +1819,8 @@ upload_truths (json_t *state,
   GNUNET_assert (json_is_array (truths));
   json_t *truth_uploads = json_object_get (state,
                                            "truth_uploads");
-
-  GNUNET_assert (json_is_array (truth_uploads));
-  size_t truth_uploads_size = json_array_size (truth_uploads);
+  GNUNET_assert (json_is_object (truth_uploads));
+  size_t truth_uploads_size = json_object_size (truth_uploads);
   size_t truths_size = json_array_size (truths);
 
   if (NULL != truth_indices)
@@ -1820,20 +1836,20 @@ upload_truths (json_t *state,
         {
           if (truth_index == tus_arr[i]->index)
           {
-            tus_arr[i]->tuo = ANASTASIS_truth_upload (tus_arr[i]->ctx,
-                                                      &tus_arr[i]->user_id,
-                                                      
tus_arr[i]->anastasis_url,
-                                                      tus_arr[i]->method,
-                                                      tus_arr[i]->instructions,
-                                                      tus_arr[i]->mime_type,
-                                                      
&tus_arr[i]->backend_salt,
-                                                      tus_arr[i]->truth_data,
-                                                      tus_arr[i]->
-                                                      truth_data_size,
-                                                      &truth_payment_cb,
-                                                      tus_arr[i],
-                                                      &truth_upload_cb,
-                                                      tus_arr[i]);
+            tus_arr[i]->tuo = ANASTASIS_truth_upload (
+              tus_arr[i]->ctx,
+              &tus_arr[i]->user_id,
+              tus_arr[i]->anastasis_url,
+              tus_arr[i]->method,
+              tus_arr[i]->instructions,
+              tus_arr[i]->mime_type,
+              &tus_arr[i]->backend_salt,
+              tus_arr[i]->truth_data,
+              tus_arr[i]->truth_data_size,
+              &truth_payment_cb,
+              tus_arr[i],
+              &truth_upload_cb,
+              tus_arr[i]);
             if (NULL == tus_arr[i]->tuo)
             {
               GNUNET_break (0);
@@ -1898,10 +1914,11 @@ upload_truths (json_t *state,
     }
     if (0 != truth_uploads_size)
     {
+      // FIXME: this seems redundant with the logic in 'enter_secret'.
       GNUNET_assert (0 ==
                      json_object_set (state,
                                       "truth_uploads",
-                                      json_array ()));
+                                      json_object ()));
     }
     json_array_foreach (truths, truth_index, truth)
     {
@@ -1938,15 +1955,6 @@ upload_truths (json_t *state,
         tus->truth_data_size = strlen (tus->truth_data);
       }
       // FIXME: other method types
-
-      json_t *truth_upload = json_pack ("{s:I, s:I}",
-                                        "status", (json_int_t) 0,
-                                        "truth_index",
-                                        (json_int_t) truth_index);
-      GNUNET_assert (0 ==
-                     json_array_append_new (json_object_get (state,
-                                                             "truth_uploads"),
-                                            truth_upload));
       ANASTASIS_CRYPTO_user_identifier_derive (tus->id_data,
                                                &tus->backend_salt,
                                                &tus->user_id);
@@ -2008,6 +2016,7 @@ enter_secret (json_t *state,
     cb (cb_cls,
         ANASTASIS_EC_INVALID,
         error);
+    json_decref (error);
     return NULL;
   }
   GNUNET_assert (NULL != state);
@@ -2024,20 +2033,20 @@ enter_secret (json_t *state,
     cb (cb_cls,
         ANASTASIS_EC_INVALID,
         error);
+    json_decref (error);
     return NULL;
   }
   GNUNET_assert (0 ==
                  json_object_set (state,
                                   "core_secret",
                                   json_deep_copy (arguments)));
-  json_t *truths = json_object_get (state,
-                                    "truths");
   GNUNET_assert (0 ==
                  json_object_set_new (state,
                                       "truth_uploads",
-                                      json_array ()));
+                                      json_object ()));
   upload_truths (state,
-                 truths,
+                 json_object_get (state,
+                                  "truths"),
                  ctx,
                  cb,
                  cb_cls);

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]