xref: /illumos-gate/usr/src/lib/libgss/g_exp_sec_context.c (revision bdb9230ac765cb7af3fc1f4119caf2c5720dceb3)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *  glue routine for gss_export_sec_context
28  */
29 
30 #include <mechglueP.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(
39 	OM_uint32 *minor_status,
40 	gss_ctx_id_t *context_handle,
41 	gss_buffer_t interprocess_token)
42 {
43 
44 	/* Initialize outputs. */
45 
46 	if (minor_status != NULL)
47 		*minor_status = 0;
48 
49 	if (interprocess_token != GSS_C_NO_BUFFER) {
50 		interprocess_token->length = 0;
51 		interprocess_token->value = NULL;
52 	}
53 
54 	/* Validate arguments. */
55 
56 	if (minor_status == NULL)
57 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
58 
59 	if (context_handle == NULL || *context_handle == GSS_C_NO_CONTEXT)
60 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
61 
62 	if (interprocess_token == GSS_C_NO_BUFFER)
63 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
64 
65 	return (GSS_S_COMPLETE);
66 }
67 
68 OM_uint32
69 gss_export_sec_context(minor_status,
70 			context_handle,
71 			interprocess_token)
72 
73 OM_uint32 *minor_status;
74 gss_ctx_id_t *context_handle;
75 gss_buffer_t interprocess_token;
76 
77 {
78 	OM_uint32		status;
79 	OM_uint32 		length;
80 	gss_union_ctx_id_t	ctx;
81 	gss_mechanism		mech;
82 	gss_buffer_desc		token;
83 	char			*buf;
84 
85 	status = val_exp_sec_ctx_args(minor_status,
86 				context_handle, interprocess_token);
87 	if (status != GSS_S_COMPLETE)
88 		return (status);
89 
90 	/*
91 	 * select the approprate underlying mechanism routine and
92 	 * call it.
93 	 */
94 
95 	ctx = (gss_union_ctx_id_t)*context_handle;
96 	mech = __gss_get_mechanism(ctx->mech_type);
97 	if (!mech)
98 		return (GSS_S_BAD_MECH);
99 	if (!mech->gss_export_sec_context)
100 		return (GSS_S_UNAVAILABLE);
101 
102 	status = mech->gss_export_sec_context(mech->context, minor_status,
103 					&ctx->internal_ctx_id, &token);
104 	if (status != GSS_S_COMPLETE)
105 		return (status);
106 
107 	length = token.length + 4 + ctx->mech_type->length;
108 	interprocess_token->length = length;
109 	interprocess_token->value = malloc(length);
110 	if (interprocess_token->value == 0) {
111 		(void) gss_release_buffer(minor_status, &token);
112 		return (GSS_S_FAILURE);
113 	}
114 	buf = interprocess_token->value;
115 	length = ctx->mech_type->length;
116 	buf[3] = (unsigned char) (length & 0xFF);
117 	length >>= 8;
118 	buf[2] = (unsigned char) (length & 0xFF);
119 	length >>= 8;
120 	buf[1] = (unsigned char) (length & 0xFF);
121 	length >>= 8;
122 	buf[0] = (unsigned char) (length & 0xFF);
123 	(void) memcpy(buf+4, ctx->mech_type->elements,
124 			(size_t)ctx->mech_type->length);
125 	(void) memcpy(buf+4+ctx->mech_type->length, token.value, token.length);
126 
127 	(void) gss_release_buffer(minor_status, &token);
128 
129 	free(ctx->mech_type->elements);
130 	free(ctx->mech_type);
131 	free(ctx);
132 	*context_handle = 0;
133 
134 	return (GSS_S_COMPLETE);
135 }
136