Implement gss_wrap_iov, gss_unwrap_iov for CFX type encryption types.
[heimdal.git] / lib / gssapi / krb5 / import_name.c
blobd488ce754b893e75a91ccaf493d81b23c4a86e1b
1 /*
2 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "gsskrb5_locl.h"
36 static OM_uint32
37 parse_krb5_name (OM_uint32 *minor_status,
38 krb5_context context,
39 const char *name,
40 gss_name_t *output_name)
42 krb5_principal princ;
43 krb5_error_code kerr;
45 kerr = krb5_parse_name (context, name, &princ);
47 if (kerr == 0) {
48 *output_name = (gss_name_t)princ;
49 return GSS_S_COMPLETE;
51 *minor_status = kerr;
53 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
54 return GSS_S_BAD_NAME;
56 return GSS_S_FAILURE;
59 static OM_uint32
60 import_krb5_name (OM_uint32 *minor_status,
61 krb5_context context,
62 const gss_buffer_t input_name_buffer,
63 gss_name_t *output_name)
65 OM_uint32 ret;
66 char *tmp;
68 tmp = malloc (input_name_buffer->length + 1);
69 if (tmp == NULL) {
70 *minor_status = ENOMEM;
71 return GSS_S_FAILURE;
73 memcpy (tmp,
74 input_name_buffer->value,
75 input_name_buffer->length);
76 tmp[input_name_buffer->length] = '\0';
78 ret = parse_krb5_name(minor_status, context, tmp, output_name);
79 free(tmp);
81 return ret;
84 OM_uint32
85 _gsskrb5_canon_name(OM_uint32 *minor_status, krb5_context context,
86 int use_dns, gss_name_t name, krb5_principal *out)
88 krb5_principal p = (krb5_principal)name;
89 krb5_error_code ret;
90 char *hostname = NULL, *service;
92 *minor_status = 0;
94 /* If its not a hostname */
95 if (krb5_principal_get_type(context, p) != MAGIC_HOSTBASED_NAME_TYPE) {
96 ret = krb5_copy_principal(context, p, out);
97 } else if (!use_dns) {
98 ret = krb5_copy_principal(context, p, out);
99 if (ret == 0)
100 krb5_principal_set_type(context, *out, KRB5_NT_SRV_HST);
101 } else {
102 if (p->name.name_string.len == 0)
103 return GSS_S_BAD_NAME;
104 else if (p->name.name_string.len > 1)
105 hostname = p->name.name_string.val[1];
107 service = p->name.name_string.val[0];
109 ret = krb5_sname_to_principal(context,
110 hostname,
111 service,
112 KRB5_NT_SRV_HST,
113 out);
116 if (ret) {
117 *minor_status = ret;
118 return GSS_S_FAILURE;
121 return 0;
125 static OM_uint32
126 import_hostbased_name (OM_uint32 *minor_status,
127 krb5_context context,
128 const gss_buffer_t input_name_buffer,
129 gss_name_t *output_name)
131 krb5_principal princ = NULL;
132 krb5_error_code kerr;
133 char *tmp, *p, *host = NULL;
135 tmp = malloc (input_name_buffer->length + 1);
136 if (tmp == NULL) {
137 *minor_status = ENOMEM;
138 return GSS_S_FAILURE;
140 memcpy (tmp,
141 input_name_buffer->value,
142 input_name_buffer->length);
143 tmp[input_name_buffer->length] = '\0';
145 p = strchr (tmp, '@');
146 if (p != NULL) {
147 *p = '\0';
148 host = p + 1;
151 kerr = krb5_make_principal(context, &princ, NULL, tmp, host, NULL);
152 free (tmp);
153 *minor_status = kerr;
154 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
155 return GSS_S_BAD_NAME;
156 else if (kerr)
157 return GSS_S_FAILURE;
159 krb5_principal_set_type(context, princ, MAGIC_HOSTBASED_NAME_TYPE);
160 *output_name = (gss_name_t)princ;
162 return 0;
165 static OM_uint32
166 import_export_name (OM_uint32 *minor_status,
167 krb5_context context,
168 const gss_buffer_t input_name_buffer,
169 gss_name_t *output_name)
171 unsigned char *p;
172 uint32_t length;
173 OM_uint32 ret;
174 char *name;
176 if (input_name_buffer->length < 10 + GSS_KRB5_MECHANISM->length)
177 return GSS_S_BAD_NAME;
179 /* TOK, MECH_OID_LEN, DER(MECH_OID), NAME_LEN, NAME */
181 p = input_name_buffer->value;
183 if (memcmp(&p[0], "\x04\x01\x00", 3) != 0 ||
184 p[3] != GSS_KRB5_MECHANISM->length + 2 ||
185 p[4] != 0x06 ||
186 p[5] != GSS_KRB5_MECHANISM->length ||
187 memcmp(&p[6], GSS_KRB5_MECHANISM->elements,
188 GSS_KRB5_MECHANISM->length) != 0)
189 return GSS_S_BAD_NAME;
191 p += 6 + GSS_KRB5_MECHANISM->length;
193 length = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
194 p += 4;
196 if (length > input_name_buffer->length - 10 - GSS_KRB5_MECHANISM->length)
197 return GSS_S_BAD_NAME;
199 name = malloc(length + 1);
200 if (name == NULL) {
201 *minor_status = ENOMEM;
202 return GSS_S_FAILURE;
204 memcpy(name, p, length);
205 name[length] = '\0';
207 ret = parse_krb5_name(minor_status, context, name, output_name);
208 free(name);
210 return ret;
213 OM_uint32 _gsskrb5_import_name
214 (OM_uint32 * minor_status,
215 const gss_buffer_t input_name_buffer,
216 const gss_OID input_name_type,
217 gss_name_t * output_name
220 krb5_context context;
222 *minor_status = 0;
223 *output_name = GSS_C_NO_NAME;
225 GSSAPI_KRB5_INIT (&context);
227 if (gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE) ||
228 gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE_X))
229 return import_hostbased_name (minor_status,
230 context,
231 input_name_buffer,
232 output_name);
233 else if (gss_oid_equal(input_name_type, GSS_C_NO_OID)
234 || gss_oid_equal(input_name_type, GSS_C_NT_USER_NAME)
235 || gss_oid_equal(input_name_type, GSS_KRB5_NT_PRINCIPAL_NAME))
236 /* default printable syntax */
237 return import_krb5_name (minor_status,
238 context,
239 input_name_buffer,
240 output_name);
241 else if (gss_oid_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) {
242 return import_export_name(minor_status,
243 context,
244 input_name_buffer,
245 output_name);
246 } else {
247 *minor_status = 0;
248 return GSS_S_BAD_NAMETYPE;