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 static struct rs_error *
50 radiusAllocHandle(const char *configFile,
54 struct rs_alloc_scheme ralloc;
58 if (configFile == NULL || configFile[0] == '\0')
59 configFile = RS_CONFIG_FILE;
61 if (rs_context_create(&rh, RS_DICT_FILE) != 0)
64 ralloc.calloc = GSSEAP_CALLOC;
65 ralloc.malloc = GSSEAP_MALLOC;
66 ralloc.free = GSSEAP_FREE;
67 ralloc.realloc = GSSEAP_REALLOC;
69 rs_context_set_alloc_scheme(rh, &ralloc);
71 if (rs_context_read_config(rh, configFile) != 0) {
72 rs_context_destroy(rh);
73 return rs_err_ctx_pop(rh);
80 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
84 m_authenticated = false;
87 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
90 rs_context_destroy(m_rh);
96 gss_eap_radius_attr_provider::allocRadHandle(const std::string &configFile)
98 m_configFile.assign(configFile);
101 * Currently none of the FreeRADIUS functions we use here actually take
102 * a handle, so we may as well leave it as NULL.
105 radiusAllocHandle(m_configFile.c_str(), &m_rh);
107 return (m_rh != NULL);
114 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
115 const gss_eap_attr_provider *ctx)
117 const gss_eap_radius_attr_provider *radius;
119 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
122 radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
124 if (!allocRadHandle(radius->m_configFile))
127 if (radius->m_vps != NULL)
128 m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
134 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
135 const gss_cred_id_t gssCred,
136 const gss_ctx_id_t gssCtx)
138 std::string configFile(RS_CONFIG_FILE);
140 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
143 if (gssCred != GSS_C_NO_CREDENTIAL && gssCred->radiusConfigFile != NULL)
144 configFile.assign(gssCred->radiusConfigFile);
146 if (!allocRadHandle(configFile))
149 if (gssCtx != GSS_C_NO_CONTEXT) {
150 if (gssCtx->acceptorCtx.vps != NULL) {
151 m_vps = copyAvps(gssCtx->acceptorCtx.vps);
161 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
163 for (std::vector<std::string>::const_iterator a = attrs.begin();
166 if (strcmp(vp->name, (*a).c_str()) == 0)
174 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
181 case PW_MS_MPPE_SEND_KEY:
182 case PW_MS_MPPE_RECV_KEY:
196 isSecretAttributeP(uint32_t attribute)
198 return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
202 isHiddenAttributeP(uint16_t attrid, uint16_t vendor)
206 /* should have been filtered */
207 assert(!isSecretAttributeP(attrid, vendor));
210 case VENDORPEC_UKERNA:
221 isHiddenAttributeP(uint32_t attribute)
223 return isHiddenAttributeP(ATTRID(attribute), VENDOR(attribute));
227 * Copy AVP list, same as paircopy except it filters out attributes
231 copyAvps(const VALUE_PAIR *src)
233 const VALUE_PAIR *vp;
234 VALUE_PAIR *dst = NULL, **pDst = &dst;
236 for (vp = src; vp != NULL; vp = vp->next) {
239 if (isSecretAttributeP(vp->attribute))
242 vpcopy = paircopyvp(vp);
243 if (vpcopy == NULL) {
245 throw new std::bad_alloc;
249 pDst = &vpcopy->next;
256 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
259 std::vector <std::string> seen;
261 for (vp = m_vps; vp != NULL; vp = vp->next) {
262 gss_buffer_desc attribute;
265 if (isHiddenAttributeP(vp->attribute))
268 if (alreadyAddedAttributeP(seen, vp))
271 snprintf(attrid, sizeof(attrid), "%s%d",
272 (char *)radiusUrnPrefix.value, vp->attribute);
274 attribute.value = attrid;
275 attribute.length = strlen(attrid);
277 if (!addAttribute(this, &attribute, data))
280 seen.push_back(std::string(vp->name));
287 gss_eap_radius_attr_provider::setAttribute(int complete,
288 const gss_buffer_t attr,
289 const gss_buffer_t value)
294 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
299 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
303 gss_buffer_t display_value,
307 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
312 duplicateBuffer(*attr, &strAttr);
313 s = (char *)strAttr.value;
315 if (attr->length < radiusUrnPrefix.length ||
316 memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
319 s += radiusUrnPrefix.length;
322 attrid = strtoul(s, NULL, 10);
324 da = dict_attrbyname(s);
326 gss_release_buffer(&tmpMinor, &strAttr);
332 gss_release_buffer(&tmpMinor, &strAttr);
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;
351 if (isHiddenAttributeP(attrid))
357 for (vp = pairfind(m_vps, attrid);
359 vp = pairfind(vp->next, attrid)) {
361 if (pairfind(vp->next, attrid) != NULL)
367 if (vp == NULL && *more == 0)
370 if (value != GSS_C_NO_BUFFER) {
371 gss_buffer_desc valueBuf;
373 valueBuf.value = (void *)vp->vp_octets;
374 valueBuf.length = vp->length;
376 duplicateBuffer(valueBuf, value);
379 if (display_value != GSS_C_NO_BUFFER) {
380 char displayString[MAX_STRING_LEN];
381 gss_buffer_desc displayBuf;
383 displayBuf.length = vp_prints_value(displayString,
384 sizeof(displayString), vp, 0);
385 displayBuf.value = (void *)displayString;
387 duplicateBuffer(displayBuf, display_value);
390 if (authenticated != NULL)
391 *authenticated = m_authenticated;
392 if (complete != NULL)
399 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
403 gss_buffer_t value) const
405 OM_uint32 major, minor;
407 major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
409 if (authenticated != NULL)
410 *authenticated = m_authenticated;
411 if (complete != NULL)
414 return !GSS_ERROR(major);
418 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
423 gss_buffer_t display_value,
427 return getAttribute(VENDORATTR(attribute, vendor),
428 authenticated, complete,
429 value, display_value, more);
433 gss_eap_radius_attr_provider::mapToAny(int authenticated,
434 gss_buffer_t type_id) const
436 if (authenticated && !m_authenticated)
437 return (gss_any_t)NULL;
439 return (gss_any_t)copyAvps(m_vps);
443 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
444 gss_any_t input) const
446 pairfree((VALUE_PAIR **)&input);
450 gss_eap_radius_attr_provider::init(void)
452 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
453 "urn:ietf:params:gss-eap:radius-avp",
454 gss_eap_radius_attr_provider::createAttrContext);
459 gss_eap_radius_attr_provider::finalize(void)
461 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
464 gss_eap_attr_provider *
465 gss_eap_radius_attr_provider::createAttrContext(void)
467 return new gss_eap_radius_attr_provider;
471 gssEapRadiusAddAvp(OM_uint32 *minor,
478 uint32_t attrid = VENDORATTR(vendor, attribute);
479 unsigned char *p = (unsigned char *)buffer->value;
480 size_t remain = buffer->length;
486 if (n > MAX_STRING_LEN)
489 vp = paircreate(attrid, PW_TYPE_OCTETS);
492 return GSS_S_FAILURE;
495 memcpy(vp->vp_octets, p, n);
502 } while (remain != 0);
504 return GSS_S_COMPLETE;
508 gssEapRadiusGetRawAvp(OM_uint32 *minor,
514 uint32_t attr = VENDORATTR(vendor, attribute);
516 *vp = pairfind(vps, attr);
518 return (*vp == NULL) ? GSS_S_UNAVAILABLE : 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 return GSS_S_UNAVAILABLE;
541 buffer->length += vp->length;
542 } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
544 buffer->value = GSSEAP_MALLOC(buffer->length);
545 if (buffer->value == NULL) {
547 return GSS_S_FAILURE;
550 p = (unsigned char *)buffer->value;
552 for (vp = pairfind(vps, attr);
553 concat && vp != NULL;
554 vp = pairfind(vp->next, attr)) {
555 memcpy(p, vp->vp_octets, vp->length);
560 return GSS_S_COMPLETE;
564 gssEapRadiusFreeAvps(OM_uint32 *minor,
569 return GSS_S_COMPLETE;
573 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
575 return gss_eap_radius_attr_provider::init()
576 ? GSS_S_COMPLETE : GSS_S_FAILURE;
580 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
582 gss_eap_radius_attr_provider::finalize();
583 return GSS_S_COMPLETE;
586 /* partition error namespace so it does not conflict with krb5 */
587 #define ERROR_TABLE_BASE_rse (46882560L)
589 #define RS_TO_COM_ERR(rse) ((rse) == RSE_OK ? 0 : (rse) + ERROR_TABLE_BASE_rse)
590 #define COM_TO_RS_ERR(err) ((err) > ERROR_TABLE_BASE_rse && \
591 (err) <= (ERROR_TABLE_BASE_rse + RSE_SOME_ERROR) ? \
592 (err) - ERROR_TABLE_BASE_rse : RSE_SOME_ERROR)
595 gssEapRadiusMapError(OM_uint32 *minor,
596 struct rs_error *err)
601 code = rs_err_code(err, 0);
603 code = RSE_SOME_ERROR;
605 *minor = RS_TO_COM_ERR(code);
607 gssEapSaveStatusInfo(*minor, "radsec: %s", rs_err_msg(err, 0));
610 return GSS_S_FAILURE;
614 gssEapRadiusAllocConn(OM_uint32 *minor,
615 const gss_cred_id_t cred,
618 struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx;
619 const char *configFile = NULL;
620 const char *configStanza = "gss-eap";
621 struct rs_error *err;
623 assert(actx->radHandle == NULL);
624 assert(actx->radConn == NULL);
626 if (cred != GSS_C_NO_CREDENTIAL) {
627 if (cred->radiusConfigFile != NULL)
628 configFile = cred->radiusConfigFile;
629 if (cred->radiusConfigStanza != NULL)
630 configStanza = cred->radiusConfigStanza;
633 err = radiusAllocHandle(configFile, &actx->radHandle);
634 if (err != NULL || actx->radHandle == NULL) {
635 return gssEapRadiusMapError(minor, err);
638 if (rs_conn_create(actx->radHandle, &actx->radConn, configStanza) != 0) {
639 return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
642 /* XXX TODO rs_conn_select_server does not exist yet */
644 if (actx->radServer != NULL) {
645 if (rs_conn_select_server(actx->radConn, actx->radServer) != 0)
646 return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
651 return GSS_S_COMPLETE;
656 * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
659 avpSize(const VALUE_PAIR *vp)
670 avpExport(rs_handle *rh,
671 const VALUE_PAIR *vp,
672 unsigned char **pBuffer,
675 unsigned char *p = *pBuffer;
676 size_t remain = *pRemain;
678 assert(remain >= avpSize(vp));
680 store_uint32_be(vp->attribute, p);
683 case PW_TYPE_INTEGER:
687 store_uint32_be(vp->lvalue, p + 5);
690 assert(vp->length <= MAX_STRING_LEN);
691 p[4] = (uint8_t)vp->length;
692 memcpy(p + 5, vp->vp_octets, vp->length);
696 *pBuffer += 5 + p[4];
697 *pRemain -= 5 + p[4];
704 avpImport(rs_handle *rh,
706 unsigned char **pBuffer,
709 unsigned char *p = *pBuffer;
710 size_t remain = *pRemain;
711 VALUE_PAIR *vp = NULL;
715 if (remain < avpSize(NULL))
718 attrid = load_uint32_be(p);
722 da = dict_attrbyvalue(attrid);
728 throw new std::bad_alloc;
736 case PW_TYPE_INTEGER:
743 vp->lvalue = load_uint32_be(p + 1);
748 /* check enough room to NUL terminate */
749 if (p[0] == MAX_STRING_LEN)
754 if (p[0] > MAX_STRING_LEN)
757 vp->length = (uint32_t)p[0];
758 memcpy(vp->vp_octets, p + 1, vp->length);
760 if (vp->type == PW_TYPE_STRING)
761 vp->vp_strvalue[vp->length] = '\0';
764 remain -= 1 + vp->length;
780 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
781 const gss_buffer_t buffer)
783 unsigned char *p = (unsigned char *)buffer->value;
784 size_t remain = buffer->length;
785 OM_uint32 configFileLen, count;
786 VALUE_PAIR **pNext = &m_vps;
788 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
794 configFileLen = load_uint32_be(p);
798 if (remain < configFileLen)
801 std::string configFile((char *)p, configFileLen);
803 remain -= configFileLen;
805 if (!allocRadHandle(configFile))
811 count = load_uint32_be(p);
818 if (!avpImport(m_rh, &attr, &p, &remain))
825 } while (remain != 0);
834 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
839 size_t remain = 4 + m_configFile.length() + 4;
841 for (vp = m_vps; vp != NULL; vp = vp->next) {
842 remain += avpSize(vp);
846 buffer->value = GSSEAP_MALLOC(remain);
847 if (buffer->value == NULL) {
848 throw new std::bad_alloc;
851 buffer->length = remain;
853 p = (unsigned char *)buffer->value;
855 store_uint32_be(m_configFile.length(), p);
859 memcpy(p, m_configFile.c_str(), m_configFile.length());
860 p += m_configFile.length();
861 remain -= m_configFile.length();
863 store_uint32_be(count, p);
867 for (vp = m_vps; vp != NULL; vp = vp->next) {
868 avpExport(m_rh, vp, &p, &remain);
875 gss_eap_radius_attr_provider::getExpiryTime(void) const
879 vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
880 if (vp == NULL || vp->lvalue == 0)
883 return time(NULL) + vp->lvalue;