Example #1
0
        public void SaveAsync(Bitmessage bm)
        {
            if (SentData == null)
            {
                throw new Exception("Payload.SaveAsync SentData == null");
            }
            if (String.IsNullOrEmpty(Command) || (Command.Length > 30))
            {
                throw new Exception("Payload.SaveAsync Command incorrect =" + Command);
            }
            if (InventoryVector == null)
            {
                throw new Exception("Payload.SaveAsync InventoryVector==null");
            }
            if (InventoryVector.Length != 32)
            {
                throw new Exception("Payload.SaveAsync InventoryVector.Length != 32");
            }

            if (bm.MemoryInventory.Insert(InventoryVector))
            {
                bm.DB.InsertOrReplaceAsync(this);
            }

            bm.OnNewPayload(this);
        }
Example #2
0
 internal void SendAsync(Bitmessage bitmessage)
 {
     LastPubkeySendTime = DateTime.UtcNow;
     SaveAsync(bitmessage.DB);
     new Task(() =>
              new Payload(Command, SentData).SaveAsync(bitmessage)
              ).Start();
 }
 internal void SendAsync(Bitmessage bitmessage)
 {
     LastPubkeySendTime = DateTime.UtcNow;
     SaveAsync(bitmessage.DB);
     new Task(() =>
         new Payload(Command, SentData).SaveAsync(bitmessage)
         ).Start();
 }
        public void SaveAsync(Bitmessage bm)
        {
            if (SentData == null)
                throw new Exception("Payload.SaveAsync SentData == null");
            if (String.IsNullOrEmpty(Command) || (Command.Length > 30))
                throw new Exception("Payload.SaveAsync Command incorrect =" + Command);
            if (InventoryVector == null)
                throw new Exception("Payload.SaveAsync InventoryVector==null");
            if (InventoryVector.Length != 32)
                throw new Exception("Payload.SaveAsync InventoryVector.Length != 32");

            if(bm.MemoryInventory.Insert(InventoryVector))
                bm.DB.InsertOrReplaceAsync(this);

            bm.OnNewPayload(this);
        }
        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;
            }
        }
Example #6
0
        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;
            }
        }
 public Broadcast(Bitmessage bm)
 {
     _bm = bm;
 }
Example #8
0
 public Broadcast(Bitmessage bm)
 {
     _bm = bm;
 }
Example #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;
            }
        }
Example #10
0
 public Msg(Bitmessage bm)
 {
     _bm = bm;
 }
Example #11
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;
            }
        }
Example #12
0
 public Msg(Bitmessage bm)
 {
     _bm = bm;
 }