2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * 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.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Portions Copyright 2009 by the Massachusetts Institute of Technology.
34 * All Rights Reserved.
36 * Export of this software from the United States of America may
37 * require a specific license from the United States Government.
38 * It is the responsibility of any person or organization contemplating
39 * export to obtain such a license before exporting.
41 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42 * distribute this software and its documentation for any purpose and
43 * without fee is hereby granted, provided that the above copyright
44 * notice appear in all copies and that both that copyright notice and
45 * this permission notice appear in supporting documentation, and that
46 * the name of M.I.T. not be used in advertising or publicity pertaining
47 * to distribution of the software without specific, written prior
48 * permission. Furthermore if you modify this software you must label
49 * your software as modified software and not distribute it in such a
50 * fashion that it might be confused with the original M.I.T. software.
51 * M.I.T. makes no representations about the suitability of
52 * this software for any purpose. It is provided "as is" without express
53 * or implied warranty.
56 #include "gssapiP_eap.h"
58 static gss_OID_desc gssEapNtPrincipalName = {
59 /* 1.3.6.1.4.1.5322.21.2.1 */
60 10, "\x2B\x06\x01\x04\x01\xA9\x4A\x15\x02\x01"
63 gss_OID GSS_EAP_NT_PRINCIPAL_NAME = &gssEapNtPrincipalName;
66 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName)
71 *pName = GSS_C_NO_NAME;
73 name = (gss_name_t)GSSEAP_CALLOC(1, sizeof(*name));
79 if (GSSEAP_MUTEX_INIT(&name->mutex) != 0) {
81 gssEapReleaseName(&tmpMinor, &name);
87 return GSS_S_COMPLETE;
91 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName)
94 krb5_context krbContext = NULL;
98 return GSS_S_COMPLETE;
102 if (name == GSS_C_NO_NAME) {
103 return GSS_S_COMPLETE;
106 GSSEAP_KRB_INIT(&krbContext);
107 krb5_free_principal(krbContext, name->krbPrincipal);
109 gssEapReleaseAttrContext(&tmpMinor, name);
111 GSSEAP_MUTEX_DESTROY(&name->mutex);
116 return GSS_S_COMPLETE;
120 krbPrincipalToName(OM_uint32 *minor,
121 krb5_principal *principal,
127 major = gssEapAllocName(minor, &name);
128 if (GSS_ERROR(major))
131 name->krbPrincipal = *principal;
134 if (name->krbPrincipal->length == 1) {
135 name->flags |= NAME_FLAG_NAI;
137 name->flags |= NAME_FLAG_SERVICE;
143 return GSS_S_COMPLETE;
147 importServiceName(OM_uint32 *minor,
148 const gss_buffer_t nameBuffer,
151 OM_uint32 major, tmpMinor;
152 krb5_context krbContext;
153 krb5_principal krbPrinc;
154 char *service, *host;
156 GSSEAP_KRB_INIT(&krbContext);
158 major = bufferToString(minor, nameBuffer, &service);
159 if (GSS_ERROR(major))
162 host = strchr(service, '@');
168 /* XXX this is probably NOT what we want to be doing */
169 *minor = krb5_sname_to_principal(krbContext, host, service,
170 KRB5_NT_SRV_HST, &krbPrinc);
172 GSSEAP_FREE(service);
173 return GSS_S_FAILURE;
176 major = krbPrincipalToName(minor, &krbPrinc, pName);
177 if (GSS_ERROR(major)) {
178 krb5_free_principal(krbContext, krbPrinc);
181 GSSEAP_FREE(service);
186 importUserName(OM_uint32 *minor,
187 const gss_buffer_t nameBuffer,
190 OM_uint32 major, tmpMinor;
191 krb5_context krbContext;
192 krb5_principal krbPrinc;
195 GSSEAP_KRB_INIT(&krbContext);
197 major = bufferToString(minor, nameBuffer, &nameString);
198 if (GSS_ERROR(major))
201 *minor = krb5_parse_name(krbContext, nameString, &krbPrinc);
203 GSSEAP_FREE(nameString);
204 return GSS_S_FAILURE;
207 major = krbPrincipalToName(minor, &krbPrinc, pName);
208 if (GSS_ERROR(major)) {
209 krb5_free_principal(krbContext, krbPrinc);
212 GSSEAP_FREE(nameString);
217 importExportedName(OM_uint32 *minor,
218 const gss_buffer_t nameBuffer,
221 OM_uint32 major, tmpMinor;
222 krb5_context krbContext;
226 enum gss_eap_token_type tok_type;
227 gss_name_t name = GSS_C_NO_NAME;
229 GSSEAP_KRB_INIT(&krbContext);
231 p = (unsigned char *)nameBuffer->value;
232 remain = nameBuffer->length;
234 if (remain < 6 + GSS_EAP_MECHANISM->length + 4)
235 return GSS_S_BAD_NAME;
237 #define UPDATE_REMAIN(n) do { \
243 tok_type = load_uint16_be(p);
244 if (tok_type != TOK_TYPE_EXPORT_NAME &&
245 tok_type != TOK_TYPE_EXPORT_NAME_COMPOSITE)
246 return GSS_S_BAD_NAME;
250 len = load_uint16_be(p);
251 if (len != 2 + GSS_EAP_MECHANISM->length)
252 return GSS_S_BAD_NAME;
257 return GSS_S_BAD_NAME;
258 if (p[1] != GSS_EAP_MECHANISM->length)
259 return GSS_S_BAD_MECH;
260 if (memcmp(&p[2], GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length))
261 return GSS_S_BAD_MECH;
262 UPDATE_REMAIN(2 + GSS_EAP_MECHANISM->length);
265 len = load_uint32_be(p);
268 #define CHECK_REMAIN(n) do { \
269 if (remain < (n)) { \
271 major = GSS_S_BAD_NAME; \
282 major = importUserName(minor, &buf, &name);
283 if (GSS_ERROR(major))
286 if (tok_type == TOK_TYPE_EXPORT_NAME_COMPOSITE) {
290 name->flags = load_uint32_be(p);
294 buf.length = load_uint32_be(p);
297 CHECK_REMAIN(buf.length);
299 UPDATE_REMAIN(buf.length);
301 major = gssEapImportAttrContext(minor, &buf, name);
302 if (GSS_ERROR(major))
306 major = GSS_S_COMPLETE;
309 if (GSS_ERROR(major))
310 gssEapReleaseName(&tmpMinor, &name);
318 gssEapImportName(OM_uint32 *minor,
319 const gss_buffer_t nameBuffer,
323 OM_uint32 major, tmpMinor;
325 *name = GSS_C_NO_NAME;
327 if (nameType == GSS_C_NULL_OID ||
328 oidEqual(nameType, GSS_C_NT_USER_NAME) ||
329 oidEqual(nameType, GSS_EAP_NT_PRINCIPAL_NAME))
330 major = importUserName(minor, nameBuffer, name);
331 else if (oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE) ||
332 oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE_X))
333 major = importServiceName(minor, nameBuffer, name);
334 else if (oidEqual(nameType, GSS_C_NT_EXPORT_NAME))
335 major = importExportedName(minor, nameBuffer, name);
337 major = GSS_S_BAD_NAMETYPE;
339 if (GSS_ERROR(major))
340 gssEapReleaseName(&tmpMinor, name);
346 gssEapExportName(OM_uint32 *minor,
347 const gss_name_t name,
348 gss_buffer_t exportedName,
351 OM_uint32 major = GSS_S_FAILURE, tmpMinor;
352 krb5_context krbContext;
353 char *krbName = NULL;
356 gss_buffer_desc attrs = GSS_C_EMPTY_BUFFER;
358 exportedName->length = 0;
359 exportedName->value = NULL;
361 GSSEAP_KRB_INIT(&krbContext);
362 GSSEAP_MUTEX_LOCK(&name->mutex);
364 *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
366 major = GSS_S_FAILURE;
369 krbNameLen = strlen(krbName);
371 exportedName->length = 6 + GSS_EAP_MECHANISM->length + 4 + krbNameLen;
373 exportedName->length += 4;
375 major = gssEapExportAttrContext(minor, name, &attrs);
376 if (GSS_ERROR(major))
378 exportedName->length += 4 + attrs.length;
381 exportedName->value = GSSEAP_MALLOC(exportedName->length);
382 if (exportedName->value == NULL) {
383 major = GSS_S_FAILURE;
388 /* TOK | MECH_OID_LEN */
389 p = (unsigned char *)exportedName->value;
390 store_uint16_be(composite
391 ? TOK_TYPE_EXPORT_NAME_COMPOSITE
392 : TOK_TYPE_EXPORT_NAME,
395 store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
400 *p++ = GSS_EAP_MECHANISM->length & 0xff;
401 memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
402 p += GSS_EAP_MECHANISM->length;
405 store_uint32_be(krbNameLen, p);
409 memcpy(p, krbName, krbNameLen);
413 store_uint32_be(name->flags, p);
416 store_uint32_be(attrs.length, p);
417 memcpy(&p[4], attrs.value, attrs.length);
418 p += 4 + attrs.length;
422 major = GSS_S_COMPLETE;
425 GSSEAP_MUTEX_UNLOCK(&name->mutex);
426 gss_release_buffer(&tmpMinor, &attrs);
427 if (GSS_ERROR(major))
428 gss_release_buffer(&tmpMinor, exportedName);
429 krb5_free_unparsed_name(krbContext, krbName);