GNUnet 0.21.0
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"
31#include "gnunet-service-core.h"
32#include "gnunet_constants.h"
33#include "gnunet_signatures.h"
34#include "gnunet_protocols.h"
35
39#define DEBUG_KX 0
40
44#define INITIAL_SET_KEY_RETRY_FREQUENCY \
45 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
46
50#define MIN_PING_FREQUENCY \
51 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
52
56#define REKEY_FREQUENCY \
57 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
58
62#define REKEY_TOLERANCE \
63 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
64
72#define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
73
74
76
82{
87
92
101
107
112
118};
120
121
126#define ENCRYPTED_HEADER_SIZE \
127 (offsetof (struct EncryptedMessage, sequence_number))
128
129
134{
139
144
149
154
159
164
169
175
181
186
191
196
201
206
211
218
223
228
233
238
243};
244
245
250
255
260
265
270
275
281
286
287
293static uint32_t
295{
296 /* Note: may want to make this non-random and instead
297 derive from key material to avoid having an undetectable
298 side-channel */
299 return htonl (
301}
302
303
309static void
311{
313
315 msg.header.size = htons (sizeof(msg));
316 msg.state = htonl ((uint32_t) kx->status);
317 msg.peer = *kx->peer;
318 msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
321}
322
323
331static void
333 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
334 uint32_t seed)
335{
336 static const char ctx[] = "authentication key";
337
338#if DEBUG_KX
339 struct GNUNET_HashCode sh;
340
341 GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
343 "Deriving Auth key from SKEY %s and seed %u\n",
344 GNUNET_h2s (&sh),
345 (unsigned int) seed);
346#endif
348 skey,
349 &seed,
350 sizeof(seed),
351 skey,
352 sizeof(
354 ctx,
355 sizeof(ctx),
356 NULL);
357}
358
359
368static void
370 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
371 uint32_t seed,
372 const struct GNUNET_PeerIdentity *identity)
373{
374 static const char ctx[] = "initialization vector";
375
376#if DEBUG_KX
377 struct GNUNET_HashCode sh;
378
379 GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
381 "Deriving IV from SKEY %s and seed %u for peer %s\n",
382 GNUNET_h2s (&sh),
383 (unsigned int) seed,
385#endif
387 skey,
388 &seed,
389 sizeof(seed),
390 identity,
391 sizeof(struct GNUNET_PeerIdentity),
392 ctx,
393 sizeof(ctx),
394 NULL);
395}
396
397
407static void
409 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
410 uint32_t seed,
411 uint32_t challenge,
412 const struct GNUNET_PeerIdentity *identity)
413{
414 static const char ctx[] = "pong initialization vector";
415
416#if DEBUG_KX
417 struct GNUNET_HashCode sh;
418
419 GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
421 "Deriving PONG IV from SKEY %s and seed %u/%u for %s\n",
422 GNUNET_h2s (&sh),
423 (unsigned int) seed,
424 (unsigned int) challenge,
426#endif
428 skey,
429 &seed,
430 sizeof(seed),
431 identity,
432 sizeof(struct GNUNET_PeerIdentity),
433 &challenge,
434 sizeof(challenge),
435 ctx,
436 sizeof(ctx),
437 NULL);
438}
439
440
449static void
451 const struct GNUNET_PeerIdentity *receiver,
452 const struct GNUNET_HashCode *key_material,
454{
455 static const char ctx[] = "aes key generation vector";
456
457#if DEBUG_KX
458 struct GNUNET_HashCode sh;
459
460 GNUNET_CRYPTO_hash (skey, sizeof(*skey), &sh);
462 "Deriving AES Keys for %s to %s from %s\n",
463 GNUNET_i2s (sender),
465 GNUNET_h2s (key_material));
466#endif
467 GNUNET_CRYPTO_kdf (skey,
469 ctx,
470 sizeof(ctx),
471 key_material,
472 sizeof(struct GNUNET_HashCode),
473 sender,
474 sizeof(struct GNUNET_PeerIdentity),
475 receiver,
476 sizeof(struct GNUNET_PeerIdentity),
477 NULL);
478}
479
480
493static int
496 const void *in,
497 void *out,
498 size_t size)
499{
500 if (size != (uint16_t) size)
501 {
502 GNUNET_break (0);
503 return GNUNET_NO;
504 }
506 (uint16_t) size,
507 &kx->encrypt_key,
508 iv,
509 out));
511 gettext_noop ("# bytes encrypted"),
512 size,
513 GNUNET_NO);
514 /* the following is too sensitive to write to log files by accident,
515 so we require manual intervention to get this one... */
516#if DEBUG_KX
518 "Encrypted %u bytes for `%s' using key %s, IV %u\n",
519 (unsigned int) size,
520 GNUNET_i2s (kx->peer),
522 GNUNET_CRYPTO_crc32_n (iv, sizeof(iv)));
523#endif
524 return GNUNET_OK;
525}
526
527
540static int
543 const void *in,
544 void *out,
545 size_t size)
546{
547 if (size != (uint16_t) size)
548 {
549 GNUNET_break (0);
550 return GNUNET_NO;
551 }
555 {
556 GNUNET_break_op (0);
557 return GNUNET_SYSERR;
558 }
560 (uint16_t) size,
561 &kx->decrypt_key,
562 iv,
563 out))
564 {
565 GNUNET_break (0);
566 return GNUNET_SYSERR;
567 }
569 gettext_noop ("# bytes decrypted"),
570 size,
571 GNUNET_NO);
572 /* the following is too sensitive to write to log files by accident,
573 so we require manual intervention to get this one... */
574#if DEBUG_KX
576 "Decrypted %u bytes from `%s' using key %s, IV %u\n",
577 (unsigned int) size,
578 GNUNET_i2s (kx->peer),
580 GNUNET_CRYPTO_crc32_n (iv, sizeof(*iv)));
581#endif
582 return GNUNET_OK;
583}
584
585
591static void
592send_key (struct GSC_KeyExchangeInfo *kx);
593
594
600static void
602{
603 struct GSC_KeyExchangeInfo *kx = cls;
604
605 kx->retry_set_key_task = NULL;
609 send_key (kx);
610}
611
612
618static void
620{
621 struct PingMessage pp;
622 struct PingMessage *pm;
624
625 pm = &kx->ping;
626 kx->ping_challenge =
628 pm->header.size = htons (sizeof(struct PingMessage));
629 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
630 pm->iv_seed = calculate_seed (kx);
631 derive_iv (&iv, &kx->encrypt_key, pm->iv_seed, kx->peer);
632 pp.challenge = kx->ping_challenge;
633 pp.target = *kx->peer;
634 do_encrypt (kx,
635 &iv,
636 &pp.target,
637 &pm->target,
638 sizeof(struct PingMessage)
639 - ((void *) &pm->target - (void *) pm));
640}
641
642
654static int
655deliver_message (void *cls, const struct GNUNET_MessageHeader *m)
656{
657 struct GSC_KeyExchangeInfo *kx = cls;
658
660 "Decrypted message of type %d from %s\n",
661 ntohs (m->type),
662 GNUNET_i2s (kx->peer));
664 {
666 gettext_noop ("# PAYLOAD dropped (out of order)"),
667 1,
668 GNUNET_NO);
669 return GNUNET_OK;
670 }
671 switch (ntohs (m->type))
672 {
676 return GNUNET_OK;
677
680 return GNUNET_OK;
681
682 default:
684 m,
685 ntohs (m->size),
688 m,
689 sizeof(struct GNUNET_MessageHeader),
691 }
692 return GNUNET_OK;
693}
694
695
705static void *
707 const struct GNUNET_PeerIdentity *pid,
708 struct GNUNET_MQ_Handle *mq)
709{
710 struct GSC_KeyExchangeInfo *kx;
711 struct GNUNET_HashCode h1;
712 struct GNUNET_HashCode h2;
713
715 "Initiating key exchange with `%s'\n",
716 GNUNET_i2s (pid));
718 gettext_noop ("# key exchanges initiated"),
719 1,
720 GNUNET_NO);
721
722 kx = GNUNET_new (struct GSC_KeyExchangeInfo);
724 kx->mq = mq;
725 kx->peer = pid;
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 */
748 kx);
749 }
750 return kx;
751}
752
753
763static 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));
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 }
792 GNUNET_free (kx);
793}
794
795
801static void
803{
804 struct GNUNET_MQ_Envelope *env;
805
807 gettext_noop ("# PING messages transmitted"),
808 1,
809 GNUNET_NO);
811 GNUNET_MQ_send (kx->mq, env);
812}
813
814
820static void
822{
823 struct GNUNET_HashCode key_material;
824
825 if (GNUNET_OK !=
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
850static void
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 enum GNUNET_GenericReturnValue do_verify = GNUNET_YES;
859
860 end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
865 {
867 gettext_noop ("# old ephemeral keys ignored"),
868 1,
869 GNUNET_NO);
871 "Received expired EPHEMERAL_KEY from %s\n",
872 GNUNET_i2s (&m->origin_identity));
873 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
874 return;
875 }
876 if (0 == memcmp (&m->ephemeral_key,
878 sizeof(m->ephemeral_key)))
879 {
882 "# duplicate ephemeral keys. Not verifying."),
883 1,
884 GNUNET_NO);
886 "Duplicate EPHEMERAL_KEY from %s, do not verify\n",
887 GNUNET_i2s (&m->origin_identity));
888 do_verify = GNUNET_NO;
889 }
890 if (0 != memcmp (&m->origin_identity,
891 kx->peer,
892 sizeof(struct GNUNET_PeerIdentity)))
893 {
895 "Received EPHEMERAL_KEY from %s, but expected %s\n",
896 GNUNET_i2s (&m->origin_identity),
897 GNUNET_i2s_full (kx->peer));
898 GNUNET_break_op (0);
899 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
900 return;
901 }
902 if (do_verify && ((ntohl (m->purpose.size) !=
904 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
905 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
906 + sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
907 + sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)) ||
908 (GNUNET_OK !=
910 &m->purpose,
911 &m->signature,
912 &m->origin_identity.public_key))))
913 {
914 /* invalid signature */
915 GNUNET_break_op (0);
918 "# EPHEMERAL_KEYs rejected (bad signature)"),
919 1,
920 GNUNET_NO);
922 "Received EPHEMERAL_KEY from %s with bad signature\n",
923 GNUNET_i2s (&m->origin_identity));
924 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
925 return;
926 }
928 start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
929 if ((end_t.abs_value_us <
931 (start_t.abs_value_us >
933 {
934 GNUNET_log (
936 _ (
937 "EPHEMERAL_KEY from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
938 GNUNET_i2s (kx->peer),
939 (unsigned long long) now.abs_value_us,
940 (unsigned long long) start_t.abs_value_us,
941 (unsigned long long) end_t.abs_value_us);
944 "# EPHEMERAL_KEY messages rejected due to time"),
945 1,
946 GNUNET_NO);
947 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
948 return;
949 }
950#if DEBUG_KX
951 {
952 struct GNUNET_HashCode eh;
953
954 GNUNET_CRYPTO_hash (&m->ephemeral_key, sizeof(m->ephemeral_key), &eh);
956 "Received valid EPHEMERAL_KEY `%s' from `%s' in state %d.\n",
957 GNUNET_h2s (&eh),
958 GNUNET_i2s (kx->peer),
959 kx->status);
960 }
961#endif
963 gettext_noop ("# valid ephemeral keys received"),
964 1,
965 GNUNET_NO);
966 kx->other_ephemeral_key = m->ephemeral_key;
967 kx->foreign_key_expires = end_t;
969
970 /* check if we still need to send the sender our key */
971 sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
972 switch (sender_status)
973 {
975 GNUNET_break_op (0);
976 break;
977
979 /* fine, need to send our key after updating our status, see below */
981 break;
982
984 /* other peer already got our key, but typemap did go down */
986 break;
987
989 /* other peer already got our key, typemap NOT down */
990 break;
991
993 /* other peer already got our key, typemap NOT down */
994 break;
995
996 default:
997 GNUNET_break (0);
998 break;
999 }
1000 /* check if we need to confirm everything is fine via PING + PONG */
1001 switch (kx->status)
1002 {
1004 GNUNET_assert (NULL == kx->keep_alive_task);
1006 monitor_notify_all (kx);
1007 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1008 send_key (kx);
1009 else
1010 send_ping (kx);
1011 break;
1012
1014 GNUNET_assert (NULL == kx->keep_alive_task);
1016 monitor_notify_all (kx);
1017 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1018 send_key (kx);
1019 else
1020 send_ping (kx);
1021 break;
1022
1024 GNUNET_assert (NULL == kx->keep_alive_task);
1025 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1026 send_key (kx);
1027 else
1028 send_ping (kx);
1029 break;
1030
1033 monitor_notify_all (kx);
1034 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1035 send_key (kx);
1036 else
1037 send_ping (kx);
1038 break;
1039
1041 if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
1042 send_key (kx);
1043 else
1044 send_ping (kx);
1045 break;
1046
1047 default:
1048 GNUNET_break (0);
1049 break;
1050 }
1051 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1052}
1053
1054
1055static void
1056send_keep_alive (void *cls);
1057
1058
1066static void
1067handle_ping (void *cls, const struct PingMessage *m)
1068{
1069 struct GSC_KeyExchangeInfo *kx = cls;
1070 struct PingMessage t;
1071 struct PongMessage tx;
1072 struct PongMessage *tp;
1073 struct GNUNET_MQ_Envelope *env;
1075
1077 gettext_noop ("# PING messages received"),
1078 1,
1079 GNUNET_NO);
1083 {
1084 /* ignore */
1086 gettext_noop (
1087 "# PING messages dropped (out of order)"),
1088 1,
1089 GNUNET_NO);
1090 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1091 return;
1092 }
1094 "Core service receives PING request from `%s'.\n",
1095 GNUNET_i2s (kx->peer));
1096 derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1097 if (GNUNET_OK != do_decrypt (kx,
1098 &iv,
1099 &m->target,
1100 &t.target,
1101 sizeof(struct PingMessage)
1102 - ((void *) &m->target - (void *) m)))
1103 {
1104 GNUNET_break_op (0);
1105 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1106 return;
1107 }
1108 if (0 !=
1109 memcmp (&t.target, &GSC_my_identity, sizeof(struct GNUNET_PeerIdentity)))
1110 {
1113 "Decryption of PING from peer `%s' failed, PING for `%s'?\n",
1114 GNUNET_i2s (kx->peer),
1115 GNUNET_i2s2 (&t.target));
1116 else
1117 GNUNET_log (
1119 "Decryption of PING from peer `%s' failed after rekey (harmless)\n",
1120 GNUNET_i2s (kx->peer));
1121 GNUNET_break_op (0);
1122 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1123 return;
1124 }
1125 /* construct PONG */
1126 tx.reserved = 0;
1127 tx.challenge = t.challenge;
1128 tx.target = t.target;
1130 tp->iv_seed = calculate_seed (kx);
1131 derive_pong_iv (&iv, &kx->encrypt_key, tp->iv_seed, t.challenge, kx->peer);
1132 do_encrypt (kx,
1133 &iv,
1134 &tx.challenge,
1135 &tp->challenge,
1136 sizeof(struct PongMessage)
1137 - ((void *) &tp->challenge - (void *) tp));
1139 gettext_noop ("# PONG messages created"),
1140 1,
1141 GNUNET_NO);
1142 GNUNET_MQ_send (kx->mq, env);
1143 if (NULL != kx->keep_alive_task)
1144 {
1147 }
1148 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1149}
1150
1151
1158static void
1160{
1161 struct GSC_KeyExchangeInfo *kx = cls;
1162 struct GNUNET_TIME_Relative retry;
1163 struct GNUNET_TIME_Relative left;
1164
1165 kx->keep_alive_task = NULL;
1167 if (0 == left.rel_value_us)
1168 {
1170 gettext_noop ("# sessions terminated by timeout"),
1171 1,
1172 GNUNET_NO);
1173 GSC_SESSIONS_end (kx->peer);
1175 monitor_notify_all (kx);
1176 send_key (kx);
1177 return;
1178 }
1180 "Sending KEEPALIVE to `%s'\n",
1181 GNUNET_i2s (kx->peer));
1183 gettext_noop ("# keepalive messages sent"),
1184 1,
1185 GNUNET_NO);
1186 setup_fresh_ping (kx);
1187 send_ping (kx);
1190 kx->keep_alive_task =
1192}
1193
1194
1202static void
1204{
1206
1207 kx->timeout =
1209 delta =
1211 if (delta.rel_value_us > 5LL * 1000LL * 1000LL)
1212 {
1213 /* we only notify monitors about timeout changes if those
1214 are bigger than the threshold (5s) */
1215 monitor_notify_all (kx);
1216 }
1217 if (NULL != kx->keep_alive_task)
1222 kx);
1223}
1224
1225
1232static void
1233handle_pong (void *cls, const struct PongMessage *m)
1234{
1235 struct GSC_KeyExchangeInfo *kx = cls;
1236 struct PongMessage t;
1238
1240 gettext_noop ("# PONG messages received"),
1241 1,
1242 GNUNET_NO);
1243 switch (kx->status)
1244 {
1247 gettext_noop (
1248 "# PONG messages dropped (connection down)"),
1249 1,
1250 GNUNET_NO);
1251 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1252 return;
1253
1256 gettext_noop (
1257 "# PONG messages dropped (out of order)"),
1258 1,
1259 GNUNET_NO);
1260 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1261 return;
1262
1264 break;
1265
1267 break;
1268
1270 break;
1271
1272 default:
1273 GNUNET_break (0);
1274 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1275 return;
1276 }
1278 "Core service receives PONG response from `%s'.\n",
1279 GNUNET_i2s (kx->peer));
1280 /* mark as garbage, just to be sure */
1281 memset (&t, 255, sizeof(t));
1282 derive_pong_iv (&iv,
1283 &kx->decrypt_key,
1284 m->iv_seed,
1285 kx->ping_challenge,
1287 if (GNUNET_OK != do_decrypt (kx,
1288 &iv,
1289 &m->challenge,
1290 &t.challenge,
1291 sizeof(struct PongMessage)
1292 - ((void *) &m->challenge - (void *) m)))
1293 {
1294 GNUNET_break_op (0);
1295 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1296 return;
1297 }
1299 gettext_noop ("# PONG messages decrypted"),
1300 1,
1301 GNUNET_NO);
1302 if ((0 !=
1303 memcmp (&t.target, kx->peer, sizeof(struct GNUNET_PeerIdentity))) ||
1304 (kx->ping_challenge != t.challenge))
1305 {
1306 /* PONG malformed */
1308 "Received malformed PONG wanted sender `%s' with challenge %u\n",
1309 GNUNET_i2s (kx->peer),
1310 (unsigned int) kx->ping_challenge);
1312 "Received malformed PONG received from `%s' with challenge %u\n",
1313 GNUNET_i2s (&t.target),
1314 (unsigned int) t.challenge);
1315 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1316 return;
1317 }
1319 "Received valid PONG from `%s'\n",
1320 GNUNET_i2s (kx->peer));
1321 /* no need to resend key any longer */
1322 if (NULL != kx->retry_set_key_task)
1323 {
1325 kx->retry_set_key_task = NULL;
1326 }
1327 switch (kx->status)
1328 {
1330 GNUNET_assert (0); /* should be impossible */
1331 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1332 return;
1333
1335 GNUNET_assert (0); /* should be impossible */
1336 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1337 return;
1338
1341 gettext_noop (
1342 "# session keys confirmed via PONG"),
1343 1,
1344 GNUNET_NO);
1346 monitor_notify_all (kx);
1347 GSC_SESSIONS_create (kx->peer, kx);
1348 GNUNET_assert (NULL == kx->keep_alive_task);
1349 update_timeout (kx);
1350 break;
1351
1354 gettext_noop ("# timeouts prevented via PONG"),
1355 1,
1356 GNUNET_NO);
1357 update_timeout (kx);
1358 break;
1359
1362 gettext_noop (
1363 "# rekey operations confirmed via PONG"),
1364 1,
1365 GNUNET_NO);
1367 monitor_notify_all (kx);
1368 update_timeout (kx);
1369 break;
1370
1371 default:
1372 GNUNET_break (0);
1373 break;
1374 }
1375 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1376}
1377
1378
1384static void
1386{
1387 struct GNUNET_MQ_Envelope *env;
1388
1390 if (NULL != kx->retry_set_key_task)
1391 {
1393 kx->retry_set_key_task = NULL;
1394 }
1395 /* always update sender status in SET KEY message */
1396#if DEBUG_KX
1397 {
1398 struct GNUNET_HashCode hc;
1399
1401 sizeof(current_ekm.ephemeral_key),
1402 &hc);
1404 "Sending EPHEMERAL_KEY %s to `%s' (my status: %d)\n",
1405 GNUNET_h2s (&hc),
1406 GNUNET_i2s (kx->peer),
1407 kx->status);
1408 }
1409#endif
1410 current_ekm.sender_status = htonl ((int32_t) (kx->status));
1412 GNUNET_MQ_send (kx->mq, env);
1414 send_ping (kx);
1415 kx->retry_set_key_task =
1418 kx);
1419}
1420
1421
1422void
1424 const void *payload,
1425 size_t payload_size)
1426{
1427 size_t used = payload_size + sizeof(struct EncryptedMessage);
1428 char pbuf[used]; /* plaintext */
1429 struct EncryptedMessage *em; /* encrypted message */
1430 struct EncryptedMessage *ph; /* plaintext header */
1431 struct GNUNET_MQ_Envelope *env;
1433 struct GNUNET_CRYPTO_AuthKey auth_key;
1434
1435 ph = (struct EncryptedMessage *) pbuf;
1436 ph->sequence_number = htonl (++kx->last_sequence_number_sent);
1437 ph->iv_seed = calculate_seed (kx);
1438 ph->reserved = 0;
1440 GNUNET_memcpy (&ph[1], payload, payload_size);
1442 payload_size,
1444 em->iv_seed = ph->iv_seed;
1445 derive_iv (&iv, &kx->encrypt_key, ph->iv_seed, kx->peer);
1447 &iv,
1448 &ph->sequence_number,
1449 &em->sequence_number,
1450 used - ENCRYPTED_HEADER_SIZE));
1451#if DEBUG_KX
1452 {
1453 struct GNUNET_HashCode hc;
1454
1456 used - ENCRYPTED_HEADER_SIZE,
1457 &hc);
1459 "Encrypted payload `%s' of %u bytes for %s\n",
1460 GNUNET_h2s (&hc),
1461 (unsigned int) (used - ENCRYPTED_HEADER_SIZE),
1462 GNUNET_i2s (kx->peer));
1463 }
1464#endif
1465 derive_auth_key (&auth_key, &kx->encrypt_key, ph->iv_seed);
1466 GNUNET_CRYPTO_hmac (&auth_key,
1467 &em->sequence_number,
1468 used - ENCRYPTED_HEADER_SIZE,
1469 &em->hmac);
1470#if DEBUG_KX
1471 {
1472 struct GNUNET_HashCode hc;
1473
1474 GNUNET_CRYPTO_hash (&auth_key, sizeof(auth_key), &hc);
1476 "For peer %s, used AC %s to create hmac %s\n",
1477 GNUNET_i2s (kx->peer),
1478 GNUNET_h2s (&hc),
1479 GNUNET_h2s2 (&em->hmac));
1480 }
1481#endif
1483 GNUNET_MQ_send (kx->mq, env);
1484}
1485
1486
1495static int
1496check_encrypted (void *cls, const struct EncryptedMessage *m)
1497{
1498 uint16_t size = ntohs (m->header.size) - sizeof(*m);
1499
1500 if (size < sizeof(struct GNUNET_MessageHeader))
1501 {
1502 GNUNET_break_op (0);
1503 return GNUNET_SYSERR;
1504 }
1505 return GNUNET_OK;
1506}
1507
1508
1516static void
1517handle_encrypted (void *cls, const struct EncryptedMessage *m)
1518{
1519 struct GSC_KeyExchangeInfo *kx = cls;
1520 struct EncryptedMessage *pt; /* plaintext */
1521 struct GNUNET_HashCode ph;
1522 uint32_t snum;
1523 struct GNUNET_TIME_Absolute t;
1525 struct GNUNET_CRYPTO_AuthKey auth_key;
1526 uint16_t size = ntohs (m->header.size);
1527 char buf[size] GNUNET_ALIGN;
1528
1530 {
1532 gettext_noop (
1533 "# DATA message dropped (out of order)"),
1534 1,
1535 GNUNET_NO);
1536 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1537 return;
1538 }
1539 if (0 ==
1541 {
1542 GNUNET_log (
1544 _ (
1545 "Session to peer `%s' went down due to key expiration (should not happen)\n"),
1546 GNUNET_i2s (kx->peer));
1548 gettext_noop (
1549 "# sessions terminated by key expiration"),
1550 1,
1551 GNUNET_NO);
1552 GSC_SESSIONS_end (kx->peer);
1553 if (NULL != kx->keep_alive_task)
1554 {
1556 kx->keep_alive_task = NULL;
1557 }
1559 monitor_notify_all (kx);
1560 send_key (kx);
1561 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1562 return;
1563 }
1564
1565 /* validate hash */
1566#if DEBUG_KX
1567 {
1568 struct GNUNET_HashCode hc;
1569
1570 GNUNET_CRYPTO_hash (&m->sequence_number, size - ENCRYPTED_HEADER_SIZE, &hc);
1572 "Received encrypted payload `%s' of %u bytes from %s\n",
1573 GNUNET_h2s (&hc),
1574 (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
1575 GNUNET_i2s (kx->peer));
1576 }
1577#endif
1578 derive_auth_key (&auth_key, &kx->decrypt_key, m->iv_seed);
1579 GNUNET_CRYPTO_hmac (&auth_key,
1580 &m->sequence_number,
1582 &ph);
1583#if DEBUG_KX
1584 {
1585 struct GNUNET_HashCode hc;
1586
1587 GNUNET_CRYPTO_hash (&auth_key, sizeof(auth_key), &hc);
1589 "For peer %s, used AC %s to verify hmac %s\n",
1590 GNUNET_i2s (kx->peer),
1591 GNUNET_h2s (&hc),
1592 GNUNET_h2s2 (&m->hmac));
1593 }
1594#endif
1595 if (0 != memcmp (&ph, &m->hmac, sizeof(struct GNUNET_HashCode)))
1596 {
1597 /* checksum failed */
1599 "Failed checksum validation for a message from `%s'\n",
1600 GNUNET_i2s (kx->peer));
1601 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1602 return;
1603 }
1604 derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
1605 /* decrypt */
1606 if (GNUNET_OK != do_decrypt (kx,
1607 &iv,
1608 &m->sequence_number,
1611 {
1612 GNUNET_break_op (0);
1613 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1614 return;
1615 }
1617 "Decrypted %u bytes from %s\n",
1618 (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
1619 GNUNET_i2s (kx->peer));
1620 pt = (struct EncryptedMessage *) buf;
1621
1622 /* validate sequence number */
1623 snum = ntohl (pt->sequence_number);
1624 if (kx->last_sequence_number_received == snum)
1625 {
1627 "Received duplicate message, ignoring.\n");
1628 /* duplicate, ignore */
1630 gettext_noop ("# bytes dropped (duplicates)"),
1631 size,
1632 GNUNET_NO);
1633 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1634 return;
1635 }
1636 if ((kx->last_sequence_number_received > snum) &&
1637 (kx->last_sequence_number_received - snum > 32))
1638 {
1640 "Received ancient out of sequence message, ignoring.\n");
1641 /* ancient out of sequence, ignore */
1643 gettext_noop (
1644 "# bytes dropped (out of sequence)"),
1645 size,
1646 GNUNET_NO);
1647 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1648 return;
1649 }
1650 if (kx->last_sequence_number_received > snum)
1651 {
1652 uint32_t rotbit = 1U << (kx->last_sequence_number_received - snum - 1);
1653
1654 if ((kx->last_packets_bitmap & rotbit) != 0)
1655 {
1657 "Received duplicate message, ignoring.\n");
1659 gettext_noop ("# bytes dropped (duplicates)"),
1660 size,
1661 GNUNET_NO);
1662 /* duplicate, ignore */
1663 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1664 return;
1665 }
1666 kx->last_packets_bitmap |= rotbit;
1667 }
1668 if (kx->last_sequence_number_received < snum)
1669 {
1670 unsigned int shift = (snum - kx->last_sequence_number_received);
1671
1672 if (shift >= 8 * sizeof(kx->last_packets_bitmap))
1673 kx->last_packets_bitmap = 0;
1674 else
1675 kx->last_packets_bitmap <<= shift;
1677 }
1678
1679 /* check timestamp */
1681 if (GNUNET_TIME_absolute_get_duration (t).rel_value_us >
1682 MAX_MESSAGE_AGE.rel_value_us)
1683 {
1685 "Message received far too old (%s). Content ignored.\n",
1688 GNUNET_YES));
1690 gettext_noop (
1691 "# bytes dropped (ancient message)"),
1692 size,
1693 GNUNET_NO);
1694 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1695 return;
1696 }
1697
1698 /* process decrypted message(s) */
1699 update_timeout (kx);
1701 gettext_noop ("# bytes of payload decrypted"),
1702 size - sizeof(struct EncryptedMessage),
1703 GNUNET_NO);
1704 if (GNUNET_OK !=
1706 &buf[sizeof(struct EncryptedMessage)],
1707 size - sizeof(struct EncryptedMessage),
1708 GNUNET_YES,
1709 GNUNET_NO))
1710 GNUNET_break_op (0);
1711
1712 GNUNET_TRANSPORT_core_receive_continue (transport, kx->peer);
1713}
1714
1715
1720static void
1722{
1723 current_ekm.header.size = htons (sizeof(struct EphemeralKeyMessage));
1725 current_ekm.sender_status = 0; /* to be set later */
1728 htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
1729 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
1730 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
1731 + sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)
1732 + sizeof(struct GNUNET_PeerIdentity));
1736 "core",
1737 "USE_EPHEMERAL_KEYS"))
1738 {
1742 REKEY_TOLERANCE)));
1743 }
1744 else
1745 {
1748 }
1756}
1757
1758
1764static void
1765do_rekey (void *cls)
1766{
1767 struct GSC_KeyExchangeInfo *pos;
1768
1769 (void) cls;
1773 {
1774 struct GNUNET_HashCode eh;
1775
1777 sizeof(current_ekm.ephemeral_key),
1778 &eh);
1779 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Rekeying to %s\n", GNUNET_h2s (&eh));
1780 }
1781 for (pos = kx_head; NULL != pos; pos = pos->next)
1782 {
1783 if (GNUNET_CORE_KX_STATE_UP == pos->status)
1784 {
1786 derive_session_keys (pos);
1787 }
1788 else if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
1789 {
1791 }
1792 monitor_notify_all (pos);
1793 send_key (pos);
1794 }
1795}
1796
1797
1804int
1806{
1808 GNUNET_MQ_hd_fixed_size (ephemeral_key,
1810 struct EphemeralKeyMessage,
1811 NULL),
1814 struct PingMessage,
1815 NULL),
1818 struct PongMessage,
1819 NULL),
1820 GNUNET_MQ_hd_var_size (encrypted,
1822 struct EncryptedMessage,
1823 NULL),
1825 };
1826
1827 my_private_key = *pk;
1832 {
1833 struct GNUNET_HashCode eh;
1834
1836 sizeof(current_ekm.ephemeral_key),
1837 &eh);
1839 "Starting with ephemeral key %s\n",
1840 GNUNET_h2s (&eh));
1841 }
1842
1845 transport =
1848 handlers,
1849 NULL,
1852 if (NULL == transport)
1853 {
1854 GSC_KX_done ();
1855 return GNUNET_SYSERR;
1856 }
1857 return GNUNET_OK;
1858}
1859
1860
1864void
1866{
1867 if (NULL != transport)
1868 {
1870 transport = NULL;
1871 }
1872 if (NULL != rekey_task)
1873 {
1875 rekey_task = NULL;
1876 }
1877 memset (&my_ephemeral_key,
1878 0,
1879 sizeof (my_ephemeral_key));
1880 memset (&my_private_key,
1881 0,
1882 sizeof (my_private_key));
1883 if (NULL != nc)
1884 {
1886 nc = NULL;
1887 }
1888}
1889
1890
1897unsigned int
1899{
1900 return GNUNET_MQ_get_length (kxinfo->mq);
1901}
1902
1903
1904int
1906{
1907 return kxinfo->has_excess_bandwidth;
1908}
1909
1910
1919void
1921{
1922 struct GNUNET_MQ_Envelope *env;
1923 struct MonitorNotifyMessage *done_msg;
1924 struct GSC_KeyExchangeInfo *kx;
1925
1927 for (kx = kx_head; NULL != kx; kx = kx->next)
1928 {
1929 struct GNUNET_MQ_Envelope *env;
1930 struct MonitorNotifyMessage *msg;
1931
1933 msg->state = htonl ((uint32_t) kx->status);
1934 msg->peer = *kx->peer;
1935 msg->timeout = GNUNET_TIME_absolute_hton (kx->timeout);
1937 }
1939 done_msg->state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
1942}
1943
1944
1945/* end of gnunet-service-core_kx.c */
struct GNUNET_MQ_MessageHandlers handlers[]
Definition: 003.c:1
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_CORE_OPTION_SEND_FULL_INBOUND
Client wants all inbound messages in full.
Definition: core.h:53
#define GNUNET_CORE_OPTION_SEND_HDR_INBOUND
Client just wants the 4-byte message headers of all inbound messages.
Definition: core.h:59
#define gettext_noop(String)
Definition: gettext.h:70
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
static struct GNUNET_FS_Handle * ctx
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
static struct GNUNET_IDENTITY_Handle * identity
Which namespace do we publish to? NULL if we do not publish to a namespace.
const struct GNUNET_CONFIGURATION_Handle * GSC_cfg
Our configuration.
struct GNUNET_PeerIdentity GSC_my_identity
Our identity.
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.
struct GNUNET_STATISTICS_Handle * GSC_stats
For creating statistics.
Globals for gnunet-service-core.
static void handle_pong(void *cls, const struct PongMessage *m)
We received a PONG message.
static void handle_encrypted(void *cls, const struct EncryptedMessage *m)
We received an encrypted message.
static void set_key_retry_task(void *cls)
Task that will retry send_key() if our previous attempt failed.
#define REKEY_TOLERANCE
What time difference do we tolerate?
unsigned int GSC_NEIGHBOURS_get_queue_length(const struct GSC_KeyExchangeInfo *kxinfo)
Check how many messages are queued for the given neighbour.
static struct GNUNET_CRYPTO_EddsaPrivateKey my_private_key
Our private key.
static struct GNUNET_CRYPTO_EcdhePrivateKey my_ephemeral_key
Our ephemeral private key.
int GSC_NEIGHBOURS_check_excess_bandwidth(const struct GSC_KeyExchangeInfo *kxinfo)
Check if the given neighbour has excess bandwidth available.
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.
#define MIN_PING_FREQUENCY
What is the minimum frequency for a PING message?
static struct GSC_KeyExchangeInfo * kx_tail
DLL tail.
void GSC_KX_handle_client_monitor_peers(struct GNUNET_MQ_Handle *mq)
Handle GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request.
static void handle_ephemeral_key(void *cls, const struct EphemeralKeyMessage *m)
We received a GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY message.
#define REKEY_FREQUENCY
How often do we rekey?
static void sign_ephemeral_key()
Setup the message that links the ephemeral key to our persistent public key and generate the appropri...
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.
static struct GNUNET_NotificationContext * nc
Notification context for broadcasting to monitors.
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_key(struct GSC_KeyExchangeInfo *kx)
Send our key (and encrypted PING) to the other peer.
static uint32_t calculate_seed(struct GSC_KeyExchangeInfo *kx)
Calculate seed value we should use for a message.
static void handle_ping(void *cls, const struct PingMessage *m)
We received a PING message.
static struct GSC_KeyExchangeInfo * kx_head
DLL head.
static struct EphemeralKeyMessage current_ekm
Current message we send for a key exchange.
int GSC_KX_init(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Initialize KX subsystem.
static void do_rekey(void *cls)
Task run to trigger rekeying.
#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...
#define INITIAL_SET_KEY_RETRY_FREQUENCY
How long do we wait for SET_KEY confirmation initially?
void GSC_KX_done()
Shutdown KX subsystem.
#define ENCRYPTED_HEADER_SIZE
Number of bytes (at the beginning) of struct EncryptedMessage that are NOT encrypted.
static void derive_session_keys(struct GSC_KeyExchangeInfo *kx)
Derive fresh session keys from the current ephemeral keys.
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).
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.
static void setup_fresh_ping(struct GSC_KeyExchangeInfo *kx)
Create a fresh PING message for transmission to the other peer.
static void update_timeout(struct GSC_KeyExchangeInfo *kx)
We've seen a valid message from the other peer.
static void send_ping(struct GSC_KeyExchangeInfo *kx)
Send our PING to the other 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.
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...
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.
static int deliver_message(void *cls, const struct GNUNET_MessageHeader *m)
Deliver P2P message to interested clients.
static void monitor_notify_all(struct GSC_KeyExchangeInfo *kx)
Inform all monitors about the KX state of the given peer.
static struct GNUNET_SCHEDULER_Task * rekey_task
Task scheduled for periodic re-generation (and thus rekeying) of our ephemeral key.
static struct GNUNET_TRANSPORT_CoreHandle * transport
Transport service.
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 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.
code for managing the key exchange (SET_KEY, PING, PONG) with other peers
void GSC_SESSIONS_reinit(const struct GNUNET_PeerIdentity *peer)
The other peer has indicated that it 'lost' the session (KX down), reinitialize the session on our en...
void GSC_SESSIONS_end(const struct GNUNET_PeerIdentity *pid)
End the session with the given peer (we are no longer connected).
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,...
void GSC_SESSIONS_create(const struct GNUNET_PeerIdentity *peer, struct GSC_KeyExchangeInfo *kx)
Create a session, a key exchange was just completed.
void GSC_SESSIONS_set_typemap(const struct GNUNET_PeerIdentity *peer, const struct GNUNET_MessageHeader *msg)
We've received a typemap message from a peer, update ours.
static unsigned long long payload
How much data are we currently storing in the database?
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_TRANSPORT_PluginMonitor * pm
Handle if we are monitoring plugin session activity.
static struct GNUNET_SCHEDULER_Task * t
Main task.
Constants for network protocols.
API of the transport service towards the CORE service (TNG version)
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.
void GNUNET_TRANSPORT_core_disconnect(struct GNUNET_TRANSPORT_CoreHandle *handle)
Disconnect from the transport service.
enum GNUNET_GenericReturnValue 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".
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don't receive messages from the peer?
GNUNET_CORE_KxState
State machine for our P2P encryption handshake.
@ GNUNET_CORE_KX_PEER_DISCONNECT
Last state of a KX (when it is being terminated).
@ GNUNET_CORE_KX_STATE_UP
The other peer has confirmed our session key + PING with a PONG message encrypted with their session ...
@ GNUNET_CORE_KX_STATE_DOWN
No handshake yet.
@ GNUNET_CORE_KX_STATE_KEY_SENT
We've sent our session key.
@ GNUNET_CORE_KX_STATE_KEY_RECEIVED
We've received the other peers session key.
@ GNUNET_CORE_KX_ITERATION_FINISHED
This is not a state in a peer's state machine, but a special value used with the GNUNET_CORE_MonitorC...
@ GNUNET_CORE_KX_STATE_REKEY_SENT
We're rekeying (or had a timeout), so we have sent the other peer our new ephemeral key,...
enum GNUNET_GenericReturnValue 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:714
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:436
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:198
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.
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.
enum GNUNET_GenericReturnValue 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:607
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
enum GNUNET_GenericReturnValue 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:690
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:214
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.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
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:99
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:330
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:221
enum GNUNET_GenericReturnValue 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:70
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:267
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
#define GNUNET_log(kind,...)
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_PACKED
gcc-ism to get packed structs.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
const char * GNUNET_h2s2(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
const char * GNUNET_i2s2(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc)
Destroy the context, force disconnect for all subscribers.
Definition: nc.c:138
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:533
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:293
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#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:63
struct GNUNET_NotificationContext * GNUNET_notification_context_create(unsigned int queue_length)
Create a new notification context.
Definition: nc.c:122
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:190
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:78
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, struct GNUNET_MQ_Handle *mq)
Add a subscriber to the notification context.
Definition: nc.c:161
#define GNUNET_MESSAGE_TYPE_CORE_PING
Check that other peer is alive (challenge).
#define GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP
uncompressed type map of the sender
#define GNUNET_MESSAGE_TYPE_CORE_PONG
Confirmation that other peer is alive.
#define GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE
Encapsulation for an encrypted message between peers.
#define GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP
gzip-compressed type map of the sender
#define GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY
Reply for monitor by CORE service.
#define GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP
Other peer confirms having received the type map.
#define GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY
Session key exchange between peers.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
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:1272
enum GNUNET_GenericReturnValue 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:101
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:86
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:404
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_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:436
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:351
#define GNUNET_TIME_UNIT_SECONDS
One second.
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:405
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:570
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
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:585
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:469
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:316
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:450
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:550
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:421
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:638
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
void receiver(void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)
Callback to read from the SOCKS5 proxy.
Definition: socks.c:330
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:36
#define GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY
Purpose is to set a session key.
Encapsulation for encrypted messages exchanged between peers.
uint32_t iv_seed
Random value used for IV generation.
struct GNUNET_TIME_AbsoluteNBO timestamp
Timestamp.
uint32_t sequence_number
Sequence number, in network byte order.
struct GNUNET_HashCode hmac
MAC of the encrypted message (starting at sequence_number), used to verify message integrity.
struct GNUNET_MessageHeader header
Message type is GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
uint32_t reserved
Reserved, always zero.
Message transmitted with the signed ephemeral key of a peer.
struct GNUNET_PeerIdentity origin_identity
Public key of the signing peer (persistent version, not the ephemeral public key).
struct GNUNET_MessageHeader header
Message type is GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY.
int32_t sender_status
Status of the sender (should be in enum PeerStateMachine), nbo.
struct GNUNET_TIME_AbsoluteNBO expiration_time
When does the given ephemeral key expire (end of validity).
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_CRYPTO_EccSignaturePurpose purpose
Information about what is being signed.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key
Ephemeral public ECC key.
type for (message) authentication keys
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
Private ECC key encoded for transmission.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
Private ECC key encoded for transmission.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
unsigned char aes_key[(256/8)]
Actual key for AES.
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Header for all communications.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
Handle to a message stream tokenizer.
Definition: mst.c:45
The notification context is the key datastructure for a convenience API used for transmission of noti...
Definition: nc.c:77
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Handle for the transport service (includes all of the state for the transport service).
Information about the status of a key exchange with another peer.
struct GSC_KeyExchangeInfo * prev
DLL.
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).
struct GNUNET_SCHEDULER_Task * retry_set_key_task
ID of task used for re-trying SET_KEY and PING message.
struct GNUNET_TIME_Relative set_key_retry_frequency
At what frequency are we currently re-trying SET_KEY messages?
struct GNUNET_TIME_Absolute last_notify_timeout
What was the last timeout we informed our monitors about?
const struct GNUNET_PeerIdentity * peer
Identity of the peer.
uint32_t last_sequence_number_sent
last sequence number transmitted
struct GNUNET_MessageStreamTokenizer * mst
Our message stream tokenizer (for encrypted payload).
struct GSC_KeyExchangeInfo * next
DLL.
uint32_t ping_challenge
What was our PING challenge number (for this peer)?
struct GNUNET_MQ_Handle * mq
Message queue for sending messages to peer.
uint32_t last_sequence_number_received
last sequence number received on this connection (highest)
struct GNUNET_CRYPTO_EcdhePublicKey other_ephemeral_key
Ephemeral public ECC key of the other peer.
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...
struct GNUNET_TIME_Absolute timeout
When should the session time out (if there are no PONGs)?
int has_excess_bandwidth
GNUNET_YES if this peer currently has excess bandwidth.
struct PingMessage ping
PING message we transmit to the other peer.
struct GNUNET_SCHEDULER_Task * keep_alive_task
ID of task used for sending keep-alive pings.
enum GNUNET_CORE_KxState status
What is our connection status?
struct GNUNET_TIME_Absolute foreign_key_expires
At what time did the other peer generate the decryption key?
uint32_t last_packets_bitmap
Bit map indicating which of the 32 sequence numbers before the last were received (good for accepting...
Message sent by the service to monitor clients to notify them about a peer changing status.
Definition: core.h:301
uint32_t state
New peer state, an enum GNUNET_CORE_KxState in NBO.
Definition: core.h:310
struct GNUNET_TIME_AbsoluteNBO timeout
How long will we stay in this state (if nothing else happens)?
Definition: core.h:320
We're sending an (encrypted) PING to the other peer to check if it can decrypt.
struct GNUNET_MessageHeader header
Message type is GNUNET_MESSAGE_TYPE_CORE_PING.
struct GNUNET_PeerIdentity target
Intended target of the PING, used primarily to check that decryption actually worked.
uint32_t challenge
Random number chosen to make replay harder.
Response to a PING.
uint32_t challenge
Random number to make replay attacks harder.
uint32_t reserved
Reserved, always zero.
uint32_t iv_seed
Seed for the IV.
struct GNUNET_PeerIdentity target
Intended target of the PING, used primarily to check that decryption actually worked.