Path: blob/main/crypto/krb5/src/kdc/kdc_preauth_ec.c
34879 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* kdc/kdc_preauth_ec.c - Encrypted challenge kdcpreauth module */2/*3* Copyright (C) 2009 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/*27* Implement Encrypted Challenge fast factor from28* draft-ietf-krb-wg-preauth-framework29*/3031#include <k5-int.h>32#include <krb5/kdcpreauth_plugin.h>33#include "kdc_util.h"3435static void36ec_edata(krb5_context context, krb5_kdc_req *request,37krb5_kdcpreauth_callbacks cb, krb5_kdcpreauth_rock rock,38krb5_kdcpreauth_moddata moddata, krb5_preauthtype pa_type,39krb5_kdcpreauth_edata_respond_fn respond, void *arg)40{41krb5_keyblock *armor_key = cb->fast_armor(context, rock);4243/* Encrypted challenge only works with FAST, and requires a client key. */44if (armor_key == NULL || !cb->have_client_keys(context, rock))45(*respond)(arg, ENOENT, NULL);46else47(*respond)(arg, 0, NULL);48}4950static void51ec_verify(krb5_context context, krb5_data *req_pkt, krb5_kdc_req *request,52krb5_enc_tkt_part *enc_tkt_reply, krb5_pa_data *data,53krb5_kdcpreauth_callbacks cb, krb5_kdcpreauth_rock rock,54krb5_kdcpreauth_moddata moddata,55krb5_kdcpreauth_verify_respond_fn respond, void *arg)56{57krb5_error_code ret;58krb5_enc_data *enc = NULL;59krb5_data der_enc_ts = empty_data(), der_enc_data;60krb5_keyblock *armor_key = cb->fast_armor(context, rock);61krb5_pa_enc_ts *ts = NULL;62krb5_keyblock *client_keys = NULL;63krb5_keyblock *challenge_key = NULL;64krb5_keyblock *kdc_challenge_key;65krb5_kdcpreauth_modreq modreq = NULL;66int i = 0;67char *ai = NULL, *realmstr = NULL;68krb5_data realm = request->server->realm;6970if (armor_key == NULL) {71ret = ENOENT;72k5_setmsg(context, ret,73_("Encrypted Challenge used outside of FAST tunnel"));74goto cleanup;75}7677der_enc_data = make_data(data->contents, data->length);78ret = decode_krb5_enc_data(&der_enc_data, &enc);79if (ret)80goto cleanup;8182ret = alloc_data(&der_enc_ts, enc->ciphertext.length);83if (ret)84goto cleanup;8586/* Check for a configured auth indicator. */87realmstr = k5memdup0(realm.data, realm.length, &ret);88if (realmstr == NULL)89goto cleanup;90ret = profile_get_string(context->profile, KRB5_CONF_REALMS, realmstr,91KRB5_CONF_ENCRYPTED_CHALLENGE_INDICATOR, NULL,92&ai);93if (ret)94goto cleanup;9596ret = cb->client_keys(context, rock, &client_keys);97if (ret)98goto cleanup;99for (i = 0; client_keys[i].enctype != ENCTYPE_NULL; i++) {100ret = krb5_c_fx_cf2_simple(context, armor_key, "clientchallengearmor",101&client_keys[i], "challengelongterm",102&challenge_key);103if (ret)104goto cleanup;105ret = krb5_c_decrypt(context, challenge_key,106KRB5_KEYUSAGE_ENC_CHALLENGE_CLIENT, NULL, enc,107&der_enc_ts);108krb5_free_keyblock(context, challenge_key);109if (!ret)110break;111}112113if (client_keys[i].enctype == ENCTYPE_NULL) {114ret = KRB5KDC_ERR_PREAUTH_FAILED;115k5_setmsg(context, ret,116_("Incorrect password in encrypted challenge"));117goto cleanup;118}119120ret = decode_krb5_pa_enc_ts(&der_enc_ts, &ts);121if (ret)122goto cleanup;123ret = krb5_check_clockskew(context, ts->patimestamp);124if (ret)125goto cleanup;126127enc_tkt_reply->flags |= TKT_FLG_PRE_AUTH;128129/*130* If this fails, we won't generate a reply to the client. That may cause131* the client to fail, but at this point the KDC has considered this a132* success, so the return value is ignored.133*/134if (krb5_c_fx_cf2_simple(context, armor_key, "kdcchallengearmor",135&client_keys[i], "challengelongterm",136&kdc_challenge_key) == 0) {137modreq = (krb5_kdcpreauth_modreq)kdc_challenge_key;138if (ai != NULL)139cb->add_auth_indicator(context, rock, ai);140}141142cleanup:143cb->free_keys(context, rock, client_keys);144free(der_enc_ts.data);145krb5_free_enc_data(context, enc);146krb5_free_pa_enc_ts(context, ts);147free(realmstr);148free(ai);149150(*respond)(arg, ret, modreq, NULL, NULL);151}152153static krb5_error_code154ec_return(krb5_context context, krb5_pa_data *padata, krb5_data *req_pkt,155krb5_kdc_req *request, krb5_kdc_rep *reply,156krb5_keyblock *encrypting_key, krb5_pa_data **send_pa,157krb5_kdcpreauth_callbacks cb, krb5_kdcpreauth_rock rock,158krb5_kdcpreauth_moddata moddata, krb5_kdcpreauth_modreq modreq)159{160krb5_error_code ret;161krb5_keyblock *challenge_key = (krb5_keyblock *)modreq;162krb5_pa_enc_ts ts;163krb5_data *der_enc_ts = NULL, *der_enc_data = NULL;164krb5_enc_data enc;165krb5_pa_data *pa = NULL;166167enc.ciphertext.data = NULL;168169if (challenge_key == NULL)170return 0;171172ret = krb5_us_timeofday(context, &ts.patimestamp, &ts.pausec);173if (ret)174goto cleanup;175ret = encode_krb5_pa_enc_ts(&ts, &der_enc_ts);176if (ret)177goto cleanup;178ret = krb5_encrypt_helper(context, challenge_key,179KRB5_KEYUSAGE_ENC_CHALLENGE_KDC, der_enc_ts,180&enc);181if (ret)182goto cleanup;183ret = encode_krb5_enc_data(&enc, &der_enc_data);184if (ret)185goto cleanup;186187pa = k5alloc(sizeof(*pa), &ret);188if (pa == NULL)189goto cleanup;190pa->pa_type = KRB5_PADATA_ENCRYPTED_CHALLENGE;191pa->length = der_enc_data->length;192/* Steal the data pointer from der_enc_data. */193pa->contents = (unsigned char *)der_enc_data->data;194der_enc_data->data = NULL;195196*send_pa = pa;197198cleanup:199krb5_free_keyblock(context, challenge_key);200krb5_free_data(context, der_enc_data);201krb5_free_data(context, der_enc_ts);202krb5_free_data_contents(context, &enc.ciphertext);203return ret;204}205206static krb5_preauthtype ec_types[] = {207KRB5_PADATA_ENCRYPTED_CHALLENGE, 0};208209krb5_error_code210kdcpreauth_encrypted_challenge_initvt(krb5_context context, int maj_ver,211int min_ver, krb5_plugin_vtable vtable)212{213krb5_kdcpreauth_vtable vt;214215if (maj_ver != 1)216return KRB5_PLUGIN_VER_NOTSUPP;217vt = (krb5_kdcpreauth_vtable)vtable;218vt->name = "encrypted_challenge";219vt->pa_type_list = ec_types;220vt->edata = ec_edata;221vt->verify = ec_verify;222vt->return_padata = ec_return;223return 0;224}225226227