2 This file is part of GNUnet.
3 (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 'origin' 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 reply 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 faking the reply harder. Must be
170 * first field after header (this is where we start to encrypt!).
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 either #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 'sequence_number'),
205 * used to verify message integrity. Everything after this value
206 * (excluding this value itself) will be encrypted and authenticated.
207 * ENCRYPTED_HEADER_SIZE must be set to the offset 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.
223 * Timestamp. Used to prevent reply 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 struct GNUNET_PeerIdentity peer;
261 * PING message we transmit to the other peer.
263 struct PingMessage ping;
266 * Ephemeral public ECC key of the other peer.
268 struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key;
271 * Key we use to encrypt our messages for the other peer
272 * (initialized by us when we do the handshake).
274 struct GNUNET_CRYPTO_SymmetricSessionKey encrypt_key;
277 * Key we use to decrypt messages from the other peer
278 * (given to us by the other peer during the handshake).
280 struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key;
283 * At what time did the other peer generate the decryption key?
285 struct GNUNET_TIME_Absolute foreign_key_expires;
288 * When should the session time out (if there are no PONGs)?
290 struct GNUNET_TIME_Absolute timeout;
293 * At what frequency are we currently re-trying SET_KEY messages?
295 struct GNUNET_TIME_Relative set_key_retry_frequency;
298 * ID of task used for re-trying SET_KEY and PING message.
300 GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
303 * ID of task used for sending keep-alive pings.
305 GNUNET_SCHEDULER_TaskIdentifier keep_alive_task;
308 * Bit map indicating which of the 32 sequence numbers before the last
309 * were received (good for accepting out-of-order packets and
310 * estimating reliability of the connection)
312 unsigned int last_packets_bitmap;
315 * last sequence number received on this connection (highest)
317 uint32_t last_sequence_number_received;
320 * last sequence number transmitted
322 uint32_t last_sequence_number_sent;
325 * What was our PING challenge number (for this peer)?
327 uint32_t ping_challenge;
330 * What is our connection status?
332 enum GNUNET_CORE_KxState status;
340 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
343 * Our ephemeral private key.
345 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
348 * Current message we send for a key exchange.
350 static struct EphemeralKeyMessage current_ekm;
353 * Our message stream tokenizer (for encrypted payload).
355 static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
360 static struct GSC_KeyExchangeInfo *kx_head;
365 static struct GSC_KeyExchangeInfo *kx_tail;
368 * Task scheduled for periodic re-generation (and thus rekeying) of our
371 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
374 * Notification context for all monitors.
376 static struct GNUNET_SERVER_NotificationContext *nc;
380 * Inform the given monitor about the KX state of
383 * @param mc monitor to inform
384 * @param kx key exchange state to inform about
387 monitor_notify (struct GNUNET_SERVER_Client *client,
388 struct GSC_KeyExchangeInfo *kx)
390 struct MonitorNotifyMessage msg;
392 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
393 msg.header.size = htons (sizeof (msg));
394 msg.state = htonl ((uint32_t) kx->status);
396 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
397 GNUNET_SERVER_notification_context_unicast (nc,
405 * Inform all monitors about the KX state of the given peer.
407 * @param kx key exchange state to inform about
410 monitor_notify_all (struct GSC_KeyExchangeInfo *kx)
412 struct MonitorNotifyMessage msg;
414 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
415 msg.header.size = htons (sizeof (msg));
416 msg.state = htonl ((uint32_t) kx->status);
418 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
419 GNUNET_SERVER_notification_context_broadcast (nc,
426 * Derive an authentication key from "set key" information
428 * @param akey authentication key to derive
429 * @param skey session key to use
430 * @param seed seed to use
433 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
434 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed)
436 static const char ctx[] = "authentication key";
438 GNUNET_CRYPTO_hmac_derive_key (akey, skey,
439 &seed, sizeof (seed),
440 skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
447 * Derive an IV from packet information
449 * @param iv initialization vector to initialize
450 * @param skey session key to use
451 * @param seed seed to use
452 * @param identity identity of the other peer to use
455 derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
456 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
457 const struct GNUNET_PeerIdentity *identity)
459 static const char ctx[] = "initialization vector";
461 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
463 sizeof (struct GNUNET_PeerIdentity), ctx,
469 * Derive an IV from pong packet information
471 * @param iv initialization vector to initialize
472 * @param skey session key to use
473 * @param seed seed to use
474 * @param challenge nonce to use
475 * @param identity identity of the other peer to use
478 derive_pong_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
479 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
480 uint32_t challenge, const struct GNUNET_PeerIdentity *identity)
482 static const char ctx[] = "pong initialization vector";
484 GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
486 sizeof (struct GNUNET_PeerIdentity),
487 &challenge, sizeof (challenge),
494 * Derive an AES key from key material
496 * @param sender peer identity of the sender
497 * @param receiver peer identity of the sender
498 * @param key_material high entropy key material to use
499 * @param skey set to derived session key
502 derive_aes_key (const struct GNUNET_PeerIdentity *sender,
503 const struct GNUNET_PeerIdentity *receiver,
504 const struct GNUNET_HashCode *key_material,
505 struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
507 static const char ctx[] = "aes key generation vector";
509 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
511 key_material, sizeof (struct GNUNET_HashCode),
512 sender, sizeof (struct GNUNET_PeerIdentity),
513 receiver, sizeof (struct GNUNET_PeerIdentity),
519 * Encrypt size bytes from @a in and write the result to @a out. Use the
520 * @a kx key for outbound traffic of the given neighbour.
522 * @param kx key information context
523 * @param iv initialization vector to use
524 * @param in ciphertext
525 * @param out plaintext
526 * @param size size of @a in/@a out
527 * @return #GNUNET_OK on success
530 do_encrypt (struct GSC_KeyExchangeInfo *kx,
531 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
532 const void *in, void *out, size_t size)
534 if (size != (uint16_t) size)
539 GNUNET_assert (size ==
540 GNUNET_CRYPTO_symmetric_encrypt (in, (uint16_t) size,
541 &kx->encrypt_key, iv, out));
542 GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# bytes encrypted"), size,
544 /* the following is too sensitive to write to log files by accident,
545 so we require manual intervention to get this one... */
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Encrypted %u bytes for `%4s' using key %u, IV %u\n",
549 (unsigned int) size, GNUNET_i2s (&kx->peer),
550 (unsigned int) kx->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
559 * Decrypt size bytes from @a in and write the result to @a out. Use the
560 * @a kx key for inbound traffic of the given neighbour. This function does
561 * NOT do any integrity-checks on the result.
563 * @param kx key information context
564 * @param iv initialization vector to use
565 * @param in ciphertext
566 * @param out plaintext
567 * @param size size of @a in / @a out
568 * @return #GNUNET_OK on success
571 do_decrypt (struct GSC_KeyExchangeInfo *kx,
572 const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
573 const void *in, void *out, size_t size)
575 if (size != (uint16_t) size)
580 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
581 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
582 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT) )
585 return GNUNET_SYSERR;
588 GNUNET_CRYPTO_symmetric_decrypt (in, (uint16_t) size, &kx->decrypt_key, iv,
592 return GNUNET_SYSERR;
594 GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# bytes decrypted"), size,
596 /* the following is too sensitive to write to log files by accident,
597 so we require manual intervention to get this one... */
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "Decrypted %u bytes from `%4s' using key %u, IV %u\n",
601 (unsigned int) size, GNUNET_i2s (&kx->peer),
602 (unsigned int) kx->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
611 * Send our key (and encrypted PING) to the other peer.
613 * @param kx key exchange context
616 send_key (struct GSC_KeyExchangeInfo *kx);
620 * Task that will retry #send_key() if our previous attempt failed.
622 * @param cls our `struct GSC_KeyExchangeInfo`
623 * @param tc scheduler context
626 set_key_retry_task (void *cls,
627 const struct GNUNET_SCHEDULER_TaskContext *tc)
629 struct GSC_KeyExchangeInfo *kx = cls;
631 kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
632 kx->set_key_retry_frequency = GNUNET_TIME_STD_BACKOFF (kx->set_key_retry_frequency);
633 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
639 * Create a fresh PING message for transmission to the other peer.
641 * @param kx key exchange context to create PING for
644 setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
646 struct PingMessage pp;
647 struct PingMessage *pm;
648 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
651 pm->header.size = htons (sizeof (struct PingMessage));
652 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
654 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
655 derive_iv (&iv, &kx->encrypt_key, pm->iv_seed, &kx->peer);
656 pp.challenge = kx->ping_challenge;
657 pp.target = kx->peer;
658 do_encrypt (kx, &iv, &pp.target, &pm->target,
659 sizeof (struct PingMessage) - ((void *) &pm->target -
665 * Start the key exchange with the given peer.
667 * @param pid identity of the peer to do a key exchange with
668 * @return key exchange information context
670 struct GSC_KeyExchangeInfo *
671 GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
673 struct GSC_KeyExchangeInfo *kx;
674 struct GNUNET_HashCode h1;
675 struct GNUNET_HashCode h2;
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "Initiating key exchange with `%s'\n",
680 GNUNET_STATISTICS_update (GSC_stats,
681 gettext_noop ("# key exchanges initiated"), 1,
683 kx = GNUNET_new (struct GSC_KeyExchangeInfo);
685 kx->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
686 GNUNET_CONTAINER_DLL_insert (kx_head,
689 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
690 monitor_notify_all (kx);
691 GNUNET_CRYPTO_hash (pid,
692 sizeof (struct GNUNET_PeerIdentity),
694 GNUNET_CRYPTO_hash (&GSC_my_identity,
695 sizeof (struct GNUNET_PeerIdentity),
697 if (0 < GNUNET_CRYPTO_hash_cmp (&h1,
700 /* peer with "lower" identity starts KX, otherwise we typically end up
701 with both peers starting the exchange and transmit the 'set key'
707 /* peer with "higher" identity starts a delayed KX, if the "lower" peer
708 * does not start a KX since he sees no reasons to do so */
709 kx->retry_set_key_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
710 &set_key_retry_task, kx);
717 * Stop key exchange with the given peer. Clean up key material.
719 * @param kx key exchange to stop
722 GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
724 GSC_SESSIONS_end (&kx->peer);
725 GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# key exchanges stopped"),
727 if (kx->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
729 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
730 kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
732 if (kx->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
734 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
735 kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
737 kx->status = GNUNET_CORE_KX_PEER_DISCONNECT;
738 monitor_notify_all (kx);
739 GNUNET_CONTAINER_DLL_remove (kx_head,
747 * Send our PING to the other peer.
749 * @param kx key exchange context
752 send_ping (struct GSC_KeyExchangeInfo *kx)
754 GSC_NEIGHBOURS_transmit (&kx->peer, &kx->ping.header,
760 * Derive fresh session keys from the current ephemeral keys.
762 * @param kx session to derive keys for
765 derive_session_keys (struct GSC_KeyExchangeInfo *kx)
767 struct GNUNET_HashCode key_material;
770 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
771 &kx->other_ephemeral_key,
777 derive_aes_key (&GSC_my_identity,
781 derive_aes_key (&kx->peer,
785 memset (&key_material, 0, sizeof (key_material));
786 /* fresh key, reset sequence numbers */
787 kx->last_sequence_number_received = 0;
788 kx->last_packets_bitmap = 0;
789 setup_fresh_ping (kx);
794 * We received a SET_KEY message. Validate and update
795 * our key material and status.
797 * @param kx key exchange status for the corresponding peer
798 * @param msg the set key message we received
801 GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
802 const struct GNUNET_MessageHeader *msg)
804 const struct EphemeralKeyMessage *m;
805 struct GNUNET_TIME_Absolute start_t;
806 struct GNUNET_TIME_Absolute end_t;
807 struct GNUNET_TIME_Absolute now;
808 enum GNUNET_CORE_KxState sender_status;
811 size = ntohs (msg->size);
812 if (sizeof (struct EphemeralKeyMessage) != size)
817 m = (const struct EphemeralKeyMessage *) msg;
818 end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
819 if ( ( (GNUNET_CORE_KX_STATE_KEY_RECEIVED == kx->status) ||
820 (GNUNET_CORE_KX_STATE_UP == kx->status) ||
821 (GNUNET_CORE_KX_STATE_REKEY_SENT == kx->status) ) &&
822 (end_t.abs_value_us <= kx->foreign_key_expires.abs_value_us) )
824 GNUNET_STATISTICS_update (GSC_stats,
825 gettext_noop ("# old ephemeral keys ignored"),
829 start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
831 GNUNET_STATISTICS_update (GSC_stats,
832 gettext_noop ("# ephemeral keys received"),
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Core service receives `%s' request from `%4s'.\n", "EPHEMERAL_KEY",
837 GNUNET_i2s (&kx->peer));
839 memcmp (&m->origin_identity,
840 &kx->peer.public_key,
841 sizeof (struct GNUNET_PeerIdentity)))
846 if ((ntohl (m->purpose.size) !=
847 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
848 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
849 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
850 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
851 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
853 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
855 &m->signature, &m->origin_identity.public_key)))
857 /* invalid signature */
861 now = GNUNET_TIME_absolute_get ();
862 if ( (end_t.abs_value_us < GNUNET_TIME_absolute_subtract (now, REKEY_TOLERANCE).abs_value_us) ||
863 (start_t.abs_value_us > GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value_us) )
865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
866 _("Ephemeral key message from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
867 GNUNET_i2s (&kx->peer),
869 start_t.abs_value_us,
873 kx->other_ephemeral_key = m->ephemeral_key;
874 kx->foreign_key_expires = end_t;
875 derive_session_keys (kx);
876 GNUNET_STATISTICS_update (GSC_stats,
877 gettext_noop ("# EPHEMERAL_KEY messages received"), 1,
880 /* check if we still need to send the sender our key */
881 sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
882 switch (sender_status)
884 case GNUNET_CORE_KX_STATE_DOWN:
887 case GNUNET_CORE_KX_STATE_KEY_SENT:
888 /* fine, need to send our key after updating our status, see below */
889 GSC_SESSIONS_reinit (&kx->peer);
891 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
892 /* other peer already got our key, but typemap did go down */
893 GSC_SESSIONS_reinit (&kx->peer);
895 case GNUNET_CORE_KX_STATE_UP:
896 /* other peer already got our key, typemap NOT down */
898 case GNUNET_CORE_KX_STATE_REKEY_SENT:
899 /* other peer already got our key, typemap NOT down */
905 /* check if we need to confirm everything is fine via PING + PONG */
908 case GNUNET_CORE_KX_STATE_DOWN:
909 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
910 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
911 monitor_notify_all (kx);
912 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
916 case GNUNET_CORE_KX_STATE_KEY_SENT:
917 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
918 kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
919 monitor_notify_all (kx);
920 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
924 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
925 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
926 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
930 case GNUNET_CORE_KX_STATE_UP:
931 kx->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
932 monitor_notify_all (kx);
933 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
935 /* we got a new key, need to reconfirm! */
938 case GNUNET_CORE_KX_STATE_REKEY_SENT:
939 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
941 /* we got a new key, need to reconfirm! */
952 * We received a PING message. Validate and transmit
955 * @param kx key exchange status for the corresponding peer
956 * @param msg the encrypted PING message itself
959 GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
960 const struct GNUNET_MessageHeader *msg)
962 const struct PingMessage *m;
963 struct PingMessage t;
964 struct PongMessage tx;
965 struct PongMessage tp;
966 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
969 msize = ntohs (msg->size);
970 if (msize != sizeof (struct PingMessage))
975 GNUNET_STATISTICS_update (GSC_stats,
976 gettext_noop ("# PING messages received"), 1,
978 if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
979 (kx->status != GNUNET_CORE_KX_STATE_UP) &&
980 (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT))
983 GNUNET_STATISTICS_update (GSC_stats,
984 gettext_noop ("# PING messages dropped (out of order)"), 1,
988 m = (const struct PingMessage *) msg;
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Core service receives `%s' request from `%4s'.\n", "PING",
991 GNUNET_i2s (&kx->peer));
992 derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
994 do_decrypt (kx, &iv, &m->target, &t.target,
995 sizeof (struct PingMessage) - ((void *) &m->target -
1002 memcmp (&t.target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1007 GNUNET_snprintf (sender, sizeof (sender), "%8s", GNUNET_i2s (&kx->peer));
1008 GNUNET_snprintf (peer, sizeof (peer), "%8s", GNUNET_i2s (&t.target));
1009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1011 ("Received PING from `%s' for different identity: I am `%s', PONG identity: `%s'\n"),
1012 sender, GNUNET_i2s (&GSC_my_identity), peer);
1013 GNUNET_break_op (0);
1016 /* construct PONG */
1018 tx.challenge = t.challenge;
1019 tx.target = t.target;
1020 tp.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
1021 tp.header.size = htons (sizeof (struct PongMessage));
1023 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1024 derive_pong_iv (&iv, &kx->encrypt_key, tp.iv_seed, t.challenge, &kx->peer);
1025 do_encrypt (kx, &iv, &tx.challenge, &tp.challenge,
1026 sizeof (struct PongMessage) - ((void *) &tp.challenge -
1028 GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# PONG messages created"),
1030 GSC_NEIGHBOURS_transmit (&kx->peer, &tp.header,
1031 GNUNET_TIME_UNIT_FOREVER_REL /* FIXME: timeout */ );
1036 * Task triggered when a neighbour entry is about to time out
1037 * (and we should prevent this by sending a PING).
1039 * @param cls the 'struct GSC_KeyExchangeInfo'
1040 * @param tc scheduler context (not used)
1043 send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1045 struct GSC_KeyExchangeInfo *kx = cls;
1046 struct GNUNET_TIME_Relative retry;
1047 struct GNUNET_TIME_Relative left;
1049 kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
1050 left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
1051 if (0 == left.rel_value_us)
1053 GNUNET_STATISTICS_update (GSC_stats,
1054 gettext_noop ("# sessions terminated by timeout"),
1056 GSC_SESSIONS_end (&kx->peer);
1057 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1058 monitor_notify_all (kx);
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending KEEPALIVE to `%s'\n",
1063 GNUNET_i2s (&kx->peer));
1064 GNUNET_STATISTICS_update (GSC_stats,
1065 gettext_noop ("# keepalive messages sent"), 1,
1067 setup_fresh_ping (kx);
1068 GSC_NEIGHBOURS_transmit (&kx->peer, &kx->ping.header,
1069 kx->set_key_retry_frequency);
1071 GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
1072 MIN_PING_FREQUENCY);
1073 kx->keep_alive_task =
1074 GNUNET_SCHEDULER_add_delayed (retry, &send_keep_alive, kx);
1079 * We've seen a valid message from the other peer.
1080 * Update the time when the session would time out
1081 * and delay sending our keep alive message further.
1083 * @param kx key exchange where we saw activity
1086 update_timeout (struct GSC_KeyExchangeInfo *kx)
1089 GNUNET_TIME_relative_to_absolute
1090 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1091 monitor_notify_all (kx);
1092 if (kx->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
1093 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1094 kx->keep_alive_task =
1095 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1096 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1097 2), &send_keep_alive, kx);
1102 * We received a PONG message. Validate and update our status.
1104 * @param kx key exchange context for the the PONG
1105 * @param msg the encrypted PONG message itself
1108 GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
1109 const struct GNUNET_MessageHeader *msg)
1111 const struct PongMessage *m;
1112 struct PongMessage t;
1113 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1116 msize = ntohs (msg->size);
1117 if (sizeof (struct PongMessage) != msize)
1119 GNUNET_break_op (0);
1122 GNUNET_STATISTICS_update (GSC_stats,
1123 gettext_noop ("# PONG messages received"), 1,
1127 case GNUNET_CORE_KX_STATE_DOWN:
1128 GNUNET_STATISTICS_update (GSC_stats,
1129 gettext_noop ("# PONG messages dropped (connection down)"), 1,
1132 case GNUNET_CORE_KX_STATE_KEY_SENT:
1133 GNUNET_STATISTICS_update (GSC_stats,
1134 gettext_noop ("# PONG messages dropped (out of order)"), 1,
1137 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1139 case GNUNET_CORE_KX_STATE_UP:
1141 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1147 m = (const struct PongMessage *) msg;
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Core service receives `%s' response from `%4s'.\n", "PONG",
1150 GNUNET_i2s (&kx->peer));
1151 /* mark as garbage, just to be sure */
1152 memset (&t, 255, sizeof (t));
1153 derive_pong_iv (&iv, &kx->decrypt_key, m->iv_seed, kx->ping_challenge,
1156 do_decrypt (kx, &iv, &m->challenge, &t.challenge,
1157 sizeof (struct PongMessage) - ((void *) &m->challenge -
1160 GNUNET_break_op (0);
1163 GNUNET_STATISTICS_update (GSC_stats,
1164 gettext_noop ("# PONG messages decrypted"), 1,
1166 if ((0 != memcmp (&t.target, &kx->peer, sizeof (struct GNUNET_PeerIdentity)))
1167 || (kx->ping_challenge != t.challenge))
1169 /* PONG malformed */
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
1172 "PONG", GNUNET_i2s (&kx->peer),
1173 (unsigned int) kx->ping_challenge);
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Received malformed `%s' received from `%4s' with challenge %u\n",
1176 "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received PONG from `%s'\n",
1180 GNUNET_i2s (&kx->peer));
1181 /* no need to resend key any longer */
1182 if (GNUNET_SCHEDULER_NO_TASK != kx->retry_set_key_task)
1184 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1185 kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
1189 case GNUNET_CORE_KX_STATE_DOWN:
1190 GNUNET_assert (0); /* should be impossible */
1192 case GNUNET_CORE_KX_STATE_KEY_SENT:
1193 GNUNET_assert (0); /* should be impossible */
1195 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
1196 GNUNET_STATISTICS_update (GSC_stats,
1198 ("# session keys confirmed via PONG"), 1,
1200 kx->status = GNUNET_CORE_KX_STATE_UP;
1201 monitor_notify_all (kx);
1202 GSC_SESSIONS_create (&kx->peer, kx);
1203 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
1204 update_timeout (kx);
1206 case GNUNET_CORE_KX_STATE_UP:
1207 GNUNET_STATISTICS_update (GSC_stats,
1209 ("# timeouts prevented via PONG"), 1,
1211 update_timeout (kx);
1213 case GNUNET_CORE_KX_STATE_REKEY_SENT:
1214 GNUNET_STATISTICS_update (GSC_stats,
1216 ("# rekey operations confirmed via PONG"), 1,
1218 kx->status = GNUNET_CORE_KX_STATE_UP;
1219 monitor_notify_all (kx);
1220 update_timeout (kx);
1230 * Send our key to the other peer.
1232 * @param kx key exchange context
1235 send_key (struct GSC_KeyExchangeInfo *kx)
1237 GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
1238 if (GNUNET_SCHEDULER_NO_TASK != kx->retry_set_key_task)
1240 GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
1241 kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
1243 /* always update sender status in SET KEY message */
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Sending key to `%s' (my status: %d)\n",
1246 GNUNET_i2s (&kx->peer),
1248 current_ekm.sender_status = htonl ((int32_t) (kx->status));
1249 GSC_NEIGHBOURS_transmit (&kx->peer, ¤t_ekm.header,
1250 kx->set_key_retry_frequency);
1251 kx->retry_set_key_task =
1252 GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
1253 &set_key_retry_task, kx);
1258 * Encrypt and transmit a message with the given payload.
1260 * @param kx key exchange context
1261 * @param payload payload of the message
1262 * @param payload_size number of bytes in 'payload'
1265 GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
1266 const void *payload, size_t payload_size)
1268 size_t used = payload_size + sizeof (struct EncryptedMessage);
1269 char pbuf[used]; /* plaintext */
1270 char cbuf[used]; /* ciphertext */
1271 struct EncryptedMessage *em; /* encrypted message */
1272 struct EncryptedMessage *ph; /* plaintext header */
1273 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1274 struct GNUNET_CRYPTO_AuthKey auth_key;
1276 ph = (struct EncryptedMessage *) pbuf;
1278 htonl (GNUNET_CRYPTO_random_u32
1279 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
1280 ph->sequence_number = htonl (++kx->last_sequence_number_sent);
1282 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1283 memcpy (&ph[1], payload, payload_size);
1285 em = (struct EncryptedMessage *) cbuf;
1286 em->header.size = htons (used);
1287 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1288 em->iv_seed = ph->iv_seed;
1289 derive_iv (&iv, &kx->encrypt_key, ph->iv_seed, &kx->peer);
1290 GNUNET_assert (GNUNET_OK ==
1291 do_encrypt (kx, &iv, &ph->sequence_number,
1292 &em->sequence_number,
1293 used - ENCRYPTED_HEADER_SIZE));
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted %u bytes for %s\n",
1295 used - ENCRYPTED_HEADER_SIZE, GNUNET_i2s (&kx->peer));
1296 derive_auth_key (&auth_key,
1299 GNUNET_CRYPTO_hmac (&auth_key, &em->sequence_number,
1300 used - ENCRYPTED_HEADER_SIZE, &em->hmac);
1301 GSC_NEIGHBOURS_transmit (&kx->peer, &em->header,
1302 GNUNET_TIME_UNIT_FOREVER_REL);
1307 * Closure for #deliver_message()
1309 struct DeliverMessageContext
1313 * Key exchange context.
1315 struct GSC_KeyExchangeInfo *kx;
1318 * Sender of the message.
1320 const struct GNUNET_PeerIdentity *peer;
1325 * We received an encrypted message. Decrypt, validate and
1326 * pass on to the appropriate clients.
1328 * @param kx key exchange context for encrypting the message
1329 * @param msg encrypted message
1332 GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
1333 const struct GNUNET_MessageHeader *msg)
1335 const struct EncryptedMessage *m;
1336 struct EncryptedMessage *pt; /* plaintext */
1337 struct GNUNET_HashCode ph;
1339 struct GNUNET_TIME_Absolute t;
1340 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1341 struct GNUNET_CRYPTO_AuthKey auth_key;
1342 struct DeliverMessageContext dmc;
1343 uint16_t size = ntohs (msg->size);
1344 char buf[size] GNUNET_ALIGN;
1347 sizeof (struct EncryptedMessage) + sizeof (struct GNUNET_MessageHeader))
1349 GNUNET_break_op (0);
1352 m = (const struct EncryptedMessage *) msg;
1353 if (GNUNET_CORE_KX_STATE_UP != kx->status)
1355 GNUNET_STATISTICS_update (GSC_stats,
1357 ("# DATA message dropped (out of order)"),
1361 if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
1363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1364 _("Session to peer `%s' went down due to key expiration (should not happen)\n"),
1365 GNUNET_i2s (&kx->peer));
1366 GNUNET_STATISTICS_update (GSC_stats,
1367 gettext_noop ("# sessions terminated by key expiration"),
1369 GSC_SESSIONS_end (&kx->peer);
1370 if (GNUNET_SCHEDULER_NO_TASK != kx->keep_alive_task)
1372 GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
1373 kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
1375 kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1376 monitor_notify_all (kx);
1382 derive_auth_key (&auth_key, &kx->decrypt_key, m->iv_seed);
1383 GNUNET_CRYPTO_hmac (&auth_key, &m->sequence_number,
1384 size - ENCRYPTED_HEADER_SIZE, &ph);
1385 if (0 != memcmp (&ph, &m->hmac, sizeof (struct GNUNET_HashCode)))
1387 /* checksum failed */
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Failed checksum validation for a message from `%s'\n",
1390 GNUNET_i2s (&kx->peer));
1393 derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1396 do_decrypt (kx, &iv, &m->sequence_number, &buf[ENCRYPTED_HEADER_SIZE],
1397 size - ENCRYPTED_HEADER_SIZE))
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Decrypted %u bytes from %s\n",
1401 size - ENCRYPTED_HEADER_SIZE,
1402 GNUNET_i2s (&kx->peer));
1403 pt = (struct EncryptedMessage *) buf;
1405 /* validate sequence number */
1406 snum = ntohl (pt->sequence_number);
1407 if (kx->last_sequence_number_received == snum)
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 "Received duplicate message, ignoring.\n");
1411 /* duplicate, ignore */
1412 GNUNET_STATISTICS_update (GSC_stats,
1413 gettext_noop ("# bytes dropped (duplicates)"),
1417 if ((kx->last_sequence_number_received > snum) &&
1418 (kx->last_sequence_number_received - snum > 32))
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Received ancient out of sequence message, ignoring.\n");
1422 /* ancient out of sequence, ignore */
1423 GNUNET_STATISTICS_update (GSC_stats,
1425 ("# bytes dropped (out of sequence)"), size,
1429 if (kx->last_sequence_number_received > snum)
1431 unsigned int rotbit = 1 << (kx->last_sequence_number_received - snum - 1);
1433 if ((kx->last_packets_bitmap & rotbit) != 0)
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436 "Received duplicate message, ignoring.\n");
1437 GNUNET_STATISTICS_update (GSC_stats,
1438 gettext_noop ("# bytes dropped (duplicates)"),
1440 /* duplicate, ignore */
1443 kx->last_packets_bitmap |= rotbit;
1445 if (kx->last_sequence_number_received < snum)
1447 unsigned int shift = (snum - kx->last_sequence_number_received);
1449 if (shift >= 8 * sizeof (kx->last_packets_bitmap))
1450 kx->last_packets_bitmap = 0;
1452 kx->last_packets_bitmap <<= shift;
1453 kx->last_sequence_number_received = snum;
1456 /* check timestamp */
1457 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
1458 if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
1459 MAX_MESSAGE_AGE.rel_value_us)
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Message received far too old (%s). Content ignored.\n",
1463 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (t),
1465 GNUNET_STATISTICS_update (GSC_stats,
1467 ("# bytes dropped (ancient message)"), size,
1472 /* process decrypted message(s) */
1473 update_timeout (kx);
1474 GNUNET_STATISTICS_update (GSC_stats,
1475 gettext_noop ("# bytes of payload decrypted"),
1476 size - sizeof (struct EncryptedMessage),
1479 dmc.peer = &kx->peer;
1481 GNUNET_SERVER_mst_receive (mst, &dmc,
1482 &buf[sizeof (struct EncryptedMessage)],
1483 size - sizeof (struct EncryptedMessage),
1486 GNUNET_break_op (0);
1491 * Deliver P2P message to interested clients.
1492 * Invokes send twice, once for clients that want the full message, and once
1493 * for clients that only want the header
1495 * @param cls always NULL
1496 * @param client who sent us the message (struct GSC_KeyExchangeInfo)
1497 * @param m the message
1500 deliver_message (void *cls,
1502 const struct GNUNET_MessageHeader *m)
1504 struct DeliverMessageContext *dmc = client;
1506 if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
1508 GNUNET_STATISTICS_update (GSC_stats,
1510 ("# PAYLOAD dropped (out of order)"),
1514 switch (ntohs (m->type))
1516 case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
1517 case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
1518 GSC_SESSIONS_set_typemap (dmc->peer, m);
1520 case GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP:
1521 GSC_SESSIONS_confirm_typemap (dmc->peer, m);
1524 GSC_CLIENTS_deliver_message (dmc->peer, m,
1526 GNUNET_CORE_OPTION_SEND_FULL_INBOUND);
1527 GSC_CLIENTS_deliver_message (dmc->peer, m,
1528 sizeof (struct GNUNET_MessageHeader),
1529 GNUNET_CORE_OPTION_SEND_HDR_INBOUND);
1536 * Setup the message that links the ephemeral key to our persistent
1537 * public key and generate the appropriate signature.
1540 sign_ephemeral_key ()
1542 current_ekm.header.size = htons (sizeof (struct EphemeralKeyMessage));
1543 current_ekm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY);
1544 current_ekm.sender_status = 0; /* to be set later */
1545 current_ekm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY);
1546 current_ekm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1547 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1548 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1549 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
1550 sizeof (struct GNUNET_PeerIdentity));
1551 current_ekm.creation_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1553 GNUNET_CONFIGURATION_get_value_yesno (GSC_cfg,
1555 "USE_EPHEMERAL_KEYS"))
1557 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_add (REKEY_FREQUENCY,
1562 current_ekm.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1564 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key,
1565 ¤t_ekm.ephemeral_key);
1566 current_ekm.origin_identity = GSC_my_identity;
1567 GNUNET_assert (GNUNET_OK ==
1568 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1569 ¤t_ekm.purpose,
1570 ¤t_ekm.signature));
1575 * Task run to trigger rekeying.
1577 * @param cls closure, NULL
1578 * @param tc scheduler context
1581 do_rekey (void *cls,
1582 const struct GNUNET_SCHEDULER_TaskContext *tc)
1584 struct GSC_KeyExchangeInfo *pos;
1586 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1589 if (NULL != my_ephemeral_key)
1590 GNUNET_free (my_ephemeral_key);
1591 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1592 GNUNET_assert (NULL != my_ephemeral_key);
1593 sign_ephemeral_key ();
1594 for (pos = kx_head; NULL != pos; pos = pos->next)
1596 if (GNUNET_CORE_KX_STATE_UP == pos->status)
1598 pos->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
1599 monitor_notify_all (pos);
1600 derive_session_keys (pos);
1602 if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
1604 pos->status = GNUNET_CORE_KX_STATE_KEY_SENT;
1605 monitor_notify_all (pos);
1607 monitor_notify_all (pos);
1614 * Initialize KX subsystem.
1616 * @param pk private key to use for the peer
1617 * @param server the server of the CORE service
1618 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1621 GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk,
1622 struct GNUNET_SERVER_Handle *server)
1624 nc = GNUNET_SERVER_notification_context_create (server,
1626 my_private_key = pk;
1627 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1628 &GSC_my_identity.public_key);
1629 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1630 if (NULL == my_ephemeral_key)
1633 GNUNET_free (my_private_key);
1634 my_private_key = NULL;
1635 return GNUNET_SYSERR;
1637 sign_ephemeral_key ();
1638 rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
1641 mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
1647 * Shutdown KX subsystem.
1652 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1654 GNUNET_SCHEDULER_cancel (rekey_task);
1655 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1657 if (NULL != my_ephemeral_key)
1659 GNUNET_free (my_ephemeral_key);
1660 my_ephemeral_key = NULL;
1662 if (NULL != my_private_key)
1664 GNUNET_free (my_private_key);
1665 my_private_key = NULL;
1669 GNUNET_SERVER_mst_destroy (mst);
1674 GNUNET_SERVER_notification_context_destroy (nc);
1681 * Handle #GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request. For this
1682 * request type, the client does not have to have transmitted an INIT
1683 * request. All current peers are returned, regardless of which
1684 * message types they accept.
1687 * @param client client sending the iteration request
1688 * @param message iteration request message
1691 GSC_KX_handle_client_monitor_peers (void *cls,
1692 struct GNUNET_SERVER_Client *client,
1693 const struct GNUNET_MessageHeader *message)
1695 struct MonitorNotifyMessage done_msg;
1696 struct GSC_KeyExchangeInfo *kx;
1698 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1699 GNUNET_SERVER_notification_context_add (nc,
1701 for (kx = kx_head; NULL != kx; kx = kx->next)
1702 monitor_notify (client, kx);
1703 done_msg.header.size = htons (sizeof (struct MonitorNotifyMessage));
1704 done_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
1705 done_msg.state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
1706 memset (&done_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1707 done_msg.timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1708 GNUNET_SERVER_notification_context_unicast (nc,
1715 /* end of gnunet-service-core_kx.c */