xref: /freebsd/crypto/krb5/src/lib/krb5/krb/copy_auth.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/copy_auth.c */
3 /*
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 /*
27  * Copyright (c) 2006-2008, Novell, Inc.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions are met:
32  *
33  *   * Redistributions of source code must retain the above copyright notice,
34  *       this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *       notice, this list of conditions and the following disclaimer in the
37  *       documentation and/or other materials provided with the distribution.
38  *   * The copyright holder's name is not used to endorse or promote products
39  *       derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  */
53 
54 #include "k5-int.h"
55 #include "int-proto.h"
56 
57 /*
58  * Copy an authdata array, with fresh allocation.
59  */
60 krb5_error_code KRB5_CALLCONV
krb5_merge_authdata(krb5_context context,krb5_authdata * const * inauthdat1,krb5_authdata * const * inauthdat2,krb5_authdata *** outauthdat)61 krb5_merge_authdata(krb5_context context,
62                     krb5_authdata *const *inauthdat1,
63                     krb5_authdata * const *inauthdat2,
64                     krb5_authdata ***outauthdat)
65 {
66     krb5_error_code retval;
67     krb5_authdata ** tempauthdat;
68     unsigned int nelems = 0, nelems2 = 0;
69 
70     *outauthdat = NULL;
71     if (!inauthdat1 && !inauthdat2) {
72         *outauthdat = 0;
73         return 0;
74     }
75 
76     if (inauthdat1)
77         while (inauthdat1[nelems]) nelems++;
78     if (inauthdat2)
79         while (inauthdat2[nelems2]) nelems2++;
80 
81     /* one more for a null terminated list */
82     if (!(tempauthdat = (krb5_authdata **) calloc(nelems+nelems2+1,
83                                                   sizeof(*tempauthdat))))
84         return ENOMEM;
85 
86     if (inauthdat1) {
87         for (nelems = 0; inauthdat1[nelems]; nelems++) {
88             retval = krb5int_copy_authdatum(context, inauthdat1[nelems],
89                                             &tempauthdat[nelems]);
90             if (retval) {
91                 krb5_free_authdata(context, tempauthdat);
92                 return retval;
93             }
94         }
95     }
96 
97     if (inauthdat2) {
98         for (nelems2 = 0; inauthdat2[nelems2]; nelems2++) {
99             retval = krb5int_copy_authdatum(context, inauthdat2[nelems2],
100                                             &tempauthdat[nelems++]);
101             if (retval) {
102                 krb5_free_authdata(context, tempauthdat);
103                 return retval;
104             }
105         }
106     }
107 
108     *outauthdat = tempauthdat;
109     return 0;
110 }
111 
112 krb5_error_code KRB5_CALLCONV
krb5_copy_authdata(krb5_context context,krb5_authdata * const * in_authdat,krb5_authdata *** out)113 krb5_copy_authdata(krb5_context context,
114                    krb5_authdata *const *in_authdat, krb5_authdata ***out)
115 {
116     return krb5_merge_authdata(context, in_authdat, NULL, out);
117 }
118