xref: /illumos-gate/usr/src/lib/libgss/g_exp_sec_context.c (revision 503a2b89eaf04b96af9e457a7806f65ce3e0b723)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*503a2b89SPeter Shoults  * Common Development and Distribution License (the "License").
6*503a2b89SPeter Shoults  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*503a2b89SPeter Shoults  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *  glue routine for gss_export_sec_context
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <mechglueP.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate 
38*503a2b89SPeter Shoults static OM_uint32 val_exp_sec_ctx_args(
39*503a2b89SPeter Shoults 	OM_uint32 *minor_status,
40*503a2b89SPeter Shoults 	gss_ctx_id_t *context_handle,
41*503a2b89SPeter Shoults 	gss_buffer_t interprocess_token)
42*503a2b89SPeter Shoults {
43*503a2b89SPeter Shoults 
44*503a2b89SPeter Shoults 	/* Initialize outputs. */
45*503a2b89SPeter Shoults 
46*503a2b89SPeter Shoults 	if (minor_status != NULL)
47*503a2b89SPeter Shoults 		*minor_status = 0;
48*503a2b89SPeter Shoults 
49*503a2b89SPeter Shoults 	if (interprocess_token != GSS_C_NO_BUFFER) {
50*503a2b89SPeter Shoults 		interprocess_token->length = 0;
51*503a2b89SPeter Shoults 		interprocess_token->value = NULL;
52*503a2b89SPeter Shoults 	}
53*503a2b89SPeter Shoults 
54*503a2b89SPeter Shoults 	/* Validate arguments. */
55*503a2b89SPeter Shoults 
56*503a2b89SPeter Shoults 	if (minor_status == NULL)
57*503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
58*503a2b89SPeter Shoults 
59*503a2b89SPeter Shoults 	if (context_handle == NULL || *context_handle == GSS_C_NO_CONTEXT)
60*503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
61*503a2b89SPeter Shoults 
62*503a2b89SPeter Shoults 	if (interprocess_token == GSS_C_NO_BUFFER)
63*503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
64*503a2b89SPeter Shoults 
65*503a2b89SPeter Shoults 	return (GSS_S_COMPLETE);
66*503a2b89SPeter Shoults }
67*503a2b89SPeter Shoults 
687c478bd9Sstevel@tonic-gate OM_uint32
697c478bd9Sstevel@tonic-gate gss_export_sec_context(minor_status,
707c478bd9Sstevel@tonic-gate 			context_handle,
717c478bd9Sstevel@tonic-gate 			interprocess_token)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
747c478bd9Sstevel@tonic-gate gss_ctx_id_t *context_handle;
757c478bd9Sstevel@tonic-gate gss_buffer_t interprocess_token;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	OM_uint32		status;
797c478bd9Sstevel@tonic-gate 	OM_uint32 		length;
807c478bd9Sstevel@tonic-gate 	gss_union_ctx_id_t	ctx;
817c478bd9Sstevel@tonic-gate 	gss_mechanism		mech;
827c478bd9Sstevel@tonic-gate 	gss_buffer_desc		token;
837c478bd9Sstevel@tonic-gate 	char			*buf;
847c478bd9Sstevel@tonic-gate 
85*503a2b89SPeter Shoults 	status = val_exp_sec_ctx_args(minor_status,
86*503a2b89SPeter Shoults 				context_handle, interprocess_token);
87*503a2b89SPeter Shoults 	if (status != GSS_S_COMPLETE)
88*503a2b89SPeter Shoults 		return (status);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * select the approprate underlying mechanism routine and
927c478bd9Sstevel@tonic-gate 	 * call it.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	ctx = (gss_union_ctx_id_t)*context_handle;
967c478bd9Sstevel@tonic-gate 	mech = __gss_get_mechanism(ctx->mech_type);
977c478bd9Sstevel@tonic-gate 	if (!mech)
987c478bd9Sstevel@tonic-gate 		return (GSS_S_BAD_MECH);
997c478bd9Sstevel@tonic-gate 	if (!mech->gss_export_sec_context)
1007c478bd9Sstevel@tonic-gate 		return (GSS_S_UNAVAILABLE);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	status = mech->gss_export_sec_context(mech->context, minor_status,
1037c478bd9Sstevel@tonic-gate 					&ctx->internal_ctx_id, &token);
1047c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE)
1057c478bd9Sstevel@tonic-gate 		return (status);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	length = token.length + 4 + ctx->mech_type->length;
1087c478bd9Sstevel@tonic-gate 	interprocess_token->length = length;
1097c478bd9Sstevel@tonic-gate 	interprocess_token->value = malloc(length);
1107c478bd9Sstevel@tonic-gate 	if (interprocess_token->value == 0) {
1117c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(minor_status, &token);
1127c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	buf = interprocess_token->value;
1157c478bd9Sstevel@tonic-gate 	length = ctx->mech_type->length;
1167c478bd9Sstevel@tonic-gate 	buf[3] = (unsigned char) (length & 0xFF);
1177c478bd9Sstevel@tonic-gate 	length >>= 8;
1187c478bd9Sstevel@tonic-gate 	buf[2] = (unsigned char) (length & 0xFF);
1197c478bd9Sstevel@tonic-gate 	length >>= 8;
1207c478bd9Sstevel@tonic-gate 	buf[1] = (unsigned char) (length & 0xFF);
1217c478bd9Sstevel@tonic-gate 	length >>= 8;
1227c478bd9Sstevel@tonic-gate 	buf[0] = (unsigned char) (length & 0xFF);
1237c478bd9Sstevel@tonic-gate 	(void) memcpy(buf+4, ctx->mech_type->elements,
1247c478bd9Sstevel@tonic-gate 			(size_t)ctx->mech_type->length);
1257c478bd9Sstevel@tonic-gate 	(void) memcpy(buf+4+ctx->mech_type->length, token.value, token.length);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(minor_status, &token);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	free(ctx->mech_type->elements);
1307c478bd9Sstevel@tonic-gate 	free(ctx->mech_type);
1317c478bd9Sstevel@tonic-gate 	free(ctx);
1327c478bd9Sstevel@tonic-gate 	*context_handle = 0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
1357c478bd9Sstevel@tonic-gate }
136