2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License 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 License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file core/gnunet-service-core_kx.c
23 * @brief code for managing the key exchange (SET_KEY, PING, PONG) with other peers
24 * @author Christian Grothoff
27 #include "gnunet-service-core_kx.h"
28 #include "gnunet-service-core.h"
29 #include "gnunet-service-core_clients.h"
30 #include "gnunet-service-core_sessions.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_transport_core_service.h"
33 #include "gnunet_constants.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_protocols.h"
40 * How long do we wait for SET_KEY confirmation initially?
42 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
45 * What is the minimum frequency for a PING message?
47 #define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
50 * How often do we rekey?
52 #define REKEY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
55 * What time difference do we tolerate?
57 #define REKEY_TOLERANCE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
60 * What is the maximum age of a message for us to consider processing
61 * it? Note that this looks at the timestamp used by the other peer,
62 * so clock skew between machines does come into play here. So this
63 * should be picked high enough so that a little bit of clock skew
64 * does not prevent peers from connecting to us.
66 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
70 GNUNET_NETWORK_STRUCT_BEGIN
73 * Message transmitted with the signed ephemeral key of a peer. The
74 * session key is then derived from the two ephemeral keys (ECDHE).
76 struct EphemeralKeyMessage
80 * Message type is #GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY.
82 struct GNUNET_MessageHeader header;
85 * Status of the sender (should be in `enum PeerStateMachine`), nbo.
87 int32_t sender_status GNUNET_PACKED;
90 * An ECC signature of the @e origin_identity asserting the validity
91 * of the given ephemeral key.
93 struct GNUNET_CRYPTO_EddsaSignature signature;
96 * Information about what is being signed.
98 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
101 * At what time was this key created (beginning of validity).
103 struct GNUNET_TIME_AbsoluteNBO creation_time;
106 * When does the given ephemeral key expire (end of validity).
108 struct GNUNET_TIME_AbsoluteNBO expiration_time;
111 * Ephemeral public ECC key.
113 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
116 * Public key of the signing peer (persistent version, not the
117 * ephemeral public key).
119 struct GNUNET_PeerIdentity origin_identity;
125 * We're sending an (encrypted) PING to the other peer to check if he
126 * can decrypt. The other peer should respond with a PONG with the
127 * same content, except this time encrypted with the receiver's key.
132 * Message type is #GNUNET_MESSAGE_TYPE_CORE_PING.
134 struct GNUNET_MessageHeader header;
139 uint32_t iv_seed GNUNET_PACKED;
142 * Intended target of the PING, used primarily to check
143 * that decryption actually worked.
145 struct GNUNET_PeerIdentity target;
148 * Random number chosen to make replay harder.
150 uint32_t challenge GNUNET_PACKED;
155 * Response to a PING. Includes data from the original PING.
160 * Message type is #GNUNET_MESSAGE_TYPE_CORE_PONG.
162 struct GNUNET_MessageHeader header;
167 uint32_t iv_seed GNUNET_PACKED;
170 * Random number to make replay attacks harder.
172 uint32_t challenge GNUNET_PACKED;
175 * Reserved, always zero.
180 * Intended target of the PING, used primarily to check
181 * that decryption actually worked.
183 struct GNUNET_PeerIdentity target;
188 * Encapsulation for encrypted messages exchanged between
189 * peers. Followed by the actual encrypted data.
191 struct EncryptedMessage
194 * Message type is #GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
196 struct GNUNET_MessageHeader header;
199 * Random value used for IV generation.
201 uint32_t iv_seed GNUNET_PACKED;
204 * MAC of the encrypted message (starting at @e sequence_number),
205 * used to verify message integrity. Everything after this value
206 * (excluding this value itself) will be encrypted and
207 * authenticated. #ENCRYPTED_HEADER_SIZE must be set to the offset
208 * of the *next* field.
210 struct GNUNET_HashCode hmac;
213 * Sequence number, in network byte order. This field
214 * must be the first encrypted/decrypted field
216 uint32_t sequence_number GNUNET_PACKED;
219 * Reserved, always zero.
221 uint32_t reserved GNUNET_PACKED;
224 * Timestamp. Used to prevent replay of ancient messages
225 * (recent messages are caught with the sequence number).
227 struct GNUNET_TIME_AbsoluteNBO timestamp;
230 GNUNET_NETWORK_STRUCT_END
234 * Number of bytes (at the beginning) of `struct EncryptedMessage`
235 * that are NOT encrypted.
237 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
241 * Information about the status of a key exchange with another peer.
243 struct GSC_KeyExchangeInfo
249 struct GSC_KeyExchangeInfo *next;
254 struct GSC_KeyExchangeInfo *prev;
257 * Identity of the peer.
259 const struct GNUNET_PeerIdentity *peer;
262 * Message queue for sending messages to @a peer.
264 struct GNUNET_MQ_Handle *mq;
267 * PING message we transmit to the other peer.
269 struct PingMessage ping;
272 * Ephemeral public ECC key of the other peer.
274 struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key;
277 * Key we use to encrypt our messages for the other peer
278 * (initialized by us when we do the handshake).
280 struct GNUNET_CRYPTO_SymmetricSessionKey encrypt_key;
283 * Key we use to decrypt messages from the other peer
284 * (given to us by the other peer during the handshake).
286 struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key;
289 * At what time did the other peer generate the decryption key?
291 struct GNUNET_TIME_Absolute foreign_key_expires;
294 * When should the session time out (if there are no PONGs)?
296 struct GNUNET_TIME_Absolute timeout;
299 * What was the last timeout we informed our monitors about?
301 struct GNUNET_TIME_Absolute last_notify_timeout;
304 * At what frequency are we currently re-trying SET_KEY messages?
306 struct GNUNET_TIME_Relative set_key_retry_frequency;
309 * ID of task used for re-trying SET_KEY and PING message.
311 struct GNUNET_SCHEDULER_Task *retry_set_key_task;
314 * ID of task used for sending keep-alive pings.
316 struct GNUNET_SCHEDULER_Task *keep_alive_task;
319 * Bit map indicating which of the 32 sequence numbers before the
320 * last were received (good for accepting out-of-order packets and
321 * estimating reliability of the connection)
323 unsigned int last_packets_bitmap;
326 * last sequence number received on this connection (highest)
328 uint32_t last_sequence_number_received;
331 * last sequence number transmitted
333 uint32_t last_sequence_number_sent;
336 * What was our PING challenge number (for this peer)?
338 uint32_t ping_challenge;
341 * #GNUNET_YES if this peer currently has excess bandwidth.
343 int has_excess_bandwidth;
346 * What is our connection status?
348 enum GNUNET_CORE_KxState status;
356 static struct GNUNET_TRANSPORT_CoreHandle *transport;
361 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
364 * Our ephemeral private key.
366 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
369 * Current message we send for a key exchange.
371 static struct EphemeralKeyMessage current_ekm;
374 * Our message stream tokenizer (for encrypted payload).
376 static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
381 static struct GSC_KeyExchangeInfo *kx_head;
386 static struct GSC_KeyExchangeInfo *kx_tail;
389 * Task scheduled for periodic re-generation (and thus rekeying) of our
392 static struct GNUNET_SCHEDULER_Task *rekey_task;
395 * Notification context for all monitors.
397 static struct GNUNET_SERVER_NotificationContext *nc;
401 * Inform the given monitor about the KX state of
404 * @param client client to inform
405 * @param kx key exchange state to inform about
408 monitor_notify (struct GNUNET_SERVER_Client *client,
409 struct GSC_KeyExchangeInfo *kx)
411 struct MonitorNotifyMessage msg;
413 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
414 msg.header.size = htons (sizeof (msg));
415 msg.state = htonl ((uint32_t) kx->status);
416 msg.peer = *kx->peer;
417 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
418 GNUNET_SERVER_notification_context_unicast (nc,
426 * Calculate seed value we should use for a message.
428 * @param kx key exchange context
431 calculate_seed (struct GSC_KeyExchangeInfo *kx)
433 /* Note: may want to make this non-random and instead
434 derive from key material to avoid having an undetectable
436 return htonl (GNUNET_CRYPTO_random_u32
437 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
442 * Inform all monitors about the KX state of the given peer.
444 * @param kx key exchange state to inform about
447 monitor_notify_all (struct GSC_KeyExchangeInfo *kx)
449 struct MonitorNotifyMessage msg;
451 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
452 msg.header.size = htons (sizeof (msg));
453 msg.state = htonl ((uint32_t) kx->status);
454 msg.peer = *kx->peer;
455 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
456 GNUNET_SERVER_notification_context_broadcast (nc,
459 kx->last_notify_timeout = kx->timeout;
464 * Derive an authentication key from "set key" information
466 * @param akey authentication key to derive
467 * @param skey session key to use
468 * @param seed seed to use
471 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
472 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
475 static const char ctx[] = "authentication key";
477 GNUNET_CRYPTO_hmac_derive_key (akey, skey,
478 &seed, sizeof (seed),
479 skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
486 * Derive an IV from packet information
488 * @param iv initialization vector to initialize
489 * @param skey session key to use
490 * @param seed seed to use
491 * @param identity identity of the other peer to use
494 derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
495 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
497 const struct GNUNET_PeerIdentity *identity)
499 static const char ctx[] = "initialization vector";
501 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
502 &seed, sizeof (seed),
504 sizeof (struct GNUNET_PeerIdentity), ctx,
510 * Derive an IV from pong packet information
512 * @param iv initialization vector to initialize
513 * @param skey session key to use
514 * @param seed seed to use
515 * @param challenge nonce to use
516 * @param identity identity of the other peer to use
519 derive_pong_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
520 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
523 const struct GNUNET_PeerIdentity *identity)
525 static const char ctx[] = "pong initialization vector";
527 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
528 &seed, sizeof (seed),
530 sizeof (struct GNUNET_PeerIdentity),
531 &challenge, sizeof (challenge),
538 * Derive an AES key from key material
540 * @param sender peer identity of the sender
541 * @param receiver peer identity of the sender
542 * @param key_material high entropy key material to use
543 * @param skey set to derived session key
546 derive_aes_key (const struct GNUNET_PeerIdentity *sender,
547 const struct GNUNET_PeerIdentity *receiver,
548 const struct GNUNET_HashCode *key_material,
549 struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
551 static const char ctx[] = "aes key generation vector";
553 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
555 key_material, sizeof (struct GNUNET_HashCode),
556 sender, sizeof (struct GNUNET_PeerIdentity),
557 receiver, sizeof (struct GNUNET_PeerIdentity),
563 * Encrypt size bytes from @a in and write the result to @a out. Use the
564 * @a kx key for outbound traffic of the given neighbour.
566 * @param kx key information context
567 * @param iv initialization vector to use
568 * @param in ciphertext
569 * @param out plaintext
570 * @param size size of @a in/@a out
571 * @return #GNUNET_OK on success
574 do_encrypt (struct GSC_KeyExchangeInfo *kx,
575 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
580 if (size != (uint16_t) size)
585 GNUNET_assert (size ==
586 GNUNET_CRYPTO_symmetric_encrypt (in,
591 GNUNET_STATISTICS_update (GSC_stats,
592 gettext_noop ("# bytes encrypted"),
595 /* the following is too sensitive to write to log files by accident,
596 so we require manual intervention to get this one... */
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Encrypted %u bytes for `%s' using key %u, IV %u\n",
601 GNUNET_i2s (kx->peer),
602 (unsigned int) kx->encrypt_key.crc32,
603 GNUNET_CRYPTO_crc32_n (iv,
611 * Decrypt size bytes from @a in and write the result to @a out. Use
612 * the @a kx key for inbound traffic of the given neighbour. This
613 * function does NOT do any integrity-checks on the result.
615 * @param kx key information context
616 * @param iv initialization vector to use
617 * @param in ciphertext
618 * @param out plaintext
619 * @param size size of @a in / @a out
620 * @return #GNUNET_OK on success
623 do_decrypt (struct GSC_KeyExchangeInfo *kx,
624 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
629 if (size != (uint16_t) size)
634 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
635 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
636 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT) )
639 return GNUNET_SYSERR;
642 GNUNET_CRYPTO_symmetric_decrypt (in,
649 return GNUNET_SYSERR;
651 GNUNET_STATISTICS_update (GSC_stats,
652 gettext_noop ("# bytes decrypted"),
655 /* the following is too sensitive to write to log files by accident,
656 so we require manual intervention to get this one... */
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Decrypted %u bytes from `%s' using key %u, IV %u\n",
661 GNUNET_i2s (kx->peer),
662 (unsigned int) kx->decrypt_key.crc32,
663 GNUNET_CRYPTO_crc32_n (iv,
672 * Send our key (and encrypted PING) to the other peer.
674 * @param kx key exchange context
677 send_key (struct GSC_KeyExchangeInfo *kx);
681 * Task that will retry #send_key() if our previous attempt failed.
683 * @param cls our `struct GSC_KeyExchangeInfo`
686 set_key_retry_task (void *cls)
688 struct GSC_KeyExchangeInfo *kx = cls;
690 kx->retry_set_key_task = NULL;
691 kx->set_key_retry_frequency = GNUNET_TIME_STD_BACKOFF (kx->set_key_retry_frequency);
692 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
698 * Create a fresh PING message for transmission to the other peer.
700 * @param kx key exchange context to create PING for
703 setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
705 struct PingMessage pp;
706 struct PingMessage *pm;
707 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
710 kx->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
712 pm->header.size = htons (sizeof (struct PingMessage));
713 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
714 pm->iv_seed = calculate_seed (kx);
719 pp.challenge = kx->ping_challenge;
720 pp.target = *kx->peer;
725 sizeof (struct PingMessage) - ((void *) &pm->target -
731 * Function called by transport to notify us that
732 * a peer connected to us (on the network level).
733 * Starts the key exchange with the given peer.
735 * @param cls closure (NULL)
736 * @param pid identity of the peer to do a key exchange with
737 * @return key exchange information context
740 handle_transport_notify_connect (void *cls,
741 const struct GNUNET_PeerIdentity *pid,
742 struct GNUNET_MQ_Handle *mq)
744 struct GSC_KeyExchangeInfo *kx;
745 struct GNUNET_HashCode h1;
746 struct GNUNET_HashCode h2;
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "Initiating key exchange with `%s'\n",
751 GNUNET_STATISTICS_update (GSC_stats,
752 gettext_noop ("# key exchanges initiated"),
755 kx = GNUNET_new (struct GSC_KeyExchangeInfo);
758 kx->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
759 GNUNET_CONTAINER_DLL_insert (kx_head,
762 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
763 monitor_notify_all (kx);
764 GNUNET_CRYPTO_hash (pid,
765 sizeof (struct GNUNET_PeerIdentity),
767 GNUNET_CRYPTO_hash (&GSC_my_identity,
768 sizeof (struct GNUNET_PeerIdentity),
770 if (0 < GNUNET_CRYPTO_hash_cmp (&h1,
773 /* peer with "lower" identity starts KX, otherwise we typically end up
774 with both peers starting the exchange and transmit the 'set key'
780 /* peer with "higher" identity starts a delayed KX, if the "lower" peer
781 * does not start a KX since he sees no reasons to do so */
782 kx->retry_set_key_task
783 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
792 * Function called by transport telling us that a peer
794 * Stop key exchange with the given peer. Clean up key material.
797 * @param peer the peer that disconnected
798 * @param handler_cls the `struct GSC_KeyExchangeInfo` of the peer
801 handle_transport_notify_disconnect (void *cls,
802 const struct GNUNET_PeerIdentity *peer,
805 struct GSC_KeyExchangeInfo *kx = handler_cls;
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Peer `%s' disconnected from us.\n",
810 GSC_SESSIONS_end (kx->peer);
811 GNUNET_STATISTICS_update (GSC_stats,
812 gettext_noop ("# key exchanges stopped"),
815 if (NULL != kx->retry_set_key_task)
817 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
818 kx->retry_set_key_task = NULL;
820 if (NULL != kx->keep_alive_task)
822 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
823 kx->keep_alive_task = NULL;
825 kx->status = GNUNET_CORE_KX_PEER_DISCONNECT;
826 monitor_notify_all (kx);
827 GNUNET_CONTAINER_DLL_remove (kx_head,
835 * Send our PING to the other peer.
837 * @param kx key exchange context
840 send_ping (struct GSC_KeyExchangeInfo *kx)
842 struct GNUNET_MQ_Envelope *env;
844 GNUNET_STATISTICS_update (GSC_stats,
845 gettext_noop ("# PING messages transmitted"),
848 env = GNUNET_MQ_msg_copy (&kx->ping.header);
849 GNUNET_MQ_send (kx->mq,
855 * Derive fresh session keys from the current ephemeral keys.
857 * @param kx session to derive keys for
860 derive_session_keys (struct GSC_KeyExchangeInfo *kx)
862 struct GNUNET_HashCode key_material;
865 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
866 &kx->other_ephemeral_key,
872 derive_aes_key (&GSC_my_identity,
876 derive_aes_key (kx->peer,
880 memset (&key_material, 0, sizeof (key_material));
881 /* fresh key, reset sequence numbers */
882 kx->last_sequence_number_received = 0;
883 kx->last_packets_bitmap = 0;
884 setup_fresh_ping (kx);
889 * We received a #GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY message.
890 * Validate and update our key material and status.
892 * @param cls key exchange status for the corresponding peer
893 * @param m the set key message we received
896 handle_ephemeral_key (void *cls,
897 const struct EphemeralKeyMessage *m)
899 struct GSC_KeyExchangeInfo *kx = cls;
900 struct GNUNET_TIME_Absolute start_t;
901 struct GNUNET_TIME_Absolute end_t;
902 struct GNUNET_TIME_Absolute now;
903 enum GNUNET_CORE_KxState sender_status;
905 end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
906 if ( ( (GNUNET_CORE_KX_STATE_KEY_RECEIVED == kx->status) ||
907 (GNUNET_CORE_KX_STATE_UP == kx->status) ||
908 (GNUNET_CORE_KX_STATE_REKEY_SENT == kx->status) ) &&
909 (end_t.abs_value_us < kx->foreign_key_expires.abs_value_us) )
911 GNUNET_STATISTICS_update (GSC_stats,
912 gettext_noop ("# old ephemeral keys ignored"),
916 start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
918 GNUNET_STATISTICS_update (GSC_stats,
919 gettext_noop ("# ephemeral keys received"),
923 memcmp (&m->origin_identity,
925 sizeof (struct GNUNET_PeerIdentity)))
927 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
928 "Received EPHEMERAL_KEY from %s, but expected %s\n",
929 GNUNET_i2s (&m->origin_identity),
930 GNUNET_i2s_full (kx->peer));
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 "Core service receives EPHEMERAL_KEY request from `%s'.\n",
936 GNUNET_i2s (kx->peer));
937 if ((ntohl (m->purpose.size) !=
938 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
939 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
940 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
941 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
942 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
944 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
947 &m->origin_identity.public_key)))
949 /* invalid signature */
953 now = GNUNET_TIME_absolute_get ();
954 if ( (end_t.abs_value_us < GNUNET_TIME_absolute_subtract (now, REKEY_TOLERANCE).abs_value_us) ||
955 (start_t.abs_value_us > GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value_us) )
957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
958 _("Ephemeral key message from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
959 GNUNET_i2s (kx->peer),
960 (unsigned long long) now.abs_value_us,
961 (unsigned long long) start_t.abs_value_us,
962 (unsigned long long) end_t.abs_value_us);
965 kx->other_ephemeral_key = m->ephemeral_key;
966 kx->foreign_key_expires = end_t;
967 derive_session_keys (kx);
968 GNUNET_STATISTICS_update (GSC_stats,
969 gettext_noop ("# EPHEMERAL_KEY messages received"), 1,
972 /* check if we still need to send the sender our key */
973 sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
974 switch (sender_status)
976 case GNUNET_CORE_KX_STATE_DOWN:
979 case GNUNET_CORE_KX_STATE_KEY_SENT:
980 /* fine, need to send our key after updating our status, see below */
981 GSC_SESSIONS_reinit (kx->peer);
983 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
984 /* other peer already got our key, but typemap did go down */
985 GSC_SESSIONS_reinit (kx->peer);
987 case GNUNET_CORE_KX_STATE_UP:
988 /* other peer already got our key, typemap NOT down */
990 case GNUNET_CORE_KX_STATE_REKEY_SENT:
991 /* other peer already got our key, typemap NOT down */
997 /* check if we need to confirm everything is fine via PING + PONG */
1000 case GNUNET_CORE_KX_STATE_DOWN:
1001 GNUNET_assert (NULL == kx->keep_alive_task);
1002 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
1003 monitor_notify_all (kx);
1004 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1009 case GNUNET_CORE_KX_STATE_KEY_SENT:
1010 GNUNET_assert (NULL == kx->keep_alive_task);
1011 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
1012 monitor_notify_all (kx);
1013 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1018 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1019 GNUNET_assert (NULL == kx->keep_alive_task);
1020 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1025 case GNUNET_CORE_KX_STATE_UP:
1026 kx->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
1027 monitor_notify_all (kx);
1028 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1033 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1034 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1047 * We received a PING message. Validate and transmit
1050 * @param cls key exchange status for the corresponding peer
1051 * @param m the encrypted PING message itself
1054 handle_ping (void *cls,
1055 const struct PingMessage *m)
1057 struct GSC_KeyExchangeInfo *kx = cls;
1058 struct PingMessage t;
1059 struct PongMessage tx;
1060 struct PongMessage *tp;
1061 struct GNUNET_MQ_Envelope *env;
1062 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1064 GNUNET_STATISTICS_update (GSC_stats,
1065 gettext_noop ("# PING messages received"),
1068 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
1069 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
1070 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT))
1073 GNUNET_STATISTICS_update (GSC_stats,
1074 gettext_noop ("# PING messages dropped (out of order)"),
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "Core service receives PING request from `%s'.\n",
1081 GNUNET_i2s (kx->peer));
1091 sizeof (struct PingMessage) - ((void *) &m->target -
1094 GNUNET_break_op (0);
1100 sizeof (struct GNUNET_PeerIdentity)))
1102 if (GNUNET_CORE_KX_STATE_REKEY_SENT != kx->status)
1103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1104 "Decryption of PING from peer `%s' failed\n",
1105 GNUNET_i2s (kx->peer));
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "Decryption of PING from peer `%s' failed after rekey (harmless)\n",
1109 GNUNET_i2s (kx->peer));
1110 GNUNET_break_op (0);
1113 /* construct PONG */
1115 tx.challenge = t.challenge;
1116 tx.target = t.target;
1117 env = GNUNET_MQ_msg (tp,
1118 GNUNET_MESSAGE_TYPE_CORE_PONG);
1119 tp->iv_seed = calculate_seed (kx);
1120 derive_pong_iv (&iv,
1129 sizeof (struct PongMessage) - ((void *) &tp->challenge -
1131 GNUNET_STATISTICS_update (GSC_stats,
1132 gettext_noop ("# PONG messages created"),
1135 GNUNET_MQ_send (kx->mq,
1141 * Task triggered when a neighbour entry is about to time out
1142 * (and we should prevent this by sending a PING).
1144 * @param cls the `struct GSC_KeyExchangeInfo`
1147 send_keep_alive (void *cls)
1149 struct GSC_KeyExchangeInfo *kx = cls;
1150 struct GNUNET_TIME_Relative retry;
1151 struct GNUNET_TIME_Relative left;
1153 kx->keep_alive_task = NULL;
1154 left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
1155 if (0 == left.rel_value_us)
1157 GNUNET_STATISTICS_update (GSC_stats,
1158 gettext_noop ("# sessions terminated by timeout"),
1161 GSC_SESSIONS_end (kx->peer);
1162 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1163 monitor_notify_all (kx);
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "Sending KEEPALIVE to `%s'\n",
1169 GNUNET_i2s (kx->peer));
1170 GNUNET_STATISTICS_update (GSC_stats,
1171 gettext_noop ("# keepalive messages sent"),
1174 setup_fresh_ping (kx);
1177 GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
1178 MIN_PING_FREQUENCY);
1179 kx->keep_alive_task =
1180 GNUNET_SCHEDULER_add_delayed (retry,
1187 * We've seen a valid message from the other peer.
1188 * Update the time when the session would time out
1189 * and delay sending our keep alive message further.
1191 * @param kx key exchange where we saw activity
1194 update_timeout (struct GSC_KeyExchangeInfo *kx)
1196 struct GNUNET_TIME_Relative delta;
1199 GNUNET_TIME_relative_to_absolute
1200 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1201 delta = GNUNET_TIME_absolute_get_difference (kx->last_notify_timeout,
1203 if (delta.rel_value_us > 5LL * 1000LL * 1000LL)
1205 /* we only notify monitors about timeout changes if those
1206 are bigger than the threshold (5s) */
1207 monitor_notify_all (kx);
1209 if (NULL != kx->keep_alive_task)
1210 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1211 kx->keep_alive_task =
1212 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1213 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1221 * We received a PONG message. Validate and update our status.
1223 * @param kx key exchange context for the the PONG
1224 * @param m the encrypted PONG message itself
1227 handle_pong (void *cls,
1228 const struct PongMessage *m)
1230 struct GSC_KeyExchangeInfo *kx = cls;
1231 struct PongMessage t;
1232 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1234 GNUNET_STATISTICS_update (GSC_stats,
1235 gettext_noop ("# PONG messages received"),
1240 case GNUNET_CORE_KX_STATE_DOWN:
1241 GNUNET_STATISTICS_update (GSC_stats,
1242 gettext_noop ("# PONG messages dropped (connection down)"), 1,
1245 case GNUNET_CORE_KX_STATE_KEY_SENT:
1246 GNUNET_STATISTICS_update (GSC_stats,
1247 gettext_noop ("# PONG messages dropped (out of order)"), 1,
1250 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1252 case GNUNET_CORE_KX_STATE_UP:
1254 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "Core service receives PONG response from `%s'.\n",
1262 GNUNET_i2s (kx->peer));
1263 /* mark as garbage, just to be sure */
1264 memset (&t, 255, sizeof (t));
1265 derive_pong_iv (&iv,
1275 sizeof (struct PongMessage) - ((void *) &m->challenge -
1278 GNUNET_break_op (0);
1281 GNUNET_STATISTICS_update (GSC_stats,
1282 gettext_noop ("# PONG messages decrypted"),
1285 if ((0 != memcmp (&t.target,
1287 sizeof (struct GNUNET_PeerIdentity))) ||
1288 (kx->ping_challenge != t.challenge))
1290 /* PONG malformed */
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292 "Received malformed PONG wanted sender `%s' with challenge %u\n",
1293 GNUNET_i2s (kx->peer),
1294 (unsigned int) kx->ping_challenge);
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Received malformed PONG received from `%s' with challenge %u\n",
1297 GNUNET_i2s (&t.target),
1298 (unsigned int) t.challenge);
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Received PONG from `%s'\n",
1303 GNUNET_i2s (kx->peer));
1304 /* no need to resend key any longer */
1305 if (NULL != kx->retry_set_key_task)
1307 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1308 kx->retry_set_key_task = NULL;
1312 case GNUNET_CORE_KX_STATE_DOWN:
1313 GNUNET_assert (0); /* should be impossible */
1315 case GNUNET_CORE_KX_STATE_KEY_SENT:
1316 GNUNET_assert (0); /* should be impossible */
1318 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1319 GNUNET_STATISTICS_update (GSC_stats,
1320 gettext_noop ("# session keys confirmed via PONG"),
1323 kx->status = GNUNET_CORE_KX_STATE_UP;
1324 monitor_notify_all (kx);
1325 GSC_SESSIONS_create (kx->peer, kx);
1326 GNUNET_assert (NULL == kx->keep_alive_task);
1327 update_timeout (kx);
1329 case GNUNET_CORE_KX_STATE_UP:
1330 GNUNET_STATISTICS_update (GSC_stats,
1331 gettext_noop ("# timeouts prevented via PONG"),
1334 update_timeout (kx);
1336 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1337 GNUNET_STATISTICS_update (GSC_stats,
1338 gettext_noop ("# rekey operations confirmed via PONG"),
1341 kx->status = GNUNET_CORE_KX_STATE_UP;
1342 monitor_notify_all (kx);
1343 update_timeout (kx);
1353 * Send our key to the other peer.
1355 * @param kx key exchange context
1358 send_key (struct GSC_KeyExchangeInfo *kx)
1360 struct GNUNET_MQ_Envelope *env;
1362 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
1363 if (NULL != kx->retry_set_key_task)
1365 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1366 kx->retry_set_key_task = NULL;
1368 /* always update sender status in SET KEY message */
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Sending key to `%s' (my status: %d)\n",
1371 GNUNET_i2s (kx->peer),
1373 current_ekm.sender_status = htonl ((int32_t) (kx->status));
1374 env = GNUNET_MQ_msg_copy (¤t_ekm.header);
1375 GNUNET_MQ_send (kx->mq,
1377 if (GNUNET_CORE_KX_STATE_KEY_SENT != kx->status)
1379 kx->retry_set_key_task =
1380 GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
1381 &set_key_retry_task,
1387 * Encrypt and transmit a message with the given payload.
1389 * @param kx key exchange context
1390 * @param payload payload of the message
1391 * @param payload_size number of bytes in @a payload
1394 GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
1395 const void *payload,
1396 size_t payload_size)
1398 size_t used = payload_size + sizeof (struct EncryptedMessage);
1399 char pbuf[used]; /* plaintext */
1400 struct EncryptedMessage *em; /* encrypted message */
1401 struct EncryptedMessage *ph; /* plaintext header */
1402 struct GNUNET_MQ_Envelope *env;
1403 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1404 struct GNUNET_CRYPTO_AuthKey auth_key;
1406 ph = (struct EncryptedMessage *) pbuf;
1407 ph->sequence_number = htonl (++kx->last_sequence_number_sent);
1408 ph->iv_seed = calculate_seed (kx);
1410 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1411 GNUNET_memcpy (&ph[1],
1414 env = GNUNET_MQ_msg_extra (em,
1416 GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1417 em->iv_seed = ph->iv_seed;
1422 GNUNET_assert (GNUNET_OK ==
1425 &ph->sequence_number,
1426 &em->sequence_number,
1427 used - ENCRYPTED_HEADER_SIZE));
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Encrypted %u bytes for %s\n",
1430 (unsigned int) (used - ENCRYPTED_HEADER_SIZE),
1431 GNUNET_i2s (kx->peer));
1432 derive_auth_key (&auth_key,
1435 GNUNET_CRYPTO_hmac (&auth_key,
1436 &em->sequence_number,
1437 used - ENCRYPTED_HEADER_SIZE,
1439 kx->has_excess_bandwidth = GNUNET_NO;
1440 GNUNET_MQ_send (kx->mq,
1446 * Closure for #deliver_message()
1448 struct DeliverMessageContext
1452 * Key exchange context.
1454 struct GSC_KeyExchangeInfo *kx;
1457 * Sender of the message.
1459 const struct GNUNET_PeerIdentity *peer;
1464 * We received an encrypted message. Check that it is
1465 * well-formed (size-wise).
1467 * @param cls key exchange context for encrypting the message
1468 * @param m encrypted message
1469 * @return #GNUNET_OK if @a msg is well-formed (size-wise)
1472 check_encrypted (void *cls,
1473 const struct EncryptedMessage *m)
1475 uint16_t size = ntohs (m->header.size) - sizeof (*m);
1477 if (size < sizeof (struct GNUNET_MessageHeader))
1479 GNUNET_break_op (0);
1480 return GNUNET_SYSERR;
1487 * We received an encrypted message. Decrypt, validate and
1488 * pass on to the appropriate clients.
1490 * @param cls key exchange context for encrypting the message
1491 * @param m encrypted message
1494 handle_encrypted (void *cls,
1495 const struct EncryptedMessage *m)
1497 struct GSC_KeyExchangeInfo *kx = cls;
1498 struct EncryptedMessage *pt; /* plaintext */
1499 struct GNUNET_HashCode ph;
1501 struct GNUNET_TIME_Absolute t;
1502 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1503 struct GNUNET_CRYPTO_AuthKey auth_key;
1504 struct DeliverMessageContext dmc;
1505 uint16_t size = ntohs (m->header.size);
1506 char buf[size] GNUNET_ALIGN;
1508 if (GNUNET_CORE_KX_STATE_UP != kx->status)
1510 GNUNET_STATISTICS_update (GSC_stats,
1511 gettext_noop ("# DATA message dropped (out of order)"),
1516 if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
1518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1519 _("Session to peer `%s' went down due to key expiration (should not happen)\n"),
1520 GNUNET_i2s (kx->peer));
1521 GNUNET_STATISTICS_update (GSC_stats,
1522 gettext_noop ("# sessions terminated by key expiration"),
1524 GSC_SESSIONS_end (kx->peer);
1525 if (NULL != kx->keep_alive_task)
1527 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1528 kx->keep_alive_task = NULL;
1530 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1531 monitor_notify_all (kx);
1537 derive_auth_key (&auth_key,
1540 GNUNET_CRYPTO_hmac (&auth_key,
1541 &m->sequence_number,
1542 size - ENCRYPTED_HEADER_SIZE,
1544 if (0 != memcmp (&ph,
1546 sizeof (struct GNUNET_HashCode)))
1548 /* checksum failed */
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "Failed checksum validation for a message from `%s'\n",
1551 GNUNET_i2s (kx->peer));
1562 &m->sequence_number,
1563 &buf[ENCRYPTED_HEADER_SIZE],
1564 size - ENCRYPTED_HEADER_SIZE))
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "Decrypted %u bytes from %s\n",
1568 (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
1569 GNUNET_i2s (kx->peer));
1570 pt = (struct EncryptedMessage *) buf;
1572 /* validate sequence number */
1573 snum = ntohl (pt->sequence_number);
1574 if (kx->last_sequence_number_received == snum)
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1577 "Received duplicate message, ignoring.\n");
1578 /* duplicate, ignore */
1579 GNUNET_STATISTICS_update (GSC_stats,
1580 gettext_noop ("# bytes dropped (duplicates)"),
1585 if ((kx->last_sequence_number_received > snum) &&
1586 (kx->last_sequence_number_received - snum > 32))
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Received ancient out of sequence message, ignoring.\n");
1590 /* ancient out of sequence, ignore */
1591 GNUNET_STATISTICS_update (GSC_stats,
1593 ("# bytes dropped (out of sequence)"), size,
1597 if (kx->last_sequence_number_received > snum)
1599 unsigned int rotbit = 1 << (kx->last_sequence_number_received - snum - 1);
1601 if ((kx->last_packets_bitmap & rotbit) != 0)
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Received duplicate message, ignoring.\n");
1605 GNUNET_STATISTICS_update (GSC_stats,
1606 gettext_noop ("# bytes dropped (duplicates)"),
1608 /* duplicate, ignore */
1611 kx->last_packets_bitmap |= rotbit;
1613 if (kx->last_sequence_number_received < snum)
1615 unsigned int shift = (snum - kx->last_sequence_number_received);
1617 if (shift >= 8 * sizeof (kx->last_packets_bitmap))
1618 kx->last_packets_bitmap = 0;
1620 kx->last_packets_bitmap <<= shift;
1621 kx->last_sequence_number_received = snum;
1624 /* check timestamp */
1625 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
1626 if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
1627 MAX_MESSAGE_AGE.rel_value_us)
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Message received far too old (%s). Content ignored.\n",
1631 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (t),
1633 GNUNET_STATISTICS_update (GSC_stats,
1635 ("# bytes dropped (ancient message)"), size,
1640 /* process decrypted message(s) */
1641 update_timeout (kx);
1642 GNUNET_STATISTICS_update (GSC_stats,
1643 gettext_noop ("# bytes of payload decrypted"),
1644 size - sizeof (struct EncryptedMessage),
1647 dmc.peer = kx->peer;
1649 GNUNET_SERVER_mst_receive (mst,
1651 &buf[sizeof (struct EncryptedMessage)],
1652 size - sizeof (struct EncryptedMessage),
1655 GNUNET_break_op (0);
1660 * One of our neighbours has excess bandwidth, remember this.
1663 * @param pid identity of the peer with excess bandwidth
1664 * @param connect_cls the `struct Neighbour`
1667 handle_transport_notify_excess_bw (void *cls,
1668 const struct GNUNET_PeerIdentity *pid,
1671 struct GSC_KeyExchangeInfo *kx = connect_cls;
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1674 "Peer %s has excess bandwidth available\n",
1676 kx->has_excess_bandwidth = GNUNET_YES;
1677 GSC_SESSIONS_solicit (pid);
1682 * Deliver P2P message to interested clients. Invokes send twice,
1683 * once for clients that want the full message, and once for clients
1684 * that only want the header
1686 * @param cls always NULL
1687 * @param client who sent us the message (struct GSC_KeyExchangeInfo)
1688 * @param m the message
1691 deliver_message (void *cls,
1693 const struct GNUNET_MessageHeader *m)
1695 struct DeliverMessageContext *dmc = client;
1697 if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
1699 GNUNET_STATISTICS_update (GSC_stats,
1700 gettext_noop ("# PAYLOAD dropped (out of order)"),
1705 switch (ntohs (m->type))
1707 case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
1708 case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
1709 GSC_SESSIONS_set_typemap (dmc->peer, m);
1711 case GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP:
1712 GSC_SESSIONS_confirm_typemap (dmc->peer, m);
1715 GSC_CLIENTS_deliver_message (dmc->peer,
1718 GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
1719 GSC_CLIENTS_deliver_message (dmc->peer,
1721 sizeof (struct GNUNET_MessageHeader),
1722 GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
1729 * Setup the message that links the ephemeral key to our persistent
1730 * public key and generate the appropriate signature.
1733 sign_ephemeral_key ()
1735 current_ekm.header.size = htons (sizeof (struct EphemeralKeyMessage));
1736 current_ekm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY);
1737 current_ekm.sender_status = 0; /* to be set later */
1738 current_ekm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY);
1739 current_ekm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1740 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1741 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1742 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
1743 sizeof (struct GNUNET_PeerIdentity));
1744 current_ekm.creation_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1746 GNUNET_CONFIGURATION_get_value_yesno (GSC_cfg,
1748 "USE_EPHEMERAL_KEYS"))
1750 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_add (REKEY_FREQUENCY,
1755 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1757 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key,
1758 ¤t_ekm.ephemeral_key);
1759 current_ekm.origin_identity = GSC_my_identity;
1760 GNUNET_assert (GNUNET_OK ==
1761 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1762 ¤t_ekm.purpose,
1763 ¤t_ekm.signature));
1768 * Task run to trigger rekeying.
1770 * @param cls closure, NULL
1773 do_rekey (void *cls)
1775 struct GSC_KeyExchangeInfo *pos;
1777 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1780 if (NULL != my_ephemeral_key)
1781 GNUNET_free (my_ephemeral_key);
1782 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1784 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1785 GNUNET_assert (NULL != my_ephemeral_key);
1786 sign_ephemeral_key ();
1787 for (pos = kx_head; NULL != pos; pos = pos->next)
1789 if (GNUNET_CORE_KX_STATE_UP == pos->status)
1791 pos->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
1792 monitor_notify_all (pos);
1793 derive_session_keys (pos);
1795 if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
1797 pos->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1798 monitor_notify_all (pos);
1800 monitor_notify_all (pos);
1807 * Initialize KX subsystem.
1809 * @param pk private key to use for the peer
1810 * @param server the server of the CORE service
1811 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1814 GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk,
1815 struct GNUNET_SERVER_Handle *server)
1817 GNUNET_MQ_hd_fixed_size (ephemeral_key,
1818 GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY,
1819 struct EphemeralKeyMessage);
1820 GNUNET_MQ_hd_fixed_size (ping,
1821 GNUNET_MESSAGE_TYPE_CORE_PING,
1822 struct PingMessage);
1823 GNUNET_MQ_hd_fixed_size (pong,
1824 GNUNET_MESSAGE_TYPE_CORE_PONG,
1825 struct PongMessage);
1826 GNUNET_MQ_hd_var_size (encrypted,
1827 GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE,
1828 struct EncryptedMessage);
1829 struct GNUNET_MQ_MessageHandler handlers[] = {
1830 make_ephemeral_key_handler (NULL),
1831 make_ping_handler (NULL),
1832 make_pong_handler (NULL),
1833 make_encrypted_handler (NULL),
1834 GNUNET_MQ_handler_end()
1837 nc = GNUNET_SERVER_notification_context_create (server,
1839 my_private_key = pk;
1840 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1841 &GSC_my_identity.public_key);
1842 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1843 if (NULL == my_ephemeral_key)
1846 GNUNET_free (my_private_key);
1847 my_private_key = NULL;
1848 return GNUNET_SYSERR;
1850 sign_ephemeral_key ();
1851 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1854 mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
1856 = GNUNET_TRANSPORT_core_connect (GSC_cfg,
1860 &handle_transport_notify_connect,
1861 &handle_transport_notify_disconnect,
1862 &handle_transport_notify_excess_bw);
1863 if (NULL == transport)
1866 return GNUNET_SYSERR;
1873 * Shutdown KX subsystem.
1878 if (NULL != transport)
1880 GNUNET_TRANSPORT_core_disconnect (transport);
1883 if (NULL != rekey_task)
1885 GNUNET_SCHEDULER_cancel (rekey_task);
1888 if (NULL != my_ephemeral_key)
1890 GNUNET_free (my_ephemeral_key);
1891 my_ephemeral_key = NULL;
1893 if (NULL != my_private_key)
1895 GNUNET_free (my_private_key);
1896 my_private_key = NULL;
1900 GNUNET_SERVER_mst_destroy (mst);
1905 GNUNET_SERVER_notification_context_destroy (nc);
1912 * Check how many messages are queued for the given neighbour.
1914 * @param kxinfo data about neighbour to check
1915 * @return number of items in the message queue
1918 GSC_NEIGHBOURS_get_queue_length (const struct GSC_KeyExchangeInfo *kxinfo)
1920 return GNUNET_MQ_get_length (kxinfo->mq);
1925 * Check if the given neighbour has excess bandwidth available.
1927 * @param target neighbour to check
1928 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
1931 GSC_NEIGHBOURS_check_excess_bandwidth (const struct GSC_KeyExchangeInfo *kxinfo)
1933 return kxinfo->has_excess_bandwidth;
1938 * Handle #GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request. For this
1939 * request type, the client does not have to have transmitted an INIT
1940 * request. All current peers are returned, regardless of which
1941 * message types they accept.
1944 * @param client client sending the iteration request
1945 * @param message iteration request message
1948 GSC_KX_handle_client_monitor_peers (void *cls,
1949 struct GNUNET_SERVER_Client *client,
1950 const struct GNUNET_MessageHeader *message)
1952 struct MonitorNotifyMessage done_msg;
1953 struct GSC_KeyExchangeInfo *kx;
1955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1956 GNUNET_SERVER_notification_context_add (nc,
1958 for (kx = kx_head; NULL != kx; kx = kx->next)
1959 monitor_notify (client, kx);
1960 done_msg.header.size = htons (sizeof (struct MonitorNotifyMessage));
1961 done_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
1962 done_msg.state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
1963 memset (&done_msg.peer,
1965 sizeof (struct GNUNET_PeerIdentity));
1966 done_msg.timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1967 GNUNET_SERVER_notification_context_unicast (nc,
1974 /* end of gnunet-service-core_kx.c */