Beispiel #1
0
        private void CheckCertificateRevocationListAsync(object state)
        {
            SecureChannelStream stream = null;

            try
            {
                stream = state as SecureChannelStream;

                stream.RemotePeerCertificate.VerifyRevocationList();
            }
            catch (InvalidCertificateException ex)
            {
                if (stream != null)
                {
                    try
                    {
                        stream.Dispose();
                    }
                    catch
                    { }
                }

                VirtualPeerHasRevokedCertificate(this, ex);
            }
            catch
            { }
        }
Beispiel #2
0
        public void GenerateNewUserId()
        {
            _userId       = SecureChannelStream.GenerateUserId(SecureChannelStream.GetPublicKeyFromPrivateKey(_privateKey));
            _maskedUserId = null; //will be generated auto in timer thread

            //trigger announce for new userId
            if (_userIdAnnounceTimer != null)
            {
                _userIdAnnounceTimer.Change(1000, USER_ID_ANNOUNCE_INTERVAL);
            }
        }
Beispiel #3
0
        public MeshNetwork CreatePrivateChat(BinaryNumber peerUserId, string peerName, bool localNetworkOnly, string invitationMessage)
        {
            //use random userId for each independent chat network for privacy reasons
            BinaryNumber randomUserId = SecureChannelStream.GenerateUserId(SecureChannelStream.GetPublicKeyFromPrivateKey(_privateKey));
            MeshNetwork  network      = new MeshNetwork(_connectionManager, randomUserId, peerUserId, peerName, localNetworkOnly, invitationMessage);

            lock (_networks)
            {
                _networks.Add(network.NetworkId, network);
            }

            return(network);
        }
Beispiel #4
0
        public void JoinNetwork(string peerID, SecureChannelStream peerStream, bool checkCertificateRevocationList)
        {
            if (_type == BitChatNetworkType.PrivateChat)
            {
                if (!peerStream.RemotePeerCertificate.IssuedTo.EmailAddress.Equals(_peerEmailAddress))
                {
                    throw new BitChatException("User with another email address '" + peerStream.RemotePeerCertificate.IssuedTo.EmailAddress.Address + " [" + peerStream.RemotePeerEP.Address.ToString() + "]' trying to join private chat.");
                }

                _peerName = peerStream.RemotePeerCertificate.IssuedTo.Name;
            }

            peerID = peerID.ToLower();

            VirtualPeer vPeer;
            bool        peerAdded = false;

            lock (_virtualPeers)
            {
                if (_virtualPeers.ContainsKey(peerID))
                {
                    vPeer = _virtualPeers[peerID];
                }
                else
                {
                    vPeer = new VirtualPeer(peerStream.RemotePeerCertificate, this);
                    _virtualPeers.Add(peerID, vPeer);

                    peerAdded = true;
                }
            }

            if (peerAdded)
            {
                VirtualPeerAdded(this, vPeer);
            }

            vPeer.AddStream(peerStream);

            if (checkCertificateRevocationList)
            {
                //start async revocation list check process
                ThreadPool.QueueUserWorkItem(CheckCertificateRevocationListAsync, peerStream);
            }
        }
Beispiel #5
0
            public void AddStream(SecureChannelStream stream)
            {
                if (!_peerCert.IssuedTo.EmailAddress.Address.Equals(stream.RemotePeerCertificate.IssuedTo.EmailAddress.Address, StringComparison.CurrentCultureIgnoreCase))
                {
                    throw new BitChatException("Secure stream certificate email address doesn't match with existing peer email address.");
                }

                lock (_streamList)
                {
                    if (_streamList.Count > 0)
                    {
                        if (!_peerCert.Equals(stream.RemotePeerCertificate))
                        {
                            throw new BitChatException("Secure stream certificates doesn't match with existing peer secure stream certificate.");
                        }
                    }
                    else
                    {
                        _peerCert = stream.RemotePeerCertificate;
                    }

                    _streamList.Add(stream);
                }

                lock (_readThreadList)
                {
                    Thread readThread = new Thread(ReadPacketAsync);
                    readThread.IsBackground = true;

                    _readThreadList.Add(readThread);

                    readThread.Start(stream);
                }

                lock (this)
                {
                    _isOnline = true;

                    if (StreamStateChanged != null)
                    {
                        StreamStateChanged(this, EventArgs.Empty);
                    }
                }
            }
Beispiel #6
0
        public MeshNetwork CreateGroupChat(string networkName, string sharedSecret, bool localNetworkOnly)
        {
            //use random userId for each independent chat network for privacy reasons
            BinaryNumber randomUserId = SecureChannelStream.GenerateUserId(SecureChannelStream.GetPublicKeyFromPrivateKey(_privateKey));
            MeshNetwork  network      = new MeshNetwork(_connectionManager, randomUserId, networkName, sharedSecret, localNetworkOnly);

            lock (_networks)
            {
                if (_networks.ContainsKey(network.NetworkId))
                {
                    network.Dispose();
                    throw new MeshException("Mesh network for group chat '" + network.NetworkName + "' already exists.");
                }

                _networks.Add(network.NetworkId, network);
            }

            return(network);
        }
Beispiel #7
0
            private void ReadPacketAsync(object state)
            {
                SecureChannelStream stream = state as SecureChannelStream;

                try
                {
                    byte[]       buffer = new byte[MAX_BUFFER_SIZE];
                    MemoryStream mS     = new MemoryStream(buffer);
                    int          dataLength;

                    while (true)
                    {
                        OffsetStream.StreamRead(stream, buffer, 0, 2);
                        dataLength = BitConverter.ToUInt16(buffer, 0) + 1;

                        mS.SetLength(dataLength);
                        mS.Position = 0;

                        OffsetStream.StreamRead(stream, buffer, 0, dataLength);

                        try
                        {
                            PacketReceived(this, mS, stream.RemotePeerEP);
                        }
                        catch
                        { }
                    }
                }
                catch (SecureChannelException ex)
                {
                    if (_network.VirtualPeerSecureChannelException != null)
                    {
                        _network.VirtualPeerSecureChannelException(_network, ex);
                    }
                }
                catch (Exception ex)
                {
                    Debug.Write("VirtualPeer.ReadPacketAsync", ex);
                }
                finally
                {
                    int totalStreamsAvailable;

                    lock (_streamList)
                    {
                        _streamList.Remove(stream);
                        totalStreamsAvailable = _streamList.Count;
                    }

                    lock (_readThreadList)
                    {
                        _readThreadList.Remove(Thread.CurrentThread);
                    }

                    if (!_disposing)
                    {
                        lock (this)
                        {
                            if (totalStreamsAvailable == 0)
                            {
                                _isOnline = false;
                            }

                            if (StreamStateChanged != null)
                            {
                                StreamStateChanged(this, EventArgs.Empty);
                            }
                        }
                    }

                    stream.Dispose();
                }
            }