xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/krb/copy_tick.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * lib/krb5/krb/copy_tick.c
4  *
5  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  *
28  * krb5_copy_ticket()
29  */
30 
31 #include <k5-int.h>
32 
33 static krb5_error_code
34 krb5_copy_enc_tkt_part(krb5_context context, const krb5_enc_tkt_part *partfrom, krb5_enc_tkt_part **partto)
35 {
36     krb5_error_code retval;
37     krb5_enc_tkt_part *tempto;
38 
39     if (!(tempto = (krb5_enc_tkt_part *)malloc(sizeof(*tempto))))
40 	return ENOMEM;
41 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT
42     *tempto = *partfrom;
43 #else
44     memcpy(tempto, partfrom, sizeof(krb5_enc_tkt_part));
45 #endif
46     retval = krb5_copy_keyblock(context, partfrom->session,
47 				&tempto->session);
48     if (retval) {
49 	krb5_xfree(tempto);
50 	return retval;
51     }
52     retval = krb5_copy_principal(context, partfrom->client, &tempto->client);
53     if (retval) {
54 	krb5_free_keyblock(context, tempto->session);
55 	krb5_xfree(tempto);
56 	return retval;
57     }
58     tempto->transited = partfrom->transited;
59     if (tempto->transited.tr_contents.length == 0) {
60 	tempto->transited.tr_contents.data = 0;
61     } else {
62 	tempto->transited.tr_contents.data =
63 	  malloc(partfrom->transited.tr_contents.length);
64 	if (!tempto->transited.tr_contents.data) {
65 	    krb5_free_principal(context, tempto->client);
66 	    krb5_free_keyblock(context, tempto->session);
67 	    krb5_xfree(tempto);
68 	    return ENOMEM;
69 	}
70 	memcpy((char *)tempto->transited.tr_contents.data,
71 	       (char *)partfrom->transited.tr_contents.data,
72 	       partfrom->transited.tr_contents.length);
73     }
74 
75     retval = krb5_copy_addresses(context, partfrom->caddrs, &tempto->caddrs);
76     if (retval) {
77 	krb5_xfree(tempto->transited.tr_contents.data);
78 	krb5_free_principal(context, tempto->client);
79 	krb5_free_keyblock(context, tempto->session);
80 	krb5_xfree(tempto);
81 	return retval;
82     }
83     if (partfrom->authorization_data) {
84 	retval = krb5_copy_authdata(context, partfrom->authorization_data,
85 				    &tempto->authorization_data);
86 	if (retval) {
87 	    krb5_free_addresses(context, tempto->caddrs);
88 	    krb5_xfree(tempto->transited.tr_contents.data);
89 	    krb5_free_principal(context, tempto->client);
90 	    krb5_free_keyblock(context, tempto->session);
91 	    krb5_xfree(tempto);
92 	    return retval;
93 	}
94     }
95     *partto = tempto;
96     return 0;
97 }
98 
99 krb5_error_code KRB5_CALLCONV
100 krb5_copy_ticket(krb5_context context, const krb5_ticket *from, krb5_ticket **pto)
101 {
102     krb5_error_code retval;
103     krb5_ticket *tempto;
104     krb5_data *scratch;
105 
106     if (!(tempto = (krb5_ticket *)malloc(sizeof(*tempto))))
107 	return ENOMEM;
108 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT
109     *tempto = *from;
110 #else
111     memcpy(tempto, from, sizeof(krb5_ticket));
112 #endif
113     retval = krb5_copy_principal(context, from->server, &tempto->server);
114     if (retval) {
115 	krb5_xfree(tempto);
116 	return retval;
117     }
118     retval = krb5_copy_data(context, &from->enc_part.ciphertext, &scratch);
119     if (retval) {
120 	krb5_free_principal(context, tempto->server);
121 	krb5_xfree(tempto);
122 	return retval;
123     }
124     tempto->enc_part.ciphertext = *scratch;
125     krb5_xfree(scratch);
126     retval = krb5_copy_enc_tkt_part(context, from->enc_part2, &tempto->enc_part2);
127     if (retval) {
128 	krb5_xfree(tempto->enc_part.ciphertext.data);
129 	krb5_free_principal(context, tempto->server);
130 	krb5_xfree(tempto);
131 	return retval;
132     }
133     *pto = tempto;
134     return 0;
135 }
136