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"
36 __RCSID("$Heimdal: ticket.c 19544 2006-12-28 20:49:18Z lha $"
39 krb5_error_code KRB5_LIB_FUNCTION
40 krb5_free_ticket(krb5_context context
,
43 free_EncTicketPart(&ticket
->ticket
);
44 krb5_free_principal(context
, ticket
->client
);
45 krb5_free_principal(context
, ticket
->server
);
50 krb5_error_code KRB5_LIB_FUNCTION
51 krb5_copy_ticket(krb5_context context
,
52 const krb5_ticket
*from
,
59 tmp
= malloc(sizeof(*tmp
));
61 krb5_set_error_string (context
, "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 find_type_in_ad(krb5_context context
,
114 krb5_keyblock
*sessionkey
,
115 const AuthorizationData
*ad
,
118 krb5_error_code ret
= 0;
122 krb5_set_error_string(context
, "Authorization data nested deeper "
123 "then %d levels, stop searching", level
);
124 ret
= ENOENT
; /* XXX */
129 * Only copy out the element the first time we get to it, we need
130 * to run over the whole authorization data fields to check if
131 * there are any container clases we need to care about.
133 for (i
= 0; i
< ad
->len
; i
++) {
134 if (!*found
&& ad
->val
[i
].ad_type
== type
) {
135 ret
= der_copy_octet_string(&ad
->val
[i
].ad_data
, data
);
137 krb5_set_error_string(context
, "malloc - out of memory");
143 switch (ad
->val
[i
].ad_type
) {
144 case KRB5_AUTHDATA_IF_RELEVANT
: {
145 AuthorizationData child
;
146 ret
= decode_AuthorizationData(ad
->val
[i
].ad_data
.data
,
147 ad
->val
[i
].ad_data
.length
,
151 krb5_set_error_string(context
, "Failed to decode "
152 "IF_RELEVANT with %d", ret
);
155 ret
= find_type_in_ad(context
, type
, data
, found
, FALSE
,
156 sessionkey
, &child
, level
+ 1);
157 free_AuthorizationData(&child
);
163 case KRB5_AUTHDATA_KDC_ISSUED
: {
166 ret
= decode_AD_KDCIssued(ad
->val
[i
].ad_data
.data
,
167 ad
->val
[i
].ad_data
.length
,
171 krb5_set_error_string(context
, "Failed to decode "
172 "AD_KDCIssued with %d", ret
);
180 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
.data
, buf
.length
,
181 &child
.elements
, &len
, ret
);
183 free_AD_KDCIssued(&child
);
184 krb5_clear_error_string(context
);
187 if(buf
.length
!= len
)
188 krb5_abortx(context
, "internal error in ASN.1 encoder");
190 ret
= krb5_c_verify_checksum(context
, sessionkey
, 19, &buf
,
191 &child
.ad_checksum
, &valid
);
192 krb5_data_free(&buf
);
194 free_AD_KDCIssued(&child
);
198 krb5_clear_error_string(context
);
200 free_AD_KDCIssued(&child
);
204 ret
= find_type_in_ad(context
, type
, data
, found
, failp
, sessionkey
,
205 &child
.elements
, level
+ 1);
206 free_AD_KDCIssued(&child
);
212 case KRB5_AUTHDATA_AND_OR
:
215 krb5_set_error_string(context
, "Authorization data contains "
216 "AND-OR element that is unknown to the "
218 ret
= ENOENT
; /* XXX */
223 krb5_set_error_string(context
, "Authorization data contains "
224 "unknown type (%d) ", ad
->val
[i
].ad_type
);
225 ret
= ENOENT
; /* XXX */
232 krb5_data_free(data
);
240 * Extract the authorization data type of `type' from the
241 * 'ticket'. Store the field in `data'. This function is to use for
242 * kerberos applications.
245 krb5_error_code KRB5_LIB_FUNCTION
246 krb5_ticket_get_authorization_data_type(krb5_context context
,
251 AuthorizationData
*ad
;
253 krb5_boolean found
= FALSE
;
255 krb5_data_zero(data
);
257 ad
= ticket
->ticket
.authorization_data
;
258 if (ticket
->ticket
.authorization_data
== NULL
) {
259 krb5_set_error_string(context
, "Ticket have not authorization data");
260 return ENOENT
; /* XXX */
263 ret
= find_type_in_ad(context
, type
, data
, &found
, TRUE
,
264 &ticket
->ticket
.key
, ad
, 0);
268 krb5_set_error_string(context
, "Ticket have not authorization "
269 "data of type %d", type
);
270 return ENOENT
; /* XXX */