attrbutes internal to the GSS EAP mechanism (not keys) can now
[mech_eap.git] / util_radius.cpp
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 /* stuff that should be provided by libradsec/libfreeradius-radius */
36 #define VENDORATTR(vendor, attr)            (((vendor) << 16) | (attr))
37
38 #ifndef ATTRID
39 #define ATTRID(attr)                        ((attr) & 0xFFFF)
40 #endif
41
42 static gss_buffer_desc radiusUrnPrefix = {
43     sizeof("urn:x-radius:") - 1,
44     (void *)"urn:x-radius:"
45 };
46
47 static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
48
49 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
50 {
51     m_vps = NULL;
52     m_authenticated = false;
53 }
54
55 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
56 {
57     if (m_vps != NULL)
58         pairfree(&m_vps);
59 }
60
61 bool
62 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
63                                                       const gss_eap_attr_provider *ctx)
64 {
65     const gss_eap_radius_attr_provider *radius;
66
67     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
68         return false;
69
70     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
71
72     if (radius->m_vps != NULL)
73         m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
74
75     m_authenticated = radius->m_authenticated;
76
77     return true;
78 }
79
80 bool
81 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
82                                                  const gss_cred_id_t gssCred,
83                                                  const gss_ctx_id_t gssCtx)
84 {
85     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
86         return false;
87
88     if (gssCtx != GSS_C_NO_CONTEXT) {
89         if (gssCtx->acceptorCtx.vps != NULL) {
90             m_vps = copyAvps(gssCtx->acceptorCtx.vps);
91             if (m_vps == NULL)
92                 return false;
93
94             /* We assume libradsec validated this for us */
95             assert(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
96             m_authenticated = true;
97         }
98     }
99
100     return true;
101 }
102
103 static bool
104 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
105 {
106     for (std::vector<std::string>::const_iterator a = attrs.begin();
107          a != attrs.end();
108          ++a) {
109         if (strcmp(vp->name, (*a).c_str()) == 0)
110             return true;
111     }
112
113     return false;
114 }
115
116 static bool
117 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
118 {
119     bool ret = false;
120
121     switch (vendor) {
122     case VENDORPEC_MS:
123         switch (attrid) {
124         case PW_MS_MPPE_SEND_KEY:
125         case PW_MS_MPPE_RECV_KEY:
126             ret = true;
127             break;
128         default:
129             break;
130         }
131     default:
132         break;
133     }
134
135     return ret;
136 }
137
138 static bool
139 isSecretAttributeP(uint32_t attribute)
140 {
141     return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
142 }
143
144 static bool
145 isInternalAttributeP(uint16_t attrid, uint16_t vendor)
146 {
147     bool ret = false;
148
149     /* should have been filtered */
150     assert(!isSecretAttributeP(attrid, vendor));
151
152     switch (vendor) {
153     case VENDORPEC_UKERNA:
154         ret = true;
155         break;
156     default:
157         break;
158     }
159
160     return ret;
161 }
162
163 static bool
164 isInternalAttributeP(uint32_t attribute)
165 {
166     return isInternalAttributeP(ATTRID(attribute), VENDOR(attribute));
167 }
168
169 /*
170  * Copy AVP list, same as paircopy except it filters out attributes
171  * containing keys.
172  */
173 static VALUE_PAIR *
174 copyAvps(const VALUE_PAIR *src)
175 {
176     const VALUE_PAIR *vp;
177     VALUE_PAIR *dst = NULL, **pDst = &dst;
178
179     for (vp = src; vp != NULL; vp = vp->next) {
180         VALUE_PAIR *vpcopy;
181
182         if (isSecretAttributeP(vp->attribute))
183             continue;
184
185         vpcopy = paircopyvp(vp);
186         if (vpcopy == NULL) {
187             pairfree(&dst);
188             throw new std::bad_alloc;
189             return NULL;
190         }
191         *pDst = vpcopy;
192         pDst = &vpcopy->next;
193      }
194
195     return dst;
196 }
197
198 bool
199 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
200                                                 void *data) const
201 {
202     VALUE_PAIR *vp;
203     std::vector <std::string> seen;
204
205     for (vp = m_vps; vp != NULL; vp = vp->next) {
206         gss_buffer_desc attribute;
207         char attrid[64];
208
209         /* Don't advertise attributes that are internal to the GSS-EAP mechanism */
210         if (isInternalAttributeP(vp->attribute))
211             continue;
212
213         if (alreadyAddedAttributeP(seen, vp))
214             continue;
215
216         snprintf(attrid, sizeof(attrid), "%s%d",
217             (char *)radiusUrnPrefix.value, vp->attribute);
218
219         attribute.value = attrid;
220         attribute.length = strlen(attrid);
221
222         if (!addAttribute(this, &attribute, data))
223             return false;
224
225         seen.push_back(std::string(vp->name));
226     }
227
228     return true;
229 }
230
231 uint32_t
232 getAttributeId(const gss_buffer_t attr)
233 {
234     OM_uint32 tmpMinor;
235     gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
236     DICT_ATTR *da;
237     char *s;
238     uint32_t attrid = 0;
239
240     if (attr->length < radiusUrnPrefix.length ||
241         memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
242         return 0;
243
244     /* need to duplicate because attr may not be NUL terminated */
245     duplicateBuffer(*attr, &strAttr);
246     s = (char *)strAttr.value + radiusUrnPrefix.length;
247
248     if (isdigit(*s)) {
249         attrid = strtoul(s, NULL, 10);
250     } else {
251         da = dict_attrbyname(s);
252         if (da != NULL)
253             attrid = da->attr;
254     }
255
256     gss_release_buffer(&tmpMinor, &strAttr);
257
258     return attrid;
259 }
260
261 bool
262 gss_eap_radius_attr_provider::setAttribute(int complete,
263                                            uint32_t attrid,
264                                            const gss_buffer_t value)
265 {
266     OM_uint32 major = GSS_S_UNAVAILABLE, minor;
267
268     if (!isSecretAttributeP(attrid) &&
269         !isInternalAttributeP(attrid)) {
270         deleteAttribute(attrid);
271
272         major = gssEapRadiusAddAvp(&minor, &m_vps,
273                                    ATTRID(attrid), VENDOR(attrid), 
274                                    value);
275     }
276
277     return !GSS_ERROR(major);
278 }
279
280 bool
281 gss_eap_radius_attr_provider::setAttribute(int complete,
282                                            const gss_buffer_t attr,
283                                            const gss_buffer_t value)
284 {
285     uint32_t attrid = getAttributeId(attr);
286
287     if (!attrid)
288         return false;
289
290     return setAttribute(complete, attrid, value);
291 }
292
293 bool
294 gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
295 {
296     if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid) ||
297         pairfind(m_vps, attrid) == NULL)
298         return false;
299
300     pairdelete(&m_vps, attrid);
301
302     return true;
303 }
304
305 bool
306 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
307 {
308     uint32_t attrid = getAttributeId(attr);
309
310     if (!attrid)
311         return false;
312
313     return deleteAttribute(attrid);
314 }
315
316 bool
317 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
318                                            int *authenticated,
319                                            int *complete,
320                                            gss_buffer_t value,
321                                            gss_buffer_t display_value,
322                                            int *more) const
323 {
324     uint32_t attrid;
325
326     attrid = getAttributeId(attr);
327     if (!attrid)
328         return false;
329
330     return getAttribute(attrid, authenticated, complete,
331                         value, display_value, more);
332 }
333
334 bool
335 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
336                                            int *authenticated,
337                                            int *complete,
338                                            gss_buffer_t value,
339                                            gss_buffer_t display_value,
340                                            int *more) const
341 {
342     VALUE_PAIR *vp;
343     int i = *more, count = 0;
344
345     *more = 0;
346
347     if (i == -1)
348         i = 0;
349
350     for (vp = pairfind(m_vps, attrid);
351          vp != NULL;
352          vp = pairfind(vp->next, attrid)) {
353         if (count++ == i) {
354             if (pairfind(vp->next, attrid) != NULL)
355                 *more = count;
356             break;
357         }
358     }
359
360     if (vp == NULL && *more == 0)
361         return false;
362
363     if (value != GSS_C_NO_BUFFER) {
364         gss_buffer_desc valueBuf;
365
366         valueBuf.value = (void *)vp->vp_octets;
367         valueBuf.length = vp->length;
368
369         duplicateBuffer(valueBuf, value);
370     }
371
372     if (display_value != GSS_C_NO_BUFFER) {
373         char displayString[MAX_STRING_LEN];
374         gss_buffer_desc displayBuf;
375
376         displayBuf.length = vp_prints_value(displayString,
377                                             sizeof(displayString), vp, 0);
378         displayBuf.value = (void *)displayString;
379
380         duplicateBuffer(displayBuf, display_value);
381     }
382
383     if (authenticated != NULL)
384         *authenticated = m_authenticated;
385     if (complete != NULL)
386         *complete = true;
387
388     return true;
389 }
390
391 bool
392 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
393                                                      uint16_t vendor,
394                                                      int *authenticated,
395                                                      int *complete,
396                                                      gss_buffer_t value) const
397 {
398     OM_uint32 major, minor;
399
400     major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
401
402     if (authenticated != NULL)
403         *authenticated = m_authenticated;
404     if (complete != NULL)
405         *complete = true;
406
407     return !GSS_ERROR(major);
408 }
409
410 bool
411 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
412                                            uint16_t vendor,
413                                            int *authenticated,
414                                            int *complete,
415                                            gss_buffer_t value,
416                                            gss_buffer_t display_value,
417                                            int *more) const
418 {
419
420     return getAttribute(VENDORATTR(attribute, vendor),
421                         authenticated, complete,
422                         value, display_value, more);
423 }
424
425 gss_any_t
426 gss_eap_radius_attr_provider::mapToAny(int authenticated,
427                                        gss_buffer_t type_id) const
428 {
429     if (authenticated && !m_authenticated)
430         return (gss_any_t)NULL;
431
432     return (gss_any_t)copyAvps(m_vps);
433 }
434
435 void
436 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
437                                                     gss_any_t input) const
438 {
439     pairfree((VALUE_PAIR **)&input);
440 }
441
442 bool
443 gss_eap_radius_attr_provider::init(void)
444 {
445     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
446                                        "urn:ietf:params:gss-eap:radius-avp",
447                                        gss_eap_radius_attr_provider::createAttrContext);
448     return true;
449 }
450
451 void
452 gss_eap_radius_attr_provider::finalize(void)
453 {
454     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
455 }
456
457 gss_eap_attr_provider *
458 gss_eap_radius_attr_provider::createAttrContext(void)
459 {
460     return new gss_eap_radius_attr_provider;
461 }
462
463 OM_uint32
464 gssEapRadiusAddAvp(OM_uint32 *minor,
465                    VALUE_PAIR **vps,
466                    uint16_t attribute,
467                    uint16_t vendor,
468                    const gss_buffer_t buffer)
469 {
470     uint32_t attrid = VENDORATTR(vendor, attribute);
471     unsigned char *p = (unsigned char *)buffer->value;
472     size_t remain = buffer->length;
473
474     do {
475         VALUE_PAIR *vp;
476         size_t n = remain;
477
478         if (n > MAX_STRING_LEN)
479             n = MAX_STRING_LEN;
480
481         vp = paircreate(attrid, PW_TYPE_OCTETS);
482         if (vp == NULL) {
483             *minor = ENOMEM;
484             return GSS_S_FAILURE;
485         }
486
487         memcpy(vp->vp_octets, p, n);
488         vp->length = n;
489
490         pairadd(vps, vp);
491
492         p += n;
493         remain -= n;
494     } while (remain != 0);
495
496     return GSS_S_COMPLETE;
497 }
498
499 OM_uint32
500 gssEapRadiusGetRawAvp(OM_uint32 *minor,
501                       VALUE_PAIR *vps,
502                       uint16_t attribute,
503                       uint16_t vendor,
504                       VALUE_PAIR **vp)
505 {
506     uint32_t attr = VENDORATTR(vendor, attribute);
507
508     *vp = pairfind(vps, attr);
509     if (*vp == NULL) {
510         *minor = GSSEAP_NO_SUCH_ATTR;
511         return GSS_S_UNAVAILABLE;
512     }
513
514     return GSS_S_COMPLETE;
515 }
516
517 OM_uint32
518 gssEapRadiusGetAvp(OM_uint32 *minor,
519                    VALUE_PAIR *vps,
520                    uint16_t attribute,
521                    uint16_t vendor,
522                    gss_buffer_t buffer,
523                    int concat)
524 {
525     VALUE_PAIR *vp;
526     unsigned char *p;
527     uint32_t attr = VENDORATTR(vendor, attribute);
528
529     buffer->length = 0;
530     buffer->value = NULL;
531
532     vp = pairfind(vps, attr);
533     if (vp == NULL) {
534         *minor = GSSEAP_NO_SUCH_ATTR;
535         return GSS_S_UNAVAILABLE;
536     }
537
538     do {
539         buffer->length += vp->length;
540     } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
541
542     buffer->value = GSSEAP_MALLOC(buffer->length);
543     if (buffer->value == NULL) {
544         *minor = ENOMEM;
545         return GSS_S_FAILURE;
546     }
547
548     p = (unsigned char *)buffer->value;
549
550     for (vp = pairfind(vps, attr);
551          concat && vp != NULL;
552          vp = pairfind(vp->next, attr)) {
553         memcpy(p, vp->vp_octets, vp->length);
554         p += vp->length;
555     }
556
557     *minor = 0;
558     return GSS_S_COMPLETE;
559 }
560
561 OM_uint32
562 gssEapRadiusFreeAvps(OM_uint32 *minor,
563                      VALUE_PAIR **vps)
564 {
565     pairfree(vps);
566     *minor = 0;
567     return GSS_S_COMPLETE;
568 }
569
570 OM_uint32
571 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
572 {
573     if (!gss_eap_radius_attr_provider::init()) {
574         *minor = GSSEAP_RADSEC_INIT_FAILURE;
575         return GSS_S_FAILURE;
576     }
577
578     return GSS_S_COMPLETE;
579 }
580
581 OM_uint32
582 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
583 {
584     gss_eap_radius_attr_provider::finalize();
585     return GSS_S_COMPLETE;
586 }
587
588 /*
589  * Encoding is:
590  * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
591  */
592 static size_t
593 avpSize(const VALUE_PAIR *vp)
594 {
595     size_t size = 4 + 1;
596
597     if (vp != NULL)
598         size += vp->length;
599
600     return size;
601 }
602
603 static bool
604 avpExport(const VALUE_PAIR *vp,
605           unsigned char **pBuffer,
606           size_t *pRemain)
607 {
608     unsigned char *p = *pBuffer;
609     size_t remain = *pRemain;
610
611     assert(remain >= avpSize(vp));
612
613     store_uint32_be(vp->attribute, p);
614
615     switch (vp->type) {
616     case PW_TYPE_INTEGER:
617     case PW_TYPE_IPADDR:
618     case PW_TYPE_DATE:
619         p[4] = 4;
620         store_uint32_be(vp->lvalue, p + 5);
621         break;
622     default:
623         assert(vp->length <= MAX_STRING_LEN);
624         p[4] = (uint8_t)vp->length;
625         memcpy(p + 5, vp->vp_octets, vp->length);
626         break;
627     }
628
629     *pBuffer += 5 + p[4];
630     *pRemain -= 5 + p[4];
631
632     return true;
633
634 }
635
636 static bool
637 avpImport(VALUE_PAIR **pVp,
638           unsigned char **pBuffer,
639           size_t *pRemain)
640 {
641     unsigned char *p = *pBuffer;
642     size_t remain = *pRemain;
643     VALUE_PAIR *vp = NULL;
644     DICT_ATTR *da;
645     uint32_t attrid;
646
647     if (remain < avpSize(NULL))
648         goto fail;
649
650     attrid = load_uint32_be(p);
651     p += 4;
652     remain -= 4;
653
654     da = dict_attrbyvalue(attrid);
655     if (da == NULL)
656         goto fail;
657
658     vp = pairalloc(da);
659     if (vp == NULL) {
660         throw new std::bad_alloc;
661         goto fail;
662     }
663
664     if (remain < p[0])
665         goto fail;
666
667     switch (vp->type) {
668     case PW_TYPE_INTEGER:
669     case PW_TYPE_IPADDR:
670     case PW_TYPE_DATE:
671         if (p[0] != 4)
672             goto fail;
673
674         vp->length = 4;
675         vp->lvalue = load_uint32_be(p + 1);
676         p += 5;
677         remain -= 5;
678         break;
679     case PW_TYPE_STRING:
680         /* check enough room to NUL terminate */
681         if (p[0] == MAX_STRING_LEN)
682             goto fail;
683         else
684         /* fallthrough */
685     default:
686         if (p[0] > MAX_STRING_LEN)
687             goto fail;
688
689         vp->length = (uint32_t)p[0];
690         memcpy(vp->vp_octets, p + 1, vp->length);
691
692         if (vp->type == PW_TYPE_STRING)
693             vp->vp_strvalue[vp->length] = '\0';
694
695         p += 1 + vp->length;
696         remain -= 1 + vp->length;
697         break;
698     }
699
700     *pVp = vp;
701     *pBuffer = p;
702     *pRemain = remain;
703
704     return true;
705
706 fail:
707     pairbasicfree(vp);
708     return false;
709 }
710
711 bool
712 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
713                                              const gss_buffer_t buffer)
714 {
715     unsigned char *p = (unsigned char *)buffer->value;
716     size_t remain = buffer->length;
717     VALUE_PAIR **pNext = &m_vps;
718
719     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
720         return false;
721
722     do {
723         VALUE_PAIR *attr;
724
725         if (!avpImport(&attr, &p, &remain))
726             return false;
727
728         *pNext = attr;
729         pNext = &attr->next;
730     } while (remain != 0);
731
732     return true;
733 }
734
735 void
736 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
737 {
738     VALUE_PAIR *vp;
739     unsigned char *p;
740     size_t remain = 0;
741
742     for (vp = m_vps; vp != NULL; vp = vp->next) {
743         remain += avpSize(vp);
744     }
745
746     buffer->value = GSSEAP_MALLOC(remain);
747     if (buffer->value == NULL) {
748         throw new std::bad_alloc;
749         return;
750     }
751     buffer->length = remain;
752
753     p = (unsigned char *)buffer->value;
754
755     for (vp = m_vps; vp != NULL; vp = vp->next) {
756         avpExport(vp, &p, &remain);
757     }
758
759     assert(remain == 0);
760 }
761
762 time_t
763 gss_eap_radius_attr_provider::getExpiryTime(void) const
764 {
765     VALUE_PAIR *vp;
766
767     vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
768     if (vp == NULL || vp->lvalue == 0)
769         return 0;
770
771     return time(NULL) + vp->lvalue;
772 }
773
774 OM_uint32
775 gssEapRadiusMapError(OM_uint32 *minor,
776                      struct rs_error *err)
777 {
778     int code;
779
780     assert(err != NULL);
781
782     code = rs_err_code(err, 0);
783
784     if (code == RSE_OK) {
785         *minor = 0;
786         return GSS_S_COMPLETE;
787     }
788
789     *minor = ERROR_TABLE_BASE_rse + code;
790
791     gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err, 0));
792     rs_err_free(err);
793
794     return GSS_S_FAILURE;
795 }