Esempio n. 1
0
        private static byte[] GenerateSymmetricKey(byte[] nodePublicKey, byte[] nodePrivateKey, byte[] ephemeralPublicKey, byte[] ephemeralPrivateKey)
        {
            if (nodePrivateKey == null && ephemeralPrivateKey == null)
            {
                throw new ArgumentNullException("nodePrivateKey", "Expected exactly one of nodePrivateKey or ephemeralPrivateKey to be null, but both were null.");
            }

            if (nodePrivateKey != null && ephemeralPrivateKey != null)
            {
                throw new ArgumentNullException("nodePrivateKey", "Expected exactly one of nodePrivateKey or ephemeralPrivateKey to be null, but both were provided.");
            }

            var privateKey = nodePrivateKey ?? ephemeralPrivateKey;
            var publicKey  = nodePrivateKey != null ? ephemeralPublicKey : nodePublicKey;

            var q = ScalarMult.Mult(privateKey, publicKey);

            var bytes  = new byte[q.Length + ephemeralPublicKey.Length + nodePublicKey.Length];
            var writer = new WriteBuffer(bytes, 0);

            writer.Write(q);
            writer.Write(ephemeralPublicKey);
            writer.Write(nodePublicKey);
            return(GenericHash.Hash(bytes, null, 32));
        }
Esempio n. 2
0
    public void Send(string op, Action <IWriteableBuffer> w = null)
    {
        var buff = new WriteBuffer();

        buff.Write(op);
        buff.Write(1); // the test player
        w.SC(buff);
        msgs.Enqueue(buff.Data);
    }
Esempio n. 3
0
 public void Write(WriteBuffer buffer)
 {
     foreach (var route in ReturnRoutes)
     {
         buffer.Write(route.EncryptedBytes);
     }
 }
Esempio n. 4
0
        public void Write(WriteBuffer buffer)
        {
            buffer.Write(SubstituteRouteCount);

            foreach (var newRoute in NewRoutes)
            {
                buffer.Write(newRoute.Item1);
                buffer.Write(newRoute.Item2.EncryptedBytes);
            }

            foreach (var verifier in Verifiers)
            {
                buffer.Write(verifier.Item1);
                buffer.Write(verifier.Item2);
            }
        }
Esempio n. 5
0
 public void SaveAll()
 {
     using (var w = new BinaryWriter(new FileStream(SaveFile, FileMode.Create)))
     {
         w.Write(replays.Count);
         var buff = new WriteBuffer();
         FC.ForEach(replays, (i, replay) => buff.Write(replay));
         w.Write(buff.Data);
         w.Close();
     }
 }
Esempio n. 6
0
        private WriteBuffer CloseTag()
        {
            if (this.writers.Count <= 1)
            {
                throw new SWFModellerException(
                          SWFModellerError.Internal,
                          "Call to CloseTag when no tag was open.");
            }

            WriteBuffer tagWriter = this.writers.Peek();

            tagWriter.Align8();
            byte[] tagData = tagWriter.Data;
            int    tagCode = (int)tagWriter.Tag;

#if DEBUG
            this.LogMessage("Body length of " + tagWriter.Tag.ToString() + " " + tagData.Length + " bytes on " + tagWriter.LogData, true);
#endif
            this.writers.Pop();

            tagWriter = this.writers.Peek();
            tagWriter.Align8();

            if (tagData.Length >= 63)
            {
                /* Long record header */
                int hdr = (tagCode << 6) | 0x3f;
                tagWriter.WriteUI16((uint)hdr);
                tagWriter.WriteSI32(tagData.Length);
            }
            else
            {
                /* Short record header */
                int hdr = (tagCode << 6) | tagData.Length;
                tagWriter.WriteUI16((uint)hdr);
            }

            tagWriter.Write(tagData, 0, tagData.Length);

            return(tagWriter);
        }
Esempio n. 7
0
        public void Write(WriteBuffer buffer)
        {
            buffer.Write(AddressTypeByte);

            if (AddressType == DestAddressType.DomainName)
            {
                buffer.Write((byte)DestAddress.Length);
            }

            buffer.Write(DestAddress);
            buffer.Write(DestPort);
            buffer.Write(RouteCount);

            foreach (var route in ReturnRoutes)
            {
                buffer.Write(route.EncryptedBytes);
            }
        }
Esempio n. 8
0
        bool WriteProtocolV1(MessageType msgType, ulong msgId, byte[] msg, ulong responseMsgId, int timeout)
        {
            if (Disposed)
            {
                return(false);
            }

            if (WriteBuffer.ShuttingDown)
            {
                return(false);
            }

            // Send the request packets
            lock (lock_sendQ)
            {
                // Split message into correct packet size
                int i    = 0;
                int left = msg?.Length ?? 0;

                byte[] pMsg = null;

                ushort totalPackets  = ((msg?.Length ?? 0) == 0) ? (ushort)1 : Convert.ToUInt16(Math.Ceiling((double)msg.Length / (double)msgBufferLength));
                ushort currentPacket = 1;

                while (true)
                {
                    if (WriteBuffer.ShuttingDown)
                    {
                        return(false);
                    }

                    pMsg = new byte[left > msgBufferLength ? msgBufferLength + protocolLength : left + protocolLength];

                    // Writing protocol header
                    var header = new RpcProtocolHeaderV1
                    {
                        MsgType       = msgType,
                        MsgId         = msgId,
                        CurrentPacket = currentPacket,
                        TotalPackets  = totalPackets,
                        PayloadSize   = msg?.Length ?? 0,
                        ResponseId    = responseMsgId
                    };
                    FastStructure.CopyTo(ref header, pMsg, 0);

                    if (left > msgBufferLength)
                    {
                        // Writing payload
                        if (msg != null && msg.Length > 0)
                        {
                            Buffer.BlockCopy(msg, i, pMsg, protocolLength, msgBufferLength);
                        }

                        left -= msgBufferLength;
                        i    += msgBufferLength;
                    }
                    else
                    {
                        // Writing last packet of payload
                        if (msg != null && msg.Length > 0)
                        {
                            Buffer.BlockCopy(msg, i, pMsg, protocolLength, left);
                        }

                        left = 0;
                    }

                    Statistics.StartWaitWrite();
                    var bytes = WriteBuffer.Write((ptr) =>
                    {
                        FastStructure.WriteBytes(ptr, pMsg, 0, pMsg.Length);
                        return(pMsg.Length);
                    }, 1000);

                    Statistics.WritePacket(bytes - protocolLength);

                    if (left <= 0)
                    {
                        break;
                    }
                    currentPacket++;
                }
            }

            return(true);
        }
Esempio n. 9
0
 public void Write(WriteBuffer buffer)
 {
     buffer.Write(Bytes);
 }
Esempio n. 10
0
        public ConnectPacketData GetConnectPacket()
        {
            var connectPacketData = new ConnectPacketData
            {
                AddressType  = _connection.AddressType,
                DestPort     = _connection.DestPort,
                RouteCount   = (uint)_connection.ReturnRoutes.Length,
                ReturnRoutes = // TODO
            };

            if (_connection.AddressType == DestAddressType.DomainName)
            {
                connectPacketData.DestDomainName = _connection.DestDomainName;
            }
            else
            {
                connectPacketData.DestIpAddress = _connection.DestIpAddress;
            }

            var bytesToEncrypt = new byte[PacketContent.MaxSendDataLen + 27];

            var buffer = new WriteBuffer(bytesToEncrypt, 0);

            buffer.Write(_connection.UserId);
            buffer.Write(_connection.ConnectionId);
            buffer.Write(_connection.NextSequenceId);
            buffer.Write((byte)PacketDataType.Connect);
            buffer.Write(connectPacketData);
            var paddingLength = bytesToEncrypt.Length - buffer.TotalWritten;

            buffer.Write(new byte[paddingLength]);

            var route     = GenerateRoute(_possibleNodes);
            var encrypted = SecretBox.Create(bytesToEncrypt, OneNonce, route.Nodes[2].SymmetricKey);

            var destIdAndDataLen = new byte[10];

            buffer.Buffer = destIdAndDataLen;
            buffer.Write((ulong)0);
            buffer.Write((ushort)(encrypted.Length - 16));

            var innerPacketBytes = new byte[bytesToEncrypt.Length + 67];

            buffer.Buffer = innerPacketBytes;

            buffer.Write(route.Nodes[2].Node.Id);
            buffer.Write(route.Nodes[2].EphemeralPublicKey);
            buffer.Write((byte)(route.Nodes[2].SymmetricKey[31] & 1));
            buffer.Write(StreamEncryption.EncryptChaCha20(destIdAndDataLen, ZeroNonce, route.Nodes[2].SymmetricKey));
            buffer.Write(encrypted);

            encrypted = SecretBox.Create(encrypted, OneNonce, route.Nodes[2].SymmetricKey);

            var destId = new byte[8];

            buffer.Buffer = destId;
            buffer.Write(route.Nodes[1].Node.Id);
        }