xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_exp_sec_context.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* #pragma ident	"@(#)g_exp_sec_context.c	1.14	04/02/23 SMI" */
2 
3 /*
4  * Copyright 1996 by Sun Microsystems, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appears in all copies and
9  * that both that copyright notice and this permission notice appear in
10  * supporting documentation, and that the name of Sun Microsystems not be used
11  * in advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission. Sun Microsystems makes no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
20  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24 
25 /*
26  *  glue routine for gss_export_sec_context
27  */
28 #ifndef LEAN_CLIENT
29 
30 #include "mglueP.h"
31 #include <stdio.h>
32 #include <errno.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #include <string.h>
37 
38 static OM_uint32
val_exp_sec_ctx_args(OM_uint32 * minor_status,gss_ctx_id_t * context_handle,gss_buffer_t interprocess_token)39 val_exp_sec_ctx_args(
40     OM_uint32 *minor_status,
41     gss_ctx_id_t *context_handle,
42     gss_buffer_t interprocess_token)
43 {
44 
45     /* Initialize outputs. */
46 
47     if (minor_status != NULL)
48 	*minor_status = 0;
49 
50     if (interprocess_token != GSS_C_NO_BUFFER) {
51 	interprocess_token->length = 0;
52 	interprocess_token->value = NULL;
53     }
54 
55     /* Validate arguments. */
56 
57     if (minor_status == NULL)
58 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
59 
60     if (context_handle == NULL || *context_handle == GSS_C_NO_CONTEXT)
61 	return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
62 
63     if (interprocess_token == GSS_C_NO_BUFFER)
64 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
65 
66     return (GSS_S_COMPLETE);
67 }
68 
69 
70 OM_uint32 KRB5_CALLCONV
gss_export_sec_context(OM_uint32 * minor_status,gss_ctx_id_t * context_handle,gss_buffer_t interprocess_token)71 gss_export_sec_context(OM_uint32 *minor_status, gss_ctx_id_t *context_handle,
72 		       gss_buffer_t interprocess_token)
73 {
74     OM_uint32		status;
75     OM_uint32 		length;
76     gss_union_ctx_id_t	ctx = NULL;
77     gss_mechanism	mech;
78     gss_buffer_desc	token = GSS_C_EMPTY_BUFFER;
79     char		*buf;
80 
81     status = val_exp_sec_ctx_args(minor_status,
82 				  context_handle, interprocess_token);
83     if (status != GSS_S_COMPLETE)
84 	return (status);
85 
86     /*
87      * select the approprate underlying mechanism routine and
88      * call it.
89      */
90 
91     ctx = (gss_union_ctx_id_t) *context_handle;
92     if (ctx->internal_ctx_id == GSS_C_NO_CONTEXT)
93 	return (GSS_S_NO_CONTEXT);
94     mech = gssint_get_mechanism (ctx->mech_type);
95     if (!mech)
96 	return GSS_S_BAD_MECH;
97     if (!mech->gss_export_sec_context)
98 	return (GSS_S_UNAVAILABLE);
99 
100     status = mech->gss_export_sec_context(minor_status,
101 					  &ctx->internal_ctx_id, &token);
102     if (status != GSS_S_COMPLETE) {
103 	map_error(minor_status, mech);
104 	goto cleanup;
105     }
106 
107     length = token.length + 4 + ctx->mech_type->length;
108     interprocess_token->length = length;
109     interprocess_token->value = gssalloc_malloc(length);
110     if (interprocess_token->value == 0) {
111 	*minor_status = ENOMEM;
112 	status = GSS_S_FAILURE;
113 	goto cleanup;
114     }
115     buf = interprocess_token->value;
116     length = ctx->mech_type->length;
117     buf[3] = (unsigned char) (length & 0xFF);
118     length >>= 8;
119     buf[2] = (unsigned char) (length & 0xFF);
120     length >>= 8;
121     buf[1] = (unsigned char) (length & 0xFF);
122     length >>= 8;
123     buf[0] = (unsigned char) (length & 0xFF);
124     memcpy(buf+4, ctx->mech_type->elements, (size_t) ctx->mech_type->length);
125     memcpy(buf+4+ctx->mech_type->length, token.value, token.length);
126 
127     status = GSS_S_COMPLETE;
128 
129 cleanup:
130     (void) gss_release_buffer(minor_status, &token);
131     if (ctx != NULL && ctx->internal_ctx_id == GSS_C_NO_CONTEXT) {
132 	/* If the mech deleted its context, delete the union context. */
133 	free(ctx->mech_type->elements);
134 	free(ctx->mech_type);
135 	free(ctx);
136 	*context_handle = GSS_C_NO_CONTEXT;
137     }
138     return status;
139 }
140 #endif /*LEAN_CLIENT */
141