Merge branch 'master' into tlv-mic
[moonshot.git] / mech_eap / import_sec_context.c
index ea1072e..978970a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, JANET(UK)
+ * Copyright (c) 2011, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -49,7 +49,7 @@
     } while (0)
 
 static OM_uint32
-gssEapImportPartialContext(OM_uint32 *minor,
+importPartialRadiusContext(OM_uint32 *minor,
                            unsigned char **pBuf,
                            size_t *pRemain,
                            gss_ctx_id_t ctx)
@@ -58,7 +58,15 @@ gssEapImportPartialContext(OM_uint32 *minor,
     unsigned char *p = *pBuf;
     size_t remain = *pRemain;
     gss_buffer_desc buf;
-    size_t serverLen;
+    size_t ctxLength, serverLen;
+
+    /* Length of partial RADIUS context */
+    CHECK_REMAIN(4);
+    ctxLength = load_uint32_be(p);
+    UPDATE_REMAIN(4);
+
+    CHECK_REMAIN(ctxLength);
+    remain = ctxLength; /* check against partial context length */
 
     /* Selected RADIUS server */
     CHECK_REMAIN(4);
@@ -96,8 +104,12 @@ gssEapImportPartialContext(OM_uint32 *minor,
         UPDATE_REMAIN(buf.length);
     }
 
+#ifdef GSSEAP_DEBUG
+    assert(remain == 0);
+#endif
+
     *pBuf = p;
-    *pRemain = remain;
+    *pRemain -= 4 + ctxLength;
 
     return GSS_S_COMPLETE;
 }
@@ -121,16 +133,9 @@ importMechanismOid(OM_uint32 *minor,
 
     oidBuf.elements = &p[4];
 
-    if (!gssEapIsConcreteMechanismOid(&oidBuf)) {
-        *minor = GSSEAP_WRONG_MECH;
-        return GSS_S_BAD_MECH;
-    }
-
-    if (!gssEapInternalizeOid(&oidBuf, pOid)) {
-        major = duplicateOid(minor, &oidBuf, pOid);
-        if (GSS_ERROR(major))
-            return major;
-    }
+    major = gssEapCanonicalizeOid(minor, &oidBuf, 0, pOid);
+    if (GSS_ERROR(major))
+        return major;
 
     *pBuf    += 4 + oidBuf.length;
     *pRemain -= 4 + oidBuf.length;
@@ -228,6 +233,47 @@ importName(OM_uint32 *minor,
 }
 
 static OM_uint32
+importConversation(OM_uint32 *minor,
+                   unsigned char **pBuf,
+                   size_t *pRemain,
+                   gss_ctx_id_t ctx)
+{
+    OM_uint32 major;
+    unsigned char *p = *pBuf;
+    size_t remain = *pRemain;
+    gss_buffer_desc tmp;
+
+    if (remain < 4) {
+        *minor = GSSEAP_TOK_TRUNC;
+        return GSS_S_DEFECTIVE_TOKEN;
+    }
+
+    tmp.length = load_uint32_be(p);
+    if (tmp.length == 0 ||
+        remain - 4 < tmp.length) {
+        *minor = GSSEAP_TOK_TRUNC;
+        return GSS_S_DEFECTIVE_TOKEN;
+    }
+
+    if (p[4] != 0x06) {
+        *minor = GSSEAP_BAD_TOK_HEADER;
+        return GSS_S_DEFECTIVE_TOKEN;
+    }
+
+    tmp.value = p + 4;
+
+    major = duplicateBuffer(minor, &tmp, &ctx->conversation);
+    if (GSS_ERROR(major))
+        return major;
+
+    *pBuf    += 4 + tmp.length;
+    *pRemain -= 4 + tmp.length;
+
+    *minor = 0;
+    return GSS_S_COMPLETE;
+}
+
+OM_uint32
 gssEapImportContext(OM_uint32 *minor,
                     gss_buffer_t token,
                     gss_ctx_id_t ctx)
@@ -251,8 +297,8 @@ gssEapImportContext(OM_uint32 *minor,
     remain -= 16;
 
     /* Validate state */
-    if (ctx->state < EAP_STATE_IDENTITY ||
-        ctx->state > EAP_STATE_ESTABLISHED)
+    if (GSSEAP_SM_STATE(ctx) < GSSEAP_STATE_INITIAL ||
+        GSSEAP_SM_STATE(ctx) > GSSEAP_STATE_ESTABLISHED)
         return GSS_S_DEFECTIVE_TOKEN;
 
     /* Only acceptor can export partial context tokens */
@@ -289,7 +335,7 @@ gssEapImportContext(OM_uint32 *minor,
         *minor = GSSEAP_TOK_TRUNC;
         return GSS_S_DEFECTIVE_TOKEN;
     }
-    ctx->expiryTime = (time_t)load_uint64_be(&p[0]); /* XXX */
+    ctx->expiryTime = (time_t)load_uint64_be(&p[0]);
     ctx->sendSeq    = load_uint64_be(&p[8]);
     ctx->recvSeq    = load_uint64_be(&p[16]);
     p      += 24;
@@ -304,11 +350,15 @@ gssEapImportContext(OM_uint32 *minor,
      * acceptor contexts.
      */
     if (!CTX_IS_INITIATOR(ctx) && !CTX_IS_ESTABLISHED(ctx)) {
-        assert((ctx->flags & CTX_FLAG_KRB_REAUTH_GSS) == 0);
-
-        major = gssEapImportPartialContext(minor, &p, &remain, ctx);
+        major = importConversation(minor, &p, &remain, ctx);
         if (GSS_ERROR(major))
             return major;
+
+        if ((ctx->flags & CTX_FLAG_KRB_REAUTH) == 0) {
+            major = importPartialRadiusContext(minor, &p, &remain, ctx);
+            if (GSS_ERROR(major))
+                return major;
+        }
     }
 
 #ifdef GSSEAP_DEBUG