Better error reporting through com_err
[mech_eap.git] / 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     *minor = krb5_sname_to_principal(krbContext, host, service,
170                                      KRB5_NT_SRV_HST, &krbPrinc);
171     if (*minor != 0) {
172         GSSEAP_FREE(service);
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         CHECK_REMAIN(4);
303         buf.length = load_uint32_be(p);
304         UPDATE_REMAIN(4);
305
306         CHECK_REMAIN(buf.length);
307         buf.value = p;
308         UPDATE_REMAIN(buf.length);
309
310         major = gssEapImportAttrContext(minor, &buf, name);
311         if (GSS_ERROR(major))
312             goto cleanup;
313     }
314
315     major = GSS_S_COMPLETE;
316
317 cleanup:
318     if (GSS_ERROR(major))
319         gssEapReleaseName(&tmpMinor, &name);
320     else
321         *pName = name;
322
323     return major;
324 }
325
326 static OM_uint32
327 importExportName(OM_uint32 *minor,
328                  const gss_buffer_t nameBuffer,
329                  gss_name_t *name)
330 {
331     return gssEapImportNameInternal(minor, nameBuffer, name,
332                                     EXPORT_NAME_FLAG_OID);
333 }
334
335 #ifdef HAVE_GSS_C_NT_COMPOSITE_EXPORT
336 static OM_uint32
337 importCompositeExportName(OM_uint32 *minor,
338                           const gss_buffer_t nameBuffer,
339                           gss_name_t *name)
340 {
341     return gssEapImportNameInternal(minor, nameBuffer, name,
342                                     EXPORT_NAME_FLAG_OID |
343                                     EXPORT_NAME_FLAG_COMPOSITE);
344 }
345 #endif
346
347 struct gss_eap_name_import_provider {
348     gss_OID oid;
349     OM_uint32 (*import)(OM_uint32 *, const gss_buffer_t, gss_name_t *);
350 };
351
352 OM_uint32
353 gssEapImportName(OM_uint32 *minor,
354                  const gss_buffer_t nameBuffer,
355                  gss_OID nameType,
356                  gss_name_t *name)
357 {
358     struct gss_eap_name_import_provider nameTypes[] = {
359         { GSS_C_NT_USER_NAME,               importUserName              },
360         { GSS_EAP_NT_PRINCIPAL_NAME,        importUserName              },
361         { GSS_C_NT_HOSTBASED_SERVICE,       importServiceName           },
362         { GSS_C_NT_HOSTBASED_SERVICE_X,     importServiceName           },
363         { GSS_C_NT_EXPORT_NAME,             importExportName            },
364 #ifdef HAVE_GSS_C_NT_COMPOSITE_EXPORT
365         { GSS_C_NT_COMPOSITE_EXPORT,        importCompositeExportName   },
366 #endif
367     };
368     size_t i;
369
370     *name = GSS_C_NO_NAME;
371
372     if (nameType == GSS_C_NO_OID)
373         nameType = nameTypes[0].oid;
374
375     for (i = 0; i < sizeof(nameTypes) / sizeof(nameTypes[0]); i++) {
376         if (oidEqual(nameTypes[i].oid, nameType))
377             return nameTypes[i].import(minor, nameBuffer, name);
378     }
379
380     return GSS_S_BAD_NAMETYPE;
381 }
382
383 OM_uint32
384 gssEapExportName(OM_uint32 *minor,
385                  const gss_name_t name,
386                  gss_buffer_t exportedName)
387 {
388     return gssEapExportNameInternal(minor, name, exportedName,
389                                     EXPORT_NAME_FLAG_OID);
390 }
391
392 OM_uint32
393 gssEapExportNameInternal(OM_uint32 *minor,
394                          const gss_name_t name,
395                          gss_buffer_t exportedName,
396                          unsigned int flags)
397 {
398     OM_uint32 major = GSS_S_FAILURE, tmpMinor;
399     krb5_context krbContext;
400     char *krbName = NULL;
401     size_t krbNameLen, exportedNameLen;
402     unsigned char *p;
403     gss_buffer_desc attrs = GSS_C_EMPTY_BUFFER;
404
405     exportedName->length = 0;
406     exportedName->value = NULL;
407
408     GSSEAP_KRB_INIT(&krbContext);
409
410     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
411     if (*minor != 0) {
412         major = GSS_S_FAILURE;
413         goto cleanup;
414     }
415     krbNameLen = strlen(krbName);
416
417     exportedNameLen = 0;
418     if (flags & EXPORT_NAME_FLAG_OID) {
419         exportedNameLen += 6 + GSS_EAP_MECHANISM->length;
420     }
421     exportedNameLen += 4 + krbNameLen;
422     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
423         major = gssEapExportAttrContext(minor, name, &attrs);
424         if (GSS_ERROR(major))
425             goto cleanup;
426         exportedNameLen += 4 + attrs.length;
427     }
428
429     exportedName->value = GSSEAP_MALLOC(exportedNameLen);
430     if (exportedName->value == NULL) {
431         major = GSS_S_FAILURE;
432         *minor = ENOMEM;
433         goto cleanup;
434     }
435     exportedName->length = exportedNameLen;
436
437     p = (unsigned char *)exportedName->value;
438
439     if (flags & EXPORT_NAME_FLAG_OID) {
440         /* TOK | MECH_OID_LEN */
441         store_uint16_be((flags & EXPORT_NAME_FLAG_COMPOSITE)
442                         ? TOK_TYPE_EXPORT_NAME_COMPOSITE
443                         : TOK_TYPE_EXPORT_NAME,
444                         p);
445         p += 2;
446         store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
447         p += 2;
448
449         /* MECH_OID */
450         *p++ = 0x06;
451         *p++ = GSS_EAP_MECHANISM->length & 0xff;
452         memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
453         p += GSS_EAP_MECHANISM->length;
454     }
455
456     /* NAME_LEN */
457     store_uint32_be(krbNameLen, p);
458     p += 4;
459
460     /* NAME */
461     memcpy(p, krbName, krbNameLen);
462     p += krbNameLen;
463
464     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
465         store_uint32_be(attrs.length, p);
466         memcpy(&p[4], attrs.value, attrs.length);
467         p += 4 + attrs.length;
468     }
469
470     *minor = 0;
471     major = GSS_S_COMPLETE;
472
473 cleanup:
474     gss_release_buffer(&tmpMinor, &attrs);
475     if (GSS_ERROR(major))
476         gss_release_buffer(&tmpMinor, exportedName);
477     krb5_free_unparsed_name(krbContext, krbName);
478
479     return major;
480 }
481
482 OM_uint32
483 gssEapDuplicateName(OM_uint32 *minor,
484                     const gss_name_t input_name,
485                     gss_name_t *dest_name)
486 {
487     OM_uint32 major, tmpMinor;
488     krb5_context krbContext;
489     gss_name_t name;
490
491     if (input_name == GSS_C_NO_NAME) {
492         *minor = EINVAL;
493         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME;
494     }
495
496     GSSEAP_KRB_INIT(&krbContext);
497
498     major = gssEapAllocName(minor, &name);
499     if (GSS_ERROR(major)) {
500         return major;
501     }
502
503     *minor = krb5_copy_principal(krbContext, input_name->krbPrincipal,
504                                  &name->krbPrincipal);
505     if (*minor != 0) {
506         major = GSS_S_FAILURE;
507         goto cleanup;
508     }
509
510     if (input_name->attrCtx != NULL) {
511         major = gssEapDuplicateAttrContext(minor, input_name, name);
512         if (GSS_ERROR(major))
513             goto cleanup;
514     }
515
516     *dest_name = name;
517
518 cleanup:
519     if (GSS_ERROR(major)) {
520         gssEapReleaseName(&tmpMinor, &name);
521     }
522
523     return major;
524 }
525
526 OM_uint32
527 gssEapDisplayName(OM_uint32 *minor,
528                   gss_name_t name,
529                   gss_buffer_t output_name_buffer,
530                   gss_OID *output_name_type)
531 {
532     OM_uint32 major;
533     krb5_context krbContext;
534     char *krbName;
535
536     GSSEAP_KRB_INIT(&krbContext);
537
538     output_name_buffer->length = 0;
539     output_name_buffer->value = NULL;
540
541     if (name == GSS_C_NO_NAME) {
542         *minor = EINVAL;
543         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME;
544     }
545
546     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
547     if (*minor != 0) {
548         return GSS_S_FAILURE;
549     }
550
551     major = makeStringBuffer(minor, krbName, output_name_buffer);
552     if (GSS_ERROR(major)) {
553         krb5_free_unparsed_name(krbContext, krbName);
554         return major;
555     }
556
557     krb5_free_unparsed_name(krbContext, krbName);
558
559     if (output_name_type != NULL)
560         *output_name_type = GSS_EAP_NT_PRINCIPAL_NAME;
561
562     return GSS_S_COMPLETE;
563 }