Corrected typo. Patch from Jouni Malinen
[freeradius.git] / src / modules / rlm_eap / radeapclient.c
1 /*
2  * radeapclient.c       EAP specific radius packet debug tool.
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 2000,2006  The FreeRADIUS server project
21  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24
25 #include <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include <freeradius-devel/libradius.h>
29
30 #include <ctype.h>
31
32 #if HAVE_GETOPT_H
33 #       include <getopt.h>
34 #endif
35
36 #include <freeradius-devel/conf.h>
37 #include <freeradius-devel/radpaths.h>
38 #include <freeradius-devel/md5.h>
39
40 #include "eap_types.h"
41 #include "eap_sim.h"
42
43 extern int sha1_data_problems;
44
45 static int retries = 10;
46 static float timeout = 3;
47 static const char *secret = NULL;
48 static int do_output = 1;
49 static int do_summary = 0;
50 static int filedone = 0;
51 static int totalapp = 0;
52 static int totaldeny = 0;
53 static char filesecret[256];
54 const char *radius_dir = RADDBDIR;
55 const char *progname = "radeapclient";
56 /* fr_randctx randctx; */
57
58
59 radlog_dest_t radlog_dest = RADLOG_STDERR;
60 const char *radlog_dir = NULL;
61 int debug_flag = 0;
62 char password[256];
63
64 struct eapsim_keys eapsim_mk;
65
66 static void map_eap_types(RADIUS_PACKET *req);
67 static void unmap_eap_types(RADIUS_PACKET *rep);
68 static int map_eapsim_types(RADIUS_PACKET *r);
69 static int unmap_eapsim_types(RADIUS_PACKET *r);
70
71 static void NEVER_RETURNS usage(void)
72 {
73         fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
74
75         fprintf(stderr, "  <command>    One of auth, acct, status, or disconnect.\n");
76         fprintf(stderr, "  -c count    Send each packet 'count' times.\n");
77         fprintf(stderr, "  -d raddb    Set dictionary directory.\n");
78         fprintf(stderr, "  -f file     Read packets from file, not stdin.\n");
79         fprintf(stderr, "  -r retries  If timeout, retry sending the packet 'retries' times.\n");
80         fprintf(stderr, "  -t timeout  Wait 'timeout' seconds before retrying (may be a floating point number).\n");
81         fprintf(stderr, "  -i id       Set request id to 'id'.  Values may be 0..255\n");
82         fprintf(stderr, "  -S file     read secret from file, not command line.\n");
83         fprintf(stderr, "  -q          Do not print anything out.\n");
84         fprintf(stderr, "  -s          Print out summary information of auth results.\n");
85         fprintf(stderr, "  -v          Show program version information.\n");
86         fprintf(stderr, "  -x          Debugging mode.\n");
87
88         exit(1);
89 }
90
91 int radlog(int lvl, const char *msg, ...)
92 {
93         va_list ap;
94         int r;
95
96         r = lvl; /* shut up compiler */
97
98         va_start(ap, msg);
99         r = vfprintf(stderr, msg, ap);
100         va_end(ap);
101         fputc('\n', stderr);
102
103         return r;
104 }
105
106 int log_debug(const char *msg, ...)
107 {
108         va_list ap;
109         int r;
110
111         va_start(ap, msg);
112         r = vfprintf(stderr, msg, ap);
113         va_end(ap);
114         fputc('\n', stderr);
115
116         return r;
117 }
118
119 static int getport(const char *name)
120 {
121         struct  servent         *svp;
122
123         svp = getservbyname (name, "udp");
124         if (!svp) {
125                 return 0;
126         }
127
128         return ntohs(svp->s_port);
129 }
130
131 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
132 {
133         int i;
134         struct timeval  tv;
135
136         for (i = 0; i < retries; i++) {
137                 fd_set          rdfdesc;
138
139                 rad_send(req, NULL, secret);
140
141                 /* And wait for reply, timing out as necessary */
142                 FD_ZERO(&rdfdesc);
143                 FD_SET(req->sockfd, &rdfdesc);
144
145                 tv.tv_sec = (int)timeout;
146                 tv.tv_usec = 1000000 * (timeout - (int) timeout);
147
148                 /* Something's wrong if we don't get exactly one fd. */
149                 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
150                         continue;
151                 }
152
153                 *rep = rad_recv(req->sockfd);
154                 if (*rep != NULL) {
155                         /*
156                          *      If we get a response from a machine
157                          *      which we did NOT send a request to,
158                          *      then complain.
159                          */
160                         if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
161                             (memcmp(&(*rep)->src_ipaddr.ipaddr,
162                                     &req->dst_ipaddr.ipaddr,
163                                     ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
164                                       sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
165                                       sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
166                             ((*rep)->src_port != req->dst_port)) {
167                                 char src[128], dst[128];
168
169                                 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
170                                 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
171                                 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
172                                         dst, req->dst_port,
173                                         src, (*rep)->src_port);
174                                 exit(1);
175                         }
176                         break;
177                 } else {        /* NULL: couldn't receive the packet */
178                         librad_perror("radclient:");
179                         exit(1);
180                 }
181         }
182
183         /* No response or no data read (?) */
184         if (i == retries) {
185                 fprintf(stderr, "radclient: no response from server\n");
186                 exit(1);
187         }
188
189         /*
190          *      FIXME: Discard the packet & listen for another.
191          *
192          *      Hmm... we should really be using eapol_test, which does
193          *      a lot more than radeapclient.
194          */
195         if (rad_verify(*rep, req, secret) != 0) {
196                 librad_perror("rad_verify");
197                 exit(1);
198         }
199
200         if (rad_decode(*rep, req, secret) != 0) {
201                 librad_perror("rad_decode");
202                 exit(1);
203         }
204
205         /* libradius debug already prints out the value pairs for us */
206         if (!librad_debug && do_output) {
207                 printf("Received response ID %d, code %d, length = %d\n",
208                                 (*rep)->id, (*rep)->code, (*rep)->data_len);
209                 vp_printlist(stdout, (*rep)->vps);
210         }
211         if((*rep)->code == PW_AUTHENTICATION_ACK) {
212                 totalapp++;
213         } else {
214                 totaldeny++;
215         }
216
217         return 0;
218 }
219
220 static void cleanresp(RADIUS_PACKET *resp)
221 {
222         VALUE_PAIR *vpnext, *vp, **last;
223
224
225         /*
226          * maybe should just copy things we care about, or keep
227          * a copy of the original input and start from there again?
228          */
229         pairdelete(&resp->vps, PW_EAP_MESSAGE);
230         pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
231
232         last = &resp->vps;
233         for(vp = *last; vp != NULL; vp = vpnext)
234         {
235                 vpnext = vp->next;
236
237                 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
238                     vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
239                    (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
240                     vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
241                 {
242                         *last = vpnext;
243                         pairbasicfree(vp);
244                 } else {
245                         last = &vp->next;
246                 }
247         }
248 }
249
250 /*
251  * we got an EAP-Request/Sim/Start message in a legal state.
252  *
253  * pick a supported version, put it into the reply, and insert a nonce.
254  */
255 static int process_eap_start(RADIUS_PACKET *req,
256                              RADIUS_PACKET *rep)
257 {
258         VALUE_PAIR *vp, *newvp;
259         VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
260         uint16_t *versions, selectedversion;
261         unsigned int i,versioncount;
262
263         /* form new response clear of any EAP stuff */
264         cleanresp(rep);
265
266         if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST)) == NULL) {
267                 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
268                 return 0;
269         }
270
271         versions = (uint16_t *)vp->vp_strvalue;
272
273         /* verify that the attribute length is big enough for a length field */
274         if(vp->length < 4)
275         {
276                 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
277                 return 0;
278         }
279
280         versioncount = ntohs(versions[0])/2;
281         /* verify that the attribute length is big enough for the given number
282          * of versions present.
283          */
284         if((unsigned)vp->length <= (versioncount*2 + 2))
285         {
286                 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
287                 return 0;
288         }
289
290         /*
291          * record the versionlist for the MK calculation.
292          */
293         eapsim_mk.versionlistlen = versioncount*2;
294         memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
295                eapsim_mk.versionlistlen);
296
297         /* walk the version list, and pick the one we support, which
298          * at present, is 1, EAP_SIM_VERSION.
299          */
300         selectedversion=0;
301         for(i=0; i < versioncount; i++)
302         {
303                 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
304                 {
305                         selectedversion=EAP_SIM_VERSION;
306                         break;
307                 }
308         }
309         if(selectedversion == 0)
310         {
311                 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
312                 for(i=0; i < versioncount; i++)
313                 {
314                         fprintf(stderr, "\tfound version %d\n",
315                                 ntohs(versions[i+1]));
316                 }
317         }
318
319         /*
320          * now make sure that we have only FULLAUTH_ID_REQ.
321          * I think that it actually might not matter - we can answer in
322          * anyway we like, but it is illegal to have more than one
323          * present.
324          */
325         anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ);
326         fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ);
327         permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ);
328
329         if(fullauthidreq_vp == NULL ||
330            anyidreq_vp != NULL ||
331            permanentidreq_vp != NULL) {
332                 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
333                         (anyidreq_vp != NULL ? "a " : "no "),
334                         (fullauthidreq_vp != NULL ? "a " : "no "),
335                         (permanentidreq_vp != NULL ? "a " : "no "));
336                 return 0;
337         }
338
339         /* okay, we have just any_id_req there, so fill in response */
340
341         /* mark the subtype as being EAP-SIM/Response/Start */
342         newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
343         newvp->vp_integer = eapsim_start;
344         pairreplace(&(rep->vps), newvp);
345
346         /* insert selected version into response. */
347         newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
348                            PW_TYPE_OCTETS);
349         versions = (uint16_t *)newvp->vp_strvalue;
350         versions[0] = htons(selectedversion);
351         newvp->length = 2;
352         pairreplace(&(rep->vps), newvp);
353
354         /* record the selected version */
355         memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
356
357         vp = newvp = NULL;
358
359         {
360                 uint32_t nonce[4];
361                 /*
362                  * insert a nonce_mt that we make up.
363                  */
364                 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
365                                    PW_TYPE_OCTETS);
366                 newvp->vp_octets[0]=0;
367                 newvp->vp_octets[1]=0;
368                 newvp->length = 18;  /* 16 bytes of nonce + padding */
369
370                 nonce[0]=fr_rand();
371                 nonce[1]=fr_rand();
372                 nonce[2]=fr_rand();
373                 nonce[3]=fr_rand();
374                 memcpy(&newvp->vp_octets[2], nonce, 16);
375                 pairreplace(&(rep->vps), newvp);
376
377                 /* also keep a copy of the nonce! */
378                 memcpy(eapsim_mk.nonce_mt, nonce, 16);
379         }
380
381         {
382                 uint16_t *pidlen, idlen;
383
384                 /*
385                  * insert the identity here.
386                  */
387                 vp = pairfind(rep->vps, PW_USER_NAME);
388                 if(vp == NULL)
389                 {
390                         fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
391                         return 0;
392                 }
393                 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
394                                    PW_TYPE_OCTETS);
395                 idlen = strlen(vp->vp_strvalue);
396                 pidlen = (uint16_t *)newvp->vp_strvalue;
397                 *pidlen = htons(idlen);
398                 newvp->length = idlen + 2;
399
400                 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
401                 pairreplace(&(rep->vps), newvp);
402
403                 /* record it */
404                 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
405                 eapsim_mk.identitylen = idlen;
406         }
407
408         return 1;
409 }
410
411 /*
412  * we got an EAP-Request/Sim/Challenge message in a legal state.
413  *
414  * use the RAND challenge to produce the SRES result, and then
415  * use that to generate a new MAC.
416  *
417  * for the moment, we ignore the RANDs, then just plug in the SRES
418  * values.
419  *
420  */
421 static int process_eap_challenge(RADIUS_PACKET *req,
422                                  RADIUS_PACKET *rep)
423 {
424         VALUE_PAIR *newvp;
425         VALUE_PAIR *mac, *randvp;
426         VALUE_PAIR *sres1,*sres2,*sres3;
427         VALUE_PAIR *Kc1, *Kc2, *Kc3;
428         uint8_t calcmac[20];
429
430         /* look for the AT_MAC and the challenge data */
431         mac   = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
432         randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND);
433         if(mac == NULL || randvp == NULL) {
434                 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
435                 return 0;
436         }
437
438         /*
439          * compare RAND with randX, to verify this is the right response
440          * to this challenge.
441          */
442         {
443           VALUE_PAIR *randcfgvp[3];
444           uint8_t *randcfg[3];
445
446           randcfg[0] = &randvp->vp_octets[2];
447           randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
448           randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
449
450           randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1);
451           randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2);
452           randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3);
453
454           if(randcfgvp[0] == NULL ||
455              randcfgvp[1] == NULL ||
456              randcfgvp[2] == NULL) {
457             fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
458             return 0;
459           }
460
461           if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
462              memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
463              memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
464             int rnum,i,j;
465
466             fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
467             for(rnum = 0; rnum < 3; rnum++) {
468               fprintf(stderr, "received   rand %d: ", rnum);
469               j=0;
470               for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
471                 if(j==4) {
472                   printf("_");
473                   j=0;
474                 }
475                 j++;
476
477                 fprintf(stderr, "%02x", randcfg[rnum][i]);
478               }
479               fprintf(stderr, "\nconfigured rand %d: ", rnum);
480               j=0;
481               for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
482                 if(j==4) {
483                   printf("_");
484                   j=0;
485                 }
486                 j++;
487
488                 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
489               }
490               fprintf(stderr, "\n");
491             }
492             return 0;
493           }
494         }
495
496         /*
497          * now dig up the sres values from the response packet,
498          * which were put there when we read things in.
499          *
500          * Really, they should be calculated from the RAND!
501          *
502          */
503         sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1);
504         sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2);
505         sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3);
506
507         if(sres1 == NULL ||
508            sres2 == NULL ||
509            sres3 == NULL) {
510                 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
511                 return 0;
512         }
513         memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
514         memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
515         memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
516
517         Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1);
518         Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2);
519         Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3);
520
521         if(Kc1 == NULL ||
522            Kc2 == NULL ||
523            Kc3 == NULL) {
524                 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
525                 return 0;
526         }
527         memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
528         memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
529         memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
530
531         /* all set, calculate keys */
532         eapsim_calculate_keys(&eapsim_mk);
533
534         if(debug_flag) {
535           eapsim_dump_mk(&eapsim_mk);
536         }
537
538         /* verify the MAC, now that we have all the keys. */
539         if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
540                            eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
541                            calcmac)) {
542                 printf("MAC check succeed\n");
543         } else {
544                 int i, j;
545                 j=0;
546                 printf("calculated MAC (");
547                 for (i = 0; i < 20; i++) {
548                         if(j==4) {
549                                 printf("_");
550                                 j=0;
551                         }
552                         j++;
553
554                         printf("%02x", calcmac[i]);
555                 }
556                 printf(" did not match\n");
557                 return 0;
558         }
559
560         /* form new response clear of any EAP stuff */
561         cleanresp(rep);
562
563         /* mark the subtype as being EAP-SIM/Response/Start */
564         newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
565         newvp->vp_integer = eapsim_challenge;
566         pairreplace(&(rep->vps), newvp);
567
568         /*
569          * fill the SIM_MAC with a field that will in fact get appended
570          * to the packet before the MAC is calculated
571          */
572         newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
573                            PW_TYPE_OCTETS);
574         memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
575         memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
576         memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
577         newvp->length = EAPSIM_SRES_SIZE*3;
578         pairreplace(&(rep->vps), newvp);
579
580         newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, PW_TYPE_OCTETS);
581         memcpy(newvp->vp_strvalue,    eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
582         newvp->length = EAPSIM_AUTH_SIZE;
583         pairreplace(&(rep->vps), newvp);
584
585         return 1;
586 }
587
588 /*
589  * this code runs the EAP-SIM client state machine.
590  * the *request* is from the server.
591  * the *reponse* is to the server.
592  *
593  */
594 static int respond_eap_sim(RADIUS_PACKET *req,
595                            RADIUS_PACKET *resp)
596 {
597         enum eapsim_clientstates state, newstate;
598         enum eapsim_subtype subtype;
599         VALUE_PAIR *vp, *statevp, *radstate, *eapid;
600         char statenamebuf[32], subtypenamebuf[32];
601
602         if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
603         {
604                 return 0;
605         }
606
607         if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
608         {
609                 return 0;
610         }
611
612         /* first, dig up the state from the request packet, setting
613          * outselves to be in EAP-SIM-Start state if there is none.
614          */
615
616         if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
617         {
618                 /* must be initial request */
619                 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, PW_TYPE_INTEGER);
620                 statevp->vp_integer = eapsim_client_init;
621                 pairreplace(&(resp->vps), statevp);
622         }
623         state = statevp->vp_integer;
624
625         /*
626          * map the attributes, and authenticate them.
627          */
628         unmap_eapsim_types(req);
629
630         printf("<+++ EAP-sim decoded packet:\n");
631         vp_printlist(stdout, req->vps);
632
633         if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
634         {
635                 return 0;
636         }
637         subtype = vp->vp_integer;
638
639         /*
640          * look for the appropriate state, and process incoming message
641          */
642         switch(state) {
643         case eapsim_client_init:
644                 switch(subtype) {
645                 case eapsim_start:
646                         newstate = process_eap_start(req, resp);
647                         break;
648
649                 case eapsim_challenge:
650                 case eapsim_notification:
651                 case eapsim_reauth:
652                 default:
653                         fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
654                                 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
655                                 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
656                         /* invalid state, drop message */
657                         return 0;
658                 }
659                 break;
660
661         case eapsim_client_start:
662                 switch(subtype) {
663                 case eapsim_start:
664                         /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
665                         newstate = process_eap_start(req, resp);
666                         break;
667
668                 case eapsim_challenge:
669                         newstate = process_eap_challenge(req, resp);
670                         break;
671
672                 default:
673                         fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
674                                 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
675                                 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
676                         /* invalid state, drop message */
677                         return 0;
678                 }
679                 break;
680
681
682         default:
683                 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
684                         sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
685                 return 0;
686         }
687
688         /* copy the eap state object in */
689         pairreplace(&(resp->vps), eapid);
690
691         /* update stete info, and send new packet */
692         map_eapsim_types(resp);
693
694         /* copy the radius state object in */
695         pairreplace(&(resp->vps), radstate);
696
697         statevp->vp_integer = newstate;
698         return 1;
699 }
700
701 static int respond_eap_md5(RADIUS_PACKET *req,
702                            RADIUS_PACKET *rep)
703 {
704         VALUE_PAIR *vp, *id, *state;
705         size_t valuesize, namesize;
706         uint8_t identifier;
707         uint8_t *value;
708         uint8_t *name;
709         FR_MD5_CTX      context;
710         uint8_t    response[16];
711
712         cleanresp(rep);
713
714         if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
715         {
716                 fprintf(stderr, "radeapclient: no state attribute found\n");
717                 return 0;
718         }
719
720         if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
721         {
722                 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
723                 return 0;
724         }
725         identifier = id->vp_integer;
726
727         if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
728         {
729                 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
730                 return 0;
731         }
732
733         /* got the details of the MD5 challenge */
734         valuesize = vp->vp_octets[0];
735         value = &vp->vp_octets[1];
736         name  = &vp->vp_octets[valuesize+1];
737         namesize = vp->length - (valuesize + 1);
738
739         /* sanitize items */
740         if(valuesize > vp->length)
741         {
742                 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
743                         valuesize, vp->length);
744                 return 0;
745         }
746
747         /* now do the CHAP operation ourself, rather than build the
748          * buffer. We could also call rad_chap_encode, but it wants
749          * a CHAP-Challenge, which we don't want to bother with.
750          */
751         fr_MD5Init(&context);
752         fr_MD5Update(&context, &identifier, 1);
753         fr_MD5Update(&context, (uint8_t *) password, strlen(password));
754         fr_MD5Update(&context, value, valuesize);
755         fr_MD5Final(response, &context);
756
757         vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
758         vp->vp_octets[0]=16;
759         memcpy(&vp->vp_strvalue[1], response, 16);
760         vp->length = 17;
761
762         pairreplace(&(rep->vps), vp);
763
764         pairreplace(&(rep->vps), id);
765
766         /* copy the state object in */
767         pairreplace(&(rep->vps), state);
768
769         return 1;
770 }
771
772
773
774 static int sendrecv_eap(RADIUS_PACKET *rep)
775 {
776         RADIUS_PACKET *req = NULL;
777         VALUE_PAIR *vp, *vpnext;
778         int tried_eap_md5 = 0;
779
780         /*
781          *      Keep a copy of the the User-Password attribute.
782          */
783         if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
784                 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
785
786         } else  if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
787                 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
788                 /*
789                  *      Otherwise keep a copy of the CHAP-Password attribute.
790                  */
791         } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
792                 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
793         } else {
794                 *password = '\0';
795         }
796
797  again:
798         rep->id++;
799
800         printf("\n+++> About to send encoded packet:\n");
801         vp_printlist(stdout, rep->vps);
802
803         /*
804          * if there are EAP types, encode them into an EAP-Message
805          *
806          */
807         map_eap_types(rep);
808
809         /*
810          *  Fix up Digest-Attributes issues
811          */
812         for (vp = rep->vps; vp != NULL; vp = vp->next) {
813                 switch (vp->attribute) {
814                 default:
815                         break;
816
817                 case PW_DIGEST_REALM:
818                 case PW_DIGEST_NONCE:
819                 case PW_DIGEST_METHOD:
820                 case PW_DIGEST_URI:
821                 case PW_DIGEST_QOP:
822                 case PW_DIGEST_ALGORITHM:
823                 case PW_DIGEST_BODY_DIGEST:
824                 case PW_DIGEST_CNONCE:
825                 case PW_DIGEST_NONCE_COUNT:
826                 case PW_DIGEST_USER_NAME:
827                         /* overlapping! */
828                         memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
829                         vp->vp_octets[0] = vp->attribute - PW_DIGEST_REALM + 1;
830                         vp->length += 2;
831                         vp->vp_octets[1] = vp->length;
832                         vp->attribute = PW_DIGEST_ATTRIBUTES;
833                         break;
834                 }
835         }
836
837         /*
838          *      If we've already sent a packet, free up the old
839          *      one, and ensure that the next packet has a unique
840          *      ID and authentication vector.
841          */
842         if (rep->data) {
843                 free(rep->data);
844                 rep->data = NULL;
845         }
846
847         fr_md5_calc(rep->vector, rep->vector,
848                         sizeof(rep->vector));
849
850         if (*password != '\0') {
851                 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
852                         strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
853                         vp->length = strlen(password);
854
855                 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
856                         strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
857                         vp->length = strlen(password);
858
859                 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
860                         strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
861                         vp->length = strlen(password);
862
863                         rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
864                         vp->length = 17;
865                 }
866         } /* there WAS a password */
867
868         /* send the response, wait for the next request */
869         send_packet(rep, &req);
870
871         /* okay got back the packet, go and decode the EAP-Message. */
872         unmap_eap_types(req);
873
874         printf("<+++ EAP decoded packet:\n");
875         vp_printlist(stdout, req->vps);
876
877         /* now look for the code type. */
878         for (vp = req->vps; vp != NULL; vp = vpnext) {
879                 vpnext = vp->next;
880
881                 switch (vp->attribute) {
882                 default:
883                         break;
884
885                 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
886                         if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
887                         {
888                                 tried_eap_md5++;
889                                 goto again;
890                         }
891                         break;
892
893                 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
894                         if(respond_eap_sim(req, rep))
895                         {
896                                 goto again;
897                         }
898                         break;
899                 }
900         }
901
902         return 1;
903 }
904
905
906 int main(int argc, char **argv)
907 {
908         RADIUS_PACKET *req;
909         char *p;
910         int c;
911         int port = 0;
912         char *filename = NULL;
913         FILE *fp;
914         int count = 1;
915         int id;
916
917         id = ((int)getpid() & 0xff);
918         librad_debug = 0;
919
920         radlog_dest = RADLOG_STDERR;
921
922         while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
923         {
924                 switch(c) {
925                 case 'c':
926                         if (!isdigit((int) *optarg))
927                                 usage();
928                         count = atoi(optarg);
929                         break;
930                 case 'd':
931                         radius_dir = optarg;
932                         break;
933                 case 'f':
934                         filename = optarg;
935                         break;
936                 case 'q':
937                         do_output = 0;
938                         break;
939                 case 'x':
940                         debug_flag++;
941                         librad_debug++;
942                         break;
943
944                 case 'X':
945 #if 0
946                   sha1_data_problems = 1; /* for debugging only */
947 #endif
948                   break;
949
950
951
952                 case 'r':
953                         if (!isdigit((int) *optarg))
954                                 usage();
955                         retries = atoi(optarg);
956                         break;
957                 case 'i':
958                         if (!isdigit((int) *optarg))
959                                 usage();
960                         id = atoi(optarg);
961                         if ((id < 0) || (id > 255)) {
962                                 usage();
963                         }
964                         break;
965                 case 's':
966                         do_summary = 1;
967                         break;
968                 case 't':
969                         if (!isdigit((int) *optarg))
970                                 usage();
971                         timeout = atof(optarg);
972                         break;
973                 case 'v':
974                         printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
975                         exit(0);
976                         break;
977                case 'S':
978                        fp = fopen(optarg, "r");
979                        if (!fp) {
980                                fprintf(stderr, "radclient: Error opening %s: %s\n",
981                                        optarg, strerror(errno));
982                                exit(1);
983                        }
984                        if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
985                                fprintf(stderr, "radclient: Error reading %s: %s\n",
986                                        optarg, strerror(errno));
987                                exit(1);
988                        }
989                        fclose(fp);
990
991                        /* truncate newline */
992                        p = filesecret + strlen(filesecret) - 1;
993                        while ((p >= filesecret) &&
994                               (*p < ' ')) {
995                                *p = '\0';
996                                --p;
997                        }
998
999                        if (strlen(filesecret) < 2) {
1000                                fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1001                                exit(1);
1002                        }
1003                        secret = filesecret;
1004                        break;
1005                 case 'h':
1006                 default:
1007                         usage();
1008                         break;
1009                 }
1010         }
1011         argc -= (optind - 1);
1012         argv += (optind - 1);
1013
1014         if ((argc < 3)  ||
1015             ((secret == NULL) && (argc < 4))) {
1016                 usage();
1017         }
1018
1019         if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1020                 librad_perror("radclient");
1021                 return 1;
1022         }
1023
1024         if ((req = rad_alloc(1)) == NULL) {
1025                 librad_perror("radclient");
1026                 exit(1);
1027         }
1028
1029 #if 0
1030         {
1031                 FILE *randinit;
1032
1033                 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1034                 {
1035                         perror("/dev/urandom");
1036                 } else {
1037                         fread(randctx.randrsl, 256, 1, randinit);
1038                         fclose(randinit);
1039                 }
1040         }
1041         fr_randinit(&randctx, 1);
1042 #endif
1043
1044         req->id = id;
1045
1046         /*
1047          *      Strip port from hostname if needed.
1048          */
1049         if ((p = strchr(argv[1], ':')) != NULL) {
1050                 *p++ = 0;
1051                 port = atoi(p);
1052         }
1053
1054         /*
1055          *      See what kind of request we want to send.
1056          */
1057         if (strcmp(argv[2], "auth") == 0) {
1058                 if (port == 0) port = getport("radius");
1059                 if (port == 0) port = PW_AUTH_UDP_PORT;
1060                 req->code = PW_AUTHENTICATION_REQUEST;
1061
1062         } else if (strcmp(argv[2], "acct") == 0) {
1063                 if (port == 0) port = getport("radacct");
1064                 if (port == 0) port = PW_ACCT_UDP_PORT;
1065                 req->code = PW_ACCOUNTING_REQUEST;
1066                 do_summary = 0;
1067
1068         } else if (strcmp(argv[2], "status") == 0) {
1069                 if (port == 0) port = getport("radius");
1070                 if (port == 0) port = PW_AUTH_UDP_PORT;
1071                 req->code = PW_STATUS_SERVER;
1072
1073         } else if (strcmp(argv[2], "disconnect") == 0) {
1074                 if (port == 0) port = PW_POD_UDP_PORT;
1075                 req->code = PW_DISCONNECT_REQUEST;
1076
1077         } else if (isdigit((int) argv[2][0])) {
1078                 if (port == 0) port = getport("radius");
1079                 if (port == 0) port = PW_AUTH_UDP_PORT;
1080                 req->code = atoi(argv[2]);
1081         } else {
1082                 usage();
1083         }
1084
1085         /*
1086          *      Resolve hostname.
1087          */
1088         req->dst_port = port;
1089         if (ip_hton(argv[1], AF_INET, &req->dst_ipaddr) < 0) {
1090                 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1091                 exit(1);
1092         }
1093
1094         /*
1095          *      Add the secret.
1096          */
1097         if (argv[3]) secret = argv[3];
1098
1099         /*
1100          *      Read valuepairs.
1101          *      Maybe read them, from stdin, if there's no
1102          *      filename, or if the filename is '-'.
1103          */
1104         if (filename && (strcmp(filename, "-") != 0)) {
1105                 fp = fopen(filename, "r");
1106                 if (!fp) {
1107                         fprintf(stderr, "radclient: Error opening %s: %s\n",
1108                                 filename, strerror(errno));
1109                         exit(1);
1110                 }
1111         } else {
1112                 fp = stdin;
1113         }
1114
1115         /*
1116          *      Send request.
1117          */
1118         if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1119                 perror("radclient: socket: ");
1120                 exit(1);
1121         }
1122
1123         while(!filedone) {
1124                 if(req->vps) pairfree(&req->vps);
1125
1126                 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1127                     == NULL) {
1128                         break;
1129                 }
1130
1131                 sendrecv_eap(req);
1132         }
1133
1134         if(do_summary) {
1135                 printf("\n\t   Total approved auths:  %d\n", totalapp);
1136                 printf("\t     Total denied auths:  %d\n", totaldeny);
1137         }
1138         return 0;
1139 }
1140
1141 /*
1142  * given a radius request with some attributes in the EAP range, build
1143  * them all into a single EAP-Message body.
1144  *
1145  * Note that this function will build multiple EAP-Message bodies
1146  * if there are multiple eligible EAP-types. This is incorrect, as the
1147  * recipient will in fact concatenate them.
1148  *
1149  * XXX - we could break the loop once we process one type. Maybe this
1150  *       just deserves an assert?
1151  *
1152  */
1153 static void map_eap_types(RADIUS_PACKET *req)
1154 {
1155         VALUE_PAIR *vp, *vpnext;
1156         int id, eapcode;
1157         EAP_PACKET ep;
1158         int eap_type;
1159
1160         vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
1161         if(vp == NULL) {
1162                 id = ((int)getpid() & 0xff);
1163         } else {
1164                 id = vp->vp_integer;
1165         }
1166
1167         vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
1168         if(vp == NULL) {
1169                 eapcode = PW_EAP_REQUEST;
1170         } else {
1171                 eapcode = vp->vp_integer;
1172         }
1173
1174
1175         for(vp = req->vps; vp != NULL; vp = vpnext) {
1176                 /* save it in case it changes! */
1177                 vpnext = vp->next;
1178
1179                 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
1180                    vp->attribute < ATTRIBUTE_EAP_BASE+256) {
1181                         break;
1182                 }
1183         }
1184
1185         if(vp == NULL) {
1186                 return;
1187         }
1188
1189         eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
1190
1191         switch(eap_type) {
1192         case PW_EAP_IDENTITY:
1193         case PW_EAP_NOTIFICATION:
1194         case PW_EAP_NAK:
1195         case PW_EAP_MD5:
1196         case PW_EAP_OTP:
1197         case PW_EAP_GTC:
1198         case PW_EAP_TLS:
1199         case PW_EAP_LEAP:
1200         case PW_EAP_TTLS:
1201         case PW_EAP_PEAP:
1202         default:
1203                 /*
1204                  * no known special handling, it is just encoded as an
1205                  * EAP-message with the given type.
1206                  */
1207
1208                 /* nuke any existing EAP-Messages */
1209                 pairdelete(&req->vps, PW_EAP_MESSAGE);
1210
1211                 memset(&ep, 0, sizeof(ep));
1212                 ep.code = eapcode;
1213                 ep.id   = id;
1214                 ep.type.type = eap_type;
1215                 ep.type.length = vp->length;
1216                 ep.type.data = malloc(vp->length);
1217                 memcpy(ep.type.data,vp->vp_octets, vp->length);
1218                 eap_basic_compose(req, &ep);
1219         }
1220 }
1221
1222 /*
1223  * given a radius request with an EAP-Message body, decode it specific
1224  * attributes.
1225  */
1226 static void unmap_eap_types(RADIUS_PACKET *rep)
1227 {
1228         VALUE_PAIR *eap1;
1229         eap_packet_t *e;
1230         int len;
1231         int type;
1232
1233         /* find eap message */
1234         e = eap_vp2packet(rep->vps);
1235
1236         /* nothing to do! */
1237         if(e == NULL) return;
1238
1239         /* create EAP-ID and EAP-CODE attributes to start */
1240         eap1 = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
1241         eap1->vp_integer = e->id;
1242         pairadd(&(rep->vps), eap1);
1243
1244         eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
1245         eap1->vp_integer = e->code;
1246         pairadd(&(rep->vps), eap1);
1247
1248         switch(e->code)
1249         {
1250         default:
1251         case PW_EAP_SUCCESS:
1252         case PW_EAP_FAILURE:
1253                 /* no data */
1254                 break;
1255
1256         case PW_EAP_REQUEST:
1257         case PW_EAP_RESPONSE:
1258                 /* there is a type field, which we use to create
1259                  * a new attribute */
1260
1261                 /* the length was decode already into the attribute
1262                  * length, and was checked already. Network byte
1263                  * order, just pull it out using math.
1264                  */
1265                 len = e->length[0]*256 + e->length[1];
1266
1267                 /* verify the length is big enough to hold type */
1268                 if(len < 5)
1269                 {
1270                         return;
1271                 }
1272
1273                 type = e->data[0];
1274
1275                 type += ATTRIBUTE_EAP_BASE;
1276                 len -= 5;
1277
1278                 if(len > MAX_STRING_LEN) {
1279                         len = MAX_STRING_LEN;
1280                 }
1281
1282                 eap1 = paircreate(type, PW_TYPE_OCTETS);
1283                 memcpy(eap1->vp_strvalue, &e->data[1], len);
1284                 eap1->length = len;
1285                 pairadd(&(rep->vps), eap1);
1286                 break;
1287         }
1288
1289         return;
1290 }
1291
1292 static int map_eapsim_types(RADIUS_PACKET *r)
1293 {
1294         EAP_PACKET ep;
1295         int ret;
1296
1297         memset(&ep, 0, sizeof(ep));
1298         ret = map_eapsim_basictypes(r, &ep);
1299         if(ret != 1) {
1300                 return ret;
1301         }
1302         eap_basic_compose(r, &ep);
1303
1304         return 1;
1305 }
1306
1307 static int unmap_eapsim_types(RADIUS_PACKET *r)
1308 {
1309         VALUE_PAIR             *esvp;
1310
1311         esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM);
1312         if (esvp == NULL) {
1313                 radlog(L_ERR, "eap: EAP-Sim attribute not found");
1314                 return 0;
1315         }
1316
1317         return unmap_eapsim_basictypes(r, esvp->vp_octets, esvp->length);
1318 }
1319
1320 #ifdef TEST_CASE
1321
1322 #include <assert.h>
1323
1324 const char *radius_dir = RADDBDIR;
1325
1326 int radlog(int lvl, const char *msg, ...)
1327 {
1328         va_list ap;
1329         int r;
1330
1331         va_start(ap, msg);
1332         r = vfprintf(stderr, msg, ap);
1333         va_end(ap);
1334         fputc('\n', stderr);
1335
1336         return r;
1337 }
1338
1339 main(int argc, char *argv[])
1340 {
1341         int filedone;
1342         RADIUS_PACKET *req,*req2;
1343         VALUE_PAIR *vp, *vpkey, *vpextra;
1344         extern unsigned int sha1_data_problems;
1345
1346         req = NULL;
1347         req2 = NULL;
1348         filedone = 0;
1349
1350         if(argc>1) {
1351           sha1_data_problems = 1;
1352         }
1353
1354         if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1355                 librad_perror("radclient");
1356                 return 1;
1357         }
1358
1359         if ((req = rad_alloc(1)) == NULL) {
1360                 librad_perror("radclient");
1361                 exit(1);
1362         }
1363
1364         if ((req2 = rad_alloc(1)) == NULL) {
1365                 librad_perror("radclient");
1366                 exit(1);
1367         }
1368
1369         while(!filedone) {
1370                 if(req->vps) pairfree(&req->vps);
1371                 if(req2->vps) pairfree(&req2->vps);
1372
1373                 if ((req->vps = readvp2(stdin, &filedone, "eapsimlib:")) == NULL) {
1374                         break;
1375                 }
1376
1377                 printf("\nRead:\n");
1378                 vp_printlist(stdout, req->vps);
1379
1380                 map_eapsim_types(req);
1381                 map_eap_types(req);
1382                 printf("Mapped to:\n");
1383                 vp_printlist(stdout, req->vps);
1384
1385                 /* find the EAP-Message, copy it to req2 */
1386                 vp = paircopy2(req->vps, PW_EAP_MESSAGE);
1387
1388                 if(vp == NULL) continue;
1389
1390                 pairadd(&req2->vps, vp);
1391
1392                 /* only call unmap for sim types here */
1393                 unmap_eap_types(req2);
1394                 unmap_eapsim_types(req2);
1395
1396                 printf("Unmapped to:\n");
1397                 vp_printlist(stdout, req2->vps);
1398
1399                 vp = pairfind(req2->vps,
1400                               ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
1401                 vpkey   = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY);
1402                 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA);
1403
1404                 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1405                         uint8_t calcmac[16];
1406
1407                         /* find the EAP-Message, copy it to req2 */
1408
1409                         memset(calcmac, 0, sizeof(calcmac));
1410                         printf("Confirming MAC...");
1411                         if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1412                                            vpextra->vp_strvalue, vpextra->length,
1413                                            calcmac)) {
1414                                 printf("succeed\n");
1415                         } else {
1416                                 int i, j;
1417
1418                                 printf("calculated MAC (");
1419                                 for (i = 0; i < 20; i++) {
1420                                         if(j==4) {
1421                                                 printf("_");
1422                                                 j=0;
1423                                         }
1424                                         j++;
1425
1426                                         printf("%02x", calcmac[i]);
1427                                 }
1428                                 printf(" did not match\n");
1429                         }
1430                 }
1431
1432                 fflush(stdout);
1433         }
1434 }
1435 #endif
1436