Inheritance: ICanBeSent, IComparable
        public Version(Payload payload)
        {
            if (payload.Length < 83)
                throw new Exception("Version.Length<83 Length=" + payload.Length);

            int pos = 0;

            Value = payload.SentData.ReadInt32(ref pos);
            Services = payload.SentData.ReadUInt64(ref pos);
            Timestamp = payload.SentData.ReadUInt64(ref pos);

            AddrRecvService = payload.SentData.ReadUInt64(ref pos);
            AddrRecvIpPrefix = payload.SentData.ReadBytes(ref pos, 12);
            AddrRecvIp = payload.SentData.ReadUInt32(ref pos);
            AddrRecvPort = payload.SentData.ReadUInt16(ref pos);

            AddrFromService = payload.SentData.ReadUInt64(ref pos);
            AddrFromIpPrefix = payload.SentData.ReadBytes(ref pos, 12);
            AddrFromIp = payload.SentData.ReadUInt32(ref pos);
            AddrFromPort = payload.SentData.ReadUInt16(ref pos);

            Nonce = payload.SentData.ReadUInt64(ref pos);
            UserAgent = payload.SentData.ReadVarStr(ref pos);
            StreamNumbers = payload.SentData.ReadVarIntList(ref pos);
        }
 public GetPubkey(Payload payload)
 {
     int pos = payload.FirstByteAfterTime;
     Version = payload.SentData.ReadVarInt(ref pos);
     Stream = payload.SentData.ReadVarInt(ref pos);
     PubKeyHash = payload.SentData.ReadBytes(ref pos, 20);
 }
 internal void OnBitmessageNewPayload(Payload payload)
 {
     if (_nodeConnectionInventory.Exists(payload.InventoryVector)) return;
     _nodeConnectionInventory.Insert(payload.InventoryVector);
     Send(new Inv(payload.InventoryVector));
 }
        public Broadcast(Bitmessage bm, Payload payload)
        {
            Status = Status.Invalid;
            try
            {
                int pos = payload.FirstByteAfterTime;
                Version = payload.SentData.ReadVarInt(ref pos);

                if (Version == 2)
                {
                    _inventoryVector = payload.InventoryVector;

                    Stream = payload.SentData.ReadVarInt(ref pos);
                    byte[] encrypted = payload.SentData.ReadBytes(ref pos, payload.Length - pos);
                    byte[] decryptedData = null;
                    Pubkey encryptionKey = null;
                    foreach (Pubkey subscriptionKey in bm.Subscriptions(Stream4DB))
                    {
                        if (subscriptionKey.Stream != _stream) continue;
                        try
                        {
                            decryptedData = subscriptionKey.DecryptAes256Cbc4Broadcast(encrypted);
                            encryptionKey = subscriptionKey;
                        }
                            // ReSharper disable EmptyGeneralCatchClause
                        catch
                        {
                        } // ReSharper restore EmptyGeneralCatchClause

                        if (decryptedData != null)
                            break;
                    }

                    if ((decryptedData == null) || (encryptionKey == null))
                    {
                        Status = Status.Encrypted;
                        return;
                    }

                    if (encryptionKey.SubscriptionIndex < int.MaxValue)
                    {
                        encryptionKey.SubscriptionIndex += 1;
                        encryptionKey.SaveAsync(bm.DB).Wait();
                    }

                    pos = 0;
                    /*var signedBroadcastVersion = */
                    decryptedData.ReadVarInt(ref pos);
                    Pubkey keyFromMsg = new Pubkey(decryptedData, ref pos);
                    if (!encryptionKey.Hash.SequenceEqual(keyFromMsg.Hash))
                        return;
                    Key = encryptionKey.Name;
                    EncodingType = (EncodingType) decryptedData.ReadVarInt(ref pos);
                    decryptedData.ReadVarStrSubjectAndBody(ref pos, out _subject, out _body);

                    int posOfEndMsg = pos;
                    UInt64 signatureLength = decryptedData.ReadVarInt(ref pos);
                    byte[] signature = decryptedData.ReadBytes(ref pos, (int) signatureLength);

                    byte[] data = new byte[posOfEndMsg];
                    Buffer.BlockCopy(decryptedData, 0, data, 0, posOfEndMsg);

                    if (data.ECDSAVerify(encryptionKey.SigningKey, signature))
                        Status = Status.Valid;
                }
            }
            catch
            {
                Status = Status.Invalid;
            }
        }
        private Payload GetPayload()
        {
            lock(_lock4GetPayload)
            if (_payload == null)
            {
                if (_version == 2)
                {
                    PrivateKey privkey = PrivateKey.GetPrivateKey(_bm.DB, Key);
                    if (privkey == null) throw new Exception("PrivateKey not found");

                    MemoryStream payload = new MemoryStream(1000 + Subject.Length + Body.Length); // TODO realy 1000?
                    Random rnd = new Random();
                    ulong dt = DateTime.UtcNow.ToUnix() + (ulong)rnd.Next(600) - 300;

                    payload.Write(dt);
                    payload.WriteVarInt(Version);
                    payload.WriteVarInt(Stream);

                    MemoryStream dataToEncrypt = new MemoryStream(1000 + Subject.Length + Body.Length); // TODO realy 1000?
                    dataToEncrypt.WriteVarInt(Version);

                    byte[] publicAddress = privkey.GetPayload4Broadcast();
                    dataToEncrypt.Write(publicAddress, 0, publicAddress.Length);

                    Byte encodingType = (byte)EncodingType;
                    dataToEncrypt.Write(encodingType);
                    dataToEncrypt.WriteVarStr("Subject:" + Subject + "\nBody:" + Body);

                    byte[] signature = privkey.Sign(dataToEncrypt.ToArray());

                    dataToEncrypt.WriteVarInt((UInt64)signature.Length);
                    dataToEncrypt.Write(signature, 0, signature.Length);

                    var privEncryptionKey = privkey.Sha512VersionStreamHashFirst32();
                    var pubEncryptionKey = ECDSA.PointMult(privEncryptionKey);

                    byte[] bytesToEncrypt = dataToEncrypt.ToArray();
                    byte[] encrypt = ECDSA.Encrypt(bytesToEncrypt, pubEncryptionKey);

                    payload.Write(encrypt, 0, encrypt.Length);

                    _payload = new Payload("broadcast", ProofOfWork.AddPow(payload.ToArray()));
                }
            }
            return _payload;
        }
        public void OnNewPayload(Payload payload)
        {
            lock (_nodeConnections)
                for (int i = _nodeConnections.Count - 1; i >= 0; --i)
                    if (_nodeConnections[i].Connected)
                        _nodeConnections[i].OnBitmessageNewPayload(payload);
                    else
                        _nodeConnections.RemoveAt(i);

            Payload.EventHandler handler = NewPayload;
            if (handler != null) handler(payload);
        }
Beispiel #7
0
        internal void Send()
        {
            Payload p = GetPayload();

            p.SaveAsync(_bm);
        }
Beispiel #8
0
        public Msg(Bitmessage bm, Payload payload)
        {
            Status = Status.Invalid;
            try
            {
                int pos = payload.FirstByteAfterTime;
                _inventoryVector = payload.InventoryVector;

                Stream = payload.SentData.ReadVarInt(ref pos);

                byte[] encrypted = payload.SentData.ReadBytes(ref pos, payload.Length - pos);

                // TODO Check ask data

                byte[]     decryptedData   = null;
                PrivateKey myEncryptionKey = null;

                foreach (PrivateKey myKey in bm.ListMyAddresses())
                {
                    if (myKey.Stream != _stream)
                    {
                        continue;
                    }
                    try
                    {
                        decryptedData   = myKey.DecryptAES256CBC4Msg(encrypted);
                        myEncryptionKey = myKey;
                    }
                    // ReSharper disable EmptyGeneralCatchClause
                    catch
                    {
                    }                     // ReSharper restore EmptyGeneralCatchClause

                    if (decryptedData != null)
                    {
                        break;
                    }
                }

                if ((decryptedData == null) || (myEncryptionKey == null))
                {
                    Status = Status.Encrypted;
                    return;
                }

                pos = 0;

                Version = decryptedData.ReadVarInt(ref pos);
                var senderKey = new Pubkey(decryptedData, ref pos);

                if (!decryptedData.ReadBytes(ref pos, 20).SequenceEqual(myEncryptionKey.Hash))
                {
                    //print 'The original sender of this message did not send it to you. Someone is attempting a Surreptitious Forwarding Attack.'
                    //print 'See: http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html'
                    //print 'your toRipe:', toRipe.encode('hex')
                    //print 'embedded destination toRipe:', decryptedData[readPosition:readPosition + 20].encode('hex')
                    return;
                }

                KeyTo   = myEncryptionKey.Name;
                KeyFrom = senderKey.Name;

                EncodingType = (EncodingType)decryptedData.ReadVarInt(ref pos);
                decryptedData.ReadVarStrSubjectAndBody(ref pos, out _subject, out _body);

                UInt64 askDataLength = decryptedData.ReadVarInt(ref pos);
                _askData = decryptedData.ReadBytes(ref pos, (int)askDataLength);

                int    posOfEndMsg     = pos;
                UInt64 signatureLength = decryptedData.ReadVarInt(ref pos);
                byte[] signature       = decryptedData.ReadBytes(ref pos, (int)signatureLength);

                var data = new byte[posOfEndMsg];
                Buffer.BlockCopy(decryptedData, 0, data, 0, posOfEndMsg);

                if (data.ECDSAVerify(senderKey.SigningKey, signature))
                {
                    Status = Status.Valid;
                    senderKey.SaveAsync(bm.DB);
                }
            }
            catch
            {
                Status = Status.Invalid;
            }
        }
Beispiel #9
0
        public Msg(Bitmessage bm, Payload payload)
        {
            Status = Status.Invalid;
            try
            {
                int pos = payload.FirstByteAfterTime;
                _inventoryVector = payload.InventoryVector;

                Stream = payload.SentData.ReadVarInt(ref pos);

                byte[] encrypted = payload.SentData.ReadBytes(ref pos, payload.Length - pos);

                // TODO Check ask data

                byte[] decryptedData = null;
                PrivateKey myEncryptionKey = null;

                foreach (PrivateKey myKey in bm.ListMyAddresses())
                {
                    if (myKey.Stream != _stream) continue;
                    try
                    {
                        decryptedData = myKey.DecryptAES256CBC4Msg(encrypted);
                        myEncryptionKey = myKey;
                    }
                        // ReSharper disable EmptyGeneralCatchClause
                    catch
                    {
                    } // ReSharper restore EmptyGeneralCatchClause

                    if (decryptedData != null)
                        break;
                }

                if ((decryptedData == null) || (myEncryptionKey == null))
                {
                    Status = Status.Encrypted;
                    return;
                }

                pos = 0;

                Version = decryptedData.ReadVarInt(ref pos);
                var senderKey = new Pubkey(decryptedData, ref pos);

                if (!decryptedData.ReadBytes(ref pos, 20).SequenceEqual(myEncryptionKey.Hash))
                    //print 'The original sender of this message did not send it to you. Someone is attempting a Surreptitious Forwarding Attack.'
                    //print 'See: http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html'
                    //print 'your toRipe:', toRipe.encode('hex')
                    //print 'embedded destination toRipe:', decryptedData[readPosition:readPosition + 20].encode('hex')
                    return;

                KeyTo = myEncryptionKey.Name;
                KeyFrom = senderKey.Name;

                EncodingType = (EncodingType) decryptedData.ReadVarInt(ref pos);
                decryptedData.ReadVarStrSubjectAndBody(ref pos, out _subject, out _body);

                UInt64 askDataLength = decryptedData.ReadVarInt(ref pos);
                _askData = decryptedData.ReadBytes(ref pos, (int) askDataLength);

                int posOfEndMsg = pos;
                UInt64 signatureLength = decryptedData.ReadVarInt(ref pos);
                byte[] signature = decryptedData.ReadBytes(ref pos, (int) signatureLength);

                var data = new byte[posOfEndMsg];
                Buffer.BlockCopy(decryptedData, 0, data, 0, posOfEndMsg);

                if (data.ECDSAVerify(senderKey.SigningKey, signature))
                {
                    Status = Status.Valid;
                    senderKey.SaveAsync(bm.DB);
                }
            }
            catch
            {
                Status = Status.Invalid;
            }
        }
Beispiel #10
0
        private Payload GetPayload()
        {
            lock (_lock4GetPayload)
                if (_payload == null)
                {
                    PrivateKey myPrivkeyFrom = PrivateKey.GetPrivateKey(_bm.DB, KeyFrom);
                    if (myPrivkeyFrom == null) throw new Exception("PrivateKey not found");

                    Pubkey pubkeyTo = Pubkey.Find(_bm.DB, KeyTo); // TODO Получать ключ, если его ещё нет
                    if (pubkeyTo == null) throw new Exception("Pubkey not found");

                    var payload = new MemoryStream(1000 + Subject.Length + Body.Length); // TODO realy 1000?
                    var rnd = new Random();
                    ulong dt = DateTime.UtcNow.ToUnix() + (ulong) rnd.Next(600) - 300;

                    payload.Write(dt);
                    payload.WriteVarInt(Stream);

                    var dataToEncrypt = new MemoryStream(1000 + Subject.Length + Body.Length); // TODO realy 1000?
                    dataToEncrypt.WriteVarInt(Version);

                    byte[] publicAddress = myPrivkeyFrom.GetPayload4Broadcast();
                    dataToEncrypt.Write(publicAddress, 0, publicAddress.Length);

                    dataToEncrypt.Write(pubkeyTo.Hash, 0, 20);

                    var encodingType = (byte) EncodingType;
                    dataToEncrypt.Write(encodingType);
                    dataToEncrypt.WriteVarStr("Subject:" + Subject + "\nBody:" + Body);

                    byte[] askMsg = PayloadOfAskData().GetFullMsg();
                    dataToEncrypt.WriteVarInt((UInt64)askMsg.Length);
                    dataToEncrypt.Write(askMsg, 0, askMsg.Length);

                    byte[] signature = myPrivkeyFrom.Sign(dataToEncrypt.ToArray());

                    //Debug.WriteLine("data=" + dataToEncrypt.ToArray().ToHex());
                    //Debug.WriteLine("SigningKey=" + myPrivkeyFrom.SigningKey.ToHex());
                    //Debug.WriteLine("signature=" + signature.ToHex());

                    dataToEncrypt.WriteVarInt((UInt64)signature.Length);
                    dataToEncrypt.Write(signature, 0, signature.Length);

                    byte[] bytesToEncrypt = dataToEncrypt.ToArray();
                    byte[] encrypt = ECDSA.Encrypt(bytesToEncrypt, pubkeyTo.EncryptionKey);

                    payload.Write(encrypt, 0, encrypt.Length);

                    _payload = new Payload("msg", ProofOfWork.AddPow(payload.ToArray()));
                }
            return _payload;
        }