Example #1
0
        private byte[] Hi()
        {
            var prev = new byte[20];

            // Add 1 to the end of salt with most significat octet first
            var key = new byte[_salt.Length + 4];

            Array.Copy(_salt, key, _salt.Length);
            byte[] g = { 0, 0, 0, 1 };
            Array.Copy(g, 0, key, _salt.Length, 4);

            // Compute initial hash
            var hmac = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");

            // Create Key
            var passwordBuffer = CryptographicBuffer.ConvertStringToBinary(Stringprep.SASLPrep(Password), BinaryStringEncoding.Utf8);
            var hmacKey        = hmac.CreateKey(passwordBuffer);

            var result = CryptographicEngine.Sign(hmacKey, key.AsBuffer()).ToArray();

            Array.Copy(result, prev, (int)result.Length);

            for (var i = 1; i < _i; ++i)
            {
                var temp = CryptographicEngine.Sign(hmacKey, prev.AsBuffer()).ToArray();
                for (var j = 0; j < temp.Length; ++j)
                {
                    result[j] ^= temp[j];
                }

                Array.Copy(temp, prev, temp.Length);
            }

            return(result);
        }
Example #2
0
        private void Parse(string address)
        {
            Match match = AddressRegex.Match(address);

            this.userName     = String.Empty;
            this.domainName   = String.Empty;
            this.resourceName = String.Empty;

            if (match != null)
            {
                // The Local part is optional
                if (match.Groups["localpart"] != null)
                {
                    this.userName = Stringprep.NamePrep(match.Groups["localpart"].Value);
                }
                // The Domain part is mandatory
                this.domainName = Stringprep.NodePrep(match.Groups["domainpart"].Value);
                // The Resource part is optional
                if (match.Groups["resourcepart"] != null)
                {
                    this.resourceName = Stringprep.ResourcePrep(match.Groups["resourcepart"].Value);
                }
            }

            this.Update();
        }
Example #3
0
        private byte[] Hi()
        {
            var prev     = new byte[20];
            var password = _utf.GetBytes(Stringprep.SASLPrep(Password));

            // Add 1 to the end of salt with most significat octet first
            var key = new byte[_salt.Length + 4];

            Array.Copy(_salt, key, _salt.Length);
            byte[] g = { 0, 0, 0, 1 };
            Array.Copy(g, 0, key, _salt.Length, 4);

            // Compute initial hash
            var hmac   = new HMACSHA1(password);
            var result = hmac.ComputeHash(key);

            Array.Copy(result, prev, result.Length);

            for (var i = 1; i < _i; ++i)
            {
                var temp = hmac.ComputeHash(prev);
                for (var j = 0; j < temp.Length; ++j)
                {
                    result[j] ^= temp[j];
                }

                Array.Copy(temp, prev, temp.Length);
            }

            return(result);
        }
Example #4
0
        public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
        {
            lock (this)
            {
                //Check tennats here
                if (ValidateHost(stream.To))
                {
                    //Create new services
                    foreach (var template in templates)
                    {
                        var service = (IXmppService)Activator.CreateInstance(template.Value);
                        service.Jid = new Jid(Stringprep.NamePrep(string.Format("{0}.{1}", template.Key, stream.To.Server).Trim('.')));

                        if (serviceManager.GetService(service.Jid) != null)
                        {
                            continue;
                        }

                        service.Name = service.Jid.ToString();
                        if (!string.IsNullOrEmpty(template.Key))
                        {
                            service.ParentService = serviceManager.GetService(new Jid(Stringprep.NamePrep(stream.To.Server)));
                        }
                        service.Configure(new Dictionary <string, string>());
                        serviceManager.RegisterService(service);
                    }
                    //Reroute
                    handlerManager.ProcessStreamStart(stream, Uri.CLIENT, xmppStream);
                }
                else
                {
                    context.Sender.SendToAndClose(xmppStream, XmppStreamError.HostUnknown);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Apply the SASLprep profile [RFC4013] of the "stringprep" algorithm [RFC3454] as the normalization
        /// algorithm to a UTF-8 [RFC3629] encoded string.
        /// </summary>
        /// <param name="input">the string to prep.</param>
        /// <param name="allowUnassigned">true if the name may contain unassigned code points.</param>
        /// <returns> the prepped name.
        /// @throws StringprepException If the name cannot be prepped with
        /// this profile.
        /// @throws NullPointerException If the name is null.
        /// </returns>
        public static string SaslPrep(string input, bool allowUnassigned)
        {
            if (input == null)
            {
                throw new System.NullReferenceException();
            }

            StringBuilder s = new StringBuilder(input);

            // Unassigned Code Points
            if (!allowUnassigned && Stringprep.Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
            }

            // Mapping
            Stringprep.Filter(s, RFC3454.B1);
            Stringprep.Map(s, RFC3454.C12, C11Replace);

            // Normalization
            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));

            // Prohibited Output
            if (Stringprep.Contains(s, RFC3454.C12) || // - Non-ASCII space characters [StringPrep, C.1.2]
                Stringprep.Contains(s, RFC3454.C21) || // - ASCII control characters [StringPrep, C.2.1]
                Stringprep.Contains(s, RFC3454.C22) || // - Non-ASCII control characters [StringPrep, C.2.2]
                Stringprep.Contains(s, RFC3454.C3) || // - Private Use characters [StringPrep, C.3]
                Stringprep.Contains(s, RFC3454.C4) || // - Non-character code points [StringPrep, C.4]
                Stringprep.Contains(s, RFC3454.C5) || // - Surrogate code points [StringPrep, C.5]
                Stringprep.Contains(s, RFC3454.C6) || // - Inappropriate for plain text characters [StringPrep, C.6]
                Stringprep.Contains(s, RFC3454.C7) || // - Inappropriate for canonical representation characters [StringPrep, C.7]
                Stringprep.Contains(s, RFC3454.C8)) // - Change display properties or deprecated characters [StringPrep, C.8]
            {
                // Table C.9 only contains code points > 0xFFFF which Java doesn't handle
                throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
            }

            // Bidirectional Characters
            bool r = Stringprep.Contains(s, RFC3454.D1);
            bool l = Stringprep.Contains(s, RFC3454.D2);

            // RFC 3454, section 6, requirement 1: already handled above (table C.8)

            // RFC 3454, section 6, requirement 2
            if (r && l)
            {
                throw new StringprepException(StringprepException.BIDI_BOTHRAL);
            }

            // RFC 3454, section 6, requirement 3
            if (r)
            {
                if (!Stringprep.Contains(s[0], RFC3454.D1) || !Stringprep.Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BIDI_LTRAL);
                }
            }

            return(s.ToString());
        }
 public void BindResource(string resource)
 {
     lock (resources)
     {
         resources.Add(Stringprep.ResourcePrep(resource));
     }
     Connected = true;
 }
Example #7
0
        /// <summary>
        ///   Initializes a new instance of the <see cref = "XmppJid" /> class with
        ///   the given user name, domain name and resource name.
        /// </summary>
        /// <param name = "userName">The user name</param>
        /// <param name = "domainName">The domain name</param>
        /// <param name = "resourceName">The resource name</param>
        public XmppJid(string userName, string domainName, string resourceName)
        {
            this.userName     = Stringprep.NamePrep(userName);
            this.domainName   = Stringprep.NodePrep(domainName);
            this.resourceName = Stringprep.ResourcePrep(resourceName);

            BuildBareAndFullJid();
        }
 public void UnbindResource(string resource)
 {
     lock (resources)
     {
         resources.Remove(Stringprep.ResourcePrep(resource));
     }
     Connected = resources.Count != 0;
 }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XmppAddress"/> class with
        /// the given user name, domain name and resource name.
        /// </summary>
        /// <param name="userName">The user name</param>
        /// <param name="domainName">The domain name</param>
        /// <param name="resourceName">The resource name</param>
        public XmppAddress(string userName, string domainName, string resourceName)
        {
            this.userName     = Stringprep.NamePrep(userName);
            this.domainName   = Stringprep.NodePrep(domainName);
            this.resourceName = Stringprep.ResourcePrep(resourceName);

            this.Update();
        }
Example #10
0
 private static void ConfigureServices(JabberConfigurationSection jabberSection, XmppServer server)
 {
     foreach (ServiceConfigurationElement se in jabberSection.Services)
     {
         var service = (IXmppService)Activator.CreateInstance(Type.GetType(se.TypeName, true));
         service.Jid  = new Jid(Stringprep.NamePrep(se.Jid));
         service.Name = se.Name;
         if (!string.IsNullOrEmpty(se.Parent))
         {
             service.ParentService = server.GetXmppService(new Jid(Stringprep.NamePrep(se.Parent)));
         }
         service.Configure(se.GetProperties());
         server.RegisterXmppService(service);
     }
 }
 private Jid NodePrep(Jid jid)
 {
     if (jid != null)
     {
         if (jid.HasUser)
         {
             jid.User = Stringprep.NodePrep(jid.User);
         }
         if (jid.HasResource)
         {
             jid.Resource = Stringprep.ResourcePrep(jid.Resource);
         }
         //BUG: Many faults here in log
         jid.Server = Stringprep.NamePrep(jid.Server);
     }
     return(jid);
 }
Example #12
0
File: Jid.cs Project: Toyz/agsXMPP
        /// <summary>
        /// builds a new Jid object
        /// </summary>
        /// <param name="user">XMPP User part</param>
        /// <param name="server">XMPP Domain part</param>
        /// <param name="resource">XMPP Resource part</param>
        public Jid(string user, string server, string resource, bool stringprep = false)
        {
            if (!stringprep)
            {
                if (user != null)
                {
                    user = EscapeNode(user);

                    this.m_User = user.ToLower();
                }

                if (server != null)
                {
                    this.m_Server = server.ToLower();
                }

                if (resource != null)
                {
                    this.m_Resource = resource;
                }
            }
            else
            {
                if (user != null)
                {
                    user = EscapeNode(user);

                    this.m_User = Stringprep.NodePrep(user);
                }

                if (server != null)
                {
                    this.m_Server = Stringprep.NamePrep(server);
                }

                if (resource != null)
                {
                    this.m_Resource = Stringprep.ResourcePrep(resource);
                }
            }

            this.BuildJid();
        }
 private bool ValidateJid(Jid jid)
 {
     if (jid == null)
     {
         return(true);
     }
     if (jid.HasUser)
     {
         if (jid.User != Jid.EscapeNode(jid.User))
         {
             return(false);
         }
         if (jid.User != Stringprep.NodePrep(jid.User))
         {
             return(false);
         }
     }
     return(true);
 }
Example #14
0
        /// <summary>
        /// builds a new Jid object
        /// </summary>
        /// <param name="user">XMPP User part</param>
        /// <param name="server">XMPP Domain part</param>
        /// <param name="resource">XMPP Resource part</param>
        public Jid(string user, string server, string resource)
        {
#if !STRINGPREP
            if (user != null)
            {
                user = EscapeNode(user);

                m_User = user.ToLower();
            }

            if (server != null)
            {
                m_Server = server.ToLower();
            }

            if (resource != null)
            {
                m_Resource = resource;
            }
#else
            if (user != null)
            {
                user = EscapeNode(user);

                m_User = Stringprep.NodePrep(user);
            }

            if (server != null)
            {
                m_Server = Stringprep.NamePrep(server);
            }

            if (resource != null)
            {
                m_Resource = Stringprep.ResourcePrep(resource);
            }
#endif
            BuildJid();
        }
Example #15
0
        private void Parse(string jid)
        {
            Match match = JidRegex.Match(jid);

            if (match != null)
            {
                if (match.Groups["userid"] != null)
                {
                    userName = Stringprep.NamePrep(match.Groups["userid"].Value);
                }
                if (match.Groups["domain"] != null)
                {
                    domainName = Stringprep.NodePrep(match.Groups["domain"].Value);
                }
                if (match.Groups["resource"] != null)
                {
                    resourceName = Stringprep.ResourcePrep(match.Groups["resource"].Value);
                }
            }

            BuildBareAndFullJid();
        }
Example #16
0
        public XmppHandlerResult ProcessElement(RegisterIq element, XmppSession session, XmppHandlerContext context)
        {
            if (element.Type == IqType.get)
            {
                element.Query.Instructions = RS.RegisterInstructions;
                element.Query.Username     = string.Empty;
                element.Query.Password     = string.Empty;
                element.ToResult();
                if (session.Jid.HasUser && context.Storages.Users.GetUser(session.Jid.User) != null)
                {
                    element.Query.Username = session.Jid.User;
                    element.Query.AddChild(new Element("registered"));
                }
                else
                {
                    element.To = null;
                }
                element.From = null;
                return(Send(session, element));
            }
            else
            {
                if (element.Query.RemoveAccount)
                {
                    if (!session.Authenticated || !session.Jid.HasUser)
                    {
                        return(Error(session, StreamErrorCondition.NotAuthorized));
                    }

                    context.Storages.Users.RemoveUser(session.Jid.User);
                    var errors = Component();
                    foreach (var s in context.Sessions.GetSessions(session.Jid.BareJid))
                    {
                        if (!session.Equals(s))
                        {
                            errors.Add(Error(s, StreamErrorCondition.NotAuthorized));
                        }
                    }

                    element.Query.Remove();
                    element.ToResult();
                    element.From = element.To = null;
                    return(Component(Send(session, element), errors));
                }

                if (string.IsNullOrEmpty(element.Query.Username))
                {
                    return(Error(session, ErrorCondition.NotAcceptable, element, RS.RegisterEmptyUsername));
                }
                if (string.IsNullOrEmpty(element.Query.Password))
                {
                    return(Error(session, ErrorCondition.NotAcceptable, element, RS.RegisterEmptyPassword));
                }
                if (Stringprep.NamePrep(element.Query.Username) != element.Query.Username)
                {
                    return(Error(session, ErrorCondition.NotAcceptable, element, RS.RegisterInvalidCharacter));
                }

                var user = context.Storages.Users.GetUser(element.Query.Username);
                if (user != null && user.Name != session.Jid.User)
                {
                    return(Error(session, ErrorCondition.Conflict, element));
                }

                context.Storages.Users.SaveUser(new XmppUser(element.Query.Username, element.Query.Password));

                element.Query.Remove();
                element.ToResult();
                if (!session.Authenticated)
                {
                    element.To = null;
                }
                element.From = null;
                return(Send(session, element));
            }
        }
 private string Nameprep(string value)
 {
     return(string.IsNullOrEmpty(value) ? value : Stringprep.NamePrep(value));
 }
        private IQ SetRegister(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            var register = (Register)iq.Query;

            iq.Type = IqType.result;

            if (register.RemoveAccount)
            {
                if (!stream.Authenticated || !iq.From.HasUser)
                {
                    context.Sender.SendToAndClose(stream, XmppStreamError.NotAuthorized);
                }

                context.UserManager.RemoveUser(iq.From);
                foreach (var s in context.SessionManager.GetBareJidSessions(iq.From))
                {
                    if (s.Stream.Id == stream.Id)
                    {
                        continue;
                    }
                    context.Sender.SendToAndClose(s.Stream, XmppStreamError.Conflict);
                }
                //TODO: remove roster subscriptions
                register.RemoveAllChildNodes();
                iq.SwitchDirection();
                return(iq);
            }

            if (string.IsNullOrEmpty(register.Username) ||
                string.IsNullOrEmpty(register.Password) ||
                Stringprep.NamePrep(register.Username) != register.Username)
            {
                var error = XmppStanzaError.ToNotAcceptable(iq);
                if (string.IsNullOrEmpty(register.Username))
                {
                    error.Error.Message = "Empty required field Username.";
                }
                else if (string.IsNullOrEmpty(register.Password))
                {
                    error.Error.Message = "Empty required field Password.";
                }
                else if (Stringprep.NamePrep(register.Username) != register.Username)
                {
                    error.Error.Message = "Invalid character.";
                }
                return(error);
            }

            var userJid = new Jid(register.Username, stream.Domain, null);

            if (context.UserManager.IsUserExists(userJid))
            {
                return(XmppStanzaError.ToConflict(iq));
            }

            var user = new User(userJid, register.Password);

            context.UserManager.SaveUser(user);

            register.RemoveAllChildNodes();
            if (stream.Authenticated)
            {
                iq.SwitchDirection();
            }
            else
            {
                iq.To = null;
            }
            iq.From = null;
            return(iq);
        }
 public void Authenticate(string userName)
 {
     User          = !string.IsNullOrEmpty(userName) ? Stringprep.NodePrep(userName) : null;
     Authenticated = true;
 }