Ejemplo n.º 1
0
        private static void HandleGarlic(GarlicMessage garlicmsg)
        {
            try
            {
                // No sessions, just accept EG
                var(aesblock, sessionkey) = Garlic.EGDecryptGarlic(
                    garlicmsg,
                    RouterContext.Inst.PrivateKey);

                if (aesblock == null)
                {
                    Logging.LogWarning($"Router: HandleGarlic: Failed to decrypt.");
                    return;
                }

                var garlic = new Garlic((BufRefLen)aesblock.Payload);

#if LOG_ALL_LEASE_MGMT
                Logging.LogDebug($"Router: HandleGarlic: {garlic}");
#endif

                foreach (var clove in garlic.Cloves)
                {
                    try
                    {
                        // Only accept Local to not turn into a bot net.
                        switch (clove.Delivery.Delivery)
                        {
                        case GarlicCloveDelivery.DeliveryMethod.Local:
#if LOG_ALL_LEASE_MGMT
                            Logging.LogDebug(
                                $"Router: HandleGarlic: Delivered Local: {clove.Message}");
#endif
                            TunnelProvider.Inst.DistributeIncomingMessage(null, clove.Message.CreateHeader16);
                            break;

                        default:
                            Logging.LogDebug($"Router HandleGarlic: Dropped clove ({clove})");
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.Log("Router: HandleGarlic switch", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Log("Router: HandleGarlic", ex);
            }
        }
Ejemplo n.º 2
0
        public void TestGarlicCreate()
        {
            var ls = new I2PDate(DateTime.Now + TimeSpan.FromMinutes(5));

            var origmessage = new DeliveryStatusMessage(I2NPMessage.GenerateMessageId());
            var bigmessage  = new DataMessage(new BufLen(BufUtils.RandomBytes(14 * 1024)));

            var garlic = new Garlic(
                new GarlicClove(
                    new GarlicCloveDeliveryLocal(origmessage),
                    ls),
                new GarlicClove(
                    new GarlicCloveDeliveryLocal(bigmessage),
                    ls)
                );

            var egmsg = Garlic.EGEncryptGarlic(
                garlic,
                Public,
                new I2PSessionKey(),
                null);

            var origegdata  = I2NPMessage.Clone(egmsg);
            var origegdata2 = I2NPMessage.Clone(egmsg);

            // Decrypt

            var(aesblock, sessionkey1) = Garlic.EGDecryptGarlic(origegdata, Private);

            var newgarlic = new Garlic((BufRefLen)aesblock.Payload);

            var g1 = new BufLen(garlic.ToByteArray());
            var g2 = new BufLen(newgarlic.ToByteArray());

            Assert.IsTrue(g1 == g2);

            // Retrieve

            var(aesblock2, sessionkey2) = Garlic.RetrieveAESBlock(origegdata2, Private, null);

            newgarlic = new Garlic((BufRefLen)aesblock2.Payload);

            g1 = new BufLen(garlic.ToByteArray());
            g2 = new BufLen(newgarlic.ToByteArray());

            Assert.IsTrue(g1 == g2);
            Assert.IsTrue(sessionkey1 == sessionkey2);
        }
Ejemplo n.º 3
0
        public void TestEncodeDecodeEG()
        {
            var m1 = new DeliveryStatusMessage(0x4321);
            var m2 = new DeliveryStatusMessage(0xa3c2);

            var garlic = new Garlic(
                new GarlicClove(
                    new GarlicCloveDeliveryDestination(
                        m1,
                        Destination.IdentHash)),
                new GarlicClove(
                    new GarlicCloveDeliveryDestination(
                        m2,
                        Destination.IdentHash))
                );

            // No tags

            var cg     = Garlic.EGEncryptGarlic(garlic, Public, new I2PSessionKey(), null);
            var egdata = I2NPMessage.Clone(cg);

            var(aesblock, sk) = Garlic.EGDecryptGarlic(egdata, Private);
            var g2 = new Garlic((BufRefLen)aesblock.Payload);

            Assert.IsTrue(BufUtils.Equal(garlic.ToByteArray(), g2.ToByteArray()));

            // With tags
            var tags = new List <I2PSessionTag>();

            for (int i = 0; i < 8; ++i)
            {
                tags.Add(new I2PSessionTag());
            }

            cg     = Garlic.EGEncryptGarlic(garlic, Public, new I2PSessionKey(), tags);
            egdata = I2NPMessage.Clone(cg);

            (aesblock, sk) = Garlic.EGDecryptGarlic(egdata, Private);
            g2             = new Garlic((BufRefLen)aesblock.Payload);

            Assert.IsTrue(BufUtils.Equal(garlic.ToByteArray(), g2.ToByteArray()));
        }