2 This file is part of GNUnet.
3 Copyright (C) 2009-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 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_neighbours.h"
31 #include "gnunet-service-core_sessions.h"
32 #include "gnunet_statistics_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 of
91 * 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 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 authenticated.
206 * #ENCRYPTED_HEADER_SIZE must be set to the offset of the *next* field.
208 struct GNUNET_HashCode hmac;
211 * Sequence number, in network byte order. This field
212 * must be the first encrypted/decrypted field
214 uint32_t sequence_number GNUNET_PACKED;
217 * Reserved, always zero.
222 * Timestamp. Used to prevent replay of ancient messages
223 * (recent messages are caught with the sequence number).
225 struct GNUNET_TIME_AbsoluteNBO timestamp;
228 GNUNET_NETWORK_STRUCT_END
232 * Number of bytes (at the beginning) of `struct EncryptedMessage`
233 * that are NOT encrypted.
235 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
239 * Information about the status of a key exchange with another peer.
241 struct GSC_KeyExchangeInfo
247 struct GSC_KeyExchangeInfo *next;
252 struct GSC_KeyExchangeInfo *prev;
255 * Identity of the peer.
257 struct GNUNET_PeerIdentity peer;
260 * PING message we transmit to the other peer.
262 struct PingMessage ping;
265 * Ephemeral public ECC key of the other peer.
267 struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key;
270 * Key we use to encrypt our messages for the other peer
271 * (initialized by us when we do the handshake).
273 struct GNUNET_CRYPTO_SymmetricSessionKey encrypt_key;
276 * Key we use to decrypt messages from the other peer
277 * (given to us by the other peer during the handshake).
279 struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key;
282 * At what time did the other peer generate the decryption key?
284 struct GNUNET_TIME_Absolute foreign_key_expires;
287 * When should the session time out (if there are no PONGs)?
289 struct GNUNET_TIME_Absolute timeout;
292 * What was the last timeout we informed our monitors about?
294 struct GNUNET_TIME_Absolute last_notify_timeout;
297 * At what frequency are we currently re-trying SET_KEY messages?
299 struct GNUNET_TIME_Relative set_key_retry_frequency;
302 * ID of task used for re-trying SET_KEY and PING message.
304 struct GNUNET_SCHEDULER_Task *retry_set_key_task;
307 * ID of task used for sending keep-alive pings.
309 struct GNUNET_SCHEDULER_Task *keep_alive_task;
312 * Bit map indicating which of the 32 sequence numbers before the last
313 * were received (good for accepting out-of-order packets and
314 * estimating reliability of the connection)
316 unsigned int last_packets_bitmap;
319 * last sequence number received on this connection (highest)
321 uint32_t last_sequence_number_received;
324 * last sequence number transmitted
326 uint32_t last_sequence_number_sent;
329 * What was our PING challenge number (for this peer)?
331 uint32_t ping_challenge;
334 * What is our connection status?
336 enum GNUNET_CORE_KxState status;
344 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
347 * Our ephemeral private key.
349 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
352 * Current message we send for a key exchange.
354 static struct EphemeralKeyMessage current_ekm;
357 * Our message stream tokenizer (for encrypted payload).
359 static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
364 static struct GSC_KeyExchangeInfo *kx_head;
369 static struct GSC_KeyExchangeInfo *kx_tail;
372 * Task scheduled for periodic re-generation (and thus rekeying) of our
375 static struct GNUNET_SCHEDULER_Task *rekey_task;
378 * Notification context for all monitors.
380 static struct GNUNET_SERVER_NotificationContext *nc;
384 * Inform the given monitor about the KX state of
387 * @param client client to inform
388 * @param kx key exchange state to inform about
391 monitor_notify (struct GNUNET_SERVER_Client *client,
392 struct GSC_KeyExchangeInfo *kx)
394 struct MonitorNotifyMessage msg;
396 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
397 msg.header.size = htons (sizeof (msg));
398 msg.state = htonl ((uint32_t) kx->status);
400 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
401 GNUNET_SERVER_notification_context_unicast (nc,
409 * Calculate seed value we should use for a message.
411 * @param kx key exchange context
414 calculate_seed (struct GSC_KeyExchangeInfo *kx)
416 /* Note: may want to make this non-random and instead
417 derive from key material to avoid having an undetectable
419 return htonl (GNUNET_CRYPTO_random_u32
420 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
425 * Inform all monitors about the KX state of the given peer.
427 * @param kx key exchange state to inform about
430 monitor_notify_all (struct GSC_KeyExchangeInfo *kx)
432 struct MonitorNotifyMessage msg;
434 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
435 msg.header.size = htons (sizeof (msg));
436 msg.state = htonl ((uint32_t) kx->status);
438 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
439 GNUNET_SERVER_notification_context_broadcast (nc,
442 kx->last_notify_timeout = kx->timeout;
447 * Derive an authentication key from "set key" information
449 * @param akey authentication key to derive
450 * @param skey session key to use
451 * @param seed seed to use
454 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
455 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
458 static const char ctx[] = "authentication key";
460 GNUNET_CRYPTO_hmac_derive_key (akey, skey,
461 &seed, sizeof (seed),
462 skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
469 * Derive an IV from packet information
471 * @param iv initialization vector to initialize
472 * @param skey session key to use
473 * @param seed seed to use
474 * @param identity identity of the other peer to use
477 derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
478 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
480 const struct GNUNET_PeerIdentity *identity)
482 static const char ctx[] = "initialization vector";
484 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
485 &seed, sizeof (seed),
487 sizeof (struct GNUNET_PeerIdentity), ctx,
493 * Derive an IV from pong packet information
495 * @param iv initialization vector to initialize
496 * @param skey session key to use
497 * @param seed seed to use
498 * @param challenge nonce to use
499 * @param identity identity of the other peer to use
502 derive_pong_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
503 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
506 const struct GNUNET_PeerIdentity *identity)
508 static const char ctx[] = "pong initialization vector";
510 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
511 &seed, sizeof (seed),
513 sizeof (struct GNUNET_PeerIdentity),
514 &challenge, sizeof (challenge),
521 * Derive an AES key from key material
523 * @param sender peer identity of the sender
524 * @param receiver peer identity of the sender
525 * @param key_material high entropy key material to use
526 * @param skey set to derived session key
529 derive_aes_key (const struct GNUNET_PeerIdentity *sender,
530 const struct GNUNET_PeerIdentity *receiver,
531 const struct GNUNET_HashCode *key_material,
532 struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
534 static const char ctx[] = "aes key generation vector";
536 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
538 key_material, sizeof (struct GNUNET_HashCode),
539 sender, sizeof (struct GNUNET_PeerIdentity),
540 receiver, sizeof (struct GNUNET_PeerIdentity),
546 * Encrypt size bytes from @a in and write the result to @a out. Use the
547 * @a kx key for outbound traffic of the given neighbour.
549 * @param kx key information context
550 * @param iv initialization vector to use
551 * @param in ciphertext
552 * @param out plaintext
553 * @param size size of @a in/@a out
554 * @return #GNUNET_OK on success
557 do_encrypt (struct GSC_KeyExchangeInfo *kx,
558 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
563 if (size != (uint16_t) size)
568 GNUNET_assert (size ==
569 GNUNET_CRYPTO_symmetric_encrypt (in, (uint16_t) size,
570 &kx->encrypt_key, iv, out));
571 GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# bytes encrypted"), size,
573 /* the following is too sensitive to write to log files by accident,
574 so we require manual intervention to get this one... */
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "Encrypted %u bytes for `%s' using key %u, IV %u\n",
579 GNUNET_i2s (&kx->peer),
580 (unsigned int) kx->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
589 * Decrypt size bytes from @a in and write the result to @a out. Use the
590 * @a kx key for inbound traffic of the given neighbour. This function does
591 * NOT do any integrity-checks on the result.
593 * @param kx key information context
594 * @param iv initialization vector to use
595 * @param in ciphertext
596 * @param out plaintext
597 * @param size size of @a in / @a out
598 * @return #GNUNET_OK on success
601 do_decrypt (struct GSC_KeyExchangeInfo *kx,
602 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
607 if (size != (uint16_t) size)
612 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
613 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
614 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT) )
617 return GNUNET_SYSERR;
620 GNUNET_CRYPTO_symmetric_decrypt (in,
627 return GNUNET_SYSERR;
629 GNUNET_STATISTICS_update (GSC_stats,
630 gettext_noop ("# bytes decrypted"),
633 /* the following is too sensitive to write to log files by accident,
634 so we require manual intervention to get this one... */
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "Decrypted %u bytes from `%s' using key %u, IV %u\n",
639 GNUNET_i2s (&kx->peer),
640 (unsigned int) kx->decrypt_key.crc32,
641 GNUNET_CRYPTO_crc32_n (iv,
650 * Send our key (and encrypted PING) to the other peer.
652 * @param kx key exchange context
655 send_key (struct GSC_KeyExchangeInfo *kx);
659 * Task that will retry #send_key() if our previous attempt failed.
661 * @param cls our `struct GSC_KeyExchangeInfo`
662 * @param tc scheduler context
665 set_key_retry_task (void *cls,
666 const struct GNUNET_SCHEDULER_TaskContext *tc)
668 struct GSC_KeyExchangeInfo *kx = cls;
670 kx->retry_set_key_task = NULL;
671 kx->set_key_retry_frequency = GNUNET_TIME_STD_BACKOFF (kx->set_key_retry_frequency);
672 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
678 * Create a fresh PING message for transmission to the other peer.
680 * @param kx key exchange context to create PING for
683 setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
685 struct PingMessage pp;
686 struct PingMessage *pm;
687 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
690 pm->header.size = htons (sizeof (struct PingMessage));
691 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
692 pm->iv_seed = calculate_seed (kx);
693 derive_iv (&iv, &kx->encrypt_key, pm->iv_seed, &kx->peer);
694 pp.challenge = kx->ping_challenge;
695 pp.target = kx->peer;
696 do_encrypt (kx, &iv, &pp.target, &pm->target,
697 sizeof (struct PingMessage) - ((void *) &pm->target -
703 * Start the key exchange with the given peer.
705 * @param pid identity of the peer to do a key exchange with
706 * @return key exchange information context
708 struct GSC_KeyExchangeInfo *
709 GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
711 struct GSC_KeyExchangeInfo *kx;
712 struct GNUNET_HashCode h1;
713 struct GNUNET_HashCode h2;
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Initiating key exchange with `%s'\n",
718 GNUNET_STATISTICS_update (GSC_stats,
719 gettext_noop ("# key exchanges initiated"), 1,
721 kx = GNUNET_new (struct GSC_KeyExchangeInfo);
723 kx->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
724 GNUNET_CONTAINER_DLL_insert (kx_head,
727 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
728 monitor_notify_all (kx);
729 GNUNET_CRYPTO_hash (pid,
730 sizeof (struct GNUNET_PeerIdentity),
732 GNUNET_CRYPTO_hash (&GSC_my_identity,
733 sizeof (struct GNUNET_PeerIdentity),
735 if (0 < GNUNET_CRYPTO_hash_cmp (&h1,
738 /* peer with "lower" identity starts KX, otherwise we typically end up
739 with both peers starting the exchange and transmit the 'set key'
745 /* peer with "higher" identity starts a delayed KX, if the "lower" peer
746 * does not start a KX since he sees no reasons to do so */
747 kx->retry_set_key_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
748 &set_key_retry_task, kx);
755 * Stop key exchange with the given peer. Clean up key material.
757 * @param kx key exchange to stop
760 GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
762 GSC_SESSIONS_end (&kx->peer);
763 GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# key exchanges stopped"),
765 if (NULL != kx->retry_set_key_task)
767 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
768 kx->retry_set_key_task = NULL;
770 if (NULL != kx->keep_alive_task)
772 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
773 kx->keep_alive_task = NULL;
775 kx->status = GNUNET_CORE_KX_PEER_DISCONNECT;
776 monitor_notify_all (kx);
777 GNUNET_CONTAINER_DLL_remove (kx_head,
785 * Send our PING to the other peer.
787 * @param kx key exchange context
790 send_ping (struct GSC_KeyExchangeInfo *kx)
792 GNUNET_STATISTICS_update (GSC_stats,
793 gettext_noop ("# PING messages transmitted"),
796 GSC_NEIGHBOURS_transmit (&kx->peer,
803 * Derive fresh session keys from the current ephemeral keys.
805 * @param kx session to derive keys for
808 derive_session_keys (struct GSC_KeyExchangeInfo *kx)
810 struct GNUNET_HashCode key_material;
813 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
814 &kx->other_ephemeral_key,
820 derive_aes_key (&GSC_my_identity,
824 derive_aes_key (&kx->peer,
828 memset (&key_material, 0, sizeof (key_material));
829 /* fresh key, reset sequence numbers */
830 kx->last_sequence_number_received = 0;
831 kx->last_packets_bitmap = 0;
832 setup_fresh_ping (kx);
837 * We received a SET_KEY message. Validate and update
838 * our key material and status.
840 * @param kx key exchange status for the corresponding peer
841 * @param msg the set key message we received
844 GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
845 const struct GNUNET_MessageHeader *msg)
847 const struct EphemeralKeyMessage *m;
848 struct GNUNET_TIME_Absolute start_t;
849 struct GNUNET_TIME_Absolute end_t;
850 struct GNUNET_TIME_Absolute now;
851 enum GNUNET_CORE_KxState sender_status;
854 size = ntohs (msg->size);
855 if (sizeof (struct EphemeralKeyMessage) != size)
860 m = (const struct EphemeralKeyMessage *) msg;
861 end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
862 if ( ( (GNUNET_CORE_KX_STATE_KEY_RECEIVED == kx->status) ||
863 (GNUNET_CORE_KX_STATE_UP == kx->status) ||
864 (GNUNET_CORE_KX_STATE_REKEY_SENT == kx->status) ) &&
865 (end_t.abs_value_us < kx->foreign_key_expires.abs_value_us) )
867 GNUNET_STATISTICS_update (GSC_stats,
868 gettext_noop ("# old ephemeral keys ignored"),
872 start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
874 GNUNET_STATISTICS_update (GSC_stats,
875 gettext_noop ("# ephemeral keys received"),
879 memcmp (&m->origin_identity,
881 sizeof (struct GNUNET_PeerIdentity)))
883 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
884 "Received EPHEMERAL_KEY from %s, but expected %s\n",
885 GNUNET_i2s (&m->origin_identity),
886 GNUNET_i2s_full (&kx->peer));
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Core service receives EPHEMERAL_KEY request from `%s'.\n",
892 GNUNET_i2s (&kx->peer));
893 if ((ntohl (m->purpose.size) !=
894 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
895 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
896 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
897 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
898 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
900 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
903 &m->origin_identity.public_key)))
905 /* invalid signature */
909 now = GNUNET_TIME_absolute_get ();
910 if ( (end_t.abs_value_us < GNUNET_TIME_absolute_subtract (now, REKEY_TOLERANCE).abs_value_us) ||
911 (start_t.abs_value_us > GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value_us) )
913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
914 _("Ephemeral key message from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
915 GNUNET_i2s (&kx->peer),
917 start_t.abs_value_us,
921 kx->other_ephemeral_key = m->ephemeral_key;
922 kx->foreign_key_expires = end_t;
923 derive_session_keys (kx);
924 GNUNET_STATISTICS_update (GSC_stats,
925 gettext_noop ("# EPHEMERAL_KEY messages received"), 1,
928 /* check if we still need to send the sender our key */
929 sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
930 switch (sender_status)
932 case GNUNET_CORE_KX_STATE_DOWN:
935 case GNUNET_CORE_KX_STATE_KEY_SENT:
936 /* fine, need to send our key after updating our status, see below */
937 GSC_SESSIONS_reinit (&kx->peer);
939 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
940 /* other peer already got our key, but typemap did go down */
941 GSC_SESSIONS_reinit (&kx->peer);
943 case GNUNET_CORE_KX_STATE_UP:
944 /* other peer already got our key, typemap NOT down */
946 case GNUNET_CORE_KX_STATE_REKEY_SENT:
947 /* other peer already got our key, typemap NOT down */
953 /* check if we need to confirm everything is fine via PING + PONG */
956 case GNUNET_CORE_KX_STATE_DOWN:
957 GNUNET_assert (NULL == kx->keep_alive_task);
958 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
959 monitor_notify_all (kx);
960 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
965 case GNUNET_CORE_KX_STATE_KEY_SENT:
966 GNUNET_assert (NULL == kx->keep_alive_task);
967 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
968 monitor_notify_all (kx);
969 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
974 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
975 GNUNET_assert (NULL == kx->keep_alive_task);
976 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
981 case GNUNET_CORE_KX_STATE_UP:
982 kx->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
983 monitor_notify_all (kx);
984 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
989 case GNUNET_CORE_KX_STATE_REKEY_SENT:
990 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1003 * We received a PING message. Validate and transmit
1006 * @param kx key exchange status for the corresponding peer
1007 * @param msg the encrypted PING message itself
1010 GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
1011 const struct GNUNET_MessageHeader *msg)
1013 const struct PingMessage *m;
1014 struct PingMessage t;
1015 struct PongMessage tx;
1016 struct PongMessage tp;
1017 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1020 msize = ntohs (msg->size);
1021 if (msize != sizeof (struct PingMessage))
1023 GNUNET_break_op (0);
1026 GNUNET_STATISTICS_update (GSC_stats,
1027 gettext_noop ("# PING messages received"), 1,
1029 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
1030 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
1031 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT))
1034 GNUNET_STATISTICS_update (GSC_stats,
1035 gettext_noop ("# PING messages dropped (out of order)"), 1,
1039 m = (const struct PingMessage *) msg;
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Core service receives PING request from `%s'.\n",
1042 GNUNET_i2s (&kx->peer));
1043 derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1045 do_decrypt (kx, &iv, &m->target, &t.target,
1046 sizeof (struct PingMessage) - ((void *) &m->target -
1049 GNUNET_break_op (0);
1055 sizeof (struct GNUNET_PeerIdentity)))
1060 GNUNET_snprintf (sender, sizeof (sender), "%8s", GNUNET_i2s (&kx->peer));
1061 GNUNET_snprintf (peer, sizeof (peer), "%8s", GNUNET_i2s (&t.target));
1062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1063 _("Received PING from `%s' for different identity: I am `%s', PONG identity: `%s'\n"),
1065 GNUNET_i2s (&GSC_my_identity),
1067 GNUNET_break_op (0);
1070 /* construct PONG */
1072 tx.challenge = t.challenge;
1073 tx.target = t.target;
1074 tp.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
1075 tp.header.size = htons (sizeof (struct PongMessage));
1076 tp.iv_seed = calculate_seed (kx);
1077 derive_pong_iv (&iv,
1086 sizeof (struct PongMessage) - ((void *) &tp.challenge -
1088 GNUNET_STATISTICS_update (GSC_stats,
1089 gettext_noop ("# PONG messages created"),
1092 GSC_NEIGHBOURS_transmit (&kx->peer,
1094 GNUNET_TIME_UNIT_FOREVER_REL /* FIXME: timeout */ );
1099 * Task triggered when a neighbour entry is about to time out
1100 * (and we should prevent this by sending a PING).
1102 * @param cls the `struct GSC_KeyExchangeInfo`
1103 * @param tc scheduler context (not used)
1106 send_keep_alive (void *cls,
1107 const struct GNUNET_SCHEDULER_TaskContext *tc)
1109 struct GSC_KeyExchangeInfo *kx = cls;
1110 struct GNUNET_TIME_Relative retry;
1111 struct GNUNET_TIME_Relative left;
1113 kx->keep_alive_task = NULL;
1114 left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
1115 if (0 == left.rel_value_us)
1117 GNUNET_STATISTICS_update (GSC_stats,
1118 gettext_noop ("# sessions terminated by timeout"),
1121 GSC_SESSIONS_end (&kx->peer);
1122 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1123 monitor_notify_all (kx);
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Sending KEEPALIVE to `%s'\n",
1129 GNUNET_i2s (&kx->peer));
1130 GNUNET_STATISTICS_update (GSC_stats,
1131 gettext_noop ("# keepalive messages sent"),
1134 setup_fresh_ping (kx);
1137 GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
1138 MIN_PING_FREQUENCY);
1139 kx->keep_alive_task =
1140 GNUNET_SCHEDULER_add_delayed (retry,
1147 * We've seen a valid message from the other peer.
1148 * Update the time when the session would time out
1149 * and delay sending our keep alive message further.
1151 * @param kx key exchange where we saw activity
1154 update_timeout (struct GSC_KeyExchangeInfo *kx)
1156 struct GNUNET_TIME_Relative delta;
1159 GNUNET_TIME_relative_to_absolute
1160 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1161 delta = GNUNET_TIME_absolute_get_difference (kx->last_notify_timeout,
1163 if (delta.rel_value_us > 5LL * 1000LL * 1000LL)
1165 /* we only notify monitors about timeout changes if those
1166 are bigger than the threshold (5s) */
1167 monitor_notify_all (kx);
1169 if (kx->keep_alive_task != NULL)
1170 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1171 kx->keep_alive_task =
1172 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1173 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1174 2), &send_keep_alive, kx);
1179 * We received a PONG message. Validate and update our status.
1181 * @param kx key exchange context for the the PONG
1182 * @param msg the encrypted PONG message itself
1185 GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
1186 const struct GNUNET_MessageHeader *msg)
1188 const struct PongMessage *m;
1189 struct PongMessage t;
1190 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1193 msize = ntohs (msg->size);
1194 if (sizeof (struct PongMessage) != msize)
1196 GNUNET_break_op (0);
1199 GNUNET_STATISTICS_update (GSC_stats,
1200 gettext_noop ("# PONG messages received"), 1,
1204 case GNUNET_CORE_KX_STATE_DOWN:
1205 GNUNET_STATISTICS_update (GSC_stats,
1206 gettext_noop ("# PONG messages dropped (connection down)"), 1,
1209 case GNUNET_CORE_KX_STATE_KEY_SENT:
1210 GNUNET_STATISTICS_update (GSC_stats,
1211 gettext_noop ("# PONG messages dropped (out of order)"), 1,
1214 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1216 case GNUNET_CORE_KX_STATE_UP:
1218 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1224 m = (const struct PongMessage *) msg;
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Core service receives PONG response from `%s'.\n",
1227 GNUNET_i2s (&kx->peer));
1228 /* mark as garbage, just to be sure */
1229 memset (&t, 255, sizeof (t));
1230 derive_pong_iv (&iv, &kx->decrypt_key, m->iv_seed, kx->ping_challenge,
1233 do_decrypt (kx, &iv, &m->challenge, &t.challenge,
1234 sizeof (struct PongMessage) - ((void *) &m->challenge -
1237 GNUNET_break_op (0);
1240 GNUNET_STATISTICS_update (GSC_stats,
1241 gettext_noop ("# PONG messages decrypted"), 1,
1243 if ((0 != memcmp (&t.target, &kx->peer, sizeof (struct GNUNET_PeerIdentity)))
1244 || (kx->ping_challenge != t.challenge))
1246 /* PONG malformed */
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Received malformed `%s' wanted sender `%s' with challenge %u\n",
1249 "PONG", GNUNET_i2s (&kx->peer),
1250 (unsigned int) kx->ping_challenge);
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "Received malformed `%s' received from `%s' with challenge %u\n",
1253 "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Received PONG from `%s'\n",
1258 GNUNET_i2s (&kx->peer));
1259 /* no need to resend key any longer */
1260 if (NULL != kx->retry_set_key_task)
1262 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1263 kx->retry_set_key_task = NULL;
1267 case GNUNET_CORE_KX_STATE_DOWN:
1268 GNUNET_assert (0); /* should be impossible */
1270 case GNUNET_CORE_KX_STATE_KEY_SENT:
1271 GNUNET_assert (0); /* should be impossible */
1273 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1274 GNUNET_STATISTICS_update (GSC_stats,
1276 ("# session keys confirmed via PONG"), 1,
1278 kx->status = GNUNET_CORE_KX_STATE_UP;
1279 monitor_notify_all (kx);
1280 GSC_SESSIONS_create (&kx->peer, kx);
1281 GNUNET_assert (NULL == kx->keep_alive_task);
1282 update_timeout (kx);
1284 case GNUNET_CORE_KX_STATE_UP:
1285 GNUNET_STATISTICS_update (GSC_stats,
1287 ("# timeouts prevented via PONG"), 1,
1289 update_timeout (kx);
1291 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1292 GNUNET_STATISTICS_update (GSC_stats,
1294 ("# rekey operations confirmed via PONG"), 1,
1296 kx->status = GNUNET_CORE_KX_STATE_UP;
1297 monitor_notify_all (kx);
1298 update_timeout (kx);
1308 * Send our key to the other peer.
1310 * @param kx key exchange context
1313 send_key (struct GSC_KeyExchangeInfo *kx)
1315 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
1316 if (NULL != kx->retry_set_key_task)
1318 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1319 kx->retry_set_key_task = NULL;
1321 /* always update sender status in SET KEY message */
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Sending key to `%s' (my status: %d)\n",
1324 GNUNET_i2s (&kx->peer),
1326 current_ekm.sender_status = htonl ((int32_t) (kx->status));
1327 GSC_NEIGHBOURS_transmit (&kx->peer,
1328 ¤t_ekm.header,
1329 kx->set_key_retry_frequency);
1330 if (GNUNET_CORE_KX_STATE_KEY_SENT != kx->status)
1332 kx->retry_set_key_task =
1333 GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
1334 &set_key_retry_task, kx);
1339 * Encrypt and transmit a message with the given payload.
1341 * @param kx key exchange context
1342 * @param payload payload of the message
1343 * @param payload_size number of bytes in @a payload
1346 GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
1347 const void *payload,
1348 size_t payload_size)
1350 size_t used = payload_size + sizeof (struct EncryptedMessage);
1351 char pbuf[used]; /* plaintext */
1352 char cbuf[used]; /* ciphertext */
1353 struct EncryptedMessage *em; /* encrypted message */
1354 struct EncryptedMessage *ph; /* plaintext header */
1355 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1356 struct GNUNET_CRYPTO_AuthKey auth_key;
1358 ph = (struct EncryptedMessage *) pbuf;
1359 ph->sequence_number = htonl (++kx->last_sequence_number_sent);
1360 ph->iv_seed = calculate_seed (kx);
1362 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1363 memcpy (&ph[1], payload, payload_size);
1365 em = (struct EncryptedMessage *) cbuf;
1366 em->header.size = htons (used);
1367 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1368 em->iv_seed = ph->iv_seed;
1369 derive_iv (&iv, &kx->encrypt_key, ph->iv_seed, &kx->peer);
1370 GNUNET_assert (GNUNET_OK ==
1371 do_encrypt (kx, &iv, &ph->sequence_number,
1372 &em->sequence_number,
1373 used - ENCRYPTED_HEADER_SIZE));
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted %u bytes for %s\n",
1375 used - ENCRYPTED_HEADER_SIZE, GNUNET_i2s (&kx->peer));
1376 derive_auth_key (&auth_key,
1379 GNUNET_CRYPTO_hmac (&auth_key, &em->sequence_number,
1380 used - ENCRYPTED_HEADER_SIZE, &em->hmac);
1381 GSC_NEIGHBOURS_transmit (&kx->peer,
1383 GNUNET_TIME_UNIT_FOREVER_REL);
1388 * Closure for #deliver_message()
1390 struct DeliverMessageContext
1394 * Key exchange context.
1396 struct GSC_KeyExchangeInfo *kx;
1399 * Sender of the message.
1401 const struct GNUNET_PeerIdentity *peer;
1406 * We received an encrypted message. Decrypt, validate and
1407 * pass on to the appropriate clients.
1409 * @param kx key exchange context for encrypting the message
1410 * @param msg encrypted message
1413 GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
1414 const struct GNUNET_MessageHeader *msg)
1416 const struct EncryptedMessage *m;
1417 struct EncryptedMessage *pt; /* plaintext */
1418 struct GNUNET_HashCode ph;
1420 struct GNUNET_TIME_Absolute t;
1421 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1422 struct GNUNET_CRYPTO_AuthKey auth_key;
1423 struct DeliverMessageContext dmc;
1424 uint16_t size = ntohs (msg->size);
1425 char buf[size] GNUNET_ALIGN;
1428 sizeof (struct EncryptedMessage) + sizeof (struct GNUNET_MessageHeader))
1430 GNUNET_break_op (0);
1433 m = (const struct EncryptedMessage *) msg;
1434 if (GNUNET_CORE_KX_STATE_UP != kx->status)
1436 GNUNET_STATISTICS_update (GSC_stats,
1438 ("# DATA message dropped (out of order)"),
1442 if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
1444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1445 _("Session to peer `%s' went down due to key expiration (should not happen)\n"),
1446 GNUNET_i2s (&kx->peer));
1447 GNUNET_STATISTICS_update (GSC_stats,
1448 gettext_noop ("# sessions terminated by key expiration"),
1450 GSC_SESSIONS_end (&kx->peer);
1451 if (NULL != kx->keep_alive_task)
1453 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1454 kx->keep_alive_task = NULL;
1456 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1457 monitor_notify_all (kx);
1463 derive_auth_key (&auth_key, &kx->decrypt_key, m->iv_seed);
1464 GNUNET_CRYPTO_hmac (&auth_key, &m->sequence_number,
1465 size - ENCRYPTED_HEADER_SIZE, &ph);
1466 if (0 != memcmp (&ph, &m->hmac, sizeof (struct GNUNET_HashCode)))
1468 /* checksum failed */
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Failed checksum validation for a message from `%s'\n",
1471 GNUNET_i2s (&kx->peer));
1474 derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1477 do_decrypt (kx, &iv, &m->sequence_number, &buf[ENCRYPTED_HEADER_SIZE],
1478 size - ENCRYPTED_HEADER_SIZE))
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Decrypted %u bytes from %s\n",
1482 size - ENCRYPTED_HEADER_SIZE,
1483 GNUNET_i2s (&kx->peer));
1484 pt = (struct EncryptedMessage *) buf;
1486 /* validate sequence number */
1487 snum = ntohl (pt->sequence_number);
1488 if (kx->last_sequence_number_received == snum)
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Received duplicate message, ignoring.\n");
1492 /* duplicate, ignore */
1493 GNUNET_STATISTICS_update (GSC_stats,
1494 gettext_noop ("# bytes dropped (duplicates)"),
1498 if ((kx->last_sequence_number_received > snum) &&
1499 (kx->last_sequence_number_received - snum > 32))
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 "Received ancient out of sequence message, ignoring.\n");
1503 /* ancient out of sequence, ignore */
1504 GNUNET_STATISTICS_update (GSC_stats,
1506 ("# bytes dropped (out of sequence)"), size,
1510 if (kx->last_sequence_number_received > snum)
1512 unsigned int rotbit = 1 << (kx->last_sequence_number_received - snum - 1);
1514 if ((kx->last_packets_bitmap & rotbit) != 0)
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Received duplicate message, ignoring.\n");
1518 GNUNET_STATISTICS_update (GSC_stats,
1519 gettext_noop ("# bytes dropped (duplicates)"),
1521 /* duplicate, ignore */
1524 kx->last_packets_bitmap |= rotbit;
1526 if (kx->last_sequence_number_received < snum)
1528 unsigned int shift = (snum - kx->last_sequence_number_received);
1530 if (shift >= 8 * sizeof (kx->last_packets_bitmap))
1531 kx->last_packets_bitmap = 0;
1533 kx->last_packets_bitmap <<= shift;
1534 kx->last_sequence_number_received = snum;
1537 /* check timestamp */
1538 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
1539 if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
1540 MAX_MESSAGE_AGE.rel_value_us)
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Message received far too old (%s). Content ignored.\n",
1544 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (t),
1546 GNUNET_STATISTICS_update (GSC_stats,
1548 ("# bytes dropped (ancient message)"), size,
1553 /* process decrypted message(s) */
1554 update_timeout (kx);
1555 GNUNET_STATISTICS_update (GSC_stats,
1556 gettext_noop ("# bytes of payload decrypted"),
1557 size - sizeof (struct EncryptedMessage),
1560 dmc.peer = &kx->peer;
1562 GNUNET_SERVER_mst_receive (mst, &dmc,
1563 &buf[sizeof (struct EncryptedMessage)],
1564 size - sizeof (struct EncryptedMessage),
1567 GNUNET_break_op (0);
1572 * Deliver P2P message to interested clients.
1573 * Invokes send twice, once for clients that want the full message, and once
1574 * for clients that only want the header
1576 * @param cls always NULL
1577 * @param client who sent us the message (struct GSC_KeyExchangeInfo)
1578 * @param m the message
1581 deliver_message (void *cls,
1583 const struct GNUNET_MessageHeader *m)
1585 struct DeliverMessageContext *dmc = client;
1587 if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
1589 GNUNET_STATISTICS_update (GSC_stats,
1591 ("# PAYLOAD dropped (out of order)"),
1595 switch (ntohs (m->type))
1597 case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
1598 case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
1599 GSC_SESSIONS_set_typemap (dmc->peer, m);
1601 case GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP:
1602 GSC_SESSIONS_confirm_typemap (dmc->peer, m);
1605 GSC_CLIENTS_deliver_message (dmc->peer, m,
1607 GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
1608 GSC_CLIENTS_deliver_message (dmc->peer, m,
1609 sizeof (struct GNUNET_MessageHeader),
1610 GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
1617 * Setup the message that links the ephemeral key to our persistent
1618 * public key and generate the appropriate signature.
1621 sign_ephemeral_key ()
1623 current_ekm.header.size = htons (sizeof (struct EphemeralKeyMessage));
1624 current_ekm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY);
1625 current_ekm.sender_status = 0; /* to be set later */
1626 current_ekm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY);
1627 current_ekm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1628 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1629 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1630 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
1631 sizeof (struct GNUNET_PeerIdentity));
1632 current_ekm.creation_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1634 GNUNET_CONFIGURATION_get_value_yesno (GSC_cfg,
1636 "USE_EPHEMERAL_KEYS"))
1638 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_add (REKEY_FREQUENCY,
1643 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1645 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key,
1646 ¤t_ekm.ephemeral_key);
1647 current_ekm.origin_identity = GSC_my_identity;
1648 GNUNET_assert (GNUNET_OK ==
1649 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1650 ¤t_ekm.purpose,
1651 ¤t_ekm.signature));
1656 * Task run to trigger rekeying.
1658 * @param cls closure, NULL
1659 * @param tc scheduler context
1662 do_rekey (void *cls,
1663 const struct GNUNET_SCHEDULER_TaskContext *tc)
1665 struct GSC_KeyExchangeInfo *pos;
1667 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1670 if (NULL != my_ephemeral_key)
1671 GNUNET_free (my_ephemeral_key);
1672 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1673 GNUNET_assert (NULL != my_ephemeral_key);
1674 sign_ephemeral_key ();
1675 for (pos = kx_head; NULL != pos; pos = pos->next)
1677 if (GNUNET_CORE_KX_STATE_UP == pos->status)
1679 pos->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
1680 monitor_notify_all (pos);
1681 derive_session_keys (pos);
1683 if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
1685 pos->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1686 monitor_notify_all (pos);
1688 monitor_notify_all (pos);
1695 * Initialize KX subsystem.
1697 * @param pk private key to use for the peer
1698 * @param server the server of the CORE service
1699 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1702 GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk,
1703 struct GNUNET_SERVER_Handle *server)
1705 nc = GNUNET_SERVER_notification_context_create (server,
1707 my_private_key = pk;
1708 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1709 &GSC_my_identity.public_key);
1710 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1711 if (NULL == my_ephemeral_key)
1714 GNUNET_free (my_private_key);
1715 my_private_key = NULL;
1716 return GNUNET_SYSERR;
1718 sign_ephemeral_key ();
1719 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1722 mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
1728 * Shutdown KX subsystem.
1733 if (NULL != rekey_task)
1735 GNUNET_SCHEDULER_cancel (rekey_task);
1738 if (NULL != my_ephemeral_key)
1740 GNUNET_free (my_ephemeral_key);
1741 my_ephemeral_key = NULL;
1743 if (NULL != my_private_key)
1745 GNUNET_free (my_private_key);
1746 my_private_key = NULL;
1750 GNUNET_SERVER_mst_destroy (mst);
1755 GNUNET_SERVER_notification_context_destroy (nc);
1762 * Handle #GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request. For this
1763 * request type, the client does not have to have transmitted an INIT
1764 * request. All current peers are returned, regardless of which
1765 * message types they accept.
1768 * @param client client sending the iteration request
1769 * @param message iteration request message
1772 GSC_KX_handle_client_monitor_peers (void *cls,
1773 struct GNUNET_SERVER_Client *client,
1774 const struct GNUNET_MessageHeader *message)
1776 struct MonitorNotifyMessage done_msg;
1777 struct GSC_KeyExchangeInfo *kx;
1779 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1780 GNUNET_SERVER_notification_context_add (nc,
1782 for (kx = kx_head; NULL != kx; kx = kx->next)
1783 monitor_notify (client, kx);
1784 done_msg.header.size = htons (sizeof (struct MonitorNotifyMessage));
1785 done_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
1786 done_msg.state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
1787 memset (&done_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1788 done_msg.timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1789 GNUNET_SERVER_notification_context_unicast (nc,
1796 /* end of gnunet-service-core_kx.c */