2 * Copyright (c) 1997 - 2007 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: rd_cred.c 20304 2007-04-11 11:15:05Z lha $"
39 static krb5_error_code
40 compare_addrs(krb5_context context
,
45 char a_str
[64], b_str
[64];
48 if(krb5_address_compare (context
, a
, b
))
51 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
52 krb5_print_address (b
, b_str
, sizeof(b_str
), &len
);
53 krb5_set_error_string(context
, "%s: %s != %s", message
, b_str
, a_str
);
54 return KRB5KRB_AP_ERR_BADADDR
;
57 krb5_error_code KRB5_LIB_FUNCTION
58 krb5_rd_cred(krb5_context context
,
59 krb5_auth_context auth_context
,
61 krb5_creds
***ret_creds
,
62 krb5_replay_data
*outdata
)
67 EncKrbCredPart enc_krb_cred_part
;
68 krb5_data enc_krb_cred_part_data
;
72 memset(&enc_krb_cred_part
, 0, sizeof(enc_krb_cred_part
));
74 if ((auth_context
->flags
&
75 (KRB5_AUTH_CONTEXT_RET_TIME
| KRB5_AUTH_CONTEXT_RET_SEQUENCE
)) &&
77 return KRB5_RC_REQUIRED
; /* XXX better error, MIT returns this */
81 ret
= decode_KRB_CRED(in_data
->data
, in_data
->length
,
84 krb5_clear_error_string(context
);
89 ret
= KRB5KRB_AP_ERR_BADVERSION
;
90 krb5_clear_error_string (context
);
94 if (cred
.msg_type
!= krb_cred
) {
95 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
96 krb5_clear_error_string (context
);
100 if (cred
.enc_part
.etype
== ETYPE_NULL
) {
101 /* DK: MIT GSS-API Compatibility */
102 enc_krb_cred_part_data
.length
= cred
.enc_part
.cipher
.length
;
103 enc_krb_cred_part_data
.data
= cred
.enc_part
.cipher
.data
;
105 /* Try both subkey and session key.
107 * RFC4120 claims we should use the session key, but Heimdal
108 * before 0.8 used the remote subkey if it was send in the
112 if (auth_context
->remote_subkey
) {
113 ret
= krb5_crypto_init(context
, auth_context
->remote_subkey
,
118 ret
= krb5_decrypt_EncryptedData(context
,
122 &enc_krb_cred_part_data
);
124 krb5_crypto_destroy(context
, crypto
);
128 * If there was not subkey, or we failed using subkey,
129 * retry using the session key
131 if (auth_context
->remote_subkey
== NULL
|| ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
)
134 ret
= krb5_crypto_init(context
, auth_context
->keyblock
,
140 ret
= krb5_decrypt_EncryptedData(context
,
144 &enc_krb_cred_part_data
);
146 krb5_crypto_destroy(context
, crypto
);
152 ret
= krb5_decode_EncKrbCredPart (context
,
153 enc_krb_cred_part_data
.data
,
154 enc_krb_cred_part_data
.length
,
157 if (enc_krb_cred_part_data
.data
!= cred
.enc_part
.cipher
.data
)
158 krb5_data_free(&enc_krb_cred_part_data
);
162 /* check sender address */
164 if (enc_krb_cred_part
.s_address
165 && auth_context
->remote_address
166 && auth_context
->remote_port
) {
169 ret
= krb5_make_addrport (context
, &a
,
170 auth_context
->remote_address
,
171 auth_context
->remote_port
);
176 ret
= compare_addrs(context
, a
, enc_krb_cred_part
.s_address
,
177 "sender address is wrong in received creds");
178 krb5_free_address(context
, a
);
184 /* check receiver address */
186 if (enc_krb_cred_part
.r_address
187 && auth_context
->local_address
) {
188 if(auth_context
->local_port
&&
189 enc_krb_cred_part
.r_address
->addr_type
== KRB5_ADDRESS_ADDRPORT
) {
191 ret
= krb5_make_addrport (context
, &a
,
192 auth_context
->local_address
,
193 auth_context
->local_port
);
197 ret
= compare_addrs(context
, a
, enc_krb_cred_part
.r_address
,
198 "receiver address is wrong in received creds");
199 krb5_free_address(context
, a
);
204 ret
= compare_addrs(context
, auth_context
->local_address
,
205 enc_krb_cred_part
.r_address
,
206 "receiver address is wrong in received creds");
212 /* check timestamp */
213 if (auth_context
->flags
& KRB5_AUTH_CONTEXT_DO_TIME
) {
216 krb5_timeofday (context
, &sec
);
218 if (enc_krb_cred_part
.timestamp
== NULL
||
219 enc_krb_cred_part
.usec
== NULL
||
220 abs(*enc_krb_cred_part
.timestamp
- sec
)
221 > context
->max_skew
) {
222 krb5_clear_error_string (context
);
223 ret
= KRB5KRB_AP_ERR_SKEW
;
228 if ((auth_context
->flags
&
229 (KRB5_AUTH_CONTEXT_RET_TIME
| KRB5_AUTH_CONTEXT_RET_SEQUENCE
))) {
230 /* if these fields are not present in the cred-part, silently
232 memset(outdata
, 0, sizeof(*outdata
));
233 if(enc_krb_cred_part
.timestamp
)
234 outdata
->timestamp
= *enc_krb_cred_part
.timestamp
;
235 if(enc_krb_cred_part
.usec
)
236 outdata
->usec
= *enc_krb_cred_part
.usec
;
237 if(enc_krb_cred_part
.nonce
)
238 outdata
->seq
= *enc_krb_cred_part
.nonce
;
241 /* Convert to NULL terminated list of creds */
243 *ret_creds
= calloc(enc_krb_cred_part
.ticket_info
.len
+ 1,
244 sizeof(**ret_creds
));
246 if (*ret_creds
== NULL
) {
248 krb5_set_error_string (context
, "malloc: out of memory");
252 for (i
= 0; i
< enc_krb_cred_part
.ticket_info
.len
; ++i
) {
253 KrbCredInfo
*kci
= &enc_krb_cred_part
.ticket_info
.val
[i
];
256 creds
= calloc(1, sizeof(*creds
));
259 krb5_set_error_string (context
, "malloc: out of memory");
263 ASN1_MALLOC_ENCODE(Ticket
, creds
->ticket
.data
, creds
->ticket
.length
,
264 &cred
.tickets
.val
[i
], &len
, ret
);
269 if(creds
->ticket
.length
!= len
)
270 krb5_abortx(context
, "internal error in ASN.1 encoder");
271 copy_EncryptionKey (&kci
->key
, &creds
->session
);
272 if (kci
->prealm
&& kci
->pname
)
273 _krb5_principalname2krb5_principal (context
,
278 creds
->flags
.b
= *kci
->flags
;
280 creds
->times
.authtime
= *kci
->authtime
;
282 creds
->times
.starttime
= *kci
->starttime
;
284 creds
->times
.endtime
= *kci
->endtime
;
286 creds
->times
.renew_till
= *kci
->renew_till
;
287 if (kci
->srealm
&& kci
->sname
)
288 _krb5_principalname2krb5_principal (context
,
293 krb5_copy_addresses (context
,
297 (*ret_creds
)[i
] = creds
;
300 (*ret_creds
)[i
] = NULL
;
302 free_KRB_CRED (&cred
);
303 free_EncKrbCredPart(&enc_krb_cred_part
);
308 free_EncKrbCredPart(&enc_krb_cred_part
);
309 free_KRB_CRED (&cred
);
311 for(i
= 0; (*ret_creds
)[i
]; i
++)
312 krb5_free_creds(context
, (*ret_creds
)[i
]);
319 krb5_error_code KRB5_LIB_FUNCTION
320 krb5_rd_cred2 (krb5_context context
,
321 krb5_auth_context auth_context
,
329 ret
= krb5_rd_cred(context
, auth_context
, in_data
, &creds
, NULL
);
333 /* Store the creds in the ccache */
335 for(i
= 0; creds
&& creds
[i
]; i
++) {
336 krb5_cc_store_cred(context
, ccache
, creds
[i
]);
337 krb5_free_creds(context
, creds
[i
]);