remove debugging statement
[moonshot.git] / mech_eap / util_radius.cpp
index 43d329f..2411b04 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, JANET(UK)
+ * Copyright (c) 2011, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * SUCH DAMAGE.
  */
 
+/*
+ * RADIUS attribute provider implementation.
+ */
+
 #include "gssapiP_eap.h"
 
-#define VENDORATTR(vendor, attr)            ((vendor) << 16 | (attr))
+#include <xercesc/util/Base64.hpp>
+
+/* stuff that should be provided by libradsec/libfreeradius-radius */
+#define VENDORATTR(vendor, attr)            (((vendor) << 16) | (attr))
 
 #ifndef ATTRID
 #define ATTRID(attr)                        ((attr) & 0xFFFF)
@@ -43,71 +50,21 @@ static gss_buffer_desc radiusUrnPrefix = {
     (void *)"urn:x-radius:"
 };
 
-static struct rs_error *
-radiusAllocHandle(const char *configFile,
-                  rs_handle **pHandle)
-{
-    rs_handle *rh;
-    struct rs_alloc_scheme ralloc;
-
-    *pHandle = NULL;
-
-    if (configFile == NULL || configFile[0] == '\0')
-        configFile = RS_CONFIG_FILE;
-
-    if (rs_context_create(&rh, RS_DICT_FILE) != 0)
-        return NULL;
-
-    ralloc.calloc = gssEapCalloc;
-    ralloc.malloc = gssEapMalloc;
-    ralloc.free = gssEapFree;
-    ralloc.realloc = gssEapRealloc;
-
-    rs_context_set_alloc_scheme(rh, &ralloc);
-
-    if (rs_context_read_config(rh, configFile) != 0) {
-        rs_context_destroy(rh);
-        return rs_err_ctx_pop(rh);
-    }
-
-    *pHandle = rh;
-    return NULL;
-}
+static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
 
 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
 {
-    m_rh = NULL;
     m_vps = NULL;
     m_authenticated = false;
 }
 
 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
 {
-    if (m_rh != NULL)
-        rs_context_destroy(m_rh);
     if (m_vps != NULL)
         pairfree(&m_vps);
 }
 
 bool
-gss_eap_radius_attr_provider::allocRadHandle(const std::string &configFile)
-{
-    m_configFile.assign(configFile);
-
-    /*
-     * Currently none of the FreeRADIUS functions we use here actually take
-     * a handle, so we may as well leave it as NULL.
-     */
-#if 0
-    radiusAllocHandle(m_configFile.c_str(), &m_rh);
-
-    return (m_rh != NULL);
-#else
-    return true;
-#endif
-}
-
-bool
 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
                                                       const gss_eap_attr_provider *ctx)
 {
@@ -118,11 +75,10 @@ gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *ma
 
     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
 
-    if (!allocRadHandle(radius->m_configFile))
-        return false;
-
     if (radius->m_vps != NULL)
-        m_vps = paircopy(const_cast<VALUE_PAIR *>(radius->getAvps()));
+        m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
+
+    m_authenticated = radius->m_authenticated;
 
     return true;
 }
@@ -132,22 +88,18 @@ gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager
                                                  const gss_cred_id_t gssCred,
                                                  const gss_ctx_id_t gssCtx)
 {
-    std::string configFile(RS_CONFIG_FILE);
-
     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
         return false;
 
-    if (gssCred != GSS_C_NO_CREDENTIAL && gssCred->radiusConfigFile != NULL)
-        configFile.assign(gssCred->radiusConfigFile);
-
-    if (!allocRadHandle(configFile))
-        return false;
-
     if (gssCtx != GSS_C_NO_CONTEXT) {
         if (gssCtx->acceptorCtx.vps != NULL) {
-            m_vps = paircopy(gssCtx->acceptorCtx.vps);
+            m_vps = copyAvps(gssCtx->acceptorCtx.vps);
             if (m_vps == NULL)
                 return false;
+
+            /* We assume libradsec validated this for us */
+            assert(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
+            m_authenticated = true;
         }
     }
 
@@ -168,32 +120,90 @@ alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
 }
 
 static bool
-isHiddenAttributeP(int attrid, uint16_t vendor)
+isSecretAttributeP(uint16_t attrid, uint16_t vendor)
 {
-    bool ret = false;
+    bool bSecretAttribute = false;
 
     switch (vendor) {
     case VENDORPEC_MS:
         switch (attrid) {
         case PW_MS_MPPE_SEND_KEY:
         case PW_MS_MPPE_RECV_KEY:
-            ret = true;
+            bSecretAttribute = true;
             break;
         default:
             break;
         }
+    default:
+        break;
+    }
+
+    return bSecretAttribute;
+}
+
+static bool
+isSecretAttributeP(uint32_t attribute)
+{
+    return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
+}
+
+static bool
+isInternalAttributeP(uint16_t attrid, uint16_t vendor)
+{
+    bool bInternalAttribute = false;
+
+    /* should have been filtered */
+    assert(!isSecretAttributeP(attrid, vendor));
+
+    switch (vendor) {
     case VENDORPEC_UKERNA:
-        ret = true;
+        bInternalAttribute = true;
         break;
     default:
         break;
     }
 
-    return ret;
+    return bInternalAttribute;
+}
+
+static bool
+isInternalAttributeP(uint32_t attribute)
+{
+    return isInternalAttributeP(ATTRID(attribute), VENDOR(attribute));
+}
+
+/*
+ * Copy AVP list, same as paircopy except it filters out attributes
+ * containing keys.
+ */
+static VALUE_PAIR *
+copyAvps(const VALUE_PAIR *src)
+{
+    const VALUE_PAIR *vp;
+    VALUE_PAIR *dst = NULL, **pDst = &dst;
+
+    for (vp = src; vp != NULL; vp = vp->next) {
+        VALUE_PAIR *vpcopy;
+
+        if (isSecretAttributeP(vp->attribute))
+            continue;
+
+        vpcopy = paircopyvp(vp);
+        if (vpcopy == NULL) {
+            pairfree(&dst);
+            throw new std::bad_alloc;
+            return NULL;
+        }
+        *pDst = vpcopy;
+        pDst = &vpcopy->next;
+     }
+
+    return dst;
 }
 
 bool
-gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
+gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
+                                                void *data) const
 {
     VALUE_PAIR *vp;
     std::vector <std::string> seen;
@@ -201,7 +211,9 @@ gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addA
     for (vp = m_vps; vp != NULL; vp = vp->next) {
         gss_buffer_desc attribute;
         char attrid[64];
-        if (isHiddenAttributeP(ATTRID(vp->attribute), VENDOR(vp->attribute)))
+
+        /* Don't advertise attributes that are internal to the GSS-EAP mechanism */
+        if (isInternalAttributeP(vp->attribute))
             continue;
 
         if (alreadyAddedAttributeP(seen, vp))
@@ -222,16 +234,89 @@ gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addA
     return true;
 }
 
-void
+uint32_t
+getAttributeId(const gss_buffer_t attr)
+{
+    OM_uint32 tmpMinor;
+    gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
+    DICT_ATTR *da;
+    char *s;
+    uint32_t attrid = 0;
+
+    if (attr->length < radiusUrnPrefix.length ||
+        memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
+        return 0;
+
+    /* need to duplicate because attr may not be NUL terminated */
+    duplicateBuffer(*attr, &strAttr);
+    s = (char *)strAttr.value + radiusUrnPrefix.length;
+
+    if (isdigit(*s)) {
+        attrid = strtoul(s, NULL, 10);
+    } else {
+        da = dict_attrbyname(s);
+        if (da != NULL)
+            attrid = da->attr;
+    }
+
+    gss_release_buffer(&tmpMinor, &strAttr);
+
+    return attrid;
+}
+
+bool
+gss_eap_radius_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
+                                           uint32_t attrid,
+                                           const gss_buffer_t value)
+{
+    OM_uint32 major = GSS_S_UNAVAILABLE, minor;
+
+    if (!isSecretAttributeP(attrid) &&
+        !isInternalAttributeP(attrid)) {
+        deleteAttribute(attrid);
+
+        major = gssEapRadiusAddAvp(&minor, &m_vps,
+                                   ATTRID(attrid), VENDOR(attrid), 
+                                   value);
+    }
+
+    return !GSS_ERROR(major);
+}
+
+bool
 gss_eap_radius_attr_provider::setAttribute(int complete,
                                            const gss_buffer_t attr,
                                            const gss_buffer_t value)
 {
+    uint32_t attrid = getAttributeId(attr);
+
+    if (!attrid)
+        return false;
+
+    return setAttribute(complete, attrid, value);
 }
 
-void
-gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
+bool
+gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
 {
+    if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid) ||
+        pairfind(m_vps, attrid) == NULL)
+        return false;
+
+    pairdelete(&m_vps, attrid);
+
+    return true;
+}
+
+bool
+gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
+{
+    uint32_t attrid = getAttributeId(attr);
+
+    if (!attrid)
+        return false;
+
+    return deleteAttribute(attrid);
 }
 
 bool
@@ -242,56 +327,29 @@ gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
                                            gss_buffer_t display_value,
                                            int *more) const
 {
-    OM_uint32 tmpMinor;
-    gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
-    DICT_ATTR *da;
-    int attrid;
-    char *s;
-
-    duplicateBuffer(*attr, &strAttr);
-    s = (char *)strAttr.value;
+    uint32_t attrid;
 
-    if (attr->length < radiusUrnPrefix.length ||
-        memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
+    attrid = getAttributeId(attr);
+    if (!attrid)
         return false;
 
-    s += radiusUrnPrefix.length;
-
-    if (isdigit(*s)) {
-        attrid = strtoul(s, NULL, 10);
-    } else {
-        da = dict_attrbyname(s);
-        if (da == NULL) {
-            gss_release_buffer(&tmpMinor, &strAttr);
-            return false;
-        }
-        attrid = da->attr;
-    }
-
-    gss_release_buffer(&tmpMinor, &strAttr);
-
     return getAttribute(attrid, authenticated, complete,
                         value, display_value, more);
 }
 
 bool
-gss_eap_radius_attr_provider::getAttribute(uint16_t vattrid,
-                                           uint16_t vendor,
+gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
                                            int *authenticated,
                                            int *complete,
                                            gss_buffer_t value,
                                            gss_buffer_t display_value,
                                            int *more) const
 {
-    uint32_t attrid = VENDORATTR(vendor, vattrid);
     VALUE_PAIR *vp;
     int i = *more, count = 0;
 
     *more = 0;
 
-    if (isHiddenAttributeP(attrid, vendor))
-        return false;
-
     if (i == -1)
         i = 0;
 
@@ -356,7 +414,8 @@ gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
 }
 
 bool
-gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
+gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
+                                           uint16_t vendor,
                                            int *authenticated,
                                            int *complete,
                                            gss_buffer_t value,
@@ -364,34 +423,60 @@ gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
                                            int *more) const
 {
 
-    return getAttribute(ATTRID(attrid), VENDOR(attrid),
+    return getAttribute(VENDORATTR(attribute, vendor),
                         authenticated, complete,
                         value, display_value, more);
 }
 
 gss_any_t
 gss_eap_radius_attr_provider::mapToAny(int authenticated,
-                                       gss_buffer_t type_id) const
+                                       gss_buffer_t type_id GSSEAP_UNUSED) const
 {
     if (authenticated && !m_authenticated)
         return (gss_any_t)NULL;
 
-    return (gss_any_t)paircopy(m_vps);
+    return (gss_any_t)copyAvps(m_vps);
 }
 
 void
-gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
+gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
                                                     gss_any_t input) const
 {
-    pairfree((VALUE_PAIR **)&input);
+    VALUE_PAIR *vp = (VALUE_PAIR *)input;
+    pairfree(&vp);
 }
 
 bool
 gss_eap_radius_attr_provider::init(void)
 {
+    struct rs_context *radContext;
+
     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
                                        "urn:ietf:params:gss-eap:radius-avp",
-                                       gss_eap_radius_attr_provider::createAttrContext);
+                                       createAttrContext);
+
+#if 1
+    /*
+     * This hack is necessary in order to force the loading of the global
+     * dictionary, otherwise accepting reauthentication tokens fails unless
+     * the acceptor has already accepted a normal authentication token.
+     */
+    if (rs_context_create(&radContext) != 0)
+        return false;
+
+    if (rs_context_read_config(radContext, RS_CONFIG_FILE) != 0) {
+        rs_context_destroy(radContext);
+        return false;
+    }
+
+    if (rs_context_init_freeradius_dict(radContext, NULL)) {
+        rs_context_destroy(radContext);
+        return false;
+    }
+
+    rs_context_destroy(radContext);
+#endif
+
     return true;
 }
 
@@ -409,13 +494,12 @@ gss_eap_radius_attr_provider::createAttrContext(void)
 
 OM_uint32
 gssEapRadiusAddAvp(OM_uint32 *minor,
-                   rs_handle *rh,
                    VALUE_PAIR **vps,
-                   uint16_t vattrid,
+                   uint16_t attribute,
                    uint16_t vendor,
-                   gss_buffer_t buffer)
+                   const gss_buffer_t buffer)
 {
-    uint16_t attrid = VENDORATTR(vendor, vattrid);
+    uint32_t attrid = VENDORATTR(vendor, attribute);
     unsigned char *p = (unsigned char *)buffer->value;
     size_t remain = buffer->length;
 
@@ -423,8 +507,12 @@ gssEapRadiusAddAvp(OM_uint32 *minor,
         VALUE_PAIR *vp;
         size_t n = remain;
 
-        if (n > MAX_STRING_LEN)
-            n = MAX_STRING_LEN;
+       /*
+         * There's an extra byte of padding; RADIUS AVPs can only
+         * be 253 octets.
+         */
+        if (n >= MAX_STRING_LEN)
+            n = MAX_STRING_LEN - 1;
 
         vp = paircreate(attrid, PW_TYPE_OCTETS);
         if (vp == NULL) {
@@ -433,6 +521,8 @@ gssEapRadiusAddAvp(OM_uint32 *minor,
         }
 
         memcpy(vp->vp_octets, p, n);
+        vp->length = n;
+
         pairadd(vps, vp);
 
         p += n;
@@ -445,35 +535,41 @@ gssEapRadiusAddAvp(OM_uint32 *minor,
 OM_uint32
 gssEapRadiusGetRawAvp(OM_uint32 *minor,
                       VALUE_PAIR *vps,
-                      uint16_t type,
+                      uint16_t attribute,
                       uint16_t vendor,
                       VALUE_PAIR **vp)
 {
-    uint16_t attr = VENDORATTR(vendor, type);
+    uint32_t attr = VENDORATTR(vendor, attribute);
 
     *vp = pairfind(vps, attr);
+    if (*vp == NULL) {
+        *minor = GSSEAP_NO_SUCH_ATTR;
+        return GSS_S_UNAVAILABLE;
+    }
 
-    return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE;
+    return GSS_S_COMPLETE;
 }
 
 OM_uint32
 gssEapRadiusGetAvp(OM_uint32 *minor,
                    VALUE_PAIR *vps,
-                   uint16_t type,
+                   uint16_t attribute,
                    uint16_t vendor,
                    gss_buffer_t buffer,
                    int concat)
 {
     VALUE_PAIR *vp;
     unsigned char *p;
-    uint32_t attr = VENDORATTR(vendor, type);
+    uint32_t attr = VENDORATTR(vendor, attribute);
 
     buffer->length = 0;
     buffer->value = NULL;
 
     vp = pairfind(vps, attr);
-    if (vp == NULL)
+    if (vp == NULL) {
+        *minor = GSSEAP_NO_SUCH_ATTR;
         return GSS_S_UNAVAILABLE;
+    }
 
     do {
         buffer->length += vp->length;
@@ -499,279 +595,179 @@ gssEapRadiusGetAvp(OM_uint32 *minor,
 }
 
 OM_uint32
-gssEapRadiusAttrProviderInit(OM_uint32 *minor)
+gssEapRadiusFreeAvps(OM_uint32 *minor,
+                     VALUE_PAIR **vps)
 {
-    return gss_eap_radius_attr_provider::init()
-        ? GSS_S_COMPLETE : GSS_S_FAILURE;
-}
-
-OM_uint32
-gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
-{
-    gss_eap_radius_attr_provider::finalize();
+    pairfree(vps);
+    *minor = 0;
     return GSS_S_COMPLETE;
 }
 
 OM_uint32
-gssEapRadiusMapError(OM_uint32 *minor,
-                     struct rs_error *err)
+gssEapRadiusAttrProviderInit(OM_uint32 *minor)
 {
-    if (err != NULL)
-        rs_err_code(err, 1);
+    if (!gss_eap_radius_attr_provider::init()) {
+        *minor = GSSEAP_RADSEC_INIT_FAILURE;
+        return GSS_S_FAILURE;
+    }
 
-    return GSS_S_FAILURE;
+    return GSS_S_COMPLETE;
 }
 
 OM_uint32
-gssEapRadiusAllocConn(OM_uint32 *minor,
-                      const gss_cred_id_t cred,
-                      gss_ctx_id_t ctx)
+gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
 {
-    struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx;
-    const char *configFile = NULL;
-    struct rs_error *err;
-
-    assert(actx->radHandle == NULL);
-    assert(actx->radConn == NULL);
-
-    if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
-        configFile = cred->radiusConfigFile;
-
-    err = radiusAllocHandle(configFile, &actx->radHandle);
-    if (err != NULL || actx->radHandle == NULL) {
-        return gssEapRadiusMapError(minor, err);
-    }
-
-    if (rs_conn_create(actx->radHandle, &actx->radConn, "gss-eap") != 0) {
-        return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
-    }
-
-    /* XXX TODO rs_conn_select_server does not exist yet */
-#if 0
-    if (actx->radServer != NULL) {
-        if (rs_conn_select_server(actx->radConn, actx->radServer) != 0)
-            return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
-    }
-#endif
+    gss_eap_radius_attr_provider::finalize();
 
     *minor = 0;
     return GSS_S_COMPLETE;
 }
 
-/*
- * Encoding is:
- * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
- */
-static size_t
-avpSize(const VALUE_PAIR *vp)
+static DDF
+avpMarshall(const VALUE_PAIR *vp)
 {
-    size_t size = 4 + 1;
+    DDF obj(NULL);
 
-    if (vp != NULL)
-        size += vp->length;
+    obj.addmember("type").integer(vp->attribute);
 
-    return size;
-}
-
-static bool
-avpExport(rs_handle *rh,
-          const VALUE_PAIR *vp,
-          unsigned char **pBuffer,
-          size_t *pRemain)
-{
-    unsigned char *p = *pBuffer;
-    size_t remain = *pRemain;
-
-    assert(remain >= avpSize(vp));
-
-    store_uint32_be(vp->attribute, p);
+    assert(vp->length <= MAX_STRING_LEN);
 
     switch (vp->type) {
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
-        p[4] = 4;
-        store_uint32_be(vp->lvalue, p + 5);
+        obj.addmember("value").integer(vp->lvalue);
         break;
-    default:
-        assert(vp->length <= MAX_STRING_LEN);
-        p[4] = (uint8_t)vp->length;
-        memcpy(p + 5, vp->vp_octets, vp->length);
+    case PW_TYPE_STRING:
+        obj.addmember("value").string(vp->vp_strvalue);
         break;
-    }
+    default: {
+        XMLSize_t len;
+        XMLByte *b64 = xercesc::Base64::encode(vp->vp_octets, vp->length, &len);
 
-    *pBuffer += 5 + p[4];
-    *pRemain -= 5 + p[4];
+        if (b64[len - 1] == '\n')
+            b64[--len] = '\0'; /* XXX there may be embedded newlines */
 
-    return true;
+        obj.addmember("value").string((char *)b64);
+        delete b64;
+        break;
+    }
+    }
 
+    return obj;
 }
 
 static bool
-avpImport(rs_handle *rh,
-          VALUE_PAIR **pVp,
-          unsigned char **pBuffer,
-          size_t *pRemain)
+avpUnmarshall(VALUE_PAIR **pVp, DDF &obj)
 {
-    unsigned char *p = *pBuffer;
-    size_t remain = *pRemain;
     VALUE_PAIR *vp = NULL;
     DICT_ATTR *da;
-    OM_uint32 attrid;
-
-    if (remain < avpSize(NULL))
-        goto fail;
+    uint32_t attrid;
 
-    attrid = load_uint32_be(p);
-    p += 4;
-    remain -= 4;
+    attrid = obj["type"].integer();
 
     da = dict_attrbyvalue(attrid);
-    if (da == NULL)
-        goto fail;
-
-    vp = pairalloc(da);
+    if (da != NULL) {
+        vp = pairalloc(da);
+    } else {
+        vp = paircreate(attrid, PW_TYPE_STRING);
+    }
     if (vp == NULL) {
         throw new std::bad_alloc;
         goto fail;
     }
 
-    if (remain < p[0])
-        goto fail;
-
     switch (vp->type) {
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
-        if (p[0] != 4)
-            goto fail;
-
         vp->length = 4;
-        vp->lvalue = load_uint32_be(p + 1);
-        p += 5;
-        remain -= 5;
+        vp->lvalue = obj["value"].integer();;
         break;
-    case PW_TYPE_STRING:
-        /* check enough room to NUL terminate */
-        if (p[0] >= MAX_STRING_LEN)
+    case PW_TYPE_STRING: {
+        const char *str = obj["value"].string();
+        size_t len = strlen(str);
+        if (str == NULL || len >= MAX_STRING_LEN)
             goto fail;
-        /* fallthrough */
-    default:
-        vp->length = (uint32_t)p[0];
-        memcpy(vp->vp_octets, p + 1, vp->length);
 
-        if (vp->type == PW_TYPE_STRING)
-            vp->vp_strvalue[vp->length] = '\0';
+        vp->length = len;
+        memcpy(vp->vp_strvalue, str, len + 1);
+        break;
+    }
+    case PW_TYPE_OCTETS:
+    default: {
+        XMLSize_t len;
+        const XMLByte *b64 = (const XMLByte *)obj["value"].string();
+        XMLByte *data = xercesc::Base64::decode(b64, &len);
+        if (data == NULL || len >= MAX_STRING_LEN) {
+            delete data;
+            goto fail;
+        }
 
-        p += 1 + vp->length;
-        remain -= 1 + vp->length;
+        vp->length = len;
+        memcpy(vp->vp_octets, data, len);
+        vp->vp_octets[len] = '\0';
+        delete data;
         break;
     }
+    }
 
     *pVp = vp;
-    *pBuffer = p;
-    *pRemain = remain;
 
     return true;
 
 fail:
-    pairbasicfree(vp);
+    if (vp != NULL)
+        pairbasicfree(vp);
+    *pVp = NULL;
     return false;
 }
 
+const char *
+gss_eap_radius_attr_provider::marshallingKey(void) const
+{
+    return "radius";
+}
+
 bool
-gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
-                                             const gss_buffer_t buffer)
+gss_eap_radius_attr_provider::unmarshallAndInit(const gss_eap_attr_ctx *ctx,
+                                                DDF &obj)
 {
-    unsigned char *p = (unsigned char *)buffer->value;
-    size_t remain = buffer->length;
-    OM_uint32 configFileLen, count;
     VALUE_PAIR **pNext = &m_vps;
 
-    if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
-        return false;
-
-    if (remain < 4)
-        return false;
-
-    configFileLen = load_uint32_be(p);
-    p += 4;
-    remain -= 4;
-
-    if (remain < configFileLen)
+    if (!gss_eap_attr_provider::unmarshallAndInit(ctx, obj))
         return false;
 
-    std::string configFile((char *)p, configFileLen);
-    p += configFileLen;
-    remain -= configFileLen;
+    DDF attrs = obj["attributes"];
+    DDF attr = attrs.first();
 
-    if (!allocRadHandle(configFile))
-        return false;
-
-    if (remain < 4)
-        return false;
-
-    count = load_uint32_be(p);
-    p += 4;
-    remain -= 4;
-
-    do {
-        VALUE_PAIR *attr;
+    while (!attr.isnull()) {
+        VALUE_PAIR *vp;
 
-        if (!avpImport(m_rh, &attr, &p, &remain))
+        if (!avpUnmarshall(&vp, attr))
             return false;
 
-        *pNext = attr;
-        pNext = &attr->next;
-
-        count--;
-    } while (remain != 0);
+        *pNext = vp;
+        pNext = &vp->next;
 
-    if (count != 0)
-        return false;
+        attr = attrs.next();
+    }
 
     return true;
 }
 
-void
-gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
+DDF
+gss_eap_radius_attr_provider::marshall(void) const
 {
-    OM_uint32 count = 0;
-    VALUE_PAIR *vp;
-    unsigned char *p;
-    size_t remain = 4 + m_configFile.length() + 4;
+    DDF obj(NULL);
+    DDF attrs = obj.structure().addmember("attributes").list();
 
-    for (vp = m_vps; vp != NULL; vp = vp->next) {
-        remain += avpSize(vp);
-        count++;
+    for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) {
+        DDF attr = avpMarshall(vp);
+        attrs.add(attr);
     }
 
-    buffer->value = GSSEAP_MALLOC(remain);
-    if (buffer->value == NULL) {
-        throw new std::bad_alloc;
-        return;
-    }
-    buffer->length = remain;
-
-    p = (unsigned char *)buffer->value;
-
-    store_uint32_be(m_configFile.length(), p);
-    p += 4;
-    remain -= 4;
-
-    memcpy(p, m_configFile.c_str(), m_configFile.length());
-    p += m_configFile.length();
-    remain -= m_configFile.length();
-
-    store_uint32_be(count, p);
-    p += 4;
-    remain -= 4;
-
-    for (vp = m_vps; vp != NULL; vp = vp->next) {
-        avpExport(m_rh, vp, &p, &remain);
-    }
-
-    assert(remain == 0);
+    return obj;
 }
 
 time_t
@@ -785,3 +781,26 @@ gss_eap_radius_attr_provider::getExpiryTime(void) const
 
     return time(NULL) + vp->lvalue;
 }
+
+OM_uint32
+gssEapRadiusMapError(OM_uint32 *minor,
+                     struct rs_error *err)
+{
+    int code;
+
+    assert(err != NULL);
+
+    code = rs_err_code(err, 0);
+
+    if (code == RSE_OK) {
+        *minor = 0;
+        return GSS_S_COMPLETE;
+    }
+
+    *minor = ERROR_TABLE_BASE_rse + code;
+
+    gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err));
+    rs_err_free(err);
+
+    return GSS_S_FAILURE;
+}