c3e33ff824382402e7340207949358ff5b397be8
[platform/upstream/krb5.git] / src / lib / krb5 / krb / copy_tick.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/copy_tick.c */
3 /*
4  * Copyright 1990,1991 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 #include "k5-int.h"
28
29 static krb5_error_code
30 copy_enc_tkt_part(krb5_context context, const krb5_enc_tkt_part *partfrom,
31                   krb5_enc_tkt_part **partto)
32 {
33     krb5_error_code retval;
34     krb5_enc_tkt_part *tempto;
35
36     if (!(tempto = (krb5_enc_tkt_part *)malloc(sizeof(*tempto))))
37         return ENOMEM;
38     *tempto = *partfrom;
39     retval = krb5_copy_keyblock(context, partfrom->session,
40                                 &tempto->session);
41     if (retval) {
42         free(tempto);
43         return retval;
44     }
45     retval = krb5_copy_principal(context, partfrom->client, &tempto->client);
46     if (retval) {
47         krb5_free_keyblock(context, tempto->session);
48         free(tempto);
49         return retval;
50     }
51     tempto->transited = partfrom->transited;
52     if (tempto->transited.tr_contents.length == 0) {
53         tempto->transited.tr_contents.data = 0;
54     } else {
55         tempto->transited.tr_contents.data =
56             malloc(partfrom->transited.tr_contents.length);
57         if (!tempto->transited.tr_contents.data) {
58             krb5_free_principal(context, tempto->client);
59             krb5_free_keyblock(context, tempto->session);
60             free(tempto);
61             return ENOMEM;
62         }
63         memcpy(tempto->transited.tr_contents.data,
64                (char *)partfrom->transited.tr_contents.data,
65                partfrom->transited.tr_contents.length);
66     }
67
68     retval = krb5_copy_addresses(context, partfrom->caddrs, &tempto->caddrs);
69     if (retval) {
70         free(tempto->transited.tr_contents.data);
71         krb5_free_principal(context, tempto->client);
72         krb5_free_keyblock(context, tempto->session);
73         free(tempto);
74         return retval;
75     }
76     if (partfrom->authorization_data) {
77         retval = krb5_copy_authdata(context, partfrom->authorization_data,
78                                     &tempto->authorization_data);
79         if (retval) {
80             krb5_free_addresses(context, tempto->caddrs);
81             free(tempto->transited.tr_contents.data);
82             krb5_free_principal(context, tempto->client);
83             krb5_free_keyblock(context, tempto->session);
84             free(tempto);
85             return retval;
86         }
87     }
88     *partto = tempto;
89     return 0;
90 }
91
92 krb5_error_code KRB5_CALLCONV
93 krb5_copy_ticket(krb5_context context, const krb5_ticket *from, krb5_ticket **pto)
94 {
95     krb5_error_code retval;
96     krb5_ticket *tempto;
97     krb5_data *scratch;
98
99     if (!(tempto = (krb5_ticket *)malloc(sizeof(*tempto))))
100         return ENOMEM;
101     *tempto = *from;
102     retval = krb5_copy_principal(context, from->server, &tempto->server);
103     if (retval) {
104         free(tempto);
105         return retval;
106     }
107     retval = krb5_copy_data(context, &from->enc_part.ciphertext, &scratch);
108     if (retval) {
109         krb5_free_principal(context, tempto->server);
110         free(tempto);
111         return retval;
112     }
113     tempto->enc_part.ciphertext = *scratch;
114     free(scratch);
115     retval = copy_enc_tkt_part(context, from->enc_part2, &tempto->enc_part2);
116     if (retval) {
117         free(tempto->enc_part.ciphertext.data);
118         krb5_free_principal(context, tempto->server);
119         free(tempto);
120         return retval;
121     }
122     *pto = tempto;
123     return 0;
124 }