xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_inq_names.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* #pragma ident	"@(#)g_inquire_names.c	1.16	04/02/23 SMI" */
2 
3 /*
4  * Copyright 1996 by Sun Microsystems, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appears in all copies and
9  * that both that copyright notice and this permission notice appear in
10  * supporting documentation, and that the name of Sun Microsystems not be used
11  * in advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission. Sun Microsystems makes no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
20  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24 
25 /*
26  *  glue routine for gss_inquire_context
27  */
28 
29 #include "mglueP.h"
30 
31 #define	MAX_MECH_OID_PAIRS 32
32 
33 /* Last argument new for V2 */
34 OM_uint32 KRB5_CALLCONV
gss_inquire_names_for_mech(OM_uint32 * minor_status,gss_OID mechanism,gss_OID_set * name_types)35 gss_inquire_names_for_mech(OM_uint32 *minor_status, gss_OID mechanism,
36 			   gss_OID_set *name_types)
37 {
38     OM_uint32		status;
39     gss_OID		selected_mech = GSS_C_NO_OID, public_mech;
40     gss_mechanism	mech;
41 
42     /* Initialize outputs. */
43 
44     if (minor_status != NULL)
45 	*minor_status = 0;
46 
47     if (name_types != NULL)
48 	*name_types = GSS_C_NO_OID_SET;
49 
50     /* Validate arguments. */
51 
52     if (minor_status == NULL)
53 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
54 
55     if (name_types == NULL)
56 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
57 
58     /*
59      * select the approprate underlying mechanism routine and
60      * call it.
61      */
62 
63     status = gssint_select_mech_type(minor_status, mechanism,
64 				     &selected_mech);
65     if (status != GSS_S_COMPLETE)
66 	return (status);
67 
68     mech = gssint_get_mechanism(selected_mech);
69     if (mech == NULL)
70 	return GSS_S_BAD_MECH;
71     else if (mech->gss_inquire_names_for_mech == NULL)
72 	return GSS_S_UNAVAILABLE;
73     public_mech = gssint_get_public_oid(selected_mech);
74     status = mech->gss_inquire_names_for_mech(minor_status, public_mech,
75 					      name_types);
76     if (status != GSS_S_COMPLETE)
77 	map_error(minor_status, mech);
78 
79     return status;
80 }
81 
82 static OM_uint32
val_inq_mechs4name_args(OM_uint32 * minor_status,const gss_name_t input_name,gss_OID_set * mech_set)83 val_inq_mechs4name_args(
84     OM_uint32 *minor_status,
85     const gss_name_t input_name,
86     gss_OID_set *mech_set)
87 {
88 
89     /* Initialize outputs. */
90     if (minor_status != NULL)
91 	*minor_status = 0;
92 
93     if (mech_set != NULL)
94 	*mech_set = GSS_C_NO_OID_SET;
95 
96     /* Validate arguments.e
97  */
98     if (minor_status == NULL)
99 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
100 
101     if (input_name == GSS_C_NO_NAME)
102 	return (GSS_S_BAD_NAME);
103 
104     return (GSS_S_COMPLETE);
105 }
106 
107 static int
mech_supports_nametype(gss_OID mech_oid,gss_OID name_type)108 mech_supports_nametype(gss_OID mech_oid, gss_OID name_type)
109 {
110     OM_uint32		status, minor;
111     gss_OID_set		types = GSS_C_NO_OID_SET;
112     int 		present;
113 
114     status = gss_inquire_names_for_mech(&minor, mech_oid, &types);
115     if (status != GSS_S_COMPLETE)
116 	return (0);
117     status = gss_test_oid_set_member(&minor, name_type, types, &present);
118     (void) gss_release_oid_set(&minor, &types);
119     return (status == GSS_S_COMPLETE && present);
120 }
121 
122 OM_uint32 KRB5_CALLCONV
gss_inquire_mechs_for_name(OM_uint32 * minor_status,const gss_name_t input_name,gss_OID_set * mech_set)123 gss_inquire_mechs_for_name(OM_uint32 *minor_status,
124 			   const gss_name_t input_name, gss_OID_set *mech_set)
125 {
126     OM_uint32		status, tmpmin;
127     gss_OID_set		all_mechs = GSS_C_NO_OID_SET;
128     gss_OID_set		mechs = GSS_C_NO_OID_SET;
129     gss_OID 		mech_oid, name_type;
130     gss_buffer_desc	name_buffer = GSS_C_EMPTY_BUFFER;
131     size_t		i;
132 
133     status = val_inq_mechs4name_args(minor_status, input_name, mech_set);
134     if (status != GSS_S_COMPLETE)
135 	return (status);
136 
137     status = gss_display_name(minor_status, input_name, &name_buffer,
138 			      &name_type);
139     if (status != GSS_S_COMPLETE)
140 	goto cleanup;
141     status = gss_indicate_mechs(minor_status, &all_mechs);
142     if (status != GSS_S_COMPLETE)
143 	goto cleanup;
144     status = gss_create_empty_oid_set(minor_status, &mechs);
145     if (status != GSS_S_COMPLETE)
146 	goto cleanup;
147     for (i = 0; i < all_mechs->count; i++) {
148 	mech_oid = &all_mechs->elements[i];
149 	if (mech_supports_nametype(mech_oid, name_type)) {
150 	    status = gss_add_oid_set_member(minor_status, mech_oid, &mechs);
151 	    if (status != GSS_S_COMPLETE)
152 		goto cleanup;
153 	}
154     }
155 
156     *mech_set = mechs;
157     mechs = GSS_C_NO_OID_SET;
158 
159 cleanup:
160     (void) gss_release_buffer(&tmpmin, &name_buffer);
161     (void) gss_release_oid_set(&tmpmin, &all_mechs);
162     (void) gss_release_oid_set(&tmpmin, &mechs);
163     return (status);
164 }
165