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_sessions.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_core_service.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_signatures.h"
34 #include "gnunet_protocols.h"
39 * How long do we wait for SET_KEY confirmation initially?
41 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
44 * What is the minimum frequency for a PING message?
46 #define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49 * How often do we rekey?
51 #define REKEY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
54 * What time difference do we tolerate?
56 #define REKEY_TOLERANCE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
59 * What is the maximum age of a message for us to consider processing
60 * it? Note that this looks at the timestamp used by the other peer,
61 * so clock skew between machines does come into play here. So this
62 * should be picked high enough so that a little bit of clock skew
63 * does not prevent peers from connecting to us.
65 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
69 GNUNET_NETWORK_STRUCT_BEGIN
72 * Message transmitted with the signed ephemeral key of a peer. The
73 * session key is then derived from the two ephemeral keys (ECDHE).
75 struct EphemeralKeyMessage
79 * Message type is #GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY.
81 struct GNUNET_MessageHeader header;
84 * Status of the sender (should be in `enum PeerStateMachine`), nbo.
86 int32_t sender_status GNUNET_PACKED;
89 * An ECC signature of the @e origin_identity asserting the validity
90 * of the given ephemeral key.
92 struct GNUNET_CRYPTO_EddsaSignature signature;
95 * Information about what is being signed.
97 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
100 * At what time was this key created (beginning of validity).
102 struct GNUNET_TIME_AbsoluteNBO creation_time;
105 * When does the given ephemeral key expire (end of validity).
107 struct GNUNET_TIME_AbsoluteNBO expiration_time;
110 * Ephemeral public ECC key.
112 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
115 * Public key of the signing peer (persistent version, not the
116 * ephemeral public key).
118 struct GNUNET_PeerIdentity origin_identity;
124 * We're sending an (encrypted) PING to the other peer to check if he
125 * can decrypt. The other peer should respond with a PONG with the
126 * same content, except this time encrypted with the receiver's key.
131 * Message type is #GNUNET_MESSAGE_TYPE_CORE_PING.
133 struct GNUNET_MessageHeader header;
138 uint32_t iv_seed GNUNET_PACKED;
141 * Intended target of the PING, used primarily to check
142 * that decryption actually worked.
144 struct GNUNET_PeerIdentity target;
147 * Random number chosen to make replay harder.
149 uint32_t challenge GNUNET_PACKED;
154 * Response to a PING. Includes data from the original PING.
159 * Message type is #GNUNET_MESSAGE_TYPE_CORE_PONG.
161 struct GNUNET_MessageHeader header;
166 uint32_t iv_seed GNUNET_PACKED;
169 * Random number to make replay attacks harder.
171 uint32_t challenge GNUNET_PACKED;
174 * Reserved, always zero.
179 * Intended target of the PING, used primarily to check
180 * that decryption actually worked.
182 struct GNUNET_PeerIdentity target;
187 * Encapsulation for encrypted messages exchanged between
188 * peers. Followed by the actual encrypted data.
190 struct EncryptedMessage
193 * Message type is #GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
195 struct GNUNET_MessageHeader header;
198 * Random value used for IV generation.
200 uint32_t iv_seed GNUNET_PACKED;
203 * MAC of the encrypted message (starting at @e sequence_number),
204 * used to verify message integrity. Everything after this value
205 * (excluding this value itself) will be encrypted and
206 * authenticated. #ENCRYPTED_HEADER_SIZE must be set to the offset
207 * of the *next* field.
209 struct GNUNET_HashCode hmac;
212 * Sequence number, in network byte order. This field
213 * must be the first encrypted/decrypted field
215 uint32_t sequence_number GNUNET_PACKED;
218 * Reserved, always zero.
220 uint32_t reserved GNUNET_PACKED;
223 * Timestamp. Used to prevent replay of ancient messages
224 * (recent messages are caught with the sequence number).
226 struct GNUNET_TIME_AbsoluteNBO timestamp;
229 GNUNET_NETWORK_STRUCT_END
233 * Number of bytes (at the beginning) of `struct EncryptedMessage`
234 * that are NOT encrypted.
236 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
240 * Information about the status of a key exchange with another peer.
242 struct GSC_KeyExchangeInfo
248 struct GSC_KeyExchangeInfo *next;
253 struct GSC_KeyExchangeInfo *prev;
256 * Identity of the peer.
258 const struct GNUNET_PeerIdentity *peer;
261 * Message queue for sending messages to @a peer.
263 struct GNUNET_MQ_Handle *mq;
266 * PING message we transmit to the other peer.
268 struct PingMessage ping;
271 * Ephemeral public ECC key of the other peer.
273 struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key;
276 * Key we use to encrypt our messages for the other peer
277 * (initialized by us when we do the handshake).
279 struct GNUNET_CRYPTO_SymmetricSessionKey encrypt_key;
282 * Key we use to decrypt messages from the other peer
283 * (given to us by the other peer during the handshake).
285 struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key;
288 * At what time did the other peer generate the decryption key?
290 struct GNUNET_TIME_Absolute foreign_key_expires;
293 * When should the session time out (if there are no PONGs)?
295 struct GNUNET_TIME_Absolute timeout;
298 * What was the last timeout we informed our monitors about?
300 struct GNUNET_TIME_Absolute last_notify_timeout;
303 * At what frequency are we currently re-trying SET_KEY messages?
305 struct GNUNET_TIME_Relative set_key_retry_frequency;
308 * ID of task used for re-trying SET_KEY and PING message.
310 struct GNUNET_SCHEDULER_Task *retry_set_key_task;
313 * ID of task used for sending keep-alive pings.
315 struct GNUNET_SCHEDULER_Task *keep_alive_task;
318 * Bit map indicating which of the 32 sequence numbers before the
319 * last were received (good for accepting out-of-order packets and
320 * estimating reliability of the connection)
322 unsigned int last_packets_bitmap;
325 * last sequence number received on this connection (highest)
327 uint32_t last_sequence_number_received;
330 * last sequence number transmitted
332 uint32_t last_sequence_number_sent;
335 * What was our PING challenge number (for this peer)?
337 uint32_t ping_challenge;
340 * #GNUNET_YES if this peer currently has excess bandwidth.
342 int has_excess_bandwidth;
345 * What is our connection status?
347 enum GNUNET_CORE_KxState status;
355 static struct GNUNET_TRANSPORT_CoreHandle *transport;
360 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
363 * Our ephemeral private key.
365 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
368 * Current message we send for a key exchange.
370 static struct EphemeralKeyMessage current_ekm;
373 * Our message stream tokenizer (for encrypted payload).
375 static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
380 static struct GSC_KeyExchangeInfo *kx_head;
385 static struct GSC_KeyExchangeInfo *kx_tail;
388 * Task scheduled for periodic re-generation (and thus rekeying) of our
391 static struct GNUNET_SCHEDULER_Task *rekey_task;
394 * Notification context for broadcasting to monitors.
396 static struct GNUNET_NotificationContext *nc;
400 * Calculate seed value we should use for a message.
402 * @param kx key exchange context
405 calculate_seed (struct GSC_KeyExchangeInfo *kx)
407 /* Note: may want to make this non-random and instead
408 derive from key material to avoid having an undetectable
410 return htonl (GNUNET_CRYPTO_random_u32
411 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
416 * Inform all monitors about the KX state of the given peer.
418 * @param kx key exchange state to inform about
421 monitor_notify_all (struct GSC_KeyExchangeInfo *kx)
423 struct MonitorNotifyMessage msg;
425 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
426 msg.header.size = htons (sizeof (msg));
427 msg.state = htonl ((uint32_t) kx->status);
428 msg.peer = *kx->peer;
429 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
430 GNUNET_notification_context_broadcast (nc,
433 kx->last_notify_timeout = kx->timeout;
438 * Derive an authentication key from "set key" information
440 * @param akey authentication key to derive
441 * @param skey session key to use
442 * @param seed seed to use
445 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
446 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
449 static const char ctx[] = "authentication key";
451 GNUNET_CRYPTO_hmac_derive_key (akey, skey,
452 &seed, sizeof (seed),
453 skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
460 * Derive an IV from packet information
462 * @param iv initialization vector to initialize
463 * @param skey session key to use
464 * @param seed seed to use
465 * @param identity identity of the other peer to use
468 derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
469 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
471 const struct GNUNET_PeerIdentity *identity)
473 static const char ctx[] = "initialization vector";
475 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
476 &seed, sizeof (seed),
478 sizeof (struct GNUNET_PeerIdentity), ctx,
484 * Derive an IV from pong packet information
486 * @param iv initialization vector to initialize
487 * @param skey session key to use
488 * @param seed seed to use
489 * @param challenge nonce to use
490 * @param identity identity of the other peer to use
493 derive_pong_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
494 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
497 const struct GNUNET_PeerIdentity *identity)
499 static const char ctx[] = "pong initialization vector";
501 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
502 &seed, sizeof (seed),
504 sizeof (struct GNUNET_PeerIdentity),
505 &challenge, sizeof (challenge),
512 * Derive an AES key from key material
514 * @param sender peer identity of the sender
515 * @param receiver peer identity of the sender
516 * @param key_material high entropy key material to use
517 * @param skey set to derived session key
520 derive_aes_key (const struct GNUNET_PeerIdentity *sender,
521 const struct GNUNET_PeerIdentity *receiver,
522 const struct GNUNET_HashCode *key_material,
523 struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
525 static const char ctx[] = "aes key generation vector";
527 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
529 key_material, sizeof (struct GNUNET_HashCode),
530 sender, sizeof (struct GNUNET_PeerIdentity),
531 receiver, sizeof (struct GNUNET_PeerIdentity),
537 * Encrypt size bytes from @a in and write the result to @a out. Use the
538 * @a kx key for outbound traffic of the given neighbour.
540 * @param kx key information context
541 * @param iv initialization vector to use
542 * @param in ciphertext
543 * @param out plaintext
544 * @param size size of @a in/@a out
545 * @return #GNUNET_OK on success
548 do_encrypt (struct GSC_KeyExchangeInfo *kx,
549 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
554 if (size != (uint16_t) size)
559 GNUNET_assert (size ==
560 GNUNET_CRYPTO_symmetric_encrypt (in,
565 GNUNET_STATISTICS_update (GSC_stats,
566 gettext_noop ("# bytes encrypted"),
569 /* the following is too sensitive to write to log files by accident,
570 so we require manual intervention to get this one... */
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Encrypted %u bytes for `%s' using key %u, IV %u\n",
575 GNUNET_i2s (kx->peer),
576 (unsigned int) kx->encrypt_key.crc32,
577 GNUNET_CRYPTO_crc32_n (iv,
585 * Decrypt size bytes from @a in and write the result to @a out. Use
586 * the @a kx key for inbound traffic of the given neighbour. This
587 * function does NOT do any integrity-checks on the result.
589 * @param kx key information context
590 * @param iv initialization vector to use
591 * @param in ciphertext
592 * @param out plaintext
593 * @param size size of @a in / @a out
594 * @return #GNUNET_OK on success
597 do_decrypt (struct GSC_KeyExchangeInfo *kx,
598 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
603 if (size != (uint16_t) size)
608 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
609 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
610 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT) )
613 return GNUNET_SYSERR;
616 GNUNET_CRYPTO_symmetric_decrypt (in,
623 return GNUNET_SYSERR;
625 GNUNET_STATISTICS_update (GSC_stats,
626 gettext_noop ("# bytes decrypted"),
629 /* the following is too sensitive to write to log files by accident,
630 so we require manual intervention to get this one... */
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "Decrypted %u bytes from `%s' using key %u, IV %u\n",
635 GNUNET_i2s (kx->peer),
636 (unsigned int) kx->decrypt_key.crc32,
637 GNUNET_CRYPTO_crc32_n (iv,
646 * Send our key (and encrypted PING) to the other peer.
648 * @param kx key exchange context
651 send_key (struct GSC_KeyExchangeInfo *kx);
655 * Task that will retry #send_key() if our previous attempt failed.
657 * @param cls our `struct GSC_KeyExchangeInfo`
660 set_key_retry_task (void *cls)
662 struct GSC_KeyExchangeInfo *kx = cls;
664 kx->retry_set_key_task = NULL;
665 kx->set_key_retry_frequency = GNUNET_TIME_STD_BACKOFF (kx->set_key_retry_frequency);
666 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
672 * Create a fresh PING message for transmission to the other peer.
674 * @param kx key exchange context to create PING for
677 setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
679 struct PingMessage pp;
680 struct PingMessage *pm;
681 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
684 kx->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
686 pm->header.size = htons (sizeof (struct PingMessage));
687 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
688 pm->iv_seed = calculate_seed (kx);
693 pp.challenge = kx->ping_challenge;
694 pp.target = *kx->peer;
699 sizeof (struct PingMessage) - ((void *) &pm->target -
705 * Function called by transport to notify us that
706 * a peer connected to us (on the network level).
707 * Starts the key exchange with the given peer.
709 * @param cls closure (NULL)
710 * @param pid identity of the peer to do a key exchange with
711 * @return key exchange information context
714 handle_transport_notify_connect (void *cls,
715 const struct GNUNET_PeerIdentity *pid,
716 struct GNUNET_MQ_Handle *mq)
718 struct GSC_KeyExchangeInfo *kx;
719 struct GNUNET_HashCode h1;
720 struct GNUNET_HashCode h2;
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Initiating key exchange with `%s'\n",
725 GNUNET_STATISTICS_update (GSC_stats,
726 gettext_noop ("# key exchanges initiated"),
729 kx = GNUNET_new (struct GSC_KeyExchangeInfo);
732 kx->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
733 GNUNET_CONTAINER_DLL_insert (kx_head,
736 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
737 monitor_notify_all (kx);
738 GNUNET_CRYPTO_hash (pid,
739 sizeof (struct GNUNET_PeerIdentity),
741 GNUNET_CRYPTO_hash (&GSC_my_identity,
742 sizeof (struct GNUNET_PeerIdentity),
744 if (0 < GNUNET_CRYPTO_hash_cmp (&h1,
747 /* peer with "lower" identity starts KX, otherwise we typically end up
748 with both peers starting the exchange and transmit the 'set key'
754 /* peer with "higher" identity starts a delayed KX, if the "lower" peer
755 * does not start a KX since he sees no reasons to do so */
756 kx->retry_set_key_task
757 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
766 * Function called by transport telling us that a peer
768 * Stop key exchange with the given peer. Clean up key material.
771 * @param peer the peer that disconnected
772 * @param handler_cls the `struct GSC_KeyExchangeInfo` of the peer
775 handle_transport_notify_disconnect (void *cls,
776 const struct GNUNET_PeerIdentity *peer,
779 struct GSC_KeyExchangeInfo *kx = handler_cls;
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Peer `%s' disconnected from us.\n",
784 GSC_SESSIONS_end (kx->peer);
785 GNUNET_STATISTICS_update (GSC_stats,
786 gettext_noop ("# key exchanges stopped"),
789 if (NULL != kx->retry_set_key_task)
791 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
792 kx->retry_set_key_task = NULL;
794 if (NULL != kx->keep_alive_task)
796 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
797 kx->keep_alive_task = NULL;
799 kx->status = GNUNET_CORE_KX_PEER_DISCONNECT;
800 monitor_notify_all (kx);
801 GNUNET_CONTAINER_DLL_remove (kx_head,
809 * Send our PING to the other peer.
811 * @param kx key exchange context
814 send_ping (struct GSC_KeyExchangeInfo *kx)
816 struct GNUNET_MQ_Envelope *env;
818 GNUNET_STATISTICS_update (GSC_stats,
819 gettext_noop ("# PING messages transmitted"),
822 env = GNUNET_MQ_msg_copy (&kx->ping.header);
823 GNUNET_MQ_send (kx->mq,
829 * Derive fresh session keys from the current ephemeral keys.
831 * @param kx session to derive keys for
834 derive_session_keys (struct GSC_KeyExchangeInfo *kx)
836 struct GNUNET_HashCode key_material;
839 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
840 &kx->other_ephemeral_key,
846 derive_aes_key (&GSC_my_identity,
850 derive_aes_key (kx->peer,
854 memset (&key_material, 0, sizeof (key_material));
855 /* fresh key, reset sequence numbers */
856 kx->last_sequence_number_received = 0;
857 kx->last_packets_bitmap = 0;
858 setup_fresh_ping (kx);
863 * We received a #GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY message.
864 * Validate and update our key material and status.
866 * @param cls key exchange status for the corresponding peer
867 * @param m the set key message we received
870 handle_ephemeral_key (void *cls,
871 const struct EphemeralKeyMessage *m)
873 struct GSC_KeyExchangeInfo *kx = cls;
874 struct GNUNET_TIME_Absolute start_t;
875 struct GNUNET_TIME_Absolute end_t;
876 struct GNUNET_TIME_Absolute now;
877 enum GNUNET_CORE_KxState sender_status;
879 end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
880 if ( ( (GNUNET_CORE_KX_STATE_KEY_RECEIVED == kx->status) ||
881 (GNUNET_CORE_KX_STATE_UP == kx->status) ||
882 (GNUNET_CORE_KX_STATE_REKEY_SENT == kx->status) ) &&
883 (end_t.abs_value_us < kx->foreign_key_expires.abs_value_us) )
885 GNUNET_STATISTICS_update (GSC_stats,
886 gettext_noop ("# old ephemeral keys ignored"),
890 start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
892 GNUNET_STATISTICS_update (GSC_stats,
893 gettext_noop ("# ephemeral keys received"),
897 memcmp (&m->origin_identity,
899 sizeof (struct GNUNET_PeerIdentity)))
901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
902 "Received EPHEMERAL_KEY from %s, but expected %s\n",
903 GNUNET_i2s (&m->origin_identity),
904 GNUNET_i2s_full (kx->peer));
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Core service receives EPHEMERAL_KEY request from `%s'.\n",
910 GNUNET_i2s (kx->peer));
911 if ((ntohl (m->purpose.size) !=
912 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
913 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
914 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
915 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
916 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
918 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
921 &m->origin_identity.public_key)))
923 /* invalid signature */
927 now = GNUNET_TIME_absolute_get ();
928 if ( (end_t.abs_value_us < GNUNET_TIME_absolute_subtract (now, REKEY_TOLERANCE).abs_value_us) ||
929 (start_t.abs_value_us > GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value_us) )
931 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
932 _("Ephemeral key message from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
933 GNUNET_i2s (kx->peer),
934 (unsigned long long) now.abs_value_us,
935 (unsigned long long) start_t.abs_value_us,
936 (unsigned long long) end_t.abs_value_us);
939 kx->other_ephemeral_key = m->ephemeral_key;
940 kx->foreign_key_expires = end_t;
941 derive_session_keys (kx);
942 GNUNET_STATISTICS_update (GSC_stats,
943 gettext_noop ("# EPHEMERAL_KEY messages received"), 1,
946 /* check if we still need to send the sender our key */
947 sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
948 switch (sender_status)
950 case GNUNET_CORE_KX_STATE_DOWN:
953 case GNUNET_CORE_KX_STATE_KEY_SENT:
954 /* fine, need to send our key after updating our status, see below */
955 GSC_SESSIONS_reinit (kx->peer);
957 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
958 /* other peer already got our key, but typemap did go down */
959 GSC_SESSIONS_reinit (kx->peer);
961 case GNUNET_CORE_KX_STATE_UP:
962 /* other peer already got our key, typemap NOT down */
964 case GNUNET_CORE_KX_STATE_REKEY_SENT:
965 /* other peer already got our key, typemap NOT down */
971 /* check if we need to confirm everything is fine via PING + PONG */
974 case GNUNET_CORE_KX_STATE_DOWN:
975 GNUNET_assert (NULL == kx->keep_alive_task);
976 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
977 monitor_notify_all (kx);
978 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
983 case GNUNET_CORE_KX_STATE_KEY_SENT:
984 GNUNET_assert (NULL == kx->keep_alive_task);
985 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
986 monitor_notify_all (kx);
987 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
992 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
993 GNUNET_assert (NULL == kx->keep_alive_task);
994 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
999 case GNUNET_CORE_KX_STATE_UP:
1000 kx->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
1001 monitor_notify_all (kx);
1002 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1007 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1008 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1021 * We received a PING message. Validate and transmit
1024 * @param cls key exchange status for the corresponding peer
1025 * @param m the encrypted PING message itself
1028 handle_ping (void *cls,
1029 const struct PingMessage *m)
1031 struct GSC_KeyExchangeInfo *kx = cls;
1032 struct PingMessage t;
1033 struct PongMessage tx;
1034 struct PongMessage *tp;
1035 struct GNUNET_MQ_Envelope *env;
1036 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1038 GNUNET_STATISTICS_update (GSC_stats,
1039 gettext_noop ("# PING messages received"),
1042 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
1043 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
1044 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT))
1047 GNUNET_STATISTICS_update (GSC_stats,
1048 gettext_noop ("# PING messages dropped (out of order)"),
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Core service receives PING request from `%s'.\n",
1055 GNUNET_i2s (kx->peer));
1065 sizeof (struct PingMessage) - ((void *) &m->target -
1068 GNUNET_break_op (0);
1074 sizeof (struct GNUNET_PeerIdentity)))
1076 if (GNUNET_CORE_KX_STATE_REKEY_SENT != kx->status)
1077 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1078 "Decryption of PING from peer `%s' failed\n",
1079 GNUNET_i2s (kx->peer));
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Decryption of PING from peer `%s' failed after rekey (harmless)\n",
1083 GNUNET_i2s (kx->peer));
1084 GNUNET_break_op (0);
1087 /* construct PONG */
1089 tx.challenge = t.challenge;
1090 tx.target = t.target;
1091 env = GNUNET_MQ_msg (tp,
1092 GNUNET_MESSAGE_TYPE_CORE_PONG);
1093 tp->iv_seed = calculate_seed (kx);
1094 derive_pong_iv (&iv,
1103 sizeof (struct PongMessage) - ((void *) &tp->challenge -
1105 GNUNET_STATISTICS_update (GSC_stats,
1106 gettext_noop ("# PONG messages created"),
1109 GNUNET_MQ_send (kx->mq,
1115 * Task triggered when a neighbour entry is about to time out
1116 * (and we should prevent this by sending a PING).
1118 * @param cls the `struct GSC_KeyExchangeInfo`
1121 send_keep_alive (void *cls)
1123 struct GSC_KeyExchangeInfo *kx = cls;
1124 struct GNUNET_TIME_Relative retry;
1125 struct GNUNET_TIME_Relative left;
1127 kx->keep_alive_task = NULL;
1128 left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
1129 if (0 == left.rel_value_us)
1131 GNUNET_STATISTICS_update (GSC_stats,
1132 gettext_noop ("# sessions terminated by timeout"),
1135 GSC_SESSIONS_end (kx->peer);
1136 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1137 monitor_notify_all (kx);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Sending KEEPALIVE to `%s'\n",
1143 GNUNET_i2s (kx->peer));
1144 GNUNET_STATISTICS_update (GSC_stats,
1145 gettext_noop ("# keepalive messages sent"),
1148 setup_fresh_ping (kx);
1151 GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
1152 MIN_PING_FREQUENCY);
1153 kx->keep_alive_task =
1154 GNUNET_SCHEDULER_add_delayed (retry,
1161 * We've seen a valid message from the other peer.
1162 * Update the time when the session would time out
1163 * and delay sending our keep alive message further.
1165 * @param kx key exchange where we saw activity
1168 update_timeout (struct GSC_KeyExchangeInfo *kx)
1170 struct GNUNET_TIME_Relative delta;
1173 GNUNET_TIME_relative_to_absolute
1174 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1175 delta = GNUNET_TIME_absolute_get_difference (kx->last_notify_timeout,
1177 if (delta.rel_value_us > 5LL * 1000LL * 1000LL)
1179 /* we only notify monitors about timeout changes if those
1180 are bigger than the threshold (5s) */
1181 monitor_notify_all (kx);
1183 if (NULL != kx->keep_alive_task)
1184 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1185 kx->keep_alive_task =
1186 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1187 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1195 * We received a PONG message. Validate and update our status.
1197 * @param kx key exchange context for the the PONG
1198 * @param m the encrypted PONG message itself
1201 handle_pong (void *cls,
1202 const struct PongMessage *m)
1204 struct GSC_KeyExchangeInfo *kx = cls;
1205 struct PongMessage t;
1206 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1208 GNUNET_STATISTICS_update (GSC_stats,
1209 gettext_noop ("# PONG messages received"),
1214 case GNUNET_CORE_KX_STATE_DOWN:
1215 GNUNET_STATISTICS_update (GSC_stats,
1216 gettext_noop ("# PONG messages dropped (connection down)"), 1,
1219 case GNUNET_CORE_KX_STATE_KEY_SENT:
1220 GNUNET_STATISTICS_update (GSC_stats,
1221 gettext_noop ("# PONG messages dropped (out of order)"), 1,
1224 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1226 case GNUNET_CORE_KX_STATE_UP:
1228 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Core service receives PONG response from `%s'.\n",
1236 GNUNET_i2s (kx->peer));
1237 /* mark as garbage, just to be sure */
1238 memset (&t, 255, sizeof (t));
1239 derive_pong_iv (&iv,
1249 sizeof (struct PongMessage) - ((void *) &m->challenge -
1252 GNUNET_break_op (0);
1255 GNUNET_STATISTICS_update (GSC_stats,
1256 gettext_noop ("# PONG messages decrypted"),
1259 if ((0 != memcmp (&t.target,
1261 sizeof (struct GNUNET_PeerIdentity))) ||
1262 (kx->ping_challenge != t.challenge))
1264 /* PONG malformed */
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Received malformed PONG wanted sender `%s' with challenge %u\n",
1267 GNUNET_i2s (kx->peer),
1268 (unsigned int) kx->ping_challenge);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Received malformed PONG received from `%s' with challenge %u\n",
1271 GNUNET_i2s (&t.target),
1272 (unsigned int) t.challenge);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Received PONG from `%s'\n",
1277 GNUNET_i2s (kx->peer));
1278 /* no need to resend key any longer */
1279 if (NULL != kx->retry_set_key_task)
1281 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1282 kx->retry_set_key_task = NULL;
1286 case GNUNET_CORE_KX_STATE_DOWN:
1287 GNUNET_assert (0); /* should be impossible */
1289 case GNUNET_CORE_KX_STATE_KEY_SENT:
1290 GNUNET_assert (0); /* should be impossible */
1292 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1293 GNUNET_STATISTICS_update (GSC_stats,
1294 gettext_noop ("# session keys confirmed via PONG"),
1297 kx->status = GNUNET_CORE_KX_STATE_UP;
1298 monitor_notify_all (kx);
1299 GSC_SESSIONS_create (kx->peer, kx);
1300 GNUNET_assert (NULL == kx->keep_alive_task);
1301 update_timeout (kx);
1303 case GNUNET_CORE_KX_STATE_UP:
1304 GNUNET_STATISTICS_update (GSC_stats,
1305 gettext_noop ("# timeouts prevented via PONG"),
1308 update_timeout (kx);
1310 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1311 GNUNET_STATISTICS_update (GSC_stats,
1312 gettext_noop ("# rekey operations confirmed via PONG"),
1315 kx->status = GNUNET_CORE_KX_STATE_UP;
1316 monitor_notify_all (kx);
1317 update_timeout (kx);
1327 * Send our key to the other peer.
1329 * @param kx key exchange context
1332 send_key (struct GSC_KeyExchangeInfo *kx)
1334 struct GNUNET_MQ_Envelope *env;
1336 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
1337 if (NULL != kx->retry_set_key_task)
1339 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1340 kx->retry_set_key_task = NULL;
1342 /* always update sender status in SET KEY message */
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344 "Sending key to `%s' (my status: %d)\n",
1345 GNUNET_i2s (kx->peer),
1347 current_ekm.sender_status = htonl ((int32_t) (kx->status));
1348 env = GNUNET_MQ_msg_copy (¤t_ekm.header);
1349 GNUNET_MQ_send (kx->mq,
1351 if (GNUNET_CORE_KX_STATE_KEY_SENT != kx->status)
1353 kx->retry_set_key_task =
1354 GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
1355 &set_key_retry_task,
1361 * Encrypt and transmit a message with the given payload.
1363 * @param kx key exchange context
1364 * @param payload payload of the message
1365 * @param payload_size number of bytes in @a payload
1368 GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
1369 const void *payload,
1370 size_t payload_size)
1372 size_t used = payload_size + sizeof (struct EncryptedMessage);
1373 char pbuf[used]; /* plaintext */
1374 struct EncryptedMessage *em; /* encrypted message */
1375 struct EncryptedMessage *ph; /* plaintext header */
1376 struct GNUNET_MQ_Envelope *env;
1377 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1378 struct GNUNET_CRYPTO_AuthKey auth_key;
1380 ph = (struct EncryptedMessage *) pbuf;
1381 ph->sequence_number = htonl (++kx->last_sequence_number_sent);
1382 ph->iv_seed = calculate_seed (kx);
1384 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1385 GNUNET_memcpy (&ph[1],
1388 env = GNUNET_MQ_msg_extra (em,
1390 GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1391 em->iv_seed = ph->iv_seed;
1396 GNUNET_assert (GNUNET_OK ==
1399 &ph->sequence_number,
1400 &em->sequence_number,
1401 used - ENCRYPTED_HEADER_SIZE));
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Encrypted %u bytes for %s\n",
1404 (unsigned int) (used - ENCRYPTED_HEADER_SIZE),
1405 GNUNET_i2s (kx->peer));
1406 derive_auth_key (&auth_key,
1409 GNUNET_CRYPTO_hmac (&auth_key,
1410 &em->sequence_number,
1411 used - ENCRYPTED_HEADER_SIZE,
1413 kx->has_excess_bandwidth = GNUNET_NO;
1414 GNUNET_MQ_send (kx->mq,
1420 * Closure for #deliver_message()
1422 struct DeliverMessageContext
1426 * Key exchange context.
1428 struct GSC_KeyExchangeInfo *kx;
1431 * Sender of the message.
1433 const struct GNUNET_PeerIdentity *peer;
1438 * We received an encrypted message. Check that it is
1439 * well-formed (size-wise).
1441 * @param cls key exchange context for encrypting the message
1442 * @param m encrypted message
1443 * @return #GNUNET_OK if @a msg is well-formed (size-wise)
1446 check_encrypted (void *cls,
1447 const struct EncryptedMessage *m)
1449 uint16_t size = ntohs (m->header.size) - sizeof (*m);
1451 if (size < sizeof (struct GNUNET_MessageHeader))
1453 GNUNET_break_op (0);
1454 return GNUNET_SYSERR;
1461 * We received an encrypted message. Decrypt, validate and
1462 * pass on to the appropriate clients.
1464 * @param cls key exchange context for encrypting the message
1465 * @param m encrypted message
1468 handle_encrypted (void *cls,
1469 const struct EncryptedMessage *m)
1471 struct GSC_KeyExchangeInfo *kx = cls;
1472 struct EncryptedMessage *pt; /* plaintext */
1473 struct GNUNET_HashCode ph;
1475 struct GNUNET_TIME_Absolute t;
1476 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1477 struct GNUNET_CRYPTO_AuthKey auth_key;
1478 struct DeliverMessageContext dmc;
1479 uint16_t size = ntohs (m->header.size);
1480 char buf[size] GNUNET_ALIGN;
1482 if (GNUNET_CORE_KX_STATE_UP != kx->status)
1484 GNUNET_STATISTICS_update (GSC_stats,
1485 gettext_noop ("# DATA message dropped (out of order)"),
1490 if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
1492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1493 _("Session to peer `%s' went down due to key expiration (should not happen)\n"),
1494 GNUNET_i2s (kx->peer));
1495 GNUNET_STATISTICS_update (GSC_stats,
1496 gettext_noop ("# sessions terminated by key expiration"),
1498 GSC_SESSIONS_end (kx->peer);
1499 if (NULL != kx->keep_alive_task)
1501 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1502 kx->keep_alive_task = NULL;
1504 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1505 monitor_notify_all (kx);
1511 derive_auth_key (&auth_key,
1514 GNUNET_CRYPTO_hmac (&auth_key,
1515 &m->sequence_number,
1516 size - ENCRYPTED_HEADER_SIZE,
1518 if (0 != memcmp (&ph,
1520 sizeof (struct GNUNET_HashCode)))
1522 /* checksum failed */
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Failed checksum validation for a message from `%s'\n",
1525 GNUNET_i2s (kx->peer));
1536 &m->sequence_number,
1537 &buf[ENCRYPTED_HEADER_SIZE],
1538 size - ENCRYPTED_HEADER_SIZE))
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "Decrypted %u bytes from %s\n",
1542 (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
1543 GNUNET_i2s (kx->peer));
1544 pt = (struct EncryptedMessage *) buf;
1546 /* validate sequence number */
1547 snum = ntohl (pt->sequence_number);
1548 if (kx->last_sequence_number_received == snum)
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "Received duplicate message, ignoring.\n");
1552 /* duplicate, ignore */
1553 GNUNET_STATISTICS_update (GSC_stats,
1554 gettext_noop ("# bytes dropped (duplicates)"),
1559 if ((kx->last_sequence_number_received > snum) &&
1560 (kx->last_sequence_number_received - snum > 32))
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 "Received ancient out of sequence message, ignoring.\n");
1564 /* ancient out of sequence, ignore */
1565 GNUNET_STATISTICS_update (GSC_stats,
1567 ("# bytes dropped (out of sequence)"), size,
1571 if (kx->last_sequence_number_received > snum)
1573 unsigned int rotbit = 1 << (kx->last_sequence_number_received - snum - 1);
1575 if ((kx->last_packets_bitmap & rotbit) != 0)
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Received duplicate message, ignoring.\n");
1579 GNUNET_STATISTICS_update (GSC_stats,
1580 gettext_noop ("# bytes dropped (duplicates)"),
1582 /* duplicate, ignore */
1585 kx->last_packets_bitmap |= rotbit;
1587 if (kx->last_sequence_number_received < snum)
1589 unsigned int shift = (snum - kx->last_sequence_number_received);
1591 if (shift >= 8 * sizeof (kx->last_packets_bitmap))
1592 kx->last_packets_bitmap = 0;
1594 kx->last_packets_bitmap <<= shift;
1595 kx->last_sequence_number_received = snum;
1598 /* check timestamp */
1599 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
1600 if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
1601 MAX_MESSAGE_AGE.rel_value_us)
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Message received far too old (%s). Content ignored.\n",
1605 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (t),
1607 GNUNET_STATISTICS_update (GSC_stats,
1609 ("# bytes dropped (ancient message)"), size,
1614 /* process decrypted message(s) */
1615 update_timeout (kx);
1616 GNUNET_STATISTICS_update (GSC_stats,
1617 gettext_noop ("# bytes of payload decrypted"),
1618 size - sizeof (struct EncryptedMessage),
1621 dmc.peer = kx->peer;
1623 GNUNET_SERVER_mst_receive (mst,
1625 &buf[sizeof (struct EncryptedMessage)],
1626 size - sizeof (struct EncryptedMessage),
1629 GNUNET_break_op (0);
1634 * One of our neighbours has excess bandwidth, remember this.
1637 * @param pid identity of the peer with excess bandwidth
1638 * @param connect_cls the `struct Neighbour`
1641 handle_transport_notify_excess_bw (void *cls,
1642 const struct GNUNET_PeerIdentity *pid,
1645 struct GSC_KeyExchangeInfo *kx = connect_cls;
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "Peer %s has excess bandwidth available\n",
1650 kx->has_excess_bandwidth = GNUNET_YES;
1651 GSC_SESSIONS_solicit (pid);
1656 * Deliver P2P message to interested clients. Invokes send twice,
1657 * once for clients that want the full message, and once for clients
1658 * that only want the header
1660 * @param cls always NULL
1661 * @param client who sent us the message (struct GSC_KeyExchangeInfo)
1662 * @param m the message
1665 deliver_message (void *cls,
1667 const struct GNUNET_MessageHeader *m)
1669 struct DeliverMessageContext *dmc = client;
1671 if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
1673 GNUNET_STATISTICS_update (GSC_stats,
1674 gettext_noop ("# PAYLOAD dropped (out of order)"),
1679 switch (ntohs (m->type))
1681 case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
1682 case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
1683 GSC_SESSIONS_set_typemap (dmc->peer, m);
1685 case GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP:
1686 GSC_SESSIONS_confirm_typemap (dmc->peer, m);
1689 GSC_CLIENTS_deliver_message (dmc->peer,
1692 GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
1693 GSC_CLIENTS_deliver_message (dmc->peer,
1695 sizeof (struct GNUNET_MessageHeader),
1696 GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
1703 * Setup the message that links the ephemeral key to our persistent
1704 * public key and generate the appropriate signature.
1707 sign_ephemeral_key ()
1709 current_ekm.header.size = htons (sizeof (struct EphemeralKeyMessage));
1710 current_ekm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY);
1711 current_ekm.sender_status = 0; /* to be set later */
1712 current_ekm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY);
1713 current_ekm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1714 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1715 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1716 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
1717 sizeof (struct GNUNET_PeerIdentity));
1718 current_ekm.creation_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1720 GNUNET_CONFIGURATION_get_value_yesno (GSC_cfg,
1722 "USE_EPHEMERAL_KEYS"))
1724 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_add (REKEY_FREQUENCY,
1729 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1731 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key,
1732 ¤t_ekm.ephemeral_key);
1733 current_ekm.origin_identity = GSC_my_identity;
1734 GNUNET_assert (GNUNET_OK ==
1735 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1736 ¤t_ekm.purpose,
1737 ¤t_ekm.signature));
1742 * Task run to trigger rekeying.
1744 * @param cls closure, NULL
1747 do_rekey (void *cls)
1749 struct GSC_KeyExchangeInfo *pos;
1751 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1754 if (NULL != my_ephemeral_key)
1755 GNUNET_free (my_ephemeral_key);
1756 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1758 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1759 GNUNET_assert (NULL != my_ephemeral_key);
1760 sign_ephemeral_key ();
1761 for (pos = kx_head; NULL != pos; pos = pos->next)
1763 if (GNUNET_CORE_KX_STATE_UP == pos->status)
1765 pos->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
1766 monitor_notify_all (pos);
1767 derive_session_keys (pos);
1769 if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
1771 pos->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1772 monitor_notify_all (pos);
1774 monitor_notify_all (pos);
1781 * Initialize KX subsystem.
1783 * @param pk private key to use for the peer
1784 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1787 GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
1789 struct GNUNET_MQ_MessageHandler handlers[] = {
1790 GNUNET_MQ_hd_fixed_size (ephemeral_key,
1791 GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY,
1792 struct EphemeralKeyMessage,
1794 GNUNET_MQ_hd_fixed_size (ping,
1795 GNUNET_MESSAGE_TYPE_CORE_PING,
1798 GNUNET_MQ_hd_fixed_size (pong,
1799 GNUNET_MESSAGE_TYPE_CORE_PONG,
1802 GNUNET_MQ_hd_var_size (encrypted,
1803 GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE,
1804 struct EncryptedMessage,
1806 GNUNET_MQ_handler_end()
1809 my_private_key = pk;
1810 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1811 &GSC_my_identity.public_key);
1812 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1813 if (NULL == my_ephemeral_key)
1816 GNUNET_free (my_private_key);
1817 my_private_key = NULL;
1818 return GNUNET_SYSERR;
1820 sign_ephemeral_key ();
1821 nc = GNUNET_notification_context_create (1);
1822 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1825 mst = GNUNET_SERVER_mst_create (&deliver_message,
1828 = GNUNET_TRANSPORT_core_connect (GSC_cfg,
1832 &handle_transport_notify_connect,
1833 &handle_transport_notify_disconnect,
1834 &handle_transport_notify_excess_bw);
1835 if (NULL == transport)
1838 return GNUNET_SYSERR;
1845 * Shutdown KX subsystem.
1850 if (NULL != transport)
1852 GNUNET_TRANSPORT_core_disconnect (transport);
1855 if (NULL != rekey_task)
1857 GNUNET_SCHEDULER_cancel (rekey_task);
1860 if (NULL != my_ephemeral_key)
1862 GNUNET_free (my_ephemeral_key);
1863 my_ephemeral_key = NULL;
1865 if (NULL != my_private_key)
1867 GNUNET_free (my_private_key);
1868 my_private_key = NULL;
1872 GNUNET_SERVER_mst_destroy (mst);
1877 GNUNET_notification_context_destroy (nc);
1884 * Check how many messages are queued for the given neighbour.
1886 * @param kxinfo data about neighbour to check
1887 * @return number of items in the message queue
1890 GSC_NEIGHBOURS_get_queue_length (const struct GSC_KeyExchangeInfo *kxinfo)
1892 return GNUNET_MQ_get_length (kxinfo->mq);
1897 * Check if the given neighbour has excess bandwidth available.
1899 * @param target neighbour to check
1900 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
1903 GSC_NEIGHBOURS_check_excess_bandwidth (const struct GSC_KeyExchangeInfo *kxinfo)
1905 return kxinfo->has_excess_bandwidth;
1910 * Handle #GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request. For this
1911 * request type, the client does not have to have transmitted an INIT
1912 * request. All current peers are returned, regardless of which
1913 * message types they accept.
1915 * @param mq message queue to add for monitoring
1918 GSC_KX_handle_client_monitor_peers (struct GNUNET_MQ_Handle *mq)
1920 struct GNUNET_MQ_Envelope *env;
1921 struct MonitorNotifyMessage *done_msg;
1922 struct GSC_KeyExchangeInfo *kx;
1924 GNUNET_notification_context_add (nc,
1926 for (kx = kx_head; NULL != kx; kx = kx->next)
1928 struct GNUNET_MQ_Envelope *env;
1929 struct MonitorNotifyMessage *msg;
1931 env = GNUNET_MQ_msg (msg,
1932 GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
1933 msg->state = htonl ((uint32_t) kx->status);
1934 msg->peer = *kx->peer;
1935 msg->timeout = GNUNET_TIME_absolute_hton (kx->timeout);
1939 env = GNUNET_MQ_msg (done_msg,
1940 GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
1941 done_msg->state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
1942 done_msg->timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1948 /* end of gnunet-service-core_kx.c */