Path: blob/main/crypto/heimdal/lib/krb5/build_auth.c
34878 views
/*1* Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "krb5_locl.h"3435static krb5_error_code36make_etypelist(krb5_context context,37krb5_authdata **auth_data)38{39EtypeList etypes;40krb5_error_code ret;41krb5_authdata ad;42u_char *buf;43size_t len = 0;44size_t buf_size;4546ret = _krb5_init_etype(context, KRB5_PDU_NONE,47&etypes.len, &etypes.val,48NULL);49if (ret)50return ret;5152ASN1_MALLOC_ENCODE(EtypeList, buf, buf_size, &etypes, &len, ret);53if (ret) {54free_EtypeList(&etypes);55return ret;56}57if(buf_size != len)58krb5_abortx(context, "internal error in ASN.1 encoder");59free_EtypeList(&etypes);6061ALLOC_SEQ(&ad, 1);62if (ad.val == NULL) {63free(buf);64krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));65return ENOMEM;66}6768ad.val[0].ad_type = KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION;69ad.val[0].ad_data.length = len;70ad.val[0].ad_data.data = buf;7172ASN1_MALLOC_ENCODE(AD_IF_RELEVANT, buf, buf_size, &ad, &len, ret);73if (ret) {74free_AuthorizationData(&ad);75return ret;76}77if(buf_size != len)78krb5_abortx(context, "internal error in ASN.1 encoder");79free_AuthorizationData(&ad);8081ALLOC(*auth_data, 1);82if (*auth_data == NULL) {83free(buf);84krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));85return ENOMEM;86}8788ALLOC_SEQ(*auth_data, 1);89if ((*auth_data)->val == NULL) {90free(*auth_data);91free(buf);92krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));93return ENOMEM;94}9596(*auth_data)->val[0].ad_type = KRB5_AUTHDATA_IF_RELEVANT;97(*auth_data)->val[0].ad_data.length = len;98(*auth_data)->val[0].ad_data.data = buf;99100return 0;101}102103KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL104_krb5_build_authenticator (krb5_context context,105krb5_auth_context auth_context,106krb5_enctype enctype,107krb5_creds *cred,108Checksum *cksum,109krb5_data *result,110krb5_key_usage usage)111{112Authenticator auth;113u_char *buf = NULL;114size_t buf_size;115size_t len = 0;116krb5_error_code ret;117krb5_crypto crypto;118119memset(&auth, 0, sizeof(auth));120121auth.authenticator_vno = 5;122copy_Realm(&cred->client->realm, &auth.crealm);123copy_PrincipalName(&cred->client->name, &auth.cname);124125krb5_us_timeofday (context, &auth.ctime, &auth.cusec);126127ret = krb5_auth_con_getlocalsubkey(context, auth_context, &auth.subkey);128if(ret)129goto fail;130131if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {132if(auth_context->local_seqnumber == 0)133krb5_generate_seq_number (context,134&cred->session,135&auth_context->local_seqnumber);136ALLOC(auth.seq_number, 1);137if(auth.seq_number == NULL) {138ret = ENOMEM;139goto fail;140}141*auth.seq_number = auth_context->local_seqnumber;142} else143auth.seq_number = NULL;144auth.authorization_data = NULL;145146if (cksum) {147ALLOC(auth.cksum, 1);148if (auth.cksum == NULL) {149ret = ENOMEM;150goto fail;151}152ret = copy_Checksum(cksum, auth.cksum);153if (ret)154goto fail;155156if (auth.cksum->cksumtype == CKSUMTYPE_GSSAPI) {157/*158* This is not GSS-API specific, we only enable it for159* GSS for now160*/161ret = make_etypelist(context, &auth.authorization_data);162if (ret)163goto fail;164}165}166167/* XXX - Copy more to auth_context? */168169auth_context->authenticator->ctime = auth.ctime;170auth_context->authenticator->cusec = auth.cusec;171172ASN1_MALLOC_ENCODE(Authenticator, buf, buf_size, &auth, &len, ret);173if (ret)174goto fail;175if(buf_size != len)176krb5_abortx(context, "internal error in ASN.1 encoder");177178ret = krb5_crypto_init(context, &cred->session, enctype, &crypto);179if (ret)180goto fail;181ret = krb5_encrypt (context,182crypto,183usage /* KRB5_KU_AP_REQ_AUTH */,184buf,185len,186result);187krb5_crypto_destroy(context, crypto);188189if (ret)190goto fail;191192fail:193free_Authenticator (&auth);194free (buf);195196return ret;197}198199200