2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public Licerevocation as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public Licerevocation for more details.
15 You should have received a copy of the GNU General Public Licerevocation
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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 * - broadcast p2p revocations
35 * - handle p2p connect (trigger SET union)
36 * - optimization: avoid sending revocation back to peer that we got it from;
37 * - optimization: have randomized delay in sending revocations to other peers
38 * to make it rare to traverse each link twice (NSE-style)
42 #include "gnunet_util_lib.h"
43 #include "gnunet_constants.h"
44 #include "gnunet_protocols.h"
45 #include "gnunet_signatures.h"
46 #include "gnunet_statistics_service.h"
47 #include "gnunet_core_service.h"
48 #include "gnunet_revocation_service.h"
49 #include "gnunet_set_service.h"
50 #include "revocation.h"
55 * Per-peer information.
61 * Core handle for sending messages to this peer.
63 struct GNUNET_CORE_TransmitHandle *th;
66 * What is the identity of the peer?
68 struct GNUNET_PeerIdentity id;
71 * Task scheduled to send message to this peer.
73 GNUNET_SCHEDULER_TaskIdentifier transmit_task;
79 * Set from all revocations known to us.
81 static struct GNUNET_SET_Handle *revocation_set;
84 * Hash map with all revoked keys, maps the hash of the public key
85 * to the respective `struct RevokeMessage`.
87 static struct GNUNET_CONTAINER_MultiHashMap *revocation_map;
90 * Handle to our current configuration.
92 static const struct GNUNET_CONFIGURATION_Handle *cfg;
95 * Handle to the statistics service.
97 static struct GNUNET_STATISTICS_Handle *stats;
100 * Handle to the core service (for flooding)
102 static struct GNUNET_CORE_Handle *coreAPI;
105 * Map of all connected peers.
107 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
110 * The peer identity of this peer.
112 static struct GNUNET_PeerIdentity my_identity;
115 * Handle to this serivce's server.
117 static struct GNUNET_SERVER_Handle *srv;
120 * Notification context for convenient sending of replies to the clients.
122 static struct GNUNET_SERVER_NotificationContext *nc;
125 * File handle for the revocation database.
127 static struct GNUNET_DISK_FileHandle *revocation_db;
130 * Amount of work required (W-bit collisions) for REVOCATION proofs, in collision-bits.
132 static unsigned long long revocation_work_required;
136 * An revoke message has been received, check that it is well-formed.
138 * @param rm the message to verify
139 * @return #GNUNET_YES if the message is verified
140 * #GNUNET_NO if the key/signature don't verify
143 verify_revoke_message (const struct RevokeMessage *rm)
146 GNUNET_REVOCATION_check_pow (&rm->public_key,
148 (unsigned int) revocation_work_required))
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Proof of work invalid: %llu!\n",
153 GNUNET_ntohll (rm->proof_of_work));
158 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
171 * Handle QUERY message from client.
174 * @param client who sent the message
175 * @param message the message received
178 handle_query_message (void *cls,
179 struct GNUNET_SERVER_Client *client,
180 const struct GNUNET_MessageHeader *message)
182 const struct QueryMessage *qm = (const struct QueryMessage *) message;
183 struct QueryResponseMessage qrm;
184 struct GNUNET_HashCode hc;
187 GNUNET_CRYPTO_hash (&qm->key,
188 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
190 res = GNUNET_CONTAINER_multihashmap_contains (revocation_map, &hc);
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 ? "Received revocation check for valid key `%s' from client\n"
194 : "Received revocation check for revoked key `%s' from client\n",
196 qrm.header.size = htons (sizeof (struct QueryResponseMessage));
197 qrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE);
198 qrm.is_valid = htons ((GNUNET_YES == res) ? GNUNET_NO : GNUNET_YES);
199 GNUNET_SERVER_notification_context_add (nc,
201 GNUNET_SERVER_notification_context_unicast (nc,
205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
210 * Flood the given revocation message to all neighbours.
212 * @param cls the `struct RevokeMessage` to flood
213 * @param target a neighbour
214 * @param value our `struct PeerEntry` for the neighbour
215 * @return #GNUNET_OK (continue to iterate)
219 const struct GNUNET_PeerIdentity *target,
222 GNUNET_break (0); // FIXME: not implemented
228 * Publicize revocation message. Stores the message locally in the
229 * database and passes it to all connected neighbours (and adds it to
230 * the set for future connections).
232 * @param rm message to publicize
233 * @return #GNUNET_OK on success, #GNUNET_NO if we encountered an error,
234 * #GNUNET_SYSERR if the message was malformed
237 publicize_rm (const struct RevokeMessage *rm)
239 struct RevokeMessage *cp;
240 struct GNUNET_HashCode hc;
241 struct GNUNET_SET_Element e;
243 GNUNET_CRYPTO_hash (&rm->public_key,
244 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
247 GNUNET_CONTAINER_multihashmap_contains (revocation_map,
250 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
251 _("Duplicate revocation received from peer. Ignored.\n"));
255 verify_revoke_message (rm))
258 return GNUNET_SYSERR;
261 if (sizeof (struct RevokeMessage) !=
262 GNUNET_DISK_file_write (revocation_db,
264 sizeof (struct RevokeMessage)))
266 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
271 GNUNET_DISK_file_sync (revocation_db))
273 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
277 /* keep copy in memory */
278 cp = (struct RevokeMessage *) GNUNET_copy_message (&rm->header);
279 GNUNET_break (GNUNET_OK ==
280 GNUNET_CONTAINER_multihashmap_put (revocation_map,
283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
284 /* add to set for future connections */
285 e.size = htons (rm->header.size);
289 GNUNET_SET_add_element (revocation_set,
296 /* flood to neighbours */
297 GNUNET_CONTAINER_multipeermap_iterate (peers,
305 * Handle REVOKE message from client.
308 * @param client who sent the message
309 * @param message the message received
312 handle_revoke_message (void *cls,
313 struct GNUNET_SERVER_Client *client,
314 const struct GNUNET_MessageHeader *message)
316 const struct RevokeMessage *rm;
317 struct RevocationResponseMessage rrm;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Received REVOKE message from client\n");
322 rm = (const struct RevokeMessage *) message;
323 if (GNUNET_SYSERR == (ret = publicize_rm (rm)))
326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
329 rrm.header.size = htons (sizeof (struct RevocationResponseMessage));
330 rrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE);
331 rrm.is_valid = htons ((GNUNET_OK == ret) ? GNUNET_NO : GNUNET_YES);
332 GNUNET_SERVER_notification_context_add (nc,
334 GNUNET_SERVER_notification_context_unicast (nc,
338 GNUNET_SERVER_receive_done (client, GNUNET_OK);
343 * Core handler for flooded revocation messages.
345 * @param cls closure unused
346 * @param message message
347 * @param peer peer identity this message is from (ignored)
350 handle_p2p_revoke_message (void *cls,
351 const struct GNUNET_PeerIdentity *peer,
352 const struct GNUNET_MessageHeader *message)
354 const struct RevokeMessage *rm;
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Received REVOKE message from peer\n");
358 rm = (const struct RevokeMessage *) message;
359 GNUNET_break_op (GNUNET_SYSERR != publicize_rm (rm));
365 * Method called whenever a peer connects. Sets up the PeerEntry and
366 * schedules the initial revocation set exchange with this peer.
369 * @param peer peer identity this notification is about
372 handle_core_connect (void *cls,
373 const struct GNUNET_PeerIdentity *peer)
375 struct PeerEntry *peer_entry;
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected to us\n",
379 peer_entry = GNUNET_new (struct PeerEntry);
380 peer_entry->id = *peer;
381 GNUNET_assert (GNUNET_OK ==
382 GNUNET_CONTAINER_multipeermap_put (peers, peer,
384 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
385 GNUNET_break (0); // FIXME: implement revocation set union on connect!
387 peer_entry->transmit_task =
388 GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), &transmit_task_cb,
391 GNUNET_STATISTICS_update (stats, "# peers connected", 1, GNUNET_NO);
396 * Method called whenever a peer disconnects. Deletes the PeerEntry and cancels
397 * any pending transmission requests to that peer.
400 * @param peer peer identity this notification is about
403 handle_core_disconnect (void *cls,
404 const struct GNUNET_PeerIdentity *peer)
406 struct PeerEntry *pos;
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Peer `%s' disconnected from us\n",
411 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
417 GNUNET_assert (GNUNET_YES ==
418 GNUNET_CONTAINER_multipeermap_remove (peers, peer,
421 if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
423 GNUNET_SCHEDULER_cancel (pos->transmit_task);
424 pos->transmit_task = GNUNET_SCHEDULER_NO_TASK;
428 GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
433 GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
438 * Free all values in a hash map.
442 * @param value value to free
443 * @return #GNUNET_OK (continue to iterate)
446 free_entry (void *cls,
447 const struct GNUNET_HashCode *key,
456 * Task run during shutdown.
462 shutdown_task (void *cls,
463 const struct GNUNET_SCHEDULER_TaskContext *tc)
465 if (NULL != revocation_set)
467 GNUNET_SET_destroy (revocation_set);
468 revocation_set = NULL;
472 GNUNET_CORE_disconnect (coreAPI);
477 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
482 GNUNET_CONTAINER_multipeermap_destroy (peers);
487 GNUNET_SERVER_notification_context_destroy (nc);
490 if (NULL != revocation_db)
492 GNUNET_DISK_file_close (revocation_db);
493 revocation_db = NULL;
495 GNUNET_CONTAINER_multihashmap_iterate (revocation_map,
498 GNUNET_CONTAINER_multihashmap_destroy (revocation_map);
503 * Called on core init/fail.
505 * @param cls service closure
506 * @param identity the public identity of this peer
509 core_init (void *cls,
510 const struct GNUNET_PeerIdentity *identity)
512 if (NULL == identity)
514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515 "Connection to core FAILED!\n");
516 GNUNET_SCHEDULER_shutdown ();
519 my_identity = *identity;
524 * Handle network size estimate clients.
527 * @param server the initialized server
528 * @param c configuration to use
532 struct GNUNET_SERVER_Handle *server,
533 const struct GNUNET_CONFIGURATION_Handle *c)
535 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
536 {&handle_query_message, NULL, GNUNET_MESSAGE_TYPE_REVOCATION_QUERY,
537 sizeof (struct QueryMessage)},
538 {&handle_revoke_message, NULL, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
539 sizeof (struct RevokeMessage)},
542 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
543 {&handle_p2p_revoke_message, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
544 sizeof (struct RevokeMessage)},
549 struct RevokeMessage *rm;
550 struct GNUNET_HashCode hc;
553 GNUNET_CONFIGURATION_get_value_filename (c,
558 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
561 GNUNET_SCHEDULER_shutdown ();
566 revocation_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
567 nc = GNUNET_SERVER_notification_context_create (server, 1);
569 GNUNET_CONFIGURATION_get_value_number (cfg, "REVOCATION", "WORKBITS",
570 &revocation_work_required))
572 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
575 GNUNET_SCHEDULER_shutdown ();
579 if (revocation_work_required >= sizeof (struct GNUNET_HashCode) * 8)
581 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
584 _("Value is too large.\n"));
585 GNUNET_SCHEDULER_shutdown ();
589 revocation_set = GNUNET_SET_create (cfg,
590 GNUNET_SET_OPERATION_UNION);
592 revocation_db = GNUNET_DISK_file_open (fn,
593 GNUNET_DISK_OPEN_READWRITE |
594 GNUNET_DISK_OPEN_CREATE,
595 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE |
596 GNUNET_DISK_PERM_GROUP_READ |
597 GNUNET_DISK_PERM_OTHER_READ);
598 if (NULL == revocation_db)
600 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
603 _("Could not open revocation database file!"));
604 GNUNET_SCHEDULER_shutdown ();
609 GNUNET_DISK_file_size (fn, &left, GNUNET_YES, GNUNET_YES))
611 while (left > sizeof (struct RevokeMessage))
613 rm = GNUNET_new (struct RevokeMessage);
614 if (sizeof (struct RevokeMessage) !=
615 GNUNET_DISK_file_read (revocation_db,
617 sizeof (struct RevokeMessage)))
619 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
623 GNUNET_SCHEDULER_shutdown ();
627 GNUNET_break (0 == ntohl (rm->reserved));
628 GNUNET_CRYPTO_hash (&rm->public_key,
629 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
631 GNUNET_break (GNUNET_OK ==
632 GNUNET_CONTAINER_multihashmap_put (revocation_map,
635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
639 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
641 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
642 GNUNET_SERVER_add_handlers (srv, handlers);
643 /* Connect to core service and register core handlers */
644 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
645 NULL, /* Closure passed to functions */
646 &core_init, /* Call core_init once connected */
647 &handle_core_connect, /* Handle connects */
648 &handle_core_disconnect, /* Handle disconnects */
649 NULL, /* Don't want notified about all incoming messages */
650 GNUNET_NO, /* For header only inbound notification */
651 NULL, /* Don't want notified about all outbound messages */
652 GNUNET_NO, /* For header only outbound notification */
653 core_handlers); /* Register these handlers */
656 GNUNET_SCHEDULER_shutdown ();
659 stats = GNUNET_STATISTICS_create ("revocation", cfg);
664 * The main function for the network size estimation service.
666 * @param argc number of arguments from the command line
667 * @param argv command line arguments
668 * @return 0 ok, 1 on error
675 GNUNET_SERVICE_run (argc, argv, "revocation", GNUNET_SERVICE_OPTION_NONE,
676 &run, NULL)) ? 0 : 1;
684 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
686 void __attribute__ ((constructor))
687 GNUNET_ARM_memory_init ()
689 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
690 mallopt (M_TOP_PAD, 1 * 1024);
697 /* end of gnunet-service-revocation.c */