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_DIGEST_HA1);
101 if (passwd->length != 32) {
102 radlog(L_AUTH, "rlm_digest: Digest-HA1 has invalid length, authentication failed.");
103 return RLM_MODULE_INVALID;
106 passwd = pairfind(request->config_items, PW_PASSWORD);
109 radlog(L_AUTH, "rlm_digest: Configuration item \"User-Password\" or Digest-HA1 is required for authentication.");
110 return RLM_MODULE_INVALID;
114 * We need these, too.
116 vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
118 DEBUG("ERROR: You set 'Auth-Type = Digest' for a request that did not contain any digest attributes!");
119 return RLM_MODULE_INVALID;
123 * Loop through the Digest-Attributes, sanity checking them.
125 DEBUG(" rlm_digest: Converting Digest-Attributes to something sane...");
127 int length = vp->length;
129 uint8_t *p = &vp->strvalue[0];
133 * Until this stupidly encoded attribure is exhausted.
137 * The attribute type must be valid
139 if ((p[0] == 0) || (p[0] > 10)) {
140 DEBUG("ERROR: Received Digest-Attributes with invalid sub-attribute %d", p[0]);
141 return RLM_MODULE_INVALID;
144 attrlen = p[1]; /* stupid VSA format */
150 DEBUG("ERROR: Received Digest-Attributes with short sub-attribute %d, of length %d", p[0], attrlen);
151 return RLM_MODULE_INVALID;
157 if (attrlen > length) {
158 DEBUG("ERROR: Received Digest-Attributes with long sub-attribute %d, of length %d", p[0], attrlen);
159 return RLM_MODULE_INVALID;
163 * Create a new attribute, broken out of
164 * the stupid sub-attribute crap.
166 * Didn't they know that VSA's exist?
168 sub = paircreate(PW_DIGEST_REALM - 1 + p[0],
171 return RLM_MODULE_FAIL; /* out of memory */
173 memcpy(&sub->strvalue[0], &p[2], attrlen - 2);
174 sub->strvalue[attrlen - 2] = '\0';
175 sub->length = attrlen - 2;
179 vp_print(stdout, sub);
184 * And add it to the request pairs.
186 pairadd(&request->packet->vps, sub);
189 * FIXME: Check for the existence
190 * of the necessary attributes!
195 } /* loop over this one attribute */
198 * Find the next one, if it exists.
200 vp = pairfind(vp->next, PW_DIGEST_ATTRIBUTES);
204 * We require access to the Digest-Nonce-Value
206 nonce = pairfind(request->packet->vps, PW_DIGEST_NONCE);
208 DEBUG("ERROR: No Digest-Nonce: Cannot perform Digest authentication");
209 return RLM_MODULE_INVALID;
213 * A1 = Digest-User-Name ":" Realm ":" Password
215 vp = pairfind(request->packet->vps, PW_DIGEST_USER_NAME);
217 DEBUG("ERROR: No Digest-User-Name: Cannot perform Digest authentication");
218 return RLM_MODULE_INVALID;
220 memcpy(&a1[0], &vp->strvalue[0], vp->length);
226 vp = pairfind(request->packet->vps, PW_DIGEST_REALM);
228 DEBUG("ERROR: No Digest-Realm: Cannot perform Digest authentication");
229 return RLM_MODULE_INVALID;
231 memcpy(&a1[a1_len], &vp->strvalue[0], vp->length);
232 a1_len += vp->length;
237 if (passwd->attribute == PW_USER_PASSWORD) {
238 memcpy(&a1[a1_len], &passwd->strvalue[0], passwd->length);
239 a1_len += passwd->length;
241 DEBUG2("A1 = %s", a1);
244 DEBUG2("A1 = %s (using Digest-HA1)", a1);
249 * See which variant we calculate.
250 * Assume MD5 if no Digest-Algorithm attribute received
252 algo = pairfind(request->packet->vps, PW_DIGEST_ALGORITHM);
253 if ((algo == NULL) ||
254 (strcasecmp(algo->strvalue, "MD5") == 0)) {
256 * Set A1 to Digest-HA1 if no User-Password found
258 if (passwd->attribute == PW_DIGEST_HA1) {
259 lrad_hex2bin(passwd->strvalue, &a1[0], 16);
262 } else if (strcasecmp(algo->strvalue, "MD5-sess") == 0) {
264 * K1 = H(A1) : Digest-Nonce ... : H(A2)
266 * If we find Digest-HA1, we assume it contains
269 if (passwd->attribute == PW_USER_PASSWORD) {
270 librad_md5_calc(hash, &a1[0], a1_len);
271 lrad_bin2hex(hash, &a1[0], 16);
272 } else { /* MUST be Digest-HA1 */
273 memcpy(&a1[0], passwd->strvalue, 32);
281 * Tack on the Digest-Nonce. Length must be even
283 if ((nonce->length & 1) != 0) {
284 DEBUG("ERROR: Received Digest-Nonce hex string with invalid length: Cannot perform Digest authentication");
285 return RLM_MODULE_INVALID;
287 memcpy(&a1[a1_len], &nonce->strvalue[0], nonce->length);
288 a1_len += nonce->length;
293 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
295 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
296 return RLM_MODULE_INVALID;
300 * Digest-CNonce length must be even
302 if ((vp->length & 1) != 0) {
303 DEBUG("ERROR: Received Digest-CNonce hex string with invalid length: Cannot perform Digest authentication");
304 return RLM_MODULE_INVALID;
306 memcpy(&a1[a1_len], &vp->strvalue[0], vp->length);
307 a1_len += vp->length;
309 } else if ((algo != NULL) &&
310 (strcasecmp(algo->strvalue, "MD5") != 0)) {
312 * We check for "MD5-sess" and "MD5".
313 * Anything else is an error.
315 DEBUG("ERROR: Unknown Digest-Algorithm \"%s\": Cannot perform Digest authentication", vp->strvalue);
316 return RLM_MODULE_INVALID;
320 * A2 = Digest-Method ":" Digest-URI
322 vp = pairfind(request->packet->vps, PW_DIGEST_METHOD);
324 DEBUG("ERROR: No Digest-Method: Cannot perform Digest authentication");
325 return RLM_MODULE_INVALID;
327 memcpy(&a2[0], &vp->strvalue[0], vp->length);
333 vp = pairfind(request->packet->vps, PW_DIGEST_URI);
335 DEBUG("ERROR: No Digest-URI: Cannot perform Digest authentication");
336 return RLM_MODULE_INVALID;
338 memcpy(&a2[a2_len], &vp->strvalue[0], vp->length);
339 a2_len += vp->length;
342 * QOP is "auth-int", tack on ": Digest-Body-Digest"
344 qop = pairfind(request->packet->vps, PW_DIGEST_QOP);
346 (strcasecmp(qop->strvalue, "auth-int") == 0)) {
350 * Add in Digest-Body-Digest
356 * Must be a hex representation of an MD5 digest.
358 body = pairfind(request->packet->vps, PW_DIGEST_BODY_DIGEST);
360 DEBUG("ERROR: No Digest-Body-Digest: Cannot perform Digest authentication");
361 return RLM_MODULE_INVALID;
364 if ((a2_len + body->length) > sizeof(a2)) {
365 DEBUG("ERROR: Digest-Body-Digest is too long");
366 return RLM_MODULE_INVALID;
369 memcpy(a2 + a2_len, body->strvalue, body->length);
370 a2_len += body->length;
372 } else if ((qop != NULL) &&
373 (strcasecmp(qop->strvalue, "auth") != 0)) {
374 DEBUG("ERROR: Unknown Digest-QOP \"%s\": Cannot perform Digest authentication", qop->strvalue);
375 return RLM_MODULE_INVALID;
379 DEBUG2("A2 = %s", a2);
382 * KD = H(A1) : Digest-Nonce ... : H(A2).
383 * Compute MD5 if Digest-Algorithm == "MD5-Sess",
384 * or if we found a User-Password.
386 if (((algo != NULL) &&
387 (strcasecmp(algo->strvalue, "MD5-Sess") == 0)) ||
388 (passwd->attribute == PW_USER_PASSWORD)) {
390 librad_md5_calc(&hash[0], &a1[0], a1_len);
392 memcpy(&hash[0], &a1[0], a1_len);
394 lrad_bin2hex(hash, kd, sizeof(hash));
399 for (i = 0; i < 16; i++) {
400 printf("%02x", hash[i]);
410 memcpy(&kd[kd_len], nonce->strvalue, nonce->length);
411 kd_len += nonce->length;
414 * No QOP defined. Do RFC 2069 compatibility.
421 } else { /* Digest-QOP MUST be "auth" or "auth-int" */
423 * Tack on ":" Digest-Nonce-Count ":" Digest-CNonce
429 vp = pairfind(request->packet->vps, PW_DIGEST_NONCE_COUNT);
431 DEBUG("ERROR: No Digest-Nonce-Count: Cannot perform Digest authentication");
432 return RLM_MODULE_INVALID;
434 memcpy(&kd[kd_len], &vp->strvalue[0], vp->length);
435 kd_len += vp->length;
440 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
442 DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
443 return RLM_MODULE_INVALID;
445 memcpy(&kd[kd_len], &vp->strvalue[0], vp->length);
446 kd_len += vp->length;
451 memcpy(&kd[kd_len], &qop->strvalue[0], qop->length);
452 kd_len += qop->length;
461 librad_md5_calc(&hash[0], &a2[0], a2_len);
463 lrad_bin2hex(hash, kd + kd_len, sizeof(hash));
468 for (i = 0; i < 16; i++) {
469 printf("%02x", hash[i]);
478 DEBUG2("KD = %s\n", &kd[0]);
481 * Take the hash of KD.
483 librad_md5_calc(&hash[0], &kd[0], kd_len);
484 memcpy(&kd[0], &hash[0], 16);
487 * Get the binary value of Digest-Response
489 vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
491 DEBUG("ERROR: No Digest-Response attribute in the request. Cannot perform digest authentication");
492 return RLM_MODULE_INVALID;
495 lrad_hex2bin(&vp->strvalue[0], &hash[0], vp->length >> 1);
500 for (i = 0; i < 16; i++) {
501 printf("%02x", kd[i]);
506 for (i = 0; i < 16; i++) {
507 printf("%02x", hash[i]);
514 * And finally, compare the digest in the packet with KD.
516 if (memcmp(&kd[0], &hash[0], 16) == 0) {
517 return RLM_MODULE_OK;
520 DEBUG("rlm_digest: FAILED authentication");
521 return RLM_MODULE_REJECT;
525 * The module name should be the only globally exported symbol.
526 * That is, everything else should be 'static'.
528 * If the module needs to temporarily modify it's instantiation
529 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
530 * The server will then take care of ensuring that the module
531 * is single-threaded.
533 module_t rlm_digest = {
536 NULL, /* initialization */
537 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 */