Beispiel #1
0
        public int Auth(bool listen, bool enrollment)
        {
            IConnection     container = this;
            int             retval    = 99;
            KeyValueMessage keyval;
            List <string>   authtypes = new List <string>();

            // authtypes.Add("NONE");
            authtypes.Add("RSA");

            string authstring = "";

            foreach (string entry in authtypes)
            {
                if (authstring == "")
                {
                    authstring = entry;
                }
                else
                {
                    authstring = authstring + " " + entry;
                }
            }

            keyval = new KeyValueMessage("Control.PreAuth");
            keyval.Add("node.uuid", Runner.Network.Node.UUID.ToString());
            keyval.Add("auth.types", authstring);
            keyval.Add("version", SimpleMesh.Service.Utility.Version);
            if (listen == false)
            {
                container.Send(keyval);
            }
            bool end;

            end = false;
            bool            error = false;
            IMessage        Recieved;
            bool            uuid         = false;
            bool            authreceived = false;
            bool            enrolling    = false;
            KeyValueMessage messages;
            List <string>   typelist;

            typelist = new List <string>();
            string conntype;
            Dictionary <string, string> Parameters = new Dictionary <string, string>();

            while (end == false)
            {
                Recieved = container.Receive(true);
                if (Recieved.Type.Substring(0, 6) != "Error.")
                {
                    switch (Recieved.Type)
                    {
                    case "Control.PreAuth":
                        messages = new KeyValueMessage(Recieved);
                        foreach (KeyValuePair <string, string> keyn in messages.Data)
                        {
                            switch (keyn.Key)
                            {
                            case "node.uuid":
                                uuid = true;
                                break;

                            case "auth.types":
                                authreceived = true;
                                string[] types = keyn.Value.Split(' ');
                                foreach (string type in types)
                                {
                                    if (authtypes.Contains(type))
                                    {
                                        typelist.Add(type);
                                    }
                                }
                                break;
                            }
                            Parameters.Add(keyn.Key, keyn.Value);
                        }
                        if (listen == true)
                        {
                            container.Send(keyval);
                        }
                        break;
                    }
                }
                else
                {
                    end   = true;
                    error = true;
                }
                if (authreceived == true && uuid == true)
                {
                    end = true;
                }
            }
            if (error == true)
            {
                retval = 1;
                return(retval);
            }
            string msg;

            msg = "Auth Types available:";
            bool first = true;

            foreach (string type in typelist)
            {
                msg = msg + " " + type;
            }
            Runner.DebugMessage("Debug.Info.Connect", msg);
            string authtotry = "";

            if (typelist.Count != 0)
            {
                if (typelist.Contains("NONE"))
                {
                    authtotry = "NONE";
                }
                else
                {
                    authtotry = typelist[0];
                }
                Runner.DebugMessage("Debug.Info.Connect", "Using " + authtotry);
            }
            else
            {
                retval = 1;
                return(retval);
            }
            Node node;

            switch (authtotry)
            {
            case "RSA":
                if (Runner.Network.NodeList.TryGetValue(Parameters["node.uuid"], out node) == true)
                {
                    container.Node = node;
                    node.Version   = Parameters["version"];

                    if (listen == true)
                    {
                        TextMessage bmsg   = new TextMessage("Control.Auth.Challenge");
                        byte[]      cookie = new byte[64];
                        Runner.Network.Random.NextBytes(cookie);
                        foreach (KeyValuePair <UUID, Auth> auth in node.AuthKeyList)
                        {
                            string   ciphertext;
                            IMessage rmsg = auth.Value.Key.Encrypt(true, cookie, out ciphertext);
                            if (rmsg.Type == "Error.OK")
                            {
                                string firstmessage = auth.Key + "!" + ciphertext;
                                rmsg      = Runner.Network.Node.Key.Encrypt(false, UTF8Encoding.UTF8.GetBytes(firstmessage), out ciphertext);
                                bmsg.Data = Runner.Network.Node.Key.UUID.ToString() + "!" + ciphertext;
                                this.Send(bmsg);
                                Boolean ending = false;
                                while (ending == false)
                                {
                                    TextMessage tmsg;
                                    tmsg = new TextMessage(this.Receive(true));
                                    Boolean denied = false;
                                    switch (tmsg.Type)
                                    {
                                    case "Control.Auth.Response":
                                        string[] chunks = tmsg.Data.Split('!');
                                        ciphertext = chunks[1];
                                        byte[] bytes;
                                        Runner.Network.Node.Key.Decrypt(true, ciphertext, out bytes);
                                        chunks = UTF8Encoding.UTF8.GetString(bytes).Split('!');
                                        foreach (KeyValuePair <UUID, Auth> authtoken in node.AuthKeyList)
                                        {
                                            if (authtoken.Key.ToString() == chunks[0])
                                            {
                                                authtoken.Value.Key.Decrypt(false, chunks[1], out bytes);
                                                for (int i = 0; i < 64; i++)
                                                {
                                                    if (cookie[i] != bytes[i])
                                                    {
                                                        denied = true;
                                                        break;
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        if (denied == false)
                                        {
                                            if (Runner.Network.NodeList.TryGetValue(Parameters["node.uuid"], out node) == true)
                                            {
                                                container.Node = node;
                                                node.Version   = Parameters["version"];
                                                TextMessage omsg = new TextMessage("Control.Auth.OK");
                                                omsg.Data     = "test";
                                                omsg.Sequence = tmsg.Sequence;
                                                this.Send(omsg);
                                                retval = 0;
                                                ending = true;
                                            }
                                            else
                                            {
                                                retval = 1;
                                            }
                                        }

                                        break;
                                    }
                                }
                            }
                            else
                            {
                                Runner.DebugMessage("Debug.Info.Auth", rmsg.Type);
                            }
                        }
                    }
                    else
                    {
                        bool ending = false;
                        while (ending == false)
                        {
                            IMessage rmsg;
                            rmsg = this.Receive(true);
                            switch (rmsg.Type)
                            {
                            case "Control.Auth.Challenge":
                                TextMessage Challenge = new TextMessage(rmsg);
                                string[]    chunks    = Challenge.Data.Split('!');
                                UUID        uuidauth  = new UUID(chunks[0]);
                                IMessage    response;
                                foreach (KeyValuePair <UUID, Auth> auth in node.AuthKeyList)
                                {
                                    if (auth.Key.ToString() == uuidauth.ToString())
                                    {
                                        byte[] firststage;
                                        response = auth.Value.Key.Decrypt(false, chunks[1], out firststage);
                                        if (response.Type == "Error.OK")
                                        {
                                            byte[] output;
                                            chunks   = UTF8Encoding.UTF8.GetString(firststage).Split('!');
                                            response = Runner.Network.Node.Key.Decrypt(true, chunks[1], out output);
                                            byte[] plaintext = new byte[64];
                                            for (int i = 0; i < 64; i++)
                                            {
                                                plaintext[i] = output[i];
                                            }
                                            Challenge.Type = "Control.Auth.Response";
                                            string ciphertext;
                                            response = Runner.Network.Node.Key.Encrypt(false, plaintext, out ciphertext);
                                            if (response.Type == "Error.OK")
                                            {
                                                ciphertext = Runner.Network.Node.Key.UUID + "!" + ciphertext;
                                                response   = auth.Value.Key.Encrypt(true, UTF8Encoding.UTF8.GetBytes(ciphertext), out ciphertext);
                                                if (response.Type == "Error.OK")
                                                {
                                                    ciphertext     = auth.Key.ToString() + "!" + ciphertext;
                                                    Challenge.Data = ciphertext;
                                                    this.Send(Challenge);
                                                    ending = true;
                                                    break;
                                                }
                                                else
                                                {
                                                    retval = 1;
                                                    ending = true;
                                                }
                                            }
                                            else
                                            {
                                                retval = 1;
                                                ending = true;
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        ending = false;
                        while (ending == false)
                        {
                            IMessage rmsg = this.Receive(true);
                            switch (rmsg.Type)
                            {
                            case "Control.Auth.OK":
                                container.Node = node;
                                node.Version   = Parameters["version"];
                                retval         = 0;
                                ending         = true;
                                break;

                            default:
                                retval = 1;
                                ending = true;
                                break;
                            }
                        }
                    }
                    Runner.DebugMessage("Debug.Info.Auth", "Remote Node is: " + node.ToString());
                    retval = 0;
                }
                else
                {
                    retval = 1;
                }
                break;

            case "NONE":
                if (Runner.Network.NodeList.TryGetValue(Parameters["node.uuid"], out node) == true)
                {
                    container.Node = node;
                    node.Version   = Parameters["version"];
                    Runner.DebugMessage("Debug.Info.Auth", "Remote Node is: " + node.ToString());
                    retval = 0;
                }
                else
                {
                    retval = 1;
                }
                break;
            }
            return(retval);
        }
Beispiel #2
0
        public int Auth(bool listen, bool enrollment)
        {
            IConnection container = this;
            int retval = 99;
            KeyValueMessage keyval;
            List<string> authtypes = new List<string>();
           // authtypes.Add("NONE");
            authtypes.Add("RSA");

            string authstring = "";
            foreach (string entry in authtypes)
            {
                if (authstring == "")
                {
                    authstring = entry;
                }
                else
                {
                    authstring = authstring + " " + entry;
                }
            }

            keyval = new KeyValueMessage("Control.PreAuth");
            keyval.Add("node.uuid", Runner.Network.Node.UUID.ToString());
            keyval.Add("auth.types", authstring);
            keyval.Add("version", SimpleMesh.Service.Utility.Version);
            if (listen == false)
            {
                container.Send(keyval);
            }
            bool end;
            end = false;
            bool error = false;
            IMessage Recieved;
            bool uuid = false;
            bool authreceived = false;
            bool enrolling = false;
            KeyValueMessage messages;
            List<string> typelist;
            typelist = new List<string>();
            string conntype;
            Dictionary<string, string> Parameters = new Dictionary<string, string>();
            while (end == false)
            {
                Recieved = container.Receive(true);
                if (Recieved.Type.Substring(0, 6) != "Error.")
                {
                    switch (Recieved.Type)
                    {
                        case "Control.PreAuth":
                            messages = new KeyValueMessage(Recieved);
                            foreach (KeyValuePair<string, string> keyn in messages.Data)
                            {
                                switch (keyn.Key)
                                {
                                    case "node.uuid":
                                        uuid = true;
                                        break;
                                    case "auth.types":
                                        authreceived = true;
                                        string[] types = keyn.Value.Split(' ');
                                        foreach (string type in types)
                                        {
                                            if (authtypes.Contains(type))
                                            {
                                                typelist.Add(type);
                                            }
                                        }
                                        break;
                                }
                                Parameters.Add(keyn.Key, keyn.Value);
                            }
                            if (listen == true)
                            {
                                container.Send(keyval);
                            }
                            break;
                    }
                }
                else
                {
                    end = true;
                    error = true;
                }
                if (authreceived == true && uuid == true)
                {
                    end = true;
                }
            }
            if (error == true)
            {
                retval = 1;
                return retval;
            }
            string msg;
            msg = "Auth Types available:";
            bool first = true;
            foreach (string type in typelist)
            {
                msg = msg + " " + type;
            }
            Runner.DebugMessage("Debug.Info.Connect", msg);
            string authtotry = "";
            if (typelist.Count != 0)
            {
                if (typelist.Contains("NONE"))
                {
                    authtotry = "NONE";
                }
                else
                {
                    authtotry = typelist[0];
                }
                Runner.DebugMessage("Debug.Info.Connect", "Using " + authtotry);
            }
            else
            {
                retval = 1;
                return retval;
            }
            Node node;
            switch (authtotry)
            {
                case "RSA":
                    if (Runner.Network.NodeList.TryGetValue(Parameters["node.uuid"], out node) == true)
                    {
                        container.Node = node;
                        node.Version = Parameters["version"];

                        if (listen == true)
                        {
                            TextMessage bmsg = new TextMessage("Control.Auth.Challenge");
                            byte[] cookie = new byte[64];
                            Runner.Network.Random.NextBytes(cookie);
                            foreach (KeyValuePair<UUID, Auth> auth in node.AuthKeyList)
                            {
                                string ciphertext;
                                IMessage rmsg = auth.Value.Key.Encrypt(true, cookie, out ciphertext);
                                if (rmsg.Type == "Error.OK")
                                {
                                    string firstmessage = auth.Key + "!" + ciphertext;
                                    rmsg = Runner.Network.Node.Key.Encrypt(false, UTF8Encoding.UTF8.GetBytes(firstmessage), out ciphertext);
                                    bmsg.Data = Runner.Network.Node.Key.UUID.ToString() + "!" + ciphertext;
                                    this.Send(bmsg);
                                    Boolean ending = false;
                                    while (ending == false)
                                    {
                                        TextMessage tmsg;
                                        tmsg = new TextMessage(this.Receive(true));
                                        Boolean denied = false;
                                        switch (tmsg.Type)
                                        {
                                            case "Control.Auth.Response":
                                                string[] chunks = tmsg.Data.Split('!');
                                                ciphertext = chunks[1];
                                                byte[] bytes;
                                                Runner.Network.Node.Key.Decrypt(true, ciphertext, out bytes);
                                                chunks = UTF8Encoding.UTF8.GetString(bytes).Split('!');
                                                foreach (KeyValuePair<UUID, Auth> authtoken in node.AuthKeyList)
                                                {
                                                    if (authtoken.Key.ToString() == chunks[0])
                                                    {
                                                        authtoken.Value.Key.Decrypt(false, chunks[1], out bytes);
                                                        for (int i = 0; i < 64; i++)
                                                        {
                                                            if (cookie[i] != bytes[i]) {
                                                                denied = true;
                                                                break;
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }
                                                if (denied == false)
                                                {
                                                    if (Runner.Network.NodeList.TryGetValue(Parameters["node.uuid"], out node) == true)
                                                    {
                                                        container.Node = node;
                                                        node.Version = Parameters["version"];
                                                        TextMessage omsg = new TextMessage("Control.Auth.OK");
                                                        omsg.Data = "test";
                                                        omsg.Sequence = tmsg.Sequence;
                                                        this.Send(omsg);
                                                        retval = 0;
                                                        ending = true;
                                                    }
                                                    else
                                                    {
                                                        retval = 1;
                                                    }
                                                }

                                                break;
                                        }
                                        }
                                }
                                else
                                {
                                    Runner.DebugMessage("Debug.Info.Auth", rmsg.Type);
                                }
                            }
                        }
                        else
                        {
                            bool ending = false;
                            while (ending == false)
                            {
                                IMessage rmsg;
                                rmsg = this.Receive(true);
                                switch (rmsg.Type)
                                {
                                    case "Control.Auth.Challenge":
                                        TextMessage Challenge = new TextMessage(rmsg);
                                        string[] chunks = Challenge.Data.Split('!');
                                        UUID uuidauth = new UUID(chunks[0]);
                                        IMessage response;
                                        foreach (KeyValuePair<UUID, Auth> auth in node.AuthKeyList)
                                        {
                                            if (auth.Key.ToString() == uuidauth.ToString())
                                            {
                                                byte[] firststage;
                                                response = auth.Value.Key.Decrypt(false, chunks[1], out firststage);
                                                if (response.Type == "Error.OK")
                                                {
                                                    byte[] output;
                                                    chunks = UTF8Encoding.UTF8.GetString(firststage).Split('!');
                                                    response = Runner.Network.Node.Key.Decrypt(true, chunks[1], out output);
                                                    byte[] plaintext = new byte[64];
                                                    for (int i = 0; i < 64; i++)
                                                    {
                                                        plaintext[i] = output[i];
                                                    }                                                    Challenge.Type = "Control.Auth.Response";
                                                    string ciphertext;
                                                    response = Runner.Network.Node.Key.Encrypt(false, plaintext, out ciphertext);
                                                    if (response.Type == "Error.OK")
                                                    {
                                                        ciphertext = Runner.Network.Node.Key.UUID + "!" + ciphertext;
                                                        response = auth.Value.Key.Encrypt(true, UTF8Encoding.UTF8.GetBytes(ciphertext), out ciphertext);
                                                        if (response.Type == "Error.OK")
                                                        {
                                                            ciphertext = auth.Key.ToString() + "!" + ciphertext;
                                                            Challenge.Data = ciphertext;
                                                            this.Send(Challenge);
                                                            ending = true;
                                                            break;
                                                        }
                                                        else
                                                        {
                                                            retval = 1;
                                                            ending = true;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        retval = 1;
                                                        ending = true;
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                }
                            }
                            ending = false;
                            while (ending == false)
                            {
                                IMessage rmsg = this.Receive(true);
                                switch (rmsg.Type)
                                {
                                    case "Control.Auth.OK":
                                        container.Node = node;
                                        node.Version = Parameters["version"];
                                        retval = 0;
                                        ending = true;
                                        break;
                                    default:
                                        retval = 1;
                                        ending = true;
                                        break;

                                }
                            }
                        }
                        Runner.DebugMessage("Debug.Info.Auth", "Remote Node is: " + node.ToString());
                        retval = 0;
                    }
                    else
                    {
                        retval = 1;
                    }
                    break;
                case "NONE":
                    if (Runner.Network.NodeList.TryGetValue(Parameters["node.uuid"], out node) == true)
                    {
                        container.Node = node;
                        node.Version = Parameters["version"];
                        Runner.DebugMessage("Debug.Info.Auth", "Remote Node is: " + node.ToString());
                        retval = 0;
                    }
                    else
                    {
                        retval = 1;
                    }
                    break;
            }
            return retval;
        }