Exemplo n.º 1
0
        public void Write(List <byte> dest)
        {
            var buf = new List <byte>();

            foreach (var one in Mappings)
            {
                one.Key.Write(buf);
                buf.Add((byte)'=');
                one.Value.Write(buf);
                buf.Add((byte)';');
            }

            dest.AddRange(BitConverter.GetBytes(BufUtils.Flip16((ushort)buf.Count)));
            dest.AddRange(buf);
        }
Exemplo n.º 2
0
        private BufLen ReadLength()
        {
            InBufPos     = 0;
            DecodeBufPos = 0;

            while (InBufPos < 16)
            {
                if (MySocket.Available > 0)
                {
                    var len = MySocket.Receive(InBuf, InBufPos, 16 - InBufPos, SocketFlags.None);
                    if (len == 0)
                    {
                        throw new EndOfStreamEncounteredException();
                    }
                    InBufPos += len;
                }
                else
                {
                    Thread.Sleep(400);
                    if (!MySocket.Connected)
                    {
                        throw new EndOfStreamEncounteredException();
                    }
                }
            }

            Cipher.ProcessBytes(new BufLen(InBuf, 0, 16));
            BlockLength   = BufUtils.Flip16(InBuf, 0);
            NTCPDataSize  = BlockLength;
            DecodeBufPos += 16;

            //Logging.LogTransport( string.Format( "NTCPReader block length: {0} bytes [0x{0:X}]", BlockLength ) );

            if (BlockLength == 0)
            {
                // Time Sync
                var result = new BufLen(InBuf, 0, 16);
                BlockLength = -1;
                return(result);
            }

            return(ReadBlock());
        }
Exemplo n.º 3
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), (BufLen)APort,
                                               Request.Address, (BufLen)BufUtils.Flip16((ushort)Session.MyRouterContext.UDPPort),
                                               (BufLen)RelayTag, (BufLen)Session.SignOnTimeB);

                var signstart = new BufLen(writer);
                writer.Write(sign);
                var padding = BufUtils.Get16BytePadding(writer - signstart);
                writer.Write(BufUtils.Random(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);
            });
        }