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
5503a2b89SPeter Shoults  * Common Development and Distribution License (the "License").
6503a2b89SPeter 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*5e01956fSGlenn Barry  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  *  glue routine gss_export_sec_context
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
29*5e01956fSGlenn Barry #ifndef LEAN_CLIENT
30*5e01956fSGlenn Barry 
317c478bd9Sstevel@tonic-gate #include <mechglueP.h>
32*5e01956fSGlenn Barry #include "gssapiP_generic.h"
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate 
38503a2b89SPeter Shoults static OM_uint32
val_imp_sec_ctx_args(OM_uint32 * minor_status,gss_buffer_t interprocess_token,gss_ctx_id_t * context_handle)39503a2b89SPeter Shoults val_imp_sec_ctx_args(
40503a2b89SPeter Shoults 	OM_uint32 *minor_status,
41503a2b89SPeter Shoults 	gss_buffer_t interprocess_token,
42503a2b89SPeter Shoults 	gss_ctx_id_t *context_handle)
43503a2b89SPeter Shoults {
44503a2b89SPeter Shoults 
45503a2b89SPeter Shoults 	/* Initialize outputs. */
46503a2b89SPeter Shoults 	if (minor_status != NULL)
47503a2b89SPeter Shoults 		*minor_status = 0;
48503a2b89SPeter Shoults 
49503a2b89SPeter Shoults 	if (context_handle != NULL)
50503a2b89SPeter Shoults 		*context_handle = GSS_C_NO_CONTEXT;
51503a2b89SPeter Shoults 
52503a2b89SPeter Shoults 	/* Validate arguments. */
53503a2b89SPeter Shoults 
54503a2b89SPeter Shoults 	if (minor_status == NULL)
55503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
56503a2b89SPeter Shoults 
57503a2b89SPeter Shoults 	if (context_handle == NULL)
58503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
59503a2b89SPeter Shoults 
60503a2b89SPeter Shoults 	if (interprocess_token == GSS_C_NO_BUFFER)
61503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
62503a2b89SPeter Shoults 
63503a2b89SPeter Shoults 	if (GSS_EMPTY_BUFFER(interprocess_token))
64503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
65503a2b89SPeter Shoults 
66503a2b89SPeter Shoults 	return (GSS_S_COMPLETE);
67503a2b89SPeter Shoults }
68503a2b89SPeter Shoults 
697c478bd9Sstevel@tonic-gate OM_uint32
gss_import_sec_context(minor_status,interprocess_token,context_handle)707c478bd9Sstevel@tonic-gate gss_import_sec_context(minor_status,
717c478bd9Sstevel@tonic-gate 			interprocess_token,
727c478bd9Sstevel@tonic-gate 			context_handle)
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate OM_uint32 *		minor_status;
757c478bd9Sstevel@tonic-gate const gss_buffer_t	interprocess_token;
767c478bd9Sstevel@tonic-gate gss_ctx_id_t 		*context_handle;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	OM_uint32		length = 0;
807c478bd9Sstevel@tonic-gate 	OM_uint32		status;
817c478bd9Sstevel@tonic-gate 	char			*p;
827c478bd9Sstevel@tonic-gate 	gss_union_ctx_id_t	ctx;
837c478bd9Sstevel@tonic-gate 	gss_buffer_desc		token;
847c478bd9Sstevel@tonic-gate 	gss_mechanism		mech;
857c478bd9Sstevel@tonic-gate 
86503a2b89SPeter Shoults 	status = val_imp_sec_ctx_args(minor_status,
87503a2b89SPeter Shoults 				interprocess_token, context_handle);
88503a2b89SPeter Shoults 	if (status != GSS_S_COMPLETE)
89503a2b89SPeter Shoults 		return (status);
907c478bd9Sstevel@tonic-gate 
91503a2b89SPeter Shoults 	/* Initial value needed below. */
927c478bd9Sstevel@tonic-gate 	status = GSS_S_FAILURE;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	ctx = (gss_union_ctx_id_t)malloc(sizeof (gss_union_ctx_id_desc));
957c478bd9Sstevel@tonic-gate 	if (!ctx)
967c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	ctx->mech_type = (gss_OID) malloc(sizeof (gss_OID_desc));
997c478bd9Sstevel@tonic-gate 	if (!ctx->mech_type) {
1007c478bd9Sstevel@tonic-gate 		free(ctx);
1017c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (interprocess_token->length >= sizeof (OM_uint32)) {
1057c478bd9Sstevel@tonic-gate 		p = interprocess_token->value;
1067c478bd9Sstevel@tonic-gate 		length = (OM_uint32)*p++;
1077c478bd9Sstevel@tonic-gate 		length = (OM_uint32)(length << 8) + *p++;
1087c478bd9Sstevel@tonic-gate 		length = (OM_uint32)(length << 8) + *p++;
1097c478bd9Sstevel@tonic-gate 		length = (OM_uint32)(length << 8) + *p++;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (length == 0 ||
1137c478bd9Sstevel@tonic-gate 	    length > (interprocess_token->length - sizeof (OM_uint32))) {
1147c478bd9Sstevel@tonic-gate 		free(ctx);
1157c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	ctx->mech_type->length = length;
1197c478bd9Sstevel@tonic-gate 	ctx->mech_type->elements = malloc(length);
1207c478bd9Sstevel@tonic-gate 	if (!ctx->mech_type->elements) {
1217c478bd9Sstevel@tonic-gate 		goto error_out;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	(void) memcpy(ctx->mech_type->elements, p, length);
1247c478bd9Sstevel@tonic-gate 	p += length;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	token.length = interprocess_token->length - sizeof (OM_uint32) - length;
1277c478bd9Sstevel@tonic-gate 	token.value = p;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * select the approprate underlying mechanism routine and
1317c478bd9Sstevel@tonic-gate 	 * call it.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	mech = __gss_get_mechanism(ctx->mech_type);
1357c478bd9Sstevel@tonic-gate 	if (!mech) {
1367c478bd9Sstevel@tonic-gate 		status = GSS_S_BAD_MECH;
1377c478bd9Sstevel@tonic-gate 		goto error_out;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (!mech->gss_import_sec_context) {
1407c478bd9Sstevel@tonic-gate 		status = GSS_S_UNAVAILABLE;
1417c478bd9Sstevel@tonic-gate 		goto error_out;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	status = mech->gss_import_sec_context(mech->context, minor_status,
1457c478bd9Sstevel@tonic-gate 					&token, &ctx->internal_ctx_id);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
1487c478bd9Sstevel@tonic-gate 		*context_handle = (gss_ctx_id_t)ctx;
1497c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
1507c478bd9Sstevel@tonic-gate 	}
151*5e01956fSGlenn Barry 	map_error(minor_status, mech);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate error_out:
1547c478bd9Sstevel@tonic-gate 	if (ctx) {
1557c478bd9Sstevel@tonic-gate 		if (ctx->mech_type) {
1567c478bd9Sstevel@tonic-gate 			if (ctx->mech_type->elements)
1577c478bd9Sstevel@tonic-gate 				free(ctx->mech_type->elements);
1587c478bd9Sstevel@tonic-gate 			free(ctx->mech_type);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		free(ctx);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	return (status);
1637c478bd9Sstevel@tonic-gate }
164*5e01956fSGlenn Barry #endif /* LEAN_CLIENT */
165