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 for gss_init_sec_context
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate #include <mechglueP.h>
29*5e01956fSGlenn Barry #include "gssapiP_generic.h"
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate 
34503a2b89SPeter Shoults static OM_uint32
val_init_sec_ctx_args(OM_uint32 * minor_status,gss_ctx_id_t * context_handle,gss_name_t target_name,gss_OID * actual_mech_type,gss_buffer_t output_token)35503a2b89SPeter Shoults val_init_sec_ctx_args(
36503a2b89SPeter Shoults 	OM_uint32 *minor_status,
37503a2b89SPeter Shoults 	gss_ctx_id_t *context_handle,
38503a2b89SPeter Shoults 	gss_name_t target_name,
39503a2b89SPeter Shoults 	gss_OID *actual_mech_type,
40503a2b89SPeter Shoults 	gss_buffer_t output_token)
41503a2b89SPeter Shoults {
42503a2b89SPeter Shoults 
43503a2b89SPeter Shoults 	/* Initialize outputs. */
44503a2b89SPeter Shoults 
45503a2b89SPeter Shoults 	if (minor_status != NULL)
46503a2b89SPeter Shoults 		*minor_status = 0;
47503a2b89SPeter Shoults 
48503a2b89SPeter Shoults 	if (actual_mech_type != NULL)
49503a2b89SPeter Shoults 		*actual_mech_type = GSS_C_NO_OID;
50503a2b89SPeter Shoults 
51503a2b89SPeter Shoults 	if (output_token != GSS_C_NO_BUFFER) {
52503a2b89SPeter Shoults 		output_token->length = 0;
53503a2b89SPeter Shoults 		output_token->value = NULL;
54503a2b89SPeter Shoults 	}
55503a2b89SPeter Shoults 
56503a2b89SPeter Shoults 	/* Validate arguments. */
57503a2b89SPeter Shoults 
58503a2b89SPeter Shoults 	if (minor_status == NULL)
59503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
60503a2b89SPeter Shoults 
61503a2b89SPeter Shoults 	if (context_handle == NULL)
62503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE | GSS_S_NO_CONTEXT);
63503a2b89SPeter Shoults 
64503a2b89SPeter Shoults 	if (target_name == NULL)
65503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
66503a2b89SPeter Shoults 
67503a2b89SPeter Shoults 	if (output_token == NULL)
68503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
69503a2b89SPeter Shoults 
70503a2b89SPeter Shoults 	return (GSS_S_COMPLETE);
71503a2b89SPeter Shoults }
72503a2b89SPeter Shoults 
737c478bd9Sstevel@tonic-gate OM_uint32
gss_init_sec_context(minor_status,claimant_cred_handle,context_handle,target_name,req_mech_type,req_flags,time_req,input_chan_bindings,input_token,actual_mech_type,output_token,ret_flags,time_rec)747c478bd9Sstevel@tonic-gate gss_init_sec_context(minor_status,
757c478bd9Sstevel@tonic-gate 			claimant_cred_handle,
767c478bd9Sstevel@tonic-gate 			context_handle,
777c478bd9Sstevel@tonic-gate 			target_name,
787c478bd9Sstevel@tonic-gate 			req_mech_type,
797c478bd9Sstevel@tonic-gate 			req_flags,
807c478bd9Sstevel@tonic-gate 			time_req,
817c478bd9Sstevel@tonic-gate 			input_chan_bindings,
827c478bd9Sstevel@tonic-gate 			input_token,
837c478bd9Sstevel@tonic-gate 			actual_mech_type,
847c478bd9Sstevel@tonic-gate 			output_token,
857c478bd9Sstevel@tonic-gate 			ret_flags,
867c478bd9Sstevel@tonic-gate 			time_rec)
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate OM_uint32 *			minor_status;
897c478bd9Sstevel@tonic-gate const gss_cred_id_t		claimant_cred_handle;
907c478bd9Sstevel@tonic-gate gss_ctx_id_t 			*context_handle;
917c478bd9Sstevel@tonic-gate const gss_name_t		target_name;
927c478bd9Sstevel@tonic-gate const gss_OID			req_mech_type;
937c478bd9Sstevel@tonic-gate OM_uint32			req_flags;
947c478bd9Sstevel@tonic-gate OM_uint32			time_req;
957c478bd9Sstevel@tonic-gate const gss_channel_bindings_t	input_chan_bindings;
967c478bd9Sstevel@tonic-gate const gss_buffer_t		input_token;
977c478bd9Sstevel@tonic-gate gss_OID *			actual_mech_type;
987c478bd9Sstevel@tonic-gate gss_buffer_t			output_token;
997c478bd9Sstevel@tonic-gate OM_uint32 *			ret_flags;
1007c478bd9Sstevel@tonic-gate OM_uint32 *			time_rec;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	OM_uint32		status, temp_minor_status;
1047c478bd9Sstevel@tonic-gate 	gss_union_name_t	union_name;
1057c478bd9Sstevel@tonic-gate 	gss_union_cred_t	union_cred;
1067c478bd9Sstevel@tonic-gate 	gss_name_t		internal_name;
1077c478bd9Sstevel@tonic-gate 	gss_union_ctx_id_t	union_ctx_id;
1087c478bd9Sstevel@tonic-gate 	gss_OID			mech_type = GSS_C_NULL_OID;
1097c478bd9Sstevel@tonic-gate 	gss_mechanism		mech;
1107c478bd9Sstevel@tonic-gate 	gss_cred_id_t		input_cred_handle;
1117c478bd9Sstevel@tonic-gate 
112503a2b89SPeter Shoults 	status = val_init_sec_ctx_args(minor_status,
113503a2b89SPeter Shoults 				context_handle,
114503a2b89SPeter Shoults 				target_name,
115503a2b89SPeter Shoults 				actual_mech_type,
116503a2b89SPeter Shoults 				output_token);
117503a2b89SPeter Shoults 	if (status != GSS_S_COMPLETE)
118503a2b89SPeter Shoults 		return (status);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (req_mech_type)
1217c478bd9Sstevel@tonic-gate 		mech_type = (gss_OID)req_mech_type;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	union_name = (gss_union_name_t)target_name;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * obtain the gss mechanism information for the requested
1277c478bd9Sstevel@tonic-gate 	 * mechanism.  If mech_type is NULL, set it to the resultant
1287c478bd9Sstevel@tonic-gate 	 * mechanism
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	mech = __gss_get_mechanism(mech_type);
1317c478bd9Sstevel@tonic-gate 	if (mech == NULL)
1327c478bd9Sstevel@tonic-gate 		return (GSS_S_BAD_MECH);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (mech->gss_init_sec_context == NULL)
1357c478bd9Sstevel@tonic-gate 		return (GSS_S_UNAVAILABLE);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (mech_type == GSS_C_NULL_OID)
1387c478bd9Sstevel@tonic-gate 		mech_type = &mech->mech_type;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * If target_name is mechanism_specific, then it must match the
1427c478bd9Sstevel@tonic-gate 	 * mech_type that we're about to use.  Otherwise, do an import on
1437c478bd9Sstevel@tonic-gate 	 * the external_name form of the target name.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	if (union_name->mech_type &&
1467c478bd9Sstevel@tonic-gate 			g_OID_equal(union_name->mech_type, mech_type)) {
1477c478bd9Sstevel@tonic-gate 		internal_name = union_name->mech_name;
1487c478bd9Sstevel@tonic-gate 	} else {
1497c478bd9Sstevel@tonic-gate 		if ((status = __gss_import_internal_name(minor_status,
1507c478bd9Sstevel@tonic-gate 					mech_type, union_name,
1517c478bd9Sstevel@tonic-gate 					&internal_name)) != GSS_S_COMPLETE)
1527c478bd9Sstevel@tonic-gate 			return (status);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * if context_handle is GSS_C_NO_CONTEXT, allocate a union context
1577c478bd9Sstevel@tonic-gate 	 * descriptor to hold the mech type information as well as the
1587c478bd9Sstevel@tonic-gate 	 * underlying mechanism context handle. Otherwise, cast the
1597c478bd9Sstevel@tonic-gate 	 * value of *context_handle to the union context variable.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (*context_handle == GSS_C_NO_CONTEXT) {
1627c478bd9Sstevel@tonic-gate 		status = GSS_S_FAILURE;
1637c478bd9Sstevel@tonic-gate 		union_ctx_id = (gss_union_ctx_id_t)
1647c478bd9Sstevel@tonic-gate 			malloc(sizeof (gss_union_ctx_id_desc));
1657c478bd9Sstevel@tonic-gate 		if (union_ctx_id == NULL)
1667c478bd9Sstevel@tonic-gate 			goto end;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		if (generic_gss_copy_oid(&temp_minor_status, mech_type,
1697c478bd9Sstevel@tonic-gate 				&union_ctx_id->mech_type) != GSS_S_COMPLETE) {
1707c478bd9Sstevel@tonic-gate 			free(union_ctx_id);
1717c478bd9Sstevel@tonic-gate 			goto end;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		/* copy the supplied context handle */
1757c478bd9Sstevel@tonic-gate 		union_ctx_id->internal_ctx_id = *context_handle;
1767c478bd9Sstevel@tonic-gate 	} else
1777c478bd9Sstevel@tonic-gate 		union_ctx_id = (gss_union_ctx_id_t)*context_handle;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * get the appropriate cred handle from the union cred struct.
1817c478bd9Sstevel@tonic-gate 	 * defaults to GSS_C_NO_CREDENTIAL if there is no cred, which will
1827c478bd9Sstevel@tonic-gate 	 * use the default credential.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	union_cred = (gss_union_cred_t)claimant_cred_handle;
1857c478bd9Sstevel@tonic-gate 	input_cred_handle = __gss_get_mechanism_cred(union_cred, mech_type);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * now call the approprate underlying mechanism routine
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	status = mech->gss_init_sec_context(
1927c478bd9Sstevel@tonic-gate 				mech->context,
1937c478bd9Sstevel@tonic-gate 				minor_status,
1947c478bd9Sstevel@tonic-gate 				input_cred_handle,
1957c478bd9Sstevel@tonic-gate 				&union_ctx_id->internal_ctx_id,
1967c478bd9Sstevel@tonic-gate 				internal_name,
1977c478bd9Sstevel@tonic-gate 				mech_type,
1987c478bd9Sstevel@tonic-gate 				req_flags,
1997c478bd9Sstevel@tonic-gate 				time_req,
2007c478bd9Sstevel@tonic-gate 				input_chan_bindings,
2017c478bd9Sstevel@tonic-gate 				input_token,
2027c478bd9Sstevel@tonic-gate 				actual_mech_type,
2037c478bd9Sstevel@tonic-gate 				output_token,
2047c478bd9Sstevel@tonic-gate 				ret_flags,
2057c478bd9Sstevel@tonic-gate 				time_rec);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE && status != GSS_S_CONTINUE_NEEDED) {
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * the spec says (the preferred) method is to delete all
2107c478bd9Sstevel@tonic-gate 		 * context info on the first call to init, and on all
2117c478bd9Sstevel@tonic-gate 		 * subsequent calls make the caller responsible for
2127c478bd9Sstevel@tonic-gate 		 * calling gss_delete_sec_context
2137c478bd9Sstevel@tonic-gate 		 */
214*5e01956fSGlenn Barry 		map_error(minor_status, mech);
2157c478bd9Sstevel@tonic-gate 		if (*context_handle == GSS_C_NO_CONTEXT) {
2167c478bd9Sstevel@tonic-gate 			free(union_ctx_id->mech_type->elements);
2177c478bd9Sstevel@tonic-gate 			free(union_ctx_id->mech_type);
2187c478bd9Sstevel@tonic-gate 			free(union_ctx_id);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	} else if (*context_handle == GSS_C_NO_CONTEXT)
2217c478bd9Sstevel@tonic-gate 		*context_handle = (gss_ctx_id_t)union_ctx_id;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate end:
2247c478bd9Sstevel@tonic-gate 	if (union_name->mech_name == NULL ||
2257c478bd9Sstevel@tonic-gate 		union_name->mech_name != internal_name) {
2267c478bd9Sstevel@tonic-gate 		(void) __gss_release_internal_name(&temp_minor_status,
2277c478bd9Sstevel@tonic-gate 					mech_type, &internal_name);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	return (status);
2317c478bd9Sstevel@tonic-gate }
232