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>
33 #include "rad_assert.h"
35 static const char rcsid[] = "$Id$";
37 static int digest_authorize(void *instance, REQUEST *request)
41 /* quiet the compiler */
45 * We need both of these attributes to do the authentication.
47 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
49 return RLM_MODULE_NOOP;
53 * Check the sanity of the attribute.
55 if (vp->length != 32) {
56 DEBUG("ERROR: Received invalid Digest-Response attribute (length %d should be 32)", vp->length);
57 return RLM_MODULE_INVALID;
63 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
65 DEBUG("ERROR: Received Digest-Response without Digest-Attributes");
66 return RLM_MODULE_INVALID;
70 * Loop through the Digest-Attributes, sanity checking them.
72 DEBUG(" rlm_digest: Converting Digest-Attributes to something sane...");
74 int length = vp->length;
76 uint8_t *p = &vp->strvalue[0];
80 * Until this stupidly encoded attribure is exhausted.
84 * The attribute type must be valid
86 if ((p[0] == 0) || (p[0] > 10)) {
87 DEBUG("ERROR: Received Digest-Attributes with invalid sub-attribute %d", p[0]);
88 return RLM_MODULE_INVALID;
91 attrlen = p[1]; /* stupid VSA format */
97 DEBUG("ERROR: Received Digest-Attributes with short sub-attribute %d, of length %d", p[0], attrlen);
98 return RLM_MODULE_INVALID;
104 if (attrlen > length) {
105 DEBUG("ERROR: Received Digest-Attributes with long sub-attribute %d, of length %d", p[0], attrlen);
106 return RLM_MODULE_INVALID;
110 * Create a new attribute, broken out of
111 * the stupid sub-attribute crap.
113 * Didn't they know that VSA's exist?
115 sub = paircreate(PW_DIGEST_REALM - 1 + p[0],
118 return RLM_MODULE_FAIL; /* out of memory */
120 memcpy(&sub->strvalue[0], &p[2], attrlen - 2);
121 sub->strvalue[attrlen - 2] = '\0';
122 sub->length = attrlen - 2;
126 vp_print(stdout, sub);
131 * And add it to the request pairs.
133 pairadd(&request->packet->vps, sub);
136 * FIXME: Check for the existence
137 * of the necessary attributes!
142 } /* loop over this one attribute */
145 * Find the next one, if it exists.
147 vp = pairfind(vp->next, PW_DIGEST_ATTRIBUTES);
151 * Everything's OK, add a digest authentication type.
153 if (pairfind(request->config_items, PW_AUTHTYPE) == NULL) {
154 DEBUG("rlm_digest: Adding Auth-Type = DIGEST");
155 pairadd(&request->config_items,
156 pairmake("Auth-Type", "DIGEST", T_OP_EQ));
159 return RLM_MODULE_OK;
163 * Convert a string in hex to one in binary
165 static void hex2bin(uint8_t *out, const uint8_t *in)
170 sscanf(in, "%02x", &tmp);
178 * Perform all of the wondrous variants of digest authentication.
180 static int digest_authenticate(void *instance, REQUEST *request)
183 int a1_len, a2_len, kd_len;
184 uint8_t a1[(MAX_STRING_LEN + 1) * 5]; /* can be 5 attributes */
185 uint8_t a2[(MAX_STRING_LEN + 1) * 3]; /* can be 3 attributes */
186 uint8_t kd[(MAX_STRING_LEN + 1) * 5];
187 uint8_t hash[16]; /* MD5 output */
189 VALUE_PAIR *qop, *nonce;
191 instance = instance; /* -Wunused */
194 * We require access to the plain-text password.
196 vp = pairfind(request->config_items, PW_PASSWORD);
198 radlog(L_AUTH, "rlm_digest: Configuration item \"User-Password\" is required for authentication.");
199 return RLM_MODULE_INVALID;
203 * We require access to the Digest-Nonce-Value
205 nonce = pairfind(request->packet->vps, PW_DIGEST_NONCE);
207 DEBUG("ERROR: No Digest-Nonce: Cannot perform Digest authentication");
208 return RLM_MODULE_INVALID;
212 * A1 = Digest-User-Name ":" Realm ":" Password
214 vp = pairfind(request->packet->vps, PW_DIGEST_USER_NAME);
216 DEBUG("ERROR: No Digest-User-Name: Cannot perform Digest authentication");
217 return RLM_MODULE_INVALID;
219 memcpy(&a1[0], &vp->strvalue[0], vp->length);
225 vp = pairfind(request->packet->vps, PW_DIGEST_REALM);
227 DEBUG("ERROR: No Digest-Realm: Cannot perform Digest authentication");
228 return RLM_MODULE_INVALID;
230 memcpy(&a1[a1_len], &vp->strvalue[0], vp->length);
231 a1_len += vp->length;
236 vp = pairfind(request->config_items, PW_PASSWORD);
238 DEBUG("ERROR: No User-Password: Cannot perform Digest authentication");
239 return RLM_MODULE_INVALID;
241 memcpy(&a1[a1_len], &vp->strvalue[0], vp->length);
242 a1_len += vp->length;
245 DEBUG2("A1 = %s", a1);
248 * See which variant we calculate.
250 vp = pairfind(request->packet->vps, PW_DIGEST_ALGORITHM);
252 (strcasecmp(vp->strvalue, "MD5-sess") == 0)) {
253 librad_md5_calc(hash, &a1[0], a1_len);
254 memcpy(&a1[0], hash, 16);
261 * Tack on the Digest-Nonce
263 hex2bin(&a1[a1_len], &nonce->strvalue[0]);
264 a1_len += (nonce->length >> 1); /* FIXME: CHECK LENGTH */
269 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
271 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
272 return RLM_MODULE_INVALID;
275 hex2bin(&a1[a1_len], &vp->strvalue[0]);
276 a1_len += (vp->length >> 1); /* FIXME: CHECK LENGTH */
278 } else if ((vp != NULL) &&
279 (strcasecmp(vp->strvalue, "MD5") != 0)) {
281 * We check for "MD5-sess" and "MD5".
282 * Anything else is an error.
284 DEBUG("ERROR: Unknown Digest-Algorithm \"%s\": Cannot perform Digest authentication", vp->strvalue);
285 return RLM_MODULE_INVALID;
289 * A2 = Digest-Method ":" Digest-URI
291 vp = pairfind(request->packet->vps, PW_DIGEST_METHOD);
293 DEBUG("ERROR: No Digest-Method: Cannot perform Digest authentication");
294 return RLM_MODULE_INVALID;
296 memcpy(&a2[0], &vp->strvalue[0], vp->length);
302 vp = pairfind(request->packet->vps, PW_DIGEST_URI);
304 DEBUG("ERROR: No Digest-URI: Cannot perform Digest authentication");
305 return RLM_MODULE_INVALID;
307 memcpy(&a2[a2_len], &vp->strvalue[0], vp->length);
308 a2_len += vp->length;
311 * QOP is "auth-int", tack on ": Digest-Body-Digest"
313 qop = pairfind(request->packet->vps, PW_DIGEST_QOP);
315 (strcasecmp(qop->strvalue, "auth-int") == 0)) {
319 * Add in Digest-Body-Digest
325 * Must be a hex representation of an MD5 digest.
327 body = pairfind(request->packet->vps, PW_DIGEST_BODY_DIGEST);
329 DEBUG("ERROR: No Digest-Body-Digest: Cannot perform Digest authentication");
330 return RLM_MODULE_INVALID;
333 rad_assert(body->length == 32); /* FIXME: check in 'auth' */
334 hex2bin(&a2[a2_len], &body->strvalue[0]);
335 a2_len += (body->length >> 1);
337 } else if ((qop != NULL) &&
338 (strcasecmp(qop->strvalue, "auth") != 0)) {
339 DEBUG("ERROR: Unknown Digest-QOP \"%s\": Cannot perform Digest authentication", qop->strvalue);
340 return RLM_MODULE_INVALID;
344 DEBUG2("A2 = %s", a2);
347 * KD = H(A1) : Digest-Nonce ... : H(A2)
349 librad_md5_calc(&hash[0], &a1[0], a1_len);
351 for (i = 0; i < 16; i++) {
352 sprintf(&kd[i * 2], "%02x", hash[i]);
358 for (i = 0; i < 16; i++) {
359 printf("%02x", hash[i]);
369 memcpy(&kd[kd_len], nonce->strvalue, nonce->length);
370 kd_len += nonce->length;
373 * No QOP defined. Do RFC 2069 compatibility.
380 } else { /* Digest-QOP MUST be "auth" or "auth-int" */
382 * Tack on ":" Digest-Nonce-Count ":" Digest-CNonce
388 vp = pairfind(request->packet->vps, PW_DIGEST_NONCE_COUNT);
390 DEBUG("ERROR: No Digest-Nonce-Count: Cannot perform Digest authentication");
391 return RLM_MODULE_INVALID;
393 memcpy(&kd[kd_len], &vp->strvalue[0], vp->length);
394 kd_len += vp->length;
399 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
401 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
402 return RLM_MODULE_INVALID;
404 memcpy(&kd[kd_len], &vp->strvalue[0], vp->length);
405 kd_len += vp->length;
410 memcpy(&kd[kd_len], &qop->strvalue[0], qop->length);
411 kd_len += qop->length;
420 librad_md5_calc(&hash[0], &a2[0], a2_len);
422 for (i = 0; i < 16; i++) {
423 sprintf(&kd[kd_len + (i * 2)], "%02x", hash[i]);
429 for (i = 0; i < 16; i++) {
430 printf("%02x", hash[i]);
439 DEBUG2("KD = %s\n", &kd[0]);
442 * Take the hash of KD.
444 librad_md5_calc(&hash[0], &kd[0], kd_len);
445 memcpy(&kd[0], &hash[0], 16);
448 * Get the binary value of Digest-Response
450 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
451 rad_assert(vp != NULL);
453 hex2bin(&hash[0], &vp->strvalue[0]);
458 for (i = 0; i < 16; i++) {
459 printf("%02x", kd[i]);
464 for (i = 0; i < 16; i++) {
465 printf("%02x", hash[i]);
472 * And finally, compare the digest in the packet with KD.
474 if (memcmp(&kd[0], &hash[0], 16) == 0) {
475 return RLM_MODULE_OK;
478 DEBUG("rlm_digest: FAILED authentication");
479 return RLM_MODULE_REJECT;
483 * The module name should be the only globally exported symbol.
484 * That is, everything else should be 'static'.
486 * If the module needs to temporarily modify it's instantiation
487 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
488 * The server will then take care of ensuring that the module
489 * is single-threaded.
491 module_t rlm_digest = {
495 NULL, /* instantiation */
498 digest_authenticate, /* authentication */
499 digest_authorize, /* authorization */
500 NULL, /* preaccounting */
501 NULL, /* accounting */
502 NULL, /* checksimul */
503 NULL, /* pre-proxy */
504 NULL, /* post-proxy */