record EAP conversation in context
authorLuke Howard <lukeh@padl.com>
Mon, 14 Mar 2011 05:27:39 +0000 (16:27 +1100)
committerLuke Howard <lukeh@padl.com>
Mon, 14 Mar 2011 05:27:39 +0000 (16:27 +1100)
mech_eap/gssapiP_eap.h
mech_eap/util.h
mech_eap/util_context.c
mech_eap/util_sm.c
mech_eap/util_token.c

index b7740da..493eaac 100644 (file)
@@ -201,6 +201,7 @@ struct gss_ctx_id_struct
         #define kerberosCtx          ctxU.kerberos
 #endif
     } ctxU;
+    gss_buffer_desc conversation;
 };
 
 #define TOK_FLAG_SENDER_IS_ACCEPTOR         0x01
index 207a43d..d3b04c4 100644 (file)
@@ -185,18 +185,22 @@ OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
 
 OM_uint32
-gssEapMakeToken(OM_uint32 *minor,
-                gss_ctx_id_t ctx,
-                const gss_buffer_t innerToken,
-                enum gss_eap_token_type tokenType,
-                gss_buffer_t outputToken);
+gssEapRecordContextTokenHeader(OM_uint32 *minor,
+                               gss_ctx_id_t ctx,
+                               enum gss_eap_token_type tokType);
 
 OM_uint32
-gssEapVerifyToken(OM_uint32 *minor,
-                  gss_ctx_id_t ctx,
-                  const gss_buffer_t inputToken,
-                  enum gss_eap_token_type *tokenType,
-                  gss_buffer_t innerInputToken);
+gssEapRecordInnerContextToken(OM_uint32 *minor,
+                              gss_ctx_id_t ctx,
+                              gss_buffer_t innerToken,
+                              OM_uint32 type);
+
+OM_uint32
+gssEapVerifyContextToken(OM_uint32 *minor,
+                         gss_ctx_id_t ctx,
+                         const gss_buffer_t inputToken,
+                         enum gss_eap_token_type tokenType,
+                         gss_buffer_t innerInputToken);
 
 OM_uint32
 gssEapContextTime(OM_uint32 *minor,
@@ -622,13 +626,11 @@ gssEapDecodeInnerTokens(OM_uint32 *minor,
                         OM_uint32 **pTypes);
 
 size_t
-tokenSize(const gss_OID_desc *mech, size_t body_size);
+tokenSize(size_t bodySize);
 
 void
-makeTokenHeader(const gss_OID_desc *mech,
-                size_t body_size,
-                unsigned char **buf,
-                enum gss_eap_token_type tok_type);
+makeTokenHeader(size_t body_size,
+                unsigned char **buf);
 
 OM_uint32
 verifyTokenHeader(OM_uint32 *minor,
index 4fb8161..0a16333 100644 (file)
@@ -130,6 +130,7 @@ gssEapReleaseContext(OM_uint32 *minor,
     gssEapReleaseOid(&tmpMinor, &ctx->mechanismUsed);
     sequenceFree(&tmpMinor, &ctx->seqState);
     gssEapReleaseCred(&tmpMinor, &ctx->defaultCred);
+    gss_release_buffer(&tmpMinor, &ctx->conversation);
 
     GSSEAP_MUTEX_DESTROY(&ctx->mutex);
 
@@ -141,42 +142,103 @@ gssEapReleaseContext(OM_uint32 *minor,
     return GSS_S_COMPLETE;
 }
 
-OM_uint32
-gssEapMakeToken(OM_uint32 *minor,
-                gss_ctx_id_t ctx,
-                const gss_buffer_t innerToken,
-                enum gss_eap_token_type tokenType,
-                gss_buffer_t outputToken)
+static OM_uint32
+recordTokens(OM_uint32 *minor,
+             gss_ctx_id_t ctx,
+             gss_buffer_t tokens,
+             size_t tokensCount)
 {
-    unsigned char *p;
+    unsigned char *buf;
+    size_t i, size, offset;
+
+    size = ctx->conversation.length;
 
-    outputToken->length = tokenSize(ctx->mechanismUsed, innerToken->length);
-    outputToken->value = GSSEAP_MALLOC(outputToken->length);
-    if (outputToken->value == NULL) {
+    for (i = 0; i < tokensCount; i++)
+        size += tokens[i].length;
+
+    buf = GSSEAP_REALLOC(ctx->conversation.value, size);
+    if (buf == NULL) {
         *minor = ENOMEM;
         return GSS_S_FAILURE;
     }
 
-    p = (unsigned char *)outputToken->value;
-    makeTokenHeader(ctx->mechanismUsed, innerToken->length, &p, tokenType);
-    memcpy(p, innerToken->value, innerToken->length);
+    offset = ctx->conversation.length;
+
+    ctx->conversation.length = size;
+    ctx->conversation.value = buf;
+
+    for (i = 0; i < tokensCount; i++) {
+        memcpy(buf + offset, tokens[i].value, tokens[i].length);
+        offset += tokens[i].length;
+    }
 
     *minor = 0;
     return GSS_S_COMPLETE;
 }
 
 OM_uint32
-gssEapVerifyToken(OM_uint32 *minor,
-                  gss_ctx_id_t ctx,
-                  const gss_buffer_t inputToken,
-                  enum gss_eap_token_type *actualToken,
-                  gss_buffer_t innerInputToken)
+gssEapRecordContextTokenHeader(OM_uint32 *minor,
+                               gss_ctx_id_t ctx,
+                               enum gss_eap_token_type tokType)
+{
+    unsigned char wireOidHeader[2], wireTokType[2];
+    gss_buffer_desc buffers[3];
+
+    assert(ctx->mechanismUsed != GSS_C_NO_OID);
+
+    wireOidHeader[0] = 0x06;
+    wireOidHeader[1] = ctx->mechanismUsed->length;
+    buffers[0].length = sizeof(wireOidHeader);
+    buffers[0].value  = wireOidHeader;
+
+    buffers[1].length = ctx->mechanismUsed->length;
+    buffers[1].value  = ctx->mechanismUsed->elements;
+
+    store_uint16_be(tokType, wireTokType);
+    buffers[2].length = sizeof(wireTokType);
+    buffers[2].value = wireTokType;
+    return recordTokens(minor, ctx, buffers, sizeof(buffers)/sizeof(buffers[0]));
+}
+
+OM_uint32
+gssEapRecordInnerContextToken(OM_uint32 *minor,
+                              gss_ctx_id_t ctx,
+                              gss_buffer_t innerToken,
+                              OM_uint32 itokType)
+{
+    gss_buffer_desc buffers[3];
+    unsigned char wireItokType[4], wireLength[4];
+
+    assert(innerToken != GSS_C_NO_BUFFER);
+
+    store_uint32_be(itokType, wireItokType);
+    buffers[0].length = sizeof(wireItokType);
+    buffers[0].value  = wireItokType;
+
+    store_uint32_be(innerToken->length, wireLength);
+    buffers[1].length = sizeof(wireLength);
+    buffers[1].value  = wireLength;
+
+    buffers[2] = *innerToken;
+
+    return recordTokens(minor, ctx, buffers, sizeof(buffers)/sizeof(buffers[0]));
+}
+
+OM_uint32
+gssEapVerifyContextToken(OM_uint32 *minor,
+                         gss_ctx_id_t ctx,
+                         const gss_buffer_t inputToken,
+                         enum gss_eap_token_type tokType,
+                         gss_buffer_t innerInputToken)
 {
     OM_uint32 major;
     size_t bodySize;
     unsigned char *p = (unsigned char *)inputToken->value;
     gss_OID_desc oidBuf;
     gss_OID oid;
+    enum gss_eap_token_type actualTokType;
+    gss_buffer_desc tokenBuf;
 
     if (ctx->mechanismUsed != GSS_C_NO_OID) {
         oid = ctx->mechanismUsed;
@@ -187,10 +249,15 @@ gssEapVerifyToken(OM_uint32 *minor,
     }
 
     major = verifyTokenHeader(minor, oid, &bodySize, &p,
-                              inputToken->length, actualToken);
+                              inputToken->length, &actualTokType);
     if (GSS_ERROR(major))
         return major;
 
+    if (actualTokType != tokType) {
+        *minor = GSSEAP_WRONG_TOK_ID;
+        return GSS_S_DEFECTIVE_TOKEN;
+    }
+
     if (ctx->mechanismUsed == GSS_C_NO_OID) {
         if (!gssEapIsConcreteMechanismOid(oid)) {
             *minor = GSSEAP_WRONG_MECH;
@@ -207,6 +274,18 @@ gssEapVerifyToken(OM_uint32 *minor,
     innerInputToken->length = bodySize;
     innerInputToken->value = p;
 
+    /*
+     * Add OID, tokenType, body to conversation; variable length
+     * header omitted. A better API to verifyTokenHeader would
+     * avoid this ugly pointer arithmetic. XXX FIXME
+     */
+    tokenBuf.value = p - (2 + oid->length + 2);
+    tokenBuf.length = 2 + oid->length + 2 + bodySize;
+
+    major = recordTokens(minor, ctx, &tokenBuf, 1);
+    if (GSS_ERROR(major))
+        return major;
+
     *minor = 0;
     return GSS_S_COMPLETE;
 }
index ca69923..72d5cf4 100644 (file)
@@ -95,21 +95,16 @@ gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state)
 #endif /* GSSEAP_DEBUG */
 
 static OM_uint32
-makeErrorToken(OM_uint32 *minor,
-               OM_uint32 majorStatus,
-               OM_uint32 minorStatus,
-               gss_buffer_set_t *outputToken)
+recordErrorToken(OM_uint32 *minor,
+                 gss_ctx_id_t ctx,
+                 OM_uint32 majorStatus,
+                 OM_uint32 minorStatus)
 {
-    OM_uint32 major;
     unsigned char errorData[8];
     gss_buffer_desc errorBuffer;
 
     assert(GSS_ERROR(majorStatus));
 
-    major = gss_create_empty_buffer_set(minor, outputToken);
-    if (GSS_ERROR(major))
-        return major;
-
     /*
      * Only return error codes that the initiator could have caused,
      * to avoid information leakage.
@@ -130,61 +125,39 @@ makeErrorToken(OM_uint32 *minor,
     errorBuffer.length = sizeof(errorData);
     errorBuffer.value = errorData;
 
-    major = gss_add_buffer_set_member(minor, &errorBuffer, outputToken);
-    if (GSS_ERROR(major))
-        return major;
-
-    return GSS_S_COMPLETE;
+    return gssEapRecordInnerContextToken(minor, ctx, &errorBuffer, 
+                                         ITOK_TYPE_CONTEXT_ERR | ITOK_FLAG_CRITICAL);
 }
 
 static OM_uint32
-allocInnerTokens(OM_uint32 *minor,
-                 size_t count,
-                 gss_buffer_set_t *pTokens,
-                 OM_uint32 **pTokenTypes)
+makeContextToken(OM_uint32 *minor,
+                 gss_ctx_id_t ctx,
+                 size_t headerOffset,
+                 gss_buffer_t outputToken)
 {
-    OM_uint32 major, tmpMinor;
-    gss_buffer_set_t tokens = GSS_C_NO_BUFFER_SET;
-    OM_uint32 *tokenTypes = NULL;
+    size_t tokSize, bodySize;
+    unsigned char *p;
 
-    major = gss_create_empty_buffer_set(minor, &tokens);
-    if (GSS_ERROR(major))
-        goto cleanup;
-
-    assert(tokens->count == 0);
-    assert(tokens->elements == NULL);
+    assert(ctx->conversation.length > headerOffset);
 
-    tokens->elements = (gss_buffer_desc *)GSSEAP_CALLOC(count, sizeof(gss_buffer_desc));
-    if (tokens->elements == NULL) {
-        major = GSS_S_FAILURE;
-        *minor = ENOMEM;
-        goto cleanup;
-    }
+    bodySize = ctx->conversation.length - headerOffset;
+    tokSize = tokenSize(bodySize);
 
-    tokenTypes = (OM_uint32 *)GSSEAP_CALLOC(count, sizeof(OM_uint32));
-    if (tokenTypes == NULL) {
-        major = GSS_S_FAILURE;
+    outputToken->value = GSSEAP_MALLOC(tokSize);
+    if (outputToken->value == NULL) {
         *minor = ENOMEM;
-        goto cleanup;
+        return GSS_S_FAILURE;
     }
 
-    major = GSS_S_COMPLETE;
-    *minor = 0;
+    outputToken->length = tokSize;
 
-cleanup:
-    if (GSS_ERROR(major)) {
-        gss_release_buffer_set(&tmpMinor, &tokens);
-        tokens = GSS_C_NO_BUFFER_SET;
-        if (tokenTypes != NULL) {
-            GSSEAP_FREE(tokenTypes);
-            tokenTypes = NULL;
-        }
-    }
+    p = (unsigned char *)outputToken->value;
 
-    *pTokens = tokens;
-    *pTokenTypes = tokenTypes;
+    makeTokenHeader(bodySize, &p);
+    memcpy(p, (unsigned char *)ctx->conversation.value + headerOffset, bodySize);
 
-    return major;
+    *minor = 0;
+    return GSS_S_COMPLETE;
 }
 
 OM_uint32
@@ -205,12 +178,13 @@ gssEapSmStep(OM_uint32 *minor,
     gss_buffer_desc unwrappedInputToken = GSS_C_EMPTY_BUFFER;
     gss_buffer_desc unwrappedOutputToken = GSS_C_EMPTY_BUFFER;
     gss_buffer_set_t innerInputTokens = GSS_C_NO_BUFFER_SET;
-    gss_buffer_set_t innerOutputTokens = GSS_C_NO_BUFFER_SET;
     OM_uint32 *inputTokenTypes = NULL, *outputTokenTypes = NULL;
     unsigned int smFlags = 0;
     size_t i, j;
     int initialContextToken = 0;
     enum gss_eap_token_type tokType;
+    size_t headerOffset, firstTokenOffset;
+    size_t innerOutputTokenCount = 0;
 
     assert(smCount > 0);
 
@@ -220,17 +194,13 @@ gssEapSmStep(OM_uint32 *minor,
     outputToken->value = NULL;
 
     if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0) {
-        major = gssEapVerifyToken(minor, ctx, inputToken, &tokType,
-                                  &unwrappedInputToken);
-        if (GSS_ERROR(major))
-            goto cleanup;
+        tokType = CTX_IS_INITIATOR(ctx) ?
+            TOK_TYPE_ACCEPTOR_CONTEXT : TOK_TYPE_INITIATOR_CONTEXT;
 
-        if (tokType != (CTX_IS_INITIATOR(ctx)
-                    ? TOK_TYPE_ACCEPTOR_CONTEXT : TOK_TYPE_INITIATOR_CONTEXT)) {
-            major = GSS_S_DEFECTIVE_TOKEN;
-            *minor = GSSEAP_WRONG_TOK_ID;
+        major = gssEapVerifyContextToken(minor, ctx, inputToken, tokType,
+                                         &unwrappedInputToken);
+        if (GSS_ERROR(major))
             goto cleanup;
-        }
     } else if (!CTX_IS_INITIATOR(ctx) || ctx->state != GSSEAP_STATE_INITIAL) {
         major = GSS_S_DEFECTIVE_TOKEN;
         *minor = GSSEAP_WRONG_SIZE;
@@ -252,12 +222,20 @@ gssEapSmStep(OM_uint32 *minor,
     if (GSS_ERROR(major))
         goto cleanup;
 
+    headerOffset = ctx->conversation.length;
+
     assert(innerInputTokens != GSS_C_NO_BUFFER_SET);
 
-    major = allocInnerTokens(minor, smCount, &innerOutputTokens, &outputTokenTypes);
+    /* Get ready to emit an output token */
+    tokType = CTX_IS_INITIATOR(ctx) ?
+        TOK_TYPE_INITIATOR_CONTEXT : TOK_TYPE_ACCEPTOR_CONTEXT;
+
+    major = gssEapRecordContextTokenHeader(minor, ctx, tokType);
     if (GSS_ERROR(major))
         goto cleanup;
 
+    firstTokenOffset = ctx->conversation.length;
+
     /* Process all the tokens that are valid for the current state. */
     for (i = 0; i < smCount; i++) {
         struct gss_eap_sm *smp = &sm[i];
@@ -321,18 +299,30 @@ gssEapSmStep(OM_uint32 *minor,
                 smFlags |= SM_FLAG_TRANSITED;
 
             if (innerOutputToken.value != NULL) {
-                innerOutputTokens->elements[innerOutputTokens->count] = innerOutputToken;
-                assert(smp->outputTokenType != ITOK_TYPE_NONE);
-                outputTokenTypes[innerOutputTokens->count] = smp->outputTokenType;
+                OM_uint32 outputTokenType = smp->outputTokenType;
+
                 if (smFlags & SM_FLAG_OUTPUT_TOKEN_CRITICAL)
-                    outputTokenTypes[innerOutputTokens->count] |= ITOK_FLAG_CRITICAL;
-                innerOutputTokens->count++;
+                    outputTokenType |= ITOK_FLAG_CRITICAL;
+
+                assert(smp->outputTokenType != ITOK_TYPE_NONE);
+
+                tmpMajor = gssEapRecordInnerContextToken(&tmpMinor, ctx,
+                                                         &innerOutputToken,
+                                                         outputTokenType);
+                if (GSS_ERROR(tmpMajor)) {
+                    major = tmpMajor;
+                    *minor = tmpMinor;
+                    break;
+                }
+
+                innerOutputTokenCount++;
             }
+
             /*
              * Break out if we made a state transition and have some tokens to send.
              */
             if ((smFlags & SM_FLAG_TRANSITED) &&
-                 ((smFlags & SM_FLAG_FORCE_SEND_TOKEN) || innerOutputTokens->count != 0)) {
+                 ((smFlags & SM_FLAG_FORCE_SEND_TOKEN) || innerOutputTokenCount != 0)) {
                 SM_ASSERT_VALID(ctx, major);
                 break;
             }
@@ -345,8 +335,6 @@ gssEapSmStep(OM_uint32 *minor,
         }
     }
 
-    assert(innerOutputTokens->count <= smCount);
-
     /* Check we understood all critical tokens sent by peer */
     if (!GSS_ERROR(major)) {
         for (j = 0; j < innerInputTokens->count; j++) {
@@ -365,38 +353,27 @@ gssEapSmStep(OM_uint32 *minor,
             goto cleanup; /* return error directly to caller */
 
         /* replace any emitted tokens with error token */
-        gss_release_buffer_set(&tmpMinor, &innerOutputTokens);
+        ctx->conversation.length = firstTokenOffset;
 
-        tmpMajor = makeErrorToken(&tmpMinor, major, *minor, &innerOutputTokens);
+        tmpMajor = recordErrorToken(&tmpMinor, ctx, major, *minor);
         if (GSS_ERROR(tmpMajor)) {
             major = tmpMajor;
             *minor = tmpMinor;
             goto cleanup;
         }
 
-        if (innerOutputTokens->count != 0)
-            outputTokenTypes[0] = ITOK_TYPE_CONTEXT_ERR | ITOK_FLAG_CRITICAL;
+        innerOutputTokenCount = 1;
     }
 
     /* Format output token from inner tokens */
-    if (innerOutputTokens->count != 0 ||            /* inner tokens to send */
+    if (innerOutputTokenCount != 0 ||               /* inner tokens to send */
         !CTX_IS_INITIATOR(ctx) ||                   /* any leg acceptor */
         !CTX_IS_ESTABLISHED(ctx)) {                 /* non-last leg initiator */
-        tmpMajor = gssEapEncodeInnerTokens(&tmpMinor, innerOutputTokens,
-                                           outputTokenTypes, &unwrappedOutputToken);
-        if (tmpMajor == GSS_S_COMPLETE) {
-            if (CTX_IS_INITIATOR(ctx))
-                tokType = TOK_TYPE_INITIATOR_CONTEXT;
-            else
-                tokType = TOK_TYPE_ACCEPTOR_CONTEXT;
-
-            tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &unwrappedOutputToken,
-                                       tokType, outputToken);
-            if (GSS_ERROR(tmpMajor)) {
-                major = tmpMajor;
-                *minor = tmpMinor;
-                goto cleanup;
-            }
+        tmpMajor = makeContextToken(&tmpMinor, ctx, headerOffset, outputToken);
+        if (GSS_ERROR(tmpMajor)) {
+            major = tmpMajor;
+            *minor = tmpMinor;
+            goto cleanup;
         }
     }
 
@@ -407,7 +384,6 @@ gssEapSmStep(OM_uint32 *minor,
 
 cleanup:
     gss_release_buffer_set(&tmpMinor, &innerInputTokens);
-    gss_release_buffer_set(&tmpMinor, &innerOutputTokens);
     if (inputTokenTypes != NULL)
         GSSEAP_FREE(inputTokenTypes);
     if (outputTokenTypes != NULL)
index a929198..b5ccc21 100644 (file)
@@ -305,12 +305,8 @@ der_read_length(unsigned char **buf, ssize_t *bufsize)
 /* returns the length of a token, given the mech oid and the body size */
 
 size_t
-tokenSize(const gss_OID_desc *mech, size_t body_size)
+tokenSize(size_t body_size)
 {
-    assert(mech != GSS_C_NO_OID);
-
-    /* set body_size to sequence contents size */
-    body_size += 4 + (size_t) mech->length;         /* NEED overflow check */
     return 1 + der_length_size(body_size) + body_size;
 }
 
@@ -319,20 +315,11 @@ tokenSize(const gss_OID_desc *mech, size_t body_size)
 
 void
 makeTokenHeader(
-    const gss_OID_desc *mech,
     size_t body_size,
-    unsigned char **buf,
-    enum gss_eap_token_type tok_type)
+    unsigned char **buf)
 {
     *(*buf)++ = 0x60;
-    der_write_length(buf, (tok_type == -1) ?2:4 + mech->length + body_size);
-    *(*buf)++ = 0x06;
-    *(*buf)++ = (unsigned char)mech->length;
-    memcpy(*buf, mech->elements, mech->length);
-    *buf += mech->length;
-    assert(tok_type != TOK_TYPE_NONE);
-    *(*buf)++ = (unsigned char)((tok_type>>8) & 0xff);
-    *(*buf)++ = (unsigned char)(tok_type & 0xff);
+    der_write_length(buf, body_size);
 }
 
 /*