5eff64e12db1b6929fc42887928bf66630f32f24
[metze/samba/wip.git] / source / heimdal / lib / krb5 / ticket.c
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "krb5_locl.h"
35
36 RCSID("$Id: ticket.c 23310 2008-06-23 03:30:49Z lha $");
37
38 krb5_error_code KRB5_LIB_FUNCTION
39 krb5_free_ticket(krb5_context context,
40                  krb5_ticket *ticket)
41 {
42     free_EncTicketPart(&ticket->ticket);
43     krb5_free_principal(context, ticket->client);
44     krb5_free_principal(context, ticket->server);
45     free(ticket);
46     return 0;
47 }
48
49 krb5_error_code KRB5_LIB_FUNCTION
50 krb5_copy_ticket(krb5_context context,
51                  const krb5_ticket *from,
52                  krb5_ticket **to)
53 {
54     krb5_error_code ret;
55     krb5_ticket *tmp;
56
57     *to = NULL;
58     tmp = malloc(sizeof(*tmp));
59     if(tmp == NULL) {
60         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
61         return ENOMEM;
62     }
63     if((ret = copy_EncTicketPart(&from->ticket, &tmp->ticket))){
64         free(tmp);
65         return ret;
66     }
67     ret = krb5_copy_principal(context, from->client, &tmp->client);
68     if(ret){
69         free_EncTicketPart(&tmp->ticket);
70         free(tmp);
71         return ret;
72     }
73     ret = krb5_copy_principal(context, from->server, &tmp->server);
74     if(ret){
75         krb5_free_principal(context, tmp->client);
76         free_EncTicketPart(&tmp->ticket);
77         free(tmp);
78         return ret;
79     }
80     *to = tmp;
81     return 0;
82 }
83
84 krb5_error_code KRB5_LIB_FUNCTION
85 krb5_ticket_get_client(krb5_context context,
86                        const krb5_ticket *ticket,
87                        krb5_principal *client)
88 {
89     return krb5_copy_principal(context, ticket->client, client);
90 }
91
92 krb5_error_code KRB5_LIB_FUNCTION
93 krb5_ticket_get_server(krb5_context context,
94                        const krb5_ticket *ticket,
95                        krb5_principal *server)
96 {
97     return krb5_copy_principal(context, ticket->server, server);
98 }
99
100 time_t KRB5_LIB_FUNCTION
101 krb5_ticket_get_endtime(krb5_context context,
102                         const krb5_ticket *ticket)
103 {
104     return ticket->ticket.endtime;
105 }
106
107 static int
108 find_type_in_ad(krb5_context context,
109                 int type, 
110                 krb5_data *data,
111                 krb5_boolean *found,
112                 krb5_boolean failp,
113                 krb5_keyblock *sessionkey,
114                 const AuthorizationData *ad,
115                 int level)
116 {
117     krb5_error_code ret = 0;
118     int i;
119
120     if (level > 9) {
121         ret = ENOENT; /* XXX */
122         krb5_set_error_message(context, ret, 
123                                "Authorization data nested deeper "
124                                "then %d levels, stop searching", level);
125         goto out;
126     }
127
128     /*
129      * Only copy out the element the first time we get to it, we need
130      * to run over the whole authorization data fields to check if
131      * there are any container clases we need to care about.
132      */
133     for (i = 0; i < ad->len; i++) {
134         if (!*found && ad->val[i].ad_type == type) {
135             ret = der_copy_octet_string(&ad->val[i].ad_data, data);
136             if (ret) {
137                 krb5_set_error_message(context, ret, "malloc: out of memory");
138                 goto out;
139             }
140             *found = TRUE;
141             continue;
142         }
143         switch (ad->val[i].ad_type) {
144         case KRB5_AUTHDATA_IF_RELEVANT: {
145             AuthorizationData child;
146             ret = decode_AuthorizationData(ad->val[i].ad_data.data,
147                                            ad->val[i].ad_data.length,
148                                            &child,
149                                            NULL);
150             if (ret) {
151                 krb5_set_error_message(context, ret, "Failed to decode "
152                                        "IF_RELEVANT with %d", (int)ret);
153                 goto out;
154             }
155             ret = find_type_in_ad(context, type, data, found, FALSE,
156                                   sessionkey, &child, level + 1);
157             free_AuthorizationData(&child);
158             if (ret)
159                 goto out;
160             break;
161         }
162 #if 0 /* XXX test */
163         case KRB5_AUTHDATA_KDC_ISSUED: {
164             AD_KDCIssued child;
165
166             ret = decode_AD_KDCIssued(ad->val[i].ad_data.data,
167                                       ad->val[i].ad_data.length,
168                                       &child,
169                                       NULL);
170             if (ret) {
171                 krb5_set_error_message(context, ret, "Failed to decode "
172                                        "AD_KDCIssued with %d", ret);
173                 goto out;
174             }
175             if (failp) {
176                 krb5_boolean valid;
177                 krb5_data buf;
178                 size_t len;
179
180                 ASN1_MALLOC_ENCODE(AuthorizationData, buf.data, buf.length, 
181                                    &child.elements, &len, ret);
182                 if (ret) {
183                     free_AD_KDCIssued(&child);
184                     krb5_clear_error_string(context);
185                     goto out;
186                 }
187                 if(buf.length != len)
188                     krb5_abortx(context, "internal error in ASN.1 encoder");
189
190                 ret = krb5_c_verify_checksum(context, sessionkey, 19, &buf,
191                                              &child.ad_checksum, &valid);
192                 krb5_data_free(&buf);
193                 if (ret) {
194                     free_AD_KDCIssued(&child);
195                     goto out;
196                 }
197                 if (!valid) {
198                     krb5_clear_error_string(context);
199                     ret = ENOENT;
200                     free_AD_KDCIssued(&child);
201                     goto out;
202                 }
203             }
204             ret = find_type_in_ad(context, type, data, found, failp, sessionkey,
205                                   &child.elements, level + 1);
206             free_AD_KDCIssued(&child);
207             if (ret)
208                 goto out;
209             break;
210         }
211 #endif
212         case KRB5_AUTHDATA_AND_OR:
213             if (!failp)
214                 break;
215             ret = ENOENT; /* XXX */
216             krb5_set_error_message(context, ret, "Authorization data contains "
217                                    "AND-OR element that is unknown to the "
218                                    "application");
219             goto out;
220         default:
221             if (!failp)
222                 break;
223             ret = ENOENT; /* XXX */
224             krb5_set_error_message(context, ret, "Authorization data contains "
225                                    "unknown type (%d) ", ad->val[i].ad_type);
226             goto out;
227         }
228     }
229 out:
230     if (ret) {
231         if (*found) {
232             krb5_data_free(data);
233             *found = 0;
234         }
235     }
236     return ret;
237 }
238
239 /*
240  * Extract the authorization data type of `type' from the
241  * 'ticket'. Store the field in `data'. This function is to use for
242  * kerberos applications.
243  */
244
245 krb5_error_code KRB5_LIB_FUNCTION
246 krb5_ticket_get_authorization_data_type(krb5_context context,
247                                         krb5_ticket *ticket,
248                                         int type,
249                                         krb5_data *data)
250 {
251     AuthorizationData *ad;
252     krb5_error_code ret;
253     krb5_boolean found = FALSE;
254
255     krb5_data_zero(data);
256
257     ad = ticket->ticket.authorization_data;
258     if (ticket->ticket.authorization_data == NULL) {
259         krb5_set_error_message(context, ENOENT,
260                                "Ticket have not authorization data");
261         return ENOENT; /* XXX */
262     }
263
264     ret = find_type_in_ad(context, type, data, &found, TRUE,
265                           &ticket->ticket.key, ad, 0);
266     if (ret)
267         return ret;
268     if (!found) {
269         krb5_set_error_message(context, ENOENT, "Ticket have not "
270                                "authorization data of type %d", type);
271         return ENOENT; /* XXX */
272     }
273     return 0;
274 }