xref: /titanic_41/usr/src/lib/libgss/g_exp_sec_context.c (revision 69bb4bb45c98da60d21839c4dc3c01ea1be60585)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *  glue routine for gss_export_sec_context
31  */
32 
33 #include <mechglueP.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #include <string.h>
40 
41 OM_uint32
42 gss_export_sec_context(minor_status,
43 			context_handle,
44 			interprocess_token)
45 
46 OM_uint32 *minor_status;
47 gss_ctx_id_t *context_handle;
48 gss_buffer_t interprocess_token;
49 
50 {
51 	OM_uint32		status;
52 	OM_uint32 		length;
53 	gss_union_ctx_id_t	ctx;
54 	gss_mechanism		mech;
55 	gss_buffer_desc		token;
56 	char			*buf;
57 
58 	if (minor_status == NULL)
59 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
60 	*minor_status = 0;
61 
62 	if (context_handle == NULL || *context_handle == GSS_C_NO_CONTEXT)
63 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
64 
65 	if (interprocess_token == NULL)
66 		return (GSS_S_CALL_INACCESSIBLE_READ);
67 
68 	/*
69 	 * select the approprate underlying mechanism routine and
70 	 * call it.
71 	 */
72 
73 	ctx = (gss_union_ctx_id_t)*context_handle;
74 	mech = __gss_get_mechanism(ctx->mech_type);
75 	if (!mech)
76 		return (GSS_S_BAD_MECH);
77 	if (!mech->gss_export_sec_context)
78 		return (GSS_S_UNAVAILABLE);
79 
80 	status = mech->gss_export_sec_context(mech->context, minor_status,
81 					&ctx->internal_ctx_id, &token);
82 	if (status != GSS_S_COMPLETE)
83 		return (status);
84 
85 	length = token.length + 4 + ctx->mech_type->length;
86 	interprocess_token->length = length;
87 	interprocess_token->value = malloc(length);
88 	if (interprocess_token->value == 0) {
89 		(void) gss_release_buffer(minor_status, &token);
90 		return (GSS_S_FAILURE);
91 	}
92 	buf = interprocess_token->value;
93 	length = ctx->mech_type->length;
94 	buf[3] = (unsigned char) (length & 0xFF);
95 	length >>= 8;
96 	buf[2] = (unsigned char) (length & 0xFF);
97 	length >>= 8;
98 	buf[1] = (unsigned char) (length & 0xFF);
99 	length >>= 8;
100 	buf[0] = (unsigned char) (length & 0xFF);
101 	(void) memcpy(buf+4, ctx->mech_type->elements,
102 			(size_t)ctx->mech_type->length);
103 	(void) memcpy(buf+4+ctx->mech_type->length, token.value, token.length);
104 
105 	(void) gss_release_buffer(minor_status, &token);
106 
107 	free(ctx->mech_type->elements);
108 	free(ctx->mech_type);
109 	free(ctx);
110 	*context_handle = 0;
111 
112 	return (GSS_S_COMPLETE);
113 }
114