r19604: This is a massive commit, and I appologise in advance for it's size.
[Samba.git] / source / heimdal / lib / krb5 / ticket.c
blobfdc2a1b3a5d8a8e8a71871c25aeb4d81a2804041
1 /*
2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "krb5_locl.h"
36 RCSID("$Id: ticket.c,v 1.15 2006/10/14 09:53:19 lha Exp $");
38 krb5_error_code KRB5_LIB_FUNCTION
39 krb5_free_ticket(krb5_context context,
40 krb5_ticket *ticket)
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;
49 krb5_error_code KRB5_LIB_FUNCTION
50 krb5_copy_ticket(krb5_context context,
51 const krb5_ticket *from,
52 krb5_ticket **to)
54 krb5_error_code ret;
55 krb5_ticket *tmp;
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;
63 if((ret = copy_EncTicketPart(&from->ticket, &tmp->ticket))){
64 free(tmp);
65 return ret;
67 ret = krb5_copy_principal(context, from->client, &tmp->client);
68 if(ret){
69 free_EncTicketPart(&tmp->ticket);
70 free(tmp);
71 return ret;
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;
80 *to = tmp;
81 return 0;
84 krb5_error_code KRB5_LIB_FUNCTION
85 krb5_ticket_get_client(krb5_context context,
86 const krb5_ticket *ticket,
87 krb5_principal *client)
89 return krb5_copy_principal(context, ticket->client, client);
92 krb5_error_code KRB5_LIB_FUNCTION
93 krb5_ticket_get_server(krb5_context context,
94 const krb5_ticket *ticket,
95 krb5_principal *server)
97 return krb5_copy_principal(context, ticket->server, server);
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)
110 /* It is not an error if nothing in here, that is reported by *found */
111 /* Setting a default error causes found to be set to FALSE, on
112 * recursion to an second embedded authz data even if the first
113 * element contains the required type */
114 krb5_error_code ret = 0;
115 int i;
117 if (level > 9) {
118 krb5_set_error_string(context, "Authorization data nested deeper "
119 "then %d levels, stop searching", level);
120 ret = ENOENT; /* XXX */
121 goto out;
125 * Only copy out the element the first time we get to it, we need
126 * to run over the whole authorization data fields to check if
127 * there are any container clases we need to care about.
129 for (i = 0; i < ad->len; i++) {
130 if (!*found && ad->val[i].ad_type == type) {
131 ret = der_copy_octet_string(&ad->val[i].ad_data, data);
132 if (ret) {
133 krb5_set_error_string(context, "malloc - out of memory");
134 goto out;
136 *found = TRUE;
137 continue;
139 switch (ad->val[i].ad_type) {
140 case KRB5_AUTHDATA_IF_RELEVANT: {
141 AuthorizationData child;
142 ret = decode_AuthorizationData(ad->val[i].ad_data.data,
143 ad->val[i].ad_data.length,
144 &child,
145 NULL);
146 if (ret) {
147 krb5_set_error_string(context, "Failed to decode "
148 "IF_RELEVANT with %d", ret);
149 goto out;
151 ret = find_type_in_ad(context, type, data, found, 0, sessionkey,
152 &child, level + 1);
153 free_AuthorizationData(&child);
154 if (ret)
155 goto out;
156 break;
158 #if 0 /* XXX test */
159 case KRB5_AUTHDATA_KDC_ISSUED: {
160 AD_KDCIssued child;
162 ret = decode_AD_KDCIssued(ad->val[i].ad_data.data,
163 ad->val[i].ad_data.length,
164 &child,
165 NULL);
166 if (ret) {
167 krb5_set_error_string(context, "Failed to decode "
168 "AD_KDCIssued with %d", ret);
169 goto out;
171 if (failp) {
172 krb5_boolean valid;
173 krb5_data buf;
174 size_t len;
176 ASN1_MALLOC_ENCODE(AuthorizationData, buf.data, buf.length,
177 &child.elements, &len, ret);
178 if (ret) {
179 free_AD_KDCIssued(&child);
180 krb5_clear_error_string(context);
181 goto out;
183 if(buf.length != len)
184 krb5_abortx(context, "internal error in ASN.1 encoder");
186 ret = krb5_c_verify_checksum(context, sessionkey, 19, &buf,
187 &child.ad_checksum, &valid);
188 krb5_data_free(&buf);
189 if (ret) {
190 free_AD_KDCIssued(&child);
191 goto out;
193 if (!valid) {
194 krb5_clear_error_string(context);
195 ret = ENOENT;
196 free_AD_KDCIssued(&child);
197 goto out;
200 ret = find_type_in_ad(context, type, data, found, failp, sessionkey,
201 &child.elements, level + 1);
202 free_AD_KDCIssued(&child);
203 if (ret)
204 goto out;
205 break;
207 #endif
208 case KRB5_AUTHDATA_AND_OR:
209 if (!failp)
210 break;
211 krb5_set_error_string(context, "Authorization data contains "
212 "AND-OR element that is unknown to the "
213 "application");
214 ret = ENOENT; /* XXX */
215 goto out;
216 default:
217 if (!failp)
218 break;
219 krb5_set_error_string(context, "Authorization data contains "
220 "unknown type (%d) ", ad->val[i].ad_type);
221 ret = ENOENT; /* XXX */
222 goto out;
225 out:
226 if (ret) {
227 if (*found) {
228 krb5_data_free(data);
229 *found = 0;
232 return ret;
236 _krb5_find_type_in_ad(krb5_context context,
237 int type,
238 krb5_data *data,
239 krb5_boolean *found,
240 krb5_keyblock *sessionkey,
241 const AuthorizationData *ad)
243 krb5_data_zero(data);
244 return find_type_in_ad(context, type, data, found, TRUE, sessionkey, ad, 0);
249 * Extract the authorization data type of `type' from the
250 * 'ticket'. Store the field in `data'. This function is to use for
251 * kerberos applications.
254 krb5_error_code KRB5_LIB_FUNCTION
255 krb5_ticket_get_authorization_data_type(krb5_context context,
256 krb5_ticket *ticket,
257 int type,
258 krb5_data *data)
260 AuthorizationData *ad;
261 krb5_error_code ret;
262 krb5_boolean found = 0;
264 ad = ticket->ticket.authorization_data;
265 if (ticket->ticket.authorization_data == NULL) {
266 krb5_set_error_string(context, "Ticket have not authorization data");
267 return ENOENT; /* XXX */
270 ret = _krb5_find_type_in_ad(context, type, data, &found, &ticket->ticket.key,
271 ticket->ticket.authorization_data);
272 if (ret)
273 return ret;
274 if (!found) {
275 krb5_set_error_string(context, "Ticket have not authorization "
276 "data of type %d", type);
277 return ENOENT; /* XXX */
279 return 0;