xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/ticket.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ticket.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc  *    without specific prior written permission.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc  * SUCH DAMAGE.
36ebfedea0SLionel Sambuc  */
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include "krb5_locl.h"
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc /**
41ebfedea0SLionel Sambuc  * Free ticket and content
42ebfedea0SLionel Sambuc  *
43ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
44ebfedea0SLionel Sambuc  * @param ticket ticket to free
45ebfedea0SLionel Sambuc  *
46ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success.  Otherwise an kerberos et
47ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
48ebfedea0SLionel Sambuc  *
49ebfedea0SLionel Sambuc  * @ingroup krb5
50ebfedea0SLionel Sambuc  */
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_free_ticket(krb5_context context,krb5_ticket * ticket)53ebfedea0SLionel Sambuc krb5_free_ticket(krb5_context context,
54ebfedea0SLionel Sambuc 		 krb5_ticket *ticket)
55ebfedea0SLionel Sambuc {
56ebfedea0SLionel Sambuc     free_EncTicketPart(&ticket->ticket);
57ebfedea0SLionel Sambuc     krb5_free_principal(context, ticket->client);
58ebfedea0SLionel Sambuc     krb5_free_principal(context, ticket->server);
59ebfedea0SLionel Sambuc     free(ticket);
60ebfedea0SLionel Sambuc     return 0;
61ebfedea0SLionel Sambuc }
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc /**
64ebfedea0SLionel Sambuc  * Copy ticket and content
65ebfedea0SLionel Sambuc  *
66ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
67ebfedea0SLionel Sambuc  * @param from ticket to copy
68ebfedea0SLionel Sambuc  * @param to new copy of ticket, free with krb5_free_ticket()
69ebfedea0SLionel Sambuc  *
70ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success.  Otherwise an kerberos et
71ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * @ingroup krb5
74ebfedea0SLionel Sambuc  */
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_ticket(krb5_context context,const krb5_ticket * from,krb5_ticket ** to)77ebfedea0SLionel Sambuc krb5_copy_ticket(krb5_context context,
78ebfedea0SLionel Sambuc 		 const krb5_ticket *from,
79ebfedea0SLionel Sambuc 		 krb5_ticket **to)
80ebfedea0SLionel Sambuc {
81ebfedea0SLionel Sambuc     krb5_error_code ret;
82ebfedea0SLionel Sambuc     krb5_ticket *tmp;
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     *to = NULL;
85ebfedea0SLionel Sambuc     tmp = malloc(sizeof(*tmp));
86ebfedea0SLionel Sambuc     if(tmp == NULL) {
87ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM,
88ebfedea0SLionel Sambuc 			       N_("malloc: out of memory", ""));
89ebfedea0SLionel Sambuc 	return ENOMEM;
90ebfedea0SLionel Sambuc     }
91ebfedea0SLionel Sambuc     if((ret = copy_EncTicketPart(&from->ticket, &tmp->ticket))){
92ebfedea0SLionel Sambuc 	free(tmp);
93ebfedea0SLionel Sambuc 	return ret;
94ebfedea0SLionel Sambuc     }
95ebfedea0SLionel Sambuc     ret = krb5_copy_principal(context, from->client, &tmp->client);
96ebfedea0SLionel Sambuc     if(ret){
97ebfedea0SLionel Sambuc 	free_EncTicketPart(&tmp->ticket);
98ebfedea0SLionel Sambuc 	free(tmp);
99ebfedea0SLionel Sambuc 	return ret;
100ebfedea0SLionel Sambuc     }
101ebfedea0SLionel Sambuc     ret = krb5_copy_principal(context, from->server, &tmp->server);
102ebfedea0SLionel Sambuc     if(ret){
103ebfedea0SLionel Sambuc 	krb5_free_principal(context, tmp->client);
104ebfedea0SLionel Sambuc 	free_EncTicketPart(&tmp->ticket);
105ebfedea0SLionel Sambuc 	free(tmp);
106ebfedea0SLionel Sambuc 	return ret;
107ebfedea0SLionel Sambuc     }
108ebfedea0SLionel Sambuc     *to = tmp;
109ebfedea0SLionel Sambuc     return 0;
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc /**
113ebfedea0SLionel Sambuc  * Return client principal in ticket
114ebfedea0SLionel Sambuc  *
115ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
116ebfedea0SLionel Sambuc  * @param ticket ticket to copy
117ebfedea0SLionel Sambuc  * @param client client principal, free with krb5_free_principal()
118ebfedea0SLionel Sambuc  *
119ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success.  Otherwise an kerberos et
120ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
121ebfedea0SLionel Sambuc  *
122ebfedea0SLionel Sambuc  * @ingroup krb5
123ebfedea0SLionel Sambuc  */
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_ticket_get_client(krb5_context context,const krb5_ticket * ticket,krb5_principal * client)126ebfedea0SLionel Sambuc krb5_ticket_get_client(krb5_context context,
127ebfedea0SLionel Sambuc 		       const krb5_ticket *ticket,
128ebfedea0SLionel Sambuc 		       krb5_principal *client)
129ebfedea0SLionel Sambuc {
130ebfedea0SLionel Sambuc     return krb5_copy_principal(context, ticket->client, client);
131ebfedea0SLionel Sambuc }
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc /**
134ebfedea0SLionel Sambuc  * Return server principal in ticket
135ebfedea0SLionel Sambuc  *
136ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
137ebfedea0SLionel Sambuc  * @param ticket ticket to copy
138ebfedea0SLionel Sambuc  * @param server server principal, free with krb5_free_principal()
139ebfedea0SLionel Sambuc  *
140ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success.  Otherwise an kerberos et
141ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
142ebfedea0SLionel Sambuc  *
143ebfedea0SLionel Sambuc  * @ingroup krb5
144ebfedea0SLionel Sambuc  */
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_ticket_get_server(krb5_context context,const krb5_ticket * ticket,krb5_principal * server)147ebfedea0SLionel Sambuc krb5_ticket_get_server(krb5_context context,
148ebfedea0SLionel Sambuc 		       const krb5_ticket *ticket,
149ebfedea0SLionel Sambuc 		       krb5_principal *server)
150ebfedea0SLionel Sambuc {
151ebfedea0SLionel Sambuc     return krb5_copy_principal(context, ticket->server, server);
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc /**
155ebfedea0SLionel Sambuc  * Return end time of ticket
156ebfedea0SLionel Sambuc  *
157ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
158ebfedea0SLionel Sambuc  * @param ticket ticket to copy
159ebfedea0SLionel Sambuc  *
160ebfedea0SLionel Sambuc  * @return end time of ticket
161ebfedea0SLionel Sambuc  *
162ebfedea0SLionel Sambuc  * @ingroup krb5
163ebfedea0SLionel Sambuc  */
164ebfedea0SLionel Sambuc 
165ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION time_t KRB5_LIB_CALL
krb5_ticket_get_endtime(krb5_context context,const krb5_ticket * ticket)166ebfedea0SLionel Sambuc krb5_ticket_get_endtime(krb5_context context,
167ebfedea0SLionel Sambuc 			const krb5_ticket *ticket)
168ebfedea0SLionel Sambuc {
169ebfedea0SLionel Sambuc     return ticket->ticket.endtime;
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc /**
173ebfedea0SLionel Sambuc  * Get the flags from the Kerberos ticket
174ebfedea0SLionel Sambuc  *
175ebfedea0SLionel Sambuc  * @param context Kerberos context
176ebfedea0SLionel Sambuc  * @param ticket Kerberos ticket
177ebfedea0SLionel Sambuc  *
178ebfedea0SLionel Sambuc  * @return ticket flags
179ebfedea0SLionel Sambuc  *
180ebfedea0SLionel Sambuc  * @ingroup krb5_ticket
181ebfedea0SLionel Sambuc  */
182ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION unsigned long KRB5_LIB_CALL
krb5_ticket_get_flags(krb5_context context,const krb5_ticket * ticket)183ebfedea0SLionel Sambuc krb5_ticket_get_flags(krb5_context context,
184ebfedea0SLionel Sambuc 		      const krb5_ticket *ticket)
185ebfedea0SLionel Sambuc {
186ebfedea0SLionel Sambuc     return TicketFlags2int(ticket->ticket.flags);
187ebfedea0SLionel Sambuc }
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc static int
find_type_in_ad(krb5_context context,int type,krb5_data * data,krb5_boolean * found,krb5_boolean failp,krb5_keyblock * sessionkey,const AuthorizationData * ad,int level)190ebfedea0SLionel Sambuc find_type_in_ad(krb5_context context,
191ebfedea0SLionel Sambuc 		int type,
192ebfedea0SLionel Sambuc 		krb5_data *data,
193ebfedea0SLionel Sambuc 		krb5_boolean *found,
194ebfedea0SLionel Sambuc 		krb5_boolean failp,
195ebfedea0SLionel Sambuc 		krb5_keyblock *sessionkey,
196ebfedea0SLionel Sambuc 		const AuthorizationData *ad,
197ebfedea0SLionel Sambuc 		int level)
198ebfedea0SLionel Sambuc {
199ebfedea0SLionel Sambuc     krb5_error_code ret = 0;
200*0a6a1f1dSLionel Sambuc     size_t i;
201ebfedea0SLionel Sambuc 
202ebfedea0SLionel Sambuc     if (level > 9) {
203ebfedea0SLionel Sambuc 	ret = ENOENT; /* XXX */
204ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
205ebfedea0SLionel Sambuc 			       N_("Authorization data nested deeper "
206ebfedea0SLionel Sambuc 				  "then %d levels, stop searching", ""),
207ebfedea0SLionel Sambuc 			       level);
208ebfedea0SLionel Sambuc 	goto out;
209ebfedea0SLionel Sambuc     }
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     /*
212ebfedea0SLionel Sambuc      * Only copy out the element the first time we get to it, we need
213ebfedea0SLionel Sambuc      * to run over the whole authorization data fields to check if
214ebfedea0SLionel Sambuc      * there are any container clases we need to care about.
215ebfedea0SLionel Sambuc      */
216ebfedea0SLionel Sambuc     for (i = 0; i < ad->len; i++) {
217ebfedea0SLionel Sambuc 	if (!*found && ad->val[i].ad_type == type) {
218ebfedea0SLionel Sambuc 	    ret = der_copy_octet_string(&ad->val[i].ad_data, data);
219ebfedea0SLionel Sambuc 	    if (ret) {
220ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
221ebfedea0SLionel Sambuc 				       N_("malloc: out of memory", ""));
222ebfedea0SLionel Sambuc 		goto out;
223ebfedea0SLionel Sambuc 	    }
224ebfedea0SLionel Sambuc 	    *found = TRUE;
225ebfedea0SLionel Sambuc 	    continue;
226ebfedea0SLionel Sambuc 	}
227ebfedea0SLionel Sambuc 	switch (ad->val[i].ad_type) {
228ebfedea0SLionel Sambuc 	case KRB5_AUTHDATA_IF_RELEVANT: {
229ebfedea0SLionel Sambuc 	    AuthorizationData child;
230ebfedea0SLionel Sambuc 	    ret = decode_AuthorizationData(ad->val[i].ad_data.data,
231ebfedea0SLionel Sambuc 					   ad->val[i].ad_data.length,
232ebfedea0SLionel Sambuc 					   &child,
233ebfedea0SLionel Sambuc 					   NULL);
234ebfedea0SLionel Sambuc 	    if (ret) {
235ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
236ebfedea0SLionel Sambuc 				       N_("Failed to decode "
237ebfedea0SLionel Sambuc 					  "IF_RELEVANT with %d", ""),
238ebfedea0SLionel Sambuc 				       (int)ret);
239ebfedea0SLionel Sambuc 		goto out;
240ebfedea0SLionel Sambuc 	    }
241ebfedea0SLionel Sambuc 	    ret = find_type_in_ad(context, type, data, found, FALSE,
242ebfedea0SLionel Sambuc 				  sessionkey, &child, level + 1);
243ebfedea0SLionel Sambuc 	    free_AuthorizationData(&child);
244ebfedea0SLionel Sambuc 	    if (ret)
245ebfedea0SLionel Sambuc 		goto out;
246ebfedea0SLionel Sambuc 	    break;
247ebfedea0SLionel Sambuc 	}
248ebfedea0SLionel Sambuc #if 0 /* XXX test */
249ebfedea0SLionel Sambuc 	case KRB5_AUTHDATA_KDC_ISSUED: {
250ebfedea0SLionel Sambuc 	    AD_KDCIssued child;
251ebfedea0SLionel Sambuc 
252ebfedea0SLionel Sambuc 	    ret = decode_AD_KDCIssued(ad->val[i].ad_data.data,
253ebfedea0SLionel Sambuc 				      ad->val[i].ad_data.length,
254ebfedea0SLionel Sambuc 				      &child,
255ebfedea0SLionel Sambuc 				      NULL);
256ebfedea0SLionel Sambuc 	    if (ret) {
257ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret,
258ebfedea0SLionel Sambuc 				       N_("Failed to decode "
259ebfedea0SLionel Sambuc 					  "AD_KDCIssued with %d", ""),
260ebfedea0SLionel Sambuc 				       ret);
261ebfedea0SLionel Sambuc 		goto out;
262ebfedea0SLionel Sambuc 	    }
263ebfedea0SLionel Sambuc 	    if (failp) {
264ebfedea0SLionel Sambuc 		krb5_boolean valid;
265ebfedea0SLionel Sambuc 		krb5_data buf;
266ebfedea0SLionel Sambuc 		size_t len;
267ebfedea0SLionel Sambuc 
268ebfedea0SLionel Sambuc 		ASN1_MALLOC_ENCODE(AuthorizationData, buf.data, buf.length,
269ebfedea0SLionel Sambuc 				   &child.elements, &len, ret);
270ebfedea0SLionel Sambuc 		if (ret) {
271ebfedea0SLionel Sambuc 		    free_AD_KDCIssued(&child);
272ebfedea0SLionel Sambuc 		    krb5_clear_error_message(context);
273ebfedea0SLionel Sambuc 		    goto out;
274ebfedea0SLionel Sambuc 		}
275ebfedea0SLionel Sambuc 		if(buf.length != len)
276ebfedea0SLionel Sambuc 		    krb5_abortx(context, "internal error in ASN.1 encoder");
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc 		ret = krb5_c_verify_checksum(context, sessionkey, 19, &buf,
279ebfedea0SLionel Sambuc 					     &child.ad_checksum, &valid);
280ebfedea0SLionel Sambuc 		krb5_data_free(&buf);
281ebfedea0SLionel Sambuc 		if (ret) {
282ebfedea0SLionel Sambuc 		    free_AD_KDCIssued(&child);
283ebfedea0SLionel Sambuc 		    goto out;
284ebfedea0SLionel Sambuc 		}
285ebfedea0SLionel Sambuc 		if (!valid) {
286ebfedea0SLionel Sambuc 		    krb5_clear_error_message(context);
287ebfedea0SLionel Sambuc 		    ret = ENOENT;
288ebfedea0SLionel Sambuc 		    free_AD_KDCIssued(&child);
289ebfedea0SLionel Sambuc 		    goto out;
290ebfedea0SLionel Sambuc 		}
291ebfedea0SLionel Sambuc 	    }
292ebfedea0SLionel Sambuc 	    ret = find_type_in_ad(context, type, data, found, failp, sessionkey,
293ebfedea0SLionel Sambuc 				  &child.elements, level + 1);
294ebfedea0SLionel Sambuc 	    free_AD_KDCIssued(&child);
295ebfedea0SLionel Sambuc 	    if (ret)
296ebfedea0SLionel Sambuc 		goto out;
297ebfedea0SLionel Sambuc 	    break;
298ebfedea0SLionel Sambuc 	}
299ebfedea0SLionel Sambuc #endif
300ebfedea0SLionel Sambuc 	case KRB5_AUTHDATA_AND_OR:
301ebfedea0SLionel Sambuc 	    if (!failp)
302ebfedea0SLionel Sambuc 		break;
303ebfedea0SLionel Sambuc 	    ret = ENOENT; /* XXX */
304ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
305ebfedea0SLionel Sambuc 				   N_("Authorization data contains "
306ebfedea0SLionel Sambuc 				      "AND-OR element that is unknown to the "
307ebfedea0SLionel Sambuc 				      "application", ""));
308ebfedea0SLionel Sambuc 	    goto out;
309ebfedea0SLionel Sambuc 	default:
310ebfedea0SLionel Sambuc 	    if (!failp)
311ebfedea0SLionel Sambuc 		break;
312ebfedea0SLionel Sambuc 	    ret = ENOENT; /* XXX */
313ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, ret,
314ebfedea0SLionel Sambuc 				   N_("Authorization data contains "
315ebfedea0SLionel Sambuc 				      "unknown type (%d) ", ""),
316ebfedea0SLionel Sambuc 				   ad->val[i].ad_type);
317ebfedea0SLionel Sambuc 	    goto out;
318ebfedea0SLionel Sambuc 	}
319ebfedea0SLionel Sambuc     }
320ebfedea0SLionel Sambuc out:
321ebfedea0SLionel Sambuc     if (ret) {
322ebfedea0SLionel Sambuc 	if (*found) {
323ebfedea0SLionel Sambuc 	    krb5_data_free(data);
324ebfedea0SLionel Sambuc 	    *found = 0;
325ebfedea0SLionel Sambuc 	}
326ebfedea0SLionel Sambuc     }
327ebfedea0SLionel Sambuc     return ret;
328ebfedea0SLionel Sambuc }
329ebfedea0SLionel Sambuc 
330ebfedea0SLionel Sambuc /**
331ebfedea0SLionel Sambuc  * Extract the authorization data type of type from the ticket. Store
332ebfedea0SLionel Sambuc  * the field in data. This function is to use for kerberos
333ebfedea0SLionel Sambuc  * applications.
334ebfedea0SLionel Sambuc  *
335ebfedea0SLionel Sambuc  * @param context a Kerberos 5 context
336ebfedea0SLionel Sambuc  * @param ticket Kerberos ticket
337ebfedea0SLionel Sambuc  * @param type type to fetch
338ebfedea0SLionel Sambuc  * @param data returned data, free with krb5_data_free()
339ebfedea0SLionel Sambuc  *
340ebfedea0SLionel Sambuc  * @ingroup krb5
341ebfedea0SLionel Sambuc  */
342ebfedea0SLionel Sambuc 
343ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_ticket_get_authorization_data_type(krb5_context context,krb5_ticket * ticket,int type,krb5_data * data)344ebfedea0SLionel Sambuc krb5_ticket_get_authorization_data_type(krb5_context context,
345ebfedea0SLionel Sambuc 					krb5_ticket *ticket,
346ebfedea0SLionel Sambuc 					int type,
347ebfedea0SLionel Sambuc 					krb5_data *data)
348ebfedea0SLionel Sambuc {
349ebfedea0SLionel Sambuc     AuthorizationData *ad;
350ebfedea0SLionel Sambuc     krb5_error_code ret;
351ebfedea0SLionel Sambuc     krb5_boolean found = FALSE;
352ebfedea0SLionel Sambuc 
353ebfedea0SLionel Sambuc     krb5_data_zero(data);
354ebfedea0SLionel Sambuc 
355ebfedea0SLionel Sambuc     ad = ticket->ticket.authorization_data;
356ebfedea0SLionel Sambuc     if (ticket->ticket.authorization_data == NULL) {
357ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOENT,
358ebfedea0SLionel Sambuc 			       N_("Ticket have not authorization data", ""));
359ebfedea0SLionel Sambuc 	return ENOENT; /* XXX */
360ebfedea0SLionel Sambuc     }
361ebfedea0SLionel Sambuc 
362ebfedea0SLionel Sambuc     ret = find_type_in_ad(context, type, data, &found, TRUE,
363ebfedea0SLionel Sambuc 			  &ticket->ticket.key, ad, 0);
364ebfedea0SLionel Sambuc     if (ret)
365ebfedea0SLionel Sambuc 	return ret;
366ebfedea0SLionel Sambuc     if (!found) {
367ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOENT,
368ebfedea0SLionel Sambuc 			       N_("Ticket have not "
369ebfedea0SLionel Sambuc 				  "authorization data of type %d", ""),
370ebfedea0SLionel Sambuc 			       type);
371ebfedea0SLionel Sambuc 	return ENOENT; /* XXX */
372ebfedea0SLionel Sambuc     }
373ebfedea0SLionel Sambuc     return 0;
374ebfedea0SLionel Sambuc }
375ebfedea0SLionel Sambuc 
376ebfedea0SLionel Sambuc static krb5_error_code
check_server_referral(krb5_context context,krb5_kdc_rep * rep,unsigned flags,krb5_const_principal requested,krb5_const_principal returned,krb5_keyblock * key)377ebfedea0SLionel Sambuc check_server_referral(krb5_context context,
378ebfedea0SLionel Sambuc 		      krb5_kdc_rep *rep,
379ebfedea0SLionel Sambuc 		      unsigned flags,
380ebfedea0SLionel Sambuc 		      krb5_const_principal requested,
381ebfedea0SLionel Sambuc 		      krb5_const_principal returned,
382ebfedea0SLionel Sambuc 		      krb5_keyblock * key)
383ebfedea0SLionel Sambuc {
384ebfedea0SLionel Sambuc     krb5_error_code ret;
385ebfedea0SLionel Sambuc     PA_ServerReferralData ref;
386ebfedea0SLionel Sambuc     krb5_crypto session;
387ebfedea0SLionel Sambuc     EncryptedData ed;
388ebfedea0SLionel Sambuc     size_t len;
389ebfedea0SLionel Sambuc     krb5_data data;
390ebfedea0SLionel Sambuc     PA_DATA *pa;
391ebfedea0SLionel Sambuc     int i = 0, cmp;
392ebfedea0SLionel Sambuc 
393ebfedea0SLionel Sambuc     if (rep->kdc_rep.padata == NULL)
394ebfedea0SLionel Sambuc 	goto noreferral;
395ebfedea0SLionel Sambuc 
396ebfedea0SLionel Sambuc     pa = krb5_find_padata(rep->kdc_rep.padata->val,
397ebfedea0SLionel Sambuc 			  rep->kdc_rep.padata->len,
398ebfedea0SLionel Sambuc 			  KRB5_PADATA_SERVER_REFERRAL, &i);
399ebfedea0SLionel Sambuc     if (pa == NULL)
400ebfedea0SLionel Sambuc 	goto noreferral;
401ebfedea0SLionel Sambuc 
402ebfedea0SLionel Sambuc     memset(&ed, 0, sizeof(ed));
403ebfedea0SLionel Sambuc     memset(&ref, 0, sizeof(ref));
404ebfedea0SLionel Sambuc 
405ebfedea0SLionel Sambuc     ret = decode_EncryptedData(pa->padata_value.data,
406ebfedea0SLionel Sambuc 			       pa->padata_value.length,
407ebfedea0SLionel Sambuc 			       &ed, &len);
408ebfedea0SLionel Sambuc     if (ret)
409ebfedea0SLionel Sambuc 	return ret;
410ebfedea0SLionel Sambuc     if (len != pa->padata_value.length) {
411ebfedea0SLionel Sambuc 	free_EncryptedData(&ed);
412ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
413ebfedea0SLionel Sambuc 			       N_("Referral EncryptedData wrong for realm %s",
414ebfedea0SLionel Sambuc 				  "realm"), requested->realm);
415ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_MODIFIED;
416ebfedea0SLionel Sambuc     }
417ebfedea0SLionel Sambuc 
418ebfedea0SLionel Sambuc     ret = krb5_crypto_init(context, key, 0, &session);
419ebfedea0SLionel Sambuc     if (ret) {
420ebfedea0SLionel Sambuc 	free_EncryptedData(&ed);
421ebfedea0SLionel Sambuc 	return ret;
422ebfedea0SLionel Sambuc     }
423ebfedea0SLionel Sambuc 
424ebfedea0SLionel Sambuc     ret = krb5_decrypt_EncryptedData(context, session,
425ebfedea0SLionel Sambuc 				     KRB5_KU_PA_SERVER_REFERRAL,
426ebfedea0SLionel Sambuc 				     &ed, &data);
427ebfedea0SLionel Sambuc     free_EncryptedData(&ed);
428ebfedea0SLionel Sambuc     krb5_crypto_destroy(context, session);
429ebfedea0SLionel Sambuc     if (ret)
430ebfedea0SLionel Sambuc 	return ret;
431ebfedea0SLionel Sambuc 
432ebfedea0SLionel Sambuc     ret = decode_PA_ServerReferralData(data.data, data.length, &ref, &len);
433ebfedea0SLionel Sambuc     if (ret) {
434ebfedea0SLionel Sambuc 	krb5_data_free(&data);
435ebfedea0SLionel Sambuc 	return ret;
436ebfedea0SLionel Sambuc     }
437ebfedea0SLionel Sambuc     krb5_data_free(&data);
438ebfedea0SLionel Sambuc 
439ebfedea0SLionel Sambuc     if (strcmp(requested->realm, returned->realm) != 0) {
440ebfedea0SLionel Sambuc 	free_PA_ServerReferralData(&ref);
441ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
442ebfedea0SLionel Sambuc 			       N_("server ref realm mismatch, "
443ebfedea0SLionel Sambuc 				  "requested realm %s got back %s", ""),
444ebfedea0SLionel Sambuc 			       requested->realm, returned->realm);
445ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_MODIFIED;
446ebfedea0SLionel Sambuc     }
447ebfedea0SLionel Sambuc 
448ebfedea0SLionel Sambuc     if (krb5_principal_is_krbtgt(context, returned)) {
449ebfedea0SLionel Sambuc 	const char *realm = returned->name.name_string.val[1];
450ebfedea0SLionel Sambuc 
451ebfedea0SLionel Sambuc 	if (ref.referred_realm == NULL
452ebfedea0SLionel Sambuc 	    || strcmp(*ref.referred_realm, realm) != 0)
453ebfedea0SLionel Sambuc 	{
454ebfedea0SLionel Sambuc 	    free_PA_ServerReferralData(&ref);
455ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
456ebfedea0SLionel Sambuc 				   N_("tgt returned with wrong ref", ""));
457ebfedea0SLionel Sambuc 	    return KRB5KRB_AP_ERR_MODIFIED;
458ebfedea0SLionel Sambuc 	}
459ebfedea0SLionel Sambuc     } else if (krb5_principal_compare(context, returned, requested) == 0) {
460ebfedea0SLionel Sambuc 	free_PA_ServerReferralData(&ref);
461ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
462ebfedea0SLionel Sambuc 			       N_("req princ no same as returned", ""));
463ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_MODIFIED;
464ebfedea0SLionel Sambuc     }
465ebfedea0SLionel Sambuc 
466ebfedea0SLionel Sambuc     if (ref.requested_principal_name) {
467ebfedea0SLionel Sambuc 	cmp = _krb5_principal_compare_PrincipalName(context,
468ebfedea0SLionel Sambuc 						    requested,
469ebfedea0SLionel Sambuc 						    ref.requested_principal_name);
470ebfedea0SLionel Sambuc 	if (!cmp) {
471ebfedea0SLionel Sambuc 	    free_PA_ServerReferralData(&ref);
472ebfedea0SLionel Sambuc 	    krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
473ebfedea0SLionel Sambuc 				   N_("referred principal not same "
474ebfedea0SLionel Sambuc 				      "as requested", ""));
475ebfedea0SLionel Sambuc 	    return KRB5KRB_AP_ERR_MODIFIED;
476ebfedea0SLionel Sambuc 	}
477ebfedea0SLionel Sambuc     } else if (flags & EXTRACT_TICKET_AS_REQ) {
478ebfedea0SLionel Sambuc 	free_PA_ServerReferralData(&ref);
479ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
480ebfedea0SLionel Sambuc 			       N_("Requested principal missing on AS-REQ", ""));
481ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_MODIFIED;
482ebfedea0SLionel Sambuc     }
483ebfedea0SLionel Sambuc 
484ebfedea0SLionel Sambuc     free_PA_ServerReferralData(&ref);
485ebfedea0SLionel Sambuc 
486ebfedea0SLionel Sambuc     return ret;
487ebfedea0SLionel Sambuc noreferral:
488ebfedea0SLionel Sambuc     /*
489ebfedea0SLionel Sambuc      * Expect excact match or that we got a krbtgt
490ebfedea0SLionel Sambuc      */
491ebfedea0SLionel Sambuc     if (krb5_principal_compare(context, requested, returned) != TRUE &&
492ebfedea0SLionel Sambuc 	(krb5_realm_compare(context, requested, returned) != TRUE &&
493ebfedea0SLionel Sambuc 	 krb5_principal_is_krbtgt(context, returned) != TRUE))
494ebfedea0SLionel Sambuc     {
495ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
496ebfedea0SLionel Sambuc 			       N_("Not same server principal returned "
497ebfedea0SLionel Sambuc 				  "as requested", ""));
498ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_MODIFIED;
499ebfedea0SLionel Sambuc     }
500ebfedea0SLionel Sambuc     return 0;
501ebfedea0SLionel Sambuc }
502ebfedea0SLionel Sambuc 
503ebfedea0SLionel Sambuc 
504ebfedea0SLionel Sambuc /*
505ebfedea0SLionel Sambuc  * Verify referral data
506ebfedea0SLionel Sambuc  */
507ebfedea0SLionel Sambuc 
508ebfedea0SLionel Sambuc 
509ebfedea0SLionel Sambuc static krb5_error_code
check_client_referral(krb5_context context,krb5_kdc_rep * rep,krb5_const_principal requested,krb5_const_principal mapped,krb5_keyblock const * key)510ebfedea0SLionel Sambuc check_client_referral(krb5_context context,
511ebfedea0SLionel Sambuc 		      krb5_kdc_rep *rep,
512ebfedea0SLionel Sambuc 		      krb5_const_principal requested,
513ebfedea0SLionel Sambuc 		      krb5_const_principal mapped,
514ebfedea0SLionel Sambuc 		      krb5_keyblock const * key)
515ebfedea0SLionel Sambuc {
516ebfedea0SLionel Sambuc     krb5_error_code ret;
517ebfedea0SLionel Sambuc     PA_ClientCanonicalized canon;
518ebfedea0SLionel Sambuc     krb5_crypto crypto;
519ebfedea0SLionel Sambuc     krb5_data data;
520ebfedea0SLionel Sambuc     PA_DATA *pa;
521ebfedea0SLionel Sambuc     size_t len;
522ebfedea0SLionel Sambuc     int i = 0;
523ebfedea0SLionel Sambuc 
524ebfedea0SLionel Sambuc     if (rep->kdc_rep.padata == NULL)
525ebfedea0SLionel Sambuc 	goto noreferral;
526ebfedea0SLionel Sambuc 
527ebfedea0SLionel Sambuc     pa = krb5_find_padata(rep->kdc_rep.padata->val,
528ebfedea0SLionel Sambuc 			  rep->kdc_rep.padata->len,
529ebfedea0SLionel Sambuc 			  KRB5_PADATA_CLIENT_CANONICALIZED, &i);
530ebfedea0SLionel Sambuc     if (pa == NULL)
531ebfedea0SLionel Sambuc 	goto noreferral;
532ebfedea0SLionel Sambuc 
533ebfedea0SLionel Sambuc     ret = decode_PA_ClientCanonicalized(pa->padata_value.data,
534ebfedea0SLionel Sambuc 					pa->padata_value.length,
535ebfedea0SLionel Sambuc 					&canon, &len);
536ebfedea0SLionel Sambuc     if (ret) {
537ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
538ebfedea0SLionel Sambuc 			       N_("Failed to decode ClientCanonicalized "
539ebfedea0SLionel Sambuc 				  "from realm %s", ""), requested->realm);
540ebfedea0SLionel Sambuc 	return ret;
541ebfedea0SLionel Sambuc     }
542ebfedea0SLionel Sambuc 
543ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
544ebfedea0SLionel Sambuc 		       &canon.names, &len, ret);
545ebfedea0SLionel Sambuc     if (ret) {
546ebfedea0SLionel Sambuc 	free_PA_ClientCanonicalized(&canon);
547ebfedea0SLionel Sambuc 	return ret;
548ebfedea0SLionel Sambuc     }
549ebfedea0SLionel Sambuc     if (data.length != len)
550ebfedea0SLionel Sambuc 	krb5_abortx(context, "internal asn.1 error");
551ebfedea0SLionel Sambuc 
552ebfedea0SLionel Sambuc     ret = krb5_crypto_init(context, key, 0, &crypto);
553ebfedea0SLionel Sambuc     if (ret) {
554ebfedea0SLionel Sambuc 	free(data.data);
555ebfedea0SLionel Sambuc 	free_PA_ClientCanonicalized(&canon);
556ebfedea0SLionel Sambuc 	return ret;
557ebfedea0SLionel Sambuc     }
558ebfedea0SLionel Sambuc 
559ebfedea0SLionel Sambuc     ret = krb5_verify_checksum(context, crypto, KRB5_KU_CANONICALIZED_NAMES,
560ebfedea0SLionel Sambuc 			       data.data, data.length,
561ebfedea0SLionel Sambuc 			       &canon.canon_checksum);
562ebfedea0SLionel Sambuc     krb5_crypto_destroy(context, crypto);
563ebfedea0SLionel Sambuc     free(data.data);
564ebfedea0SLionel Sambuc     if (ret) {
565ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret,
566ebfedea0SLionel Sambuc 			       N_("Failed to verify client canonicalized "
567ebfedea0SLionel Sambuc 				  "data from realm %s", ""),
568ebfedea0SLionel Sambuc 			       requested->realm);
569ebfedea0SLionel Sambuc 	free_PA_ClientCanonicalized(&canon);
570ebfedea0SLionel Sambuc 	return ret;
571ebfedea0SLionel Sambuc     }
572ebfedea0SLionel Sambuc 
573ebfedea0SLionel Sambuc     if (!_krb5_principal_compare_PrincipalName(context,
574ebfedea0SLionel Sambuc 					       requested,
575ebfedea0SLionel Sambuc 					       &canon.names.requested_name))
576ebfedea0SLionel Sambuc     {
577ebfedea0SLionel Sambuc 	free_PA_ClientCanonicalized(&canon);
578ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_PRINC_NOMATCH,
579ebfedea0SLionel Sambuc 			       N_("Requested name doesn't match"
580ebfedea0SLionel Sambuc 				  " in client referral", ""));
581ebfedea0SLionel Sambuc 	return KRB5_PRINC_NOMATCH;
582ebfedea0SLionel Sambuc     }
583ebfedea0SLionel Sambuc     if (!_krb5_principal_compare_PrincipalName(context,
584ebfedea0SLionel Sambuc 					       mapped,
585ebfedea0SLionel Sambuc 					       &canon.names.mapped_name))
586ebfedea0SLionel Sambuc     {
587ebfedea0SLionel Sambuc 	free_PA_ClientCanonicalized(&canon);
588ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5_PRINC_NOMATCH,
589ebfedea0SLionel Sambuc 			       N_("Mapped name doesn't match"
590ebfedea0SLionel Sambuc 				  " in client referral", ""));
591ebfedea0SLionel Sambuc 	return KRB5_PRINC_NOMATCH;
592ebfedea0SLionel Sambuc     }
593ebfedea0SLionel Sambuc 
594ebfedea0SLionel Sambuc     return 0;
595ebfedea0SLionel Sambuc 
596ebfedea0SLionel Sambuc noreferral:
597ebfedea0SLionel Sambuc     if (krb5_principal_compare(context, requested, mapped) == FALSE) {
598ebfedea0SLionel Sambuc 	krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED,
599ebfedea0SLionel Sambuc 			       N_("Not same client principal returned "
600ebfedea0SLionel Sambuc 				  "as requested", ""));
601ebfedea0SLionel Sambuc 	return KRB5KRB_AP_ERR_MODIFIED;
602ebfedea0SLionel Sambuc     }
603ebfedea0SLionel Sambuc     return 0;
604ebfedea0SLionel Sambuc }
605ebfedea0SLionel Sambuc 
606ebfedea0SLionel Sambuc 
607ebfedea0SLionel Sambuc static krb5_error_code KRB5_CALLCONV
decrypt_tkt(krb5_context context,krb5_keyblock * key,krb5_key_usage usage,krb5_const_pointer decrypt_arg,krb5_kdc_rep * dec_rep)608ebfedea0SLionel Sambuc decrypt_tkt (krb5_context context,
609ebfedea0SLionel Sambuc 	     krb5_keyblock *key,
610ebfedea0SLionel Sambuc 	     krb5_key_usage usage,
611ebfedea0SLionel Sambuc 	     krb5_const_pointer decrypt_arg,
612ebfedea0SLionel Sambuc 	     krb5_kdc_rep *dec_rep)
613ebfedea0SLionel Sambuc {
614ebfedea0SLionel Sambuc     krb5_error_code ret;
615ebfedea0SLionel Sambuc     krb5_data data;
616ebfedea0SLionel Sambuc     size_t size;
617ebfedea0SLionel Sambuc     krb5_crypto crypto;
618ebfedea0SLionel Sambuc 
619ebfedea0SLionel Sambuc     ret = krb5_crypto_init(context, key, 0, &crypto);
620ebfedea0SLionel Sambuc     if (ret)
621ebfedea0SLionel Sambuc 	return ret;
622ebfedea0SLionel Sambuc 
623ebfedea0SLionel Sambuc     ret = krb5_decrypt_EncryptedData (context,
624ebfedea0SLionel Sambuc 				      crypto,
625ebfedea0SLionel Sambuc 				      usage,
626ebfedea0SLionel Sambuc 				      &dec_rep->kdc_rep.enc_part,
627ebfedea0SLionel Sambuc 				      &data);
628ebfedea0SLionel Sambuc     krb5_crypto_destroy(context, crypto);
629ebfedea0SLionel Sambuc 
630ebfedea0SLionel Sambuc     if (ret)
631ebfedea0SLionel Sambuc 	return ret;
632ebfedea0SLionel Sambuc 
633ebfedea0SLionel Sambuc     ret = decode_EncASRepPart(data.data,
634ebfedea0SLionel Sambuc 			      data.length,
635ebfedea0SLionel Sambuc 			      &dec_rep->enc_part,
636ebfedea0SLionel Sambuc 			      &size);
637ebfedea0SLionel Sambuc     if (ret)
638ebfedea0SLionel Sambuc 	ret = decode_EncTGSRepPart(data.data,
639ebfedea0SLionel Sambuc 				   data.length,
640ebfedea0SLionel Sambuc 				   &dec_rep->enc_part,
641ebfedea0SLionel Sambuc 				   &size);
642ebfedea0SLionel Sambuc     krb5_data_free (&data);
643ebfedea0SLionel Sambuc     if (ret) {
644ebfedea0SLionel Sambuc         krb5_set_error_message(context, ret,
645ebfedea0SLionel Sambuc 			       N_("Failed to decode encpart in ticket", ""));
646ebfedea0SLionel Sambuc 	return ret;
647ebfedea0SLionel Sambuc     }
648ebfedea0SLionel Sambuc     return 0;
649ebfedea0SLionel Sambuc }
650ebfedea0SLionel Sambuc 
651ebfedea0SLionel Sambuc int
_krb5_extract_ticket(krb5_context context,krb5_kdc_rep * rep,krb5_creds * creds,krb5_keyblock * key,krb5_const_pointer keyseed,krb5_key_usage key_usage,krb5_addresses * addrs,unsigned nonce,unsigned flags,krb5_decrypt_proc decrypt_proc,krb5_const_pointer decryptarg)652ebfedea0SLionel Sambuc _krb5_extract_ticket(krb5_context context,
653ebfedea0SLionel Sambuc 		     krb5_kdc_rep *rep,
654ebfedea0SLionel Sambuc 		     krb5_creds *creds,
655ebfedea0SLionel Sambuc 		     krb5_keyblock *key,
656ebfedea0SLionel Sambuc 		     krb5_const_pointer keyseed,
657ebfedea0SLionel Sambuc 		     krb5_key_usage key_usage,
658ebfedea0SLionel Sambuc 		     krb5_addresses *addrs,
659ebfedea0SLionel Sambuc 		     unsigned nonce,
660ebfedea0SLionel Sambuc 		     unsigned flags,
661ebfedea0SLionel Sambuc 		     krb5_decrypt_proc decrypt_proc,
662ebfedea0SLionel Sambuc 		     krb5_const_pointer decryptarg)
663ebfedea0SLionel Sambuc {
664ebfedea0SLionel Sambuc     krb5_error_code ret;
665ebfedea0SLionel Sambuc     krb5_principal tmp_principal;
666*0a6a1f1dSLionel Sambuc     size_t len = 0;
667ebfedea0SLionel Sambuc     time_t tmp_time;
668ebfedea0SLionel Sambuc     krb5_timestamp sec_now;
669ebfedea0SLionel Sambuc 
670ebfedea0SLionel Sambuc     /* decrypt */
671ebfedea0SLionel Sambuc 
672ebfedea0SLionel Sambuc     if (decrypt_proc == NULL)
673ebfedea0SLionel Sambuc 	decrypt_proc = decrypt_tkt;
674ebfedea0SLionel Sambuc 
675ebfedea0SLionel Sambuc     ret = (*decrypt_proc)(context, key, key_usage, decryptarg, rep);
676ebfedea0SLionel Sambuc     if (ret)
677ebfedea0SLionel Sambuc 	goto out;
678ebfedea0SLionel Sambuc 
679ebfedea0SLionel Sambuc     /* save session key */
680ebfedea0SLionel Sambuc 
681ebfedea0SLionel Sambuc     creds->session.keyvalue.length = 0;
682ebfedea0SLionel Sambuc     creds->session.keyvalue.data   = NULL;
683ebfedea0SLionel Sambuc     creds->session.keytype = rep->enc_part.key.keytype;
684ebfedea0SLionel Sambuc     ret = krb5_data_copy (&creds->session.keyvalue,
685ebfedea0SLionel Sambuc 			  rep->enc_part.key.keyvalue.data,
686ebfedea0SLionel Sambuc 			  rep->enc_part.key.keyvalue.length);
687ebfedea0SLionel Sambuc     if (ret) {
688ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
689ebfedea0SLionel Sambuc 	goto out;
690ebfedea0SLionel Sambuc     }
691ebfedea0SLionel Sambuc 
692ebfedea0SLionel Sambuc     /* compare client and save */
693ebfedea0SLionel Sambuc     ret = _krb5_principalname2krb5_principal (context,
694ebfedea0SLionel Sambuc 					      &tmp_principal,
695ebfedea0SLionel Sambuc 					      rep->kdc_rep.cname,
696ebfedea0SLionel Sambuc 					      rep->kdc_rep.crealm);
697ebfedea0SLionel Sambuc     if (ret)
698ebfedea0SLionel Sambuc 	goto out;
699ebfedea0SLionel Sambuc 
700ebfedea0SLionel Sambuc     /* check client referral and save principal */
701ebfedea0SLionel Sambuc     /* anonymous here ? */
702ebfedea0SLionel Sambuc     if((flags & EXTRACT_TICKET_ALLOW_CNAME_MISMATCH) == 0) {
703ebfedea0SLionel Sambuc 	ret = check_client_referral(context, rep,
704ebfedea0SLionel Sambuc 				    creds->client,
705ebfedea0SLionel Sambuc 				    tmp_principal,
706ebfedea0SLionel Sambuc 				    &creds->session);
707ebfedea0SLionel Sambuc 	if (ret) {
708ebfedea0SLionel Sambuc 	    krb5_free_principal (context, tmp_principal);
709ebfedea0SLionel Sambuc 	    goto out;
710ebfedea0SLionel Sambuc 	}
711ebfedea0SLionel Sambuc     }
712ebfedea0SLionel Sambuc     krb5_free_principal (context, creds->client);
713ebfedea0SLionel Sambuc     creds->client = tmp_principal;
714ebfedea0SLionel Sambuc 
715ebfedea0SLionel Sambuc     /* check server referral and save principal */
716ebfedea0SLionel Sambuc     ret = _krb5_principalname2krb5_principal (context,
717ebfedea0SLionel Sambuc 					      &tmp_principal,
718ebfedea0SLionel Sambuc 					      rep->kdc_rep.ticket.sname,
719ebfedea0SLionel Sambuc 					      rep->kdc_rep.ticket.realm);
720ebfedea0SLionel Sambuc     if (ret)
721ebfedea0SLionel Sambuc 	goto out;
722ebfedea0SLionel Sambuc     if((flags & EXTRACT_TICKET_ALLOW_SERVER_MISMATCH) == 0){
723ebfedea0SLionel Sambuc 	ret = check_server_referral(context,
724ebfedea0SLionel Sambuc 				    rep,
725ebfedea0SLionel Sambuc 				    flags,
726ebfedea0SLionel Sambuc 				    creds->server,
727ebfedea0SLionel Sambuc 				    tmp_principal,
728ebfedea0SLionel Sambuc 				    &creds->session);
729ebfedea0SLionel Sambuc 	if (ret) {
730ebfedea0SLionel Sambuc 	    krb5_free_principal (context, tmp_principal);
731ebfedea0SLionel Sambuc 	    goto out;
732ebfedea0SLionel Sambuc 	}
733ebfedea0SLionel Sambuc     }
734ebfedea0SLionel Sambuc     krb5_free_principal(context, creds->server);
735ebfedea0SLionel Sambuc     creds->server = tmp_principal;
736ebfedea0SLionel Sambuc 
737ebfedea0SLionel Sambuc     /* verify names */
738ebfedea0SLionel Sambuc     if(flags & EXTRACT_TICKET_MATCH_REALM){
739ebfedea0SLionel Sambuc 	const char *srealm = krb5_principal_get_realm(context, creds->server);
740ebfedea0SLionel Sambuc 	const char *crealm = krb5_principal_get_realm(context, creds->client);
741ebfedea0SLionel Sambuc 
742ebfedea0SLionel Sambuc 	if (strcmp(rep->enc_part.srealm, srealm) != 0 ||
743ebfedea0SLionel Sambuc 	    strcmp(rep->enc_part.srealm, crealm) != 0)
744ebfedea0SLionel Sambuc 	{
745ebfedea0SLionel Sambuc 	    ret = KRB5KRB_AP_ERR_MODIFIED;
746ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
747ebfedea0SLionel Sambuc 	    goto out;
748ebfedea0SLionel Sambuc 	}
749ebfedea0SLionel Sambuc     }
750ebfedea0SLionel Sambuc 
751ebfedea0SLionel Sambuc     /* compare nonces */
752ebfedea0SLionel Sambuc 
753*0a6a1f1dSLionel Sambuc     if (nonce != (unsigned)rep->enc_part.nonce) {
754ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_MODIFIED;
755ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
756ebfedea0SLionel Sambuc 	goto out;
757ebfedea0SLionel Sambuc     }
758ebfedea0SLionel Sambuc 
759ebfedea0SLionel Sambuc     /* set kdc-offset */
760ebfedea0SLionel Sambuc 
761ebfedea0SLionel Sambuc     krb5_timeofday (context, &sec_now);
762ebfedea0SLionel Sambuc     if (rep->enc_part.flags.initial
763ebfedea0SLionel Sambuc 	&& (flags & EXTRACT_TICKET_TIMESYNC)
764ebfedea0SLionel Sambuc 	&& context->kdc_sec_offset == 0
765ebfedea0SLionel Sambuc 	&& krb5_config_get_bool (context, NULL,
766ebfedea0SLionel Sambuc 				 "libdefaults",
767ebfedea0SLionel Sambuc 				 "kdc_timesync",
768ebfedea0SLionel Sambuc 				 NULL)) {
769ebfedea0SLionel Sambuc 	context->kdc_sec_offset = rep->enc_part.authtime - sec_now;
770ebfedea0SLionel Sambuc 	krb5_timeofday (context, &sec_now);
771ebfedea0SLionel Sambuc     }
772ebfedea0SLionel Sambuc 
773ebfedea0SLionel Sambuc     /* check all times */
774ebfedea0SLionel Sambuc 
775ebfedea0SLionel Sambuc     if (rep->enc_part.starttime) {
776ebfedea0SLionel Sambuc 	tmp_time = *rep->enc_part.starttime;
777ebfedea0SLionel Sambuc     } else
778ebfedea0SLionel Sambuc 	tmp_time = rep->enc_part.authtime;
779ebfedea0SLionel Sambuc 
780ebfedea0SLionel Sambuc     if (creds->times.starttime == 0
781ebfedea0SLionel Sambuc 	&& abs(tmp_time - sec_now) > context->max_skew) {
782ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_SKEW;
783ebfedea0SLionel Sambuc 	krb5_set_error_message (context, ret,
784ebfedea0SLionel Sambuc 				N_("time skew (%d) larger than max (%d)", ""),
785ebfedea0SLionel Sambuc 			       abs(tmp_time - sec_now),
786ebfedea0SLionel Sambuc 			       (int)context->max_skew);
787ebfedea0SLionel Sambuc 	goto out;
788ebfedea0SLionel Sambuc     }
789ebfedea0SLionel Sambuc 
790ebfedea0SLionel Sambuc     if (creds->times.starttime != 0
791ebfedea0SLionel Sambuc 	&& tmp_time != creds->times.starttime) {
792ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
793ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_MODIFIED;
794ebfedea0SLionel Sambuc 	goto out;
795ebfedea0SLionel Sambuc     }
796ebfedea0SLionel Sambuc 
797ebfedea0SLionel Sambuc     creds->times.starttime = tmp_time;
798ebfedea0SLionel Sambuc 
799ebfedea0SLionel Sambuc     if (rep->enc_part.renew_till) {
800ebfedea0SLionel Sambuc 	tmp_time = *rep->enc_part.renew_till;
801ebfedea0SLionel Sambuc     } else
802ebfedea0SLionel Sambuc 	tmp_time = 0;
803ebfedea0SLionel Sambuc 
804ebfedea0SLionel Sambuc     if (creds->times.renew_till != 0
805ebfedea0SLionel Sambuc 	&& tmp_time > creds->times.renew_till) {
806ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
807ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_MODIFIED;
808ebfedea0SLionel Sambuc 	goto out;
809ebfedea0SLionel Sambuc     }
810ebfedea0SLionel Sambuc 
811ebfedea0SLionel Sambuc     creds->times.renew_till = tmp_time;
812ebfedea0SLionel Sambuc 
813ebfedea0SLionel Sambuc     creds->times.authtime = rep->enc_part.authtime;
814ebfedea0SLionel Sambuc 
815ebfedea0SLionel Sambuc     if (creds->times.endtime != 0
816ebfedea0SLionel Sambuc 	&& rep->enc_part.endtime > creds->times.endtime) {
817ebfedea0SLionel Sambuc 	krb5_clear_error_message (context);
818ebfedea0SLionel Sambuc 	ret = KRB5KRB_AP_ERR_MODIFIED;
819ebfedea0SLionel Sambuc 	goto out;
820ebfedea0SLionel Sambuc     }
821ebfedea0SLionel Sambuc 
822ebfedea0SLionel Sambuc     creds->times.endtime  = rep->enc_part.endtime;
823ebfedea0SLionel Sambuc 
824ebfedea0SLionel Sambuc     if(rep->enc_part.caddr)
825ebfedea0SLionel Sambuc 	krb5_copy_addresses (context, rep->enc_part.caddr, &creds->addresses);
826ebfedea0SLionel Sambuc     else if(addrs)
827ebfedea0SLionel Sambuc 	krb5_copy_addresses (context, addrs, &creds->addresses);
828ebfedea0SLionel Sambuc     else {
829ebfedea0SLionel Sambuc 	creds->addresses.len = 0;
830ebfedea0SLionel Sambuc 	creds->addresses.val = NULL;
831ebfedea0SLionel Sambuc     }
832ebfedea0SLionel Sambuc     creds->flags.b = rep->enc_part.flags;
833ebfedea0SLionel Sambuc 
834ebfedea0SLionel Sambuc     creds->authdata.len = 0;
835ebfedea0SLionel Sambuc     creds->authdata.val = NULL;
836ebfedea0SLionel Sambuc 
837ebfedea0SLionel Sambuc     /* extract ticket */
838ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
839ebfedea0SLionel Sambuc 		       &rep->kdc_rep.ticket, &len, ret);
840ebfedea0SLionel Sambuc     if(ret)
841ebfedea0SLionel Sambuc 	goto out;
842ebfedea0SLionel Sambuc     if (creds->ticket.length != len)
843ebfedea0SLionel Sambuc 	krb5_abortx(context, "internal error in ASN.1 encoder");
844ebfedea0SLionel Sambuc     creds->second_ticket.length = 0;
845ebfedea0SLionel Sambuc     creds->second_ticket.data   = NULL;
846ebfedea0SLionel Sambuc 
847ebfedea0SLionel Sambuc 
848ebfedea0SLionel Sambuc out:
849ebfedea0SLionel Sambuc     memset (rep->enc_part.key.keyvalue.data, 0,
850ebfedea0SLionel Sambuc 	    rep->enc_part.key.keyvalue.length);
851ebfedea0SLionel Sambuc     return ret;
852ebfedea0SLionel Sambuc }
853