GNUnet  0.11.x
gnunet-service-core_kx.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet-service-core_kx.h"
29 #include "gnunet-service-core.h"
33 #include "gnunet_constants.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_protocols.h"
36 #include "core.h"
37 
41 #define DEBUG_KX 0
42 
46 #define INITIAL_SET_KEY_RETRY_FREQUENCY \
47  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
48 
52 #define MIN_PING_FREQUENCY \
53  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
54 
58 #define REKEY_FREQUENCY \
59  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
60 
64 #define REKEY_TOLERANCE \
65  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
66 
74 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
75 
76 
78 
84 {
89 
94 
103 
109 
114 
120 };
122 
123 
128 #define ENCRYPTED_HEADER_SIZE \
129  (offsetof (struct EncryptedMessage, sequence_number))
130 
131 
136 {
141 
146 
150  const struct GNUNET_PeerIdentity *peer;
151 
156 
161 
166 
170  struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key;
171 
177 
183 
187  struct GNUNET_TIME_Absolute foreign_key_expires;
188 
193 
197  struct GNUNET_TIME_Absolute last_notify_timeout;
198 
202  struct GNUNET_TIME_Relative set_key_retry_frequency;
203 
208 
213 
220 
225 
230 
234  uint32_t ping_challenge;
235 
240 
245 };
246 
247 
252 
257 
262 
267 
272 
277 
283 
288 
289 
295 static uint32_t
297 {
298  /* Note: may want to make this non-random and instead
299  derive from key material to avoid having an undetectable
300  side-channel */
301  return htonl (
303 }
304 
305 
311 static void
313 {
314  struct MonitorNotifyMessage msg;
315 
317  msg.header.size = htons (sizeof(msg));
318  msg.state = htonl ((uint32_t) kx->status);
319  msg.peer = *kx->peer;
322  kx->last_notify_timeout = kx->timeout;
323 }
324 
325 
333 static void
335  const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
336  uint32_t seed)
337 {
338  static const char ctx[] = "authentication key";
339 
340 #if DEBUG_KX
341  struct GNUNET_HashCode sh;
342 
343  GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
345  "Deriving Auth key from SKEY %s and seed %u\n",
346  GNUNET_h2s (&sh),
347  (unsigned int) seed);
348 #endif
350  skey,
351  &seed,
352  sizeof(seed),
353  skey,
354  sizeof(
356  ctx,
357  sizeof(ctx),
358  NULL);
359 }
360 
361 
370 static void
372  const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
373  uint32_t seed,
374  const struct GNUNET_PeerIdentity *identity)
375 {
376  static const char ctx[] = "initialization vector";
377 
378 #if DEBUG_KX
379  struct GNUNET_HashCode sh;
380 
381  GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
383  "Deriving IV from SKEY %s and seed %u for peer %s\n",
384  GNUNET_h2s (&sh),
385  (unsigned int) seed,
386  GNUNET_i2s (identity));
387 #endif
389  skey,
390  &seed,
391  sizeof(seed),
392  identity,
393  sizeof(struct GNUNET_PeerIdentity),
394  ctx,
395  sizeof(ctx),
396  NULL);
397 }
398 
399 
409 static void
411  const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
412  uint32_t seed,
413  uint32_t challenge,
414  const struct GNUNET_PeerIdentity *identity)
415 {
416  static const char ctx[] = "pong initialization vector";
417 
418 #if DEBUG_KX
419  struct GNUNET_HashCode sh;
420 
421  GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
423  "Deriving PONG IV from SKEY %s and seed %u/%u for %s\n",
424  GNUNET_h2s (&sh),
425  (unsigned int) seed,
426  (unsigned int) challenge,
427  GNUNET_i2s (identity));
428 #endif
430  skey,
431  &seed,
432  sizeof(seed),
433  identity,
434  sizeof(struct GNUNET_PeerIdentity),
435  &challenge,
436  sizeof(challenge),
437  ctx,
438  sizeof(ctx),
439  NULL);
440 }
441 
442 
451 static void
452 derive_aes_key (const struct GNUNET_PeerIdentity *sender,
453  const struct GNUNET_PeerIdentity *receiver,
454  const struct GNUNET_HashCode *key_material,
456 {
457  static const char ctx[] = "aes key generation vector";
458 
459 #if DEBUG_KX
460  struct GNUNET_HashCode sh;
461 
462  GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
464  "Deriving AES Keys for %s to %s from %s\n",
465  GNUNET_i2s (sender),
466  GNUNET_i2s2 (receiver),
467  GNUNET_h2s (key_material));
468 #endif
469  GNUNET_CRYPTO_kdf (skey,
470  sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
471  ctx,
472  sizeof(ctx),
473  key_material,
474  sizeof(struct GNUNET_HashCode),
475  sender,
476  sizeof(struct GNUNET_PeerIdentity),
477  receiver,
478  sizeof(struct GNUNET_PeerIdentity),
479  NULL);
480 }
481 
482 
494 static int
497  const void *in,
498  void *out,
499  size_t size)
500 {
501  if (size != (uint16_t) size)
502  {
503  GNUNET_break (0);
504  return GNUNET_NO;
505  }
507  (uint16_t) size,
508  &kx->encrypt_key,
509  iv,
510  out));
512  gettext_noop ("# bytes encrypted"),
513  size,
514  GNUNET_NO);
515  /* the following is too sensitive to write to log files by accident,
516  so we require manual intervention to get this one... */
517 #if DEBUG_KX
519  "Encrypted %u bytes for `%s' using key %u, IV %u\n",
520  (unsigned int) size,
521  GNUNET_i2s (kx->peer),
522  (unsigned int) kx->encrypt_key.crc32,
523  GNUNET_CRYPTO_crc32_n (iv, sizeof(iv)));
524 #endif
525  return GNUNET_OK;
526 }
527 
528 
541 static int
544  const void *in,
545  void *out,
546  size_t size)
547 {
548  if (size != (uint16_t) size)
549  {
550  GNUNET_break (0);
551  return GNUNET_NO;
552  }
554  (kx->status != GNUNET_CORE_KX_STATE_UP) &&
556  {
557  GNUNET_break_op (0);
558  return GNUNET_SYSERR;
559  }
560  if (size != GNUNET_CRYPTO_symmetric_decrypt (in,
561  (uint16_t) size,
562  &kx->decrypt_key,
563  iv,
564  out))
565  {
566  GNUNET_break (0);
567  return GNUNET_SYSERR;
568  }
570  gettext_noop ("# bytes decrypted"),
571  size,
572  GNUNET_NO);
573  /* the following is too sensitive to write to log files by accident,
574  so we require manual intervention to get this one... */
575 #if DEBUG_KX
577  "Decrypted %u bytes from `%s' using key %u, IV %u\n",
578  (unsigned int) size,
579  GNUNET_i2s (kx->peer),
580  (unsigned int) kx->decrypt_key.crc32,
581  GNUNET_CRYPTO_crc32_n (iv, sizeof(*iv)));
582 #endif
583  return GNUNET_OK;
584 }
585 
586 
592 static void
593 send_key (struct GSC_KeyExchangeInfo *kx);
594 
595 
601 static void
603 {
604  struct GSC_KeyExchangeInfo *kx = cls;
605 
606  kx->retry_set_key_task = NULL;
610  send_key (kx);
611 }
612 
613 
619 static void
621 {
622  struct PingMessage pp;
623  struct PingMessage *pm;
625 
626  pm = &kx->ping;
627  kx->ping_challenge =
629  pm->header.size = htons (sizeof(struct PingMessage));
630  pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
631  pm->iv_seed = calculate_seed (kx);
632  derive_iv (&iv, &kx->encrypt_key, pm->iv_seed, kx->peer);
633  pp.challenge = kx->ping_challenge;
634  pp.target = *kx->peer;
635  do_encrypt (kx,
636  &iv,
637  &pp.target,
638  &pm->target,
639  sizeof(struct PingMessage)
640  - ((void *) &pm->target - (void *) pm));
641 }
642 
643 
655 static int
656 deliver_message (void *cls, const struct GNUNET_MessageHeader *m)
657 {
658  struct GSC_KeyExchangeInfo *kx = cls;
659 
661  "Decrypted message of type %d from %s\n",
662  ntohs (m->type),
663  GNUNET_i2s (kx->peer));
664  if (GNUNET_CORE_KX_STATE_UP != kx->status)
665  {
667  gettext_noop ("# PAYLOAD dropped (out of order)"),
668  1,
669  GNUNET_NO);
670  return GNUNET_OK;
671  }
672  switch (ntohs (m->type))
673  {
677  return GNUNET_OK;
678 
681  return GNUNET_OK;
682 
683  default:
685  m,
686  ntohs (m->size),
689  m,
690  sizeof(struct GNUNET_MessageHeader),
692  }
693  return GNUNET_OK;
694 }
695 
696 
706 static void *
708  const struct GNUNET_PeerIdentity *pid,
709  struct GNUNET_MQ_Handle *mq)
710 {
711  struct GSC_KeyExchangeInfo *kx;
712  struct GNUNET_HashCode h1;
713  struct GNUNET_HashCode h2;
714 
716  "Initiating key exchange with `%s'\n",
717  GNUNET_i2s (pid));
719  gettext_noop ("# key exchanges initiated"),
720  1,
721  GNUNET_NO);
722  kx = GNUNET_new (struct GSC_KeyExchangeInfo);
724  kx->mq = mq;
725  kx->peer = pid;
727  GNUNET_CONTAINER_DLL_insert (kx_head, kx_tail, kx);
729  monitor_notify_all (kx);
730  GNUNET_CRYPTO_hash (pid, sizeof(struct GNUNET_PeerIdentity), &h1);
732  sizeof(struct GNUNET_PeerIdentity),
733  &h2);
734  if (0 < GNUNET_CRYPTO_hash_cmp (&h1, &h2))
735  {
736  /* peer with "lower" identity starts KX, otherwise we typically end up
737  with both peers starting the exchange and transmit the 'set key'
738  message twice */
739  send_key (kx);
740  }
741  else
742  {
743  /* peer with "higher" identity starts a delayed KX, if the "lower" peer
744  * does not start a KX since it sees no reasons to do so */
745  kx->retry_set_key_task =
748  kx);
749  }
750  return kx;
751 }
752 
753 
763 static void
765  const struct GNUNET_PeerIdentity *peer,
766  void *handler_cls)
767 {
768  struct GSC_KeyExchangeInfo *kx = handler_cls;
769 
771  "Peer `%s' disconnected from us.\n",
772  GNUNET_i2s (peer));
773  GSC_SESSIONS_end (kx->peer);
775  gettext_noop ("# key exchanges stopped"),
776  1,
777  GNUNET_NO);
778  if (NULL != kx->retry_set_key_task)
779  {
781  kx->retry_set_key_task = NULL;
782  }
783  if (NULL != kx->keep_alive_task)
784  {
786  kx->keep_alive_task = NULL;
787  }
789  monitor_notify_all (kx);
790  GNUNET_CONTAINER_DLL_remove (kx_head, kx_tail, kx);
791  GNUNET_MST_destroy (kx->mst);
792  GNUNET_free (kx);
793 }
794 
795 
801 static void
803 {
804  struct GNUNET_MQ_Envelope *env;
805 
807  gettext_noop ("# PING messages transmitted"),
808  1,
809  GNUNET_NO);
810  env = GNUNET_MQ_msg_copy (&kx->ping.header);
811  GNUNET_MQ_send (kx->mq, env);
812 }
813 
814 
820 static void
822 {
823  struct GNUNET_HashCode key_material;
824 
825  if (GNUNET_OK !=
827  &kx->other_ephemeral_key,
828  &key_material))
829  {
830  GNUNET_break (0);
831  return;
832  }
833  derive_aes_key (&GSC_my_identity, kx->peer, &key_material, &kx->encrypt_key);
834  derive_aes_key (kx->peer, &GSC_my_identity, &key_material, &kx->decrypt_key);
835  memset (&key_material, 0, sizeof(key_material));
836  /* fresh key, reset sequence numbers */
838  kx->last_packets_bitmap = 0;
839  setup_fresh_ping (kx);
840 }
841 
842 
850 static void
851 handle_ephemeral_key (void *cls, const struct EphemeralKeyMessage *m)
852 {
853  struct GSC_KeyExchangeInfo *kx = cls;
854  struct GNUNET_TIME_Absolute start_t;
855  struct GNUNET_TIME_Absolute end_t;
856  struct GNUNET_TIME_Absolute now;
857  enum GNUNET_CORE_KxState sender_status;
858 
861  (GNUNET_CORE_KX_STATE_UP == kx->status) ||
864  {
866  gettext_noop ("# old ephemeral keys ignored"),
867  1,
868  GNUNET_NO);
870  "Received expired EPHEMERAL_KEY from %s\n",
872  return;
873  }
874  if (0 == memcmp (&m->ephemeral_key,
875  &kx->other_ephemeral_key,
876  sizeof(m->ephemeral_key)))
877  {
879  gettext_noop (
880  "# duplicate ephemeral keys ignored"),
881  1,
882  GNUNET_NO);
884  "Ignoring duplicate EPHEMERAL_KEY from %s\n",
886  return;
887  }
888  if (0 != memcmp (&m->origin_identity,
889  kx->peer,
890  sizeof(struct GNUNET_PeerIdentity)))
891  {
893  "Received EPHEMERAL_KEY from %s, but expected %s\n",
895  GNUNET_i2s_full (kx->peer));
896  GNUNET_break_op (0);
897  return;
898  }
899  if ((ntohl (m->purpose.size) !=
900  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
901  + sizeof(struct GNUNET_TIME_AbsoluteNBO)
902  + sizeof(struct GNUNET_TIME_AbsoluteNBO)
903  + sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
904  + sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)) ||
905  (GNUNET_OK !=
907  &m->purpose,
908  &m->signature,
910  {
911  /* invalid signature */
912  GNUNET_break_op (0);
914  gettext_noop (
915  "# EPHEMERAL_KEYs rejected (bad signature)"),
916  1,
917  GNUNET_NO);
919  "Received EPHEMERAL_KEY from %s with bad signature\n",
921  return;
922  }
923  now = GNUNET_TIME_absolute_get ();
925  if ((end_t.abs_value_us <
927  (start_t.abs_value_us >
928  GNUNET_TIME_absolute_add (now, REKEY_TOLERANCE).abs_value_us))
929  {
930  GNUNET_log (
932  _ (
933  "EPHEMERAL_KEY from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
934  GNUNET_i2s (kx->peer),
935  (unsigned long long) now.abs_value_us,
936  (unsigned long long) start_t.abs_value_us,
937  (unsigned long long) end_t.abs_value_us);
939  gettext_noop (
940  "# EPHEMERAL_KEY messages rejected due to time"),
941  1,
942  GNUNET_NO);
943  return;
944  }
945 #if DEBUG_KX
946  {
947  struct GNUNET_HashCode eh;
948 
949  GNUNET_CRYPTO_hash (&m->ephemeral_key, sizeof(m->ephemeral_key), &eh);
951  "Received valid EPHEMERAL_KEY `%s' from `%s' in state %d.\n",
952  GNUNET_h2s (&eh),
953  GNUNET_i2s (kx->peer),
954  kx->status);
955  }
956 #endif
958  gettext_noop ("# valid ephemeral keys received"),
959  1,
960  GNUNET_NO);
962  kx->foreign_key_expires = end_t;
963  derive_session_keys (kx);
964 
965  /* check if we still need to send the sender our key */
966  sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
967  switch (sender_status)
968  {
970  GNUNET_break_op (0);
971  break;
972 
974  /* fine, need to send our key after updating our status, see below */
976  break;
977 
979  /* other peer already got our key, but typemap did go down */
981  break;
982 
984  /* other peer already got our key, typemap NOT down */
985  break;
986 
988  /* other peer already got our key, typemap NOT down */
989  break;
990 
991  default:
992  GNUNET_break (0);
993  break;
994  }
995  /* check if we need to confirm everything is fine via PING + PONG */
996  switch (kx->status)
997  {
999  GNUNET_assert (NULL == kx->keep_alive_task);
1001  monitor_notify_all (kx);
1002  if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1003  send_key (kx);
1004  else
1005  send_ping (kx);
1006  break;
1007 
1009  GNUNET_assert (NULL == kx->keep_alive_task);
1011  monitor_notify_all (kx);
1012  if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1013  send_key (kx);
1014  else
1015  send_ping (kx);
1016  break;
1017 
1019  GNUNET_assert (NULL == kx->keep_alive_task);
1020  if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1021  send_key (kx);
1022  else
1023  send_ping (kx);
1024  break;
1025 
1028  monitor_notify_all (kx);
1029  if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1030  send_key (kx);
1031  else
1032  send_ping (kx);
1033  break;
1034 
1036  if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1037  send_key (kx);
1038  else
1039  send_ping (kx);
1040  break;
1041 
1042  default:
1043  GNUNET_break (0);
1044  break;
1045  }
1046 }
1047 
1048 
1056 static void
1057 handle_ping (void *cls, const struct PingMessage *m)
1058 {
1059  struct GSC_KeyExchangeInfo *kx = cls;
1060  struct PingMessage t;
1061  struct PongMessage tx;
1062  struct PongMessage *tp;
1063  struct GNUNET_MQ_Envelope *env;
1065 
1067  gettext_noop ("# PING messages received"),
1068  1,
1069  GNUNET_NO);
1071  (kx->status != GNUNET_CORE_KX_STATE_UP) &&
1073  {
1074  /* ignore */
1076  gettext_noop (
1077  "# PING messages dropped (out of order)"),
1078  1,
1079  GNUNET_NO);
1080  return;
1081  }
1083  "Core service receives PING request from `%s'.\n",
1084  GNUNET_i2s (kx->peer));
1085  derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1086  if (GNUNET_OK != do_decrypt (kx,
1087  &iv,
1088  &m->target,
1089  &t.target,
1090  sizeof(struct PingMessage)
1091  - ((void *) &m->target - (void *) m)))
1092  {
1093  GNUNET_break_op (0);
1094  return;
1095  }
1096  if (0 !=
1097  memcmp (&t.target, &GSC_my_identity, sizeof(struct GNUNET_PeerIdentity)))
1098  {
1101  "Decryption of PING from peer `%s' failed, PING for `%s'?\n",
1102  GNUNET_i2s (kx->peer),
1103  GNUNET_i2s2 (&t.target));
1104  else
1105  GNUNET_log (
1107  "Decryption of PING from peer `%s' failed after rekey (harmless)\n",
1108  GNUNET_i2s (kx->peer));
1109  GNUNET_break_op (0);
1110  return;
1111  }
1112  /* construct PONG */
1113  tx.reserved = 0;
1114  tx.challenge = t.challenge;
1115  tx.target = t.target;
1117  tp->iv_seed = calculate_seed (kx);
1118  derive_pong_iv (&iv, &kx->encrypt_key, tp->iv_seed, t.challenge, kx->peer);
1119  do_encrypt (kx,
1120  &iv,
1121  &tx.challenge,
1122  &tp->challenge,
1123  sizeof(struct PongMessage)
1124  - ((void *) &tp->challenge - (void *) tp));
1126  gettext_noop ("# PONG messages created"),
1127  1,
1128  GNUNET_NO);
1129  GNUNET_MQ_send (kx->mq, env);
1130 }
1131 
1132 
1139 static void
1140 send_keep_alive (void *cls)
1141 {
1142  struct GSC_KeyExchangeInfo *kx = cls;
1143  struct GNUNET_TIME_Relative retry;
1144  struct GNUNET_TIME_Relative left;
1145 
1146  kx->keep_alive_task = NULL;
1148  if (0 == left.rel_value_us)
1149  {
1151  gettext_noop ("# sessions terminated by timeout"),
1152  1,
1153  GNUNET_NO);
1154  GSC_SESSIONS_end (kx->peer);
1156  monitor_notify_all (kx);
1157  send_key (kx);
1158  return;
1159  }
1161  "Sending KEEPALIVE to `%s'\n",
1162  GNUNET_i2s (kx->peer));
1164  gettext_noop ("# keepalive messages sent"),
1165  1,
1166  GNUNET_NO);
1167  setup_fresh_ping (kx);
1168  send_ping (kx);
1171  kx->keep_alive_task =
1173 }
1174 
1175 
1183 static void
1185 {
1186  struct GNUNET_TIME_Relative delta;
1187 
1188  kx->timeout =
1190  delta =
1192  if (delta.rel_value_us > 5LL * 1000LL * 1000LL)
1193  {
1194  /* we only notify monitors about timeout changes if those
1195  are bigger than the threshold (5s) */
1196  monitor_notify_all (kx);
1197  }
1198  if (NULL != kx->keep_alive_task)
1202  &send_keep_alive,
1203  kx);
1204 }
1205 
1206 
1213 static void
1214 handle_pong (void *cls, const struct PongMessage *m)
1215 {
1216  struct GSC_KeyExchangeInfo *kx = cls;
1217  struct PongMessage t;
1219 
1221  gettext_noop ("# PONG messages received"),
1222  1,
1223  GNUNET_NO);
1224  switch (kx->status)
1225  {
1228  gettext_noop (
1229  "# PONG messages dropped (connection down)"),
1230  1,
1231  GNUNET_NO);
1232  return;
1233 
1236  gettext_noop (
1237  "# PONG messages dropped (out of order)"),
1238  1,
1239  GNUNET_NO);
1240  return;
1241 
1243  break;
1244 
1246  break;
1247 
1249  break;
1250 
1251  default:
1252  GNUNET_break (0);
1253  return;
1254  }
1256  "Core service receives PONG response from `%s'.\n",
1257  GNUNET_i2s (kx->peer));
1258  /* mark as garbage, just to be sure */
1259  memset (&t, 255, sizeof(t));
1260  derive_pong_iv (&iv,
1261  &kx->decrypt_key,
1262  m->iv_seed,
1263  kx->ping_challenge,
1264  &GSC_my_identity);
1265  if (GNUNET_OK != do_decrypt (kx,
1266  &iv,
1267  &m->challenge,
1268  &t.challenge,
1269  sizeof(struct PongMessage)
1270  - ((void *) &m->challenge - (void *) m)))
1271  {
1272  GNUNET_break_op (0);
1273  return;
1274  }
1276  gettext_noop ("# PONG messages decrypted"),
1277  1,
1278  GNUNET_NO);
1279  if ((0 !=
1280  memcmp (&t.target, kx->peer, sizeof(struct GNUNET_PeerIdentity))) ||
1281  (kx->ping_challenge != t.challenge))
1282  {
1283  /* PONG malformed */
1285  "Received malformed PONG wanted sender `%s' with challenge %u\n",
1286  GNUNET_i2s (kx->peer),
1287  (unsigned int) kx->ping_challenge);
1289  "Received malformed PONG received from `%s' with challenge %u\n",
1290  GNUNET_i2s (&t.target),
1291  (unsigned int) t.challenge);
1292  return;
1293  }
1295  "Received valid PONG from `%s'\n",
1296  GNUNET_i2s (kx->peer));
1297  /* no need to resend key any longer */
1298  if (NULL != kx->retry_set_key_task)
1299  {
1301  kx->retry_set_key_task = NULL;
1302  }
1303  switch (kx->status)
1304  {
1306  GNUNET_assert (0); /* should be impossible */
1307  return;
1308 
1310  GNUNET_assert (0); /* should be impossible */
1311  return;
1312 
1315  gettext_noop (
1316  "# session keys confirmed via PONG"),
1317  1,
1318  GNUNET_NO);
1320  monitor_notify_all (kx);
1321  GSC_SESSIONS_create (kx->peer, kx);
1322  GNUNET_assert (NULL == kx->keep_alive_task);
1323  update_timeout (kx);
1324  break;
1325 
1328  gettext_noop ("# timeouts prevented via PONG"),
1329  1,
1330  GNUNET_NO);
1331  update_timeout (kx);
1332  break;
1333 
1336  gettext_noop (
1337  "# rekey operations confirmed via PONG"),
1338  1,
1339  GNUNET_NO);
1341  monitor_notify_all (kx);
1342  update_timeout (kx);
1343  break;
1344 
1345  default:
1346  GNUNET_break (0);
1347  break;
1348  }
1349 }
1350 
1351 
1357 static void
1359 {
1360  struct GNUNET_MQ_Envelope *env;
1361 
1363  if (NULL != kx->retry_set_key_task)
1364  {
1366  kx->retry_set_key_task = NULL;
1367  }
1368  /* always update sender status in SET KEY message */
1369 #if DEBUG_KX
1370  {
1371  struct GNUNET_HashCode hc;
1372 
1374  sizeof(current_ekm.ephemeral_key),
1375  &hc);
1377  "Sending EPHEMERAL_KEY %s to `%s' (my status: %d)\n",
1378  GNUNET_h2s (&hc),
1379  GNUNET_i2s (kx->peer),
1380  kx->status);
1381  }
1382 #endif
1383  current_ekm.sender_status = htonl ((int32_t) (kx->status));
1385  GNUNET_MQ_send (kx->mq, env);
1387  send_ping (kx);
1388  kx->retry_set_key_task =
1391  kx);
1392 }
1393 
1394 
1402 void
1404  const void *payload,
1405  size_t payload_size)
1406 {
1407  size_t used = payload_size + sizeof(struct EncryptedMessage);
1408  char pbuf[used]; /* plaintext */
1409  struct EncryptedMessage *em; /* encrypted message */
1410  struct EncryptedMessage *ph; /* plaintext header */
1411  struct GNUNET_MQ_Envelope *env;
1413  struct GNUNET_CRYPTO_AuthKey auth_key;
1414 
1415  ph = (struct EncryptedMessage *) pbuf;
1416  ph->sequence_number = htonl (++kx->last_sequence_number_sent);
1417  ph->iv_seed = calculate_seed (kx);
1418  ph->reserved = 0;
1420  GNUNET_memcpy (&ph[1], payload, payload_size);
1421  env = GNUNET_MQ_msg_extra (em,
1422  payload_size,
1424  em->iv_seed = ph->iv_seed;
1425  derive_iv (&iv, &kx->encrypt_key, ph->iv_seed, kx->peer);
1427  &iv,
1428  &ph->sequence_number,
1429  &em->sequence_number,
1430  used - ENCRYPTED_HEADER_SIZE));
1431 #if DEBUG_KX
1432  {
1433  struct GNUNET_HashCode hc;
1434 
1435  GNUNET_CRYPTO_hash (&ph->sequence_number,
1436  used - ENCRYPTED_HEADER_SIZE,
1437  &hc);
1439  "Encrypted payload `%s' of %u bytes for %s\n",
1440  GNUNET_h2s (&hc),
1441  (unsigned int) (used - ENCRYPTED_HEADER_SIZE),
1442  GNUNET_i2s (kx->peer));
1443  }
1444 #endif
1445  derive_auth_key (&auth_key, &kx->encrypt_key, ph->iv_seed);
1446  GNUNET_CRYPTO_hmac (&auth_key,
1447  &em->sequence_number,
1448  used - ENCRYPTED_HEADER_SIZE,
1449  &em->hmac);
1450 #if DEBUG_KX
1451  {
1452  struct GNUNET_HashCode hc;
1453 
1454  GNUNET_CRYPTO_hash (&auth_key, sizeof(auth_key), &hc);
1456  "For peer %s, used AC %s to create hmac %s\n",
1457  GNUNET_i2s (kx->peer),
1458  GNUNET_h2s (&hc),
1459  GNUNET_h2s2 (&em->hmac));
1460  }
1461 #endif
1463  GNUNET_MQ_send (kx->mq, env);
1464 }
1465 
1466 
1475 static int
1476 check_encrypted (void *cls, const struct EncryptedMessage *m)
1477 {
1478  uint16_t size = ntohs (m->header.size) - sizeof(*m);
1479 
1480  if (size < sizeof(struct GNUNET_MessageHeader))
1481  {
1482  GNUNET_break_op (0);
1483  return GNUNET_SYSERR;
1484  }
1485  return GNUNET_OK;
1486 }
1487 
1488 
1496 static void
1497 handle_encrypted (void *cls, const struct EncryptedMessage *m)
1498 {
1499  struct GSC_KeyExchangeInfo *kx = cls;
1500  struct EncryptedMessage *pt; /* plaintext */
1501  struct GNUNET_HashCode ph;
1502  uint32_t snum;
1503  struct GNUNET_TIME_Absolute t;
1505  struct GNUNET_CRYPTO_AuthKey auth_key;
1506  uint16_t size = ntohs (m->header.size);
1507  char buf[size] GNUNET_ALIGN;
1508 
1509  if (GNUNET_CORE_KX_STATE_UP != kx->status)
1510  {
1512  gettext_noop (
1513  "# DATA message dropped (out of order)"),
1514  1,
1515  GNUNET_NO);
1516  return;
1517  }
1518  if (0 ==
1520  {
1521  GNUNET_log (
1523  _ (
1524  "Session to peer `%s' went down due to key expiration (should not happen)\n"),
1525  GNUNET_i2s (kx->peer));
1527  gettext_noop (
1528  "# sessions terminated by key expiration"),
1529  1,
1530  GNUNET_NO);
1531  GSC_SESSIONS_end (kx->peer);
1532  if (NULL != kx->keep_alive_task)
1533  {
1535  kx->keep_alive_task = NULL;
1536  }
1538  monitor_notify_all (kx);
1539  send_key (kx);
1540  return;
1541  }
1542 
1543  /* validate hash */
1544 #if DEBUG_KX
1545  {
1546  struct GNUNET_HashCode hc;
1547 
1550  "Received encrypted payload `%s' of %u bytes from %s\n",
1551  GNUNET_h2s (&hc),
1552  (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
1553  GNUNET_i2s (kx->peer));
1554  }
1555 #endif
1556  derive_auth_key (&auth_key, &kx->decrypt_key, m->iv_seed);
1557  GNUNET_CRYPTO_hmac (&auth_key,
1558  &m->sequence_number,
1559  size - ENCRYPTED_HEADER_SIZE,
1560  &ph);
1561 #if DEBUG_KX
1562  {
1563  struct GNUNET_HashCode hc;
1564 
1565  GNUNET_CRYPTO_hash (&auth_key, sizeof(auth_key), &hc);
1567  "For peer %s, used AC %s to verify hmac %s\n",
1568  GNUNET_i2s (kx->peer),
1569  GNUNET_h2s (&hc),
1570  GNUNET_h2s2 (&m->hmac));
1571  }
1572 #endif
1573  if (0 != memcmp (&ph, &m->hmac, sizeof(struct GNUNET_HashCode)))
1574  {
1575  /* checksum failed */
1577  "Failed checksum validation for a message from `%s'\n",
1578  GNUNET_i2s (kx->peer));
1579  return;
1580  }
1581  derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1582  /* decrypt */
1583  if (GNUNET_OK != do_decrypt (kx,
1584  &iv,
1585  &m->sequence_number,
1587  size - ENCRYPTED_HEADER_SIZE))
1588  {
1589  GNUNET_break_op (0);
1590  return;
1591  }
1593  "Decrypted %u bytes from %s\n",
1594  (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
1595  GNUNET_i2s (kx->peer));
1596  pt = (struct EncryptedMessage *) buf;
1597 
1598  /* validate sequence number */
1599  snum = ntohl (pt->sequence_number);
1600  if (kx->last_sequence_number_received == snum)
1601  {
1603  "Received duplicate message, ignoring.\n");
1604  /* duplicate, ignore */
1606  gettext_noop ("# bytes dropped (duplicates)"),
1607  size,
1608  GNUNET_NO);
1609  return;
1610  }
1611  if ((kx->last_sequence_number_received > snum) &&
1612  (kx->last_sequence_number_received - snum > 32))
1613  {
1615  "Received ancient out of sequence message, ignoring.\n");
1616  /* ancient out of sequence, ignore */
1618  gettext_noop (
1619  "# bytes dropped (out of sequence)"),
1620  size,
1621  GNUNET_NO);
1622  return;
1623  }
1624  if (kx->last_sequence_number_received > snum)
1625  {
1626  uint32_t rotbit = 1U << (kx->last_sequence_number_received - snum - 1);
1627 
1628  if ((kx->last_packets_bitmap & rotbit) != 0)
1629  {
1631  "Received duplicate message, ignoring.\n");
1633  gettext_noop ("# bytes dropped (duplicates)"),
1634  size,
1635  GNUNET_NO);
1636  /* duplicate, ignore */
1637  return;
1638  }
1639  kx->last_packets_bitmap |= rotbit;
1640  }
1641  if (kx->last_sequence_number_received < snum)
1642  {
1643  unsigned int shift = (snum - kx->last_sequence_number_received);
1644 
1645  if (shift >= 8 * sizeof(kx->last_packets_bitmap))
1646  kx->last_packets_bitmap = 0;
1647  else
1648  kx->last_packets_bitmap <<= shift;
1649  kx->last_sequence_number_received = snum;
1650  }
1651 
1652  /* check timestamp */
1654  if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
1655  MAX_MESSAGE_AGE.rel_value_us)
1656  {
1658  "Message received far too old (%s). Content ignored.\n",
1661  GNUNET_YES));
1663  gettext_noop (
1664  "# bytes dropped (ancient message)"),
1665  size,
1666  GNUNET_NO);
1667  return;
1668  }
1669 
1670  /* process decrypted message(s) */
1671  update_timeout (kx);
1673  gettext_noop ("# bytes of payload decrypted"),
1674  size - sizeof(struct EncryptedMessage),
1675  GNUNET_NO);
1676  if (GNUNET_OK !=
1678  &buf[sizeof(struct EncryptedMessage)],
1679  size - sizeof(struct EncryptedMessage),
1680  GNUNET_YES,
1681  GNUNET_NO))
1682  GNUNET_break_op (0);
1683 }
1684 
1685 
1693 static void
1695  const struct GNUNET_PeerIdentity *pid,
1696  void *connect_cls)
1697 {
1698  struct GSC_KeyExchangeInfo *kx = connect_cls;
1699 
1701  "Peer %s has excess bandwidth available\n",
1702  GNUNET_i2s (pid));
1704  GSC_SESSIONS_solicit (pid);
1705 }
1706 
1707 
1712 static void
1714 {
1715  current_ekm.header.size = htons (sizeof(struct EphemeralKeyMessage));
1717  current_ekm.sender_status = 0; /* to be set later */
1720  htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
1721  + sizeof(struct GNUNET_TIME_AbsoluteNBO)
1722  + sizeof(struct GNUNET_TIME_AbsoluteNBO)
1723  + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)
1724  + sizeof(struct GNUNET_PeerIdentity));
1728  "core",
1729  "USE_EPHEMERAL_KEYS"))
1730  {
1734  REKEY_TOLERANCE)));
1735  }
1736  else
1737  {
1740  }
1748 }
1749 
1750 
1756 static void
1757 do_rekey (void *cls)
1758 {
1759  struct GSC_KeyExchangeInfo *pos;
1760 
1761  rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY, &do_rekey, NULL);
1763  sign_ephemeral_key ();
1764  {
1765  struct GNUNET_HashCode eh;
1766 
1768  sizeof(current_ekm.ephemeral_key),
1769  &eh);
1770  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Rekeying to %s\n", GNUNET_h2s (&eh));
1771  }
1772  for (pos = kx_head; NULL != pos; pos = pos->next)
1773  {
1774  if (GNUNET_CORE_KX_STATE_UP == pos->status)
1775  {
1777  monitor_notify_all (pos);
1778  derive_session_keys (pos);
1779  }
1780  if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
1781  {
1783  monitor_notify_all (pos);
1784  }
1785  monitor_notify_all (pos);
1786  send_key (pos);
1787  }
1788 }
1789 
1790 
1797 int
1799 {
1800  struct GNUNET_MQ_MessageHandler handlers[] =
1801  { GNUNET_MQ_hd_fixed_size (ephemeral_key,
1803  struct EphemeralKeyMessage,
1804  NULL),
1807  struct PingMessage,
1808  NULL),
1811  struct PongMessage,
1812  NULL),
1813  GNUNET_MQ_hd_var_size (encrypted,
1815  struct EncryptedMessage,
1816  NULL),
1817  GNUNET_MQ_handler_end () };
1818 
1819  my_private_key = *pk;
1823  sign_ephemeral_key ();
1824  {
1825  struct GNUNET_HashCode eh;
1826 
1828  sizeof(current_ekm.ephemeral_key),
1829  &eh);
1831  "Starting with ephemeral key %s\n",
1832  GNUNET_h2s (&eh));
1833  }
1834 
1836  rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY, &do_rekey, NULL);
1837  transport =
1839  &GSC_my_identity,
1840  handlers,
1841  NULL,
1845  if (NULL == transport)
1846  {
1847  GSC_KX_done ();
1848  return GNUNET_SYSERR;
1849  }
1850  return GNUNET_OK;
1851 }
1852 
1853 
1857 void
1859 {
1860  if (NULL != transport)
1861  {
1863  transport = NULL;
1864  }
1865  if (NULL != rekey_task)
1866  {
1867  GNUNET_SCHEDULER_cancel (rekey_task);
1868  rekey_task = NULL;
1869  }
1870  memset (&my_ephemeral_key,
1871  0,
1872  sizeof (my_ephemeral_key));
1873  memset (&my_private_key,
1874  0,
1875  sizeof (my_private_key));
1876  if (NULL != nc)
1877  {
1879  nc = NULL;
1880  }
1881 }
1882 
1883 
1890 unsigned int
1892 {
1893  return GNUNET_MQ_get_length (kxinfo->mq);
1894 }
1895 
1896 
1903 int
1905 {
1906  return kxinfo->has_excess_bandwidth;
1907 }
1908 
1909 
1918 void
1920 {
1921  struct GNUNET_MQ_Envelope *env;
1922  struct MonitorNotifyMessage *done_msg;
1923  struct GSC_KeyExchangeInfo *kx;
1924 
1926  for (kx = kx_head; NULL != kx; kx = kx->next)
1927  {
1928  struct GNUNET_MQ_Envelope *env;
1929  struct MonitorNotifyMessage *msg;
1930 
1932  msg->state = htonl ((uint32_t) kx->status);
1933  msg->peer = *kx->peer;
1935  GNUNET_MQ_send (mq, env);
1936  }
1938  done_msg->state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
1940  GNUNET_MQ_send (mq, env);
1941 }
1942 
1943 
1944 /* end of gnunet-service-core_kx.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY
Purpose is to set a session key.
#define REKEY_TOLERANCE
What time difference do we tolerate?
void GSC_SESSIONS_confirm_typemap(const struct GNUNET_PeerIdentity *peer, const struct GNUNET_MessageHeader *msg)
The other peer has confirmed receiving our type map, check if it is current and if so...
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
Create a new envelope by copying an existing message.
Definition: mq.c:666
static struct GNUNET_CRYPTO_EcdhePrivateKey my_ephemeral_key
Our ephemeral private key.
The notification context is the key datastructure for a convenience API used for transmission of noti...
Definition: nc.c:75
const char * GNUNET_i2s2(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
static void ping(void *cls)
Send a ping to destination.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Subtract a given relative duration from the given start time.
Definition: time.c:422
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:673
static void derive_aes_key(const struct GNUNET_PeerIdentity *sender, const struct GNUNET_PeerIdentity *receiver, const struct GNUNET_HashCode *key_material, struct GNUNET_CRYPTO_SymmetricSessionKey *skey)
Derive an AES key from key material.
const struct GNUNET_PeerIdentity * peer
Identity of the peer.
void GSC_KX_handle_client_monitor_peers(struct GNUNET_MQ_Handle *mq)
Handle GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request.
uint64_t rel_value_us
The actual value.
struct GNUNET_TIME_Relative set_key_retry_frequency
At what frequency are we currently re-trying SET_KEY messages?
uint32_t iv_seed
Seed for the IV.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
enum GNUNET_CORE_KxState status
What is our connection status?
uint32_t last_packets_bitmap
Bit map indicating which of the 32 sequence numbers before the last were received (good for accepting...
struct GNUNET_PeerIdentity target
Intended target of the PING, used primarily to check that decryption actually worked.
int32_t GNUNET_CRYPTO_crc32_n(const void *buf, size_t len)
Compute the CRC32 checksum for the first len bytes of the buffer.
Definition: crypto_crc.c:106
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
struct PingMessage ping
PING message we transmit to the other peer.
uint32_t sequence_number
Sequence number, in network byte order.
static void do_rekey(void *cls)
Task run to trigger rekeying.
struct GNUNET_CRYPTO_EcdsaPrivateKey pk
Private key from command line option, or NULL.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the maximum of two relative time values.
Definition: time.c:287
static void handle_ephemeral_key(void *cls, const struct EphemeralKeyMessage *m)
We received a GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY message.
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:246
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Response to a PING.
static struct GNUNET_ATS_PerformanceHandle * ph
ATS performance handle used.
Definition: gnunet-ats.c:116
static void send_key(struct GSC_KeyExchangeInfo *kx)
Send our key (and encrypted PING) to the other peer.
#define GNUNET_TIME_UNIT_SECONDS
One second.
int GNUNET_CRYPTO_eddsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
Definition: crypto_ecc.c:819
uint32_t last_sequence_number_sent
last sequence number transmitted
struct GNUNET_TIME_AbsoluteNBO timeout
How long will we stay in this state (if nothing else happens)?
Definition: core.h:322
static int do_encrypt(struct GSC_KeyExchangeInfo *kx, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const void *in, void *out, size_t size)
Encrypt size bytes from in and write the result to out.
static struct GNUNET_TRANSPORT_CoreHandle * transport
Transport service.
static void derive_session_keys(struct GSC_KeyExchangeInfo *kx)
Derive fresh session keys from the current ephemeral keys.
unsigned int GSC_NEIGHBOURS_get_queue_length(const struct GSC_KeyExchangeInfo *kxinfo)
Check how many messages are queued for the given neighbour.
common internal definitions for core service
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
int GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a public and a private ECC key.
Definition: crypto_ecc.c:850
static void handle_transport_notify_excess_bw(void *cls, const struct GNUNET_PeerIdentity *pid, void *connect_cls)
One of our neighbours has excess bandwidth, remember this.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
static void sign_ephemeral_key()
Setup the message that links the ephemeral key to our persistent public key and generate the appropri...
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:395
GNUNET_CORE_KxState
State machine for our P2P encryption handshake.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void handle_encrypted(void *cls, const struct EncryptedMessage *m)
We received an encrypted message.
static struct GSC_KeyExchangeInfo * kx_head
DLL head.
struct GNUNET_TRANSPORT_CoreHandle * GNUNET_TRANSPORT_core_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_PeerIdentity *self, const struct GNUNET_MQ_MessageHandler *handlers, void *cls, GNUNET_TRANSPORT_NotifyConnect nc, GNUNET_TRANSPORT_NotifyDisconnect nd)
Connect to the transport service.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static void handle_ping(void *cls, const struct PingMessage *m)
We received a PING message.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_HashCode hmac
MAC of the encrypted message (starting at sequence_number), used to verify message integrity...
struct GNUNET_STATISTICS_Handle * GSC_stats
For creating statistics.
We&#39;ve received the other peers session key.
struct GNUNET_MessageStreamTokenizer * mst
Our message stream tokenizer (for encrypted payload).
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
static void derive_auth_key(struct GNUNET_CRYPTO_AuthKey *akey, const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed)
Derive an authentication key from "set key" information.
void GNUNET_CRYPTO_hmac(const struct GNUNET_CRYPTO_AuthKey *key, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104)
Definition: crypto_hash.c:455
void GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, struct GNUNET_MQ_Handle *mq)
Add a subscriber to the notification context.
Definition: nc.c:160
uint64_t abs_value_us
The actual value.
struct GNUNET_TIME_Absolute last_notify_timeout
What was the last timeout we informed our monitors about?
#define GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP
gzip-compressed type map of the sender
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_MESSAGE_TYPE_CORE_PONG
Confirmation that other peer is alive.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
struct GNUNET_TIME_AbsoluteNBO expiration_time
When does the given ephemeral key expire (end of validity).
struct GNUNET_MessageHeader header
Header with type GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY.
Definition: core.h:307
struct GNUNET_MessageHeader header
Message type is GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
static int deliver_message(void *cls, const struct GNUNET_MessageHeader *m)
Deliver P2P message to interested clients.
struct GNUNET_SCHEDULER_Task * retry_set_key_task
ID of task used for re-trying SET_KEY and PING message.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
struct GNUNET_CRYPTO_EddsaSignature signature
An ECC signature of the origin_identity asserting the validity of the given ephemeral key...
struct GNUNET_TIME_AbsoluteNBO creation_time
At what time was this key created (beginning of validity).
struct GNUNET_MQ_Handle * mq
Message queue for sending messages to peer.
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:508
const char * GNUNET_h2s2(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
static void monitor_notify_all(struct GSC_KeyExchangeInfo *kx)
Inform all monitors about the KX state of the given peer.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1269
static int do_decrypt(struct GSC_KeyExchangeInfo *kx, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const void *in, void *out, size_t size)
Decrypt size bytes from in and write the result to out.
uint32_t challenge
Random number to make replay attacks harder.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
static struct GNUNET_CRYPTO_EddsaPrivateKey my_private_key
Our private key.
void GSC_KX_encrypt_and_transmit(struct GSC_KeyExchangeInfo *kx, const void *payload, size_t payload_size)
Encrypt and transmit a message with the given payload.
static int check_encrypted(void *cls, const struct EncryptedMessage *m)
We received an encrypted message.
static void send_ping(struct GSC_KeyExchangeInfo *kx)
Send our PING to the other peer.
We&#39;re sending an (encrypted) PING to the other peer to check if it can decrypt.
static void send_keep_alive(void *cls)
Task triggered when a neighbour entry is about to time out (and we should prevent this by sending a P...
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:411
uint32_t challenge
Random number chosen to make replay harder.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
#define GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP
Other peer confirms having received the type map.
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:337
Encapsulation for encrypted messages exchanged between peers.
struct GSC_KeyExchangeInfo * next
DLL.
static struct GSC_KeyExchangeInfo * kx_tail
DLL tail.
struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key
Ephemeral public ECC key of the other peer.
uint32_t iv_seed
Seed for the IV.
void GSC_SESSIONS_set_typemap(const struct GNUNET_PeerIdentity *peer, const struct GNUNET_MessageHeader *msg)
We have received a typemap message from a peer, update ours.
Handle to a message stream tokenizer.
Definition: mst.c:43
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:702
void GNUNET_CRYPTO_symmetric_derive_iv(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, const void *salt, size_t salt_len,...)
Derive an IV.
#define GNUNET_CORE_OPTION_SEND_FULL_INBOUND
Client wants all inbound messages in full.
Definition: core.h:55
Randomness for IVs etc.
uint16_t status
See PRISM_STATUS_*-constants.
static char buf[2048]
#define MIN_PING_FREQUENCY
What is the minimum frequency for a PING message?
uint32_t ping_challenge
What was our PING challenge number (for this peer)?
struct GNUNET_MessageHeader header
Message type is GNUNET_MESSAGE_TYPE_CORE_PING.
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
int GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, const char *buf, size_t size, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:114
int GSC_KX_init(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Initialize KX subsystem.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
Information about the status of a key exchange with another peer.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Information about what is being signed.
void GSC_CLIENTS_deliver_message(const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg, uint16_t msize, uint32_t options)
Deliver P2P message to interested clients.
A 512-bit hashcode.
Globals for gnunet-service-core.
Message handler for a specific message type.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
struct GNUNET_CRYPTO_SymmetricSessionKey decrypt_key
Key we use to decrypt messages from the other peer (given to us by the other peer during the handshak...
#define GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY
Session key exchange between peers.
struct GNUNET_PeerIdentity GSC_my_identity
Our identity.
Private ECC key encoded for transmission.
static void pong(struct GNUNET_CADET_Channel *channel, const struct CadetPingMessage *ping)
Reply with a pong to origin.
static struct GNUNET_IDENTITY_Handle * identity
Which namespace do we publish to? NULL if we do not publish to a namespace.
struct GNUNET_SCHEDULER_Task * keep_alive_task
ID of task used for sending keep-alive pings.
static void update_timeout(struct GSC_KeyExchangeInfo *kx)
We&#39;ve seen a valid message from the other peer.
static void setup_fresh_ping(struct GSC_KeyExchangeInfo *kx)
Create a fresh PING message for transmission to the other peer.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
void GSC_SESSIONS_solicit(const struct GNUNET_PeerIdentity *pid)
Traffic is being solicited for the given peer.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:215
This is not a state in a peer&#39;s state machine, but a special value used with the GNUNET_CORE_MonitorC...
void GSC_SESSIONS_create(const struct GNUNET_PeerIdentity *peer, struct GSC_KeyExchangeInfo *kx)
Create a session, a key exchange was just completed.
uint32_t iv_seed
Random value used for IV generation.
#define MAX_MESSAGE_AGE
What is the maximum age of a message for us to consider processing it? Note that this looks at the ti...
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key
Ephemeral public ECC key.
static void set_key_retry_task(void *cls)
Task that will retry send_key() if our previous attempt failed.
static uint32_t calculate_seed(struct GSC_KeyExchangeInfo *kx)
Calculate seed value we should use for a message.
#define GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP
uncompressed type map of the sender
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static void * handle_transport_notify_connect(void *cls, const struct GNUNET_PeerIdentity *pid, struct GNUNET_MQ_Handle *mq)
Function called by transport to notify us that a peer connected to us (on the network level)...
#define GNUNET_MESSAGE_TYPE_CORE_PING
Check that other peer is alive (challenge).
The other peer has confirmed our session key + PING with a PONG message encrypted with their session ...
static struct GNUNET_NotificationContext * nc
Notification context for broadcasting to monitors.
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
struct GSC_KeyExchangeInfo * prev
DLL.
static unsigned long long payload
How much data are we currently storing in the database?
code for managing the key exchange (SET_KEY, PING, PONG) with other peers
struct GNUNET_PeerIdentity target
Intended target of the PING, used primarily to check that decryption actually worked.
#define ENCRYPTED_HEADER_SIZE
Number of bytes (at the beginning) of struct EncryptedMessage that are NOT encrypted.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:193
struct GNUNET_NotificationContext * GNUNET_notification_context_create(unsigned int queue_length)
Create a new notification context.
Definition: nc.c:121
static void derive_pong_iv(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed, uint32_t challenge, const struct GNUNET_PeerIdentity *identity)
Derive an IV from pong packet information.
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don&#39;t receive messages from the peer...
Handle to a message queue.
Definition: mq.c:85
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32;.
Private ECC key encoded for transmission.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Add relative times together.
Definition: time.c:579
#define GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE
Encapsulation for an encrypted message between peers.
void GNUNET_notification_context_broadcast(struct GNUNET_NotificationContext *nc, const struct GNUNET_MessageHeader *msg, int can_drop)
Send a message to all subscribers of this context.
Definition: nc.c:189
The identity of the host (wraps the signing key of the peer).
static void derive_iv(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed, const struct GNUNET_PeerIdentity *identity)
Derive an IV from packet information.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to &#39;struct&#39;s...
uint32_t last_sequence_number_received
last sequence number received on this connection (highest)
static struct GNUNET_SCHEDULER_Task * rekey_task
Task scheduled for periodic re-generation (and thus rekeying) of our ephemeral key.
#define GNUNET_PACKED
gcc-ism to get packed structs.
int has_excess_bandwidth
GNUNET_YES if this peer currently has excess bandwidth.
#define REKEY_FREQUENCY
How often do we rekey?
uint32_t reserved
Reserved, always zero.
struct GNUNET_MessageHeader header
Message type is GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:375
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:311
struct GNUNET_TIME_Absolute foreign_key_expires
At what time did the other peer generate the decryption key?
void GNUNET_TRANSPORT_core_disconnect(struct GNUNET_TRANSPORT_CoreHandle *handle)
Disconnect from the transport service.
uint32_t state
New peer state, an enum GNUNET_CORE_KxState in NBO.
Definition: core.h:312
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
int GNUNET_CRYPTO_eddsa_sign_(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
EdDSA sign a given block.
Definition: crypto_ecc.c:707
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
struct GNUNET_CRYPTO_SymmetricSessionKey encrypt_key
Key we use to encrypt our messages for the other peer (initialized by us when we do the handshake)...
int32_t sender_status
Status of the sender (should be in enum PeerStateMachine), nbo.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:331
struct GNUNET_TIME_Absolute timeout
When should the session time out (if there are no PONGs)?
Last state of a KX (when it is being terminated).
Header for all communications.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Divide relative time by a given factor.
Definition: time.c:527
Time for absolute times used by GNUnet, in microseconds.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute end)
Compute the time difference between the given start and end times.
Definition: time.c:354
void GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc)
Destroy the context, force disconnect for all subscribers.
Definition: nc.c:137
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:355
static struct EphemeralKeyMessage current_ekm
Current message we send for a key exchange.
Message sent by the service to monitor clients to notify them about a peer changing status...
Definition: core.h:302
We&#39;ve sent our session key.
static void handle_transport_notify_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer, void *handler_cls)
Function called by transport telling us that a peer disconnected.
int GSC_NEIGHBOURS_check_excess_bandwidth(const struct GSC_KeyExchangeInfo *kxinfo)
Check if the given neighbour has excess bandwidth available.
Message transmitted with the signed ephemeral key of a peer.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
ssize_t GNUNET_CRYPTO_symmetric_decrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Decrypt a given block using a symmetric sessionkey.
struct GNUNET_PeerIdentity origin_identity
Public key of the signing peer (persistent version, not the ephemeral public key).
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
Handle for the transport service (includes all of the state for the transport service).
void GNUNET_CRYPTO_hmac_derive_key(struct GNUNET_CRYPTO_AuthKey *key, const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, const void *salt, size_t salt_len,...)
Derive an authentication key.
Definition: crypto_hash.c:373
#define GNUNET_CORE_OPTION_SEND_HDR_INBOUND
Client just wants the 4-byte message headers of all inbound messages.
Definition: core.h:61
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:657
void GSC_SESSIONS_end(const struct GNUNET_PeerIdentity *pid)
End the session with the given peer (we are no longer connected).
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:90
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define INITIAL_SET_KEY_RETRY_FREQUENCY
How long do we wait for SET_KEY confirmation initially?
void GSC_KX_done()
Shutdown KX subsystem.
struct GNUNET_TIME_AbsoluteNBO timestamp
Timestamp.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
uint32_t reserved
Reserved, always zero.
const struct GNUNET_CONFIGURATION_Handle * GSC_cfg
Our configuration.
type for (message) authentication keys
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
void GSC_SESSIONS_reinit(const struct GNUNET_PeerIdentity *peer)
The other peer has indicated that it &#39;lost&#39; the session (KX down), reinitialize the session on our en...
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY
Reply for monitor by CORE service.
Time for relative time used by GNUnet, in microseconds.
static void handle_pong(void *cls, const struct PongMessage *m)
We received a PONG message.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
static struct GNUNET_TRANSPORT_PluginMonitor * pm
Handle if we are monitoring plugin session activity.
#define gettext_noop(String)
Definition: gettext.h:69
struct GNUNET_PeerIdentity peer
Identity of the peer.
Definition: core.h:317
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
We&#39;re rekeying (or had a timeout), so we have sent the other peer our new ephemeral key...
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...