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 gss_eap_radius_attr_provider::setAttribute(int complete,
231 const gss_buffer_t attr,
232 const gss_buffer_t value)
237 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
242 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
246 gss_buffer_t display_value,
250 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
255 duplicateBuffer(*attr, &strAttr);
256 s = (char *)strAttr.value;
258 if (attr->length < radiusUrnPrefix.length ||
259 memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
262 s += radiusUrnPrefix.length;
265 attrid = strtoul(s, NULL, 10);
267 da = dict_attrbyname(s);
269 gss_release_buffer(&tmpMinor, &strAttr);
275 gss_release_buffer(&tmpMinor, &strAttr);
277 return getAttribute(attrid, authenticated, complete,
278 value, display_value, more);
282 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
286 gss_buffer_t display_value,
290 int i = *more, count = 0;
294 if (isHiddenAttributeP(attrid))
300 for (vp = pairfind(m_vps, attrid);
302 vp = pairfind(vp->next, attrid)) {
304 if (pairfind(vp->next, attrid) != NULL)
310 if (vp == NULL && *more == 0)
313 if (value != GSS_C_NO_BUFFER) {
314 gss_buffer_desc valueBuf;
316 valueBuf.value = (void *)vp->vp_octets;
317 valueBuf.length = vp->length;
319 duplicateBuffer(valueBuf, value);
322 if (display_value != GSS_C_NO_BUFFER) {
323 char displayString[MAX_STRING_LEN];
324 gss_buffer_desc displayBuf;
326 displayBuf.length = vp_prints_value(displayString,
327 sizeof(displayString), vp, 0);
328 displayBuf.value = (void *)displayString;
330 duplicateBuffer(displayBuf, display_value);
333 if (authenticated != NULL)
334 *authenticated = m_authenticated;
335 if (complete != NULL)
342 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
346 gss_buffer_t value) const
348 OM_uint32 major, minor;
350 major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
352 if (authenticated != NULL)
353 *authenticated = m_authenticated;
354 if (complete != NULL)
357 return !GSS_ERROR(major);
361 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
366 gss_buffer_t display_value,
370 return getAttribute(VENDORATTR(attribute, vendor),
371 authenticated, complete,
372 value, display_value, more);
376 gss_eap_radius_attr_provider::mapToAny(int authenticated,
377 gss_buffer_t type_id) const
379 if (authenticated && !m_authenticated)
380 return (gss_any_t)NULL;
382 return (gss_any_t)copyAvps(m_vps);
386 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
387 gss_any_t input) const
389 pairfree((VALUE_PAIR **)&input);
393 gss_eap_radius_attr_provider::init(void)
395 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
396 "urn:ietf:params:gss-eap:radius-avp",
397 gss_eap_radius_attr_provider::createAttrContext);
402 gss_eap_radius_attr_provider::finalize(void)
404 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
407 gss_eap_attr_provider *
408 gss_eap_radius_attr_provider::createAttrContext(void)
410 return new gss_eap_radius_attr_provider;
414 gssEapRadiusAddAvp(OM_uint32 *minor,
420 uint32_t attrid = VENDORATTR(vendor, attribute);
421 unsigned char *p = (unsigned char *)buffer->value;
422 size_t remain = buffer->length;
428 if (n > MAX_STRING_LEN)
431 vp = paircreate(attrid, PW_TYPE_OCTETS);
434 return GSS_S_FAILURE;
437 memcpy(vp->vp_octets, p, n);
444 } while (remain != 0);
446 return GSS_S_COMPLETE;
450 gssEapRadiusGetRawAvp(OM_uint32 *minor,
456 uint32_t attr = VENDORATTR(vendor, attribute);
458 *vp = pairfind(vps, attr);
460 return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE;
464 gssEapRadiusGetAvp(OM_uint32 *minor,
473 uint32_t attr = VENDORATTR(vendor, attribute);
476 buffer->value = NULL;
478 vp = pairfind(vps, attr);
480 return GSS_S_UNAVAILABLE;
483 buffer->length += vp->length;
484 } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
486 buffer->value = GSSEAP_MALLOC(buffer->length);
487 if (buffer->value == NULL) {
489 return GSS_S_FAILURE;
492 p = (unsigned char *)buffer->value;
494 for (vp = pairfind(vps, attr);
495 concat && vp != NULL;
496 vp = pairfind(vp->next, attr)) {
497 memcpy(p, vp->vp_octets, vp->length);
502 return GSS_S_COMPLETE;
506 gssEapRadiusFreeAvps(OM_uint32 *minor,
511 return GSS_S_COMPLETE;
515 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
517 if (!gss_eap_radius_attr_provider::init()) {
518 *minor = GSSEAP_RADSEC_INIT_FAILURE;
519 return GSS_S_FAILURE;
522 return GSS_S_COMPLETE;
526 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
528 gss_eap_radius_attr_provider::finalize();
529 return GSS_S_COMPLETE;
534 * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
537 avpSize(const VALUE_PAIR *vp)
548 avpExport(const VALUE_PAIR *vp,
549 unsigned char **pBuffer,
552 unsigned char *p = *pBuffer;
553 size_t remain = *pRemain;
555 assert(remain >= avpSize(vp));
557 store_uint32_be(vp->attribute, p);
560 case PW_TYPE_INTEGER:
564 store_uint32_be(vp->lvalue, p + 5);
567 assert(vp->length <= MAX_STRING_LEN);
568 p[4] = (uint8_t)vp->length;
569 memcpy(p + 5, vp->vp_octets, vp->length);
573 *pBuffer += 5 + p[4];
574 *pRemain -= 5 + p[4];
581 avpImport(VALUE_PAIR **pVp,
582 unsigned char **pBuffer,
585 unsigned char *p = *pBuffer;
586 size_t remain = *pRemain;
587 VALUE_PAIR *vp = NULL;
591 if (remain < avpSize(NULL))
594 attrid = load_uint32_be(p);
598 da = dict_attrbyvalue(attrid);
604 throw new std::bad_alloc;
612 case PW_TYPE_INTEGER:
619 vp->lvalue = load_uint32_be(p + 1);
624 /* check enough room to NUL terminate */
625 if (p[0] == MAX_STRING_LEN)
630 if (p[0] > MAX_STRING_LEN)
633 vp->length = (uint32_t)p[0];
634 memcpy(vp->vp_octets, p + 1, vp->length);
636 if (vp->type == PW_TYPE_STRING)
637 vp->vp_strvalue[vp->length] = '\0';
640 remain -= 1 + vp->length;
656 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
657 const gss_buffer_t buffer)
659 unsigned char *p = (unsigned char *)buffer->value;
660 size_t remain = buffer->length;
661 VALUE_PAIR **pNext = &m_vps;
663 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
669 if (!avpImport(&attr, &p, &remain))
674 } while (remain != 0);
680 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
686 for (vp = m_vps; vp != NULL; vp = vp->next) {
687 remain += avpSize(vp);
690 buffer->value = GSSEAP_MALLOC(remain);
691 if (buffer->value == NULL) {
692 throw new std::bad_alloc;
695 buffer->length = remain;
697 p = (unsigned char *)buffer->value;
699 for (vp = m_vps; vp != NULL; vp = vp->next) {
700 avpExport(vp, &p, &remain);
707 gss_eap_radius_attr_provider::getExpiryTime(void) const
711 vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
712 if (vp == NULL || vp->lvalue == 0)
715 return time(NULL) + vp->lvalue;
718 /* partition error namespace so it does not conflict with krb5 */
719 #define RS_TO_COM_ERR(rse) ((rse) == RSE_OK ? 0 : (rse) + ERROR_TABLE_BASE_rse)
720 #define COM_TO_RS_ERR(err) ((err) > ERROR_TABLE_BASE_rse && \
721 (err) <= (ERROR_TABLE_BASE_rse + RSE_SOME_ERROR) ? \
722 (err) - ERROR_TABLE_BASE_rse : RSE_SOME_ERROR)
725 gssEapRadiusMapError(OM_uint32 *minor,
726 struct rs_error *err)
731 code = rs_err_code(err, 0);
733 code = RSE_SOME_ERROR;
735 *minor = RS_TO_COM_ERR(code);
737 gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err, 0));
740 return GSS_S_FAILURE;