Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / gssapi / krb5 / import_name.c
blob3454ee941f9588259b3a2d08bdf57cf86c5d48f5
1 /* $NetBSD: import_name.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "gsskrb5_locl.h"
38 static OM_uint32
39 parse_krb5_name (OM_uint32 *minor_status,
40 krb5_context context,
41 const char *name,
42 gss_name_t *output_name)
44 krb5_principal princ;
45 krb5_error_code kerr;
47 kerr = krb5_parse_name (context, name, &princ);
49 if (kerr == 0) {
50 *output_name = (gss_name_t)princ;
51 return GSS_S_COMPLETE;
53 *minor_status = kerr;
55 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
56 return GSS_S_BAD_NAME;
58 return GSS_S_FAILURE;
61 static OM_uint32
62 import_krb5_name (OM_uint32 *minor_status,
63 krb5_context context,
64 const gss_buffer_t input_name_buffer,
65 gss_name_t *output_name)
67 OM_uint32 ret;
68 char *tmp;
70 tmp = malloc (input_name_buffer->length + 1);
71 if (tmp == NULL) {
72 *minor_status = ENOMEM;
73 return GSS_S_FAILURE;
75 memcpy (tmp,
76 input_name_buffer->value,
77 input_name_buffer->length);
78 tmp[input_name_buffer->length] = '\0';
80 ret = parse_krb5_name(minor_status, context, tmp, output_name);
81 free(tmp);
83 return ret;
86 OM_uint32
87 _gsskrb5_canon_name(OM_uint32 *minor_status, krb5_context context,
88 int use_dns, krb5_const_principal sourcename, gss_name_t targetname,
89 krb5_principal *out)
91 krb5_principal p = (krb5_principal)targetname;
92 krb5_error_code ret;
93 char *hostname = NULL, *service;
95 *minor_status = 0;
97 /* If its not a hostname */
98 if (krb5_principal_get_type(context, p) != MAGIC_HOSTBASED_NAME_TYPE) {
99 ret = krb5_copy_principal(context, p, out);
100 } else if (!use_dns) {
101 ret = krb5_copy_principal(context, p, out);
102 if (ret)
103 goto out;
104 krb5_principal_set_type(context, *out, KRB5_NT_SRV_HST);
105 if (sourcename)
106 ret = krb5_principal_set_realm(context, *out, sourcename->realm);
107 } else {
108 if (p->name.name_string.len == 0)
109 return GSS_S_BAD_NAME;
110 else if (p->name.name_string.len > 1)
111 hostname = p->name.name_string.val[1];
113 service = p->name.name_string.val[0];
115 ret = krb5_sname_to_principal(context,
116 hostname,
117 service,
118 KRB5_NT_SRV_HST,
119 out);
122 out:
123 if (ret) {
124 *minor_status = ret;
125 return GSS_S_FAILURE;
128 return 0;
132 static OM_uint32
133 import_hostbased_name (OM_uint32 *minor_status,
134 krb5_context context,
135 const gss_buffer_t input_name_buffer,
136 gss_name_t *output_name)
138 krb5_principal princ = NULL;
139 krb5_error_code kerr;
140 char *tmp, *p, *host = NULL;
142 tmp = malloc (input_name_buffer->length + 1);
143 if (tmp == NULL) {
144 *minor_status = ENOMEM;
145 return GSS_S_FAILURE;
147 memcpy (tmp,
148 input_name_buffer->value,
149 input_name_buffer->length);
150 tmp[input_name_buffer->length] = '\0';
152 p = strchr (tmp, '@');
153 if (p != NULL) {
154 *p = '\0';
155 host = p + 1;
158 kerr = krb5_make_principal(context, &princ, NULL, tmp, host, NULL);
159 free (tmp);
160 *minor_status = kerr;
161 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
162 return GSS_S_BAD_NAME;
163 else if (kerr)
164 return GSS_S_FAILURE;
166 krb5_principal_set_type(context, princ, MAGIC_HOSTBASED_NAME_TYPE);
167 *output_name = (gss_name_t)princ;
169 return 0;
172 static OM_uint32
173 import_export_name (OM_uint32 *minor_status,
174 krb5_context context,
175 const gss_buffer_t input_name_buffer,
176 gss_name_t *output_name)
178 unsigned char *p;
179 uint32_t length;
180 OM_uint32 ret;
181 char *name;
183 if (input_name_buffer->length < 10 + GSS_KRB5_MECHANISM->length)
184 return GSS_S_BAD_NAME;
186 /* TOK, MECH_OID_LEN, DER(MECH_OID), NAME_LEN, NAME */
188 p = input_name_buffer->value;
190 if (memcmp(&p[0], "\x04\x01\x00", 3) != 0 ||
191 p[3] != GSS_KRB5_MECHANISM->length + 2 ||
192 p[4] != 0x06 ||
193 p[5] != GSS_KRB5_MECHANISM->length ||
194 memcmp(&p[6], GSS_KRB5_MECHANISM->elements,
195 GSS_KRB5_MECHANISM->length) != 0)
196 return GSS_S_BAD_NAME;
198 p += 6 + GSS_KRB5_MECHANISM->length;
200 length = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
201 p += 4;
203 if (length > input_name_buffer->length - 10 - GSS_KRB5_MECHANISM->length)
204 return GSS_S_BAD_NAME;
206 name = malloc(length + 1);
207 if (name == NULL) {
208 *minor_status = ENOMEM;
209 return GSS_S_FAILURE;
211 memcpy(name, p, length);
212 name[length] = '\0';
214 ret = parse_krb5_name(minor_status, context, name, output_name);
215 free(name);
217 return ret;
220 OM_uint32 GSSAPI_CALLCONV _gsskrb5_import_name
221 (OM_uint32 * minor_status,
222 const gss_buffer_t input_name_buffer,
223 const gss_OID input_name_type,
224 gss_name_t * output_name
227 krb5_context context;
229 *minor_status = 0;
230 *output_name = GSS_C_NO_NAME;
232 GSSAPI_KRB5_INIT (&context);
234 if (gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE) ||
235 gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE_X))
236 return import_hostbased_name (minor_status,
237 context,
238 input_name_buffer,
239 output_name);
240 else if (input_name_type == GSS_C_NO_OID
241 || gss_oid_equal(input_name_type, GSS_C_NT_USER_NAME)
242 || gss_oid_equal(input_name_type, GSS_KRB5_NT_PRINCIPAL_NAME))
243 /* default printable syntax */
244 return import_krb5_name (minor_status,
245 context,
246 input_name_buffer,
247 output_name);
248 else if (gss_oid_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) {
249 return import_export_name(minor_status,
250 context,
251 input_name_buffer,
252 output_name);
253 } else {
254 *minor_status = 0;
255 return GSS_S_BAD_NAMETYPE;