Merge branch 'master' into tlv-mic
[moonshot.git] / mech_eap / util_context.c
index ceb58f5..ed6edac 100644 (file)
@@ -142,159 +142,13 @@ gssEapReleaseContext(OM_uint32 *minor,
     return GSS_S_COMPLETE;
 }
 
-static OM_uint32
-recordTokens(OM_uint32 *minor,
-             gss_ctx_id_t ctx,
-             gss_buffer_t tokens,
-             size_t tokensCount)
-{
-    unsigned char *buf;
-    size_t i, size, offset;
-
-    size = ctx->conversation.length;
-
-    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;
-    }
-
-    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
-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;
-    } else {
-        oidBuf.elements = NULL;
-        oidBuf.length = 0;
-        oid = &oidBuf;
-    }
-
-    major = verifyTokenHeader(minor, oid, &bodySize, &p,
-                              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;
-            return GSS_S_BAD_MECH;
-        }
-
-        if (!gssEapInternalizeOid(oid, &ctx->mechanismUsed)) {
-            major = duplicateOid(minor, oid, &ctx->mechanismUsed);
-            if (GSS_ERROR(major))
-                return major;
-        }
-    }
-
-    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;
-}
-
 OM_uint32
 gssEapContextTime(OM_uint32 *minor,
                   gss_ctx_id_t context_handle,
                   OM_uint32 *time_rec)
 {
+    *minor = 0;
+
     if (context_handle->expiryTime == 0) {
         *time_rec = GSS_C_INDEFINITE;
     } else {
@@ -360,8 +214,9 @@ gssEapVerifyConversationMIC(OM_uint32 *minor,
      * TLV header, as well as a header for emiting a subsequent
      * token. These should not be included as input to verifyMIC.
      */
-    tokenHeaderLength = 8 + convMIC->length + 2 + ctx->mechanismUsed->length + 2;
-    assert(ctx->conversation.length > tokenHeaderLength);
+    tokenHeaderLength = ITOK_HEADER_LENGTH + convMIC->length
+        + 2 + ctx->mechanismUsed->length + 2;
+    assert(ctx->conversation.length >= tokenHeaderLength);
     iov[0].buffer.length -= tokenHeaderLength;
 
     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
@@ -380,61 +235,46 @@ gssEapVerifyConversationMIC(OM_uint32 *minor,
 }
 
 OM_uint32
-gssEapEncodeExtensions(OM_uint32 *minor,
-                       OM_uint32 *types,
-                       size_t typesCount,
-                       gss_buffer_t outputToken)
+gssEapMakeTokenChannelBindings(OM_uint32 *minor,
+                               gss_ctx_id_t ctx,
+                               gss_channel_bindings_t userChanBindings,
+                               gss_buffer_t inputToken,
+                               gss_channel_bindings_t wireChanBindings)
 {
-    size_t i;
+    gss_buffer_t wireData = &wireChanBindings->application_data;
     unsigned char *p;
+    size_t tokenHeaderLength = 0;
 
-    outputToken->value = GSSEAP_MALLOC(4 * typesCount);
-    if (outputToken->value == NULL) {
-        *minor = ENOMEM;
-        return GSS_S_FAILURE;
-    }
-    p = (unsigned char *)outputToken->value;
+    memset(wireChanBindings, 0, sizeof(*wireChanBindings));
 
-    outputToken->length = 4 * typesCount;
+    if (!CTX_IS_INITIATOR(ctx)) {
+        assert(inputToken != GSS_C_NO_BUFFER);
 
-    for (i = 0; i < typesCount; i++) {
-        store_uint32_be(types[i], p);
-        p += 4;
+        tokenHeaderLength = ITOK_HEADER_LENGTH + inputToken->length +
+            2 + ctx->mechanismUsed->length + 2;
+        assert(ctx->conversation.length >= tokenHeaderLength);
     }
 
-    *minor = 0;
-    return GSS_S_COMPLETE;
-}
+    wireData->length = ctx->conversation.length - tokenHeaderLength;
 
-OM_uint32
-gssEapProcessExtensions(OM_uint32 *minor,
-                        gss_buffer_t inputToken,
-                        struct gss_eap_itok_map *map,
-                        size_t mapCount,
-                        OM_uint32 *flags)
-{
-    size_t i;
-    unsigned char *p;
+    if (userChanBindings != GSS_C_NO_CHANNEL_BINDINGS)
+        wireData->length += userChanBindings->application_data.length;
 
-    if ((inputToken->length % 4) != 0) {
-        *minor = GSSEAP_TOK_TRUNC;
-        return GSS_S_DEFECTIVE_TOKEN;
+    wireData->value = GSSEAP_MALLOC(wireData->length);
+    if (wireData->value == NULL) {
+        *minor = ENOMEM;
+        return GSS_S_FAILURE;
     }
 
-    p = (unsigned char *)inputToken->value;
+    p = (unsigned char *)wireData->value;
 
-    for (i = 0; i < inputToken->length / 4; i++) {
-        OM_uint32 type = load_uint32_be(p);
-        size_t j;
-
-        for (j = 0; j < mapCount; j++) {
-            if (map->type == type) {
-                *flags |= map->flag;
-                break;
-            }
-        }
+    memcpy(p, ctx->conversation.value, ctx->conversation.length - tokenHeaderLength);
+    p += ctx->conversation.length - tokenHeaderLength;
 
-        p += 4;
+    if (userChanBindings != GSS_C_NO_CHANNEL_BINDINGS) {
+        memcpy(p, userChanBindings->application_data.value,
+               userChanBindings->application_data.length);
+        p += userChanBindings->application_data.length;
     }
 
     *minor = 0;