예제 #1
0
        public void TestI2PDestinationInfo4()
        {
            var destinfo = new I2PDestinationInfo(I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519);

            var asba    = destinfo.ToBase64();
            var dfromba = new I2PDestinationInfo(asba);

            Assert.IsTrue(BufUtils.Equal(destinfo.ToByteArray(), dfromba.ToByteArray()));

            var asstr    = destinfo.ToBase64();
            var dfromstr = new I2PDestinationInfo(asstr);

            var d1 = dfromba.Destination;
            var d2 = dfromstr.Destination;
            var d3 = dfromstr.Destination;

            Assert.IsTrue(BufUtils.Equal(d2.ToByteArray(), d2.ToByteArray()));

            Assert.IsTrue(d3.Padding == d2.Padding);
            Assert.IsTrue(d3.CertificateBuf == d2.CertificateBuf);
            Assert.IsTrue(d3.PublicKeyBuf == d2.PublicKeyBuf);
            Assert.IsTrue(d3.SigningPublicKeyBuf == d2.SigningPublicKeyBuf);
            Assert.IsTrue(BufUtils.Equal(d3.ToByteArray(), d2.ToByteArray()));

            Assert.IsTrue(d1.Padding == d2.Padding);
            Assert.IsTrue(d1.CertificateBuf == d2.CertificateBuf);
            Assert.IsTrue(d1.PublicKeyBuf == d2.PublicKeyBuf);
            Assert.IsTrue(d1.SigningPublicKeyBuf == d2.SigningPublicKeyBuf);
            Assert.IsTrue(BufUtils.Equal(d1.ToByteArray(), d2.ToByteArray()));

            Assert.IsTrue(d1.IdentHash == d2.IdentHash);
        }
예제 #2
0
        public void TestSimpleDatabaseStoreLeaseSetEd25519Creation()
        {
            var linfo  = new I2PLeaseInfo(Public, PublicSigningEd25519, Private, PrivateSigningEd25519);
            var leases = new List <I2PLease>();

            for (int i = 0; i < 5; ++i)
            {
                leases.Add(new I2PLease(new I2PIdentHash(true), (uint)((i * 72 + 6) * i * 1314 + 5) % 40000, I2PDate.Now));
            }
            var ls = new I2PLeaseSet(new I2PDestination(Public, PublicSigningEd25519), leases, linfo);

            var dbsm = new DatabaseStoreMessage(ls);

            var data = dbsm.Header16.HeaderAndPayload;

            var recreated = I2NPMessage.ReadHeader16(new BufRefLen(data));

            Assert.IsTrue(recreated.MessageType == I2NPMessage.MessageTypes.DatabaseStore);
            var rdsm = (DatabaseStoreMessage)recreated.Message;

            Assert.IsTrue(rdsm.LeaseSet.Leases.Count == 5);

            Assert.IsTrue(BufUtils.Equal(ls.Destination.ToByteArray(), rdsm.LeaseSet.Destination.ToByteArray()));
            Assert.IsTrue(BufUtils.Equal(ls.PublicKey.ToByteArray(), rdsm.LeaseSet.PublicKey.ToByteArray()));
            Assert.IsTrue(BufUtils.Equal(ls.PublicSigningKey.ToByteArray(), rdsm.LeaseSet.PublicSigningKey.ToByteArray()));
            for (int i = 0; i < 5; ++i)
            {
                Assert.IsTrue(BufUtils.Equal(ls.Leases[i].ToByteArray(), rdsm.LeaseSet.Leases[i].ToByteArray()));
            }

            Assert.IsTrue(rdsm.LeaseSet.VerifySignature());
        }
예제 #3
0
 private static void StoreFileFormatVersionTest_OneRun(byte[] data, Stream dest, int defaultsectorsize)
 {
     using (Store target = new Store(dest, defaultsectorsize))
     {
         var ix = target.Write(data);
         Assert.AreEqual(data.Length, target.GetDataLength(ix));
         var copy = target.Read(ix);
         Assert.IsTrue(BufUtils.Equal(copy, data));
     }
 }
예제 #4
0
        public void TestGarlicCreateMessage()
        {
            var dsm1 = new DeliveryStatusMessage(0x425c)
            {
                MessageId = 2354
            };

            var dsm2 = new DeliveryStatusMessage(0x425c)
            {
                MessageId = 2354
            };

            dsm2.Timestamp  = dsm1.Timestamp;
            dsm2.Expiration = dsm1.Expiration;

            var dsm1h = dsm1.CreateHeader16;
            var dsm2h = dsm2.CreateHeader16;

            var dsm1hap = dsm1h.HeaderAndPayload;
            var dsm2hap = dsm2h.HeaderAndPayload;

            var st11 = FreenetBase64.Encode(dsm1hap);
            var st12 = FreenetBase64.Encode(dsm2hap);

            Assert.IsTrue(dsm1hap == dsm2hap);

            var gcd1 = new GarlicCloveDeliveryDestination(
                dsm1,
                Destination.IdentHash);

            var gcd2 = new GarlicCloveDeliveryDestination(
                dsm2,
                Destination.IdentHash);

            var gcd1ar = gcd1.ToByteArray();
            var gcd2ar = gcd2.ToByteArray();

            var st1 = FreenetBase64.Encode(new BufLen(gcd1ar));
            var st2 = FreenetBase64.Encode(new BufLen(gcd2ar));

            Assert.IsTrue(BufUtils.Equal(gcd1ar, gcd2ar));

            var msg1 = new GarlicClove(gcd1);
            var msg2 = new GarlicClove(gcd2);

            var g1 = new Garlic(msg1, msg2);
            var g2 = new Garlic(new BufRefLen(g1.ToByteArray()).Clone());

            Assert.IsTrue(BufUtils.Equal(g1.ToByteArray(), g2.ToByteArray()));
        }
예제 #5
0
        public void StoreConstructorTest()
        {
            var    data = new byte[] { 0x5c, 0xff, 0x00, 0x27 };
            Stream dest = new MemoryStream();
            int    defaultsectorsize = 1024;

            using (Store target = new Store(dest, defaultsectorsize))
            {
                var ix = target.Write(data);
                Assert.AreEqual(data.Length, target.GetDataLength(ix));
                var copy = target.Read(ix);
                Assert.IsTrue(BufUtils.Equal(copy, data));
            }
        }
예제 #6
0
        public void TestI2PDestinationInfo3()
        {
            var destinfo = new I2PDestinationInfo(I2PSigningKey.SigningKeyTypes.ECDSA_SHA384_P384);

            var asba    = destinfo.ToByteArray();
            var dfromba = new I2PDestinationInfo(new BufRefLen(asba));

            Assert.IsTrue(BufUtils.Equal(destinfo.ToByteArray(), dfromba.ToByteArray()));

            var asstr    = destinfo.ToBase64();
            var dfromstr = new I2PDestinationInfo(asstr);

            Assert.IsTrue(BufUtils.Equal(destinfo.ToByteArray(), dfromstr.ToByteArray()));
        }
예제 #7
0
        public void StoreConstructorTest1()
        {
            var data              = new byte[] { 0x5c, 0xff, 0x00, 0x27 };
            var filename          = Path.GetFullPath(Path.GetTempFileName());
            int defaultsectorsize = 1024;

            using (Store target = new Store(filename, defaultsectorsize))
            {
                var ix = target.Write(data);
                Assert.AreEqual(data.Length, target.GetDataLength(ix));
                var copy = target.Read(ix);
                Assert.IsTrue(BufUtils.Equal(copy, data));
            }
            File.Delete(filename);
        }
예제 #8
0
 public bool Equals(IPEndPoint x, IPEndPoint y)
 {
     if (x == null && y == null)
     {
         return(false);
     }
     if (x == null || y == null)
     {
         return(false);
     }
     if (ReferenceEquals(x, y))
     {
         return(true);
     }
     return(BufUtils.Equal(x.Address.GetAddressBytes(), y.Address.GetAddressBytes()) && x.Port == y.Port);
 }
예제 #9
0
        public void IndexStreamTest1()
        {
            var data1 = new byte[10000];

            data1.Randomize();
            //for ( int i = 0; i < data1.Length; ++i ) data1[i] = (byte)( i & 0xff );

            var data2 = new byte[1000];

            data2.Randomize();

            var data3 = new byte[20000];

            data3.Randomize();

            var filename = Path.GetFullPath(Path.GetTempFileName());

            int defaultsectorsize = 17;

            while (defaultsectorsize < 32000)
            {
                using (Store target = new Store(filename, defaultsectorsize))
                {
                    var ixstream = target.IndexStream();
                    using (var innerstore = new Store(ixstream, defaultsectorsize * 2 + BufUtils.RandomInt(12)))
                    {
                        var ix = innerstore.Write(data1);
                        Assert.AreEqual(data1.Length, innerstore.GetDataLength(ix));
                        var copy = innerstore.Read(ix);
                        Assert.IsTrue(BufUtils.Equal(copy, data1));

                        innerstore.Write(data2, ix);
                        Assert.AreEqual(data2.Length, innerstore.GetDataLength(ix));
                        copy = innerstore.Read(ix);
                        Assert.IsTrue(BufUtils.Equal(copy, data2));

                        innerstore.Write(data3, ix);
                        Assert.AreEqual(data3.Length, innerstore.GetDataLength(ix));
                        copy = innerstore.Read(ix);
                        Assert.IsTrue(BufUtils.Equal(copy, data3));
                    }
                }

                defaultsectorsize = (int)(defaultsectorsize * 1.8);
                File.Delete(filename);
            }
        }
예제 #10
0
        public void TestI2PDestination()
        {
            var certificate = new I2PCertificate(I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519);

            var keys = I2PPrivateKey.GetNewKeyPair();

            var privkey  = keys.PrivateKey;
            var privskey = new I2PSigningPrivateKey(certificate);

            var dest = new I2PDestination(
                keys.PublicKey,
                new I2PSigningPublicKey(privskey));

            var d2 = new I2PDestination(new BufRefLen(dest.ToByteArray()));

            Assert.IsTrue(BufUtils.Equal(dest.ToByteArray(), d2.ToByteArray()));
        }
예제 #11
0
        public void StoreRecordAlignment()
        {
            var    data = new byte[] { 0x5c, 0xff, 0x00, 0x27 };
            Stream dest = new MemoryStream();
            int    defaultsectorsize = 4096;

            using (Store target = new Store(dest, defaultsectorsize))
            {
                var ix = target.Write(data);
                Assert.AreEqual(data.Length, target.GetDataLength(ix));
                var copy = target.Read(ix);
                Assert.IsTrue(BufUtils.Equal(copy, data));

                var sectorstart = target.BitmapToPos(ix);
                Assert.IsTrue(sectorstart % defaultsectorsize == 0);
            }
        }
예제 #12
0
        public void TestEncodeDecodeAES()
        {
            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 sessionkey = new I2PSessionKey();
            var sessiontag = new I2PSessionTag();

            var cg     = Garlic.AESEncryptGarlic(garlic, sessionkey, sessiontag, null);
            var egdata = I2NPMessage.Clone(cg);

            var(aesblock, sk) = Garlic.RetrieveAESBlock(egdata, Private, (t) => sessionkey);
            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 < 30; ++i)
            {
                tags.Add(new I2PSessionTag());
            }

            cg     = Garlic.AESEncryptGarlic(garlic, sessionkey, sessiontag, null);
            egdata = I2NPMessage.Clone(cg);

            (aesblock, sk) = Garlic.RetrieveAESBlock(egdata, Private, (t) => sessionkey);
            g2             = new Garlic((BufRefLen)aesblock.Payload);

            Assert.IsTrue(BufUtils.Equal(garlic.ToByteArray(), g2.ToByteArray()));
        }
예제 #13
0
        public void TestSimpleDatabaseStoreLeaseSetCreation()
        {
            var linfo = new I2PLeaseInfo(Public, PublicSigning, Private, PrivateSigning);

            var leases = new List <I2PLease>();

            for (int i = 0; i < 5; ++i)
            {
                leases.Add(new I2PLease(
                               new I2PIdentHash(true),
                               new I2PTunnelId()));
            }

            var ls = new I2PLeaseSet(new I2PDestination(Public, PublicSigning), leases, linfo);

            var dbsm = new DatabaseStoreMessage(ls);

            var data = dbsm.CreateHeader16.HeaderAndPayload.Clone();

            var recreated = I2NPMessage.ReadHeader16(new BufRefLen(data));

            Assert.IsTrue(recreated.MessageType == I2NPMessage.MessageTypes.DatabaseStore);
            var rdsm = (DatabaseStoreMessage)recreated.Message;

            Assert.IsTrue(rdsm.LeaseSet.Leases.Count() == 5);

            Assert.IsTrue(BufUtils.Equal(ls.Destination.ToByteArray(), rdsm.LeaseSet.Destination.ToByteArray()));
            Assert.IsTrue(BufUtils.Equal(ls.PublicKey.ToByteArray(), rdsm.LeaseSet.PublicKey.ToByteArray()));
            Assert.IsTrue(BufUtils.Equal(ls.PublicSigningKey.ToByteArray(), rdsm.LeaseSet.PublicSigningKey.ToByteArray()));

            var rdsmlsar = rdsm.LeaseSet.Leases.ToArray();
            var lsar     = ls.Leases.ToArray();

            // Order should be maintained
            for (int i = 0; i < 5; ++i)
            {
                Assert.IsTrue(
                    BufUtils.Equal(
                        lsar[i].ToByteArray(),
                        rdsmlsar[i].ToByteArray()));
            }

            Assert.IsTrue(rdsm.LeaseSet.VerifySignature(PublicSigning));
        }
예제 #14
0
        public void WriteTest()
        {
            var data1 = new byte[10000];

            data1.Randomize();

            var data2 = new byte[1000];

            data2.Randomize();

            var data3 = new byte[200000];

            data3.Randomize();

            int defaultsectorsize = 30;

            while (defaultsectorsize < 32000)
            {
                using (Stream dest = new MemoryStream())
                {
                    using (Store target = new Store(dest, defaultsectorsize))
                    {
                        var ix = target.Write(data1);
                        Assert.AreEqual(data1.Length, target.GetDataLength(ix));
                        var copy = target.Read(ix);
                        Assert.IsTrue(BufUtils.Equal(copy, data1));

                        target.Write(data2, ix);
                        Assert.AreEqual(data2.Length, target.GetDataLength(ix));
                        copy = target.Read(ix);
                        Assert.IsTrue(BufUtils.Equal(copy, data2));

                        target.Write(data3, ix);
                        Assert.AreEqual(data3.Length, target.GetDataLength(ix));
                        copy = target.Read(ix);
                        Assert.IsTrue(BufUtils.Equal(copy, data3));
                    }
                }

                defaultsectorsize = (int)(defaultsectorsize * 1.8);
            }
        }
예제 #15
0
        internal void ReportedAddress(IPAddress ipaddr)
        {
            DebugUtils.LogDebug("SSU My external IP " + ipaddr.ToString());

            if (LastExternalIPProcess.DeltaToNowMilliseconds < 20000)
            {
                return;
            }
            LastExternalIPProcess.SetNow();

            lock ( ReportedAddresses )
            {
                ReportedAddresses.AddLast(ipaddr);
                while (ReportedAddresses.Count > 50)
                {
                    ReportedAddresses.RemoveFirst();
                }

                var first      = ReportedAddresses.First.Value;
                var firstbytes = first.GetAddressBytes();
                if (ReportedAddresses.Count() > 10 && ReportedAddresses.All(a => BufUtils.Equal(a.GetAddressBytes(), firstbytes)))
                {
                    DebugUtils.Log("SSU Start using unanimous remote reported external IP " + ipaddr.ToString());
                    UpdateSSUReportedAddr(ipaddr);
                }
                else
                {
                    var freq = ReportedAddresses.GroupBy(a => a.GetAddressBytes()).OrderBy(g => g.Count());
                    if (freq.First().Count() > 15)
                    {
                        DebugUtils.Log("SSU Start using most frequently reported remote external IP " + ipaddr.ToString());
                        UpdateSSUReportedAddr(ipaddr);
                    }
                }
            }
        }
예제 #16
0
        internal void ReportedAddress(IPAddress ipaddr)
        {
            if (LastExternalIPProcess.DeltaToNowSeconds < (LastIPReport == null ? 1 : 60))
            {
                return;
            }
            LastExternalIPProcess.SetNow();

            Logging.LogTransport($"SSU My IP: My external IP {ipaddr}");

            lock ( ReportedAddresses )
            {
                ReportedAddresses.AddLast(ipaddr);
                while (ReportedAddresses.Count > 200)
                {
                    ReportedAddresses.RemoveFirst();
                }

                var first      = ReportedAddresses.First.Value;
                var firstbytes = first.GetAddressBytes();
                if (ReportedAddresses.Count() > 10 && ReportedAddresses.All(a => BufUtils.Equal(a.GetAddressBytes(), firstbytes)))
                {
                    Logging.LogTransport($"SSU My IP: Start using unanimous remote reported external IP {ipaddr}");
                    UpdateSSUReportedAddr(ipaddr);
                }
                else
                {
                    var freq = ReportedAddresses.GroupBy(a => a.GetAddressBytes()).OrderBy(g => g.Count());
                    if (freq.First().Count() > 15)
                    {
                        Logging.LogTransport($"SSU My IP: Start using most frequently reported remote external IP {ipaddr}");
                        UpdateSSUReportedAddr(ipaddr);
                    }
                }
            }
        }
예제 #17
0
        public IEnumerable <TunnelMessage> Process(IEnumerable <TunnelDataMessage> tdmsgs)
        {
            var result = new List <TunnelMessage>();

            RemoveUnmatchedFragments.Do(() =>
            {
                lock ( MessageFragments )
                {
                    var remove = MessageFragments.Where(p => p.Value.Created.DeltaToNow.ToMinutes > RememberUnmatchedFragmentsForMinutes).
                                 Select(p => p.Key).ToArray();
                    foreach (var key in remove)
                    {
                        Logging.LogDebug("TunnelDataFragmentReassembly: Removing old unmatched fragment for " + key.ToString());
                        MessageFragments.Remove(key);
                    }
                }
            });

            foreach (var msg in tdmsgs)
            {
                var hash = I2PHashSHA256.GetHash(msg.TunnelDataPayload, msg.IV);
                var eq   = BufUtils.Equal(msg.Checksum.PeekB(0, 4), 0, hash, 0, 4);
                if (!eq)
                {
                    // Do not stop processing, just throw away faulty data
                    // throw new SignatureCheckFailureException( "TunnelDataFragmentReassembly: SHA256 check failed in TunnelData" );
                    Logging.LogDebug("TunnelDataFragmentReassembly: SHA256 check failed in TunnelData");
                    continue;
                }

                var reader = (BufRefLen)msg.TunnelDataPayload;

                while (reader.Length > 0)
                {
                    var frag = new TunnelDataFragment(reader);

                    if (frag.FollowOnFragment)
                    {
                        List <TunnelDataFragment> fragments;

                        if (!MessageFragments.ContainsKey(frag.MessageId))
                        {
                            fragments = new List <TunnelDataFragment>();
                            MessageFragments[frag.MessageId] = new TunnelDataFragmentList(fragments);
                        }
                        else
                        {
                            fragments = MessageFragments[frag.MessageId].List;
                        }

                        if (fragments.Count <= frag.FragmentNumber)
                        {
                            fragments.AddRange(new TunnelDataFragment[frag.FragmentNumber - fragments.Count + 1]);
                        }
                        fragments[frag.FragmentNumber] = frag;

                        CheckForAllFragmentsFound(result, frag.MessageId, fragments);
                    }
                    else
                    {
                        if (frag.Fragmented)
                        {
                            List <TunnelDataFragment> fragments;

                            if (!MessageFragments.ContainsKey(frag.MessageId))
                            {
                                fragments = new List <TunnelDataFragment>();
                                MessageFragments[frag.MessageId] = new TunnelDataFragmentList(fragments);
                            }
                            else
                            {
                                fragments = MessageFragments[frag.MessageId].List;
                            }

                            if (fragments.Count == 0)
                            {
                                fragments.AddRange(new TunnelDataFragment[1]);
                            }
                            fragments[0] = frag;

                            CheckForAllFragmentsFound(result, frag.MessageId, fragments);
                        }
                        else
                        {
                            AddTunnelMessage(result, frag, frag.Payload.ToByteArray());
                        }
                    }
                }
            }

            return(result);
        }
예제 #18
0
        public void IndexStreamTest3()
        {
            const int MaxSectorSize = 32000;

            var data1 = new byte[100000];

            data1.Randomize();
            //for ( int i = 0; i < data1.Length; ++i ) data1[i] = (byte)( i & 0xff );

            var data2 = new byte[1000];

            data2.Randomize();

            var data3 = new byte[200000];

            data3.Randomize();

            int defaultsectorsize = 17;

            while (defaultsectorsize < MaxSectorSize)
            {
                using (Stream dest = new MemoryStream())
                {
                    using (Store mid = new Store(dest, MaxSectorSize - defaultsectorsize + BufUtils.RandomInt(15)))
                    {
                        mid.IndexStream();
                        mid.IndexStream();
                        mid.IndexStream();

                        using (Store target = new Store(mid.IndexStream(), defaultsectorsize))
                        {
                            var firstsector = new byte[target.FirstSectorDataSize];
                            firstsector.Populate <byte>(1);

                            var threesectors = new byte[target.SectorDataSize * 3];
                            threesectors.Populate <byte>(2);

                            var towrite = new BufLen[] { new BufLen(firstsector), new BufLen(threesectors) };

                            var strix = target.Write(towrite);

                            var ixstream = BufUtils.RandomInt(10) > 5 ?
                                           target.IndexStream(strix, true) :
                                           target.IndexStream(strix, target.FirstSectorDataSize + BufUtils.RandomInt(20));
                            using (var innerstore = new Store(ixstream, defaultsectorsize * 2 + BufUtils.RandomInt(12)))
                            {
                                var ix = innerstore.Write(data1);
                                Assert.AreEqual(data1.Length, innerstore.GetDataLength(ix));
                                var copy = innerstore.Read(ix);
                                Assert.IsTrue(BufUtils.Equal(copy, data1));

                                innerstore.Write(data2, ix);
                                Assert.AreEqual(data2.Length, innerstore.GetDataLength(ix));
                                copy = innerstore.Read(ix);
                                Assert.IsTrue(BufUtils.Equal(copy, data2));

                                innerstore.Write(data3, ix);
                                Assert.AreEqual(data3.Length, innerstore.GetDataLength(ix));
                                copy = innerstore.Read(ix);
                                Assert.IsTrue(BufUtils.Equal(copy, data3));
                            }

                            using (var innerstore = new Store(target.IndexStream(), defaultsectorsize * 2 + BufUtils.RandomInt(12)))
                            {
                                var ix = innerstore.Write(data1);
                                Assert.AreEqual(data1.Length, innerstore.GetDataLength(ix));
                                var copy = innerstore.Read(ix);
                                Assert.IsTrue(BufUtils.Equal(copy, data1));

                                innerstore.Write(data2, ix);
                                Assert.AreEqual(data2.Length, innerstore.GetDataLength(ix));
                                copy = innerstore.Read(ix);
                                Assert.IsTrue(BufUtils.Equal(copy, data2));

                                innerstore.Write(data3, ix);
                                Assert.AreEqual(data3.Length, innerstore.GetDataLength(ix));
                                copy = innerstore.Read(ix);
                                Assert.IsTrue(BufUtils.Equal(copy, data3));
                            }

                            Assert.IsTrue(BufUtils.Equal(target.Read(strix).Take(firstsector.Length).ToArray(), firstsector));
                        }
                    }
                }

                defaultsectorsize = (int)(defaultsectorsize * 1.8);
            }
        }