xref: /onnv-gate/usr/src/lib/libgss/g_store_cred.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 for gss_store_cred
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <mechglueP.h>
30*13132SGlenn.Barry@oracle.com #include "gssapiP_generic.h"
31*13132SGlenn.Barry@oracle.com #include <errno.h>
320Sstevel@tonic-gate 
33*13132SGlenn.Barry@oracle.com static OM_uint32
val_store_cred_args(OM_uint32 * minor_status,const gss_cred_id_t input_cred_handle,gss_cred_usage_t cred_usage,const gss_OID desired_mech,OM_uint32 overwrite_cred,OM_uint32 default_cred,gss_OID_set * elements_stored,gss_cred_usage_t * cred_usage_stored)34*13132SGlenn.Barry@oracle.com val_store_cred_args(
35*13132SGlenn.Barry@oracle.com         OM_uint32 *minor_status,
36*13132SGlenn.Barry@oracle.com         const gss_cred_id_t input_cred_handle,
37*13132SGlenn.Barry@oracle.com         gss_cred_usage_t cred_usage,
38*13132SGlenn.Barry@oracle.com 	/*LINTED*/
39*13132SGlenn.Barry@oracle.com         const gss_OID desired_mech,
40*13132SGlenn.Barry@oracle.com 	/*LINTED*/
41*13132SGlenn.Barry@oracle.com         OM_uint32 overwrite_cred,
42*13132SGlenn.Barry@oracle.com 	/*LINTED*/
43*13132SGlenn.Barry@oracle.com         OM_uint32 default_cred,
44*13132SGlenn.Barry@oracle.com         gss_OID_set *elements_stored,
45*13132SGlenn.Barry@oracle.com 	/*LINTED*/
46*13132SGlenn.Barry@oracle.com         gss_cred_usage_t *cred_usage_stored)
479698SPeter.Shoults@Sun.COM {
489698SPeter.Shoults@Sun.COM 
49*13132SGlenn.Barry@oracle.com         /* Initialize outputs. */
50*13132SGlenn.Barry@oracle.com 
51*13132SGlenn.Barry@oracle.com         if (minor_status != NULL)
52*13132SGlenn.Barry@oracle.com                 *minor_status = 0;
539698SPeter.Shoults@Sun.COM 
54*13132SGlenn.Barry@oracle.com         if (elements_stored != NULL)
55*13132SGlenn.Barry@oracle.com                 *elements_stored = GSS_C_NULL_OID_SET;
569698SPeter.Shoults@Sun.COM 
57*13132SGlenn.Barry@oracle.com        /* Validate arguments. */
58*13132SGlenn.Barry@oracle.com 
59*13132SGlenn.Barry@oracle.com         if (minor_status == NULL)
60*13132SGlenn.Barry@oracle.com                 return (GSS_S_CALL_INACCESSIBLE_WRITE);
619698SPeter.Shoults@Sun.COM 
62*13132SGlenn.Barry@oracle.com         if (input_cred_handle == GSS_C_NO_CREDENTIAL)
63*13132SGlenn.Barry@oracle.com                 return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CRED);
649698SPeter.Shoults@Sun.COM 
65*13132SGlenn.Barry@oracle.com 	if (cred_usage != GSS_C_ACCEPT
66*13132SGlenn.Barry@oracle.com 	    && cred_usage != GSS_C_INITIATE
67*13132SGlenn.Barry@oracle.com 	    && cred_usage != GSS_C_BOTH) {
68*13132SGlenn.Barry@oracle.com 		if (minor_status) {
69*13132SGlenn.Barry@oracle.com 			*minor_status = EINVAL;
70*13132SGlenn.Barry@oracle.com 			map_errcode(minor_status);
71*13132SGlenn.Barry@oracle.com 		}
72*13132SGlenn.Barry@oracle.com 		return GSS_S_FAILURE;
73*13132SGlenn.Barry@oracle.com 	}
749698SPeter.Shoults@Sun.COM 
759698SPeter.Shoults@Sun.COM 	return (GSS_S_COMPLETE);
769698SPeter.Shoults@Sun.COM }
779698SPeter.Shoults@Sun.COM 
gss_store_cred(minor_status,input_cred_handle,cred_usage,desired_mech,overwrite_cred,default_cred,elements_stored,cred_usage_stored)780Sstevel@tonic-gate OM_uint32 gss_store_cred(minor_status,
790Sstevel@tonic-gate 			input_cred_handle,
800Sstevel@tonic-gate 			cred_usage,
810Sstevel@tonic-gate 			desired_mech,
820Sstevel@tonic-gate 			overwrite_cred,
830Sstevel@tonic-gate 			default_cred,
840Sstevel@tonic-gate 			elements_stored,
850Sstevel@tonic-gate 			cred_usage_stored)
860Sstevel@tonic-gate 
870Sstevel@tonic-gate OM_uint32		*minor_status;
880Sstevel@tonic-gate const gss_cred_id_t	 input_cred_handle;
890Sstevel@tonic-gate gss_cred_usage_t	 cred_usage;
900Sstevel@tonic-gate const gss_OID		 desired_mech;
910Sstevel@tonic-gate OM_uint32		 overwrite_cred;
920Sstevel@tonic-gate OM_uint32		 default_cred;
930Sstevel@tonic-gate gss_OID_set		*elements_stored;
940Sstevel@tonic-gate gss_cred_usage_t	*cred_usage_stored;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	OM_uint32		major_status = GSS_S_FAILURE;
980Sstevel@tonic-gate 	gss_union_cred_t	union_cred;
990Sstevel@tonic-gate 	gss_cred_id_t		mech_cred;
1000Sstevel@tonic-gate 	gss_mechanism		mech;
1010Sstevel@tonic-gate 	gss_OID			dmech;
1020Sstevel@tonic-gate 	int			i;
1030Sstevel@tonic-gate 
104*13132SGlenn.Barry@oracle.com         major_status = val_store_cred_args(minor_status,
105*13132SGlenn.Barry@oracle.com                                            input_cred_handle,
106*13132SGlenn.Barry@oracle.com                                            cred_usage,
107*13132SGlenn.Barry@oracle.com                                            desired_mech,
108*13132SGlenn.Barry@oracle.com                                            overwrite_cred,
109*13132SGlenn.Barry@oracle.com                                            default_cred,
110*13132SGlenn.Barry@oracle.com                                            elements_stored,
111*13132SGlenn.Barry@oracle.com                                            cred_usage_stored);
1129698SPeter.Shoults@Sun.COM 	if (major_status != GSS_S_COMPLETE)
1139698SPeter.Shoults@Sun.COM 		return (major_status);
1140Sstevel@tonic-gate 
1159698SPeter.Shoults@Sun.COM 	/* Initial value needed below. */
1169698SPeter.Shoults@Sun.COM 		major_status = GSS_S_FAILURE;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (cred_usage_stored != NULL)
1190Sstevel@tonic-gate 		*cred_usage_stored = GSS_C_BOTH; /* there's no GSS_C_NEITHER */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	union_cred = (gss_union_cred_t)input_cred_handle;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/* desired_mech != GSS_C_NULL_OID -> store one element */
1240Sstevel@tonic-gate 	if (desired_mech != GSS_C_NULL_OID) {
1250Sstevel@tonic-gate 		mech = __gss_get_mechanism(desired_mech);
1260Sstevel@tonic-gate 		if (mech == NULL)
1270Sstevel@tonic-gate 			return (GSS_S_BAD_MECH);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		if (mech->gss_store_cred == NULL)
1300Sstevel@tonic-gate 			return (major_status);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		mech_cred = __gss_get_mechanism_cred(union_cred, desired_mech);
1330Sstevel@tonic-gate 		if (mech_cred == GSS_C_NO_CREDENTIAL)
1340Sstevel@tonic-gate 			return (GSS_S_NO_CRED);
1350Sstevel@tonic-gate 
136*13132SGlenn.Barry@oracle.com 		major_status = mech->gss_store_cred(mech->context,
1370Sstevel@tonic-gate 						minor_status,
1380Sstevel@tonic-gate 						(gss_cred_id_t)mech_cred,
1390Sstevel@tonic-gate 						cred_usage,
1400Sstevel@tonic-gate 						desired_mech,
1410Sstevel@tonic-gate 						overwrite_cred,
1420Sstevel@tonic-gate 						default_cred,
1430Sstevel@tonic-gate 						elements_stored,
144*13132SGlenn.Barry@oracle.com 						    cred_usage_stored);
145*13132SGlenn.Barry@oracle.com 		if (major_status != GSS_S_COMPLETE)
146*13132SGlenn.Barry@oracle.com 			map_error(minor_status, mech);
147*13132SGlenn.Barry@oracle.com 		return major_status;
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/* desired_mech == GSS_C_NULL_OID -> store all elements */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	*minor_status = 0;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	for (i = 0; i < union_cred->count; i++) {
1550Sstevel@tonic-gate 		/* Get mech and cred element */
1560Sstevel@tonic-gate 		dmech = &union_cred->mechs_array[i];
1570Sstevel@tonic-gate 		mech = __gss_get_mechanism(dmech);
1580Sstevel@tonic-gate 		if (mech == NULL)
1590Sstevel@tonic-gate 			continue;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		if (mech->gss_store_cred == NULL)
1620Sstevel@tonic-gate 			continue;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		mech_cred = __gss_get_mechanism_cred(union_cred, dmech);
1650Sstevel@tonic-gate 		if (mech_cred == GSS_C_NO_CREDENTIAL)
1660Sstevel@tonic-gate 			continue; /* can't happen, but safe to ignore */
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 		major_status = mech->gss_store_cred(mech->context,
1690Sstevel@tonic-gate 						minor_status,
1700Sstevel@tonic-gate 						(gss_cred_id_t)mech_cred,
1710Sstevel@tonic-gate 						cred_usage,
1720Sstevel@tonic-gate 						dmech,
1730Sstevel@tonic-gate 						overwrite_cred,
1740Sstevel@tonic-gate 						default_cred,
1750Sstevel@tonic-gate 						NULL,
1760Sstevel@tonic-gate 						cred_usage_stored);
177*13132SGlenn.Barry@oracle.com 		if (major_status != GSS_S_COMPLETE) {
178*13132SGlenn.Barry@oracle.com 			map_error(minor_status, mech);
1790Sstevel@tonic-gate 			continue;
180*13132SGlenn.Barry@oracle.com 		}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		/* Succeeded for at least one mech */
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		if (elements_stored == NULL)
1850Sstevel@tonic-gate 			continue;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		if (*elements_stored == GSS_C_NULL_OID_SET) {
1880Sstevel@tonic-gate 			major_status = gss_create_empty_oid_set(minor_status,
1890Sstevel@tonic-gate 						elements_stored);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 			if (GSS_ERROR(major_status))
1920Sstevel@tonic-gate 				return (major_status);
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		major_status = gss_add_oid_set_member(minor_status, dmech,
1960Sstevel@tonic-gate 			elements_stored);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		/* The caller should clean up elements_stored */
1990Sstevel@tonic-gate 		if (GSS_ERROR(major_status))
2000Sstevel@tonic-gate 			return (major_status);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/*
2040Sstevel@tonic-gate 	 * Success with some mechs may mask failure with others, but
2050Sstevel@tonic-gate 	 * that's what elements_stored is for.
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	return (major_status);
2080Sstevel@tonic-gate }
209