initial libradsec port
[mech_eap.orig] / init_sec_context.c
index e3b4d63..96ec805 100644 (file)
@@ -33,9 +33,6 @@
 #include "gssapiP_eap.h"
 
 #ifdef GSSEAP_ENABLE_REAUTH
-static int
-canReauthP(gss_cred_id_t cred);
-
 static OM_uint32
 eapGssSmInitGssReauth(OM_uint32 *minor,
                       gss_cred_id_t cred,
@@ -212,6 +209,13 @@ peerConfigInit(OM_uint32 *minor,
     krb5_error_code code;
     char *identity;
 
+    eapPeerConfig->identity = NULL;
+    eapPeerConfig->identity_len = 0;
+    eapPeerConfig->password = NULL;
+    eapPeerConfig->password_len = 0;
+
+    assert(cred != GSS_C_NO_CREDENTIAL);
+
     GSSEAP_KRB_INIT(&krbContext);
 
     eapPeerConfig->fragment_size = 1024;
@@ -246,7 +250,7 @@ peerConfigFree(OM_uint32 *minor,
 }
 
 static OM_uint32
-initReady(OM_uint32 *minor, gss_ctx_id_t ctx)
+initReady(OM_uint32 *minor, gss_ctx_id_t ctx, OM_uint32 reqFlags)
 {
     OM_uint32 major;
     const unsigned char *key;
@@ -254,6 +258,12 @@ initReady(OM_uint32 *minor, gss_ctx_id_t ctx)
     krb5_enctype encryptionType;
     int gotKey = 0;
 
+#if 1
+    /* XXX actually check for mutual auth */
+    if (reqFlags & GSS_C_MUTUAL_FLAG)
+        ctx->gssFlags |= GSS_C_MUTUAL_FLAG;
+#endif
+
     /* Cache encryption type derived from selected mechanism OID */
     major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &encryptionType);
     if (GSS_ERROR(major))
@@ -317,24 +327,35 @@ initBegin(OM_uint32 *minor,
 {
     OM_uint32 major;
 
-    if (cred != GSS_C_NO_CREDENTIAL && cred->expiryTime)
+    assert(cred != GSS_C_NO_CREDENTIAL);
+
+    if (cred->expiryTime)
         ctx->expiryTime = cred->expiryTime;
     else if (timeReq == 0 || timeReq == GSS_C_INDEFINITE)
         ctx->expiryTime = 0;
     else
         ctx->expiryTime = time(NULL) + timeReq;
 
-    if (cred != GSS_C_NO_CREDENTIAL) {
-        major = gssEapDuplicateName(minor, cred->name, &ctx->initiatorName);
-        if (GSS_ERROR(major))
-            return major;
-    }
+    /*
+     * The credential mutex protects its name, however we need to
+     * explicitly lock the acceptor name (unlikely as it may be
+     * that it has attributes set on it).
+     */
+    major = gssEapDuplicateName(minor, cred->name, &ctx->initiatorName);
+    if (GSS_ERROR(major))
+        return major;
+
+    GSSEAP_MUTEX_LOCK(&target->mutex);
 
     major = gssEapDuplicateName(minor, target, &ctx->acceptorName);
-    if (GSS_ERROR(major))
+    if (GSS_ERROR(major)) {
+        GSSEAP_MUTEX_UNLOCK(&target->mutex);
         return major;
+    }
 
-    if (mech == GSS_C_NULL_OID || oidEqual(mech, GSS_EAP_MECHANISM)) {
+    GSSEAP_MUTEX_UNLOCK(&target->mutex);
+
+    if (mech == GSS_C_NULL_OID) {
         major = gssEapDefaultMech(minor, &ctx->mechanismUsed);
     } else if (gssEapIsConcreteMechanismOid(mech)) {
         if (!gssEapInternalizeOid(mech, &ctx->mechanismUsed))
@@ -439,7 +460,7 @@ eapGssSmInitAuthenticate(OM_uint32 *minor,
 
         resp = eap_get_eapRespData(ctx->initiatorCtx.eap);
     } else if (ctx->flags & CTX_FLAG_EAP_SUCCESS) {
-        major = initReady(minor, ctx);
+        major = initReady(minor, ctx, reqFlags);
         if (GSS_ERROR(major))
             goto cleanup;
 
@@ -619,16 +640,14 @@ gss_init_sec_context(OM_uint32 *minor,
     gss_buffer_desc innerInputToken;
     gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
     enum gss_eap_token_type tokType;
+    gss_cred_id_t defaultCred = GSS_C_NO_CREDENTIAL;
+    int initialContextToken = 0;
 
     *minor = 0;
 
     output_token->length = 0;
     output_token->value = NULL;
 
-    if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_INITIATE)) {
-        return GSS_S_NO_CRED;
-    }
-
     if (ctx == GSS_C_NO_CONTEXT) {
         if (input_token != GSS_C_NO_BUFFER && input_token->length != 0) {
             return GSS_S_DEFECTIVE_TOKEN;
@@ -640,16 +659,42 @@ gss_init_sec_context(OM_uint32 *minor,
 
         ctx->flags |= CTX_FLAG_INITIATOR;
 
+        initialContextToken = 1;
+        *context_handle = ctx;
+    }
+
+    GSSEAP_MUTEX_LOCK(&ctx->mutex);
+
+    if (cred == GSS_C_NO_CREDENTIAL) {
+        if (ctx->initiatorCtx.defaultCred == GSS_C_NO_CREDENTIAL) {
+            major = gssEapAcquireCred(minor,
+                                      GSS_C_NO_NAME,
+                                      GSS_C_NO_BUFFER,
+                                      time_req,
+                                      GSS_C_NO_OID_SET,
+                                      GSS_C_INITIATE,
+                                      &defaultCred,
+                                      NULL,
+                                      NULL);
+            if (GSS_ERROR(major))
+                goto cleanup;
+        }
+
+        cred = ctx->initiatorCtx.defaultCred;
+    }
+
+    GSSEAP_MUTEX_LOCK(&cred->mutex);
+
 #ifdef GSSEAP_ENABLE_REAUTH
-        if (canReauthP(cred))
+    if (initialContextToken && gssEapCanReauthP(cred, target_name, time_req))
             ctx->state = EAP_STATE_KRB_REAUTH_GSS;
 #endif
 
-        *context_handle = ctx;
+    if ((cred->flags & CRED_FLAG_INITIATE) == 0) {
+        major = GSS_S_NO_CRED;
+        goto cleanup;
     }
 
-    GSSEAP_MUTEX_LOCK(&ctx->mutex);
-
     sm = &eapGssInitiatorSm[ctx->state];
 
     if (input_token != GSS_C_NO_BUFFER) {
@@ -709,6 +754,8 @@ gss_init_sec_context(OM_uint32 *minor,
     assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
 
 cleanup:
+    if (cred != GSS_C_NO_CREDENTIAL)
+        GSSEAP_MUTEX_UNLOCK(&cred->mutex);
     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
 
     if (GSS_ERROR(major))
@@ -720,14 +767,6 @@ cleanup:
 }
 
 #ifdef GSSEAP_ENABLE_REAUTH
-static int
-canReauthP(gss_cred_id_t cred)
-{
-    return (cred != GSS_C_NO_CREDENTIAL &&
-            cred->krbCred != GSS_C_NO_CREDENTIAL &&
-            cred->expiryTime > time(NULL));
-}
-
 static OM_uint32
 eapGssSmInitGssReauth(OM_uint32 *minor,
                       gss_cred_id_t cred,
@@ -792,5 +831,3 @@ cleanup:
     return major;
 }
 #endif /* GSSEAP_ENABLE_REAUTH */
-
-