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 #define VENDORATTR(vendor, attr) ((vendor) << 16 | (attr))
38 #define ATTRID(attr) ((attr) & 0xFFFF)
41 static gss_buffer_desc radiusUrnPrefix = {
42 sizeof("urn:x-radius:") - 1,
43 (void *)"urn:x-radius:"
46 static struct rs_error *
47 radiusAllocHandle(const char *configFile,
51 struct rs_alloc_scheme ralloc;
55 if (configFile == NULL || configFile[0] == '\0')
56 configFile = RS_CONFIG_FILE;
58 if (rs_context_create(&rh, RS_DICT_FILE) != 0)
61 ralloc.calloc = gssEapCalloc;
62 ralloc.malloc = gssEapMalloc;
63 ralloc.free = gssEapFree;
64 ralloc.realloc = gssEapRealloc;
66 rs_context_set_alloc_scheme(rh, &ralloc);
68 if (rs_context_read_config(rh, configFile) != 0) {
69 rs_context_destroy(rh);
70 return rs_err_ctx_pop(rh);
77 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
81 m_authenticated = false;
84 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
87 rs_context_destroy(m_rh);
93 gss_eap_radius_attr_provider::allocRadHandle(const std::string &configFile)
95 m_configFile.assign(configFile);
98 * Currently none of the FreeRADIUS functions we use here actually take
99 * a handle, so we may as well leave it as NULL.
102 radiusAllocHandle(m_configFile.c_str(), &m_rh);
104 return (m_rh != NULL);
111 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
112 const gss_eap_attr_provider *ctx)
114 const gss_eap_radius_attr_provider *radius;
116 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
119 radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
121 if (!allocRadHandle(radius->m_configFile))
124 if (radius->m_avps != NULL)
125 m_avps = paircopy(const_cast<VALUE_PAIR *>(radius->getAvps()));
131 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
132 const gss_cred_id_t gssCred,
133 const gss_ctx_id_t gssCtx)
135 std::string configFile(RS_CONFIG_FILE);
137 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
140 if (gssCred != GSS_C_NO_CREDENTIAL && gssCred->radiusConfigFile != NULL)
141 configFile.assign(gssCred->radiusConfigFile);
143 if (!allocRadHandle(configFile))
146 if (gssCtx != GSS_C_NO_CONTEXT) {
147 if (gssCtx->acceptorCtx.avps != NULL) {
148 m_avps = paircopy(gssCtx->acceptorCtx.avps);
158 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
160 for (std::vector<std::string>::const_iterator a = attrs.begin();
163 if (strcmp(vp->name, (*a).c_str()) == 0)
171 isHiddenAttributeP(int attrid, uint16_t vendor)
178 case PW_MS_MPPE_SEND_KEY:
179 case PW_MS_MPPE_RECV_KEY:
185 case VENDORPEC_UKERNA:
196 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
199 std::vector <std::string> seen;
201 for (vp = m_avps; vp != NULL; vp = vp->next) {
202 gss_buffer_desc attribute;
204 if (isHiddenAttributeP(ATTRID(vp->attribute), VENDOR(vp->attribute)))
207 if (alreadyAddedAttributeP(seen, vp))
210 snprintf(attrid, sizeof(attrid), "%s%d",
211 (char *)radiusUrnPrefix.value, vp->attribute);
213 attribute.value = attrid;
214 attribute.length = strlen(attrid);
216 if (!addAttribute(this, &attribute, data))
219 seen.push_back(std::string(vp->name));
226 gss_eap_radius_attr_provider::setAttribute(int complete,
227 const gss_buffer_t attr,
228 const gss_buffer_t value)
233 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
238 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
242 gss_buffer_t display_value,
246 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
251 duplicateBuffer(*attr, &strAttr);
252 s = (char *)strAttr.value;
254 if (attr->length < radiusUrnPrefix.length ||
255 memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
258 s += radiusUrnPrefix.length;
261 attrid = strtoul(s, NULL, 10);
263 da = dict_attrbyname(s);
265 gss_release_buffer(&tmpMinor, &strAttr);
271 gss_release_buffer(&tmpMinor, &strAttr);
273 return getAttribute(attrid, authenticated, complete,
274 value, display_value, more);
278 gss_eap_radius_attr_provider::getAttribute(uint16_t vattrid,
283 gss_buffer_t display_value,
286 uint32_t attrid = VENDORATTR(vendor, vattrid);
288 int i = *more, count = 0;
292 if (isHiddenAttributeP(attrid, vendor))
298 for (vp = pairfind(m_avps, attrid);
300 vp = pairfind(vp->next, attrid)) {
302 if (pairfind(vp->next, attrid) != NULL)
308 if (vp == NULL && *more == 0)
311 if (value != GSS_C_NO_BUFFER) {
312 gss_buffer_desc valueBuf;
314 valueBuf.value = (void *)vp->vp_octets;
315 valueBuf.length = vp->length;
317 duplicateBuffer(valueBuf, value);
320 if (display_value != GSS_C_NO_BUFFER) {
321 char displayString[MAX_STRING_LEN];
322 gss_buffer_desc displayBuf;
324 displayBuf.length = vp_prints_value(displayString,
325 sizeof(displayString), vp, 0);
326 displayBuf.value = (void *)displayString;
328 duplicateBuffer(displayBuf, display_value);
331 if (authenticated != NULL)
332 *authenticated = m_authenticated;
333 if (complete != NULL)
340 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
344 gss_buffer_t value) const
346 OM_uint32 major, minor;
348 major = gssEapRadiusGetAvp(&minor, m_avps, attribute, vendor, value, TRUE);
350 if (authenticated != NULL)
351 *authenticated = m_authenticated;
352 if (complete != NULL)
355 return !GSS_ERROR(major);
359 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
363 gss_buffer_t display_value,
367 return getAttribute(ATTRID(attrid), VENDOR(attrid),
368 authenticated, complete,
369 value, display_value, more);
373 gss_eap_radius_attr_provider::mapToAny(int authenticated,
374 gss_buffer_t type_id) const
376 if (authenticated && !m_authenticated)
377 return (gss_any_t)NULL;
379 return (gss_any_t)paircopy(m_avps);
383 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
384 gss_any_t input) const
386 pairfree((VALUE_PAIR **)&input);
390 gss_eap_radius_attr_provider::init(void)
392 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
393 "urn:ietf:params:gss-eap:radius-avp",
394 gss_eap_radius_attr_provider::createAttrContext);
399 gss_eap_radius_attr_provider::finalize(void)
401 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
404 gss_eap_attr_provider *
405 gss_eap_radius_attr_provider::createAttrContext(void)
407 return new gss_eap_radius_attr_provider;
411 gssEapRadiusAddAvp(OM_uint32 *minor,
418 uint16_t attrid = VENDORATTR(vendor, vattrid);
419 unsigned char *p = (unsigned char *)buffer->value;
420 size_t remain = buffer->length;
426 if (n > MAX_STRING_LEN)
429 vp = paircreate(attrid, PW_TYPE_OCTETS);
432 return GSS_S_FAILURE;
435 memcpy(vp->vp_octets, p, n);
440 } while (remain != 0);
442 return GSS_S_COMPLETE;
446 gssEapRadiusGetRawAvp(OM_uint32 *minor,
452 uint16_t attr = VENDORATTR(vendor, type);
454 *vp = pairfind(vps, attr);
456 return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE;
460 gssEapRadiusGetAvp(OM_uint32 *minor,
469 uint32_t attr = VENDORATTR(vendor, type);
472 buffer->value = NULL;
474 vp = pairfind(vps, attr);
476 return GSS_S_UNAVAILABLE;
479 buffer->length += vp->length;
480 } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
482 buffer->value = GSSEAP_MALLOC(buffer->length);
483 if (buffer->value == NULL) {
485 return GSS_S_FAILURE;
488 p = (unsigned char *)buffer->value;
490 for (vp = pairfind(vps, attr);
491 concat && vp != NULL;
492 vp = pairfind(vp->next, attr)) {
493 memcpy(p, vp->vp_octets, vp->length);
498 return GSS_S_COMPLETE;
502 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
504 return gss_eap_radius_attr_provider::init()
505 ? GSS_S_COMPLETE : GSS_S_FAILURE;
509 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
511 gss_eap_radius_attr_provider::finalize();
512 return GSS_S_COMPLETE;
516 gssEapRadiusMapError(OM_uint32 *minor,
517 struct rs_error *err)
522 return GSS_S_FAILURE;
526 gssEapRadiusAllocConn(OM_uint32 *minor,
527 const gss_cred_id_t cred,
530 struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx;
531 const char *configFile = NULL;
532 struct rs_error *err;
534 assert(actx->radHandle == NULL);
535 assert(actx->radConn == NULL);
537 if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
538 configFile = cred->radiusConfigFile;
540 err = radiusAllocHandle(configFile, &actx->radHandle);
541 if (err != NULL || actx->radHandle == NULL) {
542 return gssEapRadiusMapError(minor, err);
545 if (rs_conn_create(actx->radHandle, &actx->radConn, "gss-eap") != 0) {
546 return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
549 /* XXX TODO rs_conn_select_server does not exist yet */
551 if (actx->radServer != NULL) {
552 if (rs_conn_select_server(actx->radConn, actx->radServer) != 0)
553 return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
558 return GSS_S_COMPLETE;
563 * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
566 avpSize(const VALUE_PAIR *vp)
577 avpExport(rs_handle *rh,
578 const VALUE_PAIR *vp,
579 unsigned char **pBuffer,
582 unsigned char *p = *pBuffer;
583 size_t remain = *pRemain;
585 assert(remain >= avpSize(vp));
587 store_uint32_be(vp->attribute, p);
590 case PW_TYPE_INTEGER:
594 store_uint32_be(vp->lvalue, p + 5);
597 assert(vp->length <= MAX_STRING_LEN);
598 p[4] = (uint8_t)vp->length;
599 memcpy(p + 5, vp->vp_octets, vp->length);
603 *pBuffer += 5 + p[4];
604 *pRemain -= 5 + p[4];
611 avpImport(rs_handle *rh,
613 unsigned char **pBuffer,
616 unsigned char *p = *pBuffer;
617 size_t remain = *pRemain;
618 VALUE_PAIR *vp = NULL;
622 if (remain < avpSize(NULL))
625 attrid = load_uint32_be(p);
629 da = dict_attrbyvalue(attrid);
635 throw new std::bad_alloc;
643 case PW_TYPE_INTEGER:
650 vp->lvalue = load_uint32_be(p + 1);
655 /* check enough room to NUL terminate */
656 if (p[0] >= MAX_STRING_LEN)
660 vp->length = (uint32_t)p[0];
661 memcpy(vp->vp_octets, p + 1, vp->length);
663 if (vp->type == PW_TYPE_STRING)
664 vp->vp_strvalue[vp->length] = '\0';
667 remain -= 1 + vp->length;
683 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
684 const gss_buffer_t buffer)
686 unsigned char *p = (unsigned char *)buffer->value;
687 size_t remain = buffer->length;
688 OM_uint32 configFileLen, count;
689 VALUE_PAIR **pNext = &m_avps;
691 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
697 configFileLen = load_uint32_be(p);
701 if (remain < configFileLen)
704 std::string configFile((char *)p, configFileLen);
706 remain -= configFileLen;
708 if (!allocRadHandle(configFile))
714 count = load_uint32_be(p);
721 if (!avpImport(m_rh, &attr, &p, &remain))
728 } while (remain != 0);
737 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
742 size_t remain = 4 + m_configFile.length() + 4;
744 for (vp = m_avps; vp != NULL; vp = vp->next) {
745 remain += avpSize(vp);
749 buffer->value = GSSEAP_MALLOC(remain);
750 if (buffer->value == NULL) {
751 throw new std::bad_alloc;
754 buffer->length = remain;
756 p = (unsigned char *)buffer->value;
758 store_uint32_be(m_configFile.length(), p);
762 memcpy(p, m_configFile.c_str(), m_configFile.length());
763 p += m_configFile.length();
764 remain -= m_configFile.length();
766 store_uint32_be(count, p);
770 for (vp = m_avps; vp != NULL; vp = vp->next) {
771 avpExport(m_rh, vp, &p, &remain);
778 gss_eap_radius_attr_provider::getExpiryTime(void) const
782 vp = pairfind(m_avps, PW_SESSION_TIMEOUT);
783 if (vp == NULL || vp->lvalue == 0)
786 return time(NULL) + vp->lvalue;