Exemple #1
0
        /*
         *              HEX( KD ( HEX(H(A1)),
         *              {
         *                      nonce-value, ":" nc-value, ":",
         *                      cnonce-value, ":", qop-value, ":", HEX(H(A2)) }))
         *
         *              If authzid is specified, then A1 is
         *
         *              A1 = { H( { username-value, ":", realm-value, ":", passwd } ),
         *              ":", nonce-value, ":", cnonce-value, ":", authzid-value }
         *
         *              If authzid is not specified, then A1 is
         *
         *              A1 = { H( { username-value, ":", realm-value, ":", passwd } ),
         *              ":", nonce-value, ":", cnonce-value }
         *
         *              where
         *
         *              passwd   = *OCTET
         */

        internal void GenerateResponse()
        {
            byte[] H1;
            byte[] H2;
            byte[] H3;
            //byte[] temp;
            string A1;
            string A2;
            string A3;
            string p1;
            string p2;

            var sb = new StringBuilder();

            sb.Append(xmppClient.Username);
            sb.Append(":");
            sb.Append(step1.Realm ?? xmppClient.XmppDomain);
            sb.Append(":");
            sb.Append(xmppClient.Password);

            //H1 = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
            H1 = Hash.Md5HashBytes(Encoding.UTF8.GetBytes(sb.ToString()));
#if TEST
            var H1hex = Util.Hash.HexToString(H1);
#endif

            sb.Remove(0, sb.Length);
            sb.Append(":");
            sb.Append(step1.Nonce);
            sb.Append(":");
            sb.Append(Cnonce);

            if (Authzid != null)
            {
                sb.Append(":");
                sb.Append(Authzid);
            }
            A1 = sb.ToString();

            byte[] bA1 = Encoding.ASCII.GetBytes(A1);

            byte[] bH1A1 = new byte[H1.Length + bA1.Length];

            Array.Copy(H1, 0, bH1A1, 0, H1.Length);
            Array.Copy(bA1, 0, bH1A1, H1.Length, bA1.Length);

#if TEST
            var bH1A1hex = Util.Hash.HexToString(bH1A1);
#endif

            //H1 = new MD5CryptoServiceProvider().ComputeHash(bH1A1);
            H1 = Hash.Md5HashBytes(bH1A1);

#if TEST
            H1hex = Util.Hash.HexToString(H1);
#endif

            sb.Remove(0, sb.Length);

            /*
             *  from rfc2831
             *  If the "qop" directive's value is "auth", then A2 is:
             *
             *    A2       = { "AUTHENTICATE:", digest-uri-value }
             *
             * If the "qop" value is "auth-int" or "auth-conf" then A2 is:
             *
             *    A2       = { "AUTHENTICATE:", digest-uri-value,
             *             ":00000000000000000000000000000000" }
             */
            sb.Append("AUTHENTICATE:");
            sb.Append(DigestUri);

            if (step1.Qop != "auth")
            {
                sb.Append(":00000000000000000000000000000000");
            }

            A2 = sb.ToString();
            H2 = Encoding.ASCII.GetBytes(A2);


            //H2 = new MD5CryptoServiceProvider().ComputeHash(H2);
            H2 = Hash.Md5HashBytes(H2);
#if TEST
            var H2hex = Util.Hash.HexToString(H2);
#endif
            // create p1 and p2 as the hex representation of H1 and H2
            p1 = H1.ToHex();
            p2 = H2.ToHex();

            sb.Remove(0, sb.Length);
            sb.Append(p1);
            sb.Append(":");
            sb.Append(step1.Nonce);
            sb.Append(":");
            sb.Append(Nc);
            sb.Append(":");
            sb.Append(Cnonce);
            sb.Append(":");
            sb.Append(step1.Qop);
            sb.Append(":");
            sb.Append(p2);

            A3 = sb.ToString();

            H3 = Hash.Md5HashBytes(Encoding.ASCII.GetBytes(A3));
#if TEST
            var H3hex = Util.Hash.HexToString(H3);
#endif
            Response = H3.ToHex().ToLower();
        }