send a composite name token instead of a sec context to shib
[moonshot.git] / mech_eap / util_attr.cpp
index 733662f..4ce40fa 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <typeinfo>
 #include <string>
+#include <sstream>
 #include <exception>
 #include <new>
 
@@ -53,11 +54,17 @@ gssEapAttrProvidersInitInternal(void)
     assert(gssEapAttrProvidersInitStatus == GSS_S_UNAVAILABLE);
 
     major = gssEapRadiusAttrProviderInit(&minor);
-    if (major == GSS_S_COMPLETE)
-        major = gssEapSamlAttrProvidersInit(&minor);
-    if (major == GSS_S_COMPLETE)
-        major = gssEapLocalAttrProviderInit(&minor);
+    if (GSS_ERROR(major))
+        goto cleanup;
+
+    major = gssEapSamlAttrProvidersInit(&minor);
+    if (GSS_ERROR(major))
+        goto cleanup;
 
+    /* Allow Shibboleth initialization failure to be non-fatal */
+    gssEapLocalAttrProviderInit(&minor);
+
+cleanup:
 #ifdef GSSEAP_DEBUG
     assert(major == GSS_S_COMPLETE);
 #endif
@@ -212,7 +219,7 @@ gss_eap_attr_ctx::releaseProvider(unsigned int type)
  * Initialize a context from an existing context.
  */
 bool
-gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
+gss_eap_attr_ctx::initWithExistingContext(const gss_eap_attr_ctx *manager)
 {
     bool ret = true;
 
@@ -228,7 +235,7 @@ gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
 
         provider = m_providers[i];
 
-        ret = provider->initFromExistingContext(this,
+        ret = provider->initWithExistingContext(this,
                                                 manager->m_providers[i]);
         if (ret == false) {
             releaseProvider(i);
@@ -243,7 +250,7 @@ gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
  * Initialize a context from a GSS credential and context.
  */
 bool
-gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
+gss_eap_attr_ctx::initWithGssContext(const gss_cred_id_t cred,
                                      const gss_ctx_id_t ctx)
 {
     bool ret = true;
@@ -263,7 +270,7 @@ gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
 
         provider = m_providers[i];
 
-        ret = provider->initFromGssContext(this, cred, ctx);
+        ret = provider->initWithGssContext(this, cred, ctx);
         if (ret == false) {
             releaseProvider(i);
             break;
@@ -273,58 +280,27 @@ gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
     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)
+gss_eap_attr_ctx::initWithJsonObject(JSONObject &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;
+        foundSource[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);
+    if (obj["version"].integer() != 1)
+        return false;
 
-        CHECK_REMAIN(4);
-        providerToken.length = load_uint32_be(p);
-        UPDATE_REMAIN(4);
+    m_flags = obj["flags"].integer();
 
-        CHECK_REMAIN(providerToken.length);
-        providerToken.value = p;
-        UPDATE_REMAIN(providerToken.length);
+    JSONObject sources = obj["sources"];
 
-        if (type < ATTR_TYPE_MIN || type > ATTR_TYPE_MAX ||
-            didInit[type])
-            return false;
+    /* Initialize providers from serialized state */
+    for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) {
+        gss_eap_attr_provider *provider;
+        const char *key;
 
         if (!providerEnabled(type)) {
             releaseProvider(type);
@@ -332,32 +308,30 @@ gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
         }
 
         provider = m_providers[type];
+        key = provider->name();
+        if (key == NULL)
+            continue;
 
-        ret = provider->initFromBuffer(this, &providerToken);
-        if (ret == false) {
+        JSONObject source = sources.get(key);
+        if (!source.isNull() &&
+            !provider->initWithJsonObject(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];
 
-        ret = provider->initFromGssContext(this,
+        ret = provider->initWithGssContext(this,
                                            GSS_C_NO_CREDENTIAL,
                                            GSS_C_NO_CONTEXT);
         if (ret == false) {
@@ -369,6 +343,62 @@ gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
     return true;
 }
 
+JSONObject
+gss_eap_attr_ctx::jsonRepresentation(void) const
+{
+    JSONObject obj, sources;
+    unsigned int i;
+
+    obj.set("version", 1);
+    obj.set("flags", m_flags);
+
+    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider;
+        const char *key;
+
+        provider = m_providers[i];
+        if (provider == NULL)
+            continue; /* provider not initialised */
+
+        key = provider->name();
+        if (key == NULL)
+            continue; /* provider does not have state */
+
+        JSONObject source = provider->jsonRepresentation();
+        sources.set(key, source);
+    }
+
+    obj.set("sources", sources);
+
+    return obj;
+}
+
+/*
+ * Initialize a context from an exported context or name token
+ */
+bool
+gss_eap_attr_ctx::initWithBuffer(const gss_buffer_t buffer)
+{
+    OM_uint32 major, minor;
+    bool ret;
+    char *s;
+    json_error_t error;
+
+    major = bufferToString(&minor, buffer, &s);
+    if (GSS_ERROR(major))
+        return false;
+
+    JSONObject obj = JSONObject::load(s, 0, &error);
+    if (!obj.isNull()) {
+        ret = initWithJsonObject(obj);
+    } else
+        ret = false;
+
+    GSSEAP_FREE(s);
+
+    return ret;
+}
+
 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
 {
     for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++)
@@ -502,10 +532,8 @@ gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
     unsigned int i;
 
     major = gss_create_empty_buffer_set(&minor, attrs);
-    if (GSS_ERROR(major)) {
-        throw new std::bad_alloc;
-        return false;
-    }
+    if (GSS_ERROR(major))
+        throw std::bad_alloc();
 
     args.attrs = *attrs;
 
@@ -601,51 +629,19 @@ 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;
-    }
+    OM_uint32 minor;
+    char *s;
 
-    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider = m_providers[i];
+    JSONObject obj = jsonRepresentation();
 
-        if (provider == NULL)
-            continue;
-
-        provider->exportToBuffer(&providerTokens[i]);
-
-        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;
+#if 0
+    obj.dump(stdout);
+#endif
 
-        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;
+    s = obj.dump(JSON_COMPACT);
 
-        gss_release_buffer(&tmpMinor, &providerTokens[i]);
-    }
+    if (GSS_ERROR(makeStringBuffer(&minor, s, buffer)))
+        throw std::bad_alloc();
 }
 
 /*
@@ -682,11 +678,15 @@ gss_eap_attr_ctx::mapException(OM_uint32 *minor, std::exception &e) const
     OM_uint32 major;
 
     /* Errors we handle ourselves */
-    major = GSS_S_FAILURE;
-
     if (typeid(e) == typeid(std::bad_alloc)) {
+        major = GSS_S_FAILURE;
         *minor = ENOMEM;
         goto cleanup;
+    } else if (typeid(e) == typeid(JSONException)) {
+        major = GSS_S_BAD_NAME;
+        *minor = GSSEAP_BAD_ATTR_TOKEN;
+        gssEapSaveStatusInfo(*minor, "%s", e.what());
+        goto cleanup;
     }
 
     /* Errors we delegate to providers */
@@ -709,11 +709,6 @@ gss_eap_attr_ctx::mapException(OM_uint32 *minor, std::exception &e) const
     }
 
 cleanup:
-#if 0
-    /* rethrow for now for debugging */
-    throw e;
-#endif
-
     assert(GSS_ERROR(major));
 
     return major;
@@ -859,13 +854,15 @@ gssEapInquireName(OM_uint32 *minor,
         return GSS_S_UNAVAILABLE;
     }
 
-    try {
-        if (!name->attrCtx->getAttributeTypes(attrs)) {
-            *minor = GSSEAP_NO_ATTR_CONTEXT;
-            return GSS_S_UNAVAILABLE;
+    if (attrs != NULL) {
+        try {
+            if (!name->attrCtx->getAttributeTypes(attrs)) {
+                *minor = GSSEAP_NO_ATTR_CONTEXT;
+                return GSS_S_UNAVAILABLE;
+            }
+        } catch (std::exception &e) {
+            return name->attrCtx->mapException(minor, e);
         }
-    } catch (std::exception &e) {
-        return name->attrCtx->mapException(minor, e);
     }
 
     return GSS_S_COMPLETE;
@@ -881,8 +878,10 @@ gssEapGetNameAttribute(OM_uint32 *minor,
                        gss_buffer_t display_value,
                        int *more)
 {
-    *authenticated = 0;
-    *complete = 0;
+    if (authenticated != NULL)
+        *authenticated = 0;
+    if (complete != NULL)
+        *complete = 0;
 
     if (value != NULL) {
         value->length = 0;
@@ -1004,29 +1003,38 @@ gssEapImportAttrContext(OM_uint32 *minor,
                         gss_name_t name)
 {
     gss_eap_attr_ctx *ctx = NULL;
+    OM_uint32 major = GSS_S_FAILURE;
 
     assert(name->attrCtx == NULL);
 
     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
         return GSS_S_UNAVAILABLE;
 
-    if (buffer->length != 0) {
-        try {
-            ctx = new gss_eap_attr_ctx();
+    if (buffer->length == 0)
+        return GSS_S_COMPLETE;
 
-            if (!ctx->initFromBuffer(buffer)) {
-                delete ctx;
-                *minor = GSSEAP_BAD_ATTR_TOKEN;
-                return GSS_S_DEFECTIVE_TOKEN;
-            }
+    try {
+        ctx = new gss_eap_attr_ctx();
+
+        if (ctx->initWithBuffer(buffer)) {
             name->attrCtx = ctx;
-        } catch (std::exception &e) {
-            delete ctx;
-            return name->attrCtx->mapException(minor, e);
+            major = GSS_S_COMPLETE;
+            *minor = 0;
+        } else {
+            major = GSS_S_BAD_NAME;
+            *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
         }
+    } catch (std::exception &e) {
+        if (ctx != NULL)
+            major = ctx->mapException(minor, e);
     }
 
-    return GSS_S_COMPLETE;
+    assert(major == GSS_S_COMPLETE || name->attrCtx == NULL);
+
+    if (GSS_ERROR(major))
+        delete ctx;
+
+    return major;
 }
 
 OM_uint32
@@ -1035,27 +1043,38 @@ gssEapDuplicateAttrContext(OM_uint32 *minor,
                            gss_name_t out)
 {
     gss_eap_attr_ctx *ctx = NULL;
+    OM_uint32 major = GSS_S_FAILURE;
 
     assert(out->attrCtx == NULL);
 
+    if (in->attrCtx == NULL) {
+        *minor = 0;
+        return GSS_S_COMPLETE;
+    }
+
     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
         return GSS_S_UNAVAILABLE;
 
     try {
-        if (in->attrCtx != NULL) {
-            ctx = new gss_eap_attr_ctx();
-            if (!ctx->initFromExistingContext(in->attrCtx)) {
-                delete ctx;
-                *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
-                return GSS_S_FAILURE;
-            }
+        ctx = new gss_eap_attr_ctx();
+
+        if (ctx->initWithExistingContext(in->attrCtx)) {
             out->attrCtx = ctx;
+            major = GSS_S_COMPLETE;
+            *minor = 0;
+        } else {
+            major = GSS_S_FAILURE;
+            *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
         }
     } catch (std::exception &e) {
-        delete ctx;
-        return in->attrCtx->mapException(minor, e);
+        major = in->attrCtx->mapException(minor, e);
     }
 
+    assert(major == GSS_S_COMPLETE || out->attrCtx == NULL);
+
+    if (GSS_ERROR(major))
+        delete ctx;
+
     return GSS_S_COMPLETE;
 }
 
@@ -1135,29 +1154,32 @@ gssEapCreateAttrContext(OM_uint32 *minor,
 
     assert(gssCtx != GSS_C_NO_CONTEXT);
 
+    *pAttrContext = NULL;
+
     major = gssEapAttrProvidersInit(minor);
     if (GSS_ERROR(major))
         return major;
 
-    *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
-    major = GSS_S_FAILURE;
-
     try {
-        ctx = new gss_eap_attr_ctx();
-        if (ctx->initFromGssContext(gssCred, gssCtx)) {
-            *minor = 0;
+        /* Set *pAttrContext here to for reentrancy */
+        *pAttrContext = ctx = new gss_eap_attr_ctx();
+
+        if (ctx->initWithGssContext(gssCred, gssCtx)) {
+            *pExpiryTime = ctx->getExpiryTime();
             major = GSS_S_COMPLETE;
+            *minor = 0;
         } else {
-            delete ctx;
+            major = GSS_S_FAILURE;
+            *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
         }
     } catch (std::exception &e) {
         if (ctx != NULL)
             major = ctx->mapException(minor, e);
     }
 
-    if (major == GSS_S_COMPLETE) {
-        *pAttrContext = ctx;
-        *pExpiryTime = ctx->getExpiryTime();
+    if (GSS_ERROR(major)) {
+        delete ctx;
+        *pAttrContext = NULL;
     }
 
     return major;