GNUnet 0.28.1-dev.4-8-g14b9efcb0
 
Loading...
Searching...
No Matches
gnunet-service-cadet_tunnels.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017, 2018 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 */
33#include "gnunet_common.h"
34#include "gnunet_pils_service.h"
36#include "platform.h"
37#include "gnunet_util_lib.h"
39#include "cadet_protocol.h"
45
46
47#define LOG(level, ...) GNUNET_log_from (level, "cadet-tun", __VA_ARGS__)
48
54#define MAX_UNVERIFIED_ATTEMPTS 16
55
59#define IDLE_DESTROY_DELAY GNUNET_TIME_relative_multiply ( \
60 GNUNET_TIME_UNIT_SECONDS, 90)
61
66#define INITIAL_KX_RETRY_DELAY GNUNET_TIME_relative_multiply ( \
67 GNUNET_TIME_UNIT_MILLISECONDS, 250)
68
72#define MAX_SKIPPED_KEYS 64
73
78#define MAX_KEY_GAP 256
79
80
116
117
256
257
264typedef void
265(*CadetTunnelAxolotlCallback) (void *cls,
267
268
321
322
364
365
531
532
539int
541{
542 const struct GNUNET_PeerIdentity *my_identity;
543
546
547 if (0 > GNUNET_memcmp (my_identity, other))
548 return GNUNET_YES;
549 else if (0 < GNUNET_memcmp (my_identity, other))
550 return GNUNET_NO;
551 else
552 {
553 GNUNET_break_op (0);
554 return GNUNET_SYSERR;
555 }
556}
557
558
565static void
567{
568 struct CadetTunnel *t = ct->t;
569
571 GNUNET_CONTAINER_DLL_remove (t->connection_ready_head,
572 t->connection_ready_tail,
573 ct);
574 GNUNET_assert (0 < t->num_ready_connections);
575 t->num_ready_connections--;
576 ct->is_ready = GNUNET_NO;
577 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head,
578 t->connection_busy_tail,
579 ct);
580 t->num_busy_connections++;
581}
582
583
591const char *
592GCT_2s (const struct CadetTunnel *t)
593{
594 static char buf[64];
595
596 if (NULL == t)
597 return "Tunnel(NULL)";
598 GNUNET_snprintf (buf,
599 sizeof(buf),
600 "Tunnel %s",
601 GNUNET_i2s (GCP_get_id (t->destination)));
602 return buf;
603}
604
605
613static const char *
615{
616 static char buf[32];
617
618 switch (es)
619 {
621 return "CADET_TUNNEL_KEY_UNINITIALIZED";
623 return "CADET_TUNNEL_KEY_AX_RECV";
625 return "CADET_TUNNEL_KEY_AX_SENT";
627 return "CADET_TUNNEL_KEY_AX_SENT_AND_RECV";
629 return "CADET_TUNNEL_KEY_AX_AUTH_SENT";
631 return "CADET_TUNNEL_KEY_OK";
632 }
633 GNUNET_snprintf (buf,
634 sizeof(buf),
635 "%u (UNKNOWN STATE)",
636 es);
637 return buf;
638}
639
640
647struct CadetPeer *
649{
650 return t->destination;
651}
652
653
654unsigned int
656{
657 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
658}
659
660
668static struct CadetChannel *
671{
672 return GNUNET_CONTAINER_multihashmap32_get (t->channels,
673 ntohl (ctn.cn));
674}
675
676
677unsigned int
679{
680 return t->num_ready_connections + t->num_busy_connections;
681}
682
683
691static struct CadetTConnection *
693{
694 struct CadetTConnection *hd = t->connection_ready_head;
695
696 GNUNET_assert ((NULL == hd) ||
697 (GNUNET_YES == hd->is_ready));
698 return hd;
699}
700
701
711{
712 return t->estate;
713}
714
715
724static void
725trigger_transmissions (void *cls);
726
727
728/* ************************************** start core crypto ***************************** */
729
730
736static void
738{
740 "Creating new ephemeral ratchet key (DHRs)\n");
742}
743
744
753static void
755 struct GNUNET_HashCode *hash,
756 const void *source,
757 unsigned int len)
758{
759 static const char ctx[] = "axolotl HMAC-HASH";
760 struct GNUNET_CRYPTO_AuthKey auth_key;
761
763 &auth_key,
764 sizeof auth_key,
765 ctx,
766 sizeof(ctx),
767 key,
768 sizeof *key);
769 GNUNET_CRYPTO_hmac (&auth_key,
770 source,
771 len,
772 hash);
773}
774
775
784static void
786 struct GNUNET_CRYPTO_AeadSecretKey *out,
787 const void *source,
788 unsigned int len)
789{
790 static const char ctx[] = "axolotl derive key";
791 struct GNUNET_HashCode h;
792
794 &h,
795 source,
796 len);
798 out, sizeof(*out),
799 ctx, sizeof(ctx),
800 &h, sizeof(h));
801}
802
803
812static void
814 void *dst,
815 const void *src,
816 size_t size)
817{
819 struct GNUNET_CRYPTO_AeadNonce iv;
821
822 ax->ratchet_counter++;
823 if ((GNUNET_YES == ax->ratchet_allowed) &&
824 ((ratchet_messages <= ax->ratchet_counter) ||
827 {
829 }
830 if (GNUNET_YES == ax->ratchet_flag)
831 {
832 /* Advance ratchet */
833 struct GNUNET_CRYPTO_AeadSecretKey keys[3];
834 struct GNUNET_HashCode dh;
835 struct GNUNET_HashCode hmac;
836 static const char ctx[] = "axolotl ratchet";
837
838 new_ephemeral (ax);
839 ax->HKs = ax->NHKs;
840
841 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
843 &ax->DHRr,
844 &dh);
845 t_ax_hmac_hash (&ax->RK,
846 &hmac,
847 &dh,
848 sizeof(dh));
850 keys, sizeof(keys),
851 ctx, sizeof(ctx),
852 &hmac, sizeof(hmac));
853 ax->RK = keys[0];
854 ax->NHKs = keys[1];
855 ax->CKs = keys[2];
856
857 ax->PNs = ax->Ns;
858 ax->Ns = 0;
861 ax->ratchet_counter = 0;
865 }
866
868 &MK,
869 "0",
870 1);
871
873 sizeof iv,
874 NULL,
875 0,
876 &MK,
877 sizeof MK);
878
879 mac = (struct GNUNET_CRYPTO_AeadMac*) &((char*) dst)[size];
881 src,
882 0,
883 NULL,
884 &MK,
885 &iv,
886 dst,
887 mac));
889 &ax->CKs,
890 "1",
891 1);
892}
893
894
905 void *dst,
906 const void *src,
907 size_t size)
908{
910 struct GNUNET_CRYPTO_AeadNonce iv;
912 size_t macoffset;
913
914 /* A short (or absent) payload is what a remote peer sends, not something
915 we may compute @a macoffset from: the subtraction below underflows. */
916 if (size < sizeof(struct GNUNET_MessageHeader)
917 + sizeof(struct GNUNET_CRYPTO_AeadMac))
918 {
919 GNUNET_break_op (0);
920 return GNUNET_SYSERR;
921 }
922 macoffset = size - sizeof (struct GNUNET_CRYPTO_AeadMac);
924 &MK,
925 "0",
926 1);
927
929 sizeof iv,
930 NULL,
931 0,
932 &MK,
933 sizeof MK);
934 mac = (struct GNUNET_CRYPTO_AeadMac*) &((char*) src)[macoffset];
935 /* Failing to authenticate is what a corrupt or forged ciphertext looks
936 like, and the callers all handle it -- #try_old_ax_keys() returns -1 on
937 exactly this. Aborting here let any peer take the service down. */
939 src,
940 0,
941 NULL,
942 &MK,
943 &iv,
944 mac,
945 dst))
946 return GNUNET_SYSERR;
947 /* Only a message we actually accepted advances the receive chain. */
949 &ax->CKr,
950 "1",
951 1);
952 return GNUNET_OK;
953}
954
955
962static void
965 size_t additional_data_size,
966 const unsigned char *additional_data)
967{
968 struct GNUNET_CRYPTO_AeadNonce nonce;
969
971 sizeof nonce,
972 NULL,
973 0,
974 &ax->HKs,
975 sizeof ax->HKs);
977 ,
978 (unsigned char*) &msg
979 ->ax_header,
980 additional_data_size,
981 additional_data,
982 &ax->HKs,
983 &nonce,
984 &msg->ax_header,
985 &msg->mac));
986}
987
988
1000 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1001 size_t esize,
1003{
1004 struct GNUNET_CRYPTO_AeadNonce iv;
1005
1007 sizeof iv,
1008 NULL,
1009 0,
1010 hkr,
1011 sizeof *hkr);
1012
1013
1015 (unsigned char*) &src->ax_header,
1016 esize,
1017 (unsigned char*) &src[1],
1018 hkr,
1019 &iv,
1020 &dst->mac,
1021 &dst->ax_header);
1022}
1023
1024
1031static void
1033 struct CadetTunnelSkippedKey *key)
1034{
1036 ax->skipped_tail,
1037 key);
1038 GNUNET_free (key);
1039 ax->skipped--;
1040}
1041
1042
1053static ssize_t
1055 void *dst,
1056 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1057 size_t size)
1058{
1059 struct CadetTunnelSkippedKey *key;
1060 struct GNUNET_CRYPTO_AeadNonce iv;
1061 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1062 struct GNUNET_CRYPTO_AeadSecretKey *valid_HK;
1063 size_t esize;
1064 size_t len;
1065 unsigned int N;
1066 const unsigned char *payload;
1067
1069 "Trying skipped keys\n");
1070 esize = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1071 len = esize;
1072 /* @a size comes off the wire; #check_tunnel_encrypted() used to accept
1073 anything, so this must not be an assertion. */
1074 if ((size <= sizeof(struct GNUNET_CADET_TunnelEncryptedMessage)) ||
1075 (len < sizeof(struct GNUNET_MessageHeader)
1076 + sizeof(struct GNUNET_CRYPTO_AeadMac)))
1077 {
1078 GNUNET_break_op (0);
1079 return -1;
1080 }
1081 payload = (const unsigned char *) &src[1];
1082
1083 /* Find a correct Header Key */
1084 valid_HK = NULL;
1085 for (key = ax->skipped_head; NULL != key; key = key->next)
1086 {
1087 /* Try decrypt header */
1088 if (GNUNET_OK == t_h_decrypt (&key->HK,
1089 src,
1090 esize,
1091 &plaintext_header))
1092 {
1093 valid_HK = &key->HK;
1094 break;
1095 }
1096 }
1097 if (NULL == key)
1098 return -1;
1099
1100 /* Find the correct message key */
1101 N = ntohl (plaintext_header.ax_header.Ns);
1102 while ((NULL != key) &&
1103 (N != key->Kn))
1104 key = key->next;
1105 if ((NULL == key) ||
1106 (0 != GNUNET_memcmp (&key->HK,
1107 valid_HK)))
1108 return -1;
1109
1110 /* Decrypt payload */
1112 sizeof iv,
1113 NULL,
1114 0,
1115 &key->MK,
1116 sizeof key->MK);
1117
1118 /* NOTE: the MAC sits at the end of the *payload*. Indexing @a src, a
1119 `struct GNUNET_CADET_TunnelEncryptedMessage *', scaled the offset by the
1120 size of that struct and read far past the end of the message -- so this
1121 never authenticated anything it was given and no skipped key ever
1122 worked. */
1124 len,
1125 payload,
1126 0,
1127 NULL,
1128 &key->MK,
1129 &iv,
1130 (const struct GNUNET_CRYPTO_AeadMac*)
1131 &payload[len - sizeof (struct GNUNET_CRYPTO_AeadMac)],
1132 dst))
1133 {
1134 return -1;
1135 }
1137 key);
1138 return len - sizeof (struct GNUNET_CRYPTO_AeadMac);
1139}
1140
1141
1148static void
1150 const struct GNUNET_CRYPTO_AeadSecretKey *HKr)
1151{
1152 struct CadetTunnelSkippedKey *key;
1153
1155 key->timestamp = GNUNET_TIME_absolute_get ();
1156 key->Kn = ax->Nr;
1157 key->HK = ax->HKr;
1158 t_hmac_derive_key (&ax->CKr,
1159 &key->MK,
1160 "0",
1161 1);
1162 t_hmac_derive_key (&ax->CKr,
1163 &ax->CKr,
1164 "1",
1165 1);
1167 ax->skipped_tail,
1168 key);
1169 ax->skipped++;
1170 ax->Nr++;
1171}
1172
1173
1184static int
1186 const struct GNUNET_CRYPTO_AeadSecretKey *HKr,
1187 uint32_t Np)
1188{
1189 int gap;
1190
1191 gap = Np - ax->Nr;
1193 "Storing skipped keys [%u, %u)\n",
1194 ax->Nr,
1195 Np);
1196 if (MAX_KEY_GAP < gap)
1197 {
1198 /* Avoid DoS (forcing peer to do more than #MAX_KEY_GAP HMAC operations) */
1199 /* TODO: start new key exchange on return */
1200 GNUNET_break_op (0);
1202 "Got message %u, expected %u+\n",
1203 Np,
1204 ax->Nr);
1205 return GNUNET_SYSERR;
1206 }
1207 if (0 > gap)
1208 {
1209 /* Delayed message: don't store keys, flag to try old keys. */
1210 return GNUNET_SYSERR;
1211 }
1212
1213 while (ax->Nr < Np)
1215 HKr);
1216
1217 while (ax->skipped > MAX_SKIPPED_KEYS)
1219 ax->skipped_tail);
1220 return GNUNET_OK;
1221}
1222
1223
1234static ssize_t
1236 void *dst,
1237 const struct
1239 size_t size)
1240{
1241 struct GNUNET_HashCode hmac;
1242 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1243 uint32_t Np;
1244 uint32_t PNp;
1245 size_t esize; /* Size of encrypted payload */
1246
1247 esize = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1248
1249 if (GNUNET_OK == t_h_decrypt (&ax->HKr,
1250 src,
1251 esize,
1252 &plaintext_header))
1253 {
1254 Np = ntohl (plaintext_header.ax_header.Ns);
1255 PNp = ntohl (plaintext_header.ax_header.PNs);
1256 }
1257 else
1258 {
1259 static const char ctx[] = "axolotl ratchet";
1260 struct GNUNET_CRYPTO_AeadSecretKey keys[3]; /* RKp, NHKp, CKp */
1262 struct GNUNET_HashCode dh;
1263 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1264
1265 /* Try Next HK */
1266 if (GNUNET_OK == t_h_decrypt (&ax->NHKr,
1267 src,
1268 esize,
1269 &plaintext_header))
1270 {
1271 /* Try the skipped keys, if that fails, we're out of luck. */
1272 return try_old_ax_keys (ax,
1273 dst,
1274 src,
1275 size);
1276 }
1277 HK = ax->HKr;
1278 ax->HKr = ax->NHKr;
1279 Np = ntohl (plaintext_header.ax_header.Ns);
1280 PNp = ntohl (plaintext_header.ax_header.PNs);
1281 DHRp = &plaintext_header.ax_header.DHRs;
1282 store_ax_keys (ax,
1283 &HK,
1284 PNp);
1285
1286 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1288 DHRp,
1289 &dh);
1290 t_ax_hmac_hash (&ax->RK,
1291 &hmac,
1292 &dh, sizeof(dh));
1294 keys, sizeof(keys),
1295 ctx, sizeof(ctx),
1296 &hmac, sizeof(hmac));
1297
1298 /* Commit "purported" keys */
1299 ax->RK = keys[0];
1300 ax->NHKr = keys[1];
1301 ax->CKr = keys[2];
1302 ax->DHRr = *DHRp;
1303 ax->Nr = 0;
1305
1306 }
1307 if ((Np != ax->Nr) &&
1308 (GNUNET_OK != store_ax_keys (ax,
1309 &ax->HKr,
1310 Np)))
1311 {
1312 /* Try the skipped keys, if that fails, we're out of luck. */
1313 return try_old_ax_keys (ax,
1314 dst,
1315 src,
1316 size);
1317 }
1318
1319 if (GNUNET_OK != t_ax_decrypt (ax,
1320 dst,
1321 &src[1],
1322 esize))
1323 return -1;
1324 ax->Nr = Np + 1;
1325 return esize - sizeof (struct GNUNET_CRYPTO_AeadMac);
1326}
1327
1328
1338static int
1340 uint32_t key,
1341 void *value)
1342{
1343 struct CadetChannel *ch = value;
1344
1346 return GNUNET_OK;
1347}
1348
1349
1357void
1360{
1361 enum CadetTunnelEState old = t->estate;
1362
1363 t->estate = state;
1365 "%s estate changed from %s to %s\n",
1366 GCT_2s (t),
1367 estate2s (old),
1368 estate2s (state));
1369
1370 if ((CADET_TUNNEL_KEY_OK != old) &&
1371 (CADET_TUNNEL_KEY_OK == t->estate))
1372 {
1373 if (NULL != t->kx_task)
1374 {
1375 GNUNET_SCHEDULER_cancel (t->kx_task);
1376 t->kx_task = NULL;
1377 }
1378 /* notify all channels that have been waiting */
1381 t);
1382 if (NULL != t->send_task)
1383 GNUNET_SCHEDULER_cancel (t->send_task);
1385 t);
1386 }
1387}
1388
1389
1398static void
1400 struct CadetTConnection *ct,
1401 struct CadetTunnelAxolotl *ax)
1402{
1403 struct CadetConnection *cc;
1404 struct GNUNET_MQ_Envelope *env;
1407
1408 if (GNUNET_YES != GCT_alice_or_betty (GCP_get_id (t->destination)))
1409 return; /* only Alice may send KX */
1410 if ((NULL == ct) ||
1411 (GNUNET_NO == ct->is_ready))
1412 ct = get_ready_connection (t);
1413 if (NULL == ct)
1414 {
1416 "Wanted to send %s in state %s, but no connection is ready, deferring\n",
1417 GCT_2s (t),
1418 estate2s (t->estate));
1419 t->next_kx_attempt = GNUNET_TIME_absolute_get ();
1420 return;
1421 }
1422 cc = ct->cc;
1425 flags = GNUNET_CADET_KX_FLAG_FORCE_REPLY; /* always for KX */
1426 msg->flags = htonl (flags);
1427 msg->cid = *GCC_get_id (cc);
1429 &msg->ephemeral_key);
1430#if DEBUG_KX
1431 msg->ephemeral_key_XXX = ax->kx_0;
1432#endif
1434 "Sending KX message to %s with ephemeral %s on CID %s\n",
1435 GCT_2s (t),
1436 GNUNET_e2s (&msg->ephemeral_key),
1437 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
1439 &msg->ratchet_key);
1441 t->kx_retry_delay = GNUNET_TIME_STD_BACKOFF (t->kx_retry_delay);
1442 t->next_kx_attempt = GNUNET_TIME_relative_to_absolute (t->kx_retry_delay);
1443 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1446 else if (CADET_TUNNEL_KEY_AX_RECV == t->estate)
1449 GCC_transmit (cc,
1450 env);
1452 "# KX transmitted",
1453 1,
1454 GNUNET_NO);
1455}
1456
1457
1468static void
1470 struct CadetTConnection *ct,
1471 struct CadetTunnelAxolotl *ax,
1472 int force_reply)
1473{
1474 struct CadetConnection *cc;
1475 struct GNUNET_MQ_Envelope *env;
1477 enum GNUNET_CADET_KX_Flags flags;
1478
1479 if ((NULL == ct) ||
1480 (GNUNET_NO == ct->is_ready))
1481 ct = get_ready_connection (t);
1482 if (NULL == ct)
1483 {
1485 "Wanted to send KX_AUTH on %s, but no connection is ready, deferring\n",
1486 GCT_2s (t));
1487 t->next_kx_attempt = GNUNET_TIME_absolute_get ();
1488 t->kx_auth_requested = GNUNET_YES; /* queue KX_AUTH independent of estate */
1489 return;
1490 }
1491 t->kx_auth_requested = GNUNET_NO; /* clear flag */
1492 cc = ct->cc;
1496 if (GNUNET_YES == force_reply)
1498 msg->kx.flags = htonl (flags);
1499 msg->kx.cid = *GCC_get_id (cc);
1501 &msg->kx.ephemeral_key);
1503 &msg->kx.ratchet_key);
1504#if DEBUG_KX
1505 msg->kx.ephemeral_key_XXX = ax->kx_0;
1506 msg->r_ephemeral_key_XXX = ax->last_ephemeral;
1507#endif
1509 "Sending KX_AUTH message to %s with ephemeral %s on CID %s\n",
1510 GCT_2s (t),
1511 GNUNET_e2s (&msg->kx.ephemeral_key),
1512 GNUNET_sh2s (&msg->kx.cid.connection_of_tunnel));
1513
1514 /* Compute authenticator (this is the main difference to #send_kx()) */
1515 GNUNET_CRYPTO_hash (&ax->RK,
1516 sizeof(ax->RK),
1517 &msg->auth);
1518 /* Compute when to be triggered again; actual job will
1519 be scheduled via #connection_ready_cb() */
1520 t->kx_retry_delay
1521 = GNUNET_TIME_STD_BACKOFF (t->kx_retry_delay);
1522 t->next_kx_attempt
1523 = GNUNET_TIME_relative_to_absolute (t->kx_retry_delay);
1524
1525 /* Send via cc, mark it as unready */
1527
1528 /* Update state machine, unless we are already OK */
1529 if (CADET_TUNNEL_KEY_OK != t->estate)
1532 GCC_transmit (cc,
1533 env);
1535 "# KX_AUTH transmitted",
1536 1,
1537 GNUNET_NO);
1538}
1539
1540
1546static void
1548{
1549 while (NULL != ax->skipped_head)
1551 ax->skipped_head);
1552 GNUNET_assert (0 == ax->skipped);
1555}
1556
1557
1569static void
1571 const struct GNUNET_HashCode *key_result)
1572{
1573 struct CadetTunnelAsync *as;
1574 struct GNUNET_HashCode key_material[3];
1575 struct GNUNET_CRYPTO_AeadSecretKey keys[5];
1576 struct CadetTunnelAxolotl *ax;
1577 const struct GNUNET_PeerIdentity *pid;
1578 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key;
1579 const struct GNUNET_CRYPTO_EcdhePublicKey *ratchet_key;
1580 const char salt[] = "CADET Axolotl salt";
1581 int am_I_alice;
1582
1583 as = cls;
1584 ax = &as->ax;
1585 pid = &as->peer_id;
1586 ephemeral_key = &as->ephemeral_key;
1587 ratchet_key = &as->ratchet_key;
1588
1589 as->ecdh_op = NULL;
1590
1592 "Updating AX by KX with result from ECDH using ephemeral %s\n",
1593 GNUNET_e2s (ephemeral_key));
1594
1595 if (GNUNET_SYSERR == (am_I_alice = GCT_alice_or_betty (pid)))
1596 {
1597 GNUNET_break_op (0);
1598 if (as->cb)
1599 as->cb (as->cb_cls, GNUNET_SYSERR);
1600 return;
1601 }
1602
1603 if (0 == GNUNET_memcmp (&ax->DHRr,
1604 ratchet_key))
1605 {
1607 "# Ratchet key already known",
1608 1,
1609 GNUNET_NO);
1611 "Ratchet key already known. Ignoring KX.\n");
1612 if (as->cb)
1613 as->cb (as->cb_cls, GNUNET_NO);
1614 return;
1615 }
1616
1617 ax->DHRr = *ratchet_key;
1618 ax->last_ephemeral = *ephemeral_key;
1619 /* ECDH A B0 */
1620 if (GNUNET_YES == am_I_alice)
1621 {
1622 GNUNET_memcpy (&key_material[0], key_result,
1623 sizeof (*key_result));
1624 }
1625 else
1626 {
1627 GNUNET_CRYPTO_ecdh_eddsa (&ax->kx_0, /* b0 */
1628 &pid->public_key, /* A */
1629 &key_material[0]);
1630 }
1631 /* ECDH A0 B */
1632 if (GNUNET_YES == am_I_alice)
1633 {
1634 GNUNET_CRYPTO_ecdh_eddsa (&ax->kx_0, /* a0 */
1635 &pid->public_key, /* B */
1636 &key_material[1]);
1637 }
1638 else
1639 {
1640 GNUNET_memcpy (&key_material[1], key_result,
1641 sizeof (*key_result));
1642 }
1643
1644 /* ECDH A0 B0 */
1645 GNUNET_CRYPTO_ecc_ecdh (&ax->kx_0, /* a0 or b0 */
1646 ephemeral_key, /* B0 or A0 */
1647 &key_material[2]);
1648 /* KDF */
1650 keys, sizeof(keys),
1651 salt, sizeof(salt),
1652 &key_material, sizeof(key_material));
1653
1654 if (0 == memcmp (&ax->RK,
1655 &keys[0],
1656 sizeof(ax->RK)))
1657 {
1659 "Root key already known. Ignoring KX.\n");
1661 "# Root key already known",
1662 1,
1663 GNUNET_NO);
1664 if (as->cb)
1665 as->cb (as->cb_cls, GNUNET_NO);
1666 return;
1667 }
1668
1669 ax->RK = keys[0];
1670 if (GNUNET_YES == am_I_alice)
1671 {
1672 ax->HKr = keys[1];
1673 ax->NHKs = keys[2];
1674 ax->NHKr = keys[3];
1675 ax->CKr = keys[4];
1677 }
1678 else
1679 {
1680 ax->HKs = keys[1];
1681 ax->NHKr = keys[2];
1682 ax->NHKs = keys[3];
1683 ax->CKs = keys[4];
1684 ax->ratchet_flag = GNUNET_NO;
1687 ratchet_time);
1688 }
1689
1690 if (as->cb)
1691 as->cb (as->cb_cls, GNUNET_OK);
1692}
1693
1694
1700static void
1701retry_kx (void *cls)
1702{
1703 struct CadetTunnel *t = cls;
1704 struct CadetTunnelAxolotl *ax;
1705
1706 t->kx_task = NULL;
1708 "Trying to make KX progress on %s in state %s\n",
1709 GCT_2s (t),
1710 estate2s (t->estate));
1711 switch (t->estate)
1712 {
1713 case CADET_TUNNEL_KEY_UNINITIALIZED: /* first attempt */
1714 case CADET_TUNNEL_KEY_AX_SENT: /* trying again */
1715 send_kx (t,
1716 NULL,
1717 &t->ax);
1718 break;
1719
1722 /* We are responding, so only require reply
1723 if WE have a channel waiting. */
1724 if (NULL != t->unverified_ax)
1725 {
1726 /* Send AX_AUTH so we might get this one verified */
1727 ax = t->unverified_ax;
1728 }
1729 else
1730 {
1731 /* How can this be? */
1732 GNUNET_break (0);
1733 ax = &t->ax;
1734 }
1735 send_kx_auth (t,
1736 NULL,
1737 ax,
1738 (0 == GCT_count_channels (t))
1739 ? GNUNET_NO
1740 : GNUNET_YES);
1741 break;
1742
1744 /* We are responding, so only require reply
1745 if WE have a channel waiting. */
1746 if (NULL != t->unverified_ax)
1747 {
1748 /* Send AX_AUTH so we might get this one verified */
1749 ax = t->unverified_ax;
1750 }
1751 else
1752 {
1753 /* How can this be? */
1754 GNUNET_break (0);
1755 ax = &t->ax;
1756 }
1757 send_kx_auth (t,
1758 NULL,
1759 ax,
1760 (0 == GCT_count_channels (t))
1761 ? GNUNET_NO
1762 : GNUNET_YES);
1763 break;
1764
1766 /* Must have been the *other* peer asking us to
1767 respond with a KX_AUTH. */
1768 if (NULL != t->unverified_ax)
1769 {
1770 /* Sending AX_AUTH in response to AX so we might get this one verified */
1771 ax = t->unverified_ax;
1772 }
1773 else
1774 {
1775 /* Sending AX_AUTH in response to AX_AUTH */
1776 ax = &t->ax;
1777 }
1778 send_kx_auth (t,
1779 NULL,
1780 ax,
1781 GNUNET_NO);
1782 break;
1783 }
1784}
1785
1786
1793static void
1796{
1797 struct CadetTunnel *t = cls;
1798
1799 if (t->unverified_ax)
1800 *(t->unverified_ax) = t->as.ax;
1801
1803 "Continue handling KX message from %s from %s\n",
1804 GCT_2s (t),
1805 GNUNET_i2s (GCP_get_id (t->destination)));
1806
1808 if (GNUNET_OK != ret)
1809 {
1811 "# Useless KX",
1812 1,
1813 GNUNET_NO);
1814 return; /* duplicate KX, nothing to do */
1815 }
1816 /* move ahead in our state machine */
1817 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1820 else if (CADET_TUNNEL_KEY_AX_SENT == t->estate)
1823
1824 /* KX is still not done, try again our end. */
1825 if (CADET_TUNNEL_KEY_OK != t->estate)
1826 {
1827 if (NULL != t->kx_task)
1828 GNUNET_SCHEDULER_cancel (t->kx_task);
1829 t->kx_task
1831 t);
1832 }
1833}
1834
1835
1842void
1845{
1846 struct CadetTunnel *t = ct->t;
1847
1849 "# KX received",
1850 1,
1851 GNUNET_NO);
1852 if (GNUNET_YES ==
1853 GCT_alice_or_betty (GCP_get_id (t->destination)))
1854 {
1855 /* Betty/Bob is not allowed to send KX! */
1856 GNUNET_break_op (0);
1857 return;
1858 }
1860 "Received KX message from %s with ephemeral %s from %s on connection %s\n",
1861 GCT_2s (t),
1862 GNUNET_e2s (&msg->ephemeral_key),
1863 GNUNET_i2s (GCP_get_id (t->destination)),
1864 GCC_2s (ct->cc));
1865#if 1
1866 if ((0 ==
1867 memcmp (&t->ax.DHRr,
1868 &msg->ratchet_key,
1869 sizeof(msg->ratchet_key))) &&
1870 (0 ==
1871 memcmp (&t->ax.last_ephemeral,
1872 &msg->ephemeral_key,
1873 sizeof(msg->ephemeral_key))))
1874
1875 {
1877 "# Duplicate KX received",
1878 1,
1879 GNUNET_NO);
1880 send_kx_auth (t,
1881 ct,
1882 &t->ax,
1883 GNUNET_NO);
1884 return;
1885 }
1886#endif
1887 /* We only keep ONE unverified KX around, so if there is an existing one,
1888 clean it up. */
1889 if (NULL != t->unverified_ax)
1890 {
1891 if ((0 ==
1892 memcmp (&t->unverified_ax->DHRr,
1893 &msg->ratchet_key,
1894 sizeof(msg->ratchet_key))) &&
1895 (0 ==
1896 memcmp (&t->unverified_ax->last_ephemeral,
1897 &msg->ephemeral_key,
1898 sizeof(msg->ephemeral_key))))
1899 {
1901 "# Duplicate unverified KX received",
1902 1,
1903 GNUNET_NO);
1904#if 1
1905 send_kx_auth (t,
1906 ct,
1907 t->unverified_ax,
1908 GNUNET_NO);
1909 return;
1910#endif
1911 }
1912 if ((t->as.ecdh_op) &&
1913 (0 ==
1914 memcmp (&t->as.ratchet_key,
1915 &msg->ratchet_key,
1916 sizeof(msg->ratchet_key))) &&
1917 (0 ==
1918 memcmp (&t->as.ephemeral_key,
1919 &msg->ephemeral_key,
1920 sizeof(msg->ephemeral_key))))
1921 {
1923 "Waiting for previous ECDH operation\n");
1924 return;
1925 }
1927 "Dropping old unverified KX state.\n");
1929 "# Unverified KX dropped for fresh KX",
1930 1,
1931 GNUNET_NO);
1932 GNUNET_break (NULL == t->unverified_ax->skipped_head);
1933 memset (t->unverified_ax,
1934 0,
1935 sizeof(struct CadetTunnelAxolotl));
1936 }
1937 else
1938 {
1940 "Creating fresh unverified KX for %s\n",
1941 GCT_2s (t));
1943 "# Fresh KX setup",
1944 1,
1945 GNUNET_NO);
1946 t->unverified_ax = GNUNET_new (struct CadetTunnelAxolotl);
1947 }
1948 /* Set as the 'current' RK/DHRr the one we are currently using,
1949 so that the duplicate-detection logic of
1950 #update_ax_by_kx can work. */
1951 t->unverified_ax->RK = t->ax.RK;
1952 t->unverified_ax->DHRr = t->ax.DHRr;
1953 t->unverified_ax->DHRs = t->ax.DHRs;
1954 t->unverified_ax->kx_0 = t->ax.kx_0;
1955 t->unverified_attempts = 0;
1956
1957 t->as.ax = *(t->unverified_ax);
1958 GNUNET_memcpy (&t->as.peer_id, GCP_get_id (t->destination),
1959 sizeof (t->as.peer_id));
1960 GNUNET_memcpy (&t->as.ephemeral_key, &msg->ephemeral_key,
1961 sizeof (t->as.ephemeral_key));
1962 GNUNET_memcpy (&t->as.ratchet_key, &msg->ratchet_key,
1963 sizeof (t->as.ratchet_key));
1964 memset (&t->as.auth, 0, sizeof (t->as.auth));
1965 t->as.flags = 0;
1966 t->as.cb_cls = t;
1967 t->as.cb = &cont_GCT_handle_kx;
1968
1969 if (t->as.ecdh_op)
1970 {
1972 "Cancelling previous ECDH operation\n");
1973
1974 GNUNET_PILS_cancel (t->as.ecdh_op);
1975 }
1976
1977 t->as.ecdh_op = GNUNET_PILS_ecdh (pils, &msg->ephemeral_key,
1978 &update_ax_by_kx, &t->as);
1979}
1980
1981
1988static void
1991{
1992 struct CadetTunnel *t = cls;
1993 struct CadetTunnelAxolotl *ax_tmp;
1994 struct GNUNET_HashCode kx_auth;
1995
1996 ax_tmp = &t->as.ax;
1997
1999 "Continue handling KX_AUTH message from %s\n",
2000 GCT_2s (t));
2001
2002 if (GNUNET_OK != ret)
2003 {
2004 if (GNUNET_NO == ret)
2006 "# redundant KX_AUTH received",
2007 1,
2008 GNUNET_NO);
2009 else
2010 GNUNET_break (0); /* connect to self!? */
2011 return;
2012 }
2013 GNUNET_CRYPTO_hash (&ax_tmp->RK,
2014 sizeof(ax_tmp->RK),
2015 &kx_auth);
2016 if (0 != GNUNET_memcmp (&kx_auth,
2017 &t->as.auth))
2018 {
2019 /* This KX_AUTH is not using the latest KX/KX_AUTH data
2020 we transmitted to the sender, refuse it, try KX again. */
2022 "# KX_AUTH not using our last KX received (auth failure)",
2023 1,
2024 GNUNET_NO);
2026 "KX AUTH mismatch!\n");
2027#if DEBUG_KX
2028 {
2029 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
2030
2032 &ephemeral_key);
2033 if (0 != GNUNET_memcmp (&ephemeral_key,
2034 &msg->r_ephemeral_key_XXX))
2035 {
2037 "My ephemeral is %s!\n",
2038 GNUNET_e2s (&ephemeral_key));
2040 "Response is for ephemeral %s!\n",
2041 GNUNET_e2s (&msg->r_ephemeral_key_XXX));
2042 }
2043 }
2044#endif
2045 if (NULL == t->kx_task)
2046 t->kx_task
2047 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2048 &retry_kx,
2049 t);
2050 return;
2051 }
2052 /* Yep, we're good. */
2053 t->ax = *ax_tmp;
2054 if (NULL != t->unverified_ax)
2055 {
2056 /* We got some "stale" KX before, drop that. */
2057 cleanup_ax (t->unverified_ax);
2058 GNUNET_free (t->unverified_ax);
2059 t->unverified_ax = NULL;
2060 }
2061
2062 /* move ahead in our state machine */
2063 switch (t->estate)
2064 {
2067 /* Checked above, this is impossible. */
2068 GNUNET_assert (0);
2069 break;
2070
2071 case CADET_TUNNEL_KEY_AX_SENT: /* This is the normal case */
2072 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: /* both peers started KX */
2073 case CADET_TUNNEL_KEY_AX_AUTH_SENT: /* both peers now did KX_AUTH */
2076 break;
2077
2079 /* Did not expect another KX_AUTH, but so what, still acceptable.
2080 Nothing to do here. */
2081 break;
2082 }
2083 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (t->as.flags)))
2084 {
2085 send_kx_auth (t,
2086 NULL,
2087 &t->ax,
2088 GNUNET_NO);
2089 }
2090}
2091
2092
2099void
2102{
2103 struct CadetTunnel *t = ct->t;
2104
2106 "# KX_AUTH received",
2107 1,
2108 GNUNET_NO);
2109 if ((CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) ||
2110 (CADET_TUNNEL_KEY_AX_RECV == t->estate))
2111 {
2112 /* Confusing, we got a KX_AUTH before we even send our own
2113 KX. This should not happen. We'll send our own KX ASAP anyway,
2114 so let's ignore this here. */
2115 GNUNET_break_op (0);
2116 return;
2117 }
2119 "Handling KX_AUTH message from %s with ephemeral %s\n",
2120 GCT_2s (t),
2121 GNUNET_e2s (&msg->kx.ephemeral_key));
2122 if ((t->as.ecdh_op) &&
2123 (0 ==
2124 memcmp (&t->as.ratchet_key,
2125 &msg->kx.ratchet_key,
2126 sizeof(msg->kx.ratchet_key))) &&
2127 (0 ==
2128 memcmp (&t->as.ephemeral_key,
2129 &msg->kx.ephemeral_key,
2130 sizeof(msg->kx.ephemeral_key))))
2131 {
2133 "Waiting for previous ECDH operation\n");
2134 return;
2135 }
2136 /* We do everything in a copy until we've checked the authentication
2137 so we don't clobber anything we care about by accident. */
2138 t->as.ax = t->ax;
2139 GNUNET_memcpy (&t->as.peer_id, GCP_get_id (t->destination),
2140 sizeof (t->as.peer_id));
2141 GNUNET_memcpy (&t->as.ephemeral_key, &msg->kx.ephemeral_key,
2142 sizeof (t->as.ephemeral_key));
2143 GNUNET_memcpy (&t->as.ratchet_key, &msg->kx.ratchet_key,
2144 sizeof (t->as.ratchet_key));
2145 t->as.auth = msg->auth;
2146 t->as.flags = msg->kx.flags;
2147 t->as.cb_cls = t;
2148 t->as.cb = &cont_GCT_handle_kx_auth;
2149
2150 if (t->as.ecdh_op)
2151 {
2153 "Cancelling previous ECDH operation\n");
2154
2155 GNUNET_PILS_cancel (t->as.ecdh_op);
2156 }
2157
2158 /* Update 'ax' by the new key material */
2159 t->as.ecdh_op = GNUNET_PILS_ecdh (pils, &msg->kx.ephemeral_key,
2160 &update_ax_by_kx, &t->as);
2161}
2162
2163
2164/* ************************************** end core crypto ***************************** */
2165
2166
2175{
2176#define HIGH_BIT 0x08000000
2177 const struct GNUNET_PeerIdentity *my_identity;
2179 uint32_t ctn;
2180 int cmp;
2181 uint32_t highbit;
2182
2185
2188 if (0 < cmp)
2189 highbit = HIGH_BIT;
2190 else if (0 > cmp)
2191 highbit = 0;
2192 else
2193 GNUNET_assert (0); // loopback must never go here!
2194 ctn = ntohl (t->next_ctn.cn);
2195 while (NULL !=
2197 ctn | highbit))
2198 {
2199 ctn = ((ctn + 1) & (~HIGH_BIT));
2200 }
2201 t->next_ctn.cn = htonl ((ctn + 1) & (~HIGH_BIT));
2202 ret.cn = htonl (ctn | highbit);
2203 return ret;
2204}
2205
2206
2218 struct CadetChannel *ch)
2219{
2221
2222 ctn = get_next_free_ctn (t);
2223 if (NULL != t->destroy_task)
2224 {
2225 GNUNET_SCHEDULER_cancel (t->destroy_task);
2226 t->destroy_task = NULL;
2227 }
2230 ntohl (ctn.cn),
2231 ch,
2234 "Adding %s to %s with state %d\n",
2235 GCCH_2s (ch),
2236 GCT_2s (t),
2237 t->estate);
2238 switch (t->estate)
2239 {
2241 /* waiting for connection to start KX */
2242 break;
2243
2247 /* we're currently waiting for KX to complete */
2248 break;
2249
2251 /* waiting for OTHER peer to send us data,
2252 we might need to prompt more aggressively! */
2253 if (NULL == t->kx_task)
2254 t->kx_task
2255 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2256 &retry_kx,
2257 t);
2258 break;
2259
2261 /* We are ready. Tell the new channel that we are up. */
2263 break;
2264 }
2265 return ctn;
2266}
2267
2268
2275void
2277{
2278 struct CadetTunnel *t = ct->t;
2279
2280 if (GNUNET_YES == ct->is_ready)
2281 {
2282 GNUNET_CONTAINER_DLL_remove (t->connection_ready_head,
2283 t->connection_ready_tail,
2284 ct);
2285 t->num_ready_connections--;
2286 }
2287 else
2288 {
2289 GNUNET_CONTAINER_DLL_remove (t->connection_busy_head,
2290 t->connection_busy_tail,
2291 ct);
2292 t->num_busy_connections--;
2293 }
2294 GNUNET_free (ct);
2295}
2296
2297
2304static void
2306 struct CadetTConnection *ct)
2307{
2308 struct CadetTunnel *t = cls;
2309 struct CadetConnection *cc = ct->cc;
2310
2311 GNUNET_assert (ct->t == t);
2314}
2315
2316
2322static void
2324{
2325 struct CadetTunnel *t = cls;
2326 struct CadetTunnelQueueEntry *tq;
2327
2328 t->destroy_task = NULL;
2330 "Destroying idle %s\n",
2331 GCT_2s (t));
2335 t);
2336 GNUNET_assert (NULL == t->connection_ready_head);
2337 GNUNET_assert (NULL == t->connection_busy_head);
2338 while (NULL != (tq = t->tq_head))
2339 {
2340 if (NULL != tq->cont)
2341 tq->cont (tq->cont_cls,
2342 NULL);
2343 GCT_send_cancel (tq);
2344 }
2345 GCP_drop_tunnel (t->destination,
2346 t);
2348 if (NULL != t->maintain_connections_task)
2349 {
2350 GNUNET_SCHEDULER_cancel (t->maintain_connections_task);
2351 t->maintain_connections_task = NULL;
2352 }
2353 if (NULL != t->send_task)
2354 {
2355 GNUNET_SCHEDULER_cancel (t->send_task);
2356 t->send_task = NULL;
2357 }
2358 if (NULL != t->kx_task)
2359 {
2360 GNUNET_SCHEDULER_cancel (t->kx_task);
2361 t->kx_task = NULL;
2362 }
2363 if (NULL != t->as.ecdh_op)
2364 {
2365 GNUNET_PILS_cancel (t->as.ecdh_op);
2366 t->as.ecdh_op = NULL;
2367 }
2368 GNUNET_MST_destroy (t->mst);
2369 GNUNET_MQ_destroy (t->mq);
2370 if (NULL != t->unverified_ax)
2371 {
2372 cleanup_ax (t->unverified_ax);
2373 GNUNET_free (t->unverified_ax);
2374 }
2375 cleanup_ax (&t->ax);
2376 GNUNET_assert (NULL == t->destroy_task);
2377 GNUNET_free (t);
2378}
2379
2380
2388void
2390 struct CadetChannel *ch,
2392{
2394 "Removing %s from %s\n",
2395 GCCH_2s (ch),
2396 GCT_2s (t));
2399 ntohl (ctn.cn),
2400 ch));
2401 if ((0 ==
2402 GCT_count_channels (t)) &&
2403 (NULL == t->destroy_task))
2404 {
2405 t->destroy_task
2408 t);
2409 }
2410}
2411
2412
2421static int
2423 uint32_t key,
2424 void *value)
2425{
2426 struct CadetChannel *ch = value;
2427
2429 NULL);
2430 return GNUNET_OK;
2431}
2432
2433
2439void
2441{
2445 t);
2446 GNUNET_assert (0 ==
2448 if (NULL != t->destroy_task)
2449 {
2450 GNUNET_SCHEDULER_cancel (t->destroy_task);
2451 t->destroy_task = NULL;
2452 }
2453 destroy_tunnel (t);
2454}
2455
2456
2464static void
2466 struct CadetTConnection *ct)
2467{
2468 struct CadetTunnelQueueEntry *tq;
2469
2471 tq = t->tq_head;
2472 if (NULL == tq)
2473 {
2474 /* no messages pending right now */
2476 "Not sending payload of %s on ready %s (nothing pending)\n",
2477 GCT_2s (t),
2478 GCC_2s (ct->cc));
2479 return;
2480 }
2481 /* ready to send message 'tq' on tunnel 'ct' */
2482 GNUNET_assert (t == tq->t);
2484 t->tq_tail,
2485 tq);
2486 if (NULL != tq->cid)
2487 *tq->cid = *GCC_get_id (ct->cc);
2490 "Sending payload of %s on %s\n",
2491 GCT_2s (t),
2492 GCC_2s (ct->cc));
2493 GCC_transmit (ct->cc,
2494 tq->env);
2495 if (NULL != tq->cont)
2496 tq->cont (tq->cont_cls,
2497 GCC_get_id (ct->cc));
2498 GNUNET_free (tq);
2499}
2500
2501
2510static void
2512 int is_ready)
2513{
2514 struct CadetTConnection *ct = cls;
2515 struct CadetTunnel *t = ct->t;
2516
2517 if (GNUNET_NO == is_ready)
2518 {
2520 "%s no longer ready for %s\n",
2521 GCC_2s (ct->cc),
2522 GCT_2s (t));
2524 return;
2525 }
2527 GNUNET_CONTAINER_DLL_remove (t->connection_busy_head,
2528 t->connection_busy_tail,
2529 ct);
2530 GNUNET_assert (0 < t->num_busy_connections);
2531 t->num_busy_connections--;
2532 ct->is_ready = GNUNET_YES;
2533 GNUNET_CONTAINER_DLL_insert_tail (t->connection_ready_head,
2534 t->connection_ready_tail,
2535 ct);
2536 t->num_ready_connections++;
2537
2539 "%s now ready for %s in state %s\n",
2540 GCC_2s (ct->cc),
2541 GCT_2s (t),
2542 estate2s (t->estate));
2543 switch (t->estate)
2544 {
2547 "Do not begin KX for %s if WE have no channels waiting. Retrying after %llu\n",
2548 GCT_2s (t),
2549 (unsigned long long) GNUNET_TIME_absolute_get_remaining (
2550 t->next_kx_attempt).rel_value_us);
2551 /* Do not begin KX if WE have no channels waiting! */
2553 t->next_kx_attempt).rel_value_us)
2554 return; /* wait for timeout before retrying */
2555 /* We are uninitialized, just transmit immediately,
2556 without undue delay. */
2557
2559 "Why for %s \n",
2560 GCT_2s (t));
2561
2562 if (NULL != t->kx_task)
2563 {
2564 GNUNET_SCHEDULER_cancel (t->kx_task);
2565 t->kx_task = NULL;
2566 }
2567 send_kx (t,
2568 ct,
2569 &t->ax);
2570 if ((0 ==
2571 GCT_count_channels (t)) &&
2572 (NULL == t->destroy_task))
2573 {
2574 t->destroy_task
2577 t);
2578 }
2579 break;
2580
2585 /* we're currently waiting for KX to complete, schedule job */
2586 if (NULL == t->kx_task)
2587 t->kx_task
2588 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2589 &retry_kx,
2590 t);
2591 break;
2592
2594 if (GNUNET_YES == t->kx_auth_requested)
2595 {
2597 t->next_kx_attempt).rel_value_us)
2598 return; /* wait for timeout */
2599 if (NULL != t->kx_task)
2600 {
2601 GNUNET_SCHEDULER_cancel (t->kx_task);
2602 t->kx_task = NULL;
2603 }
2604 send_kx_auth (t,
2605 ct,
2606 &t->ax,
2607 GNUNET_NO);
2608 return;
2609 }
2611 ct);
2612 break;
2613 }
2614}
2615
2616
2625static void
2627{
2628 struct CadetTunnel *t = cls;
2629 struct CadetTConnection *ct;
2630
2631 t->send_task = NULL;
2632 if (NULL == t->tq_head)
2633 return; /* no messages pending right now */
2634 ct = get_ready_connection (t);
2635 if (NULL == ct)
2636 return; /* no connections ready */
2638 ct);
2639}
2640
2641
2689
2690
2698static void
2700 struct CadetTConnection *ct)
2701{
2702 struct EvaluationSummary *es = cls;
2703 struct CadetConnection *cc = ct->cc;
2704 unsigned int ct_length;
2705 struct CadetPeerPath *ps;
2706 const struct CadetConnectionMetrics *metrics;
2707 GNUNET_CONTAINER_HeapCostType ct_desirability;
2708 struct GNUNET_TIME_Relative uptime;
2709 struct GNUNET_TIME_Relative last_use;
2710 double score;
2711 double success_rate;
2712
2713 ps = GCC_get_path (cc,
2714 &ct_length);
2716 "Evaluating path %s of existing %s\n",
2717 GCPP_2s (ps),
2718 GCC_2s (cc));
2719 if (ps == es->path)
2720 {
2722 "Ignoring duplicate path %s.\n",
2723 GCPP_2s (es->path));
2724 es->duplicate = GNUNET_YES;
2725 return;
2726 }
2727 if (NULL != es->path)
2728 {
2729 int duplicate = GNUNET_YES;
2730
2731 for (unsigned int i = 0; i < ct_length; i++)
2732 {
2735 i) !=
2737 i))
2738 {
2739 duplicate = GNUNET_NO;
2740 break;
2741 }
2742 }
2743 if (GNUNET_YES == duplicate)
2744 {
2746 "Ignoring overlapping path %s.\n",
2747 GCPP_2s (es->path));
2748 es->duplicate = GNUNET_YES;
2749 return;
2750 }
2751 else
2752 {
2754 "Known path %s differs from proposed path\n",
2755 GCPP_2s (ps));
2756 }
2757 }
2758
2759 ct_desirability = GCPP_get_desirability (ps);
2760 metrics = GCC_get_metrics (cc);
2761 uptime = GNUNET_TIME_absolute_get_duration (metrics->age);
2762 last_use = GNUNET_TIME_absolute_get_duration (metrics->last_use);
2763 /* We add 1.0 here to avoid division by zero. */
2764 success_rate = (metrics->num_acked_transmissions + 1.0)
2765 / (metrics->num_successes + 1.0);
2766 score
2767 = ct_desirability
2768 + 100.0 / (1.0 + ct_length) /* longer paths = better */
2769 + sqrt (uptime.rel_value_us / 60000000LL) /* larger uptime = better */
2770 - last_use.rel_value_us / 1000L; /* longer idle = worse */
2771 score *= success_rate; /* weigh overall by success rate */
2772
2773 if ((NULL == es->worst) ||
2774 (score < es->worst_score))
2775 {
2776 es->worst = ct;
2777 es->worst_score = score;
2778 }
2779 es->min_length = GNUNET_MIN (es->min_length,
2780 ct_length);
2781 es->max_length = GNUNET_MAX (es->max_length,
2782 ct_length);
2783 es->min_desire = GNUNET_MIN (es->min_desire,
2784 ct_desirability);
2785 es->max_desire = GNUNET_MAX (es->max_desire,
2786 ct_desirability);
2787}
2788
2789
2799static int
2801 struct CadetPeerPath *path,
2802 unsigned int off)
2803{
2804 struct CadetTunnel *t = cls;
2805 struct EvaluationSummary es;
2806 struct CadetTConnection *ct;
2807
2808 GNUNET_assert (off < GCPP_get_length (path));
2810 off) == t->destination);
2811 es.min_length = UINT_MAX;
2812 es.max_length = 0;
2813 es.max_desire = 0;
2814 es.min_desire = UINT64_MAX;
2815 es.path = path;
2816 es.duplicate = GNUNET_NO;
2817 es.worst = NULL;
2818
2819 /* Compute evaluation summary over existing connections. */
2821 "Evaluating proposed path %s for target %s\n",
2822 GCPP_2s (path),
2823 GCT_2s (t));
2824 /* FIXME: suspect this does not ACTUALLY iterate
2825 over all existing paths, otherwise dup detection
2826 should work!!! */
2829 &es);
2830 if (GNUNET_YES == es.duplicate)
2831 return GNUNET_YES;
2832
2833 /* FIXME: not sure we should really just count
2834 'num_connections' here, as they may all have
2835 consistently failed to connect. */
2836
2837 /* We iterate by increasing path length; if we have enough paths and
2838 this one is more than twice as long than what we are currently
2839 using, then ignore all of these super-long ones! */
2841 (es.min_length * 2 < off) &&
2842 (es.max_length < off))
2843 {
2845 "Ignoring paths of length %u, they are way too long.\n",
2846 es.min_length * 2);
2847 return GNUNET_NO;
2848 }
2849 /* If we have enough paths and this one looks no better, ignore it. */
2851 (es.min_length < GCPP_get_length (path)) &&
2852 (es.min_desire > GCPP_get_desirability (path)) &&
2853 (es.max_length < off))
2854 {
2856 "Ignoring path (%u/%llu) to %s, got something better already.\n",
2857 GCPP_get_length (path),
2858 (unsigned long long) GCPP_get_desirability (path),
2859 GCP_2s (t->destination));
2860 return GNUNET_YES;
2861 }
2862
2863 /* Path is interesting (better by some metric, or we don't have
2864 enough paths yet). */
2865 ct = GNUNET_new (struct CadetTConnection);
2867 ct->t = t;
2868 ct->cc = GCC_create (t->destination,
2869 path,
2870 off,
2871 ct,
2873 ct);
2874
2875 /* FIXME: schedule job to kill connection (and path?) if it takes
2876 too long to get ready! (And track performance data on how long
2877 other connections took with the tunnel!)
2878 => Note: to be done within 'connection'-logic! */
2879 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head,
2880 t->connection_busy_tail,
2881 ct);
2882 t->num_busy_connections++;
2884 "Found interesting path %s for %s, created %s\n",
2885 GCPP_2s (path),
2886 GCT_2s (t),
2887 GCC_2s (ct->cc));
2888 return GNUNET_YES;
2889}
2890
2891
2905static void
2907{
2908 struct CadetTunnel *t = cls;
2909 struct GNUNET_TIME_Relative delay;
2910 struct EvaluationSummary es;
2911
2912 t->maintain_connections_task = NULL;
2914 "Performing connection maintenance for %s.\n",
2915 GCT_2s (t));
2916
2917 es.min_length = UINT_MAX;
2918 es.max_length = 0;
2919 es.max_desire = 0;
2920 es.min_desire = UINT64_MAX;
2921 es.path = NULL;
2922 es.worst = NULL;
2923 es.duplicate = GNUNET_NO;
2926 &es);
2927 if ((NULL != es.worst) &&
2929 {
2930 /* Clear out worst-performing connection 'es.worst'. */
2932 es.worst);
2933 }
2934
2935 /* Consider additional paths */
2936 (void) GCP_iterate_paths (t->destination,
2938 t);
2939
2940 /* FIXME: calculate when to try again based on how well we are doing;
2941 in particular, if we have to few connections, we might be able
2942 to do without this (as PATHS should tell us whenever a new path
2943 is available instantly; however, need to make sure this job is
2944 restarted after that happens).
2945 Furthermore, if the paths we do know are in a reasonably narrow
2946 quality band and are plentyful, we might also consider us stabilized
2947 and then reduce the frequency accordingly. */delay = GNUNET_TIME_UNIT_MINUTES;
2948 t->maintain_connections_task
2951 t);
2952}
2953
2954
2955void
2957 struct CadetPeerPath *p,
2958 unsigned int off)
2959{
2961 "Considering %s for %s (offset %u)\n",
2962 GCPP_2s (p),
2963 GCT_2s (t),
2964 off);
2965 (void) consider_path_cb (t,
2966 p,
2967 off);
2968}
2969
2970
2977static void
2979 const struct GNUNET_MessageHeader *msg)
2980{
2981 struct CadetTunnel *t = cls;
2982
2984 "Received KEEPALIVE on %s\n",
2985 GCT_2s (t));
2987 "# keepalives received",
2988 1,
2989 GNUNET_NO);
2990}
2991
2992
3000static int
3003{
3004 return GNUNET_OK;
3005}
3006
3007
3015static void
3018{
3019 struct CadetTunnel *t = cls;
3020 struct CadetChannel *ch;
3021
3022 ch = lookup_channel (t,
3023 msg->ctn);
3024 if (NULL == ch)
3025 {
3026 /* We don't know about such a channel, might have been destroyed on our
3027 end in the meantime, or never existed. Send back a DESTROY. */
3029 "Received %u bytes of application data for unknown channel %u, sending DESTROY\n",
3030 (unsigned int) (ntohs (msg->header.size) - sizeof(*msg)),
3031 ntohl (msg->ctn.cn));
3033 msg->ctn);
3034 return;
3035 }
3037 GCC_get_id (t->current_ct->cc),
3038 msg);
3039}
3040
3041
3050static void
3052 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
3053{
3054 struct CadetTunnel *t = cls;
3055 struct CadetChannel *ch;
3056
3057 ch = lookup_channel (t,
3058 ack->ctn);
3059 if (NULL == ch)
3060 {
3061 /* We don't know about such a channel, might have been destroyed on our
3062 end in the meantime, or never existed. Send back a DESTROY. */
3064 "Received DATA_ACK for unknown channel %u, sending DESTROY\n",
3065 ntohl (ack->ctn.cn));
3067 ack->ctn);
3068 return;
3069 }
3071 GCC_get_id (t->current_ct->cc),
3072 ack);
3073}
3074
3075
3083static void
3085 const struct
3087{
3088 struct CadetTunnel *t = cls;
3089 struct CadetChannel *ch;
3090
3092 ntohl (copen->ctn.cn));
3093 if (NULL != ch)
3094 {
3096 "Received duplicate channel CHANNEL_OPEN on h_port %s from %s (%s), resending ACK\n",
3097 GNUNET_h2s (&copen->h_port),
3098 GCT_2s (t),
3099 GCCH_2s (ch));
3101 GCC_get_id (t->current_ct->cc));
3102 return;
3103 }
3105 "Received CHANNEL_OPEN on h_port %s from %s\n",
3106 GNUNET_h2s (&copen->h_port),
3107 GCT_2s (t));
3109 copen->ctn,
3110 &copen->h_port,
3111 ntohl (copen->opt));
3112 if (NULL != t->destroy_task)
3113 {
3114 GNUNET_SCHEDULER_cancel (t->destroy_task);
3115 t->destroy_task = NULL;
3116 }
3119 ntohl (copen->ctn.cn),
3120 ch,
3122}
3123
3124
3131void
3134{
3136
3138 "Sending DESTROY message for channel ID %u\n",
3139 ntohl (ctn.cn));
3140 msg.header.size = htons (sizeof(msg));
3142 msg.reserved = htonl (0);
3143 msg.ctn = ctn;
3144 GCT_send (t,
3145 &msg.header,
3146 NULL,
3147 NULL,
3148 &ctn);
3149}
3150
3151
3160static void
3162 const struct
3164{
3165 struct CadetTunnel *t = cls;
3166 struct CadetChannel *ch;
3167
3168 ch = lookup_channel (t,
3169 cm->ctn);
3170 if (NULL == ch)
3171 {
3172 /* We don't know about such a channel, might have been destroyed on our
3173 end in the meantime, or never existed. Send back a DESTROY. */
3175 "Received channel OPEN_ACK for unknown channel %u, sending DESTROY\n",
3176 ntohl (cm->ctn.cn));
3178 cm->ctn);
3179 return;
3180 }
3182 "Received channel OPEN_ACK on channel %s from %s\n",
3183 GCCH_2s (ch),
3184 GCT_2s (t));
3186 GCC_get_id (t->current_ct->cc),
3187 &cm->port);
3188}
3189
3190
3198static void
3200 const struct
3202{
3203 struct CadetTunnel *t = cls;
3204 struct CadetChannel *ch;
3205
3206 ch = lookup_channel (t,
3207 cm->ctn);
3208 if (NULL == ch)
3209 {
3210 /* We don't know about such a channel, might have been destroyed on our
3211 end in the meantime, or never existed. */
3213 "Received channel DESTROY for unknown channel %u. Ignoring.\n",
3214 ntohl (cm->ctn.cn));
3215 return;
3216 }
3218 "Received channel DESTROY on %s from %s\n",
3219 GCCH_2s (ch),
3220 GCT_2s (t));
3222 GCC_get_id (t->current_ct->cc));
3223}
3224
3225
3236static int
3238 const struct GNUNET_MessageHeader *msg)
3239{
3240 struct CadetTunnel *t = cls;
3241
3242 GNUNET_assert (NULL != t->current_ct);
3244 msg);
3245 return GNUNET_OK;
3246}
3247
3248
3256static void
3258 enum GNUNET_MQ_Error error)
3259{
3260 GNUNET_break_op (0);
3261}
3262
3263
3271struct CadetTunnel *
3273{
3274 struct CadetTunnel *t = GNUNET_new (struct CadetTunnel);
3276 GNUNET_MQ_hd_fixed_size (plaintext_keepalive,
3278 struct GNUNET_MessageHeader,
3279 t),
3280 GNUNET_MQ_hd_var_size (plaintext_data,
3283 t),
3284 GNUNET_MQ_hd_fixed_size (plaintext_data_ack,
3287 t),
3288 GNUNET_MQ_hd_fixed_size (plaintext_channel_open,
3291 t),
3292 GNUNET_MQ_hd_fixed_size (plaintext_channel_open_ack,
3295 t),
3296 GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy,
3299 t),
3301 };
3302
3303 t->kx_retry_delay = INITIAL_KX_RETRY_DELAY;
3304 new_ephemeral (&t->ax);
3306 t->destination = destination;
3308 t->maintain_connections_task
3310 t);
3311 t->mq = GNUNET_MQ_queue_for_callbacks (NULL,
3312 NULL,
3313 NULL,
3314 NULL,
3315 handlers,
3317 t);
3319 t);
3320 return t;
3321}
3322
3323
3324int
3326 const struct
3328 struct CadetPeerPath *path)
3329{
3330 struct CadetTConnection *ct;
3331
3332 ct = GNUNET_new (struct CadetTConnection);
3334 ct->t = t;
3335 ct->cc = GCC_create_inbound (t->destination,
3336 path,
3337 ct,
3338 cid,
3340 ct);
3341 if (NULL == ct->cc)
3342 {
3344 "%s refused inbound %s (duplicate)\n",
3345 GCT_2s (t),
3346 GCC_2s (ct->cc));
3347 GNUNET_free (ct);
3348 return GNUNET_SYSERR;
3349 }
3350 /* FIXME: schedule job to kill connection (and path?) if it takes
3351 too long to get ready! (And track performance data on how long
3352 other connections took with the tunnel!)
3353 => Note: to be done within 'connection'-logic! */
3354 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head,
3355 t->connection_busy_tail,
3356 ct);
3357 t->num_busy_connections++;
3359 "%s has new %s\n",
3360 GCT_2s (t),
3361 GCC_2s (ct->cc));
3362 return GNUNET_OK;
3363}
3364
3365
3372void
3375{
3376 struct CadetTunnel *t = ct->t;
3377 uint16_t size = ntohs (msg->header.size);
3378 char cbuf[size] GNUNET_ALIGN;
3379 ssize_t decrypted_size;
3380
3382 "%s received %u bytes of encrypted data in state %d\n",
3383 GCT_2s (t),
3384 (unsigned int) size,
3385 t->estate);
3386
3387 switch (t->estate)
3388 {
3391 /* We did not even SEND our KX, how can the other peer
3392 send us encrypted data? Must have been that we went
3393 down and the other peer still things we are up.
3394 Let's send it KX back. */
3396 "# received encrypted without any KX",
3397 1,
3398 GNUNET_NO);
3399 if (NULL != t->kx_task)
3400 {
3401 GNUNET_SCHEDULER_cancel (t->kx_task);
3402 t->kx_task = NULL;
3403 }
3404 send_kx (t,
3405 ct,
3406 &t->ax);
3407 return;
3408
3410 /* We send KX, and other peer send KX to us at the same time.
3411 Neither KX is AUTH'ed, so let's try KX_AUTH this time. */
3413 "# received encrypted without KX_AUTH",
3414 1,
3415 GNUNET_NO);
3416 if (NULL != t->kx_task)
3417 {
3418 GNUNET_SCHEDULER_cancel (t->kx_task);
3419 t->kx_task = NULL;
3420 }
3421 send_kx_auth (t,
3422 ct,
3423 &t->ax,
3424 GNUNET_YES);
3425 return;
3426
3428 /* We did not get the KX of the other peer, but that
3429 might have been lost. Send our KX again immediately. */
3431 "# received encrypted without KX",
3432 1,
3433 GNUNET_NO);
3434 if (NULL != t->kx_task)
3435 {
3436 GNUNET_SCHEDULER_cancel (t->kx_task);
3437 t->kx_task = NULL;
3438 }
3439 send_kx (t,
3440 ct,
3441 &t->ax);
3442 return;
3443
3445 /* Great, first payload, we might graduate to OK! */
3447 /* We are up and running, all good. */
3448 break;
3449 }
3450
3451 decrypted_size = -1;
3452 if (CADET_TUNNEL_KEY_OK == t->estate)
3453 {
3454 /* We have well-established key material available,
3455 try that. (This is the common case.) */
3456 decrypted_size = t_ax_decrypt_and_validate (&t->ax,
3457 cbuf,
3458 msg,
3459 size);
3460 }
3461
3462 if ((-1 == decrypted_size) &&
3463 (NULL != t->unverified_ax))
3464 {
3465 /* We have un-authenticated KX material available. We should try
3466 this as a back-up option, in case the sender crashed and
3467 switched keys. */
3468 decrypted_size = t_ax_decrypt_and_validate (t->unverified_ax,
3469 cbuf,
3470 msg,
3471 size);
3472 if (-1 != decrypted_size)
3473 {
3474 /* It worked! Treat this as authentication of the AX data! */
3475 cleanup_ax (&t->ax);
3476 t->ax = *t->unverified_ax;
3477 GNUNET_free (t->unverified_ax);
3478 t->unverified_ax = NULL;
3479 }
3480 if (CADET_TUNNEL_KEY_AX_AUTH_SENT == t->estate)
3481 {
3482 /* First time it worked, move tunnel into production! */
3485 if (NULL != t->send_task)
3486 GNUNET_SCHEDULER_cancel (t->send_task);
3488 t);
3489 }
3490 }
3491 if (NULL != t->unverified_ax)
3492 {
3493 /* We had unverified KX material that was useless; so increment
3494 counter and eventually move to ignore it. Note that we even do
3495 this increment if we successfully decrypted with the old KX
3496 material and thus didn't even both with the new one. This is
3497 the ideal case, as a malicious injection of bogus KX data
3498 basically only causes us to increment a counter a few times. */t->unverified_attempts++;
3500 "Failed to decrypt message with unverified KX data %u times\n",
3501 t->unverified_attempts);
3502 if (t->unverified_attempts > MAX_UNVERIFIED_ATTEMPTS)
3503 {
3504 cleanup_ax (t->unverified_ax);
3505 GNUNET_free (t->unverified_ax);
3506 t->unverified_ax = NULL;
3507 }
3508 }
3509
3510 if (-1 == decrypted_size)
3511 {
3512 /* Decryption failed for good, complain. */
3514 "%s failed to decrypt and validate encrypted data, retrying KX\n",
3515 GCT_2s (t));
3517 "# unable to decrypt",
3518 1,
3519 GNUNET_NO);
3520 if (NULL != t->kx_task)
3521 {
3522 GNUNET_SCHEDULER_cancel (t->kx_task);
3523 t->kx_task = NULL;
3524 }
3525 send_kx (t,
3526 ct,
3527 &t->ax);
3528 return;
3529 }
3531 "# decrypted bytes",
3532 decrypted_size,
3533 GNUNET_NO);
3534
3535 /* The MST will ultimately call #handle_decrypted() on each message. */
3536 t->current_ct = ct;
3539 cbuf,
3540 decrypted_size,
3541 GNUNET_YES,
3542 GNUNET_NO));
3543 t->current_ct = NULL;
3544}
3545
3546
3547struct CadetTunnelQueueEntry *
3549 const struct GNUNET_MessageHeader *message,
3551 void *cont_cls,
3553{
3554 struct CadetTunnelQueueEntry *tq;
3555 uint16_t message_size;
3556 uint16_t payload_size;
3557 struct GNUNET_MQ_Envelope *env;
3559 struct CadetChannel *ch;
3560
3561 if (NULL != ctn)
3562 {
3563 ch = lookup_channel (t,
3564 *ctn);
3565 if ((NULL != ch) && GCCH_is_type_to_drop (ch, message))
3566 {
3567 GNUNET_break (0);
3568 return NULL;
3569 }
3570 }
3571
3572 if (CADET_TUNNEL_KEY_OK != t->estate)
3573 {
3574 GNUNET_break (0);
3575 return NULL;
3576 }
3577 message_size = ntohs (message->size);
3578 payload_size = message_size + sizeof (struct GNUNET_CRYPTO_AeadMac);
3580 "Encrypting %u bytes for %s\n",
3581 (unsigned int) message_size,
3582 GCT_2s (t));
3583 env = GNUNET_MQ_msg_extra (ax_msg,
3584 payload_size,
3586 t_ax_encrypt (&t->ax,
3587 &ax_msg[1],
3588 message,
3589 message_size);
3591 "# encrypted bytes",
3592 payload_size,
3593 GNUNET_NO);
3594 ax_msg->ax_header.Ns = htonl (t->ax.Ns++);
3595 ax_msg->ax_header.PNs = htonl (t->ax.PNs);
3596 /* FIXME: we should do this once, not once per message;
3597 this is a point multiplication, and DHRs does not
3598 change all the time. */
3600 &ax_msg->ax_header.DHRs);
3601 t_h_encrypt (&t->ax,
3602 ax_msg,
3603 payload_size,
3604 (unsigned char*) &ax_msg[1]);
3605
3606 tq = GNUNET_malloc (sizeof(*tq));
3607 tq->t = t;
3608 tq->env = env;
3609 tq->cid = &ax_msg->cid; /* will initialize 'ax_msg->cid' once we know the connection */
3610 tq->cont = cont;
3611 tq->cont_cls = cont_cls;
3613 t->tq_tail,
3614 tq);
3615 if (NULL != t->send_task)
3616 GNUNET_SCHEDULER_cancel (t->send_task);
3617 t->send_task
3619 t);
3620 return tq;
3621}
3622
3623
3624void
3626{
3627 struct CadetTunnel *t = tq->t;
3628
3630 t->tq_tail,
3631 tq);
3632 GNUNET_MQ_discard (tq->env);
3633 GNUNET_free (tq);
3634}
3635
3636
3644void
3647 void *iter_cls)
3648{
3649 struct CadetTConnection *n;
3650
3651 for (struct CadetTConnection *ct = t->connection_ready_head;
3652 NULL != ct;
3653 ct = n)
3654 {
3655 n = ct->next;
3656 iter (iter_cls,
3657 ct);
3658 }
3659 for (struct CadetTConnection *ct = t->connection_busy_head;
3660 NULL != ct;
3661 ct = n)
3662 {
3663 n = ct->next;
3664 iter (iter_cls,
3665 ct);
3666 }
3667}
3668
3669
3685
3686
3695static int
3697 uint32_t key,
3698 void *value)
3699{
3700 struct ChanIterCls *ctx = cls;
3701 struct CadetChannel *ch = value;
3702
3703 ctx->iter (ctx->iter_cls,
3704 ch);
3705 return GNUNET_OK;
3706}
3707
3708
3716void
3719 void *iter_cls)
3720{
3721 struct ChanIterCls ctx;
3722
3723 ctx.iter = iter;
3724 ctx.iter_cls = iter_cls;
3727 &ctx);
3728}
3729
3730
3739static int
3740debug_channel (void *cls,
3741 uint32_t key,
3742 void *value)
3743{
3744 const enum GNUNET_ErrorType *level = cls;
3745 struct CadetChannel *ch = value;
3746
3747 GCCH_debug (ch, *level);
3748 return GNUNET_OK;
3749}
3750
3751
3752#define LOG2(level, ...) GNUNET_log_from_nocheck (level, "cadet-tun", \
3753 __VA_ARGS__)
3754
3755
3762void
3763GCT_debug (const struct CadetTunnel *t,
3764 enum GNUNET_ErrorType level)
3765{
3766#if ! defined(GNUNET_CULL_LOGGING)
3767 struct CadetTConnection *iter_c;
3768 int do_log;
3769
3771 "cadet-tun",
3772 __FILE__, __FUNCTION__, __LINE__);
3773 if (0 == do_log)
3774 return;
3775
3776 LOG2 (level,
3777 "TTT TUNNEL TOWARDS %s in estate %s tq_len: %u #cons: %u\n",
3778 GCT_2s (t),
3779 estate2s (t->estate),
3780 t->tq_len,
3782 LOG2 (level,
3783 "TTT channels:\n");
3786 &level);
3787 LOG2 (level,
3788 "TTT connections:\n");
3789 for (iter_c = t->connection_ready_head; NULL != iter_c; iter_c = iter_c->next)
3790 GCC_debug (iter_c->cc,
3791 level);
3792 for (iter_c = t->connection_busy_head; NULL != iter_c; iter_c = iter_c->next)
3793 GCC_debug (iter_c->cc,
3794 level);
3795
3796 LOG2 (level,
3797 "TTT TUNNEL END\n");
3798#endif
3799}
3800
3801
3802/* end of gnunet-service-cadet_tunnels.c */
struct GNUNET_MQ_MessageHandlers handlers[]
Definition 003.c:1
struct GNUNET_MessageHeader * msg
Definition 005.c:2
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
P2P messages used by CADET.
GNUNET_CADET_KX_Flags
Flags to be used in GNUNET_CADET_KX.
@ GNUNET_CADET_KX_FLAG_NONE
Should the peer reply with its KX details?
@ GNUNET_CADET_KX_FLAG_FORCE_REPLY
The peer should reply with its KX details?
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CADET_Channel * ch
Channel handle.
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_FS_Handle * ctx
static GstElement * source
Appsrc instance into which we write data for the pipeline.
static struct GNUNET_SCHEDULER_Task * t
Main task.
static int state
The current state of the parser.
static char * res
Currently read line or NULL on EOF.
static char * value
Value of the record to add/remove.
static struct GNUNET_PILS_Handle * pils
Handle to PILS.
Definition gnunet-pils.c:44
static struct GNUNET_CRYPTO_PowSalt salt
Salt for PoW calculations.
int shutting_down
Signal that shutdown is happening: prevent recovery measures.
struct GNUNET_TIME_Relative ratchet_time
How long until we trigger a ratched advance due to time.
void GCCH_handle_channel_plaintext_data_ack(struct CadetChannel *ch, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, const struct GNUNET_CADET_ChannelDataAckMessage *ack)
We got an acknowledgement for payload data for a channel.
struct CadetChannel * GCCH_channel_incoming_new(struct CadetTunnel *t, struct GNUNET_CADET_ChannelTunnelNumber ctn, const struct GNUNET_HashCode *h_port, uint32_t options)
Create a new channel based on a request coming in over the network.
const char * GCCH_2s(const struct CadetChannel *ch)
Get the static string for identification of the channel.
void GCCH_handle_duplicate_open(struct CadetChannel *ch, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
We got a GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for this channel.
void GCCH_tunnel_up(struct CadetChannel *ch)
Function called once and only once after a channel was bound to its tunnel via GCT_add_channel() is r...
void GCCH_debug(struct CadetChannel *ch, enum GNUNET_ErrorType level)
Log channel info.
void GCCH_handle_channel_plaintext_data(struct CadetChannel *ch, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, const struct GNUNET_CADET_ChannelAppDataMessage *msg)
We got payload data for a channel.
void GCCH_handle_remote_destroy(struct CadetChannel *ch, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
Destroy channel, based on the other peer closing the connection.
void GCCH_handle_channel_open_ack(struct CadetChannel *ch, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, const struct GNUNET_HashCode *port)
We got an acknowledgement for the creation of the channel (the port is open on the other side).
int GCCH_is_type_to_drop(struct CadetChannel *ch, const struct GNUNET_MessageHeader *message)
Check if type of message is the one to drop.
GNUnet CADET service with encryption.
struct CadetPeerPath * GCC_get_path(struct CadetConnection *cc, unsigned int *off)
Obtain the path used by this connection.
const char * GCC_2s(const struct CadetConnection *cc)
Get a (static) string for a connection.
struct CadetConnection * GCC_create_inbound(struct CadetPeer *destination, struct CadetPeerPath *path, struct CadetTConnection *ct, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, GCC_ReadyCallback ready_cb, void *ready_cb_cls)
Create a connection to destination via path and notify cb whenever we are ready for more data.
void GCC_destroy_without_tunnel(struct CadetConnection *cc)
Destroy a connection, called if the tunnel association with the connection was already broken,...
struct CadetConnection * GCC_create(struct CadetPeer *destination, struct CadetPeerPath *path, unsigned int off, struct CadetTConnection *ct, GCC_ReadyCallback ready_cb, void *ready_cb_cls)
Create a connection to destination via path and notify cb whenever we are ready for more data.
void GCC_transmit(struct CadetConnection *cc, struct GNUNET_MQ_Envelope *env)
Transmit message msg via connection cc.
void GCC_debug(struct CadetConnection *cc, enum GNUNET_ErrorType level)
Log connection info.
const struct CadetConnectionMetrics * GCC_get_metrics(struct CadetConnection *cc)
Obtain performance metrics from cc.
const struct GNUNET_CADET_ConnectionTunnelIdentifier * GCC_get_id(struct CadetConnection *cc)
Obtain unique ID for the connection.
A connection is a live end-to-end messaging mechanism where the peers are identified by a path and kn...
const char * GCPP_2s(struct CadetPeerPath *path)
Convert a path to a human-readable string.
unsigned int GCPP_get_length(struct CadetPeerPath *path)
Return the length of the path.
GNUNET_CONTAINER_HeapCostType GCPP_get_desirability(const struct CadetPeerPath *path)
Return how much we like keeping the path.
struct CadetPeer * GCPP_get_peer_at_offset(struct CadetPeerPath *path, unsigned int off)
Obtain the peer at offset off in path.
const struct GNUNET_PeerIdentity * GCP_get_id(struct CadetPeer *cp)
Obtain the peer identity for a struct CadetPeer.
void GCP_drop_tunnel(struct CadetPeer *cp, struct CadetTunnel *t)
The tunnel to the given peer no longer exists, remove it from our data structures,...
const char * GCP_2s(const struct CadetPeer *cp)
Get the static string for a peer ID.
unsigned int GCP_iterate_paths(struct CadetPeer *cp, GCP_PathIterator callback, void *callback_cls)
Iterate over the paths to a peer.
Information we track per peer.
static void cleanup_ax(struct CadetTunnelAxolotl *ax)
Cleanup state used by ax.
void GCT_handle_kx(struct CadetTConnection *ct, const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
Continue to handle KX message.
static int consider_path_cb(void *cls, struct CadetPeerPath *path, unsigned int off)
Consider using the path p for the tunnel t.
void GCT_remove_channel(struct CadetTunnel *t, struct CadetChannel *ch, struct GNUNET_CADET_ChannelTunnelNumber ctn)
Remove a channel from a tunnel.
static struct GNUNET_CADET_ChannelTunnelNumber get_next_free_ctn(struct CadetTunnel *t)
Compute the next free channel tunnel number for this tunnel.
static struct CadetChannel * lookup_channel(struct CadetTunnel *t, struct GNUNET_CADET_ChannelTunnelNumber ctn)
Lookup a channel by its ctn.
static int notify_tunnel_up_cb(void *cls, uint32_t key, void *value)
Our tunnel became ready for the first time, notify channels that have been waiting.
struct CadetPeer * GCT_get_destination(struct CadetTunnel *t)
Return the peer to which this tunnel goes.
void(* CadetTunnelAxolotlCallback)(void *cls, enum GNUNET_GenericReturnValue res)
Signature of the follow up function from an udate AX by KX.
void GCT_send_channel_destroy(struct CadetTunnel *t, struct GNUNET_CADET_ChannelTunnelNumber ctn)
Send a DESTROY message via the tunnel.
static int debug_channel(void *cls, uint32_t key, void *value)
Call GCCH_debug() on a channel.
static void mark_connection_unready(struct CadetTConnection *ct)
Connection ct is now unready, clear it's ready flag and move it from the ready DLL to the busy DLL.
static void handle_plaintext_channel_open_ack(void *cls, const struct GNUNET_CADET_ChannelOpenAckMessage *cm)
We have received confirmation from the target peer that the given channel could be established (the p...
unsigned int GCT_count_any_connections(const struct CadetTunnel *t)
Counts the number of connections created for a tunnel, including busy connections.
static void new_ephemeral(struct CadetTunnelAxolotl *ax)
Create a new Axolotl ephemeral (ratchet) key.
#define MAX_KEY_GAP
Maximum number of keys (and thus ratchet steps) we are willing to skip before we decide this is eithe...
#define MAX_UNVERIFIED_ATTEMPTS
How often do we try to decrypt payload with unverified key material? Used to limit CPU increase upon ...
static int handle_decrypted(void *cls, const struct GNUNET_MessageHeader *msg)
Handles a message we decrypted, by injecting it into our message queue (which will do the dispatching...
static void store_skipped_key(struct CadetTunnelAxolotl *ax, const struct GNUNET_CRYPTO_AeadSecretKey *HKr)
Delete a key from the list of skipped keys.
#define LOG2(level,...)
static void retry_kx(void *cls)
Try to redo the KX or KX_AUTH handshake, if we can.
void GCT_change_estate(struct CadetTunnel *t, enum CadetTunnelEState state)
Change the tunnel encryption state.
static void send_kx(struct CadetTunnel *t, struct CadetTConnection *ct, struct CadetTunnelAxolotl *ax)
Send a KX message.
static void try_send_normal_payload(struct CadetTunnel *t, struct CadetTConnection *ct)
Send normal payload from queue in t via connection ct.
static void t_h_encrypt(struct CadetTunnelAxolotl *ax, struct GNUNET_CADET_TunnelEncryptedMessage *msg, size_t additional_data_size, const unsigned char *additional_data)
Encrypt header with the axolotl header key.
void GCT_send_cancel(struct CadetTunnelQueueEntry *tq)
Cancel a previously sent message while it's in the queue.
static void decrypted_error_cb(void *cls, enum GNUNET_MQ_Error error)
Function called if we had an error processing an incoming decrypted message.
unsigned int GCT_count_channels(struct CadetTunnel *t)
Returns the number of channels using a tunnel.
void GCT_handle_kx_auth(struct CadetTConnection *ct, const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
Handle KX_AUTH message.
void GCT_iterate_connections(struct CadetTunnel *t, GCT_ConnectionIterator iter, void *iter_cls)
Iterate over all connections of a tunnel.
void GCT_handle_encrypted(struct CadetTConnection *ct, const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
Handle encrypted message.
struct CadetTunnelQueueEntry * GCT_send(struct CadetTunnel *t, const struct GNUNET_MessageHeader *message, GCT_SendContinuation cont, void *cont_cls, struct GNUNET_CADET_ChannelTunnelNumber *ctn)
Sends an already built message on a tunnel, encrypting it and choosing the best connection if not pro...
static enum GNUNET_GenericReturnValue t_ax_decrypt(struct CadetTunnelAxolotl *ax, void *dst, const void *src, size_t size)
Decrypt data with the axolotl tunnel key.
static void connection_ready_cb(void *cls, int is_ready)
A connection is is_ready for transmission.
static void cont_GCT_handle_kx(void *cls, enum GNUNET_GenericReturnValue ret)
Continue to handle KX message.
static void maintain_connections_cb(void *cls)
Function called to maintain the connections underlying our tunnel.
static void handle_plaintext_channel_destroy(void *cls, const struct GNUNET_CADET_ChannelDestroyMessage *cm)
We received a message saying that a channel should be destroyed.
#define HIGH_BIT
static void handle_plaintext_channel_open(void *cls, const struct GNUNET_CADET_ChannelOpenMessage *copen)
We have received a request to open a channel to a port from another peer.
static int iterate_channels_cb(void *cls, uint32_t key, void *value)
Helper function for GCT_iterate_channels.
static void send_kx_auth(struct CadetTunnel *t, struct CadetTConnection *ct, struct CadetTunnelAxolotl *ax, int force_reply)
Send a KX_AUTH message.
struct GNUNET_CADET_ChannelTunnelNumber GCT_add_channel(struct CadetTunnel *t, struct CadetChannel *ch)
Add a channel to a tunnel, and notify channel that we are ready for transmission if we are already up...
static int store_ax_keys(struct CadetTunnelAxolotl *ax, const struct GNUNET_CRYPTO_AeadSecretKey *HKr, uint32_t Np)
Stage skipped AX keys and calculate the message key.
int GCT_add_inbound_connection(struct CadetTunnel *t, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, struct CadetPeerPath *path)
Add a connection to the tunnel.
void GCT_connection_lost(struct CadetTConnection *ct)
We lost a connection, remove it from our list and clean up the connection object itself.
enum CadetTunnelEState GCT_get_estate(struct CadetTunnel *t)
Get the encryption state of a tunnel.
#define INITIAL_KX_RETRY_DELAY
How long do we wait initially before retransmitting the KX? TODO: replace by 2 RTT if/once we have co...
static int check_plaintext_data(void *cls, const struct GNUNET_CADET_ChannelAppDataMessage *msg)
Check that msg is well-formed.
static void handle_plaintext_data_ack(void *cls, const struct GNUNET_CADET_ChannelDataAckMessage *ack)
We received an acknowledgement for data we sent on a channel.
static void handle_plaintext_keepalive(void *cls, const struct GNUNET_MessageHeader *msg)
We got a keepalive.
static void handle_plaintext_data(void *cls, const struct GNUNET_CADET_ChannelAppDataMessage *msg)
We received payload data for a channel.
static void trigger_transmissions(void *cls)
Called when either we have a new connection, or a new message in the queue, or some existing connecti...
static void cont_GCT_handle_kx_auth(void *cls, enum GNUNET_GenericReturnValue ret)
Continue to handle KX_AUTH message.
static void evaluate_connection(void *cls, struct CadetTConnection *ct)
Evaluate a connection, updating our summary information in cls about what kinds of connections we hav...
static void delete_skipped_key(struct CadetTunnelAxolotl *ax, struct CadetTunnelSkippedKey *key)
Delete a key from the list of skipped keys.
void GCT_destroy_tunnel_now(struct CadetTunnel *t)
Destroys the tunnel t now, without delay.
static void t_ax_encrypt(struct CadetTunnelAxolotl *ax, void *dst, const void *src, size_t size)
Encrypt data with the axolotl tunnel key.
struct CadetTunnel * GCT_create_tunnel(struct CadetPeer *destination)
Create a tunnel to destination.
#define LOG(level,...)
static struct CadetTConnection * get_ready_connection(struct CadetTunnel *t)
Find first connection that is ready in the list of our connections.
static const char * estate2s(enum CadetTunnelEState es)
Get string description for tunnel encryption state.
int GCT_alice_or_betty(const struct GNUNET_PeerIdentity *other)
Am I Alice or Betty (some call her Bob), or talking to myself?
#define IDLE_DESTROY_DELAY
How long do we wait until tearing down an idle tunnel?
static ssize_t try_old_ax_keys(struct CadetTunnelAxolotl *ax, void *dst, const struct GNUNET_CADET_TunnelEncryptedMessage *src, size_t size)
Decrypt and verify data with the appropriate tunnel key and verify that the data has not been altered...
#define MAX_SKIPPED_KEYS
Maximum number of skipped keys we keep in memory per tunnel.
static ssize_t t_ax_decrypt_and_validate(struct CadetTunnelAxolotl *ax, void *dst, const struct GNUNET_CADET_TunnelEncryptedMessage *src, size_t size)
Decrypt and verify data with the appropriate tunnel key and verify that the data has not been altered...
void GCT_consider_path(struct CadetTunnel *t, struct CadetPeerPath *p, unsigned int off)
Consider using the path p for the tunnel t.
void GCT_iterate_channels(struct CadetTunnel *t, GCT_ChannelIterator iter, void *iter_cls)
Iterate over all channels of a tunnel.
static void destroy_tunnel(void *cls)
This tunnel is no longer used, destroy it.
void GCT_debug(const struct CadetTunnel *t, enum GNUNET_ErrorType level)
Log all possible info about the tunnel state.
static int destroy_remaining_channels(void *cls, uint32_t key, void *value)
Destroy remaining channels during shutdown.
static void t_hmac_derive_key(const struct GNUNET_CRYPTO_AeadSecretKey *key, struct GNUNET_CRYPTO_AeadSecretKey *out, const void *source, unsigned int len)
Derive a symmetric encryption key from an HMAC-HASH.
static enum GNUNET_GenericReturnValue t_h_decrypt(const struct GNUNET_CRYPTO_AeadSecretKey *hkr, const struct GNUNET_CADET_TunnelEncryptedMessage *src, size_t esize, struct GNUNET_CADET_TunnelEncryptedMessage *dst)
Decrypt header with the current axolotl header key.
static void t_ax_hmac_hash(const struct GNUNET_CRYPTO_AeadSecretKey *key, struct GNUNET_HashCode *hash, const void *source, unsigned int len)
Perform a HMAC.
static void update_ax_by_kx(void *cls, const struct GNUNET_HashCode *key_result)
Update our Axolotl key state based on the KX data we received.
const char * GCT_2s(const struct CadetTunnel *t)
Get the static string for the peer this tunnel is directed.
static void destroy_t_connection(void *cls, struct CadetTConnection *ct)
Clean up connection ct of a tunnel.
Information we track per tunnel.
CadetTunnelEState
All the encryption states a tunnel can be in.
@ CADET_TUNNEL_KEY_AX_AUTH_SENT
KX received and we sent KX_AUTH back, but we got no traffic yet, so we're waiting for either KX_AUTH ...
@ CADET_TUNNEL_KEY_AX_SENT
KX message sent, waiting for other peer's KX_AUTH.
@ CADET_TUNNEL_KEY_UNINITIALIZED
Uninitialized status, we need to send KX.
@ CADET_TUNNEL_KEY_AX_SENT_AND_RECV
KX message sent and received, trying to send back KX_AUTH.
@ CADET_TUNNEL_KEY_OK
Handshake completed: session key available.
@ CADET_TUNNEL_KEY_AX_RECV
KX message received, trying to send back KX_AUTH.
#define DESIRED_CONNECTIONS_PER_TUNNEL
How many connections would we like to have per tunnel?
void(* GCT_ConnectionIterator)(void *cls, struct CadetTConnection *ct)
Iterator over connections.
void(* GCT_SendContinuation)(void *cls, const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
Function called when a transmission requested using GCT_send is done.
void(* GCT_ChannelIterator)(void *cls, struct CadetChannel *ch)
Iterator over channels.
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
static unsigned long long payload
How much data are we currently storing in the database?
#define N
static struct GNUNET_Process * p
Helper process we started.
Definition gnunet-uri.c:38
static struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
struct GNUNET_PILS_Operation * GNUNET_PILS_ecdh(struct GNUNET_PILS_Handle *handle, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, GNUNET_PILS_EcdhResultCallback cb, void *cb_cls)
Derive key material from a ECDH public key and our private key.
Definition pils_api.c:751
void GNUNET_PILS_cancel(struct GNUNET_PILS_Operation *op)
Cancel request.
Definition pils_api.c:776
const struct GNUNET_PeerIdentity * GNUNET_PILS_get_identity(const struct GNUNET_PILS_Handle *handle)
Return the current peer identity of a given handle.
Definition pils_api.c:875
API to schedule computations using continuation passing style.
API to create, modify and access statistics.
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:772
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition crypto_ecc.c:455
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_aead_decrypt(size_t ct_len, const unsigned char ct[ct_len], size_t aad_len, const unsigned char aad[aad_len], const struct GNUNET_CRYPTO_AeadSecretKey *key, const struct GNUNET_CRYPTO_AeadNonce *nonce, const struct GNUNET_CRYPTO_AeadMac *mac, void *pt)
Decrypt the given data using XChaCha20-Poly1305.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EdDSA public key and a private ECDH key.
Definition crypto_ecc.c:863
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_aead_encrypt(size_t pt_len, const unsigned char pt[pt_len], size_t aad_len, const unsigned char aad[aad_len], const struct GNUNET_CRYPTO_AeadSecretKey *key, const struct GNUNET_CRYPTO_AeadNonce *nonce, void *ct, struct GNUNET_CRYPTO_AeadMac *mac)
Encrypt the given data using XChaCha20-Poly1305.
void GNUNET_CRYPTO_ecdhe_key_clear(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Clear memory that was used to store a private key.
Definition crypto_ecc.c:434
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:218
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of 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:40
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)
#define GNUNET_CRYPTO_hkdf_gnunet(result, out_len, xts, xts_len, skm, skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap32_put(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
struct GNUNET_CONTAINER_MultiHashMap32 * GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
Create a 32-bit key multi hash map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap32_remove(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, const void *value)
Remove the given key-value pair from the map.
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
void * GNUNET_CONTAINER_multihashmap32_get(const struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Given a key find a value in the map matching the key.
void GNUNET_CONTAINER_multihashmap32_destroy(struct GNUNET_CONTAINER_MultiHashMap32 *map)
Destroy a 32-bit key hash map.
int GNUNET_CONTAINER_multihashmap32_iterate(struct GNUNET_CONTAINER_MultiHashMap32 *map, GNUNET_CONTAINER_MultiHashMapIterator32Callback it, void *it_cls)
Iterate over all entries in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
uint64_t GNUNET_CONTAINER_HeapCostType
Cost by which elements in a heap can be ordered.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_MAX(a, b)
int GNUNET_get_log_call_status(int caller_level, const char *comp, const char *file, const char *function, int line)
Decides whether a particular logging call should or should not be allowed to be made.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#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_MIN(a, b)
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
GNUNET_ErrorType
Types of errors.
#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).
const char * GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
Convert a public key value to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value 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).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_BULK
@ GNUNET_ERROR_TYPE_DEBUG
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition mq.c:514
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition mq.c:317
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
void GNUNET_MQ_inject_message(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given m...
Definition mq.c:187
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition mq.c:732
#define GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN
Ask the cadet service to create a new channel.
#define GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
Axolotl key exchange.
#define GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK
Confirm payload data end-to-end.
#define GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA
Payload data (inside an encrypted tunnel).
#define GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE
Announce connection is still alive (direction sensitive).
#define GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH
Axolotl key exchange response with authentication.
#define GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
Axolotl encrypted data.
#define GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY
Ask the cadet service to destroy a channel.
#define GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK
Confirm the creation of a channel.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_at(struct GNUNET_TIME_Absolute at, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run at the specified time.
Definition scheduler.c:1260
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition scheduler.c:1310
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:1283
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:438
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:406
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
#define GNUNET_TIME_UNIT_MINUTES
One minute.
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:452
#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
Struct containing all information regarding a channel to a remote client.
struct GNUNET_CADET_ChannelTunnelNumber ctn
Number identifying this channel in its tunnel.
Performance metrics for a connection.
unsigned long long num_acked_transmissions
How many packets that ought to generate an ACK did we send via this connection?
struct GNUNET_TIME_Absolute age
When was this connection first established? (by us sending or receiving the CREATE_ACK for the first ...
unsigned long long num_successes
Number of packets that were sent via this connection did actually receive an ACK? (Note: ACKs may be ...
struct GNUNET_TIME_Absolute last_use
When was this connection last used? (by us sending or receiving a PAYLOAD message on it)
Low-level connection to a destination.
struct CadetTConnection * ct
Which tunnel is using this connection?
unsigned int off
Offset of our destination in path.
Information regarding a possible path to reach a peer.
Struct containing all information regarding a given peer.
Entry in list of connections used by tunnel, with metadata.
struct CadetTConnection * next
Next in DLL.
struct CadetConnection * cc
Connection handle.
struct GNUNET_TIME_Absolute created
Creation time, to keep oldest connection alive.
int is_ready
Is the connection currently ready for transmission?
struct CadetTunnel * t
Tunnel this connection belongs to.
Struct used to store data required for an async update AX by KX process.
struct GNUNET_PeerIdentity peer_id
Peer identity of other peer.
uint32_t flags
Flags for the key exchange in NBO, based on enum GNUNET_CADET_KX_Flags.
void * cb_cls
Update callback closure.
struct GNUNET_HashCode auth
KDF-proof that sender could compute the 3-DH, used in lieu of a signature or payload data.
struct CadetTunnelAxolotl ax
Struct used for Axolotl.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key
Ephemeral public key of other key.
struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key
Ratchet key.
struct GNUNET_PILS_Operation * ecdh_op
Operation to derive key material.
CadetTunnelAxolotlCallback cb
Update callback.
Axolotl data, according to https://github.com/trevp/axolotl/wiki .
struct GNUNET_CRYPTO_AeadSecretKey CKr
32-byte chain keys (used for forward-secrecy) for receiving messages.
uint32_t PNs
Previous message numbers (# of msgs sent under prev ratchet)
struct GNUNET_CRYPTO_EcdhePublicKey last_ephemeral
Last ephemeral public key received from the other peer, for duplicate detection.
struct GNUNET_CRYPTO_AeadSecretKey RK
32-byte root key which gets updated by DH ratchet.
unsigned int skipped
Number of elements in skipped_head <-> skipped_tail.
struct GNUNET_TIME_Absolute ratchet_expiration
Time when the current ratchet expires and a new one is triggered (if ratchet_allowed is GNUNET_YES).
struct GNUNET_CRYPTO_AeadSecretKey NHKr
32-byte next header key (for receiving).
struct GNUNET_CRYPTO_EcdhePrivateKey kx_0
ECDH for key exchange (A0 / B0).
struct GNUNET_CRYPTO_EcdhePublicKey DHRr
ECDH Ratchet key (other peer's public key in the current DH).
uint32_t Ns
Message number (reset to 0 with each new ratchet, next message to send).
struct CadetTunnelSkippedKey * skipped_tail
Skipped messages' keys DLL, tail.
struct GNUNET_CRYPTO_AeadSecretKey CKs
32-byte chain keys (used for forward-secrecy) for sending messages.
struct GNUNET_CRYPTO_AeadSecretKey HKs
32-byte header key (currently used for sending).
struct CadetTunnelSkippedKey * skipped_head
A (double linked) list of stored message keys and associated header keys for "skipped" messages,...
uint32_t Nr
Message number (reset to 0 with each new ratchet, next message to recv).
int ratchet_allowed
True (GNUNET_YES) if we have received a message from the other peer that uses the keys from our last ...
int ratchet_flag
True (GNUNET_YES) if we have to send a new ratchet key in next msg.
struct GNUNET_CRYPTO_AeadSecretKey NHKs
32-byte next header key (for sending), used once the ratchet advances.
unsigned int ratchet_counter
Number of messages received since our last ratchet advance.
struct GNUNET_CRYPTO_EcdhePrivateKey DHRs
ECDH Ratchet key (our private key in the current DH).
struct GNUNET_CRYPTO_AeadSecretKey HKr
32-byte header key (currently used for receiving)
Struct used to save messages in a non-ready tunnel to send once connected.
struct CadetTunnelQueueEntry * prev
We are entries in a DLL.
struct GNUNET_MQ_Envelope * env
Envelope of message to send follows.
GCT_SendContinuation cont
Continuation to call once sent (on the channel layer).
struct GNUNET_CADET_ConnectionTunnelIdentifier * cid
Where to put the connection identifier into the payload of the message in env once we have it?
struct CadetTunnelQueueEntry * next
We are entries in a DLL.
struct CadetTunnel * t
Tunnel these messages belong in.
Struct to old keys for skipped messages while advancing the Axolotl ratchet.
struct GNUNET_TIME_Absolute timestamp
When was this key stored (for timeout).
struct GNUNET_CRYPTO_AeadSecretKey HK
Header key.
unsigned int Kn
Key number for a given HK.
struct CadetTunnelSkippedKey * next
DLL next.
struct GNUNET_CRYPTO_AeadSecretKey MK
Message key.
struct CadetTunnelSkippedKey * prev
DLL prev.
Struct containing all information regarding a tunnel to a peer.
struct GNUNET_SCHEDULER_Task * send_task
Task to send messages from queue (if possible).
struct CadetPeer * destination
Destination of the tunnel.
struct CadetTConnection * connection_ready_head
DLL of ready connections that are actively used to reach the destination peer.
struct CadetTunnelAsync as
Structure to store data temporally for async operations.
struct CadetTConnection * current_ct
Identification of the connection from which we are currently processing a message.
struct CadetTunnelQueueEntry * tq_tail
Queued messages, to transmit once tunnel gets connected.
struct GNUNET_SCHEDULER_Task * maintain_connections_task
Task to trim connections if too many are present.
struct CadetTConnection * connection_busy_head
DLL of connections that we maintain that might be used to reach the destination peer.
struct GNUNET_TIME_Absolute next_kx_attempt
When do we try the next KX?
struct GNUNET_TIME_Relative kx_retry_delay
How long do we wait until we retry the KX?
struct CadetTConnection * connection_busy_tail
DLL of connections that we maintain that might be used to reach the destination peer.
struct GNUNET_MQ_Handle * mq
Dispatcher for decrypted messages only (do NOT use for sending!).
enum CadetTunnelEState estate
State of the tunnel encryption.
struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key
Peer's ephemeral key, to recreate e_key and d_key when own ephemeral key changes.
unsigned int num_busy_connections
Number of connections in the connection_busy_head DLL.
struct CadetTunnelAxolotl ax
Axolotl info.
struct GNUNET_CONTAINER_MultiHashMap32 * channels
Channels inside this tunnel.
struct GNUNET_CRYPTO_AeadSecretKey e_key
Encryption ("our") key.
struct GNUNET_SCHEDULER_Task * destroy_task
Task scheduled if there are no more channels using the tunnel.
struct CadetTConnection * connection_ready_tail
DLL of ready connections that are actively used to reach the destination peer.
struct GNUNET_MessageStreamTokenizer * mst
Tokenizer for decrypted messages.
struct CadetTunnelQueueEntry * tq_head
Queued messages, to transmit once tunnel gets connected.
unsigned int num_ready_connections
Number of connections in the connection_ready_head DLL.
struct GNUNET_SCHEDULER_Task * kx_task
Task to trigger KX.
struct CadetTunnelAxolotl * unverified_ax
Unverified Axolotl info, used only if we got a fresh KX (not a KX_AUTH) while our end of the tunnel w...
unsigned int tq_len
Number of entries in the tq_head DLL.
struct GNUNET_CADET_ChannelTunnelNumber next_ctn
Channel ID for the next created channel in this tunnel.
int kx_auth_requested
Force triggering KX_AUTH independent of estate.
struct GNUNET_CRYPTO_AeadSecretKey d_key
Decryption ("their") key.
unsigned int unverified_attempts
How often have we tried and failed to decrypt a message using the unverified KX material from unverif...
Closure for iterate_channels_cb.
void * iter_cls
Closure for iter.
GCT_ChannelIterator iter
Function to call.
Closure for evaluate_connection.
unsigned int max_length
Maximum length of any of our connections, 0 if we have none.
int duplicate
Set to GNUNET_YES if we have a connection over path already.
GNUNET_CONTAINER_HeapCostType min_desire
Minimum desirability of any of our connections, UINT64_MAX if we have none.
struct CadetTConnection * worst
Connection deemed the "worst" so far encountered by evaluate_connection, NULL if we did not yet encou...
unsigned int min_length
Minimum length of any of our connections, UINT_MAX if we have none.
GNUNET_CONTAINER_HeapCostType max_desire
Maximum desirability of any of our connections, 0 if we have none.
double worst_score
Numeric score of worst, only set if worst is non-NULL.
struct CadetPeerPath * path
Path we are comparing against for evaluate_connection, can be NULL.
Encrypted axolotl header with numbers that identify which keys in which ratchet are to be used to dec...
uint32_t PNs
Number of messages sent with the previous ratchet key.
struct GNUNET_CRYPTO_EcdhePublicKey DHRs
Current ratchet key.
uint32_t Ns
Number of messages sent with the current ratchet key.
Message for cadet data traffic.
Message to acknowledge end-to-end data.
struct GNUNET_CADET_ChannelTunnelNumber ctn
ID of the channel.
Message to destroy a channel of type GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY.
struct GNUNET_CADET_ChannelTunnelNumber ctn
ID of the channel.
Message to acknowledge opening a channel of type GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
struct GNUNET_HashCode port
Port number of the channel, used to prove to the initiator that the receiver knows the port.
struct GNUNET_CADET_ChannelTunnelNumber ctn
ID of the channel.
Message to create a Channel.
struct GNUNET_HashCode h_port
Hash of destination port and listener.
struct GNUNET_CADET_ChannelTunnelNumber ctn
ID of the channel within the tunnel.
uint32_t opt
Channel options.
Number identifying a CADET channel within a tunnel.
uint32_t cn
Which number does this channel have that uniquely identifies it within its tunnel,...
Hash uniquely identifying a connection below a tunnel.
Axolotl-encrypted tunnel message with application payload.
struct GNUNET_CADET_AxHeader ax_header
Axolotl-header that specifies which keys to use in which ratchet to decrypt the body that follows.
struct GNUNET_CRYPTO_AeadMac mac
MAC of the encrypted message, used to verify message integrity.
struct GNUNET_CADET_ConnectionTunnelIdentifier cid
ID of the connection.
Message for a Key eXchange for a tunnel, with authentication.
Message for a Key eXchange for a tunnel.
uint32_t flags
Flags for the key exchange in NBO, based on enum GNUNET_CADET_KX_Flags.
Internal representation of the hash map.
type for session keys
unsigned char mac[16]
Initialization vector.
type for session keys
type for (message) authentication keys
Private ECC key encoded for transmission.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
A 512-bit hashcode.
Handle to a message queue.
Definition mq.c:87
Message handler for a specific message type.
Header for all communications.
Handle to a message stream tokenizer.
Definition mst.c:45
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:141
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.