2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 29 Authenticator */
11 /* The functions in this file handle authentication.
12 * They DO NOT perform access control or auditing.
13 * See acl.c for access control and client_side.c for auditing */
16 #include "auth/CredentialsCache.h"
17 #include "auth/digest/Config.h"
18 #include "auth/digest/Scheme.h"
19 #include "auth/digest/User.h"
20 #include "auth/digest/UserRequest.h"
21 #include "auth/Gadgets.h"
22 #include "auth/State.h"
23 #include "base/LookupTable.h"
28 #include "HttpHeaderTools.h"
29 #include "HttpReply.h"
30 #include "HttpRequest.h"
31 #include "mgr/Registration.h"
34 #include "SquidTime.h"
39 /* digest_nonce_h still uses explicit alloc()/freeOne() MemPool calls.
40 * XXX: convert to MEMPROXY_CLASS() API
46 static AUTHSSTATS authenticateDigestStats
;
48 helper
*digestauthenticators
= NULL
;
50 static hash_table
*digest_nonce_cache
;
52 static int authdigest_initialised
= 0;
53 static MemAllocator
*digest_nonce_pool
= NULL
;
55 enum http_digest_attr_type
{
68 static const LookupTable
<http_digest_attr_type
>::Record
70 {"username", DIGEST_USERNAME
},
71 {"realm", DIGEST_REALM
},
73 {"algorithm", DIGEST_ALGORITHM
},
75 {"nonce", DIGEST_NONCE
},
77 {"cnonce", DIGEST_CNONCE
},
78 {"response", DIGEST_RESPONSE
},
79 {nullptr, DIGEST_INVALID_ATTR
}
82 LookupTable
<http_digest_attr_type
>
83 DigestFieldsLookupTable(DIGEST_INVALID_ATTR
, DigestAttrs
);
91 static void authenticateDigestNonceCacheCleanup(void *data
);
92 static digest_nonce_h
*authenticateDigestNonceFindNonce(const char *nonceb64
);
93 static void authenticateDigestNonceDelete(digest_nonce_h
* nonce
);
94 static void authenticateDigestNonceSetup(void);
95 static void authDigestNonceEncode(digest_nonce_h
* nonce
);
96 static void authDigestNonceLink(digest_nonce_h
* nonce
);
98 static int authDigestNonceLinks(digest_nonce_h
* nonce
);
100 static void authDigestNonceUserUnlink(digest_nonce_h
* nonce
);
103 authDigestNonceEncode(digest_nonce_h
* nonce
)
111 nonce
->key
= xcalloc(base64_encode_len(sizeof(digest_nonce_data
)), 1);
112 struct base64_encode_ctx ctx
;
113 base64_encode_init(&ctx
);
114 size_t blen
= base64_encode_update(&ctx
, reinterpret_cast<uint8_t*>(nonce
->key
), sizeof(digest_nonce_data
), reinterpret_cast<const uint8_t*>(&(nonce
->noncedata
)));
115 blen
+= base64_encode_final(&ctx
, reinterpret_cast<uint8_t*>(nonce
->key
)+blen
);
119 authenticateDigestNonceNew(void)
121 digest_nonce_h
*newnonce
= static_cast < digest_nonce_h
* >(digest_nonce_pool
->alloc());
123 /* NONCE CREATION - NOTES AND REASONING. RBC 20010108
124 * === EXCERPT FROM RFC 2617 ===
125 * The contents of the nonce are implementation dependent. The quality
126 * of the implementation depends on a good choice. A nonce might, for
127 * example, be constructed as the base 64 encoding of
129 * time-stamp H(time-stamp ":" ETag ":" private-key)
131 * where time-stamp is a server-generated time or other non-repeating
132 * value, ETag is the value of the HTTP ETag header associated with
133 * the requested entity, and private-key is data known only to the
134 * server. With a nonce of this form a server would recalculate the
135 * hash portion after receiving the client authentication header and
136 * reject the request if it did not match the nonce from that header
137 * or if the time-stamp value is not recent enough. In this way the
138 * server can limit the time of the nonce's validity. The inclusion of
139 * the ETag prevents a replay request for an updated version of the
140 * resource. (Note: including the IP address of the client in the
141 * nonce would appear to offer the server the ability to limit the
142 * reuse of the nonce to the same client that originally got it.
143 * However, that would break proxy farms, where requests from a single
144 * user often go through different proxies in the farm. Also, IP
145 * address spoofing is not that hard.)
148 * Now for my reasoning:
149 * We will not accept a unrecognised nonce->we have all recognisable
150 * nonces stored. If we send out unique base64 encodings we guarantee
151 * that a given nonce applies to only one user (barring attacks or
152 * really bad timing with expiry and creation). Using a random
153 * component in the nonce allows us to loop to find a unique nonce.
154 * We use H(nonce_data) so the nonce is meaningless to the reciever.
155 * So our nonce looks like base64(H(timestamp,pointertohash,randomdata))
156 * And even if our randomness is not very random we don't really care
157 * - the timestamp and memory pointer also guarantee local uniqueness
158 * in the input to the hash function.
160 // NP: this will likely produce the same randomness sequences for each worker
161 // since they should all start within the 1-second resolution of seed value.
162 static std::mt19937
mt(static_cast<uint32_t>(getCurrentTime() & 0xFFFFFFFF));
163 static xuniform_int_distribution
<uint32_t> newRandomData
;
165 /* create a new nonce */
167 newnonce
->flags
.valid
= true;
168 newnonce
->noncedata
.self
= newnonce
;
169 newnonce
->noncedata
.creationtime
= current_time
.tv_sec
;
170 newnonce
->noncedata
.randomdata
= newRandomData(mt
);
172 authDigestNonceEncode(newnonce
);
174 // ensure temporal uniqueness by checking for existing nonce
175 while (authenticateDigestNonceFindNonce((char const *) (newnonce
->key
))) {
176 /* create a new nonce */
177 newnonce
->noncedata
.randomdata
= newRandomData(mt
);
178 authDigestNonceEncode(newnonce
);
181 hash_join(digest_nonce_cache
, newnonce
);
182 /* the cache's link */
183 authDigestNonceLink(newnonce
);
184 newnonce
->flags
.incache
= true;
185 debugs(29, 5, "created nonce " << newnonce
<< " at " << newnonce
->noncedata
.creationtime
);
190 authenticateDigestNonceDelete(digest_nonce_h
* nonce
)
193 assert(nonce
->references
== 0);
196 if (nonce
->flags
.incache
)
197 hash_remove_link(digest_nonce_cache
, nonce
);
201 assert(!nonce
->flags
.incache
);
203 safe_free(nonce
->key
);
205 digest_nonce_pool
->freeOne(nonce
);
210 authenticateDigestNonceSetup(void)
212 if (!digest_nonce_pool
)
213 digest_nonce_pool
= memPoolCreate("Digest Scheme nonce's", sizeof(digest_nonce_h
));
215 if (!digest_nonce_cache
) {
216 digest_nonce_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
217 assert(digest_nonce_cache
);
218 eventAdd("Digest none cache maintenance", authenticateDigestNonceCacheCleanup
, NULL
, static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->nonceGCInterval
, 1);
223 authenticateDigestNonceShutdown(void)
226 * We empty the cache of any nonces left in there.
228 digest_nonce_h
*nonce
;
230 if (digest_nonce_cache
) {
231 debugs(29, 2, "Shutting down nonce cache");
232 hash_first(digest_nonce_cache
);
234 while ((nonce
= ((digest_nonce_h
*) hash_next(digest_nonce_cache
)))) {
235 assert(nonce
->flags
.incache
);
236 authDigestNoncePurge(nonce
);
241 if (digest_nonce_pool
) {
242 delete digest_nonce_pool
;
243 digest_nonce_pool
= NULL
;
247 debugs(29, 2, "Nonce cache shutdown");
251 authenticateDigestNonceCacheCleanup(void *)
254 * We walk the hash by nonceb64 as that is the unique key we
255 * use. For big hash tables we could consider stepping through
256 * the cache, 100/200 entries at a time. Lets see how it flies
259 digest_nonce_h
*nonce
;
260 debugs(29, 3, "Cleaning the nonce cache now");
261 debugs(29, 3, "Current time: " << current_time
.tv_sec
);
262 hash_first(digest_nonce_cache
);
264 while ((nonce
= ((digest_nonce_h
*) hash_next(digest_nonce_cache
)))) {
265 debugs(29, 3, "nonce entry : " << nonce
<< " '" << (char *) nonce
->key
<< "'");
266 debugs(29, 4, "Creation time: " << nonce
->noncedata
.creationtime
);
268 if (authDigestNonceIsStale(nonce
)) {
269 debugs(29, 4, "Removing nonce " << (char *) nonce
->key
<< " from cache due to timeout.");
270 assert(nonce
->flags
.incache
);
271 /* invalidate nonce so future requests fail */
272 nonce
->flags
.valid
= false;
273 /* if it is tied to a auth_user, remove the tie */
274 authDigestNonceUserUnlink(nonce
);
275 authDigestNoncePurge(nonce
);
279 debugs(29, 3, "Finished cleaning the nonce cache.");
281 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->active())
282 eventAdd("Digest none cache maintenance", authenticateDigestNonceCacheCleanup
, NULL
, static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->nonceGCInterval
, 1);
286 authDigestNonceLink(digest_nonce_h
* nonce
)
288 assert(nonce
!= NULL
);
290 debugs(29, 9, "nonce '" << nonce
<< "' now at '" << nonce
->references
<< "'.");
295 authDigestNonceLinks(digest_nonce_h
* nonce
)
300 return nonce
->references
;
306 authDigestNonceUnlink(digest_nonce_h
* nonce
)
308 assert(nonce
!= NULL
);
310 if (nonce
->references
> 0) {
311 -- nonce
->references
;
313 debugs(29, DBG_IMPORTANT
, "Attempt to lower nonce " << nonce
<< " refcount below 0!");
316 debugs(29, 9, "nonce '" << nonce
<< "' now at '" << nonce
->references
<< "'.");
318 if (nonce
->references
== 0)
319 authenticateDigestNonceDelete(nonce
);
323 authenticateDigestNonceNonceb64(const digest_nonce_h
* nonce
)
328 return (char const *) nonce
->key
;
331 static digest_nonce_h
*
332 authenticateDigestNonceFindNonce(const char *nonceb64
)
334 digest_nonce_h
*nonce
= NULL
;
336 if (nonceb64
== NULL
)
339 debugs(29, 9, "looking for nonceb64 '" << nonceb64
<< "' in the nonce cache.");
341 nonce
= static_cast < digest_nonce_h
* >(hash_lookup(digest_nonce_cache
, nonceb64
));
343 if ((nonce
== NULL
) || (strcmp(authenticateDigestNonceNonceb64(nonce
), nonceb64
)))
346 debugs(29, 9, "Found nonce '" << nonce
<< "'");
352 authDigestNonceIsValid(digest_nonce_h
* nonce
, char nc
[9])
355 /* do we have a nonce ? */
360 intnc
= strtol(nc
, NULL
, 16);
362 /* has it already been invalidated ? */
363 if (!nonce
->flags
.valid
) {
364 debugs(29, 4, "Nonce already invalidated");
368 /* is the nonce-count ok ? */
369 if (!static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->CheckNonceCount
) {
370 /* Ignore client supplied NC */
371 intnc
= nonce
->nc
+ 1;
374 if ((static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->NonceStrictness
&& intnc
!= nonce
->nc
+ 1) ||
375 intnc
< nonce
->nc
+ 1) {
376 debugs(29, 4, "Nonce count doesn't match");
377 nonce
->flags
.valid
= false;
381 /* increment the nonce count - we've already checked that intnc is a
382 * valid representation for us, so we don't need the test here.
386 return !authDigestNonceIsStale(nonce
);
390 authDigestNonceIsStale(digest_nonce_h
* nonce
)
392 /* do we have a nonce ? */
397 /* Is it already invalidated? */
398 if (!nonce
->flags
.valid
)
401 /* has it's max duration expired? */
402 if (nonce
->noncedata
.creationtime
+ static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->noncemaxduration
< current_time
.tv_sec
) {
403 debugs(29, 4, "Nonce is too old. " <<
404 nonce
->noncedata
.creationtime
<< " " <<
405 static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->noncemaxduration
<< " " <<
406 current_time
.tv_sec
);
408 nonce
->flags
.valid
= false;
412 if (nonce
->nc
> 99999998) {
413 debugs(29, 4, "Nonce count overflow");
414 nonce
->flags
.valid
= false;
418 if (nonce
->nc
> static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->noncemaxuses
) {
419 debugs(29, 4, "Nonce count over user limit");
420 nonce
->flags
.valid
= false;
429 * \retval 0 the digest is not stale yet
430 * \retval -1 the digest will be stale on the next request
433 authDigestNonceLastRequest(digest_nonce_h
* nonce
)
438 if (nonce
->nc
== 99999997) {
439 debugs(29, 4, "Nonce count about to overflow");
443 if (nonce
->nc
>= static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->noncemaxuses
- 1) {
444 debugs(29, 4, "Nonce count about to hit user limit");
448 /* and other tests are possible. */
453 authDigestNoncePurge(digest_nonce_h
* nonce
)
458 if (!nonce
->flags
.incache
)
461 hash_remove_link(digest_nonce_cache
, nonce
);
463 nonce
->flags
.incache
= false;
465 /* the cache's link */
466 authDigestNonceUnlink(nonce
);
470 Auth::Digest::Config::rotateHelpers()
472 /* schedule closure of existing helpers */
473 if (digestauthenticators
) {
474 helperShutdown(digestauthenticators
);
477 /* NP: dynamic helper restart will ensure they start up again as needed. */
481 Auth::Digest::Config::dump(StoreEntry
* entry
, const char *name
, Auth::Config
* scheme
) const
483 if (!Auth::Config::dump(entry
, name
, scheme
))
486 storeAppendPrintf(entry
, "%s %s nonce_max_count %d\n%s %s nonce_max_duration %d seconds\n%s %s nonce_garbage_interval %d seconds\n",
487 name
, "digest", noncemaxuses
,
488 name
, "digest", (int) noncemaxduration
,
489 name
, "digest", (int) nonceGCInterval
);
490 storeAppendPrintf(entry
, "%s digest utf8 %s\n", name
, utf8
? "on" : "off");
495 Auth::Digest::Config::active() const
497 return authdigest_initialised
== 1;
501 Auth::Digest::Config::configured() const
503 if ((authenticateProgram
!= NULL
) &&
504 (authenticateChildren
.n_max
!= 0) &&
505 !realm
.isEmpty() && (noncemaxduration
> -1))
511 /* add the [www-|Proxy-]authenticate header on a 407 or 401 reply */
513 Auth::Digest::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request
, HttpReply
*rep
, Http::HdrType hdrType
, HttpRequest
*)
515 if (!authenticateProgram
)
519 digest_nonce_h
*nonce
= NULL
;
521 /* on a 407 or 401 we always use a new nonce */
522 if (auth_user_request
!= NULL
) {
523 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
526 stale
= digest_user
->credentials() == Auth::Handshake
;
528 nonce
= digest_user
->currentNonce();
533 nonce
= authenticateDigestNonceNew();
536 debugs(29, 9, "Sending type:" << hdrType
<<
537 " header: 'Digest realm=\"" << realm
<< "\", nonce=\"" <<
538 authenticateDigestNonceNonceb64(nonce
) << "\", qop=\"" << QOP_AUTH
<<
539 "\", stale=" << (stale
? "true" : "false"));
541 /* in the future, for WWW auth we may want to support the domain entry */
542 httpHeaderPutStrf(&rep
->header
, hdrType
, "Digest realm=\"" SQUIDSBUFPH
"\", nonce=\"%s\", qop=\"%s\", stale=%s",
543 SQUIDSBUFPRINT(realm
), authenticateDigestNonceNonceb64(nonce
), QOP_AUTH
, stale
? "true" : "false");
546 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
549 Auth::Digest::Config::init(Auth::Config
*)
551 if (authenticateProgram
) {
552 authenticateDigestNonceSetup();
553 authdigest_initialised
= 1;
555 if (digestauthenticators
== NULL
)
556 digestauthenticators
= new helper("digestauthenticator");
558 digestauthenticators
->cmdline
= authenticateProgram
;
560 digestauthenticators
->childs
.updateLimits(authenticateChildren
);
562 digestauthenticators
->ipc_type
= IPC_STREAM
;
564 helperOpenServers(digestauthenticators
);
569 Auth::Digest::Config::registerWithCacheManager(void)
571 Mgr::RegisterAction("digestauthenticator",
572 "Digest User Authenticator Stats",
573 authenticateDigestStats
, 0, 1);
576 /* free any allocated configuration details */
578 Auth::Digest::Config::done()
580 Auth::Config::done();
582 authdigest_initialised
= 0;
584 if (digestauthenticators
)
585 helperShutdown(digestauthenticators
);
590 delete digestauthenticators
;
591 digestauthenticators
= NULL
;
593 if (authenticateProgram
)
594 wordlistDestroy(&authenticateProgram
);
597 Auth::Digest::Config::Config() :
598 nonceGCInterval(5*60),
599 noncemaxduration(30*60),
608 Auth::Digest::Config::parse(Auth::Config
* scheme
, int n_configured
, char *param_str
)
610 if (strcmp(param_str
, "program") == 0) {
611 if (authenticateProgram
)
612 wordlistDestroy(&authenticateProgram
);
614 parse_wordlist(&authenticateProgram
);
616 requirePathnameExists("auth_param digest program", authenticateProgram
->key
);
617 } else if (strcmp(param_str
, "nonce_garbage_interval") == 0) {
618 parse_time_t(&nonceGCInterval
);
619 } else if (strcmp(param_str
, "nonce_max_duration") == 0) {
620 parse_time_t(&noncemaxduration
);
621 } else if (strcmp(param_str
, "nonce_max_count") == 0) {
622 parse_int((int *) &noncemaxuses
);
623 } else if (strcmp(param_str
, "nonce_strictness") == 0) {
624 parse_onoff(&NonceStrictness
);
625 } else if (strcmp(param_str
, "check_nonce_count") == 0) {
626 parse_onoff(&CheckNonceCount
);
627 } else if (strcmp(param_str
, "post_workaround") == 0) {
628 parse_onoff(&PostWorkaround
);
629 } else if (strcmp(param_str
, "utf8") == 0) {
632 Auth::Config::parse(scheme
, n_configured
, param_str
);
636 Auth::Digest::Config::type() const
638 return Auth::Digest::Scheme::GetInstance()->type();
642 authenticateDigestStats(StoreEntry
* sentry
)
644 if (digestauthenticators
)
645 digestauthenticators
->packStatsInto(sentry
, "Digest Authenticator Statistics");
648 /* NonceUserUnlink: remove the reference to auth_user and unlink the node from the list */
651 authDigestNonceUserUnlink(digest_nonce_h
* nonce
)
653 Auth::Digest::User
*digest_user
;
654 dlink_node
*link
, *tmplink
;
662 digest_user
= nonce
->user
;
664 /* unlink from the user list. Yes we're crossing structures but this is the only
665 * time this code is needed
667 link
= digest_user
->nonces
.head
;
673 if (tmplink
->data
== nonce
) {
674 dlinkDelete(tmplink
, &digest_user
->nonces
);
675 authDigestNonceUnlink(static_cast < digest_nonce_h
* >(tmplink
->data
));
681 /* this reference to user was not locked because freeeing the user frees
687 /* authDigesteserLinkNonce: add a nonce to a given user's struct */
689 authDigestUserLinkNonce(Auth::Digest::User
* user
, digest_nonce_h
* nonce
)
693 if (!user
|| !nonce
|| !nonce
->user
)
696 Auth::Digest::User
*digest_user
= user
;
698 node
= digest_user
->nonces
.head
;
700 while (node
&& (node
->data
!= nonce
))
706 node
= new dlink_node
;
708 dlinkAddTail(nonce
, node
, &digest_user
->nonces
);
710 authDigestNonceLink(nonce
);
712 /* ping this nonce to this auth user */
713 assert((nonce
->user
== NULL
) || (nonce
->user
== user
));
715 /* we don't lock this reference because removing the user removes the
716 * hash too. Of course if that changes we're stuffed so read the code huh?
721 /* setup the necessary info to log the username */
722 static Auth::UserRequest::Pointer
723 authDigestLogUsername(char *username
, Auth::UserRequest::Pointer auth_user_request
, const char *requestRealm
)
725 assert(auth_user_request
!= NULL
);
727 /* log the username */
728 debugs(29, 9, "Creating new user for logging '" << (username
?username
:"[no username]") << "'");
729 Auth::User::Pointer digest_user
= new Auth::Digest::User(static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest")), requestRealm
);
730 /* save the credentials */
731 digest_user
->username(username
);
732 /* set the auth_user type */
733 digest_user
->auth_type
= Auth::AUTH_BROKEN
;
734 /* link the request to the user */
735 auth_user_request
->user(digest_user
);
736 return auth_user_request
;
740 * Decode a Digest [Proxy-]Auth string, placing the results in the passed
741 * Auth_user structure.
743 Auth::UserRequest::Pointer
744 Auth::Digest::Config::decode(char const *proxy_auth
, const char *aRequestRealm
)
748 const char *pos
= NULL
;
749 char *username
= NULL
;
750 digest_nonce_h
*nonce
;
753 debugs(29, 9, "beginning");
755 Auth::Digest::UserRequest
*digest_request
= new Auth::Digest::UserRequest();
757 /* trim DIGEST from string */
759 while (xisgraph(*proxy_auth
))
762 /* Trim leading whitespace before decoding */
763 while (xisspace(*proxy_auth
))
766 String
temp(proxy_auth
);
768 while (strListGetItem(&temp
, ',', &item
, &ilen
, &pos
)) {
769 /* isolate directive name & value */
772 if ((p
= (const char *)memchr(item
, '=', ilen
)) && (p
- item
< ilen
)) {
775 vlen
= ilen
- (p
- item
);
781 SBuf
keyName(item
, nlen
);
785 // see RFC 2617 section 3.2.1 and 3.2.2 for details on the BNF
787 if (keyName
== SBuf("domain",6) || keyName
== SBuf("uri",3)) {
788 // domain is Special. Not a quoted-string, must not be de-quoted. But is wrapped in '"'
789 // BUG 3077: uri= can also be sent to us in a mangled (invalid!) form like domain
790 if (*p
== '"' && *(p
+ vlen
-1) == '"') {
791 value
.limitInit(p
+1, vlen
-2);
793 } else if (keyName
== SBuf("qop",3)) {
794 // qop is more special.
795 // On request this must not be quoted-string de-quoted. But is several values wrapped in '"'
796 // On response this is a single un-quoted token.
797 if (*p
== '"' && *(p
+ vlen
-1) == '"') {
798 value
.limitInit(p
+1, vlen
-2);
800 value
.limitInit(p
, vlen
);
802 } else if (*p
== '"') {
803 if (!httpHeaderParseQuotedString(p
, vlen
, &value
)) {
804 debugs(29, 9, "Failed to parse attribute '" << item
<< "' in '" << temp
<< "'");
808 value
.limitInit(p
, vlen
);
811 debugs(29, 9, "Failed to parse attribute '" << item
<< "' in '" << temp
<< "'");
816 const http_digest_attr_type t
= DigestFieldsLookupTable
.lookup(keyName
);
819 case DIGEST_USERNAME
:
821 if (value
.size() != 0)
822 username
= xstrndup(value
.rawBuf(), value
.size() + 1);
823 debugs(29, 9, "Found Username '" << username
<< "'");
827 safe_free(digest_request
->realm
);
828 if (value
.size() != 0)
829 digest_request
->realm
= xstrndup(value
.rawBuf(), value
.size() + 1);
830 debugs(29, 9, "Found realm '" << digest_request
->realm
<< "'");
834 safe_free(digest_request
->qop
);
835 if (value
.size() != 0)
836 digest_request
->qop
= xstrndup(value
.rawBuf(), value
.size() + 1);
837 debugs(29, 9, "Found qop '" << digest_request
->qop
<< "'");
840 case DIGEST_ALGORITHM
:
841 safe_free(digest_request
->algorithm
);
842 if (value
.size() != 0)
843 digest_request
->algorithm
= xstrndup(value
.rawBuf(), value
.size() + 1);
844 debugs(29, 9, "Found algorithm '" << digest_request
->algorithm
<< "'");
848 safe_free(digest_request
->uri
);
849 if (value
.size() != 0)
850 digest_request
->uri
= xstrndup(value
.rawBuf(), value
.size() + 1);
851 debugs(29, 9, "Found uri '" << digest_request
->uri
<< "'");
855 safe_free(digest_request
->nonceb64
);
856 if (value
.size() != 0)
857 digest_request
->nonceb64
= xstrndup(value
.rawBuf(), value
.size() + 1);
858 debugs(29, 9, "Found nonce '" << digest_request
->nonceb64
<< "'");
862 if (value
.size() != 8) {
863 debugs(29, 9, "Invalid nc '" << value
<< "' in '" << temp
<< "'");
865 xstrncpy(digest_request
->nc
, value
.rawBuf(), value
.size() + 1);
866 debugs(29, 9, "Found noncecount '" << digest_request
->nc
<< "'");
870 safe_free(digest_request
->cnonce
);
871 if (value
.size() != 0)
872 digest_request
->cnonce
= xstrndup(value
.rawBuf(), value
.size() + 1);
873 debugs(29, 9, "Found cnonce '" << digest_request
->cnonce
<< "'");
876 case DIGEST_RESPONSE
:
877 safe_free(digest_request
->response
);
878 if (value
.size() != 0)
879 digest_request
->response
= xstrndup(value
.rawBuf(), value
.size() + 1);
880 debugs(29, 9, "Found response '" << digest_request
->response
<< "'");
884 debugs(29, 3, "Unknown attribute '" << item
<< "' in '" << temp
<< "'");
891 /* now we validate the data given to us */
894 * TODO: on invalid parameters we should return 400, not 407.
895 * Find some clean way of doing this. perhaps return a valid
896 * struct, and set the direction to clientwards combined with
897 * a change to the clientwards handling code (ie let the
898 * clientwards call set the error type (but limited to known
899 * correct values - 400/401/407
902 /* 2069 requirements */
905 Auth::UserRequest::Pointer rv
;
906 /* do we have a username ? */
907 if (!username
|| username
[0] == '\0') {
908 debugs(29, 2, "Empty or not present username");
909 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
914 /* Sanity check of the username.
915 * " can not be allowed in usernames until * the digest helper protocol
918 if (strchr(username
, '"')) {
919 debugs(29, 2, "Unacceptable username '" << username
<< "'");
920 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
925 /* do we have a realm ? */
926 if (!digest_request
->realm
|| digest_request
->realm
[0] == '\0') {
927 debugs(29, 2, "Empty or not present realm");
928 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
934 if (!digest_request
->nonceb64
|| digest_request
->nonceb64
[0] == '\0') {
935 debugs(29, 2, "Empty or not present nonce");
936 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
941 /* we can't check the URI just yet. We'll check it in the
942 * authenticate phase, but needs to be given */
943 if (!digest_request
->uri
|| digest_request
->uri
[0] == '\0') {
944 debugs(29, 2, "Missing URI field");
945 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
950 /* is the response the correct length? */
951 if (!digest_request
->response
|| strlen(digest_request
->response
) != 32) {
952 debugs(29, 2, "Response length invalid");
953 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
958 /* check the algorithm is present and supported */
959 if (!digest_request
->algorithm
)
960 digest_request
->algorithm
= xstrndup("MD5", 4);
961 else if (strcmp(digest_request
->algorithm
, "MD5")
962 && strcmp(digest_request
->algorithm
, "MD5-sess")) {
963 debugs(29, 2, "invalid algorithm specified!");
964 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
969 /* 2617 requirements, indicated by qop */
970 if (digest_request
->qop
) {
972 /* check the qop is what we expected. */
973 if (strcmp(digest_request
->qop
, QOP_AUTH
) != 0) {
974 /* we received a qop option we didn't send */
975 debugs(29, 2, "Invalid qop option received");
976 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
982 if (!digest_request
->cnonce
|| digest_request
->cnonce
[0] == '\0') {
983 debugs(29, 2, "Missing cnonce field");
984 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
990 if (strlen(digest_request
->nc
) != 8 || strspn(digest_request
->nc
, "0123456789abcdefABCDEF") != 8) {
991 debugs(29, 2, "invalid nonce count");
992 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
997 /* cnonce and nc both require qop */
998 if (digest_request
->cnonce
|| digest_request
->nc
[0] != '\0') {
999 debugs(29, 2, "missing qop!");
1000 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
1001 safe_free(username
);
1006 /** below nonce state dependent **/
1009 nonce
= authenticateDigestNonceFindNonce(digest_request
->nonceb64
);
1010 /* check that we're not being hacked / the username hasn't changed */
1011 if (nonce
&& nonce
->user
&& strcmp(username
, nonce
->user
->username())) {
1012 debugs(29, 2, "Username for the nonce does not equal the username for the request");
1017 /* we couldn't find a matching nonce! */
1018 debugs(29, 2, "Unexpected or invalid nonce received from " << username
);
1019 Auth::UserRequest::Pointer auth_request
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
1020 auth_request
->user()->credentials(Auth::Handshake
);
1021 safe_free(username
);
1022 return auth_request
;
1025 digest_request
->nonce
= nonce
;
1026 authDigestNonceLink(nonce
);
1028 /* check that we're not being hacked / the username hasn't changed */
1029 if (nonce
->user
&& strcmp(username
, nonce
->user
->username())) {
1030 debugs(29, 2, "Username for the nonce does not equal the username for the request");
1031 rv
= authDigestLogUsername(username
, digest_request
, aRequestRealm
);
1032 safe_free(username
);
1036 /* the method we'll check at the authenticate step as well */
1038 /* we don't send or parse opaques. Ok so we're flexable ... */
1041 Auth::Digest::User
*digest_user
;
1043 Auth::User::Pointer auth_user
;
1045 SBuf key
= Auth::User::BuildUserKey(username
, aRequestRealm
);
1046 if (key
.isEmpty() || !(auth_user
= Auth::Digest::User::Cache()->lookup(key
))) {
1047 /* the user doesn't exist in the username cache yet */
1048 debugs(29, 9, "Creating new digest user '" << username
<< "'");
1049 digest_user
= new Auth::Digest::User(this, aRequestRealm
);
1050 /* auth_user is a parent */
1051 auth_user
= digest_user
;
1052 /* save the username */
1053 digest_user
->username(username
);
1054 /* set the user type */
1055 digest_user
->auth_type
= Auth::AUTH_DIGEST
;
1056 /* this auth_user struct is the one to get added to the
1058 /* store user in hash's */
1059 digest_user
->addToNameCache();
1062 * Add the digest to the user so we can tell if a hacking
1063 * or spoofing attack is taking place. We do this by assuming
1064 * the user agent won't change user name without warning.
1066 authDigestUserLinkNonce(digest_user
, nonce
);
1068 debugs(29, 9, "Found user '" << username
<< "' in the user cache as '" << auth_user
<< "'");
1069 digest_user
= static_cast<Auth::Digest::User
*>(auth_user
.getRaw());
1070 digest_user
->credentials(Auth::Unchecked
);
1074 /*link the request and the user */
1075 assert(digest_request
!= NULL
);
1077 digest_request
->user(digest_user
);
1078 debugs(29, 9, "username = '" << digest_user
->username() << "'\nrealm = '" <<
1079 digest_request
->realm
<< "'\nqop = '" << digest_request
->qop
<<
1080 "'\nalgorithm = '" << digest_request
->algorithm
<< "'\nuri = '" <<
1081 digest_request
->uri
<< "'\nnonce = '" << digest_request
->nonceb64
<<
1082 "'\nnc = '" << digest_request
->nc
<< "'\ncnonce = '" <<
1083 digest_request
->cnonce
<< "'\nresponse = '" <<
1084 digest_request
->response
<< "'\ndigestnonce = '" << nonce
<< "'");
1086 return digest_request
;