krb5 commit: Factor out mechglue union context creation

Greg Hudson ghudson at mit.edu
Tue Nov 19 23:29:06 EST 2019


https://github.com/krb5/krb5/commit/e885935cce1eaf617b60db673511cec39216f657
commit e885935cce1eaf617b60db673511cec39216f657
Author: Greg Hudson <ghudson at mit.edu>
Date:   Tue Nov 19 15:02:04 2019 -0500

    Factor out mechglue union context creation
    
    Add a helper function gssint_create_union_context() and use it in the
    four mechglue functions which create a context.  In
    gss_import_sec_context(), create the union context later and eliminate
    the cleanup label.

 src/lib/gssapi/mechglue/g_accept_sec_context.c |   16 ++-------
 src/lib/gssapi/mechglue/g_glue.c               |   28 ++++++++++++++
 src/lib/gssapi/mechglue/g_imp_sec_context.c    |   30 ++++-----------
 src/lib/gssapi/mechglue/g_init_sec_context.c   |   17 ++-------
 src/lib/gssapi/mechglue/g_set_context_option.c |   46 ++++++++---------------
 src/lib/gssapi/mechglue/mglueP.h               |    6 +++
 6 files changed, 64 insertions(+), 79 deletions(-)

diff --git a/src/lib/gssapi/mechglue/g_accept_sec_context.c b/src/lib/gssapi/mechglue/g_accept_sec_context.c
index 8e63a9b..4f2a66e 100644
--- a/src/lib/gssapi/mechglue/g_accept_sec_context.c
+++ b/src/lib/gssapi/mechglue/g_accept_sec_context.c
@@ -232,20 +232,10 @@ gss_cred_id_t *		d_cred;
 
     /* Now create a new context if we didn't get one. */
     if (*context_handle == GSS_C_NO_CONTEXT) {
-	status = GSS_S_FAILURE;
-	union_ctx_id = (gss_union_ctx_id_t)
-	    malloc(sizeof(gss_union_ctx_id_desc));
-	if (!union_ctx_id)
-	    return (GSS_S_FAILURE);
-
-	union_ctx_id->loopback = union_ctx_id;
-	union_ctx_id->internal_ctx_id = GSS_C_NO_CONTEXT;
-	status = generic_gss_copy_oid(&temp_minor_status, selected_mech,
-				      &union_ctx_id->mech_type);
-	if (status != GSS_S_COMPLETE) {
-	    free(union_ctx_id);
+	status = gssint_create_union_context(minor_status, selected_mech,
+					     &union_ctx_id);
+	if (status != GSS_S_COMPLETE)
 	    return (status);
-	}
     }
 
     /*
diff --git a/src/lib/gssapi/mechglue/g_glue.c b/src/lib/gssapi/mechglue/g_glue.c
index 4cd2e8f..dfef49e 100644
--- a/src/lib/gssapi/mechglue/g_glue.c
+++ b/src/lib/gssapi/mechglue/g_glue.c
@@ -758,3 +758,31 @@ gssint_create_copy_buffer(srcBuf, destBuf, addNullChar)
 
     return (GSS_S_COMPLETE);
 } /* ****** gssint_create_copy_buffer  ****** */
+
+OM_uint32
+gssint_create_union_context(OM_uint32 *minor, gss_const_OID mech_oid,
+			    gss_union_ctx_id_t *ctx_out)
+{
+    OM_uint32 status;
+    gss_union_ctx_id_t ctx;
+
+    *ctx_out = NULL;
+
+    ctx = calloc(1, sizeof(*ctx));
+    if (ctx == NULL) {
+	*minor = ENOMEM;
+	return GSS_S_FAILURE;
+    }
+
+    status = generic_gss_copy_oid(minor, mech_oid, &ctx->mech_type);
+    if (status != GSS_S_COMPLETE) {
+	free(ctx);
+	return status;
+    }
+
+    ctx->loopback = ctx;
+    ctx->internal_ctx_id = GSS_C_NO_CONTEXT;
+
+    *ctx_out = ctx;
+    return GSS_S_COMPLETE;
+}
diff --git a/src/lib/gssapi/mechglue/g_imp_sec_context.c b/src/lib/gssapi/mechglue/g_imp_sec_context.c
index a0e2d71..6315201 100644
--- a/src/lib/gssapi/mechglue/g_imp_sec_context.c
+++ b/src/lib/gssapi/mechglue/g_imp_sec_context.c
@@ -97,10 +97,6 @@ gss_ctx_id_t *		context_handle;
     /* Initial value needed below. */
     status = GSS_S_FAILURE;
 
-    ctx = (gss_union_ctx_id_t) malloc(sizeof(gss_union_ctx_id_desc));
-    if (!ctx)
-	return (GSS_S_FAILURE);
-
     if (interprocess_token->length >= sizeof (OM_uint32)) {
 	p = interprocess_token->value;
 	length = (OM_uint32)*p++;
@@ -111,7 +107,6 @@ gss_ctx_id_t *		context_handle;
 
     if (length == 0 ||
 	length > (interprocess_token->length - sizeof (OM_uint32))) {
-	free(ctx);
 	return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
     }
 
@@ -131,24 +126,18 @@ gss_ctx_id_t *		context_handle;
     status = gssint_select_mech_type(minor_status, &token_mech,
 				     &selected_mech);
     if (status != GSS_S_COMPLETE)
-	goto error_out;
+	return status;
 
     mech = gssint_get_mechanism(selected_mech);
-    if (!mech) {
-	status = GSS_S_BAD_MECH;
-	goto error_out;
-    }
+    if (!mech)
+	return GSS_S_BAD_MECH;
     if (!mech->gssspi_import_sec_context_by_mech &&
-	!mech->gss_import_sec_context) {
-	status = GSS_S_UNAVAILABLE;
-	goto error_out;
-    }
+	!mech->gss_import_sec_context)
+	return GSS_S_UNAVAILABLE;
 
-    if (generic_gss_copy_oid(minor_status, selected_mech,
-			     &ctx->mech_type) != GSS_S_COMPLETE) {
-	status = GSS_S_FAILURE;
-	goto error_out;
-    }
+    status = gssint_create_union_context(minor_status, selected_mech, &ctx);
+    if (status != GSS_S_COMPLETE)
+	return status;
 
     if (mech->gssspi_import_sec_context_by_mech) {
 	public_mech = gssint_get_public_oid(selected_mech);
@@ -160,15 +149,12 @@ gss_ctx_id_t *		context_handle;
     }
     if (status == GSS_S_COMPLETE) {
 	ctx->internal_ctx_id = mctx;
-	ctx->loopback = ctx;
 	*context_handle = (gss_ctx_id_t)ctx;
 	return (GSS_S_COMPLETE);
     }
     map_error(minor_status, mech);
     free(ctx->mech_type->elements);
     free(ctx->mech_type);
-
-error_out:
     free(ctx);
     return status;
 }
diff --git a/src/lib/gssapi/mechglue/g_init_sec_context.c b/src/lib/gssapi/mechglue/g_init_sec_context.c
index cf10192..a58074c 100644
--- a/src/lib/gssapi/mechglue/g_init_sec_context.c
+++ b/src/lib/gssapi/mechglue/g_init_sec_context.c
@@ -184,20 +184,10 @@ OM_uint32 *		time_rec;
      */
 
     if(*context_handle == GSS_C_NO_CONTEXT) {
-	status = GSS_S_FAILURE;
-	union_ctx_id = (gss_union_ctx_id_t)
-	    malloc(sizeof(gss_union_ctx_id_desc));
-	if (union_ctx_id == NULL)
+	status = gssint_create_union_context(minor_status, selected_mech,
+					     &union_ctx_id);
+	if (status != GSS_S_COMPLETE)
 	    goto end;
-
-	if (generic_gss_copy_oid(&temp_minor_status, selected_mech,
-				 &union_ctx_id->mech_type) != GSS_S_COMPLETE) {
-	    free(union_ctx_id);
-	    goto end;
-	}
-
-	/* copy the supplied context handle */
-	union_ctx_id->internal_ctx_id = GSS_C_NO_CONTEXT;
     } else {
 	union_ctx_id = (gss_union_ctx_id_t)*context_handle;
 	if (union_ctx_id->internal_ctx_id == GSS_C_NO_CONTEXT) {
@@ -248,7 +238,6 @@ OM_uint32 *		time_rec;
 	    free(union_ctx_id);
 	}
     } else if (*context_handle == GSS_C_NO_CONTEXT) {
-	union_ctx_id->loopback = union_ctx_id;
 	*context_handle = (gss_ctx_id_t)union_ctx_id;
     }
 
diff --git a/src/lib/gssapi/mechglue/g_set_context_option.c b/src/lib/gssapi/mechglue/g_set_context_option.c
index c356131..eb67a9e 100644
--- a/src/lib/gssapi/mechglue/g_set_context_option.c
+++ b/src/lib/gssapi/mechglue/g_set_context_option.c
@@ -71,37 +71,23 @@ gss_set_sec_context_option (OM_uint32 *minor_status,
 					      &internal_ctx,
 					      desired_object,
 					      value);
-    if (status == GSS_S_COMPLETE) {
-	if (ctx == NULL && internal_ctx != GSS_C_NO_CONTEXT) {
-	    /* Allocate a union context handle to wrap new context */
-	    ctx = (gss_union_ctx_id_t)malloc(sizeof(*ctx));
-	    if (ctx == NULL) {
-		*minor_status = ENOMEM;
-		gssint_delete_internal_sec_context(&minor,
-						   &mech->mech_type,
-						   &internal_ctx,
-						   GSS_C_NO_BUFFER);
-		return GSS_S_FAILURE;
-	    }
-
-	    status = generic_gss_copy_oid(minor_status,
-					  &mech->mech_type,
-					  &ctx->mech_type);
-	    if (status != GSS_S_COMPLETE) {
-		gssint_delete_internal_sec_context(&minor,
-						   ctx->mech_type,
-						   &internal_ctx,
-						   GSS_C_NO_BUFFER);
-		free(ctx);
-		return status;
-	    }
+    if (status != GSS_S_COMPLETE) {
+	map_error(minor_status, mech);
+	return status;
+    }
 
-	    ctx->loopback = ctx;
-	    ctx->internal_ctx_id = internal_ctx;
-	    *context_handle = (gss_ctx_id_t)ctx;
+    if (ctx == NULL && internal_ctx != GSS_C_NO_CONTEXT) {
+	status = gssint_create_union_context(minor_status, &mech->mech_type,
+					     &ctx);
+	if (status != GSS_S_COMPLETE) {
+	    gssint_delete_internal_sec_context(&minor, ctx->mech_type,
+					       &internal_ctx, GSS_C_NO_BUFFER);
+	    return status;
 	}
-    } else
-	map_error(minor_status, mech);
 
-    return status;
+	ctx->internal_ctx_id = internal_ctx;
+	*context_handle = (gss_ctx_id_t)ctx;
+    }
+
+    return GSS_S_COMPLETE;
 }
diff --git a/src/lib/gssapi/mechglue/mglueP.h b/src/lib/gssapi/mechglue/mglueP.h
index 2b00987..c296354 100644
--- a/src/lib/gssapi/mechglue/mglueP.h
+++ b/src/lib/gssapi/mechglue/mglueP.h
@@ -769,6 +769,12 @@ OM_uint32 gssint_create_copy_buffer(
 	int			/* NULL terminate buffer ? */
 );
 
+OM_uint32 gssint_create_union_context(
+	OM_uint32 *minor,	/* minor_status */
+	gss_const_OID,		/* mech_oid */
+	gss_union_ctx_id_t *	/* ctx_out */
+);
+
 OM_uint32 gssint_copy_oid_set(
 	OM_uint32 *,			/* minor_status */
 	const gss_OID_set_desc * const,	/* oid set */


More information about the cvs-krb5 mailing list