Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        private void SendLeaseSetUpdateGarlic(
            I2PIdentHash ffdest,
            I2PPublicKey pubkey,
            I2PLeaseSet ls,
            uint token)
        {
            // If greater than zero, a DeliveryStatusMessage
            // is requested with the Message ID set to the value of the Reply Token.
            // A floodfill router is also expected to flood the data to the closest floodfill peers
            // if the token is greater than zero.
            // https://geti2p.net/spec/i2np#databasestore

            var outtunnel = TunnelProvider.Inst.GetEstablishedOutboundTunnel(false);

            var replytunnel = ls.Leases.Random();

            if (outtunnel is null || replytunnel is null)
            {
                Logging.LogDebug($"SendLeaseSetUpdateGarlic: " +
                                 $"outtunnel: {outtunnel}, replytunnel: {replytunnel?.TunnelGw?.Id32Short}");
                return;
            }

            var ds          = new DatabaseStoreMessage(ls);
            var delivstatus = new DeliveryStatusMessage(token);

            // As explained on the network database page, local LeaseSets are sent to floodfill
            // routers in a Database Store Message wrapped in a Garlic Message so it is not
            // visible to the tunnel's outbound gateway.

            var garlic = new Garlic(
                new GarlicClove(
                    new GarlicCloveDeliveryLocal(ds)),
                new GarlicClove(
                    new GarlicCloveDeliveryTunnel(delivstatus, replytunnel.TunnelGw, replytunnel.TunnelId))
                );

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

            outtunnel.Send(
                new TunnelMessageRouter(
                    egmsg,
                    ffdest));
        }
Exemplo 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()));
        }