2 * Copyright (c) 2011, 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::initWithExistingContext(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::initWithExistingContext(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::initWithGssContext(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::initWithGssContext(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:
159 case PW_GSS_ACCEPTOR_SERVICE_NAME:
160 case PW_GSS_ACCEPTOR_HOST_NAME:
161 case PW_GSS_ACCEPTOR_SERVICE_SPECIFIC:
162 case PW_GSS_ACCEPTOR_REALM_NAME:
163 case PW_SAML_AAA_ASSERTION:
164 bInternalAttribute = true;
174 return bInternalAttribute;
178 isInternalAttributeP(uint32_t attribute)
180 return isInternalAttributeP(ATTRID(attribute), VENDOR(attribute));
184 isFragmentedAttributeP(uint16_t attrid, uint16_t vendor)
186 /* A bit of a hack for the PAC for now. Should be configurable. */
187 return (vendor == VENDORPEC_UKERNA) &&
188 !isInternalAttributeP(attrid, vendor);
192 isFragmentedAttributeP(uint32_t attribute)
194 return isFragmentedAttributeP(ATTRID(attribute), VENDOR(attribute));
198 * Copy AVP list, same as paircopy except it filters out attributes
202 copyAvps(const VALUE_PAIR *src)
204 const VALUE_PAIR *vp;
205 VALUE_PAIR *dst = NULL, **pDst = &dst;
207 for (vp = src; vp != NULL; vp = vp->next) {
210 if (isSecretAttributeP(vp->attribute))
213 vpcopy = paircopyvp(vp);
214 if (vpcopy == NULL) {
216 throw std::bad_alloc();
219 pDst = &vpcopy->next;
226 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
230 std::vector <std::string> seen;
232 for (vp = m_vps; vp != NULL; vp = vp->next) {
233 gss_buffer_desc attribute;
236 /* Don't advertise attributes that are internal to the GSS-EAP mechanism */
237 if (isInternalAttributeP(vp->attribute))
240 if (alreadyAddedAttributeP(seen, vp))
243 snprintf(attrid, sizeof(attrid), "%s%d",
244 (char *)radiusUrnPrefix.value, vp->attribute);
246 attribute.value = attrid;
247 attribute.length = strlen(attrid);
249 if (!addAttribute(m_manager, this, &attribute, data))
252 seen.push_back(std::string(vp->name));
259 getAttributeId(const gss_buffer_t attr)
262 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
267 if (attr->length < radiusUrnPrefix.length ||
268 memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
271 /* need to duplicate because attr may not be NUL terminated */
272 duplicateBuffer(*attr, &strAttr);
273 s = (char *)strAttr.value + radiusUrnPrefix.length;
276 attrid = strtoul(s, NULL, 10);
278 da = dict_attrbyname(s);
283 gss_release_buffer(&tmpMinor, &strAttr);
289 gss_eap_radius_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
291 const gss_buffer_t value)
293 OM_uint32 major = GSS_S_UNAVAILABLE, minor;
295 if (!isSecretAttributeP(attrid) &&
296 !isInternalAttributeP(attrid)) {
297 deleteAttribute(attrid);
299 major = gssEapRadiusAddAvp(&minor, &m_vps,
300 ATTRID(attrid), VENDOR(attrid),
304 return !GSS_ERROR(major);
308 gss_eap_radius_attr_provider::setAttribute(int complete,
309 const gss_buffer_t attr,
310 const gss_buffer_t value)
312 uint32_t attrid = getAttributeId(attr);
317 return setAttribute(complete, attrid, value);
321 gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
323 if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid) ||
324 pairfind(m_vps, attrid) == NULL)
327 pairdelete(&m_vps, attrid);
333 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
335 uint32_t attrid = getAttributeId(attr);
340 return deleteAttribute(attrid);
344 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
348 gss_buffer_t display_value,
353 attrid = getAttributeId(attr);
357 return getAttribute(attrid, authenticated, complete,
358 value, display_value, more);
362 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
366 gss_buffer_t display_value,
370 int i = *more, count = 0;
377 if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid)) {
379 } else if (isFragmentedAttributeP(attrid)) {
380 return getFragmentedAttribute(ATTRID(attrid),
387 for (vp = pairfind(m_vps, attrid);
389 vp = pairfind(vp->next, attrid)) {
391 if (pairfind(vp->next, attrid) != NULL)
397 if (vp == NULL && *more == 0)
400 if (value != GSS_C_NO_BUFFER) {
401 gss_buffer_desc valueBuf;
403 valueBuf.value = (void *)vp->vp_octets;
404 valueBuf.length = vp->length;
406 duplicateBuffer(valueBuf, value);
409 if (display_value != GSS_C_NO_BUFFER) {
410 char displayString[MAX_STRING_LEN];
411 gss_buffer_desc displayBuf;
413 displayBuf.length = vp_prints_value(displayString,
414 sizeof(displayString), vp, 0);
415 displayBuf.value = (void *)displayString;
417 duplicateBuffer(displayBuf, display_value);
420 if (authenticated != NULL)
421 *authenticated = m_authenticated;
422 if (complete != NULL)
429 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
433 gss_buffer_t value) const
435 OM_uint32 major, minor;
437 major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
439 if (authenticated != NULL)
440 *authenticated = m_authenticated;
441 if (complete != NULL)
444 return !GSS_ERROR(major);
448 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
453 gss_buffer_t display_value,
457 return getAttribute(VENDORATTR(attribute, vendor),
458 authenticated, complete,
459 value, display_value, more);
463 gss_eap_radius_attr_provider::mapToAny(int authenticated,
464 gss_buffer_t type_id GSSEAP_UNUSED) const
466 if (authenticated && !m_authenticated)
467 return (gss_any_t)NULL;
469 return (gss_any_t)copyAvps(m_vps);
473 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
474 gss_any_t input) const
476 VALUE_PAIR *vp = (VALUE_PAIR *)input;
481 gss_eap_radius_attr_provider::init(void)
483 struct rs_context *radContext;
485 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS, createAttrContext);
489 * This hack is necessary in order to force the loading of the global
490 * dictionary, otherwise accepting reauthentication tokens fails unless
491 * the acceptor has already accepted a normal authentication token.
493 if (rs_context_create(&radContext) != 0)
496 if (rs_context_read_config(radContext, RS_CONFIG_FILE) != 0) {
497 rs_context_destroy(radContext);
501 if (rs_context_init_freeradius_dict(radContext, NULL)) {
502 rs_context_destroy(radContext);
506 rs_context_destroy(radContext);
513 gss_eap_radius_attr_provider::finalize(void)
515 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
518 gss_eap_attr_provider *
519 gss_eap_radius_attr_provider::createAttrContext(void)
521 return new gss_eap_radius_attr_provider;
525 gssEapRadiusAddAvp(OM_uint32 *minor,
529 const gss_buffer_t buffer)
531 uint32_t attrid = VENDORATTR(vendor, attribute);
532 unsigned char *p = (unsigned char *)buffer->value;
533 size_t remain = buffer->length;
540 * There's an extra byte of padding; RADIUS AVPs can only
543 if (n >= MAX_STRING_LEN)
544 n = MAX_STRING_LEN - 1;
546 vp = paircreate(attrid, PW_TYPE_OCTETS);
549 return GSS_S_FAILURE;
552 memcpy(vp->vp_octets, p, n);
559 } while (remain != 0);
561 return GSS_S_COMPLETE;
565 gssEapRadiusGetRawAvp(OM_uint32 *minor,
571 uint32_t attr = VENDORATTR(vendor, attribute);
573 *vp = pairfind(vps, attr);
575 *minor = GSSEAP_NO_SUCH_ATTR;
576 return GSS_S_UNAVAILABLE;
579 return GSS_S_COMPLETE;
583 gssEapRadiusGetAvp(OM_uint32 *minor,
592 uint32_t attr = VENDORATTR(vendor, attribute);
594 if (buffer != GSS_C_NO_BUFFER) {
596 buffer->value = NULL;
599 vp = pairfind(vps, attr);
601 *minor = GSSEAP_NO_SUCH_ATTR;
602 return GSS_S_UNAVAILABLE;
605 if (buffer != GSS_C_NO_BUFFER) {
607 buffer->length += vp->length;
608 } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
610 buffer->value = GSSEAP_MALLOC(buffer->length);
611 if (buffer->value == NULL) {
613 return GSS_S_FAILURE;
616 p = (unsigned char *)buffer->value;
618 for (vp = pairfind(vps, attr);
619 concat && vp != NULL;
620 vp = pairfind(vp->next, attr)) {
621 memcpy(p, vp->vp_octets, vp->length);
627 return GSS_S_COMPLETE;
631 gssEapRadiusFreeAvps(OM_uint32 *minor,
636 return GSS_S_COMPLETE;
640 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
642 if (!gss_eap_radius_attr_provider::init()) {
643 *minor = GSSEAP_RADSEC_INIT_FAILURE;
644 return GSS_S_FAILURE;
647 return GSS_S_COMPLETE;
651 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
653 gss_eap_radius_attr_provider::finalize();
656 return GSS_S_COMPLETE;
660 avpToJson(const VALUE_PAIR *vp)
664 assert(vp->length <= MAX_STRING_LEN);
667 case PW_TYPE_INTEGER:
670 obj.set("value", vp->lvalue);
673 obj.set("value", vp->vp_strvalue);
678 if (base64Encode(vp->vp_octets, vp->length, &b64) < 0)
679 throw std::bad_alloc();
681 obj.set("value", b64);
687 obj.set("type", vp->attribute);
693 jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
695 VALUE_PAIR *vp = NULL;
699 JSONObject type = obj["type"];
700 JSONObject value = obj["value"];
702 if (!type.isInteger())
705 attrid = type.integer();
706 da = dict_attrbyvalue(attrid);
710 int type = base64Valid(value.string()) ?
711 PW_TYPE_OCTETS : PW_TYPE_STRING;
712 vp = paircreate(attrid, type);
715 throw std::bad_alloc();
718 case PW_TYPE_INTEGER:
721 if (!value.isInteger())
725 vp->lvalue = value.integer();
727 case PW_TYPE_STRING: {
728 if (!value.isString())
731 const char *str = value.string();
732 size_t len = strlen(str);
734 if (len >= MAX_STRING_LEN)
738 memcpy(vp->vp_strvalue, str, len + 1);
743 if (!value.isString())
746 const char *str = value.string();
747 ssize_t len = strlen(str);
749 /* this optimization requires base64Decode only understand packed encoding */
750 if (len >= BASE64_EXPAND(MAX_STRING_LEN))
753 len = base64Decode(str, vp->vp_octets);
774 gss_eap_radius_attr_provider::name(void) const
780 gss_eap_radius_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
783 VALUE_PAIR **pNext = &m_vps;
785 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
788 JSONObject attrs = obj["attributes"];
789 size_t nelems = attrs.size();
791 for (size_t i = 0; i < nelems; i++) {
792 JSONObject attr = attrs[i];
795 if (!jsonToAvp(&vp, attr))
802 m_authenticated = obj["authenticated"].integer();
808 gss_eap_radius_attr_provider::prefix(void) const
810 return "urn:ietf:params:gss-eap:radius-avp";
814 gss_eap_radius_attr_provider::jsonRepresentation(void) const
816 JSONObject obj, attrs = JSONObject::array();
818 for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) {
819 JSONObject attr = avpToJson(vp);
823 obj.set("attributes", attrs);
825 obj.set("authenticated", m_authenticated);
831 gss_eap_radius_attr_provider::getExpiryTime(void) const
835 vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
836 if (vp == NULL || vp->lvalue == 0)
839 return time(NULL) + vp->lvalue;
843 gssEapRadiusMapError(OM_uint32 *minor,
844 struct rs_error *err)
850 code = rs_err_code(err, 0);
852 if (code == RSE_OK) {
854 return GSS_S_COMPLETE;
857 *minor = ERROR_TABLE_BASE_rse + code;
859 gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err));
862 return GSS_S_FAILURE;