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"
38 typedef struct krb5_get_init_creds_ctx {
41 krb5_addresses *addrs;
43 krb5_preauthtype *pre_auth_types;
44 const char *in_tkt_service;
53 krb5_s2k_proc key_proc;
55 krb5_get_init_creds_tristate req_pac;
57 krb5_pk_init_ctx pk_init_ctx;
59 } krb5_get_init_creds_ctx;
61 static krb5_error_code
62 default_s2k_func(krb5_context context, krb5_enctype type,
63 krb5_const_pointer keyseed,
64 krb5_salt salt, krb5_data *s2kparms,
71 password.data = rk_UNCONST(keyseed);
72 password.length = strlen(keyseed);
76 krb5_data_zero(&opaque);
78 *key = malloc(sizeof(**key));
81 ret = krb5_string_to_key_data_salt_opaque(context, type, password,
91 free_init_creds_ctx(krb5_context context, krb5_get_init_creds_ctx *ctx)
95 if (ctx->pre_auth_types)
96 free (ctx->pre_auth_types);
97 free_AS_REQ(&ctx->as_req);
98 memset(&ctx->as_req, 0, sizeof(ctx->as_req));
102 get_config_time (krb5_context context,
109 ret = krb5_config_get_time (context, NULL,
116 ret = krb5_config_get_time (context, NULL,
125 static krb5_error_code
126 init_cred (krb5_context context,
128 krb5_principal client,
129 krb5_deltat start_time,
130 const char *in_tkt_service,
131 krb5_get_init_creds_opt *options)
134 krb5_const_realm client_realm;
138 krb5_timeofday (context, &now);
140 memset (cred, 0, sizeof(*cred));
143 krb5_copy_principal(context, client, &cred->client);
145 ret = krb5_get_default_principal (context,
151 client_realm = krb5_principal_get_realm (context, cred->client);
154 cred->times.starttime = now + start_time;
156 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
157 tmp = options->tkt_life;
160 cred->times.endtime = now + tmp;
162 if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE) &&
163 options->renew_life > 0) {
164 cred->times.renew_till = now + options->renew_life;
167 if (in_tkt_service) {
168 ret = krb5_parse_name (context, in_tkt_service, &cred->server);
171 krb5_principal_set_realm (context, cred->server, client_realm);
173 ret = krb5_make_principal(context, &cred->server,
174 client_realm, KRB5_TGS_NAME, client_realm,
182 krb5_free_cred_contents (context, cred);
187 * Print a message (str) to the user about the expiration in `lr'
191 report_expiration (krb5_context context,
192 krb5_prompter_fct prompter,
199 asprintf (&p, "%s%s", str, ctime(&now));
200 (*prompter) (context, data, NULL, p, 0, NULL);
205 * Parse the last_req data and show it to the user if it's interesting
209 print_expire (krb5_context context,
210 krb5_const_realm realm,
212 krb5_prompter_fct prompter,
216 LastReq *lr = &rep->enc_part.last_req;
219 krb5_boolean reported = FALSE;
221 krb5_timeofday (context, &sec);
223 t = sec + get_config_time (context,
228 for (i = 0; i < lr->len; ++i) {
229 if (lr->val[i].lr_value <= t) {
230 switch (abs(lr->val[i].lr_type)) {
232 report_expiration(context, prompter, data,
233 "Your password will expire at ",
234 lr->val[i].lr_value);
237 case LR_ACCT_EXPTIME :
238 report_expiration(context, prompter, data,
239 "Your account will expire at ",
240 lr->val[i].lr_value);
248 && rep->enc_part.key_expiration
249 && *rep->enc_part.key_expiration <= t) {
250 report_expiration(context, prompter, data,
251 "Your password/account will expire at ",
252 *rep->enc_part.key_expiration);
256 static krb5_addresses no_addrs = { 0, NULL };
258 static krb5_error_code
259 get_init_creds_common(krb5_context context,
260 krb5_principal client,
261 krb5_deltat start_time,
262 const char *in_tkt_service,
263 krb5_get_init_creds_opt *options,
264 krb5_get_init_creds_ctx *ctx)
266 krb5_get_init_creds_opt default_opt;
268 krb5_enctype *etypes;
269 krb5_preauthtype *pre_auth_types;
271 memset(ctx, 0, sizeof(*ctx));
273 if (options == NULL) {
274 krb5_get_init_creds_opt_init (&default_opt);
275 options = &default_opt;
277 _krb5_get_init_creds_opt_free_krb5_error(options);
280 if (options->opt_private) {
281 ctx->password = options->opt_private->password;
282 ctx->key_proc = options->opt_private->key_proc;
283 ctx->req_pac = options->opt_private->req_pac;
284 ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
285 ctx->ic_flags = options->opt_private->flags;
287 ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;
289 if (ctx->key_proc == NULL)
290 ctx->key_proc = default_s2k_func;
292 if (ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE)
293 ctx->flags.canonicalize = 1;
295 ctx->pre_auth_types = NULL;
298 ctx->pre_auth_types = NULL;
299 ctx->in_tkt_service = in_tkt_service;
301 ret = init_cred (context, &ctx->cred, client, start_time,
302 in_tkt_service, options);
306 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
307 ctx->flags.forwardable = options->forwardable;
309 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
310 ctx->flags.proxiable = options->proxiable;
313 ctx->flags.postdated = 1;
314 if (ctx->cred.times.renew_till)
315 ctx->flags.renewable = 1;
316 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
317 ctx->addrs = options->address_list;
318 } else if (options->opt_private) {
319 switch (options->opt_private->addressless) {
320 case KRB5_INIT_CREDS_TRISTATE_UNSET:
321 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
322 ctx->addrs = &no_addrs;
327 case KRB5_INIT_CREDS_TRISTATE_FALSE:
330 case KRB5_INIT_CREDS_TRISTATE_TRUE:
331 ctx->addrs = &no_addrs;
335 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
336 etypes = malloc((options->etype_list_length + 1)
337 * sizeof(krb5_enctype));
338 if (etypes == NULL) {
339 krb5_set_error_string(context, "malloc: out of memory");
342 memcpy (etypes, options->etype_list,
343 options->etype_list_length * sizeof(krb5_enctype));
344 etypes[options->etype_list_length] = ETYPE_NULL;
345 ctx->etypes = etypes;
347 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
348 pre_auth_types = malloc((options->preauth_list_length + 1)
349 * sizeof(krb5_preauthtype));
350 if (pre_auth_types == NULL) {
351 krb5_set_error_string(context, "malloc: out of memory");
354 memcpy (pre_auth_types, options->preauth_list,
355 options->preauth_list_length * sizeof(krb5_preauthtype));
356 pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
357 ctx->pre_auth_types = pre_auth_types;
359 if (options->flags & KRB5_GET_INIT_CREDS_OPT_SALT)
361 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
362 ctx->flags.request_anonymous = options->anonymous;
366 static krb5_error_code
367 change_password (krb5_context context,
368 krb5_principal client,
369 const char *password,
372 krb5_prompter_fct prompter,
374 krb5_get_init_creds_opt *old_options)
376 krb5_prompt prompts[2];
379 char buf1[BUFSIZ], buf2[BUFSIZ];
380 krb5_data password_data[2];
382 krb5_data result_code_string;
383 krb5_data result_string;
385 krb5_get_init_creds_opt options;
387 memset (&cpw_cred, 0, sizeof(cpw_cred));
389 krb5_get_init_creds_opt_init (&options);
390 krb5_get_init_creds_opt_set_tkt_life (&options, 60);
391 krb5_get_init_creds_opt_set_forwardable (&options, FALSE);
392 krb5_get_init_creds_opt_set_proxiable (&options, FALSE);
393 if (old_options && old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
394 krb5_get_init_creds_opt_set_preauth_list (&options,
395 old_options->preauth_list,
396 old_options->preauth_list_length);
398 krb5_data_zero (&result_code_string);
399 krb5_data_zero (&result_string);
401 ret = krb5_get_init_creds_password (context,
414 password_data[0].data = buf1;
415 password_data[0].length = sizeof(buf1);
417 prompts[0].hidden = 1;
418 prompts[0].prompt = "New password: ";
419 prompts[0].reply = &password_data[0];
420 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
422 password_data[1].data = buf2;
423 password_data[1].length = sizeof(buf2);
425 prompts[1].hidden = 1;
426 prompts[1].prompt = "Repeat new password: ";
427 prompts[1].reply = &password_data[1];
428 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
430 ret = (*prompter) (context, data, NULL, "Changing password",
433 memset (buf1, 0, sizeof(buf1));
434 memset (buf2, 0, sizeof(buf2));
438 if (strcmp (buf1, buf2) == 0)
440 memset (buf1, 0, sizeof(buf1));
441 memset (buf2, 0, sizeof(buf2));
444 ret = krb5_change_password (context,
452 asprintf (&p, "%s: %.*s\n",
453 result_code ? "Error" : "Success",
454 (int)result_string.length,
455 result_string.length > 0 ? (char*)result_string.data : "");
457 ret = (*prompter) (context, data, NULL, p, 0, NULL);
459 if (result_code == 0) {
460 strlcpy (newpw, buf1, newpw_sz);
463 krb5_set_error_string (context, "failed changing password");
468 memset (buf1, 0, sizeof(buf1));
469 memset (buf2, 0, sizeof(buf2));
470 krb5_data_free (&result_string);
471 krb5_data_free (&result_code_string);
472 krb5_free_cred_contents (context, &cpw_cred);
476 krb5_error_code KRB5_LIB_FUNCTION
477 krb5_keyblock_key_proc (krb5_context context,
480 krb5_const_pointer keyseed,
483 return krb5_copy_keyblock (context, keyseed, key);
486 krb5_error_code KRB5_LIB_FUNCTION
487 krb5_get_init_creds_keytab(krb5_context context,
489 krb5_principal client,
491 krb5_deltat start_time,
492 const char *in_tkt_service,
493 krb5_get_init_creds_opt *options)
495 krb5_get_init_creds_ctx ctx;
497 krb5_keytab_key_proc_args *a;
499 ret = get_init_creds_common(context, client, start_time,
500 in_tkt_service, options, &ctx);
504 a = malloc (sizeof(*a));
506 krb5_set_error_string(context, "malloc: out of memory");
510 a->principal = ctx.cred.client;
513 ret = krb5_get_in_cred (context,
514 KDCOptions2int(ctx.flags),
519 krb5_keytab_key_proc,
527 if (ret == 0 && creds)
530 krb5_free_cred_contents (context, &ctx.cred);
533 free_init_creds_ctx(context, &ctx);
541 static krb5_error_code
542 init_creds_init_as_req (krb5_context context,
544 const krb5_creds *creds,
545 const krb5_addresses *addrs,
546 const krb5_enctype *etypes,
551 memset(a, 0, sizeof(*a));
554 a->msg_type = krb_as_req;
555 a->req_body.kdc_options = opts;
556 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
557 if (a->req_body.cname == NULL) {
559 krb5_set_error_string(context, "malloc: out of memory");
562 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
563 if (a->req_body.sname == NULL) {
565 krb5_set_error_string(context, "malloc: out of memory");
569 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
572 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
576 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
580 if(creds->times.starttime) {
581 a->req_body.from = malloc(sizeof(*a->req_body.from));
582 if (a->req_body.from == NULL) {
584 krb5_set_error_string(context, "malloc: out of memory");
587 *a->req_body.from = creds->times.starttime;
589 if(creds->times.endtime){
590 ALLOC(a->req_body.till, 1);
591 *a->req_body.till = creds->times.endtime;
593 if(creds->times.renew_till){
594 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
595 if (a->req_body.rtime == NULL) {
597 krb5_set_error_string(context, "malloc: out of memory");
600 *a->req_body.rtime = creds->times.renew_till;
602 a->req_body.nonce = 0;
603 ret = krb5_init_etype (context,
604 &a->req_body.etype.len,
605 &a->req_body.etype.val,
611 * This means no addresses
614 if (addrs && addrs->len == 0) {
615 a->req_body.addresses = NULL;
617 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
618 if (a->req_body.addresses == NULL) {
620 krb5_set_error_string(context, "malloc: out of memory");
625 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
627 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
628 if(ret == 0 && a->req_body.addresses->len == 0) {
629 free(a->req_body.addresses);
630 a->req_body.addresses = NULL;
637 a->req_body.enc_authorization_data = NULL;
638 a->req_body.additional_tickets = NULL;
645 memset(a, 0, sizeof(*a));
649 struct pa_info_data {
652 krb5_data *s2kparams;
656 free_paid(krb5_context context, struct pa_info_data *ppaid)
658 krb5_free_salt(context, ppaid->salt);
659 if (ppaid->s2kparams)
660 krb5_free_data(context, ppaid->s2kparams);
664 static krb5_error_code
665 set_paid(struct pa_info_data *paid, krb5_context context,
667 krb5_salttype salttype, void *salt_string, size_t salt_len,
668 krb5_data *s2kparams)
671 paid->salt.salttype = salttype;
672 paid->salt.saltvalue.data = malloc(salt_len + 1);
673 if (paid->salt.saltvalue.data == NULL) {
674 krb5_clear_error_string(context);
677 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
678 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
679 paid->salt.saltvalue.length = salt_len;
683 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
685 krb5_clear_error_string(context);
686 krb5_free_salt(context, paid->salt);
690 paid->s2kparams = NULL;
695 static struct pa_info_data *
696 pa_etype_info2(krb5_context context,
697 const krb5_principal client,
699 struct pa_info_data *paid,
700 heim_octet_string *data)
707 memset(&e, 0, sizeof(e));
708 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
713 for (j = 0; j < asreq->req_body.etype.len; j++) {
714 for (i = 0; i < e.len; i++) {
715 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
717 if (e.val[i].salt == NULL)
718 ret = krb5_get_pw_salt(context, client, &salt);
720 salt.saltvalue.data = *e.val[i].salt;
721 salt.saltvalue.length = strlen(*e.val[i].salt);
725 ret = set_paid(paid, context, e.val[i].etype,
728 salt.saltvalue.length,
730 if (e.val[i].salt == NULL)
731 krb5_free_salt(context, salt);
733 free_ETYPE_INFO2(&e);
740 free_ETYPE_INFO2(&e);
744 static struct pa_info_data *
745 pa_etype_info(krb5_context context,
746 const krb5_principal client,
748 struct pa_info_data *paid,
749 heim_octet_string *data)
756 memset(&e, 0, sizeof(e));
757 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
762 for (j = 0; j < asreq->req_body.etype.len; j++) {
763 for (i = 0; i < e.len; i++) {
764 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
766 salt.salttype = KRB5_PW_SALT;
767 if (e.val[i].salt == NULL)
768 ret = krb5_get_pw_salt(context, client, &salt);
770 salt.saltvalue = *e.val[i].salt;
773 if (e.val[i].salttype)
774 salt.salttype = *e.val[i].salttype;
776 ret = set_paid(paid, context, e.val[i].etype,
779 salt.saltvalue.length,
781 if (e.val[i].salt == NULL)
782 krb5_free_salt(context, salt);
796 static struct pa_info_data *
797 pa_pw_or_afs3_salt(krb5_context context,
798 const krb5_principal client,
800 struct pa_info_data *paid,
801 heim_octet_string *data)
804 if (paid->etype == ENCTYPE_NULL)
806 ret = set_paid(paid, context,
819 krb5_preauthtype type;
820 struct pa_info_data *(*salt_info)(krb5_context,
821 const krb5_principal,
823 struct pa_info_data *,
824 heim_octet_string *);
827 static struct pa_info pa_prefs[] = {
828 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
829 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
830 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
831 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
835 find_pa_data(const METHOD_DATA *md, int type)
840 for (i = 0; i < md->len; i++)
841 if (md->val[i].padata_type == type)
846 static struct pa_info_data *
847 process_pa_info(krb5_context context,
848 const krb5_principal client,
850 struct pa_info_data *paid,
853 struct pa_info_data *p = NULL;
856 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
857 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
860 paid->salt.salttype = pa_prefs[i].type;
861 p = (*pa_prefs[i].salt_info)(context, client, asreq,
862 paid, &pa->padata_value);
867 static krb5_error_code
868 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
869 krb5_enctype etype, krb5_keyblock *key)
875 EncryptedData encdata;
881 krb5_us_timeofday (context, &p.patimestamp, &usec);
885 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
889 krb5_abortx(context, "internal error in ASN.1 encoder");
891 ret = krb5_crypto_init(context, key, 0, &crypto);
896 ret = krb5_encrypt_EncryptedData(context,
898 KRB5_KU_PA_ENC_TIMESTAMP,
904 krb5_crypto_destroy(context, crypto);
908 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
909 free_EncryptedData(&encdata);
913 krb5_abortx(context, "internal error in ASN.1 encoder");
915 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
921 static krb5_error_code
922 add_enc_ts_padata(krb5_context context,
924 krb5_principal client,
925 krb5_s2k_proc key_proc,
926 krb5_const_pointer keyseed,
927 krb5_enctype *enctypes,
930 krb5_data *s2kparams)
938 /* default to standard salt */
939 ret = krb5_get_pw_salt (context, client, &salt2);
943 enctypes = context->etypes;
945 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
949 for (i = 0; i < netypes; ++i) {
952 ret = (*key_proc)(context, enctypes[i], keyseed,
953 *salt, s2kparams, &key);
956 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
957 krb5_free_keyblock (context, key);
962 krb5_free_salt(context, salt2);
966 static krb5_error_code
967 pa_data_to_md_ts_enc(krb5_context context,
969 const krb5_principal client,
970 krb5_get_init_creds_ctx *ctx,
971 struct pa_info_data *ppaid,
974 if (ctx->key_proc == NULL || ctx->password == NULL)
978 add_enc_ts_padata(context, md, client,
979 ctx->key_proc, ctx->password,
981 &ppaid->salt, ppaid->s2kparams);
985 /* make a v5 salted pa-data */
986 add_enc_ts_padata(context, md, client,
987 ctx->key_proc, ctx->password,
988 a->req_body.etype.val, a->req_body.etype.len,
991 /* make a v4 salted pa-data */
992 salt.salttype = KRB5_PW_SALT;
993 krb5_data_zero(&salt.saltvalue);
994 add_enc_ts_padata(context, md, client,
995 ctx->key_proc, ctx->password,
996 a->req_body.etype.val, a->req_body.etype.len,
1002 static krb5_error_code
1003 pa_data_to_key_plain(krb5_context context,
1004 const krb5_principal client,
1005 krb5_get_init_creds_ctx *ctx,
1007 krb5_data *s2kparams,
1009 krb5_keyblock **key)
1011 krb5_error_code ret;
1013 ret = (*ctx->key_proc)(context, etype, ctx->password,
1014 salt, s2kparams, key);
1019 static krb5_error_code
1020 pa_data_to_md_pkinit(krb5_context context,
1022 const krb5_principal client,
1023 krb5_get_init_creds_ctx *ctx,
1026 if (ctx->pk_init_ctx == NULL)
1029 return _krb5_pk_mk_padata(context,
1035 krb5_set_error_string(context, "no support for PKINIT compiled in");
1040 static krb5_error_code
1041 pa_data_add_pac_request(krb5_context context,
1042 krb5_get_init_creds_ctx *ctx,
1046 krb5_error_code ret;
1050 switch (ctx->req_pac) {
1051 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1052 return 0; /* don't bother */
1053 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1054 req.include_pac = 1;
1056 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1057 req.include_pac = 0;
1060 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1065 krb5_abortx(context, "internal error in ASN.1 encoder");
1067 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1075 * Assumes caller always will free `out_md', even on error.
1078 static krb5_error_code
1079 process_pa_data_to_md(krb5_context context,
1080 const krb5_creds *creds,
1082 krb5_get_init_creds_ctx *ctx,
1084 METHOD_DATA **out_md,
1085 krb5_prompter_fct prompter,
1086 void *prompter_data)
1088 krb5_error_code ret;
1091 if (*out_md == NULL) {
1092 krb5_set_error_string(context, "malloc: out of memory");
1096 (*out_md)->val = NULL;
1099 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1100 * need to expose our password protecting our PKCS12 key.
1103 if (ctx->pk_init_ctx) {
1105 ret = pa_data_to_md_pkinit(context, a, creds->client, ctx, *out_md);
1109 } else if (in_md->len != 0) {
1110 struct pa_info_data paid, *ppaid;
1112 memset(&paid, 0, sizeof(paid));
1114 paid.etype = ENCTYPE_NULL;
1115 ppaid = process_pa_info(context, creds->client, a, &paid, in_md);
1117 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1119 free_paid(context, ppaid);
1122 pa_data_add_pac_request(context, ctx, *out_md);
1124 if ((*out_md)->len == 0) {
1132 static krb5_error_code
1133 process_pa_data_to_key(krb5_context context,
1134 krb5_get_init_creds_ctx *ctx,
1138 const krb5_krbhst_info *hi,
1139 krb5_keyblock **key)
1141 struct pa_info_data paid, *ppaid = NULL;
1142 krb5_error_code ret;
1146 memset(&paid, 0, sizeof(paid));
1148 etype = rep->kdc_rep.enc_part.etype;
1150 if (rep->kdc_rep.padata) {
1152 ppaid = process_pa_info(context, creds->client, a, &paid,
1153 rep->kdc_rep.padata);
1155 if (ppaid == NULL) {
1156 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1160 paid.s2kparams = NULL;
1164 if (rep->kdc_rep.padata) {
1166 pa = krb5_find_padata(rep->kdc_rep.padata->val,
1167 rep->kdc_rep.padata->len,
1168 KRB5_PADATA_PK_AS_REP,
1172 pa = krb5_find_padata(rep->kdc_rep.padata->val,
1173 rep->kdc_rep.padata->len,
1174 KRB5_PADATA_PK_AS_REP_19,
1178 if (pa && ctx->pk_init_ctx) {
1180 ret = _krb5_pk_rd_pa_reply(context,
1190 krb5_set_error_string(context, "no support for PKINIT compiled in");
1193 } else if (ctx->password)
1194 ret = pa_data_to_key_plain(context, creds->client, ctx,
1195 paid.salt, paid.s2kparams, etype, key);
1197 krb5_set_error_string(context, "No usable pa data type");
1201 free_paid(context, &paid);
1205 static krb5_error_code
1206 init_cred_loop(krb5_context context,
1207 krb5_get_init_creds_opt *init_cred_opts,
1208 const krb5_prompter_fct prompter,
1209 void *prompter_data,
1210 krb5_get_init_creds_ctx *ctx,
1212 krb5_kdc_rep *ret_as_reply)
1214 krb5_error_code ret;
1220 krb5_krbhst_info *hi = NULL;
1221 krb5_sendto_ctx stctx = NULL;
1224 memset(&md, 0, sizeof(md));
1225 memset(&rep, 0, sizeof(rep));
1227 _krb5_get_init_creds_opt_free_krb5_error(init_cred_opts);
1230 memset(ret_as_reply, 0, sizeof(*ret_as_reply));
1232 ret = init_creds_init_as_req(context, ctx->flags, creds,
1233 ctx->addrs, ctx->etypes, &ctx->as_req);
1237 ret = krb5_sendto_ctx_alloc(context, &stctx);
1240 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1242 /* Set a new nonce. */
1243 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1244 ctx->nonce &= 0xffffffff;
1245 /* XXX these just needs to be the same when using Windows PK-INIT */
1246 ctx->pk_nonce = ctx->nonce;
1249 * Increase counter when we want other pre-auth types then
1250 * KRB5_PA_ENC_TIMESTAMP.
1252 #define MAX_PA_COUNTER 3
1254 ctx->pa_counter = 0;
1255 while (ctx->pa_counter < MAX_PA_COUNTER) {
1259 if (ctx->as_req.padata) {
1260 free_METHOD_DATA(ctx->as_req.padata);
1261 free(ctx->as_req.padata);
1262 ctx->as_req.padata = NULL;
1265 /* Set a new nonce. */
1266 ctx->as_req.req_body.nonce = ctx->nonce;
1268 /* fill_in_md_data */
1269 ret = process_pa_data_to_md(context, creds, &ctx->as_req, ctx,
1270 &md, &ctx->as_req.padata,
1271 prompter, prompter_data);
1275 krb5_data_free(&ctx->req_buffer);
1277 ASN1_MALLOC_ENCODE(AS_REQ,
1278 ctx->req_buffer.data, ctx->req_buffer.length,
1279 &ctx->as_req, &len, ret);
1282 if(len != ctx->req_buffer.length)
1283 krb5_abortx(context, "internal error in ASN.1 encoder");
1285 ret = krb5_sendto_context (context, stctx, &ctx->req_buffer,
1286 creds->client->realm, &resp);
1290 memset (&rep, 0, sizeof(rep));
1291 ret = decode_AS_REP(resp.data, resp.length, &rep.kdc_rep, &size);
1293 krb5_data_free(&resp);
1294 krb5_clear_error_string(context);
1297 /* let's try to parse it as a KRB-ERROR */
1300 ret = krb5_rd_error(context, &resp, &error);
1301 if(ret && resp.data && ((char*)resp.data)[0] == 4)
1302 ret = KRB5KRB_AP_ERR_V4_REPLY;
1303 krb5_data_free(&resp);
1307 ret = krb5_error_from_rd_error(context, &error, creds);
1310 * If no preauth was set and KDC requires it, give it one
1314 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1315 free_METHOD_DATA(&md);
1316 memset(&md, 0, sizeof(md));
1319 ret = decode_METHOD_DATA(error.e_data->data,
1320 error.e_data->length,
1324 krb5_set_error_string(context,
1325 "failed to decode METHOD DATA");
1327 /* XXX guess what the server want here add add md */
1329 krb5_free_error_contents(context, &error);
1333 _krb5_get_init_creds_opt_set_krb5_error(context,
1339 krb5_free_error_contents(context, &error);
1346 krb5_keyblock *key = NULL;
1349 if (ctx->flags.request_anonymous)
1350 flags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1351 if (ctx->flags.canonicalize) {
1352 flags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1353 flags |= EXTRACT_TICKET_MATCH_REALM;
1355 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1356 flags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1358 ret = process_pa_data_to_key(context, ctx, creds,
1359 &ctx->as_req, &rep, hi, &key);
1363 ret = _krb5_extract_ticket(context,
1368 KRB5_KU_AS_REP_ENC_PART,
1374 krb5_free_keyblock(context, key);
1378 krb5_sendto_ctx_free(context, stctx);
1379 krb5_data_free(&ctx->req_buffer);
1380 free_METHOD_DATA(&md);
1381 memset(&md, 0, sizeof(md));
1383 if (ret == 0 && ret_as_reply)
1384 *ret_as_reply = rep;
1386 krb5_free_kdc_rep (context, &rep);
1390 krb5_error_code KRB5_LIB_FUNCTION
1391 krb5_get_init_creds(krb5_context context,
1393 krb5_principal client,
1394 krb5_prompter_fct prompter,
1396 krb5_deltat start_time,
1397 const char *in_tkt_service,
1398 krb5_get_init_creds_opt *options)
1400 krb5_get_init_creds_ctx ctx;
1401 krb5_kdc_rep kdc_reply;
1402 krb5_error_code ret;
1406 memset(&kdc_reply, 0, sizeof(kdc_reply));
1408 ret = get_init_creds_common(context, client, start_time,
1409 in_tkt_service, options, &ctx);
1415 memset(&kdc_reply, 0, sizeof(kdc_reply));
1417 ret = init_cred_loop(context,
1429 case KRB5KDC_ERR_KEY_EXPIRED :
1430 /* try to avoid recursion */
1432 /* don't try to change password where then where none */
1433 if (prompter == NULL || ctx.password == NULL)
1436 krb5_clear_error_string (context);
1438 if (ctx.in_tkt_service != NULL
1439 && strcmp (ctx.in_tkt_service, "kadmin/changepw") == 0)
1442 ret = change_password (context,
1460 print_expire (context,
1461 krb5_principal_get_realm (context, ctx.cred.client),
1467 memset (buf, 0, sizeof(buf));
1468 free_init_creds_ctx(context, &ctx);
1469 krb5_free_kdc_rep (context, &kdc_reply);
1473 krb5_free_cred_contents (context, &ctx.cred);
1478 krb5_error_code KRB5_LIB_FUNCTION
1479 krb5_get_init_creds_password(krb5_context context,
1481 krb5_principal client,
1482 const char *password,
1483 krb5_prompter_fct prompter,
1485 krb5_deltat start_time,
1486 const char *in_tkt_service,
1487 krb5_get_init_creds_opt *in_options)
1489 krb5_get_init_creds_opt *options;
1491 krb5_error_code ret;
1493 if (in_options == NULL) {
1494 const char *realm = krb5_principal_get_realm(context, client);
1495 ret = krb5_get_init_creds_opt_alloc(context, &options);
1497 krb5_get_init_creds_opt_set_default_flags(context,
1502 ret = _krb5_get_init_creds_opt_copy(context, in_options, &options);
1506 if (password == NULL &&
1507 options->opt_private->password == NULL &&
1508 options->opt_private->pk_init_ctx == NULL)
1511 krb5_data password_data;
1514 krb5_unparse_name (context, client, &p);
1515 asprintf (&q, "%s's Password: ", p);
1518 password_data.data = buf;
1519 password_data.length = sizeof(buf);
1521 prompt.reply = &password_data;
1522 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1524 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1527 memset (buf, 0, sizeof(buf));
1528 krb5_get_init_creds_opt_free(context, options);
1529 ret = KRB5_LIBOS_PWDINTR;
1530 krb5_clear_error_string (context);
1533 password = password_data.data;
1536 if (options->opt_private->password == NULL) {
1537 ret = krb5_get_init_creds_opt_set_pa_password(context, options,
1540 krb5_get_init_creds_opt_free(context, options);
1541 memset(buf, 0, sizeof(buf));
1546 ret = krb5_get_init_creds(context, creds, client, prompter,
1547 data, start_time, in_tkt_service, options);
1548 krb5_get_init_creds_opt_free(context, options);
1549 memset(buf, 0, sizeof(buf));
1553 static krb5_error_code
1554 init_creds_keyblock_key_proc (krb5_context context,
1557 krb5_const_pointer keyseed,
1558 krb5_keyblock **key)
1560 return krb5_copy_keyblock (context, keyseed, key);
1563 krb5_error_code KRB5_LIB_FUNCTION
1564 krb5_get_init_creds_keyblock(krb5_context context,
1566 krb5_principal client,
1567 krb5_keyblock *keyblock,
1568 krb5_deltat start_time,
1569 const char *in_tkt_service,
1570 krb5_get_init_creds_opt *options)
1572 struct krb5_get_init_creds_ctx ctx;
1573 krb5_error_code ret;
1575 ret = get_init_creds_common(context, client, start_time,
1576 in_tkt_service, options, &ctx);
1580 ret = krb5_get_in_cred (context,
1581 KDCOptions2int(ctx.flags),
1586 init_creds_keyblock_key_proc,
1593 if (ret == 0 && creds)
1596 krb5_free_cred_contents (context, &ctx.cred);
1599 free_init_creds_ctx(context, &ctx);