2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 #include "gssapiP_eap.h"
38 * Fast reauthentication support for EAP GSS.
42 krb5_encrypt_tkt_part(krb5_context, const krb5_keyblock *, krb5_ticket *);
45 encode_krb5_ticket(const krb5_ticket *rep, krb5_data **code);
48 gssDisplayName(OM_uint32 *minor,
54 gssImportName(OM_uint32 *minor,
59 static krb5_error_code
60 getAcceptorKey(krb5_context krbContext,
63 krb5_principal *princ,
67 krb5_keytab keytab = NULL;
68 krb5_keytab_entry ktent = { 0 };
69 krb5_kt_cursor cursor = NULL;
72 memset(key, 0, sizeof(*key));
74 code = krb5_kt_default(krbContext, &keytab);
78 if (cred != GSS_C_NO_CREDENTIAL && cred->name != GSS_C_NO_NAME) {
79 code = krb5_kt_get_entry(krbContext, keytab,
80 cred->name->krbPrincipal, 0,
81 ctx->encryptionType, &ktent);
85 code = krb5_kt_start_seq_get(krbContext, keytab, &cursor);
89 while ((code = krb5_kt_next_entry(krbContext, keytab,
90 &ktent, &cursor)) == 0) {
91 if (ktent.key.enctype == ctx->encryptionType)
94 krb5_free_keytab_entry_contents(krbContext, &ktent);
99 *princ = ktent.principal;
104 if (cred == GSS_C_NO_CREDENTIAL || cred->name == GSS_C_NO_NAME)
105 krb5_kt_end_seq_get(krbContext, keytab, &cursor);
106 krb5_kt_close(krbContext, keytab);
109 krb5_free_keytab_entry_contents(krbContext, &ktent);
115 freezeAttrContext(OM_uint32 *minor,
116 gss_name_t initiatorName,
117 krb5_const_principal acceptorPrinc,
118 krb5_keyblock *session,
119 krb5_authdata ***authdata)
121 OM_uint32 major, tmpMinor;
122 krb5_error_code code;
123 gss_buffer_desc attrBuf = GSS_C_EMPTY_BUFFER;
124 krb5_authdata *authData[2], authDatum = { 0 };
125 krb5_context krbContext;
127 GSSEAP_KRB_INIT(&krbContext);
129 major = gssEapExportAttrContext(minor, initiatorName, &attrBuf);
130 if (GSS_ERROR(major))
133 authDatum.ad_type = KRB5_AUTHDATA_RADIUS_AVP;
134 authDatum.length = attrBuf.length;
135 authDatum.contents = attrBuf.value;
136 authData[0] = &authDatum;
139 code = krb5_make_authdata_kdc_issued(krbContext, session, acceptorPrinc,
142 major = GSS_S_FAILURE;
145 major = GSS_S_COMPLETE;
148 gss_release_buffer(&tmpMinor, &attrBuf);
154 * Fabricate a ticket to ourselves given a GSS EAP context.
157 gssEapMakeReauthCreds(OM_uint32 *minor,
160 gss_buffer_t credBuf)
162 OM_uint32 major = GSS_S_COMPLETE;
163 krb5_error_code code;
164 krb5_context krbContext = NULL;
165 krb5_ticket ticket = { 0 };
166 krb5_keyblock session = { 0 }, acceptorKey = { 0 };
167 krb5_enc_tkt_part enc_part = { 0 };
168 krb5_data *ticketData = NULL, *credsData = NULL;
169 krb5_creds creds = { 0 };
170 krb5_auth_context authContext = NULL;
173 credBuf->value = NULL;
175 GSSEAP_KRB_INIT(&krbContext);
177 code = getAcceptorKey(krbContext, ctx, cred,
178 &ticket.server, &acceptorKey);
179 if (code == KRB5_KT_NOTFOUND) {
180 gss_buffer_desc emptyToken = { 0, "" };
183 * If we can't produce the KRB-CRED message, we need to
184 * return an empty (not NULL) token to the caller so we
185 * don't change the number of authentication legs.
187 return duplicateBuffer(minor, &emptyToken, credBuf);
188 } else if (code != 0)
191 enc_part.flags = TKT_FLG_INITIAL;
194 * Generate a random session key to place in the ticket and
195 * sign the "KDC-Issued" authorization data element.
197 code = krb5_c_make_random_key(krbContext, ctx->encryptionType,
202 enc_part.session = &session;
203 enc_part.client = ctx->initiatorName->krbPrincipal;
204 enc_part.times.authtime = time(NULL);
205 enc_part.times.starttime = enc_part.times.authtime;
206 enc_part.times.endtime = ctx->expiryTime
209 enc_part.times.renew_till = 0;
211 major = freezeAttrContext(minor, ctx->initiatorName, ticket.server,
212 &session, &enc_part.authorization_data);
213 if (GSS_ERROR(major))
216 ticket.enc_part2 = &enc_part;
218 code = krb5_encrypt_tkt_part(krbContext, &acceptorKey, &ticket);
222 code = encode_krb5_ticket(&ticket, &ticketData);
226 creds.client = enc_part.client;
227 creds.server = ticket.server;
228 creds.keyblock = session;
229 creds.times = enc_part.times;
230 creds.ticket_flags = enc_part.flags;
231 creds.ticket = *ticketData;
232 creds.authdata = enc_part.authorization_data;
234 code = krb5_auth_con_init(krbContext, &authContext);
238 code = krb5_auth_con_setflags(krbContext, authContext, 0);
242 code = krb5_auth_con_setsendsubkey(krbContext, authContext,
247 code = krb5_mk_1cred(krbContext, authContext, &creds, &credsData, NULL);
251 krbDataToGssBuffer(credsData, credBuf);
254 if (ticket.enc_part.ciphertext.data != NULL)
255 GSSEAP_FREE(ticket.enc_part.ciphertext.data);
256 krb5_free_keyblock_contents(krbContext, &session);
257 krb5_free_keyblock_contents(krbContext, &acceptorKey);
258 krb5_free_data(krbContext, ticketData);
259 krb5_auth_con_free(krbContext, authContext);
260 krb5_free_authdata(krbContext, enc_part.authorization_data);
261 if (credsData != NULL)
262 GSSEAP_FREE(credsData);
264 if (major == GSS_S_COMPLETE) {
266 major = code != 0 ? GSS_S_FAILURE : GSS_S_COMPLETE;
273 isTicketGrantingServiceP(krb5_context krbContext,
274 krb5_const_principal principal)
276 if (krb5_princ_size(krbContext, principal) == 2 &&
277 krb5_princ_component(krbContext, principal, 0)->length == 6 &&
278 memcmp(krb5_princ_component(krbContext,
279 principal, 0)->data, "krbtgt", 6) == 0)
286 * Store re-authentication (Kerberos) credentials in a credential handle.
289 gssEapStoreReauthCreds(OM_uint32 *minor,
292 gss_buffer_t credBuf)
294 OM_uint32 major = GSS_S_COMPLETE, code;
295 krb5_context krbContext = NULL;
296 krb5_auth_context authContext = NULL;
297 krb5_data credData = { 0 };
298 krb5_creds **creds = NULL;
299 krb5_principal canonPrinc;
302 if (credBuf->length == 0 || cred == GSS_C_NO_CREDENTIAL)
303 return GSS_S_COMPLETE;
305 GSSEAP_KRB_INIT(&krbContext);
307 code = krb5_auth_con_init(krbContext, &authContext);
311 code = krb5_auth_con_setflags(krbContext, authContext, 0);
315 code = krb5_auth_con_setrecvsubkey(krbContext, authContext,
320 gssBufferToKrbData(credBuf, &credData);
322 code = krb5_rd_cred(krbContext, authContext, &credData, &creds, NULL);
326 if (creds == NULL || creds[0] == NULL)
329 code = krb5_copy_principal(krbContext, creds[0]->client, &canonPrinc);
333 krb5_free_principal(krbContext, cred->name->krbPrincipal);
334 cred->name->krbPrincipal = canonPrinc;
336 cred->expiryTime = creds[0]->times.endtime;
338 code = krb5_cc_new_unique(krbContext, "MEMORY", NULL, &cred->krbCredCache);
342 code = krb5_cc_initialize(krbContext, cred->krbCredCache,
347 for (i = 0; creds[i] != NULL; i++) {
348 krb5_creds kcred = *(creds[i]);
351 * Swap in the acceptor name the client asked for so
352 * get_credentials() works. We're making the assumption that
353 * any service tickets returned are for us. We'll need to
354 * reflect some more on whether that is a safe assumption.
356 if (!isTicketGrantingServiceP(krbContext, kcred.server))
357 kcred.server = ctx->acceptorName->krbPrincipal;
359 code = krb5_cc_store_cred(krbContext, cred->krbCredCache, &kcred);
365 * To turn a credentials cache into a GSS credentials handle, we
366 * require the gss_krb5_import_cred() API (present in Heimdal, but
367 * not shipped in MIT yet).
369 major = gss_krb5_import_cred(minor, cred->krbCredCache, NULL, NULL,
371 if (GSS_ERROR(major))
377 krb5_auth_con_free(krbContext, authContext);
379 for (i = 0; creds[i] != NULL; i++)
380 krb5_free_creds(krbContext, creds[i]);
382 if (major == GSS_S_COMPLETE)
383 major = *minor ? GSS_S_FAILURE : GSS_S_COMPLETE;
388 static gss_buffer_desc radiusAvpKrbAttr = {
389 sizeof("urn:authdata-radius-avp") - 1, "urn:authdata-radius-avp"
393 * Unfortunately extracting an AD-KDCIssued authorization data element
394 * is pretty implementation-dependent. It's not possible to verify the
395 * signature ourselves because the ticket session key is not exposed
396 * outside GSS. In an ideal world, all AD-KDCIssued elements would be
397 * verified by the Kerberos library and authentication would fail if
398 * verification failed. We're not quite there yet and as a result have
399 * to go through some hoops to get this to work. The alternative would
400 * be to sign the authorization data with our long-term key, but it
401 * seems a pity to compromise the design because of current implementation
404 * (Specifically, the hoops involve a libkrb5 authorisation data plugin
405 * that exposes the verified and serialised attribute context through
406 * the Kerberos GSS mechanism's naming extensions API.)
409 defrostAttrContext(OM_uint32 *minor,
413 OM_uint32 major, tmpMinor;
414 gss_buffer_desc authData = GSS_C_EMPTY_BUFFER;
415 gss_buffer_desc authDataDisplay = GSS_C_EMPTY_BUFFER;
417 int authenticated, complete;
419 major = gssGetNameAttribute(minor, glueName, &radiusAvpKrbAttr,
420 &authenticated, &complete,
421 &authData, &authDataDisplay, &more);
422 if (major == GSS_S_COMPLETE) {
423 if (authenticated == 0)
424 major = GSS_S_BAD_NAME;
426 major = gssEapImportAttrContext(minor, &authData, mechName);
427 } else if (major == GSS_S_UNAVAILABLE) {
428 major = GSS_S_COMPLETE;
431 gss_release_buffer(&tmpMinor, &authData);
432 gss_release_buffer(&tmpMinor, &authDataDisplay);
438 * Convert a mechanism glue to an EAP mechanism name by displaying and
439 * importing it. This also handles the RADIUS attributes.
442 gssEapGlueToMechName(OM_uint32 *minor,
444 gss_name_t *pMechName)
446 OM_uint32 major, tmpMinor;
447 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
449 *pMechName = GSS_C_NO_NAME;
451 major = gssDisplayName(minor, glueName, &nameBuf, NULL);
452 if (GSS_ERROR(major))
455 major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
457 if (GSS_ERROR(major))
460 major = defrostAttrContext(minor, glueName, *pMechName);
461 if (GSS_ERROR(major))
465 if (GSS_ERROR(major)) {
466 gssReleaseName(&tmpMinor, pMechName);
467 *pMechName = GSS_C_NO_NAME;
470 gss_release_buffer(&tmpMinor, &nameBuf);
476 * Convert an EAP mechanism name to a mechanism glue name by displaying
480 gssEapMechToGlueName(OM_uint32 *minor,
482 gss_name_t *pGlueName)
484 OM_uint32 major, tmpMinor;
485 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
487 *pGlueName = GSS_C_NO_NAME;
489 major = gssEapDisplayName(minor, mechName, &nameBuf, NULL);
490 if (GSS_ERROR(major))
493 major = gssImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
495 if (GSS_ERROR(major))
499 gss_release_buffer(&tmpMinor, &nameBuf);
505 * Suck out the analgous elements of a Kerberos GSS context into an EAP
506 * one so that the application doesn't know the difference.
509 gssEapReauthComplete(OM_uint32 *minor,
515 OM_uint32 major, tmpMinor;
516 gss_buffer_set_t keyData = GSS_C_NO_BUFFER_SET;
518 if (!oidEqual(mech, gss_mech_krb5)) {
519 major = GSS_S_BAD_MECH;
523 /* Get the raw subsession key and encryptino type*/
524 major = gssInquireSecContextByOid(minor, ctx->kerberosCtx,
525 GSS_C_INQ_SSPI_SESSION_KEY, &keyData);
526 if (GSS_ERROR(major))
533 oid.length = keyData->elements[1].length;
534 oid.elements = keyData->elements[1].value;
536 /* GSS_KRB5_SESSION_KEY_ENCTYPE_OID */
537 major = decomposeOid(minor,
538 "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x04",
540 if (GSS_ERROR(major))
543 ctx->encryptionType = suffix;
547 krb5_context krbContext = NULL;
550 GSSEAP_KRB_INIT(&krbContext);
552 KRB_KEY_LENGTH(&key) = keyData->elements[0].length;
553 KRB_KEY_DATA(&key) = keyData->elements[0].value;
554 KRB_KEY_TYPE(&key) = ctx->encryptionType;
556 *minor = krb5_copy_keyblock_contents(krbContext,
557 &key, &ctx->rfc3961Key);
559 major = GSS_S_FAILURE;
564 major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
566 if (GSS_ERROR(major))
569 if (timeRec != GSS_C_INDEFINITE)
570 ctx->expiryTime = time(NULL) + timeRec;
572 /* Initialize our sequence state */
573 major = sequenceInit(minor,
574 &ctx->seqState, ctx->recvSeq,
575 ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
576 ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
578 if (GSS_ERROR(major))
581 major = GSS_S_COMPLETE;
584 gss_release_buffer_set(&tmpMinor, &keyData);
590 * The remainder of this file consists of wrappers so we can call into the
591 * mechanism glue without calling ourselves.
594 (*gssInitSecContextNext)(OM_uint32 *,
601 gss_channel_bindings_t,
609 (*gssAcceptSecContextNext)(OM_uint32 *,
613 gss_channel_bindings_t,
622 (*gssReleaseCredNext)(OM_uint32 *, gss_cred_id_t *);
625 (*gssReleaseNameNext)(OM_uint32 *, gss_name_t *);
628 (*gssInquireSecContextByOidNext)(OM_uint32 *,
634 (*gssDeleteSecContextNext)(OM_uint32 *,
639 (*gssDisplayNameNext)(OM_uint32 *,
645 (*gssImportNameNext)(OM_uint32 *,
651 (*gssStoreCredNext)(OM_uint32 *,
661 (*gssGetNameAttributeNext)(OM_uint32 *,
670 #define NEXT_SYMBOL(local, global) ((local) = dlsym(RTLD_NEXT, (global)))
673 gssEapReauthInitialize(OM_uint32 *minor)
675 NEXT_SYMBOL(gssInitSecContextNext, "gss_init_sec_context");
676 NEXT_SYMBOL(gssAcceptSecContextNext, "gss_accept_sec_context");
677 NEXT_SYMBOL(gssReleaseCredNext, "gss_release_cred");
678 NEXT_SYMBOL(gssReleaseNameNext, "gss_release_name");
679 NEXT_SYMBOL(gssInquireSecContextByOidNext, "gss_inquire_sec_context_by_oid");
680 NEXT_SYMBOL(gssDeleteSecContextNext, "gss_delete_sec_context");
681 NEXT_SYMBOL(gssDisplayNameNext, "gss_display_name");
682 NEXT_SYMBOL(gssImportNameNext, "gss_import_name");
683 NEXT_SYMBOL(gssStoreCredNext, "gss_store_cred");
684 NEXT_SYMBOL(gssGetNameAttributeNext, "gss_get_name_attribute");
686 return GSS_S_COMPLETE;
690 gssInitSecContext(OM_uint32 *minor,
692 gss_ctx_id_t *context_handle,
693 gss_name_t target_name,
697 gss_channel_bindings_t input_chan_bindings,
698 gss_buffer_t input_token,
699 gss_OID *actual_mech_type,
700 gss_buffer_t output_token,
701 OM_uint32 *ret_flags,
704 if (gssInitSecContextNext == NULL)
705 return GSS_S_UNAVAILABLE;
707 return gssInitSecContextNext(minor, cred, context_handle,
708 target_name, mech_type, req_flags,
709 time_req, input_chan_bindings,
710 input_token, actual_mech_type,
711 output_token, ret_flags, time_rec);
715 gssAcceptSecContext(OM_uint32 *minor,
716 gss_ctx_id_t *context_handle,
718 gss_buffer_t input_token,
719 gss_channel_bindings_t input_chan_bindings,
720 gss_name_t *src_name,
722 gss_buffer_t output_token,
723 OM_uint32 *ret_flags,
725 gss_cred_id_t *delegated_cred_handle)
727 if (gssAcceptSecContextNext == NULL)
728 return GSS_S_UNAVAILABLE;
730 return gssAcceptSecContextNext(minor, context_handle, cred,
731 input_token, input_chan_bindings,
732 src_name, mech_type, output_token,
733 ret_flags, time_rec, delegated_cred_handle);
737 gssReleaseCred(OM_uint32 *minor,
738 gss_cred_id_t *cred_handle)
740 if (gssReleaseCredNext == NULL)
741 return GSS_S_UNAVAILABLE;
743 return gssReleaseCredNext(minor, cred_handle);
747 gssReleaseName(OM_uint32 *minor,
750 if (gssReleaseName == NULL)
751 return GSS_S_UNAVAILABLE;
753 return gssReleaseNameNext(minor, name);
757 gssDeleteSecContext(OM_uint32 *minor,
758 gss_ctx_id_t *context_handle,
759 gss_buffer_t output_token)
761 if (gssDeleteSecContextNext == NULL)
762 return GSS_S_UNAVAILABLE;
764 return gssDeleteSecContextNext(minor, context_handle, output_token);
768 gssDisplayName(OM_uint32 *minor,
773 if (gssDisplayNameNext == NULL)
774 return GSS_S_UNAVAILABLE;
776 return gssDisplayNameNext(minor, name, buffer, name_type);
780 gssImportName(OM_uint32 *minor,
785 if (gssImportNameNext == NULL)
786 return GSS_S_UNAVAILABLE;
788 return gssImportNameNext(minor, buffer, name_type, name);
792 gssInquireSecContextByOid(OM_uint32 *minor,
793 const gss_ctx_id_t context_handle,
794 const gss_OID desired_object,
795 gss_buffer_set_t *data_set)
797 if (gssInquireSecContextByOidNext == NULL)
798 return GSS_S_UNAVAILABLE;
800 return gssInquireSecContextByOidNext(minor, context_handle,
801 desired_object, data_set);
805 gssStoreCred(OM_uint32 *minor,
806 const gss_cred_id_t input_cred_handle,
807 gss_cred_usage_t input_usage,
808 const gss_OID desired_mech,
809 OM_uint32 overwrite_cred,
810 OM_uint32 default_cred,
811 gss_OID_set *elements_stored,
812 gss_cred_usage_t *cred_usage_stored)
814 if (gssStoreCredNext == NULL)
815 return GSS_S_UNAVAILABLE;
817 return gssStoreCredNext(minor, input_cred_handle, input_usage,
818 desired_mech, overwrite_cred, default_cred,
819 elements_stored, cred_usage_stored);
823 gssGetNameAttribute(OM_uint32 *minor,
829 gss_buffer_t display_value,
832 if (gssGetNameAttributeNext == NULL)
833 return GSS_S_UNAVAILABLE;
835 return gssGetNameAttributeNext(minor, name, attr, authenticated, complete,
836 value, display_value, more);