cleanup
[mech_eap.orig] / util_reauth.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 #include "gssapiP_eap.h"
34
35 #include <dlfcn.h>
36
37 /*
38  * Fast reauthentication support for EAP GSS.
39  */
40
41 krb5_error_code
42 krb5_encrypt_tkt_part(krb5_context, const krb5_keyblock *, krb5_ticket *);
43
44 krb5_error_code
45 encode_krb5_ticket(const krb5_ticket *rep, krb5_data **code);
46
47 static krb5_error_code
48 getAcceptorKey(krb5_context krbContext,
49                gss_ctx_id_t ctx,
50                gss_cred_id_t cred,
51                krb5_principal *princ,
52                krb5_keyblock *key)
53 {
54     krb5_error_code code;
55     krb5_keytab keytab = NULL;
56     krb5_keytab_entry ktent = { 0 };
57     krb5_kt_cursor cursor = NULL;
58
59     *princ = NULL;
60     memset(key, 0, sizeof(*key));
61
62     code = krb5_kt_default(krbContext, &keytab);
63     if (code != 0)
64         goto cleanup;
65
66     if (cred != GSS_C_NO_CREDENTIAL && cred->name != GSS_C_NO_NAME) {
67         code = krb5_kt_get_entry(krbContext, keytab,
68                                  cred->name->krbPrincipal, 0,
69                                  ctx->encryptionType, &ktent);
70         if (code != 0)
71             goto cleanup;
72     } else {
73         code = krb5_kt_start_seq_get(krbContext, keytab, &cursor);
74         if (code != 0)
75             goto cleanup;
76
77         while ((code = krb5_kt_next_entry(krbContext, keytab,
78                                           &ktent, &cursor)) == 0) {
79             if (ktent.key.enctype == ctx->encryptionType)
80                 break;
81             else
82                 krb5_free_keytab_entry_contents(krbContext, &ktent);
83         }
84     }
85
86     if (code == 0) {
87         *princ = ktent.principal;
88         *key = ktent.key;
89     }
90
91 cleanup:
92     if (cred == GSS_C_NO_CREDENTIAL || cred->name == GSS_C_NO_NAME)
93         krb5_kt_end_seq_get(krbContext, keytab, &cursor);
94     krb5_kt_close(krbContext, keytab);
95
96     if (code != 0)
97         krb5_free_keytab_entry_contents(krbContext, &ktent);
98
99     return code;
100 }
101
102 static OM_uint32
103 freezeAttrContext(OM_uint32 *minor,
104                   gss_name_t initiatorName,
105                   krb5_const_principal acceptorPrinc,
106                   krb5_keyblock *session,
107                   krb5_authdata ***authdata)
108 {
109     OM_uint32 major, tmpMinor;
110     krb5_error_code code;
111     gss_buffer_desc attrBuf = GSS_C_EMPTY_BUFFER;
112     krb5_authdata *authData[2], authDatum = { 0 };
113     krb5_context krbContext;
114
115     GSSEAP_KRB_INIT(&krbContext);
116
117     major = gssEapExportAttrContext(minor, initiatorName, &attrBuf);
118     if (GSS_ERROR(major))
119         return major;
120
121     authDatum.ad_type = KRB5_AUTHDATA_RADIUS_AVP;
122     authDatum.length = attrBuf.length;
123     authDatum.contents = attrBuf.value;
124     authData[0] = &authDatum;
125     authData[1] = NULL;
126
127     code = krb5_make_authdata_kdc_issued(krbContext, session, acceptorPrinc,
128                                          authData, authdata);
129     if (code != 0) {
130         major = GSS_S_FAILURE;
131         *minor = code;
132     } else {
133         major = GSS_S_COMPLETE;
134     }
135
136     gss_release_buffer(&tmpMinor, &attrBuf);
137
138     return major;
139 }
140
141 OM_uint32
142 gssEapMakeReauthCreds(OM_uint32 *minor,
143                       gss_ctx_id_t ctx,
144                       gss_cred_id_t cred,
145                       gss_buffer_t credBuf)
146 {
147     OM_uint32 major = GSS_S_COMPLETE;
148     krb5_error_code code;
149     krb5_context krbContext = NULL;
150     krb5_ticket ticket = { 0 };
151     krb5_keyblock session = { 0 }, acceptorKey = { 0 };
152     krb5_enc_tkt_part enc_part = { 0 };
153     krb5_data *ticketData = NULL, *credsData = NULL;
154     krb5_creds creds = { 0 };
155     krb5_auth_context authContext = NULL;
156
157     credBuf->length = 0;
158     credBuf->value = NULL;
159
160     GSSEAP_KRB_INIT(&krbContext);
161
162     code = getAcceptorKey(krbContext, ctx, cred,
163                           &ticket.server, &acceptorKey);
164     if (code == KRB5_KT_NOTFOUND) {
165         gss_buffer_desc emptyToken = { 0, "" };
166
167         /*
168          * If we can't produce the KRB-CRED message, we need to
169          * return an empty (not NULL) token to the caller so we
170          * don't change the number of authentication legs.
171          */
172         return duplicateBuffer(minor, &emptyToken, credBuf);
173     } else if (code != 0)
174         goto cleanup;
175
176     enc_part.flags = TKT_FLG_INITIAL;
177
178     /*
179      * Generate a random session key to place in the ticket and
180      * sign the "KDC-Issued" authorization data element.
181      */
182     code = krb5_c_make_random_key(krbContext, ctx->encryptionType,
183                                   &session);
184     if (code != 0)
185         goto cleanup;
186
187     enc_part.session = &session;
188     enc_part.client = ctx->initiatorName->krbPrincipal;
189     enc_part.times.authtime = time(NULL);
190     enc_part.times.starttime = enc_part.times.authtime;
191     enc_part.times.endtime = ctx->expiryTime
192                              ? ctx->expiryTime
193                              : KRB5_INT32_MAX;
194     enc_part.times.renew_till = 0;
195
196     major = freezeAttrContext(minor, ctx->initiatorName, ticket.server,
197                               &session, &enc_part.authorization_data);
198     if (GSS_ERROR(major))
199         goto cleanup;
200
201     ticket.enc_part2 = &enc_part;
202
203     code = krb5_encrypt_tkt_part(krbContext, &acceptorKey, &ticket);
204     if (code != 0)
205         goto cleanup;
206
207     code = encode_krb5_ticket(&ticket, &ticketData);
208     if (code != 0)
209         goto cleanup;
210
211     creds.client = enc_part.client;
212     creds.server = ticket.server;
213     creds.keyblock = session;
214     creds.times = enc_part.times;
215     creds.ticket_flags = enc_part.flags;
216     creds.ticket = *ticketData;
217     creds.authdata = enc_part.authorization_data;
218
219     code = krb5_auth_con_init(krbContext, &authContext);
220     if (code != 0)
221         goto cleanup;
222
223     code = krb5_auth_con_setflags(krbContext, authContext, 0);
224     if (code != 0)
225         goto cleanup;
226
227     code = krb5_auth_con_setsendsubkey(krbContext, authContext,
228                                        &ctx->rfc3961Key);
229     if (code != 0)
230         goto cleanup;
231
232     code = krb5_mk_1cred(krbContext, authContext, &creds, &credsData, NULL);
233     if (code != 0)
234         goto cleanup;
235
236     krbDataToGssBuffer(credsData, credBuf);
237
238 cleanup:
239     if (ticket.enc_part.ciphertext.data != NULL)
240         GSSEAP_FREE(ticket.enc_part.ciphertext.data);
241     krb5_free_keyblock_contents(krbContext, &session);
242     krb5_free_keyblock_contents(krbContext, &acceptorKey);
243     krb5_free_data(krbContext, ticketData);
244     krb5_auth_con_free(krbContext, authContext);
245     krb5_free_authdata(krbContext, enc_part.authorization_data);
246     if (credsData != NULL)
247         GSSEAP_FREE(credsData);
248
249     if (major == GSS_S_COMPLETE) {
250         *minor = code;
251         major = code != 0 ? GSS_S_FAILURE : GSS_S_COMPLETE;
252     }
253
254     return major;
255 }
256
257 static int
258 isTicketGrantingServiceP(krb5_context krbContext,
259                          krb5_const_principal principal)
260 {
261     if (krb5_princ_size(krbContext, principal) == 2 &&
262         krb5_princ_component(krbContext, principal, 0)->length == 6 &&
263         memcmp(krb5_princ_component(krbContext,
264                                     principal, 0)->data, "krbtgt", 6) == 0)
265         return TRUE;
266
267     return FALSE;
268 }
269
270 OM_uint32
271 gssEapStoreReauthCreds(OM_uint32 *minor,
272                        gss_ctx_id_t ctx,
273                        gss_cred_id_t cred,
274                        gss_buffer_t credBuf)
275 {
276     OM_uint32 major = GSS_S_COMPLETE, code;
277     krb5_context krbContext = NULL;
278     krb5_auth_context authContext = NULL;
279     krb5_data credData = { 0 };
280     krb5_creds **creds = NULL;
281     krb5_principal canonPrinc;
282     int i;
283
284     if (credBuf->length == 0 || cred == GSS_C_NO_CREDENTIAL)
285         return GSS_S_COMPLETE;
286
287     GSSEAP_KRB_INIT(&krbContext);
288
289     code = krb5_auth_con_init(krbContext, &authContext);
290     if (code != 0)
291         goto cleanup;
292
293     code = krb5_auth_con_setflags(krbContext, authContext, 0);
294     if (code != 0)
295         goto cleanup;
296
297     code = krb5_auth_con_setrecvsubkey(krbContext, authContext,
298                                        &ctx->rfc3961Key);
299     if (code != 0)
300         goto cleanup;
301
302     gssBufferToKrbData(credBuf, &credData);
303
304     code = krb5_rd_cred(krbContext, authContext, &credData, &creds, NULL);
305     if (code != 0)
306         goto cleanup;
307
308     if (creds == NULL || creds[0] == NULL)
309         goto cleanup;
310
311     code = krb5_copy_principal(krbContext, creds[0]->client, &canonPrinc);
312     if (code != 0)
313         goto cleanup;
314
315     krb5_free_principal(krbContext, cred->name->krbPrincipal);
316     cred->name->krbPrincipal = canonPrinc;
317
318     cred->expiryTime = creds[0]->times.endtime;
319
320     code = krb5_cc_new_unique(krbContext, "MEMORY", NULL, &cred->krbCredCache);
321     if (code != 0)
322         goto cleanup;
323
324     code = krb5_cc_initialize(krbContext, cred->krbCredCache,
325                               creds[0]->client);
326     if (code != 0)
327         goto cleanup;
328
329     for (i = 0; creds[i] != NULL; i++) {
330         krb5_creds kcred = *(creds[i]);
331
332         /*
333          * Swap in the acceptor name the client asked for so
334          * get_credentials() works. We're making the assumption that
335          * any service tickets returned are for us. We'll need to
336          * reflect some more on whether that is a safe assumption.
337          */
338         if (!isTicketGrantingServiceP(krbContext, kcred.server))
339             kcred.server = ctx->acceptorName->krbPrincipal;
340
341         code = krb5_cc_store_cred(krbContext, cred->krbCredCache, &kcred);
342         if (code != 0)
343             goto cleanup;
344     }
345
346     /*
347      * To turn a credentials cache into a GSS credentials handle, we
348      * require the gss_krb5_import_cred() API (present in Heimdal, but
349      * not shipped in MIT yet).
350      */
351     major = gss_krb5_import_cred(minor, cred->krbCredCache, NULL, NULL,
352                                  &cred->krbCred);
353     if (GSS_ERROR(major))
354         goto cleanup;
355
356 cleanup:
357     *minor = code;
358
359     krb5_auth_con_free(krbContext, authContext);
360     if (creds != NULL) {
361         for (i = 0; creds[i] != NULL; i++)
362             krb5_free_creds(krbContext, creds[i]);
363     }
364     if (major == GSS_S_COMPLETE)
365         major = *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
366
367     return major;
368 }
369
370 static OM_uint32
371 (*gssInitSecContextNext)(OM_uint32 *,
372                          gss_cred_id_t,
373                          gss_ctx_id_t *,
374                          gss_name_t,
375                          gss_OID,
376                          OM_uint32,
377                          OM_uint32,
378                          gss_channel_bindings_t,
379                          gss_buffer_t,
380                          gss_OID *,
381                          gss_buffer_t,
382                          OM_uint32 *,
383                          OM_uint32 *);
384
385 static OM_uint32
386 (*gssAcceptSecContextNext)(OM_uint32 *,
387                            gss_ctx_id_t *,
388                            gss_cred_id_t,
389                            gss_buffer_t,
390                            gss_channel_bindings_t,
391                            gss_name_t *,
392                            gss_OID *,
393                            gss_buffer_t,
394                            OM_uint32 *,
395                            OM_uint32 *,
396                            gss_cred_id_t *);
397
398 static OM_uint32
399 (*gssReleaseCredNext)(OM_uint32 *, gss_cred_id_t *);
400
401 static OM_uint32
402 (*gssReleaseNameNext)(OM_uint32 *, gss_name_t *);
403
404 static OM_uint32
405 (*gssInquireSecContextByOidNext)(OM_uint32 *,
406                                  const gss_ctx_id_t,
407                                  const gss_OID,
408                                  gss_buffer_set_t *);
409
410 static OM_uint32
411 (*gssDeleteSecContextNext)(OM_uint32 *,
412                           gss_ctx_id_t *,
413                           gss_buffer_t);
414
415 static OM_uint32
416 (*gssDisplayNameNext)(OM_uint32 *,
417                       gss_name_t,
418                       gss_buffer_t,
419                       gss_OID *);
420
421 static OM_uint32
422 (*gssImportNameNext)(OM_uint32 *,
423                      gss_buffer_t,
424                      gss_OID,
425                      gss_name_t *);
426
427 static OM_uint32
428 (*gssStoreCredNext)(OM_uint32 *,
429                     const gss_cred_id_t,
430                     gss_cred_usage_t,
431                     const gss_OID,
432                     OM_uint32,
433                     OM_uint32,
434                     gss_OID_set *,
435                     gss_cred_usage_t *);
436
437 static OM_uint32
438 (*gssGetNameAttributeNext)(OM_uint32 *,
439                           gss_name_t,
440                           gss_buffer_t,
441                           int *,
442                           int *,
443                           gss_buffer_t,
444                           gss_buffer_t,
445                           int *);
446
447 #define NEXT_SYMBOL(local, global)  ((local) = dlsym(RTLD_NEXT, (global)))
448
449 OM_uint32
450 gssEapReauthInitialize(OM_uint32 *minor)
451 {
452     NEXT_SYMBOL(gssInitSecContextNext,                    "gss_init_sec_context");
453     NEXT_SYMBOL(gssAcceptSecContextNext,                  "gss_accept_sec_context");
454     NEXT_SYMBOL(gssReleaseCredNext,                       "gss_release_cred");
455     NEXT_SYMBOL(gssReleaseNameNext,                       "gss_release_name");
456     NEXT_SYMBOL(gssInquireSecContextByOidNext,            "gss_inquire_sec_context_by_oid");
457     NEXT_SYMBOL(gssDeleteSecContextNext,                  "gss_delete_sec_context");
458     NEXT_SYMBOL(gssDisplayNameNext,                       "gss_display_name");
459     NEXT_SYMBOL(gssImportNameNext,                        "gss_import_name");
460     NEXT_SYMBOL(gssStoreCredNext,                         "gss_store_cred");
461     NEXT_SYMBOL(gssGetNameAttributeNext,                  "gss_get_name_attribute");
462
463     return GSS_S_COMPLETE;
464 }
465
466 OM_uint32
467 gssInitSecContext(OM_uint32 *minor,
468                   gss_cred_id_t cred,
469                   gss_ctx_id_t *context_handle,
470                   gss_name_t target_name,
471                   gss_OID mech_type,
472                   OM_uint32 req_flags,
473                   OM_uint32 time_req,
474                   gss_channel_bindings_t input_chan_bindings,
475                   gss_buffer_t input_token,
476                   gss_OID *actual_mech_type,
477                   gss_buffer_t output_token,
478                   OM_uint32 *ret_flags,
479                   OM_uint32 *time_rec)
480 {
481     if (gssInitSecContextNext == NULL)
482         return GSS_S_UNAVAILABLE;
483
484     return gssInitSecContextNext(minor, cred, context_handle,
485                                  target_name, mech_type, req_flags,
486                                  time_req, input_chan_bindings,
487                                  input_token, actual_mech_type,
488                                  output_token, ret_flags, time_rec);
489 }
490
491 OM_uint32
492 gssAcceptSecContext(OM_uint32 *minor,
493                     gss_ctx_id_t *context_handle,
494                     gss_cred_id_t cred,
495                     gss_buffer_t input_token,
496                     gss_channel_bindings_t input_chan_bindings,
497                     gss_name_t *src_name,
498                     gss_OID *mech_type,
499                     gss_buffer_t output_token,
500                     OM_uint32 *ret_flags,
501                     OM_uint32 *time_rec,
502                     gss_cred_id_t *delegated_cred_handle)
503 {
504     if (gssAcceptSecContextNext == NULL)
505         return GSS_S_UNAVAILABLE;
506
507     return gssAcceptSecContextNext(minor, context_handle, cred,
508                                    input_token, input_chan_bindings,
509                                    src_name, mech_type, output_token,
510                                    ret_flags, time_rec, delegated_cred_handle);
511 }
512
513 OM_uint32
514 gssReleaseCred(OM_uint32 *minor,
515                gss_cred_id_t *cred_handle)
516 {
517     if (gssReleaseCredNext == NULL)
518         return GSS_S_UNAVAILABLE;
519
520     return gssReleaseCredNext(minor, cred_handle);
521 }
522
523 OM_uint32
524 gssReleaseName(OM_uint32 *minor,
525                gss_name_t *name)
526 {
527     if (gssReleaseName == NULL)
528         return GSS_S_UNAVAILABLE;
529
530     return gssReleaseNameNext(minor, name);
531 }
532
533 OM_uint32
534 gssDeleteSecContext(OM_uint32 *minor,
535                     gss_ctx_id_t *context_handle,
536                     gss_buffer_t output_token)
537 {
538     if (gssDeleteSecContextNext == NULL)
539         return GSS_S_UNAVAILABLE;
540
541     return gssDeleteSecContextNext(minor, context_handle, output_token);
542 }
543
544 static OM_uint32
545 gssDisplayName(OM_uint32 *minor,
546                gss_name_t name,
547                gss_buffer_t buffer,
548                gss_OID *name_type)
549 {
550     if (gssDisplayNameNext == NULL)
551         return GSS_S_UNAVAILABLE;
552
553     return gssDisplayNameNext(minor, name, buffer, name_type);
554 }
555
556 static OM_uint32
557 gssImportName(OM_uint32 *minor,
558               gss_buffer_t buffer,
559               gss_OID name_type,
560               gss_name_t *name)
561 {
562     if (gssImportNameNext == NULL)
563         return GSS_S_UNAVAILABLE;
564
565     return gssImportNameNext(minor, buffer, name_type, name);
566 }
567
568 OM_uint32
569 gssInquireSecContextByOid(OM_uint32 *minor,
570                           const gss_ctx_id_t context_handle,
571                           const gss_OID desired_object,
572                           gss_buffer_set_t *data_set)
573 {
574     if (gssInquireSecContextByOidNext == NULL)
575         return GSS_S_UNAVAILABLE;
576
577     return gssInquireSecContextByOidNext(minor, context_handle,
578                                          desired_object, data_set);
579 }
580
581 OM_uint32
582 gssStoreCred(OM_uint32 *minor,
583              const gss_cred_id_t input_cred_handle,
584              gss_cred_usage_t input_usage,
585              const gss_OID desired_mech,
586              OM_uint32 overwrite_cred,
587              OM_uint32 default_cred,
588              gss_OID_set *elements_stored,
589              gss_cred_usage_t *cred_usage_stored)
590 {
591     if (gssStoreCredNext == NULL)
592         return GSS_S_UNAVAILABLE;
593
594     return gssStoreCredNext(minor, input_cred_handle, input_usage,
595                             desired_mech, overwrite_cred, default_cred,
596                             elements_stored, cred_usage_stored);
597 }
598
599 OM_uint32
600 gssGetNameAttribute(OM_uint32 *minor,
601                     gss_name_t name,
602                     gss_buffer_t attr,
603                     int *authenticated,
604                     int *complete,
605                     gss_buffer_t value,
606                     gss_buffer_t display_value,
607                     int *more)
608 {
609     if (gssGetNameAttributeNext == NULL)
610         return GSS_S_UNAVAILABLE;
611
612     return gssGetNameAttributeNext(minor, name, attr, authenticated, complete,
613                                    value, display_value, more);
614 }
615
616 static gss_buffer_desc radiusAvpKrbAttr = {
617     sizeof("urn:authdata-radius-avp") - 1, "urn:authdata-radius-avp"
618 };
619
620 /*
621  * Unfortunately extracting an AD-KDCIssued authorization data element
622  * is pretty implementation-dependent. It's not possible to verify the
623  * signature ourselves because the ticket session key is not exposed
624  * outside GSS. In an ideal world, all AD-KDCIssued elements would be
625  * verified by the Kerberos library and authentication would fail if
626  * verification failed. We're not quite there yet and as a result have
627  * to go through some hoops to get this to work. The alternative would
628  * be to sign the authorization data with our long-term key, but it
629  * seems a pity to compromise the design because of current implementation
630  * limitations.
631  *
632  * (Specifically, the hoops involve a libkrb5 authorisation data plugin
633  * that exposes the verified and serialised attribute context through
634  * the Kerberos GSS mechanism's naming extensions API.)
635  */
636 static OM_uint32
637 defrostAttrContext(OM_uint32 *minor,
638                    gss_name_t glueName,
639                    gss_name_t mechName)
640 {
641     OM_uint32 major, tmpMinor;
642     gss_buffer_desc authData = GSS_C_EMPTY_BUFFER;
643     gss_buffer_desc authDataDisplay = GSS_C_EMPTY_BUFFER;
644     int more = -1;
645     int authenticated, complete;
646
647     major = gssGetNameAttribute(minor, glueName, &radiusAvpKrbAttr,
648                                 &authenticated, &complete,
649                                 &authData, &authDataDisplay, &more);
650     if (major == GSS_S_COMPLETE) {
651         if (authenticated == 0)
652             major = GSS_S_BAD_NAME;
653         else
654             major = gssEapImportAttrContext(minor, &authData, mechName);
655     } else if (major == GSS_S_UNAVAILABLE) {
656         major = GSS_S_COMPLETE;
657     }
658
659     gss_release_buffer(&tmpMinor, &authData);
660     gss_release_buffer(&tmpMinor, &authDataDisplay);
661
662     return major;
663 }
664
665 OM_uint32
666 gssEapGlueToMechName(OM_uint32 *minor,
667                      gss_name_t glueName,
668                      gss_name_t *pMechName)
669 {
670     OM_uint32 major, tmpMinor;
671     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
672
673     *pMechName = GSS_C_NO_NAME;
674
675     major = gssDisplayName(minor, glueName, &nameBuf, NULL);
676     if (GSS_ERROR(major))
677         goto cleanup;
678
679     major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
680                              pMechName);
681     if (GSS_ERROR(major))
682         goto cleanup;
683
684     major = defrostAttrContext(minor, glueName, *pMechName);
685     if (GSS_ERROR(major))
686         goto cleanup;
687
688 cleanup:
689     if (GSS_ERROR(major)) {
690         gssReleaseName(&tmpMinor, pMechName);
691         *pMechName = GSS_C_NO_NAME;
692     }
693
694     gss_release_buffer(&tmpMinor, &nameBuf);
695
696     return major;
697 }
698
699 OM_uint32
700 gssEapMechToGlueName(OM_uint32 *minor,
701                      gss_name_t mechName,
702                      gss_name_t *pGlueName)
703 {
704     OM_uint32 major, tmpMinor;
705     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
706
707     *pGlueName = GSS_C_NO_NAME;
708
709     major = gssEapDisplayName(minor, mechName, &nameBuf, NULL);
710     if (GSS_ERROR(major))
711         goto cleanup;
712
713     major = gssImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
714                           pGlueName);
715     if (GSS_ERROR(major))
716         goto cleanup;
717
718 cleanup:
719     gss_release_buffer(&tmpMinor, &nameBuf);
720
721     return major;
722 }
723
724 /*
725  * Suck out the analgous elements of a Kerberos GSS context into an EAP
726  * one so that the application doesn't know the difference.
727  */
728 OM_uint32
729 gssEapReauthComplete(OM_uint32 *minor,
730                     gss_ctx_id_t ctx,
731                     gss_cred_id_t cred,
732                     const gss_OID mech,
733                     OM_uint32 timeRec)
734 {
735     OM_uint32 major, tmpMinor;
736     gss_buffer_set_t keyData = GSS_C_NO_BUFFER_SET;
737
738     if (!oidEqual(mech, gss_mech_krb5)) {
739         major = GSS_S_BAD_MECH;
740         goto cleanup;
741     }
742
743     /* Get the raw subsession key and encryptino type*/
744     major = gssInquireSecContextByOid(minor, ctx->kerberosCtx,
745                                       GSS_C_INQ_SSPI_SESSION_KEY, &keyData);
746     if (GSS_ERROR(major))
747         goto cleanup;
748
749     {
750         gss_OID_desc oid;
751         int suffix;
752
753         oid.length = keyData->elements[1].length;
754         oid.elements = keyData->elements[1].value;
755
756         /* GSS_KRB5_SESSION_KEY_ENCTYPE_OID */
757         major = decomposeOid(minor,
758                              "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x04",
759                              10, &oid, &suffix);
760         if (GSS_ERROR(major))
761             goto cleanup;
762
763         ctx->encryptionType = suffix;
764     }
765
766     {
767         krb5_context krbContext = NULL;
768         krb5_keyblock key;
769
770         GSSEAP_KRB_INIT(&krbContext);
771
772         KRB_KEY_LENGTH(&key) = keyData->elements[0].length;
773         KRB_KEY_DATA(&key)   = keyData->elements[0].value;
774         KRB_KEY_TYPE(&key)   = ctx->encryptionType;
775
776         *minor = krb5_copy_keyblock_contents(krbContext,
777                                              &key, &ctx->rfc3961Key);
778         if (*minor != 0) {
779             major = GSS_S_FAILURE;
780             goto cleanup;
781         }
782     }
783
784     major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
785                                       &ctx->checksumType);
786     if (GSS_ERROR(major))
787         goto cleanup;
788
789     if (timeRec != GSS_C_INDEFINITE)
790         ctx->expiryTime = time(NULL) + timeRec;
791
792     major = sequenceInit(minor,
793                          &ctx->seqState, ctx->recvSeq,
794                          ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
795                          ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
796                          TRUE);
797     if (GSS_ERROR(major))
798         goto cleanup;
799
800     major = GSS_S_COMPLETE;
801
802 cleanup:
803     gss_release_buffer_set(&tmpMinor, &keyData);
804
805     return major;
806 }