2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "krb5_locl.h"
44 krb5_init_etype (krb5_context context
,
47 const krb5_enctype
*etypes
)
55 tmp
= (krb5_enctype
*)etypes
;
57 ret
= krb5_get_default_in_tkt_etypes(context
,
63 for (i
= 0; tmp
[i
]; ++i
)
66 *val
= malloc(i
* sizeof(unsigned));
81 static krb5_error_code
82 decrypt_tkt (krb5_context context
,
83 const krb5_keyblock
*key
,
84 krb5_const_pointer decrypt_arg
,
85 krb5_kdc_rep
*dec_rep
)
91 ret
= krb5_decrypt (context
,
92 dec_rep
->part1
.enc_part
.cipher
.data
,
93 dec_rep
->part1
.enc_part
.cipher
.length
,
94 dec_rep
->part1
.enc_part
.etype
,
100 ret
= decode_EncASRepPart(data
.data
,
105 ret
= decode_EncTGSRepPart(data
.data
,
109 krb5_data_free (&data
);
115 _krb5_extract_ticket(krb5_context context
,
119 krb5_const_pointer keyseed
,
120 krb5_addresses
*addrs
,
122 krb5_decrypt_proc decrypt_proc
,
123 krb5_const_pointer decryptarg
)
126 krb5_principal tmp_principal
;
133 err
= principalname2krb5_principal (&tmp_principal
,
138 tmp
= krb5_principal_compare (context
, tmp_principal
, creds
->client
);
139 krb5_free_principal (context
, tmp_principal
);
141 err
= KRB5KRB_AP_ERR_MODIFIED
;
149 encode_Ticket(buf
+ sizeof(buf
) - 1, sizeof(buf
),
150 &rep
->part1
.ticket
, &len
);
151 creds
->ticket
.data
= malloc(len
);
152 memcpy(creds
->ticket
.data
, buf
+ sizeof(buf
) - len
, len
);
153 creds
->ticket
.length
= len
;
154 creds
->second_ticket
.length
= 0;
155 creds
->second_ticket
.data
= NULL
;
160 err
= principalname2krb5_principal (&tmp_principal
,
161 rep
->part1
.ticket
.sname
,
162 rep
->part1
.ticket
.realm
);
165 tmp
= krb5_principal_compare (context
, tmp_principal
, creds
->server
);
166 krb5_free_principal (context
, tmp_principal
);
168 err
= KRB5KRB_AP_ERR_MODIFIED
;
174 if (decrypt_proc
== NULL
)
175 decrypt_proc
= decrypt_tkt
;
177 err
= (*decrypt_proc
)(context
, key
, decryptarg
, rep
);
183 if (nonce
!= rep
->part2
.nonce
) {
184 err
= KRB5KRB_AP_ERR_MODIFIED
;
190 krb5_timeofday (context
, &sec_now
);
191 if (context
->kdc_sec_offset
== 0
192 && krb5_config_get_bool (context
->cf
,
196 context
->kdc_sec_offset
= rep
->part2
.authtime
- sec_now
;
197 krb5_timeofday (context
, &sec_now
);
200 /* check all times */
202 if (rep
->part2
.starttime
) {
203 tmp_time
= *rep
->part2
.starttime
;
205 tmp_time
= rep
->part2
.authtime
;
207 if (creds
->times
.starttime
== 0
208 && abs(tmp_time
- sec_now
) > context
->max_skew
) {
209 err
= KRB5KRB_AP_ERR_MODIFIED
;
213 if (creds
->times
.starttime
!= 0
214 && tmp_time
!= creds
->times
.starttime
) {
215 err
= KRB5KRB_AP_ERR_MODIFIED
;
219 creds
->times
.starttime
= tmp_time
;
221 if (rep
->part2
.renew_till
) {
222 tmp_time
= *rep
->part2
.renew_till
;
226 if (creds
->times
.renew_till
!= 0
227 && tmp_time
> creds
->times
.renew_till
) {
228 err
= KRB5KRB_AP_ERR_MODIFIED
;
232 creds
->times
.renew_till
= tmp_time
;
234 creds
->times
.authtime
= rep
->part2
.authtime
;
236 if (creds
->times
.endtime
!= 0
237 && rep
->part2
.endtime
> creds
->times
.endtime
) {
238 err
= KRB5KRB_AP_ERR_MODIFIED
;
242 creds
->times
.endtime
= rep
->part2
.endtime
;
245 krb5_copy_addresses (context
, rep
->part2
.caddr
, &creds
->addresses
);
247 krb5_copy_addresses (context
, addrs
, &creds
->addresses
);
249 creds
->addresses
.len
= 0;
250 creds
->addresses
.val
= NULL
;
252 creds
->flags
.b
= rep
->part2
.flags
;
254 creds
->authdata
.length
= 0;
255 creds
->authdata
.data
= NULL
;
256 creds
->session
.keyvalue
.length
= 0;
257 creds
->session
.keyvalue
.data
= NULL
;
258 creds
->session
.keytype
= rep
->part2
.key
.keytype
;
259 err
= krb5_data_copy (&creds
->session
.keyvalue
,
260 rep
->part2
.key
.keyvalue
.data
,
261 rep
->part2
.key
.keyvalue
.length
);
264 memset (rep
->part2
.key
.keyvalue
.data
, 0,
265 rep
->part2
.key
.keyvalue
.length
);
270 static krb5_error_code
271 make_pa_enc_timestamp(krb5_context context
, PA_DATA
*pa
, krb5_keyblock
*key
)
276 EncryptedData encdata
;
281 krb5_us_timeofday (context
, &sec
, &usec
);
286 ret
= encode_PA_ENC_TS_ENC(buf
+ sizeof(buf
) - 1,
294 * According to the spec this is the only encryption method
295 * that must be supported so it's the safest choice. On the
296 * other hand, old KDCs might not support it.
299 ret
= krb5_encrypt_EncryptedData(context
,
300 buf
+ sizeof(buf
) - len
,
309 ret
= encode_EncryptedData(buf
+ sizeof(buf
) - 1,
313 free_EncryptedData(&encdata
);
316 pa
->padata_type
= pa_enc_timestamp
;
317 pa
->padata_value
.length
= 0;
318 krb5_data_copy(&pa
->padata_value
,
319 buf
+ sizeof(buf
) - len
,
324 static krb5_error_code
325 init_as_req (krb5_context context
,
328 const krb5_addresses
*addrs
,
329 const krb5_enctype
*etypes
,
330 const krb5_preauthtype
*ptypes
,
331 krb5_key_proc key_proc
,
332 krb5_const_pointer keyseed
,
341 memset(a
, 0, sizeof(*a
));
344 a
->msg_type
= krb_as_req
;
345 a
->req_body
.kdc_options
= opts
.b
;
346 a
->req_body
.cname
= malloc(sizeof(*a
->req_body
.cname
));
347 if (a
->req_body
.cname
== NULL
) {
351 a
->req_body
.sname
= malloc(sizeof(*a
->req_body
.sname
));
352 if (a
->req_body
.sname
== NULL
) {
356 ret
= krb5_principal2principalname (a
->req_body
.cname
, creds
->client
);
359 ret
= krb5_principal2principalname (a
->req_body
.sname
, creds
->server
);
362 ret
= copy_Realm(&creds
->client
->realm
, &a
->req_body
.realm
);
366 if(creds
->times
.starttime
) {
367 a
->req_body
.from
= malloc(sizeof(*a
->req_body
.from
));
368 if (a
->req_body
.from
== NULL
) {
372 *a
->req_body
.from
= creds
->times
.starttime
;
374 if(creds
->times
.endtime
){
375 ALLOC(a
->req_body
.till
, 1);
376 *a
->req_body
.till
= creds
->times
.endtime
;
378 if(creds
->times
.renew_till
){
379 a
->req_body
.rtime
= malloc(sizeof(*a
->req_body
.rtime
));
380 if (a
->req_body
.rtime
== NULL
) {
384 *a
->req_body
.rtime
= creds
->times
.renew_till
;
386 a
->req_body
.nonce
= nonce
;
387 ret
= krb5_init_etype (context
,
388 &a
->req_body
.etype
.len
,
389 &a
->req_body
.etype
.val
,
394 etype
= a
->req_body
.etype
.val
[0]; /* XXX */
396 a
->req_body
.addresses
= malloc(sizeof(*a
->req_body
.addresses
));
397 if (a
->req_body
.addresses
== NULL
) {
403 ret
= krb5_copy_addresses(context
, addrs
, a
->req_body
.addresses
);
405 ret
= krb5_get_all_client_addrs (a
->req_body
.addresses
);
409 a
->req_body
.enc_authorization_data
= NULL
;
410 a
->req_body
.additional_tickets
= NULL
;
412 /* not sure this is the way to use `ptypes' */
413 if (ptypes
== NULL
|| *ptypes
== KRB5_PADATA_NONE
)
415 else if (*ptypes
== KRB5_PADATA_ENC_TIMESTAMP
) {
416 a
->padata
= malloc(sizeof(*a
->padata
));
417 if (a
->padata
== NULL
) {
423 a
->padata
->val
= calloc(a
->padata
->len
, sizeof(*a
->padata
->val
));
424 if (a
->padata
->val
== NULL
) {
429 /* make a v5 salted pa-data */
432 ret
= krb5_get_salt (creds
->client
, &salt
);
437 ret
= (*key_proc
)(context
, etype
, &salt
,
439 krb5_data_free (&salt
);
442 ret
= make_pa_enc_timestamp(context
, &a
->padata
->val
[0], key
);
443 krb5_free_keyblock (context
, key
);
447 /* make a v4 salted pa-data */
450 ret
= (*key_proc
)(context
, etype
, &salt
,
454 ret
= make_pa_enc_timestamp(context
, &a
->padata
->val
[1], key
);
455 krb5_free_keyblock (context
, key
);
460 ret
= KRB5_PREAUTH_BAD_TYPE
;
470 krb5_get_in_cred(krb5_context context
,
472 const krb5_addresses
*addrs
,
473 const krb5_enctype
*etypes
,
474 const krb5_preauthtype
*ptypes
,
475 krb5_key_proc key_proc
,
476 krb5_const_pointer keyseed
,
477 krb5_decrypt_proc decrypt_proc
,
478 krb5_const_pointer decryptarg
,
480 krb5_kdc_rep
*ret_as_reply
)
497 krb5_generate_random_block (&nonce
, sizeof(nonce
));
500 ret
= init_as_req (context
,
513 etype
= a
.req_body
.etype
.val
[0]; /* XXX */
515 ret
= encode_AS_REQ ((unsigned char*)buf
+ sizeof(buf
) - 1,
523 req
.data
= buf
+ sizeof(buf
) - req
.length
;
525 ret
= krb5_sendto_kdc (context
, &req
, &creds
->client
->realm
, &resp
);
529 memset (&rep
, 0, sizeof(rep
));
530 if((ret
= decode_AS_REP(resp
.data
, resp
.length
, &rep
.part1
, &size
))){
531 /* let's try to parse it as a KRB-ERROR */
535 ret2
= krb5_rd_error(context
, &resp
, &error
);
536 if(ret2
&& ((char*)resp
.data
)[0] == 4)
537 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
538 krb5_data_free(&resp
);
540 /* XXX - send krb-error packet to caller */
541 free_KRB_ERROR (&error
);
542 return error
.error_code
;
546 krb5_data_free(&resp
);
549 if(rep
.part1
.padata
){
551 pa
= krb5_find_padata(rep
.part1
.padata
->val
, rep
.part1
.padata
->len
,
555 ret
= (*key_proc
)(context
, etype
,
556 &pa
->padata_value
, keyseed
, &key
);
558 /* make a v5 salted pa-data */
561 ret
= krb5_get_salt (creds
->client
, &salt
);
565 ret
= (*key_proc
)(context
, etype
, &salt
,
567 krb5_data_free (&salt
);
572 ret
= _krb5_extract_ticket(context
, &rep
, creds
, key
, keyseed
,
573 NULL
, nonce
, decrypt_proc
, decryptarg
);
574 memset (key
->keyvalue
.data
, 0, key
->keyvalue
.length
);
575 krb5_free_keyblock (context
, key
);
578 if (ret
== 0 && ret_as_reply
)
581 krb5_free_kdc_rep (context
, &rep
);
586 krb5_get_in_tkt(krb5_context context
,
588 const krb5_addresses
*addrs
,
589 const krb5_enctype
*etypes
,
590 const krb5_preauthtype
*ptypes
,
591 krb5_key_proc key_proc
,
592 krb5_const_pointer keyseed
,
593 krb5_decrypt_proc decrypt_proc
,
594 krb5_const_pointer decryptarg
,
597 krb5_kdc_rep
*ret_as_reply
)
601 ret
= krb5_get_in_cred (context
,
614 ret
= krb5_cc_store_cred (context
, ccache
, creds
);
615 krb5_free_creds_contents (context
, creds
);