xref: /freebsd/crypto/heimdal/lib/gssapi/mech/gss_import_name.c (revision ed549cb0c53f8438c52593ce811f6fcc812248e9)
1ae771770SStanislav Sedov /*-
2ae771770SStanislav Sedov  * Copyright (c) 2005 Doug Rabson
3ae771770SStanislav Sedov  * All rights reserved.
4ae771770SStanislav Sedov  *
5ae771770SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
6ae771770SStanislav Sedov  * modification, are permitted provided that the following conditions
7ae771770SStanislav Sedov  * are met:
8ae771770SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
9ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
10ae771770SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
11ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
12ae771770SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
13ae771770SStanislav Sedov  *
14ae771770SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ae771770SStanislav Sedov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ae771770SStanislav Sedov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ae771770SStanislav Sedov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ae771770SStanislav Sedov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ae771770SStanislav Sedov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ae771770SStanislav Sedov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ae771770SStanislav Sedov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ae771770SStanislav Sedov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ae771770SStanislav Sedov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ae771770SStanislav Sedov  * SUCH DAMAGE.
25ae771770SStanislav Sedov  *
26ae771770SStanislav Sedov  *	$FreeBSD: src/lib/libgssapi/gss_import_name.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27ae771770SStanislav Sedov  */
28ae771770SStanislav Sedov 
29ae771770SStanislav Sedov #include "mech_locl.h"
30ae771770SStanislav Sedov 
31ae771770SStanislav Sedov static OM_uint32
_gss_import_export_name(OM_uint32 * minor_status,const gss_buffer_t input_name_buffer,gss_name_t * output_name)32ae771770SStanislav Sedov _gss_import_export_name(OM_uint32 *minor_status,
33ae771770SStanislav Sedov     const gss_buffer_t input_name_buffer,
34ae771770SStanislav Sedov     gss_name_t *output_name)
35ae771770SStanislav Sedov {
36ae771770SStanislav Sedov 	OM_uint32 major_status;
37ae771770SStanislav Sedov 	unsigned char *p = input_name_buffer->value;
38ae771770SStanislav Sedov 	size_t len = input_name_buffer->length;
39ae771770SStanislav Sedov 	size_t t;
40ae771770SStanislav Sedov 	gss_OID_desc mech_oid;
41ae771770SStanislav Sedov 	gssapi_mech_interface m;
42ae771770SStanislav Sedov 	struct _gss_name *name;
43ae771770SStanislav Sedov 	gss_name_t new_canonical_name;
44ae771770SStanislav Sedov 	int composite = 0;
45ae771770SStanislav Sedov 
46ae771770SStanislav Sedov 	*minor_status = 0;
47ae771770SStanislav Sedov 	*output_name = 0;
48ae771770SStanislav Sedov 
49ae771770SStanislav Sedov 	/*
50ae771770SStanislav Sedov 	 * Make sure that TOK_ID is {4, 1}.
51ae771770SStanislav Sedov 	 */
52ae771770SStanislav Sedov 	if (len < 2)
53ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
54ae771770SStanislav Sedov 	if (p[0] != 4)
55ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
56ae771770SStanislav Sedov 	switch (p[1]) {
57ae771770SStanislav Sedov 	case 1:	/* non-composite name */
58ae771770SStanislav Sedov 		break;
59ae771770SStanislav Sedov 	case 2:	/* composite name */
60ae771770SStanislav Sedov 		composite = 1;
61ae771770SStanislav Sedov 		break;
62ae771770SStanislav Sedov 	default:
63ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
64ae771770SStanislav Sedov 	}
65ae771770SStanislav Sedov 	p += 2;
66ae771770SStanislav Sedov 	len -= 2;
67ae771770SStanislav Sedov 
68ae771770SStanislav Sedov 	/*
69ae771770SStanislav Sedov 	 * Get the mech length and the name length and sanity
70ae771770SStanislav Sedov 	 * check the size of of the buffer.
71ae771770SStanislav Sedov 	 */
72ae771770SStanislav Sedov 	if (len < 2)
73ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
74ae771770SStanislav Sedov 	t = (p[0] << 8) + p[1];
75ae771770SStanislav Sedov 	p += 2;
76ae771770SStanislav Sedov 	len -= 2;
77ae771770SStanislav Sedov 
78ae771770SStanislav Sedov 	/*
79ae771770SStanislav Sedov 	 * Check the DER encoded OID to make sure it agrees with the
80ae771770SStanislav Sedov 	 * length we just decoded.
81ae771770SStanislav Sedov 	 */
82ae771770SStanislav Sedov 	if (p[0] != 6)		/* 6=OID */
83ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
84ae771770SStanislav Sedov 	p++;
85ae771770SStanislav Sedov 	len--;
86ae771770SStanislav Sedov 	t--;
87ae771770SStanislav Sedov 	if (p[0] & 0x80) {
88ae771770SStanislav Sedov 		int digits = p[0];
89ae771770SStanislav Sedov 		p++;
90ae771770SStanislav Sedov 		len--;
91ae771770SStanislav Sedov 		t--;
92ae771770SStanislav Sedov 		mech_oid.length = 0;
93ae771770SStanislav Sedov 		while (digits--) {
94ae771770SStanislav Sedov 			mech_oid.length = (mech_oid.length << 8) | p[0];
95ae771770SStanislav Sedov 			p++;
96ae771770SStanislav Sedov 			len--;
97ae771770SStanislav Sedov 			t--;
98ae771770SStanislav Sedov 		}
99ae771770SStanislav Sedov 	} else {
100ae771770SStanislav Sedov 		mech_oid.length = p[0];
101ae771770SStanislav Sedov 		p++;
102ae771770SStanislav Sedov 		len--;
103ae771770SStanislav Sedov 		t--;
104ae771770SStanislav Sedov 	}
105ae771770SStanislav Sedov 	if (mech_oid.length != t)
106ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
107ae771770SStanislav Sedov 
108ae771770SStanislav Sedov 	mech_oid.elements = p;
109ae771770SStanislav Sedov 
110ae771770SStanislav Sedov 	if (len < t + 4)
111ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
112ae771770SStanislav Sedov 	p += t;
113ae771770SStanislav Sedov 	len -= t;
114ae771770SStanislav Sedov 
115ae771770SStanislav Sedov 	t = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
116*ed549cb0SCy Schubert 	/* p += 4; */
117ae771770SStanislav Sedov 	len -= 4;
118ae771770SStanislav Sedov 
119ae771770SStanislav Sedov 	if (!composite && len != t)
120ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
121ae771770SStanislav Sedov 
122ae771770SStanislav Sedov 	m = __gss_get_mechanism(&mech_oid);
123ae771770SStanislav Sedov 	if (!m)
124ae771770SStanislav Sedov 		return (GSS_S_BAD_MECH);
125ae771770SStanislav Sedov 
126ae771770SStanislav Sedov 	/*
127ae771770SStanislav Sedov 	 * Ask the mechanism to import the name.
128ae771770SStanislav Sedov 	 */
129ae771770SStanislav Sedov 	major_status = m->gm_import_name(minor_status,
130ae771770SStanislav Sedov 	    input_name_buffer, GSS_C_NT_EXPORT_NAME, &new_canonical_name);
131ae771770SStanislav Sedov 	if (major_status != GSS_S_COMPLETE) {
132ae771770SStanislav Sedov 		_gss_mg_error(m, major_status, *minor_status);
133ae771770SStanislav Sedov 		return major_status;
134ae771770SStanislav Sedov 	}
135ae771770SStanislav Sedov 
136ae771770SStanislav Sedov 	/*
137ae771770SStanislav Sedov 	 * Now we make a new name and mark it as an MN.
138ae771770SStanislav Sedov 	 */
139ae771770SStanislav Sedov 	name = _gss_make_name(m, new_canonical_name);
140ae771770SStanislav Sedov 	if (!name) {
141ae771770SStanislav Sedov 		m->gm_release_name(minor_status, &new_canonical_name);
142ae771770SStanislav Sedov 		return (GSS_S_FAILURE);
143ae771770SStanislav Sedov 	}
144ae771770SStanislav Sedov 
145ae771770SStanislav Sedov 	*output_name = (gss_name_t) name;
146ae771770SStanislav Sedov 
147ae771770SStanislav Sedov 	*minor_status = 0;
148ae771770SStanislav Sedov 	return (GSS_S_COMPLETE);
149ae771770SStanislav Sedov }
150ae771770SStanislav Sedov 
151ae771770SStanislav Sedov /**
152ae771770SStanislav Sedov  * Import a name internal or mechanism name
153ae771770SStanislav Sedov  *
154ae771770SStanislav Sedov  * Type of name and their format:
155ae771770SStanislav Sedov  * - GSS_C_NO_OID
156ae771770SStanislav Sedov  * - GSS_C_NT_USER_NAME
157ae771770SStanislav Sedov  * - GSS_C_NT_HOSTBASED_SERVICE
158ae771770SStanislav Sedov  * - GSS_C_NT_EXPORT_NAME
159ae771770SStanislav Sedov  * - GSS_C_NT_ANONYMOUS
160ae771770SStanislav Sedov  * - GSS_KRB5_NT_PRINCIPAL_NAME
161ae771770SStanislav Sedov  *
162ae771770SStanislav Sedov  * For more information about @ref internalVSmechname.
163ae771770SStanislav Sedov  *
164ae771770SStanislav Sedov  * @param minor_status minor status code
165ae771770SStanislav Sedov  * @param input_name_buffer import name buffer
166ae771770SStanislav Sedov  * @param input_name_type type of the import name buffer
167ae771770SStanislav Sedov  * @param output_name the resulting type, release with
168ae771770SStanislav Sedov  *        gss_release_name(), independent of input_name
169ae771770SStanislav Sedov  *
170ae771770SStanislav Sedov  * @returns a gss_error code, see gss_display_status() about printing
171ae771770SStanislav Sedov  *        the error code.
172ae771770SStanislav Sedov  *
173ae771770SStanislav Sedov  * @ingroup gssapi
174ae771770SStanislav Sedov  */
175ae771770SStanislav Sedov 
176ae771770SStanislav Sedov GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
gss_import_name(OM_uint32 * minor_status,const gss_buffer_t input_name_buffer,const gss_OID input_name_type,gss_name_t * output_name)177ae771770SStanislav Sedov gss_import_name(OM_uint32 *minor_status,
178ae771770SStanislav Sedov     const gss_buffer_t input_name_buffer,
179ae771770SStanislav Sedov     const gss_OID input_name_type,
180ae771770SStanislav Sedov     gss_name_t *output_name)
181ae771770SStanislav Sedov {
182ae771770SStanislav Sedov         struct _gss_mechanism_name *mn;
183ae771770SStanislav Sedov 	gss_OID			name_type = input_name_type;
184ae771770SStanislav Sedov 	OM_uint32		major_status, ms;
185ae771770SStanislav Sedov 	struct _gss_name	*name;
186ae771770SStanislav Sedov         struct _gss_mech_switch	*m;
187ae771770SStanislav Sedov 	gss_name_t		rname;
188ae771770SStanislav Sedov 
189ae771770SStanislav Sedov 	*output_name = GSS_C_NO_NAME;
190ae771770SStanislav Sedov 
191ae771770SStanislav Sedov 	if (input_name_buffer->length == 0) {
192ae771770SStanislav Sedov 		*minor_status = 0;
193ae771770SStanislav Sedov 		return (GSS_S_BAD_NAME);
194ae771770SStanislav Sedov 	}
195ae771770SStanislav Sedov 
196ae771770SStanislav Sedov 	_gss_load_mech();
197ae771770SStanislav Sedov 
198ae771770SStanislav Sedov 	/*
199ae771770SStanislav Sedov 	 * Use GSS_NT_USER_NAME as default name type.
200ae771770SStanislav Sedov 	 */
201ae771770SStanislav Sedov 	if (name_type == GSS_C_NO_OID)
202ae771770SStanislav Sedov 		name_type = GSS_C_NT_USER_NAME;
203ae771770SStanislav Sedov 
204ae771770SStanislav Sedov 	/*
205ae771770SStanislav Sedov 	 * If this is an exported name, we need to parse it to find
206ae771770SStanislav Sedov 	 * the mechanism and then import it as an MN. See RFC 2743
207ae771770SStanislav Sedov 	 * section 3.2 for a description of the format.
208ae771770SStanislav Sedov 	 */
209ae771770SStanislav Sedov 	if (gss_oid_equal(name_type, GSS_C_NT_EXPORT_NAME)) {
210ae771770SStanislav Sedov 		return _gss_import_export_name(minor_status,
211ae771770SStanislav Sedov 		    input_name_buffer, output_name);
212ae771770SStanislav Sedov 	}
213ae771770SStanislav Sedov 
214ae771770SStanislav Sedov 
215ae771770SStanislav Sedov 	*minor_status = 0;
216ae771770SStanislav Sedov 	name = calloc(1, sizeof(struct _gss_name));
217ae771770SStanislav Sedov 	if (!name) {
218ae771770SStanislav Sedov 		*minor_status = ENOMEM;
219ae771770SStanislav Sedov 		return (GSS_S_FAILURE);
220ae771770SStanislav Sedov 	}
221ae771770SStanislav Sedov 
222ae771770SStanislav Sedov 	HEIM_SLIST_INIT(&name->gn_mn);
223ae771770SStanislav Sedov 
224ae771770SStanislav Sedov 	major_status = _gss_copy_oid(minor_status,
225ae771770SStanislav Sedov 	    name_type, &name->gn_type);
226ae771770SStanislav Sedov 	if (major_status) {
227ae771770SStanislav Sedov 		free(name);
228ae771770SStanislav Sedov 		return (GSS_S_FAILURE);
229ae771770SStanislav Sedov 	}
230ae771770SStanislav Sedov 
231ae771770SStanislav Sedov 	major_status = _gss_copy_buffer(minor_status,
232ae771770SStanislav Sedov 	    input_name_buffer, &name->gn_value);
233ae771770SStanislav Sedov 	if (major_status)
234ae771770SStanislav Sedov 		goto out;
235ae771770SStanislav Sedov 
236ae771770SStanislav Sedov 	/*
237ae771770SStanislav Sedov 	 * Walk over the mechs and import the name into a mech name
238ae771770SStanislav Sedov 	 * for those supported this nametype.
239ae771770SStanislav Sedov 	 */
240ae771770SStanislav Sedov 
241ae771770SStanislav Sedov 	HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
242ae771770SStanislav Sedov 		int present = 0;
243ae771770SStanislav Sedov 
244ae771770SStanislav Sedov 		major_status = gss_test_oid_set_member(minor_status,
245ae771770SStanislav Sedov 		    name_type, m->gm_name_types, &present);
246ae771770SStanislav Sedov 
247ae771770SStanislav Sedov 		if (major_status || present == 0)
248ae771770SStanislav Sedov 			continue;
249ae771770SStanislav Sedov 
250ae771770SStanislav Sedov 		mn = malloc(sizeof(struct _gss_mechanism_name));
251ae771770SStanislav Sedov 		if (!mn) {
252ae771770SStanislav Sedov 			*minor_status = ENOMEM;
253ae771770SStanislav Sedov 			major_status = GSS_S_FAILURE;
254ae771770SStanislav Sedov 			goto out;
255ae771770SStanislav Sedov 		}
256ae771770SStanislav Sedov 
257ae771770SStanislav Sedov 		major_status = (*m->gm_mech.gm_import_name)(minor_status,
258ae771770SStanislav Sedov 		    &name->gn_value,
259ae771770SStanislav Sedov 		    (name->gn_type.elements
260ae771770SStanislav Sedov 			? &name->gn_type : GSS_C_NO_OID),
261ae771770SStanislav Sedov 		    &mn->gmn_name);
262ae771770SStanislav Sedov 		if (major_status != GSS_S_COMPLETE) {
263ae771770SStanislav Sedov 			_gss_mg_error(&m->gm_mech, major_status, *minor_status);
264ae771770SStanislav Sedov 			free(mn);
265ae771770SStanislav Sedov 			goto out;
266ae771770SStanislav Sedov 		}
267ae771770SStanislav Sedov 
268ae771770SStanislav Sedov 		mn->gmn_mech = &m->gm_mech;
269ae771770SStanislav Sedov 		mn->gmn_mech_oid = &m->gm_mech_oid;
270ae771770SStanislav Sedov 		HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
271ae771770SStanislav Sedov 	}
272ae771770SStanislav Sedov 
273ae771770SStanislav Sedov 	/*
274ae771770SStanislav Sedov 	 * If we can't find a mn for the name, bail out already here.
275ae771770SStanislav Sedov 	 */
276ae771770SStanislav Sedov 
277ae771770SStanislav Sedov 	mn = HEIM_SLIST_FIRST(&name->gn_mn);
278ae771770SStanislav Sedov 	if (!mn) {
279ae771770SStanislav Sedov 		*minor_status = 0;
280ae771770SStanislav Sedov 		major_status = GSS_S_NAME_NOT_MN;
281ae771770SStanislav Sedov 		goto out;
282ae771770SStanislav Sedov 	}
283ae771770SStanislav Sedov 
284ae771770SStanislav Sedov 	*output_name = (gss_name_t) name;
285ae771770SStanislav Sedov 	return (GSS_S_COMPLETE);
286ae771770SStanislav Sedov 
287ae771770SStanislav Sedov  out:
288ae771770SStanislav Sedov 	rname = (gss_name_t)name;
289ae771770SStanislav Sedov 	gss_release_name(&ms, &rname);
290ae771770SStanislav Sedov 	return major_status;
291ae771770SStanislav Sedov }
292