public byte[] PublicKey()
        {
            using (MemoryStream mS = new MemoryStream(4096))
            {
                BincodingEncoder encoder = new BincodingEncoder(mS, "DH", 1);

                encoder.Encode(_keySize);
                encoder.Encode((byte)_group);

                switch (_group)
                {
                case DiffieHellmanGroupType.RFC3526:
                    encoder.Encode(_x.ToByteArray());
                    break;

                default:
                    encoder.Encode(_p.ToByteArray());
                    encoder.Encode(_g.ToByteArray());
                    encoder.Encode(_x.ToByteArray());
                    break;
                }

                return(mS.ToArray());
            }
        }
        public void WriteTo(Stream s)
        {
            BincodingEncoder encoder = new BincodingEncoder(s);

            encoder.Encode((byte)1); //version
            encoder.Encode(_name);
            encoder.Encode((byte)_status);
            encoder.Encode(_deliveredOn);
        }
Example #3
0
            public void WriteTo(Stream s)
            {
                BincodingEncoder encoder = new BincodingEncoder(s, "FI", 2);

                if (_filePath != null)
                {
                    encoder.Encode("file_path", _filePath);
                }

                encoder.Encode("file_metadata", _fileMetaData);
                encoder.Encode("state", (byte)_state);
                encoder.Encode("block_available", _blockAvailable);

                //signal end
                encoder.EncodeNull();
            }
        public int WriteMessage(byte[] data, int offset, int count)
        {
            lock (_lock)
            {
                //encrypt message data
                byte[] encryptedData;
                using (MemoryStream mS = new MemoryStream(count))
                {
                    using (MemoryStream src = new MemoryStream(data, offset, count))
                    {
                        _crypto.GenerateIV();
                        _crypto.Encrypt(src, mS);
                    }

                    encryptedData = mS.ToArray();
                }

                byte[] aeHmac;
                using (HMAC hmac = new HMACSHA256(_key))
                {
                    aeHmac = hmac.ComputeHash(encryptedData);
                }

                //write encrypted message data

                //seek to end of stream
                _index.Position = _index.Length;
                _data.Position  = _data.Length;

                //get message offset
                uint messageOffset = Convert.ToUInt32(_data.Position);

                //write data
                BincodingEncoder encoder = new BincodingEncoder(_data);

                encoder.Encode((byte)1); //version
                encoder.Encode(_crypto.IV);
                encoder.Encode(encryptedData);
                encoder.Encode(aeHmac);

                //write message offset to index stream
                _index.Write(BitConverter.GetBytes(messageOffset), 0, 4);

                //return message number
                return(Convert.ToInt32(_index.Position / 4) - 1);
            }
        }
        public void WriteTo(MessageStore store)
        {
            using (MemoryStream mS = new MemoryStream(128))
            {
                BincodingEncoder encoder = new BincodingEncoder(mS, "MI", 1);

                encoder.Encode((byte)_type);
                encoder.Encode(_messageDate);

                switch (_type)
                {
                case MessageType.Info:
                    encoder.Encode(_message);
                    break;

                case MessageType.TextMessage:
                case MessageType.InvitationMessage:
                    encoder.Encode(_sender);
                    encoder.Encode(_message);
                    encoder.Encode(_recipients);
                    break;

                case MessageType.SharedFileMetaData:
                    encoder.Encode(_sender);
                    encoder.Encode(_sharedFileMetaData);
                    break;

                default:
                    throw new NotSupportedException("MessageType not supported: " + _type);
                }

                byte[] messageData = mS.ToArray();

                if (_messageNumber == -1)
                {
                    _messageNumber = store.WriteMessage(messageData, 0, messageData.Length);
                }
                else
                {
                    store.UpdateMessage(_messageNumber, messageData, 0, messageData.Length);
                }
            }
        }
Example #6
0
            public void WriteTo(Stream s)
            {
                BincodingEncoder encoder = new BincodingEncoder(s, "BI", 7);

                encoder.Encode("type", (byte)_type);

                if (_networkNameOrPeerEmailAddress != null)
                {
                    encoder.Encode("network_name", _networkNameOrPeerEmailAddress);
                }

                if (_sharedSecret != null)
                {
                    encoder.Encode("shared_secret", _sharedSecret);
                }

                encoder.Encode("enable_tracking", _enableTracking);
                encoder.Encode("send_invitation", _sendInvitation);

                if (_invitationSender != null)
                {
                    encoder.Encode("invitation_sender", _invitationSender);
                }

                if (_invitationMessage != null)
                {
                    encoder.Encode("invitation_message", _invitationMessage);
                }

                encoder.Encode("network_status", (byte)_networkStatus);

                if (_hashedPeerEmailAddress != null)
                {
                    encoder.Encode("hashed_peer_email_address", _hashedPeerEmailAddress.Number);
                }

                if (_networkID != null)
                {
                    encoder.Encode("network_id", _networkID.Number);
                }

                if (_networkSecret != null)
                {
                    encoder.Encode("network_secret", _networkSecret.Number);
                }

                encoder.Encode("message_store_id", _messageStoreID);
                encoder.Encode("message_store_key", _messageStoreKey);

                encoder.Encode("group_image_date_modified", _groupImageDateModified);
                if (_groupImage != null)
                {
                    encoder.Encode("group_image", _groupImage);
                }

                encoder.Encode("mute", _mute);

                encoder.Encode("peer_certs", _peerCerts);
                encoder.Encode("shared_files", _sharedFiles);

                {
                    List <Bincoding> trackerList = new List <Bincoding>(_sharedFiles.Length);

                    foreach (Uri trackerURI in _trackerURIs)
                    {
                        trackerList.Add(Bincoding.GetValue(trackerURI.AbsoluteUri));
                    }

                    encoder.Encode("tracker_list", trackerList);
                }

                //signal end of settings
                encoder.EncodeNull();
            }
Example #7
0
        protected override void WritePlainTextTo(Stream s)
        {
            BincodingEncoder encoder = new BincodingEncoder(s, "BP", 7);

            //main settings

            //bitchat local port
            encoder.Encode("local_port", _localPort);

            //check CertificateRevocationList
            encoder.Encode("check_cert_revocation", _checkCertificateRevocationList);

            //upnp enabled
            encoder.Encode("enable_upnp", _enableUPnP);

            //enable invitation
            encoder.Encode("allow_inbound_invitations", _allowInboundInvitations);
            encoder.Encode("allow_only_local_inbound_invitations", _allowOnlyLocalInboundInvitations);

            //download folder
            if (_downloadFolder != null)
            {
                encoder.Encode("download_folder", _downloadFolder);
            }

            //local cert store
            if (_localCertStore != null)
            {
                encoder.Encode("local_cert_store", _localCertStore);
            }

            //profile image
            encoder.Encode("profile_image_date_modified", _profileImageDateModified);

            if (_profileImage != null)
            {
                encoder.Encode("profile_image", _profileImage);
            }

            //tracker urls
            {
                List <Bincoding> trackerList = new List <Bincoding>(_trackerURIs.Length);

                foreach (Uri trackerURI in _trackerURIs)
                {
                    trackerList.Add(Bincoding.GetValue(trackerURI.AbsoluteUri));
                }

                encoder.Encode("tracker_list", trackerList);
            }

            //bitchat info
            {
                List <Bincoding> bitChatInfoList = new List <Bincoding>(_bitChatInfoList.Length);

                foreach (BitChatInfo info in _bitChatInfoList)
                {
                    bitChatInfoList.Add(Bincoding.GetValue(info));
                }

                encoder.Encode("bitchat_info", bitChatInfoList);
            }

            //bootstrap dht nodes
            {
                List <Bincoding> dhtNodeList = new List <Bincoding>(_bootstrapDhtNodes.Length);

                using (MemoryStream mS = new MemoryStream())
                {
                    foreach (IPEndPoint nodeEP in _bootstrapDhtNodes)
                    {
                        mS.SetLength(0);
                        IPEndPointParser.WriteTo(nodeEP, mS);

                        dhtNodeList.Add(Bincoding.GetValue(mS.ToArray()));
                    }
                }

                encoder.Encode("dht_nodes", dhtNodeList);
            }

            //proxy settings

            //proxy type
            if (_proxy != null)
            {
                encoder.Encode("proxy_type", (byte)_proxy.Type);
            }

            //proxy address
            if (_proxyAddress != null)
            {
                encoder.Encode("proxy_address", _proxyAddress);
            }

            //proxy port
            encoder.Encode("proxy_port", _proxyPort);

            //proxy credentials
            if (_proxyCredentials != null)
            {
                encoder.Encode("proxy_user", _proxyCredentials.UserName);
                encoder.Encode("proxy_pass", _proxyCredentials.Password);
            }

            //generic client data
            if ((_clientData != null) && (_clientData.Length > 0))
            {
                encoder.Encode("client_data", _clientData);
            }

            //signal end of settings
            encoder.EncodeNull();
        }
        public void UpdateMessage(int number, byte[] data, int offset, int count)
        {
            lock (_lock)
            {
                //seek to index location
                int indexPosition = number * 4;

                if (indexPosition >= _index.Length)
                {
                    throw new IOException("Cannot read message from message store: message number out of range.");
                }

                _index.Position = indexPosition;

                //read message offset
                byte[] buffer = new byte[4];
                _index.Read(buffer, 0, 4);
                uint messageOffset = BitConverter.ToUInt32(buffer, 0);

                //seek to message offset
                _data.Position = messageOffset;

                //read data
                BincodingDecoder decoder = new BincodingDecoder(_data);
                byte[]           existingEncryptedData;

                switch (decoder.DecodeNext().GetByteValue()) //version
                {
                case 1:
                    decoder.DecodeNext();
                    existingEncryptedData = decoder.DecodeNext().Value;
                    break;

                default:
                    throw new IOException("Cannot read message from message store: message version not supported.");
                }

                //encrypt message data
                byte[] newEncryptedData;
                using (MemoryStream mS = new MemoryStream(count))
                {
                    using (MemoryStream src = new MemoryStream(data, offset, count))
                    {
                        _crypto.GenerateIV();
                        _crypto.Encrypt(src, mS);
                    }

                    newEncryptedData = mS.ToArray();
                }

                byte[] aeHmac;
                using (HMAC hmac = new HMACSHA256(_key))
                {
                    aeHmac = hmac.ComputeHash(newEncryptedData);
                }

                bool lengthIsInLimit = (newEncryptedData.Length <= existingEncryptedData.Length);

                if (lengthIsInLimit)
                {
                    //seek to message offset
                    _data.Position = messageOffset;

                    //overwrite new data
                    BincodingEncoder encoder = new BincodingEncoder(_data);

                    encoder.Encode((byte)1); //version
                    encoder.Encode(_crypto.IV);
                    encoder.Encode(newEncryptedData);
                    encoder.Encode(aeHmac);
                }
                else
                {
                    //seek to index location
                    _index.Position = number * 4;

                    //seek to end of data stream
                    _data.Position = _data.Length;

                    //get message offset
                    messageOffset = Convert.ToUInt32(_data.Position);

                    //write new data
                    BincodingEncoder encoder = new BincodingEncoder(_data);

                    encoder.Encode((byte)1); //version
                    encoder.Encode(_crypto.IV);
                    encoder.Encode(newEncryptedData);
                    encoder.Encode(aeHmac);

                    //overwrite message offset to index stream
                    _index.Write(BitConverter.GetBytes(messageOffset), 0, 4);
                }
            }
        }