xref: /titanic_50/usr/src/lib/libgss/g_exp_sec_context.c (revision bde3d612a7c090234c60e6e4578821237a5db135)
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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  *  glue routine for gss_export_sec_context
27  */
28 #ifndef LEAN_CLIENT
29 
30 #include <mechglueP.h>
31 #include "gssapiP_generic.h"
32 #include <stdio.h>
33 #include <errno.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <string.h>
38 
39 static OM_uint32 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 OM_uint32
70 gss_export_sec_context(minor_status,
71 			context_handle,
72 			interprocess_token)
73 
74 OM_uint32 *minor_status;
75 gss_ctx_id_t *context_handle;
76 gss_buffer_t interprocess_token;
77 
78 {
79 	OM_uint32		status;
80 	OM_uint32 		length;
81 	gss_union_ctx_id_t	ctx;
82 	gss_mechanism		mech;
83 	gss_buffer_desc		token;
84 	char			*buf;
85 
86 	status = val_exp_sec_ctx_args(minor_status,
87 				context_handle, interprocess_token);
88 	if (status != GSS_S_COMPLETE)
89 		return (status);
90 
91 	/*
92 	 * select the approprate underlying mechanism routine and
93 	 * call it.
94 	 */
95 
96 	ctx = (gss_union_ctx_id_t)*context_handle;
97 	mech = __gss_get_mechanism(ctx->mech_type);
98 	if (!mech)
99 		return (GSS_S_BAD_MECH);
100 	if (!mech->gss_export_sec_context)
101 		return (GSS_S_UNAVAILABLE);
102 
103 	status = mech->gss_export_sec_context(mech->context, minor_status,
104 					&ctx->internal_ctx_id, &token);
105 	if (status != GSS_S_COMPLETE) {
106 		map_error(minor_status, mech);
107 		return (status);
108 	}
109 
110 	length = token.length + 4 + ctx->mech_type->length;
111 	interprocess_token->length = length;
112 	interprocess_token->value = malloc(length);
113 	if (interprocess_token->value == 0) {
114 		(void) gss_release_buffer(minor_status, &token);
115 		return (GSS_S_FAILURE);
116 	}
117 	buf = interprocess_token->value;
118 	length = ctx->mech_type->length;
119 	buf[3] = (unsigned char) (length & 0xFF);
120 	length >>= 8;
121 	buf[2] = (unsigned char) (length & 0xFF);
122 	length >>= 8;
123 	buf[1] = (unsigned char) (length & 0xFF);
124 	length >>= 8;
125 	buf[0] = (unsigned char) (length & 0xFF);
126 	(void) memcpy(buf+4, ctx->mech_type->elements,
127 			(size_t)ctx->mech_type->length);
128 	(void) memcpy(buf+4+ctx->mech_type->length, token.value, token.length);
129 
130 	(void) gss_release_buffer(minor_status, &token);
131 
132 	free(ctx->mech_type->elements);
133 	free(ctx->mech_type);
134 	free(ctx);
135 	*context_handle = 0;
136 
137 	return (GSS_S_COMPLETE);
138 }
139 #endif /*LEAN_CLIENT */
140