6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2002 The FreeRADIUS server project
21 * Copyright 2002 Alan DeKok <aland@ox.org>
34 static const char rcsid[] = "$Id$";
36 static int digest_authorize(void *instance, REQUEST *request)
40 /* quiet the compiler */
44 * We need both of these attributes to do the authentication.
46 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
48 return RLM_MODULE_NOOP;
52 * Check the sanity of the attribute.
54 if (vp->length != 32) {
55 DEBUG("ERROR: Received invalid Digest-Response attribute (length %d should be 32)", vp->length);
56 return RLM_MODULE_INVALID;
62 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
64 DEBUG("ERROR: Received Digest-Response without Digest-Attributes");
65 return RLM_MODULE_INVALID;
69 * Everything's OK, add a digest authentication type.
71 if (pairfind(request->config_items, PW_AUTHTYPE) == NULL) {
72 DEBUG("rlm_digest: Adding Auth-Type = DIGEST");
73 pairadd(&request->config_items,
74 pairmake("Auth-Type", "DIGEST", T_OP_EQ));
81 * Perform all of the wondrous variants of digest authentication.
83 static int digest_authenticate(void *instance, REQUEST *request)
86 size_t a1_len, a2_len, kd_len;
87 uint8_t a1[(MAX_STRING_LEN + 1) * 5]; /* can be 5 attributes */
88 uint8_t a2[(MAX_STRING_LEN + 1) * 3]; /* can be 3 attributes */
89 uint8_t kd[(MAX_STRING_LEN + 1) * 5];
90 uint8_t hash[16]; /* MD5 output */
91 VALUE_PAIR *vp, *passwd, *algo;
92 VALUE_PAIR *qop, *nonce;
94 instance = instance; /* -Wunused */
97 * We require access to the plain-text password.
99 passwd = pairfind(request->config_items, PW_PASSWORD);
100 if (!passwd) passwd = pairfind(request->config_items, PW_DIGEST_HA1);
102 radlog(L_AUTH, "rlm_digest: Configuration item \"User-Password\" or MD5-Password is required for authentication.");
103 return RLM_MODULE_INVALID;
107 * We need these, too.
109 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
111 DEBUG("ERROR: You set 'Auth-Type = Digest' for a request that did not contain any digest attributes!");
112 return RLM_MODULE_INVALID;
116 * Loop through the Digest-Attributes, sanity checking them.
118 DEBUG(" rlm_digest: Converting Digest-Attributes to something sane...");
120 int length = vp->length;
122 uint8_t *p = &vp->strvalue[0];
126 * Until this stupidly encoded attribure is exhausted.
130 * The attribute type must be valid
132 if ((p[0] == 0) || (p[0] > 10)) {
133 DEBUG("ERROR: Received Digest-Attributes with invalid sub-attribute %d", p[0]);
134 return RLM_MODULE_INVALID;
137 attrlen = p[1]; /* stupid VSA format */
143 DEBUG("ERROR: Received Digest-Attributes with short sub-attribute %d, of length %d", p[0], attrlen);
144 return RLM_MODULE_INVALID;
150 if (attrlen > length) {
151 DEBUG("ERROR: Received Digest-Attributes with long sub-attribute %d, of length %d", p[0], attrlen);
152 return RLM_MODULE_INVALID;
156 * Create a new attribute, broken out of
157 * the stupid sub-attribute crap.
159 * Didn't they know that VSA's exist?
161 sub = paircreate(PW_DIGEST_REALM - 1 + p[0],
164 return RLM_MODULE_FAIL; /* out of memory */
166 memcpy(&sub->strvalue[0], &p[2], attrlen - 2);
167 sub->strvalue[attrlen - 2] = '\0';
168 sub->length = attrlen - 2;
172 vp_print(stdout, sub);
177 * And add it to the request pairs.
179 pairadd(&request->packet->vps, sub);
182 * FIXME: Check for the existence
183 * of the necessary attributes!
188 } /* loop over this one attribute */
191 * Find the next one, if it exists.
193 vp = pairfind(vp->next, PW_DIGEST_ATTRIBUTES);
197 * We require access to the Digest-Nonce-Value
199 nonce = pairfind(request->packet->vps, PW_DIGEST_NONCE);
201 DEBUG("ERROR: No Digest-Nonce: Cannot perform Digest authentication");
202 return RLM_MODULE_INVALID;
206 * A1 = Digest-User-Name ":" Realm ":" Password
208 vp = pairfind(request->packet->vps, PW_DIGEST_USER_NAME);
210 DEBUG("ERROR: No Digest-User-Name: Cannot perform Digest authentication");
211 return RLM_MODULE_INVALID;
213 memcpy(&a1[0], &vp->strvalue[0], vp->length);
219 vp = pairfind(request->packet->vps, PW_DIGEST_REALM);
221 DEBUG("ERROR: No Digest-Realm: Cannot perform Digest authentication");
222 return RLM_MODULE_INVALID;
224 memcpy(&a1[a1_len], &vp->strvalue[0], vp->length);
225 a1_len += vp->length;
230 if (passwd->attribute == PW_USER_PASSWORD) {
231 memcpy(&a1[a1_len], &passwd->strvalue[0], passwd->length);
232 a1_len += passwd->length;
234 DEBUG2("A1 = %s", a1);
237 DEBUG2("A1 = %s (using Digest-HA1)", a1);
242 * See which variant we calculate.
243 * Assume MD5 if no Digest-Algorithm attribute received
245 algo = pairfind(request->packet->vps, PW_DIGEST_ALGORITHM);
246 if ((algo == NULL) ||
247 (strcasecmp(algo->strvalue, "MD5") == 0)) {
249 * Set A1 to Digest-HA1 if no User-Password found
251 if (passwd->attribute != PW_USER_PASSWORD) {
252 memcpy(&a1[0], passwd->strvalue, 16);
255 } else if (strcasecmp(algo->strvalue, "MD5-sess") == 0) {
257 * K1 = H(A1) : Digest-Nonce ... : H(A2)
259 * If we find Digest-HA1, we assume it contains
262 if (passwd->attribute == PW_USER_PASSWORD) {
263 librad_md5_calc(hash, &a1[0], a1_len);
264 lrad_bin2hex(hash, &a1[0], 16);
266 lrad_bin2hex(passwd->strvalue, &a1[0], 16);
274 * Tack on the Digest-Nonce. Length must be even
276 if ((nonce->length & 1) != 0) {
277 DEBUG("ERROR: Received Digest-Nonce hex string with invalid length: Cannot perform Digest authentication");
278 return RLM_MODULE_INVALID;
280 memcpy(&a1[a1_len], &nonce->strvalue[0], nonce->length);
281 a1_len += nonce->length;
286 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
288 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
289 return RLM_MODULE_INVALID;
293 * Digest-CNonce length must be even
295 if ((vp->length & 1) != 0) {
296 DEBUG("ERROR: Received Digest-CNonce hex string with invalid length: Cannot perform Digest authentication");
297 return RLM_MODULE_INVALID;
299 memcpy(&a1[a1_len], &vp->strvalue[0], vp->length);
300 a1_len += vp->length;
302 } else if ((algo != NULL) &&
303 (strcasecmp(algo->strvalue, "MD5") != 0)) {
305 * We check for "MD5-sess" and "MD5".
306 * Anything else is an error.
308 DEBUG("ERROR: Unknown Digest-Algorithm \"%s\": Cannot perform Digest authentication", vp->strvalue);
309 return RLM_MODULE_INVALID;
313 * A2 = Digest-Method ":" Digest-URI
315 vp = pairfind(request->packet->vps, PW_DIGEST_METHOD);
317 DEBUG("ERROR: No Digest-Method: Cannot perform Digest authentication");
318 return RLM_MODULE_INVALID;
320 memcpy(&a2[0], &vp->strvalue[0], vp->length);
326 vp = pairfind(request->packet->vps, PW_DIGEST_URI);
328 DEBUG("ERROR: No Digest-URI: Cannot perform Digest authentication");
329 return RLM_MODULE_INVALID;
331 memcpy(&a2[a2_len], &vp->strvalue[0], vp->length);
332 a2_len += vp->length;
335 * QOP is "auth-int", tack on ": Digest-Body-Digest"
337 qop = pairfind(request->packet->vps, PW_DIGEST_QOP);
339 (strcasecmp(qop->strvalue, "auth-int") == 0)) {
343 * Add in Digest-Body-Digest
349 * Must be a hex representation of an MD5 digest.
351 body = pairfind(request->packet->vps, PW_DIGEST_BODY_DIGEST);
353 DEBUG("ERROR: No Digest-Body-Digest: Cannot perform Digest authentication");
354 return RLM_MODULE_INVALID;
357 if ((a2_len + body->length) > sizeof(a2)) {
358 DEBUG("ERROR: Digest-Body-Digest is too long");
359 return RLM_MODULE_INVALID;
362 memcpy(a2 + a2_len, body->strvalue, body->length);
363 a2_len += body->length;
365 } else if ((qop != NULL) &&
366 (strcasecmp(qop->strvalue, "auth") != 0)) {
367 DEBUG("ERROR: Unknown Digest-QOP \"%s\": Cannot perform Digest authentication", qop->strvalue);
368 return RLM_MODULE_INVALID;
372 DEBUG2("A2 = %s", a2);
375 * KD = H(A1) : Digest-Nonce ... : H(A2).
376 * Compute MD5 if Digest-Algorithm == "MD5-Sess",
377 * or if we found a User-Password.
379 if (((algo != NULL) &&
380 (strcasecmp(algo->strvalue, "MD5-Sess") == 0)) ||
381 (passwd->attribute == PW_USER_PASSWORD)) {
383 librad_md5_calc(&hash[0], &a1[0], a1_len);
385 memcpy(&hash[0], &a1[0], a1_len);
387 lrad_bin2hex(hash, kd, sizeof(hash));
392 for (i = 0; i < 16; i++) {
393 printf("%02x", hash[i]);
403 memcpy(&kd[kd_len], nonce->strvalue, nonce->length);
404 kd_len += nonce->length;
407 * No QOP defined. Do RFC 2069 compatibility.
414 } else { /* Digest-QOP MUST be "auth" or "auth-int" */
416 * Tack on ":" Digest-Nonce-Count ":" Digest-CNonce
422 vp = pairfind(request->packet->vps, PW_DIGEST_NONCE_COUNT);
424 DEBUG("ERROR: No Digest-Nonce-Count: Cannot perform Digest authentication");
425 return RLM_MODULE_INVALID;
427 memcpy(&kd[kd_len], &vp->strvalue[0], vp->length);
428 kd_len += vp->length;
433 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
435 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
436 return RLM_MODULE_INVALID;
438 memcpy(&kd[kd_len], &vp->strvalue[0], vp->length);
439 kd_len += vp->length;
444 memcpy(&kd[kd_len], &qop->strvalue[0], qop->length);
445 kd_len += qop->length;
454 librad_md5_calc(&hash[0], &a2[0], a2_len);
456 lrad_bin2hex(hash, kd + kd_len, sizeof(hash));
461 for (i = 0; i < 16; i++) {
462 printf("%02x", hash[i]);
471 DEBUG2("KD = %s\n", &kd[0]);
474 * Take the hash of KD.
476 librad_md5_calc(&hash[0], &kd[0], kd_len);
477 memcpy(&kd[0], &hash[0], 16);
480 * Get the binary value of Digest-Response
482 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
484 DEBUG("ERROR: No Digest-Response attribute in the request. Cannot perform digest authentication");
485 return RLM_MODULE_INVALID;
488 lrad_hex2bin(&vp->strvalue[0], &hash[0], vp->length >> 1);
493 for (i = 0; i < 16; i++) {
494 printf("%02x", kd[i]);
499 for (i = 0; i < 16; i++) {
500 printf("%02x", hash[i]);
507 * And finally, compare the digest in the packet with KD.
509 if (memcmp(&kd[0], &hash[0], 16) == 0) {
510 return RLM_MODULE_OK;
513 DEBUG("rlm_digest: FAILED authentication");
514 return RLM_MODULE_REJECT;
518 * The module name should be the only globally exported symbol.
519 * That is, everything else should be 'static'.
521 * If the module needs to temporarily modify it's instantiation
522 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
523 * The server will then take care of ensuring that the module
524 * is single-threaded.
526 module_t rlm_digest = {
529 NULL, /* initialization */
530 NULL, /* instantiation */
532 digest_authenticate, /* authentication */
533 digest_authorize, /* authorization */
534 NULL, /* preaccounting */
535 NULL, /* accounting */
536 NULL, /* checksimul */
537 NULL, /* pre-proxy */
538 NULL, /* post-proxy */