xref: /onnv-gate/usr/src/lib/libgss/g_imp_sec_context.c (revision 13132:9615cdbf7b70)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59698SPeter.Shoults@Sun.COM  * Common Development and Distribution License (the "License").
69698SPeter.Shoults@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*13132SGlenn.Barry@oracle.com  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  *  glue routine gss_export_sec_context
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
29*13132SGlenn.Barry@oracle.com #ifndef LEAN_CLIENT
30*13132SGlenn.Barry@oracle.com 
310Sstevel@tonic-gate #include <mechglueP.h>
32*13132SGlenn.Barry@oracle.com #include "gssapiP_generic.h"
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate 
389698SPeter.Shoults@Sun.COM static OM_uint32
val_imp_sec_ctx_args(OM_uint32 * minor_status,gss_buffer_t interprocess_token,gss_ctx_id_t * context_handle)399698SPeter.Shoults@Sun.COM val_imp_sec_ctx_args(
409698SPeter.Shoults@Sun.COM 	OM_uint32 *minor_status,
419698SPeter.Shoults@Sun.COM 	gss_buffer_t interprocess_token,
429698SPeter.Shoults@Sun.COM 	gss_ctx_id_t *context_handle)
439698SPeter.Shoults@Sun.COM {
449698SPeter.Shoults@Sun.COM 
459698SPeter.Shoults@Sun.COM 	/* Initialize outputs. */
469698SPeter.Shoults@Sun.COM 	if (minor_status != NULL)
479698SPeter.Shoults@Sun.COM 		*minor_status = 0;
489698SPeter.Shoults@Sun.COM 
499698SPeter.Shoults@Sun.COM 	if (context_handle != NULL)
509698SPeter.Shoults@Sun.COM 		*context_handle = GSS_C_NO_CONTEXT;
519698SPeter.Shoults@Sun.COM 
529698SPeter.Shoults@Sun.COM 	/* Validate arguments. */
539698SPeter.Shoults@Sun.COM 
549698SPeter.Shoults@Sun.COM 	if (minor_status == NULL)
559698SPeter.Shoults@Sun.COM 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
569698SPeter.Shoults@Sun.COM 
579698SPeter.Shoults@Sun.COM 	if (context_handle == NULL)
589698SPeter.Shoults@Sun.COM 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
599698SPeter.Shoults@Sun.COM 
609698SPeter.Shoults@Sun.COM 	if (interprocess_token == GSS_C_NO_BUFFER)
619698SPeter.Shoults@Sun.COM 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
629698SPeter.Shoults@Sun.COM 
639698SPeter.Shoults@Sun.COM 	if (GSS_EMPTY_BUFFER(interprocess_token))
649698SPeter.Shoults@Sun.COM 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
659698SPeter.Shoults@Sun.COM 
669698SPeter.Shoults@Sun.COM 	return (GSS_S_COMPLETE);
679698SPeter.Shoults@Sun.COM }
689698SPeter.Shoults@Sun.COM 
690Sstevel@tonic-gate OM_uint32
gss_import_sec_context(minor_status,interprocess_token,context_handle)700Sstevel@tonic-gate gss_import_sec_context(minor_status,
710Sstevel@tonic-gate 			interprocess_token,
720Sstevel@tonic-gate 			context_handle)
730Sstevel@tonic-gate 
740Sstevel@tonic-gate OM_uint32 *		minor_status;
750Sstevel@tonic-gate const gss_buffer_t	interprocess_token;
760Sstevel@tonic-gate gss_ctx_id_t 		*context_handle;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	OM_uint32		length = 0;
800Sstevel@tonic-gate 	OM_uint32		status;
810Sstevel@tonic-gate 	char			*p;
820Sstevel@tonic-gate 	gss_union_ctx_id_t	ctx;
830Sstevel@tonic-gate 	gss_buffer_desc		token;
840Sstevel@tonic-gate 	gss_mechanism		mech;
850Sstevel@tonic-gate 
869698SPeter.Shoults@Sun.COM 	status = val_imp_sec_ctx_args(minor_status,
879698SPeter.Shoults@Sun.COM 				interprocess_token, context_handle);
889698SPeter.Shoults@Sun.COM 	if (status != GSS_S_COMPLETE)
899698SPeter.Shoults@Sun.COM 		return (status);
900Sstevel@tonic-gate 
919698SPeter.Shoults@Sun.COM 	/* Initial value needed below. */
920Sstevel@tonic-gate 	status = GSS_S_FAILURE;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	ctx = (gss_union_ctx_id_t)malloc(sizeof (gss_union_ctx_id_desc));
950Sstevel@tonic-gate 	if (!ctx)
960Sstevel@tonic-gate 		return (GSS_S_FAILURE);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	ctx->mech_type = (gss_OID) malloc(sizeof (gss_OID_desc));
990Sstevel@tonic-gate 	if (!ctx->mech_type) {
1000Sstevel@tonic-gate 		free(ctx);
1010Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (interprocess_token->length >= sizeof (OM_uint32)) {
1050Sstevel@tonic-gate 		p = interprocess_token->value;
1060Sstevel@tonic-gate 		length = (OM_uint32)*p++;
1070Sstevel@tonic-gate 		length = (OM_uint32)(length << 8) + *p++;
1080Sstevel@tonic-gate 		length = (OM_uint32)(length << 8) + *p++;
1090Sstevel@tonic-gate 		length = (OM_uint32)(length << 8) + *p++;
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (length == 0 ||
1130Sstevel@tonic-gate 	    length > (interprocess_token->length - sizeof (OM_uint32))) {
1140Sstevel@tonic-gate 		free(ctx);
1150Sstevel@tonic-gate 		return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	ctx->mech_type->length = length;
1190Sstevel@tonic-gate 	ctx->mech_type->elements = malloc(length);
1200Sstevel@tonic-gate 	if (!ctx->mech_type->elements) {
1210Sstevel@tonic-gate 		goto error_out;
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	(void) memcpy(ctx->mech_type->elements, p, length);
1240Sstevel@tonic-gate 	p += length;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	token.length = interprocess_token->length - sizeof (OM_uint32) - length;
1270Sstevel@tonic-gate 	token.value = p;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/*
1300Sstevel@tonic-gate 	 * select the approprate underlying mechanism routine and
1310Sstevel@tonic-gate 	 * call it.
1320Sstevel@tonic-gate 	 */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	mech = __gss_get_mechanism(ctx->mech_type);
1350Sstevel@tonic-gate 	if (!mech) {
1360Sstevel@tonic-gate 		status = GSS_S_BAD_MECH;
1370Sstevel@tonic-gate 		goto error_out;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	if (!mech->gss_import_sec_context) {
1400Sstevel@tonic-gate 		status = GSS_S_UNAVAILABLE;
1410Sstevel@tonic-gate 		goto error_out;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	status = mech->gss_import_sec_context(mech->context, minor_status,
1450Sstevel@tonic-gate 					&token, &ctx->internal_ctx_id);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
1480Sstevel@tonic-gate 		*context_handle = (gss_ctx_id_t)ctx;
1490Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
1500Sstevel@tonic-gate 	}
151*13132SGlenn.Barry@oracle.com 	map_error(minor_status, mech);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate error_out:
1540Sstevel@tonic-gate 	if (ctx) {
1550Sstevel@tonic-gate 		if (ctx->mech_type) {
1560Sstevel@tonic-gate 			if (ctx->mech_type->elements)
1570Sstevel@tonic-gate 				free(ctx->mech_type->elements);
1580Sstevel@tonic-gate 			free(ctx->mech_type);
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 		free(ctx);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 	return (status);
1630Sstevel@tonic-gate }
164*13132SGlenn.Barry@oracle.com #endif /* LEAN_CLIENT */
165