From 9c616547f2e77f01a3a8d1b50a764625e0269104 Mon Sep 17 00:00:00 2001 From: "Schanzenbach, Martin" Date: Fri, 17 Aug 2018 22:46:55 +0200 Subject: [PATCH] proving works --- src/include/gnunet_zklaim_service.h | 10 +++ src/zklaim/Makefile.am | 1 + src/zklaim/gnunet-service-zklaim.c | 98 +++++++++++++++++------------ src/zklaim/gnunet-zklaim.c | 20 ++++-- src/zklaim/zklaim_api.c | 87 +++++++++++++++++++------ src/zklaim/zklaim_functions.c | 5 ++ 6 files changed, 157 insertions(+), 64 deletions(-) diff --git a/src/include/gnunet_zklaim_service.h b/src/include/gnunet_zklaim_service.h index 011f2f9bb..7a46d0242 100644 --- a/src/include/gnunet_zklaim_service.h +++ b/src/include/gnunet_zklaim_service.h @@ -196,6 +196,16 @@ GNUNET_ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, GNUNET_ZKLAIM_PredicateIterator iter, void* iter_cls); +void +GNUNET_ZKLAIM_context_destroy (struct GNUNET_ZKLAIM_Context *ctx); + +int +GNUNET_ZKLAIM_context_prove_with_keyfile (struct GNUNET_ZKLAIM_Context *ctx, + const char* pkey_fn, + GNUNET_ZKLAIM_PredicateIterator iter, + void* iter_cls); + + #if 0 /* keep Emacsens' auto-indent happy */ { #endif diff --git a/src/zklaim/Makefile.am b/src/zklaim/Makefile.am index 22600c882..38c422c0c 100644 --- a/src/zklaim/Makefile.am +++ b/src/zklaim/Makefile.am @@ -48,6 +48,7 @@ libgnunetzklaim_la_LDFLAGS = \ gnunet_service_zklaim_SOURCES = \ gnunet-service-zklaim.c gnunet_service_zklaim_LDADD = \ + libgnunetzklaim.la \ $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/namestore/libgnunetnamestore.la \ diff --git a/src/zklaim/gnunet-service-zklaim.c b/src/zklaim/gnunet-service-zklaim.c index baf6b20de..0619432e6 100644 --- a/src/zklaim/gnunet-service-zklaim.c +++ b/src/zklaim/gnunet-service-zklaim.c @@ -29,7 +29,9 @@ #include "gnunet_gns_service.h" #include "gnunet_statistics_service.h" #include "gnunet_namestore_service.h" +#include "gnunet_zklaim_service.h" #include "zklaim_api.h" +#include "zklaim_functions.h" #include "zklaim/zklaim.h" /** @@ -52,6 +54,11 @@ static struct GNUNET_STATISTICS_Handle *stats; */ static const struct GNUNET_CONFIGURATION_Handle *cfg; +/** + * Proving key directory + */ +static char *pk_directory; + /** * An idp client */ @@ -123,16 +130,6 @@ struct CreateContextHandle */ struct GNUNET_NAMESTORE_QueueEntry *ns_qe; - /** - * The context name - */ - char *name; - - /** - * The attributes to support - */ - char *attrs; - }; struct LookupHandle @@ -193,6 +190,8 @@ cleanup() GNUNET_GNS_disconnect (gns_handle); if (NULL != ns_handle) GNUNET_NAMESTORE_disconnect (ns_handle); + GNUNET_free (pk_directory); + pk_directory = NULL; } /** @@ -220,8 +219,6 @@ cleanup_create_handle (struct CreateContextHandle *handle) { if (NULL != handle->ns_qe) GNUNET_NAMESTORE_cancel (handle->ns_qe); - GNUNET_free_non_null (handle->name); - GNUNET_free_non_null (handle->attrs); GNUNET_free (handle); } @@ -278,6 +275,18 @@ check_create_context_message(void *cls, return GNUNET_OK; } +static char* +get_pk_filename (char *ctx_name) +{ + char *filename; + + GNUNET_asprintf (&filename, + "%s%s%s", + pk_directory, + DIR_SEPARATOR_STR, + ctx_name); + return filename; +} static void handle_create_context_message (void *cls, @@ -286,17 +295,16 @@ handle_create_context_message (void *cls, struct CreateContextHandle *cch; struct ZkClient *zkc = cls; struct GNUNET_GNSRECORD_Data ctx_record; + struct GNUNET_ZKLAIM_Context *ctx; size_t str_len; char *tmp; char *pos; - unsigned char *data; char *rdata; - size_t data_len; + char *fn; size_t rdata_len; int num_attrs; int num_pl; int i; - zklaim_ctx *ctx; GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Received CREATE_REQUEST message\n"); @@ -304,10 +312,11 @@ handle_create_context_message (void *cls, str_len = ntohs (crm->name_len); cch = GNUNET_new (struct CreateContextHandle); - cch->name = GNUNET_strndup ((char*)&crm[1], str_len-1); + ctx = GNUNET_new (struct GNUNET_ZKLAIM_Context); + ctx->name = GNUNET_strndup ((char*)&crm[1], str_len-1); str_len = ntohs(crm->attrs_len); - fprintf(stderr, "%s\n", cch->name); - cch->attrs = GNUNET_strndup (((char*)&crm[1]) + strlen (cch->name) + 1, + fprintf(stderr, "%s\n", ctx->name); + ctx->attrs = GNUNET_strndup (((char*)&crm[1]) + strlen (ctx->name) + 1, str_len-1); cch->private_key = crm->private_key; GNUNET_CRYPTO_ecdsa_key_get_public (&crm->private_key, @@ -319,13 +328,14 @@ handle_create_context_message (void *cls, zkc->create_op_tail, cch); - tmp = GNUNET_strdup (cch->attrs); + tmp = GNUNET_strdup (ctx->attrs); pos = strtok(tmp, ","); num_attrs = 0; if (NULL == pos) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No attributes given.\n"); + GNUNET_ZKLAIM_context_destroy (ctx); send_result(GNUNET_SYSERR, cch); GNUNET_free (tmp); return; @@ -338,32 +348,34 @@ handle_create_context_message (void *cls, GNUNET_free (tmp); num_pl = (num_attrs / 5) + 1; zklaim_payload *pl = GNUNET_malloc (num_pl * sizeof (zklaim_payload)); - ctx = zklaim_context_new (); + ctx->ctx = zklaim_context_new (); for (i = 0; i < num_pl; i++) - zklaim_add_pl (ctx, pl[i]); - zklaim_hash_ctx (ctx); + zklaim_add_pl (ctx->ctx, pl[i]); + zklaim_hash_ctx (ctx->ctx); GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting trusted setup (%d payloads)... this might take a while...\n", num_pl); - if (0 != zklaim_trusted_setup (ctx)) + if (0 != zklaim_trusted_setup (ctx->ctx)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Trusted Setup failed.\n"); send_result (GNUNET_SYSERR, cch); - zklaim_ctx_free (ctx); + GNUNET_ZKLAIM_context_destroy (ctx); return; } GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Finished trusted setup.\n"); - data_len = zklaim_ctx_serialize (ctx, &data); - rdata_len = data_len + strlen (cch->attrs) + 1; - zklaim_ctx_free (ctx); - rdata = GNUNET_malloc (rdata_len); - memcpy (rdata, - cch->attrs, - strlen (cch->attrs) + 1); - memcpy (rdata + strlen (cch->attrs) + 1, - data, - data_len); + "Finished trusted setup. PK size=%lu bytes\n", + ctx->ctx->pk_size); + fn = get_pk_filename (ctx->name); + (void) GNUNET_DISK_directory_create_for_file (fn); + if (ctx->ctx->pk_size != GNUNET_DISK_fn_write (fn, + ctx->ctx->pk, + ctx->ctx->pk_size, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE)) + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, + "write", fn); + GNUNET_free (fn); + rdata_len = GNUNET_ZKLAIM_context_serialize (ctx, &rdata); ctx_record.data_size = rdata_len; ctx_record.data = rdata; ctx_record.expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us; //TODO config @@ -371,13 +383,13 @@ handle_create_context_message (void *cls, ctx_record.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; cch->ns_qe = GNUNET_NAMESTORE_records_store (ns_handle, &cch->private_key, - cch->name, + ctx->name, 1, &ctx_record, &context_store_cont, cch); GNUNET_free (rdata); - GNUNET_free (data); + GNUNET_ZKLAIM_context_destroy (ctx); } /** @@ -409,7 +421,7 @@ send_ctx_result (struct LookupHandle *lh, env = GNUNET_MQ_msg_extra (r_msg, len, GNUNET_MESSAGE_TYPE_ZKLAIM_RESULT_CTX); - r_msg->ctx_len = htonl (len); + r_msg->ctx_len = htons (len); memcpy ((char*)&r_msg[1], ctx, len); @@ -524,7 +536,15 @@ run (void *cls, { GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to namestore"); } - + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_filename (cfg, "zklaim", + "PKDIR", + &pk_directory)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "zklaim", "PKDIR"); + GNUNET_SCHEDULER_shutdown (); + return; + } gns_handle = GNUNET_GNS_connect (cfg); if (NULL == gns_handle) { diff --git a/src/zklaim/gnunet-zklaim.c b/src/zklaim/gnunet-zklaim.c index fb6695574..4146b983e 100644 --- a/src/zklaim/gnunet-zklaim.c +++ b/src/zklaim/gnunet-zklaim.c @@ -74,6 +74,11 @@ static char* create_attrs; */ static char* ego_name; +/** + * The proving key + */ +static char* pkey_fn; + /** * ZKLAIM handle */ @@ -257,7 +262,7 @@ prove_iter (void *cls, op = strtok (NULL, " "); if (NULL == op) break; - val = strtok (NULL, ";"); + val = strtok (NULL, ";"); if (NULL == val) break; if (0 != strcmp (name, attr)) @@ -320,10 +325,10 @@ handle_arguments () fprintf (stderr, "%s\n", prove_predicate); - - ret = GNUNET_ZKLAIM_context_prove (ctx, - &prove_iter, - NULL); + ret = GNUNET_ZKLAIM_context_prove_with_keyfile (ctx, + pkey_fn, + &prove_iter, + NULL); fprintf (stdout, "Prove result: %d\n", ret); } @@ -429,6 +434,11 @@ main(int argc, char *const argv[]) NULL, gettext_noop ("A credential"), &credential), + GNUNET_GETOPT_option_filename ('K', + "provingkey", + NULL, + gettext_noop ("The proving key to use"), + &pkey_fn), GNUNET_GETOPT_OPTION_END }; if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "ct", diff --git a/src/zklaim/zklaim_api.c b/src/zklaim/zklaim_api.c index 6ca94fda5..fb83cccb6 100644 --- a/src/zklaim/zklaim_api.c +++ b/src/zklaim/zklaim_api.c @@ -281,8 +281,8 @@ handle_zklaim_result_ctx (void *cls, { struct GNUNET_ZKLAIM_Handle *h = cls; struct GNUNET_ZKLAIM_Operation *op; - struct GNUNET_ZKLAIM_Context ctx; - uint16_t ctx_len = ntohl (cm->ctx_len); + struct GNUNET_ZKLAIM_Context *ctx; + uint16_t ctx_len = ntohs (cm->ctx_len); op = h->op_head; if (NULL == op) @@ -294,21 +294,22 @@ handle_zklaim_result_ctx (void *cls, GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, op); - ctx.attrs = (char*)&cm[1]; - ctx.ctx = zklaim_context_new (); - zklaim_ctx_deserialize (ctx.ctx, - (unsigned char *) &cm[1] + strlen (ctx.attrs) + 1, - ctx_len - strlen (ctx.attrs) - 1); if (NULL != op->ctx_cont) { - if (0 > ctx_len) + fprintf (stderr, + "ctx_len %d\n", ctx_len); + if (0 < ctx_len) + { + ctx = GNUNET_ZKLAIM_context_deserialize ((char*)&cm[1], + ctx_len); op->ctx_cont (op->cls, - &ctx); + ctx); + GNUNET_ZKLAIM_context_destroy (ctx); + } else op->ctx_cont (op->cls, - &ctx); + NULL); } - zklaim_ctx_free (ctx.ctx); GNUNET_free (op); } @@ -480,6 +481,15 @@ GNUNET_ZKLAIM_disconnect (struct GNUNET_ZKLAIM_Handle *h) GNUNET_free (h); } +void +GNUNET_ZKLAIM_context_destroy (struct GNUNET_ZKLAIM_Context *ctx) +{ + GNUNET_free_non_null (ctx->name); + GNUNET_free_non_null (ctx->attrs); + zklaim_ctx_free (ctx->ctx); + GNUNET_free (ctx); +} + /** * Lookup context */ @@ -543,18 +553,22 @@ GNUNET_ZKLAIM_context_serialize (const struct GNUNET_ZKLAIM_Context *ctx, char *pos; char *tmp; size_t len; - size_t len_w; + uint64_t len_w; size_t ret_len = 0; + size_t alen = strlen (ctx->attrs) + 1; + size_t nlen = strlen (ctx->name) + 1; len = zklaim_ctx_serialize (ctx->ctx, (unsigned char**) &tmp); - ret_len += strlen (ctx->attrs) + 1 + sizeof (size_t) + len; + ret_len += alen + nlen + sizeof (size_t) + len; *buf = GNUNET_malloc (ret_len); pos = *buf; - memcpy (pos, ctx->attrs, strlen (ctx->attrs) + 1); - pos += strlen (ctx->attrs) + 1; - len_w = htonl (len); - memcpy (pos, &len_w, sizeof (size_t)); - pos += sizeof (size_t); + memcpy (pos, ctx->attrs, alen); + pos += alen; + memcpy (pos, ctx->name, nlen); + pos += nlen; + len_w = htons (len); + memcpy (pos, &len_w, sizeof (uint16_t)); + pos += sizeof (uint16_t); memcpy (pos, tmp, len); GNUNET_free (tmp); return ret_len; @@ -572,9 +586,11 @@ GNUNET_ZKLAIM_context_deserialize (char *data, ctx = GNUNET_new (struct GNUNET_ZKLAIM_Context); ctx->attrs = GNUNET_strdup (data); pos = data + strlen (ctx->attrs) + 1; - len = ntohl (*((size_t*)pos)); + ctx->name = GNUNET_strdup (pos); + pos += strlen (ctx->name) + 1; + len = ntohs (*((uint16_t*)pos)); ctx->ctx = zklaim_context_new (); - pos += sizeof (size_t); + pos += sizeof (uint16_t); if (0 != zklaim_ctx_deserialize (ctx->ctx, (unsigned char*) pos, len)) @@ -582,6 +598,37 @@ GNUNET_ZKLAIM_context_deserialize (char *data, return ctx; } + +int +GNUNET_ZKLAIM_context_prove_with_keyfile (struct GNUNET_ZKLAIM_Context *ctx, + const char* pkey_fn, + GNUNET_ZKLAIM_PredicateIterator iter, + void* iter_cls) +{ + if (GNUNET_SYSERR == GNUNET_DISK_file_size (pkey_fn, + &ctx->ctx->pk_size, + GNUNET_NO, + GNUNET_YES)) + { + return GNUNET_SYSERR; + } + ctx->ctx->pk = GNUNET_malloc (ctx->ctx->pk_size); + if (GNUNET_SYSERR == GNUNET_DISK_fn_read (pkey_fn, + ctx->ctx->pk, + ctx->ctx->pk_size)) + { + GNUNET_free (ctx->ctx->pk); + ctx->ctx->pk = NULL; + ctx->ctx->pk_size = 0; + return GNUNET_SYSERR; + } + + return GNUNET_ZKLAIM_context_prove (ctx, + iter, + iter_cls); +} + + int GNUNET_ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, GNUNET_ZKLAIM_PredicateIterator iter, diff --git a/src/zklaim/zklaim_functions.c b/src/zklaim/zklaim_functions.c index e9e911623..c24549ee4 100644 --- a/src/zklaim/zklaim_functions.c +++ b/src/zklaim/zklaim_functions.c @@ -143,6 +143,11 @@ ZKLAIM_context_prove (struct GNUNET_ZKLAIM_Context *ctx, attr_name, &plw->pl.data_op[j], &plw->pl.data_ref[j]); + if ((attr_name - tmp) == (strlen (attr_name) + 1)) + { + attr_name = NULL; + break; + } attr_name = strtok (attr_name + strlen (attr_name) + 1, ","); } if (NULL == attr_name) -- 2.25.1