2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 * RADIUS attribute provider implementation.
37 #include "gssapiP_eap.h"
39 /* stuff that should be provided by libradsec/libfreeradius-radius */
40 #define VENDORATTR(vendor, attr) (((vendor) << 16) | (attr))
43 #define ATTRID(attr) ((attr) & 0xFFFF)
46 static gss_buffer_desc radiusUrnPrefix = {
47 sizeof("urn:x-radius:") - 1,
48 (void *)"urn:x-radius:"
51 static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
53 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
56 m_authenticated = false;
59 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
66 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
67 const gss_eap_attr_provider *ctx)
69 const gss_eap_radius_attr_provider *radius;
71 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
74 radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
76 if (radius->m_vps != NULL)
77 m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
79 m_authenticated = radius->m_authenticated;
85 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
86 const gss_cred_id_t gssCred,
87 const gss_ctx_id_t gssCtx)
89 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
92 if (gssCtx != GSS_C_NO_CONTEXT) {
93 if (gssCtx->acceptorCtx.vps != NULL) {
94 m_vps = copyAvps(gssCtx->acceptorCtx.vps);
98 /* We assume libradsec validated this for us */
99 assert(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
100 m_authenticated = true;
108 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
110 for (std::vector<std::string>::const_iterator a = attrs.begin();
113 if (strcmp(vp->name, (*a).c_str()) == 0)
121 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
123 bool bSecretAttribute = false;
128 case PW_MS_MPPE_SEND_KEY:
129 case PW_MS_MPPE_RECV_KEY:
130 bSecretAttribute = true;
139 return bSecretAttribute;
143 isSecretAttributeP(uint32_t attribute)
145 return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
149 isInternalAttributeP(uint16_t attrid, uint16_t vendor)
151 bool bInternalAttribute = false;
153 /* should have been filtered */
154 assert(!isSecretAttributeP(attrid, vendor));
157 case VENDORPEC_UKERNA:
158 bInternalAttribute = true;
164 return bInternalAttribute;
168 isInternalAttributeP(uint32_t attribute)
170 return isInternalAttributeP(ATTRID(attribute), VENDOR(attribute));
174 * Copy AVP list, same as paircopy except it filters out attributes
178 copyAvps(const VALUE_PAIR *src)
180 const VALUE_PAIR *vp;
181 VALUE_PAIR *dst = NULL, **pDst = &dst;
183 for (vp = src; vp != NULL; vp = vp->next) {
186 if (isSecretAttributeP(vp->attribute))
189 vpcopy = paircopyvp(vp);
190 if (vpcopy == NULL) {
192 throw new std::bad_alloc;
196 pDst = &vpcopy->next;
203 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
207 std::vector <std::string> seen;
209 for (vp = m_vps; vp != NULL; vp = vp->next) {
210 gss_buffer_desc attribute;
213 /* Don't advertise attributes that are internal to the GSS-EAP mechanism */
214 if (isInternalAttributeP(vp->attribute))
217 if (alreadyAddedAttributeP(seen, vp))
220 snprintf(attrid, sizeof(attrid), "%s%d",
221 (char *)radiusUrnPrefix.value, vp->attribute);
223 attribute.value = attrid;
224 attribute.length = strlen(attrid);
226 if (!addAttribute(this, &attribute, data))
229 seen.push_back(std::string(vp->name));
236 getAttributeId(const gss_buffer_t attr)
239 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
244 if (attr->length < radiusUrnPrefix.length ||
245 memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
248 /* need to duplicate because attr may not be NUL terminated */
249 duplicateBuffer(*attr, &strAttr);
250 s = (char *)strAttr.value + radiusUrnPrefix.length;
253 attrid = strtoul(s, NULL, 10);
255 da = dict_attrbyname(s);
260 gss_release_buffer(&tmpMinor, &strAttr);
266 gss_eap_radius_attr_provider::setAttribute(int complete,
268 const gss_buffer_t value)
270 OM_uint32 major = GSS_S_UNAVAILABLE, minor;
272 if (!isSecretAttributeP(attrid) &&
273 !isInternalAttributeP(attrid)) {
274 deleteAttribute(attrid);
276 major = gssEapRadiusAddAvp(&minor, &m_vps,
277 ATTRID(attrid), VENDOR(attrid),
281 return !GSS_ERROR(major);
285 gss_eap_radius_attr_provider::setAttribute(int complete,
286 const gss_buffer_t attr,
287 const gss_buffer_t value)
289 uint32_t attrid = getAttributeId(attr);
294 return setAttribute(complete, attrid, value);
298 gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
300 if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid) ||
301 pairfind(m_vps, attrid) == NULL)
304 pairdelete(&m_vps, attrid);
310 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
312 uint32_t attrid = getAttributeId(attr);
317 return deleteAttribute(attrid);
321 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
325 gss_buffer_t display_value,
330 attrid = getAttributeId(attr);
334 return getAttribute(attrid, authenticated, complete,
335 value, display_value, more);
339 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
343 gss_buffer_t display_value,
347 int i = *more, count = 0;
354 for (vp = pairfind(m_vps, attrid);
356 vp = pairfind(vp->next, attrid)) {
358 if (pairfind(vp->next, attrid) != NULL)
364 if (vp == NULL && *more == 0)
367 if (value != GSS_C_NO_BUFFER) {
368 gss_buffer_desc valueBuf;
370 valueBuf.value = (void *)vp->vp_octets;
371 valueBuf.length = vp->length;
373 duplicateBuffer(valueBuf, value);
376 if (display_value != GSS_C_NO_BUFFER) {
377 char displayString[MAX_STRING_LEN];
378 gss_buffer_desc displayBuf;
380 displayBuf.length = vp_prints_value(displayString,
381 sizeof(displayString), vp, 0);
382 displayBuf.value = (void *)displayString;
384 duplicateBuffer(displayBuf, display_value);
387 if (authenticated != NULL)
388 *authenticated = m_authenticated;
389 if (complete != NULL)
396 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
400 gss_buffer_t value) const
402 OM_uint32 major, minor;
404 major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
406 if (authenticated != NULL)
407 *authenticated = m_authenticated;
408 if (complete != NULL)
411 return !GSS_ERROR(major);
415 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
420 gss_buffer_t display_value,
424 return getAttribute(VENDORATTR(attribute, vendor),
425 authenticated, complete,
426 value, display_value, more);
430 gss_eap_radius_attr_provider::mapToAny(int authenticated,
431 gss_buffer_t type_id) const
433 if (authenticated && !m_authenticated)
434 return (gss_any_t)NULL;
436 return (gss_any_t)copyAvps(m_vps);
440 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
441 gss_any_t input) const
443 pairfree((VALUE_PAIR **)&input);
447 gss_eap_radius_attr_provider::init(void)
449 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
450 "urn:ietf:params:gss-eap:radius-avp",
456 gss_eap_radius_attr_provider::finalize(void)
458 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
461 gss_eap_attr_provider *
462 gss_eap_radius_attr_provider::createAttrContext(void)
464 return new gss_eap_radius_attr_provider;
468 gssEapRadiusAddAvp(OM_uint32 *minor,
472 const gss_buffer_t buffer)
474 uint32_t attrid = VENDORATTR(vendor, attribute);
475 unsigned char *p = (unsigned char *)buffer->value;
476 size_t remain = buffer->length;
482 if (n > MAX_STRING_LEN)
485 vp = paircreate(attrid, PW_TYPE_OCTETS);
488 return GSS_S_FAILURE;
491 memcpy(vp->vp_octets, p, n);
498 } while (remain != 0);
500 return GSS_S_COMPLETE;
504 gssEapRadiusGetRawAvp(OM_uint32 *minor,
510 uint32_t attr = VENDORATTR(vendor, attribute);
512 *vp = pairfind(vps, attr);
514 *minor = GSSEAP_NO_SUCH_ATTR;
515 return GSS_S_UNAVAILABLE;
518 return GSS_S_COMPLETE;
522 gssEapRadiusGetAvp(OM_uint32 *minor,
531 uint32_t attr = VENDORATTR(vendor, attribute);
534 buffer->value = NULL;
536 vp = pairfind(vps, attr);
538 *minor = GSSEAP_NO_SUCH_ATTR;
539 return GSS_S_UNAVAILABLE;
543 buffer->length += vp->length;
544 } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
546 buffer->value = GSSEAP_MALLOC(buffer->length);
547 if (buffer->value == NULL) {
549 return GSS_S_FAILURE;
552 p = (unsigned char *)buffer->value;
554 for (vp = pairfind(vps, attr);
555 concat && vp != NULL;
556 vp = pairfind(vp->next, attr)) {
557 memcpy(p, vp->vp_octets, vp->length);
562 return GSS_S_COMPLETE;
566 gssEapRadiusFreeAvps(OM_uint32 *minor,
571 return GSS_S_COMPLETE;
575 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
577 if (!gss_eap_radius_attr_provider::init()) {
578 *minor = GSSEAP_RADSEC_INIT_FAILURE;
579 return GSS_S_FAILURE;
582 return GSS_S_COMPLETE;
586 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
588 gss_eap_radius_attr_provider::finalize();
589 return GSS_S_COMPLETE;
594 * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
597 avpSize(const VALUE_PAIR *vp)
608 avpExport(const VALUE_PAIR *vp,
609 unsigned char **pBuffer,
612 unsigned char *p = *pBuffer;
613 size_t remain = *pRemain;
615 assert(remain >= avpSize(vp));
617 store_uint32_be(vp->attribute, p);
620 case PW_TYPE_INTEGER:
624 store_uint32_be(vp->lvalue, p + 5);
627 assert(vp->length <= MAX_STRING_LEN);
628 p[4] = (uint8_t)vp->length;
629 memcpy(p + 5, vp->vp_octets, vp->length);
633 *pBuffer += 5 + p[4];
634 *pRemain -= 5 + p[4];
641 avpImport(VALUE_PAIR **pVp,
642 unsigned char **pBuffer,
645 unsigned char *p = *pBuffer;
646 size_t remain = *pRemain;
647 VALUE_PAIR *vp = NULL;
651 if (remain < avpSize(NULL))
654 attrid = load_uint32_be(p);
658 da = dict_attrbyvalue(attrid);
664 throw new std::bad_alloc;
672 case PW_TYPE_INTEGER:
679 vp->lvalue = load_uint32_be(p + 1);
684 /* check enough room to NUL terminate */
685 if (p[0] == MAX_STRING_LEN)
690 if (p[0] > MAX_STRING_LEN)
693 vp->length = (uint32_t)p[0];
694 memcpy(vp->vp_octets, p + 1, vp->length);
696 if (vp->type == PW_TYPE_STRING)
697 vp->vp_strvalue[vp->length] = '\0';
700 remain -= 1 + vp->length;
716 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
717 const gss_buffer_t buffer)
719 unsigned char *p = (unsigned char *)buffer->value;
720 size_t remain = buffer->length;
721 VALUE_PAIR **pNext = &m_vps;
723 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
729 if (!avpImport(&attr, &p, &remain))
734 } while (remain != 0);
740 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
746 for (vp = m_vps; vp != NULL; vp = vp->next) {
747 remain += avpSize(vp);
750 buffer->value = GSSEAP_MALLOC(remain);
751 if (buffer->value == NULL) {
752 throw new std::bad_alloc;
755 buffer->length = remain;
757 p = (unsigned char *)buffer->value;
759 for (vp = m_vps; vp != NULL; vp = vp->next) {
760 avpExport(vp, &p, &remain);
767 gss_eap_radius_attr_provider::getExpiryTime(void) const
771 vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
772 if (vp == NULL || vp->lvalue == 0)
775 return time(NULL) + vp->lvalue;
779 gssEapRadiusMapError(OM_uint32 *minor,
780 struct rs_error *err)
786 code = rs_err_code(err, 0);
788 if (code == RSE_OK) {
790 return GSS_S_COMPLETE;
793 *minor = ERROR_TABLE_BASE_rse + code;
795 gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err, 0));
798 return GSS_S_FAILURE;