2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
38 krb5_error_code KRB5_LIB_FUNCTION
39 krb5_free_ticket(krb5_context context,
42 free_EncTicketPart(&ticket->ticket);
43 krb5_free_principal(context, ticket->client);
44 krb5_free_principal(context, ticket->server);
49 krb5_error_code KRB5_LIB_FUNCTION
50 krb5_copy_ticket(krb5_context context,
51 const krb5_ticket *from,
58 tmp = malloc(sizeof(*tmp));
60 krb5_set_error_message(context, ENOMEM,
61 N_("malloc: out of memory", ""));
64 if((ret = copy_EncTicketPart(&from->ticket, &tmp->ticket))){
68 ret = krb5_copy_principal(context, from->client, &tmp->client);
70 free_EncTicketPart(&tmp->ticket);
74 ret = krb5_copy_principal(context, from->server, &tmp->server);
76 krb5_free_principal(context, tmp->client);
77 free_EncTicketPart(&tmp->ticket);
85 krb5_error_code KRB5_LIB_FUNCTION
86 krb5_ticket_get_client(krb5_context context,
87 const krb5_ticket *ticket,
88 krb5_principal *client)
90 return krb5_copy_principal(context, ticket->client, client);
93 krb5_error_code KRB5_LIB_FUNCTION
94 krb5_ticket_get_server(krb5_context context,
95 const krb5_ticket *ticket,
96 krb5_principal *server)
98 return krb5_copy_principal(context, ticket->server, server);
101 time_t KRB5_LIB_FUNCTION
102 krb5_ticket_get_endtime(krb5_context context,
103 const krb5_ticket *ticket)
105 return ticket->ticket.endtime;
109 * Get the flags from the Kerberos ticket
111 * @param context Kerberos context
112 * @param ticket Kerberos ticket
114 * @return ticket flags
116 * @ingroup krb5_ticket
119 krb5_ticket_get_flags(krb5_context context,
120 const krb5_ticket *ticket)
122 return TicketFlags2int(ticket->ticket.flags);
126 find_type_in_ad(krb5_context context,
131 krb5_keyblock *sessionkey,
132 const AuthorizationData *ad,
135 krb5_error_code ret = 0;
139 ret = ENOENT; /* XXX */
140 krb5_set_error_message(context, ret,
141 N_("Authorization data nested deeper "
142 "then %d levels, stop searching", ""),
148 * Only copy out the element the first time we get to it, we need
149 * to run over the whole authorization data fields to check if
150 * there are any container clases we need to care about.
152 for (i = 0; i < ad->len; i++) {
153 if (!*found && ad->val[i].ad_type == type) {
154 ret = der_copy_octet_string(&ad->val[i].ad_data, data);
156 krb5_set_error_message(context, ret,
157 N_("malloc: out of memory", ""));
163 switch (ad->val[i].ad_type) {
164 case KRB5_AUTHDATA_IF_RELEVANT: {
165 AuthorizationData child;
166 ret = decode_AuthorizationData(ad->val[i].ad_data.data,
167 ad->val[i].ad_data.length,
171 krb5_set_error_message(context, ret,
172 N_("Failed to decode "
173 "IF_RELEVANT with %d", ""),
177 ret = find_type_in_ad(context, type, data, found, FALSE,
178 sessionkey, &child, level + 1);
179 free_AuthorizationData(&child);
185 case KRB5_AUTHDATA_KDC_ISSUED: {
188 ret = decode_AD_KDCIssued(ad->val[i].ad_data.data,
189 ad->val[i].ad_data.length,
193 krb5_set_error_message(context, ret,
194 N_("Failed to decode "
195 "AD_KDCIssued with %d", ""),
204 ASN1_MALLOC_ENCODE(AuthorizationData, buf.data, buf.length,
205 &child.elements, &len, ret);
207 free_AD_KDCIssued(&child);
208 krb5_clear_error_message(context);
211 if(buf.length != len)
212 krb5_abortx(context, "internal error in ASN.1 encoder");
214 ret = krb5_c_verify_checksum(context, sessionkey, 19, &buf,
215 &child.ad_checksum, &valid);
216 krb5_data_free(&buf);
218 free_AD_KDCIssued(&child);
222 krb5_clear_error_message(context);
224 free_AD_KDCIssued(&child);
228 ret = find_type_in_ad(context, type, data, found, failp, sessionkey,
229 &child.elements, level + 1);
230 free_AD_KDCIssued(&child);
236 case KRB5_AUTHDATA_AND_OR:
239 ret = ENOENT; /* XXX */
240 krb5_set_error_message(context, ret,
241 N_("Authorization data contains "
242 "AND-OR element that is unknown to the "
248 ret = ENOENT; /* XXX */
249 krb5_set_error_message(context, ret,
250 N_("Authorization data contains "
251 "unknown type (%d) ", ""),
259 krb5_data_free(data);
267 * Extract the authorization data type of `type' from the
268 * 'ticket'. Store the field in `data'. This function is to use for
269 * kerberos applications.
272 krb5_error_code KRB5_LIB_FUNCTION
273 krb5_ticket_get_authorization_data_type(krb5_context context,
278 AuthorizationData *ad;
280 krb5_boolean found = FALSE;
282 krb5_data_zero(data);
284 ad = ticket->ticket.authorization_data;
285 if (ticket->ticket.authorization_data == NULL) {
286 krb5_set_error_message(context, ENOENT,
287 N_("Ticket have not authorization data", ""));
288 return ENOENT; /* XXX */
291 ret = find_type_in_ad(context, type, data, &found, TRUE,
292 &ticket->ticket.key, ad, 0);
296 krb5_set_error_message(context, ENOENT,
297 N_("Ticket have not "
298 "authorization data of type %d", ""),
300 return ENOENT; /* XXX */