1/*-
2 * Copyright (c) 2005 Doug Rabson
3 * All rights reserved.
4 *
5 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$FreeBSD: src/lib/libgssapi/gss_export_name.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
29 */
30
31#include "mech_locl.h"
32
33GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
34gss_export_name(OM_uint32 *minor_status,
35    const gss_name_t input_name,
36    gss_buffer_t exported_name)
37{
38	struct _gss_name *name = (struct _gss_name *) input_name;
39	struct _gss_mechanism_name *mn;
40
41	_gss_mg_check_name(input_name);
42	_mg_buffer_zero(exported_name);
43
44	/*
45	 * If this name already has any attached MNs, export the first
46	 * one, otherwise export based on the first mechanism in our
47	 * list.
48	 */
49	mn = HEIM_SLIST_FIRST(&name->gn_mn);
50	if (!mn) {
51		*minor_status = 0;
52		return (GSS_S_NAME_NOT_MN);
53	}
54
55	return mn->gmn_mech->gm_export_name(minor_status,
56	    mn->gmn_name, exported_name);
57}
58
59OM_uint32
60gss_mg_export_name(OM_uint32 *minor_status, const gss_const_OID mech,
61		   const void *name, size_t length, gss_buffer_t exported_name)
62{
63    uint8_t *buf;
64
65    exported_name->length = 10 + length + mech->length;
66    exported_name->value  = malloc(exported_name->length);
67    if (exported_name->value == NULL) {
68	*minor_status = ENOMEM;
69	return GSS_S_FAILURE;
70    }
71
72    /* TOK, MECH_OID_LEN, DER(MECH_OID), NAME_LEN, NAME */
73
74    buf = exported_name->value;
75    memcpy(buf, "\x04\x01", 2);
76    buf += 2;
77    buf[0] = ((mech->length + 2) >> 8) & 0xff;
78    buf[1] = (mech->length + 2) & 0xff;
79    buf+= 2;
80    buf[0] = 0x06;
81    buf[1] = (mech->length) & 0xFF;
82    buf+= 2;
83
84    memcpy(buf, mech->elements, mech->length);
85    buf += mech->length;
86
87    buf[0] = (length >> 24) & 0xff;
88    buf[1] = (length >> 16) & 0xff;
89    buf[2] = (length >> 8) & 0xff;
90    buf[3] = (length) & 0xff;
91    buf += 4;
92
93    memcpy (buf, name, length);
94
95    *minor_status = 0;
96    return GSS_S_COMPLETE;
97}
98