initial libradsec port
[mech_eap.git] / export_sec_context.c
index 021e366..46bf118 100644 (file)
@@ -37,8 +37,50 @@ gssEapExportPartialContext(OM_uint32 *minor,
                            gss_ctx_id_t ctx,
                            gss_buffer_t token)
 {
-    /* XXX we also need to serialise the current server name */
-    return duplicateBuffer(minor, &ctx->acceptorCtx.state, token);
+    OM_uint32 major, tmpMinor;
+    size_t length, serverLen = 0;
+    unsigned char *p;
+    char serverBuf[MAXHOSTNAMELEN];
+
+    if (ctx->acceptorCtx.radConn != NULL &&
+        rs_conn_get_current_server(ctx->acceptorCtx.radConn,
+                                   serverBuf, sizeof(serverBuf)) == 0)
+        serverLen = strlen(serverBuf);
+
+    length = 4 + serverLen + 4 + ctx->acceptorCtx.state.length;
+
+    token->value = GSSEAP_MALLOC(length);
+    if (token->value == NULL) {
+        *minor = ENOMEM;
+        major = GSS_S_FAILURE;
+        goto cleanup;
+    }
+    token->length = length;
+
+    p = (unsigned char *)token->value;
+
+    store_uint32_be(serverLen, p);
+    p += 4;
+    if (serverLen != 0) {
+        memcpy(p, serverBuf, serverLen);
+        p += serverLen;
+    }
+
+    store_uint32_be(ctx->acceptorCtx.state.length, p);
+    p += 4;
+    if (ctx->acceptorCtx.state.length != 0) {
+        memcpy(p, ctx->acceptorCtx.state.value,
+               ctx->acceptorCtx.state.length);
+        p += ctx->acceptorCtx.state.length;
+    }
+
+    assert(p == (unsigned char *)token->value + token->length);
+
+cleanup:
+    if (GSS_ERROR(major))
+        gss_release_buffer(&tmpMinor, token);
+
+    return major;
 }
 
 static OM_uint32
@@ -159,8 +201,12 @@ gss_export_sec_context(OM_uint32 *minor,
     interprocess_token->length = 0;
     interprocess_token->value = NULL;
 
-    if (ctx == GSS_C_NO_CONTEXT)
+    if (ctx == GSS_C_NO_CONTEXT) {
+        *minor = EINVAL;
         return GSS_S_NO_CONTEXT;
+    }
+
+    *minor = 0;
 
     GSSEAP_MUTEX_LOCK(&ctx->mutex);