fix comment explaining key derivation
[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             major = GSS_S_BAD_NAME;     \
232             *minor = GSSEAP_TOK_TRUNC;  \
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     *minor = 0;
313
314 cleanup:
315     if (GSS_ERROR(major))
316         gssEapReleaseName(&tmpMinor, &name);
317     else
318         *pName = name;
319
320     return major;
321 }
322
323 static OM_uint32
324 importExportName(OM_uint32 *minor,
325                  const gss_buffer_t nameBuffer,
326                  gss_name_t *name)
327 {
328     return gssEapImportNameInternal(minor, nameBuffer, name,
329                                     EXPORT_NAME_FLAG_OID);
330 }
331
332 #ifdef HAVE_GSS_C_NT_COMPOSITE_EXPORT
333 static OM_uint32
334 importCompositeExportName(OM_uint32 *minor,
335                           const gss_buffer_t nameBuffer,
336                           gss_name_t *name)
337 {
338     return gssEapImportNameInternal(minor, nameBuffer, name,
339                                     EXPORT_NAME_FLAG_OID |
340                                     EXPORT_NAME_FLAG_COMPOSITE);
341 }
342 #endif
343
344 struct gss_eap_name_import_provider {
345     gss_OID oid;
346     OM_uint32 (*import)(OM_uint32 *, const gss_buffer_t, gss_name_t *);
347 };
348
349 OM_uint32
350 gssEapImportName(OM_uint32 *minor,
351                  const gss_buffer_t nameBuffer,
352                  gss_OID nameType,
353                  gss_name_t *name)
354 {
355     struct gss_eap_name_import_provider nameTypes[] = {
356         { GSS_C_NT_USER_NAME,               importUserName              },
357         { GSS_EAP_NT_PRINCIPAL_NAME,        importUserName              },
358         { GSS_C_NT_HOSTBASED_SERVICE,       importServiceName           },
359         { GSS_C_NT_HOSTBASED_SERVICE_X,     importServiceName           },
360         { GSS_C_NT_EXPORT_NAME,             importExportName            },
361 #ifdef HAVE_GSS_C_NT_COMPOSITE_EXPORT
362         { GSS_C_NT_COMPOSITE_EXPORT,        importCompositeExportName   },
363 #endif
364     };
365     size_t i;
366
367     *name = GSS_C_NO_NAME;
368
369     if (nameType == GSS_C_NO_OID)
370         nameType = nameTypes[0].oid;
371
372     for (i = 0; i < sizeof(nameTypes) / sizeof(nameTypes[0]); i++) {
373         if (oidEqual(nameTypes[i].oid, nameType))
374             return nameTypes[i].import(minor, nameBuffer, name);
375     }
376
377     return GSS_S_BAD_NAMETYPE;
378 }
379
380 OM_uint32
381 gssEapExportName(OM_uint32 *minor,
382                  const gss_name_t name,
383                  gss_buffer_t exportedName)
384 {
385     return gssEapExportNameInternal(minor, name, exportedName,
386                                     EXPORT_NAME_FLAG_OID);
387 }
388
389 OM_uint32
390 gssEapExportNameInternal(OM_uint32 *minor,
391                          const gss_name_t name,
392                          gss_buffer_t exportedName,
393                          unsigned int flags)
394 {
395     OM_uint32 major = GSS_S_FAILURE, tmpMinor;
396     krb5_context krbContext;
397     char *krbName = NULL;
398     size_t krbNameLen, exportedNameLen;
399     unsigned char *p;
400     gss_buffer_desc attrs = GSS_C_EMPTY_BUFFER;
401
402     exportedName->length = 0;
403     exportedName->value = NULL;
404
405     GSSEAP_KRB_INIT(&krbContext);
406
407     *minor = krb5_unparse_name(krbContext, name->krbPrincipal, &krbName);
408     if (*minor != 0) {
409         major = GSS_S_FAILURE;
410         goto cleanup;
411     }
412     krbNameLen = strlen(krbName);
413
414     exportedNameLen = 0;
415     if (flags & EXPORT_NAME_FLAG_OID) {
416         exportedNameLen += 6 + GSS_EAP_MECHANISM->length;
417     }
418     exportedNameLen += 4 + krbNameLen;
419     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
420         major = gssEapExportAttrContext(minor, name, &attrs);
421         if (GSS_ERROR(major))
422             goto cleanup;
423         exportedNameLen += attrs.length;
424     }
425
426     exportedName->value = GSSEAP_MALLOC(exportedNameLen);
427     if (exportedName->value == NULL) {
428         major = GSS_S_FAILURE;
429         *minor = ENOMEM;
430         goto cleanup;
431     }
432     exportedName->length = exportedNameLen;
433
434     p = (unsigned char *)exportedName->value;
435
436     if (flags & EXPORT_NAME_FLAG_OID) {
437         /* TOK | MECH_OID_LEN */
438         store_uint16_be((flags & EXPORT_NAME_FLAG_COMPOSITE)
439                         ? TOK_TYPE_EXPORT_NAME_COMPOSITE
440                         : TOK_TYPE_EXPORT_NAME,
441                         p);
442         p += 2;
443         store_uint16_be(GSS_EAP_MECHANISM->length + 2, p);
444         p += 2;
445
446         /* MECH_OID */
447         *p++ = 0x06;
448         *p++ = GSS_EAP_MECHANISM->length & 0xff;
449         memcpy(p, GSS_EAP_MECHANISM->elements, GSS_EAP_MECHANISM->length);
450         p += GSS_EAP_MECHANISM->length;
451     }
452
453     /* NAME_LEN */
454     store_uint32_be(krbNameLen, p);
455     p += 4;
456
457     /* NAME */
458     memcpy(p, krbName, krbNameLen);
459     p += krbNameLen;
460
461     if (flags & EXPORT_NAME_FLAG_COMPOSITE) {
462         memcpy(p, attrs.value, attrs.length);
463         p += attrs.length;
464     }
465
466     assert(p == (unsigned char *)exportedName->value + exportedNameLen);
467
468     major = GSS_S_COMPLETE;
469     *minor = 0;
470
471 cleanup:
472     gss_release_buffer(&tmpMinor, &attrs);
473     if (GSS_ERROR(major))
474         gss_release_buffer(&tmpMinor, exportedName);
475     krb5_free_unparsed_name(krbContext, krbName);
476
477     return major;
478 }
479
480 OM_uint32
481 gssEapDuplicateName(OM_uint32 *minor,
482                     const gss_name_t input_name,
483                     gss_name_t *dest_name)
484 {
485     OM_uint32 major, tmpMinor;
486     krb5_context krbContext;
487     gss_name_t name;
488
489     if (input_name == GSS_C_NO_NAME) {
490         *minor = EINVAL;
491         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME;
492     }
493
494     GSSEAP_KRB_INIT(&krbContext);
495
496     major = gssEapAllocName(minor, &name);
497     if (GSS_ERROR(major)) {
498         return major;
499     }
500
501     name->flags = input_name->flags;
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 }