xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_imp_name.c (revision d0ff5773cefaf3fa41b1be3e44ca35bd9d5f68ee)
1 /* #pragma ident	"@(#)g_imp_name.c	1.26	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 gss_import_name
27  *
28  */
29 
30 #include "mglueP.h"
31 #include "k5-der.h"
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #include <string.h>
37 #include <errno.h>
38 
39 /* local function to import GSS_C_EXPORT_NAME names */
40 static OM_uint32 importExportName(OM_uint32 *, gss_union_name_t, gss_OID);
41 
42 static OM_uint32
43 val_imp_name_args(
44     OM_uint32 *minor_status,
45     gss_buffer_t input_name_buffer,
46     gss_OID input_name_type,
47     gss_name_t *output_name)
48 {
49 
50     /* Initialize outputs. */
51 
52     if (minor_status != NULL)
53 	*minor_status = 0;
54 
55     if (output_name != NULL)
56 	*output_name = GSS_C_NO_NAME;
57 
58     /* Validate arguments. */
59 
60     if (minor_status == NULL)
61 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
62 
63     if (output_name == NULL)
64 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
65 
66     if (input_name_buffer == GSS_C_NO_BUFFER)
67 	return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
68 
69     if (input_name_type == GSS_C_NO_OID ||
70 	!g_OID_equal(input_name_type, GSS_C_NT_ANONYMOUS)) {
71 	if (input_name_buffer->length == 0)
72 	    return (GSS_S_BAD_NAME);
73 
74 	if (input_name_buffer->value == NULL)
75 	    return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
76     }
77 
78     return (GSS_S_COMPLETE);
79 }
80 
81 static gss_buffer_desc emptyNameBuffer;
82 
83 OM_uint32 KRB5_CALLCONV
84 gss_import_name(minor_status,
85                 input_name_buffer,
86                 input_name_type,
87                 output_name)
88 
89 OM_uint32 *		minor_status;
90 gss_buffer_t		input_name_buffer;
91 gss_OID			input_name_type;
92 gss_name_t *		output_name;
93 
94 {
95     gss_union_name_t	union_name;
96     OM_uint32		tmp, major_status = GSS_S_FAILURE;
97 
98     if (input_name_buffer == GSS_C_NO_BUFFER)
99 	input_name_buffer = &emptyNameBuffer;
100 
101     major_status = val_imp_name_args(minor_status,
102 				     input_name_buffer, input_name_type,
103 				     output_name);
104     if (major_status != GSS_S_COMPLETE)
105 	return (major_status);
106 
107     /*
108      * First create the union name struct that will hold the external
109      * name and the name type.
110      */
111     union_name = (gss_union_name_t) malloc (sizeof(gss_union_name_desc));
112     if (!union_name)
113 	return (GSS_S_FAILURE);
114 
115     union_name->loopback = 0;
116     union_name->mech_type = 0;
117     union_name->mech_name = 0;
118     union_name->name_type = 0;
119     union_name->external_name = 0;
120 
121     /*
122      * All we do here is record the external name and name_type.
123      * When the name is actually used, the underlying gss_import_name()
124      * is called for the appropriate mechanism.  The exception to this
125      * rule is when the name of GSS_C_NT_EXPORT_NAME type.  If that is
126      * the case, then we make it MN in this call.
127      */
128     major_status = gssint_create_copy_buffer(input_name_buffer,
129 					    &union_name->external_name, 0);
130     if (major_status != GSS_S_COMPLETE) {
131 	free(union_name);
132 	return (major_status);
133     }
134 
135     if (input_name_type != GSS_C_NULL_OID) {
136 	major_status = generic_gss_copy_oid(minor_status,
137 					    input_name_type,
138 					    &union_name->name_type);
139 	if (major_status != GSS_S_COMPLETE) {
140 	    map_errcode(minor_status);
141 	    goto allocation_failure;
142 	}
143     }
144 
145     /*
146      * In MIT Distribution the mechanism is determined from the nametype;
147      * This is not a good idea - first mechanism that supports a given
148      * name type is picked up; later on the caller can request a
149      * different mechanism. So we don't determine the mechanism here. Now
150      * the user level and kernel level import_name routine looks similar
151      * except the kernel routine makes a copy of the nametype structure. We
152      * do however make this an MN for names of GSS_C_NT_EXPORT_NAME type.
153      */
154     if (input_name_type != GSS_C_NULL_OID &&
155 	(g_OID_equal(input_name_type, GSS_C_NT_EXPORT_NAME) ||
156 	 g_OID_equal(input_name_type, GSS_C_NT_COMPOSITE_EXPORT))) {
157 	major_status = importExportName(minor_status, union_name, input_name_type);
158 	if (major_status != GSS_S_COMPLETE)
159 	    goto allocation_failure;
160     }
161 
162     union_name->loopback = union_name;
163     *output_name = (gss_name_t)union_name;
164     return (GSS_S_COMPLETE);
165 
166 allocation_failure:
167     if (union_name) {
168 	if (union_name->external_name) {
169 	    if (union_name->external_name->value)
170 		free(union_name->external_name->value);
171 	    free(union_name->external_name);
172 	}
173 	if (union_name->name_type)
174 	    generic_gss_release_oid(&tmp, &union_name->name_type);
175 	if (union_name->mech_name)
176 	    gssint_release_internal_name(minor_status, union_name->mech_type,
177 					&union_name->mech_name);
178 	if (union_name->mech_type)
179 	    generic_gss_release_oid(&tmp, &union_name->mech_type);
180 	free(union_name);
181     }
182     return (major_status);
183 }
184 
185 static OM_uint32
186 importExportName(minor, unionName, inputNameType)
187     OM_uint32 *minor;
188     gss_union_name_t unionName;
189     gss_OID inputNameType;
190 {
191     gss_OID_desc mechOid;
192     gss_buffer_desc expName;
193     gss_mechanism mech;
194     OM_uint32 major, mechOidLen, nameLen;
195     uint8_t b2;
196     const uint8_t *name;
197     struct k5input in, oid, old_format;
198 
199     expName.value = unionName->external_name->value;
200     expName.length = unionName->external_name->length;
201     k5_input_init(&in, expName.value, expName.length);
202 
203     if (k5_input_get_byte(&in) != 0x04)
204 	return (GSS_S_DEFECTIVE_TOKEN);
205     b2 = k5_input_get_byte(&in);
206     if (b2 != 0x01 && b2 != 0x02) /* allow composite names */
207 	return (GSS_S_DEFECTIVE_TOKEN);
208 
209     mechOidLen = k5_input_get_uint16_be(&in);
210 
211     if (!k5_der_get_value(&in, 0x06, &oid))
212 	return (GSS_S_DEFECTIVE_TOKEN);
213     /* Verify that mechOidLen is consistent with the DER OID length. */
214     if (mechOidLen != k5_der_value_len(oid.len))
215 	return (GSS_S_DEFECTIVE_TOKEN);
216     mechOid.length = oid.len;
217     mechOid.elements = (uint8_t *)oid.ptr;
218     if ((mech = gssint_get_mechanism(&mechOid)) == NULL)
219 	return (GSS_S_BAD_MECH);
220 
221     if (mech->gssspi_import_name_by_mech == NULL &&
222 	mech->gss_import_name == NULL)
223 	return (GSS_S_UNAVAILABLE);
224 
225     /*
226      * we must now determine if we should unwrap the name ourselves
227      * or make the mechanism do it - we should only unwrap it
228      * if we create it; so if mech->gss_export_name == NULL, we must
229      * have created it.
230      */
231     if (mech->gss_export_name) {
232 	if (mech->gssspi_import_name_by_mech) {
233 	    major = mech->gssspi_import_name_by_mech(minor, &mechOid, &expName,
234 						     inputNameType,
235 						     &unionName->mech_name);
236 	} else {
237 	    major = mech->gss_import_name(minor, &expName, inputNameType,
238 					  &unionName->mech_name);
239 	}
240 	if (major != GSS_S_COMPLETE)
241 	    map_error(minor, mech);
242 	else {
243 	    major = generic_gss_copy_oid(minor, &mechOid,
244 					 &unionName->mech_type);
245 	    if (major != GSS_S_COMPLETE)
246 		map_errcode(minor);
247 	}
248 	return (major);
249     }
250     /*
251      * we must have exported the name - so we now need to reconstruct it
252      * and call the mechanism to create it
253      *
254      * WARNING:	Older versions of gssint_export_internal_name() did
255      *		not export names correctly, but now it does.  In
256      *		order to stay compatible with existing exported
257      *		names we must support names exported the broken
258      *		way.
259      *
260      * Specifically, gssint_export_internal_name() used to include
261      * the name type OID in the encoding of the exported MN.
262      * Additionally, the Kerberos V mech used to make display names
263      * that included a null terminator which was counted in the
264      * display name gss_buffer_desc.
265      */
266 
267     /* next 4 bytes in the name are the name length */
268     nameLen = k5_input_get_uint32_be(&in);
269     name = k5_input_get_bytes(&in, nameLen);
270     if (name == NULL)
271 	return (GSS_S_DEFECTIVE_TOKEN);
272 
273     /*
274      * We detect broken exported names here: they always start with
275      * a two-octet network-byte order OID length, which is always
276      * less than 256 bytes, so the first octet of the length is
277      * always '\0', which is not allowed in GSS-API display names
278      * (or never occurs in them anyways).  Of course, the OID
279      * shouldn't be there, but it is.  After the OID (sans DER tag
280      * and length) there's the name itself, though null-terminated;
281      * this null terminator should also not be there, but it is.
282      */
283     if (nameLen > 0 && *name == '\0') {
284 	OM_uint32 nameTypeLen;
285 
286 	/* Skip the name type. */
287 	k5_input_init(&old_format, name, nameLen);
288 	nameTypeLen = k5_input_get_uint16_be(&old_format);
289 	if (k5_input_get_bytes(&old_format, nameTypeLen) == NULL)
290 	    return (GSS_S_DEFECTIVE_TOKEN);
291 	/* Remove a null terminator if one is present. */
292 	if (old_format.len > 0 && old_format.ptr[old_format.len - 1] == 0)
293 	    old_format.len--;
294 	name = old_format.ptr;
295 	nameLen = old_format.len;
296     }
297 
298     /*
299      * Can a name be null?  Let the mech decide.
300      *
301      * NOTE: We use GSS_C_NULL_OID as the name type when importing
302      *	 the unwrapped name.  Presumably the exported name had,
303      *	 prior to being exported been obtained in such a way
304      *	 that it has been properly perpared ("canonicalized," in
305      *	 GSS-API terms) according to some name type; we cannot
306      *	 tell what that name type was now, but the name should
307      *	 need no further preparation other than the lowest
308      *	 common denominator afforded by the mech to names
309      *	 imported with GSS_C_NULL_OID.  For the Kerberos V mech
310      *	 this means doing less busywork too (particularly once
311      *	 IDN is thrown in with Kerberos V extensions).
312      */
313     expName.length = nameLen;
314     expName.value = nameLen ? (uint8_t *)name : NULL;
315     if (mech->gssspi_import_name_by_mech) {
316 	major = mech->gssspi_import_name_by_mech(minor, &mechOid, &expName,
317 						 GSS_C_NULL_OID,
318 						 &unionName->mech_name);
319     } else {
320 	major = mech->gss_import_name(minor, &expName,
321 				      GSS_C_NULL_OID, &unionName->mech_name);
322     }
323     if (major != GSS_S_COMPLETE) {
324 	map_error(minor, mech);
325 	return (major);
326     }
327 
328     major = generic_gss_copy_oid(minor, &mechOid, &unionName->mech_type);
329     if (major != GSS_S_COMPLETE) {
330 	map_errcode(minor);
331     }
332     return major;
333 } /* importExportName */
334