Пример #1
0
        private void on_btnAdd_clicked(object sender, EventArgs e)
        {
            try {
                PublicKey result = PublicKey.Parse(txtPublicKey.Buffer.Text);
                if (Common.SHA512Str(result.Key) == Core.MyNodeID)
                    throw new Exception("Cannot add your own key!");

                tni = new TrustedNodeInfo(result);

                EditFriendDialog w = new EditFriendDialog (base.Dialog, ref tni);
                int editResult = w.Run ();

                if (editResult == (int)ResponseType.Ok)  {
                    base.Dialog.Respond(ResponseType.Ok);
                } else {
                    base.Dialog.Respond(ResponseType.Cancel);
                }
                base.Dialog.Destroy();
            }
            catch (Exception ex) {
                Gui.ShowMessageDialog(String.Format("Invalid public key: {0}", ex.Message),
                                      base.Dialog, Gtk.MessageType.Error, ButtonsType.Ok);
                base.Dialog.Respond(ResponseType.None);
                return;
            }
        }
Пример #2
0
        public bool Verify()
        {
            TrustedNodeInfo remoteNode = network.TrustedNodes[this.node.NodeID];

            byte[] buf = System.Text.Encoding.UTF8.GetBytes(CreateSignString());
            return(remoteNode.Crypto.VerifyData(buf, new SHA1CryptoServiceProvider(), signature));
        }
Пример #3
0
        private void ConnectIfNeeded()
        {
            int totalConnections = network.LocalConnections.Length;

            if (totalConnections < connectionCount)
            {
                for (int x = 0; x < (connectionCount - totalConnections); x++)
                {
                    if (nodeList.Count != 0)
                    {
                        TrustedNodeInfo node = (TrustedNodeInfo)GetNode();
                        try {
                            IDestination destination = node.FirstConnectableDestination;
                            if (destination != null)
                            {
                                ITransport transport = destination.CreateTransport(ConnectionType.NodeConnection);
                                network.ConnectTo(transport);
                            }
                        } catch (Exception ex) {
                            Core.LoggingService.LogError("AutoconnectManager: Error while trying to connect", ex);
                        }
                    }
                    else
                    {
                        // Nothing left, I give up! :(
                        network.ConnectingTo          -= connectingToHandler;
                        network.NewIncomingConnection -= incomingConnectionHandler;
                        Core.LoggingService.LogDebug("AutoconnectManager: Nothing left to connect to.");
                        return;
                    }
                }
            }
        }
Пример #4
0
        private bool IsGoodNode(TrustedNodeInfo node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            return(node.AllowConnect && node.AllowAutoConnect &&
                   node.FirstConnectableDestination != null);
        }
Пример #5
0
 public Message CreateAuthMessage(INodeConnection connection, TrustedNodeInfo messageTo)
 {
     Message p = new Message(network, MessageType.Auth);
     p.To = messageTo.NodeID;
     AuthInfo c = new AuthInfo();
     c.ProtocolVersion = Core.ProtocolVersion;
     c.NetworkName = network.NetworkName;
     c.NickName = network.LocalNode.NickName;
     p.Content = c;
     return p;
 }
Пример #6
0
        private void OnConnectionReady(LocalNodeConnection connection)
        {
            TrustedNodeInfo tnode = connection.NodeRemote.GetTrustedNode();

            lock (nodeList) {
                if (nodeList.Contains(tnode))
                {
                    nodeList.Remove(tnode);
                    nodeList.Sort(new NodeSuccessComparer());
                }
            }
        }
Пример #7
0
        public Message CreateAuthMessage(INodeConnection connection, TrustedNodeInfo messageTo)
        {
            Message p = new Message(network, MessageType.Auth);

            p.To = messageTo.NodeID;
            AuthInfo c = new AuthInfo();

            c.ProtocolVersion = Core.ProtocolVersion;
            c.NetworkName     = network.NetworkName;
            c.NickName        = network.LocalNode.NickName;
            p.Content         = c;
            return(p);
        }
Пример #8
0
        internal void ProcessMyKeyMessage(Node node, KeyInfo key)
        {
            bool acceptKey = network.RaiseReceivedKey(node, key);

            if (acceptKey)
            {
                TrustedNodeInfo tni = new TrustedNodeInfo();
                tni.Identifier = node.NickName;
                tni.PublicKey  = new PublicKey(key.Key);

                network.AddTrustedNode(tni);

                Core.Settings.SyncNetworkInfoAndSave();
            }
        }
Пример #9
0
        public PrivateChatSubpage(Network network, Node node)
            : base()
        {
            this.node = node;
            this.network = network;
            this.trustedNodeInfo = network.TrustedNodes[node.NodeID];

            if (trustedNodeInfo == null) {
                throw new Exception("Cannot have a private conversation with an untrusted node.");
            }

            base.userList.Parent.Visible = false;

            base.SendMessage += base_SendMessage;

            AddToChat(null, String.Format("Now talking with {0} ({1}).", trustedNodeInfo.Identifier, Common.FormatFingerprint(trustedNodeInfo.NodeID)));
            AddToChat(null, "This conversation is secure.");
        }
Пример #10
0
        public EditFriendDialog(Window parentWindow, ref TrustedNodeInfo tni)
            : base(parentWindow, "EditFriendDialog")
        {
            if (tni.Identifier != "") {
                nameLabel.Markup = "<b>" + tni.Identifier + "</b>";
            } else {
                nameLabel.Markup = "<b>[Unknown Nickname]</b>";
                tni.Identifier = tni.NodeID;
            }

            nodeIdLabel.Markup = "<span font=\"monospace\">" + Common.FormatFingerprint(tni.NodeID, 8) + "</span>";

            chkAllowProfile.Active = tni.AllowProfile;
            chkAllowNetworkInfo.Active = tni.AllowNetworkInfo;
            chkAllowSharedFiles.Active = tni.AllowSharedFiles;

            chkAllowConnect.Active = tni.AllowConnect;

            TreeViewColumn column;
            column = connectionsTreeView.AppendColumn("Protocol", new CellRendererText(), "text", 0);
            column = connectionsTreeView.AppendColumn("Address Details", new CellRendererText(), "text", 1);
            column.Expand = true;
            connectionsTreeView.AppendColumn("Supported", new CellRendererText(), "text", 2);
            connectionsTreeView.AppendColumn("Connectable", new CellRendererText(), "text", 3);

            ListStore addressListStore = new ListStore(typeof(string), typeof(string), typeof(string), typeof(string));
            foreach (IDestination destination in tni.Destinations) {
                addressListStore.AppendValues(destination.FriendlyTypeName, destination.ToString(), "True", destination.CanConnect.ToString());
            }
            foreach (DestinationInfo info in tni.DestinationInfos) {
                if (!info.Supported) {
                    addressListStore.AppendValues(info.FriendlyName, String.Join(", ", info.Data), "False", "False");
                }
            }
            connectionsTreeView.Model = addressListStore;

            keyTextView.Buffer.Text = tni.PublicKey.ToArmoredString();

            this.tni = tni;
        }
Пример #11
0
        internal void ProcessMyInfoMessage(Node currentNode, NodeInfo nodeInfo)
        {
            string oldNick = currentNode.NickName;

            currentNode.Bytes         = nodeInfo.Bytes;
            currentNode.ClientName    = nodeInfo.ClientName;
            currentNode.ClientVersion = nodeInfo.ClientVersion;
            currentNode.Email         = nodeInfo.Email;
            currentNode.Files         = nodeInfo.Files;
            //currentNode.HostInfo    = nodeInfo.HostInfo;
            currentNode.NickName   = nodeInfo.NickName;
            currentNode.RealName   = nodeInfo.RealName;
            currentNode.AvatarSize = nodeInfo.AvatarSize;

            TrustedNodeInfo tNode = network.TrustedNodes[currentNode.NodeID];

            tNode.Identifier = currentNode.NickName;

            tNode.DestinationInfos.Clear();
            tNode.DestinationInfos.AddRange(nodeInfo.DestinationInfos);

            Core.Settings.SyncNetworkInfoAndSave();

            /*
             * IDirectory userDirectory = network.Directory.GetSubdirectory(currentNode.NodeID);
             * if (userDirectory != null) {
             *      userDirectory.Delete();
             * }
             *
             * network.Directory.CreateSubdirectory(currentNode.NodeID, currentNode);
             */

            network.RaiseUpdateNodeInfo(oldNick, currentNode);

            network.AppendNetworkState(new NetworkState(nodeInfo));
        }
Пример #12
0
        private void ReceivedMessage(IAsyncResult result)
        {
            try {
                if (connectionState == ConnectionState.Disconnected)
                {
                    // Connection has been closed. Ignore the message.
                    Core.LoggingService.LogWarning("LocalNodeConnection: Ignored message received after connection was closed.");
                    return;
                }

                byte[] messageData = transport.EndReceiveMessage(result);

                if (messageData == null)
                {
                    return;
                }

                // Get the next one!
                ReceiveMessage();

                Message message     = null;
                string  messageFrom = null;
                try {
                    message = Message.Parse(transport.Network, messageData, out messageFrom);
                } catch (InvalidSignatureException ex) {
                    if (String.IsNullOrEmpty(messageFrom) || messageFrom == remoteNodeInfo.NodeID)
                    {
                        throw ex;
                    }
                    else
                    {
                        Core.LoggingService.LogWarning("Ignored message with invalid signature from {0}", messageFrom);
                        return;
                    }
                }

                ReceivedMessageInfo info = new ReceivedMessageInfo();
                info.Connection = this;
                info.Message    = message;
                Core.RaiseMessageReceived(info);

                if (remoteNodeInfo == null)
                {
                    KeyInfo key = (KeyInfo)message.Content;

                    RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                    provider.FromXmlString(key.Key);
                    string nodeID = FileFind.Common.SHA512Str(provider.ToXmlString(false));

                    if (nodeID.ToLower() == transport.Network.LocalNode.NodeID.ToLower())
                    {
                        throw new Exception("You cannot connect to yourself!");
                    }

                    if (!transport.Network.TrustedNodes.ContainsKey(nodeID))
                    {
                        bool acceptKey = transport.Network.RaiseReceivedKey(this, key);
                        if (acceptKey)
                        {
                            TrustedNodeInfo trustedNode = new TrustedNodeInfo();
                            trustedNode.Identifier = String.Format("[{0}]", nodeID);
                            trustedNode.PublicKey  = new PublicKey(key.Key);
                            transport.Network.AddTrustedNode(trustedNode);
                            Core.Settings.SyncNetworkInfoAndSave();
                        }
                        else
                        {
                            throw new ConnectNotTrustedException();
                        }
                    }

                    if (transport.Network.TrustedNodes[nodeID].AllowConnect == false)
                    {
                        throw new ConnectNotAllowedException(nodeID);
                    }

                    foreach (LocalNodeConnection connection in transport.Network.LocalConnections)
                    {
                        if (connection != this && connection.NodeRemote != null && connection.NodeRemote.NodeID == nodeID)
                        {
                            throw new Exception("Already connected!");
                        }
                    }

                    remoteNodeInfo = transport.Network.TrustedNodes[nodeID];
                    if (!transport.Network.Nodes.ContainsKey(RemoteNodeInfo.NodeID))
                    {
                        Node node = new Node(transport.Network, RemoteNodeInfo.NodeID);
                        node.NickName = RemoteNodeInfo.Identifier;
                        node.Verified = true;
                        transport.Network.AddNode(node);
                    }
                    this.NodeRemote = transport.Network.Nodes[RemoteNodeInfo.NodeID];

                    if (transport.Incoming == true)
                    {
                        this.SendMessage(transport.Network.MessageBuilder.CreateMyKeyMessage(null));
                    }
                    else
                    {
                        ConnectionState = ConnectionState.Authenticating;
                        RaiseConnectionInfoChanged();

                        Message m = transport.Network.MessageBuilder.CreateAuthMessage(this, RemoteNodeInfo);
                        SendMessage(m);
                    }
                }
                else
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(transport.Network.ProcessMessage), info);
                }
            } catch (Exception ex) {
                Disconnect(ex);
            }
        }
Пример #13
0
        public Message CreateMyInfoMessage(Node MessageTo)
        {
            Message p = new Message(network, MessageType.MyInfo);

            p.To = MessageTo.NodeID;
            TrustedNodeInfo t = network.TrustedNodes[MessageTo.NodeID];

            NodeInfo nodeInfo = new NodeInfo();

            nodeInfo.NodeID   = network.LocalNode.NodeID;
            nodeInfo.NickName = network.LocalNode.NickName;

            nodeInfo.AvatarSize = network.LocalNode.AvatarSize;

            if (MessageTo.IsConnectedLocally == true || t.AllowNetworkInfo == true)
            {
                nodeInfo.DestinationInfos = Core.DestinationManager.DestinationInfos;
            }
            if (t.AllowProfile == true)
            {
                nodeInfo.RealName = network.LocalNode.RealName;
                nodeInfo.Email    = network.LocalNode.Email;
            }
            nodeInfo.ClientName    = network.LocalNode.ClientName;
            nodeInfo.ClientVersion = network.LocalNode.ClientVersion;
            if (t.AllowSharedFiles == true)
            {
                nodeInfo.Bytes = network.LocalNode.Bytes;
                nodeInfo.Files = network.LocalNode.Files;
            }

            List <ConnectionInfo> connections = new List <ConnectionInfo>();
            List <ChatRoomInfo>   rooms       = new List <ChatRoomInfo>();
            List <MemoInfo>       memos       = new List <MemoInfo>();

            foreach (INodeConnection con in network.Connections)
            {
                if (con.NodeLocal != MessageTo & con.NodeRemote != MessageTo)
                {
                    if (con.ConnectionState == ConnectionState.Ready | con.ConnectionState == ConnectionState.Remote)
                    {
                        ConnectionInfo n = new ConnectionInfo();
                        Node           ConnectionSourceNode = con.NodeLocal;
                        Node           ConnectionDestNode   = con.NodeRemote;
                        n.SourceNodeID       = ConnectionSourceNode.NodeID;
                        n.SourceNodeNickname = ConnectionSourceNode.NickName;
                        n.DestNodeID         = ConnectionDestNode.NodeID;
                        n.DestNodeNickname   = ConnectionDestNode.NickName;
                        connections.Add(n);
                    }
                }
            }

            foreach (ChatRoom currentRoom in network.ChatRooms)
            {
                ChatRoomInfo roomInfo = new ChatRoomInfo();
                roomInfo.Id    = currentRoom.Id;
                roomInfo.Name  = currentRoom.Name;
                roomInfo.Users = new string[currentRoom.Users.Count];
                int x = 0;
                foreach (Node node in currentRoom.Users.Values)
                {
                    roomInfo.Users[x] = node.NodeID;
                    x++;
                }
                rooms.Add(roomInfo);
            }

            memos.AddRange(network.Memos.Where(m => m.Node.IsLocal).Select(m => new MemoInfo(m)));

            nodeInfo.KnownConnections = connections.ToArray();
            nodeInfo.KnownChatRooms   = rooms.ToArray();
            nodeInfo.KnownMemos       = memos.ToArray();

            p.Content = nodeInfo;
            return(p);
        }
Пример #14
0
        private void ReceivedMessage(IAsyncResult result)
        {
            try {
                if (connectionState == ConnectionState.Disconnected) {
                    // Connection has been closed. Ignore the message.
                    LoggingService.LogWarning("LocalNodeConnection: Ignored message received after connection was closed.");
                    return;
                }

                byte[] messageData = transport.EndReceiveMessage(result);

                if (messageData == null)
                    return;

                // Get the next one!
                ReceiveMessage();

                Message message = null;
                string messageFrom = null;
                try {
                    message = Message.Parse(transport.Network, messageData, out messageFrom);
                } catch (InvalidSignatureException ex) {
                    if (String.IsNullOrEmpty(messageFrom) || messageFrom == remoteNodeInfo.NodeID) {
                        throw ex;
                    } else {
                        LoggingService.LogWarning("Ignored message with invalid signature from {0}", messageFrom);
                        return;
                    }
                }

                ReceivedMessageInfo info = new ReceivedMessageInfo ();
                info.Connection = this;
                info.Message = message;
                Core.RaiseMessageReceived(info);

                if (remoteNodeInfo == null) {
                    KeyInfo key = (KeyInfo) message.Content;

                    RSACryptoServiceProvider provider = new RSACryptoServiceProvider ();
                    provider.FromXmlString (key.Key);
                    string nodeID = FileFind.Common.SHA512Str(provider.ToXmlString (false));

                    if (nodeID.ToLower () == transport.Network.LocalNode.NodeID.ToLower ()) {
                        throw new Exception ("You cannot connect to yourself!");
                    }

                    if (!transport.Network.TrustedNodes.ContainsKey(nodeID)) {
                        bool acceptKey = transport.Network.RaiseReceivedKey (this, key);
                        if (acceptKey) {
                            TrustedNodeInfo trustedNode = new TrustedNodeInfo();
                            trustedNode.Identifier = String.Format("[{0}]", nodeID);
                            trustedNode.PublicKey = new PublicKey(key.Key);
                            transport.Network.AddTrustedNode(trustedNode);
                            Core.Settings.SyncNetworkInfoAndSave();
                        } else {
                            throw new ConnectNotTrustedException ();
                        }
                    }

                    if (transport.Network.TrustedNodes[nodeID].AllowConnect == false)
                        throw new ConnectNotAllowedException(nodeID);

                    foreach (LocalNodeConnection connection in transport.Network.LocalConnections) {
                        if (connection != this && connection.NodeRemote != null && connection.NodeRemote.NodeID == nodeID)
                            throw new Exception ("Already connected!");
                    }

                    remoteNodeInfo = transport.Network.TrustedNodes[nodeID];
                    if (!transport.Network.Nodes.ContainsKey(RemoteNodeInfo.NodeID)) {
                        Node node = new Node (transport.Network, RemoteNodeInfo.NodeID);
                        node.NickName = RemoteNodeInfo.Identifier;
                        node.Verified = true;
                        transport.Network.AddNode(node);
                    }
                    this.NodeRemote = transport.Network.Nodes[RemoteNodeInfo.NodeID];

                    if (transport.Incoming == true) {
                        this.SendMessage(transport.Network.MessageBuilder.CreateMyKeyMessage (null));
                    } else {
                        ConnectionState = ConnectionState.Authenticating;
                        RaiseConnectionInfoChanged ();

                        Message m = transport.Network.MessageBuilder.CreateAuthMessage (this, RemoteNodeInfo);
                        SendMessage(m);
                    }

                } else {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(transport.Network.ProcessMessage), info);
                }
            } catch (Exception ex) {
                Disconnect(ex);
            }
        }
Пример #15
0
        internal void AddTrustedNode(TrustedNodeInfo info)
        {
            if (info.NodeID == Core.MyNodeID)
                throw new InvalidOperationException("Cannot add a TrustedNodeInfo with your own key!");

            this.trustedNodes.Add(info.NodeID, info);

            if (nodes.ContainsKey(info.NodeID)) {
                Node node = nodes[info.NodeID];
                if (!node.FinishedKeyExchange) {
                    node.CreateNewSessionKey();
                }
            }
        }
Пример #16
0
 public void AddPublicKey(PublicKey key)
 {
     TrustedNodeInfo info = new TrustedNodeInfo(key);
     this.trustedNodes.Add(info.NodeID, info);
     if (nodes.ContainsKey(info.NodeID)) {
         Node node = nodes[info.NodeID];
         if (!node.FinishedKeyExchange) {
             node.CreateNewSessionKey();
         }
     }
     Core.Settings.SyncNetworkInfo();
 }
Пример #17
0
 private bool IsNearby(TrustedNodeInfo info)
 {
     /*
     if (Core.ZeroconfManager != null) {
         foreach (NearbyNode nnode in Core.NearbyNodes) {
             if (nnode.NodeId == info.NodeID) {
                 return true;
             }
         }
     }
     */
     return false;
 }
Пример #18
0
        internal void ProcessMyKeyMessage(Node node, KeyInfo key)
        {
            bool acceptKey = network.RaiseReceivedKey (node, key);

            if (acceptKey) {
                TrustedNodeInfo tni = new TrustedNodeInfo();
                tni.Identifier = node.NickName;
                tni.PublicKey = new PublicKey(key.Key);

                network.AddTrustedNode(tni);

                Core.Settings.SyncNetworkInfoAndSave();
            }
        }