X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=accept_sec_context.c;h=723c93244b449c3a1fc508e458127831377bf8b3;hb=d683d523c7b4b88a15423069520221ac575376e1;hp=6208341d5fab79439c255bca926f50656d6e16df;hpb=2f177997f0fd336c95567772c7fe0df36a4fa4bd;p=mech_eap.git diff --git a/accept_sec_context.c b/accept_sec_context.c index 6208341..723c932 100644 --- a/accept_sec_context.c +++ b/accept_sec_context.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, JANET(UK) + * Copyright (c) 2011, JANET(UK) * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,402 +30,755 @@ * SUCH DAMAGE. */ +/* + * Establish a security context on the acceptor (server). These functions + * wrap around libradsec and (thus) talk to a RADIUS server or proxy. + */ + #include "gssapiP_eap.h" -#define EAP_MAX_METHODS 8 - -#define EAP_TTLS_AUTH_PAP 1 -#define EAP_TTLS_AUTH_CHAP 2 -#define EAP_TTLS_AUTH_MSCHAP 4 -#define EAP_TTLS_AUTH_MSCHAPV2 8 - -#if 1 -struct eap_user { - struct { - int vendor; - u32 method; - } methods[EAP_MAX_METHODS]; - u8 *password; - size_t password_len; - int password_hash; /* whether password is hashed with - * nt_password_hash() */ - int phase2; - int force_version; - int ttls_auth; /* bitfield of - * EAP_TTLS_AUTH_{PAP,CHAP,MSCHAP,MSCHAPV2} */ -}; +#ifdef GSSEAP_ENABLE_REAUTH +static OM_uint32 +eapGssSmAcceptGssReauth(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags); +#endif -struct eap_eapol_interface { - /* Lower layer to full authenticator variables */ - Boolean eapResp; /* shared with EAPOL Backend Authentication */ - struct wpabuf *eapRespData; - Boolean portEnabled; - int retransWhile; - Boolean eapRestart; /* shared with EAPOL Authenticator PAE */ - int eapSRTT; - int eapRTTVAR; - - /* Full authenticator to lower layer variables */ - Boolean eapReq; /* shared with EAPOL Backend Authentication */ - Boolean eapNoReq; /* shared with EAPOL Backend Authentication */ - Boolean eapSuccess; - Boolean eapFail; - Boolean eapTimeout; - struct wpabuf *eapReqData; - u8 *eapKeyData; - size_t eapKeyDataLen; - Boolean eapKeyAvailable; /* called keyAvailable in IEEE 802.1X-2004 */ - - /* AAA interface to full authenticator variables */ - Boolean aaaEapReq; - Boolean aaaEapNoReq; - Boolean aaaSuccess; - Boolean aaaFail; - struct wpabuf *aaaEapReqData; - u8 *aaaEapKeyData; - size_t aaaEapKeyDataLen; - Boolean aaaEapKeyAvailable; - int aaaMethodTimeout; - - /* Full authenticator to AAA interface variables */ - Boolean aaaEapResp; - struct wpabuf *aaaEapRespData; - /* aaaIdentity -> eap_get_identity() */ - Boolean aaaTimeout; -}; +/* + * Mark an acceptor context as ready for cryptographic operations + */ +static OM_uint32 +acceptReadyEap(OM_uint32 *minor, gss_ctx_id_t ctx, gss_cred_id_t cred) +{ + OM_uint32 major, tmpMinor; + VALUE_PAIR *vp; + gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER; -#define eapol_callbacks SERVER_eapol_callbacks + /* Cache encryption type derived from selected mechanism OID */ + major = gssEapOidToEnctype(minor, ctx->mechanismUsed, + &ctx->encryptionType); + if (GSS_ERROR(major)) + return major; -struct eapol_callbacks { - int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len, - int phase2, struct eap_user *user); - const char * (*get_eap_req_id_text)(void *ctx, size_t *len); -}; + gssEapReleaseName(&tmpMinor, &ctx->initiatorName); -#define eap_config SERVER_eap_config - -struct eap_config { - void *ssl_ctx; - void *msg_ctx; - void *eap_sim_db_priv; - Boolean backend_auth; - int eap_server; - u8 *pac_opaque_encr_key; - u8 *eap_fast_a_id; - size_t eap_fast_a_id_len; - char *eap_fast_a_id_info; - int eap_fast_prov; - int pac_key_lifetime; - int pac_key_refresh_time; - int eap_sim_aka_result_ind; - int tnc; - struct wps_context *wps; - const struct wpabuf *assoc_wps_ie; - const u8 *peer_addr; - int fragment_size; -}; + major = gssEapRadiusGetRawAvp(minor, ctx->acceptorCtx.vps, + PW_USER_NAME, 0, &vp); + if (major == GSS_S_COMPLETE) { + nameBuf.length = vp->length; + nameBuf.value = vp->vp_strvalue; + } else { + ctx->gssFlags |= GSS_C_ANON_FLAG; + } + + major = gssEapImportName(minor, &nameBuf, + (ctx->gssFlags & GSS_C_ANON_FLAG) ? + GSS_C_NT_ANONYMOUS : GSS_C_NT_USER_NAME, + &ctx->initiatorName); + if (GSS_ERROR(major)) + return major; + + major = gssEapRadiusGetRawAvp(minor, ctx->acceptorCtx.vps, + PW_MS_MPPE_SEND_KEY, VENDORPEC_MS, &vp); + if (GSS_ERROR(major)) { + *minor = GSSEAP_KEY_UNAVAILABLE; + return GSS_S_UNAVAILABLE; + } + + major = gssEapDeriveRfc3961Key(minor, + vp->vp_octets, + vp->length, + ctx->encryptionType, + &ctx->rfc3961Key); + if (GSS_ERROR(major)) + return major; + + major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key, + &ctx->checksumType); + if (GSS_ERROR(major)) + return major; + + major = sequenceInit(minor, + &ctx->seqState, ctx->recvSeq, + ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0), + ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0), + TRUE); + if (GSS_ERROR(major)) + return major; + + major = gssEapCreateAttrContext(minor, cred, ctx, + &ctx->initiatorName->attrCtx, + &ctx->expiryTime); + if (GSS_ERROR(major)) + return major; + + *minor = 0; + return GSS_S_COMPLETE; +} + +static OM_uint32 +eapGssSmAcceptAcceptorName(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) +{ + OM_uint32 major; -struct eap_sm * eap_server_sm_init(void *eapol_ctx, - struct eapol_callbacks *eapol_cb, - struct eap_config *eap_conf); -void eap_server_sm_deinit(struct eap_sm *sm); -int eap_server_sm_step(struct eap_sm *sm); -void eap_sm_notify_cached(struct eap_sm *sm); -void eap_sm_pending_cb(struct eap_sm *sm); -int eap_sm_method_pending(struct eap_sm *sm); -const u8 * eap_get_identity(struct eap_sm *sm, size_t *len); -struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm); + /* XXX TODO import and validate name from inputToken */ -#include + if (ctx->acceptorName != GSS_C_NO_NAME) { + /* Send desired target name to acceptor */ + major = gssEapDisplayName(minor, ctx->acceptorName, + outputToken, NULL); + if (GSS_ERROR(major)) + return major; + } + + return GSS_S_CONTINUE_NEEDED; +} +#ifdef GSSEAP_DEBUG static OM_uint32 -initTls(OM_uint32 *minor, - gss_ctx_id_t ctx) +eapGssSmAcceptVendorInfo(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) { - struct tls_config tconf; - struct tls_connection_params tparams; + fprintf(stderr, "GSS-EAP: vendor: %.*s\n", + (int)inputToken->length, (char *)inputToken->value); - memset(&tconf, 0, sizeof(tconf)); - ctx->acceptorCtx.tlsContext = tls_init(&tconf); - if (ctx->acceptorCtx.tlsContext == NULL) - return GSS_S_FAILURE; + return GSS_S_CONTINUE_NEEDED; +} +#endif - memset(&tparams, 0, sizeof(tparams)); - tparams.ca_cert = "ca.pem"; - tparams.client_cert = "server.pem"; - tparams.private_key = "server-key.pem"; - if (tls_global_set_params(ctx->acceptorCtx.tlsContext, &tparams)) { - return GSS_S_FAILURE; +/* + * Emit a identity EAP request to force the initiator (peer) to identify + * itself. + */ +static OM_uint32 +eapGssSmAcceptIdentity(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) +{ + OM_uint32 major; + struct wpabuf *reqData; + gss_buffer_desc pktBuffer; + + if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) { + *minor = GSSEAP_CRED_MECH_MISMATCH; + return GSS_S_BAD_MECH; + } + + if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0) { + *minor = GSSEAP_WRONG_SIZE; + return GSS_S_DEFECTIVE_TOKEN; } - if (tls_global_set_verify(ctx->acceptorCtx.tlsContext, 0)) { + reqData = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, 0, + EAP_CODE_REQUEST, 0); + if (reqData == NULL) { + *minor = ENOMEM; return GSS_S_FAILURE; } - return GSS_S_COMPLETE; + pktBuffer.length = wpabuf_len(reqData); + pktBuffer.value = (void *)wpabuf_head(reqData); + + major = duplicateBuffer(minor, &pktBuffer, outputToken); + if (GSS_ERROR(major)) + return major; + + wpabuf_free(reqData); + + GSSEAP_SM_TRANSITION_NEXT(ctx); + + *minor = 0; + + return GSS_S_CONTINUE_NEEDED; } +/* + * Returns TRUE if the input token contains an EAP identity response. + */ static int -serverGetEapUser(void *ctx, - const unsigned char *identity, - size_t identityLength, - int phase2, - struct eap_user *user) +isIdentityResponseP(gss_buffer_t inputToken) { - gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx; - OM_uint32 major, minor; - gss_buffer_desc buf; + struct wpabuf respData; + + wpabuf_set(&respData, inputToken->value, inputToken->length); - memset(user, 0, sizeof(*user)); + return (eap_get_type(&respData) == EAP_TYPE_IDENTITY); +} + +/* + * Save the asserted initiator identity from the EAP identity response. + */ +static OM_uint32 +importInitiatorIdentity(OM_uint32 *minor, + gss_ctx_id_t ctx, + gss_buffer_t inputToken) +{ + OM_uint32 tmpMinor; + struct wpabuf respData; + const unsigned char *pos; + size_t len; + gss_buffer_desc nameBuf; - buf.length = identityLength; - buf.value = (void *)identity; + wpabuf_set(&respData, inputToken->value, inputToken->length); - if (phase2 == 0) { - user->methods[0].vendor = EAP_VENDOR_IETF; - user->methods[0].method = EAP_TYPE_PEAP; - return 0; + pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, + &respData, &len); + if (pos == NULL) { + *minor = GSSEAP_PEER_BAD_MESSAGE; + return GSS_S_DEFECTIVE_TOKEN; } - major = gssEapImportName(&minor, &buf, GSS_C_NT_USER_NAME, - &gssCtx->initiatorName); - if (GSS_ERROR(major)) - return -1; + nameBuf.value = (void *)pos; + nameBuf.length = len; - /* - * OK, obviously there is no real security here, this is simply - * for testing the token exchange; this code will be completely - * replaced with libradsec once that library is available. - */ - user->methods[0].vendor = EAP_VENDOR_IETF; - user->methods[0].method = EAP_TYPE_MSCHAPV2; - user->password = (unsigned char *)strdup(" "); - user->password_len = 1; + gssEapReleaseName(&tmpMinor, &ctx->initiatorName); - return 0; + return gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME, + &ctx->initiatorName); } -static const char * -serverGetEapReqIdText(void *ctx, - size_t *len) +/* + * Pass the asserted initiator identity to the authentication server. + */ +static OM_uint32 +setInitiatorIdentity(OM_uint32 *minor, + gss_ctx_id_t ctx, + VALUE_PAIR **vps) { - *len = 0; - return NULL; + OM_uint32 major, tmpMinor; + gss_buffer_desc nameBuf; + + /* + * We should have got an EAP identity response, but if we didn't, then + * we will just avoid sending User-Name. Note that radsecproxy requires + * User-Name to be sent on every request (presumably so it can remain + * stateless). + */ + if (ctx->initiatorName != GSS_C_NO_NAME) { + major = gssEapDisplayName(minor, ctx->initiatorName, &nameBuf, NULL); + if (GSS_ERROR(major)) + return major; + + major = gssEapRadiusAddAvp(minor, vps, PW_USER_NAME, 0, &nameBuf); + if (GSS_ERROR(major)) + return major; + + gss_release_buffer(&tmpMinor, &nameBuf); + } + + *minor = 0; + return GSS_S_COMPLETE; } -#endif +/* + * Pass the asserted acceptor identity to the authentication server. + */ static OM_uint32 -acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx) +setAcceptorIdentity(OM_uint32 *minor, + gss_ctx_id_t ctx, + VALUE_PAIR **vps) { OM_uint32 major; - krb5_context krbContext; + gss_buffer_desc nameBuf; + krb5_context krbContext = NULL; + krb5_principal krbPrinc; + struct rs_context *rc = ctx->acceptorCtx.radContext; + + assert(rc != NULL); + + if (ctx->acceptorName == GSS_C_NO_NAME) { + *minor = 0; + return GSS_S_COMPLETE; + } + + if ((ctx->acceptorName->flags & NAME_FLAG_SERVICE) == 0) { + *minor = GSSEAP_BAD_SERVICE_NAME; + return GSS_S_BAD_NAME; + } GSSEAP_KRB_INIT(&krbContext); - /* Cache encryption type derived from selected mechanism OID */ - major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType); + krbPrinc = ctx->acceptorName->krbPrincipal; + assert(krbPrinc != NULL); + assert(KRB_PRINC_LENGTH(krbPrinc) >= 2); + + /* Acceptor-Service-Name */ + krbPrincComponentToGssBuffer(krbPrinc, 0, &nameBuf); + + major = gssEapRadiusAddAvp(minor, vps, + PW_GSS_ACCEPTOR_SERVICE_NAME, + VENDORPEC_UKERNA, + &nameBuf); if (GSS_ERROR(major)) return major; - if (ctx->encryptionType != ENCTYPE_NULL && - ctx->acceptorCtx.eapPolInterface->eapKeyAvailable) { - major = gssEapDeriveRFC3961Key(minor, - ctx->acceptorCtx.eapPolInterface->eapKeyData, - ctx->acceptorCtx.eapPolInterface->eapKeyDataLen, - ctx->encryptionType, - &ctx->rfc3961Key); + /* Acceptor-Host-Name */ + krbPrincComponentToGssBuffer(krbPrinc, 1, &nameBuf); + + major = gssEapRadiusAddAvp(minor, vps, + PW_GSS_ACCEPTOR_HOST_NAME, + VENDORPEC_UKERNA, + &nameBuf); + if (GSS_ERROR(major)) + return major; + + if (KRB_PRINC_LENGTH(krbPrinc) > 2) { + /* Acceptor-Service-Specific */ + krb5_principal_data ssiPrinc = *krbPrinc; + char *ssi; + + KRB_PRINC_LENGTH(&ssiPrinc) -= 2; + KRB_PRINC_NAME(&ssiPrinc) += 2; + + *minor = krb5_unparse_name_flags(krbContext, &ssiPrinc, + KRB5_PRINCIPAL_UNPARSE_NO_REALM, &ssi); + if (*minor != 0) + return GSS_S_FAILURE; + + nameBuf.value = ssi; + nameBuf.length = strlen(ssi); + + major = gssEapRadiusAddAvp(minor, vps, + PW_GSS_ACCEPTOR_SERVICE_SPECIFIC, + VENDORPEC_UKERNA, + &nameBuf); + + if (GSS_ERROR(major)) { + krb5_free_unparsed_name(krbContext, ssi); + return major; + } + krb5_free_unparsed_name(krbContext, ssi); + } + + krbPrincRealmToGssBuffer(krbPrinc, &nameBuf); + if (nameBuf.length != 0) { + /* Acceptor-Realm-Name */ + major = gssEapRadiusAddAvp(minor, vps, + PW_GSS_ACCEPTOR_REALM_NAME, + VENDORPEC_UKERNA, + &nameBuf); if (GSS_ERROR(major)) return major; - } else { - /* - * draft-howlett-eap-gss says that integrity/confidentialty should - * always be advertised as available, but if we have no keying - * material it seems confusing to the caller to advertise this. - */ - ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG); } - sequenceInit(&ctx->seqState, ctx->recvSeq, - ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0), - ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0), - TRUE); + *minor = 0; + return GSS_S_COMPLETE; +} + +/* + * Allocate a RadSec handle + */ +static OM_uint32 +createRadiusHandle(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx) +{ + struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx; + const char *configFile = RS_CONFIG_FILE; + const char *configStanza = "gss-eap"; + struct rs_alloc_scheme ralloc; + struct rs_error *err; + + assert(actx->radContext == NULL); + assert(actx->radConn == NULL); + + if (rs_context_create(&actx->radContext, RS_DICT_FILE) != 0) { + *minor = GSSEAP_RADSEC_CONTEXT_FAILURE; + return GSS_S_FAILURE; + } + + if (cred->radiusConfigFile != NULL) + configFile = cred->radiusConfigFile; + if (cred->radiusConfigStanza != NULL) + configStanza = cred->radiusConfigStanza; + + ralloc.calloc = GSSEAP_CALLOC; + ralloc.malloc = GSSEAP_MALLOC; + ralloc.free = GSSEAP_FREE; + ralloc.realloc = GSSEAP_REALLOC; + + rs_context_set_alloc_scheme(actx->radContext, &ralloc); + + if (rs_context_read_config(actx->radContext, configFile) != 0) { + err = rs_err_ctx_pop(actx->radContext); + goto fail; + } + + if (rs_conn_create(actx->radContext, &actx->radConn, configStanza) != 0) { + err = rs_err_conn_pop(actx->radConn); + goto fail; + } + + if (actx->radServer != NULL) { + if (rs_conn_select_peer(actx->radConn, actx->radServer) != 0) { + err = rs_err_conn_pop(actx->radConn); + goto fail; + } + } + *minor = 0; return GSS_S_COMPLETE; + +fail: + return gssEapRadiusMapError(minor, err); } +/* + * Process a EAP response from the initiator. + */ static OM_uint32 eapGssSmAcceptAuthenticate(OM_uint32 *minor, - gss_ctx_id_t ctx, gss_cred_id_t cred, - gss_buffer_t inputToken, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, gss_channel_bindings_t chanBindings, - gss_buffer_t outputToken) + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) { - OM_uint32 major; - OM_uint32 tmpMinor, tmpMajor; - int code; - struct wpabuf respData; - static struct eapol_callbacks cb = { serverGetEapUser, serverGetEapReqIdText }; - if (ctx->acceptorCtx.eap == NULL) { - struct eap_config eapConfig; - - major = initTls(minor, ctx); + OM_uint32 major, tmpMinor; + struct rs_connection *rconn; + struct rs_request *request = NULL; + struct rs_packet *req = NULL, *resp = NULL; + struct radius_packet *frreq, *frresp; + + if (ctx->acceptorCtx.radContext == NULL) { + /* May be NULL from an imported partial context */ + major = createRadiusHandle(minor, cred, ctx); if (GSS_ERROR(major)) goto cleanup; + } - memset(&eapConfig, 0, sizeof(eapConfig)); - eapConfig.eap_server = 1; - eapConfig.ssl_ctx = ctx->acceptorCtx.tlsContext; + if (isIdentityResponseP(inputToken)) { + major = importInitiatorIdentity(minor, ctx, inputToken); + if (GSS_ERROR(major)) + return major; + } - ctx->acceptorCtx.eap = eap_server_sm_init(ctx, &cb, &eapConfig); - if (ctx->acceptorCtx.eap == NULL) { - major = GSS_S_FAILURE; - goto cleanup; - } + rconn = ctx->acceptorCtx.radConn; - ctx->acceptorCtx.eapPolInterface = eap_get_interface(ctx->acceptorCtx.eap); - ctx->acceptorCtx.eapPolInterface->portEnabled = TRUE; - ctx->acceptorCtx.eapPolInterface->eapRestart = TRUE; + if (rs_packet_create_authn_request(rconn, &req, NULL, NULL) != 0) { + major = gssEapRadiusMapError(minor, rs_err_conn_pop(rconn)); + goto cleanup; } + frreq = rs_packet_frpkt(req); + + major = setInitiatorIdentity(minor, ctx, &frreq->vps); + if (GSS_ERROR(major)) + goto cleanup; + + major = setAcceptorIdentity(minor, ctx, &frreq->vps); + if (GSS_ERROR(major)) + goto cleanup; - if (ctx->acceptorName == GSS_C_NO_NAME && - cred != GSS_C_NO_CREDENTIAL && - cred->name != GSS_C_NO_NAME) { - major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName); + major = gssEapRadiusAddAvp(minor, &frreq->vps, + PW_EAP_MESSAGE, 0, inputToken); + if (GSS_ERROR(major)) + goto cleanup; + + if (ctx->acceptorCtx.state.length != 0) { + major = gssEapRadiusAddAvp(minor, &frreq->vps, PW_STATE, 0, + &ctx->acceptorCtx.state); if (GSS_ERROR(major)) goto cleanup; + + gss_release_buffer(&tmpMinor, &ctx->acceptorCtx.state); } - wpabuf_set(&respData, inputToken->value, inputToken->length); - ctx->acceptorCtx.eapPolInterface->eapRespData = &respData; - ctx->acceptorCtx.eapPolInterface->eapResp = TRUE; + if (rs_request_create(rconn, &request) != 0) { + major = gssEapRadiusMapError(minor, rs_err_conn_pop(rconn)); + goto cleanup; + } - code = eap_server_sm_step(ctx->acceptorCtx.eap); + rs_request_add_reqpkt(request, req); + req = NULL; - if (ctx->acceptorCtx.eapPolInterface->eapReq) { - ctx->acceptorCtx.eapPolInterface->eapReq = 0; - major = GSS_S_CONTINUE_NEEDED; + if (rs_request_send(request, &resp) != 0) { + major = gssEapRadiusMapError(minor, rs_err_conn_pop(rconn)); + goto cleanup; } - if (ctx->acceptorCtx.eapPolInterface->eapSuccess) { - ctx->acceptorCtx.eapPolInterface->eapSuccess = 0; - major = acceptReady(minor, ctx); - if (GSS_ERROR(major)) - goto cleanup; + assert(resp != NULL); - ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS; - major = GSS_S_CONTINUE_NEEDED; - } else if (ctx->acceptorCtx.eapPolInterface->eapFail) { - ctx->acceptorCtx.eapPolInterface->eapFail = 0; - major = GSS_S_FAILURE; - } else if (code == 0) { + frresp = rs_packet_frpkt(resp); + switch (frresp->code) { + case PW_AUTHENTICATION_ACK: + case PW_ACCESS_CHALLENGE: + break; + case PW_AUTHENTICATION_REJECT: + *minor = GSSEAP_RADIUS_AUTH_FAILURE; + major = GSS_S_DEFECTIVE_CREDENTIAL; + goto cleanup; + break; + default: + *minor = GSSEAP_UNKNOWN_RADIUS_CODE; major = GSS_S_FAILURE; + goto cleanup; + break; } - if (ctx->acceptorCtx.eapPolInterface->eapReqData != NULL) { - gss_buffer_desc buf; + major = gssEapRadiusGetAvp(minor, frresp->vps, PW_EAP_MESSAGE, 0, + outputToken, TRUE); + if (major == GSS_S_UNAVAILABLE && frresp->code == PW_ACCESS_CHALLENGE) { + *minor = GSSEAP_MISSING_EAP_REQUEST; + major = GSS_S_DEFECTIVE_TOKEN; + goto cleanup; + } else if (GSS_ERROR(major)) + goto cleanup; + + if (frresp->code == PW_ACCESS_CHALLENGE) { + major = gssEapRadiusGetAvp(minor, frresp->vps, PW_STATE, 0, + &ctx->acceptorCtx.state, TRUE); + if (GSS_ERROR(major) && *minor != GSSEAP_NO_SUCH_ATTR) + goto cleanup; + } else { + ctx->acceptorCtx.vps = frresp->vps; + frresp->vps = NULL; - buf.length = wpabuf_len(ctx->acceptorCtx.eapPolInterface->eapReqData); - buf.value = (void *)wpabuf_head(ctx->acceptorCtx.eapPolInterface->eapReqData); + rs_conn_destroy(ctx->acceptorCtx.radConn); + ctx->acceptorCtx.radConn = NULL; - tmpMajor = duplicateBuffer(&tmpMinor, &buf, outputToken); - if (GSS_ERROR(tmpMajor)) { - major = tmpMajor; - *minor = tmpMinor; + major = acceptReadyEap(minor, ctx, cred); + if (GSS_ERROR(major)) goto cleanup; - } + + GSSEAP_SM_TRANSITION_NEXT(ctx); } + major = GSS_S_CONTINUE_NEEDED; + *minor = 0; + cleanup: - ctx->acceptorCtx.eapPolInterface->eapRespData = NULL; + if (request != NULL) + rs_request_destroy(request); + if (req != NULL) + rs_packet_destroy(req); return major; } static OM_uint32 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor, - gss_ctx_id_t ctx, gss_cred_id_t cred, - gss_buffer_t inputToken, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, gss_channel_bindings_t chanBindings, - gss_buffer_t outputToken) + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) { OM_uint32 major, tmpMinor; gss_iov_buffer_desc iov[2]; - outputToken->length = 0; - outputToken->value = NULL; - - if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) { - ctx->state = EAP_STATE_ESTABLISHED; - return GSS_S_COMPLETE; - } - - if (inputToken->length < 14) { - return GSS_S_DEFECTIVE_TOKEN; - } - - iov[0].type = GSS_IOV_BUFFER_TYPE_DATA; + iov[0].type = GSS_IOV_BUFFER_TYPE_DATA | GSS_IOV_BUFFER_FLAG_ALLOCATE; iov[0].buffer.length = 0; iov[0].buffer.value = NULL; - if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS) - iov[0].buffer = chanBindings->application_data; - - iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER; - iov[1].buffer.length = 16; - iov[1].buffer.value = (unsigned char *)inputToken->value - 2; - - assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB); - - iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER; - iov[2].buffer.length = inputToken->length - 14; - iov[2].buffer.value = (unsigned char *)inputToken->value + 14; + iov[1].type = GSS_IOV_BUFFER_TYPE_STREAM; + iov[1].buffer = *inputToken; major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL, - iov, 3, TOK_TYPE_GSS_CB); - if (major == GSS_S_COMPLETE) { - ctx->state = EAP_STATE_ESTABLISHED; + iov, 2, TOK_TYPE_WRAP); + if (GSS_ERROR(major)) + return GSS_S_BAD_BINDINGS; + + if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS && + !bufferEqual(&iov[0].buffer, &chanBindings->application_data)) { + major = GSS_S_BAD_BINDINGS; + *minor = GSSEAP_BINDINGS_MISMATCH; + } else { + major = GSS_S_CONTINUE_NEEDED; + *minor = 0; } -#if 0 gss_release_buffer(&tmpMinor, &iov[0].buffer); -#endif return major; } +#ifdef GSSEAP_ENABLE_REAUTH static OM_uint32 -eapGssSmAcceptEstablished(OM_uint32 *minor, - gss_ctx_id_t ctx, +eapGssSmAcceptReauthCreds(OM_uint32 *minor, gss_cred_id_t cred, - gss_buffer_t inputToken, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, gss_channel_bindings_t chanBindings, - gss_buffer_t outputToken) + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) +{ + OM_uint32 major; + + /* + * If we're built with fast reauthentication enabled, then + * fabricate a ticket from the initiator to ourselves. + */ + major = gssEapMakeReauthCreds(minor, ctx, cred, outputToken); + if (major == GSS_S_UNAVAILABLE) + major = GSS_S_COMPLETE; + if (major == GSS_S_COMPLETE) + major = GSS_S_CONTINUE_NEEDED; + + return major; +} +#endif + +static OM_uint32 +eapGssSmAcceptCompleteInitiatorExts(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) { - /* Called with already established context */ - *minor = EINVAL; - return GSS_S_BAD_STATUS; + GSSEAP_SM_TRANSITION_NEXT(ctx); + + *minor = 0; + + return GSS_S_CONTINUE_NEEDED; } -static struct eap_gss_acceptor_sm { - enum gss_eap_token_type inputTokenType; - enum gss_eap_token_type outputTokenType; - OM_uint32 (*processToken)(OM_uint32 *, - gss_ctx_id_t, - gss_cred_id_t, - gss_buffer_t, - gss_channel_bindings_t, - gss_buffer_t); -} eapGssAcceptorSm[] = { - { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, eapGssSmAcceptAuthenticate }, -#if 0 - { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL }, - { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL }, +static OM_uint32 +eapGssSmAcceptCompleteAcceptorExts(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) +{ + GSSEAP_SM_TRANSITION(ctx, GSSEAP_STATE_ESTABLISHED); + + *minor = 0; + *smFlags |= SM_FLAG_FORCE_SEND_TOKEN; + + return GSS_S_COMPLETE; +} + +static struct gss_eap_sm eapGssAcceptorSm[] = { + { + ITOK_TYPE_ACCEPTOR_NAME_REQ, + ITOK_TYPE_ACCEPTOR_NAME_RESP, + GSSEAP_STATE_INITIAL, + 0, + eapGssSmAcceptAcceptorName + }, +#ifdef GSSEAP_DEBUG + { + ITOK_TYPE_VENDOR_INFO, + ITOK_TYPE_NONE, + GSSEAP_STATE_INITIAL, + 0, + eapGssSmAcceptVendorInfo, + }, +#endif +#ifdef GSSEAP_ENABLE_REAUTH + { + ITOK_TYPE_REAUTH_REQ, + ITOK_TYPE_REAUTH_RESP, + GSSEAP_STATE_INITIAL, + 0, + eapGssSmAcceptGssReauth, + }, #endif - { TOK_TYPE_GSS_CB, TOK_TYPE_NONE, eapGssSmAcceptGssChannelBindings }, - { TOK_TYPE_NONE, TOK_TYPE_NONE, eapGssSmAcceptEstablished }, + { + ITOK_TYPE_NONE, + ITOK_TYPE_EAP_REQ, + GSSEAP_STATE_INITIAL, + SM_ITOK_FLAG_CRITICAL | SM_ITOK_FLAG_REQUIRED, + eapGssSmAcceptIdentity, + }, + { + ITOK_TYPE_EAP_RESP, + ITOK_TYPE_EAP_REQ, + GSSEAP_STATE_AUTHENTICATE, + SM_ITOK_FLAG_CRITICAL | SM_ITOK_FLAG_REQUIRED, + eapGssSmAcceptAuthenticate + }, + { + ITOK_TYPE_GSS_CHANNEL_BINDINGS, + ITOK_TYPE_NONE, + GSSEAP_STATE_INITIATOR_EXTS, + SM_ITOK_FLAG_CRITICAL | SM_ITOK_FLAG_REQUIRED, + eapGssSmAcceptGssChannelBindings, + }, + { + ITOK_TYPE_NONE, + ITOK_TYPE_NONE, + GSSEAP_STATE_INITIATOR_EXTS, + 0, + eapGssSmAcceptCompleteInitiatorExts, + }, +#ifdef GSSEAP_ENABLE_REAUTH + { + ITOK_TYPE_NONE, + ITOK_TYPE_REAUTH_CREDS, + GSSEAP_STATE_ACCEPTOR_EXTS, + 0, + eapGssSmAcceptReauthCreds, + }, +#endif + { + ITOK_TYPE_NONE, + ITOK_TYPE_NONE, + GSSEAP_STATE_ACCEPTOR_EXTS, + 0, + eapGssSmAcceptCompleteAcceptorExts + }, }; OM_uint32 @@ -441,25 +794,19 @@ gss_accept_sec_context(OM_uint32 *minor, OM_uint32 *time_rec, gss_cred_id_t *delegated_cred_handle) { - OM_uint32 major; - OM_uint32 tmpMajor, tmpMinor; + OM_uint32 major, tmpMinor; gss_ctx_id_t ctx = *context_handle; - struct eap_gss_acceptor_sm *sm = NULL; - gss_buffer_desc innerInputToken, innerOutputToken; *minor = 0; - innerOutputToken.length = 0; - innerOutputToken.value = NULL; - output_token->length = 0; output_token->value = NULL; - if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) { - return GSS_S_NO_CRED; - } + if (src_name != NULL) + *src_name = GSS_C_NO_NAME; if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) { + *minor = GSSEAP_TOK_TRUNC; return GSS_S_DEFECTIVE_TOKEN; } @@ -473,45 +820,51 @@ gss_accept_sec_context(OM_uint32 *minor, GSSEAP_MUTEX_LOCK(&ctx->mutex); - sm = &eapGssAcceptorSm[ctx->state]; - - major = gssEapVerifyToken(minor, ctx, input_token, - sm->inputTokenType, &innerInputToken); - if (GSS_ERROR(major)) - goto cleanup; + if (cred == GSS_C_NO_CREDENTIAL) { + if (ctx->defaultCred == GSS_C_NO_CREDENTIAL) { + major = gssEapAcquireCred(minor, + GSS_C_NO_NAME, + GSS_C_NO_BUFFER, + GSS_C_INDEFINITE, + GSS_C_NO_OID_SET, + GSS_C_ACCEPT, + &ctx->defaultCred, + NULL, + NULL); + if (GSS_ERROR(major)) + goto cleanup; + } - /* If credentials were provided, check they're usable with this mech */ - if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) { - major = GSS_S_BAD_MECH; - goto cleanup; + cred = ctx->defaultCred; } - do { - sm = &eapGssAcceptorSm[ctx->state]; + GSSEAP_MUTEX_LOCK(&cred->mutex); - major = (sm->processToken)(minor, - ctx, - cred, - &innerInputToken, - input_chan_bindings, - &innerOutputToken); + if (cred->name != GSS_C_NO_NAME) { + major = gssEapDuplicateName(minor, cred->name, &ctx->acceptorName); if (GSS_ERROR(major)) goto cleanup; - } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0); + } + + major = gssEapSmStep(minor, + cred, + ctx, + GSS_C_NO_NAME, + GSS_C_NO_OID, + 0, + GSS_C_INDEFINITE, + input_chan_bindings, + input_token, + output_token, + eapGssAcceptorSm, + sizeof(eapGssAcceptorSm) / sizeof(eapGssAcceptorSm[0])); + if (GSS_ERROR(major)) + goto cleanup; if (mech_type != NULL) { if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type)) duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type); } - if (innerOutputToken.length != 0) { - tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken, - sm->outputTokenType, output_token); - if (GSS_ERROR(tmpMajor)) { - major = tmpMajor; - *minor = tmpMinor; - goto cleanup; - } - } if (ret_flags != NULL) *ret_flags = ctx->gssFlags; if (delegated_cred_handle != NULL) @@ -519,23 +872,102 @@ gss_accept_sec_context(OM_uint32 *minor, if (major == GSS_S_COMPLETE) { if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) { - major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name); + major = gssEapDuplicateName(&tmpMinor, ctx->initiatorName, src_name); + if (GSS_ERROR(major)) + goto cleanup; + } + if (time_rec != NULL) { + major = gssEapContextTime(&tmpMinor, ctx, time_rec); if (GSS_ERROR(major)) goto cleanup; } - if (time_rec != NULL) - gss_context_time(&tmpMinor, ctx, time_rec); } - assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED); + assert(CTX_IS_ESTABLISHED(ctx) || major == GSS_S_CONTINUE_NEEDED); cleanup: + if (cred != GSS_C_NO_CREDENTIAL) + GSSEAP_MUTEX_UNLOCK(&cred->mutex); GSSEAP_MUTEX_UNLOCK(&ctx->mutex); if (GSS_ERROR(major)) gssEapReleaseContext(&tmpMinor, context_handle); - gss_release_buffer(&tmpMinor, &innerOutputToken); + return major; +} + +#ifdef GSSEAP_ENABLE_REAUTH +static OM_uint32 +acceptReadyKrb(OM_uint32 *minor, + gss_ctx_id_t ctx, + gss_cred_id_t cred, + const gss_name_t initiator, + const gss_OID mech, + OM_uint32 timeRec) +{ + OM_uint32 major; + + major = gssEapGlueToMechName(minor, ctx, initiator, &ctx->initiatorName); + if (GSS_ERROR(major)) + return major; + + major = gssEapReauthComplete(minor, ctx, cred, mech, timeRec); + if (GSS_ERROR(major)) + return major; + + *minor = 0; + return GSS_S_COMPLETE; +} + +static OM_uint32 +eapGssSmAcceptGssReauth(OM_uint32 *minor, + gss_cred_id_t cred, + gss_ctx_id_t ctx, + gss_name_t target, + gss_OID mech, + OM_uint32 reqFlags, + OM_uint32 timeReq, + gss_channel_bindings_t chanBindings, + gss_buffer_t inputToken, + gss_buffer_t outputToken, + OM_uint32 *smFlags) +{ + OM_uint32 major, tmpMinor; + gss_name_t krbInitiator = GSS_C_NO_NAME; + OM_uint32 gssFlags, timeRec = GSS_C_INDEFINITE; + + /* + * If we're built with fast reauthentication support, it's valid + * for an initiator to send a GSS reauthentication token as its + * initial context token, causing us to short-circuit the state + * machine and process Kerberos GSS messages instead. + */ + + ctx->flags |= CTX_FLAG_KRB_REAUTH; + + major = gssAcceptSecContext(minor, + &ctx->kerberosCtx, + cred->krbCred, + inputToken, + chanBindings, + &krbInitiator, + &mech, + outputToken, + &gssFlags, + &timeRec, + NULL); + if (major == GSS_S_COMPLETE) { + major = acceptReadyKrb(minor, ctx, cred, + krbInitiator, mech, timeRec); + if (major == GSS_S_COMPLETE) { + GSSEAP_SM_TRANSITION(ctx, GSSEAP_STATE_ESTABLISHED); + } + } + + ctx->gssFlags = gssFlags; + + gssReleaseName(&tmpMinor, &krbInitiator); return major; } +#endif /* GSSEAP_ENABLE_REAUTH */