From 830137e312e8e565c9113a20411aa3073fbd1cbd Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 27 Feb 2017 15:28:00 +0100 Subject: [PATCH] implement revocation block plugin, get revocation test to pass again --- src/block/block.c | 2 +- src/block/plugin_block_template.c | 4 +- src/consensus/plugin_block_consensus.c | 2 +- src/dht/plugin_block_dht.c | 2 +- src/fs/plugin_block_fs.c | 2 +- src/gns/plugin_block_gns.c | 2 +- src/include/gnunet_block_lib.h | 5 + src/include/gnunet_plugin_lib.h | 2 +- src/regex/plugin_block_regex.c | 2 +- src/revocation/Makefile.am | 17 +- src/revocation/gnunet-service-revocation.c | 16 +- src/revocation/plugin_block_revocation.c | 257 +++++++++++++++++++++ src/set/plugin_block_set_test.c | 2 +- 13 files changed, 298 insertions(+), 17 deletions(-) create mode 100644 src/revocation/plugin_block_revocation.c diff --git a/src/block/block.c b/src/block/block.c index d6e61acf6..4b6f3826d 100644 --- a/src/block/block.c +++ b/src/block/block.c @@ -136,7 +136,7 @@ GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) ctx = GNUNET_new (struct GNUNET_BLOCK_Context); ctx->cfg = cfg; GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_", - NULL, + (void *) cfg, &add_plugin, ctx); return ctx; diff --git a/src/block/plugin_block_template.c b/src/block/plugin_block_template.c index 2edca17ec..0105fac38 100644 --- a/src/block/plugin_block_template.c +++ b/src/block/plugin_block_template.c @@ -157,6 +157,8 @@ block_plugin_template_get_key (void *cls, /** * Entry point for the plugin. + * + * @param cls a `const struct GNUNET_CONFIGURATION_Handle` */ void * libgnunet_plugin_block_template_init (void *cls) @@ -183,7 +185,7 @@ libgnunet_plugin_block_template_init (void *cls) void * libgnunet_plugin_block_template_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; diff --git a/src/consensus/plugin_block_consensus.c b/src/consensus/plugin_block_consensus.c index 0cb64867e..de0f0886f 100644 --- a/src/consensus/plugin_block_consensus.c +++ b/src/consensus/plugin_block_consensus.c @@ -128,7 +128,7 @@ libgnunet_plugin_block_consensus_init (void *cls) void * libgnunet_plugin_block_consensus_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; diff --git a/src/dht/plugin_block_dht.c b/src/dht/plugin_block_dht.c index 96349ae9d..24f8b21b2 100644 --- a/src/dht/plugin_block_dht.c +++ b/src/dht/plugin_block_dht.c @@ -237,7 +237,7 @@ libgnunet_plugin_block_dht_init (void *cls) void * libgnunet_plugin_block_dht_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; diff --git a/src/fs/plugin_block_fs.c b/src/fs/plugin_block_fs.c index c5e63c9e2..902519f15 100644 --- a/src/fs/plugin_block_fs.c +++ b/src/fs/plugin_block_fs.c @@ -276,7 +276,7 @@ libgnunet_plugin_block_fs_init (void *cls) void * libgnunet_plugin_block_fs_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; diff --git a/src/gns/plugin_block_gns.c b/src/gns/plugin_block_gns.c index 558ded6f4..a42fef953 100644 --- a/src/gns/plugin_block_gns.c +++ b/src/gns/plugin_block_gns.c @@ -238,7 +238,7 @@ libgnunet_plugin_block_gns_init (void *cls) void * libgnunet_plugin_block_gns_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; diff --git a/src/include/gnunet_block_lib.h b/src/include/gnunet_block_lib.h index 3e36f11f0..d8ee68c21 100644 --- a/src/include/gnunet_block_lib.h +++ b/src/include/gnunet_block_lib.h @@ -111,6 +111,11 @@ enum GNUNET_BLOCK_Type */ GNUNET_BLOCK_TYPE_GNS_NAMERECORD = 11, + /** + * Block type for a revocation message by which a key is revoked. + */ + GNUNET_BLOCK_TYPE_REVOCATION = 12, + /** * Block to store a cadet regex state */ diff --git a/src/include/gnunet_plugin_lib.h b/src/include/gnunet_plugin_lib.h index b76b9f9cf..9e3e72a21 100644 --- a/src/include/gnunet_plugin_lib.h +++ b/src/include/gnunet_plugin_lib.h @@ -87,7 +87,7 @@ GNUNET_PLUGIN_load (const char *library_name, /** - * Signature of a function called by 'GNUNET_PLUGIN_load_all'. + * Signature of a function called by #GNUNET_PLUGIN_load_all(). * * @param cls closure * @param library_name full name of the library (to be used with diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c index 0b3ef703e..287ecf905 100644 --- a/src/regex/plugin_block_regex.c +++ b/src/regex/plugin_block_regex.c @@ -395,7 +395,7 @@ libgnunet_plugin_block_regex_init (void *cls) void * libgnunet_plugin_block_regex_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; diff --git a/src/revocation/Makefile.am b/src/revocation/Makefile.am index 541c7120d..82755b485 100644 --- a/src/revocation/Makefile.am +++ b/src/revocation/Makefile.am @@ -1,6 +1,8 @@ # This Makefile.am is in the public domain AM_CPPFLAGS = -I$(top_srcdir)/src/include +plugindir = $(libdir)/gnunet + if MINGW WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols endif @@ -21,6 +23,20 @@ bin_PROGRAMS = \ gnunet-revocation +plugin_LTLIBRARIES = \ + libgnunet_plugin_block_revocation.la + +libgnunet_plugin_block_revocation_la_SOURCES = \ + plugin_block_revocation.c +libgnunet_plugin_block_revocation_la_LIBADD = \ + libgnunetrevocation.la \ + $(top_builddir)/src/block/libgnunetblockgroup.la \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(LTLIBINTL) +libgnunet_plugin_block_revocation_la_LDFLAGS = \ + $(GN_PLUGIN_LDFLAGS) + + gnunet_revocation_SOURCES = \ gnunet-revocation.c gnunet_revocation_LDADD = \ @@ -86,4 +102,3 @@ test_local_revocation.py: test_local_revocation.py.in Makefile EXTRA_DIST = test_revocation.conf \ test_local_revocation.py.in - diff --git a/src/revocation/gnunet-service-revocation.c b/src/revocation/gnunet-service-revocation.c index 99bec1fc8..9d077f874 100644 --- a/src/revocation/gnunet-service-revocation.c +++ b/src/revocation/gnunet-service-revocation.c @@ -38,6 +38,7 @@ #include "platform.h" #include #include "gnunet_util_lib.h" +#include "gnunet_block_lib.h" #include "gnunet_constants.h" #include "gnunet_protocols.h" #include "gnunet_signatures.h" @@ -215,7 +216,7 @@ client_connect_cb (void *cls, * @param client the new client * @param app_cls must alias @a client */ -static void +static void client_disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *client, void *app_cls) @@ -352,7 +353,7 @@ publicize_rm (const struct RevokeMessage *rm) GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); /* add to set for future connections */ e.size = htons (rm->header.size); - e.element_type = 0; + e.element_type = GNUNET_BLOCK_TYPE_REVOCATION; e.data = rm; if (GNUNET_OK != GNUNET_SET_add_element (revocation_set, @@ -452,11 +453,12 @@ add_revocation (void *cls, GNUNET_break_op (0); return; } - if (0 != element->element_type) + if (GNUNET_BLOCK_TYPE_REVOCATION != element->element_type) { GNUNET_STATISTICS_update (stats, gettext_noop ("# unsupported revocations received via set union"), - 1, GNUNET_NO); + 1, + GNUNET_NO); return; } rm = element->data; @@ -604,12 +606,12 @@ handle_core_disconnect (void *cls, void *internal_cls) { struct PeerEntry *peer_entry = internal_cls; - + if (0 == memcmp (peer, &my_identity, sizeof (my_identity))) return; - GNUNET_assert (NULL != peer_entry); + GNUNET_assert (NULL != peer_entry); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected from us\n", GNUNET_i2s (peer)); @@ -783,7 +785,7 @@ handle_revocation_union_request (void *cls, static void run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c, - struct GNUNET_SERVICE_Handle *service) + struct GNUNET_SERVICE_Handle *service) { struct GNUNET_MQ_MessageHandler core_handlers[] = { GNUNET_MQ_hd_fixed_size (p2p_revoke, diff --git a/src/revocation/plugin_block_revocation.c b/src/revocation/plugin_block_revocation.c new file mode 100644 index 000000000..eb0766b81 --- /dev/null +++ b/src/revocation/plugin_block_revocation.c @@ -0,0 +1,257 @@ +/* + This file is part of GNUnet + Copyright (C) 2017 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 + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + 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., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +/** + * @file block/plugin_block_revocation.c + * @brief revocation for a block plugin + * @author Christian Grothoff + */ + +#include "platform.h" +#include "gnunet_signatures.h" +#include "gnunet_block_plugin.h" +#include "gnunet_block_group_lib.h" +#include "revocation.h" +#include "gnunet_revocation_service.h" + +#define DEBUG_REVOCATION GNUNET_EXTRA_LOGGING + +/** + * Number of bits we set per entry in the bloomfilter. + * Do not change! + */ +#define BLOOMFILTER_K 16 + + +/** + * How big is the BF we use for DHT blocks? + */ +#define REVOCATION_BF_SIZE 8 + + +/** + * Context used inside the plugin. + */ +struct InternalContext +{ + + unsigned int matching_bits; + +}; + + +/** + * Create a new block group. + * + * @param ctx block context in which the block group is created + * @param type type of the block for which we are creating the group + * @param nonce random value used to seed the group creation + * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh + * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh + * @param va variable arguments specific to @a type + * @return block group handle, NULL if block groups are not supported + * by this @a type of block (this is not an error) + */ +static struct GNUNET_BLOCK_Group * +block_plugin_revocation_create_group (void *cls, + enum GNUNET_BLOCK_Type type, + uint32_t nonce, + const void *raw_data, + size_t raw_data_size, + va_list va) +{ + unsigned int bf_size; + const char *guard; + + guard = va_arg (va, const char *); + if (0 == strcmp (guard, + "seen-set-size")) + bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int), + BLOOMFILTER_K); + else if (0 == strcmp (guard, + "filter-size")) + bf_size = va_arg (va, unsigned int); + else + { + GNUNET_break (0); + bf_size = REVOCATION_BF_SIZE; + } + GNUNET_break (NULL == va_arg (va, const char *)); + return GNUNET_BLOCK_GROUP_bf_create (cls, + bf_size, + BLOOMFILTER_K, + type, + nonce, + raw_data, + raw_data_size); +} + + +/** + * Function called to validate a reply or a request. For + * request evaluation, simply pass "NULL" for the reply_block. + * + * @param cls our `struct InternalContext` + * @param ctx context + * @param type block type + * @param group block group to use + * @param eo control flags + * @param query original query (hash) + * @param xquery extrended query data (can be NULL, depending on type) + * @param xquery_size number of bytes in xquery + * @param reply_block response to validate + * @param reply_block_size number of bytes in reply block + * @return characterization of result + */ +static enum GNUNET_BLOCK_EvaluationResult +block_plugin_revocation_evaluate (void *cls, + struct GNUNET_BLOCK_Context *ctx, + enum GNUNET_BLOCK_Type type, + struct GNUNET_BLOCK_Group *group, + enum GNUNET_BLOCK_EvaluationOptions eo, + const struct GNUNET_HashCode *query, + const void *xquery, + size_t xquery_size, + const void *reply_block, + size_t reply_block_size) +{ + struct InternalContext *ic = cls; + struct GNUNET_HashCode chash; + const struct RevokeMessage *rm = reply_block; + + if (NULL == reply_block) + return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; + if (reply_block_size != sizeof (*rm)) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + } + if (GNUNET_YES != + GNUNET_REVOCATION_check_pow (&rm->public_key, + rm->proof_of_work, + ic->matching_bits)) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + } + if (GNUNET_OK != + GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION, + &rm->purpose, + &rm->signature, + &rm->public_key)) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + } + GNUNET_CRYPTO_hash (&rm->public_key, + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), + &chash); + if (GNUNET_YES == + GNUNET_BLOCK_GROUP_bf_test_and_set (group, + &chash)) + return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; + return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; +} + + +/** + * Function called to obtain the key for a block. + * + * @param cls closure + * @param type block type + * @param block block to get the key for + * @param block_size number of bytes in block + * @param key set to the key (query) for the given block + * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported + * (or if extracting a key from a block of this type does not work) + */ +static int +block_plugin_revocation_get_key (void *cls, + enum GNUNET_BLOCK_Type type, + const void *block, + size_t block_size, + struct GNUNET_HashCode *key) +{ + const struct RevokeMessage *rm = block; + + if (block_size != sizeof (*rm)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + GNUNET_CRYPTO_hash (&rm->public_key, + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), + key); + return GNUNET_OK; +} + + +/** + * Entry point for the plugin. + * + * @param cls the configuration to use + */ +void * +libgnunet_plugin_block_revocation_init (void *cls) +{ + static enum GNUNET_BLOCK_Type types[] = + { + GNUNET_BLOCK_TYPE_REVOCATION, + GNUNET_BLOCK_TYPE_ANY /* end of list */ + }; + const struct GNUNET_CONFIGURATION_Handle *cfg = cls; + struct GNUNET_BLOCK_PluginFunctions *api; + struct InternalContext *ic; + unsigned long long matching_bits; + + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (cfg, + "REVOCATION", + "WORKBITS", + &matching_bits)) + return NULL; + + api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); + api->evaluate = &block_plugin_revocation_evaluate; + api->get_key = &block_plugin_revocation_get_key; + api->create_group = &block_plugin_revocation_create_group; + api->types = types; + ic = GNUNET_new (struct InternalContext); + ic->matching_bits = (unsigned int) matching_bits; + api->cls = ic; + return api; +} + + +/** + * Exit point from the plugin. + */ +void * +libgnunet_plugin_block_revocation_done (void *cls) +{ + struct GNUNET_BLOCK_PluginFunctions *api = cls; + struct InternalContext *ic = api->cls; + + GNUNET_free (ic); + GNUNET_free (api); + return NULL; +} + +/* end of plugin_block_revocation.c */ diff --git a/src/set/plugin_block_set_test.c b/src/set/plugin_block_set_test.c index c4f73abe0..01b0c8602 100644 --- a/src/set/plugin_block_set_test.c +++ b/src/set/plugin_block_set_test.c @@ -114,7 +114,7 @@ libgnunet_plugin_block_set_test_init (void *cls) void * libgnunet_plugin_block_set_test_done (void *cls) { - struct GNUNET_TRANSPORT_PluginFunctions *api = cls; + struct GNUNET_BLOCK_PluginFunctions *api = cls; GNUNET_free (api); return NULL; -- 2.25.1