Beispiel #1
0
        private byte[] imm_computeAUTH(String user, String pass, String serial, long noune)
        {
            String ha1_str = user + ":" + serial + ":" + pass;

            //WSLOG(String.Format("AUTH: {0}:{1} serial={2} noune={3:X}", user, pass, serial, noune));

            byte[]  bytes_to_hash = YAPI.DefaultEncoding.GetBytes(ha1_str);
            IBuffer md5digest     = _md5Alg.HashData(bytes_to_hash.AsBuffer());

            byte[] digest   = md5digest.ToArray();
            String ha1      = YAPIContext.imm_bytesToHexStr(digest, 0, digest.Length).ToLower();
            String sha1_raw = ha1 + string.Format("{0:x2}{1:x2}{2:x2}{3:x2}", noune & 0xff, (noune >> 8) & 0xff, (noune >> 16) & 0xff, (noune >> 24) & 0xff);

            //WSLOG("raw:" + sha1_raw);
            byte[]  bytes_to_sha1 = YAPI.DefaultEncoding.GetBytes(sha1_raw);
            IBuffer sha1digest    = _sha1Alg.HashData(bytes_to_sha1.AsBuffer());

            return(sha1digest.ToArray());
        }
        // Return an Authorization header for a given request
        internal virtual string imm_getAuthorization(string request)
        {
            if (_http_params.User.Length == 0 || _http_realm.Length == 0)
            {
                return("");
            }
            _nounce_count++;
            int    pos    = request.IndexOf(' ');
            string method = request.Substring(0, pos);
            int    enduri = request.IndexOf(' ', pos + 1);

            if (enduri < 0)
            {
                enduri = request.Length;
            }
            string uri    = request.Substring(pos + 1, enduri - (pos + 1));
            string nc     = string.Format("{0:x8}", _nounce_count);
            string cnonce = string.Format("{0:x8}", _randGen.Next());

            string plaintext = method + ":" + uri;

            byte[]  bytes_to_hash = YAPI.DefaultEncoding.GetBytes(plaintext);
            var     alg           = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            IBuffer md5digest     = alg.HashData(bytes_to_hash.AsBuffer());

            byte[] digest = md5digest.ToArray();
            string ha2    = YAPIContext.imm_bytesToHexStr(digest, 0, digest.Length).ToLower();

            plaintext     = _ha1 + ":" + _nounce + ":" + nc + ":" + cnonce + ":auth:" + ha2;
            bytes_to_hash = YAPI.DefaultEncoding.GetBytes(plaintext);
            alg           = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            md5digest     = alg.HashData(bytes_to_hash.AsBuffer());
            digest        = md5digest.ToArray();
            string response = YAPIContext.imm_bytesToHexStr(digest, 0, digest.Length).ToLower();

            //System.out.print(String.format("Auth Resp ha1=%s nonce=%s nc=%s cnouce=%s ha2=%s -> %s\n", _ha1, _nounce, nc, cnonce, ha2, response));
            return
                (string.Format(
                     "Authorization: Digest username=\"{0}\", realm=\"{1}\", nonce=\"{2}\", uri=\"{3}\", qop=auth, nc={4}, cnonce=\"{5}\", response=\"{6}\", opaque=\"{7}\"\r\n",
                     _http_params.User, _http_realm, _nounce, uri, nc, cnonce, response, _opaque));
        }
        // Update the hub internal variables according
        // to a received header with WWW-Authenticate
        //todo: verify authentification
        internal virtual void imm_parseWWWAuthenticate(string header)
        {
            int pos = header.IndexOf("\r\nWWW-Authenticate:", StringComparison.Ordinal);

            if (pos == -1)
            {
                return;
            }
            header = header.Substring(pos + 19);
            int eol = header.IndexOf('\r');

            if (eol >= 0)
            {
                header = header.Substring(0, eol);
            }
            _http_realm   = "";
            _nounce       = "";
            _opaque       = "";
            _nounce_count = 0;

            string[] tags  = header.Split(' ');
            char[]   delim =
            {
                '[',
                '=',
                '\"',
                ',',
                ']'
            };
            foreach (string tag in tags)
            {
                string[] parts = tag.Split(delim, StringSplitOptions.RemoveEmptyEntries);
                string   name, value;
                if (parts.Length == 2)
                {
                    name  = parts[0];
                    value = parts[1];
                }
                else if (parts.Length == 3)
                {
                    name  = parts[0];
                    value = parts[2];
                }
                else
                {
                    continue;
                }
                switch (name)
                {
                case "realm":
                    _http_realm = value;
                    break;

                case "nonce":
                    _nounce = value;
                    break;

                case "opaque":
                    _opaque = value;
                    break;
                }
            }

            string plaintext = _http_params.User + ":" + _http_realm + ":" + _http_params.Pass;

            byte[]  bytes_to_hash = YAPI.DefaultEncoding.GetBytes(plaintext);
            var     alg           = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            IBuffer md5digest     = alg.HashData(bytes_to_hash.AsBuffer());

            byte[] digest = md5digest.ToArray();
            _ha1 = YAPIContext.imm_bytesToHexStr(digest, 0, digest.Length).ToLower();
        }