From db199dd2280071adafc14ff66cfa1c21399f06c2 Mon Sep 17 00:00:00 2001 From: jospaeth Date: Tue, 1 Sep 2020 19:20:07 +0200 Subject: change structure of struct GNUNET_ESCROW_Anchor encode method name and ego name in anchor string implement anchor_string_to_data and anchor_data_to_string in escrow_api.c (not plugin-specific) --- src/escrow/escrow_api.c | 173 +++++++++++++++++++++++++++++++---- src/escrow/gnunet-escrow.c | 17 +--- src/escrow/plugin_escrow_anastasis.c | 35 ------- src/escrow/plugin_escrow_gns.c | 38 +------- src/escrow/plugin_escrow_plaintext.c | 44 +-------- src/include/gnunet_escrow_lib.h | 37 +++++--- src/include/gnunet_escrow_plugin.h | 38 -------- 7 files changed, 191 insertions(+), 191 deletions(-) diff --git a/src/escrow/escrow_api.c b/src/escrow/escrow_api.c index e35ba3e9e..e48dc28e4 100644 --- a/src/escrow/escrow_api.c +++ b/src/escrow/escrow_api.c @@ -50,6 +50,30 @@ static int gns_initialized; static int anastasis_initialized; +/** + * Plaintext method string + */ +static const char *plaintext_string = "plaintext"; + + +/** + * GNS method string + */ +static const char *gns_string = "gns"; + + +/** + * Anastasis method string + */ +static const char *anastasis_string = "anastasis"; + + +/** + * None method string + */ +static const char *none_string = "INVALID-METHOD"; + + /** * Pointer to the plaintext plugin API */ @@ -414,43 +438,158 @@ GNUNET_ESCROW_get_status (struct GNUNET_ESCROW_Handle *h, * Deserialize an escrow anchor string (e.g. from command line) into a * GNUNET_ESCROW_Anchor struct * - * @param h the handle for the escrow component * @param anchorString the encoded escrow anchor string - * @param method the escrow method to use * * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, * NULL if we failed to parse the string */ struct GNUNET_ESCROW_Anchor * -GNUNET_ESCROW_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, - char *anchorString, - enum GNUNET_ESCROW_Key_Escrow_Method method) +GNUNET_ESCROW_anchor_string_to_data (const char *anchorString) { - const struct GNUNET_ESCROW_KeyPluginFunctions *api; - - api = init_plugin (h, method); - return api->anchor_string_to_data (h, anchorString); + struct GNUNET_ESCROW_Anchor *anchor; + uint32_t data_size; + char *anchorStringCopy, *ptr; + char *methodString, *egoNameString, *anchorDataString; + char delimiter[] = ":"; + + anchorStringCopy = GNUNET_strdup (anchorString); + anchor = NULL; + + /* parse and decode method */ + ptr = strtok (anchorStringCopy, delimiter); + if (NULL == ptr) + goto END; + GNUNET_STRINGS_urldecode (ptr, strlen (ptr), &methodString); + /* parse and decode ego name */ + ptr = strtok (NULL, delimiter); + if (NULL == ptr) + goto END; + GNUNET_STRINGS_urldecode (ptr, strlen (ptr), &egoNameString); + /* parse and decode anchor data */ + ptr = strtok (NULL, delimiter); + if (NULL == ptr) + goto END; + GNUNET_STRINGS_urldecode (ptr, strlen (ptr), &anchorDataString); + /* check if string is over */ + ptr = strtok (NULL, delimiter); + if (NULL != ptr) + goto END; + + data_size = strlen (anchorDataString); // data is NOT null-terminated + anchor = GNUNET_malloc (sizeof (struct GNUNET_ESCROW_Anchor) + + data_size + + strlen (egoNameString) + 1); + anchor->size = data_size; + anchor->method = GNUNET_ESCROW_method_string_to_number (methodString); + + // ptr is now used to fill the anchor + ptr = (char *)&anchor[1]; + strcpy (ptr, anchorDataString); + ptr += data_size; + anchor->egoName = ptr; + strcpy (ptr, egoNameString); + + END: + /* free all non-NULL strings */ + if (NULL != anchorStringCopy) + GNUNET_free (anchorStringCopy); + if (NULL != methodString) + GNUNET_free (methodString); + if (NULL != egoNameString) + GNUNET_free (egoNameString); + if (NULL != anchorDataString) + GNUNET_free (anchorDataString); + + return anchor; } /** * Serialize an escrow anchor (struct GNUNET_ESCROW_Anchor) into a string * - * @param h the handle for the escrow component * @param escrowAnchor the escrow anchor struct - * @param method the escrow method to use * * @return the encoded escrow anchor string */ char * -GNUNET_ESCROW_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, - struct GNUNET_ESCROW_Anchor *escrowAnchor, - enum GNUNET_ESCROW_Key_Escrow_Method method) +GNUNET_ESCROW_anchor_data_to_string (const struct GNUNET_ESCROW_Anchor *escrowAnchor) { - const struct GNUNET_ESCROW_KeyPluginFunctions *api; + char *anchorString, *ptr; + const char *methodString, *egoNameString, *anchorData; + char *methodStringEnc, *egoNameStringEnc, *anchorDataEnc; + + methodString = GNUNET_ESCROW_method_number_to_string (escrowAnchor->method); + GNUNET_STRINGS_urlencode (methodString, strlen (methodString), &methodStringEnc); + egoNameString = escrowAnchor->egoName; + GNUNET_STRINGS_urlencode (egoNameString, strlen (egoNameString), &egoNameStringEnc); + anchorData = (const char *)&escrowAnchor[1]; + GNUNET_STRINGS_urlencode (anchorData, escrowAnchor->size, &anchorDataEnc); + + anchorString = GNUNET_malloc (strlen (methodStringEnc) + 1 + + strlen (egoNameStringEnc) + 1 + + strlen (anchorDataEnc) + + 1); + + ptr = anchorString; + GNUNET_memcpy (ptr, methodStringEnc, strlen (methodStringEnc)); + ptr += strlen (methodStringEnc); + GNUNET_free (methodStringEnc); + *(ptr++) = ':'; + GNUNET_memcpy (ptr, egoNameStringEnc, strlen (egoNameStringEnc)); + ptr += strlen (egoNameStringEnc); + GNUNET_free (egoNameStringEnc); + *(ptr++) = ':'; + GNUNET_memcpy (ptr, anchorDataEnc, strlen (anchorDataEnc)); + ptr += strlen (anchorDataEnc); + GNUNET_free (anchorDataEnc); + *(ptr++) = '\0'; + + return anchorString; +} - api = init_plugin (h, method); - return api->anchor_data_to_string (h, escrowAnchor); + +/** + * Convert a method name string to the respective enum number + * + * @param methodString the method name string + * + * @return the enum number + */ +enum GNUNET_ESCROW_Key_Escrow_Method +GNUNET_ESCROW_method_string_to_number (const char *methodString) +{ + if (!strcmp (plaintext_string, methodString)) + return GNUNET_ESCROW_KEY_PLAINTEXT; + else if (!strcmp (gns_string, methodString)) + return GNUNET_ESCROW_KEY_GNS; + else if (!strcmp (anastasis_string, methodString)) + return GNUNET_ESCROW_KEY_ANASTASIS; + else + return GNUNET_ESCROW_KEY_NONE; +} + + +/** + * Convert a method enum number to the respective method string + * + * @param method the method enum number + * + * @return the method string + */ +const char * +GNUNET_ESCROW_method_number_to_string (enum GNUNET_ESCROW_Key_Escrow_Method method) +{ + switch (method) + { + case GNUNET_ESCROW_KEY_PLAINTEXT: + return plaintext_string; + case GNUNET_ESCROW_KEY_GNS: + return gns_string; + case GNUNET_ESCROW_KEY_ANASTASIS: + return anastasis_string; + default: + return none_string; + } } diff --git a/src/escrow/gnunet-escrow.c b/src/escrow/gnunet-escrow.c index 2d6d4d141..e66c43046 100644 --- a/src/escrow/gnunet-escrow.c +++ b/src/escrow/gnunet-escrow.c @@ -209,9 +209,7 @@ put_cb (void *cls, } else { - anchorString = GNUNET_ESCROW_anchor_data_to_string (escrow_handle, - escrowAnchor, - method); + anchorString = GNUNET_ESCROW_anchor_data_to_string (escrowAnchor); fprintf (stdout, "Escrow finished! Please keep the following anchor " "in order to restore the key later!\n%s\n", anchorString); @@ -458,13 +456,8 @@ run (void *cls, } /* determine method */ - if (!strcmp (plaintext_string, method_name)) - method = GNUNET_ESCROW_KEY_PLAINTEXT; - else if (!strcmp (gns_string, method_name)) - method = GNUNET_ESCROW_KEY_GNS; - else if (!strcmp (anastasis_string, method_name)) - method = GNUNET_ESCROW_KEY_ANASTASIS; - else + method = GNUNET_ESCROW_method_string_to_number (method_name); + if (GNUNET_ESCROW_KEY_NONE == method) { ret = 1; fprintf (stderr, _ ("unknown method name!\n")); @@ -476,9 +469,7 @@ run (void *cls, if (NULL != anchor_string) { /* parse anchor_string according to method */ - anchor = GNUNET_ESCROW_anchor_string_to_data (escrow_handle, - anchor_string, - method); + anchor = GNUNET_ESCROW_anchor_string_to_data (anchor_string); if (NULL == anchor) { ret = 1; diff --git a/src/escrow/plugin_escrow_anastasis.c b/src/escrow/plugin_escrow_anastasis.c index f4ab4dcbe..0c96d8d8d 100644 --- a/src/escrow/plugin_escrow_anastasis.c +++ b/src/escrow/plugin_escrow_anastasis.c @@ -152,40 +152,6 @@ anastasis_get_status (struct GNUNET_ESCROW_Handle *h, } -/** - * Deserialize an escrow anchor string into a GNUNET_ESCROW_Anchor struct - * - * @param anchorString the encoded escrow anchor string - * - * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, - * NULL if we failed to parse the string - */ -struct GNUNET_ESCROW_Anchor * -anastasis_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, - char *anchorString) -{ - return ESCROW_anchor_string_to_data (anchorString, - GNUNET_ESCROW_KEY_ANASTASIS); -} - - -/** - * Serialize an escrow anchor struct into a string - * - * @param h the handle for the escrow component - * @param escrowAnchor the escrow anchor struct - * - * @return the encoded escrow anchor string - */ -char * -anastasis_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, - struct GNUNET_ESCROW_Anchor *escrowAnchor) -{ - return ESCROW_anchor_data_to_string (escrowAnchor, - GNUNET_ESCROW_KEY_ANASTASIS); -} - - /** * Cancel an Anastasis plugin operation. * @@ -227,7 +193,6 @@ libgnunet_plugin_escrow_anastasis_init (void *cls) api->verify_key_escrow = &verify_anastasis_key_escrow; api->restore_key = &restore_anastasis_key_escrow; api->get_status = &anastasis_get_status; - api->anchor_string_to_data = &anastasis_anchor_string_to_data; api->cancel_plugin_operation = &cancel_anastasis_operation; ph.state = ESCROW_PLUGIN_STATE_INIT; diff --git a/src/escrow/plugin_escrow_gns.c b/src/escrow/plugin_escrow_gns.c index 121fdc4af..a3a3cc81b 100644 --- a/src/escrow/plugin_escrow_gns.c +++ b/src/escrow/plugin_escrow_gns.c @@ -1320,7 +1320,7 @@ get_user_secret_from_anchor (const struct GNUNET_ESCROW_Anchor *anchor) static void restore_private_key (struct ESCROW_PluginOperationWrapper *plugin_op_wrap, - struct GNUNET_ESCROW_Anchor *escrowAnchor, + struct GNUNET_ESCROW_Anchor *escrowAnchor, // TODO: use escrowAnchor?? PkContinuation cont, void *cont_cls) { @@ -1656,40 +1656,6 @@ gns_get_status (struct GNUNET_ESCROW_Handle *h, } -/** - * Deserialize an escrow anchor string into a GNUNET_ESCROW_Anchor struct - * - * @param anchorString the encoded escrow anchor string - * - * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, - * NULL if we failed to parse the string - */ -struct GNUNET_ESCROW_Anchor * -gns_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, - char *anchorString) -{ - return ESCROW_anchor_string_to_data (anchorString, - GNUNET_ESCROW_KEY_GNS); -} - - -/** - * Serialize an escrow anchor struct into a string - * - * @param h the handle for the escrow component - * @param escrowAnchor the escrow anchor struct - * - * @return the encoded escrow anchor string - */ -char * -gns_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, - struct GNUNET_ESCROW_Anchor *escrowAnchor) -{ - return ESCROW_anchor_data_to_string (escrowAnchor, - GNUNET_ESCROW_KEY_GNS); -} - - /** * Cancel a GNS plugin operation. * @@ -1742,8 +1708,6 @@ libgnunet_plugin_escrow_gns_init (void *cls) api->verify_key_escrow = &verify_gns_key_escrow; api->restore_key = &restore_gns_key_escrow; api->get_status = &gns_get_status; - api->anchor_string_to_data = &gns_anchor_string_to_data; - api->anchor_data_to_string = &gns_anchor_data_to_string; api->cancel_plugin_operation = &cancel_gns_operation; ph.state = ESCROW_PLUGIN_STATE_INIT; diff --git a/src/escrow/plugin_escrow_plaintext.c b/src/escrow/plugin_escrow_plaintext.c index 215c8127e..b26653667 100644 --- a/src/escrow/plugin_escrow_plaintext.c +++ b/src/escrow/plugin_escrow_plaintext.c @@ -266,8 +266,9 @@ verify_plaintext_key_escrow (struct GNUNET_ESCROW_Handle *h, } pk = GNUNET_IDENTITY_ego_get_private_key (ego); pkString = GNUNET_CRYPTO_ecdsa_private_key_to_string (pk); - verificationResult = strcmp (pkString, - (char *)&escrowAnchor[1]) == 0 ? + verificationResult = strncmp (pkString, + (char *)&escrowAnchor[1], + escrowAnchor->size) == 0 ? GNUNET_ESCROW_VALID : GNUNET_ESCROW_INVALID; w->verificationResult = verificationResult; @@ -408,7 +409,7 @@ restore_plaintext_key_escrow (struct GNUNET_ESCROW_Handle *h, } if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_private_key_from_string ((char *)&anchor[1], - strlen ((char *)&anchor[1]), + anchor->size, &pk)) { w->ego = NULL; @@ -444,41 +445,6 @@ plaintext_get_status (struct GNUNET_ESCROW_Handle *h, } -/** - * Deserialize an escrow anchor string into a GNUNET_ESCROW_Anchor struct - * - * @param h the handle for the escrow component - * @param anchorString the encoded escrow anchor string - * - * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, - * NULL if we failed to parse the string - */ -struct GNUNET_ESCROW_Anchor * -plaintext_anchor_string_to_data (struct GNUNET_ESCROW_Handle *h, - char *anchorString) -{ - return ESCROW_anchor_string_to_data (anchorString, - GNUNET_ESCROW_KEY_PLAINTEXT); -} - - -/** - * Serialize an escrow anchor struct into a string - * - * @param h the handle for the escrow component - * @param escrowAnchor the escrow anchor struct - * - * @return the encoded escrow anchor string - */ -char * -plaintext_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, - struct GNUNET_ESCROW_Anchor *escrowAnchor) -{ - return ESCROW_anchor_data_to_string (escrowAnchor, - GNUNET_ESCROW_KEY_PLAINTEXT); -} - - /** * Cancel a plaintext plugin operation. * @@ -537,8 +503,6 @@ libgnunet_plugin_escrow_plaintext_init (void *cls) api->verify_key_escrow = &verify_plaintext_key_escrow; api->restore_key = &restore_plaintext_key_escrow; api->get_status = &plaintext_get_status; - api->anchor_string_to_data = &plaintext_anchor_string_to_data; - api->anchor_data_to_string = &plaintext_anchor_data_to_string; api->cancel_plugin_operation = &cancel_plaintext_operation; ph.state = ESCROW_PLUGIN_STATE_INIT; diff --git a/src/include/gnunet_escrow_lib.h b/src/include/gnunet_escrow_lib.h index dddf5e030..f9135c28e 100644 --- a/src/include/gnunet_escrow_lib.h +++ b/src/include/gnunet_escrow_lib.h @@ -77,7 +77,7 @@ struct GNUNET_ESCROW_Anchor /** * The name of the ego that was put in escrow. */ - char *egoName; + const char *egoName; /** * The size of the anchor data. @@ -370,33 +370,48 @@ GNUNET_ESCROW_get_status ( * Deserialize an escrow anchor string (e.g. from command line) into a * GNUNET_ESCROW_Anchor struct * - * @param h the handle for the escrow component * @param anchorString the encoded escrow anchor string - * @param method the escrow method to use * * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, * NULL if we failed to parse the string */ struct GNUNET_ESCROW_Anchor * GNUNET_ESCROW_anchor_string_to_data ( - struct GNUNET_ESCROW_Handle *h, - char *anchorString, - enum GNUNET_ESCROW_Key_Escrow_Method method); + const char *anchorString); /** * Serialize an escrow anchor (struct GNUNET_ESCROW_Anchor) into a string * - * @param h the handle for the escrow component * @param escrowAnchor the escrow anchor struct - * @param method the escrow method to use * * @return the encoded escrow anchor string */ char * -GNUNET_ESCROW_anchor_data_to_string (struct GNUNET_ESCROW_Handle *h, - struct GNUNET_ESCROW_Anchor *escrowAnchor, - enum GNUNET_ESCROW_Key_Escrow_Method method); +GNUNET_ESCROW_anchor_data_to_string ( + const struct GNUNET_ESCROW_Anchor *escrowAnchor); + + +/** + * Convert a method name string to the respective enum number + * + * @param methodString the method name string + * + * @return the enum number + */ +enum GNUNET_ESCROW_Key_Escrow_Method +GNUNET_ESCROW_method_string_to_number (const char *methodString); + + +/** + * Convert a method enum number to the respective method string + * + * @param method the method enum number + * + * @return the method string + */ +const char * +GNUNET_ESCROW_method_number_to_string (enum GNUNET_ESCROW_Key_Escrow_Method method); /** diff --git a/src/include/gnunet_escrow_plugin.h b/src/include/gnunet_escrow_plugin.h index b8b88ff3f..dbdf6dce6 100644 --- a/src/include/gnunet_escrow_plugin.h +++ b/src/include/gnunet_escrow_plugin.h @@ -113,34 +113,6 @@ typedef struct GNUNET_ESCROW_Status *(*GNUNET_ESCROW_GetEscrowStatusFunction) ( struct GNUNET_IDENTITY_Ego *ego); -/** - * Function called to deserialize an escrow anchor string into a - * GNUNET_ESCROW_Anchor struct - * - * @param h the handle for the escrow component - * @param anchorString the encoded escrow anchor string - * - * @return the deserialized data packed into a GNUNET_ESCROW_Anchor struct, - * NULL if we failed to parse the string - */ -typedef struct GNUNET_ESCROW_Anchor *(*GNUNET_ESCROW_AnchorStringToDataFunction) ( - struct GNUNET_ESCROW_Handle *h, - char *anchorString); - - -/** - * Function called to serialize an escrow anchor struct into a string - * - * @param h the handle for the escrow component - * @param escrowAnchor the escrow anchor struct - * - * @return the encoded escrow anchor string - */ -typedef char *(*GNUNET_ESCROW_AnchorDataToStringFunction) ( - struct GNUNET_ESCROW_Handle *h, - struct GNUNET_ESCROW_Anchor *escrowAnchor); - - /** * Function called to cancel a plugin operation * @@ -181,16 +153,6 @@ struct GNUNET_ESCROW_KeyPluginFunctions */ GNUNET_ESCROW_GetEscrowStatusFunction get_status; - /** - * Deserialize anchor string to data - */ - GNUNET_ESCROW_AnchorStringToDataFunction anchor_string_to_data; - - /** - * Serialize anchor data to string - */ - GNUNET_ESCROW_AnchorDataToStringFunction anchor_data_to_string; - /** * Cancel plugin operation */ -- cgit v1.2.3