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
33 #include "gssapiP_eap.h"
35 /* stuff that should be provided by libradsec/libfreeradius-radius */
36 #define VENDORATTR(vendor, attr) (((vendor) << 16) | (attr))
39 #define ATTRID(attr) ((attr) & 0xFFFF)
42 static gss_buffer_desc radiusUrnPrefix = {
43 sizeof("urn:x-radius:") - 1,
44 (void *)"urn:x-radius:"
47 static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
49 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
52 m_authenticated = false;
55 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
62 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
63 const gss_eap_attr_provider *ctx)
65 const gss_eap_radius_attr_provider *radius;
67 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
70 radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
72 if (radius->m_vps != NULL)
73 m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
75 m_authenticated = radius->m_authenticated;
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)
85 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
88 if (gssCtx != GSS_C_NO_CONTEXT) {
89 if (gssCtx->acceptorCtx.vps != NULL) {
90 m_vps = copyAvps(gssCtx->acceptorCtx.vps);
94 /* We assume libradsec validated this for us */
95 assert(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
96 m_authenticated = true;
104 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
106 for (std::vector<std::string>::const_iterator a = attrs.begin();
109 if (strcmp(vp->name, (*a).c_str()) == 0)
117 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
124 case PW_MS_MPPE_SEND_KEY:
125 case PW_MS_MPPE_RECV_KEY:
139 isSecretAttributeP(uint32_t attribute)
141 return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
145 isHiddenAttributeP(uint16_t attrid, uint16_t vendor)
149 /* should have been filtered */
150 assert(!isSecretAttributeP(attrid, vendor));
153 case VENDORPEC_UKERNA:
164 isHiddenAttributeP(uint32_t attribute)
166 return isHiddenAttributeP(ATTRID(attribute), VENDOR(attribute));
170 * Copy AVP list, same as paircopy except it filters out attributes
174 copyAvps(const VALUE_PAIR *src)
176 const VALUE_PAIR *vp;
177 VALUE_PAIR *dst = NULL, **pDst = &dst;
179 for (vp = src; vp != NULL; vp = vp->next) {
182 if (isSecretAttributeP(vp->attribute))
185 vpcopy = paircopyvp(vp);
186 if (vpcopy == NULL) {
188 throw new std::bad_alloc;
192 pDst = &vpcopy->next;
199 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
202 std::vector <std::string> seen;
204 for (vp = m_vps; vp != NULL; vp = vp->next) {
205 gss_buffer_desc attribute;
208 if (isHiddenAttributeP(vp->attribute))
211 if (alreadyAddedAttributeP(seen, vp))
214 snprintf(attrid, sizeof(attrid), "%s%d",
215 (char *)radiusUrnPrefix.value, vp->attribute);
217 attribute.value = attrid;
218 attribute.length = strlen(attrid);
220 if (!addAttribute(this, &attribute, data))
223 seen.push_back(std::string(vp->name));
230 getAttributeId(const gss_buffer_t attr)
233 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
238 if (attr->length < radiusUrnPrefix.length ||
239 memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
242 /* need to duplicate because attr may not be NUL terminated */
243 duplicateBuffer(*attr, &strAttr);
244 s = (char *)strAttr.value + radiusUrnPrefix.length;
247 attrid = strtoul(s, NULL, 10);
249 da = dict_attrbyname(s);
254 gss_release_buffer(&tmpMinor, &strAttr);
260 gss_eap_radius_attr_provider::setAttribute(int complete,
262 const gss_buffer_t value)
264 OM_uint32 major = GSS_S_UNAVAILABLE, minor;
266 if (!isSecretAttributeP(attrid) &&
267 !isHiddenAttributeP(attrid)) {
268 deleteAttribute(attrid);
270 major = gssEapRadiusAddAvp(&minor, &m_vps,
271 ATTRID(attrid), VENDOR(attrid),
275 return !GSS_ERROR(major);
279 gss_eap_radius_attr_provider::setAttribute(int complete,
280 const gss_buffer_t attr,
281 const gss_buffer_t value)
283 uint32_t attrid = getAttributeId(attr);
288 return setAttribute(complete, attrid, value);
292 gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
294 if (isSecretAttributeP(attrid) || isHiddenAttributeP(attrid) ||
295 pairfind(m_vps, attrid) == NULL)
298 pairdelete(&m_vps, attrid);
304 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
306 uint32_t attrid = getAttributeId(attr);
311 return deleteAttribute(attrid);
315 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
319 gss_buffer_t display_value,
324 attrid = getAttributeId(attr);
328 return getAttribute(attrid, authenticated, complete,
329 value, display_value, more);
333 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
337 gss_buffer_t display_value,
341 int i = *more, count = 0;
345 if (isHiddenAttributeP(attrid))
351 for (vp = pairfind(m_vps, attrid);
353 vp = pairfind(vp->next, attrid)) {
355 if (pairfind(vp->next, attrid) != NULL)
361 if (vp == NULL && *more == 0)
364 if (value != GSS_C_NO_BUFFER) {
365 gss_buffer_desc valueBuf;
367 valueBuf.value = (void *)vp->vp_octets;
368 valueBuf.length = vp->length;
370 duplicateBuffer(valueBuf, value);
373 if (display_value != GSS_C_NO_BUFFER) {
374 char displayString[MAX_STRING_LEN];
375 gss_buffer_desc displayBuf;
377 displayBuf.length = vp_prints_value(displayString,
378 sizeof(displayString), vp, 0);
379 displayBuf.value = (void *)displayString;
381 duplicateBuffer(displayBuf, display_value);
384 if (authenticated != NULL)
385 *authenticated = m_authenticated;
386 if (complete != NULL)
393 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
397 gss_buffer_t value) const
399 OM_uint32 major, minor;
401 major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
403 if (authenticated != NULL)
404 *authenticated = m_authenticated;
405 if (complete != NULL)
408 return !GSS_ERROR(major);
412 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
417 gss_buffer_t display_value,
421 return getAttribute(VENDORATTR(attribute, vendor),
422 authenticated, complete,
423 value, display_value, more);
427 gss_eap_radius_attr_provider::mapToAny(int authenticated,
428 gss_buffer_t type_id) const
430 if (authenticated && !m_authenticated)
431 return (gss_any_t)NULL;
433 return (gss_any_t)copyAvps(m_vps);
437 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
438 gss_any_t input) const
440 pairfree((VALUE_PAIR **)&input);
444 gss_eap_radius_attr_provider::init(void)
446 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
447 "urn:ietf:params:gss-eap:radius-avp",
448 gss_eap_radius_attr_provider::createAttrContext);
453 gss_eap_radius_attr_provider::finalize(void)
455 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
458 gss_eap_attr_provider *
459 gss_eap_radius_attr_provider::createAttrContext(void)
461 return new gss_eap_radius_attr_provider;
465 gssEapRadiusAddAvp(OM_uint32 *minor,
469 const gss_buffer_t buffer)
471 uint32_t attrid = VENDORATTR(vendor, attribute);
472 unsigned char *p = (unsigned char *)buffer->value;
473 size_t remain = buffer->length;
479 if (n > MAX_STRING_LEN)
482 vp = paircreate(attrid, PW_TYPE_OCTETS);
485 return GSS_S_FAILURE;
488 memcpy(vp->vp_octets, p, n);
495 } while (remain != 0);
497 return GSS_S_COMPLETE;
501 gssEapRadiusGetRawAvp(OM_uint32 *minor,
507 uint32_t attr = VENDORATTR(vendor, attribute);
509 *vp = pairfind(vps, attr);
511 return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE;
515 gssEapRadiusGetAvp(OM_uint32 *minor,
524 uint32_t attr = VENDORATTR(vendor, attribute);
527 buffer->value = NULL;
529 vp = pairfind(vps, attr);
531 return GSS_S_UNAVAILABLE;
534 buffer->length += vp->length;
535 } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
537 buffer->value = GSSEAP_MALLOC(buffer->length);
538 if (buffer->value == NULL) {
540 return GSS_S_FAILURE;
543 p = (unsigned char *)buffer->value;
545 for (vp = pairfind(vps, attr);
546 concat && vp != NULL;
547 vp = pairfind(vp->next, attr)) {
548 memcpy(p, vp->vp_octets, vp->length);
553 return GSS_S_COMPLETE;
557 gssEapRadiusFreeAvps(OM_uint32 *minor,
562 return GSS_S_COMPLETE;
566 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
568 if (!gss_eap_radius_attr_provider::init()) {
569 *minor = GSSEAP_RADSEC_INIT_FAILURE;
570 return GSS_S_FAILURE;
573 return GSS_S_COMPLETE;
577 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
579 gss_eap_radius_attr_provider::finalize();
580 return GSS_S_COMPLETE;
585 * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
588 avpSize(const VALUE_PAIR *vp)
599 avpExport(const VALUE_PAIR *vp,
600 unsigned char **pBuffer,
603 unsigned char *p = *pBuffer;
604 size_t remain = *pRemain;
606 assert(remain >= avpSize(vp));
608 store_uint32_be(vp->attribute, p);
611 case PW_TYPE_INTEGER:
615 store_uint32_be(vp->lvalue, p + 5);
618 assert(vp->length <= MAX_STRING_LEN);
619 p[4] = (uint8_t)vp->length;
620 memcpy(p + 5, vp->vp_octets, vp->length);
624 *pBuffer += 5 + p[4];
625 *pRemain -= 5 + p[4];
632 avpImport(VALUE_PAIR **pVp,
633 unsigned char **pBuffer,
636 unsigned char *p = *pBuffer;
637 size_t remain = *pRemain;
638 VALUE_PAIR *vp = NULL;
642 if (remain < avpSize(NULL))
645 attrid = load_uint32_be(p);
649 da = dict_attrbyvalue(attrid);
655 throw new std::bad_alloc;
663 case PW_TYPE_INTEGER:
670 vp->lvalue = load_uint32_be(p + 1);
675 /* check enough room to NUL terminate */
676 if (p[0] == MAX_STRING_LEN)
681 if (p[0] > MAX_STRING_LEN)
684 vp->length = (uint32_t)p[0];
685 memcpy(vp->vp_octets, p + 1, vp->length);
687 if (vp->type == PW_TYPE_STRING)
688 vp->vp_strvalue[vp->length] = '\0';
691 remain -= 1 + vp->length;
707 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
708 const gss_buffer_t buffer)
710 unsigned char *p = (unsigned char *)buffer->value;
711 size_t remain = buffer->length;
712 VALUE_PAIR **pNext = &m_vps;
714 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
720 if (!avpImport(&attr, &p, &remain))
725 } while (remain != 0);
731 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
737 for (vp = m_vps; vp != NULL; vp = vp->next) {
738 remain += avpSize(vp);
741 buffer->value = GSSEAP_MALLOC(remain);
742 if (buffer->value == NULL) {
743 throw new std::bad_alloc;
746 buffer->length = remain;
748 p = (unsigned char *)buffer->value;
750 for (vp = m_vps; vp != NULL; vp = vp->next) {
751 avpExport(vp, &p, &remain);
758 gss_eap_radius_attr_provider::getExpiryTime(void) const
762 vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
763 if (vp == NULL || vp->lvalue == 0)
766 return time(NULL) + vp->lvalue;
770 gssEapRadiusMapError(OM_uint32 *minor,
771 struct rs_error *err)
777 code = rs_err_code(err, 0);
779 if (code == RSE_OK) {
781 return GSS_S_COMPLETE;
784 *minor = ERROR_TABLE_BASE_rse + code;
786 gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err, 0));
789 return GSS_S_FAILURE;