2 This file is part of GNUnet.
3 Copyright (C) 2013, 2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file revocation/gnunet-service-revocation.c
23 * @brief key revocation service
24 * @author Christian Grothoff
26 * The purpose of this service is to allow users to permanently revoke
27 * (compromised) keys. This is done by flooding the network with the
28 * revocation requests. To reduce the attack potential offered by such
29 * flooding, revocations must include a proof of work. We use the
30 * set service for efficiently computing the union of revocations of
34 * - optimization: avoid sending revocation back to peer that we got it from;
35 * - optimization: have randomized delay in sending revocations to other peers
36 * to make it rare to traverse each link twice (NSE-style)
40 #include "gnunet_util_lib.h"
41 #include "gnunet_block_lib.h"
42 #include "gnunet_constants.h"
43 #include "gnunet_protocols.h"
44 #include "gnunet_signatures.h"
45 #include "gnunet_statistics_service.h"
46 #include "gnunet_core_service.h"
47 #include "gnunet_revocation_service.h"
48 #include "gnunet_set_service.h"
49 #include "revocation.h"
54 * Per-peer information.
60 * Queue for sending messages to this peer.
62 struct GNUNET_MQ_Handle *mq;
65 * What is the identity of the peer?
67 struct GNUNET_PeerIdentity id;
70 * Tasked used to trigger the set union operation.
72 struct GNUNET_SCHEDULER_Task *transmit_task;
75 * Handle to active set union operation (over revocation sets).
77 struct GNUNET_SET_OperationHandle *so;
83 * Set from all revocations known to us.
85 static struct GNUNET_SET_Handle *revocation_set;
88 * Hash map with all revoked keys, maps the hash of the public key
89 * to the respective `struct RevokeMessage`.
91 static struct GNUNET_CONTAINER_MultiHashMap *revocation_map;
94 * Handle to our current configuration.
96 static const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Handle to the statistics service.
101 static struct GNUNET_STATISTICS_Handle *stats;
104 * Handle to the core service (for flooding)
106 static struct GNUNET_CORE_Handle *core_api;
109 * Map of all connected peers.
111 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
114 * The peer identity of this peer.
116 static struct GNUNET_PeerIdentity my_identity;
119 * File handle for the revocation database.
121 static struct GNUNET_DISK_FileHandle *revocation_db;
124 * Handle for us listening to incoming revocation set union requests.
126 static struct GNUNET_SET_ListenHandle *revocation_union_listen_handle;
129 * Amount of work required (W-bit collisions) for REVOCATION proofs, in collision-bits.
131 static unsigned long long revocation_work_required;
134 * Our application ID for set union operations. Must be the
135 * same for all (compatible) peers.
137 static struct GNUNET_HashCode revocation_set_union_app_id;
141 * Create a new PeerEntry and add it to the peers multipeermap.
143 * @param peer the peer identity
144 * @return a pointer to the new PeerEntry
146 static struct PeerEntry *
147 new_peer_entry(const struct GNUNET_PeerIdentity *peer)
149 struct PeerEntry *peer_entry;
151 peer_entry = GNUNET_new (struct PeerEntry);
152 peer_entry->id = *peer;
153 GNUNET_assert (GNUNET_OK ==
154 GNUNET_CONTAINER_multipeermap_put (peers,
157 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
163 * An revoke message has been received, check that it is well-formed.
165 * @param rm the message to verify
166 * @return #GNUNET_YES if the message is verified
167 * #GNUNET_NO if the key/signature don't verify
170 verify_revoke_message (const struct RevokeMessage *rm)
173 GNUNET_REVOCATION_check_pow (&rm->public_key,
175 (unsigned int) revocation_work_required))
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Proof of work invalid!\n");
183 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
196 * Handle client connecting to the service.
199 * @param client the new client
200 * @param mq the message queue of @a client
204 client_connect_cb (void *cls,
205 struct GNUNET_SERVICE_Client *client,
206 struct GNUNET_MQ_Handle *mq)
213 * Handle client connecting to the service.
216 * @param client the new client
217 * @param app_cls must alias @a client
220 client_disconnect_cb (void *cls,
221 struct GNUNET_SERVICE_Client *client,
224 GNUNET_assert (client == app_cls);
229 * Handle QUERY message from client.
231 * @param cls client who sent the message
232 * @param qm the message received
235 handle_query_message (void *cls,
236 const struct QueryMessage *qm)
238 struct GNUNET_SERVICE_Client *client = cls;
239 struct GNUNET_MQ_Envelope *env;
240 struct QueryResponseMessage *qrm;
241 struct GNUNET_HashCode hc;
244 GNUNET_CRYPTO_hash (&qm->key,
245 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
247 res = GNUNET_CONTAINER_multihashmap_contains (revocation_map,
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 ? "Received revocation check for valid key `%s' from client\n"
252 : "Received revocation check for revoked key `%s' from client\n",
254 env = GNUNET_MQ_msg (qrm,
255 GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE);
256 qrm->is_valid = htonl ((GNUNET_YES == res) ? GNUNET_NO : GNUNET_YES);
257 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
259 GNUNET_SERVICE_client_continue (client);
264 * Flood the given revocation message to all neighbours.
266 * @param cls the `struct RevokeMessage` to flood
267 * @param target a neighbour
268 * @param value our `struct PeerEntry` for the neighbour
269 * @return #GNUNET_OK (continue to iterate)
273 const struct GNUNET_PeerIdentity *target,
276 const struct RevokeMessage *rm = cls;
277 struct PeerEntry *pe = value;
278 struct GNUNET_MQ_Envelope *e;
279 struct RevokeMessage *cp;
282 return GNUNET_OK; /* peer connected to us via SET,
283 but we have no direct CORE
284 connection for flooding */
285 e = GNUNET_MQ_msg (cp,
286 GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Flooding revocation to `%s'\n",
290 GNUNET_i2s (target));
291 GNUNET_MQ_send (pe->mq,
298 * Publicize revocation message. Stores the message locally in the
299 * database and passes it to all connected neighbours (and adds it to
300 * the set for future connections).
302 * @param rm message to publicize
303 * @return #GNUNET_OK on success, #GNUNET_NO if we encountered an error,
304 * #GNUNET_SYSERR if the message was malformed
307 publicize_rm (const struct RevokeMessage *rm)
309 struct RevokeMessage *cp;
310 struct GNUNET_HashCode hc;
311 struct GNUNET_SET_Element e;
313 GNUNET_CRYPTO_hash (&rm->public_key,
314 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
317 GNUNET_CONTAINER_multihashmap_contains (revocation_map,
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Duplicate revocation received from peer. Ignored.\n");
325 verify_revoke_message (rm))
328 return GNUNET_SYSERR;
331 if (sizeof (struct RevokeMessage) !=
332 GNUNET_DISK_file_write (revocation_db,
334 sizeof (struct RevokeMessage)))
336 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
341 GNUNET_DISK_file_sync (revocation_db))
343 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
347 /* keep copy in memory */
348 cp = (struct RevokeMessage *) GNUNET_copy_message (&rm->header);
349 GNUNET_break (GNUNET_OK ==
350 GNUNET_CONTAINER_multihashmap_put (revocation_map,
353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
354 /* add to set for future connections */
355 e.size = htons (rm->header.size);
356 e.element_type = GNUNET_BLOCK_TYPE_REVOCATION;
359 GNUNET_SET_add_element (revocation_set,
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Added revocation info to SET\n");
372 /* flood to neighbours */
373 GNUNET_CONTAINER_multipeermap_iterate (peers,
381 * Handle REVOKE message from client.
383 * @param cls client who sent the message
384 * @param rm the message received
387 handle_revoke_message (void *cls,
388 const struct RevokeMessage *rm)
390 struct GNUNET_SERVICE_Client *client = cls;
391 struct GNUNET_MQ_Envelope *env;
392 struct RevocationResponseMessage *rrm;
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Received REVOKE message from client\n");
397 if (GNUNET_SYSERR == (ret = publicize_rm (rm)))
400 GNUNET_SERVICE_client_drop (client);
403 env = GNUNET_MQ_msg (rrm,
404 GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE);
405 rrm->is_valid = htonl ((GNUNET_OK == ret) ? GNUNET_NO : GNUNET_YES);
406 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
408 GNUNET_SERVICE_client_continue (client);
413 * Core handler for flooded revocation messages.
415 * @param cls closure unused
416 * @param rm revocation message
419 handle_p2p_revoke (void *cls,
420 const struct RevokeMessage *rm)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Received REVOKE message\n");
424 GNUNET_break_op (GNUNET_SYSERR !=
430 * Callback for set operation results. Called for each element in the
431 * result set. Each element contains a revocation, which we should
432 * validate and then add to our revocation list (and set).
435 * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
436 * @param current_size current set size
437 * @param status see `enum GNUNET_SET_Status`
440 add_revocation (void *cls,
441 const struct GNUNET_SET_Element *element,
442 uint64_t current_size,
443 enum GNUNET_SET_Status status)
445 struct PeerEntry *peer_entry = cls;
446 const struct RevokeMessage *rm;
450 case GNUNET_SET_STATUS_OK:
451 if (element->size != sizeof (struct RevokeMessage))
456 if (GNUNET_BLOCK_TYPE_REVOCATION != element->element_type)
458 GNUNET_STATISTICS_update (stats,
459 gettext_noop ("# unsupported revocations received via set union"),
465 (void) handle_p2p_revoke (NULL,
467 GNUNET_STATISTICS_update (stats,
468 gettext_noop ("# revocation messages received via set union"),
471 case GNUNET_SET_STATUS_FAILURE:
472 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
473 _("Error computing revocation set union with %s\n"),
474 GNUNET_i2s (&peer_entry->id));
475 peer_entry->so = NULL;
476 GNUNET_STATISTICS_update (stats,
477 gettext_noop ("# revocation set unions failed"),
481 case GNUNET_SET_STATUS_HALF_DONE:
483 case GNUNET_SET_STATUS_DONE:
484 peer_entry->so = NULL;
485 GNUNET_STATISTICS_update (stats,
486 gettext_noop ("# revocation set unions completed"),
498 * The timeout for performing the set union has expired,
499 * run the set operation on the revocation certificates.
504 transmit_task_cb (void *cls)
506 struct PeerEntry *peer_entry = cls;
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Starting set exchange with peer `%s'\n",
510 GNUNET_i2s (&peer_entry->id));
511 peer_entry->transmit_task = NULL;
512 GNUNET_assert (NULL == peer_entry->so);
513 peer_entry->so = GNUNET_SET_prepare (&peer_entry->id,
514 &revocation_set_union_app_id,
516 GNUNET_SET_RESULT_ADDED,
517 (struct GNUNET_SET_Option[]) {{ 0 }},
521 GNUNET_SET_commit (peer_entry->so,
524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
525 _("SET service crashed, terminating revocation service\n"));
526 GNUNET_SCHEDULER_shutdown ();
533 * Method called whenever a peer connects. Sets up the PeerEntry and
534 * schedules the initial revocation set exchange with this peer.
537 * @param peer peer identity this notification is about
540 handle_core_connect (void *cls,
541 const struct GNUNET_PeerIdentity *peer,
542 struct GNUNET_MQ_Handle *mq)
544 struct PeerEntry *peer_entry;
545 struct GNUNET_HashCode my_hash;
546 struct GNUNET_HashCode peer_hash;
548 if (0 == memcmp (peer,
550 sizeof (my_identity)))
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Peer `%s' connected to us\n",
558 GNUNET_STATISTICS_update (stats,
562 peer_entry = GNUNET_CONTAINER_multipeermap_get (peers,
564 if (NULL != peer_entry)
566 /* This can happen if "core"'s notification is a tad late
567 and CADET+SET were faster and already produced a
568 #handle_revocation_union_request() for us to deal
569 with. This should be rare, but isn't impossible. */
573 peer_entry = new_peer_entry (peer);
575 GNUNET_CRYPTO_hash (&my_identity,
576 sizeof (my_identity),
578 GNUNET_CRYPTO_hash (peer,
581 if (0 < GNUNET_CRYPTO_hash_cmp (&my_hash,
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 "Starting SET operation with peer `%s'\n",
587 peer_entry->transmit_task =
588 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
597 * Method called whenever a peer disconnects. Deletes the PeerEntry and cancels
598 * any pending transmission requests to that peer.
601 * @param peer peer identity this notification is about
602 * @param internal_cls our `struct PeerEntry` for this peer
605 handle_core_disconnect (void *cls,
606 const struct GNUNET_PeerIdentity *peer,
609 struct PeerEntry *peer_entry = internal_cls;
611 if (0 == memcmp (peer,
613 sizeof (my_identity)))
615 GNUNET_assert (NULL != peer_entry);
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Peer `%s' disconnected from us\n",
619 GNUNET_assert (GNUNET_YES ==
620 GNUNET_CONTAINER_multipeermap_remove (peers,
623 if (NULL != peer_entry->transmit_task)
625 GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
626 peer_entry->transmit_task = NULL;
628 if (NULL != peer_entry->so)
630 GNUNET_SET_operation_cancel (peer_entry->so);
631 peer_entry->so = NULL;
633 GNUNET_free (peer_entry);
634 GNUNET_STATISTICS_update (stats,
642 * Free all values in a hash map.
646 * @param value value to free
647 * @return #GNUNET_OK (continue to iterate)
650 free_entry (void *cls,
651 const struct GNUNET_HashCode *key,
660 * Task run during shutdown.
665 shutdown_task (void *cls)
667 if (NULL != revocation_set)
669 GNUNET_SET_destroy (revocation_set);
670 revocation_set = NULL;
672 if (NULL != revocation_union_listen_handle)
674 GNUNET_SET_listen_cancel (revocation_union_listen_handle);
675 revocation_union_listen_handle = NULL;
677 if (NULL != core_api)
679 GNUNET_CORE_disconnect (core_api);
684 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
689 GNUNET_CONTAINER_multipeermap_destroy (peers);
692 if (NULL != revocation_db)
694 GNUNET_DISK_file_close (revocation_db);
695 revocation_db = NULL;
697 GNUNET_CONTAINER_multihashmap_iterate (revocation_map,
700 GNUNET_CONTAINER_multihashmap_destroy (revocation_map);
705 * Called on core init/fail.
707 * @param cls service closure
708 * @param identity the public identity of this peer
711 core_init (void *cls,
712 const struct GNUNET_PeerIdentity *identity)
714 if (NULL == identity)
716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
717 "Connection to core FAILED!\n");
718 GNUNET_SCHEDULER_shutdown ();
721 my_identity = *identity;
726 * Called when another peer wants to do a set operation with the
727 * local peer. If a listen error occurs, the 'request' is NULL.
730 * @param other_peer the other peer
731 * @param context_msg message with application specific information from
733 * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
734 * to accept it, otherwise the request will be refused
735 * Note that we can't just return value from the listen callback,
736 * as it is also necessary to specify the set we want to do the
737 * operation with, whith sometimes can be derived from the context
738 * message. It's necessary to specify the timeout.
741 handle_revocation_union_request (void *cls,
742 const struct GNUNET_PeerIdentity *other_peer,
743 const struct GNUNET_MessageHeader *context_msg,
744 struct GNUNET_SET_Request *request)
746 struct PeerEntry *peer_entry;
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "Received set exchange request from peer `%s'\n",
755 GNUNET_i2s (other_peer));
756 peer_entry = GNUNET_CONTAINER_multipeermap_get (peers,
758 if (NULL == peer_entry)
760 peer_entry = new_peer_entry (other_peer);
762 if (NULL != peer_entry->so)
767 peer_entry->so = GNUNET_SET_accept (request,
768 GNUNET_SET_RESULT_ADDED,
769 (struct GNUNET_SET_Option[]) {{ 0 }},
773 GNUNET_SET_commit (peer_entry->so,
777 GNUNET_SCHEDULER_shutdown ();
784 * Handle network size estimate clients.
787 * @param server the initialized server
788 * @param c configuration to use
792 const struct GNUNET_CONFIGURATION_Handle *c,
793 struct GNUNET_SERVICE_Handle *service)
795 struct GNUNET_MQ_MessageHandler core_handlers[] = {
796 GNUNET_MQ_hd_fixed_size (p2p_revoke,
797 GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
798 struct RevokeMessage,
800 GNUNET_MQ_handler_end ()
804 struct RevokeMessage *rm;
805 struct GNUNET_HashCode hc;
807 GNUNET_CRYPTO_hash ("revocation-set-union-application-id",
808 strlen ("revocation-set-union-application-id"),
809 &revocation_set_union_app_id);
811 GNUNET_CONFIGURATION_get_value_filename (c,
816 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
819 GNUNET_SCHEDULER_shutdown ();
823 revocation_map = GNUNET_CONTAINER_multihashmap_create (16,
826 GNUNET_CONFIGURATION_get_value_number (cfg,
829 &revocation_work_required))
831 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
834 GNUNET_SCHEDULER_shutdown ();
838 if (revocation_work_required >= sizeof (struct GNUNET_HashCode) * 8)
840 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
843 _("Value is too large.\n"));
844 GNUNET_SCHEDULER_shutdown ();
848 revocation_set = GNUNET_SET_create (cfg,
849 GNUNET_SET_OPERATION_UNION);
850 revocation_union_listen_handle
851 = GNUNET_SET_listen (cfg,
852 GNUNET_SET_OPERATION_UNION,
853 &revocation_set_union_app_id,
854 &handle_revocation_union_request,
856 revocation_db = GNUNET_DISK_file_open (fn,
857 GNUNET_DISK_OPEN_READWRITE |
858 GNUNET_DISK_OPEN_CREATE,
859 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE |
860 GNUNET_DISK_PERM_GROUP_READ |
861 GNUNET_DISK_PERM_OTHER_READ);
862 if (NULL == revocation_db)
864 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
867 _("Could not open revocation database file!"));
868 GNUNET_SCHEDULER_shutdown ();
873 GNUNET_DISK_file_size (fn, &left, GNUNET_YES, GNUNET_YES))
875 while (left > sizeof (struct RevokeMessage))
877 rm = GNUNET_new (struct RevokeMessage);
878 if (sizeof (struct RevokeMessage) !=
879 GNUNET_DISK_file_read (revocation_db,
881 sizeof (struct RevokeMessage)))
883 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
887 GNUNET_SCHEDULER_shutdown ();
891 GNUNET_break (0 == ntohl (rm->reserved));
892 GNUNET_CRYPTO_hash (&rm->public_key,
893 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
895 GNUNET_break (GNUNET_OK ==
896 GNUNET_CONTAINER_multihashmap_put (revocation_map,
899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
903 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
905 peers = GNUNET_CONTAINER_multipeermap_create (128,
907 /* Connect to core service and register core handlers */
908 core_api = GNUNET_CORE_connect (cfg, /* Main configuration */
909 NULL, /* Closure passed to functions */
910 &core_init, /* Call core_init once connected */
911 &handle_core_connect, /* Handle connects */
912 &handle_core_disconnect, /* Handle disconnects */
913 core_handlers); /* Register these handlers */
914 if (NULL == core_api)
916 GNUNET_SCHEDULER_shutdown ();
919 stats = GNUNET_STATISTICS_create ("revocation",
925 * Define "main" method using service macro.
929 GNUNET_SERVICE_OPTION_NONE,
932 &client_disconnect_cb,
934 GNUNET_MQ_hd_fixed_size (query_message,
935 GNUNET_MESSAGE_TYPE_REVOCATION_QUERY,
938 GNUNET_MQ_hd_fixed_size (revoke_message,
939 GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
940 struct RevokeMessage,
942 GNUNET_MQ_handler_end ());
945 #if defined(LINUX) && defined(__GLIBC__)
949 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
951 void __attribute__ ((constructor))
952 GNUNET_REVOCATION_memory_init ()
954 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
955 mallopt (M_TOP_PAD, 1 * 1024);
962 /* end of gnunet-service-revocation.c */