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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2002 The FreeRADIUS server project
21 * Copyright 2002 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/autoconf.h>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
33 static const char rcsid[] = "$Id$";
35 static int digest_authorize(void *instance, REQUEST *request)
39 /* quiet the compiler */
43 * We need both of these attributes to do the authentication.
45 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
47 return RLM_MODULE_NOOP;
51 * Check the sanity of the attribute.
53 if (vp->length != 32) {
54 DEBUG("ERROR: Received invalid Digest-Response attribute (length %d should be 32)", vp->length);
55 return RLM_MODULE_INVALID;
61 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
63 DEBUG("ERROR: Received Digest-Response without Digest-Attributes");
64 return RLM_MODULE_INVALID;
68 * Everything's OK, add a digest authentication type.
70 if (pairfind(request->config_items, PW_AUTHTYPE) == NULL) {
71 DEBUG("rlm_digest: Adding Auth-Type = DIGEST");
72 pairadd(&request->config_items,
73 pairmake("Auth-Type", "DIGEST", T_OP_EQ));
80 * Perform all of the wondrous variants of digest authentication.
82 static int digest_authenticate(void *instance, REQUEST *request)
85 size_t a1_len, a2_len, kd_len;
86 uint8_t a1[(MAX_STRING_LEN + 1) * 5]; /* can be 5 attributes */
87 uint8_t a2[(MAX_STRING_LEN + 1) * 3]; /* can be 3 attributes */
88 uint8_t kd[(MAX_STRING_LEN + 1) * 5];
89 uint8_t hash[16]; /* MD5 output */
90 VALUE_PAIR *vp, *passwd, *algo;
91 VALUE_PAIR *qop, *nonce;
93 instance = instance; /* -Wunused */
96 * We require access to the plain-text password.
98 passwd = pairfind(request->config_items, PW_DIGEST_HA1);
100 if (passwd->length != 32) {
101 radlog(L_AUTH, "rlm_digest: Digest-HA1 has invalid length, authentication failed.");
102 return RLM_MODULE_INVALID;
105 passwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
108 radlog(L_AUTH, "rlm_digest: Cleartext-Password or Digest-HA1 is required for authentication.");
109 return RLM_MODULE_INVALID;
113 * We need these, too.
115 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
117 DEBUG("ERROR: You set 'Auth-Type = Digest' for a request that did not contain any digest attributes!");
118 return RLM_MODULE_INVALID;
122 * Loop through the Digest-Attributes, sanity checking them.
124 DEBUG(" rlm_digest: Converting Digest-Attributes to something sane...");
126 int length = vp->length;
128 uint8_t *p = &vp->vp_octets[0];
132 * Until this stupidly encoded attribure is exhausted.
136 * The attribute type must be valid
138 if ((p[0] == 0) || (p[0] > 10)) {
139 DEBUG("ERROR: Received Digest-Attributes with invalid sub-attribute %d", p[0]);
140 return RLM_MODULE_INVALID;
143 attrlen = p[1]; /* stupid VSA format */
149 DEBUG("ERROR: Received Digest-Attributes with short sub-attribute %d, of length %d", p[0], attrlen);
150 return RLM_MODULE_INVALID;
156 if (attrlen > length) {
157 DEBUG("ERROR: Received Digest-Attributes with long sub-attribute %d, of length %d", p[0], attrlen);
158 return RLM_MODULE_INVALID;
162 * Create a new attribute, broken out of
163 * the stupid sub-attribute crap.
165 * Didn't they know that VSA's exist?
167 sub = paircreate(PW_DIGEST_REALM - 1 + p[0],
170 return RLM_MODULE_FAIL; /* out of memory */
172 memcpy(&sub->vp_octets[0], &p[2], attrlen - 2);
173 sub->vp_octets[attrlen - 2] = '\0';
174 sub->length = attrlen - 2;
178 vp_print(stdout, sub);
183 * And add it to the request pairs.
185 pairadd(&request->packet->vps, sub);
188 * FIXME: Check for the existence
189 * of the necessary attributes!
194 } /* loop over this one attribute */
197 * Find the next one, if it exists.
199 vp = pairfind(vp->next, PW_DIGEST_ATTRIBUTES);
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->vp_octets[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->vp_octets[0], vp->length);
231 a1_len += vp->length;
236 if (passwd->attribute == PW_CLEARTEXT_PASSWORD) {
237 memcpy(&a1[a1_len], &passwd->vp_octets[0], passwd->length);
238 a1_len += passwd->length;
240 DEBUG2("A1 = %s", a1);
243 DEBUG2("A1 = %s (using Digest-HA1)", a1);
248 * See which variant we calculate.
249 * Assume MD5 if no Digest-Algorithm attribute received
251 algo = pairfind(request->packet->vps, PW_DIGEST_ALGORITHM);
252 if ((algo == NULL) ||
253 (strcasecmp(algo->vp_strvalue, "MD5") == 0)) {
255 * Set A1 to Digest-HA1 if no User-Password found
257 if (passwd->attribute == PW_DIGEST_HA1) {
258 lrad_hex2bin(passwd->vp_strvalue, &a1[0], 16);
261 } else if (strcasecmp(algo->vp_strvalue, "MD5-sess") == 0) {
263 * K1 = H(A1) : Digest-Nonce ... : H(A2)
265 * If we find Digest-HA1, we assume it contains
268 if (passwd->attribute == PW_CLEARTEXT_PASSWORD) {
269 librad_md5_calc(hash, &a1[0], a1_len);
270 lrad_bin2hex(hash, &a1[0], 16);
271 } else { /* MUST be Digest-HA1 */
272 memcpy(&a1[0], passwd->vp_strvalue, 32);
280 * Tack on the Digest-Nonce. Length must be even
282 if ((nonce->length & 1) != 0) {
283 DEBUG("ERROR: Received Digest-Nonce hex string with invalid length: Cannot perform Digest authentication");
284 return RLM_MODULE_INVALID;
286 memcpy(&a1[a1_len], &nonce->vp_octets[0], nonce->length);
287 a1_len += nonce->length;
292 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
294 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
295 return RLM_MODULE_INVALID;
299 * Digest-CNonce length must be even
301 if ((vp->length & 1) != 0) {
302 DEBUG("ERROR: Received Digest-CNonce hex string with invalid length: Cannot perform Digest authentication");
303 return RLM_MODULE_INVALID;
305 memcpy(&a1[a1_len], &vp->vp_octets[0], vp->length);
306 a1_len += vp->length;
308 } else if ((algo != NULL) &&
309 (strcasecmp(algo->vp_strvalue, "MD5") != 0)) {
311 * We check for "MD5-sess" and "MD5".
312 * Anything else is an error.
314 DEBUG("ERROR: Unknown Digest-Algorithm \"%s\": Cannot perform Digest authentication", vp->vp_strvalue);
315 return RLM_MODULE_INVALID;
319 * A2 = Digest-Method ":" Digest-URI
321 vp = pairfind(request->packet->vps, PW_DIGEST_METHOD);
323 DEBUG("ERROR: No Digest-Method: Cannot perform Digest authentication");
324 return RLM_MODULE_INVALID;
326 memcpy(&a2[0], &vp->vp_octets[0], vp->length);
332 vp = pairfind(request->packet->vps, PW_DIGEST_URI);
334 DEBUG("ERROR: No Digest-URI: Cannot perform Digest authentication");
335 return RLM_MODULE_INVALID;
337 memcpy(&a2[a2_len], &vp->vp_octets[0], vp->length);
338 a2_len += vp->length;
341 * QOP is "auth-int", tack on ": Digest-Body-Digest"
343 qop = pairfind(request->packet->vps, PW_DIGEST_QOP);
345 (strcasecmp(qop->vp_strvalue, "auth-int") == 0)) {
349 * Add in Digest-Body-Digest
355 * Must be a hex representation of an MD5 digest.
357 body = pairfind(request->packet->vps, PW_DIGEST_BODY_DIGEST);
359 DEBUG("ERROR: No Digest-Body-Digest: Cannot perform Digest authentication");
360 return RLM_MODULE_INVALID;
363 if ((a2_len + body->length) > sizeof(a2)) {
364 DEBUG("ERROR: Digest-Body-Digest is too long");
365 return RLM_MODULE_INVALID;
368 memcpy(a2 + a2_len, body->vp_octets, body->length);
369 a2_len += body->length;
371 } else if ((qop != NULL) &&
372 (strcasecmp(qop->vp_strvalue, "auth") != 0)) {
373 DEBUG("ERROR: Unknown Digest-QOP \"%s\": Cannot perform Digest authentication", qop->vp_strvalue);
374 return RLM_MODULE_INVALID;
378 DEBUG2("A2 = %s", a2);
381 * KD = H(A1) : Digest-Nonce ... : H(A2).
382 * Compute MD5 if Digest-Algorithm == "MD5-Sess",
383 * or if we found a User-Password.
385 if (((algo != NULL) &&
386 (strcasecmp(algo->vp_strvalue, "MD5-Sess") == 0)) ||
387 (passwd->attribute == PW_CLEARTEXT_PASSWORD)) {
389 librad_md5_calc(&hash[0], &a1[0], a1_len);
391 memcpy(&hash[0], &a1[0], a1_len);
393 lrad_bin2hex(hash, kd, sizeof(hash));
398 for (i = 0; i < 16; i++) {
399 printf("%02x", hash[i]);
409 memcpy(&kd[kd_len], nonce->vp_octets, nonce->length);
410 kd_len += nonce->length;
413 * No QOP defined. Do RFC 2069 compatibility.
420 } else { /* Digest-QOP MUST be "auth" or "auth-int" */
422 * Tack on ":" Digest-Nonce-Count ":" Digest-CNonce
428 vp = pairfind(request->packet->vps, PW_DIGEST_NONCE_COUNT);
430 DEBUG("ERROR: No Digest-Nonce-Count: Cannot perform Digest authentication");
431 return RLM_MODULE_INVALID;
433 memcpy(&kd[kd_len], &vp->vp_octets[0], vp->length);
434 kd_len += vp->length;
439 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
441 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
442 return RLM_MODULE_INVALID;
444 memcpy(&kd[kd_len], &vp->vp_octets[0], vp->length);
445 kd_len += vp->length;
450 memcpy(&kd[kd_len], &qop->vp_octets[0], qop->length);
451 kd_len += qop->length;
460 librad_md5_calc(&hash[0], &a2[0], a2_len);
462 lrad_bin2hex(hash, kd + kd_len, sizeof(hash));
467 for (i = 0; i < 16; i++) {
468 printf("%02x", hash[i]);
477 DEBUG2("KD = %s\n", &kd[0]);
480 * Take the hash of KD.
482 librad_md5_calc(&hash[0], &kd[0], kd_len);
483 memcpy(&kd[0], &hash[0], 16);
486 * Get the binary value of Digest-Response
488 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
490 DEBUG("ERROR: No Digest-Response attribute in the request. Cannot perform digest authentication");
491 return RLM_MODULE_INVALID;
494 lrad_hex2bin(&vp->vp_octets[0], &hash[0], vp->length >> 1);
499 for (i = 0; i < 16; i++) {
500 printf("%02x", kd[i]);
505 for (i = 0; i < 16; i++) {
506 printf("%02x", hash[i]);
513 * And finally, compare the digest in the packet with KD.
515 if (memcmp(&kd[0], &hash[0], 16) == 0) {
516 return RLM_MODULE_OK;
519 DEBUG("rlm_digest: FAILED authentication");
520 return RLM_MODULE_REJECT;
524 * The module name should be the only globally exported symbol.
525 * That is, everything else should be 'static'.
527 * If the module needs to temporarily modify it's instantiation
528 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
529 * The server will then take care of ensuring that the module
530 * is single-threaded.
532 module_t rlm_digest = {
536 NULL, /* instantiation */
539 digest_authenticate, /* authentication */
540 digest_authorize, /* authorization */
541 NULL, /* preaccounting */
542 NULL, /* accounting */
543 NULL, /* checksimul */
544 NULL, /* pre-proxy */
545 NULL, /* post-proxy */