New attribute provider SPI
[mech_eap.orig] / util_name.c
index 8f25002..29b0839 100644 (file)
@@ -106,8 +106,7 @@ gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName)
     GSSEAP_KRB_INIT(&krbContext);
     krb5_free_principal(krbContext, name->krbPrincipal);
 
-    radiusReleaseAttrContext(&tmpMinor, &name->radiusCtx);
-    samlReleaseAttrContext(&tmpMinor, &name->samlCtx);
+    gssEapReleaseAttrContext(&tmpMinor, name);
 
     GSSEAP_MUTEX_DESTROY(&name->mutex);
     GSSEAP_FREE(name);
@@ -222,10 +221,10 @@ importExportedName(OM_uint32 *minor,
     OM_uint32 major, tmpMinor;
     krb5_context krbContext;
     unsigned char *p;
-    int composite = 0;
     size_t len, remain;
     gss_buffer_desc buf;
     enum gss_eap_token_type tok_type;
+    gss_name_t name = GSS_C_NO_NAME;
 
     GSSEAP_KRB_INIT(&krbContext);
 
@@ -235,20 +234,23 @@ importExportedName(OM_uint32 *minor,
     if (remain < 6 + GSS_EAP_MECHANISM->length + 4)
         return GSS_S_BAD_NAME;
 
+#define UPDATE_REMAIN(n)    do {            \
+        p += (n);                           \
+        remain -= (n);                      \
+    } while (0)
+
     /* TOK_ID */
     tok_type = load_uint16_be(p);
     if (tok_type != TOK_TYPE_EXPORT_NAME &&
         tok_type != TOK_TYPE_EXPORT_NAME_COMPOSITE)
         return GSS_S_BAD_NAME;
-    p += 2;
-    remain -= 2;
+    UPDATE_REMAIN(2);
 
     /* MECH_OID_LEN */
     len = load_uint16_be(p);
     if (len != 2 + GSS_EAP_MECHANISM->length)
         return GSS_S_BAD_NAME;
-    p += 2;
-    remain -= 2;
+    UPDATE_REMAIN(2);
 
     /* MECH_OID */
     if (p[0] != 0x06)
@@ -257,34 +259,59 @@ importExportedName(OM_uint32 *minor,
         return GSS_S_BAD_MECH;
     if (memcmp(&p[2], GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length))
         return GSS_S_BAD_MECH;
-    p += 2 + GSS_EAP_MECHANISM->length;
-    remain -= 2 + GSS_EAP_MECHANISM->length;
+    UPDATE_REMAIN(2 + GSS_EAP_MECHANISM->length);
 
     /* NAME_LEN */
     len = load_uint32_be(p);
-    p += 4;
-    remain -= 4;
+    UPDATE_REMAIN(4);
 
-    if (remain < len)
-        return GSS_S_BAD_NAME;
+#define CHECK_REMAIN(n)     do {        \
+        if (remain < (n)) {             \
+            *minor = ERANGE;            \
+            major = GSS_S_BAD_NAME;     \
+            goto cleanup;               \
+        }                               \
+    } while (0)
 
     /* NAME */
+    CHECK_REMAIN(len);
     buf.length = len;
     buf.value = p;
+    UPDATE_REMAIN(len);
 
-    p += len;
-    remain -= len;
+    major = importUserName(minor, &buf, &name);
+    if (GSS_ERROR(major))
+        goto cleanup;
 
-    if (composite == 0 && remain != 0)
-        return GSS_S_BAD_NAME;
+    if (tok_type == TOK_TYPE_EXPORT_NAME_COMPOSITE) {
+        gss_buffer_desc buf;
 
-    major = importUserName(minor, &buf, pName);
-    if (GSS_ERROR(major))
-        return major;
+        CHECK_REMAIN(4);
+        name->flags = load_uint32_be(p);
+        UPDATE_REMAIN(4);
 
-    /* XXX TODO composite handling */
+        CHECK_REMAIN(4);
+        buf.length = load_uint32_be(p);
+        UPDATE_REMAIN(4);
 
-    return GSS_S_COMPLETE;
+        CHECK_REMAIN(buf.length);
+        buf.value = p;
+        UPDATE_REMAIN(buf.length);
+
+        major = gssEapImportAttrContext(minor, &buf, name);
+        if (GSS_ERROR(major))
+            goto cleanup;
+    }
+
+    major = GSS_S_COMPLETE;
+
+cleanup:
+    if (GSS_ERROR(major))
+        gssEapReleaseName(&tmpMinor, &name);
+    else
+        *pName = name;
+
+    return major;
 }
 
 OM_uint32
@@ -326,6 +353,7 @@ gssEapExportName(OM_uint32 *minor,
     char *krbName = NULL;
     size_t krbNameLen;
     unsigned char *p;
+    gss_buffer_desc attrs = GSS_C_EMPTY_BUFFER;
 
     exportedName->length = 0;
     exportedName->value = NULL;
@@ -333,25 +361,26 @@ gssEapExportName(OM_uint32 *minor,
     GSSEAP_KRB_INIT(&krbContext);
     GSSEAP_MUTEX_LOCK(&name->mutex);
 
-    /*
-     * Don't export a composite name if we don't have any attributes.
-     */
-    if (composite && !NAME_HAS_ATTRIBUTES(name))
-        composite = 0;
-
     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
-    if (*minor != 0)
+    if (*minor != 0) {
+        major = GSS_S_FAILURE;
         goto cleanup;
+    }
     krbNameLen = strlen(krbName);
 
     exportedName->length = 6 + GSS_EAP_MECHANISM->length + 4 + krbNameLen;
     if (composite) {
-        /* TODO: export SAML/AVP, this is pending specification */
-        
+        exportedName->length += 4;
+
+        major = gssEapExportAttrContext(minor, name, &attrs);
+        if (GSS_ERROR(major))
+            goto cleanup;
+        exportedName->length += 4 + attrs.length;
     }
 
     exportedName->value = GSSEAP_MALLOC(exportedName->length);
     if (exportedName->value == NULL) {
+        major = GSS_S_FAILURE;
         *minor = ENOMEM;
         goto cleanup;
     }
@@ -380,11 +409,21 @@ gssEapExportName(OM_uint32 *minor,
     memcpy(p, krbName, krbNameLen);
     p += krbNameLen;
 
+    if (composite) {
+        store_uint32_be(name->flags, p);
+        p += 4;
+
+        store_uint32_be(attrs.length, p);
+        memcpy(&p[4], attrs.value, attrs.length);
+        p += 4 + attrs.length;
+    }
+
     *minor = 0;
     major = GSS_S_COMPLETE;
 
 cleanup:
     GSSEAP_MUTEX_UNLOCK(&name->mutex);
+    gss_release_buffer(&tmpMinor, &attrs);
     if (GSS_ERROR(major))
         gss_release_buffer(&tmpMinor, exportedName);
     krb5_free_unparsed_name(krbContext, krbName);
@@ -392,125 +431,3 @@ cleanup:
     return major;
 }
 
-static gss_buffer_desc attributePrefixes[] = {
-    {
-        /* ATTR_TYPE_NONE */
-        0,
-        NULL,
-    },
-    {
-        /* ATTR_TYPE_SAML_AAA_ASSERTION */
-        sizeof("urn:ietf:params:gss-eap:saml-aaa-assertion"),
-        "urn:ietf:params:gss-eap:saml-aaa-assertion"
-    },
-    {
-        /* ATTR_TYPE_SAML_ATTR */
-        sizeof("urn:ietf:params:gss-eap:saml-attr"),
-        "urn:ietf:params:gss-eap:saml-attr"
-    },
-    {
-        /* ATTR_TYPE_RADIUS_AVP */
-        sizeof("urn:ietf:params:gss-eap:radius-avp"),
-        "urn:ietf:params:gss-eap:radius-avp",
-    }
-};
-
-enum gss_eap_attribute_type
-gssEapAttributePrefixToType(const gss_buffer_t prefix)
-{
-    enum gss_eap_attribute_type i;
-
-    for (i = ATTR_TYPE_SAML_AAA_ASSERTION;
-         i < sizeof(attributePrefixes) / sizeof(attributePrefixes[0]);
-         i++)
-    {
-        gss_buffer_t p = &attributePrefixes[i];
-
-        if (p->length == prefix->length &&
-            memcmp(p->value, prefix->value, prefix->length) == 0) {
-            return i;
-        }
-    }
-
-    return ATTR_TYPE_NONE;
-}
-
-gss_buffer_t
-gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type)
-{
-    if (type <= ATTR_TYPE_NONE ||
-        type > ATTR_TYPE_RADIUS_AVP)
-        return GSS_C_NO_BUFFER;
-
-    return &attributePrefixes[type];
-}
-
-OM_uint32
-decomposeAttributeName(OM_uint32 *minor,
-                       const gss_buffer_t attribute,
-                       gss_buffer_t prefix,
-                       gss_buffer_t suffix)
-{
-    char *p = NULL;
-    int i;
-
-    for (i = 0; i < attribute->length; i++) {
-        if (((char *)attribute->value)[i] == ' ') {
-            p = (char *)attribute->value + i + 1;
-            break;
-        }
-    }
-
-    prefix->value = attribute->value;
-    prefix->length = i;
-
-    if (p != NULL && *p != '\0')  {
-        suffix->length = attribute->length - 1 - prefix->length;
-        suffix->value = p;
-    } else {
-        suffix->length = 0;
-        suffix->value = NULL;
-    }
-
-    *minor = 0;
-    return GSS_S_COMPLETE;
-}
-
-OM_uint32
-composeAttributeName(OM_uint32 *minor,
-                       const gss_buffer_t prefix,
-                       const gss_buffer_t suffix,
-                       gss_buffer_t attribute)
-{
-    size_t len = 0;
-    char *p;
-
-    attribute->length = 0;
-    attribute->value = NULL;
-
-    if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
-        return GSS_S_COMPLETE;
-
-    len = prefix->length;
-    if (suffix != NULL) {
-        len += 1 + suffix->length;
-    }
-
-    p = attribute->value = GSSEAP_MALLOC(len + 1);
-    if (attribute->value == NULL) {
-        *minor = ENOMEM;
-        return GSS_S_FAILURE;
-    }
-    attribute->length = len;
-
-    memcpy(p, prefix->value, prefix->length);
-    if (suffix != NULL) {
-        p[prefix->length] = ' ';
-        memcpy(p + prefix->length + 1, suffix->value, suffix->length);
-    }
-
-    p[attribute->length] = '\0';
-
-    *minor = 0;
-    return GSS_S_COMPLETE;
-}