Eap channel bindings cleanup
[mech_eap.git] / mech_eap / init_sec_context.c
index 15e0520..417ad4e 100644 (file)
@@ -36,6 +36,9 @@
  */
 
 #include "gssapiP_eap.h"
+#include "radius/radius.h"
+#include "util_radius.h"
+#include "utils/radius_utils.h"
 
 static OM_uint32
 policyVariableToFlag(enum eapol_bool_var variable)
@@ -194,6 +197,151 @@ static struct eapol_callbacks gssEapPolicyCallbacks = {
 extern int wpa_debug_level;
 #endif
 
+#define CHBIND_SERVICE_NAME_FLAG 0x01
+#define CHBIND_HOST_NAME_FLAG 0x02
+#define CHBIND_SERVICE_SPECIFIC_FLAG 0x04
+#define CHBIND_REALM_NAME_FLAG 0x08
+
+extern void TestFunc();
+
+static OM_uint32
+peerInitEapChannelBinding(OM_uint32 *minor, gss_ctx_id_t ctx)
+{
+    struct wpabuf *buf = NULL;
+    int component, components = 0;
+    unsigned int requested = 0;
+    krb5_principal princ;
+    gss_buffer_desc nameBuf;
+    OM_uint32 major = GSS_S_COMPLETE;
+    krb5_context krbContext = NULL;
+
+    /* must have acceptor name, but already checked in
+     * eapGssSmInitAcceptorName(), so maybe redunadant
+     * to do so here as well? */
+    if (!ctx->acceptorName) {
+        *minor = GSSEAP_NO_ACCEPTOR_NAME;
+        return GSS_S_BAD_NAME;
+    }
+
+    princ = ctx->acceptorName->krbPrincipal;
+
+    krbPrincComponentToGssBuffer(princ, 0, &nameBuf);
+    if (nameBuf.length > 0) {
+        major = gssEapRadiusAddAttr(minor, &buf, PW_GSS_ACCEPTOR_SERVICE_NAME,
+                                    VENDORPEC_UKERNA, &nameBuf);
+        if (GSS_ERROR(major))
+            goto init_chbind_cleanup;
+        requested |= CHBIND_SERVICE_NAME_FLAG;
+    }
+
+    krbPrincComponentToGssBuffer(princ, 1, &nameBuf);
+    if (nameBuf.length > 0) {
+        major = gssEapRadiusAddAttr(minor, &buf, PW_GSS_ACCEPTOR_HOST_NAME,
+                                    VENDORPEC_UKERNA, &nameBuf);
+        if (GSS_ERROR(major))
+            goto init_chbind_cleanup;
+        requested |= CHBIND_HOST_NAME_FLAG;
+    }
+
+    GSSEAP_KRB_INIT(&krbContext);
+    *minor = krbPrincUnparseServiceSpecifics(krbContext, princ, &nameBuf);
+    if (*minor)
+        goto init_chbind_cleanup;
+
+    if (nameBuf.length > 0) {
+        major = gssEapRadiusAddAttr(minor, &buf,
+                                    PW_GSS_ACCEPTOR_SERVICE_SPECIFIC,
+                                    VENDORPEC_UKERNA, &nameBuf);
+        if (GSS_ERROR(major)) {
+            krbFreeUnparsedName(krbContext, &nameBuf);
+            goto init_chbind_cleanup;
+        }
+        requested |= CHBIND_SERVICE_SPECIFIC_FLAG;
+    }
+    krbFreeUnparsedName(krbContext, &nameBuf);
+
+    krbPrincRealmToGssBuffer(princ, &nameBuf);
+    if (nameBuf.length > 0) {
+        major = gssEapRadiusAddAttr(minor, &buf,
+                                    PW_GSS_ACCEPTOR_REALM_NAME,
+                                    VENDORPEC_UKERNA, &nameBuf);
+        requested |= CHBIND_REALM_NAME_FLAG;
+    }
+
+    if (requested==0) {
+        wpabuf_free(buf);
+        *minor = GSSEAP_BAD_ACCEPTOR_NAME;
+        return GSS_S_BAD_NAME;
+    }
+    ctx->initiatorCtx.chbindData = buf;
+    ctx->initiatorCtx.chbindReqFlags = requested;
+    buf = NULL;
+init_chbind_cleanup:
+    wpabuf_free(buf);
+    return major;
+}
+
+static void
+peerProcessChbindResponse(void *context, int code, int nsid,
+                          u8 *data, size_t len)
+{
+    radius_parser msg, vendor_specific;
+    gss_ctx_id_t ctx = (gss_ctx_id_t )context;
+    void *vsadata;
+    u8 type;
+    u32 vendor_id;
+    u32 accepted = 0;
+    size_t vsadata_len;
+    int i;
+
+    if (nsid != CHBIND_NSID_RADIUS)
+        return;
+    msg = radius_parser_start(data, len);
+    if (!msg)
+        return;
+    while (radius_parser_parse_tlv(msg, &type, &vendor_id, &vsadata,
+                                   &vsadata_len) == 0) {
+        void *unused_data;
+        size_t unused_len;
+        u8 vendor_type;
+
+        if ((type != RADIUS_ATTR_VENDOR_SPECIFIC) ||
+            (vendor_id != VENDORPEC_UKERNA))
+            continue;
+        vendor_specific = radius_parser_start(vsadata, vsadata_len);
+        if (!vendor_specific)
+            continue;
+        while (radius_parser_parse_vendor_specific(vendor_specific,
+                                                   &vendor_type,
+                                                   &unused_data,
+                                                   &unused_len) == 0) {
+            switch (vendor_type) {
+            case PW_GSS_ACCEPTOR_SERVICE_NAME:
+                accepted |= CHBIND_SERVICE_NAME_FLAG;
+                break;
+            case PW_GSS_ACCEPTOR_HOST_NAME:
+                accepted |= CHBIND_HOST_NAME_FLAG;
+                break;
+            case PW_GSS_ACCEPTOR_SERVICE_SPECIFIC:
+                accepted |= CHBIND_SERVICE_SPECIFIC_FLAG;
+                break;
+            case PW_GSS_ACCEPTOR_REALM_NAME:
+                accepted |= CHBIND_REALM_NAME_FLAG;
+                break;
+            }
+        }
+        radius_parser_finish(vendor_specific);
+    }
+    radius_parser_finish(msg);
+    if ((code == CHBIND_CODE_SUCCESS) &&
+        (accepted == ctx->initiatorCtx.chbindReqFlags)) {
+        ctx->flags |= CTX_FLAG_EAP_CHBIND_ACCEPT;
+        /* Accepted! */
+    } else {
+        /* log failures? */
+    }
+}
+
 static OM_uint32
 peerConfigInit(OM_uint32 *minor, gss_ctx_id_t ctx)
 {
@@ -258,6 +406,28 @@ peerConfigInit(OM_uint32 *minor, gss_ctx_id_t ctx)
     eapPeerConfig->subject_match = (unsigned char *)cred->subjectNameConstraint.value;
     eapPeerConfig->altsubject_match = (unsigned char *)cred->subjectAltNameConstraint.value;
 
+    /* eap channel binding */
+    if (ctx->initiatorCtx.chbindData)
+    {
+        struct eap_peer_chbind_config *chbind_config =
+            (struct eap_peer_chbind_config *)
+            GSSEAP_MALLOC(sizeof(struct eap_peer_chbind_config));
+        if (chbind_config == NULL) {
+            *minor = ENOMEM;
+            return GSS_S_FAILURE;
+        }
+
+        chbind_config->req_data = wpabuf_mhead_u8(ctx->initiatorCtx.chbindData);
+        chbind_config->req_data_len = wpabuf_len(ctx->initiatorCtx.chbindData);
+        chbind_config->nsid = CHBIND_NSID_RADIUS;
+        chbind_config->response_cb = &peerProcessChbindResponse;
+        chbind_config->ctx = ctx;
+        eapPeerConfig->chbind_config = chbind_config;
+        eapPeerConfig->chbind_config_len = 1;
+    } else {
+        eapPeerConfig->chbind_config = NULL;
+        eapPeerConfig->chbind_config_len = 0;
+    }
     *minor = 0;
     return GSS_S_COMPLETE;
 }
@@ -457,8 +627,10 @@ eapGssSmInitGssReauth(OM_uint32 *minor,
     gss_OID actualMech = GSS_C_NO_OID;
     OM_uint32 gssFlags, timeRec;
 
-    GSSEAP_ASSERT(cred != GSS_C_NO_CREDENTIAL);
-
+    /*
+     * Here we use the passed in credential handle because the resolved
+     * context credential does not currently have the reauth creds.
+     */
     if (GSSEAP_SM_STATE(ctx) == GSSEAP_STATE_INITIAL) {
         if (!gssEapCanReauthP(cred, target, timeReq))
             return GSS_S_CONTINUE_NEEDED;
@@ -470,6 +642,8 @@ eapGssSmInitGssReauth(OM_uint32 *minor,
         goto cleanup;
     }
 
+    GSSEAP_ASSERT(cred != GSS_C_NO_CREDENTIAL);
+
     major = gssEapMechToGlueName(minor, target, &mechTarget);
     if (GSS_ERROR(major))
         goto cleanup;
@@ -577,6 +751,16 @@ eapGssSmInitAcceptorName(OM_uint32 *minor,
         return GSS_S_FAILURE;
     }
 
+    /*
+     * Generate channel binding data
+     */
+    if (ctx->initiatorCtx.chbindData == NULL)
+    {
+        major = peerInitEapChannelBinding(minor, ctx);
+        if (GSS_ERROR(major))
+            return major;
+    }
+
     return GSS_S_CONTINUE_NEEDED;
 }
 
@@ -1014,8 +1198,15 @@ gssEapInitSecContext(OM_uint32 *minor,
             goto cleanup;
         }
     }
-    if (ret_flags != NULL)
-        *ret_flags = ctx->gssFlags;
+    if (ret_flags != NULL) {
+        if ((major == GSS_S_COMPLETE) &&
+            (ctx->flags & CTX_FLAG_EAP_CHBIND_ACCEPT))
+            *ret_flags = ctx->gssFlags | GSS_C_MUTUAL_FLAG;
+        else
+            *ret_flags = ctx->gssFlags & (~GSS_C_MUTUAL_FLAG);
+    }
+    if (major == GSS_S_COMPLETE)
+        major = major;
     if (time_rec != NULL)
         gssEapContextTime(&tmpMinor, ctx, time_rec);