1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * lib/krb5/krb/copy_auth.c
8  *
9  * Copyright 1990 by the Massachusetts Institute of Technology.
10  * All Rights Reserved.
11  *
12  * Export of this software from the United States of America may
13  *   require a specific license from the United States Government.
14  *   It is the responsibility of any person or organization contemplating
15  *   export to obtain such a license before exporting.
16  *
17  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18  * distribute this software and its documentation for any purpose and
19  * without fee is hereby granted, provided that the above copyright
20  * notice appear in all copies and that both that copyright notice and
21  * this permission notice appear in supporting documentation, and that
22  * the name of M.I.T. not be used in advertising or publicity pertaining
23  * to distribution of the software without specific, written prior
24  * permission.  Furthermore if you modify this software you must label
25  * your software as modified software and not distribute it in such a
26  * fashion that it might be confused with the original M.I.T. software.
27  * M.I.T. makes no representations about the suitability of
28  * this software for any purpose.  It is provided "as is" without express
29  * or implied warranty.
30  *
31  *
32  * krb5_copy_authdata()
33  */
34 
35 #include "k5-int.h"
36 
37 /*ARGSUSED*/
38 static krb5_error_code
39 krb5_copy_authdatum(krb5_context context, const krb5_authdata *inad, krb5_authdata **outad)
40 {
41     krb5_authdata *tmpad;
42 
43     if (!(tmpad = (krb5_authdata *)MALLOC(sizeof(*tmpad))))
44 	return ENOMEM;
45     *tmpad = *inad;
46     if (!(tmpad->contents = (krb5_octet *)MALLOC(inad->length))) {
47 	krb5_xfree_wrap(tmpad, inad->length);
48 	return ENOMEM;
49     }
50     (void) memcpy((char *)tmpad->contents, (char *)inad->contents, inad->length);
51     *outad = tmpad;
52     return 0;
53 }
54 
55 /*
56  * Copy an authdata array, with fresh allocation.
57  */
58 krb5_error_code KRB5_CALLCONV
59 krb5_copy_authdata(krb5_context context, krb5_authdata *const *inauthdat, krb5_authdata ***outauthdat)
60 {
61     krb5_error_code retval;
62     krb5_authdata ** tempauthdat;
63     register unsigned int nelems = 0;
64 
65     if (!inauthdat) {
66 	    *outauthdat = 0;
67 	    return 0;
68     }
69 
70     while (inauthdat[nelems]) nelems++;
71 
72     /* one more for a null terminated list */
73     if (!(tempauthdat = (krb5_authdata **) CALLOC(nelems+1,
74 						  sizeof(*tempauthdat))))
75 	return ENOMEM;
76 
77     for (nelems = 0; inauthdat[nelems]; nelems++) {
78 	retval = krb5_copy_authdatum(context, inauthdat[nelems],
79 				     &tempauthdat[nelems]);
80 	if (retval) {
81 	    krb5_free_authdata(context, tempauthdat);
82 	    return retval;
83 	}
84     }
85 
86     *outauthdat = tempauthdat;
87     return 0;
88 }
89