Path: blob/main/crypto/heimdal/lib/krb5/auth_context.c
34878 views
/*1* Copyright (c) 1997 - 2002 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"3435KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL36krb5_auth_con_init(krb5_context context,37krb5_auth_context *auth_context)38{39krb5_auth_context p;4041ALLOC(p, 1);42if(!p) {43krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));44return ENOMEM;45}46memset(p, 0, sizeof(*p));47ALLOC(p->authenticator, 1);48if (!p->authenticator) {49krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));50free(p);51return ENOMEM;52}53memset (p->authenticator, 0, sizeof(*p->authenticator));54p->flags = KRB5_AUTH_CONTEXT_DO_TIME;5556p->local_address = NULL;57p->remote_address = NULL;58p->local_port = 0;59p->remote_port = 0;60p->keytype = ENCTYPE_NULL;61p->cksumtype = CKSUMTYPE_NONE;62*auth_context = p;63return 0;64}6566KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL67krb5_auth_con_free(krb5_context context,68krb5_auth_context auth_context)69{70if (auth_context != NULL) {71krb5_free_authenticator(context, &auth_context->authenticator);72if(auth_context->local_address){73free_HostAddress(auth_context->local_address);74free(auth_context->local_address);75}76if(auth_context->remote_address){77free_HostAddress(auth_context->remote_address);78free(auth_context->remote_address);79}80krb5_free_keyblock(context, auth_context->keyblock);81krb5_free_keyblock(context, auth_context->remote_subkey);82krb5_free_keyblock(context, auth_context->local_subkey);83free (auth_context);84}85return 0;86}8788KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL89krb5_auth_con_setflags(krb5_context context,90krb5_auth_context auth_context,91int32_t flags)92{93auth_context->flags = flags;94return 0;95}969798KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL99krb5_auth_con_getflags(krb5_context context,100krb5_auth_context auth_context,101int32_t *flags)102{103*flags = auth_context->flags;104return 0;105}106107KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL108krb5_auth_con_addflags(krb5_context context,109krb5_auth_context auth_context,110int32_t addflags,111int32_t *flags)112{113if (flags)114*flags = auth_context->flags;115auth_context->flags |= addflags;116return 0;117}118119KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL120krb5_auth_con_removeflags(krb5_context context,121krb5_auth_context auth_context,122int32_t removeflags,123int32_t *flags)124{125if (flags)126*flags = auth_context->flags;127auth_context->flags &= ~removeflags;128return 0;129}130131KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL132krb5_auth_con_setaddrs(krb5_context context,133krb5_auth_context auth_context,134krb5_address *local_addr,135krb5_address *remote_addr)136{137if (local_addr) {138if (auth_context->local_address)139krb5_free_address (context, auth_context->local_address);140else141if ((auth_context->local_address = malloc(sizeof(krb5_address))) == NULL)142return ENOMEM;143krb5_copy_address(context, local_addr, auth_context->local_address);144}145if (remote_addr) {146if (auth_context->remote_address)147krb5_free_address (context, auth_context->remote_address);148else149if ((auth_context->remote_address = malloc(sizeof(krb5_address))) == NULL)150return ENOMEM;151krb5_copy_address(context, remote_addr, auth_context->remote_address);152}153return 0;154}155156KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL157krb5_auth_con_genaddrs(krb5_context context,158krb5_auth_context auth_context,159krb5_socket_t fd, int flags)160{161krb5_error_code ret;162krb5_address local_k_address, remote_k_address;163krb5_address *lptr = NULL, *rptr = NULL;164struct sockaddr_storage ss_local, ss_remote;165struct sockaddr *local = (struct sockaddr *)&ss_local;166struct sockaddr *remote = (struct sockaddr *)&ss_remote;167socklen_t len;168169if(flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_ADDR) {170if (auth_context->local_address == NULL) {171len = sizeof(ss_local);172if(rk_IS_SOCKET_ERROR(getsockname(fd, local, &len))) {173char buf[128];174ret = rk_SOCK_ERRNO;175rk_strerror_r(ret, buf, sizeof(buf));176krb5_set_error_message(context, ret, "getsockname: %s", buf);177goto out;178}179ret = krb5_sockaddr2address (context, local, &local_k_address);180if(ret) goto out;181if(flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR) {182krb5_sockaddr2port (context, local, &auth_context->local_port);183} else184auth_context->local_port = 0;185lptr = &local_k_address;186}187}188if(flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_ADDR) {189len = sizeof(ss_remote);190if(rk_IS_SOCKET_ERROR(getpeername(fd, remote, &len))) {191char buf[128];192ret = rk_SOCK_ERRNO;193rk_strerror_r(ret, buf, sizeof(buf));194krb5_set_error_message(context, ret, "getpeername: %s", buf);195goto out;196}197ret = krb5_sockaddr2address (context, remote, &remote_k_address);198if(ret) goto out;199if(flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR) {200krb5_sockaddr2port (context, remote, &auth_context->remote_port);201} else202auth_context->remote_port = 0;203rptr = &remote_k_address;204}205ret = krb5_auth_con_setaddrs (context,206auth_context,207lptr,208rptr);209out:210if (lptr)211krb5_free_address (context, lptr);212if (rptr)213krb5_free_address (context, rptr);214return ret;215216}217218KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL219krb5_auth_con_setaddrs_from_fd (krb5_context context,220krb5_auth_context auth_context,221void *p_fd)222{223krb5_socket_t fd = *(krb5_socket_t *)p_fd;224int flags = 0;225if(auth_context->local_address == NULL)226flags |= KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR;227if(auth_context->remote_address == NULL)228flags |= KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR;229return krb5_auth_con_genaddrs(context, auth_context, fd, flags);230}231232KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL233krb5_auth_con_getaddrs(krb5_context context,234krb5_auth_context auth_context,235krb5_address **local_addr,236krb5_address **remote_addr)237{238if(*local_addr)239krb5_free_address (context, *local_addr);240*local_addr = malloc (sizeof(**local_addr));241if (*local_addr == NULL) {242krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));243return ENOMEM;244}245krb5_copy_address(context,246auth_context->local_address,247*local_addr);248249if(*remote_addr)250krb5_free_address (context, *remote_addr);251*remote_addr = malloc (sizeof(**remote_addr));252if (*remote_addr == NULL) {253krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));254krb5_free_address (context, *local_addr);255*local_addr = NULL;256return ENOMEM;257}258krb5_copy_address(context,259auth_context->remote_address,260*remote_addr);261return 0;262}263264/* coverity[+alloc : arg-*2] */265static krb5_error_code266copy_key(krb5_context context,267krb5_keyblock *in,268krb5_keyblock **out)269{270if(in)271return krb5_copy_keyblock(context, in, out);272*out = NULL; /* is this right? */273return 0;274}275276KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL277krb5_auth_con_getkey(krb5_context context,278krb5_auth_context auth_context,279krb5_keyblock **keyblock)280{281return copy_key(context, auth_context->keyblock, keyblock);282}283284KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL285krb5_auth_con_getlocalsubkey(krb5_context context,286krb5_auth_context auth_context,287krb5_keyblock **keyblock)288{289return copy_key(context, auth_context->local_subkey, keyblock);290}291292/* coverity[+alloc : arg-*2] */293KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL294krb5_auth_con_getremotesubkey(krb5_context context,295krb5_auth_context auth_context,296krb5_keyblock **keyblock)297{298return copy_key(context, auth_context->remote_subkey, keyblock);299}300301KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL302krb5_auth_con_setkey(krb5_context context,303krb5_auth_context auth_context,304krb5_keyblock *keyblock)305{306if(auth_context->keyblock)307krb5_free_keyblock(context, auth_context->keyblock);308return copy_key(context, keyblock, &auth_context->keyblock);309}310311KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL312krb5_auth_con_setlocalsubkey(krb5_context context,313krb5_auth_context auth_context,314krb5_keyblock *keyblock)315{316if(auth_context->local_subkey)317krb5_free_keyblock(context, auth_context->local_subkey);318return copy_key(context, keyblock, &auth_context->local_subkey);319}320321KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL322krb5_auth_con_generatelocalsubkey(krb5_context context,323krb5_auth_context auth_context,324krb5_keyblock *key)325{326krb5_error_code ret;327krb5_keyblock *subkey;328329ret = krb5_generate_subkey_extended (context, key,330auth_context->keytype,331&subkey);332if(ret)333return ret;334if(auth_context->local_subkey)335krb5_free_keyblock(context, auth_context->local_subkey);336auth_context->local_subkey = subkey;337return 0;338}339340341KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL342krb5_auth_con_setremotesubkey(krb5_context context,343krb5_auth_context auth_context,344krb5_keyblock *keyblock)345{346if(auth_context->remote_subkey)347krb5_free_keyblock(context, auth_context->remote_subkey);348return copy_key(context, keyblock, &auth_context->remote_subkey);349}350351KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL352krb5_auth_con_setcksumtype(krb5_context context,353krb5_auth_context auth_context,354krb5_cksumtype cksumtype)355{356auth_context->cksumtype = cksumtype;357return 0;358}359360KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL361krb5_auth_con_getcksumtype(krb5_context context,362krb5_auth_context auth_context,363krb5_cksumtype *cksumtype)364{365*cksumtype = auth_context->cksumtype;366return 0;367}368369KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL370krb5_auth_con_setkeytype (krb5_context context,371krb5_auth_context auth_context,372krb5_keytype keytype)373{374auth_context->keytype = keytype;375return 0;376}377378KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL379krb5_auth_con_getkeytype (krb5_context context,380krb5_auth_context auth_context,381krb5_keytype *keytype)382{383*keytype = auth_context->keytype;384return 0;385}386387#if 0388KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL389krb5_auth_con_setenctype(krb5_context context,390krb5_auth_context auth_context,391krb5_enctype etype)392{393if(auth_context->keyblock)394krb5_free_keyblock(context, auth_context->keyblock);395ALLOC(auth_context->keyblock, 1);396if(auth_context->keyblock == NULL)397return ENOMEM;398auth_context->keyblock->keytype = etype;399return 0;400}401402KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL403krb5_auth_con_getenctype(krb5_context context,404krb5_auth_context auth_context,405krb5_enctype *etype)406{407krb5_abortx(context, "unimplemented krb5_auth_getenctype called");408}409#endif410411KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL412krb5_auth_con_getlocalseqnumber(krb5_context context,413krb5_auth_context auth_context,414int32_t *seqnumber)415{416*seqnumber = auth_context->local_seqnumber;417return 0;418}419420KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL421krb5_auth_con_setlocalseqnumber (krb5_context context,422krb5_auth_context auth_context,423int32_t seqnumber)424{425auth_context->local_seqnumber = seqnumber;426return 0;427}428429KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL430krb5_auth_con_getremoteseqnumber(krb5_context context,431krb5_auth_context auth_context,432int32_t *seqnumber)433{434*seqnumber = auth_context->remote_seqnumber;435return 0;436}437438KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL439krb5_auth_con_setremoteseqnumber (krb5_context context,440krb5_auth_context auth_context,441int32_t seqnumber)442{443auth_context->remote_seqnumber = seqnumber;444return 0;445}446447448KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL449krb5_auth_con_getauthenticator(krb5_context context,450krb5_auth_context auth_context,451krb5_authenticator *authenticator)452{453*authenticator = malloc(sizeof(**authenticator));454if (*authenticator == NULL) {455krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));456return ENOMEM;457}458459copy_Authenticator(auth_context->authenticator,460*authenticator);461return 0;462}463464465KRB5_LIB_FUNCTION void KRB5_LIB_CALL466krb5_free_authenticator(krb5_context context,467krb5_authenticator *authenticator)468{469free_Authenticator (*authenticator);470free (*authenticator);471*authenticator = NULL;472}473474475KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL476krb5_auth_con_setuserkey(krb5_context context,477krb5_auth_context auth_context,478krb5_keyblock *keyblock)479{480if(auth_context->keyblock)481krb5_free_keyblock(context, auth_context->keyblock);482return krb5_copy_keyblock(context, keyblock, &auth_context->keyblock);483}484485KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL486krb5_auth_con_getrcache(krb5_context context,487krb5_auth_context auth_context,488krb5_rcache *rcache)489{490*rcache = auth_context->rcache;491return 0;492}493494KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL495krb5_auth_con_setrcache(krb5_context context,496krb5_auth_context auth_context,497krb5_rcache rcache)498{499auth_context->rcache = rcache;500return 0;501}502503#if 0 /* not implemented */504505KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL506krb5_auth_con_initivector(krb5_context context,507krb5_auth_context auth_context)508{509krb5_abortx(context, "unimplemented krb5_auth_con_initivector called");510}511512513KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL514krb5_auth_con_setivector(krb5_context context,515krb5_auth_context auth_context,516krb5_pointer ivector)517{518krb5_abortx(context, "unimplemented krb5_auth_con_setivector called");519}520521#endif /* not implemented */522523524