2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
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 typedef struct krb5_get_init_creds_ctx
{
41 krb5_addresses
*addrs
;
43 krb5_preauthtype
*pre_auth_types
;
52 /* password and keytab_data is freed on completion */
54 krb5_keytab_key_proc_args
*keytab_data
;
56 krb5_pointer
*keyseed
;
57 krb5_s2k_proc keyproc
;
59 krb5_get_init_creds_tristate req_pac
;
61 krb5_pk_init_ctx pk_init_ctx
;
66 #define USED_PKINIT_W2K 2
67 #define USED_ENC_TS_GUESS 4
68 #define USED_ENC_TS_INFO 8
73 EncKDCRepPart enc_part
;
75 krb5_prompter_fct prompter
;
78 struct pa_info_data
*ppaid
;
80 enum PA_FX_FAST_REQUEST_enum type
;
82 #define KRB5_FAST_REPLY_KEY_USE_TO_ENCRYPT_THE_REPLY 1
83 #define KRB5_FAST_REPLY_KEY_USE_IN_TRANSACTION 2
84 #define KRB5_FAST_KDC_REPLY_KEY_REPLACED 4
85 #define KRB5_FAST_REPLY_REPLY_VERIFED 8
86 #define KRB5_FAST_STRONG 16
87 #define KRB5_FAST_EXPECTED 32 /* in exchange with KDC, fast was discovered */
88 #define KRB5_FAST_REQUIRED 64 /* fast required by action of caller */
89 #define KRB5_FAST_DISABLED 128
90 krb5_keyblock
*reply_key
;
91 krb5_ccache armor_ccache
;
92 krb5_crypto armor_crypto
;
93 krb5_keyblock armor_key
;
95 } krb5_get_init_creds_ctx
;
101 krb5_data
*s2kparams
;
105 free_paid(krb5_context context
, struct pa_info_data
*ppaid
)
107 krb5_free_salt(context
, ppaid
->salt
);
108 if (ppaid
->s2kparams
)
109 krb5_free_data(context
, ppaid
->s2kparams
);
112 static krb5_error_code KRB5_CALLCONV
113 default_s2k_func(krb5_context context
, krb5_enctype type
,
114 krb5_const_pointer keyseed
,
115 krb5_salt salt
, krb5_data
*s2kparms
,
122 _krb5_debug(context
, 5, "krb5_get_init_creds: using default_s2k_func");
124 password
.data
= rk_UNCONST(keyseed
);
125 password
.length
= strlen(keyseed
);
129 krb5_data_zero(&opaque
);
131 *key
= malloc(sizeof(**key
));
134 ret
= krb5_string_to_key_data_salt_opaque(context
, type
, password
,
144 free_init_creds_ctx(krb5_context context
, krb5_init_creds_context ctx
)
148 if (ctx
->pre_auth_types
)
149 free (ctx
->pre_auth_types
);
150 if (ctx
->in_tkt_service
)
151 free(ctx
->in_tkt_service
);
152 if (ctx
->keytab_data
)
153 free(ctx
->keytab_data
);
155 memset(ctx
->password
, 0, strlen(ctx
->password
));
159 * FAST state (we don't close the armor_ccache because we might have
160 * to destroy it, and how would we know? also, the caller should
161 * take care of cleaning up the armor_ccache).
163 if (ctx
->fast_state
.armor_crypto
)
164 krb5_crypto_destroy(context
, ctx
->fast_state
.armor_crypto
);
165 krb5_free_keyblock_contents(context
, &ctx
->fast_state
.armor_key
);
167 krb5_data_free(&ctx
->req_buffer
);
168 krb5_free_cred_contents(context
, &ctx
->cred
);
169 free_METHOD_DATA(&ctx
->md
);
170 free_AS_REP(&ctx
->as_rep
);
171 free_EncKDCRepPart(&ctx
->enc_part
);
172 free_KRB_ERROR(&ctx
->error
);
173 free_AS_REQ(&ctx
->as_req
);
175 free_paid(context
, ctx
->ppaid
);
178 memset(ctx
, 0, sizeof(*ctx
));
182 get_config_time (krb5_context context
,
189 ret
= krb5_config_get_time (context
, NULL
,
196 ret
= krb5_config_get_time (context
, NULL
,
205 static krb5_error_code
206 init_cred (krb5_context context
,
208 krb5_principal client
,
209 krb5_deltat start_time
,
210 krb5_get_init_creds_opt
*options
)
216 krb5_timeofday (context
, &now
);
218 memset (cred
, 0, sizeof(*cred
));
221 krb5_copy_principal(context
, client
, &cred
->client
);
223 ret
= krb5_get_default_principal (context
,
230 cred
->times
.starttime
= now
+ start_time
;
232 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_TKT_LIFE
)
233 tmp
= options
->tkt_life
;
236 cred
->times
.endtime
= now
+ tmp
;
238 if ((options
->flags
& KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE
) &&
239 options
->renew_life
> 0) {
240 cred
->times
.renew_till
= now
+ options
->renew_life
;
246 krb5_free_cred_contents (context
, cred
);
251 * Print a message (str) to the user about the expiration in `lr'
255 report_expiration (krb5_context context
,
256 krb5_prompter_fct prompter
,
263 if (asprintf(&p
, "%s%s", str
, ctime(&now
)) < 0 || p
== NULL
)
265 (*prompter
)(context
, data
, NULL
, p
, 0, NULL
);
270 * Check the context, and in the case there is a expiration warning,
271 * use the prompter to print the warning.
273 * @param context A Kerberos 5 context.
274 * @param options An GIC options structure
275 * @param ctx The krb5_init_creds_context check for expiration.
279 krb5_process_last_request(krb5_context context
,
280 krb5_get_init_creds_opt
*options
,
281 krb5_init_creds_context ctx
)
283 krb5_const_realm realm
;
285 krb5_boolean reported
= FALSE
;
291 * First check if there is a API consumer.
294 realm
= krb5_principal_get_realm (context
, ctx
->cred
.client
);
295 lr
= &ctx
->enc_part
.last_req
;
297 if (options
&& options
->opt_private
&& options
->opt_private
->lr
.func
) {
298 krb5_last_req_entry
**lre
;
300 lre
= calloc(lr
->len
+ 1, sizeof(**lre
));
302 return krb5_enomem(context
);
303 for (i
= 0; i
< lr
->len
; i
++) {
304 lre
[i
] = calloc(1, sizeof(*lre
[i
]));
307 lre
[i
]->lr_type
= lr
->val
[i
].lr_type
;
308 lre
[i
]->value
= lr
->val
[i
].lr_value
;
311 (*options
->opt_private
->lr
.func
)(context
, lre
,
312 options
->opt_private
->lr
.ctx
);
314 for (i
= 0; i
< lr
->len
; i
++)
320 * Now check if we should prompt the user
323 if (ctx
->prompter
== NULL
)
326 krb5_timeofday (context
, &sec
);
328 t
= sec
+ get_config_time (context
,
333 for (i
= 0; i
< lr
->len
; ++i
) {
334 if (lr
->val
[i
].lr_value
<= t
) {
335 switch (abs(lr
->val
[i
].lr_type
)) {
337 report_expiration(context
, ctx
->prompter
,
339 "Your password will expire at ",
340 lr
->val
[i
].lr_value
);
343 case LR_ACCT_EXPTIME
:
344 report_expiration(context
, ctx
->prompter
,
346 "Your account will expire at ",
347 lr
->val
[i
].lr_value
);
355 && ctx
->enc_part
.key_expiration
356 && *ctx
->enc_part
.key_expiration
<= t
) {
357 report_expiration(context
, ctx
->prompter
,
359 "Your password/account will expire at ",
360 *ctx
->enc_part
.key_expiration
);
365 static krb5_addresses no_addrs
= { 0, NULL
};
367 static krb5_error_code
368 get_init_creds_common(krb5_context context
,
369 krb5_principal client
,
370 krb5_deltat start_time
,
371 krb5_get_init_creds_opt
*options
,
372 krb5_init_creds_context ctx
)
374 krb5_get_init_creds_opt
*default_opt
= NULL
;
376 krb5_enctype
*etypes
;
377 krb5_preauthtype
*pre_auth_types
;
379 memset(ctx
, 0, sizeof(*ctx
));
381 if (options
== NULL
) {
382 const char *realm
= krb5_principal_get_realm(context
, client
);
384 krb5_get_init_creds_opt_alloc (context
, &default_opt
);
385 options
= default_opt
;
386 krb5_get_init_creds_opt_set_default_flags(context
, NULL
, realm
, options
);
389 if (options
->opt_private
) {
390 if (options
->opt_private
->password
) {
391 ret
= krb5_init_creds_set_password(context
, ctx
,
392 options
->opt_private
->password
);
397 ctx
->keyproc
= options
->opt_private
->key_proc
;
398 ctx
->req_pac
= options
->opt_private
->req_pac
;
399 ctx
->pk_init_ctx
= options
->opt_private
->pk_init_ctx
;
400 ctx
->ic_flags
= options
->opt_private
->flags
;
402 ctx
->req_pac
= KRB5_INIT_CREDS_TRISTATE_UNSET
;
404 if (ctx
->keyproc
== NULL
)
405 ctx
->keyproc
= default_s2k_func
;
407 /* Enterprise name implicitly turns on canonicalize */
408 if ((ctx
->ic_flags
& KRB5_INIT_CREDS_CANONICALIZE
) ||
409 krb5_principal_get_type(context
, client
) == KRB5_NT_ENTERPRISE_PRINCIPAL
)
410 ctx
->flags
.canonicalize
= 1;
412 ctx
->pre_auth_types
= NULL
;
415 ctx
->pre_auth_types
= NULL
;
417 ret
= init_cred(context
, &ctx
->cred
, client
, start_time
, options
);
420 krb5_get_init_creds_opt_free(context
, default_opt
);
424 ret
= krb5_init_creds_set_service(context
, ctx
, NULL
);
428 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_FORWARDABLE
)
429 ctx
->flags
.forwardable
= options
->forwardable
;
431 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_PROXIABLE
)
432 ctx
->flags
.proxiable
= options
->proxiable
;
435 ctx
->flags
.postdated
= 1;
436 if (ctx
->cred
.times
.renew_till
)
437 ctx
->flags
.renewable
= 1;
438 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST
) {
439 ctx
->addrs
= options
->address_list
;
440 } else if (options
->opt_private
) {
441 switch (options
->opt_private
->addressless
) {
442 case KRB5_INIT_CREDS_TRISTATE_UNSET
:
443 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
444 ctx
->addrs
= &no_addrs
;
449 case KRB5_INIT_CREDS_TRISTATE_FALSE
:
452 case KRB5_INIT_CREDS_TRISTATE_TRUE
:
453 ctx
->addrs
= &no_addrs
;
457 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST
) {
461 etypes
= malloc((options
->etype_list_length
+ 1)
462 * sizeof(krb5_enctype
));
463 if (etypes
== NULL
) {
464 ret
= krb5_enomem(context
);
467 memcpy (etypes
, options
->etype_list
,
468 options
->etype_list_length
* sizeof(krb5_enctype
));
469 etypes
[options
->etype_list_length
] = ETYPE_NULL
;
470 ctx
->etypes
= etypes
;
472 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST
) {
473 pre_auth_types
= malloc((options
->preauth_list_length
+ 1)
474 * sizeof(krb5_preauthtype
));
475 if (pre_auth_types
== NULL
) {
476 ret
= krb5_enomem(context
);
479 memcpy (pre_auth_types
, options
->preauth_list
,
480 options
->preauth_list_length
* sizeof(krb5_preauthtype
));
481 pre_auth_types
[options
->preauth_list_length
] = KRB5_PADATA_NONE
;
482 ctx
->pre_auth_types
= pre_auth_types
;
484 if (options
->flags
& KRB5_GET_INIT_CREDS_OPT_ANONYMOUS
)
485 ctx
->flags
.request_anonymous
= options
->anonymous
;
487 krb5_get_init_creds_opt_free(context
, default_opt
);
491 krb5_get_init_creds_opt_free(context
, default_opt
);
495 static krb5_error_code
496 change_password (krb5_context context
,
497 krb5_principal client
,
498 const char *password
,
501 krb5_prompter_fct prompter
,
503 krb5_get_init_creds_opt
*old_options
)
505 krb5_prompt prompts
[2];
508 char buf1
[BUFSIZ
], buf2
[BUFSIZ
];
509 krb5_data password_data
[2];
511 krb5_data result_code_string
;
512 krb5_data result_string
;
514 krb5_get_init_creds_opt
*options
;
516 memset (&cpw_cred
, 0, sizeof(cpw_cred
));
518 ret
= krb5_get_init_creds_opt_alloc(context
, &options
);
521 krb5_get_init_creds_opt_set_tkt_life (options
, 60);
522 krb5_get_init_creds_opt_set_forwardable (options
, FALSE
);
523 krb5_get_init_creds_opt_set_proxiable (options
, FALSE
);
524 if (old_options
&& old_options
->flags
& KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST
)
525 krb5_get_init_creds_opt_set_preauth_list (options
,
526 old_options
->preauth_list
,
527 old_options
->preauth_list_length
);
529 krb5_data_zero (&result_code_string
);
530 krb5_data_zero (&result_string
);
532 ret
= krb5_get_init_creds_password (context
,
541 krb5_get_init_creds_opt_free(context
, options
);
546 password_data
[0].data
= buf1
;
547 password_data
[0].length
= sizeof(buf1
);
549 prompts
[0].hidden
= 1;
550 prompts
[0].prompt
= "New password: ";
551 prompts
[0].reply
= &password_data
[0];
552 prompts
[0].type
= KRB5_PROMPT_TYPE_NEW_PASSWORD
;
554 password_data
[1].data
= buf2
;
555 password_data
[1].length
= sizeof(buf2
);
557 prompts
[1].hidden
= 1;
558 prompts
[1].prompt
= "Repeat new password: ";
559 prompts
[1].reply
= &password_data
[1];
560 prompts
[1].type
= KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN
;
562 ret
= (*prompter
) (context
, data
, NULL
, "Changing password",
565 memset (buf1
, 0, sizeof(buf1
));
566 memset (buf2
, 0, sizeof(buf2
));
570 if (strcmp (buf1
, buf2
) == 0)
572 memset (buf1
, 0, sizeof(buf1
));
573 memset (buf2
, 0, sizeof(buf2
));
576 ret
= krb5_set_password (context
,
585 if (asprintf(&p
, "%s: %.*s\n",
586 result_code
? "Error" : "Success",
587 (int)result_string
.length
,
588 result_string
.length
> 0 ? (char*)result_string
.data
: "") < 0)
594 /* return the result */
595 (*prompter
) (context
, data
, NULL
, p
, 0, NULL
);
598 if (result_code
== 0) {
599 strlcpy (newpw
, buf1
, newpw_sz
);
603 krb5_set_error_message(context
, ret
,
604 N_("failed changing password", ""));
608 memset (buf1
, 0, sizeof(buf1
));
609 memset (buf2
, 0, sizeof(buf2
));
610 krb5_data_free (&result_string
);
611 krb5_data_free (&result_code_string
);
612 krb5_free_cred_contents (context
, &cpw_cred
);
617 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
618 krb5_keyblock_key_proc (krb5_context context
,
621 krb5_const_pointer keyseed
,
624 return krb5_copy_keyblock (context
, keyseed
, key
);
631 static krb5_error_code
632 init_as_req (krb5_context context
,
634 const krb5_creds
*creds
,
635 const krb5_addresses
*addrs
,
636 const krb5_enctype
*etypes
,
641 memset(a
, 0, sizeof(*a
));
644 a
->msg_type
= krb_as_req
;
645 a
->req_body
.kdc_options
= opts
;
646 a
->req_body
.cname
= malloc(sizeof(*a
->req_body
.cname
));
647 if (a
->req_body
.cname
== NULL
) {
648 ret
= krb5_enomem(context
);
651 a
->req_body
.sname
= malloc(sizeof(*a
->req_body
.sname
));
652 if (a
->req_body
.sname
== NULL
) {
653 ret
= krb5_enomem(context
);
657 ret
= _krb5_principal2principalname (a
->req_body
.cname
, creds
->client
);
660 ret
= copy_Realm(&creds
->client
->realm
, &a
->req_body
.realm
);
664 ret
= _krb5_principal2principalname (a
->req_body
.sname
, creds
->server
);
668 if(creds
->times
.starttime
) {
669 a
->req_body
.from
= malloc(sizeof(*a
->req_body
.from
));
670 if (a
->req_body
.from
== NULL
) {
671 ret
= krb5_enomem(context
);
674 *a
->req_body
.from
= creds
->times
.starttime
;
676 if(creds
->times
.endtime
){
677 ALLOC(a
->req_body
.till
, 1);
678 *a
->req_body
.till
= creds
->times
.endtime
;
680 if(creds
->times
.renew_till
){
681 a
->req_body
.rtime
= malloc(sizeof(*a
->req_body
.rtime
));
682 if (a
->req_body
.rtime
== NULL
) {
683 ret
= krb5_enomem(context
);
686 *a
->req_body
.rtime
= creds
->times
.renew_till
;
688 a
->req_body
.nonce
= 0;
689 ret
= _krb5_init_etype(context
,
691 &a
->req_body
.etype
.len
,
692 &a
->req_body
.etype
.val
,
698 * This means no addresses
701 if (addrs
&& addrs
->len
== 0) {
702 a
->req_body
.addresses
= NULL
;
704 a
->req_body
.addresses
= malloc(sizeof(*a
->req_body
.addresses
));
705 if (a
->req_body
.addresses
== NULL
) {
706 ret
= krb5_enomem(context
);
711 ret
= krb5_copy_addresses(context
, addrs
, a
->req_body
.addresses
);
713 ret
= krb5_get_all_client_addrs (context
, a
->req_body
.addresses
);
714 if(ret
== 0 && a
->req_body
.addresses
->len
== 0) {
715 free(a
->req_body
.addresses
);
716 a
->req_body
.addresses
= NULL
;
723 a
->req_body
.enc_authorization_data
= NULL
;
724 a
->req_body
.additional_tickets
= NULL
;
731 memset(a
, 0, sizeof(*a
));
736 static krb5_error_code
737 set_paid(struct pa_info_data
*paid
, krb5_context context
,
739 krb5_salttype salttype
, void *salt_string
, size_t salt_len
,
740 krb5_data
*s2kparams
)
743 paid
->salt
.salttype
= salttype
;
744 paid
->salt
.saltvalue
.data
= malloc(salt_len
+ 1);
745 if (paid
->salt
.saltvalue
.data
== NULL
) {
746 krb5_clear_error_message(context
);
749 memcpy(paid
->salt
.saltvalue
.data
, salt_string
, salt_len
);
750 ((char *)paid
->salt
.saltvalue
.data
)[salt_len
] = '\0';
751 paid
->salt
.saltvalue
.length
= salt_len
;
755 ret
= krb5_copy_data(context
, s2kparams
, &paid
->s2kparams
);
757 krb5_clear_error_message(context
);
758 krb5_free_salt(context
, paid
->salt
);
762 paid
->s2kparams
= NULL
;
767 static struct pa_info_data
*
768 pa_etype_info2(krb5_context context
,
769 const krb5_principal client
,
771 struct pa_info_data
*paid
,
772 heim_octet_string
*data
)
779 memset(&e
, 0, sizeof(e
));
780 ret
= decode_ETYPE_INFO2(data
->data
, data
->length
, &e
, &sz
);
785 for (j
= 0; j
< asreq
->req_body
.etype
.len
; j
++) {
786 for (i
= 0; i
< e
.len
; i
++) {
787 if (asreq
->req_body
.etype
.val
[j
] == e
.val
[i
].etype
) {
789 if (e
.val
[i
].salt
== NULL
)
790 ret
= krb5_get_pw_salt(context
, client
, &salt
);
792 salt
.saltvalue
.data
= *e
.val
[i
].salt
;
793 salt
.saltvalue
.length
= strlen(*e
.val
[i
].salt
);
797 ret
= set_paid(paid
, context
, e
.val
[i
].etype
,
800 salt
.saltvalue
.length
,
802 if (e
.val
[i
].salt
== NULL
)
803 krb5_free_salt(context
, salt
);
805 free_ETYPE_INFO2(&e
);
812 free_ETYPE_INFO2(&e
);
816 static struct pa_info_data
*
817 pa_etype_info(krb5_context context
,
818 const krb5_principal client
,
820 struct pa_info_data
*paid
,
821 heim_octet_string
*data
)
828 memset(&e
, 0, sizeof(e
));
829 ret
= decode_ETYPE_INFO(data
->data
, data
->length
, &e
, &sz
);
834 for (j
= 0; j
< asreq
->req_body
.etype
.len
; j
++) {
835 for (i
= 0; i
< e
.len
; i
++) {
836 if (asreq
->req_body
.etype
.val
[j
] == e
.val
[i
].etype
) {
838 salt
.salttype
= KRB5_PW_SALT
;
839 if (e
.val
[i
].salt
== NULL
)
840 ret
= krb5_get_pw_salt(context
, client
, &salt
);
842 salt
.saltvalue
= *e
.val
[i
].salt
;
845 if (e
.val
[i
].salttype
)
846 salt
.salttype
= *e
.val
[i
].salttype
;
848 ret
= set_paid(paid
, context
, e
.val
[i
].etype
,
851 salt
.saltvalue
.length
,
853 if (e
.val
[i
].salt
== NULL
)
854 krb5_free_salt(context
, salt
);
868 static struct pa_info_data
*
869 pa_pw_or_afs3_salt(krb5_context context
,
870 const krb5_principal client
,
872 struct pa_info_data
*paid
,
873 heim_octet_string
*data
)
876 if (paid
->etype
== KRB5_ENCTYPE_NULL
)
878 ret
= set_paid(paid
, context
,
891 krb5_preauthtype type
;
892 struct pa_info_data
*(*salt_info
)(krb5_context
,
893 const krb5_principal
,
895 struct pa_info_data
*,
896 heim_octet_string
*);
899 static struct pa_info pa_prefs
[] = {
900 { KRB5_PADATA_ETYPE_INFO2
, pa_etype_info2
},
901 { KRB5_PADATA_ETYPE_INFO
, pa_etype_info
},
902 { KRB5_PADATA_PW_SALT
, pa_pw_or_afs3_salt
},
903 { KRB5_PADATA_AFS3_SALT
, pa_pw_or_afs3_salt
}
907 find_pa_data(const METHOD_DATA
*md
, unsigned type
)
912 for (i
= 0; i
< md
->len
; i
++)
913 if (md
->val
[i
].padata_type
== type
)
918 static struct pa_info_data
*
919 process_pa_info(krb5_context context
,
920 const krb5_principal client
,
922 struct pa_info_data
*paid
,
925 struct pa_info_data
*p
= NULL
;
928 for (i
= 0; p
== NULL
&& i
< sizeof(pa_prefs
)/sizeof(pa_prefs
[0]); i
++) {
929 PA_DATA
*pa
= find_pa_data(md
, pa_prefs
[i
].type
);
932 paid
->salt
.salttype
= (krb5_salttype
)pa_prefs
[i
].type
;
933 p
= (*pa_prefs
[i
].salt_info
)(context
, client
, asreq
,
934 paid
, &pa
->padata_value
);
939 static krb5_error_code
940 make_pa_enc_timestamp(krb5_context context
, METHOD_DATA
*md
,
941 krb5_enctype etype
, krb5_keyblock
*key
)
947 EncryptedData encdata
;
953 krb5_us_timeofday (context
, &p
.patimestamp
, &usec
);
957 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC
, buf
, buf_size
, &p
, &len
, ret
);
961 krb5_abortx(context
, "internal error in ASN.1 encoder");
963 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
968 ret
= krb5_encrypt_EncryptedData(context
,
970 KRB5_KU_PA_ENC_TIMESTAMP
,
976 krb5_crypto_destroy(context
, crypto
);
980 ASN1_MALLOC_ENCODE(EncryptedData
, buf
, buf_size
, &encdata
, &len
, ret
);
981 free_EncryptedData(&encdata
);
985 krb5_abortx(context
, "internal error in ASN.1 encoder");
987 ret
= krb5_padata_add(context
, md
, KRB5_PADATA_ENC_TIMESTAMP
, buf
, len
);
993 static krb5_error_code
994 add_enc_ts_padata(krb5_context context
,
996 krb5_principal client
,
997 krb5_s2k_proc keyproc
,
998 krb5_const_pointer keyseed
,
999 krb5_enctype
*enctypes
,
1002 krb5_data
*s2kparams
)
1004 krb5_error_code ret
;
1010 /* default to standard salt */
1011 ret
= krb5_get_pw_salt (context
, client
, &salt2
);
1017 enctypes
= context
->etypes
;
1019 for (ep
= enctypes
; *ep
!= (krb5_enctype
)ETYPE_NULL
; ep
++)
1023 for (i
= 0; i
< netypes
; ++i
) {
1026 _krb5_debug(context
, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes
[i
]);
1028 ret
= (*keyproc
)(context
, enctypes
[i
], keyseed
,
1029 *salt
, s2kparams
, &key
);
1032 ret
= make_pa_enc_timestamp (context
, md
, enctypes
[i
], key
);
1033 krb5_free_keyblock (context
, key
);
1038 krb5_free_salt(context
, salt2
);
1042 static krb5_error_code
1043 pa_data_to_md_ts_enc(krb5_context context
,
1045 const krb5_principal client
,
1046 krb5_get_init_creds_ctx
*ctx
,
1047 struct pa_info_data
*ppaid
,
1050 if (ctx
->keyproc
== NULL
|| ctx
->keyseed
== NULL
)
1054 add_enc_ts_padata(context
, md
, client
,
1055 ctx
->keyproc
, ctx
->keyseed
,
1057 &ppaid
->salt
, ppaid
->s2kparams
);
1061 _krb5_debug(context
, 5, "krb5_get_init_creds: pa-info not found, guessing salt");
1063 /* make a v5 salted pa-data */
1064 add_enc_ts_padata(context
, md
, client
,
1065 ctx
->keyproc
, ctx
->keyseed
,
1066 a
->req_body
.etype
.val
, a
->req_body
.etype
.len
,
1069 /* make a v4 salted pa-data */
1070 salt
.salttype
= KRB5_PW_SALT
;
1071 krb5_data_zero(&salt
.saltvalue
);
1072 add_enc_ts_padata(context
, md
, client
,
1073 ctx
->keyproc
, ctx
->keyseed
,
1074 a
->req_body
.etype
.val
, a
->req_body
.etype
.len
,
1080 static krb5_error_code
1081 pa_data_to_key_plain(krb5_context context
,
1082 const krb5_principal client
,
1083 krb5_get_init_creds_ctx
*ctx
,
1085 krb5_data
*s2kparams
,
1087 krb5_keyblock
**key
)
1089 krb5_error_code ret
;
1091 ret
= (*ctx
->keyproc
)(context
, etype
, ctx
->keyseed
,
1092 salt
, s2kparams
, key
);
1097 static krb5_error_code
1098 pa_data_to_md_pkinit(krb5_context context
,
1100 const krb5_principal client
,
1102 krb5_get_init_creds_ctx
*ctx
,
1105 if (ctx
->pk_init_ctx
== NULL
)
1108 return _krb5_pk_mk_padata(context
,
1116 krb5_set_error_message(context
, EINVAL
,
1117 N_("no support for PKINIT compiled in", ""));
1122 static krb5_error_code
1123 pa_data_add_pac_request(krb5_context context
,
1124 krb5_get_init_creds_ctx
*ctx
,
1127 size_t len
= 0, length
;
1128 krb5_error_code ret
;
1132 switch (ctx
->req_pac
) {
1133 case KRB5_INIT_CREDS_TRISTATE_UNSET
:
1134 return 0; /* don't bother */
1135 case KRB5_INIT_CREDS_TRISTATE_TRUE
:
1136 req
.include_pac
= 1;
1138 case KRB5_INIT_CREDS_TRISTATE_FALSE
:
1139 req
.include_pac
= 0;
1142 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST
, buf
, length
,
1147 krb5_abortx(context
, "internal error in ASN.1 encoder");
1149 ret
= krb5_padata_add(context
, md
, KRB5_PADATA_PA_PAC_REQUEST
, buf
, len
);
1157 * Assumes caller always will free `out_md', even on error.
1160 static krb5_error_code
1161 process_pa_data_to_md(krb5_context context
,
1162 const krb5_creds
*creds
,
1164 krb5_get_init_creds_ctx
*ctx
,
1166 METHOD_DATA
**out_md
,
1167 krb5_prompter_fct prompter
,
1168 void *prompter_data
)
1170 krb5_error_code ret
;
1173 if (*out_md
== NULL
)
1174 return krb5_enomem(context
);
1177 (*out_md
)->val
= NULL
;
1179 if (_krb5_have_debug(context
, 5)) {
1181 _krb5_debug(context
, 5, "KDC send %d patypes", in_md
->len
);
1182 for (i
= 0; i
< in_md
->len
; i
++)
1183 _krb5_debug(context
, 5, "KDC send PA-DATA type: %d", in_md
->val
[i
].padata_type
);
1187 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1188 * need to expose our password protecting our PKCS12 key.
1191 if (ctx
->pk_init_ctx
) {
1193 _krb5_debug(context
, 5, "krb5_get_init_creds: "
1194 "prepareing PKINIT padata (%s)",
1195 (ctx
->used_pa_types
& USED_PKINIT_W2K
) ? "win2k" : "ietf");
1197 if (ctx
->used_pa_types
& USED_PKINIT_W2K
) {
1198 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
1199 "Already tried pkinit, looping");
1200 return KRB5_GET_IN_TKT_LOOP
;
1203 ret
= pa_data_to_md_pkinit(context
, a
, creds
->client
,
1204 (ctx
->used_pa_types
& USED_PKINIT
),
1209 if (ctx
->used_pa_types
& USED_PKINIT
)
1210 ctx
->used_pa_types
|= USED_PKINIT_W2K
;
1212 ctx
->used_pa_types
|= USED_PKINIT
;
1214 } else if (in_md
->len
!= 0) {
1215 struct pa_info_data
*paid
, *ppaid
;
1218 paid
= calloc(1, sizeof(*paid
));
1220 paid
->etype
= KRB5_ENCTYPE_NULL
;
1221 ppaid
= process_pa_info(context
, creds
->client
, a
, paid
, in_md
);
1224 flag
= USED_ENC_TS_INFO
;
1226 flag
= USED_ENC_TS_GUESS
;
1228 if (ctx
->used_pa_types
& flag
) {
1230 free_paid(context
, ppaid
);
1231 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
1232 "Already tried ENC-TS-%s, looping",
1233 flag
== USED_ENC_TS_INFO
? "info" : "guess");
1234 return KRB5_GET_IN_TKT_LOOP
;
1237 pa_data_to_md_ts_enc(context
, a
, creds
->client
, ctx
, ppaid
, *out_md
);
1239 ctx
->used_pa_types
|= flag
;
1243 free_paid(context
, ctx
->ppaid
);
1251 pa_data_add_pac_request(context
, ctx
, *out_md
);
1253 if ((ctx
->fast_state
.flags
& KRB5_FAST_DISABLED
) == 0) {
1254 ret
= krb5_padata_add(context
, *out_md
, KRB5_PADATA_REQ_ENC_PA_REP
, NULL
, 0);
1259 if ((*out_md
)->len
== 0) {
1267 static krb5_error_code
1268 process_pa_data_to_key(krb5_context context
,
1269 krb5_get_init_creds_ctx
*ctx
,
1273 const krb5_krbhst_info
*hi
,
1274 krb5_keyblock
**key
)
1276 struct pa_info_data paid
, *ppaid
= NULL
;
1277 krb5_error_code ret
;
1281 memset(&paid
, 0, sizeof(paid
));
1283 etype
= rep
->enc_part
.etype
;
1287 ppaid
= process_pa_info(context
, creds
->client
, a
, &paid
,
1292 if (ppaid
== NULL
) {
1293 ret
= krb5_get_pw_salt (context
, creds
->client
, &paid
.salt
);
1297 paid
.s2kparams
= NULL
;
1304 pa
= krb5_find_padata(rep
->padata
->val
,
1306 KRB5_PADATA_PK_AS_REP
,
1310 pa
= krb5_find_padata(rep
->padata
->val
,
1312 KRB5_PADATA_PK_AS_REP_19
,
1316 if (pa
&& ctx
->pk_init_ctx
) {
1318 _krb5_debug(context
, 5, "krb5_get_init_creds: using PKINIT");
1320 ret
= _krb5_pk_rd_pa_reply(context
,
1331 krb5_set_error_message(context
, ret
, N_("no support for PKINIT compiled in", ""));
1333 } else if (ctx
->keyseed
) {
1334 _krb5_debug(context
, 5, "krb5_get_init_creds: using keyproc");
1335 ret
= pa_data_to_key_plain(context
, creds
->client
, ctx
,
1336 ppaid
->salt
, ppaid
->s2kparams
, etype
, key
);
1339 krb5_set_error_message(context
, ret
, N_("No usable pa data type", ""));
1342 free_paid(context
, &paid
);
1347 * Start a new context to get a new initial credential.
1349 * @param context A Kerberos 5 context.
1350 * @param client The Kerberos principal to get the credential for, if
1351 * NULL is given, the default principal is used as determined by
1352 * krb5_get_default_principal().
1354 * @param prompter_data
1355 * @param start_time the time the ticket should start to be valid or 0 for now.
1356 * @param options a options structure, can be NULL for default options.
1357 * @param rctx A new allocated free with krb5_init_creds_free().
1359 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1361 * @ingroup krb5_credential
1364 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1365 krb5_init_creds_init(krb5_context context
,
1366 krb5_principal client
,
1367 krb5_prompter_fct prompter
,
1368 void *prompter_data
,
1369 krb5_deltat start_time
,
1370 krb5_get_init_creds_opt
*options
,
1371 krb5_init_creds_context
*rctx
)
1373 krb5_init_creds_context ctx
;
1374 krb5_error_code ret
;
1378 ctx
= calloc(1, sizeof(*ctx
));
1380 return krb5_enomem(context
);
1382 ret
= get_init_creds_common(context
, client
, start_time
, options
, ctx
);
1388 /* Set a new nonce. */
1389 krb5_generate_random_block (&ctx
->nonce
, sizeof(ctx
->nonce
));
1390 ctx
->nonce
&= 0x7fffffff;
1391 /* XXX these just needs to be the same when using Windows PK-INIT */
1392 ctx
->pk_nonce
= ctx
->nonce
;
1394 ctx
->prompter
= prompter
;
1395 ctx
->prompter_data
= prompter_data
;
1403 * Sets the service that the is requested. This call is only neede for
1404 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1406 * @param context a Kerberos 5 context.
1407 * @param ctx a krb5_init_creds_context context.
1408 * @param service the service given as a string, for example
1409 * "kadmind/admin". If NULL, the default krbtgt in the clients
1412 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1413 * @ingroup krb5_credential
1416 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1417 krb5_init_creds_set_service(krb5_context context
,
1418 krb5_init_creds_context ctx
,
1419 const char *service
)
1421 krb5_const_realm client_realm
;
1422 krb5_principal principal
;
1423 krb5_error_code ret
;
1425 client_realm
= krb5_principal_get_realm (context
, ctx
->cred
.client
);
1428 ret
= krb5_parse_name (context
, service
, &principal
);
1431 krb5_principal_set_realm (context
, principal
, client_realm
);
1433 ret
= krb5_make_principal(context
, &principal
,
1434 client_realm
, KRB5_TGS_NAME
, client_realm
,
1441 * This is for Windows RODC that are picky about what name type
1442 * the server principal have, and the really strange part is that
1443 * they are picky about the AS-REQ name type and not the TGS-REQ
1447 if (krb5_principal_is_krbtgt(context
, principal
))
1448 krb5_principal_set_type(context
, principal
, KRB5_NT_SRV_INST
);
1450 krb5_free_principal(context
, ctx
->cred
.server
);
1451 ctx
->cred
.server
= principal
;
1457 * Sets the password that will use for the request.
1459 * @param context a Kerberos 5 context.
1460 * @param ctx ctx krb5_init_creds_context context.
1461 * @param password the password to use.
1463 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1464 * @ingroup krb5_credential
1467 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1468 krb5_init_creds_set_password(krb5_context context
,
1469 krb5_init_creds_context ctx
,
1470 const char *password
)
1472 if (ctx
->password
) {
1473 memset(ctx
->password
, 0, strlen(ctx
->password
));
1474 free(ctx
->password
);
1477 ctx
->password
= strdup(password
);
1478 if (ctx
->password
== NULL
)
1479 return krb5_enomem(context
);
1480 ctx
->keyseed
= (void *) ctx
->password
;
1482 ctx
->keyseed
= NULL
;
1483 ctx
->password
= NULL
;
1489 static krb5_error_code KRB5_CALLCONV
1490 keytab_key_proc(krb5_context context
, krb5_enctype enctype
,
1491 krb5_const_pointer keyseed
,
1492 krb5_salt salt
, krb5_data
*s2kparms
,
1493 krb5_keyblock
**key
)
1495 krb5_keytab_key_proc_args
*args
= rk_UNCONST(keyseed
);
1496 krb5_keytab keytab
= args
->keytab
;
1497 krb5_principal principal
= args
->principal
;
1498 krb5_error_code ret
;
1499 krb5_keytab real_keytab
;
1500 krb5_keytab_entry entry
;
1503 krb5_kt_default(context
, &real_keytab
);
1505 real_keytab
= keytab
;
1507 ret
= krb5_kt_get_entry (context
, real_keytab
, principal
,
1508 0, enctype
, &entry
);
1511 krb5_kt_close (context
, real_keytab
);
1516 ret
= krb5_copy_keyblock (context
, &entry
.keyblock
, key
);
1517 krb5_kt_free_entry(context
, &entry
);
1523 * Set the keytab to use for authentication.
1525 * @param context a Kerberos 5 context.
1526 * @param ctx ctx krb5_init_creds_context context.
1527 * @param keytab the keytab to read the key from.
1529 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1530 * @ingroup krb5_credential
1533 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1534 krb5_init_creds_set_keytab(krb5_context context
,
1535 krb5_init_creds_context ctx
,
1538 krb5_keytab_key_proc_args
*a
;
1539 krb5_keytab_entry entry
;
1540 krb5_kt_cursor cursor
;
1541 krb5_enctype
*etypes
= NULL
;
1542 krb5_error_code ret
;
1544 int kvno
= 0, found
= 0;
1546 a
= malloc(sizeof(*a
));
1548 return krb5_enomem(context
);
1550 a
->principal
= ctx
->cred
.client
;
1553 ctx
->keytab_data
= a
;
1554 ctx
->keyseed
= (void *)a
;
1555 ctx
->keyproc
= keytab_key_proc
;
1558 * We need to the KDC what enctypes we support for this keytab,
1559 * esp if the keytab is really a password based entry, then the
1560 * KDC might have more enctypes in the database then what we have
1564 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
1568 while(krb5_kt_next_entry(context
, keytab
, &entry
, &cursor
) == 0){
1571 if (!krb5_principal_compare(context
, entry
.principal
, ctx
->cred
.client
))
1576 /* check if we ahve this kvno already */
1577 if (entry
.vno
> kvno
) {
1578 /* remove old list of etype */
1584 } else if (entry
.vno
!= kvno
)
1587 /* check if enctype is supported */
1588 if (krb5_enctype_valid(context
, entry
.keyblock
.keytype
) != 0)
1591 /* add enctype to supported list */
1592 ptr
= realloc(etypes
, sizeof(etypes
[0]) * (netypes
+ 2));
1595 ret
= krb5_enomem(context
);
1600 etypes
[netypes
] = entry
.keyblock
.keytype
;
1601 etypes
[netypes
+ 1] = ETYPE_NULL
;
1604 krb5_kt_free_entry(context
, &entry
);
1606 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
1611 ctx
->etypes
= etypes
;
1617 ret
= KRB5_KT_NOTFOUND
;
1618 _krb5_kt_principal_not_found(context
, ret
, keytab
, ctx
->cred
.client
, 0, 0);
1624 static krb5_error_code KRB5_CALLCONV
1625 keyblock_key_proc(krb5_context context
, krb5_enctype enctype
,
1626 krb5_const_pointer keyseed
,
1627 krb5_salt salt
, krb5_data
*s2kparms
,
1628 krb5_keyblock
**key
)
1630 return krb5_copy_keyblock (context
, keyseed
, key
);
1633 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1634 krb5_init_creds_set_keyblock(krb5_context context
,
1635 krb5_init_creds_context ctx
,
1636 krb5_keyblock
*keyblock
)
1638 ctx
->keyseed
= (void *)keyblock
;
1639 ctx
->keyproc
= keyblock_key_proc
;
1644 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1645 krb5_init_creds_set_fast_ccache(krb5_context context
,
1646 krb5_init_creds_context ctx
,
1647 krb5_ccache fast_ccache
)
1649 ctx
->fast_state
.armor_ccache
= fast_ccache
;
1650 ctx
->fast_state
.flags
|= KRB5_FAST_REQUIRED
;
1658 static krb5_error_code
1659 check_fast(krb5_context context
, struct fast_state
*state
)
1661 if (state
->flags
& KRB5_FAST_EXPECTED
) {
1662 krb5_set_error_message(context
, KRB5KRB_AP_ERR_MODIFIED
,
1663 "Expected FAST, but no FAST "
1664 "was in the response from the KDC");
1665 return KRB5KRB_AP_ERR_MODIFIED
;
1671 static krb5_error_code
1672 fast_unwrap_as_rep(krb5_context context
, int32_t nonce
,
1673 krb5_data
*chksumdata
,
1674 struct fast_state
*state
, AS_REP
*rep
)
1676 PA_FX_FAST_REPLY fxfastrep
;
1677 KrbFastResponse fastrep
;
1678 krb5_error_code ret
;
1682 if (state
->armor_crypto
== NULL
|| rep
->padata
== NULL
)
1683 return check_fast(context
, state
);
1685 /* find PA_FX_FAST_REPLY */
1687 pa
= krb5_find_padata(rep
->padata
->val
, rep
->padata
->len
,
1688 KRB5_PADATA_FX_FAST
, &idx
);
1690 return check_fast(context
, state
);
1692 memset(&fxfastrep
, 0, sizeof(fxfastrep
));
1693 memset(&fastrep
, 0, sizeof(fastrep
));
1695 ret
= decode_PA_FX_FAST_REPLY(pa
->padata_value
.data
, pa
->padata_value
.length
, &fxfastrep
, NULL
);
1699 if (fxfastrep
.element
== choice_PA_FX_FAST_REPLY_armored_data
) {
1701 ret
= krb5_decrypt_EncryptedData(context
,
1702 state
->armor_crypto
,
1704 &fxfastrep
.u
.armored_data
.enc_fast_rep
,
1709 ret
= decode_KrbFastResponse(data
.data
, data
.length
, &fastrep
, NULL
);
1710 krb5_data_free(&data
);
1715 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
1719 free_METHOD_DATA(rep
->padata
);
1720 ret
= copy_METHOD_DATA(&fastrep
.padata
, rep
->padata
);
1724 if (fastrep
.strengthen_key
) {
1725 krb5_keyblock result
;
1727 ret
= _krb5_fast_cf2(context
,
1728 fastrep
.strengthen_key
,
1737 krb5_free_keyblock_contents(context
, state
->reply_key
);
1738 *state
->reply_key
= result
;
1741 if (nonce
!= fastrep
.nonce
) {
1742 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
1745 if (fastrep
.finished
) {
1746 PrincipalName cname
;
1747 krb5_realm crealm
= NULL
;
1749 if (chksumdata
== NULL
) {
1750 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
1754 ret
= krb5_verify_checksum(context
, state
->armor_crypto
,
1755 KRB5_KU_FAST_FINISHED
,
1756 chksumdata
->data
, chksumdata
->length
,
1757 &fastrep
.finished
->ticket_checksum
);
1762 ret
= copy_Realm(&fastrep
.finished
->crealm
, &crealm
);
1765 free_Realm(&rep
->crealm
);
1766 rep
->crealm
= crealm
;
1768 ret
= copy_PrincipalName(&fastrep
.finished
->cname
, &cname
);
1771 free_PrincipalName(&rep
->cname
);
1774 #if 0 /* store authenticated checksum as kdc-offset */
1775 fastrep
->finished
.timestamp
;
1776 fastrep
->finished
.usec
= 0;
1779 } else if (chksumdata
) {
1780 /* expected fastrep.finish but didn't get it */
1781 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
1785 free_PA_FX_FAST_REPLY(&fxfastrep
);
1790 static krb5_error_code
1791 fast_unwrap_error(krb5_context context
, struct fast_state
*state
, KRB_ERROR
*error
)
1793 if (state
->armor_crypto
== NULL
)
1794 return check_fast(context
, state
);
1799 static krb5_error_code
1800 make_fast_ap_fxarmor(krb5_context context
,
1801 struct fast_state
*state
,
1802 KrbFastArmor
**armor
)
1804 KrbFastArmor
*fxarmor
= NULL
;
1805 krb5_auth_context auth_context
= NULL
;
1806 krb5_creds cred
, *credp
= NULL
;
1807 krb5_error_code ret
;
1810 if (fxarmor
== NULL
) {
1815 fxarmor
->armor_type
= 1;
1817 memset(&cred
, 0, sizeof(cred
));
1819 ret
= krb5_auth_con_init (context
, &auth_context
);
1823 ret
= krb5_cc_get_principal(context
, state
->armor_ccache
, &cred
.client
);
1827 ret
= krb5_make_principal(context
, &cred
.server
,
1833 krb5_free_principal(context
, cred
.client
);
1837 ret
= krb5_get_credentials(context
, 0, state
->armor_ccache
, &cred
, &credp
);
1838 krb5_free_principal(context
, cred
.server
);
1839 krb5_free_principal(context
, cred
.client
);
1843 ret
= krb5_mk_req_extended(context
,
1848 &fxarmor
->armor_value
);
1849 krb5_free_creds(context
, credp
);
1853 if (state
->armor_crypto
)
1854 krb5_crypto_destroy(context
, state
->armor_crypto
);
1855 krb5_free_keyblock_contents(context
, &state
->armor_key
);
1857 ret
= _krb5_fast_armor_key(context
,
1858 auth_context
->local_subkey
,
1859 auth_context
->keyblock
,
1861 &state
->armor_crypto
);
1869 free_KrbFastArmor(fxarmor
);
1874 static krb5_error_code
1875 fast_wrap_req(krb5_context context
, struct fast_state
*state
, KDC_REQ
*req
)
1877 KrbFastArmor
*fxarmor
= NULL
;
1878 PA_FX_FAST_REQUEST fxreq
;
1879 krb5_error_code ret
;
1884 if (state
->flags
& KRB5_FAST_DISABLED
) {
1885 _krb5_debug(context
, 10, "fast disabled, not doing any fast wrapping");
1889 memset(&fxreq
, 0, sizeof(fxreq
));
1890 memset(&fastreq
, 0, sizeof(fastreq
));
1891 krb5_data_zero(&data
);
1893 if (state
->armor_crypto
== NULL
) {
1894 if (state
->armor_ccache
) {
1896 * Instead of keeping state in FX_COOKIE in the KDC, we
1897 * rebuild a new armor key for every request, because this
1898 * is what the MIT KDC expect and RFC6113 is vage about
1899 * what the behavior should be.
1901 state
->type
= choice_PA_FX_FAST_REQUEST_armored_data
;
1903 return check_fast(context
, state
);
1907 state
->flags
|= KRB5_FAST_EXPECTED
;
1909 fastreq
.fast_options
.hide_client_names
= 1;
1911 ret
= copy_KDC_REQ_BODY(&req
->req_body
, &fastreq
.req_body
);
1912 free_KDC_REQ_BODY(&req
->req_body
);
1914 req
->req_body
.realm
= strdup(KRB5_ANON_REALM
);
1915 ALLOC(req
->req_body
.cname
, 1);
1916 req
->req_body
.cname
->name_type
= KRB5_NT_PRINCIPAL
;
1917 ALLOC(req
->req_body
.cname
->name_string
.val
, 2);
1918 req
->req_body
.cname
->name_string
.len
= 2;
1919 req
->req_body
.cname
->name_string
.val
[0] = strdup(KRB5_WELLKNOWN_NAME
);
1920 req
->req_body
.cname
->name_string
.val
[1] = strdup(KRB5_ANON_NAME
);
1923 ret
= copy_METHOD_DATA(req
->padata
, &fastreq
.padata
);
1924 free_METHOD_DATA(req
->padata
);
1926 ALLOC(req
->padata
, 1);
1930 ASN1_MALLOC_ENCODE(KrbFastReq
, data
.data
, data
.length
, &fastreq
, &size
, ret
);
1933 heim_assert(data
.length
== size
, "ASN.1 internal error");
1935 fxreq
.element
= state
->type
;
1937 if (state
->type
== choice_PA_FX_FAST_REQUEST_armored_data
) {
1941 ret
= make_fast_ap_fxarmor(context
, state
, &fxreq
.u
.armored_data
.armor
);
1945 heim_assert(state
->armor_crypto
!= NULL
, "FAST armor key missing when FAST started");
1947 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, len
, &req
->req_body
, &size
, ret
);
1950 heim_assert(len
== size
, "ASN.1 internal error");
1952 ret
= krb5_create_checksum(context
, state
->armor_crypto
,
1953 KRB5_KU_FAST_REQ_CHKSUM
, 0,
1955 &fxreq
.u
.armored_data
.req_checksum
);
1960 ret
= krb5_encrypt_EncryptedData(context
, state
->armor_crypto
,
1965 &fxreq
.u
.armored_data
.enc_fast_req
);
1966 krb5_data_free(&data
);
1969 krb5_data_free(&data
);
1970 heim_assert(false, "unknown FAST type, internal error");
1973 ASN1_MALLOC_ENCODE(PA_FX_FAST_REQUEST
, data
.data
, data
.length
, &fxreq
, &size
, ret
);
1976 heim_assert(data
.length
== size
, "ASN.1 internal error");
1979 ret
= krb5_padata_add(context
, req
->padata
, KRB5_PADATA_FX_FAST
, data
.data
, data
.length
);
1982 krb5_data_zero(&data
);
1985 free_PA_FX_FAST_REQUEST(&fxreq
);
1987 free_KrbFastArmor(fxarmor
);
1990 krb5_data_free(&data
);
1997 * The core loop if krb5_get_init_creds() function family. Create the
1998 * packets and have the caller send them off to the KDC.
2000 * If the caller want all work been done for them, use
2001 * krb5_init_creds_get() instead.
2003 * @param context a Kerberos 5 context.
2004 * @param ctx ctx krb5_init_creds_context context.
2005 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
2006 * @param out reply to KDC.
2007 * @param hostinfo KDC address info, first round it can be NULL.
2008 * @param flags status of the round, if
2009 * KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round.
2011 * @return 0 for success, or an Kerberos 5 error code, see
2012 * krb5_get_error_message().
2014 * @ingroup krb5_credential
2017 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2018 krb5_init_creds_step(krb5_context context
,
2019 krb5_init_creds_context ctx
,
2022 krb5_krbhst_info
*hostinfo
,
2023 unsigned int *flags
)
2025 krb5_error_code ret
;
2030 krb5_data_zero(out
);
2032 if (ctx
->as_req
.req_body
.cname
== NULL
) {
2033 ret
= init_as_req(context
, ctx
->flags
, &ctx
->cred
,
2034 ctx
->addrs
, ctx
->etypes
, &ctx
->as_req
);
2036 free_init_creds_ctx(context
, ctx
);
2041 #define MAX_PA_COUNTER 10
2042 if (ctx
->pa_counter
> MAX_PA_COUNTER
) {
2043 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
2044 N_("Looping %d times while getting "
2045 "initial credentials", ""),
2047 return KRB5_GET_IN_TKT_LOOP
;
2051 _krb5_debug(context
, 5, "krb5_get_init_creds: loop %d", ctx
->pa_counter
);
2053 /* Lets process the input packet */
2054 if (in
&& in
->length
) {
2057 memset(&rep
, 0, sizeof(rep
));
2059 _krb5_debug(context
, 5, "krb5_get_init_creds: processing input");
2061 ret
= decode_AS_REP(in
->data
, in
->length
, &rep
.kdc_rep
, &size
);
2063 unsigned eflags
= EXTRACT_TICKET_AS_REQ
| EXTRACT_TICKET_TIMESYNC
;
2069 ASN1_MALLOC_ENCODE(Ticket
, data
.data
, data
.length
,
2070 &rep
.kdc_rep
.ticket
, &size
, ret
);
2073 heim_assert(data
.length
== size
, "ASN.1 internal error");
2075 ret
= fast_unwrap_as_rep(context
, ctx
->nonce
, &data
,
2076 &ctx
->fast_state
, &rep
.kdc_rep
);
2077 krb5_data_free(&data
);
2082 * Now check and extract the ticket
2085 if (ctx
->flags
.canonicalize
) {
2086 eflags
|= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH
;
2087 eflags
|= EXTRACT_TICKET_MATCH_REALM
;
2089 if (ctx
->ic_flags
& KRB5_INIT_CREDS_NO_C_CANON_CHECK
)
2090 eflags
|= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH
;
2092 ret
= process_pa_data_to_key(context
, ctx
, &ctx
->cred
,
2093 &ctx
->as_req
, &rep
.kdc_rep
,
2094 hostinfo
, &ctx
->fast_state
.reply_key
);
2096 free_AS_REP(&rep
.kdc_rep
);
2100 _krb5_debug(context
, 5, "krb5_get_init_creds: extracting ticket");
2102 ret
= _krb5_extract_ticket(context
,
2105 ctx
->fast_state
.reply_key
,
2107 KRB5_KU_AS_REP_ENC_PART
,
2115 ret
= copy_EncKDCRepPart(&rep
.enc_part
, &ctx
->enc_part
);
2117 krb5_free_keyblock(context
, ctx
->fast_state
.reply_key
);
2118 ctx
->fast_state
.reply_key
= NULL
;
2121 free_AS_REP(&rep
.kdc_rep
);
2122 free_EncASRepPart(&rep
.enc_part
);
2127 /* let's try to parse it as a KRB-ERROR */
2129 _krb5_debug(context
, 5, "krb5_get_init_creds: got an error");
2131 free_KRB_ERROR(&ctx
->error
);
2133 ret
= krb5_rd_error(context
, in
, &ctx
->error
);
2134 if(ret
&& in
->length
&& ((char*)in
->data
)[0] == 4)
2135 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
2137 _krb5_debug(context
, 5, "krb5_get_init_creds: failed to read error");
2144 ret
= fast_unwrap_error(context
, &ctx
->fast_state
, &ctx
->error
);
2152 ret
= krb5_error_from_rd_error(context
, &ctx
->error
, &ctx
->cred
);
2154 _krb5_debug(context
, 5, "krb5_get_init_creds: KRB-ERROR %d", ret
);
2157 * If no preauth was set and KDC requires it, give it one
2161 if (ret
== KRB5KDC_ERR_PREAUTH_REQUIRED
) {
2163 free_METHOD_DATA(&ctx
->md
);
2164 memset(&ctx
->md
, 0, sizeof(ctx
->md
));
2166 if (ctx
->error
.e_data
) {
2167 ret
= decode_METHOD_DATA(ctx
->error
.e_data
->data
,
2168 ctx
->error
.e_data
->length
,
2172 krb5_set_error_message(context
, ret
,
2173 N_("Failed to decode METHOD-DATA", ""));
2175 krb5_set_error_message(context
, ret
,
2176 N_("Preauth required but no preauth "
2177 "options send by KDC", ""));
2179 } else if (ret
== KRB5KRB_AP_ERR_SKEW
&& context
->kdc_sec_offset
== 0) {
2181 * Try adapt to timeskrew when we are using pre-auth, and
2182 * if there was a time skew, try again.
2184 krb5_set_real_time(context
, ctx
->error
.stime
, -1);
2185 if (context
->kdc_sec_offset
)
2188 _krb5_debug(context
, 10, "init_creds: err skew updateing kdc offset to %d",
2189 context
->kdc_sec_offset
);
2191 ctx
->used_pa_types
= 0;
2193 } else if (ret
== KRB5_KDC_ERR_WRONG_REALM
&& ctx
->flags
.canonicalize
) {
2194 /* client referal to a new realm */
2196 if (ctx
->error
.crealm
== NULL
) {
2197 krb5_set_error_message(context
, ret
,
2198 N_("Got a client referral, not but no realm", ""));
2201 _krb5_debug(context
, 5,
2202 "krb5_get_init_creds: got referal to realm %s",
2203 *ctx
->error
.crealm
);
2205 ret
= krb5_principal_set_realm(context
,
2207 *ctx
->error
.crealm
);
2209 ctx
->used_pa_types
= 0;
2210 } else if (ret
== KRB5KDC_ERR_PREAUTH_FAILED
) {
2212 if (ctx
->fast_state
.flags
& KRB5_FAST_DISABLED
)
2214 if (ctx
->fast_state
.flags
& (KRB5_FAST_REQUIRED
| KRB5_FAST_EXPECTED
))
2217 _krb5_debug(context
, 10, "preauth failed with FAST, "
2218 "and told by KD or user, trying w/o FAST");
2220 ctx
->fast_state
.flags
|= KRB5_FAST_DISABLED
;
2221 ctx
->used_pa_types
= 0;
2229 if (ctx
->as_req
.padata
) {
2230 free_METHOD_DATA(ctx
->as_req
.padata
);
2231 free(ctx
->as_req
.padata
);
2232 ctx
->as_req
.padata
= NULL
;
2235 /* Set a new nonce. */
2236 ctx
->as_req
.req_body
.nonce
= ctx
->nonce
;
2238 /* fill_in_md_data */
2239 ret
= process_pa_data_to_md(context
, &ctx
->cred
, &ctx
->as_req
, ctx
,
2240 &ctx
->md
, &ctx
->as_req
.padata
,
2241 ctx
->prompter
, ctx
->prompter_data
);
2248 copy_AS_REQ(&ctx
->as_req
, &req2
);
2250 ret
= fast_wrap_req(context
, &ctx
->fast_state
, &req2
);
2256 krb5_data_free(&ctx
->req_buffer
);
2258 ASN1_MALLOC_ENCODE(AS_REQ
,
2259 ctx
->req_buffer
.data
, ctx
->req_buffer
.length
,
2264 if(len
!= ctx
->req_buffer
.length
)
2265 krb5_abortx(context
, "internal error in ASN.1 encoder");
2267 out
->data
= ctx
->req_buffer
.data
;
2268 out
->length
= ctx
->req_buffer
.length
;
2270 *flags
= KRB5_INIT_CREDS_STEP_FLAG_CONTINUE
;
2278 * Extract the newly acquired credentials from krb5_init_creds_context
2281 * @param context A Kerberos 5 context.
2283 * @param cred credentials, free with krb5_free_cred_contents().
2285 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
2288 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2289 krb5_init_creds_get_creds(krb5_context context
,
2290 krb5_init_creds_context ctx
,
2293 return krb5_copy_creds_contents(context
, &ctx
->cred
, cred
);
2297 * Get the last error from the transaction.
2299 * @return Returns 0 or an error code
2301 * @ingroup krb5_credential
2304 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2305 krb5_init_creds_get_error(krb5_context context
,
2306 krb5_init_creds_context ctx
,
2309 krb5_error_code ret
;
2311 ret
= copy_KRB_ERROR(&ctx
->error
, error
);
2313 krb5_enomem(context
);
2320 * @ingroup krb5_credential
2324 krb5_init_creds_store(krb5_context context
,
2325 krb5_init_creds_context ctx
,
2328 krb5_error_code ret
;
2330 if (ctx
->cred
.client
== NULL
) {
2331 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
;
2332 krb5_set_error_message(context
, ret
, "init creds not completed yet");
2336 ret
= krb5_cc_initialize(context
, id
, ctx
->cred
.client
);
2340 ret
= krb5_cc_store_cred(context
, id
, &ctx
->cred
);
2344 if (ctx
->cred
.flags
.b
.enc_pa_rep
) {
2345 krb5_data data
= { 3, rk_UNCONST("yes") };
2346 ret
= krb5_cc_set_config(context
, id
, ctx
->cred
.server
,
2347 "fast_avail", &data
);
2356 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
2358 * @param context A Kerberos 5 context.
2359 * @param ctx The krb5_init_creds_context to free.
2361 * @ingroup krb5_credential
2364 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
2365 krb5_init_creds_free(krb5_context context
,
2366 krb5_init_creds_context ctx
)
2368 free_init_creds_ctx(context
, ctx
);
2373 * Get new credentials as setup by the krb5_init_creds_context.
2375 * @param context A Kerberos 5 context.
2376 * @param ctx The krb5_init_creds_context to process.
2378 * @ingroup krb5_credential
2381 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2382 krb5_init_creds_get(krb5_context context
, krb5_init_creds_context ctx
)
2384 krb5_sendto_ctx stctx
= NULL
;
2385 krb5_krbhst_info
*hostinfo
= NULL
;
2386 krb5_error_code ret
;
2388 unsigned int flags
= 0;
2390 krb5_data_zero(&in
);
2391 krb5_data_zero(&out
);
2393 ret
= krb5_sendto_ctx_alloc(context
, &stctx
);
2396 krb5_sendto_ctx_set_func(stctx
, _krb5_kdc_retry
, NULL
);
2400 ret
= krb5_init_creds_step(context
, ctx
, &in
, &out
, hostinfo
, &flags
);
2401 krb5_data_free(&in
);
2405 if ((flags
& 1) == 0)
2408 ret
= krb5_sendto_context (context
, stctx
, &out
,
2409 ctx
->cred
.client
->realm
, &in
);
2417 krb5_sendto_ctx_free(context
, stctx
);
2423 * Get new credentials using password.
2425 * @ingroup krb5_credential
2429 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2430 krb5_get_init_creds_password(krb5_context context
,
2432 krb5_principal client
,
2433 const char *password
,
2434 krb5_prompter_fct prompter
,
2436 krb5_deltat start_time
,
2437 const char *in_tkt_service
,
2438 krb5_get_init_creds_opt
*options
)
2440 krb5_init_creds_context ctx
;
2441 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
2442 krb5_error_code ret
;
2446 ret
= krb5_init_creds_init(context
, client
, prompter
, data
, start_time
, options
, &ctx
);
2450 ret
= krb5_init_creds_set_service(context
, ctx
, in_tkt_service
);
2454 if (prompter
!= NULL
&& ctx
->password
== NULL
&& password
== NULL
) {
2456 krb5_data password_data
;
2460 ret
= krb5_unparse_name(context
, client
, &p
);
2464 aret
= asprintf(&q
, "%s's Password: ", p
);
2466 if (aret
== -1 || q
== NULL
) {
2467 ret
= krb5_enomem(context
);
2471 password_data
.data
= buf
;
2472 password_data
.length
= sizeof(buf
);
2474 prompt
.reply
= &password_data
;
2475 prompt
.type
= KRB5_PROMPT_TYPE_PASSWORD
;
2477 ret
= (*prompter
) (context
, data
, NULL
, NULL
, 1, &prompt
);
2480 memset (buf
, 0, sizeof(buf
));
2481 ret
= KRB5_LIBOS_PWDINTR
;
2482 krb5_clear_error_message (context
);
2485 password
= password_data
.data
;
2489 ret
= krb5_init_creds_set_password(context
, ctx
, password
);
2494 ret
= krb5_init_creds_get(context
, ctx
);
2497 krb5_process_last_request(context
, options
, ctx
);
2500 if (ret
== KRB5KDC_ERR_KEY_EXPIRED
&& chpw
== 0) {
2501 /* try to avoid recursion */
2502 if (in_tkt_service
!= NULL
&& strcmp(in_tkt_service
, "kadmin/changepw") == 0)
2505 /* don't try to change password where then where none */
2506 if (prompter
== NULL
)
2509 ret
= change_password (context
,
2521 krb5_init_creds_free(context
, ctx
);
2527 krb5_init_creds_get_creds(context
, ctx
, creds
);
2530 krb5_init_creds_free(context
, ctx
);
2532 memset(buf
, 0, sizeof(buf
));
2533 memset(buf2
, 0, sizeof(buf2
));
2538 * Get new credentials using keyblock.
2540 * @ingroup krb5_credential
2543 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2544 krb5_get_init_creds_keyblock(krb5_context context
,
2546 krb5_principal client
,
2547 krb5_keyblock
*keyblock
,
2548 krb5_deltat start_time
,
2549 const char *in_tkt_service
,
2550 krb5_get_init_creds_opt
*options
)
2552 krb5_init_creds_context ctx
;
2553 krb5_error_code ret
;
2555 memset(creds
, 0, sizeof(*creds
));
2557 ret
= krb5_init_creds_init(context
, client
, NULL
, NULL
, start_time
, options
, &ctx
);
2561 ret
= krb5_init_creds_set_service(context
, ctx
, in_tkt_service
);
2565 ret
= krb5_init_creds_set_keyblock(context
, ctx
, keyblock
);
2569 ret
= krb5_init_creds_get(context
, ctx
);
2572 krb5_process_last_request(context
, options
, ctx
);
2576 krb5_init_creds_get_creds(context
, ctx
, creds
);
2579 krb5_init_creds_free(context
, ctx
);
2585 * Get new credentials using keytab.
2587 * @ingroup krb5_credential
2590 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2591 krb5_get_init_creds_keytab(krb5_context context
,
2593 krb5_principal client
,
2595 krb5_deltat start_time
,
2596 const char *in_tkt_service
,
2597 krb5_get_init_creds_opt
*options
)
2599 krb5_init_creds_context ctx
;
2600 krb5_error_code ret
;
2602 memset(creds
, 0, sizeof(*creds
));
2604 ret
= krb5_init_creds_init(context
, client
, NULL
, NULL
, start_time
, options
, &ctx
);
2608 ret
= krb5_init_creds_set_service(context
, ctx
, in_tkt_service
);
2612 ret
= krb5_init_creds_set_keytab(context
, ctx
, keytab
);
2616 ret
= krb5_init_creds_get(context
, ctx
);
2618 krb5_process_last_request(context
, options
, ctx
);
2622 krb5_init_creds_get_creds(context
, ctx
, creds
);
2625 krb5_init_creds_free(context
, ctx
);