Ejemplo n.º 1
0
 private bool queryIsVerified(SignedQueryByFile signedQuery)
 {
     RSACryptoServiceProvider rsaWithPublicKeyOfRemoteUser = new RSACryptoServiceProvider();
     String contactingName = "didnt find";
     foreach (Friend f in ServerApp._user.Friends)
     {
         if (f.Uris.ElementAt(0).CompareTo(signedQuery.Query.ContactingServerUri.ElementAt(0)) == 0)
         {
             contactingName = f.Name;
         }
     }
     //MessageBox.Show(ServerApp._user.Username + "says that signedQuery.Query.Name=" + contactingName);
     UserEntry userToVerify = ServerApp._pkiCommunicator.GetVerifiedUserPublicKey(contactingName);
     if (userToVerify == null)
     {
         MessageBox.Show(ServerApp._user.Username + " : User to verify was null!");
         return false;
     }
     rsaWithPublicKeyOfRemoteUser.FromXmlString(userToVerify.PubKey);
     //MessageBox.Show("pubkey of user: "******" with key: " + userToVerify.PubKey);
     byte[] data = Encoding.Default.GetBytes(signedQuery.Query.ToString());
     return rsaWithPublicKeyOfRemoteUser.VerifyData(data, "SHA1", signedQuery.Signature);
 }
Ejemplo n.º 2
0
        public void shareObject(SignedQueryByFile signedQuery)
        {
            Boolean sendMessage = false;
            QueryByFile query = signedQuery.Query;

            MessageBox.Show(ServerApp._user.Username + " received a request to share " + query.Name);

            if (ServerApp._user.SentMessages.Contains(query.Id))
            {
                MessageBox.Show(ServerApp._user.Username + " : Message was already sent so the request was discarted!");
                return;
            }

            if (ServerApp._user.ReceivedFileMessages.Contains(signedQuery.Query))
            {
                MessageBox.Show(ServerApp._user.Username + " : Message was already received so the request was discarted!");
                return;
            }

            //only accepts messages from predecessors!
            if (!isPredecessor(signedQuery.Query))
            {
                MessageBox.Show(ServerApp._user.Username + " says " + whoSentQuery(signedQuery.Query)
                                                         + " is trying to screw the comunication! (whosentMethod)");
                return;
            }
            if (!queryIsVerified(signedQuery))
            {
                MessageBox.Show("Could not verify signed query.");
                return;
            }

            ServerApp._user.ReceivedFileMessages.Add(query);

            if (signedQuery.Query.ContactingServerUri.ElementAt(0).CompareTo(signedQuery.Query.Uris.ElementAt(0)) == 0)
            {
                //In this case the sender of the message is the same that originated it.
                //So if an attacker tries to do this he'll be the one receiving the responses in the end.
                //But the true requester will still get the right response.
                sendMessage = true;

            }
            else
            {
                sendMessage = consensus(query);
            }

            if (sendMessage)
            {
                keepOrForward(query);
                ServerApp._user.SentMessages.Add(signedQuery.Query.Id);
            }
        }
Ejemplo n.º 3
0
        private void keepOrForward(QueryByFile query)
        {
            RemoteAsyncShareObjectDelegate del;
            ServerToServerServices friend;
            Friend predecessor = null;
            QueryByFile q1;
            List<String> contacting = new List<string>();
            contacting.Add(ServerApp._myUri);
            if (ServerApp._user.Username[0] == query.Name[0])
            {
                //should store in redirection
                MessageBox.Show(ServerApp._user.Username + " will put uri on redirection list. (obj=now)");
                ServerApp._user.addRedirection(new RedirectionFile(query.Name, query.Uris.ElementAt(0)));
                return;
            }
            else
            {
                foreach (Friend node in ServerApp._user.Friends)
                    if (!node.SucessorSwarm)
                    {
                        predecessor = node;
                        break;
                    }
                if (predecessor == null)
                {
                    MessageBox.Show(ServerApp._user.Username + " says : Inconsistent routing table");
                    return;
                }
                if (predecessor.Name[0] > ServerApp._user.Username[0] && query.Name[0] > predecessor.Name[0])
                {
                    //should store in redirection
                    MessageBox.Show(ServerApp._user.Username + " will put uri on redirection list. (before>now && obj>before)");
                    ServerApp._user.addRedirection(new RedirectionFile(query.Name, query.Uris.ElementAt(0)));
                    return;
                }
                if (query.Name[0] > predecessor.Name[0] && query.Name[0] < ServerApp._user.Username[0])
                {
                    //should store in redirection
                    MessageBox.Show(ServerApp._user.Username + " will put uri on redirection list. (obj>before && obj<now)");
                    ServerApp._user.addRedirection(new RedirectionFile(query.Name, query.Uris.ElementAt(0)));
                    return;
                }
                //if(lower[0] >= ServerApp._user.Username[0])

                //should continue sending
                foreach (Friend f in ServerApp._user.Friends)
                {
                    if (f.Uris.ElementAt(0) != null && f.SucessorSwarm)
                    {
                        if (f.Uris.ElementAt(0).CompareTo(query.ContactingServerUri.ElementAt(0)) != 0)
                        {
                            MessageBox.Show(ServerApp._user.Username + " sending a request to share to " + f.Name);
                            friend = ((ServerToServerServices)Activator.GetObject(typeof(ServerToServerServices),
                                f.Uris.ElementAt(0) + "/" + ServicesNames.ServerToServerServicesName));

                            q1 = new QueryByFile(query.Name, query.Uris, contacting,
                                (ServerApp._user.Username[0] > query.LowestId[0]) ? query.LowestId : ServerApp._user.Username, query.Id);

                            byte[] data = Encoding.Default.GetBytes(q1.ToString());
                            byte[] signature = ServerApp._rsaProvider.SignData(data, "SHA1");
                            SignedQueryByFile signedForwardQuery = new SignedQueryByFile(q1, signature);

                            del = new RemoteAsyncShareObjectDelegate(friend.shareObject);
                            del.BeginInvoke(signedForwardQuery, null, null);
                        }
                    }
                }

            }
        }
Ejemplo n.º 4
0
        public void shareObject(ObjectFile file)
        {
            ServerToServerServices friend;
            RemoteAsyncShareObjectDelegate del;
            ServerApp._user.addObject(file);
            List<String> uri = new List<string>();
            List<String> contacting = new List<string>();

            contacting.Add(ServerApp._primaryURI);
            uri.Add(ServerApp._primaryURI);
            QueryByFile query = new QueryByFile(file.FileName, uri, contacting, ServerApp._user.Username,DateTime.Now);

            byte[] data = Encoding.Default.GetBytes(query.ToString());
            byte[] signature = ServerApp._rsaProvider.SignData(data, "SHA1");
            SignedQueryByFile signedQuery = new SignedQueryByFile(query, signature);

            MessageBox.Show(ServerApp._user.Username + " :  wants to share " + file.FileName);

            foreach (Friend f in ServerApp._user.Friends)
            {
                if (f.Uris.ElementAt(0) != null && f.SucessorSwarm)
                {
                    friend = ((ServerToServerServices)Activator.GetObject(typeof(ServerToServerServices),
                        f.Uris.ElementAt(0) + "/" + ServicesNames.ServerToServerServicesName));

                    MessageBox.Show(ServerApp._user.Username + " sends to " + f.Name);

                    del = new RemoteAsyncShareObjectDelegate(friend.shareObject);
                    del.BeginInvoke(signedQuery, null, null);
                }
            }
        }