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,2006 The FreeRADIUS server project
21 * Copyright 2002 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
30 static int digest_authorize(void *instance, REQUEST *request)
34 /* quiet the compiler */
38 * We need both of these attributes to do the authentication.
40 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
42 return RLM_MODULE_NOOP;
46 * Check the sanity of the attribute.
48 if (vp->length != 32) {
49 DEBUG("ERROR: Received invalid Digest-Response attribute (length %d should be 32)", vp->length);
50 return RLM_MODULE_INVALID;
56 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
58 DEBUG("ERROR: Received Digest-Response without Digest-Attributes");
59 return RLM_MODULE_INVALID;
63 * Everything's OK, add a digest authentication type.
65 if (pairfind(request->config_items, PW_AUTHTYPE) == NULL) {
66 DEBUG("rlm_digest: Adding Auth-Type = DIGEST");
67 pairadd(&request->config_items,
68 pairmake("Auth-Type", "DIGEST", T_OP_EQ));
75 * Perform all of the wondrous variants of digest authentication.
77 static int digest_authenticate(void *instance, REQUEST *request)
80 size_t a1_len, a2_len, kd_len;
81 uint8_t a1[(MAX_STRING_LEN + 1) * 5]; /* can be 5 attributes */
82 uint8_t a2[(MAX_STRING_LEN + 1) * 3]; /* can be 3 attributes */
83 uint8_t kd[(MAX_STRING_LEN + 1) * 5];
84 uint8_t hash[16]; /* MD5 output */
85 VALUE_PAIR *vp, *passwd, *algo;
86 VALUE_PAIR *qop, *nonce;
88 instance = instance; /* -Wunused */
91 * We require access to the plain-text password.
93 passwd = pairfind(request->config_items, PW_DIGEST_HA1);
95 if (passwd->length != 32) {
96 radlog(L_AUTH, "rlm_digest: Digest-HA1 has invalid length, authentication failed.");
97 return RLM_MODULE_INVALID;
100 passwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
103 radlog(L_AUTH, "rlm_digest: Cleartext-Password or Digest-HA1 is required for authentication.");
104 return RLM_MODULE_INVALID;
108 * We need these, too.
110 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
112 DEBUG("ERROR: You set 'Auth-Type = Digest' for a request that did not contain any digest attributes!");
113 return RLM_MODULE_INVALID;
117 * Loop through the Digest-Attributes, sanity checking them.
119 DEBUG(" rlm_digest: Converting Digest-Attributes to something sane...");
121 int length = vp->length;
123 uint8_t *p = &vp->vp_octets[0];
127 * Until this stupidly encoded attribure is exhausted.
131 * The attribute type must be valid
133 if ((p[0] == 0) || (p[0] > 10)) {
134 DEBUG("ERROR: Received Digest-Attributes with invalid sub-attribute %d", p[0]);
135 return RLM_MODULE_INVALID;
138 attrlen = p[1]; /* stupid VSA format */
144 DEBUG("ERROR: Received Digest-Attributes with short sub-attribute %d, of length %d", p[0], attrlen);
145 return RLM_MODULE_INVALID;
151 if (attrlen > length) {
152 DEBUG("ERROR: Received Digest-Attributes with long sub-attribute %d, of length %d", p[0], attrlen);
153 return RLM_MODULE_INVALID;
157 * Create a new attribute, broken out of
158 * the stupid sub-attribute crap.
160 * Didn't they know that VSA's exist?
162 sub = radius_paircreate(request, &request->packet->vps,
163 PW_DIGEST_REALM - 1 + p[0],
165 memcpy(&sub->vp_octets[0], &p[2], attrlen - 2);
166 sub->vp_octets[attrlen - 2] = '\0';
167 sub->length = attrlen - 2;
171 vp_print(stdout, sub);
176 * FIXME: Check for the existence
177 * of the necessary attributes!
182 } /* loop over this one attribute */
185 * Find the next one, if it exists.
187 vp = pairfind(vp->next, PW_DIGEST_ATTRIBUTES);
191 * We require access to the Digest-Nonce-Value
193 nonce = pairfind(request->packet->vps, PW_DIGEST_NONCE);
195 DEBUG("ERROR: No Digest-Nonce: Cannot perform Digest authentication");
196 return RLM_MODULE_INVALID;
200 * A1 = Digest-User-Name ":" Realm ":" Password
202 vp = pairfind(request->packet->vps, PW_DIGEST_USER_NAME);
204 DEBUG("ERROR: No Digest-User-Name: Cannot perform Digest authentication");
205 return RLM_MODULE_INVALID;
207 memcpy(&a1[0], &vp->vp_octets[0], vp->length);
213 vp = pairfind(request->packet->vps, PW_DIGEST_REALM);
215 DEBUG("ERROR: No Digest-Realm: Cannot perform Digest authentication");
216 return RLM_MODULE_INVALID;
218 memcpy(&a1[a1_len], &vp->vp_octets[0], vp->length);
219 a1_len += vp->length;
224 if (passwd->attribute == PW_CLEARTEXT_PASSWORD) {
225 memcpy(&a1[a1_len], &passwd->vp_octets[0], passwd->length);
226 a1_len += passwd->length;
228 DEBUG2("A1 = %s", a1);
231 DEBUG2("A1 = %s (using Digest-HA1)", a1);
236 * See which variant we calculate.
237 * Assume MD5 if no Digest-Algorithm attribute received
239 algo = pairfind(request->packet->vps, PW_DIGEST_ALGORITHM);
240 if ((algo == NULL) ||
241 (strcasecmp(algo->vp_strvalue, "MD5") == 0)) {
243 * Set A1 to Digest-HA1 if no User-Password found
245 if (passwd->attribute == PW_DIGEST_HA1) {
246 if (lrad_hex2bin(passwd->vp_strvalue, &a1[0], 16) != 16) {
247 DEBUG2("rlm_digest: Invalid text in Digest-HA1");
248 return RLM_MODULE_INVALID;
252 } else if (strcasecmp(algo->vp_strvalue, "MD5-sess") == 0) {
254 * K1 = H(A1) : Digest-Nonce ... : H(A2)
256 * If we find Digest-HA1, we assume it contains
259 if (passwd->attribute == PW_CLEARTEXT_PASSWORD) {
260 librad_md5_calc(hash, &a1[0], a1_len);
261 lrad_bin2hex(hash, (char *) &a1[0], 16);
262 } else { /* MUST be Digest-HA1 */
263 memcpy(&a1[0], passwd->vp_strvalue, 32);
271 * Tack on the Digest-Nonce. Length must be even
273 if ((nonce->length & 1) != 0) {
274 DEBUG("ERROR: Received Digest-Nonce hex string with invalid length: Cannot perform Digest authentication");
275 return RLM_MODULE_INVALID;
277 memcpy(&a1[a1_len], &nonce->vp_octets[0], nonce->length);
278 a1_len += nonce->length;
283 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
285 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
286 return RLM_MODULE_INVALID;
290 * Digest-CNonce length must be even
292 if ((vp->length & 1) != 0) {
293 DEBUG("ERROR: Received Digest-CNonce hex string with invalid length: Cannot perform Digest authentication");
294 return RLM_MODULE_INVALID;
296 memcpy(&a1[a1_len], &vp->vp_octets[0], vp->length);
297 a1_len += vp->length;
299 } else if ((algo != NULL) &&
300 (strcasecmp(algo->vp_strvalue, "MD5") != 0)) {
302 * We check for "MD5-sess" and "MD5".
303 * Anything else is an error.
305 DEBUG("ERROR: Unknown Digest-Algorithm \"%s\": Cannot perform Digest authentication", vp->vp_strvalue);
306 return RLM_MODULE_INVALID;
310 * A2 = Digest-Method ":" Digest-URI
312 vp = pairfind(request->packet->vps, PW_DIGEST_METHOD);
314 DEBUG("ERROR: No Digest-Method: Cannot perform Digest authentication");
315 return RLM_MODULE_INVALID;
317 memcpy(&a2[0], &vp->vp_octets[0], vp->length);
323 vp = pairfind(request->packet->vps, PW_DIGEST_URI);
325 DEBUG("ERROR: No Digest-URI: Cannot perform Digest authentication");
326 return RLM_MODULE_INVALID;
328 memcpy(&a2[a2_len], &vp->vp_octets[0], vp->length);
329 a2_len += vp->length;
332 * QOP is "auth-int", tack on ": Digest-Body-Digest"
334 qop = pairfind(request->packet->vps, PW_DIGEST_QOP);
336 (strcasecmp(qop->vp_strvalue, "auth-int") == 0)) {
340 * Add in Digest-Body-Digest
346 * Must be a hex representation of an MD5 digest.
348 body = pairfind(request->packet->vps, PW_DIGEST_BODY_DIGEST);
350 DEBUG("ERROR: No Digest-Body-Digest: Cannot perform Digest authentication");
351 return RLM_MODULE_INVALID;
354 if ((a2_len + body->length) > sizeof(a2)) {
355 DEBUG("ERROR: Digest-Body-Digest is too long");
356 return RLM_MODULE_INVALID;
359 memcpy(a2 + a2_len, body->vp_octets, body->length);
360 a2_len += body->length;
362 } else if ((qop != NULL) &&
363 (strcasecmp(qop->vp_strvalue, "auth") != 0)) {
364 DEBUG("ERROR: Unknown Digest-QOP \"%s\": Cannot perform Digest authentication", qop->vp_strvalue);
365 return RLM_MODULE_INVALID;
369 DEBUG2("A2 = %s", a2);
372 * KD = H(A1) : Digest-Nonce ... : H(A2).
373 * Compute MD5 if Digest-Algorithm == "MD5-Sess",
374 * or if we found a User-Password.
376 if (((algo != NULL) &&
377 (strcasecmp(algo->vp_strvalue, "MD5-Sess") == 0)) ||
378 (passwd->attribute == PW_CLEARTEXT_PASSWORD)) {
380 librad_md5_calc(&hash[0], &a1[0], a1_len);
382 memcpy(&hash[0], &a1[0], a1_len);
384 lrad_bin2hex(hash, (char *) kd, sizeof(hash));
389 for (i = 0; i < 16; i++) {
390 printf("%02x", hash[i]);
400 memcpy(&kd[kd_len], nonce->vp_octets, nonce->length);
401 kd_len += nonce->length;
404 * No QOP defined. Do RFC 2069 compatibility.
411 } else { /* Digest-QOP MUST be "auth" or "auth-int" */
413 * Tack on ":" Digest-Nonce-Count ":" Digest-CNonce
419 vp = pairfind(request->packet->vps, PW_DIGEST_NONCE_COUNT);
421 DEBUG("ERROR: No Digest-Nonce-Count: Cannot perform Digest authentication");
422 return RLM_MODULE_INVALID;
424 memcpy(&kd[kd_len], &vp->vp_octets[0], vp->length);
425 kd_len += vp->length;
430 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
432 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
433 return RLM_MODULE_INVALID;
435 memcpy(&kd[kd_len], &vp->vp_octets[0], vp->length);
436 kd_len += vp->length;
441 memcpy(&kd[kd_len], &qop->vp_octets[0], qop->length);
442 kd_len += qop->length;
451 librad_md5_calc(&hash[0], &a2[0], a2_len);
453 lrad_bin2hex(hash, (char *) kd + kd_len, sizeof(hash));
458 for (i = 0; i < 16; i++) {
459 printf("%02x", hash[i]);
468 DEBUG2("KD = %s\n", &kd[0]);
471 * Take the hash of KD.
473 librad_md5_calc(&hash[0], &kd[0], kd_len);
474 memcpy(&kd[0], &hash[0], 16);
477 * Get the binary value of Digest-Response
479 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
481 DEBUG("ERROR: No Digest-Response attribute in the request. Cannot perform digest authentication");
482 return RLM_MODULE_INVALID;
485 if (lrad_hex2bin(&vp->vp_strvalue[0], &hash[0], vp->length >> 1) != (vp->length >> 1)) {
486 DEBUG2("rlm_digest: Invalid text in Digest-Response");
487 return RLM_MODULE_INVALID;
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 = {
530 NULL, /* instantiation */
533 digest_authenticate, /* authentication */
534 digest_authorize, /* authorization */
535 NULL, /* preaccounting */
536 NULL, /* accounting */
537 NULL, /* checksimul */
538 NULL, /* pre-proxy */
539 NULL, /* post-proxy */