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 samlReleaseAttrContext(&tmpMinor, name);
110 radiusReleaseAttrContext(&tmpMinor, name);
112 GSSEAP_MUTEX_DESTROY(&name->mutex);
117 return GSS_S_COMPLETE;
121 krbPrincipalToName(OM_uint32 *minor,
122 krb5_principal *principal,
128 major = gssEapAllocName(minor, &name);
129 if (GSS_ERROR(major))
132 name->krbPrincipal = *principal;
135 if (name->krbPrincipal->length == 1) {
136 name->flags |= NAME_FLAG_NAI;
138 name->flags |= NAME_FLAG_SERVICE;
144 return GSS_S_COMPLETE;
148 importServiceName(OM_uint32 *minor,
149 const gss_buffer_t nameBuffer,
152 OM_uint32 major, tmpMinor;
153 krb5_context krbContext;
154 krb5_principal krbPrinc;
155 char *service, *host;
157 GSSEAP_KRB_INIT(&krbContext);
159 major = bufferToString(minor, nameBuffer, &service);
160 if (GSS_ERROR(major))
163 host = strchr(service, '@');
169 /* XXX this is probably NOT what we want to be doing */
170 *minor = krb5_sname_to_principal(krbContext, host, service,
171 KRB5_NT_SRV_HST, &krbPrinc);
173 GSSEAP_FREE(service);
174 return GSS_S_FAILURE;
177 major = krbPrincipalToName(minor, &krbPrinc, pName);
178 if (GSS_ERROR(major)) {
179 krb5_free_principal(krbContext, krbPrinc);
182 GSSEAP_FREE(service);
187 importUserName(OM_uint32 *minor,
188 const gss_buffer_t nameBuffer,
191 OM_uint32 major, tmpMinor;
192 krb5_context krbContext;
193 krb5_principal krbPrinc;
196 GSSEAP_KRB_INIT(&krbContext);
198 major = bufferToString(minor, nameBuffer, &nameString);
199 if (GSS_ERROR(major))
202 *minor = krb5_parse_name(krbContext, nameString, &krbPrinc);
204 GSSEAP_FREE(nameString);
205 return GSS_S_FAILURE;
208 major = krbPrincipalToName(minor, &krbPrinc, pName);
209 if (GSS_ERROR(major)) {
210 krb5_free_principal(krbContext, krbPrinc);
213 GSSEAP_FREE(nameString);
218 importExportedName(OM_uint32 *minor,
219 const gss_buffer_t nameBuffer,
222 OM_uint32 major, tmpMinor;
223 krb5_context krbContext;
227 enum gss_eap_token_type tok_type;
228 gss_name_t name = GSS_C_NO_NAME;
230 GSSEAP_KRB_INIT(&krbContext);
232 p = (unsigned char *)nameBuffer->value;
233 remain = nameBuffer->length;
235 if (remain < 6 + GSS_EAP_MECHANISM->length + 4)
236 return GSS_S_BAD_NAME;
238 #define UPDATE_REMAIN(n) do { \
244 tok_type = load_uint16_be(p);
245 if (tok_type != TOK_TYPE_EXPORT_NAME &&
246 tok_type != TOK_TYPE_EXPORT_NAME_COMPOSITE)
247 return GSS_S_BAD_NAME;
251 len = load_uint16_be(p);
252 if (len != 2 + GSS_EAP_MECHANISM->length)
253 return GSS_S_BAD_NAME;
258 return GSS_S_BAD_NAME;
259 if (p[1] != GSS_EAP_MECHANISM->length)
260 return GSS_S_BAD_MECH;
261 if (memcmp(&p[2], GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length))
262 return GSS_S_BAD_MECH;
263 UPDATE_REMAIN(2 + GSS_EAP_MECHANISM->length);
266 len = load_uint32_be(p);
269 #define CHECK_REMAIN(n) do { \
270 if (remain < (n)) { \
272 major = GSS_S_BAD_NAME; \
283 major = importUserName(minor, &buf, &name);
284 if (GSS_ERROR(major))
287 if (tok_type == TOK_TYPE_EXPORT_NAME_COMPOSITE) {
291 name->flags = load_uint32_be(p);
294 if (name->flags & NAME_FLAG_RADIUS_ATTRIBUTES) {
296 buf.length = load_uint32_be(p);
299 CHECK_REMAIN(buf.length);
301 UPDATE_REMAIN(buf.length);
303 major = radiusImportAttrContext(minor, &buf, name);
304 if (GSS_ERROR(major))
308 if (name->flags & NAME_FLAG_SAML_ATTRIBUTES) {
310 buf.length = load_uint32_be(p);
313 CHECK_REMAIN(buf.length);
315 UPDATE_REMAIN(buf.length);
317 major = samlImportAttrContext(minor, &buf, name);
318 if (GSS_ERROR(major))
323 major = GSS_S_COMPLETE;
326 if (GSS_ERROR(major))
327 gssEapReleaseName(&tmpMinor, &name);
335 gssEapImportName(OM_uint32 *minor,
336 const gss_buffer_t nameBuffer,
340 OM_uint32 major, tmpMinor;
342 *name = GSS_C_NO_NAME;
344 if (nameType == GSS_C_NULL_OID ||
345 oidEqual(nameType, GSS_C_NT_USER_NAME) ||
346 oidEqual(nameType, GSS_EAP_NT_PRINCIPAL_NAME))
347 major = importUserName(minor, nameBuffer, name);
348 else if (oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE) ||
349 oidEqual(nameType, GSS_C_NT_HOSTBASED_SERVICE_X))
350 major = importServiceName(minor, nameBuffer, name);
351 else if (oidEqual(nameType, GSS_C_NT_EXPORT_NAME))
352 major = importExportedName(minor, nameBuffer, name);
354 major = GSS_S_BAD_NAMETYPE;
356 if (GSS_ERROR(major))
357 gssEapReleaseName(&tmpMinor, name);
363 gssEapExportName(OM_uint32 *minor,
364 const gss_name_t name,
365 gss_buffer_t exportedName,
368 OM_uint32 major = GSS_S_FAILURE, tmpMinor;
369 krb5_context krbContext;
370 char *krbName = NULL;
373 gss_buffer_desc radius, saml;
381 exportedName->length = 0;
382 exportedName->value = NULL;
384 GSSEAP_KRB_INIT(&krbContext);
385 GSSEAP_MUTEX_LOCK(&name->mutex);
388 * Don't export a composite name if we don't have any attributes.
390 if (composite && !NAME_HAS_ATTRIBUTES(name))
393 *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
395 major = GSS_S_FAILURE;
398 krbNameLen = strlen(krbName);
400 exportedName->length = 6 + GSS_EAP_MECHANISM->length + 4 + krbNameLen;
402 exportedName->length += 4;
404 if (name->flags & NAME_FLAG_RADIUS_ATTRIBUTES) {
405 major = radiusExportAttrContext(minor, name, &radius);
406 if (GSS_ERROR(major))
408 exportedName->length += 4 + radius.length;
410 if (name->flags & NAME_FLAG_SAML_ATTRIBUTES) {
411 major = samlExportAttrContext(minor, name, &saml);
412 if (GSS_ERROR(major))
414 exportedName->length += 4 + saml.length;
418 exportedName->value = GSSEAP_MALLOC(exportedName->length);
419 if (exportedName->value == NULL) {
420 major = GSS_S_FAILURE;
425 /* TOK | MECH_OID_LEN */
426 p = (unsigned char *)exportedName->value;
427 store_uint16_be(composite
428 ? TOK_TYPE_EXPORT_NAME_COMPOSITE
429 : TOK_TYPE_EXPORT_NAME,
432 store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
437 *p++ = GSS_EAP_MECHANISM->length & 0xff;
438 memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
439 p += GSS_EAP_MECHANISM->length;
442 store_uint32_be(krbNameLen, p);
446 memcpy(p, krbName, krbNameLen);
450 store_uint32_be(name->flags, p);
453 if (name->flags & NAME_FLAG_RADIUS_ATTRIBUTES) {
454 store_uint32_be(radius.length, p);
455 memcpy(&p[4], radius.value, radius.length);
456 p += 4 + radius.length;
458 if (name->flags & NAME_FLAG_SAML_ATTRIBUTES) {
459 store_uint32_be(saml.length, p);
460 memcpy(&p[4], saml.value, saml.length);
461 p += 4 + saml.length;
466 major = GSS_S_COMPLETE;
469 GSSEAP_MUTEX_UNLOCK(&name->mutex);
470 gss_release_buffer(&tmpMinor, &saml);
471 gss_release_buffer(&tmpMinor, &radius);
472 if (GSS_ERROR(major))
473 gss_release_buffer(&tmpMinor, exportedName);
474 krb5_free_unparsed_name(krbContext, krbName);
479 static gss_buffer_desc attributePrefixes[] = {
486 /* ATTR_TYPE_SAML_AAA_ASSERTION */
487 sizeof("urn:ietf:params:gss-eap:saml-aaa-assertion"),
488 "urn:ietf:params:gss-eap:saml-aaa-assertion"
491 /* ATTR_TYPE_SAML_ATTR */
492 sizeof("urn:ietf:params:gss-eap:saml-attr"),
493 "urn:ietf:params:gss-eap:saml-attr"
496 /* ATTR_TYPE_RADIUS_AVP */
497 sizeof("urn:ietf:params:gss-eap:radius-avp"),
498 "urn:ietf:params:gss-eap:radius-avp",
502 enum gss_eap_attribute_type
503 gssEapAttributePrefixToType(const gss_buffer_t prefix)
505 enum gss_eap_attribute_type i;
507 for (i = ATTR_TYPE_SAML_AAA_ASSERTION;
508 i < sizeof(attributePrefixes) / sizeof(attributePrefixes[0]);
511 if (bufferEqual(&attributePrefixes[i], prefix))
515 return ATTR_TYPE_NONE;
519 gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type)
521 if (type <= ATTR_TYPE_NONE ||
522 type > ATTR_TYPE_RADIUS_AVP)
523 return GSS_C_NO_BUFFER;
525 return &attributePrefixes[type];
529 decomposeAttributeName(OM_uint32 *minor,
530 const gss_buffer_t attribute,
537 for (i = 0; i < attribute->length; i++) {
538 if (((char *)attribute->value)[i] == ' ') {
539 p = (char *)attribute->value + i + 1;
544 prefix->value = attribute->value;
547 if (p != NULL && *p != '\0') {
548 suffix->length = attribute->length - 1 - prefix->length;
552 suffix->value = NULL;
556 return GSS_S_COMPLETE;
560 composeAttributeName(OM_uint32 *minor,
561 const gss_buffer_t prefix,
562 const gss_buffer_t suffix,
563 gss_buffer_t attribute)
568 attribute->length = 0;
569 attribute->value = NULL;
571 if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
572 return GSS_S_COMPLETE;
574 len = prefix->length;
575 if (suffix != NULL) {
576 len += 1 + suffix->length;
579 p = attribute->value = GSSEAP_MALLOC(len + 1);
580 if (attribute->value == NULL) {
582 return GSS_S_FAILURE;
584 attribute->length = len;
586 memcpy(p, prefix->value, prefix->length);
587 if (suffix != NULL) {
588 p[prefix->length] = ' ';
589 memcpy(p + prefix->length + 1, suffix->value, suffix->length);
592 p[attribute->length] = '\0';
595 return GSS_S_COMPLETE;