don't include attr length in exported composite names
[mech_eap.orig] / util_name.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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  * 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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 /*
33  * Portions Copyright 2009 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
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.
40  *
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.
54  */
55
56 #include "gssapiP_eap.h"
57
58 static gss_OID_desc gssEapNtPrincipalName = {
59     /* 1.3.6.1.4.1.5322.22.2.1  */
60     10, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\x02\x01"
61 };
62
63 gss_OID GSS_EAP_NT_PRINCIPAL_NAME = &gssEapNtPrincipalName;
64
65 OM_uint32
66 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName)
67 {
68     OM_uint32 tmpMinor;
69     gss_name_t name;
70
71     *pName = GSS_C_NO_NAME;
72
73     name = (gss_name_t)GSSEAP_CALLOC(1, sizeof(*name));
74     if (name == NULL) {
75         *minor = ENOMEM;
76         return GSS_S_FAILURE;
77     }
78
79     if (GSSEAP_MUTEX_INIT(&name->mutex) != 0) {
80         *minor = errno;
81         gssEapReleaseName(&tmpMinor, &name);
82         return GSS_S_FAILURE;
83     }
84
85     *pName = name;
86
87     return GSS_S_COMPLETE;
88 }
89
90 OM_uint32
91 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName)
92 {
93     gss_name_t name;
94     krb5_context krbContext = NULL;
95     OM_uint32 tmpMinor;
96
97     if (pName == NULL) {
98         return GSS_S_COMPLETE;
99     }
100
101     name = *pName;
102     if (name == GSS_C_NO_NAME) {
103         return GSS_S_COMPLETE;
104     }
105
106     GSSEAP_KRB_INIT(&krbContext);
107     krb5_free_principal(krbContext, name->krbPrincipal);
108
109     gssEapReleaseAttrContext(&tmpMinor, name);
110
111     GSSEAP_MUTEX_DESTROY(&name->mutex);
112     GSSEAP_FREE(name);
113     *pName = NULL;
114
115     *minor = 0;
116     return GSS_S_COMPLETE;
117 }
118
119 static OM_uint32
120 krbPrincipalToName(OM_uint32 *minor,
121                    krb5_principal *principal,
122                    gss_name_t *pName)
123 {
124     OM_uint32 major;
125     gss_name_t name;
126
127     major = gssEapAllocName(minor, &name);
128     if (GSS_ERROR(major))
129         return major;
130
131     name->krbPrincipal = *principal;
132     *principal = NULL;
133
134     if (name->krbPrincipal->length == 1) {
135         name->flags |= NAME_FLAG_NAI;
136     } else {
137         name->flags |= NAME_FLAG_SERVICE;
138     }
139
140     *pName = name;
141     *minor = 0;
142
143     return GSS_S_COMPLETE;
144 }
145
146 static OM_uint32
147 importServiceName(OM_uint32 *minor,
148                   const gss_buffer_t nameBuffer,
149                   gss_name_t *pName)
150 {
151     OM_uint32 major;
152     krb5_context krbContext;
153     krb5_principal krbPrinc;
154     char *service, *host;
155
156     GSSEAP_KRB_INIT(&krbContext);
157
158     major = bufferToString(minor, nameBuffer, &service);
159     if (GSS_ERROR(major))
160         return major;
161
162     host = strchr(service, '@');
163     if (host != NULL) {
164         *host = '\0';
165         host++;
166     }
167
168     /* XXX this is probably NOT what we want to be doing */
169     if (krb5_sname_to_principal(krbContext, host, service,
170                                 KRB5_NT_SRV_HST, &krbPrinc) != 0) {
171         GSSEAP_FREE(service);
172         *minor = GSSEAP_BAD_SERVICE_NAME;
173         return GSS_S_FAILURE;
174     }
175
176     major = krbPrincipalToName(minor, &krbPrinc, pName);
177     if (GSS_ERROR(major)) {
178         krb5_free_principal(krbContext, krbPrinc);
179     }
180
181     GSSEAP_FREE(service);
182     return major;
183 }
184
185 static OM_uint32
186 importUserName(OM_uint32 *minor,
187                const gss_buffer_t nameBuffer,
188                gss_name_t *pName)
189 {
190     OM_uint32 major;
191     krb5_context krbContext;
192     krb5_principal krbPrinc;
193     char *nameString;
194
195     GSSEAP_KRB_INIT(&krbContext);
196
197     if (nameBuffer == GSS_C_NO_BUFFER) {
198         *minor = krb5_copy_principal(krbContext,
199                                      krb5_anonymous_principal(), &krbPrinc);
200         if (*minor != 0)
201             return GSS_S_FAILURE;
202     } else {
203         major = bufferToString(minor, nameBuffer, &nameString);
204         if (GSS_ERROR(major))
205             return major;
206
207         *minor = krb5_parse_name(krbContext, nameString, &krbPrinc);
208         if (*minor != 0) {
209             GSSEAP_FREE(nameString);
210             return GSS_S_FAILURE;
211         }
212     }
213
214     major = krbPrincipalToName(minor, &krbPrinc, pName);
215     if (GSS_ERROR(major)) {
216         krb5_free_principal(krbContext, krbPrinc);
217     }
218
219     GSSEAP_FREE(nameString);
220     return major;
221 }
222
223 #define UPDATE_REMAIN(n)    do {            \
224         p += (n);                           \
225         remain -= (n);                      \
226     } while (0)
227
228 #define CHECK_REMAIN(n)     do {        \
229         if (remain < (n)) {             \
230             *minor = GSSEAP_WRONG_SIZE; \
231             major = GSS_S_BAD_NAME;     \
232             goto cleanup;               \
233         }                               \
234     } while (0)
235
236 OM_uint32
237 gssEapImportNameInternal(OM_uint32 *minor,
238                          const gss_buffer_t nameBuffer,
239                          gss_name_t *pName,
240                          unsigned int flags)
241 {
242     OM_uint32 major, tmpMinor;
243     krb5_context krbContext;
244     unsigned char *p;
245     size_t len, remain;
246     gss_buffer_desc buf;
247     enum gss_eap_token_type tokType;
248     gss_name_t name = GSS_C_NO_NAME;
249
250     GSSEAP_KRB_INIT(&krbContext);
251
252     p = (unsigned char *)nameBuffer->value;
253     remain = nameBuffer->length;
254
255     if (flags & EXPORT_NAME_FLAG_OID) {
256         if (remain < 6 + GSS_EAP_MECHANISM->length + 4)
257             return GSS_S_BAD_NAME;
258
259         if (flags & EXPORT_NAME_FLAG_COMPOSITE)
260             tokType = TOK_TYPE_EXPORT_NAME_COMPOSITE;
261         else
262             tokType = TOK_TYPE_EXPORT_NAME;
263
264         /* TOK_ID */
265         if (load_uint16_be(p) != tokType)
266             return GSS_S_BAD_NAME;
267         UPDATE_REMAIN(2);
268
269         /* MECH_OID_LEN */
270         len = load_uint16_be(p);
271         if (len != 2 + GSS_EAP_MECHANISM->length)
272             return GSS_S_BAD_NAME;
273         UPDATE_REMAIN(2);
274
275         /* MECH_OID */
276         if (p[0] != 0x06)
277             return GSS_S_BAD_NAME;
278         if (p[1] != GSS_EAP_MECHANISM->length)
279             return GSS_S_BAD_MECH;
280         if (memcmp(&p[2], GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length))
281             return GSS_S_BAD_MECH;
282         UPDATE_REMAIN(2 + GSS_EAP_MECHANISM->length);
283     }
284
285     /* NAME_LEN */
286     len = load_uint32_be(p);
287     UPDATE_REMAIN(4);
288
289     /* NAME */
290     CHECK_REMAIN(len);
291     buf.length = len;
292     buf.value = p;
293     UPDATE_REMAIN(len);
294
295     major = importUserName(minor, &buf, &name);
296     if (GSS_ERROR(major))
297         goto cleanup;
298
299     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
300         gss_buffer_desc buf;
301
302         buf.length = remain;
303         buf.value = p;
304
305         major = gssEapImportAttrContext(minor, &buf, name);
306         if (GSS_ERROR(major))
307             goto cleanup;
308     }
309
310     major = GSS_S_COMPLETE;
311
312 cleanup:
313     if (GSS_ERROR(major))
314         gssEapReleaseName(&tmpMinor, &name);
315     else
316         *pName = name;
317
318     return major;
319 }
320
321 static OM_uint32
322 importExportName(OM_uint32 *minor,
323                  const gss_buffer_t nameBuffer,
324                  gss_name_t *name)
325 {
326     return gssEapImportNameInternal(minor, nameBuffer, name,
327                                     EXPORT_NAME_FLAG_OID);
328 }
329
330 #ifdef HAVE_GSS_C_NT_COMPOSITE_EXPORT
331 static OM_uint32
332 importCompositeExportName(OM_uint32 *minor,
333                           const gss_buffer_t nameBuffer,
334                           gss_name_t *name)
335 {
336     return gssEapImportNameInternal(minor, nameBuffer, name,
337                                     EXPORT_NAME_FLAG_OID |
338                                     EXPORT_NAME_FLAG_COMPOSITE);
339 }
340 #endif
341
342 struct gss_eap_name_import_provider {
343     gss_OID oid;
344     OM_uint32 (*import)(OM_uint32 *, const gss_buffer_t, gss_name_t *);
345 };
346
347 OM_uint32
348 gssEapImportName(OM_uint32 *minor,
349                  const gss_buffer_t nameBuffer,
350                  gss_OID nameType,
351                  gss_name_t *name)
352 {
353     struct gss_eap_name_import_provider nameTypes[] = {
354         { GSS_C_NT_USER_NAME,               importUserName              },
355         { GSS_EAP_NT_PRINCIPAL_NAME,        importUserName              },
356         { GSS_C_NT_HOSTBASED_SERVICE,       importServiceName           },
357         { GSS_C_NT_HOSTBASED_SERVICE_X,     importServiceName           },
358         { GSS_C_NT_EXPORT_NAME,             importExportName            },
359 #ifdef HAVE_GSS_C_NT_COMPOSITE_EXPORT
360         { GSS_C_NT_COMPOSITE_EXPORT,        importCompositeExportName   },
361 #endif
362     };
363     size_t i;
364
365     *name = GSS_C_NO_NAME;
366
367     if (nameType == GSS_C_NO_OID)
368         nameType = nameTypes[0].oid;
369
370     for (i = 0; i < sizeof(nameTypes) / sizeof(nameTypes[0]); i++) {
371         if (oidEqual(nameTypes[i].oid, nameType))
372             return nameTypes[i].import(minor, nameBuffer, name);
373     }
374
375     return GSS_S_BAD_NAMETYPE;
376 }
377
378 OM_uint32
379 gssEapExportName(OM_uint32 *minor,
380                  const gss_name_t name,
381                  gss_buffer_t exportedName)
382 {
383     return gssEapExportNameInternal(minor, name, exportedName,
384                                     EXPORT_NAME_FLAG_OID);
385 }
386
387 OM_uint32
388 gssEapExportNameInternal(OM_uint32 *minor,
389                          const gss_name_t name,
390                          gss_buffer_t exportedName,
391                          unsigned int flags)
392 {
393     OM_uint32 major = GSS_S_FAILURE, tmpMinor;
394     krb5_context krbContext;
395     char *krbName = NULL;
396     size_t krbNameLen, exportedNameLen;
397     unsigned char *p;
398     gss_buffer_desc attrs = GSS_C_EMPTY_BUFFER;
399
400     exportedName->length = 0;
401     exportedName->value = NULL;
402
403     GSSEAP_KRB_INIT(&krbContext);
404
405     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
406     if (*minor != 0) {
407         major = GSS_S_FAILURE;
408         goto cleanup;
409     }
410     krbNameLen = strlen(krbName);
411
412     exportedNameLen = 0;
413     if (flags & EXPORT_NAME_FLAG_OID) {
414         exportedNameLen += 6 + GSS_EAP_MECHANISM->length;
415     }
416     exportedNameLen += 4 + krbNameLen;
417     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
418         major = gssEapExportAttrContext(minor, name, &attrs);
419         if (GSS_ERROR(major))
420             goto cleanup;
421         exportedNameLen += attrs.length;
422     }
423
424     exportedName->value = GSSEAP_MALLOC(exportedNameLen);
425     if (exportedName->value == NULL) {
426         major = GSS_S_FAILURE;
427         *minor = ENOMEM;
428         goto cleanup;
429     }
430     exportedName->length = exportedNameLen;
431
432     p = (unsigned char *)exportedName->value;
433
434     if (flags & EXPORT_NAME_FLAG_OID) {
435         /* TOK | MECH_OID_LEN */
436         store_uint16_be((flags & EXPORT_NAME_FLAG_COMPOSITE)
437                         ? TOK_TYPE_EXPORT_NAME_COMPOSITE
438                         : TOK_TYPE_EXPORT_NAME,
439                         p);
440         p += 2;
441         store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
442         p += 2;
443
444         /* MECH_OID */
445         *p++ = 0x06;
446         *p++ = GSS_EAP_MECHANISM->length & 0xff;
447         memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
448         p += GSS_EAP_MECHANISM->length;
449     }
450
451     /* NAME_LEN */
452     store_uint32_be(krbNameLen, p);
453     p += 4;
454
455     /* NAME */
456     memcpy(p, krbName, krbNameLen);
457     p += krbNameLen;
458
459     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
460         memcpy(p, attrs.value, attrs.length);
461         p += attrs.length;
462     }
463
464     assert(p == (unsigned char *)exportedName->value + exportedNameLen);
465
466     *minor = 0;
467     major = GSS_S_COMPLETE;
468
469 cleanup:
470     gss_release_buffer(&tmpMinor, &attrs);
471     if (GSS_ERROR(major))
472         gss_release_buffer(&tmpMinor, exportedName);
473     krb5_free_unparsed_name(krbContext, krbName);
474
475     return major;
476 }
477
478 OM_uint32
479 gssEapDuplicateName(OM_uint32 *minor,
480                     const gss_name_t input_name,
481                     gss_name_t *dest_name)
482 {
483     OM_uint32 major, tmpMinor;
484     krb5_context krbContext;
485     gss_name_t name;
486
487     if (input_name == GSS_C_NO_NAME) {
488         *minor = EINVAL;
489         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME;
490     }
491
492     GSSEAP_KRB_INIT(&krbContext);
493
494     major = gssEapAllocName(minor, &name);
495     if (GSS_ERROR(major)) {
496         return major;
497     }
498
499     *minor = krb5_copy_principal(krbContext, input_name->krbPrincipal,
500                                  &name->krbPrincipal);
501     if (*minor != 0) {
502         major = GSS_S_FAILURE;
503         goto cleanup;
504     }
505
506     if (input_name->attrCtx != NULL) {
507         major = gssEapDuplicateAttrContext(minor, input_name, name);
508         if (GSS_ERROR(major))
509             goto cleanup;
510     }
511
512     *dest_name = name;
513
514 cleanup:
515     if (GSS_ERROR(major)) {
516         gssEapReleaseName(&tmpMinor, &name);
517     }
518
519     return major;
520 }
521
522 OM_uint32
523 gssEapDisplayName(OM_uint32 *minor,
524                   gss_name_t name,
525                   gss_buffer_t output_name_buffer,
526                   gss_OID *output_name_type)
527 {
528     OM_uint32 major;
529     krb5_context krbContext;
530     char *krbName;
531
532     GSSEAP_KRB_INIT(&krbContext);
533
534     output_name_buffer->length = 0;
535     output_name_buffer->value = NULL;
536
537     if (name == GSS_C_NO_NAME) {
538         *minor = EINVAL;
539         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME;
540     }
541
542     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
543     if (*minor != 0) {
544         return GSS_S_FAILURE;
545     }
546
547     major = makeStringBuffer(minor, krbName, output_name_buffer);
548     if (GSS_ERROR(major)) {
549         krb5_free_unparsed_name(krbContext, krbName);
550         return major;
551     }
552
553     krb5_free_unparsed_name(krbContext, krbName);
554
555     if (output_name_type != NULL)
556         *output_name_type = GSS_EAP_NT_PRINCIPAL_NAME;
557
558     return GSS_S_COMPLETE;
559 }