X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=util_radius.cpp;h=4e2f6e079724311dd6304f9ca620f8050cac262e;hb=e063ba4e45d12dbc1a397653f9e77228835e4a2b;hp=dfd8964d4a9bc1df4bc8792801d6570b13515e84;hpb=595525407cf72f50e5289af8bb568693f37145bd;p=mech_eap.git diff --git a/util_radius.cpp b/util_radius.cpp index dfd8964..4e2f6e0 100644 --- a/util_radius.cpp +++ b/util_radius.cpp @@ -223,7 +223,7 @@ gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addA attribute.value = attrid; attribute.length = strlen(attrid); - if (!addAttribute(this, &attribute, data)) + if (!addAttribute(m_manager, this, &attribute, data)) return false; seen.push_back(std::string(vp->name)); @@ -449,9 +449,7 @@ 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", - createAttrContext); + gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS, createAttrContext); #if 1 /* @@ -505,7 +503,7 @@ gssEapRadiusAddAvp(OM_uint32 *minor, VALUE_PAIR *vp; size_t n = remain; - /* + /* * There's an extra byte of padding; RADIUS AVPs can only * be 253 octets. */ @@ -621,72 +619,61 @@ gssEapRadiusAttrProviderFinalize(OM_uint32 *minor) 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 json_t * +avpToJson(const VALUE_PAIR *vp) { - size_t size = 4 + 1; - - if (vp != NULL) - size += vp->length; + json_t *obj = json_object(); - return size; -} - -static bool -avpExport(const VALUE_PAIR *vp, - unsigned char **pBuffer, - size_t *pRemain) -{ - unsigned char *p = *pBuffer; - size_t remain = *pRemain; + if (obj == NULL) { + throw new std::bad_alloc; + return NULL; + } - assert(remain >= avpSize(vp)); + /* FIXME check json_object_set_new return value */ + json_object_set_new(obj, "type", json_integer(vp->attribute)); - 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); + json_object_set_new(obj, "value", json_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: + json_object_set_new(obj, "value", json_string(vp->vp_strvalue)); break; - } + default: { + char *b64; - *pBuffer += 5 + p[4]; - *pRemain -= 5 + p[4]; + if (base64Encode(vp->vp_octets, vp->length, &b64) < 0) { + json_decref(obj); + throw new std::bad_alloc; + } - return true; + json_object_set_new(obj, "value", json_string(b64)); + GSSEAP_FREE(b64); + break; + } + } + return obj; } static bool -avpImport(VALUE_PAIR **pVp, - unsigned char **pBuffer, - size_t *pRemain) +jsonToAvp(VALUE_PAIR **pVp, json_t *obj) { - unsigned char *p = *pBuffer; - size_t remain = *pRemain; VALUE_PAIR *vp = NULL; DICT_ATTR *da; uint32_t attrid; + json_t *type, *value; - if (remain < avpSize(NULL)) + type = json_object_get(obj, "type"); + value = json_object_get(obj, "value"); + if (type == NULL || value == NULL) goto fail; - attrid = load_uint32_be(p); - p += 4; - remain -= 4; - + attrid = json_integer_value(type); da = dict_attrbyvalue(attrid); if (da != NULL) { vp = pairalloc(da); @@ -698,40 +685,45 @@ avpImport(VALUE_PAIR **pVp, 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 = json_integer_value(value); break; - case PW_TYPE_STRING: - default: - if (p[0] >= MAX_STRING_LEN) + case PW_TYPE_STRING: { + const char *str = json_string_value(value); + size_t len; + + if (str == NULL || (len = strlen(str)) >= MAX_STRING_LEN) goto fail; - vp->length = (uint32_t)p[0]; - memcpy(vp->vp_octets, p + 1, vp->length); + vp->length = len; + memcpy(vp->vp_strvalue, str, len + 1); + break; + } + case PW_TYPE_OCTETS: + default: { + const char *str = json_string_value(value); + int len; + + /* this optimization requires base64Decode only understand packed encoding */ + if (str == NULL || + strlen(str) >= BASE64_EXPAND(MAX_STRING_LEN)) + goto fail; - if (vp->type == PW_TYPE_STRING) - vp->vp_strvalue[vp->length] = '\0'; + len = base64Decode(str, vp->vp_octets); + if (len < 0) + goto fail; - p += 1 + vp->length; - remain -= 1 + vp->length; + vp->length = len; + vp->vp_octets[len] = '\0'; break; } + } *pVp = vp; - *pBuffer = p; - *pRemain = remain; return true; @@ -742,55 +734,68 @@ fail: return false; } +const char * +gss_eap_radius_attr_provider::name(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::initWithJsonObject(const gss_eap_attr_ctx *ctx, + json_t *obj) { - unsigned char *p = (unsigned char *)buffer->value; - size_t remain = buffer->length; VALUE_PAIR **pNext = &m_vps; + json_t *attrs; + size_t i; - if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer)) + if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj)) return false; - do { - VALUE_PAIR *attr; + attrs = json_object_get(obj, "attributes"); - if (!avpImport(&attr, &p, &remain)) + for (i = 0; i < json_array_size(attrs); i++) { + json_t *attr = json_array_get(attrs, i); + VALUE_PAIR *vp; + + if (!jsonToAvp(&vp, attr)) return false; - *pNext = attr; - pNext = &attr->next; - } while (remain != 0); + *pNext = vp; + pNext = &vp->next; + } return true; } -void -gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const +const char * +gss_eap_radius_attr_provider::prefix(void) const { - VALUE_PAIR *vp; - unsigned char *p; - size_t remain = 0; + return "urn:ietf:params:gss-eap:radius-avp"; +} - for (vp = m_vps; vp != NULL; vp = vp->next) { - remain += avpSize(vp); - } +json_t * +gss_eap_radius_attr_provider::jsonRepresentation(void) const +{ + json_t *obj, *attrs; - buffer->value = GSSEAP_MALLOC(remain); - if (buffer->value == NULL) { + attrs = json_array(); + if (attrs == NULL) throw new std::bad_alloc; - return; - } - buffer->length = remain; - p = (unsigned char *)buffer->value; + for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) { + json_t *attr = avpToJson(vp); + json_array_append_new(attrs, attr); + } - for (vp = m_vps; vp != NULL; vp = vp->next) { - avpExport(vp, &p, &remain); + obj = json_object(); + if (obj == NULL) { + json_decref(attrs); + throw new std::bad_alloc; } - assert(remain == 0); + json_object_set_new(obj, "attributes", attrs); + + return obj; } time_t