From: Greg Hudson Date: Fri, 1 Jan 2010 23:20:56 +0000 (+0000) Subject: Factor out copying and counting of zero-terminated enctype lists into X-Git-Tag: krb5-1.8-alpha1~25 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8882447e3c33ecc132b03fe83136d4e5fbe03131;p=krb5.git Factor out copying and counting of zero-terminated enctype lists into a new file src/lib/krb5/krb/etype_list.c. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@23565 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/ccache/cc_retr.c b/src/lib/krb5/ccache/cc_retr.c index 1c4b575ba..c2e305f90 100644 --- a/src/lib/krb5/ccache/cc_retr.c +++ b/src/lib/krb5/ccache/cc_retr.c @@ -29,6 +29,7 @@ #include "k5-int.h" #include "cc-int.h" +#include "../krb/int-proto.h" #define KRB5_OK 0 @@ -279,9 +280,7 @@ krb5_cc_retrieve_cred_default (krb5_context context, krb5_ccache id, krb5_flags ret = krb5_get_tgs_ktypes (context, mcreds->server, &ktypes); if (ret) return ret; - nktypes = 0; - while (ktypes[nktypes]) - nktypes++; + nktypes = krb5int_count_etypes (ktypes); ret = krb5_cc_retrieve_cred_seq (context, id, flags, mcreds, creds, nktypes, ktypes); diff --git a/src/lib/krb5/krb/Makefile.in b/src/lib/krb5/krb/Makefile.in index f8a92b1f9..43d754cdc 100644 --- a/src/lib/krb5/krb/Makefile.in +++ b/src/lib/krb5/krb/Makefile.in @@ -40,6 +40,7 @@ STLIBOBJS= \ enc_keyhelper.o \ encode_kdc.o \ encrypt_tk.o \ + etype_list.o \ fast.o \ free_rtree.o \ fwd_tgt.o \ @@ -134,6 +135,7 @@ OBJS= $(OUTPRE)addr_comp.$(OBJEXT) \ $(OUTPRE)enc_keyhelper.$(OBJEXT) \ $(OUTPRE)encode_kdc.$(OBJEXT) \ $(OUTPRE)encrypt_tk.$(OBJEXT) \ + $(OUTPRE)etype_list.$(OBJEXT) \ $(OUTPRE)fast.$(OBJEXT) \ $(OUTPRE)free_rtree.$(OBJEXT) \ $(OUTPRE)fwd_tgt.$(OBJEXT) \ @@ -229,6 +231,7 @@ SRCS= $(srcdir)/addr_comp.c \ $(srcdir)/enc_keyhelper.c \ $(srcdir)/encode_kdc.c \ $(srcdir)/encrypt_tk.c \ + $(srcdir)/etype_list.c \ $(srcdir)/fast.c \ $(srcdir)/free_rtree.c \ $(srcdir)/fwd_tgt.c \ @@ -325,7 +328,8 @@ T_WALK_RTREE_OBJS= t_walk_rtree.o walk_rtree.o tgtname.o unparse.o \ T_KERB_OBJS= t_kerb.o conv_princ.o unparse.o set_realm.o str_conv.o T_SER_OBJS= t_ser.o ser_actx.o ser_adata.o ser_addr.o ser_auth.o ser_cksum.o \ - ser_ctx.o ser_key.o ser_princ.o serialize.o authdata.o pac.o copy_data.o + ser_ctx.o ser_key.o ser_princ.o serialize.o authdata.o pac.o \ + copy_data.o etype_list.o T_DELTAT_OBJS= t_deltat.o deltat.o @@ -333,7 +337,7 @@ T_PAC_OBJS= t_pac.o pac.o copy_data.o T_PRINC_OBJS= t_princ.o parse.o unparse.o -T_ETYPES_OBJS= t_etypes.o init_ctx.o +T_ETYPES_OBJS= t_etypes.o init_ctx.o etype_list.o t_walk_rtree: $(T_WALK_RTREE_OBJS) $(KRB5_BASE_DEPLIBS) $(CC_LINK) -o t_walk_rtree $(T_WALK_RTREE_OBJS) $(KRB5_BASE_LIBS) diff --git a/src/lib/krb5/krb/auth_con.c b/src/lib/krb5/krb/auth_con.c index a53b7d5b0..1cb5c2bab 100644 --- a/src/lib/krb5/krb/auth_con.c +++ b/src/lib/krb5/krb/auth_con.c @@ -1,5 +1,6 @@ /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ #include "k5-int.h" +#include "int-proto.h" #include "auth_con.h" static krb5_boolean chk_heimdal_seqnum(krb5_ui_4, krb5_ui_4); @@ -355,53 +356,31 @@ krb5_auth_con_getrcache(krb5_context context, krb5_auth_context auth_context, kr } krb5_error_code -krb5_auth_con_setpermetypes(krb5_context context, krb5_auth_context auth_context, const krb5_enctype *permetypes) +krb5_auth_con_setpermetypes(krb5_context context, + krb5_auth_context auth_context, + const krb5_enctype *permetypes) { - krb5_enctype * newpe; - int i; - - for (i=0; permetypes[i]; i++) - ; - i++; /* include the zero */ - - if ((newpe = (krb5_enctype *) malloc(i*sizeof(krb5_enctype))) - == NULL) - return(ENOMEM); + krb5_enctype *newpe; + krb5_error_code ret; - if (auth_context->permitted_etypes) - free(auth_context->permitted_etypes); + ret = krb5int_copy_etypes(permetypes, &newpe); + if (ret != 0) + return ret; + free(auth_context->permitted_etypes); auth_context->permitted_etypes = newpe; - - memcpy(newpe, permetypes, i*sizeof(krb5_enctype)); - return 0; } krb5_error_code -krb5_auth_con_getpermetypes(krb5_context context, krb5_auth_context auth_context, krb5_enctype **permetypes) +krb5_auth_con_getpermetypes(krb5_context context, + krb5_auth_context auth_context, + krb5_enctype **permetypes) { - krb5_enctype * newpe; - int i; - - if (! auth_context->permitted_etypes) { - *permetypes = NULL; - return(0); - } - - for (i=0; auth_context->permitted_etypes[i]; i++) - ; - i++; /* include the zero */ - - if ((newpe = (krb5_enctype *) malloc(i*sizeof(krb5_enctype))) - == NULL) - return(ENOMEM); - - *permetypes = newpe; - - memcpy(newpe, auth_context->permitted_etypes, i*sizeof(krb5_enctype)); - - return(0); + *permetypes = NULL; + if (auth_context->permitted_etypes == NULL) + return 0; + return krb5int_copy_etypes(auth_context->permitted_etypes, permetypes); } krb5_error_code KRB5_CALLCONV diff --git a/src/lib/krb5/krb/copy_ctx.c b/src/lib/krb5/krb/copy_ctx.c index a95891e4d..d916c26b2 100644 --- a/src/lib/krb5/krb/copy_ctx.c +++ b/src/lib/krb5/krb/copy_ctx.c @@ -54,26 +54,9 @@ */ #include "k5-int.h" +#include "int-proto.h" #include -/* Copy the zero-terminated enctype list old_list into *new_list. */ -static krb5_error_code -copy_enctypes(krb5_context context, const krb5_enctype *old_list, - krb5_enctype **new_list) -{ - unsigned int count; - krb5_enctype *list; - - *new_list = NULL; - for (count = 0; old_list[count]; count++); - list = malloc(sizeof(krb5_enctype) * (count + 1)); - if (list == NULL) - return ENOMEM; - memcpy(list, old_list, sizeof(krb5_enctype) * (count + 1)); - *new_list = list; - return 0; -} - krb5_error_code KRB5_CALLCONV krb5_copy_context(krb5_context ctx, krb5_context *nctx_out) { @@ -109,10 +92,10 @@ krb5_copy_context(krb5_context ctx, krb5_context *nctx_out) memset(&nctx->err, 0, sizeof(nctx->err)); - ret = copy_enctypes(nctx, ctx->in_tkt_etypes, &nctx->in_tkt_etypes); + ret = krb5int_copy_etypes(ctx->in_tkt_etypes, &nctx->in_tkt_etypes); if (ret) goto errout; - ret = copy_enctypes(nctx, ctx->tgs_etypes, &nctx->tgs_etypes); + ret = krb5int_copy_etypes(ctx->tgs_etypes, &nctx->tgs_etypes); if (ret) goto errout; diff --git a/src/lib/krb5/krb/etype_list.c b/src/lib/krb5/krb/etype_list.c new file mode 100644 index 000000000..98ebc8033 --- /dev/null +++ b/src/lib/krb5/krb/etype_list.c @@ -0,0 +1,59 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/* + * lib/krb5/krb/etype_list.c + * + * Copyright (C) 2010 by the Massachusetts Institute of Technology. + * All rights reserved. + * + * Export of this software from the United States of America may + * require a specific license from the United States Government. + * It is the responsibility of any person or organization contemplating + * export to obtain such a license before exporting. + * + * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. Furthermore if you modify this software you must label + * your software as modified software and not distribute it in such a + * fashion that it might be confused with the original M.I.T. software. + * M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * + * Helper functions related to zero-terminated lists of enctypes. + */ + +#include "k5-int.h" +#include "int-proto.h" + +size_t +krb5int_count_etypes(const krb5_enctype *list) +{ + size_t count; + + for (count = 0; list[count]; count++); + return count; +} + +/* Copy the zero-terminated enctype list old_list into *new_list. */ +krb5_error_code +krb5int_copy_etypes(const krb5_enctype *old_list, krb5_enctype **new_list) +{ + size_t count; + krb5_enctype *list; + + *new_list = NULL; + count = krb5int_count_etypes(old_list); + list = malloc(sizeof(krb5_enctype) * (count + 1)); + if (list == NULL) + return ENOMEM; + memcpy(list, old_list, sizeof(krb5_enctype) * (count + 1)); + *new_list = list; + return 0; +} + diff --git a/src/lib/krb5/krb/get_in_tkt.c b/src/lib/krb5/krb/get_in_tkt.c index 09df3f052..87521c594 100644 --- a/src/lib/krb5/krb/get_in_tkt.c +++ b/src/lib/krb5/krb/get_in_tkt.c @@ -720,13 +720,8 @@ krb5_get_in_tkt(krb5_context context, request.nonce = time_now; #endif /* APPLE_PKINIT */ - request.ktype = malloc (sizeof(get_in_tkt_enctypes)); - if (request.ktype == NULL) { - retval = ENOMEM; - goto cleanup; - } - memcpy(request.ktype, get_in_tkt_enctypes, sizeof(get_in_tkt_enctypes)); - for (request.nktypes = 0;request.ktype[request.nktypes];request.nktypes++); + retval = krb5int_copy_etypes(get_in_tkt_enctypes, &request.ktype); + request.nktypes = krb5int_count_etypes(request.ktype); if (ktypes) { int i, req, next = 0; for (req = 0; ktypes[req]; req++) { @@ -1433,10 +1428,7 @@ krb5_init_creds_init(krb5_context context, ctx->request->nktypes * sizeof(krb5_enctype)); } else if (krb5_get_default_in_tkt_ktypes(context, &ctx->request->ktype) == 0) { - for (ctx->request->nktypes = 0; - ctx->request->ktype[ctx->request->nktypes] != ENCTYPE_NULL; - ctx->request->nktypes++) - ; + ctx->request->nktypes = krb5int_count_etypes(ctx->request->ktype); } else { /* there isn't any useful default here. */ code = KRB5_CONFIG_ETYPE_NOSUPP; @@ -2134,7 +2126,7 @@ krb5int_populate_gic_opt(krb5_context context, krb5_get_init_creds_opt **out, if (addrs) krb5_get_init_creds_opt_set_address_list(opt, (krb5_address **) addrs); if (ktypes) { - for (i=0; ktypes[i]; i++); + i = krb5int_count_etypes(ktypes); if (i) krb5_get_init_creds_opt_set_etype_list(opt, ktypes, i); } diff --git a/src/lib/krb5/krb/init_ctx.c b/src/lib/krb5/krb/init_ctx.c index 65a840172..8f6a1b3dc 100644 --- a/src/lib/krb5/krb/init_ctx.c +++ b/src/lib/krb5/krb/init_ctx.c @@ -54,6 +54,7 @@ */ #include "k5-int.h" +#include "int-proto.h" #include #include "brand.c" /* There has to be a better way for windows... */ @@ -267,24 +268,6 @@ krb5_free_context(krb5_context ctx) free(ctx); } -/* Copy the zero-terminated enctype list old_list into *new_list. */ -static krb5_error_code -copy_enctypes(krb5_context context, const krb5_enctype *old_list, - krb5_enctype **new_list) -{ - unsigned int count; - krb5_enctype *list; - - *new_list = NULL; - for (count = 0; old_list[count]; count++); - list = malloc(sizeof(krb5_enctype) * (count + 1)); - if (list == NULL) - return ENOMEM; - memcpy(list, old_list, sizeof(krb5_enctype) * (count + 1)); - *new_list = list; - return 0; -} - /* * Set the desired default ktypes, making sure they are valid. */ @@ -304,7 +287,7 @@ set_default_etype_var(krb5_context context, const krb5_enctype *etypes, return KRB5_PROG_ETYPE_NOSUPP; } - code = copy_enctypes(context, etypes, &list); + code = krb5int_copy_etypes(etypes, &list); if (code) return code; } else { @@ -459,7 +442,7 @@ get_profile_etype_list(krb5_context context, krb5_enctype **etypes_ptr, if (ctx_list) { /* Use application defaults. */ - code = copy_enctypes(context, ctx_list, &etypes); + code = krb5int_copy_etypes(ctx_list, &etypes); if (code) return code; } else { diff --git a/src/lib/krb5/krb/int-proto.h b/src/lib/krb5/krb/int-proto.h index 4450bfcf1..b64bc4f77 100644 --- a/src/lib/krb5/krb/int-proto.h +++ b/src/lib/krb5/krb/int-proto.h @@ -164,4 +164,9 @@ krb5_error_code krb5int_decode_tgs_rep(krb5_context, krb5_data *, const krb5_keyblock *, krb5_keyusage, krb5_kdc_rep ** ); +/* Utility functions for zero-terminated enctype lists. */ +size_t krb5int_count_etypes(const krb5_enctype *list); +krb5_error_code krb5int_copy_etypes(const krb5_enctype *old_list, + krb5_enctype **new_list); + #endif /* KRB5_INT_FUNC_PROTO__ */ diff --git a/src/lib/krb5/krb/ser_ctx.c b/src/lib/krb5/krb/ser_ctx.c index 1dc836fee..a925b6d4a 100644 --- a/src/lib/krb5/krb/ser_ctx.c +++ b/src/lib/krb5/krb/ser_ctx.c @@ -31,6 +31,7 @@ * krb5_os_context structures. */ #include "k5-int.h" +#include "int-proto.h" /* * Routines to deal with externalizing the krb5_context: @@ -102,15 +103,10 @@ static const krb5_ser_entry krb5_profile_ser_entry = { }; #endif /* LEAN_CLIENT */ -static unsigned int +static inline unsigned int etypes_len(krb5_enctype *list) { - unsigned int i; - - if (list == NULL) - return 0; - for (i = 0; list[i]; i++); - return i; + return (list == NULL) ? 0 : krb5int_count_etypes(list); } /*