}
static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX + 1];
-static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX + 1];
/*
* Register a provider for a particular type and prefix
*/
void
gss_eap_attr_ctx::registerProvider(unsigned int type,
- const char *prefix,
gss_eap_attr_create_provider factory)
{
assert(type <= ATTR_TYPE_MAX);
assert(gssEapAttrFactories[type] == NULL);
gssEapAttrFactories[type] = factory;
- if (prefix != NULL) {
- gssEapAttrPrefixes[type].value = (void *)prefix;
- gssEapAttrPrefixes[type].length = strlen(prefix);
- } else {
- gssEapAttrPrefixes[type].value = NULL;
- gssEapAttrPrefixes[type].length = 0;
- }
}
/*
assert(type <= ATTR_TYPE_MAX);
gssEapAttrFactories[type] = NULL;
- gssEapAttrPrefixes[type].value = NULL;
- gssEapAttrPrefixes[type].length = 0;
}
/*
* Convert an attribute prefix to a type
*/
unsigned int
-gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
+gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix) const
{
unsigned int i;
for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
- if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
+ const char *pprefix;
+
+ if (!providerEnabled(i))
+ continue;
+
+ pprefix = m_providers[i]->prefix();
+ if (pprefix == NULL)
+ continue;
+
+ if (strlen(pprefix) == prefix->length &&
+ memcmp(pprefix, prefix->value, prefix->length) == 0)
return i;
}
/*
* Convert a type to an attribute prefix
*/
-const gss_buffer_t
-gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
+gss_buffer_desc
+gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type) const
{
+ gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
+
if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_MAX)
- return GSS_C_NO_BUFFER;
+ return prefix;
+
+ if (!providerEnabled(type))
+ return prefix;
- return &gssEapAttrPrefixes[type];
+ prefix.value = (void *)m_providers[type]->prefix();
+ if (prefix.value != NULL)
+ prefix.length = strlen((char *)prefix.value);
+
+ return prefix;
}
bool
return ret;
}
+#define UPDATE_REMAIN(n) do { \
+ p += (n); \
+ remain -= (n); \
+ } while (0)
+
+#define CHECK_REMAIN(n) do { \
+ if (remain < (n)) { \
+ return false; \
+ } \
+ } while (0)
+
/*
* Initialize a context from an exported context or name token
*/
bool
gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
{
- bool ret;
- gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
- gss_buffer_desc primaryBuf;
+ bool ret = false;
+ size_t remain = buffer->length;
+ unsigned char *p = (unsigned char *)buffer->value;
+ bool didInit[ATTR_TYPE_MAX + 1];
+ unsigned int type;
- if (buffer->length < 4)
- return false;
+ 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;
- m_flags = load_uint32_be(buffer->value);
+ /* TLV encoding of provider type, length, value */
+ CHECK_REMAIN(4);
+ type = load_uint32_be(p);
+ UPDATE_REMAIN(4);
- primaryBuf.length = buffer->length - 4;
- primaryBuf.value = (char *)buffer->value + 4;
+ CHECK_REMAIN(4);
+ providerToken.length = load_uint32_be(p);
+ UPDATE_REMAIN(4);
+
+ CHECK_REMAIN(providerToken.length);
+ providerToken.value = p;
+ UPDATE_REMAIN(providerToken.length);
+
+ if (type < ATTR_TYPE_MIN || type > ATTR_TYPE_MAX ||
+ didInit[type])
+ return false;
+
+ if (!providerEnabled(type)) {
+ releaseProvider(type);
+ continue;
+ }
+
+ provider = m_providers[type];
+
+ ret = provider->initFromBuffer(this, &providerToken);
+ if (ret == false) {
+ releaseProvider(type);
+ break;
+ }
+ didInit[type] = true;
+ }
- ret = primaryProvider->initFromBuffer(this, &primaryBuf);
if (ret == false)
return ret;
- for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+ /*
+ * The call the initFromGssContext methods for attribute
+ * providers that can initialize themselves from other
+ * providers.
+ */
+ for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) {
gss_eap_attr_provider *provider;
- if (!providerEnabled(i)) {
- releaseProvider(i);
+ if (didInit[type] || !providerEnabled(type))
continue;
- }
- provider = m_providers[i];
- if (provider == primaryProvider)
- continue;
+ provider = m_providers[type];
ret = provider->initFromGssContext(this,
GSS_C_NO_CREDENTIAL,
GSS_C_NO_CONTEXT);
if (ret == false) {
- releaseProvider(i);
- break;
+ releaseProvider(type);
+ return false;
}
}
- return ret;
+ return true;
}
gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
}
/*
- * Locate provider for a given prefix
- */
-gss_eap_attr_provider *
-gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
-{
- unsigned int type;
-
- type = attributePrefixToType(prefix);
-
- return m_providers[type];
-}
-
-/*
* Get primary provider. Only the primary provider is serialised when
* gss_export_sec_context() or gss_export_name_composite() is called.
*/
};
static bool
-addAttribute(const gss_eap_attr_provider *provider GSSEAP_UNUSED,
+addAttribute(const gss_eap_attr_ctx *manager,
+ const gss_eap_attr_provider *provider GSSEAP_UNUSED,
const gss_buffer_t attribute,
void *data)
{
OM_uint32 major, minor;
if (args->type != ATTR_TYPE_LOCAL) {
- gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
+ manager->composeAttributeName(args->type, attribute, &qualified);
major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
gss_release_buffer(&minor, &qualified);
} else {
void
gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
{
- const gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
- gss_buffer_desc tmp;
- unsigned char *p;
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];
+
+ if (provider == NULL)
+ continue;
- primaryProvider->exportToBuffer(&tmp);
+ provider->exportToBuffer(&providerTokens[i]);
- buffer->length = 4 + tmp.length;
- buffer->value = GSSEAP_MALLOC(buffer->length);
+ 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);
- memcpy(p + 4, tmp.value, tmp.length);
+ p += 4;
+
+ for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+ if (providerTokens[i].value == NULL)
+ continue;
- gss_release_buffer(&tmpMinor, &tmp);
+ 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]);
+ }
}
/*
void
gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
unsigned int *type,
- gss_buffer_t suffix)
+ gss_buffer_t suffix) const
{
gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
gss_eap_attr_ctx::composeAttributeName(unsigned int type,
const gss_buffer_t suffix)
{
- const gss_buffer_t prefix = attributeTypeToPrefix(type);
+ gss_buffer_desc prefix = attributeTypeToPrefix(type);
- return composeAttributeName(prefix, suffix);
+ return composeAttributeName(&prefix, suffix);
}
/*
void
gss_eap_attr_ctx::composeAttributeName(unsigned int type,
const gss_buffer_t suffix,
- gss_buffer_t attribute)
+ gss_buffer_t attribute) const
{
- gss_buffer_t prefix = attributeTypeToPrefix(type);
+ gss_buffer_desc prefix = attributeTypeToPrefix(type);
- return composeAttributeName(prefix, suffix, attribute);
+ return composeAttributeName(&prefix, suffix, attribute);
}
/*
OM_uint32
gssEapInquireName(OM_uint32 *minor,
gss_name_t name,
- int *name_is_MN GSSEAP_UNUSED,
- gss_OID *MN_mech GSSEAP_UNUSED,
+ int *name_is_MN,
+ gss_OID *MN_mech,
gss_buffer_set_t *attrs)
{
+ OM_uint32 major;
+
+ if (name_is_MN != NULL)
+ *name_is_MN = (name->mechanismUsed != GSS_C_NULL_OID);
+
+ if (MN_mech != NULL) {
+ major = gssEapCanonicalizeOid(minor, name->mechanismUsed,
+ OID_FLAG_NULL_VALID, MN_mech);
+ if (GSS_ERROR(major))
+ return major;
+ }
+
if (name->attrCtx == NULL) {
*minor = GSSEAP_NO_ATTR_CONTEXT;
return GSS_S_UNAVAILABLE;