./scripts/min-includes +n <files listed here>
[freeradius.git] / src / modules / rlm_digest / rlm_digest.c
1 /*
2  * rlm_chap.c
3  *
4  * Version:  $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2002  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/autoconf.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
32
33 static const char rcsid[] = "$Id$";
34
35 static int digest_authorize(void *instance, REQUEST *request)
36 {
37         VALUE_PAIR *vp;
38
39         /* quiet the compiler */
40         instance = instance;
41
42         /*
43          *      We need both of these attributes to do the authentication.
44          */
45         vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
46         if (vp == NULL) {
47                 return RLM_MODULE_NOOP;
48         }
49
50         /*
51          *      Check the sanity of the attribute.
52          */
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;
56         }
57
58         /*
59          *      We need these, too.
60          */
61         vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
62         if (vp == NULL) {
63                 DEBUG("ERROR: Received Digest-Response without Digest-Attributes");
64                 return RLM_MODULE_INVALID;
65         }
66
67         /*
68          *      Everything's OK, add a digest authentication type.
69          */
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));
74         }
75
76         return RLM_MODULE_OK;
77 }
78
79 /*
80  *      Perform all of the wondrous variants of digest authentication.
81  */
82 static int digest_authenticate(void *instance, REQUEST *request)
83 {
84         int i;
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;
92
93         instance = instance;    /* -Wunused */
94
95         /*
96          *      We require access to the plain-text password.
97          */
98         passwd = pairfind(request->config_items, PW_DIGEST_HA1);
99         if (passwd) {
100                 if (passwd->length != 32) {
101                         radlog(L_AUTH, "rlm_digest: Digest-HA1 has invalid length, authentication failed.");
102                         return RLM_MODULE_INVALID;
103                 }
104         } else {
105                 passwd = pairfind(request->config_items, PW_CLEARTEXT_PASSWORD);
106         }
107         if (!passwd) {
108                 radlog(L_AUTH, "rlm_digest: Cleartext-Password or Digest-HA1 is required for authentication.");
109                 return RLM_MODULE_INVALID;
110         }
111
112         /*
113          *      We need these, too.
114          */
115         vp = pairfind(request->packet->vps, PW_DIGEST_ATTRIBUTES);
116         if (vp == NULL) {
117                 DEBUG("ERROR: You set 'Auth-Type = Digest' for a request that did not contain any digest attributes!");
118                 return RLM_MODULE_INVALID;
119         }
120
121         /*
122          *      Loop through the Digest-Attributes, sanity checking them.
123          */
124         DEBUG("    rlm_digest: Converting Digest-Attributes to something sane...");
125         while (vp) {
126                 int length = vp->length;
127                 int attrlen;
128                 uint8_t *p = &vp->vp_octets[0];
129                 VALUE_PAIR *sub;
130
131                 /*
132                  *      Until this stupidly encoded attribure is exhausted.
133                  */
134                 while (length > 0) {
135                         /*
136                          *      The attribute type must be valid
137                          */
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;
141                         }
142
143                         attrlen = p[1]; /* stupid VSA format */
144
145                         /*
146                          *      Too short.
147                          */
148                         if (attrlen < 3) {
149                                 DEBUG("ERROR: Received Digest-Attributes with short sub-attribute %d, of length %d", p[0], attrlen);
150                                 return RLM_MODULE_INVALID;
151                         }
152
153                         /*
154                          *      Too long.
155                          */
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;
159                         }
160
161                         /*
162                          *      Create a new attribute, broken out of
163                          *      the stupid sub-attribute crap.
164                          *
165                          *      Didn't they know that VSA's exist?
166                          */
167                         sub = paircreate(PW_DIGEST_REALM - 1 + p[0],
168                                          PW_TYPE_STRING);
169                         if (!sub) {
170                                 return RLM_MODULE_FAIL; /* out of memory */
171                         }
172                         memcpy(&sub->vp_octets[0], &p[2], attrlen - 2);
173                         sub->vp_octets[attrlen - 2] = '\0';
174                         sub->length = attrlen - 2;
175
176                         if (debug_flag) {
177                           putchar('\t');
178                           vp_print(stdout, sub);
179                           putchar('\n');
180                         }
181
182                         /*
183                          *      And add it to the request pairs.
184                          */
185                         pairadd(&request->packet->vps, sub);
186
187                         /*
188                          *      FIXME: Check for the existence
189                          *      of the necessary attributes!
190                          */
191
192                         length -= attrlen;
193                         p += attrlen;
194                 } /* loop over this one attribute */
195
196                 /*
197                  *      Find the next one, if it exists.
198                  */
199                 vp = pairfind(vp->next, PW_DIGEST_ATTRIBUTES);
200         }
201
202         /*
203          *      We require access to the Digest-Nonce-Value
204          */
205         nonce = pairfind(request->packet->vps, PW_DIGEST_NONCE);
206         if (!nonce) {
207                 DEBUG("ERROR: No Digest-Nonce: Cannot perform Digest authentication");
208                 return RLM_MODULE_INVALID;
209         }
210
211         /*
212          *      A1 = Digest-User-Name ":" Realm ":" Password
213          */
214         vp = pairfind(request->packet->vps, PW_DIGEST_USER_NAME);
215         if (!vp) {
216                 DEBUG("ERROR: No Digest-User-Name: Cannot perform Digest authentication");
217                 return RLM_MODULE_INVALID;
218         }
219         memcpy(&a1[0], &vp->vp_octets[0], vp->length);
220         a1_len = vp->length;
221
222         a1[a1_len] = ':';
223         a1_len++;
224
225         vp = pairfind(request->packet->vps, PW_DIGEST_REALM);
226         if (!vp) {
227                 DEBUG("ERROR: No Digest-Realm: Cannot perform Digest authentication");
228                 return RLM_MODULE_INVALID;
229         }
230         memcpy(&a1[a1_len], &vp->vp_octets[0], vp->length);
231         a1_len += vp->length;
232
233         a1[a1_len] = ':';
234         a1_len++;
235
236         if (passwd->attribute == PW_CLEARTEXT_PASSWORD) {
237                 memcpy(&a1[a1_len], &passwd->vp_octets[0], passwd->length);
238                 a1_len += passwd->length;
239                 a1[a1_len] = '\0';
240                 DEBUG2("A1 = %s", a1);
241         } else {
242                 a1[a1_len] = '\0';
243                 DEBUG2("A1 = %s (using Digest-HA1)", a1);
244                 a1_len = 16;
245         }
246
247         /*
248          *      See which variant we calculate.
249          *      Assume MD5 if no Digest-Algorithm attribute received
250          */
251         algo = pairfind(request->packet->vps, PW_DIGEST_ALGORITHM);
252         if ((algo == NULL) || 
253             (strcasecmp(algo->vp_strvalue, "MD5") == 0)) {
254                 /*
255                  *      Set A1 to Digest-HA1 if no User-Password found
256                  */
257                 if (passwd->attribute == PW_DIGEST_HA1) {
258                         lrad_hex2bin(passwd->vp_strvalue, &a1[0], 16);
259                 }
260
261         } else if (strcasecmp(algo->vp_strvalue, "MD5-sess") == 0) {
262                 /*
263                  *      K1 = H(A1) : Digest-Nonce ... : H(A2)
264                  *
265                  *      If we find Digest-HA1, we assume it contains
266                  *      H(A1).
267                  */
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);
273                 }
274                 a1_len = 32;
275
276                 a1[a1_len] = ':';
277                 a1_len++;
278
279                 /*
280                  *      Tack on the Digest-Nonce. Length must be even
281                  */
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;
285                 }
286                 memcpy(&a1[a1_len], &nonce->vp_octets[0], nonce->length);
287                 a1_len += nonce->length;
288
289                 a1[a1_len] = ':';
290                 a1_len++;
291
292                 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
293                 if (!vp) {
294                         DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
295                         return RLM_MODULE_INVALID;
296                 }
297
298                 /*
299                  *      Digest-CNonce length must be even
300                  */
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;
304                 }
305                 memcpy(&a1[a1_len], &vp->vp_octets[0], vp->length);
306                 a1_len += vp->length;
307
308         } else if ((algo != NULL) &&
309                    (strcasecmp(algo->vp_strvalue, "MD5") != 0)) {
310                 /*
311                  *      We check for "MD5-sess" and "MD5".
312                  *      Anything else is an error.
313                  */
314                 DEBUG("ERROR: Unknown Digest-Algorithm \"%s\": Cannot perform Digest authentication", vp->vp_strvalue);
315                 return RLM_MODULE_INVALID;
316         }
317
318         /*
319          *      A2 = Digest-Method ":" Digest-URI
320          */
321         vp = pairfind(request->packet->vps, PW_DIGEST_METHOD);
322         if (!vp) {
323                 DEBUG("ERROR: No Digest-Method: Cannot perform Digest authentication");
324                 return RLM_MODULE_INVALID;
325         }
326         memcpy(&a2[0], &vp->vp_octets[0], vp->length);
327         a2_len = vp->length;
328
329         a2[a2_len] = ':';
330         a2_len++;
331
332         vp = pairfind(request->packet->vps, PW_DIGEST_URI);
333         if (!vp) {
334                 DEBUG("ERROR: No Digest-URI: Cannot perform Digest authentication");
335                 return RLM_MODULE_INVALID;
336         }
337         memcpy(&a2[a2_len], &vp->vp_octets[0], vp->length);
338         a2_len += vp->length;
339
340         /*
341          *  QOP is "auth-int", tack on ": Digest-Body-Digest"
342          */
343         qop = pairfind(request->packet->vps, PW_DIGEST_QOP);
344         if ((qop != NULL) &&
345             (strcasecmp(qop->vp_strvalue, "auth-int") == 0)) {
346                 VALUE_PAIR *body;
347
348                 /*
349                  *      Add in Digest-Body-Digest
350                  */
351                 a2[a2_len] = ':';
352                 a2_len++;
353
354                 /*
355                  *  Must be a hex representation of an MD5 digest.
356                  */
357                 body = pairfind(request->packet->vps, PW_DIGEST_BODY_DIGEST);
358                 if (!body) {
359                         DEBUG("ERROR: No Digest-Body-Digest: Cannot perform Digest authentication");
360                         return RLM_MODULE_INVALID;
361                 }
362
363                 if ((a2_len + body->length) > sizeof(a2)) {
364                         DEBUG("ERROR: Digest-Body-Digest is too long");
365                         return RLM_MODULE_INVALID;
366                 }
367
368                 memcpy(a2 + a2_len, body->vp_octets, body->length);
369                 a2_len += body->length;
370
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;
375         }
376
377         a2[a2_len] = '\0';
378         DEBUG2("A2 = %s", a2);
379
380         /*
381          *     KD = H(A1) : Digest-Nonce ... : H(A2).
382          *     Compute MD5 if Digest-Algorithm == "MD5-Sess",
383          *     or if we found a User-Password.
384          */
385         if (((algo != NULL) && 
386              (strcasecmp(algo->vp_strvalue, "MD5-Sess") == 0)) ||
387             (passwd->attribute == PW_CLEARTEXT_PASSWORD)) {
388                 a1[a1_len] = '\0';
389                 librad_md5_calc(&hash[0], &a1[0], a1_len);
390         } else {
391                 memcpy(&hash[0], &a1[0], a1_len);
392         }
393         lrad_bin2hex(hash, kd, sizeof(hash));
394
395 #ifndef NDEBUG
396         if (debug_flag) {
397                 printf("H(A1) = ");
398                 for (i = 0; i < 16; i++) {
399                         printf("%02x", hash[i]);
400                 }
401                 printf("\n");
402         }
403 #endif
404         kd_len = 32;
405
406         kd[kd_len] = ':';
407         kd_len++;
408
409         memcpy(&kd[kd_len], nonce->vp_octets, nonce->length);
410         kd_len += nonce->length;
411
412         /*
413          *      No QOP defined.  Do RFC 2069 compatibility.
414          */
415         if (!qop) {
416                 /*
417                  *      Do nothing here.
418                  */
419
420         } else {                /* Digest-QOP MUST be "auth" or "auth-int" */
421                 /*
422                  *      Tack on ":" Digest-Nonce-Count ":" Digest-CNonce
423                  *             ":" Digest-QOP
424                  */
425                 kd[kd_len] = ':';
426                 kd_len++;
427
428                 vp = pairfind(request->packet->vps, PW_DIGEST_NONCE_COUNT);
429                 if (!vp) {
430                         DEBUG("ERROR: No Digest-Nonce-Count: Cannot perform Digest authentication");
431                         return RLM_MODULE_INVALID;
432                 }
433                 memcpy(&kd[kd_len], &vp->vp_octets[0], vp->length);
434                 kd_len += vp->length;
435
436                 kd[kd_len] = ':';
437                 kd_len++;
438
439                 vp = pairfind(request->packet->vps, PW_DIGEST_CNONCE);
440                 if (!vp) {
441                         DEBUG("ERROR: No Digest-CNonce: Cannot perform Digest authentication");
442                         return RLM_MODULE_INVALID;
443                 }
444                 memcpy(&kd[kd_len], &vp->vp_octets[0], vp->length);
445                 kd_len += vp->length;
446
447                 kd[kd_len] = ':';
448                 kd_len++;
449
450                 memcpy(&kd[kd_len], &qop->vp_octets[0], qop->length);
451                 kd_len += qop->length;
452         }
453
454         /*
455          *      Tack on ":" H(A2)
456          */
457         kd[kd_len] = ':';
458         kd_len++;
459
460         librad_md5_calc(&hash[0], &a2[0], a2_len);
461
462         lrad_bin2hex(hash, kd + kd_len, sizeof(hash));
463
464 #ifndef NDEBUG
465         if (debug_flag) {
466                 printf("H(A2) = ");
467                 for (i = 0; i < 16; i++) {
468                         printf("%02x", hash[i]);
469                 }
470                 printf("\n");
471         }
472 #endif
473         kd_len += 32;
474
475         kd[kd_len] = 0;
476
477         DEBUG2("KD = %s\n", &kd[0]);
478
479         /*
480          *      Take the hash of KD.
481          */
482         librad_md5_calc(&hash[0], &kd[0], kd_len);
483         memcpy(&kd[0], &hash[0], 16);
484
485         /*
486          *      Get the binary value of Digest-Response
487          */
488         vp = pairfind(request->packet->vps, PW_DIGEST_RESPONSE);
489         if (!vp) {
490                 DEBUG("ERROR: No Digest-Response attribute in the request.  Cannot perform digest authentication");
491                 return RLM_MODULE_INVALID;
492         }
493
494         lrad_hex2bin(&vp->vp_octets[0], &hash[0], vp->length >> 1);
495
496 #ifndef NDEBUG
497         if (debug_flag) {
498                 printf("EXPECTED ");
499                 for (i = 0; i < 16; i++) {
500                         printf("%02x", kd[i]);
501                 }
502                 printf("\n");
503
504                 printf("RECEIVED ");
505                 for (i = 0; i < 16; i++) {
506                         printf("%02x", hash[i]);
507                 }
508                 printf("\n");
509         }
510 #endif
511
512         /*
513          *  And finally, compare the digest in the packet with KD.
514          */
515         if (memcmp(&kd[0], &hash[0], 16) == 0) {
516                 return RLM_MODULE_OK;
517         }
518
519         DEBUG("rlm_digest: FAILED authentication");
520         return RLM_MODULE_REJECT;
521 }
522
523 /*
524  *      The module name should be the only globally exported symbol.
525  *      That is, everything else should be 'static'.
526  *
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.
531  */
532 module_t rlm_digest = {
533         RLM_MODULE_INIT,
534         "digest",
535         0,                              /* type */
536         NULL,                           /* instantiation */
537         NULL,                           /* detach */
538         {
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 */
546                 NULL                    /* post-auth */
547         },
548 };