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