use DES_set_key_unchecked().
[heimdal.git] / lib / krb5 / ticket.c
blob70a1c6af0df70537d2afe9a1381fe1a15251a055
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$");
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 time_t KRB5_LIB_FUNCTION
101 krb5_ticket_get_endtime(krb5_context context,
102 const krb5_ticket *ticket)
104 return ticket->ticket.endtime;
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)
117 krb5_error_code ret = 0;
118 int i;
120 if (level > 9) {
121 krb5_set_error_string(context, "Authorization data nested deeper "
122 "then %d levels, stop searching", level);
123 ret = ENOENT; /* XXX */
124 goto out;
128 * Only copy out the element the first time we get to it, we need
129 * to run over the whole authorization data fields to check if
130 * there are any container clases we need to care about.
132 for (i = 0; i < ad->len; i++) {
133 if (!*found && ad->val[i].ad_type == type) {
134 ret = der_copy_octet_string(&ad->val[i].ad_data, data);
135 if (ret) {
136 krb5_set_error_string(context, "malloc - out of memory");
137 goto out;
139 *found = TRUE;
140 continue;
142 switch (ad->val[i].ad_type) {
143 case KRB5_AUTHDATA_IF_RELEVANT: {
144 AuthorizationData child;
145 ret = decode_AuthorizationData(ad->val[i].ad_data.data,
146 ad->val[i].ad_data.length,
147 &child,
148 NULL);
149 if (ret) {
150 krb5_set_error_string(context, "Failed to decode "
151 "IF_RELEVANT with %d", (int)ret);
152 goto out;
154 ret = find_type_in_ad(context, type, data, found, FALSE,
155 sessionkey, &child, level + 1);
156 free_AuthorizationData(&child);
157 if (ret)
158 goto out;
159 break;
161 #if 0 /* XXX test */
162 case KRB5_AUTHDATA_KDC_ISSUED: {
163 AD_KDCIssued child;
165 ret = decode_AD_KDCIssued(ad->val[i].ad_data.data,
166 ad->val[i].ad_data.length,
167 &child,
168 NULL);
169 if (ret) {
170 krb5_set_error_string(context, "Failed to decode "
171 "AD_KDCIssued with %d", ret);
172 goto out;
174 if (failp) {
175 krb5_boolean valid;
176 krb5_data buf;
177 size_t len;
179 ASN1_MALLOC_ENCODE(AuthorizationData, buf.data, buf.length,
180 &child.elements, &len, ret);
181 if (ret) {
182 free_AD_KDCIssued(&child);
183 krb5_clear_error_string(context);
184 goto out;
186 if(buf.length != len)
187 krb5_abortx(context, "internal error in ASN.1 encoder");
189 ret = krb5_c_verify_checksum(context, sessionkey, 19, &buf,
190 &child.ad_checksum, &valid);
191 krb5_data_free(&buf);
192 if (ret) {
193 free_AD_KDCIssued(&child);
194 goto out;
196 if (!valid) {
197 krb5_clear_error_string(context);
198 ret = ENOENT;
199 free_AD_KDCIssued(&child);
200 goto out;
203 ret = find_type_in_ad(context, type, data, found, failp, sessionkey,
204 &child.elements, level + 1);
205 free_AD_KDCIssued(&child);
206 if (ret)
207 goto out;
208 break;
210 #endif
211 case KRB5_AUTHDATA_AND_OR:
212 if (!failp)
213 break;
214 krb5_set_error_string(context, "Authorization data contains "
215 "AND-OR element that is unknown to the "
216 "application");
217 ret = ENOENT; /* XXX */
218 goto out;
219 default:
220 if (!failp)
221 break;
222 krb5_set_error_string(context, "Authorization data contains "
223 "unknown type (%d) ", ad->val[i].ad_type);
224 ret = ENOENT; /* XXX */
225 goto out;
228 out:
229 if (ret) {
230 if (*found) {
231 krb5_data_free(data);
232 *found = 0;
235 return ret;
239 * Extract the authorization data type of `type' from the
240 * 'ticket'. Store the field in `data'. This function is to use for
241 * kerberos applications.
244 krb5_error_code KRB5_LIB_FUNCTION
245 krb5_ticket_get_authorization_data_type(krb5_context context,
246 krb5_ticket *ticket,
247 int type,
248 krb5_data *data)
250 AuthorizationData *ad;
251 krb5_error_code ret;
252 krb5_boolean found = FALSE;
254 krb5_data_zero(data);
256 ad = ticket->ticket.authorization_data;
257 if (ticket->ticket.authorization_data == NULL) {
258 krb5_set_error_string(context, "Ticket have not authorization data");
259 return ENOENT; /* XXX */
262 ret = find_type_in_ad(context, type, data, &found, TRUE,
263 &ticket->ticket.key, ad, 0);
264 if (ret)
265 return ret;
266 if (!found) {
267 krb5_set_error_string(context, "Ticket have not authorization "
268 "data of type %d", type);
269 return ENOENT; /* XXX */
271 return 0;