b3efeb39d3ae25ee93938d9c75259512801cfc20
[samba.git] / source4 / 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,v 1.14 2005/10/27 13:21:42 lha Exp $");
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_string (context, "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 static int
101 find_type_in_ad(krb5_context context,
102                 int type, 
103                 krb5_data *data,
104                 krb5_boolean *found,
105                 krb5_boolean failp,
106                 krb5_keyblock *sessionkey,
107                 const AuthorizationData *ad,
108                 int level)
109 {
110     krb5_error_code ret = ENOENT;
111     int i;
112
113     if (level > 9) {
114         krb5_set_error_string(context, "Authorization data nested deeper "
115                               "then %d levels, stop searching", level);
116         ret = ENOENT; /* XXX */
117         goto out;
118     }
119
120     /*
121      * Only copy out the element the first time we get to it, we need
122      * to run over the whole authorization data fields to check if
123      * there are any container clases we need to care about.
124      */
125     for (i = 0; i < ad->len; i++) {
126         if (!*found && ad->val[i].ad_type == type) {
127             ret = copy_octet_string(&ad->val[i].ad_data, data);
128             if (ret) {
129                 krb5_set_error_string(context, "malloc - out of memory");
130                 goto out;
131             }
132             *found = TRUE;
133             continue;
134         }
135         switch (ad->val[i].ad_type) {
136         case KRB5_AUTHDATA_IF_RELEVANT: {
137             AuthorizationData child;
138             ret = decode_AuthorizationData(ad->val[i].ad_data.data,
139                                            ad->val[i].ad_data.length,
140                                            &child,
141                                            NULL);
142             if (ret) {
143                 krb5_set_error_string(context, "Failed to decode "
144                                       "IF_RELEVANT with %d", ret);
145                 goto out;
146             }
147             ret = find_type_in_ad(context, type, data, found, 0, sessionkey,
148                                   &child, level + 1);
149             free_AuthorizationData(&child);
150             if (ret)
151                 goto out;
152             break;
153         }
154 #if 0 /* XXX test */
155         case KRB5_AUTHDATA_KDC_ISSUED: {
156             AD_KDCIssued child;
157
158             ret = decode_AD_KDCIssued(ad->val[i].ad_data.data,
159                                       ad->val[i].ad_data.length,
160                                       &child,
161                                       NULL);
162             if (ret) {
163                 krb5_set_error_string(context, "Failed to decode "
164                                       "AD_KDCIssued with %d", ret);
165                 goto out;
166             }
167             if (failp) {
168                 krb5_boolean valid;
169                 krb5_data buf;
170                 size_t len;
171
172                 ASN1_MALLOC_ENCODE(AuthorizationData, buf.data, buf.length, 
173                                    &child.elements, &len, ret);
174                 if (ret) {
175                     free_AD_KDCIssued(&child);
176                     krb5_clear_error_string(context);
177                     goto out;
178                 }
179                 if(buf.length != len)
180                     krb5_abortx(context, "internal error in ASN.1 encoder");
181
182                 ret = krb5_c_verify_checksum(context, sessionkey, 19, &buf,
183                                              &child.ad_checksum, &valid);
184                 krb5_data_free(&buf);
185                 if (ret) {
186                     free_AD_KDCIssued(&child);
187                     goto out;
188                 }
189                 if (!valid) {
190                     krb5_clear_error_string(context);
191                     ret = ENOENT;
192                     free_AD_KDCIssued(&child);
193                     goto out;
194                 }
195             }
196             ret = find_type_in_ad(context, type, data, found, failp, sessionkey,
197                                   &child.elements, level + 1);
198             free_AD_KDCIssued(&child);
199             if (ret)
200                 goto out;
201             break;
202         }
203 #endif
204         case KRB5_AUTHDATA_AND_OR:
205             if (!failp)
206                 break;
207             krb5_set_error_string(context, "Authorization data contains "
208                                   "AND-OR element that is unknown to the "
209                                   "application");
210             ret = ENOENT; /* XXX */
211             goto out;
212         default:
213             if (!failp)
214                 break;
215             krb5_set_error_string(context, "Authorization data contains "
216                                   "unknown type (%d) ", ad->val[i].ad_type);
217             ret = ENOENT; /* XXX */
218             goto out;
219         }
220     }
221 out:
222     if (ret) {
223         if (*found) {
224             krb5_data_free(data);
225             *found = 0;
226         }
227     }
228     return ret;
229 }
230
231 int
232 _krb5_find_type_in_ad(krb5_context context,
233                       int type, 
234                       krb5_data *data,
235                       krb5_boolean *found,
236                       krb5_keyblock *sessionkey,
237                       const AuthorizationData *ad)
238 {
239     krb5_data_zero(data);
240     return find_type_in_ad(context, type, data, found, TRUE, sessionkey, ad, 0);
241 }
242
243
244 /*
245  * Extract the authorization data type of `type' from the
246  * 'ticket'. Store the field in `data'. This function is to use for
247  * kerberos applications.
248  */
249
250 krb5_error_code KRB5_LIB_FUNCTION
251 krb5_ticket_get_authorization_data_type(krb5_context context,
252                                         krb5_ticket *ticket,
253                                         int type,
254                                         krb5_data *data)
255 {
256     AuthorizationData *ad;
257     krb5_error_code ret;
258     krb5_boolean found = 0;
259
260     ad = ticket->ticket.authorization_data;
261     if (ticket->ticket.authorization_data == NULL) {
262         krb5_set_error_string(context, "Ticket have not authorization data");
263         return ENOENT; /* XXX */
264     }
265
266     ret = _krb5_find_type_in_ad(context, type, data, &found, &ticket->ticket.key,
267                                 ticket->ticket.authorization_data);
268     if (ret)
269         return ret;
270     if (!found) {
271         krb5_set_error_string(context, "Ticket have not authorization "
272                           "data of type %d", type);
273         return ENOENT; /* XXX */
274     }
275     return 0;
276 }