~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/protocol/http/src/nsHttpDigestAuth.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is mozilla.org code.
 
14
 *
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are
 
17
 * Copyright (C) 1998 Netscape Communications Corporation. All
 
18
 * Rights Reserved.
 
19
 *
 
20
 * Contributor(s): 
 
21
 *   Justin Bradford <jab@atdot.org> (original author of nsDigestAuth.cpp)
 
22
 *   An-Cheng Huang <pach@cs.cmu.edu>
 
23
 *   Darin Fisher <darin@netscape.com>
 
24
 */
 
25
 
 
26
#include <stdlib.h>
 
27
#include "nsHttp.h"
 
28
#include "nsHttpDigestAuth.h"
 
29
#include "nsIHttpChannel.h"
 
30
#include "nsIServiceManager.h"
 
31
#include "nsXPCOM.h"
 
32
#include "nsISupportsPrimitives.h"
 
33
#include "nsIURI.h"
 
34
#include "nsString.h"
 
35
#include "nsReadableUtils.h"
 
36
#include "nsEscape.h"
 
37
#include "plbase64.h"
 
38
#include "plstr.h"
 
39
#include "prprf.h"
 
40
#include "prmem.h"
 
41
#include "nsCRT.h"
 
42
 
 
43
//-----------------------------------------------------------------------------
 
44
// nsHttpDigestAuth <public>
 
45
//-----------------------------------------------------------------------------
 
46
 
 
47
nsHttpDigestAuth::nsHttpDigestAuth()
 
48
{
 
49
  mVerifier = do_GetService(SIGNATURE_VERIFIER_CONTRACTID);
 
50
  mGotVerifier = (mVerifier != nsnull);
 
51
 
 
52
#if defined(PR_LOGGING)
 
53
  if (mGotVerifier) {
 
54
    LOG(("nsHttpDigestAuth: Got signature_verifier\n"));
 
55
  } else {
 
56
    LOG(("nsHttpDigestAuth: No signature_verifier available\n"));
 
57
  }
 
58
#endif
 
59
}
 
60
 
 
61
//-----------------------------------------------------------------------------
 
62
// nsHttpDigestAuth::nsISupports
 
63
//-----------------------------------------------------------------------------
 
64
 
 
65
NS_IMPL_ISUPPORTS1(nsHttpDigestAuth, nsIHttpAuthenticator)
 
66
 
 
67
//-----------------------------------------------------------------------------
 
68
// nsHttpDigestAuth <protected>
 
69
//-----------------------------------------------------------------------------
 
70
 
 
71
nsresult
 
72
nsHttpDigestAuth::MD5Hash(const char *buf, PRUint32 len)
 
73
{
 
74
  if (!mGotVerifier)
 
75
    return NS_ERROR_NOT_INITIALIZED;
 
76
 
 
77
  nsresult rv;
 
78
 
 
79
  HASHContextStr *hid;
 
80
  rv = mVerifier->HashBegin(nsISignatureVerifier::MD5, &hid);
 
81
  if (NS_FAILED(rv)) return rv;
 
82
 
 
83
  // must call HashEnd to destroy |hid|
 
84
  unsigned char cbuf[DIGEST_LENGTH], *chash = cbuf;
 
85
  PRUint32 clen;
 
86
 
 
87
  rv  = mVerifier->HashUpdate(hid, buf, len);
 
88
  rv |= mVerifier->HashEnd(hid, &chash, &clen, DIGEST_LENGTH);
 
89
  if (NS_SUCCEEDED(rv))
 
90
    memcpy(mHashBuf, chash, DIGEST_LENGTH);
 
91
  return rv;
 
92
}
 
93
 
 
94
nsresult
 
95
nsHttpDigestAuth::GetMethodAndPath(nsIHttpChannel *httpChannel,
 
96
                                   PRBool          isProxyAuth,
 
97
                                   nsCString      &httpMethod,
 
98
                                   nsCString      &path)
 
99
{
 
100
  nsresult rv;
 
101
  nsCOMPtr<nsIURI> uri;
 
102
  rv = httpChannel->GetURI(getter_AddRefs(uri));
 
103
  if (NS_SUCCEEDED(rv)) {
 
104
    PRBool isSecure;
 
105
    rv = uri->SchemeIs("https", &isSecure);
 
106
    if (NS_SUCCEEDED(rv)) {
 
107
      //
 
108
      // if we are being called in response to a 407, and if the protocol
 
109
      // is HTTPS, then we are really using a CONNECT method.
 
110
      //
 
111
      if (isSecure && isProxyAuth) {
 
112
        httpMethod = NS_LITERAL_CSTRING("CONNECT");
 
113
        //
 
114
        // generate hostname:port string. (unfortunately uri->GetHostPort
 
115
        // leaves out the port if it matches the default value, so we can't
 
116
        // just call it.)
 
117
        //
 
118
        PRInt32 port;
 
119
        rv  = uri->GetAsciiHost(path);
 
120
        rv |= uri->GetPort(&port);
 
121
        if (NS_SUCCEEDED(rv)) {
 
122
          path.Append(':');
 
123
          path.AppendInt(port < 0 ? NS_HTTPS_DEFAULT_PORT : port);
 
124
        }
 
125
      }
 
126
      else { 
 
127
        rv  = httpChannel->GetRequestMethod(httpMethod);
 
128
        rv |= uri->GetPath(path);
 
129
        if (NS_SUCCEEDED(rv)) {
 
130
          //
 
131
          // strip any fragment identifier from the URL path.
 
132
          //
 
133
          PRInt32 ref = path.RFindChar('#');
 
134
          if (ref != kNotFound)
 
135
            path.Truncate(ref);
 
136
          //
 
137
          // make sure we escape any UTF-8 characters in the URI path.  the
 
138
          // digest auth uri attribute needs to match the request-URI.
 
139
          //
 
140
          // XXX we should really ask the HTTP channel for this string
 
141
          // instead of regenerating it here.
 
142
          //
 
143
          nsCAutoString buf;
 
144
          path = NS_EscapeURL(path, esc_OnlyNonASCII, buf);
 
145
        }
 
146
      }
 
147
    }
 
148
  }
 
149
  return rv;
 
150
}
 
151
 
 
152
//-----------------------------------------------------------------------------
 
153
// nsHttpDigestAuth::nsIHttpAuthenticator
 
154
//-----------------------------------------------------------------------------
 
155
 
 
156
NS_IMETHODIMP
 
157
nsHttpDigestAuth::ChallengeReceived(nsIHttpChannel *httpChannel,
 
158
                                    const char *challenge,
 
159
                                    PRBool isProxyAuth,
 
160
                                    nsISupports **sessionState,
 
161
                                    nsISupports **continuationState,
 
162
                                    PRBool *result)
 
163
{
 
164
  nsCAutoString realm, domain, nonce, opaque;
 
165
  PRBool stale;
 
166
  PRUint16 algorithm, qop;
 
167
 
 
168
  nsresult rv = ParseChallenge(challenge, realm, domain, nonce, opaque,
 
169
                               &stale, &algorithm, &qop);
 
170
  if (NS_FAILED(rv)) return rv;
 
171
 
 
172
  // if the challenge has the "stale" flag set, then the user identity is not
 
173
  // necessarily invalid.  by returning FALSE here we can suppress username
 
174
  // and password prompting that usually accompanies a 401/407 challenge.
 
175
  *result = !stale;
 
176
 
 
177
  // clear any existing nonce_count since we have a new challenge.
 
178
  NS_IF_RELEASE(*sessionState);
 
179
  return NS_OK;
 
180
}
 
181
 
 
182
NS_IMETHODIMP
 
183
nsHttpDigestAuth::GenerateCredentials(nsIHttpChannel *httpChannel,
 
184
                                      const char *challenge,
 
185
                                      PRBool isProxyAuth,
 
186
                                      const PRUnichar *userdomain,
 
187
                                      const PRUnichar *username,
 
188
                                      const PRUnichar *password,
 
189
                                      nsISupports **sessionState,
 
190
                                      nsISupports **continuationState,
 
191
                                      char **creds)
 
192
 
 
193
{
 
194
  LOG(("nsHttpDigestAuth::GenerateCredentials [challenge=%s]\n", challenge));
 
195
 
 
196
  NS_ENSURE_ARG_POINTER(creds);
 
197
 
 
198
  PRBool isDigestAuth = !PL_strncasecmp(challenge, "digest ", 7);
 
199
  NS_ENSURE_TRUE(isDigestAuth, NS_ERROR_UNEXPECTED);
 
200
 
 
201
  // IIS implementation requires extra quotes
 
202
  PRBool requireExtraQuotes = PR_FALSE;
 
203
  {
 
204
    nsCAutoString serverVal;
 
205
    httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Server"), serverVal);
 
206
    if (!serverVal.IsEmpty()) {
 
207
      requireExtraQuotes = !PL_strncasecmp(serverVal.get(), "Microsoft-IIS", 13);
 
208
    }
 
209
  }
 
210
 
 
211
  nsresult rv;
 
212
  nsCAutoString httpMethod;
 
213
  nsCAutoString path;
 
214
  rv = GetMethodAndPath(httpChannel, isProxyAuth, httpMethod, path);
 
215
  if (NS_FAILED(rv)) return rv;
 
216
 
 
217
  nsCAutoString realm, domain, nonce, opaque;
 
218
  PRBool stale;
 
219
  PRUint16 algorithm, qop;
 
220
 
 
221
  rv = ParseChallenge(challenge, realm, domain, nonce, opaque,
 
222
                      &stale, &algorithm, &qop);
 
223
  if (NS_FAILED(rv)) {
 
224
    LOG(("nsHttpDigestAuth::GenerateCredentials [ParseChallenge failed rv=%x]\n", rv));
 
225
    return rv;
 
226
  }
 
227
 
 
228
  char ha1_digest[EXPANDED_DIGEST_LENGTH+1];
 
229
  char ha2_digest[EXPANDED_DIGEST_LENGTH+1];
 
230
  char response_digest[EXPANDED_DIGEST_LENGTH+1];
 
231
  char upload_data_digest[EXPANDED_DIGEST_LENGTH+1];
 
232
 
 
233
  if (qop & QOP_AUTH_INT) {
 
234
    // we do not support auth-int "quality of protection" currently
 
235
    qop &= ~QOP_AUTH_INT;
 
236
 
 
237
    NS_WARNING("no support for Digest authentication with data integrity quality of protection");
 
238
 
 
239
    /* TODO: to support auth-int, we need to get an MD5 digest of
 
240
     * TODO: the data uploaded with this request.
 
241
     * TODO: however, i am not sure how to read in the file in without
 
242
     * TODO: disturbing the channel''s use of it. do i need to copy it 
 
243
     * TODO: somehow?
 
244
     */
 
245
#if 0
 
246
    if (http_channel != nsnull)
 
247
    {
 
248
      nsIInputStream * upload;
 
249
      nsCOMPtr<nsIUploadChannel> uc = do_QueryInterface(http_channel);
 
250
      NS_ENSURE_TRUE(uc, NS_ERROR_UNEXPECTED);
 
251
      uc->GetUploadStream(&upload);
 
252
      if (upload) {
 
253
        char * upload_buffer;
 
254
        int upload_buffer_length = 0;
 
255
        //TODO: read input stream into buffer
 
256
        const char * digest = (const char*)
 
257
        nsNetwerkMD5Digest(upload_buffer, upload_buffer_length);
 
258
        ExpandToHex(digest, upload_data_digest);
 
259
        NS_RELEASE(upload);
 
260
      }
 
261
    }
 
262
#endif
 
263
  }
 
264
 
 
265
  if (!(algorithm & ALGO_MD5 || algorithm & ALGO_MD5_SESS)) {
 
266
    // they asked only for algorithms that we do not support
 
267
    NS_WARNING("unsupported algorithm requested by Digest authentication");
 
268
    return NS_ERROR_NOT_IMPLEMENTED;
 
269
  }
 
270
 
 
271
  //
 
272
  // the following are for increasing security.  see RFC 2617 for more
 
273
  // information.
 
274
  // 
 
275
  // nonce_count allows the server to keep track of auth challenges (to help
 
276
  // prevent spoofing). we increase this count every time.
 
277
  //
 
278
  char nonce_count[NONCE_COUNT_LENGTH+1] = "00000001"; // in hex
 
279
  if (*sessionState) {
 
280
    nsCOMPtr<nsISupportsPRUint32> v(do_QueryInterface(*sessionState));
 
281
    if (v) {
 
282
      PRUint32 nc;
 
283
      v->GetData(&nc);
 
284
      PR_snprintf(nonce_count, sizeof(nonce_count), "%08x", ++nc);
 
285
      v->SetData(nc);
 
286
    }
 
287
  }
 
288
  else {
 
289
    nsCOMPtr<nsISupportsPRUint32> v(
 
290
            do_CreateInstance(NS_SUPPORTS_PRUINT32_CONTRACTID, &rv));
 
291
    if (v) {        
 
292
      v->SetData(1);
 
293
      NS_ADDREF(*sessionState = v);
 
294
    }
 
295
  }
 
296
  LOG(("   nonce_count=%s\n", nonce_count));
 
297
 
 
298
  //
 
299
  // this lets the client verify the server response (via a server
 
300
  // returned Authentication-Info header). also used for session info.
 
301
  //
 
302
  nsCAutoString cnonce;
 
303
  static const char hexChar[] = "0123456789abcdef"; 
 
304
  for (int i=0; i<16; ++i) {
 
305
    cnonce.Append(hexChar[(int)(15.0 * rand()/(RAND_MAX + 1.0))]);
 
306
  }
 
307
  LOG(("   cnonce=%s\n", cnonce.get()));
 
308
 
 
309
  //
 
310
  // calculate credentials
 
311
  //
 
312
 
 
313
  NS_ConvertUCS2toUTF8 cUser(username), cPass(password);
 
314
  rv = CalculateHA1(cUser, cPass, realm, algorithm, nonce, cnonce, ha1_digest);
 
315
  if (NS_FAILED(rv)) return rv;
 
316
 
 
317
  rv = CalculateHA2(httpMethod, path, qop, upload_data_digest, ha2_digest);
 
318
  if (NS_FAILED(rv)) return rv;
 
319
 
 
320
  rv = CalculateResponse(ha1_digest, ha2_digest, nonce, qop, nonce_count,
 
321
                         cnonce, response_digest);
 
322
  if (NS_FAILED(rv)) return rv;
 
323
 
 
324
  nsCAutoString authString("Digest ");
 
325
  authString += "username=\"";
 
326
  authString += cUser;
 
327
  authString += NS_LITERAL_CSTRING("\", realm=\"");
 
328
  authString += realm;
 
329
  authString += NS_LITERAL_CSTRING("\", nonce=\"");
 
330
  authString += nonce;
 
331
  authString += NS_LITERAL_CSTRING("\", uri=\"");
 
332
  authString += path;
 
333
  if (algorithm & ALGO_SPECIFIED) {
 
334
    authString += "\", algorithm=";
 
335
    if (algorithm & ALGO_MD5_SESS)
 
336
      authString += "MD5-sess";
 
337
    else
 
338
      authString += "MD5";
 
339
  } else {
 
340
    authString += "\"";
 
341
  }
 
342
  authString += ", response=\"";
 
343
  authString += response_digest;
 
344
 
 
345
  if (!opaque.IsEmpty()) {
 
346
    authString += "\", opaque=\"";
 
347
    authString += opaque;
 
348
  }
 
349
 
 
350
  if (qop) {
 
351
    authString += "\", qop=";
 
352
    if (requireExtraQuotes)
 
353
      authString += "\"";
 
354
    if (qop & QOP_AUTH_INT)
 
355
      authString += "auth-int";
 
356
    else
 
357
      authString += "auth";
 
358
    if (requireExtraQuotes)
 
359
      authString += "\"";
 
360
    authString += ", nc=";
 
361
    authString += nonce_count;
 
362
    authString += ", cnonce=\"";
 
363
    authString += cnonce;
 
364
  }
 
365
  authString += "\"";
 
366
 
 
367
  *creds = ToNewCString(authString);
 
368
  return NS_OK;
 
369
}
 
370
 
 
371
NS_IMETHODIMP
 
372
nsHttpDigestAuth::GetAuthFlags(PRUint32 *flags)
 
373
{
 
374
  *flags = REQUEST_BASED | REUSABLE_CHALLENGE;
 
375
  //
 
376
  // NOTE: digest auth credentials must be uniquely computed for each request,
 
377
  //       so we do not set the REUSABLE_CREDENTIALS flag.
 
378
  //
 
379
  return NS_OK;
 
380
}
 
381
 
 
382
nsresult
 
383
nsHttpDigestAuth::CalculateResponse(const char * ha1_digest,
 
384
                                    const char * ha2_digest,
 
385
                                    const nsAFlatCString & nonce,
 
386
                                    PRUint16 qop,
 
387
                                    const char * nonce_count,
 
388
                                    const nsAFlatCString & cnonce,
 
389
                                    char * result)
 
390
{
 
391
  PRUint32 len = 2*EXPANDED_DIGEST_LENGTH + nonce.Length() + 2;
 
392
 
 
393
  if (qop & QOP_AUTH || qop & QOP_AUTH_INT) {
 
394
    len += cnonce.Length() + NONCE_COUNT_LENGTH + 3;
 
395
    if (qop & QOP_AUTH_INT)
 
396
      len += 8; // length of "auth-int"
 
397
    else
 
398
      len += 4; // length of "auth"
 
399
  }
 
400
 
 
401
  nsCAutoString contents;
 
402
  contents.SetCapacity(len);
 
403
 
 
404
  contents.Assign(ha1_digest, EXPANDED_DIGEST_LENGTH);
 
405
  contents.Append(':');
 
406
  contents.Append(nonce);
 
407
  contents.Append(':');
 
408
 
 
409
  if (qop & QOP_AUTH || qop & QOP_AUTH_INT) {
 
410
    contents.Append(nonce_count, NONCE_COUNT_LENGTH);
 
411
    contents.Append(':');
 
412
    contents.Append(cnonce);
 
413
    contents.Append(':');
 
414
    if (qop & QOP_AUTH_INT)
 
415
      contents.Append(NS_LITERAL_CSTRING("auth-int:"));
 
416
    else
 
417
      contents.Append(NS_LITERAL_CSTRING("auth:"));
 
418
  }
 
419
 
 
420
  contents.Append(ha2_digest, EXPANDED_DIGEST_LENGTH);
 
421
 
 
422
  nsresult rv = MD5Hash(contents.get(), contents.Length());
 
423
  if (NS_SUCCEEDED(rv))
 
424
    rv = ExpandToHex(mHashBuf, result);
 
425
  return rv;
 
426
}
 
427
 
 
428
nsresult
 
429
nsHttpDigestAuth::ExpandToHex(const char * digest, char * result)
 
430
{
 
431
  PRInt16 index, value;
 
432
 
 
433
  for (index = 0; index < DIGEST_LENGTH; index++) {
 
434
    value = (digest[index] >> 4) & 0xf;
 
435
    if (value < 10)
 
436
      result[index*2] = value + '0';
 
437
    else
 
438
      result[index*2] = value - 10 + 'a';
 
439
 
 
440
    value = digest[index] & 0xf;
 
441
    if (value < 10)
 
442
      result[(index*2)+1] = value + '0';
 
443
    else
 
444
      result[(index*2)+1] = value - 10 + 'a';
 
445
  }
 
446
 
 
447
  result[EXPANDED_DIGEST_LENGTH] = 0;
 
448
  return NS_OK;
 
449
}
 
450
 
 
451
nsresult
 
452
nsHttpDigestAuth::CalculateHA1(const nsAFlatCString & username,
 
453
                               const nsAFlatCString & password,
 
454
                               const nsAFlatCString & realm,
 
455
                               PRUint16 algorithm,
 
456
                               const nsAFlatCString & nonce,
 
457
                               const nsAFlatCString & cnonce,
 
458
                               char * result)
 
459
{
 
460
  PRInt16 len = username.Length() + password.Length() + realm.Length() + 2;
 
461
  if (algorithm & ALGO_MD5_SESS) {
 
462
    PRInt16 exlen = EXPANDED_DIGEST_LENGTH + nonce.Length() + cnonce.Length() + 2;
 
463
    if (exlen > len)
 
464
        len = exlen;
 
465
  }
 
466
 
 
467
  nsCAutoString contents;
 
468
  contents.SetCapacity(len + 1);
 
469
 
 
470
  contents.Assign(username);
 
471
  contents.Append(':');
 
472
  contents.Append(realm);
 
473
  contents.Append(':');
 
474
  contents.Append(password);
 
475
 
 
476
  nsresult rv;
 
477
  rv = MD5Hash(contents.get(), contents.Length());
 
478
  if (NS_FAILED(rv))
 
479
    return rv;
 
480
 
 
481
  if (algorithm & ALGO_MD5_SESS) {
 
482
    char part1[EXPANDED_DIGEST_LENGTH+1];
 
483
    ExpandToHex(mHashBuf, part1);
 
484
 
 
485
    contents.Assign(part1, EXPANDED_DIGEST_LENGTH);
 
486
    contents.Append(':');
 
487
    contents.Append(nonce);
 
488
    contents.Append(':');
 
489
    contents.Append(cnonce);
 
490
 
 
491
    rv = MD5Hash(contents.get(), contents.Length());
 
492
    if (NS_FAILED(rv))
 
493
      return rv;
 
494
  }
 
495
 
 
496
  return ExpandToHex(mHashBuf, result);
 
497
}
 
498
 
 
499
nsresult
 
500
nsHttpDigestAuth::CalculateHA2(const nsAFlatCString & method,
 
501
                               const nsAFlatCString & path,
 
502
                               PRUint16 qop,
 
503
                               const char * bodyDigest,
 
504
                               char * result)
 
505
{
 
506
  PRInt16 methodLen = method.Length();
 
507
  PRInt16 pathLen = path.Length();
 
508
  PRInt16 len = methodLen + pathLen + 1;
 
509
 
 
510
  if (qop & QOP_AUTH_INT) {
 
511
    len += EXPANDED_DIGEST_LENGTH + 1;
 
512
  }
 
513
 
 
514
  nsCAutoString contents;
 
515
  contents.SetCapacity(len);
 
516
 
 
517
  contents.Assign(method);
 
518
  contents.Append(':');
 
519
  contents.Append(path);
 
520
 
 
521
  if (qop & QOP_AUTH_INT) {
 
522
    contents.Append(':');
 
523
    contents.Append(bodyDigest, EXPANDED_DIGEST_LENGTH);
 
524
  }
 
525
 
 
526
  nsresult rv = MD5Hash(contents.get(), contents.Length());
 
527
  if (NS_SUCCEEDED(rv))
 
528
    rv = ExpandToHex(mHashBuf, result);
 
529
  return rv;
 
530
}
 
531
 
 
532
nsresult
 
533
nsHttpDigestAuth::ParseChallenge(const char * challenge,
 
534
                                 nsACString & realm,
 
535
                                 nsACString & domain,
 
536
                                 nsACString & nonce,
 
537
                                 nsACString & opaque,
 
538
                                 PRBool * stale,
 
539
                                 PRUint16 * algorithm,
 
540
                                 PRUint16 * qop)
 
541
{
 
542
  const char *p = challenge + 7; // first 7 characters are "Digest "
 
543
 
 
544
  *stale = PR_FALSE;
 
545
  *algorithm = ALGO_MD5; // default is MD5
 
546
  *qop = 0;
 
547
 
 
548
  for (;;) {
 
549
    while (*p && (*p == ',' || nsCRT::IsAsciiSpace(*p)))
 
550
      ++p;
 
551
    if (!*p)
 
552
      break;
 
553
 
 
554
    // name
 
555
    PRInt16 nameStart = (p - challenge);
 
556
    while (*p && !nsCRT::IsAsciiSpace(*p) && *p != '=') 
 
557
      ++p;
 
558
    if (!*p)
 
559
      return NS_ERROR_INVALID_ARG;
 
560
    PRInt16 nameLength = (p - challenge) - nameStart;
 
561
 
 
562
    while (*p && (nsCRT::IsAsciiSpace(*p) || *p == '=')) 
 
563
      ++p;
 
564
    if (!*p) 
 
565
      return NS_ERROR_INVALID_ARG;
 
566
 
 
567
    PRBool quoted = PR_FALSE;
 
568
    if (*p == '"') {
 
569
      ++p;
 
570
      quoted = PR_TRUE;
 
571
    }
 
572
 
 
573
    // value
 
574
    PRInt16 valueStart = (p - challenge);
 
575
    PRInt16 valueLength = 0;
 
576
    if (quoted) {
 
577
      while (*p && *p != '"') 
 
578
        ++p;
 
579
      if (*p != '"') 
 
580
        return NS_ERROR_INVALID_ARG;
 
581
      valueLength = (p - challenge) - valueStart;
 
582
      ++p;
 
583
    } else {
 
584
      while (*p && !nsCRT::IsAsciiSpace(*p) && *p != ',') 
 
585
        ++p; 
 
586
      valueLength = (p - challenge) - valueStart;
 
587
    }
 
588
 
 
589
    // extract information
 
590
    if (nameLength == 5 &&
 
591
        nsCRT::strncasecmp(challenge+nameStart, "realm", 5) == 0)
 
592
    {
 
593
      realm.Assign(challenge+valueStart, valueLength);
 
594
    }
 
595
    else if (nameLength == 6 &&
 
596
        nsCRT::strncasecmp(challenge+nameStart, "domain", 6) == 0)
 
597
    {
 
598
      domain.Assign(challenge+valueStart, valueLength);
 
599
    }
 
600
    else if (nameLength == 5 &&
 
601
        nsCRT::strncasecmp(challenge+nameStart, "nonce", 5) == 0)
 
602
    {
 
603
      nonce.Assign(challenge+valueStart, valueLength);
 
604
    }
 
605
    else if (nameLength == 6 &&
 
606
        nsCRT::strncasecmp(challenge+nameStart, "opaque", 6) == 0)
 
607
    {
 
608
      opaque.Assign(challenge+valueStart, valueLength);
 
609
    }
 
610
    else if (nameLength == 5 &&
 
611
        nsCRT::strncasecmp(challenge+nameStart, "stale", 5) == 0)
 
612
    {
 
613
      if (nsCRT::strncasecmp(challenge+valueStart, "true", 4) == 0)
 
614
        *stale = PR_TRUE;
 
615
      else
 
616
        *stale = PR_FALSE;
 
617
    }
 
618
    else if (nameLength == 9 &&
 
619
        nsCRT::strncasecmp(challenge+nameStart, "algorithm", 9) == 0)
 
620
    {
 
621
      // we want to clear the default, so we use = not |= here
 
622
      *algorithm = ALGO_SPECIFIED;
 
623
      if (valueLength == 3 &&
 
624
          nsCRT::strncasecmp(challenge+valueStart, "MD5", 3) == 0)
 
625
        *algorithm |= ALGO_MD5;
 
626
      else if (valueLength == 8 &&
 
627
          nsCRT::strncasecmp(challenge+valueStart, "MD5-sess", 8) == 0)
 
628
        *algorithm |= ALGO_MD5_SESS;
 
629
    }
 
630
    else if (nameLength == 3 &&
 
631
        nsCRT::strncasecmp(challenge+nameStart, "qop", 3) == 0)
 
632
    {
 
633
      PRInt16 ipos = valueStart;
 
634
      while (ipos < valueStart+valueLength) {
 
635
        while (ipos < valueStart+valueLength &&
 
636
               (nsCRT::IsAsciiSpace(challenge[ipos]) ||
 
637
                challenge[ipos] == ',')) 
 
638
          ipos++;
 
639
        PRInt16 algostart = ipos;
 
640
        while (ipos < valueStart+valueLength &&
 
641
               !nsCRT::IsAsciiSpace(challenge[ipos]) &&
 
642
               challenge[ipos] != ',') 
 
643
          ipos++;
 
644
        if ((ipos - algostart) == 4 &&
 
645
            nsCRT::strncasecmp(challenge+algostart, "auth", 4) == 0)
 
646
          *qop |= QOP_AUTH;
 
647
        else if ((ipos - algostart) == 8 &&
 
648
            nsCRT::strncasecmp(challenge+algostart, "auth-int", 8) == 0)
 
649
          *qop |= QOP_AUTH_INT;
 
650
      }
 
651
    }
 
652
  }
 
653
  return NS_OK;
 
654
}
 
655
 
 
656
// vim: ts=2 sw=2