1 /* $NetBSD: rd_cred.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
4 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 static krb5_error_code
39 compare_addrs(krb5_context context
,
44 char a_str
[64], b_str
[64];
47 if(krb5_address_compare (context
, a
, b
))
50 krb5_print_address (a
, a_str
, sizeof(a_str
), &len
);
51 krb5_print_address (b
, b_str
, sizeof(b_str
), &len
);
52 krb5_set_error_message(context
, KRB5KRB_AP_ERR_BADADDR
,
53 "%s: %s != %s", message
, b_str
, a_str
);
54 return KRB5KRB_AP_ERR_BADADDR
;
57 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
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
));
73 krb5_data_zero(&enc_krb_cred_part_data
);
75 if ((auth_context
->flags
&
76 (KRB5_AUTH_CONTEXT_RET_TIME
| KRB5_AUTH_CONTEXT_RET_SEQUENCE
)) &&
78 return KRB5_RC_REQUIRED
; /* XXX better error, MIT returns this */
82 ret
= decode_KRB_CRED(in_data
->data
, in_data
->length
,
85 krb5_clear_error_message(context
);
90 ret
= KRB5KRB_AP_ERR_BADVERSION
;
91 krb5_clear_error_message (context
);
95 if (cred
.msg_type
!= krb_cred
) {
96 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
97 krb5_clear_error_message (context
);
101 if (cred
.enc_part
.etype
== ETYPE_NULL
) {
102 /* DK: MIT GSS-API Compatibility */
103 enc_krb_cred_part_data
.length
= cred
.enc_part
.cipher
.length
;
104 enc_krb_cred_part_data
.data
= cred
.enc_part
.cipher
.data
;
106 /* Try both subkey and session key.
108 * RFC4120 claims we should use the session key, but Heimdal
109 * before 0.8 used the remote subkey if it was send in the
113 if (auth_context
->remote_subkey
) {
114 ret
= krb5_crypto_init(context
, auth_context
->remote_subkey
,
119 ret
= krb5_decrypt_EncryptedData(context
,
123 &enc_krb_cred_part_data
);
125 krb5_crypto_destroy(context
, crypto
);
129 * If there was not subkey, or we failed using subkey,
130 * retry using the session key
132 if (auth_context
->remote_subkey
== NULL
|| ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
)
135 ret
= krb5_crypto_init(context
, auth_context
->keyblock
,
141 ret
= krb5_decrypt_EncryptedData(context
,
145 &enc_krb_cred_part_data
);
147 krb5_crypto_destroy(context
, crypto
);
153 ret
= decode_EncKrbCredPart(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
);
160 krb5_set_error_message(context
, ret
,
161 N_("Failed to decode "
162 "encrypte credential part", ""));
166 /* check sender address */
168 if (enc_krb_cred_part
.s_address
169 && auth_context
->remote_address
170 && auth_context
->remote_port
) {
173 ret
= krb5_make_addrport (context
, &a
,
174 auth_context
->remote_address
,
175 auth_context
->remote_port
);
180 ret
= compare_addrs(context
, a
, enc_krb_cred_part
.s_address
,
181 N_("sender address is wrong "
182 "in received creds", ""));
183 krb5_free_address(context
, a
);
189 /* check receiver address */
191 if (enc_krb_cred_part
.r_address
192 && auth_context
->local_address
) {
193 if(auth_context
->local_port
&&
194 enc_krb_cred_part
.r_address
->addr_type
== KRB5_ADDRESS_ADDRPORT
) {
196 ret
= krb5_make_addrport (context
, &a
,
197 auth_context
->local_address
,
198 auth_context
->local_port
);
202 ret
= compare_addrs(context
, a
, enc_krb_cred_part
.r_address
,
203 N_("receiver address is wrong "
204 "in received creds", ""));
205 krb5_free_address(context
, a
);
210 ret
= compare_addrs(context
, auth_context
->local_address
,
211 enc_krb_cred_part
.r_address
,
212 N_("receiver address is wrong "
213 "in received creds", ""));
219 /* check timestamp */
220 if (auth_context
->flags
& KRB5_AUTH_CONTEXT_DO_TIME
) {
223 krb5_timeofday (context
, &sec
);
225 if (enc_krb_cred_part
.timestamp
== NULL
||
226 enc_krb_cred_part
.usec
== NULL
||
227 abs(*enc_krb_cred_part
.timestamp
- sec
)
228 > context
->max_skew
) {
229 krb5_clear_error_message (context
);
230 ret
= KRB5KRB_AP_ERR_SKEW
;
235 if ((auth_context
->flags
&
236 (KRB5_AUTH_CONTEXT_RET_TIME
| KRB5_AUTH_CONTEXT_RET_SEQUENCE
))) {
237 /* if these fields are not present in the cred-part, silently
239 memset(outdata
, 0, sizeof(*outdata
));
240 if(enc_krb_cred_part
.timestamp
)
241 outdata
->timestamp
= *enc_krb_cred_part
.timestamp
;
242 if(enc_krb_cred_part
.usec
)
243 outdata
->usec
= *enc_krb_cred_part
.usec
;
244 if(enc_krb_cred_part
.nonce
)
245 outdata
->seq
= *enc_krb_cred_part
.nonce
;
248 /* Convert to NULL terminated list of creds */
250 *ret_creds
= calloc(enc_krb_cred_part
.ticket_info
.len
+ 1,
251 sizeof(**ret_creds
));
253 if (*ret_creds
== NULL
) {
255 krb5_set_error_message(context
, ret
,
256 N_("malloc: out of memory", ""));
260 for (i
= 0; i
< enc_krb_cred_part
.ticket_info
.len
; ++i
) {
261 KrbCredInfo
*kci
= &enc_krb_cred_part
.ticket_info
.val
[i
];
264 creds
= calloc(1, sizeof(*creds
));
267 krb5_set_error_message(context
, ret
,
268 N_("malloc: out of memory", ""));
272 ASN1_MALLOC_ENCODE(Ticket
, creds
->ticket
.data
, creds
->ticket
.length
,
273 &cred
.tickets
.val
[i
], &len
, ret
);
278 if(creds
->ticket
.length
!= len
)
279 krb5_abortx(context
, "internal error in ASN.1 encoder");
280 copy_EncryptionKey (&kci
->key
, &creds
->session
);
281 if (kci
->prealm
&& kci
->pname
)
282 _krb5_principalname2krb5_principal (context
,
287 creds
->flags
.b
= *kci
->flags
;
289 creds
->times
.authtime
= *kci
->authtime
;
291 creds
->times
.starttime
= *kci
->starttime
;
293 creds
->times
.endtime
= *kci
->endtime
;
295 creds
->times
.renew_till
= *kci
->renew_till
;
296 if (kci
->srealm
&& kci
->sname
)
297 _krb5_principalname2krb5_principal (context
,
302 krb5_copy_addresses (context
,
306 (*ret_creds
)[i
] = creds
;
309 (*ret_creds
)[i
] = NULL
;
311 free_KRB_CRED (&cred
);
312 free_EncKrbCredPart(&enc_krb_cred_part
);
317 free_EncKrbCredPart(&enc_krb_cred_part
);
318 free_KRB_CRED (&cred
);
320 for(i
= 0; (*ret_creds
)[i
]; i
++)
321 krb5_free_creds(context
, (*ret_creds
)[i
]);
328 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
329 krb5_rd_cred2 (krb5_context context
,
330 krb5_auth_context auth_context
,
338 ret
= krb5_rd_cred(context
, auth_context
, in_data
, &creds
, NULL
);
342 /* Store the creds in the ccache */
344 for(i
= 0; creds
&& creds
[i
]; i
++) {
345 krb5_cc_store_cred(context
, ccache
, creds
[i
]);
346 krb5_free_creds(context
, creds
[i
]);