Exemplo n.º 1
0
        private void SaveConfig(Store s)
        {
            var lookup = s.GetMatching(e => (StoreRecordId)e[0] == StoreRecordId.StoreIdConfig, 1);
            var str2ix = new Dictionary <I2PString, int>();

            foreach (var one in lookup)
            {
                var reader = new BufRefLen(one.Value);
                reader.Read32();
                var key = new I2PString(reader);
                str2ix[key] = one.Key;
            }

            AccessConfig(delegate(Dictionary <I2PString, I2PString> settings)
            {
                foreach (var one in settings)
                {
                    var rec = new BufLen[] {
                        BufUtils.To32BL((int)StoreRecordId.StoreIdConfig),
                        new BufLen(one.Key.ToByteArray()), new BufLen(one.Value.ToByteArray())
                    };

                    if (str2ix.ContainsKey(one.Key))
                    {
                        s.Write(rec, str2ix[one.Key]);
                    }
                    else
                    {
                        s.Write(rec);
                    }
                }
            });
        }
Exemplo n.º 2
0
        private void SendUnfragmentedSessionConfirmed()
        {
            var ri = new BufLen(Session.MyRouterContext.MyRouterInfo.ToByteArray());

            SendMessage(
                SSUHeader.MessageTypes.SessionConfirmed,
                Session.MACKey,
                Session.SharedKey,
                (start, writer) =>
            {
                writer.Write8((byte)((0 << 4) + 1));
                writer.WriteFlip16((ushort)ri.Length);
                writer.Write(ri);

                Session.SignOnTimeA = BufUtils.Flip32(SSUHost.SSUTime(DateTime.UtcNow));
                writer.Write32(Session.SignOnTimeA);
                var padding = BufUtils.Get16BytePadding(Session.MyRouterContext.Certificate.SignatureLength + (writer - start));
                writer.Write(BufUtils.RandomBytes(padding));

                var baddr = new BufLen(Session.RemoteEP.Address.GetAddressBytes());
                var bport = BufUtils.Flip16BL((ushort)Session.RemoteEP.Port);
#if LOG_MUCH_TRANSPORT
                Logging.LogTransport($"SSU {this}: X for signature {Request.X.Key}.");
                Logging.LogTransport($"SSU {this}: Y for signature {Request.Y.Key}.");
                Logging.LogTransport($"SSU {this}: Alice address for signature {Request.SCMessage.Address}. Port {Request.SCMessage.Port}.");
                Logging.LogTransport($"SSU {this}: Bob address for signature {baddr}. Port {bport}.");
                Logging.LogTransport($"SSU {this}: Relay tag {Request.SCMessage.RelayTag}. Signon time {(BufLen)Session.SignOnTimeA}.");
#endif

                var sign = I2PSignature.DoSign(Session.MyRouterContext.PrivateSigningKey,
                                               Request.X.Key, Request.Y.Key,
                                               Request.SCMessage.Address, Request.SCMessage.Port,
                                               baddr, bport,
                                               Request.SCMessage.RelayTag, BufUtils.To32BL(Session.SignOnTimeA)
                                               );
                writer.Write(sign);

                Logging.LogTransport($"SSU {this}: {Session.RemoteEP} " +
                                     $"sending unfragmented SessionConfirmed [0x{writer - start - SSUHeader.FIXED_HEADER_SIZE:X}] bytes.");
                return(true);
            });
        }
Exemplo n.º 3
0
        private SSUState VerifyRemoteSignature()
        {
            var baaddr = new BufLen(AAddr);
            var bbport = BufUtils.Flip16BL((ushort)Session.MyRouterContext.UDPPort);

#if LOG_MUCH_TRANSPORT
            Logging.LogTransport($"SSU {this}: X for signature {Request.X}.");
            Logging.LogTransport($"SSU {this}: Y for signature {Y.Key}.");
            Logging.LogTransport($"SSU {this}: Alice address for signature {baaddr}. Port {(BufLen)APort}.");
            Logging.LogTransport($"SSU {this}: Bob address for signature {Request.Address}. Port {bbport}.");
            Logging.LogTransport($"SSU {this}: Relay tag {(BufLen)RelayTag}. Signon time {(BufLen)ASignonTime}.");
#endif

            var signdata = new BufLen[] {
                Request.X, Y.Key,
                baaddr, BufUtils.To16BL(APort),
                Request.Address, bbport,
                BufUtils.To32BL(RelayTag), BufUtils.To32BL(ASignonTime)
            };

            var ok = I2PSignature.DoVerify(Session.RemoteRouter.SigningPublicKey, ASign, signdata);

            Logging.LogTransport($"SSU SessionCreatedState {Session.DebugId}: " +
                                 $"{Session.RemoteRouter.Certificate.SignatureType} " +
                                 $"signature check: {ok}");

            if (!ok)
            {
                throw new SignatureCheckFailureException("SSU SessionCreatedState recv sig check failure");
            }

            Logging.LogTransport("SSU SessionCreatedState: Session " + Session.DebugId + " established. Moving to Established state.");
            var next = new EstablishedState(Session);

            Session.ReportConnectionEstablished();

            NetDb.Inst.Statistics.SuccessfulConnect(Session.RemoteRouter.IdentHash);
            return(next);
        }
Exemplo n.º 4
0
        private void SendSessionCreated()
        {
            SendMessage(
                SSUHeader.MessageTypes.SessionCreated,
                Session.MyRouterContext.IntroKey,
                Session.MyRouterContext.IntroKey,
                (start, writer) =>
            {
                writer.Write(Y.Key);
                AAddr = Session.RemoteEP.Address.GetAddressBytes();
                writer.Write8((byte)AAddr.Length);
                writer.Write(AAddr);
                APort = BufUtils.Flip16((ushort)Session.RemoteEP.Port);
                writer.Write16(APort);
                writer.WriteFlip32(RelayTag);
                Session.SignOnTimeB = BufUtils.Flip32(SSUHost.SSUTime(DateTime.UtcNow));
                writer.Write32(Session.SignOnTimeB);

                var sign = I2PSignature.DoSign(Session.MyRouterContext.PrivateSigningKey,
                                               Request.X, Y.Key,
                                               new BufLen(AAddr), BufUtils.To16BL(APort),
                                               Request.Address, BufUtils.Flip16BL((ushort)Session.MyRouterContext.UDPPort),
                                               BufUtils.To32BL(RelayTag), BufUtils.To32BL(Session.SignOnTimeB));

                var signstart = new BufLen(writer);
                writer.Write(sign);
                var padding = BufUtils.Get16BytePadding(writer - signstart);
                writer.Write(BufUtils.RandomBytes(padding));

                var cipher       = new CbcBlockCipher(new AesEngine());
                var signcryptbuf = new BufLen(signstart, 0, writer - signstart);
                cipher.Init(true, Session.SharedKey.ToParametersWithIV(new BufLen(start, 16, 16)));
                cipher.ProcessBytes(signcryptbuf);
                return(true);
            });
        }
Exemplo n.º 5
0
        void Save(bool onlyupdated)
        {
            var created = 0;
            var updated = 0;
            var deleted = 0;

            var sw = new Stopwatch();

            sw.Start();

            RemoveOldRouterInfos();

            using (var s = GetStore())
            {
                foreach (var one in RouterInfos.ToArray())
                {
                    try
                    {
                        if (one.Value.Meta.Deleted)
                        {
                            if (one.Value.Meta.StoreIx > 0)
                            {
                                s.Delete(one.Value.Meta.StoreIx);
                            }
                            RouterInfos.TryRemove(one.Key, out _);
                            FloodfillInfos.TryRemove(one.Key, out _);
                            ++deleted;
                            continue;
                        }

                        if (!onlyupdated || (onlyupdated && one.Value.Meta.Updated))
                        {
                            var rec = new BufLen[]
                            {
                                BufUtils.To32BL((int)StoreRecordId.StoreIdRouterInfo),
                                new BufLen(one.Value.Router.ToByteArray())
                            };

                            if (one.Value.Meta.StoreIx > 0)
                            {
                                s.Write(rec, one.Value.Meta.StoreIx);
                                ++updated;
                            }
                            else
                            {
                                one.Value.Meta.StoreIx = s.Write(rec);
                                ++created;
                            }
                            one.Value.Meta.Updated = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.LogDebug("NetDb: Save: Store exception: " + ex.ToString());
                        one.Value.Meta.StoreIx = -1;
                    }
                }

                SaveConfig(s);
            }

            Logging.Log($"NetDb.Save( {( onlyupdated ? "updated" : "all" )} ): " +
                        $"{created} created, {updated} updated, {deleted} deleted.");

            Statistics.RemoveOldStatistics();
            UpdateSelectionProbabilities();

            sw.Stop();
            Logging.Log($"NetDB: Save: {sw.Elapsed}");
        }
Exemplo n.º 6
0
        private void SendFragmentedSessionConfirmed()
        {
            var ri       = new BufLen(Session.MyRouterContext.MyRouterInfo.ToByteArray());
            var rireader = new BufRefLen(ri);

            var datafragments = new List <BufLen>();

            while (rireader.Length > 0)
            {
                datafragments.Add(rireader.ReadBufLen(Math.Min(rireader.Length, 472)));
            }

            for (int i = 0; i < datafragments.Count; ++i)
            {
                Logging.LogTransport($"SSU {this}: {Session.RemoteEP} " +
                                     $"sending fragment {i + 1} of {datafragments.Count + 1}, [0x{datafragments[i].Length:X}] bytes.");

                SendMessage(
                    SSUHeader.MessageTypes.SessionConfirmed,
                    Session.MACKey,
                    Session.SharedKey,
                    (start, writer) =>
                {
                    writer.Write8((byte)((i << 4) + datafragments.Count + 1));
                    writer.WriteFlip16((ushort)datafragments[i].Length);
                    writer.Write(datafragments[i]);

                    return(true);
                });
            }

            SendMessage(
                SSUHeader.MessageTypes.SessionConfirmed,
                Session.MACKey,
                Session.SharedKey,
                (start, writer) =>
            {
                var frag = datafragments.Count;
                writer.Write8((byte)((frag << 4) + frag + 1));
                writer.WriteFlip16(0);

                Session.SignOnTimeA = BufUtils.Flip32(SSUHost.SSUTime(DateTime.UtcNow));
                writer.Write32(Session.SignOnTimeA);
                var padding = BufUtils.Get16BytePadding(Session.MyRouterContext.Certificate.SignatureLength + (writer - start));
                writer.Write(BufUtils.RandomBytes(padding));

                var baddr = new BufLen(Session.RemoteEP.Address.GetAddressBytes());

                var sign = I2PSignature.DoSign(Session.MyRouterContext.PrivateSigningKey,
                                               Request.X.Key, Request.Y.Key,
                                               Request.SCMessage.Address, Request.SCMessage.Port,
                                               baddr, BufUtils.Flip16BL((ushort)Session.RemoteEP.Port),
                                               Request.SCMessage.RelayTag, BufUtils.To32BL(Session.SignOnTimeA)
                                               );
                writer.Write(sign);

                Logging.LogTransport($"SSU {this}: {Session.RemoteEP} " +
                                     $"sending fragment {frag + 1} of {datafragments.Count + 1}, [0x{writer - start - SSUHeader.FIXED_HEADER_SIZE:X}] bytes.");

                return(true);
            });
        }