From: Luke Howard Date: Sat, 26 Mar 2011 15:16:40 +0000 (+1100) Subject: in progress use DDF to serialise names X-Git-Tag: dvd/201105~12^2~55 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=mech_eap.orig;a=commitdiff_plain;h=a8ec8ba7d129e544045ab73819a9213eac4e4275 in progress use DDF to serialise names get DDF marshalling working remove debugging statement --- diff --git a/util_attr.cpp b/util_attr.cpp index 733662f..80d2781 100644 --- a/util_attr.cpp +++ b/util_attr.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include @@ -273,86 +274,67 @@ gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred, return ret; } -#define UPDATE_REMAIN(n) do { \ - p += (n); \ - remain -= (n); \ - } while (0) +static DDF +findSourceForProvider(DDF &sources, const char *key) +{ + DDF source = sources.first(); -#define CHECK_REMAIN(n) do { \ - if (remain < (n)) { \ - return false; \ - } \ - } while (0) + while (!source.isnull()) { + DDF obj = source.getmember(key); + + if (strcmp(key, source.name()) == 0) + break; + + source = sources.next(); + } + + return source; +} -/* - * Initialize a context from an exported context or name token - */ bool -gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer) +gss_eap_attr_ctx::unmarshallAndInit(DDF &obj) { bool ret = false; - size_t remain = buffer->length; - unsigned char *p = (unsigned char *)buffer->value; - bool didInit[ATTR_TYPE_MAX + 1]; + bool foundSource[ATTR_TYPE_MAX + 1]; unsigned int type; for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) - didInit[type] = false; - - /* flags */ - CHECK_REMAIN(4); - m_flags = load_uint32_be(p); - UPDATE_REMAIN(4); - - while (remain) { - OM_uint32 type; - gss_buffer_desc providerToken; - gss_eap_attr_provider *provider; - - /* TLV encoding of provider type, length, value */ - CHECK_REMAIN(4); - type = load_uint32_be(p); - UPDATE_REMAIN(4); + foundSource[type] = false; - CHECK_REMAIN(4); - providerToken.length = load_uint32_be(p); - UPDATE_REMAIN(4); + if (obj["version"].integer() != 1) + return false; - CHECK_REMAIN(providerToken.length); - providerToken.value = p; - UPDATE_REMAIN(providerToken.length); + m_flags = obj["flags"].integer(); - if (type < ATTR_TYPE_MIN || type > ATTR_TYPE_MAX || - didInit[type]) - return false; + DDF sources = obj["sources"]; + /* Initialize providers from serialized state */ + for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) { if (!providerEnabled(type)) { releaseProvider(type); continue; } - provider = m_providers[type]; + gss_eap_attr_provider *provider = m_providers[type]; + const char *key = provider->marshallingKey(); + if (key == NULL) + continue; - ret = provider->initFromBuffer(this, &providerToken); - if (ret == false) { + DDF source = findSourceForProvider(sources, key); + if (source.isnull() || + !provider->unmarshallAndInit(this, source)) { releaseProvider(type); - break; + return false; } - didInit[type] = true; - } - if (ret == false) - return ret; + foundSource[type] = true; + } - /* - * The call the initFromGssContext methods for attribute - * providers that can initialize themselves from other - * providers. - */ + /* Initialize remaining providers from initialized providers */ for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) { gss_eap_attr_provider *provider; - if (didInit[type] || !providerEnabled(type)) + if (foundSource[type] || !providerEnabled(type)) continue; provider = m_providers[type]; @@ -369,6 +351,58 @@ gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer) return true; } +DDF +gss_eap_attr_ctx::marshall(void) const +{ + DDF obj(NULL); + unsigned int i; + + obj.addmember("version").integer(1); + obj.addmember("flags").integer(m_flags); + + DDF sources = obj.addmember("sources").list(); + + for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) { + gss_eap_attr_provider *provider = m_providers[i]; + + if (provider == NULL) + continue; /* provider not initialised */ + + const char *key = provider->marshallingKey(); + if (key == NULL) + continue; /* provider does not have state */ + + DDF source = provider->marshall(); + sources.add(source.name(key)); + } + + return obj; +} + +/* + * Initialize a context from an exported context or name token + */ +bool +gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer) +{ + bool ret; + + if (buffer->length == 0) + return false; + + DDF obj(NULL); + std::string str((const char *)buffer->value, buffer->length); + std::istringstream source(str); + + source >> obj; + + ret = unmarshallAndInit(obj); + + obj.destroy(); + + return ret; +} + gss_eap_attr_ctx::~gss_eap_attr_ctx(void) { for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) @@ -601,51 +635,15 @@ gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id, void gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const { - OM_uint32 tmpMinor; - gss_buffer_desc providerTokens[ATTR_TYPE_MAX + 1]; - size_t length = 4; /* m_flags */ - unsigned char *p; - unsigned int i; - - for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) { - providerTokens[i].length = 0; - providerTokens[i].value = NULL; - } - - for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) { - gss_eap_attr_provider *provider = m_providers[i]; + DDF obj = marshall(); + std::ostringstream sink; - if (provider == NULL) - continue; + sink << obj; + std::string str = sink.str(); - provider->exportToBuffer(&providerTokens[i]); + duplicateBuffer(str, buffer); - if (providerTokens[i].value != NULL) - length += 8 + providerTokens[i].length; - } - - buffer->length = length; - buffer->value = GSSEAP_MALLOC(length); - if (buffer->value == NULL) - throw new std::bad_alloc; - - p = (unsigned char *)buffer->value; - store_uint32_be(m_flags, p); - p += 4; - - for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) { - if (providerTokens[i].value == NULL) - continue; - - store_uint32_be(i, p); - p += 4; - store_uint32_be(providerTokens[i].length, p); - p += 4; - memcpy(p, providerTokens[i].value, providerTokens[i].length); - p += providerTokens[i].length; - - gss_release_buffer(&tmpMinor, &providerTokens[i]); - } + obj.destroy(); } /* diff --git a/util_attr.h b/util_attr.h index 6af4cf3..347842a 100644 --- a/util_attr.h +++ b/util_attr.h @@ -41,6 +41,10 @@ #include #include +#include + +using namespace shibsp; + struct gss_eap_attr_provider; struct gss_eap_attr_ctx; @@ -121,6 +125,7 @@ public: { return NULL; } + virtual void releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED, gss_any_t input GSSEAP_UNUSED) const { @@ -131,16 +136,22 @@ public: return NULL; } - virtual void exportToBuffer(gss_buffer_t buffer GSSEAP_UNUSED) const + virtual const char *marshallingKey(void) const { + return NULL; } - virtual bool initFromBuffer(const gss_eap_attr_ctx *manager, - const gss_buffer_t buffer GSSEAP_UNUSED) + virtual bool unmarshallAndInit(const gss_eap_attr_ctx *manager, + DDF &object GSSEAP_UNUSED) { return initWithManager(manager); } + virtual DDF marshall(void) const + { + return DDF(NULL); + } + virtual time_t getExpiryTime(void) const { return 0; } virtual OM_uint32 mapException(OM_uint32 *minor GSSEAP_UNUSED, @@ -242,6 +253,9 @@ private: unsigned int attributePrefixToType(const gss_buffer_t prefix) const; gss_buffer_desc attributeTypeToPrefix(unsigned int type) const; + bool unmarshallAndInit(DDF &object); + DDF marshall(void) const; + gss_eap_attr_provider *getPrimaryProvider(void) const; /* make non-copyable */ diff --git a/util_radius.cpp b/util_radius.cpp index 934aa5f..a272759 100644 --- a/util_radius.cpp +++ b/util_radius.cpp @@ -36,6 +36,8 @@ #include "gssapiP_eap.h" +#include + /* stuff that should be provided by libradsec/libfreeradius-radius */ #define VENDORATTR(vendor, attr) (((vendor) << 16) | (attr)) @@ -619,71 +621,48 @@ 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) -{ - size_t size = 4 + 1; - - if (vp != NULL) - size += vp->length; - - return size; -} - -static bool -avpExport(const VALUE_PAIR *vp, - unsigned char **pBuffer, - size_t *pRemain) +static DDF +avpMarshall(const VALUE_PAIR *vp) { - unsigned char *p = *pBuffer; - size_t remain = *pRemain; + DDF obj(NULL); - assert(remain >= avpSize(vp)); + obj.addmember("type").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); + 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(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; uint32_t attrid; - if (remain < avpSize(NULL)) - goto fail; - - attrid = load_uint32_be(p); - p += 4; - remain -= 4; + attrid = obj["type"].integer(); da = dict_attrbyvalue(attrid); if (da != NULL) { @@ -696,40 +675,42 @@ 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 = obj["value"].integer();; break; - case PW_TYPE_STRING: - default: - 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; - 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; @@ -740,26 +721,35 @@ fail: 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; VALUE_PAIR **pNext = &m_vps; - if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer)) + if (!gss_eap_attr_provider::unmarshallAndInit(ctx, obj)) return false; - do { - VALUE_PAIR *attr; + DDF attrs = obj["attributes"]; + DDF attr = attrs.first(); - if (!avpImport(&attr, &p, &remain)) + while (!attr.isnull()) { + VALUE_PAIR *vp; + + if (!avpUnmarshall(&vp, attr)) return false; - *pNext = attr; - pNext = &attr->next; - } while (remain != 0); + *pNext = vp; + pNext = &vp->next; + + attr = attrs.next(); + } return true; } @@ -770,31 +760,18 @@ gss_eap_radius_attr_provider::prefix(void) const return "urn:ietf:params:gss-eap:radius-avp"; } -void -gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const +DDF +gss_eap_radius_attr_provider::marshall(void) const { - VALUE_PAIR *vp; - unsigned char *p; - size_t remain = 0; - - for (vp = m_vps; vp != NULL; vp = vp->next) { - remain += avpSize(vp); - } + DDF obj(NULL); + DDF attrs = obj.structure().addmember("attributes").list(); - buffer->value = GSSEAP_MALLOC(remain); - if (buffer->value == NULL) { - throw new std::bad_alloc; - return; - } - buffer->length = remain; - - p = (unsigned char *)buffer->value; - - for (vp = m_vps; vp != NULL; vp = vp->next) { - avpExport(vp, &p, &remain); + for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) { + DDF attr = avpMarshall(vp); + attrs.add(attr); } - assert(remain == 0); + return obj; } time_t diff --git a/util_radius.h b/util_radius.h index bc6165b..0335dc8 100644 --- a/util_radius.h +++ b/util_radius.h @@ -67,10 +67,11 @@ public: gss_any_t input) const; const char *prefix(void) const; + const char *marshallingKey(void) const; - void exportToBuffer(gss_buffer_t buffer) const; - bool initFromBuffer(const gss_eap_attr_ctx *ctx, - const gss_buffer_t buffer); + bool unmarshallAndInit(const gss_eap_attr_ctx *manager, + DDF &object GSSEAP_UNUSED); + DDF marshall(void) const; bool getAttribute(uint32_t attribute, int *authenticated, diff --git a/util_saml.cpp b/util_saml.cpp index 24d125d..1a2c81e 100644 --- a/util_saml.cpp +++ b/util_saml.cpp @@ -304,6 +304,8 @@ gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSE delete ((saml2::Assertion *)input); } +<<<<<<< HEAD +<<<<<<< HEAD const char * gss_eap_saml_assertion_provider::prefix(void) const { @@ -312,18 +314,23 @@ gss_eap_saml_assertion_provider::prefix(void) const void gss_eap_saml_assertion_provider::exportToBuffer(gss_buffer_t buffer) const +======= +DDF +gss_eap_saml_assertion_provider::marshall(void) const +>>>>>>> 1ef293a... in progress use DDF to serialise names { - buffer->length = 0; - buffer->value = NULL; + return DDF(NULL); } bool -gss_eap_saml_assertion_provider::initFromBuffer(const gss_eap_attr_ctx *ctx GSSEAP_UNUSED, - const gss_buffer_t buffer GSSEAP_UNUSED) +gss_eap_saml_assertion_provider::unmarshallAndInit(const gss_eap_attr_ctx *ctx GSSEAP_UNUSED, + DDF &object GSSEAP_UNUSED) { return false; } +======= +>>>>>>> eef7b3b... get DDF marshalling working bool gss_eap_saml_assertion_provider::init(void) { @@ -688,6 +695,8 @@ gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UN { } +<<<<<<< HEAD +<<<<<<< HEAD const char * gss_eap_saml_attr_provider::prefix(void) const { @@ -696,18 +705,23 @@ gss_eap_saml_attr_provider::prefix(void) const void gss_eap_saml_attr_provider::exportToBuffer(gss_buffer_t buffer) const +======= +DDF +gss_eap_saml_attr_provider::marshall(void) const +>>>>>>> 1ef293a... in progress use DDF to serialise names { - buffer->length = 0; - buffer->value = NULL; + return DDF(NULL); } bool -gss_eap_saml_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx GSSEAP_UNUSED, - const gss_buffer_t buffer GSSEAP_UNUSED) +gss_eap_saml_attr_provider::unmarshallAndInit(const gss_eap_attr_ctx *ctx GSSEAP_UNUSED, + DDF &object GSSEAP_UNUSED) { return false; } +======= +>>>>>>> eef7b3b... get DDF marshalling working bool gss_eap_saml_attr_provider::init(void) { diff --git a/util_saml.h b/util_saml.h index 17edb2b..351e70d 100644 --- a/util_saml.h +++ b/util_saml.h @@ -75,10 +75,16 @@ public: gss_any_t input) const; const char *prefix(void) const; - void exportToBuffer(gss_buffer_t buffer) const; - - bool initFromBuffer(const gss_eap_attr_ctx *ctx, - const gss_buffer_t buffer); + const char *marshallingKey(void) const { + return NULL; + } + bool unmarshallAndInit(const gss_eap_attr_ctx *manager GSSEAP_UNUSED, + DDF &object GSSEAP_UNUSED) { + return false; + } + DDF marshall(void) const { + return DDF(NULL); + } opensaml::saml2::Assertion *initAssertion(void); @@ -131,11 +137,30 @@ public: void releaseAnyNameMapping(gss_buffer_t type_id, gss_any_t input) const; +<<<<<<< HEAD +<<<<<<< HEAD const char *prefix(void) const; void exportToBuffer(gss_buffer_t buffer) const; bool initFromBuffer(const gss_eap_attr_ctx *ctx, const gss_buffer_t buffer); +======= + bool unmarshallAndInit(const gss_eap_attr_ctx *manager, + DDF &object GSSEAP_UNUSED); + DDF marshall(void) const; +>>>>>>> 1ef293a... in progress use DDF to serialise names +======= + const char *marshallingKey(void) const { + return NULL; + } + bool unmarshallAndInit(const gss_eap_attr_ctx *manager GSSEAP_UNUSED, + DDF &object GSSEAP_UNUSED) { + return false; + } + DDF marshall(void) const { + return DDF(NULL); + } +>>>>>>> eef7b3b... get DDF marshalling working bool getAttribute(const gss_buffer_t attr, int *authenticated, diff --git a/util_shib.cpp b/util_shib.cpp index 4da9a31..a53a476 100644 --- a/util_shib.cpp +++ b/util_shib.cpp @@ -385,61 +385,44 @@ gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UN } const char * +<<<<<<< HEAD gss_eap_shib_attr_provider::prefix(void) const { return NULL; +======= +gss_eap_shib_attr_provider::marshallingKey(void) const +{ + return "local"; +>>>>>>> eef7b3b... get DDF marshalling working } -void -gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const +DDF +gss_eap_shib_attr_provider::marshall(void) const { DDF obj(NULL); - DDF attrs(NULL); - - buffer->length = 0; - buffer->value = NULL; - obj.addmember("version").integer(1); obj.addmember("authenticated").integer(m_authenticated); - attrs = obj.addmember("attributes").list(); + DDF attrs = obj.addmember("attributes").list(); for (vector::const_iterator a = m_attributes.begin(); a != m_attributes.end(); ++a) { DDF attr = (*a)->marshall(); attrs.add(attr); } - ostringstream sink; - sink << obj; - string str = sink.str(); - - duplicateBuffer(str, buffer); - - attrs.destroy(); + return obj; } bool -gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx, - const gss_buffer_t buffer) +gss_eap_shib_attr_provider::unmarshallAndInit(const gss_eap_attr_ctx *ctx, + DDF &obj) { - if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer)) + if (!gss_eap_attr_provider::unmarshallAndInit(ctx, obj)) return false; - if (buffer->length == 0) - return true; - assert(m_authenticated == false); assert(m_attributes.size() == 0); - DDF obj(NULL); - string str((const char *)buffer->value, buffer->length); - istringstream source(str); - - source >> obj; - - if (obj["version"].integer() != 1) - return false; - m_authenticated = (obj["authenticated"].integer() != 0); DDF attrs = obj["attributes"]; @@ -450,8 +433,6 @@ gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx, attr = attrs.next(); } - attrs.destroy(); - return true; } diff --git a/util_shib.h b/util_shib.h index a6fe0ec..db931fb 100644 --- a/util_shib.h +++ b/util_shib.h @@ -78,9 +78,10 @@ public: const char *prefix(void) const; - void exportToBuffer(gss_buffer_t buffer) const; - bool initFromBuffer(const gss_eap_attr_ctx *ctx, - const gss_buffer_t buffer); + const char *marshallingKey(void) const; + bool unmarshallAndInit(const gss_eap_attr_ctx *manager, + DDF &object GSSEAP_UNUSED); + DDF marshall(void) const; static bool init(void); static void finalize(void);